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, UnderstandsMacros) { 1799 verifyFormat("#define A (parentheses)"); 1800 verifyFormat("#define true ((int)1)"); 1801 verifyFormat("#define and(x)"); 1802 verifyFormat("#define if(x) x"); 1803 verifyFormat("#define return(x) (x)"); 1804 verifyFormat("#define while(x) for (; x;)"); 1805 verifyFormat("#define xor(x) (^(x))"); 1806 verifyFormat("#define __except(x)"); 1807 verifyFormat("#define __try(x)"); 1808 1809 FormatStyle Style = getLLVMStyle(); 1810 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1811 Style.BraceWrapping.AfterFunction = true; 1812 // Test that a macro definition never gets merged with the following 1813 // definition. 1814 // FIXME: The AAA macro definition probably should not be split into 3 lines. 1815 verifyFormat("#define AAA " 1816 " \\\n" 1817 " N " 1818 " \\\n" 1819 " {\n" 1820 "#define BBB }\n", 1821 Style); 1822 // verifyFormat("#define AAA N { //\n", Style); 1823 } 1824 1825 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) { 1826 FormatStyle Style = getLLVMStyleWithColumns(60); 1827 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 1828 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; 1829 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 1830 EXPECT_EQ("#define A \\\n" 1831 " if (HANDLEwernufrnuLwrmviferuvnierv) \\\n" 1832 " { \\\n" 1833 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" 1834 " }\n" 1835 "X;", 1836 format("#define A \\\n" 1837 " if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n" 1838 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" 1839 " }\n" 1840 "X;", 1841 Style)); 1842 } 1843 1844 TEST_F(FormatTest, ParseIfElse) { 1845 verifyFormat("if (true)\n" 1846 " if (true)\n" 1847 " if (true)\n" 1848 " f();\n" 1849 " else\n" 1850 " g();\n" 1851 " else\n" 1852 " h();\n" 1853 "else\n" 1854 " i();"); 1855 verifyFormat("if (true)\n" 1856 " if (true)\n" 1857 " if (true) {\n" 1858 " if (true)\n" 1859 " f();\n" 1860 " } else {\n" 1861 " g();\n" 1862 " }\n" 1863 " else\n" 1864 " h();\n" 1865 "else {\n" 1866 " i();\n" 1867 "}"); 1868 verifyFormat("if (true)\n" 1869 " if constexpr (true)\n" 1870 " if (true) {\n" 1871 " if constexpr (true)\n" 1872 " f();\n" 1873 " } else {\n" 1874 " g();\n" 1875 " }\n" 1876 " else\n" 1877 " h();\n" 1878 "else {\n" 1879 " i();\n" 1880 "}"); 1881 verifyFormat("if (true)\n" 1882 " if CONSTEXPR (true)\n" 1883 " if (true) {\n" 1884 " if CONSTEXPR (true)\n" 1885 " f();\n" 1886 " } else {\n" 1887 " g();\n" 1888 " }\n" 1889 " else\n" 1890 " h();\n" 1891 "else {\n" 1892 " i();\n" 1893 "}"); 1894 verifyFormat("void f() {\n" 1895 " if (a) {\n" 1896 " } else {\n" 1897 " }\n" 1898 "}"); 1899 } 1900 1901 TEST_F(FormatTest, ElseIf) { 1902 verifyFormat("if (a) {\n} else if (b) {\n}"); 1903 verifyFormat("if (a)\n" 1904 " f();\n" 1905 "else if (b)\n" 1906 " g();\n" 1907 "else\n" 1908 " h();"); 1909 verifyFormat("if (a)\n" 1910 " f();\n" 1911 "else // comment\n" 1912 " if (b) {\n" 1913 " g();\n" 1914 " h();\n" 1915 " }"); 1916 verifyFormat("if constexpr (a)\n" 1917 " f();\n" 1918 "else if constexpr (b)\n" 1919 " g();\n" 1920 "else\n" 1921 " h();"); 1922 verifyFormat("if CONSTEXPR (a)\n" 1923 " f();\n" 1924 "else if CONSTEXPR (b)\n" 1925 " g();\n" 1926 "else\n" 1927 " h();"); 1928 verifyFormat("if (a) {\n" 1929 " f();\n" 1930 "}\n" 1931 "// or else ..\n" 1932 "else {\n" 1933 " g()\n" 1934 "}"); 1935 1936 verifyFormat("if (a) {\n" 1937 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1938 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 1939 "}"); 1940 verifyFormat("if (a) {\n" 1941 "} else if constexpr (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1942 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 1943 "}"); 1944 verifyFormat("if (a) {\n" 1945 "} else if CONSTEXPR (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1946 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 1947 "}"); 1948 verifyFormat("if (a) {\n" 1949 "} else if (\n" 1950 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 1951 "}", 1952 getLLVMStyleWithColumns(62)); 1953 verifyFormat("if (a) {\n" 1954 "} else if constexpr (\n" 1955 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 1956 "}", 1957 getLLVMStyleWithColumns(62)); 1958 verifyFormat("if (a) {\n" 1959 "} else if CONSTEXPR (\n" 1960 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 1961 "}", 1962 getLLVMStyleWithColumns(62)); 1963 } 1964 1965 TEST_F(FormatTest, SeparatePointerReferenceAlignment) { 1966 FormatStyle Style = getLLVMStyle(); 1967 // Check first the default LLVM style 1968 // Style.PointerAlignment = FormatStyle::PAS_Right; 1969 // Style.ReferenceAlignment = FormatStyle::RAS_Pointer; 1970 verifyFormat("int *f1(int *a, int &b, int &&c);", Style); 1971 verifyFormat("int &f2(int &&c, int *a, int &b);", Style); 1972 verifyFormat("int &&f3(int &b, int &&c, int *a);", Style); 1973 verifyFormat("int *f1(int &a) const &;", Style); 1974 verifyFormat("int *f1(int &a) const & = 0;", Style); 1975 verifyFormat("int *a = f1();", Style); 1976 verifyFormat("int &b = f2();", Style); 1977 verifyFormat("int &&c = f3();", Style); 1978 verifyFormat("for (auto a = 0, b = 0; const auto &c : {1, 2, 3})", Style); 1979 verifyFormat("for (auto a = 0, b = 0; const int &c : {1, 2, 3})", Style); 1980 verifyFormat("for (auto a = 0, b = 0; const Foo &c : {1, 2, 3})", Style); 1981 verifyFormat("for (auto a = 0, b = 0; const Foo *c : {1, 2, 3})", Style); 1982 verifyFormat("for (int a = 0, b = 0; const auto &c : {1, 2, 3})", Style); 1983 verifyFormat("for (int a = 0, b = 0; const int &c : {1, 2, 3})", Style); 1984 verifyFormat("for (int a = 0, b = 0; const Foo &c : {1, 2, 3})", Style); 1985 verifyFormat("for (int a = 0, b++; const auto &c : {1, 2, 3})", Style); 1986 verifyFormat("for (int a = 0, b++; const int &c : {1, 2, 3})", Style); 1987 verifyFormat("for (int a = 0, b++; const Foo &c : {1, 2, 3})", Style); 1988 verifyFormat("for (auto x = 0; auto &c : {1, 2, 3})", Style); 1989 verifyFormat("for (auto x = 0; int &c : {1, 2, 3})", Style); 1990 verifyFormat("for (int x = 0; auto &c : {1, 2, 3})", Style); 1991 verifyFormat("for (int x = 0; int &c : {1, 2, 3})", Style); 1992 verifyFormat("for (f(); auto &c : {1, 2, 3})", Style); 1993 verifyFormat("for (f(); int &c : {1, 2, 3})", Style); 1994 1995 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 1996 verifyFormat("Const unsigned int *c;\n" 1997 "const unsigned int *d;\n" 1998 "Const unsigned int &e;\n" 1999 "const unsigned int &f;\n" 2000 "const unsigned &&g;\n" 2001 "Const unsigned h;", 2002 Style); 2003 2004 Style.PointerAlignment = FormatStyle::PAS_Left; 2005 Style.ReferenceAlignment = FormatStyle::RAS_Pointer; 2006 verifyFormat("int* f1(int* a, int& b, int&& c);", Style); 2007 verifyFormat("int& f2(int&& c, int* a, int& b);", Style); 2008 verifyFormat("int&& f3(int& b, int&& c, int* a);", Style); 2009 verifyFormat("int* f1(int& a) const& = 0;", Style); 2010 verifyFormat("int* a = f1();", Style); 2011 verifyFormat("int& b = f2();", Style); 2012 verifyFormat("int&& c = f3();", Style); 2013 verifyFormat("for (auto a = 0, b = 0; const auto& c : {1, 2, 3})", Style); 2014 verifyFormat("for (auto a = 0, b = 0; const int& c : {1, 2, 3})", Style); 2015 verifyFormat("for (auto a = 0, b = 0; const Foo& c : {1, 2, 3})", Style); 2016 verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style); 2017 verifyFormat("for (int a = 0, b = 0; const auto& c : {1, 2, 3})", Style); 2018 verifyFormat("for (int a = 0, b = 0; const int& c : {1, 2, 3})", Style); 2019 verifyFormat("for (int a = 0, b = 0; const Foo& c : {1, 2, 3})", Style); 2020 verifyFormat("for (int a = 0, b = 0; const Foo* c : {1, 2, 3})", Style); 2021 verifyFormat("for (int a = 0, b++; const auto& c : {1, 2, 3})", Style); 2022 verifyFormat("for (int a = 0, b++; const int& c : {1, 2, 3})", Style); 2023 verifyFormat("for (int a = 0, b++; const Foo& c : {1, 2, 3})", Style); 2024 verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style); 2025 verifyFormat("for (auto x = 0; auto& c : {1, 2, 3})", Style); 2026 verifyFormat("for (auto x = 0; int& c : {1, 2, 3})", Style); 2027 verifyFormat("for (int x = 0; auto& c : {1, 2, 3})", Style); 2028 verifyFormat("for (int x = 0; int& c : {1, 2, 3})", Style); 2029 verifyFormat("for (f(); auto& c : {1, 2, 3})", Style); 2030 verifyFormat("for (f(); int& c : {1, 2, 3})", Style); 2031 2032 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 2033 verifyFormat("Const unsigned int* c;\n" 2034 "const unsigned int* d;\n" 2035 "Const unsigned int& e;\n" 2036 "const unsigned int& f;\n" 2037 "const unsigned&& g;\n" 2038 "Const unsigned h;", 2039 Style); 2040 2041 Style.PointerAlignment = FormatStyle::PAS_Right; 2042 Style.ReferenceAlignment = FormatStyle::RAS_Left; 2043 verifyFormat("int *f1(int *a, int& b, int&& c);", Style); 2044 verifyFormat("int& f2(int&& c, int *a, int& b);", Style); 2045 verifyFormat("int&& f3(int& b, int&& c, int *a);", Style); 2046 verifyFormat("int *a = f1();", Style); 2047 verifyFormat("int& b = f2();", Style); 2048 verifyFormat("int&& c = f3();", Style); 2049 verifyFormat("for (auto a = 0, b = 0; const Foo *c : {1, 2, 3})", Style); 2050 verifyFormat("for (int a = 0, b = 0; const Foo *c : {1, 2, 3})", Style); 2051 verifyFormat("for (int a = 0, b++; const Foo *c : {1, 2, 3})", Style); 2052 2053 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 2054 verifyFormat("Const unsigned int *c;\n" 2055 "const unsigned int *d;\n" 2056 "Const unsigned int& e;\n" 2057 "const unsigned int& f;\n" 2058 "const unsigned g;\n" 2059 "Const unsigned h;", 2060 Style); 2061 2062 Style.PointerAlignment = FormatStyle::PAS_Left; 2063 Style.ReferenceAlignment = FormatStyle::RAS_Middle; 2064 verifyFormat("int* f1(int* a, int & b, int && c);", Style); 2065 verifyFormat("int & f2(int && c, int* a, int & b);", Style); 2066 verifyFormat("int && f3(int & b, int && c, int* a);", Style); 2067 verifyFormat("int* a = f1();", Style); 2068 verifyFormat("int & b = f2();", Style); 2069 verifyFormat("int && c = f3();", Style); 2070 verifyFormat("for (auto a = 0, b = 0; const auto & c : {1, 2, 3})", Style); 2071 verifyFormat("for (auto a = 0, b = 0; const int & c : {1, 2, 3})", Style); 2072 verifyFormat("for (auto a = 0, b = 0; const Foo & c : {1, 2, 3})", Style); 2073 verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style); 2074 verifyFormat("for (int a = 0, b++; const auto & c : {1, 2, 3})", Style); 2075 verifyFormat("for (int a = 0, b++; const int & c : {1, 2, 3})", Style); 2076 verifyFormat("for (int a = 0, b++; const Foo & c : {1, 2, 3})", Style); 2077 verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style); 2078 verifyFormat("for (auto x = 0; auto & c : {1, 2, 3})", Style); 2079 verifyFormat("for (auto x = 0; int & c : {1, 2, 3})", Style); 2080 verifyFormat("for (int x = 0; auto & c : {1, 2, 3})", Style); 2081 verifyFormat("for (int x = 0; int & c : {1, 2, 3})", Style); 2082 verifyFormat("for (f(); auto & c : {1, 2, 3})", Style); 2083 verifyFormat("for (f(); int & c : {1, 2, 3})", Style); 2084 2085 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 2086 verifyFormat("Const unsigned int* c;\n" 2087 "const unsigned int* d;\n" 2088 "Const unsigned int & e;\n" 2089 "const unsigned int & f;\n" 2090 "const unsigned && g;\n" 2091 "Const unsigned h;", 2092 Style); 2093 2094 Style.PointerAlignment = FormatStyle::PAS_Middle; 2095 Style.ReferenceAlignment = FormatStyle::RAS_Right; 2096 verifyFormat("int * f1(int * a, int &b, int &&c);", Style); 2097 verifyFormat("int &f2(int &&c, int * a, int &b);", Style); 2098 verifyFormat("int &&f3(int &b, int &&c, int * a);", Style); 2099 verifyFormat("int * a = f1();", Style); 2100 verifyFormat("int &b = f2();", Style); 2101 verifyFormat("int &&c = f3();", Style); 2102 verifyFormat("for (auto a = 0, b = 0; const Foo * c : {1, 2, 3})", Style); 2103 verifyFormat("for (int a = 0, b = 0; const Foo * c : {1, 2, 3})", Style); 2104 verifyFormat("for (int a = 0, b++; const Foo * c : {1, 2, 3})", Style); 2105 2106 // FIXME: we don't handle this yet, so output may be arbitrary until it's 2107 // specifically handled 2108 // verifyFormat("int Add2(BTree * &Root, char * szToAdd)", Style); 2109 } 2110 2111 TEST_F(FormatTest, FormatsForLoop) { 2112 verifyFormat( 2113 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 2114 " ++VeryVeryLongLoopVariable)\n" 2115 " ;"); 2116 verifyFormat("for (;;)\n" 2117 " f();"); 2118 verifyFormat("for (;;) {\n}"); 2119 verifyFormat("for (;;) {\n" 2120 " f();\n" 2121 "}"); 2122 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}"); 2123 2124 verifyFormat( 2125 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 2126 " E = UnwrappedLines.end();\n" 2127 " I != E; ++I) {\n}"); 2128 2129 verifyFormat( 2130 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" 2131 " ++IIIII) {\n}"); 2132 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" 2133 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" 2134 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); 2135 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n" 2136 " I = FD->getDeclsInPrototypeScope().begin(),\n" 2137 " E = FD->getDeclsInPrototypeScope().end();\n" 2138 " I != E; ++I) {\n}"); 2139 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n" 2140 " I = Container.begin(),\n" 2141 " E = Container.end();\n" 2142 " I != E; ++I) {\n}", 2143 getLLVMStyleWithColumns(76)); 2144 2145 verifyFormat( 2146 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 2147 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" 2148 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2149 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 2150 " ++aaaaaaaaaaa) {\n}"); 2151 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2152 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n" 2153 " ++i) {\n}"); 2154 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" 2155 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 2156 "}"); 2157 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n" 2158 " aaaaaaaaaa);\n" 2159 " iter; ++iter) {\n" 2160 "}"); 2161 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2162 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 2163 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n" 2164 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {"); 2165 2166 // These should not be formatted as Objective-C for-in loops. 2167 verifyFormat("for (Foo *x = 0; x != in; x++) {\n}"); 2168 verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}"); 2169 verifyFormat("Foo *x;\nfor (x in y) {\n}"); 2170 verifyFormat( 2171 "for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}"); 2172 2173 FormatStyle NoBinPacking = getLLVMStyle(); 2174 NoBinPacking.BinPackParameters = false; 2175 verifyFormat("for (int aaaaaaaaaaa = 1;\n" 2176 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" 2177 " aaaaaaaaaaaaaaaa,\n" 2178 " aaaaaaaaaaaaaaaa,\n" 2179 " aaaaaaaaaaaaaaaa);\n" 2180 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 2181 "}", 2182 NoBinPacking); 2183 verifyFormat( 2184 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 2185 " E = UnwrappedLines.end();\n" 2186 " I != E;\n" 2187 " ++I) {\n}", 2188 NoBinPacking); 2189 2190 FormatStyle AlignLeft = getLLVMStyle(); 2191 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 2192 verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft); 2193 } 2194 2195 TEST_F(FormatTest, RangeBasedForLoops) { 2196 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 2197 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 2198 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" 2199 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); 2200 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" 2201 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 2202 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n" 2203 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}"); 2204 } 2205 2206 TEST_F(FormatTest, ForEachLoops) { 2207 FormatStyle Style = getLLVMStyle(); 2208 EXPECT_EQ(Style.AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 2209 EXPECT_EQ(Style.AllowShortLoopsOnASingleLine, false); 2210 verifyFormat("void f() {\n" 2211 " for (;;) {\n" 2212 " }\n" 2213 " foreach (Item *item, itemlist) {\n" 2214 " }\n" 2215 " Q_FOREACH (Item *item, itemlist) {\n" 2216 " }\n" 2217 " BOOST_FOREACH (Item *item, itemlist) {\n" 2218 " }\n" 2219 " UNKNOWN_FOREACH(Item * item, itemlist) {}\n" 2220 "}", 2221 Style); 2222 verifyFormat("void f() {\n" 2223 " for (;;)\n" 2224 " int j = 1;\n" 2225 " Q_FOREACH (int v, vec)\n" 2226 " v *= 2;\n" 2227 " for (;;) {\n" 2228 " int j = 1;\n" 2229 " }\n" 2230 " Q_FOREACH (int v, vec) {\n" 2231 " v *= 2;\n" 2232 " }\n" 2233 "}", 2234 Style); 2235 2236 FormatStyle ShortBlocks = getLLVMStyle(); 2237 ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 2238 EXPECT_EQ(ShortBlocks.AllowShortLoopsOnASingleLine, false); 2239 verifyFormat("void f() {\n" 2240 " for (;;)\n" 2241 " int j = 1;\n" 2242 " Q_FOREACH (int &v, vec)\n" 2243 " v *= 2;\n" 2244 " for (;;) {\n" 2245 " int j = 1;\n" 2246 " }\n" 2247 " Q_FOREACH (int &v, vec) {\n" 2248 " int j = 1;\n" 2249 " }\n" 2250 "}", 2251 ShortBlocks); 2252 2253 FormatStyle ShortLoops = getLLVMStyle(); 2254 ShortLoops.AllowShortLoopsOnASingleLine = true; 2255 EXPECT_EQ(ShortLoops.AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 2256 verifyFormat("void f() {\n" 2257 " for (;;) int j = 1;\n" 2258 " Q_FOREACH (int &v, vec) int j = 1;\n" 2259 " for (;;) {\n" 2260 " int j = 1;\n" 2261 " }\n" 2262 " Q_FOREACH (int &v, vec) {\n" 2263 " int j = 1;\n" 2264 " }\n" 2265 "}", 2266 ShortLoops); 2267 2268 FormatStyle ShortBlocksAndLoops = getLLVMStyle(); 2269 ShortBlocksAndLoops.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 2270 ShortBlocksAndLoops.AllowShortLoopsOnASingleLine = true; 2271 verifyFormat("void f() {\n" 2272 " for (;;) int j = 1;\n" 2273 " Q_FOREACH (int &v, vec) int j = 1;\n" 2274 " for (;;) { int j = 1; }\n" 2275 " Q_FOREACH (int &v, vec) { int j = 1; }\n" 2276 "}", 2277 ShortBlocksAndLoops); 2278 2279 Style.SpaceBeforeParens = 2280 FormatStyle::SBPO_ControlStatementsExceptControlMacros; 2281 verifyFormat("void f() {\n" 2282 " for (;;) {\n" 2283 " }\n" 2284 " foreach(Item *item, itemlist) {\n" 2285 " }\n" 2286 " Q_FOREACH(Item *item, itemlist) {\n" 2287 " }\n" 2288 " BOOST_FOREACH(Item *item, itemlist) {\n" 2289 " }\n" 2290 " UNKNOWN_FOREACH(Item * item, itemlist) {}\n" 2291 "}", 2292 Style); 2293 2294 // As function-like macros. 2295 verifyFormat("#define foreach(x, y)\n" 2296 "#define Q_FOREACH(x, y)\n" 2297 "#define BOOST_FOREACH(x, y)\n" 2298 "#define UNKNOWN_FOREACH(x, y)\n"); 2299 2300 // Not as function-like macros. 2301 verifyFormat("#define foreach (x, y)\n" 2302 "#define Q_FOREACH (x, y)\n" 2303 "#define BOOST_FOREACH (x, y)\n" 2304 "#define UNKNOWN_FOREACH (x, y)\n"); 2305 2306 // handle microsoft non standard extension 2307 verifyFormat("for each (char c in x->MyStringProperty)"); 2308 } 2309 2310 TEST_F(FormatTest, FormatsWhileLoop) { 2311 verifyFormat("while (true) {\n}"); 2312 verifyFormat("while (true)\n" 2313 " f();"); 2314 verifyFormat("while () {\n}"); 2315 verifyFormat("while () {\n" 2316 " f();\n" 2317 "}"); 2318 } 2319 2320 TEST_F(FormatTest, FormatsDoWhile) { 2321 verifyFormat("do {\n" 2322 " do_something();\n" 2323 "} while (something());"); 2324 verifyFormat("do\n" 2325 " do_something();\n" 2326 "while (something());"); 2327 } 2328 2329 TEST_F(FormatTest, FormatsSwitchStatement) { 2330 verifyFormat("switch (x) {\n" 2331 "case 1:\n" 2332 " f();\n" 2333 " break;\n" 2334 "case kFoo:\n" 2335 "case ns::kBar:\n" 2336 "case kBaz:\n" 2337 " break;\n" 2338 "default:\n" 2339 " g();\n" 2340 " break;\n" 2341 "}"); 2342 verifyFormat("switch (x) {\n" 2343 "case 1: {\n" 2344 " f();\n" 2345 " break;\n" 2346 "}\n" 2347 "case 2: {\n" 2348 " break;\n" 2349 "}\n" 2350 "}"); 2351 verifyFormat("switch (x) {\n" 2352 "case 1: {\n" 2353 " f();\n" 2354 " {\n" 2355 " g();\n" 2356 " h();\n" 2357 " }\n" 2358 " break;\n" 2359 "}\n" 2360 "}"); 2361 verifyFormat("switch (x) {\n" 2362 "case 1: {\n" 2363 " f();\n" 2364 " if (foo) {\n" 2365 " g();\n" 2366 " h();\n" 2367 " }\n" 2368 " break;\n" 2369 "}\n" 2370 "}"); 2371 verifyFormat("switch (x) {\n" 2372 "case 1: {\n" 2373 " f();\n" 2374 " g();\n" 2375 "} break;\n" 2376 "}"); 2377 verifyFormat("switch (test)\n" 2378 " ;"); 2379 verifyFormat("switch (x) {\n" 2380 "default: {\n" 2381 " // Do nothing.\n" 2382 "}\n" 2383 "}"); 2384 verifyFormat("switch (x) {\n" 2385 "// comment\n" 2386 "// if 1, do f()\n" 2387 "case 1:\n" 2388 " f();\n" 2389 "}"); 2390 verifyFormat("switch (x) {\n" 2391 "case 1:\n" 2392 " // Do amazing stuff\n" 2393 " {\n" 2394 " f();\n" 2395 " g();\n" 2396 " }\n" 2397 " break;\n" 2398 "}"); 2399 verifyFormat("#define A \\\n" 2400 " switch (x) { \\\n" 2401 " case a: \\\n" 2402 " foo = b; \\\n" 2403 " }", 2404 getLLVMStyleWithColumns(20)); 2405 verifyFormat("#define OPERATION_CASE(name) \\\n" 2406 " case OP_name: \\\n" 2407 " return operations::Operation##name\n", 2408 getLLVMStyleWithColumns(40)); 2409 verifyFormat("switch (x) {\n" 2410 "case 1:;\n" 2411 "default:;\n" 2412 " int i;\n" 2413 "}"); 2414 2415 verifyGoogleFormat("switch (x) {\n" 2416 " case 1:\n" 2417 " f();\n" 2418 " break;\n" 2419 " case kFoo:\n" 2420 " case ns::kBar:\n" 2421 " case kBaz:\n" 2422 " break;\n" 2423 " default:\n" 2424 " g();\n" 2425 " break;\n" 2426 "}"); 2427 verifyGoogleFormat("switch (x) {\n" 2428 " case 1: {\n" 2429 " f();\n" 2430 " break;\n" 2431 " }\n" 2432 "}"); 2433 verifyGoogleFormat("switch (test)\n" 2434 " ;"); 2435 2436 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n" 2437 " case OP_name: \\\n" 2438 " return operations::Operation##name\n"); 2439 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n" 2440 " // Get the correction operation class.\n" 2441 " switch (OpCode) {\n" 2442 " CASE(Add);\n" 2443 " CASE(Subtract);\n" 2444 " default:\n" 2445 " return operations::Unknown;\n" 2446 " }\n" 2447 "#undef OPERATION_CASE\n" 2448 "}"); 2449 verifyFormat("DEBUG({\n" 2450 " switch (x) {\n" 2451 " case A:\n" 2452 " f();\n" 2453 " break;\n" 2454 " // fallthrough\n" 2455 " case B:\n" 2456 " g();\n" 2457 " break;\n" 2458 " }\n" 2459 "});"); 2460 EXPECT_EQ("DEBUG({\n" 2461 " switch (x) {\n" 2462 " case A:\n" 2463 " f();\n" 2464 " break;\n" 2465 " // On B:\n" 2466 " case B:\n" 2467 " g();\n" 2468 " break;\n" 2469 " }\n" 2470 "});", 2471 format("DEBUG({\n" 2472 " switch (x) {\n" 2473 " case A:\n" 2474 " f();\n" 2475 " break;\n" 2476 " // On B:\n" 2477 " case B:\n" 2478 " g();\n" 2479 " break;\n" 2480 " }\n" 2481 "});", 2482 getLLVMStyle())); 2483 EXPECT_EQ("switch (n) {\n" 2484 "case 0: {\n" 2485 " return false;\n" 2486 "}\n" 2487 "default: {\n" 2488 " return true;\n" 2489 "}\n" 2490 "}", 2491 format("switch (n)\n" 2492 "{\n" 2493 "case 0: {\n" 2494 " return false;\n" 2495 "}\n" 2496 "default: {\n" 2497 " return true;\n" 2498 "}\n" 2499 "}", 2500 getLLVMStyle())); 2501 verifyFormat("switch (a) {\n" 2502 "case (b):\n" 2503 " return;\n" 2504 "}"); 2505 2506 verifyFormat("switch (a) {\n" 2507 "case some_namespace::\n" 2508 " some_constant:\n" 2509 " return;\n" 2510 "}", 2511 getLLVMStyleWithColumns(34)); 2512 2513 FormatStyle Style = getLLVMStyle(); 2514 Style.IndentCaseLabels = true; 2515 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 2516 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2517 Style.BraceWrapping.AfterCaseLabel = true; 2518 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 2519 EXPECT_EQ("switch (n)\n" 2520 "{\n" 2521 " case 0:\n" 2522 " {\n" 2523 " return false;\n" 2524 " }\n" 2525 " default:\n" 2526 " {\n" 2527 " return true;\n" 2528 " }\n" 2529 "}", 2530 format("switch (n) {\n" 2531 " case 0: {\n" 2532 " return false;\n" 2533 " }\n" 2534 " default: {\n" 2535 " return true;\n" 2536 " }\n" 2537 "}", 2538 Style)); 2539 Style.BraceWrapping.AfterCaseLabel = false; 2540 EXPECT_EQ("switch (n)\n" 2541 "{\n" 2542 " case 0: {\n" 2543 " return false;\n" 2544 " }\n" 2545 " default: {\n" 2546 " return true;\n" 2547 " }\n" 2548 "}", 2549 format("switch (n) {\n" 2550 " case 0:\n" 2551 " {\n" 2552 " return false;\n" 2553 " }\n" 2554 " default:\n" 2555 " {\n" 2556 " return true;\n" 2557 " }\n" 2558 "}", 2559 Style)); 2560 Style.IndentCaseLabels = false; 2561 Style.IndentCaseBlocks = true; 2562 EXPECT_EQ("switch (n)\n" 2563 "{\n" 2564 "case 0:\n" 2565 " {\n" 2566 " return false;\n" 2567 " }\n" 2568 "case 1:\n" 2569 " break;\n" 2570 "default:\n" 2571 " {\n" 2572 " return true;\n" 2573 " }\n" 2574 "}", 2575 format("switch (n) {\n" 2576 "case 0: {\n" 2577 " return false;\n" 2578 "}\n" 2579 "case 1:\n" 2580 " break;\n" 2581 "default: {\n" 2582 " return true;\n" 2583 "}\n" 2584 "}", 2585 Style)); 2586 Style.IndentCaseLabels = true; 2587 Style.IndentCaseBlocks = true; 2588 EXPECT_EQ("switch (n)\n" 2589 "{\n" 2590 " case 0:\n" 2591 " {\n" 2592 " return false;\n" 2593 " }\n" 2594 " case 1:\n" 2595 " break;\n" 2596 " default:\n" 2597 " {\n" 2598 " return true;\n" 2599 " }\n" 2600 "}", 2601 format("switch (n) {\n" 2602 "case 0: {\n" 2603 " return false;\n" 2604 "}\n" 2605 "case 1:\n" 2606 " break;\n" 2607 "default: {\n" 2608 " return true;\n" 2609 "}\n" 2610 "}", 2611 Style)); 2612 } 2613 2614 TEST_F(FormatTest, CaseRanges) { 2615 verifyFormat("switch (x) {\n" 2616 "case 'A' ... 'Z':\n" 2617 "case 1 ... 5:\n" 2618 "case a ... b:\n" 2619 " break;\n" 2620 "}"); 2621 } 2622 2623 TEST_F(FormatTest, ShortEnums) { 2624 FormatStyle Style = getLLVMStyle(); 2625 Style.AllowShortEnumsOnASingleLine = true; 2626 verifyFormat("enum { A, B, C } ShortEnum1, ShortEnum2;", Style); 2627 verifyFormat("typedef enum { A, B, C } ShortEnum1, ShortEnum2;", Style); 2628 Style.AllowShortEnumsOnASingleLine = false; 2629 verifyFormat("enum {\n" 2630 " A,\n" 2631 " B,\n" 2632 " C\n" 2633 "} ShortEnum1, ShortEnum2;", 2634 Style); 2635 verifyFormat("typedef enum {\n" 2636 " A,\n" 2637 " B,\n" 2638 " C\n" 2639 "} ShortEnum1, ShortEnum2;", 2640 Style); 2641 verifyFormat("enum {\n" 2642 " A,\n" 2643 "} ShortEnum1, ShortEnum2;", 2644 Style); 2645 verifyFormat("typedef enum {\n" 2646 " A,\n" 2647 "} ShortEnum1, ShortEnum2;", 2648 Style); 2649 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2650 Style.BraceWrapping.AfterEnum = true; 2651 verifyFormat("enum\n" 2652 "{\n" 2653 " A,\n" 2654 " B,\n" 2655 " C\n" 2656 "} ShortEnum1, ShortEnum2;", 2657 Style); 2658 verifyFormat("typedef enum\n" 2659 "{\n" 2660 " A,\n" 2661 " B,\n" 2662 " C\n" 2663 "} ShortEnum1, ShortEnum2;", 2664 Style); 2665 } 2666 2667 TEST_F(FormatTest, ShortCaseLabels) { 2668 FormatStyle Style = getLLVMStyle(); 2669 Style.AllowShortCaseLabelsOnASingleLine = true; 2670 verifyFormat("switch (a) {\n" 2671 "case 1: x = 1; break;\n" 2672 "case 2: return;\n" 2673 "case 3:\n" 2674 "case 4:\n" 2675 "case 5: return;\n" 2676 "case 6: // comment\n" 2677 " return;\n" 2678 "case 7:\n" 2679 " // comment\n" 2680 " return;\n" 2681 "case 8:\n" 2682 " x = 8; // comment\n" 2683 " break;\n" 2684 "default: y = 1; break;\n" 2685 "}", 2686 Style); 2687 verifyFormat("switch (a) {\n" 2688 "case 0: return; // comment\n" 2689 "case 1: break; // comment\n" 2690 "case 2: return;\n" 2691 "// comment\n" 2692 "case 3: return;\n" 2693 "// comment 1\n" 2694 "// comment 2\n" 2695 "// comment 3\n" 2696 "case 4: break; /* comment */\n" 2697 "case 5:\n" 2698 " // comment\n" 2699 " break;\n" 2700 "case 6: /* comment */ x = 1; break;\n" 2701 "case 7: x = /* comment */ 1; break;\n" 2702 "case 8:\n" 2703 " x = 1; /* comment */\n" 2704 " break;\n" 2705 "case 9:\n" 2706 " break; // comment line 1\n" 2707 " // comment line 2\n" 2708 "}", 2709 Style); 2710 EXPECT_EQ("switch (a) {\n" 2711 "case 1:\n" 2712 " x = 8;\n" 2713 " // fall through\n" 2714 "case 2: x = 8;\n" 2715 "// comment\n" 2716 "case 3:\n" 2717 " return; /* comment line 1\n" 2718 " * comment line 2 */\n" 2719 "case 4: i = 8;\n" 2720 "// something else\n" 2721 "#if FOO\n" 2722 "case 5: break;\n" 2723 "#endif\n" 2724 "}", 2725 format("switch (a) {\n" 2726 "case 1: x = 8;\n" 2727 " // fall through\n" 2728 "case 2:\n" 2729 " x = 8;\n" 2730 "// comment\n" 2731 "case 3:\n" 2732 " return; /* comment line 1\n" 2733 " * comment line 2 */\n" 2734 "case 4:\n" 2735 " i = 8;\n" 2736 "// something else\n" 2737 "#if FOO\n" 2738 "case 5: break;\n" 2739 "#endif\n" 2740 "}", 2741 Style)); 2742 EXPECT_EQ("switch (a) {\n" 2743 "case 0:\n" 2744 " return; // long long long long long long long long long long " 2745 "long long comment\n" 2746 " // line\n" 2747 "}", 2748 format("switch (a) {\n" 2749 "case 0: return; // long long long long long long long long " 2750 "long long long long comment line\n" 2751 "}", 2752 Style)); 2753 EXPECT_EQ("switch (a) {\n" 2754 "case 0:\n" 2755 " return; /* long long long long long long long long long long " 2756 "long long comment\n" 2757 " line */\n" 2758 "}", 2759 format("switch (a) {\n" 2760 "case 0: return; /* long long long long long long long long " 2761 "long long long long comment line */\n" 2762 "}", 2763 Style)); 2764 verifyFormat("switch (a) {\n" 2765 "#if FOO\n" 2766 "case 0: return 0;\n" 2767 "#endif\n" 2768 "}", 2769 Style); 2770 verifyFormat("switch (a) {\n" 2771 "case 1: {\n" 2772 "}\n" 2773 "case 2: {\n" 2774 " return;\n" 2775 "}\n" 2776 "case 3: {\n" 2777 " x = 1;\n" 2778 " return;\n" 2779 "}\n" 2780 "case 4:\n" 2781 " if (x)\n" 2782 " return;\n" 2783 "}", 2784 Style); 2785 Style.ColumnLimit = 21; 2786 verifyFormat("switch (a) {\n" 2787 "case 1: x = 1; break;\n" 2788 "case 2: return;\n" 2789 "case 3:\n" 2790 "case 4:\n" 2791 "case 5: return;\n" 2792 "default:\n" 2793 " y = 1;\n" 2794 " break;\n" 2795 "}", 2796 Style); 2797 Style.ColumnLimit = 80; 2798 Style.AllowShortCaseLabelsOnASingleLine = false; 2799 Style.IndentCaseLabels = true; 2800 EXPECT_EQ("switch (n) {\n" 2801 " default /*comments*/:\n" 2802 " return true;\n" 2803 " case 0:\n" 2804 " return false;\n" 2805 "}", 2806 format("switch (n) {\n" 2807 "default/*comments*/:\n" 2808 " return true;\n" 2809 "case 0:\n" 2810 " return false;\n" 2811 "}", 2812 Style)); 2813 Style.AllowShortCaseLabelsOnASingleLine = true; 2814 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2815 Style.BraceWrapping.AfterCaseLabel = true; 2816 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 2817 EXPECT_EQ("switch (n)\n" 2818 "{\n" 2819 " case 0:\n" 2820 " {\n" 2821 " return false;\n" 2822 " }\n" 2823 " default:\n" 2824 " {\n" 2825 " return true;\n" 2826 " }\n" 2827 "}", 2828 format("switch (n) {\n" 2829 " case 0: {\n" 2830 " return false;\n" 2831 " }\n" 2832 " default:\n" 2833 " {\n" 2834 " return true;\n" 2835 " }\n" 2836 "}", 2837 Style)); 2838 } 2839 2840 TEST_F(FormatTest, FormatsLabels) { 2841 verifyFormat("void f() {\n" 2842 " some_code();\n" 2843 "test_label:\n" 2844 " some_other_code();\n" 2845 " {\n" 2846 " some_more_code();\n" 2847 " another_label:\n" 2848 " some_more_code();\n" 2849 " }\n" 2850 "}"); 2851 verifyFormat("{\n" 2852 " some_code();\n" 2853 "test_label:\n" 2854 " some_other_code();\n" 2855 "}"); 2856 verifyFormat("{\n" 2857 " some_code();\n" 2858 "test_label:;\n" 2859 " int i = 0;\n" 2860 "}"); 2861 FormatStyle Style = getLLVMStyle(); 2862 Style.IndentGotoLabels = false; 2863 verifyFormat("void f() {\n" 2864 " some_code();\n" 2865 "test_label:\n" 2866 " some_other_code();\n" 2867 " {\n" 2868 " some_more_code();\n" 2869 "another_label:\n" 2870 " some_more_code();\n" 2871 " }\n" 2872 "}", 2873 Style); 2874 verifyFormat("{\n" 2875 " some_code();\n" 2876 "test_label:\n" 2877 " some_other_code();\n" 2878 "}", 2879 Style); 2880 verifyFormat("{\n" 2881 " some_code();\n" 2882 "test_label:;\n" 2883 " int i = 0;\n" 2884 "}"); 2885 } 2886 2887 TEST_F(FormatTest, MultiLineControlStatements) { 2888 FormatStyle Style = getLLVMStyleWithColumns(20); 2889 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom; 2890 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine; 2891 // Short lines should keep opening brace on same line. 2892 EXPECT_EQ("if (foo) {\n" 2893 " bar();\n" 2894 "}", 2895 format("if(foo){bar();}", Style)); 2896 EXPECT_EQ("if (foo) {\n" 2897 " bar();\n" 2898 "} else {\n" 2899 " baz();\n" 2900 "}", 2901 format("if(foo){bar();}else{baz();}", Style)); 2902 EXPECT_EQ("if (foo && bar) {\n" 2903 " baz();\n" 2904 "}", 2905 format("if(foo&&bar){baz();}", Style)); 2906 EXPECT_EQ("if (foo) {\n" 2907 " bar();\n" 2908 "} else if (baz) {\n" 2909 " quux();\n" 2910 "}", 2911 format("if(foo){bar();}else if(baz){quux();}", Style)); 2912 EXPECT_EQ( 2913 "if (foo) {\n" 2914 " bar();\n" 2915 "} else if (baz) {\n" 2916 " quux();\n" 2917 "} else {\n" 2918 " foobar();\n" 2919 "}", 2920 format("if(foo){bar();}else if(baz){quux();}else{foobar();}", Style)); 2921 EXPECT_EQ("for (;;) {\n" 2922 " foo();\n" 2923 "}", 2924 format("for(;;){foo();}")); 2925 EXPECT_EQ("while (1) {\n" 2926 " foo();\n" 2927 "}", 2928 format("while(1){foo();}", Style)); 2929 EXPECT_EQ("switch (foo) {\n" 2930 "case bar:\n" 2931 " return;\n" 2932 "}", 2933 format("switch(foo){case bar:return;}", Style)); 2934 EXPECT_EQ("try {\n" 2935 " foo();\n" 2936 "} catch (...) {\n" 2937 " bar();\n" 2938 "}", 2939 format("try{foo();}catch(...){bar();}", Style)); 2940 EXPECT_EQ("do {\n" 2941 " foo();\n" 2942 "} while (bar &&\n" 2943 " baz);", 2944 format("do{foo();}while(bar&&baz);", Style)); 2945 // Long lines should put opening brace on new line. 2946 EXPECT_EQ("if (foo && bar &&\n" 2947 " baz)\n" 2948 "{\n" 2949 " quux();\n" 2950 "}", 2951 format("if(foo&&bar&&baz){quux();}", Style)); 2952 EXPECT_EQ("if (foo && bar &&\n" 2953 " baz)\n" 2954 "{\n" 2955 " quux();\n" 2956 "}", 2957 format("if (foo && bar &&\n" 2958 " baz) {\n" 2959 " quux();\n" 2960 "}", 2961 Style)); 2962 EXPECT_EQ("if (foo) {\n" 2963 " bar();\n" 2964 "} else if (baz ||\n" 2965 " quux)\n" 2966 "{\n" 2967 " foobar();\n" 2968 "}", 2969 format("if(foo){bar();}else if(baz||quux){foobar();}", Style)); 2970 EXPECT_EQ( 2971 "if (foo) {\n" 2972 " bar();\n" 2973 "} else if (baz ||\n" 2974 " quux)\n" 2975 "{\n" 2976 " foobar();\n" 2977 "} else {\n" 2978 " barbaz();\n" 2979 "}", 2980 format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}", 2981 Style)); 2982 EXPECT_EQ("for (int i = 0;\n" 2983 " i < 10; ++i)\n" 2984 "{\n" 2985 " foo();\n" 2986 "}", 2987 format("for(int i=0;i<10;++i){foo();}", Style)); 2988 EXPECT_EQ("foreach (int i,\n" 2989 " list)\n" 2990 "{\n" 2991 " foo();\n" 2992 "}", 2993 format("foreach(int i, list){foo();}", Style)); 2994 Style.ColumnLimit = 2995 40; // to concentrate at brace wrapping, not line wrap due to column limit 2996 EXPECT_EQ("foreach (int i, list) {\n" 2997 " foo();\n" 2998 "}", 2999 format("foreach(int i, list){foo();}", Style)); 3000 Style.ColumnLimit = 3001 20; // to concentrate at brace wrapping, not line wrap due to column limit 3002 EXPECT_EQ("while (foo || bar ||\n" 3003 " baz)\n" 3004 "{\n" 3005 " quux();\n" 3006 "}", 3007 format("while(foo||bar||baz){quux();}", Style)); 3008 EXPECT_EQ("switch (\n" 3009 " foo = barbaz)\n" 3010 "{\n" 3011 "case quux:\n" 3012 " return;\n" 3013 "}", 3014 format("switch(foo=barbaz){case quux:return;}", Style)); 3015 EXPECT_EQ("try {\n" 3016 " foo();\n" 3017 "} catch (\n" 3018 " Exception &bar)\n" 3019 "{\n" 3020 " baz();\n" 3021 "}", 3022 format("try{foo();}catch(Exception&bar){baz();}", Style)); 3023 Style.ColumnLimit = 3024 40; // to concentrate at brace wrapping, not line wrap due to column limit 3025 EXPECT_EQ("try {\n" 3026 " foo();\n" 3027 "} catch (Exception &bar) {\n" 3028 " baz();\n" 3029 "}", 3030 format("try{foo();}catch(Exception&bar){baz();}", Style)); 3031 Style.ColumnLimit = 3032 20; // to concentrate at brace wrapping, not line wrap due to column limit 3033 3034 Style.BraceWrapping.BeforeElse = true; 3035 EXPECT_EQ( 3036 "if (foo) {\n" 3037 " bar();\n" 3038 "}\n" 3039 "else if (baz ||\n" 3040 " quux)\n" 3041 "{\n" 3042 " foobar();\n" 3043 "}\n" 3044 "else {\n" 3045 " barbaz();\n" 3046 "}", 3047 format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}", 3048 Style)); 3049 3050 Style.BraceWrapping.BeforeCatch = true; 3051 EXPECT_EQ("try {\n" 3052 " foo();\n" 3053 "}\n" 3054 "catch (...) {\n" 3055 " baz();\n" 3056 "}", 3057 format("try{foo();}catch(...){baz();}", Style)); 3058 3059 Style.BraceWrapping.AfterFunction = true; 3060 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine; 3061 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 3062 Style.ColumnLimit = 80; 3063 verifyFormat("void shortfunction() { bar(); }", Style); 3064 3065 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 3066 verifyFormat("void shortfunction()\n" 3067 "{\n" 3068 " bar();\n" 3069 "}", 3070 Style); 3071 } 3072 3073 TEST_F(FormatTest, BeforeWhile) { 3074 FormatStyle Style = getLLVMStyle(); 3075 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom; 3076 3077 verifyFormat("do {\n" 3078 " foo();\n" 3079 "} while (1);", 3080 Style); 3081 Style.BraceWrapping.BeforeWhile = true; 3082 verifyFormat("do {\n" 3083 " foo();\n" 3084 "}\n" 3085 "while (1);", 3086 Style); 3087 } 3088 3089 //===----------------------------------------------------------------------===// 3090 // Tests for classes, namespaces, etc. 3091 //===----------------------------------------------------------------------===// 3092 3093 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 3094 verifyFormat("class A {};"); 3095 } 3096 3097 TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 3098 verifyFormat("class A {\n" 3099 "public:\n" 3100 "public: // comment\n" 3101 "protected:\n" 3102 "private:\n" 3103 " void f() {}\n" 3104 "};"); 3105 verifyFormat("export class A {\n" 3106 "public:\n" 3107 "public: // comment\n" 3108 "protected:\n" 3109 "private:\n" 3110 " void f() {}\n" 3111 "};"); 3112 verifyGoogleFormat("class A {\n" 3113 " public:\n" 3114 " protected:\n" 3115 " private:\n" 3116 " void f() {}\n" 3117 "};"); 3118 verifyGoogleFormat("export class A {\n" 3119 " public:\n" 3120 " protected:\n" 3121 " private:\n" 3122 " void f() {}\n" 3123 "};"); 3124 verifyFormat("class A {\n" 3125 "public slots:\n" 3126 " void f1() {}\n" 3127 "public Q_SLOTS:\n" 3128 " void f2() {}\n" 3129 "protected slots:\n" 3130 " void f3() {}\n" 3131 "protected Q_SLOTS:\n" 3132 " void f4() {}\n" 3133 "private slots:\n" 3134 " void f5() {}\n" 3135 "private Q_SLOTS:\n" 3136 " void f6() {}\n" 3137 "signals:\n" 3138 " void g1();\n" 3139 "Q_SIGNALS:\n" 3140 " void g2();\n" 3141 "};"); 3142 3143 // Don't interpret 'signals' the wrong way. 3144 verifyFormat("signals.set();"); 3145 verifyFormat("for (Signals signals : f()) {\n}"); 3146 verifyFormat("{\n" 3147 " signals.set(); // This needs indentation.\n" 3148 "}"); 3149 verifyFormat("void f() {\n" 3150 "label:\n" 3151 " signals.baz();\n" 3152 "}"); 3153 verifyFormat("private[1];"); 3154 verifyFormat("testArray[public] = 1;"); 3155 verifyFormat("public();"); 3156 verifyFormat("myFunc(public);"); 3157 verifyFormat("std::vector<int> testVec = {private};"); 3158 verifyFormat("private.p = 1;"); 3159 verifyFormat("void function(private...){};"); 3160 verifyFormat("if (private && public)\n"); 3161 verifyFormat("private &= true;"); 3162 verifyFormat("int x = private * public;"); 3163 verifyFormat("public *= private;"); 3164 verifyFormat("int x = public + private;"); 3165 verifyFormat("private++;"); 3166 verifyFormat("++private;"); 3167 verifyFormat("public += private;"); 3168 verifyFormat("public = public - private;"); 3169 verifyFormat("public->foo();"); 3170 verifyFormat("private--;"); 3171 verifyFormat("--private;"); 3172 verifyFormat("public -= 1;"); 3173 verifyFormat("if (!private && !public)\n"); 3174 verifyFormat("public != private;"); 3175 verifyFormat("int x = public / private;"); 3176 verifyFormat("public /= 2;"); 3177 verifyFormat("public = public % 2;"); 3178 verifyFormat("public %= 2;"); 3179 verifyFormat("if (public < private)\n"); 3180 verifyFormat("public << private;"); 3181 verifyFormat("public <<= private;"); 3182 verifyFormat("if (public > private)\n"); 3183 verifyFormat("public >> private;"); 3184 verifyFormat("public >>= private;"); 3185 verifyFormat("public ^ private;"); 3186 verifyFormat("public ^= private;"); 3187 verifyFormat("public | private;"); 3188 verifyFormat("public |= private;"); 3189 verifyFormat("auto x = private ? 1 : 2;"); 3190 verifyFormat("if (public == private)\n"); 3191 verifyFormat("void foo(public, private)"); 3192 verifyFormat("public::foo();"); 3193 } 3194 3195 TEST_F(FormatTest, SeparatesLogicalBlocks) { 3196 EXPECT_EQ("class A {\n" 3197 "public:\n" 3198 " void f();\n" 3199 "\n" 3200 "private:\n" 3201 " void g() {}\n" 3202 " // test\n" 3203 "protected:\n" 3204 " int h;\n" 3205 "};", 3206 format("class A {\n" 3207 "public:\n" 3208 "void f();\n" 3209 "private:\n" 3210 "void g() {}\n" 3211 "// test\n" 3212 "protected:\n" 3213 "int h;\n" 3214 "};")); 3215 EXPECT_EQ("class A {\n" 3216 "protected:\n" 3217 "public:\n" 3218 " void f();\n" 3219 "};", 3220 format("class A {\n" 3221 "protected:\n" 3222 "\n" 3223 "public:\n" 3224 "\n" 3225 " void f();\n" 3226 "};")); 3227 3228 // Even ensure proper spacing inside macros. 3229 EXPECT_EQ("#define B \\\n" 3230 " class A { \\\n" 3231 " protected: \\\n" 3232 " public: \\\n" 3233 " void f(); \\\n" 3234 " };", 3235 format("#define B \\\n" 3236 " class A { \\\n" 3237 " protected: \\\n" 3238 " \\\n" 3239 " public: \\\n" 3240 " \\\n" 3241 " void f(); \\\n" 3242 " };", 3243 getGoogleStyle())); 3244 // But don't remove empty lines after macros ending in access specifiers. 3245 EXPECT_EQ("#define A private:\n" 3246 "\n" 3247 "int i;", 3248 format("#define A private:\n" 3249 "\n" 3250 "int i;")); 3251 } 3252 3253 TEST_F(FormatTest, FormatsClasses) { 3254 verifyFormat("class A : public B {};"); 3255 verifyFormat("class A : public ::B {};"); 3256 3257 verifyFormat( 3258 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3259 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 3260 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 3261 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3262 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 3263 verifyFormat( 3264 "class A : public B, public C, public D, public E, public F {};"); 3265 verifyFormat("class AAAAAAAAAAAA : public B,\n" 3266 " public C,\n" 3267 " public D,\n" 3268 " public E,\n" 3269 " public F,\n" 3270 " public G {};"); 3271 3272 verifyFormat("class\n" 3273 " ReallyReallyLongClassName {\n" 3274 " int i;\n" 3275 "};", 3276 getLLVMStyleWithColumns(32)); 3277 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 3278 " aaaaaaaaaaaaaaaa> {};"); 3279 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" 3280 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n" 3281 " aaaaaaaaaaaaaaaaaaaaaa> {};"); 3282 verifyFormat("template <class R, class C>\n" 3283 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n" 3284 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};"); 3285 verifyFormat("class ::A::B {};"); 3286 } 3287 3288 TEST_F(FormatTest, BreakInheritanceStyle) { 3289 FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle(); 3290 StyleWithInheritanceBreakBeforeComma.BreakInheritanceList = 3291 FormatStyle::BILS_BeforeComma; 3292 verifyFormat("class MyClass : public X {};", 3293 StyleWithInheritanceBreakBeforeComma); 3294 verifyFormat("class MyClass\n" 3295 " : public X\n" 3296 " , public Y {};", 3297 StyleWithInheritanceBreakBeforeComma); 3298 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n" 3299 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n" 3300 " , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", 3301 StyleWithInheritanceBreakBeforeComma); 3302 verifyFormat("struct aaaaaaaaaaaaa\n" 3303 " : public aaaaaaaaaaaaaaaaaaa< // break\n" 3304 " aaaaaaaaaaaaaaaa> {};", 3305 StyleWithInheritanceBreakBeforeComma); 3306 3307 FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle(); 3308 StyleWithInheritanceBreakAfterColon.BreakInheritanceList = 3309 FormatStyle::BILS_AfterColon; 3310 verifyFormat("class MyClass : public X {};", 3311 StyleWithInheritanceBreakAfterColon); 3312 verifyFormat("class MyClass : public X, public Y {};", 3313 StyleWithInheritanceBreakAfterColon); 3314 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n" 3315 " public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3316 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", 3317 StyleWithInheritanceBreakAfterColon); 3318 verifyFormat("struct aaaaaaaaaaaaa :\n" 3319 " public aaaaaaaaaaaaaaaaaaa< // break\n" 3320 " aaaaaaaaaaaaaaaa> {};", 3321 StyleWithInheritanceBreakAfterColon); 3322 3323 FormatStyle StyleWithInheritanceBreakAfterComma = getLLVMStyle(); 3324 StyleWithInheritanceBreakAfterComma.BreakInheritanceList = 3325 FormatStyle::BILS_AfterComma; 3326 verifyFormat("class MyClass : public X {};", 3327 StyleWithInheritanceBreakAfterComma); 3328 verifyFormat("class MyClass : public X,\n" 3329 " public Y {};", 3330 StyleWithInheritanceBreakAfterComma); 3331 verifyFormat( 3332 "class AAAAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3333 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC " 3334 "{};", 3335 StyleWithInheritanceBreakAfterComma); 3336 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 3337 " aaaaaaaaaaaaaaaa> {};", 3338 StyleWithInheritanceBreakAfterComma); 3339 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 3340 " : public OnceBreak,\n" 3341 " public AlwaysBreak,\n" 3342 " EvenBasesFitInOneLine {};", 3343 StyleWithInheritanceBreakAfterComma); 3344 } 3345 3346 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { 3347 verifyFormat("class A {\n} a, b;"); 3348 verifyFormat("struct A {\n} a, b;"); 3349 verifyFormat("union A {\n} a;"); 3350 } 3351 3352 TEST_F(FormatTest, FormatsEnum) { 3353 verifyFormat("enum {\n" 3354 " Zero,\n" 3355 " One = 1,\n" 3356 " Two = One + 1,\n" 3357 " Three = (One + Two),\n" 3358 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3359 " Five = (One, Two, Three, Four, 5)\n" 3360 "};"); 3361 verifyGoogleFormat("enum {\n" 3362 " Zero,\n" 3363 " One = 1,\n" 3364 " Two = One + 1,\n" 3365 " Three = (One + Two),\n" 3366 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3367 " Five = (One, Two, Three, Four, 5)\n" 3368 "};"); 3369 verifyFormat("enum Enum {};"); 3370 verifyFormat("enum {};"); 3371 verifyFormat("enum X E {} d;"); 3372 verifyFormat("enum __attribute__((...)) E {} d;"); 3373 verifyFormat("enum __declspec__((...)) E {} d;"); 3374 verifyFormat("enum {\n" 3375 " Bar = Foo<int, int>::value\n" 3376 "};", 3377 getLLVMStyleWithColumns(30)); 3378 3379 verifyFormat("enum ShortEnum { A, B, C };"); 3380 verifyGoogleFormat("enum ShortEnum { A, B, C };"); 3381 3382 EXPECT_EQ("enum KeepEmptyLines {\n" 3383 " ONE,\n" 3384 "\n" 3385 " TWO,\n" 3386 "\n" 3387 " THREE\n" 3388 "}", 3389 format("enum KeepEmptyLines {\n" 3390 " ONE,\n" 3391 "\n" 3392 " TWO,\n" 3393 "\n" 3394 "\n" 3395 " THREE\n" 3396 "}")); 3397 verifyFormat("enum E { // comment\n" 3398 " ONE,\n" 3399 " TWO\n" 3400 "};\n" 3401 "int i;"); 3402 3403 FormatStyle EightIndent = getLLVMStyle(); 3404 EightIndent.IndentWidth = 8; 3405 verifyFormat("enum {\n" 3406 " VOID,\n" 3407 " CHAR,\n" 3408 " SHORT,\n" 3409 " INT,\n" 3410 " LONG,\n" 3411 " SIGNED,\n" 3412 " UNSIGNED,\n" 3413 " BOOL,\n" 3414 " FLOAT,\n" 3415 " DOUBLE,\n" 3416 " COMPLEX\n" 3417 "};", 3418 EightIndent); 3419 3420 // Not enums. 3421 verifyFormat("enum X f() {\n" 3422 " a();\n" 3423 " return 42;\n" 3424 "}"); 3425 verifyFormat("enum X Type::f() {\n" 3426 " a();\n" 3427 " return 42;\n" 3428 "}"); 3429 verifyFormat("enum ::X f() {\n" 3430 " a();\n" 3431 " return 42;\n" 3432 "}"); 3433 verifyFormat("enum ns::X f() {\n" 3434 " a();\n" 3435 " return 42;\n" 3436 "}"); 3437 } 3438 3439 TEST_F(FormatTest, FormatsEnumsWithErrors) { 3440 verifyFormat("enum Type {\n" 3441 " One = 0; // These semicolons should be commas.\n" 3442 " Two = 1;\n" 3443 "};"); 3444 verifyFormat("namespace n {\n" 3445 "enum Type {\n" 3446 " One,\n" 3447 " Two, // missing };\n" 3448 " int i;\n" 3449 "}\n" 3450 "void g() {}"); 3451 } 3452 3453 TEST_F(FormatTest, FormatsEnumStruct) { 3454 verifyFormat("enum struct {\n" 3455 " Zero,\n" 3456 " One = 1,\n" 3457 " Two = One + 1,\n" 3458 " Three = (One + Two),\n" 3459 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3460 " Five = (One, Two, Three, Four, 5)\n" 3461 "};"); 3462 verifyFormat("enum struct Enum {};"); 3463 verifyFormat("enum struct {};"); 3464 verifyFormat("enum struct X E {} d;"); 3465 verifyFormat("enum struct __attribute__((...)) E {} d;"); 3466 verifyFormat("enum struct __declspec__((...)) E {} d;"); 3467 verifyFormat("enum struct X f() {\n a();\n return 42;\n}"); 3468 } 3469 3470 TEST_F(FormatTest, FormatsEnumClass) { 3471 verifyFormat("enum class {\n" 3472 " Zero,\n" 3473 " One = 1,\n" 3474 " Two = One + 1,\n" 3475 " Three = (One + Two),\n" 3476 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3477 " Five = (One, Two, Three, Four, 5)\n" 3478 "};"); 3479 verifyFormat("enum class Enum {};"); 3480 verifyFormat("enum class {};"); 3481 verifyFormat("enum class X E {} d;"); 3482 verifyFormat("enum class __attribute__((...)) E {} d;"); 3483 verifyFormat("enum class __declspec__((...)) E {} d;"); 3484 verifyFormat("enum class X f() {\n a();\n return 42;\n}"); 3485 } 3486 3487 TEST_F(FormatTest, FormatsEnumTypes) { 3488 verifyFormat("enum X : int {\n" 3489 " A, // Force multiple lines.\n" 3490 " B\n" 3491 "};"); 3492 verifyFormat("enum X : int { A, B };"); 3493 verifyFormat("enum X : std::uint32_t { A, B };"); 3494 } 3495 3496 TEST_F(FormatTest, FormatsTypedefEnum) { 3497 FormatStyle Style = getLLVMStyleWithColumns(40); 3498 verifyFormat("typedef enum {} EmptyEnum;"); 3499 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 3500 verifyFormat("typedef enum {\n" 3501 " ZERO = 0,\n" 3502 " ONE = 1,\n" 3503 " TWO = 2,\n" 3504 " THREE = 3\n" 3505 "} LongEnum;", 3506 Style); 3507 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3508 Style.BraceWrapping.AfterEnum = true; 3509 verifyFormat("typedef enum {} EmptyEnum;"); 3510 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 3511 verifyFormat("typedef enum\n" 3512 "{\n" 3513 " ZERO = 0,\n" 3514 " ONE = 1,\n" 3515 " TWO = 2,\n" 3516 " THREE = 3\n" 3517 "} LongEnum;", 3518 Style); 3519 } 3520 3521 TEST_F(FormatTest, FormatsNSEnums) { 3522 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }"); 3523 verifyGoogleFormat( 3524 "typedef NS_CLOSED_ENUM(NSInteger, SomeName) { AAA, BBB }"); 3525 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n" 3526 " // Information about someDecentlyLongValue.\n" 3527 " someDecentlyLongValue,\n" 3528 " // Information about anotherDecentlyLongValue.\n" 3529 " anotherDecentlyLongValue,\n" 3530 " // Information about aThirdDecentlyLongValue.\n" 3531 " aThirdDecentlyLongValue\n" 3532 "};"); 3533 verifyGoogleFormat("typedef NS_CLOSED_ENUM(NSInteger, MyType) {\n" 3534 " // Information about someDecentlyLongValue.\n" 3535 " someDecentlyLongValue,\n" 3536 " // Information about anotherDecentlyLongValue.\n" 3537 " anotherDecentlyLongValue,\n" 3538 " // Information about aThirdDecentlyLongValue.\n" 3539 " aThirdDecentlyLongValue\n" 3540 "};"); 3541 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n" 3542 " a = 1,\n" 3543 " b = 2,\n" 3544 " c = 3,\n" 3545 "};"); 3546 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n" 3547 " a = 1,\n" 3548 " b = 2,\n" 3549 " c = 3,\n" 3550 "};"); 3551 verifyGoogleFormat("typedef CF_CLOSED_ENUM(NSInteger, MyType) {\n" 3552 " a = 1,\n" 3553 " b = 2,\n" 3554 " c = 3,\n" 3555 "};"); 3556 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n" 3557 " a = 1,\n" 3558 " b = 2,\n" 3559 " c = 3,\n" 3560 "};"); 3561 } 3562 3563 TEST_F(FormatTest, FormatsBitfields) { 3564 verifyFormat("struct Bitfields {\n" 3565 " unsigned sClass : 8;\n" 3566 " unsigned ValueKind : 2;\n" 3567 "};"); 3568 verifyFormat("struct A {\n" 3569 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" 3570 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" 3571 "};"); 3572 verifyFormat("struct MyStruct {\n" 3573 " uchar data;\n" 3574 " uchar : 8;\n" 3575 " uchar : 8;\n" 3576 " uchar other;\n" 3577 "};"); 3578 FormatStyle Style = getLLVMStyle(); 3579 Style.BitFieldColonSpacing = FormatStyle::BFCS_None; 3580 verifyFormat("struct Bitfields {\n" 3581 " unsigned sClass:8;\n" 3582 " unsigned ValueKind:2;\n" 3583 " uchar other;\n" 3584 "};", 3585 Style); 3586 verifyFormat("struct A {\n" 3587 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1,\n" 3588 " bbbbbbbbbbbbbbbbbbbbbbbbb:2;\n" 3589 "};", 3590 Style); 3591 Style.BitFieldColonSpacing = FormatStyle::BFCS_Before; 3592 verifyFormat("struct Bitfields {\n" 3593 " unsigned sClass :8;\n" 3594 " unsigned ValueKind :2;\n" 3595 " uchar other;\n" 3596 "};", 3597 Style); 3598 Style.BitFieldColonSpacing = FormatStyle::BFCS_After; 3599 verifyFormat("struct Bitfields {\n" 3600 " unsigned sClass: 8;\n" 3601 " unsigned ValueKind: 2;\n" 3602 " uchar other;\n" 3603 "};", 3604 Style); 3605 } 3606 3607 TEST_F(FormatTest, FormatsNamespaces) { 3608 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 3609 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 3610 3611 verifyFormat("namespace some_namespace {\n" 3612 "class A {};\n" 3613 "void f() { f(); }\n" 3614 "}", 3615 LLVMWithNoNamespaceFix); 3616 verifyFormat("namespace N::inline D {\n" 3617 "class A {};\n" 3618 "void f() { f(); }\n" 3619 "}", 3620 LLVMWithNoNamespaceFix); 3621 verifyFormat("namespace N::inline D::E {\n" 3622 "class A {};\n" 3623 "void f() { f(); }\n" 3624 "}", 3625 LLVMWithNoNamespaceFix); 3626 verifyFormat("namespace [[deprecated(\"foo[bar\")]] some_namespace {\n" 3627 "class A {};\n" 3628 "void f() { f(); }\n" 3629 "}", 3630 LLVMWithNoNamespaceFix); 3631 verifyFormat("/* something */ namespace some_namespace {\n" 3632 "class A {};\n" 3633 "void f() { f(); }\n" 3634 "}", 3635 LLVMWithNoNamespaceFix); 3636 verifyFormat("namespace {\n" 3637 "class A {};\n" 3638 "void f() { f(); }\n" 3639 "}", 3640 LLVMWithNoNamespaceFix); 3641 verifyFormat("/* something */ namespace {\n" 3642 "class A {};\n" 3643 "void f() { f(); }\n" 3644 "}", 3645 LLVMWithNoNamespaceFix); 3646 verifyFormat("inline namespace X {\n" 3647 "class A {};\n" 3648 "void f() { f(); }\n" 3649 "}", 3650 LLVMWithNoNamespaceFix); 3651 verifyFormat("/* something */ inline namespace X {\n" 3652 "class A {};\n" 3653 "void f() { f(); }\n" 3654 "}", 3655 LLVMWithNoNamespaceFix); 3656 verifyFormat("export namespace X {\n" 3657 "class A {};\n" 3658 "void f() { f(); }\n" 3659 "}", 3660 LLVMWithNoNamespaceFix); 3661 verifyFormat("using namespace some_namespace;\n" 3662 "class A {};\n" 3663 "void f() { f(); }", 3664 LLVMWithNoNamespaceFix); 3665 3666 // This code is more common than we thought; if we 3667 // layout this correctly the semicolon will go into 3668 // its own line, which is undesirable. 3669 verifyFormat("namespace {};", LLVMWithNoNamespaceFix); 3670 verifyFormat("namespace {\n" 3671 "class A {};\n" 3672 "};", 3673 LLVMWithNoNamespaceFix); 3674 3675 verifyFormat("namespace {\n" 3676 "int SomeVariable = 0; // comment\n" 3677 "} // namespace", 3678 LLVMWithNoNamespaceFix); 3679 EXPECT_EQ("#ifndef HEADER_GUARD\n" 3680 "#define HEADER_GUARD\n" 3681 "namespace my_namespace {\n" 3682 "int i;\n" 3683 "} // my_namespace\n" 3684 "#endif // HEADER_GUARD", 3685 format("#ifndef HEADER_GUARD\n" 3686 " #define HEADER_GUARD\n" 3687 " namespace my_namespace {\n" 3688 "int i;\n" 3689 "} // my_namespace\n" 3690 "#endif // HEADER_GUARD", 3691 LLVMWithNoNamespaceFix)); 3692 3693 EXPECT_EQ("namespace A::B {\n" 3694 "class C {};\n" 3695 "}", 3696 format("namespace A::B {\n" 3697 "class C {};\n" 3698 "}", 3699 LLVMWithNoNamespaceFix)); 3700 3701 FormatStyle Style = getLLVMStyle(); 3702 Style.NamespaceIndentation = FormatStyle::NI_All; 3703 EXPECT_EQ("namespace out {\n" 3704 " int i;\n" 3705 " namespace in {\n" 3706 " int i;\n" 3707 " } // namespace in\n" 3708 "} // namespace out", 3709 format("namespace out {\n" 3710 "int i;\n" 3711 "namespace in {\n" 3712 "int i;\n" 3713 "} // namespace in\n" 3714 "} // namespace out", 3715 Style)); 3716 3717 FormatStyle ShortInlineFunctions = getLLVMStyle(); 3718 ShortInlineFunctions.NamespaceIndentation = FormatStyle::NI_All; 3719 ShortInlineFunctions.AllowShortFunctionsOnASingleLine = 3720 FormatStyle::SFS_Inline; 3721 verifyFormat("namespace {\n" 3722 " void f() {\n" 3723 " return;\n" 3724 " }\n" 3725 "} // namespace\n", 3726 ShortInlineFunctions); 3727 verifyFormat("namespace {\n" 3728 " int some_int;\n" 3729 " void f() {\n" 3730 " return;\n" 3731 " }\n" 3732 "} // namespace\n", 3733 ShortInlineFunctions); 3734 verifyFormat("namespace interface {\n" 3735 " void f() {\n" 3736 " return;\n" 3737 " }\n" 3738 "} // namespace interface\n", 3739 ShortInlineFunctions); 3740 verifyFormat("namespace {\n" 3741 " class X {\n" 3742 " void f() { return; }\n" 3743 " };\n" 3744 "} // namespace\n", 3745 ShortInlineFunctions); 3746 verifyFormat("namespace {\n" 3747 " struct X {\n" 3748 " void f() { return; }\n" 3749 " };\n" 3750 "} // namespace\n", 3751 ShortInlineFunctions); 3752 verifyFormat("namespace {\n" 3753 " union X {\n" 3754 " void f() { return; }\n" 3755 " };\n" 3756 "} // namespace\n", 3757 ShortInlineFunctions); 3758 verifyFormat("extern \"C\" {\n" 3759 "void f() {\n" 3760 " return;\n" 3761 "}\n" 3762 "} // namespace\n", 3763 ShortInlineFunctions); 3764 verifyFormat("namespace {\n" 3765 " class X {\n" 3766 " void f() { return; }\n" 3767 " } x;\n" 3768 "} // namespace\n", 3769 ShortInlineFunctions); 3770 verifyFormat("namespace {\n" 3771 " [[nodiscard]] class X {\n" 3772 " void f() { return; }\n" 3773 " };\n" 3774 "} // namespace\n", 3775 ShortInlineFunctions); 3776 verifyFormat("namespace {\n" 3777 " static class X {\n" 3778 " void f() { return; }\n" 3779 " } x;\n" 3780 "} // namespace\n", 3781 ShortInlineFunctions); 3782 verifyFormat("namespace {\n" 3783 " constexpr class X {\n" 3784 " void f() { return; }\n" 3785 " } x;\n" 3786 "} // namespace\n", 3787 ShortInlineFunctions); 3788 3789 ShortInlineFunctions.IndentExternBlock = FormatStyle::IEBS_Indent; 3790 verifyFormat("extern \"C\" {\n" 3791 " void f() {\n" 3792 " return;\n" 3793 " }\n" 3794 "} // namespace\n", 3795 ShortInlineFunctions); 3796 3797 Style.NamespaceIndentation = FormatStyle::NI_Inner; 3798 EXPECT_EQ("namespace out {\n" 3799 "int i;\n" 3800 "namespace in {\n" 3801 " int i;\n" 3802 "} // namespace in\n" 3803 "} // namespace out", 3804 format("namespace out {\n" 3805 "int i;\n" 3806 "namespace in {\n" 3807 "int i;\n" 3808 "} // namespace in\n" 3809 "} // namespace out", 3810 Style)); 3811 3812 Style.NamespaceIndentation = FormatStyle::NI_None; 3813 verifyFormat("template <class T>\n" 3814 "concept a_concept = X<>;\n" 3815 "namespace B {\n" 3816 "struct b_struct {};\n" 3817 "} // namespace B\n", 3818 Style); 3819 verifyFormat("template <int I> constexpr void foo requires(I == 42) {}\n" 3820 "namespace ns {\n" 3821 "void foo() {}\n" 3822 "} // namespace ns\n", 3823 Style); 3824 } 3825 3826 TEST_F(FormatTest, NamespaceMacros) { 3827 FormatStyle Style = getLLVMStyle(); 3828 Style.NamespaceMacros.push_back("TESTSUITE"); 3829 3830 verifyFormat("TESTSUITE(A) {\n" 3831 "int foo();\n" 3832 "} // TESTSUITE(A)", 3833 Style); 3834 3835 verifyFormat("TESTSUITE(A, B) {\n" 3836 "int foo();\n" 3837 "} // TESTSUITE(A)", 3838 Style); 3839 3840 // Properly indent according to NamespaceIndentation style 3841 Style.NamespaceIndentation = FormatStyle::NI_All; 3842 verifyFormat("TESTSUITE(A) {\n" 3843 " int foo();\n" 3844 "} // TESTSUITE(A)", 3845 Style); 3846 verifyFormat("TESTSUITE(A) {\n" 3847 " namespace B {\n" 3848 " int foo();\n" 3849 " } // namespace B\n" 3850 "} // TESTSUITE(A)", 3851 Style); 3852 verifyFormat("namespace A {\n" 3853 " TESTSUITE(B) {\n" 3854 " int foo();\n" 3855 " } // TESTSUITE(B)\n" 3856 "} // namespace A", 3857 Style); 3858 3859 Style.NamespaceIndentation = FormatStyle::NI_Inner; 3860 verifyFormat("TESTSUITE(A) {\n" 3861 "TESTSUITE(B) {\n" 3862 " int foo();\n" 3863 "} // TESTSUITE(B)\n" 3864 "} // TESTSUITE(A)", 3865 Style); 3866 verifyFormat("TESTSUITE(A) {\n" 3867 "namespace B {\n" 3868 " int foo();\n" 3869 "} // namespace B\n" 3870 "} // TESTSUITE(A)", 3871 Style); 3872 verifyFormat("namespace A {\n" 3873 "TESTSUITE(B) {\n" 3874 " int foo();\n" 3875 "} // TESTSUITE(B)\n" 3876 "} // namespace A", 3877 Style); 3878 3879 // Properly merge namespace-macros blocks in CompactNamespaces mode 3880 Style.NamespaceIndentation = FormatStyle::NI_None; 3881 Style.CompactNamespaces = true; 3882 verifyFormat("TESTSUITE(A) { TESTSUITE(B) {\n" 3883 "}} // TESTSUITE(A::B)", 3884 Style); 3885 3886 EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n" 3887 "}} // TESTSUITE(out::in)", 3888 format("TESTSUITE(out) {\n" 3889 "TESTSUITE(in) {\n" 3890 "} // TESTSUITE(in)\n" 3891 "} // TESTSUITE(out)", 3892 Style)); 3893 3894 EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n" 3895 "}} // TESTSUITE(out::in)", 3896 format("TESTSUITE(out) {\n" 3897 "TESTSUITE(in) {\n" 3898 "} // TESTSUITE(in)\n" 3899 "} // TESTSUITE(out)", 3900 Style)); 3901 3902 // Do not merge different namespaces/macros 3903 EXPECT_EQ("namespace out {\n" 3904 "TESTSUITE(in) {\n" 3905 "} // TESTSUITE(in)\n" 3906 "} // namespace out", 3907 format("namespace out {\n" 3908 "TESTSUITE(in) {\n" 3909 "} // TESTSUITE(in)\n" 3910 "} // namespace out", 3911 Style)); 3912 EXPECT_EQ("TESTSUITE(out) {\n" 3913 "namespace in {\n" 3914 "} // namespace in\n" 3915 "} // TESTSUITE(out)", 3916 format("TESTSUITE(out) {\n" 3917 "namespace in {\n" 3918 "} // namespace in\n" 3919 "} // TESTSUITE(out)", 3920 Style)); 3921 Style.NamespaceMacros.push_back("FOOBAR"); 3922 EXPECT_EQ("TESTSUITE(out) {\n" 3923 "FOOBAR(in) {\n" 3924 "} // FOOBAR(in)\n" 3925 "} // TESTSUITE(out)", 3926 format("TESTSUITE(out) {\n" 3927 "FOOBAR(in) {\n" 3928 "} // FOOBAR(in)\n" 3929 "} // TESTSUITE(out)", 3930 Style)); 3931 } 3932 3933 TEST_F(FormatTest, FormatsCompactNamespaces) { 3934 FormatStyle Style = getLLVMStyle(); 3935 Style.CompactNamespaces = true; 3936 Style.NamespaceMacros.push_back("TESTSUITE"); 3937 3938 verifyFormat("namespace A { namespace B {\n" 3939 "}} // namespace A::B", 3940 Style); 3941 3942 EXPECT_EQ("namespace out { namespace in {\n" 3943 "}} // namespace out::in", 3944 format("namespace out {\n" 3945 "namespace in {\n" 3946 "} // namespace in\n" 3947 "} // namespace out", 3948 Style)); 3949 3950 // Only namespaces which have both consecutive opening and end get compacted 3951 EXPECT_EQ("namespace out {\n" 3952 "namespace in1 {\n" 3953 "} // namespace in1\n" 3954 "namespace in2 {\n" 3955 "} // namespace in2\n" 3956 "} // namespace out", 3957 format("namespace out {\n" 3958 "namespace in1 {\n" 3959 "} // namespace in1\n" 3960 "namespace in2 {\n" 3961 "} // namespace in2\n" 3962 "} // namespace out", 3963 Style)); 3964 3965 EXPECT_EQ("namespace out {\n" 3966 "int i;\n" 3967 "namespace in {\n" 3968 "int j;\n" 3969 "} // namespace in\n" 3970 "int k;\n" 3971 "} // namespace out", 3972 format("namespace out { int i;\n" 3973 "namespace in { int j; } // namespace in\n" 3974 "int k; } // namespace out", 3975 Style)); 3976 3977 EXPECT_EQ("namespace A { namespace B { namespace C {\n" 3978 "}}} // namespace A::B::C\n", 3979 format("namespace A { namespace B {\n" 3980 "namespace C {\n" 3981 "}} // namespace B::C\n" 3982 "} // namespace A\n", 3983 Style)); 3984 3985 Style.ColumnLimit = 40; 3986 EXPECT_EQ("namespace aaaaaaaaaa {\n" 3987 "namespace bbbbbbbbbb {\n" 3988 "}} // namespace aaaaaaaaaa::bbbbbbbbbb", 3989 format("namespace aaaaaaaaaa {\n" 3990 "namespace bbbbbbbbbb {\n" 3991 "} // namespace bbbbbbbbbb\n" 3992 "} // namespace aaaaaaaaaa", 3993 Style)); 3994 3995 EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n" 3996 "namespace cccccc {\n" 3997 "}}} // namespace aaaaaa::bbbbbb::cccccc", 3998 format("namespace aaaaaa {\n" 3999 "namespace bbbbbb {\n" 4000 "namespace cccccc {\n" 4001 "} // namespace cccccc\n" 4002 "} // namespace bbbbbb\n" 4003 "} // namespace aaaaaa", 4004 Style)); 4005 Style.ColumnLimit = 80; 4006 4007 // Extra semicolon after 'inner' closing brace prevents merging 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 // Extra semicolon after 'outer' closing brace is conserved 4017 EXPECT_EQ("namespace out { namespace in {\n" 4018 "}}; // namespace out::in", 4019 format("namespace out {\n" 4020 "namespace in {\n" 4021 "} // namespace in\n" 4022 "}; // namespace out", 4023 Style)); 4024 4025 Style.NamespaceIndentation = FormatStyle::NI_All; 4026 EXPECT_EQ("namespace out { namespace in {\n" 4027 " int i;\n" 4028 "}} // namespace out::in", 4029 format("namespace out {\n" 4030 "namespace in {\n" 4031 "int i;\n" 4032 "} // namespace in\n" 4033 "} // namespace out", 4034 Style)); 4035 EXPECT_EQ("namespace out { namespace mid {\n" 4036 " namespace in {\n" 4037 " int j;\n" 4038 " } // namespace in\n" 4039 " int k;\n" 4040 "}} // namespace out::mid", 4041 format("namespace out { namespace mid {\n" 4042 "namespace in { int j; } // namespace in\n" 4043 "int k; }} // namespace out::mid", 4044 Style)); 4045 4046 Style.NamespaceIndentation = FormatStyle::NI_Inner; 4047 EXPECT_EQ("namespace out { namespace in {\n" 4048 " int i;\n" 4049 "}} // namespace out::in", 4050 format("namespace out {\n" 4051 "namespace in {\n" 4052 "int i;\n" 4053 "} // namespace in\n" 4054 "} // namespace out", 4055 Style)); 4056 EXPECT_EQ("namespace out { namespace mid { namespace in {\n" 4057 " int i;\n" 4058 "}}} // namespace out::mid::in", 4059 format("namespace out {\n" 4060 "namespace mid {\n" 4061 "namespace in {\n" 4062 "int i;\n" 4063 "} // namespace in\n" 4064 "} // namespace mid\n" 4065 "} // namespace out", 4066 Style)); 4067 4068 Style.CompactNamespaces = true; 4069 Style.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 4070 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4071 Style.BraceWrapping.BeforeLambdaBody = true; 4072 verifyFormat("namespace out { namespace in {\n" 4073 "}} // namespace out::in", 4074 Style); 4075 EXPECT_EQ("namespace out { namespace in {\n" 4076 "}} // namespace out::in", 4077 format("namespace out {\n" 4078 "namespace in {\n" 4079 "} // namespace in\n" 4080 "} // namespace out", 4081 Style)); 4082 } 4083 4084 TEST_F(FormatTest, FormatsExternC) { 4085 verifyFormat("extern \"C\" {\nint a;"); 4086 verifyFormat("extern \"C\" {}"); 4087 verifyFormat("extern \"C\" {\n" 4088 "int foo();\n" 4089 "}"); 4090 verifyFormat("extern \"C\" int foo() {}"); 4091 verifyFormat("extern \"C\" int foo();"); 4092 verifyFormat("extern \"C\" int foo() {\n" 4093 " int i = 42;\n" 4094 " return i;\n" 4095 "}"); 4096 4097 FormatStyle Style = getLLVMStyle(); 4098 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4099 Style.BraceWrapping.AfterFunction = true; 4100 verifyFormat("extern \"C\" int foo() {}", Style); 4101 verifyFormat("extern \"C\" int foo();", Style); 4102 verifyFormat("extern \"C\" int foo()\n" 4103 "{\n" 4104 " int i = 42;\n" 4105 " return i;\n" 4106 "}", 4107 Style); 4108 4109 Style.BraceWrapping.AfterExternBlock = true; 4110 Style.BraceWrapping.SplitEmptyRecord = false; 4111 verifyFormat("extern \"C\"\n" 4112 "{}", 4113 Style); 4114 verifyFormat("extern \"C\"\n" 4115 "{\n" 4116 " int foo();\n" 4117 "}", 4118 Style); 4119 } 4120 4121 TEST_F(FormatTest, IndentExternBlockStyle) { 4122 FormatStyle Style = getLLVMStyle(); 4123 Style.IndentWidth = 2; 4124 4125 Style.IndentExternBlock = FormatStyle::IEBS_Indent; 4126 verifyFormat("extern \"C\" { /*9*/\n" 4127 "}", 4128 Style); 4129 verifyFormat("extern \"C\" {\n" 4130 " int foo10();\n" 4131 "}", 4132 Style); 4133 4134 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; 4135 verifyFormat("extern \"C\" { /*11*/\n" 4136 "}", 4137 Style); 4138 verifyFormat("extern \"C\" {\n" 4139 "int foo12();\n" 4140 "}", 4141 Style); 4142 4143 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4144 Style.BraceWrapping.AfterExternBlock = true; 4145 Style.IndentExternBlock = FormatStyle::IEBS_Indent; 4146 verifyFormat("extern \"C\"\n" 4147 "{ /*13*/\n" 4148 "}", 4149 Style); 4150 verifyFormat("extern \"C\"\n{\n" 4151 " int foo14();\n" 4152 "}", 4153 Style); 4154 4155 Style.BraceWrapping.AfterExternBlock = false; 4156 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; 4157 verifyFormat("extern \"C\" { /*15*/\n" 4158 "}", 4159 Style); 4160 verifyFormat("extern \"C\" {\n" 4161 "int foo16();\n" 4162 "}", 4163 Style); 4164 4165 Style.BraceWrapping.AfterExternBlock = true; 4166 verifyFormat("extern \"C\"\n" 4167 "{ /*13*/\n" 4168 "}", 4169 Style); 4170 verifyFormat("extern \"C\"\n" 4171 "{\n" 4172 "int foo14();\n" 4173 "}", 4174 Style); 4175 4176 Style.IndentExternBlock = FormatStyle::IEBS_Indent; 4177 verifyFormat("extern \"C\"\n" 4178 "{ /*13*/\n" 4179 "}", 4180 Style); 4181 verifyFormat("extern \"C\"\n" 4182 "{\n" 4183 " int foo14();\n" 4184 "}", 4185 Style); 4186 } 4187 4188 TEST_F(FormatTest, FormatsInlineASM) { 4189 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); 4190 verifyFormat("asm(\"nop\" ::: \"memory\");"); 4191 verifyFormat( 4192 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 4193 " \"cpuid\\n\\t\"\n" 4194 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 4195 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" 4196 " : \"a\"(value));"); 4197 EXPECT_EQ( 4198 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 4199 " __asm {\n" 4200 " mov edx,[that] // vtable in edx\n" 4201 " mov eax,methodIndex\n" 4202 " call [edx][eax*4] // stdcall\n" 4203 " }\n" 4204 "}", 4205 format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 4206 " __asm {\n" 4207 " mov edx,[that] // vtable in edx\n" 4208 " mov eax,methodIndex\n" 4209 " call [edx][eax*4] // stdcall\n" 4210 " }\n" 4211 "}")); 4212 EXPECT_EQ("_asm {\n" 4213 " xor eax, eax;\n" 4214 " cpuid;\n" 4215 "}", 4216 format("_asm {\n" 4217 " xor eax, eax;\n" 4218 " cpuid;\n" 4219 "}")); 4220 verifyFormat("void function() {\n" 4221 " // comment\n" 4222 " asm(\"\");\n" 4223 "}"); 4224 EXPECT_EQ("__asm {\n" 4225 "}\n" 4226 "int i;", 4227 format("__asm {\n" 4228 "}\n" 4229 "int i;")); 4230 } 4231 4232 TEST_F(FormatTest, FormatTryCatch) { 4233 verifyFormat("try {\n" 4234 " throw a * b;\n" 4235 "} catch (int a) {\n" 4236 " // Do nothing.\n" 4237 "} catch (...) {\n" 4238 " exit(42);\n" 4239 "}"); 4240 4241 // Function-level try statements. 4242 verifyFormat("int f() try { return 4; } catch (...) {\n" 4243 " return 5;\n" 4244 "}"); 4245 verifyFormat("class A {\n" 4246 " int a;\n" 4247 " A() try : a(0) {\n" 4248 " } catch (...) {\n" 4249 " throw;\n" 4250 " }\n" 4251 "};\n"); 4252 verifyFormat("class A {\n" 4253 " int a;\n" 4254 " A() try : a(0), b{1} {\n" 4255 " } catch (...) {\n" 4256 " throw;\n" 4257 " }\n" 4258 "};\n"); 4259 verifyFormat("class A {\n" 4260 " int a;\n" 4261 " A() try : a(0), b{1}, c{2} {\n" 4262 " } catch (...) {\n" 4263 " throw;\n" 4264 " }\n" 4265 "};\n"); 4266 verifyFormat("class A {\n" 4267 " int a;\n" 4268 " A() try : a(0), b{1}, c{2} {\n" 4269 " { // New scope.\n" 4270 " }\n" 4271 " } catch (...) {\n" 4272 " throw;\n" 4273 " }\n" 4274 "};\n"); 4275 4276 // Incomplete try-catch blocks. 4277 verifyIncompleteFormat("try {} catch ("); 4278 } 4279 4280 TEST_F(FormatTest, FormatTryAsAVariable) { 4281 verifyFormat("int try;"); 4282 verifyFormat("int try, size;"); 4283 verifyFormat("try = foo();"); 4284 verifyFormat("if (try < size) {\n return true;\n}"); 4285 4286 verifyFormat("int catch;"); 4287 verifyFormat("int catch, size;"); 4288 verifyFormat("catch = foo();"); 4289 verifyFormat("if (catch < size) {\n return true;\n}"); 4290 4291 FormatStyle Style = getLLVMStyle(); 4292 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4293 Style.BraceWrapping.AfterFunction = true; 4294 Style.BraceWrapping.BeforeCatch = true; 4295 verifyFormat("try {\n" 4296 " int bar = 1;\n" 4297 "}\n" 4298 "catch (...) {\n" 4299 " int bar = 1;\n" 4300 "}", 4301 Style); 4302 verifyFormat("#if NO_EX\n" 4303 "try\n" 4304 "#endif\n" 4305 "{\n" 4306 "}\n" 4307 "#if NO_EX\n" 4308 "catch (...) {\n" 4309 "}", 4310 Style); 4311 verifyFormat("try /* abc */ {\n" 4312 " int bar = 1;\n" 4313 "}\n" 4314 "catch (...) {\n" 4315 " int bar = 1;\n" 4316 "}", 4317 Style); 4318 verifyFormat("try\n" 4319 "// abc\n" 4320 "{\n" 4321 " int bar = 1;\n" 4322 "}\n" 4323 "catch (...) {\n" 4324 " int bar = 1;\n" 4325 "}", 4326 Style); 4327 } 4328 4329 TEST_F(FormatTest, FormatSEHTryCatch) { 4330 verifyFormat("__try {\n" 4331 " int a = b * c;\n" 4332 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n" 4333 " // Do nothing.\n" 4334 "}"); 4335 4336 verifyFormat("__try {\n" 4337 " int a = b * c;\n" 4338 "} __finally {\n" 4339 " // Do nothing.\n" 4340 "}"); 4341 4342 verifyFormat("DEBUG({\n" 4343 " __try {\n" 4344 " } __finally {\n" 4345 " }\n" 4346 "});\n"); 4347 } 4348 4349 TEST_F(FormatTest, IncompleteTryCatchBlocks) { 4350 verifyFormat("try {\n" 4351 " f();\n" 4352 "} catch {\n" 4353 " g();\n" 4354 "}"); 4355 verifyFormat("try {\n" 4356 " f();\n" 4357 "} catch (A a) MACRO(x) {\n" 4358 " g();\n" 4359 "} catch (B b) MACRO(x) {\n" 4360 " g();\n" 4361 "}"); 4362 } 4363 4364 TEST_F(FormatTest, FormatTryCatchBraceStyles) { 4365 FormatStyle Style = getLLVMStyle(); 4366 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, 4367 FormatStyle::BS_WebKit}) { 4368 Style.BreakBeforeBraces = BraceStyle; 4369 verifyFormat("try {\n" 4370 " // something\n" 4371 "} catch (...) {\n" 4372 " // something\n" 4373 "}", 4374 Style); 4375 } 4376 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 4377 verifyFormat("try {\n" 4378 " // something\n" 4379 "}\n" 4380 "catch (...) {\n" 4381 " // something\n" 4382 "}", 4383 Style); 4384 verifyFormat("__try {\n" 4385 " // something\n" 4386 "}\n" 4387 "__finally {\n" 4388 " // something\n" 4389 "}", 4390 Style); 4391 verifyFormat("@try {\n" 4392 " // something\n" 4393 "}\n" 4394 "@finally {\n" 4395 " // something\n" 4396 "}", 4397 Style); 4398 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 4399 verifyFormat("try\n" 4400 "{\n" 4401 " // something\n" 4402 "}\n" 4403 "catch (...)\n" 4404 "{\n" 4405 " // something\n" 4406 "}", 4407 Style); 4408 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 4409 verifyFormat("try\n" 4410 " {\n" 4411 " // something white\n" 4412 " }\n" 4413 "catch (...)\n" 4414 " {\n" 4415 " // something white\n" 4416 " }", 4417 Style); 4418 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 4419 verifyFormat("try\n" 4420 " {\n" 4421 " // something\n" 4422 " }\n" 4423 "catch (...)\n" 4424 " {\n" 4425 " // something\n" 4426 " }", 4427 Style); 4428 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4429 Style.BraceWrapping.BeforeCatch = true; 4430 verifyFormat("try {\n" 4431 " // something\n" 4432 "}\n" 4433 "catch (...) {\n" 4434 " // something\n" 4435 "}", 4436 Style); 4437 } 4438 4439 TEST_F(FormatTest, StaticInitializers) { 4440 verifyFormat("static SomeClass SC = {1, 'a'};"); 4441 4442 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" 4443 " 100000000, " 4444 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); 4445 4446 // Here, everything other than the "}" would fit on a line. 4447 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" 4448 " 10000000000000000000000000};"); 4449 EXPECT_EQ("S s = {a,\n" 4450 "\n" 4451 " b};", 4452 format("S s = {\n" 4453 " a,\n" 4454 "\n" 4455 " b\n" 4456 "};")); 4457 4458 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first 4459 // line. However, the formatting looks a bit off and this probably doesn't 4460 // happen often in practice. 4461 verifyFormat("static int Variable[1] = {\n" 4462 " {1000000000000000000000000000000000000}};", 4463 getLLVMStyleWithColumns(40)); 4464 } 4465 4466 TEST_F(FormatTest, DesignatedInitializers) { 4467 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 4468 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" 4469 " .bbbbbbbbbb = 2,\n" 4470 " .cccccccccc = 3,\n" 4471 " .dddddddddd = 4,\n" 4472 " .eeeeeeeeee = 5};"); 4473 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 4474 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" 4475 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" 4476 " .ccccccccccccccccccccccccccc = 3,\n" 4477 " .ddddddddddddddddddddddddddd = 4,\n" 4478 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); 4479 4480 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); 4481 4482 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 4483 verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n" 4484 " [2] = bbbbbbbbbb,\n" 4485 " [3] = cccccccccc,\n" 4486 " [4] = dddddddddd,\n" 4487 " [5] = eeeeeeeeee};"); 4488 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 4489 " [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4490 " [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 4491 " [3] = cccccccccccccccccccccccccccccccccccccc,\n" 4492 " [4] = dddddddddddddddddddddddddddddddddddddd,\n" 4493 " [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};"); 4494 } 4495 4496 TEST_F(FormatTest, NestedStaticInitializers) { 4497 verifyFormat("static A x = {{{}}};\n"); 4498 verifyFormat("static A x = {{{init1, init2, init3, init4},\n" 4499 " {init1, init2, init3, init4}}};", 4500 getLLVMStyleWithColumns(50)); 4501 4502 verifyFormat("somes Status::global_reps[3] = {\n" 4503 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 4504 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 4505 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", 4506 getLLVMStyleWithColumns(60)); 4507 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n" 4508 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 4509 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 4510 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};"); 4511 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" 4512 " {rect.fRight - rect.fLeft, rect.fBottom - " 4513 "rect.fTop}};"); 4514 4515 verifyFormat( 4516 "SomeArrayOfSomeType a = {\n" 4517 " {{1, 2, 3},\n" 4518 " {1, 2, 3},\n" 4519 " {111111111111111111111111111111, 222222222222222222222222222222,\n" 4520 " 333333333333333333333333333333},\n" 4521 " {1, 2, 3},\n" 4522 " {1, 2, 3}}};"); 4523 verifyFormat( 4524 "SomeArrayOfSomeType a = {\n" 4525 " {{1, 2, 3}},\n" 4526 " {{1, 2, 3}},\n" 4527 " {{111111111111111111111111111111, 222222222222222222222222222222,\n" 4528 " 333333333333333333333333333333}},\n" 4529 " {{1, 2, 3}},\n" 4530 " {{1, 2, 3}}};"); 4531 4532 verifyFormat("struct {\n" 4533 " unsigned bit;\n" 4534 " const char *const name;\n" 4535 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" 4536 " {kOsWin, \"Windows\"},\n" 4537 " {kOsLinux, \"Linux\"},\n" 4538 " {kOsCrOS, \"Chrome OS\"}};"); 4539 verifyFormat("struct {\n" 4540 " unsigned bit;\n" 4541 " const char *const name;\n" 4542 "} kBitsToOs[] = {\n" 4543 " {kOsMac, \"Mac\"},\n" 4544 " {kOsWin, \"Windows\"},\n" 4545 " {kOsLinux, \"Linux\"},\n" 4546 " {kOsCrOS, \"Chrome OS\"},\n" 4547 "};"); 4548 } 4549 4550 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 4551 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 4552 " \\\n" 4553 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 4554 } 4555 4556 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 4557 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 4558 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 4559 4560 // Do break defaulted and deleted functions. 4561 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 4562 " default;", 4563 getLLVMStyleWithColumns(40)); 4564 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 4565 " delete;", 4566 getLLVMStyleWithColumns(40)); 4567 } 4568 4569 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { 4570 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3", 4571 getLLVMStyleWithColumns(40)); 4572 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 4573 getLLVMStyleWithColumns(40)); 4574 EXPECT_EQ("#define Q \\\n" 4575 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" 4576 " \"aaaaaaaa.cpp\"", 4577 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 4578 getLLVMStyleWithColumns(40))); 4579 } 4580 4581 TEST_F(FormatTest, UnderstandsLinePPDirective) { 4582 EXPECT_EQ("# 123 \"A string literal\"", 4583 format(" # 123 \"A string literal\"")); 4584 } 4585 4586 TEST_F(FormatTest, LayoutUnknownPPDirective) { 4587 EXPECT_EQ("#;", format("#;")); 4588 verifyFormat("#\n;\n;\n;"); 4589 } 4590 4591 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 4592 EXPECT_EQ("#line 42 \"test\"\n", 4593 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 4594 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", 4595 getLLVMStyleWithColumns(12))); 4596 } 4597 4598 TEST_F(FormatTest, EndOfFileEndsPPDirective) { 4599 EXPECT_EQ("#line 42 \"test\"", 4600 format("# \\\n line \\\n 42 \\\n \"test\"")); 4601 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); 4602 } 4603 4604 TEST_F(FormatTest, DoesntRemoveUnknownTokens) { 4605 verifyFormat("#define A \\x20"); 4606 verifyFormat("#define A \\ x20"); 4607 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20")); 4608 verifyFormat("#define A ''"); 4609 verifyFormat("#define A ''qqq"); 4610 verifyFormat("#define A `qqq"); 4611 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); 4612 EXPECT_EQ("const char *c = STRINGIFY(\n" 4613 "\\na : b);", 4614 format("const char * c = STRINGIFY(\n" 4615 "\\na : b);")); 4616 4617 verifyFormat("a\r\\"); 4618 verifyFormat("a\v\\"); 4619 verifyFormat("a\f\\"); 4620 } 4621 4622 TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) { 4623 FormatStyle style = getChromiumStyle(FormatStyle::LK_Cpp); 4624 style.IndentWidth = 4; 4625 style.PPIndentWidth = 1; 4626 4627 style.IndentPPDirectives = FormatStyle::PPDIS_None; 4628 verifyFormat("#ifdef __linux__\n" 4629 "void foo() {\n" 4630 " int x = 0;\n" 4631 "}\n" 4632 "#define FOO\n" 4633 "#endif\n" 4634 "void bar() {\n" 4635 " int y = 0;\n" 4636 "}\n", 4637 style); 4638 4639 style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 4640 verifyFormat("#ifdef __linux__\n" 4641 "void foo() {\n" 4642 " int x = 0;\n" 4643 "}\n" 4644 "# define FOO foo\n" 4645 "#endif\n" 4646 "void bar() {\n" 4647 " int y = 0;\n" 4648 "}\n", 4649 style); 4650 4651 style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash; 4652 verifyFormat("#ifdef __linux__\n" 4653 "void foo() {\n" 4654 " int x = 0;\n" 4655 "}\n" 4656 " #define FOO foo\n" 4657 "#endif\n" 4658 "void bar() {\n" 4659 " int y = 0;\n" 4660 "}\n", 4661 style); 4662 } 4663 4664 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 4665 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 4666 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 4667 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 4668 // FIXME: We never break before the macro name. 4669 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12)); 4670 4671 verifyFormat("#define A A\n#define A A"); 4672 verifyFormat("#define A(X) A\n#define A A"); 4673 4674 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 4675 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 4676 } 4677 4678 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 4679 EXPECT_EQ("// somecomment\n" 4680 "#include \"a.h\"\n" 4681 "#define A( \\\n" 4682 " A, B)\n" 4683 "#include \"b.h\"\n" 4684 "// somecomment\n", 4685 format(" // somecomment\n" 4686 " #include \"a.h\"\n" 4687 "#define A(A,\\\n" 4688 " B)\n" 4689 " #include \"b.h\"\n" 4690 " // somecomment\n", 4691 getLLVMStyleWithColumns(13))); 4692 } 4693 4694 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } 4695 4696 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 4697 EXPECT_EQ("#define A \\\n" 4698 " c; \\\n" 4699 " e;\n" 4700 "f;", 4701 format("#define A c; e;\n" 4702 "f;", 4703 getLLVMStyleWithColumns(14))); 4704 } 4705 4706 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } 4707 4708 TEST_F(FormatTest, MacroDefinitionInsideStatement) { 4709 EXPECT_EQ("int x,\n" 4710 "#define A\n" 4711 " y;", 4712 format("int x,\n#define A\ny;")); 4713 } 4714 4715 TEST_F(FormatTest, HashInMacroDefinition) { 4716 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle())); 4717 EXPECT_EQ("#define A(c) u#c", format("#define A(c) u#c", getLLVMStyle())); 4718 EXPECT_EQ("#define A(c) U#c", format("#define A(c) U#c", getLLVMStyle())); 4719 EXPECT_EQ("#define A(c) u8#c", format("#define A(c) u8#c", getLLVMStyle())); 4720 EXPECT_EQ("#define A(c) LR#c", format("#define A(c) LR#c", getLLVMStyle())); 4721 EXPECT_EQ("#define A(c) uR#c", format("#define A(c) uR#c", getLLVMStyle())); 4722 EXPECT_EQ("#define A(c) UR#c", format("#define A(c) UR#c", getLLVMStyle())); 4723 EXPECT_EQ("#define A(c) u8R#c", format("#define A(c) u8R#c", getLLVMStyle())); 4724 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 4725 verifyFormat("#define A \\\n" 4726 " { \\\n" 4727 " f(#c); \\\n" 4728 " }", 4729 getLLVMStyleWithColumns(11)); 4730 4731 verifyFormat("#define A(X) \\\n" 4732 " void function##X()", 4733 getLLVMStyleWithColumns(22)); 4734 4735 verifyFormat("#define A(a, b, c) \\\n" 4736 " void a##b##c()", 4737 getLLVMStyleWithColumns(22)); 4738 4739 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 4740 } 4741 4742 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 4743 EXPECT_EQ("#define A (x)", format("#define A (x)")); 4744 EXPECT_EQ("#define A(x)", format("#define A(x)")); 4745 4746 FormatStyle Style = getLLVMStyle(); 4747 Style.SpaceBeforeParens = FormatStyle::SBPO_Never; 4748 verifyFormat("#define true ((foo)1)", Style); 4749 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 4750 verifyFormat("#define false((foo)0)", Style); 4751 } 4752 4753 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 4754 EXPECT_EQ("#define A b;", format("#define A \\\n" 4755 " \\\n" 4756 " b;", 4757 getLLVMStyleWithColumns(25))); 4758 EXPECT_EQ("#define A \\\n" 4759 " \\\n" 4760 " a; \\\n" 4761 " b;", 4762 format("#define A \\\n" 4763 " \\\n" 4764 " a; \\\n" 4765 " b;", 4766 getLLVMStyleWithColumns(11))); 4767 EXPECT_EQ("#define A \\\n" 4768 " a; \\\n" 4769 " \\\n" 4770 " b;", 4771 format("#define A \\\n" 4772 " a; \\\n" 4773 " \\\n" 4774 " b;", 4775 getLLVMStyleWithColumns(11))); 4776 } 4777 4778 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 4779 verifyIncompleteFormat("#define A :"); 4780 verifyFormat("#define SOMECASES \\\n" 4781 " case 1: \\\n" 4782 " case 2\n", 4783 getLLVMStyleWithColumns(20)); 4784 verifyFormat("#define MACRO(a) \\\n" 4785 " if (a) \\\n" 4786 " f(); \\\n" 4787 " else \\\n" 4788 " g()", 4789 getLLVMStyleWithColumns(18)); 4790 verifyFormat("#define A template <typename T>"); 4791 verifyIncompleteFormat("#define STR(x) #x\n" 4792 "f(STR(this_is_a_string_literal{));"); 4793 verifyFormat("#pragma omp threadprivate( \\\n" 4794 " y)), // expected-warning", 4795 getLLVMStyleWithColumns(28)); 4796 verifyFormat("#d, = };"); 4797 verifyFormat("#if \"a"); 4798 verifyIncompleteFormat("({\n" 4799 "#define b \\\n" 4800 " } \\\n" 4801 " a\n" 4802 "a", 4803 getLLVMStyleWithColumns(15)); 4804 verifyFormat("#define A \\\n" 4805 " { \\\n" 4806 " {\n" 4807 "#define B \\\n" 4808 " } \\\n" 4809 " }", 4810 getLLVMStyleWithColumns(15)); 4811 verifyNoCrash("#if a\na(\n#else\n#endif\n{a"); 4812 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}"); 4813 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};"); 4814 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}"); 4815 } 4816 4817 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { 4818 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. 4819 EXPECT_EQ("class A : public QObject {\n" 4820 " Q_OBJECT\n" 4821 "\n" 4822 " A() {}\n" 4823 "};", 4824 format("class A : public QObject {\n" 4825 " Q_OBJECT\n" 4826 "\n" 4827 " A() {\n}\n" 4828 "} ;")); 4829 EXPECT_EQ("MACRO\n" 4830 "/*static*/ int i;", 4831 format("MACRO\n" 4832 " /*static*/ int i;")); 4833 EXPECT_EQ("SOME_MACRO\n" 4834 "namespace {\n" 4835 "void f();\n" 4836 "} // namespace", 4837 format("SOME_MACRO\n" 4838 " namespace {\n" 4839 "void f( );\n" 4840 "} // namespace")); 4841 // Only if the identifier contains at least 5 characters. 4842 EXPECT_EQ("HTTP f();", format("HTTP\nf();")); 4843 EXPECT_EQ("MACRO\nf();", format("MACRO\nf();")); 4844 // Only if everything is upper case. 4845 EXPECT_EQ("class A : public QObject {\n" 4846 " Q_Object A() {}\n" 4847 "};", 4848 format("class A : public QObject {\n" 4849 " Q_Object\n" 4850 " A() {\n}\n" 4851 "} ;")); 4852 4853 // Only if the next line can actually start an unwrapped line. 4854 EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;", 4855 format("SOME_WEIRD_LOG_MACRO\n" 4856 "<< SomeThing;")); 4857 4858 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " 4859 "(n, buffers))\n", 4860 getChromiumStyle(FormatStyle::LK_Cpp)); 4861 4862 // See PR41483 4863 EXPECT_EQ("/**/ FOO(a)\n" 4864 "FOO(b)", 4865 format("/**/ FOO(a)\n" 4866 "FOO(b)")); 4867 } 4868 4869 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { 4870 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 4871 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 4872 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 4873 "class X {};\n" 4874 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 4875 "int *createScopDetectionPass() { return 0; }", 4876 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 4877 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 4878 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 4879 " class X {};\n" 4880 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 4881 " int *createScopDetectionPass() { return 0; }")); 4882 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as 4883 // braces, so that inner block is indented one level more. 4884 EXPECT_EQ("int q() {\n" 4885 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 4886 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 4887 " IPC_END_MESSAGE_MAP()\n" 4888 "}", 4889 format("int q() {\n" 4890 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 4891 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 4892 " IPC_END_MESSAGE_MAP()\n" 4893 "}")); 4894 4895 // Same inside macros. 4896 EXPECT_EQ("#define LIST(L) \\\n" 4897 " L(A) \\\n" 4898 " L(B) \\\n" 4899 " L(C)", 4900 format("#define LIST(L) \\\n" 4901 " L(A) \\\n" 4902 " L(B) \\\n" 4903 " L(C)", 4904 getGoogleStyle())); 4905 4906 // These must not be recognized as macros. 4907 EXPECT_EQ("int q() {\n" 4908 " f(x);\n" 4909 " f(x) {}\n" 4910 " f(x)->g();\n" 4911 " f(x)->*g();\n" 4912 " f(x).g();\n" 4913 " f(x) = x;\n" 4914 " f(x) += x;\n" 4915 " f(x) -= x;\n" 4916 " f(x) *= x;\n" 4917 " f(x) /= x;\n" 4918 " f(x) %= x;\n" 4919 " f(x) &= x;\n" 4920 " f(x) |= x;\n" 4921 " f(x) ^= x;\n" 4922 " f(x) >>= x;\n" 4923 " f(x) <<= x;\n" 4924 " f(x)[y].z();\n" 4925 " LOG(INFO) << x;\n" 4926 " ifstream(x) >> x;\n" 4927 "}\n", 4928 format("int q() {\n" 4929 " f(x)\n;\n" 4930 " f(x)\n {}\n" 4931 " f(x)\n->g();\n" 4932 " f(x)\n->*g();\n" 4933 " f(x)\n.g();\n" 4934 " f(x)\n = x;\n" 4935 " f(x)\n += x;\n" 4936 " f(x)\n -= x;\n" 4937 " f(x)\n *= x;\n" 4938 " f(x)\n /= x;\n" 4939 " f(x)\n %= x;\n" 4940 " f(x)\n &= x;\n" 4941 " f(x)\n |= x;\n" 4942 " f(x)\n ^= x;\n" 4943 " f(x)\n >>= x;\n" 4944 " f(x)\n <<= x;\n" 4945 " f(x)\n[y].z();\n" 4946 " LOG(INFO)\n << x;\n" 4947 " ifstream(x)\n >> x;\n" 4948 "}\n")); 4949 EXPECT_EQ("int q() {\n" 4950 " F(x)\n" 4951 " if (1) {\n" 4952 " }\n" 4953 " F(x)\n" 4954 " while (1) {\n" 4955 " }\n" 4956 " F(x)\n" 4957 " G(x);\n" 4958 " F(x)\n" 4959 " try {\n" 4960 " Q();\n" 4961 " } catch (...) {\n" 4962 " }\n" 4963 "}\n", 4964 format("int q() {\n" 4965 "F(x)\n" 4966 "if (1) {}\n" 4967 "F(x)\n" 4968 "while (1) {}\n" 4969 "F(x)\n" 4970 "G(x);\n" 4971 "F(x)\n" 4972 "try { Q(); } catch (...) {}\n" 4973 "}\n")); 4974 EXPECT_EQ("class A {\n" 4975 " A() : t(0) {}\n" 4976 " A(int i) noexcept() : {}\n" 4977 " A(X x)\n" // FIXME: function-level try blocks are broken. 4978 " try : t(0) {\n" 4979 " } catch (...) {\n" 4980 " }\n" 4981 "};", 4982 format("class A {\n" 4983 " A()\n : t(0) {}\n" 4984 " A(int i)\n noexcept() : {}\n" 4985 " A(X x)\n" 4986 " try : t(0) {} catch (...) {}\n" 4987 "};")); 4988 FormatStyle Style = getLLVMStyle(); 4989 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4990 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 4991 Style.BraceWrapping.AfterFunction = true; 4992 EXPECT_EQ("void f()\n" 4993 "try\n" 4994 "{\n" 4995 "}", 4996 format("void f() try {\n" 4997 "}", 4998 Style)); 4999 EXPECT_EQ("class SomeClass {\n" 5000 "public:\n" 5001 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 5002 "};", 5003 format("class SomeClass {\n" 5004 "public:\n" 5005 " SomeClass()\n" 5006 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 5007 "};")); 5008 EXPECT_EQ("class SomeClass {\n" 5009 "public:\n" 5010 " SomeClass()\n" 5011 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 5012 "};", 5013 format("class SomeClass {\n" 5014 "public:\n" 5015 " SomeClass()\n" 5016 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 5017 "};", 5018 getLLVMStyleWithColumns(40))); 5019 5020 verifyFormat("MACRO(>)"); 5021 5022 // Some macros contain an implicit semicolon. 5023 Style = getLLVMStyle(); 5024 Style.StatementMacros.push_back("FOO"); 5025 verifyFormat("FOO(a) int b = 0;"); 5026 verifyFormat("FOO(a)\n" 5027 "int b = 0;", 5028 Style); 5029 verifyFormat("FOO(a);\n" 5030 "int b = 0;", 5031 Style); 5032 verifyFormat("FOO(argc, argv, \"4.0.2\")\n" 5033 "int b = 0;", 5034 Style); 5035 verifyFormat("FOO()\n" 5036 "int b = 0;", 5037 Style); 5038 verifyFormat("FOO\n" 5039 "int b = 0;", 5040 Style); 5041 verifyFormat("void f() {\n" 5042 " FOO(a)\n" 5043 " return a;\n" 5044 "}", 5045 Style); 5046 verifyFormat("FOO(a)\n" 5047 "FOO(b)", 5048 Style); 5049 verifyFormat("int a = 0;\n" 5050 "FOO(b)\n" 5051 "int c = 0;", 5052 Style); 5053 verifyFormat("int a = 0;\n" 5054 "int x = FOO(a)\n" 5055 "int b = 0;", 5056 Style); 5057 verifyFormat("void foo(int a) { FOO(a) }\n" 5058 "uint32_t bar() {}", 5059 Style); 5060 } 5061 5062 TEST_F(FormatTest, FormatsMacrosWithZeroColumnWidth) { 5063 FormatStyle ZeroColumn = getLLVMStyleWithColumns(0); 5064 5065 verifyFormat("#define A LOOOOOOOOOOOOOOOOOOONG() LOOOOOOOOOOOOOOOOOOONG()", 5066 ZeroColumn); 5067 } 5068 5069 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { 5070 verifyFormat("#define A \\\n" 5071 " f({ \\\n" 5072 " g(); \\\n" 5073 " });", 5074 getLLVMStyleWithColumns(11)); 5075 } 5076 5077 TEST_F(FormatTest, IndentPreprocessorDirectives) { 5078 FormatStyle Style = getLLVMStyleWithColumns(40); 5079 Style.IndentPPDirectives = FormatStyle::PPDIS_None; 5080 verifyFormat("#ifdef _WIN32\n" 5081 "#define A 0\n" 5082 "#ifdef VAR2\n" 5083 "#define B 1\n" 5084 "#include <someheader.h>\n" 5085 "#define MACRO \\\n" 5086 " some_very_long_func_aaaaaaaaaa();\n" 5087 "#endif\n" 5088 "#else\n" 5089 "#define A 1\n" 5090 "#endif", 5091 Style); 5092 Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 5093 verifyFormat("#ifdef _WIN32\n" 5094 "# define A 0\n" 5095 "# ifdef VAR2\n" 5096 "# define B 1\n" 5097 "# include <someheader.h>\n" 5098 "# define MACRO \\\n" 5099 " some_very_long_func_aaaaaaaaaa();\n" 5100 "# endif\n" 5101 "#else\n" 5102 "# define A 1\n" 5103 "#endif", 5104 Style); 5105 verifyFormat("#if A\n" 5106 "# define MACRO \\\n" 5107 " void a(int x) { \\\n" 5108 " b(); \\\n" 5109 " c(); \\\n" 5110 " d(); \\\n" 5111 " e(); \\\n" 5112 " f(); \\\n" 5113 " }\n" 5114 "#endif", 5115 Style); 5116 // Comments before include guard. 5117 verifyFormat("// file comment\n" 5118 "// file comment\n" 5119 "#ifndef HEADER_H\n" 5120 "#define HEADER_H\n" 5121 "code();\n" 5122 "#endif", 5123 Style); 5124 // Test with include guards. 5125 verifyFormat("#ifndef HEADER_H\n" 5126 "#define HEADER_H\n" 5127 "code();\n" 5128 "#endif", 5129 Style); 5130 // Include guards must have a #define with the same variable immediately 5131 // after #ifndef. 5132 verifyFormat("#ifndef NOT_GUARD\n" 5133 "# define FOO\n" 5134 "code();\n" 5135 "#endif", 5136 Style); 5137 5138 // Include guards must cover the entire file. 5139 verifyFormat("code();\n" 5140 "code();\n" 5141 "#ifndef NOT_GUARD\n" 5142 "# define NOT_GUARD\n" 5143 "code();\n" 5144 "#endif", 5145 Style); 5146 verifyFormat("#ifndef NOT_GUARD\n" 5147 "# define NOT_GUARD\n" 5148 "code();\n" 5149 "#endif\n" 5150 "code();", 5151 Style); 5152 // Test with trailing blank lines. 5153 verifyFormat("#ifndef HEADER_H\n" 5154 "#define HEADER_H\n" 5155 "code();\n" 5156 "#endif\n", 5157 Style); 5158 // Include guards don't have #else. 5159 verifyFormat("#ifndef NOT_GUARD\n" 5160 "# define NOT_GUARD\n" 5161 "code();\n" 5162 "#else\n" 5163 "#endif", 5164 Style); 5165 verifyFormat("#ifndef NOT_GUARD\n" 5166 "# define NOT_GUARD\n" 5167 "code();\n" 5168 "#elif FOO\n" 5169 "#endif", 5170 Style); 5171 // Non-identifier #define after potential include guard. 5172 verifyFormat("#ifndef FOO\n" 5173 "# define 1\n" 5174 "#endif\n", 5175 Style); 5176 // #if closes past last non-preprocessor line. 5177 verifyFormat("#ifndef FOO\n" 5178 "#define FOO\n" 5179 "#if 1\n" 5180 "int i;\n" 5181 "# define A 0\n" 5182 "#endif\n" 5183 "#endif\n", 5184 Style); 5185 // Don't crash if there is an #elif directive without a condition. 5186 verifyFormat("#if 1\n" 5187 "int x;\n" 5188 "#elif\n" 5189 "int y;\n" 5190 "#else\n" 5191 "int z;\n" 5192 "#endif", 5193 Style); 5194 // FIXME: This doesn't handle the case where there's code between the 5195 // #ifndef and #define but all other conditions hold. This is because when 5196 // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the 5197 // previous code line yet, so we can't detect it. 5198 EXPECT_EQ("#ifndef NOT_GUARD\n" 5199 "code();\n" 5200 "#define NOT_GUARD\n" 5201 "code();\n" 5202 "#endif", 5203 format("#ifndef NOT_GUARD\n" 5204 "code();\n" 5205 "# define NOT_GUARD\n" 5206 "code();\n" 5207 "#endif", 5208 Style)); 5209 // FIXME: This doesn't handle cases where legitimate preprocessor lines may 5210 // be outside an include guard. Examples are #pragma once and 5211 // #pragma GCC diagnostic, or anything else that does not change the meaning 5212 // of the file if it's included multiple times. 5213 EXPECT_EQ("#ifdef WIN32\n" 5214 "# pragma once\n" 5215 "#endif\n" 5216 "#ifndef HEADER_H\n" 5217 "# define HEADER_H\n" 5218 "code();\n" 5219 "#endif", 5220 format("#ifdef WIN32\n" 5221 "# pragma once\n" 5222 "#endif\n" 5223 "#ifndef HEADER_H\n" 5224 "#define HEADER_H\n" 5225 "code();\n" 5226 "#endif", 5227 Style)); 5228 // FIXME: This does not detect when there is a single non-preprocessor line 5229 // in front of an include-guard-like structure where other conditions hold 5230 // because ScopedLineState hides the line. 5231 EXPECT_EQ("code();\n" 5232 "#ifndef HEADER_H\n" 5233 "#define HEADER_H\n" 5234 "code();\n" 5235 "#endif", 5236 format("code();\n" 5237 "#ifndef HEADER_H\n" 5238 "# define HEADER_H\n" 5239 "code();\n" 5240 "#endif", 5241 Style)); 5242 // Keep comments aligned with #, otherwise indent comments normally. These 5243 // tests cannot use verifyFormat because messUp manipulates leading 5244 // whitespace. 5245 { 5246 const char *Expected = "" 5247 "void f() {\n" 5248 "#if 1\n" 5249 "// Preprocessor aligned.\n" 5250 "# define A 0\n" 5251 " // Code. Separated by blank line.\n" 5252 "\n" 5253 "# define B 0\n" 5254 " // Code. Not aligned with #\n" 5255 "# define C 0\n" 5256 "#endif"; 5257 const char *ToFormat = "" 5258 "void f() {\n" 5259 "#if 1\n" 5260 "// Preprocessor aligned.\n" 5261 "# define A 0\n" 5262 "// Code. Separated by blank line.\n" 5263 "\n" 5264 "# define B 0\n" 5265 " // Code. Not aligned with #\n" 5266 "# define C 0\n" 5267 "#endif"; 5268 EXPECT_EQ(Expected, format(ToFormat, Style)); 5269 EXPECT_EQ(Expected, format(Expected, Style)); 5270 } 5271 // Keep block quotes aligned. 5272 { 5273 const char *Expected = "" 5274 "void f() {\n" 5275 "#if 1\n" 5276 "/* Preprocessor aligned. */\n" 5277 "# define A 0\n" 5278 " /* Code. Separated by blank line. */\n" 5279 "\n" 5280 "# define B 0\n" 5281 " /* Code. Not aligned with # */\n" 5282 "# define C 0\n" 5283 "#endif"; 5284 const char *ToFormat = "" 5285 "void f() {\n" 5286 "#if 1\n" 5287 "/* Preprocessor aligned. */\n" 5288 "# define A 0\n" 5289 "/* Code. Separated by blank line. */\n" 5290 "\n" 5291 "# define B 0\n" 5292 " /* Code. Not aligned with # */\n" 5293 "# define C 0\n" 5294 "#endif"; 5295 EXPECT_EQ(Expected, format(ToFormat, Style)); 5296 EXPECT_EQ(Expected, format(Expected, Style)); 5297 } 5298 // Keep comments aligned with un-indented directives. 5299 { 5300 const char *Expected = "" 5301 "void f() {\n" 5302 "// Preprocessor aligned.\n" 5303 "#define A 0\n" 5304 " // Code. Separated by blank line.\n" 5305 "\n" 5306 "#define B 0\n" 5307 " // Code. Not aligned with #\n" 5308 "#define C 0\n"; 5309 const char *ToFormat = "" 5310 "void f() {\n" 5311 "// Preprocessor aligned.\n" 5312 "#define A 0\n" 5313 "// Code. Separated by blank line.\n" 5314 "\n" 5315 "#define B 0\n" 5316 " // Code. Not aligned with #\n" 5317 "#define C 0\n"; 5318 EXPECT_EQ(Expected, format(ToFormat, Style)); 5319 EXPECT_EQ(Expected, format(Expected, Style)); 5320 } 5321 // Test AfterHash with tabs. 5322 { 5323 FormatStyle Tabbed = Style; 5324 Tabbed.UseTab = FormatStyle::UT_Always; 5325 Tabbed.IndentWidth = 8; 5326 Tabbed.TabWidth = 8; 5327 verifyFormat("#ifdef _WIN32\n" 5328 "#\tdefine A 0\n" 5329 "#\tifdef VAR2\n" 5330 "#\t\tdefine B 1\n" 5331 "#\t\tinclude <someheader.h>\n" 5332 "#\t\tdefine MACRO \\\n" 5333 "\t\t\tsome_very_long_func_aaaaaaaaaa();\n" 5334 "#\tendif\n" 5335 "#else\n" 5336 "#\tdefine A 1\n" 5337 "#endif", 5338 Tabbed); 5339 } 5340 5341 // Regression test: Multiline-macro inside include guards. 5342 verifyFormat("#ifndef HEADER_H\n" 5343 "#define HEADER_H\n" 5344 "#define A() \\\n" 5345 " int i; \\\n" 5346 " int j;\n" 5347 "#endif // HEADER_H", 5348 getLLVMStyleWithColumns(20)); 5349 5350 Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash; 5351 // Basic before hash indent tests 5352 verifyFormat("#ifdef _WIN32\n" 5353 " #define A 0\n" 5354 " #ifdef VAR2\n" 5355 " #define B 1\n" 5356 " #include <someheader.h>\n" 5357 " #define MACRO \\\n" 5358 " some_very_long_func_aaaaaaaaaa();\n" 5359 " #endif\n" 5360 "#else\n" 5361 " #define A 1\n" 5362 "#endif", 5363 Style); 5364 verifyFormat("#if A\n" 5365 " #define MACRO \\\n" 5366 " void a(int x) { \\\n" 5367 " b(); \\\n" 5368 " c(); \\\n" 5369 " d(); \\\n" 5370 " e(); \\\n" 5371 " f(); \\\n" 5372 " }\n" 5373 "#endif", 5374 Style); 5375 // Keep comments aligned with indented directives. These 5376 // tests cannot use verifyFormat because messUp manipulates leading 5377 // whitespace. 5378 { 5379 const char *Expected = "void f() {\n" 5380 "// Aligned to preprocessor.\n" 5381 "#if 1\n" 5382 " // Aligned to code.\n" 5383 " int a;\n" 5384 " #if 1\n" 5385 " // Aligned to preprocessor.\n" 5386 " #define A 0\n" 5387 " // Aligned to code.\n" 5388 " int b;\n" 5389 " #endif\n" 5390 "#endif\n" 5391 "}"; 5392 const char *ToFormat = "void f() {\n" 5393 "// Aligned to preprocessor.\n" 5394 "#if 1\n" 5395 "// Aligned to code.\n" 5396 "int a;\n" 5397 "#if 1\n" 5398 "// Aligned to preprocessor.\n" 5399 "#define A 0\n" 5400 "// Aligned to code.\n" 5401 "int b;\n" 5402 "#endif\n" 5403 "#endif\n" 5404 "}"; 5405 EXPECT_EQ(Expected, format(ToFormat, Style)); 5406 EXPECT_EQ(Expected, format(Expected, Style)); 5407 } 5408 { 5409 const char *Expected = "void f() {\n" 5410 "/* Aligned to preprocessor. */\n" 5411 "#if 1\n" 5412 " /* Aligned to code. */\n" 5413 " int a;\n" 5414 " #if 1\n" 5415 " /* Aligned to preprocessor. */\n" 5416 " #define A 0\n" 5417 " /* Aligned to code. */\n" 5418 " int b;\n" 5419 " #endif\n" 5420 "#endif\n" 5421 "}"; 5422 const char *ToFormat = "void f() {\n" 5423 "/* Aligned to preprocessor. */\n" 5424 "#if 1\n" 5425 "/* Aligned to code. */\n" 5426 "int a;\n" 5427 "#if 1\n" 5428 "/* Aligned to preprocessor. */\n" 5429 "#define A 0\n" 5430 "/* Aligned to code. */\n" 5431 "int b;\n" 5432 "#endif\n" 5433 "#endif\n" 5434 "}"; 5435 EXPECT_EQ(Expected, format(ToFormat, Style)); 5436 EXPECT_EQ(Expected, format(Expected, Style)); 5437 } 5438 5439 // Test single comment before preprocessor 5440 verifyFormat("// Comment\n" 5441 "\n" 5442 "#if 1\n" 5443 "#endif", 5444 Style); 5445 } 5446 5447 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 5448 verifyFormat("{\n { a #c; }\n}"); 5449 } 5450 5451 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 5452 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 5453 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 5454 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 5455 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 5456 } 5457 5458 TEST_F(FormatTest, EscapedNewlines) { 5459 FormatStyle Narrow = getLLVMStyleWithColumns(11); 5460 EXPECT_EQ("#define A \\\n int i; \\\n int j;", 5461 format("#define A \\\nint i;\\\n int j;", Narrow)); 5462 EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;")); 5463 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 5464 EXPECT_EQ("/* \\ \\ \\\n */", format("\\\n/* \\ \\ \\\n */")); 5465 EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>")); 5466 5467 FormatStyle AlignLeft = getLLVMStyle(); 5468 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 5469 EXPECT_EQ("#define MACRO(x) \\\n" 5470 "private: \\\n" 5471 " int x(int a);\n", 5472 format("#define MACRO(x) \\\n" 5473 "private: \\\n" 5474 " int x(int a);\n", 5475 AlignLeft)); 5476 5477 // CRLF line endings 5478 EXPECT_EQ("#define A \\\r\n int i; \\\r\n int j;", 5479 format("#define A \\\r\nint i;\\\r\n int j;", Narrow)); 5480 EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;")); 5481 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 5482 EXPECT_EQ("/* \\ \\ \\\r\n */", format("\\\r\n/* \\ \\ \\\r\n */")); 5483 EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>")); 5484 EXPECT_EQ("#define MACRO(x) \\\r\n" 5485 "private: \\\r\n" 5486 " int x(int a);\r\n", 5487 format("#define MACRO(x) \\\r\n" 5488 "private: \\\r\n" 5489 " int x(int a);\r\n", 5490 AlignLeft)); 5491 5492 FormatStyle DontAlign = getLLVMStyle(); 5493 DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 5494 DontAlign.MaxEmptyLinesToKeep = 3; 5495 // FIXME: can't use verifyFormat here because the newline before 5496 // "public:" is not inserted the first time it's reformatted 5497 EXPECT_EQ("#define A \\\n" 5498 " class Foo { \\\n" 5499 " void bar(); \\\n" 5500 "\\\n" 5501 "\\\n" 5502 "\\\n" 5503 " public: \\\n" 5504 " void baz(); \\\n" 5505 " };", 5506 format("#define A \\\n" 5507 " class Foo { \\\n" 5508 " void bar(); \\\n" 5509 "\\\n" 5510 "\\\n" 5511 "\\\n" 5512 " public: \\\n" 5513 " void baz(); \\\n" 5514 " };", 5515 DontAlign)); 5516 } 5517 5518 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 5519 verifyFormat("#define A \\\n" 5520 " int v( \\\n" 5521 " a); \\\n" 5522 " int i;", 5523 getLLVMStyleWithColumns(11)); 5524 } 5525 5526 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 5527 EXPECT_EQ( 5528 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 5529 " \\\n" 5530 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 5531 "\n" 5532 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 5533 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 5534 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 5535 "\\\n" 5536 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 5537 " \n" 5538 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 5539 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 5540 } 5541 5542 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 5543 EXPECT_EQ("int\n" 5544 "#define A\n" 5545 " a;", 5546 format("int\n#define A\na;")); 5547 verifyFormat("functionCallTo(\n" 5548 " someOtherFunction(\n" 5549 " withSomeParameters, whichInSequence,\n" 5550 " areLongerThanALine(andAnotherCall,\n" 5551 "#define A B\n" 5552 " withMoreParamters,\n" 5553 " whichStronglyInfluenceTheLayout),\n" 5554 " andMoreParameters),\n" 5555 " trailing);", 5556 getLLVMStyleWithColumns(69)); 5557 verifyFormat("Foo::Foo()\n" 5558 "#ifdef BAR\n" 5559 " : baz(0)\n" 5560 "#endif\n" 5561 "{\n" 5562 "}"); 5563 verifyFormat("void f() {\n" 5564 " if (true)\n" 5565 "#ifdef A\n" 5566 " f(42);\n" 5567 " x();\n" 5568 "#else\n" 5569 " g();\n" 5570 " x();\n" 5571 "#endif\n" 5572 "}"); 5573 verifyFormat("void f(param1, param2,\n" 5574 " param3,\n" 5575 "#ifdef A\n" 5576 " param4(param5,\n" 5577 "#ifdef A1\n" 5578 " param6,\n" 5579 "#ifdef A2\n" 5580 " param7),\n" 5581 "#else\n" 5582 " param8),\n" 5583 " param9,\n" 5584 "#endif\n" 5585 " param10,\n" 5586 "#endif\n" 5587 " param11)\n" 5588 "#else\n" 5589 " param12)\n" 5590 "#endif\n" 5591 "{\n" 5592 " x();\n" 5593 "}", 5594 getLLVMStyleWithColumns(28)); 5595 verifyFormat("#if 1\n" 5596 "int i;"); 5597 verifyFormat("#if 1\n" 5598 "#endif\n" 5599 "#if 1\n" 5600 "#else\n" 5601 "#endif\n"); 5602 verifyFormat("DEBUG({\n" 5603 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5604 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 5605 "});\n" 5606 "#if a\n" 5607 "#else\n" 5608 "#endif"); 5609 5610 verifyIncompleteFormat("void f(\n" 5611 "#if A\n" 5612 ");\n" 5613 "#else\n" 5614 "#endif"); 5615 } 5616 5617 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { 5618 verifyFormat("#endif\n" 5619 "#if B"); 5620 } 5621 5622 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { 5623 FormatStyle SingleLine = getLLVMStyle(); 5624 SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; 5625 verifyFormat("#if 0\n" 5626 "#elif 1\n" 5627 "#endif\n" 5628 "void foo() {\n" 5629 " if (test) foo2();\n" 5630 "}", 5631 SingleLine); 5632 } 5633 5634 TEST_F(FormatTest, LayoutBlockInsideParens) { 5635 verifyFormat("functionCall({ int i; });"); 5636 verifyFormat("functionCall({\n" 5637 " int i;\n" 5638 " int j;\n" 5639 "});"); 5640 verifyFormat("functionCall(\n" 5641 " {\n" 5642 " int i;\n" 5643 " int j;\n" 5644 " },\n" 5645 " aaaa, bbbb, cccc);"); 5646 verifyFormat("functionA(functionB({\n" 5647 " int i;\n" 5648 " int j;\n" 5649 " }),\n" 5650 " aaaa, bbbb, cccc);"); 5651 verifyFormat("functionCall(\n" 5652 " {\n" 5653 " int i;\n" 5654 " int j;\n" 5655 " },\n" 5656 " aaaa, bbbb, // comment\n" 5657 " cccc);"); 5658 verifyFormat("functionA(functionB({\n" 5659 " int i;\n" 5660 " int j;\n" 5661 " }),\n" 5662 " aaaa, bbbb, // comment\n" 5663 " cccc);"); 5664 verifyFormat("functionCall(aaaa, bbbb, { int i; });"); 5665 verifyFormat("functionCall(aaaa, bbbb, {\n" 5666 " int i;\n" 5667 " int j;\n" 5668 "});"); 5669 verifyFormat( 5670 "Aaa(\n" // FIXME: There shouldn't be a linebreak here. 5671 " {\n" 5672 " int i; // break\n" 5673 " },\n" 5674 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 5675 " ccccccccccccccccc));"); 5676 verifyFormat("DEBUG({\n" 5677 " if (a)\n" 5678 " f();\n" 5679 "});"); 5680 } 5681 5682 TEST_F(FormatTest, LayoutBlockInsideStatement) { 5683 EXPECT_EQ("SOME_MACRO { int i; }\n" 5684 "int i;", 5685 format(" SOME_MACRO {int i;} int i;")); 5686 } 5687 5688 TEST_F(FormatTest, LayoutNestedBlocks) { 5689 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 5690 " struct s {\n" 5691 " int i;\n" 5692 " };\n" 5693 " s kBitsToOs[] = {{10}};\n" 5694 " for (int i = 0; i < 10; ++i)\n" 5695 " return;\n" 5696 "}"); 5697 verifyFormat("call(parameter, {\n" 5698 " something();\n" 5699 " // Comment using all columns.\n" 5700 " somethingelse();\n" 5701 "});", 5702 getLLVMStyleWithColumns(40)); 5703 verifyFormat("DEBUG( //\n" 5704 " { f(); }, a);"); 5705 verifyFormat("DEBUG( //\n" 5706 " {\n" 5707 " f(); //\n" 5708 " },\n" 5709 " a);"); 5710 5711 EXPECT_EQ("call(parameter, {\n" 5712 " something();\n" 5713 " // Comment too\n" 5714 " // looooooooooong.\n" 5715 " somethingElse();\n" 5716 "});", 5717 format("call(parameter, {\n" 5718 " something();\n" 5719 " // Comment too looooooooooong.\n" 5720 " somethingElse();\n" 5721 "});", 5722 getLLVMStyleWithColumns(29))); 5723 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });")); 5724 EXPECT_EQ("DEBUG({ // comment\n" 5725 " int i;\n" 5726 "});", 5727 format("DEBUG({ // comment\n" 5728 "int i;\n" 5729 "});")); 5730 EXPECT_EQ("DEBUG({\n" 5731 " int i;\n" 5732 "\n" 5733 " // comment\n" 5734 " int j;\n" 5735 "});", 5736 format("DEBUG({\n" 5737 " int i;\n" 5738 "\n" 5739 " // comment\n" 5740 " int j;\n" 5741 "});")); 5742 5743 verifyFormat("DEBUG({\n" 5744 " if (a)\n" 5745 " return;\n" 5746 "});"); 5747 verifyGoogleFormat("DEBUG({\n" 5748 " if (a) return;\n" 5749 "});"); 5750 FormatStyle Style = getGoogleStyle(); 5751 Style.ColumnLimit = 45; 5752 verifyFormat("Debug(\n" 5753 " aaaaa,\n" 5754 " {\n" 5755 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" 5756 " },\n" 5757 " a);", 5758 Style); 5759 5760 verifyFormat("SomeFunction({MACRO({ return output; }), b});"); 5761 5762 verifyNoCrash("^{v^{a}}"); 5763 } 5764 5765 TEST_F(FormatTest, FormatNestedBlocksInMacros) { 5766 EXPECT_EQ("#define MACRO() \\\n" 5767 " Debug(aaa, /* force line break */ \\\n" 5768 " { \\\n" 5769 " int i; \\\n" 5770 " int j; \\\n" 5771 " })", 5772 format("#define MACRO() Debug(aaa, /* force line break */ \\\n" 5773 " { int i; int j; })", 5774 getGoogleStyle())); 5775 5776 EXPECT_EQ("#define A \\\n" 5777 " [] { \\\n" 5778 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 5779 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 5780 " }", 5781 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 5782 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 5783 getGoogleStyle())); 5784 } 5785 5786 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 5787 EXPECT_EQ("{}", format("{}")); 5788 verifyFormat("enum E {};"); 5789 verifyFormat("enum E {}"); 5790 FormatStyle Style = getLLVMStyle(); 5791 Style.SpaceInEmptyBlock = true; 5792 EXPECT_EQ("void f() { }", format("void f() {}", Style)); 5793 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 5794 EXPECT_EQ("while (true) { }", format("while (true) {}", Style)); 5795 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 5796 Style.BraceWrapping.BeforeElse = false; 5797 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 5798 verifyFormat("if (a)\n" 5799 "{\n" 5800 "} else if (b)\n" 5801 "{\n" 5802 "} else\n" 5803 "{ }", 5804 Style); 5805 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never; 5806 verifyFormat("if (a) {\n" 5807 "} else if (b) {\n" 5808 "} else {\n" 5809 "}", 5810 Style); 5811 Style.BraceWrapping.BeforeElse = true; 5812 verifyFormat("if (a) { }\n" 5813 "else if (b) { }\n" 5814 "else { }", 5815 Style); 5816 } 5817 5818 TEST_F(FormatTest, FormatBeginBlockEndMacros) { 5819 FormatStyle Style = getLLVMStyle(); 5820 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$"; 5821 Style.MacroBlockEnd = "^[A-Z_]+_END$"; 5822 verifyFormat("FOO_BEGIN\n" 5823 " FOO_ENTRY\n" 5824 "FOO_END", 5825 Style); 5826 verifyFormat("FOO_BEGIN\n" 5827 " NESTED_FOO_BEGIN\n" 5828 " NESTED_FOO_ENTRY\n" 5829 " NESTED_FOO_END\n" 5830 "FOO_END", 5831 Style); 5832 verifyFormat("FOO_BEGIN(Foo, Bar)\n" 5833 " int x;\n" 5834 " x = 1;\n" 5835 "FOO_END(Baz)", 5836 Style); 5837 } 5838 5839 //===----------------------------------------------------------------------===// 5840 // Line break tests. 5841 //===----------------------------------------------------------------------===// 5842 5843 TEST_F(FormatTest, PreventConfusingIndents) { 5844 verifyFormat( 5845 "void f() {\n" 5846 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 5847 " parameter, parameter, parameter)),\n" 5848 " SecondLongCall(parameter));\n" 5849 "}"); 5850 verifyFormat( 5851 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5852 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 5853 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5854 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 5855 verifyFormat( 5856 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5857 " [aaaaaaaaaaaaaaaaaaaaaaaa\n" 5858 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 5859 " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); 5860 verifyFormat( 5861 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 5862 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 5863 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 5864 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 5865 verifyFormat("int a = bbbb && ccc &&\n" 5866 " fffff(\n" 5867 "#define A Just forcing a new line\n" 5868 " ddd);"); 5869 } 5870 5871 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 5872 verifyFormat( 5873 "bool aaaaaaa =\n" 5874 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 5875 " bbbbbbbb();"); 5876 verifyFormat( 5877 "bool aaaaaaa =\n" 5878 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" 5879 " bbbbbbbb();"); 5880 5881 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 5882 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 5883 " ccccccccc == ddddddddddd;"); 5884 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 5885 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" 5886 " ccccccccc == ddddddddddd;"); 5887 verifyFormat( 5888 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 5889 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" 5890 " ccccccccc == ddddddddddd;"); 5891 5892 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 5893 " aaaaaa) &&\n" 5894 " bbbbbb && cccccc;"); 5895 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 5896 " aaaaaa) >>\n" 5897 " bbbbbb;"); 5898 verifyFormat("aa = Whitespaces.addUntouchableComment(\n" 5899 " SourceMgr.getSpellingColumnNumber(\n" 5900 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 5901 " 1);"); 5902 5903 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5904 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 5905 " cccccc) {\n}"); 5906 verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5907 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 5908 " cccccc) {\n}"); 5909 verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5910 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 5911 " cccccc) {\n}"); 5912 verifyFormat("b = a &&\n" 5913 " // Comment\n" 5914 " b.c && d;"); 5915 5916 // If the LHS of a comparison is not a binary expression itself, the 5917 // additional linebreak confuses many people. 5918 verifyFormat( 5919 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5920 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 5921 "}"); 5922 verifyFormat( 5923 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5924 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 5925 "}"); 5926 verifyFormat( 5927 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 5928 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 5929 "}"); 5930 verifyFormat( 5931 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5932 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n" 5933 "}"); 5934 // Even explicit parentheses stress the precedence enough to make the 5935 // additional break unnecessary. 5936 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5937 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 5938 "}"); 5939 // This cases is borderline, but with the indentation it is still readable. 5940 verifyFormat( 5941 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5942 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5943 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 5944 "}", 5945 getLLVMStyleWithColumns(75)); 5946 5947 // If the LHS is a binary expression, we should still use the additional break 5948 // as otherwise the formatting hides the operator precedence. 5949 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5950 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5951 " 5) {\n" 5952 "}"); 5953 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5954 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n" 5955 " 5) {\n" 5956 "}"); 5957 5958 FormatStyle OnePerLine = getLLVMStyle(); 5959 OnePerLine.BinPackParameters = false; 5960 verifyFormat( 5961 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5962 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5963 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 5964 OnePerLine); 5965 5966 verifyFormat("int i = someFunction(aaaaaaa, 0)\n" 5967 " .aaa(aaaaaaaaaaaaa) *\n" 5968 " aaaaaaa +\n" 5969 " aaaaaaa;", 5970 getLLVMStyleWithColumns(40)); 5971 } 5972 5973 TEST_F(FormatTest, ExpressionIndentation) { 5974 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5975 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5976 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5977 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5978 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 5979 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 5980 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5981 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 5982 " ccccccccccccccccccccccccccccccccccccccccc;"); 5983 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5984 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5985 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5986 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 5987 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5988 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5989 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5990 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 5991 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5992 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5993 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5994 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 5995 verifyFormat("if () {\n" 5996 "} else if (aaaaa && bbbbb > // break\n" 5997 " ccccc) {\n" 5998 "}"); 5999 verifyFormat("if () {\n" 6000 "} else if constexpr (aaaaa && bbbbb > // break\n" 6001 " ccccc) {\n" 6002 "}"); 6003 verifyFormat("if () {\n" 6004 "} else if CONSTEXPR (aaaaa && bbbbb > // break\n" 6005 " ccccc) {\n" 6006 "}"); 6007 verifyFormat("if () {\n" 6008 "} else if (aaaaa &&\n" 6009 " bbbbb > // break\n" 6010 " ccccc &&\n" 6011 " ddddd) {\n" 6012 "}"); 6013 6014 // Presence of a trailing comment used to change indentation of b. 6015 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 6016 " b;\n" 6017 "return aaaaaaaaaaaaaaaaaaa +\n" 6018 " b; //", 6019 getLLVMStyleWithColumns(30)); 6020 } 6021 6022 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 6023 // Not sure what the best system is here. Like this, the LHS can be found 6024 // immediately above an operator (everything with the same or a higher 6025 // indent). The RHS is aligned right of the operator and so compasses 6026 // everything until something with the same indent as the operator is found. 6027 // FIXME: Is this a good system? 6028 FormatStyle Style = getLLVMStyle(); 6029 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 6030 verifyFormat( 6031 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6032 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6033 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6034 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6035 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6036 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6037 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6038 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6039 " > ccccccccccccccccccccccccccccccccccccccccc;", 6040 Style); 6041 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6042 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6043 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6044 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 6045 Style); 6046 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6047 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6048 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6049 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 6050 Style); 6051 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6052 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6053 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6054 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 6055 Style); 6056 verifyFormat("if () {\n" 6057 "} else if (aaaaa\n" 6058 " && bbbbb // break\n" 6059 " > ccccc) {\n" 6060 "}", 6061 Style); 6062 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6063 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 6064 Style); 6065 verifyFormat("return (a)\n" 6066 " // comment\n" 6067 " + b;", 6068 Style); 6069 verifyFormat( 6070 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6071 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6072 " + cc;", 6073 Style); 6074 6075 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6076 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 6077 Style); 6078 6079 // Forced by comments. 6080 verifyFormat( 6081 "unsigned ContentSize =\n" 6082 " sizeof(int16_t) // DWARF ARange version number\n" 6083 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 6084 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 6085 " + sizeof(int8_t); // Segment Size (in bytes)"); 6086 6087 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 6088 " == boost::fusion::at_c<1>(iiii).second;", 6089 Style); 6090 6091 Style.ColumnLimit = 60; 6092 verifyFormat("zzzzzzzzzz\n" 6093 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6094 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 6095 Style); 6096 6097 Style.ColumnLimit = 80; 6098 Style.IndentWidth = 4; 6099 Style.TabWidth = 4; 6100 Style.UseTab = FormatStyle::UT_Always; 6101 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6102 Style.AlignOperands = FormatStyle::OAS_DontAlign; 6103 EXPECT_EQ("return someVeryVeryLongConditionThatBarelyFitsOnALine\n" 6104 "\t&& (someOtherLongishConditionPart1\n" 6105 "\t\t|| someOtherEvenLongerNestedConditionPart2);", 6106 format("return someVeryVeryLongConditionThatBarelyFitsOnALine && " 6107 "(someOtherLongishConditionPart1 || " 6108 "someOtherEvenLongerNestedConditionPart2);", 6109 Style)); 6110 } 6111 6112 TEST_F(FormatTest, ExpressionIndentationStrictAlign) { 6113 FormatStyle Style = getLLVMStyle(); 6114 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 6115 Style.AlignOperands = FormatStyle::OAS_AlignAfterOperator; 6116 6117 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6118 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6119 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6120 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6121 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6122 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6123 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6124 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6125 " > ccccccccccccccccccccccccccccccccccccccccc;", 6126 Style); 6127 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6128 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6129 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6130 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 6131 Style); 6132 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6133 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6134 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6135 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 6136 Style); 6137 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6138 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6139 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6140 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 6141 Style); 6142 verifyFormat("if () {\n" 6143 "} else if (aaaaa\n" 6144 " && bbbbb // break\n" 6145 " > ccccc) {\n" 6146 "}", 6147 Style); 6148 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6149 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 6150 Style); 6151 verifyFormat("return (a)\n" 6152 " // comment\n" 6153 " + b;", 6154 Style); 6155 verifyFormat( 6156 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6157 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6158 " + cc;", 6159 Style); 6160 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 6161 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 6162 " : 3333333333333333;", 6163 Style); 6164 verifyFormat( 6165 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 6166 " : ccccccccccccccc ? dddddddddddddddddd\n" 6167 " : eeeeeeeeeeeeeeeeee)\n" 6168 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 6169 " : 3333333333333333;", 6170 Style); 6171 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6172 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 6173 Style); 6174 6175 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 6176 " == boost::fusion::at_c<1>(iiii).second;", 6177 Style); 6178 6179 Style.ColumnLimit = 60; 6180 verifyFormat("zzzzzzzzzzzzz\n" 6181 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6182 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 6183 Style); 6184 6185 // Forced by comments. 6186 Style.ColumnLimit = 80; 6187 verifyFormat( 6188 "unsigned ContentSize\n" 6189 " = sizeof(int16_t) // DWARF ARange version number\n" 6190 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 6191 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 6192 " + sizeof(int8_t); // Segment Size (in bytes)", 6193 Style); 6194 6195 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 6196 verifyFormat( 6197 "unsigned ContentSize =\n" 6198 " sizeof(int16_t) // DWARF ARange version number\n" 6199 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 6200 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 6201 " + sizeof(int8_t); // Segment Size (in bytes)", 6202 Style); 6203 6204 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 6205 verifyFormat( 6206 "unsigned ContentSize =\n" 6207 " sizeof(int16_t) // DWARF ARange version number\n" 6208 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 6209 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 6210 " + sizeof(int8_t); // Segment Size (in bytes)", 6211 Style); 6212 } 6213 6214 TEST_F(FormatTest, EnforcedOperatorWraps) { 6215 // Here we'd like to wrap after the || operators, but a comment is forcing an 6216 // earlier wrap. 6217 verifyFormat("bool x = aaaaa //\n" 6218 " || bbbbb\n" 6219 " //\n" 6220 " || cccc;"); 6221 } 6222 6223 TEST_F(FormatTest, NoOperandAlignment) { 6224 FormatStyle Style = getLLVMStyle(); 6225 Style.AlignOperands = FormatStyle::OAS_DontAlign; 6226 verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n" 6227 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6228 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 6229 Style); 6230 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 6231 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6232 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6233 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6234 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6235 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6236 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6237 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6238 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6239 " > ccccccccccccccccccccccccccccccccccccccccc;", 6240 Style); 6241 6242 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6243 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6244 " + cc;", 6245 Style); 6246 verifyFormat("int a = aa\n" 6247 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 6248 " * cccccccccccccccccccccccccccccccccccc;\n", 6249 Style); 6250 6251 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6252 verifyFormat("return (a > b\n" 6253 " // comment1\n" 6254 " // comment2\n" 6255 " || c);", 6256 Style); 6257 } 6258 6259 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { 6260 FormatStyle Style = getLLVMStyle(); 6261 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 6262 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 6263 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6264 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 6265 Style); 6266 } 6267 6268 TEST_F(FormatTest, AllowBinPackingInsideArguments) { 6269 FormatStyle Style = getLLVMStyleWithColumns(40); 6270 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 6271 Style.BinPackArguments = false; 6272 verifyFormat("void test() {\n" 6273 " someFunction(\n" 6274 " this + argument + is + quite\n" 6275 " + long + so + it + gets + wrapped\n" 6276 " + but + remains + bin - packed);\n" 6277 "}", 6278 Style); 6279 verifyFormat("void test() {\n" 6280 " someFunction(arg1,\n" 6281 " this + argument + is\n" 6282 " + quite + long + so\n" 6283 " + it + gets + wrapped\n" 6284 " + but + remains + bin\n" 6285 " - packed,\n" 6286 " arg3);\n" 6287 "}", 6288 Style); 6289 verifyFormat("void test() {\n" 6290 " someFunction(\n" 6291 " arg1,\n" 6292 " this + argument + has\n" 6293 " + anotherFunc(nested,\n" 6294 " calls + whose\n" 6295 " + arguments\n" 6296 " + are + also\n" 6297 " + wrapped,\n" 6298 " in + addition)\n" 6299 " + to + being + bin - packed,\n" 6300 " arg3);\n" 6301 "}", 6302 Style); 6303 6304 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 6305 verifyFormat("void test() {\n" 6306 " someFunction(\n" 6307 " arg1,\n" 6308 " this + argument + has +\n" 6309 " anotherFunc(nested,\n" 6310 " calls + whose +\n" 6311 " arguments +\n" 6312 " are + also +\n" 6313 " wrapped,\n" 6314 " in + addition) +\n" 6315 " to + being + bin - packed,\n" 6316 " arg3);\n" 6317 "}", 6318 Style); 6319 } 6320 6321 TEST_F(FormatTest, ConstructorInitializers) { 6322 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 6323 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 6324 getLLVMStyleWithColumns(45)); 6325 verifyFormat("Constructor()\n" 6326 " : Inttializer(FitsOnTheLine) {}", 6327 getLLVMStyleWithColumns(44)); 6328 verifyFormat("Constructor()\n" 6329 " : Inttializer(FitsOnTheLine) {}", 6330 getLLVMStyleWithColumns(43)); 6331 6332 verifyFormat("template <typename T>\n" 6333 "Constructor() : Initializer(FitsOnTheLine) {}", 6334 getLLVMStyleWithColumns(45)); 6335 6336 verifyFormat( 6337 "SomeClass::Constructor()\n" 6338 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 6339 6340 verifyFormat( 6341 "SomeClass::Constructor()\n" 6342 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6343 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 6344 verifyFormat( 6345 "SomeClass::Constructor()\n" 6346 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6347 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 6348 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6349 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6350 " : aaaaaaaaaa(aaaaaa) {}"); 6351 6352 verifyFormat("Constructor()\n" 6353 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6354 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6355 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6356 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 6357 6358 verifyFormat("Constructor()\n" 6359 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6360 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 6361 6362 verifyFormat("Constructor(int Parameter = 0)\n" 6363 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 6364 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 6365 verifyFormat("Constructor()\n" 6366 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 6367 "}", 6368 getLLVMStyleWithColumns(60)); 6369 verifyFormat("Constructor()\n" 6370 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6371 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 6372 6373 // Here a line could be saved by splitting the second initializer onto two 6374 // lines, but that is not desirable. 6375 verifyFormat("Constructor()\n" 6376 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 6377 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 6378 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 6379 6380 FormatStyle OnePerLine = getLLVMStyle(); 6381 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_Never; 6382 verifyFormat("MyClass::MyClass()\n" 6383 " : a(a),\n" 6384 " b(b),\n" 6385 " c(c) {}", 6386 OnePerLine); 6387 verifyFormat("MyClass::MyClass()\n" 6388 " : a(a), // comment\n" 6389 " b(b),\n" 6390 " c(c) {}", 6391 OnePerLine); 6392 verifyFormat("MyClass::MyClass(int a)\n" 6393 " : b(a), // comment\n" 6394 " c(a + 1) { // lined up\n" 6395 "}", 6396 OnePerLine); 6397 verifyFormat("Constructor()\n" 6398 " : a(b, b, b) {}", 6399 OnePerLine); 6400 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6401 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 6402 verifyFormat("SomeClass::Constructor()\n" 6403 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6404 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6405 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6406 OnePerLine); 6407 verifyFormat("SomeClass::Constructor()\n" 6408 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 6409 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6410 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6411 OnePerLine); 6412 verifyFormat("MyClass::MyClass(int var)\n" 6413 " : some_var_(var), // 4 space indent\n" 6414 " some_other_var_(var + 1) { // lined up\n" 6415 "}", 6416 OnePerLine); 6417 verifyFormat("Constructor()\n" 6418 " : aaaaa(aaaaaa),\n" 6419 " aaaaa(aaaaaa),\n" 6420 " aaaaa(aaaaaa),\n" 6421 " aaaaa(aaaaaa),\n" 6422 " aaaaa(aaaaaa) {}", 6423 OnePerLine); 6424 verifyFormat("Constructor()\n" 6425 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 6426 " aaaaaaaaaaaaaaaaaaaaaa) {}", 6427 OnePerLine); 6428 OnePerLine.BinPackParameters = false; 6429 verifyFormat( 6430 "Constructor()\n" 6431 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n" 6432 " aaaaaaaaaaa().aaa(),\n" 6433 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6434 OnePerLine); 6435 OnePerLine.ColumnLimit = 60; 6436 verifyFormat("Constructor()\n" 6437 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6438 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 6439 OnePerLine); 6440 6441 EXPECT_EQ("Constructor()\n" 6442 " : // Comment forcing unwanted break.\n" 6443 " aaaa(aaaa) {}", 6444 format("Constructor() :\n" 6445 " // Comment forcing unwanted break.\n" 6446 " aaaa(aaaa) {}")); 6447 } 6448 6449 TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) { 6450 FormatStyle Style = getLLVMStyleWithColumns(60); 6451 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 6452 Style.BinPackParameters = false; 6453 6454 for (int i = 0; i < 4; ++i) { 6455 // Test all combinations of parameters that should not have an effect. 6456 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1; 6457 Style.AllowAllArgumentsOnNextLine = i & 2; 6458 6459 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6460 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 6461 verifyFormat("Constructor()\n" 6462 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6463 Style); 6464 verifyFormat("Constructor() : a(a), b(b) {}", Style); 6465 6466 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6467 verifyFormat("Constructor()\n" 6468 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 6469 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 6470 Style); 6471 verifyFormat("Constructor() : a(a), b(b) {}", Style); 6472 6473 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 6474 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6475 verifyFormat("Constructor()\n" 6476 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6477 Style); 6478 6479 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6480 verifyFormat("Constructor()\n" 6481 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6482 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6483 Style); 6484 6485 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 6486 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6487 verifyFormat("Constructor() :\n" 6488 " aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6489 Style); 6490 6491 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6492 verifyFormat("Constructor() :\n" 6493 " aaaaaaaaaaaaaaaaaa(a),\n" 6494 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6495 Style); 6496 } 6497 6498 // Test interactions between AllowAllParametersOfDeclarationOnNextLine and 6499 // AllowAllConstructorInitializersOnNextLine in all 6500 // BreakConstructorInitializers modes 6501 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 6502 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6503 verifyFormat("SomeClassWithALongName::Constructor(\n" 6504 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n" 6505 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 6506 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 6507 Style); 6508 6509 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6510 verifyFormat("SomeClassWithALongName::Constructor(\n" 6511 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6512 " int bbbbbbbbbbbbb,\n" 6513 " int cccccccccccccccc)\n" 6514 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6515 Style); 6516 6517 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6518 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6519 verifyFormat("SomeClassWithALongName::Constructor(\n" 6520 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6521 " int bbbbbbbbbbbbb)\n" 6522 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 6523 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 6524 Style); 6525 6526 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 6527 6528 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6529 verifyFormat("SomeClassWithALongName::Constructor(\n" 6530 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n" 6531 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6532 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6533 Style); 6534 6535 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6536 verifyFormat("SomeClassWithALongName::Constructor(\n" 6537 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6538 " int bbbbbbbbbbbbb,\n" 6539 " int cccccccccccccccc)\n" 6540 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6541 Style); 6542 6543 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6544 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6545 verifyFormat("SomeClassWithALongName::Constructor(\n" 6546 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6547 " int bbbbbbbbbbbbb)\n" 6548 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6549 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6550 Style); 6551 6552 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 6553 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6554 verifyFormat("SomeClassWithALongName::Constructor(\n" 6555 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n" 6556 " aaaaaaaaaaaaaaaaaaaa(a),\n" 6557 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6558 Style); 6559 6560 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6561 verifyFormat("SomeClassWithALongName::Constructor(\n" 6562 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6563 " int bbbbbbbbbbbbb,\n" 6564 " int cccccccccccccccc) :\n" 6565 " aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6566 Style); 6567 6568 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6569 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6570 verifyFormat("SomeClassWithALongName::Constructor(\n" 6571 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6572 " int bbbbbbbbbbbbb) :\n" 6573 " aaaaaaaaaaaaaaaaaaaa(a),\n" 6574 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6575 Style); 6576 } 6577 6578 TEST_F(FormatTest, AllowAllArgumentsOnNextLine) { 6579 FormatStyle Style = getLLVMStyleWithColumns(60); 6580 Style.BinPackArguments = false; 6581 for (int i = 0; i < 4; ++i) { 6582 // Test all combinations of parameters that should not have an effect. 6583 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1; 6584 Style.PackConstructorInitializers = 6585 i & 2 ? FormatStyle::PCIS_BinPack : FormatStyle::PCIS_Never; 6586 6587 Style.AllowAllArgumentsOnNextLine = true; 6588 verifyFormat("void foo() {\n" 6589 " FunctionCallWithReallyLongName(\n" 6590 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n" 6591 "}", 6592 Style); 6593 Style.AllowAllArgumentsOnNextLine = false; 6594 verifyFormat("void foo() {\n" 6595 " FunctionCallWithReallyLongName(\n" 6596 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6597 " bbbbbbbbbbbb);\n" 6598 "}", 6599 Style); 6600 6601 Style.AllowAllArgumentsOnNextLine = true; 6602 verifyFormat("void foo() {\n" 6603 " auto VariableWithReallyLongName = {\n" 6604 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n" 6605 "}", 6606 Style); 6607 Style.AllowAllArgumentsOnNextLine = false; 6608 verifyFormat("void foo() {\n" 6609 " auto VariableWithReallyLongName = {\n" 6610 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6611 " bbbbbbbbbbbb};\n" 6612 "}", 6613 Style); 6614 } 6615 6616 // This parameter should not affect declarations. 6617 Style.BinPackParameters = false; 6618 Style.AllowAllArgumentsOnNextLine = false; 6619 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6620 verifyFormat("void FunctionCallWithReallyLongName(\n" 6621 " int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);", 6622 Style); 6623 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6624 verifyFormat("void FunctionCallWithReallyLongName(\n" 6625 " int aaaaaaaaaaaaaaaaaaaaaaa,\n" 6626 " int bbbbbbbbbbbb);", 6627 Style); 6628 } 6629 6630 TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) { 6631 // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign 6632 // and BAS_Align. 6633 FormatStyle Style = getLLVMStyleWithColumns(35); 6634 StringRef Input = "functionCall(paramA, paramB, paramC);\n" 6635 "void functionDecl(int A, int B, int C);"; 6636 Style.AllowAllArgumentsOnNextLine = false; 6637 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6638 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 6639 " paramC);\n" 6640 "void functionDecl(int A, int B,\n" 6641 " int C);"), 6642 format(Input, Style)); 6643 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 6644 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 6645 " paramC);\n" 6646 "void functionDecl(int A, int B,\n" 6647 " int C);"), 6648 format(Input, Style)); 6649 // However, BAS_AlwaysBreak should take precedence over 6650 // AllowAllArgumentsOnNextLine. 6651 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 6652 EXPECT_EQ(StringRef("functionCall(\n" 6653 " paramA, paramB, paramC);\n" 6654 "void functionDecl(\n" 6655 " int A, int B, int C);"), 6656 format(Input, Style)); 6657 6658 // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the 6659 // first argument. 6660 Style.AllowAllArgumentsOnNextLine = true; 6661 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 6662 EXPECT_EQ(StringRef("functionCall(\n" 6663 " paramA, paramB, paramC);\n" 6664 "void functionDecl(\n" 6665 " int A, int B, int C);"), 6666 format(Input, Style)); 6667 // It wouldn't fit on one line with aligned parameters so this setting 6668 // doesn't change anything for BAS_Align. 6669 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 6670 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 6671 " paramC);\n" 6672 "void functionDecl(int A, int B,\n" 6673 " int C);"), 6674 format(Input, Style)); 6675 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6676 EXPECT_EQ(StringRef("functionCall(\n" 6677 " paramA, paramB, paramC);\n" 6678 "void functionDecl(\n" 6679 " int A, int B, int C);"), 6680 format(Input, Style)); 6681 } 6682 6683 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) { 6684 FormatStyle Style = getLLVMStyle(); 6685 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 6686 6687 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 6688 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}", 6689 getStyleWithColumns(Style, 45)); 6690 verifyFormat("Constructor() :\n" 6691 " Initializer(FitsOnTheLine) {}", 6692 getStyleWithColumns(Style, 44)); 6693 verifyFormat("Constructor() :\n" 6694 " Initializer(FitsOnTheLine) {}", 6695 getStyleWithColumns(Style, 43)); 6696 6697 verifyFormat("template <typename T>\n" 6698 "Constructor() : Initializer(FitsOnTheLine) {}", 6699 getStyleWithColumns(Style, 50)); 6700 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6701 verifyFormat( 6702 "SomeClass::Constructor() :\n" 6703 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 6704 Style); 6705 6706 Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack; 6707 verifyFormat( 6708 "SomeClass::Constructor() :\n" 6709 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 6710 Style); 6711 6712 verifyFormat( 6713 "SomeClass::Constructor() :\n" 6714 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6715 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6716 Style); 6717 verifyFormat( 6718 "SomeClass::Constructor() :\n" 6719 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6720 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 6721 Style); 6722 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6723 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 6724 " aaaaaaaaaa(aaaaaa) {}", 6725 Style); 6726 6727 verifyFormat("Constructor() :\n" 6728 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6729 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6730 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6731 " aaaaaaaaaaaaaaaaaaaaaaa() {}", 6732 Style); 6733 6734 verifyFormat("Constructor() :\n" 6735 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6736 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6737 Style); 6738 6739 verifyFormat("Constructor(int Parameter = 0) :\n" 6740 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 6741 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}", 6742 Style); 6743 verifyFormat("Constructor() :\n" 6744 " aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 6745 "}", 6746 getStyleWithColumns(Style, 60)); 6747 verifyFormat("Constructor() :\n" 6748 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6749 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}", 6750 Style); 6751 6752 // Here a line could be saved by splitting the second initializer onto two 6753 // lines, but that is not desirable. 6754 verifyFormat("Constructor() :\n" 6755 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 6756 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 6757 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6758 Style); 6759 6760 FormatStyle OnePerLine = Style; 6761 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6762 verifyFormat("SomeClass::Constructor() :\n" 6763 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6764 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6765 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6766 OnePerLine); 6767 verifyFormat("SomeClass::Constructor() :\n" 6768 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 6769 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6770 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6771 OnePerLine); 6772 verifyFormat("MyClass::MyClass(int var) :\n" 6773 " some_var_(var), // 4 space indent\n" 6774 " some_other_var_(var + 1) { // lined up\n" 6775 "}", 6776 OnePerLine); 6777 verifyFormat("Constructor() :\n" 6778 " aaaaa(aaaaaa),\n" 6779 " aaaaa(aaaaaa),\n" 6780 " aaaaa(aaaaaa),\n" 6781 " aaaaa(aaaaaa),\n" 6782 " aaaaa(aaaaaa) {}", 6783 OnePerLine); 6784 verifyFormat("Constructor() :\n" 6785 " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 6786 " aaaaaaaaaaaaaaaaaaaaaa) {}", 6787 OnePerLine); 6788 OnePerLine.BinPackParameters = false; 6789 verifyFormat("Constructor() :\n" 6790 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 6791 " aaaaaaaaaaa().aaa(),\n" 6792 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6793 OnePerLine); 6794 OnePerLine.ColumnLimit = 60; 6795 verifyFormat("Constructor() :\n" 6796 " aaaaaaaaaaaaaaaaaaaa(a),\n" 6797 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 6798 OnePerLine); 6799 6800 EXPECT_EQ("Constructor() :\n" 6801 " // Comment forcing unwanted break.\n" 6802 " aaaa(aaaa) {}", 6803 format("Constructor() :\n" 6804 " // Comment forcing unwanted break.\n" 6805 " aaaa(aaaa) {}", 6806 Style)); 6807 6808 Style.ColumnLimit = 0; 6809 verifyFormat("SomeClass::Constructor() :\n" 6810 " a(a) {}", 6811 Style); 6812 verifyFormat("SomeClass::Constructor() noexcept :\n" 6813 " a(a) {}", 6814 Style); 6815 verifyFormat("SomeClass::Constructor() :\n" 6816 " a(a), b(b), c(c) {}", 6817 Style); 6818 verifyFormat("SomeClass::Constructor() :\n" 6819 " a(a) {\n" 6820 " foo();\n" 6821 " bar();\n" 6822 "}", 6823 Style); 6824 6825 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 6826 verifyFormat("SomeClass::Constructor() :\n" 6827 " a(a), b(b), c(c) {\n" 6828 "}", 6829 Style); 6830 verifyFormat("SomeClass::Constructor() :\n" 6831 " a(a) {\n" 6832 "}", 6833 Style); 6834 6835 Style.ColumnLimit = 80; 6836 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 6837 Style.ConstructorInitializerIndentWidth = 2; 6838 verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style); 6839 verifyFormat("SomeClass::Constructor() :\n" 6840 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6841 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}", 6842 Style); 6843 6844 // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as 6845 // well 6846 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 6847 verifyFormat( 6848 "class SomeClass\n" 6849 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6850 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 6851 Style); 6852 Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 6853 verifyFormat( 6854 "class SomeClass\n" 6855 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6856 " , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 6857 Style); 6858 Style.BreakInheritanceList = FormatStyle::BILS_AfterColon; 6859 verifyFormat( 6860 "class SomeClass :\n" 6861 " public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6862 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 6863 Style); 6864 Style.BreakInheritanceList = FormatStyle::BILS_AfterComma; 6865 verifyFormat( 6866 "class SomeClass\n" 6867 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6868 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 6869 Style); 6870 } 6871 6872 #ifndef EXPENSIVE_CHECKS 6873 // Expensive checks enables libstdc++ checking which includes validating the 6874 // state of ranges used in std::priority_queue - this blows out the 6875 // runtime/scalability of the function and makes this test unacceptably slow. 6876 TEST_F(FormatTest, MemoizationTests) { 6877 // This breaks if the memoization lookup does not take \c Indent and 6878 // \c LastSpace into account. 6879 verifyFormat( 6880 "extern CFRunLoopTimerRef\n" 6881 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 6882 " CFTimeInterval interval, CFOptionFlags flags,\n" 6883 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 6884 " CFRunLoopTimerContext *context) {}"); 6885 6886 // Deep nesting somewhat works around our memoization. 6887 verifyFormat( 6888 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 6889 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 6890 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 6891 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 6892 " aaaaa())))))))))))))))))))))))))))))))))))))));", 6893 getLLVMStyleWithColumns(65)); 6894 verifyFormat( 6895 "aaaaa(\n" 6896 " aaaaa,\n" 6897 " aaaaa(\n" 6898 " aaaaa,\n" 6899 " aaaaa(\n" 6900 " aaaaa,\n" 6901 " aaaaa(\n" 6902 " aaaaa,\n" 6903 " aaaaa(\n" 6904 " aaaaa,\n" 6905 " aaaaa(\n" 6906 " aaaaa,\n" 6907 " aaaaa(\n" 6908 " aaaaa,\n" 6909 " aaaaa(\n" 6910 " aaaaa,\n" 6911 " aaaaa(\n" 6912 " aaaaa,\n" 6913 " aaaaa(\n" 6914 " aaaaa,\n" 6915 " aaaaa(\n" 6916 " aaaaa,\n" 6917 " aaaaa(\n" 6918 " aaaaa,\n" 6919 " aaaaa))))))))))));", 6920 getLLVMStyleWithColumns(65)); 6921 verifyFormat( 6922 "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" 6923 " a),\n" 6924 " a),\n" 6925 " a),\n" 6926 " a),\n" 6927 " a),\n" 6928 " a),\n" 6929 " a),\n" 6930 " a),\n" 6931 " a),\n" 6932 " a),\n" 6933 " a),\n" 6934 " a),\n" 6935 " a),\n" 6936 " a),\n" 6937 " a),\n" 6938 " a),\n" 6939 " a)", 6940 getLLVMStyleWithColumns(65)); 6941 6942 // This test takes VERY long when memoization is broken. 6943 FormatStyle OnePerLine = getLLVMStyle(); 6944 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6945 OnePerLine.BinPackParameters = false; 6946 std::string input = "Constructor()\n" 6947 " : aaaa(a,\n"; 6948 for (unsigned i = 0, e = 80; i != e; ++i) { 6949 input += " a,\n"; 6950 } 6951 input += " a) {}"; 6952 verifyFormat(input, OnePerLine); 6953 } 6954 #endif 6955 6956 TEST_F(FormatTest, BreaksAsHighAsPossible) { 6957 verifyFormat( 6958 "void f() {\n" 6959 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 6960 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 6961 " f();\n" 6962 "}"); 6963 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 6964 " Intervals[i - 1].getRange().getLast()) {\n}"); 6965 } 6966 6967 TEST_F(FormatTest, BreaksFunctionDeclarations) { 6968 // Principially, we break function declarations in a certain order: 6969 // 1) break amongst arguments. 6970 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 6971 " Cccccccccccccc cccccccccccccc);"); 6972 verifyFormat("template <class TemplateIt>\n" 6973 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 6974 " TemplateIt *stop) {}"); 6975 6976 // 2) break after return type. 6977 verifyFormat( 6978 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6979 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", 6980 getGoogleStyle()); 6981 6982 // 3) break after (. 6983 verifyFormat( 6984 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 6985 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", 6986 getGoogleStyle()); 6987 6988 // 4) break before after nested name specifiers. 6989 verifyFormat( 6990 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6991 "SomeClasssssssssssssssssssssssssssssssssssssss::\n" 6992 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", 6993 getGoogleStyle()); 6994 6995 // However, there are exceptions, if a sufficient amount of lines can be 6996 // saved. 6997 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 6998 // more adjusting. 6999 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 7000 " Cccccccccccccc cccccccccc,\n" 7001 " Cccccccccccccc cccccccccc,\n" 7002 " Cccccccccccccc cccccccccc,\n" 7003 " Cccccccccccccc cccccccccc);"); 7004 verifyFormat( 7005 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7006 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 7007 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 7008 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", 7009 getGoogleStyle()); 7010 verifyFormat( 7011 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 7012 " Cccccccccccccc cccccccccc,\n" 7013 " Cccccccccccccc cccccccccc,\n" 7014 " Cccccccccccccc cccccccccc,\n" 7015 " Cccccccccccccc cccccccccc,\n" 7016 " Cccccccccccccc cccccccccc,\n" 7017 " Cccccccccccccc cccccccccc);"); 7018 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 7019 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 7020 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 7021 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 7022 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 7023 7024 // Break after multi-line parameters. 7025 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7026 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7027 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7028 " bbbb bbbb);"); 7029 verifyFormat("void SomeLoooooooooooongFunction(\n" 7030 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 7031 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7032 " int bbbbbbbbbbbbb);"); 7033 7034 // Treat overloaded operators like other functions. 7035 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 7036 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 7037 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 7038 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 7039 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 7040 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 7041 verifyGoogleFormat( 7042 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 7043 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 7044 verifyGoogleFormat( 7045 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 7046 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 7047 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7048 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 7049 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 7050 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 7051 verifyGoogleFormat( 7052 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" 7053 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7054 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); 7055 verifyGoogleFormat("template <typename T>\n" 7056 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7057 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n" 7058 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);"); 7059 7060 FormatStyle Style = getLLVMStyle(); 7061 Style.PointerAlignment = FormatStyle::PAS_Left; 7062 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7063 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", 7064 Style); 7065 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 7066 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 7067 Style); 7068 } 7069 7070 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) { 7071 // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516: 7072 // Prefer keeping `::` followed by `operator` together. 7073 EXPECT_EQ("const aaaa::bbbbbbb &\n" 7074 "ccccccccc::operator++() {\n" 7075 " stuff();\n" 7076 "}", 7077 format("const aaaa::bbbbbbb\n" 7078 "&ccccccccc::operator++() { stuff(); }", 7079 getLLVMStyleWithColumns(40))); 7080 } 7081 7082 TEST_F(FormatTest, TrailingReturnType) { 7083 verifyFormat("auto foo() -> int;\n"); 7084 // correct trailing return type spacing 7085 verifyFormat("auto operator->() -> int;\n"); 7086 verifyFormat("auto operator++(int) -> int;\n"); 7087 7088 verifyFormat("struct S {\n" 7089 " auto bar() const -> int;\n" 7090 "};"); 7091 verifyFormat("template <size_t Order, typename T>\n" 7092 "auto load_img(const std::string &filename)\n" 7093 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 7094 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 7095 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 7096 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 7097 verifyFormat("template <typename T>\n" 7098 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" 7099 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); 7100 7101 // Not trailing return types. 7102 verifyFormat("void f() { auto a = b->c(); }"); 7103 verifyFormat("auto a = p->foo();"); 7104 verifyFormat("int a = p->foo();"); 7105 verifyFormat("auto lmbd = [] NOEXCEPT -> int { return 0; };"); 7106 } 7107 7108 TEST_F(FormatTest, DeductionGuides) { 7109 verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;"); 7110 verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;"); 7111 verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;"); 7112 verifyFormat( 7113 "template <class... T>\n" 7114 "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;"); 7115 verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;"); 7116 verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;"); 7117 verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;"); 7118 verifyFormat("template <class T> A() -> A<(3 < 2)>;"); 7119 verifyFormat("template <class T> A() -> A<((3) < (2))>;"); 7120 verifyFormat("template <class T> x() -> x<1>;"); 7121 verifyFormat("template <class T> explicit x(T &) -> x<1>;"); 7122 7123 // Ensure not deduction guides. 7124 verifyFormat("c()->f<int>();"); 7125 verifyFormat("x()->foo<1>;"); 7126 verifyFormat("x = p->foo<3>();"); 7127 verifyFormat("x()->x<1>();"); 7128 verifyFormat("x()->x<1>;"); 7129 } 7130 7131 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 7132 // Avoid breaking before trailing 'const' or other trailing annotations, if 7133 // they are not function-like. 7134 FormatStyle Style = getGoogleStyleWithColumns(47); 7135 verifyFormat("void someLongFunction(\n" 7136 " int someLoooooooooooooongParameter) const {\n}", 7137 getLLVMStyleWithColumns(47)); 7138 verifyFormat("LoooooongReturnType\n" 7139 "someLoooooooongFunction() const {}", 7140 getLLVMStyleWithColumns(47)); 7141 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 7142 " const {}", 7143 Style); 7144 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 7145 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 7146 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 7147 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 7148 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 7149 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 7150 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 7151 " aaaaaaaaaaa aaaaa) const override;"); 7152 verifyGoogleFormat( 7153 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7154 " const override;"); 7155 7156 // Even if the first parameter has to be wrapped. 7157 verifyFormat("void someLongFunction(\n" 7158 " int someLongParameter) const {}", 7159 getLLVMStyleWithColumns(46)); 7160 verifyFormat("void someLongFunction(\n" 7161 " int someLongParameter) const {}", 7162 Style); 7163 verifyFormat("void someLongFunction(\n" 7164 " int someLongParameter) override {}", 7165 Style); 7166 verifyFormat("void someLongFunction(\n" 7167 " int someLongParameter) OVERRIDE {}", 7168 Style); 7169 verifyFormat("void someLongFunction(\n" 7170 " int someLongParameter) final {}", 7171 Style); 7172 verifyFormat("void someLongFunction(\n" 7173 " int someLongParameter) FINAL {}", 7174 Style); 7175 verifyFormat("void someLongFunction(\n" 7176 " int parameter) const override {}", 7177 Style); 7178 7179 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 7180 verifyFormat("void someLongFunction(\n" 7181 " int someLongParameter) const\n" 7182 "{\n" 7183 "}", 7184 Style); 7185 7186 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 7187 verifyFormat("void someLongFunction(\n" 7188 " int someLongParameter) const\n" 7189 " {\n" 7190 " }", 7191 Style); 7192 7193 // Unless these are unknown annotations. 7194 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 7195 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7196 " LONG_AND_UGLY_ANNOTATION;"); 7197 7198 // Breaking before function-like trailing annotations is fine to keep them 7199 // close to their arguments. 7200 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7201 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 7202 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 7203 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 7204 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 7205 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 7206 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 7207 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 7208 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 7209 7210 verifyFormat( 7211 "void aaaaaaaaaaaaaaaaaa()\n" 7212 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 7213 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 7214 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7215 " __attribute__((unused));"); 7216 verifyGoogleFormat( 7217 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7218 " GUARDED_BY(aaaaaaaaaaaa);"); 7219 verifyGoogleFormat( 7220 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7221 " GUARDED_BY(aaaaaaaaaaaa);"); 7222 verifyGoogleFormat( 7223 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 7224 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7225 verifyGoogleFormat( 7226 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 7227 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7228 } 7229 7230 TEST_F(FormatTest, FunctionAnnotations) { 7231 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 7232 "int OldFunction(const string ¶meter) {}"); 7233 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 7234 "string OldFunction(const string ¶meter) {}"); 7235 verifyFormat("template <typename T>\n" 7236 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 7237 "string OldFunction(const string ¶meter) {}"); 7238 7239 // Not function annotations. 7240 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7241 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 7242 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" 7243 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); 7244 verifyFormat("MACRO(abc).function() // wrap\n" 7245 " << abc;"); 7246 verifyFormat("MACRO(abc)->function() // wrap\n" 7247 " << abc;"); 7248 verifyFormat("MACRO(abc)::function() // wrap\n" 7249 " << abc;"); 7250 } 7251 7252 TEST_F(FormatTest, BreaksDesireably) { 7253 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 7254 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 7255 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 7256 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7257 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 7258 "}"); 7259 7260 verifyFormat( 7261 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7262 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 7263 7264 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7265 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7266 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 7267 7268 verifyFormat( 7269 "aaaaaaaa(aaaaaaaaaaaaa,\n" 7270 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7271 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 7272 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7273 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 7274 7275 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7276 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7277 7278 verifyFormat( 7279 "void f() {\n" 7280 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 7281 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 7282 "}"); 7283 verifyFormat( 7284 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7285 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 7286 verifyFormat( 7287 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7288 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 7289 verifyFormat( 7290 "aaaaaa(aaa,\n" 7291 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7292 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7293 " aaaa);"); 7294 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7295 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7296 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7297 7298 // Indent consistently independent of call expression and unary operator. 7299 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 7300 " dddddddddddddddddddddddddddddd));"); 7301 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 7302 " dddddddddddddddddddddddddddddd));"); 7303 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 7304 " dddddddddddddddddddddddddddddd));"); 7305 7306 // This test case breaks on an incorrect memoization, i.e. an optimization not 7307 // taking into account the StopAt value. 7308 verifyFormat( 7309 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 7310 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 7311 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 7312 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7313 7314 verifyFormat("{\n {\n {\n" 7315 " Annotation.SpaceRequiredBefore =\n" 7316 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 7317 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 7318 " }\n }\n}"); 7319 7320 // Break on an outer level if there was a break on an inner level. 7321 EXPECT_EQ("f(g(h(a, // comment\n" 7322 " b, c),\n" 7323 " d, e),\n" 7324 " x, y);", 7325 format("f(g(h(a, // comment\n" 7326 " b, c), d, e), x, y);")); 7327 7328 // Prefer breaking similar line breaks. 7329 verifyFormat( 7330 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 7331 " NSTrackingMouseEnteredAndExited |\n" 7332 " NSTrackingActiveAlways;"); 7333 } 7334 7335 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 7336 FormatStyle NoBinPacking = getGoogleStyle(); 7337 NoBinPacking.BinPackParameters = false; 7338 NoBinPacking.BinPackArguments = true; 7339 verifyFormat("void f() {\n" 7340 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 7341 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 7342 "}", 7343 NoBinPacking); 7344 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 7345 " int aaaaaaaaaaaaaaaaaaaa,\n" 7346 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 7347 NoBinPacking); 7348 7349 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 7350 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7351 " vector<int> bbbbbbbbbbbbbbb);", 7352 NoBinPacking); 7353 // FIXME: This behavior difference is probably not wanted. However, currently 7354 // we cannot distinguish BreakBeforeParameter being set because of the wrapped 7355 // template arguments from BreakBeforeParameter being set because of the 7356 // one-per-line formatting. 7357 verifyFormat( 7358 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n" 7359 " aaaaaaaaaa> aaaaaaaaaa);", 7360 NoBinPacking); 7361 verifyFormat( 7362 "void fffffffffff(\n" 7363 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n" 7364 " aaaaaaaaaa);"); 7365 } 7366 7367 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 7368 FormatStyle NoBinPacking = getGoogleStyle(); 7369 NoBinPacking.BinPackParameters = false; 7370 NoBinPacking.BinPackArguments = false; 7371 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 7372 " aaaaaaaaaaaaaaaaaaaa,\n" 7373 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 7374 NoBinPacking); 7375 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 7376 " aaaaaaaaaaaaa,\n" 7377 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 7378 NoBinPacking); 7379 verifyFormat( 7380 "aaaaaaaa(aaaaaaaaaaaaa,\n" 7381 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7382 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 7383 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7384 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 7385 NoBinPacking); 7386 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 7387 " .aaaaaaaaaaaaaaaaaa();", 7388 NoBinPacking); 7389 verifyFormat("void f() {\n" 7390 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7391 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 7392 "}", 7393 NoBinPacking); 7394 7395 verifyFormat( 7396 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7397 " aaaaaaaaaaaa,\n" 7398 " aaaaaaaaaaaa);", 7399 NoBinPacking); 7400 verifyFormat( 7401 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 7402 " ddddddddddddddddddddddddddddd),\n" 7403 " test);", 7404 NoBinPacking); 7405 7406 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 7407 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 7408 " aaaaaaaaaaaaaaaaaaaaaaa>\n" 7409 " aaaaaaaaaaaaaaaaaa;", 7410 NoBinPacking); 7411 verifyFormat("a(\"a\"\n" 7412 " \"a\",\n" 7413 " a);"); 7414 7415 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 7416 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 7417 " aaaaaaaaa,\n" 7418 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7419 NoBinPacking); 7420 verifyFormat( 7421 "void f() {\n" 7422 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 7423 " .aaaaaaa();\n" 7424 "}", 7425 NoBinPacking); 7426 verifyFormat( 7427 "template <class SomeType, class SomeOtherType>\n" 7428 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 7429 NoBinPacking); 7430 } 7431 7432 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 7433 FormatStyle Style = getLLVMStyleWithColumns(15); 7434 Style.ExperimentalAutoDetectBinPacking = true; 7435 EXPECT_EQ("aaa(aaaa,\n" 7436 " aaaa,\n" 7437 " aaaa);\n" 7438 "aaa(aaaa,\n" 7439 " aaaa,\n" 7440 " aaaa);", 7441 format("aaa(aaaa,\n" // one-per-line 7442 " aaaa,\n" 7443 " aaaa );\n" 7444 "aaa(aaaa, aaaa, aaaa);", // inconclusive 7445 Style)); 7446 EXPECT_EQ("aaa(aaaa, aaaa,\n" 7447 " aaaa);\n" 7448 "aaa(aaaa, aaaa,\n" 7449 " aaaa);", 7450 format("aaa(aaaa, aaaa,\n" // bin-packed 7451 " aaaa );\n" 7452 "aaa(aaaa, aaaa, aaaa);", // inconclusive 7453 Style)); 7454 } 7455 7456 TEST_F(FormatTest, FormatsBuilderPattern) { 7457 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 7458 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 7459 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 7460 " .StartsWith(\".init\", ORDER_INIT)\n" 7461 " .StartsWith(\".fini\", ORDER_FINI)\n" 7462 " .StartsWith(\".hash\", ORDER_HASH)\n" 7463 " .Default(ORDER_TEXT);\n"); 7464 7465 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 7466 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 7467 verifyFormat("aaaaaaa->aaaaaaa\n" 7468 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7469 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7470 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 7471 verifyFormat( 7472 "aaaaaaa->aaaaaaa\n" 7473 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7474 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 7475 verifyFormat( 7476 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 7477 " aaaaaaaaaaaaaa);"); 7478 verifyFormat( 7479 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 7480 " aaaaaa->aaaaaaaaaaaa()\n" 7481 " ->aaaaaaaaaaaaaaaa(\n" 7482 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7483 " ->aaaaaaaaaaaaaaaaa();"); 7484 verifyGoogleFormat( 7485 "void f() {\n" 7486 " someo->Add((new util::filetools::Handler(dir))\n" 7487 " ->OnEvent1(NewPermanentCallback(\n" 7488 " this, &HandlerHolderClass::EventHandlerCBA))\n" 7489 " ->OnEvent2(NewPermanentCallback(\n" 7490 " this, &HandlerHolderClass::EventHandlerCBB))\n" 7491 " ->OnEvent3(NewPermanentCallback(\n" 7492 " this, &HandlerHolderClass::EventHandlerCBC))\n" 7493 " ->OnEvent5(NewPermanentCallback(\n" 7494 " this, &HandlerHolderClass::EventHandlerCBD))\n" 7495 " ->OnEvent6(NewPermanentCallback(\n" 7496 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 7497 "}"); 7498 7499 verifyFormat( 7500 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 7501 verifyFormat("aaaaaaaaaaaaaaa()\n" 7502 " .aaaaaaaaaaaaaaa()\n" 7503 " .aaaaaaaaaaaaaaa()\n" 7504 " .aaaaaaaaaaaaaaa()\n" 7505 " .aaaaaaaaaaaaaaa();"); 7506 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 7507 " .aaaaaaaaaaaaaaa()\n" 7508 " .aaaaaaaaaaaaaaa()\n" 7509 " .aaaaaaaaaaaaaaa();"); 7510 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 7511 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 7512 " .aaaaaaaaaaaaaaa();"); 7513 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 7514 " ->aaaaaaaaaaaaaae(0)\n" 7515 " ->aaaaaaaaaaaaaaa();"); 7516 7517 // Don't linewrap after very short segments. 7518 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7519 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7520 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7521 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7522 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7523 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7524 verifyFormat("aaa()\n" 7525 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7526 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7527 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7528 7529 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 7530 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7531 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 7532 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 7533 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 7534 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 7535 7536 // Prefer not to break after empty parentheses. 7537 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 7538 " First->LastNewlineOffset);"); 7539 7540 // Prefer not to create "hanging" indents. 7541 verifyFormat( 7542 "return !soooooooooooooome_map\n" 7543 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7544 " .second;"); 7545 verifyFormat( 7546 "return aaaaaaaaaaaaaaaa\n" 7547 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n" 7548 " .aaaa(aaaaaaaaaaaaaa);"); 7549 // No hanging indent here. 7550 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n" 7551 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7552 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n" 7553 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7554 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 7555 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7556 getLLVMStyleWithColumns(60)); 7557 verifyFormat("aaaaaaaaaaaaaaaaaa\n" 7558 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 7559 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7560 getLLVMStyleWithColumns(59)); 7561 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7562 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7563 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7564 7565 // Dont break if only closing statements before member call 7566 verifyFormat("test() {\n" 7567 " ([]() -> {\n" 7568 " int b = 32;\n" 7569 " return 3;\n" 7570 " }).foo();\n" 7571 "}"); 7572 verifyFormat("test() {\n" 7573 " (\n" 7574 " []() -> {\n" 7575 " int b = 32;\n" 7576 " return 3;\n" 7577 " },\n" 7578 " foo, bar)\n" 7579 " .foo();\n" 7580 "}"); 7581 verifyFormat("test() {\n" 7582 " ([]() -> {\n" 7583 " int b = 32;\n" 7584 " return 3;\n" 7585 " })\n" 7586 " .foo()\n" 7587 " .bar();\n" 7588 "}"); 7589 verifyFormat("test() {\n" 7590 " ([]() -> {\n" 7591 " int b = 32;\n" 7592 " return 3;\n" 7593 " })\n" 7594 " .foo(\"aaaaaaaaaaaaaaaaa\"\n" 7595 " \"bbbb\");\n" 7596 "}", 7597 getLLVMStyleWithColumns(30)); 7598 } 7599 7600 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 7601 verifyFormat( 7602 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7603 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 7604 verifyFormat( 7605 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 7606 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 7607 7608 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 7609 " ccccccccccccccccccccccccc) {\n}"); 7610 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 7611 " ccccccccccccccccccccccccc) {\n}"); 7612 7613 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 7614 " ccccccccccccccccccccccccc) {\n}"); 7615 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 7616 " ccccccccccccccccccccccccc) {\n}"); 7617 7618 verifyFormat( 7619 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 7620 " ccccccccccccccccccccccccc) {\n}"); 7621 verifyFormat( 7622 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 7623 " ccccccccccccccccccccccccc) {\n}"); 7624 7625 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 7626 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 7627 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 7628 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 7629 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 7630 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 7631 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 7632 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 7633 7634 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 7635 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 7636 " aaaaaaaaaaaaaaa != aa) {\n}"); 7637 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 7638 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 7639 " aaaaaaaaaaaaaaa != aa) {\n}"); 7640 } 7641 7642 TEST_F(FormatTest, BreaksAfterAssignments) { 7643 verifyFormat( 7644 "unsigned Cost =\n" 7645 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 7646 " SI->getPointerAddressSpaceee());\n"); 7647 verifyFormat( 7648 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 7649 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 7650 7651 verifyFormat( 7652 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 7653 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 7654 verifyFormat("unsigned OriginalStartColumn =\n" 7655 " SourceMgr.getSpellingColumnNumber(\n" 7656 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 7657 " 1;"); 7658 } 7659 7660 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) { 7661 FormatStyle Style = getLLVMStyle(); 7662 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 7663 " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;", 7664 Style); 7665 7666 Style.PenaltyBreakAssignment = 20; 7667 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 7668 " cccccccccccccccccccccccccc;", 7669 Style); 7670 } 7671 7672 TEST_F(FormatTest, AlignsAfterAssignments) { 7673 verifyFormat( 7674 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7675 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7676 verifyFormat( 7677 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7678 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7679 verifyFormat( 7680 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7681 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7682 verifyFormat( 7683 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7684 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 7685 verifyFormat( 7686 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 7687 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 7688 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 7689 } 7690 7691 TEST_F(FormatTest, AlignsAfterReturn) { 7692 verifyFormat( 7693 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7694 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7695 verifyFormat( 7696 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7697 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 7698 verifyFormat( 7699 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 7700 " aaaaaaaaaaaaaaaaaaaaaa();"); 7701 verifyFormat( 7702 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 7703 " aaaaaaaaaaaaaaaaaaaaaa());"); 7704 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7705 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7706 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7707 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 7708 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7709 verifyFormat("return\n" 7710 " // true if code is one of a or b.\n" 7711 " code == a || code == b;"); 7712 } 7713 7714 TEST_F(FormatTest, AlignsAfterOpenBracket) { 7715 verifyFormat( 7716 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 7717 " aaaaaaaaa aaaaaaa) {}"); 7718 verifyFormat( 7719 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 7720 " aaaaaaaaaaa aaaaaaaaa);"); 7721 verifyFormat( 7722 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 7723 " aaaaaaaaaaaaaaaaaaaaa));"); 7724 FormatStyle Style = getLLVMStyle(); 7725 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7726 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7727 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 7728 Style); 7729 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 7730 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 7731 Style); 7732 verifyFormat("SomeLongVariableName->someFunction(\n" 7733 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 7734 Style); 7735 verifyFormat( 7736 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 7737 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 7738 Style); 7739 verifyFormat( 7740 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 7741 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7742 Style); 7743 verifyFormat( 7744 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 7745 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 7746 Style); 7747 7748 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n" 7749 " ccccccc(aaaaaaaaaaaaaaaaa, //\n" 7750 " b));", 7751 Style); 7752 7753 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 7754 Style.BinPackArguments = false; 7755 Style.BinPackParameters = false; 7756 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7757 " aaaaaaaaaaa aaaaaaaa,\n" 7758 " aaaaaaaaa aaaaaaa,\n" 7759 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 7760 Style); 7761 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 7762 " aaaaaaaaaaa aaaaaaaaa,\n" 7763 " aaaaaaaaaaa aaaaaaaaa,\n" 7764 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7765 Style); 7766 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" 7767 " aaaaaaaaaaaaaaa,\n" 7768 " aaaaaaaaaaaaaaaaaaaaa,\n" 7769 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 7770 Style); 7771 verifyFormat( 7772 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" 7773 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 7774 Style); 7775 verifyFormat( 7776 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" 7777 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 7778 Style); 7779 verifyFormat( 7780 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 7781 " aaaaaaaaaaaaaaaaaaaaa(\n" 7782 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n" 7783 " aaaaaaaaaaaaaaaa);", 7784 Style); 7785 verifyFormat( 7786 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 7787 " aaaaaaaaaaaaaaaaaaaaa(\n" 7788 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n" 7789 " aaaaaaaaaaaaaaaa);", 7790 Style); 7791 } 7792 7793 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 7794 FormatStyle Style = getLLVMStyleWithColumns(40); 7795 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7796 " bbbbbbbbbbbbbbbbbbbbbb);", 7797 Style); 7798 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 7799 Style.AlignOperands = FormatStyle::OAS_DontAlign; 7800 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7801 " bbbbbbbbbbbbbbbbbbbbbb);", 7802 Style); 7803 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7804 Style.AlignOperands = FormatStyle::OAS_Align; 7805 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7806 " bbbbbbbbbbbbbbbbbbbbbb);", 7807 Style); 7808 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7809 Style.AlignOperands = FormatStyle::OAS_DontAlign; 7810 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7811 " bbbbbbbbbbbbbbbbbbbbbb);", 7812 Style); 7813 } 7814 7815 TEST_F(FormatTest, BreaksConditionalExpressions) { 7816 verifyFormat( 7817 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7818 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7819 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7820 verifyFormat( 7821 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 7822 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7823 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7824 verifyFormat( 7825 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7826 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7827 verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n" 7828 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7829 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7830 verifyFormat( 7831 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 7832 " : aaaaaaaaaaaaa);"); 7833 verifyFormat( 7834 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7835 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7836 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7837 " aaaaaaaaaaaaa);"); 7838 verifyFormat( 7839 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7840 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7841 " aaaaaaaaaaaaa);"); 7842 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7843 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7844 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7845 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7846 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7847 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7848 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7849 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7850 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7851 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7852 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7853 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7854 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7855 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7856 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7857 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7858 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7859 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7860 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7861 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7862 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 7863 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7864 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7865 " : aaaaaaaaaaaaaaaa;"); 7866 verifyFormat( 7867 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7868 " ? aaaaaaaaaaaaaaa\n" 7869 " : aaaaaaaaaaaaaaa;"); 7870 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 7871 " aaaaaaaaa\n" 7872 " ? b\n" 7873 " : c);"); 7874 verifyFormat("return aaaa == bbbb\n" 7875 " // comment\n" 7876 " ? aaaa\n" 7877 " : bbbb;"); 7878 verifyFormat("unsigned Indent =\n" 7879 " format(TheLine.First,\n" 7880 " IndentForLevel[TheLine.Level] >= 0\n" 7881 " ? IndentForLevel[TheLine.Level]\n" 7882 " : TheLine * 2,\n" 7883 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 7884 getLLVMStyleWithColumns(60)); 7885 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 7886 " ? aaaaaaaaaaaaaaa\n" 7887 " : bbbbbbbbbbbbbbb //\n" 7888 " ? ccccccccccccccc\n" 7889 " : ddddddddddddddd;"); 7890 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 7891 " ? aaaaaaaaaaaaaaa\n" 7892 " : (bbbbbbbbbbbbbbb //\n" 7893 " ? ccccccccccccccc\n" 7894 " : ddddddddddddddd);"); 7895 verifyFormat( 7896 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7897 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7898 " aaaaaaaaaaaaaaaaaaaaa +\n" 7899 " aaaaaaaaaaaaaaaaaaaaa\n" 7900 " : aaaaaaaaaa;"); 7901 verifyFormat( 7902 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7903 " : aaaaaaaaaaaaaaaaaaaaaa\n" 7904 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7905 7906 FormatStyle NoBinPacking = getLLVMStyle(); 7907 NoBinPacking.BinPackArguments = false; 7908 verifyFormat( 7909 "void f() {\n" 7910 " g(aaa,\n" 7911 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 7912 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7913 " ? aaaaaaaaaaaaaaa\n" 7914 " : aaaaaaaaaaaaaaa);\n" 7915 "}", 7916 NoBinPacking); 7917 verifyFormat( 7918 "void f() {\n" 7919 " g(aaa,\n" 7920 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 7921 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7922 " ?: aaaaaaaaaaaaaaa);\n" 7923 "}", 7924 NoBinPacking); 7925 7926 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 7927 " // comment.\n" 7928 " ccccccccccccccccccccccccccccccccccccccc\n" 7929 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7930 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 7931 7932 // Assignments in conditional expressions. Apparently not uncommon :-(. 7933 verifyFormat("return a != b\n" 7934 " // comment\n" 7935 " ? a = b\n" 7936 " : a = b;"); 7937 verifyFormat("return a != b\n" 7938 " // comment\n" 7939 " ? a = a != b\n" 7940 " // comment\n" 7941 " ? a = b\n" 7942 " : a\n" 7943 " : a;\n"); 7944 verifyFormat("return a != b\n" 7945 " // comment\n" 7946 " ? a\n" 7947 " : a = a != b\n" 7948 " // comment\n" 7949 " ? a = b\n" 7950 " : a;"); 7951 7952 // Chained conditionals 7953 FormatStyle Style = getLLVMStyleWithColumns(70); 7954 Style.AlignOperands = FormatStyle::OAS_Align; 7955 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7956 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7957 " : 3333333333333333;", 7958 Style); 7959 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7960 " : bbbbbbbbbb ? 2222222222222222\n" 7961 " : 3333333333333333;", 7962 Style); 7963 verifyFormat("return aaaaaaaaaa ? 1111111111111111\n" 7964 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 7965 " : 3333333333333333;", 7966 Style); 7967 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7968 " : bbbbbbbbbbbbbb ? 222222\n" 7969 " : 333333;", 7970 Style); 7971 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7972 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7973 " : cccccccccccccc ? 3333333333333333\n" 7974 " : 4444444444444444;", 7975 Style); 7976 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc)\n" 7977 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7978 " : 3333333333333333;", 7979 Style); 7980 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7981 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7982 " : (aaa ? bbb : ccc);", 7983 Style); 7984 verifyFormat( 7985 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7986 " : cccccccccccccccccc)\n" 7987 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7988 " : 3333333333333333;", 7989 Style); 7990 verifyFormat( 7991 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7992 " : cccccccccccccccccc)\n" 7993 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7994 " : 3333333333333333;", 7995 Style); 7996 verifyFormat( 7997 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7998 " : dddddddddddddddddd)\n" 7999 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8000 " : 3333333333333333;", 8001 Style); 8002 verifyFormat( 8003 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8004 " : dddddddddddddddddd)\n" 8005 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8006 " : 3333333333333333;", 8007 Style); 8008 verifyFormat( 8009 "return aaaaaaaaa ? 1111111111111111\n" 8010 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8011 " : a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8012 " : dddddddddddddddddd)\n", 8013 Style); 8014 verifyFormat( 8015 "return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 8016 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8017 " : (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8018 " : cccccccccccccccccc);", 8019 Style); 8020 verifyFormat( 8021 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8022 " : ccccccccccccccc ? dddddddddddddddddd\n" 8023 " : eeeeeeeeeeeeeeeeee)\n" 8024 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8025 " : 3333333333333333;", 8026 Style); 8027 verifyFormat( 8028 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8029 " : ccccccccccccccc ? dddddddddddddddddd\n" 8030 " : eeeeeeeeeeeeeeeeee)\n" 8031 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8032 " : 3333333333333333;", 8033 Style); 8034 verifyFormat( 8035 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8036 " : cccccccccccc ? dddddddddddddddddd\n" 8037 " : eeeeeeeeeeeeeeeeee)\n" 8038 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8039 " : 3333333333333333;", 8040 Style); 8041 verifyFormat( 8042 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8043 " : cccccccccccccccccc\n" 8044 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8045 " : 3333333333333333;", 8046 Style); 8047 verifyFormat( 8048 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8049 " : cccccccccccccccc ? dddddddddddddddddd\n" 8050 " : eeeeeeeeeeeeeeeeee\n" 8051 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 8052 " : 3333333333333333;", 8053 Style); 8054 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa\n" 8055 " ? (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8056 " : cccccccccccccccccc ? dddddddddddddddddd\n" 8057 " : eeeeeeeeeeeeeeeeee)\n" 8058 " : bbbbbbbbbbbbbbbbbbb ? 2222222222222222\n" 8059 " : 3333333333333333;", 8060 Style); 8061 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaa\n" 8062 " ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 8063 " : cccccccccccccccc ? dddddddddddddddddd\n" 8064 " : eeeeeeeeeeeeeeeeee\n" 8065 " : bbbbbbbbbbbbbbbbbbbbbbb ? 2222222222222222\n" 8066 " : 3333333333333333;", 8067 Style); 8068 8069 Style.AlignOperands = FormatStyle::OAS_DontAlign; 8070 Style.BreakBeforeTernaryOperators = false; 8071 // FIXME: Aligning the question marks is weird given DontAlign. 8072 // Consider disabling this alignment in this case. Also check whether this 8073 // will render the adjustment from https://reviews.llvm.org/D82199 8074 // unnecessary. 8075 verifyFormat("int x = aaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa :\n" 8076 " bbbb ? cccccccccccccccccc :\n" 8077 " ddddd;\n", 8078 Style); 8079 8080 EXPECT_EQ( 8081 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n" 8082 " /*\n" 8083 " */\n" 8084 " function() {\n" 8085 " try {\n" 8086 " return JJJJJJJJJJJJJJ(\n" 8087 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n" 8088 " }\n" 8089 " } :\n" 8090 " function() {};", 8091 format( 8092 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n" 8093 " /*\n" 8094 " */\n" 8095 " function() {\n" 8096 " try {\n" 8097 " return JJJJJJJJJJJJJJ(\n" 8098 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n" 8099 " }\n" 8100 " } :\n" 8101 " function() {};", 8102 getGoogleStyle(FormatStyle::LK_JavaScript))); 8103 } 8104 8105 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 8106 FormatStyle Style = getLLVMStyleWithColumns(70); 8107 Style.BreakBeforeTernaryOperators = false; 8108 verifyFormat( 8109 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 8110 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 8111 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8112 Style); 8113 verifyFormat( 8114 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 8115 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 8116 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8117 Style); 8118 verifyFormat( 8119 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 8120 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8121 Style); 8122 verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n" 8123 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 8124 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8125 Style); 8126 verifyFormat( 8127 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 8128 " aaaaaaaaaaaaa);", 8129 Style); 8130 verifyFormat( 8131 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8132 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 8133 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8134 " aaaaaaaaaaaaa);", 8135 Style); 8136 verifyFormat( 8137 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8138 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8139 " aaaaaaaaaaaaa);", 8140 Style); 8141 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 8142 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8143 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 8144 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8145 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8146 Style); 8147 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8148 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 8149 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8150 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 8151 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8152 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 8153 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8154 Style); 8155 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8156 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 8157 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8158 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 8159 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8160 Style); 8161 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 8162 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 8163 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 8164 Style); 8165 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 8166 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 8167 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 8168 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 8169 Style); 8170 verifyFormat( 8171 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 8172 " aaaaaaaaaaaaaaa :\n" 8173 " aaaaaaaaaaaaaaa;", 8174 Style); 8175 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 8176 " aaaaaaaaa ?\n" 8177 " b :\n" 8178 " c);", 8179 Style); 8180 verifyFormat("unsigned Indent =\n" 8181 " format(TheLine.First,\n" 8182 " IndentForLevel[TheLine.Level] >= 0 ?\n" 8183 " IndentForLevel[TheLine.Level] :\n" 8184 " TheLine * 2,\n" 8185 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 8186 Style); 8187 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 8188 " aaaaaaaaaaaaaaa :\n" 8189 " bbbbbbbbbbbbbbb ? //\n" 8190 " ccccccccccccccc :\n" 8191 " ddddddddddddddd;", 8192 Style); 8193 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 8194 " aaaaaaaaaaaaaaa :\n" 8195 " (bbbbbbbbbbbbbbb ? //\n" 8196 " ccccccccccccccc :\n" 8197 " ddddddddddddddd);", 8198 Style); 8199 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 8200 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n" 8201 " ccccccccccccccccccccccccccc;", 8202 Style); 8203 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 8204 " aaaaa :\n" 8205 " bbbbbbbbbbbbbbb + cccccccccccccccc;", 8206 Style); 8207 8208 // Chained conditionals 8209 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 8210 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8211 " 3333333333333333;", 8212 Style); 8213 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 8214 " bbbbbbbbbb ? 2222222222222222 :\n" 8215 " 3333333333333333;", 8216 Style); 8217 verifyFormat("return aaaaaaaaaa ? 1111111111111111 :\n" 8218 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8219 " 3333333333333333;", 8220 Style); 8221 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 8222 " bbbbbbbbbbbbbbbb ? 222222 :\n" 8223 " 333333;", 8224 Style); 8225 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 8226 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8227 " cccccccccccccccc ? 3333333333333333 :\n" 8228 " 4444444444444444;", 8229 Style); 8230 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc) :\n" 8231 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8232 " 3333333333333333;", 8233 Style); 8234 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 8235 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8236 " (aaa ? bbb : ccc);", 8237 Style); 8238 verifyFormat( 8239 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8240 " cccccccccccccccccc) :\n" 8241 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8242 " 3333333333333333;", 8243 Style); 8244 verifyFormat( 8245 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8246 " cccccccccccccccccc) :\n" 8247 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8248 " 3333333333333333;", 8249 Style); 8250 verifyFormat( 8251 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8252 " dddddddddddddddddd) :\n" 8253 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8254 " 3333333333333333;", 8255 Style); 8256 verifyFormat( 8257 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8258 " dddddddddddddddddd) :\n" 8259 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8260 " 3333333333333333;", 8261 Style); 8262 verifyFormat( 8263 "return aaaaaaaaa ? 1111111111111111 :\n" 8264 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8265 " a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8266 " dddddddddddddddddd)\n", 8267 Style); 8268 verifyFormat( 8269 "return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 8270 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8271 " (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8272 " cccccccccccccccccc);", 8273 Style); 8274 verifyFormat( 8275 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8276 " ccccccccccccccccc ? dddddddddddddddddd :\n" 8277 " eeeeeeeeeeeeeeeeee) :\n" 8278 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8279 " 3333333333333333;", 8280 Style); 8281 verifyFormat( 8282 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8283 " ccccccccccccc ? dddddddddddddddddd :\n" 8284 " eeeeeeeeeeeeeeeeee) :\n" 8285 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8286 " 3333333333333333;", 8287 Style); 8288 verifyFormat( 8289 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8290 " ccccccccccccccccc ? dddddddddddddddddd :\n" 8291 " eeeeeeeeeeeeeeeeee) :\n" 8292 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8293 " 3333333333333333;", 8294 Style); 8295 verifyFormat( 8296 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8297 " cccccccccccccccccc :\n" 8298 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8299 " 3333333333333333;", 8300 Style); 8301 verifyFormat( 8302 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8303 " cccccccccccccccccc ? dddddddddddddddddd :\n" 8304 " eeeeeeeeeeeeeeeeee :\n" 8305 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8306 " 3333333333333333;", 8307 Style); 8308 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n" 8309 " (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8310 " cccccccccccccccccc ? dddddddddddddddddd :\n" 8311 " eeeeeeeeeeeeeeeeee) :\n" 8312 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8313 " 3333333333333333;", 8314 Style); 8315 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n" 8316 " aaaaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8317 " cccccccccccccccccccc ? dddddddddddddddddd :\n" 8318 " eeeeeeeeeeeeeeeeee :\n" 8319 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8320 " 3333333333333333;", 8321 Style); 8322 } 8323 8324 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 8325 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 8326 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 8327 verifyFormat("bool a = true, b = false;"); 8328 8329 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8330 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 8331 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 8332 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 8333 verifyFormat( 8334 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 8335 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 8336 " d = e && f;"); 8337 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 8338 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 8339 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 8340 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 8341 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 8342 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 8343 8344 FormatStyle Style = getGoogleStyle(); 8345 Style.PointerAlignment = FormatStyle::PAS_Left; 8346 Style.DerivePointerAlignment = false; 8347 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8348 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 8349 " *b = bbbbbbbbbbbbbbbbbbb;", 8350 Style); 8351 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 8352 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 8353 Style); 8354 verifyFormat("vector<int*> a, b;", Style); 8355 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style); 8356 } 8357 8358 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 8359 verifyFormat("arr[foo ? bar : baz];"); 8360 verifyFormat("f()[foo ? bar : baz];"); 8361 verifyFormat("(a + b)[foo ? bar : baz];"); 8362 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 8363 } 8364 8365 TEST_F(FormatTest, AlignsStringLiterals) { 8366 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 8367 " \"short literal\");"); 8368 verifyFormat( 8369 "looooooooooooooooooooooooongFunction(\n" 8370 " \"short literal\"\n" 8371 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 8372 verifyFormat("someFunction(\"Always break between multi-line\"\n" 8373 " \" string literals\",\n" 8374 " and, other, parameters);"); 8375 EXPECT_EQ("fun + \"1243\" /* comment */\n" 8376 " \"5678\";", 8377 format("fun + \"1243\" /* comment */\n" 8378 " \"5678\";", 8379 getLLVMStyleWithColumns(28))); 8380 EXPECT_EQ( 8381 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 8382 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 8383 " \"aaaaaaaaaaaaaaaa\";", 8384 format("aaaaaa =" 8385 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 8386 "aaaaaaaaaaaaaaaaaaaaa\" " 8387 "\"aaaaaaaaaaaaaaaa\";")); 8388 verifyFormat("a = a + \"a\"\n" 8389 " \"a\"\n" 8390 " \"a\";"); 8391 verifyFormat("f(\"a\", \"b\"\n" 8392 " \"c\");"); 8393 8394 verifyFormat( 8395 "#define LL_FORMAT \"ll\"\n" 8396 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 8397 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 8398 8399 verifyFormat("#define A(X) \\\n" 8400 " \"aaaaa\" #X \"bbbbbb\" \\\n" 8401 " \"ccccc\"", 8402 getLLVMStyleWithColumns(23)); 8403 verifyFormat("#define A \"def\"\n" 8404 "f(\"abc\" A \"ghi\"\n" 8405 " \"jkl\");"); 8406 8407 verifyFormat("f(L\"a\"\n" 8408 " L\"b\");"); 8409 verifyFormat("#define A(X) \\\n" 8410 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 8411 " L\"ccccc\"", 8412 getLLVMStyleWithColumns(25)); 8413 8414 verifyFormat("f(@\"a\"\n" 8415 " @\"b\");"); 8416 verifyFormat("NSString s = @\"a\"\n" 8417 " @\"b\"\n" 8418 " @\"c\";"); 8419 verifyFormat("NSString s = @\"a\"\n" 8420 " \"b\"\n" 8421 " \"c\";"); 8422 } 8423 8424 TEST_F(FormatTest, ReturnTypeBreakingStyle) { 8425 FormatStyle Style = getLLVMStyle(); 8426 // No declarations or definitions should be moved to own line. 8427 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; 8428 verifyFormat("class A {\n" 8429 " int f() { return 1; }\n" 8430 " int g();\n" 8431 "};\n" 8432 "int f() { return 1; }\n" 8433 "int g();\n", 8434 Style); 8435 8436 // All declarations and definitions should have the return type moved to its 8437 // own line. 8438 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 8439 Style.TypenameMacros = {"LIST"}; 8440 verifyFormat("SomeType\n" 8441 "funcdecl(LIST(uint64_t));", 8442 Style); 8443 verifyFormat("class E {\n" 8444 " int\n" 8445 " f() {\n" 8446 " return 1;\n" 8447 " }\n" 8448 " int\n" 8449 " g();\n" 8450 "};\n" 8451 "int\n" 8452 "f() {\n" 8453 " return 1;\n" 8454 "}\n" 8455 "int\n" 8456 "g();\n", 8457 Style); 8458 8459 // Top-level definitions, and no kinds of declarations should have the 8460 // return type moved to its own line. 8461 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; 8462 verifyFormat("class B {\n" 8463 " int f() { return 1; }\n" 8464 " int g();\n" 8465 "};\n" 8466 "int\n" 8467 "f() {\n" 8468 " return 1;\n" 8469 "}\n" 8470 "int g();\n", 8471 Style); 8472 8473 // Top-level definitions and declarations should have the return type moved 8474 // to its own line. 8475 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel; 8476 verifyFormat("class C {\n" 8477 " int f() { return 1; }\n" 8478 " int g();\n" 8479 "};\n" 8480 "int\n" 8481 "f() {\n" 8482 " return 1;\n" 8483 "}\n" 8484 "int\n" 8485 "g();\n", 8486 Style); 8487 8488 // All definitions should have the return type moved to its own line, but no 8489 // kinds of declarations. 8490 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 8491 verifyFormat("class D {\n" 8492 " int\n" 8493 " f() {\n" 8494 " return 1;\n" 8495 " }\n" 8496 " int g();\n" 8497 "};\n" 8498 "int\n" 8499 "f() {\n" 8500 " return 1;\n" 8501 "}\n" 8502 "int g();\n", 8503 Style); 8504 verifyFormat("const char *\n" 8505 "f(void) {\n" // Break here. 8506 " return \"\";\n" 8507 "}\n" 8508 "const char *bar(void);\n", // No break here. 8509 Style); 8510 verifyFormat("template <class T>\n" 8511 "T *\n" 8512 "f(T &c) {\n" // Break here. 8513 " return NULL;\n" 8514 "}\n" 8515 "template <class T> T *f(T &c);\n", // No break here. 8516 Style); 8517 verifyFormat("class C {\n" 8518 " int\n" 8519 " operator+() {\n" 8520 " return 1;\n" 8521 " }\n" 8522 " int\n" 8523 " operator()() {\n" 8524 " return 1;\n" 8525 " }\n" 8526 "};\n", 8527 Style); 8528 verifyFormat("void\n" 8529 "A::operator()() {}\n" 8530 "void\n" 8531 "A::operator>>() {}\n" 8532 "void\n" 8533 "A::operator+() {}\n" 8534 "void\n" 8535 "A::operator*() {}\n" 8536 "void\n" 8537 "A::operator->() {}\n" 8538 "void\n" 8539 "A::operator void *() {}\n" 8540 "void\n" 8541 "A::operator void &() {}\n" 8542 "void\n" 8543 "A::operator void &&() {}\n" 8544 "void\n" 8545 "A::operator char *() {}\n" 8546 "void\n" 8547 "A::operator[]() {}\n" 8548 "void\n" 8549 "A::operator!() {}\n" 8550 "void\n" 8551 "A::operator**() {}\n" 8552 "void\n" 8553 "A::operator<Foo> *() {}\n" 8554 "void\n" 8555 "A::operator<Foo> **() {}\n" 8556 "void\n" 8557 "A::operator<Foo> &() {}\n" 8558 "void\n" 8559 "A::operator void **() {}\n", 8560 Style); 8561 verifyFormat("constexpr auto\n" 8562 "operator()() const -> reference {}\n" 8563 "constexpr auto\n" 8564 "operator>>() const -> reference {}\n" 8565 "constexpr auto\n" 8566 "operator+() const -> reference {}\n" 8567 "constexpr auto\n" 8568 "operator*() const -> reference {}\n" 8569 "constexpr auto\n" 8570 "operator->() const -> reference {}\n" 8571 "constexpr auto\n" 8572 "operator++() const -> reference {}\n" 8573 "constexpr auto\n" 8574 "operator void *() const -> reference {}\n" 8575 "constexpr auto\n" 8576 "operator void **() const -> reference {}\n" 8577 "constexpr auto\n" 8578 "operator void *() const -> reference {}\n" 8579 "constexpr auto\n" 8580 "operator void &() const -> reference {}\n" 8581 "constexpr auto\n" 8582 "operator void &&() const -> reference {}\n" 8583 "constexpr auto\n" 8584 "operator char *() const -> reference {}\n" 8585 "constexpr auto\n" 8586 "operator!() const -> reference {}\n" 8587 "constexpr auto\n" 8588 "operator[]() const -> reference {}\n", 8589 Style); 8590 verifyFormat("void *operator new(std::size_t s);", // No break here. 8591 Style); 8592 verifyFormat("void *\n" 8593 "operator new(std::size_t s) {}", 8594 Style); 8595 verifyFormat("void *\n" 8596 "operator delete[](void *ptr) {}", 8597 Style); 8598 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 8599 verifyFormat("const char *\n" 8600 "f(void)\n" // Break here. 8601 "{\n" 8602 " return \"\";\n" 8603 "}\n" 8604 "const char *bar(void);\n", // No break here. 8605 Style); 8606 verifyFormat("template <class T>\n" 8607 "T *\n" // Problem here: no line break 8608 "f(T &c)\n" // Break here. 8609 "{\n" 8610 " return NULL;\n" 8611 "}\n" 8612 "template <class T> T *f(T &c);\n", // No break here. 8613 Style); 8614 verifyFormat("int\n" 8615 "foo(A<bool> a)\n" 8616 "{\n" 8617 " return a;\n" 8618 "}\n", 8619 Style); 8620 verifyFormat("int\n" 8621 "foo(A<8> a)\n" 8622 "{\n" 8623 " return a;\n" 8624 "}\n", 8625 Style); 8626 verifyFormat("int\n" 8627 "foo(A<B<bool>, 8> a)\n" 8628 "{\n" 8629 " return a;\n" 8630 "}\n", 8631 Style); 8632 verifyFormat("int\n" 8633 "foo(A<B<8>, bool> a)\n" 8634 "{\n" 8635 " return a;\n" 8636 "}\n", 8637 Style); 8638 verifyFormat("int\n" 8639 "foo(A<B<bool>, bool> a)\n" 8640 "{\n" 8641 " return a;\n" 8642 "}\n", 8643 Style); 8644 verifyFormat("int\n" 8645 "foo(A<B<8>, 8> a)\n" 8646 "{\n" 8647 " return a;\n" 8648 "}\n", 8649 Style); 8650 8651 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 8652 Style.BraceWrapping.AfterFunction = true; 8653 verifyFormat("int f(i);\n" // No break here. 8654 "int\n" // Break here. 8655 "f(i)\n" 8656 "{\n" 8657 " return i + 1;\n" 8658 "}\n" 8659 "int\n" // Break here. 8660 "f(i)\n" 8661 "{\n" 8662 " return i + 1;\n" 8663 "};", 8664 Style); 8665 verifyFormat("int f(a, b, c);\n" // No break here. 8666 "int\n" // Break here. 8667 "f(a, b, c)\n" // Break here. 8668 "short a, b;\n" 8669 "float c;\n" 8670 "{\n" 8671 " return a + b < c;\n" 8672 "}\n" 8673 "int\n" // Break here. 8674 "f(a, b, c)\n" // Break here. 8675 "short a, b;\n" 8676 "float c;\n" 8677 "{\n" 8678 " return a + b < c;\n" 8679 "};", 8680 Style); 8681 verifyFormat("byte *\n" // Break here. 8682 "f(a)\n" // Break here. 8683 "byte a[];\n" 8684 "{\n" 8685 " return a;\n" 8686 "}", 8687 Style); 8688 verifyFormat("bool f(int a, int) override;\n" 8689 "Bar g(int a, Bar) final;\n" 8690 "Bar h(a, Bar) final;", 8691 Style); 8692 verifyFormat("int\n" 8693 "f(a)", 8694 Style); 8695 verifyFormat("bool\n" 8696 "f(size_t = 0, bool b = false)\n" 8697 "{\n" 8698 " return !b;\n" 8699 "}", 8700 Style); 8701 8702 // The return breaking style doesn't affect: 8703 // * function and object definitions with attribute-like macros 8704 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n" 8705 " ABSL_GUARDED_BY(mutex) = {};", 8706 getGoogleStyleWithColumns(40)); 8707 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n" 8708 " ABSL_GUARDED_BY(mutex); // comment", 8709 getGoogleStyleWithColumns(40)); 8710 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n" 8711 " ABSL_GUARDED_BY(mutex1)\n" 8712 " ABSL_GUARDED_BY(mutex2);", 8713 getGoogleStyleWithColumns(40)); 8714 verifyFormat("Tttttt f(int a, int b)\n" 8715 " ABSL_GUARDED_BY(mutex1)\n" 8716 " ABSL_GUARDED_BY(mutex2);", 8717 getGoogleStyleWithColumns(40)); 8718 // * typedefs 8719 verifyFormat("typedef ATTR(X) char x;", getGoogleStyle()); 8720 8721 Style = getGNUStyle(); 8722 8723 // Test for comments at the end of function declarations. 8724 verifyFormat("void\n" 8725 "foo (int a, /*abc*/ int b) // def\n" 8726 "{\n" 8727 "}\n", 8728 Style); 8729 8730 verifyFormat("void\n" 8731 "foo (int a, /* abc */ int b) /* def */\n" 8732 "{\n" 8733 "}\n", 8734 Style); 8735 8736 // Definitions that should not break after return type 8737 verifyFormat("void foo (int a, int b); // def\n", Style); 8738 verifyFormat("void foo (int a, int b); /* def */\n", Style); 8739 verifyFormat("void foo (int a, int b);\n", Style); 8740 } 8741 8742 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 8743 FormatStyle NoBreak = getLLVMStyle(); 8744 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 8745 FormatStyle Break = getLLVMStyle(); 8746 Break.AlwaysBreakBeforeMultilineStrings = true; 8747 verifyFormat("aaaa = \"bbbb\"\n" 8748 " \"cccc\";", 8749 NoBreak); 8750 verifyFormat("aaaa =\n" 8751 " \"bbbb\"\n" 8752 " \"cccc\";", 8753 Break); 8754 verifyFormat("aaaa(\"bbbb\"\n" 8755 " \"cccc\");", 8756 NoBreak); 8757 verifyFormat("aaaa(\n" 8758 " \"bbbb\"\n" 8759 " \"cccc\");", 8760 Break); 8761 verifyFormat("aaaa(qqq, \"bbbb\"\n" 8762 " \"cccc\");", 8763 NoBreak); 8764 verifyFormat("aaaa(qqq,\n" 8765 " \"bbbb\"\n" 8766 " \"cccc\");", 8767 Break); 8768 verifyFormat("aaaa(qqq,\n" 8769 " L\"bbbb\"\n" 8770 " L\"cccc\");", 8771 Break); 8772 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 8773 " \"bbbb\"));", 8774 Break); 8775 verifyFormat("string s = someFunction(\n" 8776 " \"abc\"\n" 8777 " \"abc\");", 8778 Break); 8779 8780 // As we break before unary operators, breaking right after them is bad. 8781 verifyFormat("string foo = abc ? \"x\"\n" 8782 " \"blah blah blah blah blah blah\"\n" 8783 " : \"y\";", 8784 Break); 8785 8786 // Don't break if there is no column gain. 8787 verifyFormat("f(\"aaaa\"\n" 8788 " \"bbbb\");", 8789 Break); 8790 8791 // Treat literals with escaped newlines like multi-line string literals. 8792 EXPECT_EQ("x = \"a\\\n" 8793 "b\\\n" 8794 "c\";", 8795 format("x = \"a\\\n" 8796 "b\\\n" 8797 "c\";", 8798 NoBreak)); 8799 EXPECT_EQ("xxxx =\n" 8800 " \"a\\\n" 8801 "b\\\n" 8802 "c\";", 8803 format("xxxx = \"a\\\n" 8804 "b\\\n" 8805 "c\";", 8806 Break)); 8807 8808 EXPECT_EQ("NSString *const kString =\n" 8809 " @\"aaaa\"\n" 8810 " @\"bbbb\";", 8811 format("NSString *const kString = @\"aaaa\"\n" 8812 "@\"bbbb\";", 8813 Break)); 8814 8815 Break.ColumnLimit = 0; 8816 verifyFormat("const char *hello = \"hello llvm\";", Break); 8817 } 8818 8819 TEST_F(FormatTest, AlignsPipes) { 8820 verifyFormat( 8821 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8822 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8823 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8824 verifyFormat( 8825 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 8826 " << aaaaaaaaaaaaaaaaaaaa;"); 8827 verifyFormat( 8828 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8829 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8830 verifyFormat( 8831 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 8832 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8833 verifyFormat( 8834 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 8835 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 8836 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 8837 verifyFormat( 8838 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8839 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8840 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8841 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8842 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8843 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8844 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 8845 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 8846 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 8847 verifyFormat( 8848 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8849 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8850 verifyFormat( 8851 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n" 8852 " aaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8853 8854 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 8855 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 8856 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8857 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8858 " aaaaaaaaaaaaaaaaaaaaa)\n" 8859 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8860 verifyFormat("LOG_IF(aaa == //\n" 8861 " bbb)\n" 8862 " << a << b;"); 8863 8864 // But sometimes, breaking before the first "<<" is desirable. 8865 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 8866 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 8867 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 8868 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8869 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8870 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 8871 " << BEF << IsTemplate << Description << E->getType();"); 8872 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 8873 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8874 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8875 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 8876 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8877 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8878 " << aaa;"); 8879 8880 verifyFormat( 8881 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8882 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8883 8884 // Incomplete string literal. 8885 EXPECT_EQ("llvm::errs() << \"\n" 8886 " << a;", 8887 format("llvm::errs() << \"\n<<a;")); 8888 8889 verifyFormat("void f() {\n" 8890 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 8891 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 8892 "}"); 8893 8894 // Handle 'endl'. 8895 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 8896 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 8897 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 8898 8899 // Handle '\n'. 8900 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 8901 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 8902 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 8903 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 8904 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 8905 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 8906 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 8907 } 8908 8909 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) { 8910 verifyFormat("return out << \"somepacket = {\\n\"\n" 8911 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 8912 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 8913 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 8914 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 8915 " << \"}\";"); 8916 8917 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 8918 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 8919 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 8920 verifyFormat( 8921 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 8922 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 8923 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 8924 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 8925 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 8926 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 8927 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 8928 verifyFormat( 8929 "void f() {\n" 8930 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 8931 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 8932 "}"); 8933 8934 // Breaking before the first "<<" is generally not desirable. 8935 verifyFormat( 8936 "llvm::errs()\n" 8937 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8938 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8939 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8940 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 8941 getLLVMStyleWithColumns(70)); 8942 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 8943 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8944 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 8945 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8946 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 8947 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 8948 getLLVMStyleWithColumns(70)); 8949 8950 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 8951 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 8952 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;"); 8953 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 8954 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 8955 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);"); 8956 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n" 8957 " (aaaa + aaaa);", 8958 getLLVMStyleWithColumns(40)); 8959 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n" 8960 " (aaaaaaa + aaaaa));", 8961 getLLVMStyleWithColumns(40)); 8962 verifyFormat( 8963 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n" 8964 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n" 8965 " bbbbbbbbbbbbbbbbbbbbbbb);"); 8966 } 8967 8968 TEST_F(FormatTest, UnderstandsEquals) { 8969 verifyFormat( 8970 "aaaaaaaaaaaaaaaaa =\n" 8971 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8972 verifyFormat( 8973 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8974 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 8975 verifyFormat( 8976 "if (a) {\n" 8977 " f();\n" 8978 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8979 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 8980 "}"); 8981 8982 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8983 " 100000000 + 10000000) {\n}"); 8984 } 8985 8986 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 8987 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 8988 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 8989 8990 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 8991 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 8992 8993 verifyFormat( 8994 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 8995 " Parameter2);"); 8996 8997 verifyFormat( 8998 "ShortObject->shortFunction(\n" 8999 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 9000 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 9001 9002 verifyFormat("loooooooooooooongFunction(\n" 9003 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 9004 9005 verifyFormat( 9006 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 9007 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 9008 9009 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 9010 " .WillRepeatedly(Return(SomeValue));"); 9011 verifyFormat("void f() {\n" 9012 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 9013 " .Times(2)\n" 9014 " .WillRepeatedly(Return(SomeValue));\n" 9015 "}"); 9016 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 9017 " ccccccccccccccccccccccc);"); 9018 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9019 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9020 " .aaaaa(aaaaa),\n" 9021 " aaaaaaaaaaaaaaaaaaaaa);"); 9022 verifyFormat("void f() {\n" 9023 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9024 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 9025 "}"); 9026 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9027 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9028 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9029 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9030 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 9031 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9032 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9033 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9034 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 9035 "}"); 9036 9037 // Here, it is not necessary to wrap at "." or "->". 9038 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 9039 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 9040 verifyFormat( 9041 "aaaaaaaaaaa->aaaaaaaaa(\n" 9042 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9043 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 9044 9045 verifyFormat( 9046 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9047 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 9048 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 9049 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 9050 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 9051 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 9052 9053 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9054 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9055 " .a();"); 9056 9057 FormatStyle NoBinPacking = getLLVMStyle(); 9058 NoBinPacking.BinPackParameters = false; 9059 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 9060 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 9061 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 9062 " aaaaaaaaaaaaaaaaaaa,\n" 9063 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 9064 NoBinPacking); 9065 9066 // If there is a subsequent call, change to hanging indentation. 9067 verifyFormat( 9068 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9069 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 9070 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 9071 verifyFormat( 9072 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9073 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 9074 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9075 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9076 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 9077 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9078 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 9079 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 9080 } 9081 9082 TEST_F(FormatTest, WrapsTemplateDeclarations) { 9083 verifyFormat("template <typename T>\n" 9084 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 9085 verifyFormat("template <typename T>\n" 9086 "// T should be one of {A, B}.\n" 9087 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 9088 verifyFormat( 9089 "template <typename T>\n" 9090 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 9091 verifyFormat("template <typename T>\n" 9092 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 9093 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 9094 verifyFormat( 9095 "template <typename T>\n" 9096 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 9097 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 9098 verifyFormat( 9099 "template <typename T>\n" 9100 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 9101 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 9102 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9103 verifyFormat("template <typename T>\n" 9104 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9105 " int aaaaaaaaaaaaaaaaaaaaaa);"); 9106 verifyFormat( 9107 "template <typename T1, typename T2 = char, typename T3 = char,\n" 9108 " typename T4 = char>\n" 9109 "void f();"); 9110 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 9111 " template <typename> class cccccccccccccccccccccc,\n" 9112 " typename ddddddddddddd>\n" 9113 "class C {};"); 9114 verifyFormat( 9115 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 9116 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9117 9118 verifyFormat("void f() {\n" 9119 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 9120 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 9121 "}"); 9122 9123 verifyFormat("template <typename T> class C {};"); 9124 verifyFormat("template <typename T> void f();"); 9125 verifyFormat("template <typename T> void f() {}"); 9126 verifyFormat( 9127 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 9128 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9129 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 9130 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 9131 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9132 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 9133 " bbbbbbbbbbbbbbbbbbbbbbbb);", 9134 getLLVMStyleWithColumns(72)); 9135 EXPECT_EQ("static_cast<A< //\n" 9136 " B> *>(\n" 9137 "\n" 9138 ");", 9139 format("static_cast<A<//\n" 9140 " B>*>(\n" 9141 "\n" 9142 " );")); 9143 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9144 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 9145 9146 FormatStyle AlwaysBreak = getLLVMStyle(); 9147 AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 9148 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 9149 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 9150 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 9151 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9152 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 9153 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 9154 verifyFormat("template <template <typename> class Fooooooo,\n" 9155 " template <typename> class Baaaaaaar>\n" 9156 "struct C {};", 9157 AlwaysBreak); 9158 verifyFormat("template <typename T> // T can be A, B or C.\n" 9159 "struct C {};", 9160 AlwaysBreak); 9161 verifyFormat("template <enum E> class A {\n" 9162 "public:\n" 9163 " E *f();\n" 9164 "};"); 9165 9166 FormatStyle NeverBreak = getLLVMStyle(); 9167 NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No; 9168 verifyFormat("template <typename T> class C {};", NeverBreak); 9169 verifyFormat("template <typename T> void f();", NeverBreak); 9170 verifyFormat("template <typename T> void f() {}", NeverBreak); 9171 verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa " 9172 "bbbbbbbbbbbbbbbbbbbb) {}", 9173 NeverBreak); 9174 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9175 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 9176 " ccccccccccccccccccccccccccccccccccccccccccccccc);", 9177 NeverBreak); 9178 verifyFormat("template <template <typename> class Fooooooo,\n" 9179 " template <typename> class Baaaaaaar>\n" 9180 "struct C {};", 9181 NeverBreak); 9182 verifyFormat("template <typename T> // T can be A, B or C.\n" 9183 "struct C {};", 9184 NeverBreak); 9185 verifyFormat("template <enum E> class A {\n" 9186 "public:\n" 9187 " E *f();\n" 9188 "};", 9189 NeverBreak); 9190 NeverBreak.PenaltyBreakTemplateDeclaration = 100; 9191 verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa " 9192 "bbbbbbbbbbbbbbbbbbbb) {}", 9193 NeverBreak); 9194 } 9195 9196 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) { 9197 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 9198 Style.ColumnLimit = 60; 9199 EXPECT_EQ("// Baseline - no comments.\n" 9200 "template <\n" 9201 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 9202 "void f() {}", 9203 format("// Baseline - no comments.\n" 9204 "template <\n" 9205 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 9206 "void f() {}", 9207 Style)); 9208 9209 EXPECT_EQ("template <\n" 9210 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 9211 "void f() {}", 9212 format("template <\n" 9213 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 9214 "void f() {}", 9215 Style)); 9216 9217 EXPECT_EQ( 9218 "template <\n" 9219 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 9220 "void f() {}", 9221 format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 9222 "void f() {}", 9223 Style)); 9224 9225 EXPECT_EQ( 9226 "template <\n" 9227 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 9228 " // multiline\n" 9229 "void f() {}", 9230 format("template <\n" 9231 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 9232 " // multiline\n" 9233 "void f() {}", 9234 Style)); 9235 9236 EXPECT_EQ( 9237 "template <typename aaaaaaaaaa<\n" 9238 " bbbbbbbbbbbb>::value> // trailing loooong\n" 9239 "void f() {}", 9240 format( 9241 "template <\n" 9242 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n" 9243 "void f() {}", 9244 Style)); 9245 } 9246 9247 TEST_F(FormatTest, WrapsTemplateParameters) { 9248 FormatStyle Style = getLLVMStyle(); 9249 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 9250 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 9251 verifyFormat( 9252 "template <typename... a> struct q {};\n" 9253 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 9254 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 9255 " y;", 9256 Style); 9257 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 9258 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9259 verifyFormat( 9260 "template <typename... a> struct r {};\n" 9261 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 9262 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 9263 " y;", 9264 Style); 9265 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 9266 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 9267 verifyFormat("template <typename... a> struct s {};\n" 9268 "extern s<\n" 9269 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 9270 "aaaaaaaaaaaaaaaaaaaaaa,\n" 9271 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 9272 "aaaaaaaaaaaaaaaaaaaaaa>\n" 9273 " y;", 9274 Style); 9275 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 9276 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9277 verifyFormat("template <typename... a> struct t {};\n" 9278 "extern t<\n" 9279 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 9280 "aaaaaaaaaaaaaaaaaaaaaa,\n" 9281 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 9282 "aaaaaaaaaaaaaaaaaaaaaa>\n" 9283 " y;", 9284 Style); 9285 } 9286 9287 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 9288 verifyFormat( 9289 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 9290 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 9291 verifyFormat( 9292 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 9293 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9294 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 9295 9296 // FIXME: Should we have the extra indent after the second break? 9297 verifyFormat( 9298 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 9299 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 9300 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 9301 9302 verifyFormat( 9303 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 9304 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 9305 9306 // Breaking at nested name specifiers is generally not desirable. 9307 verifyFormat( 9308 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9309 " aaaaaaaaaaaaaaaaaaaaaaa);"); 9310 9311 verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n" 9312 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 9313 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9314 " aaaaaaaaaaaaaaaaaaaaa);", 9315 getLLVMStyleWithColumns(74)); 9316 9317 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 9318 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9319 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 9320 } 9321 9322 TEST_F(FormatTest, UnderstandsTemplateParameters) { 9323 verifyFormat("A<int> a;"); 9324 verifyFormat("A<A<A<int>>> a;"); 9325 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 9326 verifyFormat("bool x = a < 1 || 2 > a;"); 9327 verifyFormat("bool x = 5 < f<int>();"); 9328 verifyFormat("bool x = f<int>() > 5;"); 9329 verifyFormat("bool x = 5 < a<int>::x;"); 9330 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 9331 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 9332 9333 verifyGoogleFormat("A<A<int>> a;"); 9334 verifyGoogleFormat("A<A<A<int>>> a;"); 9335 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 9336 verifyGoogleFormat("A<A<int> > a;"); 9337 verifyGoogleFormat("A<A<A<int> > > a;"); 9338 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 9339 verifyGoogleFormat("A<::A<int>> a;"); 9340 verifyGoogleFormat("A<::A> a;"); 9341 verifyGoogleFormat("A< ::A> a;"); 9342 verifyGoogleFormat("A< ::A<int> > a;"); 9343 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 9344 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 9345 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 9346 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 9347 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 9348 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 9349 9350 verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 9351 9352 // template closer followed by a token that starts with > or = 9353 verifyFormat("bool b = a<1> > 1;"); 9354 verifyFormat("bool b = a<1> >= 1;"); 9355 verifyFormat("int i = a<1> >> 1;"); 9356 FormatStyle Style = getLLVMStyle(); 9357 Style.SpaceBeforeAssignmentOperators = false; 9358 verifyFormat("bool b= a<1> == 1;", Style); 9359 verifyFormat("a<int> = 1;", Style); 9360 verifyFormat("a<int> >>= 1;", Style); 9361 9362 verifyFormat("test < a | b >> c;"); 9363 verifyFormat("test<test<a | b>> c;"); 9364 verifyFormat("test >> a >> b;"); 9365 verifyFormat("test << a >> b;"); 9366 9367 verifyFormat("f<int>();"); 9368 verifyFormat("template <typename T> void f() {}"); 9369 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 9370 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 9371 "sizeof(char)>::type>;"); 9372 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 9373 verifyFormat("f(a.operator()<A>());"); 9374 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9375 " .template operator()<A>());", 9376 getLLVMStyleWithColumns(35)); 9377 9378 // Not template parameters. 9379 verifyFormat("return a < b && c > d;"); 9380 verifyFormat("void f() {\n" 9381 " while (a < b && c > d) {\n" 9382 " }\n" 9383 "}"); 9384 verifyFormat("template <typename... Types>\n" 9385 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 9386 9387 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9388 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 9389 getLLVMStyleWithColumns(60)); 9390 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 9391 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 9392 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 9393 verifyFormat("some_templated_type<decltype([](int i) { return i; })>"); 9394 } 9395 9396 TEST_F(FormatTest, UnderstandsShiftOperators) { 9397 verifyFormat("if (i < x >> 1)"); 9398 verifyFormat("while (i < x >> 1)"); 9399 verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)"); 9400 verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)"); 9401 verifyFormat( 9402 "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)"); 9403 verifyFormat("Foo.call<Bar<Function>>()"); 9404 verifyFormat("if (Foo.call<Bar<Function>>() == 0)"); 9405 verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; " 9406 "++i, v = v >> 1)"); 9407 verifyFormat("if (w<u<v<x>>, 1>::t)"); 9408 } 9409 9410 TEST_F(FormatTest, BitshiftOperatorWidth) { 9411 EXPECT_EQ("int a = 1 << 2; /* foo\n" 9412 " bar */", 9413 format("int a=1<<2; /* foo\n" 9414 " bar */")); 9415 9416 EXPECT_EQ("int b = 256 >> 1; /* foo\n" 9417 " bar */", 9418 format("int b =256>>1 ; /* foo\n" 9419 " bar */")); 9420 } 9421 9422 TEST_F(FormatTest, UnderstandsBinaryOperators) { 9423 verifyFormat("COMPARE(a, ==, b);"); 9424 verifyFormat("auto s = sizeof...(Ts) - 1;"); 9425 } 9426 9427 TEST_F(FormatTest, UnderstandsPointersToMembers) { 9428 verifyFormat("int A::*x;"); 9429 verifyFormat("int (S::*func)(void *);"); 9430 verifyFormat("void f() { int (S::*func)(void *); }"); 9431 verifyFormat("typedef bool *(Class::*Member)() const;"); 9432 verifyFormat("void f() {\n" 9433 " (a->*f)();\n" 9434 " a->*x;\n" 9435 " (a.*f)();\n" 9436 " ((*a).*f)();\n" 9437 " a.*x;\n" 9438 "}"); 9439 verifyFormat("void f() {\n" 9440 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 9441 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 9442 "}"); 9443 verifyFormat( 9444 "(aaaaaaaaaa->*bbbbbbb)(\n" 9445 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 9446 FormatStyle Style = getLLVMStyle(); 9447 Style.PointerAlignment = FormatStyle::PAS_Left; 9448 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 9449 } 9450 9451 TEST_F(FormatTest, UnderstandsUnaryOperators) { 9452 verifyFormat("int a = -2;"); 9453 verifyFormat("f(-1, -2, -3);"); 9454 verifyFormat("a[-1] = 5;"); 9455 verifyFormat("int a = 5 + -2;"); 9456 verifyFormat("if (i == -1) {\n}"); 9457 verifyFormat("if (i != -1) {\n}"); 9458 verifyFormat("if (i > -1) {\n}"); 9459 verifyFormat("if (i < -1) {\n}"); 9460 verifyFormat("++(a->f());"); 9461 verifyFormat("--(a->f());"); 9462 verifyFormat("(a->f())++;"); 9463 verifyFormat("a[42]++;"); 9464 verifyFormat("if (!(a->f())) {\n}"); 9465 verifyFormat("if (!+i) {\n}"); 9466 verifyFormat("~&a;"); 9467 9468 verifyFormat("a-- > b;"); 9469 verifyFormat("b ? -a : c;"); 9470 verifyFormat("n * sizeof char16;"); 9471 verifyFormat("n * alignof char16;", getGoogleStyle()); 9472 verifyFormat("sizeof(char);"); 9473 verifyFormat("alignof(char);", getGoogleStyle()); 9474 9475 verifyFormat("return -1;"); 9476 verifyFormat("throw -1;"); 9477 verifyFormat("switch (a) {\n" 9478 "case -1:\n" 9479 " break;\n" 9480 "}"); 9481 verifyFormat("#define X -1"); 9482 verifyFormat("#define X -kConstant"); 9483 9484 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 9485 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 9486 9487 verifyFormat("int a = /* confusing comment */ -1;"); 9488 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 9489 verifyFormat("int a = i /* confusing comment */++;"); 9490 9491 verifyFormat("co_yield -1;"); 9492 verifyFormat("co_return -1;"); 9493 9494 // Check that * is not treated as a binary operator when we set 9495 // PointerAlignment as PAS_Left after a keyword and not a declaration. 9496 FormatStyle PASLeftStyle = getLLVMStyle(); 9497 PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left; 9498 verifyFormat("co_return *a;", PASLeftStyle); 9499 verifyFormat("co_await *a;", PASLeftStyle); 9500 verifyFormat("co_yield *a", PASLeftStyle); 9501 verifyFormat("return *a;", PASLeftStyle); 9502 } 9503 9504 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 9505 verifyFormat("if (!aaaaaaaaaa( // break\n" 9506 " aaaaa)) {\n" 9507 "}"); 9508 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 9509 " aaaaa));"); 9510 verifyFormat("*aaa = aaaaaaa( // break\n" 9511 " bbbbbb);"); 9512 } 9513 9514 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 9515 verifyFormat("bool operator<();"); 9516 verifyFormat("bool operator>();"); 9517 verifyFormat("bool operator=();"); 9518 verifyFormat("bool operator==();"); 9519 verifyFormat("bool operator!=();"); 9520 verifyFormat("int operator+();"); 9521 verifyFormat("int operator++();"); 9522 verifyFormat("int operator++(int) volatile noexcept;"); 9523 verifyFormat("bool operator,();"); 9524 verifyFormat("bool operator();"); 9525 verifyFormat("bool operator()();"); 9526 verifyFormat("bool operator[]();"); 9527 verifyFormat("operator bool();"); 9528 verifyFormat("operator int();"); 9529 verifyFormat("operator void *();"); 9530 verifyFormat("operator SomeType<int>();"); 9531 verifyFormat("operator SomeType<int, int>();"); 9532 verifyFormat("operator SomeType<SomeType<int>>();"); 9533 verifyFormat("operator< <>();"); 9534 verifyFormat("operator<< <>();"); 9535 verifyFormat("< <>"); 9536 9537 verifyFormat("void *operator new(std::size_t size);"); 9538 verifyFormat("void *operator new[](std::size_t size);"); 9539 verifyFormat("void operator delete(void *ptr);"); 9540 verifyFormat("void operator delete[](void *ptr);"); 9541 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 9542 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 9543 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 9544 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 9545 9546 verifyFormat( 9547 "ostream &operator<<(ostream &OutputStream,\n" 9548 " SomeReallyLongType WithSomeReallyLongValue);"); 9549 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 9550 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 9551 " return left.group < right.group;\n" 9552 "}"); 9553 verifyFormat("SomeType &operator=(const SomeType &S);"); 9554 verifyFormat("f.template operator()<int>();"); 9555 9556 verifyGoogleFormat("operator void*();"); 9557 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 9558 verifyGoogleFormat("operator ::A();"); 9559 9560 verifyFormat("using A::operator+;"); 9561 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 9562 "int i;"); 9563 9564 // Calling an operator as a member function. 9565 verifyFormat("void f() { a.operator*(); }"); 9566 verifyFormat("void f() { a.operator*(b & b); }"); 9567 verifyFormat("void f() { a->operator&(a * b); }"); 9568 verifyFormat("void f() { NS::a.operator+(*b * *b); }"); 9569 // TODO: Calling an operator as a non-member function is hard to distinguish. 9570 // https://llvm.org/PR50629 9571 // verifyFormat("void f() { operator*(a & a); }"); 9572 // verifyFormat("void f() { operator&(a, b * b); }"); 9573 9574 verifyFormat("::operator delete(foo);"); 9575 verifyFormat("::operator new(n * sizeof(foo));"); 9576 verifyFormat("foo() { ::operator delete(foo); }"); 9577 verifyFormat("foo() { ::operator new(n * sizeof(foo)); }"); 9578 } 9579 9580 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 9581 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 9582 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 9583 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 9584 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 9585 verifyFormat("Deleted &operator=(const Deleted &) &;"); 9586 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 9587 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 9588 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 9589 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 9590 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 9591 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 9592 verifyFormat("void Fn(T const &) const &;"); 9593 verifyFormat("void Fn(T const volatile &&) const volatile &&;"); 9594 verifyFormat("template <typename T>\n" 9595 "void F(T) && = delete;", 9596 getGoogleStyle()); 9597 9598 FormatStyle AlignLeft = getLLVMStyle(); 9599 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 9600 verifyFormat("void A::b() && {}", AlignLeft); 9601 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 9602 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 9603 AlignLeft); 9604 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 9605 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 9606 verifyFormat("auto Function(T t) & -> void {}", AlignLeft); 9607 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft); 9608 verifyFormat("auto Function(T) & -> void {}", AlignLeft); 9609 verifyFormat("auto Function(T) & -> void;", AlignLeft); 9610 verifyFormat("void Fn(T const&) const&;", AlignLeft); 9611 verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft); 9612 9613 FormatStyle Spaces = getLLVMStyle(); 9614 Spaces.SpacesInCStyleCastParentheses = true; 9615 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 9616 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 9617 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 9618 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 9619 9620 Spaces.SpacesInCStyleCastParentheses = false; 9621 Spaces.SpacesInParentheses = true; 9622 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 9623 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", 9624 Spaces); 9625 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 9626 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 9627 9628 FormatStyle BreakTemplate = getLLVMStyle(); 9629 BreakTemplate.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 9630 9631 verifyFormat("struct f {\n" 9632 " template <class T>\n" 9633 " int &foo(const std::string &str) &noexcept {}\n" 9634 "};", 9635 BreakTemplate); 9636 9637 verifyFormat("struct f {\n" 9638 " template <class T>\n" 9639 " int &foo(const std::string &str) &&noexcept {}\n" 9640 "};", 9641 BreakTemplate); 9642 9643 verifyFormat("struct f {\n" 9644 " template <class T>\n" 9645 " int &foo(const std::string &str) const &noexcept {}\n" 9646 "};", 9647 BreakTemplate); 9648 9649 verifyFormat("struct f {\n" 9650 " template <class T>\n" 9651 " int &foo(const std::string &str) const &noexcept {}\n" 9652 "};", 9653 BreakTemplate); 9654 9655 verifyFormat("struct f {\n" 9656 " template <class T>\n" 9657 " auto foo(const std::string &str) &&noexcept -> int & {}\n" 9658 "};", 9659 BreakTemplate); 9660 9661 FormatStyle AlignLeftBreakTemplate = getLLVMStyle(); 9662 AlignLeftBreakTemplate.AlwaysBreakTemplateDeclarations = 9663 FormatStyle::BTDS_Yes; 9664 AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left; 9665 9666 verifyFormat("struct f {\n" 9667 " template <class T>\n" 9668 " int& foo(const std::string& str) & noexcept {}\n" 9669 "};", 9670 AlignLeftBreakTemplate); 9671 9672 verifyFormat("struct f {\n" 9673 " template <class T>\n" 9674 " int& foo(const std::string& str) && noexcept {}\n" 9675 "};", 9676 AlignLeftBreakTemplate); 9677 9678 verifyFormat("struct f {\n" 9679 " template <class T>\n" 9680 " int& foo(const std::string& str) const& noexcept {}\n" 9681 "};", 9682 AlignLeftBreakTemplate); 9683 9684 verifyFormat("struct f {\n" 9685 " template <class T>\n" 9686 " int& foo(const std::string& str) const&& noexcept {}\n" 9687 "};", 9688 AlignLeftBreakTemplate); 9689 9690 verifyFormat("struct f {\n" 9691 " template <class T>\n" 9692 " auto foo(const std::string& str) && noexcept -> int& {}\n" 9693 "};", 9694 AlignLeftBreakTemplate); 9695 9696 // The `&` in `Type&` should not be confused with a trailing `&` of 9697 // DEPRECATED(reason) member function. 9698 verifyFormat("struct f {\n" 9699 " template <class T>\n" 9700 " DEPRECATED(reason)\n" 9701 " Type &foo(arguments) {}\n" 9702 "};", 9703 BreakTemplate); 9704 9705 verifyFormat("struct f {\n" 9706 " template <class T>\n" 9707 " DEPRECATED(reason)\n" 9708 " Type& foo(arguments) {}\n" 9709 "};", 9710 AlignLeftBreakTemplate); 9711 9712 verifyFormat("void (*foopt)(int) = &func;"); 9713 } 9714 9715 TEST_F(FormatTest, UnderstandsNewAndDelete) { 9716 verifyFormat("void f() {\n" 9717 " A *a = new A;\n" 9718 " A *a = new (placement) A;\n" 9719 " delete a;\n" 9720 " delete (A *)a;\n" 9721 "}"); 9722 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 9723 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 9724 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 9725 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 9726 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 9727 verifyFormat("delete[] h->p;"); 9728 9729 verifyFormat("void operator delete(void *foo) ATTRIB;"); 9730 verifyFormat("void operator new(void *foo) ATTRIB;"); 9731 verifyFormat("void operator delete[](void *foo) ATTRIB;"); 9732 verifyFormat("void operator delete(void *ptr) noexcept;"); 9733 } 9734 9735 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 9736 verifyFormat("int *f(int *a) {}"); 9737 verifyFormat("int main(int argc, char **argv) {}"); 9738 verifyFormat("Test::Test(int b) : a(b * b) {}"); 9739 verifyIndependentOfContext("f(a, *a);"); 9740 verifyFormat("void g() { f(*a); }"); 9741 verifyIndependentOfContext("int a = b * 10;"); 9742 verifyIndependentOfContext("int a = 10 * b;"); 9743 verifyIndependentOfContext("int a = b * c;"); 9744 verifyIndependentOfContext("int a += b * c;"); 9745 verifyIndependentOfContext("int a -= b * c;"); 9746 verifyIndependentOfContext("int a *= b * c;"); 9747 verifyIndependentOfContext("int a /= b * c;"); 9748 verifyIndependentOfContext("int a = *b;"); 9749 verifyIndependentOfContext("int a = *b * c;"); 9750 verifyIndependentOfContext("int a = b * *c;"); 9751 verifyIndependentOfContext("int a = b * (10);"); 9752 verifyIndependentOfContext("S << b * (10);"); 9753 verifyIndependentOfContext("return 10 * b;"); 9754 verifyIndependentOfContext("return *b * *c;"); 9755 verifyIndependentOfContext("return a & ~b;"); 9756 verifyIndependentOfContext("f(b ? *c : *d);"); 9757 verifyIndependentOfContext("int a = b ? *c : *d;"); 9758 verifyIndependentOfContext("*b = a;"); 9759 verifyIndependentOfContext("a * ~b;"); 9760 verifyIndependentOfContext("a * !b;"); 9761 verifyIndependentOfContext("a * +b;"); 9762 verifyIndependentOfContext("a * -b;"); 9763 verifyIndependentOfContext("a * ++b;"); 9764 verifyIndependentOfContext("a * --b;"); 9765 verifyIndependentOfContext("a[4] * b;"); 9766 verifyIndependentOfContext("a[a * a] = 1;"); 9767 verifyIndependentOfContext("f() * b;"); 9768 verifyIndependentOfContext("a * [self dostuff];"); 9769 verifyIndependentOfContext("int x = a * (a + b);"); 9770 verifyIndependentOfContext("(a *)(a + b);"); 9771 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 9772 verifyIndependentOfContext("int *pa = (int *)&a;"); 9773 verifyIndependentOfContext("return sizeof(int **);"); 9774 verifyIndependentOfContext("return sizeof(int ******);"); 9775 verifyIndependentOfContext("return (int **&)a;"); 9776 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 9777 verifyFormat("void f(Type (*parameter)[10]) {}"); 9778 verifyFormat("void f(Type (¶meter)[10]) {}"); 9779 verifyGoogleFormat("return sizeof(int**);"); 9780 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 9781 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 9782 verifyFormat("auto a = [](int **&, int ***) {};"); 9783 verifyFormat("auto PointerBinding = [](const char *S) {};"); 9784 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 9785 verifyFormat("[](const decltype(*a) &value) {}"); 9786 verifyFormat("[](const typeof(*a) &value) {}"); 9787 verifyFormat("[](const _Atomic(a *) &value) {}"); 9788 verifyFormat("[](const __underlying_type(a) &value) {}"); 9789 verifyFormat("decltype(a * b) F();"); 9790 verifyFormat("typeof(a * b) F();"); 9791 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 9792 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 9793 verifyIndependentOfContext("typedef void (*f)(int *a);"); 9794 verifyIndependentOfContext("int i{a * b};"); 9795 verifyIndependentOfContext("aaa && aaa->f();"); 9796 verifyIndependentOfContext("int x = ~*p;"); 9797 verifyFormat("Constructor() : a(a), area(width * height) {}"); 9798 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 9799 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 9800 verifyFormat("void f() { f(a, c * d); }"); 9801 verifyFormat("void f() { f(new a(), c * d); }"); 9802 verifyFormat("void f(const MyOverride &override);"); 9803 verifyFormat("void f(const MyFinal &final);"); 9804 verifyIndependentOfContext("bool a = f() && override.f();"); 9805 verifyIndependentOfContext("bool a = f() && final.f();"); 9806 9807 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 9808 9809 verifyIndependentOfContext("A<int *> a;"); 9810 verifyIndependentOfContext("A<int **> a;"); 9811 verifyIndependentOfContext("A<int *, int *> a;"); 9812 verifyIndependentOfContext("A<int *[]> a;"); 9813 verifyIndependentOfContext( 9814 "const char *const p = reinterpret_cast<const char *const>(q);"); 9815 verifyIndependentOfContext("A<int **, int **> a;"); 9816 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 9817 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 9818 verifyFormat("for (; a && b;) {\n}"); 9819 verifyFormat("bool foo = true && [] { return false; }();"); 9820 9821 verifyFormat( 9822 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9823 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9824 9825 verifyGoogleFormat("int const* a = &b;"); 9826 verifyGoogleFormat("**outparam = 1;"); 9827 verifyGoogleFormat("*outparam = a * b;"); 9828 verifyGoogleFormat("int main(int argc, char** argv) {}"); 9829 verifyGoogleFormat("A<int*> a;"); 9830 verifyGoogleFormat("A<int**> a;"); 9831 verifyGoogleFormat("A<int*, int*> a;"); 9832 verifyGoogleFormat("A<int**, int**> a;"); 9833 verifyGoogleFormat("f(b ? *c : *d);"); 9834 verifyGoogleFormat("int a = b ? *c : *d;"); 9835 verifyGoogleFormat("Type* t = **x;"); 9836 verifyGoogleFormat("Type* t = *++*x;"); 9837 verifyGoogleFormat("*++*x;"); 9838 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 9839 verifyGoogleFormat("Type* t = x++ * y;"); 9840 verifyGoogleFormat( 9841 "const char* const p = reinterpret_cast<const char* const>(q);"); 9842 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 9843 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 9844 verifyGoogleFormat("template <typename T>\n" 9845 "void f(int i = 0, SomeType** temps = NULL);"); 9846 9847 FormatStyle Left = getLLVMStyle(); 9848 Left.PointerAlignment = FormatStyle::PAS_Left; 9849 verifyFormat("x = *a(x) = *a(y);", Left); 9850 verifyFormat("for (;; *a = b) {\n}", Left); 9851 verifyFormat("return *this += 1;", Left); 9852 verifyFormat("throw *x;", Left); 9853 verifyFormat("delete *x;", Left); 9854 verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left); 9855 verifyFormat("[](const decltype(*a)* ptr) {}", Left); 9856 verifyFormat("[](const typeof(*a)* ptr) {}", Left); 9857 verifyFormat("[](const _Atomic(a*)* ptr) {}", Left); 9858 verifyFormat("[](const __underlying_type(a)* ptr) {}", Left); 9859 verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left); 9860 verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left); 9861 verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left); 9862 verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left); 9863 9864 verifyIndependentOfContext("a = *(x + y);"); 9865 verifyIndependentOfContext("a = &(x + y);"); 9866 verifyIndependentOfContext("*(x + y).call();"); 9867 verifyIndependentOfContext("&(x + y)->call();"); 9868 verifyFormat("void f() { &(*I).first; }"); 9869 9870 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 9871 verifyFormat("f(* /* confusing comment */ foo);"); 9872 verifyFormat("void (* /*deleter*/)(const Slice &key, void *value)"); 9873 verifyFormat("void foo(int * // this is the first paramters\n" 9874 " ,\n" 9875 " int second);"); 9876 verifyFormat("double term = a * // first\n" 9877 " b;"); 9878 verifyFormat( 9879 "int *MyValues = {\n" 9880 " *A, // Operator detection might be confused by the '{'\n" 9881 " *BB // Operator detection might be confused by previous comment\n" 9882 "};"); 9883 9884 verifyIndependentOfContext("if (int *a = &b)"); 9885 verifyIndependentOfContext("if (int &a = *b)"); 9886 verifyIndependentOfContext("if (a & b[i])"); 9887 verifyIndependentOfContext("if constexpr (a & b[i])"); 9888 verifyIndependentOfContext("if CONSTEXPR (a & b[i])"); 9889 verifyIndependentOfContext("if (a * (b * c))"); 9890 verifyIndependentOfContext("if constexpr (a * (b * c))"); 9891 verifyIndependentOfContext("if CONSTEXPR (a * (b * c))"); 9892 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 9893 verifyIndependentOfContext("if (*b[i])"); 9894 verifyIndependentOfContext("if (int *a = (&b))"); 9895 verifyIndependentOfContext("while (int *a = &b)"); 9896 verifyIndependentOfContext("while (a * (b * c))"); 9897 verifyIndependentOfContext("size = sizeof *a;"); 9898 verifyIndependentOfContext("if (a && (b = c))"); 9899 verifyFormat("void f() {\n" 9900 " for (const int &v : Values) {\n" 9901 " }\n" 9902 "}"); 9903 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 9904 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 9905 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 9906 9907 verifyFormat("#define A (!a * b)"); 9908 verifyFormat("#define MACRO \\\n" 9909 " int *i = a * b; \\\n" 9910 " void f(a *b);", 9911 getLLVMStyleWithColumns(19)); 9912 9913 verifyIndependentOfContext("A = new SomeType *[Length];"); 9914 verifyIndependentOfContext("A = new SomeType *[Length]();"); 9915 verifyIndependentOfContext("T **t = new T *;"); 9916 verifyIndependentOfContext("T **t = new T *();"); 9917 verifyGoogleFormat("A = new SomeType*[Length]();"); 9918 verifyGoogleFormat("A = new SomeType*[Length];"); 9919 verifyGoogleFormat("T** t = new T*;"); 9920 verifyGoogleFormat("T** t = new T*();"); 9921 9922 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 9923 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 9924 verifyFormat("template <bool a, bool b> " 9925 "typename t::if<x && y>::type f() {}"); 9926 verifyFormat("template <int *y> f() {}"); 9927 verifyFormat("vector<int *> v;"); 9928 verifyFormat("vector<int *const> v;"); 9929 verifyFormat("vector<int *const **const *> v;"); 9930 verifyFormat("vector<int *volatile> v;"); 9931 verifyFormat("vector<a *_Nonnull> v;"); 9932 verifyFormat("vector<a *_Nullable> v;"); 9933 verifyFormat("vector<a *_Null_unspecified> v;"); 9934 verifyFormat("vector<a *__ptr32> v;"); 9935 verifyFormat("vector<a *__ptr64> v;"); 9936 verifyFormat("vector<a *__capability> v;"); 9937 FormatStyle TypeMacros = getLLVMStyle(); 9938 TypeMacros.TypenameMacros = {"LIST"}; 9939 verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros); 9940 verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros); 9941 verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros); 9942 verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros); 9943 verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros); // multiplication 9944 9945 FormatStyle CustomQualifier = getLLVMStyle(); 9946 // Add identifiers that should not be parsed as a qualifier by default. 9947 CustomQualifier.AttributeMacros.push_back("__my_qualifier"); 9948 CustomQualifier.AttributeMacros.push_back("_My_qualifier"); 9949 CustomQualifier.AttributeMacros.push_back("my_other_qualifier"); 9950 verifyFormat("vector<a * __my_qualifier> parse_as_multiply;"); 9951 verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier); 9952 verifyFormat("vector<a * _My_qualifier> parse_as_multiply;"); 9953 verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier); 9954 verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;"); 9955 verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier); 9956 verifyFormat("vector<a * _NotAQualifier> v;"); 9957 verifyFormat("vector<a * __not_a_qualifier> v;"); 9958 verifyFormat("vector<a * b> v;"); 9959 verifyFormat("foo<b && false>();"); 9960 verifyFormat("foo<b & 1>();"); 9961 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 9962 verifyFormat("typeof(*::std::declval<const T &>()) void F();"); 9963 verifyFormat("_Atomic(*::std::declval<const T &>()) void F();"); 9964 verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();"); 9965 verifyFormat( 9966 "template <class T, class = typename std::enable_if<\n" 9967 " std::is_integral<T>::value &&\n" 9968 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 9969 "void F();", 9970 getLLVMStyleWithColumns(70)); 9971 verifyFormat("template <class T,\n" 9972 " class = typename std::enable_if<\n" 9973 " std::is_integral<T>::value &&\n" 9974 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n" 9975 " class U>\n" 9976 "void F();", 9977 getLLVMStyleWithColumns(70)); 9978 verifyFormat( 9979 "template <class T,\n" 9980 " class = typename ::std::enable_if<\n" 9981 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 9982 "void F();", 9983 getGoogleStyleWithColumns(68)); 9984 9985 verifyIndependentOfContext("MACRO(int *i);"); 9986 verifyIndependentOfContext("MACRO(auto *a);"); 9987 verifyIndependentOfContext("MACRO(const A *a);"); 9988 verifyIndependentOfContext("MACRO(_Atomic(A) *a);"); 9989 verifyIndependentOfContext("MACRO(decltype(A) *a);"); 9990 verifyIndependentOfContext("MACRO(typeof(A) *a);"); 9991 verifyIndependentOfContext("MACRO(__underlying_type(A) *a);"); 9992 verifyIndependentOfContext("MACRO(A *const a);"); 9993 verifyIndependentOfContext("MACRO(A *restrict a);"); 9994 verifyIndependentOfContext("MACRO(A *__restrict__ a);"); 9995 verifyIndependentOfContext("MACRO(A *__restrict a);"); 9996 verifyIndependentOfContext("MACRO(A *volatile a);"); 9997 verifyIndependentOfContext("MACRO(A *__volatile a);"); 9998 verifyIndependentOfContext("MACRO(A *__volatile__ a);"); 9999 verifyIndependentOfContext("MACRO(A *_Nonnull a);"); 10000 verifyIndependentOfContext("MACRO(A *_Nullable a);"); 10001 verifyIndependentOfContext("MACRO(A *_Null_unspecified a);"); 10002 verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);"); 10003 verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);"); 10004 verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);"); 10005 verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);"); 10006 verifyIndependentOfContext("MACRO(A *__ptr32 a);"); 10007 verifyIndependentOfContext("MACRO(A *__ptr64 a);"); 10008 verifyIndependentOfContext("MACRO(A *__capability);"); 10009 verifyIndependentOfContext("MACRO(A &__capability);"); 10010 verifyFormat("MACRO(A *__my_qualifier);"); // type declaration 10011 verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication 10012 // If we add __my_qualifier to AttributeMacros it should always be parsed as 10013 // a type declaration: 10014 verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier); 10015 verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier); 10016 // Also check that TypenameMacros prevents parsing it as multiplication: 10017 verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication 10018 verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros); // type 10019 10020 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 10021 verifyFormat("void f() { f(float{1}, a * a); }"); 10022 verifyFormat("void f() { f(float(1), a * a); }"); 10023 10024 verifyFormat("f((void (*)(int))g);"); 10025 verifyFormat("f((void (&)(int))g);"); 10026 verifyFormat("f((void (^)(int))g);"); 10027 10028 // FIXME: Is there a way to make this work? 10029 // verifyIndependentOfContext("MACRO(A *a);"); 10030 verifyFormat("MACRO(A &B);"); 10031 verifyFormat("MACRO(A *B);"); 10032 verifyFormat("void f() { MACRO(A * B); }"); 10033 verifyFormat("void f() { MACRO(A & B); }"); 10034 10035 // This lambda was mis-formatted after D88956 (treating it as a binop): 10036 verifyFormat("auto x = [](const decltype(x) &ptr) {};"); 10037 verifyFormat("auto x = [](const decltype(x) *ptr) {};"); 10038 verifyFormat("#define lambda [](const decltype(x) &ptr) {}"); 10039 verifyFormat("#define lambda [](const decltype(x) *ptr) {}"); 10040 10041 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 10042 verifyFormat("return options != nullptr && operator==(*options);"); 10043 10044 EXPECT_EQ("#define OP(x) \\\n" 10045 " ostream &operator<<(ostream &s, const A &a) { \\\n" 10046 " return s << a.DebugString(); \\\n" 10047 " }", 10048 format("#define OP(x) \\\n" 10049 " ostream &operator<<(ostream &s, const A &a) { \\\n" 10050 " return s << a.DebugString(); \\\n" 10051 " }", 10052 getLLVMStyleWithColumns(50))); 10053 10054 // FIXME: We cannot handle this case yet; we might be able to figure out that 10055 // foo<x> d > v; doesn't make sense. 10056 verifyFormat("foo<a<b && c> d> v;"); 10057 10058 FormatStyle PointerMiddle = getLLVMStyle(); 10059 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 10060 verifyFormat("delete *x;", PointerMiddle); 10061 verifyFormat("int * x;", PointerMiddle); 10062 verifyFormat("int *[] x;", PointerMiddle); 10063 verifyFormat("template <int * y> f() {}", PointerMiddle); 10064 verifyFormat("int * f(int * a) {}", PointerMiddle); 10065 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 10066 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 10067 verifyFormat("A<int *> a;", PointerMiddle); 10068 verifyFormat("A<int **> a;", PointerMiddle); 10069 verifyFormat("A<int *, int *> a;", PointerMiddle); 10070 verifyFormat("A<int *[]> a;", PointerMiddle); 10071 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 10072 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 10073 verifyFormat("T ** t = new T *;", PointerMiddle); 10074 10075 // Member function reference qualifiers aren't binary operators. 10076 verifyFormat("string // break\n" 10077 "operator()() & {}"); 10078 verifyFormat("string // break\n" 10079 "operator()() && {}"); 10080 verifyGoogleFormat("template <typename T>\n" 10081 "auto x() & -> int {}"); 10082 10083 // Should be binary operators when used as an argument expression (overloaded 10084 // operator invoked as a member function). 10085 verifyFormat("void f() { a.operator()(a * a); }"); 10086 verifyFormat("void f() { a->operator()(a & a); }"); 10087 verifyFormat("void f() { a.operator()(*a & *a); }"); 10088 verifyFormat("void f() { a->operator()(*a * *a); }"); 10089 10090 verifyFormat("int operator()(T (&&)[N]) { return 1; }"); 10091 verifyFormat("int operator()(T (&)[N]) { return 0; }"); 10092 } 10093 10094 TEST_F(FormatTest, UnderstandsAttributes) { 10095 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 10096 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 10097 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 10098 verifyFormat("__attribute__((nodebug)) ::qualified_type f();"); 10099 FormatStyle AfterType = getLLVMStyle(); 10100 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 10101 verifyFormat("__attribute__((nodebug)) void\n" 10102 "foo() {}\n", 10103 AfterType); 10104 verifyFormat("__unused void\n" 10105 "foo() {}", 10106 AfterType); 10107 10108 FormatStyle CustomAttrs = getLLVMStyle(); 10109 CustomAttrs.AttributeMacros.push_back("__unused"); 10110 CustomAttrs.AttributeMacros.push_back("__attr1"); 10111 CustomAttrs.AttributeMacros.push_back("__attr2"); 10112 CustomAttrs.AttributeMacros.push_back("no_underscore_attr"); 10113 verifyFormat("vector<SomeType *__attribute((foo))> v;"); 10114 verifyFormat("vector<SomeType *__attribute__((foo))> v;"); 10115 verifyFormat("vector<SomeType * __not_attribute__((foo))> v;"); 10116 // Check that it is parsed as a multiplication without AttributeMacros and 10117 // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros. 10118 verifyFormat("vector<SomeType * __attr1> v;"); 10119 verifyFormat("vector<SomeType __attr1 *> v;"); 10120 verifyFormat("vector<SomeType __attr1 *const> v;"); 10121 verifyFormat("vector<SomeType __attr1 * __attr2> v;"); 10122 verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs); 10123 verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs); 10124 verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs); 10125 verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs); 10126 verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs); 10127 verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs); 10128 verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs); 10129 10130 // Check that these are not parsed as function declarations: 10131 CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 10132 CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman; 10133 verifyFormat("SomeType s(InitValue);", CustomAttrs); 10134 verifyFormat("SomeType s{InitValue};", CustomAttrs); 10135 verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs); 10136 verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs); 10137 verifyFormat("SomeType s __unused(InitValue);", CustomAttrs); 10138 verifyFormat("SomeType s __unused{InitValue};", CustomAttrs); 10139 verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs); 10140 verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs); 10141 } 10142 10143 TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) { 10144 // Check that qualifiers on pointers don't break parsing of casts. 10145 verifyFormat("x = (foo *const)*v;"); 10146 verifyFormat("x = (foo *volatile)*v;"); 10147 verifyFormat("x = (foo *restrict)*v;"); 10148 verifyFormat("x = (foo *__attribute__((foo)))*v;"); 10149 verifyFormat("x = (foo *_Nonnull)*v;"); 10150 verifyFormat("x = (foo *_Nullable)*v;"); 10151 verifyFormat("x = (foo *_Null_unspecified)*v;"); 10152 verifyFormat("x = (foo *_Nonnull)*v;"); 10153 verifyFormat("x = (foo *[[clang::attr]])*v;"); 10154 verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;"); 10155 verifyFormat("x = (foo *__ptr32)*v;"); 10156 verifyFormat("x = (foo *__ptr64)*v;"); 10157 verifyFormat("x = (foo *__capability)*v;"); 10158 10159 // Check that we handle multiple trailing qualifiers and skip them all to 10160 // determine that the expression is a cast to a pointer type. 10161 FormatStyle LongPointerRight = getLLVMStyleWithColumns(999); 10162 FormatStyle LongPointerLeft = getLLVMStyleWithColumns(999); 10163 LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left; 10164 StringRef AllQualifiers = 10165 "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified " 10166 "_Nonnull [[clang::attr]] __ptr32 __ptr64 __capability"; 10167 verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight); 10168 verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft); 10169 10170 // Also check that address-of is not parsed as a binary bitwise-and: 10171 verifyFormat("x = (foo *const)&v;"); 10172 verifyFormat(("x = (foo *" + AllQualifiers + ")&v;").str(), LongPointerRight); 10173 verifyFormat(("x = (foo* " + AllQualifiers + ")&v;").str(), LongPointerLeft); 10174 10175 // Check custom qualifiers: 10176 FormatStyle CustomQualifier = getLLVMStyleWithColumns(999); 10177 CustomQualifier.AttributeMacros.push_back("__my_qualifier"); 10178 verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier. 10179 verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier); 10180 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)*v;").str(), 10181 CustomQualifier); 10182 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)&v;").str(), 10183 CustomQualifier); 10184 10185 // Check that unknown identifiers result in binary operator parsing: 10186 verifyFormat("x = (foo * __unknown_qualifier) * v;"); 10187 verifyFormat("x = (foo * __unknown_qualifier) & v;"); 10188 } 10189 10190 TEST_F(FormatTest, UnderstandsSquareAttributes) { 10191 verifyFormat("SomeType s [[unused]] (InitValue);"); 10192 verifyFormat("SomeType s [[gnu::unused]] (InitValue);"); 10193 verifyFormat("SomeType s [[using gnu: unused]] (InitValue);"); 10194 verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}"); 10195 verifyFormat("void f() [[deprecated(\"so sorry\")]];"); 10196 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10197 " [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 10198 verifyFormat("[[nodiscard]] bool f() { return false; }"); 10199 verifyFormat("class [[nodiscard]] f {\npublic:\n f() {}\n}"); 10200 verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n f() {}\n}"); 10201 verifyFormat("class [[gnu::unused]] f {\npublic:\n f() {}\n}"); 10202 verifyFormat("[[nodiscard]] ::qualified_type f();"); 10203 10204 // Make sure we do not mistake attributes for array subscripts. 10205 verifyFormat("int a() {}\n" 10206 "[[unused]] int b() {}\n"); 10207 verifyFormat("NSArray *arr;\n" 10208 "arr[[Foo() bar]];"); 10209 10210 // On the other hand, we still need to correctly find array subscripts. 10211 verifyFormat("int a = std::vector<int>{1, 2, 3}[0];"); 10212 10213 // Make sure that we do not mistake Objective-C method inside array literals 10214 // as attributes, even if those method names are also keywords. 10215 verifyFormat("@[ [foo bar] ];"); 10216 verifyFormat("@[ [NSArray class] ];"); 10217 verifyFormat("@[ [foo enum] ];"); 10218 10219 verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }"); 10220 10221 // Make sure we do not parse attributes as lambda introducers. 10222 FormatStyle MultiLineFunctions = getLLVMStyle(); 10223 MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 10224 verifyFormat("[[unused]] int b() {\n" 10225 " return 42;\n" 10226 "}\n", 10227 MultiLineFunctions); 10228 } 10229 10230 TEST_F(FormatTest, AttributeClass) { 10231 FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp); 10232 verifyFormat("class S {\n" 10233 " S(S&&) = default;\n" 10234 "};", 10235 Style); 10236 verifyFormat("class [[nodiscard]] S {\n" 10237 " S(S&&) = default;\n" 10238 "};", 10239 Style); 10240 verifyFormat("class __attribute((maybeunused)) S {\n" 10241 " S(S&&) = default;\n" 10242 "};", 10243 Style); 10244 verifyFormat("struct S {\n" 10245 " S(S&&) = default;\n" 10246 "};", 10247 Style); 10248 verifyFormat("struct [[nodiscard]] S {\n" 10249 " S(S&&) = default;\n" 10250 "};", 10251 Style); 10252 } 10253 10254 TEST_F(FormatTest, AttributesAfterMacro) { 10255 FormatStyle Style = getLLVMStyle(); 10256 verifyFormat("MACRO;\n" 10257 "__attribute__((maybe_unused)) int foo() {\n" 10258 " //...\n" 10259 "}"); 10260 10261 verifyFormat("MACRO;\n" 10262 "[[nodiscard]] int foo() {\n" 10263 " //...\n" 10264 "}"); 10265 10266 EXPECT_EQ("MACRO\n\n" 10267 "__attribute__((maybe_unused)) int foo() {\n" 10268 " //...\n" 10269 "}", 10270 format("MACRO\n\n" 10271 "__attribute__((maybe_unused)) int foo() {\n" 10272 " //...\n" 10273 "}")); 10274 10275 EXPECT_EQ("MACRO\n\n" 10276 "[[nodiscard]] int foo() {\n" 10277 " //...\n" 10278 "}", 10279 format("MACRO\n\n" 10280 "[[nodiscard]] int foo() {\n" 10281 " //...\n" 10282 "}")); 10283 } 10284 10285 TEST_F(FormatTest, AttributePenaltyBreaking) { 10286 FormatStyle Style = getLLVMStyle(); 10287 verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n" 10288 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", 10289 Style); 10290 verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n" 10291 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", 10292 Style); 10293 verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const " 10294 "shared_ptr<ALongTypeName> &C d) {\n}", 10295 Style); 10296 } 10297 10298 TEST_F(FormatTest, UnderstandsEllipsis) { 10299 FormatStyle Style = getLLVMStyle(); 10300 verifyFormat("int printf(const char *fmt, ...);"); 10301 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 10302 verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}"); 10303 10304 verifyFormat("template <int *...PP> a;", Style); 10305 10306 Style.PointerAlignment = FormatStyle::PAS_Left; 10307 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style); 10308 10309 verifyFormat("template <int*... PP> a;", Style); 10310 10311 Style.PointerAlignment = FormatStyle::PAS_Middle; 10312 verifyFormat("template <int *... PP> a;", Style); 10313 } 10314 10315 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 10316 EXPECT_EQ("int *a;\n" 10317 "int *a;\n" 10318 "int *a;", 10319 format("int *a;\n" 10320 "int* a;\n" 10321 "int *a;", 10322 getGoogleStyle())); 10323 EXPECT_EQ("int* a;\n" 10324 "int* a;\n" 10325 "int* a;", 10326 format("int* a;\n" 10327 "int* a;\n" 10328 "int *a;", 10329 getGoogleStyle())); 10330 EXPECT_EQ("int *a;\n" 10331 "int *a;\n" 10332 "int *a;", 10333 format("int *a;\n" 10334 "int * a;\n" 10335 "int * a;", 10336 getGoogleStyle())); 10337 EXPECT_EQ("auto x = [] {\n" 10338 " int *a;\n" 10339 " int *a;\n" 10340 " int *a;\n" 10341 "};", 10342 format("auto x=[]{int *a;\n" 10343 "int * a;\n" 10344 "int * a;};", 10345 getGoogleStyle())); 10346 } 10347 10348 TEST_F(FormatTest, UnderstandsRvalueReferences) { 10349 verifyFormat("int f(int &&a) {}"); 10350 verifyFormat("int f(int a, char &&b) {}"); 10351 verifyFormat("void f() { int &&a = b; }"); 10352 verifyGoogleFormat("int f(int a, char&& b) {}"); 10353 verifyGoogleFormat("void f() { int&& a = b; }"); 10354 10355 verifyIndependentOfContext("A<int &&> a;"); 10356 verifyIndependentOfContext("A<int &&, int &&> a;"); 10357 verifyGoogleFormat("A<int&&> a;"); 10358 verifyGoogleFormat("A<int&&, int&&> a;"); 10359 10360 // Not rvalue references: 10361 verifyFormat("template <bool B, bool C> class A {\n" 10362 " static_assert(B && C, \"Something is wrong\");\n" 10363 "};"); 10364 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 10365 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 10366 verifyFormat("#define A(a, b) (a && b)"); 10367 } 10368 10369 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 10370 verifyFormat("void f() {\n" 10371 " x[aaaaaaaaa -\n" 10372 " b] = 23;\n" 10373 "}", 10374 getLLVMStyleWithColumns(15)); 10375 } 10376 10377 TEST_F(FormatTest, FormatsCasts) { 10378 verifyFormat("Type *A = static_cast<Type *>(P);"); 10379 verifyFormat("Type *A = (Type *)P;"); 10380 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 10381 verifyFormat("int a = (int)(2.0f);"); 10382 verifyFormat("int a = (int)2.0f;"); 10383 verifyFormat("x[(int32)y];"); 10384 verifyFormat("x = (int32)y;"); 10385 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 10386 verifyFormat("int a = (int)*b;"); 10387 verifyFormat("int a = (int)2.0f;"); 10388 verifyFormat("int a = (int)~0;"); 10389 verifyFormat("int a = (int)++a;"); 10390 verifyFormat("int a = (int)sizeof(int);"); 10391 verifyFormat("int a = (int)+2;"); 10392 verifyFormat("my_int a = (my_int)2.0f;"); 10393 verifyFormat("my_int a = (my_int)sizeof(int);"); 10394 verifyFormat("return (my_int)aaa;"); 10395 verifyFormat("#define x ((int)-1)"); 10396 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 10397 verifyFormat("#define p(q) ((int *)&q)"); 10398 verifyFormat("fn(a)(b) + 1;"); 10399 10400 verifyFormat("void f() { my_int a = (my_int)*b; }"); 10401 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 10402 verifyFormat("my_int a = (my_int)~0;"); 10403 verifyFormat("my_int a = (my_int)++a;"); 10404 verifyFormat("my_int a = (my_int)-2;"); 10405 verifyFormat("my_int a = (my_int)1;"); 10406 verifyFormat("my_int a = (my_int *)1;"); 10407 verifyFormat("my_int a = (const my_int)-1;"); 10408 verifyFormat("my_int a = (const my_int *)-1;"); 10409 verifyFormat("my_int a = (my_int)(my_int)-1;"); 10410 verifyFormat("my_int a = (ns::my_int)-2;"); 10411 verifyFormat("case (my_int)ONE:"); 10412 verifyFormat("auto x = (X)this;"); 10413 // Casts in Obj-C style calls used to not be recognized as such. 10414 verifyFormat("int a = [(type*)[((type*)val) arg] arg];", getGoogleStyle()); 10415 10416 // FIXME: single value wrapped with paren will be treated as cast. 10417 verifyFormat("void f(int i = (kValue)*kMask) {}"); 10418 10419 verifyFormat("{ (void)F; }"); 10420 10421 // Don't break after a cast's 10422 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 10423 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 10424 " bbbbbbbbbbbbbbbbbbbbbb);"); 10425 10426 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(x)"); 10427 verifyFormat("#define CONF_BOOL(x) (bool *)(x)"); 10428 verifyFormat("#define CONF_BOOL(x) (bool)(x)"); 10429 verifyFormat("bool *y = (bool *)(void *)(x);"); 10430 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)(x)"); 10431 verifyFormat("bool *y = (bool *)(void *)(int)(x);"); 10432 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)foo(x)"); 10433 verifyFormat("bool *y = (bool *)(void *)(int)foo(x);"); 10434 10435 // These are not casts. 10436 verifyFormat("void f(int *) {}"); 10437 verifyFormat("f(foo)->b;"); 10438 verifyFormat("f(foo).b;"); 10439 verifyFormat("f(foo)(b);"); 10440 verifyFormat("f(foo)[b];"); 10441 verifyFormat("[](foo) { return 4; }(bar);"); 10442 verifyFormat("(*funptr)(foo)[4];"); 10443 verifyFormat("funptrs[4](foo)[4];"); 10444 verifyFormat("void f(int *);"); 10445 verifyFormat("void f(int *) = 0;"); 10446 verifyFormat("void f(SmallVector<int>) {}"); 10447 verifyFormat("void f(SmallVector<int>);"); 10448 verifyFormat("void f(SmallVector<int>) = 0;"); 10449 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 10450 verifyFormat("int a = sizeof(int) * b;"); 10451 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 10452 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 10453 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 10454 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 10455 10456 // These are not casts, but at some point were confused with casts. 10457 verifyFormat("virtual void foo(int *) override;"); 10458 verifyFormat("virtual void foo(char &) const;"); 10459 verifyFormat("virtual void foo(int *a, char *) const;"); 10460 verifyFormat("int a = sizeof(int *) + b;"); 10461 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 10462 verifyFormat("bool b = f(g<int>) && c;"); 10463 verifyFormat("typedef void (*f)(int i) func;"); 10464 verifyFormat("void operator++(int) noexcept;"); 10465 verifyFormat("void operator++(int &) noexcept;"); 10466 verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t " 10467 "&) noexcept;"); 10468 verifyFormat( 10469 "void operator delete(std::size_t, const std::nothrow_t &) noexcept;"); 10470 verifyFormat("void operator delete(const std::nothrow_t &) noexcept;"); 10471 verifyFormat("void operator delete(std::nothrow_t &) noexcept;"); 10472 verifyFormat("void operator delete(nothrow_t &) noexcept;"); 10473 verifyFormat("void operator delete(foo &) noexcept;"); 10474 verifyFormat("void operator delete(foo) noexcept;"); 10475 verifyFormat("void operator delete(int) noexcept;"); 10476 verifyFormat("void operator delete(int &) noexcept;"); 10477 verifyFormat("void operator delete(int &) volatile noexcept;"); 10478 verifyFormat("void operator delete(int &) const"); 10479 verifyFormat("void operator delete(int &) = default"); 10480 verifyFormat("void operator delete(int &) = delete"); 10481 verifyFormat("void operator delete(int &) [[noreturn]]"); 10482 verifyFormat("void operator delete(int &) throw();"); 10483 verifyFormat("void operator delete(int &) throw(int);"); 10484 verifyFormat("auto operator delete(int &) -> int;"); 10485 verifyFormat("auto operator delete(int &) override"); 10486 verifyFormat("auto operator delete(int &) final"); 10487 10488 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 10489 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 10490 // FIXME: The indentation here is not ideal. 10491 verifyFormat( 10492 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10493 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 10494 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 10495 } 10496 10497 TEST_F(FormatTest, FormatsFunctionTypes) { 10498 verifyFormat("A<bool()> a;"); 10499 verifyFormat("A<SomeType()> a;"); 10500 verifyFormat("A<void (*)(int, std::string)> a;"); 10501 verifyFormat("A<void *(int)>;"); 10502 verifyFormat("void *(*a)(int *, SomeType *);"); 10503 verifyFormat("int (*func)(void *);"); 10504 verifyFormat("void f() { int (*func)(void *); }"); 10505 verifyFormat("template <class CallbackClass>\n" 10506 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 10507 10508 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 10509 verifyGoogleFormat("void* (*a)(int);"); 10510 verifyGoogleFormat( 10511 "template <class CallbackClass>\n" 10512 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 10513 10514 // Other constructs can look somewhat like function types: 10515 verifyFormat("A<sizeof(*x)> a;"); 10516 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 10517 verifyFormat("some_var = function(*some_pointer_var)[0];"); 10518 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 10519 verifyFormat("int x = f(&h)();"); 10520 verifyFormat("returnsFunction(¶m1, ¶m2)(param);"); 10521 verifyFormat("std::function<\n" 10522 " LooooooooooongTemplatedType<\n" 10523 " SomeType>*(\n" 10524 " LooooooooooooooooongType type)>\n" 10525 " function;", 10526 getGoogleStyleWithColumns(40)); 10527 } 10528 10529 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 10530 verifyFormat("A (*foo_)[6];"); 10531 verifyFormat("vector<int> (*foo_)[6];"); 10532 } 10533 10534 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 10535 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10536 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 10537 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 10538 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 10539 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10540 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 10541 10542 // Different ways of ()-initializiation. 10543 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10544 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 10545 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10546 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 10547 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10548 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 10549 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10550 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 10551 10552 // Lambdas should not confuse the variable declaration heuristic. 10553 verifyFormat("LooooooooooooooooongType\n" 10554 " variable(nullptr, [](A *a) {});", 10555 getLLVMStyleWithColumns(40)); 10556 } 10557 10558 TEST_F(FormatTest, BreaksLongDeclarations) { 10559 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 10560 " AnotherNameForTheLongType;"); 10561 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 10562 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 10563 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10564 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 10565 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 10566 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 10567 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10568 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10569 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 10570 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10571 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 10572 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10573 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 10574 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10575 verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n" 10576 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10577 verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n" 10578 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10579 verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n" 10580 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10581 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10582 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);"); 10583 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10584 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}"); 10585 FormatStyle Indented = getLLVMStyle(); 10586 Indented.IndentWrappedFunctionNames = true; 10587 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10588 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 10589 Indented); 10590 verifyFormat( 10591 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10592 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 10593 Indented); 10594 verifyFormat( 10595 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 10596 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 10597 Indented); 10598 verifyFormat( 10599 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 10600 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 10601 Indented); 10602 10603 // FIXME: Without the comment, this breaks after "(". 10604 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 10605 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 10606 getGoogleStyle()); 10607 10608 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 10609 " int LoooooooooooooooooooongParam2) {}"); 10610 verifyFormat( 10611 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 10612 " SourceLocation L, IdentifierIn *II,\n" 10613 " Type *T) {}"); 10614 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 10615 "ReallyReaaallyLongFunctionName(\n" 10616 " const std::string &SomeParameter,\n" 10617 " const SomeType<string, SomeOtherTemplateParameter>\n" 10618 " &ReallyReallyLongParameterName,\n" 10619 " const SomeType<string, SomeOtherTemplateParameter>\n" 10620 " &AnotherLongParameterName) {}"); 10621 verifyFormat("template <typename A>\n" 10622 "SomeLoooooooooooooooooooooongType<\n" 10623 " typename some_namespace::SomeOtherType<A>::Type>\n" 10624 "Function() {}"); 10625 10626 verifyGoogleFormat( 10627 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 10628 " aaaaaaaaaaaaaaaaaaaaaaa;"); 10629 verifyGoogleFormat( 10630 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 10631 " SourceLocation L) {}"); 10632 verifyGoogleFormat( 10633 "some_namespace::LongReturnType\n" 10634 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 10635 " int first_long_parameter, int second_parameter) {}"); 10636 10637 verifyGoogleFormat("template <typename T>\n" 10638 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 10639 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 10640 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 10641 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 10642 10643 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 10644 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10645 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 10646 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10647 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 10648 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 10649 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10650 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 10651 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 10652 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 10653 10654 verifyFormat("template <typename T> // Templates on own line.\n" 10655 "static int // Some comment.\n" 10656 "MyFunction(int a);", 10657 getLLVMStyle()); 10658 } 10659 10660 TEST_F(FormatTest, FormatsAccessModifiers) { 10661 FormatStyle Style = getLLVMStyle(); 10662 EXPECT_EQ(Style.EmptyLineBeforeAccessModifier, 10663 FormatStyle::ELBAMS_LogicalBlock); 10664 verifyFormat("struct foo {\n" 10665 "private:\n" 10666 " void f() {}\n" 10667 "\n" 10668 "private:\n" 10669 " int i;\n" 10670 "\n" 10671 "protected:\n" 10672 " int j;\n" 10673 "};\n", 10674 Style); 10675 verifyFormat("struct foo {\n" 10676 "private:\n" 10677 " void f() {}\n" 10678 "\n" 10679 "private:\n" 10680 " int i;\n" 10681 "\n" 10682 "protected:\n" 10683 " int j;\n" 10684 "};\n", 10685 "struct foo {\n" 10686 "private:\n" 10687 " void f() {}\n" 10688 "private:\n" 10689 " int i;\n" 10690 "protected:\n" 10691 " int j;\n" 10692 "};\n", 10693 Style); 10694 verifyFormat("struct foo { /* comment */\n" 10695 "private:\n" 10696 " int i;\n" 10697 " // comment\n" 10698 "private:\n" 10699 " int j;\n" 10700 "};\n", 10701 Style); 10702 verifyFormat("struct foo {\n" 10703 "#ifdef FOO\n" 10704 "#endif\n" 10705 "private:\n" 10706 " int i;\n" 10707 "#ifdef FOO\n" 10708 "private:\n" 10709 "#endif\n" 10710 " int j;\n" 10711 "};\n", 10712 Style); 10713 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10714 verifyFormat("struct foo {\n" 10715 "private:\n" 10716 " void f() {}\n" 10717 "private:\n" 10718 " int i;\n" 10719 "protected:\n" 10720 " int j;\n" 10721 "};\n", 10722 Style); 10723 verifyFormat("struct foo {\n" 10724 "private:\n" 10725 " void f() {}\n" 10726 "private:\n" 10727 " int i;\n" 10728 "protected:\n" 10729 " int j;\n" 10730 "};\n", 10731 "struct foo {\n" 10732 "\n" 10733 "private:\n" 10734 " void f() {}\n" 10735 "\n" 10736 "private:\n" 10737 " int i;\n" 10738 "\n" 10739 "protected:\n" 10740 " int j;\n" 10741 "};\n", 10742 Style); 10743 verifyFormat("struct foo { /* comment */\n" 10744 "private:\n" 10745 " int i;\n" 10746 " // comment\n" 10747 "private:\n" 10748 " int j;\n" 10749 "};\n", 10750 "struct foo { /* comment */\n" 10751 "\n" 10752 "private:\n" 10753 " int i;\n" 10754 " // comment\n" 10755 "\n" 10756 "private:\n" 10757 " int j;\n" 10758 "};\n", 10759 Style); 10760 verifyFormat("struct foo {\n" 10761 "#ifdef FOO\n" 10762 "#endif\n" 10763 "private:\n" 10764 " int i;\n" 10765 "#ifdef FOO\n" 10766 "private:\n" 10767 "#endif\n" 10768 " int j;\n" 10769 "};\n", 10770 "struct foo {\n" 10771 "#ifdef FOO\n" 10772 "#endif\n" 10773 "\n" 10774 "private:\n" 10775 " int i;\n" 10776 "#ifdef FOO\n" 10777 "\n" 10778 "private:\n" 10779 "#endif\n" 10780 " int j;\n" 10781 "};\n", 10782 Style); 10783 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10784 verifyFormat("struct foo {\n" 10785 "private:\n" 10786 " void f() {}\n" 10787 "\n" 10788 "private:\n" 10789 " int i;\n" 10790 "\n" 10791 "protected:\n" 10792 " int j;\n" 10793 "};\n", 10794 Style); 10795 verifyFormat("struct foo {\n" 10796 "private:\n" 10797 " void f() {}\n" 10798 "\n" 10799 "private:\n" 10800 " int i;\n" 10801 "\n" 10802 "protected:\n" 10803 " int j;\n" 10804 "};\n", 10805 "struct foo {\n" 10806 "private:\n" 10807 " void f() {}\n" 10808 "private:\n" 10809 " int i;\n" 10810 "protected:\n" 10811 " int j;\n" 10812 "};\n", 10813 Style); 10814 verifyFormat("struct foo { /* comment */\n" 10815 "private:\n" 10816 " int i;\n" 10817 " // comment\n" 10818 "\n" 10819 "private:\n" 10820 " int j;\n" 10821 "};\n", 10822 "struct foo { /* comment */\n" 10823 "private:\n" 10824 " int i;\n" 10825 " // comment\n" 10826 "\n" 10827 "private:\n" 10828 " int j;\n" 10829 "};\n", 10830 Style); 10831 verifyFormat("struct foo {\n" 10832 "#ifdef FOO\n" 10833 "#endif\n" 10834 "\n" 10835 "private:\n" 10836 " int i;\n" 10837 "#ifdef FOO\n" 10838 "\n" 10839 "private:\n" 10840 "#endif\n" 10841 " int j;\n" 10842 "};\n", 10843 "struct foo {\n" 10844 "#ifdef FOO\n" 10845 "#endif\n" 10846 "private:\n" 10847 " int i;\n" 10848 "#ifdef FOO\n" 10849 "private:\n" 10850 "#endif\n" 10851 " int j;\n" 10852 "};\n", 10853 Style); 10854 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 10855 EXPECT_EQ("struct foo {\n" 10856 "\n" 10857 "private:\n" 10858 " void f() {}\n" 10859 "\n" 10860 "private:\n" 10861 " int i;\n" 10862 "\n" 10863 "protected:\n" 10864 " int j;\n" 10865 "};\n", 10866 format("struct foo {\n" 10867 "\n" 10868 "private:\n" 10869 " void f() {}\n" 10870 "\n" 10871 "private:\n" 10872 " int i;\n" 10873 "\n" 10874 "protected:\n" 10875 " int j;\n" 10876 "};\n", 10877 Style)); 10878 verifyFormat("struct foo {\n" 10879 "private:\n" 10880 " void f() {}\n" 10881 "private:\n" 10882 " int i;\n" 10883 "protected:\n" 10884 " int j;\n" 10885 "};\n", 10886 Style); 10887 EXPECT_EQ("struct foo { /* comment */\n" 10888 "\n" 10889 "private:\n" 10890 " int i;\n" 10891 " // comment\n" 10892 "\n" 10893 "private:\n" 10894 " int j;\n" 10895 "};\n", 10896 format("struct foo { /* comment */\n" 10897 "\n" 10898 "private:\n" 10899 " int i;\n" 10900 " // comment\n" 10901 "\n" 10902 "private:\n" 10903 " int j;\n" 10904 "};\n", 10905 Style)); 10906 verifyFormat("struct foo { /* comment */\n" 10907 "private:\n" 10908 " int i;\n" 10909 " // comment\n" 10910 "private:\n" 10911 " int j;\n" 10912 "};\n", 10913 Style); 10914 EXPECT_EQ("struct foo {\n" 10915 "#ifdef FOO\n" 10916 "#endif\n" 10917 "\n" 10918 "private:\n" 10919 " int i;\n" 10920 "#ifdef FOO\n" 10921 "\n" 10922 "private:\n" 10923 "#endif\n" 10924 " int j;\n" 10925 "};\n", 10926 format("struct foo {\n" 10927 "#ifdef FOO\n" 10928 "#endif\n" 10929 "\n" 10930 "private:\n" 10931 " int i;\n" 10932 "#ifdef FOO\n" 10933 "\n" 10934 "private:\n" 10935 "#endif\n" 10936 " int j;\n" 10937 "};\n", 10938 Style)); 10939 verifyFormat("struct foo {\n" 10940 "#ifdef FOO\n" 10941 "#endif\n" 10942 "private:\n" 10943 " int i;\n" 10944 "#ifdef FOO\n" 10945 "private:\n" 10946 "#endif\n" 10947 " int j;\n" 10948 "};\n", 10949 Style); 10950 10951 FormatStyle NoEmptyLines = getLLVMStyle(); 10952 NoEmptyLines.MaxEmptyLinesToKeep = 0; 10953 verifyFormat("struct foo {\n" 10954 "private:\n" 10955 " void f() {}\n" 10956 "\n" 10957 "private:\n" 10958 " int i;\n" 10959 "\n" 10960 "public:\n" 10961 "protected:\n" 10962 " int j;\n" 10963 "};\n", 10964 NoEmptyLines); 10965 10966 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10967 verifyFormat("struct foo {\n" 10968 "private:\n" 10969 " void f() {}\n" 10970 "private:\n" 10971 " int i;\n" 10972 "public:\n" 10973 "protected:\n" 10974 " int j;\n" 10975 "};\n", 10976 NoEmptyLines); 10977 10978 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10979 verifyFormat("struct foo {\n" 10980 "private:\n" 10981 " void f() {}\n" 10982 "\n" 10983 "private:\n" 10984 " int i;\n" 10985 "\n" 10986 "public:\n" 10987 "\n" 10988 "protected:\n" 10989 " int j;\n" 10990 "};\n", 10991 NoEmptyLines); 10992 } 10993 10994 TEST_F(FormatTest, FormatsAfterAccessModifiers) { 10995 10996 FormatStyle Style = getLLVMStyle(); 10997 EXPECT_EQ(Style.EmptyLineAfterAccessModifier, FormatStyle::ELAAMS_Never); 10998 verifyFormat("struct foo {\n" 10999 "private:\n" 11000 " void f() {}\n" 11001 "\n" 11002 "private:\n" 11003 " int i;\n" 11004 "\n" 11005 "protected:\n" 11006 " int j;\n" 11007 "};\n", 11008 Style); 11009 11010 // Check if lines are removed. 11011 verifyFormat("struct foo {\n" 11012 "private:\n" 11013 " void f() {}\n" 11014 "\n" 11015 "private:\n" 11016 " int i;\n" 11017 "\n" 11018 "protected:\n" 11019 " int j;\n" 11020 "};\n", 11021 "struct foo {\n" 11022 "private:\n" 11023 "\n" 11024 " void f() {}\n" 11025 "\n" 11026 "private:\n" 11027 "\n" 11028 " int i;\n" 11029 "\n" 11030 "protected:\n" 11031 "\n" 11032 " int j;\n" 11033 "};\n", 11034 Style); 11035 11036 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11037 verifyFormat("struct foo {\n" 11038 "private:\n" 11039 "\n" 11040 " void f() {}\n" 11041 "\n" 11042 "private:\n" 11043 "\n" 11044 " int i;\n" 11045 "\n" 11046 "protected:\n" 11047 "\n" 11048 " int j;\n" 11049 "};\n", 11050 Style); 11051 11052 // Check if lines are added. 11053 verifyFormat("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 "struct foo {\n" 11067 "private:\n" 11068 " void f() {}\n" 11069 "\n" 11070 "private:\n" 11071 " int i;\n" 11072 "\n" 11073 "protected:\n" 11074 " int j;\n" 11075 "};\n", 11076 Style); 11077 11078 // Leave tests rely on the code layout, test::messUp can not be used. 11079 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 11080 Style.MaxEmptyLinesToKeep = 0u; 11081 verifyFormat("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 Style); 11092 11093 // Check if MaxEmptyLinesToKeep is respected. 11094 EXPECT_EQ("struct foo {\n" 11095 "private:\n" 11096 " void f() {}\n" 11097 "\n" 11098 "private:\n" 11099 " int i;\n" 11100 "\n" 11101 "protected:\n" 11102 " int j;\n" 11103 "};\n", 11104 format("struct foo {\n" 11105 "private:\n" 11106 "\n\n\n" 11107 " void f() {}\n" 11108 "\n" 11109 "private:\n" 11110 "\n\n\n" 11111 " int i;\n" 11112 "\n" 11113 "protected:\n" 11114 "\n\n\n" 11115 " int j;\n" 11116 "};\n", 11117 Style)); 11118 11119 Style.MaxEmptyLinesToKeep = 1u; 11120 EXPECT_EQ("struct foo {\n" 11121 "private:\n" 11122 "\n" 11123 " void f() {}\n" 11124 "\n" 11125 "private:\n" 11126 "\n" 11127 " int i;\n" 11128 "\n" 11129 "protected:\n" 11130 "\n" 11131 " int j;\n" 11132 "};\n", 11133 format("struct foo {\n" 11134 "private:\n" 11135 "\n" 11136 " void f() {}\n" 11137 "\n" 11138 "private:\n" 11139 "\n" 11140 " int i;\n" 11141 "\n" 11142 "protected:\n" 11143 "\n" 11144 " int j;\n" 11145 "};\n", 11146 Style)); 11147 // Check if no lines are kept. 11148 EXPECT_EQ("struct foo {\n" 11149 "private:\n" 11150 " void f() {}\n" 11151 "\n" 11152 "private:\n" 11153 " int i;\n" 11154 "\n" 11155 "protected:\n" 11156 " int j;\n" 11157 "};\n", 11158 format("struct foo {\n" 11159 "private:\n" 11160 " void f() {}\n" 11161 "\n" 11162 "private:\n" 11163 " int i;\n" 11164 "\n" 11165 "protected:\n" 11166 " int j;\n" 11167 "};\n", 11168 Style)); 11169 // Check if MaxEmptyLinesToKeep is respected. 11170 EXPECT_EQ("struct foo {\n" 11171 "private:\n" 11172 "\n" 11173 " void f() {}\n" 11174 "\n" 11175 "private:\n" 11176 "\n" 11177 " int i;\n" 11178 "\n" 11179 "protected:\n" 11180 "\n" 11181 " int j;\n" 11182 "};\n", 11183 format("struct foo {\n" 11184 "private:\n" 11185 "\n\n\n" 11186 " void f() {}\n" 11187 "\n" 11188 "private:\n" 11189 "\n\n\n" 11190 " int i;\n" 11191 "\n" 11192 "protected:\n" 11193 "\n\n\n" 11194 " int j;\n" 11195 "};\n", 11196 Style)); 11197 11198 Style.MaxEmptyLinesToKeep = 10u; 11199 EXPECT_EQ("struct foo {\n" 11200 "private:\n" 11201 "\n\n\n" 11202 " void f() {}\n" 11203 "\n" 11204 "private:\n" 11205 "\n\n\n" 11206 " int i;\n" 11207 "\n" 11208 "protected:\n" 11209 "\n\n\n" 11210 " int j;\n" 11211 "};\n", 11212 format("struct foo {\n" 11213 "private:\n" 11214 "\n\n\n" 11215 " void f() {}\n" 11216 "\n" 11217 "private:\n" 11218 "\n\n\n" 11219 " int i;\n" 11220 "\n" 11221 "protected:\n" 11222 "\n\n\n" 11223 " int j;\n" 11224 "};\n", 11225 Style)); 11226 11227 // Test with comments. 11228 Style = getLLVMStyle(); 11229 verifyFormat("struct foo {\n" 11230 "private:\n" 11231 " // comment\n" 11232 " void f() {}\n" 11233 "\n" 11234 "private: /* comment */\n" 11235 " int i;\n" 11236 "};\n", 11237 Style); 11238 verifyFormat("struct foo {\n" 11239 "private:\n" 11240 " // comment\n" 11241 " void f() {}\n" 11242 "\n" 11243 "private: /* comment */\n" 11244 " int i;\n" 11245 "};\n", 11246 "struct foo {\n" 11247 "private:\n" 11248 "\n" 11249 " // comment\n" 11250 " void f() {}\n" 11251 "\n" 11252 "private: /* comment */\n" 11253 "\n" 11254 " int i;\n" 11255 "};\n", 11256 Style); 11257 11258 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11259 verifyFormat("struct foo {\n" 11260 "private:\n" 11261 "\n" 11262 " // comment\n" 11263 " void f() {}\n" 11264 "\n" 11265 "private: /* comment */\n" 11266 "\n" 11267 " int i;\n" 11268 "};\n", 11269 "struct foo {\n" 11270 "private:\n" 11271 " // comment\n" 11272 " void f() {}\n" 11273 "\n" 11274 "private: /* comment */\n" 11275 " int i;\n" 11276 "};\n", 11277 Style); 11278 verifyFormat("struct foo {\n" 11279 "private:\n" 11280 "\n" 11281 " // comment\n" 11282 " void f() {}\n" 11283 "\n" 11284 "private: /* comment */\n" 11285 "\n" 11286 " int i;\n" 11287 "};\n", 11288 Style); 11289 11290 // Test with preprocessor defines. 11291 Style = getLLVMStyle(); 11292 verifyFormat("struct foo {\n" 11293 "private:\n" 11294 "#ifdef FOO\n" 11295 "#endif\n" 11296 " void f() {}\n" 11297 "};\n", 11298 Style); 11299 verifyFormat("struct foo {\n" 11300 "private:\n" 11301 "#ifdef FOO\n" 11302 "#endif\n" 11303 " void f() {}\n" 11304 "};\n", 11305 "struct foo {\n" 11306 "private:\n" 11307 "\n" 11308 "#ifdef FOO\n" 11309 "#endif\n" 11310 " void f() {}\n" 11311 "};\n", 11312 Style); 11313 11314 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11315 verifyFormat("struct foo {\n" 11316 "private:\n" 11317 "\n" 11318 "#ifdef FOO\n" 11319 "#endif\n" 11320 " void f() {}\n" 11321 "};\n", 11322 "struct foo {\n" 11323 "private:\n" 11324 "#ifdef FOO\n" 11325 "#endif\n" 11326 " void f() {}\n" 11327 "};\n", 11328 Style); 11329 verifyFormat("struct foo {\n" 11330 "private:\n" 11331 "\n" 11332 "#ifdef FOO\n" 11333 "#endif\n" 11334 " void f() {}\n" 11335 "};\n", 11336 Style); 11337 } 11338 11339 TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) { 11340 // Combined tests of EmptyLineAfterAccessModifier and 11341 // EmptyLineBeforeAccessModifier. 11342 FormatStyle Style = getLLVMStyle(); 11343 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 11344 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11345 verifyFormat("struct foo {\n" 11346 "private:\n" 11347 "\n" 11348 "protected:\n" 11349 "};\n", 11350 Style); 11351 11352 Style.MaxEmptyLinesToKeep = 10u; 11353 // Both remove all new lines. 11354 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 11355 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 11356 verifyFormat("struct foo {\n" 11357 "private:\n" 11358 "protected:\n" 11359 "};\n", 11360 "struct foo {\n" 11361 "private:\n" 11362 "\n\n\n" 11363 "protected:\n" 11364 "};\n", 11365 Style); 11366 11367 // Leave tests rely on the code layout, test::messUp can not be used. 11368 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 11369 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 11370 Style.MaxEmptyLinesToKeep = 10u; 11371 EXPECT_EQ("struct foo {\n" 11372 "private:\n" 11373 "\n\n\n" 11374 "protected:\n" 11375 "};\n", 11376 format("struct foo {\n" 11377 "private:\n" 11378 "\n\n\n" 11379 "protected:\n" 11380 "};\n", 11381 Style)); 11382 Style.MaxEmptyLinesToKeep = 3u; 11383 EXPECT_EQ("struct foo {\n" 11384 "private:\n" 11385 "\n\n\n" 11386 "protected:\n" 11387 "};\n", 11388 format("struct foo {\n" 11389 "private:\n" 11390 "\n\n\n" 11391 "protected:\n" 11392 "};\n", 11393 Style)); 11394 Style.MaxEmptyLinesToKeep = 1u; 11395 EXPECT_EQ("struct foo {\n" 11396 "private:\n" 11397 "\n\n\n" 11398 "protected:\n" 11399 "};\n", 11400 format("struct foo {\n" 11401 "private:\n" 11402 "\n\n\n" 11403 "protected:\n" 11404 "};\n", 11405 Style)); // Based on new lines in original document and not 11406 // on the setting. 11407 11408 Style.MaxEmptyLinesToKeep = 10u; 11409 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 11410 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 11411 // Newlines are kept if they are greater than zero, 11412 // test::messUp removes all new lines which changes the logic 11413 EXPECT_EQ("struct foo {\n" 11414 "private:\n" 11415 "\n\n\n" 11416 "protected:\n" 11417 "};\n", 11418 format("struct foo {\n" 11419 "private:\n" 11420 "\n\n\n" 11421 "protected:\n" 11422 "};\n", 11423 Style)); 11424 11425 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 11426 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11427 // test::messUp removes all new lines which changes the logic 11428 EXPECT_EQ("struct foo {\n" 11429 "private:\n" 11430 "\n\n\n" 11431 "protected:\n" 11432 "};\n", 11433 format("struct foo {\n" 11434 "private:\n" 11435 "\n\n\n" 11436 "protected:\n" 11437 "};\n", 11438 Style)); 11439 11440 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 11441 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 11442 EXPECT_EQ("struct foo {\n" 11443 "private:\n" 11444 "\n\n\n" 11445 "protected:\n" 11446 "};\n", 11447 format("struct foo {\n" 11448 "private:\n" 11449 "\n\n\n" 11450 "protected:\n" 11451 "};\n", 11452 Style)); // test::messUp removes all new lines which changes 11453 // the logic. 11454 11455 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 11456 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 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 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 11469 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 11470 EXPECT_EQ("struct foo {\n" 11471 "private:\n" 11472 "\n\n\n" 11473 "protected:\n" 11474 "};\n", 11475 format("struct foo {\n" 11476 "private:\n" 11477 "\n\n\n" 11478 "protected:\n" 11479 "};\n", 11480 Style)); // test::messUp removes all new lines which changes 11481 // the logic. 11482 11483 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 11484 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11485 verifyFormat("struct foo {\n" 11486 "private:\n" 11487 "protected:\n" 11488 "};\n", 11489 "struct foo {\n" 11490 "private:\n" 11491 "\n\n\n" 11492 "protected:\n" 11493 "};\n", 11494 Style); 11495 11496 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 11497 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11498 verifyFormat("struct foo {\n" 11499 "private:\n" 11500 "protected:\n" 11501 "};\n", 11502 "struct foo {\n" 11503 "private:\n" 11504 "\n\n\n" 11505 "protected:\n" 11506 "};\n", 11507 Style); 11508 11509 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 11510 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 11511 verifyFormat("struct foo {\n" 11512 "private:\n" 11513 "protected:\n" 11514 "};\n", 11515 "struct foo {\n" 11516 "private:\n" 11517 "\n\n\n" 11518 "protected:\n" 11519 "};\n", 11520 Style); 11521 11522 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 11523 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 11524 verifyFormat("struct foo {\n" 11525 "private:\n" 11526 "protected:\n" 11527 "};\n", 11528 "struct foo {\n" 11529 "private:\n" 11530 "\n\n\n" 11531 "protected:\n" 11532 "};\n", 11533 Style); 11534 } 11535 11536 TEST_F(FormatTest, FormatsArrays) { 11537 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 11538 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 11539 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 11540 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 11541 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 11542 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 11543 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11544 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 11545 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11546 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 11547 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11548 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 11549 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 11550 verifyFormat( 11551 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 11552 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 11553 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 11554 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" 11555 " .aaaaaaaaaaaaaaaaaaaaaa();"); 11556 11557 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 11558 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 11559 verifyFormat( 11560 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 11561 " .aaaaaaa[0]\n" 11562 " .aaaaaaaaaaaaaaaaaaaaaa();"); 11563 verifyFormat("a[::b::c];"); 11564 11565 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 11566 11567 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 11568 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 11569 } 11570 11571 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 11572 verifyFormat("(a)->b();"); 11573 verifyFormat("--a;"); 11574 } 11575 11576 TEST_F(FormatTest, HandlesIncludeDirectives) { 11577 verifyFormat("#include <string>\n" 11578 "#include <a/b/c.h>\n" 11579 "#include \"a/b/string\"\n" 11580 "#include \"string.h\"\n" 11581 "#include \"string.h\"\n" 11582 "#include <a-a>\n" 11583 "#include < path with space >\n" 11584 "#include_next <test.h>" 11585 "#include \"abc.h\" // this is included for ABC\n" 11586 "#include \"some long include\" // with a comment\n" 11587 "#include \"some very long include path\"\n" 11588 "#include <some/very/long/include/path>\n", 11589 getLLVMStyleWithColumns(35)); 11590 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 11591 EXPECT_EQ("#include <a>", format("#include<a>")); 11592 11593 verifyFormat("#import <string>"); 11594 verifyFormat("#import <a/b/c.h>"); 11595 verifyFormat("#import \"a/b/string\""); 11596 verifyFormat("#import \"string.h\""); 11597 verifyFormat("#import \"string.h\""); 11598 verifyFormat("#if __has_include(<strstream>)\n" 11599 "#include <strstream>\n" 11600 "#endif"); 11601 11602 verifyFormat("#define MY_IMPORT <a/b>"); 11603 11604 verifyFormat("#if __has_include(<a/b>)"); 11605 verifyFormat("#if __has_include_next(<a/b>)"); 11606 verifyFormat("#define F __has_include(<a/b>)"); 11607 verifyFormat("#define F __has_include_next(<a/b>)"); 11608 11609 // Protocol buffer definition or missing "#". 11610 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 11611 getLLVMStyleWithColumns(30)); 11612 11613 FormatStyle Style = getLLVMStyle(); 11614 Style.AlwaysBreakBeforeMultilineStrings = true; 11615 Style.ColumnLimit = 0; 11616 verifyFormat("#import \"abc.h\"", Style); 11617 11618 // But 'import' might also be a regular C++ namespace. 11619 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11620 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 11621 } 11622 11623 //===----------------------------------------------------------------------===// 11624 // Error recovery tests. 11625 //===----------------------------------------------------------------------===// 11626 11627 TEST_F(FormatTest, IncompleteParameterLists) { 11628 FormatStyle NoBinPacking = getLLVMStyle(); 11629 NoBinPacking.BinPackParameters = false; 11630 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 11631 " double *min_x,\n" 11632 " double *max_x,\n" 11633 " double *min_y,\n" 11634 " double *max_y,\n" 11635 " double *min_z,\n" 11636 " double *max_z, ) {}", 11637 NoBinPacking); 11638 } 11639 11640 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 11641 verifyFormat("void f() { return; }\n42"); 11642 verifyFormat("void f() {\n" 11643 " if (0)\n" 11644 " return;\n" 11645 "}\n" 11646 "42"); 11647 verifyFormat("void f() { return }\n42"); 11648 verifyFormat("void f() {\n" 11649 " if (0)\n" 11650 " return\n" 11651 "}\n" 11652 "42"); 11653 } 11654 11655 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 11656 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 11657 EXPECT_EQ("void f() {\n" 11658 " if (a)\n" 11659 " return\n" 11660 "}", 11661 format("void f ( ) { if ( a ) return }")); 11662 EXPECT_EQ("namespace N {\n" 11663 "void f()\n" 11664 "}", 11665 format("namespace N { void f() }")); 11666 EXPECT_EQ("namespace N {\n" 11667 "void f() {}\n" 11668 "void g()\n" 11669 "} // namespace N", 11670 format("namespace N { void f( ) { } void g( ) }")); 11671 } 11672 11673 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 11674 verifyFormat("int aaaaaaaa =\n" 11675 " // Overlylongcomment\n" 11676 " b;", 11677 getLLVMStyleWithColumns(20)); 11678 verifyFormat("function(\n" 11679 " ShortArgument,\n" 11680 " LoooooooooooongArgument);\n", 11681 getLLVMStyleWithColumns(20)); 11682 } 11683 11684 TEST_F(FormatTest, IncorrectAccessSpecifier) { 11685 verifyFormat("public:"); 11686 verifyFormat("class A {\n" 11687 "public\n" 11688 " void f() {}\n" 11689 "};"); 11690 verifyFormat("public\n" 11691 "int qwerty;"); 11692 verifyFormat("public\n" 11693 "B {}"); 11694 verifyFormat("public\n" 11695 "{}"); 11696 verifyFormat("public\n" 11697 "B { int x; }"); 11698 } 11699 11700 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 11701 verifyFormat("{"); 11702 verifyFormat("#})"); 11703 verifyNoCrash("(/**/[:!] ?[)."); 11704 } 11705 11706 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) { 11707 // Found by oss-fuzz: 11708 // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212 11709 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 11710 Style.ColumnLimit = 60; 11711 verifyNoCrash( 11712 "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20" 11713 "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20" 11714 "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a", 11715 Style); 11716 } 11717 11718 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 11719 verifyFormat("do {\n}"); 11720 verifyFormat("do {\n}\n" 11721 "f();"); 11722 verifyFormat("do {\n}\n" 11723 "wheeee(fun);"); 11724 verifyFormat("do {\n" 11725 " f();\n" 11726 "}"); 11727 } 11728 11729 TEST_F(FormatTest, IncorrectCodeMissingParens) { 11730 verifyFormat("if {\n foo;\n foo();\n}"); 11731 verifyFormat("switch {\n foo;\n foo();\n}"); 11732 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 11733 verifyFormat("while {\n foo;\n foo();\n}"); 11734 verifyFormat("do {\n foo;\n foo();\n} while;"); 11735 } 11736 11737 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 11738 verifyIncompleteFormat("namespace {\n" 11739 "class Foo { Foo (\n" 11740 "};\n" 11741 "} // namespace"); 11742 } 11743 11744 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 11745 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 11746 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 11747 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 11748 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 11749 11750 EXPECT_EQ("{\n" 11751 " {\n" 11752 " breakme(\n" 11753 " qwe);\n" 11754 " }\n", 11755 format("{\n" 11756 " {\n" 11757 " breakme(qwe);\n" 11758 "}\n", 11759 getLLVMStyleWithColumns(10))); 11760 } 11761 11762 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 11763 verifyFormat("int x = {\n" 11764 " avariable,\n" 11765 " b(alongervariable)};", 11766 getLLVMStyleWithColumns(25)); 11767 } 11768 11769 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 11770 verifyFormat("return (a)(b){1, 2, 3};"); 11771 } 11772 11773 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 11774 verifyFormat("vector<int> x{1, 2, 3, 4};"); 11775 verifyFormat("vector<int> x{\n" 11776 " 1,\n" 11777 " 2,\n" 11778 " 3,\n" 11779 " 4,\n" 11780 "};"); 11781 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 11782 verifyFormat("f({1, 2});"); 11783 verifyFormat("auto v = Foo{-1};"); 11784 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 11785 verifyFormat("Class::Class : member{1, 2, 3} {}"); 11786 verifyFormat("new vector<int>{1, 2, 3};"); 11787 verifyFormat("new int[3]{1, 2, 3};"); 11788 verifyFormat("new int{1};"); 11789 verifyFormat("return {arg1, arg2};"); 11790 verifyFormat("return {arg1, SomeType{parameter}};"); 11791 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 11792 verifyFormat("new T{arg1, arg2};"); 11793 verifyFormat("f(MyMap[{composite, key}]);"); 11794 verifyFormat("class Class {\n" 11795 " T member = {arg1, arg2};\n" 11796 "};"); 11797 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 11798 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 11799 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 11800 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 11801 verifyFormat("int a = std::is_integral<int>{} + 0;"); 11802 11803 verifyFormat("int foo(int i) { return fo1{}(i); }"); 11804 verifyFormat("int foo(int i) { return fo1{}(i); }"); 11805 verifyFormat("auto i = decltype(x){};"); 11806 verifyFormat("auto i = typeof(x){};"); 11807 verifyFormat("auto i = _Atomic(x){};"); 11808 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 11809 verifyFormat("Node n{1, Node{1000}, //\n" 11810 " 2};"); 11811 verifyFormat("Aaaa aaaaaaa{\n" 11812 " {\n" 11813 " aaaa,\n" 11814 " },\n" 11815 "};"); 11816 verifyFormat("class C : public D {\n" 11817 " SomeClass SC{2};\n" 11818 "};"); 11819 verifyFormat("class C : public A {\n" 11820 " class D : public B {\n" 11821 " void f() { int i{2}; }\n" 11822 " };\n" 11823 "};"); 11824 verifyFormat("#define A {a, a},"); 11825 // Don't confuse braced list initializers with compound statements. 11826 verifyFormat( 11827 "class A {\n" 11828 " A() : a{} {}\n" 11829 " A(int b) : b(b) {}\n" 11830 " A(int a, int b) : a(a), bs{{bs...}} { f(); }\n" 11831 " int a, b;\n" 11832 " explicit Expr(const Scalar<Result> &x) : u{Constant<Result>{x}} {}\n" 11833 " explicit Expr(Scalar<Result> &&x) : u{Constant<Result>{std::move(x)}} " 11834 "{}\n" 11835 "};"); 11836 11837 // Avoid breaking between equal sign and opening brace 11838 FormatStyle AvoidBreakingFirstArgument = getLLVMStyle(); 11839 AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200; 11840 verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n" 11841 " {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n" 11842 " {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n" 11843 " {\"ccccccccccccccccccccc\", 2}};", 11844 AvoidBreakingFirstArgument); 11845 11846 // Binpacking only if there is no trailing comma 11847 verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n" 11848 " cccccccccc, dddddddddd};", 11849 getLLVMStyleWithColumns(50)); 11850 verifyFormat("const Aaaaaa aaaaa = {\n" 11851 " aaaaaaaaaaa,\n" 11852 " bbbbbbbbbbb,\n" 11853 " ccccccccccc,\n" 11854 " ddddddddddd,\n" 11855 "};", 11856 getLLVMStyleWithColumns(50)); 11857 11858 // Cases where distinguising braced lists and blocks is hard. 11859 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);"); 11860 verifyFormat("void f() {\n" 11861 " return; // comment\n" 11862 "}\n" 11863 "SomeType t;"); 11864 verifyFormat("void f() {\n" 11865 " if (a) {\n" 11866 " f();\n" 11867 " }\n" 11868 "}\n" 11869 "SomeType t;"); 11870 11871 // In combination with BinPackArguments = false. 11872 FormatStyle NoBinPacking = getLLVMStyle(); 11873 NoBinPacking.BinPackArguments = false; 11874 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 11875 " bbbbb,\n" 11876 " ccccc,\n" 11877 " ddddd,\n" 11878 " eeeee,\n" 11879 " ffffff,\n" 11880 " ggggg,\n" 11881 " hhhhhh,\n" 11882 " iiiiii,\n" 11883 " jjjjjj,\n" 11884 " kkkkkk};", 11885 NoBinPacking); 11886 verifyFormat("const Aaaaaa aaaaa = {\n" 11887 " aaaaa,\n" 11888 " bbbbb,\n" 11889 " ccccc,\n" 11890 " ddddd,\n" 11891 " eeeee,\n" 11892 " ffffff,\n" 11893 " ggggg,\n" 11894 " hhhhhh,\n" 11895 " iiiiii,\n" 11896 " jjjjjj,\n" 11897 " kkkkkk,\n" 11898 "};", 11899 NoBinPacking); 11900 verifyFormat( 11901 "const Aaaaaa aaaaa = {\n" 11902 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 11903 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 11904 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 11905 "};", 11906 NoBinPacking); 11907 11908 NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 11909 EXPECT_EQ("static uint8 CddDp83848Reg[] = {\n" 11910 " CDDDP83848_BMCR_REGISTER,\n" 11911 " CDDDP83848_BMSR_REGISTER,\n" 11912 " CDDDP83848_RBR_REGISTER};", 11913 format("static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n" 11914 " CDDDP83848_BMSR_REGISTER,\n" 11915 " CDDDP83848_RBR_REGISTER};", 11916 NoBinPacking)); 11917 11918 // FIXME: The alignment of these trailing comments might be bad. Then again, 11919 // this might be utterly useless in real code. 11920 verifyFormat("Constructor::Constructor()\n" 11921 " : some_value{ //\n" 11922 " aaaaaaa, //\n" 11923 " bbbbbbb} {}"); 11924 11925 // In braced lists, the first comment is always assumed to belong to the 11926 // first element. Thus, it can be moved to the next or previous line as 11927 // appropriate. 11928 EXPECT_EQ("function({// First element:\n" 11929 " 1,\n" 11930 " // Second element:\n" 11931 " 2});", 11932 format("function({\n" 11933 " // First element:\n" 11934 " 1,\n" 11935 " // Second element:\n" 11936 " 2});")); 11937 EXPECT_EQ("std::vector<int> MyNumbers{\n" 11938 " // First element:\n" 11939 " 1,\n" 11940 " // Second element:\n" 11941 " 2};", 11942 format("std::vector<int> MyNumbers{// First element:\n" 11943 " 1,\n" 11944 " // Second element:\n" 11945 " 2};", 11946 getLLVMStyleWithColumns(30))); 11947 // A trailing comma should still lead to an enforced line break and no 11948 // binpacking. 11949 EXPECT_EQ("vector<int> SomeVector = {\n" 11950 " // aaa\n" 11951 " 1,\n" 11952 " 2,\n" 11953 "};", 11954 format("vector<int> SomeVector = { // aaa\n" 11955 " 1, 2, };")); 11956 11957 // C++11 brace initializer list l-braces should not be treated any differently 11958 // when breaking before lambda bodies is enabled 11959 FormatStyle BreakBeforeLambdaBody = getLLVMStyle(); 11960 BreakBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 11961 BreakBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 11962 BreakBeforeLambdaBody.AlwaysBreakBeforeMultilineStrings = true; 11963 verifyFormat( 11964 "std::runtime_error{\n" 11965 " \"Long string which will force a break onto the next line...\"};", 11966 BreakBeforeLambdaBody); 11967 11968 FormatStyle ExtraSpaces = getLLVMStyle(); 11969 ExtraSpaces.Cpp11BracedListStyle = false; 11970 ExtraSpaces.ColumnLimit = 75; 11971 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 11972 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 11973 verifyFormat("f({ 1, 2 });", ExtraSpaces); 11974 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 11975 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 11976 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 11977 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 11978 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 11979 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 11980 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 11981 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 11982 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 11983 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 11984 verifyFormat("class Class {\n" 11985 " T member = { arg1, arg2 };\n" 11986 "};", 11987 ExtraSpaces); 11988 verifyFormat( 11989 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11990 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 11991 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 11992 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 11993 ExtraSpaces); 11994 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 11995 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 11996 ExtraSpaces); 11997 verifyFormat( 11998 "someFunction(OtherParam,\n" 11999 " BracedList{ // comment 1 (Forcing interesting break)\n" 12000 " param1, param2,\n" 12001 " // comment 2\n" 12002 " param3, param4 });", 12003 ExtraSpaces); 12004 verifyFormat( 12005 "std::this_thread::sleep_for(\n" 12006 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 12007 ExtraSpaces); 12008 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n" 12009 " aaaaaaa,\n" 12010 " aaaaaaaaaa,\n" 12011 " aaaaa,\n" 12012 " aaaaaaaaaaaaaaa,\n" 12013 " aaa,\n" 12014 " aaaaaaaaaa,\n" 12015 " a,\n" 12016 " aaaaaaaaaaaaaaaaaaaaa,\n" 12017 " aaaaaaaaaaaa,\n" 12018 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 12019 " aaaaaaa,\n" 12020 " a};"); 12021 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 12022 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces); 12023 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces); 12024 12025 // Avoid breaking between initializer/equal sign and opening brace 12026 ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200; 12027 verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n" 12028 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 12029 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 12030 " { \"ccccccccccccccccccccc\", 2 }\n" 12031 "};", 12032 ExtraSpaces); 12033 verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n" 12034 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 12035 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 12036 " { \"ccccccccccccccccccccc\", 2 }\n" 12037 "};", 12038 ExtraSpaces); 12039 12040 FormatStyle SpaceBeforeBrace = getLLVMStyle(); 12041 SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true; 12042 verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace); 12043 verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace); 12044 12045 FormatStyle SpaceBetweenBraces = getLLVMStyle(); 12046 SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always; 12047 SpaceBetweenBraces.SpacesInParentheses = true; 12048 SpaceBetweenBraces.SpacesInSquareBrackets = true; 12049 verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces); 12050 verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces); 12051 verifyFormat("vector< int > x{ // comment 1\n" 12052 " 1, 2, 3, 4 };", 12053 SpaceBetweenBraces); 12054 SpaceBetweenBraces.ColumnLimit = 20; 12055 EXPECT_EQ("vector< int > x{\n" 12056 " 1, 2, 3, 4 };", 12057 format("vector<int>x{1,2,3,4};", SpaceBetweenBraces)); 12058 SpaceBetweenBraces.ColumnLimit = 24; 12059 EXPECT_EQ("vector< int > x{ 1, 2,\n" 12060 " 3, 4 };", 12061 format("vector<int>x{1,2,3,4};", SpaceBetweenBraces)); 12062 EXPECT_EQ("vector< int > x{\n" 12063 " 1,\n" 12064 " 2,\n" 12065 " 3,\n" 12066 " 4,\n" 12067 "};", 12068 format("vector<int>x{1,2,3,4,};", SpaceBetweenBraces)); 12069 verifyFormat("vector< int > x{};", SpaceBetweenBraces); 12070 SpaceBetweenBraces.SpaceInEmptyParentheses = true; 12071 verifyFormat("vector< int > x{ };", SpaceBetweenBraces); 12072 } 12073 12074 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 12075 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12076 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12077 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12078 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12079 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12080 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 12081 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 12082 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12083 " 1, 22, 333, 4444, 55555, //\n" 12084 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12085 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 12086 verifyFormat( 12087 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12088 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12089 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 12090 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 12091 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 12092 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 12093 " 7777777};"); 12094 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 12095 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 12096 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 12097 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 12098 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 12099 " // Separating comment.\n" 12100 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 12101 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 12102 " // Leading comment\n" 12103 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 12104 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 12105 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 12106 " 1, 1, 1, 1};", 12107 getLLVMStyleWithColumns(39)); 12108 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 12109 " 1, 1, 1, 1};", 12110 getLLVMStyleWithColumns(38)); 12111 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 12112 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 12113 getLLVMStyleWithColumns(43)); 12114 verifyFormat( 12115 "static unsigned SomeValues[10][3] = {\n" 12116 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 12117 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 12118 verifyFormat("static auto fields = new vector<string>{\n" 12119 " \"aaaaaaaaaaaaa\",\n" 12120 " \"aaaaaaaaaaaaa\",\n" 12121 " \"aaaaaaaaaaaa\",\n" 12122 " \"aaaaaaaaaaaaaa\",\n" 12123 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 12124 " \"aaaaaaaaaaaa\",\n" 12125 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 12126 "};"); 12127 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 12128 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 12129 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 12130 " 3, cccccccccccccccccccccc};", 12131 getLLVMStyleWithColumns(60)); 12132 12133 // Trailing commas. 12134 verifyFormat("vector<int> x = {\n" 12135 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 12136 "};", 12137 getLLVMStyleWithColumns(39)); 12138 verifyFormat("vector<int> x = {\n" 12139 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 12140 "};", 12141 getLLVMStyleWithColumns(39)); 12142 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 12143 " 1, 1, 1, 1,\n" 12144 " /**/ /**/};", 12145 getLLVMStyleWithColumns(39)); 12146 12147 // Trailing comment in the first line. 12148 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 12149 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 12150 " 111111111, 222222222, 3333333333, 444444444, //\n" 12151 " 11111111, 22222222, 333333333, 44444444};"); 12152 // Trailing comment in the last line. 12153 verifyFormat("int aaaaa[] = {\n" 12154 " 1, 2, 3, // comment\n" 12155 " 4, 5, 6 // comment\n" 12156 "};"); 12157 12158 // With nested lists, we should either format one item per line or all nested 12159 // lists one on line. 12160 // FIXME: For some nested lists, we can do better. 12161 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 12162 " {aaaaaaaaaaaaaaaaaaa},\n" 12163 " {aaaaaaaaaaaaaaaaaaaaa},\n" 12164 " {aaaaaaaaaaaaaaaaa}};", 12165 getLLVMStyleWithColumns(60)); 12166 verifyFormat( 12167 "SomeStruct my_struct_array = {\n" 12168 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 12169 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 12170 " {aaa, aaa},\n" 12171 " {aaa, aaa},\n" 12172 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 12173 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 12174 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 12175 12176 // No column layout should be used here. 12177 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 12178 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 12179 12180 verifyNoCrash("a<,"); 12181 12182 // No braced initializer here. 12183 verifyFormat("void f() {\n" 12184 " struct Dummy {};\n" 12185 " f(v);\n" 12186 "}"); 12187 12188 // Long lists should be formatted in columns even if they are nested. 12189 verifyFormat( 12190 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12191 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12192 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12193 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12194 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 12195 " 1, 22, 333, 4444, 55555, 666666, 7777777});"); 12196 12197 // Allow "single-column" layout even if that violates the column limit. There 12198 // isn't going to be a better way. 12199 verifyFormat("std::vector<int> a = {\n" 12200 " aaaaaaaa,\n" 12201 " aaaaaaaa,\n" 12202 " aaaaaaaa,\n" 12203 " aaaaaaaa,\n" 12204 " aaaaaaaaaa,\n" 12205 " aaaaaaaa,\n" 12206 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};", 12207 getLLVMStyleWithColumns(30)); 12208 verifyFormat("vector<int> aaaa = {\n" 12209 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 12210 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 12211 " aaaaaa.aaaaaaa,\n" 12212 " aaaaaa.aaaaaaa,\n" 12213 " aaaaaa.aaaaaaa,\n" 12214 " aaaaaa.aaaaaaa,\n" 12215 "};"); 12216 12217 // Don't create hanging lists. 12218 verifyFormat("someFunction(Param, {List1, List2,\n" 12219 " List3});", 12220 getLLVMStyleWithColumns(35)); 12221 verifyFormat("someFunction(Param, Param,\n" 12222 " {List1, List2,\n" 12223 " List3});", 12224 getLLVMStyleWithColumns(35)); 12225 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n" 12226 " aaaaaaaaaaaaaaaaaaaaaaa);"); 12227 } 12228 12229 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 12230 FormatStyle DoNotMerge = getLLVMStyle(); 12231 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 12232 12233 verifyFormat("void f() { return 42; }"); 12234 verifyFormat("void f() {\n" 12235 " return 42;\n" 12236 "}", 12237 DoNotMerge); 12238 verifyFormat("void f() {\n" 12239 " // Comment\n" 12240 "}"); 12241 verifyFormat("{\n" 12242 "#error {\n" 12243 " int a;\n" 12244 "}"); 12245 verifyFormat("{\n" 12246 " int a;\n" 12247 "#error {\n" 12248 "}"); 12249 verifyFormat("void f() {} // comment"); 12250 verifyFormat("void f() { int a; } // comment"); 12251 verifyFormat("void f() {\n" 12252 "} // comment", 12253 DoNotMerge); 12254 verifyFormat("void f() {\n" 12255 " int a;\n" 12256 "} // comment", 12257 DoNotMerge); 12258 verifyFormat("void f() {\n" 12259 "} // comment", 12260 getLLVMStyleWithColumns(15)); 12261 12262 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 12263 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 12264 12265 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 12266 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 12267 verifyFormat("class C {\n" 12268 " C()\n" 12269 " : iiiiiiii(nullptr),\n" 12270 " kkkkkkk(nullptr),\n" 12271 " mmmmmmm(nullptr),\n" 12272 " nnnnnnn(nullptr) {}\n" 12273 "};", 12274 getGoogleStyle()); 12275 12276 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 12277 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 12278 EXPECT_EQ("class C {\n" 12279 " A() : b(0) {}\n" 12280 "};", 12281 format("class C{A():b(0){}};", NoColumnLimit)); 12282 EXPECT_EQ("A()\n" 12283 " : b(0) {\n" 12284 "}", 12285 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 12286 12287 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 12288 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 12289 FormatStyle::SFS_None; 12290 EXPECT_EQ("A()\n" 12291 " : b(0) {\n" 12292 "}", 12293 format("A():b(0){}", DoNotMergeNoColumnLimit)); 12294 EXPECT_EQ("A()\n" 12295 " : b(0) {\n" 12296 "}", 12297 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 12298 12299 verifyFormat("#define A \\\n" 12300 " void f() { \\\n" 12301 " int i; \\\n" 12302 " }", 12303 getLLVMStyleWithColumns(20)); 12304 verifyFormat("#define A \\\n" 12305 " void f() { int i; }", 12306 getLLVMStyleWithColumns(21)); 12307 verifyFormat("#define A \\\n" 12308 " void f() { \\\n" 12309 " int i; \\\n" 12310 " } \\\n" 12311 " int j;", 12312 getLLVMStyleWithColumns(22)); 12313 verifyFormat("#define A \\\n" 12314 " void f() { int i; } \\\n" 12315 " int j;", 12316 getLLVMStyleWithColumns(23)); 12317 } 12318 12319 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) { 12320 FormatStyle MergeEmptyOnly = getLLVMStyle(); 12321 MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 12322 verifyFormat("class C {\n" 12323 " int f() {}\n" 12324 "};", 12325 MergeEmptyOnly); 12326 verifyFormat("class C {\n" 12327 " int f() {\n" 12328 " return 42;\n" 12329 " }\n" 12330 "};", 12331 MergeEmptyOnly); 12332 verifyFormat("int f() {}", MergeEmptyOnly); 12333 verifyFormat("int f() {\n" 12334 " return 42;\n" 12335 "}", 12336 MergeEmptyOnly); 12337 12338 // Also verify behavior when BraceWrapping.AfterFunction = true 12339 MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 12340 MergeEmptyOnly.BraceWrapping.AfterFunction = true; 12341 verifyFormat("int f() {}", MergeEmptyOnly); 12342 verifyFormat("class C {\n" 12343 " int f() {}\n" 12344 "};", 12345 MergeEmptyOnly); 12346 } 12347 12348 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 12349 FormatStyle MergeInlineOnly = getLLVMStyle(); 12350 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 12351 verifyFormat("class C {\n" 12352 " int f() { return 42; }\n" 12353 "};", 12354 MergeInlineOnly); 12355 verifyFormat("int f() {\n" 12356 " return 42;\n" 12357 "}", 12358 MergeInlineOnly); 12359 12360 // SFS_Inline implies SFS_Empty 12361 verifyFormat("class C {\n" 12362 " int f() {}\n" 12363 "};", 12364 MergeInlineOnly); 12365 verifyFormat("int f() {}", MergeInlineOnly); 12366 12367 // Also verify behavior when BraceWrapping.AfterFunction = true 12368 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 12369 MergeInlineOnly.BraceWrapping.AfterFunction = true; 12370 verifyFormat("class C {\n" 12371 " int f() { return 42; }\n" 12372 "};", 12373 MergeInlineOnly); 12374 verifyFormat("int f()\n" 12375 "{\n" 12376 " return 42;\n" 12377 "}", 12378 MergeInlineOnly); 12379 12380 // SFS_Inline implies SFS_Empty 12381 verifyFormat("int f() {}", MergeInlineOnly); 12382 verifyFormat("class C {\n" 12383 " int f() {}\n" 12384 "};", 12385 MergeInlineOnly); 12386 12387 MergeInlineOnly.BraceWrapping.AfterClass = true; 12388 MergeInlineOnly.BraceWrapping.AfterStruct = true; 12389 verifyFormat("class C\n" 12390 "{\n" 12391 " int f() { return 42; }\n" 12392 "};", 12393 MergeInlineOnly); 12394 verifyFormat("struct C\n" 12395 "{\n" 12396 " int f() { return 42; }\n" 12397 "};", 12398 MergeInlineOnly); 12399 verifyFormat("int f()\n" 12400 "{\n" 12401 " return 42;\n" 12402 "}", 12403 MergeInlineOnly); 12404 verifyFormat("int f() {}", MergeInlineOnly); 12405 verifyFormat("class C\n" 12406 "{\n" 12407 " int f() { return 42; }\n" 12408 "};", 12409 MergeInlineOnly); 12410 verifyFormat("struct C\n" 12411 "{\n" 12412 " int f() { return 42; }\n" 12413 "};", 12414 MergeInlineOnly); 12415 verifyFormat("struct C\n" 12416 "// comment\n" 12417 "/* comment */\n" 12418 "// comment\n" 12419 "{\n" 12420 " int f() { return 42; }\n" 12421 "};", 12422 MergeInlineOnly); 12423 verifyFormat("/* comment */ struct C\n" 12424 "{\n" 12425 " int f() { return 42; }\n" 12426 "};", 12427 MergeInlineOnly); 12428 } 12429 12430 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) { 12431 FormatStyle MergeInlineOnly = getLLVMStyle(); 12432 MergeInlineOnly.AllowShortFunctionsOnASingleLine = 12433 FormatStyle::SFS_InlineOnly; 12434 verifyFormat("class C {\n" 12435 " int f() { return 42; }\n" 12436 "};", 12437 MergeInlineOnly); 12438 verifyFormat("int f() {\n" 12439 " return 42;\n" 12440 "}", 12441 MergeInlineOnly); 12442 12443 // SFS_InlineOnly does not imply SFS_Empty 12444 verifyFormat("class C {\n" 12445 " int f() {}\n" 12446 "};", 12447 MergeInlineOnly); 12448 verifyFormat("int f() {\n" 12449 "}", 12450 MergeInlineOnly); 12451 12452 // Also verify behavior when BraceWrapping.AfterFunction = true 12453 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 12454 MergeInlineOnly.BraceWrapping.AfterFunction = true; 12455 verifyFormat("class C {\n" 12456 " int f() { return 42; }\n" 12457 "};", 12458 MergeInlineOnly); 12459 verifyFormat("int f()\n" 12460 "{\n" 12461 " return 42;\n" 12462 "}", 12463 MergeInlineOnly); 12464 12465 // SFS_InlineOnly does not imply SFS_Empty 12466 verifyFormat("int f()\n" 12467 "{\n" 12468 "}", 12469 MergeInlineOnly); 12470 verifyFormat("class C {\n" 12471 " int f() {}\n" 12472 "};", 12473 MergeInlineOnly); 12474 } 12475 12476 TEST_F(FormatTest, SplitEmptyFunction) { 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 = false; 12482 12483 verifyFormat("int f()\n" 12484 "{}", 12485 Style); 12486 verifyFormat("int f()\n" 12487 "{\n" 12488 " return 42;\n" 12489 "}", 12490 Style); 12491 verifyFormat("int f()\n" 12492 "{\n" 12493 " // some comment\n" 12494 "}", 12495 Style); 12496 12497 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 12498 verifyFormat("int f() {}", Style); 12499 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12500 "{}", 12501 Style); 12502 verifyFormat("int f()\n" 12503 "{\n" 12504 " return 0;\n" 12505 "}", 12506 Style); 12507 12508 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 12509 verifyFormat("class Foo {\n" 12510 " int f() {}\n" 12511 "};\n", 12512 Style); 12513 verifyFormat("class Foo {\n" 12514 " int f() { return 0; }\n" 12515 "};\n", 12516 Style); 12517 verifyFormat("class Foo {\n" 12518 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12519 " {}\n" 12520 "};\n", 12521 Style); 12522 verifyFormat("class Foo {\n" 12523 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12524 " {\n" 12525 " return 0;\n" 12526 " }\n" 12527 "};\n", 12528 Style); 12529 12530 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 12531 verifyFormat("int f() {}", Style); 12532 verifyFormat("int f() { return 0; }", Style); 12533 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12534 "{}", 12535 Style); 12536 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12537 "{\n" 12538 " return 0;\n" 12539 "}", 12540 Style); 12541 } 12542 12543 TEST_F(FormatTest, SplitEmptyFunctionButNotRecord) { 12544 FormatStyle Style = getLLVMStyleWithColumns(40); 12545 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 12546 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12547 Style.BraceWrapping.AfterFunction = true; 12548 Style.BraceWrapping.SplitEmptyFunction = true; 12549 Style.BraceWrapping.SplitEmptyRecord = false; 12550 12551 verifyFormat("class C {};", Style); 12552 verifyFormat("struct C {};", Style); 12553 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 12554 " int bbbbbbbbbbbbbbbbbbbbbbbb)\n" 12555 "{\n" 12556 "}", 12557 Style); 12558 verifyFormat("class C {\n" 12559 " C()\n" 12560 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa(),\n" 12561 " bbbbbbbbbbbbbbbbbbb()\n" 12562 " {\n" 12563 " }\n" 12564 " void\n" 12565 " m(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 12566 " int bbbbbbbbbbbbbbbbbbbbbbbb)\n" 12567 " {\n" 12568 " }\n" 12569 "};", 12570 Style); 12571 } 12572 12573 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) { 12574 FormatStyle Style = getLLVMStyle(); 12575 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 12576 verifyFormat("#ifdef A\n" 12577 "int f() {}\n" 12578 "#else\n" 12579 "int g() {}\n" 12580 "#endif", 12581 Style); 12582 } 12583 12584 TEST_F(FormatTest, SplitEmptyClass) { 12585 FormatStyle Style = getLLVMStyle(); 12586 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12587 Style.BraceWrapping.AfterClass = true; 12588 Style.BraceWrapping.SplitEmptyRecord = false; 12589 12590 verifyFormat("class Foo\n" 12591 "{};", 12592 Style); 12593 verifyFormat("/* something */ class Foo\n" 12594 "{};", 12595 Style); 12596 verifyFormat("template <typename X> class Foo\n" 12597 "{};", 12598 Style); 12599 verifyFormat("class Foo\n" 12600 "{\n" 12601 " Foo();\n" 12602 "};", 12603 Style); 12604 verifyFormat("typedef class Foo\n" 12605 "{\n" 12606 "} Foo_t;", 12607 Style); 12608 12609 Style.BraceWrapping.SplitEmptyRecord = true; 12610 Style.BraceWrapping.AfterStruct = true; 12611 verifyFormat("class rep\n" 12612 "{\n" 12613 "};", 12614 Style); 12615 verifyFormat("struct rep\n" 12616 "{\n" 12617 "};", 12618 Style); 12619 verifyFormat("template <typename T> class rep\n" 12620 "{\n" 12621 "};", 12622 Style); 12623 verifyFormat("template <typename T> struct rep\n" 12624 "{\n" 12625 "};", 12626 Style); 12627 verifyFormat("class rep\n" 12628 "{\n" 12629 " int x;\n" 12630 "};", 12631 Style); 12632 verifyFormat("struct rep\n" 12633 "{\n" 12634 " int x;\n" 12635 "};", 12636 Style); 12637 verifyFormat("template <typename T> class rep\n" 12638 "{\n" 12639 " int x;\n" 12640 "};", 12641 Style); 12642 verifyFormat("template <typename T> struct rep\n" 12643 "{\n" 12644 " int x;\n" 12645 "};", 12646 Style); 12647 verifyFormat("template <typename T> class rep // Foo\n" 12648 "{\n" 12649 " int x;\n" 12650 "};", 12651 Style); 12652 verifyFormat("template <typename T> struct rep // Bar\n" 12653 "{\n" 12654 " int x;\n" 12655 "};", 12656 Style); 12657 12658 verifyFormat("template <typename T> class rep<T>\n" 12659 "{\n" 12660 " int x;\n" 12661 "};", 12662 Style); 12663 12664 verifyFormat("template <typename T> class rep<std::complex<T>>\n" 12665 "{\n" 12666 " int x;\n" 12667 "};", 12668 Style); 12669 verifyFormat("template <typename T> class rep<std::complex<T>>\n" 12670 "{\n" 12671 "};", 12672 Style); 12673 12674 verifyFormat("#include \"stdint.h\"\n" 12675 "namespace rep {}", 12676 Style); 12677 verifyFormat("#include <stdint.h>\n" 12678 "namespace rep {}", 12679 Style); 12680 verifyFormat("#include <stdint.h>\n" 12681 "namespace rep {}", 12682 "#include <stdint.h>\n" 12683 "namespace rep {\n" 12684 "\n" 12685 "\n" 12686 "}", 12687 Style); 12688 } 12689 12690 TEST_F(FormatTest, SplitEmptyStruct) { 12691 FormatStyle Style = getLLVMStyle(); 12692 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12693 Style.BraceWrapping.AfterStruct = true; 12694 Style.BraceWrapping.SplitEmptyRecord = false; 12695 12696 verifyFormat("struct Foo\n" 12697 "{};", 12698 Style); 12699 verifyFormat("/* something */ struct Foo\n" 12700 "{};", 12701 Style); 12702 verifyFormat("template <typename X> struct Foo\n" 12703 "{};", 12704 Style); 12705 verifyFormat("struct Foo\n" 12706 "{\n" 12707 " Foo();\n" 12708 "};", 12709 Style); 12710 verifyFormat("typedef struct Foo\n" 12711 "{\n" 12712 "} Foo_t;", 12713 Style); 12714 // typedef struct Bar {} Bar_t; 12715 } 12716 12717 TEST_F(FormatTest, SplitEmptyUnion) { 12718 FormatStyle Style = getLLVMStyle(); 12719 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12720 Style.BraceWrapping.AfterUnion = true; 12721 Style.BraceWrapping.SplitEmptyRecord = false; 12722 12723 verifyFormat("union Foo\n" 12724 "{};", 12725 Style); 12726 verifyFormat("/* something */ union Foo\n" 12727 "{};", 12728 Style); 12729 verifyFormat("union Foo\n" 12730 "{\n" 12731 " A,\n" 12732 "};", 12733 Style); 12734 verifyFormat("typedef union Foo\n" 12735 "{\n" 12736 "} Foo_t;", 12737 Style); 12738 } 12739 12740 TEST_F(FormatTest, SplitEmptyNamespace) { 12741 FormatStyle Style = getLLVMStyle(); 12742 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12743 Style.BraceWrapping.AfterNamespace = true; 12744 Style.BraceWrapping.SplitEmptyNamespace = false; 12745 12746 verifyFormat("namespace Foo\n" 12747 "{};", 12748 Style); 12749 verifyFormat("/* something */ namespace Foo\n" 12750 "{};", 12751 Style); 12752 verifyFormat("inline namespace Foo\n" 12753 "{};", 12754 Style); 12755 verifyFormat("/* something */ inline namespace Foo\n" 12756 "{};", 12757 Style); 12758 verifyFormat("export namespace Foo\n" 12759 "{};", 12760 Style); 12761 verifyFormat("namespace Foo\n" 12762 "{\n" 12763 "void Bar();\n" 12764 "};", 12765 Style); 12766 } 12767 12768 TEST_F(FormatTest, NeverMergeShortRecords) { 12769 FormatStyle Style = getLLVMStyle(); 12770 12771 verifyFormat("class Foo {\n" 12772 " Foo();\n" 12773 "};", 12774 Style); 12775 verifyFormat("typedef class Foo {\n" 12776 " Foo();\n" 12777 "} Foo_t;", 12778 Style); 12779 verifyFormat("struct Foo {\n" 12780 " Foo();\n" 12781 "};", 12782 Style); 12783 verifyFormat("typedef struct Foo {\n" 12784 " Foo();\n" 12785 "} Foo_t;", 12786 Style); 12787 verifyFormat("union Foo {\n" 12788 " A,\n" 12789 "};", 12790 Style); 12791 verifyFormat("typedef union Foo {\n" 12792 " A,\n" 12793 "} Foo_t;", 12794 Style); 12795 verifyFormat("namespace Foo {\n" 12796 "void Bar();\n" 12797 "};", 12798 Style); 12799 12800 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12801 Style.BraceWrapping.AfterClass = true; 12802 Style.BraceWrapping.AfterStruct = true; 12803 Style.BraceWrapping.AfterUnion = true; 12804 Style.BraceWrapping.AfterNamespace = true; 12805 verifyFormat("class Foo\n" 12806 "{\n" 12807 " Foo();\n" 12808 "};", 12809 Style); 12810 verifyFormat("typedef class Foo\n" 12811 "{\n" 12812 " Foo();\n" 12813 "} Foo_t;", 12814 Style); 12815 verifyFormat("struct Foo\n" 12816 "{\n" 12817 " Foo();\n" 12818 "};", 12819 Style); 12820 verifyFormat("typedef struct Foo\n" 12821 "{\n" 12822 " Foo();\n" 12823 "} Foo_t;", 12824 Style); 12825 verifyFormat("union Foo\n" 12826 "{\n" 12827 " A,\n" 12828 "};", 12829 Style); 12830 verifyFormat("typedef union Foo\n" 12831 "{\n" 12832 " A,\n" 12833 "} Foo_t;", 12834 Style); 12835 verifyFormat("namespace Foo\n" 12836 "{\n" 12837 "void Bar();\n" 12838 "};", 12839 Style); 12840 } 12841 12842 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 12843 // Elaborate type variable declarations. 12844 verifyFormat("struct foo a = {bar};\nint n;"); 12845 verifyFormat("class foo a = {bar};\nint n;"); 12846 verifyFormat("union foo a = {bar};\nint n;"); 12847 12848 // Elaborate types inside function definitions. 12849 verifyFormat("struct foo f() {}\nint n;"); 12850 verifyFormat("class foo f() {}\nint n;"); 12851 verifyFormat("union foo f() {}\nint n;"); 12852 12853 // Templates. 12854 verifyFormat("template <class X> void f() {}\nint n;"); 12855 verifyFormat("template <struct X> void f() {}\nint n;"); 12856 verifyFormat("template <union X> void f() {}\nint n;"); 12857 12858 // Actual definitions... 12859 verifyFormat("struct {\n} n;"); 12860 verifyFormat( 12861 "template <template <class T, class Y>, class Z> class X {\n} n;"); 12862 verifyFormat("union Z {\n int n;\n} x;"); 12863 verifyFormat("class MACRO Z {\n} n;"); 12864 verifyFormat("class MACRO(X) Z {\n} n;"); 12865 verifyFormat("class __attribute__(X) Z {\n} n;"); 12866 verifyFormat("class __declspec(X) Z {\n} n;"); 12867 verifyFormat("class A##B##C {\n} n;"); 12868 verifyFormat("class alignas(16) Z {\n} n;"); 12869 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 12870 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 12871 12872 // Redefinition from nested context: 12873 verifyFormat("class A::B::C {\n} n;"); 12874 12875 // Template definitions. 12876 verifyFormat( 12877 "template <typename F>\n" 12878 "Matcher(const Matcher<F> &Other,\n" 12879 " typename enable_if_c<is_base_of<F, T>::value &&\n" 12880 " !is_same<F, T>::value>::type * = 0)\n" 12881 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 12882 12883 // FIXME: This is still incorrectly handled at the formatter side. 12884 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 12885 verifyFormat("int i = SomeFunction(a<b, a> b);"); 12886 12887 // FIXME: 12888 // This now gets parsed incorrectly as class definition. 12889 // verifyFormat("class A<int> f() {\n}\nint n;"); 12890 12891 // Elaborate types where incorrectly parsing the structural element would 12892 // break the indent. 12893 verifyFormat("if (true)\n" 12894 " class X x;\n" 12895 "else\n" 12896 " f();\n"); 12897 12898 // This is simply incomplete. Formatting is not important, but must not crash. 12899 verifyFormat("class A:"); 12900 } 12901 12902 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 12903 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 12904 format("#error Leave all white!!!!! space* alone!\n")); 12905 EXPECT_EQ( 12906 "#warning Leave all white!!!!! space* alone!\n", 12907 format("#warning Leave all white!!!!! space* alone!\n")); 12908 EXPECT_EQ("#error 1", format(" # error 1")); 12909 EXPECT_EQ("#warning 1", format(" # warning 1")); 12910 } 12911 12912 TEST_F(FormatTest, FormatHashIfExpressions) { 12913 verifyFormat("#if AAAA && BBBB"); 12914 verifyFormat("#if (AAAA && BBBB)"); 12915 verifyFormat("#elif (AAAA && BBBB)"); 12916 // FIXME: Come up with a better indentation for #elif. 12917 verifyFormat( 12918 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 12919 " defined(BBBBBBBB)\n" 12920 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 12921 " defined(BBBBBBBB)\n" 12922 "#endif", 12923 getLLVMStyleWithColumns(65)); 12924 } 12925 12926 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 12927 FormatStyle AllowsMergedIf = getGoogleStyle(); 12928 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 12929 FormatStyle::SIS_WithoutElse; 12930 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 12931 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 12932 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 12933 EXPECT_EQ("if (true) return 42;", 12934 format("if (true)\nreturn 42;", AllowsMergedIf)); 12935 FormatStyle ShortMergedIf = AllowsMergedIf; 12936 ShortMergedIf.ColumnLimit = 25; 12937 verifyFormat("#define A \\\n" 12938 " if (true) return 42;", 12939 ShortMergedIf); 12940 verifyFormat("#define A \\\n" 12941 " f(); \\\n" 12942 " if (true)\n" 12943 "#define B", 12944 ShortMergedIf); 12945 verifyFormat("#define A \\\n" 12946 " f(); \\\n" 12947 " if (true)\n" 12948 "g();", 12949 ShortMergedIf); 12950 verifyFormat("{\n" 12951 "#ifdef A\n" 12952 " // Comment\n" 12953 " if (true) continue;\n" 12954 "#endif\n" 12955 " // Comment\n" 12956 " if (true) continue;\n" 12957 "}", 12958 ShortMergedIf); 12959 ShortMergedIf.ColumnLimit = 33; 12960 verifyFormat("#define A \\\n" 12961 " if constexpr (true) return 42;", 12962 ShortMergedIf); 12963 verifyFormat("#define A \\\n" 12964 " if CONSTEXPR (true) return 42;", 12965 ShortMergedIf); 12966 ShortMergedIf.ColumnLimit = 29; 12967 verifyFormat("#define A \\\n" 12968 " if (aaaaaaaaaa) return 1; \\\n" 12969 " return 2;", 12970 ShortMergedIf); 12971 ShortMergedIf.ColumnLimit = 28; 12972 verifyFormat("#define A \\\n" 12973 " if (aaaaaaaaaa) \\\n" 12974 " return 1; \\\n" 12975 " return 2;", 12976 ShortMergedIf); 12977 verifyFormat("#define A \\\n" 12978 " if constexpr (aaaaaaa) \\\n" 12979 " return 1; \\\n" 12980 " return 2;", 12981 ShortMergedIf); 12982 verifyFormat("#define A \\\n" 12983 " if CONSTEXPR (aaaaaaa) \\\n" 12984 " return 1; \\\n" 12985 " return 2;", 12986 ShortMergedIf); 12987 } 12988 12989 TEST_F(FormatTest, FormatStarDependingOnContext) { 12990 verifyFormat("void f(int *a);"); 12991 verifyFormat("void f() { f(fint * b); }"); 12992 verifyFormat("class A {\n void f(int *a);\n};"); 12993 verifyFormat("class A {\n int *a;\n};"); 12994 verifyFormat("namespace a {\n" 12995 "namespace b {\n" 12996 "class A {\n" 12997 " void f() {}\n" 12998 " int *a;\n" 12999 "};\n" 13000 "} // namespace b\n" 13001 "} // namespace a"); 13002 } 13003 13004 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 13005 verifyFormat("while"); 13006 verifyFormat("operator"); 13007 } 13008 13009 TEST_F(FormatTest, SkipsDeeplyNestedLines) { 13010 // This code would be painfully slow to format if we didn't skip it. 13011 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 13012 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 13013 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 13014 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 13015 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 13016 "A(1, 1)\n" 13017 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x 13018 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 13019 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 13020 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 13021 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 13022 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 13023 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 13024 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 13025 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 13026 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n"); 13027 // Deeply nested part is untouched, rest is formatted. 13028 EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n", 13029 format(std::string("int i;\n") + Code + "int j;\n", 13030 getLLVMStyle(), SC_ExpectIncomplete)); 13031 } 13032 13033 //===----------------------------------------------------------------------===// 13034 // Objective-C tests. 13035 //===----------------------------------------------------------------------===// 13036 13037 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 13038 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 13039 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 13040 format("-(NSUInteger)indexOfObject:(id)anObject;")); 13041 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 13042 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 13043 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 13044 format("-(NSInteger)Method3:(id)anObject;")); 13045 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 13046 format("-(NSInteger)Method4:(id)anObject;")); 13047 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 13048 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 13049 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 13050 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 13051 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 13052 "forAllCells:(BOOL)flag;", 13053 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 13054 "forAllCells:(BOOL)flag;")); 13055 13056 // Very long objectiveC method declaration. 13057 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 13058 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 13059 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 13060 " inRange:(NSRange)range\n" 13061 " outRange:(NSRange)out_range\n" 13062 " outRange1:(NSRange)out_range1\n" 13063 " outRange2:(NSRange)out_range2\n" 13064 " outRange3:(NSRange)out_range3\n" 13065 " outRange4:(NSRange)out_range4\n" 13066 " outRange5:(NSRange)out_range5\n" 13067 " outRange6:(NSRange)out_range6\n" 13068 " outRange7:(NSRange)out_range7\n" 13069 " outRange8:(NSRange)out_range8\n" 13070 " outRange9:(NSRange)out_range9;"); 13071 13072 // When the function name has to be wrapped. 13073 FormatStyle Style = getLLVMStyle(); 13074 // ObjC ignores IndentWrappedFunctionNames when wrapping methods 13075 // and always indents instead. 13076 Style.IndentWrappedFunctionNames = false; 13077 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 13078 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 13079 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 13080 "}", 13081 Style); 13082 Style.IndentWrappedFunctionNames = true; 13083 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 13084 " veryLooooooooooongName:(NSString)cccccccccccccc\n" 13085 " anotherName:(NSString)dddddddddddddd {\n" 13086 "}", 13087 Style); 13088 13089 verifyFormat("- (int)sum:(vector<int>)numbers;"); 13090 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 13091 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 13092 // protocol lists (but not for template classes): 13093 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 13094 13095 verifyFormat("- (int (*)())foo:(int (*)())f;"); 13096 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 13097 13098 // If there's no return type (very rare in practice!), LLVM and Google style 13099 // agree. 13100 verifyFormat("- foo;"); 13101 verifyFormat("- foo:(int)f;"); 13102 verifyGoogleFormat("- foo:(int)foo;"); 13103 } 13104 13105 TEST_F(FormatTest, BreaksStringLiterals) { 13106 EXPECT_EQ("\"some text \"\n" 13107 "\"other\";", 13108 format("\"some text other\";", getLLVMStyleWithColumns(12))); 13109 EXPECT_EQ("\"some text \"\n" 13110 "\"other\";", 13111 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 13112 EXPECT_EQ( 13113 "#define A \\\n" 13114 " \"some \" \\\n" 13115 " \"text \" \\\n" 13116 " \"other\";", 13117 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 13118 EXPECT_EQ( 13119 "#define A \\\n" 13120 " \"so \" \\\n" 13121 " \"text \" \\\n" 13122 " \"other\";", 13123 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 13124 13125 EXPECT_EQ("\"some text\"", 13126 format("\"some text\"", getLLVMStyleWithColumns(1))); 13127 EXPECT_EQ("\"some text\"", 13128 format("\"some text\"", getLLVMStyleWithColumns(11))); 13129 EXPECT_EQ("\"some \"\n" 13130 "\"text\"", 13131 format("\"some text\"", getLLVMStyleWithColumns(10))); 13132 EXPECT_EQ("\"some \"\n" 13133 "\"text\"", 13134 format("\"some text\"", getLLVMStyleWithColumns(7))); 13135 EXPECT_EQ("\"some\"\n" 13136 "\" tex\"\n" 13137 "\"t\"", 13138 format("\"some text\"", getLLVMStyleWithColumns(6))); 13139 EXPECT_EQ("\"some\"\n" 13140 "\" tex\"\n" 13141 "\" and\"", 13142 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 13143 EXPECT_EQ("\"some\"\n" 13144 "\"/tex\"\n" 13145 "\"/and\"", 13146 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 13147 13148 EXPECT_EQ("variable =\n" 13149 " \"long string \"\n" 13150 " \"literal\";", 13151 format("variable = \"long string literal\";", 13152 getLLVMStyleWithColumns(20))); 13153 13154 EXPECT_EQ("variable = f(\n" 13155 " \"long string \"\n" 13156 " \"literal\",\n" 13157 " short,\n" 13158 " loooooooooooooooooooong);", 13159 format("variable = f(\"long string literal\", short, " 13160 "loooooooooooooooooooong);", 13161 getLLVMStyleWithColumns(20))); 13162 13163 EXPECT_EQ( 13164 "f(g(\"long string \"\n" 13165 " \"literal\"),\n" 13166 " b);", 13167 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 13168 EXPECT_EQ("f(g(\"long string \"\n" 13169 " \"literal\",\n" 13170 " a),\n" 13171 " b);", 13172 format("f(g(\"long string literal\", a), b);", 13173 getLLVMStyleWithColumns(20))); 13174 EXPECT_EQ( 13175 "f(\"one two\".split(\n" 13176 " variable));", 13177 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 13178 EXPECT_EQ("f(\"one two three four five six \"\n" 13179 " \"seven\".split(\n" 13180 " really_looooong_variable));", 13181 format("f(\"one two three four five six seven\"." 13182 "split(really_looooong_variable));", 13183 getLLVMStyleWithColumns(33))); 13184 13185 EXPECT_EQ("f(\"some \"\n" 13186 " \"text\",\n" 13187 " other);", 13188 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 13189 13190 // Only break as a last resort. 13191 verifyFormat( 13192 "aaaaaaaaaaaaaaaaaaaa(\n" 13193 " aaaaaaaaaaaaaaaaaaaa,\n" 13194 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 13195 13196 EXPECT_EQ("\"splitmea\"\n" 13197 "\"trandomp\"\n" 13198 "\"oint\"", 13199 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 13200 13201 EXPECT_EQ("\"split/\"\n" 13202 "\"pathat/\"\n" 13203 "\"slashes\"", 13204 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 13205 13206 EXPECT_EQ("\"split/\"\n" 13207 "\"pathat/\"\n" 13208 "\"slashes\"", 13209 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 13210 EXPECT_EQ("\"split at \"\n" 13211 "\"spaces/at/\"\n" 13212 "\"slashes.at.any$\"\n" 13213 "\"non-alphanumeric%\"\n" 13214 "\"1111111111characte\"\n" 13215 "\"rs\"", 13216 format("\"split at " 13217 "spaces/at/" 13218 "slashes.at." 13219 "any$non-" 13220 "alphanumeric%" 13221 "1111111111characte" 13222 "rs\"", 13223 getLLVMStyleWithColumns(20))); 13224 13225 // Verify that splitting the strings understands 13226 // Style::AlwaysBreakBeforeMultilineStrings. 13227 EXPECT_EQ("aaaaaaaaaaaa(\n" 13228 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 13229 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 13230 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 13231 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 13232 "aaaaaaaaaaaaaaaaaaaaaa\");", 13233 getGoogleStyle())); 13234 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 13235 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 13236 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 13237 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 13238 "aaaaaaaaaaaaaaaaaaaaaa\";", 13239 getGoogleStyle())); 13240 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 13241 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 13242 format("llvm::outs() << " 13243 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 13244 "aaaaaaaaaaaaaaaaaaa\";")); 13245 EXPECT_EQ("ffff(\n" 13246 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 13247 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 13248 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 13249 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 13250 getGoogleStyle())); 13251 13252 FormatStyle Style = getLLVMStyleWithColumns(12); 13253 Style.BreakStringLiterals = false; 13254 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 13255 13256 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 13257 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 13258 EXPECT_EQ("#define A \\\n" 13259 " \"some \" \\\n" 13260 " \"text \" \\\n" 13261 " \"other\";", 13262 format("#define A \"some text other\";", AlignLeft)); 13263 } 13264 13265 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) { 13266 EXPECT_EQ("C a = \"some more \"\n" 13267 " \"text\";", 13268 format("C a = \"some more text\";", getLLVMStyleWithColumns(18))); 13269 } 13270 13271 TEST_F(FormatTest, FullyRemoveEmptyLines) { 13272 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 13273 NoEmptyLines.MaxEmptyLinesToKeep = 0; 13274 EXPECT_EQ("int i = a(b());", 13275 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 13276 } 13277 13278 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 13279 EXPECT_EQ( 13280 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 13281 "(\n" 13282 " \"x\t\");", 13283 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 13284 "aaaaaaa(" 13285 "\"x\t\");")); 13286 } 13287 13288 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 13289 EXPECT_EQ( 13290 "u8\"utf8 string \"\n" 13291 "u8\"literal\";", 13292 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 13293 EXPECT_EQ( 13294 "u\"utf16 string \"\n" 13295 "u\"literal\";", 13296 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 13297 EXPECT_EQ( 13298 "U\"utf32 string \"\n" 13299 "U\"literal\";", 13300 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 13301 EXPECT_EQ("L\"wide string \"\n" 13302 "L\"literal\";", 13303 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 13304 EXPECT_EQ("@\"NSString \"\n" 13305 "@\"literal\";", 13306 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 13307 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26)); 13308 13309 // This input makes clang-format try to split the incomplete unicode escape 13310 // sequence, which used to lead to a crasher. 13311 verifyNoCrash( 13312 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 13313 getLLVMStyleWithColumns(60)); 13314 } 13315 13316 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 13317 FormatStyle Style = getGoogleStyleWithColumns(15); 13318 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 13319 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 13320 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 13321 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 13322 EXPECT_EQ("u8R\"x(raw literal)x\";", 13323 format("u8R\"x(raw literal)x\";", Style)); 13324 } 13325 13326 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 13327 FormatStyle Style = getLLVMStyleWithColumns(20); 13328 EXPECT_EQ( 13329 "_T(\"aaaaaaaaaaaaaa\")\n" 13330 "_T(\"aaaaaaaaaaaaaa\")\n" 13331 "_T(\"aaaaaaaaaaaa\")", 13332 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 13333 EXPECT_EQ("f(x,\n" 13334 " _T(\"aaaaaaaaaaaa\")\n" 13335 " _T(\"aaa\"),\n" 13336 " z);", 13337 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 13338 13339 // FIXME: Handle embedded spaces in one iteration. 13340 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 13341 // "_T(\"aaaaaaaaaaaaa\")\n" 13342 // "_T(\"aaaaaaaaaaaaa\")\n" 13343 // "_T(\"a\")", 13344 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 13345 // getLLVMStyleWithColumns(20))); 13346 EXPECT_EQ( 13347 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 13348 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 13349 EXPECT_EQ("f(\n" 13350 "#if !TEST\n" 13351 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 13352 "#endif\n" 13353 ");", 13354 format("f(\n" 13355 "#if !TEST\n" 13356 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 13357 "#endif\n" 13358 ");")); 13359 EXPECT_EQ("f(\n" 13360 "\n" 13361 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 13362 format("f(\n" 13363 "\n" 13364 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 13365 // Regression test for accessing tokens past the end of a vector in the 13366 // TokenLexer. 13367 verifyNoCrash(R"(_T( 13368 " 13369 ) 13370 )"); 13371 } 13372 13373 TEST_F(FormatTest, BreaksStringLiteralOperands) { 13374 // In a function call with two operands, the second can be broken with no line 13375 // break before it. 13376 EXPECT_EQ( 13377 "func(a, \"long long \"\n" 13378 " \"long long\");", 13379 format("func(a, \"long long long long\");", getLLVMStyleWithColumns(24))); 13380 // In a function call with three operands, the second must be broken with a 13381 // line break before it. 13382 EXPECT_EQ("func(a,\n" 13383 " \"long long long \"\n" 13384 " \"long\",\n" 13385 " c);", 13386 format("func(a, \"long long long long\", c);", 13387 getLLVMStyleWithColumns(24))); 13388 // In a function call with three operands, the third must be broken with a 13389 // line break before it. 13390 EXPECT_EQ("func(a, b,\n" 13391 " \"long long long \"\n" 13392 " \"long\");", 13393 format("func(a, b, \"long long long long\");", 13394 getLLVMStyleWithColumns(24))); 13395 // In a function call with three operands, both the second and the third must 13396 // be broken with a line break before them. 13397 EXPECT_EQ("func(a,\n" 13398 " \"long long long \"\n" 13399 " \"long\",\n" 13400 " \"long long long \"\n" 13401 " \"long\");", 13402 format("func(a, \"long long long long\", \"long long long long\");", 13403 getLLVMStyleWithColumns(24))); 13404 // In a chain of << with two operands, the second can be broken with no line 13405 // break before it. 13406 EXPECT_EQ("a << \"line line \"\n" 13407 " \"line\";", 13408 format("a << \"line line line\";", getLLVMStyleWithColumns(20))); 13409 // In a chain of << with three operands, the second can be broken with no line 13410 // break before it. 13411 EXPECT_EQ( 13412 "abcde << \"line \"\n" 13413 " \"line line\"\n" 13414 " << c;", 13415 format("abcde << \"line line line\" << c;", getLLVMStyleWithColumns(20))); 13416 // In a chain of << with three operands, the third must be broken with a line 13417 // break before it. 13418 EXPECT_EQ( 13419 "a << b\n" 13420 " << \"line line \"\n" 13421 " \"line\";", 13422 format("a << b << \"line line line\";", getLLVMStyleWithColumns(20))); 13423 // In a chain of << with three operands, the second can be broken with no line 13424 // break before it and the third must be broken with a line break before it. 13425 EXPECT_EQ("abcd << \"line line \"\n" 13426 " \"line\"\n" 13427 " << \"line line \"\n" 13428 " \"line\";", 13429 format("abcd << \"line line line\" << \"line line line\";", 13430 getLLVMStyleWithColumns(20))); 13431 // In a chain of binary operators with two operands, the second can be broken 13432 // with no line break before it. 13433 EXPECT_EQ( 13434 "abcd + \"line line \"\n" 13435 " \"line line\";", 13436 format("abcd + \"line line line line\";", getLLVMStyleWithColumns(20))); 13437 // In a chain of binary operators with three operands, the second must be 13438 // broken with a line break before it. 13439 EXPECT_EQ("abcd +\n" 13440 " \"line line \"\n" 13441 " \"line line\" +\n" 13442 " e;", 13443 format("abcd + \"line line line line\" + e;", 13444 getLLVMStyleWithColumns(20))); 13445 // In a function call with two operands, with AlignAfterOpenBracket enabled, 13446 // the first must be broken with a line break before it. 13447 FormatStyle Style = getLLVMStyleWithColumns(25); 13448 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 13449 EXPECT_EQ("someFunction(\n" 13450 " \"long long long \"\n" 13451 " \"long\",\n" 13452 " a);", 13453 format("someFunction(\"long long long long\", a);", Style)); 13454 } 13455 13456 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 13457 EXPECT_EQ( 13458 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 13459 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 13460 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 13461 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 13462 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 13463 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 13464 } 13465 13466 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 13467 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 13468 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 13469 EXPECT_EQ("fffffffffff(g(R\"x(\n" 13470 "multiline raw string literal xxxxxxxxxxxxxx\n" 13471 ")x\",\n" 13472 " a),\n" 13473 " b);", 13474 format("fffffffffff(g(R\"x(\n" 13475 "multiline raw string literal xxxxxxxxxxxxxx\n" 13476 ")x\", a), b);", 13477 getGoogleStyleWithColumns(20))); 13478 EXPECT_EQ("fffffffffff(\n" 13479 " g(R\"x(qqq\n" 13480 "multiline raw string literal xxxxxxxxxxxxxx\n" 13481 ")x\",\n" 13482 " a),\n" 13483 " b);", 13484 format("fffffffffff(g(R\"x(qqq\n" 13485 "multiline raw string literal xxxxxxxxxxxxxx\n" 13486 ")x\", a), b);", 13487 getGoogleStyleWithColumns(20))); 13488 13489 EXPECT_EQ("fffffffffff(R\"x(\n" 13490 "multiline raw string literal xxxxxxxxxxxxxx\n" 13491 ")x\");", 13492 format("fffffffffff(R\"x(\n" 13493 "multiline raw string literal xxxxxxxxxxxxxx\n" 13494 ")x\");", 13495 getGoogleStyleWithColumns(20))); 13496 EXPECT_EQ("fffffffffff(R\"x(\n" 13497 "multiline raw string literal xxxxxxxxxxxxxx\n" 13498 ")x\" + bbbbbb);", 13499 format("fffffffffff(R\"x(\n" 13500 "multiline raw string literal xxxxxxxxxxxxxx\n" 13501 ")x\" + bbbbbb);", 13502 getGoogleStyleWithColumns(20))); 13503 EXPECT_EQ("fffffffffff(\n" 13504 " R\"x(\n" 13505 "multiline raw string literal xxxxxxxxxxxxxx\n" 13506 ")x\" +\n" 13507 " bbbbbb);", 13508 format("fffffffffff(\n" 13509 " R\"x(\n" 13510 "multiline raw string literal xxxxxxxxxxxxxx\n" 13511 ")x\" + bbbbbb);", 13512 getGoogleStyleWithColumns(20))); 13513 EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);", 13514 format("fffffffffff(\n" 13515 " R\"(single line raw string)\" + bbbbbb);")); 13516 } 13517 13518 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 13519 verifyFormat("string a = \"unterminated;"); 13520 EXPECT_EQ("function(\"unterminated,\n" 13521 " OtherParameter);", 13522 format("function( \"unterminated,\n" 13523 " OtherParameter);")); 13524 } 13525 13526 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 13527 FormatStyle Style = getLLVMStyle(); 13528 Style.Standard = FormatStyle::LS_Cpp03; 13529 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 13530 format("#define x(_a) printf(\"foo\"_a);", Style)); 13531 } 13532 13533 TEST_F(FormatTest, CppLexVersion) { 13534 FormatStyle Style = getLLVMStyle(); 13535 // Formatting of x * y differs if x is a type. 13536 verifyFormat("void foo() { MACRO(a * b); }", Style); 13537 verifyFormat("void foo() { MACRO(int *b); }", Style); 13538 13539 // LLVM style uses latest lexer. 13540 verifyFormat("void foo() { MACRO(char8_t *b); }", Style); 13541 Style.Standard = FormatStyle::LS_Cpp17; 13542 // But in c++17, char8_t isn't a keyword. 13543 verifyFormat("void foo() { MACRO(char8_t * b); }", Style); 13544 } 13545 13546 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 13547 13548 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 13549 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 13550 " \"ddeeefff\");", 13551 format("someFunction(\"aaabbbcccdddeeefff\");", 13552 getLLVMStyleWithColumns(25))); 13553 EXPECT_EQ("someFunction1234567890(\n" 13554 " \"aaabbbcccdddeeefff\");", 13555 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 13556 getLLVMStyleWithColumns(26))); 13557 EXPECT_EQ("someFunction1234567890(\n" 13558 " \"aaabbbcccdddeeeff\"\n" 13559 " \"f\");", 13560 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 13561 getLLVMStyleWithColumns(25))); 13562 EXPECT_EQ("someFunction1234567890(\n" 13563 " \"aaabbbcccdddeeeff\"\n" 13564 " \"f\");", 13565 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 13566 getLLVMStyleWithColumns(24))); 13567 EXPECT_EQ("someFunction(\n" 13568 " \"aaabbbcc ddde \"\n" 13569 " \"efff\");", 13570 format("someFunction(\"aaabbbcc ddde efff\");", 13571 getLLVMStyleWithColumns(25))); 13572 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 13573 " \"ddeeefff\");", 13574 format("someFunction(\"aaabbbccc ddeeefff\");", 13575 getLLVMStyleWithColumns(25))); 13576 EXPECT_EQ("someFunction1234567890(\n" 13577 " \"aaabb \"\n" 13578 " \"cccdddeeefff\");", 13579 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 13580 getLLVMStyleWithColumns(25))); 13581 EXPECT_EQ("#define A \\\n" 13582 " string s = \\\n" 13583 " \"123456789\" \\\n" 13584 " \"0\"; \\\n" 13585 " int i;", 13586 format("#define A string s = \"1234567890\"; int i;", 13587 getLLVMStyleWithColumns(20))); 13588 EXPECT_EQ("someFunction(\n" 13589 " \"aaabbbcc \"\n" 13590 " \"dddeeefff\");", 13591 format("someFunction(\"aaabbbcc dddeeefff\");", 13592 getLLVMStyleWithColumns(25))); 13593 } 13594 13595 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 13596 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 13597 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 13598 EXPECT_EQ("\"test\"\n" 13599 "\"\\n\"", 13600 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 13601 EXPECT_EQ("\"tes\\\\\"\n" 13602 "\"n\"", 13603 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 13604 EXPECT_EQ("\"\\\\\\\\\"\n" 13605 "\"\\n\"", 13606 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 13607 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 13608 EXPECT_EQ("\"\\uff01\"\n" 13609 "\"test\"", 13610 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 13611 EXPECT_EQ("\"\\Uff01ff02\"", 13612 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 13613 EXPECT_EQ("\"\\x000000000001\"\n" 13614 "\"next\"", 13615 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 13616 EXPECT_EQ("\"\\x000000000001next\"", 13617 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 13618 EXPECT_EQ("\"\\x000000000001\"", 13619 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 13620 EXPECT_EQ("\"test\"\n" 13621 "\"\\000000\"\n" 13622 "\"000001\"", 13623 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 13624 EXPECT_EQ("\"test\\000\"\n" 13625 "\"00000000\"\n" 13626 "\"1\"", 13627 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 13628 } 13629 13630 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 13631 verifyFormat("void f() {\n" 13632 " return g() {}\n" 13633 " void h() {}"); 13634 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 13635 "g();\n" 13636 "}"); 13637 } 13638 13639 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 13640 verifyFormat( 13641 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 13642 } 13643 13644 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 13645 verifyFormat("class X {\n" 13646 " void f() {\n" 13647 " }\n" 13648 "};", 13649 getLLVMStyleWithColumns(12)); 13650 } 13651 13652 TEST_F(FormatTest, ConfigurableIndentWidth) { 13653 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 13654 EightIndent.IndentWidth = 8; 13655 EightIndent.ContinuationIndentWidth = 8; 13656 verifyFormat("void f() {\n" 13657 " someFunction();\n" 13658 " if (true) {\n" 13659 " f();\n" 13660 " }\n" 13661 "}", 13662 EightIndent); 13663 verifyFormat("class X {\n" 13664 " void f() {\n" 13665 " }\n" 13666 "};", 13667 EightIndent); 13668 verifyFormat("int x[] = {\n" 13669 " call(),\n" 13670 " call()};", 13671 EightIndent); 13672 } 13673 13674 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 13675 verifyFormat("double\n" 13676 "f();", 13677 getLLVMStyleWithColumns(8)); 13678 } 13679 13680 TEST_F(FormatTest, ConfigurableUseOfTab) { 13681 FormatStyle Tab = getLLVMStyleWithColumns(42); 13682 Tab.IndentWidth = 8; 13683 Tab.UseTab = FormatStyle::UT_Always; 13684 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left; 13685 13686 EXPECT_EQ("if (aaaaaaaa && // q\n" 13687 " bb)\t\t// w\n" 13688 "\t;", 13689 format("if (aaaaaaaa &&// q\n" 13690 "bb)// w\n" 13691 ";", 13692 Tab)); 13693 EXPECT_EQ("if (aaa && bbb) // w\n" 13694 "\t;", 13695 format("if(aaa&&bbb)// w\n" 13696 ";", 13697 Tab)); 13698 13699 verifyFormat("class X {\n" 13700 "\tvoid f() {\n" 13701 "\t\tsomeFunction(parameter1,\n" 13702 "\t\t\t parameter2);\n" 13703 "\t}\n" 13704 "};", 13705 Tab); 13706 verifyFormat("#define A \\\n" 13707 "\tvoid f() { \\\n" 13708 "\t\tsomeFunction( \\\n" 13709 "\t\t parameter1, \\\n" 13710 "\t\t parameter2); \\\n" 13711 "\t}", 13712 Tab); 13713 verifyFormat("int a;\t // x\n" 13714 "int bbbbbbbb; // x\n", 13715 Tab); 13716 13717 Tab.TabWidth = 4; 13718 Tab.IndentWidth = 8; 13719 verifyFormat("class TabWidth4Indent8 {\n" 13720 "\t\tvoid f() {\n" 13721 "\t\t\t\tsomeFunction(parameter1,\n" 13722 "\t\t\t\t\t\t\t parameter2);\n" 13723 "\t\t}\n" 13724 "};", 13725 Tab); 13726 13727 Tab.TabWidth = 4; 13728 Tab.IndentWidth = 4; 13729 verifyFormat("class TabWidth4Indent4 {\n" 13730 "\tvoid f() {\n" 13731 "\t\tsomeFunction(parameter1,\n" 13732 "\t\t\t\t\t parameter2);\n" 13733 "\t}\n" 13734 "};", 13735 Tab); 13736 13737 Tab.TabWidth = 8; 13738 Tab.IndentWidth = 4; 13739 verifyFormat("class TabWidth8Indent4 {\n" 13740 " void f() {\n" 13741 "\tsomeFunction(parameter1,\n" 13742 "\t\t parameter2);\n" 13743 " }\n" 13744 "};", 13745 Tab); 13746 13747 Tab.TabWidth = 8; 13748 Tab.IndentWidth = 8; 13749 EXPECT_EQ("/*\n" 13750 "\t a\t\tcomment\n" 13751 "\t in multiple lines\n" 13752 " */", 13753 format(" /*\t \t \n" 13754 " \t \t a\t\tcomment\t \t\n" 13755 " \t \t in multiple lines\t\n" 13756 " \t */", 13757 Tab)); 13758 13759 Tab.UseTab = FormatStyle::UT_ForIndentation; 13760 verifyFormat("{\n" 13761 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13762 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13763 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13764 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13765 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13766 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13767 "};", 13768 Tab); 13769 verifyFormat("enum AA {\n" 13770 "\ta1, // Force multiple lines\n" 13771 "\ta2,\n" 13772 "\ta3\n" 13773 "};", 13774 Tab); 13775 EXPECT_EQ("if (aaaaaaaa && // q\n" 13776 " bb) // w\n" 13777 "\t;", 13778 format("if (aaaaaaaa &&// q\n" 13779 "bb)// w\n" 13780 ";", 13781 Tab)); 13782 verifyFormat("class X {\n" 13783 "\tvoid f() {\n" 13784 "\t\tsomeFunction(parameter1,\n" 13785 "\t\t parameter2);\n" 13786 "\t}\n" 13787 "};", 13788 Tab); 13789 verifyFormat("{\n" 13790 "\tQ(\n" 13791 "\t {\n" 13792 "\t\t int a;\n" 13793 "\t\t someFunction(aaaaaaaa,\n" 13794 "\t\t bbbbbbb);\n" 13795 "\t },\n" 13796 "\t p);\n" 13797 "}", 13798 Tab); 13799 EXPECT_EQ("{\n" 13800 "\t/* aaaa\n" 13801 "\t bbbb */\n" 13802 "}", 13803 format("{\n" 13804 "/* aaaa\n" 13805 " bbbb */\n" 13806 "}", 13807 Tab)); 13808 EXPECT_EQ("{\n" 13809 "\t/*\n" 13810 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13811 "\t bbbbbbbbbbbbb\n" 13812 "\t*/\n" 13813 "}", 13814 format("{\n" 13815 "/*\n" 13816 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13817 "*/\n" 13818 "}", 13819 Tab)); 13820 EXPECT_EQ("{\n" 13821 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13822 "\t// bbbbbbbbbbbbb\n" 13823 "}", 13824 format("{\n" 13825 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13826 "}", 13827 Tab)); 13828 EXPECT_EQ("{\n" 13829 "\t/*\n" 13830 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13831 "\t bbbbbbbbbbbbb\n" 13832 "\t*/\n" 13833 "}", 13834 format("{\n" 13835 "\t/*\n" 13836 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13837 "\t*/\n" 13838 "}", 13839 Tab)); 13840 EXPECT_EQ("{\n" 13841 "\t/*\n" 13842 "\n" 13843 "\t*/\n" 13844 "}", 13845 format("{\n" 13846 "\t/*\n" 13847 "\n" 13848 "\t*/\n" 13849 "}", 13850 Tab)); 13851 EXPECT_EQ("{\n" 13852 "\t/*\n" 13853 " asdf\n" 13854 "\t*/\n" 13855 "}", 13856 format("{\n" 13857 "\t/*\n" 13858 " asdf\n" 13859 "\t*/\n" 13860 "}", 13861 Tab)); 13862 13863 verifyFormat("void f() {\n" 13864 "\treturn true ? aaaaaaaaaaaaaaaaaa\n" 13865 "\t : bbbbbbbbbbbbbbbbbb\n" 13866 "}", 13867 Tab); 13868 FormatStyle TabNoBreak = Tab; 13869 TabNoBreak.BreakBeforeTernaryOperators = false; 13870 verifyFormat("void f() {\n" 13871 "\treturn true ? aaaaaaaaaaaaaaaaaa :\n" 13872 "\t bbbbbbbbbbbbbbbbbb\n" 13873 "}", 13874 TabNoBreak); 13875 verifyFormat("void f() {\n" 13876 "\treturn true ?\n" 13877 "\t aaaaaaaaaaaaaaaaaaaa :\n" 13878 "\t bbbbbbbbbbbbbbbbbbbb\n" 13879 "}", 13880 TabNoBreak); 13881 13882 Tab.UseTab = FormatStyle::UT_Never; 13883 EXPECT_EQ("/*\n" 13884 " a\t\tcomment\n" 13885 " in multiple lines\n" 13886 " */", 13887 format(" /*\t \t \n" 13888 " \t \t a\t\tcomment\t \t\n" 13889 " \t \t in multiple lines\t\n" 13890 " \t */", 13891 Tab)); 13892 EXPECT_EQ("/* some\n" 13893 " comment */", 13894 format(" \t \t /* some\n" 13895 " \t \t comment */", 13896 Tab)); 13897 EXPECT_EQ("int a; /* some\n" 13898 " comment */", 13899 format(" \t \t int a; /* some\n" 13900 " \t \t comment */", 13901 Tab)); 13902 13903 EXPECT_EQ("int a; /* some\n" 13904 "comment */", 13905 format(" \t \t int\ta; /* some\n" 13906 " \t \t comment */", 13907 Tab)); 13908 EXPECT_EQ("f(\"\t\t\"); /* some\n" 13909 " comment */", 13910 format(" \t \t f(\"\t\t\"); /* some\n" 13911 " \t \t comment */", 13912 Tab)); 13913 EXPECT_EQ("{\n" 13914 " /*\n" 13915 " * Comment\n" 13916 " */\n" 13917 " int i;\n" 13918 "}", 13919 format("{\n" 13920 "\t/*\n" 13921 "\t * Comment\n" 13922 "\t */\n" 13923 "\t int i;\n" 13924 "}", 13925 Tab)); 13926 13927 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 13928 Tab.TabWidth = 8; 13929 Tab.IndentWidth = 8; 13930 EXPECT_EQ("if (aaaaaaaa && // q\n" 13931 " bb) // w\n" 13932 "\t;", 13933 format("if (aaaaaaaa &&// q\n" 13934 "bb)// w\n" 13935 ";", 13936 Tab)); 13937 EXPECT_EQ("if (aaa && bbb) // w\n" 13938 "\t;", 13939 format("if(aaa&&bbb)// w\n" 13940 ";", 13941 Tab)); 13942 verifyFormat("class X {\n" 13943 "\tvoid f() {\n" 13944 "\t\tsomeFunction(parameter1,\n" 13945 "\t\t\t parameter2);\n" 13946 "\t}\n" 13947 "};", 13948 Tab); 13949 verifyFormat("#define A \\\n" 13950 "\tvoid f() { \\\n" 13951 "\t\tsomeFunction( \\\n" 13952 "\t\t parameter1, \\\n" 13953 "\t\t parameter2); \\\n" 13954 "\t}", 13955 Tab); 13956 Tab.TabWidth = 4; 13957 Tab.IndentWidth = 8; 13958 verifyFormat("class TabWidth4Indent8 {\n" 13959 "\t\tvoid f() {\n" 13960 "\t\t\t\tsomeFunction(parameter1,\n" 13961 "\t\t\t\t\t\t\t parameter2);\n" 13962 "\t\t}\n" 13963 "};", 13964 Tab); 13965 Tab.TabWidth = 4; 13966 Tab.IndentWidth = 4; 13967 verifyFormat("class TabWidth4Indent4 {\n" 13968 "\tvoid f() {\n" 13969 "\t\tsomeFunction(parameter1,\n" 13970 "\t\t\t\t\t parameter2);\n" 13971 "\t}\n" 13972 "};", 13973 Tab); 13974 Tab.TabWidth = 8; 13975 Tab.IndentWidth = 4; 13976 verifyFormat("class TabWidth8Indent4 {\n" 13977 " void f() {\n" 13978 "\tsomeFunction(parameter1,\n" 13979 "\t\t parameter2);\n" 13980 " }\n" 13981 "};", 13982 Tab); 13983 Tab.TabWidth = 8; 13984 Tab.IndentWidth = 8; 13985 EXPECT_EQ("/*\n" 13986 "\t a\t\tcomment\n" 13987 "\t in multiple lines\n" 13988 " */", 13989 format(" /*\t \t \n" 13990 " \t \t a\t\tcomment\t \t\n" 13991 " \t \t in multiple lines\t\n" 13992 " \t */", 13993 Tab)); 13994 verifyFormat("{\n" 13995 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13996 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13997 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13998 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13999 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14000 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14001 "};", 14002 Tab); 14003 verifyFormat("enum AA {\n" 14004 "\ta1, // Force multiple lines\n" 14005 "\ta2,\n" 14006 "\ta3\n" 14007 "};", 14008 Tab); 14009 EXPECT_EQ("if (aaaaaaaa && // q\n" 14010 " bb) // w\n" 14011 "\t;", 14012 format("if (aaaaaaaa &&// q\n" 14013 "bb)// w\n" 14014 ";", 14015 Tab)); 14016 verifyFormat("class X {\n" 14017 "\tvoid f() {\n" 14018 "\t\tsomeFunction(parameter1,\n" 14019 "\t\t\t parameter2);\n" 14020 "\t}\n" 14021 "};", 14022 Tab); 14023 verifyFormat("{\n" 14024 "\tQ(\n" 14025 "\t {\n" 14026 "\t\t int a;\n" 14027 "\t\t someFunction(aaaaaaaa,\n" 14028 "\t\t\t\t bbbbbbb);\n" 14029 "\t },\n" 14030 "\t p);\n" 14031 "}", 14032 Tab); 14033 EXPECT_EQ("{\n" 14034 "\t/* aaaa\n" 14035 "\t bbbb */\n" 14036 "}", 14037 format("{\n" 14038 "/* aaaa\n" 14039 " bbbb */\n" 14040 "}", 14041 Tab)); 14042 EXPECT_EQ("{\n" 14043 "\t/*\n" 14044 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14045 "\t bbbbbbbbbbbbb\n" 14046 "\t*/\n" 14047 "}", 14048 format("{\n" 14049 "/*\n" 14050 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14051 "*/\n" 14052 "}", 14053 Tab)); 14054 EXPECT_EQ("{\n" 14055 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14056 "\t// bbbbbbbbbbbbb\n" 14057 "}", 14058 format("{\n" 14059 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14060 "}", 14061 Tab)); 14062 EXPECT_EQ("{\n" 14063 "\t/*\n" 14064 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14065 "\t bbbbbbbbbbbbb\n" 14066 "\t*/\n" 14067 "}", 14068 format("{\n" 14069 "\t/*\n" 14070 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14071 "\t*/\n" 14072 "}", 14073 Tab)); 14074 EXPECT_EQ("{\n" 14075 "\t/*\n" 14076 "\n" 14077 "\t*/\n" 14078 "}", 14079 format("{\n" 14080 "\t/*\n" 14081 "\n" 14082 "\t*/\n" 14083 "}", 14084 Tab)); 14085 EXPECT_EQ("{\n" 14086 "\t/*\n" 14087 " asdf\n" 14088 "\t*/\n" 14089 "}", 14090 format("{\n" 14091 "\t/*\n" 14092 " asdf\n" 14093 "\t*/\n" 14094 "}", 14095 Tab)); 14096 EXPECT_EQ("/* some\n" 14097 " comment */", 14098 format(" \t \t /* some\n" 14099 " \t \t comment */", 14100 Tab)); 14101 EXPECT_EQ("int a; /* some\n" 14102 " comment */", 14103 format(" \t \t int a; /* some\n" 14104 " \t \t comment */", 14105 Tab)); 14106 EXPECT_EQ("int a; /* some\n" 14107 "comment */", 14108 format(" \t \t int\ta; /* some\n" 14109 " \t \t comment */", 14110 Tab)); 14111 EXPECT_EQ("f(\"\t\t\"); /* some\n" 14112 " comment */", 14113 format(" \t \t f(\"\t\t\"); /* some\n" 14114 " \t \t comment */", 14115 Tab)); 14116 EXPECT_EQ("{\n" 14117 "\t/*\n" 14118 "\t * Comment\n" 14119 "\t */\n" 14120 "\tint i;\n" 14121 "}", 14122 format("{\n" 14123 "\t/*\n" 14124 "\t * Comment\n" 14125 "\t */\n" 14126 "\t int i;\n" 14127 "}", 14128 Tab)); 14129 Tab.TabWidth = 2; 14130 Tab.IndentWidth = 2; 14131 EXPECT_EQ("{\n" 14132 "\t/* aaaa\n" 14133 "\t\t bbbb */\n" 14134 "}", 14135 format("{\n" 14136 "/* aaaa\n" 14137 "\t bbbb */\n" 14138 "}", 14139 Tab)); 14140 EXPECT_EQ("{\n" 14141 "\t/*\n" 14142 "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14143 "\t\tbbbbbbbbbbbbb\n" 14144 "\t*/\n" 14145 "}", 14146 format("{\n" 14147 "/*\n" 14148 "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14149 "*/\n" 14150 "}", 14151 Tab)); 14152 Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 14153 Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 14154 Tab.TabWidth = 4; 14155 Tab.IndentWidth = 4; 14156 verifyFormat("class Assign {\n" 14157 "\tvoid f() {\n" 14158 "\t\tint x = 123;\n" 14159 "\t\tint random = 4;\n" 14160 "\t\tstd::string alphabet =\n" 14161 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 14162 "\t}\n" 14163 "};", 14164 Tab); 14165 14166 Tab.UseTab = FormatStyle::UT_AlignWithSpaces; 14167 Tab.TabWidth = 8; 14168 Tab.IndentWidth = 8; 14169 EXPECT_EQ("if (aaaaaaaa && // q\n" 14170 " bb) // w\n" 14171 "\t;", 14172 format("if (aaaaaaaa &&// q\n" 14173 "bb)// w\n" 14174 ";", 14175 Tab)); 14176 EXPECT_EQ("if (aaa && bbb) // w\n" 14177 "\t;", 14178 format("if(aaa&&bbb)// w\n" 14179 ";", 14180 Tab)); 14181 verifyFormat("class X {\n" 14182 "\tvoid f() {\n" 14183 "\t\tsomeFunction(parameter1,\n" 14184 "\t\t parameter2);\n" 14185 "\t}\n" 14186 "};", 14187 Tab); 14188 verifyFormat("#define A \\\n" 14189 "\tvoid f() { \\\n" 14190 "\t\tsomeFunction( \\\n" 14191 "\t\t parameter1, \\\n" 14192 "\t\t parameter2); \\\n" 14193 "\t}", 14194 Tab); 14195 Tab.TabWidth = 4; 14196 Tab.IndentWidth = 8; 14197 verifyFormat("class TabWidth4Indent8 {\n" 14198 "\t\tvoid f() {\n" 14199 "\t\t\t\tsomeFunction(parameter1,\n" 14200 "\t\t\t\t parameter2);\n" 14201 "\t\t}\n" 14202 "};", 14203 Tab); 14204 Tab.TabWidth = 4; 14205 Tab.IndentWidth = 4; 14206 verifyFormat("class TabWidth4Indent4 {\n" 14207 "\tvoid f() {\n" 14208 "\t\tsomeFunction(parameter1,\n" 14209 "\t\t parameter2);\n" 14210 "\t}\n" 14211 "};", 14212 Tab); 14213 Tab.TabWidth = 8; 14214 Tab.IndentWidth = 4; 14215 verifyFormat("class TabWidth8Indent4 {\n" 14216 " void f() {\n" 14217 "\tsomeFunction(parameter1,\n" 14218 "\t parameter2);\n" 14219 " }\n" 14220 "};", 14221 Tab); 14222 Tab.TabWidth = 8; 14223 Tab.IndentWidth = 8; 14224 EXPECT_EQ("/*\n" 14225 " a\t\tcomment\n" 14226 " in multiple lines\n" 14227 " */", 14228 format(" /*\t \t \n" 14229 " \t \t a\t\tcomment\t \t\n" 14230 " \t \t in multiple lines\t\n" 14231 " \t */", 14232 Tab)); 14233 verifyFormat("{\n" 14234 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14235 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14236 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14237 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14238 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14239 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 14240 "};", 14241 Tab); 14242 verifyFormat("enum AA {\n" 14243 "\ta1, // Force multiple lines\n" 14244 "\ta2,\n" 14245 "\ta3\n" 14246 "};", 14247 Tab); 14248 EXPECT_EQ("if (aaaaaaaa && // q\n" 14249 " bb) // w\n" 14250 "\t;", 14251 format("if (aaaaaaaa &&// q\n" 14252 "bb)// w\n" 14253 ";", 14254 Tab)); 14255 verifyFormat("class X {\n" 14256 "\tvoid f() {\n" 14257 "\t\tsomeFunction(parameter1,\n" 14258 "\t\t parameter2);\n" 14259 "\t}\n" 14260 "};", 14261 Tab); 14262 verifyFormat("{\n" 14263 "\tQ(\n" 14264 "\t {\n" 14265 "\t\t int a;\n" 14266 "\t\t someFunction(aaaaaaaa,\n" 14267 "\t\t bbbbbbb);\n" 14268 "\t },\n" 14269 "\t p);\n" 14270 "}", 14271 Tab); 14272 EXPECT_EQ("{\n" 14273 "\t/* aaaa\n" 14274 "\t bbbb */\n" 14275 "}", 14276 format("{\n" 14277 "/* aaaa\n" 14278 " bbbb */\n" 14279 "}", 14280 Tab)); 14281 EXPECT_EQ("{\n" 14282 "\t/*\n" 14283 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14284 "\t bbbbbbbbbbbbb\n" 14285 "\t*/\n" 14286 "}", 14287 format("{\n" 14288 "/*\n" 14289 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14290 "*/\n" 14291 "}", 14292 Tab)); 14293 EXPECT_EQ("{\n" 14294 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14295 "\t// bbbbbbbbbbbbb\n" 14296 "}", 14297 format("{\n" 14298 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14299 "}", 14300 Tab)); 14301 EXPECT_EQ("{\n" 14302 "\t/*\n" 14303 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14304 "\t bbbbbbbbbbbbb\n" 14305 "\t*/\n" 14306 "}", 14307 format("{\n" 14308 "\t/*\n" 14309 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14310 "\t*/\n" 14311 "}", 14312 Tab)); 14313 EXPECT_EQ("{\n" 14314 "\t/*\n" 14315 "\n" 14316 "\t*/\n" 14317 "}", 14318 format("{\n" 14319 "\t/*\n" 14320 "\n" 14321 "\t*/\n" 14322 "}", 14323 Tab)); 14324 EXPECT_EQ("{\n" 14325 "\t/*\n" 14326 " asdf\n" 14327 "\t*/\n" 14328 "}", 14329 format("{\n" 14330 "\t/*\n" 14331 " asdf\n" 14332 "\t*/\n" 14333 "}", 14334 Tab)); 14335 EXPECT_EQ("/* some\n" 14336 " comment */", 14337 format(" \t \t /* some\n" 14338 " \t \t comment */", 14339 Tab)); 14340 EXPECT_EQ("int a; /* some\n" 14341 " comment */", 14342 format(" \t \t int a; /* some\n" 14343 " \t \t comment */", 14344 Tab)); 14345 EXPECT_EQ("int a; /* some\n" 14346 "comment */", 14347 format(" \t \t int\ta; /* some\n" 14348 " \t \t comment */", 14349 Tab)); 14350 EXPECT_EQ("f(\"\t\t\"); /* some\n" 14351 " comment */", 14352 format(" \t \t f(\"\t\t\"); /* some\n" 14353 " \t \t comment */", 14354 Tab)); 14355 EXPECT_EQ("{\n" 14356 "\t/*\n" 14357 "\t * Comment\n" 14358 "\t */\n" 14359 "\tint i;\n" 14360 "}", 14361 format("{\n" 14362 "\t/*\n" 14363 "\t * Comment\n" 14364 "\t */\n" 14365 "\t int i;\n" 14366 "}", 14367 Tab)); 14368 Tab.TabWidth = 2; 14369 Tab.IndentWidth = 2; 14370 EXPECT_EQ("{\n" 14371 "\t/* aaaa\n" 14372 "\t bbbb */\n" 14373 "}", 14374 format("{\n" 14375 "/* aaaa\n" 14376 " bbbb */\n" 14377 "}", 14378 Tab)); 14379 EXPECT_EQ("{\n" 14380 "\t/*\n" 14381 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14382 "\t bbbbbbbbbbbbb\n" 14383 "\t*/\n" 14384 "}", 14385 format("{\n" 14386 "/*\n" 14387 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14388 "*/\n" 14389 "}", 14390 Tab)); 14391 Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 14392 Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 14393 Tab.TabWidth = 4; 14394 Tab.IndentWidth = 4; 14395 verifyFormat("class Assign {\n" 14396 "\tvoid f() {\n" 14397 "\t\tint x = 123;\n" 14398 "\t\tint random = 4;\n" 14399 "\t\tstd::string alphabet =\n" 14400 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 14401 "\t}\n" 14402 "};", 14403 Tab); 14404 Tab.AlignOperands = FormatStyle::OAS_Align; 14405 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n" 14406 " cccccccccccccccccccc;", 14407 Tab); 14408 // no alignment 14409 verifyFormat("int aaaaaaaaaa =\n" 14410 "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 14411 Tab); 14412 verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n" 14413 " : bbbbbbbbbbbbbb ? 222222222222222\n" 14414 " : 333333333333333;", 14415 Tab); 14416 Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 14417 Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator; 14418 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n" 14419 " + cccccccccccccccccccc;", 14420 Tab); 14421 } 14422 14423 TEST_F(FormatTest, ZeroTabWidth) { 14424 FormatStyle Tab = getLLVMStyleWithColumns(42); 14425 Tab.IndentWidth = 8; 14426 Tab.UseTab = FormatStyle::UT_Never; 14427 Tab.TabWidth = 0; 14428 EXPECT_EQ("void a(){\n" 14429 " // line starts with '\t'\n" 14430 "};", 14431 format("void a(){\n" 14432 "\t// line starts with '\t'\n" 14433 "};", 14434 Tab)); 14435 14436 EXPECT_EQ("void a(){\n" 14437 " // line starts with '\t'\n" 14438 "};", 14439 format("void a(){\n" 14440 "\t\t// line starts with '\t'\n" 14441 "};", 14442 Tab)); 14443 14444 Tab.UseTab = FormatStyle::UT_ForIndentation; 14445 EXPECT_EQ("void a(){\n" 14446 " // line starts with '\t'\n" 14447 "};", 14448 format("void a(){\n" 14449 "\t// line starts with '\t'\n" 14450 "};", 14451 Tab)); 14452 14453 EXPECT_EQ("void a(){\n" 14454 " // line starts with '\t'\n" 14455 "};", 14456 format("void a(){\n" 14457 "\t\t// line starts with '\t'\n" 14458 "};", 14459 Tab)); 14460 14461 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 14462 EXPECT_EQ("void a(){\n" 14463 " // line starts with '\t'\n" 14464 "};", 14465 format("void a(){\n" 14466 "\t// line starts with '\t'\n" 14467 "};", 14468 Tab)); 14469 14470 EXPECT_EQ("void a(){\n" 14471 " // line starts with '\t'\n" 14472 "};", 14473 format("void a(){\n" 14474 "\t\t// line starts with '\t'\n" 14475 "};", 14476 Tab)); 14477 14478 Tab.UseTab = FormatStyle::UT_AlignWithSpaces; 14479 EXPECT_EQ("void a(){\n" 14480 " // line starts with '\t'\n" 14481 "};", 14482 format("void a(){\n" 14483 "\t// line starts with '\t'\n" 14484 "};", 14485 Tab)); 14486 14487 EXPECT_EQ("void a(){\n" 14488 " // line starts with '\t'\n" 14489 "};", 14490 format("void a(){\n" 14491 "\t\t// line starts with '\t'\n" 14492 "};", 14493 Tab)); 14494 14495 Tab.UseTab = FormatStyle::UT_Always; 14496 EXPECT_EQ("void a(){\n" 14497 "// line starts with '\t'\n" 14498 "};", 14499 format("void a(){\n" 14500 "\t// line starts with '\t'\n" 14501 "};", 14502 Tab)); 14503 14504 EXPECT_EQ("void a(){\n" 14505 "// line starts with '\t'\n" 14506 "};", 14507 format("void a(){\n" 14508 "\t\t// line starts with '\t'\n" 14509 "};", 14510 Tab)); 14511 } 14512 14513 TEST_F(FormatTest, CalculatesOriginalColumn) { 14514 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14515 "q\"; /* some\n" 14516 " comment */", 14517 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14518 "q\"; /* some\n" 14519 " comment */", 14520 getLLVMStyle())); 14521 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 14522 "/* some\n" 14523 " comment */", 14524 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 14525 " /* some\n" 14526 " comment */", 14527 getLLVMStyle())); 14528 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14529 "qqq\n" 14530 "/* some\n" 14531 " comment */", 14532 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14533 "qqq\n" 14534 " /* some\n" 14535 " comment */", 14536 getLLVMStyle())); 14537 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14538 "wwww; /* some\n" 14539 " comment */", 14540 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14541 "wwww; /* some\n" 14542 " comment */", 14543 getLLVMStyle())); 14544 } 14545 14546 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 14547 FormatStyle NoSpace = getLLVMStyle(); 14548 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 14549 14550 verifyFormat("while(true)\n" 14551 " continue;", 14552 NoSpace); 14553 verifyFormat("for(;;)\n" 14554 " continue;", 14555 NoSpace); 14556 verifyFormat("if(true)\n" 14557 " f();\n" 14558 "else if(true)\n" 14559 " f();", 14560 NoSpace); 14561 verifyFormat("do {\n" 14562 " do_something();\n" 14563 "} while(something());", 14564 NoSpace); 14565 verifyFormat("switch(x) {\n" 14566 "default:\n" 14567 " break;\n" 14568 "}", 14569 NoSpace); 14570 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 14571 verifyFormat("size_t x = sizeof(x);", NoSpace); 14572 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 14573 verifyFormat("auto f(int x) -> typeof(x);", NoSpace); 14574 verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace); 14575 verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace); 14576 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 14577 verifyFormat("alignas(128) char a[128];", NoSpace); 14578 verifyFormat("size_t x = alignof(MyType);", NoSpace); 14579 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 14580 verifyFormat("int f() throw(Deprecated);", NoSpace); 14581 verifyFormat("typedef void (*cb)(int);", NoSpace); 14582 verifyFormat("T A::operator()();", NoSpace); 14583 verifyFormat("X A::operator++(T);", NoSpace); 14584 verifyFormat("auto lambda = []() { return 0; };", NoSpace); 14585 14586 FormatStyle Space = getLLVMStyle(); 14587 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 14588 14589 verifyFormat("int f ();", Space); 14590 verifyFormat("void f (int a, T b) {\n" 14591 " while (true)\n" 14592 " continue;\n" 14593 "}", 14594 Space); 14595 verifyFormat("if (true)\n" 14596 " f ();\n" 14597 "else if (true)\n" 14598 " f ();", 14599 Space); 14600 verifyFormat("do {\n" 14601 " do_something ();\n" 14602 "} while (something ());", 14603 Space); 14604 verifyFormat("switch (x) {\n" 14605 "default:\n" 14606 " break;\n" 14607 "}", 14608 Space); 14609 verifyFormat("A::A () : a (1) {}", Space); 14610 verifyFormat("void f () __attribute__ ((asdf));", Space); 14611 verifyFormat("*(&a + 1);\n" 14612 "&((&a)[1]);\n" 14613 "a[(b + c) * d];\n" 14614 "(((a + 1) * 2) + 3) * 4;", 14615 Space); 14616 verifyFormat("#define A(x) x", Space); 14617 verifyFormat("#define A (x) x", Space); 14618 verifyFormat("#if defined(x)\n" 14619 "#endif", 14620 Space); 14621 verifyFormat("auto i = std::make_unique<int> (5);", Space); 14622 verifyFormat("size_t x = sizeof (x);", Space); 14623 verifyFormat("auto f (int x) -> decltype (x);", Space); 14624 verifyFormat("auto f (int x) -> typeof (x);", Space); 14625 verifyFormat("auto f (int x) -> _Atomic (x);", Space); 14626 verifyFormat("auto f (int x) -> __underlying_type (x);", Space); 14627 verifyFormat("int f (T x) noexcept (x.create ());", Space); 14628 verifyFormat("alignas (128) char a[128];", Space); 14629 verifyFormat("size_t x = alignof (MyType);", Space); 14630 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 14631 verifyFormat("int f () throw (Deprecated);", Space); 14632 verifyFormat("typedef void (*cb) (int);", Space); 14633 // FIXME these tests regressed behaviour. 14634 // verifyFormat("T A::operator() ();", Space); 14635 // verifyFormat("X A::operator++ (T);", Space); 14636 verifyFormat("auto lambda = [] () { return 0; };", Space); 14637 verifyFormat("int x = int (y);", Space); 14638 14639 FormatStyle SomeSpace = getLLVMStyle(); 14640 SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses; 14641 14642 verifyFormat("[]() -> float {}", SomeSpace); 14643 verifyFormat("[] (auto foo) {}", SomeSpace); 14644 verifyFormat("[foo]() -> int {}", SomeSpace); 14645 verifyFormat("int f();", SomeSpace); 14646 verifyFormat("void f (int a, T b) {\n" 14647 " while (true)\n" 14648 " continue;\n" 14649 "}", 14650 SomeSpace); 14651 verifyFormat("if (true)\n" 14652 " f();\n" 14653 "else if (true)\n" 14654 " f();", 14655 SomeSpace); 14656 verifyFormat("do {\n" 14657 " do_something();\n" 14658 "} while (something());", 14659 SomeSpace); 14660 verifyFormat("switch (x) {\n" 14661 "default:\n" 14662 " break;\n" 14663 "}", 14664 SomeSpace); 14665 verifyFormat("A::A() : a (1) {}", SomeSpace); 14666 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace); 14667 verifyFormat("*(&a + 1);\n" 14668 "&((&a)[1]);\n" 14669 "a[(b + c) * d];\n" 14670 "(((a + 1) * 2) + 3) * 4;", 14671 SomeSpace); 14672 verifyFormat("#define A(x) x", SomeSpace); 14673 verifyFormat("#define A (x) x", SomeSpace); 14674 verifyFormat("#if defined(x)\n" 14675 "#endif", 14676 SomeSpace); 14677 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace); 14678 verifyFormat("size_t x = sizeof (x);", SomeSpace); 14679 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace); 14680 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace); 14681 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace); 14682 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace); 14683 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace); 14684 verifyFormat("alignas (128) char a[128];", SomeSpace); 14685 verifyFormat("size_t x = alignof (MyType);", SomeSpace); 14686 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", 14687 SomeSpace); 14688 verifyFormat("int f() throw (Deprecated);", SomeSpace); 14689 verifyFormat("typedef void (*cb) (int);", SomeSpace); 14690 verifyFormat("T A::operator()();", SomeSpace); 14691 // FIXME these tests regressed behaviour. 14692 // verifyFormat("X A::operator++ (T);", SomeSpace); 14693 verifyFormat("int x = int (y);", SomeSpace); 14694 verifyFormat("auto lambda = []() { return 0; };", SomeSpace); 14695 14696 FormatStyle SpaceControlStatements = getLLVMStyle(); 14697 SpaceControlStatements.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14698 SpaceControlStatements.SpaceBeforeParensOptions.AfterControlStatements = true; 14699 14700 verifyFormat("while (true)\n" 14701 " continue;", 14702 SpaceControlStatements); 14703 verifyFormat("if (true)\n" 14704 " f();\n" 14705 "else if (true)\n" 14706 " f();", 14707 SpaceControlStatements); 14708 verifyFormat("for (;;) {\n" 14709 " do_something();\n" 14710 "}", 14711 SpaceControlStatements); 14712 verifyFormat("do {\n" 14713 " do_something();\n" 14714 "} while (something());", 14715 SpaceControlStatements); 14716 verifyFormat("switch (x) {\n" 14717 "default:\n" 14718 " break;\n" 14719 "}", 14720 SpaceControlStatements); 14721 14722 FormatStyle SpaceFuncDecl = getLLVMStyle(); 14723 SpaceFuncDecl.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14724 SpaceFuncDecl.SpaceBeforeParensOptions.AfterFunctionDeclarationName = true; 14725 14726 verifyFormat("int f ();", SpaceFuncDecl); 14727 verifyFormat("void f(int a, T b) {}", SpaceFuncDecl); 14728 verifyFormat("A::A() : a(1) {}", SpaceFuncDecl); 14729 verifyFormat("void f () __attribute__((asdf));", SpaceFuncDecl); 14730 verifyFormat("#define A(x) x", SpaceFuncDecl); 14731 verifyFormat("#define A (x) x", SpaceFuncDecl); 14732 verifyFormat("#if defined(x)\n" 14733 "#endif", 14734 SpaceFuncDecl); 14735 verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDecl); 14736 verifyFormat("size_t x = sizeof(x);", SpaceFuncDecl); 14737 verifyFormat("auto f (int x) -> decltype(x);", SpaceFuncDecl); 14738 verifyFormat("auto f (int x) -> typeof(x);", SpaceFuncDecl); 14739 verifyFormat("auto f (int x) -> _Atomic(x);", SpaceFuncDecl); 14740 verifyFormat("auto f (int x) -> __underlying_type(x);", SpaceFuncDecl); 14741 verifyFormat("int f (T x) noexcept(x.create());", SpaceFuncDecl); 14742 verifyFormat("alignas(128) char a[128];", SpaceFuncDecl); 14743 verifyFormat("size_t x = alignof(MyType);", SpaceFuncDecl); 14744 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", 14745 SpaceFuncDecl); 14746 verifyFormat("int f () throw(Deprecated);", SpaceFuncDecl); 14747 verifyFormat("typedef void (*cb)(int);", SpaceFuncDecl); 14748 // FIXME these tests regressed behaviour. 14749 // verifyFormat("T A::operator() ();", SpaceFuncDecl); 14750 // verifyFormat("X A::operator++ (T);", SpaceFuncDecl); 14751 verifyFormat("T A::operator()() {}", SpaceFuncDecl); 14752 verifyFormat("auto lambda = []() { return 0; };", SpaceFuncDecl); 14753 verifyFormat("int x = int(y);", SpaceFuncDecl); 14754 verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}", 14755 SpaceFuncDecl); 14756 14757 FormatStyle SpaceFuncDef = getLLVMStyle(); 14758 SpaceFuncDef.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14759 SpaceFuncDef.SpaceBeforeParensOptions.AfterFunctionDefinitionName = true; 14760 14761 verifyFormat("int f();", SpaceFuncDef); 14762 verifyFormat("void f (int a, T b) {}", SpaceFuncDef); 14763 verifyFormat("A::A() : a(1) {}", SpaceFuncDef); 14764 verifyFormat("void f() __attribute__((asdf));", SpaceFuncDef); 14765 verifyFormat("#define A(x) x", SpaceFuncDef); 14766 verifyFormat("#define A (x) x", SpaceFuncDef); 14767 verifyFormat("#if defined(x)\n" 14768 "#endif", 14769 SpaceFuncDef); 14770 verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDef); 14771 verifyFormat("size_t x = sizeof(x);", SpaceFuncDef); 14772 verifyFormat("auto f(int x) -> decltype(x);", SpaceFuncDef); 14773 verifyFormat("auto f(int x) -> typeof(x);", SpaceFuncDef); 14774 verifyFormat("auto f(int x) -> _Atomic(x);", SpaceFuncDef); 14775 verifyFormat("auto f(int x) -> __underlying_type(x);", SpaceFuncDef); 14776 verifyFormat("int f(T x) noexcept(x.create());", SpaceFuncDef); 14777 verifyFormat("alignas(128) char a[128];", SpaceFuncDef); 14778 verifyFormat("size_t x = alignof(MyType);", SpaceFuncDef); 14779 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", 14780 SpaceFuncDef); 14781 verifyFormat("int f() throw(Deprecated);", SpaceFuncDef); 14782 verifyFormat("typedef void (*cb)(int);", SpaceFuncDef); 14783 verifyFormat("T A::operator()();", SpaceFuncDef); 14784 verifyFormat("X A::operator++(T);", SpaceFuncDef); 14785 // verifyFormat("T A::operator() () {}", SpaceFuncDef); 14786 verifyFormat("auto lambda = [] () { return 0; };", SpaceFuncDef); 14787 verifyFormat("int x = int(y);", SpaceFuncDef); 14788 verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}", 14789 SpaceFuncDef); 14790 14791 FormatStyle SpaceIfMacros = getLLVMStyle(); 14792 SpaceIfMacros.IfMacros.clear(); 14793 SpaceIfMacros.IfMacros.push_back("MYIF"); 14794 SpaceIfMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14795 SpaceIfMacros.SpaceBeforeParensOptions.AfterIfMacros = true; 14796 verifyFormat("MYIF (a)\n return;", SpaceIfMacros); 14797 verifyFormat("MYIF (a)\n return;\nelse MYIF (b)\n return;", SpaceIfMacros); 14798 verifyFormat("MYIF (a)\n return;\nelse\n return;", SpaceIfMacros); 14799 14800 FormatStyle SpaceForeachMacros = getLLVMStyle(); 14801 EXPECT_EQ(SpaceForeachMacros.AllowShortBlocksOnASingleLine, 14802 FormatStyle::SBS_Never); 14803 EXPECT_EQ(SpaceForeachMacros.AllowShortLoopsOnASingleLine, false); 14804 SpaceForeachMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14805 SpaceForeachMacros.SpaceBeforeParensOptions.AfterForeachMacros = true; 14806 verifyFormat("for (;;) {\n" 14807 "}", 14808 SpaceForeachMacros); 14809 verifyFormat("foreach (Item *item, itemlist) {\n" 14810 "}", 14811 SpaceForeachMacros); 14812 verifyFormat("Q_FOREACH (Item *item, itemlist) {\n" 14813 "}", 14814 SpaceForeachMacros); 14815 verifyFormat("BOOST_FOREACH (Item *item, itemlist) {\n" 14816 "}", 14817 SpaceForeachMacros); 14818 verifyFormat("UNKNOWN_FOREACH(Item *item, itemlist) {}", SpaceForeachMacros); 14819 14820 FormatStyle SomeSpace2 = getLLVMStyle(); 14821 SomeSpace2.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14822 SomeSpace2.SpaceBeforeParensOptions.BeforeNonEmptyParentheses = true; 14823 verifyFormat("[]() -> float {}", SomeSpace2); 14824 verifyFormat("[] (auto foo) {}", SomeSpace2); 14825 verifyFormat("[foo]() -> int {}", SomeSpace2); 14826 verifyFormat("int f();", SomeSpace2); 14827 verifyFormat("void f (int a, T b) {\n" 14828 " while (true)\n" 14829 " continue;\n" 14830 "}", 14831 SomeSpace2); 14832 verifyFormat("if (true)\n" 14833 " f();\n" 14834 "else if (true)\n" 14835 " f();", 14836 SomeSpace2); 14837 verifyFormat("do {\n" 14838 " do_something();\n" 14839 "} while (something());", 14840 SomeSpace2); 14841 verifyFormat("switch (x) {\n" 14842 "default:\n" 14843 " break;\n" 14844 "}", 14845 SomeSpace2); 14846 verifyFormat("A::A() : a (1) {}", SomeSpace2); 14847 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace2); 14848 verifyFormat("*(&a + 1);\n" 14849 "&((&a)[1]);\n" 14850 "a[(b + c) * d];\n" 14851 "(((a + 1) * 2) + 3) * 4;", 14852 SomeSpace2); 14853 verifyFormat("#define A(x) x", SomeSpace2); 14854 verifyFormat("#define A (x) x", SomeSpace2); 14855 verifyFormat("#if defined(x)\n" 14856 "#endif", 14857 SomeSpace2); 14858 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace2); 14859 verifyFormat("size_t x = sizeof (x);", SomeSpace2); 14860 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace2); 14861 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace2); 14862 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace2); 14863 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace2); 14864 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace2); 14865 verifyFormat("alignas (128) char a[128];", SomeSpace2); 14866 verifyFormat("size_t x = alignof (MyType);", SomeSpace2); 14867 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", 14868 SomeSpace2); 14869 verifyFormat("int f() throw (Deprecated);", SomeSpace2); 14870 verifyFormat("typedef void (*cb) (int);", SomeSpace2); 14871 verifyFormat("T A::operator()();", SomeSpace2); 14872 // verifyFormat("X A::operator++ (T);", SomeSpace2); 14873 verifyFormat("int x = int (y);", SomeSpace2); 14874 verifyFormat("auto lambda = []() { return 0; };", SomeSpace2); 14875 14876 FormatStyle SpaceAfterOverloadedOperator = getLLVMStyle(); 14877 SpaceAfterOverloadedOperator.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14878 SpaceAfterOverloadedOperator.SpaceBeforeParensOptions 14879 .AfterOverloadedOperator = true; 14880 14881 verifyFormat("auto operator++ () -> int;", SpaceAfterOverloadedOperator); 14882 verifyFormat("X A::operator++ ();", SpaceAfterOverloadedOperator); 14883 verifyFormat("some_object.operator++ ();", SpaceAfterOverloadedOperator); 14884 verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator); 14885 14886 SpaceAfterOverloadedOperator.SpaceBeforeParensOptions 14887 .AfterOverloadedOperator = false; 14888 14889 verifyFormat("auto operator++() -> int;", SpaceAfterOverloadedOperator); 14890 verifyFormat("X A::operator++();", SpaceAfterOverloadedOperator); 14891 verifyFormat("some_object.operator++();", SpaceAfterOverloadedOperator); 14892 verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator); 14893 } 14894 14895 TEST_F(FormatTest, SpaceAfterLogicalNot) { 14896 FormatStyle Spaces = getLLVMStyle(); 14897 Spaces.SpaceAfterLogicalNot = true; 14898 14899 verifyFormat("bool x = ! y", Spaces); 14900 verifyFormat("if (! isFailure())", Spaces); 14901 verifyFormat("if (! (a && b))", Spaces); 14902 verifyFormat("\"Error!\"", Spaces); 14903 verifyFormat("! ! x", Spaces); 14904 } 14905 14906 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 14907 FormatStyle Spaces = getLLVMStyle(); 14908 14909 Spaces.SpacesInParentheses = true; 14910 verifyFormat("do_something( ::globalVar );", Spaces); 14911 verifyFormat("call( x, y, z );", Spaces); 14912 verifyFormat("call();", Spaces); 14913 verifyFormat("std::function<void( int, int )> callback;", Spaces); 14914 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 14915 Spaces); 14916 verifyFormat("while ( (bool)1 )\n" 14917 " continue;", 14918 Spaces); 14919 verifyFormat("for ( ;; )\n" 14920 " continue;", 14921 Spaces); 14922 verifyFormat("if ( true )\n" 14923 " f();\n" 14924 "else if ( true )\n" 14925 " f();", 14926 Spaces); 14927 verifyFormat("do {\n" 14928 " do_something( (int)i );\n" 14929 "} while ( something() );", 14930 Spaces); 14931 verifyFormat("switch ( x ) {\n" 14932 "default:\n" 14933 " break;\n" 14934 "}", 14935 Spaces); 14936 14937 Spaces.SpacesInParentheses = false; 14938 Spaces.SpacesInCStyleCastParentheses = true; 14939 verifyFormat("Type *A = ( Type * )P;", Spaces); 14940 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 14941 verifyFormat("x = ( int32 )y;", Spaces); 14942 verifyFormat("int a = ( int )(2.0f);", Spaces); 14943 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 14944 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 14945 verifyFormat("#define x (( int )-1)", Spaces); 14946 14947 // Run the first set of tests again with: 14948 Spaces.SpacesInParentheses = false; 14949 Spaces.SpaceInEmptyParentheses = true; 14950 Spaces.SpacesInCStyleCastParentheses = true; 14951 verifyFormat("call(x, y, z);", Spaces); 14952 verifyFormat("call( );", Spaces); 14953 verifyFormat("std::function<void(int, int)> callback;", Spaces); 14954 verifyFormat("while (( bool )1)\n" 14955 " continue;", 14956 Spaces); 14957 verifyFormat("for (;;)\n" 14958 " continue;", 14959 Spaces); 14960 verifyFormat("if (true)\n" 14961 " f( );\n" 14962 "else if (true)\n" 14963 " f( );", 14964 Spaces); 14965 verifyFormat("do {\n" 14966 " do_something(( int )i);\n" 14967 "} while (something( ));", 14968 Spaces); 14969 verifyFormat("switch (x) {\n" 14970 "default:\n" 14971 " break;\n" 14972 "}", 14973 Spaces); 14974 14975 // Run the first set of tests again with: 14976 Spaces.SpaceAfterCStyleCast = true; 14977 verifyFormat("call(x, y, z);", Spaces); 14978 verifyFormat("call( );", Spaces); 14979 verifyFormat("std::function<void(int, int)> callback;", Spaces); 14980 verifyFormat("while (( bool ) 1)\n" 14981 " continue;", 14982 Spaces); 14983 verifyFormat("for (;;)\n" 14984 " continue;", 14985 Spaces); 14986 verifyFormat("if (true)\n" 14987 " f( );\n" 14988 "else if (true)\n" 14989 " f( );", 14990 Spaces); 14991 verifyFormat("do {\n" 14992 " do_something(( int ) i);\n" 14993 "} while (something( ));", 14994 Spaces); 14995 verifyFormat("switch (x) {\n" 14996 "default:\n" 14997 " break;\n" 14998 "}", 14999 Spaces); 15000 verifyFormat("#define CONF_BOOL(x) ( bool * ) ( void * ) (x)", Spaces); 15001 verifyFormat("#define CONF_BOOL(x) ( bool * ) (x)", Spaces); 15002 verifyFormat("#define CONF_BOOL(x) ( bool ) (x)", Spaces); 15003 verifyFormat("bool *y = ( bool * ) ( void * ) (x);", Spaces); 15004 verifyFormat("bool *y = ( bool * ) (x);", Spaces); 15005 15006 // Run subset of tests again with: 15007 Spaces.SpacesInCStyleCastParentheses = false; 15008 Spaces.SpaceAfterCStyleCast = true; 15009 verifyFormat("while ((bool) 1)\n" 15010 " continue;", 15011 Spaces); 15012 verifyFormat("do {\n" 15013 " do_something((int) i);\n" 15014 "} while (something( ));", 15015 Spaces); 15016 15017 verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces); 15018 verifyFormat("size_t idx = (size_t) a;", Spaces); 15019 verifyFormat("size_t idx = (size_t) (a - 1);", Spaces); 15020 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); 15021 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); 15022 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); 15023 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); 15024 verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (x)", Spaces); 15025 verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (int) (x)", Spaces); 15026 verifyFormat("bool *y = (bool *) (void *) (x);", Spaces); 15027 verifyFormat("bool *y = (bool *) (void *) (int) (x);", Spaces); 15028 verifyFormat("bool *y = (bool *) (void *) (int) foo(x);", Spaces); 15029 Spaces.ColumnLimit = 80; 15030 Spaces.IndentWidth = 4; 15031 Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 15032 verifyFormat("void foo( ) {\n" 15033 " size_t foo = (*(function))(\n" 15034 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " 15035 "BarrrrrrrrrrrrLong,\n" 15036 " FoooooooooLooooong);\n" 15037 "}", 15038 Spaces); 15039 Spaces.SpaceAfterCStyleCast = false; 15040 verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces); 15041 verifyFormat("size_t idx = (size_t)a;", Spaces); 15042 verifyFormat("size_t idx = (size_t)(a - 1);", Spaces); 15043 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); 15044 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); 15045 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); 15046 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); 15047 15048 verifyFormat("void foo( ) {\n" 15049 " size_t foo = (*(function))(\n" 15050 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " 15051 "BarrrrrrrrrrrrLong,\n" 15052 " FoooooooooLooooong);\n" 15053 "}", 15054 Spaces); 15055 } 15056 15057 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 15058 verifyFormat("int a[5];"); 15059 verifyFormat("a[3] += 42;"); 15060 15061 FormatStyle Spaces = getLLVMStyle(); 15062 Spaces.SpacesInSquareBrackets = true; 15063 // Not lambdas. 15064 verifyFormat("int a[ 5 ];", Spaces); 15065 verifyFormat("a[ 3 ] += 42;", Spaces); 15066 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 15067 verifyFormat("double &operator[](int i) { return 0; }\n" 15068 "int i;", 15069 Spaces); 15070 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 15071 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 15072 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 15073 // Lambdas. 15074 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 15075 verifyFormat("return [ i, args... ] {};", Spaces); 15076 verifyFormat("int foo = [ &bar ]() {};", Spaces); 15077 verifyFormat("int foo = [ = ]() {};", Spaces); 15078 verifyFormat("int foo = [ & ]() {};", Spaces); 15079 verifyFormat("int foo = [ =, &bar ]() {};", Spaces); 15080 verifyFormat("int foo = [ &bar, = ]() {};", Spaces); 15081 } 15082 15083 TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) { 15084 FormatStyle NoSpaceStyle = getLLVMStyle(); 15085 verifyFormat("int a[5];", NoSpaceStyle); 15086 verifyFormat("a[3] += 42;", NoSpaceStyle); 15087 15088 verifyFormat("int a[1];", NoSpaceStyle); 15089 verifyFormat("int 1 [a];", NoSpaceStyle); 15090 verifyFormat("int a[1][2];", NoSpaceStyle); 15091 verifyFormat("a[7] = 5;", NoSpaceStyle); 15092 verifyFormat("int a = (f())[23];", NoSpaceStyle); 15093 verifyFormat("f([] {})", NoSpaceStyle); 15094 15095 FormatStyle Space = getLLVMStyle(); 15096 Space.SpaceBeforeSquareBrackets = true; 15097 verifyFormat("int c = []() -> int { return 2; }();\n", Space); 15098 verifyFormat("return [i, args...] {};", Space); 15099 15100 verifyFormat("int a [5];", Space); 15101 verifyFormat("a [3] += 42;", Space); 15102 verifyFormat("constexpr char hello []{\"hello\"};", Space); 15103 verifyFormat("double &operator[](int i) { return 0; }\n" 15104 "int i;", 15105 Space); 15106 verifyFormat("std::unique_ptr<int []> foo() {}", Space); 15107 verifyFormat("int i = a [a][a]->f();", Space); 15108 verifyFormat("int i = (*b) [a]->f();", Space); 15109 15110 verifyFormat("int a [1];", Space); 15111 verifyFormat("int 1 [a];", Space); 15112 verifyFormat("int a [1][2];", Space); 15113 verifyFormat("a [7] = 5;", Space); 15114 verifyFormat("int a = (f()) [23];", Space); 15115 verifyFormat("f([] {})", Space); 15116 } 15117 15118 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 15119 verifyFormat("int a = 5;"); 15120 verifyFormat("a += 42;"); 15121 verifyFormat("a or_eq 8;"); 15122 15123 FormatStyle Spaces = getLLVMStyle(); 15124 Spaces.SpaceBeforeAssignmentOperators = false; 15125 verifyFormat("int a= 5;", Spaces); 15126 verifyFormat("a+= 42;", Spaces); 15127 verifyFormat("a or_eq 8;", Spaces); 15128 } 15129 15130 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) { 15131 verifyFormat("class Foo : public Bar {};"); 15132 verifyFormat("Foo::Foo() : foo(1) {}"); 15133 verifyFormat("for (auto a : b) {\n}"); 15134 verifyFormat("int x = a ? b : c;"); 15135 verifyFormat("{\n" 15136 "label0:\n" 15137 " int x = 0;\n" 15138 "}"); 15139 verifyFormat("switch (x) {\n" 15140 "case 1:\n" 15141 "default:\n" 15142 "}"); 15143 verifyFormat("switch (allBraces) {\n" 15144 "case 1: {\n" 15145 " break;\n" 15146 "}\n" 15147 "case 2: {\n" 15148 " [[fallthrough]];\n" 15149 "}\n" 15150 "default: {\n" 15151 " break;\n" 15152 "}\n" 15153 "}"); 15154 15155 FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30); 15156 CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false; 15157 verifyFormat("class Foo : public Bar {};", CtorInitializerStyle); 15158 verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle); 15159 verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle); 15160 verifyFormat("int x = a ? b : c;", CtorInitializerStyle); 15161 verifyFormat("{\n" 15162 "label1:\n" 15163 " int x = 0;\n" 15164 "}", 15165 CtorInitializerStyle); 15166 verifyFormat("switch (x) {\n" 15167 "case 1:\n" 15168 "default:\n" 15169 "}", 15170 CtorInitializerStyle); 15171 verifyFormat("switch (allBraces) {\n" 15172 "case 1: {\n" 15173 " break;\n" 15174 "}\n" 15175 "case 2: {\n" 15176 " [[fallthrough]];\n" 15177 "}\n" 15178 "default: {\n" 15179 " break;\n" 15180 "}\n" 15181 "}", 15182 CtorInitializerStyle); 15183 CtorInitializerStyle.BreakConstructorInitializers = 15184 FormatStyle::BCIS_AfterColon; 15185 verifyFormat("Fooooooooooo::Fooooooooooo():\n" 15186 " aaaaaaaaaaaaaaaa(1),\n" 15187 " bbbbbbbbbbbbbbbb(2) {}", 15188 CtorInitializerStyle); 15189 CtorInitializerStyle.BreakConstructorInitializers = 15190 FormatStyle::BCIS_BeforeComma; 15191 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 15192 " : aaaaaaaaaaaaaaaa(1)\n" 15193 " , bbbbbbbbbbbbbbbb(2) {}", 15194 CtorInitializerStyle); 15195 CtorInitializerStyle.BreakConstructorInitializers = 15196 FormatStyle::BCIS_BeforeColon; 15197 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 15198 " : aaaaaaaaaaaaaaaa(1),\n" 15199 " bbbbbbbbbbbbbbbb(2) {}", 15200 CtorInitializerStyle); 15201 CtorInitializerStyle.ConstructorInitializerIndentWidth = 0; 15202 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 15203 ": aaaaaaaaaaaaaaaa(1),\n" 15204 " bbbbbbbbbbbbbbbb(2) {}", 15205 CtorInitializerStyle); 15206 15207 FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30); 15208 InheritanceStyle.SpaceBeforeInheritanceColon = false; 15209 verifyFormat("class Foo: public Bar {};", InheritanceStyle); 15210 verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle); 15211 verifyFormat("for (auto a : b) {\n}", InheritanceStyle); 15212 verifyFormat("int x = a ? b : c;", InheritanceStyle); 15213 verifyFormat("{\n" 15214 "label2:\n" 15215 " int x = 0;\n" 15216 "}", 15217 InheritanceStyle); 15218 verifyFormat("switch (x) {\n" 15219 "case 1:\n" 15220 "default:\n" 15221 "}", 15222 InheritanceStyle); 15223 verifyFormat("switch (allBraces) {\n" 15224 "case 1: {\n" 15225 " break;\n" 15226 "}\n" 15227 "case 2: {\n" 15228 " [[fallthrough]];\n" 15229 "}\n" 15230 "default: {\n" 15231 " break;\n" 15232 "}\n" 15233 "}", 15234 InheritanceStyle); 15235 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma; 15236 verifyFormat("class Foooooooooooooooooooooo\n" 15237 " : public aaaaaaaaaaaaaaaaaa,\n" 15238 " public bbbbbbbbbbbbbbbbbb {\n" 15239 "}", 15240 InheritanceStyle); 15241 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon; 15242 verifyFormat("class Foooooooooooooooooooooo:\n" 15243 " public aaaaaaaaaaaaaaaaaa,\n" 15244 " public bbbbbbbbbbbbbbbbbb {\n" 15245 "}", 15246 InheritanceStyle); 15247 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 15248 verifyFormat("class Foooooooooooooooooooooo\n" 15249 " : public aaaaaaaaaaaaaaaaaa\n" 15250 " , public bbbbbbbbbbbbbbbbbb {\n" 15251 "}", 15252 InheritanceStyle); 15253 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 15254 verifyFormat("class Foooooooooooooooooooooo\n" 15255 " : public aaaaaaaaaaaaaaaaaa,\n" 15256 " public bbbbbbbbbbbbbbbbbb {\n" 15257 "}", 15258 InheritanceStyle); 15259 InheritanceStyle.ConstructorInitializerIndentWidth = 0; 15260 verifyFormat("class Foooooooooooooooooooooo\n" 15261 ": public aaaaaaaaaaaaaaaaaa,\n" 15262 " public bbbbbbbbbbbbbbbbbb {}", 15263 InheritanceStyle); 15264 15265 FormatStyle ForLoopStyle = getLLVMStyle(); 15266 ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false; 15267 verifyFormat("class Foo : public Bar {};", ForLoopStyle); 15268 verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle); 15269 verifyFormat("for (auto a: b) {\n}", ForLoopStyle); 15270 verifyFormat("int x = a ? b : c;", ForLoopStyle); 15271 verifyFormat("{\n" 15272 "label2:\n" 15273 " int x = 0;\n" 15274 "}", 15275 ForLoopStyle); 15276 verifyFormat("switch (x) {\n" 15277 "case 1:\n" 15278 "default:\n" 15279 "}", 15280 ForLoopStyle); 15281 verifyFormat("switch (allBraces) {\n" 15282 "case 1: {\n" 15283 " break;\n" 15284 "}\n" 15285 "case 2: {\n" 15286 " [[fallthrough]];\n" 15287 "}\n" 15288 "default: {\n" 15289 " break;\n" 15290 "}\n" 15291 "}", 15292 ForLoopStyle); 15293 15294 FormatStyle CaseStyle = getLLVMStyle(); 15295 CaseStyle.SpaceBeforeCaseColon = true; 15296 verifyFormat("class Foo : public Bar {};", CaseStyle); 15297 verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle); 15298 verifyFormat("for (auto a : b) {\n}", CaseStyle); 15299 verifyFormat("int x = a ? b : c;", CaseStyle); 15300 verifyFormat("switch (x) {\n" 15301 "case 1 :\n" 15302 "default :\n" 15303 "}", 15304 CaseStyle); 15305 verifyFormat("switch (allBraces) {\n" 15306 "case 1 : {\n" 15307 " break;\n" 15308 "}\n" 15309 "case 2 : {\n" 15310 " [[fallthrough]];\n" 15311 "}\n" 15312 "default : {\n" 15313 " break;\n" 15314 "}\n" 15315 "}", 15316 CaseStyle); 15317 15318 FormatStyle NoSpaceStyle = getLLVMStyle(); 15319 EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false); 15320 NoSpaceStyle.SpaceBeforeCtorInitializerColon = false; 15321 NoSpaceStyle.SpaceBeforeInheritanceColon = false; 15322 NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 15323 verifyFormat("class Foo: public Bar {};", NoSpaceStyle); 15324 verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle); 15325 verifyFormat("for (auto a: b) {\n}", NoSpaceStyle); 15326 verifyFormat("int x = a ? b : c;", NoSpaceStyle); 15327 verifyFormat("{\n" 15328 "label3:\n" 15329 " int x = 0;\n" 15330 "}", 15331 NoSpaceStyle); 15332 verifyFormat("switch (x) {\n" 15333 "case 1:\n" 15334 "default:\n" 15335 "}", 15336 NoSpaceStyle); 15337 verifyFormat("switch (allBraces) {\n" 15338 "case 1: {\n" 15339 " break;\n" 15340 "}\n" 15341 "case 2: {\n" 15342 " [[fallthrough]];\n" 15343 "}\n" 15344 "default: {\n" 15345 " break;\n" 15346 "}\n" 15347 "}", 15348 NoSpaceStyle); 15349 15350 FormatStyle InvertedSpaceStyle = getLLVMStyle(); 15351 InvertedSpaceStyle.SpaceBeforeCaseColon = true; 15352 InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false; 15353 InvertedSpaceStyle.SpaceBeforeInheritanceColon = false; 15354 InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 15355 verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle); 15356 verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle); 15357 verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle); 15358 verifyFormat("int x = a ? b : c;", InvertedSpaceStyle); 15359 verifyFormat("{\n" 15360 "label3:\n" 15361 " int x = 0;\n" 15362 "}", 15363 InvertedSpaceStyle); 15364 verifyFormat("switch (x) {\n" 15365 "case 1 :\n" 15366 "case 2 : {\n" 15367 " break;\n" 15368 "}\n" 15369 "default :\n" 15370 " break;\n" 15371 "}", 15372 InvertedSpaceStyle); 15373 verifyFormat("switch (allBraces) {\n" 15374 "case 1 : {\n" 15375 " break;\n" 15376 "}\n" 15377 "case 2 : {\n" 15378 " [[fallthrough]];\n" 15379 "}\n" 15380 "default : {\n" 15381 " break;\n" 15382 "}\n" 15383 "}", 15384 InvertedSpaceStyle); 15385 } 15386 15387 TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) { 15388 FormatStyle Style = getLLVMStyle(); 15389 15390 Style.PointerAlignment = FormatStyle::PAS_Left; 15391 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; 15392 verifyFormat("void* const* x = NULL;", Style); 15393 15394 #define verifyQualifierSpaces(Code, Pointers, Qualifiers) \ 15395 do { \ 15396 Style.PointerAlignment = FormatStyle::Pointers; \ 15397 Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers; \ 15398 verifyFormat(Code, Style); \ 15399 } while (false) 15400 15401 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default); 15402 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default); 15403 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default); 15404 15405 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before); 15406 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before); 15407 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before); 15408 15409 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After); 15410 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After); 15411 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After); 15412 15413 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both); 15414 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both); 15415 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both); 15416 15417 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default); 15418 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, 15419 SAPQ_Default); 15420 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 15421 SAPQ_Default); 15422 15423 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before); 15424 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, 15425 SAPQ_Before); 15426 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 15427 SAPQ_Before); 15428 15429 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After); 15430 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After); 15431 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 15432 SAPQ_After); 15433 15434 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both); 15435 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both); 15436 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both); 15437 15438 #undef verifyQualifierSpaces 15439 15440 FormatStyle Spaces = getLLVMStyle(); 15441 Spaces.AttributeMacros.push_back("qualified"); 15442 Spaces.PointerAlignment = FormatStyle::PAS_Right; 15443 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; 15444 verifyFormat("SomeType *volatile *a = NULL;", Spaces); 15445 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces); 15446 verifyFormat("std::vector<SomeType *const *> x;", Spaces); 15447 verifyFormat("std::vector<SomeType *qualified *> x;", Spaces); 15448 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 15449 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before; 15450 verifyFormat("SomeType * volatile *a = NULL;", Spaces); 15451 verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces); 15452 verifyFormat("std::vector<SomeType * const *> x;", Spaces); 15453 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces); 15454 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 15455 15456 // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left. 15457 Spaces.PointerAlignment = FormatStyle::PAS_Left; 15458 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before; 15459 verifyFormat("SomeType* volatile* a = NULL;", Spaces); 15460 verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces); 15461 verifyFormat("std::vector<SomeType* const*> x;", Spaces); 15462 verifyFormat("std::vector<SomeType* qualified*> x;", Spaces); 15463 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 15464 // However, setting it to SAPQ_After should add spaces after __attribute, etc. 15465 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After; 15466 verifyFormat("SomeType* volatile * a = NULL;", Spaces); 15467 verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces); 15468 verifyFormat("std::vector<SomeType* const *> x;", Spaces); 15469 verifyFormat("std::vector<SomeType* qualified *> x;", Spaces); 15470 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 15471 15472 // PAS_Middle should not have any noticeable changes even for SAPQ_Both 15473 Spaces.PointerAlignment = FormatStyle::PAS_Middle; 15474 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After; 15475 verifyFormat("SomeType * volatile * a = NULL;", Spaces); 15476 verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces); 15477 verifyFormat("std::vector<SomeType * const *> x;", Spaces); 15478 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces); 15479 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 15480 } 15481 15482 TEST_F(FormatTest, AlignConsecutiveMacros) { 15483 FormatStyle Style = getLLVMStyle(); 15484 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 15485 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 15486 Style.AlignConsecutiveMacros = FormatStyle::ACS_None; 15487 15488 verifyFormat("#define a 3\n" 15489 "#define bbbb 4\n" 15490 "#define ccc (5)", 15491 Style); 15492 15493 verifyFormat("#define f(x) (x * x)\n" 15494 "#define fff(x, y, z) (x * y + z)\n" 15495 "#define ffff(x, y) (x - y)", 15496 Style); 15497 15498 verifyFormat("#define foo(x, y) (x + y)\n" 15499 "#define bar (5, 6)(2 + 2)", 15500 Style); 15501 15502 verifyFormat("#define a 3\n" 15503 "#define bbbb 4\n" 15504 "#define ccc (5)\n" 15505 "#define f(x) (x * x)\n" 15506 "#define fff(x, y, z) (x * y + z)\n" 15507 "#define ffff(x, y) (x - y)", 15508 Style); 15509 15510 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15511 verifyFormat("#define a 3\n" 15512 "#define bbbb 4\n" 15513 "#define ccc (5)", 15514 Style); 15515 15516 verifyFormat("#define f(x) (x * x)\n" 15517 "#define fff(x, y, z) (x * y + z)\n" 15518 "#define ffff(x, y) (x - y)", 15519 Style); 15520 15521 verifyFormat("#define foo(x, y) (x + y)\n" 15522 "#define bar (5, 6)(2 + 2)", 15523 Style); 15524 15525 verifyFormat("#define a 3\n" 15526 "#define bbbb 4\n" 15527 "#define ccc (5)\n" 15528 "#define f(x) (x * x)\n" 15529 "#define fff(x, y, z) (x * y + z)\n" 15530 "#define ffff(x, y) (x - y)", 15531 Style); 15532 15533 verifyFormat("#define a 5\n" 15534 "#define foo(x, y) (x + y)\n" 15535 "#define CCC (6)\n" 15536 "auto lambda = []() {\n" 15537 " auto ii = 0;\n" 15538 " float j = 0;\n" 15539 " return 0;\n" 15540 "};\n" 15541 "int i = 0;\n" 15542 "float i2 = 0;\n" 15543 "auto v = type{\n" 15544 " i = 1, //\n" 15545 " (i = 2), //\n" 15546 " i = 3 //\n" 15547 "};", 15548 Style); 15549 15550 Style.AlignConsecutiveMacros = FormatStyle::ACS_None; 15551 Style.ColumnLimit = 20; 15552 15553 verifyFormat("#define a \\\n" 15554 " \"aabbbbbbbbbbbb\"\n" 15555 "#define D \\\n" 15556 " \"aabbbbbbbbbbbb\" \\\n" 15557 " \"ccddeeeeeeeee\"\n" 15558 "#define B \\\n" 15559 " \"QQQQQQQQQQQQQ\" \\\n" 15560 " \"FFFFFFFFFFFFF\" \\\n" 15561 " \"LLLLLLLL\"\n", 15562 Style); 15563 15564 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15565 verifyFormat("#define a \\\n" 15566 " \"aabbbbbbbbbbbb\"\n" 15567 "#define D \\\n" 15568 " \"aabbbbbbbbbbbb\" \\\n" 15569 " \"ccddeeeeeeeee\"\n" 15570 "#define B \\\n" 15571 " \"QQQQQQQQQQQQQ\" \\\n" 15572 " \"FFFFFFFFFFFFF\" \\\n" 15573 " \"LLLLLLLL\"\n", 15574 Style); 15575 15576 // Test across comments 15577 Style.MaxEmptyLinesToKeep = 10; 15578 Style.ReflowComments = false; 15579 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossComments; 15580 EXPECT_EQ("#define a 3\n" 15581 "// line comment\n" 15582 "#define bbbb 4\n" 15583 "#define ccc (5)", 15584 format("#define a 3\n" 15585 "// line comment\n" 15586 "#define bbbb 4\n" 15587 "#define ccc (5)", 15588 Style)); 15589 15590 EXPECT_EQ("#define a 3\n" 15591 "/* block comment */\n" 15592 "#define bbbb 4\n" 15593 "#define ccc (5)", 15594 format("#define a 3\n" 15595 "/* block comment */\n" 15596 "#define bbbb 4\n" 15597 "#define ccc (5)", 15598 Style)); 15599 15600 EXPECT_EQ("#define a 3\n" 15601 "/* multi-line *\n" 15602 " * block comment */\n" 15603 "#define bbbb 4\n" 15604 "#define ccc (5)", 15605 format("#define a 3\n" 15606 "/* multi-line *\n" 15607 " * block comment */\n" 15608 "#define bbbb 4\n" 15609 "#define ccc (5)", 15610 Style)); 15611 15612 EXPECT_EQ("#define a 3\n" 15613 "// multi-line line comment\n" 15614 "//\n" 15615 "#define bbbb 4\n" 15616 "#define ccc (5)", 15617 format("#define a 3\n" 15618 "// multi-line line comment\n" 15619 "//\n" 15620 "#define bbbb 4\n" 15621 "#define ccc (5)", 15622 Style)); 15623 15624 EXPECT_EQ("#define a 3\n" 15625 "// empty lines still break.\n" 15626 "\n" 15627 "#define bbbb 4\n" 15628 "#define ccc (5)", 15629 format("#define a 3\n" 15630 "// empty lines still break.\n" 15631 "\n" 15632 "#define bbbb 4\n" 15633 "#define ccc (5)", 15634 Style)); 15635 15636 // Test across empty lines 15637 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLines; 15638 EXPECT_EQ("#define a 3\n" 15639 "\n" 15640 "#define bbbb 4\n" 15641 "#define ccc (5)", 15642 format("#define a 3\n" 15643 "\n" 15644 "#define bbbb 4\n" 15645 "#define ccc (5)", 15646 Style)); 15647 15648 EXPECT_EQ("#define a 3\n" 15649 "\n" 15650 "\n" 15651 "\n" 15652 "#define bbbb 4\n" 15653 "#define ccc (5)", 15654 format("#define a 3\n" 15655 "\n" 15656 "\n" 15657 "\n" 15658 "#define bbbb 4\n" 15659 "#define ccc (5)", 15660 Style)); 15661 15662 EXPECT_EQ("#define a 3\n" 15663 "// comments should break alignment\n" 15664 "//\n" 15665 "#define bbbb 4\n" 15666 "#define ccc (5)", 15667 format("#define a 3\n" 15668 "// comments should break alignment\n" 15669 "//\n" 15670 "#define bbbb 4\n" 15671 "#define ccc (5)", 15672 Style)); 15673 15674 // Test across empty lines and comments 15675 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLinesAndComments; 15676 verifyFormat("#define a 3\n" 15677 "\n" 15678 "// line comment\n" 15679 "#define bbbb 4\n" 15680 "#define ccc (5)", 15681 Style); 15682 15683 EXPECT_EQ("#define a 3\n" 15684 "\n" 15685 "\n" 15686 "/* multi-line *\n" 15687 " * block comment */\n" 15688 "\n" 15689 "\n" 15690 "#define bbbb 4\n" 15691 "#define ccc (5)", 15692 format("#define a 3\n" 15693 "\n" 15694 "\n" 15695 "/* multi-line *\n" 15696 " * block comment */\n" 15697 "\n" 15698 "\n" 15699 "#define bbbb 4\n" 15700 "#define ccc (5)", 15701 Style)); 15702 15703 EXPECT_EQ("#define a 3\n" 15704 "\n" 15705 "\n" 15706 "/* multi-line *\n" 15707 " * block comment */\n" 15708 "\n" 15709 "\n" 15710 "#define bbbb 4\n" 15711 "#define ccc (5)", 15712 format("#define a 3\n" 15713 "\n" 15714 "\n" 15715 "/* multi-line *\n" 15716 " * block comment */\n" 15717 "\n" 15718 "\n" 15719 "#define bbbb 4\n" 15720 "#define ccc (5)", 15721 Style)); 15722 } 15723 15724 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) { 15725 FormatStyle Alignment = getLLVMStyle(); 15726 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15727 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossEmptyLines; 15728 15729 Alignment.MaxEmptyLinesToKeep = 10; 15730 /* Test alignment across empty lines */ 15731 EXPECT_EQ("int a = 5;\n" 15732 "\n" 15733 "int oneTwoThree = 123;", 15734 format("int a = 5;\n" 15735 "\n" 15736 "int oneTwoThree= 123;", 15737 Alignment)); 15738 EXPECT_EQ("int a = 5;\n" 15739 "int one = 1;\n" 15740 "\n" 15741 "int oneTwoThree = 123;", 15742 format("int a = 5;\n" 15743 "int one = 1;\n" 15744 "\n" 15745 "int oneTwoThree = 123;", 15746 Alignment)); 15747 EXPECT_EQ("int a = 5;\n" 15748 "int one = 1;\n" 15749 "\n" 15750 "int oneTwoThree = 123;\n" 15751 "int oneTwo = 12;", 15752 format("int a = 5;\n" 15753 "int one = 1;\n" 15754 "\n" 15755 "int oneTwoThree = 123;\n" 15756 "int oneTwo = 12;", 15757 Alignment)); 15758 15759 /* Test across comments */ 15760 EXPECT_EQ("int a = 5;\n" 15761 "/* block comment */\n" 15762 "int oneTwoThree = 123;", 15763 format("int a = 5;\n" 15764 "/* block comment */\n" 15765 "int oneTwoThree=123;", 15766 Alignment)); 15767 15768 EXPECT_EQ("int a = 5;\n" 15769 "// line comment\n" 15770 "int oneTwoThree = 123;", 15771 format("int a = 5;\n" 15772 "// line comment\n" 15773 "int oneTwoThree=123;", 15774 Alignment)); 15775 15776 /* Test across comments and newlines */ 15777 EXPECT_EQ("int a = 5;\n" 15778 "\n" 15779 "/* block comment */\n" 15780 "int oneTwoThree = 123;", 15781 format("int a = 5;\n" 15782 "\n" 15783 "/* block comment */\n" 15784 "int oneTwoThree=123;", 15785 Alignment)); 15786 15787 EXPECT_EQ("int a = 5;\n" 15788 "\n" 15789 "// line comment\n" 15790 "int oneTwoThree = 123;", 15791 format("int a = 5;\n" 15792 "\n" 15793 "// line comment\n" 15794 "int oneTwoThree=123;", 15795 Alignment)); 15796 } 15797 15798 TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) { 15799 FormatStyle Alignment = getLLVMStyle(); 15800 Alignment.AlignConsecutiveDeclarations = 15801 FormatStyle::ACS_AcrossEmptyLinesAndComments; 15802 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 15803 15804 Alignment.MaxEmptyLinesToKeep = 10; 15805 /* Test alignment across empty lines */ 15806 EXPECT_EQ("int a = 5;\n" 15807 "\n" 15808 "float const oneTwoThree = 123;", 15809 format("int a = 5;\n" 15810 "\n" 15811 "float const oneTwoThree = 123;", 15812 Alignment)); 15813 EXPECT_EQ("int a = 5;\n" 15814 "float const one = 1;\n" 15815 "\n" 15816 "int oneTwoThree = 123;", 15817 format("int a = 5;\n" 15818 "float const one = 1;\n" 15819 "\n" 15820 "int oneTwoThree = 123;", 15821 Alignment)); 15822 15823 /* Test across comments */ 15824 EXPECT_EQ("float const a = 5;\n" 15825 "/* block comment */\n" 15826 "int oneTwoThree = 123;", 15827 format("float const a = 5;\n" 15828 "/* block comment */\n" 15829 "int oneTwoThree=123;", 15830 Alignment)); 15831 15832 EXPECT_EQ("float const a = 5;\n" 15833 "// line comment\n" 15834 "int oneTwoThree = 123;", 15835 format("float const a = 5;\n" 15836 "// line comment\n" 15837 "int oneTwoThree=123;", 15838 Alignment)); 15839 15840 /* Test across comments and newlines */ 15841 EXPECT_EQ("float const a = 5;\n" 15842 "\n" 15843 "/* block comment */\n" 15844 "int oneTwoThree = 123;", 15845 format("float const a = 5;\n" 15846 "\n" 15847 "/* block comment */\n" 15848 "int oneTwoThree=123;", 15849 Alignment)); 15850 15851 EXPECT_EQ("float const a = 5;\n" 15852 "\n" 15853 "// line comment\n" 15854 "int oneTwoThree = 123;", 15855 format("float const a = 5;\n" 15856 "\n" 15857 "// line comment\n" 15858 "int oneTwoThree=123;", 15859 Alignment)); 15860 } 15861 15862 TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) { 15863 FormatStyle Alignment = getLLVMStyle(); 15864 Alignment.AlignConsecutiveBitFields = 15865 FormatStyle::ACS_AcrossEmptyLinesAndComments; 15866 15867 Alignment.MaxEmptyLinesToKeep = 10; 15868 /* Test alignment across empty lines */ 15869 EXPECT_EQ("int a : 5;\n" 15870 "\n" 15871 "int longbitfield : 6;", 15872 format("int a : 5;\n" 15873 "\n" 15874 "int longbitfield : 6;", 15875 Alignment)); 15876 EXPECT_EQ("int a : 5;\n" 15877 "int one : 1;\n" 15878 "\n" 15879 "int longbitfield : 6;", 15880 format("int a : 5;\n" 15881 "int one : 1;\n" 15882 "\n" 15883 "int longbitfield : 6;", 15884 Alignment)); 15885 15886 /* Test across comments */ 15887 EXPECT_EQ("int a : 5;\n" 15888 "/* block comment */\n" 15889 "int longbitfield : 6;", 15890 format("int a : 5;\n" 15891 "/* block comment */\n" 15892 "int longbitfield : 6;", 15893 Alignment)); 15894 EXPECT_EQ("int a : 5;\n" 15895 "int one : 1;\n" 15896 "// line comment\n" 15897 "int longbitfield : 6;", 15898 format("int a : 5;\n" 15899 "int one : 1;\n" 15900 "// line comment\n" 15901 "int longbitfield : 6;", 15902 Alignment)); 15903 15904 /* Test across comments and newlines */ 15905 EXPECT_EQ("int a : 5;\n" 15906 "/* block comment */\n" 15907 "\n" 15908 "int longbitfield : 6;", 15909 format("int a : 5;\n" 15910 "/* block comment */\n" 15911 "\n" 15912 "int longbitfield : 6;", 15913 Alignment)); 15914 EXPECT_EQ("int a : 5;\n" 15915 "int one : 1;\n" 15916 "\n" 15917 "// line comment\n" 15918 "\n" 15919 "int longbitfield : 6;", 15920 format("int a : 5;\n" 15921 "int one : 1;\n" 15922 "\n" 15923 "// line comment \n" 15924 "\n" 15925 "int longbitfield : 6;", 15926 Alignment)); 15927 } 15928 15929 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) { 15930 FormatStyle Alignment = getLLVMStyle(); 15931 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15932 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossComments; 15933 15934 Alignment.MaxEmptyLinesToKeep = 10; 15935 /* Test alignment across empty lines */ 15936 EXPECT_EQ("int a = 5;\n" 15937 "\n" 15938 "int oneTwoThree = 123;", 15939 format("int a = 5;\n" 15940 "\n" 15941 "int oneTwoThree= 123;", 15942 Alignment)); 15943 EXPECT_EQ("int a = 5;\n" 15944 "int one = 1;\n" 15945 "\n" 15946 "int oneTwoThree = 123;", 15947 format("int a = 5;\n" 15948 "int one = 1;\n" 15949 "\n" 15950 "int oneTwoThree = 123;", 15951 Alignment)); 15952 15953 /* Test across comments */ 15954 EXPECT_EQ("int a = 5;\n" 15955 "/* block comment */\n" 15956 "int oneTwoThree = 123;", 15957 format("int a = 5;\n" 15958 "/* block comment */\n" 15959 "int oneTwoThree=123;", 15960 Alignment)); 15961 15962 EXPECT_EQ("int a = 5;\n" 15963 "// line comment\n" 15964 "int oneTwoThree = 123;", 15965 format("int a = 5;\n" 15966 "// line comment\n" 15967 "int oneTwoThree=123;", 15968 Alignment)); 15969 15970 EXPECT_EQ("int a = 5;\n" 15971 "/*\n" 15972 " * multi-line block comment\n" 15973 " */\n" 15974 "int oneTwoThree = 123;", 15975 format("int a = 5;\n" 15976 "/*\n" 15977 " * multi-line block comment\n" 15978 " */\n" 15979 "int oneTwoThree=123;", 15980 Alignment)); 15981 15982 EXPECT_EQ("int a = 5;\n" 15983 "//\n" 15984 "// multi-line line comment\n" 15985 "//\n" 15986 "int oneTwoThree = 123;", 15987 format("int a = 5;\n" 15988 "//\n" 15989 "// multi-line line comment\n" 15990 "//\n" 15991 "int oneTwoThree=123;", 15992 Alignment)); 15993 15994 /* Test across comments and newlines */ 15995 EXPECT_EQ("int a = 5;\n" 15996 "\n" 15997 "/* block comment */\n" 15998 "int oneTwoThree = 123;", 15999 format("int a = 5;\n" 16000 "\n" 16001 "/* block comment */\n" 16002 "int oneTwoThree=123;", 16003 Alignment)); 16004 16005 EXPECT_EQ("int a = 5;\n" 16006 "\n" 16007 "// line comment\n" 16008 "int oneTwoThree = 123;", 16009 format("int a = 5;\n" 16010 "\n" 16011 "// line comment\n" 16012 "int oneTwoThree=123;", 16013 Alignment)); 16014 } 16015 16016 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) { 16017 FormatStyle Alignment = getLLVMStyle(); 16018 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 16019 Alignment.AlignConsecutiveAssignments = 16020 FormatStyle::ACS_AcrossEmptyLinesAndComments; 16021 verifyFormat("int a = 5;\n" 16022 "int oneTwoThree = 123;", 16023 Alignment); 16024 verifyFormat("int a = method();\n" 16025 "int oneTwoThree = 133;", 16026 Alignment); 16027 verifyFormat("a &= 5;\n" 16028 "bcd *= 5;\n" 16029 "ghtyf += 5;\n" 16030 "dvfvdb -= 5;\n" 16031 "a /= 5;\n" 16032 "vdsvsv %= 5;\n" 16033 "sfdbddfbdfbb ^= 5;\n" 16034 "dvsdsv |= 5;\n" 16035 "int dsvvdvsdvvv = 123;", 16036 Alignment); 16037 verifyFormat("int i = 1, j = 10;\n" 16038 "something = 2000;", 16039 Alignment); 16040 verifyFormat("something = 2000;\n" 16041 "int i = 1, j = 10;\n", 16042 Alignment); 16043 verifyFormat("something = 2000;\n" 16044 "another = 911;\n" 16045 "int i = 1, j = 10;\n" 16046 "oneMore = 1;\n" 16047 "i = 2;", 16048 Alignment); 16049 verifyFormat("int a = 5;\n" 16050 "int one = 1;\n" 16051 "method();\n" 16052 "int oneTwoThree = 123;\n" 16053 "int oneTwo = 12;", 16054 Alignment); 16055 verifyFormat("int oneTwoThree = 123;\n" 16056 "int oneTwo = 12;\n" 16057 "method();\n", 16058 Alignment); 16059 verifyFormat("int oneTwoThree = 123; // comment\n" 16060 "int oneTwo = 12; // comment", 16061 Alignment); 16062 16063 // Bug 25167 16064 /* Uncomment when fixed 16065 verifyFormat("#if A\n" 16066 "#else\n" 16067 "int aaaaaaaa = 12;\n" 16068 "#endif\n" 16069 "#if B\n" 16070 "#else\n" 16071 "int a = 12;\n" 16072 "#endif\n", 16073 Alignment); 16074 verifyFormat("enum foo {\n" 16075 "#if A\n" 16076 "#else\n" 16077 " aaaaaaaa = 12;\n" 16078 "#endif\n" 16079 "#if B\n" 16080 "#else\n" 16081 " a = 12;\n" 16082 "#endif\n" 16083 "};\n", 16084 Alignment); 16085 */ 16086 16087 Alignment.MaxEmptyLinesToKeep = 10; 16088 /* Test alignment across empty lines */ 16089 EXPECT_EQ("int a = 5;\n" 16090 "\n" 16091 "int oneTwoThree = 123;", 16092 format("int a = 5;\n" 16093 "\n" 16094 "int oneTwoThree= 123;", 16095 Alignment)); 16096 EXPECT_EQ("int a = 5;\n" 16097 "int one = 1;\n" 16098 "\n" 16099 "int oneTwoThree = 123;", 16100 format("int a = 5;\n" 16101 "int one = 1;\n" 16102 "\n" 16103 "int oneTwoThree = 123;", 16104 Alignment)); 16105 EXPECT_EQ("int a = 5;\n" 16106 "int one = 1;\n" 16107 "\n" 16108 "int oneTwoThree = 123;\n" 16109 "int oneTwo = 12;", 16110 format("int a = 5;\n" 16111 "int one = 1;\n" 16112 "\n" 16113 "int oneTwoThree = 123;\n" 16114 "int oneTwo = 12;", 16115 Alignment)); 16116 16117 /* Test across comments */ 16118 EXPECT_EQ("int a = 5;\n" 16119 "/* block comment */\n" 16120 "int oneTwoThree = 123;", 16121 format("int a = 5;\n" 16122 "/* block comment */\n" 16123 "int oneTwoThree=123;", 16124 Alignment)); 16125 16126 EXPECT_EQ("int a = 5;\n" 16127 "// line comment\n" 16128 "int oneTwoThree = 123;", 16129 format("int a = 5;\n" 16130 "// line comment\n" 16131 "int oneTwoThree=123;", 16132 Alignment)); 16133 16134 /* Test across comments and newlines */ 16135 EXPECT_EQ("int a = 5;\n" 16136 "\n" 16137 "/* block comment */\n" 16138 "int oneTwoThree = 123;", 16139 format("int a = 5;\n" 16140 "\n" 16141 "/* block comment */\n" 16142 "int oneTwoThree=123;", 16143 Alignment)); 16144 16145 EXPECT_EQ("int a = 5;\n" 16146 "\n" 16147 "// line comment\n" 16148 "int oneTwoThree = 123;", 16149 format("int a = 5;\n" 16150 "\n" 16151 "// line comment\n" 16152 "int oneTwoThree=123;", 16153 Alignment)); 16154 16155 EXPECT_EQ("int a = 5;\n" 16156 "//\n" 16157 "// multi-line line comment\n" 16158 "//\n" 16159 "int oneTwoThree = 123;", 16160 format("int a = 5;\n" 16161 "//\n" 16162 "// multi-line line comment\n" 16163 "//\n" 16164 "int oneTwoThree=123;", 16165 Alignment)); 16166 16167 EXPECT_EQ("int a = 5;\n" 16168 "/*\n" 16169 " * multi-line block comment\n" 16170 " */\n" 16171 "int oneTwoThree = 123;", 16172 format("int a = 5;\n" 16173 "/*\n" 16174 " * multi-line block comment\n" 16175 " */\n" 16176 "int oneTwoThree=123;", 16177 Alignment)); 16178 16179 EXPECT_EQ("int a = 5;\n" 16180 "\n" 16181 "/* block comment */\n" 16182 "\n" 16183 "\n" 16184 "\n" 16185 "int oneTwoThree = 123;", 16186 format("int a = 5;\n" 16187 "\n" 16188 "/* block comment */\n" 16189 "\n" 16190 "\n" 16191 "\n" 16192 "int oneTwoThree=123;", 16193 Alignment)); 16194 16195 EXPECT_EQ("int a = 5;\n" 16196 "\n" 16197 "// line comment\n" 16198 "\n" 16199 "\n" 16200 "\n" 16201 "int oneTwoThree = 123;", 16202 format("int a = 5;\n" 16203 "\n" 16204 "// line comment\n" 16205 "\n" 16206 "\n" 16207 "\n" 16208 "int oneTwoThree=123;", 16209 Alignment)); 16210 16211 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 16212 verifyFormat("#define A \\\n" 16213 " int aaaa = 12; \\\n" 16214 " int b = 23; \\\n" 16215 " int ccc = 234; \\\n" 16216 " int dddddddddd = 2345;", 16217 Alignment); 16218 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 16219 verifyFormat("#define A \\\n" 16220 " int aaaa = 12; \\\n" 16221 " int b = 23; \\\n" 16222 " int ccc = 234; \\\n" 16223 " int dddddddddd = 2345;", 16224 Alignment); 16225 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 16226 verifyFormat("#define A " 16227 " \\\n" 16228 " int aaaa = 12; " 16229 " \\\n" 16230 " int b = 23; " 16231 " \\\n" 16232 " int ccc = 234; " 16233 " \\\n" 16234 " int dddddddddd = 2345;", 16235 Alignment); 16236 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 16237 "k = 4, int l = 5,\n" 16238 " int m = 6) {\n" 16239 " int j = 10;\n" 16240 " otherThing = 1;\n" 16241 "}", 16242 Alignment); 16243 verifyFormat("void SomeFunction(int parameter = 0) {\n" 16244 " int i = 1;\n" 16245 " int j = 2;\n" 16246 " int big = 10000;\n" 16247 "}", 16248 Alignment); 16249 verifyFormat("class C {\n" 16250 "public:\n" 16251 " int i = 1;\n" 16252 " virtual void f() = 0;\n" 16253 "};", 16254 Alignment); 16255 verifyFormat("int i = 1;\n" 16256 "if (SomeType t = getSomething()) {\n" 16257 "}\n" 16258 "int j = 2;\n" 16259 "int big = 10000;", 16260 Alignment); 16261 verifyFormat("int j = 7;\n" 16262 "for (int k = 0; k < N; ++k) {\n" 16263 "}\n" 16264 "int j = 2;\n" 16265 "int big = 10000;\n" 16266 "}", 16267 Alignment); 16268 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 16269 verifyFormat("int i = 1;\n" 16270 "LooooooooooongType loooooooooooooooooooooongVariable\n" 16271 " = someLooooooooooooooooongFunction();\n" 16272 "int j = 2;", 16273 Alignment); 16274 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 16275 verifyFormat("int i = 1;\n" 16276 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 16277 " someLooooooooooooooooongFunction();\n" 16278 "int j = 2;", 16279 Alignment); 16280 16281 verifyFormat("auto lambda = []() {\n" 16282 " auto i = 0;\n" 16283 " return 0;\n" 16284 "};\n" 16285 "int i = 0;\n" 16286 "auto v = type{\n" 16287 " i = 1, //\n" 16288 " (i = 2), //\n" 16289 " i = 3 //\n" 16290 "};", 16291 Alignment); 16292 16293 verifyFormat( 16294 "int i = 1;\n" 16295 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 16296 " loooooooooooooooooooooongParameterB);\n" 16297 "int j = 2;", 16298 Alignment); 16299 16300 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 16301 " typename B = very_long_type_name_1,\n" 16302 " typename T_2 = very_long_type_name_2>\n" 16303 "auto foo() {}\n", 16304 Alignment); 16305 verifyFormat("int a, b = 1;\n" 16306 "int c = 2;\n" 16307 "int dd = 3;\n", 16308 Alignment); 16309 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 16310 "float b[1][] = {{3.f}};\n", 16311 Alignment); 16312 verifyFormat("for (int i = 0; i < 1; i++)\n" 16313 " int x = 1;\n", 16314 Alignment); 16315 verifyFormat("for (i = 0; i < 1; i++)\n" 16316 " x = 1;\n" 16317 "y = 1;\n", 16318 Alignment); 16319 16320 Alignment.ReflowComments = true; 16321 Alignment.ColumnLimit = 50; 16322 EXPECT_EQ("int x = 0;\n" 16323 "int yy = 1; /// specificlennospace\n" 16324 "int zzz = 2;\n", 16325 format("int x = 0;\n" 16326 "int yy = 1; ///specificlennospace\n" 16327 "int zzz = 2;\n", 16328 Alignment)); 16329 } 16330 16331 TEST_F(FormatTest, AlignConsecutiveAssignments) { 16332 FormatStyle Alignment = getLLVMStyle(); 16333 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 16334 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16335 verifyFormat("int a = 5;\n" 16336 "int oneTwoThree = 123;", 16337 Alignment); 16338 verifyFormat("int a = 5;\n" 16339 "int oneTwoThree = 123;", 16340 Alignment); 16341 16342 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16343 verifyFormat("int a = 5;\n" 16344 "int oneTwoThree = 123;", 16345 Alignment); 16346 verifyFormat("int a = method();\n" 16347 "int oneTwoThree = 133;", 16348 Alignment); 16349 verifyFormat("a &= 5;\n" 16350 "bcd *= 5;\n" 16351 "ghtyf += 5;\n" 16352 "dvfvdb -= 5;\n" 16353 "a /= 5;\n" 16354 "vdsvsv %= 5;\n" 16355 "sfdbddfbdfbb ^= 5;\n" 16356 "dvsdsv |= 5;\n" 16357 "int dsvvdvsdvvv = 123;", 16358 Alignment); 16359 verifyFormat("int i = 1, j = 10;\n" 16360 "something = 2000;", 16361 Alignment); 16362 verifyFormat("something = 2000;\n" 16363 "int i = 1, j = 10;\n", 16364 Alignment); 16365 verifyFormat("something = 2000;\n" 16366 "another = 911;\n" 16367 "int i = 1, j = 10;\n" 16368 "oneMore = 1;\n" 16369 "i = 2;", 16370 Alignment); 16371 verifyFormat("int a = 5;\n" 16372 "int one = 1;\n" 16373 "method();\n" 16374 "int oneTwoThree = 123;\n" 16375 "int oneTwo = 12;", 16376 Alignment); 16377 verifyFormat("int oneTwoThree = 123;\n" 16378 "int oneTwo = 12;\n" 16379 "method();\n", 16380 Alignment); 16381 verifyFormat("int oneTwoThree = 123; // comment\n" 16382 "int oneTwo = 12; // comment", 16383 Alignment); 16384 verifyFormat("int f() = default;\n" 16385 "int &operator() = default;\n" 16386 "int &operator=() {", 16387 Alignment); 16388 verifyFormat("int f() = delete;\n" 16389 "int &operator() = delete;\n" 16390 "int &operator=() {", 16391 Alignment); 16392 verifyFormat("int f() = default; // comment\n" 16393 "int &operator() = default; // comment\n" 16394 "int &operator=() {", 16395 Alignment); 16396 verifyFormat("int f() = default;\n" 16397 "int &operator() = default;\n" 16398 "int &operator==() {", 16399 Alignment); 16400 verifyFormat("int f() = default;\n" 16401 "int &operator() = default;\n" 16402 "int &operator<=() {", 16403 Alignment); 16404 verifyFormat("int f() = default;\n" 16405 "int &operator() = default;\n" 16406 "int &operator!=() {", 16407 Alignment); 16408 verifyFormat("int f() = default;\n" 16409 "int &operator() = default;\n" 16410 "int &operator=();", 16411 Alignment); 16412 verifyFormat("int f() = delete;\n" 16413 "int &operator() = delete;\n" 16414 "int &operator=();", 16415 Alignment); 16416 verifyFormat("/* long long padding */ int f() = default;\n" 16417 "int &operator() = default;\n" 16418 "int &operator/**/ =();", 16419 Alignment); 16420 // https://llvm.org/PR33697 16421 FormatStyle AlignmentWithPenalty = getLLVMStyle(); 16422 AlignmentWithPenalty.AlignConsecutiveAssignments = 16423 FormatStyle::ACS_Consecutive; 16424 AlignmentWithPenalty.PenaltyReturnTypeOnItsOwnLine = 5000; 16425 verifyFormat("class SSSSSSSSSSSSSSSSSSSSSSSSSSSS {\n" 16426 " void f() = delete;\n" 16427 " SSSSSSSSSSSSSSSSSSSSSSSSSSSS &operator=(\n" 16428 " const SSSSSSSSSSSSSSSSSSSSSSSSSSSS &other) = delete;\n" 16429 "};\n", 16430 AlignmentWithPenalty); 16431 16432 // Bug 25167 16433 /* Uncomment when fixed 16434 verifyFormat("#if A\n" 16435 "#else\n" 16436 "int aaaaaaaa = 12;\n" 16437 "#endif\n" 16438 "#if B\n" 16439 "#else\n" 16440 "int a = 12;\n" 16441 "#endif\n", 16442 Alignment); 16443 verifyFormat("enum foo {\n" 16444 "#if A\n" 16445 "#else\n" 16446 " aaaaaaaa = 12;\n" 16447 "#endif\n" 16448 "#if B\n" 16449 "#else\n" 16450 " a = 12;\n" 16451 "#endif\n" 16452 "};\n", 16453 Alignment); 16454 */ 16455 16456 EXPECT_EQ("int a = 5;\n" 16457 "\n" 16458 "int oneTwoThree = 123;", 16459 format("int a = 5;\n" 16460 "\n" 16461 "int oneTwoThree= 123;", 16462 Alignment)); 16463 EXPECT_EQ("int a = 5;\n" 16464 "int one = 1;\n" 16465 "\n" 16466 "int oneTwoThree = 123;", 16467 format("int a = 5;\n" 16468 "int one = 1;\n" 16469 "\n" 16470 "int oneTwoThree = 123;", 16471 Alignment)); 16472 EXPECT_EQ("int a = 5;\n" 16473 "int one = 1;\n" 16474 "\n" 16475 "int oneTwoThree = 123;\n" 16476 "int oneTwo = 12;", 16477 format("int a = 5;\n" 16478 "int one = 1;\n" 16479 "\n" 16480 "int oneTwoThree = 123;\n" 16481 "int oneTwo = 12;", 16482 Alignment)); 16483 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 16484 verifyFormat("#define A \\\n" 16485 " int aaaa = 12; \\\n" 16486 " int b = 23; \\\n" 16487 " int ccc = 234; \\\n" 16488 " int dddddddddd = 2345;", 16489 Alignment); 16490 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 16491 verifyFormat("#define A \\\n" 16492 " int aaaa = 12; \\\n" 16493 " int b = 23; \\\n" 16494 " int ccc = 234; \\\n" 16495 " int dddddddddd = 2345;", 16496 Alignment); 16497 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 16498 verifyFormat("#define A " 16499 " \\\n" 16500 " int aaaa = 12; " 16501 " \\\n" 16502 " int b = 23; " 16503 " \\\n" 16504 " int ccc = 234; " 16505 " \\\n" 16506 " int dddddddddd = 2345;", 16507 Alignment); 16508 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 16509 "k = 4, int l = 5,\n" 16510 " int m = 6) {\n" 16511 " int j = 10;\n" 16512 " otherThing = 1;\n" 16513 "}", 16514 Alignment); 16515 verifyFormat("void SomeFunction(int parameter = 0) {\n" 16516 " int i = 1;\n" 16517 " int j = 2;\n" 16518 " int big = 10000;\n" 16519 "}", 16520 Alignment); 16521 verifyFormat("class C {\n" 16522 "public:\n" 16523 " int i = 1;\n" 16524 " virtual void f() = 0;\n" 16525 "};", 16526 Alignment); 16527 verifyFormat("int i = 1;\n" 16528 "if (SomeType t = getSomething()) {\n" 16529 "}\n" 16530 "int j = 2;\n" 16531 "int big = 10000;", 16532 Alignment); 16533 verifyFormat("int j = 7;\n" 16534 "for (int k = 0; k < N; ++k) {\n" 16535 "}\n" 16536 "int j = 2;\n" 16537 "int big = 10000;\n" 16538 "}", 16539 Alignment); 16540 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 16541 verifyFormat("int i = 1;\n" 16542 "LooooooooooongType loooooooooooooooooooooongVariable\n" 16543 " = someLooooooooooooooooongFunction();\n" 16544 "int j = 2;", 16545 Alignment); 16546 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 16547 verifyFormat("int i = 1;\n" 16548 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 16549 " someLooooooooooooooooongFunction();\n" 16550 "int j = 2;", 16551 Alignment); 16552 16553 verifyFormat("auto lambda = []() {\n" 16554 " auto i = 0;\n" 16555 " return 0;\n" 16556 "};\n" 16557 "int i = 0;\n" 16558 "auto v = type{\n" 16559 " i = 1, //\n" 16560 " (i = 2), //\n" 16561 " i = 3 //\n" 16562 "};", 16563 Alignment); 16564 16565 verifyFormat( 16566 "int i = 1;\n" 16567 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 16568 " loooooooooooooooooooooongParameterB);\n" 16569 "int j = 2;", 16570 Alignment); 16571 16572 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 16573 " typename B = very_long_type_name_1,\n" 16574 " typename T_2 = very_long_type_name_2>\n" 16575 "auto foo() {}\n", 16576 Alignment); 16577 verifyFormat("int a, b = 1;\n" 16578 "int c = 2;\n" 16579 "int dd = 3;\n", 16580 Alignment); 16581 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 16582 "float b[1][] = {{3.f}};\n", 16583 Alignment); 16584 verifyFormat("for (int i = 0; i < 1; i++)\n" 16585 " int x = 1;\n", 16586 Alignment); 16587 verifyFormat("for (i = 0; i < 1; i++)\n" 16588 " x = 1;\n" 16589 "y = 1;\n", 16590 Alignment); 16591 16592 EXPECT_EQ(Alignment.ReflowComments, true); 16593 Alignment.ColumnLimit = 50; 16594 EXPECT_EQ("int x = 0;\n" 16595 "int yy = 1; /// specificlennospace\n" 16596 "int zzz = 2;\n", 16597 format("int x = 0;\n" 16598 "int yy = 1; ///specificlennospace\n" 16599 "int zzz = 2;\n", 16600 Alignment)); 16601 16602 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n" 16603 "auto b = [] {\n" 16604 " f();\n" 16605 " return;\n" 16606 "};", 16607 Alignment); 16608 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n" 16609 "auto b = g([] {\n" 16610 " f();\n" 16611 " return;\n" 16612 "});", 16613 Alignment); 16614 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n" 16615 "auto b = g(param, [] {\n" 16616 " f();\n" 16617 " return;\n" 16618 "});", 16619 Alignment); 16620 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n" 16621 "auto b = [] {\n" 16622 " if (condition) {\n" 16623 " return;\n" 16624 " }\n" 16625 "};", 16626 Alignment); 16627 16628 verifyFormat("auto b = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 16629 " ccc ? aaaaa : bbbbb,\n" 16630 " dddddddddddddddddddddddddd);", 16631 Alignment); 16632 // FIXME: https://llvm.org/PR53497 16633 // verifyFormat("auto aaaaaaaaaaaa = f();\n" 16634 // "auto b = f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 16635 // " ccc ? aaaaa : bbbbb,\n" 16636 // " dddddddddddddddddddddddddd);", 16637 // Alignment); 16638 } 16639 16640 TEST_F(FormatTest, AlignConsecutiveBitFields) { 16641 FormatStyle Alignment = getLLVMStyle(); 16642 Alignment.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive; 16643 verifyFormat("int const a : 5;\n" 16644 "int oneTwoThree : 23;", 16645 Alignment); 16646 16647 // Initializers are allowed starting with c++2a 16648 verifyFormat("int const a : 5 = 1;\n" 16649 "int oneTwoThree : 23 = 0;", 16650 Alignment); 16651 16652 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16653 verifyFormat("int const a : 5;\n" 16654 "int oneTwoThree : 23;", 16655 Alignment); 16656 16657 verifyFormat("int const a : 5; // comment\n" 16658 "int oneTwoThree : 23; // comment", 16659 Alignment); 16660 16661 verifyFormat("int const a : 5 = 1;\n" 16662 "int oneTwoThree : 23 = 0;", 16663 Alignment); 16664 16665 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16666 verifyFormat("int const a : 5 = 1;\n" 16667 "int oneTwoThree : 23 = 0;", 16668 Alignment); 16669 verifyFormat("int const a : 5 = {1};\n" 16670 "int oneTwoThree : 23 = 0;", 16671 Alignment); 16672 16673 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None; 16674 verifyFormat("int const a :5;\n" 16675 "int oneTwoThree:23;", 16676 Alignment); 16677 16678 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before; 16679 verifyFormat("int const a :5;\n" 16680 "int oneTwoThree :23;", 16681 Alignment); 16682 16683 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After; 16684 verifyFormat("int const a : 5;\n" 16685 "int oneTwoThree: 23;", 16686 Alignment); 16687 16688 // Known limitations: ':' is only recognized as a bitfield colon when 16689 // followed by a number. 16690 /* 16691 verifyFormat("int oneTwoThree : SOME_CONSTANT;\n" 16692 "int a : 5;", 16693 Alignment); 16694 */ 16695 } 16696 16697 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 16698 FormatStyle Alignment = getLLVMStyle(); 16699 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 16700 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_None; 16701 Alignment.PointerAlignment = FormatStyle::PAS_Right; 16702 verifyFormat("float const a = 5;\n" 16703 "int oneTwoThree = 123;", 16704 Alignment); 16705 verifyFormat("int a = 5;\n" 16706 "float const oneTwoThree = 123;", 16707 Alignment); 16708 16709 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16710 verifyFormat("float const a = 5;\n" 16711 "int oneTwoThree = 123;", 16712 Alignment); 16713 verifyFormat("int a = method();\n" 16714 "float const oneTwoThree = 133;", 16715 Alignment); 16716 verifyFormat("int i = 1, j = 10;\n" 16717 "something = 2000;", 16718 Alignment); 16719 verifyFormat("something = 2000;\n" 16720 "int i = 1, j = 10;\n", 16721 Alignment); 16722 verifyFormat("float something = 2000;\n" 16723 "double another = 911;\n" 16724 "int i = 1, j = 10;\n" 16725 "const int *oneMore = 1;\n" 16726 "unsigned i = 2;", 16727 Alignment); 16728 verifyFormat("float a = 5;\n" 16729 "int one = 1;\n" 16730 "method();\n" 16731 "const double oneTwoThree = 123;\n" 16732 "const unsigned int oneTwo = 12;", 16733 Alignment); 16734 verifyFormat("int oneTwoThree{0}; // comment\n" 16735 "unsigned oneTwo; // comment", 16736 Alignment); 16737 verifyFormat("unsigned int *a;\n" 16738 "int *b;\n" 16739 "unsigned int Const *c;\n" 16740 "unsigned int const *d;\n" 16741 "unsigned int Const &e;\n" 16742 "unsigned int const &f;", 16743 Alignment); 16744 verifyFormat("Const unsigned int *c;\n" 16745 "const unsigned int *d;\n" 16746 "Const unsigned int &e;\n" 16747 "const unsigned int &f;\n" 16748 "const unsigned g;\n" 16749 "Const unsigned h;", 16750 Alignment); 16751 EXPECT_EQ("float const a = 5;\n" 16752 "\n" 16753 "int oneTwoThree = 123;", 16754 format("float const a = 5;\n" 16755 "\n" 16756 "int oneTwoThree= 123;", 16757 Alignment)); 16758 EXPECT_EQ("float a = 5;\n" 16759 "int one = 1;\n" 16760 "\n" 16761 "unsigned oneTwoThree = 123;", 16762 format("float a = 5;\n" 16763 "int one = 1;\n" 16764 "\n" 16765 "unsigned oneTwoThree = 123;", 16766 Alignment)); 16767 EXPECT_EQ("float a = 5;\n" 16768 "int one = 1;\n" 16769 "\n" 16770 "unsigned oneTwoThree = 123;\n" 16771 "int oneTwo = 12;", 16772 format("float a = 5;\n" 16773 "int one = 1;\n" 16774 "\n" 16775 "unsigned oneTwoThree = 123;\n" 16776 "int oneTwo = 12;", 16777 Alignment)); 16778 // Function prototype alignment 16779 verifyFormat("int a();\n" 16780 "double b();", 16781 Alignment); 16782 verifyFormat("int a(int x);\n" 16783 "double b();", 16784 Alignment); 16785 unsigned OldColumnLimit = Alignment.ColumnLimit; 16786 // We need to set ColumnLimit to zero, in order to stress nested alignments, 16787 // otherwise the function parameters will be re-flowed onto a single line. 16788 Alignment.ColumnLimit = 0; 16789 EXPECT_EQ("int a(int x,\n" 16790 " float y);\n" 16791 "double b(int x,\n" 16792 " double y);", 16793 format("int a(int x,\n" 16794 " float y);\n" 16795 "double b(int x,\n" 16796 " double y);", 16797 Alignment)); 16798 // This ensures that function parameters of function declarations are 16799 // correctly indented when their owning functions are indented. 16800 // The failure case here is for 'double y' to not be indented enough. 16801 EXPECT_EQ("double a(int x);\n" 16802 "int b(int y,\n" 16803 " double z);", 16804 format("double a(int x);\n" 16805 "int b(int y,\n" 16806 " double z);", 16807 Alignment)); 16808 // Set ColumnLimit low so that we induce wrapping immediately after 16809 // the function name and opening paren. 16810 Alignment.ColumnLimit = 13; 16811 verifyFormat("int function(\n" 16812 " int x,\n" 16813 " bool y);", 16814 Alignment); 16815 Alignment.ColumnLimit = OldColumnLimit; 16816 // Ensure function pointers don't screw up recursive alignment 16817 verifyFormat("int a(int x, void (*fp)(int y));\n" 16818 "double b();", 16819 Alignment); 16820 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16821 // Ensure recursive alignment is broken by function braces, so that the 16822 // "a = 1" does not align with subsequent assignments inside the function 16823 // body. 16824 verifyFormat("int func(int a = 1) {\n" 16825 " int b = 2;\n" 16826 " int cc = 3;\n" 16827 "}", 16828 Alignment); 16829 verifyFormat("float something = 2000;\n" 16830 "double another = 911;\n" 16831 "int i = 1, j = 10;\n" 16832 "const int *oneMore = 1;\n" 16833 "unsigned i = 2;", 16834 Alignment); 16835 verifyFormat("int oneTwoThree = {0}; // comment\n" 16836 "unsigned oneTwo = 0; // comment", 16837 Alignment); 16838 // Make sure that scope is correctly tracked, in the absence of braces 16839 verifyFormat("for (int i = 0; i < n; i++)\n" 16840 " j = i;\n" 16841 "double x = 1;\n", 16842 Alignment); 16843 verifyFormat("if (int i = 0)\n" 16844 " j = i;\n" 16845 "double x = 1;\n", 16846 Alignment); 16847 // Ensure operator[] and operator() are comprehended 16848 verifyFormat("struct test {\n" 16849 " long long int foo();\n" 16850 " int operator[](int a);\n" 16851 " double bar();\n" 16852 "};\n", 16853 Alignment); 16854 verifyFormat("struct test {\n" 16855 " long long int foo();\n" 16856 " int operator()(int a);\n" 16857 " double bar();\n" 16858 "};\n", 16859 Alignment); 16860 // http://llvm.org/PR52914 16861 verifyFormat("char *a[] = {\"a\", // comment\n" 16862 " \"bb\"};\n" 16863 "int bbbbbbb = 0;", 16864 Alignment); 16865 16866 // PAS_Right 16867 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16868 " int const i = 1;\n" 16869 " int *j = 2;\n" 16870 " int big = 10000;\n" 16871 "\n" 16872 " unsigned oneTwoThree = 123;\n" 16873 " int oneTwo = 12;\n" 16874 " method();\n" 16875 " float k = 2;\n" 16876 " int ll = 10000;\n" 16877 "}", 16878 format("void SomeFunction(int parameter= 0) {\n" 16879 " int const i= 1;\n" 16880 " int *j=2;\n" 16881 " int big = 10000;\n" 16882 "\n" 16883 "unsigned oneTwoThree =123;\n" 16884 "int oneTwo = 12;\n" 16885 " method();\n" 16886 "float k= 2;\n" 16887 "int ll=10000;\n" 16888 "}", 16889 Alignment)); 16890 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16891 " int const i = 1;\n" 16892 " int **j = 2, ***k;\n" 16893 " int &k = i;\n" 16894 " int &&l = i + j;\n" 16895 " int big = 10000;\n" 16896 "\n" 16897 " unsigned oneTwoThree = 123;\n" 16898 " int oneTwo = 12;\n" 16899 " method();\n" 16900 " float k = 2;\n" 16901 " int ll = 10000;\n" 16902 "}", 16903 format("void SomeFunction(int parameter= 0) {\n" 16904 " int const i= 1;\n" 16905 " int **j=2,***k;\n" 16906 "int &k=i;\n" 16907 "int &&l=i+j;\n" 16908 " int big = 10000;\n" 16909 "\n" 16910 "unsigned oneTwoThree =123;\n" 16911 "int oneTwo = 12;\n" 16912 " method();\n" 16913 "float k= 2;\n" 16914 "int ll=10000;\n" 16915 "}", 16916 Alignment)); 16917 // variables are aligned at their name, pointers are at the right most 16918 // position 16919 verifyFormat("int *a;\n" 16920 "int **b;\n" 16921 "int ***c;\n" 16922 "int foobar;\n", 16923 Alignment); 16924 16925 // PAS_Left 16926 FormatStyle AlignmentLeft = Alignment; 16927 AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left; 16928 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16929 " int const i = 1;\n" 16930 " int* j = 2;\n" 16931 " int big = 10000;\n" 16932 "\n" 16933 " unsigned oneTwoThree = 123;\n" 16934 " int oneTwo = 12;\n" 16935 " method();\n" 16936 " float k = 2;\n" 16937 " int ll = 10000;\n" 16938 "}", 16939 format("void SomeFunction(int parameter= 0) {\n" 16940 " int const i= 1;\n" 16941 " int *j=2;\n" 16942 " int big = 10000;\n" 16943 "\n" 16944 "unsigned oneTwoThree =123;\n" 16945 "int oneTwo = 12;\n" 16946 " method();\n" 16947 "float k= 2;\n" 16948 "int ll=10000;\n" 16949 "}", 16950 AlignmentLeft)); 16951 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16952 " int const i = 1;\n" 16953 " int** j = 2;\n" 16954 " int& k = i;\n" 16955 " int&& l = i + j;\n" 16956 " int big = 10000;\n" 16957 "\n" 16958 " unsigned oneTwoThree = 123;\n" 16959 " int oneTwo = 12;\n" 16960 " method();\n" 16961 " float k = 2;\n" 16962 " int ll = 10000;\n" 16963 "}", 16964 format("void SomeFunction(int parameter= 0) {\n" 16965 " int const i= 1;\n" 16966 " int **j=2;\n" 16967 "int &k=i;\n" 16968 "int &&l=i+j;\n" 16969 " int big = 10000;\n" 16970 "\n" 16971 "unsigned oneTwoThree =123;\n" 16972 "int oneTwo = 12;\n" 16973 " method();\n" 16974 "float k= 2;\n" 16975 "int ll=10000;\n" 16976 "}", 16977 AlignmentLeft)); 16978 // variables are aligned at their name, pointers are at the left most position 16979 verifyFormat("int* a;\n" 16980 "int** b;\n" 16981 "int*** c;\n" 16982 "int foobar;\n", 16983 AlignmentLeft); 16984 16985 // PAS_Middle 16986 FormatStyle AlignmentMiddle = Alignment; 16987 AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle; 16988 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16989 " int const i = 1;\n" 16990 " int * j = 2;\n" 16991 " int big = 10000;\n" 16992 "\n" 16993 " unsigned oneTwoThree = 123;\n" 16994 " int oneTwo = 12;\n" 16995 " method();\n" 16996 " float k = 2;\n" 16997 " int ll = 10000;\n" 16998 "}", 16999 format("void SomeFunction(int parameter= 0) {\n" 17000 " int const i= 1;\n" 17001 " int *j=2;\n" 17002 " int big = 10000;\n" 17003 "\n" 17004 "unsigned oneTwoThree =123;\n" 17005 "int oneTwo = 12;\n" 17006 " method();\n" 17007 "float k= 2;\n" 17008 "int ll=10000;\n" 17009 "}", 17010 AlignmentMiddle)); 17011 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 17012 " int const i = 1;\n" 17013 " int ** j = 2, ***k;\n" 17014 " int & k = i;\n" 17015 " int && l = i + j;\n" 17016 " int big = 10000;\n" 17017 "\n" 17018 " unsigned oneTwoThree = 123;\n" 17019 " int oneTwo = 12;\n" 17020 " method();\n" 17021 " float k = 2;\n" 17022 " int ll = 10000;\n" 17023 "}", 17024 format("void SomeFunction(int parameter= 0) {\n" 17025 " int const i= 1;\n" 17026 " int **j=2,***k;\n" 17027 "int &k=i;\n" 17028 "int &&l=i+j;\n" 17029 " int big = 10000;\n" 17030 "\n" 17031 "unsigned oneTwoThree =123;\n" 17032 "int oneTwo = 12;\n" 17033 " method();\n" 17034 "float k= 2;\n" 17035 "int ll=10000;\n" 17036 "}", 17037 AlignmentMiddle)); 17038 // variables are aligned at their name, pointers are in the middle 17039 verifyFormat("int * a;\n" 17040 "int * b;\n" 17041 "int *** c;\n" 17042 "int foobar;\n", 17043 AlignmentMiddle); 17044 17045 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 17046 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 17047 verifyFormat("#define A \\\n" 17048 " int aaaa = 12; \\\n" 17049 " float b = 23; \\\n" 17050 " const int ccc = 234; \\\n" 17051 " unsigned dddddddddd = 2345;", 17052 Alignment); 17053 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 17054 verifyFormat("#define A \\\n" 17055 " int aaaa = 12; \\\n" 17056 " float b = 23; \\\n" 17057 " const int ccc = 234; \\\n" 17058 " unsigned dddddddddd = 2345;", 17059 Alignment); 17060 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 17061 Alignment.ColumnLimit = 30; 17062 verifyFormat("#define A \\\n" 17063 " int aaaa = 12; \\\n" 17064 " float b = 23; \\\n" 17065 " const int ccc = 234; \\\n" 17066 " int dddddddddd = 2345;", 17067 Alignment); 17068 Alignment.ColumnLimit = 80; 17069 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 17070 "k = 4, int l = 5,\n" 17071 " int m = 6) {\n" 17072 " const int j = 10;\n" 17073 " otherThing = 1;\n" 17074 "}", 17075 Alignment); 17076 verifyFormat("void SomeFunction(int parameter = 0) {\n" 17077 " int const i = 1;\n" 17078 " int *j = 2;\n" 17079 " int big = 10000;\n" 17080 "}", 17081 Alignment); 17082 verifyFormat("class C {\n" 17083 "public:\n" 17084 " int i = 1;\n" 17085 " virtual void f() = 0;\n" 17086 "};", 17087 Alignment); 17088 verifyFormat("float i = 1;\n" 17089 "if (SomeType t = getSomething()) {\n" 17090 "}\n" 17091 "const unsigned j = 2;\n" 17092 "int big = 10000;", 17093 Alignment); 17094 verifyFormat("float j = 7;\n" 17095 "for (int k = 0; k < N; ++k) {\n" 17096 "}\n" 17097 "unsigned j = 2;\n" 17098 "int big = 10000;\n" 17099 "}", 17100 Alignment); 17101 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 17102 verifyFormat("float i = 1;\n" 17103 "LooooooooooongType loooooooooooooooooooooongVariable\n" 17104 " = someLooooooooooooooooongFunction();\n" 17105 "int j = 2;", 17106 Alignment); 17107 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 17108 verifyFormat("int i = 1;\n" 17109 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 17110 " someLooooooooooooooooongFunction();\n" 17111 "int j = 2;", 17112 Alignment); 17113 17114 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 17115 verifyFormat("auto lambda = []() {\n" 17116 " auto ii = 0;\n" 17117 " float j = 0;\n" 17118 " return 0;\n" 17119 "};\n" 17120 "int i = 0;\n" 17121 "float i2 = 0;\n" 17122 "auto v = type{\n" 17123 " i = 1, //\n" 17124 " (i = 2), //\n" 17125 " i = 3 //\n" 17126 "};", 17127 Alignment); 17128 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 17129 17130 verifyFormat( 17131 "int i = 1;\n" 17132 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 17133 " loooooooooooooooooooooongParameterB);\n" 17134 "int j = 2;", 17135 Alignment); 17136 17137 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 17138 // We expect declarations and assignments to align, as long as it doesn't 17139 // exceed the column limit, starting a new alignment sequence whenever it 17140 // happens. 17141 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 17142 Alignment.ColumnLimit = 30; 17143 verifyFormat("float ii = 1;\n" 17144 "unsigned j = 2;\n" 17145 "int someVerylongVariable = 1;\n" 17146 "AnotherLongType ll = 123456;\n" 17147 "VeryVeryLongType k = 2;\n" 17148 "int myvar = 1;", 17149 Alignment); 17150 Alignment.ColumnLimit = 80; 17151 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 17152 17153 verifyFormat( 17154 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 17155 " typename LongType, typename B>\n" 17156 "auto foo() {}\n", 17157 Alignment); 17158 verifyFormat("float a, b = 1;\n" 17159 "int c = 2;\n" 17160 "int dd = 3;\n", 17161 Alignment); 17162 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 17163 "float b[1][] = {{3.f}};\n", 17164 Alignment); 17165 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 17166 verifyFormat("float a, b = 1;\n" 17167 "int c = 2;\n" 17168 "int dd = 3;\n", 17169 Alignment); 17170 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 17171 "float b[1][] = {{3.f}};\n", 17172 Alignment); 17173 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 17174 17175 Alignment.ColumnLimit = 30; 17176 Alignment.BinPackParameters = false; 17177 verifyFormat("void foo(float a,\n" 17178 " float b,\n" 17179 " int c,\n" 17180 " uint32_t *d) {\n" 17181 " int *e = 0;\n" 17182 " float f = 0;\n" 17183 " double g = 0;\n" 17184 "}\n" 17185 "void bar(ino_t a,\n" 17186 " int b,\n" 17187 " uint32_t *c,\n" 17188 " bool d) {}\n", 17189 Alignment); 17190 Alignment.BinPackParameters = true; 17191 Alignment.ColumnLimit = 80; 17192 17193 // Bug 33507 17194 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 17195 verifyFormat( 17196 "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n" 17197 " static const Version verVs2017;\n" 17198 " return true;\n" 17199 "});\n", 17200 Alignment); 17201 Alignment.PointerAlignment = FormatStyle::PAS_Right; 17202 17203 // See llvm.org/PR35641 17204 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 17205 verifyFormat("int func() { //\n" 17206 " int b;\n" 17207 " unsigned c;\n" 17208 "}", 17209 Alignment); 17210 17211 // See PR37175 17212 FormatStyle Style = getMozillaStyle(); 17213 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 17214 EXPECT_EQ("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n" 17215 "foo(int a);", 17216 format("DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style)); 17217 17218 Alignment.PointerAlignment = FormatStyle::PAS_Left; 17219 verifyFormat("unsigned int* a;\n" 17220 "int* b;\n" 17221 "unsigned int Const* c;\n" 17222 "unsigned int const* d;\n" 17223 "unsigned int Const& e;\n" 17224 "unsigned int const& f;", 17225 Alignment); 17226 verifyFormat("Const unsigned int* c;\n" 17227 "const unsigned int* d;\n" 17228 "Const unsigned int& e;\n" 17229 "const unsigned int& f;\n" 17230 "const unsigned g;\n" 17231 "Const unsigned h;", 17232 Alignment); 17233 17234 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 17235 verifyFormat("unsigned int * a;\n" 17236 "int * b;\n" 17237 "unsigned int Const * c;\n" 17238 "unsigned int const * d;\n" 17239 "unsigned int Const & e;\n" 17240 "unsigned int const & f;", 17241 Alignment); 17242 verifyFormat("Const unsigned int * c;\n" 17243 "const unsigned int * d;\n" 17244 "Const unsigned int & e;\n" 17245 "const unsigned int & f;\n" 17246 "const unsigned g;\n" 17247 "Const unsigned h;", 17248 Alignment); 17249 } 17250 17251 TEST_F(FormatTest, AlignWithLineBreaks) { 17252 auto Style = getLLVMStyleWithColumns(120); 17253 17254 EXPECT_EQ(Style.AlignConsecutiveAssignments, FormatStyle::ACS_None); 17255 EXPECT_EQ(Style.AlignConsecutiveDeclarations, FormatStyle::ACS_None); 17256 verifyFormat("void foo() {\n" 17257 " int myVar = 5;\n" 17258 " double x = 3.14;\n" 17259 " auto str = \"Hello \"\n" 17260 " \"World\";\n" 17261 " auto s = \"Hello \"\n" 17262 " \"Again\";\n" 17263 "}", 17264 Style); 17265 17266 // clang-format off 17267 verifyFormat("void foo() {\n" 17268 " const int capacityBefore = Entries.capacity();\n" 17269 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 17270 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 17271 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 17272 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 17273 "}", 17274 Style); 17275 // clang-format on 17276 17277 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 17278 verifyFormat("void foo() {\n" 17279 " int myVar = 5;\n" 17280 " double x = 3.14;\n" 17281 " auto str = \"Hello \"\n" 17282 " \"World\";\n" 17283 " auto s = \"Hello \"\n" 17284 " \"Again\";\n" 17285 "}", 17286 Style); 17287 17288 // clang-format off 17289 verifyFormat("void foo() {\n" 17290 " const int capacityBefore = Entries.capacity();\n" 17291 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 17292 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 17293 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 17294 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 17295 "}", 17296 Style); 17297 // clang-format on 17298 17299 Style.AlignConsecutiveAssignments = FormatStyle::ACS_None; 17300 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 17301 verifyFormat("void foo() {\n" 17302 " int myVar = 5;\n" 17303 " double x = 3.14;\n" 17304 " auto str = \"Hello \"\n" 17305 " \"World\";\n" 17306 " auto s = \"Hello \"\n" 17307 " \"Again\";\n" 17308 "}", 17309 Style); 17310 17311 // clang-format off 17312 verifyFormat("void foo() {\n" 17313 " const int capacityBefore = Entries.capacity();\n" 17314 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 17315 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 17316 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 17317 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 17318 "}", 17319 Style); 17320 // clang-format on 17321 17322 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 17323 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 17324 17325 verifyFormat("void foo() {\n" 17326 " int myVar = 5;\n" 17327 " double x = 3.14;\n" 17328 " auto str = \"Hello \"\n" 17329 " \"World\";\n" 17330 " auto s = \"Hello \"\n" 17331 " \"Again\";\n" 17332 "}", 17333 Style); 17334 17335 // clang-format off 17336 verifyFormat("void foo() {\n" 17337 " const int capacityBefore = Entries.capacity();\n" 17338 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 17339 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 17340 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 17341 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 17342 "}", 17343 Style); 17344 // clang-format on 17345 17346 Style = getLLVMStyleWithColumns(120); 17347 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 17348 Style.ContinuationIndentWidth = 4; 17349 Style.IndentWidth = 4; 17350 17351 // clang-format off 17352 verifyFormat("void SomeFunc() {\n" 17353 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 17354 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 17355 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 17356 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 17357 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 17358 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 17359 "}", 17360 Style); 17361 // clang-format on 17362 17363 Style.BinPackArguments = false; 17364 17365 // clang-format off 17366 verifyFormat("void SomeFunc() {\n" 17367 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(\n" 17368 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 17369 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(\n" 17370 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 17371 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(\n" 17372 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 17373 "}", 17374 Style); 17375 // clang-format on 17376 } 17377 17378 TEST_F(FormatTest, AlignWithInitializerPeriods) { 17379 auto Style = getLLVMStyleWithColumns(60); 17380 17381 verifyFormat("void foo1(void) {\n" 17382 " BYTE p[1] = 1;\n" 17383 " A B = {.one_foooooooooooooooo = 2,\n" 17384 " .two_fooooooooooooo = 3,\n" 17385 " .three_fooooooooooooo = 4};\n" 17386 " BYTE payload = 2;\n" 17387 "}", 17388 Style); 17389 17390 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 17391 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_None; 17392 verifyFormat("void foo2(void) {\n" 17393 " BYTE p[1] = 1;\n" 17394 " A B = {.one_foooooooooooooooo = 2,\n" 17395 " .two_fooooooooooooo = 3,\n" 17396 " .three_fooooooooooooo = 4};\n" 17397 " BYTE payload = 2;\n" 17398 "}", 17399 Style); 17400 17401 Style.AlignConsecutiveAssignments = FormatStyle::ACS_None; 17402 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 17403 verifyFormat("void foo3(void) {\n" 17404 " BYTE p[1] = 1;\n" 17405 " A B = {.one_foooooooooooooooo = 2,\n" 17406 " .two_fooooooooooooo = 3,\n" 17407 " .three_fooooooooooooo = 4};\n" 17408 " BYTE payload = 2;\n" 17409 "}", 17410 Style); 17411 17412 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 17413 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 17414 verifyFormat("void foo4(void) {\n" 17415 " BYTE p[1] = 1;\n" 17416 " A B = {.one_foooooooooooooooo = 2,\n" 17417 " .two_fooooooooooooo = 3,\n" 17418 " .three_fooooooooooooo = 4};\n" 17419 " BYTE payload = 2;\n" 17420 "}", 17421 Style); 17422 } 17423 17424 TEST_F(FormatTest, LinuxBraceBreaking) { 17425 FormatStyle LinuxBraceStyle = getLLVMStyle(); 17426 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 17427 verifyFormat("namespace a\n" 17428 "{\n" 17429 "class A\n" 17430 "{\n" 17431 " void f()\n" 17432 " {\n" 17433 " if (true) {\n" 17434 " a();\n" 17435 " b();\n" 17436 " } else {\n" 17437 " a();\n" 17438 " }\n" 17439 " }\n" 17440 " void g() { return; }\n" 17441 "};\n" 17442 "struct B {\n" 17443 " int x;\n" 17444 "};\n" 17445 "} // namespace a\n", 17446 LinuxBraceStyle); 17447 verifyFormat("enum X {\n" 17448 " Y = 0,\n" 17449 "}\n", 17450 LinuxBraceStyle); 17451 verifyFormat("struct S {\n" 17452 " int Type;\n" 17453 " union {\n" 17454 " int x;\n" 17455 " double y;\n" 17456 " } Value;\n" 17457 " class C\n" 17458 " {\n" 17459 " MyFavoriteType Value;\n" 17460 " } Class;\n" 17461 "}\n", 17462 LinuxBraceStyle); 17463 } 17464 17465 TEST_F(FormatTest, MozillaBraceBreaking) { 17466 FormatStyle MozillaBraceStyle = getLLVMStyle(); 17467 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 17468 MozillaBraceStyle.FixNamespaceComments = false; 17469 verifyFormat("namespace a {\n" 17470 "class A\n" 17471 "{\n" 17472 " void f()\n" 17473 " {\n" 17474 " if (true) {\n" 17475 " a();\n" 17476 " b();\n" 17477 " }\n" 17478 " }\n" 17479 " void g() { return; }\n" 17480 "};\n" 17481 "enum E\n" 17482 "{\n" 17483 " A,\n" 17484 " // foo\n" 17485 " B,\n" 17486 " C\n" 17487 "};\n" 17488 "struct B\n" 17489 "{\n" 17490 " int x;\n" 17491 "};\n" 17492 "}\n", 17493 MozillaBraceStyle); 17494 verifyFormat("struct S\n" 17495 "{\n" 17496 " int Type;\n" 17497 " union\n" 17498 " {\n" 17499 " int x;\n" 17500 " double y;\n" 17501 " } Value;\n" 17502 " class C\n" 17503 " {\n" 17504 " MyFavoriteType Value;\n" 17505 " } Class;\n" 17506 "}\n", 17507 MozillaBraceStyle); 17508 } 17509 17510 TEST_F(FormatTest, StroustrupBraceBreaking) { 17511 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 17512 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 17513 verifyFormat("namespace a {\n" 17514 "class A {\n" 17515 " void f()\n" 17516 " {\n" 17517 " if (true) {\n" 17518 " a();\n" 17519 " b();\n" 17520 " }\n" 17521 " }\n" 17522 " void g() { return; }\n" 17523 "};\n" 17524 "struct B {\n" 17525 " int x;\n" 17526 "};\n" 17527 "} // namespace a\n", 17528 StroustrupBraceStyle); 17529 17530 verifyFormat("void foo()\n" 17531 "{\n" 17532 " if (a) {\n" 17533 " a();\n" 17534 " }\n" 17535 " else {\n" 17536 " b();\n" 17537 " }\n" 17538 "}\n", 17539 StroustrupBraceStyle); 17540 17541 verifyFormat("#ifdef _DEBUG\n" 17542 "int foo(int i = 0)\n" 17543 "#else\n" 17544 "int foo(int i = 5)\n" 17545 "#endif\n" 17546 "{\n" 17547 " return i;\n" 17548 "}", 17549 StroustrupBraceStyle); 17550 17551 verifyFormat("void foo() {}\n" 17552 "void bar()\n" 17553 "#ifdef _DEBUG\n" 17554 "{\n" 17555 " foo();\n" 17556 "}\n" 17557 "#else\n" 17558 "{\n" 17559 "}\n" 17560 "#endif", 17561 StroustrupBraceStyle); 17562 17563 verifyFormat("void foobar() { int i = 5; }\n" 17564 "#ifdef _DEBUG\n" 17565 "void bar() {}\n" 17566 "#else\n" 17567 "void bar() { foobar(); }\n" 17568 "#endif", 17569 StroustrupBraceStyle); 17570 } 17571 17572 TEST_F(FormatTest, AllmanBraceBreaking) { 17573 FormatStyle AllmanBraceStyle = getLLVMStyle(); 17574 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 17575 17576 EXPECT_EQ("namespace a\n" 17577 "{\n" 17578 "void f();\n" 17579 "void g();\n" 17580 "} // namespace a\n", 17581 format("namespace a\n" 17582 "{\n" 17583 "void f();\n" 17584 "void g();\n" 17585 "}\n", 17586 AllmanBraceStyle)); 17587 17588 verifyFormat("namespace a\n" 17589 "{\n" 17590 "class A\n" 17591 "{\n" 17592 " void f()\n" 17593 " {\n" 17594 " if (true)\n" 17595 " {\n" 17596 " a();\n" 17597 " b();\n" 17598 " }\n" 17599 " }\n" 17600 " void g() { return; }\n" 17601 "};\n" 17602 "struct B\n" 17603 "{\n" 17604 " int x;\n" 17605 "};\n" 17606 "union C\n" 17607 "{\n" 17608 "};\n" 17609 "} // namespace a", 17610 AllmanBraceStyle); 17611 17612 verifyFormat("void f()\n" 17613 "{\n" 17614 " if (true)\n" 17615 " {\n" 17616 " a();\n" 17617 " }\n" 17618 " else if (false)\n" 17619 " {\n" 17620 " b();\n" 17621 " }\n" 17622 " else\n" 17623 " {\n" 17624 " c();\n" 17625 " }\n" 17626 "}\n", 17627 AllmanBraceStyle); 17628 17629 verifyFormat("void f()\n" 17630 "{\n" 17631 " for (int i = 0; i < 10; ++i)\n" 17632 " {\n" 17633 " a();\n" 17634 " }\n" 17635 " while (false)\n" 17636 " {\n" 17637 " b();\n" 17638 " }\n" 17639 " do\n" 17640 " {\n" 17641 " c();\n" 17642 " } while (false)\n" 17643 "}\n", 17644 AllmanBraceStyle); 17645 17646 verifyFormat("void f(int a)\n" 17647 "{\n" 17648 " switch (a)\n" 17649 " {\n" 17650 " case 0:\n" 17651 " break;\n" 17652 " case 1:\n" 17653 " {\n" 17654 " break;\n" 17655 " }\n" 17656 " case 2:\n" 17657 " {\n" 17658 " }\n" 17659 " break;\n" 17660 " default:\n" 17661 " break;\n" 17662 " }\n" 17663 "}\n", 17664 AllmanBraceStyle); 17665 17666 verifyFormat("enum X\n" 17667 "{\n" 17668 " Y = 0,\n" 17669 "}\n", 17670 AllmanBraceStyle); 17671 verifyFormat("enum X\n" 17672 "{\n" 17673 " Y = 0\n" 17674 "}\n", 17675 AllmanBraceStyle); 17676 17677 verifyFormat("@interface BSApplicationController ()\n" 17678 "{\n" 17679 "@private\n" 17680 " id _extraIvar;\n" 17681 "}\n" 17682 "@end\n", 17683 AllmanBraceStyle); 17684 17685 verifyFormat("#ifdef _DEBUG\n" 17686 "int foo(int i = 0)\n" 17687 "#else\n" 17688 "int foo(int i = 5)\n" 17689 "#endif\n" 17690 "{\n" 17691 " return i;\n" 17692 "}", 17693 AllmanBraceStyle); 17694 17695 verifyFormat("void foo() {}\n" 17696 "void bar()\n" 17697 "#ifdef _DEBUG\n" 17698 "{\n" 17699 " foo();\n" 17700 "}\n" 17701 "#else\n" 17702 "{\n" 17703 "}\n" 17704 "#endif", 17705 AllmanBraceStyle); 17706 17707 verifyFormat("void foobar() { int i = 5; }\n" 17708 "#ifdef _DEBUG\n" 17709 "void bar() {}\n" 17710 "#else\n" 17711 "void bar() { foobar(); }\n" 17712 "#endif", 17713 AllmanBraceStyle); 17714 17715 EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine, 17716 FormatStyle::SLS_All); 17717 17718 verifyFormat("[](int i) { return i + 2; };\n" 17719 "[](int i, int j)\n" 17720 "{\n" 17721 " auto x = i + j;\n" 17722 " auto y = i * j;\n" 17723 " return x ^ y;\n" 17724 "};\n" 17725 "void foo()\n" 17726 "{\n" 17727 " auto shortLambda = [](int i) { return i + 2; };\n" 17728 " auto longLambda = [](int i, int j)\n" 17729 " {\n" 17730 " auto x = i + j;\n" 17731 " auto y = i * j;\n" 17732 " return x ^ y;\n" 17733 " };\n" 17734 "}", 17735 AllmanBraceStyle); 17736 17737 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 17738 17739 verifyFormat("[](int i)\n" 17740 "{\n" 17741 " return i + 2;\n" 17742 "};\n" 17743 "[](int i, int j)\n" 17744 "{\n" 17745 " auto x = i + j;\n" 17746 " auto y = i * j;\n" 17747 " return x ^ y;\n" 17748 "};\n" 17749 "void foo()\n" 17750 "{\n" 17751 " auto shortLambda = [](int i)\n" 17752 " {\n" 17753 " return i + 2;\n" 17754 " };\n" 17755 " auto longLambda = [](int i, int j)\n" 17756 " {\n" 17757 " auto x = i + j;\n" 17758 " auto y = i * j;\n" 17759 " return x ^ y;\n" 17760 " };\n" 17761 "}", 17762 AllmanBraceStyle); 17763 17764 // Reset 17765 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All; 17766 17767 // This shouldn't affect ObjC blocks.. 17768 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 17769 " // ...\n" 17770 " int i;\n" 17771 "}];", 17772 AllmanBraceStyle); 17773 verifyFormat("void (^block)(void) = ^{\n" 17774 " // ...\n" 17775 " int i;\n" 17776 "};", 17777 AllmanBraceStyle); 17778 // .. or dict literals. 17779 verifyFormat("void f()\n" 17780 "{\n" 17781 " // ...\n" 17782 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 17783 "}", 17784 AllmanBraceStyle); 17785 verifyFormat("void f()\n" 17786 "{\n" 17787 " // ...\n" 17788 " [object someMethod:@{a : @\"b\"}];\n" 17789 "}", 17790 AllmanBraceStyle); 17791 verifyFormat("int f()\n" 17792 "{ // comment\n" 17793 " return 42;\n" 17794 "}", 17795 AllmanBraceStyle); 17796 17797 AllmanBraceStyle.ColumnLimit = 19; 17798 verifyFormat("void f() { int i; }", AllmanBraceStyle); 17799 AllmanBraceStyle.ColumnLimit = 18; 17800 verifyFormat("void f()\n" 17801 "{\n" 17802 " int i;\n" 17803 "}", 17804 AllmanBraceStyle); 17805 AllmanBraceStyle.ColumnLimit = 80; 17806 17807 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 17808 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = 17809 FormatStyle::SIS_WithoutElse; 17810 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 17811 verifyFormat("void f(bool b)\n" 17812 "{\n" 17813 " if (b)\n" 17814 " {\n" 17815 " return;\n" 17816 " }\n" 17817 "}\n", 17818 BreakBeforeBraceShortIfs); 17819 verifyFormat("void f(bool b)\n" 17820 "{\n" 17821 " if constexpr (b)\n" 17822 " {\n" 17823 " return;\n" 17824 " }\n" 17825 "}\n", 17826 BreakBeforeBraceShortIfs); 17827 verifyFormat("void f(bool b)\n" 17828 "{\n" 17829 " if CONSTEXPR (b)\n" 17830 " {\n" 17831 " return;\n" 17832 " }\n" 17833 "}\n", 17834 BreakBeforeBraceShortIfs); 17835 verifyFormat("void f(bool b)\n" 17836 "{\n" 17837 " if (b) return;\n" 17838 "}\n", 17839 BreakBeforeBraceShortIfs); 17840 verifyFormat("void f(bool b)\n" 17841 "{\n" 17842 " if constexpr (b) return;\n" 17843 "}\n", 17844 BreakBeforeBraceShortIfs); 17845 verifyFormat("void f(bool b)\n" 17846 "{\n" 17847 " if CONSTEXPR (b) return;\n" 17848 "}\n", 17849 BreakBeforeBraceShortIfs); 17850 verifyFormat("void f(bool b)\n" 17851 "{\n" 17852 " while (b)\n" 17853 " {\n" 17854 " return;\n" 17855 " }\n" 17856 "}\n", 17857 BreakBeforeBraceShortIfs); 17858 } 17859 17860 TEST_F(FormatTest, WhitesmithsBraceBreaking) { 17861 FormatStyle WhitesmithsBraceStyle = getLLVMStyleWithColumns(0); 17862 WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 17863 17864 // Make a few changes to the style for testing purposes 17865 WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine = 17866 FormatStyle::SFS_Empty; 17867 WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 17868 17869 // FIXME: this test case can't decide whether there should be a blank line 17870 // after the ~D() line or not. It adds one if one doesn't exist in the test 17871 // and it removes the line if one exists. 17872 /* 17873 verifyFormat("class A;\n" 17874 "namespace B\n" 17875 " {\n" 17876 "class C;\n" 17877 "// Comment\n" 17878 "class D\n" 17879 " {\n" 17880 "public:\n" 17881 " D();\n" 17882 " ~D() {}\n" 17883 "private:\n" 17884 " enum E\n" 17885 " {\n" 17886 " F\n" 17887 " }\n" 17888 " };\n" 17889 " } // namespace B\n", 17890 WhitesmithsBraceStyle); 17891 */ 17892 17893 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None; 17894 verifyFormat("namespace a\n" 17895 " {\n" 17896 "class A\n" 17897 " {\n" 17898 " void f()\n" 17899 " {\n" 17900 " if (true)\n" 17901 " {\n" 17902 " a();\n" 17903 " b();\n" 17904 " }\n" 17905 " }\n" 17906 " void g()\n" 17907 " {\n" 17908 " return;\n" 17909 " }\n" 17910 " };\n" 17911 "struct B\n" 17912 " {\n" 17913 " int x;\n" 17914 " };\n" 17915 " } // namespace a", 17916 WhitesmithsBraceStyle); 17917 17918 verifyFormat("namespace a\n" 17919 " {\n" 17920 "namespace b\n" 17921 " {\n" 17922 "class A\n" 17923 " {\n" 17924 " void f()\n" 17925 " {\n" 17926 " if (true)\n" 17927 " {\n" 17928 " a();\n" 17929 " b();\n" 17930 " }\n" 17931 " }\n" 17932 " void g()\n" 17933 " {\n" 17934 " return;\n" 17935 " }\n" 17936 " };\n" 17937 "struct B\n" 17938 " {\n" 17939 " int x;\n" 17940 " };\n" 17941 " } // namespace b\n" 17942 " } // namespace a", 17943 WhitesmithsBraceStyle); 17944 17945 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner; 17946 verifyFormat("namespace a\n" 17947 " {\n" 17948 "namespace b\n" 17949 " {\n" 17950 " class A\n" 17951 " {\n" 17952 " void f()\n" 17953 " {\n" 17954 " if (true)\n" 17955 " {\n" 17956 " a();\n" 17957 " b();\n" 17958 " }\n" 17959 " }\n" 17960 " void g()\n" 17961 " {\n" 17962 " return;\n" 17963 " }\n" 17964 " };\n" 17965 " struct B\n" 17966 " {\n" 17967 " int x;\n" 17968 " };\n" 17969 " } // namespace b\n" 17970 " } // namespace a", 17971 WhitesmithsBraceStyle); 17972 17973 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All; 17974 verifyFormat("namespace a\n" 17975 " {\n" 17976 " namespace b\n" 17977 " {\n" 17978 " class A\n" 17979 " {\n" 17980 " void f()\n" 17981 " {\n" 17982 " if (true)\n" 17983 " {\n" 17984 " a();\n" 17985 " b();\n" 17986 " }\n" 17987 " }\n" 17988 " void g()\n" 17989 " {\n" 17990 " return;\n" 17991 " }\n" 17992 " };\n" 17993 " struct B\n" 17994 " {\n" 17995 " int x;\n" 17996 " };\n" 17997 " } // namespace b\n" 17998 " } // namespace a", 17999 WhitesmithsBraceStyle); 18000 18001 verifyFormat("void f()\n" 18002 " {\n" 18003 " if (true)\n" 18004 " {\n" 18005 " a();\n" 18006 " }\n" 18007 " else if (false)\n" 18008 " {\n" 18009 " b();\n" 18010 " }\n" 18011 " else\n" 18012 " {\n" 18013 " c();\n" 18014 " }\n" 18015 " }\n", 18016 WhitesmithsBraceStyle); 18017 18018 verifyFormat("void f()\n" 18019 " {\n" 18020 " for (int i = 0; i < 10; ++i)\n" 18021 " {\n" 18022 " a();\n" 18023 " }\n" 18024 " while (false)\n" 18025 " {\n" 18026 " b();\n" 18027 " }\n" 18028 " do\n" 18029 " {\n" 18030 " c();\n" 18031 " } while (false)\n" 18032 " }\n", 18033 WhitesmithsBraceStyle); 18034 18035 WhitesmithsBraceStyle.IndentCaseLabels = true; 18036 verifyFormat("void switchTest1(int a)\n" 18037 " {\n" 18038 " switch (a)\n" 18039 " {\n" 18040 " case 2:\n" 18041 " {\n" 18042 " }\n" 18043 " break;\n" 18044 " }\n" 18045 " }\n", 18046 WhitesmithsBraceStyle); 18047 18048 verifyFormat("void switchTest2(int a)\n" 18049 " {\n" 18050 " switch (a)\n" 18051 " {\n" 18052 " case 0:\n" 18053 " break;\n" 18054 " case 1:\n" 18055 " {\n" 18056 " break;\n" 18057 " }\n" 18058 " case 2:\n" 18059 " {\n" 18060 " }\n" 18061 " break;\n" 18062 " default:\n" 18063 " break;\n" 18064 " }\n" 18065 " }\n", 18066 WhitesmithsBraceStyle); 18067 18068 verifyFormat("void switchTest3(int a)\n" 18069 " {\n" 18070 " switch (a)\n" 18071 " {\n" 18072 " case 0:\n" 18073 " {\n" 18074 " foo(x);\n" 18075 " }\n" 18076 " break;\n" 18077 " default:\n" 18078 " {\n" 18079 " foo(1);\n" 18080 " }\n" 18081 " break;\n" 18082 " }\n" 18083 " }\n", 18084 WhitesmithsBraceStyle); 18085 18086 WhitesmithsBraceStyle.IndentCaseLabels = false; 18087 18088 verifyFormat("void switchTest4(int a)\n" 18089 " {\n" 18090 " switch (a)\n" 18091 " {\n" 18092 " case 2:\n" 18093 " {\n" 18094 " }\n" 18095 " break;\n" 18096 " }\n" 18097 " }\n", 18098 WhitesmithsBraceStyle); 18099 18100 verifyFormat("void switchTest5(int a)\n" 18101 " {\n" 18102 " switch (a)\n" 18103 " {\n" 18104 " case 0:\n" 18105 " break;\n" 18106 " case 1:\n" 18107 " {\n" 18108 " foo();\n" 18109 " break;\n" 18110 " }\n" 18111 " case 2:\n" 18112 " {\n" 18113 " }\n" 18114 " break;\n" 18115 " default:\n" 18116 " break;\n" 18117 " }\n" 18118 " }\n", 18119 WhitesmithsBraceStyle); 18120 18121 verifyFormat("void switchTest6(int a)\n" 18122 " {\n" 18123 " switch (a)\n" 18124 " {\n" 18125 " case 0:\n" 18126 " {\n" 18127 " foo(x);\n" 18128 " }\n" 18129 " break;\n" 18130 " default:\n" 18131 " {\n" 18132 " foo(1);\n" 18133 " }\n" 18134 " break;\n" 18135 " }\n" 18136 " }\n", 18137 WhitesmithsBraceStyle); 18138 18139 verifyFormat("enum X\n" 18140 " {\n" 18141 " Y = 0, // testing\n" 18142 " }\n", 18143 WhitesmithsBraceStyle); 18144 18145 verifyFormat("enum X\n" 18146 " {\n" 18147 " Y = 0\n" 18148 " }\n", 18149 WhitesmithsBraceStyle); 18150 verifyFormat("enum X\n" 18151 " {\n" 18152 " Y = 0,\n" 18153 " Z = 1\n" 18154 " };\n", 18155 WhitesmithsBraceStyle); 18156 18157 verifyFormat("@interface BSApplicationController ()\n" 18158 " {\n" 18159 "@private\n" 18160 " id _extraIvar;\n" 18161 " }\n" 18162 "@end\n", 18163 WhitesmithsBraceStyle); 18164 18165 verifyFormat("#ifdef _DEBUG\n" 18166 "int foo(int i = 0)\n" 18167 "#else\n" 18168 "int foo(int i = 5)\n" 18169 "#endif\n" 18170 " {\n" 18171 " return i;\n" 18172 " }", 18173 WhitesmithsBraceStyle); 18174 18175 verifyFormat("void foo() {}\n" 18176 "void bar()\n" 18177 "#ifdef _DEBUG\n" 18178 " {\n" 18179 " foo();\n" 18180 " }\n" 18181 "#else\n" 18182 " {\n" 18183 " }\n" 18184 "#endif", 18185 WhitesmithsBraceStyle); 18186 18187 verifyFormat("void foobar()\n" 18188 " {\n" 18189 " int i = 5;\n" 18190 " }\n" 18191 "#ifdef _DEBUG\n" 18192 "void bar()\n" 18193 " {\n" 18194 " }\n" 18195 "#else\n" 18196 "void bar()\n" 18197 " {\n" 18198 " foobar();\n" 18199 " }\n" 18200 "#endif", 18201 WhitesmithsBraceStyle); 18202 18203 // This shouldn't affect ObjC blocks.. 18204 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 18205 " // ...\n" 18206 " int i;\n" 18207 "}];", 18208 WhitesmithsBraceStyle); 18209 verifyFormat("void (^block)(void) = ^{\n" 18210 " // ...\n" 18211 " int i;\n" 18212 "};", 18213 WhitesmithsBraceStyle); 18214 // .. or dict literals. 18215 verifyFormat("void f()\n" 18216 " {\n" 18217 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 18218 " }", 18219 WhitesmithsBraceStyle); 18220 18221 verifyFormat("int f()\n" 18222 " { // comment\n" 18223 " return 42;\n" 18224 " }", 18225 WhitesmithsBraceStyle); 18226 18227 FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle; 18228 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = 18229 FormatStyle::SIS_OnlyFirstIf; 18230 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 18231 verifyFormat("void f(bool b)\n" 18232 " {\n" 18233 " if (b)\n" 18234 " {\n" 18235 " return;\n" 18236 " }\n" 18237 " }\n", 18238 BreakBeforeBraceShortIfs); 18239 verifyFormat("void f(bool b)\n" 18240 " {\n" 18241 " if (b) return;\n" 18242 " }\n", 18243 BreakBeforeBraceShortIfs); 18244 verifyFormat("void f(bool b)\n" 18245 " {\n" 18246 " while (b)\n" 18247 " {\n" 18248 " return;\n" 18249 " }\n" 18250 " }\n", 18251 BreakBeforeBraceShortIfs); 18252 } 18253 18254 TEST_F(FormatTest, GNUBraceBreaking) { 18255 FormatStyle GNUBraceStyle = getLLVMStyle(); 18256 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 18257 verifyFormat("namespace a\n" 18258 "{\n" 18259 "class A\n" 18260 "{\n" 18261 " void f()\n" 18262 " {\n" 18263 " int a;\n" 18264 " {\n" 18265 " int b;\n" 18266 " }\n" 18267 " if (true)\n" 18268 " {\n" 18269 " a();\n" 18270 " b();\n" 18271 " }\n" 18272 " }\n" 18273 " void g() { return; }\n" 18274 "}\n" 18275 "} // namespace a", 18276 GNUBraceStyle); 18277 18278 verifyFormat("void f()\n" 18279 "{\n" 18280 " if (true)\n" 18281 " {\n" 18282 " a();\n" 18283 " }\n" 18284 " else if (false)\n" 18285 " {\n" 18286 " b();\n" 18287 " }\n" 18288 " else\n" 18289 " {\n" 18290 " c();\n" 18291 " }\n" 18292 "}\n", 18293 GNUBraceStyle); 18294 18295 verifyFormat("void f()\n" 18296 "{\n" 18297 " for (int i = 0; i < 10; ++i)\n" 18298 " {\n" 18299 " a();\n" 18300 " }\n" 18301 " while (false)\n" 18302 " {\n" 18303 " b();\n" 18304 " }\n" 18305 " do\n" 18306 " {\n" 18307 " c();\n" 18308 " }\n" 18309 " while (false);\n" 18310 "}\n", 18311 GNUBraceStyle); 18312 18313 verifyFormat("void f(int a)\n" 18314 "{\n" 18315 " switch (a)\n" 18316 " {\n" 18317 " case 0:\n" 18318 " break;\n" 18319 " case 1:\n" 18320 " {\n" 18321 " break;\n" 18322 " }\n" 18323 " case 2:\n" 18324 " {\n" 18325 " }\n" 18326 " break;\n" 18327 " default:\n" 18328 " break;\n" 18329 " }\n" 18330 "}\n", 18331 GNUBraceStyle); 18332 18333 verifyFormat("enum X\n" 18334 "{\n" 18335 " Y = 0,\n" 18336 "}\n", 18337 GNUBraceStyle); 18338 18339 verifyFormat("@interface BSApplicationController ()\n" 18340 "{\n" 18341 "@private\n" 18342 " id _extraIvar;\n" 18343 "}\n" 18344 "@end\n", 18345 GNUBraceStyle); 18346 18347 verifyFormat("#ifdef _DEBUG\n" 18348 "int foo(int i = 0)\n" 18349 "#else\n" 18350 "int foo(int i = 5)\n" 18351 "#endif\n" 18352 "{\n" 18353 " return i;\n" 18354 "}", 18355 GNUBraceStyle); 18356 18357 verifyFormat("void foo() {}\n" 18358 "void bar()\n" 18359 "#ifdef _DEBUG\n" 18360 "{\n" 18361 " foo();\n" 18362 "}\n" 18363 "#else\n" 18364 "{\n" 18365 "}\n" 18366 "#endif", 18367 GNUBraceStyle); 18368 18369 verifyFormat("void foobar() { int i = 5; }\n" 18370 "#ifdef _DEBUG\n" 18371 "void bar() {}\n" 18372 "#else\n" 18373 "void bar() { foobar(); }\n" 18374 "#endif", 18375 GNUBraceStyle); 18376 } 18377 18378 TEST_F(FormatTest, WebKitBraceBreaking) { 18379 FormatStyle WebKitBraceStyle = getLLVMStyle(); 18380 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 18381 WebKitBraceStyle.FixNamespaceComments = false; 18382 verifyFormat("namespace a {\n" 18383 "class A {\n" 18384 " void f()\n" 18385 " {\n" 18386 " if (true) {\n" 18387 " a();\n" 18388 " b();\n" 18389 " }\n" 18390 " }\n" 18391 " void g() { return; }\n" 18392 "};\n" 18393 "enum E {\n" 18394 " A,\n" 18395 " // foo\n" 18396 " B,\n" 18397 " C\n" 18398 "};\n" 18399 "struct B {\n" 18400 " int x;\n" 18401 "};\n" 18402 "}\n", 18403 WebKitBraceStyle); 18404 verifyFormat("struct S {\n" 18405 " int Type;\n" 18406 " union {\n" 18407 " int x;\n" 18408 " double y;\n" 18409 " } Value;\n" 18410 " class C {\n" 18411 " MyFavoriteType Value;\n" 18412 " } Class;\n" 18413 "};\n", 18414 WebKitBraceStyle); 18415 } 18416 18417 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 18418 verifyFormat("void f() {\n" 18419 " try {\n" 18420 " } catch (const Exception &e) {\n" 18421 " }\n" 18422 "}\n", 18423 getLLVMStyle()); 18424 } 18425 18426 TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) { 18427 auto Style = getLLVMStyle(); 18428 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 18429 Style.AlignConsecutiveAssignments = 18430 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 18431 Style.AlignConsecutiveDeclarations = 18432 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 18433 verifyFormat("struct test demo[] = {\n" 18434 " {56, 23, \"hello\"},\n" 18435 " {-1, 93463, \"world\"},\n" 18436 " { 7, 5, \"!!\"}\n" 18437 "};\n", 18438 Style); 18439 18440 verifyFormat("struct test demo[] = {\n" 18441 " {56, 23, \"hello\"}, // first line\n" 18442 " {-1, 93463, \"world\"}, // second line\n" 18443 " { 7, 5, \"!!\"} // third line\n" 18444 "};\n", 18445 Style); 18446 18447 verifyFormat("struct test demo[4] = {\n" 18448 " { 56, 23, 21, \"oh\"}, // first line\n" 18449 " { -1, 93463, 22, \"my\"}, // second line\n" 18450 " { 7, 5, 1, \"goodness\"} // third line\n" 18451 " {234, 5, 1, \"gracious\"} // fourth line\n" 18452 "};\n", 18453 Style); 18454 18455 verifyFormat("struct test demo[3] = {\n" 18456 " {56, 23, \"hello\"},\n" 18457 " {-1, 93463, \"world\"},\n" 18458 " { 7, 5, \"!!\"}\n" 18459 "};\n", 18460 Style); 18461 18462 verifyFormat("struct test demo[3] = {\n" 18463 " {int{56}, 23, \"hello\"},\n" 18464 " {int{-1}, 93463, \"world\"},\n" 18465 " { int{7}, 5, \"!!\"}\n" 18466 "};\n", 18467 Style); 18468 18469 verifyFormat("struct test demo[] = {\n" 18470 " {56, 23, \"hello\"},\n" 18471 " {-1, 93463, \"world\"},\n" 18472 " { 7, 5, \"!!\"},\n" 18473 "};\n", 18474 Style); 18475 18476 verifyFormat("test demo[] = {\n" 18477 " {56, 23, \"hello\"},\n" 18478 " {-1, 93463, \"world\"},\n" 18479 " { 7, 5, \"!!\"},\n" 18480 "};\n", 18481 Style); 18482 18483 verifyFormat("demo = std::array<struct test, 3>{\n" 18484 " test{56, 23, \"hello\"},\n" 18485 " test{-1, 93463, \"world\"},\n" 18486 " test{ 7, 5, \"!!\"},\n" 18487 "};\n", 18488 Style); 18489 18490 verifyFormat("test demo[] = {\n" 18491 " {56, 23, \"hello\"},\n" 18492 "#if X\n" 18493 " {-1, 93463, \"world\"},\n" 18494 "#endif\n" 18495 " { 7, 5, \"!!\"}\n" 18496 "};\n", 18497 Style); 18498 18499 verifyFormat( 18500 "test demo[] = {\n" 18501 " { 7, 23,\n" 18502 " \"hello world i am a very long line that really, in any\"\n" 18503 " \"just world, ought to be split over multiple lines\"},\n" 18504 " {-1, 93463, \"world\"},\n" 18505 " {56, 5, \"!!\"}\n" 18506 "};\n", 18507 Style); 18508 18509 verifyFormat("return GradForUnaryCwise(g, {\n" 18510 " {{\"sign\"}, \"Sign\", " 18511 " {\"x\", \"dy\"}},\n" 18512 " { {\"dx\"}, \"Mul\", {\"dy\"" 18513 ", \"sign\"}},\n" 18514 "});\n", 18515 Style); 18516 18517 Style.ColumnLimit = 0; 18518 EXPECT_EQ( 18519 "test demo[] = {\n" 18520 " {56, 23, \"hello world i am a very long line that really, " 18521 "in any just world, ought to be split over multiple lines\"},\n" 18522 " {-1, 93463, " 18523 " \"world\"},\n" 18524 " { 7, 5, " 18525 " \"!!\"},\n" 18526 "};", 18527 format("test demo[] = {{56, 23, \"hello world i am a very long line " 18528 "that really, in any just world, ought to be split over multiple " 18529 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 18530 Style)); 18531 18532 Style.ColumnLimit = 80; 18533 verifyFormat("test demo[] = {\n" 18534 " {56, 23, /* a comment */ \"hello\"},\n" 18535 " {-1, 93463, \"world\"},\n" 18536 " { 7, 5, \"!!\"}\n" 18537 "};\n", 18538 Style); 18539 18540 verifyFormat("test demo[] = {\n" 18541 " {56, 23, \"hello\"},\n" 18542 " {-1, 93463, \"world\" /* comment here */},\n" 18543 " { 7, 5, \"!!\"}\n" 18544 "};\n", 18545 Style); 18546 18547 verifyFormat("test demo[] = {\n" 18548 " {56, /* a comment */ 23, \"hello\"},\n" 18549 " {-1, 93463, \"world\"},\n" 18550 " { 7, 5, \"!!\"}\n" 18551 "};\n", 18552 Style); 18553 18554 Style.ColumnLimit = 20; 18555 EXPECT_EQ( 18556 "demo = std::array<\n" 18557 " struct test, 3>{\n" 18558 " test{\n" 18559 " 56, 23,\n" 18560 " \"hello \"\n" 18561 " \"world i \"\n" 18562 " \"am a very \"\n" 18563 " \"long line \"\n" 18564 " \"that \"\n" 18565 " \"really, \"\n" 18566 " \"in any \"\n" 18567 " \"just \"\n" 18568 " \"world, \"\n" 18569 " \"ought to \"\n" 18570 " \"be split \"\n" 18571 " \"over \"\n" 18572 " \"multiple \"\n" 18573 " \"lines\"},\n" 18574 " test{-1, 93463,\n" 18575 " \"world\"},\n" 18576 " test{ 7, 5,\n" 18577 " \"!!\" },\n" 18578 "};", 18579 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world " 18580 "i am a very long line that really, in any just world, ought " 18581 "to be split over multiple lines\"},test{-1, 93463, \"world\"}," 18582 "test{7, 5, \"!!\"},};", 18583 Style)); 18584 // This caused a core dump by enabling Alignment in the LLVMStyle globally 18585 Style = getLLVMStyleWithColumns(50); 18586 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 18587 verifyFormat("static A x = {\n" 18588 " {{init1, init2, init3, init4},\n" 18589 " {init1, init2, init3, init4}}\n" 18590 "};", 18591 Style); 18592 Style.ColumnLimit = 100; 18593 EXPECT_EQ( 18594 "test demo[] = {\n" 18595 " {56, 23,\n" 18596 " \"hello world i am a very long line that really, in any just world" 18597 ", ought to be split over \"\n" 18598 " \"multiple lines\" },\n" 18599 " {-1, 93463, \"world\"},\n" 18600 " { 7, 5, \"!!\"},\n" 18601 "};", 18602 format("test demo[] = {{56, 23, \"hello world i am a very long line " 18603 "that really, in any just world, ought to be split over multiple " 18604 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 18605 Style)); 18606 18607 Style = getLLVMStyleWithColumns(50); 18608 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 18609 Style.AlignConsecutiveAssignments = 18610 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 18611 Style.AlignConsecutiveDeclarations = 18612 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 18613 verifyFormat("struct test demo[] = {\n" 18614 " {56, 23, \"hello\"},\n" 18615 " {-1, 93463, \"world\"},\n" 18616 " { 7, 5, \"!!\"}\n" 18617 "};\n" 18618 "static A x = {\n" 18619 " {{init1, init2, init3, init4},\n" 18620 " {init1, init2, init3, init4}}\n" 18621 "};", 18622 Style); 18623 Style.ColumnLimit = 100; 18624 Style.AlignConsecutiveAssignments = 18625 FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments; 18626 Style.AlignConsecutiveDeclarations = 18627 FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments; 18628 verifyFormat("struct test demo[] = {\n" 18629 " {56, 23, \"hello\"},\n" 18630 " {-1, 93463, \"world\"},\n" 18631 " { 7, 5, \"!!\"}\n" 18632 "};\n" 18633 "struct test demo[4] = {\n" 18634 " { 56, 23, 21, \"oh\"}, // first line\n" 18635 " { -1, 93463, 22, \"my\"}, // second line\n" 18636 " { 7, 5, 1, \"goodness\"} // third line\n" 18637 " {234, 5, 1, \"gracious\"} // fourth line\n" 18638 "};\n", 18639 Style); 18640 EXPECT_EQ( 18641 "test demo[] = {\n" 18642 " {56,\n" 18643 " \"hello world i am a very long line that really, in any just world" 18644 ", ought to be split over \"\n" 18645 " \"multiple lines\", 23},\n" 18646 " {-1, \"world\", 93463},\n" 18647 " { 7, \"!!\", 5},\n" 18648 "};", 18649 format("test demo[] = {{56, \"hello world i am a very long line " 18650 "that really, in any just world, ought to be split over multiple " 18651 "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};", 18652 Style)); 18653 } 18654 18655 TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) { 18656 auto Style = getLLVMStyle(); 18657 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; 18658 /* FIXME: This case gets misformatted. 18659 verifyFormat("auto foo = Items{\n" 18660 " Section{0, bar(), },\n" 18661 " Section{1, boo() }\n" 18662 "};\n", 18663 Style); 18664 */ 18665 verifyFormat("auto foo = Items{\n" 18666 " Section{\n" 18667 " 0, bar(),\n" 18668 " }\n" 18669 "};\n", 18670 Style); 18671 verifyFormat("struct test demo[] = {\n" 18672 " {56, 23, \"hello\"},\n" 18673 " {-1, 93463, \"world\"},\n" 18674 " {7, 5, \"!!\" }\n" 18675 "};\n", 18676 Style); 18677 verifyFormat("struct test demo[] = {\n" 18678 " {56, 23, \"hello\"}, // first line\n" 18679 " {-1, 93463, \"world\"}, // second line\n" 18680 " {7, 5, \"!!\" } // third line\n" 18681 "};\n", 18682 Style); 18683 verifyFormat("struct test demo[4] = {\n" 18684 " {56, 23, 21, \"oh\" }, // first line\n" 18685 " {-1, 93463, 22, \"my\" }, // second line\n" 18686 " {7, 5, 1, \"goodness\"} // third line\n" 18687 " {234, 5, 1, \"gracious\"} // fourth line\n" 18688 "};\n", 18689 Style); 18690 verifyFormat("struct test demo[3] = {\n" 18691 " {56, 23, \"hello\"},\n" 18692 " {-1, 93463, \"world\"},\n" 18693 " {7, 5, \"!!\" }\n" 18694 "};\n", 18695 Style); 18696 18697 verifyFormat("struct test demo[3] = {\n" 18698 " {int{56}, 23, \"hello\"},\n" 18699 " {int{-1}, 93463, \"world\"},\n" 18700 " {int{7}, 5, \"!!\" }\n" 18701 "};\n", 18702 Style); 18703 verifyFormat("struct test demo[] = {\n" 18704 " {56, 23, \"hello\"},\n" 18705 " {-1, 93463, \"world\"},\n" 18706 " {7, 5, \"!!\" },\n" 18707 "};\n", 18708 Style); 18709 verifyFormat("test demo[] = {\n" 18710 " {56, 23, \"hello\"},\n" 18711 " {-1, 93463, \"world\"},\n" 18712 " {7, 5, \"!!\" },\n" 18713 "};\n", 18714 Style); 18715 verifyFormat("demo = std::array<struct test, 3>{\n" 18716 " test{56, 23, \"hello\"},\n" 18717 " test{-1, 93463, \"world\"},\n" 18718 " test{7, 5, \"!!\" },\n" 18719 "};\n", 18720 Style); 18721 verifyFormat("test demo[] = {\n" 18722 " {56, 23, \"hello\"},\n" 18723 "#if X\n" 18724 " {-1, 93463, \"world\"},\n" 18725 "#endif\n" 18726 " {7, 5, \"!!\" }\n" 18727 "};\n", 18728 Style); 18729 verifyFormat( 18730 "test demo[] = {\n" 18731 " {7, 23,\n" 18732 " \"hello world i am a very long line that really, in any\"\n" 18733 " \"just world, ought to be split over multiple lines\"},\n" 18734 " {-1, 93463, \"world\" },\n" 18735 " {56, 5, \"!!\" }\n" 18736 "};\n", 18737 Style); 18738 18739 verifyFormat("return GradForUnaryCwise(g, {\n" 18740 " {{\"sign\"}, \"Sign\", {\"x\", " 18741 "\"dy\"} },\n" 18742 " {{\"dx\"}, \"Mul\", " 18743 "{\"dy\", \"sign\"}},\n" 18744 "});\n", 18745 Style); 18746 18747 Style.ColumnLimit = 0; 18748 EXPECT_EQ( 18749 "test demo[] = {\n" 18750 " {56, 23, \"hello world i am a very long line that really, in any " 18751 "just world, ought to be split over multiple lines\"},\n" 18752 " {-1, 93463, \"world\" " 18753 " },\n" 18754 " {7, 5, \"!!\" " 18755 " },\n" 18756 "};", 18757 format("test demo[] = {{56, 23, \"hello world i am a very long line " 18758 "that really, in any just world, ought to be split over multiple " 18759 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 18760 Style)); 18761 18762 Style.ColumnLimit = 80; 18763 verifyFormat("test demo[] = {\n" 18764 " {56, 23, /* a comment */ \"hello\"},\n" 18765 " {-1, 93463, \"world\" },\n" 18766 " {7, 5, \"!!\" }\n" 18767 "};\n", 18768 Style); 18769 18770 verifyFormat("test demo[] = {\n" 18771 " {56, 23, \"hello\" },\n" 18772 " {-1, 93463, \"world\" /* comment here */},\n" 18773 " {7, 5, \"!!\" }\n" 18774 "};\n", 18775 Style); 18776 18777 verifyFormat("test demo[] = {\n" 18778 " {56, /* a comment */ 23, \"hello\"},\n" 18779 " {-1, 93463, \"world\"},\n" 18780 " {7, 5, \"!!\" }\n" 18781 "};\n", 18782 Style); 18783 18784 Style.ColumnLimit = 20; 18785 EXPECT_EQ( 18786 "demo = std::array<\n" 18787 " struct test, 3>{\n" 18788 " test{\n" 18789 " 56, 23,\n" 18790 " \"hello \"\n" 18791 " \"world i \"\n" 18792 " \"am a very \"\n" 18793 " \"long line \"\n" 18794 " \"that \"\n" 18795 " \"really, \"\n" 18796 " \"in any \"\n" 18797 " \"just \"\n" 18798 " \"world, \"\n" 18799 " \"ought to \"\n" 18800 " \"be split \"\n" 18801 " \"over \"\n" 18802 " \"multiple \"\n" 18803 " \"lines\"},\n" 18804 " test{-1, 93463,\n" 18805 " \"world\"},\n" 18806 " test{7, 5,\n" 18807 " \"!!\" },\n" 18808 "};", 18809 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world " 18810 "i am a very long line that really, in any just world, ought " 18811 "to be split over multiple lines\"},test{-1, 93463, \"world\"}," 18812 "test{7, 5, \"!!\"},};", 18813 Style)); 18814 18815 // This caused a core dump by enabling Alignment in the LLVMStyle globally 18816 Style = getLLVMStyleWithColumns(50); 18817 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; 18818 verifyFormat("static A x = {\n" 18819 " {{init1, init2, init3, init4},\n" 18820 " {init1, init2, init3, init4}}\n" 18821 "};", 18822 Style); 18823 Style.ColumnLimit = 100; 18824 EXPECT_EQ( 18825 "test demo[] = {\n" 18826 " {56, 23,\n" 18827 " \"hello world i am a very long line that really, in any just world" 18828 ", ought to be split over \"\n" 18829 " \"multiple lines\" },\n" 18830 " {-1, 93463, \"world\"},\n" 18831 " {7, 5, \"!!\" },\n" 18832 "};", 18833 format("test demo[] = {{56, 23, \"hello world i am a very long line " 18834 "that really, in any just world, ought to be split over multiple " 18835 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 18836 Style)); 18837 } 18838 18839 TEST_F(FormatTest, UnderstandsPragmas) { 18840 verifyFormat("#pragma omp reduction(| : var)"); 18841 verifyFormat("#pragma omp reduction(+ : var)"); 18842 18843 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 18844 "(including parentheses).", 18845 format("#pragma mark Any non-hyphenated or hyphenated string " 18846 "(including parentheses).")); 18847 } 18848 18849 TEST_F(FormatTest, UnderstandPragmaOption) { 18850 verifyFormat("#pragma option -C -A"); 18851 18852 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 18853 } 18854 18855 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) { 18856 FormatStyle Style = getLLVMStyleWithColumns(20); 18857 18858 // See PR41213 18859 EXPECT_EQ("/*\n" 18860 " *\t9012345\n" 18861 " * /8901\n" 18862 " */", 18863 format("/*\n" 18864 " *\t9012345 /8901\n" 18865 " */", 18866 Style)); 18867 EXPECT_EQ("/*\n" 18868 " *345678\n" 18869 " *\t/8901\n" 18870 " */", 18871 format("/*\n" 18872 " *345678\t/8901\n" 18873 " */", 18874 Style)); 18875 18876 verifyFormat("int a; // the\n" 18877 " // comment", 18878 Style); 18879 EXPECT_EQ("int a; /* first line\n" 18880 " * second\n" 18881 " * line third\n" 18882 " * line\n" 18883 " */", 18884 format("int a; /* first line\n" 18885 " * second\n" 18886 " * line third\n" 18887 " * line\n" 18888 " */", 18889 Style)); 18890 EXPECT_EQ("int a; // first line\n" 18891 " // second\n" 18892 " // line third\n" 18893 " // line", 18894 format("int a; // first line\n" 18895 " // second line\n" 18896 " // third line", 18897 Style)); 18898 18899 Style.PenaltyExcessCharacter = 90; 18900 verifyFormat("int a; // the comment", Style); 18901 EXPECT_EQ("int a; // the comment\n" 18902 " // aaa", 18903 format("int a; // the comment aaa", Style)); 18904 EXPECT_EQ("int a; /* first line\n" 18905 " * second line\n" 18906 " * third line\n" 18907 " */", 18908 format("int a; /* first line\n" 18909 " * second line\n" 18910 " * third line\n" 18911 " */", 18912 Style)); 18913 EXPECT_EQ("int a; // first line\n" 18914 " // second line\n" 18915 " // third line", 18916 format("int a; // first line\n" 18917 " // second line\n" 18918 " // third line", 18919 Style)); 18920 // FIXME: Investigate why this is not getting the same layout as the test 18921 // above. 18922 EXPECT_EQ("int a; /* first line\n" 18923 " * second line\n" 18924 " * third line\n" 18925 " */", 18926 format("int a; /* first line second line third line" 18927 "\n*/", 18928 Style)); 18929 18930 EXPECT_EQ("// foo bar baz bazfoo\n" 18931 "// foo bar foo bar\n", 18932 format("// foo bar baz bazfoo\n" 18933 "// foo bar foo bar\n", 18934 Style)); 18935 EXPECT_EQ("// foo bar baz bazfoo\n" 18936 "// foo bar foo bar\n", 18937 format("// foo bar baz bazfoo\n" 18938 "// foo bar foo bar\n", 18939 Style)); 18940 18941 // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the 18942 // next one. 18943 EXPECT_EQ("// foo bar baz bazfoo\n" 18944 "// bar foo bar\n", 18945 format("// foo bar baz bazfoo bar\n" 18946 "// foo bar\n", 18947 Style)); 18948 18949 EXPECT_EQ("// foo bar baz bazfoo\n" 18950 "// foo bar baz bazfoo\n" 18951 "// bar foo bar\n", 18952 format("// foo bar baz bazfoo\n" 18953 "// foo bar baz bazfoo bar\n" 18954 "// foo bar\n", 18955 Style)); 18956 18957 EXPECT_EQ("// foo bar baz bazfoo\n" 18958 "// foo bar baz bazfoo\n" 18959 "// bar foo bar\n", 18960 format("// foo bar baz bazfoo\n" 18961 "// foo bar baz bazfoo bar\n" 18962 "// foo bar\n", 18963 Style)); 18964 18965 // Make sure we do not keep protruding characters if strict mode reflow is 18966 // cheaper than keeping protruding characters. 18967 Style.ColumnLimit = 21; 18968 EXPECT_EQ( 18969 "// foo foo foo foo\n" 18970 "// foo foo foo foo\n" 18971 "// foo foo foo foo\n", 18972 format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", Style)); 18973 18974 EXPECT_EQ("int a = /* long block\n" 18975 " comment */\n" 18976 " 42;", 18977 format("int a = /* long block comment */ 42;", Style)); 18978 } 18979 18980 TEST_F(FormatTest, BreakPenaltyAfterLParen) { 18981 FormatStyle Style = getLLVMStyle(); 18982 Style.ColumnLimit = 8; 18983 Style.PenaltyExcessCharacter = 15; 18984 verifyFormat("int foo(\n" 18985 " int aaaaaaaaaaaaaaaaaaaaaaaa);", 18986 Style); 18987 Style.PenaltyBreakOpenParenthesis = 200; 18988 EXPECT_EQ("int foo(int aaaaaaaaaaaaaaaaaaaaaaaa);", 18989 format("int foo(\n" 18990 " int aaaaaaaaaaaaaaaaaaaaaaaa);", 18991 Style)); 18992 } 18993 18994 TEST_F(FormatTest, BreakPenaltyAfterCastLParen) { 18995 FormatStyle Style = getLLVMStyle(); 18996 Style.ColumnLimit = 5; 18997 Style.PenaltyExcessCharacter = 150; 18998 verifyFormat("foo((\n" 18999 " int)aaaaaaaaaaaaaaaaaaaaaaaa);", 19000 19001 Style); 19002 Style.PenaltyBreakOpenParenthesis = 100000; 19003 EXPECT_EQ("foo((int)\n" 19004 " aaaaaaaaaaaaaaaaaaaaaaaa);", 19005 format("foo((\n" 19006 "int)aaaaaaaaaaaaaaaaaaaaaaaa);", 19007 Style)); 19008 } 19009 19010 TEST_F(FormatTest, BreakPenaltyAfterForLoopLParen) { 19011 FormatStyle Style = getLLVMStyle(); 19012 Style.ColumnLimit = 4; 19013 Style.PenaltyExcessCharacter = 100; 19014 verifyFormat("for (\n" 19015 " int iiiiiiiiiiiiiiiii =\n" 19016 " 0;\n" 19017 " iiiiiiiiiiiiiiiii <\n" 19018 " 2;\n" 19019 " iiiiiiiiiiiiiiiii++) {\n" 19020 "}", 19021 19022 Style); 19023 Style.PenaltyBreakOpenParenthesis = 1250; 19024 EXPECT_EQ("for (int iiiiiiiiiiiiiiiii =\n" 19025 " 0;\n" 19026 " iiiiiiiiiiiiiiiii <\n" 19027 " 2;\n" 19028 " iiiiiiiiiiiiiiiii++) {\n" 19029 "}", 19030 format("for (\n" 19031 " int iiiiiiiiiiiiiiiii =\n" 19032 " 0;\n" 19033 " iiiiiiiiiiiiiiiii <\n" 19034 " 2;\n" 19035 " iiiiiiiiiiiiiiiii++) {\n" 19036 "}", 19037 Style)); 19038 } 19039 19040 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 19041 for (size_t i = 1; i < Styles.size(); ++i) \ 19042 EXPECT_EQ(Styles[0], Styles[i]) \ 19043 << "Style #" << i << " of " << Styles.size() << " differs from Style #0" 19044 19045 TEST_F(FormatTest, GetsPredefinedStyleByName) { 19046 SmallVector<FormatStyle, 3> Styles; 19047 Styles.resize(3); 19048 19049 Styles[0] = getLLVMStyle(); 19050 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 19051 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 19052 EXPECT_ALL_STYLES_EQUAL(Styles); 19053 19054 Styles[0] = getGoogleStyle(); 19055 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 19056 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 19057 EXPECT_ALL_STYLES_EQUAL(Styles); 19058 19059 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 19060 EXPECT_TRUE( 19061 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 19062 EXPECT_TRUE( 19063 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 19064 EXPECT_ALL_STYLES_EQUAL(Styles); 19065 19066 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 19067 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 19068 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 19069 EXPECT_ALL_STYLES_EQUAL(Styles); 19070 19071 Styles[0] = getMozillaStyle(); 19072 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 19073 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 19074 EXPECT_ALL_STYLES_EQUAL(Styles); 19075 19076 Styles[0] = getWebKitStyle(); 19077 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 19078 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 19079 EXPECT_ALL_STYLES_EQUAL(Styles); 19080 19081 Styles[0] = getGNUStyle(); 19082 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 19083 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 19084 EXPECT_ALL_STYLES_EQUAL(Styles); 19085 19086 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 19087 } 19088 19089 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 19090 SmallVector<FormatStyle, 8> Styles; 19091 Styles.resize(2); 19092 19093 Styles[0] = getGoogleStyle(); 19094 Styles[1] = getLLVMStyle(); 19095 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 19096 EXPECT_ALL_STYLES_EQUAL(Styles); 19097 19098 Styles.resize(5); 19099 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 19100 Styles[1] = getLLVMStyle(); 19101 Styles[1].Language = FormatStyle::LK_JavaScript; 19102 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 19103 19104 Styles[2] = getLLVMStyle(); 19105 Styles[2].Language = FormatStyle::LK_JavaScript; 19106 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 19107 "BasedOnStyle: Google", 19108 &Styles[2]) 19109 .value()); 19110 19111 Styles[3] = getLLVMStyle(); 19112 Styles[3].Language = FormatStyle::LK_JavaScript; 19113 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 19114 "Language: JavaScript", 19115 &Styles[3]) 19116 .value()); 19117 19118 Styles[4] = getLLVMStyle(); 19119 Styles[4].Language = FormatStyle::LK_JavaScript; 19120 EXPECT_EQ(0, parseConfiguration("---\n" 19121 "BasedOnStyle: LLVM\n" 19122 "IndentWidth: 123\n" 19123 "---\n" 19124 "BasedOnStyle: Google\n" 19125 "Language: JavaScript", 19126 &Styles[4]) 19127 .value()); 19128 EXPECT_ALL_STYLES_EQUAL(Styles); 19129 } 19130 19131 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 19132 Style.FIELD = false; \ 19133 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 19134 EXPECT_TRUE(Style.FIELD); \ 19135 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 19136 EXPECT_FALSE(Style.FIELD); 19137 19138 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 19139 19140 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 19141 Style.STRUCT.FIELD = false; \ 19142 EXPECT_EQ(0, \ 19143 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 19144 .value()); \ 19145 EXPECT_TRUE(Style.STRUCT.FIELD); \ 19146 EXPECT_EQ(0, \ 19147 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 19148 .value()); \ 19149 EXPECT_FALSE(Style.STRUCT.FIELD); 19150 19151 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 19152 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 19153 19154 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 19155 EXPECT_NE(VALUE, Style.FIELD) << "Initial value already the same!"; \ 19156 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 19157 EXPECT_EQ(VALUE, Style.FIELD) << "Unexpected value after parsing!" 19158 19159 TEST_F(FormatTest, ParsesConfigurationBools) { 19160 FormatStyle Style = {}; 19161 Style.Language = FormatStyle::LK_Cpp; 19162 CHECK_PARSE_BOOL(AlignTrailingComments); 19163 CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine); 19164 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 19165 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 19166 CHECK_PARSE_BOOL(AllowShortEnumsOnASingleLine); 19167 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 19168 CHECK_PARSE_BOOL(BinPackArguments); 19169 CHECK_PARSE_BOOL(BinPackParameters); 19170 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 19171 CHECK_PARSE_BOOL(BreakBeforeConceptDeclarations); 19172 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 19173 CHECK_PARSE_BOOL(BreakStringLiterals); 19174 CHECK_PARSE_BOOL(CompactNamespaces); 19175 CHECK_PARSE_BOOL(DeriveLineEnding); 19176 CHECK_PARSE_BOOL(DerivePointerAlignment); 19177 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 19178 CHECK_PARSE_BOOL(DisableFormat); 19179 CHECK_PARSE_BOOL(IndentAccessModifiers); 19180 CHECK_PARSE_BOOL(IndentCaseLabels); 19181 CHECK_PARSE_BOOL(IndentCaseBlocks); 19182 CHECK_PARSE_BOOL(IndentGotoLabels); 19183 CHECK_PARSE_BOOL(IndentRequires); 19184 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 19185 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 19186 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 19187 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 19188 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 19189 CHECK_PARSE_BOOL(ReflowComments); 19190 CHECK_PARSE_BOOL(RemoveBracesLLVM); 19191 CHECK_PARSE_BOOL(SortUsingDeclarations); 19192 CHECK_PARSE_BOOL(SpacesInParentheses); 19193 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 19194 CHECK_PARSE_BOOL(SpacesInConditionalStatement); 19195 CHECK_PARSE_BOOL(SpaceInEmptyBlock); 19196 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 19197 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 19198 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 19199 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 19200 CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); 19201 CHECK_PARSE_BOOL(SpaceAfterLogicalNot); 19202 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 19203 CHECK_PARSE_BOOL(SpaceBeforeCaseColon); 19204 CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList); 19205 CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon); 19206 CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon); 19207 CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon); 19208 CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets); 19209 CHECK_PARSE_BOOL(UseCRLF); 19210 19211 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel); 19212 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 19213 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 19214 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 19215 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 19216 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 19217 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 19218 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 19219 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock); 19220 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 19221 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 19222 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeLambdaBody); 19223 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeWhile); 19224 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 19225 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction); 19226 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord); 19227 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace); 19228 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterControlStatements); 19229 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterForeachMacros); 19230 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, 19231 AfterFunctionDeclarationName); 19232 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, 19233 AfterFunctionDefinitionName); 19234 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterIfMacros); 19235 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterOverloadedOperator); 19236 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, BeforeNonEmptyParentheses); 19237 } 19238 19239 #undef CHECK_PARSE_BOOL 19240 19241 TEST_F(FormatTest, ParsesConfiguration) { 19242 FormatStyle Style = {}; 19243 Style.Language = FormatStyle::LK_Cpp; 19244 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 19245 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 19246 ConstructorInitializerIndentWidth, 1234u); 19247 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 19248 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 19249 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 19250 CHECK_PARSE("PenaltyBreakAssignment: 1234", PenaltyBreakAssignment, 1234u); 19251 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 19252 PenaltyBreakBeforeFirstCallParameter, 1234u); 19253 CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234", 19254 PenaltyBreakTemplateDeclaration, 1234u); 19255 CHECK_PARSE("PenaltyBreakOpenParenthesis: 1234", PenaltyBreakOpenParenthesis, 19256 1234u); 19257 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 19258 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 19259 PenaltyReturnTypeOnItsOwnLine, 1234u); 19260 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 19261 SpacesBeforeTrailingComments, 1234u); 19262 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 19263 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 19264 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$"); 19265 19266 Style.QualifierAlignment = FormatStyle::QAS_Right; 19267 CHECK_PARSE("QualifierAlignment: Leave", QualifierAlignment, 19268 FormatStyle::QAS_Leave); 19269 CHECK_PARSE("QualifierAlignment: Right", QualifierAlignment, 19270 FormatStyle::QAS_Right); 19271 CHECK_PARSE("QualifierAlignment: Left", QualifierAlignment, 19272 FormatStyle::QAS_Left); 19273 CHECK_PARSE("QualifierAlignment: Custom", QualifierAlignment, 19274 FormatStyle::QAS_Custom); 19275 19276 Style.QualifierOrder.clear(); 19277 CHECK_PARSE("QualifierOrder: [ const, volatile, type ]", QualifierOrder, 19278 std::vector<std::string>({"const", "volatile", "type"})); 19279 Style.QualifierOrder.clear(); 19280 CHECK_PARSE("QualifierOrder: [const, type]", QualifierOrder, 19281 std::vector<std::string>({"const", "type"})); 19282 Style.QualifierOrder.clear(); 19283 CHECK_PARSE("QualifierOrder: [volatile, type]", QualifierOrder, 19284 std::vector<std::string>({"volatile", "type"})); 19285 19286 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 19287 CHECK_PARSE("AlignConsecutiveAssignments: None", AlignConsecutiveAssignments, 19288 FormatStyle::ACS_None); 19289 CHECK_PARSE("AlignConsecutiveAssignments: Consecutive", 19290 AlignConsecutiveAssignments, FormatStyle::ACS_Consecutive); 19291 CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLines", 19292 AlignConsecutiveAssignments, FormatStyle::ACS_AcrossEmptyLines); 19293 CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLinesAndComments", 19294 AlignConsecutiveAssignments, 19295 FormatStyle::ACS_AcrossEmptyLinesAndComments); 19296 // For backwards compability, false / true should still parse 19297 CHECK_PARSE("AlignConsecutiveAssignments: false", AlignConsecutiveAssignments, 19298 FormatStyle::ACS_None); 19299 CHECK_PARSE("AlignConsecutiveAssignments: true", AlignConsecutiveAssignments, 19300 FormatStyle::ACS_Consecutive); 19301 19302 Style.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive; 19303 CHECK_PARSE("AlignConsecutiveBitFields: None", AlignConsecutiveBitFields, 19304 FormatStyle::ACS_None); 19305 CHECK_PARSE("AlignConsecutiveBitFields: Consecutive", 19306 AlignConsecutiveBitFields, FormatStyle::ACS_Consecutive); 19307 CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLines", 19308 AlignConsecutiveBitFields, FormatStyle::ACS_AcrossEmptyLines); 19309 CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLinesAndComments", 19310 AlignConsecutiveBitFields, 19311 FormatStyle::ACS_AcrossEmptyLinesAndComments); 19312 // For backwards compability, false / true should still parse 19313 CHECK_PARSE("AlignConsecutiveBitFields: false", AlignConsecutiveBitFields, 19314 FormatStyle::ACS_None); 19315 CHECK_PARSE("AlignConsecutiveBitFields: true", AlignConsecutiveBitFields, 19316 FormatStyle::ACS_Consecutive); 19317 19318 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 19319 CHECK_PARSE("AlignConsecutiveMacros: None", AlignConsecutiveMacros, 19320 FormatStyle::ACS_None); 19321 CHECK_PARSE("AlignConsecutiveMacros: Consecutive", AlignConsecutiveMacros, 19322 FormatStyle::ACS_Consecutive); 19323 CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLines", 19324 AlignConsecutiveMacros, FormatStyle::ACS_AcrossEmptyLines); 19325 CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLinesAndComments", 19326 AlignConsecutiveMacros, 19327 FormatStyle::ACS_AcrossEmptyLinesAndComments); 19328 // For backwards compability, false / true should still parse 19329 CHECK_PARSE("AlignConsecutiveMacros: false", AlignConsecutiveMacros, 19330 FormatStyle::ACS_None); 19331 CHECK_PARSE("AlignConsecutiveMacros: true", AlignConsecutiveMacros, 19332 FormatStyle::ACS_Consecutive); 19333 19334 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 19335 CHECK_PARSE("AlignConsecutiveDeclarations: None", 19336 AlignConsecutiveDeclarations, FormatStyle::ACS_None); 19337 CHECK_PARSE("AlignConsecutiveDeclarations: Consecutive", 19338 AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive); 19339 CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLines", 19340 AlignConsecutiveDeclarations, FormatStyle::ACS_AcrossEmptyLines); 19341 CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLinesAndComments", 19342 AlignConsecutiveDeclarations, 19343 FormatStyle::ACS_AcrossEmptyLinesAndComments); 19344 // For backwards compability, false / true should still parse 19345 CHECK_PARSE("AlignConsecutiveDeclarations: false", 19346 AlignConsecutiveDeclarations, FormatStyle::ACS_None); 19347 CHECK_PARSE("AlignConsecutiveDeclarations: true", 19348 AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive); 19349 19350 Style.PointerAlignment = FormatStyle::PAS_Middle; 19351 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 19352 FormatStyle::PAS_Left); 19353 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 19354 FormatStyle::PAS_Right); 19355 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 19356 FormatStyle::PAS_Middle); 19357 Style.ReferenceAlignment = FormatStyle::RAS_Middle; 19358 CHECK_PARSE("ReferenceAlignment: Pointer", ReferenceAlignment, 19359 FormatStyle::RAS_Pointer); 19360 CHECK_PARSE("ReferenceAlignment: Left", ReferenceAlignment, 19361 FormatStyle::RAS_Left); 19362 CHECK_PARSE("ReferenceAlignment: Right", ReferenceAlignment, 19363 FormatStyle::RAS_Right); 19364 CHECK_PARSE("ReferenceAlignment: Middle", ReferenceAlignment, 19365 FormatStyle::RAS_Middle); 19366 // For backward compatibility: 19367 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 19368 FormatStyle::PAS_Left); 19369 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 19370 FormatStyle::PAS_Right); 19371 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 19372 FormatStyle::PAS_Middle); 19373 19374 Style.Standard = FormatStyle::LS_Auto; 19375 CHECK_PARSE("Standard: c++03", Standard, FormatStyle::LS_Cpp03); 19376 CHECK_PARSE("Standard: c++11", Standard, FormatStyle::LS_Cpp11); 19377 CHECK_PARSE("Standard: c++14", Standard, FormatStyle::LS_Cpp14); 19378 CHECK_PARSE("Standard: c++17", Standard, FormatStyle::LS_Cpp17); 19379 CHECK_PARSE("Standard: c++20", Standard, FormatStyle::LS_Cpp20); 19380 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 19381 CHECK_PARSE("Standard: Latest", Standard, FormatStyle::LS_Latest); 19382 // Legacy aliases: 19383 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 19384 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Latest); 19385 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 19386 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 19387 19388 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 19389 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 19390 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 19391 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 19392 FormatStyle::BOS_None); 19393 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 19394 FormatStyle::BOS_All); 19395 // For backward compatibility: 19396 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 19397 FormatStyle::BOS_None); 19398 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 19399 FormatStyle::BOS_All); 19400 19401 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 19402 CHECK_PARSE("BreakConstructorInitializers: BeforeComma", 19403 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 19404 CHECK_PARSE("BreakConstructorInitializers: AfterColon", 19405 BreakConstructorInitializers, FormatStyle::BCIS_AfterColon); 19406 CHECK_PARSE("BreakConstructorInitializers: BeforeColon", 19407 BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon); 19408 // For backward compatibility: 19409 CHECK_PARSE("BreakConstructorInitializersBeforeComma: true", 19410 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 19411 19412 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 19413 CHECK_PARSE("BreakInheritanceList: AfterComma", BreakInheritanceList, 19414 FormatStyle::BILS_AfterComma); 19415 CHECK_PARSE("BreakInheritanceList: BeforeComma", BreakInheritanceList, 19416 FormatStyle::BILS_BeforeComma); 19417 CHECK_PARSE("BreakInheritanceList: AfterColon", BreakInheritanceList, 19418 FormatStyle::BILS_AfterColon); 19419 CHECK_PARSE("BreakInheritanceList: BeforeColon", BreakInheritanceList, 19420 FormatStyle::BILS_BeforeColon); 19421 // For backward compatibility: 19422 CHECK_PARSE("BreakBeforeInheritanceComma: true", BreakInheritanceList, 19423 FormatStyle::BILS_BeforeComma); 19424 19425 Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack; 19426 CHECK_PARSE("PackConstructorInitializers: Never", PackConstructorInitializers, 19427 FormatStyle::PCIS_Never); 19428 CHECK_PARSE("PackConstructorInitializers: BinPack", 19429 PackConstructorInitializers, FormatStyle::PCIS_BinPack); 19430 CHECK_PARSE("PackConstructorInitializers: CurrentLine", 19431 PackConstructorInitializers, FormatStyle::PCIS_CurrentLine); 19432 CHECK_PARSE("PackConstructorInitializers: NextLine", 19433 PackConstructorInitializers, FormatStyle::PCIS_NextLine); 19434 // For backward compatibility: 19435 CHECK_PARSE("BasedOnStyle: Google\n" 19436 "ConstructorInitializerAllOnOneLineOrOnePerLine: true\n" 19437 "AllowAllConstructorInitializersOnNextLine: false", 19438 PackConstructorInitializers, FormatStyle::PCIS_CurrentLine); 19439 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 19440 CHECK_PARSE("BasedOnStyle: Google\n" 19441 "ConstructorInitializerAllOnOneLineOrOnePerLine: false", 19442 PackConstructorInitializers, FormatStyle::PCIS_BinPack); 19443 CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n" 19444 "AllowAllConstructorInitializersOnNextLine: true", 19445 PackConstructorInitializers, FormatStyle::PCIS_NextLine); 19446 Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack; 19447 CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n" 19448 "AllowAllConstructorInitializersOnNextLine: false", 19449 PackConstructorInitializers, FormatStyle::PCIS_CurrentLine); 19450 19451 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 19452 CHECK_PARSE("EmptyLineBeforeAccessModifier: Never", 19453 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Never); 19454 CHECK_PARSE("EmptyLineBeforeAccessModifier: Leave", 19455 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Leave); 19456 CHECK_PARSE("EmptyLineBeforeAccessModifier: LogicalBlock", 19457 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_LogicalBlock); 19458 CHECK_PARSE("EmptyLineBeforeAccessModifier: Always", 19459 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Always); 19460 19461 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 19462 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 19463 FormatStyle::BAS_Align); 19464 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 19465 FormatStyle::BAS_DontAlign); 19466 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 19467 FormatStyle::BAS_AlwaysBreak); 19468 CHECK_PARSE("AlignAfterOpenBracket: BlockIndent", AlignAfterOpenBracket, 19469 FormatStyle::BAS_BlockIndent); 19470 // For backward compatibility: 19471 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 19472 FormatStyle::BAS_DontAlign); 19473 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 19474 FormatStyle::BAS_Align); 19475 19476 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left; 19477 CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines, 19478 FormatStyle::ENAS_DontAlign); 19479 CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines, 19480 FormatStyle::ENAS_Left); 19481 CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines, 19482 FormatStyle::ENAS_Right); 19483 // For backward compatibility: 19484 CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines, 19485 FormatStyle::ENAS_Left); 19486 CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines, 19487 FormatStyle::ENAS_Right); 19488 19489 Style.AlignOperands = FormatStyle::OAS_Align; 19490 CHECK_PARSE("AlignOperands: DontAlign", AlignOperands, 19491 FormatStyle::OAS_DontAlign); 19492 CHECK_PARSE("AlignOperands: Align", AlignOperands, FormatStyle::OAS_Align); 19493 CHECK_PARSE("AlignOperands: AlignAfterOperator", AlignOperands, 19494 FormatStyle::OAS_AlignAfterOperator); 19495 // For backward compatibility: 19496 CHECK_PARSE("AlignOperands: false", AlignOperands, 19497 FormatStyle::OAS_DontAlign); 19498 CHECK_PARSE("AlignOperands: true", AlignOperands, FormatStyle::OAS_Align); 19499 19500 Style.UseTab = FormatStyle::UT_ForIndentation; 19501 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 19502 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 19503 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 19504 CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab, 19505 FormatStyle::UT_ForContinuationAndIndentation); 19506 CHECK_PARSE("UseTab: AlignWithSpaces", UseTab, 19507 FormatStyle::UT_AlignWithSpaces); 19508 // For backward compatibility: 19509 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 19510 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 19511 19512 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 19513 CHECK_PARSE("AllowShortBlocksOnASingleLine: Never", 19514 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 19515 CHECK_PARSE("AllowShortBlocksOnASingleLine: Empty", 19516 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Empty); 19517 CHECK_PARSE("AllowShortBlocksOnASingleLine: Always", 19518 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always); 19519 // For backward compatibility: 19520 CHECK_PARSE("AllowShortBlocksOnASingleLine: false", 19521 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 19522 CHECK_PARSE("AllowShortBlocksOnASingleLine: true", 19523 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always); 19524 19525 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 19526 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 19527 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 19528 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 19529 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 19530 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 19531 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 19532 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 19533 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 19534 // For backward compatibility: 19535 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 19536 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 19537 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 19538 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 19539 19540 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Both; 19541 CHECK_PARSE("SpaceAroundPointerQualifiers: Default", 19542 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Default); 19543 CHECK_PARSE("SpaceAroundPointerQualifiers: Before", 19544 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Before); 19545 CHECK_PARSE("SpaceAroundPointerQualifiers: After", 19546 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_After); 19547 CHECK_PARSE("SpaceAroundPointerQualifiers: Both", 19548 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Both); 19549 19550 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 19551 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 19552 FormatStyle::SBPO_Never); 19553 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 19554 FormatStyle::SBPO_Always); 19555 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 19556 FormatStyle::SBPO_ControlStatements); 19557 CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptControlMacros", 19558 SpaceBeforeParens, 19559 FormatStyle::SBPO_ControlStatementsExceptControlMacros); 19560 CHECK_PARSE("SpaceBeforeParens: NonEmptyParentheses", SpaceBeforeParens, 19561 FormatStyle::SBPO_NonEmptyParentheses); 19562 CHECK_PARSE("SpaceBeforeParens: Custom", SpaceBeforeParens, 19563 FormatStyle::SBPO_Custom); 19564 // For backward compatibility: 19565 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 19566 FormatStyle::SBPO_Never); 19567 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 19568 FormatStyle::SBPO_ControlStatements); 19569 CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptForEachMacros", 19570 SpaceBeforeParens, 19571 FormatStyle::SBPO_ControlStatementsExceptControlMacros); 19572 19573 Style.ColumnLimit = 123; 19574 FormatStyle BaseStyle = getLLVMStyle(); 19575 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 19576 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 19577 19578 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 19579 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 19580 FormatStyle::BS_Attach); 19581 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 19582 FormatStyle::BS_Linux); 19583 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 19584 FormatStyle::BS_Mozilla); 19585 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 19586 FormatStyle::BS_Stroustrup); 19587 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 19588 FormatStyle::BS_Allman); 19589 CHECK_PARSE("BreakBeforeBraces: Whitesmiths", BreakBeforeBraces, 19590 FormatStyle::BS_Whitesmiths); 19591 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 19592 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 19593 FormatStyle::BS_WebKit); 19594 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 19595 FormatStyle::BS_Custom); 19596 19597 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never; 19598 CHECK_PARSE("BraceWrapping:\n" 19599 " AfterControlStatement: MultiLine", 19600 BraceWrapping.AfterControlStatement, 19601 FormatStyle::BWACS_MultiLine); 19602 CHECK_PARSE("BraceWrapping:\n" 19603 " AfterControlStatement: Always", 19604 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always); 19605 CHECK_PARSE("BraceWrapping:\n" 19606 " AfterControlStatement: Never", 19607 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never); 19608 // For backward compatibility: 19609 CHECK_PARSE("BraceWrapping:\n" 19610 " AfterControlStatement: true", 19611 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always); 19612 CHECK_PARSE("BraceWrapping:\n" 19613 " AfterControlStatement: false", 19614 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never); 19615 19616 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 19617 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 19618 FormatStyle::RTBS_None); 19619 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 19620 FormatStyle::RTBS_All); 19621 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 19622 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 19623 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 19624 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 19625 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 19626 AlwaysBreakAfterReturnType, 19627 FormatStyle::RTBS_TopLevelDefinitions); 19628 19629 Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 19630 CHECK_PARSE("AlwaysBreakTemplateDeclarations: No", 19631 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_No); 19632 CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine", 19633 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine); 19634 CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes", 19635 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes); 19636 CHECK_PARSE("AlwaysBreakTemplateDeclarations: false", 19637 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine); 19638 CHECK_PARSE("AlwaysBreakTemplateDeclarations: true", 19639 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes); 19640 19641 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 19642 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 19643 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 19644 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 19645 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 19646 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 19647 AlwaysBreakAfterDefinitionReturnType, 19648 FormatStyle::DRTBS_TopLevel); 19649 19650 Style.NamespaceIndentation = FormatStyle::NI_All; 19651 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 19652 FormatStyle::NI_None); 19653 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 19654 FormatStyle::NI_Inner); 19655 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 19656 FormatStyle::NI_All); 19657 19658 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_OnlyFirstIf; 19659 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Never", 19660 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never); 19661 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: WithoutElse", 19662 AllowShortIfStatementsOnASingleLine, 19663 FormatStyle::SIS_WithoutElse); 19664 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: OnlyFirstIf", 19665 AllowShortIfStatementsOnASingleLine, 19666 FormatStyle::SIS_OnlyFirstIf); 19667 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: AllIfsAndElse", 19668 AllowShortIfStatementsOnASingleLine, 19669 FormatStyle::SIS_AllIfsAndElse); 19670 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Always", 19671 AllowShortIfStatementsOnASingleLine, 19672 FormatStyle::SIS_OnlyFirstIf); 19673 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: false", 19674 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never); 19675 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: true", 19676 AllowShortIfStatementsOnASingleLine, 19677 FormatStyle::SIS_WithoutElse); 19678 19679 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; 19680 CHECK_PARSE("IndentExternBlock: AfterExternBlock", IndentExternBlock, 19681 FormatStyle::IEBS_AfterExternBlock); 19682 CHECK_PARSE("IndentExternBlock: Indent", IndentExternBlock, 19683 FormatStyle::IEBS_Indent); 19684 CHECK_PARSE("IndentExternBlock: NoIndent", IndentExternBlock, 19685 FormatStyle::IEBS_NoIndent); 19686 CHECK_PARSE("IndentExternBlock: true", IndentExternBlock, 19687 FormatStyle::IEBS_Indent); 19688 CHECK_PARSE("IndentExternBlock: false", IndentExternBlock, 19689 FormatStyle::IEBS_NoIndent); 19690 19691 Style.BitFieldColonSpacing = FormatStyle::BFCS_None; 19692 CHECK_PARSE("BitFieldColonSpacing: Both", BitFieldColonSpacing, 19693 FormatStyle::BFCS_Both); 19694 CHECK_PARSE("BitFieldColonSpacing: None", BitFieldColonSpacing, 19695 FormatStyle::BFCS_None); 19696 CHECK_PARSE("BitFieldColonSpacing: Before", BitFieldColonSpacing, 19697 FormatStyle::BFCS_Before); 19698 CHECK_PARSE("BitFieldColonSpacing: After", BitFieldColonSpacing, 19699 FormatStyle::BFCS_After); 19700 19701 Style.SortJavaStaticImport = FormatStyle::SJSIO_Before; 19702 CHECK_PARSE("SortJavaStaticImport: After", SortJavaStaticImport, 19703 FormatStyle::SJSIO_After); 19704 CHECK_PARSE("SortJavaStaticImport: Before", SortJavaStaticImport, 19705 FormatStyle::SJSIO_Before); 19706 19707 // FIXME: This is required because parsing a configuration simply overwrites 19708 // the first N elements of the list instead of resetting it. 19709 Style.ForEachMacros.clear(); 19710 std::vector<std::string> BoostForeach; 19711 BoostForeach.push_back("BOOST_FOREACH"); 19712 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 19713 std::vector<std::string> BoostAndQForeach; 19714 BoostAndQForeach.push_back("BOOST_FOREACH"); 19715 BoostAndQForeach.push_back("Q_FOREACH"); 19716 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 19717 BoostAndQForeach); 19718 19719 Style.IfMacros.clear(); 19720 std::vector<std::string> CustomIfs; 19721 CustomIfs.push_back("MYIF"); 19722 CHECK_PARSE("IfMacros: [MYIF]", IfMacros, CustomIfs); 19723 19724 Style.AttributeMacros.clear(); 19725 CHECK_PARSE("BasedOnStyle: LLVM", AttributeMacros, 19726 std::vector<std::string>{"__capability"}); 19727 CHECK_PARSE("AttributeMacros: [attr1, attr2]", AttributeMacros, 19728 std::vector<std::string>({"attr1", "attr2"})); 19729 19730 Style.StatementAttributeLikeMacros.clear(); 19731 CHECK_PARSE("StatementAttributeLikeMacros: [emit,Q_EMIT]", 19732 StatementAttributeLikeMacros, 19733 std::vector<std::string>({"emit", "Q_EMIT"})); 19734 19735 Style.StatementMacros.clear(); 19736 CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros, 19737 std::vector<std::string>{"QUNUSED"}); 19738 CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros, 19739 std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"})); 19740 19741 Style.NamespaceMacros.clear(); 19742 CHECK_PARSE("NamespaceMacros: [TESTSUITE]", NamespaceMacros, 19743 std::vector<std::string>{"TESTSUITE"}); 19744 CHECK_PARSE("NamespaceMacros: [TESTSUITE, SUITE]", NamespaceMacros, 19745 std::vector<std::string>({"TESTSUITE", "SUITE"})); 19746 19747 Style.WhitespaceSensitiveMacros.clear(); 19748 CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE]", 19749 WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"}); 19750 CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE, ASSERT]", 19751 WhitespaceSensitiveMacros, 19752 std::vector<std::string>({"STRINGIZE", "ASSERT"})); 19753 Style.WhitespaceSensitiveMacros.clear(); 19754 CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE']", 19755 WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"}); 19756 CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE', 'ASSERT']", 19757 WhitespaceSensitiveMacros, 19758 std::vector<std::string>({"STRINGIZE", "ASSERT"})); 19759 19760 Style.IncludeStyle.IncludeCategories.clear(); 19761 std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = { 19762 {"abc/.*", 2, 0, false}, {".*", 1, 0, true}}; 19763 CHECK_PARSE("IncludeCategories:\n" 19764 " - Regex: abc/.*\n" 19765 " Priority: 2\n" 19766 " - Regex: .*\n" 19767 " Priority: 1\n" 19768 " CaseSensitive: true\n", 19769 IncludeStyle.IncludeCategories, ExpectedCategories); 19770 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex, 19771 "abc$"); 19772 CHECK_PARSE("IncludeIsMainSourceRegex: 'abc$'", 19773 IncludeStyle.IncludeIsMainSourceRegex, "abc$"); 19774 19775 Style.SortIncludes = FormatStyle::SI_Never; 19776 CHECK_PARSE("SortIncludes: true", SortIncludes, 19777 FormatStyle::SI_CaseSensitive); 19778 CHECK_PARSE("SortIncludes: false", SortIncludes, FormatStyle::SI_Never); 19779 CHECK_PARSE("SortIncludes: CaseInsensitive", SortIncludes, 19780 FormatStyle::SI_CaseInsensitive); 19781 CHECK_PARSE("SortIncludes: CaseSensitive", SortIncludes, 19782 FormatStyle::SI_CaseSensitive); 19783 CHECK_PARSE("SortIncludes: Never", SortIncludes, FormatStyle::SI_Never); 19784 19785 Style.RawStringFormats.clear(); 19786 std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = { 19787 { 19788 FormatStyle::LK_TextProto, 19789 {"pb", "proto"}, 19790 {"PARSE_TEXT_PROTO"}, 19791 /*CanonicalDelimiter=*/"", 19792 "llvm", 19793 }, 19794 { 19795 FormatStyle::LK_Cpp, 19796 {"cc", "cpp"}, 19797 {"C_CODEBLOCK", "CPPEVAL"}, 19798 /*CanonicalDelimiter=*/"cc", 19799 /*BasedOnStyle=*/"", 19800 }, 19801 }; 19802 19803 CHECK_PARSE("RawStringFormats:\n" 19804 " - Language: TextProto\n" 19805 " Delimiters:\n" 19806 " - 'pb'\n" 19807 " - 'proto'\n" 19808 " EnclosingFunctions:\n" 19809 " - 'PARSE_TEXT_PROTO'\n" 19810 " BasedOnStyle: llvm\n" 19811 " - Language: Cpp\n" 19812 " Delimiters:\n" 19813 " - 'cc'\n" 19814 " - 'cpp'\n" 19815 " EnclosingFunctions:\n" 19816 " - 'C_CODEBLOCK'\n" 19817 " - 'CPPEVAL'\n" 19818 " CanonicalDelimiter: 'cc'", 19819 RawStringFormats, ExpectedRawStringFormats); 19820 19821 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 19822 " Minimum: 0\n" 19823 " Maximum: 0", 19824 SpacesInLineCommentPrefix.Minimum, 0u); 19825 EXPECT_EQ(Style.SpacesInLineCommentPrefix.Maximum, 0u); 19826 Style.SpacesInLineCommentPrefix.Minimum = 1; 19827 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 19828 " Minimum: 2", 19829 SpacesInLineCommentPrefix.Minimum, 0u); 19830 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 19831 " Maximum: -1", 19832 SpacesInLineCommentPrefix.Maximum, -1u); 19833 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 19834 " Minimum: 2", 19835 SpacesInLineCommentPrefix.Minimum, 2u); 19836 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 19837 " Maximum: 1", 19838 SpacesInLineCommentPrefix.Maximum, 1u); 19839 EXPECT_EQ(Style.SpacesInLineCommentPrefix.Minimum, 1u); 19840 19841 Style.SpacesInAngles = FormatStyle::SIAS_Always; 19842 CHECK_PARSE("SpacesInAngles: Never", SpacesInAngles, FormatStyle::SIAS_Never); 19843 CHECK_PARSE("SpacesInAngles: Always", SpacesInAngles, 19844 FormatStyle::SIAS_Always); 19845 CHECK_PARSE("SpacesInAngles: Leave", SpacesInAngles, FormatStyle::SIAS_Leave); 19846 // For backward compatibility: 19847 CHECK_PARSE("SpacesInAngles: false", SpacesInAngles, FormatStyle::SIAS_Never); 19848 CHECK_PARSE("SpacesInAngles: true", SpacesInAngles, FormatStyle::SIAS_Always); 19849 } 19850 19851 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 19852 FormatStyle Style = {}; 19853 Style.Language = FormatStyle::LK_Cpp; 19854 CHECK_PARSE("Language: Cpp\n" 19855 "IndentWidth: 12", 19856 IndentWidth, 12u); 19857 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 19858 "IndentWidth: 34", 19859 &Style), 19860 ParseError::Unsuitable); 19861 FormatStyle BinPackedTCS = {}; 19862 BinPackedTCS.Language = FormatStyle::LK_JavaScript; 19863 EXPECT_EQ(parseConfiguration("BinPackArguments: true\n" 19864 "InsertTrailingCommas: Wrapped", 19865 &BinPackedTCS), 19866 ParseError::BinPackTrailingCommaConflict); 19867 EXPECT_EQ(12u, Style.IndentWidth); 19868 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 19869 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 19870 19871 Style.Language = FormatStyle::LK_JavaScript; 19872 CHECK_PARSE("Language: JavaScript\n" 19873 "IndentWidth: 12", 19874 IndentWidth, 12u); 19875 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 19876 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 19877 "IndentWidth: 34", 19878 &Style), 19879 ParseError::Unsuitable); 19880 EXPECT_EQ(23u, Style.IndentWidth); 19881 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 19882 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 19883 19884 CHECK_PARSE("BasedOnStyle: LLVM\n" 19885 "IndentWidth: 67", 19886 IndentWidth, 67u); 19887 19888 CHECK_PARSE("---\n" 19889 "Language: JavaScript\n" 19890 "IndentWidth: 12\n" 19891 "---\n" 19892 "Language: Cpp\n" 19893 "IndentWidth: 34\n" 19894 "...\n", 19895 IndentWidth, 12u); 19896 19897 Style.Language = FormatStyle::LK_Cpp; 19898 CHECK_PARSE("---\n" 19899 "Language: JavaScript\n" 19900 "IndentWidth: 12\n" 19901 "---\n" 19902 "Language: Cpp\n" 19903 "IndentWidth: 34\n" 19904 "...\n", 19905 IndentWidth, 34u); 19906 CHECK_PARSE("---\n" 19907 "IndentWidth: 78\n" 19908 "---\n" 19909 "Language: JavaScript\n" 19910 "IndentWidth: 56\n" 19911 "...\n", 19912 IndentWidth, 78u); 19913 19914 Style.ColumnLimit = 123; 19915 Style.IndentWidth = 234; 19916 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 19917 Style.TabWidth = 345; 19918 EXPECT_FALSE(parseConfiguration("---\n" 19919 "IndentWidth: 456\n" 19920 "BreakBeforeBraces: Allman\n" 19921 "---\n" 19922 "Language: JavaScript\n" 19923 "IndentWidth: 111\n" 19924 "TabWidth: 111\n" 19925 "---\n" 19926 "Language: Cpp\n" 19927 "BreakBeforeBraces: Stroustrup\n" 19928 "TabWidth: 789\n" 19929 "...\n", 19930 &Style)); 19931 EXPECT_EQ(123u, Style.ColumnLimit); 19932 EXPECT_EQ(456u, Style.IndentWidth); 19933 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 19934 EXPECT_EQ(789u, Style.TabWidth); 19935 19936 EXPECT_EQ(parseConfiguration("---\n" 19937 "Language: JavaScript\n" 19938 "IndentWidth: 56\n" 19939 "---\n" 19940 "IndentWidth: 78\n" 19941 "...\n", 19942 &Style), 19943 ParseError::Error); 19944 EXPECT_EQ(parseConfiguration("---\n" 19945 "Language: JavaScript\n" 19946 "IndentWidth: 56\n" 19947 "---\n" 19948 "Language: JavaScript\n" 19949 "IndentWidth: 78\n" 19950 "...\n", 19951 &Style), 19952 ParseError::Error); 19953 19954 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 19955 } 19956 19957 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 19958 FormatStyle Style = {}; 19959 Style.Language = FormatStyle::LK_JavaScript; 19960 Style.BreakBeforeTernaryOperators = true; 19961 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 19962 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 19963 19964 Style.BreakBeforeTernaryOperators = true; 19965 EXPECT_EQ(0, parseConfiguration("---\n" 19966 "BasedOnStyle: Google\n" 19967 "---\n" 19968 "Language: JavaScript\n" 19969 "IndentWidth: 76\n" 19970 "...\n", 19971 &Style) 19972 .value()); 19973 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 19974 EXPECT_EQ(76u, Style.IndentWidth); 19975 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 19976 } 19977 19978 TEST_F(FormatTest, ConfigurationRoundTripTest) { 19979 FormatStyle Style = getLLVMStyle(); 19980 std::string YAML = configurationAsText(Style); 19981 FormatStyle ParsedStyle = {}; 19982 ParsedStyle.Language = FormatStyle::LK_Cpp; 19983 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 19984 EXPECT_EQ(Style, ParsedStyle); 19985 } 19986 19987 TEST_F(FormatTest, WorksFor8bitEncodings) { 19988 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 19989 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 19990 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 19991 "\"\xef\xee\xf0\xf3...\"", 19992 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 19993 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 19994 "\xef\xee\xf0\xf3...\"", 19995 getLLVMStyleWithColumns(12))); 19996 } 19997 19998 TEST_F(FormatTest, HandlesUTF8BOM) { 19999 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 20000 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 20001 format("\xef\xbb\xbf#include <iostream>")); 20002 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 20003 format("\xef\xbb\xbf\n#include <iostream>")); 20004 } 20005 20006 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 20007 #if !defined(_MSC_VER) 20008 20009 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 20010 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 20011 getLLVMStyleWithColumns(35)); 20012 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 20013 getLLVMStyleWithColumns(31)); 20014 verifyFormat("// Однажды в студёную зимнюю пору...", 20015 getLLVMStyleWithColumns(36)); 20016 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 20017 verifyFormat("/* Однажды в студёную зимнюю пору... */", 20018 getLLVMStyleWithColumns(39)); 20019 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 20020 getLLVMStyleWithColumns(35)); 20021 } 20022 20023 TEST_F(FormatTest, SplitsUTF8Strings) { 20024 // Non-printable characters' width is currently considered to be the length in 20025 // bytes in UTF8. The characters can be displayed in very different manner 20026 // (zero-width, single width with a substitution glyph, expanded to their code 20027 // (e.g. "<8d>"), so there's no single correct way to handle them. 20028 EXPECT_EQ("\"aaaaÄ\"\n" 20029 "\"\xc2\x8d\";", 20030 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 20031 EXPECT_EQ("\"aaaaaaaÄ\"\n" 20032 "\"\xc2\x8d\";", 20033 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 20034 EXPECT_EQ("\"Однажды, в \"\n" 20035 "\"студёную \"\n" 20036 "\"зимнюю \"\n" 20037 "\"пору,\"", 20038 format("\"Однажды, в студёную зимнюю пору,\"", 20039 getLLVMStyleWithColumns(13))); 20040 EXPECT_EQ( 20041 "\"一 二 三 \"\n" 20042 "\"四 五六 \"\n" 20043 "\"七 八 九 \"\n" 20044 "\"十\"", 20045 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 20046 EXPECT_EQ("\"一\t\"\n" 20047 "\"二 \t\"\n" 20048 "\"三 四 \"\n" 20049 "\"五\t\"\n" 20050 "\"六 \t\"\n" 20051 "\"七 \"\n" 20052 "\"八九十\tqq\"", 20053 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 20054 getLLVMStyleWithColumns(11))); 20055 20056 // UTF8 character in an escape sequence. 20057 EXPECT_EQ("\"aaaaaa\"\n" 20058 "\"\\\xC2\x8D\"", 20059 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 20060 } 20061 20062 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 20063 EXPECT_EQ("const char *sssss =\n" 20064 " \"一二三四五六七八\\\n" 20065 " 九 十\";", 20066 format("const char *sssss = \"一二三四五六七八\\\n" 20067 " 九 十\";", 20068 getLLVMStyleWithColumns(30))); 20069 } 20070 20071 TEST_F(FormatTest, SplitsUTF8LineComments) { 20072 EXPECT_EQ("// aaaaÄ\xc2\x8d", 20073 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 20074 EXPECT_EQ("// Я из лесу\n" 20075 "// вышел; был\n" 20076 "// сильный\n" 20077 "// мороз.", 20078 format("// Я из лесу вышел; был сильный мороз.", 20079 getLLVMStyleWithColumns(13))); 20080 EXPECT_EQ("// 一二三\n" 20081 "// 四五六七\n" 20082 "// 八 九\n" 20083 "// 十", 20084 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 20085 } 20086 20087 TEST_F(FormatTest, SplitsUTF8BlockComments) { 20088 EXPECT_EQ("/* Гляжу,\n" 20089 " * поднимается\n" 20090 " * медленно в\n" 20091 " * гору\n" 20092 " * Лошадка,\n" 20093 " * везущая\n" 20094 " * хворосту\n" 20095 " * воз. */", 20096 format("/* Гляжу, поднимается медленно в гору\n" 20097 " * Лошадка, везущая хворосту воз. */", 20098 getLLVMStyleWithColumns(13))); 20099 EXPECT_EQ( 20100 "/* 一二三\n" 20101 " * 四五六七\n" 20102 " * 八 九\n" 20103 " * 十 */", 20104 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 20105 EXPECT_EQ("/* \n" 20106 " * \n" 20107 " * - */", 20108 format("/* - */", getLLVMStyleWithColumns(12))); 20109 } 20110 20111 #endif // _MSC_VER 20112 20113 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 20114 FormatStyle Style = getLLVMStyle(); 20115 20116 Style.ConstructorInitializerIndentWidth = 4; 20117 verifyFormat( 20118 "SomeClass::Constructor()\n" 20119 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 20120 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 20121 Style); 20122 20123 Style.ConstructorInitializerIndentWidth = 2; 20124 verifyFormat( 20125 "SomeClass::Constructor()\n" 20126 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 20127 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 20128 Style); 20129 20130 Style.ConstructorInitializerIndentWidth = 0; 20131 verifyFormat( 20132 "SomeClass::Constructor()\n" 20133 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 20134 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 20135 Style); 20136 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 20137 verifyFormat( 20138 "SomeLongTemplateVariableName<\n" 20139 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 20140 Style); 20141 verifyFormat("bool smaller = 1 < " 20142 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 20143 " " 20144 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 20145 Style); 20146 20147 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 20148 verifyFormat("SomeClass::Constructor() :\n" 20149 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n" 20150 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}", 20151 Style); 20152 } 20153 20154 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 20155 FormatStyle Style = getLLVMStyle(); 20156 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 20157 Style.ConstructorInitializerIndentWidth = 4; 20158 verifyFormat("SomeClass::Constructor()\n" 20159 " : a(a)\n" 20160 " , b(b)\n" 20161 " , c(c) {}", 20162 Style); 20163 verifyFormat("SomeClass::Constructor()\n" 20164 " : a(a) {}", 20165 Style); 20166 20167 Style.ColumnLimit = 0; 20168 verifyFormat("SomeClass::Constructor()\n" 20169 " : a(a) {}", 20170 Style); 20171 verifyFormat("SomeClass::Constructor() noexcept\n" 20172 " : a(a) {}", 20173 Style); 20174 verifyFormat("SomeClass::Constructor()\n" 20175 " : a(a)\n" 20176 " , b(b)\n" 20177 " , c(c) {}", 20178 Style); 20179 verifyFormat("SomeClass::Constructor()\n" 20180 " : a(a) {\n" 20181 " foo();\n" 20182 " bar();\n" 20183 "}", 20184 Style); 20185 20186 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 20187 verifyFormat("SomeClass::Constructor()\n" 20188 " : a(a)\n" 20189 " , b(b)\n" 20190 " , c(c) {\n}", 20191 Style); 20192 verifyFormat("SomeClass::Constructor()\n" 20193 " : a(a) {\n}", 20194 Style); 20195 20196 Style.ColumnLimit = 80; 20197 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 20198 Style.ConstructorInitializerIndentWidth = 2; 20199 verifyFormat("SomeClass::Constructor()\n" 20200 " : a(a)\n" 20201 " , b(b)\n" 20202 " , c(c) {}", 20203 Style); 20204 20205 Style.ConstructorInitializerIndentWidth = 0; 20206 verifyFormat("SomeClass::Constructor()\n" 20207 ": a(a)\n" 20208 ", b(b)\n" 20209 ", c(c) {}", 20210 Style); 20211 20212 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 20213 Style.ConstructorInitializerIndentWidth = 4; 20214 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 20215 verifyFormat( 20216 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 20217 Style); 20218 verifyFormat( 20219 "SomeClass::Constructor()\n" 20220 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 20221 Style); 20222 Style.ConstructorInitializerIndentWidth = 4; 20223 Style.ColumnLimit = 60; 20224 verifyFormat("SomeClass::Constructor()\n" 20225 " : aaaaaaaa(aaaaaaaa)\n" 20226 " , aaaaaaaa(aaaaaaaa)\n" 20227 " , aaaaaaaa(aaaaaaaa) {}", 20228 Style); 20229 } 20230 20231 TEST_F(FormatTest, ConstructorInitializersWithPreprocessorDirective) { 20232 FormatStyle Style = getLLVMStyle(); 20233 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 20234 Style.ConstructorInitializerIndentWidth = 4; 20235 verifyFormat("SomeClass::Constructor()\n" 20236 " : a{a}\n" 20237 " , b{b} {}", 20238 Style); 20239 verifyFormat("SomeClass::Constructor()\n" 20240 " : a{a}\n" 20241 "#if CONDITION\n" 20242 " , b{b}\n" 20243 "#endif\n" 20244 "{\n}", 20245 Style); 20246 Style.ConstructorInitializerIndentWidth = 2; 20247 verifyFormat("SomeClass::Constructor()\n" 20248 "#if CONDITION\n" 20249 " : a{a}\n" 20250 "#endif\n" 20251 " , b{b}\n" 20252 " , c{c} {\n}", 20253 Style); 20254 Style.ConstructorInitializerIndentWidth = 0; 20255 verifyFormat("SomeClass::Constructor()\n" 20256 ": a{a}\n" 20257 "#ifdef CONDITION\n" 20258 ", b{b}\n" 20259 "#else\n" 20260 ", c{c}\n" 20261 "#endif\n" 20262 ", d{d} {\n}", 20263 Style); 20264 Style.ConstructorInitializerIndentWidth = 4; 20265 verifyFormat("SomeClass::Constructor()\n" 20266 " : a{a}\n" 20267 "#if WINDOWS\n" 20268 "#if DEBUG\n" 20269 " , b{0}\n" 20270 "#else\n" 20271 " , b{1}\n" 20272 "#endif\n" 20273 "#else\n" 20274 "#if DEBUG\n" 20275 " , b{2}\n" 20276 "#else\n" 20277 " , b{3}\n" 20278 "#endif\n" 20279 "#endif\n" 20280 "{\n}", 20281 Style); 20282 verifyFormat("SomeClass::Constructor()\n" 20283 " : a{a}\n" 20284 "#if WINDOWS\n" 20285 " , b{0}\n" 20286 "#if DEBUG\n" 20287 " , c{0}\n" 20288 "#else\n" 20289 " , c{1}\n" 20290 "#endif\n" 20291 "#else\n" 20292 "#if DEBUG\n" 20293 " , c{2}\n" 20294 "#else\n" 20295 " , c{3}\n" 20296 "#endif\n" 20297 " , b{1}\n" 20298 "#endif\n" 20299 "{\n}", 20300 Style); 20301 } 20302 20303 TEST_F(FormatTest, Destructors) { 20304 verifyFormat("void F(int &i) { i.~int(); }"); 20305 verifyFormat("void F(int &i) { i->~int(); }"); 20306 } 20307 20308 TEST_F(FormatTest, FormatsWithWebKitStyle) { 20309 FormatStyle Style = getWebKitStyle(); 20310 20311 // Don't indent in outer namespaces. 20312 verifyFormat("namespace outer {\n" 20313 "int i;\n" 20314 "namespace inner {\n" 20315 " int i;\n" 20316 "} // namespace inner\n" 20317 "} // namespace outer\n" 20318 "namespace other_outer {\n" 20319 "int i;\n" 20320 "}", 20321 Style); 20322 20323 // Don't indent case labels. 20324 verifyFormat("switch (variable) {\n" 20325 "case 1:\n" 20326 "case 2:\n" 20327 " doSomething();\n" 20328 " break;\n" 20329 "default:\n" 20330 " ++variable;\n" 20331 "}", 20332 Style); 20333 20334 // Wrap before binary operators. 20335 EXPECT_EQ("void f()\n" 20336 "{\n" 20337 " if (aaaaaaaaaaaaaaaa\n" 20338 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 20339 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 20340 " return;\n" 20341 "}", 20342 format("void f() {\n" 20343 "if (aaaaaaaaaaaaaaaa\n" 20344 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 20345 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 20346 "return;\n" 20347 "}", 20348 Style)); 20349 20350 // Allow functions on a single line. 20351 verifyFormat("void f() { return; }", Style); 20352 20353 // Allow empty blocks on a single line and insert a space in empty blocks. 20354 EXPECT_EQ("void f() { }", format("void f() {}", Style)); 20355 EXPECT_EQ("while (true) { }", format("while (true) {}", Style)); 20356 // However, don't merge non-empty short loops. 20357 EXPECT_EQ("while (true) {\n" 20358 " continue;\n" 20359 "}", 20360 format("while (true) { continue; }", Style)); 20361 20362 // Constructor initializers are formatted one per line with the "," on the 20363 // new line. 20364 verifyFormat("Constructor()\n" 20365 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 20366 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 20367 " aaaaaaaaaaaaaa)\n" 20368 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 20369 "{\n" 20370 "}", 20371 Style); 20372 verifyFormat("SomeClass::Constructor()\n" 20373 " : a(a)\n" 20374 "{\n" 20375 "}", 20376 Style); 20377 EXPECT_EQ("SomeClass::Constructor()\n" 20378 " : a(a)\n" 20379 "{\n" 20380 "}", 20381 format("SomeClass::Constructor():a(a){}", Style)); 20382 verifyFormat("SomeClass::Constructor()\n" 20383 " : a(a)\n" 20384 " , b(b)\n" 20385 " , c(c)\n" 20386 "{\n" 20387 "}", 20388 Style); 20389 verifyFormat("SomeClass::Constructor()\n" 20390 " : a(a)\n" 20391 "{\n" 20392 " foo();\n" 20393 " bar();\n" 20394 "}", 20395 Style); 20396 20397 // Access specifiers should be aligned left. 20398 verifyFormat("class C {\n" 20399 "public:\n" 20400 " int i;\n" 20401 "};", 20402 Style); 20403 20404 // Do not align comments. 20405 verifyFormat("int a; // Do not\n" 20406 "double b; // align comments.", 20407 Style); 20408 20409 // Do not align operands. 20410 EXPECT_EQ("ASSERT(aaaa\n" 20411 " || bbbb);", 20412 format("ASSERT ( aaaa\n||bbbb);", Style)); 20413 20414 // Accept input's line breaks. 20415 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 20416 " || bbbbbbbbbbbbbbb) {\n" 20417 " i++;\n" 20418 "}", 20419 format("if (aaaaaaaaaaaaaaa\n" 20420 "|| bbbbbbbbbbbbbbb) { i++; }", 20421 Style)); 20422 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 20423 " i++;\n" 20424 "}", 20425 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 20426 20427 // Don't automatically break all macro definitions (llvm.org/PR17842). 20428 verifyFormat("#define aNumber 10", Style); 20429 // However, generally keep the line breaks that the user authored. 20430 EXPECT_EQ("#define aNumber \\\n" 20431 " 10", 20432 format("#define aNumber \\\n" 20433 " 10", 20434 Style)); 20435 20436 // Keep empty and one-element array literals on a single line. 20437 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 20438 " copyItems:YES];", 20439 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 20440 "copyItems:YES];", 20441 Style)); 20442 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 20443 " copyItems:YES];", 20444 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 20445 " copyItems:YES];", 20446 Style)); 20447 // FIXME: This does not seem right, there should be more indentation before 20448 // the array literal's entries. Nested blocks have the same problem. 20449 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 20450 " @\"a\",\n" 20451 " @\"a\"\n" 20452 "]\n" 20453 " copyItems:YES];", 20454 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 20455 " @\"a\",\n" 20456 " @\"a\"\n" 20457 " ]\n" 20458 " copyItems:YES];", 20459 Style)); 20460 EXPECT_EQ( 20461 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 20462 " copyItems:YES];", 20463 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 20464 " copyItems:YES];", 20465 Style)); 20466 20467 verifyFormat("[self.a b:c c:d];", Style); 20468 EXPECT_EQ("[self.a b:c\n" 20469 " c:d];", 20470 format("[self.a b:c\n" 20471 "c:d];", 20472 Style)); 20473 } 20474 20475 TEST_F(FormatTest, FormatsLambdas) { 20476 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 20477 verifyFormat( 20478 "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();\n"); 20479 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 20480 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 20481 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 20482 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 20483 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 20484 verifyFormat("auto c = [a = [b = 42] {}] {};\n"); 20485 verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n"); 20486 verifyFormat("int x = f(*+[] {});"); 20487 verifyFormat("void f() {\n" 20488 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 20489 "}\n"); 20490 verifyFormat("void f() {\n" 20491 " other(x.begin(), //\n" 20492 " x.end(), //\n" 20493 " [&](int, int) { return 1; });\n" 20494 "}\n"); 20495 verifyFormat("void f() {\n" 20496 " other.other.other.other.other(\n" 20497 " x.begin(), x.end(),\n" 20498 " [something, rather](int, int, int, int, int, int, int) { " 20499 "return 1; });\n" 20500 "}\n"); 20501 verifyFormat( 20502 "void f() {\n" 20503 " other.other.other.other.other(\n" 20504 " x.begin(), x.end(),\n" 20505 " [something, rather](int, int, int, int, int, int, int) {\n" 20506 " //\n" 20507 " });\n" 20508 "}\n"); 20509 verifyFormat("SomeFunction([]() { // A cool function...\n" 20510 " return 43;\n" 20511 "});"); 20512 EXPECT_EQ("SomeFunction([]() {\n" 20513 "#define A a\n" 20514 " return 43;\n" 20515 "});", 20516 format("SomeFunction([](){\n" 20517 "#define A a\n" 20518 "return 43;\n" 20519 "});")); 20520 verifyFormat("void f() {\n" 20521 " SomeFunction([](decltype(x), A *a) {});\n" 20522 " SomeFunction([](typeof(x), A *a) {});\n" 20523 " SomeFunction([](_Atomic(x), A *a) {});\n" 20524 " SomeFunction([](__underlying_type(x), A *a) {});\n" 20525 "}"); 20526 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 20527 " [](const aaaaaaaaaa &a) { return a; });"); 20528 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 20529 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 20530 "});"); 20531 verifyFormat("Constructor()\n" 20532 " : Field([] { // comment\n" 20533 " int i;\n" 20534 " }) {}"); 20535 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 20536 " return some_parameter.size();\n" 20537 "};"); 20538 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 20539 " [](const string &s) { return s; };"); 20540 verifyFormat("int i = aaaaaa ? 1 //\n" 20541 " : [] {\n" 20542 " return 2; //\n" 20543 " }();"); 20544 verifyFormat("llvm::errs() << \"number of twos is \"\n" 20545 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 20546 " return x == 2; // force break\n" 20547 " });"); 20548 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 20549 " [=](int iiiiiiiiiiii) {\n" 20550 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n" 20551 " aaaaaaaaaaaaaaaaaaaaaaa;\n" 20552 " });", 20553 getLLVMStyleWithColumns(60)); 20554 20555 verifyFormat("SomeFunction({[&] {\n" 20556 " // comment\n" 20557 " },\n" 20558 " [&] {\n" 20559 " // comment\n" 20560 " }});"); 20561 verifyFormat("SomeFunction({[&] {\n" 20562 " // comment\n" 20563 "}});"); 20564 verifyFormat( 20565 "virtual aaaaaaaaaaaaaaaa(\n" 20566 " std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n" 20567 " aaaaa aaaaaaaaa);"); 20568 20569 // Lambdas with return types. 20570 verifyFormat("int c = []() -> int { return 2; }();\n"); 20571 verifyFormat("int c = []() -> int * { return 2; }();\n"); 20572 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 20573 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 20574 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 20575 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 20576 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 20577 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 20578 verifyFormat("[a, a]() -> a<1> {};"); 20579 verifyFormat("[]() -> foo<5 + 2> { return {}; };"); 20580 verifyFormat("[]() -> foo<5 - 2> { return {}; };"); 20581 verifyFormat("[]() -> foo<5 / 2> { return {}; };"); 20582 verifyFormat("[]() -> foo<5 * 2> { return {}; };"); 20583 verifyFormat("[]() -> foo<5 % 2> { return {}; };"); 20584 verifyFormat("[]() -> foo<5 << 2> { return {}; };"); 20585 verifyFormat("[]() -> foo<!5> { return {}; };"); 20586 verifyFormat("[]() -> foo<~5> { return {}; };"); 20587 verifyFormat("[]() -> foo<5 | 2> { return {}; };"); 20588 verifyFormat("[]() -> foo<5 || 2> { return {}; };"); 20589 verifyFormat("[]() -> foo<5 & 2> { return {}; };"); 20590 verifyFormat("[]() -> foo<5 && 2> { return {}; };"); 20591 verifyFormat("[]() -> foo<5 == 2> { return {}; };"); 20592 verifyFormat("[]() -> foo<5 != 2> { return {}; };"); 20593 verifyFormat("[]() -> foo<5 >= 2> { return {}; };"); 20594 verifyFormat("[]() -> foo<5 <= 2> { return {}; };"); 20595 verifyFormat("[]() -> foo<5 < 2> { return {}; };"); 20596 verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };"); 20597 verifyFormat("namespace bar {\n" 20598 "// broken:\n" 20599 "auto foo{[]() -> foo<5 + 2> { return {}; }};\n" 20600 "} // namespace bar"); 20601 verifyFormat("namespace bar {\n" 20602 "// broken:\n" 20603 "auto foo{[]() -> foo<5 - 2> { return {}; }};\n" 20604 "} // namespace bar"); 20605 verifyFormat("namespace bar {\n" 20606 "// broken:\n" 20607 "auto foo{[]() -> foo<5 / 2> { return {}; }};\n" 20608 "} // namespace bar"); 20609 verifyFormat("namespace bar {\n" 20610 "// broken:\n" 20611 "auto foo{[]() -> foo<5 * 2> { return {}; }};\n" 20612 "} // namespace bar"); 20613 verifyFormat("namespace bar {\n" 20614 "// broken:\n" 20615 "auto foo{[]() -> foo<5 % 2> { return {}; }};\n" 20616 "} // namespace bar"); 20617 verifyFormat("namespace bar {\n" 20618 "// broken:\n" 20619 "auto foo{[]() -> foo<5 << 2> { return {}; }};\n" 20620 "} // namespace bar"); 20621 verifyFormat("namespace bar {\n" 20622 "// broken:\n" 20623 "auto foo{[]() -> foo<!5> { return {}; }};\n" 20624 "} // namespace bar"); 20625 verifyFormat("namespace bar {\n" 20626 "// broken:\n" 20627 "auto foo{[]() -> foo<~5> { return {}; }};\n" 20628 "} // namespace bar"); 20629 verifyFormat("namespace bar {\n" 20630 "// broken:\n" 20631 "auto foo{[]() -> foo<5 | 2> { return {}; }};\n" 20632 "} // namespace bar"); 20633 verifyFormat("namespace bar {\n" 20634 "// broken:\n" 20635 "auto foo{[]() -> foo<5 || 2> { return {}; }};\n" 20636 "} // namespace bar"); 20637 verifyFormat("namespace bar {\n" 20638 "// broken:\n" 20639 "auto foo{[]() -> foo<5 & 2> { return {}; }};\n" 20640 "} // namespace bar"); 20641 verifyFormat("namespace bar {\n" 20642 "// broken:\n" 20643 "auto foo{[]() -> foo<5 && 2> { return {}; }};\n" 20644 "} // namespace bar"); 20645 verifyFormat("namespace bar {\n" 20646 "// broken:\n" 20647 "auto foo{[]() -> foo<5 == 2> { return {}; }};\n" 20648 "} // namespace bar"); 20649 verifyFormat("namespace bar {\n" 20650 "// broken:\n" 20651 "auto foo{[]() -> foo<5 != 2> { return {}; }};\n" 20652 "} // namespace bar"); 20653 verifyFormat("namespace bar {\n" 20654 "// broken:\n" 20655 "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n" 20656 "} // namespace bar"); 20657 verifyFormat("namespace bar {\n" 20658 "// broken:\n" 20659 "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n" 20660 "} // namespace bar"); 20661 verifyFormat("namespace bar {\n" 20662 "// broken:\n" 20663 "auto foo{[]() -> foo<5 < 2> { return {}; }};\n" 20664 "} // namespace bar"); 20665 verifyFormat("namespace bar {\n" 20666 "// broken:\n" 20667 "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n" 20668 "} // namespace bar"); 20669 verifyFormat("[]() -> a<1> {};"); 20670 verifyFormat("[]() -> a<1> { ; };"); 20671 verifyFormat("[]() -> a<1> { ; }();"); 20672 verifyFormat("[a, a]() -> a<true> {};"); 20673 verifyFormat("[]() -> a<true> {};"); 20674 verifyFormat("[]() -> a<true> { ; };"); 20675 verifyFormat("[]() -> a<true> { ; }();"); 20676 verifyFormat("[a, a]() -> a<false> {};"); 20677 verifyFormat("[]() -> a<false> {};"); 20678 verifyFormat("[]() -> a<false> { ; };"); 20679 verifyFormat("[]() -> a<false> { ; }();"); 20680 verifyFormat("auto foo{[]() -> foo<false> { ; }};"); 20681 verifyFormat("namespace bar {\n" 20682 "auto foo{[]() -> foo<false> { ; }};\n" 20683 "} // namespace bar"); 20684 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 20685 " int j) -> int {\n" 20686 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 20687 "};"); 20688 verifyFormat( 20689 "aaaaaaaaaaaaaaaaaaaaaa(\n" 20690 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 20691 " return aaaaaaaaaaaaaaaaa;\n" 20692 " });", 20693 getLLVMStyleWithColumns(70)); 20694 verifyFormat("[]() //\n" 20695 " -> int {\n" 20696 " return 1; //\n" 20697 "};"); 20698 verifyFormat("[]() -> Void<T...> {};"); 20699 verifyFormat("[a, b]() -> Tuple<T...> { return {}; };"); 20700 verifyFormat("SomeFunction({[]() -> int[] { return {}; }});"); 20701 verifyFormat("SomeFunction({[]() -> int *[] { return {}; }});"); 20702 verifyFormat("SomeFunction({[]() -> int (*)[] { return {}; }});"); 20703 verifyFormat("SomeFunction({[]() -> ns::type<int (*)[]> { return {}; }});"); 20704 verifyFormat("return int{[x = x]() { return x; }()};"); 20705 20706 // Lambdas with explicit template argument lists. 20707 verifyFormat( 20708 "auto L = []<template <typename> class T, class U>(T<U> &&a) {};\n"); 20709 verifyFormat("auto L = []<class T>(T) {\n" 20710 " {\n" 20711 " f();\n" 20712 " g();\n" 20713 " }\n" 20714 "};\n"); 20715 verifyFormat("auto L = []<class... T>(T...) {\n" 20716 " {\n" 20717 " f();\n" 20718 " g();\n" 20719 " }\n" 20720 "};\n"); 20721 verifyFormat("auto L = []<typename... T>(T...) {\n" 20722 " {\n" 20723 " f();\n" 20724 " g();\n" 20725 " }\n" 20726 "};\n"); 20727 verifyFormat("auto L = []<template <typename...> class T>(T...) {\n" 20728 " {\n" 20729 " f();\n" 20730 " g();\n" 20731 " }\n" 20732 "};\n"); 20733 verifyFormat("auto L = []</*comment*/ class... T>(T...) {\n" 20734 " {\n" 20735 " f();\n" 20736 " g();\n" 20737 " }\n" 20738 "};\n"); 20739 20740 // Multiple lambdas in the same parentheses change indentation rules. These 20741 // lambdas are forced to start on new lines. 20742 verifyFormat("SomeFunction(\n" 20743 " []() {\n" 20744 " //\n" 20745 " },\n" 20746 " []() {\n" 20747 " //\n" 20748 " });"); 20749 20750 // A lambda passed as arg0 is always pushed to the next line. 20751 verifyFormat("SomeFunction(\n" 20752 " [this] {\n" 20753 " //\n" 20754 " },\n" 20755 " 1);\n"); 20756 20757 // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like 20758 // the arg0 case above. 20759 auto Style = getGoogleStyle(); 20760 Style.BinPackArguments = false; 20761 verifyFormat("SomeFunction(\n" 20762 " a,\n" 20763 " [this] {\n" 20764 " //\n" 20765 " },\n" 20766 " b);\n", 20767 Style); 20768 verifyFormat("SomeFunction(\n" 20769 " a,\n" 20770 " [this] {\n" 20771 " //\n" 20772 " },\n" 20773 " b);\n"); 20774 20775 // A lambda with a very long line forces arg0 to be pushed out irrespective of 20776 // the BinPackArguments value (as long as the code is wide enough). 20777 verifyFormat( 20778 "something->SomeFunction(\n" 20779 " a,\n" 20780 " [this] {\n" 20781 " " 20782 "D0000000000000000000000000000000000000000000000000000000000001();\n" 20783 " },\n" 20784 " b);\n"); 20785 20786 // A multi-line lambda is pulled up as long as the introducer fits on the 20787 // previous line and there are no further args. 20788 verifyFormat("function(1, [this, that] {\n" 20789 " //\n" 20790 "});\n"); 20791 verifyFormat("function([this, that] {\n" 20792 " //\n" 20793 "});\n"); 20794 // FIXME: this format is not ideal and we should consider forcing the first 20795 // arg onto its own line. 20796 verifyFormat("function(a, b, c, //\n" 20797 " d, [this, that] {\n" 20798 " //\n" 20799 " });\n"); 20800 20801 // Multiple lambdas are treated correctly even when there is a short arg0. 20802 verifyFormat("SomeFunction(\n" 20803 " 1,\n" 20804 " [this] {\n" 20805 " //\n" 20806 " },\n" 20807 " [this] {\n" 20808 " //\n" 20809 " },\n" 20810 " 1);\n"); 20811 20812 // More complex introducers. 20813 verifyFormat("return [i, args...] {};"); 20814 20815 // Not lambdas. 20816 verifyFormat("constexpr char hello[]{\"hello\"};"); 20817 verifyFormat("double &operator[](int i) { return 0; }\n" 20818 "int i;"); 20819 verifyFormat("std::unique_ptr<int[]> foo() {}"); 20820 verifyFormat("int i = a[a][a]->f();"); 20821 verifyFormat("int i = (*b)[a]->f();"); 20822 20823 // Other corner cases. 20824 verifyFormat("void f() {\n" 20825 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 20826 " );\n" 20827 "}"); 20828 20829 // Lambdas created through weird macros. 20830 verifyFormat("void f() {\n" 20831 " MACRO((const AA &a) { return 1; });\n" 20832 " MACRO((AA &a) { return 1; });\n" 20833 "}"); 20834 20835 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 20836 " doo_dah();\n" 20837 " doo_dah();\n" 20838 " })) {\n" 20839 "}"); 20840 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n" 20841 " doo_dah();\n" 20842 " doo_dah();\n" 20843 " })) {\n" 20844 "}"); 20845 verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n" 20846 " doo_dah();\n" 20847 " doo_dah();\n" 20848 " })) {\n" 20849 "}"); 20850 verifyFormat("auto lambda = []() {\n" 20851 " int a = 2\n" 20852 "#if A\n" 20853 " + 2\n" 20854 "#endif\n" 20855 " ;\n" 20856 "};"); 20857 20858 // Lambdas with complex multiline introducers. 20859 verifyFormat( 20860 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 20861 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n" 20862 " -> ::std::unordered_set<\n" 20863 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 20864 " //\n" 20865 " });"); 20866 20867 FormatStyle DoNotMerge = getLLVMStyle(); 20868 DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 20869 verifyFormat("auto c = []() {\n" 20870 " return b;\n" 20871 "};", 20872 "auto c = []() { return b; };", DoNotMerge); 20873 verifyFormat("auto c = []() {\n" 20874 "};", 20875 " auto c = []() {};", DoNotMerge); 20876 20877 FormatStyle MergeEmptyOnly = getLLVMStyle(); 20878 MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty; 20879 verifyFormat("auto c = []() {\n" 20880 " return b;\n" 20881 "};", 20882 "auto c = []() {\n" 20883 " return b;\n" 20884 " };", 20885 MergeEmptyOnly); 20886 verifyFormat("auto c = []() {};", 20887 "auto c = []() {\n" 20888 "};", 20889 MergeEmptyOnly); 20890 20891 FormatStyle MergeInline = getLLVMStyle(); 20892 MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline; 20893 verifyFormat("auto c = []() {\n" 20894 " return b;\n" 20895 "};", 20896 "auto c = []() { return b; };", MergeInline); 20897 verifyFormat("function([]() { return b; })", "function([]() { return b; })", 20898 MergeInline); 20899 verifyFormat("function([]() { return b; }, a)", 20900 "function([]() { return b; }, a)", MergeInline); 20901 verifyFormat("function(a, []() { return b; })", 20902 "function(a, []() { return b; })", MergeInline); 20903 20904 // Check option "BraceWrapping.BeforeLambdaBody" and different state of 20905 // AllowShortLambdasOnASingleLine 20906 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle(); 20907 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 20908 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 20909 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20910 FormatStyle::ShortLambdaStyle::SLS_None; 20911 verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n" 20912 " []()\n" 20913 " {\n" 20914 " return 17;\n" 20915 " });", 20916 LLVMWithBeforeLambdaBody); 20917 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n" 20918 " []()\n" 20919 " {\n" 20920 " });", 20921 LLVMWithBeforeLambdaBody); 20922 verifyFormat("auto fct_SLS_None = []()\n" 20923 "{\n" 20924 " return 17;\n" 20925 "};", 20926 LLVMWithBeforeLambdaBody); 20927 verifyFormat("TwoNestedLambdas_SLS_None(\n" 20928 " []()\n" 20929 " {\n" 20930 " return Call(\n" 20931 " []()\n" 20932 " {\n" 20933 " return 17;\n" 20934 " });\n" 20935 " });", 20936 LLVMWithBeforeLambdaBody); 20937 verifyFormat("void Fct() {\n" 20938 " return {[]()\n" 20939 " {\n" 20940 " return 17;\n" 20941 " }};\n" 20942 "}", 20943 LLVMWithBeforeLambdaBody); 20944 20945 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20946 FormatStyle::ShortLambdaStyle::SLS_Empty; 20947 verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n" 20948 " []()\n" 20949 " {\n" 20950 " return 17;\n" 20951 " });", 20952 LLVMWithBeforeLambdaBody); 20953 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});", 20954 LLVMWithBeforeLambdaBody); 20955 verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL" 20956 "ongFunctionName_SLS_Empty(\n" 20957 " []() {});", 20958 LLVMWithBeforeLambdaBody); 20959 verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n" 20960 " []()\n" 20961 " {\n" 20962 " return 17;\n" 20963 " });", 20964 LLVMWithBeforeLambdaBody); 20965 verifyFormat("auto fct_SLS_Empty = []()\n" 20966 "{\n" 20967 " return 17;\n" 20968 "};", 20969 LLVMWithBeforeLambdaBody); 20970 verifyFormat("TwoNestedLambdas_SLS_Empty(\n" 20971 " []()\n" 20972 " {\n" 20973 " return Call([]() {});\n" 20974 " });", 20975 LLVMWithBeforeLambdaBody); 20976 verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n" 20977 " []()\n" 20978 " {\n" 20979 " return Call([]() {});\n" 20980 " });", 20981 LLVMWithBeforeLambdaBody); 20982 verifyFormat( 20983 "FctWithLongLineInLambda_SLS_Empty(\n" 20984 " []()\n" 20985 " {\n" 20986 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 20987 " AndShouldNotBeConsiderAsInline,\n" 20988 " LambdaBodyMustBeBreak);\n" 20989 " });", 20990 LLVMWithBeforeLambdaBody); 20991 20992 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20993 FormatStyle::ShortLambdaStyle::SLS_Inline; 20994 verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });", 20995 LLVMWithBeforeLambdaBody); 20996 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});", 20997 LLVMWithBeforeLambdaBody); 20998 verifyFormat("auto fct_SLS_Inline = []()\n" 20999 "{\n" 21000 " return 17;\n" 21001 "};", 21002 LLVMWithBeforeLambdaBody); 21003 verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return " 21004 "17; }); });", 21005 LLVMWithBeforeLambdaBody); 21006 verifyFormat( 21007 "FctWithLongLineInLambda_SLS_Inline(\n" 21008 " []()\n" 21009 " {\n" 21010 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 21011 " AndShouldNotBeConsiderAsInline,\n" 21012 " LambdaBodyMustBeBreak);\n" 21013 " });", 21014 LLVMWithBeforeLambdaBody); 21015 verifyFormat("FctWithMultipleParams_SLS_Inline(" 21016 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n" 21017 " []() { return 17; });", 21018 LLVMWithBeforeLambdaBody); 21019 verifyFormat( 21020 "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });", 21021 LLVMWithBeforeLambdaBody); 21022 21023 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 21024 FormatStyle::ShortLambdaStyle::SLS_All; 21025 verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });", 21026 LLVMWithBeforeLambdaBody); 21027 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});", 21028 LLVMWithBeforeLambdaBody); 21029 verifyFormat("auto fct_SLS_All = []() { return 17; };", 21030 LLVMWithBeforeLambdaBody); 21031 verifyFormat("FctWithOneParam_SLS_All(\n" 21032 " []()\n" 21033 " {\n" 21034 " // A cool function...\n" 21035 " return 43;\n" 21036 " });", 21037 LLVMWithBeforeLambdaBody); 21038 verifyFormat("FctWithMultipleParams_SLS_All(" 21039 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n" 21040 " []() { return 17; });", 21041 LLVMWithBeforeLambdaBody); 21042 verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });", 21043 LLVMWithBeforeLambdaBody); 21044 verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });", 21045 LLVMWithBeforeLambdaBody); 21046 verifyFormat( 21047 "FctWithLongLineInLambda_SLS_All(\n" 21048 " []()\n" 21049 " {\n" 21050 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 21051 " AndShouldNotBeConsiderAsInline,\n" 21052 " LambdaBodyMustBeBreak);\n" 21053 " });", 21054 LLVMWithBeforeLambdaBody); 21055 verifyFormat( 21056 "auto fct_SLS_All = []()\n" 21057 "{\n" 21058 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 21059 " AndShouldNotBeConsiderAsInline,\n" 21060 " LambdaBodyMustBeBreak);\n" 21061 "};", 21062 LLVMWithBeforeLambdaBody); 21063 LLVMWithBeforeLambdaBody.BinPackParameters = false; 21064 verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);", 21065 LLVMWithBeforeLambdaBody); 21066 verifyFormat( 21067 "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n" 21068 " FirstParam,\n" 21069 " SecondParam,\n" 21070 " ThirdParam,\n" 21071 " FourthParam);", 21072 LLVMWithBeforeLambdaBody); 21073 verifyFormat("FctWithLongLineInLambda_SLS_All(\n" 21074 " []() { return " 21075 "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n" 21076 " FirstParam,\n" 21077 " SecondParam,\n" 21078 " ThirdParam,\n" 21079 " FourthParam);", 21080 LLVMWithBeforeLambdaBody); 21081 verifyFormat( 21082 "FctWithLongLineInLambda_SLS_All(FirstParam,\n" 21083 " SecondParam,\n" 21084 " ThirdParam,\n" 21085 " FourthParam,\n" 21086 " []() { return SomeValueNotSoLong; });", 21087 LLVMWithBeforeLambdaBody); 21088 verifyFormat("FctWithLongLineInLambda_SLS_All(\n" 21089 " []()\n" 21090 " {\n" 21091 " return " 21092 "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB" 21093 "eConsiderAsInline;\n" 21094 " });", 21095 LLVMWithBeforeLambdaBody); 21096 verifyFormat( 21097 "FctWithLongLineInLambda_SLS_All(\n" 21098 " []()\n" 21099 " {\n" 21100 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 21101 " AndShouldNotBeConsiderAsInline,\n" 21102 " LambdaBodyMustBeBreak);\n" 21103 " });", 21104 LLVMWithBeforeLambdaBody); 21105 verifyFormat("FctWithTwoParams_SLS_All(\n" 21106 " []()\n" 21107 " {\n" 21108 " // A cool function...\n" 21109 " return 43;\n" 21110 " },\n" 21111 " 87);", 21112 LLVMWithBeforeLambdaBody); 21113 verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);", 21114 LLVMWithBeforeLambdaBody); 21115 verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });", 21116 LLVMWithBeforeLambdaBody); 21117 verifyFormat( 21118 "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });", 21119 LLVMWithBeforeLambdaBody); 21120 verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; " 21121 "}); }, x);", 21122 LLVMWithBeforeLambdaBody); 21123 verifyFormat("TwoNestedLambdas_SLS_All(\n" 21124 " []()\n" 21125 " {\n" 21126 " // A cool function...\n" 21127 " return Call([]() { return 17; });\n" 21128 " });", 21129 LLVMWithBeforeLambdaBody); 21130 verifyFormat("TwoNestedLambdas_SLS_All(\n" 21131 " []()\n" 21132 " {\n" 21133 " return Call(\n" 21134 " []()\n" 21135 " {\n" 21136 " // A cool function...\n" 21137 " return 17;\n" 21138 " });\n" 21139 " });", 21140 LLVMWithBeforeLambdaBody); 21141 21142 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 21143 FormatStyle::ShortLambdaStyle::SLS_None; 21144 21145 verifyFormat("auto select = [this]() -> const Library::Object *\n" 21146 "{\n" 21147 " return MyAssignment::SelectFromList(this);\n" 21148 "};\n", 21149 LLVMWithBeforeLambdaBody); 21150 21151 verifyFormat("auto select = [this]() -> const Library::Object &\n" 21152 "{\n" 21153 " return MyAssignment::SelectFromList(this);\n" 21154 "};\n", 21155 LLVMWithBeforeLambdaBody); 21156 21157 verifyFormat("auto select = [this]() -> std::unique_ptr<Object>\n" 21158 "{\n" 21159 " return MyAssignment::SelectFromList(this);\n" 21160 "};\n", 21161 LLVMWithBeforeLambdaBody); 21162 21163 verifyFormat("namespace test {\n" 21164 "class Test {\n" 21165 "public:\n" 21166 " Test() = default;\n" 21167 "};\n" 21168 "} // namespace test", 21169 LLVMWithBeforeLambdaBody); 21170 21171 // Lambdas with different indentation styles. 21172 Style = getLLVMStyleWithColumns(100); 21173 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 21174 " return promise.then(\n" 21175 " [this, &someVariable, someObject = " 21176 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 21177 " return someObject.startAsyncAction().then(\n" 21178 " [this, &someVariable](AsyncActionResult result) " 21179 "mutable { result.processMore(); });\n" 21180 " });\n" 21181 "}\n", 21182 format("SomeResult doSomething(SomeObject promise) {\n" 21183 " return promise.then([this, &someVariable, someObject = " 21184 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 21185 " return someObject.startAsyncAction().then([this, " 21186 "&someVariable](AsyncActionResult result) mutable {\n" 21187 " result.processMore();\n" 21188 " });\n" 21189 " });\n" 21190 "}\n", 21191 Style)); 21192 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope; 21193 verifyFormat("test() {\n" 21194 " ([]() -> {\n" 21195 " int b = 32;\n" 21196 " return 3;\n" 21197 " }).foo();\n" 21198 "}", 21199 Style); 21200 verifyFormat("test() {\n" 21201 " []() -> {\n" 21202 " int b = 32;\n" 21203 " return 3;\n" 21204 " }\n" 21205 "}", 21206 Style); 21207 verifyFormat("std::sort(v.begin(), v.end(),\n" 21208 " [](const auto &someLongArgumentName, const auto " 21209 "&someOtherLongArgumentName) {\n" 21210 " return someLongArgumentName.someMemberVariable < " 21211 "someOtherLongArgumentName.someMemberVariable;\n" 21212 "});", 21213 Style); 21214 verifyFormat("test() {\n" 21215 " (\n" 21216 " []() -> {\n" 21217 " int b = 32;\n" 21218 " return 3;\n" 21219 " },\n" 21220 " foo, bar)\n" 21221 " .foo();\n" 21222 "}", 21223 Style); 21224 verifyFormat("test() {\n" 21225 " ([]() -> {\n" 21226 " int b = 32;\n" 21227 " return 3;\n" 21228 " })\n" 21229 " .foo()\n" 21230 " .bar();\n" 21231 "}", 21232 Style); 21233 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 21234 " return promise.then(\n" 21235 " [this, &someVariable, someObject = " 21236 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 21237 " return someObject.startAsyncAction().then(\n" 21238 " [this, &someVariable](AsyncActionResult result) mutable { " 21239 "result.processMore(); });\n" 21240 " });\n" 21241 "}\n", 21242 format("SomeResult doSomething(SomeObject promise) {\n" 21243 " return promise.then([this, &someVariable, someObject = " 21244 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 21245 " return someObject.startAsyncAction().then([this, " 21246 "&someVariable](AsyncActionResult result) mutable {\n" 21247 " result.processMore();\n" 21248 " });\n" 21249 " });\n" 21250 "}\n", 21251 Style)); 21252 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 21253 " return promise.then([this, &someVariable] {\n" 21254 " return someObject.startAsyncAction().then(\n" 21255 " [this, &someVariable](AsyncActionResult result) mutable { " 21256 "result.processMore(); });\n" 21257 " });\n" 21258 "}\n", 21259 format("SomeResult doSomething(SomeObject promise) {\n" 21260 " return promise.then([this, &someVariable] {\n" 21261 " return someObject.startAsyncAction().then([this, " 21262 "&someVariable](AsyncActionResult result) mutable {\n" 21263 " result.processMore();\n" 21264 " });\n" 21265 " });\n" 21266 "}\n", 21267 Style)); 21268 Style = getGoogleStyle(); 21269 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope; 21270 EXPECT_EQ("#define A \\\n" 21271 " [] { \\\n" 21272 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 21273 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 21274 " }", 21275 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 21276 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 21277 Style)); 21278 // TODO: The current formatting has a minor issue that's not worth fixing 21279 // right now whereby the closing brace is indented relative to the signature 21280 // instead of being aligned. This only happens with macros. 21281 } 21282 21283 TEST_F(FormatTest, LambdaWithLineComments) { 21284 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle(); 21285 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 21286 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 21287 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 21288 FormatStyle::ShortLambdaStyle::SLS_All; 21289 21290 verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody); 21291 verifyFormat("auto k = []() // comment\n" 21292 "{ return; }", 21293 LLVMWithBeforeLambdaBody); 21294 verifyFormat("auto k = []() /* comment */ { return; }", 21295 LLVMWithBeforeLambdaBody); 21296 verifyFormat("auto k = []() /* comment */ /* comment */ { return; }", 21297 LLVMWithBeforeLambdaBody); 21298 verifyFormat("auto k = []() // X\n" 21299 "{ return; }", 21300 LLVMWithBeforeLambdaBody); 21301 verifyFormat( 21302 "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n" 21303 "{ return; }", 21304 LLVMWithBeforeLambdaBody); 21305 } 21306 21307 TEST_F(FormatTest, EmptyLinesInLambdas) { 21308 verifyFormat("auto lambda = []() {\n" 21309 " x(); //\n" 21310 "};", 21311 "auto lambda = []() {\n" 21312 "\n" 21313 " x(); //\n" 21314 "\n" 21315 "};"); 21316 } 21317 21318 TEST_F(FormatTest, FormatsBlocks) { 21319 FormatStyle ShortBlocks = getLLVMStyle(); 21320 ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 21321 verifyFormat("int (^Block)(int, int);", ShortBlocks); 21322 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 21323 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 21324 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 21325 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 21326 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 21327 21328 verifyFormat("foo(^{ bar(); });", ShortBlocks); 21329 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 21330 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 21331 21332 verifyFormat("[operation setCompletionBlock:^{\n" 21333 " [self onOperationDone];\n" 21334 "}];"); 21335 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 21336 " [self onOperationDone];\n" 21337 "}]};"); 21338 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 21339 " f();\n" 21340 "}];"); 21341 verifyFormat("int a = [operation block:^int(int *i) {\n" 21342 " return 1;\n" 21343 "}];"); 21344 verifyFormat("[myObject doSomethingWith:arg1\n" 21345 " aaa:^int(int *a) {\n" 21346 " return 1;\n" 21347 " }\n" 21348 " bbb:f(a * bbbbbbbb)];"); 21349 21350 verifyFormat("[operation setCompletionBlock:^{\n" 21351 " [self.delegate newDataAvailable];\n" 21352 "}];", 21353 getLLVMStyleWithColumns(60)); 21354 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 21355 " NSString *path = [self sessionFilePath];\n" 21356 " if (path) {\n" 21357 " // ...\n" 21358 " }\n" 21359 "});"); 21360 verifyFormat("[[SessionService sharedService]\n" 21361 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 21362 " if (window) {\n" 21363 " [self windowDidLoad:window];\n" 21364 " } else {\n" 21365 " [self errorLoadingWindow];\n" 21366 " }\n" 21367 " }];"); 21368 verifyFormat("void (^largeBlock)(void) = ^{\n" 21369 " // ...\n" 21370 "};\n", 21371 getLLVMStyleWithColumns(40)); 21372 verifyFormat("[[SessionService sharedService]\n" 21373 " loadWindowWithCompletionBlock: //\n" 21374 " ^(SessionWindow *window) {\n" 21375 " if (window) {\n" 21376 " [self windowDidLoad:window];\n" 21377 " } else {\n" 21378 " [self errorLoadingWindow];\n" 21379 " }\n" 21380 " }];", 21381 getLLVMStyleWithColumns(60)); 21382 verifyFormat("[myObject doSomethingWith:arg1\n" 21383 " firstBlock:^(Foo *a) {\n" 21384 " // ...\n" 21385 " int i;\n" 21386 " }\n" 21387 " secondBlock:^(Bar *b) {\n" 21388 " // ...\n" 21389 " int i;\n" 21390 " }\n" 21391 " thirdBlock:^Foo(Bar *b) {\n" 21392 " // ...\n" 21393 " int i;\n" 21394 " }];"); 21395 verifyFormat("[myObject doSomethingWith:arg1\n" 21396 " firstBlock:-1\n" 21397 " secondBlock:^(Bar *b) {\n" 21398 " // ...\n" 21399 " int i;\n" 21400 " }];"); 21401 21402 verifyFormat("f(^{\n" 21403 " @autoreleasepool {\n" 21404 " if (a) {\n" 21405 " g();\n" 21406 " }\n" 21407 " }\n" 21408 "});"); 21409 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 21410 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" 21411 "};"); 21412 21413 FormatStyle FourIndent = getLLVMStyle(); 21414 FourIndent.ObjCBlockIndentWidth = 4; 21415 verifyFormat("[operation setCompletionBlock:^{\n" 21416 " [self onOperationDone];\n" 21417 "}];", 21418 FourIndent); 21419 } 21420 21421 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 21422 FormatStyle ZeroColumn = getLLVMStyleWithColumns(0); 21423 21424 verifyFormat("[[SessionService sharedService] " 21425 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 21426 " if (window) {\n" 21427 " [self windowDidLoad:window];\n" 21428 " } else {\n" 21429 " [self errorLoadingWindow];\n" 21430 " }\n" 21431 "}];", 21432 ZeroColumn); 21433 EXPECT_EQ("[[SessionService sharedService]\n" 21434 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 21435 " if (window) {\n" 21436 " [self windowDidLoad:window];\n" 21437 " } else {\n" 21438 " [self errorLoadingWindow];\n" 21439 " }\n" 21440 " }];", 21441 format("[[SessionService sharedService]\n" 21442 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 21443 " if (window) {\n" 21444 " [self windowDidLoad:window];\n" 21445 " } else {\n" 21446 " [self errorLoadingWindow];\n" 21447 " }\n" 21448 "}];", 21449 ZeroColumn)); 21450 verifyFormat("[myObject doSomethingWith:arg1\n" 21451 " firstBlock:^(Foo *a) {\n" 21452 " // ...\n" 21453 " int i;\n" 21454 " }\n" 21455 " secondBlock:^(Bar *b) {\n" 21456 " // ...\n" 21457 " int i;\n" 21458 " }\n" 21459 " thirdBlock:^Foo(Bar *b) {\n" 21460 " // ...\n" 21461 " int i;\n" 21462 " }];", 21463 ZeroColumn); 21464 verifyFormat("f(^{\n" 21465 " @autoreleasepool {\n" 21466 " if (a) {\n" 21467 " g();\n" 21468 " }\n" 21469 " }\n" 21470 "});", 21471 ZeroColumn); 21472 verifyFormat("void (^largeBlock)(void) = ^{\n" 21473 " // ...\n" 21474 "};", 21475 ZeroColumn); 21476 21477 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 21478 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 21479 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 21480 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 21481 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 21482 " int i;\n" 21483 "};", 21484 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 21485 } 21486 21487 TEST_F(FormatTest, SupportsCRLF) { 21488 EXPECT_EQ("int a;\r\n" 21489 "int b;\r\n" 21490 "int c;\r\n", 21491 format("int a;\r\n" 21492 " int b;\r\n" 21493 " int c;\r\n", 21494 getLLVMStyle())); 21495 EXPECT_EQ("int a;\r\n" 21496 "int b;\r\n" 21497 "int c;\r\n", 21498 format("int a;\r\n" 21499 " int b;\n" 21500 " int c;\r\n", 21501 getLLVMStyle())); 21502 EXPECT_EQ("int a;\n" 21503 "int b;\n" 21504 "int c;\n", 21505 format("int a;\r\n" 21506 " int b;\n" 21507 " int c;\n", 21508 getLLVMStyle())); 21509 EXPECT_EQ("\"aaaaaaa \"\r\n" 21510 "\"bbbbbbb\";\r\n", 21511 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 21512 EXPECT_EQ("#define A \\\r\n" 21513 " b; \\\r\n" 21514 " c; \\\r\n" 21515 " d;\r\n", 21516 format("#define A \\\r\n" 21517 " b; \\\r\n" 21518 " c; d; \r\n", 21519 getGoogleStyle())); 21520 21521 EXPECT_EQ("/*\r\n" 21522 "multi line block comments\r\n" 21523 "should not introduce\r\n" 21524 "an extra carriage return\r\n" 21525 "*/\r\n", 21526 format("/*\r\n" 21527 "multi line block comments\r\n" 21528 "should not introduce\r\n" 21529 "an extra carriage return\r\n" 21530 "*/\r\n")); 21531 EXPECT_EQ("/*\r\n" 21532 "\r\n" 21533 "*/", 21534 format("/*\r\n" 21535 " \r\r\r\n" 21536 "*/")); 21537 21538 FormatStyle style = getLLVMStyle(); 21539 21540 style.DeriveLineEnding = true; 21541 style.UseCRLF = false; 21542 EXPECT_EQ("union FooBarBazQux {\n" 21543 " int foo;\n" 21544 " int bar;\n" 21545 " int baz;\n" 21546 "};", 21547 format("union FooBarBazQux {\r\n" 21548 " int foo;\n" 21549 " int bar;\r\n" 21550 " int baz;\n" 21551 "};", 21552 style)); 21553 style.UseCRLF = true; 21554 EXPECT_EQ("union FooBarBazQux {\r\n" 21555 " int foo;\r\n" 21556 " int bar;\r\n" 21557 " int baz;\r\n" 21558 "};", 21559 format("union FooBarBazQux {\r\n" 21560 " int foo;\n" 21561 " int bar;\r\n" 21562 " int baz;\n" 21563 "};", 21564 style)); 21565 21566 style.DeriveLineEnding = false; 21567 style.UseCRLF = false; 21568 EXPECT_EQ("union FooBarBazQux {\n" 21569 " int foo;\n" 21570 " int bar;\n" 21571 " int baz;\n" 21572 " int qux;\n" 21573 "};", 21574 format("union FooBarBazQux {\r\n" 21575 " int foo;\n" 21576 " int bar;\r\n" 21577 " int baz;\n" 21578 " int qux;\r\n" 21579 "};", 21580 style)); 21581 style.UseCRLF = true; 21582 EXPECT_EQ("union FooBarBazQux {\r\n" 21583 " int foo;\r\n" 21584 " int bar;\r\n" 21585 " int baz;\r\n" 21586 " int qux;\r\n" 21587 "};", 21588 format("union FooBarBazQux {\r\n" 21589 " int foo;\n" 21590 " int bar;\r\n" 21591 " int baz;\n" 21592 " int qux;\n" 21593 "};", 21594 style)); 21595 21596 style.DeriveLineEnding = true; 21597 style.UseCRLF = false; 21598 EXPECT_EQ("union FooBarBazQux {\r\n" 21599 " int foo;\r\n" 21600 " int bar;\r\n" 21601 " int baz;\r\n" 21602 " int qux;\r\n" 21603 "};", 21604 format("union FooBarBazQux {\r\n" 21605 " int foo;\n" 21606 " int bar;\r\n" 21607 " int baz;\n" 21608 " int qux;\r\n" 21609 "};", 21610 style)); 21611 style.UseCRLF = true; 21612 EXPECT_EQ("union FooBarBazQux {\n" 21613 " int foo;\n" 21614 " int bar;\n" 21615 " int baz;\n" 21616 " int qux;\n" 21617 "};", 21618 format("union FooBarBazQux {\r\n" 21619 " int foo;\n" 21620 " int bar;\r\n" 21621 " int baz;\n" 21622 " int qux;\n" 21623 "};", 21624 style)); 21625 } 21626 21627 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 21628 verifyFormat("MY_CLASS(C) {\n" 21629 " int i;\n" 21630 " int j;\n" 21631 "};"); 21632 } 21633 21634 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 21635 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 21636 TwoIndent.ContinuationIndentWidth = 2; 21637 21638 EXPECT_EQ("int i =\n" 21639 " longFunction(\n" 21640 " arg);", 21641 format("int i = longFunction(arg);", TwoIndent)); 21642 21643 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 21644 SixIndent.ContinuationIndentWidth = 6; 21645 21646 EXPECT_EQ("int i =\n" 21647 " longFunction(\n" 21648 " arg);", 21649 format("int i = longFunction(arg);", SixIndent)); 21650 } 21651 21652 TEST_F(FormatTest, WrappedClosingParenthesisIndent) { 21653 FormatStyle Style = getLLVMStyle(); 21654 verifyFormat("int Foo::getter(\n" 21655 " //\n" 21656 ") const {\n" 21657 " return foo;\n" 21658 "}", 21659 Style); 21660 verifyFormat("void Foo::setter(\n" 21661 " //\n" 21662 ") {\n" 21663 " foo = 1;\n" 21664 "}", 21665 Style); 21666 } 21667 21668 TEST_F(FormatTest, SpacesInAngles) { 21669 FormatStyle Spaces = getLLVMStyle(); 21670 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 21671 21672 verifyFormat("vector< ::std::string > x1;", Spaces); 21673 verifyFormat("Foo< int, Bar > x2;", Spaces); 21674 verifyFormat("Foo< ::int, ::Bar > x3;", Spaces); 21675 21676 verifyFormat("static_cast< int >(arg);", Spaces); 21677 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 21678 verifyFormat("f< int, float >();", Spaces); 21679 verifyFormat("template <> g() {}", Spaces); 21680 verifyFormat("template < std::vector< int > > f() {}", Spaces); 21681 verifyFormat("std::function< void(int, int) > fct;", Spaces); 21682 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 21683 Spaces); 21684 21685 Spaces.Standard = FormatStyle::LS_Cpp03; 21686 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 21687 verifyFormat("A< A< int > >();", Spaces); 21688 21689 Spaces.SpacesInAngles = FormatStyle::SIAS_Never; 21690 verifyFormat("A<A<int> >();", Spaces); 21691 21692 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave; 21693 verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;", 21694 Spaces); 21695 verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;", 21696 Spaces); 21697 21698 verifyFormat("A<A<int> >();", Spaces); 21699 verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces); 21700 verifyFormat("A< A< int > >();", Spaces); 21701 21702 Spaces.Standard = FormatStyle::LS_Cpp11; 21703 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 21704 verifyFormat("A< A< int > >();", Spaces); 21705 21706 Spaces.SpacesInAngles = FormatStyle::SIAS_Never; 21707 verifyFormat("vector<::std::string> x4;", Spaces); 21708 verifyFormat("vector<int> x5;", Spaces); 21709 verifyFormat("Foo<int, Bar> x6;", Spaces); 21710 verifyFormat("Foo<::int, ::Bar> x7;", Spaces); 21711 21712 verifyFormat("A<A<int>>();", Spaces); 21713 21714 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave; 21715 verifyFormat("vector<::std::string> x4;", Spaces); 21716 verifyFormat("vector< ::std::string > x4;", Spaces); 21717 verifyFormat("vector<int> x5;", Spaces); 21718 verifyFormat("vector< int > x5;", Spaces); 21719 verifyFormat("Foo<int, Bar> x6;", Spaces); 21720 verifyFormat("Foo< int, Bar > x6;", Spaces); 21721 verifyFormat("Foo<::int, ::Bar> x7;", Spaces); 21722 verifyFormat("Foo< ::int, ::Bar > x7;", Spaces); 21723 21724 verifyFormat("A<A<int>>();", Spaces); 21725 verifyFormat("A< A< int > >();", Spaces); 21726 verifyFormat("A<A<int > >();", Spaces); 21727 verifyFormat("A< A< int>>();", Spaces); 21728 21729 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 21730 verifyFormat("// clang-format off\n" 21731 "foo<<<1, 1>>>();\n" 21732 "// clang-format on\n", 21733 Spaces); 21734 verifyFormat("// clang-format off\n" 21735 "foo< < <1, 1> > >();\n" 21736 "// clang-format on\n", 21737 Spaces); 21738 } 21739 21740 TEST_F(FormatTest, SpaceAfterTemplateKeyword) { 21741 FormatStyle Style = getLLVMStyle(); 21742 Style.SpaceAfterTemplateKeyword = false; 21743 verifyFormat("template<int> void foo();", Style); 21744 } 21745 21746 TEST_F(FormatTest, TripleAngleBrackets) { 21747 verifyFormat("f<<<1, 1>>>();"); 21748 verifyFormat("f<<<1, 1, 1, s>>>();"); 21749 verifyFormat("f<<<a, b, c, d>>>();"); 21750 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 21751 verifyFormat("f<param><<<1, 1>>>();"); 21752 verifyFormat("f<1><<<1, 1>>>();"); 21753 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 21754 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 21755 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 21756 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n" 21757 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();"); 21758 } 21759 21760 TEST_F(FormatTest, MergeLessLessAtEnd) { 21761 verifyFormat("<<"); 21762 EXPECT_EQ("< < <", format("\\\n<<<")); 21763 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 21764 "aaallvm::outs() <<"); 21765 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 21766 "aaaallvm::outs()\n <<"); 21767 } 21768 21769 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 21770 std::string code = "#if A\n" 21771 "#if B\n" 21772 "a.\n" 21773 "#endif\n" 21774 " a = 1;\n" 21775 "#else\n" 21776 "#endif\n" 21777 "#if C\n" 21778 "#else\n" 21779 "#endif\n"; 21780 EXPECT_EQ(code, format(code)); 21781 } 21782 21783 TEST_F(FormatTest, HandleConflictMarkers) { 21784 // Git/SVN conflict markers. 21785 EXPECT_EQ("int a;\n" 21786 "void f() {\n" 21787 " callme(some(parameter1,\n" 21788 "<<<<<<< text by the vcs\n" 21789 " parameter2),\n" 21790 "||||||| text by the vcs\n" 21791 " parameter2),\n" 21792 " parameter3,\n" 21793 "======= text by the vcs\n" 21794 " parameter2, parameter3),\n" 21795 ">>>>>>> text by the vcs\n" 21796 " otherparameter);\n", 21797 format("int a;\n" 21798 "void f() {\n" 21799 " callme(some(parameter1,\n" 21800 "<<<<<<< text by the vcs\n" 21801 " parameter2),\n" 21802 "||||||| text by the vcs\n" 21803 " parameter2),\n" 21804 " parameter3,\n" 21805 "======= text by the vcs\n" 21806 " parameter2,\n" 21807 " parameter3),\n" 21808 ">>>>>>> text by the vcs\n" 21809 " otherparameter);\n")); 21810 21811 // Perforce markers. 21812 EXPECT_EQ("void f() {\n" 21813 " function(\n" 21814 ">>>> text by the vcs\n" 21815 " parameter,\n" 21816 "==== text by the vcs\n" 21817 " parameter,\n" 21818 "==== text by the vcs\n" 21819 " parameter,\n" 21820 "<<<< text by the vcs\n" 21821 " parameter);\n", 21822 format("void f() {\n" 21823 " function(\n" 21824 ">>>> text by the vcs\n" 21825 " parameter,\n" 21826 "==== text by the vcs\n" 21827 " parameter,\n" 21828 "==== text by the vcs\n" 21829 " parameter,\n" 21830 "<<<< text by the vcs\n" 21831 " parameter);\n")); 21832 21833 EXPECT_EQ("<<<<<<<\n" 21834 "|||||||\n" 21835 "=======\n" 21836 ">>>>>>>", 21837 format("<<<<<<<\n" 21838 "|||||||\n" 21839 "=======\n" 21840 ">>>>>>>")); 21841 21842 EXPECT_EQ("<<<<<<<\n" 21843 "|||||||\n" 21844 "int i;\n" 21845 "=======\n" 21846 ">>>>>>>", 21847 format("<<<<<<<\n" 21848 "|||||||\n" 21849 "int i;\n" 21850 "=======\n" 21851 ">>>>>>>")); 21852 21853 // FIXME: Handle parsing of macros around conflict markers correctly: 21854 EXPECT_EQ("#define Macro \\\n" 21855 "<<<<<<<\n" 21856 "Something \\\n" 21857 "|||||||\n" 21858 "Else \\\n" 21859 "=======\n" 21860 "Other \\\n" 21861 ">>>>>>>\n" 21862 " End int i;\n", 21863 format("#define Macro \\\n" 21864 "<<<<<<<\n" 21865 " Something \\\n" 21866 "|||||||\n" 21867 " Else \\\n" 21868 "=======\n" 21869 " Other \\\n" 21870 ">>>>>>>\n" 21871 " End\n" 21872 "int i;\n")); 21873 21874 verifyFormat(R"(==== 21875 #ifdef A 21876 a 21877 #else 21878 b 21879 #endif 21880 )"); 21881 } 21882 21883 TEST_F(FormatTest, DisableRegions) { 21884 EXPECT_EQ("int i;\n" 21885 "// clang-format off\n" 21886 " int j;\n" 21887 "// clang-format on\n" 21888 "int k;", 21889 format(" int i;\n" 21890 " // clang-format off\n" 21891 " int j;\n" 21892 " // clang-format on\n" 21893 " int k;")); 21894 EXPECT_EQ("int i;\n" 21895 "/* clang-format off */\n" 21896 " int j;\n" 21897 "/* clang-format on */\n" 21898 "int k;", 21899 format(" int i;\n" 21900 " /* clang-format off */\n" 21901 " int j;\n" 21902 " /* clang-format on */\n" 21903 " int k;")); 21904 21905 // Don't reflow comments within disabled regions. 21906 EXPECT_EQ("// clang-format off\n" 21907 "// long long long long long long line\n" 21908 "/* clang-format on */\n" 21909 "/* long long long\n" 21910 " * long long long\n" 21911 " * line */\n" 21912 "int i;\n" 21913 "/* clang-format off */\n" 21914 "/* long long long long long long line */\n", 21915 format("// clang-format off\n" 21916 "// long long long long long long line\n" 21917 "/* clang-format on */\n" 21918 "/* long long long long long long line */\n" 21919 "int i;\n" 21920 "/* clang-format off */\n" 21921 "/* long long long long long long line */\n", 21922 getLLVMStyleWithColumns(20))); 21923 } 21924 21925 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 21926 format("? ) ="); 21927 verifyNoCrash("#define a\\\n /**/}"); 21928 } 21929 21930 TEST_F(FormatTest, FormatsTableGenCode) { 21931 FormatStyle Style = getLLVMStyle(); 21932 Style.Language = FormatStyle::LK_TableGen; 21933 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 21934 } 21935 21936 TEST_F(FormatTest, ArrayOfTemplates) { 21937 EXPECT_EQ("auto a = new unique_ptr<int>[10];", 21938 format("auto a = new unique_ptr<int > [ 10];")); 21939 21940 FormatStyle Spaces = getLLVMStyle(); 21941 Spaces.SpacesInSquareBrackets = true; 21942 EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];", 21943 format("auto a = new unique_ptr<int > [10];", Spaces)); 21944 } 21945 21946 TEST_F(FormatTest, ArrayAsTemplateType) { 21947 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;", 21948 format("auto a = unique_ptr < Foo < Bar>[ 10]> ;")); 21949 21950 FormatStyle Spaces = getLLVMStyle(); 21951 Spaces.SpacesInSquareBrackets = true; 21952 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;", 21953 format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces)); 21954 } 21955 21956 TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); } 21957 21958 TEST(FormatStyle, GetStyleWithEmptyFileName) { 21959 llvm::vfs::InMemoryFileSystem FS; 21960 auto Style1 = getStyle("file", "", "Google", "", &FS); 21961 ASSERT_TRUE((bool)Style1); 21962 ASSERT_EQ(*Style1, getGoogleStyle()); 21963 } 21964 21965 TEST(FormatStyle, GetStyleOfFile) { 21966 llvm::vfs::InMemoryFileSystem FS; 21967 // Test 1: format file in the same directory. 21968 ASSERT_TRUE( 21969 FS.addFile("/a/.clang-format", 0, 21970 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 21971 ASSERT_TRUE( 21972 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 21973 auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS); 21974 ASSERT_TRUE((bool)Style1); 21975 ASSERT_EQ(*Style1, getLLVMStyle()); 21976 21977 // Test 2.1: fallback to default. 21978 ASSERT_TRUE( 21979 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 21980 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS); 21981 ASSERT_TRUE((bool)Style2); 21982 ASSERT_EQ(*Style2, getMozillaStyle()); 21983 21984 // Test 2.2: no format on 'none' fallback style. 21985 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 21986 ASSERT_TRUE((bool)Style2); 21987 ASSERT_EQ(*Style2, getNoStyle()); 21988 21989 // Test 2.3: format if config is found with no based style while fallback is 21990 // 'none'. 21991 ASSERT_TRUE(FS.addFile("/b/.clang-format", 0, 21992 llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2"))); 21993 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 21994 ASSERT_TRUE((bool)Style2); 21995 ASSERT_EQ(*Style2, getLLVMStyle()); 21996 21997 // Test 2.4: format if yaml with no based style, while fallback is 'none'. 21998 Style2 = getStyle("{}", "a.h", "none", "", &FS); 21999 ASSERT_TRUE((bool)Style2); 22000 ASSERT_EQ(*Style2, getLLVMStyle()); 22001 22002 // Test 3: format file in parent directory. 22003 ASSERT_TRUE( 22004 FS.addFile("/c/.clang-format", 0, 22005 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 22006 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, 22007 llvm::MemoryBuffer::getMemBuffer("int i;"))); 22008 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS); 22009 ASSERT_TRUE((bool)Style3); 22010 ASSERT_EQ(*Style3, getGoogleStyle()); 22011 22012 // Test 4: error on invalid fallback style 22013 auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS); 22014 ASSERT_FALSE((bool)Style4); 22015 llvm::consumeError(Style4.takeError()); 22016 22017 // Test 5: error on invalid yaml on command line 22018 auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS); 22019 ASSERT_FALSE((bool)Style5); 22020 llvm::consumeError(Style5.takeError()); 22021 22022 // Test 6: error on invalid style 22023 auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS); 22024 ASSERT_FALSE((bool)Style6); 22025 llvm::consumeError(Style6.takeError()); 22026 22027 // Test 7: found config file, error on parsing it 22028 ASSERT_TRUE( 22029 FS.addFile("/d/.clang-format", 0, 22030 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n" 22031 "InvalidKey: InvalidValue"))); 22032 ASSERT_TRUE( 22033 FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 22034 auto Style7a = getStyle("file", "/d/.clang-format", "LLVM", "", &FS); 22035 ASSERT_FALSE((bool)Style7a); 22036 llvm::consumeError(Style7a.takeError()); 22037 22038 auto Style7b = getStyle("file", "/d/.clang-format", "LLVM", "", &FS, true); 22039 ASSERT_TRUE((bool)Style7b); 22040 22041 // Test 8: inferred per-language defaults apply. 22042 auto StyleTd = getStyle("file", "x.td", "llvm", "", &FS); 22043 ASSERT_TRUE((bool)StyleTd); 22044 ASSERT_EQ(*StyleTd, getLLVMStyle(FormatStyle::LK_TableGen)); 22045 22046 // Test 9.1.1: overwriting a file style, when no parent file exists with no 22047 // fallback style. 22048 ASSERT_TRUE(FS.addFile( 22049 "/e/sub/.clang-format", 0, 22050 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: InheritParentConfig\n" 22051 "ColumnLimit: 20"))); 22052 ASSERT_TRUE(FS.addFile("/e/sub/code.cpp", 0, 22053 llvm::MemoryBuffer::getMemBuffer("int i;"))); 22054 auto Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS); 22055 ASSERT_TRUE(static_cast<bool>(Style9)); 22056 ASSERT_EQ(*Style9, [] { 22057 auto Style = getNoStyle(); 22058 Style.ColumnLimit = 20; 22059 return Style; 22060 }()); 22061 22062 // Test 9.1.2: propagate more than one level with no parent file. 22063 ASSERT_TRUE(FS.addFile("/e/sub/sub/code.cpp", 0, 22064 llvm::MemoryBuffer::getMemBuffer("int i;"))); 22065 ASSERT_TRUE(FS.addFile("/e/sub/sub/.clang-format", 0, 22066 llvm::MemoryBuffer::getMemBuffer( 22067 "BasedOnStyle: InheritParentConfig\n" 22068 "WhitespaceSensitiveMacros: ['FOO', 'BAR']"))); 22069 std::vector<std::string> NonDefaultWhiteSpaceMacros{"FOO", "BAR"}; 22070 22071 ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros); 22072 Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS); 22073 ASSERT_TRUE(static_cast<bool>(Style9)); 22074 ASSERT_EQ(*Style9, [&NonDefaultWhiteSpaceMacros] { 22075 auto Style = getNoStyle(); 22076 Style.ColumnLimit = 20; 22077 Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros; 22078 return Style; 22079 }()); 22080 22081 // Test 9.2: with LLVM fallback style 22082 Style9 = getStyle("file", "/e/sub/code.cpp", "LLVM", "", &FS); 22083 ASSERT_TRUE(static_cast<bool>(Style9)); 22084 ASSERT_EQ(*Style9, [] { 22085 auto Style = getLLVMStyle(); 22086 Style.ColumnLimit = 20; 22087 return Style; 22088 }()); 22089 22090 // Test 9.3: with a parent file 22091 ASSERT_TRUE( 22092 FS.addFile("/e/.clang-format", 0, 22093 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google\n" 22094 "UseTab: Always"))); 22095 Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS); 22096 ASSERT_TRUE(static_cast<bool>(Style9)); 22097 ASSERT_EQ(*Style9, [] { 22098 auto Style = getGoogleStyle(); 22099 Style.ColumnLimit = 20; 22100 Style.UseTab = FormatStyle::UT_Always; 22101 return Style; 22102 }()); 22103 22104 // Test 9.4: propagate more than one level with a parent file. 22105 const auto SubSubStyle = [&NonDefaultWhiteSpaceMacros] { 22106 auto Style = getGoogleStyle(); 22107 Style.ColumnLimit = 20; 22108 Style.UseTab = FormatStyle::UT_Always; 22109 Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros; 22110 return Style; 22111 }(); 22112 22113 ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros); 22114 Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS); 22115 ASSERT_TRUE(static_cast<bool>(Style9)); 22116 ASSERT_EQ(*Style9, SubSubStyle); 22117 22118 // Test 9.5: use InheritParentConfig as style name 22119 Style9 = 22120 getStyle("inheritparentconfig", "/e/sub/sub/code.cpp", "none", "", &FS); 22121 ASSERT_TRUE(static_cast<bool>(Style9)); 22122 ASSERT_EQ(*Style9, SubSubStyle); 22123 22124 // Test 9.6: use command line style with inheritance 22125 Style9 = getStyle("{BasedOnStyle: InheritParentConfig}", "/e/sub/code.cpp", 22126 "none", "", &FS); 22127 ASSERT_TRUE(static_cast<bool>(Style9)); 22128 ASSERT_EQ(*Style9, SubSubStyle); 22129 22130 // Test 9.7: use command line style with inheritance and own config 22131 Style9 = getStyle("{BasedOnStyle: InheritParentConfig, " 22132 "WhitespaceSensitiveMacros: ['FOO', 'BAR']}", 22133 "/e/sub/code.cpp", "none", "", &FS); 22134 ASSERT_TRUE(static_cast<bool>(Style9)); 22135 ASSERT_EQ(*Style9, SubSubStyle); 22136 22137 // Test 9.8: use inheritance from a file without BasedOnStyle 22138 ASSERT_TRUE(FS.addFile("/e/withoutbase/.clang-format", 0, 22139 llvm::MemoryBuffer::getMemBuffer("ColumnLimit: 123"))); 22140 ASSERT_TRUE( 22141 FS.addFile("/e/withoutbase/sub/.clang-format", 0, 22142 llvm::MemoryBuffer::getMemBuffer( 22143 "BasedOnStyle: InheritParentConfig\nIndentWidth: 7"))); 22144 // Make sure we do not use the fallback style 22145 Style9 = getStyle("file", "/e/withoutbase/code.cpp", "google", "", &FS); 22146 ASSERT_TRUE(static_cast<bool>(Style9)); 22147 ASSERT_EQ(*Style9, [] { 22148 auto Style = getLLVMStyle(); 22149 Style.ColumnLimit = 123; 22150 return Style; 22151 }()); 22152 22153 Style9 = getStyle("file", "/e/withoutbase/sub/code.cpp", "google", "", &FS); 22154 ASSERT_TRUE(static_cast<bool>(Style9)); 22155 ASSERT_EQ(*Style9, [] { 22156 auto Style = getLLVMStyle(); 22157 Style.ColumnLimit = 123; 22158 Style.IndentWidth = 7; 22159 return Style; 22160 }()); 22161 22162 // Test 9.9: use inheritance from a specific config file. 22163 Style9 = getStyle("file:/e/sub/sub/.clang-format", "/e/sub/sub/code.cpp", 22164 "none", "", &FS); 22165 ASSERT_TRUE(static_cast<bool>(Style9)); 22166 ASSERT_EQ(*Style9, SubSubStyle); 22167 } 22168 22169 TEST(FormatStyle, GetStyleOfSpecificFile) { 22170 llvm::vfs::InMemoryFileSystem FS; 22171 // Specify absolute path to a format file in a parent directory. 22172 ASSERT_TRUE( 22173 FS.addFile("/e/.clang-format", 0, 22174 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 22175 ASSERT_TRUE( 22176 FS.addFile("/e/explicit.clang-format", 0, 22177 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 22178 ASSERT_TRUE(FS.addFile("/e/sub/sub/sub/test.cpp", 0, 22179 llvm::MemoryBuffer::getMemBuffer("int i;"))); 22180 auto Style = getStyle("file:/e/explicit.clang-format", 22181 "/e/sub/sub/sub/test.cpp", "LLVM", "", &FS); 22182 ASSERT_TRUE(static_cast<bool>(Style)); 22183 ASSERT_EQ(*Style, getGoogleStyle()); 22184 22185 // Specify relative path to a format file. 22186 ASSERT_TRUE( 22187 FS.addFile("../../e/explicit.clang-format", 0, 22188 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 22189 Style = getStyle("file:../../e/explicit.clang-format", 22190 "/e/sub/sub/sub/test.cpp", "LLVM", "", &FS); 22191 ASSERT_TRUE(static_cast<bool>(Style)); 22192 ASSERT_EQ(*Style, getGoogleStyle()); 22193 22194 // Specify path to a format file that does not exist. 22195 Style = getStyle("file:/e/missing.clang-format", "/e/sub/sub/sub/test.cpp", 22196 "LLVM", "", &FS); 22197 ASSERT_FALSE(static_cast<bool>(Style)); 22198 llvm::consumeError(Style.takeError()); 22199 22200 // Specify path to a file on the filesystem. 22201 SmallString<128> FormatFilePath; 22202 std::error_code ECF = llvm::sys::fs::createTemporaryFile( 22203 "FormatFileTest", "tpl", FormatFilePath); 22204 EXPECT_FALSE((bool)ECF); 22205 llvm::raw_fd_ostream FormatFileTest(FormatFilePath, ECF); 22206 EXPECT_FALSE((bool)ECF); 22207 FormatFileTest << "BasedOnStyle: Google\n"; 22208 FormatFileTest.close(); 22209 22210 SmallString<128> TestFilePath; 22211 std::error_code ECT = 22212 llvm::sys::fs::createTemporaryFile("CodeFileTest", "cc", TestFilePath); 22213 EXPECT_FALSE((bool)ECT); 22214 llvm::raw_fd_ostream CodeFileTest(TestFilePath, ECT); 22215 CodeFileTest << "int i;\n"; 22216 CodeFileTest.close(); 22217 22218 std::string format_file_arg = std::string("file:") + FormatFilePath.c_str(); 22219 Style = getStyle(format_file_arg, TestFilePath, "LLVM", "", nullptr); 22220 22221 llvm::sys::fs::remove(FormatFilePath.c_str()); 22222 llvm::sys::fs::remove(TestFilePath.c_str()); 22223 ASSERT_TRUE(static_cast<bool>(Style)); 22224 ASSERT_EQ(*Style, getGoogleStyle()); 22225 } 22226 22227 TEST_F(ReplacementTest, FormatCodeAfterReplacements) { 22228 // Column limit is 20. 22229 std::string Code = "Type *a =\n" 22230 " new Type();\n" 22231 "g(iiiii, 0, jjjjj,\n" 22232 " 0, kkkkk, 0, mm);\n" 22233 "int bad = format ;"; 22234 std::string Expected = "auto a = new Type();\n" 22235 "g(iiiii, nullptr,\n" 22236 " jjjjj, nullptr,\n" 22237 " kkkkk, nullptr,\n" 22238 " mm);\n" 22239 "int bad = format ;"; 22240 FileID ID = Context.createInMemoryFile("format.cpp", Code); 22241 tooling::Replacements Replaces = toReplacements( 22242 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6, 22243 "auto "), 22244 tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1, 22245 "nullptr"), 22246 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1, 22247 "nullptr"), 22248 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1, 22249 "nullptr")}); 22250 22251 FormatStyle Style = getLLVMStyle(); 22252 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. 22253 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 22254 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 22255 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 22256 auto Result = applyAllReplacements(Code, *FormattedReplaces); 22257 EXPECT_TRUE(static_cast<bool>(Result)); 22258 EXPECT_EQ(Expected, *Result); 22259 } 22260 22261 TEST_F(ReplacementTest, SortIncludesAfterReplacement) { 22262 std::string Code = "#include \"a.h\"\n" 22263 "#include \"c.h\"\n" 22264 "\n" 22265 "int main() {\n" 22266 " return 0;\n" 22267 "}"; 22268 std::string Expected = "#include \"a.h\"\n" 22269 "#include \"b.h\"\n" 22270 "#include \"c.h\"\n" 22271 "\n" 22272 "int main() {\n" 22273 " return 0;\n" 22274 "}"; 22275 FileID ID = Context.createInMemoryFile("fix.cpp", Code); 22276 tooling::Replacements Replaces = toReplacements( 22277 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0, 22278 "#include \"b.h\"\n")}); 22279 22280 FormatStyle Style = getLLVMStyle(); 22281 Style.SortIncludes = FormatStyle::SI_CaseSensitive; 22282 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 22283 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 22284 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 22285 auto Result = applyAllReplacements(Code, *FormattedReplaces); 22286 EXPECT_TRUE(static_cast<bool>(Result)); 22287 EXPECT_EQ(Expected, *Result); 22288 } 22289 22290 TEST_F(FormatTest, FormatSortsUsingDeclarations) { 22291 EXPECT_EQ("using std::cin;\n" 22292 "using std::cout;", 22293 format("using std::cout;\n" 22294 "using std::cin;", 22295 getGoogleStyle())); 22296 } 22297 22298 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) { 22299 FormatStyle Style = getLLVMStyle(); 22300 Style.Standard = FormatStyle::LS_Cpp03; 22301 // cpp03 recognize this string as identifier u8 and literal character 'a' 22302 EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style)); 22303 } 22304 22305 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) { 22306 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers 22307 // all modes, including C++11, C++14 and C++17 22308 EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';")); 22309 } 22310 22311 TEST_F(FormatTest, DoNotFormatLikelyXml) { 22312 EXPECT_EQ("<!-- ;> -->", format("<!-- ;> -->", getGoogleStyle())); 22313 EXPECT_EQ(" <!-- >; -->", format(" <!-- >; -->", getGoogleStyle())); 22314 } 22315 22316 TEST_F(FormatTest, StructuredBindings) { 22317 // Structured bindings is a C++17 feature. 22318 // all modes, including C++11, C++14 and C++17 22319 verifyFormat("auto [a, b] = f();"); 22320 EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();")); 22321 EXPECT_EQ("const auto [a, b] = f();", format("const auto[a, b] = f();")); 22322 EXPECT_EQ("auto const [a, b] = f();", format("auto const[a, b] = f();")); 22323 EXPECT_EQ("auto const volatile [a, b] = f();", 22324 format("auto const volatile[a, b] = f();")); 22325 EXPECT_EQ("auto [a, b, c] = f();", format("auto [ a , b,c ] = f();")); 22326 EXPECT_EQ("auto &[a, b, c] = f();", 22327 format("auto &[ a , b,c ] = f();")); 22328 EXPECT_EQ("auto &&[a, b, c] = f();", 22329 format("auto &&[ a , b,c ] = f();")); 22330 EXPECT_EQ("auto const &[a, b] = f();", format("auto const&[a, b] = f();")); 22331 EXPECT_EQ("auto const volatile &&[a, b] = f();", 22332 format("auto const volatile &&[a, b] = f();")); 22333 EXPECT_EQ("auto const &&[a, b] = f();", 22334 format("auto const && [a, b] = f();")); 22335 EXPECT_EQ("const auto &[a, b] = f();", 22336 format("const auto & [a, b] = f();")); 22337 EXPECT_EQ("const auto volatile &&[a, b] = f();", 22338 format("const auto volatile &&[a, b] = f();")); 22339 EXPECT_EQ("volatile const auto &&[a, b] = f();", 22340 format("volatile const auto &&[a, b] = f();")); 22341 EXPECT_EQ("const auto &&[a, b] = f();", 22342 format("const auto && [a, b] = f();")); 22343 22344 // Make sure we don't mistake structured bindings for lambdas. 22345 FormatStyle PointerMiddle = getLLVMStyle(); 22346 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 22347 verifyFormat("auto [a1, b]{A * i};", getGoogleStyle()); 22348 verifyFormat("auto [a2, b]{A * i};", getLLVMStyle()); 22349 verifyFormat("auto [a3, b]{A * i};", PointerMiddle); 22350 verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle()); 22351 verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle()); 22352 verifyFormat("auto const [a3, b]{A * i};", PointerMiddle); 22353 verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle()); 22354 verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle()); 22355 verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle); 22356 verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle()); 22357 verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle()); 22358 verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle); 22359 22360 EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}", 22361 format("for (const auto && [a, b] : some_range) {\n}")); 22362 EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}", 22363 format("for (const auto & [a, b] : some_range) {\n}")); 22364 EXPECT_EQ("for (const auto [a, b] : some_range) {\n}", 22365 format("for (const auto[a, b] : some_range) {\n}")); 22366 EXPECT_EQ("auto [x, y](expr);", format("auto[x,y] (expr);")); 22367 EXPECT_EQ("auto &[x, y](expr);", format("auto & [x,y] (expr);")); 22368 EXPECT_EQ("auto &&[x, y](expr);", format("auto && [x,y] (expr);")); 22369 EXPECT_EQ("auto const &[x, y](expr);", 22370 format("auto const & [x,y] (expr);")); 22371 EXPECT_EQ("auto const &&[x, y](expr);", 22372 format("auto const && [x,y] (expr);")); 22373 EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y] {expr};")); 22374 EXPECT_EQ("auto const &[x, y]{expr};", 22375 format("auto const & [x,y] {expr};")); 22376 EXPECT_EQ("auto const &&[x, y]{expr};", 22377 format("auto const && [x,y] {expr};")); 22378 22379 FormatStyle Spaces = getLLVMStyle(); 22380 Spaces.SpacesInSquareBrackets = true; 22381 verifyFormat("auto [ a, b ] = f();", Spaces); 22382 verifyFormat("auto &&[ a, b ] = f();", Spaces); 22383 verifyFormat("auto &[ a, b ] = f();", Spaces); 22384 verifyFormat("auto const &&[ a, b ] = f();", Spaces); 22385 verifyFormat("auto const &[ a, b ] = f();", Spaces); 22386 } 22387 22388 TEST_F(FormatTest, FileAndCode) { 22389 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", "")); 22390 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", "")); 22391 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", "")); 22392 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "")); 22393 EXPECT_EQ(FormatStyle::LK_ObjC, 22394 guessLanguage("foo.h", "@interface Foo\n@end\n")); 22395 EXPECT_EQ( 22396 FormatStyle::LK_ObjC, 22397 guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }")); 22398 EXPECT_EQ(FormatStyle::LK_ObjC, 22399 guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))")); 22400 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;")); 22401 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", "")); 22402 EXPECT_EQ(FormatStyle::LK_ObjC, 22403 guessLanguage("foo", "@interface Foo\n@end\n")); 22404 EXPECT_EQ(FormatStyle::LK_ObjC, 22405 guessLanguage("foo.h", "int DoStuff(CGRect rect);\n")); 22406 EXPECT_EQ( 22407 FormatStyle::LK_ObjC, 22408 guessLanguage("foo.h", 22409 "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n")); 22410 EXPECT_EQ( 22411 FormatStyle::LK_Cpp, 22412 guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;")); 22413 } 22414 22415 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) { 22416 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];")); 22417 EXPECT_EQ(FormatStyle::LK_ObjC, 22418 guessLanguage("foo.h", "array[[calculator getIndex]];")); 22419 EXPECT_EQ(FormatStyle::LK_Cpp, 22420 guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];")); 22421 EXPECT_EQ( 22422 FormatStyle::LK_Cpp, 22423 guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];")); 22424 EXPECT_EQ(FormatStyle::LK_ObjC, 22425 guessLanguage("foo.h", "[[noreturn foo] bar];")); 22426 EXPECT_EQ(FormatStyle::LK_Cpp, 22427 guessLanguage("foo.h", "[[clang::fallthrough]];")); 22428 EXPECT_EQ(FormatStyle::LK_ObjC, 22429 guessLanguage("foo.h", "[[clang:fallthrough] foo];")); 22430 EXPECT_EQ(FormatStyle::LK_Cpp, 22431 guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];")); 22432 EXPECT_EQ(FormatStyle::LK_Cpp, 22433 guessLanguage("foo.h", "[[using clang: fallthrough]];")); 22434 EXPECT_EQ(FormatStyle::LK_ObjC, 22435 guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];")); 22436 EXPECT_EQ(FormatStyle::LK_Cpp, 22437 guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];")); 22438 EXPECT_EQ( 22439 FormatStyle::LK_Cpp, 22440 guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)")); 22441 EXPECT_EQ( 22442 FormatStyle::LK_Cpp, 22443 guessLanguage("foo.h", 22444 "[[clang::callable_when(\"unconsumed\", \"unknown\")]]")); 22445 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]")); 22446 } 22447 22448 TEST_F(FormatTest, GuessLanguageWithCaret) { 22449 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);")); 22450 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);")); 22451 EXPECT_EQ(FormatStyle::LK_ObjC, 22452 guessLanguage("foo.h", "int(^)(char, float);")); 22453 EXPECT_EQ(FormatStyle::LK_ObjC, 22454 guessLanguage("foo.h", "int(^foo)(char, float);")); 22455 EXPECT_EQ(FormatStyle::LK_ObjC, 22456 guessLanguage("foo.h", "int(^foo[10])(char, float);")); 22457 EXPECT_EQ(FormatStyle::LK_ObjC, 22458 guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);")); 22459 EXPECT_EQ( 22460 FormatStyle::LK_ObjC, 22461 guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);")); 22462 } 22463 22464 TEST_F(FormatTest, GuessLanguageWithPragmas) { 22465 EXPECT_EQ(FormatStyle::LK_Cpp, 22466 guessLanguage("foo.h", "__pragma(warning(disable:))")); 22467 EXPECT_EQ(FormatStyle::LK_Cpp, 22468 guessLanguage("foo.h", "#pragma(warning(disable:))")); 22469 EXPECT_EQ(FormatStyle::LK_Cpp, 22470 guessLanguage("foo.h", "_Pragma(warning(disable:))")); 22471 } 22472 22473 TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) { 22474 // ASM symbolic names are identifiers that must be surrounded by [] without 22475 // space in between: 22476 // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands 22477 22478 // Example from https://bugs.llvm.org/show_bug.cgi?id=45108. 22479 verifyFormat(R"(// 22480 asm volatile("mrs %x[result], FPCR" : [result] "=r"(result)); 22481 )"); 22482 22483 // A list of several ASM symbolic names. 22484 verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)"); 22485 22486 // ASM symbolic names in inline ASM with inputs and outputs. 22487 verifyFormat(R"(// 22488 asm("cmoveq %1, %2, %[result]" 22489 : [result] "=r"(result) 22490 : "r"(test), "r"(new), "[result]"(old)); 22491 )"); 22492 22493 // ASM symbolic names in inline ASM with no outputs. 22494 verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)"); 22495 } 22496 22497 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) { 22498 EXPECT_EQ(FormatStyle::LK_Cpp, 22499 guessLanguage("foo.h", "void f() {\n" 22500 " asm (\"mov %[e], %[d]\"\n" 22501 " : [d] \"=rm\" (d)\n" 22502 " [e] \"rm\" (*e));\n" 22503 "}")); 22504 EXPECT_EQ(FormatStyle::LK_Cpp, 22505 guessLanguage("foo.h", "void f() {\n" 22506 " _asm (\"mov %[e], %[d]\"\n" 22507 " : [d] \"=rm\" (d)\n" 22508 " [e] \"rm\" (*e));\n" 22509 "}")); 22510 EXPECT_EQ(FormatStyle::LK_Cpp, 22511 guessLanguage("foo.h", "void f() {\n" 22512 " __asm (\"mov %[e], %[d]\"\n" 22513 " : [d] \"=rm\" (d)\n" 22514 " [e] \"rm\" (*e));\n" 22515 "}")); 22516 EXPECT_EQ(FormatStyle::LK_Cpp, 22517 guessLanguage("foo.h", "void f() {\n" 22518 " __asm__ (\"mov %[e], %[d]\"\n" 22519 " : [d] \"=rm\" (d)\n" 22520 " [e] \"rm\" (*e));\n" 22521 "}")); 22522 EXPECT_EQ(FormatStyle::LK_Cpp, 22523 guessLanguage("foo.h", "void f() {\n" 22524 " asm (\"mov %[e], %[d]\"\n" 22525 " : [d] \"=rm\" (d),\n" 22526 " [e] \"rm\" (*e));\n" 22527 "}")); 22528 EXPECT_EQ(FormatStyle::LK_Cpp, 22529 guessLanguage("foo.h", "void f() {\n" 22530 " asm volatile (\"mov %[e], %[d]\"\n" 22531 " : [d] \"=rm\" (d)\n" 22532 " [e] \"rm\" (*e));\n" 22533 "}")); 22534 } 22535 22536 TEST_F(FormatTest, GuessLanguageWithChildLines) { 22537 EXPECT_EQ(FormatStyle::LK_Cpp, 22538 guessLanguage("foo.h", "#define FOO ({ std::string s; })")); 22539 EXPECT_EQ(FormatStyle::LK_ObjC, 22540 guessLanguage("foo.h", "#define FOO ({ NSString *s; })")); 22541 EXPECT_EQ( 22542 FormatStyle::LK_Cpp, 22543 guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })")); 22544 EXPECT_EQ( 22545 FormatStyle::LK_ObjC, 22546 guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })")); 22547 } 22548 22549 TEST_F(FormatTest, TypenameMacros) { 22550 std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"}; 22551 22552 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353 22553 FormatStyle Google = getGoogleStyleWithColumns(0); 22554 Google.TypenameMacros = TypenameMacros; 22555 verifyFormat("struct foo {\n" 22556 " int bar;\n" 22557 " TAILQ_ENTRY(a) bleh;\n" 22558 "};", 22559 Google); 22560 22561 FormatStyle Macros = getLLVMStyle(); 22562 Macros.TypenameMacros = TypenameMacros; 22563 22564 verifyFormat("STACK_OF(int) a;", Macros); 22565 verifyFormat("STACK_OF(int) *a;", Macros); 22566 verifyFormat("STACK_OF(int const *) *a;", Macros); 22567 verifyFormat("STACK_OF(int *const) *a;", Macros); 22568 verifyFormat("STACK_OF(int, string) a;", Macros); 22569 verifyFormat("STACK_OF(LIST(int)) a;", Macros); 22570 verifyFormat("STACK_OF(LIST(int)) a, b;", Macros); 22571 verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros); 22572 verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros); 22573 verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros); 22574 verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros); 22575 22576 Macros.PointerAlignment = FormatStyle::PAS_Left; 22577 verifyFormat("STACK_OF(int)* a;", Macros); 22578 verifyFormat("STACK_OF(int*)* a;", Macros); 22579 verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros); 22580 verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros); 22581 verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros); 22582 } 22583 22584 TEST_F(FormatTest, AtomicQualifier) { 22585 // Check that we treate _Atomic as a type and not a function call 22586 FormatStyle Google = getGoogleStyleWithColumns(0); 22587 verifyFormat("struct foo {\n" 22588 " int a1;\n" 22589 " _Atomic(a) a2;\n" 22590 " _Atomic(_Atomic(int) *const) a3;\n" 22591 "};", 22592 Google); 22593 verifyFormat("_Atomic(uint64_t) a;"); 22594 verifyFormat("_Atomic(uint64_t) *a;"); 22595 verifyFormat("_Atomic(uint64_t const *) *a;"); 22596 verifyFormat("_Atomic(uint64_t *const) *a;"); 22597 verifyFormat("_Atomic(const uint64_t *) *a;"); 22598 verifyFormat("_Atomic(uint64_t) a;"); 22599 verifyFormat("_Atomic(_Atomic(uint64_t)) a;"); 22600 verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;"); 22601 verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}"); 22602 verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);"); 22603 22604 verifyFormat("_Atomic(uint64_t) *s(InitValue);"); 22605 verifyFormat("_Atomic(uint64_t) *s{InitValue};"); 22606 FormatStyle Style = getLLVMStyle(); 22607 Style.PointerAlignment = FormatStyle::PAS_Left; 22608 verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style); 22609 verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style); 22610 verifyFormat("_Atomic(int)* a;", Style); 22611 verifyFormat("_Atomic(int*)* a;", Style); 22612 verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style); 22613 22614 Style.SpacesInCStyleCastParentheses = true; 22615 Style.SpacesInParentheses = false; 22616 verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style); 22617 Style.SpacesInCStyleCastParentheses = false; 22618 Style.SpacesInParentheses = true; 22619 verifyFormat("x = (_Atomic( uint64_t ))*a;", Style); 22620 verifyFormat("x = (_Atomic( uint64_t ))&a;", Style); 22621 } 22622 22623 TEST_F(FormatTest, AmbersandInLamda) { 22624 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899 22625 FormatStyle AlignStyle = getLLVMStyle(); 22626 AlignStyle.PointerAlignment = FormatStyle::PAS_Left; 22627 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle); 22628 AlignStyle.PointerAlignment = FormatStyle::PAS_Right; 22629 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle); 22630 } 22631 22632 TEST_F(FormatTest, SpacesInConditionalStatement) { 22633 FormatStyle Spaces = getLLVMStyle(); 22634 Spaces.IfMacros.clear(); 22635 Spaces.IfMacros.push_back("MYIF"); 22636 Spaces.SpacesInConditionalStatement = true; 22637 verifyFormat("for ( int i = 0; i; i++ )\n continue;", Spaces); 22638 verifyFormat("if ( !a )\n return;", Spaces); 22639 verifyFormat("if ( a )\n return;", Spaces); 22640 verifyFormat("if constexpr ( a )\n return;", Spaces); 22641 verifyFormat("MYIF ( a )\n return;", Spaces); 22642 verifyFormat("MYIF ( a )\n return;\nelse MYIF ( b )\n return;", Spaces); 22643 verifyFormat("MYIF ( a )\n return;\nelse\n return;", Spaces); 22644 verifyFormat("switch ( a )\ncase 1:\n return;", Spaces); 22645 verifyFormat("while ( a )\n return;", Spaces); 22646 verifyFormat("while ( (a && b) )\n return;", Spaces); 22647 verifyFormat("do {\n} while ( 1 != 0 );", Spaces); 22648 verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces); 22649 // Check that space on the left of "::" is inserted as expected at beginning 22650 // of condition. 22651 verifyFormat("while ( ::func() )\n return;", Spaces); 22652 22653 // Check impact of ControlStatementsExceptControlMacros is honored. 22654 Spaces.SpaceBeforeParens = 22655 FormatStyle::SBPO_ControlStatementsExceptControlMacros; 22656 verifyFormat("MYIF( a )\n return;", Spaces); 22657 verifyFormat("MYIF( a )\n return;\nelse MYIF( b )\n return;", Spaces); 22658 verifyFormat("MYIF( a )\n return;\nelse\n return;", Spaces); 22659 } 22660 22661 TEST_F(FormatTest, AlternativeOperators) { 22662 // Test case for ensuring alternate operators are not 22663 // combined with their right most neighbour. 22664 verifyFormat("int a and b;"); 22665 verifyFormat("int a and_eq b;"); 22666 verifyFormat("int a bitand b;"); 22667 verifyFormat("int a bitor b;"); 22668 verifyFormat("int a compl b;"); 22669 verifyFormat("int a not b;"); 22670 verifyFormat("int a not_eq b;"); 22671 verifyFormat("int a or b;"); 22672 verifyFormat("int a xor b;"); 22673 verifyFormat("int a xor_eq b;"); 22674 verifyFormat("return this not_eq bitand other;"); 22675 verifyFormat("bool operator not_eq(const X bitand other)"); 22676 22677 verifyFormat("int a and 5;"); 22678 verifyFormat("int a and_eq 5;"); 22679 verifyFormat("int a bitand 5;"); 22680 verifyFormat("int a bitor 5;"); 22681 verifyFormat("int a compl 5;"); 22682 verifyFormat("int a not 5;"); 22683 verifyFormat("int a not_eq 5;"); 22684 verifyFormat("int a or 5;"); 22685 verifyFormat("int a xor 5;"); 22686 verifyFormat("int a xor_eq 5;"); 22687 22688 verifyFormat("int a compl(5);"); 22689 verifyFormat("int a not(5);"); 22690 22691 /* FIXME handle alternate tokens 22692 * https://en.cppreference.com/w/cpp/language/operator_alternative 22693 // alternative tokens 22694 verifyFormat("compl foo();"); // ~foo(); 22695 verifyFormat("foo() <%%>;"); // foo(); 22696 verifyFormat("void foo() <%%>;"); // void foo(){} 22697 verifyFormat("int a <:1:>;"); // int a[1];[ 22698 verifyFormat("%:define ABC abc"); // #define ABC abc 22699 verifyFormat("%:%:"); // ## 22700 */ 22701 } 22702 22703 TEST_F(FormatTest, STLWhileNotDefineChed) { 22704 verifyFormat("#if defined(while)\n" 22705 "#define while EMIT WARNING C4005\n" 22706 "#endif // while"); 22707 } 22708 22709 TEST_F(FormatTest, OperatorSpacing) { 22710 FormatStyle Style = getLLVMStyle(); 22711 Style.PointerAlignment = FormatStyle::PAS_Right; 22712 verifyFormat("Foo::operator*();", Style); 22713 verifyFormat("Foo::operator void *();", Style); 22714 verifyFormat("Foo::operator void **();", Style); 22715 verifyFormat("Foo::operator void *&();", Style); 22716 verifyFormat("Foo::operator void *&&();", Style); 22717 verifyFormat("Foo::operator void const *();", Style); 22718 verifyFormat("Foo::operator void const **();", Style); 22719 verifyFormat("Foo::operator void const *&();", Style); 22720 verifyFormat("Foo::operator void const *&&();", Style); 22721 verifyFormat("Foo::operator()(void *);", Style); 22722 verifyFormat("Foo::operator*(void *);", Style); 22723 verifyFormat("Foo::operator*();", Style); 22724 verifyFormat("Foo::operator**();", Style); 22725 verifyFormat("Foo::operator&();", Style); 22726 verifyFormat("Foo::operator<int> *();", Style); 22727 verifyFormat("Foo::operator<Foo> *();", Style); 22728 verifyFormat("Foo::operator<int> **();", Style); 22729 verifyFormat("Foo::operator<Foo> **();", Style); 22730 verifyFormat("Foo::operator<int> &();", Style); 22731 verifyFormat("Foo::operator<Foo> &();", Style); 22732 verifyFormat("Foo::operator<int> &&();", Style); 22733 verifyFormat("Foo::operator<Foo> &&();", Style); 22734 verifyFormat("Foo::operator<int> *&();", Style); 22735 verifyFormat("Foo::operator<Foo> *&();", Style); 22736 verifyFormat("Foo::operator<int> *&&();", Style); 22737 verifyFormat("Foo::operator<Foo> *&&();", Style); 22738 verifyFormat("operator*(int (*)(), class Foo);", Style); 22739 22740 verifyFormat("Foo::operator&();", Style); 22741 verifyFormat("Foo::operator void &();", Style); 22742 verifyFormat("Foo::operator void const &();", Style); 22743 verifyFormat("Foo::operator()(void &);", Style); 22744 verifyFormat("Foo::operator&(void &);", Style); 22745 verifyFormat("Foo::operator&();", Style); 22746 verifyFormat("operator&(int (&)(), class Foo);", Style); 22747 verifyFormat("operator&&(int (&)(), class Foo);", Style); 22748 22749 verifyFormat("Foo::operator&&();", Style); 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 verifyFormat("operator const nsTArrayRight<E> &()", Style); 22758 verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()", 22759 Style); 22760 verifyFormat("operator void **()", Style); 22761 verifyFormat("operator const FooRight<Object> &()", Style); 22762 verifyFormat("operator const FooRight<Object> *()", Style); 22763 verifyFormat("operator const FooRight<Object> **()", Style); 22764 verifyFormat("operator const FooRight<Object> *&()", Style); 22765 verifyFormat("operator const FooRight<Object> *&&()", Style); 22766 22767 Style.PointerAlignment = FormatStyle::PAS_Left; 22768 verifyFormat("Foo::operator*();", Style); 22769 verifyFormat("Foo::operator**();", Style); 22770 verifyFormat("Foo::operator void*();", Style); 22771 verifyFormat("Foo::operator void**();", Style); 22772 verifyFormat("Foo::operator void*&();", Style); 22773 verifyFormat("Foo::operator void*&&();", Style); 22774 verifyFormat("Foo::operator void const*();", Style); 22775 verifyFormat("Foo::operator void const**();", Style); 22776 verifyFormat("Foo::operator void const*&();", Style); 22777 verifyFormat("Foo::operator void const*&&();", Style); 22778 verifyFormat("Foo::operator/*comment*/ void*();", Style); 22779 verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style); 22780 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style); 22781 verifyFormat("Foo::operator()(void*);", Style); 22782 verifyFormat("Foo::operator*(void*);", Style); 22783 verifyFormat("Foo::operator*();", Style); 22784 verifyFormat("Foo::operator<int>*();", Style); 22785 verifyFormat("Foo::operator<Foo>*();", Style); 22786 verifyFormat("Foo::operator<int>**();", Style); 22787 verifyFormat("Foo::operator<Foo>**();", Style); 22788 verifyFormat("Foo::operator<Foo>*&();", Style); 22789 verifyFormat("Foo::operator<int>&();", Style); 22790 verifyFormat("Foo::operator<Foo>&();", Style); 22791 verifyFormat("Foo::operator<int>&&();", Style); 22792 verifyFormat("Foo::operator<Foo>&&();", Style); 22793 verifyFormat("Foo::operator<int>*&();", Style); 22794 verifyFormat("Foo::operator<Foo>*&();", Style); 22795 verifyFormat("operator*(int (*)(), class Foo);", Style); 22796 22797 verifyFormat("Foo::operator&();", Style); 22798 verifyFormat("Foo::operator void&();", Style); 22799 verifyFormat("Foo::operator void const&();", Style); 22800 verifyFormat("Foo::operator/*comment*/ void&();", Style); 22801 verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style); 22802 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style); 22803 verifyFormat("Foo::operator()(void&);", Style); 22804 verifyFormat("Foo::operator&(void&);", Style); 22805 verifyFormat("Foo::operator&();", Style); 22806 verifyFormat("operator&(int (&)(), class Foo);", Style); 22807 verifyFormat("operator&(int (&&)(), class Foo);", Style); 22808 verifyFormat("operator&&(int (&&)(), class Foo);", Style); 22809 22810 verifyFormat("Foo::operator&&();", Style); 22811 verifyFormat("Foo::operator void&&();", Style); 22812 verifyFormat("Foo::operator void const&&();", Style); 22813 verifyFormat("Foo::operator/*comment*/ void&&();", Style); 22814 verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style); 22815 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style); 22816 verifyFormat("Foo::operator()(void&&);", Style); 22817 verifyFormat("Foo::operator&&(void&&);", Style); 22818 verifyFormat("Foo::operator&&();", Style); 22819 verifyFormat("operator&&(int (&&)(), class Foo);", Style); 22820 verifyFormat("operator const nsTArrayLeft<E>&()", Style); 22821 verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()", 22822 Style); 22823 verifyFormat("operator void**()", Style); 22824 verifyFormat("operator const FooLeft<Object>&()", Style); 22825 verifyFormat("operator const FooLeft<Object>*()", Style); 22826 verifyFormat("operator const FooLeft<Object>**()", Style); 22827 verifyFormat("operator const FooLeft<Object>*&()", Style); 22828 verifyFormat("operator const FooLeft<Object>*&&()", Style); 22829 22830 // PR45107 22831 verifyFormat("operator Vector<String>&();", Style); 22832 verifyFormat("operator const Vector<String>&();", Style); 22833 verifyFormat("operator foo::Bar*();", Style); 22834 verifyFormat("operator const Foo<X>::Bar<Y>*();", Style); 22835 verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();", 22836 Style); 22837 22838 Style.PointerAlignment = FormatStyle::PAS_Middle; 22839 verifyFormat("Foo::operator*();", Style); 22840 verifyFormat("Foo::operator void *();", Style); 22841 verifyFormat("Foo::operator()(void *);", Style); 22842 verifyFormat("Foo::operator*(void *);", Style); 22843 verifyFormat("Foo::operator*();", Style); 22844 verifyFormat("operator*(int (*)(), class Foo);", Style); 22845 22846 verifyFormat("Foo::operator&();", Style); 22847 verifyFormat("Foo::operator void &();", Style); 22848 verifyFormat("Foo::operator void const &();", Style); 22849 verifyFormat("Foo::operator()(void &);", Style); 22850 verifyFormat("Foo::operator&(void &);", Style); 22851 verifyFormat("Foo::operator&();", Style); 22852 verifyFormat("operator&(int (&)(), class Foo);", Style); 22853 22854 verifyFormat("Foo::operator&&();", Style); 22855 verifyFormat("Foo::operator void &&();", Style); 22856 verifyFormat("Foo::operator void const &&();", Style); 22857 verifyFormat("Foo::operator()(void &&);", Style); 22858 verifyFormat("Foo::operator&&(void &&);", Style); 22859 verifyFormat("Foo::operator&&();", Style); 22860 verifyFormat("operator&&(int (&&)(), class Foo);", Style); 22861 } 22862 22863 TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) { 22864 FormatStyle Style = getLLVMStyle(); 22865 // PR46157 22866 verifyFormat("foo(operator+, -42);", Style); 22867 verifyFormat("foo(operator++, -42);", Style); 22868 verifyFormat("foo(operator--, -42);", Style); 22869 verifyFormat("foo(-42, operator--);", Style); 22870 verifyFormat("foo(-42, operator, );", Style); 22871 verifyFormat("foo(operator, , -42);", Style); 22872 } 22873 22874 TEST_F(FormatTest, WhitespaceSensitiveMacros) { 22875 FormatStyle Style = getLLVMStyle(); 22876 Style.WhitespaceSensitiveMacros.push_back("FOO"); 22877 22878 // Don't use the helpers here, since 'mess up' will change the whitespace 22879 // and these are all whitespace sensitive by definition 22880 EXPECT_EQ("FOO(String-ized&Messy+But(: :Still)=Intentional);", 22881 format("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style)); 22882 EXPECT_EQ( 22883 "FOO(String-ized&Messy+But\\(: :Still)=Intentional);", 22884 format("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style)); 22885 EXPECT_EQ("FOO(String-ized&Messy+But,: :Still=Intentional);", 22886 format("FOO(String-ized&Messy+But,: :Still=Intentional);", Style)); 22887 EXPECT_EQ("FOO(String-ized&Messy+But,: :\n" 22888 " Still=Intentional);", 22889 format("FOO(String-ized&Messy+But,: :\n" 22890 " Still=Intentional);", 22891 Style)); 22892 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 22893 EXPECT_EQ("FOO(String-ized=&Messy+But,: :\n" 22894 " Still=Intentional);", 22895 format("FOO(String-ized=&Messy+But,: :\n" 22896 " Still=Intentional);", 22897 Style)); 22898 22899 Style.ColumnLimit = 21; 22900 EXPECT_EQ("FOO(String-ized&Messy+But: :Still=Intentional);", 22901 format("FOO(String-ized&Messy+But: :Still=Intentional);", Style)); 22902 } 22903 22904 TEST_F(FormatTest, VeryLongNamespaceCommentSplit) { 22905 // These tests are not in NamespaceFixer because that doesn't 22906 // test its interaction with line wrapping 22907 FormatStyle Style = getLLVMStyleWithColumns(80); 22908 verifyFormat("namespace {\n" 22909 "int i;\n" 22910 "int j;\n" 22911 "} // namespace", 22912 Style); 22913 22914 verifyFormat("namespace AAA {\n" 22915 "int i;\n" 22916 "int j;\n" 22917 "} // namespace AAA", 22918 Style); 22919 22920 EXPECT_EQ("namespace Averyveryveryverylongnamespace {\n" 22921 "int i;\n" 22922 "int j;\n" 22923 "} // namespace Averyveryveryverylongnamespace", 22924 format("namespace Averyveryveryverylongnamespace {\n" 22925 "int i;\n" 22926 "int j;\n" 22927 "}", 22928 Style)); 22929 22930 EXPECT_EQ( 22931 "namespace " 22932 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" 22933 " went::mad::now {\n" 22934 "int i;\n" 22935 "int j;\n" 22936 "} // namespace\n" 22937 " // " 22938 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" 22939 "went::mad::now", 22940 format("namespace " 22941 "would::it::save::you::a::lot::of::time::if_::i::" 22942 "just::gave::up::and_::went::mad::now {\n" 22943 "int i;\n" 22944 "int j;\n" 22945 "}", 22946 Style)); 22947 22948 // This used to duplicate the comment again and again on subsequent runs 22949 EXPECT_EQ( 22950 "namespace " 22951 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" 22952 " went::mad::now {\n" 22953 "int i;\n" 22954 "int j;\n" 22955 "} // namespace\n" 22956 " // " 22957 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" 22958 "went::mad::now", 22959 format("namespace " 22960 "would::it::save::you::a::lot::of::time::if_::i::" 22961 "just::gave::up::and_::went::mad::now {\n" 22962 "int i;\n" 22963 "int j;\n" 22964 "} // namespace\n" 22965 " // " 22966 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::" 22967 "and_::went::mad::now", 22968 Style)); 22969 } 22970 22971 TEST_F(FormatTest, LikelyUnlikely) { 22972 FormatStyle Style = getLLVMStyle(); 22973 22974 verifyFormat("if (argc > 5) [[unlikely]] {\n" 22975 " return 29;\n" 22976 "}", 22977 Style); 22978 22979 verifyFormat("if (argc > 5) [[likely]] {\n" 22980 " return 29;\n" 22981 "}", 22982 Style); 22983 22984 verifyFormat("if (argc > 5) [[unlikely]] {\n" 22985 " return 29;\n" 22986 "} else [[likely]] {\n" 22987 " return 42;\n" 22988 "}\n", 22989 Style); 22990 22991 verifyFormat("if (argc > 5) [[unlikely]] {\n" 22992 " return 29;\n" 22993 "} else if (argc > 10) [[likely]] {\n" 22994 " return 99;\n" 22995 "} else {\n" 22996 " return 42;\n" 22997 "}\n", 22998 Style); 22999 23000 verifyFormat("if (argc > 5) [[gnu::unused]] {\n" 23001 " return 29;\n" 23002 "}", 23003 Style); 23004 23005 verifyFormat("if (argc > 5) [[unlikely]]\n" 23006 " return 29;\n", 23007 Style); 23008 verifyFormat("if (argc > 5) [[likely]]\n" 23009 " return 29;\n", 23010 Style); 23011 23012 Style.AttributeMacros.push_back("UNLIKELY"); 23013 Style.AttributeMacros.push_back("LIKELY"); 23014 verifyFormat("if (argc > 5) UNLIKELY\n" 23015 " return 29;\n", 23016 Style); 23017 23018 verifyFormat("if (argc > 5) UNLIKELY {\n" 23019 " return 29;\n" 23020 "}", 23021 Style); 23022 verifyFormat("if (argc > 5) UNLIKELY {\n" 23023 " return 29;\n" 23024 "} else [[likely]] {\n" 23025 " return 42;\n" 23026 "}\n", 23027 Style); 23028 verifyFormat("if (argc > 5) UNLIKELY {\n" 23029 " return 29;\n" 23030 "} else LIKELY {\n" 23031 " return 42;\n" 23032 "}\n", 23033 Style); 23034 verifyFormat("if (argc > 5) [[unlikely]] {\n" 23035 " return 29;\n" 23036 "} else LIKELY {\n" 23037 " return 42;\n" 23038 "}\n", 23039 Style); 23040 } 23041 23042 TEST_F(FormatTest, PenaltyIndentedWhitespace) { 23043 verifyFormat("Constructor()\n" 23044 " : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 23045 " aaaa(aaaaaaaaaaaaaaaaaa, " 23046 "aaaaaaaaaaaaaaaaaat))"); 23047 verifyFormat("Constructor()\n" 23048 " : aaaaaaaaaaaaa(aaaaaa), " 23049 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)"); 23050 23051 FormatStyle StyleWithWhitespacePenalty = getLLVMStyle(); 23052 StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5; 23053 verifyFormat("Constructor()\n" 23054 " : aaaaaa(aaaaaa),\n" 23055 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 23056 " aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))", 23057 StyleWithWhitespacePenalty); 23058 verifyFormat("Constructor()\n" 23059 " : aaaaaaaaaaaaa(aaaaaa), " 23060 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)", 23061 StyleWithWhitespacePenalty); 23062 } 23063 23064 TEST_F(FormatTest, LLVMDefaultStyle) { 23065 FormatStyle Style = getLLVMStyle(); 23066 verifyFormat("extern \"C\" {\n" 23067 "int foo();\n" 23068 "}", 23069 Style); 23070 } 23071 TEST_F(FormatTest, GNUDefaultStyle) { 23072 FormatStyle Style = getGNUStyle(); 23073 verifyFormat("extern \"C\"\n" 23074 "{\n" 23075 " int foo ();\n" 23076 "}", 23077 Style); 23078 } 23079 TEST_F(FormatTest, MozillaDefaultStyle) { 23080 FormatStyle Style = getMozillaStyle(); 23081 verifyFormat("extern \"C\"\n" 23082 "{\n" 23083 " int foo();\n" 23084 "}", 23085 Style); 23086 } 23087 TEST_F(FormatTest, GoogleDefaultStyle) { 23088 FormatStyle Style = getGoogleStyle(); 23089 verifyFormat("extern \"C\" {\n" 23090 "int foo();\n" 23091 "}", 23092 Style); 23093 } 23094 TEST_F(FormatTest, ChromiumDefaultStyle) { 23095 FormatStyle Style = getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp); 23096 verifyFormat("extern \"C\" {\n" 23097 "int foo();\n" 23098 "}", 23099 Style); 23100 } 23101 TEST_F(FormatTest, MicrosoftDefaultStyle) { 23102 FormatStyle Style = getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp); 23103 verifyFormat("extern \"C\"\n" 23104 "{\n" 23105 " int foo();\n" 23106 "}", 23107 Style); 23108 } 23109 TEST_F(FormatTest, WebKitDefaultStyle) { 23110 FormatStyle Style = getWebKitStyle(); 23111 verifyFormat("extern \"C\" {\n" 23112 "int foo();\n" 23113 "}", 23114 Style); 23115 } 23116 23117 TEST_F(FormatTest, ConceptsAndRequires) { 23118 FormatStyle Style = getLLVMStyle(); 23119 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 23120 23121 verifyFormat("template <typename T>\n" 23122 "concept Hashable = requires(T a) {\n" 23123 " { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n" 23124 "};", 23125 Style); 23126 verifyFormat("template <typename T>\n" 23127 "concept EqualityComparable = requires(T a, T b) {\n" 23128 " { a == b } -> bool;\n" 23129 "};", 23130 Style); 23131 verifyFormat("template <typename T>\n" 23132 "concept EqualityComparable = requires(T a, T b) {\n" 23133 " { a == b } -> bool;\n" 23134 " { a != b } -> bool;\n" 23135 "};", 23136 Style); 23137 verifyFormat("template <typename T>\n" 23138 "concept EqualityComparable = requires(T a, T b) {\n" 23139 " { a == b } -> bool;\n" 23140 " { a != b } -> bool;\n" 23141 "};", 23142 Style); 23143 23144 verifyFormat("template <typename It>\n" 23145 "requires Iterator<It>\n" 23146 "void sort(It begin, It end) {\n" 23147 " //....\n" 23148 "}", 23149 Style); 23150 23151 verifyFormat("template <typename T>\n" 23152 "concept Large = sizeof(T) > 10;", 23153 Style); 23154 23155 verifyFormat("template <typename T, typename U>\n" 23156 "concept FooableWith = requires(T t, U u) {\n" 23157 " typename T::foo_type;\n" 23158 " { t.foo(u) } -> typename T::foo_type;\n" 23159 " t++;\n" 23160 "};\n" 23161 "void doFoo(FooableWith<int> auto t) {\n" 23162 " t.foo(3);\n" 23163 "}", 23164 Style); 23165 verifyFormat("template <typename T>\n" 23166 "concept Context = sizeof(T) == 1;", 23167 Style); 23168 verifyFormat("template <typename T>\n" 23169 "concept Context = is_specialization_of_v<context, T>;", 23170 Style); 23171 verifyFormat("template <typename T>\n" 23172 "concept Node = std::is_object_v<T>;", 23173 Style); 23174 verifyFormat("template <typename T>\n" 23175 "concept Tree = true;", 23176 Style); 23177 23178 verifyFormat("template <typename T> int g(T i) requires Concept1<I> {\n" 23179 " //...\n" 23180 "}", 23181 Style); 23182 23183 verifyFormat( 23184 "template <typename T> int g(T i) requires Concept1<I> && Concept2<I> {\n" 23185 " //...\n" 23186 "}", 23187 Style); 23188 23189 verifyFormat( 23190 "template <typename T> int g(T i) requires Concept1<I> || Concept2<I> {\n" 23191 " //...\n" 23192 "}", 23193 Style); 23194 23195 verifyFormat("template <typename T>\n" 23196 "veryveryvery_long_return_type g(T i) requires Concept1<I> || " 23197 "Concept2<I> {\n" 23198 " //...\n" 23199 "}", 23200 Style); 23201 23202 verifyFormat("template <typename T>\n" 23203 "veryveryvery_long_return_type g(T i) requires Concept1<I> && " 23204 "Concept2<I> {\n" 23205 " //...\n" 23206 "}", 23207 Style); 23208 23209 verifyFormat( 23210 "template <typename T>\n" 23211 "veryveryvery_long_return_type g(T i) requires Concept1 && Concept2 {\n" 23212 " //...\n" 23213 "}", 23214 Style); 23215 23216 verifyFormat( 23217 "template <typename T>\n" 23218 "veryveryvery_long_return_type g(T i) requires Concept1 || Concept2 {\n" 23219 " //...\n" 23220 "}", 23221 Style); 23222 23223 verifyFormat("template <typename It>\n" 23224 "requires Foo<It>() && Bar<It> {\n" 23225 " //....\n" 23226 "}", 23227 Style); 23228 23229 verifyFormat("template <typename It>\n" 23230 "requires Foo<Bar<It>>() && Bar<Foo<It, It>> {\n" 23231 " //....\n" 23232 "}", 23233 Style); 23234 23235 verifyFormat("template <typename It>\n" 23236 "requires Foo<Bar<It, It>>() && Bar<Foo<It, It>> {\n" 23237 " //....\n" 23238 "}", 23239 Style); 23240 23241 verifyFormat( 23242 "template <typename It>\n" 23243 "requires Foo<Bar<It>, Baz<It>>() && Bar<Foo<It>, Baz<It, It>> {\n" 23244 " //....\n" 23245 "}", 23246 Style); 23247 23248 Style.IndentRequires = true; 23249 verifyFormat("template <typename It>\n" 23250 " requires Iterator<It>\n" 23251 "void sort(It begin, It end) {\n" 23252 " //....\n" 23253 "}", 23254 Style); 23255 verifyFormat("template <std::size index_>\n" 23256 " requires(index_ < sizeof...(Children_))\n" 23257 "Tree auto &child() {\n" 23258 " // ...\n" 23259 "}", 23260 Style); 23261 23262 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 23263 verifyFormat("template <typename T>\n" 23264 "concept Hashable = requires (T a) {\n" 23265 " { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n" 23266 "};", 23267 Style); 23268 23269 verifyFormat("template <class T = void>\n" 23270 " requires EqualityComparable<T> || Same<T, void>\n" 23271 "struct equal_to;", 23272 Style); 23273 23274 verifyFormat("template <class T>\n" 23275 " requires requires {\n" 23276 " T{};\n" 23277 " T (int);\n" 23278 " }\n", 23279 Style); 23280 23281 Style.ColumnLimit = 78; 23282 verifyFormat("template <typename T>\n" 23283 "concept Context = Traits<typename T::traits_type> and\n" 23284 " Interface<typename T::interface_type> and\n" 23285 " Request<typename T::request_type> and\n" 23286 " Response<typename T::response_type> and\n" 23287 " ContextExtension<typename T::extension_type> and\n" 23288 " ::std::is_copy_constructable<T> and " 23289 "::std::is_move_constructable<T> and\n" 23290 " requires (T c) {\n" 23291 " { c.response; } -> Response;\n" 23292 "} and requires (T c) {\n" 23293 " { c.request; } -> Request;\n" 23294 "}\n", 23295 Style); 23296 23297 verifyFormat("template <typename T>\n" 23298 "concept Context = Traits<typename T::traits_type> or\n" 23299 " Interface<typename T::interface_type> or\n" 23300 " Request<typename T::request_type> or\n" 23301 " Response<typename T::response_type> or\n" 23302 " ContextExtension<typename T::extension_type> or\n" 23303 " ::std::is_copy_constructable<T> or " 23304 "::std::is_move_constructable<T> or\n" 23305 " requires (T c) {\n" 23306 " { c.response; } -> Response;\n" 23307 "} or requires (T c) {\n" 23308 " { c.request; } -> Request;\n" 23309 "}\n", 23310 Style); 23311 23312 verifyFormat("template <typename T>\n" 23313 "concept Context = Traits<typename T::traits_type> &&\n" 23314 " Interface<typename T::interface_type> &&\n" 23315 " Request<typename T::request_type> &&\n" 23316 " Response<typename T::response_type> &&\n" 23317 " ContextExtension<typename T::extension_type> &&\n" 23318 " ::std::is_copy_constructable<T> && " 23319 "::std::is_move_constructable<T> &&\n" 23320 " requires (T c) {\n" 23321 " { c.response; } -> Response;\n" 23322 "} && requires (T c) {\n" 23323 " { c.request; } -> Request;\n" 23324 "}\n", 23325 Style); 23326 23327 verifyFormat("template <typename T>\nconcept someConcept = Constraint1<T> && " 23328 "Constraint2<T>;"); 23329 23330 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 23331 Style.BraceWrapping.AfterFunction = true; 23332 Style.BraceWrapping.AfterClass = true; 23333 Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 23334 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 23335 verifyFormat("void Foo () requires (std::copyable<T>)\n" 23336 "{\n" 23337 " return\n" 23338 "}\n", 23339 Style); 23340 23341 verifyFormat("void Foo () requires std::copyable<T>\n" 23342 "{\n" 23343 " return\n" 23344 "}\n", 23345 Style); 23346 23347 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 23348 " requires (std::invocable<F, std::invoke_result_t<Args>...>)\n" 23349 "struct constant;", 23350 Style); 23351 23352 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 23353 " requires std::invocable<F, std::invoke_result_t<Args>...>\n" 23354 "struct constant;", 23355 Style); 23356 23357 verifyFormat("template <class T>\n" 23358 "class plane_with_very_very_very_long_name\n" 23359 "{\n" 23360 " constexpr plane_with_very_very_very_long_name () requires " 23361 "std::copyable<T>\n" 23362 " : plane_with_very_very_very_long_name (1)\n" 23363 " {\n" 23364 " }\n" 23365 "}\n", 23366 Style); 23367 23368 verifyFormat("template <class T>\n" 23369 "class plane_with_long_name\n" 23370 "{\n" 23371 " constexpr plane_with_long_name () requires std::copyable<T>\n" 23372 " : plane_with_long_name (1)\n" 23373 " {\n" 23374 " }\n" 23375 "}\n", 23376 Style); 23377 23378 Style.BreakBeforeConceptDeclarations = false; 23379 verifyFormat("template <typename T> concept Tree = true;", Style); 23380 23381 Style.IndentRequires = false; 23382 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 23383 "requires (std::invocable<F, std::invoke_result_t<Args>...>) " 23384 "struct constant;", 23385 Style); 23386 } 23387 23388 TEST_F(FormatTest, StatementAttributeLikeMacros) { 23389 FormatStyle Style = getLLVMStyle(); 23390 StringRef Source = "void Foo::slot() {\n" 23391 " unsigned char MyChar = 'x';\n" 23392 " emit signal(MyChar);\n" 23393 " Q_EMIT signal(MyChar);\n" 23394 "}"; 23395 23396 EXPECT_EQ(Source, format(Source, Style)); 23397 23398 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 23399 EXPECT_EQ("void Foo::slot() {\n" 23400 " unsigned char MyChar = 'x';\n" 23401 " emit signal(MyChar);\n" 23402 " Q_EMIT signal(MyChar);\n" 23403 "}", 23404 format(Source, Style)); 23405 23406 Style.StatementAttributeLikeMacros.push_back("emit"); 23407 EXPECT_EQ(Source, format(Source, Style)); 23408 23409 Style.StatementAttributeLikeMacros = {}; 23410 EXPECT_EQ("void Foo::slot() {\n" 23411 " unsigned char MyChar = 'x';\n" 23412 " emit signal(MyChar);\n" 23413 " Q_EMIT signal(MyChar);\n" 23414 "}", 23415 format(Source, Style)); 23416 } 23417 23418 TEST_F(FormatTest, IndentAccessModifiers) { 23419 FormatStyle Style = getLLVMStyle(); 23420 Style.IndentAccessModifiers = true; 23421 // Members are *two* levels below the record; 23422 // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation. 23423 verifyFormat("class C {\n" 23424 " int i;\n" 23425 "};\n", 23426 Style); 23427 verifyFormat("union C {\n" 23428 " int i;\n" 23429 " unsigned u;\n" 23430 "};\n", 23431 Style); 23432 // Access modifiers should be indented one level below the record. 23433 verifyFormat("class C {\n" 23434 " public:\n" 23435 " int i;\n" 23436 "};\n", 23437 Style); 23438 verifyFormat("struct S {\n" 23439 " private:\n" 23440 " class C {\n" 23441 " int j;\n" 23442 "\n" 23443 " public:\n" 23444 " C();\n" 23445 " };\n" 23446 "\n" 23447 " public:\n" 23448 " int i;\n" 23449 "};\n", 23450 Style); 23451 // Enumerations are not records and should be unaffected. 23452 Style.AllowShortEnumsOnASingleLine = false; 23453 verifyFormat("enum class E {\n" 23454 " A,\n" 23455 " B\n" 23456 "};\n", 23457 Style); 23458 // Test with a different indentation width; 23459 // also proves that the result is Style.AccessModifierOffset agnostic. 23460 Style.IndentWidth = 3; 23461 verifyFormat("class C {\n" 23462 " public:\n" 23463 " int i;\n" 23464 "};\n", 23465 Style); 23466 } 23467 23468 TEST_F(FormatTest, LimitlessStringsAndComments) { 23469 auto Style = getLLVMStyleWithColumns(0); 23470 constexpr StringRef Code = 23471 "/**\n" 23472 " * This is a multiline comment with quite some long lines, at least for " 23473 "the LLVM Style.\n" 23474 " * We will redo this with strings and line comments. Just to check if " 23475 "everything is working.\n" 23476 " */\n" 23477 "bool foo() {\n" 23478 " /* Single line multi line comment. */\n" 23479 " const std::string String = \"This is a multiline string with quite " 23480 "some long lines, at least for the LLVM Style.\"\n" 23481 " \"We already did it with multi line " 23482 "comments, and we will do it with line comments. Just to check if " 23483 "everything is working.\";\n" 23484 " // This is a line comment (block) with quite some long lines, at " 23485 "least for the LLVM Style.\n" 23486 " // We already did this with multi line comments and strings. Just to " 23487 "check if everything is working.\n" 23488 " const std::string SmallString = \"Hello World\";\n" 23489 " // Small line comment\n" 23490 " return String.size() > SmallString.size();\n" 23491 "}"; 23492 EXPECT_EQ(Code, format(Code, Style)); 23493 } 23494 23495 TEST_F(FormatTest, FormatDecayCopy) { 23496 // error cases from unit tests 23497 verifyFormat("foo(auto())"); 23498 verifyFormat("foo(auto{})"); 23499 verifyFormat("foo(auto({}))"); 23500 verifyFormat("foo(auto{{}})"); 23501 23502 verifyFormat("foo(auto(1))"); 23503 verifyFormat("foo(auto{1})"); 23504 verifyFormat("foo(new auto(1))"); 23505 verifyFormat("foo(new auto{1})"); 23506 verifyFormat("decltype(auto(1)) x;"); 23507 verifyFormat("decltype(auto{1}) x;"); 23508 verifyFormat("auto(x);"); 23509 verifyFormat("auto{x};"); 23510 verifyFormat("new auto{x};"); 23511 verifyFormat("auto{x} = y;"); 23512 verifyFormat("auto(x) = y;"); // actually a declaration, but this is clearly 23513 // the user's own fault 23514 verifyFormat("integral auto(x) = y;"); // actually a declaration, but this is 23515 // clearly the user's own fault 23516 verifyFormat("auto(*p)() = f;"); // actually a declaration; TODO FIXME 23517 } 23518 23519 TEST_F(FormatTest, Cpp20ModulesSupport) { 23520 FormatStyle Style = getLLVMStyle(); 23521 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 23522 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 23523 23524 verifyFormat("export import foo;", Style); 23525 verifyFormat("export import foo:bar;", Style); 23526 verifyFormat("export import foo.bar;", Style); 23527 verifyFormat("export import foo.bar:baz;", Style); 23528 verifyFormat("export import :bar;", Style); 23529 verifyFormat("export module foo:bar;", Style); 23530 verifyFormat("export module foo;", Style); 23531 verifyFormat("export module foo.bar;", Style); 23532 verifyFormat("export module foo.bar:baz;", Style); 23533 verifyFormat("export import <string_view>;", Style); 23534 23535 verifyFormat("export type_name var;", Style); 23536 verifyFormat("template <class T> export using A = B<T>;", Style); 23537 verifyFormat("export using A = B;", Style); 23538 verifyFormat("export int func() {\n" 23539 " foo();\n" 23540 "}", 23541 Style); 23542 verifyFormat("export struct {\n" 23543 " int foo;\n" 23544 "};", 23545 Style); 23546 verifyFormat("export {\n" 23547 " int foo;\n" 23548 "};", 23549 Style); 23550 verifyFormat("export export char const *hello() { return \"hello\"; }"); 23551 23552 verifyFormat("import bar;", Style); 23553 verifyFormat("import foo.bar;", Style); 23554 verifyFormat("import foo:bar;", Style); 23555 verifyFormat("import :bar;", Style); 23556 verifyFormat("import <ctime>;", Style); 23557 verifyFormat("import \"header\";", Style); 23558 23559 verifyFormat("module foo;", Style); 23560 verifyFormat("module foo:bar;", Style); 23561 verifyFormat("module foo.bar;", Style); 23562 verifyFormat("module;", Style); 23563 23564 verifyFormat("export namespace hi {\n" 23565 "const char *sayhi();\n" 23566 "}", 23567 Style); 23568 23569 verifyFormat("module :private;", Style); 23570 verifyFormat("import <foo/bar.h>;", Style); 23571 verifyFormat("import foo...bar;", Style); 23572 verifyFormat("import ..........;", Style); 23573 verifyFormat("module foo:private;", Style); 23574 verifyFormat("import a", Style); 23575 verifyFormat("module a", Style); 23576 verifyFormat("export import a", Style); 23577 verifyFormat("export module a", Style); 23578 23579 verifyFormat("import", Style); 23580 verifyFormat("module", Style); 23581 verifyFormat("export", Style); 23582 } 23583 23584 TEST_F(FormatTest, CoroutineForCoawait) { 23585 FormatStyle Style = getLLVMStyle(); 23586 verifyFormat("for co_await (auto x : range())\n ;"); 23587 verifyFormat("for (auto i : arr) {\n" 23588 "}", 23589 Style); 23590 verifyFormat("for co_await (auto i : arr) {\n" 23591 "}", 23592 Style); 23593 verifyFormat("for co_await (auto i : foo(T{})) {\n" 23594 "}", 23595 Style); 23596 } 23597 23598 TEST_F(FormatTest, CoroutineCoAwait) { 23599 verifyFormat("int x = co_await foo();"); 23600 verifyFormat("int x = (co_await foo());"); 23601 verifyFormat("co_await (42);"); 23602 verifyFormat("void operator co_await(int);"); 23603 verifyFormat("void operator co_await(a);"); 23604 verifyFormat("co_await a;"); 23605 verifyFormat("co_await missing_await_resume{};"); 23606 verifyFormat("co_await a; // comment"); 23607 verifyFormat("void test0() { co_await a; }"); 23608 verifyFormat("co_await co_await co_await foo();"); 23609 verifyFormat("co_await foo().bar();"); 23610 verifyFormat("co_await [this]() -> Task { co_return x; }"); 23611 verifyFormat("co_await [this](int a, int b) -> Task { co_return co_await " 23612 "foo(); }(x, y);"); 23613 23614 FormatStyle Style = getLLVMStyleWithColumns(40); 23615 verifyFormat("co_await [this](int a, int b) -> Task {\n" 23616 " co_return co_await foo();\n" 23617 "}(x, y);", 23618 Style); 23619 verifyFormat("co_await;"); 23620 } 23621 23622 TEST_F(FormatTest, CoroutineCoYield) { 23623 verifyFormat("int x = co_yield foo();"); 23624 verifyFormat("int x = (co_yield foo());"); 23625 verifyFormat("co_yield (42);"); 23626 verifyFormat("co_yield {42};"); 23627 verifyFormat("co_yield 42;"); 23628 verifyFormat("co_yield n++;"); 23629 verifyFormat("co_yield ++n;"); 23630 verifyFormat("co_yield;"); 23631 } 23632 23633 TEST_F(FormatTest, CoroutineCoReturn) { 23634 verifyFormat("co_return (42);"); 23635 verifyFormat("co_return;"); 23636 verifyFormat("co_return {};"); 23637 verifyFormat("co_return x;"); 23638 verifyFormat("co_return co_await foo();"); 23639 verifyFormat("co_return co_yield foo();"); 23640 } 23641 23642 TEST_F(FormatTest, EmptyShortBlock) { 23643 auto Style = getLLVMStyle(); 23644 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 23645 23646 verifyFormat("try {\n" 23647 " doA();\n" 23648 "} catch (Exception &e) {\n" 23649 " e.printStackTrace();\n" 23650 "}\n", 23651 Style); 23652 23653 verifyFormat("try {\n" 23654 " doA();\n" 23655 "} catch (Exception &e) {}\n", 23656 Style); 23657 } 23658 23659 TEST_F(FormatTest, ShortTemplatedArgumentLists) { 23660 auto Style = getLLVMStyle(); 23661 23662 verifyFormat("template <> struct S : Template<int (*)[]> {};\n", Style); 23663 verifyFormat("template <> struct S : Template<int (*)[10]> {};\n", Style); 23664 verifyFormat("struct Y : X<[] { return 0; }> {};", Style); 23665 verifyFormat("struct Y<[] { return 0; }> {};", Style); 23666 23667 verifyFormat("struct Z : X<decltype([] { return 0; }){}> {};", Style); 23668 } 23669 23670 TEST_F(FormatTest, RemoveBraces) { 23671 FormatStyle Style = getLLVMStyle(); 23672 Style.RemoveBracesLLVM = true; 23673 23674 // The following eight test cases are fully-braced versions of the examples at 23675 // "llvm.org/docs/CodingStandards.html#don-t-use-braces-on-simple-single- 23676 // statement-bodies-of-if-else-loop-statements". 23677 23678 // 1. Omit the braces, since the body is simple and clearly associated with 23679 // the if. 23680 verifyFormat("if (isa<FunctionDecl>(D))\n" 23681 " handleFunctionDecl(D);\n" 23682 "else if (isa<VarDecl>(D))\n" 23683 " handleVarDecl(D);", 23684 "if (isa<FunctionDecl>(D)) {\n" 23685 " handleFunctionDecl(D);\n" 23686 "} else if (isa<VarDecl>(D)) {\n" 23687 " handleVarDecl(D);\n" 23688 "}", 23689 Style); 23690 23691 // 2. Here we document the condition itself and not the body. 23692 verifyFormat("if (isa<VarDecl>(D)) {\n" 23693 " // It is necessary that we explain the situation with this\n" 23694 " // surprisingly long comment, so it would be unclear\n" 23695 " // without the braces whether the following statement is in\n" 23696 " // the scope of the `if`.\n" 23697 " // Because the condition is documented, we can't really\n" 23698 " // hoist this comment that applies to the body above the\n" 23699 " // if.\n" 23700 " handleOtherDecl(D);\n" 23701 "}", 23702 Style); 23703 23704 // 3. Use braces on the outer `if` to avoid a potential dangling else 23705 // situation. 23706 verifyFormat("if (isa<VarDecl>(D)) {\n" 23707 " for (auto *A : D.attrs())\n" 23708 " if (shouldProcessAttr(A))\n" 23709 " handleAttr(A);\n" 23710 "}", 23711 "if (isa<VarDecl>(D)) {\n" 23712 " for (auto *A : D.attrs()) {\n" 23713 " if (shouldProcessAttr(A)) {\n" 23714 " handleAttr(A);\n" 23715 " }\n" 23716 " }\n" 23717 "}", 23718 Style); 23719 23720 // 4. Use braces for the `if` block to keep it uniform with the else block. 23721 verifyFormat("if (isa<FunctionDecl>(D)) {\n" 23722 " handleFunctionDecl(D);\n" 23723 "} else {\n" 23724 " // In this else case, it is necessary that we explain the\n" 23725 " // situation with this surprisingly long comment, so it\n" 23726 " // would be unclear without the braces whether the\n" 23727 " // following statement is in the scope of the `if`.\n" 23728 " handleOtherDecl(D);\n" 23729 "}", 23730 Style); 23731 23732 // 5. This should also omit braces. The `for` loop contains only a single 23733 // statement, so it shouldn't have braces. The `if` also only contains a 23734 // single simple statement (the for loop), so it also should omit braces. 23735 verifyFormat("if (isa<FunctionDecl>(D))\n" 23736 " for (auto *A : D.attrs())\n" 23737 " handleAttr(A);", 23738 "if (isa<FunctionDecl>(D)) {\n" 23739 " for (auto *A : D.attrs()) {\n" 23740 " handleAttr(A);\n" 23741 " }\n" 23742 "}", 23743 Style); 23744 23745 // 6. Use braces for the outer `if` since the nested `for` is braced. 23746 verifyFormat("if (isa<FunctionDecl>(D)) {\n" 23747 " for (auto *A : D.attrs()) {\n" 23748 " // In this for loop body, it is necessary that we explain\n" 23749 " // the situation with this surprisingly long comment,\n" 23750 " // forcing braces on the `for` block.\n" 23751 " handleAttr(A);\n" 23752 " }\n" 23753 "}", 23754 Style); 23755 23756 // 7. Use braces on the outer block because there are more than two levels of 23757 // nesting. 23758 verifyFormat("if (isa<FunctionDecl>(D)) {\n" 23759 " for (auto *A : D.attrs())\n" 23760 " for (ssize_t i : llvm::seq<ssize_t>(count))\n" 23761 " handleAttrOnDecl(D, A, i);\n" 23762 "}", 23763 "if (isa<FunctionDecl>(D)) {\n" 23764 " for (auto *A : D.attrs()) {\n" 23765 " for (ssize_t i : llvm::seq<ssize_t>(count)) {\n" 23766 " handleAttrOnDecl(D, A, i);\n" 23767 " }\n" 23768 " }\n" 23769 "}", 23770 Style); 23771 23772 // 8. Use braces on the outer block because of a nested `if`, otherwise the 23773 // compiler would warn: `add explicit braces to avoid dangling else` 23774 verifyFormat("if (auto *D = dyn_cast<FunctionDecl>(D)) {\n" 23775 " if (shouldProcess(D))\n" 23776 " handleVarDecl(D);\n" 23777 " else\n" 23778 " markAsIgnored(D);\n" 23779 "}", 23780 "if (auto *D = dyn_cast<FunctionDecl>(D)) {\n" 23781 " if (shouldProcess(D)) {\n" 23782 " handleVarDecl(D);\n" 23783 " } else {\n" 23784 " markAsIgnored(D);\n" 23785 " }\n" 23786 "}", 23787 Style); 23788 23789 verifyFormat("if (a)\n" 23790 " b; // comment\n" 23791 "else if (c)\n" 23792 " d; /* comment */\n" 23793 "else\n" 23794 " e;", 23795 "if (a) {\n" 23796 " b; // comment\n" 23797 "} else if (c) {\n" 23798 " d; /* comment */\n" 23799 "} else {\n" 23800 " e;\n" 23801 "}", 23802 Style); 23803 23804 verifyFormat("if (a) {\n" 23805 " b;\n" 23806 " c;\n" 23807 "} else if (d) {\n" 23808 " e;\n" 23809 "}", 23810 Style); 23811 23812 verifyFormat("if (a) {\n" 23813 "#undef NDEBUG\n" 23814 " b;\n" 23815 "} else {\n" 23816 " c;\n" 23817 "}", 23818 Style); 23819 23820 verifyFormat("if (a) {\n" 23821 " // comment\n" 23822 "} else if (b) {\n" 23823 " c;\n" 23824 "}", 23825 Style); 23826 23827 verifyFormat("if (a) {\n" 23828 " b;\n" 23829 "} else {\n" 23830 " { c; }\n" 23831 "}", 23832 Style); 23833 23834 verifyFormat("if (a) {\n" 23835 " if (b) // comment\n" 23836 " c;\n" 23837 "} else if (d) {\n" 23838 " e;\n" 23839 "}", 23840 "if (a) {\n" 23841 " if (b) { // comment\n" 23842 " c;\n" 23843 " }\n" 23844 "} else if (d) {\n" 23845 " e;\n" 23846 "}", 23847 Style); 23848 23849 verifyFormat("if (a) {\n" 23850 " if (b) {\n" 23851 " c;\n" 23852 " // comment\n" 23853 " } else if (d) {\n" 23854 " e;\n" 23855 " }\n" 23856 "}", 23857 Style); 23858 23859 verifyFormat("if (a) {\n" 23860 " if (b)\n" 23861 " c;\n" 23862 "}", 23863 "if (a) {\n" 23864 " if (b) {\n" 23865 " c;\n" 23866 " }\n" 23867 "}", 23868 Style); 23869 23870 verifyFormat("if (a)\n" 23871 " if (b)\n" 23872 " c;\n" 23873 " else\n" 23874 " d;\n" 23875 "else\n" 23876 " e;", 23877 "if (a) {\n" 23878 " if (b) {\n" 23879 " c;\n" 23880 " } else {\n" 23881 " d;\n" 23882 " }\n" 23883 "} else {\n" 23884 " e;\n" 23885 "}", 23886 Style); 23887 23888 verifyFormat("if (a) {\n" 23889 " // comment\n" 23890 " if (b)\n" 23891 " c;\n" 23892 " else if (d)\n" 23893 " e;\n" 23894 "} else {\n" 23895 " g;\n" 23896 "}", 23897 "if (a) {\n" 23898 " // comment\n" 23899 " if (b) {\n" 23900 " c;\n" 23901 " } else if (d) {\n" 23902 " e;\n" 23903 " }\n" 23904 "} else {\n" 23905 " g;\n" 23906 "}", 23907 Style); 23908 23909 verifyFormat("if (a)\n" 23910 " b;\n" 23911 "else if (c)\n" 23912 " d;\n" 23913 "else\n" 23914 " e;", 23915 "if (a) {\n" 23916 " b;\n" 23917 "} else {\n" 23918 " if (c) {\n" 23919 " d;\n" 23920 " } else {\n" 23921 " e;\n" 23922 " }\n" 23923 "}", 23924 Style); 23925 23926 verifyFormat("if (a) {\n" 23927 " if (b)\n" 23928 " c;\n" 23929 " else if (d)\n" 23930 " e;\n" 23931 "} else {\n" 23932 " g;\n" 23933 "}", 23934 "if (a) {\n" 23935 " if (b)\n" 23936 " c;\n" 23937 " else {\n" 23938 " if (d)\n" 23939 " e;\n" 23940 " }\n" 23941 "} else {\n" 23942 " g;\n" 23943 "}", 23944 Style); 23945 23946 verifyFormat("if (a)\n" 23947 " b;\n" 23948 "else if (c)\n" 23949 " while (d)\n" 23950 " e;\n" 23951 "// comment", 23952 "if (a)\n" 23953 "{\n" 23954 " b;\n" 23955 "} else if (c) {\n" 23956 " while (d) {\n" 23957 " e;\n" 23958 " }\n" 23959 "}\n" 23960 "// comment", 23961 Style); 23962 23963 verifyFormat("if (a) {\n" 23964 " b;\n" 23965 "} else if (c) {\n" 23966 " d;\n" 23967 "} else {\n" 23968 " e;\n" 23969 " g;\n" 23970 "}", 23971 Style); 23972 23973 verifyFormat("if (a) {\n" 23974 " b;\n" 23975 "} else if (c) {\n" 23976 " d;\n" 23977 "} else {\n" 23978 " e;\n" 23979 "} // comment", 23980 Style); 23981 23982 verifyFormat("int abs = [](int i) {\n" 23983 " if (i >= 0)\n" 23984 " return i;\n" 23985 " return -i;\n" 23986 "};", 23987 "int abs = [](int i) {\n" 23988 " if (i >= 0) {\n" 23989 " return i;\n" 23990 " }\n" 23991 " return -i;\n" 23992 "};", 23993 Style); 23994 23995 // FIXME: See https://github.com/llvm/llvm-project/issues/53543. 23996 #if 0 23997 Style.ColumnLimit = 65; 23998 23999 verifyFormat("if (condition) {\n" 24000 " ff(Indices,\n" 24001 " [&](unsigned LHSI, unsigned RHSI) { return true; });\n" 24002 "} else {\n" 24003 " ff(Indices,\n" 24004 " [&](unsigned LHSI, unsigned RHSI) { return true; });\n" 24005 "}", 24006 Style); 24007 24008 Style.ColumnLimit = 20; 24009 24010 verifyFormat("if (a) {\n" 24011 " b = c + // 1 -\n" 24012 " d;\n" 24013 "}", 24014 Style); 24015 24016 verifyFormat("if (a) {\n" 24017 " b = c >= 0 ? d\n" 24018 " : e;\n" 24019 "}", 24020 "if (a) {\n" 24021 " b = c >= 0 ? d : e;\n" 24022 "}", 24023 Style); 24024 #endif 24025 24026 Style.ColumnLimit = 20; 24027 24028 verifyFormat("if (a)\n" 24029 " b = c > 0 ? d : e;", 24030 "if (a) {\n" 24031 " b = c > 0 ? d : e;\n" 24032 "}", 24033 Style); 24034 24035 Style.ColumnLimit = 0; 24036 24037 verifyFormat("if (a)\n" 24038 " b234567890223456789032345678904234567890 = " 24039 "c234567890223456789032345678904234567890;", 24040 "if (a) {\n" 24041 " b234567890223456789032345678904234567890 = " 24042 "c234567890223456789032345678904234567890;\n" 24043 "}", 24044 Style); 24045 } 24046 24047 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndent) { 24048 auto Style = getLLVMStyle(); 24049 24050 StringRef Short = "functionCall(paramA, paramB, paramC);\n" 24051 "void functionDecl(int a, int b, int c);"; 24052 24053 StringRef Medium = "functionCall(paramA, paramB, paramC, paramD, paramE, " 24054 "paramF, paramG, paramH, paramI);\n" 24055 "void functionDecl(int argumentA, int argumentB, int " 24056 "argumentC, int argumentD, int argumentE);"; 24057 24058 verifyFormat(Short, Style); 24059 24060 StringRef NoBreak = "functionCall(paramA, paramB, paramC, paramD, paramE, " 24061 "paramF, paramG, paramH,\n" 24062 " paramI);\n" 24063 "void functionDecl(int argumentA, int argumentB, int " 24064 "argumentC, int argumentD,\n" 24065 " int argumentE);"; 24066 24067 verifyFormat(NoBreak, Medium, Style); 24068 verifyFormat(NoBreak, 24069 "functionCall(\n" 24070 " paramA,\n" 24071 " paramB,\n" 24072 " paramC,\n" 24073 " paramD,\n" 24074 " paramE,\n" 24075 " paramF,\n" 24076 " paramG,\n" 24077 " paramH,\n" 24078 " paramI\n" 24079 ");\n" 24080 "void functionDecl(\n" 24081 " int argumentA,\n" 24082 " int argumentB,\n" 24083 " int argumentC,\n" 24084 " int argumentD,\n" 24085 " int argumentE\n" 24086 ");", 24087 Style); 24088 24089 verifyFormat("outerFunctionCall(nestedFunctionCall(argument1),\n" 24090 " nestedLongFunctionCall(argument1, " 24091 "argument2, argument3,\n" 24092 " argument4, " 24093 "argument5));", 24094 Style); 24095 24096 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; 24097 24098 verifyFormat(Short, Style); 24099 verifyFormat( 24100 "functionCall(\n" 24101 " paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, " 24102 "paramI\n" 24103 ");\n" 24104 "void functionDecl(\n" 24105 " int argumentA, int argumentB, int argumentC, int argumentD, int " 24106 "argumentE\n" 24107 ");", 24108 Medium, Style); 24109 24110 Style.AllowAllArgumentsOnNextLine = false; 24111 Style.AllowAllParametersOfDeclarationOnNextLine = false; 24112 24113 verifyFormat(Short, Style); 24114 verifyFormat( 24115 "functionCall(\n" 24116 " paramA, paramB, paramC, paramD, paramE, paramF, paramG, paramH, " 24117 "paramI\n" 24118 ");\n" 24119 "void functionDecl(\n" 24120 " int argumentA, int argumentB, int argumentC, int argumentD, int " 24121 "argumentE\n" 24122 ");", 24123 Medium, Style); 24124 24125 Style.BinPackArguments = false; 24126 Style.BinPackParameters = false; 24127 24128 verifyFormat(Short, Style); 24129 24130 verifyFormat("functionCall(\n" 24131 " paramA,\n" 24132 " paramB,\n" 24133 " paramC,\n" 24134 " paramD,\n" 24135 " paramE,\n" 24136 " paramF,\n" 24137 " paramG,\n" 24138 " paramH,\n" 24139 " paramI\n" 24140 ");\n" 24141 "void functionDecl(\n" 24142 " int argumentA,\n" 24143 " int argumentB,\n" 24144 " int argumentC,\n" 24145 " int argumentD,\n" 24146 " int argumentE\n" 24147 ");", 24148 Medium, Style); 24149 24150 verifyFormat("outerFunctionCall(\n" 24151 " nestedFunctionCall(argument1),\n" 24152 " nestedLongFunctionCall(\n" 24153 " argument1,\n" 24154 " argument2,\n" 24155 " argument3,\n" 24156 " argument4,\n" 24157 " argument5\n" 24158 " )\n" 24159 ");", 24160 Style); 24161 24162 verifyFormat("int a = (int)b;", Style); 24163 verifyFormat("int a = (int)b;", 24164 "int a = (\n" 24165 " int\n" 24166 ") b;", 24167 Style); 24168 24169 verifyFormat("return (true);", Style); 24170 verifyFormat("return (true);", 24171 "return (\n" 24172 " true\n" 24173 ");", 24174 Style); 24175 24176 verifyFormat("void foo();", Style); 24177 verifyFormat("void foo();", 24178 "void foo(\n" 24179 ");", 24180 Style); 24181 24182 verifyFormat("void foo() {}", Style); 24183 verifyFormat("void foo() {}", 24184 "void foo(\n" 24185 ") {\n" 24186 "}", 24187 Style); 24188 24189 verifyFormat("auto string = std::string();", Style); 24190 verifyFormat("auto string = std::string();", 24191 "auto string = std::string(\n" 24192 ");", 24193 Style); 24194 24195 verifyFormat("void (*functionPointer)() = nullptr;", Style); 24196 verifyFormat("void (*functionPointer)() = nullptr;", 24197 "void (\n" 24198 " *functionPointer\n" 24199 ")\n" 24200 "(\n" 24201 ") = nullptr;", 24202 Style); 24203 } 24204 24205 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentIfStatement) { 24206 auto Style = getLLVMStyle(); 24207 24208 verifyFormat("if (foo()) {\n" 24209 " return;\n" 24210 "}", 24211 Style); 24212 24213 verifyFormat("if (quitelongarg !=\n" 24214 " (alsolongarg - 1)) { // ABC is a very longgggggggggggg " 24215 "comment\n" 24216 " return;\n" 24217 "}", 24218 Style); 24219 24220 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; 24221 24222 verifyFormat("if (foo()) {\n" 24223 " return;\n" 24224 "}", 24225 Style); 24226 24227 verifyFormat("if (quitelongarg !=\n" 24228 " (alsolongarg - 1)) { // ABC is a very longgggggggggggg " 24229 "comment\n" 24230 " return;\n" 24231 "}", 24232 Style); 24233 } 24234 24235 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentForStatement) { 24236 auto Style = getLLVMStyle(); 24237 24238 verifyFormat("for (int i = 0; i < 5; ++i) {\n" 24239 " doSomething();\n" 24240 "}", 24241 Style); 24242 24243 verifyFormat("for (int myReallyLongCountVariable = 0; " 24244 "myReallyLongCountVariable < count;\n" 24245 " myReallyLongCountVariable++) {\n" 24246 " doSomething();\n" 24247 "}", 24248 Style); 24249 24250 Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; 24251 24252 verifyFormat("for (int i = 0; i < 5; ++i) {\n" 24253 " doSomething();\n" 24254 "}", 24255 Style); 24256 24257 verifyFormat("for (int myReallyLongCountVariable = 0; " 24258 "myReallyLongCountVariable < count;\n" 24259 " myReallyLongCountVariable++) {\n" 24260 " doSomething();\n" 24261 "}", 24262 Style); 24263 } 24264 24265 TEST_F(FormatTest, UnderstandsDigraphs) { 24266 verifyFormat("int arr<:5:> = {};"); 24267 verifyFormat("int arr[5] = <%%>;"); 24268 verifyFormat("int arr<:::qualified_variable:> = {};"); 24269 verifyFormat("int arr[::qualified_variable] = <%%>;"); 24270 verifyFormat("%:include <header>"); 24271 verifyFormat("%:define A x##y"); 24272 verifyFormat("#define A x%:%:y"); 24273 } 24274 24275 } // namespace 24276 } // namespace format 24277 } // namespace clang 24278