1 //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "clang/Format/Format.h" 10 11 #include "../Tooling/ReplacementTest.h" 12 #include "FormatTestUtils.h" 13 14 #include "llvm/Support/Debug.h" 15 #include "llvm/Support/MemoryBuffer.h" 16 #include "gtest/gtest.h" 17 18 #define DEBUG_TYPE "format-test" 19 20 using clang::tooling::ReplacementTest; 21 using clang::tooling::toReplacements; 22 using testing::ScopedTrace; 23 24 namespace clang { 25 namespace format { 26 namespace { 27 28 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); } 29 30 class FormatTest : public ::testing::Test { 31 protected: 32 enum StatusCheck { SC_ExpectComplete, SC_ExpectIncomplete, SC_DoNotCheck }; 33 34 std::string format(llvm::StringRef Code, 35 const FormatStyle &Style = getLLVMStyle(), 36 StatusCheck CheckComplete = SC_ExpectComplete) { 37 LLVM_DEBUG(llvm::errs() << "---\n"); 38 LLVM_DEBUG(llvm::errs() << Code << "\n\n"); 39 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size())); 40 FormattingAttemptStatus Status; 41 tooling::Replacements Replaces = 42 reformat(Style, Code, Ranges, "<stdin>", &Status); 43 if (CheckComplete != SC_DoNotCheck) { 44 bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete; 45 EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete) 46 << Code << "\n\n"; 47 } 48 ReplacementCount = Replaces.size(); 49 auto Result = applyAllReplacements(Code, Replaces); 50 EXPECT_TRUE(static_cast<bool>(Result)); 51 LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n"); 52 return *Result; 53 } 54 55 FormatStyle getStyleWithColumns(FormatStyle Style, unsigned ColumnLimit) { 56 Style.ColumnLimit = ColumnLimit; 57 return Style; 58 } 59 60 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { 61 return getStyleWithColumns(getLLVMStyle(), ColumnLimit); 62 } 63 64 FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) { 65 return getStyleWithColumns(getGoogleStyle(), ColumnLimit); 66 } 67 68 void _verifyFormat(const char *File, int Line, llvm::StringRef Expected, 69 llvm::StringRef Code, 70 const FormatStyle &Style = getLLVMStyle()) { 71 ScopedTrace t(File, Line, ::testing::Message() << Code.str()); 72 EXPECT_EQ(Expected.str(), format(Expected, Style)) 73 << "Expected code is not stable"; 74 EXPECT_EQ(Expected.str(), format(Code, Style)); 75 if (Style.Language == FormatStyle::LK_Cpp) { 76 // Objective-C++ is a superset of C++, so everything checked for C++ 77 // needs to be checked for Objective-C++ as well. 78 FormatStyle ObjCStyle = Style; 79 ObjCStyle.Language = FormatStyle::LK_ObjC; 80 EXPECT_EQ(Expected.str(), format(test::messUp(Code), ObjCStyle)); 81 } 82 } 83 84 void _verifyFormat(const char *File, int Line, llvm::StringRef Code, 85 const FormatStyle &Style = getLLVMStyle()) { 86 _verifyFormat(File, Line, Code, test::messUp(Code), Style); 87 } 88 89 void _verifyIncompleteFormat(const char *File, int Line, llvm::StringRef Code, 90 const FormatStyle &Style = getLLVMStyle()) { 91 ScopedTrace t(File, Line, ::testing::Message() << Code.str()); 92 EXPECT_EQ(Code.str(), 93 format(test::messUp(Code), Style, SC_ExpectIncomplete)); 94 } 95 96 void _verifyIndependentOfContext(const char *File, int Line, 97 llvm::StringRef Text, 98 const FormatStyle &Style = getLLVMStyle()) { 99 _verifyFormat(File, Line, Text, Style); 100 _verifyFormat(File, Line, llvm::Twine("void f() { " + Text + " }").str(), 101 Style); 102 } 103 104 /// \brief Verify that clang-format does not crash on the given input. 105 void verifyNoCrash(llvm::StringRef Code, 106 const FormatStyle &Style = getLLVMStyle()) { 107 format(Code, Style, SC_DoNotCheck); 108 } 109 110 int ReplacementCount; 111 }; 112 113 #define verifyIndependentOfContext(...) \ 114 _verifyIndependentOfContext(__FILE__, __LINE__, __VA_ARGS__) 115 #define verifyIncompleteFormat(...) \ 116 _verifyIncompleteFormat(__FILE__, __LINE__, __VA_ARGS__) 117 #define verifyFormat(...) _verifyFormat(__FILE__, __LINE__, __VA_ARGS__) 118 #define verifyGoogleFormat(Code) verifyFormat(Code, getGoogleStyle()) 119 120 TEST_F(FormatTest, MessUp) { 121 EXPECT_EQ("1 2 3", test::messUp("1 2 3")); 122 EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n")); 123 EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc")); 124 EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc")); 125 EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne")); 126 } 127 128 TEST_F(FormatTest, DefaultLLVMStyleIsCpp) { 129 EXPECT_EQ(FormatStyle::LK_Cpp, getLLVMStyle().Language); 130 } 131 132 TEST_F(FormatTest, LLVMStyleOverride) { 133 EXPECT_EQ(FormatStyle::LK_Proto, 134 getLLVMStyle(FormatStyle::LK_Proto).Language); 135 } 136 137 //===----------------------------------------------------------------------===// 138 // Basic function tests. 139 //===----------------------------------------------------------------------===// 140 141 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) { 142 EXPECT_EQ(";", format(";")); 143 } 144 145 TEST_F(FormatTest, FormatsGlobalStatementsAt0) { 146 EXPECT_EQ("int i;", format(" int i;")); 147 EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;")); 148 EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); 149 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); 150 } 151 152 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { 153 EXPECT_EQ("int i;", format("int\ni;")); 154 } 155 156 TEST_F(FormatTest, FormatsNestedBlockStatements) { 157 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}")); 158 } 159 160 TEST_F(FormatTest, FormatsNestedCall) { 161 verifyFormat("Method(f1, f2(f3));"); 162 verifyFormat("Method(f1(f2, f3()));"); 163 verifyFormat("Method(f1(f2, (f3())));"); 164 } 165 166 TEST_F(FormatTest, NestedNameSpecifiers) { 167 verifyFormat("vector<::Type> v;"); 168 verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())"); 169 verifyFormat("static constexpr bool Bar = decltype(bar())::value;"); 170 verifyFormat("static constexpr bool Bar = typeof(bar())::value;"); 171 verifyFormat("static constexpr bool Bar = __underlying_type(bar())::value;"); 172 verifyFormat("static constexpr bool Bar = _Atomic(bar())::value;"); 173 verifyFormat("bool a = 2 < ::SomeFunction();"); 174 verifyFormat("ALWAYS_INLINE ::std::string getName();"); 175 verifyFormat("some::string getName();"); 176 } 177 178 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) { 179 EXPECT_EQ("if (a) {\n" 180 " f();\n" 181 "}", 182 format("if(a){f();}")); 183 EXPECT_EQ(4, ReplacementCount); 184 EXPECT_EQ("if (a) {\n" 185 " f();\n" 186 "}", 187 format("if (a) {\n" 188 " f();\n" 189 "}")); 190 EXPECT_EQ(0, ReplacementCount); 191 EXPECT_EQ("/*\r\n" 192 "\r\n" 193 "*/\r\n", 194 format("/*\r\n" 195 "\r\n" 196 "*/\r\n")); 197 EXPECT_EQ(0, ReplacementCount); 198 } 199 200 TEST_F(FormatTest, RemovesEmptyLines) { 201 EXPECT_EQ("class C {\n" 202 " int i;\n" 203 "};", 204 format("class C {\n" 205 " int i;\n" 206 "\n" 207 "};")); 208 209 // Don't remove empty lines at the start of namespaces or extern "C" blocks. 210 EXPECT_EQ("namespace N {\n" 211 "\n" 212 "int i;\n" 213 "}", 214 format("namespace N {\n" 215 "\n" 216 "int i;\n" 217 "}", 218 getGoogleStyle())); 219 EXPECT_EQ("/* something */ namespace N {\n" 220 "\n" 221 "int i;\n" 222 "}", 223 format("/* something */ namespace N {\n" 224 "\n" 225 "int i;\n" 226 "}", 227 getGoogleStyle())); 228 EXPECT_EQ("inline namespace N {\n" 229 "\n" 230 "int i;\n" 231 "}", 232 format("inline namespace N {\n" 233 "\n" 234 "int i;\n" 235 "}", 236 getGoogleStyle())); 237 EXPECT_EQ("/* something */ inline namespace N {\n" 238 "\n" 239 "int i;\n" 240 "}", 241 format("/* something */ inline namespace N {\n" 242 "\n" 243 "int i;\n" 244 "}", 245 getGoogleStyle())); 246 EXPECT_EQ("export namespace N {\n" 247 "\n" 248 "int i;\n" 249 "}", 250 format("export namespace N {\n" 251 "\n" 252 "int i;\n" 253 "}", 254 getGoogleStyle())); 255 EXPECT_EQ("extern /**/ \"C\" /**/ {\n" 256 "\n" 257 "int i;\n" 258 "}", 259 format("extern /**/ \"C\" /**/ {\n" 260 "\n" 261 "int i;\n" 262 "}", 263 getGoogleStyle())); 264 265 auto CustomStyle = clang::format::getLLVMStyle(); 266 CustomStyle.BreakBeforeBraces = clang::format::FormatStyle::BS_Custom; 267 CustomStyle.BraceWrapping.AfterNamespace = true; 268 CustomStyle.KeepEmptyLinesAtTheStartOfBlocks = false; 269 EXPECT_EQ("namespace N\n" 270 "{\n" 271 "\n" 272 "int i;\n" 273 "}", 274 format("namespace N\n" 275 "{\n" 276 "\n" 277 "\n" 278 "int i;\n" 279 "}", 280 CustomStyle)); 281 EXPECT_EQ("/* something */ namespace N\n" 282 "{\n" 283 "\n" 284 "int i;\n" 285 "}", 286 format("/* something */ namespace N {\n" 287 "\n" 288 "\n" 289 "int i;\n" 290 "}", 291 CustomStyle)); 292 EXPECT_EQ("inline namespace N\n" 293 "{\n" 294 "\n" 295 "int i;\n" 296 "}", 297 format("inline namespace N\n" 298 "{\n" 299 "\n" 300 "\n" 301 "int i;\n" 302 "}", 303 CustomStyle)); 304 EXPECT_EQ("/* something */ inline namespace N\n" 305 "{\n" 306 "\n" 307 "int i;\n" 308 "}", 309 format("/* something */ inline namespace N\n" 310 "{\n" 311 "\n" 312 "int i;\n" 313 "}", 314 CustomStyle)); 315 EXPECT_EQ("export namespace N\n" 316 "{\n" 317 "\n" 318 "int i;\n" 319 "}", 320 format("export namespace N\n" 321 "{\n" 322 "\n" 323 "int i;\n" 324 "}", 325 CustomStyle)); 326 EXPECT_EQ("namespace a\n" 327 "{\n" 328 "namespace b\n" 329 "{\n" 330 "\n" 331 "class AA {};\n" 332 "\n" 333 "} // namespace b\n" 334 "} // namespace a\n", 335 format("namespace a\n" 336 "{\n" 337 "namespace b\n" 338 "{\n" 339 "\n" 340 "\n" 341 "class AA {};\n" 342 "\n" 343 "\n" 344 "}\n" 345 "}\n", 346 CustomStyle)); 347 EXPECT_EQ("namespace A /* comment */\n" 348 "{\n" 349 "class B {}\n" 350 "} // namespace A", 351 format("namespace A /* comment */ { class B {} }", CustomStyle)); 352 EXPECT_EQ("namespace A\n" 353 "{ /* comment */\n" 354 "class B {}\n" 355 "} // namespace A", 356 format("namespace A {/* comment */ class B {} }", CustomStyle)); 357 EXPECT_EQ("namespace A\n" 358 "{ /* comment */\n" 359 "\n" 360 "class B {}\n" 361 "\n" 362 "" 363 "} // namespace A", 364 format("namespace A { /* comment */\n" 365 "\n" 366 "\n" 367 "class B {}\n" 368 "\n" 369 "\n" 370 "}", 371 CustomStyle)); 372 EXPECT_EQ("namespace A /* comment */\n" 373 "{\n" 374 "\n" 375 "class B {}\n" 376 "\n" 377 "} // namespace A", 378 format("namespace A/* comment */ {\n" 379 "\n" 380 "\n" 381 "class B {}\n" 382 "\n" 383 "\n" 384 "}", 385 CustomStyle)); 386 387 // ...but do keep inlining and removing empty lines for non-block extern "C" 388 // functions. 389 verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle()); 390 EXPECT_EQ("extern \"C\" int f() {\n" 391 " int i = 42;\n" 392 " return i;\n" 393 "}", 394 format("extern \"C\" int f() {\n" 395 "\n" 396 " int i = 42;\n" 397 " return i;\n" 398 "}", 399 getGoogleStyle())); 400 401 // Remove empty lines at the beginning and end of blocks. 402 EXPECT_EQ("void f() {\n" 403 "\n" 404 " if (a) {\n" 405 "\n" 406 " f();\n" 407 " }\n" 408 "}", 409 format("void f() {\n" 410 "\n" 411 " if (a) {\n" 412 "\n" 413 " f();\n" 414 "\n" 415 " }\n" 416 "\n" 417 "}", 418 getLLVMStyle())); 419 EXPECT_EQ("void f() {\n" 420 " if (a) {\n" 421 " f();\n" 422 " }\n" 423 "}", 424 format("void f() {\n" 425 "\n" 426 " if (a) {\n" 427 "\n" 428 " f();\n" 429 "\n" 430 " }\n" 431 "\n" 432 "}", 433 getGoogleStyle())); 434 435 // Don't remove empty lines in more complex control statements. 436 EXPECT_EQ("void f() {\n" 437 " if (a) {\n" 438 " f();\n" 439 "\n" 440 " } else if (b) {\n" 441 " f();\n" 442 " }\n" 443 "}", 444 format("void f() {\n" 445 " if (a) {\n" 446 " f();\n" 447 "\n" 448 " } else if (b) {\n" 449 " f();\n" 450 "\n" 451 " }\n" 452 "\n" 453 "}")); 454 455 // Don't remove empty lines before namespace endings. 456 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 457 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 458 EXPECT_EQ("namespace {\n" 459 "int i;\n" 460 "\n" 461 "}", 462 format("namespace {\n" 463 "int i;\n" 464 "\n" 465 "}", 466 LLVMWithNoNamespaceFix)); 467 EXPECT_EQ("namespace {\n" 468 "int i;\n" 469 "}", 470 format("namespace {\n" 471 "int i;\n" 472 "}", 473 LLVMWithNoNamespaceFix)); 474 EXPECT_EQ("namespace {\n" 475 "int i;\n" 476 "\n" 477 "};", 478 format("namespace {\n" 479 "int i;\n" 480 "\n" 481 "};", 482 LLVMWithNoNamespaceFix)); 483 EXPECT_EQ("namespace {\n" 484 "int i;\n" 485 "};", 486 format("namespace {\n" 487 "int i;\n" 488 "};", 489 LLVMWithNoNamespaceFix)); 490 EXPECT_EQ("namespace {\n" 491 "int i;\n" 492 "\n" 493 "}", 494 format("namespace {\n" 495 "int i;\n" 496 "\n" 497 "}")); 498 EXPECT_EQ("namespace {\n" 499 "int i;\n" 500 "\n" 501 "} // namespace", 502 format("namespace {\n" 503 "int i;\n" 504 "\n" 505 "} // namespace")); 506 507 FormatStyle Style = getLLVMStyle(); 508 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 509 Style.MaxEmptyLinesToKeep = 2; 510 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 511 Style.BraceWrapping.AfterClass = true; 512 Style.BraceWrapping.AfterFunction = true; 513 Style.KeepEmptyLinesAtTheStartOfBlocks = false; 514 515 EXPECT_EQ("class Foo\n" 516 "{\n" 517 " Foo() {}\n" 518 "\n" 519 " void funk() {}\n" 520 "};", 521 format("class Foo\n" 522 "{\n" 523 " Foo()\n" 524 " {\n" 525 " }\n" 526 "\n" 527 " void funk() {}\n" 528 "};", 529 Style)); 530 } 531 532 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) { 533 verifyFormat("x = (a) and (b);"); 534 verifyFormat("x = (a) or (b);"); 535 verifyFormat("x = (a) bitand (b);"); 536 verifyFormat("x = (a) bitor (b);"); 537 verifyFormat("x = (a) not_eq (b);"); 538 verifyFormat("x = (a) and_eq (b);"); 539 verifyFormat("x = (a) or_eq (b);"); 540 verifyFormat("x = (a) xor (b);"); 541 } 542 543 TEST_F(FormatTest, RecognizesUnaryOperatorKeywords) { 544 verifyFormat("x = compl(a);"); 545 verifyFormat("x = not(a);"); 546 verifyFormat("x = bitand(a);"); 547 // Unary operator must not be merged with the next identifier 548 verifyFormat("x = compl a;"); 549 verifyFormat("x = not a;"); 550 verifyFormat("x = bitand a;"); 551 } 552 553 //===----------------------------------------------------------------------===// 554 // Tests for control statements. 555 //===----------------------------------------------------------------------===// 556 557 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) { 558 verifyFormat("if (true)\n f();\ng();"); 559 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); 560 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); 561 verifyFormat("if constexpr (true)\n" 562 " f();\ng();"); 563 verifyFormat("if CONSTEXPR (true)\n" 564 " f();\ng();"); 565 verifyFormat("if constexpr (a)\n" 566 " if constexpr (b)\n" 567 " if constexpr (c)\n" 568 " g();\n" 569 "h();"); 570 verifyFormat("if CONSTEXPR (a)\n" 571 " if CONSTEXPR (b)\n" 572 " if CONSTEXPR (c)\n" 573 " g();\n" 574 "h();"); 575 verifyFormat("if constexpr (a)\n" 576 " if constexpr (b) {\n" 577 " f();\n" 578 " }\n" 579 "g();"); 580 verifyFormat("if CONSTEXPR (a)\n" 581 " if CONSTEXPR (b) {\n" 582 " f();\n" 583 " }\n" 584 "g();"); 585 586 verifyFormat("if (a)\n" 587 " g();"); 588 verifyFormat("if (a) {\n" 589 " g()\n" 590 "};"); 591 verifyFormat("if (a)\n" 592 " g();\n" 593 "else\n" 594 " g();"); 595 verifyFormat("if (a) {\n" 596 " g();\n" 597 "} else\n" 598 " g();"); 599 verifyFormat("if (a)\n" 600 " g();\n" 601 "else {\n" 602 " g();\n" 603 "}"); 604 verifyFormat("if (a) {\n" 605 " g();\n" 606 "} else {\n" 607 " g();\n" 608 "}"); 609 verifyFormat("if (a)\n" 610 " g();\n" 611 "else if (b)\n" 612 " g();\n" 613 "else\n" 614 " g();"); 615 verifyFormat("if (a) {\n" 616 " g();\n" 617 "} else if (b)\n" 618 " g();\n" 619 "else\n" 620 " g();"); 621 verifyFormat("if (a)\n" 622 " g();\n" 623 "else if (b) {\n" 624 " g();\n" 625 "} else\n" 626 " g();"); 627 verifyFormat("if (a)\n" 628 " g();\n" 629 "else if (b)\n" 630 " g();\n" 631 "else {\n" 632 " g();\n" 633 "}"); 634 verifyFormat("if (a)\n" 635 " g();\n" 636 "else if (b) {\n" 637 " g();\n" 638 "} else {\n" 639 " g();\n" 640 "}"); 641 verifyFormat("if (a) {\n" 642 " g();\n" 643 "} else if (b) {\n" 644 " g();\n" 645 "} else {\n" 646 " g();\n" 647 "}"); 648 649 FormatStyle AllowsMergedIf = getLLVMStyle(); 650 AllowsMergedIf.IfMacros.push_back("MYIF"); 651 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left; 652 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 653 FormatStyle::SIS_WithoutElse; 654 verifyFormat("if (a)\n" 655 " // comment\n" 656 " f();", 657 AllowsMergedIf); 658 verifyFormat("{\n" 659 " if (a)\n" 660 " label:\n" 661 " f();\n" 662 "}", 663 AllowsMergedIf); 664 verifyFormat("#define A \\\n" 665 " if (a) \\\n" 666 " label: \\\n" 667 " f()", 668 AllowsMergedIf); 669 verifyFormat("if (a)\n" 670 " ;", 671 AllowsMergedIf); 672 verifyFormat("if (a)\n" 673 " if (b) return;", 674 AllowsMergedIf); 675 676 verifyFormat("if (a) // Can't merge this\n" 677 " f();\n", 678 AllowsMergedIf); 679 verifyFormat("if (a) /* still don't merge */\n" 680 " f();", 681 AllowsMergedIf); 682 verifyFormat("if (a) { // Never merge this\n" 683 " f();\n" 684 "}", 685 AllowsMergedIf); 686 verifyFormat("if (a) { /* Never merge this */\n" 687 " f();\n" 688 "}", 689 AllowsMergedIf); 690 verifyFormat("MYIF (a)\n" 691 " // comment\n" 692 " f();", 693 AllowsMergedIf); 694 verifyFormat("{\n" 695 " MYIF (a)\n" 696 " label:\n" 697 " f();\n" 698 "}", 699 AllowsMergedIf); 700 verifyFormat("#define A \\\n" 701 " MYIF (a) \\\n" 702 " label: \\\n" 703 " f()", 704 AllowsMergedIf); 705 verifyFormat("MYIF (a)\n" 706 " ;", 707 AllowsMergedIf); 708 verifyFormat("MYIF (a)\n" 709 " MYIF (b) return;", 710 AllowsMergedIf); 711 712 verifyFormat("MYIF (a) // Can't merge this\n" 713 " f();\n", 714 AllowsMergedIf); 715 verifyFormat("MYIF (a) /* still don't merge */\n" 716 " f();", 717 AllowsMergedIf); 718 verifyFormat("MYIF (a) { // Never merge this\n" 719 " f();\n" 720 "}", 721 AllowsMergedIf); 722 verifyFormat("MYIF (a) { /* Never merge this */\n" 723 " f();\n" 724 "}", 725 AllowsMergedIf); 726 727 AllowsMergedIf.ColumnLimit = 14; 728 // Where line-lengths matter, a 2-letter synonym that maintains line length. 729 // Not IF to avoid any confusion that IF is somehow special. 730 AllowsMergedIf.IfMacros.push_back("FI"); 731 verifyFormat("if (a) return;", AllowsMergedIf); 732 verifyFormat("if (aaaaaaaaa)\n" 733 " return;", 734 AllowsMergedIf); 735 verifyFormat("FI (a) return;", AllowsMergedIf); 736 verifyFormat("FI (aaaaaaaaa)\n" 737 " return;", 738 AllowsMergedIf); 739 740 AllowsMergedIf.ColumnLimit = 13; 741 verifyFormat("if (a)\n return;", AllowsMergedIf); 742 verifyFormat("FI (a)\n return;", AllowsMergedIf); 743 744 FormatStyle AllowsMergedIfElse = getLLVMStyle(); 745 AllowsMergedIfElse.IfMacros.push_back("MYIF"); 746 AllowsMergedIfElse.AllowShortIfStatementsOnASingleLine = 747 FormatStyle::SIS_AllIfsAndElse; 748 verifyFormat("if (a)\n" 749 " // comment\n" 750 " f();\n" 751 "else\n" 752 " // comment\n" 753 " f();", 754 AllowsMergedIfElse); 755 verifyFormat("{\n" 756 " if (a)\n" 757 " label:\n" 758 " f();\n" 759 " else\n" 760 " label:\n" 761 " f();\n" 762 "}", 763 AllowsMergedIfElse); 764 verifyFormat("if (a)\n" 765 " ;\n" 766 "else\n" 767 " ;", 768 AllowsMergedIfElse); 769 verifyFormat("if (a) {\n" 770 "} else {\n" 771 "}", 772 AllowsMergedIfElse); 773 verifyFormat("if (a) return;\n" 774 "else if (b) return;\n" 775 "else return;", 776 AllowsMergedIfElse); 777 verifyFormat("if (a) {\n" 778 "} else return;", 779 AllowsMergedIfElse); 780 verifyFormat("if (a) {\n" 781 "} else if (b) return;\n" 782 "else return;", 783 AllowsMergedIfElse); 784 verifyFormat("if (a) return;\n" 785 "else if (b) {\n" 786 "} else return;", 787 AllowsMergedIfElse); 788 verifyFormat("if (a)\n" 789 " if (b) return;\n" 790 " else return;", 791 AllowsMergedIfElse); 792 verifyFormat("if constexpr (a)\n" 793 " if constexpr (b) return;\n" 794 " else if constexpr (c) return;\n" 795 " else return;", 796 AllowsMergedIfElse); 797 verifyFormat("MYIF (a)\n" 798 " // comment\n" 799 " f();\n" 800 "else\n" 801 " // comment\n" 802 " f();", 803 AllowsMergedIfElse); 804 verifyFormat("{\n" 805 " MYIF (a)\n" 806 " label:\n" 807 " f();\n" 808 " else\n" 809 " label:\n" 810 " f();\n" 811 "}", 812 AllowsMergedIfElse); 813 verifyFormat("MYIF (a)\n" 814 " ;\n" 815 "else\n" 816 " ;", 817 AllowsMergedIfElse); 818 verifyFormat("MYIF (a) {\n" 819 "} else {\n" 820 "}", 821 AllowsMergedIfElse); 822 verifyFormat("MYIF (a) return;\n" 823 "else MYIF (b) return;\n" 824 "else return;", 825 AllowsMergedIfElse); 826 verifyFormat("MYIF (a) {\n" 827 "} else return;", 828 AllowsMergedIfElse); 829 verifyFormat("MYIF (a) {\n" 830 "} else MYIF (b) return;\n" 831 "else return;", 832 AllowsMergedIfElse); 833 verifyFormat("MYIF (a) return;\n" 834 "else MYIF (b) {\n" 835 "} else return;", 836 AllowsMergedIfElse); 837 verifyFormat("MYIF (a)\n" 838 " MYIF (b) return;\n" 839 " else return;", 840 AllowsMergedIfElse); 841 verifyFormat("MYIF constexpr (a)\n" 842 " MYIF constexpr (b) return;\n" 843 " else MYIF constexpr (c) return;\n" 844 " else return;", 845 AllowsMergedIfElse); 846 } 847 848 TEST_F(FormatTest, FormatIfWithoutCompoundStatementButElseWith) { 849 FormatStyle AllowsMergedIf = getLLVMStyle(); 850 AllowsMergedIf.IfMacros.push_back("MYIF"); 851 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left; 852 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 853 FormatStyle::SIS_WithoutElse; 854 verifyFormat("if (a)\n" 855 " f();\n" 856 "else {\n" 857 " g();\n" 858 "}", 859 AllowsMergedIf); 860 verifyFormat("if (a)\n" 861 " f();\n" 862 "else\n" 863 " g();\n", 864 AllowsMergedIf); 865 866 verifyFormat("if (a) g();", AllowsMergedIf); 867 verifyFormat("if (a) {\n" 868 " g()\n" 869 "};", 870 AllowsMergedIf); 871 verifyFormat("if (a)\n" 872 " g();\n" 873 "else\n" 874 " g();", 875 AllowsMergedIf); 876 verifyFormat("if (a) {\n" 877 " g();\n" 878 "} else\n" 879 " g();", 880 AllowsMergedIf); 881 verifyFormat("if (a)\n" 882 " g();\n" 883 "else {\n" 884 " g();\n" 885 "}", 886 AllowsMergedIf); 887 verifyFormat("if (a) {\n" 888 " g();\n" 889 "} else {\n" 890 " g();\n" 891 "}", 892 AllowsMergedIf); 893 verifyFormat("if (a)\n" 894 " g();\n" 895 "else if (b)\n" 896 " g();\n" 897 "else\n" 898 " g();", 899 AllowsMergedIf); 900 verifyFormat("if (a) {\n" 901 " g();\n" 902 "} else if (b)\n" 903 " g();\n" 904 "else\n" 905 " g();", 906 AllowsMergedIf); 907 verifyFormat("if (a)\n" 908 " g();\n" 909 "else if (b) {\n" 910 " g();\n" 911 "} else\n" 912 " g();", 913 AllowsMergedIf); 914 verifyFormat("if (a)\n" 915 " g();\n" 916 "else if (b)\n" 917 " g();\n" 918 "else {\n" 919 " g();\n" 920 "}", 921 AllowsMergedIf); 922 verifyFormat("if (a)\n" 923 " g();\n" 924 "else if (b) {\n" 925 " g();\n" 926 "} else {\n" 927 " g();\n" 928 "}", 929 AllowsMergedIf); 930 verifyFormat("if (a) {\n" 931 " g();\n" 932 "} else if (b) {\n" 933 " g();\n" 934 "} else {\n" 935 " g();\n" 936 "}", 937 AllowsMergedIf); 938 verifyFormat("MYIF (a)\n" 939 " f();\n" 940 "else {\n" 941 " g();\n" 942 "}", 943 AllowsMergedIf); 944 verifyFormat("MYIF (a)\n" 945 " f();\n" 946 "else\n" 947 " g();\n", 948 AllowsMergedIf); 949 950 verifyFormat("MYIF (a) g();", AllowsMergedIf); 951 verifyFormat("MYIF (a) {\n" 952 " g()\n" 953 "};", 954 AllowsMergedIf); 955 verifyFormat("MYIF (a)\n" 956 " g();\n" 957 "else\n" 958 " g();", 959 AllowsMergedIf); 960 verifyFormat("MYIF (a) {\n" 961 " g();\n" 962 "} else\n" 963 " g();", 964 AllowsMergedIf); 965 verifyFormat("MYIF (a)\n" 966 " g();\n" 967 "else {\n" 968 " g();\n" 969 "}", 970 AllowsMergedIf); 971 verifyFormat("MYIF (a) {\n" 972 " g();\n" 973 "} else {\n" 974 " g();\n" 975 "}", 976 AllowsMergedIf); 977 verifyFormat("MYIF (a)\n" 978 " g();\n" 979 "else MYIF (b)\n" 980 " g();\n" 981 "else\n" 982 " g();", 983 AllowsMergedIf); 984 verifyFormat("MYIF (a)\n" 985 " g();\n" 986 "else if (b)\n" 987 " g();\n" 988 "else\n" 989 " g();", 990 AllowsMergedIf); 991 verifyFormat("MYIF (a) {\n" 992 " g();\n" 993 "} else MYIF (b)\n" 994 " g();\n" 995 "else\n" 996 " g();", 997 AllowsMergedIf); 998 verifyFormat("MYIF (a) {\n" 999 " g();\n" 1000 "} else if (b)\n" 1001 " g();\n" 1002 "else\n" 1003 " g();", 1004 AllowsMergedIf); 1005 verifyFormat("MYIF (a)\n" 1006 " g();\n" 1007 "else MYIF (b) {\n" 1008 " g();\n" 1009 "} else\n" 1010 " g();", 1011 AllowsMergedIf); 1012 verifyFormat("MYIF (a)\n" 1013 " g();\n" 1014 "else if (b) {\n" 1015 " g();\n" 1016 "} else\n" 1017 " g();", 1018 AllowsMergedIf); 1019 verifyFormat("MYIF (a)\n" 1020 " g();\n" 1021 "else MYIF (b)\n" 1022 " g();\n" 1023 "else {\n" 1024 " g();\n" 1025 "}", 1026 AllowsMergedIf); 1027 verifyFormat("MYIF (a)\n" 1028 " g();\n" 1029 "else if (b)\n" 1030 " g();\n" 1031 "else {\n" 1032 " g();\n" 1033 "}", 1034 AllowsMergedIf); 1035 verifyFormat("MYIF (a)\n" 1036 " g();\n" 1037 "else MYIF (b) {\n" 1038 " g();\n" 1039 "} else {\n" 1040 " g();\n" 1041 "}", 1042 AllowsMergedIf); 1043 verifyFormat("MYIF (a)\n" 1044 " g();\n" 1045 "else if (b) {\n" 1046 " g();\n" 1047 "} else {\n" 1048 " g();\n" 1049 "}", 1050 AllowsMergedIf); 1051 verifyFormat("MYIF (a) {\n" 1052 " g();\n" 1053 "} else MYIF (b) {\n" 1054 " g();\n" 1055 "} else {\n" 1056 " g();\n" 1057 "}", 1058 AllowsMergedIf); 1059 verifyFormat("MYIF (a) {\n" 1060 " g();\n" 1061 "} else if (b) {\n" 1062 " g();\n" 1063 "} else {\n" 1064 " g();\n" 1065 "}", 1066 AllowsMergedIf); 1067 1068 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 1069 FormatStyle::SIS_OnlyFirstIf; 1070 1071 verifyFormat("if (a) f();\n" 1072 "else {\n" 1073 " g();\n" 1074 "}", 1075 AllowsMergedIf); 1076 verifyFormat("if (a) f();\n" 1077 "else {\n" 1078 " if (a) f();\n" 1079 " else {\n" 1080 " g();\n" 1081 " }\n" 1082 " g();\n" 1083 "}", 1084 AllowsMergedIf); 1085 1086 verifyFormat("if (a) g();", AllowsMergedIf); 1087 verifyFormat("if (a) {\n" 1088 " g()\n" 1089 "};", 1090 AllowsMergedIf); 1091 verifyFormat("if (a) g();\n" 1092 "else\n" 1093 " g();", 1094 AllowsMergedIf); 1095 verifyFormat("if (a) {\n" 1096 " g();\n" 1097 "} else\n" 1098 " g();", 1099 AllowsMergedIf); 1100 verifyFormat("if (a) g();\n" 1101 "else {\n" 1102 " g();\n" 1103 "}", 1104 AllowsMergedIf); 1105 verifyFormat("if (a) {\n" 1106 " g();\n" 1107 "} else {\n" 1108 " g();\n" 1109 "}", 1110 AllowsMergedIf); 1111 verifyFormat("if (a) g();\n" 1112 "else if (b)\n" 1113 " g();\n" 1114 "else\n" 1115 " g();", 1116 AllowsMergedIf); 1117 verifyFormat("if (a) {\n" 1118 " g();\n" 1119 "} else if (b)\n" 1120 " g();\n" 1121 "else\n" 1122 " g();", 1123 AllowsMergedIf); 1124 verifyFormat("if (a) g();\n" 1125 "else if (b) {\n" 1126 " g();\n" 1127 "} else\n" 1128 " g();", 1129 AllowsMergedIf); 1130 verifyFormat("if (a) g();\n" 1131 "else if (b)\n" 1132 " g();\n" 1133 "else {\n" 1134 " g();\n" 1135 "}", 1136 AllowsMergedIf); 1137 verifyFormat("if (a) g();\n" 1138 "else if (b) {\n" 1139 " g();\n" 1140 "} else {\n" 1141 " g();\n" 1142 "}", 1143 AllowsMergedIf); 1144 verifyFormat("if (a) {\n" 1145 " g();\n" 1146 "} else if (b) {\n" 1147 " g();\n" 1148 "} else {\n" 1149 " g();\n" 1150 "}", 1151 AllowsMergedIf); 1152 verifyFormat("MYIF (a) f();\n" 1153 "else {\n" 1154 " g();\n" 1155 "}", 1156 AllowsMergedIf); 1157 verifyFormat("MYIF (a) f();\n" 1158 "else {\n" 1159 " if (a) f();\n" 1160 " else {\n" 1161 " g();\n" 1162 " }\n" 1163 " g();\n" 1164 "}", 1165 AllowsMergedIf); 1166 1167 verifyFormat("MYIF (a) g();", AllowsMergedIf); 1168 verifyFormat("MYIF (a) {\n" 1169 " g()\n" 1170 "};", 1171 AllowsMergedIf); 1172 verifyFormat("MYIF (a) g();\n" 1173 "else\n" 1174 " g();", 1175 AllowsMergedIf); 1176 verifyFormat("MYIF (a) {\n" 1177 " g();\n" 1178 "} else\n" 1179 " g();", 1180 AllowsMergedIf); 1181 verifyFormat("MYIF (a) g();\n" 1182 "else {\n" 1183 " g();\n" 1184 "}", 1185 AllowsMergedIf); 1186 verifyFormat("MYIF (a) {\n" 1187 " g();\n" 1188 "} else {\n" 1189 " g();\n" 1190 "}", 1191 AllowsMergedIf); 1192 verifyFormat("MYIF (a) g();\n" 1193 "else MYIF (b)\n" 1194 " g();\n" 1195 "else\n" 1196 " g();", 1197 AllowsMergedIf); 1198 verifyFormat("MYIF (a) g();\n" 1199 "else if (b)\n" 1200 " g();\n" 1201 "else\n" 1202 " g();", 1203 AllowsMergedIf); 1204 verifyFormat("MYIF (a) {\n" 1205 " g();\n" 1206 "} else MYIF (b)\n" 1207 " g();\n" 1208 "else\n" 1209 " g();", 1210 AllowsMergedIf); 1211 verifyFormat("MYIF (a) {\n" 1212 " g();\n" 1213 "} else if (b)\n" 1214 " g();\n" 1215 "else\n" 1216 " g();", 1217 AllowsMergedIf); 1218 verifyFormat("MYIF (a) g();\n" 1219 "else MYIF (b) {\n" 1220 " g();\n" 1221 "} else\n" 1222 " g();", 1223 AllowsMergedIf); 1224 verifyFormat("MYIF (a) g();\n" 1225 "else if (b) {\n" 1226 " g();\n" 1227 "} else\n" 1228 " g();", 1229 AllowsMergedIf); 1230 verifyFormat("MYIF (a) g();\n" 1231 "else MYIF (b)\n" 1232 " g();\n" 1233 "else {\n" 1234 " g();\n" 1235 "}", 1236 AllowsMergedIf); 1237 verifyFormat("MYIF (a) g();\n" 1238 "else if (b)\n" 1239 " g();\n" 1240 "else {\n" 1241 " g();\n" 1242 "}", 1243 AllowsMergedIf); 1244 verifyFormat("MYIF (a) g();\n" 1245 "else MYIF (b) {\n" 1246 " g();\n" 1247 "} else {\n" 1248 " g();\n" 1249 "}", 1250 AllowsMergedIf); 1251 verifyFormat("MYIF (a) g();\n" 1252 "else if (b) {\n" 1253 " g();\n" 1254 "} else {\n" 1255 " g();\n" 1256 "}", 1257 AllowsMergedIf); 1258 verifyFormat("MYIF (a) {\n" 1259 " g();\n" 1260 "} else MYIF (b) {\n" 1261 " g();\n" 1262 "} else {\n" 1263 " g();\n" 1264 "}", 1265 AllowsMergedIf); 1266 verifyFormat("MYIF (a) {\n" 1267 " g();\n" 1268 "} else if (b) {\n" 1269 " g();\n" 1270 "} else {\n" 1271 " g();\n" 1272 "}", 1273 AllowsMergedIf); 1274 1275 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 1276 FormatStyle::SIS_AllIfsAndElse; 1277 1278 verifyFormat("if (a) f();\n" 1279 "else {\n" 1280 " g();\n" 1281 "}", 1282 AllowsMergedIf); 1283 verifyFormat("if (a) f();\n" 1284 "else {\n" 1285 " if (a) f();\n" 1286 " else {\n" 1287 " g();\n" 1288 " }\n" 1289 " g();\n" 1290 "}", 1291 AllowsMergedIf); 1292 1293 verifyFormat("if (a) g();", AllowsMergedIf); 1294 verifyFormat("if (a) {\n" 1295 " g()\n" 1296 "};", 1297 AllowsMergedIf); 1298 verifyFormat("if (a) g();\n" 1299 "else g();", 1300 AllowsMergedIf); 1301 verifyFormat("if (a) {\n" 1302 " g();\n" 1303 "} else g();", 1304 AllowsMergedIf); 1305 verifyFormat("if (a) g();\n" 1306 "else {\n" 1307 " g();\n" 1308 "}", 1309 AllowsMergedIf); 1310 verifyFormat("if (a) {\n" 1311 " g();\n" 1312 "} else {\n" 1313 " g();\n" 1314 "}", 1315 AllowsMergedIf); 1316 verifyFormat("if (a) g();\n" 1317 "else if (b) g();\n" 1318 "else g();", 1319 AllowsMergedIf); 1320 verifyFormat("if (a) {\n" 1321 " g();\n" 1322 "} else if (b) g();\n" 1323 "else g();", 1324 AllowsMergedIf); 1325 verifyFormat("if (a) g();\n" 1326 "else if (b) {\n" 1327 " g();\n" 1328 "} else g();", 1329 AllowsMergedIf); 1330 verifyFormat("if (a) g();\n" 1331 "else if (b) g();\n" 1332 "else {\n" 1333 " g();\n" 1334 "}", 1335 AllowsMergedIf); 1336 verifyFormat("if (a) g();\n" 1337 "else if (b) {\n" 1338 " g();\n" 1339 "} else {\n" 1340 " g();\n" 1341 "}", 1342 AllowsMergedIf); 1343 verifyFormat("if (a) {\n" 1344 " g();\n" 1345 "} else if (b) {\n" 1346 " g();\n" 1347 "} else {\n" 1348 " g();\n" 1349 "}", 1350 AllowsMergedIf); 1351 verifyFormat("MYIF (a) f();\n" 1352 "else {\n" 1353 " g();\n" 1354 "}", 1355 AllowsMergedIf); 1356 verifyFormat("MYIF (a) f();\n" 1357 "else {\n" 1358 " if (a) f();\n" 1359 " else {\n" 1360 " g();\n" 1361 " }\n" 1362 " g();\n" 1363 "}", 1364 AllowsMergedIf); 1365 1366 verifyFormat("MYIF (a) g();", AllowsMergedIf); 1367 verifyFormat("MYIF (a) {\n" 1368 " g()\n" 1369 "};", 1370 AllowsMergedIf); 1371 verifyFormat("MYIF (a) g();\n" 1372 "else g();", 1373 AllowsMergedIf); 1374 verifyFormat("MYIF (a) {\n" 1375 " g();\n" 1376 "} else g();", 1377 AllowsMergedIf); 1378 verifyFormat("MYIF (a) g();\n" 1379 "else {\n" 1380 " g();\n" 1381 "}", 1382 AllowsMergedIf); 1383 verifyFormat("MYIF (a) {\n" 1384 " g();\n" 1385 "} else {\n" 1386 " g();\n" 1387 "}", 1388 AllowsMergedIf); 1389 verifyFormat("MYIF (a) g();\n" 1390 "else MYIF (b) g();\n" 1391 "else g();", 1392 AllowsMergedIf); 1393 verifyFormat("MYIF (a) g();\n" 1394 "else if (b) g();\n" 1395 "else g();", 1396 AllowsMergedIf); 1397 verifyFormat("MYIF (a) {\n" 1398 " g();\n" 1399 "} else MYIF (b) g();\n" 1400 "else g();", 1401 AllowsMergedIf); 1402 verifyFormat("MYIF (a) {\n" 1403 " g();\n" 1404 "} else if (b) g();\n" 1405 "else g();", 1406 AllowsMergedIf); 1407 verifyFormat("MYIF (a) g();\n" 1408 "else MYIF (b) {\n" 1409 " g();\n" 1410 "} else g();", 1411 AllowsMergedIf); 1412 verifyFormat("MYIF (a) g();\n" 1413 "else if (b) {\n" 1414 " g();\n" 1415 "} else g();", 1416 AllowsMergedIf); 1417 verifyFormat("MYIF (a) g();\n" 1418 "else MYIF (b) g();\n" 1419 "else {\n" 1420 " g();\n" 1421 "}", 1422 AllowsMergedIf); 1423 verifyFormat("MYIF (a) g();\n" 1424 "else if (b) g();\n" 1425 "else {\n" 1426 " g();\n" 1427 "}", 1428 AllowsMergedIf); 1429 verifyFormat("MYIF (a) g();\n" 1430 "else MYIF (b) {\n" 1431 " g();\n" 1432 "} else {\n" 1433 " g();\n" 1434 "}", 1435 AllowsMergedIf); 1436 verifyFormat("MYIF (a) g();\n" 1437 "else if (b) {\n" 1438 " g();\n" 1439 "} else {\n" 1440 " g();\n" 1441 "}", 1442 AllowsMergedIf); 1443 verifyFormat("MYIF (a) {\n" 1444 " g();\n" 1445 "} else MYIF (b) {\n" 1446 " g();\n" 1447 "} else {\n" 1448 " g();\n" 1449 "}", 1450 AllowsMergedIf); 1451 verifyFormat("MYIF (a) {\n" 1452 " g();\n" 1453 "} else if (b) {\n" 1454 " g();\n" 1455 "} else {\n" 1456 " g();\n" 1457 "}", 1458 AllowsMergedIf); 1459 } 1460 1461 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) { 1462 FormatStyle AllowsMergedLoops = getLLVMStyle(); 1463 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true; 1464 verifyFormat("while (true) continue;", AllowsMergedLoops); 1465 verifyFormat("for (;;) continue;", AllowsMergedLoops); 1466 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops); 1467 verifyFormat("while (true)\n" 1468 " ;", 1469 AllowsMergedLoops); 1470 verifyFormat("for (;;)\n" 1471 " ;", 1472 AllowsMergedLoops); 1473 verifyFormat("for (;;)\n" 1474 " for (;;) continue;", 1475 AllowsMergedLoops); 1476 verifyFormat("for (;;) // Can't merge this\n" 1477 " continue;", 1478 AllowsMergedLoops); 1479 verifyFormat("for (;;) /* still don't merge */\n" 1480 " continue;", 1481 AllowsMergedLoops); 1482 verifyFormat("do a++;\n" 1483 "while (true);", 1484 AllowsMergedLoops); 1485 verifyFormat("do /* Don't merge */\n" 1486 " a++;\n" 1487 "while (true);", 1488 AllowsMergedLoops); 1489 verifyFormat("do // Don't merge\n" 1490 " a++;\n" 1491 "while (true);", 1492 AllowsMergedLoops); 1493 verifyFormat("do\n" 1494 " // Don't merge\n" 1495 " a++;\n" 1496 "while (true);", 1497 AllowsMergedLoops); 1498 // Without braces labels are interpreted differently. 1499 verifyFormat("{\n" 1500 " do\n" 1501 " label:\n" 1502 " a++;\n" 1503 " while (true);\n" 1504 "}", 1505 AllowsMergedLoops); 1506 } 1507 1508 TEST_F(FormatTest, FormatShortBracedStatements) { 1509 FormatStyle AllowSimpleBracedStatements = getLLVMStyle(); 1510 AllowSimpleBracedStatements.IfMacros.push_back("MYIF"); 1511 // Where line-lengths matter, a 2-letter synonym that maintains line length. 1512 // Not IF to avoid any confusion that IF is somehow special. 1513 AllowSimpleBracedStatements.IfMacros.push_back("FI"); 1514 AllowSimpleBracedStatements.ColumnLimit = 40; 1515 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = 1516 FormatStyle::SBS_Always; 1517 1518 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1519 FormatStyle::SIS_WithoutElse; 1520 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 1521 1522 AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom; 1523 AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true; 1524 AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false; 1525 1526 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1527 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 1528 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1529 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1530 verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements); 1531 verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1532 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1533 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1534 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 1535 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 1536 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1537 verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements); 1538 verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements); 1539 verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1540 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 1541 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 1542 verifyFormat("if (true) { fffffffffffffffffffffff(); }", 1543 AllowSimpleBracedStatements); 1544 verifyFormat("if (true) {\n" 1545 " ffffffffffffffffffffffff();\n" 1546 "}", 1547 AllowSimpleBracedStatements); 1548 verifyFormat("if (true) {\n" 1549 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1550 "}", 1551 AllowSimpleBracedStatements); 1552 verifyFormat("if (true) { //\n" 1553 " f();\n" 1554 "}", 1555 AllowSimpleBracedStatements); 1556 verifyFormat("if (true) {\n" 1557 " f();\n" 1558 " f();\n" 1559 "}", 1560 AllowSimpleBracedStatements); 1561 verifyFormat("if (true) {\n" 1562 " f();\n" 1563 "} else {\n" 1564 " f();\n" 1565 "}", 1566 AllowSimpleBracedStatements); 1567 verifyFormat("FI (true) { fffffffffffffffffffffff(); }", 1568 AllowSimpleBracedStatements); 1569 verifyFormat("MYIF (true) {\n" 1570 " ffffffffffffffffffffffff();\n" 1571 "}", 1572 AllowSimpleBracedStatements); 1573 verifyFormat("MYIF (true) {\n" 1574 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1575 "}", 1576 AllowSimpleBracedStatements); 1577 verifyFormat("MYIF (true) { //\n" 1578 " f();\n" 1579 "}", 1580 AllowSimpleBracedStatements); 1581 verifyFormat("MYIF (true) {\n" 1582 " f();\n" 1583 " f();\n" 1584 "}", 1585 AllowSimpleBracedStatements); 1586 verifyFormat("MYIF (true) {\n" 1587 " f();\n" 1588 "} else {\n" 1589 " f();\n" 1590 "}", 1591 AllowSimpleBracedStatements); 1592 1593 verifyFormat("struct A2 {\n" 1594 " int X;\n" 1595 "};", 1596 AllowSimpleBracedStatements); 1597 verifyFormat("typedef struct A2 {\n" 1598 " int X;\n" 1599 "} A2_t;", 1600 AllowSimpleBracedStatements); 1601 verifyFormat("template <int> struct A2 {\n" 1602 " struct B {};\n" 1603 "};", 1604 AllowSimpleBracedStatements); 1605 1606 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1607 FormatStyle::SIS_Never; 1608 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1609 verifyFormat("if (true) {\n" 1610 " f();\n" 1611 "}", 1612 AllowSimpleBracedStatements); 1613 verifyFormat("if (true) {\n" 1614 " f();\n" 1615 "} else {\n" 1616 " f();\n" 1617 "}", 1618 AllowSimpleBracedStatements); 1619 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1620 verifyFormat("MYIF (true) {\n" 1621 " f();\n" 1622 "}", 1623 AllowSimpleBracedStatements); 1624 verifyFormat("MYIF (true) {\n" 1625 " f();\n" 1626 "} else {\n" 1627 " f();\n" 1628 "}", 1629 AllowSimpleBracedStatements); 1630 1631 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 1632 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1633 verifyFormat("while (true) {\n" 1634 " f();\n" 1635 "}", 1636 AllowSimpleBracedStatements); 1637 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1638 verifyFormat("for (;;) {\n" 1639 " f();\n" 1640 "}", 1641 AllowSimpleBracedStatements); 1642 1643 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1644 FormatStyle::SIS_WithoutElse; 1645 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 1646 AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement = 1647 FormatStyle::BWACS_Always; 1648 1649 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1650 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 1651 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1652 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1653 verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements); 1654 verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1655 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1656 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1657 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 1658 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 1659 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1660 verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements); 1661 verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements); 1662 verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1663 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 1664 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 1665 verifyFormat("if (true) { fffffffffffffffffffffff(); }", 1666 AllowSimpleBracedStatements); 1667 verifyFormat("if (true)\n" 1668 "{\n" 1669 " ffffffffffffffffffffffff();\n" 1670 "}", 1671 AllowSimpleBracedStatements); 1672 verifyFormat("if (true)\n" 1673 "{\n" 1674 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1675 "}", 1676 AllowSimpleBracedStatements); 1677 verifyFormat("if (true)\n" 1678 "{ //\n" 1679 " f();\n" 1680 "}", 1681 AllowSimpleBracedStatements); 1682 verifyFormat("if (true)\n" 1683 "{\n" 1684 " f();\n" 1685 " f();\n" 1686 "}", 1687 AllowSimpleBracedStatements); 1688 verifyFormat("if (true)\n" 1689 "{\n" 1690 " f();\n" 1691 "} else\n" 1692 "{\n" 1693 " f();\n" 1694 "}", 1695 AllowSimpleBracedStatements); 1696 verifyFormat("FI (true) { fffffffffffffffffffffff(); }", 1697 AllowSimpleBracedStatements); 1698 verifyFormat("MYIF (true)\n" 1699 "{\n" 1700 " ffffffffffffffffffffffff();\n" 1701 "}", 1702 AllowSimpleBracedStatements); 1703 verifyFormat("MYIF (true)\n" 1704 "{\n" 1705 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1706 "}", 1707 AllowSimpleBracedStatements); 1708 verifyFormat("MYIF (true)\n" 1709 "{ //\n" 1710 " f();\n" 1711 "}", 1712 AllowSimpleBracedStatements); 1713 verifyFormat("MYIF (true)\n" 1714 "{\n" 1715 " f();\n" 1716 " f();\n" 1717 "}", 1718 AllowSimpleBracedStatements); 1719 verifyFormat("MYIF (true)\n" 1720 "{\n" 1721 " f();\n" 1722 "} else\n" 1723 "{\n" 1724 " f();\n" 1725 "}", 1726 AllowSimpleBracedStatements); 1727 1728 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1729 FormatStyle::SIS_Never; 1730 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1731 verifyFormat("if (true)\n" 1732 "{\n" 1733 " f();\n" 1734 "}", 1735 AllowSimpleBracedStatements); 1736 verifyFormat("if (true)\n" 1737 "{\n" 1738 " f();\n" 1739 "} else\n" 1740 "{\n" 1741 " f();\n" 1742 "}", 1743 AllowSimpleBracedStatements); 1744 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1745 verifyFormat("MYIF (true)\n" 1746 "{\n" 1747 " f();\n" 1748 "}", 1749 AllowSimpleBracedStatements); 1750 verifyFormat("MYIF (true)\n" 1751 "{\n" 1752 " f();\n" 1753 "} else\n" 1754 "{\n" 1755 " f();\n" 1756 "}", 1757 AllowSimpleBracedStatements); 1758 1759 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 1760 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1761 verifyFormat("while (true)\n" 1762 "{\n" 1763 " f();\n" 1764 "}", 1765 AllowSimpleBracedStatements); 1766 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1767 verifyFormat("for (;;)\n" 1768 "{\n" 1769 " f();\n" 1770 "}", 1771 AllowSimpleBracedStatements); 1772 } 1773 1774 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) { 1775 FormatStyle Style = getLLVMStyleWithColumns(60); 1776 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 1777 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; 1778 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 1779 EXPECT_EQ("#define A \\\n" 1780 " if (HANDLEwernufrnuLwrmviferuvnierv) \\\n" 1781 " { \\\n" 1782 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" 1783 " }\n" 1784 "X;", 1785 format("#define A \\\n" 1786 " if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n" 1787 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" 1788 " }\n" 1789 "X;", 1790 Style)); 1791 } 1792 1793 TEST_F(FormatTest, ParseIfElse) { 1794 verifyFormat("if (true)\n" 1795 " if (true)\n" 1796 " if (true)\n" 1797 " f();\n" 1798 " else\n" 1799 " g();\n" 1800 " else\n" 1801 " h();\n" 1802 "else\n" 1803 " i();"); 1804 verifyFormat("if (true)\n" 1805 " if (true)\n" 1806 " if (true) {\n" 1807 " if (true)\n" 1808 " f();\n" 1809 " } else {\n" 1810 " g();\n" 1811 " }\n" 1812 " else\n" 1813 " h();\n" 1814 "else {\n" 1815 " i();\n" 1816 "}"); 1817 verifyFormat("if (true)\n" 1818 " if constexpr (true)\n" 1819 " if (true) {\n" 1820 " if constexpr (true)\n" 1821 " f();\n" 1822 " } else {\n" 1823 " g();\n" 1824 " }\n" 1825 " else\n" 1826 " h();\n" 1827 "else {\n" 1828 " i();\n" 1829 "}"); 1830 verifyFormat("if (true)\n" 1831 " if CONSTEXPR (true)\n" 1832 " if (true) {\n" 1833 " if CONSTEXPR (true)\n" 1834 " f();\n" 1835 " } else {\n" 1836 " g();\n" 1837 " }\n" 1838 " else\n" 1839 " h();\n" 1840 "else {\n" 1841 " i();\n" 1842 "}"); 1843 verifyFormat("void f() {\n" 1844 " if (a) {\n" 1845 " } else {\n" 1846 " }\n" 1847 "}"); 1848 } 1849 1850 TEST_F(FormatTest, ElseIf) { 1851 verifyFormat("if (a) {\n} else if (b) {\n}"); 1852 verifyFormat("if (a)\n" 1853 " f();\n" 1854 "else if (b)\n" 1855 " g();\n" 1856 "else\n" 1857 " h();"); 1858 verifyFormat("if (a)\n" 1859 " f();\n" 1860 "else // comment\n" 1861 " if (b) {\n" 1862 " g();\n" 1863 " h();\n" 1864 " }"); 1865 verifyFormat("if constexpr (a)\n" 1866 " f();\n" 1867 "else if constexpr (b)\n" 1868 " g();\n" 1869 "else\n" 1870 " h();"); 1871 verifyFormat("if CONSTEXPR (a)\n" 1872 " f();\n" 1873 "else if CONSTEXPR (b)\n" 1874 " g();\n" 1875 "else\n" 1876 " h();"); 1877 verifyFormat("if (a) {\n" 1878 " f();\n" 1879 "}\n" 1880 "// or else ..\n" 1881 "else {\n" 1882 " g()\n" 1883 "}"); 1884 1885 verifyFormat("if (a) {\n" 1886 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1887 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 1888 "}"); 1889 verifyFormat("if (a) {\n" 1890 "} else if constexpr (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1891 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 1892 "}"); 1893 verifyFormat("if (a) {\n" 1894 "} else if CONSTEXPR (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1895 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 1896 "}"); 1897 verifyFormat("if (a) {\n" 1898 "} else if (\n" 1899 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 1900 "}", 1901 getLLVMStyleWithColumns(62)); 1902 verifyFormat("if (a) {\n" 1903 "} else if constexpr (\n" 1904 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 1905 "}", 1906 getLLVMStyleWithColumns(62)); 1907 verifyFormat("if (a) {\n" 1908 "} else if CONSTEXPR (\n" 1909 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 1910 "}", 1911 getLLVMStyleWithColumns(62)); 1912 } 1913 1914 TEST_F(FormatTest, SeparatePointerReferenceAlignment) { 1915 FormatStyle Style = getLLVMStyle(); 1916 // Check first the default LLVM style 1917 // Style.PointerAlignment = FormatStyle::PAS_Right; 1918 // Style.ReferenceAlignment = FormatStyle::RAS_Pointer; 1919 verifyFormat("int *f1(int *a, int &b, int &&c);", Style); 1920 verifyFormat("int &f2(int &&c, int *a, int &b);", Style); 1921 verifyFormat("int &&f3(int &b, int &&c, int *a);", Style); 1922 verifyFormat("int *f1(int &a) const &;", Style); 1923 verifyFormat("int *f1(int &a) const & = 0;", Style); 1924 verifyFormat("int *a = f1();", Style); 1925 verifyFormat("int &b = f2();", Style); 1926 verifyFormat("int &&c = f3();", Style); 1927 1928 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 1929 verifyFormat("Const unsigned int *c;\n" 1930 "const unsigned int *d;\n" 1931 "Const unsigned int &e;\n" 1932 "const unsigned int &f;\n" 1933 "const unsigned &&g;\n" 1934 "Const unsigned h;", 1935 Style); 1936 1937 Style.PointerAlignment = FormatStyle::PAS_Left; 1938 Style.ReferenceAlignment = FormatStyle::RAS_Pointer; 1939 verifyFormat("int* f1(int* a, int& b, int&& c);", Style); 1940 verifyFormat("int& f2(int&& c, int* a, int& b);", Style); 1941 verifyFormat("int&& f3(int& b, int&& c, int* a);", Style); 1942 verifyFormat("int* f1(int& a) const& = 0;", Style); 1943 verifyFormat("int* a = f1();", Style); 1944 verifyFormat("int& b = f2();", Style); 1945 verifyFormat("int&& c = f3();", Style); 1946 1947 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 1948 verifyFormat("Const unsigned int* c;\n" 1949 "const unsigned int* d;\n" 1950 "Const unsigned int& e;\n" 1951 "const unsigned int& f;\n" 1952 "const unsigned&& g;\n" 1953 "Const unsigned h;", 1954 Style); 1955 1956 Style.PointerAlignment = FormatStyle::PAS_Right; 1957 Style.ReferenceAlignment = FormatStyle::RAS_Left; 1958 verifyFormat("int *f1(int *a, int& b, int&& c);", Style); 1959 verifyFormat("int& f2(int&& c, int *a, int& b);", Style); 1960 verifyFormat("int&& f3(int& b, int&& c, int *a);", Style); 1961 verifyFormat("int *a = f1();", Style); 1962 verifyFormat("int& b = f2();", Style); 1963 verifyFormat("int&& c = f3();", Style); 1964 1965 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 1966 verifyFormat("Const unsigned int *c;\n" 1967 "const unsigned int *d;\n" 1968 "Const unsigned int& e;\n" 1969 "const unsigned int& f;\n" 1970 "const unsigned g;\n" 1971 "Const unsigned h;", 1972 Style); 1973 1974 Style.PointerAlignment = FormatStyle::PAS_Left; 1975 Style.ReferenceAlignment = FormatStyle::RAS_Middle; 1976 verifyFormat("int* f1(int* a, int & b, int && c);", Style); 1977 verifyFormat("int & f2(int && c, int* a, int & b);", Style); 1978 verifyFormat("int && f3(int & b, int && c, int* a);", Style); 1979 verifyFormat("int* a = f1();", Style); 1980 verifyFormat("int & b = f2();", Style); 1981 verifyFormat("int && c = f3();", Style); 1982 1983 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 1984 verifyFormat("Const unsigned int* c;\n" 1985 "const unsigned int* d;\n" 1986 "Const unsigned int & e;\n" 1987 "const unsigned int & f;\n" 1988 "const unsigned && g;\n" 1989 "Const unsigned h;", 1990 Style); 1991 1992 Style.PointerAlignment = FormatStyle::PAS_Middle; 1993 Style.ReferenceAlignment = FormatStyle::RAS_Right; 1994 verifyFormat("int * f1(int * a, int &b, int &&c);", Style); 1995 verifyFormat("int &f2(int &&c, int * a, int &b);", Style); 1996 verifyFormat("int &&f3(int &b, int &&c, int * a);", Style); 1997 verifyFormat("int * a = f1();", Style); 1998 verifyFormat("int &b = f2();", Style); 1999 verifyFormat("int &&c = f3();", Style); 2000 2001 // FIXME: we don't handle this yet, so output may be arbitrary until it's 2002 // specifically handled 2003 // verifyFormat("int Add2(BTree * &Root, char * szToAdd)", Style); 2004 } 2005 2006 TEST_F(FormatTest, FormatsForLoop) { 2007 verifyFormat( 2008 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 2009 " ++VeryVeryLongLoopVariable)\n" 2010 " ;"); 2011 verifyFormat("for (;;)\n" 2012 " f();"); 2013 verifyFormat("for (;;) {\n}"); 2014 verifyFormat("for (;;) {\n" 2015 " f();\n" 2016 "}"); 2017 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}"); 2018 2019 verifyFormat( 2020 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 2021 " E = UnwrappedLines.end();\n" 2022 " I != E; ++I) {\n}"); 2023 2024 verifyFormat( 2025 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" 2026 " ++IIIII) {\n}"); 2027 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" 2028 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" 2029 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); 2030 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n" 2031 " I = FD->getDeclsInPrototypeScope().begin(),\n" 2032 " E = FD->getDeclsInPrototypeScope().end();\n" 2033 " I != E; ++I) {\n}"); 2034 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n" 2035 " I = Container.begin(),\n" 2036 " E = Container.end();\n" 2037 " I != E; ++I) {\n}", 2038 getLLVMStyleWithColumns(76)); 2039 2040 verifyFormat( 2041 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 2042 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" 2043 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2044 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 2045 " ++aaaaaaaaaaa) {\n}"); 2046 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2047 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n" 2048 " ++i) {\n}"); 2049 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" 2050 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 2051 "}"); 2052 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n" 2053 " aaaaaaaaaa);\n" 2054 " iter; ++iter) {\n" 2055 "}"); 2056 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2057 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 2058 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n" 2059 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {"); 2060 2061 // These should not be formatted as Objective-C for-in loops. 2062 verifyFormat("for (Foo *x = 0; x != in; x++) {\n}"); 2063 verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}"); 2064 verifyFormat("Foo *x;\nfor (x in y) {\n}"); 2065 verifyFormat( 2066 "for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}"); 2067 2068 FormatStyle NoBinPacking = getLLVMStyle(); 2069 NoBinPacking.BinPackParameters = false; 2070 verifyFormat("for (int aaaaaaaaaaa = 1;\n" 2071 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" 2072 " aaaaaaaaaaaaaaaa,\n" 2073 " aaaaaaaaaaaaaaaa,\n" 2074 " aaaaaaaaaaaaaaaa);\n" 2075 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 2076 "}", 2077 NoBinPacking); 2078 verifyFormat( 2079 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 2080 " E = UnwrappedLines.end();\n" 2081 " I != E;\n" 2082 " ++I) {\n}", 2083 NoBinPacking); 2084 2085 FormatStyle AlignLeft = getLLVMStyle(); 2086 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 2087 verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft); 2088 } 2089 2090 TEST_F(FormatTest, RangeBasedForLoops) { 2091 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 2092 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 2093 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" 2094 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); 2095 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" 2096 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 2097 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n" 2098 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}"); 2099 } 2100 2101 TEST_F(FormatTest, ForEachLoops) { 2102 verifyFormat("void f() {\n" 2103 " foreach (Item *item, itemlist) {}\n" 2104 " Q_FOREACH (Item *item, itemlist) {}\n" 2105 " BOOST_FOREACH (Item *item, itemlist) {}\n" 2106 " UNKNOWN_FORACH(Item * item, itemlist) {}\n" 2107 "}"); 2108 2109 FormatStyle Style = getLLVMStyle(); 2110 Style.SpaceBeforeParens = 2111 FormatStyle::SBPO_ControlStatementsExceptControlMacros; 2112 verifyFormat("void f() {\n" 2113 " foreach(Item *item, itemlist) {}\n" 2114 " Q_FOREACH(Item *item, itemlist) {}\n" 2115 " BOOST_FOREACH(Item *item, itemlist) {}\n" 2116 " UNKNOWN_FORACH(Item * item, itemlist) {}\n" 2117 "}", 2118 Style); 2119 2120 // As function-like macros. 2121 verifyFormat("#define foreach(x, y)\n" 2122 "#define Q_FOREACH(x, y)\n" 2123 "#define BOOST_FOREACH(x, y)\n" 2124 "#define UNKNOWN_FOREACH(x, y)\n"); 2125 2126 // Not as function-like macros. 2127 verifyFormat("#define foreach (x, y)\n" 2128 "#define Q_FOREACH (x, y)\n" 2129 "#define BOOST_FOREACH (x, y)\n" 2130 "#define UNKNOWN_FOREACH (x, y)\n"); 2131 2132 // handle microsoft non standard extension 2133 verifyFormat("for each (char c in x->MyStringProperty)"); 2134 } 2135 2136 TEST_F(FormatTest, FormatsWhileLoop) { 2137 verifyFormat("while (true) {\n}"); 2138 verifyFormat("while (true)\n" 2139 " f();"); 2140 verifyFormat("while () {\n}"); 2141 verifyFormat("while () {\n" 2142 " f();\n" 2143 "}"); 2144 } 2145 2146 TEST_F(FormatTest, FormatsDoWhile) { 2147 verifyFormat("do {\n" 2148 " do_something();\n" 2149 "} while (something());"); 2150 verifyFormat("do\n" 2151 " do_something();\n" 2152 "while (something());"); 2153 } 2154 2155 TEST_F(FormatTest, FormatsSwitchStatement) { 2156 verifyFormat("switch (x) {\n" 2157 "case 1:\n" 2158 " f();\n" 2159 " break;\n" 2160 "case kFoo:\n" 2161 "case ns::kBar:\n" 2162 "case kBaz:\n" 2163 " break;\n" 2164 "default:\n" 2165 " g();\n" 2166 " break;\n" 2167 "}"); 2168 verifyFormat("switch (x) {\n" 2169 "case 1: {\n" 2170 " f();\n" 2171 " break;\n" 2172 "}\n" 2173 "case 2: {\n" 2174 " break;\n" 2175 "}\n" 2176 "}"); 2177 verifyFormat("switch (x) {\n" 2178 "case 1: {\n" 2179 " f();\n" 2180 " {\n" 2181 " g();\n" 2182 " h();\n" 2183 " }\n" 2184 " break;\n" 2185 "}\n" 2186 "}"); 2187 verifyFormat("switch (x) {\n" 2188 "case 1: {\n" 2189 " f();\n" 2190 " if (foo) {\n" 2191 " g();\n" 2192 " h();\n" 2193 " }\n" 2194 " break;\n" 2195 "}\n" 2196 "}"); 2197 verifyFormat("switch (x) {\n" 2198 "case 1: {\n" 2199 " f();\n" 2200 " g();\n" 2201 "} break;\n" 2202 "}"); 2203 verifyFormat("switch (test)\n" 2204 " ;"); 2205 verifyFormat("switch (x) {\n" 2206 "default: {\n" 2207 " // Do nothing.\n" 2208 "}\n" 2209 "}"); 2210 verifyFormat("switch (x) {\n" 2211 "// comment\n" 2212 "// if 1, do f()\n" 2213 "case 1:\n" 2214 " f();\n" 2215 "}"); 2216 verifyFormat("switch (x) {\n" 2217 "case 1:\n" 2218 " // Do amazing stuff\n" 2219 " {\n" 2220 " f();\n" 2221 " g();\n" 2222 " }\n" 2223 " break;\n" 2224 "}"); 2225 verifyFormat("#define A \\\n" 2226 " switch (x) { \\\n" 2227 " case a: \\\n" 2228 " foo = b; \\\n" 2229 " }", 2230 getLLVMStyleWithColumns(20)); 2231 verifyFormat("#define OPERATION_CASE(name) \\\n" 2232 " case OP_name: \\\n" 2233 " return operations::Operation##name\n", 2234 getLLVMStyleWithColumns(40)); 2235 verifyFormat("switch (x) {\n" 2236 "case 1:;\n" 2237 "default:;\n" 2238 " int i;\n" 2239 "}"); 2240 2241 verifyGoogleFormat("switch (x) {\n" 2242 " case 1:\n" 2243 " f();\n" 2244 " break;\n" 2245 " case kFoo:\n" 2246 " case ns::kBar:\n" 2247 " case kBaz:\n" 2248 " break;\n" 2249 " default:\n" 2250 " g();\n" 2251 " break;\n" 2252 "}"); 2253 verifyGoogleFormat("switch (x) {\n" 2254 " case 1: {\n" 2255 " f();\n" 2256 " break;\n" 2257 " }\n" 2258 "}"); 2259 verifyGoogleFormat("switch (test)\n" 2260 " ;"); 2261 2262 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n" 2263 " case OP_name: \\\n" 2264 " return operations::Operation##name\n"); 2265 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n" 2266 " // Get the correction operation class.\n" 2267 " switch (OpCode) {\n" 2268 " CASE(Add);\n" 2269 " CASE(Subtract);\n" 2270 " default:\n" 2271 " return operations::Unknown;\n" 2272 " }\n" 2273 "#undef OPERATION_CASE\n" 2274 "}"); 2275 verifyFormat("DEBUG({\n" 2276 " switch (x) {\n" 2277 " case A:\n" 2278 " f();\n" 2279 " break;\n" 2280 " // fallthrough\n" 2281 " case B:\n" 2282 " g();\n" 2283 " break;\n" 2284 " }\n" 2285 "});"); 2286 EXPECT_EQ("DEBUG({\n" 2287 " switch (x) {\n" 2288 " case A:\n" 2289 " f();\n" 2290 " break;\n" 2291 " // On B:\n" 2292 " case B:\n" 2293 " g();\n" 2294 " break;\n" 2295 " }\n" 2296 "});", 2297 format("DEBUG({\n" 2298 " switch (x) {\n" 2299 " case A:\n" 2300 " f();\n" 2301 " break;\n" 2302 " // On B:\n" 2303 " case B:\n" 2304 " g();\n" 2305 " break;\n" 2306 " }\n" 2307 "});", 2308 getLLVMStyle())); 2309 EXPECT_EQ("switch (n) {\n" 2310 "case 0: {\n" 2311 " return false;\n" 2312 "}\n" 2313 "default: {\n" 2314 " return true;\n" 2315 "}\n" 2316 "}", 2317 format("switch (n)\n" 2318 "{\n" 2319 "case 0: {\n" 2320 " return false;\n" 2321 "}\n" 2322 "default: {\n" 2323 " return true;\n" 2324 "}\n" 2325 "}", 2326 getLLVMStyle())); 2327 verifyFormat("switch (a) {\n" 2328 "case (b):\n" 2329 " return;\n" 2330 "}"); 2331 2332 verifyFormat("switch (a) {\n" 2333 "case some_namespace::\n" 2334 " some_constant:\n" 2335 " return;\n" 2336 "}", 2337 getLLVMStyleWithColumns(34)); 2338 2339 FormatStyle Style = getLLVMStyle(); 2340 Style.IndentCaseLabels = true; 2341 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 2342 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2343 Style.BraceWrapping.AfterCaseLabel = true; 2344 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 2345 EXPECT_EQ("switch (n)\n" 2346 "{\n" 2347 " case 0:\n" 2348 " {\n" 2349 " return false;\n" 2350 " }\n" 2351 " default:\n" 2352 " {\n" 2353 " return true;\n" 2354 " }\n" 2355 "}", 2356 format("switch (n) {\n" 2357 " case 0: {\n" 2358 " return false;\n" 2359 " }\n" 2360 " default: {\n" 2361 " return true;\n" 2362 " }\n" 2363 "}", 2364 Style)); 2365 Style.BraceWrapping.AfterCaseLabel = false; 2366 EXPECT_EQ("switch (n)\n" 2367 "{\n" 2368 " case 0: {\n" 2369 " return false;\n" 2370 " }\n" 2371 " default: {\n" 2372 " return true;\n" 2373 " }\n" 2374 "}", 2375 format("switch (n) {\n" 2376 " case 0:\n" 2377 " {\n" 2378 " return false;\n" 2379 " }\n" 2380 " default:\n" 2381 " {\n" 2382 " return true;\n" 2383 " }\n" 2384 "}", 2385 Style)); 2386 Style.IndentCaseLabels = false; 2387 Style.IndentCaseBlocks = true; 2388 EXPECT_EQ("switch (n)\n" 2389 "{\n" 2390 "case 0:\n" 2391 " {\n" 2392 " return false;\n" 2393 " }\n" 2394 "case 1:\n" 2395 " break;\n" 2396 "default:\n" 2397 " {\n" 2398 " return true;\n" 2399 " }\n" 2400 "}", 2401 format("switch (n) {\n" 2402 "case 0: {\n" 2403 " return false;\n" 2404 "}\n" 2405 "case 1:\n" 2406 " break;\n" 2407 "default: {\n" 2408 " return true;\n" 2409 "}\n" 2410 "}", 2411 Style)); 2412 Style.IndentCaseLabels = true; 2413 Style.IndentCaseBlocks = true; 2414 EXPECT_EQ("switch (n)\n" 2415 "{\n" 2416 " case 0:\n" 2417 " {\n" 2418 " return false;\n" 2419 " }\n" 2420 " case 1:\n" 2421 " break;\n" 2422 " default:\n" 2423 " {\n" 2424 " return true;\n" 2425 " }\n" 2426 "}", 2427 format("switch (n) {\n" 2428 "case 0: {\n" 2429 " return false;\n" 2430 "}\n" 2431 "case 1:\n" 2432 " break;\n" 2433 "default: {\n" 2434 " return true;\n" 2435 "}\n" 2436 "}", 2437 Style)); 2438 } 2439 2440 TEST_F(FormatTest, CaseRanges) { 2441 verifyFormat("switch (x) {\n" 2442 "case 'A' ... 'Z':\n" 2443 "case 1 ... 5:\n" 2444 "case a ... b:\n" 2445 " break;\n" 2446 "}"); 2447 } 2448 2449 TEST_F(FormatTest, ShortEnums) { 2450 FormatStyle Style = getLLVMStyle(); 2451 Style.AllowShortEnumsOnASingleLine = true; 2452 verifyFormat("enum { A, B, C } ShortEnum1, ShortEnum2;", Style); 2453 Style.AllowShortEnumsOnASingleLine = false; 2454 verifyFormat("enum {\n" 2455 " A,\n" 2456 " B,\n" 2457 " C\n" 2458 "} ShortEnum1, ShortEnum2;", 2459 Style); 2460 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2461 Style.BraceWrapping.AfterEnum = true; 2462 verifyFormat("enum\n" 2463 "{\n" 2464 " A,\n" 2465 " B,\n" 2466 " C\n" 2467 "} ShortEnum1, ShortEnum2;", 2468 Style); 2469 } 2470 2471 TEST_F(FormatTest, ShortCaseLabels) { 2472 FormatStyle Style = getLLVMStyle(); 2473 Style.AllowShortCaseLabelsOnASingleLine = true; 2474 verifyFormat("switch (a) {\n" 2475 "case 1: x = 1; break;\n" 2476 "case 2: return;\n" 2477 "case 3:\n" 2478 "case 4:\n" 2479 "case 5: return;\n" 2480 "case 6: // comment\n" 2481 " return;\n" 2482 "case 7:\n" 2483 " // comment\n" 2484 " return;\n" 2485 "case 8:\n" 2486 " x = 8; // comment\n" 2487 " break;\n" 2488 "default: y = 1; break;\n" 2489 "}", 2490 Style); 2491 verifyFormat("switch (a) {\n" 2492 "case 0: return; // comment\n" 2493 "case 1: break; // comment\n" 2494 "case 2: return;\n" 2495 "// comment\n" 2496 "case 3: return;\n" 2497 "// comment 1\n" 2498 "// comment 2\n" 2499 "// comment 3\n" 2500 "case 4: break; /* comment */\n" 2501 "case 5:\n" 2502 " // comment\n" 2503 " break;\n" 2504 "case 6: /* comment */ x = 1; break;\n" 2505 "case 7: x = /* comment */ 1; break;\n" 2506 "case 8:\n" 2507 " x = 1; /* comment */\n" 2508 " break;\n" 2509 "case 9:\n" 2510 " break; // comment line 1\n" 2511 " // comment line 2\n" 2512 "}", 2513 Style); 2514 EXPECT_EQ("switch (a) {\n" 2515 "case 1:\n" 2516 " x = 8;\n" 2517 " // fall through\n" 2518 "case 2: x = 8;\n" 2519 "// comment\n" 2520 "case 3:\n" 2521 " return; /* comment line 1\n" 2522 " * comment line 2 */\n" 2523 "case 4: i = 8;\n" 2524 "// something else\n" 2525 "#if FOO\n" 2526 "case 5: break;\n" 2527 "#endif\n" 2528 "}", 2529 format("switch (a) {\n" 2530 "case 1: x = 8;\n" 2531 " // fall through\n" 2532 "case 2:\n" 2533 " x = 8;\n" 2534 "// comment\n" 2535 "case 3:\n" 2536 " return; /* comment line 1\n" 2537 " * comment line 2 */\n" 2538 "case 4:\n" 2539 " i = 8;\n" 2540 "// something else\n" 2541 "#if FOO\n" 2542 "case 5: break;\n" 2543 "#endif\n" 2544 "}", 2545 Style)); 2546 EXPECT_EQ("switch (a) {\n" 2547 "case 0:\n" 2548 " return; // long long long long long long long long long long " 2549 "long long comment\n" 2550 " // line\n" 2551 "}", 2552 format("switch (a) {\n" 2553 "case 0: return; // long long long long long long long long " 2554 "long long long long comment line\n" 2555 "}", 2556 Style)); 2557 EXPECT_EQ("switch (a) {\n" 2558 "case 0:\n" 2559 " return; /* long long long long long long long long long long " 2560 "long long comment\n" 2561 " line */\n" 2562 "}", 2563 format("switch (a) {\n" 2564 "case 0: return; /* long long long long long long long long " 2565 "long long long long comment line */\n" 2566 "}", 2567 Style)); 2568 verifyFormat("switch (a) {\n" 2569 "#if FOO\n" 2570 "case 0: return 0;\n" 2571 "#endif\n" 2572 "}", 2573 Style); 2574 verifyFormat("switch (a) {\n" 2575 "case 1: {\n" 2576 "}\n" 2577 "case 2: {\n" 2578 " return;\n" 2579 "}\n" 2580 "case 3: {\n" 2581 " x = 1;\n" 2582 " return;\n" 2583 "}\n" 2584 "case 4:\n" 2585 " if (x)\n" 2586 " return;\n" 2587 "}", 2588 Style); 2589 Style.ColumnLimit = 21; 2590 verifyFormat("switch (a) {\n" 2591 "case 1: x = 1; break;\n" 2592 "case 2: return;\n" 2593 "case 3:\n" 2594 "case 4:\n" 2595 "case 5: return;\n" 2596 "default:\n" 2597 " y = 1;\n" 2598 " break;\n" 2599 "}", 2600 Style); 2601 Style.ColumnLimit = 80; 2602 Style.AllowShortCaseLabelsOnASingleLine = false; 2603 Style.IndentCaseLabels = true; 2604 EXPECT_EQ("switch (n) {\n" 2605 " default /*comments*/:\n" 2606 " return true;\n" 2607 " case 0:\n" 2608 " return false;\n" 2609 "}", 2610 format("switch (n) {\n" 2611 "default/*comments*/:\n" 2612 " return true;\n" 2613 "case 0:\n" 2614 " return false;\n" 2615 "}", 2616 Style)); 2617 Style.AllowShortCaseLabelsOnASingleLine = true; 2618 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2619 Style.BraceWrapping.AfterCaseLabel = true; 2620 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 2621 EXPECT_EQ("switch (n)\n" 2622 "{\n" 2623 " case 0:\n" 2624 " {\n" 2625 " return false;\n" 2626 " }\n" 2627 " default:\n" 2628 " {\n" 2629 " return true;\n" 2630 " }\n" 2631 "}", 2632 format("switch (n) {\n" 2633 " case 0: {\n" 2634 " return false;\n" 2635 " }\n" 2636 " default:\n" 2637 " {\n" 2638 " return true;\n" 2639 " }\n" 2640 "}", 2641 Style)); 2642 } 2643 2644 TEST_F(FormatTest, FormatsLabels) { 2645 verifyFormat("void f() {\n" 2646 " some_code();\n" 2647 "test_label:\n" 2648 " some_other_code();\n" 2649 " {\n" 2650 " some_more_code();\n" 2651 " another_label:\n" 2652 " some_more_code();\n" 2653 " }\n" 2654 "}"); 2655 verifyFormat("{\n" 2656 " some_code();\n" 2657 "test_label:\n" 2658 " some_other_code();\n" 2659 "}"); 2660 verifyFormat("{\n" 2661 " some_code();\n" 2662 "test_label:;\n" 2663 " int i = 0;\n" 2664 "}"); 2665 FormatStyle Style = getLLVMStyle(); 2666 Style.IndentGotoLabels = false; 2667 verifyFormat("void f() {\n" 2668 " some_code();\n" 2669 "test_label:\n" 2670 " some_other_code();\n" 2671 " {\n" 2672 " some_more_code();\n" 2673 "another_label:\n" 2674 " some_more_code();\n" 2675 " }\n" 2676 "}", 2677 Style); 2678 verifyFormat("{\n" 2679 " some_code();\n" 2680 "test_label:\n" 2681 " some_other_code();\n" 2682 "}", 2683 Style); 2684 verifyFormat("{\n" 2685 " some_code();\n" 2686 "test_label:;\n" 2687 " int i = 0;\n" 2688 "}"); 2689 } 2690 2691 TEST_F(FormatTest, MultiLineControlStatements) { 2692 FormatStyle Style = getLLVMStyle(); 2693 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom; 2694 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine; 2695 Style.ColumnLimit = 20; 2696 // Short lines should keep opening brace on same line. 2697 EXPECT_EQ("if (foo) {\n" 2698 " bar();\n" 2699 "}", 2700 format("if(foo){bar();}", Style)); 2701 EXPECT_EQ("if (foo) {\n" 2702 " bar();\n" 2703 "} else {\n" 2704 " baz();\n" 2705 "}", 2706 format("if(foo){bar();}else{baz();}", Style)); 2707 EXPECT_EQ("if (foo && bar) {\n" 2708 " baz();\n" 2709 "}", 2710 format("if(foo&&bar){baz();}", Style)); 2711 EXPECT_EQ("if (foo) {\n" 2712 " bar();\n" 2713 "} else if (baz) {\n" 2714 " quux();\n" 2715 "}", 2716 format("if(foo){bar();}else if(baz){quux();}", Style)); 2717 EXPECT_EQ( 2718 "if (foo) {\n" 2719 " bar();\n" 2720 "} else if (baz) {\n" 2721 " quux();\n" 2722 "} else {\n" 2723 " foobar();\n" 2724 "}", 2725 format("if(foo){bar();}else if(baz){quux();}else{foobar();}", Style)); 2726 EXPECT_EQ("for (;;) {\n" 2727 " foo();\n" 2728 "}", 2729 format("for(;;){foo();}")); 2730 EXPECT_EQ("while (1) {\n" 2731 " foo();\n" 2732 "}", 2733 format("while(1){foo();}", Style)); 2734 EXPECT_EQ("switch (foo) {\n" 2735 "case bar:\n" 2736 " return;\n" 2737 "}", 2738 format("switch(foo){case bar:return;}", Style)); 2739 EXPECT_EQ("try {\n" 2740 " foo();\n" 2741 "} catch (...) {\n" 2742 " bar();\n" 2743 "}", 2744 format("try{foo();}catch(...){bar();}", Style)); 2745 EXPECT_EQ("do {\n" 2746 " foo();\n" 2747 "} while (bar &&\n" 2748 " baz);", 2749 format("do{foo();}while(bar&&baz);", Style)); 2750 // Long lines should put opening brace on new line. 2751 EXPECT_EQ("if (foo && bar &&\n" 2752 " baz)\n" 2753 "{\n" 2754 " quux();\n" 2755 "}", 2756 format("if(foo&&bar&&baz){quux();}", Style)); 2757 EXPECT_EQ("if (foo && bar &&\n" 2758 " baz)\n" 2759 "{\n" 2760 " quux();\n" 2761 "}", 2762 format("if (foo && bar &&\n" 2763 " baz) {\n" 2764 " quux();\n" 2765 "}", 2766 Style)); 2767 EXPECT_EQ("if (foo) {\n" 2768 " bar();\n" 2769 "} else if (baz ||\n" 2770 " quux)\n" 2771 "{\n" 2772 " foobar();\n" 2773 "}", 2774 format("if(foo){bar();}else if(baz||quux){foobar();}", Style)); 2775 EXPECT_EQ( 2776 "if (foo) {\n" 2777 " bar();\n" 2778 "} else if (baz ||\n" 2779 " quux)\n" 2780 "{\n" 2781 " foobar();\n" 2782 "} else {\n" 2783 " barbaz();\n" 2784 "}", 2785 format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}", 2786 Style)); 2787 EXPECT_EQ("for (int i = 0;\n" 2788 " i < 10; ++i)\n" 2789 "{\n" 2790 " foo();\n" 2791 "}", 2792 format("for(int i=0;i<10;++i){foo();}", Style)); 2793 EXPECT_EQ("foreach (int i,\n" 2794 " list)\n" 2795 "{\n" 2796 " foo();\n" 2797 "}", 2798 format("foreach(int i, list){foo();}", Style)); 2799 Style.ColumnLimit = 2800 40; // to concentrate at brace wrapping, not line wrap due to column limit 2801 EXPECT_EQ("foreach (int i, list) {\n" 2802 " foo();\n" 2803 "}", 2804 format("foreach(int i, list){foo();}", Style)); 2805 Style.ColumnLimit = 2806 20; // to concentrate at brace wrapping, not line wrap due to column limit 2807 EXPECT_EQ("while (foo || bar ||\n" 2808 " baz)\n" 2809 "{\n" 2810 " quux();\n" 2811 "}", 2812 format("while(foo||bar||baz){quux();}", Style)); 2813 EXPECT_EQ("switch (\n" 2814 " foo = barbaz)\n" 2815 "{\n" 2816 "case quux:\n" 2817 " return;\n" 2818 "}", 2819 format("switch(foo=barbaz){case quux:return;}", Style)); 2820 EXPECT_EQ("try {\n" 2821 " foo();\n" 2822 "} catch (\n" 2823 " Exception &bar)\n" 2824 "{\n" 2825 " baz();\n" 2826 "}", 2827 format("try{foo();}catch(Exception&bar){baz();}", Style)); 2828 Style.ColumnLimit = 2829 40; // to concentrate at brace wrapping, not line wrap due to column limit 2830 EXPECT_EQ("try {\n" 2831 " foo();\n" 2832 "} catch (Exception &bar) {\n" 2833 " baz();\n" 2834 "}", 2835 format("try{foo();}catch(Exception&bar){baz();}", Style)); 2836 Style.ColumnLimit = 2837 20; // to concentrate at brace wrapping, not line wrap due to column limit 2838 2839 Style.BraceWrapping.BeforeElse = true; 2840 EXPECT_EQ( 2841 "if (foo) {\n" 2842 " bar();\n" 2843 "}\n" 2844 "else if (baz ||\n" 2845 " quux)\n" 2846 "{\n" 2847 " foobar();\n" 2848 "}\n" 2849 "else {\n" 2850 " barbaz();\n" 2851 "}", 2852 format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}", 2853 Style)); 2854 2855 Style.BraceWrapping.BeforeCatch = true; 2856 EXPECT_EQ("try {\n" 2857 " foo();\n" 2858 "}\n" 2859 "catch (...) {\n" 2860 " baz();\n" 2861 "}", 2862 format("try{foo();}catch(...){baz();}", Style)); 2863 } 2864 2865 TEST_F(FormatTest, BeforeWhile) { 2866 FormatStyle Style = getLLVMStyle(); 2867 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom; 2868 2869 verifyFormat("do {\n" 2870 " foo();\n" 2871 "} while (1);", 2872 Style); 2873 Style.BraceWrapping.BeforeWhile = true; 2874 verifyFormat("do {\n" 2875 " foo();\n" 2876 "}\n" 2877 "while (1);", 2878 Style); 2879 } 2880 2881 //===----------------------------------------------------------------------===// 2882 // Tests for classes, namespaces, etc. 2883 //===----------------------------------------------------------------------===// 2884 2885 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 2886 verifyFormat("class A {};"); 2887 } 2888 2889 TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 2890 verifyFormat("class A {\n" 2891 "public:\n" 2892 "public: // comment\n" 2893 "protected:\n" 2894 "private:\n" 2895 " void f() {}\n" 2896 "};"); 2897 verifyFormat("export class A {\n" 2898 "public:\n" 2899 "public: // comment\n" 2900 "protected:\n" 2901 "private:\n" 2902 " void f() {}\n" 2903 "};"); 2904 verifyGoogleFormat("class A {\n" 2905 " public:\n" 2906 " protected:\n" 2907 " private:\n" 2908 " void f() {}\n" 2909 "};"); 2910 verifyGoogleFormat("export class A {\n" 2911 " public:\n" 2912 " protected:\n" 2913 " private:\n" 2914 " void f() {}\n" 2915 "};"); 2916 verifyFormat("class A {\n" 2917 "public slots:\n" 2918 " void f1() {}\n" 2919 "public Q_SLOTS:\n" 2920 " void f2() {}\n" 2921 "protected slots:\n" 2922 " void f3() {}\n" 2923 "protected Q_SLOTS:\n" 2924 " void f4() {}\n" 2925 "private slots:\n" 2926 " void f5() {}\n" 2927 "private Q_SLOTS:\n" 2928 " void f6() {}\n" 2929 "signals:\n" 2930 " void g1();\n" 2931 "Q_SIGNALS:\n" 2932 " void g2();\n" 2933 "};"); 2934 2935 // Don't interpret 'signals' the wrong way. 2936 verifyFormat("signals.set();"); 2937 verifyFormat("for (Signals signals : f()) {\n}"); 2938 verifyFormat("{\n" 2939 " signals.set(); // This needs indentation.\n" 2940 "}"); 2941 verifyFormat("void f() {\n" 2942 "label:\n" 2943 " signals.baz();\n" 2944 "}"); 2945 } 2946 2947 TEST_F(FormatTest, SeparatesLogicalBlocks) { 2948 EXPECT_EQ("class A {\n" 2949 "public:\n" 2950 " void f();\n" 2951 "\n" 2952 "private:\n" 2953 " void g() {}\n" 2954 " // test\n" 2955 "protected:\n" 2956 " int h;\n" 2957 "};", 2958 format("class A {\n" 2959 "public:\n" 2960 "void f();\n" 2961 "private:\n" 2962 "void g() {}\n" 2963 "// test\n" 2964 "protected:\n" 2965 "int h;\n" 2966 "};")); 2967 EXPECT_EQ("class A {\n" 2968 "protected:\n" 2969 "public:\n" 2970 " void f();\n" 2971 "};", 2972 format("class A {\n" 2973 "protected:\n" 2974 "\n" 2975 "public:\n" 2976 "\n" 2977 " void f();\n" 2978 "};")); 2979 2980 // Even ensure proper spacing inside macros. 2981 EXPECT_EQ("#define B \\\n" 2982 " class A { \\\n" 2983 " protected: \\\n" 2984 " public: \\\n" 2985 " void f(); \\\n" 2986 " };", 2987 format("#define B \\\n" 2988 " class A { \\\n" 2989 " protected: \\\n" 2990 " \\\n" 2991 " public: \\\n" 2992 " \\\n" 2993 " void f(); \\\n" 2994 " };", 2995 getGoogleStyle())); 2996 // But don't remove empty lines after macros ending in access specifiers. 2997 EXPECT_EQ("#define A private:\n" 2998 "\n" 2999 "int i;", 3000 format("#define A private:\n" 3001 "\n" 3002 "int i;")); 3003 } 3004 3005 TEST_F(FormatTest, FormatsClasses) { 3006 verifyFormat("class A : public B {};"); 3007 verifyFormat("class A : public ::B {};"); 3008 3009 verifyFormat( 3010 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3011 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 3012 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 3013 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3014 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 3015 verifyFormat( 3016 "class A : public B, public C, public D, public E, public F {};"); 3017 verifyFormat("class AAAAAAAAAAAA : public B,\n" 3018 " public C,\n" 3019 " public D,\n" 3020 " public E,\n" 3021 " public F,\n" 3022 " public G {};"); 3023 3024 verifyFormat("class\n" 3025 " ReallyReallyLongClassName {\n" 3026 " int i;\n" 3027 "};", 3028 getLLVMStyleWithColumns(32)); 3029 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 3030 " aaaaaaaaaaaaaaaa> {};"); 3031 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" 3032 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n" 3033 " aaaaaaaaaaaaaaaaaaaaaa> {};"); 3034 verifyFormat("template <class R, class C>\n" 3035 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n" 3036 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};"); 3037 verifyFormat("class ::A::B {};"); 3038 } 3039 3040 TEST_F(FormatTest, BreakInheritanceStyle) { 3041 FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle(); 3042 StyleWithInheritanceBreakBeforeComma.BreakInheritanceList = 3043 FormatStyle::BILS_BeforeComma; 3044 verifyFormat("class MyClass : public X {};", 3045 StyleWithInheritanceBreakBeforeComma); 3046 verifyFormat("class MyClass\n" 3047 " : public X\n" 3048 " , public Y {};", 3049 StyleWithInheritanceBreakBeforeComma); 3050 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n" 3051 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n" 3052 " , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", 3053 StyleWithInheritanceBreakBeforeComma); 3054 verifyFormat("struct aaaaaaaaaaaaa\n" 3055 " : public aaaaaaaaaaaaaaaaaaa< // break\n" 3056 " aaaaaaaaaaaaaaaa> {};", 3057 StyleWithInheritanceBreakBeforeComma); 3058 3059 FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle(); 3060 StyleWithInheritanceBreakAfterColon.BreakInheritanceList = 3061 FormatStyle::BILS_AfterColon; 3062 verifyFormat("class MyClass : public X {};", 3063 StyleWithInheritanceBreakAfterColon); 3064 verifyFormat("class MyClass : public X, public Y {};", 3065 StyleWithInheritanceBreakAfterColon); 3066 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n" 3067 " public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3068 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", 3069 StyleWithInheritanceBreakAfterColon); 3070 verifyFormat("struct aaaaaaaaaaaaa :\n" 3071 " public aaaaaaaaaaaaaaaaaaa< // break\n" 3072 " aaaaaaaaaaaaaaaa> {};", 3073 StyleWithInheritanceBreakAfterColon); 3074 3075 FormatStyle StyleWithInheritanceBreakAfterComma = getLLVMStyle(); 3076 StyleWithInheritanceBreakAfterComma.BreakInheritanceList = 3077 FormatStyle::BILS_AfterComma; 3078 verifyFormat("class MyClass : public X {};", 3079 StyleWithInheritanceBreakAfterComma); 3080 verifyFormat("class MyClass : public X,\n" 3081 " public Y {};", 3082 StyleWithInheritanceBreakAfterComma); 3083 verifyFormat( 3084 "class AAAAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3085 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC " 3086 "{};", 3087 StyleWithInheritanceBreakAfterComma); 3088 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 3089 " aaaaaaaaaaaaaaaa> {};", 3090 StyleWithInheritanceBreakAfterComma); 3091 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 3092 " : public OnceBreak,\n" 3093 " public AlwaysBreak,\n" 3094 " EvenBasesFitInOneLine {};", 3095 StyleWithInheritanceBreakAfterComma); 3096 } 3097 3098 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { 3099 verifyFormat("class A {\n} a, b;"); 3100 verifyFormat("struct A {\n} a, b;"); 3101 verifyFormat("union A {\n} a;"); 3102 } 3103 3104 TEST_F(FormatTest, FormatsEnum) { 3105 verifyFormat("enum {\n" 3106 " Zero,\n" 3107 " One = 1,\n" 3108 " Two = One + 1,\n" 3109 " Three = (One + Two),\n" 3110 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3111 " Five = (One, Two, Three, Four, 5)\n" 3112 "};"); 3113 verifyGoogleFormat("enum {\n" 3114 " Zero,\n" 3115 " One = 1,\n" 3116 " Two = One + 1,\n" 3117 " Three = (One + Two),\n" 3118 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3119 " Five = (One, Two, Three, Four, 5)\n" 3120 "};"); 3121 verifyFormat("enum Enum {};"); 3122 verifyFormat("enum {};"); 3123 verifyFormat("enum X E {} d;"); 3124 verifyFormat("enum __attribute__((...)) E {} d;"); 3125 verifyFormat("enum __declspec__((...)) E {} d;"); 3126 verifyFormat("enum {\n" 3127 " Bar = Foo<int, int>::value\n" 3128 "};", 3129 getLLVMStyleWithColumns(30)); 3130 3131 verifyFormat("enum ShortEnum { A, B, C };"); 3132 verifyGoogleFormat("enum ShortEnum { A, B, C };"); 3133 3134 EXPECT_EQ("enum KeepEmptyLines {\n" 3135 " ONE,\n" 3136 "\n" 3137 " TWO,\n" 3138 "\n" 3139 " THREE\n" 3140 "}", 3141 format("enum KeepEmptyLines {\n" 3142 " ONE,\n" 3143 "\n" 3144 " TWO,\n" 3145 "\n" 3146 "\n" 3147 " THREE\n" 3148 "}")); 3149 verifyFormat("enum E { // comment\n" 3150 " ONE,\n" 3151 " TWO\n" 3152 "};\n" 3153 "int i;"); 3154 3155 FormatStyle EightIndent = getLLVMStyle(); 3156 EightIndent.IndentWidth = 8; 3157 verifyFormat("enum {\n" 3158 " VOID,\n" 3159 " CHAR,\n" 3160 " SHORT,\n" 3161 " INT,\n" 3162 " LONG,\n" 3163 " SIGNED,\n" 3164 " UNSIGNED,\n" 3165 " BOOL,\n" 3166 " FLOAT,\n" 3167 " DOUBLE,\n" 3168 " COMPLEX\n" 3169 "};", 3170 EightIndent); 3171 3172 // Not enums. 3173 verifyFormat("enum X f() {\n" 3174 " a();\n" 3175 " return 42;\n" 3176 "}"); 3177 verifyFormat("enum X Type::f() {\n" 3178 " a();\n" 3179 " return 42;\n" 3180 "}"); 3181 verifyFormat("enum ::X f() {\n" 3182 " a();\n" 3183 " return 42;\n" 3184 "}"); 3185 verifyFormat("enum ns::X f() {\n" 3186 " a();\n" 3187 " return 42;\n" 3188 "}"); 3189 } 3190 3191 TEST_F(FormatTest, FormatsEnumsWithErrors) { 3192 verifyFormat("enum Type {\n" 3193 " One = 0; // These semicolons should be commas.\n" 3194 " Two = 1;\n" 3195 "};"); 3196 verifyFormat("namespace n {\n" 3197 "enum Type {\n" 3198 " One,\n" 3199 " Two, // missing };\n" 3200 " int i;\n" 3201 "}\n" 3202 "void g() {}"); 3203 } 3204 3205 TEST_F(FormatTest, FormatsEnumStruct) { 3206 verifyFormat("enum struct {\n" 3207 " Zero,\n" 3208 " One = 1,\n" 3209 " Two = One + 1,\n" 3210 " Three = (One + Two),\n" 3211 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3212 " Five = (One, Two, Three, Four, 5)\n" 3213 "};"); 3214 verifyFormat("enum struct Enum {};"); 3215 verifyFormat("enum struct {};"); 3216 verifyFormat("enum struct X E {} d;"); 3217 verifyFormat("enum struct __attribute__((...)) E {} d;"); 3218 verifyFormat("enum struct __declspec__((...)) E {} d;"); 3219 verifyFormat("enum struct X f() {\n a();\n return 42;\n}"); 3220 } 3221 3222 TEST_F(FormatTest, FormatsEnumClass) { 3223 verifyFormat("enum class {\n" 3224 " Zero,\n" 3225 " One = 1,\n" 3226 " Two = One + 1,\n" 3227 " Three = (One + Two),\n" 3228 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3229 " Five = (One, Two, Three, Four, 5)\n" 3230 "};"); 3231 verifyFormat("enum class Enum {};"); 3232 verifyFormat("enum class {};"); 3233 verifyFormat("enum class X E {} d;"); 3234 verifyFormat("enum class __attribute__((...)) E {} d;"); 3235 verifyFormat("enum class __declspec__((...)) E {} d;"); 3236 verifyFormat("enum class X f() {\n a();\n return 42;\n}"); 3237 } 3238 3239 TEST_F(FormatTest, FormatsEnumTypes) { 3240 verifyFormat("enum X : int {\n" 3241 " A, // Force multiple lines.\n" 3242 " B\n" 3243 "};"); 3244 verifyFormat("enum X : int { A, B };"); 3245 verifyFormat("enum X : std::uint32_t { A, B };"); 3246 } 3247 3248 TEST_F(FormatTest, FormatsTypedefEnum) { 3249 FormatStyle Style = getLLVMStyle(); 3250 Style.ColumnLimit = 40; 3251 verifyFormat("typedef enum {} EmptyEnum;"); 3252 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 3253 verifyFormat("typedef enum {\n" 3254 " ZERO = 0,\n" 3255 " ONE = 1,\n" 3256 " TWO = 2,\n" 3257 " THREE = 3\n" 3258 "} LongEnum;", 3259 Style); 3260 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3261 Style.BraceWrapping.AfterEnum = true; 3262 verifyFormat("typedef enum {} EmptyEnum;"); 3263 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 3264 verifyFormat("typedef enum\n" 3265 "{\n" 3266 " ZERO = 0,\n" 3267 " ONE = 1,\n" 3268 " TWO = 2,\n" 3269 " THREE = 3\n" 3270 "} LongEnum;", 3271 Style); 3272 } 3273 3274 TEST_F(FormatTest, FormatsNSEnums) { 3275 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }"); 3276 verifyGoogleFormat( 3277 "typedef NS_CLOSED_ENUM(NSInteger, SomeName) { AAA, BBB }"); 3278 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n" 3279 " // Information about someDecentlyLongValue.\n" 3280 " someDecentlyLongValue,\n" 3281 " // Information about anotherDecentlyLongValue.\n" 3282 " anotherDecentlyLongValue,\n" 3283 " // Information about aThirdDecentlyLongValue.\n" 3284 " aThirdDecentlyLongValue\n" 3285 "};"); 3286 verifyGoogleFormat("typedef NS_CLOSED_ENUM(NSInteger, MyType) {\n" 3287 " // Information about someDecentlyLongValue.\n" 3288 " someDecentlyLongValue,\n" 3289 " // Information about anotherDecentlyLongValue.\n" 3290 " anotherDecentlyLongValue,\n" 3291 " // Information about aThirdDecentlyLongValue.\n" 3292 " aThirdDecentlyLongValue\n" 3293 "};"); 3294 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n" 3295 " a = 1,\n" 3296 " b = 2,\n" 3297 " c = 3,\n" 3298 "};"); 3299 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n" 3300 " a = 1,\n" 3301 " b = 2,\n" 3302 " c = 3,\n" 3303 "};"); 3304 verifyGoogleFormat("typedef CF_CLOSED_ENUM(NSInteger, MyType) {\n" 3305 " a = 1,\n" 3306 " b = 2,\n" 3307 " c = 3,\n" 3308 "};"); 3309 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n" 3310 " a = 1,\n" 3311 " b = 2,\n" 3312 " c = 3,\n" 3313 "};"); 3314 } 3315 3316 TEST_F(FormatTest, FormatsBitfields) { 3317 verifyFormat("struct Bitfields {\n" 3318 " unsigned sClass : 8;\n" 3319 " unsigned ValueKind : 2;\n" 3320 "};"); 3321 verifyFormat("struct A {\n" 3322 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" 3323 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" 3324 "};"); 3325 verifyFormat("struct MyStruct {\n" 3326 " uchar data;\n" 3327 " uchar : 8;\n" 3328 " uchar : 8;\n" 3329 " uchar other;\n" 3330 "};"); 3331 FormatStyle Style = getLLVMStyle(); 3332 Style.BitFieldColonSpacing = FormatStyle::BFCS_None; 3333 verifyFormat("struct Bitfields {\n" 3334 " unsigned sClass:8;\n" 3335 " unsigned ValueKind:2;\n" 3336 " uchar other;\n" 3337 "};", 3338 Style); 3339 verifyFormat("struct A {\n" 3340 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1,\n" 3341 " bbbbbbbbbbbbbbbbbbbbbbbbb:2;\n" 3342 "};", 3343 Style); 3344 Style.BitFieldColonSpacing = FormatStyle::BFCS_Before; 3345 verifyFormat("struct Bitfields {\n" 3346 " unsigned sClass :8;\n" 3347 " unsigned ValueKind :2;\n" 3348 " uchar other;\n" 3349 "};", 3350 Style); 3351 Style.BitFieldColonSpacing = FormatStyle::BFCS_After; 3352 verifyFormat("struct Bitfields {\n" 3353 " unsigned sClass: 8;\n" 3354 " unsigned ValueKind: 2;\n" 3355 " uchar other;\n" 3356 "};", 3357 Style); 3358 } 3359 3360 TEST_F(FormatTest, FormatsNamespaces) { 3361 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 3362 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 3363 3364 verifyFormat("namespace some_namespace {\n" 3365 "class A {};\n" 3366 "void f() { f(); }\n" 3367 "}", 3368 LLVMWithNoNamespaceFix); 3369 verifyFormat("namespace N::inline D {\n" 3370 "class A {};\n" 3371 "void f() { f(); }\n" 3372 "}", 3373 LLVMWithNoNamespaceFix); 3374 verifyFormat("namespace N::inline D::E {\n" 3375 "class A {};\n" 3376 "void f() { f(); }\n" 3377 "}", 3378 LLVMWithNoNamespaceFix); 3379 verifyFormat("namespace [[deprecated(\"foo[bar\")]] some_namespace {\n" 3380 "class A {};\n" 3381 "void f() { f(); }\n" 3382 "}", 3383 LLVMWithNoNamespaceFix); 3384 verifyFormat("/* something */ namespace some_namespace {\n" 3385 "class A {};\n" 3386 "void f() { f(); }\n" 3387 "}", 3388 LLVMWithNoNamespaceFix); 3389 verifyFormat("namespace {\n" 3390 "class A {};\n" 3391 "void f() { f(); }\n" 3392 "}", 3393 LLVMWithNoNamespaceFix); 3394 verifyFormat("/* something */ namespace {\n" 3395 "class A {};\n" 3396 "void f() { f(); }\n" 3397 "}", 3398 LLVMWithNoNamespaceFix); 3399 verifyFormat("inline namespace X {\n" 3400 "class A {};\n" 3401 "void f() { f(); }\n" 3402 "}", 3403 LLVMWithNoNamespaceFix); 3404 verifyFormat("/* something */ inline namespace X {\n" 3405 "class A {};\n" 3406 "void f() { f(); }\n" 3407 "}", 3408 LLVMWithNoNamespaceFix); 3409 verifyFormat("export namespace X {\n" 3410 "class A {};\n" 3411 "void f() { f(); }\n" 3412 "}", 3413 LLVMWithNoNamespaceFix); 3414 verifyFormat("using namespace some_namespace;\n" 3415 "class A {};\n" 3416 "void f() { f(); }", 3417 LLVMWithNoNamespaceFix); 3418 3419 // This code is more common than we thought; if we 3420 // layout this correctly the semicolon will go into 3421 // its own line, which is undesirable. 3422 verifyFormat("namespace {};", LLVMWithNoNamespaceFix); 3423 verifyFormat("namespace {\n" 3424 "class A {};\n" 3425 "};", 3426 LLVMWithNoNamespaceFix); 3427 3428 verifyFormat("namespace {\n" 3429 "int SomeVariable = 0; // comment\n" 3430 "} // namespace", 3431 LLVMWithNoNamespaceFix); 3432 EXPECT_EQ("#ifndef HEADER_GUARD\n" 3433 "#define HEADER_GUARD\n" 3434 "namespace my_namespace {\n" 3435 "int i;\n" 3436 "} // my_namespace\n" 3437 "#endif // HEADER_GUARD", 3438 format("#ifndef HEADER_GUARD\n" 3439 " #define HEADER_GUARD\n" 3440 " namespace my_namespace {\n" 3441 "int i;\n" 3442 "} // my_namespace\n" 3443 "#endif // HEADER_GUARD", 3444 LLVMWithNoNamespaceFix)); 3445 3446 EXPECT_EQ("namespace A::B {\n" 3447 "class C {};\n" 3448 "}", 3449 format("namespace A::B {\n" 3450 "class C {};\n" 3451 "}", 3452 LLVMWithNoNamespaceFix)); 3453 3454 FormatStyle Style = getLLVMStyle(); 3455 Style.NamespaceIndentation = FormatStyle::NI_All; 3456 EXPECT_EQ("namespace out {\n" 3457 " int i;\n" 3458 " namespace in {\n" 3459 " int i;\n" 3460 " } // namespace in\n" 3461 "} // namespace out", 3462 format("namespace out {\n" 3463 "int i;\n" 3464 "namespace in {\n" 3465 "int i;\n" 3466 "} // namespace in\n" 3467 "} // namespace out", 3468 Style)); 3469 3470 Style.NamespaceIndentation = FormatStyle::NI_Inner; 3471 EXPECT_EQ("namespace out {\n" 3472 "int i;\n" 3473 "namespace in {\n" 3474 " int i;\n" 3475 "} // namespace in\n" 3476 "} // namespace out", 3477 format("namespace out {\n" 3478 "int i;\n" 3479 "namespace in {\n" 3480 "int i;\n" 3481 "} // namespace in\n" 3482 "} // namespace out", 3483 Style)); 3484 } 3485 3486 TEST_F(FormatTest, NamespaceMacros) { 3487 FormatStyle Style = getLLVMStyle(); 3488 Style.NamespaceMacros.push_back("TESTSUITE"); 3489 3490 verifyFormat("TESTSUITE(A) {\n" 3491 "int foo();\n" 3492 "} // TESTSUITE(A)", 3493 Style); 3494 3495 verifyFormat("TESTSUITE(A, B) {\n" 3496 "int foo();\n" 3497 "} // TESTSUITE(A)", 3498 Style); 3499 3500 // Properly indent according to NamespaceIndentation style 3501 Style.NamespaceIndentation = FormatStyle::NI_All; 3502 verifyFormat("TESTSUITE(A) {\n" 3503 " int foo();\n" 3504 "} // TESTSUITE(A)", 3505 Style); 3506 verifyFormat("TESTSUITE(A) {\n" 3507 " namespace B {\n" 3508 " int foo();\n" 3509 " } // namespace B\n" 3510 "} // TESTSUITE(A)", 3511 Style); 3512 verifyFormat("namespace A {\n" 3513 " TESTSUITE(B) {\n" 3514 " int foo();\n" 3515 " } // TESTSUITE(B)\n" 3516 "} // namespace A", 3517 Style); 3518 3519 Style.NamespaceIndentation = FormatStyle::NI_Inner; 3520 verifyFormat("TESTSUITE(A) {\n" 3521 "TESTSUITE(B) {\n" 3522 " int foo();\n" 3523 "} // TESTSUITE(B)\n" 3524 "} // TESTSUITE(A)", 3525 Style); 3526 verifyFormat("TESTSUITE(A) {\n" 3527 "namespace B {\n" 3528 " int foo();\n" 3529 "} // namespace B\n" 3530 "} // TESTSUITE(A)", 3531 Style); 3532 verifyFormat("namespace A {\n" 3533 "TESTSUITE(B) {\n" 3534 " int foo();\n" 3535 "} // TESTSUITE(B)\n" 3536 "} // namespace A", 3537 Style); 3538 3539 // Properly merge namespace-macros blocks in CompactNamespaces mode 3540 Style.NamespaceIndentation = FormatStyle::NI_None; 3541 Style.CompactNamespaces = true; 3542 verifyFormat("TESTSUITE(A) { TESTSUITE(B) {\n" 3543 "}} // TESTSUITE(A::B)", 3544 Style); 3545 3546 EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n" 3547 "}} // TESTSUITE(out::in)", 3548 format("TESTSUITE(out) {\n" 3549 "TESTSUITE(in) {\n" 3550 "} // TESTSUITE(in)\n" 3551 "} // TESTSUITE(out)", 3552 Style)); 3553 3554 EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n" 3555 "}} // TESTSUITE(out::in)", 3556 format("TESTSUITE(out) {\n" 3557 "TESTSUITE(in) {\n" 3558 "} // TESTSUITE(in)\n" 3559 "} // TESTSUITE(out)", 3560 Style)); 3561 3562 // Do not merge different namespaces/macros 3563 EXPECT_EQ("namespace out {\n" 3564 "TESTSUITE(in) {\n" 3565 "} // TESTSUITE(in)\n" 3566 "} // namespace out", 3567 format("namespace out {\n" 3568 "TESTSUITE(in) {\n" 3569 "} // TESTSUITE(in)\n" 3570 "} // namespace out", 3571 Style)); 3572 EXPECT_EQ("TESTSUITE(out) {\n" 3573 "namespace in {\n" 3574 "} // namespace in\n" 3575 "} // TESTSUITE(out)", 3576 format("TESTSUITE(out) {\n" 3577 "namespace in {\n" 3578 "} // namespace in\n" 3579 "} // TESTSUITE(out)", 3580 Style)); 3581 Style.NamespaceMacros.push_back("FOOBAR"); 3582 EXPECT_EQ("TESTSUITE(out) {\n" 3583 "FOOBAR(in) {\n" 3584 "} // FOOBAR(in)\n" 3585 "} // TESTSUITE(out)", 3586 format("TESTSUITE(out) {\n" 3587 "FOOBAR(in) {\n" 3588 "} // FOOBAR(in)\n" 3589 "} // TESTSUITE(out)", 3590 Style)); 3591 } 3592 3593 TEST_F(FormatTest, FormatsCompactNamespaces) { 3594 FormatStyle Style = getLLVMStyle(); 3595 Style.CompactNamespaces = true; 3596 Style.NamespaceMacros.push_back("TESTSUITE"); 3597 3598 verifyFormat("namespace A { namespace B {\n" 3599 "}} // namespace A::B", 3600 Style); 3601 3602 EXPECT_EQ("namespace out { namespace in {\n" 3603 "}} // namespace out::in", 3604 format("namespace out {\n" 3605 "namespace in {\n" 3606 "} // namespace in\n" 3607 "} // namespace out", 3608 Style)); 3609 3610 // Only namespaces which have both consecutive opening and end get compacted 3611 EXPECT_EQ("namespace out {\n" 3612 "namespace in1 {\n" 3613 "} // namespace in1\n" 3614 "namespace in2 {\n" 3615 "} // namespace in2\n" 3616 "} // namespace out", 3617 format("namespace out {\n" 3618 "namespace in1 {\n" 3619 "} // namespace in1\n" 3620 "namespace in2 {\n" 3621 "} // namespace in2\n" 3622 "} // namespace out", 3623 Style)); 3624 3625 EXPECT_EQ("namespace out {\n" 3626 "int i;\n" 3627 "namespace in {\n" 3628 "int j;\n" 3629 "} // namespace in\n" 3630 "int k;\n" 3631 "} // namespace out", 3632 format("namespace out { int i;\n" 3633 "namespace in { int j; } // namespace in\n" 3634 "int k; } // namespace out", 3635 Style)); 3636 3637 EXPECT_EQ("namespace A { namespace B { namespace C {\n" 3638 "}}} // namespace A::B::C\n", 3639 format("namespace A { namespace B {\n" 3640 "namespace C {\n" 3641 "}} // namespace B::C\n" 3642 "} // namespace A\n", 3643 Style)); 3644 3645 Style.ColumnLimit = 40; 3646 EXPECT_EQ("namespace aaaaaaaaaa {\n" 3647 "namespace bbbbbbbbbb {\n" 3648 "}} // namespace aaaaaaaaaa::bbbbbbbbbb", 3649 format("namespace aaaaaaaaaa {\n" 3650 "namespace bbbbbbbbbb {\n" 3651 "} // namespace bbbbbbbbbb\n" 3652 "} // namespace aaaaaaaaaa", 3653 Style)); 3654 3655 EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n" 3656 "namespace cccccc {\n" 3657 "}}} // namespace aaaaaa::bbbbbb::cccccc", 3658 format("namespace aaaaaa {\n" 3659 "namespace bbbbbb {\n" 3660 "namespace cccccc {\n" 3661 "} // namespace cccccc\n" 3662 "} // namespace bbbbbb\n" 3663 "} // namespace aaaaaa", 3664 Style)); 3665 Style.ColumnLimit = 80; 3666 3667 // Extra semicolon after 'inner' closing brace prevents merging 3668 EXPECT_EQ("namespace out { namespace in {\n" 3669 "}; } // namespace out::in", 3670 format("namespace out {\n" 3671 "namespace in {\n" 3672 "}; // namespace in\n" 3673 "} // namespace out", 3674 Style)); 3675 3676 // Extra semicolon after 'outer' closing brace is conserved 3677 EXPECT_EQ("namespace out { namespace in {\n" 3678 "}}; // namespace out::in", 3679 format("namespace out {\n" 3680 "namespace in {\n" 3681 "} // namespace in\n" 3682 "}; // namespace out", 3683 Style)); 3684 3685 Style.NamespaceIndentation = FormatStyle::NI_All; 3686 EXPECT_EQ("namespace out { namespace in {\n" 3687 " int i;\n" 3688 "}} // namespace out::in", 3689 format("namespace out {\n" 3690 "namespace in {\n" 3691 "int i;\n" 3692 "} // namespace in\n" 3693 "} // namespace out", 3694 Style)); 3695 EXPECT_EQ("namespace out { namespace mid {\n" 3696 " namespace in {\n" 3697 " int j;\n" 3698 " } // namespace in\n" 3699 " int k;\n" 3700 "}} // namespace out::mid", 3701 format("namespace out { namespace mid {\n" 3702 "namespace in { int j; } // namespace in\n" 3703 "int k; }} // namespace out::mid", 3704 Style)); 3705 3706 Style.NamespaceIndentation = FormatStyle::NI_Inner; 3707 EXPECT_EQ("namespace out { namespace in {\n" 3708 " int i;\n" 3709 "}} // namespace out::in", 3710 format("namespace out {\n" 3711 "namespace in {\n" 3712 "int i;\n" 3713 "} // namespace in\n" 3714 "} // namespace out", 3715 Style)); 3716 EXPECT_EQ("namespace out { namespace mid { namespace in {\n" 3717 " int i;\n" 3718 "}}} // namespace out::mid::in", 3719 format("namespace out {\n" 3720 "namespace mid {\n" 3721 "namespace in {\n" 3722 "int i;\n" 3723 "} // namespace in\n" 3724 "} // namespace mid\n" 3725 "} // namespace out", 3726 Style)); 3727 } 3728 3729 TEST_F(FormatTest, FormatsExternC) { 3730 verifyFormat("extern \"C\" {\nint a;"); 3731 verifyFormat("extern \"C\" {}"); 3732 verifyFormat("extern \"C\" {\n" 3733 "int foo();\n" 3734 "}"); 3735 verifyFormat("extern \"C\" int foo() {}"); 3736 verifyFormat("extern \"C\" int foo();"); 3737 verifyFormat("extern \"C\" int foo() {\n" 3738 " int i = 42;\n" 3739 " return i;\n" 3740 "}"); 3741 3742 FormatStyle Style = getLLVMStyle(); 3743 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3744 Style.BraceWrapping.AfterFunction = true; 3745 verifyFormat("extern \"C\" int foo() {}", Style); 3746 verifyFormat("extern \"C\" int foo();", Style); 3747 verifyFormat("extern \"C\" int foo()\n" 3748 "{\n" 3749 " int i = 42;\n" 3750 " return i;\n" 3751 "}", 3752 Style); 3753 3754 Style.BraceWrapping.AfterExternBlock = true; 3755 Style.BraceWrapping.SplitEmptyRecord = false; 3756 verifyFormat("extern \"C\"\n" 3757 "{}", 3758 Style); 3759 verifyFormat("extern \"C\"\n" 3760 "{\n" 3761 " int foo();\n" 3762 "}", 3763 Style); 3764 } 3765 3766 TEST_F(FormatTest, IndentExternBlockStyle) { 3767 FormatStyle Style = getLLVMStyle(); 3768 Style.IndentWidth = 2; 3769 3770 Style.IndentExternBlock = FormatStyle::IEBS_Indent; 3771 verifyFormat("extern \"C\" { /*9*/\n}", Style); 3772 verifyFormat("extern \"C\" {\n" 3773 " int foo10();\n" 3774 "}", 3775 Style); 3776 3777 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; 3778 verifyFormat("extern \"C\" { /*11*/\n}", Style); 3779 verifyFormat("extern \"C\" {\n" 3780 "int foo12();\n" 3781 "}", 3782 Style); 3783 3784 Style.IndentExternBlock = FormatStyle::IEBS_AfterExternBlock; 3785 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3786 Style.BraceWrapping.AfterExternBlock = true; 3787 verifyFormat("extern \"C\"\n{ /*13*/\n}", Style); 3788 verifyFormat("extern \"C\"\n{\n" 3789 " int foo14();\n" 3790 "}", 3791 Style); 3792 3793 Style.IndentExternBlock = FormatStyle::IEBS_AfterExternBlock; 3794 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3795 Style.BraceWrapping.AfterExternBlock = false; 3796 verifyFormat("extern \"C\" { /*15*/\n}", Style); 3797 verifyFormat("extern \"C\" {\n" 3798 "int foo16();\n" 3799 "}", 3800 Style); 3801 } 3802 3803 TEST_F(FormatTest, FormatsInlineASM) { 3804 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); 3805 verifyFormat("asm(\"nop\" ::: \"memory\");"); 3806 verifyFormat( 3807 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 3808 " \"cpuid\\n\\t\"\n" 3809 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 3810 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" 3811 " : \"a\"(value));"); 3812 EXPECT_EQ( 3813 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 3814 " __asm {\n" 3815 " mov edx,[that] // vtable in edx\n" 3816 " mov eax,methodIndex\n" 3817 " call [edx][eax*4] // stdcall\n" 3818 " }\n" 3819 "}", 3820 format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 3821 " __asm {\n" 3822 " mov edx,[that] // vtable in edx\n" 3823 " mov eax,methodIndex\n" 3824 " call [edx][eax*4] // stdcall\n" 3825 " }\n" 3826 "}")); 3827 EXPECT_EQ("_asm {\n" 3828 " xor eax, eax;\n" 3829 " cpuid;\n" 3830 "}", 3831 format("_asm {\n" 3832 " xor eax, eax;\n" 3833 " cpuid;\n" 3834 "}")); 3835 verifyFormat("void function() {\n" 3836 " // comment\n" 3837 " asm(\"\");\n" 3838 "}"); 3839 EXPECT_EQ("__asm {\n" 3840 "}\n" 3841 "int i;", 3842 format("__asm {\n" 3843 "}\n" 3844 "int i;")); 3845 } 3846 3847 TEST_F(FormatTest, FormatTryCatch) { 3848 verifyFormat("try {\n" 3849 " throw a * b;\n" 3850 "} catch (int a) {\n" 3851 " // Do nothing.\n" 3852 "} catch (...) {\n" 3853 " exit(42);\n" 3854 "}"); 3855 3856 // Function-level try statements. 3857 verifyFormat("int f() try { return 4; } catch (...) {\n" 3858 " return 5;\n" 3859 "}"); 3860 verifyFormat("class A {\n" 3861 " int a;\n" 3862 " A() try : a(0) {\n" 3863 " } catch (...) {\n" 3864 " throw;\n" 3865 " }\n" 3866 "};\n"); 3867 verifyFormat("class A {\n" 3868 " int a;\n" 3869 " A() try : a(0), b{1} {\n" 3870 " } catch (...) {\n" 3871 " throw;\n" 3872 " }\n" 3873 "};\n"); 3874 verifyFormat("class A {\n" 3875 " int a;\n" 3876 " A() try : a(0), b{1}, c{2} {\n" 3877 " } catch (...) {\n" 3878 " throw;\n" 3879 " }\n" 3880 "};\n"); 3881 verifyFormat("class A {\n" 3882 " int a;\n" 3883 " A() try : a(0), b{1}, c{2} {\n" 3884 " { // New scope.\n" 3885 " }\n" 3886 " } catch (...) {\n" 3887 " throw;\n" 3888 " }\n" 3889 "};\n"); 3890 3891 // Incomplete try-catch blocks. 3892 verifyIncompleteFormat("try {} catch ("); 3893 } 3894 3895 TEST_F(FormatTest, FormatTryAsAVariable) { 3896 verifyFormat("int try;"); 3897 verifyFormat("int try, size;"); 3898 verifyFormat("try = foo();"); 3899 verifyFormat("if (try < size) {\n return true;\n}"); 3900 3901 verifyFormat("int catch;"); 3902 verifyFormat("int catch, size;"); 3903 verifyFormat("catch = foo();"); 3904 verifyFormat("if (catch < size) {\n return true;\n}"); 3905 3906 FormatStyle Style = getLLVMStyle(); 3907 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3908 Style.BraceWrapping.AfterFunction = true; 3909 Style.BraceWrapping.BeforeCatch = true; 3910 verifyFormat("try {\n" 3911 " int bar = 1;\n" 3912 "}\n" 3913 "catch (...) {\n" 3914 " int bar = 1;\n" 3915 "}", 3916 Style); 3917 verifyFormat("#if NO_EX\n" 3918 "try\n" 3919 "#endif\n" 3920 "{\n" 3921 "}\n" 3922 "#if NO_EX\n" 3923 "catch (...) {\n" 3924 "}", 3925 Style); 3926 verifyFormat("try /* abc */ {\n" 3927 " int bar = 1;\n" 3928 "}\n" 3929 "catch (...) {\n" 3930 " int bar = 1;\n" 3931 "}", 3932 Style); 3933 verifyFormat("try\n" 3934 "// abc\n" 3935 "{\n" 3936 " int bar = 1;\n" 3937 "}\n" 3938 "catch (...) {\n" 3939 " int bar = 1;\n" 3940 "}", 3941 Style); 3942 } 3943 3944 TEST_F(FormatTest, FormatSEHTryCatch) { 3945 verifyFormat("__try {\n" 3946 " int a = b * c;\n" 3947 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n" 3948 " // Do nothing.\n" 3949 "}"); 3950 3951 verifyFormat("__try {\n" 3952 " int a = b * c;\n" 3953 "} __finally {\n" 3954 " // Do nothing.\n" 3955 "}"); 3956 3957 verifyFormat("DEBUG({\n" 3958 " __try {\n" 3959 " } __finally {\n" 3960 " }\n" 3961 "});\n"); 3962 } 3963 3964 TEST_F(FormatTest, IncompleteTryCatchBlocks) { 3965 verifyFormat("try {\n" 3966 " f();\n" 3967 "} catch {\n" 3968 " g();\n" 3969 "}"); 3970 verifyFormat("try {\n" 3971 " f();\n" 3972 "} catch (A a) MACRO(x) {\n" 3973 " g();\n" 3974 "} catch (B b) MACRO(x) {\n" 3975 " g();\n" 3976 "}"); 3977 } 3978 3979 TEST_F(FormatTest, FormatTryCatchBraceStyles) { 3980 FormatStyle Style = getLLVMStyle(); 3981 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, 3982 FormatStyle::BS_WebKit}) { 3983 Style.BreakBeforeBraces = BraceStyle; 3984 verifyFormat("try {\n" 3985 " // something\n" 3986 "} catch (...) {\n" 3987 " // something\n" 3988 "}", 3989 Style); 3990 } 3991 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 3992 verifyFormat("try {\n" 3993 " // something\n" 3994 "}\n" 3995 "catch (...) {\n" 3996 " // something\n" 3997 "}", 3998 Style); 3999 verifyFormat("__try {\n" 4000 " // something\n" 4001 "}\n" 4002 "__finally {\n" 4003 " // something\n" 4004 "}", 4005 Style); 4006 verifyFormat("@try {\n" 4007 " // something\n" 4008 "}\n" 4009 "@finally {\n" 4010 " // something\n" 4011 "}", 4012 Style); 4013 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 4014 verifyFormat("try\n" 4015 "{\n" 4016 " // something\n" 4017 "}\n" 4018 "catch (...)\n" 4019 "{\n" 4020 " // something\n" 4021 "}", 4022 Style); 4023 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 4024 verifyFormat("try\n" 4025 " {\n" 4026 " // something white\n" 4027 " }\n" 4028 "catch (...)\n" 4029 " {\n" 4030 " // something white\n" 4031 " }", 4032 Style); 4033 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 4034 verifyFormat("try\n" 4035 " {\n" 4036 " // something\n" 4037 " }\n" 4038 "catch (...)\n" 4039 " {\n" 4040 " // something\n" 4041 " }", 4042 Style); 4043 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4044 Style.BraceWrapping.BeforeCatch = true; 4045 verifyFormat("try {\n" 4046 " // something\n" 4047 "}\n" 4048 "catch (...) {\n" 4049 " // something\n" 4050 "}", 4051 Style); 4052 } 4053 4054 TEST_F(FormatTest, StaticInitializers) { 4055 verifyFormat("static SomeClass SC = {1, 'a'};"); 4056 4057 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" 4058 " 100000000, " 4059 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); 4060 4061 // Here, everything other than the "}" would fit on a line. 4062 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" 4063 " 10000000000000000000000000};"); 4064 EXPECT_EQ("S s = {a,\n" 4065 "\n" 4066 " b};", 4067 format("S s = {\n" 4068 " a,\n" 4069 "\n" 4070 " b\n" 4071 "};")); 4072 4073 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first 4074 // line. However, the formatting looks a bit off and this probably doesn't 4075 // happen often in practice. 4076 verifyFormat("static int Variable[1] = {\n" 4077 " {1000000000000000000000000000000000000}};", 4078 getLLVMStyleWithColumns(40)); 4079 } 4080 4081 TEST_F(FormatTest, DesignatedInitializers) { 4082 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 4083 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" 4084 " .bbbbbbbbbb = 2,\n" 4085 " .cccccccccc = 3,\n" 4086 " .dddddddddd = 4,\n" 4087 " .eeeeeeeeee = 5};"); 4088 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 4089 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" 4090 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" 4091 " .ccccccccccccccccccccccccccc = 3,\n" 4092 " .ddddddddddddddddddddddddddd = 4,\n" 4093 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); 4094 4095 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); 4096 4097 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 4098 verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n" 4099 " [2] = bbbbbbbbbb,\n" 4100 " [3] = cccccccccc,\n" 4101 " [4] = dddddddddd,\n" 4102 " [5] = eeeeeeeeee};"); 4103 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 4104 " [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4105 " [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 4106 " [3] = cccccccccccccccccccccccccccccccccccccc,\n" 4107 " [4] = dddddddddddddddddddddddddddddddddddddd,\n" 4108 " [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};"); 4109 } 4110 4111 TEST_F(FormatTest, NestedStaticInitializers) { 4112 verifyFormat("static A x = {{{}}};\n"); 4113 verifyFormat("static A x = {{{init1, init2, init3, init4},\n" 4114 " {init1, init2, init3, init4}}};", 4115 getLLVMStyleWithColumns(50)); 4116 4117 verifyFormat("somes Status::global_reps[3] = {\n" 4118 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 4119 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 4120 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", 4121 getLLVMStyleWithColumns(60)); 4122 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n" 4123 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 4124 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 4125 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};"); 4126 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" 4127 " {rect.fRight - rect.fLeft, rect.fBottom - " 4128 "rect.fTop}};"); 4129 4130 verifyFormat( 4131 "SomeArrayOfSomeType a = {\n" 4132 " {{1, 2, 3},\n" 4133 " {1, 2, 3},\n" 4134 " {111111111111111111111111111111, 222222222222222222222222222222,\n" 4135 " 333333333333333333333333333333},\n" 4136 " {1, 2, 3},\n" 4137 " {1, 2, 3}}};"); 4138 verifyFormat( 4139 "SomeArrayOfSomeType a = {\n" 4140 " {{1, 2, 3}},\n" 4141 " {{1, 2, 3}},\n" 4142 " {{111111111111111111111111111111, 222222222222222222222222222222,\n" 4143 " 333333333333333333333333333333}},\n" 4144 " {{1, 2, 3}},\n" 4145 " {{1, 2, 3}}};"); 4146 4147 verifyFormat("struct {\n" 4148 " unsigned bit;\n" 4149 " const char *const name;\n" 4150 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" 4151 " {kOsWin, \"Windows\"},\n" 4152 " {kOsLinux, \"Linux\"},\n" 4153 " {kOsCrOS, \"Chrome OS\"}};"); 4154 verifyFormat("struct {\n" 4155 " unsigned bit;\n" 4156 " const char *const name;\n" 4157 "} kBitsToOs[] = {\n" 4158 " {kOsMac, \"Mac\"},\n" 4159 " {kOsWin, \"Windows\"},\n" 4160 " {kOsLinux, \"Linux\"},\n" 4161 " {kOsCrOS, \"Chrome OS\"},\n" 4162 "};"); 4163 } 4164 4165 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 4166 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 4167 " \\\n" 4168 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 4169 } 4170 4171 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 4172 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 4173 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 4174 4175 // Do break defaulted and deleted functions. 4176 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 4177 " default;", 4178 getLLVMStyleWithColumns(40)); 4179 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 4180 " delete;", 4181 getLLVMStyleWithColumns(40)); 4182 } 4183 4184 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { 4185 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3", 4186 getLLVMStyleWithColumns(40)); 4187 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 4188 getLLVMStyleWithColumns(40)); 4189 EXPECT_EQ("#define Q \\\n" 4190 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" 4191 " \"aaaaaaaa.cpp\"", 4192 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 4193 getLLVMStyleWithColumns(40))); 4194 } 4195 4196 TEST_F(FormatTest, UnderstandsLinePPDirective) { 4197 EXPECT_EQ("# 123 \"A string literal\"", 4198 format(" # 123 \"A string literal\"")); 4199 } 4200 4201 TEST_F(FormatTest, LayoutUnknownPPDirective) { 4202 EXPECT_EQ("#;", format("#;")); 4203 verifyFormat("#\n;\n;\n;"); 4204 } 4205 4206 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 4207 EXPECT_EQ("#line 42 \"test\"\n", 4208 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 4209 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", 4210 getLLVMStyleWithColumns(12))); 4211 } 4212 4213 TEST_F(FormatTest, EndOfFileEndsPPDirective) { 4214 EXPECT_EQ("#line 42 \"test\"", 4215 format("# \\\n line \\\n 42 \\\n \"test\"")); 4216 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); 4217 } 4218 4219 TEST_F(FormatTest, DoesntRemoveUnknownTokens) { 4220 verifyFormat("#define A \\x20"); 4221 verifyFormat("#define A \\ x20"); 4222 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20")); 4223 verifyFormat("#define A ''"); 4224 verifyFormat("#define A ''qqq"); 4225 verifyFormat("#define A `qqq"); 4226 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); 4227 EXPECT_EQ("const char *c = STRINGIFY(\n" 4228 "\\na : b);", 4229 format("const char * c = STRINGIFY(\n" 4230 "\\na : b);")); 4231 4232 verifyFormat("a\r\\"); 4233 verifyFormat("a\v\\"); 4234 verifyFormat("a\f\\"); 4235 } 4236 4237 TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) { 4238 FormatStyle style = getChromiumStyle(FormatStyle::LK_Cpp); 4239 style.IndentWidth = 4; 4240 style.PPIndentWidth = 1; 4241 4242 style.IndentPPDirectives = FormatStyle::PPDIS_None; 4243 verifyFormat("#ifdef __linux__\n" 4244 "void foo() {\n" 4245 " int x = 0;\n" 4246 "}\n" 4247 "#define FOO\n" 4248 "#endif\n" 4249 "void bar() {\n" 4250 " int y = 0;\n" 4251 "}\n", 4252 style); 4253 4254 style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 4255 verifyFormat("#ifdef __linux__\n" 4256 "void foo() {\n" 4257 " int x = 0;\n" 4258 "}\n" 4259 "# define FOO foo\n" 4260 "#endif\n" 4261 "void bar() {\n" 4262 " int y = 0;\n" 4263 "}\n", 4264 style); 4265 4266 style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash; 4267 verifyFormat("#ifdef __linux__\n" 4268 "void foo() {\n" 4269 " int x = 0;\n" 4270 "}\n" 4271 " #define FOO foo\n" 4272 "#endif\n" 4273 "void bar() {\n" 4274 " int y = 0;\n" 4275 "}\n", 4276 style); 4277 } 4278 4279 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 4280 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 4281 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 4282 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 4283 // FIXME: We never break before the macro name. 4284 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12)); 4285 4286 verifyFormat("#define A A\n#define A A"); 4287 verifyFormat("#define A(X) A\n#define A A"); 4288 4289 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 4290 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 4291 } 4292 4293 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 4294 EXPECT_EQ("// somecomment\n" 4295 "#include \"a.h\"\n" 4296 "#define A( \\\n" 4297 " A, B)\n" 4298 "#include \"b.h\"\n" 4299 "// somecomment\n", 4300 format(" // somecomment\n" 4301 " #include \"a.h\"\n" 4302 "#define A(A,\\\n" 4303 " B)\n" 4304 " #include \"b.h\"\n" 4305 " // somecomment\n", 4306 getLLVMStyleWithColumns(13))); 4307 } 4308 4309 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } 4310 4311 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 4312 EXPECT_EQ("#define A \\\n" 4313 " c; \\\n" 4314 " e;\n" 4315 "f;", 4316 format("#define A c; e;\n" 4317 "f;", 4318 getLLVMStyleWithColumns(14))); 4319 } 4320 4321 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } 4322 4323 TEST_F(FormatTest, MacroDefinitionInsideStatement) { 4324 EXPECT_EQ("int x,\n" 4325 "#define A\n" 4326 " y;", 4327 format("int x,\n#define A\ny;")); 4328 } 4329 4330 TEST_F(FormatTest, HashInMacroDefinition) { 4331 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle())); 4332 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 4333 verifyFormat("#define A \\\n" 4334 " { \\\n" 4335 " f(#c); \\\n" 4336 " }", 4337 getLLVMStyleWithColumns(11)); 4338 4339 verifyFormat("#define A(X) \\\n" 4340 " void function##X()", 4341 getLLVMStyleWithColumns(22)); 4342 4343 verifyFormat("#define A(a, b, c) \\\n" 4344 " void a##b##c()", 4345 getLLVMStyleWithColumns(22)); 4346 4347 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 4348 } 4349 4350 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 4351 EXPECT_EQ("#define A (x)", format("#define A (x)")); 4352 EXPECT_EQ("#define A(x)", format("#define A(x)")); 4353 4354 FormatStyle Style = getLLVMStyle(); 4355 Style.SpaceBeforeParens = FormatStyle::SBPO_Never; 4356 verifyFormat("#define true ((foo)1)", Style); 4357 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 4358 verifyFormat("#define false((foo)0)", Style); 4359 } 4360 4361 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 4362 EXPECT_EQ("#define A b;", format("#define A \\\n" 4363 " \\\n" 4364 " b;", 4365 getLLVMStyleWithColumns(25))); 4366 EXPECT_EQ("#define A \\\n" 4367 " \\\n" 4368 " a; \\\n" 4369 " b;", 4370 format("#define A \\\n" 4371 " \\\n" 4372 " a; \\\n" 4373 " b;", 4374 getLLVMStyleWithColumns(11))); 4375 EXPECT_EQ("#define A \\\n" 4376 " a; \\\n" 4377 " \\\n" 4378 " b;", 4379 format("#define A \\\n" 4380 " a; \\\n" 4381 " \\\n" 4382 " b;", 4383 getLLVMStyleWithColumns(11))); 4384 } 4385 4386 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 4387 verifyIncompleteFormat("#define A :"); 4388 verifyFormat("#define SOMECASES \\\n" 4389 " case 1: \\\n" 4390 " case 2\n", 4391 getLLVMStyleWithColumns(20)); 4392 verifyFormat("#define MACRO(a) \\\n" 4393 " if (a) \\\n" 4394 " f(); \\\n" 4395 " else \\\n" 4396 " g()", 4397 getLLVMStyleWithColumns(18)); 4398 verifyFormat("#define A template <typename T>"); 4399 verifyIncompleteFormat("#define STR(x) #x\n" 4400 "f(STR(this_is_a_string_literal{));"); 4401 verifyFormat("#pragma omp threadprivate( \\\n" 4402 " y)), // expected-warning", 4403 getLLVMStyleWithColumns(28)); 4404 verifyFormat("#d, = };"); 4405 verifyFormat("#if \"a"); 4406 verifyIncompleteFormat("({\n" 4407 "#define b \\\n" 4408 " } \\\n" 4409 " a\n" 4410 "a", 4411 getLLVMStyleWithColumns(15)); 4412 verifyFormat("#define A \\\n" 4413 " { \\\n" 4414 " {\n" 4415 "#define B \\\n" 4416 " } \\\n" 4417 " }", 4418 getLLVMStyleWithColumns(15)); 4419 verifyNoCrash("#if a\na(\n#else\n#endif\n{a"); 4420 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}"); 4421 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};"); 4422 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}"); 4423 } 4424 4425 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { 4426 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. 4427 EXPECT_EQ("class A : public QObject {\n" 4428 " Q_OBJECT\n" 4429 "\n" 4430 " A() {}\n" 4431 "};", 4432 format("class A : public QObject {\n" 4433 " Q_OBJECT\n" 4434 "\n" 4435 " A() {\n}\n" 4436 "} ;")); 4437 EXPECT_EQ("MACRO\n" 4438 "/*static*/ int i;", 4439 format("MACRO\n" 4440 " /*static*/ int i;")); 4441 EXPECT_EQ("SOME_MACRO\n" 4442 "namespace {\n" 4443 "void f();\n" 4444 "} // namespace", 4445 format("SOME_MACRO\n" 4446 " namespace {\n" 4447 "void f( );\n" 4448 "} // namespace")); 4449 // Only if the identifier contains at least 5 characters. 4450 EXPECT_EQ("HTTP f();", format("HTTP\nf();")); 4451 EXPECT_EQ("MACRO\nf();", format("MACRO\nf();")); 4452 // Only if everything is upper case. 4453 EXPECT_EQ("class A : public QObject {\n" 4454 " Q_Object A() {}\n" 4455 "};", 4456 format("class A : public QObject {\n" 4457 " Q_Object\n" 4458 " A() {\n}\n" 4459 "} ;")); 4460 4461 // Only if the next line can actually start an unwrapped line. 4462 EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;", 4463 format("SOME_WEIRD_LOG_MACRO\n" 4464 "<< SomeThing;")); 4465 4466 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " 4467 "(n, buffers))\n", 4468 getChromiumStyle(FormatStyle::LK_Cpp)); 4469 4470 // See PR41483 4471 EXPECT_EQ("/**/ FOO(a)\n" 4472 "FOO(b)", 4473 format("/**/ FOO(a)\n" 4474 "FOO(b)")); 4475 } 4476 4477 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { 4478 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 4479 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 4480 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 4481 "class X {};\n" 4482 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 4483 "int *createScopDetectionPass() { return 0; }", 4484 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 4485 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 4486 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 4487 " class X {};\n" 4488 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 4489 " int *createScopDetectionPass() { return 0; }")); 4490 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as 4491 // braces, so that inner block is indented one level more. 4492 EXPECT_EQ("int q() {\n" 4493 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 4494 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 4495 " IPC_END_MESSAGE_MAP()\n" 4496 "}", 4497 format("int q() {\n" 4498 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 4499 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 4500 " IPC_END_MESSAGE_MAP()\n" 4501 "}")); 4502 4503 // Same inside macros. 4504 EXPECT_EQ("#define LIST(L) \\\n" 4505 " L(A) \\\n" 4506 " L(B) \\\n" 4507 " L(C)", 4508 format("#define LIST(L) \\\n" 4509 " L(A) \\\n" 4510 " L(B) \\\n" 4511 " L(C)", 4512 getGoogleStyle())); 4513 4514 // These must not be recognized as macros. 4515 EXPECT_EQ("int q() {\n" 4516 " f(x);\n" 4517 " f(x) {}\n" 4518 " f(x)->g();\n" 4519 " f(x)->*g();\n" 4520 " f(x).g();\n" 4521 " f(x) = x;\n" 4522 " f(x) += x;\n" 4523 " f(x) -= x;\n" 4524 " f(x) *= x;\n" 4525 " f(x) /= x;\n" 4526 " f(x) %= x;\n" 4527 " f(x) &= x;\n" 4528 " f(x) |= x;\n" 4529 " f(x) ^= x;\n" 4530 " f(x) >>= x;\n" 4531 " f(x) <<= x;\n" 4532 " f(x)[y].z();\n" 4533 " LOG(INFO) << x;\n" 4534 " ifstream(x) >> x;\n" 4535 "}\n", 4536 format("int q() {\n" 4537 " f(x)\n;\n" 4538 " f(x)\n {}\n" 4539 " f(x)\n->g();\n" 4540 " f(x)\n->*g();\n" 4541 " f(x)\n.g();\n" 4542 " f(x)\n = x;\n" 4543 " f(x)\n += x;\n" 4544 " f(x)\n -= x;\n" 4545 " f(x)\n *= x;\n" 4546 " f(x)\n /= x;\n" 4547 " f(x)\n %= x;\n" 4548 " f(x)\n &= x;\n" 4549 " f(x)\n |= x;\n" 4550 " f(x)\n ^= x;\n" 4551 " f(x)\n >>= x;\n" 4552 " f(x)\n <<= x;\n" 4553 " f(x)\n[y].z();\n" 4554 " LOG(INFO)\n << x;\n" 4555 " ifstream(x)\n >> x;\n" 4556 "}\n")); 4557 EXPECT_EQ("int q() {\n" 4558 " F(x)\n" 4559 " if (1) {\n" 4560 " }\n" 4561 " F(x)\n" 4562 " while (1) {\n" 4563 " }\n" 4564 " F(x)\n" 4565 " G(x);\n" 4566 " F(x)\n" 4567 " try {\n" 4568 " Q();\n" 4569 " } catch (...) {\n" 4570 " }\n" 4571 "}\n", 4572 format("int q() {\n" 4573 "F(x)\n" 4574 "if (1) {}\n" 4575 "F(x)\n" 4576 "while (1) {}\n" 4577 "F(x)\n" 4578 "G(x);\n" 4579 "F(x)\n" 4580 "try { Q(); } catch (...) {}\n" 4581 "}\n")); 4582 EXPECT_EQ("class A {\n" 4583 " A() : t(0) {}\n" 4584 " A(int i) noexcept() : {}\n" 4585 " A(X x)\n" // FIXME: function-level try blocks are broken. 4586 " try : t(0) {\n" 4587 " } catch (...) {\n" 4588 " }\n" 4589 "};", 4590 format("class A {\n" 4591 " A()\n : t(0) {}\n" 4592 " A(int i)\n noexcept() : {}\n" 4593 " A(X x)\n" 4594 " try : t(0) {} catch (...) {}\n" 4595 "};")); 4596 FormatStyle Style = getLLVMStyle(); 4597 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4598 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 4599 Style.BraceWrapping.AfterFunction = true; 4600 EXPECT_EQ("void f()\n" 4601 "try\n" 4602 "{\n" 4603 "}", 4604 format("void f() try {\n" 4605 "}", 4606 Style)); 4607 EXPECT_EQ("class SomeClass {\n" 4608 "public:\n" 4609 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 4610 "};", 4611 format("class SomeClass {\n" 4612 "public:\n" 4613 " SomeClass()\n" 4614 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 4615 "};")); 4616 EXPECT_EQ("class SomeClass {\n" 4617 "public:\n" 4618 " SomeClass()\n" 4619 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 4620 "};", 4621 format("class SomeClass {\n" 4622 "public:\n" 4623 " SomeClass()\n" 4624 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 4625 "};", 4626 getLLVMStyleWithColumns(40))); 4627 4628 verifyFormat("MACRO(>)"); 4629 4630 // Some macros contain an implicit semicolon. 4631 Style = getLLVMStyle(); 4632 Style.StatementMacros.push_back("FOO"); 4633 verifyFormat("FOO(a) int b = 0;"); 4634 verifyFormat("FOO(a)\n" 4635 "int b = 0;", 4636 Style); 4637 verifyFormat("FOO(a);\n" 4638 "int b = 0;", 4639 Style); 4640 verifyFormat("FOO(argc, argv, \"4.0.2\")\n" 4641 "int b = 0;", 4642 Style); 4643 verifyFormat("FOO()\n" 4644 "int b = 0;", 4645 Style); 4646 verifyFormat("FOO\n" 4647 "int b = 0;", 4648 Style); 4649 verifyFormat("void f() {\n" 4650 " FOO(a)\n" 4651 " return a;\n" 4652 "}", 4653 Style); 4654 verifyFormat("FOO(a)\n" 4655 "FOO(b)", 4656 Style); 4657 verifyFormat("int a = 0;\n" 4658 "FOO(b)\n" 4659 "int c = 0;", 4660 Style); 4661 verifyFormat("int a = 0;\n" 4662 "int x = FOO(a)\n" 4663 "int b = 0;", 4664 Style); 4665 verifyFormat("void foo(int a) { FOO(a) }\n" 4666 "uint32_t bar() {}", 4667 Style); 4668 } 4669 4670 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { 4671 verifyFormat("#define A \\\n" 4672 " f({ \\\n" 4673 " g(); \\\n" 4674 " });", 4675 getLLVMStyleWithColumns(11)); 4676 } 4677 4678 TEST_F(FormatTest, IndentPreprocessorDirectives) { 4679 FormatStyle Style = getLLVMStyle(); 4680 Style.IndentPPDirectives = FormatStyle::PPDIS_None; 4681 Style.ColumnLimit = 40; 4682 verifyFormat("#ifdef _WIN32\n" 4683 "#define A 0\n" 4684 "#ifdef VAR2\n" 4685 "#define B 1\n" 4686 "#include <someheader.h>\n" 4687 "#define MACRO \\\n" 4688 " some_very_long_func_aaaaaaaaaa();\n" 4689 "#endif\n" 4690 "#else\n" 4691 "#define A 1\n" 4692 "#endif", 4693 Style); 4694 Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 4695 verifyFormat("#ifdef _WIN32\n" 4696 "# define A 0\n" 4697 "# ifdef VAR2\n" 4698 "# define B 1\n" 4699 "# include <someheader.h>\n" 4700 "# define MACRO \\\n" 4701 " some_very_long_func_aaaaaaaaaa();\n" 4702 "# endif\n" 4703 "#else\n" 4704 "# define A 1\n" 4705 "#endif", 4706 Style); 4707 verifyFormat("#if A\n" 4708 "# define MACRO \\\n" 4709 " void a(int x) { \\\n" 4710 " b(); \\\n" 4711 " c(); \\\n" 4712 " d(); \\\n" 4713 " e(); \\\n" 4714 " f(); \\\n" 4715 " }\n" 4716 "#endif", 4717 Style); 4718 // Comments before include guard. 4719 verifyFormat("// file comment\n" 4720 "// file comment\n" 4721 "#ifndef HEADER_H\n" 4722 "#define HEADER_H\n" 4723 "code();\n" 4724 "#endif", 4725 Style); 4726 // Test with include guards. 4727 verifyFormat("#ifndef HEADER_H\n" 4728 "#define HEADER_H\n" 4729 "code();\n" 4730 "#endif", 4731 Style); 4732 // Include guards must have a #define with the same variable immediately 4733 // after #ifndef. 4734 verifyFormat("#ifndef NOT_GUARD\n" 4735 "# define FOO\n" 4736 "code();\n" 4737 "#endif", 4738 Style); 4739 4740 // Include guards must cover the entire file. 4741 verifyFormat("code();\n" 4742 "code();\n" 4743 "#ifndef NOT_GUARD\n" 4744 "# define NOT_GUARD\n" 4745 "code();\n" 4746 "#endif", 4747 Style); 4748 verifyFormat("#ifndef NOT_GUARD\n" 4749 "# define NOT_GUARD\n" 4750 "code();\n" 4751 "#endif\n" 4752 "code();", 4753 Style); 4754 // Test with trailing blank lines. 4755 verifyFormat("#ifndef HEADER_H\n" 4756 "#define HEADER_H\n" 4757 "code();\n" 4758 "#endif\n", 4759 Style); 4760 // Include guards don't have #else. 4761 verifyFormat("#ifndef NOT_GUARD\n" 4762 "# define NOT_GUARD\n" 4763 "code();\n" 4764 "#else\n" 4765 "#endif", 4766 Style); 4767 verifyFormat("#ifndef NOT_GUARD\n" 4768 "# define NOT_GUARD\n" 4769 "code();\n" 4770 "#elif FOO\n" 4771 "#endif", 4772 Style); 4773 // Non-identifier #define after potential include guard. 4774 verifyFormat("#ifndef FOO\n" 4775 "# define 1\n" 4776 "#endif\n", 4777 Style); 4778 // #if closes past last non-preprocessor line. 4779 verifyFormat("#ifndef FOO\n" 4780 "#define FOO\n" 4781 "#if 1\n" 4782 "int i;\n" 4783 "# define A 0\n" 4784 "#endif\n" 4785 "#endif\n", 4786 Style); 4787 // Don't crash if there is an #elif directive without a condition. 4788 verifyFormat("#if 1\n" 4789 "int x;\n" 4790 "#elif\n" 4791 "int y;\n" 4792 "#else\n" 4793 "int z;\n" 4794 "#endif", 4795 Style); 4796 // FIXME: This doesn't handle the case where there's code between the 4797 // #ifndef and #define but all other conditions hold. This is because when 4798 // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the 4799 // previous code line yet, so we can't detect it. 4800 EXPECT_EQ("#ifndef NOT_GUARD\n" 4801 "code();\n" 4802 "#define NOT_GUARD\n" 4803 "code();\n" 4804 "#endif", 4805 format("#ifndef NOT_GUARD\n" 4806 "code();\n" 4807 "# define NOT_GUARD\n" 4808 "code();\n" 4809 "#endif", 4810 Style)); 4811 // FIXME: This doesn't handle cases where legitimate preprocessor lines may 4812 // be outside an include guard. Examples are #pragma once and 4813 // #pragma GCC diagnostic, or anything else that does not change the meaning 4814 // of the file if it's included multiple times. 4815 EXPECT_EQ("#ifdef WIN32\n" 4816 "# pragma once\n" 4817 "#endif\n" 4818 "#ifndef HEADER_H\n" 4819 "# define HEADER_H\n" 4820 "code();\n" 4821 "#endif", 4822 format("#ifdef WIN32\n" 4823 "# pragma once\n" 4824 "#endif\n" 4825 "#ifndef HEADER_H\n" 4826 "#define HEADER_H\n" 4827 "code();\n" 4828 "#endif", 4829 Style)); 4830 // FIXME: This does not detect when there is a single non-preprocessor line 4831 // in front of an include-guard-like structure where other conditions hold 4832 // because ScopedLineState hides the line. 4833 EXPECT_EQ("code();\n" 4834 "#ifndef HEADER_H\n" 4835 "#define HEADER_H\n" 4836 "code();\n" 4837 "#endif", 4838 format("code();\n" 4839 "#ifndef HEADER_H\n" 4840 "# define HEADER_H\n" 4841 "code();\n" 4842 "#endif", 4843 Style)); 4844 // Keep comments aligned with #, otherwise indent comments normally. These 4845 // tests cannot use verifyFormat because messUp manipulates leading 4846 // whitespace. 4847 { 4848 const char *Expected = "" 4849 "void f() {\n" 4850 "#if 1\n" 4851 "// Preprocessor aligned.\n" 4852 "# define A 0\n" 4853 " // Code. Separated by blank line.\n" 4854 "\n" 4855 "# define B 0\n" 4856 " // Code. Not aligned with #\n" 4857 "# define C 0\n" 4858 "#endif"; 4859 const char *ToFormat = "" 4860 "void f() {\n" 4861 "#if 1\n" 4862 "// Preprocessor aligned.\n" 4863 "# define A 0\n" 4864 "// Code. Separated by blank line.\n" 4865 "\n" 4866 "# define B 0\n" 4867 " // Code. Not aligned with #\n" 4868 "# define C 0\n" 4869 "#endif"; 4870 EXPECT_EQ(Expected, format(ToFormat, Style)); 4871 EXPECT_EQ(Expected, format(Expected, Style)); 4872 } 4873 // Keep block quotes aligned. 4874 { 4875 const char *Expected = "" 4876 "void f() {\n" 4877 "#if 1\n" 4878 "/* Preprocessor aligned. */\n" 4879 "# define A 0\n" 4880 " /* Code. Separated by blank line. */\n" 4881 "\n" 4882 "# define B 0\n" 4883 " /* Code. Not aligned with # */\n" 4884 "# define C 0\n" 4885 "#endif"; 4886 const char *ToFormat = "" 4887 "void f() {\n" 4888 "#if 1\n" 4889 "/* Preprocessor aligned. */\n" 4890 "# define A 0\n" 4891 "/* Code. Separated by blank line. */\n" 4892 "\n" 4893 "# define B 0\n" 4894 " /* Code. Not aligned with # */\n" 4895 "# define C 0\n" 4896 "#endif"; 4897 EXPECT_EQ(Expected, format(ToFormat, Style)); 4898 EXPECT_EQ(Expected, format(Expected, Style)); 4899 } 4900 // Keep comments aligned with un-indented directives. 4901 { 4902 const char *Expected = "" 4903 "void f() {\n" 4904 "// Preprocessor aligned.\n" 4905 "#define A 0\n" 4906 " // Code. Separated by blank line.\n" 4907 "\n" 4908 "#define B 0\n" 4909 " // Code. Not aligned with #\n" 4910 "#define C 0\n"; 4911 const char *ToFormat = "" 4912 "void f() {\n" 4913 "// Preprocessor aligned.\n" 4914 "#define A 0\n" 4915 "// Code. Separated by blank line.\n" 4916 "\n" 4917 "#define B 0\n" 4918 " // Code. Not aligned with #\n" 4919 "#define C 0\n"; 4920 EXPECT_EQ(Expected, format(ToFormat, Style)); 4921 EXPECT_EQ(Expected, format(Expected, Style)); 4922 } 4923 // Test AfterHash with tabs. 4924 { 4925 FormatStyle Tabbed = Style; 4926 Tabbed.UseTab = FormatStyle::UT_Always; 4927 Tabbed.IndentWidth = 8; 4928 Tabbed.TabWidth = 8; 4929 verifyFormat("#ifdef _WIN32\n" 4930 "#\tdefine A 0\n" 4931 "#\tifdef VAR2\n" 4932 "#\t\tdefine B 1\n" 4933 "#\t\tinclude <someheader.h>\n" 4934 "#\t\tdefine MACRO \\\n" 4935 "\t\t\tsome_very_long_func_aaaaaaaaaa();\n" 4936 "#\tendif\n" 4937 "#else\n" 4938 "#\tdefine A 1\n" 4939 "#endif", 4940 Tabbed); 4941 } 4942 4943 // Regression test: Multiline-macro inside include guards. 4944 verifyFormat("#ifndef HEADER_H\n" 4945 "#define HEADER_H\n" 4946 "#define A() \\\n" 4947 " int i; \\\n" 4948 " int j;\n" 4949 "#endif // HEADER_H", 4950 getLLVMStyleWithColumns(20)); 4951 4952 Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash; 4953 // Basic before hash indent tests 4954 verifyFormat("#ifdef _WIN32\n" 4955 " #define A 0\n" 4956 " #ifdef VAR2\n" 4957 " #define B 1\n" 4958 " #include <someheader.h>\n" 4959 " #define MACRO \\\n" 4960 " some_very_long_func_aaaaaaaaaa();\n" 4961 " #endif\n" 4962 "#else\n" 4963 " #define A 1\n" 4964 "#endif", 4965 Style); 4966 verifyFormat("#if A\n" 4967 " #define MACRO \\\n" 4968 " void a(int x) { \\\n" 4969 " b(); \\\n" 4970 " c(); \\\n" 4971 " d(); \\\n" 4972 " e(); \\\n" 4973 " f(); \\\n" 4974 " }\n" 4975 "#endif", 4976 Style); 4977 // Keep comments aligned with indented directives. These 4978 // tests cannot use verifyFormat because messUp manipulates leading 4979 // whitespace. 4980 { 4981 const char *Expected = "void f() {\n" 4982 "// Aligned to preprocessor.\n" 4983 "#if 1\n" 4984 " // Aligned to code.\n" 4985 " int a;\n" 4986 " #if 1\n" 4987 " // Aligned to preprocessor.\n" 4988 " #define A 0\n" 4989 " // Aligned to code.\n" 4990 " int b;\n" 4991 " #endif\n" 4992 "#endif\n" 4993 "}"; 4994 const char *ToFormat = "void f() {\n" 4995 "// Aligned to preprocessor.\n" 4996 "#if 1\n" 4997 "// Aligned to code.\n" 4998 "int a;\n" 4999 "#if 1\n" 5000 "// Aligned to preprocessor.\n" 5001 "#define A 0\n" 5002 "// Aligned to code.\n" 5003 "int b;\n" 5004 "#endif\n" 5005 "#endif\n" 5006 "}"; 5007 EXPECT_EQ(Expected, format(ToFormat, Style)); 5008 EXPECT_EQ(Expected, format(Expected, Style)); 5009 } 5010 { 5011 const char *Expected = "void f() {\n" 5012 "/* Aligned to preprocessor. */\n" 5013 "#if 1\n" 5014 " /* Aligned to code. */\n" 5015 " int a;\n" 5016 " #if 1\n" 5017 " /* Aligned to preprocessor. */\n" 5018 " #define A 0\n" 5019 " /* Aligned to code. */\n" 5020 " int b;\n" 5021 " #endif\n" 5022 "#endif\n" 5023 "}"; 5024 const char *ToFormat = "void f() {\n" 5025 "/* Aligned to preprocessor. */\n" 5026 "#if 1\n" 5027 "/* Aligned to code. */\n" 5028 "int a;\n" 5029 "#if 1\n" 5030 "/* Aligned to preprocessor. */\n" 5031 "#define A 0\n" 5032 "/* Aligned to code. */\n" 5033 "int b;\n" 5034 "#endif\n" 5035 "#endif\n" 5036 "}"; 5037 EXPECT_EQ(Expected, format(ToFormat, Style)); 5038 EXPECT_EQ(Expected, format(Expected, Style)); 5039 } 5040 5041 // Test single comment before preprocessor 5042 verifyFormat("// Comment\n" 5043 "\n" 5044 "#if 1\n" 5045 "#endif", 5046 Style); 5047 } 5048 5049 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 5050 verifyFormat("{\n { a #c; }\n}"); 5051 } 5052 5053 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 5054 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 5055 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 5056 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 5057 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 5058 } 5059 5060 TEST_F(FormatTest, EscapedNewlines) { 5061 FormatStyle Narrow = getLLVMStyleWithColumns(11); 5062 EXPECT_EQ("#define A \\\n int i; \\\n int j;", 5063 format("#define A \\\nint i;\\\n int j;", Narrow)); 5064 EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;")); 5065 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 5066 EXPECT_EQ("/* \\ \\ \\\n */", format("\\\n/* \\ \\ \\\n */")); 5067 EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>")); 5068 5069 FormatStyle AlignLeft = getLLVMStyle(); 5070 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 5071 EXPECT_EQ("#define MACRO(x) \\\n" 5072 "private: \\\n" 5073 " int x(int a);\n", 5074 format("#define MACRO(x) \\\n" 5075 "private: \\\n" 5076 " int x(int a);\n", 5077 AlignLeft)); 5078 5079 // CRLF line endings 5080 EXPECT_EQ("#define A \\\r\n int i; \\\r\n int j;", 5081 format("#define A \\\r\nint i;\\\r\n int j;", Narrow)); 5082 EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;")); 5083 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 5084 EXPECT_EQ("/* \\ \\ \\\r\n */", format("\\\r\n/* \\ \\ \\\r\n */")); 5085 EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>")); 5086 EXPECT_EQ("#define MACRO(x) \\\r\n" 5087 "private: \\\r\n" 5088 " int x(int a);\r\n", 5089 format("#define MACRO(x) \\\r\n" 5090 "private: \\\r\n" 5091 " int x(int a);\r\n", 5092 AlignLeft)); 5093 5094 FormatStyle DontAlign = getLLVMStyle(); 5095 DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 5096 DontAlign.MaxEmptyLinesToKeep = 3; 5097 // FIXME: can't use verifyFormat here because the newline before 5098 // "public:" is not inserted the first time it's reformatted 5099 EXPECT_EQ("#define A \\\n" 5100 " class Foo { \\\n" 5101 " void bar(); \\\n" 5102 "\\\n" 5103 "\\\n" 5104 "\\\n" 5105 " public: \\\n" 5106 " void baz(); \\\n" 5107 " };", 5108 format("#define A \\\n" 5109 " class Foo { \\\n" 5110 " void bar(); \\\n" 5111 "\\\n" 5112 "\\\n" 5113 "\\\n" 5114 " public: \\\n" 5115 " void baz(); \\\n" 5116 " };", 5117 DontAlign)); 5118 } 5119 5120 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 5121 verifyFormat("#define A \\\n" 5122 " int v( \\\n" 5123 " a); \\\n" 5124 " int i;", 5125 getLLVMStyleWithColumns(11)); 5126 } 5127 5128 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 5129 EXPECT_EQ( 5130 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 5131 " \\\n" 5132 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 5133 "\n" 5134 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 5135 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 5136 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 5137 "\\\n" 5138 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 5139 " \n" 5140 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 5141 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 5142 } 5143 5144 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 5145 EXPECT_EQ("int\n" 5146 "#define A\n" 5147 " a;", 5148 format("int\n#define A\na;")); 5149 verifyFormat("functionCallTo(\n" 5150 " someOtherFunction(\n" 5151 " withSomeParameters, whichInSequence,\n" 5152 " areLongerThanALine(andAnotherCall,\n" 5153 "#define A B\n" 5154 " withMoreParamters,\n" 5155 " whichStronglyInfluenceTheLayout),\n" 5156 " andMoreParameters),\n" 5157 " trailing);", 5158 getLLVMStyleWithColumns(69)); 5159 verifyFormat("Foo::Foo()\n" 5160 "#ifdef BAR\n" 5161 " : baz(0)\n" 5162 "#endif\n" 5163 "{\n" 5164 "}"); 5165 verifyFormat("void f() {\n" 5166 " if (true)\n" 5167 "#ifdef A\n" 5168 " f(42);\n" 5169 " x();\n" 5170 "#else\n" 5171 " g();\n" 5172 " x();\n" 5173 "#endif\n" 5174 "}"); 5175 verifyFormat("void f(param1, param2,\n" 5176 " param3,\n" 5177 "#ifdef A\n" 5178 " param4(param5,\n" 5179 "#ifdef A1\n" 5180 " param6,\n" 5181 "#ifdef A2\n" 5182 " param7),\n" 5183 "#else\n" 5184 " param8),\n" 5185 " param9,\n" 5186 "#endif\n" 5187 " param10,\n" 5188 "#endif\n" 5189 " param11)\n" 5190 "#else\n" 5191 " param12)\n" 5192 "#endif\n" 5193 "{\n" 5194 " x();\n" 5195 "}", 5196 getLLVMStyleWithColumns(28)); 5197 verifyFormat("#if 1\n" 5198 "int i;"); 5199 verifyFormat("#if 1\n" 5200 "#endif\n" 5201 "#if 1\n" 5202 "#else\n" 5203 "#endif\n"); 5204 verifyFormat("DEBUG({\n" 5205 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5206 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 5207 "});\n" 5208 "#if a\n" 5209 "#else\n" 5210 "#endif"); 5211 5212 verifyIncompleteFormat("void f(\n" 5213 "#if A\n" 5214 ");\n" 5215 "#else\n" 5216 "#endif"); 5217 } 5218 5219 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { 5220 verifyFormat("#endif\n" 5221 "#if B"); 5222 } 5223 5224 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { 5225 FormatStyle SingleLine = getLLVMStyle(); 5226 SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; 5227 verifyFormat("#if 0\n" 5228 "#elif 1\n" 5229 "#endif\n" 5230 "void foo() {\n" 5231 " if (test) foo2();\n" 5232 "}", 5233 SingleLine); 5234 } 5235 5236 TEST_F(FormatTest, LayoutBlockInsideParens) { 5237 verifyFormat("functionCall({ int i; });"); 5238 verifyFormat("functionCall({\n" 5239 " int i;\n" 5240 " int j;\n" 5241 "});"); 5242 verifyFormat("functionCall(\n" 5243 " {\n" 5244 " int i;\n" 5245 " int j;\n" 5246 " },\n" 5247 " aaaa, bbbb, cccc);"); 5248 verifyFormat("functionA(functionB({\n" 5249 " int i;\n" 5250 " int j;\n" 5251 " }),\n" 5252 " aaaa, bbbb, cccc);"); 5253 verifyFormat("functionCall(\n" 5254 " {\n" 5255 " int i;\n" 5256 " int j;\n" 5257 " },\n" 5258 " aaaa, bbbb, // comment\n" 5259 " cccc);"); 5260 verifyFormat("functionA(functionB({\n" 5261 " int i;\n" 5262 " int j;\n" 5263 " }),\n" 5264 " aaaa, bbbb, // comment\n" 5265 " cccc);"); 5266 verifyFormat("functionCall(aaaa, bbbb, { int i; });"); 5267 verifyFormat("functionCall(aaaa, bbbb, {\n" 5268 " int i;\n" 5269 " int j;\n" 5270 "});"); 5271 verifyFormat( 5272 "Aaa(\n" // FIXME: There shouldn't be a linebreak here. 5273 " {\n" 5274 " int i; // break\n" 5275 " },\n" 5276 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 5277 " ccccccccccccccccc));"); 5278 verifyFormat("DEBUG({\n" 5279 " if (a)\n" 5280 " f();\n" 5281 "});"); 5282 } 5283 5284 TEST_F(FormatTest, LayoutBlockInsideStatement) { 5285 EXPECT_EQ("SOME_MACRO { int i; }\n" 5286 "int i;", 5287 format(" SOME_MACRO {int i;} int i;")); 5288 } 5289 5290 TEST_F(FormatTest, LayoutNestedBlocks) { 5291 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 5292 " struct s {\n" 5293 " int i;\n" 5294 " };\n" 5295 " s kBitsToOs[] = {{10}};\n" 5296 " for (int i = 0; i < 10; ++i)\n" 5297 " return;\n" 5298 "}"); 5299 verifyFormat("call(parameter, {\n" 5300 " something();\n" 5301 " // Comment using all columns.\n" 5302 " somethingelse();\n" 5303 "});", 5304 getLLVMStyleWithColumns(40)); 5305 verifyFormat("DEBUG( //\n" 5306 " { f(); }, a);"); 5307 verifyFormat("DEBUG( //\n" 5308 " {\n" 5309 " f(); //\n" 5310 " },\n" 5311 " a);"); 5312 5313 EXPECT_EQ("call(parameter, {\n" 5314 " something();\n" 5315 " // Comment too\n" 5316 " // looooooooooong.\n" 5317 " somethingElse();\n" 5318 "});", 5319 format("call(parameter, {\n" 5320 " something();\n" 5321 " // Comment too looooooooooong.\n" 5322 " somethingElse();\n" 5323 "});", 5324 getLLVMStyleWithColumns(29))); 5325 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });")); 5326 EXPECT_EQ("DEBUG({ // comment\n" 5327 " int i;\n" 5328 "});", 5329 format("DEBUG({ // comment\n" 5330 "int i;\n" 5331 "});")); 5332 EXPECT_EQ("DEBUG({\n" 5333 " int i;\n" 5334 "\n" 5335 " // comment\n" 5336 " int j;\n" 5337 "});", 5338 format("DEBUG({\n" 5339 " int i;\n" 5340 "\n" 5341 " // comment\n" 5342 " int j;\n" 5343 "});")); 5344 5345 verifyFormat("DEBUG({\n" 5346 " if (a)\n" 5347 " return;\n" 5348 "});"); 5349 verifyGoogleFormat("DEBUG({\n" 5350 " if (a) return;\n" 5351 "});"); 5352 FormatStyle Style = getGoogleStyle(); 5353 Style.ColumnLimit = 45; 5354 verifyFormat("Debug(\n" 5355 " aaaaa,\n" 5356 " {\n" 5357 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" 5358 " },\n" 5359 " a);", 5360 Style); 5361 5362 verifyFormat("SomeFunction({MACRO({ return output; }), b});"); 5363 5364 verifyNoCrash("^{v^{a}}"); 5365 } 5366 5367 TEST_F(FormatTest, FormatNestedBlocksInMacros) { 5368 EXPECT_EQ("#define MACRO() \\\n" 5369 " Debug(aaa, /* force line break */ \\\n" 5370 " { \\\n" 5371 " int i; \\\n" 5372 " int j; \\\n" 5373 " })", 5374 format("#define MACRO() Debug(aaa, /* force line break */ \\\n" 5375 " { int i; int j; })", 5376 getGoogleStyle())); 5377 5378 EXPECT_EQ("#define A \\\n" 5379 " [] { \\\n" 5380 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 5381 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 5382 " }", 5383 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 5384 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 5385 getGoogleStyle())); 5386 } 5387 5388 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 5389 EXPECT_EQ("{}", format("{}")); 5390 verifyFormat("enum E {};"); 5391 verifyFormat("enum E {}"); 5392 FormatStyle Style = getLLVMStyle(); 5393 Style.SpaceInEmptyBlock = true; 5394 EXPECT_EQ("void f() { }", format("void f() {}", Style)); 5395 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 5396 EXPECT_EQ("while (true) { }", format("while (true) {}", Style)); 5397 } 5398 5399 TEST_F(FormatTest, FormatBeginBlockEndMacros) { 5400 FormatStyle Style = getLLVMStyle(); 5401 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$"; 5402 Style.MacroBlockEnd = "^[A-Z_]+_END$"; 5403 verifyFormat("FOO_BEGIN\n" 5404 " FOO_ENTRY\n" 5405 "FOO_END", 5406 Style); 5407 verifyFormat("FOO_BEGIN\n" 5408 " NESTED_FOO_BEGIN\n" 5409 " NESTED_FOO_ENTRY\n" 5410 " NESTED_FOO_END\n" 5411 "FOO_END", 5412 Style); 5413 verifyFormat("FOO_BEGIN(Foo, Bar)\n" 5414 " int x;\n" 5415 " x = 1;\n" 5416 "FOO_END(Baz)", 5417 Style); 5418 } 5419 5420 //===----------------------------------------------------------------------===// 5421 // Line break tests. 5422 //===----------------------------------------------------------------------===// 5423 5424 TEST_F(FormatTest, PreventConfusingIndents) { 5425 verifyFormat( 5426 "void f() {\n" 5427 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 5428 " parameter, parameter, parameter)),\n" 5429 " SecondLongCall(parameter));\n" 5430 "}"); 5431 verifyFormat( 5432 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5433 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 5434 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5435 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 5436 verifyFormat( 5437 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5438 " [aaaaaaaaaaaaaaaaaaaaaaaa\n" 5439 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 5440 " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); 5441 verifyFormat( 5442 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 5443 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 5444 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 5445 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 5446 verifyFormat("int a = bbbb && ccc &&\n" 5447 " fffff(\n" 5448 "#define A Just forcing a new line\n" 5449 " ddd);"); 5450 } 5451 5452 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 5453 verifyFormat( 5454 "bool aaaaaaa =\n" 5455 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 5456 " bbbbbbbb();"); 5457 verifyFormat( 5458 "bool aaaaaaa =\n" 5459 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" 5460 " bbbbbbbb();"); 5461 5462 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 5463 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 5464 " ccccccccc == ddddddddddd;"); 5465 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 5466 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" 5467 " ccccccccc == ddddddddddd;"); 5468 verifyFormat( 5469 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 5470 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" 5471 " ccccccccc == ddddddddddd;"); 5472 5473 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 5474 " aaaaaa) &&\n" 5475 " bbbbbb && cccccc;"); 5476 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 5477 " aaaaaa) >>\n" 5478 " bbbbbb;"); 5479 verifyFormat("aa = Whitespaces.addUntouchableComment(\n" 5480 " SourceMgr.getSpellingColumnNumber(\n" 5481 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 5482 " 1);"); 5483 5484 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5485 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 5486 " cccccc) {\n}"); 5487 verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5488 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 5489 " cccccc) {\n}"); 5490 verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5491 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 5492 " cccccc) {\n}"); 5493 verifyFormat("b = a &&\n" 5494 " // Comment\n" 5495 " b.c && d;"); 5496 5497 // If the LHS of a comparison is not a binary expression itself, the 5498 // additional linebreak confuses many people. 5499 verifyFormat( 5500 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5501 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 5502 "}"); 5503 verifyFormat( 5504 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5505 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 5506 "}"); 5507 verifyFormat( 5508 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 5509 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 5510 "}"); 5511 verifyFormat( 5512 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5513 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n" 5514 "}"); 5515 // Even explicit parentheses stress the precedence enough to make the 5516 // additional break unnecessary. 5517 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5518 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 5519 "}"); 5520 // This cases is borderline, but with the indentation it is still readable. 5521 verifyFormat( 5522 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5523 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5524 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 5525 "}", 5526 getLLVMStyleWithColumns(75)); 5527 5528 // If the LHS is a binary expression, we should still use the additional break 5529 // as otherwise the formatting hides the operator precedence. 5530 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5531 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5532 " 5) {\n" 5533 "}"); 5534 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5535 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n" 5536 " 5) {\n" 5537 "}"); 5538 5539 FormatStyle OnePerLine = getLLVMStyle(); 5540 OnePerLine.BinPackParameters = false; 5541 verifyFormat( 5542 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5543 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5544 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 5545 OnePerLine); 5546 5547 verifyFormat("int i = someFunction(aaaaaaa, 0)\n" 5548 " .aaa(aaaaaaaaaaaaa) *\n" 5549 " aaaaaaa +\n" 5550 " aaaaaaa;", 5551 getLLVMStyleWithColumns(40)); 5552 } 5553 5554 TEST_F(FormatTest, ExpressionIndentation) { 5555 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5556 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5557 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5558 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5559 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 5560 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 5561 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5562 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 5563 " ccccccccccccccccccccccccccccccccccccccccc;"); 5564 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5565 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5566 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5567 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 5568 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5569 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5570 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5571 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 5572 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5573 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5574 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5575 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 5576 verifyFormat("if () {\n" 5577 "} else if (aaaaa && bbbbb > // break\n" 5578 " ccccc) {\n" 5579 "}"); 5580 verifyFormat("if () {\n" 5581 "} else if constexpr (aaaaa && bbbbb > // break\n" 5582 " ccccc) {\n" 5583 "}"); 5584 verifyFormat("if () {\n" 5585 "} else if CONSTEXPR (aaaaa && bbbbb > // break\n" 5586 " ccccc) {\n" 5587 "}"); 5588 verifyFormat("if () {\n" 5589 "} else if (aaaaa &&\n" 5590 " bbbbb > // break\n" 5591 " ccccc &&\n" 5592 " ddddd) {\n" 5593 "}"); 5594 5595 // Presence of a trailing comment used to change indentation of b. 5596 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 5597 " b;\n" 5598 "return aaaaaaaaaaaaaaaaaaa +\n" 5599 " b; //", 5600 getLLVMStyleWithColumns(30)); 5601 } 5602 5603 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 5604 // Not sure what the best system is here. Like this, the LHS can be found 5605 // immediately above an operator (everything with the same or a higher 5606 // indent). The RHS is aligned right of the operator and so compasses 5607 // everything until something with the same indent as the operator is found. 5608 // FIXME: Is this a good system? 5609 FormatStyle Style = getLLVMStyle(); 5610 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 5611 verifyFormat( 5612 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5613 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5614 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5615 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5616 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5617 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5618 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5619 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5620 " > ccccccccccccccccccccccccccccccccccccccccc;", 5621 Style); 5622 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5623 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5624 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5625 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5626 Style); 5627 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5628 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5629 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5630 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5631 Style); 5632 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5633 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5634 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5635 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5636 Style); 5637 verifyFormat("if () {\n" 5638 "} else if (aaaaa\n" 5639 " && bbbbb // break\n" 5640 " > ccccc) {\n" 5641 "}", 5642 Style); 5643 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5644 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 5645 Style); 5646 verifyFormat("return (a)\n" 5647 " // comment\n" 5648 " + b;", 5649 Style); 5650 verifyFormat( 5651 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5652 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5653 " + cc;", 5654 Style); 5655 5656 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5657 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5658 Style); 5659 5660 // Forced by comments. 5661 verifyFormat( 5662 "unsigned ContentSize =\n" 5663 " sizeof(int16_t) // DWARF ARange version number\n" 5664 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 5665 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 5666 " + sizeof(int8_t); // Segment Size (in bytes)"); 5667 5668 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 5669 " == boost::fusion::at_c<1>(iiii).second;", 5670 Style); 5671 5672 Style.ColumnLimit = 60; 5673 verifyFormat("zzzzzzzzzz\n" 5674 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5675 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 5676 Style); 5677 5678 Style.ColumnLimit = 80; 5679 Style.IndentWidth = 4; 5680 Style.TabWidth = 4; 5681 Style.UseTab = FormatStyle::UT_Always; 5682 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5683 Style.AlignOperands = FormatStyle::OAS_DontAlign; 5684 EXPECT_EQ("return someVeryVeryLongConditionThatBarelyFitsOnALine\n" 5685 "\t&& (someOtherLongishConditionPart1\n" 5686 "\t\t|| someOtherEvenLongerNestedConditionPart2);", 5687 format("return someVeryVeryLongConditionThatBarelyFitsOnALine && " 5688 "(someOtherLongishConditionPart1 || " 5689 "someOtherEvenLongerNestedConditionPart2);", 5690 Style)); 5691 } 5692 5693 TEST_F(FormatTest, ExpressionIndentationStrictAlign) { 5694 FormatStyle Style = getLLVMStyle(); 5695 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 5696 Style.AlignOperands = FormatStyle::OAS_AlignAfterOperator; 5697 5698 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5699 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5700 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5701 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5702 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5703 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5704 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5705 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5706 " > ccccccccccccccccccccccccccccccccccccccccc;", 5707 Style); 5708 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5709 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5710 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5711 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5712 Style); 5713 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5714 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5715 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5716 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5717 Style); 5718 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5719 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5720 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5721 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5722 Style); 5723 verifyFormat("if () {\n" 5724 "} else if (aaaaa\n" 5725 " && bbbbb // break\n" 5726 " > ccccc) {\n" 5727 "}", 5728 Style); 5729 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5730 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 5731 Style); 5732 verifyFormat("return (a)\n" 5733 " // comment\n" 5734 " + b;", 5735 Style); 5736 verifyFormat( 5737 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5738 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5739 " + cc;", 5740 Style); 5741 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 5742 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 5743 " : 3333333333333333;", 5744 Style); 5745 verifyFormat( 5746 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 5747 " : ccccccccccccccc ? dddddddddddddddddd\n" 5748 " : eeeeeeeeeeeeeeeeee)\n" 5749 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 5750 " : 3333333333333333;", 5751 Style); 5752 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5753 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5754 Style); 5755 5756 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 5757 " == boost::fusion::at_c<1>(iiii).second;", 5758 Style); 5759 5760 Style.ColumnLimit = 60; 5761 verifyFormat("zzzzzzzzzzzzz\n" 5762 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5763 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 5764 Style); 5765 5766 // Forced by comments. 5767 Style.ColumnLimit = 80; 5768 verifyFormat( 5769 "unsigned ContentSize\n" 5770 " = sizeof(int16_t) // DWARF ARange version number\n" 5771 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 5772 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 5773 " + sizeof(int8_t); // Segment Size (in bytes)", 5774 Style); 5775 5776 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 5777 verifyFormat( 5778 "unsigned ContentSize =\n" 5779 " sizeof(int16_t) // DWARF ARange version number\n" 5780 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 5781 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 5782 " + sizeof(int8_t); // Segment Size (in bytes)", 5783 Style); 5784 5785 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 5786 verifyFormat( 5787 "unsigned ContentSize =\n" 5788 " sizeof(int16_t) // DWARF ARange version number\n" 5789 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 5790 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 5791 " + sizeof(int8_t); // Segment Size (in bytes)", 5792 Style); 5793 } 5794 5795 TEST_F(FormatTest, EnforcedOperatorWraps) { 5796 // Here we'd like to wrap after the || operators, but a comment is forcing an 5797 // earlier wrap. 5798 verifyFormat("bool x = aaaaa //\n" 5799 " || bbbbb\n" 5800 " //\n" 5801 " || cccc;"); 5802 } 5803 5804 TEST_F(FormatTest, NoOperandAlignment) { 5805 FormatStyle Style = getLLVMStyle(); 5806 Style.AlignOperands = FormatStyle::OAS_DontAlign; 5807 verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n" 5808 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5809 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5810 Style); 5811 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 5812 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5813 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5814 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5815 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5816 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5817 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5818 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5819 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5820 " > ccccccccccccccccccccccccccccccccccccccccc;", 5821 Style); 5822 5823 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5824 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5825 " + cc;", 5826 Style); 5827 verifyFormat("int a = aa\n" 5828 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5829 " * cccccccccccccccccccccccccccccccccccc;\n", 5830 Style); 5831 5832 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5833 verifyFormat("return (a > b\n" 5834 " // comment1\n" 5835 " // comment2\n" 5836 " || c);", 5837 Style); 5838 } 5839 5840 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { 5841 FormatStyle Style = getLLVMStyle(); 5842 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 5843 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5844 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5845 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 5846 Style); 5847 } 5848 5849 TEST_F(FormatTest, AllowBinPackingInsideArguments) { 5850 FormatStyle Style = getLLVMStyle(); 5851 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 5852 Style.BinPackArguments = false; 5853 Style.ColumnLimit = 40; 5854 verifyFormat("void test() {\n" 5855 " someFunction(\n" 5856 " this + argument + is + quite\n" 5857 " + long + so + it + gets + wrapped\n" 5858 " + but + remains + bin - packed);\n" 5859 "}", 5860 Style); 5861 verifyFormat("void test() {\n" 5862 " someFunction(arg1,\n" 5863 " this + argument + is\n" 5864 " + quite + long + so\n" 5865 " + it + gets + wrapped\n" 5866 " + but + remains + bin\n" 5867 " - packed,\n" 5868 " arg3);\n" 5869 "}", 5870 Style); 5871 verifyFormat("void test() {\n" 5872 " someFunction(\n" 5873 " arg1,\n" 5874 " this + argument + has\n" 5875 " + anotherFunc(nested,\n" 5876 " calls + whose\n" 5877 " + arguments\n" 5878 " + are + also\n" 5879 " + wrapped,\n" 5880 " in + addition)\n" 5881 " + to + being + bin - packed,\n" 5882 " arg3);\n" 5883 "}", 5884 Style); 5885 5886 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 5887 verifyFormat("void test() {\n" 5888 " someFunction(\n" 5889 " arg1,\n" 5890 " this + argument + has +\n" 5891 " anotherFunc(nested,\n" 5892 " calls + whose +\n" 5893 " arguments +\n" 5894 " are + also +\n" 5895 " wrapped,\n" 5896 " in + addition) +\n" 5897 " to + being + bin - packed,\n" 5898 " arg3);\n" 5899 "}", 5900 Style); 5901 } 5902 5903 TEST_F(FormatTest, ConstructorInitializers) { 5904 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 5905 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 5906 getLLVMStyleWithColumns(45)); 5907 verifyFormat("Constructor()\n" 5908 " : Inttializer(FitsOnTheLine) {}", 5909 getLLVMStyleWithColumns(44)); 5910 verifyFormat("Constructor()\n" 5911 " : Inttializer(FitsOnTheLine) {}", 5912 getLLVMStyleWithColumns(43)); 5913 5914 verifyFormat("template <typename T>\n" 5915 "Constructor() : Initializer(FitsOnTheLine) {}", 5916 getLLVMStyleWithColumns(45)); 5917 5918 verifyFormat( 5919 "SomeClass::Constructor()\n" 5920 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 5921 5922 verifyFormat( 5923 "SomeClass::Constructor()\n" 5924 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 5925 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 5926 verifyFormat( 5927 "SomeClass::Constructor()\n" 5928 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5929 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 5930 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5931 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5932 " : aaaaaaaaaa(aaaaaa) {}"); 5933 5934 verifyFormat("Constructor()\n" 5935 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5936 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5937 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5938 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 5939 5940 verifyFormat("Constructor()\n" 5941 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5942 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 5943 5944 verifyFormat("Constructor(int Parameter = 0)\n" 5945 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 5946 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 5947 verifyFormat("Constructor()\n" 5948 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 5949 "}", 5950 getLLVMStyleWithColumns(60)); 5951 verifyFormat("Constructor()\n" 5952 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5953 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 5954 5955 // Here a line could be saved by splitting the second initializer onto two 5956 // lines, but that is not desirable. 5957 verifyFormat("Constructor()\n" 5958 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 5959 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 5960 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 5961 5962 FormatStyle OnePerLine = getLLVMStyle(); 5963 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_Never; 5964 verifyFormat("MyClass::MyClass()\n" 5965 " : a(a),\n" 5966 " b(b),\n" 5967 " c(c) {}", 5968 OnePerLine); 5969 verifyFormat("MyClass::MyClass()\n" 5970 " : a(a), // comment\n" 5971 " b(b),\n" 5972 " c(c) {}", 5973 OnePerLine); 5974 verifyFormat("MyClass::MyClass(int a)\n" 5975 " : b(a), // comment\n" 5976 " c(a + 1) { // lined up\n" 5977 "}", 5978 OnePerLine); 5979 verifyFormat("Constructor()\n" 5980 " : a(b, b, b) {}", 5981 OnePerLine); 5982 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 5983 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 5984 verifyFormat("SomeClass::Constructor()\n" 5985 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 5986 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 5987 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 5988 OnePerLine); 5989 verifyFormat("SomeClass::Constructor()\n" 5990 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 5991 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 5992 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 5993 OnePerLine); 5994 verifyFormat("MyClass::MyClass(int var)\n" 5995 " : some_var_(var), // 4 space indent\n" 5996 " some_other_var_(var + 1) { // lined up\n" 5997 "}", 5998 OnePerLine); 5999 verifyFormat("Constructor()\n" 6000 " : aaaaa(aaaaaa),\n" 6001 " aaaaa(aaaaaa),\n" 6002 " aaaaa(aaaaaa),\n" 6003 " aaaaa(aaaaaa),\n" 6004 " aaaaa(aaaaaa) {}", 6005 OnePerLine); 6006 verifyFormat("Constructor()\n" 6007 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 6008 " aaaaaaaaaaaaaaaaaaaaaa) {}", 6009 OnePerLine); 6010 OnePerLine.BinPackParameters = false; 6011 verifyFormat( 6012 "Constructor()\n" 6013 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n" 6014 " aaaaaaaaaaa().aaa(),\n" 6015 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6016 OnePerLine); 6017 OnePerLine.ColumnLimit = 60; 6018 verifyFormat("Constructor()\n" 6019 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6020 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 6021 OnePerLine); 6022 6023 EXPECT_EQ("Constructor()\n" 6024 " : // Comment forcing unwanted break.\n" 6025 " aaaa(aaaa) {}", 6026 format("Constructor() :\n" 6027 " // Comment forcing unwanted break.\n" 6028 " aaaa(aaaa) {}")); 6029 } 6030 6031 TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) { 6032 FormatStyle Style = getLLVMStyle(); 6033 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 6034 Style.ColumnLimit = 60; 6035 Style.BinPackParameters = false; 6036 6037 for (int i = 0; i < 4; ++i) { 6038 // Test all combinations of parameters that should not have an effect. 6039 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1; 6040 Style.AllowAllArgumentsOnNextLine = i & 2; 6041 6042 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6043 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 6044 verifyFormat("Constructor()\n" 6045 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6046 Style); 6047 verifyFormat("Constructor() : a(a), b(b) {}", Style); 6048 6049 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6050 verifyFormat("Constructor()\n" 6051 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 6052 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 6053 Style); 6054 verifyFormat("Constructor() : a(a), b(b) {}", Style); 6055 6056 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 6057 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6058 verifyFormat("Constructor()\n" 6059 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6060 Style); 6061 6062 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6063 verifyFormat("Constructor()\n" 6064 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6065 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6066 Style); 6067 6068 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 6069 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6070 verifyFormat("Constructor() :\n" 6071 " aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6072 Style); 6073 6074 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6075 verifyFormat("Constructor() :\n" 6076 " aaaaaaaaaaaaaaaaaa(a),\n" 6077 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6078 Style); 6079 } 6080 6081 // Test interactions between AllowAllParametersOfDeclarationOnNextLine and 6082 // AllowAllConstructorInitializersOnNextLine in all 6083 // BreakConstructorInitializers modes 6084 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 6085 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6086 verifyFormat("SomeClassWithALongName::Constructor(\n" 6087 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n" 6088 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 6089 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 6090 Style); 6091 6092 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6093 verifyFormat("SomeClassWithALongName::Constructor(\n" 6094 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6095 " int bbbbbbbbbbbbb,\n" 6096 " int cccccccccccccccc)\n" 6097 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6098 Style); 6099 6100 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6101 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6102 verifyFormat("SomeClassWithALongName::Constructor(\n" 6103 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6104 " int bbbbbbbbbbbbb)\n" 6105 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 6106 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 6107 Style); 6108 6109 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 6110 6111 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6112 verifyFormat("SomeClassWithALongName::Constructor(\n" 6113 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n" 6114 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6115 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6116 Style); 6117 6118 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6119 verifyFormat("SomeClassWithALongName::Constructor(\n" 6120 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6121 " int bbbbbbbbbbbbb,\n" 6122 " int cccccccccccccccc)\n" 6123 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6124 Style); 6125 6126 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6127 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6128 verifyFormat("SomeClassWithALongName::Constructor(\n" 6129 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6130 " int bbbbbbbbbbbbb)\n" 6131 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6132 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6133 Style); 6134 6135 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 6136 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6137 verifyFormat("SomeClassWithALongName::Constructor(\n" 6138 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n" 6139 " aaaaaaaaaaaaaaaaaaaa(a),\n" 6140 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6141 Style); 6142 6143 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6144 verifyFormat("SomeClassWithALongName::Constructor(\n" 6145 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6146 " int bbbbbbbbbbbbb,\n" 6147 " int cccccccccccccccc) :\n" 6148 " aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6149 Style); 6150 6151 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6152 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6153 verifyFormat("SomeClassWithALongName::Constructor(\n" 6154 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6155 " int bbbbbbbbbbbbb) :\n" 6156 " aaaaaaaaaaaaaaaaaaaa(a),\n" 6157 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6158 Style); 6159 } 6160 6161 TEST_F(FormatTest, AllowAllArgumentsOnNextLine) { 6162 FormatStyle Style = getLLVMStyle(); 6163 Style.ColumnLimit = 60; 6164 Style.BinPackArguments = false; 6165 for (int i = 0; i < 4; ++i) { 6166 // Test all combinations of parameters that should not have an effect. 6167 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1; 6168 Style.PackConstructorInitializers = 6169 i & 2 ? FormatStyle::PCIS_BinPack : FormatStyle::PCIS_Never; 6170 6171 Style.AllowAllArgumentsOnNextLine = true; 6172 verifyFormat("void foo() {\n" 6173 " FunctionCallWithReallyLongName(\n" 6174 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n" 6175 "}", 6176 Style); 6177 Style.AllowAllArgumentsOnNextLine = false; 6178 verifyFormat("void foo() {\n" 6179 " FunctionCallWithReallyLongName(\n" 6180 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6181 " bbbbbbbbbbbb);\n" 6182 "}", 6183 Style); 6184 6185 Style.AllowAllArgumentsOnNextLine = true; 6186 verifyFormat("void foo() {\n" 6187 " auto VariableWithReallyLongName = {\n" 6188 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n" 6189 "}", 6190 Style); 6191 Style.AllowAllArgumentsOnNextLine = false; 6192 verifyFormat("void foo() {\n" 6193 " auto VariableWithReallyLongName = {\n" 6194 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6195 " bbbbbbbbbbbb};\n" 6196 "}", 6197 Style); 6198 } 6199 6200 // This parameter should not affect declarations. 6201 Style.BinPackParameters = false; 6202 Style.AllowAllArgumentsOnNextLine = false; 6203 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6204 verifyFormat("void FunctionCallWithReallyLongName(\n" 6205 " int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);", 6206 Style); 6207 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6208 verifyFormat("void FunctionCallWithReallyLongName(\n" 6209 " int aaaaaaaaaaaaaaaaaaaaaaa,\n" 6210 " int bbbbbbbbbbbb);", 6211 Style); 6212 } 6213 6214 TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) { 6215 // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign 6216 // and BAS_Align. 6217 auto Style = getLLVMStyle(); 6218 Style.ColumnLimit = 35; 6219 StringRef Input = "functionCall(paramA, paramB, paramC);\n" 6220 "void functionDecl(int A, int B, int C);"; 6221 Style.AllowAllArgumentsOnNextLine = false; 6222 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6223 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 6224 " paramC);\n" 6225 "void functionDecl(int A, int B,\n" 6226 " int C);"), 6227 format(Input, Style)); 6228 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 6229 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 6230 " paramC);\n" 6231 "void functionDecl(int A, int B,\n" 6232 " int C);"), 6233 format(Input, Style)); 6234 // However, BAS_AlwaysBreak should take precedence over 6235 // AllowAllArgumentsOnNextLine. 6236 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 6237 EXPECT_EQ(StringRef("functionCall(\n" 6238 " paramA, paramB, paramC);\n" 6239 "void functionDecl(\n" 6240 " int A, int B, int C);"), 6241 format(Input, Style)); 6242 6243 // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the 6244 // first argument. 6245 Style.AllowAllArgumentsOnNextLine = true; 6246 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 6247 EXPECT_EQ(StringRef("functionCall(\n" 6248 " paramA, paramB, paramC);\n" 6249 "void functionDecl(\n" 6250 " int A, int B, int C);"), 6251 format(Input, Style)); 6252 // It wouldn't fit on one line with aligned parameters so this setting 6253 // doesn't change anything for BAS_Align. 6254 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 6255 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 6256 " paramC);\n" 6257 "void functionDecl(int A, int B,\n" 6258 " int C);"), 6259 format(Input, Style)); 6260 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6261 EXPECT_EQ(StringRef("functionCall(\n" 6262 " paramA, paramB, paramC);\n" 6263 "void functionDecl(\n" 6264 " int A, int B, int C);"), 6265 format(Input, Style)); 6266 } 6267 6268 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) { 6269 FormatStyle Style = getLLVMStyle(); 6270 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 6271 6272 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 6273 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}", 6274 getStyleWithColumns(Style, 45)); 6275 verifyFormat("Constructor() :\n" 6276 " Initializer(FitsOnTheLine) {}", 6277 getStyleWithColumns(Style, 44)); 6278 verifyFormat("Constructor() :\n" 6279 " Initializer(FitsOnTheLine) {}", 6280 getStyleWithColumns(Style, 43)); 6281 6282 verifyFormat("template <typename T>\n" 6283 "Constructor() : Initializer(FitsOnTheLine) {}", 6284 getStyleWithColumns(Style, 50)); 6285 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6286 verifyFormat( 6287 "SomeClass::Constructor() :\n" 6288 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 6289 Style); 6290 6291 Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack; 6292 verifyFormat( 6293 "SomeClass::Constructor() :\n" 6294 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 6295 Style); 6296 6297 verifyFormat( 6298 "SomeClass::Constructor() :\n" 6299 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6300 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6301 Style); 6302 verifyFormat( 6303 "SomeClass::Constructor() :\n" 6304 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6305 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 6306 Style); 6307 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6308 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 6309 " aaaaaaaaaa(aaaaaa) {}", 6310 Style); 6311 6312 verifyFormat("Constructor() :\n" 6313 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6314 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6315 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6316 " aaaaaaaaaaaaaaaaaaaaaaa() {}", 6317 Style); 6318 6319 verifyFormat("Constructor() :\n" 6320 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6321 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6322 Style); 6323 6324 verifyFormat("Constructor(int Parameter = 0) :\n" 6325 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 6326 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}", 6327 Style); 6328 verifyFormat("Constructor() :\n" 6329 " aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 6330 "}", 6331 getStyleWithColumns(Style, 60)); 6332 verifyFormat("Constructor() :\n" 6333 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6334 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}", 6335 Style); 6336 6337 // Here a line could be saved by splitting the second initializer onto two 6338 // lines, but that is not desirable. 6339 verifyFormat("Constructor() :\n" 6340 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 6341 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 6342 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6343 Style); 6344 6345 FormatStyle OnePerLine = Style; 6346 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6347 verifyFormat("SomeClass::Constructor() :\n" 6348 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6349 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6350 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6351 OnePerLine); 6352 verifyFormat("SomeClass::Constructor() :\n" 6353 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 6354 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6355 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6356 OnePerLine); 6357 verifyFormat("MyClass::MyClass(int var) :\n" 6358 " some_var_(var), // 4 space indent\n" 6359 " some_other_var_(var + 1) { // lined up\n" 6360 "}", 6361 OnePerLine); 6362 verifyFormat("Constructor() :\n" 6363 " aaaaa(aaaaaa),\n" 6364 " aaaaa(aaaaaa),\n" 6365 " aaaaa(aaaaaa),\n" 6366 " aaaaa(aaaaaa),\n" 6367 " aaaaa(aaaaaa) {}", 6368 OnePerLine); 6369 verifyFormat("Constructor() :\n" 6370 " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 6371 " aaaaaaaaaaaaaaaaaaaaaa) {}", 6372 OnePerLine); 6373 OnePerLine.BinPackParameters = false; 6374 verifyFormat("Constructor() :\n" 6375 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 6376 " aaaaaaaaaaa().aaa(),\n" 6377 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6378 OnePerLine); 6379 OnePerLine.ColumnLimit = 60; 6380 verifyFormat("Constructor() :\n" 6381 " aaaaaaaaaaaaaaaaaaaa(a),\n" 6382 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 6383 OnePerLine); 6384 6385 EXPECT_EQ("Constructor() :\n" 6386 " // Comment forcing unwanted break.\n" 6387 " aaaa(aaaa) {}", 6388 format("Constructor() :\n" 6389 " // Comment forcing unwanted break.\n" 6390 " aaaa(aaaa) {}", 6391 Style)); 6392 6393 Style.ColumnLimit = 0; 6394 verifyFormat("SomeClass::Constructor() :\n" 6395 " a(a) {}", 6396 Style); 6397 verifyFormat("SomeClass::Constructor() noexcept :\n" 6398 " a(a) {}", 6399 Style); 6400 verifyFormat("SomeClass::Constructor() :\n" 6401 " a(a), b(b), c(c) {}", 6402 Style); 6403 verifyFormat("SomeClass::Constructor() :\n" 6404 " a(a) {\n" 6405 " foo();\n" 6406 " bar();\n" 6407 "}", 6408 Style); 6409 6410 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 6411 verifyFormat("SomeClass::Constructor() :\n" 6412 " a(a), b(b), c(c) {\n" 6413 "}", 6414 Style); 6415 verifyFormat("SomeClass::Constructor() :\n" 6416 " a(a) {\n" 6417 "}", 6418 Style); 6419 6420 Style.ColumnLimit = 80; 6421 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 6422 Style.ConstructorInitializerIndentWidth = 2; 6423 verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style); 6424 verifyFormat("SomeClass::Constructor() :\n" 6425 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6426 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}", 6427 Style); 6428 6429 // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as 6430 // well 6431 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 6432 verifyFormat( 6433 "class SomeClass\n" 6434 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6435 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 6436 Style); 6437 Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 6438 verifyFormat( 6439 "class SomeClass\n" 6440 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6441 " , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 6442 Style); 6443 Style.BreakInheritanceList = FormatStyle::BILS_AfterColon; 6444 verifyFormat( 6445 "class SomeClass :\n" 6446 " public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6447 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 6448 Style); 6449 Style.BreakInheritanceList = FormatStyle::BILS_AfterComma; 6450 verifyFormat( 6451 "class SomeClass\n" 6452 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6453 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 6454 Style); 6455 } 6456 6457 #ifndef EXPENSIVE_CHECKS 6458 // Expensive checks enables libstdc++ checking which includes validating the 6459 // state of ranges used in std::priority_queue - this blows out the 6460 // runtime/scalability of the function and makes this test unacceptably slow. 6461 TEST_F(FormatTest, MemoizationTests) { 6462 // This breaks if the memoization lookup does not take \c Indent and 6463 // \c LastSpace into account. 6464 verifyFormat( 6465 "extern CFRunLoopTimerRef\n" 6466 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 6467 " CFTimeInterval interval, CFOptionFlags flags,\n" 6468 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 6469 " CFRunLoopTimerContext *context) {}"); 6470 6471 // Deep nesting somewhat works around our memoization. 6472 verifyFormat( 6473 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 6474 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 6475 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 6476 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 6477 " aaaaa())))))))))))))))))))))))))))))))))))))));", 6478 getLLVMStyleWithColumns(65)); 6479 verifyFormat( 6480 "aaaaa(\n" 6481 " aaaaa,\n" 6482 " aaaaa(\n" 6483 " aaaaa,\n" 6484 " aaaaa(\n" 6485 " aaaaa,\n" 6486 " aaaaa(\n" 6487 " aaaaa,\n" 6488 " aaaaa(\n" 6489 " aaaaa,\n" 6490 " aaaaa(\n" 6491 " aaaaa,\n" 6492 " aaaaa(\n" 6493 " aaaaa,\n" 6494 " aaaaa(\n" 6495 " aaaaa,\n" 6496 " aaaaa(\n" 6497 " aaaaa,\n" 6498 " aaaaa(\n" 6499 " aaaaa,\n" 6500 " aaaaa(\n" 6501 " aaaaa,\n" 6502 " aaaaa(\n" 6503 " aaaaa,\n" 6504 " aaaaa))))))))))));", 6505 getLLVMStyleWithColumns(65)); 6506 verifyFormat( 6507 "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" 6508 " a),\n" 6509 " a),\n" 6510 " a),\n" 6511 " a),\n" 6512 " a),\n" 6513 " a),\n" 6514 " a),\n" 6515 " a),\n" 6516 " a),\n" 6517 " a),\n" 6518 " a),\n" 6519 " a),\n" 6520 " a),\n" 6521 " a),\n" 6522 " a),\n" 6523 " a),\n" 6524 " a)", 6525 getLLVMStyleWithColumns(65)); 6526 6527 // This test takes VERY long when memoization is broken. 6528 FormatStyle OnePerLine = getLLVMStyle(); 6529 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6530 OnePerLine.BinPackParameters = false; 6531 std::string input = "Constructor()\n" 6532 " : aaaa(a,\n"; 6533 for (unsigned i = 0, e = 80; i != e; ++i) { 6534 input += " a,\n"; 6535 } 6536 input += " a) {}"; 6537 verifyFormat(input, OnePerLine); 6538 } 6539 #endif 6540 6541 TEST_F(FormatTest, BreaksAsHighAsPossible) { 6542 verifyFormat( 6543 "void f() {\n" 6544 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 6545 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 6546 " f();\n" 6547 "}"); 6548 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 6549 " Intervals[i - 1].getRange().getLast()) {\n}"); 6550 } 6551 6552 TEST_F(FormatTest, BreaksFunctionDeclarations) { 6553 // Principially, we break function declarations in a certain order: 6554 // 1) break amongst arguments. 6555 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 6556 " Cccccccccccccc cccccccccccccc);"); 6557 verifyFormat("template <class TemplateIt>\n" 6558 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 6559 " TemplateIt *stop) {}"); 6560 6561 // 2) break after return type. 6562 verifyFormat( 6563 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6564 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", 6565 getGoogleStyle()); 6566 6567 // 3) break after (. 6568 verifyFormat( 6569 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 6570 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", 6571 getGoogleStyle()); 6572 6573 // 4) break before after nested name specifiers. 6574 verifyFormat( 6575 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6576 "SomeClasssssssssssssssssssssssssssssssssssssss::\n" 6577 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", 6578 getGoogleStyle()); 6579 6580 // However, there are exceptions, if a sufficient amount of lines can be 6581 // saved. 6582 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 6583 // more adjusting. 6584 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 6585 " Cccccccccccccc cccccccccc,\n" 6586 " Cccccccccccccc cccccccccc,\n" 6587 " Cccccccccccccc cccccccccc,\n" 6588 " Cccccccccccccc cccccccccc);"); 6589 verifyFormat( 6590 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6591 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6592 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6593 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", 6594 getGoogleStyle()); 6595 verifyFormat( 6596 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 6597 " Cccccccccccccc cccccccccc,\n" 6598 " Cccccccccccccc cccccccccc,\n" 6599 " Cccccccccccccc cccccccccc,\n" 6600 " Cccccccccccccc cccccccccc,\n" 6601 " Cccccccccccccc cccccccccc,\n" 6602 " Cccccccccccccc cccccccccc);"); 6603 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 6604 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6605 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6606 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6607 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 6608 6609 // Break after multi-line parameters. 6610 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6611 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6612 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6613 " bbbb bbbb);"); 6614 verifyFormat("void SomeLoooooooooooongFunction(\n" 6615 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6616 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6617 " int bbbbbbbbbbbbb);"); 6618 6619 // Treat overloaded operators like other functions. 6620 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 6621 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 6622 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 6623 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 6624 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 6625 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 6626 verifyGoogleFormat( 6627 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 6628 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 6629 verifyGoogleFormat( 6630 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 6631 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 6632 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6633 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 6634 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 6635 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 6636 verifyGoogleFormat( 6637 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" 6638 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6639 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); 6640 verifyGoogleFormat("template <typename T>\n" 6641 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6642 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n" 6643 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);"); 6644 6645 FormatStyle Style = getLLVMStyle(); 6646 Style.PointerAlignment = FormatStyle::PAS_Left; 6647 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6648 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", 6649 Style); 6650 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 6651 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6652 Style); 6653 } 6654 6655 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) { 6656 // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516: 6657 // Prefer keeping `::` followed by `operator` together. 6658 EXPECT_EQ("const aaaa::bbbbbbb &\n" 6659 "ccccccccc::operator++() {\n" 6660 " stuff();\n" 6661 "}", 6662 format("const aaaa::bbbbbbb\n" 6663 "&ccccccccc::operator++() { stuff(); }", 6664 getLLVMStyleWithColumns(40))); 6665 } 6666 6667 TEST_F(FormatTest, TrailingReturnType) { 6668 verifyFormat("auto foo() -> int;\n"); 6669 // correct trailing return type spacing 6670 verifyFormat("auto operator->() -> int;\n"); 6671 verifyFormat("auto operator++(int) -> int;\n"); 6672 6673 verifyFormat("struct S {\n" 6674 " auto bar() const -> int;\n" 6675 "};"); 6676 verifyFormat("template <size_t Order, typename T>\n" 6677 "auto load_img(const std::string &filename)\n" 6678 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 6679 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 6680 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 6681 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 6682 verifyFormat("template <typename T>\n" 6683 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" 6684 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); 6685 6686 // Not trailing return types. 6687 verifyFormat("void f() { auto a = b->c(); }"); 6688 } 6689 6690 TEST_F(FormatTest, DeductionGuides) { 6691 verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;"); 6692 verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;"); 6693 verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;"); 6694 verifyFormat( 6695 "template <class... T>\n" 6696 "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;"); 6697 verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;"); 6698 verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;"); 6699 verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;"); 6700 verifyFormat("template <class T> A() -> A<(3 < 2)>;"); 6701 verifyFormat("template <class T> A() -> A<((3) < (2))>;"); 6702 verifyFormat("template <class T> x() -> x<1>;"); 6703 verifyFormat("template <class T> explicit x(T &) -> x<1>;"); 6704 6705 // Ensure not deduction guides. 6706 verifyFormat("c()->f<int>();"); 6707 verifyFormat("x()->foo<1>;"); 6708 verifyFormat("x = p->foo<3>();"); 6709 verifyFormat("x()->x<1>();"); 6710 verifyFormat("x()->x<1>;"); 6711 } 6712 6713 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 6714 // Avoid breaking before trailing 'const' or other trailing annotations, if 6715 // they are not function-like. 6716 FormatStyle Style = getGoogleStyle(); 6717 Style.ColumnLimit = 47; 6718 verifyFormat("void someLongFunction(\n" 6719 " int someLoooooooooooooongParameter) const {\n}", 6720 getLLVMStyleWithColumns(47)); 6721 verifyFormat("LoooooongReturnType\n" 6722 "someLoooooooongFunction() const {}", 6723 getLLVMStyleWithColumns(47)); 6724 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 6725 " const {}", 6726 Style); 6727 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 6728 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 6729 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 6730 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 6731 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 6732 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 6733 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 6734 " aaaaaaaaaaa aaaaa) const override;"); 6735 verifyGoogleFormat( 6736 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 6737 " const override;"); 6738 6739 // Even if the first parameter has to be wrapped. 6740 verifyFormat("void someLongFunction(\n" 6741 " int someLongParameter) const {}", 6742 getLLVMStyleWithColumns(46)); 6743 verifyFormat("void someLongFunction(\n" 6744 " int someLongParameter) const {}", 6745 Style); 6746 verifyFormat("void someLongFunction(\n" 6747 " int someLongParameter) override {}", 6748 Style); 6749 verifyFormat("void someLongFunction(\n" 6750 " int someLongParameter) OVERRIDE {}", 6751 Style); 6752 verifyFormat("void someLongFunction(\n" 6753 " int someLongParameter) final {}", 6754 Style); 6755 verifyFormat("void someLongFunction(\n" 6756 " int someLongParameter) FINAL {}", 6757 Style); 6758 verifyFormat("void someLongFunction(\n" 6759 " int parameter) const override {}", 6760 Style); 6761 6762 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 6763 verifyFormat("void someLongFunction(\n" 6764 " int someLongParameter) const\n" 6765 "{\n" 6766 "}", 6767 Style); 6768 6769 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 6770 verifyFormat("void someLongFunction(\n" 6771 " int someLongParameter) const\n" 6772 " {\n" 6773 " }", 6774 Style); 6775 6776 // Unless these are unknown annotations. 6777 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 6778 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6779 " LONG_AND_UGLY_ANNOTATION;"); 6780 6781 // Breaking before function-like trailing annotations is fine to keep them 6782 // close to their arguments. 6783 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6784 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 6785 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 6786 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 6787 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 6788 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 6789 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 6790 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 6791 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 6792 6793 verifyFormat( 6794 "void aaaaaaaaaaaaaaaaaa()\n" 6795 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 6796 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 6797 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6798 " __attribute__((unused));"); 6799 verifyGoogleFormat( 6800 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6801 " GUARDED_BY(aaaaaaaaaaaa);"); 6802 verifyGoogleFormat( 6803 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6804 " GUARDED_BY(aaaaaaaaaaaa);"); 6805 verifyGoogleFormat( 6806 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 6807 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6808 verifyGoogleFormat( 6809 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 6810 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 6811 } 6812 6813 TEST_F(FormatTest, FunctionAnnotations) { 6814 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 6815 "int OldFunction(const string ¶meter) {}"); 6816 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 6817 "string OldFunction(const string ¶meter) {}"); 6818 verifyFormat("template <typename T>\n" 6819 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 6820 "string OldFunction(const string ¶meter) {}"); 6821 6822 // Not function annotations. 6823 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6824 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 6825 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" 6826 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); 6827 verifyFormat("MACRO(abc).function() // wrap\n" 6828 " << abc;"); 6829 verifyFormat("MACRO(abc)->function() // wrap\n" 6830 " << abc;"); 6831 verifyFormat("MACRO(abc)::function() // wrap\n" 6832 " << abc;"); 6833 } 6834 6835 TEST_F(FormatTest, BreaksDesireably) { 6836 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 6837 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 6838 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 6839 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6840 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 6841 "}"); 6842 6843 verifyFormat( 6844 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6845 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 6846 6847 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6848 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6849 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 6850 6851 verifyFormat( 6852 "aaaaaaaa(aaaaaaaaaaaaa,\n" 6853 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6854 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 6855 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6856 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 6857 6858 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 6859 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6860 6861 verifyFormat( 6862 "void f() {\n" 6863 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 6864 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 6865 "}"); 6866 verifyFormat( 6867 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6868 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 6869 verifyFormat( 6870 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6871 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 6872 verifyFormat( 6873 "aaaaaa(aaa,\n" 6874 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6875 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6876 " aaaa);"); 6877 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6878 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6879 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6880 6881 // Indent consistently independent of call expression and unary operator. 6882 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 6883 " dddddddddddddddddddddddddddddd));"); 6884 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 6885 " dddddddddddddddddddddddddddddd));"); 6886 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 6887 " dddddddddddddddddddddddddddddd));"); 6888 6889 // This test case breaks on an incorrect memoization, i.e. an optimization not 6890 // taking into account the StopAt value. 6891 verifyFormat( 6892 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 6893 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 6894 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 6895 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6896 6897 verifyFormat("{\n {\n {\n" 6898 " Annotation.SpaceRequiredBefore =\n" 6899 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 6900 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 6901 " }\n }\n}"); 6902 6903 // Break on an outer level if there was a break on an inner level. 6904 EXPECT_EQ("f(g(h(a, // comment\n" 6905 " b, c),\n" 6906 " d, e),\n" 6907 " x, y);", 6908 format("f(g(h(a, // comment\n" 6909 " b, c), d, e), x, y);")); 6910 6911 // Prefer breaking similar line breaks. 6912 verifyFormat( 6913 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 6914 " NSTrackingMouseEnteredAndExited |\n" 6915 " NSTrackingActiveAlways;"); 6916 } 6917 6918 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 6919 FormatStyle NoBinPacking = getGoogleStyle(); 6920 NoBinPacking.BinPackParameters = false; 6921 NoBinPacking.BinPackArguments = true; 6922 verifyFormat("void f() {\n" 6923 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 6924 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 6925 "}", 6926 NoBinPacking); 6927 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 6928 " int aaaaaaaaaaaaaaaaaaaa,\n" 6929 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6930 NoBinPacking); 6931 6932 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 6933 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6934 " vector<int> bbbbbbbbbbbbbbb);", 6935 NoBinPacking); 6936 // FIXME: This behavior difference is probably not wanted. However, currently 6937 // we cannot distinguish BreakBeforeParameter being set because of the wrapped 6938 // template arguments from BreakBeforeParameter being set because of the 6939 // one-per-line formatting. 6940 verifyFormat( 6941 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n" 6942 " aaaaaaaaaa> aaaaaaaaaa);", 6943 NoBinPacking); 6944 verifyFormat( 6945 "void fffffffffff(\n" 6946 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n" 6947 " aaaaaaaaaa);"); 6948 } 6949 6950 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 6951 FormatStyle NoBinPacking = getGoogleStyle(); 6952 NoBinPacking.BinPackParameters = false; 6953 NoBinPacking.BinPackArguments = false; 6954 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 6955 " aaaaaaaaaaaaaaaaaaaa,\n" 6956 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 6957 NoBinPacking); 6958 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 6959 " aaaaaaaaaaaaa,\n" 6960 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 6961 NoBinPacking); 6962 verifyFormat( 6963 "aaaaaaaa(aaaaaaaaaaaaa,\n" 6964 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6965 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 6966 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6967 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 6968 NoBinPacking); 6969 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 6970 " .aaaaaaaaaaaaaaaaaa();", 6971 NoBinPacking); 6972 verifyFormat("void f() {\n" 6973 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6974 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 6975 "}", 6976 NoBinPacking); 6977 6978 verifyFormat( 6979 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6980 " aaaaaaaaaaaa,\n" 6981 " aaaaaaaaaaaa);", 6982 NoBinPacking); 6983 verifyFormat( 6984 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 6985 " ddddddddddddddddddddddddddddd),\n" 6986 " test);", 6987 NoBinPacking); 6988 6989 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 6990 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 6991 " aaaaaaaaaaaaaaaaaaaaaaa>\n" 6992 " aaaaaaaaaaaaaaaaaa;", 6993 NoBinPacking); 6994 verifyFormat("a(\"a\"\n" 6995 " \"a\",\n" 6996 " a);"); 6997 6998 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 6999 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 7000 " aaaaaaaaa,\n" 7001 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7002 NoBinPacking); 7003 verifyFormat( 7004 "void f() {\n" 7005 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 7006 " .aaaaaaa();\n" 7007 "}", 7008 NoBinPacking); 7009 verifyFormat( 7010 "template <class SomeType, class SomeOtherType>\n" 7011 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 7012 NoBinPacking); 7013 } 7014 7015 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 7016 FormatStyle Style = getLLVMStyleWithColumns(15); 7017 Style.ExperimentalAutoDetectBinPacking = true; 7018 EXPECT_EQ("aaa(aaaa,\n" 7019 " aaaa,\n" 7020 " aaaa);\n" 7021 "aaa(aaaa,\n" 7022 " aaaa,\n" 7023 " aaaa);", 7024 format("aaa(aaaa,\n" // one-per-line 7025 " aaaa,\n" 7026 " aaaa );\n" 7027 "aaa(aaaa, aaaa, aaaa);", // inconclusive 7028 Style)); 7029 EXPECT_EQ("aaa(aaaa, aaaa,\n" 7030 " aaaa);\n" 7031 "aaa(aaaa, aaaa,\n" 7032 " aaaa);", 7033 format("aaa(aaaa, aaaa,\n" // bin-packed 7034 " aaaa );\n" 7035 "aaa(aaaa, aaaa, aaaa);", // inconclusive 7036 Style)); 7037 } 7038 7039 TEST_F(FormatTest, FormatsBuilderPattern) { 7040 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 7041 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 7042 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 7043 " .StartsWith(\".init\", ORDER_INIT)\n" 7044 " .StartsWith(\".fini\", ORDER_FINI)\n" 7045 " .StartsWith(\".hash\", ORDER_HASH)\n" 7046 " .Default(ORDER_TEXT);\n"); 7047 7048 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 7049 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 7050 verifyFormat("aaaaaaa->aaaaaaa\n" 7051 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7052 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7053 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 7054 verifyFormat( 7055 "aaaaaaa->aaaaaaa\n" 7056 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7057 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 7058 verifyFormat( 7059 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 7060 " aaaaaaaaaaaaaa);"); 7061 verifyFormat( 7062 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 7063 " aaaaaa->aaaaaaaaaaaa()\n" 7064 " ->aaaaaaaaaaaaaaaa(\n" 7065 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7066 " ->aaaaaaaaaaaaaaaaa();"); 7067 verifyGoogleFormat( 7068 "void f() {\n" 7069 " someo->Add((new util::filetools::Handler(dir))\n" 7070 " ->OnEvent1(NewPermanentCallback(\n" 7071 " this, &HandlerHolderClass::EventHandlerCBA))\n" 7072 " ->OnEvent2(NewPermanentCallback(\n" 7073 " this, &HandlerHolderClass::EventHandlerCBB))\n" 7074 " ->OnEvent3(NewPermanentCallback(\n" 7075 " this, &HandlerHolderClass::EventHandlerCBC))\n" 7076 " ->OnEvent5(NewPermanentCallback(\n" 7077 " this, &HandlerHolderClass::EventHandlerCBD))\n" 7078 " ->OnEvent6(NewPermanentCallback(\n" 7079 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 7080 "}"); 7081 7082 verifyFormat( 7083 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 7084 verifyFormat("aaaaaaaaaaaaaaa()\n" 7085 " .aaaaaaaaaaaaaaa()\n" 7086 " .aaaaaaaaaaaaaaa()\n" 7087 " .aaaaaaaaaaaaaaa()\n" 7088 " .aaaaaaaaaaaaaaa();"); 7089 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 7090 " .aaaaaaaaaaaaaaa()\n" 7091 " .aaaaaaaaaaaaaaa()\n" 7092 " .aaaaaaaaaaaaaaa();"); 7093 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 7094 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 7095 " .aaaaaaaaaaaaaaa();"); 7096 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 7097 " ->aaaaaaaaaaaaaae(0)\n" 7098 " ->aaaaaaaaaaaaaaa();"); 7099 7100 // Don't linewrap after very short segments. 7101 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7102 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7103 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7104 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7105 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7106 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7107 verifyFormat("aaa()\n" 7108 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7109 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7110 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7111 7112 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 7113 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7114 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 7115 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 7116 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 7117 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 7118 7119 // Prefer not to break after empty parentheses. 7120 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 7121 " First->LastNewlineOffset);"); 7122 7123 // Prefer not to create "hanging" indents. 7124 verifyFormat( 7125 "return !soooooooooooooome_map\n" 7126 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7127 " .second;"); 7128 verifyFormat( 7129 "return aaaaaaaaaaaaaaaa\n" 7130 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n" 7131 " .aaaa(aaaaaaaaaaaaaa);"); 7132 // No hanging indent here. 7133 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n" 7134 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7135 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n" 7136 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7137 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 7138 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7139 getLLVMStyleWithColumns(60)); 7140 verifyFormat("aaaaaaaaaaaaaaaaaa\n" 7141 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 7142 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7143 getLLVMStyleWithColumns(59)); 7144 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7145 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7146 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7147 7148 // Dont break if only closing statements before member call 7149 verifyFormat("test() {\n" 7150 " ([]() -> {\n" 7151 " int b = 32;\n" 7152 " return 3;\n" 7153 " }).foo();\n" 7154 "}"); 7155 verifyFormat("test() {\n" 7156 " (\n" 7157 " []() -> {\n" 7158 " int b = 32;\n" 7159 " return 3;\n" 7160 " },\n" 7161 " foo, bar)\n" 7162 " .foo();\n" 7163 "}"); 7164 verifyFormat("test() {\n" 7165 " ([]() -> {\n" 7166 " int b = 32;\n" 7167 " return 3;\n" 7168 " })\n" 7169 " .foo()\n" 7170 " .bar();\n" 7171 "}"); 7172 verifyFormat("test() {\n" 7173 " ([]() -> {\n" 7174 " int b = 32;\n" 7175 " return 3;\n" 7176 " })\n" 7177 " .foo(\"aaaaaaaaaaaaaaaaa\"\n" 7178 " \"bbbb\");\n" 7179 "}", 7180 getLLVMStyleWithColumns(30)); 7181 } 7182 7183 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 7184 verifyFormat( 7185 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7186 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 7187 verifyFormat( 7188 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 7189 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 7190 7191 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 7192 " ccccccccccccccccccccccccc) {\n}"); 7193 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 7194 " ccccccccccccccccccccccccc) {\n}"); 7195 7196 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 7197 " ccccccccccccccccccccccccc) {\n}"); 7198 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 7199 " ccccccccccccccccccccccccc) {\n}"); 7200 7201 verifyFormat( 7202 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 7203 " ccccccccccccccccccccccccc) {\n}"); 7204 verifyFormat( 7205 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 7206 " ccccccccccccccccccccccccc) {\n}"); 7207 7208 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 7209 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 7210 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 7211 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 7212 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 7213 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 7214 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 7215 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 7216 7217 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 7218 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 7219 " aaaaaaaaaaaaaaa != aa) {\n}"); 7220 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 7221 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 7222 " aaaaaaaaaaaaaaa != aa) {\n}"); 7223 } 7224 7225 TEST_F(FormatTest, BreaksAfterAssignments) { 7226 verifyFormat( 7227 "unsigned Cost =\n" 7228 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 7229 " SI->getPointerAddressSpaceee());\n"); 7230 verifyFormat( 7231 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 7232 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 7233 7234 verifyFormat( 7235 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 7236 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 7237 verifyFormat("unsigned OriginalStartColumn =\n" 7238 " SourceMgr.getSpellingColumnNumber(\n" 7239 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 7240 " 1;"); 7241 } 7242 7243 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) { 7244 FormatStyle Style = getLLVMStyle(); 7245 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 7246 " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;", 7247 Style); 7248 7249 Style.PenaltyBreakAssignment = 20; 7250 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 7251 " cccccccccccccccccccccccccc;", 7252 Style); 7253 } 7254 7255 TEST_F(FormatTest, AlignsAfterAssignments) { 7256 verifyFormat( 7257 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7258 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7259 verifyFormat( 7260 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7261 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7262 verifyFormat( 7263 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7264 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7265 verifyFormat( 7266 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7267 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 7268 verifyFormat( 7269 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 7270 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 7271 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 7272 } 7273 7274 TEST_F(FormatTest, AlignsAfterReturn) { 7275 verifyFormat( 7276 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7277 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7278 verifyFormat( 7279 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7280 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 7281 verifyFormat( 7282 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 7283 " aaaaaaaaaaaaaaaaaaaaaa();"); 7284 verifyFormat( 7285 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 7286 " aaaaaaaaaaaaaaaaaaaaaa());"); 7287 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7288 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7289 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7290 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 7291 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7292 verifyFormat("return\n" 7293 " // true if code is one of a or b.\n" 7294 " code == a || code == b;"); 7295 } 7296 7297 TEST_F(FormatTest, AlignsAfterOpenBracket) { 7298 verifyFormat( 7299 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 7300 " aaaaaaaaa aaaaaaa) {}"); 7301 verifyFormat( 7302 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 7303 " aaaaaaaaaaa aaaaaaaaa);"); 7304 verifyFormat( 7305 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 7306 " aaaaaaaaaaaaaaaaaaaaa));"); 7307 FormatStyle Style = getLLVMStyle(); 7308 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7309 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7310 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 7311 Style); 7312 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 7313 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 7314 Style); 7315 verifyFormat("SomeLongVariableName->someFunction(\n" 7316 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 7317 Style); 7318 verifyFormat( 7319 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 7320 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 7321 Style); 7322 verifyFormat( 7323 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 7324 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7325 Style); 7326 verifyFormat( 7327 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 7328 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 7329 Style); 7330 7331 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n" 7332 " ccccccc(aaaaaaaaaaaaaaaaa, //\n" 7333 " b));", 7334 Style); 7335 7336 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 7337 Style.BinPackArguments = false; 7338 Style.BinPackParameters = false; 7339 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7340 " aaaaaaaaaaa aaaaaaaa,\n" 7341 " aaaaaaaaa aaaaaaa,\n" 7342 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 7343 Style); 7344 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 7345 " aaaaaaaaaaa aaaaaaaaa,\n" 7346 " aaaaaaaaaaa aaaaaaaaa,\n" 7347 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7348 Style); 7349 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" 7350 " aaaaaaaaaaaaaaa,\n" 7351 " aaaaaaaaaaaaaaaaaaaaa,\n" 7352 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 7353 Style); 7354 verifyFormat( 7355 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" 7356 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 7357 Style); 7358 verifyFormat( 7359 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" 7360 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 7361 Style); 7362 verifyFormat( 7363 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 7364 " aaaaaaaaaaaaaaaaaaaaa(\n" 7365 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n" 7366 " aaaaaaaaaaaaaaaa);", 7367 Style); 7368 verifyFormat( 7369 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 7370 " aaaaaaaaaaaaaaaaaaaaa(\n" 7371 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n" 7372 " aaaaaaaaaaaaaaaa);", 7373 Style); 7374 } 7375 7376 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 7377 FormatStyle Style = getLLVMStyleWithColumns(40); 7378 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7379 " bbbbbbbbbbbbbbbbbbbbbb);", 7380 Style); 7381 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 7382 Style.AlignOperands = FormatStyle::OAS_DontAlign; 7383 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7384 " bbbbbbbbbbbbbbbbbbbbbb);", 7385 Style); 7386 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7387 Style.AlignOperands = FormatStyle::OAS_Align; 7388 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7389 " bbbbbbbbbbbbbbbbbbbbbb);", 7390 Style); 7391 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7392 Style.AlignOperands = FormatStyle::OAS_DontAlign; 7393 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7394 " bbbbbbbbbbbbbbbbbbbbbb);", 7395 Style); 7396 } 7397 7398 TEST_F(FormatTest, BreaksConditionalExpressions) { 7399 verifyFormat( 7400 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7401 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7402 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7403 verifyFormat( 7404 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 7405 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7406 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7407 verifyFormat( 7408 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7409 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7410 verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n" 7411 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7412 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7413 verifyFormat( 7414 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 7415 " : aaaaaaaaaaaaa);"); 7416 verifyFormat( 7417 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7418 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7419 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7420 " aaaaaaaaaaaaa);"); 7421 verifyFormat( 7422 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7423 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7424 " aaaaaaaaaaaaa);"); 7425 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7426 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7427 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7428 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7429 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7430 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7431 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7432 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7433 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7434 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7435 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7436 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7437 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7438 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7439 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7440 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7441 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7442 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7443 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7444 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7445 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 7446 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7447 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7448 " : aaaaaaaaaaaaaaaa;"); 7449 verifyFormat( 7450 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7451 " ? aaaaaaaaaaaaaaa\n" 7452 " : aaaaaaaaaaaaaaa;"); 7453 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 7454 " aaaaaaaaa\n" 7455 " ? b\n" 7456 " : c);"); 7457 verifyFormat("return aaaa == bbbb\n" 7458 " // comment\n" 7459 " ? aaaa\n" 7460 " : bbbb;"); 7461 verifyFormat("unsigned Indent =\n" 7462 " format(TheLine.First,\n" 7463 " IndentForLevel[TheLine.Level] >= 0\n" 7464 " ? IndentForLevel[TheLine.Level]\n" 7465 " : TheLine * 2,\n" 7466 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 7467 getLLVMStyleWithColumns(60)); 7468 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 7469 " ? aaaaaaaaaaaaaaa\n" 7470 " : bbbbbbbbbbbbbbb //\n" 7471 " ? ccccccccccccccc\n" 7472 " : ddddddddddddddd;"); 7473 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 7474 " ? aaaaaaaaaaaaaaa\n" 7475 " : (bbbbbbbbbbbbbbb //\n" 7476 " ? ccccccccccccccc\n" 7477 " : ddddddddddddddd);"); 7478 verifyFormat( 7479 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7480 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7481 " aaaaaaaaaaaaaaaaaaaaa +\n" 7482 " aaaaaaaaaaaaaaaaaaaaa\n" 7483 " : aaaaaaaaaa;"); 7484 verifyFormat( 7485 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7486 " : aaaaaaaaaaaaaaaaaaaaaa\n" 7487 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7488 7489 FormatStyle NoBinPacking = getLLVMStyle(); 7490 NoBinPacking.BinPackArguments = false; 7491 verifyFormat( 7492 "void f() {\n" 7493 " g(aaa,\n" 7494 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 7495 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7496 " ? aaaaaaaaaaaaaaa\n" 7497 " : aaaaaaaaaaaaaaa);\n" 7498 "}", 7499 NoBinPacking); 7500 verifyFormat( 7501 "void f() {\n" 7502 " g(aaa,\n" 7503 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 7504 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7505 " ?: aaaaaaaaaaaaaaa);\n" 7506 "}", 7507 NoBinPacking); 7508 7509 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 7510 " // comment.\n" 7511 " ccccccccccccccccccccccccccccccccccccccc\n" 7512 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7513 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 7514 7515 // Assignments in conditional expressions. Apparently not uncommon :-(. 7516 verifyFormat("return a != b\n" 7517 " // comment\n" 7518 " ? a = b\n" 7519 " : a = b;"); 7520 verifyFormat("return a != b\n" 7521 " // comment\n" 7522 " ? a = a != b\n" 7523 " // comment\n" 7524 " ? a = b\n" 7525 " : a\n" 7526 " : a;\n"); 7527 verifyFormat("return a != b\n" 7528 " // comment\n" 7529 " ? a\n" 7530 " : a = a != b\n" 7531 " // comment\n" 7532 " ? a = b\n" 7533 " : a;"); 7534 7535 // Chained conditionals 7536 FormatStyle Style = getLLVMStyle(); 7537 Style.ColumnLimit = 70; 7538 Style.AlignOperands = FormatStyle::OAS_Align; 7539 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7540 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7541 " : 3333333333333333;", 7542 Style); 7543 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7544 " : bbbbbbbbbb ? 2222222222222222\n" 7545 " : 3333333333333333;", 7546 Style); 7547 verifyFormat("return aaaaaaaaaa ? 1111111111111111\n" 7548 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 7549 " : 3333333333333333;", 7550 Style); 7551 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7552 " : bbbbbbbbbbbbbb ? 222222\n" 7553 " : 333333;", 7554 Style); 7555 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7556 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7557 " : cccccccccccccc ? 3333333333333333\n" 7558 " : 4444444444444444;", 7559 Style); 7560 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc)\n" 7561 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7562 " : 3333333333333333;", 7563 Style); 7564 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7565 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7566 " : (aaa ? bbb : ccc);", 7567 Style); 7568 verifyFormat( 7569 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7570 " : cccccccccccccccccc)\n" 7571 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7572 " : 3333333333333333;", 7573 Style); 7574 verifyFormat( 7575 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7576 " : cccccccccccccccccc)\n" 7577 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7578 " : 3333333333333333;", 7579 Style); 7580 verifyFormat( 7581 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7582 " : dddddddddddddddddd)\n" 7583 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7584 " : 3333333333333333;", 7585 Style); 7586 verifyFormat( 7587 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7588 " : dddddddddddddddddd)\n" 7589 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7590 " : 3333333333333333;", 7591 Style); 7592 verifyFormat( 7593 "return aaaaaaaaa ? 1111111111111111\n" 7594 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7595 " : a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7596 " : dddddddddddddddddd)\n", 7597 Style); 7598 verifyFormat( 7599 "return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7600 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7601 " : (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7602 " : cccccccccccccccccc);", 7603 Style); 7604 verifyFormat( 7605 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7606 " : ccccccccccccccc ? dddddddddddddddddd\n" 7607 " : eeeeeeeeeeeeeeeeee)\n" 7608 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7609 " : 3333333333333333;", 7610 Style); 7611 verifyFormat( 7612 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7613 " : ccccccccccccccc ? dddddddddddddddddd\n" 7614 " : eeeeeeeeeeeeeeeeee)\n" 7615 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7616 " : 3333333333333333;", 7617 Style); 7618 verifyFormat( 7619 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7620 " : cccccccccccc ? dddddddddddddddddd\n" 7621 " : eeeeeeeeeeeeeeeeee)\n" 7622 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7623 " : 3333333333333333;", 7624 Style); 7625 verifyFormat( 7626 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7627 " : cccccccccccccccccc\n" 7628 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7629 " : 3333333333333333;", 7630 Style); 7631 verifyFormat( 7632 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7633 " : cccccccccccccccc ? dddddddddddddddddd\n" 7634 " : eeeeeeeeeeeeeeeeee\n" 7635 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7636 " : 3333333333333333;", 7637 Style); 7638 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa\n" 7639 " ? (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7640 " : cccccccccccccccccc ? dddddddddddddddddd\n" 7641 " : eeeeeeeeeeeeeeeeee)\n" 7642 " : bbbbbbbbbbbbbbbbbbb ? 2222222222222222\n" 7643 " : 3333333333333333;", 7644 Style); 7645 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaa\n" 7646 " ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7647 " : cccccccccccccccc ? dddddddddddddddddd\n" 7648 " : eeeeeeeeeeeeeeeeee\n" 7649 " : bbbbbbbbbbbbbbbbbbbbbbb ? 2222222222222222\n" 7650 " : 3333333333333333;", 7651 Style); 7652 7653 Style.AlignOperands = FormatStyle::OAS_DontAlign; 7654 Style.BreakBeforeTernaryOperators = false; 7655 // FIXME: Aligning the question marks is weird given DontAlign. 7656 // Consider disabling this alignment in this case. Also check whether this 7657 // will render the adjustment from https://reviews.llvm.org/D82199 7658 // unnecessary. 7659 verifyFormat("int x = aaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa :\n" 7660 " bbbb ? cccccccccccccccccc :\n" 7661 " ddddd;\n", 7662 Style); 7663 7664 EXPECT_EQ( 7665 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n" 7666 " /*\n" 7667 " */\n" 7668 " function() {\n" 7669 " try {\n" 7670 " return JJJJJJJJJJJJJJ(\n" 7671 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n" 7672 " }\n" 7673 " } :\n" 7674 " function() {};", 7675 format( 7676 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n" 7677 " /*\n" 7678 " */\n" 7679 " function() {\n" 7680 " try {\n" 7681 " return JJJJJJJJJJJJJJ(\n" 7682 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n" 7683 " }\n" 7684 " } :\n" 7685 " function() {};", 7686 getGoogleStyle(FormatStyle::LK_JavaScript))); 7687 } 7688 7689 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 7690 FormatStyle Style = getLLVMStyle(); 7691 Style.BreakBeforeTernaryOperators = false; 7692 Style.ColumnLimit = 70; 7693 verifyFormat( 7694 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7695 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7696 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7697 Style); 7698 verifyFormat( 7699 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 7700 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7701 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7702 Style); 7703 verifyFormat( 7704 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7705 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7706 Style); 7707 verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n" 7708 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7709 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7710 Style); 7711 verifyFormat( 7712 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 7713 " aaaaaaaaaaaaa);", 7714 Style); 7715 verifyFormat( 7716 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7717 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7718 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7719 " aaaaaaaaaaaaa);", 7720 Style); 7721 verifyFormat( 7722 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7723 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7724 " aaaaaaaaaaaaa);", 7725 Style); 7726 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7727 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7728 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 7729 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7730 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7731 Style); 7732 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7733 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7734 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7735 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 7736 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7737 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7738 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7739 Style); 7740 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7741 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 7742 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7743 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7744 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7745 Style); 7746 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7747 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7748 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 7749 Style); 7750 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 7751 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7752 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7753 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 7754 Style); 7755 verifyFormat( 7756 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7757 " aaaaaaaaaaaaaaa :\n" 7758 " aaaaaaaaaaaaaaa;", 7759 Style); 7760 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 7761 " aaaaaaaaa ?\n" 7762 " b :\n" 7763 " c);", 7764 Style); 7765 verifyFormat("unsigned Indent =\n" 7766 " format(TheLine.First,\n" 7767 " IndentForLevel[TheLine.Level] >= 0 ?\n" 7768 " IndentForLevel[TheLine.Level] :\n" 7769 " TheLine * 2,\n" 7770 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 7771 Style); 7772 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 7773 " aaaaaaaaaaaaaaa :\n" 7774 " bbbbbbbbbbbbbbb ? //\n" 7775 " ccccccccccccccc :\n" 7776 " ddddddddddddddd;", 7777 Style); 7778 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 7779 " aaaaaaaaaaaaaaa :\n" 7780 " (bbbbbbbbbbbbbbb ? //\n" 7781 " ccccccccccccccc :\n" 7782 " ddddddddddddddd);", 7783 Style); 7784 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7785 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n" 7786 " ccccccccccccccccccccccccccc;", 7787 Style); 7788 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7789 " aaaaa :\n" 7790 " bbbbbbbbbbbbbbb + cccccccccccccccc;", 7791 Style); 7792 7793 // Chained conditionals 7794 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7795 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7796 " 3333333333333333;", 7797 Style); 7798 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7799 " bbbbbbbbbb ? 2222222222222222 :\n" 7800 " 3333333333333333;", 7801 Style); 7802 verifyFormat("return aaaaaaaaaa ? 1111111111111111 :\n" 7803 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7804 " 3333333333333333;", 7805 Style); 7806 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7807 " bbbbbbbbbbbbbbbb ? 222222 :\n" 7808 " 333333;", 7809 Style); 7810 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7811 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7812 " cccccccccccccccc ? 3333333333333333 :\n" 7813 " 4444444444444444;", 7814 Style); 7815 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc) :\n" 7816 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7817 " 3333333333333333;", 7818 Style); 7819 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7820 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7821 " (aaa ? bbb : ccc);", 7822 Style); 7823 verifyFormat( 7824 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7825 " cccccccccccccccccc) :\n" 7826 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7827 " 3333333333333333;", 7828 Style); 7829 verifyFormat( 7830 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7831 " cccccccccccccccccc) :\n" 7832 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7833 " 3333333333333333;", 7834 Style); 7835 verifyFormat( 7836 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7837 " dddddddddddddddddd) :\n" 7838 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7839 " 3333333333333333;", 7840 Style); 7841 verifyFormat( 7842 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7843 " dddddddddddddddddd) :\n" 7844 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7845 " 3333333333333333;", 7846 Style); 7847 verifyFormat( 7848 "return aaaaaaaaa ? 1111111111111111 :\n" 7849 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7850 " a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7851 " dddddddddddddddddd)\n", 7852 Style); 7853 verifyFormat( 7854 "return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7855 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7856 " (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7857 " cccccccccccccccccc);", 7858 Style); 7859 verifyFormat( 7860 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7861 " ccccccccccccccccc ? dddddddddddddddddd :\n" 7862 " eeeeeeeeeeeeeeeeee) :\n" 7863 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7864 " 3333333333333333;", 7865 Style); 7866 verifyFormat( 7867 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7868 " ccccccccccccc ? dddddddddddddddddd :\n" 7869 " eeeeeeeeeeeeeeeeee) :\n" 7870 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7871 " 3333333333333333;", 7872 Style); 7873 verifyFormat( 7874 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7875 " ccccccccccccccccc ? dddddddddddddddddd :\n" 7876 " eeeeeeeeeeeeeeeeee) :\n" 7877 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7878 " 3333333333333333;", 7879 Style); 7880 verifyFormat( 7881 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7882 " cccccccccccccccccc :\n" 7883 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7884 " 3333333333333333;", 7885 Style); 7886 verifyFormat( 7887 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7888 " cccccccccccccccccc ? dddddddddddddddddd :\n" 7889 " eeeeeeeeeeeeeeeeee :\n" 7890 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7891 " 3333333333333333;", 7892 Style); 7893 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n" 7894 " (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7895 " cccccccccccccccccc ? dddddddddddddddddd :\n" 7896 " eeeeeeeeeeeeeeeeee) :\n" 7897 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7898 " 3333333333333333;", 7899 Style); 7900 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n" 7901 " aaaaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7902 " cccccccccccccccccccc ? dddddddddddddddddd :\n" 7903 " eeeeeeeeeeeeeeeeee :\n" 7904 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7905 " 3333333333333333;", 7906 Style); 7907 } 7908 7909 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 7910 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 7911 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 7912 verifyFormat("bool a = true, b = false;"); 7913 7914 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 7915 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 7916 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 7917 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 7918 verifyFormat( 7919 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 7920 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 7921 " d = e && f;"); 7922 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 7923 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 7924 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 7925 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 7926 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 7927 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 7928 7929 FormatStyle Style = getGoogleStyle(); 7930 Style.PointerAlignment = FormatStyle::PAS_Left; 7931 Style.DerivePointerAlignment = false; 7932 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7933 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 7934 " *b = bbbbbbbbbbbbbbbbbbb;", 7935 Style); 7936 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 7937 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 7938 Style); 7939 verifyFormat("vector<int*> a, b;", Style); 7940 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style); 7941 } 7942 7943 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 7944 verifyFormat("arr[foo ? bar : baz];"); 7945 verifyFormat("f()[foo ? bar : baz];"); 7946 verifyFormat("(a + b)[foo ? bar : baz];"); 7947 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 7948 } 7949 7950 TEST_F(FormatTest, AlignsStringLiterals) { 7951 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 7952 " \"short literal\");"); 7953 verifyFormat( 7954 "looooooooooooooooooooooooongFunction(\n" 7955 " \"short literal\"\n" 7956 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 7957 verifyFormat("someFunction(\"Always break between multi-line\"\n" 7958 " \" string literals\",\n" 7959 " and, other, parameters);"); 7960 EXPECT_EQ("fun + \"1243\" /* comment */\n" 7961 " \"5678\";", 7962 format("fun + \"1243\" /* comment */\n" 7963 " \"5678\";", 7964 getLLVMStyleWithColumns(28))); 7965 EXPECT_EQ( 7966 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7967 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 7968 " \"aaaaaaaaaaaaaaaa\";", 7969 format("aaaaaa =" 7970 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 7971 "aaaaaaaaaaaaaaaaaaaaa\" " 7972 "\"aaaaaaaaaaaaaaaa\";")); 7973 verifyFormat("a = a + \"a\"\n" 7974 " \"a\"\n" 7975 " \"a\";"); 7976 verifyFormat("f(\"a\", \"b\"\n" 7977 " \"c\");"); 7978 7979 verifyFormat( 7980 "#define LL_FORMAT \"ll\"\n" 7981 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 7982 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 7983 7984 verifyFormat("#define A(X) \\\n" 7985 " \"aaaaa\" #X \"bbbbbb\" \\\n" 7986 " \"ccccc\"", 7987 getLLVMStyleWithColumns(23)); 7988 verifyFormat("#define A \"def\"\n" 7989 "f(\"abc\" A \"ghi\"\n" 7990 " \"jkl\");"); 7991 7992 verifyFormat("f(L\"a\"\n" 7993 " L\"b\");"); 7994 verifyFormat("#define A(X) \\\n" 7995 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 7996 " L\"ccccc\"", 7997 getLLVMStyleWithColumns(25)); 7998 7999 verifyFormat("f(@\"a\"\n" 8000 " @\"b\");"); 8001 verifyFormat("NSString s = @\"a\"\n" 8002 " @\"b\"\n" 8003 " @\"c\";"); 8004 verifyFormat("NSString s = @\"a\"\n" 8005 " \"b\"\n" 8006 " \"c\";"); 8007 } 8008 8009 TEST_F(FormatTest, ReturnTypeBreakingStyle) { 8010 FormatStyle Style = getLLVMStyle(); 8011 // No declarations or definitions should be moved to own line. 8012 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; 8013 verifyFormat("class A {\n" 8014 " int f() { return 1; }\n" 8015 " int g();\n" 8016 "};\n" 8017 "int f() { return 1; }\n" 8018 "int g();\n", 8019 Style); 8020 8021 // All declarations and definitions should have the return type moved to its 8022 // own line. 8023 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 8024 Style.TypenameMacros = {"LIST"}; 8025 verifyFormat("SomeType\n" 8026 "funcdecl(LIST(uint64_t));", 8027 Style); 8028 verifyFormat("class E {\n" 8029 " int\n" 8030 " f() {\n" 8031 " return 1;\n" 8032 " }\n" 8033 " int\n" 8034 " g();\n" 8035 "};\n" 8036 "int\n" 8037 "f() {\n" 8038 " return 1;\n" 8039 "}\n" 8040 "int\n" 8041 "g();\n", 8042 Style); 8043 8044 // Top-level definitions, and no kinds of declarations should have the 8045 // return type moved to its own line. 8046 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; 8047 verifyFormat("class B {\n" 8048 " int f() { return 1; }\n" 8049 " int g();\n" 8050 "};\n" 8051 "int\n" 8052 "f() {\n" 8053 " return 1;\n" 8054 "}\n" 8055 "int g();\n", 8056 Style); 8057 8058 // Top-level definitions and declarations should have the return type moved 8059 // to its own line. 8060 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel; 8061 verifyFormat("class C {\n" 8062 " int f() { return 1; }\n" 8063 " int g();\n" 8064 "};\n" 8065 "int\n" 8066 "f() {\n" 8067 " return 1;\n" 8068 "}\n" 8069 "int\n" 8070 "g();\n", 8071 Style); 8072 8073 // All definitions should have the return type moved to its own line, but no 8074 // kinds of declarations. 8075 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 8076 verifyFormat("class D {\n" 8077 " int\n" 8078 " f() {\n" 8079 " return 1;\n" 8080 " }\n" 8081 " int g();\n" 8082 "};\n" 8083 "int\n" 8084 "f() {\n" 8085 " return 1;\n" 8086 "}\n" 8087 "int g();\n", 8088 Style); 8089 verifyFormat("const char *\n" 8090 "f(void) {\n" // Break here. 8091 " return \"\";\n" 8092 "}\n" 8093 "const char *bar(void);\n", // No break here. 8094 Style); 8095 verifyFormat("template <class T>\n" 8096 "T *\n" 8097 "f(T &c) {\n" // Break here. 8098 " return NULL;\n" 8099 "}\n" 8100 "template <class T> T *f(T &c);\n", // No break here. 8101 Style); 8102 verifyFormat("class C {\n" 8103 " int\n" 8104 " operator+() {\n" 8105 " return 1;\n" 8106 " }\n" 8107 " int\n" 8108 " operator()() {\n" 8109 " return 1;\n" 8110 " }\n" 8111 "};\n", 8112 Style); 8113 verifyFormat("void\n" 8114 "A::operator()() {}\n" 8115 "void\n" 8116 "A::operator>>() {}\n" 8117 "void\n" 8118 "A::operator+() {}\n" 8119 "void\n" 8120 "A::operator*() {}\n" 8121 "void\n" 8122 "A::operator->() {}\n" 8123 "void\n" 8124 "A::operator void *() {}\n" 8125 "void\n" 8126 "A::operator void &() {}\n" 8127 "void\n" 8128 "A::operator void &&() {}\n" 8129 "void\n" 8130 "A::operator char *() {}\n" 8131 "void\n" 8132 "A::operator[]() {}\n" 8133 "void\n" 8134 "A::operator!() {}\n" 8135 "void\n" 8136 "A::operator**() {}\n" 8137 "void\n" 8138 "A::operator<Foo> *() {}\n" 8139 "void\n" 8140 "A::operator<Foo> **() {}\n" 8141 "void\n" 8142 "A::operator<Foo> &() {}\n" 8143 "void\n" 8144 "A::operator void **() {}\n", 8145 Style); 8146 verifyFormat("constexpr auto\n" 8147 "operator()() const -> reference {}\n" 8148 "constexpr auto\n" 8149 "operator>>() const -> reference {}\n" 8150 "constexpr auto\n" 8151 "operator+() const -> reference {}\n" 8152 "constexpr auto\n" 8153 "operator*() const -> reference {}\n" 8154 "constexpr auto\n" 8155 "operator->() const -> reference {}\n" 8156 "constexpr auto\n" 8157 "operator++() const -> reference {}\n" 8158 "constexpr auto\n" 8159 "operator void *() const -> reference {}\n" 8160 "constexpr auto\n" 8161 "operator void **() const -> reference {}\n" 8162 "constexpr auto\n" 8163 "operator void *() const -> reference {}\n" 8164 "constexpr auto\n" 8165 "operator void &() const -> reference {}\n" 8166 "constexpr auto\n" 8167 "operator void &&() const -> reference {}\n" 8168 "constexpr auto\n" 8169 "operator char *() const -> reference {}\n" 8170 "constexpr auto\n" 8171 "operator!() const -> reference {}\n" 8172 "constexpr auto\n" 8173 "operator[]() const -> reference {}\n", 8174 Style); 8175 verifyFormat("void *operator new(std::size_t s);", // No break here. 8176 Style); 8177 verifyFormat("void *\n" 8178 "operator new(std::size_t s) {}", 8179 Style); 8180 verifyFormat("void *\n" 8181 "operator delete[](void *ptr) {}", 8182 Style); 8183 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 8184 verifyFormat("const char *\n" 8185 "f(void)\n" // Break here. 8186 "{\n" 8187 " return \"\";\n" 8188 "}\n" 8189 "const char *bar(void);\n", // No break here. 8190 Style); 8191 verifyFormat("template <class T>\n" 8192 "T *\n" // Problem here: no line break 8193 "f(T &c)\n" // Break here. 8194 "{\n" 8195 " return NULL;\n" 8196 "}\n" 8197 "template <class T> T *f(T &c);\n", // No break here. 8198 Style); 8199 verifyFormat("int\n" 8200 "foo(A<bool> a)\n" 8201 "{\n" 8202 " return a;\n" 8203 "}\n", 8204 Style); 8205 verifyFormat("int\n" 8206 "foo(A<8> a)\n" 8207 "{\n" 8208 " return a;\n" 8209 "}\n", 8210 Style); 8211 verifyFormat("int\n" 8212 "foo(A<B<bool>, 8> a)\n" 8213 "{\n" 8214 " return a;\n" 8215 "}\n", 8216 Style); 8217 verifyFormat("int\n" 8218 "foo(A<B<8>, bool> a)\n" 8219 "{\n" 8220 " return a;\n" 8221 "}\n", 8222 Style); 8223 verifyFormat("int\n" 8224 "foo(A<B<bool>, bool> a)\n" 8225 "{\n" 8226 " return a;\n" 8227 "}\n", 8228 Style); 8229 verifyFormat("int\n" 8230 "foo(A<B<8>, 8> a)\n" 8231 "{\n" 8232 " return a;\n" 8233 "}\n", 8234 Style); 8235 8236 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 8237 Style.BraceWrapping.AfterFunction = true; 8238 verifyFormat("int f(i);\n" // No break here. 8239 "int\n" // Break here. 8240 "f(i)\n" 8241 "{\n" 8242 " return i + 1;\n" 8243 "}\n" 8244 "int\n" // Break here. 8245 "f(i)\n" 8246 "{\n" 8247 " return i + 1;\n" 8248 "};", 8249 Style); 8250 verifyFormat("int f(a, b, c);\n" // No break here. 8251 "int\n" // Break here. 8252 "f(a, b, c)\n" // Break here. 8253 "short a, b;\n" 8254 "float c;\n" 8255 "{\n" 8256 " return a + b < c;\n" 8257 "}\n" 8258 "int\n" // Break here. 8259 "f(a, b, c)\n" // Break here. 8260 "short a, b;\n" 8261 "float c;\n" 8262 "{\n" 8263 " return a + b < c;\n" 8264 "};", 8265 Style); 8266 verifyFormat("byte *\n" // Break here. 8267 "f(a)\n" // Break here. 8268 "byte a[];\n" 8269 "{\n" 8270 " return a;\n" 8271 "}", 8272 Style); 8273 verifyFormat("bool f(int a, int) override;\n" 8274 "Bar g(int a, Bar) final;\n" 8275 "Bar h(a, Bar) final;", 8276 Style); 8277 verifyFormat("int\n" 8278 "f(a)", 8279 Style); 8280 8281 // The return breaking style doesn't affect: 8282 // * function and object definitions with attribute-like macros 8283 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n" 8284 " ABSL_GUARDED_BY(mutex) = {};", 8285 getGoogleStyleWithColumns(40)); 8286 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n" 8287 " ABSL_GUARDED_BY(mutex); // comment", 8288 getGoogleStyleWithColumns(40)); 8289 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n" 8290 " ABSL_GUARDED_BY(mutex1)\n" 8291 " ABSL_GUARDED_BY(mutex2);", 8292 getGoogleStyleWithColumns(40)); 8293 verifyFormat("Tttttt f(int a, int b)\n" 8294 " ABSL_GUARDED_BY(mutex1)\n" 8295 " ABSL_GUARDED_BY(mutex2);", 8296 getGoogleStyleWithColumns(40)); 8297 // * typedefs 8298 verifyFormat("typedef ATTR(X) char x;", getGoogleStyle()); 8299 8300 Style = getGNUStyle(); 8301 8302 // Test for comments at the end of function declarations. 8303 verifyFormat("void\n" 8304 "foo (int a, /*abc*/ int b) // def\n" 8305 "{\n" 8306 "}\n", 8307 Style); 8308 8309 verifyFormat("void\n" 8310 "foo (int a, /* abc */ int b) /* def */\n" 8311 "{\n" 8312 "}\n", 8313 Style); 8314 8315 // Definitions that should not break after return type 8316 verifyFormat("void foo (int a, int b); // def\n", Style); 8317 verifyFormat("void foo (int a, int b); /* def */\n", Style); 8318 verifyFormat("void foo (int a, int b);\n", Style); 8319 } 8320 8321 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 8322 FormatStyle NoBreak = getLLVMStyle(); 8323 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 8324 FormatStyle Break = getLLVMStyle(); 8325 Break.AlwaysBreakBeforeMultilineStrings = true; 8326 verifyFormat("aaaa = \"bbbb\"\n" 8327 " \"cccc\";", 8328 NoBreak); 8329 verifyFormat("aaaa =\n" 8330 " \"bbbb\"\n" 8331 " \"cccc\";", 8332 Break); 8333 verifyFormat("aaaa(\"bbbb\"\n" 8334 " \"cccc\");", 8335 NoBreak); 8336 verifyFormat("aaaa(\n" 8337 " \"bbbb\"\n" 8338 " \"cccc\");", 8339 Break); 8340 verifyFormat("aaaa(qqq, \"bbbb\"\n" 8341 " \"cccc\");", 8342 NoBreak); 8343 verifyFormat("aaaa(qqq,\n" 8344 " \"bbbb\"\n" 8345 " \"cccc\");", 8346 Break); 8347 verifyFormat("aaaa(qqq,\n" 8348 " L\"bbbb\"\n" 8349 " L\"cccc\");", 8350 Break); 8351 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 8352 " \"bbbb\"));", 8353 Break); 8354 verifyFormat("string s = someFunction(\n" 8355 " \"abc\"\n" 8356 " \"abc\");", 8357 Break); 8358 8359 // As we break before unary operators, breaking right after them is bad. 8360 verifyFormat("string foo = abc ? \"x\"\n" 8361 " \"blah blah blah blah blah blah\"\n" 8362 " : \"y\";", 8363 Break); 8364 8365 // Don't break if there is no column gain. 8366 verifyFormat("f(\"aaaa\"\n" 8367 " \"bbbb\");", 8368 Break); 8369 8370 // Treat literals with escaped newlines like multi-line string literals. 8371 EXPECT_EQ("x = \"a\\\n" 8372 "b\\\n" 8373 "c\";", 8374 format("x = \"a\\\n" 8375 "b\\\n" 8376 "c\";", 8377 NoBreak)); 8378 EXPECT_EQ("xxxx =\n" 8379 " \"a\\\n" 8380 "b\\\n" 8381 "c\";", 8382 format("xxxx = \"a\\\n" 8383 "b\\\n" 8384 "c\";", 8385 Break)); 8386 8387 EXPECT_EQ("NSString *const kString =\n" 8388 " @\"aaaa\"\n" 8389 " @\"bbbb\";", 8390 format("NSString *const kString = @\"aaaa\"\n" 8391 "@\"bbbb\";", 8392 Break)); 8393 8394 Break.ColumnLimit = 0; 8395 verifyFormat("const char *hello = \"hello llvm\";", Break); 8396 } 8397 8398 TEST_F(FormatTest, AlignsPipes) { 8399 verifyFormat( 8400 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8401 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8402 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8403 verifyFormat( 8404 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 8405 " << aaaaaaaaaaaaaaaaaaaa;"); 8406 verifyFormat( 8407 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8408 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8409 verifyFormat( 8410 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 8411 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8412 verifyFormat( 8413 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 8414 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 8415 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 8416 verifyFormat( 8417 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8418 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8419 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8420 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8421 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8422 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8423 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 8424 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 8425 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 8426 verifyFormat( 8427 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8428 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8429 verifyFormat( 8430 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n" 8431 " aaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8432 8433 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 8434 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 8435 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8436 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8437 " aaaaaaaaaaaaaaaaaaaaa)\n" 8438 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8439 verifyFormat("LOG_IF(aaa == //\n" 8440 " bbb)\n" 8441 " << a << b;"); 8442 8443 // But sometimes, breaking before the first "<<" is desirable. 8444 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 8445 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 8446 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 8447 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8448 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8449 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 8450 " << BEF << IsTemplate << Description << E->getType();"); 8451 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 8452 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8453 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8454 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 8455 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8456 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8457 " << aaa;"); 8458 8459 verifyFormat( 8460 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8461 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8462 8463 // Incomplete string literal. 8464 EXPECT_EQ("llvm::errs() << \"\n" 8465 " << a;", 8466 format("llvm::errs() << \"\n<<a;")); 8467 8468 verifyFormat("void f() {\n" 8469 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 8470 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 8471 "}"); 8472 8473 // Handle 'endl'. 8474 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 8475 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 8476 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 8477 8478 // Handle '\n'. 8479 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 8480 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 8481 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 8482 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 8483 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 8484 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 8485 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 8486 } 8487 8488 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) { 8489 verifyFormat("return out << \"somepacket = {\\n\"\n" 8490 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 8491 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 8492 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 8493 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 8494 " << \"}\";"); 8495 8496 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 8497 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 8498 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 8499 verifyFormat( 8500 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 8501 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 8502 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 8503 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 8504 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 8505 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 8506 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 8507 verifyFormat( 8508 "void f() {\n" 8509 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 8510 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 8511 "}"); 8512 8513 // Breaking before the first "<<" is generally not desirable. 8514 verifyFormat( 8515 "llvm::errs()\n" 8516 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8517 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8518 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8519 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 8520 getLLVMStyleWithColumns(70)); 8521 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 8522 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8523 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 8524 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8525 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 8526 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 8527 getLLVMStyleWithColumns(70)); 8528 8529 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 8530 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 8531 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;"); 8532 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 8533 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 8534 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);"); 8535 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n" 8536 " (aaaa + aaaa);", 8537 getLLVMStyleWithColumns(40)); 8538 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n" 8539 " (aaaaaaa + aaaaa));", 8540 getLLVMStyleWithColumns(40)); 8541 verifyFormat( 8542 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n" 8543 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n" 8544 " bbbbbbbbbbbbbbbbbbbbbbb);"); 8545 } 8546 8547 TEST_F(FormatTest, UnderstandsEquals) { 8548 verifyFormat( 8549 "aaaaaaaaaaaaaaaaa =\n" 8550 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8551 verifyFormat( 8552 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8553 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 8554 verifyFormat( 8555 "if (a) {\n" 8556 " f();\n" 8557 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8558 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 8559 "}"); 8560 8561 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8562 " 100000000 + 10000000) {\n}"); 8563 } 8564 8565 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 8566 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 8567 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 8568 8569 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 8570 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 8571 8572 verifyFormat( 8573 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 8574 " Parameter2);"); 8575 8576 verifyFormat( 8577 "ShortObject->shortFunction(\n" 8578 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 8579 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 8580 8581 verifyFormat("loooooooooooooongFunction(\n" 8582 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 8583 8584 verifyFormat( 8585 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 8586 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 8587 8588 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 8589 " .WillRepeatedly(Return(SomeValue));"); 8590 verifyFormat("void f() {\n" 8591 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 8592 " .Times(2)\n" 8593 " .WillRepeatedly(Return(SomeValue));\n" 8594 "}"); 8595 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 8596 " ccccccccccccccccccccccc);"); 8597 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8598 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8599 " .aaaaa(aaaaa),\n" 8600 " aaaaaaaaaaaaaaaaaaaaa);"); 8601 verifyFormat("void f() {\n" 8602 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8603 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 8604 "}"); 8605 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8606 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8607 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8608 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8609 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 8610 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8611 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8612 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8613 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 8614 "}"); 8615 8616 // Here, it is not necessary to wrap at "." or "->". 8617 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 8618 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 8619 verifyFormat( 8620 "aaaaaaaaaaa->aaaaaaaaa(\n" 8621 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8622 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 8623 8624 verifyFormat( 8625 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8626 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 8627 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 8628 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 8629 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 8630 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 8631 8632 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8633 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8634 " .a();"); 8635 8636 FormatStyle NoBinPacking = getLLVMStyle(); 8637 NoBinPacking.BinPackParameters = false; 8638 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 8639 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 8640 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 8641 " aaaaaaaaaaaaaaaaaaa,\n" 8642 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8643 NoBinPacking); 8644 8645 // If there is a subsequent call, change to hanging indentation. 8646 verifyFormat( 8647 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8648 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 8649 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8650 verifyFormat( 8651 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8652 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 8653 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8654 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8655 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8656 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8657 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8658 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 8659 } 8660 8661 TEST_F(FormatTest, WrapsTemplateDeclarations) { 8662 verifyFormat("template <typename T>\n" 8663 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 8664 verifyFormat("template <typename T>\n" 8665 "// T should be one of {A, B}.\n" 8666 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 8667 verifyFormat( 8668 "template <typename T>\n" 8669 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 8670 verifyFormat("template <typename T>\n" 8671 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 8672 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 8673 verifyFormat( 8674 "template <typename T>\n" 8675 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 8676 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 8677 verifyFormat( 8678 "template <typename T>\n" 8679 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 8680 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 8681 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8682 verifyFormat("template <typename T>\n" 8683 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8684 " int aaaaaaaaaaaaaaaaaaaaaa);"); 8685 verifyFormat( 8686 "template <typename T1, typename T2 = char, typename T3 = char,\n" 8687 " typename T4 = char>\n" 8688 "void f();"); 8689 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 8690 " template <typename> class cccccccccccccccccccccc,\n" 8691 " typename ddddddddddddd>\n" 8692 "class C {};"); 8693 verifyFormat( 8694 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 8695 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8696 8697 verifyFormat("void f() {\n" 8698 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 8699 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 8700 "}"); 8701 8702 verifyFormat("template <typename T> class C {};"); 8703 verifyFormat("template <typename T> void f();"); 8704 verifyFormat("template <typename T> void f() {}"); 8705 verifyFormat( 8706 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 8707 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8708 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 8709 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 8710 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8711 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 8712 " bbbbbbbbbbbbbbbbbbbbbbbb);", 8713 getLLVMStyleWithColumns(72)); 8714 EXPECT_EQ("static_cast<A< //\n" 8715 " B> *>(\n" 8716 "\n" 8717 ");", 8718 format("static_cast<A<//\n" 8719 " B>*>(\n" 8720 "\n" 8721 " );")); 8722 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8723 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 8724 8725 FormatStyle AlwaysBreak = getLLVMStyle(); 8726 AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 8727 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 8728 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 8729 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 8730 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8731 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 8732 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 8733 verifyFormat("template <template <typename> class Fooooooo,\n" 8734 " template <typename> class Baaaaaaar>\n" 8735 "struct C {};", 8736 AlwaysBreak); 8737 verifyFormat("template <typename T> // T can be A, B or C.\n" 8738 "struct C {};", 8739 AlwaysBreak); 8740 verifyFormat("template <enum E> class A {\n" 8741 "public:\n" 8742 " E *f();\n" 8743 "};"); 8744 8745 FormatStyle NeverBreak = getLLVMStyle(); 8746 NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No; 8747 verifyFormat("template <typename T> class C {};", NeverBreak); 8748 verifyFormat("template <typename T> void f();", NeverBreak); 8749 verifyFormat("template <typename T> void f() {}", NeverBreak); 8750 verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa " 8751 "bbbbbbbbbbbbbbbbbbbb) {}", 8752 NeverBreak); 8753 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8754 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 8755 " ccccccccccccccccccccccccccccccccccccccccccccccc);", 8756 NeverBreak); 8757 verifyFormat("template <template <typename> class Fooooooo,\n" 8758 " template <typename> class Baaaaaaar>\n" 8759 "struct C {};", 8760 NeverBreak); 8761 verifyFormat("template <typename T> // T can be A, B or C.\n" 8762 "struct C {};", 8763 NeverBreak); 8764 verifyFormat("template <enum E> class A {\n" 8765 "public:\n" 8766 " E *f();\n" 8767 "};", 8768 NeverBreak); 8769 NeverBreak.PenaltyBreakTemplateDeclaration = 100; 8770 verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa " 8771 "bbbbbbbbbbbbbbbbbbbb) {}", 8772 NeverBreak); 8773 } 8774 8775 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) { 8776 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 8777 Style.ColumnLimit = 60; 8778 EXPECT_EQ("// Baseline - no comments.\n" 8779 "template <\n" 8780 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 8781 "void f() {}", 8782 format("// Baseline - no comments.\n" 8783 "template <\n" 8784 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 8785 "void f() {}", 8786 Style)); 8787 8788 EXPECT_EQ("template <\n" 8789 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 8790 "void f() {}", 8791 format("template <\n" 8792 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 8793 "void f() {}", 8794 Style)); 8795 8796 EXPECT_EQ( 8797 "template <\n" 8798 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 8799 "void f() {}", 8800 format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 8801 "void f() {}", 8802 Style)); 8803 8804 EXPECT_EQ( 8805 "template <\n" 8806 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 8807 " // multiline\n" 8808 "void f() {}", 8809 format("template <\n" 8810 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 8811 " // multiline\n" 8812 "void f() {}", 8813 Style)); 8814 8815 EXPECT_EQ( 8816 "template <typename aaaaaaaaaa<\n" 8817 " bbbbbbbbbbbb>::value> // trailing loooong\n" 8818 "void f() {}", 8819 format( 8820 "template <\n" 8821 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n" 8822 "void f() {}", 8823 Style)); 8824 } 8825 8826 TEST_F(FormatTest, WrapsTemplateParameters) { 8827 FormatStyle Style = getLLVMStyle(); 8828 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 8829 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 8830 verifyFormat( 8831 "template <typename... a> struct q {};\n" 8832 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 8833 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 8834 " y;", 8835 Style); 8836 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 8837 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 8838 verifyFormat( 8839 "template <typename... a> struct r {};\n" 8840 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 8841 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 8842 " y;", 8843 Style); 8844 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 8845 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 8846 verifyFormat("template <typename... a> struct s {};\n" 8847 "extern s<\n" 8848 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 8849 "aaaaaaaaaaaaaaaaaaaaaa,\n" 8850 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 8851 "aaaaaaaaaaaaaaaaaaaaaa>\n" 8852 " y;", 8853 Style); 8854 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 8855 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 8856 verifyFormat("template <typename... a> struct t {};\n" 8857 "extern t<\n" 8858 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 8859 "aaaaaaaaaaaaaaaaaaaaaa,\n" 8860 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 8861 "aaaaaaaaaaaaaaaaaaaaaa>\n" 8862 " y;", 8863 Style); 8864 } 8865 8866 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 8867 verifyFormat( 8868 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8869 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8870 verifyFormat( 8871 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8872 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8873 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 8874 8875 // FIXME: Should we have the extra indent after the second break? 8876 verifyFormat( 8877 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8878 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8879 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8880 8881 verifyFormat( 8882 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 8883 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 8884 8885 // Breaking at nested name specifiers is generally not desirable. 8886 verifyFormat( 8887 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8888 " aaaaaaaaaaaaaaaaaaaaaaa);"); 8889 8890 verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n" 8891 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8892 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8893 " aaaaaaaaaaaaaaaaaaaaa);", 8894 getLLVMStyleWithColumns(74)); 8895 8896 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8897 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8898 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8899 } 8900 8901 TEST_F(FormatTest, UnderstandsTemplateParameters) { 8902 verifyFormat("A<int> a;"); 8903 verifyFormat("A<A<A<int>>> a;"); 8904 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 8905 verifyFormat("bool x = a < 1 || 2 > a;"); 8906 verifyFormat("bool x = 5 < f<int>();"); 8907 verifyFormat("bool x = f<int>() > 5;"); 8908 verifyFormat("bool x = 5 < a<int>::x;"); 8909 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 8910 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 8911 8912 verifyGoogleFormat("A<A<int>> a;"); 8913 verifyGoogleFormat("A<A<A<int>>> a;"); 8914 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 8915 verifyGoogleFormat("A<A<int> > a;"); 8916 verifyGoogleFormat("A<A<A<int> > > a;"); 8917 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 8918 verifyGoogleFormat("A<::A<int>> a;"); 8919 verifyGoogleFormat("A<::A> a;"); 8920 verifyGoogleFormat("A< ::A> a;"); 8921 verifyGoogleFormat("A< ::A<int> > a;"); 8922 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 8923 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 8924 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 8925 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 8926 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 8927 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 8928 8929 verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 8930 8931 // template closer followed by a token that starts with > or = 8932 verifyFormat("bool b = a<1> > 1;"); 8933 verifyFormat("bool b = a<1> >= 1;"); 8934 verifyFormat("int i = a<1> >> 1;"); 8935 FormatStyle Style = getLLVMStyle(); 8936 Style.SpaceBeforeAssignmentOperators = false; 8937 verifyFormat("bool b= a<1> == 1;", Style); 8938 verifyFormat("a<int> = 1;", Style); 8939 verifyFormat("a<int> >>= 1;", Style); 8940 8941 verifyFormat("test < a | b >> c;"); 8942 verifyFormat("test<test<a | b>> c;"); 8943 verifyFormat("test >> a >> b;"); 8944 verifyFormat("test << a >> b;"); 8945 8946 verifyFormat("f<int>();"); 8947 verifyFormat("template <typename T> void f() {}"); 8948 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 8949 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 8950 "sizeof(char)>::type>;"); 8951 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 8952 verifyFormat("f(a.operator()<A>());"); 8953 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8954 " .template operator()<A>());", 8955 getLLVMStyleWithColumns(35)); 8956 8957 // Not template parameters. 8958 verifyFormat("return a < b && c > d;"); 8959 verifyFormat("void f() {\n" 8960 " while (a < b && c > d) {\n" 8961 " }\n" 8962 "}"); 8963 verifyFormat("template <typename... Types>\n" 8964 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 8965 8966 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8967 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 8968 getLLVMStyleWithColumns(60)); 8969 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 8970 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 8971 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 8972 verifyFormat("some_templated_type<decltype([](int i) { return i; })>"); 8973 } 8974 8975 TEST_F(FormatTest, UnderstandsShiftOperators) { 8976 verifyFormat("if (i < x >> 1)"); 8977 verifyFormat("while (i < x >> 1)"); 8978 verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)"); 8979 verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)"); 8980 verifyFormat( 8981 "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)"); 8982 verifyFormat("Foo.call<Bar<Function>>()"); 8983 verifyFormat("if (Foo.call<Bar<Function>>() == 0)"); 8984 verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; " 8985 "++i, v = v >> 1)"); 8986 verifyFormat("if (w<u<v<x>>, 1>::t)"); 8987 } 8988 8989 TEST_F(FormatTest, BitshiftOperatorWidth) { 8990 EXPECT_EQ("int a = 1 << 2; /* foo\n" 8991 " bar */", 8992 format("int a=1<<2; /* foo\n" 8993 " bar */")); 8994 8995 EXPECT_EQ("int b = 256 >> 1; /* foo\n" 8996 " bar */", 8997 format("int b =256>>1 ; /* foo\n" 8998 " bar */")); 8999 } 9000 9001 TEST_F(FormatTest, UnderstandsBinaryOperators) { 9002 verifyFormat("COMPARE(a, ==, b);"); 9003 verifyFormat("auto s = sizeof...(Ts) - 1;"); 9004 } 9005 9006 TEST_F(FormatTest, UnderstandsPointersToMembers) { 9007 verifyFormat("int A::*x;"); 9008 verifyFormat("int (S::*func)(void *);"); 9009 verifyFormat("void f() { int (S::*func)(void *); }"); 9010 verifyFormat("typedef bool *(Class::*Member)() const;"); 9011 verifyFormat("void f() {\n" 9012 " (a->*f)();\n" 9013 " a->*x;\n" 9014 " (a.*f)();\n" 9015 " ((*a).*f)();\n" 9016 " a.*x;\n" 9017 "}"); 9018 verifyFormat("void f() {\n" 9019 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 9020 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 9021 "}"); 9022 verifyFormat( 9023 "(aaaaaaaaaa->*bbbbbbb)(\n" 9024 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 9025 FormatStyle Style = getLLVMStyle(); 9026 Style.PointerAlignment = FormatStyle::PAS_Left; 9027 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 9028 } 9029 9030 TEST_F(FormatTest, UnderstandsUnaryOperators) { 9031 verifyFormat("int a = -2;"); 9032 verifyFormat("f(-1, -2, -3);"); 9033 verifyFormat("a[-1] = 5;"); 9034 verifyFormat("int a = 5 + -2;"); 9035 verifyFormat("if (i == -1) {\n}"); 9036 verifyFormat("if (i != -1) {\n}"); 9037 verifyFormat("if (i > -1) {\n}"); 9038 verifyFormat("if (i < -1) {\n}"); 9039 verifyFormat("++(a->f());"); 9040 verifyFormat("--(a->f());"); 9041 verifyFormat("(a->f())++;"); 9042 verifyFormat("a[42]++;"); 9043 verifyFormat("if (!(a->f())) {\n}"); 9044 verifyFormat("if (!+i) {\n}"); 9045 verifyFormat("~&a;"); 9046 9047 verifyFormat("a-- > b;"); 9048 verifyFormat("b ? -a : c;"); 9049 verifyFormat("n * sizeof char16;"); 9050 verifyFormat("n * alignof char16;", getGoogleStyle()); 9051 verifyFormat("sizeof(char);"); 9052 verifyFormat("alignof(char);", getGoogleStyle()); 9053 9054 verifyFormat("return -1;"); 9055 verifyFormat("throw -1;"); 9056 verifyFormat("switch (a) {\n" 9057 "case -1:\n" 9058 " break;\n" 9059 "}"); 9060 verifyFormat("#define X -1"); 9061 verifyFormat("#define X -kConstant"); 9062 9063 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 9064 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 9065 9066 verifyFormat("int a = /* confusing comment */ -1;"); 9067 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 9068 verifyFormat("int a = i /* confusing comment */++;"); 9069 9070 verifyFormat("co_yield -1;"); 9071 verifyFormat("co_return -1;"); 9072 9073 // Check that * is not treated as a binary operator when we set 9074 // PointerAlignment as PAS_Left after a keyword and not a declaration. 9075 FormatStyle PASLeftStyle = getLLVMStyle(); 9076 PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left; 9077 verifyFormat("co_return *a;", PASLeftStyle); 9078 verifyFormat("co_await *a;", PASLeftStyle); 9079 verifyFormat("co_yield *a", PASLeftStyle); 9080 verifyFormat("return *a;", PASLeftStyle); 9081 } 9082 9083 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 9084 verifyFormat("if (!aaaaaaaaaa( // break\n" 9085 " aaaaa)) {\n" 9086 "}"); 9087 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 9088 " aaaaa));"); 9089 verifyFormat("*aaa = aaaaaaa( // break\n" 9090 " bbbbbb);"); 9091 } 9092 9093 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 9094 verifyFormat("bool operator<();"); 9095 verifyFormat("bool operator>();"); 9096 verifyFormat("bool operator=();"); 9097 verifyFormat("bool operator==();"); 9098 verifyFormat("bool operator!=();"); 9099 verifyFormat("int operator+();"); 9100 verifyFormat("int operator++();"); 9101 verifyFormat("int operator++(int) volatile noexcept;"); 9102 verifyFormat("bool operator,();"); 9103 verifyFormat("bool operator();"); 9104 verifyFormat("bool operator()();"); 9105 verifyFormat("bool operator[]();"); 9106 verifyFormat("operator bool();"); 9107 verifyFormat("operator int();"); 9108 verifyFormat("operator void *();"); 9109 verifyFormat("operator SomeType<int>();"); 9110 verifyFormat("operator SomeType<int, int>();"); 9111 verifyFormat("operator SomeType<SomeType<int>>();"); 9112 verifyFormat("void *operator new(std::size_t size);"); 9113 verifyFormat("void *operator new[](std::size_t size);"); 9114 verifyFormat("void operator delete(void *ptr);"); 9115 verifyFormat("void operator delete[](void *ptr);"); 9116 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 9117 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 9118 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 9119 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 9120 9121 verifyFormat( 9122 "ostream &operator<<(ostream &OutputStream,\n" 9123 " SomeReallyLongType WithSomeReallyLongValue);"); 9124 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 9125 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 9126 " return left.group < right.group;\n" 9127 "}"); 9128 verifyFormat("SomeType &operator=(const SomeType &S);"); 9129 verifyFormat("f.template operator()<int>();"); 9130 9131 verifyGoogleFormat("operator void*();"); 9132 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 9133 verifyGoogleFormat("operator ::A();"); 9134 9135 verifyFormat("using A::operator+;"); 9136 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 9137 "int i;"); 9138 9139 // Calling an operator as a member function. 9140 verifyFormat("void f() { a.operator*(); }"); 9141 verifyFormat("void f() { a.operator*(b & b); }"); 9142 verifyFormat("void f() { a->operator&(a * b); }"); 9143 verifyFormat("void f() { NS::a.operator+(*b * *b); }"); 9144 // TODO: Calling an operator as a non-member function is hard to distinguish. 9145 // https://llvm.org/PR50629 9146 // verifyFormat("void f() { operator*(a & a); }"); 9147 // verifyFormat("void f() { operator&(a, b * b); }"); 9148 } 9149 9150 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 9151 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 9152 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 9153 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 9154 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 9155 verifyFormat("Deleted &operator=(const Deleted &) &;"); 9156 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 9157 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 9158 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 9159 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 9160 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 9161 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 9162 verifyFormat("void Fn(T const &) const &;"); 9163 verifyFormat("void Fn(T const volatile &&) const volatile &&;"); 9164 verifyFormat("template <typename T>\n" 9165 "void F(T) && = delete;", 9166 getGoogleStyle()); 9167 9168 FormatStyle AlignLeft = getLLVMStyle(); 9169 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 9170 verifyFormat("void A::b() && {}", AlignLeft); 9171 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 9172 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 9173 AlignLeft); 9174 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 9175 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 9176 verifyFormat("auto Function(T t) & -> void {}", AlignLeft); 9177 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft); 9178 verifyFormat("auto Function(T) & -> void {}", AlignLeft); 9179 verifyFormat("auto Function(T) & -> void;", AlignLeft); 9180 verifyFormat("void Fn(T const&) const&;", AlignLeft); 9181 verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft); 9182 9183 FormatStyle Spaces = getLLVMStyle(); 9184 Spaces.SpacesInCStyleCastParentheses = true; 9185 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 9186 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 9187 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 9188 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 9189 9190 Spaces.SpacesInCStyleCastParentheses = false; 9191 Spaces.SpacesInParentheses = true; 9192 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 9193 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", 9194 Spaces); 9195 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 9196 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 9197 9198 FormatStyle BreakTemplate = getLLVMStyle(); 9199 BreakTemplate.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 9200 9201 verifyFormat("struct f {\n" 9202 " template <class T>\n" 9203 " int &foo(const std::string &str) &noexcept {}\n" 9204 "};", 9205 BreakTemplate); 9206 9207 verifyFormat("struct f {\n" 9208 " template <class T>\n" 9209 " int &foo(const std::string &str) &&noexcept {}\n" 9210 "};", 9211 BreakTemplate); 9212 9213 verifyFormat("struct f {\n" 9214 " template <class T>\n" 9215 " int &foo(const std::string &str) const &noexcept {}\n" 9216 "};", 9217 BreakTemplate); 9218 9219 verifyFormat("struct f {\n" 9220 " template <class T>\n" 9221 " int &foo(const std::string &str) const &noexcept {}\n" 9222 "};", 9223 BreakTemplate); 9224 9225 verifyFormat("struct f {\n" 9226 " template <class T>\n" 9227 " auto foo(const std::string &str) &&noexcept -> int & {}\n" 9228 "};", 9229 BreakTemplate); 9230 9231 FormatStyle AlignLeftBreakTemplate = getLLVMStyle(); 9232 AlignLeftBreakTemplate.AlwaysBreakTemplateDeclarations = 9233 FormatStyle::BTDS_Yes; 9234 AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left; 9235 9236 verifyFormat("struct f {\n" 9237 " template <class T>\n" 9238 " int& foo(const std::string& str) & noexcept {}\n" 9239 "};", 9240 AlignLeftBreakTemplate); 9241 9242 verifyFormat("struct f {\n" 9243 " template <class T>\n" 9244 " int& foo(const std::string& str) && noexcept {}\n" 9245 "};", 9246 AlignLeftBreakTemplate); 9247 9248 verifyFormat("struct f {\n" 9249 " template <class T>\n" 9250 " int& foo(const std::string& str) const& noexcept {}\n" 9251 "};", 9252 AlignLeftBreakTemplate); 9253 9254 verifyFormat("struct f {\n" 9255 " template <class T>\n" 9256 " int& foo(const std::string& str) const&& noexcept {}\n" 9257 "};", 9258 AlignLeftBreakTemplate); 9259 9260 verifyFormat("struct f {\n" 9261 " template <class T>\n" 9262 " auto foo(const std::string& str) && noexcept -> int& {}\n" 9263 "};", 9264 AlignLeftBreakTemplate); 9265 9266 // The `&` in `Type&` should not be confused with a trailing `&` of 9267 // DEPRECATED(reason) member function. 9268 verifyFormat("struct f {\n" 9269 " template <class T>\n" 9270 " DEPRECATED(reason)\n" 9271 " Type &foo(arguments) {}\n" 9272 "};", 9273 BreakTemplate); 9274 9275 verifyFormat("struct f {\n" 9276 " template <class T>\n" 9277 " DEPRECATED(reason)\n" 9278 " Type& foo(arguments) {}\n" 9279 "};", 9280 AlignLeftBreakTemplate); 9281 9282 verifyFormat("void (*foopt)(int) = &func;"); 9283 } 9284 9285 TEST_F(FormatTest, UnderstandsNewAndDelete) { 9286 verifyFormat("void f() {\n" 9287 " A *a = new A;\n" 9288 " A *a = new (placement) A;\n" 9289 " delete a;\n" 9290 " delete (A *)a;\n" 9291 "}"); 9292 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 9293 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 9294 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 9295 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 9296 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 9297 verifyFormat("delete[] h->p;"); 9298 } 9299 9300 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 9301 verifyFormat("int *f(int *a) {}"); 9302 verifyFormat("int main(int argc, char **argv) {}"); 9303 verifyFormat("Test::Test(int b) : a(b * b) {}"); 9304 verifyIndependentOfContext("f(a, *a);"); 9305 verifyFormat("void g() { f(*a); }"); 9306 verifyIndependentOfContext("int a = b * 10;"); 9307 verifyIndependentOfContext("int a = 10 * b;"); 9308 verifyIndependentOfContext("int a = b * c;"); 9309 verifyIndependentOfContext("int a += b * c;"); 9310 verifyIndependentOfContext("int a -= b * c;"); 9311 verifyIndependentOfContext("int a *= b * c;"); 9312 verifyIndependentOfContext("int a /= b * c;"); 9313 verifyIndependentOfContext("int a = *b;"); 9314 verifyIndependentOfContext("int a = *b * c;"); 9315 verifyIndependentOfContext("int a = b * *c;"); 9316 verifyIndependentOfContext("int a = b * (10);"); 9317 verifyIndependentOfContext("S << b * (10);"); 9318 verifyIndependentOfContext("return 10 * b;"); 9319 verifyIndependentOfContext("return *b * *c;"); 9320 verifyIndependentOfContext("return a & ~b;"); 9321 verifyIndependentOfContext("f(b ? *c : *d);"); 9322 verifyIndependentOfContext("int a = b ? *c : *d;"); 9323 verifyIndependentOfContext("*b = a;"); 9324 verifyIndependentOfContext("a * ~b;"); 9325 verifyIndependentOfContext("a * !b;"); 9326 verifyIndependentOfContext("a * +b;"); 9327 verifyIndependentOfContext("a * -b;"); 9328 verifyIndependentOfContext("a * ++b;"); 9329 verifyIndependentOfContext("a * --b;"); 9330 verifyIndependentOfContext("a[4] * b;"); 9331 verifyIndependentOfContext("a[a * a] = 1;"); 9332 verifyIndependentOfContext("f() * b;"); 9333 verifyIndependentOfContext("a * [self dostuff];"); 9334 verifyIndependentOfContext("int x = a * (a + b);"); 9335 verifyIndependentOfContext("(a *)(a + b);"); 9336 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 9337 verifyIndependentOfContext("int *pa = (int *)&a;"); 9338 verifyIndependentOfContext("return sizeof(int **);"); 9339 verifyIndependentOfContext("return sizeof(int ******);"); 9340 verifyIndependentOfContext("return (int **&)a;"); 9341 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 9342 verifyFormat("void f(Type (*parameter)[10]) {}"); 9343 verifyFormat("void f(Type (¶meter)[10]) {}"); 9344 verifyGoogleFormat("return sizeof(int**);"); 9345 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 9346 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 9347 verifyFormat("auto a = [](int **&, int ***) {};"); 9348 verifyFormat("auto PointerBinding = [](const char *S) {};"); 9349 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 9350 verifyFormat("[](const decltype(*a) &value) {}"); 9351 verifyFormat("[](const typeof(*a) &value) {}"); 9352 verifyFormat("[](const _Atomic(a *) &value) {}"); 9353 verifyFormat("[](const __underlying_type(a) &value) {}"); 9354 verifyFormat("decltype(a * b) F();"); 9355 verifyFormat("typeof(a * b) F();"); 9356 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 9357 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 9358 verifyIndependentOfContext("typedef void (*f)(int *a);"); 9359 verifyIndependentOfContext("int i{a * b};"); 9360 verifyIndependentOfContext("aaa && aaa->f();"); 9361 verifyIndependentOfContext("int x = ~*p;"); 9362 verifyFormat("Constructor() : a(a), area(width * height) {}"); 9363 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 9364 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 9365 verifyFormat("void f() { f(a, c * d); }"); 9366 verifyFormat("void f() { f(new a(), c * d); }"); 9367 verifyFormat("void f(const MyOverride &override);"); 9368 verifyFormat("void f(const MyFinal &final);"); 9369 verifyIndependentOfContext("bool a = f() && override.f();"); 9370 verifyIndependentOfContext("bool a = f() && final.f();"); 9371 9372 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 9373 9374 verifyIndependentOfContext("A<int *> a;"); 9375 verifyIndependentOfContext("A<int **> a;"); 9376 verifyIndependentOfContext("A<int *, int *> a;"); 9377 verifyIndependentOfContext("A<int *[]> a;"); 9378 verifyIndependentOfContext( 9379 "const char *const p = reinterpret_cast<const char *const>(q);"); 9380 verifyIndependentOfContext("A<int **, int **> a;"); 9381 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 9382 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 9383 verifyFormat("for (; a && b;) {\n}"); 9384 verifyFormat("bool foo = true && [] { return false; }();"); 9385 9386 verifyFormat( 9387 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9388 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9389 9390 verifyGoogleFormat("int const* a = &b;"); 9391 verifyGoogleFormat("**outparam = 1;"); 9392 verifyGoogleFormat("*outparam = a * b;"); 9393 verifyGoogleFormat("int main(int argc, char** argv) {}"); 9394 verifyGoogleFormat("A<int*> a;"); 9395 verifyGoogleFormat("A<int**> a;"); 9396 verifyGoogleFormat("A<int*, int*> a;"); 9397 verifyGoogleFormat("A<int**, int**> a;"); 9398 verifyGoogleFormat("f(b ? *c : *d);"); 9399 verifyGoogleFormat("int a = b ? *c : *d;"); 9400 verifyGoogleFormat("Type* t = **x;"); 9401 verifyGoogleFormat("Type* t = *++*x;"); 9402 verifyGoogleFormat("*++*x;"); 9403 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 9404 verifyGoogleFormat("Type* t = x++ * y;"); 9405 verifyGoogleFormat( 9406 "const char* const p = reinterpret_cast<const char* const>(q);"); 9407 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 9408 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 9409 verifyGoogleFormat("template <typename T>\n" 9410 "void f(int i = 0, SomeType** temps = NULL);"); 9411 9412 FormatStyle Left = getLLVMStyle(); 9413 Left.PointerAlignment = FormatStyle::PAS_Left; 9414 verifyFormat("x = *a(x) = *a(y);", Left); 9415 verifyFormat("for (;; *a = b) {\n}", Left); 9416 verifyFormat("return *this += 1;", Left); 9417 verifyFormat("throw *x;", Left); 9418 verifyFormat("delete *x;", Left); 9419 verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left); 9420 verifyFormat("[](const decltype(*a)* ptr) {}", Left); 9421 verifyFormat("[](const typeof(*a)* ptr) {}", Left); 9422 verifyFormat("[](const _Atomic(a*)* ptr) {}", Left); 9423 verifyFormat("[](const __underlying_type(a)* ptr) {}", Left); 9424 verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left); 9425 verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left); 9426 verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left); 9427 verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left); 9428 9429 verifyIndependentOfContext("a = *(x + y);"); 9430 verifyIndependentOfContext("a = &(x + y);"); 9431 verifyIndependentOfContext("*(x + y).call();"); 9432 verifyIndependentOfContext("&(x + y)->call();"); 9433 verifyFormat("void f() { &(*I).first; }"); 9434 9435 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 9436 verifyFormat( 9437 "int *MyValues = {\n" 9438 " *A, // Operator detection might be confused by the '{'\n" 9439 " *BB // Operator detection might be confused by previous comment\n" 9440 "};"); 9441 9442 verifyIndependentOfContext("if (int *a = &b)"); 9443 verifyIndependentOfContext("if (int &a = *b)"); 9444 verifyIndependentOfContext("if (a & b[i])"); 9445 verifyIndependentOfContext("if constexpr (a & b[i])"); 9446 verifyIndependentOfContext("if CONSTEXPR (a & b[i])"); 9447 verifyIndependentOfContext("if (a * (b * c))"); 9448 verifyIndependentOfContext("if constexpr (a * (b * c))"); 9449 verifyIndependentOfContext("if CONSTEXPR (a * (b * c))"); 9450 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 9451 verifyIndependentOfContext("if (*b[i])"); 9452 verifyIndependentOfContext("if (int *a = (&b))"); 9453 verifyIndependentOfContext("while (int *a = &b)"); 9454 verifyIndependentOfContext("while (a * (b * c))"); 9455 verifyIndependentOfContext("size = sizeof *a;"); 9456 verifyIndependentOfContext("if (a && (b = c))"); 9457 verifyFormat("void f() {\n" 9458 " for (const int &v : Values) {\n" 9459 " }\n" 9460 "}"); 9461 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 9462 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 9463 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 9464 9465 verifyFormat("#define A (!a * b)"); 9466 verifyFormat("#define MACRO \\\n" 9467 " int *i = a * b; \\\n" 9468 " void f(a *b);", 9469 getLLVMStyleWithColumns(19)); 9470 9471 verifyIndependentOfContext("A = new SomeType *[Length];"); 9472 verifyIndependentOfContext("A = new SomeType *[Length]();"); 9473 verifyIndependentOfContext("T **t = new T *;"); 9474 verifyIndependentOfContext("T **t = new T *();"); 9475 verifyGoogleFormat("A = new SomeType*[Length]();"); 9476 verifyGoogleFormat("A = new SomeType*[Length];"); 9477 verifyGoogleFormat("T** t = new T*;"); 9478 verifyGoogleFormat("T** t = new T*();"); 9479 9480 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 9481 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 9482 verifyFormat("template <bool a, bool b> " 9483 "typename t::if<x && y>::type f() {}"); 9484 verifyFormat("template <int *y> f() {}"); 9485 verifyFormat("vector<int *> v;"); 9486 verifyFormat("vector<int *const> v;"); 9487 verifyFormat("vector<int *const **const *> v;"); 9488 verifyFormat("vector<int *volatile> v;"); 9489 verifyFormat("vector<a *_Nonnull> v;"); 9490 verifyFormat("vector<a *_Nullable> v;"); 9491 verifyFormat("vector<a *_Null_unspecified> v;"); 9492 verifyFormat("vector<a *__ptr32> v;"); 9493 verifyFormat("vector<a *__ptr64> v;"); 9494 verifyFormat("vector<a *__capability> v;"); 9495 FormatStyle TypeMacros = getLLVMStyle(); 9496 TypeMacros.TypenameMacros = {"LIST"}; 9497 verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros); 9498 verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros); 9499 verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros); 9500 verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros); 9501 verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros); // multiplication 9502 9503 FormatStyle CustomQualifier = getLLVMStyle(); 9504 // Add identifiers that should not be parsed as a qualifier by default. 9505 CustomQualifier.AttributeMacros.push_back("__my_qualifier"); 9506 CustomQualifier.AttributeMacros.push_back("_My_qualifier"); 9507 CustomQualifier.AttributeMacros.push_back("my_other_qualifier"); 9508 verifyFormat("vector<a * __my_qualifier> parse_as_multiply;"); 9509 verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier); 9510 verifyFormat("vector<a * _My_qualifier> parse_as_multiply;"); 9511 verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier); 9512 verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;"); 9513 verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier); 9514 verifyFormat("vector<a * _NotAQualifier> v;"); 9515 verifyFormat("vector<a * __not_a_qualifier> v;"); 9516 verifyFormat("vector<a * b> v;"); 9517 verifyFormat("foo<b && false>();"); 9518 verifyFormat("foo<b & 1>();"); 9519 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 9520 verifyFormat("typeof(*::std::declval<const T &>()) void F();"); 9521 verifyFormat("_Atomic(*::std::declval<const T &>()) void F();"); 9522 verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();"); 9523 verifyFormat( 9524 "template <class T, class = typename std::enable_if<\n" 9525 " std::is_integral<T>::value &&\n" 9526 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 9527 "void F();", 9528 getLLVMStyleWithColumns(70)); 9529 verifyFormat("template <class T,\n" 9530 " class = typename std::enable_if<\n" 9531 " std::is_integral<T>::value &&\n" 9532 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n" 9533 " class U>\n" 9534 "void F();", 9535 getLLVMStyleWithColumns(70)); 9536 verifyFormat( 9537 "template <class T,\n" 9538 " class = typename ::std::enable_if<\n" 9539 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 9540 "void F();", 9541 getGoogleStyleWithColumns(68)); 9542 9543 verifyIndependentOfContext("MACRO(int *i);"); 9544 verifyIndependentOfContext("MACRO(auto *a);"); 9545 verifyIndependentOfContext("MACRO(const A *a);"); 9546 verifyIndependentOfContext("MACRO(_Atomic(A) *a);"); 9547 verifyIndependentOfContext("MACRO(decltype(A) *a);"); 9548 verifyIndependentOfContext("MACRO(typeof(A) *a);"); 9549 verifyIndependentOfContext("MACRO(__underlying_type(A) *a);"); 9550 verifyIndependentOfContext("MACRO(A *const a);"); 9551 verifyIndependentOfContext("MACRO(A *restrict a);"); 9552 verifyIndependentOfContext("MACRO(A *__restrict__ a);"); 9553 verifyIndependentOfContext("MACRO(A *__restrict a);"); 9554 verifyIndependentOfContext("MACRO(A *volatile a);"); 9555 verifyIndependentOfContext("MACRO(A *__volatile a);"); 9556 verifyIndependentOfContext("MACRO(A *__volatile__ a);"); 9557 verifyIndependentOfContext("MACRO(A *_Nonnull a);"); 9558 verifyIndependentOfContext("MACRO(A *_Nullable a);"); 9559 verifyIndependentOfContext("MACRO(A *_Null_unspecified a);"); 9560 verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);"); 9561 verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);"); 9562 verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);"); 9563 verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);"); 9564 verifyIndependentOfContext("MACRO(A *__ptr32 a);"); 9565 verifyIndependentOfContext("MACRO(A *__ptr64 a);"); 9566 verifyIndependentOfContext("MACRO(A *__capability);"); 9567 verifyIndependentOfContext("MACRO(A &__capability);"); 9568 verifyFormat("MACRO(A *__my_qualifier);"); // type declaration 9569 verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication 9570 // If we add __my_qualifier to AttributeMacros it should always be parsed as 9571 // a type declaration: 9572 verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier); 9573 verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier); 9574 // Also check that TypenameMacros prevents parsing it as multiplication: 9575 verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication 9576 verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros); // type 9577 9578 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 9579 verifyFormat("void f() { f(float{1}, a * a); }"); 9580 verifyFormat("void f() { f(float(1), a * a); }"); 9581 9582 verifyFormat("f((void (*)(int))g);"); 9583 verifyFormat("f((void (&)(int))g);"); 9584 verifyFormat("f((void (^)(int))g);"); 9585 9586 // FIXME: Is there a way to make this work? 9587 // verifyIndependentOfContext("MACRO(A *a);"); 9588 verifyFormat("MACRO(A &B);"); 9589 verifyFormat("MACRO(A *B);"); 9590 verifyFormat("void f() { MACRO(A * B); }"); 9591 verifyFormat("void f() { MACRO(A & B); }"); 9592 9593 // This lambda was mis-formatted after D88956 (treating it as a binop): 9594 verifyFormat("auto x = [](const decltype(x) &ptr) {};"); 9595 verifyFormat("auto x = [](const decltype(x) *ptr) {};"); 9596 verifyFormat("#define lambda [](const decltype(x) &ptr) {}"); 9597 verifyFormat("#define lambda [](const decltype(x) *ptr) {}"); 9598 9599 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 9600 verifyFormat("return options != nullptr && operator==(*options);"); 9601 9602 EXPECT_EQ("#define OP(x) \\\n" 9603 " ostream &operator<<(ostream &s, const A &a) { \\\n" 9604 " return s << a.DebugString(); \\\n" 9605 " }", 9606 format("#define OP(x) \\\n" 9607 " ostream &operator<<(ostream &s, const A &a) { \\\n" 9608 " return s << a.DebugString(); \\\n" 9609 " }", 9610 getLLVMStyleWithColumns(50))); 9611 9612 // FIXME: We cannot handle this case yet; we might be able to figure out that 9613 // foo<x> d > v; doesn't make sense. 9614 verifyFormat("foo<a<b && c> d> v;"); 9615 9616 FormatStyle PointerMiddle = getLLVMStyle(); 9617 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 9618 verifyFormat("delete *x;", PointerMiddle); 9619 verifyFormat("int * x;", PointerMiddle); 9620 verifyFormat("int *[] x;", PointerMiddle); 9621 verifyFormat("template <int * y> f() {}", PointerMiddle); 9622 verifyFormat("int * f(int * a) {}", PointerMiddle); 9623 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 9624 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 9625 verifyFormat("A<int *> a;", PointerMiddle); 9626 verifyFormat("A<int **> a;", PointerMiddle); 9627 verifyFormat("A<int *, int *> a;", PointerMiddle); 9628 verifyFormat("A<int *[]> a;", PointerMiddle); 9629 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 9630 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 9631 verifyFormat("T ** t = new T *;", PointerMiddle); 9632 9633 // Member function reference qualifiers aren't binary operators. 9634 verifyFormat("string // break\n" 9635 "operator()() & {}"); 9636 verifyFormat("string // break\n" 9637 "operator()() && {}"); 9638 verifyGoogleFormat("template <typename T>\n" 9639 "auto x() & -> int {}"); 9640 9641 // Should be binary operators when used as an argument expression (overloaded 9642 // operator invoked as a member function). 9643 verifyFormat("void f() { a.operator()(a * a); }"); 9644 verifyFormat("void f() { a->operator()(a & a); }"); 9645 verifyFormat("void f() { a.operator()(*a & *a); }"); 9646 verifyFormat("void f() { a->operator()(*a * *a); }"); 9647 } 9648 9649 TEST_F(FormatTest, UnderstandsAttributes) { 9650 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 9651 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 9652 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 9653 FormatStyle AfterType = getLLVMStyle(); 9654 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 9655 verifyFormat("__attribute__((nodebug)) void\n" 9656 "foo() {}\n", 9657 AfterType); 9658 verifyFormat("__unused void\n" 9659 "foo() {}", 9660 AfterType); 9661 9662 FormatStyle CustomAttrs = getLLVMStyle(); 9663 CustomAttrs.AttributeMacros.push_back("__unused"); 9664 CustomAttrs.AttributeMacros.push_back("__attr1"); 9665 CustomAttrs.AttributeMacros.push_back("__attr2"); 9666 CustomAttrs.AttributeMacros.push_back("no_underscore_attr"); 9667 verifyFormat("vector<SomeType *__attribute((foo))> v;"); 9668 verifyFormat("vector<SomeType *__attribute__((foo))> v;"); 9669 verifyFormat("vector<SomeType * __not_attribute__((foo))> v;"); 9670 // Check that it is parsed as a multiplication without AttributeMacros and 9671 // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros. 9672 verifyFormat("vector<SomeType * __attr1> v;"); 9673 verifyFormat("vector<SomeType __attr1 *> v;"); 9674 verifyFormat("vector<SomeType __attr1 *const> v;"); 9675 verifyFormat("vector<SomeType __attr1 * __attr2> v;"); 9676 verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs); 9677 verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs); 9678 verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs); 9679 verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs); 9680 verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs); 9681 verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs); 9682 verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs); 9683 9684 // Check that these are not parsed as function declarations: 9685 CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 9686 CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman; 9687 verifyFormat("SomeType s(InitValue);", CustomAttrs); 9688 verifyFormat("SomeType s{InitValue};", CustomAttrs); 9689 verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs); 9690 verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs); 9691 verifyFormat("SomeType s __unused(InitValue);", CustomAttrs); 9692 verifyFormat("SomeType s __unused{InitValue};", CustomAttrs); 9693 verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs); 9694 verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs); 9695 } 9696 9697 TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) { 9698 // Check that qualifiers on pointers don't break parsing of casts. 9699 verifyFormat("x = (foo *const)*v;"); 9700 verifyFormat("x = (foo *volatile)*v;"); 9701 verifyFormat("x = (foo *restrict)*v;"); 9702 verifyFormat("x = (foo *__attribute__((foo)))*v;"); 9703 verifyFormat("x = (foo *_Nonnull)*v;"); 9704 verifyFormat("x = (foo *_Nullable)*v;"); 9705 verifyFormat("x = (foo *_Null_unspecified)*v;"); 9706 verifyFormat("x = (foo *_Nonnull)*v;"); 9707 verifyFormat("x = (foo *[[clang::attr]])*v;"); 9708 verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;"); 9709 verifyFormat("x = (foo *__ptr32)*v;"); 9710 verifyFormat("x = (foo *__ptr64)*v;"); 9711 verifyFormat("x = (foo *__capability)*v;"); 9712 9713 // Check that we handle multiple trailing qualifiers and skip them all to 9714 // determine that the expression is a cast to a pointer type. 9715 FormatStyle LongPointerRight = getLLVMStyleWithColumns(999); 9716 FormatStyle LongPointerLeft = getLLVMStyleWithColumns(999); 9717 LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left; 9718 StringRef AllQualifiers = 9719 "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified " 9720 "_Nonnull [[clang::attr]] __ptr32 __ptr64 __capability"; 9721 verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight); 9722 verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft); 9723 9724 // Also check that address-of is not parsed as a binary bitwise-and: 9725 verifyFormat("x = (foo *const)&v;"); 9726 verifyFormat(("x = (foo *" + AllQualifiers + ")&v;").str(), LongPointerRight); 9727 verifyFormat(("x = (foo* " + AllQualifiers + ")&v;").str(), LongPointerLeft); 9728 9729 // Check custom qualifiers: 9730 FormatStyle CustomQualifier = getLLVMStyleWithColumns(999); 9731 CustomQualifier.AttributeMacros.push_back("__my_qualifier"); 9732 verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier. 9733 verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier); 9734 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)*v;").str(), 9735 CustomQualifier); 9736 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)&v;").str(), 9737 CustomQualifier); 9738 9739 // Check that unknown identifiers result in binary operator parsing: 9740 verifyFormat("x = (foo * __unknown_qualifier) * v;"); 9741 verifyFormat("x = (foo * __unknown_qualifier) & v;"); 9742 } 9743 9744 TEST_F(FormatTest, UnderstandsSquareAttributes) { 9745 verifyFormat("SomeType s [[unused]] (InitValue);"); 9746 verifyFormat("SomeType s [[gnu::unused]] (InitValue);"); 9747 verifyFormat("SomeType s [[using gnu: unused]] (InitValue);"); 9748 verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}"); 9749 verifyFormat("void f() [[deprecated(\"so sorry\")]];"); 9750 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9751 " [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 9752 verifyFormat("[[nodiscard]] bool f() { return false; }"); 9753 verifyFormat("class [[nodiscard]] f {\npublic:\n f() {}\n}"); 9754 verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n f() {}\n}"); 9755 verifyFormat("class [[gnu::unused]] f {\npublic:\n f() {}\n}"); 9756 9757 // Make sure we do not mistake attributes for array subscripts. 9758 verifyFormat("int a() {}\n" 9759 "[[unused]] int b() {}\n"); 9760 verifyFormat("NSArray *arr;\n" 9761 "arr[[Foo() bar]];"); 9762 9763 // On the other hand, we still need to correctly find array subscripts. 9764 verifyFormat("int a = std::vector<int>{1, 2, 3}[0];"); 9765 9766 // Make sure that we do not mistake Objective-C method inside array literals 9767 // as attributes, even if those method names are also keywords. 9768 verifyFormat("@[ [foo bar] ];"); 9769 verifyFormat("@[ [NSArray class] ];"); 9770 verifyFormat("@[ [foo enum] ];"); 9771 9772 verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }"); 9773 9774 // Make sure we do not parse attributes as lambda introducers. 9775 FormatStyle MultiLineFunctions = getLLVMStyle(); 9776 MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 9777 verifyFormat("[[unused]] int b() {\n" 9778 " return 42;\n" 9779 "}\n", 9780 MultiLineFunctions); 9781 } 9782 9783 TEST_F(FormatTest, AttributeClass) { 9784 FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp); 9785 verifyFormat("class S {\n" 9786 " S(S&&) = default;\n" 9787 "};", 9788 Style); 9789 verifyFormat("class [[nodiscard]] S {\n" 9790 " S(S&&) = default;\n" 9791 "};", 9792 Style); 9793 verifyFormat("class __attribute((maybeunused)) S {\n" 9794 " S(S&&) = default;\n" 9795 "};", 9796 Style); 9797 verifyFormat("struct S {\n" 9798 " S(S&&) = default;\n" 9799 "};", 9800 Style); 9801 verifyFormat("struct [[nodiscard]] S {\n" 9802 " S(S&&) = default;\n" 9803 "};", 9804 Style); 9805 } 9806 9807 TEST_F(FormatTest, AttributesAfterMacro) { 9808 FormatStyle Style = getLLVMStyle(); 9809 verifyFormat("MACRO;\n" 9810 "__attribute__((maybe_unused)) int foo() {\n" 9811 " //...\n" 9812 "}"); 9813 9814 verifyFormat("MACRO;\n" 9815 "[[nodiscard]] int foo() {\n" 9816 " //...\n" 9817 "}"); 9818 9819 EXPECT_EQ("MACRO\n\n" 9820 "__attribute__((maybe_unused)) int foo() {\n" 9821 " //...\n" 9822 "}", 9823 format("MACRO\n\n" 9824 "__attribute__((maybe_unused)) int foo() {\n" 9825 " //...\n" 9826 "}")); 9827 9828 EXPECT_EQ("MACRO\n\n" 9829 "[[nodiscard]] int foo() {\n" 9830 " //...\n" 9831 "}", 9832 format("MACRO\n\n" 9833 "[[nodiscard]] int foo() {\n" 9834 " //...\n" 9835 "}")); 9836 } 9837 9838 TEST_F(FormatTest, AttributePenaltyBreaking) { 9839 FormatStyle Style = getLLVMStyle(); 9840 verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n" 9841 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", 9842 Style); 9843 verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n" 9844 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", 9845 Style); 9846 verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const " 9847 "shared_ptr<ALongTypeName> &C d) {\n}", 9848 Style); 9849 } 9850 9851 TEST_F(FormatTest, UnderstandsEllipsis) { 9852 FormatStyle Style = getLLVMStyle(); 9853 verifyFormat("int printf(const char *fmt, ...);"); 9854 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 9855 verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}"); 9856 9857 verifyFormat("template <int *...PP> a;", Style); 9858 9859 Style.PointerAlignment = FormatStyle::PAS_Left; 9860 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style); 9861 9862 verifyFormat("template <int*... PP> a;", Style); 9863 9864 Style.PointerAlignment = FormatStyle::PAS_Middle; 9865 verifyFormat("template <int *... PP> a;", Style); 9866 } 9867 9868 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 9869 EXPECT_EQ("int *a;\n" 9870 "int *a;\n" 9871 "int *a;", 9872 format("int *a;\n" 9873 "int* a;\n" 9874 "int *a;", 9875 getGoogleStyle())); 9876 EXPECT_EQ("int* a;\n" 9877 "int* a;\n" 9878 "int* a;", 9879 format("int* a;\n" 9880 "int* a;\n" 9881 "int *a;", 9882 getGoogleStyle())); 9883 EXPECT_EQ("int *a;\n" 9884 "int *a;\n" 9885 "int *a;", 9886 format("int *a;\n" 9887 "int * a;\n" 9888 "int * a;", 9889 getGoogleStyle())); 9890 EXPECT_EQ("auto x = [] {\n" 9891 " int *a;\n" 9892 " int *a;\n" 9893 " int *a;\n" 9894 "};", 9895 format("auto x=[]{int *a;\n" 9896 "int * a;\n" 9897 "int * a;};", 9898 getGoogleStyle())); 9899 } 9900 9901 TEST_F(FormatTest, UnderstandsRvalueReferences) { 9902 verifyFormat("int f(int &&a) {}"); 9903 verifyFormat("int f(int a, char &&b) {}"); 9904 verifyFormat("void f() { int &&a = b; }"); 9905 verifyGoogleFormat("int f(int a, char&& b) {}"); 9906 verifyGoogleFormat("void f() { int&& a = b; }"); 9907 9908 verifyIndependentOfContext("A<int &&> a;"); 9909 verifyIndependentOfContext("A<int &&, int &&> a;"); 9910 verifyGoogleFormat("A<int&&> a;"); 9911 verifyGoogleFormat("A<int&&, int&&> a;"); 9912 9913 // Not rvalue references: 9914 verifyFormat("template <bool B, bool C> class A {\n" 9915 " static_assert(B && C, \"Something is wrong\");\n" 9916 "};"); 9917 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 9918 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 9919 verifyFormat("#define A(a, b) (a && b)"); 9920 } 9921 9922 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 9923 verifyFormat("void f() {\n" 9924 " x[aaaaaaaaa -\n" 9925 " b] = 23;\n" 9926 "}", 9927 getLLVMStyleWithColumns(15)); 9928 } 9929 9930 TEST_F(FormatTest, FormatsCasts) { 9931 verifyFormat("Type *A = static_cast<Type *>(P);"); 9932 verifyFormat("Type *A = (Type *)P;"); 9933 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 9934 verifyFormat("int a = (int)(2.0f);"); 9935 verifyFormat("int a = (int)2.0f;"); 9936 verifyFormat("x[(int32)y];"); 9937 verifyFormat("x = (int32)y;"); 9938 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 9939 verifyFormat("int a = (int)*b;"); 9940 verifyFormat("int a = (int)2.0f;"); 9941 verifyFormat("int a = (int)~0;"); 9942 verifyFormat("int a = (int)++a;"); 9943 verifyFormat("int a = (int)sizeof(int);"); 9944 verifyFormat("int a = (int)+2;"); 9945 verifyFormat("my_int a = (my_int)2.0f;"); 9946 verifyFormat("my_int a = (my_int)sizeof(int);"); 9947 verifyFormat("return (my_int)aaa;"); 9948 verifyFormat("#define x ((int)-1)"); 9949 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 9950 verifyFormat("#define p(q) ((int *)&q)"); 9951 verifyFormat("fn(a)(b) + 1;"); 9952 9953 verifyFormat("void f() { my_int a = (my_int)*b; }"); 9954 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 9955 verifyFormat("my_int a = (my_int)~0;"); 9956 verifyFormat("my_int a = (my_int)++a;"); 9957 verifyFormat("my_int a = (my_int)-2;"); 9958 verifyFormat("my_int a = (my_int)1;"); 9959 verifyFormat("my_int a = (my_int *)1;"); 9960 verifyFormat("my_int a = (const my_int)-1;"); 9961 verifyFormat("my_int a = (const my_int *)-1;"); 9962 verifyFormat("my_int a = (my_int)(my_int)-1;"); 9963 verifyFormat("my_int a = (ns::my_int)-2;"); 9964 verifyFormat("case (my_int)ONE:"); 9965 verifyFormat("auto x = (X)this;"); 9966 // Casts in Obj-C style calls used to not be recognized as such. 9967 verifyFormat("int a = [(type*)[((type*)val) arg] arg];", getGoogleStyle()); 9968 9969 // FIXME: single value wrapped with paren will be treated as cast. 9970 verifyFormat("void f(int i = (kValue)*kMask) {}"); 9971 9972 verifyFormat("{ (void)F; }"); 9973 9974 // Don't break after a cast's 9975 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 9976 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 9977 " bbbbbbbbbbbbbbbbbbbbbb);"); 9978 9979 // These are not casts. 9980 verifyFormat("void f(int *) {}"); 9981 verifyFormat("f(foo)->b;"); 9982 verifyFormat("f(foo).b;"); 9983 verifyFormat("f(foo)(b);"); 9984 verifyFormat("f(foo)[b];"); 9985 verifyFormat("[](foo) { return 4; }(bar);"); 9986 verifyFormat("(*funptr)(foo)[4];"); 9987 verifyFormat("funptrs[4](foo)[4];"); 9988 verifyFormat("void f(int *);"); 9989 verifyFormat("void f(int *) = 0;"); 9990 verifyFormat("void f(SmallVector<int>) {}"); 9991 verifyFormat("void f(SmallVector<int>);"); 9992 verifyFormat("void f(SmallVector<int>) = 0;"); 9993 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 9994 verifyFormat("int a = sizeof(int) * b;"); 9995 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 9996 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 9997 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 9998 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 9999 10000 // These are not casts, but at some point were confused with casts. 10001 verifyFormat("virtual void foo(int *) override;"); 10002 verifyFormat("virtual void foo(char &) const;"); 10003 verifyFormat("virtual void foo(int *a, char *) const;"); 10004 verifyFormat("int a = sizeof(int *) + b;"); 10005 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 10006 verifyFormat("bool b = f(g<int>) && c;"); 10007 verifyFormat("typedef void (*f)(int i) func;"); 10008 verifyFormat("void operator++(int) noexcept;"); 10009 verifyFormat("void operator++(int &) noexcept;"); 10010 verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t " 10011 "&) noexcept;"); 10012 verifyFormat( 10013 "void operator delete(std::size_t, const std::nothrow_t &) noexcept;"); 10014 verifyFormat("void operator delete(const std::nothrow_t &) noexcept;"); 10015 verifyFormat("void operator delete(std::nothrow_t &) noexcept;"); 10016 verifyFormat("void operator delete(nothrow_t &) noexcept;"); 10017 verifyFormat("void operator delete(foo &) noexcept;"); 10018 verifyFormat("void operator delete(foo) noexcept;"); 10019 verifyFormat("void operator delete(int) noexcept;"); 10020 verifyFormat("void operator delete(int &) noexcept;"); 10021 verifyFormat("void operator delete(int &) volatile noexcept;"); 10022 verifyFormat("void operator delete(int &) const"); 10023 verifyFormat("void operator delete(int &) = default"); 10024 verifyFormat("void operator delete(int &) = delete"); 10025 verifyFormat("void operator delete(int &) [[noreturn]]"); 10026 verifyFormat("void operator delete(int &) throw();"); 10027 verifyFormat("void operator delete(int &) throw(int);"); 10028 verifyFormat("auto operator delete(int &) -> int;"); 10029 verifyFormat("auto operator delete(int &) override"); 10030 verifyFormat("auto operator delete(int &) final"); 10031 10032 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 10033 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 10034 // FIXME: The indentation here is not ideal. 10035 verifyFormat( 10036 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10037 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 10038 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 10039 } 10040 10041 TEST_F(FormatTest, FormatsFunctionTypes) { 10042 verifyFormat("A<bool()> a;"); 10043 verifyFormat("A<SomeType()> a;"); 10044 verifyFormat("A<void (*)(int, std::string)> a;"); 10045 verifyFormat("A<void *(int)>;"); 10046 verifyFormat("void *(*a)(int *, SomeType *);"); 10047 verifyFormat("int (*func)(void *);"); 10048 verifyFormat("void f() { int (*func)(void *); }"); 10049 verifyFormat("template <class CallbackClass>\n" 10050 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 10051 10052 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 10053 verifyGoogleFormat("void* (*a)(int);"); 10054 verifyGoogleFormat( 10055 "template <class CallbackClass>\n" 10056 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 10057 10058 // Other constructs can look somewhat like function types: 10059 verifyFormat("A<sizeof(*x)> a;"); 10060 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 10061 verifyFormat("some_var = function(*some_pointer_var)[0];"); 10062 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 10063 verifyFormat("int x = f(&h)();"); 10064 verifyFormat("returnsFunction(¶m1, ¶m2)(param);"); 10065 verifyFormat("std::function<\n" 10066 " LooooooooooongTemplatedType<\n" 10067 " SomeType>*(\n" 10068 " LooooooooooooooooongType type)>\n" 10069 " function;", 10070 getGoogleStyleWithColumns(40)); 10071 } 10072 10073 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 10074 verifyFormat("A (*foo_)[6];"); 10075 verifyFormat("vector<int> (*foo_)[6];"); 10076 } 10077 10078 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 10079 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10080 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 10081 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 10082 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 10083 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10084 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 10085 10086 // Different ways of ()-initializiation. 10087 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10088 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 10089 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10090 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 10091 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10092 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 10093 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10094 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 10095 10096 // Lambdas should not confuse the variable declaration heuristic. 10097 verifyFormat("LooooooooooooooooongType\n" 10098 " variable(nullptr, [](A *a) {});", 10099 getLLVMStyleWithColumns(40)); 10100 } 10101 10102 TEST_F(FormatTest, BreaksLongDeclarations) { 10103 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 10104 " AnotherNameForTheLongType;"); 10105 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 10106 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 10107 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10108 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 10109 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 10110 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 10111 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10112 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10113 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 10114 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10115 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 10116 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10117 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 10118 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10119 verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n" 10120 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10121 verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n" 10122 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10123 verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n" 10124 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10125 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10126 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);"); 10127 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10128 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}"); 10129 FormatStyle Indented = getLLVMStyle(); 10130 Indented.IndentWrappedFunctionNames = true; 10131 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10132 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 10133 Indented); 10134 verifyFormat( 10135 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10136 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 10137 Indented); 10138 verifyFormat( 10139 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 10140 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 10141 Indented); 10142 verifyFormat( 10143 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 10144 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 10145 Indented); 10146 10147 // FIXME: Without the comment, this breaks after "(". 10148 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 10149 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 10150 getGoogleStyle()); 10151 10152 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 10153 " int LoooooooooooooooooooongParam2) {}"); 10154 verifyFormat( 10155 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 10156 " SourceLocation L, IdentifierIn *II,\n" 10157 " Type *T) {}"); 10158 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 10159 "ReallyReaaallyLongFunctionName(\n" 10160 " const std::string &SomeParameter,\n" 10161 " const SomeType<string, SomeOtherTemplateParameter>\n" 10162 " &ReallyReallyLongParameterName,\n" 10163 " const SomeType<string, SomeOtherTemplateParameter>\n" 10164 " &AnotherLongParameterName) {}"); 10165 verifyFormat("template <typename A>\n" 10166 "SomeLoooooooooooooooooooooongType<\n" 10167 " typename some_namespace::SomeOtherType<A>::Type>\n" 10168 "Function() {}"); 10169 10170 verifyGoogleFormat( 10171 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 10172 " aaaaaaaaaaaaaaaaaaaaaaa;"); 10173 verifyGoogleFormat( 10174 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 10175 " SourceLocation L) {}"); 10176 verifyGoogleFormat( 10177 "some_namespace::LongReturnType\n" 10178 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 10179 " int first_long_parameter, int second_parameter) {}"); 10180 10181 verifyGoogleFormat("template <typename T>\n" 10182 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 10183 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 10184 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 10185 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 10186 10187 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 10188 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10189 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 10190 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10191 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 10192 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 10193 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10194 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 10195 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 10196 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 10197 10198 verifyFormat("template <typename T> // Templates on own line.\n" 10199 "static int // Some comment.\n" 10200 "MyFunction(int a);", 10201 getLLVMStyle()); 10202 } 10203 10204 TEST_F(FormatTest, FormatsAccessModifiers) { 10205 FormatStyle Style = getLLVMStyle(); 10206 EXPECT_EQ(Style.EmptyLineBeforeAccessModifier, 10207 FormatStyle::ELBAMS_LogicalBlock); 10208 verifyFormat("struct foo {\n" 10209 "private:\n" 10210 " void f() {}\n" 10211 "\n" 10212 "private:\n" 10213 " int i;\n" 10214 "\n" 10215 "protected:\n" 10216 " int j;\n" 10217 "};\n", 10218 Style); 10219 verifyFormat("struct foo {\n" 10220 "private:\n" 10221 " void f() {}\n" 10222 "\n" 10223 "private:\n" 10224 " int i;\n" 10225 "\n" 10226 "protected:\n" 10227 " int j;\n" 10228 "};\n", 10229 "struct foo {\n" 10230 "private:\n" 10231 " void f() {}\n" 10232 "private:\n" 10233 " int i;\n" 10234 "protected:\n" 10235 " int j;\n" 10236 "};\n", 10237 Style); 10238 verifyFormat("struct foo { /* comment */\n" 10239 "private:\n" 10240 " int i;\n" 10241 " // comment\n" 10242 "private:\n" 10243 " int j;\n" 10244 "};\n", 10245 Style); 10246 verifyFormat("struct foo {\n" 10247 "#ifdef FOO\n" 10248 "#endif\n" 10249 "private:\n" 10250 " int i;\n" 10251 "#ifdef FOO\n" 10252 "private:\n" 10253 "#endif\n" 10254 " int j;\n" 10255 "};\n", 10256 Style); 10257 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10258 verifyFormat("struct foo {\n" 10259 "private:\n" 10260 " void f() {}\n" 10261 "private:\n" 10262 " int i;\n" 10263 "protected:\n" 10264 " int j;\n" 10265 "};\n", 10266 Style); 10267 verifyFormat("struct foo {\n" 10268 "private:\n" 10269 " void f() {}\n" 10270 "private:\n" 10271 " int i;\n" 10272 "protected:\n" 10273 " int j;\n" 10274 "};\n", 10275 "struct foo {\n" 10276 "\n" 10277 "private:\n" 10278 " void f() {}\n" 10279 "\n" 10280 "private:\n" 10281 " int i;\n" 10282 "\n" 10283 "protected:\n" 10284 " int j;\n" 10285 "};\n", 10286 Style); 10287 verifyFormat("struct foo { /* comment */\n" 10288 "private:\n" 10289 " int i;\n" 10290 " // comment\n" 10291 "private:\n" 10292 " int j;\n" 10293 "};\n", 10294 "struct foo { /* comment */\n" 10295 "\n" 10296 "private:\n" 10297 " int i;\n" 10298 " // comment\n" 10299 "\n" 10300 "private:\n" 10301 " int j;\n" 10302 "};\n", 10303 Style); 10304 verifyFormat("struct foo {\n" 10305 "#ifdef FOO\n" 10306 "#endif\n" 10307 "private:\n" 10308 " int i;\n" 10309 "#ifdef FOO\n" 10310 "private:\n" 10311 "#endif\n" 10312 " int j;\n" 10313 "};\n", 10314 "struct foo {\n" 10315 "#ifdef FOO\n" 10316 "#endif\n" 10317 "\n" 10318 "private:\n" 10319 " int i;\n" 10320 "#ifdef FOO\n" 10321 "\n" 10322 "private:\n" 10323 "#endif\n" 10324 " int j;\n" 10325 "};\n", 10326 Style); 10327 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10328 verifyFormat("struct foo {\n" 10329 "private:\n" 10330 " void f() {}\n" 10331 "\n" 10332 "private:\n" 10333 " int i;\n" 10334 "\n" 10335 "protected:\n" 10336 " int j;\n" 10337 "};\n", 10338 Style); 10339 verifyFormat("struct foo {\n" 10340 "private:\n" 10341 " void f() {}\n" 10342 "\n" 10343 "private:\n" 10344 " int i;\n" 10345 "\n" 10346 "protected:\n" 10347 " int j;\n" 10348 "};\n", 10349 "struct foo {\n" 10350 "private:\n" 10351 " void f() {}\n" 10352 "private:\n" 10353 " int i;\n" 10354 "protected:\n" 10355 " int j;\n" 10356 "};\n", 10357 Style); 10358 verifyFormat("struct foo { /* comment */\n" 10359 "private:\n" 10360 " int i;\n" 10361 " // comment\n" 10362 "\n" 10363 "private:\n" 10364 " int j;\n" 10365 "};\n", 10366 "struct foo { /* comment */\n" 10367 "private:\n" 10368 " int i;\n" 10369 " // comment\n" 10370 "\n" 10371 "private:\n" 10372 " int j;\n" 10373 "};\n", 10374 Style); 10375 verifyFormat("struct foo {\n" 10376 "#ifdef FOO\n" 10377 "#endif\n" 10378 "\n" 10379 "private:\n" 10380 " int i;\n" 10381 "#ifdef FOO\n" 10382 "\n" 10383 "private:\n" 10384 "#endif\n" 10385 " int j;\n" 10386 "};\n", 10387 "struct foo {\n" 10388 "#ifdef FOO\n" 10389 "#endif\n" 10390 "private:\n" 10391 " int i;\n" 10392 "#ifdef FOO\n" 10393 "private:\n" 10394 "#endif\n" 10395 " int j;\n" 10396 "};\n", 10397 Style); 10398 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 10399 EXPECT_EQ("struct foo {\n" 10400 "\n" 10401 "private:\n" 10402 " void f() {}\n" 10403 "\n" 10404 "private:\n" 10405 " int i;\n" 10406 "\n" 10407 "protected:\n" 10408 " int j;\n" 10409 "};\n", 10410 format("struct foo {\n" 10411 "\n" 10412 "private:\n" 10413 " void f() {}\n" 10414 "\n" 10415 "private:\n" 10416 " int i;\n" 10417 "\n" 10418 "protected:\n" 10419 " int j;\n" 10420 "};\n", 10421 Style)); 10422 verifyFormat("struct foo {\n" 10423 "private:\n" 10424 " void f() {}\n" 10425 "private:\n" 10426 " int i;\n" 10427 "protected:\n" 10428 " int j;\n" 10429 "};\n", 10430 Style); 10431 EXPECT_EQ("struct foo { /* comment */\n" 10432 "\n" 10433 "private:\n" 10434 " int i;\n" 10435 " // comment\n" 10436 "\n" 10437 "private:\n" 10438 " int j;\n" 10439 "};\n", 10440 format("struct foo { /* comment */\n" 10441 "\n" 10442 "private:\n" 10443 " int i;\n" 10444 " // comment\n" 10445 "\n" 10446 "private:\n" 10447 " int j;\n" 10448 "};\n", 10449 Style)); 10450 verifyFormat("struct foo { /* comment */\n" 10451 "private:\n" 10452 " int i;\n" 10453 " // comment\n" 10454 "private:\n" 10455 " int j;\n" 10456 "};\n", 10457 Style); 10458 EXPECT_EQ("struct foo {\n" 10459 "#ifdef FOO\n" 10460 "#endif\n" 10461 "\n" 10462 "private:\n" 10463 " int i;\n" 10464 "#ifdef FOO\n" 10465 "\n" 10466 "private:\n" 10467 "#endif\n" 10468 " int j;\n" 10469 "};\n", 10470 format("struct foo {\n" 10471 "#ifdef FOO\n" 10472 "#endif\n" 10473 "\n" 10474 "private:\n" 10475 " int i;\n" 10476 "#ifdef FOO\n" 10477 "\n" 10478 "private:\n" 10479 "#endif\n" 10480 " int j;\n" 10481 "};\n", 10482 Style)); 10483 verifyFormat("struct foo {\n" 10484 "#ifdef FOO\n" 10485 "#endif\n" 10486 "private:\n" 10487 " int i;\n" 10488 "#ifdef FOO\n" 10489 "private:\n" 10490 "#endif\n" 10491 " int j;\n" 10492 "};\n", 10493 Style); 10494 10495 FormatStyle NoEmptyLines = getLLVMStyle(); 10496 NoEmptyLines.MaxEmptyLinesToKeep = 0; 10497 verifyFormat("struct foo {\n" 10498 "private:\n" 10499 " void f() {}\n" 10500 "\n" 10501 "private:\n" 10502 " int i;\n" 10503 "\n" 10504 "public:\n" 10505 "protected:\n" 10506 " int j;\n" 10507 "};\n", 10508 NoEmptyLines); 10509 10510 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10511 verifyFormat("struct foo {\n" 10512 "private:\n" 10513 " void f() {}\n" 10514 "private:\n" 10515 " int i;\n" 10516 "public:\n" 10517 "protected:\n" 10518 " int j;\n" 10519 "};\n", 10520 NoEmptyLines); 10521 10522 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10523 verifyFormat("struct foo {\n" 10524 "private:\n" 10525 " void f() {}\n" 10526 "\n" 10527 "private:\n" 10528 " int i;\n" 10529 "\n" 10530 "public:\n" 10531 "\n" 10532 "protected:\n" 10533 " int j;\n" 10534 "};\n", 10535 NoEmptyLines); 10536 } 10537 10538 TEST_F(FormatTest, FormatsAfterAccessModifiers) { 10539 10540 FormatStyle Style = getLLVMStyle(); 10541 EXPECT_EQ(Style.EmptyLineAfterAccessModifier, FormatStyle::ELAAMS_Never); 10542 verifyFormat("struct foo {\n" 10543 "private:\n" 10544 " void f() {}\n" 10545 "\n" 10546 "private:\n" 10547 " int i;\n" 10548 "\n" 10549 "protected:\n" 10550 " int j;\n" 10551 "};\n", 10552 Style); 10553 10554 // Check if lines are removed. 10555 verifyFormat("struct foo {\n" 10556 "private:\n" 10557 " void f() {}\n" 10558 "\n" 10559 "private:\n" 10560 " int i;\n" 10561 "\n" 10562 "protected:\n" 10563 " int j;\n" 10564 "};\n", 10565 "struct foo {\n" 10566 "private:\n" 10567 "\n" 10568 " void f() {}\n" 10569 "\n" 10570 "private:\n" 10571 "\n" 10572 " int i;\n" 10573 "\n" 10574 "protected:\n" 10575 "\n" 10576 " int j;\n" 10577 "};\n", 10578 Style); 10579 10580 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10581 verifyFormat("struct foo {\n" 10582 "private:\n" 10583 "\n" 10584 " void f() {}\n" 10585 "\n" 10586 "private:\n" 10587 "\n" 10588 " int i;\n" 10589 "\n" 10590 "protected:\n" 10591 "\n" 10592 " int j;\n" 10593 "};\n", 10594 Style); 10595 10596 // Check if lines are added. 10597 verifyFormat("struct foo {\n" 10598 "private:\n" 10599 "\n" 10600 " void f() {}\n" 10601 "\n" 10602 "private:\n" 10603 "\n" 10604 " int i;\n" 10605 "\n" 10606 "protected:\n" 10607 "\n" 10608 " int j;\n" 10609 "};\n", 10610 "struct foo {\n" 10611 "private:\n" 10612 " void f() {}\n" 10613 "\n" 10614 "private:\n" 10615 " int i;\n" 10616 "\n" 10617 "protected:\n" 10618 " int j;\n" 10619 "};\n", 10620 Style); 10621 10622 // Leave tests rely on the code layout, test::messUp can not be used. 10623 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 10624 Style.MaxEmptyLinesToKeep = 0u; 10625 verifyFormat("struct foo {\n" 10626 "private:\n" 10627 " void f() {}\n" 10628 "\n" 10629 "private:\n" 10630 " int i;\n" 10631 "\n" 10632 "protected:\n" 10633 " int j;\n" 10634 "};\n", 10635 Style); 10636 10637 // Check if MaxEmptyLinesToKeep is respected. 10638 EXPECT_EQ("struct foo {\n" 10639 "private:\n" 10640 " void f() {}\n" 10641 "\n" 10642 "private:\n" 10643 " int i;\n" 10644 "\n" 10645 "protected:\n" 10646 " int j;\n" 10647 "};\n", 10648 format("struct foo {\n" 10649 "private:\n" 10650 "\n\n\n" 10651 " void f() {}\n" 10652 "\n" 10653 "private:\n" 10654 "\n\n\n" 10655 " int i;\n" 10656 "\n" 10657 "protected:\n" 10658 "\n\n\n" 10659 " int j;\n" 10660 "};\n", 10661 Style)); 10662 10663 Style.MaxEmptyLinesToKeep = 1u; 10664 EXPECT_EQ("struct foo {\n" 10665 "private:\n" 10666 "\n" 10667 " void f() {}\n" 10668 "\n" 10669 "private:\n" 10670 "\n" 10671 " int i;\n" 10672 "\n" 10673 "protected:\n" 10674 "\n" 10675 " int j;\n" 10676 "};\n", 10677 format("struct foo {\n" 10678 "private:\n" 10679 "\n" 10680 " void f() {}\n" 10681 "\n" 10682 "private:\n" 10683 "\n" 10684 " int i;\n" 10685 "\n" 10686 "protected:\n" 10687 "\n" 10688 " int j;\n" 10689 "};\n", 10690 Style)); 10691 // Check if no lines are kept. 10692 EXPECT_EQ("struct foo {\n" 10693 "private:\n" 10694 " void f() {}\n" 10695 "\n" 10696 "private:\n" 10697 " int i;\n" 10698 "\n" 10699 "protected:\n" 10700 " int j;\n" 10701 "};\n", 10702 format("struct foo {\n" 10703 "private:\n" 10704 " void f() {}\n" 10705 "\n" 10706 "private:\n" 10707 " int i;\n" 10708 "\n" 10709 "protected:\n" 10710 " int j;\n" 10711 "};\n", 10712 Style)); 10713 // Check if MaxEmptyLinesToKeep is respected. 10714 EXPECT_EQ("struct foo {\n" 10715 "private:\n" 10716 "\n" 10717 " void f() {}\n" 10718 "\n" 10719 "private:\n" 10720 "\n" 10721 " int i;\n" 10722 "\n" 10723 "protected:\n" 10724 "\n" 10725 " int j;\n" 10726 "};\n", 10727 format("struct foo {\n" 10728 "private:\n" 10729 "\n\n\n" 10730 " void f() {}\n" 10731 "\n" 10732 "private:\n" 10733 "\n\n\n" 10734 " int i;\n" 10735 "\n" 10736 "protected:\n" 10737 "\n\n\n" 10738 " int j;\n" 10739 "};\n", 10740 Style)); 10741 10742 Style.MaxEmptyLinesToKeep = 10u; 10743 EXPECT_EQ("struct foo {\n" 10744 "private:\n" 10745 "\n\n\n" 10746 " void f() {}\n" 10747 "\n" 10748 "private:\n" 10749 "\n\n\n" 10750 " int i;\n" 10751 "\n" 10752 "protected:\n" 10753 "\n\n\n" 10754 " int j;\n" 10755 "};\n", 10756 format("struct foo {\n" 10757 "private:\n" 10758 "\n\n\n" 10759 " void f() {}\n" 10760 "\n" 10761 "private:\n" 10762 "\n\n\n" 10763 " int i;\n" 10764 "\n" 10765 "protected:\n" 10766 "\n\n\n" 10767 " int j;\n" 10768 "};\n", 10769 Style)); 10770 10771 // Test with comments. 10772 Style = getLLVMStyle(); 10773 verifyFormat("struct foo {\n" 10774 "private:\n" 10775 " // comment\n" 10776 " void f() {}\n" 10777 "\n" 10778 "private: /* comment */\n" 10779 " int i;\n" 10780 "};\n", 10781 Style); 10782 verifyFormat("struct foo {\n" 10783 "private:\n" 10784 " // comment\n" 10785 " void f() {}\n" 10786 "\n" 10787 "private: /* comment */\n" 10788 " int i;\n" 10789 "};\n", 10790 "struct foo {\n" 10791 "private:\n" 10792 "\n" 10793 " // comment\n" 10794 " void f() {}\n" 10795 "\n" 10796 "private: /* comment */\n" 10797 "\n" 10798 " int i;\n" 10799 "};\n", 10800 Style); 10801 10802 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10803 verifyFormat("struct foo {\n" 10804 "private:\n" 10805 "\n" 10806 " // comment\n" 10807 " void f() {}\n" 10808 "\n" 10809 "private: /* comment */\n" 10810 "\n" 10811 " int i;\n" 10812 "};\n", 10813 "struct foo {\n" 10814 "private:\n" 10815 " // comment\n" 10816 " void f() {}\n" 10817 "\n" 10818 "private: /* comment */\n" 10819 " int i;\n" 10820 "};\n", 10821 Style); 10822 verifyFormat("struct foo {\n" 10823 "private:\n" 10824 "\n" 10825 " // comment\n" 10826 " void f() {}\n" 10827 "\n" 10828 "private: /* comment */\n" 10829 "\n" 10830 " int i;\n" 10831 "};\n", 10832 Style); 10833 10834 // Test with preprocessor defines. 10835 Style = getLLVMStyle(); 10836 verifyFormat("struct foo {\n" 10837 "private:\n" 10838 "#ifdef FOO\n" 10839 "#endif\n" 10840 " void f() {}\n" 10841 "};\n", 10842 Style); 10843 verifyFormat("struct foo {\n" 10844 "private:\n" 10845 "#ifdef FOO\n" 10846 "#endif\n" 10847 " void f() {}\n" 10848 "};\n", 10849 "struct foo {\n" 10850 "private:\n" 10851 "\n" 10852 "#ifdef FOO\n" 10853 "#endif\n" 10854 " void f() {}\n" 10855 "};\n", 10856 Style); 10857 10858 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10859 verifyFormat("struct foo {\n" 10860 "private:\n" 10861 "\n" 10862 "#ifdef FOO\n" 10863 "#endif\n" 10864 " void f() {}\n" 10865 "};\n", 10866 "struct foo {\n" 10867 "private:\n" 10868 "#ifdef FOO\n" 10869 "#endif\n" 10870 " void f() {}\n" 10871 "};\n", 10872 Style); 10873 verifyFormat("struct foo {\n" 10874 "private:\n" 10875 "\n" 10876 "#ifdef FOO\n" 10877 "#endif\n" 10878 " void f() {}\n" 10879 "};\n", 10880 Style); 10881 } 10882 10883 TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) { 10884 // Combined tests of EmptyLineAfterAccessModifier and 10885 // EmptyLineBeforeAccessModifier. 10886 FormatStyle Style = getLLVMStyle(); 10887 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10888 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10889 verifyFormat("struct foo {\n" 10890 "private:\n" 10891 "\n" 10892 "protected:\n" 10893 "};\n", 10894 Style); 10895 10896 Style.MaxEmptyLinesToKeep = 10u; 10897 // Both remove all new lines. 10898 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10899 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 10900 verifyFormat("struct foo {\n" 10901 "private:\n" 10902 "protected:\n" 10903 "};\n", 10904 "struct foo {\n" 10905 "private:\n" 10906 "\n\n\n" 10907 "protected:\n" 10908 "};\n", 10909 Style); 10910 10911 // Leave tests rely on the code layout, test::messUp can not be used. 10912 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 10913 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 10914 Style.MaxEmptyLinesToKeep = 10u; 10915 EXPECT_EQ("struct foo {\n" 10916 "private:\n" 10917 "\n\n\n" 10918 "protected:\n" 10919 "};\n", 10920 format("struct foo {\n" 10921 "private:\n" 10922 "\n\n\n" 10923 "protected:\n" 10924 "};\n", 10925 Style)); 10926 Style.MaxEmptyLinesToKeep = 3u; 10927 EXPECT_EQ("struct foo {\n" 10928 "private:\n" 10929 "\n\n\n" 10930 "protected:\n" 10931 "};\n", 10932 format("struct foo {\n" 10933 "private:\n" 10934 "\n\n\n" 10935 "protected:\n" 10936 "};\n", 10937 Style)); 10938 Style.MaxEmptyLinesToKeep = 1u; 10939 EXPECT_EQ("struct foo {\n" 10940 "private:\n" 10941 "\n\n\n" 10942 "protected:\n" 10943 "};\n", 10944 format("struct foo {\n" 10945 "private:\n" 10946 "\n\n\n" 10947 "protected:\n" 10948 "};\n", 10949 Style)); // Based on new lines in original document and not 10950 // on the setting. 10951 10952 Style.MaxEmptyLinesToKeep = 10u; 10953 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10954 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 10955 // Newlines are kept if they are greater than zero, 10956 // test::messUp removes all new lines which changes the logic 10957 EXPECT_EQ("struct foo {\n" 10958 "private:\n" 10959 "\n\n\n" 10960 "protected:\n" 10961 "};\n", 10962 format("struct foo {\n" 10963 "private:\n" 10964 "\n\n\n" 10965 "protected:\n" 10966 "};\n", 10967 Style)); 10968 10969 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 10970 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10971 // test::messUp removes all new lines which changes the logic 10972 EXPECT_EQ("struct foo {\n" 10973 "private:\n" 10974 "\n\n\n" 10975 "protected:\n" 10976 "};\n", 10977 format("struct foo {\n" 10978 "private:\n" 10979 "\n\n\n" 10980 "protected:\n" 10981 "};\n", 10982 Style)); 10983 10984 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 10985 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 10986 EXPECT_EQ("struct foo {\n" 10987 "private:\n" 10988 "\n\n\n" 10989 "protected:\n" 10990 "};\n", 10991 format("struct foo {\n" 10992 "private:\n" 10993 "\n\n\n" 10994 "protected:\n" 10995 "};\n", 10996 Style)); // test::messUp removes all new lines which changes 10997 // the logic. 10998 10999 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 11000 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 11001 verifyFormat("struct foo {\n" 11002 "private:\n" 11003 "protected:\n" 11004 "};\n", 11005 "struct foo {\n" 11006 "private:\n" 11007 "\n\n\n" 11008 "protected:\n" 11009 "};\n", 11010 Style); 11011 11012 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 11013 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 11014 EXPECT_EQ("struct foo {\n" 11015 "private:\n" 11016 "\n\n\n" 11017 "protected:\n" 11018 "};\n", 11019 format("struct foo {\n" 11020 "private:\n" 11021 "\n\n\n" 11022 "protected:\n" 11023 "};\n", 11024 Style)); // test::messUp removes all new lines which changes 11025 // the logic. 11026 11027 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 11028 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11029 verifyFormat("struct foo {\n" 11030 "private:\n" 11031 "protected:\n" 11032 "};\n", 11033 "struct foo {\n" 11034 "private:\n" 11035 "\n\n\n" 11036 "protected:\n" 11037 "};\n", 11038 Style); 11039 11040 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 11041 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11042 verifyFormat("struct foo {\n" 11043 "private:\n" 11044 "protected:\n" 11045 "};\n", 11046 "struct foo {\n" 11047 "private:\n" 11048 "\n\n\n" 11049 "protected:\n" 11050 "};\n", 11051 Style); 11052 11053 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 11054 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 11055 verifyFormat("struct foo {\n" 11056 "private:\n" 11057 "protected:\n" 11058 "};\n", 11059 "struct foo {\n" 11060 "private:\n" 11061 "\n\n\n" 11062 "protected:\n" 11063 "};\n", 11064 Style); 11065 11066 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 11067 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 11068 verifyFormat("struct foo {\n" 11069 "private:\n" 11070 "protected:\n" 11071 "};\n", 11072 "struct foo {\n" 11073 "private:\n" 11074 "\n\n\n" 11075 "protected:\n" 11076 "};\n", 11077 Style); 11078 } 11079 11080 TEST_F(FormatTest, FormatsArrays) { 11081 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 11082 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 11083 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 11084 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 11085 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 11086 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 11087 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11088 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 11089 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11090 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 11091 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11092 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 11093 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 11094 verifyFormat( 11095 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 11096 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 11097 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 11098 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" 11099 " .aaaaaaaaaaaaaaaaaaaaaa();"); 11100 11101 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 11102 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 11103 verifyFormat( 11104 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 11105 " .aaaaaaa[0]\n" 11106 " .aaaaaaaaaaaaaaaaaaaaaa();"); 11107 verifyFormat("a[::b::c];"); 11108 11109 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 11110 11111 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 11112 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 11113 } 11114 11115 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 11116 verifyFormat("(a)->b();"); 11117 verifyFormat("--a;"); 11118 } 11119 11120 TEST_F(FormatTest, HandlesIncludeDirectives) { 11121 verifyFormat("#include <string>\n" 11122 "#include <a/b/c.h>\n" 11123 "#include \"a/b/string\"\n" 11124 "#include \"string.h\"\n" 11125 "#include \"string.h\"\n" 11126 "#include <a-a>\n" 11127 "#include < path with space >\n" 11128 "#include_next <test.h>" 11129 "#include \"abc.h\" // this is included for ABC\n" 11130 "#include \"some long include\" // with a comment\n" 11131 "#include \"some very long include path\"\n" 11132 "#include <some/very/long/include/path>\n", 11133 getLLVMStyleWithColumns(35)); 11134 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 11135 EXPECT_EQ("#include <a>", format("#include<a>")); 11136 11137 verifyFormat("#import <string>"); 11138 verifyFormat("#import <a/b/c.h>"); 11139 verifyFormat("#import \"a/b/string\""); 11140 verifyFormat("#import \"string.h\""); 11141 verifyFormat("#import \"string.h\""); 11142 verifyFormat("#if __has_include(<strstream>)\n" 11143 "#include <strstream>\n" 11144 "#endif"); 11145 11146 verifyFormat("#define MY_IMPORT <a/b>"); 11147 11148 verifyFormat("#if __has_include(<a/b>)"); 11149 verifyFormat("#if __has_include_next(<a/b>)"); 11150 verifyFormat("#define F __has_include(<a/b>)"); 11151 verifyFormat("#define F __has_include_next(<a/b>)"); 11152 11153 // Protocol buffer definition or missing "#". 11154 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 11155 getLLVMStyleWithColumns(30)); 11156 11157 FormatStyle Style = getLLVMStyle(); 11158 Style.AlwaysBreakBeforeMultilineStrings = true; 11159 Style.ColumnLimit = 0; 11160 verifyFormat("#import \"abc.h\"", Style); 11161 11162 // But 'import' might also be a regular C++ namespace. 11163 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11164 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 11165 } 11166 11167 //===----------------------------------------------------------------------===// 11168 // Error recovery tests. 11169 //===----------------------------------------------------------------------===// 11170 11171 TEST_F(FormatTest, IncompleteParameterLists) { 11172 FormatStyle NoBinPacking = getLLVMStyle(); 11173 NoBinPacking.BinPackParameters = false; 11174 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 11175 " double *min_x,\n" 11176 " double *max_x,\n" 11177 " double *min_y,\n" 11178 " double *max_y,\n" 11179 " double *min_z,\n" 11180 " double *max_z, ) {}", 11181 NoBinPacking); 11182 } 11183 11184 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 11185 verifyFormat("void f() { return; }\n42"); 11186 verifyFormat("void f() {\n" 11187 " if (0)\n" 11188 " return;\n" 11189 "}\n" 11190 "42"); 11191 verifyFormat("void f() { return }\n42"); 11192 verifyFormat("void f() {\n" 11193 " if (0)\n" 11194 " return\n" 11195 "}\n" 11196 "42"); 11197 } 11198 11199 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 11200 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 11201 EXPECT_EQ("void f() {\n" 11202 " if (a)\n" 11203 " return\n" 11204 "}", 11205 format("void f ( ) { if ( a ) return }")); 11206 EXPECT_EQ("namespace N {\n" 11207 "void f()\n" 11208 "}", 11209 format("namespace N { void f() }")); 11210 EXPECT_EQ("namespace N {\n" 11211 "void f() {}\n" 11212 "void g()\n" 11213 "} // namespace N", 11214 format("namespace N { void f( ) { } void g( ) }")); 11215 } 11216 11217 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 11218 verifyFormat("int aaaaaaaa =\n" 11219 " // Overlylongcomment\n" 11220 " b;", 11221 getLLVMStyleWithColumns(20)); 11222 verifyFormat("function(\n" 11223 " ShortArgument,\n" 11224 " LoooooooooooongArgument);\n", 11225 getLLVMStyleWithColumns(20)); 11226 } 11227 11228 TEST_F(FormatTest, IncorrectAccessSpecifier) { 11229 verifyFormat("public:"); 11230 verifyFormat("class A {\n" 11231 "public\n" 11232 " void f() {}\n" 11233 "};"); 11234 verifyFormat("public\n" 11235 "int qwerty;"); 11236 verifyFormat("public\n" 11237 "B {}"); 11238 verifyFormat("public\n" 11239 "{}"); 11240 verifyFormat("public\n" 11241 "B { int x; }"); 11242 } 11243 11244 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 11245 verifyFormat("{"); 11246 verifyFormat("#})"); 11247 verifyNoCrash("(/**/[:!] ?[)."); 11248 } 11249 11250 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) { 11251 // Found by oss-fuzz: 11252 // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212 11253 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 11254 Style.ColumnLimit = 60; 11255 verifyNoCrash( 11256 "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20" 11257 "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20" 11258 "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a", 11259 Style); 11260 } 11261 11262 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 11263 verifyFormat("do {\n}"); 11264 verifyFormat("do {\n}\n" 11265 "f();"); 11266 verifyFormat("do {\n}\n" 11267 "wheeee(fun);"); 11268 verifyFormat("do {\n" 11269 " f();\n" 11270 "}"); 11271 } 11272 11273 TEST_F(FormatTest, IncorrectCodeMissingParens) { 11274 verifyFormat("if {\n foo;\n foo();\n}"); 11275 verifyFormat("switch {\n foo;\n foo();\n}"); 11276 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 11277 verifyFormat("while {\n foo;\n foo();\n}"); 11278 verifyFormat("do {\n foo;\n foo();\n} while;"); 11279 } 11280 11281 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 11282 verifyIncompleteFormat("namespace {\n" 11283 "class Foo { Foo (\n" 11284 "};\n" 11285 "} // namespace"); 11286 } 11287 11288 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 11289 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 11290 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 11291 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 11292 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 11293 11294 EXPECT_EQ("{\n" 11295 " {\n" 11296 " breakme(\n" 11297 " qwe);\n" 11298 " }\n", 11299 format("{\n" 11300 " {\n" 11301 " breakme(qwe);\n" 11302 "}\n", 11303 getLLVMStyleWithColumns(10))); 11304 } 11305 11306 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 11307 verifyFormat("int x = {\n" 11308 " avariable,\n" 11309 " b(alongervariable)};", 11310 getLLVMStyleWithColumns(25)); 11311 } 11312 11313 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 11314 verifyFormat("return (a)(b){1, 2, 3};"); 11315 } 11316 11317 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 11318 verifyFormat("vector<int> x{1, 2, 3, 4};"); 11319 verifyFormat("vector<int> x{\n" 11320 " 1,\n" 11321 " 2,\n" 11322 " 3,\n" 11323 " 4,\n" 11324 "};"); 11325 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 11326 verifyFormat("f({1, 2});"); 11327 verifyFormat("auto v = Foo{-1};"); 11328 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 11329 verifyFormat("Class::Class : member{1, 2, 3} {}"); 11330 verifyFormat("new vector<int>{1, 2, 3};"); 11331 verifyFormat("new int[3]{1, 2, 3};"); 11332 verifyFormat("new int{1};"); 11333 verifyFormat("return {arg1, arg2};"); 11334 verifyFormat("return {arg1, SomeType{parameter}};"); 11335 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 11336 verifyFormat("new T{arg1, arg2};"); 11337 verifyFormat("f(MyMap[{composite, key}]);"); 11338 verifyFormat("class Class {\n" 11339 " T member = {arg1, arg2};\n" 11340 "};"); 11341 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 11342 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 11343 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 11344 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 11345 verifyFormat("int a = std::is_integral<int>{} + 0;"); 11346 11347 verifyFormat("int foo(int i) { return fo1{}(i); }"); 11348 verifyFormat("int foo(int i) { return fo1{}(i); }"); 11349 verifyFormat("auto i = decltype(x){};"); 11350 verifyFormat("auto i = typeof(x){};"); 11351 verifyFormat("auto i = _Atomic(x){};"); 11352 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 11353 verifyFormat("Node n{1, Node{1000}, //\n" 11354 " 2};"); 11355 verifyFormat("Aaaa aaaaaaa{\n" 11356 " {\n" 11357 " aaaa,\n" 11358 " },\n" 11359 "};"); 11360 verifyFormat("class C : public D {\n" 11361 " SomeClass SC{2};\n" 11362 "};"); 11363 verifyFormat("class C : public A {\n" 11364 " class D : public B {\n" 11365 " void f() { int i{2}; }\n" 11366 " };\n" 11367 "};"); 11368 verifyFormat("#define A {a, a},"); 11369 11370 // Avoid breaking between equal sign and opening brace 11371 FormatStyle AvoidBreakingFirstArgument = getLLVMStyle(); 11372 AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200; 11373 verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n" 11374 " {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n" 11375 " {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n" 11376 " {\"ccccccccccccccccccccc\", 2}};", 11377 AvoidBreakingFirstArgument); 11378 11379 // Binpacking only if there is no trailing comma 11380 verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n" 11381 " cccccccccc, dddddddddd};", 11382 getLLVMStyleWithColumns(50)); 11383 verifyFormat("const Aaaaaa aaaaa = {\n" 11384 " aaaaaaaaaaa,\n" 11385 " bbbbbbbbbbb,\n" 11386 " ccccccccccc,\n" 11387 " ddddddddddd,\n" 11388 "};", 11389 getLLVMStyleWithColumns(50)); 11390 11391 // Cases where distinguising braced lists and blocks is hard. 11392 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);"); 11393 verifyFormat("void f() {\n" 11394 " return; // comment\n" 11395 "}\n" 11396 "SomeType t;"); 11397 verifyFormat("void f() {\n" 11398 " if (a) {\n" 11399 " f();\n" 11400 " }\n" 11401 "}\n" 11402 "SomeType t;"); 11403 11404 // In combination with BinPackArguments = false. 11405 FormatStyle NoBinPacking = getLLVMStyle(); 11406 NoBinPacking.BinPackArguments = false; 11407 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 11408 " bbbbb,\n" 11409 " ccccc,\n" 11410 " ddddd,\n" 11411 " eeeee,\n" 11412 " ffffff,\n" 11413 " ggggg,\n" 11414 " hhhhhh,\n" 11415 " iiiiii,\n" 11416 " jjjjjj,\n" 11417 " kkkkkk};", 11418 NoBinPacking); 11419 verifyFormat("const Aaaaaa aaaaa = {\n" 11420 " aaaaa,\n" 11421 " bbbbb,\n" 11422 " ccccc,\n" 11423 " ddddd,\n" 11424 " eeeee,\n" 11425 " ffffff,\n" 11426 " ggggg,\n" 11427 " hhhhhh,\n" 11428 " iiiiii,\n" 11429 " jjjjjj,\n" 11430 " kkkkkk,\n" 11431 "};", 11432 NoBinPacking); 11433 verifyFormat( 11434 "const Aaaaaa aaaaa = {\n" 11435 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 11436 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 11437 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 11438 "};", 11439 NoBinPacking); 11440 11441 NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 11442 EXPECT_EQ("static uint8 CddDp83848Reg[] = {\n" 11443 " CDDDP83848_BMCR_REGISTER,\n" 11444 " CDDDP83848_BMSR_REGISTER,\n" 11445 " CDDDP83848_RBR_REGISTER};", 11446 format("static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n" 11447 " CDDDP83848_BMSR_REGISTER,\n" 11448 " CDDDP83848_RBR_REGISTER};", 11449 NoBinPacking)); 11450 11451 // FIXME: The alignment of these trailing comments might be bad. Then again, 11452 // this might be utterly useless in real code. 11453 verifyFormat("Constructor::Constructor()\n" 11454 " : some_value{ //\n" 11455 " aaaaaaa, //\n" 11456 " bbbbbbb} {}"); 11457 11458 // In braced lists, the first comment is always assumed to belong to the 11459 // first element. Thus, it can be moved to the next or previous line as 11460 // appropriate. 11461 EXPECT_EQ("function({// First element:\n" 11462 " 1,\n" 11463 " // Second element:\n" 11464 " 2});", 11465 format("function({\n" 11466 " // First element:\n" 11467 " 1,\n" 11468 " // Second element:\n" 11469 " 2});")); 11470 EXPECT_EQ("std::vector<int> MyNumbers{\n" 11471 " // First element:\n" 11472 " 1,\n" 11473 " // Second element:\n" 11474 " 2};", 11475 format("std::vector<int> MyNumbers{// First element:\n" 11476 " 1,\n" 11477 " // Second element:\n" 11478 " 2};", 11479 getLLVMStyleWithColumns(30))); 11480 // A trailing comma should still lead to an enforced line break and no 11481 // binpacking. 11482 EXPECT_EQ("vector<int> SomeVector = {\n" 11483 " // aaa\n" 11484 " 1,\n" 11485 " 2,\n" 11486 "};", 11487 format("vector<int> SomeVector = { // aaa\n" 11488 " 1, 2, };")); 11489 11490 // C++11 brace initializer list l-braces should not be treated any differently 11491 // when breaking before lambda bodies is enabled 11492 FormatStyle BreakBeforeLambdaBody = getLLVMStyle(); 11493 BreakBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 11494 BreakBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 11495 BreakBeforeLambdaBody.AlwaysBreakBeforeMultilineStrings = true; 11496 verifyFormat( 11497 "std::runtime_error{\n" 11498 " \"Long string which will force a break onto the next line...\"};", 11499 BreakBeforeLambdaBody); 11500 11501 FormatStyle ExtraSpaces = getLLVMStyle(); 11502 ExtraSpaces.Cpp11BracedListStyle = false; 11503 ExtraSpaces.ColumnLimit = 75; 11504 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 11505 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 11506 verifyFormat("f({ 1, 2 });", ExtraSpaces); 11507 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 11508 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 11509 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 11510 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 11511 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 11512 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 11513 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 11514 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 11515 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 11516 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 11517 verifyFormat("class Class {\n" 11518 " T member = { arg1, arg2 };\n" 11519 "};", 11520 ExtraSpaces); 11521 verifyFormat( 11522 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11523 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 11524 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 11525 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 11526 ExtraSpaces); 11527 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 11528 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 11529 ExtraSpaces); 11530 verifyFormat( 11531 "someFunction(OtherParam,\n" 11532 " BracedList{ // comment 1 (Forcing interesting break)\n" 11533 " param1, param2,\n" 11534 " // comment 2\n" 11535 " param3, param4 });", 11536 ExtraSpaces); 11537 verifyFormat( 11538 "std::this_thread::sleep_for(\n" 11539 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 11540 ExtraSpaces); 11541 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n" 11542 " aaaaaaa,\n" 11543 " aaaaaaaaaa,\n" 11544 " aaaaa,\n" 11545 " aaaaaaaaaaaaaaa,\n" 11546 " aaa,\n" 11547 " aaaaaaaaaa,\n" 11548 " a,\n" 11549 " aaaaaaaaaaaaaaaaaaaaa,\n" 11550 " aaaaaaaaaaaa,\n" 11551 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 11552 " aaaaaaa,\n" 11553 " a};"); 11554 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 11555 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces); 11556 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces); 11557 11558 // Avoid breaking between initializer/equal sign and opening brace 11559 ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200; 11560 verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n" 11561 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 11562 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 11563 " { \"ccccccccccccccccccccc\", 2 }\n" 11564 "};", 11565 ExtraSpaces); 11566 verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n" 11567 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 11568 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 11569 " { \"ccccccccccccccccccccc\", 2 }\n" 11570 "};", 11571 ExtraSpaces); 11572 11573 FormatStyle SpaceBeforeBrace = getLLVMStyle(); 11574 SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true; 11575 verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace); 11576 verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace); 11577 11578 FormatStyle SpaceBetweenBraces = getLLVMStyle(); 11579 SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always; 11580 SpaceBetweenBraces.SpacesInParentheses = true; 11581 SpaceBetweenBraces.SpacesInSquareBrackets = true; 11582 verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces); 11583 verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces); 11584 verifyFormat("vector< int > x{ // comment 1\n" 11585 " 1, 2, 3, 4 };", 11586 SpaceBetweenBraces); 11587 SpaceBetweenBraces.ColumnLimit = 20; 11588 EXPECT_EQ("vector< int > x{\n" 11589 " 1, 2, 3, 4 };", 11590 format("vector<int>x{1,2,3,4};", SpaceBetweenBraces)); 11591 SpaceBetweenBraces.ColumnLimit = 24; 11592 EXPECT_EQ("vector< int > x{ 1, 2,\n" 11593 " 3, 4 };", 11594 format("vector<int>x{1,2,3,4};", SpaceBetweenBraces)); 11595 EXPECT_EQ("vector< int > x{\n" 11596 " 1,\n" 11597 " 2,\n" 11598 " 3,\n" 11599 " 4,\n" 11600 "};", 11601 format("vector<int>x{1,2,3,4,};", SpaceBetweenBraces)); 11602 verifyFormat("vector< int > x{};", SpaceBetweenBraces); 11603 SpaceBetweenBraces.SpaceInEmptyParentheses = true; 11604 verifyFormat("vector< int > x{ };", SpaceBetweenBraces); 11605 } 11606 11607 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 11608 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11609 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11610 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11611 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11612 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11613 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 11614 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 11615 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11616 " 1, 22, 333, 4444, 55555, //\n" 11617 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11618 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 11619 verifyFormat( 11620 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11621 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11622 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 11623 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 11624 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 11625 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 11626 " 7777777};"); 11627 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 11628 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 11629 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 11630 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 11631 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 11632 " // Separating comment.\n" 11633 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 11634 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 11635 " // Leading comment\n" 11636 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 11637 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 11638 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 11639 " 1, 1, 1, 1};", 11640 getLLVMStyleWithColumns(39)); 11641 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 11642 " 1, 1, 1, 1};", 11643 getLLVMStyleWithColumns(38)); 11644 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 11645 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 11646 getLLVMStyleWithColumns(43)); 11647 verifyFormat( 11648 "static unsigned SomeValues[10][3] = {\n" 11649 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 11650 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 11651 verifyFormat("static auto fields = new vector<string>{\n" 11652 " \"aaaaaaaaaaaaa\",\n" 11653 " \"aaaaaaaaaaaaa\",\n" 11654 " \"aaaaaaaaaaaa\",\n" 11655 " \"aaaaaaaaaaaaaa\",\n" 11656 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 11657 " \"aaaaaaaaaaaa\",\n" 11658 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 11659 "};"); 11660 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 11661 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 11662 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 11663 " 3, cccccccccccccccccccccc};", 11664 getLLVMStyleWithColumns(60)); 11665 11666 // Trailing commas. 11667 verifyFormat("vector<int> x = {\n" 11668 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 11669 "};", 11670 getLLVMStyleWithColumns(39)); 11671 verifyFormat("vector<int> x = {\n" 11672 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 11673 "};", 11674 getLLVMStyleWithColumns(39)); 11675 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 11676 " 1, 1, 1, 1,\n" 11677 " /**/ /**/};", 11678 getLLVMStyleWithColumns(39)); 11679 11680 // Trailing comment in the first line. 11681 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 11682 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 11683 " 111111111, 222222222, 3333333333, 444444444, //\n" 11684 " 11111111, 22222222, 333333333, 44444444};"); 11685 // Trailing comment in the last line. 11686 verifyFormat("int aaaaa[] = {\n" 11687 " 1, 2, 3, // comment\n" 11688 " 4, 5, 6 // comment\n" 11689 "};"); 11690 11691 // With nested lists, we should either format one item per line or all nested 11692 // lists one on line. 11693 // FIXME: For some nested lists, we can do better. 11694 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 11695 " {aaaaaaaaaaaaaaaaaaa},\n" 11696 " {aaaaaaaaaaaaaaaaaaaaa},\n" 11697 " {aaaaaaaaaaaaaaaaa}};", 11698 getLLVMStyleWithColumns(60)); 11699 verifyFormat( 11700 "SomeStruct my_struct_array = {\n" 11701 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 11702 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 11703 " {aaa, aaa},\n" 11704 " {aaa, aaa},\n" 11705 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 11706 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 11707 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 11708 11709 // No column layout should be used here. 11710 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 11711 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 11712 11713 verifyNoCrash("a<,"); 11714 11715 // No braced initializer here. 11716 verifyFormat("void f() {\n" 11717 " struct Dummy {};\n" 11718 " f(v);\n" 11719 "}"); 11720 11721 // Long lists should be formatted in columns even if they are nested. 11722 verifyFormat( 11723 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11724 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11725 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11726 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11727 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11728 " 1, 22, 333, 4444, 55555, 666666, 7777777});"); 11729 11730 // Allow "single-column" layout even if that violates the column limit. There 11731 // isn't going to be a better way. 11732 verifyFormat("std::vector<int> a = {\n" 11733 " aaaaaaaa,\n" 11734 " aaaaaaaa,\n" 11735 " aaaaaaaa,\n" 11736 " aaaaaaaa,\n" 11737 " aaaaaaaaaa,\n" 11738 " aaaaaaaa,\n" 11739 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};", 11740 getLLVMStyleWithColumns(30)); 11741 verifyFormat("vector<int> aaaa = {\n" 11742 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11743 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11744 " aaaaaa.aaaaaaa,\n" 11745 " aaaaaa.aaaaaaa,\n" 11746 " aaaaaa.aaaaaaa,\n" 11747 " aaaaaa.aaaaaaa,\n" 11748 "};"); 11749 11750 // Don't create hanging lists. 11751 verifyFormat("someFunction(Param, {List1, List2,\n" 11752 " List3});", 11753 getLLVMStyleWithColumns(35)); 11754 verifyFormat("someFunction(Param, Param,\n" 11755 " {List1, List2,\n" 11756 " List3});", 11757 getLLVMStyleWithColumns(35)); 11758 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n" 11759 " aaaaaaaaaaaaaaaaaaaaaaa);"); 11760 } 11761 11762 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 11763 FormatStyle DoNotMerge = getLLVMStyle(); 11764 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 11765 11766 verifyFormat("void f() { return 42; }"); 11767 verifyFormat("void f() {\n" 11768 " return 42;\n" 11769 "}", 11770 DoNotMerge); 11771 verifyFormat("void f() {\n" 11772 " // Comment\n" 11773 "}"); 11774 verifyFormat("{\n" 11775 "#error {\n" 11776 " int a;\n" 11777 "}"); 11778 verifyFormat("{\n" 11779 " int a;\n" 11780 "#error {\n" 11781 "}"); 11782 verifyFormat("void f() {} // comment"); 11783 verifyFormat("void f() { int a; } // comment"); 11784 verifyFormat("void f() {\n" 11785 "} // comment", 11786 DoNotMerge); 11787 verifyFormat("void f() {\n" 11788 " int a;\n" 11789 "} // comment", 11790 DoNotMerge); 11791 verifyFormat("void f() {\n" 11792 "} // comment", 11793 getLLVMStyleWithColumns(15)); 11794 11795 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 11796 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 11797 11798 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 11799 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 11800 verifyFormat("class C {\n" 11801 " C()\n" 11802 " : iiiiiiii(nullptr),\n" 11803 " kkkkkkk(nullptr),\n" 11804 " mmmmmmm(nullptr),\n" 11805 " nnnnnnn(nullptr) {}\n" 11806 "};", 11807 getGoogleStyle()); 11808 11809 FormatStyle NoColumnLimit = getLLVMStyle(); 11810 NoColumnLimit.ColumnLimit = 0; 11811 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 11812 EXPECT_EQ("class C {\n" 11813 " A() : b(0) {}\n" 11814 "};", 11815 format("class C{A():b(0){}};", NoColumnLimit)); 11816 EXPECT_EQ("A()\n" 11817 " : b(0) {\n" 11818 "}", 11819 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 11820 11821 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 11822 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 11823 FormatStyle::SFS_None; 11824 EXPECT_EQ("A()\n" 11825 " : b(0) {\n" 11826 "}", 11827 format("A():b(0){}", DoNotMergeNoColumnLimit)); 11828 EXPECT_EQ("A()\n" 11829 " : b(0) {\n" 11830 "}", 11831 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 11832 11833 verifyFormat("#define A \\\n" 11834 " void f() { \\\n" 11835 " int i; \\\n" 11836 " }", 11837 getLLVMStyleWithColumns(20)); 11838 verifyFormat("#define A \\\n" 11839 " void f() { int i; }", 11840 getLLVMStyleWithColumns(21)); 11841 verifyFormat("#define A \\\n" 11842 " void f() { \\\n" 11843 " int i; \\\n" 11844 " } \\\n" 11845 " int j;", 11846 getLLVMStyleWithColumns(22)); 11847 verifyFormat("#define A \\\n" 11848 " void f() { int i; } \\\n" 11849 " int j;", 11850 getLLVMStyleWithColumns(23)); 11851 } 11852 11853 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) { 11854 FormatStyle MergeEmptyOnly = getLLVMStyle(); 11855 MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 11856 verifyFormat("class C {\n" 11857 " int f() {}\n" 11858 "};", 11859 MergeEmptyOnly); 11860 verifyFormat("class C {\n" 11861 " int f() {\n" 11862 " return 42;\n" 11863 " }\n" 11864 "};", 11865 MergeEmptyOnly); 11866 verifyFormat("int f() {}", MergeEmptyOnly); 11867 verifyFormat("int f() {\n" 11868 " return 42;\n" 11869 "}", 11870 MergeEmptyOnly); 11871 11872 // Also verify behavior when BraceWrapping.AfterFunction = true 11873 MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 11874 MergeEmptyOnly.BraceWrapping.AfterFunction = true; 11875 verifyFormat("int f() {}", MergeEmptyOnly); 11876 verifyFormat("class C {\n" 11877 " int f() {}\n" 11878 "};", 11879 MergeEmptyOnly); 11880 } 11881 11882 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 11883 FormatStyle MergeInlineOnly = getLLVMStyle(); 11884 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 11885 verifyFormat("class C {\n" 11886 " int f() { return 42; }\n" 11887 "};", 11888 MergeInlineOnly); 11889 verifyFormat("int f() {\n" 11890 " return 42;\n" 11891 "}", 11892 MergeInlineOnly); 11893 11894 // SFS_Inline implies SFS_Empty 11895 verifyFormat("class C {\n" 11896 " int f() {}\n" 11897 "};", 11898 MergeInlineOnly); 11899 verifyFormat("int f() {}", MergeInlineOnly); 11900 11901 // Also verify behavior when BraceWrapping.AfterFunction = true 11902 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 11903 MergeInlineOnly.BraceWrapping.AfterFunction = true; 11904 verifyFormat("class C {\n" 11905 " int f() { return 42; }\n" 11906 "};", 11907 MergeInlineOnly); 11908 verifyFormat("int f()\n" 11909 "{\n" 11910 " return 42;\n" 11911 "}", 11912 MergeInlineOnly); 11913 11914 // SFS_Inline implies SFS_Empty 11915 verifyFormat("int f() {}", MergeInlineOnly); 11916 verifyFormat("class C {\n" 11917 " int f() {}\n" 11918 "};", 11919 MergeInlineOnly); 11920 } 11921 11922 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) { 11923 FormatStyle MergeInlineOnly = getLLVMStyle(); 11924 MergeInlineOnly.AllowShortFunctionsOnASingleLine = 11925 FormatStyle::SFS_InlineOnly; 11926 verifyFormat("class C {\n" 11927 " int f() { return 42; }\n" 11928 "};", 11929 MergeInlineOnly); 11930 verifyFormat("int f() {\n" 11931 " return 42;\n" 11932 "}", 11933 MergeInlineOnly); 11934 11935 // SFS_InlineOnly does not imply SFS_Empty 11936 verifyFormat("class C {\n" 11937 " int f() {}\n" 11938 "};", 11939 MergeInlineOnly); 11940 verifyFormat("int f() {\n" 11941 "}", 11942 MergeInlineOnly); 11943 11944 // Also verify behavior when BraceWrapping.AfterFunction = true 11945 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 11946 MergeInlineOnly.BraceWrapping.AfterFunction = true; 11947 verifyFormat("class C {\n" 11948 " int f() { return 42; }\n" 11949 "};", 11950 MergeInlineOnly); 11951 verifyFormat("int f()\n" 11952 "{\n" 11953 " return 42;\n" 11954 "}", 11955 MergeInlineOnly); 11956 11957 // SFS_InlineOnly does not imply SFS_Empty 11958 verifyFormat("int f()\n" 11959 "{\n" 11960 "}", 11961 MergeInlineOnly); 11962 verifyFormat("class C {\n" 11963 " int f() {}\n" 11964 "};", 11965 MergeInlineOnly); 11966 } 11967 11968 TEST_F(FormatTest, SplitEmptyFunction) { 11969 FormatStyle Style = getLLVMStyle(); 11970 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 11971 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 11972 Style.BraceWrapping.AfterFunction = true; 11973 Style.BraceWrapping.SplitEmptyFunction = false; 11974 Style.ColumnLimit = 40; 11975 11976 verifyFormat("int f()\n" 11977 "{}", 11978 Style); 11979 verifyFormat("int f()\n" 11980 "{\n" 11981 " return 42;\n" 11982 "}", 11983 Style); 11984 verifyFormat("int f()\n" 11985 "{\n" 11986 " // some comment\n" 11987 "}", 11988 Style); 11989 11990 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 11991 verifyFormat("int f() {}", Style); 11992 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 11993 "{}", 11994 Style); 11995 verifyFormat("int f()\n" 11996 "{\n" 11997 " return 0;\n" 11998 "}", 11999 Style); 12000 12001 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 12002 verifyFormat("class Foo {\n" 12003 " int f() {}\n" 12004 "};\n", 12005 Style); 12006 verifyFormat("class Foo {\n" 12007 " int f() { return 0; }\n" 12008 "};\n", 12009 Style); 12010 verifyFormat("class Foo {\n" 12011 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12012 " {}\n" 12013 "};\n", 12014 Style); 12015 verifyFormat("class Foo {\n" 12016 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12017 " {\n" 12018 " return 0;\n" 12019 " }\n" 12020 "};\n", 12021 Style); 12022 12023 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 12024 verifyFormat("int f() {}", Style); 12025 verifyFormat("int f() { return 0; }", Style); 12026 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12027 "{}", 12028 Style); 12029 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12030 "{\n" 12031 " return 0;\n" 12032 "}", 12033 Style); 12034 } 12035 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) { 12036 FormatStyle Style = getLLVMStyle(); 12037 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 12038 verifyFormat("#ifdef A\n" 12039 "int f() {}\n" 12040 "#else\n" 12041 "int g() {}\n" 12042 "#endif", 12043 Style); 12044 } 12045 12046 TEST_F(FormatTest, SplitEmptyClass) { 12047 FormatStyle Style = getLLVMStyle(); 12048 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12049 Style.BraceWrapping.AfterClass = true; 12050 Style.BraceWrapping.SplitEmptyRecord = false; 12051 12052 verifyFormat("class Foo\n" 12053 "{};", 12054 Style); 12055 verifyFormat("/* something */ class Foo\n" 12056 "{};", 12057 Style); 12058 verifyFormat("template <typename X> class Foo\n" 12059 "{};", 12060 Style); 12061 verifyFormat("class Foo\n" 12062 "{\n" 12063 " Foo();\n" 12064 "};", 12065 Style); 12066 verifyFormat("typedef class Foo\n" 12067 "{\n" 12068 "} Foo_t;", 12069 Style); 12070 12071 Style.BraceWrapping.SplitEmptyRecord = true; 12072 Style.BraceWrapping.AfterStruct = true; 12073 verifyFormat("class rep\n" 12074 "{\n" 12075 "};", 12076 Style); 12077 verifyFormat("struct rep\n" 12078 "{\n" 12079 "};", 12080 Style); 12081 verifyFormat("template <typename T> class rep\n" 12082 "{\n" 12083 "};", 12084 Style); 12085 verifyFormat("template <typename T> struct rep\n" 12086 "{\n" 12087 "};", 12088 Style); 12089 verifyFormat("class rep\n" 12090 "{\n" 12091 " int x;\n" 12092 "};", 12093 Style); 12094 verifyFormat("struct rep\n" 12095 "{\n" 12096 " int x;\n" 12097 "};", 12098 Style); 12099 verifyFormat("template <typename T> class rep\n" 12100 "{\n" 12101 " int x;\n" 12102 "};", 12103 Style); 12104 verifyFormat("template <typename T> struct rep\n" 12105 "{\n" 12106 " int x;\n" 12107 "};", 12108 Style); 12109 verifyFormat("template <typename T> class rep // Foo\n" 12110 "{\n" 12111 " int x;\n" 12112 "};", 12113 Style); 12114 verifyFormat("template <typename T> struct rep // Bar\n" 12115 "{\n" 12116 " int x;\n" 12117 "};", 12118 Style); 12119 12120 verifyFormat("template <typename T> class rep<T>\n" 12121 "{\n" 12122 " int x;\n" 12123 "};", 12124 Style); 12125 12126 verifyFormat("template <typename T> class rep<std::complex<T>>\n" 12127 "{\n" 12128 " int x;\n" 12129 "};", 12130 Style); 12131 verifyFormat("template <typename T> class rep<std::complex<T>>\n" 12132 "{\n" 12133 "};", 12134 Style); 12135 12136 verifyFormat("#include \"stdint.h\"\n" 12137 "namespace rep {}", 12138 Style); 12139 verifyFormat("#include <stdint.h>\n" 12140 "namespace rep {}", 12141 Style); 12142 verifyFormat("#include <stdint.h>\n" 12143 "namespace rep {}", 12144 "#include <stdint.h>\n" 12145 "namespace rep {\n" 12146 "\n" 12147 "\n" 12148 "}", 12149 Style); 12150 } 12151 12152 TEST_F(FormatTest, SplitEmptyStruct) { 12153 FormatStyle Style = getLLVMStyle(); 12154 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12155 Style.BraceWrapping.AfterStruct = true; 12156 Style.BraceWrapping.SplitEmptyRecord = false; 12157 12158 verifyFormat("struct Foo\n" 12159 "{};", 12160 Style); 12161 verifyFormat("/* something */ struct Foo\n" 12162 "{};", 12163 Style); 12164 verifyFormat("template <typename X> struct Foo\n" 12165 "{};", 12166 Style); 12167 verifyFormat("struct Foo\n" 12168 "{\n" 12169 " Foo();\n" 12170 "};", 12171 Style); 12172 verifyFormat("typedef struct Foo\n" 12173 "{\n" 12174 "} Foo_t;", 12175 Style); 12176 // typedef struct Bar {} Bar_t; 12177 } 12178 12179 TEST_F(FormatTest, SplitEmptyUnion) { 12180 FormatStyle Style = getLLVMStyle(); 12181 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12182 Style.BraceWrapping.AfterUnion = true; 12183 Style.BraceWrapping.SplitEmptyRecord = false; 12184 12185 verifyFormat("union Foo\n" 12186 "{};", 12187 Style); 12188 verifyFormat("/* something */ union Foo\n" 12189 "{};", 12190 Style); 12191 verifyFormat("union Foo\n" 12192 "{\n" 12193 " A,\n" 12194 "};", 12195 Style); 12196 verifyFormat("typedef union Foo\n" 12197 "{\n" 12198 "} Foo_t;", 12199 Style); 12200 } 12201 12202 TEST_F(FormatTest, SplitEmptyNamespace) { 12203 FormatStyle Style = getLLVMStyle(); 12204 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12205 Style.BraceWrapping.AfterNamespace = true; 12206 Style.BraceWrapping.SplitEmptyNamespace = false; 12207 12208 verifyFormat("namespace Foo\n" 12209 "{};", 12210 Style); 12211 verifyFormat("/* something */ namespace Foo\n" 12212 "{};", 12213 Style); 12214 verifyFormat("inline namespace Foo\n" 12215 "{};", 12216 Style); 12217 verifyFormat("/* something */ inline namespace Foo\n" 12218 "{};", 12219 Style); 12220 verifyFormat("export namespace Foo\n" 12221 "{};", 12222 Style); 12223 verifyFormat("namespace Foo\n" 12224 "{\n" 12225 "void Bar();\n" 12226 "};", 12227 Style); 12228 } 12229 12230 TEST_F(FormatTest, NeverMergeShortRecords) { 12231 FormatStyle Style = getLLVMStyle(); 12232 12233 verifyFormat("class Foo {\n" 12234 " Foo();\n" 12235 "};", 12236 Style); 12237 verifyFormat("typedef class Foo {\n" 12238 " Foo();\n" 12239 "} Foo_t;", 12240 Style); 12241 verifyFormat("struct Foo {\n" 12242 " Foo();\n" 12243 "};", 12244 Style); 12245 verifyFormat("typedef struct Foo {\n" 12246 " Foo();\n" 12247 "} Foo_t;", 12248 Style); 12249 verifyFormat("union Foo {\n" 12250 " A,\n" 12251 "};", 12252 Style); 12253 verifyFormat("typedef union Foo {\n" 12254 " A,\n" 12255 "} Foo_t;", 12256 Style); 12257 verifyFormat("namespace Foo {\n" 12258 "void Bar();\n" 12259 "};", 12260 Style); 12261 12262 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12263 Style.BraceWrapping.AfterClass = true; 12264 Style.BraceWrapping.AfterStruct = true; 12265 Style.BraceWrapping.AfterUnion = true; 12266 Style.BraceWrapping.AfterNamespace = true; 12267 verifyFormat("class Foo\n" 12268 "{\n" 12269 " Foo();\n" 12270 "};", 12271 Style); 12272 verifyFormat("typedef class Foo\n" 12273 "{\n" 12274 " Foo();\n" 12275 "} Foo_t;", 12276 Style); 12277 verifyFormat("struct Foo\n" 12278 "{\n" 12279 " Foo();\n" 12280 "};", 12281 Style); 12282 verifyFormat("typedef struct Foo\n" 12283 "{\n" 12284 " Foo();\n" 12285 "} Foo_t;", 12286 Style); 12287 verifyFormat("union Foo\n" 12288 "{\n" 12289 " A,\n" 12290 "};", 12291 Style); 12292 verifyFormat("typedef union Foo\n" 12293 "{\n" 12294 " A,\n" 12295 "} Foo_t;", 12296 Style); 12297 verifyFormat("namespace Foo\n" 12298 "{\n" 12299 "void Bar();\n" 12300 "};", 12301 Style); 12302 } 12303 12304 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 12305 // Elaborate type variable declarations. 12306 verifyFormat("struct foo a = {bar};\nint n;"); 12307 verifyFormat("class foo a = {bar};\nint n;"); 12308 verifyFormat("union foo a = {bar};\nint n;"); 12309 12310 // Elaborate types inside function definitions. 12311 verifyFormat("struct foo f() {}\nint n;"); 12312 verifyFormat("class foo f() {}\nint n;"); 12313 verifyFormat("union foo f() {}\nint n;"); 12314 12315 // Templates. 12316 verifyFormat("template <class X> void f() {}\nint n;"); 12317 verifyFormat("template <struct X> void f() {}\nint n;"); 12318 verifyFormat("template <union X> void f() {}\nint n;"); 12319 12320 // Actual definitions... 12321 verifyFormat("struct {\n} n;"); 12322 verifyFormat( 12323 "template <template <class T, class Y>, class Z> class X {\n} n;"); 12324 verifyFormat("union Z {\n int n;\n} x;"); 12325 verifyFormat("class MACRO Z {\n} n;"); 12326 verifyFormat("class MACRO(X) Z {\n} n;"); 12327 verifyFormat("class __attribute__(X) Z {\n} n;"); 12328 verifyFormat("class __declspec(X) Z {\n} n;"); 12329 verifyFormat("class A##B##C {\n} n;"); 12330 verifyFormat("class alignas(16) Z {\n} n;"); 12331 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 12332 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 12333 12334 // Redefinition from nested context: 12335 verifyFormat("class A::B::C {\n} n;"); 12336 12337 // Template definitions. 12338 verifyFormat( 12339 "template <typename F>\n" 12340 "Matcher(const Matcher<F> &Other,\n" 12341 " typename enable_if_c<is_base_of<F, T>::value &&\n" 12342 " !is_same<F, T>::value>::type * = 0)\n" 12343 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 12344 12345 // FIXME: This is still incorrectly handled at the formatter side. 12346 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 12347 verifyFormat("int i = SomeFunction(a<b, a> b);"); 12348 12349 // FIXME: 12350 // This now gets parsed incorrectly as class definition. 12351 // verifyFormat("class A<int> f() {\n}\nint n;"); 12352 12353 // Elaborate types where incorrectly parsing the structural element would 12354 // break the indent. 12355 verifyFormat("if (true)\n" 12356 " class X x;\n" 12357 "else\n" 12358 " f();\n"); 12359 12360 // This is simply incomplete. Formatting is not important, but must not crash. 12361 verifyFormat("class A:"); 12362 } 12363 12364 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 12365 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 12366 format("#error Leave all white!!!!! space* alone!\n")); 12367 EXPECT_EQ( 12368 "#warning Leave all white!!!!! space* alone!\n", 12369 format("#warning Leave all white!!!!! space* alone!\n")); 12370 EXPECT_EQ("#error 1", format(" # error 1")); 12371 EXPECT_EQ("#warning 1", format(" # warning 1")); 12372 } 12373 12374 TEST_F(FormatTest, FormatHashIfExpressions) { 12375 verifyFormat("#if AAAA && BBBB"); 12376 verifyFormat("#if (AAAA && BBBB)"); 12377 verifyFormat("#elif (AAAA && BBBB)"); 12378 // FIXME: Come up with a better indentation for #elif. 12379 verifyFormat( 12380 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 12381 " defined(BBBBBBBB)\n" 12382 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 12383 " defined(BBBBBBBB)\n" 12384 "#endif", 12385 getLLVMStyleWithColumns(65)); 12386 } 12387 12388 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 12389 FormatStyle AllowsMergedIf = getGoogleStyle(); 12390 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 12391 FormatStyle::SIS_WithoutElse; 12392 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 12393 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 12394 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 12395 EXPECT_EQ("if (true) return 42;", 12396 format("if (true)\nreturn 42;", AllowsMergedIf)); 12397 FormatStyle ShortMergedIf = AllowsMergedIf; 12398 ShortMergedIf.ColumnLimit = 25; 12399 verifyFormat("#define A \\\n" 12400 " if (true) return 42;", 12401 ShortMergedIf); 12402 verifyFormat("#define A \\\n" 12403 " f(); \\\n" 12404 " if (true)\n" 12405 "#define B", 12406 ShortMergedIf); 12407 verifyFormat("#define A \\\n" 12408 " f(); \\\n" 12409 " if (true)\n" 12410 "g();", 12411 ShortMergedIf); 12412 verifyFormat("{\n" 12413 "#ifdef A\n" 12414 " // Comment\n" 12415 " if (true) continue;\n" 12416 "#endif\n" 12417 " // Comment\n" 12418 " if (true) continue;\n" 12419 "}", 12420 ShortMergedIf); 12421 ShortMergedIf.ColumnLimit = 33; 12422 verifyFormat("#define A \\\n" 12423 " if constexpr (true) return 42;", 12424 ShortMergedIf); 12425 verifyFormat("#define A \\\n" 12426 " if CONSTEXPR (true) return 42;", 12427 ShortMergedIf); 12428 ShortMergedIf.ColumnLimit = 29; 12429 verifyFormat("#define A \\\n" 12430 " if (aaaaaaaaaa) return 1; \\\n" 12431 " return 2;", 12432 ShortMergedIf); 12433 ShortMergedIf.ColumnLimit = 28; 12434 verifyFormat("#define A \\\n" 12435 " if (aaaaaaaaaa) \\\n" 12436 " return 1; \\\n" 12437 " return 2;", 12438 ShortMergedIf); 12439 verifyFormat("#define A \\\n" 12440 " if constexpr (aaaaaaa) \\\n" 12441 " return 1; \\\n" 12442 " return 2;", 12443 ShortMergedIf); 12444 verifyFormat("#define A \\\n" 12445 " if CONSTEXPR (aaaaaaa) \\\n" 12446 " return 1; \\\n" 12447 " return 2;", 12448 ShortMergedIf); 12449 } 12450 12451 TEST_F(FormatTest, FormatStarDependingOnContext) { 12452 verifyFormat("void f(int *a);"); 12453 verifyFormat("void f() { f(fint * b); }"); 12454 verifyFormat("class A {\n void f(int *a);\n};"); 12455 verifyFormat("class A {\n int *a;\n};"); 12456 verifyFormat("namespace a {\n" 12457 "namespace b {\n" 12458 "class A {\n" 12459 " void f() {}\n" 12460 " int *a;\n" 12461 "};\n" 12462 "} // namespace b\n" 12463 "} // namespace a"); 12464 } 12465 12466 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 12467 verifyFormat("while"); 12468 verifyFormat("operator"); 12469 } 12470 12471 TEST_F(FormatTest, SkipsDeeplyNestedLines) { 12472 // This code would be painfully slow to format if we didn't skip it. 12473 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 12474 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12475 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12476 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12477 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12478 "A(1, 1)\n" 12479 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x 12480 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12481 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12482 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12483 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12484 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12485 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12486 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12487 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12488 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n"); 12489 // Deeply nested part is untouched, rest is formatted. 12490 EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n", 12491 format(std::string("int i;\n") + Code + "int j;\n", 12492 getLLVMStyle(), SC_ExpectIncomplete)); 12493 } 12494 12495 //===----------------------------------------------------------------------===// 12496 // Objective-C tests. 12497 //===----------------------------------------------------------------------===// 12498 12499 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 12500 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 12501 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 12502 format("-(NSUInteger)indexOfObject:(id)anObject;")); 12503 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 12504 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 12505 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 12506 format("-(NSInteger)Method3:(id)anObject;")); 12507 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 12508 format("-(NSInteger)Method4:(id)anObject;")); 12509 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 12510 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 12511 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 12512 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 12513 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 12514 "forAllCells:(BOOL)flag;", 12515 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 12516 "forAllCells:(BOOL)flag;")); 12517 12518 // Very long objectiveC method declaration. 12519 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 12520 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 12521 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 12522 " inRange:(NSRange)range\n" 12523 " outRange:(NSRange)out_range\n" 12524 " outRange1:(NSRange)out_range1\n" 12525 " outRange2:(NSRange)out_range2\n" 12526 " outRange3:(NSRange)out_range3\n" 12527 " outRange4:(NSRange)out_range4\n" 12528 " outRange5:(NSRange)out_range5\n" 12529 " outRange6:(NSRange)out_range6\n" 12530 " outRange7:(NSRange)out_range7\n" 12531 " outRange8:(NSRange)out_range8\n" 12532 " outRange9:(NSRange)out_range9;"); 12533 12534 // When the function name has to be wrapped. 12535 FormatStyle Style = getLLVMStyle(); 12536 // ObjC ignores IndentWrappedFunctionNames when wrapping methods 12537 // and always indents instead. 12538 Style.IndentWrappedFunctionNames = false; 12539 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 12540 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 12541 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 12542 "}", 12543 Style); 12544 Style.IndentWrappedFunctionNames = true; 12545 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 12546 " veryLooooooooooongName:(NSString)cccccccccccccc\n" 12547 " anotherName:(NSString)dddddddddddddd {\n" 12548 "}", 12549 Style); 12550 12551 verifyFormat("- (int)sum:(vector<int>)numbers;"); 12552 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 12553 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 12554 // protocol lists (but not for template classes): 12555 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 12556 12557 verifyFormat("- (int (*)())foo:(int (*)())f;"); 12558 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 12559 12560 // If there's no return type (very rare in practice!), LLVM and Google style 12561 // agree. 12562 verifyFormat("- foo;"); 12563 verifyFormat("- foo:(int)f;"); 12564 verifyGoogleFormat("- foo:(int)foo;"); 12565 } 12566 12567 TEST_F(FormatTest, BreaksStringLiterals) { 12568 EXPECT_EQ("\"some text \"\n" 12569 "\"other\";", 12570 format("\"some text other\";", getLLVMStyleWithColumns(12))); 12571 EXPECT_EQ("\"some text \"\n" 12572 "\"other\";", 12573 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 12574 EXPECT_EQ( 12575 "#define A \\\n" 12576 " \"some \" \\\n" 12577 " \"text \" \\\n" 12578 " \"other\";", 12579 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 12580 EXPECT_EQ( 12581 "#define A \\\n" 12582 " \"so \" \\\n" 12583 " \"text \" \\\n" 12584 " \"other\";", 12585 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 12586 12587 EXPECT_EQ("\"some text\"", 12588 format("\"some text\"", getLLVMStyleWithColumns(1))); 12589 EXPECT_EQ("\"some text\"", 12590 format("\"some text\"", getLLVMStyleWithColumns(11))); 12591 EXPECT_EQ("\"some \"\n" 12592 "\"text\"", 12593 format("\"some text\"", getLLVMStyleWithColumns(10))); 12594 EXPECT_EQ("\"some \"\n" 12595 "\"text\"", 12596 format("\"some text\"", getLLVMStyleWithColumns(7))); 12597 EXPECT_EQ("\"some\"\n" 12598 "\" tex\"\n" 12599 "\"t\"", 12600 format("\"some text\"", getLLVMStyleWithColumns(6))); 12601 EXPECT_EQ("\"some\"\n" 12602 "\" tex\"\n" 12603 "\" and\"", 12604 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 12605 EXPECT_EQ("\"some\"\n" 12606 "\"/tex\"\n" 12607 "\"/and\"", 12608 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 12609 12610 EXPECT_EQ("variable =\n" 12611 " \"long string \"\n" 12612 " \"literal\";", 12613 format("variable = \"long string literal\";", 12614 getLLVMStyleWithColumns(20))); 12615 12616 EXPECT_EQ("variable = f(\n" 12617 " \"long string \"\n" 12618 " \"literal\",\n" 12619 " short,\n" 12620 " loooooooooooooooooooong);", 12621 format("variable = f(\"long string literal\", short, " 12622 "loooooooooooooooooooong);", 12623 getLLVMStyleWithColumns(20))); 12624 12625 EXPECT_EQ( 12626 "f(g(\"long string \"\n" 12627 " \"literal\"),\n" 12628 " b);", 12629 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 12630 EXPECT_EQ("f(g(\"long string \"\n" 12631 " \"literal\",\n" 12632 " a),\n" 12633 " b);", 12634 format("f(g(\"long string literal\", a), b);", 12635 getLLVMStyleWithColumns(20))); 12636 EXPECT_EQ( 12637 "f(\"one two\".split(\n" 12638 " variable));", 12639 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 12640 EXPECT_EQ("f(\"one two three four five six \"\n" 12641 " \"seven\".split(\n" 12642 " really_looooong_variable));", 12643 format("f(\"one two three four five six seven\"." 12644 "split(really_looooong_variable));", 12645 getLLVMStyleWithColumns(33))); 12646 12647 EXPECT_EQ("f(\"some \"\n" 12648 " \"text\",\n" 12649 " other);", 12650 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 12651 12652 // Only break as a last resort. 12653 verifyFormat( 12654 "aaaaaaaaaaaaaaaaaaaa(\n" 12655 " aaaaaaaaaaaaaaaaaaaa,\n" 12656 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 12657 12658 EXPECT_EQ("\"splitmea\"\n" 12659 "\"trandomp\"\n" 12660 "\"oint\"", 12661 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 12662 12663 EXPECT_EQ("\"split/\"\n" 12664 "\"pathat/\"\n" 12665 "\"slashes\"", 12666 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 12667 12668 EXPECT_EQ("\"split/\"\n" 12669 "\"pathat/\"\n" 12670 "\"slashes\"", 12671 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 12672 EXPECT_EQ("\"split at \"\n" 12673 "\"spaces/at/\"\n" 12674 "\"slashes.at.any$\"\n" 12675 "\"non-alphanumeric%\"\n" 12676 "\"1111111111characte\"\n" 12677 "\"rs\"", 12678 format("\"split at " 12679 "spaces/at/" 12680 "slashes.at." 12681 "any$non-" 12682 "alphanumeric%" 12683 "1111111111characte" 12684 "rs\"", 12685 getLLVMStyleWithColumns(20))); 12686 12687 // Verify that splitting the strings understands 12688 // Style::AlwaysBreakBeforeMultilineStrings. 12689 EXPECT_EQ("aaaaaaaaaaaa(\n" 12690 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 12691 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 12692 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 12693 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 12694 "aaaaaaaaaaaaaaaaaaaaaa\");", 12695 getGoogleStyle())); 12696 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 12697 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 12698 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 12699 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 12700 "aaaaaaaaaaaaaaaaaaaaaa\";", 12701 getGoogleStyle())); 12702 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 12703 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 12704 format("llvm::outs() << " 12705 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 12706 "aaaaaaaaaaaaaaaaaaa\";")); 12707 EXPECT_EQ("ffff(\n" 12708 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 12709 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 12710 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 12711 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 12712 getGoogleStyle())); 12713 12714 FormatStyle Style = getLLVMStyleWithColumns(12); 12715 Style.BreakStringLiterals = false; 12716 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 12717 12718 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 12719 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 12720 EXPECT_EQ("#define A \\\n" 12721 " \"some \" \\\n" 12722 " \"text \" \\\n" 12723 " \"other\";", 12724 format("#define A \"some text other\";", AlignLeft)); 12725 } 12726 12727 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) { 12728 EXPECT_EQ("C a = \"some more \"\n" 12729 " \"text\";", 12730 format("C a = \"some more text\";", getLLVMStyleWithColumns(18))); 12731 } 12732 12733 TEST_F(FormatTest, FullyRemoveEmptyLines) { 12734 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 12735 NoEmptyLines.MaxEmptyLinesToKeep = 0; 12736 EXPECT_EQ("int i = a(b());", 12737 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 12738 } 12739 12740 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 12741 EXPECT_EQ( 12742 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 12743 "(\n" 12744 " \"x\t\");", 12745 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 12746 "aaaaaaa(" 12747 "\"x\t\");")); 12748 } 12749 12750 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 12751 EXPECT_EQ( 12752 "u8\"utf8 string \"\n" 12753 "u8\"literal\";", 12754 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 12755 EXPECT_EQ( 12756 "u\"utf16 string \"\n" 12757 "u\"literal\";", 12758 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 12759 EXPECT_EQ( 12760 "U\"utf32 string \"\n" 12761 "U\"literal\";", 12762 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 12763 EXPECT_EQ("L\"wide string \"\n" 12764 "L\"literal\";", 12765 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 12766 EXPECT_EQ("@\"NSString \"\n" 12767 "@\"literal\";", 12768 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 12769 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26)); 12770 12771 // This input makes clang-format try to split the incomplete unicode escape 12772 // sequence, which used to lead to a crasher. 12773 verifyNoCrash( 12774 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 12775 getLLVMStyleWithColumns(60)); 12776 } 12777 12778 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 12779 FormatStyle Style = getGoogleStyleWithColumns(15); 12780 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 12781 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 12782 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 12783 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 12784 EXPECT_EQ("u8R\"x(raw literal)x\";", 12785 format("u8R\"x(raw literal)x\";", Style)); 12786 } 12787 12788 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 12789 FormatStyle Style = getLLVMStyleWithColumns(20); 12790 EXPECT_EQ( 12791 "_T(\"aaaaaaaaaaaaaa\")\n" 12792 "_T(\"aaaaaaaaaaaaaa\")\n" 12793 "_T(\"aaaaaaaaaaaa\")", 12794 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 12795 EXPECT_EQ("f(x,\n" 12796 " _T(\"aaaaaaaaaaaa\")\n" 12797 " _T(\"aaa\"),\n" 12798 " z);", 12799 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 12800 12801 // FIXME: Handle embedded spaces in one iteration. 12802 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 12803 // "_T(\"aaaaaaaaaaaaa\")\n" 12804 // "_T(\"aaaaaaaaaaaaa\")\n" 12805 // "_T(\"a\")", 12806 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 12807 // getLLVMStyleWithColumns(20))); 12808 EXPECT_EQ( 12809 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 12810 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 12811 EXPECT_EQ("f(\n" 12812 "#if !TEST\n" 12813 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 12814 "#endif\n" 12815 ");", 12816 format("f(\n" 12817 "#if !TEST\n" 12818 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 12819 "#endif\n" 12820 ");")); 12821 EXPECT_EQ("f(\n" 12822 "\n" 12823 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 12824 format("f(\n" 12825 "\n" 12826 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 12827 } 12828 12829 TEST_F(FormatTest, BreaksStringLiteralOperands) { 12830 // In a function call with two operands, the second can be broken with no line 12831 // break before it. 12832 EXPECT_EQ( 12833 "func(a, \"long long \"\n" 12834 " \"long long\");", 12835 format("func(a, \"long long long long\");", getLLVMStyleWithColumns(24))); 12836 // In a function call with three operands, the second must be broken with a 12837 // line break before it. 12838 EXPECT_EQ("func(a,\n" 12839 " \"long long long \"\n" 12840 " \"long\",\n" 12841 " c);", 12842 format("func(a, \"long long long long\", c);", 12843 getLLVMStyleWithColumns(24))); 12844 // In a function call with three operands, the third must be broken with a 12845 // line break before it. 12846 EXPECT_EQ("func(a, b,\n" 12847 " \"long long long \"\n" 12848 " \"long\");", 12849 format("func(a, b, \"long long long long\");", 12850 getLLVMStyleWithColumns(24))); 12851 // In a function call with three operands, both the second and the third must 12852 // be broken with a line break before them. 12853 EXPECT_EQ("func(a,\n" 12854 " \"long long long \"\n" 12855 " \"long\",\n" 12856 " \"long long long \"\n" 12857 " \"long\");", 12858 format("func(a, \"long long long long\", \"long long long long\");", 12859 getLLVMStyleWithColumns(24))); 12860 // In a chain of << with two operands, the second can be broken with no line 12861 // break before it. 12862 EXPECT_EQ("a << \"line line \"\n" 12863 " \"line\";", 12864 format("a << \"line line line\";", getLLVMStyleWithColumns(20))); 12865 // In a chain of << with three operands, the second can be broken with no line 12866 // break before it. 12867 EXPECT_EQ( 12868 "abcde << \"line \"\n" 12869 " \"line line\"\n" 12870 " << c;", 12871 format("abcde << \"line line line\" << c;", getLLVMStyleWithColumns(20))); 12872 // In a chain of << with three operands, the third must be broken with a line 12873 // break before it. 12874 EXPECT_EQ( 12875 "a << b\n" 12876 " << \"line line \"\n" 12877 " \"line\";", 12878 format("a << b << \"line line line\";", getLLVMStyleWithColumns(20))); 12879 // In a chain of << with three operands, the second can be broken with no line 12880 // break before it and the third must be broken with a line break before it. 12881 EXPECT_EQ("abcd << \"line line \"\n" 12882 " \"line\"\n" 12883 " << \"line line \"\n" 12884 " \"line\";", 12885 format("abcd << \"line line line\" << \"line line line\";", 12886 getLLVMStyleWithColumns(20))); 12887 // In a chain of binary operators with two operands, the second can be broken 12888 // with no line break before it. 12889 EXPECT_EQ( 12890 "abcd + \"line line \"\n" 12891 " \"line line\";", 12892 format("abcd + \"line line line line\";", getLLVMStyleWithColumns(20))); 12893 // In a chain of binary operators with three operands, the second must be 12894 // broken with a line break before it. 12895 EXPECT_EQ("abcd +\n" 12896 " \"line line \"\n" 12897 " \"line line\" +\n" 12898 " e;", 12899 format("abcd + \"line line line line\" + e;", 12900 getLLVMStyleWithColumns(20))); 12901 // In a function call with two operands, with AlignAfterOpenBracket enabled, 12902 // the first must be broken with a line break before it. 12903 FormatStyle Style = getLLVMStyleWithColumns(25); 12904 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 12905 EXPECT_EQ("someFunction(\n" 12906 " \"long long long \"\n" 12907 " \"long\",\n" 12908 " a);", 12909 format("someFunction(\"long long long long\", a);", Style)); 12910 } 12911 12912 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 12913 EXPECT_EQ( 12914 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 12915 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 12916 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 12917 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 12918 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 12919 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 12920 } 12921 12922 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 12923 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 12924 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 12925 EXPECT_EQ("fffffffffff(g(R\"x(\n" 12926 "multiline raw string literal xxxxxxxxxxxxxx\n" 12927 ")x\",\n" 12928 " a),\n" 12929 " b);", 12930 format("fffffffffff(g(R\"x(\n" 12931 "multiline raw string literal xxxxxxxxxxxxxx\n" 12932 ")x\", a), b);", 12933 getGoogleStyleWithColumns(20))); 12934 EXPECT_EQ("fffffffffff(\n" 12935 " g(R\"x(qqq\n" 12936 "multiline raw string literal xxxxxxxxxxxxxx\n" 12937 ")x\",\n" 12938 " a),\n" 12939 " b);", 12940 format("fffffffffff(g(R\"x(qqq\n" 12941 "multiline raw string literal xxxxxxxxxxxxxx\n" 12942 ")x\", a), b);", 12943 getGoogleStyleWithColumns(20))); 12944 12945 EXPECT_EQ("fffffffffff(R\"x(\n" 12946 "multiline raw string literal xxxxxxxxxxxxxx\n" 12947 ")x\");", 12948 format("fffffffffff(R\"x(\n" 12949 "multiline raw string literal xxxxxxxxxxxxxx\n" 12950 ")x\");", 12951 getGoogleStyleWithColumns(20))); 12952 EXPECT_EQ("fffffffffff(R\"x(\n" 12953 "multiline raw string literal xxxxxxxxxxxxxx\n" 12954 ")x\" + bbbbbb);", 12955 format("fffffffffff(R\"x(\n" 12956 "multiline raw string literal xxxxxxxxxxxxxx\n" 12957 ")x\" + bbbbbb);", 12958 getGoogleStyleWithColumns(20))); 12959 EXPECT_EQ("fffffffffff(\n" 12960 " R\"x(\n" 12961 "multiline raw string literal xxxxxxxxxxxxxx\n" 12962 ")x\" +\n" 12963 " bbbbbb);", 12964 format("fffffffffff(\n" 12965 " R\"x(\n" 12966 "multiline raw string literal xxxxxxxxxxxxxx\n" 12967 ")x\" + bbbbbb);", 12968 getGoogleStyleWithColumns(20))); 12969 EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);", 12970 format("fffffffffff(\n" 12971 " R\"(single line raw string)\" + bbbbbb);")); 12972 } 12973 12974 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 12975 verifyFormat("string a = \"unterminated;"); 12976 EXPECT_EQ("function(\"unterminated,\n" 12977 " OtherParameter);", 12978 format("function( \"unterminated,\n" 12979 " OtherParameter);")); 12980 } 12981 12982 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 12983 FormatStyle Style = getLLVMStyle(); 12984 Style.Standard = FormatStyle::LS_Cpp03; 12985 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 12986 format("#define x(_a) printf(\"foo\"_a);", Style)); 12987 } 12988 12989 TEST_F(FormatTest, CppLexVersion) { 12990 FormatStyle Style = getLLVMStyle(); 12991 // Formatting of x * y differs if x is a type. 12992 verifyFormat("void foo() { MACRO(a * b); }", Style); 12993 verifyFormat("void foo() { MACRO(int *b); }", Style); 12994 12995 // LLVM style uses latest lexer. 12996 verifyFormat("void foo() { MACRO(char8_t *b); }", Style); 12997 Style.Standard = FormatStyle::LS_Cpp17; 12998 // But in c++17, char8_t isn't a keyword. 12999 verifyFormat("void foo() { MACRO(char8_t * b); }", Style); 13000 } 13001 13002 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 13003 13004 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 13005 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 13006 " \"ddeeefff\");", 13007 format("someFunction(\"aaabbbcccdddeeefff\");", 13008 getLLVMStyleWithColumns(25))); 13009 EXPECT_EQ("someFunction1234567890(\n" 13010 " \"aaabbbcccdddeeefff\");", 13011 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 13012 getLLVMStyleWithColumns(26))); 13013 EXPECT_EQ("someFunction1234567890(\n" 13014 " \"aaabbbcccdddeeeff\"\n" 13015 " \"f\");", 13016 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 13017 getLLVMStyleWithColumns(25))); 13018 EXPECT_EQ("someFunction1234567890(\n" 13019 " \"aaabbbcccdddeeeff\"\n" 13020 " \"f\");", 13021 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 13022 getLLVMStyleWithColumns(24))); 13023 EXPECT_EQ("someFunction(\n" 13024 " \"aaabbbcc ddde \"\n" 13025 " \"efff\");", 13026 format("someFunction(\"aaabbbcc ddde efff\");", 13027 getLLVMStyleWithColumns(25))); 13028 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 13029 " \"ddeeefff\");", 13030 format("someFunction(\"aaabbbccc ddeeefff\");", 13031 getLLVMStyleWithColumns(25))); 13032 EXPECT_EQ("someFunction1234567890(\n" 13033 " \"aaabb \"\n" 13034 " \"cccdddeeefff\");", 13035 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 13036 getLLVMStyleWithColumns(25))); 13037 EXPECT_EQ("#define A \\\n" 13038 " string s = \\\n" 13039 " \"123456789\" \\\n" 13040 " \"0\"; \\\n" 13041 " int i;", 13042 format("#define A string s = \"1234567890\"; int i;", 13043 getLLVMStyleWithColumns(20))); 13044 EXPECT_EQ("someFunction(\n" 13045 " \"aaabbbcc \"\n" 13046 " \"dddeeefff\");", 13047 format("someFunction(\"aaabbbcc dddeeefff\");", 13048 getLLVMStyleWithColumns(25))); 13049 } 13050 13051 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 13052 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 13053 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 13054 EXPECT_EQ("\"test\"\n" 13055 "\"\\n\"", 13056 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 13057 EXPECT_EQ("\"tes\\\\\"\n" 13058 "\"n\"", 13059 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 13060 EXPECT_EQ("\"\\\\\\\\\"\n" 13061 "\"\\n\"", 13062 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 13063 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 13064 EXPECT_EQ("\"\\uff01\"\n" 13065 "\"test\"", 13066 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 13067 EXPECT_EQ("\"\\Uff01ff02\"", 13068 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 13069 EXPECT_EQ("\"\\x000000000001\"\n" 13070 "\"next\"", 13071 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 13072 EXPECT_EQ("\"\\x000000000001next\"", 13073 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 13074 EXPECT_EQ("\"\\x000000000001\"", 13075 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 13076 EXPECT_EQ("\"test\"\n" 13077 "\"\\000000\"\n" 13078 "\"000001\"", 13079 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 13080 EXPECT_EQ("\"test\\000\"\n" 13081 "\"00000000\"\n" 13082 "\"1\"", 13083 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 13084 } 13085 13086 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 13087 verifyFormat("void f() {\n" 13088 " return g() {}\n" 13089 " void h() {}"); 13090 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 13091 "g();\n" 13092 "}"); 13093 } 13094 13095 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 13096 verifyFormat( 13097 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 13098 } 13099 13100 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 13101 verifyFormat("class X {\n" 13102 " void f() {\n" 13103 " }\n" 13104 "};", 13105 getLLVMStyleWithColumns(12)); 13106 } 13107 13108 TEST_F(FormatTest, ConfigurableIndentWidth) { 13109 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 13110 EightIndent.IndentWidth = 8; 13111 EightIndent.ContinuationIndentWidth = 8; 13112 verifyFormat("void f() {\n" 13113 " someFunction();\n" 13114 " if (true) {\n" 13115 " f();\n" 13116 " }\n" 13117 "}", 13118 EightIndent); 13119 verifyFormat("class X {\n" 13120 " void f() {\n" 13121 " }\n" 13122 "};", 13123 EightIndent); 13124 verifyFormat("int x[] = {\n" 13125 " call(),\n" 13126 " call()};", 13127 EightIndent); 13128 } 13129 13130 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 13131 verifyFormat("double\n" 13132 "f();", 13133 getLLVMStyleWithColumns(8)); 13134 } 13135 13136 TEST_F(FormatTest, ConfigurableUseOfTab) { 13137 FormatStyle Tab = getLLVMStyleWithColumns(42); 13138 Tab.IndentWidth = 8; 13139 Tab.UseTab = FormatStyle::UT_Always; 13140 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left; 13141 13142 EXPECT_EQ("if (aaaaaaaa && // q\n" 13143 " bb)\t\t// w\n" 13144 "\t;", 13145 format("if (aaaaaaaa &&// q\n" 13146 "bb)// w\n" 13147 ";", 13148 Tab)); 13149 EXPECT_EQ("if (aaa && bbb) // w\n" 13150 "\t;", 13151 format("if(aaa&&bbb)// w\n" 13152 ";", 13153 Tab)); 13154 13155 verifyFormat("class X {\n" 13156 "\tvoid f() {\n" 13157 "\t\tsomeFunction(parameter1,\n" 13158 "\t\t\t parameter2);\n" 13159 "\t}\n" 13160 "};", 13161 Tab); 13162 verifyFormat("#define A \\\n" 13163 "\tvoid f() { \\\n" 13164 "\t\tsomeFunction( \\\n" 13165 "\t\t parameter1, \\\n" 13166 "\t\t parameter2); \\\n" 13167 "\t}", 13168 Tab); 13169 verifyFormat("int a;\t // x\n" 13170 "int bbbbbbbb; // x\n", 13171 Tab); 13172 13173 Tab.TabWidth = 4; 13174 Tab.IndentWidth = 8; 13175 verifyFormat("class TabWidth4Indent8 {\n" 13176 "\t\tvoid f() {\n" 13177 "\t\t\t\tsomeFunction(parameter1,\n" 13178 "\t\t\t\t\t\t\t parameter2);\n" 13179 "\t\t}\n" 13180 "};", 13181 Tab); 13182 13183 Tab.TabWidth = 4; 13184 Tab.IndentWidth = 4; 13185 verifyFormat("class TabWidth4Indent4 {\n" 13186 "\tvoid f() {\n" 13187 "\t\tsomeFunction(parameter1,\n" 13188 "\t\t\t\t\t parameter2);\n" 13189 "\t}\n" 13190 "};", 13191 Tab); 13192 13193 Tab.TabWidth = 8; 13194 Tab.IndentWidth = 4; 13195 verifyFormat("class TabWidth8Indent4 {\n" 13196 " void f() {\n" 13197 "\tsomeFunction(parameter1,\n" 13198 "\t\t parameter2);\n" 13199 " }\n" 13200 "};", 13201 Tab); 13202 13203 Tab.TabWidth = 8; 13204 Tab.IndentWidth = 8; 13205 EXPECT_EQ("/*\n" 13206 "\t a\t\tcomment\n" 13207 "\t in multiple lines\n" 13208 " */", 13209 format(" /*\t \t \n" 13210 " \t \t a\t\tcomment\t \t\n" 13211 " \t \t in multiple lines\t\n" 13212 " \t */", 13213 Tab)); 13214 13215 Tab.UseTab = FormatStyle::UT_ForIndentation; 13216 verifyFormat("{\n" 13217 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13218 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13219 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13220 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13221 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13222 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13223 "};", 13224 Tab); 13225 verifyFormat("enum AA {\n" 13226 "\ta1, // Force multiple lines\n" 13227 "\ta2,\n" 13228 "\ta3\n" 13229 "};", 13230 Tab); 13231 EXPECT_EQ("if (aaaaaaaa && // q\n" 13232 " bb) // w\n" 13233 "\t;", 13234 format("if (aaaaaaaa &&// q\n" 13235 "bb)// w\n" 13236 ";", 13237 Tab)); 13238 verifyFormat("class X {\n" 13239 "\tvoid f() {\n" 13240 "\t\tsomeFunction(parameter1,\n" 13241 "\t\t parameter2);\n" 13242 "\t}\n" 13243 "};", 13244 Tab); 13245 verifyFormat("{\n" 13246 "\tQ(\n" 13247 "\t {\n" 13248 "\t\t int a;\n" 13249 "\t\t someFunction(aaaaaaaa,\n" 13250 "\t\t bbbbbbb);\n" 13251 "\t },\n" 13252 "\t p);\n" 13253 "}", 13254 Tab); 13255 EXPECT_EQ("{\n" 13256 "\t/* aaaa\n" 13257 "\t bbbb */\n" 13258 "}", 13259 format("{\n" 13260 "/* aaaa\n" 13261 " bbbb */\n" 13262 "}", 13263 Tab)); 13264 EXPECT_EQ("{\n" 13265 "\t/*\n" 13266 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13267 "\t bbbbbbbbbbbbb\n" 13268 "\t*/\n" 13269 "}", 13270 format("{\n" 13271 "/*\n" 13272 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13273 "*/\n" 13274 "}", 13275 Tab)); 13276 EXPECT_EQ("{\n" 13277 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13278 "\t// bbbbbbbbbbbbb\n" 13279 "}", 13280 format("{\n" 13281 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13282 "}", 13283 Tab)); 13284 EXPECT_EQ("{\n" 13285 "\t/*\n" 13286 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13287 "\t bbbbbbbbbbbbb\n" 13288 "\t*/\n" 13289 "}", 13290 format("{\n" 13291 "\t/*\n" 13292 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13293 "\t*/\n" 13294 "}", 13295 Tab)); 13296 EXPECT_EQ("{\n" 13297 "\t/*\n" 13298 "\n" 13299 "\t*/\n" 13300 "}", 13301 format("{\n" 13302 "\t/*\n" 13303 "\n" 13304 "\t*/\n" 13305 "}", 13306 Tab)); 13307 EXPECT_EQ("{\n" 13308 "\t/*\n" 13309 " asdf\n" 13310 "\t*/\n" 13311 "}", 13312 format("{\n" 13313 "\t/*\n" 13314 " asdf\n" 13315 "\t*/\n" 13316 "}", 13317 Tab)); 13318 13319 Tab.UseTab = FormatStyle::UT_Never; 13320 EXPECT_EQ("/*\n" 13321 " a\t\tcomment\n" 13322 " in multiple lines\n" 13323 " */", 13324 format(" /*\t \t \n" 13325 " \t \t a\t\tcomment\t \t\n" 13326 " \t \t in multiple lines\t\n" 13327 " \t */", 13328 Tab)); 13329 EXPECT_EQ("/* some\n" 13330 " comment */", 13331 format(" \t \t /* some\n" 13332 " \t \t comment */", 13333 Tab)); 13334 EXPECT_EQ("int a; /* some\n" 13335 " comment */", 13336 format(" \t \t int a; /* some\n" 13337 " \t \t comment */", 13338 Tab)); 13339 13340 EXPECT_EQ("int a; /* some\n" 13341 "comment */", 13342 format(" \t \t int\ta; /* some\n" 13343 " \t \t comment */", 13344 Tab)); 13345 EXPECT_EQ("f(\"\t\t\"); /* some\n" 13346 " comment */", 13347 format(" \t \t f(\"\t\t\"); /* some\n" 13348 " \t \t comment */", 13349 Tab)); 13350 EXPECT_EQ("{\n" 13351 " /*\n" 13352 " * Comment\n" 13353 " */\n" 13354 " int i;\n" 13355 "}", 13356 format("{\n" 13357 "\t/*\n" 13358 "\t * Comment\n" 13359 "\t */\n" 13360 "\t int i;\n" 13361 "}", 13362 Tab)); 13363 13364 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 13365 Tab.TabWidth = 8; 13366 Tab.IndentWidth = 8; 13367 EXPECT_EQ("if (aaaaaaaa && // q\n" 13368 " bb) // w\n" 13369 "\t;", 13370 format("if (aaaaaaaa &&// q\n" 13371 "bb)// w\n" 13372 ";", 13373 Tab)); 13374 EXPECT_EQ("if (aaa && bbb) // w\n" 13375 "\t;", 13376 format("if(aaa&&bbb)// w\n" 13377 ";", 13378 Tab)); 13379 verifyFormat("class X {\n" 13380 "\tvoid f() {\n" 13381 "\t\tsomeFunction(parameter1,\n" 13382 "\t\t\t parameter2);\n" 13383 "\t}\n" 13384 "};", 13385 Tab); 13386 verifyFormat("#define A \\\n" 13387 "\tvoid f() { \\\n" 13388 "\t\tsomeFunction( \\\n" 13389 "\t\t parameter1, \\\n" 13390 "\t\t parameter2); \\\n" 13391 "\t}", 13392 Tab); 13393 Tab.TabWidth = 4; 13394 Tab.IndentWidth = 8; 13395 verifyFormat("class TabWidth4Indent8 {\n" 13396 "\t\tvoid f() {\n" 13397 "\t\t\t\tsomeFunction(parameter1,\n" 13398 "\t\t\t\t\t\t\t parameter2);\n" 13399 "\t\t}\n" 13400 "};", 13401 Tab); 13402 Tab.TabWidth = 4; 13403 Tab.IndentWidth = 4; 13404 verifyFormat("class TabWidth4Indent4 {\n" 13405 "\tvoid f() {\n" 13406 "\t\tsomeFunction(parameter1,\n" 13407 "\t\t\t\t\t parameter2);\n" 13408 "\t}\n" 13409 "};", 13410 Tab); 13411 Tab.TabWidth = 8; 13412 Tab.IndentWidth = 4; 13413 verifyFormat("class TabWidth8Indent4 {\n" 13414 " void f() {\n" 13415 "\tsomeFunction(parameter1,\n" 13416 "\t\t parameter2);\n" 13417 " }\n" 13418 "};", 13419 Tab); 13420 Tab.TabWidth = 8; 13421 Tab.IndentWidth = 8; 13422 EXPECT_EQ("/*\n" 13423 "\t a\t\tcomment\n" 13424 "\t in multiple lines\n" 13425 " */", 13426 format(" /*\t \t \n" 13427 " \t \t a\t\tcomment\t \t\n" 13428 " \t \t in multiple lines\t\n" 13429 " \t */", 13430 Tab)); 13431 verifyFormat("{\n" 13432 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13433 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13434 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13435 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13436 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13437 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13438 "};", 13439 Tab); 13440 verifyFormat("enum AA {\n" 13441 "\ta1, // Force multiple lines\n" 13442 "\ta2,\n" 13443 "\ta3\n" 13444 "};", 13445 Tab); 13446 EXPECT_EQ("if (aaaaaaaa && // q\n" 13447 " bb) // w\n" 13448 "\t;", 13449 format("if (aaaaaaaa &&// q\n" 13450 "bb)// w\n" 13451 ";", 13452 Tab)); 13453 verifyFormat("class X {\n" 13454 "\tvoid f() {\n" 13455 "\t\tsomeFunction(parameter1,\n" 13456 "\t\t\t parameter2);\n" 13457 "\t}\n" 13458 "};", 13459 Tab); 13460 verifyFormat("{\n" 13461 "\tQ(\n" 13462 "\t {\n" 13463 "\t\t int a;\n" 13464 "\t\t someFunction(aaaaaaaa,\n" 13465 "\t\t\t\t bbbbbbb);\n" 13466 "\t },\n" 13467 "\t p);\n" 13468 "}", 13469 Tab); 13470 EXPECT_EQ("{\n" 13471 "\t/* aaaa\n" 13472 "\t bbbb */\n" 13473 "}", 13474 format("{\n" 13475 "/* aaaa\n" 13476 " bbbb */\n" 13477 "}", 13478 Tab)); 13479 EXPECT_EQ("{\n" 13480 "\t/*\n" 13481 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13482 "\t bbbbbbbbbbbbb\n" 13483 "\t*/\n" 13484 "}", 13485 format("{\n" 13486 "/*\n" 13487 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13488 "*/\n" 13489 "}", 13490 Tab)); 13491 EXPECT_EQ("{\n" 13492 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13493 "\t// bbbbbbbbbbbbb\n" 13494 "}", 13495 format("{\n" 13496 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13497 "}", 13498 Tab)); 13499 EXPECT_EQ("{\n" 13500 "\t/*\n" 13501 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13502 "\t bbbbbbbbbbbbb\n" 13503 "\t*/\n" 13504 "}", 13505 format("{\n" 13506 "\t/*\n" 13507 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13508 "\t*/\n" 13509 "}", 13510 Tab)); 13511 EXPECT_EQ("{\n" 13512 "\t/*\n" 13513 "\n" 13514 "\t*/\n" 13515 "}", 13516 format("{\n" 13517 "\t/*\n" 13518 "\n" 13519 "\t*/\n" 13520 "}", 13521 Tab)); 13522 EXPECT_EQ("{\n" 13523 "\t/*\n" 13524 " asdf\n" 13525 "\t*/\n" 13526 "}", 13527 format("{\n" 13528 "\t/*\n" 13529 " asdf\n" 13530 "\t*/\n" 13531 "}", 13532 Tab)); 13533 EXPECT_EQ("/* some\n" 13534 " comment */", 13535 format(" \t \t /* some\n" 13536 " \t \t comment */", 13537 Tab)); 13538 EXPECT_EQ("int a; /* some\n" 13539 " comment */", 13540 format(" \t \t int a; /* some\n" 13541 " \t \t comment */", 13542 Tab)); 13543 EXPECT_EQ("int a; /* some\n" 13544 "comment */", 13545 format(" \t \t int\ta; /* some\n" 13546 " \t \t comment */", 13547 Tab)); 13548 EXPECT_EQ("f(\"\t\t\"); /* some\n" 13549 " comment */", 13550 format(" \t \t f(\"\t\t\"); /* some\n" 13551 " \t \t comment */", 13552 Tab)); 13553 EXPECT_EQ("{\n" 13554 "\t/*\n" 13555 "\t * Comment\n" 13556 "\t */\n" 13557 "\tint i;\n" 13558 "}", 13559 format("{\n" 13560 "\t/*\n" 13561 "\t * Comment\n" 13562 "\t */\n" 13563 "\t int i;\n" 13564 "}", 13565 Tab)); 13566 Tab.TabWidth = 2; 13567 Tab.IndentWidth = 2; 13568 EXPECT_EQ("{\n" 13569 "\t/* aaaa\n" 13570 "\t\t bbbb */\n" 13571 "}", 13572 format("{\n" 13573 "/* aaaa\n" 13574 "\t bbbb */\n" 13575 "}", 13576 Tab)); 13577 EXPECT_EQ("{\n" 13578 "\t/*\n" 13579 "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13580 "\t\tbbbbbbbbbbbbb\n" 13581 "\t*/\n" 13582 "}", 13583 format("{\n" 13584 "/*\n" 13585 "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13586 "*/\n" 13587 "}", 13588 Tab)); 13589 Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 13590 Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 13591 Tab.TabWidth = 4; 13592 Tab.IndentWidth = 4; 13593 verifyFormat("class Assign {\n" 13594 "\tvoid f() {\n" 13595 "\t\tint x = 123;\n" 13596 "\t\tint random = 4;\n" 13597 "\t\tstd::string alphabet =\n" 13598 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 13599 "\t}\n" 13600 "};", 13601 Tab); 13602 13603 Tab.UseTab = FormatStyle::UT_AlignWithSpaces; 13604 Tab.TabWidth = 8; 13605 Tab.IndentWidth = 8; 13606 EXPECT_EQ("if (aaaaaaaa && // q\n" 13607 " bb) // w\n" 13608 "\t;", 13609 format("if (aaaaaaaa &&// q\n" 13610 "bb)// w\n" 13611 ";", 13612 Tab)); 13613 EXPECT_EQ("if (aaa && bbb) // w\n" 13614 "\t;", 13615 format("if(aaa&&bbb)// w\n" 13616 ";", 13617 Tab)); 13618 verifyFormat("class X {\n" 13619 "\tvoid f() {\n" 13620 "\t\tsomeFunction(parameter1,\n" 13621 "\t\t parameter2);\n" 13622 "\t}\n" 13623 "};", 13624 Tab); 13625 verifyFormat("#define A \\\n" 13626 "\tvoid f() { \\\n" 13627 "\t\tsomeFunction( \\\n" 13628 "\t\t parameter1, \\\n" 13629 "\t\t parameter2); \\\n" 13630 "\t}", 13631 Tab); 13632 Tab.TabWidth = 4; 13633 Tab.IndentWidth = 8; 13634 verifyFormat("class TabWidth4Indent8 {\n" 13635 "\t\tvoid f() {\n" 13636 "\t\t\t\tsomeFunction(parameter1,\n" 13637 "\t\t\t\t parameter2);\n" 13638 "\t\t}\n" 13639 "};", 13640 Tab); 13641 Tab.TabWidth = 4; 13642 Tab.IndentWidth = 4; 13643 verifyFormat("class TabWidth4Indent4 {\n" 13644 "\tvoid f() {\n" 13645 "\t\tsomeFunction(parameter1,\n" 13646 "\t\t parameter2);\n" 13647 "\t}\n" 13648 "};", 13649 Tab); 13650 Tab.TabWidth = 8; 13651 Tab.IndentWidth = 4; 13652 verifyFormat("class TabWidth8Indent4 {\n" 13653 " void f() {\n" 13654 "\tsomeFunction(parameter1,\n" 13655 "\t parameter2);\n" 13656 " }\n" 13657 "};", 13658 Tab); 13659 Tab.TabWidth = 8; 13660 Tab.IndentWidth = 8; 13661 EXPECT_EQ("/*\n" 13662 " a\t\tcomment\n" 13663 " in multiple lines\n" 13664 " */", 13665 format(" /*\t \t \n" 13666 " \t \t a\t\tcomment\t \t\n" 13667 " \t \t in multiple lines\t\n" 13668 " \t */", 13669 Tab)); 13670 verifyFormat("{\n" 13671 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13672 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13673 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13674 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13675 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13676 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13677 "};", 13678 Tab); 13679 verifyFormat("enum AA {\n" 13680 "\ta1, // Force multiple lines\n" 13681 "\ta2,\n" 13682 "\ta3\n" 13683 "};", 13684 Tab); 13685 EXPECT_EQ("if (aaaaaaaa && // q\n" 13686 " bb) // w\n" 13687 "\t;", 13688 format("if (aaaaaaaa &&// q\n" 13689 "bb)// w\n" 13690 ";", 13691 Tab)); 13692 verifyFormat("class X {\n" 13693 "\tvoid f() {\n" 13694 "\t\tsomeFunction(parameter1,\n" 13695 "\t\t parameter2);\n" 13696 "\t}\n" 13697 "};", 13698 Tab); 13699 verifyFormat("{\n" 13700 "\tQ(\n" 13701 "\t {\n" 13702 "\t\t int a;\n" 13703 "\t\t someFunction(aaaaaaaa,\n" 13704 "\t\t bbbbbbb);\n" 13705 "\t },\n" 13706 "\t p);\n" 13707 "}", 13708 Tab); 13709 EXPECT_EQ("{\n" 13710 "\t/* aaaa\n" 13711 "\t bbbb */\n" 13712 "}", 13713 format("{\n" 13714 "/* aaaa\n" 13715 " bbbb */\n" 13716 "}", 13717 Tab)); 13718 EXPECT_EQ("{\n" 13719 "\t/*\n" 13720 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13721 "\t bbbbbbbbbbbbb\n" 13722 "\t*/\n" 13723 "}", 13724 format("{\n" 13725 "/*\n" 13726 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13727 "*/\n" 13728 "}", 13729 Tab)); 13730 EXPECT_EQ("{\n" 13731 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13732 "\t// bbbbbbbbbbbbb\n" 13733 "}", 13734 format("{\n" 13735 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13736 "}", 13737 Tab)); 13738 EXPECT_EQ("{\n" 13739 "\t/*\n" 13740 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13741 "\t bbbbbbbbbbbbb\n" 13742 "\t*/\n" 13743 "}", 13744 format("{\n" 13745 "\t/*\n" 13746 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13747 "\t*/\n" 13748 "}", 13749 Tab)); 13750 EXPECT_EQ("{\n" 13751 "\t/*\n" 13752 "\n" 13753 "\t*/\n" 13754 "}", 13755 format("{\n" 13756 "\t/*\n" 13757 "\n" 13758 "\t*/\n" 13759 "}", 13760 Tab)); 13761 EXPECT_EQ("{\n" 13762 "\t/*\n" 13763 " asdf\n" 13764 "\t*/\n" 13765 "}", 13766 format("{\n" 13767 "\t/*\n" 13768 " asdf\n" 13769 "\t*/\n" 13770 "}", 13771 Tab)); 13772 EXPECT_EQ("/* some\n" 13773 " comment */", 13774 format(" \t \t /* some\n" 13775 " \t \t comment */", 13776 Tab)); 13777 EXPECT_EQ("int a; /* some\n" 13778 " comment */", 13779 format(" \t \t int a; /* some\n" 13780 " \t \t comment */", 13781 Tab)); 13782 EXPECT_EQ("int a; /* some\n" 13783 "comment */", 13784 format(" \t \t int\ta; /* some\n" 13785 " \t \t comment */", 13786 Tab)); 13787 EXPECT_EQ("f(\"\t\t\"); /* some\n" 13788 " comment */", 13789 format(" \t \t f(\"\t\t\"); /* some\n" 13790 " \t \t comment */", 13791 Tab)); 13792 EXPECT_EQ("{\n" 13793 "\t/*\n" 13794 "\t * Comment\n" 13795 "\t */\n" 13796 "\tint i;\n" 13797 "}", 13798 format("{\n" 13799 "\t/*\n" 13800 "\t * Comment\n" 13801 "\t */\n" 13802 "\t int i;\n" 13803 "}", 13804 Tab)); 13805 Tab.TabWidth = 2; 13806 Tab.IndentWidth = 2; 13807 EXPECT_EQ("{\n" 13808 "\t/* aaaa\n" 13809 "\t bbbb */\n" 13810 "}", 13811 format("{\n" 13812 "/* aaaa\n" 13813 " bbbb */\n" 13814 "}", 13815 Tab)); 13816 EXPECT_EQ("{\n" 13817 "\t/*\n" 13818 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13819 "\t bbbbbbbbbbbbb\n" 13820 "\t*/\n" 13821 "}", 13822 format("{\n" 13823 "/*\n" 13824 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13825 "*/\n" 13826 "}", 13827 Tab)); 13828 Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 13829 Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 13830 Tab.TabWidth = 4; 13831 Tab.IndentWidth = 4; 13832 verifyFormat("class Assign {\n" 13833 "\tvoid f() {\n" 13834 "\t\tint x = 123;\n" 13835 "\t\tint random = 4;\n" 13836 "\t\tstd::string alphabet =\n" 13837 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 13838 "\t}\n" 13839 "};", 13840 Tab); 13841 Tab.AlignOperands = FormatStyle::OAS_Align; 13842 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n" 13843 " cccccccccccccccccccc;", 13844 Tab); 13845 // no alignment 13846 verifyFormat("int aaaaaaaaaa =\n" 13847 "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 13848 Tab); 13849 verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n" 13850 " : bbbbbbbbbbbbbb ? 222222222222222\n" 13851 " : 333333333333333;", 13852 Tab); 13853 Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 13854 Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator; 13855 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n" 13856 " + cccccccccccccccccccc;", 13857 Tab); 13858 } 13859 13860 TEST_F(FormatTest, ZeroTabWidth) { 13861 FormatStyle Tab = getLLVMStyleWithColumns(42); 13862 Tab.IndentWidth = 8; 13863 Tab.UseTab = FormatStyle::UT_Never; 13864 Tab.TabWidth = 0; 13865 EXPECT_EQ("void a(){\n" 13866 " // line starts with '\t'\n" 13867 "};", 13868 format("void a(){\n" 13869 "\t// line starts with '\t'\n" 13870 "};", 13871 Tab)); 13872 13873 EXPECT_EQ("void a(){\n" 13874 " // line starts with '\t'\n" 13875 "};", 13876 format("void a(){\n" 13877 "\t\t// line starts with '\t'\n" 13878 "};", 13879 Tab)); 13880 13881 Tab.UseTab = FormatStyle::UT_ForIndentation; 13882 EXPECT_EQ("void a(){\n" 13883 " // line starts with '\t'\n" 13884 "};", 13885 format("void a(){\n" 13886 "\t// line starts with '\t'\n" 13887 "};", 13888 Tab)); 13889 13890 EXPECT_EQ("void a(){\n" 13891 " // line starts with '\t'\n" 13892 "};", 13893 format("void a(){\n" 13894 "\t\t// line starts with '\t'\n" 13895 "};", 13896 Tab)); 13897 13898 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 13899 EXPECT_EQ("void a(){\n" 13900 " // line starts with '\t'\n" 13901 "};", 13902 format("void a(){\n" 13903 "\t// line starts with '\t'\n" 13904 "};", 13905 Tab)); 13906 13907 EXPECT_EQ("void a(){\n" 13908 " // line starts with '\t'\n" 13909 "};", 13910 format("void a(){\n" 13911 "\t\t// line starts with '\t'\n" 13912 "};", 13913 Tab)); 13914 13915 Tab.UseTab = FormatStyle::UT_AlignWithSpaces; 13916 EXPECT_EQ("void a(){\n" 13917 " // line starts with '\t'\n" 13918 "};", 13919 format("void a(){\n" 13920 "\t// line starts with '\t'\n" 13921 "};", 13922 Tab)); 13923 13924 EXPECT_EQ("void a(){\n" 13925 " // line starts with '\t'\n" 13926 "};", 13927 format("void a(){\n" 13928 "\t\t// line starts with '\t'\n" 13929 "};", 13930 Tab)); 13931 13932 Tab.UseTab = FormatStyle::UT_Always; 13933 EXPECT_EQ("void a(){\n" 13934 "// line starts with '\t'\n" 13935 "};", 13936 format("void a(){\n" 13937 "\t// line starts with '\t'\n" 13938 "};", 13939 Tab)); 13940 13941 EXPECT_EQ("void a(){\n" 13942 "// line starts with '\t'\n" 13943 "};", 13944 format("void a(){\n" 13945 "\t\t// line starts with '\t'\n" 13946 "};", 13947 Tab)); 13948 } 13949 13950 TEST_F(FormatTest, CalculatesOriginalColumn) { 13951 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13952 "q\"; /* some\n" 13953 " comment */", 13954 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13955 "q\"; /* some\n" 13956 " comment */", 13957 getLLVMStyle())); 13958 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 13959 "/* some\n" 13960 " comment */", 13961 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 13962 " /* some\n" 13963 " comment */", 13964 getLLVMStyle())); 13965 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13966 "qqq\n" 13967 "/* some\n" 13968 " comment */", 13969 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13970 "qqq\n" 13971 " /* some\n" 13972 " comment */", 13973 getLLVMStyle())); 13974 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13975 "wwww; /* some\n" 13976 " comment */", 13977 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13978 "wwww; /* some\n" 13979 " comment */", 13980 getLLVMStyle())); 13981 } 13982 13983 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 13984 FormatStyle NoSpace = getLLVMStyle(); 13985 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 13986 13987 verifyFormat("while(true)\n" 13988 " continue;", 13989 NoSpace); 13990 verifyFormat("for(;;)\n" 13991 " continue;", 13992 NoSpace); 13993 verifyFormat("if(true)\n" 13994 " f();\n" 13995 "else if(true)\n" 13996 " f();", 13997 NoSpace); 13998 verifyFormat("do {\n" 13999 " do_something();\n" 14000 "} while(something());", 14001 NoSpace); 14002 verifyFormat("switch(x) {\n" 14003 "default:\n" 14004 " break;\n" 14005 "}", 14006 NoSpace); 14007 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 14008 verifyFormat("size_t x = sizeof(x);", NoSpace); 14009 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 14010 verifyFormat("auto f(int x) -> typeof(x);", NoSpace); 14011 verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace); 14012 verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace); 14013 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 14014 verifyFormat("alignas(128) char a[128];", NoSpace); 14015 verifyFormat("size_t x = alignof(MyType);", NoSpace); 14016 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 14017 verifyFormat("int f() throw(Deprecated);", NoSpace); 14018 verifyFormat("typedef void (*cb)(int);", NoSpace); 14019 verifyFormat("T A::operator()();", NoSpace); 14020 verifyFormat("X A::operator++(T);", NoSpace); 14021 verifyFormat("auto lambda = []() { return 0; };", NoSpace); 14022 14023 FormatStyle Space = getLLVMStyle(); 14024 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 14025 14026 verifyFormat("int f ();", Space); 14027 verifyFormat("void f (int a, T b) {\n" 14028 " while (true)\n" 14029 " continue;\n" 14030 "}", 14031 Space); 14032 verifyFormat("if (true)\n" 14033 " f ();\n" 14034 "else if (true)\n" 14035 " f ();", 14036 Space); 14037 verifyFormat("do {\n" 14038 " do_something ();\n" 14039 "} while (something ());", 14040 Space); 14041 verifyFormat("switch (x) {\n" 14042 "default:\n" 14043 " break;\n" 14044 "}", 14045 Space); 14046 verifyFormat("A::A () : a (1) {}", Space); 14047 verifyFormat("void f () __attribute__ ((asdf));", Space); 14048 verifyFormat("*(&a + 1);\n" 14049 "&((&a)[1]);\n" 14050 "a[(b + c) * d];\n" 14051 "(((a + 1) * 2) + 3) * 4;", 14052 Space); 14053 verifyFormat("#define A(x) x", Space); 14054 verifyFormat("#define A (x) x", Space); 14055 verifyFormat("#if defined(x)\n" 14056 "#endif", 14057 Space); 14058 verifyFormat("auto i = std::make_unique<int> (5);", Space); 14059 verifyFormat("size_t x = sizeof (x);", Space); 14060 verifyFormat("auto f (int x) -> decltype (x);", Space); 14061 verifyFormat("auto f (int x) -> typeof (x);", Space); 14062 verifyFormat("auto f (int x) -> _Atomic (x);", Space); 14063 verifyFormat("auto f (int x) -> __underlying_type (x);", Space); 14064 verifyFormat("int f (T x) noexcept (x.create ());", Space); 14065 verifyFormat("alignas (128) char a[128];", Space); 14066 verifyFormat("size_t x = alignof (MyType);", Space); 14067 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 14068 verifyFormat("int f () throw (Deprecated);", Space); 14069 verifyFormat("typedef void (*cb) (int);", Space); 14070 verifyFormat("T A::operator() ();", Space); 14071 verifyFormat("X A::operator++ (T);", Space); 14072 verifyFormat("auto lambda = [] () { return 0; };", Space); 14073 verifyFormat("int x = int (y);", Space); 14074 14075 FormatStyle SomeSpace = getLLVMStyle(); 14076 SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses; 14077 14078 verifyFormat("[]() -> float {}", SomeSpace); 14079 verifyFormat("[] (auto foo) {}", SomeSpace); 14080 verifyFormat("[foo]() -> int {}", SomeSpace); 14081 verifyFormat("int f();", SomeSpace); 14082 verifyFormat("void f (int a, T b) {\n" 14083 " while (true)\n" 14084 " continue;\n" 14085 "}", 14086 SomeSpace); 14087 verifyFormat("if (true)\n" 14088 " f();\n" 14089 "else if (true)\n" 14090 " f();", 14091 SomeSpace); 14092 verifyFormat("do {\n" 14093 " do_something();\n" 14094 "} while (something());", 14095 SomeSpace); 14096 verifyFormat("switch (x) {\n" 14097 "default:\n" 14098 " break;\n" 14099 "}", 14100 SomeSpace); 14101 verifyFormat("A::A() : a (1) {}", SomeSpace); 14102 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace); 14103 verifyFormat("*(&a + 1);\n" 14104 "&((&a)[1]);\n" 14105 "a[(b + c) * d];\n" 14106 "(((a + 1) * 2) + 3) * 4;", 14107 SomeSpace); 14108 verifyFormat("#define A(x) x", SomeSpace); 14109 verifyFormat("#define A (x) x", SomeSpace); 14110 verifyFormat("#if defined(x)\n" 14111 "#endif", 14112 SomeSpace); 14113 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace); 14114 verifyFormat("size_t x = sizeof (x);", SomeSpace); 14115 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace); 14116 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace); 14117 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace); 14118 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace); 14119 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace); 14120 verifyFormat("alignas (128) char a[128];", SomeSpace); 14121 verifyFormat("size_t x = alignof (MyType);", SomeSpace); 14122 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", 14123 SomeSpace); 14124 verifyFormat("int f() throw (Deprecated);", SomeSpace); 14125 verifyFormat("typedef void (*cb) (int);", SomeSpace); 14126 verifyFormat("T A::operator()();", SomeSpace); 14127 verifyFormat("X A::operator++ (T);", SomeSpace); 14128 verifyFormat("int x = int (y);", SomeSpace); 14129 verifyFormat("auto lambda = []() { return 0; };", SomeSpace); 14130 } 14131 14132 TEST_F(FormatTest, SpaceAfterLogicalNot) { 14133 FormatStyle Spaces = getLLVMStyle(); 14134 Spaces.SpaceAfterLogicalNot = true; 14135 14136 verifyFormat("bool x = ! y", Spaces); 14137 verifyFormat("if (! isFailure())", Spaces); 14138 verifyFormat("if (! (a && b))", Spaces); 14139 verifyFormat("\"Error!\"", Spaces); 14140 verifyFormat("! ! x", Spaces); 14141 } 14142 14143 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 14144 FormatStyle Spaces = getLLVMStyle(); 14145 14146 Spaces.SpacesInParentheses = true; 14147 verifyFormat("do_something( ::globalVar );", Spaces); 14148 verifyFormat("call( x, y, z );", Spaces); 14149 verifyFormat("call();", Spaces); 14150 verifyFormat("std::function<void( int, int )> callback;", Spaces); 14151 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 14152 Spaces); 14153 verifyFormat("while ( (bool)1 )\n" 14154 " continue;", 14155 Spaces); 14156 verifyFormat("for ( ;; )\n" 14157 " continue;", 14158 Spaces); 14159 verifyFormat("if ( true )\n" 14160 " f();\n" 14161 "else if ( true )\n" 14162 " f();", 14163 Spaces); 14164 verifyFormat("do {\n" 14165 " do_something( (int)i );\n" 14166 "} while ( something() );", 14167 Spaces); 14168 verifyFormat("switch ( x ) {\n" 14169 "default:\n" 14170 " break;\n" 14171 "}", 14172 Spaces); 14173 14174 Spaces.SpacesInParentheses = false; 14175 Spaces.SpacesInCStyleCastParentheses = true; 14176 verifyFormat("Type *A = ( Type * )P;", Spaces); 14177 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 14178 verifyFormat("x = ( int32 )y;", Spaces); 14179 verifyFormat("int a = ( int )(2.0f);", Spaces); 14180 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 14181 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 14182 verifyFormat("#define x (( int )-1)", Spaces); 14183 14184 // Run the first set of tests again with: 14185 Spaces.SpacesInParentheses = false; 14186 Spaces.SpaceInEmptyParentheses = true; 14187 Spaces.SpacesInCStyleCastParentheses = true; 14188 verifyFormat("call(x, y, z);", Spaces); 14189 verifyFormat("call( );", Spaces); 14190 verifyFormat("std::function<void(int, int)> callback;", Spaces); 14191 verifyFormat("while (( bool )1)\n" 14192 " continue;", 14193 Spaces); 14194 verifyFormat("for (;;)\n" 14195 " continue;", 14196 Spaces); 14197 verifyFormat("if (true)\n" 14198 " f( );\n" 14199 "else if (true)\n" 14200 " f( );", 14201 Spaces); 14202 verifyFormat("do {\n" 14203 " do_something(( int )i);\n" 14204 "} while (something( ));", 14205 Spaces); 14206 verifyFormat("switch (x) {\n" 14207 "default:\n" 14208 " break;\n" 14209 "}", 14210 Spaces); 14211 14212 // Run the first set of tests again with: 14213 Spaces.SpaceAfterCStyleCast = true; 14214 verifyFormat("call(x, y, z);", Spaces); 14215 verifyFormat("call( );", Spaces); 14216 verifyFormat("std::function<void(int, int)> callback;", Spaces); 14217 verifyFormat("while (( bool ) 1)\n" 14218 " continue;", 14219 Spaces); 14220 verifyFormat("for (;;)\n" 14221 " continue;", 14222 Spaces); 14223 verifyFormat("if (true)\n" 14224 " f( );\n" 14225 "else if (true)\n" 14226 " f( );", 14227 Spaces); 14228 verifyFormat("do {\n" 14229 " do_something(( int ) i);\n" 14230 "} while (something( ));", 14231 Spaces); 14232 verifyFormat("switch (x) {\n" 14233 "default:\n" 14234 " break;\n" 14235 "}", 14236 Spaces); 14237 14238 // Run subset of tests again with: 14239 Spaces.SpacesInCStyleCastParentheses = false; 14240 Spaces.SpaceAfterCStyleCast = true; 14241 verifyFormat("while ((bool) 1)\n" 14242 " continue;", 14243 Spaces); 14244 verifyFormat("do {\n" 14245 " do_something((int) i);\n" 14246 "} while (something( ));", 14247 Spaces); 14248 14249 verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces); 14250 verifyFormat("size_t idx = (size_t) a;", Spaces); 14251 verifyFormat("size_t idx = (size_t) (a - 1);", Spaces); 14252 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); 14253 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); 14254 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); 14255 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); 14256 Spaces.ColumnLimit = 80; 14257 Spaces.IndentWidth = 4; 14258 Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 14259 verifyFormat("void foo( ) {\n" 14260 " size_t foo = (*(function))(\n" 14261 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " 14262 "BarrrrrrrrrrrrLong,\n" 14263 " FoooooooooLooooong);\n" 14264 "}", 14265 Spaces); 14266 Spaces.SpaceAfterCStyleCast = false; 14267 verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces); 14268 verifyFormat("size_t idx = (size_t)a;", Spaces); 14269 verifyFormat("size_t idx = (size_t)(a - 1);", Spaces); 14270 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); 14271 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); 14272 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); 14273 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); 14274 14275 verifyFormat("void foo( ) {\n" 14276 " size_t foo = (*(function))(\n" 14277 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " 14278 "BarrrrrrrrrrrrLong,\n" 14279 " FoooooooooLooooong);\n" 14280 "}", 14281 Spaces); 14282 } 14283 14284 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 14285 verifyFormat("int a[5];"); 14286 verifyFormat("a[3] += 42;"); 14287 14288 FormatStyle Spaces = getLLVMStyle(); 14289 Spaces.SpacesInSquareBrackets = true; 14290 // Not lambdas. 14291 verifyFormat("int a[ 5 ];", Spaces); 14292 verifyFormat("a[ 3 ] += 42;", Spaces); 14293 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 14294 verifyFormat("double &operator[](int i) { return 0; }\n" 14295 "int i;", 14296 Spaces); 14297 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 14298 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 14299 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 14300 // Lambdas. 14301 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 14302 verifyFormat("return [ i, args... ] {};", Spaces); 14303 verifyFormat("int foo = [ &bar ]() {};", Spaces); 14304 verifyFormat("int foo = [ = ]() {};", Spaces); 14305 verifyFormat("int foo = [ & ]() {};", Spaces); 14306 verifyFormat("int foo = [ =, &bar ]() {};", Spaces); 14307 verifyFormat("int foo = [ &bar, = ]() {};", Spaces); 14308 } 14309 14310 TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) { 14311 FormatStyle NoSpaceStyle = getLLVMStyle(); 14312 verifyFormat("int a[5];", NoSpaceStyle); 14313 verifyFormat("a[3] += 42;", NoSpaceStyle); 14314 14315 verifyFormat("int a[1];", NoSpaceStyle); 14316 verifyFormat("int 1 [a];", NoSpaceStyle); 14317 verifyFormat("int a[1][2];", NoSpaceStyle); 14318 verifyFormat("a[7] = 5;", NoSpaceStyle); 14319 verifyFormat("int a = (f())[23];", NoSpaceStyle); 14320 verifyFormat("f([] {})", NoSpaceStyle); 14321 14322 FormatStyle Space = getLLVMStyle(); 14323 Space.SpaceBeforeSquareBrackets = true; 14324 verifyFormat("int c = []() -> int { return 2; }();\n", Space); 14325 verifyFormat("return [i, args...] {};", Space); 14326 14327 verifyFormat("int a [5];", Space); 14328 verifyFormat("a [3] += 42;", Space); 14329 verifyFormat("constexpr char hello []{\"hello\"};", Space); 14330 verifyFormat("double &operator[](int i) { return 0; }\n" 14331 "int i;", 14332 Space); 14333 verifyFormat("std::unique_ptr<int []> foo() {}", Space); 14334 verifyFormat("int i = a [a][a]->f();", Space); 14335 verifyFormat("int i = (*b) [a]->f();", Space); 14336 14337 verifyFormat("int a [1];", Space); 14338 verifyFormat("int 1 [a];", Space); 14339 verifyFormat("int a [1][2];", Space); 14340 verifyFormat("a [7] = 5;", Space); 14341 verifyFormat("int a = (f()) [23];", Space); 14342 verifyFormat("f([] {})", Space); 14343 } 14344 14345 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 14346 verifyFormat("int a = 5;"); 14347 verifyFormat("a += 42;"); 14348 verifyFormat("a or_eq 8;"); 14349 14350 FormatStyle Spaces = getLLVMStyle(); 14351 Spaces.SpaceBeforeAssignmentOperators = false; 14352 verifyFormat("int a= 5;", Spaces); 14353 verifyFormat("a+= 42;", Spaces); 14354 verifyFormat("a or_eq 8;", Spaces); 14355 } 14356 14357 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) { 14358 verifyFormat("class Foo : public Bar {};"); 14359 verifyFormat("Foo::Foo() : foo(1) {}"); 14360 verifyFormat("for (auto a : b) {\n}"); 14361 verifyFormat("int x = a ? b : c;"); 14362 verifyFormat("{\n" 14363 "label0:\n" 14364 " int x = 0;\n" 14365 "}"); 14366 verifyFormat("switch (x) {\n" 14367 "case 1:\n" 14368 "default:\n" 14369 "}"); 14370 verifyFormat("switch (allBraces) {\n" 14371 "case 1: {\n" 14372 " break;\n" 14373 "}\n" 14374 "case 2: {\n" 14375 " [[fallthrough]];\n" 14376 "}\n" 14377 "default: {\n" 14378 " break;\n" 14379 "}\n" 14380 "}"); 14381 14382 FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30); 14383 CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false; 14384 verifyFormat("class Foo : public Bar {};", CtorInitializerStyle); 14385 verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle); 14386 verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle); 14387 verifyFormat("int x = a ? b : c;", CtorInitializerStyle); 14388 verifyFormat("{\n" 14389 "label1:\n" 14390 " int x = 0;\n" 14391 "}", 14392 CtorInitializerStyle); 14393 verifyFormat("switch (x) {\n" 14394 "case 1:\n" 14395 "default:\n" 14396 "}", 14397 CtorInitializerStyle); 14398 verifyFormat("switch (allBraces) {\n" 14399 "case 1: {\n" 14400 " break;\n" 14401 "}\n" 14402 "case 2: {\n" 14403 " [[fallthrough]];\n" 14404 "}\n" 14405 "default: {\n" 14406 " break;\n" 14407 "}\n" 14408 "}", 14409 CtorInitializerStyle); 14410 CtorInitializerStyle.BreakConstructorInitializers = 14411 FormatStyle::BCIS_AfterColon; 14412 verifyFormat("Fooooooooooo::Fooooooooooo():\n" 14413 " aaaaaaaaaaaaaaaa(1),\n" 14414 " bbbbbbbbbbbbbbbb(2) {}", 14415 CtorInitializerStyle); 14416 CtorInitializerStyle.BreakConstructorInitializers = 14417 FormatStyle::BCIS_BeforeComma; 14418 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 14419 " : aaaaaaaaaaaaaaaa(1)\n" 14420 " , bbbbbbbbbbbbbbbb(2) {}", 14421 CtorInitializerStyle); 14422 CtorInitializerStyle.BreakConstructorInitializers = 14423 FormatStyle::BCIS_BeforeColon; 14424 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 14425 " : aaaaaaaaaaaaaaaa(1),\n" 14426 " bbbbbbbbbbbbbbbb(2) {}", 14427 CtorInitializerStyle); 14428 CtorInitializerStyle.ConstructorInitializerIndentWidth = 0; 14429 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 14430 ": aaaaaaaaaaaaaaaa(1),\n" 14431 " bbbbbbbbbbbbbbbb(2) {}", 14432 CtorInitializerStyle); 14433 14434 FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30); 14435 InheritanceStyle.SpaceBeforeInheritanceColon = false; 14436 verifyFormat("class Foo: public Bar {};", InheritanceStyle); 14437 verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle); 14438 verifyFormat("for (auto a : b) {\n}", InheritanceStyle); 14439 verifyFormat("int x = a ? b : c;", InheritanceStyle); 14440 verifyFormat("{\n" 14441 "label2:\n" 14442 " int x = 0;\n" 14443 "}", 14444 InheritanceStyle); 14445 verifyFormat("switch (x) {\n" 14446 "case 1:\n" 14447 "default:\n" 14448 "}", 14449 InheritanceStyle); 14450 verifyFormat("switch (allBraces) {\n" 14451 "case 1: {\n" 14452 " break;\n" 14453 "}\n" 14454 "case 2: {\n" 14455 " [[fallthrough]];\n" 14456 "}\n" 14457 "default: {\n" 14458 " break;\n" 14459 "}\n" 14460 "}", 14461 InheritanceStyle); 14462 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma; 14463 verifyFormat("class Foooooooooooooooooooooo\n" 14464 " : public aaaaaaaaaaaaaaaaaa,\n" 14465 " public bbbbbbbbbbbbbbbbbb {\n" 14466 "}", 14467 InheritanceStyle); 14468 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon; 14469 verifyFormat("class Foooooooooooooooooooooo:\n" 14470 " public aaaaaaaaaaaaaaaaaa,\n" 14471 " public bbbbbbbbbbbbbbbbbb {\n" 14472 "}", 14473 InheritanceStyle); 14474 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 14475 verifyFormat("class Foooooooooooooooooooooo\n" 14476 " : public aaaaaaaaaaaaaaaaaa\n" 14477 " , public bbbbbbbbbbbbbbbbbb {\n" 14478 "}", 14479 InheritanceStyle); 14480 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 14481 verifyFormat("class Foooooooooooooooooooooo\n" 14482 " : public aaaaaaaaaaaaaaaaaa,\n" 14483 " public bbbbbbbbbbbbbbbbbb {\n" 14484 "}", 14485 InheritanceStyle); 14486 InheritanceStyle.ConstructorInitializerIndentWidth = 0; 14487 verifyFormat("class Foooooooooooooooooooooo\n" 14488 ": public aaaaaaaaaaaaaaaaaa,\n" 14489 " public bbbbbbbbbbbbbbbbbb {}", 14490 InheritanceStyle); 14491 14492 FormatStyle ForLoopStyle = getLLVMStyle(); 14493 ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false; 14494 verifyFormat("class Foo : public Bar {};", ForLoopStyle); 14495 verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle); 14496 verifyFormat("for (auto a: b) {\n}", ForLoopStyle); 14497 verifyFormat("int x = a ? b : c;", ForLoopStyle); 14498 verifyFormat("{\n" 14499 "label2:\n" 14500 " int x = 0;\n" 14501 "}", 14502 ForLoopStyle); 14503 verifyFormat("switch (x) {\n" 14504 "case 1:\n" 14505 "default:\n" 14506 "}", 14507 ForLoopStyle); 14508 verifyFormat("switch (allBraces) {\n" 14509 "case 1: {\n" 14510 " break;\n" 14511 "}\n" 14512 "case 2: {\n" 14513 " [[fallthrough]];\n" 14514 "}\n" 14515 "default: {\n" 14516 " break;\n" 14517 "}\n" 14518 "}", 14519 ForLoopStyle); 14520 14521 FormatStyle CaseStyle = getLLVMStyle(); 14522 CaseStyle.SpaceBeforeCaseColon = true; 14523 verifyFormat("class Foo : public Bar {};", CaseStyle); 14524 verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle); 14525 verifyFormat("for (auto a : b) {\n}", CaseStyle); 14526 verifyFormat("int x = a ? b : c;", CaseStyle); 14527 verifyFormat("switch (x) {\n" 14528 "case 1 :\n" 14529 "default :\n" 14530 "}", 14531 CaseStyle); 14532 verifyFormat("switch (allBraces) {\n" 14533 "case 1 : {\n" 14534 " break;\n" 14535 "}\n" 14536 "case 2 : {\n" 14537 " [[fallthrough]];\n" 14538 "}\n" 14539 "default : {\n" 14540 " break;\n" 14541 "}\n" 14542 "}", 14543 CaseStyle); 14544 14545 FormatStyle NoSpaceStyle = getLLVMStyle(); 14546 EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false); 14547 NoSpaceStyle.SpaceBeforeCtorInitializerColon = false; 14548 NoSpaceStyle.SpaceBeforeInheritanceColon = false; 14549 NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 14550 verifyFormat("class Foo: public Bar {};", NoSpaceStyle); 14551 verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle); 14552 verifyFormat("for (auto a: b) {\n}", NoSpaceStyle); 14553 verifyFormat("int x = a ? b : c;", NoSpaceStyle); 14554 verifyFormat("{\n" 14555 "label3:\n" 14556 " int x = 0;\n" 14557 "}", 14558 NoSpaceStyle); 14559 verifyFormat("switch (x) {\n" 14560 "case 1:\n" 14561 "default:\n" 14562 "}", 14563 NoSpaceStyle); 14564 verifyFormat("switch (allBraces) {\n" 14565 "case 1: {\n" 14566 " break;\n" 14567 "}\n" 14568 "case 2: {\n" 14569 " [[fallthrough]];\n" 14570 "}\n" 14571 "default: {\n" 14572 " break;\n" 14573 "}\n" 14574 "}", 14575 NoSpaceStyle); 14576 14577 FormatStyle InvertedSpaceStyle = getLLVMStyle(); 14578 InvertedSpaceStyle.SpaceBeforeCaseColon = true; 14579 InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false; 14580 InvertedSpaceStyle.SpaceBeforeInheritanceColon = false; 14581 InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 14582 verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle); 14583 verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle); 14584 verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle); 14585 verifyFormat("int x = a ? b : c;", InvertedSpaceStyle); 14586 verifyFormat("{\n" 14587 "label3:\n" 14588 " int x = 0;\n" 14589 "}", 14590 InvertedSpaceStyle); 14591 verifyFormat("switch (x) {\n" 14592 "case 1 :\n" 14593 "case 2 : {\n" 14594 " break;\n" 14595 "}\n" 14596 "default :\n" 14597 " break;\n" 14598 "}", 14599 InvertedSpaceStyle); 14600 verifyFormat("switch (allBraces) {\n" 14601 "case 1 : {\n" 14602 " break;\n" 14603 "}\n" 14604 "case 2 : {\n" 14605 " [[fallthrough]];\n" 14606 "}\n" 14607 "default : {\n" 14608 " break;\n" 14609 "}\n" 14610 "}", 14611 InvertedSpaceStyle); 14612 } 14613 14614 TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) { 14615 FormatStyle Style = getLLVMStyle(); 14616 14617 Style.PointerAlignment = FormatStyle::PAS_Left; 14618 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; 14619 verifyFormat("void* const* x = NULL;", Style); 14620 14621 #define verifyQualifierSpaces(Code, Pointers, Qualifiers) \ 14622 do { \ 14623 Style.PointerAlignment = FormatStyle::Pointers; \ 14624 Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers; \ 14625 verifyFormat(Code, Style); \ 14626 } while (false) 14627 14628 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default); 14629 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default); 14630 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default); 14631 14632 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before); 14633 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before); 14634 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before); 14635 14636 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After); 14637 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After); 14638 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After); 14639 14640 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both); 14641 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both); 14642 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both); 14643 14644 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default); 14645 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, 14646 SAPQ_Default); 14647 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 14648 SAPQ_Default); 14649 14650 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before); 14651 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, 14652 SAPQ_Before); 14653 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 14654 SAPQ_Before); 14655 14656 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After); 14657 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After); 14658 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 14659 SAPQ_After); 14660 14661 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both); 14662 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both); 14663 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both); 14664 14665 #undef verifyQualifierSpaces 14666 14667 FormatStyle Spaces = getLLVMStyle(); 14668 Spaces.AttributeMacros.push_back("qualified"); 14669 Spaces.PointerAlignment = FormatStyle::PAS_Right; 14670 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; 14671 verifyFormat("SomeType *volatile *a = NULL;", Spaces); 14672 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces); 14673 verifyFormat("std::vector<SomeType *const *> x;", Spaces); 14674 verifyFormat("std::vector<SomeType *qualified *> x;", Spaces); 14675 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 14676 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before; 14677 verifyFormat("SomeType * volatile *a = NULL;", Spaces); 14678 verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces); 14679 verifyFormat("std::vector<SomeType * const *> x;", Spaces); 14680 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces); 14681 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 14682 14683 // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left. 14684 Spaces.PointerAlignment = FormatStyle::PAS_Left; 14685 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before; 14686 verifyFormat("SomeType* volatile* a = NULL;", Spaces); 14687 verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces); 14688 verifyFormat("std::vector<SomeType* const*> x;", Spaces); 14689 verifyFormat("std::vector<SomeType* qualified*> x;", Spaces); 14690 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 14691 // However, setting it to SAPQ_After should add spaces after __attribute, etc. 14692 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After; 14693 verifyFormat("SomeType* volatile * a = NULL;", Spaces); 14694 verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces); 14695 verifyFormat("std::vector<SomeType* const *> x;", Spaces); 14696 verifyFormat("std::vector<SomeType* qualified *> x;", Spaces); 14697 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 14698 14699 // PAS_Middle should not have any noticeable changes even for SAPQ_Both 14700 Spaces.PointerAlignment = FormatStyle::PAS_Middle; 14701 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After; 14702 verifyFormat("SomeType * volatile * a = NULL;", Spaces); 14703 verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces); 14704 verifyFormat("std::vector<SomeType * const *> x;", Spaces); 14705 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces); 14706 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 14707 } 14708 14709 TEST_F(FormatTest, AlignConsecutiveMacros) { 14710 FormatStyle Style = getLLVMStyle(); 14711 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 14712 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 14713 Style.AlignConsecutiveMacros = FormatStyle::ACS_None; 14714 14715 verifyFormat("#define a 3\n" 14716 "#define bbbb 4\n" 14717 "#define ccc (5)", 14718 Style); 14719 14720 verifyFormat("#define f(x) (x * x)\n" 14721 "#define fff(x, y, z) (x * y + z)\n" 14722 "#define ffff(x, y) (x - y)", 14723 Style); 14724 14725 verifyFormat("#define foo(x, y) (x + y)\n" 14726 "#define bar (5, 6)(2 + 2)", 14727 Style); 14728 14729 verifyFormat("#define a 3\n" 14730 "#define bbbb 4\n" 14731 "#define ccc (5)\n" 14732 "#define f(x) (x * x)\n" 14733 "#define fff(x, y, z) (x * y + z)\n" 14734 "#define ffff(x, y) (x - y)", 14735 Style); 14736 14737 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 14738 verifyFormat("#define a 3\n" 14739 "#define bbbb 4\n" 14740 "#define ccc (5)", 14741 Style); 14742 14743 verifyFormat("#define f(x) (x * x)\n" 14744 "#define fff(x, y, z) (x * y + z)\n" 14745 "#define ffff(x, y) (x - y)", 14746 Style); 14747 14748 verifyFormat("#define foo(x, y) (x + y)\n" 14749 "#define bar (5, 6)(2 + 2)", 14750 Style); 14751 14752 verifyFormat("#define a 3\n" 14753 "#define bbbb 4\n" 14754 "#define ccc (5)\n" 14755 "#define f(x) (x * x)\n" 14756 "#define fff(x, y, z) (x * y + z)\n" 14757 "#define ffff(x, y) (x - y)", 14758 Style); 14759 14760 verifyFormat("#define a 5\n" 14761 "#define foo(x, y) (x + y)\n" 14762 "#define CCC (6)\n" 14763 "auto lambda = []() {\n" 14764 " auto ii = 0;\n" 14765 " float j = 0;\n" 14766 " return 0;\n" 14767 "};\n" 14768 "int i = 0;\n" 14769 "float i2 = 0;\n" 14770 "auto v = type{\n" 14771 " i = 1, //\n" 14772 " (i = 2), //\n" 14773 " i = 3 //\n" 14774 "};", 14775 Style); 14776 14777 Style.AlignConsecutiveMacros = FormatStyle::ACS_None; 14778 Style.ColumnLimit = 20; 14779 14780 verifyFormat("#define a \\\n" 14781 " \"aabbbbbbbbbbbb\"\n" 14782 "#define D \\\n" 14783 " \"aabbbbbbbbbbbb\" \\\n" 14784 " \"ccddeeeeeeeee\"\n" 14785 "#define B \\\n" 14786 " \"QQQQQQQQQQQQQ\" \\\n" 14787 " \"FFFFFFFFFFFFF\" \\\n" 14788 " \"LLLLLLLL\"\n", 14789 Style); 14790 14791 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 14792 verifyFormat("#define a \\\n" 14793 " \"aabbbbbbbbbbbb\"\n" 14794 "#define D \\\n" 14795 " \"aabbbbbbbbbbbb\" \\\n" 14796 " \"ccddeeeeeeeee\"\n" 14797 "#define B \\\n" 14798 " \"QQQQQQQQQQQQQ\" \\\n" 14799 " \"FFFFFFFFFFFFF\" \\\n" 14800 " \"LLLLLLLL\"\n", 14801 Style); 14802 14803 // Test across comments 14804 Style.MaxEmptyLinesToKeep = 10; 14805 Style.ReflowComments = false; 14806 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossComments; 14807 EXPECT_EQ("#define a 3\n" 14808 "// line comment\n" 14809 "#define bbbb 4\n" 14810 "#define ccc (5)", 14811 format("#define a 3\n" 14812 "// line comment\n" 14813 "#define bbbb 4\n" 14814 "#define ccc (5)", 14815 Style)); 14816 14817 EXPECT_EQ("#define a 3\n" 14818 "/* block comment */\n" 14819 "#define bbbb 4\n" 14820 "#define ccc (5)", 14821 format("#define a 3\n" 14822 "/* block comment */\n" 14823 "#define bbbb 4\n" 14824 "#define ccc (5)", 14825 Style)); 14826 14827 EXPECT_EQ("#define a 3\n" 14828 "/* multi-line *\n" 14829 " * block comment */\n" 14830 "#define bbbb 4\n" 14831 "#define ccc (5)", 14832 format("#define a 3\n" 14833 "/* multi-line *\n" 14834 " * block comment */\n" 14835 "#define bbbb 4\n" 14836 "#define ccc (5)", 14837 Style)); 14838 14839 EXPECT_EQ("#define a 3\n" 14840 "// multi-line line comment\n" 14841 "//\n" 14842 "#define bbbb 4\n" 14843 "#define ccc (5)", 14844 format("#define a 3\n" 14845 "// multi-line line comment\n" 14846 "//\n" 14847 "#define bbbb 4\n" 14848 "#define ccc (5)", 14849 Style)); 14850 14851 EXPECT_EQ("#define a 3\n" 14852 "// empty lines still break.\n" 14853 "\n" 14854 "#define bbbb 4\n" 14855 "#define ccc (5)", 14856 format("#define a 3\n" 14857 "// empty lines still break.\n" 14858 "\n" 14859 "#define bbbb 4\n" 14860 "#define ccc (5)", 14861 Style)); 14862 14863 // Test across empty lines 14864 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLines; 14865 EXPECT_EQ("#define a 3\n" 14866 "\n" 14867 "#define bbbb 4\n" 14868 "#define ccc (5)", 14869 format("#define a 3\n" 14870 "\n" 14871 "#define bbbb 4\n" 14872 "#define ccc (5)", 14873 Style)); 14874 14875 EXPECT_EQ("#define a 3\n" 14876 "\n" 14877 "\n" 14878 "\n" 14879 "#define bbbb 4\n" 14880 "#define ccc (5)", 14881 format("#define a 3\n" 14882 "\n" 14883 "\n" 14884 "\n" 14885 "#define bbbb 4\n" 14886 "#define ccc (5)", 14887 Style)); 14888 14889 EXPECT_EQ("#define a 3\n" 14890 "// comments should break alignment\n" 14891 "//\n" 14892 "#define bbbb 4\n" 14893 "#define ccc (5)", 14894 format("#define a 3\n" 14895 "// comments should break alignment\n" 14896 "//\n" 14897 "#define bbbb 4\n" 14898 "#define ccc (5)", 14899 Style)); 14900 14901 // Test across empty lines and comments 14902 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLinesAndComments; 14903 verifyFormat("#define a 3\n" 14904 "\n" 14905 "// line comment\n" 14906 "#define bbbb 4\n" 14907 "#define ccc (5)", 14908 Style); 14909 14910 EXPECT_EQ("#define a 3\n" 14911 "\n" 14912 "\n" 14913 "/* multi-line *\n" 14914 " * block comment */\n" 14915 "\n" 14916 "\n" 14917 "#define bbbb 4\n" 14918 "#define ccc (5)", 14919 format("#define a 3\n" 14920 "\n" 14921 "\n" 14922 "/* multi-line *\n" 14923 " * block comment */\n" 14924 "\n" 14925 "\n" 14926 "#define bbbb 4\n" 14927 "#define ccc (5)", 14928 Style)); 14929 14930 EXPECT_EQ("#define a 3\n" 14931 "\n" 14932 "\n" 14933 "/* multi-line *\n" 14934 " * block comment */\n" 14935 "\n" 14936 "\n" 14937 "#define bbbb 4\n" 14938 "#define ccc (5)", 14939 format("#define a 3\n" 14940 "\n" 14941 "\n" 14942 "/* multi-line *\n" 14943 " * block comment */\n" 14944 "\n" 14945 "\n" 14946 "#define bbbb 4\n" 14947 "#define ccc (5)", 14948 Style)); 14949 } 14950 14951 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) { 14952 FormatStyle Alignment = getLLVMStyle(); 14953 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 14954 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossEmptyLines; 14955 14956 Alignment.MaxEmptyLinesToKeep = 10; 14957 /* Test alignment across empty lines */ 14958 EXPECT_EQ("int a = 5;\n" 14959 "\n" 14960 "int oneTwoThree = 123;", 14961 format("int a = 5;\n" 14962 "\n" 14963 "int oneTwoThree= 123;", 14964 Alignment)); 14965 EXPECT_EQ("int a = 5;\n" 14966 "int one = 1;\n" 14967 "\n" 14968 "int oneTwoThree = 123;", 14969 format("int a = 5;\n" 14970 "int one = 1;\n" 14971 "\n" 14972 "int oneTwoThree = 123;", 14973 Alignment)); 14974 EXPECT_EQ("int a = 5;\n" 14975 "int one = 1;\n" 14976 "\n" 14977 "int oneTwoThree = 123;\n" 14978 "int oneTwo = 12;", 14979 format("int a = 5;\n" 14980 "int one = 1;\n" 14981 "\n" 14982 "int oneTwoThree = 123;\n" 14983 "int oneTwo = 12;", 14984 Alignment)); 14985 14986 /* Test across comments */ 14987 EXPECT_EQ("int a = 5;\n" 14988 "/* block comment */\n" 14989 "int oneTwoThree = 123;", 14990 format("int a = 5;\n" 14991 "/* block comment */\n" 14992 "int oneTwoThree=123;", 14993 Alignment)); 14994 14995 EXPECT_EQ("int a = 5;\n" 14996 "// line comment\n" 14997 "int oneTwoThree = 123;", 14998 format("int a = 5;\n" 14999 "// line comment\n" 15000 "int oneTwoThree=123;", 15001 Alignment)); 15002 15003 /* Test across comments and newlines */ 15004 EXPECT_EQ("int a = 5;\n" 15005 "\n" 15006 "/* block comment */\n" 15007 "int oneTwoThree = 123;", 15008 format("int a = 5;\n" 15009 "\n" 15010 "/* block comment */\n" 15011 "int oneTwoThree=123;", 15012 Alignment)); 15013 15014 EXPECT_EQ("int a = 5;\n" 15015 "\n" 15016 "// line comment\n" 15017 "int oneTwoThree = 123;", 15018 format("int a = 5;\n" 15019 "\n" 15020 "// line comment\n" 15021 "int oneTwoThree=123;", 15022 Alignment)); 15023 } 15024 15025 TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) { 15026 FormatStyle Alignment = getLLVMStyle(); 15027 Alignment.AlignConsecutiveDeclarations = 15028 FormatStyle::ACS_AcrossEmptyLinesAndComments; 15029 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 15030 15031 Alignment.MaxEmptyLinesToKeep = 10; 15032 /* Test alignment across empty lines */ 15033 EXPECT_EQ("int a = 5;\n" 15034 "\n" 15035 "float const oneTwoThree = 123;", 15036 format("int a = 5;\n" 15037 "\n" 15038 "float const oneTwoThree = 123;", 15039 Alignment)); 15040 EXPECT_EQ("int a = 5;\n" 15041 "float const one = 1;\n" 15042 "\n" 15043 "int oneTwoThree = 123;", 15044 format("int a = 5;\n" 15045 "float const one = 1;\n" 15046 "\n" 15047 "int oneTwoThree = 123;", 15048 Alignment)); 15049 15050 /* Test across comments */ 15051 EXPECT_EQ("float const a = 5;\n" 15052 "/* block comment */\n" 15053 "int oneTwoThree = 123;", 15054 format("float const a = 5;\n" 15055 "/* block comment */\n" 15056 "int oneTwoThree=123;", 15057 Alignment)); 15058 15059 EXPECT_EQ("float const a = 5;\n" 15060 "// line comment\n" 15061 "int oneTwoThree = 123;", 15062 format("float const a = 5;\n" 15063 "// line comment\n" 15064 "int oneTwoThree=123;", 15065 Alignment)); 15066 15067 /* Test across comments and newlines */ 15068 EXPECT_EQ("float const a = 5;\n" 15069 "\n" 15070 "/* block comment */\n" 15071 "int oneTwoThree = 123;", 15072 format("float const a = 5;\n" 15073 "\n" 15074 "/* block comment */\n" 15075 "int oneTwoThree=123;", 15076 Alignment)); 15077 15078 EXPECT_EQ("float const a = 5;\n" 15079 "\n" 15080 "// line comment\n" 15081 "int oneTwoThree = 123;", 15082 format("float const a = 5;\n" 15083 "\n" 15084 "// line comment\n" 15085 "int oneTwoThree=123;", 15086 Alignment)); 15087 } 15088 15089 TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) { 15090 FormatStyle Alignment = getLLVMStyle(); 15091 Alignment.AlignConsecutiveBitFields = 15092 FormatStyle::ACS_AcrossEmptyLinesAndComments; 15093 15094 Alignment.MaxEmptyLinesToKeep = 10; 15095 /* Test alignment across empty lines */ 15096 EXPECT_EQ("int a : 5;\n" 15097 "\n" 15098 "int longbitfield : 6;", 15099 format("int a : 5;\n" 15100 "\n" 15101 "int longbitfield : 6;", 15102 Alignment)); 15103 EXPECT_EQ("int a : 5;\n" 15104 "int one : 1;\n" 15105 "\n" 15106 "int longbitfield : 6;", 15107 format("int a : 5;\n" 15108 "int one : 1;\n" 15109 "\n" 15110 "int longbitfield : 6;", 15111 Alignment)); 15112 15113 /* Test across comments */ 15114 EXPECT_EQ("int a : 5;\n" 15115 "/* block comment */\n" 15116 "int longbitfield : 6;", 15117 format("int a : 5;\n" 15118 "/* block comment */\n" 15119 "int longbitfield : 6;", 15120 Alignment)); 15121 EXPECT_EQ("int a : 5;\n" 15122 "int one : 1;\n" 15123 "// line comment\n" 15124 "int longbitfield : 6;", 15125 format("int a : 5;\n" 15126 "int one : 1;\n" 15127 "// line comment\n" 15128 "int longbitfield : 6;", 15129 Alignment)); 15130 15131 /* Test across comments and newlines */ 15132 EXPECT_EQ("int a : 5;\n" 15133 "/* block comment */\n" 15134 "\n" 15135 "int longbitfield : 6;", 15136 format("int a : 5;\n" 15137 "/* block comment */\n" 15138 "\n" 15139 "int longbitfield : 6;", 15140 Alignment)); 15141 EXPECT_EQ("int a : 5;\n" 15142 "int one : 1;\n" 15143 "\n" 15144 "// line comment\n" 15145 "\n" 15146 "int longbitfield : 6;", 15147 format("int a : 5;\n" 15148 "int one : 1;\n" 15149 "\n" 15150 "// line comment \n" 15151 "\n" 15152 "int longbitfield : 6;", 15153 Alignment)); 15154 } 15155 15156 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) { 15157 FormatStyle Alignment = getLLVMStyle(); 15158 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15159 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossComments; 15160 15161 Alignment.MaxEmptyLinesToKeep = 10; 15162 /* Test alignment across empty lines */ 15163 EXPECT_EQ("int a = 5;\n" 15164 "\n" 15165 "int oneTwoThree = 123;", 15166 format("int a = 5;\n" 15167 "\n" 15168 "int oneTwoThree= 123;", 15169 Alignment)); 15170 EXPECT_EQ("int a = 5;\n" 15171 "int one = 1;\n" 15172 "\n" 15173 "int oneTwoThree = 123;", 15174 format("int a = 5;\n" 15175 "int one = 1;\n" 15176 "\n" 15177 "int oneTwoThree = 123;", 15178 Alignment)); 15179 15180 /* Test across comments */ 15181 EXPECT_EQ("int a = 5;\n" 15182 "/* block comment */\n" 15183 "int oneTwoThree = 123;", 15184 format("int a = 5;\n" 15185 "/* block comment */\n" 15186 "int oneTwoThree=123;", 15187 Alignment)); 15188 15189 EXPECT_EQ("int a = 5;\n" 15190 "// line comment\n" 15191 "int oneTwoThree = 123;", 15192 format("int a = 5;\n" 15193 "// line comment\n" 15194 "int oneTwoThree=123;", 15195 Alignment)); 15196 15197 EXPECT_EQ("int a = 5;\n" 15198 "/*\n" 15199 " * multi-line block comment\n" 15200 " */\n" 15201 "int oneTwoThree = 123;", 15202 format("int a = 5;\n" 15203 "/*\n" 15204 " * multi-line block comment\n" 15205 " */\n" 15206 "int oneTwoThree=123;", 15207 Alignment)); 15208 15209 EXPECT_EQ("int a = 5;\n" 15210 "//\n" 15211 "// multi-line line comment\n" 15212 "//\n" 15213 "int oneTwoThree = 123;", 15214 format("int a = 5;\n" 15215 "//\n" 15216 "// multi-line line comment\n" 15217 "//\n" 15218 "int oneTwoThree=123;", 15219 Alignment)); 15220 15221 /* Test across comments and newlines */ 15222 EXPECT_EQ("int a = 5;\n" 15223 "\n" 15224 "/* block comment */\n" 15225 "int oneTwoThree = 123;", 15226 format("int a = 5;\n" 15227 "\n" 15228 "/* block comment */\n" 15229 "int oneTwoThree=123;", 15230 Alignment)); 15231 15232 EXPECT_EQ("int a = 5;\n" 15233 "\n" 15234 "// line comment\n" 15235 "int oneTwoThree = 123;", 15236 format("int a = 5;\n" 15237 "\n" 15238 "// line comment\n" 15239 "int oneTwoThree=123;", 15240 Alignment)); 15241 } 15242 15243 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) { 15244 FormatStyle Alignment = getLLVMStyle(); 15245 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15246 Alignment.AlignConsecutiveAssignments = 15247 FormatStyle::ACS_AcrossEmptyLinesAndComments; 15248 verifyFormat("int a = 5;\n" 15249 "int oneTwoThree = 123;", 15250 Alignment); 15251 verifyFormat("int a = method();\n" 15252 "int oneTwoThree = 133;", 15253 Alignment); 15254 verifyFormat("a &= 5;\n" 15255 "bcd *= 5;\n" 15256 "ghtyf += 5;\n" 15257 "dvfvdb -= 5;\n" 15258 "a /= 5;\n" 15259 "vdsvsv %= 5;\n" 15260 "sfdbddfbdfbb ^= 5;\n" 15261 "dvsdsv |= 5;\n" 15262 "int dsvvdvsdvvv = 123;", 15263 Alignment); 15264 verifyFormat("int i = 1, j = 10;\n" 15265 "something = 2000;", 15266 Alignment); 15267 verifyFormat("something = 2000;\n" 15268 "int i = 1, j = 10;\n", 15269 Alignment); 15270 verifyFormat("something = 2000;\n" 15271 "another = 911;\n" 15272 "int i = 1, j = 10;\n" 15273 "oneMore = 1;\n" 15274 "i = 2;", 15275 Alignment); 15276 verifyFormat("int a = 5;\n" 15277 "int one = 1;\n" 15278 "method();\n" 15279 "int oneTwoThree = 123;\n" 15280 "int oneTwo = 12;", 15281 Alignment); 15282 verifyFormat("int oneTwoThree = 123;\n" 15283 "int oneTwo = 12;\n" 15284 "method();\n", 15285 Alignment); 15286 verifyFormat("int oneTwoThree = 123; // comment\n" 15287 "int oneTwo = 12; // comment", 15288 Alignment); 15289 15290 // Bug 25167 15291 /* Uncomment when fixed 15292 verifyFormat("#if A\n" 15293 "#else\n" 15294 "int aaaaaaaa = 12;\n" 15295 "#endif\n" 15296 "#if B\n" 15297 "#else\n" 15298 "int a = 12;\n" 15299 "#endif\n", 15300 Alignment); 15301 verifyFormat("enum foo {\n" 15302 "#if A\n" 15303 "#else\n" 15304 " aaaaaaaa = 12;\n" 15305 "#endif\n" 15306 "#if B\n" 15307 "#else\n" 15308 " a = 12;\n" 15309 "#endif\n" 15310 "};\n", 15311 Alignment); 15312 */ 15313 15314 Alignment.MaxEmptyLinesToKeep = 10; 15315 /* Test alignment across empty lines */ 15316 EXPECT_EQ("int a = 5;\n" 15317 "\n" 15318 "int oneTwoThree = 123;", 15319 format("int a = 5;\n" 15320 "\n" 15321 "int oneTwoThree= 123;", 15322 Alignment)); 15323 EXPECT_EQ("int a = 5;\n" 15324 "int one = 1;\n" 15325 "\n" 15326 "int oneTwoThree = 123;", 15327 format("int a = 5;\n" 15328 "int one = 1;\n" 15329 "\n" 15330 "int oneTwoThree = 123;", 15331 Alignment)); 15332 EXPECT_EQ("int a = 5;\n" 15333 "int one = 1;\n" 15334 "\n" 15335 "int oneTwoThree = 123;\n" 15336 "int oneTwo = 12;", 15337 format("int a = 5;\n" 15338 "int one = 1;\n" 15339 "\n" 15340 "int oneTwoThree = 123;\n" 15341 "int oneTwo = 12;", 15342 Alignment)); 15343 15344 /* Test across comments */ 15345 EXPECT_EQ("int a = 5;\n" 15346 "/* block comment */\n" 15347 "int oneTwoThree = 123;", 15348 format("int a = 5;\n" 15349 "/* block comment */\n" 15350 "int oneTwoThree=123;", 15351 Alignment)); 15352 15353 EXPECT_EQ("int a = 5;\n" 15354 "// line comment\n" 15355 "int oneTwoThree = 123;", 15356 format("int a = 5;\n" 15357 "// line comment\n" 15358 "int oneTwoThree=123;", 15359 Alignment)); 15360 15361 /* Test across comments and newlines */ 15362 EXPECT_EQ("int a = 5;\n" 15363 "\n" 15364 "/* block comment */\n" 15365 "int oneTwoThree = 123;", 15366 format("int a = 5;\n" 15367 "\n" 15368 "/* block comment */\n" 15369 "int oneTwoThree=123;", 15370 Alignment)); 15371 15372 EXPECT_EQ("int a = 5;\n" 15373 "\n" 15374 "// line comment\n" 15375 "int oneTwoThree = 123;", 15376 format("int a = 5;\n" 15377 "\n" 15378 "// line comment\n" 15379 "int oneTwoThree=123;", 15380 Alignment)); 15381 15382 EXPECT_EQ("int a = 5;\n" 15383 "//\n" 15384 "// multi-line line comment\n" 15385 "//\n" 15386 "int oneTwoThree = 123;", 15387 format("int a = 5;\n" 15388 "//\n" 15389 "// multi-line line comment\n" 15390 "//\n" 15391 "int oneTwoThree=123;", 15392 Alignment)); 15393 15394 EXPECT_EQ("int a = 5;\n" 15395 "/*\n" 15396 " * multi-line block comment\n" 15397 " */\n" 15398 "int oneTwoThree = 123;", 15399 format("int a = 5;\n" 15400 "/*\n" 15401 " * multi-line block comment\n" 15402 " */\n" 15403 "int oneTwoThree=123;", 15404 Alignment)); 15405 15406 EXPECT_EQ("int a = 5;\n" 15407 "\n" 15408 "/* block comment */\n" 15409 "\n" 15410 "\n" 15411 "\n" 15412 "int oneTwoThree = 123;", 15413 format("int a = 5;\n" 15414 "\n" 15415 "/* block comment */\n" 15416 "\n" 15417 "\n" 15418 "\n" 15419 "int oneTwoThree=123;", 15420 Alignment)); 15421 15422 EXPECT_EQ("int a = 5;\n" 15423 "\n" 15424 "// line comment\n" 15425 "\n" 15426 "\n" 15427 "\n" 15428 "int oneTwoThree = 123;", 15429 format("int a = 5;\n" 15430 "\n" 15431 "// line comment\n" 15432 "\n" 15433 "\n" 15434 "\n" 15435 "int oneTwoThree=123;", 15436 Alignment)); 15437 15438 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 15439 verifyFormat("#define A \\\n" 15440 " int aaaa = 12; \\\n" 15441 " int b = 23; \\\n" 15442 " int ccc = 234; \\\n" 15443 " int dddddddddd = 2345;", 15444 Alignment); 15445 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 15446 verifyFormat("#define A \\\n" 15447 " int aaaa = 12; \\\n" 15448 " int b = 23; \\\n" 15449 " int ccc = 234; \\\n" 15450 " int dddddddddd = 2345;", 15451 Alignment); 15452 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 15453 verifyFormat("#define A " 15454 " \\\n" 15455 " int aaaa = 12; " 15456 " \\\n" 15457 " int b = 23; " 15458 " \\\n" 15459 " int ccc = 234; " 15460 " \\\n" 15461 " int dddddddddd = 2345;", 15462 Alignment); 15463 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 15464 "k = 4, int l = 5,\n" 15465 " int m = 6) {\n" 15466 " int j = 10;\n" 15467 " otherThing = 1;\n" 15468 "}", 15469 Alignment); 15470 verifyFormat("void SomeFunction(int parameter = 0) {\n" 15471 " int i = 1;\n" 15472 " int j = 2;\n" 15473 " int big = 10000;\n" 15474 "}", 15475 Alignment); 15476 verifyFormat("class C {\n" 15477 "public:\n" 15478 " int i = 1;\n" 15479 " virtual void f() = 0;\n" 15480 "};", 15481 Alignment); 15482 verifyFormat("int i = 1;\n" 15483 "if (SomeType t = getSomething()) {\n" 15484 "}\n" 15485 "int j = 2;\n" 15486 "int big = 10000;", 15487 Alignment); 15488 verifyFormat("int j = 7;\n" 15489 "for (int k = 0; k < N; ++k) {\n" 15490 "}\n" 15491 "int j = 2;\n" 15492 "int big = 10000;\n" 15493 "}", 15494 Alignment); 15495 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 15496 verifyFormat("int i = 1;\n" 15497 "LooooooooooongType loooooooooooooooooooooongVariable\n" 15498 " = someLooooooooooooooooongFunction();\n" 15499 "int j = 2;", 15500 Alignment); 15501 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 15502 verifyFormat("int i = 1;\n" 15503 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 15504 " someLooooooooooooooooongFunction();\n" 15505 "int j = 2;", 15506 Alignment); 15507 15508 verifyFormat("auto lambda = []() {\n" 15509 " auto i = 0;\n" 15510 " return 0;\n" 15511 "};\n" 15512 "int i = 0;\n" 15513 "auto v = type{\n" 15514 " i = 1, //\n" 15515 " (i = 2), //\n" 15516 " i = 3 //\n" 15517 "};", 15518 Alignment); 15519 15520 verifyFormat( 15521 "int i = 1;\n" 15522 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 15523 " loooooooooooooooooooooongParameterB);\n" 15524 "int j = 2;", 15525 Alignment); 15526 15527 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 15528 " typename B = very_long_type_name_1,\n" 15529 " typename T_2 = very_long_type_name_2>\n" 15530 "auto foo() {}\n", 15531 Alignment); 15532 verifyFormat("int a, b = 1;\n" 15533 "int c = 2;\n" 15534 "int dd = 3;\n", 15535 Alignment); 15536 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 15537 "float b[1][] = {{3.f}};\n", 15538 Alignment); 15539 verifyFormat("for (int i = 0; i < 1; i++)\n" 15540 " int x = 1;\n", 15541 Alignment); 15542 verifyFormat("for (i = 0; i < 1; i++)\n" 15543 " x = 1;\n" 15544 "y = 1;\n", 15545 Alignment); 15546 15547 Alignment.ReflowComments = true; 15548 Alignment.ColumnLimit = 50; 15549 EXPECT_EQ("int x = 0;\n" 15550 "int yy = 1; /// specificlennospace\n" 15551 "int zzz = 2;\n", 15552 format("int x = 0;\n" 15553 "int yy = 1; ///specificlennospace\n" 15554 "int zzz = 2;\n", 15555 Alignment)); 15556 } 15557 15558 TEST_F(FormatTest, AlignConsecutiveAssignments) { 15559 FormatStyle Alignment = getLLVMStyle(); 15560 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15561 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 15562 verifyFormat("int a = 5;\n" 15563 "int oneTwoThree = 123;", 15564 Alignment); 15565 verifyFormat("int a = 5;\n" 15566 "int oneTwoThree = 123;", 15567 Alignment); 15568 15569 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 15570 verifyFormat("int a = 5;\n" 15571 "int oneTwoThree = 123;", 15572 Alignment); 15573 verifyFormat("int a = method();\n" 15574 "int oneTwoThree = 133;", 15575 Alignment); 15576 verifyFormat("a &= 5;\n" 15577 "bcd *= 5;\n" 15578 "ghtyf += 5;\n" 15579 "dvfvdb -= 5;\n" 15580 "a /= 5;\n" 15581 "vdsvsv %= 5;\n" 15582 "sfdbddfbdfbb ^= 5;\n" 15583 "dvsdsv |= 5;\n" 15584 "int dsvvdvsdvvv = 123;", 15585 Alignment); 15586 verifyFormat("int i = 1, j = 10;\n" 15587 "something = 2000;", 15588 Alignment); 15589 verifyFormat("something = 2000;\n" 15590 "int i = 1, j = 10;\n", 15591 Alignment); 15592 verifyFormat("something = 2000;\n" 15593 "another = 911;\n" 15594 "int i = 1, j = 10;\n" 15595 "oneMore = 1;\n" 15596 "i = 2;", 15597 Alignment); 15598 verifyFormat("int a = 5;\n" 15599 "int one = 1;\n" 15600 "method();\n" 15601 "int oneTwoThree = 123;\n" 15602 "int oneTwo = 12;", 15603 Alignment); 15604 verifyFormat("int oneTwoThree = 123;\n" 15605 "int oneTwo = 12;\n" 15606 "method();\n", 15607 Alignment); 15608 verifyFormat("int oneTwoThree = 123; // comment\n" 15609 "int oneTwo = 12; // comment", 15610 Alignment); 15611 15612 // Bug 25167 15613 /* Uncomment when fixed 15614 verifyFormat("#if A\n" 15615 "#else\n" 15616 "int aaaaaaaa = 12;\n" 15617 "#endif\n" 15618 "#if B\n" 15619 "#else\n" 15620 "int a = 12;\n" 15621 "#endif\n", 15622 Alignment); 15623 verifyFormat("enum foo {\n" 15624 "#if A\n" 15625 "#else\n" 15626 " aaaaaaaa = 12;\n" 15627 "#endif\n" 15628 "#if B\n" 15629 "#else\n" 15630 " a = 12;\n" 15631 "#endif\n" 15632 "};\n", 15633 Alignment); 15634 */ 15635 15636 EXPECT_EQ("int a = 5;\n" 15637 "\n" 15638 "int oneTwoThree = 123;", 15639 format("int a = 5;\n" 15640 "\n" 15641 "int oneTwoThree= 123;", 15642 Alignment)); 15643 EXPECT_EQ("int a = 5;\n" 15644 "int one = 1;\n" 15645 "\n" 15646 "int oneTwoThree = 123;", 15647 format("int a = 5;\n" 15648 "int one = 1;\n" 15649 "\n" 15650 "int oneTwoThree = 123;", 15651 Alignment)); 15652 EXPECT_EQ("int a = 5;\n" 15653 "int one = 1;\n" 15654 "\n" 15655 "int oneTwoThree = 123;\n" 15656 "int oneTwo = 12;", 15657 format("int a = 5;\n" 15658 "int one = 1;\n" 15659 "\n" 15660 "int oneTwoThree = 123;\n" 15661 "int oneTwo = 12;", 15662 Alignment)); 15663 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 15664 verifyFormat("#define A \\\n" 15665 " int aaaa = 12; \\\n" 15666 " int b = 23; \\\n" 15667 " int ccc = 234; \\\n" 15668 " int dddddddddd = 2345;", 15669 Alignment); 15670 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 15671 verifyFormat("#define A \\\n" 15672 " int aaaa = 12; \\\n" 15673 " int b = 23; \\\n" 15674 " int ccc = 234; \\\n" 15675 " int dddddddddd = 2345;", 15676 Alignment); 15677 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 15678 verifyFormat("#define A " 15679 " \\\n" 15680 " int aaaa = 12; " 15681 " \\\n" 15682 " int b = 23; " 15683 " \\\n" 15684 " int ccc = 234; " 15685 " \\\n" 15686 " int dddddddddd = 2345;", 15687 Alignment); 15688 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 15689 "k = 4, int l = 5,\n" 15690 " int m = 6) {\n" 15691 " int j = 10;\n" 15692 " otherThing = 1;\n" 15693 "}", 15694 Alignment); 15695 verifyFormat("void SomeFunction(int parameter = 0) {\n" 15696 " int i = 1;\n" 15697 " int j = 2;\n" 15698 " int big = 10000;\n" 15699 "}", 15700 Alignment); 15701 verifyFormat("class C {\n" 15702 "public:\n" 15703 " int i = 1;\n" 15704 " virtual void f() = 0;\n" 15705 "};", 15706 Alignment); 15707 verifyFormat("int i = 1;\n" 15708 "if (SomeType t = getSomething()) {\n" 15709 "}\n" 15710 "int j = 2;\n" 15711 "int big = 10000;", 15712 Alignment); 15713 verifyFormat("int j = 7;\n" 15714 "for (int k = 0; k < N; ++k) {\n" 15715 "}\n" 15716 "int j = 2;\n" 15717 "int big = 10000;\n" 15718 "}", 15719 Alignment); 15720 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 15721 verifyFormat("int i = 1;\n" 15722 "LooooooooooongType loooooooooooooooooooooongVariable\n" 15723 " = someLooooooooooooooooongFunction();\n" 15724 "int j = 2;", 15725 Alignment); 15726 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 15727 verifyFormat("int i = 1;\n" 15728 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 15729 " someLooooooooooooooooongFunction();\n" 15730 "int j = 2;", 15731 Alignment); 15732 15733 verifyFormat("auto lambda = []() {\n" 15734 " auto i = 0;\n" 15735 " return 0;\n" 15736 "};\n" 15737 "int i = 0;\n" 15738 "auto v = type{\n" 15739 " i = 1, //\n" 15740 " (i = 2), //\n" 15741 " i = 3 //\n" 15742 "};", 15743 Alignment); 15744 15745 verifyFormat( 15746 "int i = 1;\n" 15747 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 15748 " loooooooooooooooooooooongParameterB);\n" 15749 "int j = 2;", 15750 Alignment); 15751 15752 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 15753 " typename B = very_long_type_name_1,\n" 15754 " typename T_2 = very_long_type_name_2>\n" 15755 "auto foo() {}\n", 15756 Alignment); 15757 verifyFormat("int a, b = 1;\n" 15758 "int c = 2;\n" 15759 "int dd = 3;\n", 15760 Alignment); 15761 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 15762 "float b[1][] = {{3.f}};\n", 15763 Alignment); 15764 verifyFormat("for (int i = 0; i < 1; i++)\n" 15765 " int x = 1;\n", 15766 Alignment); 15767 verifyFormat("for (i = 0; i < 1; i++)\n" 15768 " x = 1;\n" 15769 "y = 1;\n", 15770 Alignment); 15771 15772 Alignment.ReflowComments = true; 15773 Alignment.ColumnLimit = 50; 15774 EXPECT_EQ("int x = 0;\n" 15775 "int yy = 1; /// specificlennospace\n" 15776 "int zzz = 2;\n", 15777 format("int x = 0;\n" 15778 "int yy = 1; ///specificlennospace\n" 15779 "int zzz = 2;\n", 15780 Alignment)); 15781 } 15782 15783 TEST_F(FormatTest, AlignConsecutiveBitFields) { 15784 FormatStyle Alignment = getLLVMStyle(); 15785 Alignment.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive; 15786 verifyFormat("int const a : 5;\n" 15787 "int oneTwoThree : 23;", 15788 Alignment); 15789 15790 // Initializers are allowed starting with c++2a 15791 verifyFormat("int const a : 5 = 1;\n" 15792 "int oneTwoThree : 23 = 0;", 15793 Alignment); 15794 15795 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 15796 verifyFormat("int const a : 5;\n" 15797 "int oneTwoThree : 23;", 15798 Alignment); 15799 15800 verifyFormat("int const a : 5; // comment\n" 15801 "int oneTwoThree : 23; // comment", 15802 Alignment); 15803 15804 verifyFormat("int const a : 5 = 1;\n" 15805 "int oneTwoThree : 23 = 0;", 15806 Alignment); 15807 15808 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 15809 verifyFormat("int const a : 5 = 1;\n" 15810 "int oneTwoThree : 23 = 0;", 15811 Alignment); 15812 verifyFormat("int const a : 5 = {1};\n" 15813 "int oneTwoThree : 23 = 0;", 15814 Alignment); 15815 15816 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None; 15817 verifyFormat("int const a :5;\n" 15818 "int oneTwoThree:23;", 15819 Alignment); 15820 15821 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before; 15822 verifyFormat("int const a :5;\n" 15823 "int oneTwoThree :23;", 15824 Alignment); 15825 15826 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After; 15827 verifyFormat("int const a : 5;\n" 15828 "int oneTwoThree: 23;", 15829 Alignment); 15830 15831 // Known limitations: ':' is only recognized as a bitfield colon when 15832 // followed by a number. 15833 /* 15834 verifyFormat("int oneTwoThree : SOME_CONSTANT;\n" 15835 "int a : 5;", 15836 Alignment); 15837 */ 15838 } 15839 15840 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 15841 FormatStyle Alignment = getLLVMStyle(); 15842 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15843 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_None; 15844 Alignment.PointerAlignment = FormatStyle::PAS_Right; 15845 verifyFormat("float const a = 5;\n" 15846 "int oneTwoThree = 123;", 15847 Alignment); 15848 verifyFormat("int a = 5;\n" 15849 "float const oneTwoThree = 123;", 15850 Alignment); 15851 15852 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 15853 verifyFormat("float const a = 5;\n" 15854 "int oneTwoThree = 123;", 15855 Alignment); 15856 verifyFormat("int a = method();\n" 15857 "float const oneTwoThree = 133;", 15858 Alignment); 15859 verifyFormat("int i = 1, j = 10;\n" 15860 "something = 2000;", 15861 Alignment); 15862 verifyFormat("something = 2000;\n" 15863 "int i = 1, j = 10;\n", 15864 Alignment); 15865 verifyFormat("float something = 2000;\n" 15866 "double another = 911;\n" 15867 "int i = 1, j = 10;\n" 15868 "const int *oneMore = 1;\n" 15869 "unsigned i = 2;", 15870 Alignment); 15871 verifyFormat("float a = 5;\n" 15872 "int one = 1;\n" 15873 "method();\n" 15874 "const double oneTwoThree = 123;\n" 15875 "const unsigned int oneTwo = 12;", 15876 Alignment); 15877 verifyFormat("int oneTwoThree{0}; // comment\n" 15878 "unsigned oneTwo; // comment", 15879 Alignment); 15880 verifyFormat("unsigned int *a;\n" 15881 "int *b;\n" 15882 "unsigned int Const *c;\n" 15883 "unsigned int const *d;\n" 15884 "unsigned int Const &e;\n" 15885 "unsigned int const &f;", 15886 Alignment); 15887 verifyFormat("Const unsigned int *c;\n" 15888 "const unsigned int *d;\n" 15889 "Const unsigned int &e;\n" 15890 "const unsigned int &f;\n" 15891 "const unsigned g;\n" 15892 "Const unsigned h;", 15893 Alignment); 15894 EXPECT_EQ("float const a = 5;\n" 15895 "\n" 15896 "int oneTwoThree = 123;", 15897 format("float const a = 5;\n" 15898 "\n" 15899 "int oneTwoThree= 123;", 15900 Alignment)); 15901 EXPECT_EQ("float a = 5;\n" 15902 "int one = 1;\n" 15903 "\n" 15904 "unsigned oneTwoThree = 123;", 15905 format("float a = 5;\n" 15906 "int one = 1;\n" 15907 "\n" 15908 "unsigned oneTwoThree = 123;", 15909 Alignment)); 15910 EXPECT_EQ("float a = 5;\n" 15911 "int one = 1;\n" 15912 "\n" 15913 "unsigned oneTwoThree = 123;\n" 15914 "int oneTwo = 12;", 15915 format("float a = 5;\n" 15916 "int one = 1;\n" 15917 "\n" 15918 "unsigned oneTwoThree = 123;\n" 15919 "int oneTwo = 12;", 15920 Alignment)); 15921 // Function prototype alignment 15922 verifyFormat("int a();\n" 15923 "double b();", 15924 Alignment); 15925 verifyFormat("int a(int x);\n" 15926 "double b();", 15927 Alignment); 15928 unsigned OldColumnLimit = Alignment.ColumnLimit; 15929 // We need to set ColumnLimit to zero, in order to stress nested alignments, 15930 // otherwise the function parameters will be re-flowed onto a single line. 15931 Alignment.ColumnLimit = 0; 15932 EXPECT_EQ("int a(int x,\n" 15933 " float y);\n" 15934 "double b(int x,\n" 15935 " double y);", 15936 format("int a(int x,\n" 15937 " float y);\n" 15938 "double b(int x,\n" 15939 " double y);", 15940 Alignment)); 15941 // This ensures that function parameters of function declarations are 15942 // correctly indented when their owning functions are indented. 15943 // The failure case here is for 'double y' to not be indented enough. 15944 EXPECT_EQ("double a(int x);\n" 15945 "int b(int y,\n" 15946 " double z);", 15947 format("double a(int x);\n" 15948 "int b(int y,\n" 15949 " double z);", 15950 Alignment)); 15951 // Set ColumnLimit low so that we induce wrapping immediately after 15952 // the function name and opening paren. 15953 Alignment.ColumnLimit = 13; 15954 verifyFormat("int function(\n" 15955 " int x,\n" 15956 " bool y);", 15957 Alignment); 15958 Alignment.ColumnLimit = OldColumnLimit; 15959 // Ensure function pointers don't screw up recursive alignment 15960 verifyFormat("int a(int x, void (*fp)(int y));\n" 15961 "double b();", 15962 Alignment); 15963 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 15964 // Ensure recursive alignment is broken by function braces, so that the 15965 // "a = 1" does not align with subsequent assignments inside the function 15966 // body. 15967 verifyFormat("int func(int a = 1) {\n" 15968 " int b = 2;\n" 15969 " int cc = 3;\n" 15970 "}", 15971 Alignment); 15972 verifyFormat("float something = 2000;\n" 15973 "double another = 911;\n" 15974 "int i = 1, j = 10;\n" 15975 "const int *oneMore = 1;\n" 15976 "unsigned i = 2;", 15977 Alignment); 15978 verifyFormat("int oneTwoThree = {0}; // comment\n" 15979 "unsigned oneTwo = 0; // comment", 15980 Alignment); 15981 // Make sure that scope is correctly tracked, in the absence of braces 15982 verifyFormat("for (int i = 0; i < n; i++)\n" 15983 " j = i;\n" 15984 "double x = 1;\n", 15985 Alignment); 15986 verifyFormat("if (int i = 0)\n" 15987 " j = i;\n" 15988 "double x = 1;\n", 15989 Alignment); 15990 // Ensure operator[] and operator() are comprehended 15991 verifyFormat("struct test {\n" 15992 " long long int foo();\n" 15993 " int operator[](int a);\n" 15994 " double bar();\n" 15995 "};\n", 15996 Alignment); 15997 verifyFormat("struct test {\n" 15998 " long long int foo();\n" 15999 " int operator()(int a);\n" 16000 " double bar();\n" 16001 "};\n", 16002 Alignment); 16003 16004 // PAS_Right 16005 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16006 " int const i = 1;\n" 16007 " int *j = 2;\n" 16008 " int big = 10000;\n" 16009 "\n" 16010 " unsigned oneTwoThree = 123;\n" 16011 " int oneTwo = 12;\n" 16012 " method();\n" 16013 " float k = 2;\n" 16014 " int ll = 10000;\n" 16015 "}", 16016 format("void SomeFunction(int parameter= 0) {\n" 16017 " int const i= 1;\n" 16018 " int *j=2;\n" 16019 " int big = 10000;\n" 16020 "\n" 16021 "unsigned oneTwoThree =123;\n" 16022 "int oneTwo = 12;\n" 16023 " method();\n" 16024 "float k= 2;\n" 16025 "int ll=10000;\n" 16026 "}", 16027 Alignment)); 16028 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16029 " int const i = 1;\n" 16030 " int **j = 2, ***k;\n" 16031 " int &k = i;\n" 16032 " int &&l = i + j;\n" 16033 " int big = 10000;\n" 16034 "\n" 16035 " unsigned oneTwoThree = 123;\n" 16036 " int oneTwo = 12;\n" 16037 " method();\n" 16038 " float k = 2;\n" 16039 " int ll = 10000;\n" 16040 "}", 16041 format("void SomeFunction(int parameter= 0) {\n" 16042 " int const i= 1;\n" 16043 " int **j=2,***k;\n" 16044 "int &k=i;\n" 16045 "int &&l=i+j;\n" 16046 " int big = 10000;\n" 16047 "\n" 16048 "unsigned oneTwoThree =123;\n" 16049 "int oneTwo = 12;\n" 16050 " method();\n" 16051 "float k= 2;\n" 16052 "int ll=10000;\n" 16053 "}", 16054 Alignment)); 16055 // variables are aligned at their name, pointers are at the right most 16056 // position 16057 verifyFormat("int *a;\n" 16058 "int **b;\n" 16059 "int ***c;\n" 16060 "int foobar;\n", 16061 Alignment); 16062 16063 // PAS_Left 16064 FormatStyle AlignmentLeft = Alignment; 16065 AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left; 16066 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16067 " int const i = 1;\n" 16068 " int* j = 2;\n" 16069 " int big = 10000;\n" 16070 "\n" 16071 " unsigned oneTwoThree = 123;\n" 16072 " int oneTwo = 12;\n" 16073 " method();\n" 16074 " float k = 2;\n" 16075 " int ll = 10000;\n" 16076 "}", 16077 format("void SomeFunction(int parameter= 0) {\n" 16078 " int const i= 1;\n" 16079 " int *j=2;\n" 16080 " int big = 10000;\n" 16081 "\n" 16082 "unsigned oneTwoThree =123;\n" 16083 "int oneTwo = 12;\n" 16084 " method();\n" 16085 "float k= 2;\n" 16086 "int ll=10000;\n" 16087 "}", 16088 AlignmentLeft)); 16089 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16090 " int const i = 1;\n" 16091 " int** j = 2;\n" 16092 " int& k = i;\n" 16093 " int&& l = i + j;\n" 16094 " int big = 10000;\n" 16095 "\n" 16096 " unsigned oneTwoThree = 123;\n" 16097 " int oneTwo = 12;\n" 16098 " method();\n" 16099 " float k = 2;\n" 16100 " int ll = 10000;\n" 16101 "}", 16102 format("void SomeFunction(int parameter= 0) {\n" 16103 " int const i= 1;\n" 16104 " int **j=2;\n" 16105 "int &k=i;\n" 16106 "int &&l=i+j;\n" 16107 " int big = 10000;\n" 16108 "\n" 16109 "unsigned oneTwoThree =123;\n" 16110 "int oneTwo = 12;\n" 16111 " method();\n" 16112 "float k= 2;\n" 16113 "int ll=10000;\n" 16114 "}", 16115 AlignmentLeft)); 16116 // variables are aligned at their name, pointers are at the left most position 16117 verifyFormat("int* a;\n" 16118 "int** b;\n" 16119 "int*** c;\n" 16120 "int foobar;\n", 16121 AlignmentLeft); 16122 16123 // PAS_Middle 16124 FormatStyle AlignmentMiddle = Alignment; 16125 AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle; 16126 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16127 " int const i = 1;\n" 16128 " int * j = 2;\n" 16129 " int big = 10000;\n" 16130 "\n" 16131 " unsigned oneTwoThree = 123;\n" 16132 " int oneTwo = 12;\n" 16133 " method();\n" 16134 " float k = 2;\n" 16135 " int ll = 10000;\n" 16136 "}", 16137 format("void SomeFunction(int parameter= 0) {\n" 16138 " int const i= 1;\n" 16139 " int *j=2;\n" 16140 " int big = 10000;\n" 16141 "\n" 16142 "unsigned oneTwoThree =123;\n" 16143 "int oneTwo = 12;\n" 16144 " method();\n" 16145 "float k= 2;\n" 16146 "int ll=10000;\n" 16147 "}", 16148 AlignmentMiddle)); 16149 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16150 " int const i = 1;\n" 16151 " int ** j = 2, ***k;\n" 16152 " int & k = i;\n" 16153 " int && l = i + j;\n" 16154 " int big = 10000;\n" 16155 "\n" 16156 " unsigned oneTwoThree = 123;\n" 16157 " int oneTwo = 12;\n" 16158 " method();\n" 16159 " float k = 2;\n" 16160 " int ll = 10000;\n" 16161 "}", 16162 format("void SomeFunction(int parameter= 0) {\n" 16163 " int const i= 1;\n" 16164 " int **j=2,***k;\n" 16165 "int &k=i;\n" 16166 "int &&l=i+j;\n" 16167 " int big = 10000;\n" 16168 "\n" 16169 "unsigned oneTwoThree =123;\n" 16170 "int oneTwo = 12;\n" 16171 " method();\n" 16172 "float k= 2;\n" 16173 "int ll=10000;\n" 16174 "}", 16175 AlignmentMiddle)); 16176 // variables are aligned at their name, pointers are in the middle 16177 verifyFormat("int * a;\n" 16178 "int * b;\n" 16179 "int *** c;\n" 16180 "int foobar;\n", 16181 AlignmentMiddle); 16182 16183 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16184 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 16185 verifyFormat("#define A \\\n" 16186 " int aaaa = 12; \\\n" 16187 " float b = 23; \\\n" 16188 " const int ccc = 234; \\\n" 16189 " unsigned dddddddddd = 2345;", 16190 Alignment); 16191 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 16192 verifyFormat("#define A \\\n" 16193 " int aaaa = 12; \\\n" 16194 " float b = 23; \\\n" 16195 " const int ccc = 234; \\\n" 16196 " unsigned dddddddddd = 2345;", 16197 Alignment); 16198 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 16199 Alignment.ColumnLimit = 30; 16200 verifyFormat("#define A \\\n" 16201 " int aaaa = 12; \\\n" 16202 " float b = 23; \\\n" 16203 " const int ccc = 234; \\\n" 16204 " int dddddddddd = 2345;", 16205 Alignment); 16206 Alignment.ColumnLimit = 80; 16207 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 16208 "k = 4, int l = 5,\n" 16209 " int m = 6) {\n" 16210 " const int j = 10;\n" 16211 " otherThing = 1;\n" 16212 "}", 16213 Alignment); 16214 verifyFormat("void SomeFunction(int parameter = 0) {\n" 16215 " int const i = 1;\n" 16216 " int *j = 2;\n" 16217 " int big = 10000;\n" 16218 "}", 16219 Alignment); 16220 verifyFormat("class C {\n" 16221 "public:\n" 16222 " int i = 1;\n" 16223 " virtual void f() = 0;\n" 16224 "};", 16225 Alignment); 16226 verifyFormat("float i = 1;\n" 16227 "if (SomeType t = getSomething()) {\n" 16228 "}\n" 16229 "const unsigned j = 2;\n" 16230 "int big = 10000;", 16231 Alignment); 16232 verifyFormat("float j = 7;\n" 16233 "for (int k = 0; k < N; ++k) {\n" 16234 "}\n" 16235 "unsigned j = 2;\n" 16236 "int big = 10000;\n" 16237 "}", 16238 Alignment); 16239 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 16240 verifyFormat("float i = 1;\n" 16241 "LooooooooooongType loooooooooooooooooooooongVariable\n" 16242 " = someLooooooooooooooooongFunction();\n" 16243 "int j = 2;", 16244 Alignment); 16245 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 16246 verifyFormat("int i = 1;\n" 16247 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 16248 " someLooooooooooooooooongFunction();\n" 16249 "int j = 2;", 16250 Alignment); 16251 16252 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16253 verifyFormat("auto lambda = []() {\n" 16254 " auto ii = 0;\n" 16255 " float j = 0;\n" 16256 " return 0;\n" 16257 "};\n" 16258 "int i = 0;\n" 16259 "float i2 = 0;\n" 16260 "auto v = type{\n" 16261 " i = 1, //\n" 16262 " (i = 2), //\n" 16263 " i = 3 //\n" 16264 "};", 16265 Alignment); 16266 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16267 16268 verifyFormat( 16269 "int i = 1;\n" 16270 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 16271 " loooooooooooooooooooooongParameterB);\n" 16272 "int j = 2;", 16273 Alignment); 16274 16275 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 16276 // We expect declarations and assignments to align, as long as it doesn't 16277 // exceed the column limit, starting a new alignment sequence whenever it 16278 // happens. 16279 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16280 Alignment.ColumnLimit = 30; 16281 verifyFormat("float ii = 1;\n" 16282 "unsigned j = 2;\n" 16283 "int someVerylongVariable = 1;\n" 16284 "AnotherLongType ll = 123456;\n" 16285 "VeryVeryLongType k = 2;\n" 16286 "int myvar = 1;", 16287 Alignment); 16288 Alignment.ColumnLimit = 80; 16289 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16290 16291 verifyFormat( 16292 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 16293 " typename LongType, typename B>\n" 16294 "auto foo() {}\n", 16295 Alignment); 16296 verifyFormat("float a, b = 1;\n" 16297 "int c = 2;\n" 16298 "int dd = 3;\n", 16299 Alignment); 16300 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 16301 "float b[1][] = {{3.f}};\n", 16302 Alignment); 16303 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16304 verifyFormat("float a, b = 1;\n" 16305 "int c = 2;\n" 16306 "int dd = 3;\n", 16307 Alignment); 16308 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 16309 "float b[1][] = {{3.f}};\n", 16310 Alignment); 16311 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16312 16313 Alignment.ColumnLimit = 30; 16314 Alignment.BinPackParameters = false; 16315 verifyFormat("void foo(float a,\n" 16316 " float b,\n" 16317 " int c,\n" 16318 " uint32_t *d) {\n" 16319 " int *e = 0;\n" 16320 " float f = 0;\n" 16321 " double g = 0;\n" 16322 "}\n" 16323 "void bar(ino_t a,\n" 16324 " int b,\n" 16325 " uint32_t *c,\n" 16326 " bool d) {}\n", 16327 Alignment); 16328 Alignment.BinPackParameters = true; 16329 Alignment.ColumnLimit = 80; 16330 16331 // Bug 33507 16332 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 16333 verifyFormat( 16334 "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n" 16335 " static const Version verVs2017;\n" 16336 " return true;\n" 16337 "});\n", 16338 Alignment); 16339 Alignment.PointerAlignment = FormatStyle::PAS_Right; 16340 16341 // See llvm.org/PR35641 16342 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16343 verifyFormat("int func() { //\n" 16344 " int b;\n" 16345 " unsigned c;\n" 16346 "}", 16347 Alignment); 16348 16349 // See PR37175 16350 FormatStyle Style = getMozillaStyle(); 16351 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16352 EXPECT_EQ("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n" 16353 "foo(int a);", 16354 format("DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style)); 16355 16356 Alignment.PointerAlignment = FormatStyle::PAS_Left; 16357 verifyFormat("unsigned int* a;\n" 16358 "int* b;\n" 16359 "unsigned int Const* c;\n" 16360 "unsigned int const* d;\n" 16361 "unsigned int Const& e;\n" 16362 "unsigned int const& f;", 16363 Alignment); 16364 verifyFormat("Const unsigned int* c;\n" 16365 "const unsigned int* d;\n" 16366 "Const unsigned int& e;\n" 16367 "const unsigned int& f;\n" 16368 "const unsigned g;\n" 16369 "Const unsigned h;", 16370 Alignment); 16371 16372 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 16373 verifyFormat("unsigned int * a;\n" 16374 "int * b;\n" 16375 "unsigned int Const * c;\n" 16376 "unsigned int const * d;\n" 16377 "unsigned int Const & e;\n" 16378 "unsigned int const & f;", 16379 Alignment); 16380 verifyFormat("Const unsigned int * c;\n" 16381 "const unsigned int * d;\n" 16382 "Const unsigned int & e;\n" 16383 "const unsigned int & f;\n" 16384 "const unsigned g;\n" 16385 "Const unsigned h;", 16386 Alignment); 16387 } 16388 16389 TEST_F(FormatTest, AlignWithLineBreaks) { 16390 auto Style = getLLVMStyleWithColumns(120); 16391 16392 EXPECT_EQ(Style.AlignConsecutiveAssignments, FormatStyle::ACS_None); 16393 EXPECT_EQ(Style.AlignConsecutiveDeclarations, FormatStyle::ACS_None); 16394 verifyFormat("void foo() {\n" 16395 " int myVar = 5;\n" 16396 " double x = 3.14;\n" 16397 " auto str = \"Hello \"\n" 16398 " \"World\";\n" 16399 " auto s = \"Hello \"\n" 16400 " \"Again\";\n" 16401 "}", 16402 Style); 16403 16404 // clang-format off 16405 verifyFormat("void foo() {\n" 16406 " const int capacityBefore = Entries.capacity();\n" 16407 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16408 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16409 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16410 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16411 "}", 16412 Style); 16413 // clang-format on 16414 16415 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16416 verifyFormat("void foo() {\n" 16417 " int myVar = 5;\n" 16418 " double x = 3.14;\n" 16419 " auto str = \"Hello \"\n" 16420 " \"World\";\n" 16421 " auto s = \"Hello \"\n" 16422 " \"Again\";\n" 16423 "}", 16424 Style); 16425 16426 // clang-format off 16427 verifyFormat("void foo() {\n" 16428 " const int capacityBefore = Entries.capacity();\n" 16429 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16430 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16431 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16432 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16433 "}", 16434 Style); 16435 // clang-format on 16436 16437 Style.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16438 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16439 verifyFormat("void foo() {\n" 16440 " int myVar = 5;\n" 16441 " double x = 3.14;\n" 16442 " auto str = \"Hello \"\n" 16443 " \"World\";\n" 16444 " auto s = \"Hello \"\n" 16445 " \"Again\";\n" 16446 "}", 16447 Style); 16448 16449 // clang-format off 16450 verifyFormat("void foo() {\n" 16451 " const int capacityBefore = Entries.capacity();\n" 16452 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16453 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16454 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16455 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16456 "}", 16457 Style); 16458 // clang-format on 16459 16460 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16461 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16462 16463 verifyFormat("void foo() {\n" 16464 " int myVar = 5;\n" 16465 " double x = 3.14;\n" 16466 " auto str = \"Hello \"\n" 16467 " \"World\";\n" 16468 " auto s = \"Hello \"\n" 16469 " \"Again\";\n" 16470 "}", 16471 Style); 16472 16473 // clang-format off 16474 verifyFormat("void foo() {\n" 16475 " const int capacityBefore = Entries.capacity();\n" 16476 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16477 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16478 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16479 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16480 "}", 16481 Style); 16482 // clang-format on 16483 16484 Style = getLLVMStyleWithColumns(120); 16485 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16486 Style.ContinuationIndentWidth = 4; 16487 Style.IndentWidth = 4; 16488 16489 // clang-format off 16490 verifyFormat("void SomeFunc() {\n" 16491 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 16492 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16493 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 16494 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16495 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 16496 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16497 "}", 16498 Style); 16499 // clang-format on 16500 16501 Style.BinPackArguments = false; 16502 16503 // clang-format off 16504 verifyFormat("void SomeFunc() {\n" 16505 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(\n" 16506 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16507 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(\n" 16508 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16509 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(\n" 16510 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16511 "}", 16512 Style); 16513 // clang-format on 16514 } 16515 16516 TEST_F(FormatTest, AlignWithInitializerPeriods) { 16517 auto Style = getLLVMStyleWithColumns(60); 16518 16519 verifyFormat("void foo1(void) {\n" 16520 " BYTE p[1] = 1;\n" 16521 " A B = {.one_foooooooooooooooo = 2,\n" 16522 " .two_fooooooooooooo = 3,\n" 16523 " .three_fooooooooooooo = 4};\n" 16524 " BYTE payload = 2;\n" 16525 "}", 16526 Style); 16527 16528 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16529 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_None; 16530 verifyFormat("void foo2(void) {\n" 16531 " BYTE p[1] = 1;\n" 16532 " A B = {.one_foooooooooooooooo = 2,\n" 16533 " .two_fooooooooooooo = 3,\n" 16534 " .three_fooooooooooooo = 4};\n" 16535 " BYTE payload = 2;\n" 16536 "}", 16537 Style); 16538 16539 Style.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16540 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16541 verifyFormat("void foo3(void) {\n" 16542 " BYTE p[1] = 1;\n" 16543 " A B = {.one_foooooooooooooooo = 2,\n" 16544 " .two_fooooooooooooo = 3,\n" 16545 " .three_fooooooooooooo = 4};\n" 16546 " BYTE payload = 2;\n" 16547 "}", 16548 Style); 16549 16550 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16551 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16552 verifyFormat("void foo4(void) {\n" 16553 " BYTE p[1] = 1;\n" 16554 " A B = {.one_foooooooooooooooo = 2,\n" 16555 " .two_fooooooooooooo = 3,\n" 16556 " .three_fooooooooooooo = 4};\n" 16557 " BYTE payload = 2;\n" 16558 "}", 16559 Style); 16560 } 16561 16562 TEST_F(FormatTest, LinuxBraceBreaking) { 16563 FormatStyle LinuxBraceStyle = getLLVMStyle(); 16564 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 16565 verifyFormat("namespace a\n" 16566 "{\n" 16567 "class A\n" 16568 "{\n" 16569 " void f()\n" 16570 " {\n" 16571 " if (true) {\n" 16572 " a();\n" 16573 " b();\n" 16574 " } else {\n" 16575 " a();\n" 16576 " }\n" 16577 " }\n" 16578 " void g() { return; }\n" 16579 "};\n" 16580 "struct B {\n" 16581 " int x;\n" 16582 "};\n" 16583 "} // namespace a\n", 16584 LinuxBraceStyle); 16585 verifyFormat("enum X {\n" 16586 " Y = 0,\n" 16587 "}\n", 16588 LinuxBraceStyle); 16589 verifyFormat("struct S {\n" 16590 " int Type;\n" 16591 " union {\n" 16592 " int x;\n" 16593 " double y;\n" 16594 " } Value;\n" 16595 " class C\n" 16596 " {\n" 16597 " MyFavoriteType Value;\n" 16598 " } Class;\n" 16599 "}\n", 16600 LinuxBraceStyle); 16601 } 16602 16603 TEST_F(FormatTest, MozillaBraceBreaking) { 16604 FormatStyle MozillaBraceStyle = getLLVMStyle(); 16605 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 16606 MozillaBraceStyle.FixNamespaceComments = false; 16607 verifyFormat("namespace a {\n" 16608 "class A\n" 16609 "{\n" 16610 " void f()\n" 16611 " {\n" 16612 " if (true) {\n" 16613 " a();\n" 16614 " b();\n" 16615 " }\n" 16616 " }\n" 16617 " void g() { return; }\n" 16618 "};\n" 16619 "enum E\n" 16620 "{\n" 16621 " A,\n" 16622 " // foo\n" 16623 " B,\n" 16624 " C\n" 16625 "};\n" 16626 "struct B\n" 16627 "{\n" 16628 " int x;\n" 16629 "};\n" 16630 "}\n", 16631 MozillaBraceStyle); 16632 verifyFormat("struct S\n" 16633 "{\n" 16634 " int Type;\n" 16635 " union\n" 16636 " {\n" 16637 " int x;\n" 16638 " double y;\n" 16639 " } Value;\n" 16640 " class C\n" 16641 " {\n" 16642 " MyFavoriteType Value;\n" 16643 " } Class;\n" 16644 "}\n", 16645 MozillaBraceStyle); 16646 } 16647 16648 TEST_F(FormatTest, StroustrupBraceBreaking) { 16649 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 16650 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 16651 verifyFormat("namespace a {\n" 16652 "class A {\n" 16653 " void f()\n" 16654 " {\n" 16655 " if (true) {\n" 16656 " a();\n" 16657 " b();\n" 16658 " }\n" 16659 " }\n" 16660 " void g() { return; }\n" 16661 "};\n" 16662 "struct B {\n" 16663 " int x;\n" 16664 "};\n" 16665 "} // namespace a\n", 16666 StroustrupBraceStyle); 16667 16668 verifyFormat("void foo()\n" 16669 "{\n" 16670 " if (a) {\n" 16671 " a();\n" 16672 " }\n" 16673 " else {\n" 16674 " b();\n" 16675 " }\n" 16676 "}\n", 16677 StroustrupBraceStyle); 16678 16679 verifyFormat("#ifdef _DEBUG\n" 16680 "int foo(int i = 0)\n" 16681 "#else\n" 16682 "int foo(int i = 5)\n" 16683 "#endif\n" 16684 "{\n" 16685 " return i;\n" 16686 "}", 16687 StroustrupBraceStyle); 16688 16689 verifyFormat("void foo() {}\n" 16690 "void bar()\n" 16691 "#ifdef _DEBUG\n" 16692 "{\n" 16693 " foo();\n" 16694 "}\n" 16695 "#else\n" 16696 "{\n" 16697 "}\n" 16698 "#endif", 16699 StroustrupBraceStyle); 16700 16701 verifyFormat("void foobar() { int i = 5; }\n" 16702 "#ifdef _DEBUG\n" 16703 "void bar() {}\n" 16704 "#else\n" 16705 "void bar() { foobar(); }\n" 16706 "#endif", 16707 StroustrupBraceStyle); 16708 } 16709 16710 TEST_F(FormatTest, AllmanBraceBreaking) { 16711 FormatStyle AllmanBraceStyle = getLLVMStyle(); 16712 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 16713 16714 EXPECT_EQ("namespace a\n" 16715 "{\n" 16716 "void f();\n" 16717 "void g();\n" 16718 "} // namespace a\n", 16719 format("namespace a\n" 16720 "{\n" 16721 "void f();\n" 16722 "void g();\n" 16723 "}\n", 16724 AllmanBraceStyle)); 16725 16726 verifyFormat("namespace a\n" 16727 "{\n" 16728 "class A\n" 16729 "{\n" 16730 " void f()\n" 16731 " {\n" 16732 " if (true)\n" 16733 " {\n" 16734 " a();\n" 16735 " b();\n" 16736 " }\n" 16737 " }\n" 16738 " void g() { return; }\n" 16739 "};\n" 16740 "struct B\n" 16741 "{\n" 16742 " int x;\n" 16743 "};\n" 16744 "union C\n" 16745 "{\n" 16746 "};\n" 16747 "} // namespace a", 16748 AllmanBraceStyle); 16749 16750 verifyFormat("void f()\n" 16751 "{\n" 16752 " if (true)\n" 16753 " {\n" 16754 " a();\n" 16755 " }\n" 16756 " else if (false)\n" 16757 " {\n" 16758 " b();\n" 16759 " }\n" 16760 " else\n" 16761 " {\n" 16762 " c();\n" 16763 " }\n" 16764 "}\n", 16765 AllmanBraceStyle); 16766 16767 verifyFormat("void f()\n" 16768 "{\n" 16769 " for (int i = 0; i < 10; ++i)\n" 16770 " {\n" 16771 " a();\n" 16772 " }\n" 16773 " while (false)\n" 16774 " {\n" 16775 " b();\n" 16776 " }\n" 16777 " do\n" 16778 " {\n" 16779 " c();\n" 16780 " } while (false)\n" 16781 "}\n", 16782 AllmanBraceStyle); 16783 16784 verifyFormat("void f(int a)\n" 16785 "{\n" 16786 " switch (a)\n" 16787 " {\n" 16788 " case 0:\n" 16789 " break;\n" 16790 " case 1:\n" 16791 " {\n" 16792 " break;\n" 16793 " }\n" 16794 " case 2:\n" 16795 " {\n" 16796 " }\n" 16797 " break;\n" 16798 " default:\n" 16799 " break;\n" 16800 " }\n" 16801 "}\n", 16802 AllmanBraceStyle); 16803 16804 verifyFormat("enum X\n" 16805 "{\n" 16806 " Y = 0,\n" 16807 "}\n", 16808 AllmanBraceStyle); 16809 verifyFormat("enum X\n" 16810 "{\n" 16811 " Y = 0\n" 16812 "}\n", 16813 AllmanBraceStyle); 16814 16815 verifyFormat("@interface BSApplicationController ()\n" 16816 "{\n" 16817 "@private\n" 16818 " id _extraIvar;\n" 16819 "}\n" 16820 "@end\n", 16821 AllmanBraceStyle); 16822 16823 verifyFormat("#ifdef _DEBUG\n" 16824 "int foo(int i = 0)\n" 16825 "#else\n" 16826 "int foo(int i = 5)\n" 16827 "#endif\n" 16828 "{\n" 16829 " return i;\n" 16830 "}", 16831 AllmanBraceStyle); 16832 16833 verifyFormat("void foo() {}\n" 16834 "void bar()\n" 16835 "#ifdef _DEBUG\n" 16836 "{\n" 16837 " foo();\n" 16838 "}\n" 16839 "#else\n" 16840 "{\n" 16841 "}\n" 16842 "#endif", 16843 AllmanBraceStyle); 16844 16845 verifyFormat("void foobar() { int i = 5; }\n" 16846 "#ifdef _DEBUG\n" 16847 "void bar() {}\n" 16848 "#else\n" 16849 "void bar() { foobar(); }\n" 16850 "#endif", 16851 AllmanBraceStyle); 16852 16853 EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine, 16854 FormatStyle::SLS_All); 16855 16856 verifyFormat("[](int i) { return i + 2; };\n" 16857 "[](int i, int j)\n" 16858 "{\n" 16859 " auto x = i + j;\n" 16860 " auto y = i * j;\n" 16861 " return x ^ y;\n" 16862 "};\n" 16863 "void foo()\n" 16864 "{\n" 16865 " auto shortLambda = [](int i) { return i + 2; };\n" 16866 " auto longLambda = [](int i, int j)\n" 16867 " {\n" 16868 " auto x = i + j;\n" 16869 " auto y = i * j;\n" 16870 " return x ^ y;\n" 16871 " };\n" 16872 "}", 16873 AllmanBraceStyle); 16874 16875 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 16876 16877 verifyFormat("[](int i)\n" 16878 "{\n" 16879 " return i + 2;\n" 16880 "};\n" 16881 "[](int i, int j)\n" 16882 "{\n" 16883 " auto x = i + j;\n" 16884 " auto y = i * j;\n" 16885 " return x ^ y;\n" 16886 "};\n" 16887 "void foo()\n" 16888 "{\n" 16889 " auto shortLambda = [](int i)\n" 16890 " {\n" 16891 " return i + 2;\n" 16892 " };\n" 16893 " auto longLambda = [](int i, int j)\n" 16894 " {\n" 16895 " auto x = i + j;\n" 16896 " auto y = i * j;\n" 16897 " return x ^ y;\n" 16898 " };\n" 16899 "}", 16900 AllmanBraceStyle); 16901 16902 // Reset 16903 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All; 16904 16905 // This shouldn't affect ObjC blocks.. 16906 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 16907 " // ...\n" 16908 " int i;\n" 16909 "}];", 16910 AllmanBraceStyle); 16911 verifyFormat("void (^block)(void) = ^{\n" 16912 " // ...\n" 16913 " int i;\n" 16914 "};", 16915 AllmanBraceStyle); 16916 // .. or dict literals. 16917 verifyFormat("void f()\n" 16918 "{\n" 16919 " // ...\n" 16920 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 16921 "}", 16922 AllmanBraceStyle); 16923 verifyFormat("void f()\n" 16924 "{\n" 16925 " // ...\n" 16926 " [object someMethod:@{a : @\"b\"}];\n" 16927 "}", 16928 AllmanBraceStyle); 16929 verifyFormat("int f()\n" 16930 "{ // comment\n" 16931 " return 42;\n" 16932 "}", 16933 AllmanBraceStyle); 16934 16935 AllmanBraceStyle.ColumnLimit = 19; 16936 verifyFormat("void f() { int i; }", AllmanBraceStyle); 16937 AllmanBraceStyle.ColumnLimit = 18; 16938 verifyFormat("void f()\n" 16939 "{\n" 16940 " int i;\n" 16941 "}", 16942 AllmanBraceStyle); 16943 AllmanBraceStyle.ColumnLimit = 80; 16944 16945 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 16946 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = 16947 FormatStyle::SIS_WithoutElse; 16948 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 16949 verifyFormat("void f(bool b)\n" 16950 "{\n" 16951 " if (b)\n" 16952 " {\n" 16953 " return;\n" 16954 " }\n" 16955 "}\n", 16956 BreakBeforeBraceShortIfs); 16957 verifyFormat("void f(bool b)\n" 16958 "{\n" 16959 " if constexpr (b)\n" 16960 " {\n" 16961 " return;\n" 16962 " }\n" 16963 "}\n", 16964 BreakBeforeBraceShortIfs); 16965 verifyFormat("void f(bool b)\n" 16966 "{\n" 16967 " if CONSTEXPR (b)\n" 16968 " {\n" 16969 " return;\n" 16970 " }\n" 16971 "}\n", 16972 BreakBeforeBraceShortIfs); 16973 verifyFormat("void f(bool b)\n" 16974 "{\n" 16975 " if (b) return;\n" 16976 "}\n", 16977 BreakBeforeBraceShortIfs); 16978 verifyFormat("void f(bool b)\n" 16979 "{\n" 16980 " if constexpr (b) return;\n" 16981 "}\n", 16982 BreakBeforeBraceShortIfs); 16983 verifyFormat("void f(bool b)\n" 16984 "{\n" 16985 " if CONSTEXPR (b) return;\n" 16986 "}\n", 16987 BreakBeforeBraceShortIfs); 16988 verifyFormat("void f(bool b)\n" 16989 "{\n" 16990 " while (b)\n" 16991 " {\n" 16992 " return;\n" 16993 " }\n" 16994 "}\n", 16995 BreakBeforeBraceShortIfs); 16996 } 16997 16998 TEST_F(FormatTest, WhitesmithsBraceBreaking) { 16999 FormatStyle WhitesmithsBraceStyle = getLLVMStyle(); 17000 WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 17001 17002 // Make a few changes to the style for testing purposes 17003 WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine = 17004 FormatStyle::SFS_Empty; 17005 WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 17006 WhitesmithsBraceStyle.ColumnLimit = 0; 17007 17008 // FIXME: this test case can't decide whether there should be a blank line 17009 // after the ~D() line or not. It adds one if one doesn't exist in the test 17010 // and it removes the line if one exists. 17011 /* 17012 verifyFormat("class A;\n" 17013 "namespace B\n" 17014 " {\n" 17015 "class C;\n" 17016 "// Comment\n" 17017 "class D\n" 17018 " {\n" 17019 "public:\n" 17020 " D();\n" 17021 " ~D() {}\n" 17022 "private:\n" 17023 " enum E\n" 17024 " {\n" 17025 " F\n" 17026 " }\n" 17027 " };\n" 17028 " } // namespace B\n", 17029 WhitesmithsBraceStyle); 17030 */ 17031 17032 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None; 17033 verifyFormat("namespace a\n" 17034 " {\n" 17035 "class A\n" 17036 " {\n" 17037 " void f()\n" 17038 " {\n" 17039 " if (true)\n" 17040 " {\n" 17041 " a();\n" 17042 " b();\n" 17043 " }\n" 17044 " }\n" 17045 " void g()\n" 17046 " {\n" 17047 " return;\n" 17048 " }\n" 17049 " };\n" 17050 "struct B\n" 17051 " {\n" 17052 " int x;\n" 17053 " };\n" 17054 " } // namespace a", 17055 WhitesmithsBraceStyle); 17056 17057 verifyFormat("namespace a\n" 17058 " {\n" 17059 "namespace b\n" 17060 " {\n" 17061 "class A\n" 17062 " {\n" 17063 " void f()\n" 17064 " {\n" 17065 " if (true)\n" 17066 " {\n" 17067 " a();\n" 17068 " b();\n" 17069 " }\n" 17070 " }\n" 17071 " void g()\n" 17072 " {\n" 17073 " return;\n" 17074 " }\n" 17075 " };\n" 17076 "struct B\n" 17077 " {\n" 17078 " int x;\n" 17079 " };\n" 17080 " } // namespace b\n" 17081 " } // namespace a", 17082 WhitesmithsBraceStyle); 17083 17084 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner; 17085 verifyFormat("namespace a\n" 17086 " {\n" 17087 "namespace b\n" 17088 " {\n" 17089 " class A\n" 17090 " {\n" 17091 " void f()\n" 17092 " {\n" 17093 " if (true)\n" 17094 " {\n" 17095 " a();\n" 17096 " b();\n" 17097 " }\n" 17098 " }\n" 17099 " void g()\n" 17100 " {\n" 17101 " return;\n" 17102 " }\n" 17103 " };\n" 17104 " struct B\n" 17105 " {\n" 17106 " int x;\n" 17107 " };\n" 17108 " } // namespace b\n" 17109 " } // namespace a", 17110 WhitesmithsBraceStyle); 17111 17112 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All; 17113 verifyFormat("namespace a\n" 17114 " {\n" 17115 " namespace b\n" 17116 " {\n" 17117 " class A\n" 17118 " {\n" 17119 " void f()\n" 17120 " {\n" 17121 " if (true)\n" 17122 " {\n" 17123 " a();\n" 17124 " b();\n" 17125 " }\n" 17126 " }\n" 17127 " void g()\n" 17128 " {\n" 17129 " return;\n" 17130 " }\n" 17131 " };\n" 17132 " struct B\n" 17133 " {\n" 17134 " int x;\n" 17135 " };\n" 17136 " } // namespace b\n" 17137 " } // namespace a", 17138 WhitesmithsBraceStyle); 17139 17140 verifyFormat("void f()\n" 17141 " {\n" 17142 " if (true)\n" 17143 " {\n" 17144 " a();\n" 17145 " }\n" 17146 " else if (false)\n" 17147 " {\n" 17148 " b();\n" 17149 " }\n" 17150 " else\n" 17151 " {\n" 17152 " c();\n" 17153 " }\n" 17154 " }\n", 17155 WhitesmithsBraceStyle); 17156 17157 verifyFormat("void f()\n" 17158 " {\n" 17159 " for (int i = 0; i < 10; ++i)\n" 17160 " {\n" 17161 " a();\n" 17162 " }\n" 17163 " while (false)\n" 17164 " {\n" 17165 " b();\n" 17166 " }\n" 17167 " do\n" 17168 " {\n" 17169 " c();\n" 17170 " } while (false)\n" 17171 " }\n", 17172 WhitesmithsBraceStyle); 17173 17174 WhitesmithsBraceStyle.IndentCaseLabels = true; 17175 verifyFormat("void switchTest1(int a)\n" 17176 " {\n" 17177 " switch (a)\n" 17178 " {\n" 17179 " case 2:\n" 17180 " {\n" 17181 " }\n" 17182 " break;\n" 17183 " }\n" 17184 " }\n", 17185 WhitesmithsBraceStyle); 17186 17187 verifyFormat("void switchTest2(int a)\n" 17188 " {\n" 17189 " switch (a)\n" 17190 " {\n" 17191 " case 0:\n" 17192 " break;\n" 17193 " case 1:\n" 17194 " {\n" 17195 " break;\n" 17196 " }\n" 17197 " case 2:\n" 17198 " {\n" 17199 " }\n" 17200 " break;\n" 17201 " default:\n" 17202 " break;\n" 17203 " }\n" 17204 " }\n", 17205 WhitesmithsBraceStyle); 17206 17207 verifyFormat("void switchTest3(int a)\n" 17208 " {\n" 17209 " switch (a)\n" 17210 " {\n" 17211 " case 0:\n" 17212 " {\n" 17213 " foo(x);\n" 17214 " }\n" 17215 " break;\n" 17216 " default:\n" 17217 " {\n" 17218 " foo(1);\n" 17219 " }\n" 17220 " break;\n" 17221 " }\n" 17222 " }\n", 17223 WhitesmithsBraceStyle); 17224 17225 WhitesmithsBraceStyle.IndentCaseLabels = false; 17226 17227 verifyFormat("void switchTest4(int a)\n" 17228 " {\n" 17229 " switch (a)\n" 17230 " {\n" 17231 " case 2:\n" 17232 " {\n" 17233 " }\n" 17234 " break;\n" 17235 " }\n" 17236 " }\n", 17237 WhitesmithsBraceStyle); 17238 17239 verifyFormat("void switchTest5(int a)\n" 17240 " {\n" 17241 " switch (a)\n" 17242 " {\n" 17243 " case 0:\n" 17244 " break;\n" 17245 " case 1:\n" 17246 " {\n" 17247 " foo();\n" 17248 " break;\n" 17249 " }\n" 17250 " case 2:\n" 17251 " {\n" 17252 " }\n" 17253 " break;\n" 17254 " default:\n" 17255 " break;\n" 17256 " }\n" 17257 " }\n", 17258 WhitesmithsBraceStyle); 17259 17260 verifyFormat("void switchTest6(int a)\n" 17261 " {\n" 17262 " switch (a)\n" 17263 " {\n" 17264 " case 0:\n" 17265 " {\n" 17266 " foo(x);\n" 17267 " }\n" 17268 " break;\n" 17269 " default:\n" 17270 " {\n" 17271 " foo(1);\n" 17272 " }\n" 17273 " break;\n" 17274 " }\n" 17275 " }\n", 17276 WhitesmithsBraceStyle); 17277 17278 verifyFormat("enum X\n" 17279 " {\n" 17280 " Y = 0, // testing\n" 17281 " }\n", 17282 WhitesmithsBraceStyle); 17283 17284 verifyFormat("enum X\n" 17285 " {\n" 17286 " Y = 0\n" 17287 " }\n", 17288 WhitesmithsBraceStyle); 17289 verifyFormat("enum X\n" 17290 " {\n" 17291 " Y = 0,\n" 17292 " Z = 1\n" 17293 " };\n", 17294 WhitesmithsBraceStyle); 17295 17296 verifyFormat("@interface BSApplicationController ()\n" 17297 " {\n" 17298 "@private\n" 17299 " id _extraIvar;\n" 17300 " }\n" 17301 "@end\n", 17302 WhitesmithsBraceStyle); 17303 17304 verifyFormat("#ifdef _DEBUG\n" 17305 "int foo(int i = 0)\n" 17306 "#else\n" 17307 "int foo(int i = 5)\n" 17308 "#endif\n" 17309 " {\n" 17310 " return i;\n" 17311 " }", 17312 WhitesmithsBraceStyle); 17313 17314 verifyFormat("void foo() {}\n" 17315 "void bar()\n" 17316 "#ifdef _DEBUG\n" 17317 " {\n" 17318 " foo();\n" 17319 " }\n" 17320 "#else\n" 17321 " {\n" 17322 " }\n" 17323 "#endif", 17324 WhitesmithsBraceStyle); 17325 17326 verifyFormat("void foobar()\n" 17327 " {\n" 17328 " int i = 5;\n" 17329 " }\n" 17330 "#ifdef _DEBUG\n" 17331 "void bar()\n" 17332 " {\n" 17333 " }\n" 17334 "#else\n" 17335 "void bar()\n" 17336 " {\n" 17337 " foobar();\n" 17338 " }\n" 17339 "#endif", 17340 WhitesmithsBraceStyle); 17341 17342 // This shouldn't affect ObjC blocks.. 17343 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 17344 " // ...\n" 17345 " int i;\n" 17346 "}];", 17347 WhitesmithsBraceStyle); 17348 verifyFormat("void (^block)(void) = ^{\n" 17349 " // ...\n" 17350 " int i;\n" 17351 "};", 17352 WhitesmithsBraceStyle); 17353 // .. or dict literals. 17354 verifyFormat("void f()\n" 17355 " {\n" 17356 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 17357 " }", 17358 WhitesmithsBraceStyle); 17359 17360 verifyFormat("int f()\n" 17361 " { // comment\n" 17362 " return 42;\n" 17363 " }", 17364 WhitesmithsBraceStyle); 17365 17366 FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle; 17367 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = 17368 FormatStyle::SIS_OnlyFirstIf; 17369 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 17370 verifyFormat("void f(bool b)\n" 17371 " {\n" 17372 " if (b)\n" 17373 " {\n" 17374 " return;\n" 17375 " }\n" 17376 " }\n", 17377 BreakBeforeBraceShortIfs); 17378 verifyFormat("void f(bool b)\n" 17379 " {\n" 17380 " if (b) return;\n" 17381 " }\n", 17382 BreakBeforeBraceShortIfs); 17383 verifyFormat("void f(bool b)\n" 17384 " {\n" 17385 " while (b)\n" 17386 " {\n" 17387 " return;\n" 17388 " }\n" 17389 " }\n", 17390 BreakBeforeBraceShortIfs); 17391 } 17392 17393 TEST_F(FormatTest, GNUBraceBreaking) { 17394 FormatStyle GNUBraceStyle = getLLVMStyle(); 17395 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 17396 verifyFormat("namespace a\n" 17397 "{\n" 17398 "class A\n" 17399 "{\n" 17400 " void f()\n" 17401 " {\n" 17402 " int a;\n" 17403 " {\n" 17404 " int b;\n" 17405 " }\n" 17406 " if (true)\n" 17407 " {\n" 17408 " a();\n" 17409 " b();\n" 17410 " }\n" 17411 " }\n" 17412 " void g() { return; }\n" 17413 "}\n" 17414 "} // namespace a", 17415 GNUBraceStyle); 17416 17417 verifyFormat("void f()\n" 17418 "{\n" 17419 " if (true)\n" 17420 " {\n" 17421 " a();\n" 17422 " }\n" 17423 " else if (false)\n" 17424 " {\n" 17425 " b();\n" 17426 " }\n" 17427 " else\n" 17428 " {\n" 17429 " c();\n" 17430 " }\n" 17431 "}\n", 17432 GNUBraceStyle); 17433 17434 verifyFormat("void f()\n" 17435 "{\n" 17436 " for (int i = 0; i < 10; ++i)\n" 17437 " {\n" 17438 " a();\n" 17439 " }\n" 17440 " while (false)\n" 17441 " {\n" 17442 " b();\n" 17443 " }\n" 17444 " do\n" 17445 " {\n" 17446 " c();\n" 17447 " }\n" 17448 " while (false);\n" 17449 "}\n", 17450 GNUBraceStyle); 17451 17452 verifyFormat("void f(int a)\n" 17453 "{\n" 17454 " switch (a)\n" 17455 " {\n" 17456 " case 0:\n" 17457 " break;\n" 17458 " case 1:\n" 17459 " {\n" 17460 " break;\n" 17461 " }\n" 17462 " case 2:\n" 17463 " {\n" 17464 " }\n" 17465 " break;\n" 17466 " default:\n" 17467 " break;\n" 17468 " }\n" 17469 "}\n", 17470 GNUBraceStyle); 17471 17472 verifyFormat("enum X\n" 17473 "{\n" 17474 " Y = 0,\n" 17475 "}\n", 17476 GNUBraceStyle); 17477 17478 verifyFormat("@interface BSApplicationController ()\n" 17479 "{\n" 17480 "@private\n" 17481 " id _extraIvar;\n" 17482 "}\n" 17483 "@end\n", 17484 GNUBraceStyle); 17485 17486 verifyFormat("#ifdef _DEBUG\n" 17487 "int foo(int i = 0)\n" 17488 "#else\n" 17489 "int foo(int i = 5)\n" 17490 "#endif\n" 17491 "{\n" 17492 " return i;\n" 17493 "}", 17494 GNUBraceStyle); 17495 17496 verifyFormat("void foo() {}\n" 17497 "void bar()\n" 17498 "#ifdef _DEBUG\n" 17499 "{\n" 17500 " foo();\n" 17501 "}\n" 17502 "#else\n" 17503 "{\n" 17504 "}\n" 17505 "#endif", 17506 GNUBraceStyle); 17507 17508 verifyFormat("void foobar() { int i = 5; }\n" 17509 "#ifdef _DEBUG\n" 17510 "void bar() {}\n" 17511 "#else\n" 17512 "void bar() { foobar(); }\n" 17513 "#endif", 17514 GNUBraceStyle); 17515 } 17516 17517 TEST_F(FormatTest, WebKitBraceBreaking) { 17518 FormatStyle WebKitBraceStyle = getLLVMStyle(); 17519 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 17520 WebKitBraceStyle.FixNamespaceComments = false; 17521 verifyFormat("namespace a {\n" 17522 "class A {\n" 17523 " void f()\n" 17524 " {\n" 17525 " if (true) {\n" 17526 " a();\n" 17527 " b();\n" 17528 " }\n" 17529 " }\n" 17530 " void g() { return; }\n" 17531 "};\n" 17532 "enum E {\n" 17533 " A,\n" 17534 " // foo\n" 17535 " B,\n" 17536 " C\n" 17537 "};\n" 17538 "struct B {\n" 17539 " int x;\n" 17540 "};\n" 17541 "}\n", 17542 WebKitBraceStyle); 17543 verifyFormat("struct S {\n" 17544 " int Type;\n" 17545 " union {\n" 17546 " int x;\n" 17547 " double y;\n" 17548 " } Value;\n" 17549 " class C {\n" 17550 " MyFavoriteType Value;\n" 17551 " } Class;\n" 17552 "};\n", 17553 WebKitBraceStyle); 17554 } 17555 17556 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 17557 verifyFormat("void f() {\n" 17558 " try {\n" 17559 " } catch (const Exception &e) {\n" 17560 " }\n" 17561 "}\n", 17562 getLLVMStyle()); 17563 } 17564 17565 TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) { 17566 auto Style = getLLVMStyle(); 17567 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 17568 Style.AlignConsecutiveAssignments = 17569 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 17570 Style.AlignConsecutiveDeclarations = 17571 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 17572 verifyFormat("struct test demo[] = {\n" 17573 " {56, 23, \"hello\"},\n" 17574 " {-1, 93463, \"world\"},\n" 17575 " { 7, 5, \"!!\"}\n" 17576 "};\n", 17577 Style); 17578 17579 verifyFormat("struct test demo[] = {\n" 17580 " {56, 23, \"hello\"}, // first line\n" 17581 " {-1, 93463, \"world\"}, // second line\n" 17582 " { 7, 5, \"!!\"} // third line\n" 17583 "};\n", 17584 Style); 17585 17586 verifyFormat("struct test demo[4] = {\n" 17587 " { 56, 23, 21, \"oh\"}, // first line\n" 17588 " { -1, 93463, 22, \"my\"}, // second line\n" 17589 " { 7, 5, 1, \"goodness\"} // third line\n" 17590 " {234, 5, 1, \"gracious\"} // fourth line\n" 17591 "};\n", 17592 Style); 17593 17594 verifyFormat("struct test demo[3] = {\n" 17595 " {56, 23, \"hello\"},\n" 17596 " {-1, 93463, \"world\"},\n" 17597 " { 7, 5, \"!!\"}\n" 17598 "};\n", 17599 Style); 17600 17601 verifyFormat("struct test demo[3] = {\n" 17602 " {int{56}, 23, \"hello\"},\n" 17603 " {int{-1}, 93463, \"world\"},\n" 17604 " { int{7}, 5, \"!!\"}\n" 17605 "};\n", 17606 Style); 17607 17608 verifyFormat("struct test demo[] = {\n" 17609 " {56, 23, \"hello\"},\n" 17610 " {-1, 93463, \"world\"},\n" 17611 " { 7, 5, \"!!\"},\n" 17612 "};\n", 17613 Style); 17614 17615 verifyFormat("test demo[] = {\n" 17616 " {56, 23, \"hello\"},\n" 17617 " {-1, 93463, \"world\"},\n" 17618 " { 7, 5, \"!!\"},\n" 17619 "};\n", 17620 Style); 17621 17622 verifyFormat("demo = std::array<struct test, 3>{\n" 17623 " test{56, 23, \"hello\"},\n" 17624 " test{-1, 93463, \"world\"},\n" 17625 " test{ 7, 5, \"!!\"},\n" 17626 "};\n", 17627 Style); 17628 17629 verifyFormat("test demo[] = {\n" 17630 " {56, 23, \"hello\"},\n" 17631 "#if X\n" 17632 " {-1, 93463, \"world\"},\n" 17633 "#endif\n" 17634 " { 7, 5, \"!!\"}\n" 17635 "};\n", 17636 Style); 17637 17638 verifyFormat( 17639 "test demo[] = {\n" 17640 " { 7, 23,\n" 17641 " \"hello world i am a very long line that really, in any\"\n" 17642 " \"just world, ought to be split over multiple lines\"},\n" 17643 " {-1, 93463, \"world\"},\n" 17644 " {56, 5, \"!!\"}\n" 17645 "};\n", 17646 Style); 17647 17648 verifyFormat("return GradForUnaryCwise(g, {\n" 17649 " {{\"sign\"}, \"Sign\", " 17650 " {\"x\", \"dy\"}},\n" 17651 " { {\"dx\"}, \"Mul\", {\"dy\"" 17652 ", \"sign\"}},\n" 17653 "});\n", 17654 Style); 17655 17656 Style.ColumnLimit = 0; 17657 EXPECT_EQ( 17658 "test demo[] = {\n" 17659 " {56, 23, \"hello world i am a very long line that really, " 17660 "in any just world, ought to be split over multiple lines\"},\n" 17661 " {-1, 93463, " 17662 " \"world\"},\n" 17663 " { 7, 5, " 17664 " \"!!\"},\n" 17665 "};", 17666 format("test demo[] = {{56, 23, \"hello world i am a very long line " 17667 "that really, in any just world, ought to be split over multiple " 17668 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 17669 Style)); 17670 17671 Style.ColumnLimit = 80; 17672 verifyFormat("test demo[] = {\n" 17673 " {56, 23, /* a comment */ \"hello\"},\n" 17674 " {-1, 93463, \"world\"},\n" 17675 " { 7, 5, \"!!\"}\n" 17676 "};\n", 17677 Style); 17678 17679 verifyFormat("test demo[] = {\n" 17680 " {56, 23, \"hello\"},\n" 17681 " {-1, 93463, \"world\" /* comment here */},\n" 17682 " { 7, 5, \"!!\"}\n" 17683 "};\n", 17684 Style); 17685 17686 verifyFormat("test demo[] = {\n" 17687 " {56, /* a comment */ 23, \"hello\"},\n" 17688 " {-1, 93463, \"world\"},\n" 17689 " { 7, 5, \"!!\"}\n" 17690 "};\n", 17691 Style); 17692 17693 Style.ColumnLimit = 20; 17694 EXPECT_EQ( 17695 "demo = std::array<\n" 17696 " struct test, 3>{\n" 17697 " test{\n" 17698 " 56, 23,\n" 17699 " \"hello \"\n" 17700 " \"world i \"\n" 17701 " \"am a very \"\n" 17702 " \"long line \"\n" 17703 " \"that \"\n" 17704 " \"really, \"\n" 17705 " \"in any \"\n" 17706 " \"just \"\n" 17707 " \"world, \"\n" 17708 " \"ought to \"\n" 17709 " \"be split \"\n" 17710 " \"over \"\n" 17711 " \"multiple \"\n" 17712 " \"lines\"},\n" 17713 " test{-1, 93463,\n" 17714 " \"world\"},\n" 17715 " test{ 7, 5,\n" 17716 " \"!!\" },\n" 17717 "};", 17718 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world " 17719 "i am a very long line that really, in any just world, ought " 17720 "to be split over multiple lines\"},test{-1, 93463, \"world\"}," 17721 "test{7, 5, \"!!\"},};", 17722 Style)); 17723 // This caused a core dump by enabling Alignment in the LLVMStyle globally 17724 Style = getLLVMStyleWithColumns(50); 17725 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 17726 verifyFormat("static A x = {\n" 17727 " {{init1, init2, init3, init4},\n" 17728 " {init1, init2, init3, init4}}\n" 17729 "};", 17730 Style); 17731 Style.ColumnLimit = 100; 17732 EXPECT_EQ( 17733 "test demo[] = {\n" 17734 " {56, 23,\n" 17735 " \"hello world i am a very long line that really, in any just world" 17736 ", ought to be split over \"\n" 17737 " \"multiple lines\" },\n" 17738 " {-1, 93463, \"world\"},\n" 17739 " { 7, 5, \"!!\"},\n" 17740 "};", 17741 format("test demo[] = {{56, 23, \"hello world i am a very long line " 17742 "that really, in any just world, ought to be split over multiple " 17743 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 17744 Style)); 17745 17746 Style = getLLVMStyleWithColumns(50); 17747 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 17748 Style.AlignConsecutiveAssignments = 17749 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 17750 Style.AlignConsecutiveDeclarations = 17751 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 17752 verifyFormat("struct test demo[] = {\n" 17753 " {56, 23, \"hello\"},\n" 17754 " {-1, 93463, \"world\"},\n" 17755 " { 7, 5, \"!!\"}\n" 17756 "};\n" 17757 "static A x = {\n" 17758 " {{init1, init2, init3, init4},\n" 17759 " {init1, init2, init3, init4}}\n" 17760 "};", 17761 Style); 17762 Style.ColumnLimit = 100; 17763 Style.AlignConsecutiveAssignments = 17764 FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments; 17765 Style.AlignConsecutiveDeclarations = 17766 FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments; 17767 verifyFormat("struct test demo[] = {\n" 17768 " {56, 23, \"hello\"},\n" 17769 " {-1, 93463, \"world\"},\n" 17770 " { 7, 5, \"!!\"}\n" 17771 "};\n" 17772 "struct test demo[4] = {\n" 17773 " { 56, 23, 21, \"oh\"}, // first line\n" 17774 " { -1, 93463, 22, \"my\"}, // second line\n" 17775 " { 7, 5, 1, \"goodness\"} // third line\n" 17776 " {234, 5, 1, \"gracious\"} // fourth line\n" 17777 "};\n", 17778 Style); 17779 EXPECT_EQ( 17780 "test demo[] = {\n" 17781 " {56,\n" 17782 " \"hello world i am a very long line that really, in any just world" 17783 ", ought to be split over \"\n" 17784 " \"multiple lines\", 23},\n" 17785 " {-1, \"world\", 93463},\n" 17786 " { 7, \"!!\", 5},\n" 17787 "};", 17788 format("test demo[] = {{56, \"hello world i am a very long line " 17789 "that really, in any just world, ought to be split over multiple " 17790 "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};", 17791 Style)); 17792 } 17793 17794 TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) { 17795 auto Style = getLLVMStyle(); 17796 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; 17797 verifyFormat("struct test demo[] = {\n" 17798 " {56, 23, \"hello\"},\n" 17799 " {-1, 93463, \"world\"},\n" 17800 " {7, 5, \"!!\" }\n" 17801 "};\n", 17802 Style); 17803 17804 verifyFormat("struct test demo[] = {\n" 17805 " {56, 23, \"hello\"}, // first line\n" 17806 " {-1, 93463, \"world\"}, // second line\n" 17807 " {7, 5, \"!!\" } // third line\n" 17808 "};\n", 17809 Style); 17810 verifyFormat("struct test demo[4] = {\n" 17811 " {56, 23, 21, \"oh\" }, // first line\n" 17812 " {-1, 93463, 22, \"my\" }, // second line\n" 17813 " {7, 5, 1, \"goodness\"} // third line\n" 17814 " {234, 5, 1, \"gracious\"} // fourth line\n" 17815 "};\n", 17816 Style); 17817 verifyFormat("struct test demo[3] = {\n" 17818 " {56, 23, \"hello\"},\n" 17819 " {-1, 93463, \"world\"},\n" 17820 " {7, 5, \"!!\" }\n" 17821 "};\n", 17822 Style); 17823 17824 verifyFormat("struct test demo[3] = {\n" 17825 " {int{56}, 23, \"hello\"},\n" 17826 " {int{-1}, 93463, \"world\"},\n" 17827 " {int{7}, 5, \"!!\" }\n" 17828 "};\n", 17829 Style); 17830 verifyFormat("struct test demo[] = {\n" 17831 " {56, 23, \"hello\"},\n" 17832 " {-1, 93463, \"world\"},\n" 17833 " {7, 5, \"!!\" },\n" 17834 "};\n", 17835 Style); 17836 verifyFormat("test demo[] = {\n" 17837 " {56, 23, \"hello\"},\n" 17838 " {-1, 93463, \"world\"},\n" 17839 " {7, 5, \"!!\" },\n" 17840 "};\n", 17841 Style); 17842 verifyFormat("demo = std::array<struct test, 3>{\n" 17843 " test{56, 23, \"hello\"},\n" 17844 " test{-1, 93463, \"world\"},\n" 17845 " test{7, 5, \"!!\" },\n" 17846 "};\n", 17847 Style); 17848 verifyFormat("test demo[] = {\n" 17849 " {56, 23, \"hello\"},\n" 17850 "#if X\n" 17851 " {-1, 93463, \"world\"},\n" 17852 "#endif\n" 17853 " {7, 5, \"!!\" }\n" 17854 "};\n", 17855 Style); 17856 verifyFormat( 17857 "test demo[] = {\n" 17858 " {7, 23,\n" 17859 " \"hello world i am a very long line that really, in any\"\n" 17860 " \"just world, ought to be split over multiple lines\"},\n" 17861 " {-1, 93463, \"world\" },\n" 17862 " {56, 5, \"!!\" }\n" 17863 "};\n", 17864 Style); 17865 17866 verifyFormat("return GradForUnaryCwise(g, {\n" 17867 " {{\"sign\"}, \"Sign\", {\"x\", " 17868 "\"dy\"} },\n" 17869 " {{\"dx\"}, \"Mul\", " 17870 "{\"dy\", \"sign\"}},\n" 17871 "});\n", 17872 Style); 17873 17874 Style.ColumnLimit = 0; 17875 EXPECT_EQ( 17876 "test demo[] = {\n" 17877 " {56, 23, \"hello world i am a very long line that really, in any " 17878 "just world, ought to be split over multiple lines\"},\n" 17879 " {-1, 93463, \"world\" " 17880 " },\n" 17881 " {7, 5, \"!!\" " 17882 " },\n" 17883 "};", 17884 format("test demo[] = {{56, 23, \"hello world i am a very long line " 17885 "that really, in any just world, ought to be split over multiple " 17886 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 17887 Style)); 17888 17889 Style.ColumnLimit = 80; 17890 verifyFormat("test demo[] = {\n" 17891 " {56, 23, /* a comment */ \"hello\"},\n" 17892 " {-1, 93463, \"world\" },\n" 17893 " {7, 5, \"!!\" }\n" 17894 "};\n", 17895 Style); 17896 17897 verifyFormat("test demo[] = {\n" 17898 " {56, 23, \"hello\" },\n" 17899 " {-1, 93463, \"world\" /* comment here */},\n" 17900 " {7, 5, \"!!\" }\n" 17901 "};\n", 17902 Style); 17903 17904 verifyFormat("test demo[] = {\n" 17905 " {56, /* a comment */ 23, \"hello\"},\n" 17906 " {-1, 93463, \"world\"},\n" 17907 " {7, 5, \"!!\" }\n" 17908 "};\n", 17909 Style); 17910 17911 Style.ColumnLimit = 20; 17912 EXPECT_EQ( 17913 "demo = std::array<\n" 17914 " struct test, 3>{\n" 17915 " test{\n" 17916 " 56, 23,\n" 17917 " \"hello \"\n" 17918 " \"world i \"\n" 17919 " \"am a very \"\n" 17920 " \"long line \"\n" 17921 " \"that \"\n" 17922 " \"really, \"\n" 17923 " \"in any \"\n" 17924 " \"just \"\n" 17925 " \"world, \"\n" 17926 " \"ought to \"\n" 17927 " \"be split \"\n" 17928 " \"over \"\n" 17929 " \"multiple \"\n" 17930 " \"lines\"},\n" 17931 " test{-1, 93463,\n" 17932 " \"world\"},\n" 17933 " test{7, 5,\n" 17934 " \"!!\" },\n" 17935 "};", 17936 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world " 17937 "i am a very long line that really, in any just world, ought " 17938 "to be split over multiple lines\"},test{-1, 93463, \"world\"}," 17939 "test{7, 5, \"!!\"},};", 17940 Style)); 17941 17942 // This caused a core dump by enabling Alignment in the LLVMStyle globally 17943 Style = getLLVMStyleWithColumns(50); 17944 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; 17945 verifyFormat("static A x = {\n" 17946 " {{init1, init2, init3, init4},\n" 17947 " {init1, init2, init3, init4}}\n" 17948 "};", 17949 Style); 17950 Style.ColumnLimit = 100; 17951 EXPECT_EQ( 17952 "test demo[] = {\n" 17953 " {56, 23,\n" 17954 " \"hello world i am a very long line that really, in any just world" 17955 ", ought to be split over \"\n" 17956 " \"multiple lines\" },\n" 17957 " {-1, 93463, \"world\"},\n" 17958 " {7, 5, \"!!\" },\n" 17959 "};", 17960 format("test demo[] = {{56, 23, \"hello world i am a very long line " 17961 "that really, in any just world, ought to be split over multiple " 17962 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 17963 Style)); 17964 } 17965 17966 TEST_F(FormatTest, UnderstandsPragmas) { 17967 verifyFormat("#pragma omp reduction(| : var)"); 17968 verifyFormat("#pragma omp reduction(+ : var)"); 17969 17970 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 17971 "(including parentheses).", 17972 format("#pragma mark Any non-hyphenated or hyphenated string " 17973 "(including parentheses).")); 17974 } 17975 17976 TEST_F(FormatTest, UnderstandPragmaOption) { 17977 verifyFormat("#pragma option -C -A"); 17978 17979 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 17980 } 17981 17982 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) { 17983 FormatStyle Style = getLLVMStyle(); 17984 Style.ColumnLimit = 20; 17985 17986 // See PR41213 17987 EXPECT_EQ("/*\n" 17988 " *\t9012345\n" 17989 " * /8901\n" 17990 " */", 17991 format("/*\n" 17992 " *\t9012345 /8901\n" 17993 " */", 17994 Style)); 17995 EXPECT_EQ("/*\n" 17996 " *345678\n" 17997 " *\t/8901\n" 17998 " */", 17999 format("/*\n" 18000 " *345678\t/8901\n" 18001 " */", 18002 Style)); 18003 18004 verifyFormat("int a; // the\n" 18005 " // comment", 18006 Style); 18007 EXPECT_EQ("int a; /* first line\n" 18008 " * second\n" 18009 " * line third\n" 18010 " * line\n" 18011 " */", 18012 format("int a; /* first line\n" 18013 " * second\n" 18014 " * line third\n" 18015 " * line\n" 18016 " */", 18017 Style)); 18018 EXPECT_EQ("int a; // first line\n" 18019 " // second\n" 18020 " // line third\n" 18021 " // line", 18022 format("int a; // first line\n" 18023 " // second line\n" 18024 " // third line", 18025 Style)); 18026 18027 Style.PenaltyExcessCharacter = 90; 18028 verifyFormat("int a; // the comment", Style); 18029 EXPECT_EQ("int a; // the comment\n" 18030 " // aaa", 18031 format("int a; // the comment aaa", Style)); 18032 EXPECT_EQ("int a; /* first line\n" 18033 " * second line\n" 18034 " * third line\n" 18035 " */", 18036 format("int a; /* first line\n" 18037 " * second line\n" 18038 " * third line\n" 18039 " */", 18040 Style)); 18041 EXPECT_EQ("int a; // first line\n" 18042 " // second line\n" 18043 " // third line", 18044 format("int a; // first line\n" 18045 " // second line\n" 18046 " // third line", 18047 Style)); 18048 // FIXME: Investigate why this is not getting the same layout as the test 18049 // above. 18050 EXPECT_EQ("int a; /* first line\n" 18051 " * second line\n" 18052 " * third line\n" 18053 " */", 18054 format("int a; /* first line second line third line" 18055 "\n*/", 18056 Style)); 18057 18058 EXPECT_EQ("// foo bar baz bazfoo\n" 18059 "// foo bar foo bar\n", 18060 format("// foo bar baz bazfoo\n" 18061 "// foo bar foo bar\n", 18062 Style)); 18063 EXPECT_EQ("// foo bar baz bazfoo\n" 18064 "// foo bar foo bar\n", 18065 format("// foo bar baz bazfoo\n" 18066 "// foo bar foo bar\n", 18067 Style)); 18068 18069 // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the 18070 // next one. 18071 EXPECT_EQ("// foo bar baz bazfoo\n" 18072 "// bar foo bar\n", 18073 format("// foo bar baz bazfoo bar\n" 18074 "// foo bar\n", 18075 Style)); 18076 18077 EXPECT_EQ("// foo bar baz bazfoo\n" 18078 "// foo bar baz bazfoo\n" 18079 "// bar foo bar\n", 18080 format("// foo bar baz bazfoo\n" 18081 "// foo bar baz bazfoo bar\n" 18082 "// foo bar\n", 18083 Style)); 18084 18085 EXPECT_EQ("// foo bar baz bazfoo\n" 18086 "// foo bar baz bazfoo\n" 18087 "// bar foo bar\n", 18088 format("// foo bar baz bazfoo\n" 18089 "// foo bar baz bazfoo bar\n" 18090 "// foo bar\n", 18091 Style)); 18092 18093 // Make sure we do not keep protruding characters if strict mode reflow is 18094 // cheaper than keeping protruding characters. 18095 Style.ColumnLimit = 21; 18096 EXPECT_EQ( 18097 "// foo foo foo foo\n" 18098 "// foo foo foo foo\n" 18099 "// foo foo foo foo\n", 18100 format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", Style)); 18101 18102 EXPECT_EQ("int a = /* long block\n" 18103 " comment */\n" 18104 " 42;", 18105 format("int a = /* long block comment */ 42;", Style)); 18106 } 18107 18108 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 18109 for (size_t i = 1; i < Styles.size(); ++i) \ 18110 EXPECT_EQ(Styles[0], Styles[i]) \ 18111 << "Style #" << i << " of " << Styles.size() << " differs from Style #0" 18112 18113 TEST_F(FormatTest, GetsPredefinedStyleByName) { 18114 SmallVector<FormatStyle, 3> Styles; 18115 Styles.resize(3); 18116 18117 Styles[0] = getLLVMStyle(); 18118 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 18119 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 18120 EXPECT_ALL_STYLES_EQUAL(Styles); 18121 18122 Styles[0] = getGoogleStyle(); 18123 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 18124 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 18125 EXPECT_ALL_STYLES_EQUAL(Styles); 18126 18127 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 18128 EXPECT_TRUE( 18129 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 18130 EXPECT_TRUE( 18131 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 18132 EXPECT_ALL_STYLES_EQUAL(Styles); 18133 18134 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 18135 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 18136 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 18137 EXPECT_ALL_STYLES_EQUAL(Styles); 18138 18139 Styles[0] = getMozillaStyle(); 18140 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 18141 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 18142 EXPECT_ALL_STYLES_EQUAL(Styles); 18143 18144 Styles[0] = getWebKitStyle(); 18145 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 18146 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 18147 EXPECT_ALL_STYLES_EQUAL(Styles); 18148 18149 Styles[0] = getGNUStyle(); 18150 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 18151 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 18152 EXPECT_ALL_STYLES_EQUAL(Styles); 18153 18154 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 18155 } 18156 18157 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 18158 SmallVector<FormatStyle, 8> Styles; 18159 Styles.resize(2); 18160 18161 Styles[0] = getGoogleStyle(); 18162 Styles[1] = getLLVMStyle(); 18163 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 18164 EXPECT_ALL_STYLES_EQUAL(Styles); 18165 18166 Styles.resize(5); 18167 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 18168 Styles[1] = getLLVMStyle(); 18169 Styles[1].Language = FormatStyle::LK_JavaScript; 18170 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 18171 18172 Styles[2] = getLLVMStyle(); 18173 Styles[2].Language = FormatStyle::LK_JavaScript; 18174 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 18175 "BasedOnStyle: Google", 18176 &Styles[2]) 18177 .value()); 18178 18179 Styles[3] = getLLVMStyle(); 18180 Styles[3].Language = FormatStyle::LK_JavaScript; 18181 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 18182 "Language: JavaScript", 18183 &Styles[3]) 18184 .value()); 18185 18186 Styles[4] = getLLVMStyle(); 18187 Styles[4].Language = FormatStyle::LK_JavaScript; 18188 EXPECT_EQ(0, parseConfiguration("---\n" 18189 "BasedOnStyle: LLVM\n" 18190 "IndentWidth: 123\n" 18191 "---\n" 18192 "BasedOnStyle: Google\n" 18193 "Language: JavaScript", 18194 &Styles[4]) 18195 .value()); 18196 EXPECT_ALL_STYLES_EQUAL(Styles); 18197 } 18198 18199 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 18200 Style.FIELD = false; \ 18201 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 18202 EXPECT_TRUE(Style.FIELD); \ 18203 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 18204 EXPECT_FALSE(Style.FIELD); 18205 18206 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 18207 18208 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 18209 Style.STRUCT.FIELD = false; \ 18210 EXPECT_EQ(0, \ 18211 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 18212 .value()); \ 18213 EXPECT_TRUE(Style.STRUCT.FIELD); \ 18214 EXPECT_EQ(0, \ 18215 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 18216 .value()); \ 18217 EXPECT_FALSE(Style.STRUCT.FIELD); 18218 18219 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 18220 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 18221 18222 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 18223 EXPECT_NE(VALUE, Style.FIELD) << "Initial value already the same!"; \ 18224 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 18225 EXPECT_EQ(VALUE, Style.FIELD) << "Unexpected value after parsing!" 18226 18227 TEST_F(FormatTest, ParsesConfigurationBools) { 18228 FormatStyle Style = {}; 18229 Style.Language = FormatStyle::LK_Cpp; 18230 CHECK_PARSE_BOOL(AlignTrailingComments); 18231 CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine); 18232 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 18233 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 18234 CHECK_PARSE_BOOL(AllowShortEnumsOnASingleLine); 18235 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 18236 CHECK_PARSE_BOOL(BinPackArguments); 18237 CHECK_PARSE_BOOL(BinPackParameters); 18238 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 18239 CHECK_PARSE_BOOL(BreakBeforeConceptDeclarations); 18240 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 18241 CHECK_PARSE_BOOL(BreakStringLiterals); 18242 CHECK_PARSE_BOOL(CompactNamespaces); 18243 CHECK_PARSE_BOOL(DeriveLineEnding); 18244 CHECK_PARSE_BOOL(DerivePointerAlignment); 18245 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 18246 CHECK_PARSE_BOOL(DisableFormat); 18247 CHECK_PARSE_BOOL(IndentAccessModifiers); 18248 CHECK_PARSE_BOOL(IndentCaseLabels); 18249 CHECK_PARSE_BOOL(IndentCaseBlocks); 18250 CHECK_PARSE_BOOL(IndentGotoLabels); 18251 CHECK_PARSE_BOOL(IndentRequires); 18252 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 18253 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 18254 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 18255 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 18256 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 18257 CHECK_PARSE_BOOL(ReflowComments); 18258 CHECK_PARSE_BOOL(SortUsingDeclarations); 18259 CHECK_PARSE_BOOL(SpacesInParentheses); 18260 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 18261 CHECK_PARSE_BOOL(SpacesInConditionalStatement); 18262 CHECK_PARSE_BOOL(SpaceInEmptyBlock); 18263 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 18264 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 18265 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 18266 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 18267 CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); 18268 CHECK_PARSE_BOOL(SpaceAfterLogicalNot); 18269 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 18270 CHECK_PARSE_BOOL(SpaceBeforeCaseColon); 18271 CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList); 18272 CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon); 18273 CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon); 18274 CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon); 18275 CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets); 18276 CHECK_PARSE_BOOL(UseCRLF); 18277 18278 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel); 18279 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 18280 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 18281 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 18282 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 18283 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 18284 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 18285 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 18286 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock); 18287 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 18288 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 18289 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeLambdaBody); 18290 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeWhile); 18291 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 18292 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction); 18293 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord); 18294 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace); 18295 } 18296 18297 #undef CHECK_PARSE_BOOL 18298 18299 TEST_F(FormatTest, ParsesConfiguration) { 18300 FormatStyle Style = {}; 18301 Style.Language = FormatStyle::LK_Cpp; 18302 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 18303 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 18304 ConstructorInitializerIndentWidth, 1234u); 18305 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 18306 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 18307 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 18308 CHECK_PARSE("PenaltyBreakAssignment: 1234", PenaltyBreakAssignment, 1234u); 18309 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 18310 PenaltyBreakBeforeFirstCallParameter, 1234u); 18311 CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234", 18312 PenaltyBreakTemplateDeclaration, 1234u); 18313 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 18314 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 18315 PenaltyReturnTypeOnItsOwnLine, 1234u); 18316 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 18317 SpacesBeforeTrailingComments, 1234u); 18318 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 18319 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 18320 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$"); 18321 18322 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 18323 CHECK_PARSE("AlignConsecutiveAssignments: None", AlignConsecutiveAssignments, 18324 FormatStyle::ACS_None); 18325 CHECK_PARSE("AlignConsecutiveAssignments: Consecutive", 18326 AlignConsecutiveAssignments, FormatStyle::ACS_Consecutive); 18327 CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLines", 18328 AlignConsecutiveAssignments, FormatStyle::ACS_AcrossEmptyLines); 18329 CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLinesAndComments", 18330 AlignConsecutiveAssignments, 18331 FormatStyle::ACS_AcrossEmptyLinesAndComments); 18332 // For backwards compability, false / true should still parse 18333 CHECK_PARSE("AlignConsecutiveAssignments: false", AlignConsecutiveAssignments, 18334 FormatStyle::ACS_None); 18335 CHECK_PARSE("AlignConsecutiveAssignments: true", AlignConsecutiveAssignments, 18336 FormatStyle::ACS_Consecutive); 18337 18338 Style.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive; 18339 CHECK_PARSE("AlignConsecutiveBitFields: None", AlignConsecutiveBitFields, 18340 FormatStyle::ACS_None); 18341 CHECK_PARSE("AlignConsecutiveBitFields: Consecutive", 18342 AlignConsecutiveBitFields, FormatStyle::ACS_Consecutive); 18343 CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLines", 18344 AlignConsecutiveBitFields, FormatStyle::ACS_AcrossEmptyLines); 18345 CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLinesAndComments", 18346 AlignConsecutiveBitFields, 18347 FormatStyle::ACS_AcrossEmptyLinesAndComments); 18348 // For backwards compability, false / true should still parse 18349 CHECK_PARSE("AlignConsecutiveBitFields: false", AlignConsecutiveBitFields, 18350 FormatStyle::ACS_None); 18351 CHECK_PARSE("AlignConsecutiveBitFields: true", AlignConsecutiveBitFields, 18352 FormatStyle::ACS_Consecutive); 18353 18354 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 18355 CHECK_PARSE("AlignConsecutiveMacros: None", AlignConsecutiveMacros, 18356 FormatStyle::ACS_None); 18357 CHECK_PARSE("AlignConsecutiveMacros: Consecutive", AlignConsecutiveMacros, 18358 FormatStyle::ACS_Consecutive); 18359 CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLines", 18360 AlignConsecutiveMacros, FormatStyle::ACS_AcrossEmptyLines); 18361 CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLinesAndComments", 18362 AlignConsecutiveMacros, 18363 FormatStyle::ACS_AcrossEmptyLinesAndComments); 18364 // For backwards compability, false / true should still parse 18365 CHECK_PARSE("AlignConsecutiveMacros: false", AlignConsecutiveMacros, 18366 FormatStyle::ACS_None); 18367 CHECK_PARSE("AlignConsecutiveMacros: true", AlignConsecutiveMacros, 18368 FormatStyle::ACS_Consecutive); 18369 18370 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 18371 CHECK_PARSE("AlignConsecutiveDeclarations: None", 18372 AlignConsecutiveDeclarations, FormatStyle::ACS_None); 18373 CHECK_PARSE("AlignConsecutiveDeclarations: Consecutive", 18374 AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive); 18375 CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLines", 18376 AlignConsecutiveDeclarations, FormatStyle::ACS_AcrossEmptyLines); 18377 CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLinesAndComments", 18378 AlignConsecutiveDeclarations, 18379 FormatStyle::ACS_AcrossEmptyLinesAndComments); 18380 // For backwards compability, false / true should still parse 18381 CHECK_PARSE("AlignConsecutiveDeclarations: false", 18382 AlignConsecutiveDeclarations, FormatStyle::ACS_None); 18383 CHECK_PARSE("AlignConsecutiveDeclarations: true", 18384 AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive); 18385 18386 Style.PointerAlignment = FormatStyle::PAS_Middle; 18387 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 18388 FormatStyle::PAS_Left); 18389 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 18390 FormatStyle::PAS_Right); 18391 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 18392 FormatStyle::PAS_Middle); 18393 Style.ReferenceAlignment = FormatStyle::RAS_Middle; 18394 CHECK_PARSE("ReferenceAlignment: Pointer", ReferenceAlignment, 18395 FormatStyle::RAS_Pointer); 18396 CHECK_PARSE("ReferenceAlignment: Left", ReferenceAlignment, 18397 FormatStyle::RAS_Left); 18398 CHECK_PARSE("ReferenceAlignment: Right", ReferenceAlignment, 18399 FormatStyle::RAS_Right); 18400 CHECK_PARSE("ReferenceAlignment: Middle", ReferenceAlignment, 18401 FormatStyle::RAS_Middle); 18402 // For backward compatibility: 18403 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 18404 FormatStyle::PAS_Left); 18405 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 18406 FormatStyle::PAS_Right); 18407 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 18408 FormatStyle::PAS_Middle); 18409 18410 Style.Standard = FormatStyle::LS_Auto; 18411 CHECK_PARSE("Standard: c++03", Standard, FormatStyle::LS_Cpp03); 18412 CHECK_PARSE("Standard: c++11", Standard, FormatStyle::LS_Cpp11); 18413 CHECK_PARSE("Standard: c++14", Standard, FormatStyle::LS_Cpp14); 18414 CHECK_PARSE("Standard: c++17", Standard, FormatStyle::LS_Cpp17); 18415 CHECK_PARSE("Standard: c++20", Standard, FormatStyle::LS_Cpp20); 18416 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 18417 CHECK_PARSE("Standard: Latest", Standard, FormatStyle::LS_Latest); 18418 // Legacy aliases: 18419 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 18420 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Latest); 18421 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 18422 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 18423 18424 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 18425 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 18426 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 18427 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 18428 FormatStyle::BOS_None); 18429 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 18430 FormatStyle::BOS_All); 18431 // For backward compatibility: 18432 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 18433 FormatStyle::BOS_None); 18434 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 18435 FormatStyle::BOS_All); 18436 18437 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 18438 CHECK_PARSE("BreakConstructorInitializers: BeforeComma", 18439 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 18440 CHECK_PARSE("BreakConstructorInitializers: AfterColon", 18441 BreakConstructorInitializers, FormatStyle::BCIS_AfterColon); 18442 CHECK_PARSE("BreakConstructorInitializers: BeforeColon", 18443 BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon); 18444 // For backward compatibility: 18445 CHECK_PARSE("BreakConstructorInitializersBeforeComma: true", 18446 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 18447 18448 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 18449 CHECK_PARSE("BreakInheritanceList: AfterComma", BreakInheritanceList, 18450 FormatStyle::BILS_AfterComma); 18451 CHECK_PARSE("BreakInheritanceList: BeforeComma", BreakInheritanceList, 18452 FormatStyle::BILS_BeforeComma); 18453 CHECK_PARSE("BreakInheritanceList: AfterColon", BreakInheritanceList, 18454 FormatStyle::BILS_AfterColon); 18455 CHECK_PARSE("BreakInheritanceList: BeforeColon", BreakInheritanceList, 18456 FormatStyle::BILS_BeforeColon); 18457 // For backward compatibility: 18458 CHECK_PARSE("BreakBeforeInheritanceComma: true", BreakInheritanceList, 18459 FormatStyle::BILS_BeforeComma); 18460 18461 Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack; 18462 CHECK_PARSE("PackConstructorInitializers: Never", PackConstructorInitializers, 18463 FormatStyle::PCIS_Never); 18464 CHECK_PARSE("PackConstructorInitializers: BinPack", 18465 PackConstructorInitializers, FormatStyle::PCIS_BinPack); 18466 CHECK_PARSE("PackConstructorInitializers: CurrentLine", 18467 PackConstructorInitializers, FormatStyle::PCIS_CurrentLine); 18468 CHECK_PARSE("PackConstructorInitializers: NextLine", 18469 PackConstructorInitializers, FormatStyle::PCIS_NextLine); 18470 // For backward compatibility: 18471 CHECK_PARSE("BasedOnStyle: Google\n" 18472 "ConstructorInitializerAllOnOneLineOrOnePerLine: true\n" 18473 "AllowAllConstructorInitializersOnNextLine: false", 18474 PackConstructorInitializers, FormatStyle::PCIS_CurrentLine); 18475 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 18476 CHECK_PARSE("BasedOnStyle: Google\n" 18477 "ConstructorInitializerAllOnOneLineOrOnePerLine: false", 18478 PackConstructorInitializers, FormatStyle::PCIS_BinPack); 18479 CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n" 18480 "AllowAllConstructorInitializersOnNextLine: true", 18481 PackConstructorInitializers, FormatStyle::PCIS_NextLine); 18482 Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack; 18483 CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n" 18484 "AllowAllConstructorInitializersOnNextLine: false", 18485 PackConstructorInitializers, FormatStyle::PCIS_CurrentLine); 18486 18487 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 18488 CHECK_PARSE("EmptyLineBeforeAccessModifier: Never", 18489 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Never); 18490 CHECK_PARSE("EmptyLineBeforeAccessModifier: Leave", 18491 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Leave); 18492 CHECK_PARSE("EmptyLineBeforeAccessModifier: LogicalBlock", 18493 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_LogicalBlock); 18494 CHECK_PARSE("EmptyLineBeforeAccessModifier: Always", 18495 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Always); 18496 18497 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 18498 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 18499 FormatStyle::BAS_Align); 18500 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 18501 FormatStyle::BAS_DontAlign); 18502 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 18503 FormatStyle::BAS_AlwaysBreak); 18504 // For backward compatibility: 18505 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 18506 FormatStyle::BAS_DontAlign); 18507 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 18508 FormatStyle::BAS_Align); 18509 18510 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left; 18511 CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines, 18512 FormatStyle::ENAS_DontAlign); 18513 CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines, 18514 FormatStyle::ENAS_Left); 18515 CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines, 18516 FormatStyle::ENAS_Right); 18517 // For backward compatibility: 18518 CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines, 18519 FormatStyle::ENAS_Left); 18520 CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines, 18521 FormatStyle::ENAS_Right); 18522 18523 Style.AlignOperands = FormatStyle::OAS_Align; 18524 CHECK_PARSE("AlignOperands: DontAlign", AlignOperands, 18525 FormatStyle::OAS_DontAlign); 18526 CHECK_PARSE("AlignOperands: Align", AlignOperands, FormatStyle::OAS_Align); 18527 CHECK_PARSE("AlignOperands: AlignAfterOperator", AlignOperands, 18528 FormatStyle::OAS_AlignAfterOperator); 18529 // For backward compatibility: 18530 CHECK_PARSE("AlignOperands: false", AlignOperands, 18531 FormatStyle::OAS_DontAlign); 18532 CHECK_PARSE("AlignOperands: true", AlignOperands, FormatStyle::OAS_Align); 18533 18534 Style.UseTab = FormatStyle::UT_ForIndentation; 18535 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 18536 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 18537 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 18538 CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab, 18539 FormatStyle::UT_ForContinuationAndIndentation); 18540 CHECK_PARSE("UseTab: AlignWithSpaces", UseTab, 18541 FormatStyle::UT_AlignWithSpaces); 18542 // For backward compatibility: 18543 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 18544 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 18545 18546 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 18547 CHECK_PARSE("AllowShortBlocksOnASingleLine: Never", 18548 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 18549 CHECK_PARSE("AllowShortBlocksOnASingleLine: Empty", 18550 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Empty); 18551 CHECK_PARSE("AllowShortBlocksOnASingleLine: Always", 18552 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always); 18553 // For backward compatibility: 18554 CHECK_PARSE("AllowShortBlocksOnASingleLine: false", 18555 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 18556 CHECK_PARSE("AllowShortBlocksOnASingleLine: true", 18557 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always); 18558 18559 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 18560 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 18561 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 18562 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 18563 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 18564 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 18565 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 18566 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 18567 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 18568 // For backward compatibility: 18569 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 18570 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 18571 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 18572 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 18573 18574 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Both; 18575 CHECK_PARSE("SpaceAroundPointerQualifiers: Default", 18576 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Default); 18577 CHECK_PARSE("SpaceAroundPointerQualifiers: Before", 18578 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Before); 18579 CHECK_PARSE("SpaceAroundPointerQualifiers: After", 18580 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_After); 18581 CHECK_PARSE("SpaceAroundPointerQualifiers: Both", 18582 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Both); 18583 18584 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 18585 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 18586 FormatStyle::SBPO_Never); 18587 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 18588 FormatStyle::SBPO_Always); 18589 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 18590 FormatStyle::SBPO_ControlStatements); 18591 CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptControlMacros", 18592 SpaceBeforeParens, 18593 FormatStyle::SBPO_ControlStatementsExceptControlMacros); 18594 CHECK_PARSE("SpaceBeforeParens: NonEmptyParentheses", SpaceBeforeParens, 18595 FormatStyle::SBPO_NonEmptyParentheses); 18596 // For backward compatibility: 18597 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 18598 FormatStyle::SBPO_Never); 18599 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 18600 FormatStyle::SBPO_ControlStatements); 18601 CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptForEachMacros", 18602 SpaceBeforeParens, 18603 FormatStyle::SBPO_ControlStatementsExceptControlMacros); 18604 18605 Style.ColumnLimit = 123; 18606 FormatStyle BaseStyle = getLLVMStyle(); 18607 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 18608 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 18609 18610 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 18611 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 18612 FormatStyle::BS_Attach); 18613 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 18614 FormatStyle::BS_Linux); 18615 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 18616 FormatStyle::BS_Mozilla); 18617 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 18618 FormatStyle::BS_Stroustrup); 18619 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 18620 FormatStyle::BS_Allman); 18621 CHECK_PARSE("BreakBeforeBraces: Whitesmiths", BreakBeforeBraces, 18622 FormatStyle::BS_Whitesmiths); 18623 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 18624 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 18625 FormatStyle::BS_WebKit); 18626 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 18627 FormatStyle::BS_Custom); 18628 18629 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never; 18630 CHECK_PARSE("BraceWrapping:\n" 18631 " AfterControlStatement: MultiLine", 18632 BraceWrapping.AfterControlStatement, 18633 FormatStyle::BWACS_MultiLine); 18634 CHECK_PARSE("BraceWrapping:\n" 18635 " AfterControlStatement: Always", 18636 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always); 18637 CHECK_PARSE("BraceWrapping:\n" 18638 " AfterControlStatement: Never", 18639 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never); 18640 // For backward compatibility: 18641 CHECK_PARSE("BraceWrapping:\n" 18642 " AfterControlStatement: true", 18643 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always); 18644 CHECK_PARSE("BraceWrapping:\n" 18645 " AfterControlStatement: false", 18646 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never); 18647 18648 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 18649 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 18650 FormatStyle::RTBS_None); 18651 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 18652 FormatStyle::RTBS_All); 18653 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 18654 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 18655 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 18656 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 18657 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 18658 AlwaysBreakAfterReturnType, 18659 FormatStyle::RTBS_TopLevelDefinitions); 18660 18661 Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 18662 CHECK_PARSE("AlwaysBreakTemplateDeclarations: No", 18663 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_No); 18664 CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine", 18665 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine); 18666 CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes", 18667 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes); 18668 CHECK_PARSE("AlwaysBreakTemplateDeclarations: false", 18669 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine); 18670 CHECK_PARSE("AlwaysBreakTemplateDeclarations: true", 18671 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes); 18672 18673 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 18674 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 18675 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 18676 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 18677 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 18678 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 18679 AlwaysBreakAfterDefinitionReturnType, 18680 FormatStyle::DRTBS_TopLevel); 18681 18682 Style.NamespaceIndentation = FormatStyle::NI_All; 18683 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 18684 FormatStyle::NI_None); 18685 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 18686 FormatStyle::NI_Inner); 18687 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 18688 FormatStyle::NI_All); 18689 18690 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_OnlyFirstIf; 18691 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Never", 18692 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never); 18693 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: WithoutElse", 18694 AllowShortIfStatementsOnASingleLine, 18695 FormatStyle::SIS_WithoutElse); 18696 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: OnlyFirstIf", 18697 AllowShortIfStatementsOnASingleLine, 18698 FormatStyle::SIS_OnlyFirstIf); 18699 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: AllIfsAndElse", 18700 AllowShortIfStatementsOnASingleLine, 18701 FormatStyle::SIS_AllIfsAndElse); 18702 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Always", 18703 AllowShortIfStatementsOnASingleLine, 18704 FormatStyle::SIS_OnlyFirstIf); 18705 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: false", 18706 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never); 18707 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: true", 18708 AllowShortIfStatementsOnASingleLine, 18709 FormatStyle::SIS_WithoutElse); 18710 18711 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; 18712 CHECK_PARSE("IndentExternBlock: AfterExternBlock", IndentExternBlock, 18713 FormatStyle::IEBS_AfterExternBlock); 18714 CHECK_PARSE("IndentExternBlock: Indent", IndentExternBlock, 18715 FormatStyle::IEBS_Indent); 18716 CHECK_PARSE("IndentExternBlock: NoIndent", IndentExternBlock, 18717 FormatStyle::IEBS_NoIndent); 18718 CHECK_PARSE("IndentExternBlock: true", IndentExternBlock, 18719 FormatStyle::IEBS_Indent); 18720 CHECK_PARSE("IndentExternBlock: false", IndentExternBlock, 18721 FormatStyle::IEBS_NoIndent); 18722 18723 Style.BitFieldColonSpacing = FormatStyle::BFCS_None; 18724 CHECK_PARSE("BitFieldColonSpacing: Both", BitFieldColonSpacing, 18725 FormatStyle::BFCS_Both); 18726 CHECK_PARSE("BitFieldColonSpacing: None", BitFieldColonSpacing, 18727 FormatStyle::BFCS_None); 18728 CHECK_PARSE("BitFieldColonSpacing: Before", BitFieldColonSpacing, 18729 FormatStyle::BFCS_Before); 18730 CHECK_PARSE("BitFieldColonSpacing: After", BitFieldColonSpacing, 18731 FormatStyle::BFCS_After); 18732 18733 Style.SortJavaStaticImport = FormatStyle::SJSIO_Before; 18734 CHECK_PARSE("SortJavaStaticImport: After", SortJavaStaticImport, 18735 FormatStyle::SJSIO_After); 18736 CHECK_PARSE("SortJavaStaticImport: Before", SortJavaStaticImport, 18737 FormatStyle::SJSIO_Before); 18738 18739 // FIXME: This is required because parsing a configuration simply overwrites 18740 // the first N elements of the list instead of resetting it. 18741 Style.ForEachMacros.clear(); 18742 std::vector<std::string> BoostForeach; 18743 BoostForeach.push_back("BOOST_FOREACH"); 18744 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 18745 std::vector<std::string> BoostAndQForeach; 18746 BoostAndQForeach.push_back("BOOST_FOREACH"); 18747 BoostAndQForeach.push_back("Q_FOREACH"); 18748 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 18749 BoostAndQForeach); 18750 18751 Style.IfMacros.clear(); 18752 std::vector<std::string> CustomIfs; 18753 CustomIfs.push_back("MYIF"); 18754 CHECK_PARSE("IfMacros: [MYIF]", IfMacros, CustomIfs); 18755 18756 Style.AttributeMacros.clear(); 18757 CHECK_PARSE("BasedOnStyle: LLVM", AttributeMacros, 18758 std::vector<std::string>{"__capability"}); 18759 CHECK_PARSE("AttributeMacros: [attr1, attr2]", AttributeMacros, 18760 std::vector<std::string>({"attr1", "attr2"})); 18761 18762 Style.StatementAttributeLikeMacros.clear(); 18763 CHECK_PARSE("StatementAttributeLikeMacros: [emit,Q_EMIT]", 18764 StatementAttributeLikeMacros, 18765 std::vector<std::string>({"emit", "Q_EMIT"})); 18766 18767 Style.StatementMacros.clear(); 18768 CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros, 18769 std::vector<std::string>{"QUNUSED"}); 18770 CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros, 18771 std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"})); 18772 18773 Style.NamespaceMacros.clear(); 18774 CHECK_PARSE("NamespaceMacros: [TESTSUITE]", NamespaceMacros, 18775 std::vector<std::string>{"TESTSUITE"}); 18776 CHECK_PARSE("NamespaceMacros: [TESTSUITE, SUITE]", NamespaceMacros, 18777 std::vector<std::string>({"TESTSUITE", "SUITE"})); 18778 18779 Style.WhitespaceSensitiveMacros.clear(); 18780 CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE]", 18781 WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"}); 18782 CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE, ASSERT]", 18783 WhitespaceSensitiveMacros, 18784 std::vector<std::string>({"STRINGIZE", "ASSERT"})); 18785 Style.WhitespaceSensitiveMacros.clear(); 18786 CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE']", 18787 WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"}); 18788 CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE', 'ASSERT']", 18789 WhitespaceSensitiveMacros, 18790 std::vector<std::string>({"STRINGIZE", "ASSERT"})); 18791 18792 Style.IncludeStyle.IncludeCategories.clear(); 18793 std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = { 18794 {"abc/.*", 2, 0, false}, {".*", 1, 0, true}}; 18795 CHECK_PARSE("IncludeCategories:\n" 18796 " - Regex: abc/.*\n" 18797 " Priority: 2\n" 18798 " - Regex: .*\n" 18799 " Priority: 1\n" 18800 " CaseSensitive: true\n", 18801 IncludeStyle.IncludeCategories, ExpectedCategories); 18802 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex, 18803 "abc$"); 18804 CHECK_PARSE("IncludeIsMainSourceRegex: 'abc$'", 18805 IncludeStyle.IncludeIsMainSourceRegex, "abc$"); 18806 18807 Style.SortIncludes = FormatStyle::SI_Never; 18808 CHECK_PARSE("SortIncludes: true", SortIncludes, 18809 FormatStyle::SI_CaseSensitive); 18810 CHECK_PARSE("SortIncludes: false", SortIncludes, FormatStyle::SI_Never); 18811 CHECK_PARSE("SortIncludes: CaseInsensitive", SortIncludes, 18812 FormatStyle::SI_CaseInsensitive); 18813 CHECK_PARSE("SortIncludes: CaseSensitive", SortIncludes, 18814 FormatStyle::SI_CaseSensitive); 18815 CHECK_PARSE("SortIncludes: Never", SortIncludes, FormatStyle::SI_Never); 18816 18817 Style.RawStringFormats.clear(); 18818 std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = { 18819 { 18820 FormatStyle::LK_TextProto, 18821 {"pb", "proto"}, 18822 {"PARSE_TEXT_PROTO"}, 18823 /*CanonicalDelimiter=*/"", 18824 "llvm", 18825 }, 18826 { 18827 FormatStyle::LK_Cpp, 18828 {"cc", "cpp"}, 18829 {"C_CODEBLOCK", "CPPEVAL"}, 18830 /*CanonicalDelimiter=*/"cc", 18831 /*BasedOnStyle=*/"", 18832 }, 18833 }; 18834 18835 CHECK_PARSE("RawStringFormats:\n" 18836 " - Language: TextProto\n" 18837 " Delimiters:\n" 18838 " - 'pb'\n" 18839 " - 'proto'\n" 18840 " EnclosingFunctions:\n" 18841 " - 'PARSE_TEXT_PROTO'\n" 18842 " BasedOnStyle: llvm\n" 18843 " - Language: Cpp\n" 18844 " Delimiters:\n" 18845 " - 'cc'\n" 18846 " - 'cpp'\n" 18847 " EnclosingFunctions:\n" 18848 " - 'C_CODEBLOCK'\n" 18849 " - 'CPPEVAL'\n" 18850 " CanonicalDelimiter: 'cc'", 18851 RawStringFormats, ExpectedRawStringFormats); 18852 18853 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 18854 " Minimum: 0\n" 18855 " Maximum: 0", 18856 SpacesInLineCommentPrefix.Minimum, 0u); 18857 EXPECT_EQ(Style.SpacesInLineCommentPrefix.Maximum, 0u); 18858 Style.SpacesInLineCommentPrefix.Minimum = 1; 18859 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 18860 " Minimum: 2", 18861 SpacesInLineCommentPrefix.Minimum, 0u); 18862 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 18863 " Maximum: -1", 18864 SpacesInLineCommentPrefix.Maximum, -1u); 18865 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 18866 " Minimum: 2", 18867 SpacesInLineCommentPrefix.Minimum, 2u); 18868 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 18869 " Maximum: 1", 18870 SpacesInLineCommentPrefix.Maximum, 1u); 18871 EXPECT_EQ(Style.SpacesInLineCommentPrefix.Minimum, 1u); 18872 18873 Style.SpacesInAngles = FormatStyle::SIAS_Always; 18874 CHECK_PARSE("SpacesInAngles: Never", SpacesInAngles, FormatStyle::SIAS_Never); 18875 CHECK_PARSE("SpacesInAngles: Always", SpacesInAngles, 18876 FormatStyle::SIAS_Always); 18877 CHECK_PARSE("SpacesInAngles: Leave", SpacesInAngles, FormatStyle::SIAS_Leave); 18878 // For backward compatibility: 18879 CHECK_PARSE("SpacesInAngles: false", SpacesInAngles, FormatStyle::SIAS_Never); 18880 CHECK_PARSE("SpacesInAngles: true", SpacesInAngles, FormatStyle::SIAS_Always); 18881 } 18882 18883 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 18884 FormatStyle Style = {}; 18885 Style.Language = FormatStyle::LK_Cpp; 18886 CHECK_PARSE("Language: Cpp\n" 18887 "IndentWidth: 12", 18888 IndentWidth, 12u); 18889 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 18890 "IndentWidth: 34", 18891 &Style), 18892 ParseError::Unsuitable); 18893 FormatStyle BinPackedTCS = {}; 18894 BinPackedTCS.Language = FormatStyle::LK_JavaScript; 18895 EXPECT_EQ(parseConfiguration("BinPackArguments: true\n" 18896 "InsertTrailingCommas: Wrapped", 18897 &BinPackedTCS), 18898 ParseError::BinPackTrailingCommaConflict); 18899 EXPECT_EQ(12u, Style.IndentWidth); 18900 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 18901 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 18902 18903 Style.Language = FormatStyle::LK_JavaScript; 18904 CHECK_PARSE("Language: JavaScript\n" 18905 "IndentWidth: 12", 18906 IndentWidth, 12u); 18907 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 18908 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 18909 "IndentWidth: 34", 18910 &Style), 18911 ParseError::Unsuitable); 18912 EXPECT_EQ(23u, Style.IndentWidth); 18913 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 18914 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 18915 18916 CHECK_PARSE("BasedOnStyle: LLVM\n" 18917 "IndentWidth: 67", 18918 IndentWidth, 67u); 18919 18920 CHECK_PARSE("---\n" 18921 "Language: JavaScript\n" 18922 "IndentWidth: 12\n" 18923 "---\n" 18924 "Language: Cpp\n" 18925 "IndentWidth: 34\n" 18926 "...\n", 18927 IndentWidth, 12u); 18928 18929 Style.Language = FormatStyle::LK_Cpp; 18930 CHECK_PARSE("---\n" 18931 "Language: JavaScript\n" 18932 "IndentWidth: 12\n" 18933 "---\n" 18934 "Language: Cpp\n" 18935 "IndentWidth: 34\n" 18936 "...\n", 18937 IndentWidth, 34u); 18938 CHECK_PARSE("---\n" 18939 "IndentWidth: 78\n" 18940 "---\n" 18941 "Language: JavaScript\n" 18942 "IndentWidth: 56\n" 18943 "...\n", 18944 IndentWidth, 78u); 18945 18946 Style.ColumnLimit = 123; 18947 Style.IndentWidth = 234; 18948 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 18949 Style.TabWidth = 345; 18950 EXPECT_FALSE(parseConfiguration("---\n" 18951 "IndentWidth: 456\n" 18952 "BreakBeforeBraces: Allman\n" 18953 "---\n" 18954 "Language: JavaScript\n" 18955 "IndentWidth: 111\n" 18956 "TabWidth: 111\n" 18957 "---\n" 18958 "Language: Cpp\n" 18959 "BreakBeforeBraces: Stroustrup\n" 18960 "TabWidth: 789\n" 18961 "...\n", 18962 &Style)); 18963 EXPECT_EQ(123u, Style.ColumnLimit); 18964 EXPECT_EQ(456u, Style.IndentWidth); 18965 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 18966 EXPECT_EQ(789u, Style.TabWidth); 18967 18968 EXPECT_EQ(parseConfiguration("---\n" 18969 "Language: JavaScript\n" 18970 "IndentWidth: 56\n" 18971 "---\n" 18972 "IndentWidth: 78\n" 18973 "...\n", 18974 &Style), 18975 ParseError::Error); 18976 EXPECT_EQ(parseConfiguration("---\n" 18977 "Language: JavaScript\n" 18978 "IndentWidth: 56\n" 18979 "---\n" 18980 "Language: JavaScript\n" 18981 "IndentWidth: 78\n" 18982 "...\n", 18983 &Style), 18984 ParseError::Error); 18985 18986 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 18987 } 18988 18989 #undef CHECK_PARSE 18990 18991 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 18992 FormatStyle Style = {}; 18993 Style.Language = FormatStyle::LK_JavaScript; 18994 Style.BreakBeforeTernaryOperators = true; 18995 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 18996 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 18997 18998 Style.BreakBeforeTernaryOperators = true; 18999 EXPECT_EQ(0, parseConfiguration("---\n" 19000 "BasedOnStyle: Google\n" 19001 "---\n" 19002 "Language: JavaScript\n" 19003 "IndentWidth: 76\n" 19004 "...\n", 19005 &Style) 19006 .value()); 19007 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 19008 EXPECT_EQ(76u, Style.IndentWidth); 19009 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 19010 } 19011 19012 TEST_F(FormatTest, ConfigurationRoundTripTest) { 19013 FormatStyle Style = getLLVMStyle(); 19014 std::string YAML = configurationAsText(Style); 19015 FormatStyle ParsedStyle = {}; 19016 ParsedStyle.Language = FormatStyle::LK_Cpp; 19017 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 19018 EXPECT_EQ(Style, ParsedStyle); 19019 } 19020 19021 TEST_F(FormatTest, WorksFor8bitEncodings) { 19022 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 19023 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 19024 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 19025 "\"\xef\xee\xf0\xf3...\"", 19026 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 19027 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 19028 "\xef\xee\xf0\xf3...\"", 19029 getLLVMStyleWithColumns(12))); 19030 } 19031 19032 TEST_F(FormatTest, HandlesUTF8BOM) { 19033 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 19034 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 19035 format("\xef\xbb\xbf#include <iostream>")); 19036 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 19037 format("\xef\xbb\xbf\n#include <iostream>")); 19038 } 19039 19040 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 19041 #if !defined(_MSC_VER) 19042 19043 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 19044 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 19045 getLLVMStyleWithColumns(35)); 19046 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 19047 getLLVMStyleWithColumns(31)); 19048 verifyFormat("// Однажды в студёную зимнюю пору...", 19049 getLLVMStyleWithColumns(36)); 19050 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 19051 verifyFormat("/* Однажды в студёную зимнюю пору... */", 19052 getLLVMStyleWithColumns(39)); 19053 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 19054 getLLVMStyleWithColumns(35)); 19055 } 19056 19057 TEST_F(FormatTest, SplitsUTF8Strings) { 19058 // Non-printable characters' width is currently considered to be the length in 19059 // bytes in UTF8. The characters can be displayed in very different manner 19060 // (zero-width, single width with a substitution glyph, expanded to their code 19061 // (e.g. "<8d>"), so there's no single correct way to handle them. 19062 EXPECT_EQ("\"aaaaÄ\"\n" 19063 "\"\xc2\x8d\";", 19064 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 19065 EXPECT_EQ("\"aaaaaaaÄ\"\n" 19066 "\"\xc2\x8d\";", 19067 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 19068 EXPECT_EQ("\"Однажды, в \"\n" 19069 "\"студёную \"\n" 19070 "\"зимнюю \"\n" 19071 "\"пору,\"", 19072 format("\"Однажды, в студёную зимнюю пору,\"", 19073 getLLVMStyleWithColumns(13))); 19074 EXPECT_EQ( 19075 "\"一 二 三 \"\n" 19076 "\"四 五六 \"\n" 19077 "\"七 八 九 \"\n" 19078 "\"十\"", 19079 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 19080 EXPECT_EQ("\"一\t\"\n" 19081 "\"二 \t\"\n" 19082 "\"三 四 \"\n" 19083 "\"五\t\"\n" 19084 "\"六 \t\"\n" 19085 "\"七 \"\n" 19086 "\"八九十\tqq\"", 19087 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 19088 getLLVMStyleWithColumns(11))); 19089 19090 // UTF8 character in an escape sequence. 19091 EXPECT_EQ("\"aaaaaa\"\n" 19092 "\"\\\xC2\x8D\"", 19093 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 19094 } 19095 19096 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 19097 EXPECT_EQ("const char *sssss =\n" 19098 " \"一二三四五六七八\\\n" 19099 " 九 十\";", 19100 format("const char *sssss = \"一二三四五六七八\\\n" 19101 " 九 十\";", 19102 getLLVMStyleWithColumns(30))); 19103 } 19104 19105 TEST_F(FormatTest, SplitsUTF8LineComments) { 19106 EXPECT_EQ("// aaaaÄ\xc2\x8d", 19107 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 19108 EXPECT_EQ("// Я из лесу\n" 19109 "// вышел; был\n" 19110 "// сильный\n" 19111 "// мороз.", 19112 format("// Я из лесу вышел; был сильный мороз.", 19113 getLLVMStyleWithColumns(13))); 19114 EXPECT_EQ("// 一二三\n" 19115 "// 四五六七\n" 19116 "// 八 九\n" 19117 "// 十", 19118 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 19119 } 19120 19121 TEST_F(FormatTest, SplitsUTF8BlockComments) { 19122 EXPECT_EQ("/* Гляжу,\n" 19123 " * поднимается\n" 19124 " * медленно в\n" 19125 " * гору\n" 19126 " * Лошадка,\n" 19127 " * везущая\n" 19128 " * хворосту\n" 19129 " * воз. */", 19130 format("/* Гляжу, поднимается медленно в гору\n" 19131 " * Лошадка, везущая хворосту воз. */", 19132 getLLVMStyleWithColumns(13))); 19133 EXPECT_EQ( 19134 "/* 一二三\n" 19135 " * 四五六七\n" 19136 " * 八 九\n" 19137 " * 十 */", 19138 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 19139 EXPECT_EQ("/* \n" 19140 " * \n" 19141 " * - */", 19142 format("/* - */", getLLVMStyleWithColumns(12))); 19143 } 19144 19145 #endif // _MSC_VER 19146 19147 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 19148 FormatStyle Style = getLLVMStyle(); 19149 19150 Style.ConstructorInitializerIndentWidth = 4; 19151 verifyFormat( 19152 "SomeClass::Constructor()\n" 19153 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 19154 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 19155 Style); 19156 19157 Style.ConstructorInitializerIndentWidth = 2; 19158 verifyFormat( 19159 "SomeClass::Constructor()\n" 19160 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 19161 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 19162 Style); 19163 19164 Style.ConstructorInitializerIndentWidth = 0; 19165 verifyFormat( 19166 "SomeClass::Constructor()\n" 19167 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 19168 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 19169 Style); 19170 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 19171 verifyFormat( 19172 "SomeLongTemplateVariableName<\n" 19173 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 19174 Style); 19175 verifyFormat("bool smaller = 1 < " 19176 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 19177 " " 19178 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 19179 Style); 19180 19181 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 19182 verifyFormat("SomeClass::Constructor() :\n" 19183 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n" 19184 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}", 19185 Style); 19186 } 19187 19188 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 19189 FormatStyle Style = getLLVMStyle(); 19190 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 19191 Style.ConstructorInitializerIndentWidth = 4; 19192 verifyFormat("SomeClass::Constructor()\n" 19193 " : a(a)\n" 19194 " , b(b)\n" 19195 " , c(c) {}", 19196 Style); 19197 verifyFormat("SomeClass::Constructor()\n" 19198 " : a(a) {}", 19199 Style); 19200 19201 Style.ColumnLimit = 0; 19202 verifyFormat("SomeClass::Constructor()\n" 19203 " : a(a) {}", 19204 Style); 19205 verifyFormat("SomeClass::Constructor() noexcept\n" 19206 " : a(a) {}", 19207 Style); 19208 verifyFormat("SomeClass::Constructor()\n" 19209 " : a(a)\n" 19210 " , b(b)\n" 19211 " , c(c) {}", 19212 Style); 19213 verifyFormat("SomeClass::Constructor()\n" 19214 " : a(a) {\n" 19215 " foo();\n" 19216 " bar();\n" 19217 "}", 19218 Style); 19219 19220 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 19221 verifyFormat("SomeClass::Constructor()\n" 19222 " : a(a)\n" 19223 " , b(b)\n" 19224 " , c(c) {\n}", 19225 Style); 19226 verifyFormat("SomeClass::Constructor()\n" 19227 " : a(a) {\n}", 19228 Style); 19229 19230 Style.ColumnLimit = 80; 19231 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 19232 Style.ConstructorInitializerIndentWidth = 2; 19233 verifyFormat("SomeClass::Constructor()\n" 19234 " : a(a)\n" 19235 " , b(b)\n" 19236 " , c(c) {}", 19237 Style); 19238 19239 Style.ConstructorInitializerIndentWidth = 0; 19240 verifyFormat("SomeClass::Constructor()\n" 19241 ": a(a)\n" 19242 ", b(b)\n" 19243 ", c(c) {}", 19244 Style); 19245 19246 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 19247 Style.ConstructorInitializerIndentWidth = 4; 19248 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 19249 verifyFormat( 19250 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 19251 Style); 19252 verifyFormat( 19253 "SomeClass::Constructor()\n" 19254 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 19255 Style); 19256 Style.ConstructorInitializerIndentWidth = 4; 19257 Style.ColumnLimit = 60; 19258 verifyFormat("SomeClass::Constructor()\n" 19259 " : aaaaaaaa(aaaaaaaa)\n" 19260 " , aaaaaaaa(aaaaaaaa)\n" 19261 " , aaaaaaaa(aaaaaaaa) {}", 19262 Style); 19263 } 19264 19265 TEST_F(FormatTest, Destructors) { 19266 verifyFormat("void F(int &i) { i.~int(); }"); 19267 verifyFormat("void F(int &i) { i->~int(); }"); 19268 } 19269 19270 TEST_F(FormatTest, FormatsWithWebKitStyle) { 19271 FormatStyle Style = getWebKitStyle(); 19272 19273 // Don't indent in outer namespaces. 19274 verifyFormat("namespace outer {\n" 19275 "int i;\n" 19276 "namespace inner {\n" 19277 " int i;\n" 19278 "} // namespace inner\n" 19279 "} // namespace outer\n" 19280 "namespace other_outer {\n" 19281 "int i;\n" 19282 "}", 19283 Style); 19284 19285 // Don't indent case labels. 19286 verifyFormat("switch (variable) {\n" 19287 "case 1:\n" 19288 "case 2:\n" 19289 " doSomething();\n" 19290 " break;\n" 19291 "default:\n" 19292 " ++variable;\n" 19293 "}", 19294 Style); 19295 19296 // Wrap before binary operators. 19297 EXPECT_EQ("void f()\n" 19298 "{\n" 19299 " if (aaaaaaaaaaaaaaaa\n" 19300 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 19301 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 19302 " return;\n" 19303 "}", 19304 format("void f() {\n" 19305 "if (aaaaaaaaaaaaaaaa\n" 19306 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 19307 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 19308 "return;\n" 19309 "}", 19310 Style)); 19311 19312 // Allow functions on a single line. 19313 verifyFormat("void f() { return; }", Style); 19314 19315 // Allow empty blocks on a single line and insert a space in empty blocks. 19316 EXPECT_EQ("void f() { }", format("void f() {}", Style)); 19317 EXPECT_EQ("while (true) { }", format("while (true) {}", Style)); 19318 // However, don't merge non-empty short loops. 19319 EXPECT_EQ("while (true) {\n" 19320 " continue;\n" 19321 "}", 19322 format("while (true) { continue; }", Style)); 19323 19324 // Constructor initializers are formatted one per line with the "," on the 19325 // new line. 19326 verifyFormat("Constructor()\n" 19327 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 19328 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 19329 " aaaaaaaaaaaaaa)\n" 19330 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 19331 "{\n" 19332 "}", 19333 Style); 19334 verifyFormat("SomeClass::Constructor()\n" 19335 " : a(a)\n" 19336 "{\n" 19337 "}", 19338 Style); 19339 EXPECT_EQ("SomeClass::Constructor()\n" 19340 " : a(a)\n" 19341 "{\n" 19342 "}", 19343 format("SomeClass::Constructor():a(a){}", Style)); 19344 verifyFormat("SomeClass::Constructor()\n" 19345 " : a(a)\n" 19346 " , b(b)\n" 19347 " , c(c)\n" 19348 "{\n" 19349 "}", 19350 Style); 19351 verifyFormat("SomeClass::Constructor()\n" 19352 " : a(a)\n" 19353 "{\n" 19354 " foo();\n" 19355 " bar();\n" 19356 "}", 19357 Style); 19358 19359 // Access specifiers should be aligned left. 19360 verifyFormat("class C {\n" 19361 "public:\n" 19362 " int i;\n" 19363 "};", 19364 Style); 19365 19366 // Do not align comments. 19367 verifyFormat("int a; // Do not\n" 19368 "double b; // align comments.", 19369 Style); 19370 19371 // Do not align operands. 19372 EXPECT_EQ("ASSERT(aaaa\n" 19373 " || bbbb);", 19374 format("ASSERT ( aaaa\n||bbbb);", Style)); 19375 19376 // Accept input's line breaks. 19377 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 19378 " || bbbbbbbbbbbbbbb) {\n" 19379 " i++;\n" 19380 "}", 19381 format("if (aaaaaaaaaaaaaaa\n" 19382 "|| bbbbbbbbbbbbbbb) { i++; }", 19383 Style)); 19384 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 19385 " i++;\n" 19386 "}", 19387 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 19388 19389 // Don't automatically break all macro definitions (llvm.org/PR17842). 19390 verifyFormat("#define aNumber 10", Style); 19391 // However, generally keep the line breaks that the user authored. 19392 EXPECT_EQ("#define aNumber \\\n" 19393 " 10", 19394 format("#define aNumber \\\n" 19395 " 10", 19396 Style)); 19397 19398 // Keep empty and one-element array literals on a single line. 19399 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 19400 " copyItems:YES];", 19401 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 19402 "copyItems:YES];", 19403 Style)); 19404 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 19405 " copyItems:YES];", 19406 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 19407 " copyItems:YES];", 19408 Style)); 19409 // FIXME: This does not seem right, there should be more indentation before 19410 // the array literal's entries. Nested blocks have the same problem. 19411 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 19412 " @\"a\",\n" 19413 " @\"a\"\n" 19414 "]\n" 19415 " copyItems:YES];", 19416 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 19417 " @\"a\",\n" 19418 " @\"a\"\n" 19419 " ]\n" 19420 " copyItems:YES];", 19421 Style)); 19422 EXPECT_EQ( 19423 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 19424 " copyItems:YES];", 19425 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 19426 " copyItems:YES];", 19427 Style)); 19428 19429 verifyFormat("[self.a b:c c:d];", Style); 19430 EXPECT_EQ("[self.a b:c\n" 19431 " c:d];", 19432 format("[self.a b:c\n" 19433 "c:d];", 19434 Style)); 19435 } 19436 19437 TEST_F(FormatTest, FormatsLambdas) { 19438 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 19439 verifyFormat( 19440 "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();\n"); 19441 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 19442 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 19443 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 19444 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 19445 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 19446 verifyFormat("auto c = [a = [b = 42] {}] {};\n"); 19447 verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n"); 19448 verifyFormat("int x = f(*+[] {});"); 19449 verifyFormat("void f() {\n" 19450 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 19451 "}\n"); 19452 verifyFormat("void f() {\n" 19453 " other(x.begin(), //\n" 19454 " x.end(), //\n" 19455 " [&](int, int) { return 1; });\n" 19456 "}\n"); 19457 verifyFormat("void f() {\n" 19458 " other.other.other.other.other(\n" 19459 " x.begin(), x.end(),\n" 19460 " [something, rather](int, int, int, int, int, int, int) { " 19461 "return 1; });\n" 19462 "}\n"); 19463 verifyFormat( 19464 "void f() {\n" 19465 " other.other.other.other.other(\n" 19466 " x.begin(), x.end(),\n" 19467 " [something, rather](int, int, int, int, int, int, int) {\n" 19468 " //\n" 19469 " });\n" 19470 "}\n"); 19471 verifyFormat("SomeFunction([]() { // A cool function...\n" 19472 " return 43;\n" 19473 "});"); 19474 EXPECT_EQ("SomeFunction([]() {\n" 19475 "#define A a\n" 19476 " return 43;\n" 19477 "});", 19478 format("SomeFunction([](){\n" 19479 "#define A a\n" 19480 "return 43;\n" 19481 "});")); 19482 verifyFormat("void f() {\n" 19483 " SomeFunction([](decltype(x), A *a) {});\n" 19484 " SomeFunction([](typeof(x), A *a) {});\n" 19485 " SomeFunction([](_Atomic(x), A *a) {});\n" 19486 " SomeFunction([](__underlying_type(x), A *a) {});\n" 19487 "}"); 19488 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 19489 " [](const aaaaaaaaaa &a) { return a; });"); 19490 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 19491 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 19492 "});"); 19493 verifyFormat("Constructor()\n" 19494 " : Field([] { // comment\n" 19495 " int i;\n" 19496 " }) {}"); 19497 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 19498 " return some_parameter.size();\n" 19499 "};"); 19500 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 19501 " [](const string &s) { return s; };"); 19502 verifyFormat("int i = aaaaaa ? 1 //\n" 19503 " : [] {\n" 19504 " return 2; //\n" 19505 " }();"); 19506 verifyFormat("llvm::errs() << \"number of twos is \"\n" 19507 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 19508 " return x == 2; // force break\n" 19509 " });"); 19510 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 19511 " [=](int iiiiiiiiiiii) {\n" 19512 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n" 19513 " aaaaaaaaaaaaaaaaaaaaaaa;\n" 19514 " });", 19515 getLLVMStyleWithColumns(60)); 19516 19517 verifyFormat("SomeFunction({[&] {\n" 19518 " // comment\n" 19519 " },\n" 19520 " [&] {\n" 19521 " // comment\n" 19522 " }});"); 19523 verifyFormat("SomeFunction({[&] {\n" 19524 " // comment\n" 19525 "}});"); 19526 verifyFormat( 19527 "virtual aaaaaaaaaaaaaaaa(\n" 19528 " std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n" 19529 " aaaaa aaaaaaaaa);"); 19530 19531 // Lambdas with return types. 19532 verifyFormat("int c = []() -> int { return 2; }();\n"); 19533 verifyFormat("int c = []() -> int * { return 2; }();\n"); 19534 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 19535 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 19536 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 19537 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 19538 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 19539 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 19540 verifyFormat("[a, a]() -> a<1> {};"); 19541 verifyFormat("[]() -> foo<5 + 2> { return {}; };"); 19542 verifyFormat("[]() -> foo<5 - 2> { return {}; };"); 19543 verifyFormat("[]() -> foo<5 / 2> { return {}; };"); 19544 verifyFormat("[]() -> foo<5 * 2> { return {}; };"); 19545 verifyFormat("[]() -> foo<5 % 2> { return {}; };"); 19546 verifyFormat("[]() -> foo<5 << 2> { return {}; };"); 19547 verifyFormat("[]() -> foo<!5> { return {}; };"); 19548 verifyFormat("[]() -> foo<~5> { return {}; };"); 19549 verifyFormat("[]() -> foo<5 | 2> { return {}; };"); 19550 verifyFormat("[]() -> foo<5 || 2> { return {}; };"); 19551 verifyFormat("[]() -> foo<5 & 2> { return {}; };"); 19552 verifyFormat("[]() -> foo<5 && 2> { return {}; };"); 19553 verifyFormat("[]() -> foo<5 == 2> { return {}; };"); 19554 verifyFormat("[]() -> foo<5 != 2> { return {}; };"); 19555 verifyFormat("[]() -> foo<5 >= 2> { return {}; };"); 19556 verifyFormat("[]() -> foo<5 <= 2> { return {}; };"); 19557 verifyFormat("[]() -> foo<5 < 2> { return {}; };"); 19558 verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };"); 19559 verifyFormat("namespace bar {\n" 19560 "// broken:\n" 19561 "auto foo{[]() -> foo<5 + 2> { return {}; }};\n" 19562 "} // namespace bar"); 19563 verifyFormat("namespace bar {\n" 19564 "// broken:\n" 19565 "auto foo{[]() -> foo<5 - 2> { return {}; }};\n" 19566 "} // namespace bar"); 19567 verifyFormat("namespace bar {\n" 19568 "// broken:\n" 19569 "auto foo{[]() -> foo<5 / 2> { return {}; }};\n" 19570 "} // namespace bar"); 19571 verifyFormat("namespace bar {\n" 19572 "// broken:\n" 19573 "auto foo{[]() -> foo<5 * 2> { return {}; }};\n" 19574 "} // namespace bar"); 19575 verifyFormat("namespace bar {\n" 19576 "// broken:\n" 19577 "auto foo{[]() -> foo<5 % 2> { return {}; }};\n" 19578 "} // namespace bar"); 19579 verifyFormat("namespace bar {\n" 19580 "// broken:\n" 19581 "auto foo{[]() -> foo<5 << 2> { return {}; }};\n" 19582 "} // namespace bar"); 19583 verifyFormat("namespace bar {\n" 19584 "// broken:\n" 19585 "auto foo{[]() -> foo<!5> { return {}; }};\n" 19586 "} // namespace bar"); 19587 verifyFormat("namespace bar {\n" 19588 "// broken:\n" 19589 "auto foo{[]() -> foo<~5> { return {}; }};\n" 19590 "} // namespace bar"); 19591 verifyFormat("namespace bar {\n" 19592 "// broken:\n" 19593 "auto foo{[]() -> foo<5 | 2> { return {}; }};\n" 19594 "} // namespace bar"); 19595 verifyFormat("namespace bar {\n" 19596 "// broken:\n" 19597 "auto foo{[]() -> foo<5 || 2> { return {}; }};\n" 19598 "} // namespace bar"); 19599 verifyFormat("namespace bar {\n" 19600 "// broken:\n" 19601 "auto foo{[]() -> foo<5 & 2> { return {}; }};\n" 19602 "} // namespace bar"); 19603 verifyFormat("namespace bar {\n" 19604 "// broken:\n" 19605 "auto foo{[]() -> foo<5 && 2> { return {}; }};\n" 19606 "} // namespace bar"); 19607 verifyFormat("namespace bar {\n" 19608 "// broken:\n" 19609 "auto foo{[]() -> foo<5 == 2> { return {}; }};\n" 19610 "} // namespace bar"); 19611 verifyFormat("namespace bar {\n" 19612 "// broken:\n" 19613 "auto foo{[]() -> foo<5 != 2> { return {}; }};\n" 19614 "} // namespace bar"); 19615 verifyFormat("namespace bar {\n" 19616 "// broken:\n" 19617 "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n" 19618 "} // namespace bar"); 19619 verifyFormat("namespace bar {\n" 19620 "// broken:\n" 19621 "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n" 19622 "} // namespace bar"); 19623 verifyFormat("namespace bar {\n" 19624 "// broken:\n" 19625 "auto foo{[]() -> foo<5 < 2> { return {}; }};\n" 19626 "} // namespace bar"); 19627 verifyFormat("namespace bar {\n" 19628 "// broken:\n" 19629 "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n" 19630 "} // namespace bar"); 19631 verifyFormat("[]() -> a<1> {};"); 19632 verifyFormat("[]() -> a<1> { ; };"); 19633 verifyFormat("[]() -> a<1> { ; }();"); 19634 verifyFormat("[a, a]() -> a<true> {};"); 19635 verifyFormat("[]() -> a<true> {};"); 19636 verifyFormat("[]() -> a<true> { ; };"); 19637 verifyFormat("[]() -> a<true> { ; }();"); 19638 verifyFormat("[a, a]() -> a<false> {};"); 19639 verifyFormat("[]() -> a<false> {};"); 19640 verifyFormat("[]() -> a<false> { ; };"); 19641 verifyFormat("[]() -> a<false> { ; }();"); 19642 verifyFormat("auto foo{[]() -> foo<false> { ; }};"); 19643 verifyFormat("namespace bar {\n" 19644 "auto foo{[]() -> foo<false> { ; }};\n" 19645 "} // namespace bar"); 19646 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 19647 " int j) -> int {\n" 19648 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 19649 "};"); 19650 verifyFormat( 19651 "aaaaaaaaaaaaaaaaaaaaaa(\n" 19652 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 19653 " return aaaaaaaaaaaaaaaaa;\n" 19654 " });", 19655 getLLVMStyleWithColumns(70)); 19656 verifyFormat("[]() //\n" 19657 " -> int {\n" 19658 " return 1; //\n" 19659 "};"); 19660 verifyFormat("[]() -> Void<T...> {};"); 19661 verifyFormat("[a, b]() -> Tuple<T...> { return {}; };"); 19662 19663 // Lambdas with explicit template argument lists. 19664 verifyFormat( 19665 "auto L = []<template <typename> class T, class U>(T<U> &&a) {};\n"); 19666 19667 // Multiple lambdas in the same parentheses change indentation rules. These 19668 // lambdas are forced to start on new lines. 19669 verifyFormat("SomeFunction(\n" 19670 " []() {\n" 19671 " //\n" 19672 " },\n" 19673 " []() {\n" 19674 " //\n" 19675 " });"); 19676 19677 // A lambda passed as arg0 is always pushed to the next line. 19678 verifyFormat("SomeFunction(\n" 19679 " [this] {\n" 19680 " //\n" 19681 " },\n" 19682 " 1);\n"); 19683 19684 // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like 19685 // the arg0 case above. 19686 auto Style = getGoogleStyle(); 19687 Style.BinPackArguments = false; 19688 verifyFormat("SomeFunction(\n" 19689 " a,\n" 19690 " [this] {\n" 19691 " //\n" 19692 " },\n" 19693 " b);\n", 19694 Style); 19695 verifyFormat("SomeFunction(\n" 19696 " a,\n" 19697 " [this] {\n" 19698 " //\n" 19699 " },\n" 19700 " b);\n"); 19701 19702 // A lambda with a very long line forces arg0 to be pushed out irrespective of 19703 // the BinPackArguments value (as long as the code is wide enough). 19704 verifyFormat( 19705 "something->SomeFunction(\n" 19706 " a,\n" 19707 " [this] {\n" 19708 " " 19709 "D0000000000000000000000000000000000000000000000000000000000001();\n" 19710 " },\n" 19711 " b);\n"); 19712 19713 // A multi-line lambda is pulled up as long as the introducer fits on the 19714 // previous line and there are no further args. 19715 verifyFormat("function(1, [this, that] {\n" 19716 " //\n" 19717 "});\n"); 19718 verifyFormat("function([this, that] {\n" 19719 " //\n" 19720 "});\n"); 19721 // FIXME: this format is not ideal and we should consider forcing the first 19722 // arg onto its own line. 19723 verifyFormat("function(a, b, c, //\n" 19724 " d, [this, that] {\n" 19725 " //\n" 19726 " });\n"); 19727 19728 // Multiple lambdas are treated correctly even when there is a short arg0. 19729 verifyFormat("SomeFunction(\n" 19730 " 1,\n" 19731 " [this] {\n" 19732 " //\n" 19733 " },\n" 19734 " [this] {\n" 19735 " //\n" 19736 " },\n" 19737 " 1);\n"); 19738 19739 // More complex introducers. 19740 verifyFormat("return [i, args...] {};"); 19741 19742 // Not lambdas. 19743 verifyFormat("constexpr char hello[]{\"hello\"};"); 19744 verifyFormat("double &operator[](int i) { return 0; }\n" 19745 "int i;"); 19746 verifyFormat("std::unique_ptr<int[]> foo() {}"); 19747 verifyFormat("int i = a[a][a]->f();"); 19748 verifyFormat("int i = (*b)[a]->f();"); 19749 19750 // Other corner cases. 19751 verifyFormat("void f() {\n" 19752 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 19753 " );\n" 19754 "}"); 19755 19756 // Lambdas created through weird macros. 19757 verifyFormat("void f() {\n" 19758 " MACRO((const AA &a) { return 1; });\n" 19759 " MACRO((AA &a) { return 1; });\n" 19760 "}"); 19761 19762 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 19763 " doo_dah();\n" 19764 " doo_dah();\n" 19765 " })) {\n" 19766 "}"); 19767 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n" 19768 " doo_dah();\n" 19769 " doo_dah();\n" 19770 " })) {\n" 19771 "}"); 19772 verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n" 19773 " doo_dah();\n" 19774 " doo_dah();\n" 19775 " })) {\n" 19776 "}"); 19777 verifyFormat("auto lambda = []() {\n" 19778 " int a = 2\n" 19779 "#if A\n" 19780 " + 2\n" 19781 "#endif\n" 19782 " ;\n" 19783 "};"); 19784 19785 // Lambdas with complex multiline introducers. 19786 verifyFormat( 19787 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 19788 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n" 19789 " -> ::std::unordered_set<\n" 19790 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 19791 " //\n" 19792 " });"); 19793 19794 FormatStyle DoNotMerge = getLLVMStyle(); 19795 DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 19796 verifyFormat("auto c = []() {\n" 19797 " return b;\n" 19798 "};", 19799 "auto c = []() { return b; };", DoNotMerge); 19800 verifyFormat("auto c = []() {\n" 19801 "};", 19802 " auto c = []() {};", DoNotMerge); 19803 19804 FormatStyle MergeEmptyOnly = getLLVMStyle(); 19805 MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty; 19806 verifyFormat("auto c = []() {\n" 19807 " return b;\n" 19808 "};", 19809 "auto c = []() {\n" 19810 " return b;\n" 19811 " };", 19812 MergeEmptyOnly); 19813 verifyFormat("auto c = []() {};", 19814 "auto c = []() {\n" 19815 "};", 19816 MergeEmptyOnly); 19817 19818 FormatStyle MergeInline = getLLVMStyle(); 19819 MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline; 19820 verifyFormat("auto c = []() {\n" 19821 " return b;\n" 19822 "};", 19823 "auto c = []() { return b; };", MergeInline); 19824 verifyFormat("function([]() { return b; })", "function([]() { return b; })", 19825 MergeInline); 19826 verifyFormat("function([]() { return b; }, a)", 19827 "function([]() { return b; }, a)", MergeInline); 19828 verifyFormat("function(a, []() { return b; })", 19829 "function(a, []() { return b; })", MergeInline); 19830 19831 // Check option "BraceWrapping.BeforeLambdaBody" and different state of 19832 // AllowShortLambdasOnASingleLine 19833 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle(); 19834 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 19835 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 19836 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 19837 FormatStyle::ShortLambdaStyle::SLS_None; 19838 verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n" 19839 " []()\n" 19840 " {\n" 19841 " return 17;\n" 19842 " });", 19843 LLVMWithBeforeLambdaBody); 19844 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n" 19845 " []()\n" 19846 " {\n" 19847 " });", 19848 LLVMWithBeforeLambdaBody); 19849 verifyFormat("auto fct_SLS_None = []()\n" 19850 "{\n" 19851 " return 17;\n" 19852 "};", 19853 LLVMWithBeforeLambdaBody); 19854 verifyFormat("TwoNestedLambdas_SLS_None(\n" 19855 " []()\n" 19856 " {\n" 19857 " return Call(\n" 19858 " []()\n" 19859 " {\n" 19860 " return 17;\n" 19861 " });\n" 19862 " });", 19863 LLVMWithBeforeLambdaBody); 19864 verifyFormat("void Fct() {\n" 19865 " return {[]()\n" 19866 " {\n" 19867 " return 17;\n" 19868 " }};\n" 19869 "}", 19870 LLVMWithBeforeLambdaBody); 19871 19872 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 19873 FormatStyle::ShortLambdaStyle::SLS_Empty; 19874 verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n" 19875 " []()\n" 19876 " {\n" 19877 " return 17;\n" 19878 " });", 19879 LLVMWithBeforeLambdaBody); 19880 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});", 19881 LLVMWithBeforeLambdaBody); 19882 verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL" 19883 "ongFunctionName_SLS_Empty(\n" 19884 " []() {});", 19885 LLVMWithBeforeLambdaBody); 19886 verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n" 19887 " []()\n" 19888 " {\n" 19889 " return 17;\n" 19890 " });", 19891 LLVMWithBeforeLambdaBody); 19892 verifyFormat("auto fct_SLS_Empty = []()\n" 19893 "{\n" 19894 " return 17;\n" 19895 "};", 19896 LLVMWithBeforeLambdaBody); 19897 verifyFormat("TwoNestedLambdas_SLS_Empty(\n" 19898 " []()\n" 19899 " {\n" 19900 " return Call([]() {});\n" 19901 " });", 19902 LLVMWithBeforeLambdaBody); 19903 verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n" 19904 " []()\n" 19905 " {\n" 19906 " return Call([]() {});\n" 19907 " });", 19908 LLVMWithBeforeLambdaBody); 19909 verifyFormat( 19910 "FctWithLongLineInLambda_SLS_Empty(\n" 19911 " []()\n" 19912 " {\n" 19913 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 19914 " AndShouldNotBeConsiderAsInline,\n" 19915 " LambdaBodyMustBeBreak);\n" 19916 " });", 19917 LLVMWithBeforeLambdaBody); 19918 19919 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 19920 FormatStyle::ShortLambdaStyle::SLS_Inline; 19921 verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });", 19922 LLVMWithBeforeLambdaBody); 19923 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});", 19924 LLVMWithBeforeLambdaBody); 19925 verifyFormat("auto fct_SLS_Inline = []()\n" 19926 "{\n" 19927 " return 17;\n" 19928 "};", 19929 LLVMWithBeforeLambdaBody); 19930 verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return " 19931 "17; }); });", 19932 LLVMWithBeforeLambdaBody); 19933 verifyFormat( 19934 "FctWithLongLineInLambda_SLS_Inline(\n" 19935 " []()\n" 19936 " {\n" 19937 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 19938 " AndShouldNotBeConsiderAsInline,\n" 19939 " LambdaBodyMustBeBreak);\n" 19940 " });", 19941 LLVMWithBeforeLambdaBody); 19942 verifyFormat("FctWithMultipleParams_SLS_Inline(" 19943 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n" 19944 " []() { return 17; });", 19945 LLVMWithBeforeLambdaBody); 19946 verifyFormat( 19947 "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });", 19948 LLVMWithBeforeLambdaBody); 19949 19950 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 19951 FormatStyle::ShortLambdaStyle::SLS_All; 19952 verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });", 19953 LLVMWithBeforeLambdaBody); 19954 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});", 19955 LLVMWithBeforeLambdaBody); 19956 verifyFormat("auto fct_SLS_All = []() { return 17; };", 19957 LLVMWithBeforeLambdaBody); 19958 verifyFormat("FctWithOneParam_SLS_All(\n" 19959 " []()\n" 19960 " {\n" 19961 " // A cool function...\n" 19962 " return 43;\n" 19963 " });", 19964 LLVMWithBeforeLambdaBody); 19965 verifyFormat("FctWithMultipleParams_SLS_All(" 19966 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n" 19967 " []() { return 17; });", 19968 LLVMWithBeforeLambdaBody); 19969 verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });", 19970 LLVMWithBeforeLambdaBody); 19971 verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });", 19972 LLVMWithBeforeLambdaBody); 19973 verifyFormat( 19974 "FctWithLongLineInLambda_SLS_All(\n" 19975 " []()\n" 19976 " {\n" 19977 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 19978 " AndShouldNotBeConsiderAsInline,\n" 19979 " LambdaBodyMustBeBreak);\n" 19980 " });", 19981 LLVMWithBeforeLambdaBody); 19982 verifyFormat( 19983 "auto fct_SLS_All = []()\n" 19984 "{\n" 19985 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 19986 " AndShouldNotBeConsiderAsInline,\n" 19987 " LambdaBodyMustBeBreak);\n" 19988 "};", 19989 LLVMWithBeforeLambdaBody); 19990 LLVMWithBeforeLambdaBody.BinPackParameters = false; 19991 verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);", 19992 LLVMWithBeforeLambdaBody); 19993 verifyFormat( 19994 "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n" 19995 " FirstParam,\n" 19996 " SecondParam,\n" 19997 " ThirdParam,\n" 19998 " FourthParam);", 19999 LLVMWithBeforeLambdaBody); 20000 verifyFormat("FctWithLongLineInLambda_SLS_All(\n" 20001 " []() { return " 20002 "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n" 20003 " FirstParam,\n" 20004 " SecondParam,\n" 20005 " ThirdParam,\n" 20006 " FourthParam);", 20007 LLVMWithBeforeLambdaBody); 20008 verifyFormat( 20009 "FctWithLongLineInLambda_SLS_All(FirstParam,\n" 20010 " SecondParam,\n" 20011 " ThirdParam,\n" 20012 " FourthParam,\n" 20013 " []() { return SomeValueNotSoLong; });", 20014 LLVMWithBeforeLambdaBody); 20015 verifyFormat("FctWithLongLineInLambda_SLS_All(\n" 20016 " []()\n" 20017 " {\n" 20018 " return " 20019 "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB" 20020 "eConsiderAsInline;\n" 20021 " });", 20022 LLVMWithBeforeLambdaBody); 20023 verifyFormat( 20024 "FctWithLongLineInLambda_SLS_All(\n" 20025 " []()\n" 20026 " {\n" 20027 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 20028 " AndShouldNotBeConsiderAsInline,\n" 20029 " LambdaBodyMustBeBreak);\n" 20030 " });", 20031 LLVMWithBeforeLambdaBody); 20032 verifyFormat("FctWithTwoParams_SLS_All(\n" 20033 " []()\n" 20034 " {\n" 20035 " // A cool function...\n" 20036 " return 43;\n" 20037 " },\n" 20038 " 87);", 20039 LLVMWithBeforeLambdaBody); 20040 verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);", 20041 LLVMWithBeforeLambdaBody); 20042 verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });", 20043 LLVMWithBeforeLambdaBody); 20044 verifyFormat( 20045 "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });", 20046 LLVMWithBeforeLambdaBody); 20047 verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; " 20048 "}); }, x);", 20049 LLVMWithBeforeLambdaBody); 20050 verifyFormat("TwoNestedLambdas_SLS_All(\n" 20051 " []()\n" 20052 " {\n" 20053 " // A cool function...\n" 20054 " return Call([]() { return 17; });\n" 20055 " });", 20056 LLVMWithBeforeLambdaBody); 20057 verifyFormat("TwoNestedLambdas_SLS_All(\n" 20058 " []()\n" 20059 " {\n" 20060 " return Call(\n" 20061 " []()\n" 20062 " {\n" 20063 " // A cool function...\n" 20064 " return 17;\n" 20065 " });\n" 20066 " });", 20067 LLVMWithBeforeLambdaBody); 20068 20069 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20070 FormatStyle::ShortLambdaStyle::SLS_None; 20071 20072 verifyFormat("auto select = [this]() -> const Library::Object *\n" 20073 "{\n" 20074 " return MyAssignment::SelectFromList(this);\n" 20075 "};\n", 20076 LLVMWithBeforeLambdaBody); 20077 20078 verifyFormat("auto select = [this]() -> const Library::Object &\n" 20079 "{\n" 20080 " return MyAssignment::SelectFromList(this);\n" 20081 "};\n", 20082 LLVMWithBeforeLambdaBody); 20083 20084 verifyFormat("auto select = [this]() -> std::unique_ptr<Object>\n" 20085 "{\n" 20086 " return MyAssignment::SelectFromList(this);\n" 20087 "};\n", 20088 LLVMWithBeforeLambdaBody); 20089 20090 verifyFormat("namespace test {\n" 20091 "class Test {\n" 20092 "public:\n" 20093 " Test() = default;\n" 20094 "};\n" 20095 "} // namespace test", 20096 LLVMWithBeforeLambdaBody); 20097 20098 // Lambdas with different indentation styles. 20099 Style = getLLVMStyleWithColumns(100); 20100 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 20101 " return promise.then(\n" 20102 " [this, &someVariable, someObject = " 20103 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 20104 " return someObject.startAsyncAction().then(\n" 20105 " [this, &someVariable](AsyncActionResult result) " 20106 "mutable { result.processMore(); });\n" 20107 " });\n" 20108 "}\n", 20109 format("SomeResult doSomething(SomeObject promise) {\n" 20110 " return promise.then([this, &someVariable, someObject = " 20111 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 20112 " return someObject.startAsyncAction().then([this, " 20113 "&someVariable](AsyncActionResult result) mutable {\n" 20114 " result.processMore();\n" 20115 " });\n" 20116 " });\n" 20117 "}\n", 20118 Style)); 20119 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope; 20120 verifyFormat("test() {\n" 20121 " ([]() -> {\n" 20122 " int b = 32;\n" 20123 " return 3;\n" 20124 " }).foo();\n" 20125 "}", 20126 Style); 20127 verifyFormat("test() {\n" 20128 " []() -> {\n" 20129 " int b = 32;\n" 20130 " return 3;\n" 20131 " }\n" 20132 "}", 20133 Style); 20134 verifyFormat("std::sort(v.begin(), v.end(),\n" 20135 " [](const auto &someLongArgumentName, const auto " 20136 "&someOtherLongArgumentName) {\n" 20137 " return someLongArgumentName.someMemberVariable < " 20138 "someOtherLongArgumentName.someMemberVariable;\n" 20139 "});", 20140 Style); 20141 verifyFormat("test() {\n" 20142 " (\n" 20143 " []() -> {\n" 20144 " int b = 32;\n" 20145 " return 3;\n" 20146 " },\n" 20147 " foo, bar)\n" 20148 " .foo();\n" 20149 "}", 20150 Style); 20151 verifyFormat("test() {\n" 20152 " ([]() -> {\n" 20153 " int b = 32;\n" 20154 " return 3;\n" 20155 " })\n" 20156 " .foo()\n" 20157 " .bar();\n" 20158 "}", 20159 Style); 20160 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 20161 " return promise.then(\n" 20162 " [this, &someVariable, someObject = " 20163 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 20164 " return someObject.startAsyncAction().then(\n" 20165 " [this, &someVariable](AsyncActionResult result) mutable { " 20166 "result.processMore(); });\n" 20167 " });\n" 20168 "}\n", 20169 format("SomeResult doSomething(SomeObject promise) {\n" 20170 " return promise.then([this, &someVariable, someObject = " 20171 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 20172 " return someObject.startAsyncAction().then([this, " 20173 "&someVariable](AsyncActionResult result) mutable {\n" 20174 " result.processMore();\n" 20175 " });\n" 20176 " });\n" 20177 "}\n", 20178 Style)); 20179 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 20180 " return promise.then([this, &someVariable] {\n" 20181 " return someObject.startAsyncAction().then(\n" 20182 " [this, &someVariable](AsyncActionResult result) mutable { " 20183 "result.processMore(); });\n" 20184 " });\n" 20185 "}\n", 20186 format("SomeResult doSomething(SomeObject promise) {\n" 20187 " return promise.then([this, &someVariable] {\n" 20188 " return someObject.startAsyncAction().then([this, " 20189 "&someVariable](AsyncActionResult result) mutable {\n" 20190 " result.processMore();\n" 20191 " });\n" 20192 " });\n" 20193 "}\n", 20194 Style)); 20195 Style = getGoogleStyle(); 20196 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope; 20197 EXPECT_EQ("#define A \\\n" 20198 " [] { \\\n" 20199 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 20200 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 20201 " }", 20202 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 20203 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 20204 Style)); 20205 // TODO: The current formatting has a minor issue that's not worth fixing 20206 // right now whereby the closing brace is indented relative to the signature 20207 // instead of being aligned. This only happens with macros. 20208 } 20209 20210 TEST_F(FormatTest, LambdaWithLineComments) { 20211 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle(); 20212 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 20213 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 20214 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20215 FormatStyle::ShortLambdaStyle::SLS_All; 20216 20217 verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody); 20218 verifyFormat("auto k = []() // comment\n" 20219 "{ return; }", 20220 LLVMWithBeforeLambdaBody); 20221 verifyFormat("auto k = []() /* comment */ { return; }", 20222 LLVMWithBeforeLambdaBody); 20223 verifyFormat("auto k = []() /* comment */ /* comment */ { return; }", 20224 LLVMWithBeforeLambdaBody); 20225 verifyFormat("auto k = []() // X\n" 20226 "{ return; }", 20227 LLVMWithBeforeLambdaBody); 20228 verifyFormat( 20229 "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n" 20230 "{ return; }", 20231 LLVMWithBeforeLambdaBody); 20232 } 20233 20234 TEST_F(FormatTest, EmptyLinesInLambdas) { 20235 verifyFormat("auto lambda = []() {\n" 20236 " x(); //\n" 20237 "};", 20238 "auto lambda = []() {\n" 20239 "\n" 20240 " x(); //\n" 20241 "\n" 20242 "};"); 20243 } 20244 20245 TEST_F(FormatTest, FormatsBlocks) { 20246 FormatStyle ShortBlocks = getLLVMStyle(); 20247 ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 20248 verifyFormat("int (^Block)(int, int);", ShortBlocks); 20249 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 20250 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 20251 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 20252 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 20253 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 20254 20255 verifyFormat("foo(^{ bar(); });", ShortBlocks); 20256 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 20257 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 20258 20259 verifyFormat("[operation setCompletionBlock:^{\n" 20260 " [self onOperationDone];\n" 20261 "}];"); 20262 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 20263 " [self onOperationDone];\n" 20264 "}]};"); 20265 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 20266 " f();\n" 20267 "}];"); 20268 verifyFormat("int a = [operation block:^int(int *i) {\n" 20269 " return 1;\n" 20270 "}];"); 20271 verifyFormat("[myObject doSomethingWith:arg1\n" 20272 " aaa:^int(int *a) {\n" 20273 " return 1;\n" 20274 " }\n" 20275 " bbb:f(a * bbbbbbbb)];"); 20276 20277 verifyFormat("[operation setCompletionBlock:^{\n" 20278 " [self.delegate newDataAvailable];\n" 20279 "}];", 20280 getLLVMStyleWithColumns(60)); 20281 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 20282 " NSString *path = [self sessionFilePath];\n" 20283 " if (path) {\n" 20284 " // ...\n" 20285 " }\n" 20286 "});"); 20287 verifyFormat("[[SessionService sharedService]\n" 20288 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 20289 " if (window) {\n" 20290 " [self windowDidLoad:window];\n" 20291 " } else {\n" 20292 " [self errorLoadingWindow];\n" 20293 " }\n" 20294 " }];"); 20295 verifyFormat("void (^largeBlock)(void) = ^{\n" 20296 " // ...\n" 20297 "};\n", 20298 getLLVMStyleWithColumns(40)); 20299 verifyFormat("[[SessionService sharedService]\n" 20300 " loadWindowWithCompletionBlock: //\n" 20301 " ^(SessionWindow *window) {\n" 20302 " if (window) {\n" 20303 " [self windowDidLoad:window];\n" 20304 " } else {\n" 20305 " [self errorLoadingWindow];\n" 20306 " }\n" 20307 " }];", 20308 getLLVMStyleWithColumns(60)); 20309 verifyFormat("[myObject doSomethingWith:arg1\n" 20310 " firstBlock:^(Foo *a) {\n" 20311 " // ...\n" 20312 " int i;\n" 20313 " }\n" 20314 " secondBlock:^(Bar *b) {\n" 20315 " // ...\n" 20316 " int i;\n" 20317 " }\n" 20318 " thirdBlock:^Foo(Bar *b) {\n" 20319 " // ...\n" 20320 " int i;\n" 20321 " }];"); 20322 verifyFormat("[myObject doSomethingWith:arg1\n" 20323 " firstBlock:-1\n" 20324 " secondBlock:^(Bar *b) {\n" 20325 " // ...\n" 20326 " int i;\n" 20327 " }];"); 20328 20329 verifyFormat("f(^{\n" 20330 " @autoreleasepool {\n" 20331 " if (a) {\n" 20332 " g();\n" 20333 " }\n" 20334 " }\n" 20335 "});"); 20336 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 20337 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" 20338 "};"); 20339 20340 FormatStyle FourIndent = getLLVMStyle(); 20341 FourIndent.ObjCBlockIndentWidth = 4; 20342 verifyFormat("[operation setCompletionBlock:^{\n" 20343 " [self onOperationDone];\n" 20344 "}];", 20345 FourIndent); 20346 } 20347 20348 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 20349 FormatStyle ZeroColumn = getLLVMStyle(); 20350 ZeroColumn.ColumnLimit = 0; 20351 20352 verifyFormat("[[SessionService sharedService] " 20353 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 20354 " if (window) {\n" 20355 " [self windowDidLoad:window];\n" 20356 " } else {\n" 20357 " [self errorLoadingWindow];\n" 20358 " }\n" 20359 "}];", 20360 ZeroColumn); 20361 EXPECT_EQ("[[SessionService sharedService]\n" 20362 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 20363 " if (window) {\n" 20364 " [self windowDidLoad:window];\n" 20365 " } else {\n" 20366 " [self errorLoadingWindow];\n" 20367 " }\n" 20368 " }];", 20369 format("[[SessionService sharedService]\n" 20370 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 20371 " if (window) {\n" 20372 " [self windowDidLoad:window];\n" 20373 " } else {\n" 20374 " [self errorLoadingWindow];\n" 20375 " }\n" 20376 "}];", 20377 ZeroColumn)); 20378 verifyFormat("[myObject doSomethingWith:arg1\n" 20379 " firstBlock:^(Foo *a) {\n" 20380 " // ...\n" 20381 " int i;\n" 20382 " }\n" 20383 " secondBlock:^(Bar *b) {\n" 20384 " // ...\n" 20385 " int i;\n" 20386 " }\n" 20387 " thirdBlock:^Foo(Bar *b) {\n" 20388 " // ...\n" 20389 " int i;\n" 20390 " }];", 20391 ZeroColumn); 20392 verifyFormat("f(^{\n" 20393 " @autoreleasepool {\n" 20394 " if (a) {\n" 20395 " g();\n" 20396 " }\n" 20397 " }\n" 20398 "});", 20399 ZeroColumn); 20400 verifyFormat("void (^largeBlock)(void) = ^{\n" 20401 " // ...\n" 20402 "};", 20403 ZeroColumn); 20404 20405 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 20406 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 20407 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 20408 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 20409 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 20410 " int i;\n" 20411 "};", 20412 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 20413 } 20414 20415 TEST_F(FormatTest, SupportsCRLF) { 20416 EXPECT_EQ("int a;\r\n" 20417 "int b;\r\n" 20418 "int c;\r\n", 20419 format("int a;\r\n" 20420 " int b;\r\n" 20421 " int c;\r\n", 20422 getLLVMStyle())); 20423 EXPECT_EQ("int a;\r\n" 20424 "int b;\r\n" 20425 "int c;\r\n", 20426 format("int a;\r\n" 20427 " int b;\n" 20428 " int c;\r\n", 20429 getLLVMStyle())); 20430 EXPECT_EQ("int a;\n" 20431 "int b;\n" 20432 "int c;\n", 20433 format("int a;\r\n" 20434 " int b;\n" 20435 " int c;\n", 20436 getLLVMStyle())); 20437 EXPECT_EQ("\"aaaaaaa \"\r\n" 20438 "\"bbbbbbb\";\r\n", 20439 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 20440 EXPECT_EQ("#define A \\\r\n" 20441 " b; \\\r\n" 20442 " c; \\\r\n" 20443 " d;\r\n", 20444 format("#define A \\\r\n" 20445 " b; \\\r\n" 20446 " c; d; \r\n", 20447 getGoogleStyle())); 20448 20449 EXPECT_EQ("/*\r\n" 20450 "multi line block comments\r\n" 20451 "should not introduce\r\n" 20452 "an extra carriage return\r\n" 20453 "*/\r\n", 20454 format("/*\r\n" 20455 "multi line block comments\r\n" 20456 "should not introduce\r\n" 20457 "an extra carriage return\r\n" 20458 "*/\r\n")); 20459 EXPECT_EQ("/*\r\n" 20460 "\r\n" 20461 "*/", 20462 format("/*\r\n" 20463 " \r\r\r\n" 20464 "*/")); 20465 20466 FormatStyle style = getLLVMStyle(); 20467 20468 style.DeriveLineEnding = true; 20469 style.UseCRLF = false; 20470 EXPECT_EQ("union FooBarBazQux {\n" 20471 " int foo;\n" 20472 " int bar;\n" 20473 " int baz;\n" 20474 "};", 20475 format("union FooBarBazQux {\r\n" 20476 " int foo;\n" 20477 " int bar;\r\n" 20478 " int baz;\n" 20479 "};", 20480 style)); 20481 style.UseCRLF = true; 20482 EXPECT_EQ("union FooBarBazQux {\r\n" 20483 " int foo;\r\n" 20484 " int bar;\r\n" 20485 " int baz;\r\n" 20486 "};", 20487 format("union FooBarBazQux {\r\n" 20488 " int foo;\n" 20489 " int bar;\r\n" 20490 " int baz;\n" 20491 "};", 20492 style)); 20493 20494 style.DeriveLineEnding = false; 20495 style.UseCRLF = false; 20496 EXPECT_EQ("union FooBarBazQux {\n" 20497 " int foo;\n" 20498 " int bar;\n" 20499 " int baz;\n" 20500 " int qux;\n" 20501 "};", 20502 format("union FooBarBazQux {\r\n" 20503 " int foo;\n" 20504 " int bar;\r\n" 20505 " int baz;\n" 20506 " int qux;\r\n" 20507 "};", 20508 style)); 20509 style.UseCRLF = true; 20510 EXPECT_EQ("union FooBarBazQux {\r\n" 20511 " int foo;\r\n" 20512 " int bar;\r\n" 20513 " int baz;\r\n" 20514 " int qux;\r\n" 20515 "};", 20516 format("union FooBarBazQux {\r\n" 20517 " int foo;\n" 20518 " int bar;\r\n" 20519 " int baz;\n" 20520 " int qux;\n" 20521 "};", 20522 style)); 20523 20524 style.DeriveLineEnding = true; 20525 style.UseCRLF = false; 20526 EXPECT_EQ("union FooBarBazQux {\r\n" 20527 " int foo;\r\n" 20528 " int bar;\r\n" 20529 " int baz;\r\n" 20530 " int qux;\r\n" 20531 "};", 20532 format("union FooBarBazQux {\r\n" 20533 " int foo;\n" 20534 " int bar;\r\n" 20535 " int baz;\n" 20536 " int qux;\r\n" 20537 "};", 20538 style)); 20539 style.UseCRLF = true; 20540 EXPECT_EQ("union FooBarBazQux {\n" 20541 " int foo;\n" 20542 " int bar;\n" 20543 " int baz;\n" 20544 " int qux;\n" 20545 "};", 20546 format("union FooBarBazQux {\r\n" 20547 " int foo;\n" 20548 " int bar;\r\n" 20549 " int baz;\n" 20550 " int qux;\n" 20551 "};", 20552 style)); 20553 } 20554 20555 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 20556 verifyFormat("MY_CLASS(C) {\n" 20557 " int i;\n" 20558 " int j;\n" 20559 "};"); 20560 } 20561 20562 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 20563 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 20564 TwoIndent.ContinuationIndentWidth = 2; 20565 20566 EXPECT_EQ("int i =\n" 20567 " longFunction(\n" 20568 " arg);", 20569 format("int i = longFunction(arg);", TwoIndent)); 20570 20571 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 20572 SixIndent.ContinuationIndentWidth = 6; 20573 20574 EXPECT_EQ("int i =\n" 20575 " longFunction(\n" 20576 " arg);", 20577 format("int i = longFunction(arg);", SixIndent)); 20578 } 20579 20580 TEST_F(FormatTest, WrappedClosingParenthesisIndent) { 20581 FormatStyle Style = getLLVMStyle(); 20582 verifyFormat("int Foo::getter(\n" 20583 " //\n" 20584 ") const {\n" 20585 " return foo;\n" 20586 "}", 20587 Style); 20588 verifyFormat("void Foo::setter(\n" 20589 " //\n" 20590 ") {\n" 20591 " foo = 1;\n" 20592 "}", 20593 Style); 20594 } 20595 20596 TEST_F(FormatTest, SpacesInAngles) { 20597 FormatStyle Spaces = getLLVMStyle(); 20598 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 20599 20600 verifyFormat("vector< ::std::string > x1;", Spaces); 20601 verifyFormat("Foo< int, Bar > x2;", Spaces); 20602 verifyFormat("Foo< ::int, ::Bar > x3;", Spaces); 20603 20604 verifyFormat("static_cast< int >(arg);", Spaces); 20605 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 20606 verifyFormat("f< int, float >();", Spaces); 20607 verifyFormat("template <> g() {}", Spaces); 20608 verifyFormat("template < std::vector< int > > f() {}", Spaces); 20609 verifyFormat("std::function< void(int, int) > fct;", Spaces); 20610 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 20611 Spaces); 20612 20613 Spaces.Standard = FormatStyle::LS_Cpp03; 20614 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 20615 verifyFormat("A< A< int > >();", Spaces); 20616 20617 Spaces.SpacesInAngles = FormatStyle::SIAS_Never; 20618 verifyFormat("A<A<int> >();", Spaces); 20619 20620 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave; 20621 verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;", 20622 Spaces); 20623 verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;", 20624 Spaces); 20625 20626 verifyFormat("A<A<int> >();", Spaces); 20627 verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces); 20628 verifyFormat("A< A< int > >();", Spaces); 20629 20630 Spaces.Standard = FormatStyle::LS_Cpp11; 20631 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 20632 verifyFormat("A< A< int > >();", Spaces); 20633 20634 Spaces.SpacesInAngles = FormatStyle::SIAS_Never; 20635 verifyFormat("vector<::std::string> x4;", Spaces); 20636 verifyFormat("vector<int> x5;", Spaces); 20637 verifyFormat("Foo<int, Bar> x6;", Spaces); 20638 verifyFormat("Foo<::int, ::Bar> x7;", Spaces); 20639 20640 verifyFormat("A<A<int>>();", Spaces); 20641 20642 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave; 20643 verifyFormat("vector<::std::string> x4;", Spaces); 20644 verifyFormat("vector< ::std::string > x4;", Spaces); 20645 verifyFormat("vector<int> x5;", Spaces); 20646 verifyFormat("vector< int > x5;", Spaces); 20647 verifyFormat("Foo<int, Bar> x6;", Spaces); 20648 verifyFormat("Foo< int, Bar > x6;", Spaces); 20649 verifyFormat("Foo<::int, ::Bar> x7;", Spaces); 20650 verifyFormat("Foo< ::int, ::Bar > x7;", Spaces); 20651 20652 verifyFormat("A<A<int>>();", Spaces); 20653 verifyFormat("A< A< int > >();", Spaces); 20654 verifyFormat("A<A<int > >();", Spaces); 20655 verifyFormat("A< A< int>>();", Spaces); 20656 } 20657 20658 TEST_F(FormatTest, SpaceAfterTemplateKeyword) { 20659 FormatStyle Style = getLLVMStyle(); 20660 Style.SpaceAfterTemplateKeyword = false; 20661 verifyFormat("template<int> void foo();", Style); 20662 } 20663 20664 TEST_F(FormatTest, TripleAngleBrackets) { 20665 verifyFormat("f<<<1, 1>>>();"); 20666 verifyFormat("f<<<1, 1, 1, s>>>();"); 20667 verifyFormat("f<<<a, b, c, d>>>();"); 20668 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 20669 verifyFormat("f<param><<<1, 1>>>();"); 20670 verifyFormat("f<1><<<1, 1>>>();"); 20671 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 20672 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 20673 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 20674 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n" 20675 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();"); 20676 } 20677 20678 TEST_F(FormatTest, MergeLessLessAtEnd) { 20679 verifyFormat("<<"); 20680 EXPECT_EQ("< < <", format("\\\n<<<")); 20681 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 20682 "aaallvm::outs() <<"); 20683 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 20684 "aaaallvm::outs()\n <<"); 20685 } 20686 20687 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 20688 std::string code = "#if A\n" 20689 "#if B\n" 20690 "a.\n" 20691 "#endif\n" 20692 " a = 1;\n" 20693 "#else\n" 20694 "#endif\n" 20695 "#if C\n" 20696 "#else\n" 20697 "#endif\n"; 20698 EXPECT_EQ(code, format(code)); 20699 } 20700 20701 TEST_F(FormatTest, HandleConflictMarkers) { 20702 // Git/SVN conflict markers. 20703 EXPECT_EQ("int a;\n" 20704 "void f() {\n" 20705 " callme(some(parameter1,\n" 20706 "<<<<<<< text by the vcs\n" 20707 " parameter2),\n" 20708 "||||||| text by the vcs\n" 20709 " parameter2),\n" 20710 " parameter3,\n" 20711 "======= text by the vcs\n" 20712 " parameter2, parameter3),\n" 20713 ">>>>>>> text by the vcs\n" 20714 " otherparameter);\n", 20715 format("int a;\n" 20716 "void f() {\n" 20717 " callme(some(parameter1,\n" 20718 "<<<<<<< text by the vcs\n" 20719 " parameter2),\n" 20720 "||||||| text by the vcs\n" 20721 " parameter2),\n" 20722 " parameter3,\n" 20723 "======= text by the vcs\n" 20724 " parameter2,\n" 20725 " parameter3),\n" 20726 ">>>>>>> text by the vcs\n" 20727 " otherparameter);\n")); 20728 20729 // Perforce markers. 20730 EXPECT_EQ("void f() {\n" 20731 " function(\n" 20732 ">>>> text by the vcs\n" 20733 " parameter,\n" 20734 "==== text by the vcs\n" 20735 " parameter,\n" 20736 "==== text by the vcs\n" 20737 " parameter,\n" 20738 "<<<< text by the vcs\n" 20739 " parameter);\n", 20740 format("void f() {\n" 20741 " function(\n" 20742 ">>>> text by the vcs\n" 20743 " parameter,\n" 20744 "==== text by the vcs\n" 20745 " parameter,\n" 20746 "==== text by the vcs\n" 20747 " parameter,\n" 20748 "<<<< text by the vcs\n" 20749 " parameter);\n")); 20750 20751 EXPECT_EQ("<<<<<<<\n" 20752 "|||||||\n" 20753 "=======\n" 20754 ">>>>>>>", 20755 format("<<<<<<<\n" 20756 "|||||||\n" 20757 "=======\n" 20758 ">>>>>>>")); 20759 20760 EXPECT_EQ("<<<<<<<\n" 20761 "|||||||\n" 20762 "int i;\n" 20763 "=======\n" 20764 ">>>>>>>", 20765 format("<<<<<<<\n" 20766 "|||||||\n" 20767 "int i;\n" 20768 "=======\n" 20769 ">>>>>>>")); 20770 20771 // FIXME: Handle parsing of macros around conflict markers correctly: 20772 EXPECT_EQ("#define Macro \\\n" 20773 "<<<<<<<\n" 20774 "Something \\\n" 20775 "|||||||\n" 20776 "Else \\\n" 20777 "=======\n" 20778 "Other \\\n" 20779 ">>>>>>>\n" 20780 " End int i;\n", 20781 format("#define Macro \\\n" 20782 "<<<<<<<\n" 20783 " Something \\\n" 20784 "|||||||\n" 20785 " Else \\\n" 20786 "=======\n" 20787 " Other \\\n" 20788 ">>>>>>>\n" 20789 " End\n" 20790 "int i;\n")); 20791 } 20792 20793 TEST_F(FormatTest, DisableRegions) { 20794 EXPECT_EQ("int i;\n" 20795 "// clang-format off\n" 20796 " int j;\n" 20797 "// clang-format on\n" 20798 "int k;", 20799 format(" int i;\n" 20800 " // clang-format off\n" 20801 " int j;\n" 20802 " // clang-format on\n" 20803 " int k;")); 20804 EXPECT_EQ("int i;\n" 20805 "/* clang-format off */\n" 20806 " int j;\n" 20807 "/* clang-format on */\n" 20808 "int k;", 20809 format(" int i;\n" 20810 " /* clang-format off */\n" 20811 " int j;\n" 20812 " /* clang-format on */\n" 20813 " int k;")); 20814 20815 // Don't reflow comments within disabled regions. 20816 EXPECT_EQ("// clang-format off\n" 20817 "// long long long long long long line\n" 20818 "/* clang-format on */\n" 20819 "/* long long long\n" 20820 " * long long long\n" 20821 " * line */\n" 20822 "int i;\n" 20823 "/* clang-format off */\n" 20824 "/* long long long long long long line */\n", 20825 format("// clang-format off\n" 20826 "// long long long long long long line\n" 20827 "/* clang-format on */\n" 20828 "/* long long long long long long line */\n" 20829 "int i;\n" 20830 "/* clang-format off */\n" 20831 "/* long long long long long long line */\n", 20832 getLLVMStyleWithColumns(20))); 20833 } 20834 20835 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 20836 format("? ) ="); 20837 verifyNoCrash("#define a\\\n /**/}"); 20838 } 20839 20840 TEST_F(FormatTest, FormatsTableGenCode) { 20841 FormatStyle Style = getLLVMStyle(); 20842 Style.Language = FormatStyle::LK_TableGen; 20843 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 20844 } 20845 20846 TEST_F(FormatTest, ArrayOfTemplates) { 20847 EXPECT_EQ("auto a = new unique_ptr<int>[10];", 20848 format("auto a = new unique_ptr<int > [ 10];")); 20849 20850 FormatStyle Spaces = getLLVMStyle(); 20851 Spaces.SpacesInSquareBrackets = true; 20852 EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];", 20853 format("auto a = new unique_ptr<int > [10];", Spaces)); 20854 } 20855 20856 TEST_F(FormatTest, ArrayAsTemplateType) { 20857 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;", 20858 format("auto a = unique_ptr < Foo < Bar>[ 10]> ;")); 20859 20860 FormatStyle Spaces = getLLVMStyle(); 20861 Spaces.SpacesInSquareBrackets = true; 20862 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;", 20863 format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces)); 20864 } 20865 20866 TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); } 20867 20868 TEST(FormatStyle, GetStyleWithEmptyFileName) { 20869 llvm::vfs::InMemoryFileSystem FS; 20870 auto Style1 = getStyle("file", "", "Google", "", &FS); 20871 ASSERT_TRUE((bool)Style1); 20872 ASSERT_EQ(*Style1, getGoogleStyle()); 20873 } 20874 20875 TEST(FormatStyle, GetStyleOfFile) { 20876 llvm::vfs::InMemoryFileSystem FS; 20877 // Test 1: format file in the same directory. 20878 ASSERT_TRUE( 20879 FS.addFile("/a/.clang-format", 0, 20880 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 20881 ASSERT_TRUE( 20882 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 20883 auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS); 20884 ASSERT_TRUE((bool)Style1); 20885 ASSERT_EQ(*Style1, getLLVMStyle()); 20886 20887 // Test 2.1: fallback to default. 20888 ASSERT_TRUE( 20889 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 20890 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS); 20891 ASSERT_TRUE((bool)Style2); 20892 ASSERT_EQ(*Style2, getMozillaStyle()); 20893 20894 // Test 2.2: no format on 'none' fallback style. 20895 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 20896 ASSERT_TRUE((bool)Style2); 20897 ASSERT_EQ(*Style2, getNoStyle()); 20898 20899 // Test 2.3: format if config is found with no based style while fallback is 20900 // 'none'. 20901 ASSERT_TRUE(FS.addFile("/b/.clang-format", 0, 20902 llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2"))); 20903 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 20904 ASSERT_TRUE((bool)Style2); 20905 ASSERT_EQ(*Style2, getLLVMStyle()); 20906 20907 // Test 2.4: format if yaml with no based style, while fallback is 'none'. 20908 Style2 = getStyle("{}", "a.h", "none", "", &FS); 20909 ASSERT_TRUE((bool)Style2); 20910 ASSERT_EQ(*Style2, getLLVMStyle()); 20911 20912 // Test 3: format file in parent directory. 20913 ASSERT_TRUE( 20914 FS.addFile("/c/.clang-format", 0, 20915 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 20916 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, 20917 llvm::MemoryBuffer::getMemBuffer("int i;"))); 20918 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS); 20919 ASSERT_TRUE((bool)Style3); 20920 ASSERT_EQ(*Style3, getGoogleStyle()); 20921 20922 // Test 4: error on invalid fallback style 20923 auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS); 20924 ASSERT_FALSE((bool)Style4); 20925 llvm::consumeError(Style4.takeError()); 20926 20927 // Test 5: error on invalid yaml on command line 20928 auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS); 20929 ASSERT_FALSE((bool)Style5); 20930 llvm::consumeError(Style5.takeError()); 20931 20932 // Test 6: error on invalid style 20933 auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS); 20934 ASSERT_FALSE((bool)Style6); 20935 llvm::consumeError(Style6.takeError()); 20936 20937 // Test 7: found config file, error on parsing it 20938 ASSERT_TRUE( 20939 FS.addFile("/d/.clang-format", 0, 20940 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n" 20941 "InvalidKey: InvalidValue"))); 20942 ASSERT_TRUE( 20943 FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 20944 auto Style7a = getStyle("file", "/d/.clang-format", "LLVM", "", &FS); 20945 ASSERT_FALSE((bool)Style7a); 20946 llvm::consumeError(Style7a.takeError()); 20947 20948 auto Style7b = getStyle("file", "/d/.clang-format", "LLVM", "", &FS, true); 20949 ASSERT_TRUE((bool)Style7b); 20950 20951 // Test 8: inferred per-language defaults apply. 20952 auto StyleTd = getStyle("file", "x.td", "llvm", "", &FS); 20953 ASSERT_TRUE((bool)StyleTd); 20954 ASSERT_EQ(*StyleTd, getLLVMStyle(FormatStyle::LK_TableGen)); 20955 20956 // Test 9.1: overwriting a file style, when parent no file exists with no 20957 // fallback style 20958 ASSERT_TRUE(FS.addFile( 20959 "/e/sub/.clang-format", 0, 20960 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: InheritParentConfig\n" 20961 "ColumnLimit: 20"))); 20962 ASSERT_TRUE(FS.addFile("/e/sub/code.cpp", 0, 20963 llvm::MemoryBuffer::getMemBuffer("int i;"))); 20964 auto Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS); 20965 ASSERT_TRUE(static_cast<bool>(Style9)); 20966 ASSERT_EQ(*Style9, [] { 20967 auto Style = getNoStyle(); 20968 Style.ColumnLimit = 20; 20969 return Style; 20970 }()); 20971 20972 // Test 9.2: with LLVM fallback style 20973 Style9 = getStyle("file", "/e/sub/code.cpp", "LLVM", "", &FS); 20974 ASSERT_TRUE(static_cast<bool>(Style9)); 20975 ASSERT_EQ(*Style9, [] { 20976 auto Style = getLLVMStyle(); 20977 Style.ColumnLimit = 20; 20978 return Style; 20979 }()); 20980 20981 // Test 9.3: with a parent file 20982 ASSERT_TRUE( 20983 FS.addFile("/e/.clang-format", 0, 20984 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google\n" 20985 "UseTab: Always"))); 20986 Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS); 20987 ASSERT_TRUE(static_cast<bool>(Style9)); 20988 ASSERT_EQ(*Style9, [] { 20989 auto Style = getGoogleStyle(); 20990 Style.ColumnLimit = 20; 20991 Style.UseTab = FormatStyle::UT_Always; 20992 return Style; 20993 }()); 20994 20995 // Test 9.4: propagate more than one level 20996 ASSERT_TRUE(FS.addFile("/e/sub/sub/code.cpp", 0, 20997 llvm::MemoryBuffer::getMemBuffer("int i;"))); 20998 ASSERT_TRUE(FS.addFile("/e/sub/sub/.clang-format", 0, 20999 llvm::MemoryBuffer::getMemBuffer( 21000 "BasedOnStyle: InheritParentConfig\n" 21001 "WhitespaceSensitiveMacros: ['FOO', 'BAR']"))); 21002 std::vector<std::string> NonDefaultWhiteSpaceMacros{"FOO", "BAR"}; 21003 21004 const auto SubSubStyle = [&NonDefaultWhiteSpaceMacros] { 21005 auto Style = getGoogleStyle(); 21006 Style.ColumnLimit = 20; 21007 Style.UseTab = FormatStyle::UT_Always; 21008 Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros; 21009 return Style; 21010 }(); 21011 21012 ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros); 21013 Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS); 21014 ASSERT_TRUE(static_cast<bool>(Style9)); 21015 ASSERT_EQ(*Style9, SubSubStyle); 21016 21017 // Test 9.5: use InheritParentConfig as style name 21018 Style9 = 21019 getStyle("inheritparentconfig", "/e/sub/sub/code.cpp", "none", "", &FS); 21020 ASSERT_TRUE(static_cast<bool>(Style9)); 21021 ASSERT_EQ(*Style9, SubSubStyle); 21022 21023 // Test 9.6: use command line style with inheritance 21024 Style9 = getStyle("{BasedOnStyle: InheritParentConfig}", "/e/sub/code.cpp", 21025 "none", "", &FS); 21026 ASSERT_TRUE(static_cast<bool>(Style9)); 21027 ASSERT_EQ(*Style9, SubSubStyle); 21028 21029 // Test 9.7: use command line style with inheritance and own config 21030 Style9 = getStyle("{BasedOnStyle: InheritParentConfig, " 21031 "WhitespaceSensitiveMacros: ['FOO', 'BAR']}", 21032 "/e/sub/code.cpp", "none", "", &FS); 21033 ASSERT_TRUE(static_cast<bool>(Style9)); 21034 ASSERT_EQ(*Style9, SubSubStyle); 21035 21036 // Test 9.8: use inheritance from a file without BasedOnStyle 21037 ASSERT_TRUE(FS.addFile("/e/withoutbase/.clang-format", 0, 21038 llvm::MemoryBuffer::getMemBuffer("ColumnLimit: 123"))); 21039 ASSERT_TRUE( 21040 FS.addFile("/e/withoutbase/sub/.clang-format", 0, 21041 llvm::MemoryBuffer::getMemBuffer( 21042 "BasedOnStyle: InheritParentConfig\nIndentWidth: 7"))); 21043 // Make sure we do not use the fallback style 21044 Style9 = getStyle("file", "/e/withoutbase/code.cpp", "google", "", &FS); 21045 ASSERT_TRUE(static_cast<bool>(Style9)); 21046 ASSERT_EQ(*Style9, [] { 21047 auto Style = getLLVMStyle(); 21048 Style.ColumnLimit = 123; 21049 return Style; 21050 }()); 21051 21052 Style9 = getStyle("file", "/e/withoutbase/sub/code.cpp", "google", "", &FS); 21053 ASSERT_TRUE(static_cast<bool>(Style9)); 21054 ASSERT_EQ(*Style9, [] { 21055 auto Style = getLLVMStyle(); 21056 Style.ColumnLimit = 123; 21057 Style.IndentWidth = 7; 21058 return Style; 21059 }()); 21060 } 21061 21062 TEST_F(ReplacementTest, FormatCodeAfterReplacements) { 21063 // Column limit is 20. 21064 std::string Code = "Type *a =\n" 21065 " new Type();\n" 21066 "g(iiiii, 0, jjjjj,\n" 21067 " 0, kkkkk, 0, mm);\n" 21068 "int bad = format ;"; 21069 std::string Expected = "auto a = new Type();\n" 21070 "g(iiiii, nullptr,\n" 21071 " jjjjj, nullptr,\n" 21072 " kkkkk, nullptr,\n" 21073 " mm);\n" 21074 "int bad = format ;"; 21075 FileID ID = Context.createInMemoryFile("format.cpp", Code); 21076 tooling::Replacements Replaces = toReplacements( 21077 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6, 21078 "auto "), 21079 tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1, 21080 "nullptr"), 21081 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1, 21082 "nullptr"), 21083 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1, 21084 "nullptr")}); 21085 21086 format::FormatStyle Style = format::getLLVMStyle(); 21087 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. 21088 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 21089 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 21090 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 21091 auto Result = applyAllReplacements(Code, *FormattedReplaces); 21092 EXPECT_TRUE(static_cast<bool>(Result)); 21093 EXPECT_EQ(Expected, *Result); 21094 } 21095 21096 TEST_F(ReplacementTest, SortIncludesAfterReplacement) { 21097 std::string Code = "#include \"a.h\"\n" 21098 "#include \"c.h\"\n" 21099 "\n" 21100 "int main() {\n" 21101 " return 0;\n" 21102 "}"; 21103 std::string Expected = "#include \"a.h\"\n" 21104 "#include \"b.h\"\n" 21105 "#include \"c.h\"\n" 21106 "\n" 21107 "int main() {\n" 21108 " return 0;\n" 21109 "}"; 21110 FileID ID = Context.createInMemoryFile("fix.cpp", Code); 21111 tooling::Replacements Replaces = toReplacements( 21112 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0, 21113 "#include \"b.h\"\n")}); 21114 21115 format::FormatStyle Style = format::getLLVMStyle(); 21116 Style.SortIncludes = FormatStyle::SI_CaseSensitive; 21117 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 21118 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 21119 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 21120 auto Result = applyAllReplacements(Code, *FormattedReplaces); 21121 EXPECT_TRUE(static_cast<bool>(Result)); 21122 EXPECT_EQ(Expected, *Result); 21123 } 21124 21125 TEST_F(FormatTest, FormatSortsUsingDeclarations) { 21126 EXPECT_EQ("using std::cin;\n" 21127 "using std::cout;", 21128 format("using std::cout;\n" 21129 "using std::cin;", 21130 getGoogleStyle())); 21131 } 21132 21133 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) { 21134 format::FormatStyle Style = format::getLLVMStyle(); 21135 Style.Standard = FormatStyle::LS_Cpp03; 21136 // cpp03 recognize this string as identifier u8 and literal character 'a' 21137 EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style)); 21138 } 21139 21140 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) { 21141 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers 21142 // all modes, including C++11, C++14 and C++17 21143 EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';")); 21144 } 21145 21146 TEST_F(FormatTest, DoNotFormatLikelyXml) { 21147 EXPECT_EQ("<!-- ;> -->", format("<!-- ;> -->", getGoogleStyle())); 21148 EXPECT_EQ(" <!-- >; -->", format(" <!-- >; -->", getGoogleStyle())); 21149 } 21150 21151 TEST_F(FormatTest, StructuredBindings) { 21152 // Structured bindings is a C++17 feature. 21153 // all modes, including C++11, C++14 and C++17 21154 verifyFormat("auto [a, b] = f();"); 21155 EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();")); 21156 EXPECT_EQ("const auto [a, b] = f();", format("const auto[a, b] = f();")); 21157 EXPECT_EQ("auto const [a, b] = f();", format("auto const[a, b] = f();")); 21158 EXPECT_EQ("auto const volatile [a, b] = f();", 21159 format("auto const volatile[a, b] = f();")); 21160 EXPECT_EQ("auto [a, b, c] = f();", format("auto [ a , b,c ] = f();")); 21161 EXPECT_EQ("auto &[a, b, c] = f();", 21162 format("auto &[ a , b,c ] = f();")); 21163 EXPECT_EQ("auto &&[a, b, c] = f();", 21164 format("auto &&[ a , b,c ] = f();")); 21165 EXPECT_EQ("auto const &[a, b] = f();", format("auto const&[a, b] = f();")); 21166 EXPECT_EQ("auto const volatile &&[a, b] = f();", 21167 format("auto const volatile &&[a, b] = f();")); 21168 EXPECT_EQ("auto const &&[a, b] = f();", 21169 format("auto const && [a, b] = f();")); 21170 EXPECT_EQ("const auto &[a, b] = f();", 21171 format("const auto & [a, b] = f();")); 21172 EXPECT_EQ("const auto volatile &&[a, b] = f();", 21173 format("const auto volatile &&[a, b] = f();")); 21174 EXPECT_EQ("volatile const auto &&[a, b] = f();", 21175 format("volatile const auto &&[a, b] = f();")); 21176 EXPECT_EQ("const auto &&[a, b] = f();", 21177 format("const auto && [a, b] = f();")); 21178 21179 // Make sure we don't mistake structured bindings for lambdas. 21180 FormatStyle PointerMiddle = getLLVMStyle(); 21181 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 21182 verifyFormat("auto [a1, b]{A * i};", getGoogleStyle()); 21183 verifyFormat("auto [a2, b]{A * i};", getLLVMStyle()); 21184 verifyFormat("auto [a3, b]{A * i};", PointerMiddle); 21185 verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle()); 21186 verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle()); 21187 verifyFormat("auto const [a3, b]{A * i};", PointerMiddle); 21188 verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle()); 21189 verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle()); 21190 verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle); 21191 verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle()); 21192 verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle()); 21193 verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle); 21194 21195 EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}", 21196 format("for (const auto && [a, b] : some_range) {\n}")); 21197 EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}", 21198 format("for (const auto & [a, b] : some_range) {\n}")); 21199 EXPECT_EQ("for (const auto [a, b] : some_range) {\n}", 21200 format("for (const auto[a, b] : some_range) {\n}")); 21201 EXPECT_EQ("auto [x, y](expr);", format("auto[x,y] (expr);")); 21202 EXPECT_EQ("auto &[x, y](expr);", format("auto & [x,y] (expr);")); 21203 EXPECT_EQ("auto &&[x, y](expr);", format("auto && [x,y] (expr);")); 21204 EXPECT_EQ("auto const &[x, y](expr);", 21205 format("auto const & [x,y] (expr);")); 21206 EXPECT_EQ("auto const &&[x, y](expr);", 21207 format("auto const && [x,y] (expr);")); 21208 EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y] {expr};")); 21209 EXPECT_EQ("auto const &[x, y]{expr};", 21210 format("auto const & [x,y] {expr};")); 21211 EXPECT_EQ("auto const &&[x, y]{expr};", 21212 format("auto const && [x,y] {expr};")); 21213 21214 format::FormatStyle Spaces = format::getLLVMStyle(); 21215 Spaces.SpacesInSquareBrackets = true; 21216 verifyFormat("auto [ a, b ] = f();", Spaces); 21217 verifyFormat("auto &&[ a, b ] = f();", Spaces); 21218 verifyFormat("auto &[ a, b ] = f();", Spaces); 21219 verifyFormat("auto const &&[ a, b ] = f();", Spaces); 21220 verifyFormat("auto const &[ a, b ] = f();", Spaces); 21221 } 21222 21223 TEST_F(FormatTest, FileAndCode) { 21224 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", "")); 21225 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", "")); 21226 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", "")); 21227 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "")); 21228 EXPECT_EQ(FormatStyle::LK_ObjC, 21229 guessLanguage("foo.h", "@interface Foo\n@end\n")); 21230 EXPECT_EQ( 21231 FormatStyle::LK_ObjC, 21232 guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }")); 21233 EXPECT_EQ(FormatStyle::LK_ObjC, 21234 guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))")); 21235 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;")); 21236 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", "")); 21237 EXPECT_EQ(FormatStyle::LK_ObjC, 21238 guessLanguage("foo", "@interface Foo\n@end\n")); 21239 EXPECT_EQ(FormatStyle::LK_ObjC, 21240 guessLanguage("foo.h", "int DoStuff(CGRect rect);\n")); 21241 EXPECT_EQ( 21242 FormatStyle::LK_ObjC, 21243 guessLanguage("foo.h", 21244 "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n")); 21245 EXPECT_EQ( 21246 FormatStyle::LK_Cpp, 21247 guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;")); 21248 } 21249 21250 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) { 21251 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];")); 21252 EXPECT_EQ(FormatStyle::LK_ObjC, 21253 guessLanguage("foo.h", "array[[calculator getIndex]];")); 21254 EXPECT_EQ(FormatStyle::LK_Cpp, 21255 guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];")); 21256 EXPECT_EQ( 21257 FormatStyle::LK_Cpp, 21258 guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];")); 21259 EXPECT_EQ(FormatStyle::LK_ObjC, 21260 guessLanguage("foo.h", "[[noreturn foo] bar];")); 21261 EXPECT_EQ(FormatStyle::LK_Cpp, 21262 guessLanguage("foo.h", "[[clang::fallthrough]];")); 21263 EXPECT_EQ(FormatStyle::LK_ObjC, 21264 guessLanguage("foo.h", "[[clang:fallthrough] foo];")); 21265 EXPECT_EQ(FormatStyle::LK_Cpp, 21266 guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];")); 21267 EXPECT_EQ(FormatStyle::LK_Cpp, 21268 guessLanguage("foo.h", "[[using clang: fallthrough]];")); 21269 EXPECT_EQ(FormatStyle::LK_ObjC, 21270 guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];")); 21271 EXPECT_EQ(FormatStyle::LK_Cpp, 21272 guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];")); 21273 EXPECT_EQ( 21274 FormatStyle::LK_Cpp, 21275 guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)")); 21276 EXPECT_EQ( 21277 FormatStyle::LK_Cpp, 21278 guessLanguage("foo.h", 21279 "[[clang::callable_when(\"unconsumed\", \"unknown\")]]")); 21280 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]")); 21281 } 21282 21283 TEST_F(FormatTest, GuessLanguageWithCaret) { 21284 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);")); 21285 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);")); 21286 EXPECT_EQ(FormatStyle::LK_ObjC, 21287 guessLanguage("foo.h", "int(^)(char, float);")); 21288 EXPECT_EQ(FormatStyle::LK_ObjC, 21289 guessLanguage("foo.h", "int(^foo)(char, float);")); 21290 EXPECT_EQ(FormatStyle::LK_ObjC, 21291 guessLanguage("foo.h", "int(^foo[10])(char, float);")); 21292 EXPECT_EQ(FormatStyle::LK_ObjC, 21293 guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);")); 21294 EXPECT_EQ( 21295 FormatStyle::LK_ObjC, 21296 guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);")); 21297 } 21298 21299 TEST_F(FormatTest, GuessLanguageWithPragmas) { 21300 EXPECT_EQ(FormatStyle::LK_Cpp, 21301 guessLanguage("foo.h", "__pragma(warning(disable:))")); 21302 EXPECT_EQ(FormatStyle::LK_Cpp, 21303 guessLanguage("foo.h", "#pragma(warning(disable:))")); 21304 EXPECT_EQ(FormatStyle::LK_Cpp, 21305 guessLanguage("foo.h", "_Pragma(warning(disable:))")); 21306 } 21307 21308 TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) { 21309 // ASM symbolic names are identifiers that must be surrounded by [] without 21310 // space in between: 21311 // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands 21312 21313 // Example from https://bugs.llvm.org/show_bug.cgi?id=45108. 21314 verifyFormat(R"(// 21315 asm volatile("mrs %x[result], FPCR" : [result] "=r"(result)); 21316 )"); 21317 21318 // A list of several ASM symbolic names. 21319 verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)"); 21320 21321 // ASM symbolic names in inline ASM with inputs and outputs. 21322 verifyFormat(R"(// 21323 asm("cmoveq %1, %2, %[result]" 21324 : [result] "=r"(result) 21325 : "r"(test), "r"(new), "[result]"(old)); 21326 )"); 21327 21328 // ASM symbolic names in inline ASM with no outputs. 21329 verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)"); 21330 } 21331 21332 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) { 21333 EXPECT_EQ(FormatStyle::LK_Cpp, 21334 guessLanguage("foo.h", "void f() {\n" 21335 " asm (\"mov %[e], %[d]\"\n" 21336 " : [d] \"=rm\" (d)\n" 21337 " [e] \"rm\" (*e));\n" 21338 "}")); 21339 EXPECT_EQ(FormatStyle::LK_Cpp, 21340 guessLanguage("foo.h", "void f() {\n" 21341 " _asm (\"mov %[e], %[d]\"\n" 21342 " : [d] \"=rm\" (d)\n" 21343 " [e] \"rm\" (*e));\n" 21344 "}")); 21345 EXPECT_EQ(FormatStyle::LK_Cpp, 21346 guessLanguage("foo.h", "void f() {\n" 21347 " __asm (\"mov %[e], %[d]\"\n" 21348 " : [d] \"=rm\" (d)\n" 21349 " [e] \"rm\" (*e));\n" 21350 "}")); 21351 EXPECT_EQ(FormatStyle::LK_Cpp, 21352 guessLanguage("foo.h", "void f() {\n" 21353 " __asm__ (\"mov %[e], %[d]\"\n" 21354 " : [d] \"=rm\" (d)\n" 21355 " [e] \"rm\" (*e));\n" 21356 "}")); 21357 EXPECT_EQ(FormatStyle::LK_Cpp, 21358 guessLanguage("foo.h", "void f() {\n" 21359 " asm (\"mov %[e], %[d]\"\n" 21360 " : [d] \"=rm\" (d),\n" 21361 " [e] \"rm\" (*e));\n" 21362 "}")); 21363 EXPECT_EQ(FormatStyle::LK_Cpp, 21364 guessLanguage("foo.h", "void f() {\n" 21365 " asm volatile (\"mov %[e], %[d]\"\n" 21366 " : [d] \"=rm\" (d)\n" 21367 " [e] \"rm\" (*e));\n" 21368 "}")); 21369 } 21370 21371 TEST_F(FormatTest, GuessLanguageWithChildLines) { 21372 EXPECT_EQ(FormatStyle::LK_Cpp, 21373 guessLanguage("foo.h", "#define FOO ({ std::string s; })")); 21374 EXPECT_EQ(FormatStyle::LK_ObjC, 21375 guessLanguage("foo.h", "#define FOO ({ NSString *s; })")); 21376 EXPECT_EQ( 21377 FormatStyle::LK_Cpp, 21378 guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })")); 21379 EXPECT_EQ( 21380 FormatStyle::LK_ObjC, 21381 guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })")); 21382 } 21383 21384 TEST_F(FormatTest, TypenameMacros) { 21385 std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"}; 21386 21387 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353 21388 FormatStyle Google = getGoogleStyleWithColumns(0); 21389 Google.TypenameMacros = TypenameMacros; 21390 verifyFormat("struct foo {\n" 21391 " int bar;\n" 21392 " TAILQ_ENTRY(a) bleh;\n" 21393 "};", 21394 Google); 21395 21396 FormatStyle Macros = getLLVMStyle(); 21397 Macros.TypenameMacros = TypenameMacros; 21398 21399 verifyFormat("STACK_OF(int) a;", Macros); 21400 verifyFormat("STACK_OF(int) *a;", Macros); 21401 verifyFormat("STACK_OF(int const *) *a;", Macros); 21402 verifyFormat("STACK_OF(int *const) *a;", Macros); 21403 verifyFormat("STACK_OF(int, string) a;", Macros); 21404 verifyFormat("STACK_OF(LIST(int)) a;", Macros); 21405 verifyFormat("STACK_OF(LIST(int)) a, b;", Macros); 21406 verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros); 21407 verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros); 21408 verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros); 21409 verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros); 21410 21411 Macros.PointerAlignment = FormatStyle::PAS_Left; 21412 verifyFormat("STACK_OF(int)* a;", Macros); 21413 verifyFormat("STACK_OF(int*)* a;", Macros); 21414 verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros); 21415 verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros); 21416 verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros); 21417 } 21418 21419 TEST_F(FormatTest, AtomicQualifier) { 21420 // Check that we treate _Atomic as a type and not a function call 21421 FormatStyle Google = getGoogleStyleWithColumns(0); 21422 verifyFormat("struct foo {\n" 21423 " int a1;\n" 21424 " _Atomic(a) a2;\n" 21425 " _Atomic(_Atomic(int) *const) a3;\n" 21426 "};", 21427 Google); 21428 verifyFormat("_Atomic(uint64_t) a;"); 21429 verifyFormat("_Atomic(uint64_t) *a;"); 21430 verifyFormat("_Atomic(uint64_t const *) *a;"); 21431 verifyFormat("_Atomic(uint64_t *const) *a;"); 21432 verifyFormat("_Atomic(const uint64_t *) *a;"); 21433 verifyFormat("_Atomic(uint64_t) a;"); 21434 verifyFormat("_Atomic(_Atomic(uint64_t)) a;"); 21435 verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;"); 21436 verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}"); 21437 verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);"); 21438 21439 verifyFormat("_Atomic(uint64_t) *s(InitValue);"); 21440 verifyFormat("_Atomic(uint64_t) *s{InitValue};"); 21441 FormatStyle Style = getLLVMStyle(); 21442 Style.PointerAlignment = FormatStyle::PAS_Left; 21443 verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style); 21444 verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style); 21445 verifyFormat("_Atomic(int)* a;", Style); 21446 verifyFormat("_Atomic(int*)* a;", Style); 21447 verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style); 21448 21449 Style.SpacesInCStyleCastParentheses = true; 21450 Style.SpacesInParentheses = false; 21451 verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style); 21452 Style.SpacesInCStyleCastParentheses = false; 21453 Style.SpacesInParentheses = true; 21454 verifyFormat("x = (_Atomic( uint64_t ))*a;", Style); 21455 verifyFormat("x = (_Atomic( uint64_t ))&a;", Style); 21456 } 21457 21458 TEST_F(FormatTest, AmbersandInLamda) { 21459 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899 21460 FormatStyle AlignStyle = getLLVMStyle(); 21461 AlignStyle.PointerAlignment = FormatStyle::PAS_Left; 21462 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle); 21463 AlignStyle.PointerAlignment = FormatStyle::PAS_Right; 21464 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle); 21465 } 21466 21467 TEST_F(FormatTest, SpacesInConditionalStatement) { 21468 FormatStyle Spaces = getLLVMStyle(); 21469 Spaces.IfMacros.clear(); 21470 Spaces.IfMacros.push_back("MYIF"); 21471 Spaces.SpacesInConditionalStatement = true; 21472 verifyFormat("for ( int i = 0; i; i++ )\n continue;", Spaces); 21473 verifyFormat("if ( !a )\n return;", Spaces); 21474 verifyFormat("if ( a )\n return;", Spaces); 21475 verifyFormat("if constexpr ( a )\n return;", Spaces); 21476 verifyFormat("MYIF ( a )\n return;", Spaces); 21477 verifyFormat("MYIF ( a )\n return;\nelse MYIF ( b )\n return;", Spaces); 21478 verifyFormat("MYIF ( a )\n return;\nelse\n return;", Spaces); 21479 verifyFormat("switch ( a )\ncase 1:\n return;", Spaces); 21480 verifyFormat("while ( a )\n return;", Spaces); 21481 verifyFormat("while ( (a && b) )\n return;", Spaces); 21482 verifyFormat("do {\n} while ( 1 != 0 );", Spaces); 21483 verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces); 21484 // Check that space on the left of "::" is inserted as expected at beginning 21485 // of condition. 21486 verifyFormat("while ( ::func() )\n return;", Spaces); 21487 21488 // Check impact of ControlStatementsExceptControlMacros is honored. 21489 Spaces.SpaceBeforeParens = 21490 FormatStyle::SBPO_ControlStatementsExceptControlMacros; 21491 verifyFormat("MYIF( a )\n return;", Spaces); 21492 verifyFormat("MYIF( a )\n return;\nelse MYIF( b )\n return;", Spaces); 21493 verifyFormat("MYIF( a )\n return;\nelse\n return;", Spaces); 21494 } 21495 21496 TEST_F(FormatTest, AlternativeOperators) { 21497 // Test case for ensuring alternate operators are not 21498 // combined with their right most neighbour. 21499 verifyFormat("int a and b;"); 21500 verifyFormat("int a and_eq b;"); 21501 verifyFormat("int a bitand b;"); 21502 verifyFormat("int a bitor b;"); 21503 verifyFormat("int a compl b;"); 21504 verifyFormat("int a not b;"); 21505 verifyFormat("int a not_eq b;"); 21506 verifyFormat("int a or b;"); 21507 verifyFormat("int a xor b;"); 21508 verifyFormat("int a xor_eq b;"); 21509 verifyFormat("return this not_eq bitand other;"); 21510 verifyFormat("bool operator not_eq(const X bitand other)"); 21511 21512 verifyFormat("int a and 5;"); 21513 verifyFormat("int a and_eq 5;"); 21514 verifyFormat("int a bitand 5;"); 21515 verifyFormat("int a bitor 5;"); 21516 verifyFormat("int a compl 5;"); 21517 verifyFormat("int a not 5;"); 21518 verifyFormat("int a not_eq 5;"); 21519 verifyFormat("int a or 5;"); 21520 verifyFormat("int a xor 5;"); 21521 verifyFormat("int a xor_eq 5;"); 21522 21523 verifyFormat("int a compl(5);"); 21524 verifyFormat("int a not(5);"); 21525 21526 /* FIXME handle alternate tokens 21527 * https://en.cppreference.com/w/cpp/language/operator_alternative 21528 // alternative tokens 21529 verifyFormat("compl foo();"); // ~foo(); 21530 verifyFormat("foo() <%%>;"); // foo(); 21531 verifyFormat("void foo() <%%>;"); // void foo(){} 21532 verifyFormat("int a <:1:>;"); // int a[1];[ 21533 verifyFormat("%:define ABC abc"); // #define ABC abc 21534 verifyFormat("%:%:"); // ## 21535 */ 21536 } 21537 21538 TEST_F(FormatTest, STLWhileNotDefineChed) { 21539 verifyFormat("#if defined(while)\n" 21540 "#define while EMIT WARNING C4005\n" 21541 "#endif // while"); 21542 } 21543 21544 TEST_F(FormatTest, OperatorSpacing) { 21545 FormatStyle Style = getLLVMStyle(); 21546 Style.PointerAlignment = FormatStyle::PAS_Right; 21547 verifyFormat("Foo::operator*();", Style); 21548 verifyFormat("Foo::operator void *();", Style); 21549 verifyFormat("Foo::operator void **();", Style); 21550 verifyFormat("Foo::operator void *&();", Style); 21551 verifyFormat("Foo::operator void *&&();", Style); 21552 verifyFormat("Foo::operator void const *();", Style); 21553 verifyFormat("Foo::operator void const **();", Style); 21554 verifyFormat("Foo::operator void const *&();", Style); 21555 verifyFormat("Foo::operator void const *&&();", Style); 21556 verifyFormat("Foo::operator()(void *);", Style); 21557 verifyFormat("Foo::operator*(void *);", Style); 21558 verifyFormat("Foo::operator*();", Style); 21559 verifyFormat("Foo::operator**();", Style); 21560 verifyFormat("Foo::operator&();", Style); 21561 verifyFormat("Foo::operator<int> *();", Style); 21562 verifyFormat("Foo::operator<Foo> *();", Style); 21563 verifyFormat("Foo::operator<int> **();", Style); 21564 verifyFormat("Foo::operator<Foo> **();", Style); 21565 verifyFormat("Foo::operator<int> &();", Style); 21566 verifyFormat("Foo::operator<Foo> &();", Style); 21567 verifyFormat("Foo::operator<int> &&();", Style); 21568 verifyFormat("Foo::operator<Foo> &&();", Style); 21569 verifyFormat("Foo::operator<int> *&();", Style); 21570 verifyFormat("Foo::operator<Foo> *&();", Style); 21571 verifyFormat("Foo::operator<int> *&&();", Style); 21572 verifyFormat("Foo::operator<Foo> *&&();", Style); 21573 verifyFormat("operator*(int (*)(), class Foo);", Style); 21574 21575 verifyFormat("Foo::operator&();", Style); 21576 verifyFormat("Foo::operator void &();", Style); 21577 verifyFormat("Foo::operator void const &();", Style); 21578 verifyFormat("Foo::operator()(void &);", Style); 21579 verifyFormat("Foo::operator&(void &);", Style); 21580 verifyFormat("Foo::operator&();", Style); 21581 verifyFormat("operator&(int (&)(), class Foo);", Style); 21582 21583 verifyFormat("Foo::operator&&();", Style); 21584 verifyFormat("Foo::operator**();", Style); 21585 verifyFormat("Foo::operator void &&();", Style); 21586 verifyFormat("Foo::operator void const &&();", Style); 21587 verifyFormat("Foo::operator()(void &&);", Style); 21588 verifyFormat("Foo::operator&&(void &&);", Style); 21589 verifyFormat("Foo::operator&&();", Style); 21590 verifyFormat("operator&&(int(&&)(), class Foo);", Style); 21591 verifyFormat("operator const nsTArrayRight<E> &()", Style); 21592 verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()", 21593 Style); 21594 verifyFormat("operator void **()", Style); 21595 verifyFormat("operator const FooRight<Object> &()", Style); 21596 verifyFormat("operator const FooRight<Object> *()", Style); 21597 verifyFormat("operator const FooRight<Object> **()", Style); 21598 verifyFormat("operator const FooRight<Object> *&()", Style); 21599 verifyFormat("operator const FooRight<Object> *&&()", Style); 21600 21601 Style.PointerAlignment = FormatStyle::PAS_Left; 21602 verifyFormat("Foo::operator*();", Style); 21603 verifyFormat("Foo::operator**();", Style); 21604 verifyFormat("Foo::operator void*();", Style); 21605 verifyFormat("Foo::operator void**();", Style); 21606 verifyFormat("Foo::operator void*&();", Style); 21607 verifyFormat("Foo::operator void*&&();", Style); 21608 verifyFormat("Foo::operator void const*();", Style); 21609 verifyFormat("Foo::operator void const**();", Style); 21610 verifyFormat("Foo::operator void const*&();", Style); 21611 verifyFormat("Foo::operator void const*&&();", Style); 21612 verifyFormat("Foo::operator/*comment*/ void*();", Style); 21613 verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style); 21614 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style); 21615 verifyFormat("Foo::operator()(void*);", Style); 21616 verifyFormat("Foo::operator*(void*);", Style); 21617 verifyFormat("Foo::operator*();", Style); 21618 verifyFormat("Foo::operator<int>*();", Style); 21619 verifyFormat("Foo::operator<Foo>*();", Style); 21620 verifyFormat("Foo::operator<int>**();", Style); 21621 verifyFormat("Foo::operator<Foo>**();", Style); 21622 verifyFormat("Foo::operator<Foo>*&();", Style); 21623 verifyFormat("Foo::operator<int>&();", Style); 21624 verifyFormat("Foo::operator<Foo>&();", Style); 21625 verifyFormat("Foo::operator<int>&&();", Style); 21626 verifyFormat("Foo::operator<Foo>&&();", Style); 21627 verifyFormat("Foo::operator<int>*&();", Style); 21628 verifyFormat("Foo::operator<Foo>*&();", Style); 21629 verifyFormat("operator*(int (*)(), class Foo);", Style); 21630 21631 verifyFormat("Foo::operator&();", Style); 21632 verifyFormat("Foo::operator void&();", Style); 21633 verifyFormat("Foo::operator void const&();", Style); 21634 verifyFormat("Foo::operator/*comment*/ void&();", Style); 21635 verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style); 21636 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style); 21637 verifyFormat("Foo::operator()(void&);", Style); 21638 verifyFormat("Foo::operator&(void&);", Style); 21639 verifyFormat("Foo::operator&();", Style); 21640 verifyFormat("operator&(int (&)(), class Foo);", Style); 21641 21642 verifyFormat("Foo::operator&&();", Style); 21643 verifyFormat("Foo::operator void&&();", Style); 21644 verifyFormat("Foo::operator void const&&();", Style); 21645 verifyFormat("Foo::operator/*comment*/ void&&();", Style); 21646 verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style); 21647 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style); 21648 verifyFormat("Foo::operator()(void&&);", Style); 21649 verifyFormat("Foo::operator&&(void&&);", Style); 21650 verifyFormat("Foo::operator&&();", Style); 21651 verifyFormat("operator&&(int(&&)(), class Foo);", Style); 21652 verifyFormat("operator const nsTArrayLeft<E>&()", Style); 21653 verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()", 21654 Style); 21655 verifyFormat("operator void**()", Style); 21656 verifyFormat("operator const FooLeft<Object>&()", Style); 21657 verifyFormat("operator const FooLeft<Object>*()", Style); 21658 verifyFormat("operator const FooLeft<Object>**()", Style); 21659 verifyFormat("operator const FooLeft<Object>*&()", Style); 21660 verifyFormat("operator const FooLeft<Object>*&&()", Style); 21661 21662 // PR45107 21663 verifyFormat("operator Vector<String>&();", Style); 21664 verifyFormat("operator const Vector<String>&();", Style); 21665 verifyFormat("operator foo::Bar*();", Style); 21666 verifyFormat("operator const Foo<X>::Bar<Y>*();", Style); 21667 verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();", 21668 Style); 21669 21670 Style.PointerAlignment = FormatStyle::PAS_Middle; 21671 verifyFormat("Foo::operator*();", Style); 21672 verifyFormat("Foo::operator void *();", Style); 21673 verifyFormat("Foo::operator()(void *);", Style); 21674 verifyFormat("Foo::operator*(void *);", Style); 21675 verifyFormat("Foo::operator*();", Style); 21676 verifyFormat("operator*(int (*)(), class Foo);", Style); 21677 21678 verifyFormat("Foo::operator&();", Style); 21679 verifyFormat("Foo::operator void &();", Style); 21680 verifyFormat("Foo::operator void const &();", Style); 21681 verifyFormat("Foo::operator()(void &);", Style); 21682 verifyFormat("Foo::operator&(void &);", Style); 21683 verifyFormat("Foo::operator&();", Style); 21684 verifyFormat("operator&(int (&)(), class Foo);", Style); 21685 21686 verifyFormat("Foo::operator&&();", Style); 21687 verifyFormat("Foo::operator void &&();", Style); 21688 verifyFormat("Foo::operator void const &&();", Style); 21689 verifyFormat("Foo::operator()(void &&);", Style); 21690 verifyFormat("Foo::operator&&(void &&);", Style); 21691 verifyFormat("Foo::operator&&();", Style); 21692 verifyFormat("operator&&(int(&&)(), class Foo);", Style); 21693 } 21694 21695 TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) { 21696 FormatStyle Style = getLLVMStyle(); 21697 // PR46157 21698 verifyFormat("foo(operator+, -42);", Style); 21699 verifyFormat("foo(operator++, -42);", Style); 21700 verifyFormat("foo(operator--, -42);", Style); 21701 verifyFormat("foo(-42, operator--);", Style); 21702 verifyFormat("foo(-42, operator, );", Style); 21703 verifyFormat("foo(operator, , -42);", Style); 21704 } 21705 21706 TEST_F(FormatTest, WhitespaceSensitiveMacros) { 21707 FormatStyle Style = getLLVMStyle(); 21708 Style.WhitespaceSensitiveMacros.push_back("FOO"); 21709 21710 // Don't use the helpers here, since 'mess up' will change the whitespace 21711 // and these are all whitespace sensitive by definition 21712 EXPECT_EQ("FOO(String-ized&Messy+But(: :Still)=Intentional);", 21713 format("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style)); 21714 EXPECT_EQ( 21715 "FOO(String-ized&Messy+But\\(: :Still)=Intentional);", 21716 format("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style)); 21717 EXPECT_EQ("FOO(String-ized&Messy+But,: :Still=Intentional);", 21718 format("FOO(String-ized&Messy+But,: :Still=Intentional);", Style)); 21719 EXPECT_EQ("FOO(String-ized&Messy+But,: :\n" 21720 " Still=Intentional);", 21721 format("FOO(String-ized&Messy+But,: :\n" 21722 " Still=Intentional);", 21723 Style)); 21724 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 21725 EXPECT_EQ("FOO(String-ized=&Messy+But,: :\n" 21726 " Still=Intentional);", 21727 format("FOO(String-ized=&Messy+But,: :\n" 21728 " Still=Intentional);", 21729 Style)); 21730 21731 Style.ColumnLimit = 21; 21732 EXPECT_EQ("FOO(String-ized&Messy+But: :Still=Intentional);", 21733 format("FOO(String-ized&Messy+But: :Still=Intentional);", Style)); 21734 } 21735 21736 TEST_F(FormatTest, VeryLongNamespaceCommentSplit) { 21737 // These tests are not in NamespaceFixer because that doesn't 21738 // test its interaction with line wrapping 21739 FormatStyle Style = getLLVMStyle(); 21740 Style.ColumnLimit = 80; 21741 verifyFormat("namespace {\n" 21742 "int i;\n" 21743 "int j;\n" 21744 "} // namespace", 21745 Style); 21746 21747 verifyFormat("namespace AAA {\n" 21748 "int i;\n" 21749 "int j;\n" 21750 "} // namespace AAA", 21751 Style); 21752 21753 EXPECT_EQ("namespace Averyveryveryverylongnamespace {\n" 21754 "int i;\n" 21755 "int j;\n" 21756 "} // namespace Averyveryveryverylongnamespace", 21757 format("namespace Averyveryveryverylongnamespace {\n" 21758 "int i;\n" 21759 "int j;\n" 21760 "}", 21761 Style)); 21762 21763 EXPECT_EQ( 21764 "namespace " 21765 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" 21766 " went::mad::now {\n" 21767 "int i;\n" 21768 "int j;\n" 21769 "} // namespace\n" 21770 " // " 21771 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" 21772 "went::mad::now", 21773 format("namespace " 21774 "would::it::save::you::a::lot::of::time::if_::i::" 21775 "just::gave::up::and_::went::mad::now {\n" 21776 "int i;\n" 21777 "int j;\n" 21778 "}", 21779 Style)); 21780 21781 // This used to duplicate the comment again and again on subsequent runs 21782 EXPECT_EQ( 21783 "namespace " 21784 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" 21785 " went::mad::now {\n" 21786 "int i;\n" 21787 "int j;\n" 21788 "} // namespace\n" 21789 " // " 21790 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" 21791 "went::mad::now", 21792 format("namespace " 21793 "would::it::save::you::a::lot::of::time::if_::i::" 21794 "just::gave::up::and_::went::mad::now {\n" 21795 "int i;\n" 21796 "int j;\n" 21797 "} // namespace\n" 21798 " // " 21799 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::" 21800 "and_::went::mad::now", 21801 Style)); 21802 } 21803 21804 TEST_F(FormatTest, LikelyUnlikely) { 21805 FormatStyle Style = getLLVMStyle(); 21806 21807 verifyFormat("if (argc > 5) [[unlikely]] {\n" 21808 " return 29;\n" 21809 "}", 21810 Style); 21811 21812 verifyFormat("if (argc > 5) [[likely]] {\n" 21813 " return 29;\n" 21814 "}", 21815 Style); 21816 21817 verifyFormat("if (argc > 5) [[unlikely]] {\n" 21818 " return 29;\n" 21819 "} else [[likely]] {\n" 21820 " return 42;\n" 21821 "}\n", 21822 Style); 21823 21824 verifyFormat("if (argc > 5) [[unlikely]] {\n" 21825 " return 29;\n" 21826 "} else if (argc > 10) [[likely]] {\n" 21827 " return 99;\n" 21828 "} else {\n" 21829 " return 42;\n" 21830 "}\n", 21831 Style); 21832 21833 verifyFormat("if (argc > 5) [[gnu::unused]] {\n" 21834 " return 29;\n" 21835 "}", 21836 Style); 21837 } 21838 21839 TEST_F(FormatTest, PenaltyIndentedWhitespace) { 21840 verifyFormat("Constructor()\n" 21841 " : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 21842 " aaaa(aaaaaaaaaaaaaaaaaa, " 21843 "aaaaaaaaaaaaaaaaaat))"); 21844 verifyFormat("Constructor()\n" 21845 " : aaaaaaaaaaaaa(aaaaaa), " 21846 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)"); 21847 21848 FormatStyle StyleWithWhitespacePenalty = getLLVMStyle(); 21849 StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5; 21850 verifyFormat("Constructor()\n" 21851 " : aaaaaa(aaaaaa),\n" 21852 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 21853 " aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))", 21854 StyleWithWhitespacePenalty); 21855 verifyFormat("Constructor()\n" 21856 " : aaaaaaaaaaaaa(aaaaaa), " 21857 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)", 21858 StyleWithWhitespacePenalty); 21859 } 21860 21861 TEST_F(FormatTest, LLVMDefaultStyle) { 21862 FormatStyle Style = getLLVMStyle(); 21863 verifyFormat("extern \"C\" {\n" 21864 "int foo();\n" 21865 "}", 21866 Style); 21867 } 21868 TEST_F(FormatTest, GNUDefaultStyle) { 21869 FormatStyle Style = getGNUStyle(); 21870 verifyFormat("extern \"C\"\n" 21871 "{\n" 21872 " int foo ();\n" 21873 "}", 21874 Style); 21875 } 21876 TEST_F(FormatTest, MozillaDefaultStyle) { 21877 FormatStyle Style = getMozillaStyle(); 21878 verifyFormat("extern \"C\"\n" 21879 "{\n" 21880 " int foo();\n" 21881 "}", 21882 Style); 21883 } 21884 TEST_F(FormatTest, GoogleDefaultStyle) { 21885 FormatStyle Style = getGoogleStyle(); 21886 verifyFormat("extern \"C\" {\n" 21887 "int foo();\n" 21888 "}", 21889 Style); 21890 } 21891 TEST_F(FormatTest, ChromiumDefaultStyle) { 21892 FormatStyle Style = getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp); 21893 verifyFormat("extern \"C\" {\n" 21894 "int foo();\n" 21895 "}", 21896 Style); 21897 } 21898 TEST_F(FormatTest, MicrosoftDefaultStyle) { 21899 FormatStyle Style = getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp); 21900 verifyFormat("extern \"C\"\n" 21901 "{\n" 21902 " int foo();\n" 21903 "}", 21904 Style); 21905 } 21906 TEST_F(FormatTest, WebKitDefaultStyle) { 21907 FormatStyle Style = getWebKitStyle(); 21908 verifyFormat("extern \"C\" {\n" 21909 "int foo();\n" 21910 "}", 21911 Style); 21912 } 21913 21914 TEST_F(FormatTest, ConceptsAndRequires) { 21915 FormatStyle Style = getLLVMStyle(); 21916 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 21917 21918 verifyFormat("template <typename T>\n" 21919 "concept Hashable = requires(T a) {\n" 21920 " { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n" 21921 "};", 21922 Style); 21923 verifyFormat("template <typename T>\n" 21924 "concept EqualityComparable = requires(T a, T b) {\n" 21925 " { a == b } -> bool;\n" 21926 "};", 21927 Style); 21928 verifyFormat("template <typename T>\n" 21929 "concept EqualityComparable = requires(T a, T b) {\n" 21930 " { a == b } -> bool;\n" 21931 " { a != b } -> bool;\n" 21932 "};", 21933 Style); 21934 verifyFormat("template <typename T>\n" 21935 "concept EqualityComparable = requires(T a, T b) {\n" 21936 " { a == b } -> bool;\n" 21937 " { a != b } -> bool;\n" 21938 "};", 21939 Style); 21940 21941 verifyFormat("template <typename It>\n" 21942 "requires Iterator<It>\n" 21943 "void sort(It begin, It end) {\n" 21944 " //....\n" 21945 "}", 21946 Style); 21947 21948 verifyFormat("template <typename T>\n" 21949 "concept Large = sizeof(T) > 10;", 21950 Style); 21951 21952 verifyFormat("template <typename T, typename U>\n" 21953 "concept FooableWith = requires(T t, U u) {\n" 21954 " typename T::foo_type;\n" 21955 " { t.foo(u) } -> typename T::foo_type;\n" 21956 " t++;\n" 21957 "};\n" 21958 "void doFoo(FooableWith<int> auto t) {\n" 21959 " t.foo(3);\n" 21960 "}", 21961 Style); 21962 verifyFormat("template <typename T>\n" 21963 "concept Context = sizeof(T) == 1;", 21964 Style); 21965 verifyFormat("template <typename T>\n" 21966 "concept Context = is_specialization_of_v<context, T>;", 21967 Style); 21968 verifyFormat("template <typename T>\n" 21969 "concept Node = std::is_object_v<T>;", 21970 Style); 21971 verifyFormat("template <typename T>\n" 21972 "concept Tree = true;", 21973 Style); 21974 21975 verifyFormat("template <typename T> int g(T i) requires Concept1<I> {\n" 21976 " //...\n" 21977 "}", 21978 Style); 21979 21980 verifyFormat( 21981 "template <typename T> int g(T i) requires Concept1<I> && Concept2<I> {\n" 21982 " //...\n" 21983 "}", 21984 Style); 21985 21986 verifyFormat( 21987 "template <typename T> int g(T i) requires Concept1<I> || Concept2<I> {\n" 21988 " //...\n" 21989 "}", 21990 Style); 21991 21992 verifyFormat("template <typename T>\n" 21993 "veryveryvery_long_return_type g(T i) requires Concept1<I> || " 21994 "Concept2<I> {\n" 21995 " //...\n" 21996 "}", 21997 Style); 21998 21999 verifyFormat("template <typename T>\n" 22000 "veryveryvery_long_return_type g(T i) requires Concept1<I> && " 22001 "Concept2<I> {\n" 22002 " //...\n" 22003 "}", 22004 Style); 22005 22006 verifyFormat( 22007 "template <typename T>\n" 22008 "veryveryvery_long_return_type g(T i) requires Concept1 && Concept2 {\n" 22009 " //...\n" 22010 "}", 22011 Style); 22012 22013 verifyFormat( 22014 "template <typename T>\n" 22015 "veryveryvery_long_return_type g(T i) requires Concept1 || Concept2 {\n" 22016 " //...\n" 22017 "}", 22018 Style); 22019 22020 verifyFormat("template <typename It>\n" 22021 "requires Foo<It>() && Bar<It> {\n" 22022 " //....\n" 22023 "}", 22024 Style); 22025 22026 verifyFormat("template <typename It>\n" 22027 "requires Foo<Bar<It>>() && Bar<Foo<It, It>> {\n" 22028 " //....\n" 22029 "}", 22030 Style); 22031 22032 verifyFormat("template <typename It>\n" 22033 "requires Foo<Bar<It, It>>() && Bar<Foo<It, It>> {\n" 22034 " //....\n" 22035 "}", 22036 Style); 22037 22038 verifyFormat( 22039 "template <typename It>\n" 22040 "requires Foo<Bar<It>, Baz<It>>() && Bar<Foo<It>, Baz<It, It>> {\n" 22041 " //....\n" 22042 "}", 22043 Style); 22044 22045 Style.IndentRequires = true; 22046 verifyFormat("template <typename It>\n" 22047 " requires Iterator<It>\n" 22048 "void sort(It begin, It end) {\n" 22049 " //....\n" 22050 "}", 22051 Style); 22052 verifyFormat("template <std::size index_>\n" 22053 " requires(index_ < sizeof...(Children_))\n" 22054 "Tree auto &child() {\n" 22055 " // ...\n" 22056 "}", 22057 Style); 22058 22059 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 22060 verifyFormat("template <typename T>\n" 22061 "concept Hashable = requires (T a) {\n" 22062 " { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n" 22063 "};", 22064 Style); 22065 22066 verifyFormat("template <class T = void>\n" 22067 " requires EqualityComparable<T> || Same<T, void>\n" 22068 "struct equal_to;", 22069 Style); 22070 22071 verifyFormat("template <class T>\n" 22072 " requires requires {\n" 22073 " T{};\n" 22074 " T (int);\n" 22075 " }\n", 22076 Style); 22077 22078 Style.ColumnLimit = 78; 22079 verifyFormat("template <typename T>\n" 22080 "concept Context = Traits<typename T::traits_type> and\n" 22081 " Interface<typename T::interface_type> and\n" 22082 " Request<typename T::request_type> and\n" 22083 " Response<typename T::response_type> and\n" 22084 " ContextExtension<typename T::extension_type> and\n" 22085 " ::std::is_copy_constructable<T> and " 22086 "::std::is_move_constructable<T> and\n" 22087 " requires (T c) {\n" 22088 " { c.response; } -> Response;\n" 22089 "} and requires (T c) {\n" 22090 " { c.request; } -> Request;\n" 22091 "}\n", 22092 Style); 22093 22094 verifyFormat("template <typename T>\n" 22095 "concept Context = Traits<typename T::traits_type> or\n" 22096 " Interface<typename T::interface_type> or\n" 22097 " Request<typename T::request_type> or\n" 22098 " Response<typename T::response_type> or\n" 22099 " ContextExtension<typename T::extension_type> or\n" 22100 " ::std::is_copy_constructable<T> or " 22101 "::std::is_move_constructable<T> or\n" 22102 " requires (T c) {\n" 22103 " { c.response; } -> Response;\n" 22104 "} or requires (T c) {\n" 22105 " { c.request; } -> Request;\n" 22106 "}\n", 22107 Style); 22108 22109 verifyFormat("template <typename T>\n" 22110 "concept Context = Traits<typename T::traits_type> &&\n" 22111 " Interface<typename T::interface_type> &&\n" 22112 " Request<typename T::request_type> &&\n" 22113 " Response<typename T::response_type> &&\n" 22114 " ContextExtension<typename T::extension_type> &&\n" 22115 " ::std::is_copy_constructable<T> && " 22116 "::std::is_move_constructable<T> &&\n" 22117 " requires (T c) {\n" 22118 " { c.response; } -> Response;\n" 22119 "} && requires (T c) {\n" 22120 " { c.request; } -> Request;\n" 22121 "}\n", 22122 Style); 22123 22124 verifyFormat("template <typename T>\nconcept someConcept = Constraint1<T> && " 22125 "Constraint2<T>;"); 22126 22127 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 22128 Style.BraceWrapping.AfterFunction = true; 22129 Style.BraceWrapping.AfterClass = true; 22130 Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 22131 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 22132 verifyFormat("void Foo () requires (std::copyable<T>)\n" 22133 "{\n" 22134 " return\n" 22135 "}\n", 22136 Style); 22137 22138 verifyFormat("void Foo () requires std::copyable<T>\n" 22139 "{\n" 22140 " return\n" 22141 "}\n", 22142 Style); 22143 22144 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 22145 " requires (std::invocable<F, std::invoke_result_t<Args>...>)\n" 22146 "struct constant;", 22147 Style); 22148 22149 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 22150 " requires std::invocable<F, std::invoke_result_t<Args>...>\n" 22151 "struct constant;", 22152 Style); 22153 22154 verifyFormat("template <class T>\n" 22155 "class plane_with_very_very_very_long_name\n" 22156 "{\n" 22157 " constexpr plane_with_very_very_very_long_name () requires " 22158 "std::copyable<T>\n" 22159 " : plane_with_very_very_very_long_name (1)\n" 22160 " {\n" 22161 " }\n" 22162 "}\n", 22163 Style); 22164 22165 verifyFormat("template <class T>\n" 22166 "class plane_with_long_name\n" 22167 "{\n" 22168 " constexpr plane_with_long_name () requires std::copyable<T>\n" 22169 " : plane_with_long_name (1)\n" 22170 " {\n" 22171 " }\n" 22172 "}\n", 22173 Style); 22174 22175 Style.BreakBeforeConceptDeclarations = false; 22176 verifyFormat("template <typename T> concept Tree = true;", Style); 22177 22178 Style.IndentRequires = false; 22179 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 22180 "requires (std::invocable<F, std::invoke_result_t<Args>...>) " 22181 "struct constant;", 22182 Style); 22183 } 22184 22185 TEST_F(FormatTest, StatementAttributeLikeMacros) { 22186 FormatStyle Style = getLLVMStyle(); 22187 StringRef Source = "void Foo::slot() {\n" 22188 " unsigned char MyChar = 'x';\n" 22189 " emit signal(MyChar);\n" 22190 " Q_EMIT signal(MyChar);\n" 22191 "}"; 22192 22193 EXPECT_EQ(Source, format(Source, Style)); 22194 22195 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 22196 EXPECT_EQ("void Foo::slot() {\n" 22197 " unsigned char MyChar = 'x';\n" 22198 " emit signal(MyChar);\n" 22199 " Q_EMIT signal(MyChar);\n" 22200 "}", 22201 format(Source, Style)); 22202 22203 Style.StatementAttributeLikeMacros.push_back("emit"); 22204 EXPECT_EQ(Source, format(Source, Style)); 22205 22206 Style.StatementAttributeLikeMacros = {}; 22207 EXPECT_EQ("void Foo::slot() {\n" 22208 " unsigned char MyChar = 'x';\n" 22209 " emit signal(MyChar);\n" 22210 " Q_EMIT signal(MyChar);\n" 22211 "}", 22212 format(Source, Style)); 22213 } 22214 22215 TEST_F(FormatTest, IndentAccessModifiers) { 22216 FormatStyle Style = getLLVMStyle(); 22217 Style.IndentAccessModifiers = true; 22218 // Members are *two* levels below the record; 22219 // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation. 22220 verifyFormat("class C {\n" 22221 " int i;\n" 22222 "};\n", 22223 Style); 22224 verifyFormat("union C {\n" 22225 " int i;\n" 22226 " unsigned u;\n" 22227 "};\n", 22228 Style); 22229 // Access modifiers should be indented one level below the record. 22230 verifyFormat("class C {\n" 22231 " public:\n" 22232 " int i;\n" 22233 "};\n", 22234 Style); 22235 verifyFormat("struct S {\n" 22236 " private:\n" 22237 " class C {\n" 22238 " int j;\n" 22239 "\n" 22240 " public:\n" 22241 " C();\n" 22242 " };\n" 22243 "\n" 22244 " public:\n" 22245 " int i;\n" 22246 "};\n", 22247 Style); 22248 // Enumerations are not records and should be unaffected. 22249 Style.AllowShortEnumsOnASingleLine = false; 22250 verifyFormat("enum class E {\n" 22251 " A,\n" 22252 " B\n" 22253 "};\n", 22254 Style); 22255 // Test with a different indentation width; 22256 // also proves that the result is Style.AccessModifierOffset agnostic. 22257 Style.IndentWidth = 3; 22258 verifyFormat("class C {\n" 22259 " public:\n" 22260 " int i;\n" 22261 "};\n", 22262 Style); 22263 } 22264 22265 TEST_F(FormatTest, LimitlessStringsAndComments) { 22266 auto Style = getLLVMStyleWithColumns(0); 22267 constexpr StringRef Code = 22268 "/**\n" 22269 " * This is a multiline comment with quite some long lines, at least for " 22270 "the LLVM Style.\n" 22271 " * We will redo this with strings and line comments. Just to check if " 22272 "everything is working.\n" 22273 " */\n" 22274 "bool foo() {\n" 22275 " /* Single line multi line comment. */\n" 22276 " const std::string String = \"This is a multiline string with quite " 22277 "some long lines, at least for the LLVM Style.\"\n" 22278 " \"We already did it with multi line " 22279 "comments, and we will do it with line comments. Just to check if " 22280 "everything is working.\";\n" 22281 " // This is a line comment (block) with quite some long lines, at " 22282 "least for the LLVM Style.\n" 22283 " // We already did this with multi line comments and strings. Just to " 22284 "check if everything is working.\n" 22285 " const std::string SmallString = \"Hello World\";\n" 22286 " // Small line comment\n" 22287 " return String.size() > SmallString.size();\n" 22288 "}"; 22289 EXPECT_EQ(Code, format(Code, Style)); 22290 } 22291 } // namespace 22292 } // namespace format 22293 } // namespace clang 22294