1 //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "clang/Format/Format.h" 10 11 #include "../Tooling/ReplacementTest.h" 12 #include "FormatTestUtils.h" 13 14 #include "llvm/Support/Debug.h" 15 #include "llvm/Support/MemoryBuffer.h" 16 #include "gtest/gtest.h" 17 18 #define DEBUG_TYPE "format-test" 19 20 using clang::tooling::ReplacementTest; 21 using clang::tooling::toReplacements; 22 using testing::ScopedTrace; 23 24 namespace clang { 25 namespace format { 26 namespace { 27 28 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); } 29 30 class FormatTest : public ::testing::Test { 31 protected: 32 enum StatusCheck { SC_ExpectComplete, SC_ExpectIncomplete, SC_DoNotCheck }; 33 34 std::string format(llvm::StringRef Code, 35 const FormatStyle &Style = getLLVMStyle(), 36 StatusCheck CheckComplete = SC_ExpectComplete) { 37 LLVM_DEBUG(llvm::errs() << "---\n"); 38 LLVM_DEBUG(llvm::errs() << Code << "\n\n"); 39 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size())); 40 FormattingAttemptStatus Status; 41 tooling::Replacements Replaces = 42 reformat(Style, Code, Ranges, "<stdin>", &Status); 43 if (CheckComplete != SC_DoNotCheck) { 44 bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete; 45 EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete) 46 << Code << "\n\n"; 47 } 48 ReplacementCount = Replaces.size(); 49 auto Result = applyAllReplacements(Code, Replaces); 50 EXPECT_TRUE(static_cast<bool>(Result)); 51 LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n"); 52 return *Result; 53 } 54 55 FormatStyle getStyleWithColumns(FormatStyle Style, unsigned ColumnLimit) { 56 Style.ColumnLimit = ColumnLimit; 57 return Style; 58 } 59 60 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { 61 return getStyleWithColumns(getLLVMStyle(), ColumnLimit); 62 } 63 64 FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) { 65 return getStyleWithColumns(getGoogleStyle(), ColumnLimit); 66 } 67 68 void _verifyFormat(const char *File, int Line, llvm::StringRef Expected, 69 llvm::StringRef Code, 70 const FormatStyle &Style = getLLVMStyle()) { 71 ScopedTrace t(File, Line, ::testing::Message() << Code.str()); 72 EXPECT_EQ(Expected.str(), format(Expected, Style)) 73 << "Expected code is not stable"; 74 EXPECT_EQ(Expected.str(), format(Code, Style)); 75 if (Style.Language == FormatStyle::LK_Cpp) { 76 // Objective-C++ is a superset of C++, so everything checked for C++ 77 // needs to be checked for Objective-C++ as well. 78 FormatStyle ObjCStyle = Style; 79 ObjCStyle.Language = FormatStyle::LK_ObjC; 80 EXPECT_EQ(Expected.str(), format(test::messUp(Code), ObjCStyle)); 81 } 82 } 83 84 void _verifyFormat(const char *File, int Line, llvm::StringRef Code, 85 const FormatStyle &Style = getLLVMStyle()) { 86 _verifyFormat(File, Line, Code, test::messUp(Code), Style); 87 } 88 89 void _verifyIncompleteFormat(const char *File, int Line, llvm::StringRef Code, 90 const FormatStyle &Style = getLLVMStyle()) { 91 ScopedTrace t(File, Line, ::testing::Message() << Code.str()); 92 EXPECT_EQ(Code.str(), 93 format(test::messUp(Code), Style, SC_ExpectIncomplete)); 94 } 95 96 void _verifyIndependentOfContext(const char *File, int Line, 97 llvm::StringRef Text, 98 const FormatStyle &Style = getLLVMStyle()) { 99 _verifyFormat(File, Line, Text, Style); 100 _verifyFormat(File, Line, llvm::Twine("void f() { " + Text + " }").str(), 101 Style); 102 } 103 104 /// \brief Verify that clang-format does not crash on the given input. 105 void verifyNoCrash(llvm::StringRef Code, 106 const FormatStyle &Style = getLLVMStyle()) { 107 format(Code, Style, SC_DoNotCheck); 108 } 109 110 int ReplacementCount; 111 }; 112 113 #define verifyIndependentOfContext(...) \ 114 _verifyIndependentOfContext(__FILE__, __LINE__, __VA_ARGS__) 115 #define verifyIncompleteFormat(...) \ 116 _verifyIncompleteFormat(__FILE__, __LINE__, __VA_ARGS__) 117 #define verifyFormat(...) _verifyFormat(__FILE__, __LINE__, __VA_ARGS__) 118 #define verifyGoogleFormat(Code) verifyFormat(Code, getGoogleStyle()) 119 120 TEST_F(FormatTest, MessUp) { 121 EXPECT_EQ("1 2 3", test::messUp("1 2 3")); 122 EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n")); 123 EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc")); 124 EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc")); 125 EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne")); 126 } 127 128 TEST_F(FormatTest, DefaultLLVMStyleIsCpp) { 129 EXPECT_EQ(FormatStyle::LK_Cpp, getLLVMStyle().Language); 130 } 131 132 TEST_F(FormatTest, LLVMStyleOverride) { 133 EXPECT_EQ(FormatStyle::LK_Proto, 134 getLLVMStyle(FormatStyle::LK_Proto).Language); 135 } 136 137 //===----------------------------------------------------------------------===// 138 // Basic function tests. 139 //===----------------------------------------------------------------------===// 140 141 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) { 142 EXPECT_EQ(";", format(";")); 143 } 144 145 TEST_F(FormatTest, FormatsGlobalStatementsAt0) { 146 EXPECT_EQ("int i;", format(" int i;")); 147 EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;")); 148 EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); 149 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); 150 } 151 152 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { 153 EXPECT_EQ("int i;", format("int\ni;")); 154 } 155 156 TEST_F(FormatTest, FormatsNestedBlockStatements) { 157 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}")); 158 } 159 160 TEST_F(FormatTest, FormatsNestedCall) { 161 verifyFormat("Method(f1, f2(f3));"); 162 verifyFormat("Method(f1(f2, f3()));"); 163 verifyFormat("Method(f1(f2, (f3())));"); 164 } 165 166 TEST_F(FormatTest, NestedNameSpecifiers) { 167 verifyFormat("vector<::Type> v;"); 168 verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())"); 169 verifyFormat("static constexpr bool Bar = decltype(bar())::value;"); 170 verifyFormat("static constexpr bool Bar = typeof(bar())::value;"); 171 verifyFormat("static constexpr bool Bar = __underlying_type(bar())::value;"); 172 verifyFormat("static constexpr bool Bar = _Atomic(bar())::value;"); 173 verifyFormat("bool a = 2 < ::SomeFunction();"); 174 verifyFormat("ALWAYS_INLINE ::std::string getName();"); 175 verifyFormat("some::string getName();"); 176 } 177 178 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) { 179 EXPECT_EQ("if (a) {\n" 180 " f();\n" 181 "}", 182 format("if(a){f();}")); 183 EXPECT_EQ(4, ReplacementCount); 184 EXPECT_EQ("if (a) {\n" 185 " f();\n" 186 "}", 187 format("if (a) {\n" 188 " f();\n" 189 "}")); 190 EXPECT_EQ(0, ReplacementCount); 191 EXPECT_EQ("/*\r\n" 192 "\r\n" 193 "*/\r\n", 194 format("/*\r\n" 195 "\r\n" 196 "*/\r\n")); 197 EXPECT_EQ(0, ReplacementCount); 198 } 199 200 TEST_F(FormatTest, RemovesEmptyLines) { 201 EXPECT_EQ("class C {\n" 202 " int i;\n" 203 "};", 204 format("class C {\n" 205 " int i;\n" 206 "\n" 207 "};")); 208 209 // Don't remove empty lines at the start of namespaces or extern "C" blocks. 210 EXPECT_EQ("namespace N {\n" 211 "\n" 212 "int i;\n" 213 "}", 214 format("namespace N {\n" 215 "\n" 216 "int i;\n" 217 "}", 218 getGoogleStyle())); 219 EXPECT_EQ("/* something */ namespace N {\n" 220 "\n" 221 "int i;\n" 222 "}", 223 format("/* something */ namespace N {\n" 224 "\n" 225 "int i;\n" 226 "}", 227 getGoogleStyle())); 228 EXPECT_EQ("inline namespace N {\n" 229 "\n" 230 "int i;\n" 231 "}", 232 format("inline namespace N {\n" 233 "\n" 234 "int i;\n" 235 "}", 236 getGoogleStyle())); 237 EXPECT_EQ("/* something */ inline namespace N {\n" 238 "\n" 239 "int i;\n" 240 "}", 241 format("/* something */ inline namespace N {\n" 242 "\n" 243 "int i;\n" 244 "}", 245 getGoogleStyle())); 246 EXPECT_EQ("export namespace N {\n" 247 "\n" 248 "int i;\n" 249 "}", 250 format("export namespace N {\n" 251 "\n" 252 "int i;\n" 253 "}", 254 getGoogleStyle())); 255 EXPECT_EQ("extern /**/ \"C\" /**/ {\n" 256 "\n" 257 "int i;\n" 258 "}", 259 format("extern /**/ \"C\" /**/ {\n" 260 "\n" 261 "int i;\n" 262 "}", 263 getGoogleStyle())); 264 265 auto CustomStyle = getLLVMStyle(); 266 CustomStyle.BreakBeforeBraces = FormatStyle::BS_Custom; 267 CustomStyle.BraceWrapping.AfterNamespace = true; 268 CustomStyle.KeepEmptyLinesAtTheStartOfBlocks = false; 269 EXPECT_EQ("namespace N\n" 270 "{\n" 271 "\n" 272 "int i;\n" 273 "}", 274 format("namespace N\n" 275 "{\n" 276 "\n" 277 "\n" 278 "int i;\n" 279 "}", 280 CustomStyle)); 281 EXPECT_EQ("/* something */ namespace N\n" 282 "{\n" 283 "\n" 284 "int i;\n" 285 "}", 286 format("/* something */ namespace N {\n" 287 "\n" 288 "\n" 289 "int i;\n" 290 "}", 291 CustomStyle)); 292 EXPECT_EQ("inline namespace N\n" 293 "{\n" 294 "\n" 295 "int i;\n" 296 "}", 297 format("inline namespace N\n" 298 "{\n" 299 "\n" 300 "\n" 301 "int i;\n" 302 "}", 303 CustomStyle)); 304 EXPECT_EQ("/* something */ inline namespace N\n" 305 "{\n" 306 "\n" 307 "int i;\n" 308 "}", 309 format("/* something */ inline namespace N\n" 310 "{\n" 311 "\n" 312 "int i;\n" 313 "}", 314 CustomStyle)); 315 EXPECT_EQ("export namespace N\n" 316 "{\n" 317 "\n" 318 "int i;\n" 319 "}", 320 format("export namespace N\n" 321 "{\n" 322 "\n" 323 "int i;\n" 324 "}", 325 CustomStyle)); 326 EXPECT_EQ("namespace a\n" 327 "{\n" 328 "namespace b\n" 329 "{\n" 330 "\n" 331 "class AA {};\n" 332 "\n" 333 "} // namespace b\n" 334 "} // namespace a\n", 335 format("namespace a\n" 336 "{\n" 337 "namespace b\n" 338 "{\n" 339 "\n" 340 "\n" 341 "class AA {};\n" 342 "\n" 343 "\n" 344 "}\n" 345 "}\n", 346 CustomStyle)); 347 EXPECT_EQ("namespace A /* comment */\n" 348 "{\n" 349 "class B {}\n" 350 "} // namespace A", 351 format("namespace A /* comment */ { class B {} }", CustomStyle)); 352 EXPECT_EQ("namespace A\n" 353 "{ /* comment */\n" 354 "class B {}\n" 355 "} // namespace A", 356 format("namespace A {/* comment */ class B {} }", CustomStyle)); 357 EXPECT_EQ("namespace A\n" 358 "{ /* comment */\n" 359 "\n" 360 "class B {}\n" 361 "\n" 362 "" 363 "} // namespace A", 364 format("namespace A { /* comment */\n" 365 "\n" 366 "\n" 367 "class B {}\n" 368 "\n" 369 "\n" 370 "}", 371 CustomStyle)); 372 EXPECT_EQ("namespace A /* comment */\n" 373 "{\n" 374 "\n" 375 "class B {}\n" 376 "\n" 377 "} // namespace A", 378 format("namespace A/* comment */ {\n" 379 "\n" 380 "\n" 381 "class B {}\n" 382 "\n" 383 "\n" 384 "}", 385 CustomStyle)); 386 387 // ...but do keep inlining and removing empty lines for non-block extern "C" 388 // functions. 389 verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle()); 390 EXPECT_EQ("extern \"C\" int f() {\n" 391 " int i = 42;\n" 392 " return i;\n" 393 "}", 394 format("extern \"C\" int f() {\n" 395 "\n" 396 " int i = 42;\n" 397 " return i;\n" 398 "}", 399 getGoogleStyle())); 400 401 // Remove empty lines at the beginning and end of blocks. 402 EXPECT_EQ("void f() {\n" 403 "\n" 404 " if (a) {\n" 405 "\n" 406 " f();\n" 407 " }\n" 408 "}", 409 format("void f() {\n" 410 "\n" 411 " if (a) {\n" 412 "\n" 413 " f();\n" 414 "\n" 415 " }\n" 416 "\n" 417 "}", 418 getLLVMStyle())); 419 EXPECT_EQ("void f() {\n" 420 " if (a) {\n" 421 " f();\n" 422 " }\n" 423 "}", 424 format("void f() {\n" 425 "\n" 426 " if (a) {\n" 427 "\n" 428 " f();\n" 429 "\n" 430 " }\n" 431 "\n" 432 "}", 433 getGoogleStyle())); 434 435 // Don't remove empty lines in more complex control statements. 436 EXPECT_EQ("void f() {\n" 437 " if (a) {\n" 438 " f();\n" 439 "\n" 440 " } else if (b) {\n" 441 " f();\n" 442 " }\n" 443 "}", 444 format("void f() {\n" 445 " if (a) {\n" 446 " f();\n" 447 "\n" 448 " } else if (b) {\n" 449 " f();\n" 450 "\n" 451 " }\n" 452 "\n" 453 "}")); 454 455 // Don't remove empty lines before namespace endings. 456 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 457 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 458 EXPECT_EQ("namespace {\n" 459 "int i;\n" 460 "\n" 461 "}", 462 format("namespace {\n" 463 "int i;\n" 464 "\n" 465 "}", 466 LLVMWithNoNamespaceFix)); 467 EXPECT_EQ("namespace {\n" 468 "int i;\n" 469 "}", 470 format("namespace {\n" 471 "int i;\n" 472 "}", 473 LLVMWithNoNamespaceFix)); 474 EXPECT_EQ("namespace {\n" 475 "int i;\n" 476 "\n" 477 "};", 478 format("namespace {\n" 479 "int i;\n" 480 "\n" 481 "};", 482 LLVMWithNoNamespaceFix)); 483 EXPECT_EQ("namespace {\n" 484 "int i;\n" 485 "};", 486 format("namespace {\n" 487 "int i;\n" 488 "};", 489 LLVMWithNoNamespaceFix)); 490 EXPECT_EQ("namespace {\n" 491 "int i;\n" 492 "\n" 493 "}", 494 format("namespace {\n" 495 "int i;\n" 496 "\n" 497 "}")); 498 EXPECT_EQ("namespace {\n" 499 "int i;\n" 500 "\n" 501 "} // namespace", 502 format("namespace {\n" 503 "int i;\n" 504 "\n" 505 "} // namespace")); 506 507 FormatStyle Style = getLLVMStyle(); 508 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 509 Style.MaxEmptyLinesToKeep = 2; 510 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 511 Style.BraceWrapping.AfterClass = true; 512 Style.BraceWrapping.AfterFunction = true; 513 Style.KeepEmptyLinesAtTheStartOfBlocks = false; 514 515 EXPECT_EQ("class Foo\n" 516 "{\n" 517 " Foo() {}\n" 518 "\n" 519 " void funk() {}\n" 520 "};", 521 format("class Foo\n" 522 "{\n" 523 " Foo()\n" 524 " {\n" 525 " }\n" 526 "\n" 527 " void funk() {}\n" 528 "};", 529 Style)); 530 } 531 532 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) { 533 verifyFormat("x = (a) and (b);"); 534 verifyFormat("x = (a) or (b);"); 535 verifyFormat("x = (a) bitand (b);"); 536 verifyFormat("x = (a) bitor (b);"); 537 verifyFormat("x = (a) not_eq (b);"); 538 verifyFormat("x = (a) and_eq (b);"); 539 verifyFormat("x = (a) or_eq (b);"); 540 verifyFormat("x = (a) xor (b);"); 541 } 542 543 TEST_F(FormatTest, RecognizesUnaryOperatorKeywords) { 544 verifyFormat("x = compl(a);"); 545 verifyFormat("x = not(a);"); 546 verifyFormat("x = bitand(a);"); 547 // Unary operator must not be merged with the next identifier 548 verifyFormat("x = compl a;"); 549 verifyFormat("x = not a;"); 550 verifyFormat("x = bitand a;"); 551 } 552 553 //===----------------------------------------------------------------------===// 554 // Tests for control statements. 555 //===----------------------------------------------------------------------===// 556 557 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) { 558 verifyFormat("if (true)\n f();\ng();"); 559 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); 560 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); 561 verifyFormat("if constexpr (true)\n" 562 " f();\ng();"); 563 verifyFormat("if CONSTEXPR (true)\n" 564 " f();\ng();"); 565 verifyFormat("if constexpr (a)\n" 566 " if constexpr (b)\n" 567 " if constexpr (c)\n" 568 " g();\n" 569 "h();"); 570 verifyFormat("if CONSTEXPR (a)\n" 571 " if CONSTEXPR (b)\n" 572 " if CONSTEXPR (c)\n" 573 " g();\n" 574 "h();"); 575 verifyFormat("if constexpr (a)\n" 576 " if constexpr (b) {\n" 577 " f();\n" 578 " }\n" 579 "g();"); 580 verifyFormat("if CONSTEXPR (a)\n" 581 " if CONSTEXPR (b) {\n" 582 " f();\n" 583 " }\n" 584 "g();"); 585 586 verifyFormat("if (a)\n" 587 " g();"); 588 verifyFormat("if (a) {\n" 589 " g()\n" 590 "};"); 591 verifyFormat("if (a)\n" 592 " g();\n" 593 "else\n" 594 " g();"); 595 verifyFormat("if (a) {\n" 596 " g();\n" 597 "} else\n" 598 " g();"); 599 verifyFormat("if (a)\n" 600 " g();\n" 601 "else {\n" 602 " g();\n" 603 "}"); 604 verifyFormat("if (a) {\n" 605 " g();\n" 606 "} else {\n" 607 " g();\n" 608 "}"); 609 verifyFormat("if (a)\n" 610 " g();\n" 611 "else if (b)\n" 612 " g();\n" 613 "else\n" 614 " g();"); 615 verifyFormat("if (a) {\n" 616 " g();\n" 617 "} else if (b)\n" 618 " g();\n" 619 "else\n" 620 " g();"); 621 verifyFormat("if (a)\n" 622 " g();\n" 623 "else if (b) {\n" 624 " g();\n" 625 "} else\n" 626 " g();"); 627 verifyFormat("if (a)\n" 628 " g();\n" 629 "else if (b)\n" 630 " g();\n" 631 "else {\n" 632 " g();\n" 633 "}"); 634 verifyFormat("if (a)\n" 635 " g();\n" 636 "else if (b) {\n" 637 " g();\n" 638 "} else {\n" 639 " g();\n" 640 "}"); 641 verifyFormat("if (a) {\n" 642 " g();\n" 643 "} else if (b) {\n" 644 " g();\n" 645 "} else {\n" 646 " g();\n" 647 "}"); 648 649 FormatStyle AllowsMergedIf = getLLVMStyle(); 650 AllowsMergedIf.IfMacros.push_back("MYIF"); 651 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left; 652 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 653 FormatStyle::SIS_WithoutElse; 654 verifyFormat("if (a)\n" 655 " // comment\n" 656 " f();", 657 AllowsMergedIf); 658 verifyFormat("{\n" 659 " if (a)\n" 660 " label:\n" 661 " f();\n" 662 "}", 663 AllowsMergedIf); 664 verifyFormat("#define A \\\n" 665 " if (a) \\\n" 666 " label: \\\n" 667 " f()", 668 AllowsMergedIf); 669 verifyFormat("if (a)\n" 670 " ;", 671 AllowsMergedIf); 672 verifyFormat("if (a)\n" 673 " if (b) return;", 674 AllowsMergedIf); 675 676 verifyFormat("if (a) // Can't merge this\n" 677 " f();\n", 678 AllowsMergedIf); 679 verifyFormat("if (a) /* still don't merge */\n" 680 " f();", 681 AllowsMergedIf); 682 verifyFormat("if (a) { // Never merge this\n" 683 " f();\n" 684 "}", 685 AllowsMergedIf); 686 verifyFormat("if (a) { /* Never merge this */\n" 687 " f();\n" 688 "}", 689 AllowsMergedIf); 690 verifyFormat("MYIF (a)\n" 691 " // comment\n" 692 " f();", 693 AllowsMergedIf); 694 verifyFormat("{\n" 695 " MYIF (a)\n" 696 " label:\n" 697 " f();\n" 698 "}", 699 AllowsMergedIf); 700 verifyFormat("#define A \\\n" 701 " MYIF (a) \\\n" 702 " label: \\\n" 703 " f()", 704 AllowsMergedIf); 705 verifyFormat("MYIF (a)\n" 706 " ;", 707 AllowsMergedIf); 708 verifyFormat("MYIF (a)\n" 709 " MYIF (b) return;", 710 AllowsMergedIf); 711 712 verifyFormat("MYIF (a) // Can't merge this\n" 713 " f();\n", 714 AllowsMergedIf); 715 verifyFormat("MYIF (a) /* still don't merge */\n" 716 " f();", 717 AllowsMergedIf); 718 verifyFormat("MYIF (a) { // Never merge this\n" 719 " f();\n" 720 "}", 721 AllowsMergedIf); 722 verifyFormat("MYIF (a) { /* Never merge this */\n" 723 " f();\n" 724 "}", 725 AllowsMergedIf); 726 727 AllowsMergedIf.ColumnLimit = 14; 728 // Where line-lengths matter, a 2-letter synonym that maintains line length. 729 // Not IF to avoid any confusion that IF is somehow special. 730 AllowsMergedIf.IfMacros.push_back("FI"); 731 verifyFormat("if (a) return;", AllowsMergedIf); 732 verifyFormat("if (aaaaaaaaa)\n" 733 " return;", 734 AllowsMergedIf); 735 verifyFormat("FI (a) return;", AllowsMergedIf); 736 verifyFormat("FI (aaaaaaaaa)\n" 737 " return;", 738 AllowsMergedIf); 739 740 AllowsMergedIf.ColumnLimit = 13; 741 verifyFormat("if (a)\n return;", AllowsMergedIf); 742 verifyFormat("FI (a)\n return;", AllowsMergedIf); 743 744 FormatStyle AllowsMergedIfElse = getLLVMStyle(); 745 AllowsMergedIfElse.IfMacros.push_back("MYIF"); 746 AllowsMergedIfElse.AllowShortIfStatementsOnASingleLine = 747 FormatStyle::SIS_AllIfsAndElse; 748 verifyFormat("if (a)\n" 749 " // comment\n" 750 " f();\n" 751 "else\n" 752 " // comment\n" 753 " f();", 754 AllowsMergedIfElse); 755 verifyFormat("{\n" 756 " if (a)\n" 757 " label:\n" 758 " f();\n" 759 " else\n" 760 " label:\n" 761 " f();\n" 762 "}", 763 AllowsMergedIfElse); 764 verifyFormat("if (a)\n" 765 " ;\n" 766 "else\n" 767 " ;", 768 AllowsMergedIfElse); 769 verifyFormat("if (a) {\n" 770 "} else {\n" 771 "}", 772 AllowsMergedIfElse); 773 verifyFormat("if (a) return;\n" 774 "else if (b) return;\n" 775 "else return;", 776 AllowsMergedIfElse); 777 verifyFormat("if (a) {\n" 778 "} else return;", 779 AllowsMergedIfElse); 780 verifyFormat("if (a) {\n" 781 "} else if (b) return;\n" 782 "else return;", 783 AllowsMergedIfElse); 784 verifyFormat("if (a) return;\n" 785 "else if (b) {\n" 786 "} else return;", 787 AllowsMergedIfElse); 788 verifyFormat("if (a)\n" 789 " if (b) return;\n" 790 " else return;", 791 AllowsMergedIfElse); 792 verifyFormat("if constexpr (a)\n" 793 " if constexpr (b) return;\n" 794 " else if constexpr (c) return;\n" 795 " else return;", 796 AllowsMergedIfElse); 797 verifyFormat("MYIF (a)\n" 798 " // comment\n" 799 " f();\n" 800 "else\n" 801 " // comment\n" 802 " f();", 803 AllowsMergedIfElse); 804 verifyFormat("{\n" 805 " MYIF (a)\n" 806 " label:\n" 807 " f();\n" 808 " else\n" 809 " label:\n" 810 " f();\n" 811 "}", 812 AllowsMergedIfElse); 813 verifyFormat("MYIF (a)\n" 814 " ;\n" 815 "else\n" 816 " ;", 817 AllowsMergedIfElse); 818 verifyFormat("MYIF (a) {\n" 819 "} else {\n" 820 "}", 821 AllowsMergedIfElse); 822 verifyFormat("MYIF (a) return;\n" 823 "else MYIF (b) return;\n" 824 "else return;", 825 AllowsMergedIfElse); 826 verifyFormat("MYIF (a) {\n" 827 "} else return;", 828 AllowsMergedIfElse); 829 verifyFormat("MYIF (a) {\n" 830 "} else MYIF (b) return;\n" 831 "else return;", 832 AllowsMergedIfElse); 833 verifyFormat("MYIF (a) return;\n" 834 "else MYIF (b) {\n" 835 "} else return;", 836 AllowsMergedIfElse); 837 verifyFormat("MYIF (a)\n" 838 " MYIF (b) return;\n" 839 " else return;", 840 AllowsMergedIfElse); 841 verifyFormat("MYIF constexpr (a)\n" 842 " MYIF constexpr (b) return;\n" 843 " else MYIF constexpr (c) return;\n" 844 " else return;", 845 AllowsMergedIfElse); 846 } 847 848 TEST_F(FormatTest, FormatIfWithoutCompoundStatementButElseWith) { 849 FormatStyle AllowsMergedIf = getLLVMStyle(); 850 AllowsMergedIf.IfMacros.push_back("MYIF"); 851 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left; 852 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 853 FormatStyle::SIS_WithoutElse; 854 verifyFormat("if (a)\n" 855 " f();\n" 856 "else {\n" 857 " g();\n" 858 "}", 859 AllowsMergedIf); 860 verifyFormat("if (a)\n" 861 " f();\n" 862 "else\n" 863 " g();\n", 864 AllowsMergedIf); 865 866 verifyFormat("if (a) g();", AllowsMergedIf); 867 verifyFormat("if (a) {\n" 868 " g()\n" 869 "};", 870 AllowsMergedIf); 871 verifyFormat("if (a)\n" 872 " g();\n" 873 "else\n" 874 " g();", 875 AllowsMergedIf); 876 verifyFormat("if (a) {\n" 877 " g();\n" 878 "} else\n" 879 " g();", 880 AllowsMergedIf); 881 verifyFormat("if (a)\n" 882 " g();\n" 883 "else {\n" 884 " g();\n" 885 "}", 886 AllowsMergedIf); 887 verifyFormat("if (a) {\n" 888 " g();\n" 889 "} else {\n" 890 " g();\n" 891 "}", 892 AllowsMergedIf); 893 verifyFormat("if (a)\n" 894 " g();\n" 895 "else if (b)\n" 896 " g();\n" 897 "else\n" 898 " g();", 899 AllowsMergedIf); 900 verifyFormat("if (a) {\n" 901 " g();\n" 902 "} else if (b)\n" 903 " g();\n" 904 "else\n" 905 " g();", 906 AllowsMergedIf); 907 verifyFormat("if (a)\n" 908 " g();\n" 909 "else if (b) {\n" 910 " g();\n" 911 "} else\n" 912 " g();", 913 AllowsMergedIf); 914 verifyFormat("if (a)\n" 915 " g();\n" 916 "else if (b)\n" 917 " g();\n" 918 "else {\n" 919 " g();\n" 920 "}", 921 AllowsMergedIf); 922 verifyFormat("if (a)\n" 923 " g();\n" 924 "else if (b) {\n" 925 " g();\n" 926 "} else {\n" 927 " g();\n" 928 "}", 929 AllowsMergedIf); 930 verifyFormat("if (a) {\n" 931 " g();\n" 932 "} else if (b) {\n" 933 " g();\n" 934 "} else {\n" 935 " g();\n" 936 "}", 937 AllowsMergedIf); 938 verifyFormat("MYIF (a)\n" 939 " f();\n" 940 "else {\n" 941 " g();\n" 942 "}", 943 AllowsMergedIf); 944 verifyFormat("MYIF (a)\n" 945 " f();\n" 946 "else\n" 947 " g();\n", 948 AllowsMergedIf); 949 950 verifyFormat("MYIF (a) g();", AllowsMergedIf); 951 verifyFormat("MYIF (a) {\n" 952 " g()\n" 953 "};", 954 AllowsMergedIf); 955 verifyFormat("MYIF (a)\n" 956 " g();\n" 957 "else\n" 958 " g();", 959 AllowsMergedIf); 960 verifyFormat("MYIF (a) {\n" 961 " g();\n" 962 "} else\n" 963 " g();", 964 AllowsMergedIf); 965 verifyFormat("MYIF (a)\n" 966 " g();\n" 967 "else {\n" 968 " g();\n" 969 "}", 970 AllowsMergedIf); 971 verifyFormat("MYIF (a) {\n" 972 " g();\n" 973 "} else {\n" 974 " g();\n" 975 "}", 976 AllowsMergedIf); 977 verifyFormat("MYIF (a)\n" 978 " g();\n" 979 "else MYIF (b)\n" 980 " g();\n" 981 "else\n" 982 " g();", 983 AllowsMergedIf); 984 verifyFormat("MYIF (a)\n" 985 " g();\n" 986 "else if (b)\n" 987 " g();\n" 988 "else\n" 989 " g();", 990 AllowsMergedIf); 991 verifyFormat("MYIF (a) {\n" 992 " g();\n" 993 "} else MYIF (b)\n" 994 " g();\n" 995 "else\n" 996 " g();", 997 AllowsMergedIf); 998 verifyFormat("MYIF (a) {\n" 999 " g();\n" 1000 "} else if (b)\n" 1001 " g();\n" 1002 "else\n" 1003 " g();", 1004 AllowsMergedIf); 1005 verifyFormat("MYIF (a)\n" 1006 " g();\n" 1007 "else MYIF (b) {\n" 1008 " g();\n" 1009 "} else\n" 1010 " g();", 1011 AllowsMergedIf); 1012 verifyFormat("MYIF (a)\n" 1013 " g();\n" 1014 "else if (b) {\n" 1015 " g();\n" 1016 "} else\n" 1017 " g();", 1018 AllowsMergedIf); 1019 verifyFormat("MYIF (a)\n" 1020 " g();\n" 1021 "else MYIF (b)\n" 1022 " g();\n" 1023 "else {\n" 1024 " g();\n" 1025 "}", 1026 AllowsMergedIf); 1027 verifyFormat("MYIF (a)\n" 1028 " g();\n" 1029 "else if (b)\n" 1030 " g();\n" 1031 "else {\n" 1032 " g();\n" 1033 "}", 1034 AllowsMergedIf); 1035 verifyFormat("MYIF (a)\n" 1036 " g();\n" 1037 "else MYIF (b) {\n" 1038 " g();\n" 1039 "} else {\n" 1040 " g();\n" 1041 "}", 1042 AllowsMergedIf); 1043 verifyFormat("MYIF (a)\n" 1044 " g();\n" 1045 "else if (b) {\n" 1046 " g();\n" 1047 "} else {\n" 1048 " g();\n" 1049 "}", 1050 AllowsMergedIf); 1051 verifyFormat("MYIF (a) {\n" 1052 " g();\n" 1053 "} else MYIF (b) {\n" 1054 " g();\n" 1055 "} else {\n" 1056 " g();\n" 1057 "}", 1058 AllowsMergedIf); 1059 verifyFormat("MYIF (a) {\n" 1060 " g();\n" 1061 "} else if (b) {\n" 1062 " g();\n" 1063 "} else {\n" 1064 " g();\n" 1065 "}", 1066 AllowsMergedIf); 1067 1068 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 1069 FormatStyle::SIS_OnlyFirstIf; 1070 1071 verifyFormat("if (a) f();\n" 1072 "else {\n" 1073 " g();\n" 1074 "}", 1075 AllowsMergedIf); 1076 verifyFormat("if (a) f();\n" 1077 "else {\n" 1078 " if (a) f();\n" 1079 " else {\n" 1080 " g();\n" 1081 " }\n" 1082 " g();\n" 1083 "}", 1084 AllowsMergedIf); 1085 1086 verifyFormat("if (a) g();", AllowsMergedIf); 1087 verifyFormat("if (a) {\n" 1088 " g()\n" 1089 "};", 1090 AllowsMergedIf); 1091 verifyFormat("if (a) g();\n" 1092 "else\n" 1093 " g();", 1094 AllowsMergedIf); 1095 verifyFormat("if (a) {\n" 1096 " g();\n" 1097 "} else\n" 1098 " g();", 1099 AllowsMergedIf); 1100 verifyFormat("if (a) g();\n" 1101 "else {\n" 1102 " g();\n" 1103 "}", 1104 AllowsMergedIf); 1105 verifyFormat("if (a) {\n" 1106 " g();\n" 1107 "} else {\n" 1108 " g();\n" 1109 "}", 1110 AllowsMergedIf); 1111 verifyFormat("if (a) g();\n" 1112 "else if (b)\n" 1113 " g();\n" 1114 "else\n" 1115 " g();", 1116 AllowsMergedIf); 1117 verifyFormat("if (a) {\n" 1118 " g();\n" 1119 "} else if (b)\n" 1120 " g();\n" 1121 "else\n" 1122 " g();", 1123 AllowsMergedIf); 1124 verifyFormat("if (a) g();\n" 1125 "else if (b) {\n" 1126 " g();\n" 1127 "} else\n" 1128 " g();", 1129 AllowsMergedIf); 1130 verifyFormat("if (a) g();\n" 1131 "else if (b)\n" 1132 " g();\n" 1133 "else {\n" 1134 " g();\n" 1135 "}", 1136 AllowsMergedIf); 1137 verifyFormat("if (a) g();\n" 1138 "else if (b) {\n" 1139 " g();\n" 1140 "} else {\n" 1141 " g();\n" 1142 "}", 1143 AllowsMergedIf); 1144 verifyFormat("if (a) {\n" 1145 " g();\n" 1146 "} else if (b) {\n" 1147 " g();\n" 1148 "} else {\n" 1149 " g();\n" 1150 "}", 1151 AllowsMergedIf); 1152 verifyFormat("MYIF (a) f();\n" 1153 "else {\n" 1154 " g();\n" 1155 "}", 1156 AllowsMergedIf); 1157 verifyFormat("MYIF (a) f();\n" 1158 "else {\n" 1159 " if (a) f();\n" 1160 " else {\n" 1161 " g();\n" 1162 " }\n" 1163 " g();\n" 1164 "}", 1165 AllowsMergedIf); 1166 1167 verifyFormat("MYIF (a) g();", AllowsMergedIf); 1168 verifyFormat("MYIF (a) {\n" 1169 " g()\n" 1170 "};", 1171 AllowsMergedIf); 1172 verifyFormat("MYIF (a) g();\n" 1173 "else\n" 1174 " g();", 1175 AllowsMergedIf); 1176 verifyFormat("MYIF (a) {\n" 1177 " g();\n" 1178 "} else\n" 1179 " g();", 1180 AllowsMergedIf); 1181 verifyFormat("MYIF (a) g();\n" 1182 "else {\n" 1183 " g();\n" 1184 "}", 1185 AllowsMergedIf); 1186 verifyFormat("MYIF (a) {\n" 1187 " g();\n" 1188 "} else {\n" 1189 " g();\n" 1190 "}", 1191 AllowsMergedIf); 1192 verifyFormat("MYIF (a) g();\n" 1193 "else MYIF (b)\n" 1194 " g();\n" 1195 "else\n" 1196 " g();", 1197 AllowsMergedIf); 1198 verifyFormat("MYIF (a) g();\n" 1199 "else if (b)\n" 1200 " g();\n" 1201 "else\n" 1202 " g();", 1203 AllowsMergedIf); 1204 verifyFormat("MYIF (a) {\n" 1205 " g();\n" 1206 "} else MYIF (b)\n" 1207 " g();\n" 1208 "else\n" 1209 " g();", 1210 AllowsMergedIf); 1211 verifyFormat("MYIF (a) {\n" 1212 " g();\n" 1213 "} else if (b)\n" 1214 " g();\n" 1215 "else\n" 1216 " g();", 1217 AllowsMergedIf); 1218 verifyFormat("MYIF (a) g();\n" 1219 "else MYIF (b) {\n" 1220 " g();\n" 1221 "} else\n" 1222 " g();", 1223 AllowsMergedIf); 1224 verifyFormat("MYIF (a) g();\n" 1225 "else if (b) {\n" 1226 " g();\n" 1227 "} else\n" 1228 " g();", 1229 AllowsMergedIf); 1230 verifyFormat("MYIF (a) g();\n" 1231 "else MYIF (b)\n" 1232 " g();\n" 1233 "else {\n" 1234 " g();\n" 1235 "}", 1236 AllowsMergedIf); 1237 verifyFormat("MYIF (a) g();\n" 1238 "else if (b)\n" 1239 " g();\n" 1240 "else {\n" 1241 " g();\n" 1242 "}", 1243 AllowsMergedIf); 1244 verifyFormat("MYIF (a) g();\n" 1245 "else MYIF (b) {\n" 1246 " g();\n" 1247 "} else {\n" 1248 " g();\n" 1249 "}", 1250 AllowsMergedIf); 1251 verifyFormat("MYIF (a) g();\n" 1252 "else if (b) {\n" 1253 " g();\n" 1254 "} else {\n" 1255 " g();\n" 1256 "}", 1257 AllowsMergedIf); 1258 verifyFormat("MYIF (a) {\n" 1259 " g();\n" 1260 "} else MYIF (b) {\n" 1261 " g();\n" 1262 "} else {\n" 1263 " g();\n" 1264 "}", 1265 AllowsMergedIf); 1266 verifyFormat("MYIF (a) {\n" 1267 " g();\n" 1268 "} else if (b) {\n" 1269 " g();\n" 1270 "} else {\n" 1271 " g();\n" 1272 "}", 1273 AllowsMergedIf); 1274 1275 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 1276 FormatStyle::SIS_AllIfsAndElse; 1277 1278 verifyFormat("if (a) f();\n" 1279 "else {\n" 1280 " g();\n" 1281 "}", 1282 AllowsMergedIf); 1283 verifyFormat("if (a) f();\n" 1284 "else {\n" 1285 " if (a) f();\n" 1286 " else {\n" 1287 " g();\n" 1288 " }\n" 1289 " g();\n" 1290 "}", 1291 AllowsMergedIf); 1292 1293 verifyFormat("if (a) g();", AllowsMergedIf); 1294 verifyFormat("if (a) {\n" 1295 " g()\n" 1296 "};", 1297 AllowsMergedIf); 1298 verifyFormat("if (a) g();\n" 1299 "else g();", 1300 AllowsMergedIf); 1301 verifyFormat("if (a) {\n" 1302 " g();\n" 1303 "} else g();", 1304 AllowsMergedIf); 1305 verifyFormat("if (a) g();\n" 1306 "else {\n" 1307 " g();\n" 1308 "}", 1309 AllowsMergedIf); 1310 verifyFormat("if (a) {\n" 1311 " g();\n" 1312 "} else {\n" 1313 " g();\n" 1314 "}", 1315 AllowsMergedIf); 1316 verifyFormat("if (a) g();\n" 1317 "else if (b) g();\n" 1318 "else g();", 1319 AllowsMergedIf); 1320 verifyFormat("if (a) {\n" 1321 " g();\n" 1322 "} else if (b) g();\n" 1323 "else g();", 1324 AllowsMergedIf); 1325 verifyFormat("if (a) g();\n" 1326 "else if (b) {\n" 1327 " g();\n" 1328 "} else g();", 1329 AllowsMergedIf); 1330 verifyFormat("if (a) g();\n" 1331 "else if (b) g();\n" 1332 "else {\n" 1333 " g();\n" 1334 "}", 1335 AllowsMergedIf); 1336 verifyFormat("if (a) g();\n" 1337 "else if (b) {\n" 1338 " g();\n" 1339 "} else {\n" 1340 " g();\n" 1341 "}", 1342 AllowsMergedIf); 1343 verifyFormat("if (a) {\n" 1344 " g();\n" 1345 "} else if (b) {\n" 1346 " g();\n" 1347 "} else {\n" 1348 " g();\n" 1349 "}", 1350 AllowsMergedIf); 1351 verifyFormat("MYIF (a) f();\n" 1352 "else {\n" 1353 " g();\n" 1354 "}", 1355 AllowsMergedIf); 1356 verifyFormat("MYIF (a) f();\n" 1357 "else {\n" 1358 " if (a) f();\n" 1359 " else {\n" 1360 " g();\n" 1361 " }\n" 1362 " g();\n" 1363 "}", 1364 AllowsMergedIf); 1365 1366 verifyFormat("MYIF (a) g();", AllowsMergedIf); 1367 verifyFormat("MYIF (a) {\n" 1368 " g()\n" 1369 "};", 1370 AllowsMergedIf); 1371 verifyFormat("MYIF (a) g();\n" 1372 "else g();", 1373 AllowsMergedIf); 1374 verifyFormat("MYIF (a) {\n" 1375 " g();\n" 1376 "} else g();", 1377 AllowsMergedIf); 1378 verifyFormat("MYIF (a) g();\n" 1379 "else {\n" 1380 " g();\n" 1381 "}", 1382 AllowsMergedIf); 1383 verifyFormat("MYIF (a) {\n" 1384 " g();\n" 1385 "} else {\n" 1386 " g();\n" 1387 "}", 1388 AllowsMergedIf); 1389 verifyFormat("MYIF (a) g();\n" 1390 "else MYIF (b) g();\n" 1391 "else g();", 1392 AllowsMergedIf); 1393 verifyFormat("MYIF (a) g();\n" 1394 "else if (b) g();\n" 1395 "else g();", 1396 AllowsMergedIf); 1397 verifyFormat("MYIF (a) {\n" 1398 " g();\n" 1399 "} else MYIF (b) g();\n" 1400 "else g();", 1401 AllowsMergedIf); 1402 verifyFormat("MYIF (a) {\n" 1403 " g();\n" 1404 "} else if (b) g();\n" 1405 "else g();", 1406 AllowsMergedIf); 1407 verifyFormat("MYIF (a) g();\n" 1408 "else MYIF (b) {\n" 1409 " g();\n" 1410 "} else g();", 1411 AllowsMergedIf); 1412 verifyFormat("MYIF (a) g();\n" 1413 "else if (b) {\n" 1414 " g();\n" 1415 "} else g();", 1416 AllowsMergedIf); 1417 verifyFormat("MYIF (a) g();\n" 1418 "else MYIF (b) g();\n" 1419 "else {\n" 1420 " g();\n" 1421 "}", 1422 AllowsMergedIf); 1423 verifyFormat("MYIF (a) g();\n" 1424 "else if (b) g();\n" 1425 "else {\n" 1426 " g();\n" 1427 "}", 1428 AllowsMergedIf); 1429 verifyFormat("MYIF (a) g();\n" 1430 "else MYIF (b) {\n" 1431 " g();\n" 1432 "} else {\n" 1433 " g();\n" 1434 "}", 1435 AllowsMergedIf); 1436 verifyFormat("MYIF (a) g();\n" 1437 "else if (b) {\n" 1438 " g();\n" 1439 "} else {\n" 1440 " g();\n" 1441 "}", 1442 AllowsMergedIf); 1443 verifyFormat("MYIF (a) {\n" 1444 " g();\n" 1445 "} else MYIF (b) {\n" 1446 " g();\n" 1447 "} else {\n" 1448 " g();\n" 1449 "}", 1450 AllowsMergedIf); 1451 verifyFormat("MYIF (a) {\n" 1452 " g();\n" 1453 "} else if (b) {\n" 1454 " g();\n" 1455 "} else {\n" 1456 " g();\n" 1457 "}", 1458 AllowsMergedIf); 1459 } 1460 1461 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) { 1462 FormatStyle AllowsMergedLoops = getLLVMStyle(); 1463 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true; 1464 verifyFormat("while (true) continue;", AllowsMergedLoops); 1465 verifyFormat("for (;;) continue;", AllowsMergedLoops); 1466 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops); 1467 verifyFormat("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 verifyFormat("for (auto a = 0, b = 0; const auto &c : {1, 2, 3})", Style); 1928 verifyFormat("for (auto a = 0, b = 0; const int &c : {1, 2, 3})", Style); 1929 verifyFormat("for (auto a = 0, b = 0; const Foo &c : {1, 2, 3})", Style); 1930 verifyFormat("for (auto a = 0, b = 0; const Foo *c : {1, 2, 3})", Style); 1931 verifyFormat("for (int a = 0, b = 0; const auto &c : {1, 2, 3})", Style); 1932 verifyFormat("for (int a = 0, b = 0; const int &c : {1, 2, 3})", Style); 1933 verifyFormat("for (int a = 0, b = 0; const Foo &c : {1, 2, 3})", Style); 1934 verifyFormat("for (int a = 0, b++; const auto &c : {1, 2, 3})", Style); 1935 verifyFormat("for (int a = 0, b++; const int &c : {1, 2, 3})", Style); 1936 verifyFormat("for (int a = 0, b++; const Foo &c : {1, 2, 3})", Style); 1937 verifyFormat("for (auto x = 0; auto &c : {1, 2, 3})", Style); 1938 verifyFormat("for (auto x = 0; int &c : {1, 2, 3})", Style); 1939 verifyFormat("for (int x = 0; auto &c : {1, 2, 3})", Style); 1940 verifyFormat("for (int x = 0; int &c : {1, 2, 3})", Style); 1941 verifyFormat("for (f(); auto &c : {1, 2, 3})", Style); 1942 verifyFormat("for (f(); int &c : {1, 2, 3})", Style); 1943 1944 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 1945 verifyFormat("Const unsigned int *c;\n" 1946 "const unsigned int *d;\n" 1947 "Const unsigned int &e;\n" 1948 "const unsigned int &f;\n" 1949 "const unsigned &&g;\n" 1950 "Const unsigned h;", 1951 Style); 1952 1953 Style.PointerAlignment = FormatStyle::PAS_Left; 1954 Style.ReferenceAlignment = FormatStyle::RAS_Pointer; 1955 verifyFormat("int* f1(int* a, int& b, int&& c);", Style); 1956 verifyFormat("int& f2(int&& c, int* a, int& b);", Style); 1957 verifyFormat("int&& f3(int& b, int&& c, int* a);", Style); 1958 verifyFormat("int* f1(int& a) const& = 0;", Style); 1959 verifyFormat("int* a = f1();", Style); 1960 verifyFormat("int& b = f2();", Style); 1961 verifyFormat("int&& c = f3();", Style); 1962 verifyFormat("for (auto a = 0, b = 0; const auto& c : {1, 2, 3})", Style); 1963 verifyFormat("for (auto a = 0, b = 0; const int& c : {1, 2, 3})", Style); 1964 verifyFormat("for (auto a = 0, b = 0; const Foo& c : {1, 2, 3})", Style); 1965 verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style); 1966 verifyFormat("for (int a = 0, b = 0; const auto& c : {1, 2, 3})", Style); 1967 verifyFormat("for (int a = 0, b = 0; const int& c : {1, 2, 3})", Style); 1968 verifyFormat("for (int a = 0, b = 0; const Foo& c : {1, 2, 3})", Style); 1969 verifyFormat("for (int a = 0, b = 0; const Foo* c : {1, 2, 3})", Style); 1970 verifyFormat("for (int a = 0, b++; const auto& c : {1, 2, 3})", Style); 1971 verifyFormat("for (int a = 0, b++; const int& c : {1, 2, 3})", Style); 1972 verifyFormat("for (int a = 0, b++; const Foo& c : {1, 2, 3})", Style); 1973 verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style); 1974 verifyFormat("for (auto x = 0; auto& c : {1, 2, 3})", Style); 1975 verifyFormat("for (auto x = 0; int& c : {1, 2, 3})", Style); 1976 verifyFormat("for (int x = 0; auto& c : {1, 2, 3})", Style); 1977 verifyFormat("for (int x = 0; int& c : {1, 2, 3})", Style); 1978 verifyFormat("for (f(); auto& c : {1, 2, 3})", Style); 1979 verifyFormat("for (f(); int& c : {1, 2, 3})", Style); 1980 1981 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 1982 verifyFormat("Const unsigned int* c;\n" 1983 "const unsigned int* d;\n" 1984 "Const unsigned int& e;\n" 1985 "const unsigned int& f;\n" 1986 "const unsigned&& g;\n" 1987 "Const unsigned h;", 1988 Style); 1989 1990 Style.PointerAlignment = FormatStyle::PAS_Right; 1991 Style.ReferenceAlignment = FormatStyle::RAS_Left; 1992 verifyFormat("int *f1(int *a, int& b, int&& c);", Style); 1993 verifyFormat("int& f2(int&& c, int *a, int& b);", Style); 1994 verifyFormat("int&& f3(int& b, int&& c, int *a);", Style); 1995 verifyFormat("int *a = f1();", Style); 1996 verifyFormat("int& b = f2();", Style); 1997 verifyFormat("int&& c = f3();", Style); 1998 verifyFormat("for (auto a = 0, b = 0; const Foo *c : {1, 2, 3})", Style); 1999 verifyFormat("for (int a = 0, b = 0; const Foo *c : {1, 2, 3})", Style); 2000 verifyFormat("for (int a = 0, b++; const Foo *c : {1, 2, 3})", Style); 2001 2002 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 2003 verifyFormat("Const unsigned int *c;\n" 2004 "const unsigned int *d;\n" 2005 "Const unsigned int& e;\n" 2006 "const unsigned int& f;\n" 2007 "const unsigned g;\n" 2008 "Const unsigned h;", 2009 Style); 2010 2011 Style.PointerAlignment = FormatStyle::PAS_Left; 2012 Style.ReferenceAlignment = FormatStyle::RAS_Middle; 2013 verifyFormat("int* f1(int* a, int & b, int && c);", Style); 2014 verifyFormat("int & f2(int && c, int* a, int & b);", Style); 2015 verifyFormat("int && f3(int & b, int && c, int* a);", Style); 2016 verifyFormat("int* a = f1();", Style); 2017 verifyFormat("int & b = f2();", Style); 2018 verifyFormat("int && c = f3();", Style); 2019 verifyFormat("for (auto a = 0, b = 0; const auto & c : {1, 2, 3})", Style); 2020 verifyFormat("for (auto a = 0, b = 0; const int & c : {1, 2, 3})", Style); 2021 verifyFormat("for (auto a = 0, b = 0; const Foo & c : {1, 2, 3})", Style); 2022 verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style); 2023 verifyFormat("for (int a = 0, b++; const auto & c : {1, 2, 3})", Style); 2024 verifyFormat("for (int a = 0, b++; const int & c : {1, 2, 3})", Style); 2025 verifyFormat("for (int a = 0, b++; const Foo & c : {1, 2, 3})", Style); 2026 verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style); 2027 verifyFormat("for (auto x = 0; auto & c : {1, 2, 3})", Style); 2028 verifyFormat("for (auto x = 0; int & c : {1, 2, 3})", Style); 2029 verifyFormat("for (int x = 0; auto & c : {1, 2, 3})", Style); 2030 verifyFormat("for (int x = 0; int & c : {1, 2, 3})", Style); 2031 verifyFormat("for (f(); auto & c : {1, 2, 3})", Style); 2032 verifyFormat("for (f(); int & c : {1, 2, 3})", Style); 2033 2034 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 2035 verifyFormat("Const unsigned int* c;\n" 2036 "const unsigned int* d;\n" 2037 "Const unsigned int & e;\n" 2038 "const unsigned int & f;\n" 2039 "const unsigned && g;\n" 2040 "Const unsigned h;", 2041 Style); 2042 2043 Style.PointerAlignment = FormatStyle::PAS_Middle; 2044 Style.ReferenceAlignment = FormatStyle::RAS_Right; 2045 verifyFormat("int * f1(int * a, int &b, int &&c);", Style); 2046 verifyFormat("int &f2(int &&c, int * a, int &b);", Style); 2047 verifyFormat("int &&f3(int &b, int &&c, int * a);", Style); 2048 verifyFormat("int * a = f1();", Style); 2049 verifyFormat("int &b = f2();", Style); 2050 verifyFormat("int &&c = f3();", Style); 2051 verifyFormat("for (auto a = 0, b = 0; const Foo * c : {1, 2, 3})", Style); 2052 verifyFormat("for (int a = 0, b = 0; const Foo * c : {1, 2, 3})", Style); 2053 verifyFormat("for (int a = 0, b++; const Foo * c : {1, 2, 3})", Style); 2054 2055 // FIXME: we don't handle this yet, so output may be arbitrary until it's 2056 // specifically handled 2057 // verifyFormat("int Add2(BTree * &Root, char * szToAdd)", Style); 2058 } 2059 2060 TEST_F(FormatTest, FormatsForLoop) { 2061 verifyFormat( 2062 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 2063 " ++VeryVeryLongLoopVariable)\n" 2064 " ;"); 2065 verifyFormat("for (;;)\n" 2066 " f();"); 2067 verifyFormat("for (;;) {\n}"); 2068 verifyFormat("for (;;) {\n" 2069 " f();\n" 2070 "}"); 2071 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}"); 2072 2073 verifyFormat( 2074 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 2075 " E = UnwrappedLines.end();\n" 2076 " I != E; ++I) {\n}"); 2077 2078 verifyFormat( 2079 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" 2080 " ++IIIII) {\n}"); 2081 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" 2082 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" 2083 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); 2084 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n" 2085 " I = FD->getDeclsInPrototypeScope().begin(),\n" 2086 " E = FD->getDeclsInPrototypeScope().end();\n" 2087 " I != E; ++I) {\n}"); 2088 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n" 2089 " I = Container.begin(),\n" 2090 " E = Container.end();\n" 2091 " I != E; ++I) {\n}", 2092 getLLVMStyleWithColumns(76)); 2093 2094 verifyFormat( 2095 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 2096 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" 2097 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2098 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 2099 " ++aaaaaaaaaaa) {\n}"); 2100 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2101 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n" 2102 " ++i) {\n}"); 2103 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" 2104 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 2105 "}"); 2106 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n" 2107 " aaaaaaaaaa);\n" 2108 " iter; ++iter) {\n" 2109 "}"); 2110 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2111 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 2112 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n" 2113 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {"); 2114 2115 // These should not be formatted as Objective-C for-in loops. 2116 verifyFormat("for (Foo *x = 0; x != in; x++) {\n}"); 2117 verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}"); 2118 verifyFormat("Foo *x;\nfor (x in y) {\n}"); 2119 verifyFormat( 2120 "for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}"); 2121 2122 FormatStyle NoBinPacking = getLLVMStyle(); 2123 NoBinPacking.BinPackParameters = false; 2124 verifyFormat("for (int aaaaaaaaaaa = 1;\n" 2125 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" 2126 " aaaaaaaaaaaaaaaa,\n" 2127 " aaaaaaaaaaaaaaaa,\n" 2128 " aaaaaaaaaaaaaaaa);\n" 2129 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 2130 "}", 2131 NoBinPacking); 2132 verifyFormat( 2133 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 2134 " E = UnwrappedLines.end();\n" 2135 " I != E;\n" 2136 " ++I) {\n}", 2137 NoBinPacking); 2138 2139 FormatStyle AlignLeft = getLLVMStyle(); 2140 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 2141 verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft); 2142 } 2143 2144 TEST_F(FormatTest, RangeBasedForLoops) { 2145 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 2146 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 2147 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" 2148 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); 2149 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" 2150 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 2151 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n" 2152 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}"); 2153 } 2154 2155 TEST_F(FormatTest, ForEachLoops) { 2156 verifyFormat("void f() {\n" 2157 " foreach (Item *item, itemlist) {}\n" 2158 " Q_FOREACH (Item *item, itemlist) {}\n" 2159 " BOOST_FOREACH (Item *item, itemlist) {}\n" 2160 " UNKNOWN_FORACH(Item * item, itemlist) {}\n" 2161 "}"); 2162 2163 FormatStyle Style = getLLVMStyle(); 2164 Style.SpaceBeforeParens = 2165 FormatStyle::SBPO_ControlStatementsExceptControlMacros; 2166 verifyFormat("void f() {\n" 2167 " foreach(Item *item, itemlist) {}\n" 2168 " Q_FOREACH(Item *item, itemlist) {}\n" 2169 " BOOST_FOREACH(Item *item, itemlist) {}\n" 2170 " UNKNOWN_FORACH(Item * item, itemlist) {}\n" 2171 "}", 2172 Style); 2173 2174 // As function-like macros. 2175 verifyFormat("#define foreach(x, y)\n" 2176 "#define Q_FOREACH(x, y)\n" 2177 "#define BOOST_FOREACH(x, y)\n" 2178 "#define UNKNOWN_FOREACH(x, y)\n"); 2179 2180 // Not as function-like macros. 2181 verifyFormat("#define foreach (x, y)\n" 2182 "#define Q_FOREACH (x, y)\n" 2183 "#define BOOST_FOREACH (x, y)\n" 2184 "#define UNKNOWN_FOREACH (x, y)\n"); 2185 2186 // handle microsoft non standard extension 2187 verifyFormat("for each (char c in x->MyStringProperty)"); 2188 } 2189 2190 TEST_F(FormatTest, FormatsWhileLoop) { 2191 verifyFormat("while (true) {\n}"); 2192 verifyFormat("while (true)\n" 2193 " f();"); 2194 verifyFormat("while () {\n}"); 2195 verifyFormat("while () {\n" 2196 " f();\n" 2197 "}"); 2198 } 2199 2200 TEST_F(FormatTest, FormatsDoWhile) { 2201 verifyFormat("do {\n" 2202 " do_something();\n" 2203 "} while (something());"); 2204 verifyFormat("do\n" 2205 " do_something();\n" 2206 "while (something());"); 2207 } 2208 2209 TEST_F(FormatTest, FormatsSwitchStatement) { 2210 verifyFormat("switch (x) {\n" 2211 "case 1:\n" 2212 " f();\n" 2213 " break;\n" 2214 "case kFoo:\n" 2215 "case ns::kBar:\n" 2216 "case kBaz:\n" 2217 " break;\n" 2218 "default:\n" 2219 " g();\n" 2220 " break;\n" 2221 "}"); 2222 verifyFormat("switch (x) {\n" 2223 "case 1: {\n" 2224 " f();\n" 2225 " break;\n" 2226 "}\n" 2227 "case 2: {\n" 2228 " break;\n" 2229 "}\n" 2230 "}"); 2231 verifyFormat("switch (x) {\n" 2232 "case 1: {\n" 2233 " f();\n" 2234 " {\n" 2235 " g();\n" 2236 " h();\n" 2237 " }\n" 2238 " break;\n" 2239 "}\n" 2240 "}"); 2241 verifyFormat("switch (x) {\n" 2242 "case 1: {\n" 2243 " f();\n" 2244 " if (foo) {\n" 2245 " g();\n" 2246 " h();\n" 2247 " }\n" 2248 " break;\n" 2249 "}\n" 2250 "}"); 2251 verifyFormat("switch (x) {\n" 2252 "case 1: {\n" 2253 " f();\n" 2254 " g();\n" 2255 "} break;\n" 2256 "}"); 2257 verifyFormat("switch (test)\n" 2258 " ;"); 2259 verifyFormat("switch (x) {\n" 2260 "default: {\n" 2261 " // Do nothing.\n" 2262 "}\n" 2263 "}"); 2264 verifyFormat("switch (x) {\n" 2265 "// comment\n" 2266 "// if 1, do f()\n" 2267 "case 1:\n" 2268 " f();\n" 2269 "}"); 2270 verifyFormat("switch (x) {\n" 2271 "case 1:\n" 2272 " // Do amazing stuff\n" 2273 " {\n" 2274 " f();\n" 2275 " g();\n" 2276 " }\n" 2277 " break;\n" 2278 "}"); 2279 verifyFormat("#define A \\\n" 2280 " switch (x) { \\\n" 2281 " case a: \\\n" 2282 " foo = b; \\\n" 2283 " }", 2284 getLLVMStyleWithColumns(20)); 2285 verifyFormat("#define OPERATION_CASE(name) \\\n" 2286 " case OP_name: \\\n" 2287 " return operations::Operation##name\n", 2288 getLLVMStyleWithColumns(40)); 2289 verifyFormat("switch (x) {\n" 2290 "case 1:;\n" 2291 "default:;\n" 2292 " int i;\n" 2293 "}"); 2294 2295 verifyGoogleFormat("switch (x) {\n" 2296 " case 1:\n" 2297 " f();\n" 2298 " break;\n" 2299 " case kFoo:\n" 2300 " case ns::kBar:\n" 2301 " case kBaz:\n" 2302 " break;\n" 2303 " default:\n" 2304 " g();\n" 2305 " break;\n" 2306 "}"); 2307 verifyGoogleFormat("switch (x) {\n" 2308 " case 1: {\n" 2309 " f();\n" 2310 " break;\n" 2311 " }\n" 2312 "}"); 2313 verifyGoogleFormat("switch (test)\n" 2314 " ;"); 2315 2316 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n" 2317 " case OP_name: \\\n" 2318 " return operations::Operation##name\n"); 2319 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n" 2320 " // Get the correction operation class.\n" 2321 " switch (OpCode) {\n" 2322 " CASE(Add);\n" 2323 " CASE(Subtract);\n" 2324 " default:\n" 2325 " return operations::Unknown;\n" 2326 " }\n" 2327 "#undef OPERATION_CASE\n" 2328 "}"); 2329 verifyFormat("DEBUG({\n" 2330 " switch (x) {\n" 2331 " case A:\n" 2332 " f();\n" 2333 " break;\n" 2334 " // fallthrough\n" 2335 " case B:\n" 2336 " g();\n" 2337 " break;\n" 2338 " }\n" 2339 "});"); 2340 EXPECT_EQ("DEBUG({\n" 2341 " switch (x) {\n" 2342 " case A:\n" 2343 " f();\n" 2344 " break;\n" 2345 " // On B:\n" 2346 " case B:\n" 2347 " g();\n" 2348 " break;\n" 2349 " }\n" 2350 "});", 2351 format("DEBUG({\n" 2352 " switch (x) {\n" 2353 " case A:\n" 2354 " f();\n" 2355 " break;\n" 2356 " // On B:\n" 2357 " case B:\n" 2358 " g();\n" 2359 " break;\n" 2360 " }\n" 2361 "});", 2362 getLLVMStyle())); 2363 EXPECT_EQ("switch (n) {\n" 2364 "case 0: {\n" 2365 " return false;\n" 2366 "}\n" 2367 "default: {\n" 2368 " return true;\n" 2369 "}\n" 2370 "}", 2371 format("switch (n)\n" 2372 "{\n" 2373 "case 0: {\n" 2374 " return false;\n" 2375 "}\n" 2376 "default: {\n" 2377 " return true;\n" 2378 "}\n" 2379 "}", 2380 getLLVMStyle())); 2381 verifyFormat("switch (a) {\n" 2382 "case (b):\n" 2383 " return;\n" 2384 "}"); 2385 2386 verifyFormat("switch (a) {\n" 2387 "case some_namespace::\n" 2388 " some_constant:\n" 2389 " return;\n" 2390 "}", 2391 getLLVMStyleWithColumns(34)); 2392 2393 FormatStyle Style = getLLVMStyle(); 2394 Style.IndentCaseLabels = true; 2395 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 2396 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2397 Style.BraceWrapping.AfterCaseLabel = true; 2398 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 2399 EXPECT_EQ("switch (n)\n" 2400 "{\n" 2401 " case 0:\n" 2402 " {\n" 2403 " return false;\n" 2404 " }\n" 2405 " default:\n" 2406 " {\n" 2407 " return true;\n" 2408 " }\n" 2409 "}", 2410 format("switch (n) {\n" 2411 " case 0: {\n" 2412 " return false;\n" 2413 " }\n" 2414 " default: {\n" 2415 " return true;\n" 2416 " }\n" 2417 "}", 2418 Style)); 2419 Style.BraceWrapping.AfterCaseLabel = false; 2420 EXPECT_EQ("switch (n)\n" 2421 "{\n" 2422 " case 0: {\n" 2423 " return false;\n" 2424 " }\n" 2425 " default: {\n" 2426 " return true;\n" 2427 " }\n" 2428 "}", 2429 format("switch (n) {\n" 2430 " case 0:\n" 2431 " {\n" 2432 " return false;\n" 2433 " }\n" 2434 " default:\n" 2435 " {\n" 2436 " return true;\n" 2437 " }\n" 2438 "}", 2439 Style)); 2440 Style.IndentCaseLabels = false; 2441 Style.IndentCaseBlocks = true; 2442 EXPECT_EQ("switch (n)\n" 2443 "{\n" 2444 "case 0:\n" 2445 " {\n" 2446 " return false;\n" 2447 " }\n" 2448 "case 1:\n" 2449 " break;\n" 2450 "default:\n" 2451 " {\n" 2452 " return true;\n" 2453 " }\n" 2454 "}", 2455 format("switch (n) {\n" 2456 "case 0: {\n" 2457 " return false;\n" 2458 "}\n" 2459 "case 1:\n" 2460 " break;\n" 2461 "default: {\n" 2462 " return true;\n" 2463 "}\n" 2464 "}", 2465 Style)); 2466 Style.IndentCaseLabels = true; 2467 Style.IndentCaseBlocks = true; 2468 EXPECT_EQ("switch (n)\n" 2469 "{\n" 2470 " case 0:\n" 2471 " {\n" 2472 " return false;\n" 2473 " }\n" 2474 " case 1:\n" 2475 " break;\n" 2476 " default:\n" 2477 " {\n" 2478 " return true;\n" 2479 " }\n" 2480 "}", 2481 format("switch (n) {\n" 2482 "case 0: {\n" 2483 " return false;\n" 2484 "}\n" 2485 "case 1:\n" 2486 " break;\n" 2487 "default: {\n" 2488 " return true;\n" 2489 "}\n" 2490 "}", 2491 Style)); 2492 } 2493 2494 TEST_F(FormatTest, CaseRanges) { 2495 verifyFormat("switch (x) {\n" 2496 "case 'A' ... 'Z':\n" 2497 "case 1 ... 5:\n" 2498 "case a ... b:\n" 2499 " break;\n" 2500 "}"); 2501 } 2502 2503 TEST_F(FormatTest, ShortEnums) { 2504 FormatStyle Style = getLLVMStyle(); 2505 Style.AllowShortEnumsOnASingleLine = true; 2506 verifyFormat("enum { A, B, C } ShortEnum1, ShortEnum2;", Style); 2507 Style.AllowShortEnumsOnASingleLine = false; 2508 verifyFormat("enum {\n" 2509 " A,\n" 2510 " B,\n" 2511 " C\n" 2512 "} ShortEnum1, ShortEnum2;", 2513 Style); 2514 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2515 Style.BraceWrapping.AfterEnum = true; 2516 verifyFormat("enum\n" 2517 "{\n" 2518 " A,\n" 2519 " B,\n" 2520 " C\n" 2521 "} ShortEnum1, ShortEnum2;", 2522 Style); 2523 } 2524 2525 TEST_F(FormatTest, ShortCaseLabels) { 2526 FormatStyle Style = getLLVMStyle(); 2527 Style.AllowShortCaseLabelsOnASingleLine = true; 2528 verifyFormat("switch (a) {\n" 2529 "case 1: x = 1; break;\n" 2530 "case 2: return;\n" 2531 "case 3:\n" 2532 "case 4:\n" 2533 "case 5: return;\n" 2534 "case 6: // comment\n" 2535 " return;\n" 2536 "case 7:\n" 2537 " // comment\n" 2538 " return;\n" 2539 "case 8:\n" 2540 " x = 8; // comment\n" 2541 " break;\n" 2542 "default: y = 1; break;\n" 2543 "}", 2544 Style); 2545 verifyFormat("switch (a) {\n" 2546 "case 0: return; // comment\n" 2547 "case 1: break; // comment\n" 2548 "case 2: return;\n" 2549 "// comment\n" 2550 "case 3: return;\n" 2551 "// comment 1\n" 2552 "// comment 2\n" 2553 "// comment 3\n" 2554 "case 4: break; /* comment */\n" 2555 "case 5:\n" 2556 " // comment\n" 2557 " break;\n" 2558 "case 6: /* comment */ x = 1; break;\n" 2559 "case 7: x = /* comment */ 1; break;\n" 2560 "case 8:\n" 2561 " x = 1; /* comment */\n" 2562 " break;\n" 2563 "case 9:\n" 2564 " break; // comment line 1\n" 2565 " // comment line 2\n" 2566 "}", 2567 Style); 2568 EXPECT_EQ("switch (a) {\n" 2569 "case 1:\n" 2570 " x = 8;\n" 2571 " // fall through\n" 2572 "case 2: x = 8;\n" 2573 "// comment\n" 2574 "case 3:\n" 2575 " return; /* comment line 1\n" 2576 " * comment line 2 */\n" 2577 "case 4: i = 8;\n" 2578 "// something else\n" 2579 "#if FOO\n" 2580 "case 5: break;\n" 2581 "#endif\n" 2582 "}", 2583 format("switch (a) {\n" 2584 "case 1: x = 8;\n" 2585 " // fall through\n" 2586 "case 2:\n" 2587 " x = 8;\n" 2588 "// comment\n" 2589 "case 3:\n" 2590 " return; /* comment line 1\n" 2591 " * comment line 2 */\n" 2592 "case 4:\n" 2593 " i = 8;\n" 2594 "// something else\n" 2595 "#if FOO\n" 2596 "case 5: break;\n" 2597 "#endif\n" 2598 "}", 2599 Style)); 2600 EXPECT_EQ("switch (a) {\n" 2601 "case 0:\n" 2602 " return; // long long long long long long long long long long " 2603 "long long comment\n" 2604 " // line\n" 2605 "}", 2606 format("switch (a) {\n" 2607 "case 0: return; // long long long long long long long long " 2608 "long long long long comment line\n" 2609 "}", 2610 Style)); 2611 EXPECT_EQ("switch (a) {\n" 2612 "case 0:\n" 2613 " return; /* long long long long long long long long long long " 2614 "long long comment\n" 2615 " line */\n" 2616 "}", 2617 format("switch (a) {\n" 2618 "case 0: return; /* long long long long long long long long " 2619 "long long long long comment line */\n" 2620 "}", 2621 Style)); 2622 verifyFormat("switch (a) {\n" 2623 "#if FOO\n" 2624 "case 0: return 0;\n" 2625 "#endif\n" 2626 "}", 2627 Style); 2628 verifyFormat("switch (a) {\n" 2629 "case 1: {\n" 2630 "}\n" 2631 "case 2: {\n" 2632 " return;\n" 2633 "}\n" 2634 "case 3: {\n" 2635 " x = 1;\n" 2636 " return;\n" 2637 "}\n" 2638 "case 4:\n" 2639 " if (x)\n" 2640 " return;\n" 2641 "}", 2642 Style); 2643 Style.ColumnLimit = 21; 2644 verifyFormat("switch (a) {\n" 2645 "case 1: x = 1; break;\n" 2646 "case 2: return;\n" 2647 "case 3:\n" 2648 "case 4:\n" 2649 "case 5: return;\n" 2650 "default:\n" 2651 " y = 1;\n" 2652 " break;\n" 2653 "}", 2654 Style); 2655 Style.ColumnLimit = 80; 2656 Style.AllowShortCaseLabelsOnASingleLine = false; 2657 Style.IndentCaseLabels = true; 2658 EXPECT_EQ("switch (n) {\n" 2659 " default /*comments*/:\n" 2660 " return true;\n" 2661 " case 0:\n" 2662 " return false;\n" 2663 "}", 2664 format("switch (n) {\n" 2665 "default/*comments*/:\n" 2666 " return true;\n" 2667 "case 0:\n" 2668 " return false;\n" 2669 "}", 2670 Style)); 2671 Style.AllowShortCaseLabelsOnASingleLine = true; 2672 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2673 Style.BraceWrapping.AfterCaseLabel = true; 2674 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 2675 EXPECT_EQ("switch (n)\n" 2676 "{\n" 2677 " case 0:\n" 2678 " {\n" 2679 " return false;\n" 2680 " }\n" 2681 " default:\n" 2682 " {\n" 2683 " return true;\n" 2684 " }\n" 2685 "}", 2686 format("switch (n) {\n" 2687 " case 0: {\n" 2688 " return false;\n" 2689 " }\n" 2690 " default:\n" 2691 " {\n" 2692 " return true;\n" 2693 " }\n" 2694 "}", 2695 Style)); 2696 } 2697 2698 TEST_F(FormatTest, FormatsLabels) { 2699 verifyFormat("void f() {\n" 2700 " some_code();\n" 2701 "test_label:\n" 2702 " some_other_code();\n" 2703 " {\n" 2704 " some_more_code();\n" 2705 " another_label:\n" 2706 " some_more_code();\n" 2707 " }\n" 2708 "}"); 2709 verifyFormat("{\n" 2710 " some_code();\n" 2711 "test_label:\n" 2712 " some_other_code();\n" 2713 "}"); 2714 verifyFormat("{\n" 2715 " some_code();\n" 2716 "test_label:;\n" 2717 " int i = 0;\n" 2718 "}"); 2719 FormatStyle Style = getLLVMStyle(); 2720 Style.IndentGotoLabels = false; 2721 verifyFormat("void f() {\n" 2722 " some_code();\n" 2723 "test_label:\n" 2724 " some_other_code();\n" 2725 " {\n" 2726 " some_more_code();\n" 2727 "another_label:\n" 2728 " some_more_code();\n" 2729 " }\n" 2730 "}", 2731 Style); 2732 verifyFormat("{\n" 2733 " some_code();\n" 2734 "test_label:\n" 2735 " some_other_code();\n" 2736 "}", 2737 Style); 2738 verifyFormat("{\n" 2739 " some_code();\n" 2740 "test_label:;\n" 2741 " int i = 0;\n" 2742 "}"); 2743 } 2744 2745 TEST_F(FormatTest, MultiLineControlStatements) { 2746 FormatStyle Style = getLLVMStyle(); 2747 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom; 2748 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine; 2749 Style.ColumnLimit = 20; 2750 // Short lines should keep opening brace on same line. 2751 EXPECT_EQ("if (foo) {\n" 2752 " bar();\n" 2753 "}", 2754 format("if(foo){bar();}", Style)); 2755 EXPECT_EQ("if (foo) {\n" 2756 " bar();\n" 2757 "} else {\n" 2758 " baz();\n" 2759 "}", 2760 format("if(foo){bar();}else{baz();}", Style)); 2761 EXPECT_EQ("if (foo && bar) {\n" 2762 " baz();\n" 2763 "}", 2764 format("if(foo&&bar){baz();}", Style)); 2765 EXPECT_EQ("if (foo) {\n" 2766 " bar();\n" 2767 "} else if (baz) {\n" 2768 " quux();\n" 2769 "}", 2770 format("if(foo){bar();}else if(baz){quux();}", Style)); 2771 EXPECT_EQ( 2772 "if (foo) {\n" 2773 " bar();\n" 2774 "} else if (baz) {\n" 2775 " quux();\n" 2776 "} else {\n" 2777 " foobar();\n" 2778 "}", 2779 format("if(foo){bar();}else if(baz){quux();}else{foobar();}", Style)); 2780 EXPECT_EQ("for (;;) {\n" 2781 " foo();\n" 2782 "}", 2783 format("for(;;){foo();}")); 2784 EXPECT_EQ("while (1) {\n" 2785 " foo();\n" 2786 "}", 2787 format("while(1){foo();}", Style)); 2788 EXPECT_EQ("switch (foo) {\n" 2789 "case bar:\n" 2790 " return;\n" 2791 "}", 2792 format("switch(foo){case bar:return;}", Style)); 2793 EXPECT_EQ("try {\n" 2794 " foo();\n" 2795 "} catch (...) {\n" 2796 " bar();\n" 2797 "}", 2798 format("try{foo();}catch(...){bar();}", Style)); 2799 EXPECT_EQ("do {\n" 2800 " foo();\n" 2801 "} while (bar &&\n" 2802 " baz);", 2803 format("do{foo();}while(bar&&baz);", Style)); 2804 // Long lines should put opening brace on new line. 2805 EXPECT_EQ("if (foo && bar &&\n" 2806 " baz)\n" 2807 "{\n" 2808 " quux();\n" 2809 "}", 2810 format("if(foo&&bar&&baz){quux();}", Style)); 2811 EXPECT_EQ("if (foo && bar &&\n" 2812 " baz)\n" 2813 "{\n" 2814 " quux();\n" 2815 "}", 2816 format("if (foo && bar &&\n" 2817 " baz) {\n" 2818 " quux();\n" 2819 "}", 2820 Style)); 2821 EXPECT_EQ("if (foo) {\n" 2822 " bar();\n" 2823 "} else if (baz ||\n" 2824 " quux)\n" 2825 "{\n" 2826 " foobar();\n" 2827 "}", 2828 format("if(foo){bar();}else if(baz||quux){foobar();}", Style)); 2829 EXPECT_EQ( 2830 "if (foo) {\n" 2831 " bar();\n" 2832 "} else if (baz ||\n" 2833 " quux)\n" 2834 "{\n" 2835 " foobar();\n" 2836 "} else {\n" 2837 " barbaz();\n" 2838 "}", 2839 format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}", 2840 Style)); 2841 EXPECT_EQ("for (int i = 0;\n" 2842 " i < 10; ++i)\n" 2843 "{\n" 2844 " foo();\n" 2845 "}", 2846 format("for(int i=0;i<10;++i){foo();}", Style)); 2847 EXPECT_EQ("foreach (int i,\n" 2848 " list)\n" 2849 "{\n" 2850 " foo();\n" 2851 "}", 2852 format("foreach(int i, list){foo();}", Style)); 2853 Style.ColumnLimit = 2854 40; // to concentrate at brace wrapping, not line wrap due to column limit 2855 EXPECT_EQ("foreach (int i, list) {\n" 2856 " foo();\n" 2857 "}", 2858 format("foreach(int i, list){foo();}", Style)); 2859 Style.ColumnLimit = 2860 20; // to concentrate at brace wrapping, not line wrap due to column limit 2861 EXPECT_EQ("while (foo || bar ||\n" 2862 " baz)\n" 2863 "{\n" 2864 " quux();\n" 2865 "}", 2866 format("while(foo||bar||baz){quux();}", Style)); 2867 EXPECT_EQ("switch (\n" 2868 " foo = barbaz)\n" 2869 "{\n" 2870 "case quux:\n" 2871 " return;\n" 2872 "}", 2873 format("switch(foo=barbaz){case quux:return;}", Style)); 2874 EXPECT_EQ("try {\n" 2875 " foo();\n" 2876 "} catch (\n" 2877 " Exception &bar)\n" 2878 "{\n" 2879 " baz();\n" 2880 "}", 2881 format("try{foo();}catch(Exception&bar){baz();}", Style)); 2882 Style.ColumnLimit = 2883 40; // to concentrate at brace wrapping, not line wrap due to column limit 2884 EXPECT_EQ("try {\n" 2885 " foo();\n" 2886 "} catch (Exception &bar) {\n" 2887 " baz();\n" 2888 "}", 2889 format("try{foo();}catch(Exception&bar){baz();}", Style)); 2890 Style.ColumnLimit = 2891 20; // to concentrate at brace wrapping, not line wrap due to column limit 2892 2893 Style.BraceWrapping.BeforeElse = true; 2894 EXPECT_EQ( 2895 "if (foo) {\n" 2896 " bar();\n" 2897 "}\n" 2898 "else if (baz ||\n" 2899 " quux)\n" 2900 "{\n" 2901 " foobar();\n" 2902 "}\n" 2903 "else {\n" 2904 " barbaz();\n" 2905 "}", 2906 format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}", 2907 Style)); 2908 2909 Style.BraceWrapping.BeforeCatch = true; 2910 EXPECT_EQ("try {\n" 2911 " foo();\n" 2912 "}\n" 2913 "catch (...) {\n" 2914 " baz();\n" 2915 "}", 2916 format("try{foo();}catch(...){baz();}", Style)); 2917 2918 Style.BraceWrapping.AfterFunction = true; 2919 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine; 2920 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 2921 Style.ColumnLimit = 80; 2922 verifyFormat("void shortfunction() { bar(); }", Style); 2923 2924 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 2925 verifyFormat("void shortfunction()\n" 2926 "{\n" 2927 " bar();\n" 2928 "}", 2929 Style); 2930 } 2931 2932 TEST_F(FormatTest, BeforeWhile) { 2933 FormatStyle Style = getLLVMStyle(); 2934 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom; 2935 2936 verifyFormat("do {\n" 2937 " foo();\n" 2938 "} while (1);", 2939 Style); 2940 Style.BraceWrapping.BeforeWhile = true; 2941 verifyFormat("do {\n" 2942 " foo();\n" 2943 "}\n" 2944 "while (1);", 2945 Style); 2946 } 2947 2948 //===----------------------------------------------------------------------===// 2949 // Tests for classes, namespaces, etc. 2950 //===----------------------------------------------------------------------===// 2951 2952 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 2953 verifyFormat("class A {};"); 2954 } 2955 2956 TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 2957 verifyFormat("class A {\n" 2958 "public:\n" 2959 "public: // comment\n" 2960 "protected:\n" 2961 "private:\n" 2962 " void f() {}\n" 2963 "};"); 2964 verifyFormat("export class A {\n" 2965 "public:\n" 2966 "public: // comment\n" 2967 "protected:\n" 2968 "private:\n" 2969 " void f() {}\n" 2970 "};"); 2971 verifyGoogleFormat("class A {\n" 2972 " public:\n" 2973 " protected:\n" 2974 " private:\n" 2975 " void f() {}\n" 2976 "};"); 2977 verifyGoogleFormat("export class A {\n" 2978 " public:\n" 2979 " protected:\n" 2980 " private:\n" 2981 " void f() {}\n" 2982 "};"); 2983 verifyFormat("class A {\n" 2984 "public slots:\n" 2985 " void f1() {}\n" 2986 "public Q_SLOTS:\n" 2987 " void f2() {}\n" 2988 "protected slots:\n" 2989 " void f3() {}\n" 2990 "protected Q_SLOTS:\n" 2991 " void f4() {}\n" 2992 "private slots:\n" 2993 " void f5() {}\n" 2994 "private Q_SLOTS:\n" 2995 " void f6() {}\n" 2996 "signals:\n" 2997 " void g1();\n" 2998 "Q_SIGNALS:\n" 2999 " void g2();\n" 3000 "};"); 3001 3002 // Don't interpret 'signals' the wrong way. 3003 verifyFormat("signals.set();"); 3004 verifyFormat("for (Signals signals : f()) {\n}"); 3005 verifyFormat("{\n" 3006 " signals.set(); // This needs indentation.\n" 3007 "}"); 3008 verifyFormat("void f() {\n" 3009 "label:\n" 3010 " signals.baz();\n" 3011 "}"); 3012 } 3013 3014 TEST_F(FormatTest, SeparatesLogicalBlocks) { 3015 EXPECT_EQ("class A {\n" 3016 "public:\n" 3017 " void f();\n" 3018 "\n" 3019 "private:\n" 3020 " void g() {}\n" 3021 " // test\n" 3022 "protected:\n" 3023 " int h;\n" 3024 "};", 3025 format("class A {\n" 3026 "public:\n" 3027 "void f();\n" 3028 "private:\n" 3029 "void g() {}\n" 3030 "// test\n" 3031 "protected:\n" 3032 "int h;\n" 3033 "};")); 3034 EXPECT_EQ("class A {\n" 3035 "protected:\n" 3036 "public:\n" 3037 " void f();\n" 3038 "};", 3039 format("class A {\n" 3040 "protected:\n" 3041 "\n" 3042 "public:\n" 3043 "\n" 3044 " void f();\n" 3045 "};")); 3046 3047 // Even ensure proper spacing inside macros. 3048 EXPECT_EQ("#define B \\\n" 3049 " class A { \\\n" 3050 " protected: \\\n" 3051 " public: \\\n" 3052 " void f(); \\\n" 3053 " };", 3054 format("#define B \\\n" 3055 " class A { \\\n" 3056 " protected: \\\n" 3057 " \\\n" 3058 " public: \\\n" 3059 " \\\n" 3060 " void f(); \\\n" 3061 " };", 3062 getGoogleStyle())); 3063 // But don't remove empty lines after macros ending in access specifiers. 3064 EXPECT_EQ("#define A private:\n" 3065 "\n" 3066 "int i;", 3067 format("#define A private:\n" 3068 "\n" 3069 "int i;")); 3070 } 3071 3072 TEST_F(FormatTest, FormatsClasses) { 3073 verifyFormat("class A : public B {};"); 3074 verifyFormat("class A : public ::B {};"); 3075 3076 verifyFormat( 3077 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3078 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 3079 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 3080 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3081 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 3082 verifyFormat( 3083 "class A : public B, public C, public D, public E, public F {};"); 3084 verifyFormat("class AAAAAAAAAAAA : public B,\n" 3085 " public C,\n" 3086 " public D,\n" 3087 " public E,\n" 3088 " public F,\n" 3089 " public G {};"); 3090 3091 verifyFormat("class\n" 3092 " ReallyReallyLongClassName {\n" 3093 " int i;\n" 3094 "};", 3095 getLLVMStyleWithColumns(32)); 3096 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 3097 " aaaaaaaaaaaaaaaa> {};"); 3098 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" 3099 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n" 3100 " aaaaaaaaaaaaaaaaaaaaaa> {};"); 3101 verifyFormat("template <class R, class C>\n" 3102 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n" 3103 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};"); 3104 verifyFormat("class ::A::B {};"); 3105 } 3106 3107 TEST_F(FormatTest, BreakInheritanceStyle) { 3108 FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle(); 3109 StyleWithInheritanceBreakBeforeComma.BreakInheritanceList = 3110 FormatStyle::BILS_BeforeComma; 3111 verifyFormat("class MyClass : public X {};", 3112 StyleWithInheritanceBreakBeforeComma); 3113 verifyFormat("class MyClass\n" 3114 " : public X\n" 3115 " , public Y {};", 3116 StyleWithInheritanceBreakBeforeComma); 3117 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n" 3118 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n" 3119 " , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", 3120 StyleWithInheritanceBreakBeforeComma); 3121 verifyFormat("struct aaaaaaaaaaaaa\n" 3122 " : public aaaaaaaaaaaaaaaaaaa< // break\n" 3123 " aaaaaaaaaaaaaaaa> {};", 3124 StyleWithInheritanceBreakBeforeComma); 3125 3126 FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle(); 3127 StyleWithInheritanceBreakAfterColon.BreakInheritanceList = 3128 FormatStyle::BILS_AfterColon; 3129 verifyFormat("class MyClass : public X {};", 3130 StyleWithInheritanceBreakAfterColon); 3131 verifyFormat("class MyClass : public X, public Y {};", 3132 StyleWithInheritanceBreakAfterColon); 3133 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n" 3134 " public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3135 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", 3136 StyleWithInheritanceBreakAfterColon); 3137 verifyFormat("struct aaaaaaaaaaaaa :\n" 3138 " public aaaaaaaaaaaaaaaaaaa< // break\n" 3139 " aaaaaaaaaaaaaaaa> {};", 3140 StyleWithInheritanceBreakAfterColon); 3141 3142 FormatStyle StyleWithInheritanceBreakAfterComma = getLLVMStyle(); 3143 StyleWithInheritanceBreakAfterComma.BreakInheritanceList = 3144 FormatStyle::BILS_AfterComma; 3145 verifyFormat("class MyClass : public X {};", 3146 StyleWithInheritanceBreakAfterComma); 3147 verifyFormat("class MyClass : public X,\n" 3148 " public Y {};", 3149 StyleWithInheritanceBreakAfterComma); 3150 verifyFormat( 3151 "class AAAAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3152 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC " 3153 "{};", 3154 StyleWithInheritanceBreakAfterComma); 3155 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 3156 " aaaaaaaaaaaaaaaa> {};", 3157 StyleWithInheritanceBreakAfterComma); 3158 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 3159 " : public OnceBreak,\n" 3160 " public AlwaysBreak,\n" 3161 " EvenBasesFitInOneLine {};", 3162 StyleWithInheritanceBreakAfterComma); 3163 } 3164 3165 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { 3166 verifyFormat("class A {\n} a, b;"); 3167 verifyFormat("struct A {\n} a, b;"); 3168 verifyFormat("union A {\n} a;"); 3169 } 3170 3171 TEST_F(FormatTest, FormatsEnum) { 3172 verifyFormat("enum {\n" 3173 " Zero,\n" 3174 " One = 1,\n" 3175 " Two = One + 1,\n" 3176 " Three = (One + Two),\n" 3177 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3178 " Five = (One, Two, Three, Four, 5)\n" 3179 "};"); 3180 verifyGoogleFormat("enum {\n" 3181 " Zero,\n" 3182 " One = 1,\n" 3183 " Two = One + 1,\n" 3184 " Three = (One + Two),\n" 3185 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3186 " Five = (One, Two, Three, Four, 5)\n" 3187 "};"); 3188 verifyFormat("enum Enum {};"); 3189 verifyFormat("enum {};"); 3190 verifyFormat("enum X E {} d;"); 3191 verifyFormat("enum __attribute__((...)) E {} d;"); 3192 verifyFormat("enum __declspec__((...)) E {} d;"); 3193 verifyFormat("enum {\n" 3194 " Bar = Foo<int, int>::value\n" 3195 "};", 3196 getLLVMStyleWithColumns(30)); 3197 3198 verifyFormat("enum ShortEnum { A, B, C };"); 3199 verifyGoogleFormat("enum ShortEnum { A, B, C };"); 3200 3201 EXPECT_EQ("enum KeepEmptyLines {\n" 3202 " ONE,\n" 3203 "\n" 3204 " TWO,\n" 3205 "\n" 3206 " THREE\n" 3207 "}", 3208 format("enum KeepEmptyLines {\n" 3209 " ONE,\n" 3210 "\n" 3211 " TWO,\n" 3212 "\n" 3213 "\n" 3214 " THREE\n" 3215 "}")); 3216 verifyFormat("enum E { // comment\n" 3217 " ONE,\n" 3218 " TWO\n" 3219 "};\n" 3220 "int i;"); 3221 3222 FormatStyle EightIndent = getLLVMStyle(); 3223 EightIndent.IndentWidth = 8; 3224 verifyFormat("enum {\n" 3225 " VOID,\n" 3226 " CHAR,\n" 3227 " SHORT,\n" 3228 " INT,\n" 3229 " LONG,\n" 3230 " SIGNED,\n" 3231 " UNSIGNED,\n" 3232 " BOOL,\n" 3233 " FLOAT,\n" 3234 " DOUBLE,\n" 3235 " COMPLEX\n" 3236 "};", 3237 EightIndent); 3238 3239 // Not enums. 3240 verifyFormat("enum X f() {\n" 3241 " a();\n" 3242 " return 42;\n" 3243 "}"); 3244 verifyFormat("enum X Type::f() {\n" 3245 " a();\n" 3246 " return 42;\n" 3247 "}"); 3248 verifyFormat("enum ::X f() {\n" 3249 " a();\n" 3250 " return 42;\n" 3251 "}"); 3252 verifyFormat("enum ns::X f() {\n" 3253 " a();\n" 3254 " return 42;\n" 3255 "}"); 3256 } 3257 3258 TEST_F(FormatTest, FormatsEnumsWithErrors) { 3259 verifyFormat("enum Type {\n" 3260 " One = 0; // These semicolons should be commas.\n" 3261 " Two = 1;\n" 3262 "};"); 3263 verifyFormat("namespace n {\n" 3264 "enum Type {\n" 3265 " One,\n" 3266 " Two, // missing };\n" 3267 " int i;\n" 3268 "}\n" 3269 "void g() {}"); 3270 } 3271 3272 TEST_F(FormatTest, FormatsEnumStruct) { 3273 verifyFormat("enum struct {\n" 3274 " Zero,\n" 3275 " One = 1,\n" 3276 " Two = One + 1,\n" 3277 " Three = (One + Two),\n" 3278 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3279 " Five = (One, Two, Three, Four, 5)\n" 3280 "};"); 3281 verifyFormat("enum struct Enum {};"); 3282 verifyFormat("enum struct {};"); 3283 verifyFormat("enum struct X E {} d;"); 3284 verifyFormat("enum struct __attribute__((...)) E {} d;"); 3285 verifyFormat("enum struct __declspec__((...)) E {} d;"); 3286 verifyFormat("enum struct X f() {\n a();\n return 42;\n}"); 3287 } 3288 3289 TEST_F(FormatTest, FormatsEnumClass) { 3290 verifyFormat("enum class {\n" 3291 " Zero,\n" 3292 " One = 1,\n" 3293 " Two = One + 1,\n" 3294 " Three = (One + Two),\n" 3295 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3296 " Five = (One, Two, Three, Four, 5)\n" 3297 "};"); 3298 verifyFormat("enum class Enum {};"); 3299 verifyFormat("enum class {};"); 3300 verifyFormat("enum class X E {} d;"); 3301 verifyFormat("enum class __attribute__((...)) E {} d;"); 3302 verifyFormat("enum class __declspec__((...)) E {} d;"); 3303 verifyFormat("enum class X f() {\n a();\n return 42;\n}"); 3304 } 3305 3306 TEST_F(FormatTest, FormatsEnumTypes) { 3307 verifyFormat("enum X : int {\n" 3308 " A, // Force multiple lines.\n" 3309 " B\n" 3310 "};"); 3311 verifyFormat("enum X : int { A, B };"); 3312 verifyFormat("enum X : std::uint32_t { A, B };"); 3313 } 3314 3315 TEST_F(FormatTest, FormatsTypedefEnum) { 3316 FormatStyle Style = getLLVMStyle(); 3317 Style.ColumnLimit = 40; 3318 verifyFormat("typedef enum {} EmptyEnum;"); 3319 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 3320 verifyFormat("typedef enum {\n" 3321 " ZERO = 0,\n" 3322 " ONE = 1,\n" 3323 " TWO = 2,\n" 3324 " THREE = 3\n" 3325 "} LongEnum;", 3326 Style); 3327 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3328 Style.BraceWrapping.AfterEnum = true; 3329 verifyFormat("typedef enum {} EmptyEnum;"); 3330 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 3331 verifyFormat("typedef enum\n" 3332 "{\n" 3333 " ZERO = 0,\n" 3334 " ONE = 1,\n" 3335 " TWO = 2,\n" 3336 " THREE = 3\n" 3337 "} LongEnum;", 3338 Style); 3339 } 3340 3341 TEST_F(FormatTest, FormatsNSEnums) { 3342 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }"); 3343 verifyGoogleFormat( 3344 "typedef NS_CLOSED_ENUM(NSInteger, SomeName) { AAA, BBB }"); 3345 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n" 3346 " // Information about someDecentlyLongValue.\n" 3347 " someDecentlyLongValue,\n" 3348 " // Information about anotherDecentlyLongValue.\n" 3349 " anotherDecentlyLongValue,\n" 3350 " // Information about aThirdDecentlyLongValue.\n" 3351 " aThirdDecentlyLongValue\n" 3352 "};"); 3353 verifyGoogleFormat("typedef NS_CLOSED_ENUM(NSInteger, MyType) {\n" 3354 " // Information about someDecentlyLongValue.\n" 3355 " someDecentlyLongValue,\n" 3356 " // Information about anotherDecentlyLongValue.\n" 3357 " anotherDecentlyLongValue,\n" 3358 " // Information about aThirdDecentlyLongValue.\n" 3359 " aThirdDecentlyLongValue\n" 3360 "};"); 3361 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n" 3362 " a = 1,\n" 3363 " b = 2,\n" 3364 " c = 3,\n" 3365 "};"); 3366 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n" 3367 " a = 1,\n" 3368 " b = 2,\n" 3369 " c = 3,\n" 3370 "};"); 3371 verifyGoogleFormat("typedef CF_CLOSED_ENUM(NSInteger, MyType) {\n" 3372 " a = 1,\n" 3373 " b = 2,\n" 3374 " c = 3,\n" 3375 "};"); 3376 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n" 3377 " a = 1,\n" 3378 " b = 2,\n" 3379 " c = 3,\n" 3380 "};"); 3381 } 3382 3383 TEST_F(FormatTest, FormatsBitfields) { 3384 verifyFormat("struct Bitfields {\n" 3385 " unsigned sClass : 8;\n" 3386 " unsigned ValueKind : 2;\n" 3387 "};"); 3388 verifyFormat("struct A {\n" 3389 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" 3390 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" 3391 "};"); 3392 verifyFormat("struct MyStruct {\n" 3393 " uchar data;\n" 3394 " uchar : 8;\n" 3395 " uchar : 8;\n" 3396 " uchar other;\n" 3397 "};"); 3398 FormatStyle Style = getLLVMStyle(); 3399 Style.BitFieldColonSpacing = FormatStyle::BFCS_None; 3400 verifyFormat("struct Bitfields {\n" 3401 " unsigned sClass:8;\n" 3402 " unsigned ValueKind:2;\n" 3403 " uchar other;\n" 3404 "};", 3405 Style); 3406 verifyFormat("struct A {\n" 3407 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1,\n" 3408 " bbbbbbbbbbbbbbbbbbbbbbbbb:2;\n" 3409 "};", 3410 Style); 3411 Style.BitFieldColonSpacing = FormatStyle::BFCS_Before; 3412 verifyFormat("struct Bitfields {\n" 3413 " unsigned sClass :8;\n" 3414 " unsigned ValueKind :2;\n" 3415 " uchar other;\n" 3416 "};", 3417 Style); 3418 Style.BitFieldColonSpacing = FormatStyle::BFCS_After; 3419 verifyFormat("struct Bitfields {\n" 3420 " unsigned sClass: 8;\n" 3421 " unsigned ValueKind: 2;\n" 3422 " uchar other;\n" 3423 "};", 3424 Style); 3425 } 3426 3427 TEST_F(FormatTest, FormatsNamespaces) { 3428 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 3429 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 3430 3431 verifyFormat("namespace some_namespace {\n" 3432 "class A {};\n" 3433 "void f() { f(); }\n" 3434 "}", 3435 LLVMWithNoNamespaceFix); 3436 verifyFormat("namespace N::inline D {\n" 3437 "class A {};\n" 3438 "void f() { f(); }\n" 3439 "}", 3440 LLVMWithNoNamespaceFix); 3441 verifyFormat("namespace N::inline D::E {\n" 3442 "class A {};\n" 3443 "void f() { f(); }\n" 3444 "}", 3445 LLVMWithNoNamespaceFix); 3446 verifyFormat("namespace [[deprecated(\"foo[bar\")]] some_namespace {\n" 3447 "class A {};\n" 3448 "void f() { f(); }\n" 3449 "}", 3450 LLVMWithNoNamespaceFix); 3451 verifyFormat("/* something */ namespace some_namespace {\n" 3452 "class A {};\n" 3453 "void f() { f(); }\n" 3454 "}", 3455 LLVMWithNoNamespaceFix); 3456 verifyFormat("namespace {\n" 3457 "class A {};\n" 3458 "void f() { f(); }\n" 3459 "}", 3460 LLVMWithNoNamespaceFix); 3461 verifyFormat("/* something */ namespace {\n" 3462 "class A {};\n" 3463 "void f() { f(); }\n" 3464 "}", 3465 LLVMWithNoNamespaceFix); 3466 verifyFormat("inline namespace X {\n" 3467 "class A {};\n" 3468 "void f() { f(); }\n" 3469 "}", 3470 LLVMWithNoNamespaceFix); 3471 verifyFormat("/* something */ inline namespace X {\n" 3472 "class A {};\n" 3473 "void f() { f(); }\n" 3474 "}", 3475 LLVMWithNoNamespaceFix); 3476 verifyFormat("export namespace X {\n" 3477 "class A {};\n" 3478 "void f() { f(); }\n" 3479 "}", 3480 LLVMWithNoNamespaceFix); 3481 verifyFormat("using namespace some_namespace;\n" 3482 "class A {};\n" 3483 "void f() { f(); }", 3484 LLVMWithNoNamespaceFix); 3485 3486 // This code is more common than we thought; if we 3487 // layout this correctly the semicolon will go into 3488 // its own line, which is undesirable. 3489 verifyFormat("namespace {};", LLVMWithNoNamespaceFix); 3490 verifyFormat("namespace {\n" 3491 "class A {};\n" 3492 "};", 3493 LLVMWithNoNamespaceFix); 3494 3495 verifyFormat("namespace {\n" 3496 "int SomeVariable = 0; // comment\n" 3497 "} // namespace", 3498 LLVMWithNoNamespaceFix); 3499 EXPECT_EQ("#ifndef HEADER_GUARD\n" 3500 "#define HEADER_GUARD\n" 3501 "namespace my_namespace {\n" 3502 "int i;\n" 3503 "} // my_namespace\n" 3504 "#endif // HEADER_GUARD", 3505 format("#ifndef HEADER_GUARD\n" 3506 " #define HEADER_GUARD\n" 3507 " namespace my_namespace {\n" 3508 "int i;\n" 3509 "} // my_namespace\n" 3510 "#endif // HEADER_GUARD", 3511 LLVMWithNoNamespaceFix)); 3512 3513 EXPECT_EQ("namespace A::B {\n" 3514 "class C {};\n" 3515 "}", 3516 format("namespace A::B {\n" 3517 "class C {};\n" 3518 "}", 3519 LLVMWithNoNamespaceFix)); 3520 3521 FormatStyle Style = getLLVMStyle(); 3522 Style.NamespaceIndentation = FormatStyle::NI_All; 3523 EXPECT_EQ("namespace out {\n" 3524 " int i;\n" 3525 " namespace in {\n" 3526 " int i;\n" 3527 " } // namespace in\n" 3528 "} // namespace out", 3529 format("namespace out {\n" 3530 "int i;\n" 3531 "namespace in {\n" 3532 "int i;\n" 3533 "} // namespace in\n" 3534 "} // namespace out", 3535 Style)); 3536 3537 Style.NamespaceIndentation = FormatStyle::NI_Inner; 3538 EXPECT_EQ("namespace out {\n" 3539 "int i;\n" 3540 "namespace in {\n" 3541 " int i;\n" 3542 "} // namespace in\n" 3543 "} // namespace out", 3544 format("namespace out {\n" 3545 "int i;\n" 3546 "namespace in {\n" 3547 "int i;\n" 3548 "} // namespace in\n" 3549 "} // namespace out", 3550 Style)); 3551 3552 Style.NamespaceIndentation = FormatStyle::NI_None; 3553 verifyFormat("template <class T>\n" 3554 "concept a_concept = X<>;\n" 3555 "namespace B {\n" 3556 "struct b_struct {};\n" 3557 "} // namespace B\n", 3558 Style); 3559 } 3560 3561 TEST_F(FormatTest, NamespaceMacros) { 3562 FormatStyle Style = getLLVMStyle(); 3563 Style.NamespaceMacros.push_back("TESTSUITE"); 3564 3565 verifyFormat("TESTSUITE(A) {\n" 3566 "int foo();\n" 3567 "} // TESTSUITE(A)", 3568 Style); 3569 3570 verifyFormat("TESTSUITE(A, B) {\n" 3571 "int foo();\n" 3572 "} // TESTSUITE(A)", 3573 Style); 3574 3575 // Properly indent according to NamespaceIndentation style 3576 Style.NamespaceIndentation = FormatStyle::NI_All; 3577 verifyFormat("TESTSUITE(A) {\n" 3578 " int foo();\n" 3579 "} // TESTSUITE(A)", 3580 Style); 3581 verifyFormat("TESTSUITE(A) {\n" 3582 " namespace B {\n" 3583 " int foo();\n" 3584 " } // namespace B\n" 3585 "} // TESTSUITE(A)", 3586 Style); 3587 verifyFormat("namespace A {\n" 3588 " TESTSUITE(B) {\n" 3589 " int foo();\n" 3590 " } // TESTSUITE(B)\n" 3591 "} // namespace A", 3592 Style); 3593 3594 Style.NamespaceIndentation = FormatStyle::NI_Inner; 3595 verifyFormat("TESTSUITE(A) {\n" 3596 "TESTSUITE(B) {\n" 3597 " int foo();\n" 3598 "} // TESTSUITE(B)\n" 3599 "} // TESTSUITE(A)", 3600 Style); 3601 verifyFormat("TESTSUITE(A) {\n" 3602 "namespace B {\n" 3603 " int foo();\n" 3604 "} // namespace B\n" 3605 "} // TESTSUITE(A)", 3606 Style); 3607 verifyFormat("namespace A {\n" 3608 "TESTSUITE(B) {\n" 3609 " int foo();\n" 3610 "} // TESTSUITE(B)\n" 3611 "} // namespace A", 3612 Style); 3613 3614 // Properly merge namespace-macros blocks in CompactNamespaces mode 3615 Style.NamespaceIndentation = FormatStyle::NI_None; 3616 Style.CompactNamespaces = true; 3617 verifyFormat("TESTSUITE(A) { TESTSUITE(B) {\n" 3618 "}} // TESTSUITE(A::B)", 3619 Style); 3620 3621 EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n" 3622 "}} // TESTSUITE(out::in)", 3623 format("TESTSUITE(out) {\n" 3624 "TESTSUITE(in) {\n" 3625 "} // TESTSUITE(in)\n" 3626 "} // TESTSUITE(out)", 3627 Style)); 3628 3629 EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n" 3630 "}} // TESTSUITE(out::in)", 3631 format("TESTSUITE(out) {\n" 3632 "TESTSUITE(in) {\n" 3633 "} // TESTSUITE(in)\n" 3634 "} // TESTSUITE(out)", 3635 Style)); 3636 3637 // Do not merge different namespaces/macros 3638 EXPECT_EQ("namespace out {\n" 3639 "TESTSUITE(in) {\n" 3640 "} // TESTSUITE(in)\n" 3641 "} // namespace out", 3642 format("namespace out {\n" 3643 "TESTSUITE(in) {\n" 3644 "} // TESTSUITE(in)\n" 3645 "} // namespace out", 3646 Style)); 3647 EXPECT_EQ("TESTSUITE(out) {\n" 3648 "namespace in {\n" 3649 "} // namespace in\n" 3650 "} // TESTSUITE(out)", 3651 format("TESTSUITE(out) {\n" 3652 "namespace in {\n" 3653 "} // namespace in\n" 3654 "} // TESTSUITE(out)", 3655 Style)); 3656 Style.NamespaceMacros.push_back("FOOBAR"); 3657 EXPECT_EQ("TESTSUITE(out) {\n" 3658 "FOOBAR(in) {\n" 3659 "} // FOOBAR(in)\n" 3660 "} // TESTSUITE(out)", 3661 format("TESTSUITE(out) {\n" 3662 "FOOBAR(in) {\n" 3663 "} // FOOBAR(in)\n" 3664 "} // TESTSUITE(out)", 3665 Style)); 3666 } 3667 3668 TEST_F(FormatTest, FormatsCompactNamespaces) { 3669 FormatStyle Style = getLLVMStyle(); 3670 Style.CompactNamespaces = true; 3671 Style.NamespaceMacros.push_back("TESTSUITE"); 3672 3673 verifyFormat("namespace A { namespace B {\n" 3674 "}} // namespace A::B", 3675 Style); 3676 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 // Only namespaces which have both consecutive opening and end get compacted 3686 EXPECT_EQ("namespace out {\n" 3687 "namespace in1 {\n" 3688 "} // namespace in1\n" 3689 "namespace in2 {\n" 3690 "} // namespace in2\n" 3691 "} // namespace out", 3692 format("namespace out {\n" 3693 "namespace in1 {\n" 3694 "} // namespace in1\n" 3695 "namespace in2 {\n" 3696 "} // namespace in2\n" 3697 "} // namespace out", 3698 Style)); 3699 3700 EXPECT_EQ("namespace out {\n" 3701 "int i;\n" 3702 "namespace in {\n" 3703 "int j;\n" 3704 "} // namespace in\n" 3705 "int k;\n" 3706 "} // namespace out", 3707 format("namespace out { int i;\n" 3708 "namespace in { int j; } // namespace in\n" 3709 "int k; } // namespace out", 3710 Style)); 3711 3712 EXPECT_EQ("namespace A { namespace B { namespace C {\n" 3713 "}}} // namespace A::B::C\n", 3714 format("namespace A { namespace B {\n" 3715 "namespace C {\n" 3716 "}} // namespace B::C\n" 3717 "} // namespace A\n", 3718 Style)); 3719 3720 Style.ColumnLimit = 40; 3721 EXPECT_EQ("namespace aaaaaaaaaa {\n" 3722 "namespace bbbbbbbbbb {\n" 3723 "}} // namespace aaaaaaaaaa::bbbbbbbbbb", 3724 format("namespace aaaaaaaaaa {\n" 3725 "namespace bbbbbbbbbb {\n" 3726 "} // namespace bbbbbbbbbb\n" 3727 "} // namespace aaaaaaaaaa", 3728 Style)); 3729 3730 EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n" 3731 "namespace cccccc {\n" 3732 "}}} // namespace aaaaaa::bbbbbb::cccccc", 3733 format("namespace aaaaaa {\n" 3734 "namespace bbbbbb {\n" 3735 "namespace cccccc {\n" 3736 "} // namespace cccccc\n" 3737 "} // namespace bbbbbb\n" 3738 "} // namespace aaaaaa", 3739 Style)); 3740 Style.ColumnLimit = 80; 3741 3742 // Extra semicolon after 'inner' closing brace prevents merging 3743 EXPECT_EQ("namespace out { namespace in {\n" 3744 "}; } // namespace out::in", 3745 format("namespace out {\n" 3746 "namespace in {\n" 3747 "}; // namespace in\n" 3748 "} // namespace out", 3749 Style)); 3750 3751 // Extra semicolon after 'outer' closing brace is conserved 3752 EXPECT_EQ("namespace out { namespace in {\n" 3753 "}}; // namespace out::in", 3754 format("namespace out {\n" 3755 "namespace in {\n" 3756 "} // namespace in\n" 3757 "}; // namespace out", 3758 Style)); 3759 3760 Style.NamespaceIndentation = FormatStyle::NI_All; 3761 EXPECT_EQ("namespace out { namespace in {\n" 3762 " int i;\n" 3763 "}} // namespace out::in", 3764 format("namespace out {\n" 3765 "namespace in {\n" 3766 "int i;\n" 3767 "} // namespace in\n" 3768 "} // namespace out", 3769 Style)); 3770 EXPECT_EQ("namespace out { namespace mid {\n" 3771 " namespace in {\n" 3772 " int j;\n" 3773 " } // namespace in\n" 3774 " int k;\n" 3775 "}} // namespace out::mid", 3776 format("namespace out { namespace mid {\n" 3777 "namespace in { int j; } // namespace in\n" 3778 "int k; }} // namespace out::mid", 3779 Style)); 3780 3781 Style.NamespaceIndentation = FormatStyle::NI_Inner; 3782 EXPECT_EQ("namespace out { namespace in {\n" 3783 " int i;\n" 3784 "}} // namespace out::in", 3785 format("namespace out {\n" 3786 "namespace in {\n" 3787 "int i;\n" 3788 "} // namespace in\n" 3789 "} // namespace out", 3790 Style)); 3791 EXPECT_EQ("namespace out { namespace mid { namespace in {\n" 3792 " int i;\n" 3793 "}}} // namespace out::mid::in", 3794 format("namespace out {\n" 3795 "namespace mid {\n" 3796 "namespace in {\n" 3797 "int i;\n" 3798 "} // namespace in\n" 3799 "} // namespace mid\n" 3800 "} // namespace out", 3801 Style)); 3802 } 3803 3804 TEST_F(FormatTest, FormatsExternC) { 3805 verifyFormat("extern \"C\" {\nint a;"); 3806 verifyFormat("extern \"C\" {}"); 3807 verifyFormat("extern \"C\" {\n" 3808 "int foo();\n" 3809 "}"); 3810 verifyFormat("extern \"C\" int foo() {}"); 3811 verifyFormat("extern \"C\" int foo();"); 3812 verifyFormat("extern \"C\" int foo() {\n" 3813 " int i = 42;\n" 3814 " return i;\n" 3815 "}"); 3816 3817 FormatStyle Style = getLLVMStyle(); 3818 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3819 Style.BraceWrapping.AfterFunction = true; 3820 verifyFormat("extern \"C\" int foo() {}", Style); 3821 verifyFormat("extern \"C\" int foo();", Style); 3822 verifyFormat("extern \"C\" int foo()\n" 3823 "{\n" 3824 " int i = 42;\n" 3825 " return i;\n" 3826 "}", 3827 Style); 3828 3829 Style.BraceWrapping.AfterExternBlock = true; 3830 Style.BraceWrapping.SplitEmptyRecord = false; 3831 verifyFormat("extern \"C\"\n" 3832 "{}", 3833 Style); 3834 verifyFormat("extern \"C\"\n" 3835 "{\n" 3836 " int foo();\n" 3837 "}", 3838 Style); 3839 } 3840 3841 TEST_F(FormatTest, IndentExternBlockStyle) { 3842 FormatStyle Style = getLLVMStyle(); 3843 Style.IndentWidth = 2; 3844 3845 Style.IndentExternBlock = FormatStyle::IEBS_Indent; 3846 verifyFormat("extern \"C\" { /*9*/\n" 3847 "}", 3848 Style); 3849 verifyFormat("extern \"C\" {\n" 3850 " int foo10();\n" 3851 "}", 3852 Style); 3853 3854 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; 3855 verifyFormat("extern \"C\" { /*11*/\n" 3856 "}", 3857 Style); 3858 verifyFormat("extern \"C\" {\n" 3859 "int foo12();\n" 3860 "}", 3861 Style); 3862 3863 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3864 Style.BraceWrapping.AfterExternBlock = true; 3865 Style.IndentExternBlock = FormatStyle::IEBS_Indent; 3866 verifyFormat("extern \"C\"\n" 3867 "{ /*13*/\n" 3868 "}", 3869 Style); 3870 verifyFormat("extern \"C\"\n{\n" 3871 " int foo14();\n" 3872 "}", 3873 Style); 3874 3875 Style.BraceWrapping.AfterExternBlock = false; 3876 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; 3877 verifyFormat("extern \"C\" { /*15*/\n" 3878 "}", 3879 Style); 3880 verifyFormat("extern \"C\" {\n" 3881 "int foo16();\n" 3882 "}", 3883 Style); 3884 3885 Style.BraceWrapping.AfterExternBlock = true; 3886 verifyFormat("extern \"C\"\n" 3887 "{ /*13*/\n" 3888 "}", 3889 Style); 3890 verifyFormat("extern \"C\"\n" 3891 "{\n" 3892 "int foo14();\n" 3893 "}", 3894 Style); 3895 3896 Style.IndentExternBlock = FormatStyle::IEBS_Indent; 3897 verifyFormat("extern \"C\"\n" 3898 "{ /*13*/\n" 3899 "}", 3900 Style); 3901 verifyFormat("extern \"C\"\n" 3902 "{\n" 3903 " int foo14();\n" 3904 "}", 3905 Style); 3906 } 3907 3908 TEST_F(FormatTest, FormatsInlineASM) { 3909 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); 3910 verifyFormat("asm(\"nop\" ::: \"memory\");"); 3911 verifyFormat( 3912 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 3913 " \"cpuid\\n\\t\"\n" 3914 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 3915 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" 3916 " : \"a\"(value));"); 3917 EXPECT_EQ( 3918 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 3919 " __asm {\n" 3920 " mov edx,[that] // vtable in edx\n" 3921 " mov eax,methodIndex\n" 3922 " call [edx][eax*4] // stdcall\n" 3923 " }\n" 3924 "}", 3925 format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 3926 " __asm {\n" 3927 " mov edx,[that] // vtable in edx\n" 3928 " mov eax,methodIndex\n" 3929 " call [edx][eax*4] // stdcall\n" 3930 " }\n" 3931 "}")); 3932 EXPECT_EQ("_asm {\n" 3933 " xor eax, eax;\n" 3934 " cpuid;\n" 3935 "}", 3936 format("_asm {\n" 3937 " xor eax, eax;\n" 3938 " cpuid;\n" 3939 "}")); 3940 verifyFormat("void function() {\n" 3941 " // comment\n" 3942 " asm(\"\");\n" 3943 "}"); 3944 EXPECT_EQ("__asm {\n" 3945 "}\n" 3946 "int i;", 3947 format("__asm {\n" 3948 "}\n" 3949 "int i;")); 3950 } 3951 3952 TEST_F(FormatTest, FormatTryCatch) { 3953 verifyFormat("try {\n" 3954 " throw a * b;\n" 3955 "} catch (int a) {\n" 3956 " // Do nothing.\n" 3957 "} catch (...) {\n" 3958 " exit(42);\n" 3959 "}"); 3960 3961 // Function-level try statements. 3962 verifyFormat("int f() try { return 4; } catch (...) {\n" 3963 " return 5;\n" 3964 "}"); 3965 verifyFormat("class A {\n" 3966 " int a;\n" 3967 " A() try : a(0) {\n" 3968 " } catch (...) {\n" 3969 " throw;\n" 3970 " }\n" 3971 "};\n"); 3972 verifyFormat("class A {\n" 3973 " int a;\n" 3974 " A() try : a(0), b{1} {\n" 3975 " } catch (...) {\n" 3976 " throw;\n" 3977 " }\n" 3978 "};\n"); 3979 verifyFormat("class A {\n" 3980 " int a;\n" 3981 " A() try : a(0), b{1}, c{2} {\n" 3982 " } catch (...) {\n" 3983 " throw;\n" 3984 " }\n" 3985 "};\n"); 3986 verifyFormat("class A {\n" 3987 " int a;\n" 3988 " A() try : a(0), b{1}, c{2} {\n" 3989 " { // New scope.\n" 3990 " }\n" 3991 " } catch (...) {\n" 3992 " throw;\n" 3993 " }\n" 3994 "};\n"); 3995 3996 // Incomplete try-catch blocks. 3997 verifyIncompleteFormat("try {} catch ("); 3998 } 3999 4000 TEST_F(FormatTest, FormatTryAsAVariable) { 4001 verifyFormat("int try;"); 4002 verifyFormat("int try, size;"); 4003 verifyFormat("try = foo();"); 4004 verifyFormat("if (try < size) {\n return true;\n}"); 4005 4006 verifyFormat("int catch;"); 4007 verifyFormat("int catch, size;"); 4008 verifyFormat("catch = foo();"); 4009 verifyFormat("if (catch < size) {\n return true;\n}"); 4010 4011 FormatStyle Style = getLLVMStyle(); 4012 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4013 Style.BraceWrapping.AfterFunction = true; 4014 Style.BraceWrapping.BeforeCatch = true; 4015 verifyFormat("try {\n" 4016 " int bar = 1;\n" 4017 "}\n" 4018 "catch (...) {\n" 4019 " int bar = 1;\n" 4020 "}", 4021 Style); 4022 verifyFormat("#if NO_EX\n" 4023 "try\n" 4024 "#endif\n" 4025 "{\n" 4026 "}\n" 4027 "#if NO_EX\n" 4028 "catch (...) {\n" 4029 "}", 4030 Style); 4031 verifyFormat("try /* abc */ {\n" 4032 " int bar = 1;\n" 4033 "}\n" 4034 "catch (...) {\n" 4035 " int bar = 1;\n" 4036 "}", 4037 Style); 4038 verifyFormat("try\n" 4039 "// abc\n" 4040 "{\n" 4041 " int bar = 1;\n" 4042 "}\n" 4043 "catch (...) {\n" 4044 " int bar = 1;\n" 4045 "}", 4046 Style); 4047 } 4048 4049 TEST_F(FormatTest, FormatSEHTryCatch) { 4050 verifyFormat("__try {\n" 4051 " int a = b * c;\n" 4052 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n" 4053 " // Do nothing.\n" 4054 "}"); 4055 4056 verifyFormat("__try {\n" 4057 " int a = b * c;\n" 4058 "} __finally {\n" 4059 " // Do nothing.\n" 4060 "}"); 4061 4062 verifyFormat("DEBUG({\n" 4063 " __try {\n" 4064 " } __finally {\n" 4065 " }\n" 4066 "});\n"); 4067 } 4068 4069 TEST_F(FormatTest, IncompleteTryCatchBlocks) { 4070 verifyFormat("try {\n" 4071 " f();\n" 4072 "} catch {\n" 4073 " g();\n" 4074 "}"); 4075 verifyFormat("try {\n" 4076 " f();\n" 4077 "} catch (A a) MACRO(x) {\n" 4078 " g();\n" 4079 "} catch (B b) MACRO(x) {\n" 4080 " g();\n" 4081 "}"); 4082 } 4083 4084 TEST_F(FormatTest, FormatTryCatchBraceStyles) { 4085 FormatStyle Style = getLLVMStyle(); 4086 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, 4087 FormatStyle::BS_WebKit}) { 4088 Style.BreakBeforeBraces = BraceStyle; 4089 verifyFormat("try {\n" 4090 " // something\n" 4091 "} catch (...) {\n" 4092 " // something\n" 4093 "}", 4094 Style); 4095 } 4096 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 4097 verifyFormat("try {\n" 4098 " // something\n" 4099 "}\n" 4100 "catch (...) {\n" 4101 " // something\n" 4102 "}", 4103 Style); 4104 verifyFormat("__try {\n" 4105 " // something\n" 4106 "}\n" 4107 "__finally {\n" 4108 " // something\n" 4109 "}", 4110 Style); 4111 verifyFormat("@try {\n" 4112 " // something\n" 4113 "}\n" 4114 "@finally {\n" 4115 " // something\n" 4116 "}", 4117 Style); 4118 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 4119 verifyFormat("try\n" 4120 "{\n" 4121 " // something\n" 4122 "}\n" 4123 "catch (...)\n" 4124 "{\n" 4125 " // something\n" 4126 "}", 4127 Style); 4128 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 4129 verifyFormat("try\n" 4130 " {\n" 4131 " // something white\n" 4132 " }\n" 4133 "catch (...)\n" 4134 " {\n" 4135 " // something white\n" 4136 " }", 4137 Style); 4138 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 4139 verifyFormat("try\n" 4140 " {\n" 4141 " // something\n" 4142 " }\n" 4143 "catch (...)\n" 4144 " {\n" 4145 " // something\n" 4146 " }", 4147 Style); 4148 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4149 Style.BraceWrapping.BeforeCatch = true; 4150 verifyFormat("try {\n" 4151 " // something\n" 4152 "}\n" 4153 "catch (...) {\n" 4154 " // something\n" 4155 "}", 4156 Style); 4157 } 4158 4159 TEST_F(FormatTest, StaticInitializers) { 4160 verifyFormat("static SomeClass SC = {1, 'a'};"); 4161 4162 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" 4163 " 100000000, " 4164 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); 4165 4166 // Here, everything other than the "}" would fit on a line. 4167 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" 4168 " 10000000000000000000000000};"); 4169 EXPECT_EQ("S s = {a,\n" 4170 "\n" 4171 " b};", 4172 format("S s = {\n" 4173 " a,\n" 4174 "\n" 4175 " b\n" 4176 "};")); 4177 4178 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first 4179 // line. However, the formatting looks a bit off and this probably doesn't 4180 // happen often in practice. 4181 verifyFormat("static int Variable[1] = {\n" 4182 " {1000000000000000000000000000000000000}};", 4183 getLLVMStyleWithColumns(40)); 4184 } 4185 4186 TEST_F(FormatTest, DesignatedInitializers) { 4187 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 4188 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" 4189 " .bbbbbbbbbb = 2,\n" 4190 " .cccccccccc = 3,\n" 4191 " .dddddddddd = 4,\n" 4192 " .eeeeeeeeee = 5};"); 4193 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 4194 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" 4195 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" 4196 " .ccccccccccccccccccccccccccc = 3,\n" 4197 " .ddddddddddddddddddddddddddd = 4,\n" 4198 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); 4199 4200 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); 4201 4202 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 4203 verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n" 4204 " [2] = bbbbbbbbbb,\n" 4205 " [3] = cccccccccc,\n" 4206 " [4] = dddddddddd,\n" 4207 " [5] = eeeeeeeeee};"); 4208 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 4209 " [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4210 " [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 4211 " [3] = cccccccccccccccccccccccccccccccccccccc,\n" 4212 " [4] = dddddddddddddddddddddddddddddddddddddd,\n" 4213 " [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};"); 4214 } 4215 4216 TEST_F(FormatTest, NestedStaticInitializers) { 4217 verifyFormat("static A x = {{{}}};\n"); 4218 verifyFormat("static A x = {{{init1, init2, init3, init4},\n" 4219 " {init1, init2, init3, init4}}};", 4220 getLLVMStyleWithColumns(50)); 4221 4222 verifyFormat("somes Status::global_reps[3] = {\n" 4223 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 4224 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 4225 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", 4226 getLLVMStyleWithColumns(60)); 4227 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n" 4228 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 4229 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 4230 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};"); 4231 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" 4232 " {rect.fRight - rect.fLeft, rect.fBottom - " 4233 "rect.fTop}};"); 4234 4235 verifyFormat( 4236 "SomeArrayOfSomeType a = {\n" 4237 " {{1, 2, 3},\n" 4238 " {1, 2, 3},\n" 4239 " {111111111111111111111111111111, 222222222222222222222222222222,\n" 4240 " 333333333333333333333333333333},\n" 4241 " {1, 2, 3},\n" 4242 " {1, 2, 3}}};"); 4243 verifyFormat( 4244 "SomeArrayOfSomeType a = {\n" 4245 " {{1, 2, 3}},\n" 4246 " {{1, 2, 3}},\n" 4247 " {{111111111111111111111111111111, 222222222222222222222222222222,\n" 4248 " 333333333333333333333333333333}},\n" 4249 " {{1, 2, 3}},\n" 4250 " {{1, 2, 3}}};"); 4251 4252 verifyFormat("struct {\n" 4253 " unsigned bit;\n" 4254 " const char *const name;\n" 4255 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" 4256 " {kOsWin, \"Windows\"},\n" 4257 " {kOsLinux, \"Linux\"},\n" 4258 " {kOsCrOS, \"Chrome OS\"}};"); 4259 verifyFormat("struct {\n" 4260 " unsigned bit;\n" 4261 " const char *const name;\n" 4262 "} kBitsToOs[] = {\n" 4263 " {kOsMac, \"Mac\"},\n" 4264 " {kOsWin, \"Windows\"},\n" 4265 " {kOsLinux, \"Linux\"},\n" 4266 " {kOsCrOS, \"Chrome OS\"},\n" 4267 "};"); 4268 } 4269 4270 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 4271 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 4272 " \\\n" 4273 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 4274 } 4275 4276 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 4277 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 4278 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 4279 4280 // Do break defaulted and deleted functions. 4281 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 4282 " default;", 4283 getLLVMStyleWithColumns(40)); 4284 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 4285 " delete;", 4286 getLLVMStyleWithColumns(40)); 4287 } 4288 4289 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { 4290 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3", 4291 getLLVMStyleWithColumns(40)); 4292 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 4293 getLLVMStyleWithColumns(40)); 4294 EXPECT_EQ("#define Q \\\n" 4295 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" 4296 " \"aaaaaaaa.cpp\"", 4297 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 4298 getLLVMStyleWithColumns(40))); 4299 } 4300 4301 TEST_F(FormatTest, UnderstandsLinePPDirective) { 4302 EXPECT_EQ("# 123 \"A string literal\"", 4303 format(" # 123 \"A string literal\"")); 4304 } 4305 4306 TEST_F(FormatTest, LayoutUnknownPPDirective) { 4307 EXPECT_EQ("#;", format("#;")); 4308 verifyFormat("#\n;\n;\n;"); 4309 } 4310 4311 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 4312 EXPECT_EQ("#line 42 \"test\"\n", 4313 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 4314 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", 4315 getLLVMStyleWithColumns(12))); 4316 } 4317 4318 TEST_F(FormatTest, EndOfFileEndsPPDirective) { 4319 EXPECT_EQ("#line 42 \"test\"", 4320 format("# \\\n line \\\n 42 \\\n \"test\"")); 4321 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); 4322 } 4323 4324 TEST_F(FormatTest, DoesntRemoveUnknownTokens) { 4325 verifyFormat("#define A \\x20"); 4326 verifyFormat("#define A \\ x20"); 4327 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20")); 4328 verifyFormat("#define A ''"); 4329 verifyFormat("#define A ''qqq"); 4330 verifyFormat("#define A `qqq"); 4331 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); 4332 EXPECT_EQ("const char *c = STRINGIFY(\n" 4333 "\\na : b);", 4334 format("const char * c = STRINGIFY(\n" 4335 "\\na : b);")); 4336 4337 verifyFormat("a\r\\"); 4338 verifyFormat("a\v\\"); 4339 verifyFormat("a\f\\"); 4340 } 4341 4342 TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) { 4343 FormatStyle style = getChromiumStyle(FormatStyle::LK_Cpp); 4344 style.IndentWidth = 4; 4345 style.PPIndentWidth = 1; 4346 4347 style.IndentPPDirectives = FormatStyle::PPDIS_None; 4348 verifyFormat("#ifdef __linux__\n" 4349 "void foo() {\n" 4350 " int x = 0;\n" 4351 "}\n" 4352 "#define FOO\n" 4353 "#endif\n" 4354 "void bar() {\n" 4355 " int y = 0;\n" 4356 "}\n", 4357 style); 4358 4359 style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 4360 verifyFormat("#ifdef __linux__\n" 4361 "void foo() {\n" 4362 " int x = 0;\n" 4363 "}\n" 4364 "# define FOO foo\n" 4365 "#endif\n" 4366 "void bar() {\n" 4367 " int y = 0;\n" 4368 "}\n", 4369 style); 4370 4371 style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash; 4372 verifyFormat("#ifdef __linux__\n" 4373 "void foo() {\n" 4374 " int x = 0;\n" 4375 "}\n" 4376 " #define FOO foo\n" 4377 "#endif\n" 4378 "void bar() {\n" 4379 " int y = 0;\n" 4380 "}\n", 4381 style); 4382 } 4383 4384 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 4385 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 4386 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 4387 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 4388 // FIXME: We never break before the macro name. 4389 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12)); 4390 4391 verifyFormat("#define A A\n#define A A"); 4392 verifyFormat("#define A(X) A\n#define A A"); 4393 4394 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 4395 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 4396 } 4397 4398 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 4399 EXPECT_EQ("// somecomment\n" 4400 "#include \"a.h\"\n" 4401 "#define A( \\\n" 4402 " A, B)\n" 4403 "#include \"b.h\"\n" 4404 "// somecomment\n", 4405 format(" // somecomment\n" 4406 " #include \"a.h\"\n" 4407 "#define A(A,\\\n" 4408 " B)\n" 4409 " #include \"b.h\"\n" 4410 " // somecomment\n", 4411 getLLVMStyleWithColumns(13))); 4412 } 4413 4414 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } 4415 4416 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 4417 EXPECT_EQ("#define A \\\n" 4418 " c; \\\n" 4419 " e;\n" 4420 "f;", 4421 format("#define A c; e;\n" 4422 "f;", 4423 getLLVMStyleWithColumns(14))); 4424 } 4425 4426 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } 4427 4428 TEST_F(FormatTest, MacroDefinitionInsideStatement) { 4429 EXPECT_EQ("int x,\n" 4430 "#define A\n" 4431 " y;", 4432 format("int x,\n#define A\ny;")); 4433 } 4434 4435 TEST_F(FormatTest, HashInMacroDefinition) { 4436 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle())); 4437 EXPECT_EQ("#define A(c) u#c", format("#define A(c) u#c", getLLVMStyle())); 4438 EXPECT_EQ("#define A(c) U#c", format("#define A(c) U#c", getLLVMStyle())); 4439 EXPECT_EQ("#define A(c) u8#c", format("#define A(c) u8#c", getLLVMStyle())); 4440 EXPECT_EQ("#define A(c) LR#c", format("#define A(c) LR#c", getLLVMStyle())); 4441 EXPECT_EQ("#define A(c) uR#c", format("#define A(c) uR#c", getLLVMStyle())); 4442 EXPECT_EQ("#define A(c) UR#c", format("#define A(c) UR#c", getLLVMStyle())); 4443 EXPECT_EQ("#define A(c) u8R#c", format("#define A(c) u8R#c", getLLVMStyle())); 4444 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 4445 verifyFormat("#define A \\\n" 4446 " { \\\n" 4447 " f(#c); \\\n" 4448 " }", 4449 getLLVMStyleWithColumns(11)); 4450 4451 verifyFormat("#define A(X) \\\n" 4452 " void function##X()", 4453 getLLVMStyleWithColumns(22)); 4454 4455 verifyFormat("#define A(a, b, c) \\\n" 4456 " void a##b##c()", 4457 getLLVMStyleWithColumns(22)); 4458 4459 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 4460 } 4461 4462 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 4463 EXPECT_EQ("#define A (x)", format("#define A (x)")); 4464 EXPECT_EQ("#define A(x)", format("#define A(x)")); 4465 4466 FormatStyle Style = getLLVMStyle(); 4467 Style.SpaceBeforeParens = FormatStyle::SBPO_Never; 4468 verifyFormat("#define true ((foo)1)", Style); 4469 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 4470 verifyFormat("#define false((foo)0)", Style); 4471 } 4472 4473 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 4474 EXPECT_EQ("#define A b;", format("#define A \\\n" 4475 " \\\n" 4476 " b;", 4477 getLLVMStyleWithColumns(25))); 4478 EXPECT_EQ("#define A \\\n" 4479 " \\\n" 4480 " a; \\\n" 4481 " b;", 4482 format("#define A \\\n" 4483 " \\\n" 4484 " a; \\\n" 4485 " b;", 4486 getLLVMStyleWithColumns(11))); 4487 EXPECT_EQ("#define A \\\n" 4488 " a; \\\n" 4489 " \\\n" 4490 " b;", 4491 format("#define A \\\n" 4492 " a; \\\n" 4493 " \\\n" 4494 " b;", 4495 getLLVMStyleWithColumns(11))); 4496 } 4497 4498 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 4499 verifyIncompleteFormat("#define A :"); 4500 verifyFormat("#define SOMECASES \\\n" 4501 " case 1: \\\n" 4502 " case 2\n", 4503 getLLVMStyleWithColumns(20)); 4504 verifyFormat("#define MACRO(a) \\\n" 4505 " if (a) \\\n" 4506 " f(); \\\n" 4507 " else \\\n" 4508 " g()", 4509 getLLVMStyleWithColumns(18)); 4510 verifyFormat("#define A template <typename T>"); 4511 verifyIncompleteFormat("#define STR(x) #x\n" 4512 "f(STR(this_is_a_string_literal{));"); 4513 verifyFormat("#pragma omp threadprivate( \\\n" 4514 " y)), // expected-warning", 4515 getLLVMStyleWithColumns(28)); 4516 verifyFormat("#d, = };"); 4517 verifyFormat("#if \"a"); 4518 verifyIncompleteFormat("({\n" 4519 "#define b \\\n" 4520 " } \\\n" 4521 " a\n" 4522 "a", 4523 getLLVMStyleWithColumns(15)); 4524 verifyFormat("#define A \\\n" 4525 " { \\\n" 4526 " {\n" 4527 "#define B \\\n" 4528 " } \\\n" 4529 " }", 4530 getLLVMStyleWithColumns(15)); 4531 verifyNoCrash("#if a\na(\n#else\n#endif\n{a"); 4532 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}"); 4533 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};"); 4534 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}"); 4535 } 4536 4537 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { 4538 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. 4539 EXPECT_EQ("class A : public QObject {\n" 4540 " Q_OBJECT\n" 4541 "\n" 4542 " A() {}\n" 4543 "};", 4544 format("class A : public QObject {\n" 4545 " Q_OBJECT\n" 4546 "\n" 4547 " A() {\n}\n" 4548 "} ;")); 4549 EXPECT_EQ("MACRO\n" 4550 "/*static*/ int i;", 4551 format("MACRO\n" 4552 " /*static*/ int i;")); 4553 EXPECT_EQ("SOME_MACRO\n" 4554 "namespace {\n" 4555 "void f();\n" 4556 "} // namespace", 4557 format("SOME_MACRO\n" 4558 " namespace {\n" 4559 "void f( );\n" 4560 "} // namespace")); 4561 // Only if the identifier contains at least 5 characters. 4562 EXPECT_EQ("HTTP f();", format("HTTP\nf();")); 4563 EXPECT_EQ("MACRO\nf();", format("MACRO\nf();")); 4564 // Only if everything is upper case. 4565 EXPECT_EQ("class A : public QObject {\n" 4566 " Q_Object A() {}\n" 4567 "};", 4568 format("class A : public QObject {\n" 4569 " Q_Object\n" 4570 " A() {\n}\n" 4571 "} ;")); 4572 4573 // Only if the next line can actually start an unwrapped line. 4574 EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;", 4575 format("SOME_WEIRD_LOG_MACRO\n" 4576 "<< SomeThing;")); 4577 4578 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " 4579 "(n, buffers))\n", 4580 getChromiumStyle(FormatStyle::LK_Cpp)); 4581 4582 // See PR41483 4583 EXPECT_EQ("/**/ FOO(a)\n" 4584 "FOO(b)", 4585 format("/**/ FOO(a)\n" 4586 "FOO(b)")); 4587 } 4588 4589 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { 4590 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 4591 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 4592 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 4593 "class X {};\n" 4594 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 4595 "int *createScopDetectionPass() { return 0; }", 4596 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 4597 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 4598 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 4599 " class X {};\n" 4600 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 4601 " int *createScopDetectionPass() { return 0; }")); 4602 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as 4603 // braces, so that inner block is indented one level more. 4604 EXPECT_EQ("int q() {\n" 4605 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 4606 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 4607 " IPC_END_MESSAGE_MAP()\n" 4608 "}", 4609 format("int q() {\n" 4610 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 4611 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 4612 " IPC_END_MESSAGE_MAP()\n" 4613 "}")); 4614 4615 // Same inside macros. 4616 EXPECT_EQ("#define LIST(L) \\\n" 4617 " L(A) \\\n" 4618 " L(B) \\\n" 4619 " L(C)", 4620 format("#define LIST(L) \\\n" 4621 " L(A) \\\n" 4622 " L(B) \\\n" 4623 " L(C)", 4624 getGoogleStyle())); 4625 4626 // These must not be recognized as macros. 4627 EXPECT_EQ("int q() {\n" 4628 " f(x);\n" 4629 " f(x) {}\n" 4630 " f(x)->g();\n" 4631 " f(x)->*g();\n" 4632 " f(x).g();\n" 4633 " f(x) = x;\n" 4634 " f(x) += x;\n" 4635 " f(x) -= x;\n" 4636 " f(x) *= x;\n" 4637 " f(x) /= x;\n" 4638 " f(x) %= x;\n" 4639 " f(x) &= x;\n" 4640 " f(x) |= x;\n" 4641 " f(x) ^= x;\n" 4642 " f(x) >>= x;\n" 4643 " f(x) <<= x;\n" 4644 " f(x)[y].z();\n" 4645 " LOG(INFO) << x;\n" 4646 " ifstream(x) >> x;\n" 4647 "}\n", 4648 format("int q() {\n" 4649 " f(x)\n;\n" 4650 " f(x)\n {}\n" 4651 " f(x)\n->g();\n" 4652 " f(x)\n->*g();\n" 4653 " f(x)\n.g();\n" 4654 " f(x)\n = x;\n" 4655 " f(x)\n += x;\n" 4656 " f(x)\n -= x;\n" 4657 " f(x)\n *= x;\n" 4658 " f(x)\n /= x;\n" 4659 " f(x)\n %= x;\n" 4660 " f(x)\n &= x;\n" 4661 " f(x)\n |= x;\n" 4662 " f(x)\n ^= x;\n" 4663 " f(x)\n >>= x;\n" 4664 " f(x)\n <<= x;\n" 4665 " f(x)\n[y].z();\n" 4666 " LOG(INFO)\n << x;\n" 4667 " ifstream(x)\n >> x;\n" 4668 "}\n")); 4669 EXPECT_EQ("int q() {\n" 4670 " F(x)\n" 4671 " if (1) {\n" 4672 " }\n" 4673 " F(x)\n" 4674 " while (1) {\n" 4675 " }\n" 4676 " F(x)\n" 4677 " G(x);\n" 4678 " F(x)\n" 4679 " try {\n" 4680 " Q();\n" 4681 " } catch (...) {\n" 4682 " }\n" 4683 "}\n", 4684 format("int q() {\n" 4685 "F(x)\n" 4686 "if (1) {}\n" 4687 "F(x)\n" 4688 "while (1) {}\n" 4689 "F(x)\n" 4690 "G(x);\n" 4691 "F(x)\n" 4692 "try { Q(); } catch (...) {}\n" 4693 "}\n")); 4694 EXPECT_EQ("class A {\n" 4695 " A() : t(0) {}\n" 4696 " A(int i) noexcept() : {}\n" 4697 " A(X x)\n" // FIXME: function-level try blocks are broken. 4698 " try : t(0) {\n" 4699 " } catch (...) {\n" 4700 " }\n" 4701 "};", 4702 format("class A {\n" 4703 " A()\n : t(0) {}\n" 4704 " A(int i)\n noexcept() : {}\n" 4705 " A(X x)\n" 4706 " try : t(0) {} catch (...) {}\n" 4707 "};")); 4708 FormatStyle Style = getLLVMStyle(); 4709 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4710 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 4711 Style.BraceWrapping.AfterFunction = true; 4712 EXPECT_EQ("void f()\n" 4713 "try\n" 4714 "{\n" 4715 "}", 4716 format("void f() try {\n" 4717 "}", 4718 Style)); 4719 EXPECT_EQ("class SomeClass {\n" 4720 "public:\n" 4721 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 4722 "};", 4723 format("class SomeClass {\n" 4724 "public:\n" 4725 " SomeClass()\n" 4726 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 4727 "};")); 4728 EXPECT_EQ("class SomeClass {\n" 4729 "public:\n" 4730 " SomeClass()\n" 4731 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 4732 "};", 4733 format("class SomeClass {\n" 4734 "public:\n" 4735 " SomeClass()\n" 4736 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 4737 "};", 4738 getLLVMStyleWithColumns(40))); 4739 4740 verifyFormat("MACRO(>)"); 4741 4742 // Some macros contain an implicit semicolon. 4743 Style = getLLVMStyle(); 4744 Style.StatementMacros.push_back("FOO"); 4745 verifyFormat("FOO(a) int b = 0;"); 4746 verifyFormat("FOO(a)\n" 4747 "int b = 0;", 4748 Style); 4749 verifyFormat("FOO(a);\n" 4750 "int b = 0;", 4751 Style); 4752 verifyFormat("FOO(argc, argv, \"4.0.2\")\n" 4753 "int b = 0;", 4754 Style); 4755 verifyFormat("FOO()\n" 4756 "int b = 0;", 4757 Style); 4758 verifyFormat("FOO\n" 4759 "int b = 0;", 4760 Style); 4761 verifyFormat("void f() {\n" 4762 " FOO(a)\n" 4763 " return a;\n" 4764 "}", 4765 Style); 4766 verifyFormat("FOO(a)\n" 4767 "FOO(b)", 4768 Style); 4769 verifyFormat("int a = 0;\n" 4770 "FOO(b)\n" 4771 "int c = 0;", 4772 Style); 4773 verifyFormat("int a = 0;\n" 4774 "int x = FOO(a)\n" 4775 "int b = 0;", 4776 Style); 4777 verifyFormat("void foo(int a) { FOO(a) }\n" 4778 "uint32_t bar() {}", 4779 Style); 4780 } 4781 4782 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { 4783 verifyFormat("#define A \\\n" 4784 " f({ \\\n" 4785 " g(); \\\n" 4786 " });", 4787 getLLVMStyleWithColumns(11)); 4788 } 4789 4790 TEST_F(FormatTest, IndentPreprocessorDirectives) { 4791 FormatStyle Style = getLLVMStyle(); 4792 Style.IndentPPDirectives = FormatStyle::PPDIS_None; 4793 Style.ColumnLimit = 40; 4794 verifyFormat("#ifdef _WIN32\n" 4795 "#define A 0\n" 4796 "#ifdef VAR2\n" 4797 "#define B 1\n" 4798 "#include <someheader.h>\n" 4799 "#define MACRO \\\n" 4800 " some_very_long_func_aaaaaaaaaa();\n" 4801 "#endif\n" 4802 "#else\n" 4803 "#define A 1\n" 4804 "#endif", 4805 Style); 4806 Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 4807 verifyFormat("#ifdef _WIN32\n" 4808 "# define A 0\n" 4809 "# ifdef VAR2\n" 4810 "# define B 1\n" 4811 "# include <someheader.h>\n" 4812 "# define MACRO \\\n" 4813 " some_very_long_func_aaaaaaaaaa();\n" 4814 "# endif\n" 4815 "#else\n" 4816 "# define A 1\n" 4817 "#endif", 4818 Style); 4819 verifyFormat("#if A\n" 4820 "# define MACRO \\\n" 4821 " void a(int x) { \\\n" 4822 " b(); \\\n" 4823 " c(); \\\n" 4824 " d(); \\\n" 4825 " e(); \\\n" 4826 " f(); \\\n" 4827 " }\n" 4828 "#endif", 4829 Style); 4830 // Comments before include guard. 4831 verifyFormat("// file comment\n" 4832 "// file comment\n" 4833 "#ifndef HEADER_H\n" 4834 "#define HEADER_H\n" 4835 "code();\n" 4836 "#endif", 4837 Style); 4838 // Test with include guards. 4839 verifyFormat("#ifndef HEADER_H\n" 4840 "#define HEADER_H\n" 4841 "code();\n" 4842 "#endif", 4843 Style); 4844 // Include guards must have a #define with the same variable immediately 4845 // after #ifndef. 4846 verifyFormat("#ifndef NOT_GUARD\n" 4847 "# define FOO\n" 4848 "code();\n" 4849 "#endif", 4850 Style); 4851 4852 // Include guards must cover the entire file. 4853 verifyFormat("code();\n" 4854 "code();\n" 4855 "#ifndef NOT_GUARD\n" 4856 "# define NOT_GUARD\n" 4857 "code();\n" 4858 "#endif", 4859 Style); 4860 verifyFormat("#ifndef NOT_GUARD\n" 4861 "# define NOT_GUARD\n" 4862 "code();\n" 4863 "#endif\n" 4864 "code();", 4865 Style); 4866 // Test with trailing blank lines. 4867 verifyFormat("#ifndef HEADER_H\n" 4868 "#define HEADER_H\n" 4869 "code();\n" 4870 "#endif\n", 4871 Style); 4872 // Include guards don't have #else. 4873 verifyFormat("#ifndef NOT_GUARD\n" 4874 "# define NOT_GUARD\n" 4875 "code();\n" 4876 "#else\n" 4877 "#endif", 4878 Style); 4879 verifyFormat("#ifndef NOT_GUARD\n" 4880 "# define NOT_GUARD\n" 4881 "code();\n" 4882 "#elif FOO\n" 4883 "#endif", 4884 Style); 4885 // Non-identifier #define after potential include guard. 4886 verifyFormat("#ifndef FOO\n" 4887 "# define 1\n" 4888 "#endif\n", 4889 Style); 4890 // #if closes past last non-preprocessor line. 4891 verifyFormat("#ifndef FOO\n" 4892 "#define FOO\n" 4893 "#if 1\n" 4894 "int i;\n" 4895 "# define A 0\n" 4896 "#endif\n" 4897 "#endif\n", 4898 Style); 4899 // Don't crash if there is an #elif directive without a condition. 4900 verifyFormat("#if 1\n" 4901 "int x;\n" 4902 "#elif\n" 4903 "int y;\n" 4904 "#else\n" 4905 "int z;\n" 4906 "#endif", 4907 Style); 4908 // FIXME: This doesn't handle the case where there's code between the 4909 // #ifndef and #define but all other conditions hold. This is because when 4910 // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the 4911 // previous code line yet, so we can't detect it. 4912 EXPECT_EQ("#ifndef NOT_GUARD\n" 4913 "code();\n" 4914 "#define NOT_GUARD\n" 4915 "code();\n" 4916 "#endif", 4917 format("#ifndef NOT_GUARD\n" 4918 "code();\n" 4919 "# define NOT_GUARD\n" 4920 "code();\n" 4921 "#endif", 4922 Style)); 4923 // FIXME: This doesn't handle cases where legitimate preprocessor lines may 4924 // be outside an include guard. Examples are #pragma once and 4925 // #pragma GCC diagnostic, or anything else that does not change the meaning 4926 // of the file if it's included multiple times. 4927 EXPECT_EQ("#ifdef WIN32\n" 4928 "# pragma once\n" 4929 "#endif\n" 4930 "#ifndef HEADER_H\n" 4931 "# define HEADER_H\n" 4932 "code();\n" 4933 "#endif", 4934 format("#ifdef WIN32\n" 4935 "# pragma once\n" 4936 "#endif\n" 4937 "#ifndef HEADER_H\n" 4938 "#define HEADER_H\n" 4939 "code();\n" 4940 "#endif", 4941 Style)); 4942 // FIXME: This does not detect when there is a single non-preprocessor line 4943 // in front of an include-guard-like structure where other conditions hold 4944 // because ScopedLineState hides the line. 4945 EXPECT_EQ("code();\n" 4946 "#ifndef HEADER_H\n" 4947 "#define HEADER_H\n" 4948 "code();\n" 4949 "#endif", 4950 format("code();\n" 4951 "#ifndef HEADER_H\n" 4952 "# define HEADER_H\n" 4953 "code();\n" 4954 "#endif", 4955 Style)); 4956 // Keep comments aligned with #, otherwise indent comments normally. These 4957 // tests cannot use verifyFormat because messUp manipulates leading 4958 // whitespace. 4959 { 4960 const char *Expected = "" 4961 "void f() {\n" 4962 "#if 1\n" 4963 "// Preprocessor aligned.\n" 4964 "# define A 0\n" 4965 " // Code. Separated by blank line.\n" 4966 "\n" 4967 "# define B 0\n" 4968 " // Code. Not aligned with #\n" 4969 "# define C 0\n" 4970 "#endif"; 4971 const char *ToFormat = "" 4972 "void f() {\n" 4973 "#if 1\n" 4974 "// Preprocessor aligned.\n" 4975 "# define A 0\n" 4976 "// Code. Separated by blank line.\n" 4977 "\n" 4978 "# define B 0\n" 4979 " // Code. Not aligned with #\n" 4980 "# define C 0\n" 4981 "#endif"; 4982 EXPECT_EQ(Expected, format(ToFormat, Style)); 4983 EXPECT_EQ(Expected, format(Expected, Style)); 4984 } 4985 // Keep block quotes aligned. 4986 { 4987 const char *Expected = "" 4988 "void f() {\n" 4989 "#if 1\n" 4990 "/* Preprocessor aligned. */\n" 4991 "# define A 0\n" 4992 " /* Code. Separated by blank line. */\n" 4993 "\n" 4994 "# define B 0\n" 4995 " /* Code. Not aligned with # */\n" 4996 "# define C 0\n" 4997 "#endif"; 4998 const char *ToFormat = "" 4999 "void f() {\n" 5000 "#if 1\n" 5001 "/* Preprocessor aligned. */\n" 5002 "# define A 0\n" 5003 "/* Code. Separated by blank line. */\n" 5004 "\n" 5005 "# define B 0\n" 5006 " /* Code. Not aligned with # */\n" 5007 "# define C 0\n" 5008 "#endif"; 5009 EXPECT_EQ(Expected, format(ToFormat, Style)); 5010 EXPECT_EQ(Expected, format(Expected, Style)); 5011 } 5012 // Keep comments aligned with un-indented directives. 5013 { 5014 const char *Expected = "" 5015 "void f() {\n" 5016 "// Preprocessor aligned.\n" 5017 "#define A 0\n" 5018 " // Code. Separated by blank line.\n" 5019 "\n" 5020 "#define B 0\n" 5021 " // Code. Not aligned with #\n" 5022 "#define C 0\n"; 5023 const char *ToFormat = "" 5024 "void f() {\n" 5025 "// Preprocessor aligned.\n" 5026 "#define A 0\n" 5027 "// Code. Separated by blank line.\n" 5028 "\n" 5029 "#define B 0\n" 5030 " // Code. Not aligned with #\n" 5031 "#define C 0\n"; 5032 EXPECT_EQ(Expected, format(ToFormat, Style)); 5033 EXPECT_EQ(Expected, format(Expected, Style)); 5034 } 5035 // Test AfterHash with tabs. 5036 { 5037 FormatStyle Tabbed = Style; 5038 Tabbed.UseTab = FormatStyle::UT_Always; 5039 Tabbed.IndentWidth = 8; 5040 Tabbed.TabWidth = 8; 5041 verifyFormat("#ifdef _WIN32\n" 5042 "#\tdefine A 0\n" 5043 "#\tifdef VAR2\n" 5044 "#\t\tdefine B 1\n" 5045 "#\t\tinclude <someheader.h>\n" 5046 "#\t\tdefine MACRO \\\n" 5047 "\t\t\tsome_very_long_func_aaaaaaaaaa();\n" 5048 "#\tendif\n" 5049 "#else\n" 5050 "#\tdefine A 1\n" 5051 "#endif", 5052 Tabbed); 5053 } 5054 5055 // Regression test: Multiline-macro inside include guards. 5056 verifyFormat("#ifndef HEADER_H\n" 5057 "#define HEADER_H\n" 5058 "#define A() \\\n" 5059 " int i; \\\n" 5060 " int j;\n" 5061 "#endif // HEADER_H", 5062 getLLVMStyleWithColumns(20)); 5063 5064 Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash; 5065 // Basic before hash indent tests 5066 verifyFormat("#ifdef _WIN32\n" 5067 " #define A 0\n" 5068 " #ifdef VAR2\n" 5069 " #define B 1\n" 5070 " #include <someheader.h>\n" 5071 " #define MACRO \\\n" 5072 " some_very_long_func_aaaaaaaaaa();\n" 5073 " #endif\n" 5074 "#else\n" 5075 " #define A 1\n" 5076 "#endif", 5077 Style); 5078 verifyFormat("#if A\n" 5079 " #define MACRO \\\n" 5080 " void a(int x) { \\\n" 5081 " b(); \\\n" 5082 " c(); \\\n" 5083 " d(); \\\n" 5084 " e(); \\\n" 5085 " f(); \\\n" 5086 " }\n" 5087 "#endif", 5088 Style); 5089 // Keep comments aligned with indented directives. These 5090 // tests cannot use verifyFormat because messUp manipulates leading 5091 // whitespace. 5092 { 5093 const char *Expected = "void f() {\n" 5094 "// Aligned to preprocessor.\n" 5095 "#if 1\n" 5096 " // Aligned to code.\n" 5097 " int a;\n" 5098 " #if 1\n" 5099 " // Aligned to preprocessor.\n" 5100 " #define A 0\n" 5101 " // Aligned to code.\n" 5102 " int b;\n" 5103 " #endif\n" 5104 "#endif\n" 5105 "}"; 5106 const char *ToFormat = "void f() {\n" 5107 "// Aligned to preprocessor.\n" 5108 "#if 1\n" 5109 "// Aligned to code.\n" 5110 "int a;\n" 5111 "#if 1\n" 5112 "// Aligned to preprocessor.\n" 5113 "#define A 0\n" 5114 "// Aligned to code.\n" 5115 "int b;\n" 5116 "#endif\n" 5117 "#endif\n" 5118 "}"; 5119 EXPECT_EQ(Expected, format(ToFormat, Style)); 5120 EXPECT_EQ(Expected, format(Expected, Style)); 5121 } 5122 { 5123 const char *Expected = "void f() {\n" 5124 "/* Aligned to preprocessor. */\n" 5125 "#if 1\n" 5126 " /* Aligned to code. */\n" 5127 " int a;\n" 5128 " #if 1\n" 5129 " /* Aligned to preprocessor. */\n" 5130 " #define A 0\n" 5131 " /* Aligned to code. */\n" 5132 " int b;\n" 5133 " #endif\n" 5134 "#endif\n" 5135 "}"; 5136 const char *ToFormat = "void f() {\n" 5137 "/* Aligned to preprocessor. */\n" 5138 "#if 1\n" 5139 "/* Aligned to code. */\n" 5140 "int a;\n" 5141 "#if 1\n" 5142 "/* Aligned to preprocessor. */\n" 5143 "#define A 0\n" 5144 "/* Aligned to code. */\n" 5145 "int b;\n" 5146 "#endif\n" 5147 "#endif\n" 5148 "}"; 5149 EXPECT_EQ(Expected, format(ToFormat, Style)); 5150 EXPECT_EQ(Expected, format(Expected, Style)); 5151 } 5152 5153 // Test single comment before preprocessor 5154 verifyFormat("// Comment\n" 5155 "\n" 5156 "#if 1\n" 5157 "#endif", 5158 Style); 5159 } 5160 5161 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 5162 verifyFormat("{\n { a #c; }\n}"); 5163 } 5164 5165 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 5166 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 5167 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 5168 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 5169 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 5170 } 5171 5172 TEST_F(FormatTest, EscapedNewlines) { 5173 FormatStyle Narrow = getLLVMStyleWithColumns(11); 5174 EXPECT_EQ("#define A \\\n int i; \\\n int j;", 5175 format("#define A \\\nint i;\\\n int j;", Narrow)); 5176 EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;")); 5177 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 5178 EXPECT_EQ("/* \\ \\ \\\n */", format("\\\n/* \\ \\ \\\n */")); 5179 EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>")); 5180 5181 FormatStyle AlignLeft = getLLVMStyle(); 5182 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 5183 EXPECT_EQ("#define MACRO(x) \\\n" 5184 "private: \\\n" 5185 " int x(int a);\n", 5186 format("#define MACRO(x) \\\n" 5187 "private: \\\n" 5188 " int x(int a);\n", 5189 AlignLeft)); 5190 5191 // CRLF line endings 5192 EXPECT_EQ("#define A \\\r\n int i; \\\r\n int j;", 5193 format("#define A \\\r\nint i;\\\r\n int j;", Narrow)); 5194 EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;")); 5195 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 5196 EXPECT_EQ("/* \\ \\ \\\r\n */", format("\\\r\n/* \\ \\ \\\r\n */")); 5197 EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>")); 5198 EXPECT_EQ("#define MACRO(x) \\\r\n" 5199 "private: \\\r\n" 5200 " int x(int a);\r\n", 5201 format("#define MACRO(x) \\\r\n" 5202 "private: \\\r\n" 5203 " int x(int a);\r\n", 5204 AlignLeft)); 5205 5206 FormatStyle DontAlign = getLLVMStyle(); 5207 DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 5208 DontAlign.MaxEmptyLinesToKeep = 3; 5209 // FIXME: can't use verifyFormat here because the newline before 5210 // "public:" is not inserted the first time it's reformatted 5211 EXPECT_EQ("#define A \\\n" 5212 " class Foo { \\\n" 5213 " void bar(); \\\n" 5214 "\\\n" 5215 "\\\n" 5216 "\\\n" 5217 " public: \\\n" 5218 " void baz(); \\\n" 5219 " };", 5220 format("#define A \\\n" 5221 " class Foo { \\\n" 5222 " void bar(); \\\n" 5223 "\\\n" 5224 "\\\n" 5225 "\\\n" 5226 " public: \\\n" 5227 " void baz(); \\\n" 5228 " };", 5229 DontAlign)); 5230 } 5231 5232 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 5233 verifyFormat("#define A \\\n" 5234 " int v( \\\n" 5235 " a); \\\n" 5236 " int i;", 5237 getLLVMStyleWithColumns(11)); 5238 } 5239 5240 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 5241 EXPECT_EQ( 5242 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 5243 " \\\n" 5244 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 5245 "\n" 5246 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 5247 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 5248 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 5249 "\\\n" 5250 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 5251 " \n" 5252 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 5253 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 5254 } 5255 5256 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 5257 EXPECT_EQ("int\n" 5258 "#define A\n" 5259 " a;", 5260 format("int\n#define A\na;")); 5261 verifyFormat("functionCallTo(\n" 5262 " someOtherFunction(\n" 5263 " withSomeParameters, whichInSequence,\n" 5264 " areLongerThanALine(andAnotherCall,\n" 5265 "#define A B\n" 5266 " withMoreParamters,\n" 5267 " whichStronglyInfluenceTheLayout),\n" 5268 " andMoreParameters),\n" 5269 " trailing);", 5270 getLLVMStyleWithColumns(69)); 5271 verifyFormat("Foo::Foo()\n" 5272 "#ifdef BAR\n" 5273 " : baz(0)\n" 5274 "#endif\n" 5275 "{\n" 5276 "}"); 5277 verifyFormat("void f() {\n" 5278 " if (true)\n" 5279 "#ifdef A\n" 5280 " f(42);\n" 5281 " x();\n" 5282 "#else\n" 5283 " g();\n" 5284 " x();\n" 5285 "#endif\n" 5286 "}"); 5287 verifyFormat("void f(param1, param2,\n" 5288 " param3,\n" 5289 "#ifdef A\n" 5290 " param4(param5,\n" 5291 "#ifdef A1\n" 5292 " param6,\n" 5293 "#ifdef A2\n" 5294 " param7),\n" 5295 "#else\n" 5296 " param8),\n" 5297 " param9,\n" 5298 "#endif\n" 5299 " param10,\n" 5300 "#endif\n" 5301 " param11)\n" 5302 "#else\n" 5303 " param12)\n" 5304 "#endif\n" 5305 "{\n" 5306 " x();\n" 5307 "}", 5308 getLLVMStyleWithColumns(28)); 5309 verifyFormat("#if 1\n" 5310 "int i;"); 5311 verifyFormat("#if 1\n" 5312 "#endif\n" 5313 "#if 1\n" 5314 "#else\n" 5315 "#endif\n"); 5316 verifyFormat("DEBUG({\n" 5317 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5318 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 5319 "});\n" 5320 "#if a\n" 5321 "#else\n" 5322 "#endif"); 5323 5324 verifyIncompleteFormat("void f(\n" 5325 "#if A\n" 5326 ");\n" 5327 "#else\n" 5328 "#endif"); 5329 } 5330 5331 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { 5332 verifyFormat("#endif\n" 5333 "#if B"); 5334 } 5335 5336 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { 5337 FormatStyle SingleLine = getLLVMStyle(); 5338 SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; 5339 verifyFormat("#if 0\n" 5340 "#elif 1\n" 5341 "#endif\n" 5342 "void foo() {\n" 5343 " if (test) foo2();\n" 5344 "}", 5345 SingleLine); 5346 } 5347 5348 TEST_F(FormatTest, LayoutBlockInsideParens) { 5349 verifyFormat("functionCall({ int i; });"); 5350 verifyFormat("functionCall({\n" 5351 " int i;\n" 5352 " int j;\n" 5353 "});"); 5354 verifyFormat("functionCall(\n" 5355 " {\n" 5356 " int i;\n" 5357 " int j;\n" 5358 " },\n" 5359 " aaaa, bbbb, cccc);"); 5360 verifyFormat("functionA(functionB({\n" 5361 " int i;\n" 5362 " int j;\n" 5363 " }),\n" 5364 " aaaa, bbbb, cccc);"); 5365 verifyFormat("functionCall(\n" 5366 " {\n" 5367 " int i;\n" 5368 " int j;\n" 5369 " },\n" 5370 " aaaa, bbbb, // comment\n" 5371 " cccc);"); 5372 verifyFormat("functionA(functionB({\n" 5373 " int i;\n" 5374 " int j;\n" 5375 " }),\n" 5376 " aaaa, bbbb, // comment\n" 5377 " cccc);"); 5378 verifyFormat("functionCall(aaaa, bbbb, { int i; });"); 5379 verifyFormat("functionCall(aaaa, bbbb, {\n" 5380 " int i;\n" 5381 " int j;\n" 5382 "});"); 5383 verifyFormat( 5384 "Aaa(\n" // FIXME: There shouldn't be a linebreak here. 5385 " {\n" 5386 " int i; // break\n" 5387 " },\n" 5388 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 5389 " ccccccccccccccccc));"); 5390 verifyFormat("DEBUG({\n" 5391 " if (a)\n" 5392 " f();\n" 5393 "});"); 5394 } 5395 5396 TEST_F(FormatTest, LayoutBlockInsideStatement) { 5397 EXPECT_EQ("SOME_MACRO { int i; }\n" 5398 "int i;", 5399 format(" SOME_MACRO {int i;} int i;")); 5400 } 5401 5402 TEST_F(FormatTest, LayoutNestedBlocks) { 5403 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 5404 " struct s {\n" 5405 " int i;\n" 5406 " };\n" 5407 " s kBitsToOs[] = {{10}};\n" 5408 " for (int i = 0; i < 10; ++i)\n" 5409 " return;\n" 5410 "}"); 5411 verifyFormat("call(parameter, {\n" 5412 " something();\n" 5413 " // Comment using all columns.\n" 5414 " somethingelse();\n" 5415 "});", 5416 getLLVMStyleWithColumns(40)); 5417 verifyFormat("DEBUG( //\n" 5418 " { f(); }, a);"); 5419 verifyFormat("DEBUG( //\n" 5420 " {\n" 5421 " f(); //\n" 5422 " },\n" 5423 " a);"); 5424 5425 EXPECT_EQ("call(parameter, {\n" 5426 " something();\n" 5427 " // Comment too\n" 5428 " // looooooooooong.\n" 5429 " somethingElse();\n" 5430 "});", 5431 format("call(parameter, {\n" 5432 " something();\n" 5433 " // Comment too looooooooooong.\n" 5434 " somethingElse();\n" 5435 "});", 5436 getLLVMStyleWithColumns(29))); 5437 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });")); 5438 EXPECT_EQ("DEBUG({ // comment\n" 5439 " int i;\n" 5440 "});", 5441 format("DEBUG({ // comment\n" 5442 "int i;\n" 5443 "});")); 5444 EXPECT_EQ("DEBUG({\n" 5445 " int i;\n" 5446 "\n" 5447 " // comment\n" 5448 " int j;\n" 5449 "});", 5450 format("DEBUG({\n" 5451 " int i;\n" 5452 "\n" 5453 " // comment\n" 5454 " int j;\n" 5455 "});")); 5456 5457 verifyFormat("DEBUG({\n" 5458 " if (a)\n" 5459 " return;\n" 5460 "});"); 5461 verifyGoogleFormat("DEBUG({\n" 5462 " if (a) return;\n" 5463 "});"); 5464 FormatStyle Style = getGoogleStyle(); 5465 Style.ColumnLimit = 45; 5466 verifyFormat("Debug(\n" 5467 " aaaaa,\n" 5468 " {\n" 5469 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" 5470 " },\n" 5471 " a);", 5472 Style); 5473 5474 verifyFormat("SomeFunction({MACRO({ return output; }), b});"); 5475 5476 verifyNoCrash("^{v^{a}}"); 5477 } 5478 5479 TEST_F(FormatTest, FormatNestedBlocksInMacros) { 5480 EXPECT_EQ("#define MACRO() \\\n" 5481 " Debug(aaa, /* force line break */ \\\n" 5482 " { \\\n" 5483 " int i; \\\n" 5484 " int j; \\\n" 5485 " })", 5486 format("#define MACRO() Debug(aaa, /* force line break */ \\\n" 5487 " { int i; int j; })", 5488 getGoogleStyle())); 5489 5490 EXPECT_EQ("#define A \\\n" 5491 " [] { \\\n" 5492 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 5493 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 5494 " }", 5495 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 5496 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 5497 getGoogleStyle())); 5498 } 5499 5500 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 5501 EXPECT_EQ("{}", format("{}")); 5502 verifyFormat("enum E {};"); 5503 verifyFormat("enum E {}"); 5504 FormatStyle Style = getLLVMStyle(); 5505 Style.SpaceInEmptyBlock = true; 5506 EXPECT_EQ("void f() { }", format("void f() {}", Style)); 5507 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 5508 EXPECT_EQ("while (true) { }", format("while (true) {}", Style)); 5509 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 5510 Style.BraceWrapping.BeforeElse = false; 5511 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 5512 verifyFormat("if (a)\n" 5513 "{\n" 5514 "} else if (b)\n" 5515 "{\n" 5516 "} else\n" 5517 "{ }", 5518 Style); 5519 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never; 5520 verifyFormat("if (a) {\n" 5521 "} else if (b) {\n" 5522 "} else {\n" 5523 "}", 5524 Style); 5525 Style.BraceWrapping.BeforeElse = true; 5526 verifyFormat("if (a) { }\n" 5527 "else if (b) { }\n" 5528 "else { }", 5529 Style); 5530 } 5531 5532 TEST_F(FormatTest, FormatBeginBlockEndMacros) { 5533 FormatStyle Style = getLLVMStyle(); 5534 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$"; 5535 Style.MacroBlockEnd = "^[A-Z_]+_END$"; 5536 verifyFormat("FOO_BEGIN\n" 5537 " FOO_ENTRY\n" 5538 "FOO_END", 5539 Style); 5540 verifyFormat("FOO_BEGIN\n" 5541 " NESTED_FOO_BEGIN\n" 5542 " NESTED_FOO_ENTRY\n" 5543 " NESTED_FOO_END\n" 5544 "FOO_END", 5545 Style); 5546 verifyFormat("FOO_BEGIN(Foo, Bar)\n" 5547 " int x;\n" 5548 " x = 1;\n" 5549 "FOO_END(Baz)", 5550 Style); 5551 } 5552 5553 //===----------------------------------------------------------------------===// 5554 // Line break tests. 5555 //===----------------------------------------------------------------------===// 5556 5557 TEST_F(FormatTest, PreventConfusingIndents) { 5558 verifyFormat( 5559 "void f() {\n" 5560 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 5561 " parameter, parameter, parameter)),\n" 5562 " SecondLongCall(parameter));\n" 5563 "}"); 5564 verifyFormat( 5565 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5566 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 5567 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5568 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 5569 verifyFormat( 5570 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5571 " [aaaaaaaaaaaaaaaaaaaaaaaa\n" 5572 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 5573 " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); 5574 verifyFormat( 5575 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 5576 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 5577 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 5578 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 5579 verifyFormat("int a = bbbb && ccc &&\n" 5580 " fffff(\n" 5581 "#define A Just forcing a new line\n" 5582 " ddd);"); 5583 } 5584 5585 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 5586 verifyFormat( 5587 "bool aaaaaaa =\n" 5588 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 5589 " bbbbbbbb();"); 5590 verifyFormat( 5591 "bool aaaaaaa =\n" 5592 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" 5593 " bbbbbbbb();"); 5594 5595 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 5596 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 5597 " ccccccccc == ddddddddddd;"); 5598 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 5599 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" 5600 " ccccccccc == ddddddddddd;"); 5601 verifyFormat( 5602 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 5603 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" 5604 " ccccccccc == ddddddddddd;"); 5605 5606 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 5607 " aaaaaa) &&\n" 5608 " bbbbbb && cccccc;"); 5609 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 5610 " aaaaaa) >>\n" 5611 " bbbbbb;"); 5612 verifyFormat("aa = Whitespaces.addUntouchableComment(\n" 5613 " SourceMgr.getSpellingColumnNumber(\n" 5614 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 5615 " 1);"); 5616 5617 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5618 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 5619 " cccccc) {\n}"); 5620 verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5621 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 5622 " cccccc) {\n}"); 5623 verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5624 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 5625 " cccccc) {\n}"); 5626 verifyFormat("b = a &&\n" 5627 " // Comment\n" 5628 " b.c && d;"); 5629 5630 // If the LHS of a comparison is not a binary expression itself, the 5631 // additional linebreak confuses many people. 5632 verifyFormat( 5633 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5634 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 5635 "}"); 5636 verifyFormat( 5637 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5638 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 5639 "}"); 5640 verifyFormat( 5641 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 5642 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 5643 "}"); 5644 verifyFormat( 5645 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5646 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n" 5647 "}"); 5648 // Even explicit parentheses stress the precedence enough to make the 5649 // additional break unnecessary. 5650 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5651 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 5652 "}"); 5653 // This cases is borderline, but with the indentation it is still readable. 5654 verifyFormat( 5655 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5656 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5657 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 5658 "}", 5659 getLLVMStyleWithColumns(75)); 5660 5661 // If the LHS is a binary expression, we should still use the additional break 5662 // as otherwise the formatting hides the operator precedence. 5663 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5664 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5665 " 5) {\n" 5666 "}"); 5667 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5668 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n" 5669 " 5) {\n" 5670 "}"); 5671 5672 FormatStyle OnePerLine = getLLVMStyle(); 5673 OnePerLine.BinPackParameters = false; 5674 verifyFormat( 5675 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5676 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5677 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 5678 OnePerLine); 5679 5680 verifyFormat("int i = someFunction(aaaaaaa, 0)\n" 5681 " .aaa(aaaaaaaaaaaaa) *\n" 5682 " aaaaaaa +\n" 5683 " aaaaaaa;", 5684 getLLVMStyleWithColumns(40)); 5685 } 5686 5687 TEST_F(FormatTest, ExpressionIndentation) { 5688 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5689 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5690 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5691 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5692 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 5693 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 5694 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5695 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 5696 " ccccccccccccccccccccccccccccccccccccccccc;"); 5697 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5698 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5699 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5700 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 5701 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5702 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5703 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5704 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 5705 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5706 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5707 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5708 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 5709 verifyFormat("if () {\n" 5710 "} else if (aaaaa && bbbbb > // break\n" 5711 " ccccc) {\n" 5712 "}"); 5713 verifyFormat("if () {\n" 5714 "} else if constexpr (aaaaa && bbbbb > // break\n" 5715 " ccccc) {\n" 5716 "}"); 5717 verifyFormat("if () {\n" 5718 "} else if CONSTEXPR (aaaaa && bbbbb > // break\n" 5719 " ccccc) {\n" 5720 "}"); 5721 verifyFormat("if () {\n" 5722 "} else if (aaaaa &&\n" 5723 " bbbbb > // break\n" 5724 " ccccc &&\n" 5725 " ddddd) {\n" 5726 "}"); 5727 5728 // Presence of a trailing comment used to change indentation of b. 5729 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 5730 " b;\n" 5731 "return aaaaaaaaaaaaaaaaaaa +\n" 5732 " b; //", 5733 getLLVMStyleWithColumns(30)); 5734 } 5735 5736 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 5737 // Not sure what the best system is here. Like this, the LHS can be found 5738 // immediately above an operator (everything with the same or a higher 5739 // indent). The RHS is aligned right of the operator and so compasses 5740 // everything until something with the same indent as the operator is found. 5741 // FIXME: Is this a good system? 5742 FormatStyle Style = getLLVMStyle(); 5743 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 5744 verifyFormat( 5745 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5746 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5747 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5748 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5749 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5750 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5751 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5752 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5753 " > ccccccccccccccccccccccccccccccccccccccccc;", 5754 Style); 5755 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5756 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5757 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5758 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5759 Style); 5760 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5761 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5762 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5763 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5764 Style); 5765 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5766 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5767 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5768 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5769 Style); 5770 verifyFormat("if () {\n" 5771 "} else if (aaaaa\n" 5772 " && bbbbb // break\n" 5773 " > ccccc) {\n" 5774 "}", 5775 Style); 5776 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5777 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 5778 Style); 5779 verifyFormat("return (a)\n" 5780 " // comment\n" 5781 " + b;", 5782 Style); 5783 verifyFormat( 5784 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5785 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5786 " + cc;", 5787 Style); 5788 5789 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5790 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5791 Style); 5792 5793 // Forced by comments. 5794 verifyFormat( 5795 "unsigned ContentSize =\n" 5796 " sizeof(int16_t) // DWARF ARange version number\n" 5797 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 5798 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 5799 " + sizeof(int8_t); // Segment Size (in bytes)"); 5800 5801 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 5802 " == boost::fusion::at_c<1>(iiii).second;", 5803 Style); 5804 5805 Style.ColumnLimit = 60; 5806 verifyFormat("zzzzzzzzzz\n" 5807 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5808 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 5809 Style); 5810 5811 Style.ColumnLimit = 80; 5812 Style.IndentWidth = 4; 5813 Style.TabWidth = 4; 5814 Style.UseTab = FormatStyle::UT_Always; 5815 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5816 Style.AlignOperands = FormatStyle::OAS_DontAlign; 5817 EXPECT_EQ("return someVeryVeryLongConditionThatBarelyFitsOnALine\n" 5818 "\t&& (someOtherLongishConditionPart1\n" 5819 "\t\t|| someOtherEvenLongerNestedConditionPart2);", 5820 format("return someVeryVeryLongConditionThatBarelyFitsOnALine && " 5821 "(someOtherLongishConditionPart1 || " 5822 "someOtherEvenLongerNestedConditionPart2);", 5823 Style)); 5824 } 5825 5826 TEST_F(FormatTest, ExpressionIndentationStrictAlign) { 5827 FormatStyle Style = getLLVMStyle(); 5828 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 5829 Style.AlignOperands = FormatStyle::OAS_AlignAfterOperator; 5830 5831 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5832 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5833 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5834 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5835 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5836 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5837 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5838 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5839 " > ccccccccccccccccccccccccccccccccccccccccc;", 5840 Style); 5841 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5842 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5843 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5844 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5845 Style); 5846 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5847 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5848 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5849 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5850 Style); 5851 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5852 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5853 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5854 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5855 Style); 5856 verifyFormat("if () {\n" 5857 "} else if (aaaaa\n" 5858 " && bbbbb // break\n" 5859 " > ccccc) {\n" 5860 "}", 5861 Style); 5862 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5863 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 5864 Style); 5865 verifyFormat("return (a)\n" 5866 " // comment\n" 5867 " + b;", 5868 Style); 5869 verifyFormat( 5870 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5871 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5872 " + cc;", 5873 Style); 5874 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 5875 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 5876 " : 3333333333333333;", 5877 Style); 5878 verifyFormat( 5879 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 5880 " : ccccccccccccccc ? dddddddddddddddddd\n" 5881 " : eeeeeeeeeeeeeeeeee)\n" 5882 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 5883 " : 3333333333333333;", 5884 Style); 5885 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5886 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5887 Style); 5888 5889 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 5890 " == boost::fusion::at_c<1>(iiii).second;", 5891 Style); 5892 5893 Style.ColumnLimit = 60; 5894 verifyFormat("zzzzzzzzzzzzz\n" 5895 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5896 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 5897 Style); 5898 5899 // Forced by comments. 5900 Style.ColumnLimit = 80; 5901 verifyFormat( 5902 "unsigned ContentSize\n" 5903 " = sizeof(int16_t) // DWARF ARange version number\n" 5904 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 5905 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 5906 " + sizeof(int8_t); // Segment Size (in bytes)", 5907 Style); 5908 5909 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 5910 verifyFormat( 5911 "unsigned ContentSize =\n" 5912 " sizeof(int16_t) // DWARF ARange version number\n" 5913 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 5914 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 5915 " + sizeof(int8_t); // Segment Size (in bytes)", 5916 Style); 5917 5918 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 5919 verifyFormat( 5920 "unsigned ContentSize =\n" 5921 " sizeof(int16_t) // DWARF ARange version number\n" 5922 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 5923 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 5924 " + sizeof(int8_t); // Segment Size (in bytes)", 5925 Style); 5926 } 5927 5928 TEST_F(FormatTest, EnforcedOperatorWraps) { 5929 // Here we'd like to wrap after the || operators, but a comment is forcing an 5930 // earlier wrap. 5931 verifyFormat("bool x = aaaaa //\n" 5932 " || bbbbb\n" 5933 " //\n" 5934 " || cccc;"); 5935 } 5936 5937 TEST_F(FormatTest, NoOperandAlignment) { 5938 FormatStyle Style = getLLVMStyle(); 5939 Style.AlignOperands = FormatStyle::OAS_DontAlign; 5940 verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n" 5941 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5942 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5943 Style); 5944 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 5945 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5946 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5947 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5948 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5949 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5950 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5951 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5952 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5953 " > ccccccccccccccccccccccccccccccccccccccccc;", 5954 Style); 5955 5956 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5957 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5958 " + cc;", 5959 Style); 5960 verifyFormat("int a = aa\n" 5961 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5962 " * cccccccccccccccccccccccccccccccccccc;\n", 5963 Style); 5964 5965 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5966 verifyFormat("return (a > b\n" 5967 " // comment1\n" 5968 " // comment2\n" 5969 " || c);", 5970 Style); 5971 } 5972 5973 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { 5974 FormatStyle Style = getLLVMStyle(); 5975 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 5976 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5977 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5978 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 5979 Style); 5980 } 5981 5982 TEST_F(FormatTest, AllowBinPackingInsideArguments) { 5983 FormatStyle Style = getLLVMStyle(); 5984 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 5985 Style.BinPackArguments = false; 5986 Style.ColumnLimit = 40; 5987 verifyFormat("void test() {\n" 5988 " someFunction(\n" 5989 " this + argument + is + quite\n" 5990 " + long + so + it + gets + wrapped\n" 5991 " + but + remains + bin - packed);\n" 5992 "}", 5993 Style); 5994 verifyFormat("void test() {\n" 5995 " someFunction(arg1,\n" 5996 " this + argument + is\n" 5997 " + quite + long + so\n" 5998 " + it + gets + wrapped\n" 5999 " + but + remains + bin\n" 6000 " - packed,\n" 6001 " arg3);\n" 6002 "}", 6003 Style); 6004 verifyFormat("void test() {\n" 6005 " someFunction(\n" 6006 " arg1,\n" 6007 " this + argument + has\n" 6008 " + anotherFunc(nested,\n" 6009 " calls + whose\n" 6010 " + arguments\n" 6011 " + are + also\n" 6012 " + wrapped,\n" 6013 " in + addition)\n" 6014 " + to + being + bin - packed,\n" 6015 " arg3);\n" 6016 "}", 6017 Style); 6018 6019 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 6020 verifyFormat("void test() {\n" 6021 " someFunction(\n" 6022 " arg1,\n" 6023 " this + argument + has +\n" 6024 " anotherFunc(nested,\n" 6025 " calls + whose +\n" 6026 " arguments +\n" 6027 " are + also +\n" 6028 " wrapped,\n" 6029 " in + addition) +\n" 6030 " to + being + bin - packed,\n" 6031 " arg3);\n" 6032 "}", 6033 Style); 6034 } 6035 6036 TEST_F(FormatTest, ConstructorInitializers) { 6037 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 6038 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 6039 getLLVMStyleWithColumns(45)); 6040 verifyFormat("Constructor()\n" 6041 " : Inttializer(FitsOnTheLine) {}", 6042 getLLVMStyleWithColumns(44)); 6043 verifyFormat("Constructor()\n" 6044 " : Inttializer(FitsOnTheLine) {}", 6045 getLLVMStyleWithColumns(43)); 6046 6047 verifyFormat("template <typename T>\n" 6048 "Constructor() : Initializer(FitsOnTheLine) {}", 6049 getLLVMStyleWithColumns(45)); 6050 6051 verifyFormat( 6052 "SomeClass::Constructor()\n" 6053 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 6054 6055 verifyFormat( 6056 "SomeClass::Constructor()\n" 6057 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6058 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 6059 verifyFormat( 6060 "SomeClass::Constructor()\n" 6061 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6062 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 6063 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6064 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6065 " : aaaaaaaaaa(aaaaaa) {}"); 6066 6067 verifyFormat("Constructor()\n" 6068 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6069 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6070 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6071 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 6072 6073 verifyFormat("Constructor()\n" 6074 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6075 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 6076 6077 verifyFormat("Constructor(int Parameter = 0)\n" 6078 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 6079 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 6080 verifyFormat("Constructor()\n" 6081 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 6082 "}", 6083 getLLVMStyleWithColumns(60)); 6084 verifyFormat("Constructor()\n" 6085 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6086 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 6087 6088 // Here a line could be saved by splitting the second initializer onto two 6089 // lines, but that is not desirable. 6090 verifyFormat("Constructor()\n" 6091 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 6092 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 6093 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 6094 6095 FormatStyle OnePerLine = getLLVMStyle(); 6096 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_Never; 6097 verifyFormat("MyClass::MyClass()\n" 6098 " : a(a),\n" 6099 " b(b),\n" 6100 " c(c) {}", 6101 OnePerLine); 6102 verifyFormat("MyClass::MyClass()\n" 6103 " : a(a), // comment\n" 6104 " b(b),\n" 6105 " c(c) {}", 6106 OnePerLine); 6107 verifyFormat("MyClass::MyClass(int a)\n" 6108 " : b(a), // comment\n" 6109 " c(a + 1) { // lined up\n" 6110 "}", 6111 OnePerLine); 6112 verifyFormat("Constructor()\n" 6113 " : a(b, b, b) {}", 6114 OnePerLine); 6115 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6116 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 6117 verifyFormat("SomeClass::Constructor()\n" 6118 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6119 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6120 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6121 OnePerLine); 6122 verifyFormat("SomeClass::Constructor()\n" 6123 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 6124 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6125 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6126 OnePerLine); 6127 verifyFormat("MyClass::MyClass(int var)\n" 6128 " : some_var_(var), // 4 space indent\n" 6129 " some_other_var_(var + 1) { // lined up\n" 6130 "}", 6131 OnePerLine); 6132 verifyFormat("Constructor()\n" 6133 " : aaaaa(aaaaaa),\n" 6134 " aaaaa(aaaaaa),\n" 6135 " aaaaa(aaaaaa),\n" 6136 " aaaaa(aaaaaa),\n" 6137 " aaaaa(aaaaaa) {}", 6138 OnePerLine); 6139 verifyFormat("Constructor()\n" 6140 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 6141 " aaaaaaaaaaaaaaaaaaaaaa) {}", 6142 OnePerLine); 6143 OnePerLine.BinPackParameters = false; 6144 verifyFormat( 6145 "Constructor()\n" 6146 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n" 6147 " aaaaaaaaaaa().aaa(),\n" 6148 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6149 OnePerLine); 6150 OnePerLine.ColumnLimit = 60; 6151 verifyFormat("Constructor()\n" 6152 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6153 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 6154 OnePerLine); 6155 6156 EXPECT_EQ("Constructor()\n" 6157 " : // Comment forcing unwanted break.\n" 6158 " aaaa(aaaa) {}", 6159 format("Constructor() :\n" 6160 " // Comment forcing unwanted break.\n" 6161 " aaaa(aaaa) {}")); 6162 } 6163 6164 TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) { 6165 FormatStyle Style = getLLVMStyle(); 6166 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 6167 Style.ColumnLimit = 60; 6168 Style.BinPackParameters = false; 6169 6170 for (int i = 0; i < 4; ++i) { 6171 // Test all combinations of parameters that should not have an effect. 6172 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1; 6173 Style.AllowAllArgumentsOnNextLine = i & 2; 6174 6175 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6176 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 6177 verifyFormat("Constructor()\n" 6178 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6179 Style); 6180 verifyFormat("Constructor() : a(a), b(b) {}", Style); 6181 6182 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6183 verifyFormat("Constructor()\n" 6184 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 6185 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 6186 Style); 6187 verifyFormat("Constructor() : a(a), b(b) {}", Style); 6188 6189 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 6190 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6191 verifyFormat("Constructor()\n" 6192 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6193 Style); 6194 6195 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6196 verifyFormat("Constructor()\n" 6197 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6198 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6199 Style); 6200 6201 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 6202 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6203 verifyFormat("Constructor() :\n" 6204 " aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6205 Style); 6206 6207 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6208 verifyFormat("Constructor() :\n" 6209 " aaaaaaaaaaaaaaaaaa(a),\n" 6210 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6211 Style); 6212 } 6213 6214 // Test interactions between AllowAllParametersOfDeclarationOnNextLine and 6215 // AllowAllConstructorInitializersOnNextLine in all 6216 // BreakConstructorInitializers modes 6217 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 6218 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6219 verifyFormat("SomeClassWithALongName::Constructor(\n" 6220 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n" 6221 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 6222 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 6223 Style); 6224 6225 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6226 verifyFormat("SomeClassWithALongName::Constructor(\n" 6227 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6228 " int bbbbbbbbbbbbb,\n" 6229 " int cccccccccccccccc)\n" 6230 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6231 Style); 6232 6233 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6234 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6235 verifyFormat("SomeClassWithALongName::Constructor(\n" 6236 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6237 " int bbbbbbbbbbbbb)\n" 6238 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 6239 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 6240 Style); 6241 6242 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 6243 6244 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6245 verifyFormat("SomeClassWithALongName::Constructor(\n" 6246 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n" 6247 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6248 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6249 Style); 6250 6251 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6252 verifyFormat("SomeClassWithALongName::Constructor(\n" 6253 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6254 " int bbbbbbbbbbbbb,\n" 6255 " int cccccccccccccccc)\n" 6256 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6257 Style); 6258 6259 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6260 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6261 verifyFormat("SomeClassWithALongName::Constructor(\n" 6262 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6263 " int bbbbbbbbbbbbb)\n" 6264 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6265 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6266 Style); 6267 6268 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 6269 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6270 verifyFormat("SomeClassWithALongName::Constructor(\n" 6271 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n" 6272 " aaaaaaaaaaaaaaaaaaaa(a),\n" 6273 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6274 Style); 6275 6276 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6277 verifyFormat("SomeClassWithALongName::Constructor(\n" 6278 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6279 " int bbbbbbbbbbbbb,\n" 6280 " int cccccccccccccccc) :\n" 6281 " aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6282 Style); 6283 6284 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6285 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6286 verifyFormat("SomeClassWithALongName::Constructor(\n" 6287 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6288 " int bbbbbbbbbbbbb) :\n" 6289 " aaaaaaaaaaaaaaaaaaaa(a),\n" 6290 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6291 Style); 6292 } 6293 6294 TEST_F(FormatTest, AllowAllArgumentsOnNextLine) { 6295 FormatStyle Style = getLLVMStyle(); 6296 Style.ColumnLimit = 60; 6297 Style.BinPackArguments = false; 6298 for (int i = 0; i < 4; ++i) { 6299 // Test all combinations of parameters that should not have an effect. 6300 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1; 6301 Style.PackConstructorInitializers = 6302 i & 2 ? FormatStyle::PCIS_BinPack : FormatStyle::PCIS_Never; 6303 6304 Style.AllowAllArgumentsOnNextLine = true; 6305 verifyFormat("void foo() {\n" 6306 " FunctionCallWithReallyLongName(\n" 6307 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n" 6308 "}", 6309 Style); 6310 Style.AllowAllArgumentsOnNextLine = false; 6311 verifyFormat("void foo() {\n" 6312 " FunctionCallWithReallyLongName(\n" 6313 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6314 " bbbbbbbbbbbb);\n" 6315 "}", 6316 Style); 6317 6318 Style.AllowAllArgumentsOnNextLine = true; 6319 verifyFormat("void foo() {\n" 6320 " auto VariableWithReallyLongName = {\n" 6321 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n" 6322 "}", 6323 Style); 6324 Style.AllowAllArgumentsOnNextLine = false; 6325 verifyFormat("void foo() {\n" 6326 " auto VariableWithReallyLongName = {\n" 6327 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6328 " bbbbbbbbbbbb};\n" 6329 "}", 6330 Style); 6331 } 6332 6333 // This parameter should not affect declarations. 6334 Style.BinPackParameters = false; 6335 Style.AllowAllArgumentsOnNextLine = false; 6336 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6337 verifyFormat("void FunctionCallWithReallyLongName(\n" 6338 " int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);", 6339 Style); 6340 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6341 verifyFormat("void FunctionCallWithReallyLongName(\n" 6342 " int aaaaaaaaaaaaaaaaaaaaaaa,\n" 6343 " int bbbbbbbbbbbb);", 6344 Style); 6345 } 6346 6347 TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) { 6348 // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign 6349 // and BAS_Align. 6350 auto Style = getLLVMStyle(); 6351 Style.ColumnLimit = 35; 6352 StringRef Input = "functionCall(paramA, paramB, paramC);\n" 6353 "void functionDecl(int A, int B, int C);"; 6354 Style.AllowAllArgumentsOnNextLine = false; 6355 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6356 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 6357 " paramC);\n" 6358 "void functionDecl(int A, int B,\n" 6359 " int C);"), 6360 format(Input, Style)); 6361 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 6362 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 6363 " paramC);\n" 6364 "void functionDecl(int A, int B,\n" 6365 " int C);"), 6366 format(Input, Style)); 6367 // However, BAS_AlwaysBreak should take precedence over 6368 // AllowAllArgumentsOnNextLine. 6369 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 6370 EXPECT_EQ(StringRef("functionCall(\n" 6371 " paramA, paramB, paramC);\n" 6372 "void functionDecl(\n" 6373 " int A, int B, int C);"), 6374 format(Input, Style)); 6375 6376 // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the 6377 // first argument. 6378 Style.AllowAllArgumentsOnNextLine = true; 6379 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 6380 EXPECT_EQ(StringRef("functionCall(\n" 6381 " paramA, paramB, paramC);\n" 6382 "void functionDecl(\n" 6383 " int A, int B, int C);"), 6384 format(Input, Style)); 6385 // It wouldn't fit on one line with aligned parameters so this setting 6386 // doesn't change anything for BAS_Align. 6387 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 6388 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 6389 " paramC);\n" 6390 "void functionDecl(int A, int B,\n" 6391 " int C);"), 6392 format(Input, Style)); 6393 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6394 EXPECT_EQ(StringRef("functionCall(\n" 6395 " paramA, paramB, paramC);\n" 6396 "void functionDecl(\n" 6397 " int A, int B, int C);"), 6398 format(Input, Style)); 6399 } 6400 6401 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) { 6402 FormatStyle Style = getLLVMStyle(); 6403 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 6404 6405 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 6406 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}", 6407 getStyleWithColumns(Style, 45)); 6408 verifyFormat("Constructor() :\n" 6409 " Initializer(FitsOnTheLine) {}", 6410 getStyleWithColumns(Style, 44)); 6411 verifyFormat("Constructor() :\n" 6412 " Initializer(FitsOnTheLine) {}", 6413 getStyleWithColumns(Style, 43)); 6414 6415 verifyFormat("template <typename T>\n" 6416 "Constructor() : Initializer(FitsOnTheLine) {}", 6417 getStyleWithColumns(Style, 50)); 6418 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6419 verifyFormat( 6420 "SomeClass::Constructor() :\n" 6421 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 6422 Style); 6423 6424 Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack; 6425 verifyFormat( 6426 "SomeClass::Constructor() :\n" 6427 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 6428 Style); 6429 6430 verifyFormat( 6431 "SomeClass::Constructor() :\n" 6432 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6433 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6434 Style); 6435 verifyFormat( 6436 "SomeClass::Constructor() :\n" 6437 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6438 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 6439 Style); 6440 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6441 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 6442 " aaaaaaaaaa(aaaaaa) {}", 6443 Style); 6444 6445 verifyFormat("Constructor() :\n" 6446 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6447 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6448 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6449 " aaaaaaaaaaaaaaaaaaaaaaa() {}", 6450 Style); 6451 6452 verifyFormat("Constructor() :\n" 6453 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6454 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6455 Style); 6456 6457 verifyFormat("Constructor(int Parameter = 0) :\n" 6458 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 6459 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}", 6460 Style); 6461 verifyFormat("Constructor() :\n" 6462 " aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 6463 "}", 6464 getStyleWithColumns(Style, 60)); 6465 verifyFormat("Constructor() :\n" 6466 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6467 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}", 6468 Style); 6469 6470 // Here a line could be saved by splitting the second initializer onto two 6471 // lines, but that is not desirable. 6472 verifyFormat("Constructor() :\n" 6473 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 6474 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 6475 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6476 Style); 6477 6478 FormatStyle OnePerLine = Style; 6479 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6480 verifyFormat("SomeClass::Constructor() :\n" 6481 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6482 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6483 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6484 OnePerLine); 6485 verifyFormat("SomeClass::Constructor() :\n" 6486 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 6487 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6488 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6489 OnePerLine); 6490 verifyFormat("MyClass::MyClass(int var) :\n" 6491 " some_var_(var), // 4 space indent\n" 6492 " some_other_var_(var + 1) { // lined up\n" 6493 "}", 6494 OnePerLine); 6495 verifyFormat("Constructor() :\n" 6496 " aaaaa(aaaaaa),\n" 6497 " aaaaa(aaaaaa),\n" 6498 " aaaaa(aaaaaa),\n" 6499 " aaaaa(aaaaaa),\n" 6500 " aaaaa(aaaaaa) {}", 6501 OnePerLine); 6502 verifyFormat("Constructor() :\n" 6503 " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 6504 " aaaaaaaaaaaaaaaaaaaaaa) {}", 6505 OnePerLine); 6506 OnePerLine.BinPackParameters = false; 6507 verifyFormat("Constructor() :\n" 6508 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 6509 " aaaaaaaaaaa().aaa(),\n" 6510 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6511 OnePerLine); 6512 OnePerLine.ColumnLimit = 60; 6513 verifyFormat("Constructor() :\n" 6514 " aaaaaaaaaaaaaaaaaaaa(a),\n" 6515 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 6516 OnePerLine); 6517 6518 EXPECT_EQ("Constructor() :\n" 6519 " // Comment forcing unwanted break.\n" 6520 " aaaa(aaaa) {}", 6521 format("Constructor() :\n" 6522 " // Comment forcing unwanted break.\n" 6523 " aaaa(aaaa) {}", 6524 Style)); 6525 6526 Style.ColumnLimit = 0; 6527 verifyFormat("SomeClass::Constructor() :\n" 6528 " a(a) {}", 6529 Style); 6530 verifyFormat("SomeClass::Constructor() noexcept :\n" 6531 " a(a) {}", 6532 Style); 6533 verifyFormat("SomeClass::Constructor() :\n" 6534 " a(a), b(b), c(c) {}", 6535 Style); 6536 verifyFormat("SomeClass::Constructor() :\n" 6537 " a(a) {\n" 6538 " foo();\n" 6539 " bar();\n" 6540 "}", 6541 Style); 6542 6543 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 6544 verifyFormat("SomeClass::Constructor() :\n" 6545 " a(a), b(b), c(c) {\n" 6546 "}", 6547 Style); 6548 verifyFormat("SomeClass::Constructor() :\n" 6549 " a(a) {\n" 6550 "}", 6551 Style); 6552 6553 Style.ColumnLimit = 80; 6554 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 6555 Style.ConstructorInitializerIndentWidth = 2; 6556 verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style); 6557 verifyFormat("SomeClass::Constructor() :\n" 6558 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6559 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}", 6560 Style); 6561 6562 // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as 6563 // well 6564 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 6565 verifyFormat( 6566 "class SomeClass\n" 6567 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6568 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 6569 Style); 6570 Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 6571 verifyFormat( 6572 "class SomeClass\n" 6573 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6574 " , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 6575 Style); 6576 Style.BreakInheritanceList = FormatStyle::BILS_AfterColon; 6577 verifyFormat( 6578 "class SomeClass :\n" 6579 " public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6580 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 6581 Style); 6582 Style.BreakInheritanceList = FormatStyle::BILS_AfterComma; 6583 verifyFormat( 6584 "class SomeClass\n" 6585 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6586 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 6587 Style); 6588 } 6589 6590 #ifndef EXPENSIVE_CHECKS 6591 // Expensive checks enables libstdc++ checking which includes validating the 6592 // state of ranges used in std::priority_queue - this blows out the 6593 // runtime/scalability of the function and makes this test unacceptably slow. 6594 TEST_F(FormatTest, MemoizationTests) { 6595 // This breaks if the memoization lookup does not take \c Indent and 6596 // \c LastSpace into account. 6597 verifyFormat( 6598 "extern CFRunLoopTimerRef\n" 6599 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 6600 " CFTimeInterval interval, CFOptionFlags flags,\n" 6601 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 6602 " CFRunLoopTimerContext *context) {}"); 6603 6604 // Deep nesting somewhat works around our memoization. 6605 verifyFormat( 6606 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 6607 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 6608 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 6609 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 6610 " aaaaa())))))))))))))))))))))))))))))))))))))));", 6611 getLLVMStyleWithColumns(65)); 6612 verifyFormat( 6613 "aaaaa(\n" 6614 " aaaaa,\n" 6615 " aaaaa(\n" 6616 " aaaaa,\n" 6617 " aaaaa(\n" 6618 " aaaaa,\n" 6619 " aaaaa(\n" 6620 " aaaaa,\n" 6621 " aaaaa(\n" 6622 " aaaaa,\n" 6623 " aaaaa(\n" 6624 " aaaaa,\n" 6625 " aaaaa(\n" 6626 " aaaaa,\n" 6627 " aaaaa(\n" 6628 " aaaaa,\n" 6629 " aaaaa(\n" 6630 " aaaaa,\n" 6631 " aaaaa(\n" 6632 " aaaaa,\n" 6633 " aaaaa(\n" 6634 " aaaaa,\n" 6635 " aaaaa(\n" 6636 " aaaaa,\n" 6637 " aaaaa))))))))))));", 6638 getLLVMStyleWithColumns(65)); 6639 verifyFormat( 6640 "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" 6641 " a),\n" 6642 " a),\n" 6643 " a),\n" 6644 " a),\n" 6645 " a),\n" 6646 " a),\n" 6647 " a),\n" 6648 " a),\n" 6649 " a),\n" 6650 " a),\n" 6651 " a),\n" 6652 " a),\n" 6653 " a),\n" 6654 " a),\n" 6655 " a),\n" 6656 " a),\n" 6657 " a)", 6658 getLLVMStyleWithColumns(65)); 6659 6660 // This test takes VERY long when memoization is broken. 6661 FormatStyle OnePerLine = getLLVMStyle(); 6662 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6663 OnePerLine.BinPackParameters = false; 6664 std::string input = "Constructor()\n" 6665 " : aaaa(a,\n"; 6666 for (unsigned i = 0, e = 80; i != e; ++i) { 6667 input += " a,\n"; 6668 } 6669 input += " a) {}"; 6670 verifyFormat(input, OnePerLine); 6671 } 6672 #endif 6673 6674 TEST_F(FormatTest, BreaksAsHighAsPossible) { 6675 verifyFormat( 6676 "void f() {\n" 6677 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 6678 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 6679 " f();\n" 6680 "}"); 6681 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 6682 " Intervals[i - 1].getRange().getLast()) {\n}"); 6683 } 6684 6685 TEST_F(FormatTest, BreaksFunctionDeclarations) { 6686 // Principially, we break function declarations in a certain order: 6687 // 1) break amongst arguments. 6688 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 6689 " Cccccccccccccc cccccccccccccc);"); 6690 verifyFormat("template <class TemplateIt>\n" 6691 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 6692 " TemplateIt *stop) {}"); 6693 6694 // 2) break after return type. 6695 verifyFormat( 6696 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6697 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", 6698 getGoogleStyle()); 6699 6700 // 3) break after (. 6701 verifyFormat( 6702 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 6703 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", 6704 getGoogleStyle()); 6705 6706 // 4) break before after nested name specifiers. 6707 verifyFormat( 6708 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6709 "SomeClasssssssssssssssssssssssssssssssssssssss::\n" 6710 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", 6711 getGoogleStyle()); 6712 6713 // However, there are exceptions, if a sufficient amount of lines can be 6714 // saved. 6715 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 6716 // more adjusting. 6717 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 6718 " Cccccccccccccc cccccccccc,\n" 6719 " Cccccccccccccc cccccccccc,\n" 6720 " Cccccccccccccc cccccccccc,\n" 6721 " Cccccccccccccc cccccccccc);"); 6722 verifyFormat( 6723 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6724 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6725 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6726 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", 6727 getGoogleStyle()); 6728 verifyFormat( 6729 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 6730 " Cccccccccccccc cccccccccc,\n" 6731 " Cccccccccccccc cccccccccc,\n" 6732 " Cccccccccccccc cccccccccc,\n" 6733 " Cccccccccccccc cccccccccc,\n" 6734 " Cccccccccccccc cccccccccc,\n" 6735 " Cccccccccccccc cccccccccc);"); 6736 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 6737 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6738 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6739 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6740 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 6741 6742 // Break after multi-line parameters. 6743 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6744 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6745 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6746 " bbbb bbbb);"); 6747 verifyFormat("void SomeLoooooooooooongFunction(\n" 6748 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6749 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6750 " int bbbbbbbbbbbbb);"); 6751 6752 // Treat overloaded operators like other functions. 6753 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 6754 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 6755 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 6756 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 6757 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 6758 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 6759 verifyGoogleFormat( 6760 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 6761 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 6762 verifyGoogleFormat( 6763 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 6764 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 6765 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6766 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 6767 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 6768 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 6769 verifyGoogleFormat( 6770 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" 6771 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6772 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); 6773 verifyGoogleFormat("template <typename T>\n" 6774 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6775 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n" 6776 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);"); 6777 6778 FormatStyle Style = getLLVMStyle(); 6779 Style.PointerAlignment = FormatStyle::PAS_Left; 6780 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6781 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", 6782 Style); 6783 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 6784 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6785 Style); 6786 } 6787 6788 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) { 6789 // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516: 6790 // Prefer keeping `::` followed by `operator` together. 6791 EXPECT_EQ("const aaaa::bbbbbbb &\n" 6792 "ccccccccc::operator++() {\n" 6793 " stuff();\n" 6794 "}", 6795 format("const aaaa::bbbbbbb\n" 6796 "&ccccccccc::operator++() { stuff(); }", 6797 getLLVMStyleWithColumns(40))); 6798 } 6799 6800 TEST_F(FormatTest, TrailingReturnType) { 6801 verifyFormat("auto foo() -> int;\n"); 6802 // correct trailing return type spacing 6803 verifyFormat("auto operator->() -> int;\n"); 6804 verifyFormat("auto operator++(int) -> int;\n"); 6805 6806 verifyFormat("struct S {\n" 6807 " auto bar() const -> int;\n" 6808 "};"); 6809 verifyFormat("template <size_t Order, typename T>\n" 6810 "auto load_img(const std::string &filename)\n" 6811 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 6812 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 6813 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 6814 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 6815 verifyFormat("template <typename T>\n" 6816 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" 6817 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); 6818 6819 // Not trailing return types. 6820 verifyFormat("void f() { auto a = b->c(); }"); 6821 verifyFormat("auto a = p->foo();"); 6822 verifyFormat("int a = p->foo();"); 6823 verifyFormat("auto lmbd = [] NOEXCEPT -> int { return 0; };"); 6824 } 6825 6826 TEST_F(FormatTest, DeductionGuides) { 6827 verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;"); 6828 verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;"); 6829 verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;"); 6830 verifyFormat( 6831 "template <class... T>\n" 6832 "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;"); 6833 verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;"); 6834 verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;"); 6835 verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;"); 6836 verifyFormat("template <class T> A() -> A<(3 < 2)>;"); 6837 verifyFormat("template <class T> A() -> A<((3) < (2))>;"); 6838 verifyFormat("template <class T> x() -> x<1>;"); 6839 verifyFormat("template <class T> explicit x(T &) -> x<1>;"); 6840 6841 // Ensure not deduction guides. 6842 verifyFormat("c()->f<int>();"); 6843 verifyFormat("x()->foo<1>;"); 6844 verifyFormat("x = p->foo<3>();"); 6845 verifyFormat("x()->x<1>();"); 6846 verifyFormat("x()->x<1>;"); 6847 } 6848 6849 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 6850 // Avoid breaking before trailing 'const' or other trailing annotations, if 6851 // they are not function-like. 6852 FormatStyle Style = getGoogleStyle(); 6853 Style.ColumnLimit = 47; 6854 verifyFormat("void someLongFunction(\n" 6855 " int someLoooooooooooooongParameter) const {\n}", 6856 getLLVMStyleWithColumns(47)); 6857 verifyFormat("LoooooongReturnType\n" 6858 "someLoooooooongFunction() const {}", 6859 getLLVMStyleWithColumns(47)); 6860 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 6861 " const {}", 6862 Style); 6863 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 6864 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 6865 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 6866 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 6867 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 6868 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 6869 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 6870 " aaaaaaaaaaa aaaaa) const override;"); 6871 verifyGoogleFormat( 6872 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 6873 " const override;"); 6874 6875 // Even if the first parameter has to be wrapped. 6876 verifyFormat("void someLongFunction(\n" 6877 " int someLongParameter) const {}", 6878 getLLVMStyleWithColumns(46)); 6879 verifyFormat("void someLongFunction(\n" 6880 " int someLongParameter) const {}", 6881 Style); 6882 verifyFormat("void someLongFunction(\n" 6883 " int someLongParameter) override {}", 6884 Style); 6885 verifyFormat("void someLongFunction(\n" 6886 " int someLongParameter) OVERRIDE {}", 6887 Style); 6888 verifyFormat("void someLongFunction(\n" 6889 " int someLongParameter) final {}", 6890 Style); 6891 verifyFormat("void someLongFunction(\n" 6892 " int someLongParameter) FINAL {}", 6893 Style); 6894 verifyFormat("void someLongFunction(\n" 6895 " int parameter) const override {}", 6896 Style); 6897 6898 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 6899 verifyFormat("void someLongFunction(\n" 6900 " int someLongParameter) const\n" 6901 "{\n" 6902 "}", 6903 Style); 6904 6905 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 6906 verifyFormat("void someLongFunction(\n" 6907 " int someLongParameter) const\n" 6908 " {\n" 6909 " }", 6910 Style); 6911 6912 // Unless these are unknown annotations. 6913 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 6914 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6915 " LONG_AND_UGLY_ANNOTATION;"); 6916 6917 // Breaking before function-like trailing annotations is fine to keep them 6918 // close to their arguments. 6919 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6920 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 6921 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 6922 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 6923 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 6924 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 6925 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 6926 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 6927 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 6928 6929 verifyFormat( 6930 "void aaaaaaaaaaaaaaaaaa()\n" 6931 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 6932 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 6933 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6934 " __attribute__((unused));"); 6935 verifyGoogleFormat( 6936 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6937 " GUARDED_BY(aaaaaaaaaaaa);"); 6938 verifyGoogleFormat( 6939 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6940 " GUARDED_BY(aaaaaaaaaaaa);"); 6941 verifyGoogleFormat( 6942 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 6943 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6944 verifyGoogleFormat( 6945 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 6946 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 6947 } 6948 6949 TEST_F(FormatTest, FunctionAnnotations) { 6950 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 6951 "int OldFunction(const string ¶meter) {}"); 6952 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 6953 "string OldFunction(const string ¶meter) {}"); 6954 verifyFormat("template <typename T>\n" 6955 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 6956 "string OldFunction(const string ¶meter) {}"); 6957 6958 // Not function annotations. 6959 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6960 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 6961 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" 6962 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); 6963 verifyFormat("MACRO(abc).function() // wrap\n" 6964 " << abc;"); 6965 verifyFormat("MACRO(abc)->function() // wrap\n" 6966 " << abc;"); 6967 verifyFormat("MACRO(abc)::function() // wrap\n" 6968 " << abc;"); 6969 } 6970 6971 TEST_F(FormatTest, BreaksDesireably) { 6972 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 6973 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 6974 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 6975 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6976 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 6977 "}"); 6978 6979 verifyFormat( 6980 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6981 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 6982 6983 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6984 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6985 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 6986 6987 verifyFormat( 6988 "aaaaaaaa(aaaaaaaaaaaaa,\n" 6989 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6990 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 6991 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6992 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 6993 6994 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 6995 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6996 6997 verifyFormat( 6998 "void f() {\n" 6999 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 7000 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 7001 "}"); 7002 verifyFormat( 7003 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7004 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 7005 verifyFormat( 7006 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7007 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 7008 verifyFormat( 7009 "aaaaaa(aaa,\n" 7010 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7011 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7012 " aaaa);"); 7013 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7014 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7015 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7016 7017 // Indent consistently independent of call expression and unary operator. 7018 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 7019 " dddddddddddddddddddddddddddddd));"); 7020 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 7021 " dddddddddddddddddddddddddddddd));"); 7022 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 7023 " dddddddddddddddddddddddddddddd));"); 7024 7025 // This test case breaks on an incorrect memoization, i.e. an optimization not 7026 // taking into account the StopAt value. 7027 verifyFormat( 7028 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 7029 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 7030 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 7031 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7032 7033 verifyFormat("{\n {\n {\n" 7034 " Annotation.SpaceRequiredBefore =\n" 7035 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 7036 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 7037 " }\n }\n}"); 7038 7039 // Break on an outer level if there was a break on an inner level. 7040 EXPECT_EQ("f(g(h(a, // comment\n" 7041 " b, c),\n" 7042 " d, e),\n" 7043 " x, y);", 7044 format("f(g(h(a, // comment\n" 7045 " b, c), d, e), x, y);")); 7046 7047 // Prefer breaking similar line breaks. 7048 verifyFormat( 7049 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 7050 " NSTrackingMouseEnteredAndExited |\n" 7051 " NSTrackingActiveAlways;"); 7052 } 7053 7054 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 7055 FormatStyle NoBinPacking = getGoogleStyle(); 7056 NoBinPacking.BinPackParameters = false; 7057 NoBinPacking.BinPackArguments = true; 7058 verifyFormat("void f() {\n" 7059 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 7060 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 7061 "}", 7062 NoBinPacking); 7063 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 7064 " int aaaaaaaaaaaaaaaaaaaa,\n" 7065 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 7066 NoBinPacking); 7067 7068 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 7069 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7070 " vector<int> bbbbbbbbbbbbbbb);", 7071 NoBinPacking); 7072 // FIXME: This behavior difference is probably not wanted. However, currently 7073 // we cannot distinguish BreakBeforeParameter being set because of the wrapped 7074 // template arguments from BreakBeforeParameter being set because of the 7075 // one-per-line formatting. 7076 verifyFormat( 7077 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n" 7078 " aaaaaaaaaa> aaaaaaaaaa);", 7079 NoBinPacking); 7080 verifyFormat( 7081 "void fffffffffff(\n" 7082 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n" 7083 " aaaaaaaaaa);"); 7084 } 7085 7086 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 7087 FormatStyle NoBinPacking = getGoogleStyle(); 7088 NoBinPacking.BinPackParameters = false; 7089 NoBinPacking.BinPackArguments = false; 7090 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 7091 " aaaaaaaaaaaaaaaaaaaa,\n" 7092 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 7093 NoBinPacking); 7094 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 7095 " aaaaaaaaaaaaa,\n" 7096 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 7097 NoBinPacking); 7098 verifyFormat( 7099 "aaaaaaaa(aaaaaaaaaaaaa,\n" 7100 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7101 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 7102 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7103 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 7104 NoBinPacking); 7105 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 7106 " .aaaaaaaaaaaaaaaaaa();", 7107 NoBinPacking); 7108 verifyFormat("void f() {\n" 7109 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7110 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 7111 "}", 7112 NoBinPacking); 7113 7114 verifyFormat( 7115 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7116 " aaaaaaaaaaaa,\n" 7117 " aaaaaaaaaaaa);", 7118 NoBinPacking); 7119 verifyFormat( 7120 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 7121 " ddddddddddddddddddddddddddddd),\n" 7122 " test);", 7123 NoBinPacking); 7124 7125 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 7126 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 7127 " aaaaaaaaaaaaaaaaaaaaaaa>\n" 7128 " aaaaaaaaaaaaaaaaaa;", 7129 NoBinPacking); 7130 verifyFormat("a(\"a\"\n" 7131 " \"a\",\n" 7132 " a);"); 7133 7134 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 7135 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 7136 " aaaaaaaaa,\n" 7137 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7138 NoBinPacking); 7139 verifyFormat( 7140 "void f() {\n" 7141 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 7142 " .aaaaaaa();\n" 7143 "}", 7144 NoBinPacking); 7145 verifyFormat( 7146 "template <class SomeType, class SomeOtherType>\n" 7147 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 7148 NoBinPacking); 7149 } 7150 7151 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 7152 FormatStyle Style = getLLVMStyleWithColumns(15); 7153 Style.ExperimentalAutoDetectBinPacking = true; 7154 EXPECT_EQ("aaa(aaaa,\n" 7155 " aaaa,\n" 7156 " aaaa);\n" 7157 "aaa(aaaa,\n" 7158 " aaaa,\n" 7159 " aaaa);", 7160 format("aaa(aaaa,\n" // one-per-line 7161 " aaaa,\n" 7162 " aaaa );\n" 7163 "aaa(aaaa, aaaa, aaaa);", // inconclusive 7164 Style)); 7165 EXPECT_EQ("aaa(aaaa, aaaa,\n" 7166 " aaaa);\n" 7167 "aaa(aaaa, aaaa,\n" 7168 " aaaa);", 7169 format("aaa(aaaa, aaaa,\n" // bin-packed 7170 " aaaa );\n" 7171 "aaa(aaaa, aaaa, aaaa);", // inconclusive 7172 Style)); 7173 } 7174 7175 TEST_F(FormatTest, FormatsBuilderPattern) { 7176 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 7177 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 7178 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 7179 " .StartsWith(\".init\", ORDER_INIT)\n" 7180 " .StartsWith(\".fini\", ORDER_FINI)\n" 7181 " .StartsWith(\".hash\", ORDER_HASH)\n" 7182 " .Default(ORDER_TEXT);\n"); 7183 7184 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 7185 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 7186 verifyFormat("aaaaaaa->aaaaaaa\n" 7187 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7188 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7189 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 7190 verifyFormat( 7191 "aaaaaaa->aaaaaaa\n" 7192 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7193 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 7194 verifyFormat( 7195 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 7196 " aaaaaaaaaaaaaa);"); 7197 verifyFormat( 7198 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 7199 " aaaaaa->aaaaaaaaaaaa()\n" 7200 " ->aaaaaaaaaaaaaaaa(\n" 7201 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7202 " ->aaaaaaaaaaaaaaaaa();"); 7203 verifyGoogleFormat( 7204 "void f() {\n" 7205 " someo->Add((new util::filetools::Handler(dir))\n" 7206 " ->OnEvent1(NewPermanentCallback(\n" 7207 " this, &HandlerHolderClass::EventHandlerCBA))\n" 7208 " ->OnEvent2(NewPermanentCallback(\n" 7209 " this, &HandlerHolderClass::EventHandlerCBB))\n" 7210 " ->OnEvent3(NewPermanentCallback(\n" 7211 " this, &HandlerHolderClass::EventHandlerCBC))\n" 7212 " ->OnEvent5(NewPermanentCallback(\n" 7213 " this, &HandlerHolderClass::EventHandlerCBD))\n" 7214 " ->OnEvent6(NewPermanentCallback(\n" 7215 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 7216 "}"); 7217 7218 verifyFormat( 7219 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 7220 verifyFormat("aaaaaaaaaaaaaaa()\n" 7221 " .aaaaaaaaaaaaaaa()\n" 7222 " .aaaaaaaaaaaaaaa()\n" 7223 " .aaaaaaaaaaaaaaa()\n" 7224 " .aaaaaaaaaaaaaaa();"); 7225 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 7226 " .aaaaaaaaaaaaaaa()\n" 7227 " .aaaaaaaaaaaaaaa()\n" 7228 " .aaaaaaaaaaaaaaa();"); 7229 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 7230 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 7231 " .aaaaaaaaaaaaaaa();"); 7232 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 7233 " ->aaaaaaaaaaaaaae(0)\n" 7234 " ->aaaaaaaaaaaaaaa();"); 7235 7236 // Don't linewrap after very short segments. 7237 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7238 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7239 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7240 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7241 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7242 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7243 verifyFormat("aaa()\n" 7244 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7245 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7246 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7247 7248 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 7249 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7250 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 7251 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 7252 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 7253 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 7254 7255 // Prefer not to break after empty parentheses. 7256 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 7257 " First->LastNewlineOffset);"); 7258 7259 // Prefer not to create "hanging" indents. 7260 verifyFormat( 7261 "return !soooooooooooooome_map\n" 7262 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7263 " .second;"); 7264 verifyFormat( 7265 "return aaaaaaaaaaaaaaaa\n" 7266 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n" 7267 " .aaaa(aaaaaaaaaaaaaa);"); 7268 // No hanging indent here. 7269 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n" 7270 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7271 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n" 7272 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7273 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 7274 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7275 getLLVMStyleWithColumns(60)); 7276 verifyFormat("aaaaaaaaaaaaaaaaaa\n" 7277 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 7278 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7279 getLLVMStyleWithColumns(59)); 7280 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7281 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7282 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7283 7284 // Dont break if only closing statements before member call 7285 verifyFormat("test() {\n" 7286 " ([]() -> {\n" 7287 " int b = 32;\n" 7288 " return 3;\n" 7289 " }).foo();\n" 7290 "}"); 7291 verifyFormat("test() {\n" 7292 " (\n" 7293 " []() -> {\n" 7294 " int b = 32;\n" 7295 " return 3;\n" 7296 " },\n" 7297 " foo, bar)\n" 7298 " .foo();\n" 7299 "}"); 7300 verifyFormat("test() {\n" 7301 " ([]() -> {\n" 7302 " int b = 32;\n" 7303 " return 3;\n" 7304 " })\n" 7305 " .foo()\n" 7306 " .bar();\n" 7307 "}"); 7308 verifyFormat("test() {\n" 7309 " ([]() -> {\n" 7310 " int b = 32;\n" 7311 " return 3;\n" 7312 " })\n" 7313 " .foo(\"aaaaaaaaaaaaaaaaa\"\n" 7314 " \"bbbb\");\n" 7315 "}", 7316 getLLVMStyleWithColumns(30)); 7317 } 7318 7319 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 7320 verifyFormat( 7321 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7322 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 7323 verifyFormat( 7324 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 7325 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 7326 7327 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 7328 " ccccccccccccccccccccccccc) {\n}"); 7329 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 7330 " ccccccccccccccccccccccccc) {\n}"); 7331 7332 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 7333 " ccccccccccccccccccccccccc) {\n}"); 7334 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 7335 " ccccccccccccccccccccccccc) {\n}"); 7336 7337 verifyFormat( 7338 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 7339 " ccccccccccccccccccccccccc) {\n}"); 7340 verifyFormat( 7341 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 7342 " ccccccccccccccccccccccccc) {\n}"); 7343 7344 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 7345 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 7346 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 7347 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 7348 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 7349 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 7350 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 7351 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 7352 7353 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 7354 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 7355 " aaaaaaaaaaaaaaa != aa) {\n}"); 7356 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 7357 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 7358 " aaaaaaaaaaaaaaa != aa) {\n}"); 7359 } 7360 7361 TEST_F(FormatTest, BreaksAfterAssignments) { 7362 verifyFormat( 7363 "unsigned Cost =\n" 7364 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 7365 " SI->getPointerAddressSpaceee());\n"); 7366 verifyFormat( 7367 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 7368 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 7369 7370 verifyFormat( 7371 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 7372 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 7373 verifyFormat("unsigned OriginalStartColumn =\n" 7374 " SourceMgr.getSpellingColumnNumber(\n" 7375 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 7376 " 1;"); 7377 } 7378 7379 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) { 7380 FormatStyle Style = getLLVMStyle(); 7381 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 7382 " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;", 7383 Style); 7384 7385 Style.PenaltyBreakAssignment = 20; 7386 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 7387 " cccccccccccccccccccccccccc;", 7388 Style); 7389 } 7390 7391 TEST_F(FormatTest, AlignsAfterAssignments) { 7392 verifyFormat( 7393 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7394 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7395 verifyFormat( 7396 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7397 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7398 verifyFormat( 7399 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7400 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7401 verifyFormat( 7402 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7403 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 7404 verifyFormat( 7405 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 7406 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 7407 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 7408 } 7409 7410 TEST_F(FormatTest, AlignsAfterReturn) { 7411 verifyFormat( 7412 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7413 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7414 verifyFormat( 7415 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7416 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 7417 verifyFormat( 7418 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 7419 " aaaaaaaaaaaaaaaaaaaaaa();"); 7420 verifyFormat( 7421 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 7422 " aaaaaaaaaaaaaaaaaaaaaa());"); 7423 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7424 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7425 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7426 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 7427 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7428 verifyFormat("return\n" 7429 " // true if code is one of a or b.\n" 7430 " code == a || code == b;"); 7431 } 7432 7433 TEST_F(FormatTest, AlignsAfterOpenBracket) { 7434 verifyFormat( 7435 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 7436 " aaaaaaaaa aaaaaaa) {}"); 7437 verifyFormat( 7438 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 7439 " aaaaaaaaaaa aaaaaaaaa);"); 7440 verifyFormat( 7441 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 7442 " aaaaaaaaaaaaaaaaaaaaa));"); 7443 FormatStyle Style = getLLVMStyle(); 7444 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7445 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7446 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 7447 Style); 7448 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 7449 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 7450 Style); 7451 verifyFormat("SomeLongVariableName->someFunction(\n" 7452 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 7453 Style); 7454 verifyFormat( 7455 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 7456 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 7457 Style); 7458 verifyFormat( 7459 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 7460 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7461 Style); 7462 verifyFormat( 7463 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 7464 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 7465 Style); 7466 7467 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n" 7468 " ccccccc(aaaaaaaaaaaaaaaaa, //\n" 7469 " b));", 7470 Style); 7471 7472 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 7473 Style.BinPackArguments = false; 7474 Style.BinPackParameters = false; 7475 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7476 " aaaaaaaaaaa aaaaaaaa,\n" 7477 " aaaaaaaaa aaaaaaa,\n" 7478 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 7479 Style); 7480 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 7481 " aaaaaaaaaaa aaaaaaaaa,\n" 7482 " aaaaaaaaaaa aaaaaaaaa,\n" 7483 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7484 Style); 7485 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" 7486 " aaaaaaaaaaaaaaa,\n" 7487 " aaaaaaaaaaaaaaaaaaaaa,\n" 7488 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 7489 Style); 7490 verifyFormat( 7491 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" 7492 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 7493 Style); 7494 verifyFormat( 7495 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" 7496 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 7497 Style); 7498 verifyFormat( 7499 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 7500 " aaaaaaaaaaaaaaaaaaaaa(\n" 7501 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n" 7502 " aaaaaaaaaaaaaaaa);", 7503 Style); 7504 verifyFormat( 7505 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 7506 " aaaaaaaaaaaaaaaaaaaaa(\n" 7507 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n" 7508 " aaaaaaaaaaaaaaaa);", 7509 Style); 7510 } 7511 7512 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 7513 FormatStyle Style = getLLVMStyleWithColumns(40); 7514 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7515 " bbbbbbbbbbbbbbbbbbbbbb);", 7516 Style); 7517 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 7518 Style.AlignOperands = FormatStyle::OAS_DontAlign; 7519 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7520 " bbbbbbbbbbbbbbbbbbbbbb);", 7521 Style); 7522 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7523 Style.AlignOperands = FormatStyle::OAS_Align; 7524 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7525 " bbbbbbbbbbbbbbbbbbbbbb);", 7526 Style); 7527 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7528 Style.AlignOperands = FormatStyle::OAS_DontAlign; 7529 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7530 " bbbbbbbbbbbbbbbbbbbbbb);", 7531 Style); 7532 } 7533 7534 TEST_F(FormatTest, BreaksConditionalExpressions) { 7535 verifyFormat( 7536 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7537 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7538 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7539 verifyFormat( 7540 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 7541 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7542 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7543 verifyFormat( 7544 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7545 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7546 verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n" 7547 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7548 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7549 verifyFormat( 7550 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 7551 " : aaaaaaaaaaaaa);"); 7552 verifyFormat( 7553 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7554 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7555 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7556 " aaaaaaaaaaaaa);"); 7557 verifyFormat( 7558 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7559 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7560 " aaaaaaaaaaaaa);"); 7561 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7562 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7563 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7564 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7565 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7566 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7567 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7568 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7569 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7570 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7571 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7572 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7573 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7574 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7575 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7576 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7577 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7578 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7579 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7580 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7581 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 7582 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7583 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7584 " : aaaaaaaaaaaaaaaa;"); 7585 verifyFormat( 7586 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7587 " ? aaaaaaaaaaaaaaa\n" 7588 " : aaaaaaaaaaaaaaa;"); 7589 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 7590 " aaaaaaaaa\n" 7591 " ? b\n" 7592 " : c);"); 7593 verifyFormat("return aaaa == bbbb\n" 7594 " // comment\n" 7595 " ? aaaa\n" 7596 " : bbbb;"); 7597 verifyFormat("unsigned Indent =\n" 7598 " format(TheLine.First,\n" 7599 " IndentForLevel[TheLine.Level] >= 0\n" 7600 " ? IndentForLevel[TheLine.Level]\n" 7601 " : TheLine * 2,\n" 7602 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 7603 getLLVMStyleWithColumns(60)); 7604 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 7605 " ? aaaaaaaaaaaaaaa\n" 7606 " : bbbbbbbbbbbbbbb //\n" 7607 " ? ccccccccccccccc\n" 7608 " : ddddddddddddddd;"); 7609 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 7610 " ? aaaaaaaaaaaaaaa\n" 7611 " : (bbbbbbbbbbbbbbb //\n" 7612 " ? ccccccccccccccc\n" 7613 " : ddddddddddddddd);"); 7614 verifyFormat( 7615 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7616 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7617 " aaaaaaaaaaaaaaaaaaaaa +\n" 7618 " aaaaaaaaaaaaaaaaaaaaa\n" 7619 " : aaaaaaaaaa;"); 7620 verifyFormat( 7621 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7622 " : aaaaaaaaaaaaaaaaaaaaaa\n" 7623 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7624 7625 FormatStyle NoBinPacking = getLLVMStyle(); 7626 NoBinPacking.BinPackArguments = false; 7627 verifyFormat( 7628 "void f() {\n" 7629 " g(aaa,\n" 7630 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 7631 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7632 " ? aaaaaaaaaaaaaaa\n" 7633 " : aaaaaaaaaaaaaaa);\n" 7634 "}", 7635 NoBinPacking); 7636 verifyFormat( 7637 "void f() {\n" 7638 " g(aaa,\n" 7639 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 7640 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7641 " ?: aaaaaaaaaaaaaaa);\n" 7642 "}", 7643 NoBinPacking); 7644 7645 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 7646 " // comment.\n" 7647 " ccccccccccccccccccccccccccccccccccccccc\n" 7648 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7649 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 7650 7651 // Assignments in conditional expressions. Apparently not uncommon :-(. 7652 verifyFormat("return a != b\n" 7653 " // comment\n" 7654 " ? a = b\n" 7655 " : a = b;"); 7656 verifyFormat("return a != b\n" 7657 " // comment\n" 7658 " ? a = a != b\n" 7659 " // comment\n" 7660 " ? a = b\n" 7661 " : a\n" 7662 " : a;\n"); 7663 verifyFormat("return a != b\n" 7664 " // comment\n" 7665 " ? a\n" 7666 " : a = a != b\n" 7667 " // comment\n" 7668 " ? a = b\n" 7669 " : a;"); 7670 7671 // Chained conditionals 7672 FormatStyle Style = getLLVMStyle(); 7673 Style.ColumnLimit = 70; 7674 Style.AlignOperands = FormatStyle::OAS_Align; 7675 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7676 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7677 " : 3333333333333333;", 7678 Style); 7679 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7680 " : bbbbbbbbbb ? 2222222222222222\n" 7681 " : 3333333333333333;", 7682 Style); 7683 verifyFormat("return aaaaaaaaaa ? 1111111111111111\n" 7684 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 7685 " : 3333333333333333;", 7686 Style); 7687 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7688 " : bbbbbbbbbbbbbb ? 222222\n" 7689 " : 333333;", 7690 Style); 7691 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7692 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7693 " : cccccccccccccc ? 3333333333333333\n" 7694 " : 4444444444444444;", 7695 Style); 7696 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc)\n" 7697 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7698 " : 3333333333333333;", 7699 Style); 7700 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7701 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7702 " : (aaa ? bbb : ccc);", 7703 Style); 7704 verifyFormat( 7705 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7706 " : cccccccccccccccccc)\n" 7707 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7708 " : 3333333333333333;", 7709 Style); 7710 verifyFormat( 7711 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7712 " : cccccccccccccccccc)\n" 7713 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7714 " : 3333333333333333;", 7715 Style); 7716 verifyFormat( 7717 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7718 " : dddddddddddddddddd)\n" 7719 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7720 " : 3333333333333333;", 7721 Style); 7722 verifyFormat( 7723 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7724 " : dddddddddddddddddd)\n" 7725 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7726 " : 3333333333333333;", 7727 Style); 7728 verifyFormat( 7729 "return aaaaaaaaa ? 1111111111111111\n" 7730 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7731 " : a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7732 " : dddddddddddddddddd)\n", 7733 Style); 7734 verifyFormat( 7735 "return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7736 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7737 " : (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7738 " : cccccccccccccccccc);", 7739 Style); 7740 verifyFormat( 7741 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7742 " : ccccccccccccccc ? dddddddddddddddddd\n" 7743 " : eeeeeeeeeeeeeeeeee)\n" 7744 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7745 " : 3333333333333333;", 7746 Style); 7747 verifyFormat( 7748 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7749 " : ccccccccccccccc ? dddddddddddddddddd\n" 7750 " : eeeeeeeeeeeeeeeeee)\n" 7751 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7752 " : 3333333333333333;", 7753 Style); 7754 verifyFormat( 7755 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7756 " : cccccccccccc ? dddddddddddddddddd\n" 7757 " : eeeeeeeeeeeeeeeeee)\n" 7758 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7759 " : 3333333333333333;", 7760 Style); 7761 verifyFormat( 7762 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7763 " : cccccccccccccccccc\n" 7764 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7765 " : 3333333333333333;", 7766 Style); 7767 verifyFormat( 7768 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7769 " : cccccccccccccccc ? dddddddddddddddddd\n" 7770 " : eeeeeeeeeeeeeeeeee\n" 7771 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7772 " : 3333333333333333;", 7773 Style); 7774 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa\n" 7775 " ? (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7776 " : cccccccccccccccccc ? dddddddddddddddddd\n" 7777 " : eeeeeeeeeeeeeeeeee)\n" 7778 " : bbbbbbbbbbbbbbbbbbb ? 2222222222222222\n" 7779 " : 3333333333333333;", 7780 Style); 7781 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaa\n" 7782 " ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7783 " : cccccccccccccccc ? dddddddddddddddddd\n" 7784 " : eeeeeeeeeeeeeeeeee\n" 7785 " : bbbbbbbbbbbbbbbbbbbbbbb ? 2222222222222222\n" 7786 " : 3333333333333333;", 7787 Style); 7788 7789 Style.AlignOperands = FormatStyle::OAS_DontAlign; 7790 Style.BreakBeforeTernaryOperators = false; 7791 // FIXME: Aligning the question marks is weird given DontAlign. 7792 // Consider disabling this alignment in this case. Also check whether this 7793 // will render the adjustment from https://reviews.llvm.org/D82199 7794 // unnecessary. 7795 verifyFormat("int x = aaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa :\n" 7796 " bbbb ? cccccccccccccccccc :\n" 7797 " ddddd;\n", 7798 Style); 7799 7800 EXPECT_EQ( 7801 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n" 7802 " /*\n" 7803 " */\n" 7804 " function() {\n" 7805 " try {\n" 7806 " return JJJJJJJJJJJJJJ(\n" 7807 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n" 7808 " }\n" 7809 " } :\n" 7810 " function() {};", 7811 format( 7812 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n" 7813 " /*\n" 7814 " */\n" 7815 " function() {\n" 7816 " try {\n" 7817 " return JJJJJJJJJJJJJJ(\n" 7818 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n" 7819 " }\n" 7820 " } :\n" 7821 " function() {};", 7822 getGoogleStyle(FormatStyle::LK_JavaScript))); 7823 } 7824 7825 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 7826 FormatStyle Style = getLLVMStyle(); 7827 Style.BreakBeforeTernaryOperators = false; 7828 Style.ColumnLimit = 70; 7829 verifyFormat( 7830 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7831 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7832 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7833 Style); 7834 verifyFormat( 7835 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 7836 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7837 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7838 Style); 7839 verifyFormat( 7840 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7841 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7842 Style); 7843 verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n" 7844 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7845 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7846 Style); 7847 verifyFormat( 7848 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 7849 " aaaaaaaaaaaaa);", 7850 Style); 7851 verifyFormat( 7852 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7853 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7854 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7855 " aaaaaaaaaaaaa);", 7856 Style); 7857 verifyFormat( 7858 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7859 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7860 " aaaaaaaaaaaaa);", 7861 Style); 7862 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7863 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7864 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 7865 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7866 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7867 Style); 7868 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7869 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7870 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7871 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 7872 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7873 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7874 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7875 Style); 7876 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7877 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 7878 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7879 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7880 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7881 Style); 7882 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7883 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7884 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 7885 Style); 7886 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 7887 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7888 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7889 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 7890 Style); 7891 verifyFormat( 7892 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7893 " aaaaaaaaaaaaaaa :\n" 7894 " aaaaaaaaaaaaaaa;", 7895 Style); 7896 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 7897 " aaaaaaaaa ?\n" 7898 " b :\n" 7899 " c);", 7900 Style); 7901 verifyFormat("unsigned Indent =\n" 7902 " format(TheLine.First,\n" 7903 " IndentForLevel[TheLine.Level] >= 0 ?\n" 7904 " IndentForLevel[TheLine.Level] :\n" 7905 " TheLine * 2,\n" 7906 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 7907 Style); 7908 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 7909 " aaaaaaaaaaaaaaa :\n" 7910 " bbbbbbbbbbbbbbb ? //\n" 7911 " ccccccccccccccc :\n" 7912 " ddddddddddddddd;", 7913 Style); 7914 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 7915 " aaaaaaaaaaaaaaa :\n" 7916 " (bbbbbbbbbbbbbbb ? //\n" 7917 " ccccccccccccccc :\n" 7918 " ddddddddddddddd);", 7919 Style); 7920 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7921 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n" 7922 " ccccccccccccccccccccccccccc;", 7923 Style); 7924 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7925 " aaaaa :\n" 7926 " bbbbbbbbbbbbbbb + cccccccccccccccc;", 7927 Style); 7928 7929 // Chained conditionals 7930 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7931 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7932 " 3333333333333333;", 7933 Style); 7934 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7935 " bbbbbbbbbb ? 2222222222222222 :\n" 7936 " 3333333333333333;", 7937 Style); 7938 verifyFormat("return aaaaaaaaaa ? 1111111111111111 :\n" 7939 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7940 " 3333333333333333;", 7941 Style); 7942 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7943 " bbbbbbbbbbbbbbbb ? 222222 :\n" 7944 " 333333;", 7945 Style); 7946 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7947 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7948 " cccccccccccccccc ? 3333333333333333 :\n" 7949 " 4444444444444444;", 7950 Style); 7951 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc) :\n" 7952 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7953 " 3333333333333333;", 7954 Style); 7955 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7956 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7957 " (aaa ? bbb : ccc);", 7958 Style); 7959 verifyFormat( 7960 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7961 " cccccccccccccccccc) :\n" 7962 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7963 " 3333333333333333;", 7964 Style); 7965 verifyFormat( 7966 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7967 " cccccccccccccccccc) :\n" 7968 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7969 " 3333333333333333;", 7970 Style); 7971 verifyFormat( 7972 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7973 " dddddddddddddddddd) :\n" 7974 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7975 " 3333333333333333;", 7976 Style); 7977 verifyFormat( 7978 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7979 " dddddddddddddddddd) :\n" 7980 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7981 " 3333333333333333;", 7982 Style); 7983 verifyFormat( 7984 "return aaaaaaaaa ? 1111111111111111 :\n" 7985 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7986 " a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7987 " dddddddddddddddddd)\n", 7988 Style); 7989 verifyFormat( 7990 "return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7991 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7992 " (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7993 " cccccccccccccccccc);", 7994 Style); 7995 verifyFormat( 7996 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7997 " ccccccccccccccccc ? dddddddddddddddddd :\n" 7998 " eeeeeeeeeeeeeeeeee) :\n" 7999 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8000 " 3333333333333333;", 8001 Style); 8002 verifyFormat( 8003 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8004 " ccccccccccccc ? dddddddddddddddddd :\n" 8005 " eeeeeeeeeeeeeeeeee) :\n" 8006 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8007 " 3333333333333333;", 8008 Style); 8009 verifyFormat( 8010 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8011 " ccccccccccccccccc ? dddddddddddddddddd :\n" 8012 " eeeeeeeeeeeeeeeeee) :\n" 8013 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8014 " 3333333333333333;", 8015 Style); 8016 verifyFormat( 8017 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8018 " cccccccccccccccccc :\n" 8019 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8020 " 3333333333333333;", 8021 Style); 8022 verifyFormat( 8023 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8024 " cccccccccccccccccc ? dddddddddddddddddd :\n" 8025 " eeeeeeeeeeeeeeeeee :\n" 8026 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8027 " 3333333333333333;", 8028 Style); 8029 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n" 8030 " (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8031 " cccccccccccccccccc ? dddddddddddddddddd :\n" 8032 " eeeeeeeeeeeeeeeeee) :\n" 8033 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8034 " 3333333333333333;", 8035 Style); 8036 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n" 8037 " aaaaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8038 " cccccccccccccccccccc ? dddddddddddddddddd :\n" 8039 " eeeeeeeeeeeeeeeeee :\n" 8040 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8041 " 3333333333333333;", 8042 Style); 8043 } 8044 8045 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 8046 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 8047 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 8048 verifyFormat("bool a = true, b = false;"); 8049 8050 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8051 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 8052 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 8053 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 8054 verifyFormat( 8055 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 8056 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 8057 " d = e && f;"); 8058 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 8059 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 8060 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 8061 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 8062 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 8063 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 8064 8065 FormatStyle Style = getGoogleStyle(); 8066 Style.PointerAlignment = FormatStyle::PAS_Left; 8067 Style.DerivePointerAlignment = false; 8068 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8069 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 8070 " *b = bbbbbbbbbbbbbbbbbbb;", 8071 Style); 8072 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 8073 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 8074 Style); 8075 verifyFormat("vector<int*> a, b;", Style); 8076 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style); 8077 } 8078 8079 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 8080 verifyFormat("arr[foo ? bar : baz];"); 8081 verifyFormat("f()[foo ? bar : baz];"); 8082 verifyFormat("(a + b)[foo ? bar : baz];"); 8083 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 8084 } 8085 8086 TEST_F(FormatTest, AlignsStringLiterals) { 8087 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 8088 " \"short literal\");"); 8089 verifyFormat( 8090 "looooooooooooooooooooooooongFunction(\n" 8091 " \"short literal\"\n" 8092 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 8093 verifyFormat("someFunction(\"Always break between multi-line\"\n" 8094 " \" string literals\",\n" 8095 " and, other, parameters);"); 8096 EXPECT_EQ("fun + \"1243\" /* comment */\n" 8097 " \"5678\";", 8098 format("fun + \"1243\" /* comment */\n" 8099 " \"5678\";", 8100 getLLVMStyleWithColumns(28))); 8101 EXPECT_EQ( 8102 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 8103 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 8104 " \"aaaaaaaaaaaaaaaa\";", 8105 format("aaaaaa =" 8106 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 8107 "aaaaaaaaaaaaaaaaaaaaa\" " 8108 "\"aaaaaaaaaaaaaaaa\";")); 8109 verifyFormat("a = a + \"a\"\n" 8110 " \"a\"\n" 8111 " \"a\";"); 8112 verifyFormat("f(\"a\", \"b\"\n" 8113 " \"c\");"); 8114 8115 verifyFormat( 8116 "#define LL_FORMAT \"ll\"\n" 8117 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 8118 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 8119 8120 verifyFormat("#define A(X) \\\n" 8121 " \"aaaaa\" #X \"bbbbbb\" \\\n" 8122 " \"ccccc\"", 8123 getLLVMStyleWithColumns(23)); 8124 verifyFormat("#define A \"def\"\n" 8125 "f(\"abc\" A \"ghi\"\n" 8126 " \"jkl\");"); 8127 8128 verifyFormat("f(L\"a\"\n" 8129 " L\"b\");"); 8130 verifyFormat("#define A(X) \\\n" 8131 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 8132 " L\"ccccc\"", 8133 getLLVMStyleWithColumns(25)); 8134 8135 verifyFormat("f(@\"a\"\n" 8136 " @\"b\");"); 8137 verifyFormat("NSString s = @\"a\"\n" 8138 " @\"b\"\n" 8139 " @\"c\";"); 8140 verifyFormat("NSString s = @\"a\"\n" 8141 " \"b\"\n" 8142 " \"c\";"); 8143 } 8144 8145 TEST_F(FormatTest, ReturnTypeBreakingStyle) { 8146 FormatStyle Style = getLLVMStyle(); 8147 // No declarations or definitions should be moved to own line. 8148 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; 8149 verifyFormat("class A {\n" 8150 " int f() { return 1; }\n" 8151 " int g();\n" 8152 "};\n" 8153 "int f() { return 1; }\n" 8154 "int g();\n", 8155 Style); 8156 8157 // All declarations and definitions should have the return type moved to its 8158 // own line. 8159 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 8160 Style.TypenameMacros = {"LIST"}; 8161 verifyFormat("SomeType\n" 8162 "funcdecl(LIST(uint64_t));", 8163 Style); 8164 verifyFormat("class E {\n" 8165 " int\n" 8166 " f() {\n" 8167 " return 1;\n" 8168 " }\n" 8169 " int\n" 8170 " g();\n" 8171 "};\n" 8172 "int\n" 8173 "f() {\n" 8174 " return 1;\n" 8175 "}\n" 8176 "int\n" 8177 "g();\n", 8178 Style); 8179 8180 // Top-level definitions, and no kinds of declarations should have the 8181 // return type moved to its own line. 8182 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; 8183 verifyFormat("class B {\n" 8184 " int f() { return 1; }\n" 8185 " int g();\n" 8186 "};\n" 8187 "int\n" 8188 "f() {\n" 8189 " return 1;\n" 8190 "}\n" 8191 "int g();\n", 8192 Style); 8193 8194 // Top-level definitions and declarations should have the return type moved 8195 // to its own line. 8196 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel; 8197 verifyFormat("class C {\n" 8198 " int f() { return 1; }\n" 8199 " int g();\n" 8200 "};\n" 8201 "int\n" 8202 "f() {\n" 8203 " return 1;\n" 8204 "}\n" 8205 "int\n" 8206 "g();\n", 8207 Style); 8208 8209 // All definitions should have the return type moved to its own line, but no 8210 // kinds of declarations. 8211 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 8212 verifyFormat("class D {\n" 8213 " int\n" 8214 " f() {\n" 8215 " return 1;\n" 8216 " }\n" 8217 " int g();\n" 8218 "};\n" 8219 "int\n" 8220 "f() {\n" 8221 " return 1;\n" 8222 "}\n" 8223 "int g();\n", 8224 Style); 8225 verifyFormat("const char *\n" 8226 "f(void) {\n" // Break here. 8227 " return \"\";\n" 8228 "}\n" 8229 "const char *bar(void);\n", // No break here. 8230 Style); 8231 verifyFormat("template <class T>\n" 8232 "T *\n" 8233 "f(T &c) {\n" // Break here. 8234 " return NULL;\n" 8235 "}\n" 8236 "template <class T> T *f(T &c);\n", // No break here. 8237 Style); 8238 verifyFormat("class C {\n" 8239 " int\n" 8240 " operator+() {\n" 8241 " return 1;\n" 8242 " }\n" 8243 " int\n" 8244 " operator()() {\n" 8245 " return 1;\n" 8246 " }\n" 8247 "};\n", 8248 Style); 8249 verifyFormat("void\n" 8250 "A::operator()() {}\n" 8251 "void\n" 8252 "A::operator>>() {}\n" 8253 "void\n" 8254 "A::operator+() {}\n" 8255 "void\n" 8256 "A::operator*() {}\n" 8257 "void\n" 8258 "A::operator->() {}\n" 8259 "void\n" 8260 "A::operator void *() {}\n" 8261 "void\n" 8262 "A::operator void &() {}\n" 8263 "void\n" 8264 "A::operator void &&() {}\n" 8265 "void\n" 8266 "A::operator char *() {}\n" 8267 "void\n" 8268 "A::operator[]() {}\n" 8269 "void\n" 8270 "A::operator!() {}\n" 8271 "void\n" 8272 "A::operator**() {}\n" 8273 "void\n" 8274 "A::operator<Foo> *() {}\n" 8275 "void\n" 8276 "A::operator<Foo> **() {}\n" 8277 "void\n" 8278 "A::operator<Foo> &() {}\n" 8279 "void\n" 8280 "A::operator void **() {}\n", 8281 Style); 8282 verifyFormat("constexpr auto\n" 8283 "operator()() const -> reference {}\n" 8284 "constexpr auto\n" 8285 "operator>>() const -> reference {}\n" 8286 "constexpr auto\n" 8287 "operator+() const -> reference {}\n" 8288 "constexpr auto\n" 8289 "operator*() const -> reference {}\n" 8290 "constexpr auto\n" 8291 "operator->() const -> reference {}\n" 8292 "constexpr auto\n" 8293 "operator++() const -> reference {}\n" 8294 "constexpr auto\n" 8295 "operator void *() const -> reference {}\n" 8296 "constexpr auto\n" 8297 "operator void **() const -> reference {}\n" 8298 "constexpr auto\n" 8299 "operator void *() const -> reference {}\n" 8300 "constexpr auto\n" 8301 "operator void &() const -> reference {}\n" 8302 "constexpr auto\n" 8303 "operator void &&() const -> reference {}\n" 8304 "constexpr auto\n" 8305 "operator char *() const -> reference {}\n" 8306 "constexpr auto\n" 8307 "operator!() const -> reference {}\n" 8308 "constexpr auto\n" 8309 "operator[]() const -> reference {}\n", 8310 Style); 8311 verifyFormat("void *operator new(std::size_t s);", // No break here. 8312 Style); 8313 verifyFormat("void *\n" 8314 "operator new(std::size_t s) {}", 8315 Style); 8316 verifyFormat("void *\n" 8317 "operator delete[](void *ptr) {}", 8318 Style); 8319 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 8320 verifyFormat("const char *\n" 8321 "f(void)\n" // Break here. 8322 "{\n" 8323 " return \"\";\n" 8324 "}\n" 8325 "const char *bar(void);\n", // No break here. 8326 Style); 8327 verifyFormat("template <class T>\n" 8328 "T *\n" // Problem here: no line break 8329 "f(T &c)\n" // Break here. 8330 "{\n" 8331 " return NULL;\n" 8332 "}\n" 8333 "template <class T> T *f(T &c);\n", // No break here. 8334 Style); 8335 verifyFormat("int\n" 8336 "foo(A<bool> a)\n" 8337 "{\n" 8338 " return a;\n" 8339 "}\n", 8340 Style); 8341 verifyFormat("int\n" 8342 "foo(A<8> a)\n" 8343 "{\n" 8344 " return a;\n" 8345 "}\n", 8346 Style); 8347 verifyFormat("int\n" 8348 "foo(A<B<bool>, 8> a)\n" 8349 "{\n" 8350 " return a;\n" 8351 "}\n", 8352 Style); 8353 verifyFormat("int\n" 8354 "foo(A<B<8>, bool> a)\n" 8355 "{\n" 8356 " return a;\n" 8357 "}\n", 8358 Style); 8359 verifyFormat("int\n" 8360 "foo(A<B<bool>, bool> a)\n" 8361 "{\n" 8362 " return a;\n" 8363 "}\n", 8364 Style); 8365 verifyFormat("int\n" 8366 "foo(A<B<8>, 8> a)\n" 8367 "{\n" 8368 " return a;\n" 8369 "}\n", 8370 Style); 8371 8372 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 8373 Style.BraceWrapping.AfterFunction = true; 8374 verifyFormat("int f(i);\n" // No break here. 8375 "int\n" // Break here. 8376 "f(i)\n" 8377 "{\n" 8378 " return i + 1;\n" 8379 "}\n" 8380 "int\n" // Break here. 8381 "f(i)\n" 8382 "{\n" 8383 " return i + 1;\n" 8384 "};", 8385 Style); 8386 verifyFormat("int f(a, b, c);\n" // No break here. 8387 "int\n" // Break here. 8388 "f(a, b, c)\n" // Break here. 8389 "short a, b;\n" 8390 "float c;\n" 8391 "{\n" 8392 " return a + b < c;\n" 8393 "}\n" 8394 "int\n" // Break here. 8395 "f(a, b, c)\n" // Break here. 8396 "short a, b;\n" 8397 "float c;\n" 8398 "{\n" 8399 " return a + b < c;\n" 8400 "};", 8401 Style); 8402 verifyFormat("byte *\n" // Break here. 8403 "f(a)\n" // Break here. 8404 "byte a[];\n" 8405 "{\n" 8406 " return a;\n" 8407 "}", 8408 Style); 8409 verifyFormat("bool f(int a, int) override;\n" 8410 "Bar g(int a, Bar) final;\n" 8411 "Bar h(a, Bar) final;", 8412 Style); 8413 verifyFormat("int\n" 8414 "f(a)", 8415 Style); 8416 verifyFormat("bool\n" 8417 "f(size_t = 0, bool b = false)\n" 8418 "{\n" 8419 " return !b;\n" 8420 "}", 8421 Style); 8422 8423 // The return breaking style doesn't affect: 8424 // * function and object definitions with attribute-like macros 8425 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n" 8426 " ABSL_GUARDED_BY(mutex) = {};", 8427 getGoogleStyleWithColumns(40)); 8428 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n" 8429 " ABSL_GUARDED_BY(mutex); // comment", 8430 getGoogleStyleWithColumns(40)); 8431 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n" 8432 " ABSL_GUARDED_BY(mutex1)\n" 8433 " ABSL_GUARDED_BY(mutex2);", 8434 getGoogleStyleWithColumns(40)); 8435 verifyFormat("Tttttt f(int a, int b)\n" 8436 " ABSL_GUARDED_BY(mutex1)\n" 8437 " ABSL_GUARDED_BY(mutex2);", 8438 getGoogleStyleWithColumns(40)); 8439 // * typedefs 8440 verifyFormat("typedef ATTR(X) char x;", getGoogleStyle()); 8441 8442 Style = getGNUStyle(); 8443 8444 // Test for comments at the end of function declarations. 8445 verifyFormat("void\n" 8446 "foo (int a, /*abc*/ int b) // def\n" 8447 "{\n" 8448 "}\n", 8449 Style); 8450 8451 verifyFormat("void\n" 8452 "foo (int a, /* abc */ int b) /* def */\n" 8453 "{\n" 8454 "}\n", 8455 Style); 8456 8457 // Definitions that should not break after return type 8458 verifyFormat("void foo (int a, int b); // def\n", Style); 8459 verifyFormat("void foo (int a, int b); /* def */\n", Style); 8460 verifyFormat("void foo (int a, int b);\n", Style); 8461 } 8462 8463 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 8464 FormatStyle NoBreak = getLLVMStyle(); 8465 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 8466 FormatStyle Break = getLLVMStyle(); 8467 Break.AlwaysBreakBeforeMultilineStrings = true; 8468 verifyFormat("aaaa = \"bbbb\"\n" 8469 " \"cccc\";", 8470 NoBreak); 8471 verifyFormat("aaaa =\n" 8472 " \"bbbb\"\n" 8473 " \"cccc\";", 8474 Break); 8475 verifyFormat("aaaa(\"bbbb\"\n" 8476 " \"cccc\");", 8477 NoBreak); 8478 verifyFormat("aaaa(\n" 8479 " \"bbbb\"\n" 8480 " \"cccc\");", 8481 Break); 8482 verifyFormat("aaaa(qqq, \"bbbb\"\n" 8483 " \"cccc\");", 8484 NoBreak); 8485 verifyFormat("aaaa(qqq,\n" 8486 " \"bbbb\"\n" 8487 " \"cccc\");", 8488 Break); 8489 verifyFormat("aaaa(qqq,\n" 8490 " L\"bbbb\"\n" 8491 " L\"cccc\");", 8492 Break); 8493 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 8494 " \"bbbb\"));", 8495 Break); 8496 verifyFormat("string s = someFunction(\n" 8497 " \"abc\"\n" 8498 " \"abc\");", 8499 Break); 8500 8501 // As we break before unary operators, breaking right after them is bad. 8502 verifyFormat("string foo = abc ? \"x\"\n" 8503 " \"blah blah blah blah blah blah\"\n" 8504 " : \"y\";", 8505 Break); 8506 8507 // Don't break if there is no column gain. 8508 verifyFormat("f(\"aaaa\"\n" 8509 " \"bbbb\");", 8510 Break); 8511 8512 // Treat literals with escaped newlines like multi-line string literals. 8513 EXPECT_EQ("x = \"a\\\n" 8514 "b\\\n" 8515 "c\";", 8516 format("x = \"a\\\n" 8517 "b\\\n" 8518 "c\";", 8519 NoBreak)); 8520 EXPECT_EQ("xxxx =\n" 8521 " \"a\\\n" 8522 "b\\\n" 8523 "c\";", 8524 format("xxxx = \"a\\\n" 8525 "b\\\n" 8526 "c\";", 8527 Break)); 8528 8529 EXPECT_EQ("NSString *const kString =\n" 8530 " @\"aaaa\"\n" 8531 " @\"bbbb\";", 8532 format("NSString *const kString = @\"aaaa\"\n" 8533 "@\"bbbb\";", 8534 Break)); 8535 8536 Break.ColumnLimit = 0; 8537 verifyFormat("const char *hello = \"hello llvm\";", Break); 8538 } 8539 8540 TEST_F(FormatTest, AlignsPipes) { 8541 verifyFormat( 8542 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8543 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8544 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8545 verifyFormat( 8546 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 8547 " << aaaaaaaaaaaaaaaaaaaa;"); 8548 verifyFormat( 8549 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8550 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8551 verifyFormat( 8552 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 8553 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8554 verifyFormat( 8555 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 8556 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 8557 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 8558 verifyFormat( 8559 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8560 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8561 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8562 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8563 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8564 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8565 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 8566 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 8567 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 8568 verifyFormat( 8569 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8570 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8571 verifyFormat( 8572 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n" 8573 " aaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8574 8575 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 8576 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 8577 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8578 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8579 " aaaaaaaaaaaaaaaaaaaaa)\n" 8580 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8581 verifyFormat("LOG_IF(aaa == //\n" 8582 " bbb)\n" 8583 " << a << b;"); 8584 8585 // But sometimes, breaking before the first "<<" is desirable. 8586 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 8587 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 8588 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 8589 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8590 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8591 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 8592 " << BEF << IsTemplate << Description << E->getType();"); 8593 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 8594 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8595 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8596 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 8597 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8598 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8599 " << aaa;"); 8600 8601 verifyFormat( 8602 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8603 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8604 8605 // Incomplete string literal. 8606 EXPECT_EQ("llvm::errs() << \"\n" 8607 " << a;", 8608 format("llvm::errs() << \"\n<<a;")); 8609 8610 verifyFormat("void f() {\n" 8611 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 8612 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 8613 "}"); 8614 8615 // Handle 'endl'. 8616 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 8617 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 8618 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 8619 8620 // Handle '\n'. 8621 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 8622 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 8623 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 8624 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 8625 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 8626 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 8627 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 8628 } 8629 8630 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) { 8631 verifyFormat("return out << \"somepacket = {\\n\"\n" 8632 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 8633 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 8634 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 8635 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 8636 " << \"}\";"); 8637 8638 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 8639 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 8640 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 8641 verifyFormat( 8642 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 8643 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 8644 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 8645 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 8646 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 8647 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 8648 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 8649 verifyFormat( 8650 "void f() {\n" 8651 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 8652 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 8653 "}"); 8654 8655 // Breaking before the first "<<" is generally not desirable. 8656 verifyFormat( 8657 "llvm::errs()\n" 8658 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8659 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8660 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8661 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 8662 getLLVMStyleWithColumns(70)); 8663 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 8664 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8665 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 8666 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8667 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 8668 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 8669 getLLVMStyleWithColumns(70)); 8670 8671 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 8672 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 8673 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;"); 8674 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 8675 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 8676 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);"); 8677 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n" 8678 " (aaaa + aaaa);", 8679 getLLVMStyleWithColumns(40)); 8680 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n" 8681 " (aaaaaaa + aaaaa));", 8682 getLLVMStyleWithColumns(40)); 8683 verifyFormat( 8684 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n" 8685 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n" 8686 " bbbbbbbbbbbbbbbbbbbbbbb);"); 8687 } 8688 8689 TEST_F(FormatTest, UnderstandsEquals) { 8690 verifyFormat( 8691 "aaaaaaaaaaaaaaaaa =\n" 8692 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8693 verifyFormat( 8694 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8695 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 8696 verifyFormat( 8697 "if (a) {\n" 8698 " f();\n" 8699 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8700 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 8701 "}"); 8702 8703 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8704 " 100000000 + 10000000) {\n}"); 8705 } 8706 8707 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 8708 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 8709 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 8710 8711 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 8712 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 8713 8714 verifyFormat( 8715 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 8716 " Parameter2);"); 8717 8718 verifyFormat( 8719 "ShortObject->shortFunction(\n" 8720 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 8721 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 8722 8723 verifyFormat("loooooooooooooongFunction(\n" 8724 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 8725 8726 verifyFormat( 8727 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 8728 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 8729 8730 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 8731 " .WillRepeatedly(Return(SomeValue));"); 8732 verifyFormat("void f() {\n" 8733 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 8734 " .Times(2)\n" 8735 " .WillRepeatedly(Return(SomeValue));\n" 8736 "}"); 8737 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 8738 " ccccccccccccccccccccccc);"); 8739 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8740 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8741 " .aaaaa(aaaaa),\n" 8742 " aaaaaaaaaaaaaaaaaaaaa);"); 8743 verifyFormat("void f() {\n" 8744 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8745 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 8746 "}"); 8747 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8748 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8749 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8750 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8751 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 8752 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8753 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8754 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8755 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 8756 "}"); 8757 8758 // Here, it is not necessary to wrap at "." or "->". 8759 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 8760 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 8761 verifyFormat( 8762 "aaaaaaaaaaa->aaaaaaaaa(\n" 8763 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8764 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 8765 8766 verifyFormat( 8767 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8768 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 8769 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 8770 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 8771 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 8772 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 8773 8774 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8775 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8776 " .a();"); 8777 8778 FormatStyle NoBinPacking = getLLVMStyle(); 8779 NoBinPacking.BinPackParameters = false; 8780 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 8781 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 8782 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 8783 " aaaaaaaaaaaaaaaaaaa,\n" 8784 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8785 NoBinPacking); 8786 8787 // If there is a subsequent call, change to hanging indentation. 8788 verifyFormat( 8789 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8790 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 8791 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8792 verifyFormat( 8793 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8794 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 8795 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8796 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8797 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8798 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8799 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8800 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 8801 } 8802 8803 TEST_F(FormatTest, WrapsTemplateDeclarations) { 8804 verifyFormat("template <typename T>\n" 8805 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 8806 verifyFormat("template <typename T>\n" 8807 "// T should be one of {A, B}.\n" 8808 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 8809 verifyFormat( 8810 "template <typename T>\n" 8811 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 8812 verifyFormat("template <typename T>\n" 8813 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 8814 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 8815 verifyFormat( 8816 "template <typename T>\n" 8817 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 8818 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 8819 verifyFormat( 8820 "template <typename T>\n" 8821 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 8822 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 8823 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8824 verifyFormat("template <typename T>\n" 8825 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8826 " int aaaaaaaaaaaaaaaaaaaaaa);"); 8827 verifyFormat( 8828 "template <typename T1, typename T2 = char, typename T3 = char,\n" 8829 " typename T4 = char>\n" 8830 "void f();"); 8831 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 8832 " template <typename> class cccccccccccccccccccccc,\n" 8833 " typename ddddddddddddd>\n" 8834 "class C {};"); 8835 verifyFormat( 8836 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 8837 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8838 8839 verifyFormat("void f() {\n" 8840 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 8841 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 8842 "}"); 8843 8844 verifyFormat("template <typename T> class C {};"); 8845 verifyFormat("template <typename T> void f();"); 8846 verifyFormat("template <typename T> void f() {}"); 8847 verifyFormat( 8848 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 8849 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8850 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 8851 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 8852 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8853 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 8854 " bbbbbbbbbbbbbbbbbbbbbbbb);", 8855 getLLVMStyleWithColumns(72)); 8856 EXPECT_EQ("static_cast<A< //\n" 8857 " B> *>(\n" 8858 "\n" 8859 ");", 8860 format("static_cast<A<//\n" 8861 " B>*>(\n" 8862 "\n" 8863 " );")); 8864 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8865 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 8866 8867 FormatStyle AlwaysBreak = getLLVMStyle(); 8868 AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 8869 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 8870 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 8871 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 8872 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8873 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 8874 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 8875 verifyFormat("template <template <typename> class Fooooooo,\n" 8876 " template <typename> class Baaaaaaar>\n" 8877 "struct C {};", 8878 AlwaysBreak); 8879 verifyFormat("template <typename T> // T can be A, B or C.\n" 8880 "struct C {};", 8881 AlwaysBreak); 8882 verifyFormat("template <enum E> class A {\n" 8883 "public:\n" 8884 " E *f();\n" 8885 "};"); 8886 8887 FormatStyle NeverBreak = getLLVMStyle(); 8888 NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No; 8889 verifyFormat("template <typename T> class C {};", NeverBreak); 8890 verifyFormat("template <typename T> void f();", NeverBreak); 8891 verifyFormat("template <typename T> void f() {}", NeverBreak); 8892 verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa " 8893 "bbbbbbbbbbbbbbbbbbbb) {}", 8894 NeverBreak); 8895 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8896 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 8897 " ccccccccccccccccccccccccccccccccccccccccccccccc);", 8898 NeverBreak); 8899 verifyFormat("template <template <typename> class Fooooooo,\n" 8900 " template <typename> class Baaaaaaar>\n" 8901 "struct C {};", 8902 NeverBreak); 8903 verifyFormat("template <typename T> // T can be A, B or C.\n" 8904 "struct C {};", 8905 NeverBreak); 8906 verifyFormat("template <enum E> class A {\n" 8907 "public:\n" 8908 " E *f();\n" 8909 "};", 8910 NeverBreak); 8911 NeverBreak.PenaltyBreakTemplateDeclaration = 100; 8912 verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa " 8913 "bbbbbbbbbbbbbbbbbbbb) {}", 8914 NeverBreak); 8915 } 8916 8917 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) { 8918 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 8919 Style.ColumnLimit = 60; 8920 EXPECT_EQ("// Baseline - no comments.\n" 8921 "template <\n" 8922 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 8923 "void f() {}", 8924 format("// Baseline - no comments.\n" 8925 "template <\n" 8926 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 8927 "void f() {}", 8928 Style)); 8929 8930 EXPECT_EQ("template <\n" 8931 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 8932 "void f() {}", 8933 format("template <\n" 8934 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 8935 "void f() {}", 8936 Style)); 8937 8938 EXPECT_EQ( 8939 "template <\n" 8940 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 8941 "void f() {}", 8942 format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 8943 "void f() {}", 8944 Style)); 8945 8946 EXPECT_EQ( 8947 "template <\n" 8948 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 8949 " // multiline\n" 8950 "void f() {}", 8951 format("template <\n" 8952 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 8953 " // multiline\n" 8954 "void f() {}", 8955 Style)); 8956 8957 EXPECT_EQ( 8958 "template <typename aaaaaaaaaa<\n" 8959 " bbbbbbbbbbbb>::value> // trailing loooong\n" 8960 "void f() {}", 8961 format( 8962 "template <\n" 8963 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n" 8964 "void f() {}", 8965 Style)); 8966 } 8967 8968 TEST_F(FormatTest, WrapsTemplateParameters) { 8969 FormatStyle Style = getLLVMStyle(); 8970 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 8971 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 8972 verifyFormat( 8973 "template <typename... a> struct q {};\n" 8974 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 8975 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 8976 " y;", 8977 Style); 8978 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 8979 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 8980 verifyFormat( 8981 "template <typename... a> struct r {};\n" 8982 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 8983 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 8984 " y;", 8985 Style); 8986 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 8987 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 8988 verifyFormat("template <typename... a> struct s {};\n" 8989 "extern s<\n" 8990 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 8991 "aaaaaaaaaaaaaaaaaaaaaa,\n" 8992 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 8993 "aaaaaaaaaaaaaaaaaaaaaa>\n" 8994 " y;", 8995 Style); 8996 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 8997 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 8998 verifyFormat("template <typename... a> struct t {};\n" 8999 "extern t<\n" 9000 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 9001 "aaaaaaaaaaaaaaaaaaaaaa,\n" 9002 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 9003 "aaaaaaaaaaaaaaaaaaaaaa>\n" 9004 " y;", 9005 Style); 9006 } 9007 9008 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 9009 verifyFormat( 9010 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 9011 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 9012 verifyFormat( 9013 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 9014 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9015 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 9016 9017 // FIXME: Should we have the extra indent after the second break? 9018 verifyFormat( 9019 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 9020 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 9021 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 9022 9023 verifyFormat( 9024 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 9025 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 9026 9027 // Breaking at nested name specifiers is generally not desirable. 9028 verifyFormat( 9029 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9030 " aaaaaaaaaaaaaaaaaaaaaaa);"); 9031 9032 verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n" 9033 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 9034 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9035 " aaaaaaaaaaaaaaaaaaaaa);", 9036 getLLVMStyleWithColumns(74)); 9037 9038 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 9039 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9040 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 9041 } 9042 9043 TEST_F(FormatTest, UnderstandsTemplateParameters) { 9044 verifyFormat("A<int> a;"); 9045 verifyFormat("A<A<A<int>>> a;"); 9046 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 9047 verifyFormat("bool x = a < 1 || 2 > a;"); 9048 verifyFormat("bool x = 5 < f<int>();"); 9049 verifyFormat("bool x = f<int>() > 5;"); 9050 verifyFormat("bool x = 5 < a<int>::x;"); 9051 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 9052 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 9053 9054 verifyGoogleFormat("A<A<int>> a;"); 9055 verifyGoogleFormat("A<A<A<int>>> a;"); 9056 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 9057 verifyGoogleFormat("A<A<int> > a;"); 9058 verifyGoogleFormat("A<A<A<int> > > a;"); 9059 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 9060 verifyGoogleFormat("A<::A<int>> a;"); 9061 verifyGoogleFormat("A<::A> a;"); 9062 verifyGoogleFormat("A< ::A> a;"); 9063 verifyGoogleFormat("A< ::A<int> > a;"); 9064 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 9065 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 9066 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 9067 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 9068 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 9069 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 9070 9071 verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 9072 9073 // template closer followed by a token that starts with > or = 9074 verifyFormat("bool b = a<1> > 1;"); 9075 verifyFormat("bool b = a<1> >= 1;"); 9076 verifyFormat("int i = a<1> >> 1;"); 9077 FormatStyle Style = getLLVMStyle(); 9078 Style.SpaceBeforeAssignmentOperators = false; 9079 verifyFormat("bool b= a<1> == 1;", Style); 9080 verifyFormat("a<int> = 1;", Style); 9081 verifyFormat("a<int> >>= 1;", Style); 9082 9083 verifyFormat("test < a | b >> c;"); 9084 verifyFormat("test<test<a | b>> c;"); 9085 verifyFormat("test >> a >> b;"); 9086 verifyFormat("test << a >> b;"); 9087 9088 verifyFormat("f<int>();"); 9089 verifyFormat("template <typename T> void f() {}"); 9090 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 9091 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 9092 "sizeof(char)>::type>;"); 9093 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 9094 verifyFormat("f(a.operator()<A>());"); 9095 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9096 " .template operator()<A>());", 9097 getLLVMStyleWithColumns(35)); 9098 9099 // Not template parameters. 9100 verifyFormat("return a < b && c > d;"); 9101 verifyFormat("void f() {\n" 9102 " while (a < b && c > d) {\n" 9103 " }\n" 9104 "}"); 9105 verifyFormat("template <typename... Types>\n" 9106 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 9107 9108 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9109 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 9110 getLLVMStyleWithColumns(60)); 9111 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 9112 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 9113 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 9114 verifyFormat("some_templated_type<decltype([](int i) { return i; })>"); 9115 } 9116 9117 TEST_F(FormatTest, UnderstandsShiftOperators) { 9118 verifyFormat("if (i < x >> 1)"); 9119 verifyFormat("while (i < x >> 1)"); 9120 verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)"); 9121 verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)"); 9122 verifyFormat( 9123 "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)"); 9124 verifyFormat("Foo.call<Bar<Function>>()"); 9125 verifyFormat("if (Foo.call<Bar<Function>>() == 0)"); 9126 verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; " 9127 "++i, v = v >> 1)"); 9128 verifyFormat("if (w<u<v<x>>, 1>::t)"); 9129 } 9130 9131 TEST_F(FormatTest, BitshiftOperatorWidth) { 9132 EXPECT_EQ("int a = 1 << 2; /* foo\n" 9133 " bar */", 9134 format("int a=1<<2; /* foo\n" 9135 " bar */")); 9136 9137 EXPECT_EQ("int b = 256 >> 1; /* foo\n" 9138 " bar */", 9139 format("int b =256>>1 ; /* foo\n" 9140 " bar */")); 9141 } 9142 9143 TEST_F(FormatTest, UnderstandsBinaryOperators) { 9144 verifyFormat("COMPARE(a, ==, b);"); 9145 verifyFormat("auto s = sizeof...(Ts) - 1;"); 9146 } 9147 9148 TEST_F(FormatTest, UnderstandsPointersToMembers) { 9149 verifyFormat("int A::*x;"); 9150 verifyFormat("int (S::*func)(void *);"); 9151 verifyFormat("void f() { int (S::*func)(void *); }"); 9152 verifyFormat("typedef bool *(Class::*Member)() const;"); 9153 verifyFormat("void f() {\n" 9154 " (a->*f)();\n" 9155 " a->*x;\n" 9156 " (a.*f)();\n" 9157 " ((*a).*f)();\n" 9158 " a.*x;\n" 9159 "}"); 9160 verifyFormat("void f() {\n" 9161 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 9162 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 9163 "}"); 9164 verifyFormat( 9165 "(aaaaaaaaaa->*bbbbbbb)(\n" 9166 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 9167 FormatStyle Style = getLLVMStyle(); 9168 Style.PointerAlignment = FormatStyle::PAS_Left; 9169 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 9170 } 9171 9172 TEST_F(FormatTest, UnderstandsUnaryOperators) { 9173 verifyFormat("int a = -2;"); 9174 verifyFormat("f(-1, -2, -3);"); 9175 verifyFormat("a[-1] = 5;"); 9176 verifyFormat("int a = 5 + -2;"); 9177 verifyFormat("if (i == -1) {\n}"); 9178 verifyFormat("if (i != -1) {\n}"); 9179 verifyFormat("if (i > -1) {\n}"); 9180 verifyFormat("if (i < -1) {\n}"); 9181 verifyFormat("++(a->f());"); 9182 verifyFormat("--(a->f());"); 9183 verifyFormat("(a->f())++;"); 9184 verifyFormat("a[42]++;"); 9185 verifyFormat("if (!(a->f())) {\n}"); 9186 verifyFormat("if (!+i) {\n}"); 9187 verifyFormat("~&a;"); 9188 9189 verifyFormat("a-- > b;"); 9190 verifyFormat("b ? -a : c;"); 9191 verifyFormat("n * sizeof char16;"); 9192 verifyFormat("n * alignof char16;", getGoogleStyle()); 9193 verifyFormat("sizeof(char);"); 9194 verifyFormat("alignof(char);", getGoogleStyle()); 9195 9196 verifyFormat("return -1;"); 9197 verifyFormat("throw -1;"); 9198 verifyFormat("switch (a) {\n" 9199 "case -1:\n" 9200 " break;\n" 9201 "}"); 9202 verifyFormat("#define X -1"); 9203 verifyFormat("#define X -kConstant"); 9204 9205 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 9206 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 9207 9208 verifyFormat("int a = /* confusing comment */ -1;"); 9209 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 9210 verifyFormat("int a = i /* confusing comment */++;"); 9211 9212 verifyFormat("co_yield -1;"); 9213 verifyFormat("co_return -1;"); 9214 9215 // Check that * is not treated as a binary operator when we set 9216 // PointerAlignment as PAS_Left after a keyword and not a declaration. 9217 FormatStyle PASLeftStyle = getLLVMStyle(); 9218 PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left; 9219 verifyFormat("co_return *a;", PASLeftStyle); 9220 verifyFormat("co_await *a;", PASLeftStyle); 9221 verifyFormat("co_yield *a", PASLeftStyle); 9222 verifyFormat("return *a;", PASLeftStyle); 9223 } 9224 9225 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 9226 verifyFormat("if (!aaaaaaaaaa( // break\n" 9227 " aaaaa)) {\n" 9228 "}"); 9229 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 9230 " aaaaa));"); 9231 verifyFormat("*aaa = aaaaaaa( // break\n" 9232 " bbbbbb);"); 9233 } 9234 9235 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 9236 verifyFormat("bool operator<();"); 9237 verifyFormat("bool operator>();"); 9238 verifyFormat("bool operator=();"); 9239 verifyFormat("bool operator==();"); 9240 verifyFormat("bool operator!=();"); 9241 verifyFormat("int operator+();"); 9242 verifyFormat("int operator++();"); 9243 verifyFormat("int operator++(int) volatile noexcept;"); 9244 verifyFormat("bool operator,();"); 9245 verifyFormat("bool operator();"); 9246 verifyFormat("bool operator()();"); 9247 verifyFormat("bool operator[]();"); 9248 verifyFormat("operator bool();"); 9249 verifyFormat("operator int();"); 9250 verifyFormat("operator void *();"); 9251 verifyFormat("operator SomeType<int>();"); 9252 verifyFormat("operator SomeType<int, int>();"); 9253 verifyFormat("operator SomeType<SomeType<int>>();"); 9254 verifyFormat("void *operator new(std::size_t size);"); 9255 verifyFormat("void *operator new[](std::size_t size);"); 9256 verifyFormat("void operator delete(void *ptr);"); 9257 verifyFormat("void operator delete[](void *ptr);"); 9258 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 9259 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 9260 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 9261 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 9262 9263 verifyFormat( 9264 "ostream &operator<<(ostream &OutputStream,\n" 9265 " SomeReallyLongType WithSomeReallyLongValue);"); 9266 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 9267 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 9268 " return left.group < right.group;\n" 9269 "}"); 9270 verifyFormat("SomeType &operator=(const SomeType &S);"); 9271 verifyFormat("f.template operator()<int>();"); 9272 9273 verifyGoogleFormat("operator void*();"); 9274 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 9275 verifyGoogleFormat("operator ::A();"); 9276 9277 verifyFormat("using A::operator+;"); 9278 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 9279 "int i;"); 9280 9281 // Calling an operator as a member function. 9282 verifyFormat("void f() { a.operator*(); }"); 9283 verifyFormat("void f() { a.operator*(b & b); }"); 9284 verifyFormat("void f() { a->operator&(a * b); }"); 9285 verifyFormat("void f() { NS::a.operator+(*b * *b); }"); 9286 // TODO: Calling an operator as a non-member function is hard to distinguish. 9287 // https://llvm.org/PR50629 9288 // verifyFormat("void f() { operator*(a & a); }"); 9289 // verifyFormat("void f() { operator&(a, b * b); }"); 9290 9291 verifyFormat("::operator delete(foo);"); 9292 verifyFormat("::operator new(n * sizeof(foo));"); 9293 verifyFormat("foo() { ::operator delete(foo); }"); 9294 verifyFormat("foo() { ::operator new(n * sizeof(foo)); }"); 9295 } 9296 9297 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 9298 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 9299 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 9300 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 9301 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 9302 verifyFormat("Deleted &operator=(const Deleted &) &;"); 9303 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 9304 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 9305 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 9306 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 9307 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 9308 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 9309 verifyFormat("void Fn(T const &) const &;"); 9310 verifyFormat("void Fn(T const volatile &&) const volatile &&;"); 9311 verifyFormat("template <typename T>\n" 9312 "void F(T) && = delete;", 9313 getGoogleStyle()); 9314 9315 FormatStyle AlignLeft = getLLVMStyle(); 9316 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 9317 verifyFormat("void A::b() && {}", AlignLeft); 9318 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 9319 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 9320 AlignLeft); 9321 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 9322 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 9323 verifyFormat("auto Function(T t) & -> void {}", AlignLeft); 9324 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft); 9325 verifyFormat("auto Function(T) & -> void {}", AlignLeft); 9326 verifyFormat("auto Function(T) & -> void;", AlignLeft); 9327 verifyFormat("void Fn(T const&) const&;", AlignLeft); 9328 verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft); 9329 9330 FormatStyle Spaces = getLLVMStyle(); 9331 Spaces.SpacesInCStyleCastParentheses = true; 9332 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 9333 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 9334 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 9335 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 9336 9337 Spaces.SpacesInCStyleCastParentheses = false; 9338 Spaces.SpacesInParentheses = true; 9339 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 9340 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", 9341 Spaces); 9342 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 9343 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 9344 9345 FormatStyle BreakTemplate = getLLVMStyle(); 9346 BreakTemplate.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 9347 9348 verifyFormat("struct f {\n" 9349 " template <class T>\n" 9350 " int &foo(const std::string &str) &noexcept {}\n" 9351 "};", 9352 BreakTemplate); 9353 9354 verifyFormat("struct f {\n" 9355 " template <class T>\n" 9356 " int &foo(const std::string &str) &&noexcept {}\n" 9357 "};", 9358 BreakTemplate); 9359 9360 verifyFormat("struct f {\n" 9361 " template <class T>\n" 9362 " int &foo(const std::string &str) const &noexcept {}\n" 9363 "};", 9364 BreakTemplate); 9365 9366 verifyFormat("struct f {\n" 9367 " template <class T>\n" 9368 " int &foo(const std::string &str) const &noexcept {}\n" 9369 "};", 9370 BreakTemplate); 9371 9372 verifyFormat("struct f {\n" 9373 " template <class T>\n" 9374 " auto foo(const std::string &str) &&noexcept -> int & {}\n" 9375 "};", 9376 BreakTemplate); 9377 9378 FormatStyle AlignLeftBreakTemplate = getLLVMStyle(); 9379 AlignLeftBreakTemplate.AlwaysBreakTemplateDeclarations = 9380 FormatStyle::BTDS_Yes; 9381 AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left; 9382 9383 verifyFormat("struct f {\n" 9384 " template <class T>\n" 9385 " int& foo(const std::string& str) & noexcept {}\n" 9386 "};", 9387 AlignLeftBreakTemplate); 9388 9389 verifyFormat("struct f {\n" 9390 " template <class T>\n" 9391 " int& foo(const std::string& str) && noexcept {}\n" 9392 "};", 9393 AlignLeftBreakTemplate); 9394 9395 verifyFormat("struct f {\n" 9396 " template <class T>\n" 9397 " int& foo(const std::string& str) const& noexcept {}\n" 9398 "};", 9399 AlignLeftBreakTemplate); 9400 9401 verifyFormat("struct f {\n" 9402 " template <class T>\n" 9403 " int& foo(const std::string& str) const&& noexcept {}\n" 9404 "};", 9405 AlignLeftBreakTemplate); 9406 9407 verifyFormat("struct f {\n" 9408 " template <class T>\n" 9409 " auto foo(const std::string& str) && noexcept -> int& {}\n" 9410 "};", 9411 AlignLeftBreakTemplate); 9412 9413 // The `&` in `Type&` should not be confused with a trailing `&` of 9414 // DEPRECATED(reason) member function. 9415 verifyFormat("struct f {\n" 9416 " template <class T>\n" 9417 " DEPRECATED(reason)\n" 9418 " Type &foo(arguments) {}\n" 9419 "};", 9420 BreakTemplate); 9421 9422 verifyFormat("struct f {\n" 9423 " template <class T>\n" 9424 " DEPRECATED(reason)\n" 9425 " Type& foo(arguments) {}\n" 9426 "};", 9427 AlignLeftBreakTemplate); 9428 9429 verifyFormat("void (*foopt)(int) = &func;"); 9430 } 9431 9432 TEST_F(FormatTest, UnderstandsNewAndDelete) { 9433 verifyFormat("void f() {\n" 9434 " A *a = new A;\n" 9435 " A *a = new (placement) A;\n" 9436 " delete a;\n" 9437 " delete (A *)a;\n" 9438 "}"); 9439 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 9440 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 9441 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 9442 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 9443 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 9444 verifyFormat("delete[] h->p;"); 9445 } 9446 9447 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 9448 verifyFormat("int *f(int *a) {}"); 9449 verifyFormat("int main(int argc, char **argv) {}"); 9450 verifyFormat("Test::Test(int b) : a(b * b) {}"); 9451 verifyIndependentOfContext("f(a, *a);"); 9452 verifyFormat("void g() { f(*a); }"); 9453 verifyIndependentOfContext("int a = b * 10;"); 9454 verifyIndependentOfContext("int a = 10 * b;"); 9455 verifyIndependentOfContext("int a = b * c;"); 9456 verifyIndependentOfContext("int a += b * c;"); 9457 verifyIndependentOfContext("int a -= b * c;"); 9458 verifyIndependentOfContext("int a *= b * c;"); 9459 verifyIndependentOfContext("int a /= b * c;"); 9460 verifyIndependentOfContext("int a = *b;"); 9461 verifyIndependentOfContext("int a = *b * c;"); 9462 verifyIndependentOfContext("int a = b * *c;"); 9463 verifyIndependentOfContext("int a = b * (10);"); 9464 verifyIndependentOfContext("S << b * (10);"); 9465 verifyIndependentOfContext("return 10 * b;"); 9466 verifyIndependentOfContext("return *b * *c;"); 9467 verifyIndependentOfContext("return a & ~b;"); 9468 verifyIndependentOfContext("f(b ? *c : *d);"); 9469 verifyIndependentOfContext("int a = b ? *c : *d;"); 9470 verifyIndependentOfContext("*b = a;"); 9471 verifyIndependentOfContext("a * ~b;"); 9472 verifyIndependentOfContext("a * !b;"); 9473 verifyIndependentOfContext("a * +b;"); 9474 verifyIndependentOfContext("a * -b;"); 9475 verifyIndependentOfContext("a * ++b;"); 9476 verifyIndependentOfContext("a * --b;"); 9477 verifyIndependentOfContext("a[4] * b;"); 9478 verifyIndependentOfContext("a[a * a] = 1;"); 9479 verifyIndependentOfContext("f() * b;"); 9480 verifyIndependentOfContext("a * [self dostuff];"); 9481 verifyIndependentOfContext("int x = a * (a + b);"); 9482 verifyIndependentOfContext("(a *)(a + b);"); 9483 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 9484 verifyIndependentOfContext("int *pa = (int *)&a;"); 9485 verifyIndependentOfContext("return sizeof(int **);"); 9486 verifyIndependentOfContext("return sizeof(int ******);"); 9487 verifyIndependentOfContext("return (int **&)a;"); 9488 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 9489 verifyFormat("void f(Type (*parameter)[10]) {}"); 9490 verifyFormat("void f(Type (¶meter)[10]) {}"); 9491 verifyGoogleFormat("return sizeof(int**);"); 9492 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 9493 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 9494 verifyFormat("auto a = [](int **&, int ***) {};"); 9495 verifyFormat("auto PointerBinding = [](const char *S) {};"); 9496 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 9497 verifyFormat("[](const decltype(*a) &value) {}"); 9498 verifyFormat("[](const typeof(*a) &value) {}"); 9499 verifyFormat("[](const _Atomic(a *) &value) {}"); 9500 verifyFormat("[](const __underlying_type(a) &value) {}"); 9501 verifyFormat("decltype(a * b) F();"); 9502 verifyFormat("typeof(a * b) F();"); 9503 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 9504 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 9505 verifyIndependentOfContext("typedef void (*f)(int *a);"); 9506 verifyIndependentOfContext("int i{a * b};"); 9507 verifyIndependentOfContext("aaa && aaa->f();"); 9508 verifyIndependentOfContext("int x = ~*p;"); 9509 verifyFormat("Constructor() : a(a), area(width * height) {}"); 9510 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 9511 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 9512 verifyFormat("void f() { f(a, c * d); }"); 9513 verifyFormat("void f() { f(new a(), c * d); }"); 9514 verifyFormat("void f(const MyOverride &override);"); 9515 verifyFormat("void f(const MyFinal &final);"); 9516 verifyIndependentOfContext("bool a = f() && override.f();"); 9517 verifyIndependentOfContext("bool a = f() && final.f();"); 9518 9519 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 9520 9521 verifyIndependentOfContext("A<int *> a;"); 9522 verifyIndependentOfContext("A<int **> a;"); 9523 verifyIndependentOfContext("A<int *, int *> a;"); 9524 verifyIndependentOfContext("A<int *[]> a;"); 9525 verifyIndependentOfContext( 9526 "const char *const p = reinterpret_cast<const char *const>(q);"); 9527 verifyIndependentOfContext("A<int **, int **> a;"); 9528 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 9529 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 9530 verifyFormat("for (; a && b;) {\n}"); 9531 verifyFormat("bool foo = true && [] { return false; }();"); 9532 9533 verifyFormat( 9534 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9535 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9536 9537 verifyGoogleFormat("int const* a = &b;"); 9538 verifyGoogleFormat("**outparam = 1;"); 9539 verifyGoogleFormat("*outparam = a * b;"); 9540 verifyGoogleFormat("int main(int argc, char** argv) {}"); 9541 verifyGoogleFormat("A<int*> a;"); 9542 verifyGoogleFormat("A<int**> a;"); 9543 verifyGoogleFormat("A<int*, int*> a;"); 9544 verifyGoogleFormat("A<int**, int**> a;"); 9545 verifyGoogleFormat("f(b ? *c : *d);"); 9546 verifyGoogleFormat("int a = b ? *c : *d;"); 9547 verifyGoogleFormat("Type* t = **x;"); 9548 verifyGoogleFormat("Type* t = *++*x;"); 9549 verifyGoogleFormat("*++*x;"); 9550 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 9551 verifyGoogleFormat("Type* t = x++ * y;"); 9552 verifyGoogleFormat( 9553 "const char* const p = reinterpret_cast<const char* const>(q);"); 9554 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 9555 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 9556 verifyGoogleFormat("template <typename T>\n" 9557 "void f(int i = 0, SomeType** temps = NULL);"); 9558 9559 FormatStyle Left = getLLVMStyle(); 9560 Left.PointerAlignment = FormatStyle::PAS_Left; 9561 verifyFormat("x = *a(x) = *a(y);", Left); 9562 verifyFormat("for (;; *a = b) {\n}", Left); 9563 verifyFormat("return *this += 1;", Left); 9564 verifyFormat("throw *x;", Left); 9565 verifyFormat("delete *x;", Left); 9566 verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left); 9567 verifyFormat("[](const decltype(*a)* ptr) {}", Left); 9568 verifyFormat("[](const typeof(*a)* ptr) {}", Left); 9569 verifyFormat("[](const _Atomic(a*)* ptr) {}", Left); 9570 verifyFormat("[](const __underlying_type(a)* ptr) {}", Left); 9571 verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left); 9572 verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left); 9573 verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left); 9574 verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left); 9575 9576 verifyIndependentOfContext("a = *(x + y);"); 9577 verifyIndependentOfContext("a = &(x + y);"); 9578 verifyIndependentOfContext("*(x + y).call();"); 9579 verifyIndependentOfContext("&(x + y)->call();"); 9580 verifyFormat("void f() { &(*I).first; }"); 9581 9582 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 9583 verifyFormat("f(* /* confusing comment */ foo);"); 9584 verifyFormat("void (* /*deleter*/)(const Slice &key, void *value)"); 9585 verifyFormat("void foo(int * // this is the first paramters\n" 9586 " ,\n" 9587 " int second);"); 9588 verifyFormat("double term = a * // first\n" 9589 " b;"); 9590 verifyFormat( 9591 "int *MyValues = {\n" 9592 " *A, // Operator detection might be confused by the '{'\n" 9593 " *BB // Operator detection might be confused by previous comment\n" 9594 "};"); 9595 9596 verifyIndependentOfContext("if (int *a = &b)"); 9597 verifyIndependentOfContext("if (int &a = *b)"); 9598 verifyIndependentOfContext("if (a & b[i])"); 9599 verifyIndependentOfContext("if constexpr (a & b[i])"); 9600 verifyIndependentOfContext("if CONSTEXPR (a & b[i])"); 9601 verifyIndependentOfContext("if (a * (b * c))"); 9602 verifyIndependentOfContext("if constexpr (a * (b * c))"); 9603 verifyIndependentOfContext("if CONSTEXPR (a * (b * c))"); 9604 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 9605 verifyIndependentOfContext("if (*b[i])"); 9606 verifyIndependentOfContext("if (int *a = (&b))"); 9607 verifyIndependentOfContext("while (int *a = &b)"); 9608 verifyIndependentOfContext("while (a * (b * c))"); 9609 verifyIndependentOfContext("size = sizeof *a;"); 9610 verifyIndependentOfContext("if (a && (b = c))"); 9611 verifyFormat("void f() {\n" 9612 " for (const int &v : Values) {\n" 9613 " }\n" 9614 "}"); 9615 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 9616 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 9617 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 9618 9619 verifyFormat("#define A (!a * b)"); 9620 verifyFormat("#define MACRO \\\n" 9621 " int *i = a * b; \\\n" 9622 " void f(a *b);", 9623 getLLVMStyleWithColumns(19)); 9624 9625 verifyIndependentOfContext("A = new SomeType *[Length];"); 9626 verifyIndependentOfContext("A = new SomeType *[Length]();"); 9627 verifyIndependentOfContext("T **t = new T *;"); 9628 verifyIndependentOfContext("T **t = new T *();"); 9629 verifyGoogleFormat("A = new SomeType*[Length]();"); 9630 verifyGoogleFormat("A = new SomeType*[Length];"); 9631 verifyGoogleFormat("T** t = new T*;"); 9632 verifyGoogleFormat("T** t = new T*();"); 9633 9634 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 9635 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 9636 verifyFormat("template <bool a, bool b> " 9637 "typename t::if<x && y>::type f() {}"); 9638 verifyFormat("template <int *y> f() {}"); 9639 verifyFormat("vector<int *> v;"); 9640 verifyFormat("vector<int *const> v;"); 9641 verifyFormat("vector<int *const **const *> v;"); 9642 verifyFormat("vector<int *volatile> v;"); 9643 verifyFormat("vector<a *_Nonnull> v;"); 9644 verifyFormat("vector<a *_Nullable> v;"); 9645 verifyFormat("vector<a *_Null_unspecified> v;"); 9646 verifyFormat("vector<a *__ptr32> v;"); 9647 verifyFormat("vector<a *__ptr64> v;"); 9648 verifyFormat("vector<a *__capability> v;"); 9649 FormatStyle TypeMacros = getLLVMStyle(); 9650 TypeMacros.TypenameMacros = {"LIST"}; 9651 verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros); 9652 verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros); 9653 verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros); 9654 verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros); 9655 verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros); // multiplication 9656 9657 FormatStyle CustomQualifier = getLLVMStyle(); 9658 // Add identifiers that should not be parsed as a qualifier by default. 9659 CustomQualifier.AttributeMacros.push_back("__my_qualifier"); 9660 CustomQualifier.AttributeMacros.push_back("_My_qualifier"); 9661 CustomQualifier.AttributeMacros.push_back("my_other_qualifier"); 9662 verifyFormat("vector<a * __my_qualifier> parse_as_multiply;"); 9663 verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier); 9664 verifyFormat("vector<a * _My_qualifier> parse_as_multiply;"); 9665 verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier); 9666 verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;"); 9667 verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier); 9668 verifyFormat("vector<a * _NotAQualifier> v;"); 9669 verifyFormat("vector<a * __not_a_qualifier> v;"); 9670 verifyFormat("vector<a * b> v;"); 9671 verifyFormat("foo<b && false>();"); 9672 verifyFormat("foo<b & 1>();"); 9673 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 9674 verifyFormat("typeof(*::std::declval<const T &>()) void F();"); 9675 verifyFormat("_Atomic(*::std::declval<const T &>()) void F();"); 9676 verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();"); 9677 verifyFormat( 9678 "template <class T, class = typename std::enable_if<\n" 9679 " std::is_integral<T>::value &&\n" 9680 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 9681 "void F();", 9682 getLLVMStyleWithColumns(70)); 9683 verifyFormat("template <class T,\n" 9684 " class = typename std::enable_if<\n" 9685 " std::is_integral<T>::value &&\n" 9686 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n" 9687 " class U>\n" 9688 "void F();", 9689 getLLVMStyleWithColumns(70)); 9690 verifyFormat( 9691 "template <class T,\n" 9692 " class = typename ::std::enable_if<\n" 9693 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 9694 "void F();", 9695 getGoogleStyleWithColumns(68)); 9696 9697 verifyIndependentOfContext("MACRO(int *i);"); 9698 verifyIndependentOfContext("MACRO(auto *a);"); 9699 verifyIndependentOfContext("MACRO(const A *a);"); 9700 verifyIndependentOfContext("MACRO(_Atomic(A) *a);"); 9701 verifyIndependentOfContext("MACRO(decltype(A) *a);"); 9702 verifyIndependentOfContext("MACRO(typeof(A) *a);"); 9703 verifyIndependentOfContext("MACRO(__underlying_type(A) *a);"); 9704 verifyIndependentOfContext("MACRO(A *const a);"); 9705 verifyIndependentOfContext("MACRO(A *restrict a);"); 9706 verifyIndependentOfContext("MACRO(A *__restrict__ a);"); 9707 verifyIndependentOfContext("MACRO(A *__restrict a);"); 9708 verifyIndependentOfContext("MACRO(A *volatile a);"); 9709 verifyIndependentOfContext("MACRO(A *__volatile a);"); 9710 verifyIndependentOfContext("MACRO(A *__volatile__ a);"); 9711 verifyIndependentOfContext("MACRO(A *_Nonnull a);"); 9712 verifyIndependentOfContext("MACRO(A *_Nullable a);"); 9713 verifyIndependentOfContext("MACRO(A *_Null_unspecified a);"); 9714 verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);"); 9715 verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);"); 9716 verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);"); 9717 verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);"); 9718 verifyIndependentOfContext("MACRO(A *__ptr32 a);"); 9719 verifyIndependentOfContext("MACRO(A *__ptr64 a);"); 9720 verifyIndependentOfContext("MACRO(A *__capability);"); 9721 verifyIndependentOfContext("MACRO(A &__capability);"); 9722 verifyFormat("MACRO(A *__my_qualifier);"); // type declaration 9723 verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication 9724 // If we add __my_qualifier to AttributeMacros it should always be parsed as 9725 // a type declaration: 9726 verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier); 9727 verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier); 9728 // Also check that TypenameMacros prevents parsing it as multiplication: 9729 verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication 9730 verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros); // type 9731 9732 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 9733 verifyFormat("void f() { f(float{1}, a * a); }"); 9734 verifyFormat("void f() { f(float(1), a * a); }"); 9735 9736 verifyFormat("f((void (*)(int))g);"); 9737 verifyFormat("f((void (&)(int))g);"); 9738 verifyFormat("f((void (^)(int))g);"); 9739 9740 // FIXME: Is there a way to make this work? 9741 // verifyIndependentOfContext("MACRO(A *a);"); 9742 verifyFormat("MACRO(A &B);"); 9743 verifyFormat("MACRO(A *B);"); 9744 verifyFormat("void f() { MACRO(A * B); }"); 9745 verifyFormat("void f() { MACRO(A & B); }"); 9746 9747 // This lambda was mis-formatted after D88956 (treating it as a binop): 9748 verifyFormat("auto x = [](const decltype(x) &ptr) {};"); 9749 verifyFormat("auto x = [](const decltype(x) *ptr) {};"); 9750 verifyFormat("#define lambda [](const decltype(x) &ptr) {}"); 9751 verifyFormat("#define lambda [](const decltype(x) *ptr) {}"); 9752 9753 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 9754 verifyFormat("return options != nullptr && operator==(*options);"); 9755 9756 EXPECT_EQ("#define OP(x) \\\n" 9757 " ostream &operator<<(ostream &s, const A &a) { \\\n" 9758 " return s << a.DebugString(); \\\n" 9759 " }", 9760 format("#define OP(x) \\\n" 9761 " ostream &operator<<(ostream &s, const A &a) { \\\n" 9762 " return s << a.DebugString(); \\\n" 9763 " }", 9764 getLLVMStyleWithColumns(50))); 9765 9766 // FIXME: We cannot handle this case yet; we might be able to figure out that 9767 // foo<x> d > v; doesn't make sense. 9768 verifyFormat("foo<a<b && c> d> v;"); 9769 9770 FormatStyle PointerMiddle = getLLVMStyle(); 9771 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 9772 verifyFormat("delete *x;", PointerMiddle); 9773 verifyFormat("int * x;", PointerMiddle); 9774 verifyFormat("int *[] x;", PointerMiddle); 9775 verifyFormat("template <int * y> f() {}", PointerMiddle); 9776 verifyFormat("int * f(int * a) {}", PointerMiddle); 9777 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 9778 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 9779 verifyFormat("A<int *> a;", PointerMiddle); 9780 verifyFormat("A<int **> a;", PointerMiddle); 9781 verifyFormat("A<int *, int *> a;", PointerMiddle); 9782 verifyFormat("A<int *[]> a;", PointerMiddle); 9783 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 9784 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 9785 verifyFormat("T ** t = new T *;", PointerMiddle); 9786 9787 // Member function reference qualifiers aren't binary operators. 9788 verifyFormat("string // break\n" 9789 "operator()() & {}"); 9790 verifyFormat("string // break\n" 9791 "operator()() && {}"); 9792 verifyGoogleFormat("template <typename T>\n" 9793 "auto x() & -> int {}"); 9794 9795 // Should be binary operators when used as an argument expression (overloaded 9796 // operator invoked as a member function). 9797 verifyFormat("void f() { a.operator()(a * a); }"); 9798 verifyFormat("void f() { a->operator()(a & a); }"); 9799 verifyFormat("void f() { a.operator()(*a & *a); }"); 9800 verifyFormat("void f() { a->operator()(*a * *a); }"); 9801 9802 verifyFormat("int operator()(T (&&)[N]) { return 1; }"); 9803 verifyFormat("int operator()(T (&)[N]) { return 0; }"); 9804 } 9805 9806 TEST_F(FormatTest, UnderstandsAttributes) { 9807 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 9808 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 9809 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 9810 FormatStyle AfterType = getLLVMStyle(); 9811 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 9812 verifyFormat("__attribute__((nodebug)) void\n" 9813 "foo() {}\n", 9814 AfterType); 9815 verifyFormat("__unused void\n" 9816 "foo() {}", 9817 AfterType); 9818 9819 FormatStyle CustomAttrs = getLLVMStyle(); 9820 CustomAttrs.AttributeMacros.push_back("__unused"); 9821 CustomAttrs.AttributeMacros.push_back("__attr1"); 9822 CustomAttrs.AttributeMacros.push_back("__attr2"); 9823 CustomAttrs.AttributeMacros.push_back("no_underscore_attr"); 9824 verifyFormat("vector<SomeType *__attribute((foo))> v;"); 9825 verifyFormat("vector<SomeType *__attribute__((foo))> v;"); 9826 verifyFormat("vector<SomeType * __not_attribute__((foo))> v;"); 9827 // Check that it is parsed as a multiplication without AttributeMacros and 9828 // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros. 9829 verifyFormat("vector<SomeType * __attr1> v;"); 9830 verifyFormat("vector<SomeType __attr1 *> v;"); 9831 verifyFormat("vector<SomeType __attr1 *const> v;"); 9832 verifyFormat("vector<SomeType __attr1 * __attr2> v;"); 9833 verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs); 9834 verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs); 9835 verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs); 9836 verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs); 9837 verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs); 9838 verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs); 9839 verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs); 9840 9841 // Check that these are not parsed as function declarations: 9842 CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 9843 CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman; 9844 verifyFormat("SomeType s(InitValue);", CustomAttrs); 9845 verifyFormat("SomeType s{InitValue};", CustomAttrs); 9846 verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs); 9847 verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs); 9848 verifyFormat("SomeType s __unused(InitValue);", CustomAttrs); 9849 verifyFormat("SomeType s __unused{InitValue};", CustomAttrs); 9850 verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs); 9851 verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs); 9852 } 9853 9854 TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) { 9855 // Check that qualifiers on pointers don't break parsing of casts. 9856 verifyFormat("x = (foo *const)*v;"); 9857 verifyFormat("x = (foo *volatile)*v;"); 9858 verifyFormat("x = (foo *restrict)*v;"); 9859 verifyFormat("x = (foo *__attribute__((foo)))*v;"); 9860 verifyFormat("x = (foo *_Nonnull)*v;"); 9861 verifyFormat("x = (foo *_Nullable)*v;"); 9862 verifyFormat("x = (foo *_Null_unspecified)*v;"); 9863 verifyFormat("x = (foo *_Nonnull)*v;"); 9864 verifyFormat("x = (foo *[[clang::attr]])*v;"); 9865 verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;"); 9866 verifyFormat("x = (foo *__ptr32)*v;"); 9867 verifyFormat("x = (foo *__ptr64)*v;"); 9868 verifyFormat("x = (foo *__capability)*v;"); 9869 9870 // Check that we handle multiple trailing qualifiers and skip them all to 9871 // determine that the expression is a cast to a pointer type. 9872 FormatStyle LongPointerRight = getLLVMStyleWithColumns(999); 9873 FormatStyle LongPointerLeft = getLLVMStyleWithColumns(999); 9874 LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left; 9875 StringRef AllQualifiers = 9876 "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified " 9877 "_Nonnull [[clang::attr]] __ptr32 __ptr64 __capability"; 9878 verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight); 9879 verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft); 9880 9881 // Also check that address-of is not parsed as a binary bitwise-and: 9882 verifyFormat("x = (foo *const)&v;"); 9883 verifyFormat(("x = (foo *" + AllQualifiers + ")&v;").str(), LongPointerRight); 9884 verifyFormat(("x = (foo* " + AllQualifiers + ")&v;").str(), LongPointerLeft); 9885 9886 // Check custom qualifiers: 9887 FormatStyle CustomQualifier = getLLVMStyleWithColumns(999); 9888 CustomQualifier.AttributeMacros.push_back("__my_qualifier"); 9889 verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier. 9890 verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier); 9891 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)*v;").str(), 9892 CustomQualifier); 9893 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)&v;").str(), 9894 CustomQualifier); 9895 9896 // Check that unknown identifiers result in binary operator parsing: 9897 verifyFormat("x = (foo * __unknown_qualifier) * v;"); 9898 verifyFormat("x = (foo * __unknown_qualifier) & v;"); 9899 } 9900 9901 TEST_F(FormatTest, UnderstandsSquareAttributes) { 9902 verifyFormat("SomeType s [[unused]] (InitValue);"); 9903 verifyFormat("SomeType s [[gnu::unused]] (InitValue);"); 9904 verifyFormat("SomeType s [[using gnu: unused]] (InitValue);"); 9905 verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}"); 9906 verifyFormat("void f() [[deprecated(\"so sorry\")]];"); 9907 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9908 " [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 9909 verifyFormat("[[nodiscard]] bool f() { return false; }"); 9910 verifyFormat("class [[nodiscard]] f {\npublic:\n f() {}\n}"); 9911 verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n f() {}\n}"); 9912 verifyFormat("class [[gnu::unused]] f {\npublic:\n f() {}\n}"); 9913 9914 // Make sure we do not mistake attributes for array subscripts. 9915 verifyFormat("int a() {}\n" 9916 "[[unused]] int b() {}\n"); 9917 verifyFormat("NSArray *arr;\n" 9918 "arr[[Foo() bar]];"); 9919 9920 // On the other hand, we still need to correctly find array subscripts. 9921 verifyFormat("int a = std::vector<int>{1, 2, 3}[0];"); 9922 9923 // Make sure that we do not mistake Objective-C method inside array literals 9924 // as attributes, even if those method names are also keywords. 9925 verifyFormat("@[ [foo bar] ];"); 9926 verifyFormat("@[ [NSArray class] ];"); 9927 verifyFormat("@[ [foo enum] ];"); 9928 9929 verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }"); 9930 9931 // Make sure we do not parse attributes as lambda introducers. 9932 FormatStyle MultiLineFunctions = getLLVMStyle(); 9933 MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 9934 verifyFormat("[[unused]] int b() {\n" 9935 " return 42;\n" 9936 "}\n", 9937 MultiLineFunctions); 9938 } 9939 9940 TEST_F(FormatTest, AttributeClass) { 9941 FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp); 9942 verifyFormat("class S {\n" 9943 " S(S&&) = default;\n" 9944 "};", 9945 Style); 9946 verifyFormat("class [[nodiscard]] S {\n" 9947 " S(S&&) = default;\n" 9948 "};", 9949 Style); 9950 verifyFormat("class __attribute((maybeunused)) S {\n" 9951 " S(S&&) = default;\n" 9952 "};", 9953 Style); 9954 verifyFormat("struct S {\n" 9955 " S(S&&) = default;\n" 9956 "};", 9957 Style); 9958 verifyFormat("struct [[nodiscard]] S {\n" 9959 " S(S&&) = default;\n" 9960 "};", 9961 Style); 9962 } 9963 9964 TEST_F(FormatTest, AttributesAfterMacro) { 9965 FormatStyle Style = getLLVMStyle(); 9966 verifyFormat("MACRO;\n" 9967 "__attribute__((maybe_unused)) int foo() {\n" 9968 " //...\n" 9969 "}"); 9970 9971 verifyFormat("MACRO;\n" 9972 "[[nodiscard]] int foo() {\n" 9973 " //...\n" 9974 "}"); 9975 9976 EXPECT_EQ("MACRO\n\n" 9977 "__attribute__((maybe_unused)) int foo() {\n" 9978 " //...\n" 9979 "}", 9980 format("MACRO\n\n" 9981 "__attribute__((maybe_unused)) int foo() {\n" 9982 " //...\n" 9983 "}")); 9984 9985 EXPECT_EQ("MACRO\n\n" 9986 "[[nodiscard]] int foo() {\n" 9987 " //...\n" 9988 "}", 9989 format("MACRO\n\n" 9990 "[[nodiscard]] int foo() {\n" 9991 " //...\n" 9992 "}")); 9993 } 9994 9995 TEST_F(FormatTest, AttributePenaltyBreaking) { 9996 FormatStyle Style = getLLVMStyle(); 9997 verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n" 9998 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", 9999 Style); 10000 verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n" 10001 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", 10002 Style); 10003 verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const " 10004 "shared_ptr<ALongTypeName> &C d) {\n}", 10005 Style); 10006 } 10007 10008 TEST_F(FormatTest, UnderstandsEllipsis) { 10009 FormatStyle Style = getLLVMStyle(); 10010 verifyFormat("int printf(const char *fmt, ...);"); 10011 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 10012 verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}"); 10013 10014 verifyFormat("template <int *...PP> a;", Style); 10015 10016 Style.PointerAlignment = FormatStyle::PAS_Left; 10017 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style); 10018 10019 verifyFormat("template <int*... PP> a;", Style); 10020 10021 Style.PointerAlignment = FormatStyle::PAS_Middle; 10022 verifyFormat("template <int *... PP> a;", Style); 10023 } 10024 10025 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 10026 EXPECT_EQ("int *a;\n" 10027 "int *a;\n" 10028 "int *a;", 10029 format("int *a;\n" 10030 "int* a;\n" 10031 "int *a;", 10032 getGoogleStyle())); 10033 EXPECT_EQ("int* a;\n" 10034 "int* a;\n" 10035 "int* a;", 10036 format("int* a;\n" 10037 "int* a;\n" 10038 "int *a;", 10039 getGoogleStyle())); 10040 EXPECT_EQ("int *a;\n" 10041 "int *a;\n" 10042 "int *a;", 10043 format("int *a;\n" 10044 "int * a;\n" 10045 "int * a;", 10046 getGoogleStyle())); 10047 EXPECT_EQ("auto x = [] {\n" 10048 " int *a;\n" 10049 " int *a;\n" 10050 " int *a;\n" 10051 "};", 10052 format("auto x=[]{int *a;\n" 10053 "int * a;\n" 10054 "int * a;};", 10055 getGoogleStyle())); 10056 } 10057 10058 TEST_F(FormatTest, UnderstandsRvalueReferences) { 10059 verifyFormat("int f(int &&a) {}"); 10060 verifyFormat("int f(int a, char &&b) {}"); 10061 verifyFormat("void f() { int &&a = b; }"); 10062 verifyGoogleFormat("int f(int a, char&& b) {}"); 10063 verifyGoogleFormat("void f() { int&& a = b; }"); 10064 10065 verifyIndependentOfContext("A<int &&> a;"); 10066 verifyIndependentOfContext("A<int &&, int &&> a;"); 10067 verifyGoogleFormat("A<int&&> a;"); 10068 verifyGoogleFormat("A<int&&, int&&> a;"); 10069 10070 // Not rvalue references: 10071 verifyFormat("template <bool B, bool C> class A {\n" 10072 " static_assert(B && C, \"Something is wrong\");\n" 10073 "};"); 10074 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 10075 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 10076 verifyFormat("#define A(a, b) (a && b)"); 10077 } 10078 10079 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 10080 verifyFormat("void f() {\n" 10081 " x[aaaaaaaaa -\n" 10082 " b] = 23;\n" 10083 "}", 10084 getLLVMStyleWithColumns(15)); 10085 } 10086 10087 TEST_F(FormatTest, FormatsCasts) { 10088 verifyFormat("Type *A = static_cast<Type *>(P);"); 10089 verifyFormat("Type *A = (Type *)P;"); 10090 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 10091 verifyFormat("int a = (int)(2.0f);"); 10092 verifyFormat("int a = (int)2.0f;"); 10093 verifyFormat("x[(int32)y];"); 10094 verifyFormat("x = (int32)y;"); 10095 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 10096 verifyFormat("int a = (int)*b;"); 10097 verifyFormat("int a = (int)2.0f;"); 10098 verifyFormat("int a = (int)~0;"); 10099 verifyFormat("int a = (int)++a;"); 10100 verifyFormat("int a = (int)sizeof(int);"); 10101 verifyFormat("int a = (int)+2;"); 10102 verifyFormat("my_int a = (my_int)2.0f;"); 10103 verifyFormat("my_int a = (my_int)sizeof(int);"); 10104 verifyFormat("return (my_int)aaa;"); 10105 verifyFormat("#define x ((int)-1)"); 10106 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 10107 verifyFormat("#define p(q) ((int *)&q)"); 10108 verifyFormat("fn(a)(b) + 1;"); 10109 10110 verifyFormat("void f() { my_int a = (my_int)*b; }"); 10111 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 10112 verifyFormat("my_int a = (my_int)~0;"); 10113 verifyFormat("my_int a = (my_int)++a;"); 10114 verifyFormat("my_int a = (my_int)-2;"); 10115 verifyFormat("my_int a = (my_int)1;"); 10116 verifyFormat("my_int a = (my_int *)1;"); 10117 verifyFormat("my_int a = (const my_int)-1;"); 10118 verifyFormat("my_int a = (const my_int *)-1;"); 10119 verifyFormat("my_int a = (my_int)(my_int)-1;"); 10120 verifyFormat("my_int a = (ns::my_int)-2;"); 10121 verifyFormat("case (my_int)ONE:"); 10122 verifyFormat("auto x = (X)this;"); 10123 // Casts in Obj-C style calls used to not be recognized as such. 10124 verifyFormat("int a = [(type*)[((type*)val) arg] arg];", getGoogleStyle()); 10125 10126 // FIXME: single value wrapped with paren will be treated as cast. 10127 verifyFormat("void f(int i = (kValue)*kMask) {}"); 10128 10129 verifyFormat("{ (void)F; }"); 10130 10131 // Don't break after a cast's 10132 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 10133 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 10134 " bbbbbbbbbbbbbbbbbbbbbb);"); 10135 10136 // These are not casts. 10137 verifyFormat("void f(int *) {}"); 10138 verifyFormat("f(foo)->b;"); 10139 verifyFormat("f(foo).b;"); 10140 verifyFormat("f(foo)(b);"); 10141 verifyFormat("f(foo)[b];"); 10142 verifyFormat("[](foo) { return 4; }(bar);"); 10143 verifyFormat("(*funptr)(foo)[4];"); 10144 verifyFormat("funptrs[4](foo)[4];"); 10145 verifyFormat("void f(int *);"); 10146 verifyFormat("void f(int *) = 0;"); 10147 verifyFormat("void f(SmallVector<int>) {}"); 10148 verifyFormat("void f(SmallVector<int>);"); 10149 verifyFormat("void f(SmallVector<int>) = 0;"); 10150 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 10151 verifyFormat("int a = sizeof(int) * b;"); 10152 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 10153 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 10154 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 10155 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 10156 10157 // These are not casts, but at some point were confused with casts. 10158 verifyFormat("virtual void foo(int *) override;"); 10159 verifyFormat("virtual void foo(char &) const;"); 10160 verifyFormat("virtual void foo(int *a, char *) const;"); 10161 verifyFormat("int a = sizeof(int *) + b;"); 10162 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 10163 verifyFormat("bool b = f(g<int>) && c;"); 10164 verifyFormat("typedef void (*f)(int i) func;"); 10165 verifyFormat("void operator++(int) noexcept;"); 10166 verifyFormat("void operator++(int &) noexcept;"); 10167 verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t " 10168 "&) noexcept;"); 10169 verifyFormat( 10170 "void operator delete(std::size_t, const std::nothrow_t &) noexcept;"); 10171 verifyFormat("void operator delete(const std::nothrow_t &) noexcept;"); 10172 verifyFormat("void operator delete(std::nothrow_t &) noexcept;"); 10173 verifyFormat("void operator delete(nothrow_t &) noexcept;"); 10174 verifyFormat("void operator delete(foo &) noexcept;"); 10175 verifyFormat("void operator delete(foo) noexcept;"); 10176 verifyFormat("void operator delete(int) noexcept;"); 10177 verifyFormat("void operator delete(int &) noexcept;"); 10178 verifyFormat("void operator delete(int &) volatile noexcept;"); 10179 verifyFormat("void operator delete(int &) const"); 10180 verifyFormat("void operator delete(int &) = default"); 10181 verifyFormat("void operator delete(int &) = delete"); 10182 verifyFormat("void operator delete(int &) [[noreturn]]"); 10183 verifyFormat("void operator delete(int &) throw();"); 10184 verifyFormat("void operator delete(int &) throw(int);"); 10185 verifyFormat("auto operator delete(int &) -> int;"); 10186 verifyFormat("auto operator delete(int &) override"); 10187 verifyFormat("auto operator delete(int &) final"); 10188 10189 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 10190 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 10191 // FIXME: The indentation here is not ideal. 10192 verifyFormat( 10193 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10194 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 10195 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 10196 } 10197 10198 TEST_F(FormatTest, FormatsFunctionTypes) { 10199 verifyFormat("A<bool()> a;"); 10200 verifyFormat("A<SomeType()> a;"); 10201 verifyFormat("A<void (*)(int, std::string)> a;"); 10202 verifyFormat("A<void *(int)>;"); 10203 verifyFormat("void *(*a)(int *, SomeType *);"); 10204 verifyFormat("int (*func)(void *);"); 10205 verifyFormat("void f() { int (*func)(void *); }"); 10206 verifyFormat("template <class CallbackClass>\n" 10207 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 10208 10209 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 10210 verifyGoogleFormat("void* (*a)(int);"); 10211 verifyGoogleFormat( 10212 "template <class CallbackClass>\n" 10213 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 10214 10215 // Other constructs can look somewhat like function types: 10216 verifyFormat("A<sizeof(*x)> a;"); 10217 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 10218 verifyFormat("some_var = function(*some_pointer_var)[0];"); 10219 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 10220 verifyFormat("int x = f(&h)();"); 10221 verifyFormat("returnsFunction(¶m1, ¶m2)(param);"); 10222 verifyFormat("std::function<\n" 10223 " LooooooooooongTemplatedType<\n" 10224 " SomeType>*(\n" 10225 " LooooooooooooooooongType type)>\n" 10226 " function;", 10227 getGoogleStyleWithColumns(40)); 10228 } 10229 10230 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 10231 verifyFormat("A (*foo_)[6];"); 10232 verifyFormat("vector<int> (*foo_)[6];"); 10233 } 10234 10235 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 10236 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10237 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 10238 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 10239 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 10240 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10241 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 10242 10243 // Different ways of ()-initializiation. 10244 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10245 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 10246 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10247 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 10248 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10249 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 10250 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10251 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 10252 10253 // Lambdas should not confuse the variable declaration heuristic. 10254 verifyFormat("LooooooooooooooooongType\n" 10255 " variable(nullptr, [](A *a) {});", 10256 getLLVMStyleWithColumns(40)); 10257 } 10258 10259 TEST_F(FormatTest, BreaksLongDeclarations) { 10260 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 10261 " AnotherNameForTheLongType;"); 10262 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 10263 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 10264 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10265 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 10266 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 10267 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 10268 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10269 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10270 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 10271 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10272 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 10273 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10274 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 10275 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10276 verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n" 10277 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10278 verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n" 10279 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10280 verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n" 10281 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10282 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10283 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);"); 10284 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10285 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}"); 10286 FormatStyle Indented = getLLVMStyle(); 10287 Indented.IndentWrappedFunctionNames = true; 10288 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10289 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 10290 Indented); 10291 verifyFormat( 10292 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10293 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 10294 Indented); 10295 verifyFormat( 10296 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 10297 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 10298 Indented); 10299 verifyFormat( 10300 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 10301 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 10302 Indented); 10303 10304 // FIXME: Without the comment, this breaks after "(". 10305 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 10306 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 10307 getGoogleStyle()); 10308 10309 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 10310 " int LoooooooooooooooooooongParam2) {}"); 10311 verifyFormat( 10312 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 10313 " SourceLocation L, IdentifierIn *II,\n" 10314 " Type *T) {}"); 10315 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 10316 "ReallyReaaallyLongFunctionName(\n" 10317 " const std::string &SomeParameter,\n" 10318 " const SomeType<string, SomeOtherTemplateParameter>\n" 10319 " &ReallyReallyLongParameterName,\n" 10320 " const SomeType<string, SomeOtherTemplateParameter>\n" 10321 " &AnotherLongParameterName) {}"); 10322 verifyFormat("template <typename A>\n" 10323 "SomeLoooooooooooooooooooooongType<\n" 10324 " typename some_namespace::SomeOtherType<A>::Type>\n" 10325 "Function() {}"); 10326 10327 verifyGoogleFormat( 10328 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 10329 " aaaaaaaaaaaaaaaaaaaaaaa;"); 10330 verifyGoogleFormat( 10331 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 10332 " SourceLocation L) {}"); 10333 verifyGoogleFormat( 10334 "some_namespace::LongReturnType\n" 10335 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 10336 " int first_long_parameter, int second_parameter) {}"); 10337 10338 verifyGoogleFormat("template <typename T>\n" 10339 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 10340 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 10341 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 10342 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 10343 10344 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 10345 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10346 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 10347 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10348 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 10349 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 10350 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10351 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 10352 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 10353 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 10354 10355 verifyFormat("template <typename T> // Templates on own line.\n" 10356 "static int // Some comment.\n" 10357 "MyFunction(int a);", 10358 getLLVMStyle()); 10359 } 10360 10361 TEST_F(FormatTest, FormatsAccessModifiers) { 10362 FormatStyle Style = getLLVMStyle(); 10363 EXPECT_EQ(Style.EmptyLineBeforeAccessModifier, 10364 FormatStyle::ELBAMS_LogicalBlock); 10365 verifyFormat("struct foo {\n" 10366 "private:\n" 10367 " void f() {}\n" 10368 "\n" 10369 "private:\n" 10370 " int i;\n" 10371 "\n" 10372 "protected:\n" 10373 " int j;\n" 10374 "};\n", 10375 Style); 10376 verifyFormat("struct foo {\n" 10377 "private:\n" 10378 " void f() {}\n" 10379 "\n" 10380 "private:\n" 10381 " int i;\n" 10382 "\n" 10383 "protected:\n" 10384 " int j;\n" 10385 "};\n", 10386 "struct foo {\n" 10387 "private:\n" 10388 " void f() {}\n" 10389 "private:\n" 10390 " int i;\n" 10391 "protected:\n" 10392 " int j;\n" 10393 "};\n", 10394 Style); 10395 verifyFormat("struct foo { /* comment */\n" 10396 "private:\n" 10397 " int i;\n" 10398 " // comment\n" 10399 "private:\n" 10400 " int j;\n" 10401 "};\n", 10402 Style); 10403 verifyFormat("struct foo {\n" 10404 "#ifdef FOO\n" 10405 "#endif\n" 10406 "private:\n" 10407 " int i;\n" 10408 "#ifdef FOO\n" 10409 "private:\n" 10410 "#endif\n" 10411 " int j;\n" 10412 "};\n", 10413 Style); 10414 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10415 verifyFormat("struct foo {\n" 10416 "private:\n" 10417 " void f() {}\n" 10418 "private:\n" 10419 " int i;\n" 10420 "protected:\n" 10421 " int j;\n" 10422 "};\n", 10423 Style); 10424 verifyFormat("struct foo {\n" 10425 "private:\n" 10426 " void f() {}\n" 10427 "private:\n" 10428 " int i;\n" 10429 "protected:\n" 10430 " int j;\n" 10431 "};\n", 10432 "struct foo {\n" 10433 "\n" 10434 "private:\n" 10435 " void f() {}\n" 10436 "\n" 10437 "private:\n" 10438 " int i;\n" 10439 "\n" 10440 "protected:\n" 10441 " int j;\n" 10442 "};\n", 10443 Style); 10444 verifyFormat("struct foo { /* comment */\n" 10445 "private:\n" 10446 " int i;\n" 10447 " // comment\n" 10448 "private:\n" 10449 " int j;\n" 10450 "};\n", 10451 "struct foo { /* comment */\n" 10452 "\n" 10453 "private:\n" 10454 " int i;\n" 10455 " // comment\n" 10456 "\n" 10457 "private:\n" 10458 " int j;\n" 10459 "};\n", 10460 Style); 10461 verifyFormat("struct foo {\n" 10462 "#ifdef FOO\n" 10463 "#endif\n" 10464 "private:\n" 10465 " int i;\n" 10466 "#ifdef FOO\n" 10467 "private:\n" 10468 "#endif\n" 10469 " int j;\n" 10470 "};\n", 10471 "struct foo {\n" 10472 "#ifdef FOO\n" 10473 "#endif\n" 10474 "\n" 10475 "private:\n" 10476 " int i;\n" 10477 "#ifdef FOO\n" 10478 "\n" 10479 "private:\n" 10480 "#endif\n" 10481 " int j;\n" 10482 "};\n", 10483 Style); 10484 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10485 verifyFormat("struct foo {\n" 10486 "private:\n" 10487 " void f() {}\n" 10488 "\n" 10489 "private:\n" 10490 " int i;\n" 10491 "\n" 10492 "protected:\n" 10493 " int j;\n" 10494 "};\n", 10495 Style); 10496 verifyFormat("struct foo {\n" 10497 "private:\n" 10498 " void f() {}\n" 10499 "\n" 10500 "private:\n" 10501 " int i;\n" 10502 "\n" 10503 "protected:\n" 10504 " int j;\n" 10505 "};\n", 10506 "struct foo {\n" 10507 "private:\n" 10508 " void f() {}\n" 10509 "private:\n" 10510 " int i;\n" 10511 "protected:\n" 10512 " int j;\n" 10513 "};\n", 10514 Style); 10515 verifyFormat("struct foo { /* comment */\n" 10516 "private:\n" 10517 " int i;\n" 10518 " // comment\n" 10519 "\n" 10520 "private:\n" 10521 " int j;\n" 10522 "};\n", 10523 "struct foo { /* comment */\n" 10524 "private:\n" 10525 " int i;\n" 10526 " // comment\n" 10527 "\n" 10528 "private:\n" 10529 " int j;\n" 10530 "};\n", 10531 Style); 10532 verifyFormat("struct foo {\n" 10533 "#ifdef FOO\n" 10534 "#endif\n" 10535 "\n" 10536 "private:\n" 10537 " int i;\n" 10538 "#ifdef FOO\n" 10539 "\n" 10540 "private:\n" 10541 "#endif\n" 10542 " int j;\n" 10543 "};\n", 10544 "struct foo {\n" 10545 "#ifdef FOO\n" 10546 "#endif\n" 10547 "private:\n" 10548 " int i;\n" 10549 "#ifdef FOO\n" 10550 "private:\n" 10551 "#endif\n" 10552 " int j;\n" 10553 "};\n", 10554 Style); 10555 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 10556 EXPECT_EQ("struct foo {\n" 10557 "\n" 10558 "private:\n" 10559 " void f() {}\n" 10560 "\n" 10561 "private:\n" 10562 " int i;\n" 10563 "\n" 10564 "protected:\n" 10565 " int j;\n" 10566 "};\n", 10567 format("struct foo {\n" 10568 "\n" 10569 "private:\n" 10570 " void f() {}\n" 10571 "\n" 10572 "private:\n" 10573 " int i;\n" 10574 "\n" 10575 "protected:\n" 10576 " int j;\n" 10577 "};\n", 10578 Style)); 10579 verifyFormat("struct foo {\n" 10580 "private:\n" 10581 " void f() {}\n" 10582 "private:\n" 10583 " int i;\n" 10584 "protected:\n" 10585 " int j;\n" 10586 "};\n", 10587 Style); 10588 EXPECT_EQ("struct foo { /* comment */\n" 10589 "\n" 10590 "private:\n" 10591 " int i;\n" 10592 " // comment\n" 10593 "\n" 10594 "private:\n" 10595 " int j;\n" 10596 "};\n", 10597 format("struct foo { /* comment */\n" 10598 "\n" 10599 "private:\n" 10600 " int i;\n" 10601 " // comment\n" 10602 "\n" 10603 "private:\n" 10604 " int j;\n" 10605 "};\n", 10606 Style)); 10607 verifyFormat("struct foo { /* comment */\n" 10608 "private:\n" 10609 " int i;\n" 10610 " // comment\n" 10611 "private:\n" 10612 " int j;\n" 10613 "};\n", 10614 Style); 10615 EXPECT_EQ("struct foo {\n" 10616 "#ifdef FOO\n" 10617 "#endif\n" 10618 "\n" 10619 "private:\n" 10620 " int i;\n" 10621 "#ifdef FOO\n" 10622 "\n" 10623 "private:\n" 10624 "#endif\n" 10625 " int j;\n" 10626 "};\n", 10627 format("struct foo {\n" 10628 "#ifdef FOO\n" 10629 "#endif\n" 10630 "\n" 10631 "private:\n" 10632 " int i;\n" 10633 "#ifdef FOO\n" 10634 "\n" 10635 "private:\n" 10636 "#endif\n" 10637 " int j;\n" 10638 "};\n", 10639 Style)); 10640 verifyFormat("struct foo {\n" 10641 "#ifdef FOO\n" 10642 "#endif\n" 10643 "private:\n" 10644 " int i;\n" 10645 "#ifdef FOO\n" 10646 "private:\n" 10647 "#endif\n" 10648 " int j;\n" 10649 "};\n", 10650 Style); 10651 10652 FormatStyle NoEmptyLines = getLLVMStyle(); 10653 NoEmptyLines.MaxEmptyLinesToKeep = 0; 10654 verifyFormat("struct foo {\n" 10655 "private:\n" 10656 " void f() {}\n" 10657 "\n" 10658 "private:\n" 10659 " int i;\n" 10660 "\n" 10661 "public:\n" 10662 "protected:\n" 10663 " int j;\n" 10664 "};\n", 10665 NoEmptyLines); 10666 10667 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10668 verifyFormat("struct foo {\n" 10669 "private:\n" 10670 " void f() {}\n" 10671 "private:\n" 10672 " int i;\n" 10673 "public:\n" 10674 "protected:\n" 10675 " int j;\n" 10676 "};\n", 10677 NoEmptyLines); 10678 10679 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10680 verifyFormat("struct foo {\n" 10681 "private:\n" 10682 " void f() {}\n" 10683 "\n" 10684 "private:\n" 10685 " int i;\n" 10686 "\n" 10687 "public:\n" 10688 "\n" 10689 "protected:\n" 10690 " int j;\n" 10691 "};\n", 10692 NoEmptyLines); 10693 } 10694 10695 TEST_F(FormatTest, FormatsAfterAccessModifiers) { 10696 10697 FormatStyle Style = getLLVMStyle(); 10698 EXPECT_EQ(Style.EmptyLineAfterAccessModifier, FormatStyle::ELAAMS_Never); 10699 verifyFormat("struct foo {\n" 10700 "private:\n" 10701 " void f() {}\n" 10702 "\n" 10703 "private:\n" 10704 " int i;\n" 10705 "\n" 10706 "protected:\n" 10707 " int j;\n" 10708 "};\n", 10709 Style); 10710 10711 // Check if lines are removed. 10712 verifyFormat("struct foo {\n" 10713 "private:\n" 10714 " void f() {}\n" 10715 "\n" 10716 "private:\n" 10717 " int i;\n" 10718 "\n" 10719 "protected:\n" 10720 " int j;\n" 10721 "};\n", 10722 "struct foo {\n" 10723 "private:\n" 10724 "\n" 10725 " void f() {}\n" 10726 "\n" 10727 "private:\n" 10728 "\n" 10729 " int i;\n" 10730 "\n" 10731 "protected:\n" 10732 "\n" 10733 " int j;\n" 10734 "};\n", 10735 Style); 10736 10737 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10738 verifyFormat("struct foo {\n" 10739 "private:\n" 10740 "\n" 10741 " void f() {}\n" 10742 "\n" 10743 "private:\n" 10744 "\n" 10745 " int i;\n" 10746 "\n" 10747 "protected:\n" 10748 "\n" 10749 " int j;\n" 10750 "};\n", 10751 Style); 10752 10753 // Check if lines are added. 10754 verifyFormat("struct foo {\n" 10755 "private:\n" 10756 "\n" 10757 " void f() {}\n" 10758 "\n" 10759 "private:\n" 10760 "\n" 10761 " int i;\n" 10762 "\n" 10763 "protected:\n" 10764 "\n" 10765 " int j;\n" 10766 "};\n", 10767 "struct foo {\n" 10768 "private:\n" 10769 " void f() {}\n" 10770 "\n" 10771 "private:\n" 10772 " int i;\n" 10773 "\n" 10774 "protected:\n" 10775 " int j;\n" 10776 "};\n", 10777 Style); 10778 10779 // Leave tests rely on the code layout, test::messUp can not be used. 10780 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 10781 Style.MaxEmptyLinesToKeep = 0u; 10782 verifyFormat("struct foo {\n" 10783 "private:\n" 10784 " void f() {}\n" 10785 "\n" 10786 "private:\n" 10787 " int i;\n" 10788 "\n" 10789 "protected:\n" 10790 " int j;\n" 10791 "};\n", 10792 Style); 10793 10794 // Check if MaxEmptyLinesToKeep is respected. 10795 EXPECT_EQ("struct foo {\n" 10796 "private:\n" 10797 " void f() {}\n" 10798 "\n" 10799 "private:\n" 10800 " int i;\n" 10801 "\n" 10802 "protected:\n" 10803 " int j;\n" 10804 "};\n", 10805 format("struct foo {\n" 10806 "private:\n" 10807 "\n\n\n" 10808 " void f() {}\n" 10809 "\n" 10810 "private:\n" 10811 "\n\n\n" 10812 " int i;\n" 10813 "\n" 10814 "protected:\n" 10815 "\n\n\n" 10816 " int j;\n" 10817 "};\n", 10818 Style)); 10819 10820 Style.MaxEmptyLinesToKeep = 1u; 10821 EXPECT_EQ("struct foo {\n" 10822 "private:\n" 10823 "\n" 10824 " void f() {}\n" 10825 "\n" 10826 "private:\n" 10827 "\n" 10828 " int i;\n" 10829 "\n" 10830 "protected:\n" 10831 "\n" 10832 " int j;\n" 10833 "};\n", 10834 format("struct foo {\n" 10835 "private:\n" 10836 "\n" 10837 " void f() {}\n" 10838 "\n" 10839 "private:\n" 10840 "\n" 10841 " int i;\n" 10842 "\n" 10843 "protected:\n" 10844 "\n" 10845 " int j;\n" 10846 "};\n", 10847 Style)); 10848 // Check if no lines are kept. 10849 EXPECT_EQ("struct foo {\n" 10850 "private:\n" 10851 " void f() {}\n" 10852 "\n" 10853 "private:\n" 10854 " int i;\n" 10855 "\n" 10856 "protected:\n" 10857 " int j;\n" 10858 "};\n", 10859 format("struct foo {\n" 10860 "private:\n" 10861 " void f() {}\n" 10862 "\n" 10863 "private:\n" 10864 " int i;\n" 10865 "\n" 10866 "protected:\n" 10867 " int j;\n" 10868 "};\n", 10869 Style)); 10870 // Check if MaxEmptyLinesToKeep is respected. 10871 EXPECT_EQ("struct foo {\n" 10872 "private:\n" 10873 "\n" 10874 " void f() {}\n" 10875 "\n" 10876 "private:\n" 10877 "\n" 10878 " int i;\n" 10879 "\n" 10880 "protected:\n" 10881 "\n" 10882 " int j;\n" 10883 "};\n", 10884 format("struct foo {\n" 10885 "private:\n" 10886 "\n\n\n" 10887 " void f() {}\n" 10888 "\n" 10889 "private:\n" 10890 "\n\n\n" 10891 " int i;\n" 10892 "\n" 10893 "protected:\n" 10894 "\n\n\n" 10895 " int j;\n" 10896 "};\n", 10897 Style)); 10898 10899 Style.MaxEmptyLinesToKeep = 10u; 10900 EXPECT_EQ("struct foo {\n" 10901 "private:\n" 10902 "\n\n\n" 10903 " void f() {}\n" 10904 "\n" 10905 "private:\n" 10906 "\n\n\n" 10907 " int i;\n" 10908 "\n" 10909 "protected:\n" 10910 "\n\n\n" 10911 " int j;\n" 10912 "};\n", 10913 format("struct foo {\n" 10914 "private:\n" 10915 "\n\n\n" 10916 " void f() {}\n" 10917 "\n" 10918 "private:\n" 10919 "\n\n\n" 10920 " int i;\n" 10921 "\n" 10922 "protected:\n" 10923 "\n\n\n" 10924 " int j;\n" 10925 "};\n", 10926 Style)); 10927 10928 // Test with comments. 10929 Style = getLLVMStyle(); 10930 verifyFormat("struct foo {\n" 10931 "private:\n" 10932 " // comment\n" 10933 " void f() {}\n" 10934 "\n" 10935 "private: /* comment */\n" 10936 " int i;\n" 10937 "};\n", 10938 Style); 10939 verifyFormat("struct foo {\n" 10940 "private:\n" 10941 " // comment\n" 10942 " void f() {}\n" 10943 "\n" 10944 "private: /* comment */\n" 10945 " int i;\n" 10946 "};\n", 10947 "struct foo {\n" 10948 "private:\n" 10949 "\n" 10950 " // comment\n" 10951 " void f() {}\n" 10952 "\n" 10953 "private: /* comment */\n" 10954 "\n" 10955 " int i;\n" 10956 "};\n", 10957 Style); 10958 10959 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10960 verifyFormat("struct foo {\n" 10961 "private:\n" 10962 "\n" 10963 " // comment\n" 10964 " void f() {}\n" 10965 "\n" 10966 "private: /* comment */\n" 10967 "\n" 10968 " int i;\n" 10969 "};\n", 10970 "struct foo {\n" 10971 "private:\n" 10972 " // comment\n" 10973 " void f() {}\n" 10974 "\n" 10975 "private: /* comment */\n" 10976 " int i;\n" 10977 "};\n", 10978 Style); 10979 verifyFormat("struct foo {\n" 10980 "private:\n" 10981 "\n" 10982 " // comment\n" 10983 " void f() {}\n" 10984 "\n" 10985 "private: /* comment */\n" 10986 "\n" 10987 " int i;\n" 10988 "};\n", 10989 Style); 10990 10991 // Test with preprocessor defines. 10992 Style = getLLVMStyle(); 10993 verifyFormat("struct foo {\n" 10994 "private:\n" 10995 "#ifdef FOO\n" 10996 "#endif\n" 10997 " void f() {}\n" 10998 "};\n", 10999 Style); 11000 verifyFormat("struct foo {\n" 11001 "private:\n" 11002 "#ifdef FOO\n" 11003 "#endif\n" 11004 " void f() {}\n" 11005 "};\n", 11006 "struct foo {\n" 11007 "private:\n" 11008 "\n" 11009 "#ifdef FOO\n" 11010 "#endif\n" 11011 " void f() {}\n" 11012 "};\n", 11013 Style); 11014 11015 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11016 verifyFormat("struct foo {\n" 11017 "private:\n" 11018 "\n" 11019 "#ifdef FOO\n" 11020 "#endif\n" 11021 " void f() {}\n" 11022 "};\n", 11023 "struct foo {\n" 11024 "private:\n" 11025 "#ifdef FOO\n" 11026 "#endif\n" 11027 " void f() {}\n" 11028 "};\n", 11029 Style); 11030 verifyFormat("struct foo {\n" 11031 "private:\n" 11032 "\n" 11033 "#ifdef FOO\n" 11034 "#endif\n" 11035 " void f() {}\n" 11036 "};\n", 11037 Style); 11038 } 11039 11040 TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) { 11041 // Combined tests of EmptyLineAfterAccessModifier and 11042 // EmptyLineBeforeAccessModifier. 11043 FormatStyle Style = getLLVMStyle(); 11044 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 11045 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11046 verifyFormat("struct foo {\n" 11047 "private:\n" 11048 "\n" 11049 "protected:\n" 11050 "};\n", 11051 Style); 11052 11053 Style.MaxEmptyLinesToKeep = 10u; 11054 // Both remove all new lines. 11055 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 11056 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 11057 verifyFormat("struct foo {\n" 11058 "private:\n" 11059 "protected:\n" 11060 "};\n", 11061 "struct foo {\n" 11062 "private:\n" 11063 "\n\n\n" 11064 "protected:\n" 11065 "};\n", 11066 Style); 11067 11068 // Leave tests rely on the code layout, test::messUp can not be used. 11069 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 11070 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 11071 Style.MaxEmptyLinesToKeep = 10u; 11072 EXPECT_EQ("struct foo {\n" 11073 "private:\n" 11074 "\n\n\n" 11075 "protected:\n" 11076 "};\n", 11077 format("struct foo {\n" 11078 "private:\n" 11079 "\n\n\n" 11080 "protected:\n" 11081 "};\n", 11082 Style)); 11083 Style.MaxEmptyLinesToKeep = 3u; 11084 EXPECT_EQ("struct foo {\n" 11085 "private:\n" 11086 "\n\n\n" 11087 "protected:\n" 11088 "};\n", 11089 format("struct foo {\n" 11090 "private:\n" 11091 "\n\n\n" 11092 "protected:\n" 11093 "};\n", 11094 Style)); 11095 Style.MaxEmptyLinesToKeep = 1u; 11096 EXPECT_EQ("struct foo {\n" 11097 "private:\n" 11098 "\n\n\n" 11099 "protected:\n" 11100 "};\n", 11101 format("struct foo {\n" 11102 "private:\n" 11103 "\n\n\n" 11104 "protected:\n" 11105 "};\n", 11106 Style)); // Based on new lines in original document and not 11107 // on the setting. 11108 11109 Style.MaxEmptyLinesToKeep = 10u; 11110 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 11111 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 11112 // Newlines are kept if they are greater than zero, 11113 // test::messUp removes all new lines which changes the logic 11114 EXPECT_EQ("struct foo {\n" 11115 "private:\n" 11116 "\n\n\n" 11117 "protected:\n" 11118 "};\n", 11119 format("struct foo {\n" 11120 "private:\n" 11121 "\n\n\n" 11122 "protected:\n" 11123 "};\n", 11124 Style)); 11125 11126 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 11127 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11128 // test::messUp removes all new lines which changes the logic 11129 EXPECT_EQ("struct foo {\n" 11130 "private:\n" 11131 "\n\n\n" 11132 "protected:\n" 11133 "};\n", 11134 format("struct foo {\n" 11135 "private:\n" 11136 "\n\n\n" 11137 "protected:\n" 11138 "};\n", 11139 Style)); 11140 11141 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 11142 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 11143 EXPECT_EQ("struct foo {\n" 11144 "private:\n" 11145 "\n\n\n" 11146 "protected:\n" 11147 "};\n", 11148 format("struct foo {\n" 11149 "private:\n" 11150 "\n\n\n" 11151 "protected:\n" 11152 "};\n", 11153 Style)); // test::messUp removes all new lines which changes 11154 // the logic. 11155 11156 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 11157 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 11158 verifyFormat("struct foo {\n" 11159 "private:\n" 11160 "protected:\n" 11161 "};\n", 11162 "struct foo {\n" 11163 "private:\n" 11164 "\n\n\n" 11165 "protected:\n" 11166 "};\n", 11167 Style); 11168 11169 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 11170 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 11171 EXPECT_EQ("struct foo {\n" 11172 "private:\n" 11173 "\n\n\n" 11174 "protected:\n" 11175 "};\n", 11176 format("struct foo {\n" 11177 "private:\n" 11178 "\n\n\n" 11179 "protected:\n" 11180 "};\n", 11181 Style)); // test::messUp removes all new lines which changes 11182 // the logic. 11183 11184 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 11185 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11186 verifyFormat("struct foo {\n" 11187 "private:\n" 11188 "protected:\n" 11189 "};\n", 11190 "struct foo {\n" 11191 "private:\n" 11192 "\n\n\n" 11193 "protected:\n" 11194 "};\n", 11195 Style); 11196 11197 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 11198 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11199 verifyFormat("struct foo {\n" 11200 "private:\n" 11201 "protected:\n" 11202 "};\n", 11203 "struct foo {\n" 11204 "private:\n" 11205 "\n\n\n" 11206 "protected:\n" 11207 "};\n", 11208 Style); 11209 11210 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 11211 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 11212 verifyFormat("struct foo {\n" 11213 "private:\n" 11214 "protected:\n" 11215 "};\n", 11216 "struct foo {\n" 11217 "private:\n" 11218 "\n\n\n" 11219 "protected:\n" 11220 "};\n", 11221 Style); 11222 11223 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 11224 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 11225 verifyFormat("struct foo {\n" 11226 "private:\n" 11227 "protected:\n" 11228 "};\n", 11229 "struct foo {\n" 11230 "private:\n" 11231 "\n\n\n" 11232 "protected:\n" 11233 "};\n", 11234 Style); 11235 } 11236 11237 TEST_F(FormatTest, FormatsArrays) { 11238 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 11239 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 11240 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 11241 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 11242 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 11243 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 11244 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11245 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 11246 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11247 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 11248 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11249 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 11250 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 11251 verifyFormat( 11252 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 11253 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 11254 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 11255 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" 11256 " .aaaaaaaaaaaaaaaaaaaaaa();"); 11257 11258 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 11259 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 11260 verifyFormat( 11261 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 11262 " .aaaaaaa[0]\n" 11263 " .aaaaaaaaaaaaaaaaaaaaaa();"); 11264 verifyFormat("a[::b::c];"); 11265 11266 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 11267 11268 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 11269 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 11270 } 11271 11272 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 11273 verifyFormat("(a)->b();"); 11274 verifyFormat("--a;"); 11275 } 11276 11277 TEST_F(FormatTest, HandlesIncludeDirectives) { 11278 verifyFormat("#include <string>\n" 11279 "#include <a/b/c.h>\n" 11280 "#include \"a/b/string\"\n" 11281 "#include \"string.h\"\n" 11282 "#include \"string.h\"\n" 11283 "#include <a-a>\n" 11284 "#include < path with space >\n" 11285 "#include_next <test.h>" 11286 "#include \"abc.h\" // this is included for ABC\n" 11287 "#include \"some long include\" // with a comment\n" 11288 "#include \"some very long include path\"\n" 11289 "#include <some/very/long/include/path>\n", 11290 getLLVMStyleWithColumns(35)); 11291 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 11292 EXPECT_EQ("#include <a>", format("#include<a>")); 11293 11294 verifyFormat("#import <string>"); 11295 verifyFormat("#import <a/b/c.h>"); 11296 verifyFormat("#import \"a/b/string\""); 11297 verifyFormat("#import \"string.h\""); 11298 verifyFormat("#import \"string.h\""); 11299 verifyFormat("#if __has_include(<strstream>)\n" 11300 "#include <strstream>\n" 11301 "#endif"); 11302 11303 verifyFormat("#define MY_IMPORT <a/b>"); 11304 11305 verifyFormat("#if __has_include(<a/b>)"); 11306 verifyFormat("#if __has_include_next(<a/b>)"); 11307 verifyFormat("#define F __has_include(<a/b>)"); 11308 verifyFormat("#define F __has_include_next(<a/b>)"); 11309 11310 // Protocol buffer definition or missing "#". 11311 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 11312 getLLVMStyleWithColumns(30)); 11313 11314 FormatStyle Style = getLLVMStyle(); 11315 Style.AlwaysBreakBeforeMultilineStrings = true; 11316 Style.ColumnLimit = 0; 11317 verifyFormat("#import \"abc.h\"", Style); 11318 11319 // But 'import' might also be a regular C++ namespace. 11320 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11321 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 11322 } 11323 11324 //===----------------------------------------------------------------------===// 11325 // Error recovery tests. 11326 //===----------------------------------------------------------------------===// 11327 11328 TEST_F(FormatTest, IncompleteParameterLists) { 11329 FormatStyle NoBinPacking = getLLVMStyle(); 11330 NoBinPacking.BinPackParameters = false; 11331 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 11332 " double *min_x,\n" 11333 " double *max_x,\n" 11334 " double *min_y,\n" 11335 " double *max_y,\n" 11336 " double *min_z,\n" 11337 " double *max_z, ) {}", 11338 NoBinPacking); 11339 } 11340 11341 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 11342 verifyFormat("void f() { return; }\n42"); 11343 verifyFormat("void f() {\n" 11344 " if (0)\n" 11345 " return;\n" 11346 "}\n" 11347 "42"); 11348 verifyFormat("void f() { return }\n42"); 11349 verifyFormat("void f() {\n" 11350 " if (0)\n" 11351 " return\n" 11352 "}\n" 11353 "42"); 11354 } 11355 11356 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 11357 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 11358 EXPECT_EQ("void f() {\n" 11359 " if (a)\n" 11360 " return\n" 11361 "}", 11362 format("void f ( ) { if ( a ) return }")); 11363 EXPECT_EQ("namespace N {\n" 11364 "void f()\n" 11365 "}", 11366 format("namespace N { void f() }")); 11367 EXPECT_EQ("namespace N {\n" 11368 "void f() {}\n" 11369 "void g()\n" 11370 "} // namespace N", 11371 format("namespace N { void f( ) { } void g( ) }")); 11372 } 11373 11374 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 11375 verifyFormat("int aaaaaaaa =\n" 11376 " // Overlylongcomment\n" 11377 " b;", 11378 getLLVMStyleWithColumns(20)); 11379 verifyFormat("function(\n" 11380 " ShortArgument,\n" 11381 " LoooooooooooongArgument);\n", 11382 getLLVMStyleWithColumns(20)); 11383 } 11384 11385 TEST_F(FormatTest, IncorrectAccessSpecifier) { 11386 verifyFormat("public:"); 11387 verifyFormat("class A {\n" 11388 "public\n" 11389 " void f() {}\n" 11390 "};"); 11391 verifyFormat("public\n" 11392 "int qwerty;"); 11393 verifyFormat("public\n" 11394 "B {}"); 11395 verifyFormat("public\n" 11396 "{}"); 11397 verifyFormat("public\n" 11398 "B { int x; }"); 11399 } 11400 11401 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 11402 verifyFormat("{"); 11403 verifyFormat("#})"); 11404 verifyNoCrash("(/**/[:!] ?[)."); 11405 } 11406 11407 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) { 11408 // Found by oss-fuzz: 11409 // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212 11410 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 11411 Style.ColumnLimit = 60; 11412 verifyNoCrash( 11413 "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20" 11414 "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20" 11415 "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a", 11416 Style); 11417 } 11418 11419 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 11420 verifyFormat("do {\n}"); 11421 verifyFormat("do {\n}\n" 11422 "f();"); 11423 verifyFormat("do {\n}\n" 11424 "wheeee(fun);"); 11425 verifyFormat("do {\n" 11426 " f();\n" 11427 "}"); 11428 } 11429 11430 TEST_F(FormatTest, IncorrectCodeMissingParens) { 11431 verifyFormat("if {\n foo;\n foo();\n}"); 11432 verifyFormat("switch {\n foo;\n foo();\n}"); 11433 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 11434 verifyFormat("while {\n foo;\n foo();\n}"); 11435 verifyFormat("do {\n foo;\n foo();\n} while;"); 11436 } 11437 11438 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 11439 verifyIncompleteFormat("namespace {\n" 11440 "class Foo { Foo (\n" 11441 "};\n" 11442 "} // namespace"); 11443 } 11444 11445 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 11446 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 11447 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 11448 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 11449 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 11450 11451 EXPECT_EQ("{\n" 11452 " {\n" 11453 " breakme(\n" 11454 " qwe);\n" 11455 " }\n", 11456 format("{\n" 11457 " {\n" 11458 " breakme(qwe);\n" 11459 "}\n", 11460 getLLVMStyleWithColumns(10))); 11461 } 11462 11463 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 11464 verifyFormat("int x = {\n" 11465 " avariable,\n" 11466 " b(alongervariable)};", 11467 getLLVMStyleWithColumns(25)); 11468 } 11469 11470 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 11471 verifyFormat("return (a)(b){1, 2, 3};"); 11472 } 11473 11474 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 11475 verifyFormat("vector<int> x{1, 2, 3, 4};"); 11476 verifyFormat("vector<int> x{\n" 11477 " 1,\n" 11478 " 2,\n" 11479 " 3,\n" 11480 " 4,\n" 11481 "};"); 11482 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 11483 verifyFormat("f({1, 2});"); 11484 verifyFormat("auto v = Foo{-1};"); 11485 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 11486 verifyFormat("Class::Class : member{1, 2, 3} {}"); 11487 verifyFormat("new vector<int>{1, 2, 3};"); 11488 verifyFormat("new int[3]{1, 2, 3};"); 11489 verifyFormat("new int{1};"); 11490 verifyFormat("return {arg1, arg2};"); 11491 verifyFormat("return {arg1, SomeType{parameter}};"); 11492 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 11493 verifyFormat("new T{arg1, arg2};"); 11494 verifyFormat("f(MyMap[{composite, key}]);"); 11495 verifyFormat("class Class {\n" 11496 " T member = {arg1, arg2};\n" 11497 "};"); 11498 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 11499 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 11500 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 11501 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 11502 verifyFormat("int a = std::is_integral<int>{} + 0;"); 11503 11504 verifyFormat("int foo(int i) { return fo1{}(i); }"); 11505 verifyFormat("int foo(int i) { return fo1{}(i); }"); 11506 verifyFormat("auto i = decltype(x){};"); 11507 verifyFormat("auto i = typeof(x){};"); 11508 verifyFormat("auto i = _Atomic(x){};"); 11509 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 11510 verifyFormat("Node n{1, Node{1000}, //\n" 11511 " 2};"); 11512 verifyFormat("Aaaa aaaaaaa{\n" 11513 " {\n" 11514 " aaaa,\n" 11515 " },\n" 11516 "};"); 11517 verifyFormat("class C : public D {\n" 11518 " SomeClass SC{2};\n" 11519 "};"); 11520 verifyFormat("class C : public A {\n" 11521 " class D : public B {\n" 11522 " void f() { int i{2}; }\n" 11523 " };\n" 11524 "};"); 11525 verifyFormat("#define A {a, a},"); 11526 // Don't confuse braced list initializers with compound statements. 11527 verifyFormat( 11528 "class A {\n" 11529 " A() : a{} {}\n" 11530 " A(int b) : b(b) {}\n" 11531 " A(int a, int b) : a(a), bs{{bs...}} { f(); }\n" 11532 " int a, b;\n" 11533 " explicit Expr(const Scalar<Result> &x) : u{Constant<Result>{x}} {}\n" 11534 " explicit Expr(Scalar<Result> &&x) : u{Constant<Result>{std::move(x)}} " 11535 "{}\n" 11536 "};"); 11537 11538 // Avoid breaking between equal sign and opening brace 11539 FormatStyle AvoidBreakingFirstArgument = getLLVMStyle(); 11540 AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200; 11541 verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n" 11542 " {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n" 11543 " {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n" 11544 " {\"ccccccccccccccccccccc\", 2}};", 11545 AvoidBreakingFirstArgument); 11546 11547 // Binpacking only if there is no trailing comma 11548 verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n" 11549 " cccccccccc, dddddddddd};", 11550 getLLVMStyleWithColumns(50)); 11551 verifyFormat("const Aaaaaa aaaaa = {\n" 11552 " aaaaaaaaaaa,\n" 11553 " bbbbbbbbbbb,\n" 11554 " ccccccccccc,\n" 11555 " ddddddddddd,\n" 11556 "};", 11557 getLLVMStyleWithColumns(50)); 11558 11559 // Cases where distinguising braced lists and blocks is hard. 11560 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);"); 11561 verifyFormat("void f() {\n" 11562 " return; // comment\n" 11563 "}\n" 11564 "SomeType t;"); 11565 verifyFormat("void f() {\n" 11566 " if (a) {\n" 11567 " f();\n" 11568 " }\n" 11569 "}\n" 11570 "SomeType t;"); 11571 11572 // In combination with BinPackArguments = false. 11573 FormatStyle NoBinPacking = getLLVMStyle(); 11574 NoBinPacking.BinPackArguments = false; 11575 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 11576 " bbbbb,\n" 11577 " ccccc,\n" 11578 " ddddd,\n" 11579 " eeeee,\n" 11580 " ffffff,\n" 11581 " ggggg,\n" 11582 " hhhhhh,\n" 11583 " iiiiii,\n" 11584 " jjjjjj,\n" 11585 " kkkkkk};", 11586 NoBinPacking); 11587 verifyFormat("const Aaaaaa aaaaa = {\n" 11588 " aaaaa,\n" 11589 " bbbbb,\n" 11590 " ccccc,\n" 11591 " ddddd,\n" 11592 " eeeee,\n" 11593 " ffffff,\n" 11594 " ggggg,\n" 11595 " hhhhhh,\n" 11596 " iiiiii,\n" 11597 " jjjjjj,\n" 11598 " kkkkkk,\n" 11599 "};", 11600 NoBinPacking); 11601 verifyFormat( 11602 "const Aaaaaa aaaaa = {\n" 11603 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 11604 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 11605 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 11606 "};", 11607 NoBinPacking); 11608 11609 NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 11610 EXPECT_EQ("static uint8 CddDp83848Reg[] = {\n" 11611 " CDDDP83848_BMCR_REGISTER,\n" 11612 " CDDDP83848_BMSR_REGISTER,\n" 11613 " CDDDP83848_RBR_REGISTER};", 11614 format("static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n" 11615 " CDDDP83848_BMSR_REGISTER,\n" 11616 " CDDDP83848_RBR_REGISTER};", 11617 NoBinPacking)); 11618 11619 // FIXME: The alignment of these trailing comments might be bad. Then again, 11620 // this might be utterly useless in real code. 11621 verifyFormat("Constructor::Constructor()\n" 11622 " : some_value{ //\n" 11623 " aaaaaaa, //\n" 11624 " bbbbbbb} {}"); 11625 11626 // In braced lists, the first comment is always assumed to belong to the 11627 // first element. Thus, it can be moved to the next or previous line as 11628 // appropriate. 11629 EXPECT_EQ("function({// First element:\n" 11630 " 1,\n" 11631 " // Second element:\n" 11632 " 2});", 11633 format("function({\n" 11634 " // First element:\n" 11635 " 1,\n" 11636 " // Second element:\n" 11637 " 2});")); 11638 EXPECT_EQ("std::vector<int> MyNumbers{\n" 11639 " // First element:\n" 11640 " 1,\n" 11641 " // Second element:\n" 11642 " 2};", 11643 format("std::vector<int> MyNumbers{// First element:\n" 11644 " 1,\n" 11645 " // Second element:\n" 11646 " 2};", 11647 getLLVMStyleWithColumns(30))); 11648 // A trailing comma should still lead to an enforced line break and no 11649 // binpacking. 11650 EXPECT_EQ("vector<int> SomeVector = {\n" 11651 " // aaa\n" 11652 " 1,\n" 11653 " 2,\n" 11654 "};", 11655 format("vector<int> SomeVector = { // aaa\n" 11656 " 1, 2, };")); 11657 11658 // C++11 brace initializer list l-braces should not be treated any differently 11659 // when breaking before lambda bodies is enabled 11660 FormatStyle BreakBeforeLambdaBody = getLLVMStyle(); 11661 BreakBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 11662 BreakBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 11663 BreakBeforeLambdaBody.AlwaysBreakBeforeMultilineStrings = true; 11664 verifyFormat( 11665 "std::runtime_error{\n" 11666 " \"Long string which will force a break onto the next line...\"};", 11667 BreakBeforeLambdaBody); 11668 11669 FormatStyle ExtraSpaces = getLLVMStyle(); 11670 ExtraSpaces.Cpp11BracedListStyle = false; 11671 ExtraSpaces.ColumnLimit = 75; 11672 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 11673 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 11674 verifyFormat("f({ 1, 2 });", ExtraSpaces); 11675 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 11676 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 11677 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 11678 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 11679 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 11680 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 11681 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 11682 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 11683 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 11684 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 11685 verifyFormat("class Class {\n" 11686 " T member = { arg1, arg2 };\n" 11687 "};", 11688 ExtraSpaces); 11689 verifyFormat( 11690 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11691 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 11692 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 11693 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 11694 ExtraSpaces); 11695 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 11696 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 11697 ExtraSpaces); 11698 verifyFormat( 11699 "someFunction(OtherParam,\n" 11700 " BracedList{ // comment 1 (Forcing interesting break)\n" 11701 " param1, param2,\n" 11702 " // comment 2\n" 11703 " param3, param4 });", 11704 ExtraSpaces); 11705 verifyFormat( 11706 "std::this_thread::sleep_for(\n" 11707 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 11708 ExtraSpaces); 11709 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n" 11710 " aaaaaaa,\n" 11711 " aaaaaaaaaa,\n" 11712 " aaaaa,\n" 11713 " aaaaaaaaaaaaaaa,\n" 11714 " aaa,\n" 11715 " aaaaaaaaaa,\n" 11716 " a,\n" 11717 " aaaaaaaaaaaaaaaaaaaaa,\n" 11718 " aaaaaaaaaaaa,\n" 11719 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 11720 " aaaaaaa,\n" 11721 " a};"); 11722 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 11723 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces); 11724 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces); 11725 11726 // Avoid breaking between initializer/equal sign and opening brace 11727 ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200; 11728 verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n" 11729 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 11730 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 11731 " { \"ccccccccccccccccccccc\", 2 }\n" 11732 "};", 11733 ExtraSpaces); 11734 verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n" 11735 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 11736 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 11737 " { \"ccccccccccccccccccccc\", 2 }\n" 11738 "};", 11739 ExtraSpaces); 11740 11741 FormatStyle SpaceBeforeBrace = getLLVMStyle(); 11742 SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true; 11743 verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace); 11744 verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace); 11745 11746 FormatStyle SpaceBetweenBraces = getLLVMStyle(); 11747 SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always; 11748 SpaceBetweenBraces.SpacesInParentheses = true; 11749 SpaceBetweenBraces.SpacesInSquareBrackets = true; 11750 verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces); 11751 verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces); 11752 verifyFormat("vector< int > x{ // comment 1\n" 11753 " 1, 2, 3, 4 };", 11754 SpaceBetweenBraces); 11755 SpaceBetweenBraces.ColumnLimit = 20; 11756 EXPECT_EQ("vector< int > x{\n" 11757 " 1, 2, 3, 4 };", 11758 format("vector<int>x{1,2,3,4};", SpaceBetweenBraces)); 11759 SpaceBetweenBraces.ColumnLimit = 24; 11760 EXPECT_EQ("vector< int > x{ 1, 2,\n" 11761 " 3, 4 };", 11762 format("vector<int>x{1,2,3,4};", SpaceBetweenBraces)); 11763 EXPECT_EQ("vector< int > x{\n" 11764 " 1,\n" 11765 " 2,\n" 11766 " 3,\n" 11767 " 4,\n" 11768 "};", 11769 format("vector<int>x{1,2,3,4,};", SpaceBetweenBraces)); 11770 verifyFormat("vector< int > x{};", SpaceBetweenBraces); 11771 SpaceBetweenBraces.SpaceInEmptyParentheses = true; 11772 verifyFormat("vector< int > x{ };", SpaceBetweenBraces); 11773 } 11774 11775 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 11776 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11777 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11778 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11779 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11780 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11781 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 11782 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 11783 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11784 " 1, 22, 333, 4444, 55555, //\n" 11785 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11786 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 11787 verifyFormat( 11788 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11789 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11790 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 11791 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 11792 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 11793 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 11794 " 7777777};"); 11795 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 11796 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 11797 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 11798 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 11799 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 11800 " // Separating comment.\n" 11801 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 11802 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 11803 " // Leading comment\n" 11804 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 11805 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 11806 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 11807 " 1, 1, 1, 1};", 11808 getLLVMStyleWithColumns(39)); 11809 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 11810 " 1, 1, 1, 1};", 11811 getLLVMStyleWithColumns(38)); 11812 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 11813 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 11814 getLLVMStyleWithColumns(43)); 11815 verifyFormat( 11816 "static unsigned SomeValues[10][3] = {\n" 11817 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 11818 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 11819 verifyFormat("static auto fields = new vector<string>{\n" 11820 " \"aaaaaaaaaaaaa\",\n" 11821 " \"aaaaaaaaaaaaa\",\n" 11822 " \"aaaaaaaaaaaa\",\n" 11823 " \"aaaaaaaaaaaaaa\",\n" 11824 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 11825 " \"aaaaaaaaaaaa\",\n" 11826 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 11827 "};"); 11828 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 11829 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 11830 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 11831 " 3, cccccccccccccccccccccc};", 11832 getLLVMStyleWithColumns(60)); 11833 11834 // Trailing commas. 11835 verifyFormat("vector<int> x = {\n" 11836 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 11837 "};", 11838 getLLVMStyleWithColumns(39)); 11839 verifyFormat("vector<int> x = {\n" 11840 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 11841 "};", 11842 getLLVMStyleWithColumns(39)); 11843 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 11844 " 1, 1, 1, 1,\n" 11845 " /**/ /**/};", 11846 getLLVMStyleWithColumns(39)); 11847 11848 // Trailing comment in the first line. 11849 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 11850 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 11851 " 111111111, 222222222, 3333333333, 444444444, //\n" 11852 " 11111111, 22222222, 333333333, 44444444};"); 11853 // Trailing comment in the last line. 11854 verifyFormat("int aaaaa[] = {\n" 11855 " 1, 2, 3, // comment\n" 11856 " 4, 5, 6 // comment\n" 11857 "};"); 11858 11859 // With nested lists, we should either format one item per line or all nested 11860 // lists one on line. 11861 // FIXME: For some nested lists, we can do better. 11862 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 11863 " {aaaaaaaaaaaaaaaaaaa},\n" 11864 " {aaaaaaaaaaaaaaaaaaaaa},\n" 11865 " {aaaaaaaaaaaaaaaaa}};", 11866 getLLVMStyleWithColumns(60)); 11867 verifyFormat( 11868 "SomeStruct my_struct_array = {\n" 11869 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 11870 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 11871 " {aaa, aaa},\n" 11872 " {aaa, aaa},\n" 11873 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 11874 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 11875 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 11876 11877 // No column layout should be used here. 11878 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 11879 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 11880 11881 verifyNoCrash("a<,"); 11882 11883 // No braced initializer here. 11884 verifyFormat("void f() {\n" 11885 " struct Dummy {};\n" 11886 " f(v);\n" 11887 "}"); 11888 11889 // Long lists should be formatted in columns even if they are nested. 11890 verifyFormat( 11891 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11892 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11893 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11894 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11895 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11896 " 1, 22, 333, 4444, 55555, 666666, 7777777});"); 11897 11898 // Allow "single-column" layout even if that violates the column limit. There 11899 // isn't going to be a better way. 11900 verifyFormat("std::vector<int> a = {\n" 11901 " aaaaaaaa,\n" 11902 " aaaaaaaa,\n" 11903 " aaaaaaaa,\n" 11904 " aaaaaaaa,\n" 11905 " aaaaaaaaaa,\n" 11906 " aaaaaaaa,\n" 11907 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};", 11908 getLLVMStyleWithColumns(30)); 11909 verifyFormat("vector<int> aaaa = {\n" 11910 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11911 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11912 " aaaaaa.aaaaaaa,\n" 11913 " aaaaaa.aaaaaaa,\n" 11914 " aaaaaa.aaaaaaa,\n" 11915 " aaaaaa.aaaaaaa,\n" 11916 "};"); 11917 11918 // Don't create hanging lists. 11919 verifyFormat("someFunction(Param, {List1, List2,\n" 11920 " List3});", 11921 getLLVMStyleWithColumns(35)); 11922 verifyFormat("someFunction(Param, Param,\n" 11923 " {List1, List2,\n" 11924 " List3});", 11925 getLLVMStyleWithColumns(35)); 11926 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n" 11927 " aaaaaaaaaaaaaaaaaaaaaaa);"); 11928 } 11929 11930 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 11931 FormatStyle DoNotMerge = getLLVMStyle(); 11932 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 11933 11934 verifyFormat("void f() { return 42; }"); 11935 verifyFormat("void f() {\n" 11936 " return 42;\n" 11937 "}", 11938 DoNotMerge); 11939 verifyFormat("void f() {\n" 11940 " // Comment\n" 11941 "}"); 11942 verifyFormat("{\n" 11943 "#error {\n" 11944 " int a;\n" 11945 "}"); 11946 verifyFormat("{\n" 11947 " int a;\n" 11948 "#error {\n" 11949 "}"); 11950 verifyFormat("void f() {} // comment"); 11951 verifyFormat("void f() { int a; } // comment"); 11952 verifyFormat("void f() {\n" 11953 "} // comment", 11954 DoNotMerge); 11955 verifyFormat("void f() {\n" 11956 " int a;\n" 11957 "} // comment", 11958 DoNotMerge); 11959 verifyFormat("void f() {\n" 11960 "} // comment", 11961 getLLVMStyleWithColumns(15)); 11962 11963 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 11964 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 11965 11966 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 11967 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 11968 verifyFormat("class C {\n" 11969 " C()\n" 11970 " : iiiiiiii(nullptr),\n" 11971 " kkkkkkk(nullptr),\n" 11972 " mmmmmmm(nullptr),\n" 11973 " nnnnnnn(nullptr) {}\n" 11974 "};", 11975 getGoogleStyle()); 11976 11977 FormatStyle NoColumnLimit = getLLVMStyle(); 11978 NoColumnLimit.ColumnLimit = 0; 11979 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 11980 EXPECT_EQ("class C {\n" 11981 " A() : b(0) {}\n" 11982 "};", 11983 format("class C{A():b(0){}};", NoColumnLimit)); 11984 EXPECT_EQ("A()\n" 11985 " : b(0) {\n" 11986 "}", 11987 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 11988 11989 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 11990 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 11991 FormatStyle::SFS_None; 11992 EXPECT_EQ("A()\n" 11993 " : b(0) {\n" 11994 "}", 11995 format("A():b(0){}", DoNotMergeNoColumnLimit)); 11996 EXPECT_EQ("A()\n" 11997 " : b(0) {\n" 11998 "}", 11999 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 12000 12001 verifyFormat("#define A \\\n" 12002 " void f() { \\\n" 12003 " int i; \\\n" 12004 " }", 12005 getLLVMStyleWithColumns(20)); 12006 verifyFormat("#define A \\\n" 12007 " void f() { int i; }", 12008 getLLVMStyleWithColumns(21)); 12009 verifyFormat("#define A \\\n" 12010 " void f() { \\\n" 12011 " int i; \\\n" 12012 " } \\\n" 12013 " int j;", 12014 getLLVMStyleWithColumns(22)); 12015 verifyFormat("#define A \\\n" 12016 " void f() { int i; } \\\n" 12017 " int j;", 12018 getLLVMStyleWithColumns(23)); 12019 } 12020 12021 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) { 12022 FormatStyle MergeEmptyOnly = getLLVMStyle(); 12023 MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 12024 verifyFormat("class C {\n" 12025 " int f() {}\n" 12026 "};", 12027 MergeEmptyOnly); 12028 verifyFormat("class C {\n" 12029 " int f() {\n" 12030 " return 42;\n" 12031 " }\n" 12032 "};", 12033 MergeEmptyOnly); 12034 verifyFormat("int f() {}", MergeEmptyOnly); 12035 verifyFormat("int f() {\n" 12036 " return 42;\n" 12037 "}", 12038 MergeEmptyOnly); 12039 12040 // Also verify behavior when BraceWrapping.AfterFunction = true 12041 MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 12042 MergeEmptyOnly.BraceWrapping.AfterFunction = true; 12043 verifyFormat("int f() {}", MergeEmptyOnly); 12044 verifyFormat("class C {\n" 12045 " int f() {}\n" 12046 "};", 12047 MergeEmptyOnly); 12048 } 12049 12050 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 12051 FormatStyle MergeInlineOnly = getLLVMStyle(); 12052 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 12053 verifyFormat("class C {\n" 12054 " int f() { return 42; }\n" 12055 "};", 12056 MergeInlineOnly); 12057 verifyFormat("int f() {\n" 12058 " return 42;\n" 12059 "}", 12060 MergeInlineOnly); 12061 12062 // SFS_Inline implies SFS_Empty 12063 verifyFormat("class C {\n" 12064 " int f() {}\n" 12065 "};", 12066 MergeInlineOnly); 12067 verifyFormat("int f() {}", MergeInlineOnly); 12068 12069 // Also verify behavior when BraceWrapping.AfterFunction = true 12070 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 12071 MergeInlineOnly.BraceWrapping.AfterFunction = true; 12072 verifyFormat("class C {\n" 12073 " int f() { return 42; }\n" 12074 "};", 12075 MergeInlineOnly); 12076 verifyFormat("int f()\n" 12077 "{\n" 12078 " return 42;\n" 12079 "}", 12080 MergeInlineOnly); 12081 12082 // SFS_Inline implies SFS_Empty 12083 verifyFormat("int f() {}", MergeInlineOnly); 12084 verifyFormat("class C {\n" 12085 " int f() {}\n" 12086 "};", 12087 MergeInlineOnly); 12088 } 12089 12090 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) { 12091 FormatStyle MergeInlineOnly = getLLVMStyle(); 12092 MergeInlineOnly.AllowShortFunctionsOnASingleLine = 12093 FormatStyle::SFS_InlineOnly; 12094 verifyFormat("class C {\n" 12095 " int f() { return 42; }\n" 12096 "};", 12097 MergeInlineOnly); 12098 verifyFormat("int f() {\n" 12099 " return 42;\n" 12100 "}", 12101 MergeInlineOnly); 12102 12103 // SFS_InlineOnly does not imply SFS_Empty 12104 verifyFormat("class C {\n" 12105 " int f() {}\n" 12106 "};", 12107 MergeInlineOnly); 12108 verifyFormat("int f() {\n" 12109 "}", 12110 MergeInlineOnly); 12111 12112 // Also verify behavior when BraceWrapping.AfterFunction = true 12113 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 12114 MergeInlineOnly.BraceWrapping.AfterFunction = true; 12115 verifyFormat("class C {\n" 12116 " int f() { return 42; }\n" 12117 "};", 12118 MergeInlineOnly); 12119 verifyFormat("int f()\n" 12120 "{\n" 12121 " return 42;\n" 12122 "}", 12123 MergeInlineOnly); 12124 12125 // SFS_InlineOnly does not imply SFS_Empty 12126 verifyFormat("int f()\n" 12127 "{\n" 12128 "}", 12129 MergeInlineOnly); 12130 verifyFormat("class C {\n" 12131 " int f() {}\n" 12132 "};", 12133 MergeInlineOnly); 12134 } 12135 12136 TEST_F(FormatTest, SplitEmptyFunction) { 12137 FormatStyle Style = getLLVMStyle(); 12138 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 12139 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12140 Style.BraceWrapping.AfterFunction = true; 12141 Style.BraceWrapping.SplitEmptyFunction = false; 12142 Style.ColumnLimit = 40; 12143 12144 verifyFormat("int f()\n" 12145 "{}", 12146 Style); 12147 verifyFormat("int f()\n" 12148 "{\n" 12149 " return 42;\n" 12150 "}", 12151 Style); 12152 verifyFormat("int f()\n" 12153 "{\n" 12154 " // some comment\n" 12155 "}", 12156 Style); 12157 12158 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 12159 verifyFormat("int f() {}", Style); 12160 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12161 "{}", 12162 Style); 12163 verifyFormat("int f()\n" 12164 "{\n" 12165 " return 0;\n" 12166 "}", 12167 Style); 12168 12169 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 12170 verifyFormat("class Foo {\n" 12171 " int f() {}\n" 12172 "};\n", 12173 Style); 12174 verifyFormat("class Foo {\n" 12175 " int f() { return 0; }\n" 12176 "};\n", 12177 Style); 12178 verifyFormat("class Foo {\n" 12179 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12180 " {}\n" 12181 "};\n", 12182 Style); 12183 verifyFormat("class Foo {\n" 12184 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12185 " {\n" 12186 " return 0;\n" 12187 " }\n" 12188 "};\n", 12189 Style); 12190 12191 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 12192 verifyFormat("int f() {}", Style); 12193 verifyFormat("int f() { return 0; }", Style); 12194 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12195 "{}", 12196 Style); 12197 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12198 "{\n" 12199 " return 0;\n" 12200 "}", 12201 Style); 12202 } 12203 12204 TEST_F(FormatTest, SplitEmptyFunctionButNotRecord) { 12205 FormatStyle Style = getLLVMStyle(); 12206 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 12207 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12208 Style.BraceWrapping.AfterFunction = true; 12209 Style.BraceWrapping.SplitEmptyFunction = true; 12210 Style.BraceWrapping.SplitEmptyRecord = false; 12211 Style.ColumnLimit = 40; 12212 12213 verifyFormat("class C {};", Style); 12214 verifyFormat("struct C {};", Style); 12215 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 12216 " int bbbbbbbbbbbbbbbbbbbbbbbb)\n" 12217 "{\n" 12218 "}", 12219 Style); 12220 verifyFormat("class C {\n" 12221 " C()\n" 12222 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa(),\n" 12223 " bbbbbbbbbbbbbbbbbbb()\n" 12224 " {\n" 12225 " }\n" 12226 " void\n" 12227 " m(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 12228 " int bbbbbbbbbbbbbbbbbbbbbbbb)\n" 12229 " {\n" 12230 " }\n" 12231 "};", 12232 Style); 12233 } 12234 12235 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) { 12236 FormatStyle Style = getLLVMStyle(); 12237 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 12238 verifyFormat("#ifdef A\n" 12239 "int f() {}\n" 12240 "#else\n" 12241 "int g() {}\n" 12242 "#endif", 12243 Style); 12244 } 12245 12246 TEST_F(FormatTest, SplitEmptyClass) { 12247 FormatStyle Style = getLLVMStyle(); 12248 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12249 Style.BraceWrapping.AfterClass = true; 12250 Style.BraceWrapping.SplitEmptyRecord = false; 12251 12252 verifyFormat("class Foo\n" 12253 "{};", 12254 Style); 12255 verifyFormat("/* something */ class Foo\n" 12256 "{};", 12257 Style); 12258 verifyFormat("template <typename X> class Foo\n" 12259 "{};", 12260 Style); 12261 verifyFormat("class Foo\n" 12262 "{\n" 12263 " Foo();\n" 12264 "};", 12265 Style); 12266 verifyFormat("typedef class Foo\n" 12267 "{\n" 12268 "} Foo_t;", 12269 Style); 12270 12271 Style.BraceWrapping.SplitEmptyRecord = true; 12272 Style.BraceWrapping.AfterStruct = true; 12273 verifyFormat("class rep\n" 12274 "{\n" 12275 "};", 12276 Style); 12277 verifyFormat("struct rep\n" 12278 "{\n" 12279 "};", 12280 Style); 12281 verifyFormat("template <typename T> class rep\n" 12282 "{\n" 12283 "};", 12284 Style); 12285 verifyFormat("template <typename T> struct rep\n" 12286 "{\n" 12287 "};", 12288 Style); 12289 verifyFormat("class rep\n" 12290 "{\n" 12291 " int x;\n" 12292 "};", 12293 Style); 12294 verifyFormat("struct rep\n" 12295 "{\n" 12296 " int x;\n" 12297 "};", 12298 Style); 12299 verifyFormat("template <typename T> class rep\n" 12300 "{\n" 12301 " int x;\n" 12302 "};", 12303 Style); 12304 verifyFormat("template <typename T> struct rep\n" 12305 "{\n" 12306 " int x;\n" 12307 "};", 12308 Style); 12309 verifyFormat("template <typename T> class rep // Foo\n" 12310 "{\n" 12311 " int x;\n" 12312 "};", 12313 Style); 12314 verifyFormat("template <typename T> struct rep // Bar\n" 12315 "{\n" 12316 " int x;\n" 12317 "};", 12318 Style); 12319 12320 verifyFormat("template <typename T> class rep<T>\n" 12321 "{\n" 12322 " int x;\n" 12323 "};", 12324 Style); 12325 12326 verifyFormat("template <typename T> class rep<std::complex<T>>\n" 12327 "{\n" 12328 " int x;\n" 12329 "};", 12330 Style); 12331 verifyFormat("template <typename T> class rep<std::complex<T>>\n" 12332 "{\n" 12333 "};", 12334 Style); 12335 12336 verifyFormat("#include \"stdint.h\"\n" 12337 "namespace rep {}", 12338 Style); 12339 verifyFormat("#include <stdint.h>\n" 12340 "namespace rep {}", 12341 Style); 12342 verifyFormat("#include <stdint.h>\n" 12343 "namespace rep {}", 12344 "#include <stdint.h>\n" 12345 "namespace rep {\n" 12346 "\n" 12347 "\n" 12348 "}", 12349 Style); 12350 } 12351 12352 TEST_F(FormatTest, SplitEmptyStruct) { 12353 FormatStyle Style = getLLVMStyle(); 12354 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12355 Style.BraceWrapping.AfterStruct = true; 12356 Style.BraceWrapping.SplitEmptyRecord = false; 12357 12358 verifyFormat("struct Foo\n" 12359 "{};", 12360 Style); 12361 verifyFormat("/* something */ struct Foo\n" 12362 "{};", 12363 Style); 12364 verifyFormat("template <typename X> struct Foo\n" 12365 "{};", 12366 Style); 12367 verifyFormat("struct Foo\n" 12368 "{\n" 12369 " Foo();\n" 12370 "};", 12371 Style); 12372 verifyFormat("typedef struct Foo\n" 12373 "{\n" 12374 "} Foo_t;", 12375 Style); 12376 // typedef struct Bar {} Bar_t; 12377 } 12378 12379 TEST_F(FormatTest, SplitEmptyUnion) { 12380 FormatStyle Style = getLLVMStyle(); 12381 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12382 Style.BraceWrapping.AfterUnion = true; 12383 Style.BraceWrapping.SplitEmptyRecord = false; 12384 12385 verifyFormat("union Foo\n" 12386 "{};", 12387 Style); 12388 verifyFormat("/* something */ union Foo\n" 12389 "{};", 12390 Style); 12391 verifyFormat("union Foo\n" 12392 "{\n" 12393 " A,\n" 12394 "};", 12395 Style); 12396 verifyFormat("typedef union Foo\n" 12397 "{\n" 12398 "} Foo_t;", 12399 Style); 12400 } 12401 12402 TEST_F(FormatTest, SplitEmptyNamespace) { 12403 FormatStyle Style = getLLVMStyle(); 12404 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12405 Style.BraceWrapping.AfterNamespace = true; 12406 Style.BraceWrapping.SplitEmptyNamespace = false; 12407 12408 verifyFormat("namespace Foo\n" 12409 "{};", 12410 Style); 12411 verifyFormat("/* something */ namespace Foo\n" 12412 "{};", 12413 Style); 12414 verifyFormat("inline namespace Foo\n" 12415 "{};", 12416 Style); 12417 verifyFormat("/* something */ inline namespace Foo\n" 12418 "{};", 12419 Style); 12420 verifyFormat("export namespace Foo\n" 12421 "{};", 12422 Style); 12423 verifyFormat("namespace Foo\n" 12424 "{\n" 12425 "void Bar();\n" 12426 "};", 12427 Style); 12428 } 12429 12430 TEST_F(FormatTest, NeverMergeShortRecords) { 12431 FormatStyle Style = getLLVMStyle(); 12432 12433 verifyFormat("class Foo {\n" 12434 " Foo();\n" 12435 "};", 12436 Style); 12437 verifyFormat("typedef class Foo {\n" 12438 " Foo();\n" 12439 "} Foo_t;", 12440 Style); 12441 verifyFormat("struct Foo {\n" 12442 " Foo();\n" 12443 "};", 12444 Style); 12445 verifyFormat("typedef struct Foo {\n" 12446 " Foo();\n" 12447 "} Foo_t;", 12448 Style); 12449 verifyFormat("union Foo {\n" 12450 " A,\n" 12451 "};", 12452 Style); 12453 verifyFormat("typedef union Foo {\n" 12454 " A,\n" 12455 "} Foo_t;", 12456 Style); 12457 verifyFormat("namespace Foo {\n" 12458 "void Bar();\n" 12459 "};", 12460 Style); 12461 12462 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12463 Style.BraceWrapping.AfterClass = true; 12464 Style.BraceWrapping.AfterStruct = true; 12465 Style.BraceWrapping.AfterUnion = true; 12466 Style.BraceWrapping.AfterNamespace = true; 12467 verifyFormat("class Foo\n" 12468 "{\n" 12469 " Foo();\n" 12470 "};", 12471 Style); 12472 verifyFormat("typedef class Foo\n" 12473 "{\n" 12474 " Foo();\n" 12475 "} Foo_t;", 12476 Style); 12477 verifyFormat("struct Foo\n" 12478 "{\n" 12479 " Foo();\n" 12480 "};", 12481 Style); 12482 verifyFormat("typedef struct Foo\n" 12483 "{\n" 12484 " Foo();\n" 12485 "} Foo_t;", 12486 Style); 12487 verifyFormat("union Foo\n" 12488 "{\n" 12489 " A,\n" 12490 "};", 12491 Style); 12492 verifyFormat("typedef union Foo\n" 12493 "{\n" 12494 " A,\n" 12495 "} Foo_t;", 12496 Style); 12497 verifyFormat("namespace Foo\n" 12498 "{\n" 12499 "void Bar();\n" 12500 "};", 12501 Style); 12502 } 12503 12504 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 12505 // Elaborate type variable declarations. 12506 verifyFormat("struct foo a = {bar};\nint n;"); 12507 verifyFormat("class foo a = {bar};\nint n;"); 12508 verifyFormat("union foo a = {bar};\nint n;"); 12509 12510 // Elaborate types inside function definitions. 12511 verifyFormat("struct foo f() {}\nint n;"); 12512 verifyFormat("class foo f() {}\nint n;"); 12513 verifyFormat("union foo f() {}\nint n;"); 12514 12515 // Templates. 12516 verifyFormat("template <class X> void f() {}\nint n;"); 12517 verifyFormat("template <struct X> void f() {}\nint n;"); 12518 verifyFormat("template <union X> void f() {}\nint n;"); 12519 12520 // Actual definitions... 12521 verifyFormat("struct {\n} n;"); 12522 verifyFormat( 12523 "template <template <class T, class Y>, class Z> class X {\n} n;"); 12524 verifyFormat("union Z {\n int n;\n} x;"); 12525 verifyFormat("class MACRO Z {\n} n;"); 12526 verifyFormat("class MACRO(X) Z {\n} n;"); 12527 verifyFormat("class __attribute__(X) Z {\n} n;"); 12528 verifyFormat("class __declspec(X) Z {\n} n;"); 12529 verifyFormat("class A##B##C {\n} n;"); 12530 verifyFormat("class alignas(16) Z {\n} n;"); 12531 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 12532 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 12533 12534 // Redefinition from nested context: 12535 verifyFormat("class A::B::C {\n} n;"); 12536 12537 // Template definitions. 12538 verifyFormat( 12539 "template <typename F>\n" 12540 "Matcher(const Matcher<F> &Other,\n" 12541 " typename enable_if_c<is_base_of<F, T>::value &&\n" 12542 " !is_same<F, T>::value>::type * = 0)\n" 12543 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 12544 12545 // FIXME: This is still incorrectly handled at the formatter side. 12546 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 12547 verifyFormat("int i = SomeFunction(a<b, a> b);"); 12548 12549 // FIXME: 12550 // This now gets parsed incorrectly as class definition. 12551 // verifyFormat("class A<int> f() {\n}\nint n;"); 12552 12553 // Elaborate types where incorrectly parsing the structural element would 12554 // break the indent. 12555 verifyFormat("if (true)\n" 12556 " class X x;\n" 12557 "else\n" 12558 " f();\n"); 12559 12560 // This is simply incomplete. Formatting is not important, but must not crash. 12561 verifyFormat("class A:"); 12562 } 12563 12564 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 12565 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 12566 format("#error Leave all white!!!!! space* alone!\n")); 12567 EXPECT_EQ( 12568 "#warning Leave all white!!!!! space* alone!\n", 12569 format("#warning Leave all white!!!!! space* alone!\n")); 12570 EXPECT_EQ("#error 1", format(" # error 1")); 12571 EXPECT_EQ("#warning 1", format(" # warning 1")); 12572 } 12573 12574 TEST_F(FormatTest, FormatHashIfExpressions) { 12575 verifyFormat("#if AAAA && BBBB"); 12576 verifyFormat("#if (AAAA && BBBB)"); 12577 verifyFormat("#elif (AAAA && BBBB)"); 12578 // FIXME: Come up with a better indentation for #elif. 12579 verifyFormat( 12580 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 12581 " defined(BBBBBBBB)\n" 12582 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 12583 " defined(BBBBBBBB)\n" 12584 "#endif", 12585 getLLVMStyleWithColumns(65)); 12586 } 12587 12588 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 12589 FormatStyle AllowsMergedIf = getGoogleStyle(); 12590 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 12591 FormatStyle::SIS_WithoutElse; 12592 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 12593 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 12594 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 12595 EXPECT_EQ("if (true) return 42;", 12596 format("if (true)\nreturn 42;", AllowsMergedIf)); 12597 FormatStyle ShortMergedIf = AllowsMergedIf; 12598 ShortMergedIf.ColumnLimit = 25; 12599 verifyFormat("#define A \\\n" 12600 " if (true) return 42;", 12601 ShortMergedIf); 12602 verifyFormat("#define A \\\n" 12603 " f(); \\\n" 12604 " if (true)\n" 12605 "#define B", 12606 ShortMergedIf); 12607 verifyFormat("#define A \\\n" 12608 " f(); \\\n" 12609 " if (true)\n" 12610 "g();", 12611 ShortMergedIf); 12612 verifyFormat("{\n" 12613 "#ifdef A\n" 12614 " // Comment\n" 12615 " if (true) continue;\n" 12616 "#endif\n" 12617 " // Comment\n" 12618 " if (true) continue;\n" 12619 "}", 12620 ShortMergedIf); 12621 ShortMergedIf.ColumnLimit = 33; 12622 verifyFormat("#define A \\\n" 12623 " if constexpr (true) return 42;", 12624 ShortMergedIf); 12625 verifyFormat("#define A \\\n" 12626 " if CONSTEXPR (true) return 42;", 12627 ShortMergedIf); 12628 ShortMergedIf.ColumnLimit = 29; 12629 verifyFormat("#define A \\\n" 12630 " if (aaaaaaaaaa) return 1; \\\n" 12631 " return 2;", 12632 ShortMergedIf); 12633 ShortMergedIf.ColumnLimit = 28; 12634 verifyFormat("#define A \\\n" 12635 " if (aaaaaaaaaa) \\\n" 12636 " return 1; \\\n" 12637 " return 2;", 12638 ShortMergedIf); 12639 verifyFormat("#define A \\\n" 12640 " if constexpr (aaaaaaa) \\\n" 12641 " return 1; \\\n" 12642 " return 2;", 12643 ShortMergedIf); 12644 verifyFormat("#define A \\\n" 12645 " if CONSTEXPR (aaaaaaa) \\\n" 12646 " return 1; \\\n" 12647 " return 2;", 12648 ShortMergedIf); 12649 } 12650 12651 TEST_F(FormatTest, FormatStarDependingOnContext) { 12652 verifyFormat("void f(int *a);"); 12653 verifyFormat("void f() { f(fint * b); }"); 12654 verifyFormat("class A {\n void f(int *a);\n};"); 12655 verifyFormat("class A {\n int *a;\n};"); 12656 verifyFormat("namespace a {\n" 12657 "namespace b {\n" 12658 "class A {\n" 12659 " void f() {}\n" 12660 " int *a;\n" 12661 "};\n" 12662 "} // namespace b\n" 12663 "} // namespace a"); 12664 } 12665 12666 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 12667 verifyFormat("while"); 12668 verifyFormat("operator"); 12669 } 12670 12671 TEST_F(FormatTest, SkipsDeeplyNestedLines) { 12672 // This code would be painfully slow to format if we didn't skip it. 12673 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 12674 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12675 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12676 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12677 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12678 "A(1, 1)\n" 12679 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x 12680 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12681 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12682 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12683 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12684 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12685 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12686 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12687 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12688 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n"); 12689 // Deeply nested part is untouched, rest is formatted. 12690 EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n", 12691 format(std::string("int i;\n") + Code + "int j;\n", 12692 getLLVMStyle(), SC_ExpectIncomplete)); 12693 } 12694 12695 //===----------------------------------------------------------------------===// 12696 // Objective-C tests. 12697 //===----------------------------------------------------------------------===// 12698 12699 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 12700 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 12701 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 12702 format("-(NSUInteger)indexOfObject:(id)anObject;")); 12703 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 12704 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 12705 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 12706 format("-(NSInteger)Method3:(id)anObject;")); 12707 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 12708 format("-(NSInteger)Method4:(id)anObject;")); 12709 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 12710 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 12711 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 12712 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 12713 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 12714 "forAllCells:(BOOL)flag;", 12715 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 12716 "forAllCells:(BOOL)flag;")); 12717 12718 // Very long objectiveC method declaration. 12719 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 12720 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 12721 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 12722 " inRange:(NSRange)range\n" 12723 " outRange:(NSRange)out_range\n" 12724 " outRange1:(NSRange)out_range1\n" 12725 " outRange2:(NSRange)out_range2\n" 12726 " outRange3:(NSRange)out_range3\n" 12727 " outRange4:(NSRange)out_range4\n" 12728 " outRange5:(NSRange)out_range5\n" 12729 " outRange6:(NSRange)out_range6\n" 12730 " outRange7:(NSRange)out_range7\n" 12731 " outRange8:(NSRange)out_range8\n" 12732 " outRange9:(NSRange)out_range9;"); 12733 12734 // When the function name has to be wrapped. 12735 FormatStyle Style = getLLVMStyle(); 12736 // ObjC ignores IndentWrappedFunctionNames when wrapping methods 12737 // and always indents instead. 12738 Style.IndentWrappedFunctionNames = false; 12739 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 12740 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 12741 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 12742 "}", 12743 Style); 12744 Style.IndentWrappedFunctionNames = true; 12745 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 12746 " veryLooooooooooongName:(NSString)cccccccccccccc\n" 12747 " anotherName:(NSString)dddddddddddddd {\n" 12748 "}", 12749 Style); 12750 12751 verifyFormat("- (int)sum:(vector<int>)numbers;"); 12752 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 12753 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 12754 // protocol lists (but not for template classes): 12755 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 12756 12757 verifyFormat("- (int (*)())foo:(int (*)())f;"); 12758 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 12759 12760 // If there's no return type (very rare in practice!), LLVM and Google style 12761 // agree. 12762 verifyFormat("- foo;"); 12763 verifyFormat("- foo:(int)f;"); 12764 verifyGoogleFormat("- foo:(int)foo;"); 12765 } 12766 12767 TEST_F(FormatTest, BreaksStringLiterals) { 12768 EXPECT_EQ("\"some text \"\n" 12769 "\"other\";", 12770 format("\"some text other\";", getLLVMStyleWithColumns(12))); 12771 EXPECT_EQ("\"some text \"\n" 12772 "\"other\";", 12773 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 12774 EXPECT_EQ( 12775 "#define A \\\n" 12776 " \"some \" \\\n" 12777 " \"text \" \\\n" 12778 " \"other\";", 12779 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 12780 EXPECT_EQ( 12781 "#define A \\\n" 12782 " \"so \" \\\n" 12783 " \"text \" \\\n" 12784 " \"other\";", 12785 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 12786 12787 EXPECT_EQ("\"some text\"", 12788 format("\"some text\"", getLLVMStyleWithColumns(1))); 12789 EXPECT_EQ("\"some text\"", 12790 format("\"some text\"", getLLVMStyleWithColumns(11))); 12791 EXPECT_EQ("\"some \"\n" 12792 "\"text\"", 12793 format("\"some text\"", getLLVMStyleWithColumns(10))); 12794 EXPECT_EQ("\"some \"\n" 12795 "\"text\"", 12796 format("\"some text\"", getLLVMStyleWithColumns(7))); 12797 EXPECT_EQ("\"some\"\n" 12798 "\" tex\"\n" 12799 "\"t\"", 12800 format("\"some text\"", getLLVMStyleWithColumns(6))); 12801 EXPECT_EQ("\"some\"\n" 12802 "\" tex\"\n" 12803 "\" and\"", 12804 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 12805 EXPECT_EQ("\"some\"\n" 12806 "\"/tex\"\n" 12807 "\"/and\"", 12808 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 12809 12810 EXPECT_EQ("variable =\n" 12811 " \"long string \"\n" 12812 " \"literal\";", 12813 format("variable = \"long string literal\";", 12814 getLLVMStyleWithColumns(20))); 12815 12816 EXPECT_EQ("variable = f(\n" 12817 " \"long string \"\n" 12818 " \"literal\",\n" 12819 " short,\n" 12820 " loooooooooooooooooooong);", 12821 format("variable = f(\"long string literal\", short, " 12822 "loooooooooooooooooooong);", 12823 getLLVMStyleWithColumns(20))); 12824 12825 EXPECT_EQ( 12826 "f(g(\"long string \"\n" 12827 " \"literal\"),\n" 12828 " b);", 12829 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 12830 EXPECT_EQ("f(g(\"long string \"\n" 12831 " \"literal\",\n" 12832 " a),\n" 12833 " b);", 12834 format("f(g(\"long string literal\", a), b);", 12835 getLLVMStyleWithColumns(20))); 12836 EXPECT_EQ( 12837 "f(\"one two\".split(\n" 12838 " variable));", 12839 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 12840 EXPECT_EQ("f(\"one two three four five six \"\n" 12841 " \"seven\".split(\n" 12842 " really_looooong_variable));", 12843 format("f(\"one two three four five six seven\"." 12844 "split(really_looooong_variable));", 12845 getLLVMStyleWithColumns(33))); 12846 12847 EXPECT_EQ("f(\"some \"\n" 12848 " \"text\",\n" 12849 " other);", 12850 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 12851 12852 // Only break as a last resort. 12853 verifyFormat( 12854 "aaaaaaaaaaaaaaaaaaaa(\n" 12855 " aaaaaaaaaaaaaaaaaaaa,\n" 12856 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 12857 12858 EXPECT_EQ("\"splitmea\"\n" 12859 "\"trandomp\"\n" 12860 "\"oint\"", 12861 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 12862 12863 EXPECT_EQ("\"split/\"\n" 12864 "\"pathat/\"\n" 12865 "\"slashes\"", 12866 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 12867 12868 EXPECT_EQ("\"split/\"\n" 12869 "\"pathat/\"\n" 12870 "\"slashes\"", 12871 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 12872 EXPECT_EQ("\"split at \"\n" 12873 "\"spaces/at/\"\n" 12874 "\"slashes.at.any$\"\n" 12875 "\"non-alphanumeric%\"\n" 12876 "\"1111111111characte\"\n" 12877 "\"rs\"", 12878 format("\"split at " 12879 "spaces/at/" 12880 "slashes.at." 12881 "any$non-" 12882 "alphanumeric%" 12883 "1111111111characte" 12884 "rs\"", 12885 getLLVMStyleWithColumns(20))); 12886 12887 // Verify that splitting the strings understands 12888 // Style::AlwaysBreakBeforeMultilineStrings. 12889 EXPECT_EQ("aaaaaaaaaaaa(\n" 12890 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 12891 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 12892 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 12893 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 12894 "aaaaaaaaaaaaaaaaaaaaaa\");", 12895 getGoogleStyle())); 12896 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 12897 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 12898 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 12899 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 12900 "aaaaaaaaaaaaaaaaaaaaaa\";", 12901 getGoogleStyle())); 12902 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 12903 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 12904 format("llvm::outs() << " 12905 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 12906 "aaaaaaaaaaaaaaaaaaa\";")); 12907 EXPECT_EQ("ffff(\n" 12908 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 12909 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 12910 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 12911 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 12912 getGoogleStyle())); 12913 12914 FormatStyle Style = getLLVMStyleWithColumns(12); 12915 Style.BreakStringLiterals = false; 12916 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 12917 12918 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 12919 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 12920 EXPECT_EQ("#define A \\\n" 12921 " \"some \" \\\n" 12922 " \"text \" \\\n" 12923 " \"other\";", 12924 format("#define A \"some text other\";", AlignLeft)); 12925 } 12926 12927 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) { 12928 EXPECT_EQ("C a = \"some more \"\n" 12929 " \"text\";", 12930 format("C a = \"some more text\";", getLLVMStyleWithColumns(18))); 12931 } 12932 12933 TEST_F(FormatTest, FullyRemoveEmptyLines) { 12934 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 12935 NoEmptyLines.MaxEmptyLinesToKeep = 0; 12936 EXPECT_EQ("int i = a(b());", 12937 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 12938 } 12939 12940 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 12941 EXPECT_EQ( 12942 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 12943 "(\n" 12944 " \"x\t\");", 12945 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 12946 "aaaaaaa(" 12947 "\"x\t\");")); 12948 } 12949 12950 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 12951 EXPECT_EQ( 12952 "u8\"utf8 string \"\n" 12953 "u8\"literal\";", 12954 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 12955 EXPECT_EQ( 12956 "u\"utf16 string \"\n" 12957 "u\"literal\";", 12958 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 12959 EXPECT_EQ( 12960 "U\"utf32 string \"\n" 12961 "U\"literal\";", 12962 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 12963 EXPECT_EQ("L\"wide string \"\n" 12964 "L\"literal\";", 12965 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 12966 EXPECT_EQ("@\"NSString \"\n" 12967 "@\"literal\";", 12968 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 12969 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26)); 12970 12971 // This input makes clang-format try to split the incomplete unicode escape 12972 // sequence, which used to lead to a crasher. 12973 verifyNoCrash( 12974 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 12975 getLLVMStyleWithColumns(60)); 12976 } 12977 12978 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 12979 FormatStyle Style = getGoogleStyleWithColumns(15); 12980 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 12981 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 12982 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 12983 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 12984 EXPECT_EQ("u8R\"x(raw literal)x\";", 12985 format("u8R\"x(raw literal)x\";", Style)); 12986 } 12987 12988 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 12989 FormatStyle Style = getLLVMStyleWithColumns(20); 12990 EXPECT_EQ( 12991 "_T(\"aaaaaaaaaaaaaa\")\n" 12992 "_T(\"aaaaaaaaaaaaaa\")\n" 12993 "_T(\"aaaaaaaaaaaa\")", 12994 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 12995 EXPECT_EQ("f(x,\n" 12996 " _T(\"aaaaaaaaaaaa\")\n" 12997 " _T(\"aaa\"),\n" 12998 " z);", 12999 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 13000 13001 // FIXME: Handle embedded spaces in one iteration. 13002 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 13003 // "_T(\"aaaaaaaaaaaaa\")\n" 13004 // "_T(\"aaaaaaaaaaaaa\")\n" 13005 // "_T(\"a\")", 13006 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 13007 // getLLVMStyleWithColumns(20))); 13008 EXPECT_EQ( 13009 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 13010 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 13011 EXPECT_EQ("f(\n" 13012 "#if !TEST\n" 13013 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 13014 "#endif\n" 13015 ");", 13016 format("f(\n" 13017 "#if !TEST\n" 13018 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 13019 "#endif\n" 13020 ");")); 13021 EXPECT_EQ("f(\n" 13022 "\n" 13023 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 13024 format("f(\n" 13025 "\n" 13026 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 13027 // Regression test for accessing tokens past the end of a vector in the 13028 // TokenLexer. 13029 verifyNoCrash(R"(_T( 13030 " 13031 ) 13032 )"); 13033 } 13034 13035 TEST_F(FormatTest, BreaksStringLiteralOperands) { 13036 // In a function call with two operands, the second can be broken with no line 13037 // break before it. 13038 EXPECT_EQ( 13039 "func(a, \"long long \"\n" 13040 " \"long long\");", 13041 format("func(a, \"long long long long\");", getLLVMStyleWithColumns(24))); 13042 // In a function call with three operands, the second must be broken with a 13043 // line break before it. 13044 EXPECT_EQ("func(a,\n" 13045 " \"long long long \"\n" 13046 " \"long\",\n" 13047 " c);", 13048 format("func(a, \"long long long long\", c);", 13049 getLLVMStyleWithColumns(24))); 13050 // In a function call with three operands, the third must be broken with a 13051 // line break before it. 13052 EXPECT_EQ("func(a, b,\n" 13053 " \"long long long \"\n" 13054 " \"long\");", 13055 format("func(a, b, \"long long long long\");", 13056 getLLVMStyleWithColumns(24))); 13057 // In a function call with three operands, both the second and the third must 13058 // be broken with a line break before them. 13059 EXPECT_EQ("func(a,\n" 13060 " \"long long long \"\n" 13061 " \"long\",\n" 13062 " \"long long long \"\n" 13063 " \"long\");", 13064 format("func(a, \"long long long long\", \"long long long long\");", 13065 getLLVMStyleWithColumns(24))); 13066 // In a chain of << with two operands, the second can be broken with no line 13067 // break before it. 13068 EXPECT_EQ("a << \"line line \"\n" 13069 " \"line\";", 13070 format("a << \"line line line\";", getLLVMStyleWithColumns(20))); 13071 // In a chain of << with three operands, the second can be broken with no line 13072 // break before it. 13073 EXPECT_EQ( 13074 "abcde << \"line \"\n" 13075 " \"line line\"\n" 13076 " << c;", 13077 format("abcde << \"line line line\" << c;", getLLVMStyleWithColumns(20))); 13078 // In a chain of << with three operands, the third must be broken with a line 13079 // break before it. 13080 EXPECT_EQ( 13081 "a << b\n" 13082 " << \"line line \"\n" 13083 " \"line\";", 13084 format("a << b << \"line line line\";", getLLVMStyleWithColumns(20))); 13085 // In a chain of << with three operands, the second can be broken with no line 13086 // break before it and the third must be broken with a line break before it. 13087 EXPECT_EQ("abcd << \"line line \"\n" 13088 " \"line\"\n" 13089 " << \"line line \"\n" 13090 " \"line\";", 13091 format("abcd << \"line line line\" << \"line line line\";", 13092 getLLVMStyleWithColumns(20))); 13093 // In a chain of binary operators with two operands, the second can be broken 13094 // with no line break before it. 13095 EXPECT_EQ( 13096 "abcd + \"line line \"\n" 13097 " \"line line\";", 13098 format("abcd + \"line line line line\";", getLLVMStyleWithColumns(20))); 13099 // In a chain of binary operators with three operands, the second must be 13100 // broken with a line break before it. 13101 EXPECT_EQ("abcd +\n" 13102 " \"line line \"\n" 13103 " \"line line\" +\n" 13104 " e;", 13105 format("abcd + \"line line line line\" + e;", 13106 getLLVMStyleWithColumns(20))); 13107 // In a function call with two operands, with AlignAfterOpenBracket enabled, 13108 // the first must be broken with a line break before it. 13109 FormatStyle Style = getLLVMStyleWithColumns(25); 13110 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 13111 EXPECT_EQ("someFunction(\n" 13112 " \"long long long \"\n" 13113 " \"long\",\n" 13114 " a);", 13115 format("someFunction(\"long long long long\", a);", Style)); 13116 } 13117 13118 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 13119 EXPECT_EQ( 13120 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 13121 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 13122 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 13123 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 13124 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 13125 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 13126 } 13127 13128 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 13129 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 13130 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 13131 EXPECT_EQ("fffffffffff(g(R\"x(\n" 13132 "multiline raw string literal xxxxxxxxxxxxxx\n" 13133 ")x\",\n" 13134 " a),\n" 13135 " b);", 13136 format("fffffffffff(g(R\"x(\n" 13137 "multiline raw string literal xxxxxxxxxxxxxx\n" 13138 ")x\", a), b);", 13139 getGoogleStyleWithColumns(20))); 13140 EXPECT_EQ("fffffffffff(\n" 13141 " g(R\"x(qqq\n" 13142 "multiline raw string literal xxxxxxxxxxxxxx\n" 13143 ")x\",\n" 13144 " a),\n" 13145 " b);", 13146 format("fffffffffff(g(R\"x(qqq\n" 13147 "multiline raw string literal xxxxxxxxxxxxxx\n" 13148 ")x\", a), b);", 13149 getGoogleStyleWithColumns(20))); 13150 13151 EXPECT_EQ("fffffffffff(R\"x(\n" 13152 "multiline raw string literal xxxxxxxxxxxxxx\n" 13153 ")x\");", 13154 format("fffffffffff(R\"x(\n" 13155 "multiline raw string literal xxxxxxxxxxxxxx\n" 13156 ")x\");", 13157 getGoogleStyleWithColumns(20))); 13158 EXPECT_EQ("fffffffffff(R\"x(\n" 13159 "multiline raw string literal xxxxxxxxxxxxxx\n" 13160 ")x\" + bbbbbb);", 13161 format("fffffffffff(R\"x(\n" 13162 "multiline raw string literal xxxxxxxxxxxxxx\n" 13163 ")x\" + bbbbbb);", 13164 getGoogleStyleWithColumns(20))); 13165 EXPECT_EQ("fffffffffff(\n" 13166 " R\"x(\n" 13167 "multiline raw string literal xxxxxxxxxxxxxx\n" 13168 ")x\" +\n" 13169 " bbbbbb);", 13170 format("fffffffffff(\n" 13171 " R\"x(\n" 13172 "multiline raw string literal xxxxxxxxxxxxxx\n" 13173 ")x\" + bbbbbb);", 13174 getGoogleStyleWithColumns(20))); 13175 EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);", 13176 format("fffffffffff(\n" 13177 " R\"(single line raw string)\" + bbbbbb);")); 13178 } 13179 13180 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 13181 verifyFormat("string a = \"unterminated;"); 13182 EXPECT_EQ("function(\"unterminated,\n" 13183 " OtherParameter);", 13184 format("function( \"unterminated,\n" 13185 " OtherParameter);")); 13186 } 13187 13188 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 13189 FormatStyle Style = getLLVMStyle(); 13190 Style.Standard = FormatStyle::LS_Cpp03; 13191 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 13192 format("#define x(_a) printf(\"foo\"_a);", Style)); 13193 } 13194 13195 TEST_F(FormatTest, CppLexVersion) { 13196 FormatStyle Style = getLLVMStyle(); 13197 // Formatting of x * y differs if x is a type. 13198 verifyFormat("void foo() { MACRO(a * b); }", Style); 13199 verifyFormat("void foo() { MACRO(int *b); }", Style); 13200 13201 // LLVM style uses latest lexer. 13202 verifyFormat("void foo() { MACRO(char8_t *b); }", Style); 13203 Style.Standard = FormatStyle::LS_Cpp17; 13204 // But in c++17, char8_t isn't a keyword. 13205 verifyFormat("void foo() { MACRO(char8_t * b); }", Style); 13206 } 13207 13208 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 13209 13210 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 13211 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 13212 " \"ddeeefff\");", 13213 format("someFunction(\"aaabbbcccdddeeefff\");", 13214 getLLVMStyleWithColumns(25))); 13215 EXPECT_EQ("someFunction1234567890(\n" 13216 " \"aaabbbcccdddeeefff\");", 13217 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 13218 getLLVMStyleWithColumns(26))); 13219 EXPECT_EQ("someFunction1234567890(\n" 13220 " \"aaabbbcccdddeeeff\"\n" 13221 " \"f\");", 13222 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 13223 getLLVMStyleWithColumns(25))); 13224 EXPECT_EQ("someFunction1234567890(\n" 13225 " \"aaabbbcccdddeeeff\"\n" 13226 " \"f\");", 13227 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 13228 getLLVMStyleWithColumns(24))); 13229 EXPECT_EQ("someFunction(\n" 13230 " \"aaabbbcc ddde \"\n" 13231 " \"efff\");", 13232 format("someFunction(\"aaabbbcc ddde efff\");", 13233 getLLVMStyleWithColumns(25))); 13234 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 13235 " \"ddeeefff\");", 13236 format("someFunction(\"aaabbbccc ddeeefff\");", 13237 getLLVMStyleWithColumns(25))); 13238 EXPECT_EQ("someFunction1234567890(\n" 13239 " \"aaabb \"\n" 13240 " \"cccdddeeefff\");", 13241 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 13242 getLLVMStyleWithColumns(25))); 13243 EXPECT_EQ("#define A \\\n" 13244 " string s = \\\n" 13245 " \"123456789\" \\\n" 13246 " \"0\"; \\\n" 13247 " int i;", 13248 format("#define A string s = \"1234567890\"; int i;", 13249 getLLVMStyleWithColumns(20))); 13250 EXPECT_EQ("someFunction(\n" 13251 " \"aaabbbcc \"\n" 13252 " \"dddeeefff\");", 13253 format("someFunction(\"aaabbbcc dddeeefff\");", 13254 getLLVMStyleWithColumns(25))); 13255 } 13256 13257 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 13258 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 13259 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 13260 EXPECT_EQ("\"test\"\n" 13261 "\"\\n\"", 13262 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 13263 EXPECT_EQ("\"tes\\\\\"\n" 13264 "\"n\"", 13265 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 13266 EXPECT_EQ("\"\\\\\\\\\"\n" 13267 "\"\\n\"", 13268 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 13269 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 13270 EXPECT_EQ("\"\\uff01\"\n" 13271 "\"test\"", 13272 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 13273 EXPECT_EQ("\"\\Uff01ff02\"", 13274 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 13275 EXPECT_EQ("\"\\x000000000001\"\n" 13276 "\"next\"", 13277 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 13278 EXPECT_EQ("\"\\x000000000001next\"", 13279 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 13280 EXPECT_EQ("\"\\x000000000001\"", 13281 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 13282 EXPECT_EQ("\"test\"\n" 13283 "\"\\000000\"\n" 13284 "\"000001\"", 13285 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 13286 EXPECT_EQ("\"test\\000\"\n" 13287 "\"00000000\"\n" 13288 "\"1\"", 13289 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 13290 } 13291 13292 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 13293 verifyFormat("void f() {\n" 13294 " return g() {}\n" 13295 " void h() {}"); 13296 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 13297 "g();\n" 13298 "}"); 13299 } 13300 13301 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 13302 verifyFormat( 13303 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 13304 } 13305 13306 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 13307 verifyFormat("class X {\n" 13308 " void f() {\n" 13309 " }\n" 13310 "};", 13311 getLLVMStyleWithColumns(12)); 13312 } 13313 13314 TEST_F(FormatTest, ConfigurableIndentWidth) { 13315 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 13316 EightIndent.IndentWidth = 8; 13317 EightIndent.ContinuationIndentWidth = 8; 13318 verifyFormat("void f() {\n" 13319 " someFunction();\n" 13320 " if (true) {\n" 13321 " f();\n" 13322 " }\n" 13323 "}", 13324 EightIndent); 13325 verifyFormat("class X {\n" 13326 " void f() {\n" 13327 " }\n" 13328 "};", 13329 EightIndent); 13330 verifyFormat("int x[] = {\n" 13331 " call(),\n" 13332 " call()};", 13333 EightIndent); 13334 } 13335 13336 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 13337 verifyFormat("double\n" 13338 "f();", 13339 getLLVMStyleWithColumns(8)); 13340 } 13341 13342 TEST_F(FormatTest, ConfigurableUseOfTab) { 13343 FormatStyle Tab = getLLVMStyleWithColumns(42); 13344 Tab.IndentWidth = 8; 13345 Tab.UseTab = FormatStyle::UT_Always; 13346 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left; 13347 13348 EXPECT_EQ("if (aaaaaaaa && // q\n" 13349 " bb)\t\t// w\n" 13350 "\t;", 13351 format("if (aaaaaaaa &&// q\n" 13352 "bb)// w\n" 13353 ";", 13354 Tab)); 13355 EXPECT_EQ("if (aaa && bbb) // w\n" 13356 "\t;", 13357 format("if(aaa&&bbb)// w\n" 13358 ";", 13359 Tab)); 13360 13361 verifyFormat("class X {\n" 13362 "\tvoid f() {\n" 13363 "\t\tsomeFunction(parameter1,\n" 13364 "\t\t\t parameter2);\n" 13365 "\t}\n" 13366 "};", 13367 Tab); 13368 verifyFormat("#define A \\\n" 13369 "\tvoid f() { \\\n" 13370 "\t\tsomeFunction( \\\n" 13371 "\t\t parameter1, \\\n" 13372 "\t\t parameter2); \\\n" 13373 "\t}", 13374 Tab); 13375 verifyFormat("int a;\t // x\n" 13376 "int bbbbbbbb; // x\n", 13377 Tab); 13378 13379 Tab.TabWidth = 4; 13380 Tab.IndentWidth = 8; 13381 verifyFormat("class TabWidth4Indent8 {\n" 13382 "\t\tvoid f() {\n" 13383 "\t\t\t\tsomeFunction(parameter1,\n" 13384 "\t\t\t\t\t\t\t parameter2);\n" 13385 "\t\t}\n" 13386 "};", 13387 Tab); 13388 13389 Tab.TabWidth = 4; 13390 Tab.IndentWidth = 4; 13391 verifyFormat("class TabWidth4Indent4 {\n" 13392 "\tvoid f() {\n" 13393 "\t\tsomeFunction(parameter1,\n" 13394 "\t\t\t\t\t parameter2);\n" 13395 "\t}\n" 13396 "};", 13397 Tab); 13398 13399 Tab.TabWidth = 8; 13400 Tab.IndentWidth = 4; 13401 verifyFormat("class TabWidth8Indent4 {\n" 13402 " void f() {\n" 13403 "\tsomeFunction(parameter1,\n" 13404 "\t\t parameter2);\n" 13405 " }\n" 13406 "};", 13407 Tab); 13408 13409 Tab.TabWidth = 8; 13410 Tab.IndentWidth = 8; 13411 EXPECT_EQ("/*\n" 13412 "\t a\t\tcomment\n" 13413 "\t in multiple lines\n" 13414 " */", 13415 format(" /*\t \t \n" 13416 " \t \t a\t\tcomment\t \t\n" 13417 " \t \t in multiple lines\t\n" 13418 " \t */", 13419 Tab)); 13420 13421 Tab.UseTab = FormatStyle::UT_ForIndentation; 13422 verifyFormat("{\n" 13423 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13424 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13425 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13426 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13427 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13428 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13429 "};", 13430 Tab); 13431 verifyFormat("enum AA {\n" 13432 "\ta1, // Force multiple lines\n" 13433 "\ta2,\n" 13434 "\ta3\n" 13435 "};", 13436 Tab); 13437 EXPECT_EQ("if (aaaaaaaa && // q\n" 13438 " bb) // w\n" 13439 "\t;", 13440 format("if (aaaaaaaa &&// q\n" 13441 "bb)// w\n" 13442 ";", 13443 Tab)); 13444 verifyFormat("class X {\n" 13445 "\tvoid f() {\n" 13446 "\t\tsomeFunction(parameter1,\n" 13447 "\t\t parameter2);\n" 13448 "\t}\n" 13449 "};", 13450 Tab); 13451 verifyFormat("{\n" 13452 "\tQ(\n" 13453 "\t {\n" 13454 "\t\t int a;\n" 13455 "\t\t someFunction(aaaaaaaa,\n" 13456 "\t\t bbbbbbb);\n" 13457 "\t },\n" 13458 "\t p);\n" 13459 "}", 13460 Tab); 13461 EXPECT_EQ("{\n" 13462 "\t/* aaaa\n" 13463 "\t bbbb */\n" 13464 "}", 13465 format("{\n" 13466 "/* aaaa\n" 13467 " bbbb */\n" 13468 "}", 13469 Tab)); 13470 EXPECT_EQ("{\n" 13471 "\t/*\n" 13472 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13473 "\t bbbbbbbbbbbbb\n" 13474 "\t*/\n" 13475 "}", 13476 format("{\n" 13477 "/*\n" 13478 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13479 "*/\n" 13480 "}", 13481 Tab)); 13482 EXPECT_EQ("{\n" 13483 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13484 "\t// bbbbbbbbbbbbb\n" 13485 "}", 13486 format("{\n" 13487 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13488 "}", 13489 Tab)); 13490 EXPECT_EQ("{\n" 13491 "\t/*\n" 13492 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13493 "\t bbbbbbbbbbbbb\n" 13494 "\t*/\n" 13495 "}", 13496 format("{\n" 13497 "\t/*\n" 13498 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13499 "\t*/\n" 13500 "}", 13501 Tab)); 13502 EXPECT_EQ("{\n" 13503 "\t/*\n" 13504 "\n" 13505 "\t*/\n" 13506 "}", 13507 format("{\n" 13508 "\t/*\n" 13509 "\n" 13510 "\t*/\n" 13511 "}", 13512 Tab)); 13513 EXPECT_EQ("{\n" 13514 "\t/*\n" 13515 " asdf\n" 13516 "\t*/\n" 13517 "}", 13518 format("{\n" 13519 "\t/*\n" 13520 " asdf\n" 13521 "\t*/\n" 13522 "}", 13523 Tab)); 13524 13525 verifyFormat("void f() {\n" 13526 "\treturn true ? aaaaaaaaaaaaaaaaaa\n" 13527 "\t : bbbbbbbbbbbbbbbbbb\n" 13528 "}", 13529 Tab); 13530 FormatStyle TabNoBreak = Tab; 13531 TabNoBreak.BreakBeforeTernaryOperators = false; 13532 verifyFormat("void f() {\n" 13533 "\treturn true ? aaaaaaaaaaaaaaaaaa :\n" 13534 "\t bbbbbbbbbbbbbbbbbb\n" 13535 "}", 13536 TabNoBreak); 13537 verifyFormat("void f() {\n" 13538 "\treturn true ?\n" 13539 "\t aaaaaaaaaaaaaaaaaaaa :\n" 13540 "\t bbbbbbbbbbbbbbbbbbbb\n" 13541 "}", 13542 TabNoBreak); 13543 13544 Tab.UseTab = FormatStyle::UT_Never; 13545 EXPECT_EQ("/*\n" 13546 " a\t\tcomment\n" 13547 " in multiple lines\n" 13548 " */", 13549 format(" /*\t \t \n" 13550 " \t \t a\t\tcomment\t \t\n" 13551 " \t \t in multiple lines\t\n" 13552 " \t */", 13553 Tab)); 13554 EXPECT_EQ("/* some\n" 13555 " comment */", 13556 format(" \t \t /* some\n" 13557 " \t \t comment */", 13558 Tab)); 13559 EXPECT_EQ("int a; /* some\n" 13560 " comment */", 13561 format(" \t \t int a; /* some\n" 13562 " \t \t comment */", 13563 Tab)); 13564 13565 EXPECT_EQ("int a; /* some\n" 13566 "comment */", 13567 format(" \t \t int\ta; /* some\n" 13568 " \t \t comment */", 13569 Tab)); 13570 EXPECT_EQ("f(\"\t\t\"); /* some\n" 13571 " comment */", 13572 format(" \t \t f(\"\t\t\"); /* some\n" 13573 " \t \t comment */", 13574 Tab)); 13575 EXPECT_EQ("{\n" 13576 " /*\n" 13577 " * Comment\n" 13578 " */\n" 13579 " int i;\n" 13580 "}", 13581 format("{\n" 13582 "\t/*\n" 13583 "\t * Comment\n" 13584 "\t */\n" 13585 "\t int i;\n" 13586 "}", 13587 Tab)); 13588 13589 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 13590 Tab.TabWidth = 8; 13591 Tab.IndentWidth = 8; 13592 EXPECT_EQ("if (aaaaaaaa && // q\n" 13593 " bb) // w\n" 13594 "\t;", 13595 format("if (aaaaaaaa &&// q\n" 13596 "bb)// w\n" 13597 ";", 13598 Tab)); 13599 EXPECT_EQ("if (aaa && bbb) // w\n" 13600 "\t;", 13601 format("if(aaa&&bbb)// w\n" 13602 ";", 13603 Tab)); 13604 verifyFormat("class X {\n" 13605 "\tvoid f() {\n" 13606 "\t\tsomeFunction(parameter1,\n" 13607 "\t\t\t parameter2);\n" 13608 "\t}\n" 13609 "};", 13610 Tab); 13611 verifyFormat("#define A \\\n" 13612 "\tvoid f() { \\\n" 13613 "\t\tsomeFunction( \\\n" 13614 "\t\t parameter1, \\\n" 13615 "\t\t parameter2); \\\n" 13616 "\t}", 13617 Tab); 13618 Tab.TabWidth = 4; 13619 Tab.IndentWidth = 8; 13620 verifyFormat("class TabWidth4Indent8 {\n" 13621 "\t\tvoid f() {\n" 13622 "\t\t\t\tsomeFunction(parameter1,\n" 13623 "\t\t\t\t\t\t\t parameter2);\n" 13624 "\t\t}\n" 13625 "};", 13626 Tab); 13627 Tab.TabWidth = 4; 13628 Tab.IndentWidth = 4; 13629 verifyFormat("class TabWidth4Indent4 {\n" 13630 "\tvoid f() {\n" 13631 "\t\tsomeFunction(parameter1,\n" 13632 "\t\t\t\t\t parameter2);\n" 13633 "\t}\n" 13634 "};", 13635 Tab); 13636 Tab.TabWidth = 8; 13637 Tab.IndentWidth = 4; 13638 verifyFormat("class TabWidth8Indent4 {\n" 13639 " void f() {\n" 13640 "\tsomeFunction(parameter1,\n" 13641 "\t\t parameter2);\n" 13642 " }\n" 13643 "};", 13644 Tab); 13645 Tab.TabWidth = 8; 13646 Tab.IndentWidth = 8; 13647 EXPECT_EQ("/*\n" 13648 "\t a\t\tcomment\n" 13649 "\t in multiple lines\n" 13650 " */", 13651 format(" /*\t \t \n" 13652 " \t \t a\t\tcomment\t \t\n" 13653 " \t \t in multiple lines\t\n" 13654 " \t */", 13655 Tab)); 13656 verifyFormat("{\n" 13657 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13658 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13659 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13660 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13661 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13662 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13663 "};", 13664 Tab); 13665 verifyFormat("enum AA {\n" 13666 "\ta1, // Force multiple lines\n" 13667 "\ta2,\n" 13668 "\ta3\n" 13669 "};", 13670 Tab); 13671 EXPECT_EQ("if (aaaaaaaa && // q\n" 13672 " bb) // w\n" 13673 "\t;", 13674 format("if (aaaaaaaa &&// q\n" 13675 "bb)// w\n" 13676 ";", 13677 Tab)); 13678 verifyFormat("class X {\n" 13679 "\tvoid f() {\n" 13680 "\t\tsomeFunction(parameter1,\n" 13681 "\t\t\t parameter2);\n" 13682 "\t}\n" 13683 "};", 13684 Tab); 13685 verifyFormat("{\n" 13686 "\tQ(\n" 13687 "\t {\n" 13688 "\t\t int a;\n" 13689 "\t\t someFunction(aaaaaaaa,\n" 13690 "\t\t\t\t bbbbbbb);\n" 13691 "\t },\n" 13692 "\t p);\n" 13693 "}", 13694 Tab); 13695 EXPECT_EQ("{\n" 13696 "\t/* aaaa\n" 13697 "\t bbbb */\n" 13698 "}", 13699 format("{\n" 13700 "/* aaaa\n" 13701 " bbbb */\n" 13702 "}", 13703 Tab)); 13704 EXPECT_EQ("{\n" 13705 "\t/*\n" 13706 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13707 "\t bbbbbbbbbbbbb\n" 13708 "\t*/\n" 13709 "}", 13710 format("{\n" 13711 "/*\n" 13712 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13713 "*/\n" 13714 "}", 13715 Tab)); 13716 EXPECT_EQ("{\n" 13717 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13718 "\t// bbbbbbbbbbbbb\n" 13719 "}", 13720 format("{\n" 13721 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13722 "}", 13723 Tab)); 13724 EXPECT_EQ("{\n" 13725 "\t/*\n" 13726 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13727 "\t bbbbbbbbbbbbb\n" 13728 "\t*/\n" 13729 "}", 13730 format("{\n" 13731 "\t/*\n" 13732 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13733 "\t*/\n" 13734 "}", 13735 Tab)); 13736 EXPECT_EQ("{\n" 13737 "\t/*\n" 13738 "\n" 13739 "\t*/\n" 13740 "}", 13741 format("{\n" 13742 "\t/*\n" 13743 "\n" 13744 "\t*/\n" 13745 "}", 13746 Tab)); 13747 EXPECT_EQ("{\n" 13748 "\t/*\n" 13749 " asdf\n" 13750 "\t*/\n" 13751 "}", 13752 format("{\n" 13753 "\t/*\n" 13754 " asdf\n" 13755 "\t*/\n" 13756 "}", 13757 Tab)); 13758 EXPECT_EQ("/* some\n" 13759 " comment */", 13760 format(" \t \t /* some\n" 13761 " \t \t comment */", 13762 Tab)); 13763 EXPECT_EQ("int a; /* some\n" 13764 " comment */", 13765 format(" \t \t int a; /* some\n" 13766 " \t \t comment */", 13767 Tab)); 13768 EXPECT_EQ("int a; /* some\n" 13769 "comment */", 13770 format(" \t \t int\ta; /* some\n" 13771 " \t \t comment */", 13772 Tab)); 13773 EXPECT_EQ("f(\"\t\t\"); /* some\n" 13774 " comment */", 13775 format(" \t \t f(\"\t\t\"); /* some\n" 13776 " \t \t comment */", 13777 Tab)); 13778 EXPECT_EQ("{\n" 13779 "\t/*\n" 13780 "\t * Comment\n" 13781 "\t */\n" 13782 "\tint i;\n" 13783 "}", 13784 format("{\n" 13785 "\t/*\n" 13786 "\t * Comment\n" 13787 "\t */\n" 13788 "\t int i;\n" 13789 "}", 13790 Tab)); 13791 Tab.TabWidth = 2; 13792 Tab.IndentWidth = 2; 13793 EXPECT_EQ("{\n" 13794 "\t/* aaaa\n" 13795 "\t\t bbbb */\n" 13796 "}", 13797 format("{\n" 13798 "/* aaaa\n" 13799 "\t bbbb */\n" 13800 "}", 13801 Tab)); 13802 EXPECT_EQ("{\n" 13803 "\t/*\n" 13804 "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13805 "\t\tbbbbbbbbbbbbb\n" 13806 "\t*/\n" 13807 "}", 13808 format("{\n" 13809 "/*\n" 13810 "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13811 "*/\n" 13812 "}", 13813 Tab)); 13814 Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 13815 Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 13816 Tab.TabWidth = 4; 13817 Tab.IndentWidth = 4; 13818 verifyFormat("class Assign {\n" 13819 "\tvoid f() {\n" 13820 "\t\tint x = 123;\n" 13821 "\t\tint random = 4;\n" 13822 "\t\tstd::string alphabet =\n" 13823 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 13824 "\t}\n" 13825 "};", 13826 Tab); 13827 13828 Tab.UseTab = FormatStyle::UT_AlignWithSpaces; 13829 Tab.TabWidth = 8; 13830 Tab.IndentWidth = 8; 13831 EXPECT_EQ("if (aaaaaaaa && // q\n" 13832 " bb) // w\n" 13833 "\t;", 13834 format("if (aaaaaaaa &&// q\n" 13835 "bb)// w\n" 13836 ";", 13837 Tab)); 13838 EXPECT_EQ("if (aaa && bbb) // w\n" 13839 "\t;", 13840 format("if(aaa&&bbb)// w\n" 13841 ";", 13842 Tab)); 13843 verifyFormat("class X {\n" 13844 "\tvoid f() {\n" 13845 "\t\tsomeFunction(parameter1,\n" 13846 "\t\t parameter2);\n" 13847 "\t}\n" 13848 "};", 13849 Tab); 13850 verifyFormat("#define A \\\n" 13851 "\tvoid f() { \\\n" 13852 "\t\tsomeFunction( \\\n" 13853 "\t\t parameter1, \\\n" 13854 "\t\t parameter2); \\\n" 13855 "\t}", 13856 Tab); 13857 Tab.TabWidth = 4; 13858 Tab.IndentWidth = 8; 13859 verifyFormat("class TabWidth4Indent8 {\n" 13860 "\t\tvoid f() {\n" 13861 "\t\t\t\tsomeFunction(parameter1,\n" 13862 "\t\t\t\t parameter2);\n" 13863 "\t\t}\n" 13864 "};", 13865 Tab); 13866 Tab.TabWidth = 4; 13867 Tab.IndentWidth = 4; 13868 verifyFormat("class TabWidth4Indent4 {\n" 13869 "\tvoid f() {\n" 13870 "\t\tsomeFunction(parameter1,\n" 13871 "\t\t parameter2);\n" 13872 "\t}\n" 13873 "};", 13874 Tab); 13875 Tab.TabWidth = 8; 13876 Tab.IndentWidth = 4; 13877 verifyFormat("class TabWidth8Indent4 {\n" 13878 " void f() {\n" 13879 "\tsomeFunction(parameter1,\n" 13880 "\t parameter2);\n" 13881 " }\n" 13882 "};", 13883 Tab); 13884 Tab.TabWidth = 8; 13885 Tab.IndentWidth = 8; 13886 EXPECT_EQ("/*\n" 13887 " a\t\tcomment\n" 13888 " in multiple lines\n" 13889 " */", 13890 format(" /*\t \t \n" 13891 " \t \t a\t\tcomment\t \t\n" 13892 " \t \t in multiple lines\t\n" 13893 " \t */", 13894 Tab)); 13895 verifyFormat("{\n" 13896 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13897 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13898 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13899 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13900 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13901 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13902 "};", 13903 Tab); 13904 verifyFormat("enum AA {\n" 13905 "\ta1, // Force multiple lines\n" 13906 "\ta2,\n" 13907 "\ta3\n" 13908 "};", 13909 Tab); 13910 EXPECT_EQ("if (aaaaaaaa && // q\n" 13911 " bb) // w\n" 13912 "\t;", 13913 format("if (aaaaaaaa &&// q\n" 13914 "bb)// w\n" 13915 ";", 13916 Tab)); 13917 verifyFormat("class X {\n" 13918 "\tvoid f() {\n" 13919 "\t\tsomeFunction(parameter1,\n" 13920 "\t\t parameter2);\n" 13921 "\t}\n" 13922 "};", 13923 Tab); 13924 verifyFormat("{\n" 13925 "\tQ(\n" 13926 "\t {\n" 13927 "\t\t int a;\n" 13928 "\t\t someFunction(aaaaaaaa,\n" 13929 "\t\t bbbbbbb);\n" 13930 "\t },\n" 13931 "\t p);\n" 13932 "}", 13933 Tab); 13934 EXPECT_EQ("{\n" 13935 "\t/* aaaa\n" 13936 "\t bbbb */\n" 13937 "}", 13938 format("{\n" 13939 "/* aaaa\n" 13940 " bbbb */\n" 13941 "}", 13942 Tab)); 13943 EXPECT_EQ("{\n" 13944 "\t/*\n" 13945 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13946 "\t bbbbbbbbbbbbb\n" 13947 "\t*/\n" 13948 "}", 13949 format("{\n" 13950 "/*\n" 13951 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13952 "*/\n" 13953 "}", 13954 Tab)); 13955 EXPECT_EQ("{\n" 13956 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13957 "\t// bbbbbbbbbbbbb\n" 13958 "}", 13959 format("{\n" 13960 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13961 "}", 13962 Tab)); 13963 EXPECT_EQ("{\n" 13964 "\t/*\n" 13965 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13966 "\t bbbbbbbbbbbbb\n" 13967 "\t*/\n" 13968 "}", 13969 format("{\n" 13970 "\t/*\n" 13971 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13972 "\t*/\n" 13973 "}", 13974 Tab)); 13975 EXPECT_EQ("{\n" 13976 "\t/*\n" 13977 "\n" 13978 "\t*/\n" 13979 "}", 13980 format("{\n" 13981 "\t/*\n" 13982 "\n" 13983 "\t*/\n" 13984 "}", 13985 Tab)); 13986 EXPECT_EQ("{\n" 13987 "\t/*\n" 13988 " asdf\n" 13989 "\t*/\n" 13990 "}", 13991 format("{\n" 13992 "\t/*\n" 13993 " asdf\n" 13994 "\t*/\n" 13995 "}", 13996 Tab)); 13997 EXPECT_EQ("/* some\n" 13998 " comment */", 13999 format(" \t \t /* some\n" 14000 " \t \t comment */", 14001 Tab)); 14002 EXPECT_EQ("int a; /* some\n" 14003 " comment */", 14004 format(" \t \t int a; /* some\n" 14005 " \t \t comment */", 14006 Tab)); 14007 EXPECT_EQ("int a; /* some\n" 14008 "comment */", 14009 format(" \t \t int\ta; /* some\n" 14010 " \t \t comment */", 14011 Tab)); 14012 EXPECT_EQ("f(\"\t\t\"); /* some\n" 14013 " comment */", 14014 format(" \t \t f(\"\t\t\"); /* some\n" 14015 " \t \t comment */", 14016 Tab)); 14017 EXPECT_EQ("{\n" 14018 "\t/*\n" 14019 "\t * Comment\n" 14020 "\t */\n" 14021 "\tint i;\n" 14022 "}", 14023 format("{\n" 14024 "\t/*\n" 14025 "\t * Comment\n" 14026 "\t */\n" 14027 "\t int i;\n" 14028 "}", 14029 Tab)); 14030 Tab.TabWidth = 2; 14031 Tab.IndentWidth = 2; 14032 EXPECT_EQ("{\n" 14033 "\t/* aaaa\n" 14034 "\t bbbb */\n" 14035 "}", 14036 format("{\n" 14037 "/* aaaa\n" 14038 " bbbb */\n" 14039 "}", 14040 Tab)); 14041 EXPECT_EQ("{\n" 14042 "\t/*\n" 14043 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14044 "\t bbbbbbbbbbbbb\n" 14045 "\t*/\n" 14046 "}", 14047 format("{\n" 14048 "/*\n" 14049 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14050 "*/\n" 14051 "}", 14052 Tab)); 14053 Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 14054 Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 14055 Tab.TabWidth = 4; 14056 Tab.IndentWidth = 4; 14057 verifyFormat("class Assign {\n" 14058 "\tvoid f() {\n" 14059 "\t\tint x = 123;\n" 14060 "\t\tint random = 4;\n" 14061 "\t\tstd::string alphabet =\n" 14062 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 14063 "\t}\n" 14064 "};", 14065 Tab); 14066 Tab.AlignOperands = FormatStyle::OAS_Align; 14067 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n" 14068 " cccccccccccccccccccc;", 14069 Tab); 14070 // no alignment 14071 verifyFormat("int aaaaaaaaaa =\n" 14072 "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 14073 Tab); 14074 verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n" 14075 " : bbbbbbbbbbbbbb ? 222222222222222\n" 14076 " : 333333333333333;", 14077 Tab); 14078 Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 14079 Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator; 14080 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n" 14081 " + cccccccccccccccccccc;", 14082 Tab); 14083 } 14084 14085 TEST_F(FormatTest, ZeroTabWidth) { 14086 FormatStyle Tab = getLLVMStyleWithColumns(42); 14087 Tab.IndentWidth = 8; 14088 Tab.UseTab = FormatStyle::UT_Never; 14089 Tab.TabWidth = 0; 14090 EXPECT_EQ("void a(){\n" 14091 " // line starts with '\t'\n" 14092 "};", 14093 format("void a(){\n" 14094 "\t// line starts with '\t'\n" 14095 "};", 14096 Tab)); 14097 14098 EXPECT_EQ("void a(){\n" 14099 " // line starts with '\t'\n" 14100 "};", 14101 format("void a(){\n" 14102 "\t\t// line starts with '\t'\n" 14103 "};", 14104 Tab)); 14105 14106 Tab.UseTab = FormatStyle::UT_ForIndentation; 14107 EXPECT_EQ("void a(){\n" 14108 " // line starts with '\t'\n" 14109 "};", 14110 format("void a(){\n" 14111 "\t// line starts with '\t'\n" 14112 "};", 14113 Tab)); 14114 14115 EXPECT_EQ("void a(){\n" 14116 " // line starts with '\t'\n" 14117 "};", 14118 format("void a(){\n" 14119 "\t\t// line starts with '\t'\n" 14120 "};", 14121 Tab)); 14122 14123 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 14124 EXPECT_EQ("void a(){\n" 14125 " // line starts with '\t'\n" 14126 "};", 14127 format("void a(){\n" 14128 "\t// line starts with '\t'\n" 14129 "};", 14130 Tab)); 14131 14132 EXPECT_EQ("void a(){\n" 14133 " // line starts with '\t'\n" 14134 "};", 14135 format("void a(){\n" 14136 "\t\t// line starts with '\t'\n" 14137 "};", 14138 Tab)); 14139 14140 Tab.UseTab = FormatStyle::UT_AlignWithSpaces; 14141 EXPECT_EQ("void a(){\n" 14142 " // line starts with '\t'\n" 14143 "};", 14144 format("void a(){\n" 14145 "\t// line starts with '\t'\n" 14146 "};", 14147 Tab)); 14148 14149 EXPECT_EQ("void a(){\n" 14150 " // line starts with '\t'\n" 14151 "};", 14152 format("void a(){\n" 14153 "\t\t// line starts with '\t'\n" 14154 "};", 14155 Tab)); 14156 14157 Tab.UseTab = FormatStyle::UT_Always; 14158 EXPECT_EQ("void a(){\n" 14159 "// line starts with '\t'\n" 14160 "};", 14161 format("void a(){\n" 14162 "\t// line starts with '\t'\n" 14163 "};", 14164 Tab)); 14165 14166 EXPECT_EQ("void a(){\n" 14167 "// line starts with '\t'\n" 14168 "};", 14169 format("void a(){\n" 14170 "\t\t// line starts with '\t'\n" 14171 "};", 14172 Tab)); 14173 } 14174 14175 TEST_F(FormatTest, CalculatesOriginalColumn) { 14176 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14177 "q\"; /* some\n" 14178 " comment */", 14179 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14180 "q\"; /* some\n" 14181 " comment */", 14182 getLLVMStyle())); 14183 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 14184 "/* some\n" 14185 " comment */", 14186 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 14187 " /* some\n" 14188 " comment */", 14189 getLLVMStyle())); 14190 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14191 "qqq\n" 14192 "/* some\n" 14193 " comment */", 14194 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14195 "qqq\n" 14196 " /* some\n" 14197 " comment */", 14198 getLLVMStyle())); 14199 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14200 "wwww; /* some\n" 14201 " comment */", 14202 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14203 "wwww; /* some\n" 14204 " comment */", 14205 getLLVMStyle())); 14206 } 14207 14208 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 14209 FormatStyle NoSpace = getLLVMStyle(); 14210 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 14211 14212 verifyFormat("while(true)\n" 14213 " continue;", 14214 NoSpace); 14215 verifyFormat("for(;;)\n" 14216 " continue;", 14217 NoSpace); 14218 verifyFormat("if(true)\n" 14219 " f();\n" 14220 "else if(true)\n" 14221 " f();", 14222 NoSpace); 14223 verifyFormat("do {\n" 14224 " do_something();\n" 14225 "} while(something());", 14226 NoSpace); 14227 verifyFormat("switch(x) {\n" 14228 "default:\n" 14229 " break;\n" 14230 "}", 14231 NoSpace); 14232 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 14233 verifyFormat("size_t x = sizeof(x);", NoSpace); 14234 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 14235 verifyFormat("auto f(int x) -> typeof(x);", NoSpace); 14236 verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace); 14237 verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace); 14238 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 14239 verifyFormat("alignas(128) char a[128];", NoSpace); 14240 verifyFormat("size_t x = alignof(MyType);", NoSpace); 14241 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 14242 verifyFormat("int f() throw(Deprecated);", NoSpace); 14243 verifyFormat("typedef void (*cb)(int);", NoSpace); 14244 verifyFormat("T A::operator()();", NoSpace); 14245 verifyFormat("X A::operator++(T);", NoSpace); 14246 verifyFormat("auto lambda = []() { return 0; };", NoSpace); 14247 14248 FormatStyle Space = getLLVMStyle(); 14249 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 14250 14251 verifyFormat("int f ();", Space); 14252 verifyFormat("void f (int a, T b) {\n" 14253 " while (true)\n" 14254 " continue;\n" 14255 "}", 14256 Space); 14257 verifyFormat("if (true)\n" 14258 " f ();\n" 14259 "else if (true)\n" 14260 " f ();", 14261 Space); 14262 verifyFormat("do {\n" 14263 " do_something ();\n" 14264 "} while (something ());", 14265 Space); 14266 verifyFormat("switch (x) {\n" 14267 "default:\n" 14268 " break;\n" 14269 "}", 14270 Space); 14271 verifyFormat("A::A () : a (1) {}", Space); 14272 verifyFormat("void f () __attribute__ ((asdf));", Space); 14273 verifyFormat("*(&a + 1);\n" 14274 "&((&a)[1]);\n" 14275 "a[(b + c) * d];\n" 14276 "(((a + 1) * 2) + 3) * 4;", 14277 Space); 14278 verifyFormat("#define A(x) x", Space); 14279 verifyFormat("#define A (x) x", Space); 14280 verifyFormat("#if defined(x)\n" 14281 "#endif", 14282 Space); 14283 verifyFormat("auto i = std::make_unique<int> (5);", Space); 14284 verifyFormat("size_t x = sizeof (x);", Space); 14285 verifyFormat("auto f (int x) -> decltype (x);", Space); 14286 verifyFormat("auto f (int x) -> typeof (x);", Space); 14287 verifyFormat("auto f (int x) -> _Atomic (x);", Space); 14288 verifyFormat("auto f (int x) -> __underlying_type (x);", Space); 14289 verifyFormat("int f (T x) noexcept (x.create ());", Space); 14290 verifyFormat("alignas (128) char a[128];", Space); 14291 verifyFormat("size_t x = alignof (MyType);", Space); 14292 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 14293 verifyFormat("int f () throw (Deprecated);", Space); 14294 verifyFormat("typedef void (*cb) (int);", Space); 14295 // FIXME these tests regressed behaviour. 14296 // verifyFormat("T A::operator() ();", Space); 14297 // verifyFormat("X A::operator++ (T);", Space); 14298 verifyFormat("auto lambda = [] () { return 0; };", Space); 14299 verifyFormat("int x = int (y);", Space); 14300 14301 FormatStyle SomeSpace = getLLVMStyle(); 14302 SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses; 14303 14304 verifyFormat("[]() -> float {}", SomeSpace); 14305 verifyFormat("[] (auto foo) {}", SomeSpace); 14306 verifyFormat("[foo]() -> int {}", SomeSpace); 14307 verifyFormat("int f();", SomeSpace); 14308 verifyFormat("void f (int a, T b) {\n" 14309 " while (true)\n" 14310 " continue;\n" 14311 "}", 14312 SomeSpace); 14313 verifyFormat("if (true)\n" 14314 " f();\n" 14315 "else if (true)\n" 14316 " f();", 14317 SomeSpace); 14318 verifyFormat("do {\n" 14319 " do_something();\n" 14320 "} while (something());", 14321 SomeSpace); 14322 verifyFormat("switch (x) {\n" 14323 "default:\n" 14324 " break;\n" 14325 "}", 14326 SomeSpace); 14327 verifyFormat("A::A() : a (1) {}", SomeSpace); 14328 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace); 14329 verifyFormat("*(&a + 1);\n" 14330 "&((&a)[1]);\n" 14331 "a[(b + c) * d];\n" 14332 "(((a + 1) * 2) + 3) * 4;", 14333 SomeSpace); 14334 verifyFormat("#define A(x) x", SomeSpace); 14335 verifyFormat("#define A (x) x", SomeSpace); 14336 verifyFormat("#if defined(x)\n" 14337 "#endif", 14338 SomeSpace); 14339 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace); 14340 verifyFormat("size_t x = sizeof (x);", SomeSpace); 14341 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace); 14342 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace); 14343 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace); 14344 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace); 14345 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace); 14346 verifyFormat("alignas (128) char a[128];", SomeSpace); 14347 verifyFormat("size_t x = alignof (MyType);", SomeSpace); 14348 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", 14349 SomeSpace); 14350 verifyFormat("int f() throw (Deprecated);", SomeSpace); 14351 verifyFormat("typedef void (*cb) (int);", SomeSpace); 14352 verifyFormat("T A::operator()();", SomeSpace); 14353 // FIXME these tests regressed behaviour. 14354 // verifyFormat("X A::operator++ (T);", SomeSpace); 14355 verifyFormat("int x = int (y);", SomeSpace); 14356 verifyFormat("auto lambda = []() { return 0; };", SomeSpace); 14357 14358 FormatStyle SpaceControlStatements = getLLVMStyle(); 14359 SpaceControlStatements.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14360 SpaceControlStatements.SpaceBeforeParensOptions.AfterControlStatements = true; 14361 14362 verifyFormat("while (true)\n" 14363 " continue;", 14364 SpaceControlStatements); 14365 verifyFormat("if (true)\n" 14366 " f();\n" 14367 "else if (true)\n" 14368 " f();", 14369 SpaceControlStatements); 14370 verifyFormat("for (;;) {\n" 14371 " do_something();\n" 14372 "}", 14373 SpaceControlStatements); 14374 verifyFormat("do {\n" 14375 " do_something();\n" 14376 "} while (something());", 14377 SpaceControlStatements); 14378 verifyFormat("switch (x) {\n" 14379 "default:\n" 14380 " break;\n" 14381 "}", 14382 SpaceControlStatements); 14383 14384 FormatStyle SpaceFuncDecl = getLLVMStyle(); 14385 SpaceFuncDecl.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14386 SpaceFuncDecl.SpaceBeforeParensOptions.AfterFunctionDeclarationName = true; 14387 14388 verifyFormat("int f ();", SpaceFuncDecl); 14389 verifyFormat("void f(int a, T b) {}", SpaceFuncDecl); 14390 verifyFormat("A::A() : a(1) {}", SpaceFuncDecl); 14391 verifyFormat("void f () __attribute__((asdf));", SpaceFuncDecl); 14392 verifyFormat("#define A(x) x", SpaceFuncDecl); 14393 verifyFormat("#define A (x) x", SpaceFuncDecl); 14394 verifyFormat("#if defined(x)\n" 14395 "#endif", 14396 SpaceFuncDecl); 14397 verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDecl); 14398 verifyFormat("size_t x = sizeof(x);", SpaceFuncDecl); 14399 verifyFormat("auto f (int x) -> decltype(x);", SpaceFuncDecl); 14400 verifyFormat("auto f (int x) -> typeof(x);", SpaceFuncDecl); 14401 verifyFormat("auto f (int x) -> _Atomic(x);", SpaceFuncDecl); 14402 verifyFormat("auto f (int x) -> __underlying_type(x);", SpaceFuncDecl); 14403 verifyFormat("int f (T x) noexcept(x.create());", SpaceFuncDecl); 14404 verifyFormat("alignas(128) char a[128];", SpaceFuncDecl); 14405 verifyFormat("size_t x = alignof(MyType);", SpaceFuncDecl); 14406 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", 14407 SpaceFuncDecl); 14408 verifyFormat("int f () throw(Deprecated);", SpaceFuncDecl); 14409 verifyFormat("typedef void (*cb)(int);", SpaceFuncDecl); 14410 // FIXME these tests regressed behaviour. 14411 // verifyFormat("T A::operator() ();", SpaceFuncDecl); 14412 // verifyFormat("X A::operator++ (T);", SpaceFuncDecl); 14413 verifyFormat("T A::operator()() {}", SpaceFuncDecl); 14414 verifyFormat("auto lambda = []() { return 0; };", SpaceFuncDecl); 14415 verifyFormat("int x = int(y);", SpaceFuncDecl); 14416 verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}", 14417 SpaceFuncDecl); 14418 14419 FormatStyle SpaceFuncDef = getLLVMStyle(); 14420 SpaceFuncDef.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14421 SpaceFuncDef.SpaceBeforeParensOptions.AfterFunctionDefinitionName = true; 14422 14423 verifyFormat("int f();", SpaceFuncDef); 14424 verifyFormat("void f (int a, T b) {}", SpaceFuncDef); 14425 verifyFormat("A::A() : a(1) {}", SpaceFuncDef); 14426 verifyFormat("void f() __attribute__((asdf));", SpaceFuncDef); 14427 verifyFormat("#define A(x) x", SpaceFuncDef); 14428 verifyFormat("#define A (x) x", SpaceFuncDef); 14429 verifyFormat("#if defined(x)\n" 14430 "#endif", 14431 SpaceFuncDef); 14432 verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDef); 14433 verifyFormat("size_t x = sizeof(x);", SpaceFuncDef); 14434 verifyFormat("auto f(int x) -> decltype(x);", SpaceFuncDef); 14435 verifyFormat("auto f(int x) -> typeof(x);", SpaceFuncDef); 14436 verifyFormat("auto f(int x) -> _Atomic(x);", SpaceFuncDef); 14437 verifyFormat("auto f(int x) -> __underlying_type(x);", SpaceFuncDef); 14438 verifyFormat("int f(T x) noexcept(x.create());", SpaceFuncDef); 14439 verifyFormat("alignas(128) char a[128];", SpaceFuncDef); 14440 verifyFormat("size_t x = alignof(MyType);", SpaceFuncDef); 14441 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", 14442 SpaceFuncDef); 14443 verifyFormat("int f() throw(Deprecated);", SpaceFuncDef); 14444 verifyFormat("typedef void (*cb)(int);", SpaceFuncDef); 14445 verifyFormat("T A::operator()();", SpaceFuncDef); 14446 verifyFormat("X A::operator++(T);", SpaceFuncDef); 14447 // verifyFormat("T A::operator() () {}", SpaceFuncDef); 14448 verifyFormat("auto lambda = [] () { return 0; };", SpaceFuncDef); 14449 verifyFormat("int x = int(y);", SpaceFuncDef); 14450 verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}", 14451 SpaceFuncDef); 14452 14453 FormatStyle SpaceIfMacros = getLLVMStyle(); 14454 SpaceIfMacros.IfMacros.clear(); 14455 SpaceIfMacros.IfMacros.push_back("MYIF"); 14456 SpaceIfMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14457 SpaceIfMacros.SpaceBeforeParensOptions.AfterIfMacros = true; 14458 verifyFormat("MYIF (a)\n return;", SpaceIfMacros); 14459 verifyFormat("MYIF (a)\n return;\nelse MYIF (b)\n return;", SpaceIfMacros); 14460 verifyFormat("MYIF (a)\n return;\nelse\n return;", SpaceIfMacros); 14461 14462 FormatStyle SpaceForeachMacros = getLLVMStyle(); 14463 SpaceForeachMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14464 SpaceForeachMacros.SpaceBeforeParensOptions.AfterForeachMacros = true; 14465 verifyFormat("foreach (Item *item, itemlist) {}", SpaceForeachMacros); 14466 verifyFormat("Q_FOREACH (Item *item, itemlist) {}", SpaceForeachMacros); 14467 verifyFormat("BOOST_FOREACH (Item *item, itemlist) {}", SpaceForeachMacros); 14468 verifyFormat("UNKNOWN_FOREACH(Item *item, itemlist) {}", SpaceForeachMacros); 14469 14470 FormatStyle SomeSpace2 = getLLVMStyle(); 14471 SomeSpace2.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14472 SomeSpace2.SpaceBeforeParensOptions.BeforeNonEmptyParentheses = true; 14473 verifyFormat("[]() -> float {}", SomeSpace2); 14474 verifyFormat("[] (auto foo) {}", SomeSpace2); 14475 verifyFormat("[foo]() -> int {}", SomeSpace2); 14476 verifyFormat("int f();", SomeSpace2); 14477 verifyFormat("void f (int a, T b) {\n" 14478 " while (true)\n" 14479 " continue;\n" 14480 "}", 14481 SomeSpace2); 14482 verifyFormat("if (true)\n" 14483 " f();\n" 14484 "else if (true)\n" 14485 " f();", 14486 SomeSpace2); 14487 verifyFormat("do {\n" 14488 " do_something();\n" 14489 "} while (something());", 14490 SomeSpace2); 14491 verifyFormat("switch (x) {\n" 14492 "default:\n" 14493 " break;\n" 14494 "}", 14495 SomeSpace2); 14496 verifyFormat("A::A() : a (1) {}", SomeSpace2); 14497 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace2); 14498 verifyFormat("*(&a + 1);\n" 14499 "&((&a)[1]);\n" 14500 "a[(b + c) * d];\n" 14501 "(((a + 1) * 2) + 3) * 4;", 14502 SomeSpace2); 14503 verifyFormat("#define A(x) x", SomeSpace2); 14504 verifyFormat("#define A (x) x", SomeSpace2); 14505 verifyFormat("#if defined(x)\n" 14506 "#endif", 14507 SomeSpace2); 14508 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace2); 14509 verifyFormat("size_t x = sizeof (x);", SomeSpace2); 14510 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace2); 14511 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace2); 14512 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace2); 14513 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace2); 14514 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace2); 14515 verifyFormat("alignas (128) char a[128];", SomeSpace2); 14516 verifyFormat("size_t x = alignof (MyType);", SomeSpace2); 14517 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", 14518 SomeSpace2); 14519 verifyFormat("int f() throw (Deprecated);", SomeSpace2); 14520 verifyFormat("typedef void (*cb) (int);", SomeSpace2); 14521 verifyFormat("T A::operator()();", SomeSpace2); 14522 // verifyFormat("X A::operator++ (T);", SomeSpace2); 14523 verifyFormat("int x = int (y);", SomeSpace2); 14524 verifyFormat("auto lambda = []() { return 0; };", SomeSpace2); 14525 } 14526 14527 TEST_F(FormatTest, SpaceAfterLogicalNot) { 14528 FormatStyle Spaces = getLLVMStyle(); 14529 Spaces.SpaceAfterLogicalNot = true; 14530 14531 verifyFormat("bool x = ! y", Spaces); 14532 verifyFormat("if (! isFailure())", Spaces); 14533 verifyFormat("if (! (a && b))", Spaces); 14534 verifyFormat("\"Error!\"", Spaces); 14535 verifyFormat("! ! x", Spaces); 14536 } 14537 14538 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 14539 FormatStyle Spaces = getLLVMStyle(); 14540 14541 Spaces.SpacesInParentheses = true; 14542 verifyFormat("do_something( ::globalVar );", Spaces); 14543 verifyFormat("call( x, y, z );", Spaces); 14544 verifyFormat("call();", Spaces); 14545 verifyFormat("std::function<void( int, int )> callback;", Spaces); 14546 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 14547 Spaces); 14548 verifyFormat("while ( (bool)1 )\n" 14549 " continue;", 14550 Spaces); 14551 verifyFormat("for ( ;; )\n" 14552 " continue;", 14553 Spaces); 14554 verifyFormat("if ( true )\n" 14555 " f();\n" 14556 "else if ( true )\n" 14557 " f();", 14558 Spaces); 14559 verifyFormat("do {\n" 14560 " do_something( (int)i );\n" 14561 "} while ( something() );", 14562 Spaces); 14563 verifyFormat("switch ( x ) {\n" 14564 "default:\n" 14565 " break;\n" 14566 "}", 14567 Spaces); 14568 14569 Spaces.SpacesInParentheses = false; 14570 Spaces.SpacesInCStyleCastParentheses = true; 14571 verifyFormat("Type *A = ( Type * )P;", Spaces); 14572 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 14573 verifyFormat("x = ( int32 )y;", Spaces); 14574 verifyFormat("int a = ( int )(2.0f);", Spaces); 14575 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 14576 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 14577 verifyFormat("#define x (( int )-1)", Spaces); 14578 14579 // Run the first set of tests again with: 14580 Spaces.SpacesInParentheses = false; 14581 Spaces.SpaceInEmptyParentheses = true; 14582 Spaces.SpacesInCStyleCastParentheses = true; 14583 verifyFormat("call(x, y, z);", Spaces); 14584 verifyFormat("call( );", Spaces); 14585 verifyFormat("std::function<void(int, int)> callback;", Spaces); 14586 verifyFormat("while (( bool )1)\n" 14587 " continue;", 14588 Spaces); 14589 verifyFormat("for (;;)\n" 14590 " continue;", 14591 Spaces); 14592 verifyFormat("if (true)\n" 14593 " f( );\n" 14594 "else if (true)\n" 14595 " f( );", 14596 Spaces); 14597 verifyFormat("do {\n" 14598 " do_something(( int )i);\n" 14599 "} while (something( ));", 14600 Spaces); 14601 verifyFormat("switch (x) {\n" 14602 "default:\n" 14603 " break;\n" 14604 "}", 14605 Spaces); 14606 14607 // Run the first set of tests again with: 14608 Spaces.SpaceAfterCStyleCast = true; 14609 verifyFormat("call(x, y, z);", Spaces); 14610 verifyFormat("call( );", Spaces); 14611 verifyFormat("std::function<void(int, int)> callback;", Spaces); 14612 verifyFormat("while (( bool ) 1)\n" 14613 " continue;", 14614 Spaces); 14615 verifyFormat("for (;;)\n" 14616 " continue;", 14617 Spaces); 14618 verifyFormat("if (true)\n" 14619 " f( );\n" 14620 "else if (true)\n" 14621 " f( );", 14622 Spaces); 14623 verifyFormat("do {\n" 14624 " do_something(( int ) i);\n" 14625 "} while (something( ));", 14626 Spaces); 14627 verifyFormat("switch (x) {\n" 14628 "default:\n" 14629 " break;\n" 14630 "}", 14631 Spaces); 14632 14633 // Run subset of tests again with: 14634 Spaces.SpacesInCStyleCastParentheses = false; 14635 Spaces.SpaceAfterCStyleCast = true; 14636 verifyFormat("while ((bool) 1)\n" 14637 " continue;", 14638 Spaces); 14639 verifyFormat("do {\n" 14640 " do_something((int) i);\n" 14641 "} while (something( ));", 14642 Spaces); 14643 14644 verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces); 14645 verifyFormat("size_t idx = (size_t) a;", Spaces); 14646 verifyFormat("size_t idx = (size_t) (a - 1);", Spaces); 14647 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); 14648 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); 14649 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); 14650 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); 14651 Spaces.ColumnLimit = 80; 14652 Spaces.IndentWidth = 4; 14653 Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 14654 verifyFormat("void foo( ) {\n" 14655 " size_t foo = (*(function))(\n" 14656 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " 14657 "BarrrrrrrrrrrrLong,\n" 14658 " FoooooooooLooooong);\n" 14659 "}", 14660 Spaces); 14661 Spaces.SpaceAfterCStyleCast = false; 14662 verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces); 14663 verifyFormat("size_t idx = (size_t)a;", Spaces); 14664 verifyFormat("size_t idx = (size_t)(a - 1);", Spaces); 14665 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); 14666 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); 14667 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); 14668 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); 14669 14670 verifyFormat("void foo( ) {\n" 14671 " size_t foo = (*(function))(\n" 14672 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " 14673 "BarrrrrrrrrrrrLong,\n" 14674 " FoooooooooLooooong);\n" 14675 "}", 14676 Spaces); 14677 } 14678 14679 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 14680 verifyFormat("int a[5];"); 14681 verifyFormat("a[3] += 42;"); 14682 14683 FormatStyle Spaces = getLLVMStyle(); 14684 Spaces.SpacesInSquareBrackets = true; 14685 // Not lambdas. 14686 verifyFormat("int a[ 5 ];", Spaces); 14687 verifyFormat("a[ 3 ] += 42;", Spaces); 14688 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 14689 verifyFormat("double &operator[](int i) { return 0; }\n" 14690 "int i;", 14691 Spaces); 14692 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 14693 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 14694 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 14695 // Lambdas. 14696 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 14697 verifyFormat("return [ i, args... ] {};", Spaces); 14698 verifyFormat("int foo = [ &bar ]() {};", Spaces); 14699 verifyFormat("int foo = [ = ]() {};", Spaces); 14700 verifyFormat("int foo = [ & ]() {};", Spaces); 14701 verifyFormat("int foo = [ =, &bar ]() {};", Spaces); 14702 verifyFormat("int foo = [ &bar, = ]() {};", Spaces); 14703 } 14704 14705 TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) { 14706 FormatStyle NoSpaceStyle = getLLVMStyle(); 14707 verifyFormat("int a[5];", NoSpaceStyle); 14708 verifyFormat("a[3] += 42;", NoSpaceStyle); 14709 14710 verifyFormat("int a[1];", NoSpaceStyle); 14711 verifyFormat("int 1 [a];", NoSpaceStyle); 14712 verifyFormat("int a[1][2];", NoSpaceStyle); 14713 verifyFormat("a[7] = 5;", NoSpaceStyle); 14714 verifyFormat("int a = (f())[23];", NoSpaceStyle); 14715 verifyFormat("f([] {})", NoSpaceStyle); 14716 14717 FormatStyle Space = getLLVMStyle(); 14718 Space.SpaceBeforeSquareBrackets = true; 14719 verifyFormat("int c = []() -> int { return 2; }();\n", Space); 14720 verifyFormat("return [i, args...] {};", Space); 14721 14722 verifyFormat("int a [5];", Space); 14723 verifyFormat("a [3] += 42;", Space); 14724 verifyFormat("constexpr char hello []{\"hello\"};", Space); 14725 verifyFormat("double &operator[](int i) { return 0; }\n" 14726 "int i;", 14727 Space); 14728 verifyFormat("std::unique_ptr<int []> foo() {}", Space); 14729 verifyFormat("int i = a [a][a]->f();", Space); 14730 verifyFormat("int i = (*b) [a]->f();", Space); 14731 14732 verifyFormat("int a [1];", Space); 14733 verifyFormat("int 1 [a];", Space); 14734 verifyFormat("int a [1][2];", Space); 14735 verifyFormat("a [7] = 5;", Space); 14736 verifyFormat("int a = (f()) [23];", Space); 14737 verifyFormat("f([] {})", Space); 14738 } 14739 14740 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 14741 verifyFormat("int a = 5;"); 14742 verifyFormat("a += 42;"); 14743 verifyFormat("a or_eq 8;"); 14744 14745 FormatStyle Spaces = getLLVMStyle(); 14746 Spaces.SpaceBeforeAssignmentOperators = false; 14747 verifyFormat("int a= 5;", Spaces); 14748 verifyFormat("a+= 42;", Spaces); 14749 verifyFormat("a or_eq 8;", Spaces); 14750 } 14751 14752 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) { 14753 verifyFormat("class Foo : public Bar {};"); 14754 verifyFormat("Foo::Foo() : foo(1) {}"); 14755 verifyFormat("for (auto a : b) {\n}"); 14756 verifyFormat("int x = a ? b : c;"); 14757 verifyFormat("{\n" 14758 "label0:\n" 14759 " int x = 0;\n" 14760 "}"); 14761 verifyFormat("switch (x) {\n" 14762 "case 1:\n" 14763 "default:\n" 14764 "}"); 14765 verifyFormat("switch (allBraces) {\n" 14766 "case 1: {\n" 14767 " break;\n" 14768 "}\n" 14769 "case 2: {\n" 14770 " [[fallthrough]];\n" 14771 "}\n" 14772 "default: {\n" 14773 " break;\n" 14774 "}\n" 14775 "}"); 14776 14777 FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30); 14778 CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false; 14779 verifyFormat("class Foo : public Bar {};", CtorInitializerStyle); 14780 verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle); 14781 verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle); 14782 verifyFormat("int x = a ? b : c;", CtorInitializerStyle); 14783 verifyFormat("{\n" 14784 "label1:\n" 14785 " int x = 0;\n" 14786 "}", 14787 CtorInitializerStyle); 14788 verifyFormat("switch (x) {\n" 14789 "case 1:\n" 14790 "default:\n" 14791 "}", 14792 CtorInitializerStyle); 14793 verifyFormat("switch (allBraces) {\n" 14794 "case 1: {\n" 14795 " break;\n" 14796 "}\n" 14797 "case 2: {\n" 14798 " [[fallthrough]];\n" 14799 "}\n" 14800 "default: {\n" 14801 " break;\n" 14802 "}\n" 14803 "}", 14804 CtorInitializerStyle); 14805 CtorInitializerStyle.BreakConstructorInitializers = 14806 FormatStyle::BCIS_AfterColon; 14807 verifyFormat("Fooooooooooo::Fooooooooooo():\n" 14808 " aaaaaaaaaaaaaaaa(1),\n" 14809 " bbbbbbbbbbbbbbbb(2) {}", 14810 CtorInitializerStyle); 14811 CtorInitializerStyle.BreakConstructorInitializers = 14812 FormatStyle::BCIS_BeforeComma; 14813 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 14814 " : aaaaaaaaaaaaaaaa(1)\n" 14815 " , bbbbbbbbbbbbbbbb(2) {}", 14816 CtorInitializerStyle); 14817 CtorInitializerStyle.BreakConstructorInitializers = 14818 FormatStyle::BCIS_BeforeColon; 14819 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 14820 " : aaaaaaaaaaaaaaaa(1),\n" 14821 " bbbbbbbbbbbbbbbb(2) {}", 14822 CtorInitializerStyle); 14823 CtorInitializerStyle.ConstructorInitializerIndentWidth = 0; 14824 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 14825 ": aaaaaaaaaaaaaaaa(1),\n" 14826 " bbbbbbbbbbbbbbbb(2) {}", 14827 CtorInitializerStyle); 14828 14829 FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30); 14830 InheritanceStyle.SpaceBeforeInheritanceColon = false; 14831 verifyFormat("class Foo: public Bar {};", InheritanceStyle); 14832 verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle); 14833 verifyFormat("for (auto a : b) {\n}", InheritanceStyle); 14834 verifyFormat("int x = a ? b : c;", InheritanceStyle); 14835 verifyFormat("{\n" 14836 "label2:\n" 14837 " int x = 0;\n" 14838 "}", 14839 InheritanceStyle); 14840 verifyFormat("switch (x) {\n" 14841 "case 1:\n" 14842 "default:\n" 14843 "}", 14844 InheritanceStyle); 14845 verifyFormat("switch (allBraces) {\n" 14846 "case 1: {\n" 14847 " break;\n" 14848 "}\n" 14849 "case 2: {\n" 14850 " [[fallthrough]];\n" 14851 "}\n" 14852 "default: {\n" 14853 " break;\n" 14854 "}\n" 14855 "}", 14856 InheritanceStyle); 14857 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma; 14858 verifyFormat("class Foooooooooooooooooooooo\n" 14859 " : public aaaaaaaaaaaaaaaaaa,\n" 14860 " public bbbbbbbbbbbbbbbbbb {\n" 14861 "}", 14862 InheritanceStyle); 14863 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon; 14864 verifyFormat("class Foooooooooooooooooooooo:\n" 14865 " public aaaaaaaaaaaaaaaaaa,\n" 14866 " public bbbbbbbbbbbbbbbbbb {\n" 14867 "}", 14868 InheritanceStyle); 14869 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 14870 verifyFormat("class Foooooooooooooooooooooo\n" 14871 " : public aaaaaaaaaaaaaaaaaa\n" 14872 " , public bbbbbbbbbbbbbbbbbb {\n" 14873 "}", 14874 InheritanceStyle); 14875 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 14876 verifyFormat("class Foooooooooooooooooooooo\n" 14877 " : public aaaaaaaaaaaaaaaaaa,\n" 14878 " public bbbbbbbbbbbbbbbbbb {\n" 14879 "}", 14880 InheritanceStyle); 14881 InheritanceStyle.ConstructorInitializerIndentWidth = 0; 14882 verifyFormat("class Foooooooooooooooooooooo\n" 14883 ": public aaaaaaaaaaaaaaaaaa,\n" 14884 " public bbbbbbbbbbbbbbbbbb {}", 14885 InheritanceStyle); 14886 14887 FormatStyle ForLoopStyle = getLLVMStyle(); 14888 ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false; 14889 verifyFormat("class Foo : public Bar {};", ForLoopStyle); 14890 verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle); 14891 verifyFormat("for (auto a: b) {\n}", ForLoopStyle); 14892 verifyFormat("int x = a ? b : c;", ForLoopStyle); 14893 verifyFormat("{\n" 14894 "label2:\n" 14895 " int x = 0;\n" 14896 "}", 14897 ForLoopStyle); 14898 verifyFormat("switch (x) {\n" 14899 "case 1:\n" 14900 "default:\n" 14901 "}", 14902 ForLoopStyle); 14903 verifyFormat("switch (allBraces) {\n" 14904 "case 1: {\n" 14905 " break;\n" 14906 "}\n" 14907 "case 2: {\n" 14908 " [[fallthrough]];\n" 14909 "}\n" 14910 "default: {\n" 14911 " break;\n" 14912 "}\n" 14913 "}", 14914 ForLoopStyle); 14915 14916 FormatStyle CaseStyle = getLLVMStyle(); 14917 CaseStyle.SpaceBeforeCaseColon = true; 14918 verifyFormat("class Foo : public Bar {};", CaseStyle); 14919 verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle); 14920 verifyFormat("for (auto a : b) {\n}", CaseStyle); 14921 verifyFormat("int x = a ? b : c;", CaseStyle); 14922 verifyFormat("switch (x) {\n" 14923 "case 1 :\n" 14924 "default :\n" 14925 "}", 14926 CaseStyle); 14927 verifyFormat("switch (allBraces) {\n" 14928 "case 1 : {\n" 14929 " break;\n" 14930 "}\n" 14931 "case 2 : {\n" 14932 " [[fallthrough]];\n" 14933 "}\n" 14934 "default : {\n" 14935 " break;\n" 14936 "}\n" 14937 "}", 14938 CaseStyle); 14939 14940 FormatStyle NoSpaceStyle = getLLVMStyle(); 14941 EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false); 14942 NoSpaceStyle.SpaceBeforeCtorInitializerColon = false; 14943 NoSpaceStyle.SpaceBeforeInheritanceColon = false; 14944 NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 14945 verifyFormat("class Foo: public Bar {};", NoSpaceStyle); 14946 verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle); 14947 verifyFormat("for (auto a: b) {\n}", NoSpaceStyle); 14948 verifyFormat("int x = a ? b : c;", NoSpaceStyle); 14949 verifyFormat("{\n" 14950 "label3:\n" 14951 " int x = 0;\n" 14952 "}", 14953 NoSpaceStyle); 14954 verifyFormat("switch (x) {\n" 14955 "case 1:\n" 14956 "default:\n" 14957 "}", 14958 NoSpaceStyle); 14959 verifyFormat("switch (allBraces) {\n" 14960 "case 1: {\n" 14961 " break;\n" 14962 "}\n" 14963 "case 2: {\n" 14964 " [[fallthrough]];\n" 14965 "}\n" 14966 "default: {\n" 14967 " break;\n" 14968 "}\n" 14969 "}", 14970 NoSpaceStyle); 14971 14972 FormatStyle InvertedSpaceStyle = getLLVMStyle(); 14973 InvertedSpaceStyle.SpaceBeforeCaseColon = true; 14974 InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false; 14975 InvertedSpaceStyle.SpaceBeforeInheritanceColon = false; 14976 InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 14977 verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle); 14978 verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle); 14979 verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle); 14980 verifyFormat("int x = a ? b : c;", InvertedSpaceStyle); 14981 verifyFormat("{\n" 14982 "label3:\n" 14983 " int x = 0;\n" 14984 "}", 14985 InvertedSpaceStyle); 14986 verifyFormat("switch (x) {\n" 14987 "case 1 :\n" 14988 "case 2 : {\n" 14989 " break;\n" 14990 "}\n" 14991 "default :\n" 14992 " break;\n" 14993 "}", 14994 InvertedSpaceStyle); 14995 verifyFormat("switch (allBraces) {\n" 14996 "case 1 : {\n" 14997 " break;\n" 14998 "}\n" 14999 "case 2 : {\n" 15000 " [[fallthrough]];\n" 15001 "}\n" 15002 "default : {\n" 15003 " break;\n" 15004 "}\n" 15005 "}", 15006 InvertedSpaceStyle); 15007 } 15008 15009 TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) { 15010 FormatStyle Style = getLLVMStyle(); 15011 15012 Style.PointerAlignment = FormatStyle::PAS_Left; 15013 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; 15014 verifyFormat("void* const* x = NULL;", Style); 15015 15016 #define verifyQualifierSpaces(Code, Pointers, Qualifiers) \ 15017 do { \ 15018 Style.PointerAlignment = FormatStyle::Pointers; \ 15019 Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers; \ 15020 verifyFormat(Code, Style); \ 15021 } while (false) 15022 15023 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default); 15024 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default); 15025 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default); 15026 15027 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before); 15028 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before); 15029 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before); 15030 15031 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After); 15032 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After); 15033 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After); 15034 15035 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both); 15036 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both); 15037 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both); 15038 15039 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default); 15040 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, 15041 SAPQ_Default); 15042 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 15043 SAPQ_Default); 15044 15045 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before); 15046 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, 15047 SAPQ_Before); 15048 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 15049 SAPQ_Before); 15050 15051 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After); 15052 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After); 15053 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 15054 SAPQ_After); 15055 15056 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both); 15057 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both); 15058 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both); 15059 15060 #undef verifyQualifierSpaces 15061 15062 FormatStyle Spaces = getLLVMStyle(); 15063 Spaces.AttributeMacros.push_back("qualified"); 15064 Spaces.PointerAlignment = FormatStyle::PAS_Right; 15065 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; 15066 verifyFormat("SomeType *volatile *a = NULL;", Spaces); 15067 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces); 15068 verifyFormat("std::vector<SomeType *const *> x;", Spaces); 15069 verifyFormat("std::vector<SomeType *qualified *> x;", Spaces); 15070 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 15071 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before; 15072 verifyFormat("SomeType * volatile *a = NULL;", Spaces); 15073 verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces); 15074 verifyFormat("std::vector<SomeType * const *> x;", Spaces); 15075 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces); 15076 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 15077 15078 // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left. 15079 Spaces.PointerAlignment = FormatStyle::PAS_Left; 15080 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before; 15081 verifyFormat("SomeType* volatile* a = NULL;", Spaces); 15082 verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces); 15083 verifyFormat("std::vector<SomeType* const*> x;", Spaces); 15084 verifyFormat("std::vector<SomeType* qualified*> x;", Spaces); 15085 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 15086 // However, setting it to SAPQ_After should add spaces after __attribute, etc. 15087 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After; 15088 verifyFormat("SomeType* volatile * a = NULL;", Spaces); 15089 verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces); 15090 verifyFormat("std::vector<SomeType* const *> x;", Spaces); 15091 verifyFormat("std::vector<SomeType* qualified *> x;", Spaces); 15092 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 15093 15094 // PAS_Middle should not have any noticeable changes even for SAPQ_Both 15095 Spaces.PointerAlignment = FormatStyle::PAS_Middle; 15096 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After; 15097 verifyFormat("SomeType * volatile * a = NULL;", Spaces); 15098 verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces); 15099 verifyFormat("std::vector<SomeType * const *> x;", Spaces); 15100 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces); 15101 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 15102 } 15103 15104 TEST_F(FormatTest, AlignConsecutiveMacros) { 15105 FormatStyle Style = getLLVMStyle(); 15106 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 15107 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 15108 Style.AlignConsecutiveMacros = FormatStyle::ACS_None; 15109 15110 verifyFormat("#define a 3\n" 15111 "#define bbbb 4\n" 15112 "#define ccc (5)", 15113 Style); 15114 15115 verifyFormat("#define f(x) (x * x)\n" 15116 "#define fff(x, y, z) (x * y + z)\n" 15117 "#define ffff(x, y) (x - y)", 15118 Style); 15119 15120 verifyFormat("#define foo(x, y) (x + y)\n" 15121 "#define bar (5, 6)(2 + 2)", 15122 Style); 15123 15124 verifyFormat("#define a 3\n" 15125 "#define bbbb 4\n" 15126 "#define ccc (5)\n" 15127 "#define f(x) (x * x)\n" 15128 "#define fff(x, y, z) (x * y + z)\n" 15129 "#define ffff(x, y) (x - y)", 15130 Style); 15131 15132 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15133 verifyFormat("#define a 3\n" 15134 "#define bbbb 4\n" 15135 "#define ccc (5)", 15136 Style); 15137 15138 verifyFormat("#define f(x) (x * x)\n" 15139 "#define fff(x, y, z) (x * y + z)\n" 15140 "#define ffff(x, y) (x - y)", 15141 Style); 15142 15143 verifyFormat("#define foo(x, y) (x + y)\n" 15144 "#define bar (5, 6)(2 + 2)", 15145 Style); 15146 15147 verifyFormat("#define a 3\n" 15148 "#define bbbb 4\n" 15149 "#define ccc (5)\n" 15150 "#define f(x) (x * x)\n" 15151 "#define fff(x, y, z) (x * y + z)\n" 15152 "#define ffff(x, y) (x - y)", 15153 Style); 15154 15155 verifyFormat("#define a 5\n" 15156 "#define foo(x, y) (x + y)\n" 15157 "#define CCC (6)\n" 15158 "auto lambda = []() {\n" 15159 " auto ii = 0;\n" 15160 " float j = 0;\n" 15161 " return 0;\n" 15162 "};\n" 15163 "int i = 0;\n" 15164 "float i2 = 0;\n" 15165 "auto v = type{\n" 15166 " i = 1, //\n" 15167 " (i = 2), //\n" 15168 " i = 3 //\n" 15169 "};", 15170 Style); 15171 15172 Style.AlignConsecutiveMacros = FormatStyle::ACS_None; 15173 Style.ColumnLimit = 20; 15174 15175 verifyFormat("#define a \\\n" 15176 " \"aabbbbbbbbbbbb\"\n" 15177 "#define D \\\n" 15178 " \"aabbbbbbbbbbbb\" \\\n" 15179 " \"ccddeeeeeeeee\"\n" 15180 "#define B \\\n" 15181 " \"QQQQQQQQQQQQQ\" \\\n" 15182 " \"FFFFFFFFFFFFF\" \\\n" 15183 " \"LLLLLLLL\"\n", 15184 Style); 15185 15186 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15187 verifyFormat("#define a \\\n" 15188 " \"aabbbbbbbbbbbb\"\n" 15189 "#define D \\\n" 15190 " \"aabbbbbbbbbbbb\" \\\n" 15191 " \"ccddeeeeeeeee\"\n" 15192 "#define B \\\n" 15193 " \"QQQQQQQQQQQQQ\" \\\n" 15194 " \"FFFFFFFFFFFFF\" \\\n" 15195 " \"LLLLLLLL\"\n", 15196 Style); 15197 15198 // Test across comments 15199 Style.MaxEmptyLinesToKeep = 10; 15200 Style.ReflowComments = false; 15201 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossComments; 15202 EXPECT_EQ("#define a 3\n" 15203 "// line comment\n" 15204 "#define bbbb 4\n" 15205 "#define ccc (5)", 15206 format("#define a 3\n" 15207 "// line comment\n" 15208 "#define bbbb 4\n" 15209 "#define ccc (5)", 15210 Style)); 15211 15212 EXPECT_EQ("#define a 3\n" 15213 "/* block comment */\n" 15214 "#define bbbb 4\n" 15215 "#define ccc (5)", 15216 format("#define a 3\n" 15217 "/* block comment */\n" 15218 "#define bbbb 4\n" 15219 "#define ccc (5)", 15220 Style)); 15221 15222 EXPECT_EQ("#define a 3\n" 15223 "/* multi-line *\n" 15224 " * block comment */\n" 15225 "#define bbbb 4\n" 15226 "#define ccc (5)", 15227 format("#define a 3\n" 15228 "/* multi-line *\n" 15229 " * block comment */\n" 15230 "#define bbbb 4\n" 15231 "#define ccc (5)", 15232 Style)); 15233 15234 EXPECT_EQ("#define a 3\n" 15235 "// multi-line line comment\n" 15236 "//\n" 15237 "#define bbbb 4\n" 15238 "#define ccc (5)", 15239 format("#define a 3\n" 15240 "// multi-line line comment\n" 15241 "//\n" 15242 "#define bbbb 4\n" 15243 "#define ccc (5)", 15244 Style)); 15245 15246 EXPECT_EQ("#define a 3\n" 15247 "// empty lines still break.\n" 15248 "\n" 15249 "#define bbbb 4\n" 15250 "#define ccc (5)", 15251 format("#define a 3\n" 15252 "// empty lines still break.\n" 15253 "\n" 15254 "#define bbbb 4\n" 15255 "#define ccc (5)", 15256 Style)); 15257 15258 // Test across empty lines 15259 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLines; 15260 EXPECT_EQ("#define a 3\n" 15261 "\n" 15262 "#define bbbb 4\n" 15263 "#define ccc (5)", 15264 format("#define a 3\n" 15265 "\n" 15266 "#define bbbb 4\n" 15267 "#define ccc (5)", 15268 Style)); 15269 15270 EXPECT_EQ("#define a 3\n" 15271 "\n" 15272 "\n" 15273 "\n" 15274 "#define bbbb 4\n" 15275 "#define ccc (5)", 15276 format("#define a 3\n" 15277 "\n" 15278 "\n" 15279 "\n" 15280 "#define bbbb 4\n" 15281 "#define ccc (5)", 15282 Style)); 15283 15284 EXPECT_EQ("#define a 3\n" 15285 "// comments should break alignment\n" 15286 "//\n" 15287 "#define bbbb 4\n" 15288 "#define ccc (5)", 15289 format("#define a 3\n" 15290 "// comments should break alignment\n" 15291 "//\n" 15292 "#define bbbb 4\n" 15293 "#define ccc (5)", 15294 Style)); 15295 15296 // Test across empty lines and comments 15297 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLinesAndComments; 15298 verifyFormat("#define a 3\n" 15299 "\n" 15300 "// line comment\n" 15301 "#define bbbb 4\n" 15302 "#define ccc (5)", 15303 Style); 15304 15305 EXPECT_EQ("#define a 3\n" 15306 "\n" 15307 "\n" 15308 "/* multi-line *\n" 15309 " * block comment */\n" 15310 "\n" 15311 "\n" 15312 "#define bbbb 4\n" 15313 "#define ccc (5)", 15314 format("#define a 3\n" 15315 "\n" 15316 "\n" 15317 "/* multi-line *\n" 15318 " * block comment */\n" 15319 "\n" 15320 "\n" 15321 "#define bbbb 4\n" 15322 "#define ccc (5)", 15323 Style)); 15324 15325 EXPECT_EQ("#define a 3\n" 15326 "\n" 15327 "\n" 15328 "/* multi-line *\n" 15329 " * block comment */\n" 15330 "\n" 15331 "\n" 15332 "#define bbbb 4\n" 15333 "#define ccc (5)", 15334 format("#define a 3\n" 15335 "\n" 15336 "\n" 15337 "/* multi-line *\n" 15338 " * block comment */\n" 15339 "\n" 15340 "\n" 15341 "#define bbbb 4\n" 15342 "#define ccc (5)", 15343 Style)); 15344 } 15345 15346 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) { 15347 FormatStyle Alignment = getLLVMStyle(); 15348 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15349 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossEmptyLines; 15350 15351 Alignment.MaxEmptyLinesToKeep = 10; 15352 /* Test alignment across empty lines */ 15353 EXPECT_EQ("int a = 5;\n" 15354 "\n" 15355 "int oneTwoThree = 123;", 15356 format("int a = 5;\n" 15357 "\n" 15358 "int oneTwoThree= 123;", 15359 Alignment)); 15360 EXPECT_EQ("int a = 5;\n" 15361 "int one = 1;\n" 15362 "\n" 15363 "int oneTwoThree = 123;", 15364 format("int a = 5;\n" 15365 "int one = 1;\n" 15366 "\n" 15367 "int oneTwoThree = 123;", 15368 Alignment)); 15369 EXPECT_EQ("int a = 5;\n" 15370 "int one = 1;\n" 15371 "\n" 15372 "int oneTwoThree = 123;\n" 15373 "int oneTwo = 12;", 15374 format("int a = 5;\n" 15375 "int one = 1;\n" 15376 "\n" 15377 "int oneTwoThree = 123;\n" 15378 "int oneTwo = 12;", 15379 Alignment)); 15380 15381 /* Test across comments */ 15382 EXPECT_EQ("int a = 5;\n" 15383 "/* block comment */\n" 15384 "int oneTwoThree = 123;", 15385 format("int a = 5;\n" 15386 "/* block comment */\n" 15387 "int oneTwoThree=123;", 15388 Alignment)); 15389 15390 EXPECT_EQ("int a = 5;\n" 15391 "// line comment\n" 15392 "int oneTwoThree = 123;", 15393 format("int a = 5;\n" 15394 "// line comment\n" 15395 "int oneTwoThree=123;", 15396 Alignment)); 15397 15398 /* Test across comments and newlines */ 15399 EXPECT_EQ("int a = 5;\n" 15400 "\n" 15401 "/* block comment */\n" 15402 "int oneTwoThree = 123;", 15403 format("int a = 5;\n" 15404 "\n" 15405 "/* block comment */\n" 15406 "int oneTwoThree=123;", 15407 Alignment)); 15408 15409 EXPECT_EQ("int a = 5;\n" 15410 "\n" 15411 "// line comment\n" 15412 "int oneTwoThree = 123;", 15413 format("int a = 5;\n" 15414 "\n" 15415 "// line comment\n" 15416 "int oneTwoThree=123;", 15417 Alignment)); 15418 } 15419 15420 TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) { 15421 FormatStyle Alignment = getLLVMStyle(); 15422 Alignment.AlignConsecutiveDeclarations = 15423 FormatStyle::ACS_AcrossEmptyLinesAndComments; 15424 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 15425 15426 Alignment.MaxEmptyLinesToKeep = 10; 15427 /* Test alignment across empty lines */ 15428 EXPECT_EQ("int a = 5;\n" 15429 "\n" 15430 "float const oneTwoThree = 123;", 15431 format("int a = 5;\n" 15432 "\n" 15433 "float const oneTwoThree = 123;", 15434 Alignment)); 15435 EXPECT_EQ("int a = 5;\n" 15436 "float const one = 1;\n" 15437 "\n" 15438 "int oneTwoThree = 123;", 15439 format("int a = 5;\n" 15440 "float const one = 1;\n" 15441 "\n" 15442 "int oneTwoThree = 123;", 15443 Alignment)); 15444 15445 /* Test across comments */ 15446 EXPECT_EQ("float const a = 5;\n" 15447 "/* block comment */\n" 15448 "int oneTwoThree = 123;", 15449 format("float const a = 5;\n" 15450 "/* block comment */\n" 15451 "int oneTwoThree=123;", 15452 Alignment)); 15453 15454 EXPECT_EQ("float const a = 5;\n" 15455 "// line comment\n" 15456 "int oneTwoThree = 123;", 15457 format("float const a = 5;\n" 15458 "// line comment\n" 15459 "int oneTwoThree=123;", 15460 Alignment)); 15461 15462 /* Test across comments and newlines */ 15463 EXPECT_EQ("float const a = 5;\n" 15464 "\n" 15465 "/* block comment */\n" 15466 "int oneTwoThree = 123;", 15467 format("float const a = 5;\n" 15468 "\n" 15469 "/* block comment */\n" 15470 "int oneTwoThree=123;", 15471 Alignment)); 15472 15473 EXPECT_EQ("float const a = 5;\n" 15474 "\n" 15475 "// line comment\n" 15476 "int oneTwoThree = 123;", 15477 format("float const a = 5;\n" 15478 "\n" 15479 "// line comment\n" 15480 "int oneTwoThree=123;", 15481 Alignment)); 15482 } 15483 15484 TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) { 15485 FormatStyle Alignment = getLLVMStyle(); 15486 Alignment.AlignConsecutiveBitFields = 15487 FormatStyle::ACS_AcrossEmptyLinesAndComments; 15488 15489 Alignment.MaxEmptyLinesToKeep = 10; 15490 /* Test alignment across empty lines */ 15491 EXPECT_EQ("int a : 5;\n" 15492 "\n" 15493 "int longbitfield : 6;", 15494 format("int a : 5;\n" 15495 "\n" 15496 "int longbitfield : 6;", 15497 Alignment)); 15498 EXPECT_EQ("int a : 5;\n" 15499 "int one : 1;\n" 15500 "\n" 15501 "int longbitfield : 6;", 15502 format("int a : 5;\n" 15503 "int one : 1;\n" 15504 "\n" 15505 "int longbitfield : 6;", 15506 Alignment)); 15507 15508 /* Test across comments */ 15509 EXPECT_EQ("int a : 5;\n" 15510 "/* block comment */\n" 15511 "int longbitfield : 6;", 15512 format("int a : 5;\n" 15513 "/* block comment */\n" 15514 "int longbitfield : 6;", 15515 Alignment)); 15516 EXPECT_EQ("int a : 5;\n" 15517 "int one : 1;\n" 15518 "// line comment\n" 15519 "int longbitfield : 6;", 15520 format("int a : 5;\n" 15521 "int one : 1;\n" 15522 "// line comment\n" 15523 "int longbitfield : 6;", 15524 Alignment)); 15525 15526 /* Test across comments and newlines */ 15527 EXPECT_EQ("int a : 5;\n" 15528 "/* block comment */\n" 15529 "\n" 15530 "int longbitfield : 6;", 15531 format("int a : 5;\n" 15532 "/* block comment */\n" 15533 "\n" 15534 "int longbitfield : 6;", 15535 Alignment)); 15536 EXPECT_EQ("int a : 5;\n" 15537 "int one : 1;\n" 15538 "\n" 15539 "// line comment\n" 15540 "\n" 15541 "int longbitfield : 6;", 15542 format("int a : 5;\n" 15543 "int one : 1;\n" 15544 "\n" 15545 "// line comment \n" 15546 "\n" 15547 "int longbitfield : 6;", 15548 Alignment)); 15549 } 15550 15551 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) { 15552 FormatStyle Alignment = getLLVMStyle(); 15553 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15554 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossComments; 15555 15556 Alignment.MaxEmptyLinesToKeep = 10; 15557 /* Test alignment across empty lines */ 15558 EXPECT_EQ("int a = 5;\n" 15559 "\n" 15560 "int oneTwoThree = 123;", 15561 format("int a = 5;\n" 15562 "\n" 15563 "int oneTwoThree= 123;", 15564 Alignment)); 15565 EXPECT_EQ("int a = 5;\n" 15566 "int one = 1;\n" 15567 "\n" 15568 "int oneTwoThree = 123;", 15569 format("int a = 5;\n" 15570 "int one = 1;\n" 15571 "\n" 15572 "int oneTwoThree = 123;", 15573 Alignment)); 15574 15575 /* Test across comments */ 15576 EXPECT_EQ("int a = 5;\n" 15577 "/* block comment */\n" 15578 "int oneTwoThree = 123;", 15579 format("int a = 5;\n" 15580 "/* block comment */\n" 15581 "int oneTwoThree=123;", 15582 Alignment)); 15583 15584 EXPECT_EQ("int a = 5;\n" 15585 "// line comment\n" 15586 "int oneTwoThree = 123;", 15587 format("int a = 5;\n" 15588 "// line comment\n" 15589 "int oneTwoThree=123;", 15590 Alignment)); 15591 15592 EXPECT_EQ("int a = 5;\n" 15593 "/*\n" 15594 " * multi-line block comment\n" 15595 " */\n" 15596 "int oneTwoThree = 123;", 15597 format("int a = 5;\n" 15598 "/*\n" 15599 " * multi-line block comment\n" 15600 " */\n" 15601 "int oneTwoThree=123;", 15602 Alignment)); 15603 15604 EXPECT_EQ("int a = 5;\n" 15605 "//\n" 15606 "// multi-line line comment\n" 15607 "//\n" 15608 "int oneTwoThree = 123;", 15609 format("int a = 5;\n" 15610 "//\n" 15611 "// multi-line line comment\n" 15612 "//\n" 15613 "int oneTwoThree=123;", 15614 Alignment)); 15615 15616 /* Test across comments and newlines */ 15617 EXPECT_EQ("int a = 5;\n" 15618 "\n" 15619 "/* block comment */\n" 15620 "int oneTwoThree = 123;", 15621 format("int a = 5;\n" 15622 "\n" 15623 "/* block comment */\n" 15624 "int oneTwoThree=123;", 15625 Alignment)); 15626 15627 EXPECT_EQ("int a = 5;\n" 15628 "\n" 15629 "// line comment\n" 15630 "int oneTwoThree = 123;", 15631 format("int a = 5;\n" 15632 "\n" 15633 "// line comment\n" 15634 "int oneTwoThree=123;", 15635 Alignment)); 15636 } 15637 15638 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) { 15639 FormatStyle Alignment = getLLVMStyle(); 15640 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15641 Alignment.AlignConsecutiveAssignments = 15642 FormatStyle::ACS_AcrossEmptyLinesAndComments; 15643 verifyFormat("int a = 5;\n" 15644 "int oneTwoThree = 123;", 15645 Alignment); 15646 verifyFormat("int a = method();\n" 15647 "int oneTwoThree = 133;", 15648 Alignment); 15649 verifyFormat("a &= 5;\n" 15650 "bcd *= 5;\n" 15651 "ghtyf += 5;\n" 15652 "dvfvdb -= 5;\n" 15653 "a /= 5;\n" 15654 "vdsvsv %= 5;\n" 15655 "sfdbddfbdfbb ^= 5;\n" 15656 "dvsdsv |= 5;\n" 15657 "int dsvvdvsdvvv = 123;", 15658 Alignment); 15659 verifyFormat("int i = 1, j = 10;\n" 15660 "something = 2000;", 15661 Alignment); 15662 verifyFormat("something = 2000;\n" 15663 "int i = 1, j = 10;\n", 15664 Alignment); 15665 verifyFormat("something = 2000;\n" 15666 "another = 911;\n" 15667 "int i = 1, j = 10;\n" 15668 "oneMore = 1;\n" 15669 "i = 2;", 15670 Alignment); 15671 verifyFormat("int a = 5;\n" 15672 "int one = 1;\n" 15673 "method();\n" 15674 "int oneTwoThree = 123;\n" 15675 "int oneTwo = 12;", 15676 Alignment); 15677 verifyFormat("int oneTwoThree = 123;\n" 15678 "int oneTwo = 12;\n" 15679 "method();\n", 15680 Alignment); 15681 verifyFormat("int oneTwoThree = 123; // comment\n" 15682 "int oneTwo = 12; // comment", 15683 Alignment); 15684 15685 // Bug 25167 15686 /* Uncomment when fixed 15687 verifyFormat("#if A\n" 15688 "#else\n" 15689 "int aaaaaaaa = 12;\n" 15690 "#endif\n" 15691 "#if B\n" 15692 "#else\n" 15693 "int a = 12;\n" 15694 "#endif\n", 15695 Alignment); 15696 verifyFormat("enum foo {\n" 15697 "#if A\n" 15698 "#else\n" 15699 " aaaaaaaa = 12;\n" 15700 "#endif\n" 15701 "#if B\n" 15702 "#else\n" 15703 " a = 12;\n" 15704 "#endif\n" 15705 "};\n", 15706 Alignment); 15707 */ 15708 15709 Alignment.MaxEmptyLinesToKeep = 10; 15710 /* Test alignment across empty lines */ 15711 EXPECT_EQ("int a = 5;\n" 15712 "\n" 15713 "int oneTwoThree = 123;", 15714 format("int a = 5;\n" 15715 "\n" 15716 "int oneTwoThree= 123;", 15717 Alignment)); 15718 EXPECT_EQ("int a = 5;\n" 15719 "int one = 1;\n" 15720 "\n" 15721 "int oneTwoThree = 123;", 15722 format("int a = 5;\n" 15723 "int one = 1;\n" 15724 "\n" 15725 "int oneTwoThree = 123;", 15726 Alignment)); 15727 EXPECT_EQ("int a = 5;\n" 15728 "int one = 1;\n" 15729 "\n" 15730 "int oneTwoThree = 123;\n" 15731 "int oneTwo = 12;", 15732 format("int a = 5;\n" 15733 "int one = 1;\n" 15734 "\n" 15735 "int oneTwoThree = 123;\n" 15736 "int oneTwo = 12;", 15737 Alignment)); 15738 15739 /* Test across comments */ 15740 EXPECT_EQ("int a = 5;\n" 15741 "/* block comment */\n" 15742 "int oneTwoThree = 123;", 15743 format("int a = 5;\n" 15744 "/* block comment */\n" 15745 "int oneTwoThree=123;", 15746 Alignment)); 15747 15748 EXPECT_EQ("int a = 5;\n" 15749 "// line comment\n" 15750 "int oneTwoThree = 123;", 15751 format("int a = 5;\n" 15752 "// line comment\n" 15753 "int oneTwoThree=123;", 15754 Alignment)); 15755 15756 /* Test across comments and newlines */ 15757 EXPECT_EQ("int a = 5;\n" 15758 "\n" 15759 "/* block comment */\n" 15760 "int oneTwoThree = 123;", 15761 format("int a = 5;\n" 15762 "\n" 15763 "/* block comment */\n" 15764 "int oneTwoThree=123;", 15765 Alignment)); 15766 15767 EXPECT_EQ("int a = 5;\n" 15768 "\n" 15769 "// line comment\n" 15770 "int oneTwoThree = 123;", 15771 format("int a = 5;\n" 15772 "\n" 15773 "// line comment\n" 15774 "int oneTwoThree=123;", 15775 Alignment)); 15776 15777 EXPECT_EQ("int a = 5;\n" 15778 "//\n" 15779 "// multi-line line comment\n" 15780 "//\n" 15781 "int oneTwoThree = 123;", 15782 format("int a = 5;\n" 15783 "//\n" 15784 "// multi-line line comment\n" 15785 "//\n" 15786 "int oneTwoThree=123;", 15787 Alignment)); 15788 15789 EXPECT_EQ("int a = 5;\n" 15790 "/*\n" 15791 " * multi-line block comment\n" 15792 " */\n" 15793 "int oneTwoThree = 123;", 15794 format("int a = 5;\n" 15795 "/*\n" 15796 " * multi-line block comment\n" 15797 " */\n" 15798 "int oneTwoThree=123;", 15799 Alignment)); 15800 15801 EXPECT_EQ("int a = 5;\n" 15802 "\n" 15803 "/* block comment */\n" 15804 "\n" 15805 "\n" 15806 "\n" 15807 "int oneTwoThree = 123;", 15808 format("int a = 5;\n" 15809 "\n" 15810 "/* block comment */\n" 15811 "\n" 15812 "\n" 15813 "\n" 15814 "int oneTwoThree=123;", 15815 Alignment)); 15816 15817 EXPECT_EQ("int a = 5;\n" 15818 "\n" 15819 "// line comment\n" 15820 "\n" 15821 "\n" 15822 "\n" 15823 "int oneTwoThree = 123;", 15824 format("int a = 5;\n" 15825 "\n" 15826 "// line comment\n" 15827 "\n" 15828 "\n" 15829 "\n" 15830 "int oneTwoThree=123;", 15831 Alignment)); 15832 15833 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 15834 verifyFormat("#define A \\\n" 15835 " int aaaa = 12; \\\n" 15836 " int b = 23; \\\n" 15837 " int ccc = 234; \\\n" 15838 " int dddddddddd = 2345;", 15839 Alignment); 15840 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 15841 verifyFormat("#define A \\\n" 15842 " int aaaa = 12; \\\n" 15843 " int b = 23; \\\n" 15844 " int ccc = 234; \\\n" 15845 " int dddddddddd = 2345;", 15846 Alignment); 15847 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 15848 verifyFormat("#define A " 15849 " \\\n" 15850 " int aaaa = 12; " 15851 " \\\n" 15852 " int b = 23; " 15853 " \\\n" 15854 " int ccc = 234; " 15855 " \\\n" 15856 " int dddddddddd = 2345;", 15857 Alignment); 15858 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 15859 "k = 4, int l = 5,\n" 15860 " int m = 6) {\n" 15861 " int j = 10;\n" 15862 " otherThing = 1;\n" 15863 "}", 15864 Alignment); 15865 verifyFormat("void SomeFunction(int parameter = 0) {\n" 15866 " int i = 1;\n" 15867 " int j = 2;\n" 15868 " int big = 10000;\n" 15869 "}", 15870 Alignment); 15871 verifyFormat("class C {\n" 15872 "public:\n" 15873 " int i = 1;\n" 15874 " virtual void f() = 0;\n" 15875 "};", 15876 Alignment); 15877 verifyFormat("int i = 1;\n" 15878 "if (SomeType t = getSomething()) {\n" 15879 "}\n" 15880 "int j = 2;\n" 15881 "int big = 10000;", 15882 Alignment); 15883 verifyFormat("int j = 7;\n" 15884 "for (int k = 0; k < N; ++k) {\n" 15885 "}\n" 15886 "int j = 2;\n" 15887 "int big = 10000;\n" 15888 "}", 15889 Alignment); 15890 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 15891 verifyFormat("int i = 1;\n" 15892 "LooooooooooongType loooooooooooooooooooooongVariable\n" 15893 " = someLooooooooooooooooongFunction();\n" 15894 "int j = 2;", 15895 Alignment); 15896 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 15897 verifyFormat("int i = 1;\n" 15898 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 15899 " someLooooooooooooooooongFunction();\n" 15900 "int j = 2;", 15901 Alignment); 15902 15903 verifyFormat("auto lambda = []() {\n" 15904 " auto i = 0;\n" 15905 " return 0;\n" 15906 "};\n" 15907 "int i = 0;\n" 15908 "auto v = type{\n" 15909 " i = 1, //\n" 15910 " (i = 2), //\n" 15911 " i = 3 //\n" 15912 "};", 15913 Alignment); 15914 15915 verifyFormat( 15916 "int i = 1;\n" 15917 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 15918 " loooooooooooooooooooooongParameterB);\n" 15919 "int j = 2;", 15920 Alignment); 15921 15922 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 15923 " typename B = very_long_type_name_1,\n" 15924 " typename T_2 = very_long_type_name_2>\n" 15925 "auto foo() {}\n", 15926 Alignment); 15927 verifyFormat("int a, b = 1;\n" 15928 "int c = 2;\n" 15929 "int dd = 3;\n", 15930 Alignment); 15931 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 15932 "float b[1][] = {{3.f}};\n", 15933 Alignment); 15934 verifyFormat("for (int i = 0; i < 1; i++)\n" 15935 " int x = 1;\n", 15936 Alignment); 15937 verifyFormat("for (i = 0; i < 1; i++)\n" 15938 " x = 1;\n" 15939 "y = 1;\n", 15940 Alignment); 15941 15942 Alignment.ReflowComments = true; 15943 Alignment.ColumnLimit = 50; 15944 EXPECT_EQ("int x = 0;\n" 15945 "int yy = 1; /// specificlennospace\n" 15946 "int zzz = 2;\n", 15947 format("int x = 0;\n" 15948 "int yy = 1; ///specificlennospace\n" 15949 "int zzz = 2;\n", 15950 Alignment)); 15951 } 15952 15953 TEST_F(FormatTest, AlignConsecutiveAssignments) { 15954 FormatStyle Alignment = getLLVMStyle(); 15955 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15956 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 15957 verifyFormat("int a = 5;\n" 15958 "int oneTwoThree = 123;", 15959 Alignment); 15960 verifyFormat("int a = 5;\n" 15961 "int oneTwoThree = 123;", 15962 Alignment); 15963 15964 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 15965 verifyFormat("int a = 5;\n" 15966 "int oneTwoThree = 123;", 15967 Alignment); 15968 verifyFormat("int a = method();\n" 15969 "int oneTwoThree = 133;", 15970 Alignment); 15971 verifyFormat("a &= 5;\n" 15972 "bcd *= 5;\n" 15973 "ghtyf += 5;\n" 15974 "dvfvdb -= 5;\n" 15975 "a /= 5;\n" 15976 "vdsvsv %= 5;\n" 15977 "sfdbddfbdfbb ^= 5;\n" 15978 "dvsdsv |= 5;\n" 15979 "int dsvvdvsdvvv = 123;", 15980 Alignment); 15981 verifyFormat("int i = 1, j = 10;\n" 15982 "something = 2000;", 15983 Alignment); 15984 verifyFormat("something = 2000;\n" 15985 "int i = 1, j = 10;\n", 15986 Alignment); 15987 verifyFormat("something = 2000;\n" 15988 "another = 911;\n" 15989 "int i = 1, j = 10;\n" 15990 "oneMore = 1;\n" 15991 "i = 2;", 15992 Alignment); 15993 verifyFormat("int a = 5;\n" 15994 "int one = 1;\n" 15995 "method();\n" 15996 "int oneTwoThree = 123;\n" 15997 "int oneTwo = 12;", 15998 Alignment); 15999 verifyFormat("int oneTwoThree = 123;\n" 16000 "int oneTwo = 12;\n" 16001 "method();\n", 16002 Alignment); 16003 verifyFormat("int oneTwoThree = 123; // comment\n" 16004 "int oneTwo = 12; // comment", 16005 Alignment); 16006 16007 // Bug 25167 16008 /* Uncomment when fixed 16009 verifyFormat("#if A\n" 16010 "#else\n" 16011 "int aaaaaaaa = 12;\n" 16012 "#endif\n" 16013 "#if B\n" 16014 "#else\n" 16015 "int a = 12;\n" 16016 "#endif\n", 16017 Alignment); 16018 verifyFormat("enum foo {\n" 16019 "#if A\n" 16020 "#else\n" 16021 " aaaaaaaa = 12;\n" 16022 "#endif\n" 16023 "#if B\n" 16024 "#else\n" 16025 " a = 12;\n" 16026 "#endif\n" 16027 "};\n", 16028 Alignment); 16029 */ 16030 16031 EXPECT_EQ("int a = 5;\n" 16032 "\n" 16033 "int oneTwoThree = 123;", 16034 format("int a = 5;\n" 16035 "\n" 16036 "int oneTwoThree= 123;", 16037 Alignment)); 16038 EXPECT_EQ("int a = 5;\n" 16039 "int one = 1;\n" 16040 "\n" 16041 "int oneTwoThree = 123;", 16042 format("int a = 5;\n" 16043 "int one = 1;\n" 16044 "\n" 16045 "int oneTwoThree = 123;", 16046 Alignment)); 16047 EXPECT_EQ("int a = 5;\n" 16048 "int one = 1;\n" 16049 "\n" 16050 "int oneTwoThree = 123;\n" 16051 "int oneTwo = 12;", 16052 format("int a = 5;\n" 16053 "int one = 1;\n" 16054 "\n" 16055 "int oneTwoThree = 123;\n" 16056 "int oneTwo = 12;", 16057 Alignment)); 16058 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 16059 verifyFormat("#define A \\\n" 16060 " int aaaa = 12; \\\n" 16061 " int b = 23; \\\n" 16062 " int ccc = 234; \\\n" 16063 " int dddddddddd = 2345;", 16064 Alignment); 16065 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 16066 verifyFormat("#define A \\\n" 16067 " int aaaa = 12; \\\n" 16068 " int b = 23; \\\n" 16069 " int ccc = 234; \\\n" 16070 " int dddddddddd = 2345;", 16071 Alignment); 16072 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 16073 verifyFormat("#define A " 16074 " \\\n" 16075 " int aaaa = 12; " 16076 " \\\n" 16077 " int b = 23; " 16078 " \\\n" 16079 " int ccc = 234; " 16080 " \\\n" 16081 " int dddddddddd = 2345;", 16082 Alignment); 16083 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 16084 "k = 4, int l = 5,\n" 16085 " int m = 6) {\n" 16086 " int j = 10;\n" 16087 " otherThing = 1;\n" 16088 "}", 16089 Alignment); 16090 verifyFormat("void SomeFunction(int parameter = 0) {\n" 16091 " int i = 1;\n" 16092 " int j = 2;\n" 16093 " int big = 10000;\n" 16094 "}", 16095 Alignment); 16096 verifyFormat("class C {\n" 16097 "public:\n" 16098 " int i = 1;\n" 16099 " virtual void f() = 0;\n" 16100 "};", 16101 Alignment); 16102 verifyFormat("int i = 1;\n" 16103 "if (SomeType t = getSomething()) {\n" 16104 "}\n" 16105 "int j = 2;\n" 16106 "int big = 10000;", 16107 Alignment); 16108 verifyFormat("int j = 7;\n" 16109 "for (int k = 0; k < N; ++k) {\n" 16110 "}\n" 16111 "int j = 2;\n" 16112 "int big = 10000;\n" 16113 "}", 16114 Alignment); 16115 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 16116 verifyFormat("int i = 1;\n" 16117 "LooooooooooongType loooooooooooooooooooooongVariable\n" 16118 " = someLooooooooooooooooongFunction();\n" 16119 "int j = 2;", 16120 Alignment); 16121 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 16122 verifyFormat("int i = 1;\n" 16123 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 16124 " someLooooooooooooooooongFunction();\n" 16125 "int j = 2;", 16126 Alignment); 16127 16128 verifyFormat("auto lambda = []() {\n" 16129 " auto i = 0;\n" 16130 " return 0;\n" 16131 "};\n" 16132 "int i = 0;\n" 16133 "auto v = type{\n" 16134 " i = 1, //\n" 16135 " (i = 2), //\n" 16136 " i = 3 //\n" 16137 "};", 16138 Alignment); 16139 16140 verifyFormat( 16141 "int i = 1;\n" 16142 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 16143 " loooooooooooooooooooooongParameterB);\n" 16144 "int j = 2;", 16145 Alignment); 16146 16147 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 16148 " typename B = very_long_type_name_1,\n" 16149 " typename T_2 = very_long_type_name_2>\n" 16150 "auto foo() {}\n", 16151 Alignment); 16152 verifyFormat("int a, b = 1;\n" 16153 "int c = 2;\n" 16154 "int dd = 3;\n", 16155 Alignment); 16156 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 16157 "float b[1][] = {{3.f}};\n", 16158 Alignment); 16159 verifyFormat("for (int i = 0; i < 1; i++)\n" 16160 " int x = 1;\n", 16161 Alignment); 16162 verifyFormat("for (i = 0; i < 1; i++)\n" 16163 " x = 1;\n" 16164 "y = 1;\n", 16165 Alignment); 16166 16167 Alignment.ReflowComments = true; 16168 Alignment.ColumnLimit = 50; 16169 EXPECT_EQ("int x = 0;\n" 16170 "int yy = 1; /// specificlennospace\n" 16171 "int zzz = 2;\n", 16172 format("int x = 0;\n" 16173 "int yy = 1; ///specificlennospace\n" 16174 "int zzz = 2;\n", 16175 Alignment)); 16176 } 16177 16178 TEST_F(FormatTest, AlignConsecutiveBitFields) { 16179 FormatStyle Alignment = getLLVMStyle(); 16180 Alignment.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive; 16181 verifyFormat("int const a : 5;\n" 16182 "int oneTwoThree : 23;", 16183 Alignment); 16184 16185 // Initializers are allowed starting with c++2a 16186 verifyFormat("int const a : 5 = 1;\n" 16187 "int oneTwoThree : 23 = 0;", 16188 Alignment); 16189 16190 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16191 verifyFormat("int const a : 5;\n" 16192 "int oneTwoThree : 23;", 16193 Alignment); 16194 16195 verifyFormat("int const a : 5; // comment\n" 16196 "int oneTwoThree : 23; // comment", 16197 Alignment); 16198 16199 verifyFormat("int const a : 5 = 1;\n" 16200 "int oneTwoThree : 23 = 0;", 16201 Alignment); 16202 16203 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16204 verifyFormat("int const a : 5 = 1;\n" 16205 "int oneTwoThree : 23 = 0;", 16206 Alignment); 16207 verifyFormat("int const a : 5 = {1};\n" 16208 "int oneTwoThree : 23 = 0;", 16209 Alignment); 16210 16211 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None; 16212 verifyFormat("int const a :5;\n" 16213 "int oneTwoThree:23;", 16214 Alignment); 16215 16216 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before; 16217 verifyFormat("int const a :5;\n" 16218 "int oneTwoThree :23;", 16219 Alignment); 16220 16221 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After; 16222 verifyFormat("int const a : 5;\n" 16223 "int oneTwoThree: 23;", 16224 Alignment); 16225 16226 // Known limitations: ':' is only recognized as a bitfield colon when 16227 // followed by a number. 16228 /* 16229 verifyFormat("int oneTwoThree : SOME_CONSTANT;\n" 16230 "int a : 5;", 16231 Alignment); 16232 */ 16233 } 16234 16235 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 16236 FormatStyle Alignment = getLLVMStyle(); 16237 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 16238 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_None; 16239 Alignment.PointerAlignment = FormatStyle::PAS_Right; 16240 verifyFormat("float const a = 5;\n" 16241 "int oneTwoThree = 123;", 16242 Alignment); 16243 verifyFormat("int a = 5;\n" 16244 "float const oneTwoThree = 123;", 16245 Alignment); 16246 16247 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16248 verifyFormat("float const a = 5;\n" 16249 "int oneTwoThree = 123;", 16250 Alignment); 16251 verifyFormat("int a = method();\n" 16252 "float const oneTwoThree = 133;", 16253 Alignment); 16254 verifyFormat("int i = 1, j = 10;\n" 16255 "something = 2000;", 16256 Alignment); 16257 verifyFormat("something = 2000;\n" 16258 "int i = 1, j = 10;\n", 16259 Alignment); 16260 verifyFormat("float something = 2000;\n" 16261 "double another = 911;\n" 16262 "int i = 1, j = 10;\n" 16263 "const int *oneMore = 1;\n" 16264 "unsigned i = 2;", 16265 Alignment); 16266 verifyFormat("float a = 5;\n" 16267 "int one = 1;\n" 16268 "method();\n" 16269 "const double oneTwoThree = 123;\n" 16270 "const unsigned int oneTwo = 12;", 16271 Alignment); 16272 verifyFormat("int oneTwoThree{0}; // comment\n" 16273 "unsigned oneTwo; // comment", 16274 Alignment); 16275 verifyFormat("unsigned int *a;\n" 16276 "int *b;\n" 16277 "unsigned int Const *c;\n" 16278 "unsigned int const *d;\n" 16279 "unsigned int Const &e;\n" 16280 "unsigned int const &f;", 16281 Alignment); 16282 verifyFormat("Const unsigned int *c;\n" 16283 "const unsigned int *d;\n" 16284 "Const unsigned int &e;\n" 16285 "const unsigned int &f;\n" 16286 "const unsigned g;\n" 16287 "Const unsigned h;", 16288 Alignment); 16289 EXPECT_EQ("float const a = 5;\n" 16290 "\n" 16291 "int oneTwoThree = 123;", 16292 format("float const a = 5;\n" 16293 "\n" 16294 "int oneTwoThree= 123;", 16295 Alignment)); 16296 EXPECT_EQ("float a = 5;\n" 16297 "int one = 1;\n" 16298 "\n" 16299 "unsigned oneTwoThree = 123;", 16300 format("float a = 5;\n" 16301 "int one = 1;\n" 16302 "\n" 16303 "unsigned oneTwoThree = 123;", 16304 Alignment)); 16305 EXPECT_EQ("float a = 5;\n" 16306 "int one = 1;\n" 16307 "\n" 16308 "unsigned oneTwoThree = 123;\n" 16309 "int oneTwo = 12;", 16310 format("float a = 5;\n" 16311 "int one = 1;\n" 16312 "\n" 16313 "unsigned oneTwoThree = 123;\n" 16314 "int oneTwo = 12;", 16315 Alignment)); 16316 // Function prototype alignment 16317 verifyFormat("int a();\n" 16318 "double b();", 16319 Alignment); 16320 verifyFormat("int a(int x);\n" 16321 "double b();", 16322 Alignment); 16323 unsigned OldColumnLimit = Alignment.ColumnLimit; 16324 // We need to set ColumnLimit to zero, in order to stress nested alignments, 16325 // otherwise the function parameters will be re-flowed onto a single line. 16326 Alignment.ColumnLimit = 0; 16327 EXPECT_EQ("int a(int x,\n" 16328 " float y);\n" 16329 "double b(int x,\n" 16330 " double y);", 16331 format("int a(int x,\n" 16332 " float y);\n" 16333 "double b(int x,\n" 16334 " double y);", 16335 Alignment)); 16336 // This ensures that function parameters of function declarations are 16337 // correctly indented when their owning functions are indented. 16338 // The failure case here is for 'double y' to not be indented enough. 16339 EXPECT_EQ("double a(int x);\n" 16340 "int b(int y,\n" 16341 " double z);", 16342 format("double a(int x);\n" 16343 "int b(int y,\n" 16344 " double z);", 16345 Alignment)); 16346 // Set ColumnLimit low so that we induce wrapping immediately after 16347 // the function name and opening paren. 16348 Alignment.ColumnLimit = 13; 16349 verifyFormat("int function(\n" 16350 " int x,\n" 16351 " bool y);", 16352 Alignment); 16353 Alignment.ColumnLimit = OldColumnLimit; 16354 // Ensure function pointers don't screw up recursive alignment 16355 verifyFormat("int a(int x, void (*fp)(int y));\n" 16356 "double b();", 16357 Alignment); 16358 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16359 // Ensure recursive alignment is broken by function braces, so that the 16360 // "a = 1" does not align with subsequent assignments inside the function 16361 // body. 16362 verifyFormat("int func(int a = 1) {\n" 16363 " int b = 2;\n" 16364 " int cc = 3;\n" 16365 "}", 16366 Alignment); 16367 verifyFormat("float something = 2000;\n" 16368 "double another = 911;\n" 16369 "int i = 1, j = 10;\n" 16370 "const int *oneMore = 1;\n" 16371 "unsigned i = 2;", 16372 Alignment); 16373 verifyFormat("int oneTwoThree = {0}; // comment\n" 16374 "unsigned oneTwo = 0; // comment", 16375 Alignment); 16376 // Make sure that scope is correctly tracked, in the absence of braces 16377 verifyFormat("for (int i = 0; i < n; i++)\n" 16378 " j = i;\n" 16379 "double x = 1;\n", 16380 Alignment); 16381 verifyFormat("if (int i = 0)\n" 16382 " j = i;\n" 16383 "double x = 1;\n", 16384 Alignment); 16385 // Ensure operator[] and operator() are comprehended 16386 verifyFormat("struct test {\n" 16387 " long long int foo();\n" 16388 " int operator[](int a);\n" 16389 " double bar();\n" 16390 "};\n", 16391 Alignment); 16392 verifyFormat("struct test {\n" 16393 " long long int foo();\n" 16394 " int operator()(int a);\n" 16395 " double bar();\n" 16396 "};\n", 16397 Alignment); 16398 16399 // PAS_Right 16400 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16401 " int const i = 1;\n" 16402 " int *j = 2;\n" 16403 " int big = 10000;\n" 16404 "\n" 16405 " unsigned oneTwoThree = 123;\n" 16406 " int oneTwo = 12;\n" 16407 " method();\n" 16408 " float k = 2;\n" 16409 " int ll = 10000;\n" 16410 "}", 16411 format("void SomeFunction(int parameter= 0) {\n" 16412 " int const i= 1;\n" 16413 " int *j=2;\n" 16414 " int big = 10000;\n" 16415 "\n" 16416 "unsigned oneTwoThree =123;\n" 16417 "int oneTwo = 12;\n" 16418 " method();\n" 16419 "float k= 2;\n" 16420 "int ll=10000;\n" 16421 "}", 16422 Alignment)); 16423 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16424 " int const i = 1;\n" 16425 " int **j = 2, ***k;\n" 16426 " int &k = i;\n" 16427 " int &&l = i + j;\n" 16428 " int big = 10000;\n" 16429 "\n" 16430 " unsigned oneTwoThree = 123;\n" 16431 " int oneTwo = 12;\n" 16432 " method();\n" 16433 " float k = 2;\n" 16434 " int ll = 10000;\n" 16435 "}", 16436 format("void SomeFunction(int parameter= 0) {\n" 16437 " int const i= 1;\n" 16438 " int **j=2,***k;\n" 16439 "int &k=i;\n" 16440 "int &&l=i+j;\n" 16441 " int big = 10000;\n" 16442 "\n" 16443 "unsigned oneTwoThree =123;\n" 16444 "int oneTwo = 12;\n" 16445 " method();\n" 16446 "float k= 2;\n" 16447 "int ll=10000;\n" 16448 "}", 16449 Alignment)); 16450 // variables are aligned at their name, pointers are at the right most 16451 // position 16452 verifyFormat("int *a;\n" 16453 "int **b;\n" 16454 "int ***c;\n" 16455 "int foobar;\n", 16456 Alignment); 16457 16458 // PAS_Left 16459 FormatStyle AlignmentLeft = Alignment; 16460 AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left; 16461 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16462 " int const i = 1;\n" 16463 " int* j = 2;\n" 16464 " int big = 10000;\n" 16465 "\n" 16466 " unsigned oneTwoThree = 123;\n" 16467 " int oneTwo = 12;\n" 16468 " method();\n" 16469 " float k = 2;\n" 16470 " int ll = 10000;\n" 16471 "}", 16472 format("void SomeFunction(int parameter= 0) {\n" 16473 " int const i= 1;\n" 16474 " int *j=2;\n" 16475 " int big = 10000;\n" 16476 "\n" 16477 "unsigned oneTwoThree =123;\n" 16478 "int oneTwo = 12;\n" 16479 " method();\n" 16480 "float k= 2;\n" 16481 "int ll=10000;\n" 16482 "}", 16483 AlignmentLeft)); 16484 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16485 " int const i = 1;\n" 16486 " int** j = 2;\n" 16487 " int& k = i;\n" 16488 " int&& l = i + j;\n" 16489 " int big = 10000;\n" 16490 "\n" 16491 " unsigned oneTwoThree = 123;\n" 16492 " int oneTwo = 12;\n" 16493 " method();\n" 16494 " float k = 2;\n" 16495 " int ll = 10000;\n" 16496 "}", 16497 format("void SomeFunction(int parameter= 0) {\n" 16498 " int const i= 1;\n" 16499 " int **j=2;\n" 16500 "int &k=i;\n" 16501 "int &&l=i+j;\n" 16502 " int big = 10000;\n" 16503 "\n" 16504 "unsigned oneTwoThree =123;\n" 16505 "int oneTwo = 12;\n" 16506 " method();\n" 16507 "float k= 2;\n" 16508 "int ll=10000;\n" 16509 "}", 16510 AlignmentLeft)); 16511 // variables are aligned at their name, pointers are at the left most position 16512 verifyFormat("int* a;\n" 16513 "int** b;\n" 16514 "int*** c;\n" 16515 "int foobar;\n", 16516 AlignmentLeft); 16517 16518 // PAS_Middle 16519 FormatStyle AlignmentMiddle = Alignment; 16520 AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle; 16521 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16522 " int const i = 1;\n" 16523 " int * j = 2;\n" 16524 " int big = 10000;\n" 16525 "\n" 16526 " unsigned oneTwoThree = 123;\n" 16527 " int oneTwo = 12;\n" 16528 " method();\n" 16529 " float k = 2;\n" 16530 " int ll = 10000;\n" 16531 "}", 16532 format("void SomeFunction(int parameter= 0) {\n" 16533 " int const i= 1;\n" 16534 " int *j=2;\n" 16535 " int big = 10000;\n" 16536 "\n" 16537 "unsigned oneTwoThree =123;\n" 16538 "int oneTwo = 12;\n" 16539 " method();\n" 16540 "float k= 2;\n" 16541 "int ll=10000;\n" 16542 "}", 16543 AlignmentMiddle)); 16544 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16545 " int const i = 1;\n" 16546 " int ** j = 2, ***k;\n" 16547 " int & k = i;\n" 16548 " int && l = i + j;\n" 16549 " int big = 10000;\n" 16550 "\n" 16551 " unsigned oneTwoThree = 123;\n" 16552 " int oneTwo = 12;\n" 16553 " method();\n" 16554 " float k = 2;\n" 16555 " int ll = 10000;\n" 16556 "}", 16557 format("void SomeFunction(int parameter= 0) {\n" 16558 " int const i= 1;\n" 16559 " int **j=2,***k;\n" 16560 "int &k=i;\n" 16561 "int &&l=i+j;\n" 16562 " int big = 10000;\n" 16563 "\n" 16564 "unsigned oneTwoThree =123;\n" 16565 "int oneTwo = 12;\n" 16566 " method();\n" 16567 "float k= 2;\n" 16568 "int ll=10000;\n" 16569 "}", 16570 AlignmentMiddle)); 16571 // variables are aligned at their name, pointers are in the middle 16572 verifyFormat("int * a;\n" 16573 "int * b;\n" 16574 "int *** c;\n" 16575 "int foobar;\n", 16576 AlignmentMiddle); 16577 16578 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16579 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 16580 verifyFormat("#define A \\\n" 16581 " int aaaa = 12; \\\n" 16582 " float b = 23; \\\n" 16583 " const int ccc = 234; \\\n" 16584 " unsigned dddddddddd = 2345;", 16585 Alignment); 16586 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 16587 verifyFormat("#define A \\\n" 16588 " int aaaa = 12; \\\n" 16589 " float b = 23; \\\n" 16590 " const int ccc = 234; \\\n" 16591 " unsigned dddddddddd = 2345;", 16592 Alignment); 16593 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 16594 Alignment.ColumnLimit = 30; 16595 verifyFormat("#define A \\\n" 16596 " int aaaa = 12; \\\n" 16597 " float b = 23; \\\n" 16598 " const int ccc = 234; \\\n" 16599 " int dddddddddd = 2345;", 16600 Alignment); 16601 Alignment.ColumnLimit = 80; 16602 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 16603 "k = 4, int l = 5,\n" 16604 " int m = 6) {\n" 16605 " const int j = 10;\n" 16606 " otherThing = 1;\n" 16607 "}", 16608 Alignment); 16609 verifyFormat("void SomeFunction(int parameter = 0) {\n" 16610 " int const i = 1;\n" 16611 " int *j = 2;\n" 16612 " int big = 10000;\n" 16613 "}", 16614 Alignment); 16615 verifyFormat("class C {\n" 16616 "public:\n" 16617 " int i = 1;\n" 16618 " virtual void f() = 0;\n" 16619 "};", 16620 Alignment); 16621 verifyFormat("float i = 1;\n" 16622 "if (SomeType t = getSomething()) {\n" 16623 "}\n" 16624 "const unsigned j = 2;\n" 16625 "int big = 10000;", 16626 Alignment); 16627 verifyFormat("float j = 7;\n" 16628 "for (int k = 0; k < N; ++k) {\n" 16629 "}\n" 16630 "unsigned j = 2;\n" 16631 "int big = 10000;\n" 16632 "}", 16633 Alignment); 16634 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 16635 verifyFormat("float i = 1;\n" 16636 "LooooooooooongType loooooooooooooooooooooongVariable\n" 16637 " = someLooooooooooooooooongFunction();\n" 16638 "int j = 2;", 16639 Alignment); 16640 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 16641 verifyFormat("int i = 1;\n" 16642 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 16643 " someLooooooooooooooooongFunction();\n" 16644 "int j = 2;", 16645 Alignment); 16646 16647 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16648 verifyFormat("auto lambda = []() {\n" 16649 " auto ii = 0;\n" 16650 " float j = 0;\n" 16651 " return 0;\n" 16652 "};\n" 16653 "int i = 0;\n" 16654 "float i2 = 0;\n" 16655 "auto v = type{\n" 16656 " i = 1, //\n" 16657 " (i = 2), //\n" 16658 " i = 3 //\n" 16659 "};", 16660 Alignment); 16661 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16662 16663 verifyFormat( 16664 "int i = 1;\n" 16665 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 16666 " loooooooooooooooooooooongParameterB);\n" 16667 "int j = 2;", 16668 Alignment); 16669 16670 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 16671 // We expect declarations and assignments to align, as long as it doesn't 16672 // exceed the column limit, starting a new alignment sequence whenever it 16673 // happens. 16674 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16675 Alignment.ColumnLimit = 30; 16676 verifyFormat("float ii = 1;\n" 16677 "unsigned j = 2;\n" 16678 "int someVerylongVariable = 1;\n" 16679 "AnotherLongType ll = 123456;\n" 16680 "VeryVeryLongType k = 2;\n" 16681 "int myvar = 1;", 16682 Alignment); 16683 Alignment.ColumnLimit = 80; 16684 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16685 16686 verifyFormat( 16687 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 16688 " typename LongType, typename B>\n" 16689 "auto foo() {}\n", 16690 Alignment); 16691 verifyFormat("float a, b = 1;\n" 16692 "int c = 2;\n" 16693 "int dd = 3;\n", 16694 Alignment); 16695 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 16696 "float b[1][] = {{3.f}};\n", 16697 Alignment); 16698 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16699 verifyFormat("float a, b = 1;\n" 16700 "int c = 2;\n" 16701 "int dd = 3;\n", 16702 Alignment); 16703 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 16704 "float b[1][] = {{3.f}};\n", 16705 Alignment); 16706 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16707 16708 Alignment.ColumnLimit = 30; 16709 Alignment.BinPackParameters = false; 16710 verifyFormat("void foo(float a,\n" 16711 " float b,\n" 16712 " int c,\n" 16713 " uint32_t *d) {\n" 16714 " int *e = 0;\n" 16715 " float f = 0;\n" 16716 " double g = 0;\n" 16717 "}\n" 16718 "void bar(ino_t a,\n" 16719 " int b,\n" 16720 " uint32_t *c,\n" 16721 " bool d) {}\n", 16722 Alignment); 16723 Alignment.BinPackParameters = true; 16724 Alignment.ColumnLimit = 80; 16725 16726 // Bug 33507 16727 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 16728 verifyFormat( 16729 "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n" 16730 " static const Version verVs2017;\n" 16731 " return true;\n" 16732 "});\n", 16733 Alignment); 16734 Alignment.PointerAlignment = FormatStyle::PAS_Right; 16735 16736 // See llvm.org/PR35641 16737 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16738 verifyFormat("int func() { //\n" 16739 " int b;\n" 16740 " unsigned c;\n" 16741 "}", 16742 Alignment); 16743 16744 // See PR37175 16745 FormatStyle Style = getMozillaStyle(); 16746 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16747 EXPECT_EQ("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n" 16748 "foo(int a);", 16749 format("DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style)); 16750 16751 Alignment.PointerAlignment = FormatStyle::PAS_Left; 16752 verifyFormat("unsigned int* a;\n" 16753 "int* b;\n" 16754 "unsigned int Const* c;\n" 16755 "unsigned int const* d;\n" 16756 "unsigned int Const& e;\n" 16757 "unsigned int const& f;", 16758 Alignment); 16759 verifyFormat("Const unsigned int* c;\n" 16760 "const unsigned int* d;\n" 16761 "Const unsigned int& e;\n" 16762 "const unsigned int& f;\n" 16763 "const unsigned g;\n" 16764 "Const unsigned h;", 16765 Alignment); 16766 16767 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 16768 verifyFormat("unsigned int * a;\n" 16769 "int * b;\n" 16770 "unsigned int Const * c;\n" 16771 "unsigned int const * d;\n" 16772 "unsigned int Const & e;\n" 16773 "unsigned int const & f;", 16774 Alignment); 16775 verifyFormat("Const unsigned int * c;\n" 16776 "const unsigned int * d;\n" 16777 "Const unsigned int & e;\n" 16778 "const unsigned int & f;\n" 16779 "const unsigned g;\n" 16780 "Const unsigned h;", 16781 Alignment); 16782 } 16783 16784 TEST_F(FormatTest, AlignWithLineBreaks) { 16785 auto Style = getLLVMStyleWithColumns(120); 16786 16787 EXPECT_EQ(Style.AlignConsecutiveAssignments, FormatStyle::ACS_None); 16788 EXPECT_EQ(Style.AlignConsecutiveDeclarations, FormatStyle::ACS_None); 16789 verifyFormat("void foo() {\n" 16790 " int myVar = 5;\n" 16791 " double x = 3.14;\n" 16792 " auto str = \"Hello \"\n" 16793 " \"World\";\n" 16794 " auto s = \"Hello \"\n" 16795 " \"Again\";\n" 16796 "}", 16797 Style); 16798 16799 // clang-format off 16800 verifyFormat("void foo() {\n" 16801 " const int capacityBefore = Entries.capacity();\n" 16802 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16803 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16804 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16805 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16806 "}", 16807 Style); 16808 // clang-format on 16809 16810 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16811 verifyFormat("void foo() {\n" 16812 " int myVar = 5;\n" 16813 " double x = 3.14;\n" 16814 " auto str = \"Hello \"\n" 16815 " \"World\";\n" 16816 " auto s = \"Hello \"\n" 16817 " \"Again\";\n" 16818 "}", 16819 Style); 16820 16821 // clang-format off 16822 verifyFormat("void foo() {\n" 16823 " const int capacityBefore = Entries.capacity();\n" 16824 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16825 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16826 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16827 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16828 "}", 16829 Style); 16830 // clang-format on 16831 16832 Style.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16833 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16834 verifyFormat("void foo() {\n" 16835 " int myVar = 5;\n" 16836 " double x = 3.14;\n" 16837 " auto str = \"Hello \"\n" 16838 " \"World\";\n" 16839 " auto s = \"Hello \"\n" 16840 " \"Again\";\n" 16841 "}", 16842 Style); 16843 16844 // clang-format off 16845 verifyFormat("void foo() {\n" 16846 " const int capacityBefore = Entries.capacity();\n" 16847 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16848 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16849 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16850 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16851 "}", 16852 Style); 16853 // clang-format on 16854 16855 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16856 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16857 16858 verifyFormat("void foo() {\n" 16859 " int myVar = 5;\n" 16860 " double x = 3.14;\n" 16861 " auto str = \"Hello \"\n" 16862 " \"World\";\n" 16863 " auto s = \"Hello \"\n" 16864 " \"Again\";\n" 16865 "}", 16866 Style); 16867 16868 // clang-format off 16869 verifyFormat("void foo() {\n" 16870 " const int capacityBefore = Entries.capacity();\n" 16871 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16872 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16873 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16874 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16875 "}", 16876 Style); 16877 // clang-format on 16878 16879 Style = getLLVMStyleWithColumns(120); 16880 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16881 Style.ContinuationIndentWidth = 4; 16882 Style.IndentWidth = 4; 16883 16884 // clang-format off 16885 verifyFormat("void SomeFunc() {\n" 16886 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 16887 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16888 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 16889 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16890 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 16891 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16892 "}", 16893 Style); 16894 // clang-format on 16895 16896 Style.BinPackArguments = false; 16897 16898 // clang-format off 16899 verifyFormat("void SomeFunc() {\n" 16900 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(\n" 16901 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16902 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(\n" 16903 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16904 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(\n" 16905 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16906 "}", 16907 Style); 16908 // clang-format on 16909 } 16910 16911 TEST_F(FormatTest, AlignWithInitializerPeriods) { 16912 auto Style = getLLVMStyleWithColumns(60); 16913 16914 verifyFormat("void foo1(void) {\n" 16915 " BYTE p[1] = 1;\n" 16916 " A B = {.one_foooooooooooooooo = 2,\n" 16917 " .two_fooooooooooooo = 3,\n" 16918 " .three_fooooooooooooo = 4};\n" 16919 " BYTE payload = 2;\n" 16920 "}", 16921 Style); 16922 16923 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16924 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_None; 16925 verifyFormat("void foo2(void) {\n" 16926 " BYTE p[1] = 1;\n" 16927 " A B = {.one_foooooooooooooooo = 2,\n" 16928 " .two_fooooooooooooo = 3,\n" 16929 " .three_fooooooooooooo = 4};\n" 16930 " BYTE payload = 2;\n" 16931 "}", 16932 Style); 16933 16934 Style.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16935 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16936 verifyFormat("void foo3(void) {\n" 16937 " BYTE p[1] = 1;\n" 16938 " A B = {.one_foooooooooooooooo = 2,\n" 16939 " .two_fooooooooooooo = 3,\n" 16940 " .three_fooooooooooooo = 4};\n" 16941 " BYTE payload = 2;\n" 16942 "}", 16943 Style); 16944 16945 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16946 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16947 verifyFormat("void foo4(void) {\n" 16948 " BYTE p[1] = 1;\n" 16949 " A B = {.one_foooooooooooooooo = 2,\n" 16950 " .two_fooooooooooooo = 3,\n" 16951 " .three_fooooooooooooo = 4};\n" 16952 " BYTE payload = 2;\n" 16953 "}", 16954 Style); 16955 } 16956 16957 TEST_F(FormatTest, LinuxBraceBreaking) { 16958 FormatStyle LinuxBraceStyle = getLLVMStyle(); 16959 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 16960 verifyFormat("namespace a\n" 16961 "{\n" 16962 "class A\n" 16963 "{\n" 16964 " void f()\n" 16965 " {\n" 16966 " if (true) {\n" 16967 " a();\n" 16968 " b();\n" 16969 " } else {\n" 16970 " a();\n" 16971 " }\n" 16972 " }\n" 16973 " void g() { return; }\n" 16974 "};\n" 16975 "struct B {\n" 16976 " int x;\n" 16977 "};\n" 16978 "} // namespace a\n", 16979 LinuxBraceStyle); 16980 verifyFormat("enum X {\n" 16981 " Y = 0,\n" 16982 "}\n", 16983 LinuxBraceStyle); 16984 verifyFormat("struct S {\n" 16985 " int Type;\n" 16986 " union {\n" 16987 " int x;\n" 16988 " double y;\n" 16989 " } Value;\n" 16990 " class C\n" 16991 " {\n" 16992 " MyFavoriteType Value;\n" 16993 " } Class;\n" 16994 "}\n", 16995 LinuxBraceStyle); 16996 } 16997 16998 TEST_F(FormatTest, MozillaBraceBreaking) { 16999 FormatStyle MozillaBraceStyle = getLLVMStyle(); 17000 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 17001 MozillaBraceStyle.FixNamespaceComments = false; 17002 verifyFormat("namespace a {\n" 17003 "class A\n" 17004 "{\n" 17005 " void f()\n" 17006 " {\n" 17007 " if (true) {\n" 17008 " a();\n" 17009 " b();\n" 17010 " }\n" 17011 " }\n" 17012 " void g() { return; }\n" 17013 "};\n" 17014 "enum E\n" 17015 "{\n" 17016 " A,\n" 17017 " // foo\n" 17018 " B,\n" 17019 " C\n" 17020 "};\n" 17021 "struct B\n" 17022 "{\n" 17023 " int x;\n" 17024 "};\n" 17025 "}\n", 17026 MozillaBraceStyle); 17027 verifyFormat("struct S\n" 17028 "{\n" 17029 " int Type;\n" 17030 " union\n" 17031 " {\n" 17032 " int x;\n" 17033 " double y;\n" 17034 " } Value;\n" 17035 " class C\n" 17036 " {\n" 17037 " MyFavoriteType Value;\n" 17038 " } Class;\n" 17039 "}\n", 17040 MozillaBraceStyle); 17041 } 17042 17043 TEST_F(FormatTest, StroustrupBraceBreaking) { 17044 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 17045 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 17046 verifyFormat("namespace a {\n" 17047 "class A {\n" 17048 " void f()\n" 17049 " {\n" 17050 " if (true) {\n" 17051 " a();\n" 17052 " b();\n" 17053 " }\n" 17054 " }\n" 17055 " void g() { return; }\n" 17056 "};\n" 17057 "struct B {\n" 17058 " int x;\n" 17059 "};\n" 17060 "} // namespace a\n", 17061 StroustrupBraceStyle); 17062 17063 verifyFormat("void foo()\n" 17064 "{\n" 17065 " if (a) {\n" 17066 " a();\n" 17067 " }\n" 17068 " else {\n" 17069 " b();\n" 17070 " }\n" 17071 "}\n", 17072 StroustrupBraceStyle); 17073 17074 verifyFormat("#ifdef _DEBUG\n" 17075 "int foo(int i = 0)\n" 17076 "#else\n" 17077 "int foo(int i = 5)\n" 17078 "#endif\n" 17079 "{\n" 17080 " return i;\n" 17081 "}", 17082 StroustrupBraceStyle); 17083 17084 verifyFormat("void foo() {}\n" 17085 "void bar()\n" 17086 "#ifdef _DEBUG\n" 17087 "{\n" 17088 " foo();\n" 17089 "}\n" 17090 "#else\n" 17091 "{\n" 17092 "}\n" 17093 "#endif", 17094 StroustrupBraceStyle); 17095 17096 verifyFormat("void foobar() { int i = 5; }\n" 17097 "#ifdef _DEBUG\n" 17098 "void bar() {}\n" 17099 "#else\n" 17100 "void bar() { foobar(); }\n" 17101 "#endif", 17102 StroustrupBraceStyle); 17103 } 17104 17105 TEST_F(FormatTest, AllmanBraceBreaking) { 17106 FormatStyle AllmanBraceStyle = getLLVMStyle(); 17107 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 17108 17109 EXPECT_EQ("namespace a\n" 17110 "{\n" 17111 "void f();\n" 17112 "void g();\n" 17113 "} // namespace a\n", 17114 format("namespace a\n" 17115 "{\n" 17116 "void f();\n" 17117 "void g();\n" 17118 "}\n", 17119 AllmanBraceStyle)); 17120 17121 verifyFormat("namespace a\n" 17122 "{\n" 17123 "class A\n" 17124 "{\n" 17125 " void f()\n" 17126 " {\n" 17127 " if (true)\n" 17128 " {\n" 17129 " a();\n" 17130 " b();\n" 17131 " }\n" 17132 " }\n" 17133 " void g() { return; }\n" 17134 "};\n" 17135 "struct B\n" 17136 "{\n" 17137 " int x;\n" 17138 "};\n" 17139 "union C\n" 17140 "{\n" 17141 "};\n" 17142 "} // namespace a", 17143 AllmanBraceStyle); 17144 17145 verifyFormat("void f()\n" 17146 "{\n" 17147 " if (true)\n" 17148 " {\n" 17149 " a();\n" 17150 " }\n" 17151 " else if (false)\n" 17152 " {\n" 17153 " b();\n" 17154 " }\n" 17155 " else\n" 17156 " {\n" 17157 " c();\n" 17158 " }\n" 17159 "}\n", 17160 AllmanBraceStyle); 17161 17162 verifyFormat("void f()\n" 17163 "{\n" 17164 " for (int i = 0; i < 10; ++i)\n" 17165 " {\n" 17166 " a();\n" 17167 " }\n" 17168 " while (false)\n" 17169 " {\n" 17170 " b();\n" 17171 " }\n" 17172 " do\n" 17173 " {\n" 17174 " c();\n" 17175 " } while (false)\n" 17176 "}\n", 17177 AllmanBraceStyle); 17178 17179 verifyFormat("void f(int a)\n" 17180 "{\n" 17181 " switch (a)\n" 17182 " {\n" 17183 " case 0:\n" 17184 " break;\n" 17185 " case 1:\n" 17186 " {\n" 17187 " break;\n" 17188 " }\n" 17189 " case 2:\n" 17190 " {\n" 17191 " }\n" 17192 " break;\n" 17193 " default:\n" 17194 " break;\n" 17195 " }\n" 17196 "}\n", 17197 AllmanBraceStyle); 17198 17199 verifyFormat("enum X\n" 17200 "{\n" 17201 " Y = 0,\n" 17202 "}\n", 17203 AllmanBraceStyle); 17204 verifyFormat("enum X\n" 17205 "{\n" 17206 " Y = 0\n" 17207 "}\n", 17208 AllmanBraceStyle); 17209 17210 verifyFormat("@interface BSApplicationController ()\n" 17211 "{\n" 17212 "@private\n" 17213 " id _extraIvar;\n" 17214 "}\n" 17215 "@end\n", 17216 AllmanBraceStyle); 17217 17218 verifyFormat("#ifdef _DEBUG\n" 17219 "int foo(int i = 0)\n" 17220 "#else\n" 17221 "int foo(int i = 5)\n" 17222 "#endif\n" 17223 "{\n" 17224 " return i;\n" 17225 "}", 17226 AllmanBraceStyle); 17227 17228 verifyFormat("void foo() {}\n" 17229 "void bar()\n" 17230 "#ifdef _DEBUG\n" 17231 "{\n" 17232 " foo();\n" 17233 "}\n" 17234 "#else\n" 17235 "{\n" 17236 "}\n" 17237 "#endif", 17238 AllmanBraceStyle); 17239 17240 verifyFormat("void foobar() { int i = 5; }\n" 17241 "#ifdef _DEBUG\n" 17242 "void bar() {}\n" 17243 "#else\n" 17244 "void bar() { foobar(); }\n" 17245 "#endif", 17246 AllmanBraceStyle); 17247 17248 EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine, 17249 FormatStyle::SLS_All); 17250 17251 verifyFormat("[](int i) { return i + 2; };\n" 17252 "[](int i, int j)\n" 17253 "{\n" 17254 " auto x = i + j;\n" 17255 " auto y = i * j;\n" 17256 " return x ^ y;\n" 17257 "};\n" 17258 "void foo()\n" 17259 "{\n" 17260 " auto shortLambda = [](int i) { return i + 2; };\n" 17261 " auto longLambda = [](int i, int j)\n" 17262 " {\n" 17263 " auto x = i + j;\n" 17264 " auto y = i * j;\n" 17265 " return x ^ y;\n" 17266 " };\n" 17267 "}", 17268 AllmanBraceStyle); 17269 17270 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 17271 17272 verifyFormat("[](int i)\n" 17273 "{\n" 17274 " return i + 2;\n" 17275 "};\n" 17276 "[](int i, int j)\n" 17277 "{\n" 17278 " auto x = i + j;\n" 17279 " auto y = i * j;\n" 17280 " return x ^ y;\n" 17281 "};\n" 17282 "void foo()\n" 17283 "{\n" 17284 " auto shortLambda = [](int i)\n" 17285 " {\n" 17286 " return i + 2;\n" 17287 " };\n" 17288 " auto longLambda = [](int i, int j)\n" 17289 " {\n" 17290 " auto x = i + j;\n" 17291 " auto y = i * j;\n" 17292 " return x ^ y;\n" 17293 " };\n" 17294 "}", 17295 AllmanBraceStyle); 17296 17297 // Reset 17298 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All; 17299 17300 // This shouldn't affect ObjC blocks.. 17301 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 17302 " // ...\n" 17303 " int i;\n" 17304 "}];", 17305 AllmanBraceStyle); 17306 verifyFormat("void (^block)(void) = ^{\n" 17307 " // ...\n" 17308 " int i;\n" 17309 "};", 17310 AllmanBraceStyle); 17311 // .. or dict literals. 17312 verifyFormat("void f()\n" 17313 "{\n" 17314 " // ...\n" 17315 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 17316 "}", 17317 AllmanBraceStyle); 17318 verifyFormat("void f()\n" 17319 "{\n" 17320 " // ...\n" 17321 " [object someMethod:@{a : @\"b\"}];\n" 17322 "}", 17323 AllmanBraceStyle); 17324 verifyFormat("int f()\n" 17325 "{ // comment\n" 17326 " return 42;\n" 17327 "}", 17328 AllmanBraceStyle); 17329 17330 AllmanBraceStyle.ColumnLimit = 19; 17331 verifyFormat("void f() { int i; }", AllmanBraceStyle); 17332 AllmanBraceStyle.ColumnLimit = 18; 17333 verifyFormat("void f()\n" 17334 "{\n" 17335 " int i;\n" 17336 "}", 17337 AllmanBraceStyle); 17338 AllmanBraceStyle.ColumnLimit = 80; 17339 17340 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 17341 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = 17342 FormatStyle::SIS_WithoutElse; 17343 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 17344 verifyFormat("void f(bool b)\n" 17345 "{\n" 17346 " if (b)\n" 17347 " {\n" 17348 " return;\n" 17349 " }\n" 17350 "}\n", 17351 BreakBeforeBraceShortIfs); 17352 verifyFormat("void f(bool b)\n" 17353 "{\n" 17354 " if constexpr (b)\n" 17355 " {\n" 17356 " return;\n" 17357 " }\n" 17358 "}\n", 17359 BreakBeforeBraceShortIfs); 17360 verifyFormat("void f(bool b)\n" 17361 "{\n" 17362 " if CONSTEXPR (b)\n" 17363 " {\n" 17364 " return;\n" 17365 " }\n" 17366 "}\n", 17367 BreakBeforeBraceShortIfs); 17368 verifyFormat("void f(bool b)\n" 17369 "{\n" 17370 " if (b) return;\n" 17371 "}\n", 17372 BreakBeforeBraceShortIfs); 17373 verifyFormat("void f(bool b)\n" 17374 "{\n" 17375 " if constexpr (b) return;\n" 17376 "}\n", 17377 BreakBeforeBraceShortIfs); 17378 verifyFormat("void f(bool b)\n" 17379 "{\n" 17380 " if CONSTEXPR (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, WhitesmithsBraceBreaking) { 17394 FormatStyle WhitesmithsBraceStyle = getLLVMStyle(); 17395 WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 17396 17397 // Make a few changes to the style for testing purposes 17398 WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine = 17399 FormatStyle::SFS_Empty; 17400 WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 17401 WhitesmithsBraceStyle.ColumnLimit = 0; 17402 17403 // FIXME: this test case can't decide whether there should be a blank line 17404 // after the ~D() line or not. It adds one if one doesn't exist in the test 17405 // and it removes the line if one exists. 17406 /* 17407 verifyFormat("class A;\n" 17408 "namespace B\n" 17409 " {\n" 17410 "class C;\n" 17411 "// Comment\n" 17412 "class D\n" 17413 " {\n" 17414 "public:\n" 17415 " D();\n" 17416 " ~D() {}\n" 17417 "private:\n" 17418 " enum E\n" 17419 " {\n" 17420 " F\n" 17421 " }\n" 17422 " };\n" 17423 " } // namespace B\n", 17424 WhitesmithsBraceStyle); 17425 */ 17426 17427 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None; 17428 verifyFormat("namespace a\n" 17429 " {\n" 17430 "class A\n" 17431 " {\n" 17432 " void f()\n" 17433 " {\n" 17434 " if (true)\n" 17435 " {\n" 17436 " a();\n" 17437 " b();\n" 17438 " }\n" 17439 " }\n" 17440 " void g()\n" 17441 " {\n" 17442 " return;\n" 17443 " }\n" 17444 " };\n" 17445 "struct B\n" 17446 " {\n" 17447 " int x;\n" 17448 " };\n" 17449 " } // namespace a", 17450 WhitesmithsBraceStyle); 17451 17452 verifyFormat("namespace a\n" 17453 " {\n" 17454 "namespace b\n" 17455 " {\n" 17456 "class A\n" 17457 " {\n" 17458 " void f()\n" 17459 " {\n" 17460 " if (true)\n" 17461 " {\n" 17462 " a();\n" 17463 " b();\n" 17464 " }\n" 17465 " }\n" 17466 " void g()\n" 17467 " {\n" 17468 " return;\n" 17469 " }\n" 17470 " };\n" 17471 "struct B\n" 17472 " {\n" 17473 " int x;\n" 17474 " };\n" 17475 " } // namespace b\n" 17476 " } // namespace a", 17477 WhitesmithsBraceStyle); 17478 17479 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner; 17480 verifyFormat("namespace a\n" 17481 " {\n" 17482 "namespace b\n" 17483 " {\n" 17484 " class A\n" 17485 " {\n" 17486 " void f()\n" 17487 " {\n" 17488 " if (true)\n" 17489 " {\n" 17490 " a();\n" 17491 " b();\n" 17492 " }\n" 17493 " }\n" 17494 " void g()\n" 17495 " {\n" 17496 " return;\n" 17497 " }\n" 17498 " };\n" 17499 " struct B\n" 17500 " {\n" 17501 " int x;\n" 17502 " };\n" 17503 " } // namespace b\n" 17504 " } // namespace a", 17505 WhitesmithsBraceStyle); 17506 17507 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All; 17508 verifyFormat("namespace a\n" 17509 " {\n" 17510 " namespace b\n" 17511 " {\n" 17512 " class A\n" 17513 " {\n" 17514 " void f()\n" 17515 " {\n" 17516 " if (true)\n" 17517 " {\n" 17518 " a();\n" 17519 " b();\n" 17520 " }\n" 17521 " }\n" 17522 " void g()\n" 17523 " {\n" 17524 " return;\n" 17525 " }\n" 17526 " };\n" 17527 " struct B\n" 17528 " {\n" 17529 " int x;\n" 17530 " };\n" 17531 " } // namespace b\n" 17532 " } // namespace a", 17533 WhitesmithsBraceStyle); 17534 17535 verifyFormat("void f()\n" 17536 " {\n" 17537 " if (true)\n" 17538 " {\n" 17539 " a();\n" 17540 " }\n" 17541 " else if (false)\n" 17542 " {\n" 17543 " b();\n" 17544 " }\n" 17545 " else\n" 17546 " {\n" 17547 " c();\n" 17548 " }\n" 17549 " }\n", 17550 WhitesmithsBraceStyle); 17551 17552 verifyFormat("void f()\n" 17553 " {\n" 17554 " for (int i = 0; i < 10; ++i)\n" 17555 " {\n" 17556 " a();\n" 17557 " }\n" 17558 " while (false)\n" 17559 " {\n" 17560 " b();\n" 17561 " }\n" 17562 " do\n" 17563 " {\n" 17564 " c();\n" 17565 " } while (false)\n" 17566 " }\n", 17567 WhitesmithsBraceStyle); 17568 17569 WhitesmithsBraceStyle.IndentCaseLabels = true; 17570 verifyFormat("void switchTest1(int a)\n" 17571 " {\n" 17572 " switch (a)\n" 17573 " {\n" 17574 " case 2:\n" 17575 " {\n" 17576 " }\n" 17577 " break;\n" 17578 " }\n" 17579 " }\n", 17580 WhitesmithsBraceStyle); 17581 17582 verifyFormat("void switchTest2(int a)\n" 17583 " {\n" 17584 " switch (a)\n" 17585 " {\n" 17586 " case 0:\n" 17587 " break;\n" 17588 " case 1:\n" 17589 " {\n" 17590 " break;\n" 17591 " }\n" 17592 " case 2:\n" 17593 " {\n" 17594 " }\n" 17595 " break;\n" 17596 " default:\n" 17597 " break;\n" 17598 " }\n" 17599 " }\n", 17600 WhitesmithsBraceStyle); 17601 17602 verifyFormat("void switchTest3(int a)\n" 17603 " {\n" 17604 " switch (a)\n" 17605 " {\n" 17606 " case 0:\n" 17607 " {\n" 17608 " foo(x);\n" 17609 " }\n" 17610 " break;\n" 17611 " default:\n" 17612 " {\n" 17613 " foo(1);\n" 17614 " }\n" 17615 " break;\n" 17616 " }\n" 17617 " }\n", 17618 WhitesmithsBraceStyle); 17619 17620 WhitesmithsBraceStyle.IndentCaseLabels = false; 17621 17622 verifyFormat("void switchTest4(int a)\n" 17623 " {\n" 17624 " switch (a)\n" 17625 " {\n" 17626 " case 2:\n" 17627 " {\n" 17628 " }\n" 17629 " break;\n" 17630 " }\n" 17631 " }\n", 17632 WhitesmithsBraceStyle); 17633 17634 verifyFormat("void switchTest5(int a)\n" 17635 " {\n" 17636 " switch (a)\n" 17637 " {\n" 17638 " case 0:\n" 17639 " break;\n" 17640 " case 1:\n" 17641 " {\n" 17642 " foo();\n" 17643 " break;\n" 17644 " }\n" 17645 " case 2:\n" 17646 " {\n" 17647 " }\n" 17648 " break;\n" 17649 " default:\n" 17650 " break;\n" 17651 " }\n" 17652 " }\n", 17653 WhitesmithsBraceStyle); 17654 17655 verifyFormat("void switchTest6(int a)\n" 17656 " {\n" 17657 " switch (a)\n" 17658 " {\n" 17659 " case 0:\n" 17660 " {\n" 17661 " foo(x);\n" 17662 " }\n" 17663 " break;\n" 17664 " default:\n" 17665 " {\n" 17666 " foo(1);\n" 17667 " }\n" 17668 " break;\n" 17669 " }\n" 17670 " }\n", 17671 WhitesmithsBraceStyle); 17672 17673 verifyFormat("enum X\n" 17674 " {\n" 17675 " Y = 0, // testing\n" 17676 " }\n", 17677 WhitesmithsBraceStyle); 17678 17679 verifyFormat("enum X\n" 17680 " {\n" 17681 " Y = 0\n" 17682 " }\n", 17683 WhitesmithsBraceStyle); 17684 verifyFormat("enum X\n" 17685 " {\n" 17686 " Y = 0,\n" 17687 " Z = 1\n" 17688 " };\n", 17689 WhitesmithsBraceStyle); 17690 17691 verifyFormat("@interface BSApplicationController ()\n" 17692 " {\n" 17693 "@private\n" 17694 " id _extraIvar;\n" 17695 " }\n" 17696 "@end\n", 17697 WhitesmithsBraceStyle); 17698 17699 verifyFormat("#ifdef _DEBUG\n" 17700 "int foo(int i = 0)\n" 17701 "#else\n" 17702 "int foo(int i = 5)\n" 17703 "#endif\n" 17704 " {\n" 17705 " return i;\n" 17706 " }", 17707 WhitesmithsBraceStyle); 17708 17709 verifyFormat("void foo() {}\n" 17710 "void bar()\n" 17711 "#ifdef _DEBUG\n" 17712 " {\n" 17713 " foo();\n" 17714 " }\n" 17715 "#else\n" 17716 " {\n" 17717 " }\n" 17718 "#endif", 17719 WhitesmithsBraceStyle); 17720 17721 verifyFormat("void foobar()\n" 17722 " {\n" 17723 " int i = 5;\n" 17724 " }\n" 17725 "#ifdef _DEBUG\n" 17726 "void bar()\n" 17727 " {\n" 17728 " }\n" 17729 "#else\n" 17730 "void bar()\n" 17731 " {\n" 17732 " foobar();\n" 17733 " }\n" 17734 "#endif", 17735 WhitesmithsBraceStyle); 17736 17737 // This shouldn't affect ObjC blocks.. 17738 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 17739 " // ...\n" 17740 " int i;\n" 17741 "}];", 17742 WhitesmithsBraceStyle); 17743 verifyFormat("void (^block)(void) = ^{\n" 17744 " // ...\n" 17745 " int i;\n" 17746 "};", 17747 WhitesmithsBraceStyle); 17748 // .. or dict literals. 17749 verifyFormat("void f()\n" 17750 " {\n" 17751 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 17752 " }", 17753 WhitesmithsBraceStyle); 17754 17755 verifyFormat("int f()\n" 17756 " { // comment\n" 17757 " return 42;\n" 17758 " }", 17759 WhitesmithsBraceStyle); 17760 17761 FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle; 17762 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = 17763 FormatStyle::SIS_OnlyFirstIf; 17764 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 17765 verifyFormat("void f(bool b)\n" 17766 " {\n" 17767 " if (b)\n" 17768 " {\n" 17769 " return;\n" 17770 " }\n" 17771 " }\n", 17772 BreakBeforeBraceShortIfs); 17773 verifyFormat("void f(bool b)\n" 17774 " {\n" 17775 " if (b) return;\n" 17776 " }\n", 17777 BreakBeforeBraceShortIfs); 17778 verifyFormat("void f(bool b)\n" 17779 " {\n" 17780 " while (b)\n" 17781 " {\n" 17782 " return;\n" 17783 " }\n" 17784 " }\n", 17785 BreakBeforeBraceShortIfs); 17786 } 17787 17788 TEST_F(FormatTest, GNUBraceBreaking) { 17789 FormatStyle GNUBraceStyle = getLLVMStyle(); 17790 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 17791 verifyFormat("namespace a\n" 17792 "{\n" 17793 "class A\n" 17794 "{\n" 17795 " void f()\n" 17796 " {\n" 17797 " int a;\n" 17798 " {\n" 17799 " int b;\n" 17800 " }\n" 17801 " if (true)\n" 17802 " {\n" 17803 " a();\n" 17804 " b();\n" 17805 " }\n" 17806 " }\n" 17807 " void g() { return; }\n" 17808 "}\n" 17809 "} // namespace a", 17810 GNUBraceStyle); 17811 17812 verifyFormat("void f()\n" 17813 "{\n" 17814 " if (true)\n" 17815 " {\n" 17816 " a();\n" 17817 " }\n" 17818 " else if (false)\n" 17819 " {\n" 17820 " b();\n" 17821 " }\n" 17822 " else\n" 17823 " {\n" 17824 " c();\n" 17825 " }\n" 17826 "}\n", 17827 GNUBraceStyle); 17828 17829 verifyFormat("void f()\n" 17830 "{\n" 17831 " for (int i = 0; i < 10; ++i)\n" 17832 " {\n" 17833 " a();\n" 17834 " }\n" 17835 " while (false)\n" 17836 " {\n" 17837 " b();\n" 17838 " }\n" 17839 " do\n" 17840 " {\n" 17841 " c();\n" 17842 " }\n" 17843 " while (false);\n" 17844 "}\n", 17845 GNUBraceStyle); 17846 17847 verifyFormat("void f(int a)\n" 17848 "{\n" 17849 " switch (a)\n" 17850 " {\n" 17851 " case 0:\n" 17852 " break;\n" 17853 " case 1:\n" 17854 " {\n" 17855 " break;\n" 17856 " }\n" 17857 " case 2:\n" 17858 " {\n" 17859 " }\n" 17860 " break;\n" 17861 " default:\n" 17862 " break;\n" 17863 " }\n" 17864 "}\n", 17865 GNUBraceStyle); 17866 17867 verifyFormat("enum X\n" 17868 "{\n" 17869 " Y = 0,\n" 17870 "}\n", 17871 GNUBraceStyle); 17872 17873 verifyFormat("@interface BSApplicationController ()\n" 17874 "{\n" 17875 "@private\n" 17876 " id _extraIvar;\n" 17877 "}\n" 17878 "@end\n", 17879 GNUBraceStyle); 17880 17881 verifyFormat("#ifdef _DEBUG\n" 17882 "int foo(int i = 0)\n" 17883 "#else\n" 17884 "int foo(int i = 5)\n" 17885 "#endif\n" 17886 "{\n" 17887 " return i;\n" 17888 "}", 17889 GNUBraceStyle); 17890 17891 verifyFormat("void foo() {}\n" 17892 "void bar()\n" 17893 "#ifdef _DEBUG\n" 17894 "{\n" 17895 " foo();\n" 17896 "}\n" 17897 "#else\n" 17898 "{\n" 17899 "}\n" 17900 "#endif", 17901 GNUBraceStyle); 17902 17903 verifyFormat("void foobar() { int i = 5; }\n" 17904 "#ifdef _DEBUG\n" 17905 "void bar() {}\n" 17906 "#else\n" 17907 "void bar() { foobar(); }\n" 17908 "#endif", 17909 GNUBraceStyle); 17910 } 17911 17912 TEST_F(FormatTest, WebKitBraceBreaking) { 17913 FormatStyle WebKitBraceStyle = getLLVMStyle(); 17914 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 17915 WebKitBraceStyle.FixNamespaceComments = false; 17916 verifyFormat("namespace a {\n" 17917 "class A {\n" 17918 " void f()\n" 17919 " {\n" 17920 " if (true) {\n" 17921 " a();\n" 17922 " b();\n" 17923 " }\n" 17924 " }\n" 17925 " void g() { return; }\n" 17926 "};\n" 17927 "enum E {\n" 17928 " A,\n" 17929 " // foo\n" 17930 " B,\n" 17931 " C\n" 17932 "};\n" 17933 "struct B {\n" 17934 " int x;\n" 17935 "};\n" 17936 "}\n", 17937 WebKitBraceStyle); 17938 verifyFormat("struct S {\n" 17939 " int Type;\n" 17940 " union {\n" 17941 " int x;\n" 17942 " double y;\n" 17943 " } Value;\n" 17944 " class C {\n" 17945 " MyFavoriteType Value;\n" 17946 " } Class;\n" 17947 "};\n", 17948 WebKitBraceStyle); 17949 } 17950 17951 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 17952 verifyFormat("void f() {\n" 17953 " try {\n" 17954 " } catch (const Exception &e) {\n" 17955 " }\n" 17956 "}\n", 17957 getLLVMStyle()); 17958 } 17959 17960 TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) { 17961 auto Style = getLLVMStyle(); 17962 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 17963 Style.AlignConsecutiveAssignments = 17964 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 17965 Style.AlignConsecutiveDeclarations = 17966 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 17967 verifyFormat("struct test demo[] = {\n" 17968 " {56, 23, \"hello\"},\n" 17969 " {-1, 93463, \"world\"},\n" 17970 " { 7, 5, \"!!\"}\n" 17971 "};\n", 17972 Style); 17973 17974 verifyFormat("struct test demo[] = {\n" 17975 " {56, 23, \"hello\"}, // first line\n" 17976 " {-1, 93463, \"world\"}, // second line\n" 17977 " { 7, 5, \"!!\"} // third line\n" 17978 "};\n", 17979 Style); 17980 17981 verifyFormat("struct test demo[4] = {\n" 17982 " { 56, 23, 21, \"oh\"}, // first line\n" 17983 " { -1, 93463, 22, \"my\"}, // second line\n" 17984 " { 7, 5, 1, \"goodness\"} // third line\n" 17985 " {234, 5, 1, \"gracious\"} // fourth line\n" 17986 "};\n", 17987 Style); 17988 17989 verifyFormat("struct test demo[3] = {\n" 17990 " {56, 23, \"hello\"},\n" 17991 " {-1, 93463, \"world\"},\n" 17992 " { 7, 5, \"!!\"}\n" 17993 "};\n", 17994 Style); 17995 17996 verifyFormat("struct test demo[3] = {\n" 17997 " {int{56}, 23, \"hello\"},\n" 17998 " {int{-1}, 93463, \"world\"},\n" 17999 " { int{7}, 5, \"!!\"}\n" 18000 "};\n", 18001 Style); 18002 18003 verifyFormat("struct test demo[] = {\n" 18004 " {56, 23, \"hello\"},\n" 18005 " {-1, 93463, \"world\"},\n" 18006 " { 7, 5, \"!!\"},\n" 18007 "};\n", 18008 Style); 18009 18010 verifyFormat("test demo[] = {\n" 18011 " {56, 23, \"hello\"},\n" 18012 " {-1, 93463, \"world\"},\n" 18013 " { 7, 5, \"!!\"},\n" 18014 "};\n", 18015 Style); 18016 18017 verifyFormat("demo = std::array<struct test, 3>{\n" 18018 " test{56, 23, \"hello\"},\n" 18019 " test{-1, 93463, \"world\"},\n" 18020 " test{ 7, 5, \"!!\"},\n" 18021 "};\n", 18022 Style); 18023 18024 verifyFormat("test demo[] = {\n" 18025 " {56, 23, \"hello\"},\n" 18026 "#if X\n" 18027 " {-1, 93463, \"world\"},\n" 18028 "#endif\n" 18029 " { 7, 5, \"!!\"}\n" 18030 "};\n", 18031 Style); 18032 18033 verifyFormat( 18034 "test demo[] = {\n" 18035 " { 7, 23,\n" 18036 " \"hello world i am a very long line that really, in any\"\n" 18037 " \"just world, ought to be split over multiple lines\"},\n" 18038 " {-1, 93463, \"world\"},\n" 18039 " {56, 5, \"!!\"}\n" 18040 "};\n", 18041 Style); 18042 18043 verifyFormat("return GradForUnaryCwise(g, {\n" 18044 " {{\"sign\"}, \"Sign\", " 18045 " {\"x\", \"dy\"}},\n" 18046 " { {\"dx\"}, \"Mul\", {\"dy\"" 18047 ", \"sign\"}},\n" 18048 "});\n", 18049 Style); 18050 18051 Style.ColumnLimit = 0; 18052 EXPECT_EQ( 18053 "test demo[] = {\n" 18054 " {56, 23, \"hello world i am a very long line that really, " 18055 "in any just world, ought to be split over multiple lines\"},\n" 18056 " {-1, 93463, " 18057 " \"world\"},\n" 18058 " { 7, 5, " 18059 " \"!!\"},\n" 18060 "};", 18061 format("test demo[] = {{56, 23, \"hello world i am a very long line " 18062 "that really, in any just world, ought to be split over multiple " 18063 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 18064 Style)); 18065 18066 Style.ColumnLimit = 80; 18067 verifyFormat("test demo[] = {\n" 18068 " {56, 23, /* a comment */ \"hello\"},\n" 18069 " {-1, 93463, \"world\"},\n" 18070 " { 7, 5, \"!!\"}\n" 18071 "};\n", 18072 Style); 18073 18074 verifyFormat("test demo[] = {\n" 18075 " {56, 23, \"hello\"},\n" 18076 " {-1, 93463, \"world\" /* comment here */},\n" 18077 " { 7, 5, \"!!\"}\n" 18078 "};\n", 18079 Style); 18080 18081 verifyFormat("test demo[] = {\n" 18082 " {56, /* a comment */ 23, \"hello\"},\n" 18083 " {-1, 93463, \"world\"},\n" 18084 " { 7, 5, \"!!\"}\n" 18085 "};\n", 18086 Style); 18087 18088 Style.ColumnLimit = 20; 18089 EXPECT_EQ( 18090 "demo = std::array<\n" 18091 " struct test, 3>{\n" 18092 " test{\n" 18093 " 56, 23,\n" 18094 " \"hello \"\n" 18095 " \"world i \"\n" 18096 " \"am a very \"\n" 18097 " \"long line \"\n" 18098 " \"that \"\n" 18099 " \"really, \"\n" 18100 " \"in any \"\n" 18101 " \"just \"\n" 18102 " \"world, \"\n" 18103 " \"ought to \"\n" 18104 " \"be split \"\n" 18105 " \"over \"\n" 18106 " \"multiple \"\n" 18107 " \"lines\"},\n" 18108 " test{-1, 93463,\n" 18109 " \"world\"},\n" 18110 " test{ 7, 5,\n" 18111 " \"!!\" },\n" 18112 "};", 18113 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world " 18114 "i am a very long line that really, in any just world, ought " 18115 "to be split over multiple lines\"},test{-1, 93463, \"world\"}," 18116 "test{7, 5, \"!!\"},};", 18117 Style)); 18118 // This caused a core dump by enabling Alignment in the LLVMStyle globally 18119 Style = getLLVMStyleWithColumns(50); 18120 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 18121 verifyFormat("static A x = {\n" 18122 " {{init1, init2, init3, init4},\n" 18123 " {init1, init2, init3, init4}}\n" 18124 "};", 18125 Style); 18126 Style.ColumnLimit = 100; 18127 EXPECT_EQ( 18128 "test demo[] = {\n" 18129 " {56, 23,\n" 18130 " \"hello world i am a very long line that really, in any just world" 18131 ", ought to be split over \"\n" 18132 " \"multiple lines\" },\n" 18133 " {-1, 93463, \"world\"},\n" 18134 " { 7, 5, \"!!\"},\n" 18135 "};", 18136 format("test demo[] = {{56, 23, \"hello world i am a very long line " 18137 "that really, in any just world, ought to be split over multiple " 18138 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 18139 Style)); 18140 18141 Style = getLLVMStyleWithColumns(50); 18142 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 18143 Style.AlignConsecutiveAssignments = 18144 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 18145 Style.AlignConsecutiveDeclarations = 18146 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 18147 verifyFormat("struct test demo[] = {\n" 18148 " {56, 23, \"hello\"},\n" 18149 " {-1, 93463, \"world\"},\n" 18150 " { 7, 5, \"!!\"}\n" 18151 "};\n" 18152 "static A x = {\n" 18153 " {{init1, init2, init3, init4},\n" 18154 " {init1, init2, init3, init4}}\n" 18155 "};", 18156 Style); 18157 Style.ColumnLimit = 100; 18158 Style.AlignConsecutiveAssignments = 18159 FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments; 18160 Style.AlignConsecutiveDeclarations = 18161 FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments; 18162 verifyFormat("struct test demo[] = {\n" 18163 " {56, 23, \"hello\"},\n" 18164 " {-1, 93463, \"world\"},\n" 18165 " { 7, 5, \"!!\"}\n" 18166 "};\n" 18167 "struct test demo[4] = {\n" 18168 " { 56, 23, 21, \"oh\"}, // first line\n" 18169 " { -1, 93463, 22, \"my\"}, // second line\n" 18170 " { 7, 5, 1, \"goodness\"} // third line\n" 18171 " {234, 5, 1, \"gracious\"} // fourth line\n" 18172 "};\n", 18173 Style); 18174 EXPECT_EQ( 18175 "test demo[] = {\n" 18176 " {56,\n" 18177 " \"hello world i am a very long line that really, in any just world" 18178 ", ought to be split over \"\n" 18179 " \"multiple lines\", 23},\n" 18180 " {-1, \"world\", 93463},\n" 18181 " { 7, \"!!\", 5},\n" 18182 "};", 18183 format("test demo[] = {{56, \"hello world i am a very long line " 18184 "that really, in any just world, ought to be split over multiple " 18185 "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};", 18186 Style)); 18187 } 18188 18189 TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) { 18190 auto Style = getLLVMStyle(); 18191 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; 18192 /* FIXME: This case gets misformatted. 18193 verifyFormat("auto foo = Items{\n" 18194 " Section{0, bar(), },\n" 18195 " Section{1, boo() }\n" 18196 "};\n", 18197 Style); 18198 */ 18199 verifyFormat("auto foo = Items{\n" 18200 " Section{\n" 18201 " 0, bar(),\n" 18202 " }\n" 18203 "};\n", 18204 Style); 18205 verifyFormat("struct test demo[] = {\n" 18206 " {56, 23, \"hello\"},\n" 18207 " {-1, 93463, \"world\"},\n" 18208 " {7, 5, \"!!\" }\n" 18209 "};\n", 18210 Style); 18211 verifyFormat("struct test demo[] = {\n" 18212 " {56, 23, \"hello\"}, // first line\n" 18213 " {-1, 93463, \"world\"}, // second line\n" 18214 " {7, 5, \"!!\" } // third line\n" 18215 "};\n", 18216 Style); 18217 verifyFormat("struct test demo[4] = {\n" 18218 " {56, 23, 21, \"oh\" }, // first line\n" 18219 " {-1, 93463, 22, \"my\" }, // second line\n" 18220 " {7, 5, 1, \"goodness\"} // third line\n" 18221 " {234, 5, 1, \"gracious\"} // fourth line\n" 18222 "};\n", 18223 Style); 18224 verifyFormat("struct test demo[3] = {\n" 18225 " {56, 23, \"hello\"},\n" 18226 " {-1, 93463, \"world\"},\n" 18227 " {7, 5, \"!!\" }\n" 18228 "};\n", 18229 Style); 18230 18231 verifyFormat("struct test demo[3] = {\n" 18232 " {int{56}, 23, \"hello\"},\n" 18233 " {int{-1}, 93463, \"world\"},\n" 18234 " {int{7}, 5, \"!!\" }\n" 18235 "};\n", 18236 Style); 18237 verifyFormat("struct test demo[] = {\n" 18238 " {56, 23, \"hello\"},\n" 18239 " {-1, 93463, \"world\"},\n" 18240 " {7, 5, \"!!\" },\n" 18241 "};\n", 18242 Style); 18243 verifyFormat("test demo[] = {\n" 18244 " {56, 23, \"hello\"},\n" 18245 " {-1, 93463, \"world\"},\n" 18246 " {7, 5, \"!!\" },\n" 18247 "};\n", 18248 Style); 18249 verifyFormat("demo = std::array<struct test, 3>{\n" 18250 " test{56, 23, \"hello\"},\n" 18251 " test{-1, 93463, \"world\"},\n" 18252 " test{7, 5, \"!!\" },\n" 18253 "};\n", 18254 Style); 18255 verifyFormat("test demo[] = {\n" 18256 " {56, 23, \"hello\"},\n" 18257 "#if X\n" 18258 " {-1, 93463, \"world\"},\n" 18259 "#endif\n" 18260 " {7, 5, \"!!\" }\n" 18261 "};\n", 18262 Style); 18263 verifyFormat( 18264 "test demo[] = {\n" 18265 " {7, 23,\n" 18266 " \"hello world i am a very long line that really, in any\"\n" 18267 " \"just world, ought to be split over multiple lines\"},\n" 18268 " {-1, 93463, \"world\" },\n" 18269 " {56, 5, \"!!\" }\n" 18270 "};\n", 18271 Style); 18272 18273 verifyFormat("return GradForUnaryCwise(g, {\n" 18274 " {{\"sign\"}, \"Sign\", {\"x\", " 18275 "\"dy\"} },\n" 18276 " {{\"dx\"}, \"Mul\", " 18277 "{\"dy\", \"sign\"}},\n" 18278 "});\n", 18279 Style); 18280 18281 Style.ColumnLimit = 0; 18282 EXPECT_EQ( 18283 "test demo[] = {\n" 18284 " {56, 23, \"hello world i am a very long line that really, in any " 18285 "just world, ought to be split over multiple lines\"},\n" 18286 " {-1, 93463, \"world\" " 18287 " },\n" 18288 " {7, 5, \"!!\" " 18289 " },\n" 18290 "};", 18291 format("test demo[] = {{56, 23, \"hello world i am a very long line " 18292 "that really, in any just world, ought to be split over multiple " 18293 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 18294 Style)); 18295 18296 Style.ColumnLimit = 80; 18297 verifyFormat("test demo[] = {\n" 18298 " {56, 23, /* a comment */ \"hello\"},\n" 18299 " {-1, 93463, \"world\" },\n" 18300 " {7, 5, \"!!\" }\n" 18301 "};\n", 18302 Style); 18303 18304 verifyFormat("test demo[] = {\n" 18305 " {56, 23, \"hello\" },\n" 18306 " {-1, 93463, \"world\" /* comment here */},\n" 18307 " {7, 5, \"!!\" }\n" 18308 "};\n", 18309 Style); 18310 18311 verifyFormat("test demo[] = {\n" 18312 " {56, /* a comment */ 23, \"hello\"},\n" 18313 " {-1, 93463, \"world\"},\n" 18314 " {7, 5, \"!!\" }\n" 18315 "};\n", 18316 Style); 18317 18318 Style.ColumnLimit = 20; 18319 EXPECT_EQ( 18320 "demo = std::array<\n" 18321 " struct test, 3>{\n" 18322 " test{\n" 18323 " 56, 23,\n" 18324 " \"hello \"\n" 18325 " \"world i \"\n" 18326 " \"am a very \"\n" 18327 " \"long line \"\n" 18328 " \"that \"\n" 18329 " \"really, \"\n" 18330 " \"in any \"\n" 18331 " \"just \"\n" 18332 " \"world, \"\n" 18333 " \"ought to \"\n" 18334 " \"be split \"\n" 18335 " \"over \"\n" 18336 " \"multiple \"\n" 18337 " \"lines\"},\n" 18338 " test{-1, 93463,\n" 18339 " \"world\"},\n" 18340 " test{7, 5,\n" 18341 " \"!!\" },\n" 18342 "};", 18343 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world " 18344 "i am a very long line that really, in any just world, ought " 18345 "to be split over multiple lines\"},test{-1, 93463, \"world\"}," 18346 "test{7, 5, \"!!\"},};", 18347 Style)); 18348 18349 // This caused a core dump by enabling Alignment in the LLVMStyle globally 18350 Style = getLLVMStyleWithColumns(50); 18351 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; 18352 verifyFormat("static A x = {\n" 18353 " {{init1, init2, init3, init4},\n" 18354 " {init1, init2, init3, init4}}\n" 18355 "};", 18356 Style); 18357 Style.ColumnLimit = 100; 18358 EXPECT_EQ( 18359 "test demo[] = {\n" 18360 " {56, 23,\n" 18361 " \"hello world i am a very long line that really, in any just world" 18362 ", ought to be split over \"\n" 18363 " \"multiple lines\" },\n" 18364 " {-1, 93463, \"world\"},\n" 18365 " {7, 5, \"!!\" },\n" 18366 "};", 18367 format("test demo[] = {{56, 23, \"hello world i am a very long line " 18368 "that really, in any just world, ought to be split over multiple " 18369 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 18370 Style)); 18371 } 18372 18373 TEST_F(FormatTest, UnderstandsPragmas) { 18374 verifyFormat("#pragma omp reduction(| : var)"); 18375 verifyFormat("#pragma omp reduction(+ : var)"); 18376 18377 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 18378 "(including parentheses).", 18379 format("#pragma mark Any non-hyphenated or hyphenated string " 18380 "(including parentheses).")); 18381 } 18382 18383 TEST_F(FormatTest, UnderstandPragmaOption) { 18384 verifyFormat("#pragma option -C -A"); 18385 18386 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 18387 } 18388 18389 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) { 18390 FormatStyle Style = getLLVMStyle(); 18391 Style.ColumnLimit = 20; 18392 18393 // See PR41213 18394 EXPECT_EQ("/*\n" 18395 " *\t9012345\n" 18396 " * /8901\n" 18397 " */", 18398 format("/*\n" 18399 " *\t9012345 /8901\n" 18400 " */", 18401 Style)); 18402 EXPECT_EQ("/*\n" 18403 " *345678\n" 18404 " *\t/8901\n" 18405 " */", 18406 format("/*\n" 18407 " *345678\t/8901\n" 18408 " */", 18409 Style)); 18410 18411 verifyFormat("int a; // the\n" 18412 " // comment", 18413 Style); 18414 EXPECT_EQ("int a; /* first line\n" 18415 " * second\n" 18416 " * line third\n" 18417 " * line\n" 18418 " */", 18419 format("int a; /* first line\n" 18420 " * second\n" 18421 " * line third\n" 18422 " * line\n" 18423 " */", 18424 Style)); 18425 EXPECT_EQ("int a; // first line\n" 18426 " // second\n" 18427 " // line third\n" 18428 " // line", 18429 format("int a; // first line\n" 18430 " // second line\n" 18431 " // third line", 18432 Style)); 18433 18434 Style.PenaltyExcessCharacter = 90; 18435 verifyFormat("int a; // the comment", Style); 18436 EXPECT_EQ("int a; // the comment\n" 18437 " // aaa", 18438 format("int a; // the comment aaa", Style)); 18439 EXPECT_EQ("int a; /* first line\n" 18440 " * second line\n" 18441 " * third line\n" 18442 " */", 18443 format("int a; /* first line\n" 18444 " * second line\n" 18445 " * third line\n" 18446 " */", 18447 Style)); 18448 EXPECT_EQ("int a; // first line\n" 18449 " // second line\n" 18450 " // third line", 18451 format("int a; // first line\n" 18452 " // second line\n" 18453 " // third line", 18454 Style)); 18455 // FIXME: Investigate why this is not getting the same layout as the test 18456 // above. 18457 EXPECT_EQ("int a; /* first line\n" 18458 " * second line\n" 18459 " * third line\n" 18460 " */", 18461 format("int a; /* first line second line third line" 18462 "\n*/", 18463 Style)); 18464 18465 EXPECT_EQ("// foo bar baz bazfoo\n" 18466 "// foo bar foo bar\n", 18467 format("// foo bar baz bazfoo\n" 18468 "// foo bar foo bar\n", 18469 Style)); 18470 EXPECT_EQ("// foo bar baz bazfoo\n" 18471 "// foo bar foo bar\n", 18472 format("// foo bar baz bazfoo\n" 18473 "// foo bar foo bar\n", 18474 Style)); 18475 18476 // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the 18477 // next one. 18478 EXPECT_EQ("// foo bar baz bazfoo\n" 18479 "// bar foo bar\n", 18480 format("// foo bar baz bazfoo bar\n" 18481 "// foo bar\n", 18482 Style)); 18483 18484 EXPECT_EQ("// foo bar baz bazfoo\n" 18485 "// foo bar baz bazfoo\n" 18486 "// bar foo bar\n", 18487 format("// foo bar baz bazfoo\n" 18488 "// foo bar baz bazfoo bar\n" 18489 "// foo bar\n", 18490 Style)); 18491 18492 EXPECT_EQ("// foo bar baz bazfoo\n" 18493 "// foo bar baz bazfoo\n" 18494 "// bar foo bar\n", 18495 format("// foo bar baz bazfoo\n" 18496 "// foo bar baz bazfoo bar\n" 18497 "// foo bar\n", 18498 Style)); 18499 18500 // Make sure we do not keep protruding characters if strict mode reflow is 18501 // cheaper than keeping protruding characters. 18502 Style.ColumnLimit = 21; 18503 EXPECT_EQ( 18504 "// foo foo foo foo\n" 18505 "// foo foo foo foo\n" 18506 "// foo foo foo foo\n", 18507 format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", Style)); 18508 18509 EXPECT_EQ("int a = /* long block\n" 18510 " comment */\n" 18511 " 42;", 18512 format("int a = /* long block comment */ 42;", Style)); 18513 } 18514 18515 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 18516 for (size_t i = 1; i < Styles.size(); ++i) \ 18517 EXPECT_EQ(Styles[0], Styles[i]) \ 18518 << "Style #" << i << " of " << Styles.size() << " differs from Style #0" 18519 18520 TEST_F(FormatTest, GetsPredefinedStyleByName) { 18521 SmallVector<FormatStyle, 3> Styles; 18522 Styles.resize(3); 18523 18524 Styles[0] = getLLVMStyle(); 18525 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 18526 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 18527 EXPECT_ALL_STYLES_EQUAL(Styles); 18528 18529 Styles[0] = getGoogleStyle(); 18530 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 18531 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 18532 EXPECT_ALL_STYLES_EQUAL(Styles); 18533 18534 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 18535 EXPECT_TRUE( 18536 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 18537 EXPECT_TRUE( 18538 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 18539 EXPECT_ALL_STYLES_EQUAL(Styles); 18540 18541 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 18542 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 18543 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 18544 EXPECT_ALL_STYLES_EQUAL(Styles); 18545 18546 Styles[0] = getMozillaStyle(); 18547 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 18548 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 18549 EXPECT_ALL_STYLES_EQUAL(Styles); 18550 18551 Styles[0] = getWebKitStyle(); 18552 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 18553 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 18554 EXPECT_ALL_STYLES_EQUAL(Styles); 18555 18556 Styles[0] = getGNUStyle(); 18557 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 18558 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 18559 EXPECT_ALL_STYLES_EQUAL(Styles); 18560 18561 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 18562 } 18563 18564 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 18565 SmallVector<FormatStyle, 8> Styles; 18566 Styles.resize(2); 18567 18568 Styles[0] = getGoogleStyle(); 18569 Styles[1] = getLLVMStyle(); 18570 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 18571 EXPECT_ALL_STYLES_EQUAL(Styles); 18572 18573 Styles.resize(5); 18574 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 18575 Styles[1] = getLLVMStyle(); 18576 Styles[1].Language = FormatStyle::LK_JavaScript; 18577 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 18578 18579 Styles[2] = getLLVMStyle(); 18580 Styles[2].Language = FormatStyle::LK_JavaScript; 18581 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 18582 "BasedOnStyle: Google", 18583 &Styles[2]) 18584 .value()); 18585 18586 Styles[3] = getLLVMStyle(); 18587 Styles[3].Language = FormatStyle::LK_JavaScript; 18588 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 18589 "Language: JavaScript", 18590 &Styles[3]) 18591 .value()); 18592 18593 Styles[4] = getLLVMStyle(); 18594 Styles[4].Language = FormatStyle::LK_JavaScript; 18595 EXPECT_EQ(0, parseConfiguration("---\n" 18596 "BasedOnStyle: LLVM\n" 18597 "IndentWidth: 123\n" 18598 "---\n" 18599 "BasedOnStyle: Google\n" 18600 "Language: JavaScript", 18601 &Styles[4]) 18602 .value()); 18603 EXPECT_ALL_STYLES_EQUAL(Styles); 18604 } 18605 18606 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 18607 Style.FIELD = false; \ 18608 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 18609 EXPECT_TRUE(Style.FIELD); \ 18610 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 18611 EXPECT_FALSE(Style.FIELD); 18612 18613 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 18614 18615 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 18616 Style.STRUCT.FIELD = false; \ 18617 EXPECT_EQ(0, \ 18618 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 18619 .value()); \ 18620 EXPECT_TRUE(Style.STRUCT.FIELD); \ 18621 EXPECT_EQ(0, \ 18622 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 18623 .value()); \ 18624 EXPECT_FALSE(Style.STRUCT.FIELD); 18625 18626 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 18627 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 18628 18629 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 18630 EXPECT_NE(VALUE, Style.FIELD) << "Initial value already the same!"; \ 18631 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 18632 EXPECT_EQ(VALUE, Style.FIELD) << "Unexpected value after parsing!" 18633 18634 TEST_F(FormatTest, ParsesConfigurationBools) { 18635 FormatStyle Style = {}; 18636 Style.Language = FormatStyle::LK_Cpp; 18637 CHECK_PARSE_BOOL(AlignTrailingComments); 18638 CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine); 18639 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 18640 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 18641 CHECK_PARSE_BOOL(AllowShortEnumsOnASingleLine); 18642 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 18643 CHECK_PARSE_BOOL(BinPackArguments); 18644 CHECK_PARSE_BOOL(BinPackParameters); 18645 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 18646 CHECK_PARSE_BOOL(BreakBeforeConceptDeclarations); 18647 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 18648 CHECK_PARSE_BOOL(BreakStringLiterals); 18649 CHECK_PARSE_BOOL(CompactNamespaces); 18650 CHECK_PARSE_BOOL(DeriveLineEnding); 18651 CHECK_PARSE_BOOL(DerivePointerAlignment); 18652 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 18653 CHECK_PARSE_BOOL(DisableFormat); 18654 CHECK_PARSE_BOOL(IndentAccessModifiers); 18655 CHECK_PARSE_BOOL(IndentCaseLabels); 18656 CHECK_PARSE_BOOL(IndentCaseBlocks); 18657 CHECK_PARSE_BOOL(IndentGotoLabels); 18658 CHECK_PARSE_BOOL(IndentRequires); 18659 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 18660 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 18661 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 18662 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 18663 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 18664 CHECK_PARSE_BOOL(ReflowComments); 18665 CHECK_PARSE_BOOL(SortUsingDeclarations); 18666 CHECK_PARSE_BOOL(SpacesInParentheses); 18667 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 18668 CHECK_PARSE_BOOL(SpacesInConditionalStatement); 18669 CHECK_PARSE_BOOL(SpaceInEmptyBlock); 18670 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 18671 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 18672 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 18673 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 18674 CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); 18675 CHECK_PARSE_BOOL(SpaceAfterLogicalNot); 18676 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 18677 CHECK_PARSE_BOOL(SpaceBeforeCaseColon); 18678 CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList); 18679 CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon); 18680 CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon); 18681 CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon); 18682 CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets); 18683 CHECK_PARSE_BOOL(UseCRLF); 18684 18685 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel); 18686 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 18687 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 18688 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 18689 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 18690 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 18691 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 18692 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 18693 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock); 18694 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 18695 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 18696 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeLambdaBody); 18697 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeWhile); 18698 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 18699 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction); 18700 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord); 18701 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace); 18702 } 18703 18704 #undef CHECK_PARSE_BOOL 18705 18706 TEST_F(FormatTest, ParsesConfiguration) { 18707 FormatStyle Style = {}; 18708 Style.Language = FormatStyle::LK_Cpp; 18709 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 18710 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 18711 ConstructorInitializerIndentWidth, 1234u); 18712 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 18713 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 18714 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 18715 CHECK_PARSE("PenaltyBreakAssignment: 1234", PenaltyBreakAssignment, 1234u); 18716 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 18717 PenaltyBreakBeforeFirstCallParameter, 1234u); 18718 CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234", 18719 PenaltyBreakTemplateDeclaration, 1234u); 18720 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 18721 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 18722 PenaltyReturnTypeOnItsOwnLine, 1234u); 18723 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 18724 SpacesBeforeTrailingComments, 1234u); 18725 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 18726 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 18727 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$"); 18728 18729 Style.QualifierAlignment = FormatStyle::QAS_Right; 18730 CHECK_PARSE("QualifierAlignment: Leave", QualifierAlignment, 18731 FormatStyle::QAS_Leave); 18732 CHECK_PARSE("QualifierAlignment: Right", QualifierAlignment, 18733 FormatStyle::QAS_Right); 18734 CHECK_PARSE("QualifierAlignment: Left", QualifierAlignment, 18735 FormatStyle::QAS_Left); 18736 CHECK_PARSE("QualifierAlignment: Custom", QualifierAlignment, 18737 FormatStyle::QAS_Custom); 18738 18739 Style.QualifierOrder.clear(); 18740 CHECK_PARSE("QualifierOrder: [ const, volatile, type ]", QualifierOrder, 18741 std::vector<std::string>({"const", "volatile", "type"})); 18742 Style.QualifierOrder.clear(); 18743 CHECK_PARSE("QualifierOrder: [const, type]", QualifierOrder, 18744 std::vector<std::string>({"const", "type"})); 18745 Style.QualifierOrder.clear(); 18746 CHECK_PARSE("QualifierOrder: [volatile, type]", QualifierOrder, 18747 std::vector<std::string>({"volatile", "type"})); 18748 18749 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 18750 CHECK_PARSE("AlignConsecutiveAssignments: None", AlignConsecutiveAssignments, 18751 FormatStyle::ACS_None); 18752 CHECK_PARSE("AlignConsecutiveAssignments: Consecutive", 18753 AlignConsecutiveAssignments, FormatStyle::ACS_Consecutive); 18754 CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLines", 18755 AlignConsecutiveAssignments, FormatStyle::ACS_AcrossEmptyLines); 18756 CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLinesAndComments", 18757 AlignConsecutiveAssignments, 18758 FormatStyle::ACS_AcrossEmptyLinesAndComments); 18759 // For backwards compability, false / true should still parse 18760 CHECK_PARSE("AlignConsecutiveAssignments: false", AlignConsecutiveAssignments, 18761 FormatStyle::ACS_None); 18762 CHECK_PARSE("AlignConsecutiveAssignments: true", AlignConsecutiveAssignments, 18763 FormatStyle::ACS_Consecutive); 18764 18765 Style.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive; 18766 CHECK_PARSE("AlignConsecutiveBitFields: None", AlignConsecutiveBitFields, 18767 FormatStyle::ACS_None); 18768 CHECK_PARSE("AlignConsecutiveBitFields: Consecutive", 18769 AlignConsecutiveBitFields, FormatStyle::ACS_Consecutive); 18770 CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLines", 18771 AlignConsecutiveBitFields, FormatStyle::ACS_AcrossEmptyLines); 18772 CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLinesAndComments", 18773 AlignConsecutiveBitFields, 18774 FormatStyle::ACS_AcrossEmptyLinesAndComments); 18775 // For backwards compability, false / true should still parse 18776 CHECK_PARSE("AlignConsecutiveBitFields: false", AlignConsecutiveBitFields, 18777 FormatStyle::ACS_None); 18778 CHECK_PARSE("AlignConsecutiveBitFields: true", AlignConsecutiveBitFields, 18779 FormatStyle::ACS_Consecutive); 18780 18781 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 18782 CHECK_PARSE("AlignConsecutiveMacros: None", AlignConsecutiveMacros, 18783 FormatStyle::ACS_None); 18784 CHECK_PARSE("AlignConsecutiveMacros: Consecutive", AlignConsecutiveMacros, 18785 FormatStyle::ACS_Consecutive); 18786 CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLines", 18787 AlignConsecutiveMacros, FormatStyle::ACS_AcrossEmptyLines); 18788 CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLinesAndComments", 18789 AlignConsecutiveMacros, 18790 FormatStyle::ACS_AcrossEmptyLinesAndComments); 18791 // For backwards compability, false / true should still parse 18792 CHECK_PARSE("AlignConsecutiveMacros: false", AlignConsecutiveMacros, 18793 FormatStyle::ACS_None); 18794 CHECK_PARSE("AlignConsecutiveMacros: true", AlignConsecutiveMacros, 18795 FormatStyle::ACS_Consecutive); 18796 18797 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 18798 CHECK_PARSE("AlignConsecutiveDeclarations: None", 18799 AlignConsecutiveDeclarations, FormatStyle::ACS_None); 18800 CHECK_PARSE("AlignConsecutiveDeclarations: Consecutive", 18801 AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive); 18802 CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLines", 18803 AlignConsecutiveDeclarations, FormatStyle::ACS_AcrossEmptyLines); 18804 CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLinesAndComments", 18805 AlignConsecutiveDeclarations, 18806 FormatStyle::ACS_AcrossEmptyLinesAndComments); 18807 // For backwards compability, false / true should still parse 18808 CHECK_PARSE("AlignConsecutiveDeclarations: false", 18809 AlignConsecutiveDeclarations, FormatStyle::ACS_None); 18810 CHECK_PARSE("AlignConsecutiveDeclarations: true", 18811 AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive); 18812 18813 Style.PointerAlignment = FormatStyle::PAS_Middle; 18814 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 18815 FormatStyle::PAS_Left); 18816 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 18817 FormatStyle::PAS_Right); 18818 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 18819 FormatStyle::PAS_Middle); 18820 Style.ReferenceAlignment = FormatStyle::RAS_Middle; 18821 CHECK_PARSE("ReferenceAlignment: Pointer", ReferenceAlignment, 18822 FormatStyle::RAS_Pointer); 18823 CHECK_PARSE("ReferenceAlignment: Left", ReferenceAlignment, 18824 FormatStyle::RAS_Left); 18825 CHECK_PARSE("ReferenceAlignment: Right", ReferenceAlignment, 18826 FormatStyle::RAS_Right); 18827 CHECK_PARSE("ReferenceAlignment: Middle", ReferenceAlignment, 18828 FormatStyle::RAS_Middle); 18829 // For backward compatibility: 18830 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 18831 FormatStyle::PAS_Left); 18832 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 18833 FormatStyle::PAS_Right); 18834 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 18835 FormatStyle::PAS_Middle); 18836 18837 Style.Standard = FormatStyle::LS_Auto; 18838 CHECK_PARSE("Standard: c++03", Standard, FormatStyle::LS_Cpp03); 18839 CHECK_PARSE("Standard: c++11", Standard, FormatStyle::LS_Cpp11); 18840 CHECK_PARSE("Standard: c++14", Standard, FormatStyle::LS_Cpp14); 18841 CHECK_PARSE("Standard: c++17", Standard, FormatStyle::LS_Cpp17); 18842 CHECK_PARSE("Standard: c++20", Standard, FormatStyle::LS_Cpp20); 18843 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 18844 CHECK_PARSE("Standard: Latest", Standard, FormatStyle::LS_Latest); 18845 // Legacy aliases: 18846 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 18847 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Latest); 18848 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 18849 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 18850 18851 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 18852 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 18853 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 18854 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 18855 FormatStyle::BOS_None); 18856 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 18857 FormatStyle::BOS_All); 18858 // For backward compatibility: 18859 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 18860 FormatStyle::BOS_None); 18861 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 18862 FormatStyle::BOS_All); 18863 18864 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 18865 CHECK_PARSE("BreakConstructorInitializers: BeforeComma", 18866 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 18867 CHECK_PARSE("BreakConstructorInitializers: AfterColon", 18868 BreakConstructorInitializers, FormatStyle::BCIS_AfterColon); 18869 CHECK_PARSE("BreakConstructorInitializers: BeforeColon", 18870 BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon); 18871 // For backward compatibility: 18872 CHECK_PARSE("BreakConstructorInitializersBeforeComma: true", 18873 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 18874 18875 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 18876 CHECK_PARSE("BreakInheritanceList: AfterComma", BreakInheritanceList, 18877 FormatStyle::BILS_AfterComma); 18878 CHECK_PARSE("BreakInheritanceList: BeforeComma", BreakInheritanceList, 18879 FormatStyle::BILS_BeforeComma); 18880 CHECK_PARSE("BreakInheritanceList: AfterColon", BreakInheritanceList, 18881 FormatStyle::BILS_AfterColon); 18882 CHECK_PARSE("BreakInheritanceList: BeforeColon", BreakInheritanceList, 18883 FormatStyle::BILS_BeforeColon); 18884 // For backward compatibility: 18885 CHECK_PARSE("BreakBeforeInheritanceComma: true", BreakInheritanceList, 18886 FormatStyle::BILS_BeforeComma); 18887 18888 Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack; 18889 CHECK_PARSE("PackConstructorInitializers: Never", PackConstructorInitializers, 18890 FormatStyle::PCIS_Never); 18891 CHECK_PARSE("PackConstructorInitializers: BinPack", 18892 PackConstructorInitializers, FormatStyle::PCIS_BinPack); 18893 CHECK_PARSE("PackConstructorInitializers: CurrentLine", 18894 PackConstructorInitializers, FormatStyle::PCIS_CurrentLine); 18895 CHECK_PARSE("PackConstructorInitializers: NextLine", 18896 PackConstructorInitializers, FormatStyle::PCIS_NextLine); 18897 // For backward compatibility: 18898 CHECK_PARSE("BasedOnStyle: Google\n" 18899 "ConstructorInitializerAllOnOneLineOrOnePerLine: true\n" 18900 "AllowAllConstructorInitializersOnNextLine: false", 18901 PackConstructorInitializers, FormatStyle::PCIS_CurrentLine); 18902 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 18903 CHECK_PARSE("BasedOnStyle: Google\n" 18904 "ConstructorInitializerAllOnOneLineOrOnePerLine: false", 18905 PackConstructorInitializers, FormatStyle::PCIS_BinPack); 18906 CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n" 18907 "AllowAllConstructorInitializersOnNextLine: true", 18908 PackConstructorInitializers, FormatStyle::PCIS_NextLine); 18909 Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack; 18910 CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n" 18911 "AllowAllConstructorInitializersOnNextLine: false", 18912 PackConstructorInitializers, FormatStyle::PCIS_CurrentLine); 18913 18914 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 18915 CHECK_PARSE("EmptyLineBeforeAccessModifier: Never", 18916 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Never); 18917 CHECK_PARSE("EmptyLineBeforeAccessModifier: Leave", 18918 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Leave); 18919 CHECK_PARSE("EmptyLineBeforeAccessModifier: LogicalBlock", 18920 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_LogicalBlock); 18921 CHECK_PARSE("EmptyLineBeforeAccessModifier: Always", 18922 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Always); 18923 18924 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 18925 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 18926 FormatStyle::BAS_Align); 18927 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 18928 FormatStyle::BAS_DontAlign); 18929 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 18930 FormatStyle::BAS_AlwaysBreak); 18931 // For backward compatibility: 18932 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 18933 FormatStyle::BAS_DontAlign); 18934 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 18935 FormatStyle::BAS_Align); 18936 18937 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left; 18938 CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines, 18939 FormatStyle::ENAS_DontAlign); 18940 CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines, 18941 FormatStyle::ENAS_Left); 18942 CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines, 18943 FormatStyle::ENAS_Right); 18944 // For backward compatibility: 18945 CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines, 18946 FormatStyle::ENAS_Left); 18947 CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines, 18948 FormatStyle::ENAS_Right); 18949 18950 Style.AlignOperands = FormatStyle::OAS_Align; 18951 CHECK_PARSE("AlignOperands: DontAlign", AlignOperands, 18952 FormatStyle::OAS_DontAlign); 18953 CHECK_PARSE("AlignOperands: Align", AlignOperands, FormatStyle::OAS_Align); 18954 CHECK_PARSE("AlignOperands: AlignAfterOperator", AlignOperands, 18955 FormatStyle::OAS_AlignAfterOperator); 18956 // For backward compatibility: 18957 CHECK_PARSE("AlignOperands: false", AlignOperands, 18958 FormatStyle::OAS_DontAlign); 18959 CHECK_PARSE("AlignOperands: true", AlignOperands, FormatStyle::OAS_Align); 18960 18961 Style.UseTab = FormatStyle::UT_ForIndentation; 18962 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 18963 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 18964 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 18965 CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab, 18966 FormatStyle::UT_ForContinuationAndIndentation); 18967 CHECK_PARSE("UseTab: AlignWithSpaces", UseTab, 18968 FormatStyle::UT_AlignWithSpaces); 18969 // For backward compatibility: 18970 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 18971 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 18972 18973 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 18974 CHECK_PARSE("AllowShortBlocksOnASingleLine: Never", 18975 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 18976 CHECK_PARSE("AllowShortBlocksOnASingleLine: Empty", 18977 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Empty); 18978 CHECK_PARSE("AllowShortBlocksOnASingleLine: Always", 18979 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always); 18980 // For backward compatibility: 18981 CHECK_PARSE("AllowShortBlocksOnASingleLine: false", 18982 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 18983 CHECK_PARSE("AllowShortBlocksOnASingleLine: true", 18984 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always); 18985 18986 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 18987 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 18988 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 18989 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 18990 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 18991 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 18992 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 18993 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 18994 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 18995 // For backward compatibility: 18996 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 18997 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 18998 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 18999 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 19000 19001 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Both; 19002 CHECK_PARSE("SpaceAroundPointerQualifiers: Default", 19003 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Default); 19004 CHECK_PARSE("SpaceAroundPointerQualifiers: Before", 19005 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Before); 19006 CHECK_PARSE("SpaceAroundPointerQualifiers: After", 19007 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_After); 19008 CHECK_PARSE("SpaceAroundPointerQualifiers: Both", 19009 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Both); 19010 19011 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 19012 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 19013 FormatStyle::SBPO_Never); 19014 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 19015 FormatStyle::SBPO_Always); 19016 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 19017 FormatStyle::SBPO_ControlStatements); 19018 CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptControlMacros", 19019 SpaceBeforeParens, 19020 FormatStyle::SBPO_ControlStatementsExceptControlMacros); 19021 CHECK_PARSE("SpaceBeforeParens: NonEmptyParentheses", SpaceBeforeParens, 19022 FormatStyle::SBPO_NonEmptyParentheses); 19023 CHECK_PARSE("SpaceBeforeParens: Custom", SpaceBeforeParens, 19024 FormatStyle::SBPO_Custom); 19025 // For backward compatibility: 19026 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 19027 FormatStyle::SBPO_Never); 19028 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 19029 FormatStyle::SBPO_ControlStatements); 19030 CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptForEachMacros", 19031 SpaceBeforeParens, 19032 FormatStyle::SBPO_ControlStatementsExceptControlMacros); 19033 19034 Style.ColumnLimit = 123; 19035 FormatStyle BaseStyle = getLLVMStyle(); 19036 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 19037 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 19038 19039 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 19040 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 19041 FormatStyle::BS_Attach); 19042 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 19043 FormatStyle::BS_Linux); 19044 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 19045 FormatStyle::BS_Mozilla); 19046 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 19047 FormatStyle::BS_Stroustrup); 19048 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 19049 FormatStyle::BS_Allman); 19050 CHECK_PARSE("BreakBeforeBraces: Whitesmiths", BreakBeforeBraces, 19051 FormatStyle::BS_Whitesmiths); 19052 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 19053 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 19054 FormatStyle::BS_WebKit); 19055 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 19056 FormatStyle::BS_Custom); 19057 19058 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never; 19059 CHECK_PARSE("BraceWrapping:\n" 19060 " AfterControlStatement: MultiLine", 19061 BraceWrapping.AfterControlStatement, 19062 FormatStyle::BWACS_MultiLine); 19063 CHECK_PARSE("BraceWrapping:\n" 19064 " AfterControlStatement: Always", 19065 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always); 19066 CHECK_PARSE("BraceWrapping:\n" 19067 " AfterControlStatement: Never", 19068 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never); 19069 // For backward compatibility: 19070 CHECK_PARSE("BraceWrapping:\n" 19071 " AfterControlStatement: true", 19072 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always); 19073 CHECK_PARSE("BraceWrapping:\n" 19074 " AfterControlStatement: false", 19075 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never); 19076 19077 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 19078 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 19079 FormatStyle::RTBS_None); 19080 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 19081 FormatStyle::RTBS_All); 19082 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 19083 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 19084 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 19085 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 19086 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 19087 AlwaysBreakAfterReturnType, 19088 FormatStyle::RTBS_TopLevelDefinitions); 19089 19090 Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 19091 CHECK_PARSE("AlwaysBreakTemplateDeclarations: No", 19092 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_No); 19093 CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine", 19094 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine); 19095 CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes", 19096 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes); 19097 CHECK_PARSE("AlwaysBreakTemplateDeclarations: false", 19098 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine); 19099 CHECK_PARSE("AlwaysBreakTemplateDeclarations: true", 19100 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes); 19101 19102 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 19103 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 19104 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 19105 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 19106 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 19107 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 19108 AlwaysBreakAfterDefinitionReturnType, 19109 FormatStyle::DRTBS_TopLevel); 19110 19111 Style.NamespaceIndentation = FormatStyle::NI_All; 19112 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 19113 FormatStyle::NI_None); 19114 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 19115 FormatStyle::NI_Inner); 19116 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 19117 FormatStyle::NI_All); 19118 19119 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_OnlyFirstIf; 19120 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Never", 19121 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never); 19122 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: WithoutElse", 19123 AllowShortIfStatementsOnASingleLine, 19124 FormatStyle::SIS_WithoutElse); 19125 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: OnlyFirstIf", 19126 AllowShortIfStatementsOnASingleLine, 19127 FormatStyle::SIS_OnlyFirstIf); 19128 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: AllIfsAndElse", 19129 AllowShortIfStatementsOnASingleLine, 19130 FormatStyle::SIS_AllIfsAndElse); 19131 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Always", 19132 AllowShortIfStatementsOnASingleLine, 19133 FormatStyle::SIS_OnlyFirstIf); 19134 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: false", 19135 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never); 19136 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: true", 19137 AllowShortIfStatementsOnASingleLine, 19138 FormatStyle::SIS_WithoutElse); 19139 19140 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; 19141 CHECK_PARSE("IndentExternBlock: AfterExternBlock", IndentExternBlock, 19142 FormatStyle::IEBS_AfterExternBlock); 19143 CHECK_PARSE("IndentExternBlock: Indent", IndentExternBlock, 19144 FormatStyle::IEBS_Indent); 19145 CHECK_PARSE("IndentExternBlock: NoIndent", IndentExternBlock, 19146 FormatStyle::IEBS_NoIndent); 19147 CHECK_PARSE("IndentExternBlock: true", IndentExternBlock, 19148 FormatStyle::IEBS_Indent); 19149 CHECK_PARSE("IndentExternBlock: false", IndentExternBlock, 19150 FormatStyle::IEBS_NoIndent); 19151 19152 Style.BitFieldColonSpacing = FormatStyle::BFCS_None; 19153 CHECK_PARSE("BitFieldColonSpacing: Both", BitFieldColonSpacing, 19154 FormatStyle::BFCS_Both); 19155 CHECK_PARSE("BitFieldColonSpacing: None", BitFieldColonSpacing, 19156 FormatStyle::BFCS_None); 19157 CHECK_PARSE("BitFieldColonSpacing: Before", BitFieldColonSpacing, 19158 FormatStyle::BFCS_Before); 19159 CHECK_PARSE("BitFieldColonSpacing: After", BitFieldColonSpacing, 19160 FormatStyle::BFCS_After); 19161 19162 Style.SortJavaStaticImport = FormatStyle::SJSIO_Before; 19163 CHECK_PARSE("SortJavaStaticImport: After", SortJavaStaticImport, 19164 FormatStyle::SJSIO_After); 19165 CHECK_PARSE("SortJavaStaticImport: Before", SortJavaStaticImport, 19166 FormatStyle::SJSIO_Before); 19167 19168 // FIXME: This is required because parsing a configuration simply overwrites 19169 // the first N elements of the list instead of resetting it. 19170 Style.ForEachMacros.clear(); 19171 std::vector<std::string> BoostForeach; 19172 BoostForeach.push_back("BOOST_FOREACH"); 19173 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 19174 std::vector<std::string> BoostAndQForeach; 19175 BoostAndQForeach.push_back("BOOST_FOREACH"); 19176 BoostAndQForeach.push_back("Q_FOREACH"); 19177 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 19178 BoostAndQForeach); 19179 19180 Style.IfMacros.clear(); 19181 std::vector<std::string> CustomIfs; 19182 CustomIfs.push_back("MYIF"); 19183 CHECK_PARSE("IfMacros: [MYIF]", IfMacros, CustomIfs); 19184 19185 Style.AttributeMacros.clear(); 19186 CHECK_PARSE("BasedOnStyle: LLVM", AttributeMacros, 19187 std::vector<std::string>{"__capability"}); 19188 CHECK_PARSE("AttributeMacros: [attr1, attr2]", AttributeMacros, 19189 std::vector<std::string>({"attr1", "attr2"})); 19190 19191 Style.StatementAttributeLikeMacros.clear(); 19192 CHECK_PARSE("StatementAttributeLikeMacros: [emit,Q_EMIT]", 19193 StatementAttributeLikeMacros, 19194 std::vector<std::string>({"emit", "Q_EMIT"})); 19195 19196 Style.StatementMacros.clear(); 19197 CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros, 19198 std::vector<std::string>{"QUNUSED"}); 19199 CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros, 19200 std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"})); 19201 19202 Style.NamespaceMacros.clear(); 19203 CHECK_PARSE("NamespaceMacros: [TESTSUITE]", NamespaceMacros, 19204 std::vector<std::string>{"TESTSUITE"}); 19205 CHECK_PARSE("NamespaceMacros: [TESTSUITE, SUITE]", NamespaceMacros, 19206 std::vector<std::string>({"TESTSUITE", "SUITE"})); 19207 19208 Style.WhitespaceSensitiveMacros.clear(); 19209 CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE]", 19210 WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"}); 19211 CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE, ASSERT]", 19212 WhitespaceSensitiveMacros, 19213 std::vector<std::string>({"STRINGIZE", "ASSERT"})); 19214 Style.WhitespaceSensitiveMacros.clear(); 19215 CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE']", 19216 WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"}); 19217 CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE', 'ASSERT']", 19218 WhitespaceSensitiveMacros, 19219 std::vector<std::string>({"STRINGIZE", "ASSERT"})); 19220 19221 Style.IncludeStyle.IncludeCategories.clear(); 19222 std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = { 19223 {"abc/.*", 2, 0, false}, {".*", 1, 0, true}}; 19224 CHECK_PARSE("IncludeCategories:\n" 19225 " - Regex: abc/.*\n" 19226 " Priority: 2\n" 19227 " - Regex: .*\n" 19228 " Priority: 1\n" 19229 " CaseSensitive: true\n", 19230 IncludeStyle.IncludeCategories, ExpectedCategories); 19231 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex, 19232 "abc$"); 19233 CHECK_PARSE("IncludeIsMainSourceRegex: 'abc$'", 19234 IncludeStyle.IncludeIsMainSourceRegex, "abc$"); 19235 19236 Style.SortIncludes = FormatStyle::SI_Never; 19237 CHECK_PARSE("SortIncludes: true", SortIncludes, 19238 FormatStyle::SI_CaseSensitive); 19239 CHECK_PARSE("SortIncludes: false", SortIncludes, FormatStyle::SI_Never); 19240 CHECK_PARSE("SortIncludes: CaseInsensitive", SortIncludes, 19241 FormatStyle::SI_CaseInsensitive); 19242 CHECK_PARSE("SortIncludes: CaseSensitive", SortIncludes, 19243 FormatStyle::SI_CaseSensitive); 19244 CHECK_PARSE("SortIncludes: Never", SortIncludes, FormatStyle::SI_Never); 19245 19246 Style.RawStringFormats.clear(); 19247 std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = { 19248 { 19249 FormatStyle::LK_TextProto, 19250 {"pb", "proto"}, 19251 {"PARSE_TEXT_PROTO"}, 19252 /*CanonicalDelimiter=*/"", 19253 "llvm", 19254 }, 19255 { 19256 FormatStyle::LK_Cpp, 19257 {"cc", "cpp"}, 19258 {"C_CODEBLOCK", "CPPEVAL"}, 19259 /*CanonicalDelimiter=*/"cc", 19260 /*BasedOnStyle=*/"", 19261 }, 19262 }; 19263 19264 CHECK_PARSE("RawStringFormats:\n" 19265 " - Language: TextProto\n" 19266 " Delimiters:\n" 19267 " - 'pb'\n" 19268 " - 'proto'\n" 19269 " EnclosingFunctions:\n" 19270 " - 'PARSE_TEXT_PROTO'\n" 19271 " BasedOnStyle: llvm\n" 19272 " - Language: Cpp\n" 19273 " Delimiters:\n" 19274 " - 'cc'\n" 19275 " - 'cpp'\n" 19276 " EnclosingFunctions:\n" 19277 " - 'C_CODEBLOCK'\n" 19278 " - 'CPPEVAL'\n" 19279 " CanonicalDelimiter: 'cc'", 19280 RawStringFormats, ExpectedRawStringFormats); 19281 19282 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 19283 " Minimum: 0\n" 19284 " Maximum: 0", 19285 SpacesInLineCommentPrefix.Minimum, 0u); 19286 EXPECT_EQ(Style.SpacesInLineCommentPrefix.Maximum, 0u); 19287 Style.SpacesInLineCommentPrefix.Minimum = 1; 19288 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 19289 " Minimum: 2", 19290 SpacesInLineCommentPrefix.Minimum, 0u); 19291 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 19292 " Maximum: -1", 19293 SpacesInLineCommentPrefix.Maximum, -1u); 19294 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 19295 " Minimum: 2", 19296 SpacesInLineCommentPrefix.Minimum, 2u); 19297 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 19298 " Maximum: 1", 19299 SpacesInLineCommentPrefix.Maximum, 1u); 19300 EXPECT_EQ(Style.SpacesInLineCommentPrefix.Minimum, 1u); 19301 19302 Style.SpacesInAngles = FormatStyle::SIAS_Always; 19303 CHECK_PARSE("SpacesInAngles: Never", SpacesInAngles, FormatStyle::SIAS_Never); 19304 CHECK_PARSE("SpacesInAngles: Always", SpacesInAngles, 19305 FormatStyle::SIAS_Always); 19306 CHECK_PARSE("SpacesInAngles: Leave", SpacesInAngles, FormatStyle::SIAS_Leave); 19307 // For backward compatibility: 19308 CHECK_PARSE("SpacesInAngles: false", SpacesInAngles, FormatStyle::SIAS_Never); 19309 CHECK_PARSE("SpacesInAngles: true", SpacesInAngles, FormatStyle::SIAS_Always); 19310 } 19311 19312 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 19313 FormatStyle Style = {}; 19314 Style.Language = FormatStyle::LK_Cpp; 19315 CHECK_PARSE("Language: Cpp\n" 19316 "IndentWidth: 12", 19317 IndentWidth, 12u); 19318 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 19319 "IndentWidth: 34", 19320 &Style), 19321 ParseError::Unsuitable); 19322 FormatStyle BinPackedTCS = {}; 19323 BinPackedTCS.Language = FormatStyle::LK_JavaScript; 19324 EXPECT_EQ(parseConfiguration("BinPackArguments: true\n" 19325 "InsertTrailingCommas: Wrapped", 19326 &BinPackedTCS), 19327 ParseError::BinPackTrailingCommaConflict); 19328 EXPECT_EQ(12u, Style.IndentWidth); 19329 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 19330 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 19331 19332 Style.Language = FormatStyle::LK_JavaScript; 19333 CHECK_PARSE("Language: JavaScript\n" 19334 "IndentWidth: 12", 19335 IndentWidth, 12u); 19336 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 19337 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 19338 "IndentWidth: 34", 19339 &Style), 19340 ParseError::Unsuitable); 19341 EXPECT_EQ(23u, Style.IndentWidth); 19342 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 19343 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 19344 19345 CHECK_PARSE("BasedOnStyle: LLVM\n" 19346 "IndentWidth: 67", 19347 IndentWidth, 67u); 19348 19349 CHECK_PARSE("---\n" 19350 "Language: JavaScript\n" 19351 "IndentWidth: 12\n" 19352 "---\n" 19353 "Language: Cpp\n" 19354 "IndentWidth: 34\n" 19355 "...\n", 19356 IndentWidth, 12u); 19357 19358 Style.Language = FormatStyle::LK_Cpp; 19359 CHECK_PARSE("---\n" 19360 "Language: JavaScript\n" 19361 "IndentWidth: 12\n" 19362 "---\n" 19363 "Language: Cpp\n" 19364 "IndentWidth: 34\n" 19365 "...\n", 19366 IndentWidth, 34u); 19367 CHECK_PARSE("---\n" 19368 "IndentWidth: 78\n" 19369 "---\n" 19370 "Language: JavaScript\n" 19371 "IndentWidth: 56\n" 19372 "...\n", 19373 IndentWidth, 78u); 19374 19375 Style.ColumnLimit = 123; 19376 Style.IndentWidth = 234; 19377 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 19378 Style.TabWidth = 345; 19379 EXPECT_FALSE(parseConfiguration("---\n" 19380 "IndentWidth: 456\n" 19381 "BreakBeforeBraces: Allman\n" 19382 "---\n" 19383 "Language: JavaScript\n" 19384 "IndentWidth: 111\n" 19385 "TabWidth: 111\n" 19386 "---\n" 19387 "Language: Cpp\n" 19388 "BreakBeforeBraces: Stroustrup\n" 19389 "TabWidth: 789\n" 19390 "...\n", 19391 &Style)); 19392 EXPECT_EQ(123u, Style.ColumnLimit); 19393 EXPECT_EQ(456u, Style.IndentWidth); 19394 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 19395 EXPECT_EQ(789u, Style.TabWidth); 19396 19397 EXPECT_EQ(parseConfiguration("---\n" 19398 "Language: JavaScript\n" 19399 "IndentWidth: 56\n" 19400 "---\n" 19401 "IndentWidth: 78\n" 19402 "...\n", 19403 &Style), 19404 ParseError::Error); 19405 EXPECT_EQ(parseConfiguration("---\n" 19406 "Language: JavaScript\n" 19407 "IndentWidth: 56\n" 19408 "---\n" 19409 "Language: JavaScript\n" 19410 "IndentWidth: 78\n" 19411 "...\n", 19412 &Style), 19413 ParseError::Error); 19414 19415 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 19416 } 19417 19418 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 19419 FormatStyle Style = {}; 19420 Style.Language = FormatStyle::LK_JavaScript; 19421 Style.BreakBeforeTernaryOperators = true; 19422 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 19423 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 19424 19425 Style.BreakBeforeTernaryOperators = true; 19426 EXPECT_EQ(0, parseConfiguration("---\n" 19427 "BasedOnStyle: Google\n" 19428 "---\n" 19429 "Language: JavaScript\n" 19430 "IndentWidth: 76\n" 19431 "...\n", 19432 &Style) 19433 .value()); 19434 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 19435 EXPECT_EQ(76u, Style.IndentWidth); 19436 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 19437 } 19438 19439 TEST_F(FormatTest, ConfigurationRoundTripTest) { 19440 FormatStyle Style = getLLVMStyle(); 19441 std::string YAML = configurationAsText(Style); 19442 FormatStyle ParsedStyle = {}; 19443 ParsedStyle.Language = FormatStyle::LK_Cpp; 19444 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 19445 EXPECT_EQ(Style, ParsedStyle); 19446 } 19447 19448 TEST_F(FormatTest, WorksFor8bitEncodings) { 19449 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 19450 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 19451 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 19452 "\"\xef\xee\xf0\xf3...\"", 19453 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 19454 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 19455 "\xef\xee\xf0\xf3...\"", 19456 getLLVMStyleWithColumns(12))); 19457 } 19458 19459 TEST_F(FormatTest, HandlesUTF8BOM) { 19460 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 19461 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 19462 format("\xef\xbb\xbf#include <iostream>")); 19463 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 19464 format("\xef\xbb\xbf\n#include <iostream>")); 19465 } 19466 19467 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 19468 #if !defined(_MSC_VER) 19469 19470 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 19471 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 19472 getLLVMStyleWithColumns(35)); 19473 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 19474 getLLVMStyleWithColumns(31)); 19475 verifyFormat("// Однажды в студёную зимнюю пору...", 19476 getLLVMStyleWithColumns(36)); 19477 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 19478 verifyFormat("/* Однажды в студёную зимнюю пору... */", 19479 getLLVMStyleWithColumns(39)); 19480 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 19481 getLLVMStyleWithColumns(35)); 19482 } 19483 19484 TEST_F(FormatTest, SplitsUTF8Strings) { 19485 // Non-printable characters' width is currently considered to be the length in 19486 // bytes in UTF8. The characters can be displayed in very different manner 19487 // (zero-width, single width with a substitution glyph, expanded to their code 19488 // (e.g. "<8d>"), so there's no single correct way to handle them. 19489 EXPECT_EQ("\"aaaaÄ\"\n" 19490 "\"\xc2\x8d\";", 19491 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 19492 EXPECT_EQ("\"aaaaaaaÄ\"\n" 19493 "\"\xc2\x8d\";", 19494 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 19495 EXPECT_EQ("\"Однажды, в \"\n" 19496 "\"студёную \"\n" 19497 "\"зимнюю \"\n" 19498 "\"пору,\"", 19499 format("\"Однажды, в студёную зимнюю пору,\"", 19500 getLLVMStyleWithColumns(13))); 19501 EXPECT_EQ( 19502 "\"一 二 三 \"\n" 19503 "\"四 五六 \"\n" 19504 "\"七 八 九 \"\n" 19505 "\"十\"", 19506 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 19507 EXPECT_EQ("\"一\t\"\n" 19508 "\"二 \t\"\n" 19509 "\"三 四 \"\n" 19510 "\"五\t\"\n" 19511 "\"六 \t\"\n" 19512 "\"七 \"\n" 19513 "\"八九十\tqq\"", 19514 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 19515 getLLVMStyleWithColumns(11))); 19516 19517 // UTF8 character in an escape sequence. 19518 EXPECT_EQ("\"aaaaaa\"\n" 19519 "\"\\\xC2\x8D\"", 19520 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 19521 } 19522 19523 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 19524 EXPECT_EQ("const char *sssss =\n" 19525 " \"一二三四五六七八\\\n" 19526 " 九 十\";", 19527 format("const char *sssss = \"一二三四五六七八\\\n" 19528 " 九 十\";", 19529 getLLVMStyleWithColumns(30))); 19530 } 19531 19532 TEST_F(FormatTest, SplitsUTF8LineComments) { 19533 EXPECT_EQ("// aaaaÄ\xc2\x8d", 19534 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 19535 EXPECT_EQ("// Я из лесу\n" 19536 "// вышел; был\n" 19537 "// сильный\n" 19538 "// мороз.", 19539 format("// Я из лесу вышел; был сильный мороз.", 19540 getLLVMStyleWithColumns(13))); 19541 EXPECT_EQ("// 一二三\n" 19542 "// 四五六七\n" 19543 "// 八 九\n" 19544 "// 十", 19545 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 19546 } 19547 19548 TEST_F(FormatTest, SplitsUTF8BlockComments) { 19549 EXPECT_EQ("/* Гляжу,\n" 19550 " * поднимается\n" 19551 " * медленно в\n" 19552 " * гору\n" 19553 " * Лошадка,\n" 19554 " * везущая\n" 19555 " * хворосту\n" 19556 " * воз. */", 19557 format("/* Гляжу, поднимается медленно в гору\n" 19558 " * Лошадка, везущая хворосту воз. */", 19559 getLLVMStyleWithColumns(13))); 19560 EXPECT_EQ( 19561 "/* 一二三\n" 19562 " * 四五六七\n" 19563 " * 八 九\n" 19564 " * 十 */", 19565 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 19566 EXPECT_EQ("/* \n" 19567 " * \n" 19568 " * - */", 19569 format("/* - */", getLLVMStyleWithColumns(12))); 19570 } 19571 19572 #endif // _MSC_VER 19573 19574 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 19575 FormatStyle Style = getLLVMStyle(); 19576 19577 Style.ConstructorInitializerIndentWidth = 4; 19578 verifyFormat( 19579 "SomeClass::Constructor()\n" 19580 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 19581 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 19582 Style); 19583 19584 Style.ConstructorInitializerIndentWidth = 2; 19585 verifyFormat( 19586 "SomeClass::Constructor()\n" 19587 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 19588 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 19589 Style); 19590 19591 Style.ConstructorInitializerIndentWidth = 0; 19592 verifyFormat( 19593 "SomeClass::Constructor()\n" 19594 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 19595 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 19596 Style); 19597 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 19598 verifyFormat( 19599 "SomeLongTemplateVariableName<\n" 19600 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 19601 Style); 19602 verifyFormat("bool smaller = 1 < " 19603 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 19604 " " 19605 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 19606 Style); 19607 19608 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 19609 verifyFormat("SomeClass::Constructor() :\n" 19610 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n" 19611 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}", 19612 Style); 19613 } 19614 19615 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 19616 FormatStyle Style = getLLVMStyle(); 19617 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 19618 Style.ConstructorInitializerIndentWidth = 4; 19619 verifyFormat("SomeClass::Constructor()\n" 19620 " : a(a)\n" 19621 " , b(b)\n" 19622 " , c(c) {}", 19623 Style); 19624 verifyFormat("SomeClass::Constructor()\n" 19625 " : a(a) {}", 19626 Style); 19627 19628 Style.ColumnLimit = 0; 19629 verifyFormat("SomeClass::Constructor()\n" 19630 " : a(a) {}", 19631 Style); 19632 verifyFormat("SomeClass::Constructor() noexcept\n" 19633 " : a(a) {}", 19634 Style); 19635 verifyFormat("SomeClass::Constructor()\n" 19636 " : a(a)\n" 19637 " , b(b)\n" 19638 " , c(c) {}", 19639 Style); 19640 verifyFormat("SomeClass::Constructor()\n" 19641 " : a(a) {\n" 19642 " foo();\n" 19643 " bar();\n" 19644 "}", 19645 Style); 19646 19647 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 19648 verifyFormat("SomeClass::Constructor()\n" 19649 " : a(a)\n" 19650 " , b(b)\n" 19651 " , c(c) {\n}", 19652 Style); 19653 verifyFormat("SomeClass::Constructor()\n" 19654 " : a(a) {\n}", 19655 Style); 19656 19657 Style.ColumnLimit = 80; 19658 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 19659 Style.ConstructorInitializerIndentWidth = 2; 19660 verifyFormat("SomeClass::Constructor()\n" 19661 " : a(a)\n" 19662 " , b(b)\n" 19663 " , c(c) {}", 19664 Style); 19665 19666 Style.ConstructorInitializerIndentWidth = 0; 19667 verifyFormat("SomeClass::Constructor()\n" 19668 ": a(a)\n" 19669 ", b(b)\n" 19670 ", c(c) {}", 19671 Style); 19672 19673 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 19674 Style.ConstructorInitializerIndentWidth = 4; 19675 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 19676 verifyFormat( 19677 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 19678 Style); 19679 verifyFormat( 19680 "SomeClass::Constructor()\n" 19681 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 19682 Style); 19683 Style.ConstructorInitializerIndentWidth = 4; 19684 Style.ColumnLimit = 60; 19685 verifyFormat("SomeClass::Constructor()\n" 19686 " : aaaaaaaa(aaaaaaaa)\n" 19687 " , aaaaaaaa(aaaaaaaa)\n" 19688 " , aaaaaaaa(aaaaaaaa) {}", 19689 Style); 19690 } 19691 19692 TEST_F(FormatTest, ConstructorInitializersWithPreprocessorDirective) { 19693 FormatStyle Style = getLLVMStyle(); 19694 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 19695 Style.ConstructorInitializerIndentWidth = 4; 19696 verifyFormat("SomeClass::Constructor()\n" 19697 " : a{a}\n" 19698 " , b{b} {}", 19699 Style); 19700 verifyFormat("SomeClass::Constructor()\n" 19701 " : a{a}\n" 19702 "#if CONDITION\n" 19703 " , b{b}\n" 19704 "#endif\n" 19705 "{\n}", 19706 Style); 19707 Style.ConstructorInitializerIndentWidth = 2; 19708 verifyFormat("SomeClass::Constructor()\n" 19709 "#if CONDITION\n" 19710 " : a{a}\n" 19711 "#endif\n" 19712 " , b{b}\n" 19713 " , c{c} {\n}", 19714 Style); 19715 Style.ConstructorInitializerIndentWidth = 0; 19716 verifyFormat("SomeClass::Constructor()\n" 19717 ": a{a}\n" 19718 "#ifdef CONDITION\n" 19719 ", b{b}\n" 19720 "#else\n" 19721 ", c{c}\n" 19722 "#endif\n" 19723 ", d{d} {\n}", 19724 Style); 19725 Style.ConstructorInitializerIndentWidth = 4; 19726 verifyFormat("SomeClass::Constructor()\n" 19727 " : a{a}\n" 19728 "#if WINDOWS\n" 19729 "#if DEBUG\n" 19730 " , b{0}\n" 19731 "#else\n" 19732 " , b{1}\n" 19733 "#endif\n" 19734 "#else\n" 19735 "#if DEBUG\n" 19736 " , b{2}\n" 19737 "#else\n" 19738 " , b{3}\n" 19739 "#endif\n" 19740 "#endif\n" 19741 "{\n}", 19742 Style); 19743 verifyFormat("SomeClass::Constructor()\n" 19744 " : a{a}\n" 19745 "#if WINDOWS\n" 19746 " , b{0}\n" 19747 "#if DEBUG\n" 19748 " , c{0}\n" 19749 "#else\n" 19750 " , c{1}\n" 19751 "#endif\n" 19752 "#else\n" 19753 "#if DEBUG\n" 19754 " , c{2}\n" 19755 "#else\n" 19756 " , c{3}\n" 19757 "#endif\n" 19758 " , b{1}\n" 19759 "#endif\n" 19760 "{\n}", 19761 Style); 19762 } 19763 19764 TEST_F(FormatTest, Destructors) { 19765 verifyFormat("void F(int &i) { i.~int(); }"); 19766 verifyFormat("void F(int &i) { i->~int(); }"); 19767 } 19768 19769 TEST_F(FormatTest, FormatsWithWebKitStyle) { 19770 FormatStyle Style = getWebKitStyle(); 19771 19772 // Don't indent in outer namespaces. 19773 verifyFormat("namespace outer {\n" 19774 "int i;\n" 19775 "namespace inner {\n" 19776 " int i;\n" 19777 "} // namespace inner\n" 19778 "} // namespace outer\n" 19779 "namespace other_outer {\n" 19780 "int i;\n" 19781 "}", 19782 Style); 19783 19784 // Don't indent case labels. 19785 verifyFormat("switch (variable) {\n" 19786 "case 1:\n" 19787 "case 2:\n" 19788 " doSomething();\n" 19789 " break;\n" 19790 "default:\n" 19791 " ++variable;\n" 19792 "}", 19793 Style); 19794 19795 // Wrap before binary operators. 19796 EXPECT_EQ("void f()\n" 19797 "{\n" 19798 " if (aaaaaaaaaaaaaaaa\n" 19799 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 19800 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 19801 " return;\n" 19802 "}", 19803 format("void f() {\n" 19804 "if (aaaaaaaaaaaaaaaa\n" 19805 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 19806 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 19807 "return;\n" 19808 "}", 19809 Style)); 19810 19811 // Allow functions on a single line. 19812 verifyFormat("void f() { return; }", Style); 19813 19814 // Allow empty blocks on a single line and insert a space in empty blocks. 19815 EXPECT_EQ("void f() { }", format("void f() {}", Style)); 19816 EXPECT_EQ("while (true) { }", format("while (true) {}", Style)); 19817 // However, don't merge non-empty short loops. 19818 EXPECT_EQ("while (true) {\n" 19819 " continue;\n" 19820 "}", 19821 format("while (true) { continue; }", Style)); 19822 19823 // Constructor initializers are formatted one per line with the "," on the 19824 // new line. 19825 verifyFormat("Constructor()\n" 19826 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 19827 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 19828 " aaaaaaaaaaaaaa)\n" 19829 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 19830 "{\n" 19831 "}", 19832 Style); 19833 verifyFormat("SomeClass::Constructor()\n" 19834 " : a(a)\n" 19835 "{\n" 19836 "}", 19837 Style); 19838 EXPECT_EQ("SomeClass::Constructor()\n" 19839 " : a(a)\n" 19840 "{\n" 19841 "}", 19842 format("SomeClass::Constructor():a(a){}", Style)); 19843 verifyFormat("SomeClass::Constructor()\n" 19844 " : a(a)\n" 19845 " , b(b)\n" 19846 " , c(c)\n" 19847 "{\n" 19848 "}", 19849 Style); 19850 verifyFormat("SomeClass::Constructor()\n" 19851 " : a(a)\n" 19852 "{\n" 19853 " foo();\n" 19854 " bar();\n" 19855 "}", 19856 Style); 19857 19858 // Access specifiers should be aligned left. 19859 verifyFormat("class C {\n" 19860 "public:\n" 19861 " int i;\n" 19862 "};", 19863 Style); 19864 19865 // Do not align comments. 19866 verifyFormat("int a; // Do not\n" 19867 "double b; // align comments.", 19868 Style); 19869 19870 // Do not align operands. 19871 EXPECT_EQ("ASSERT(aaaa\n" 19872 " || bbbb);", 19873 format("ASSERT ( aaaa\n||bbbb);", Style)); 19874 19875 // Accept input's line breaks. 19876 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 19877 " || bbbbbbbbbbbbbbb) {\n" 19878 " i++;\n" 19879 "}", 19880 format("if (aaaaaaaaaaaaaaa\n" 19881 "|| bbbbbbbbbbbbbbb) { i++; }", 19882 Style)); 19883 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 19884 " i++;\n" 19885 "}", 19886 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 19887 19888 // Don't automatically break all macro definitions (llvm.org/PR17842). 19889 verifyFormat("#define aNumber 10", Style); 19890 // However, generally keep the line breaks that the user authored. 19891 EXPECT_EQ("#define aNumber \\\n" 19892 " 10", 19893 format("#define aNumber \\\n" 19894 " 10", 19895 Style)); 19896 19897 // Keep empty and one-element array literals on a single line. 19898 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 19899 " copyItems:YES];", 19900 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 19901 "copyItems:YES];", 19902 Style)); 19903 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 19904 " copyItems:YES];", 19905 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 19906 " copyItems:YES];", 19907 Style)); 19908 // FIXME: This does not seem right, there should be more indentation before 19909 // the array literal's entries. Nested blocks have the same problem. 19910 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 19911 " @\"a\",\n" 19912 " @\"a\"\n" 19913 "]\n" 19914 " copyItems:YES];", 19915 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 19916 " @\"a\",\n" 19917 " @\"a\"\n" 19918 " ]\n" 19919 " copyItems:YES];", 19920 Style)); 19921 EXPECT_EQ( 19922 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 19923 " copyItems:YES];", 19924 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 19925 " copyItems:YES];", 19926 Style)); 19927 19928 verifyFormat("[self.a b:c c:d];", Style); 19929 EXPECT_EQ("[self.a b:c\n" 19930 " c:d];", 19931 format("[self.a b:c\n" 19932 "c:d];", 19933 Style)); 19934 } 19935 19936 TEST_F(FormatTest, FormatsLambdas) { 19937 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 19938 verifyFormat( 19939 "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();\n"); 19940 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 19941 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 19942 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 19943 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 19944 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 19945 verifyFormat("auto c = [a = [b = 42] {}] {};\n"); 19946 verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n"); 19947 verifyFormat("int x = f(*+[] {});"); 19948 verifyFormat("void f() {\n" 19949 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 19950 "}\n"); 19951 verifyFormat("void f() {\n" 19952 " other(x.begin(), //\n" 19953 " x.end(), //\n" 19954 " [&](int, int) { return 1; });\n" 19955 "}\n"); 19956 verifyFormat("void f() {\n" 19957 " other.other.other.other.other(\n" 19958 " x.begin(), x.end(),\n" 19959 " [something, rather](int, int, int, int, int, int, int) { " 19960 "return 1; });\n" 19961 "}\n"); 19962 verifyFormat( 19963 "void f() {\n" 19964 " other.other.other.other.other(\n" 19965 " x.begin(), x.end(),\n" 19966 " [something, rather](int, int, int, int, int, int, int) {\n" 19967 " //\n" 19968 " });\n" 19969 "}\n"); 19970 verifyFormat("SomeFunction([]() { // A cool function...\n" 19971 " return 43;\n" 19972 "});"); 19973 EXPECT_EQ("SomeFunction([]() {\n" 19974 "#define A a\n" 19975 " return 43;\n" 19976 "});", 19977 format("SomeFunction([](){\n" 19978 "#define A a\n" 19979 "return 43;\n" 19980 "});")); 19981 verifyFormat("void f() {\n" 19982 " SomeFunction([](decltype(x), A *a) {});\n" 19983 " SomeFunction([](typeof(x), A *a) {});\n" 19984 " SomeFunction([](_Atomic(x), A *a) {});\n" 19985 " SomeFunction([](__underlying_type(x), A *a) {});\n" 19986 "}"); 19987 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 19988 " [](const aaaaaaaaaa &a) { return a; });"); 19989 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 19990 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 19991 "});"); 19992 verifyFormat("Constructor()\n" 19993 " : Field([] { // comment\n" 19994 " int i;\n" 19995 " }) {}"); 19996 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 19997 " return some_parameter.size();\n" 19998 "};"); 19999 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 20000 " [](const string &s) { return s; };"); 20001 verifyFormat("int i = aaaaaa ? 1 //\n" 20002 " : [] {\n" 20003 " return 2; //\n" 20004 " }();"); 20005 verifyFormat("llvm::errs() << \"number of twos is \"\n" 20006 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 20007 " return x == 2; // force break\n" 20008 " });"); 20009 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 20010 " [=](int iiiiiiiiiiii) {\n" 20011 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n" 20012 " aaaaaaaaaaaaaaaaaaaaaaa;\n" 20013 " });", 20014 getLLVMStyleWithColumns(60)); 20015 20016 verifyFormat("SomeFunction({[&] {\n" 20017 " // comment\n" 20018 " },\n" 20019 " [&] {\n" 20020 " // comment\n" 20021 " }});"); 20022 verifyFormat("SomeFunction({[&] {\n" 20023 " // comment\n" 20024 "}});"); 20025 verifyFormat( 20026 "virtual aaaaaaaaaaaaaaaa(\n" 20027 " std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n" 20028 " aaaaa aaaaaaaaa);"); 20029 20030 // Lambdas with return types. 20031 verifyFormat("int c = []() -> int { return 2; }();\n"); 20032 verifyFormat("int c = []() -> int * { return 2; }();\n"); 20033 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 20034 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 20035 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 20036 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 20037 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 20038 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 20039 verifyFormat("[a, a]() -> a<1> {};"); 20040 verifyFormat("[]() -> foo<5 + 2> { return {}; };"); 20041 verifyFormat("[]() -> foo<5 - 2> { return {}; };"); 20042 verifyFormat("[]() -> foo<5 / 2> { return {}; };"); 20043 verifyFormat("[]() -> foo<5 * 2> { return {}; };"); 20044 verifyFormat("[]() -> foo<5 % 2> { return {}; };"); 20045 verifyFormat("[]() -> foo<5 << 2> { return {}; };"); 20046 verifyFormat("[]() -> foo<!5> { return {}; };"); 20047 verifyFormat("[]() -> foo<~5> { return {}; };"); 20048 verifyFormat("[]() -> foo<5 | 2> { return {}; };"); 20049 verifyFormat("[]() -> foo<5 || 2> { return {}; };"); 20050 verifyFormat("[]() -> foo<5 & 2> { return {}; };"); 20051 verifyFormat("[]() -> foo<5 && 2> { return {}; };"); 20052 verifyFormat("[]() -> foo<5 == 2> { return {}; };"); 20053 verifyFormat("[]() -> foo<5 != 2> { return {}; };"); 20054 verifyFormat("[]() -> foo<5 >= 2> { return {}; };"); 20055 verifyFormat("[]() -> foo<5 <= 2> { return {}; };"); 20056 verifyFormat("[]() -> foo<5 < 2> { return {}; };"); 20057 verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };"); 20058 verifyFormat("namespace bar {\n" 20059 "// broken:\n" 20060 "auto foo{[]() -> foo<5 + 2> { return {}; }};\n" 20061 "} // namespace bar"); 20062 verifyFormat("namespace bar {\n" 20063 "// broken:\n" 20064 "auto foo{[]() -> foo<5 - 2> { return {}; }};\n" 20065 "} // namespace bar"); 20066 verifyFormat("namespace bar {\n" 20067 "// broken:\n" 20068 "auto foo{[]() -> foo<5 / 2> { return {}; }};\n" 20069 "} // namespace bar"); 20070 verifyFormat("namespace bar {\n" 20071 "// broken:\n" 20072 "auto foo{[]() -> foo<5 * 2> { return {}; }};\n" 20073 "} // namespace bar"); 20074 verifyFormat("namespace bar {\n" 20075 "// broken:\n" 20076 "auto foo{[]() -> foo<5 % 2> { return {}; }};\n" 20077 "} // namespace bar"); 20078 verifyFormat("namespace bar {\n" 20079 "// broken:\n" 20080 "auto foo{[]() -> foo<5 << 2> { return {}; }};\n" 20081 "} // namespace bar"); 20082 verifyFormat("namespace bar {\n" 20083 "// broken:\n" 20084 "auto foo{[]() -> foo<!5> { return {}; }};\n" 20085 "} // namespace bar"); 20086 verifyFormat("namespace bar {\n" 20087 "// broken:\n" 20088 "auto foo{[]() -> foo<~5> { return {}; }};\n" 20089 "} // namespace bar"); 20090 verifyFormat("namespace bar {\n" 20091 "// broken:\n" 20092 "auto foo{[]() -> foo<5 | 2> { return {}; }};\n" 20093 "} // namespace bar"); 20094 verifyFormat("namespace bar {\n" 20095 "// broken:\n" 20096 "auto foo{[]() -> foo<5 || 2> { return {}; }};\n" 20097 "} // namespace bar"); 20098 verifyFormat("namespace bar {\n" 20099 "// broken:\n" 20100 "auto foo{[]() -> foo<5 & 2> { return {}; }};\n" 20101 "} // namespace bar"); 20102 verifyFormat("namespace bar {\n" 20103 "// broken:\n" 20104 "auto foo{[]() -> foo<5 && 2> { return {}; }};\n" 20105 "} // namespace bar"); 20106 verifyFormat("namespace bar {\n" 20107 "// broken:\n" 20108 "auto foo{[]() -> foo<5 == 2> { return {}; }};\n" 20109 "} // namespace bar"); 20110 verifyFormat("namespace bar {\n" 20111 "// broken:\n" 20112 "auto foo{[]() -> foo<5 != 2> { return {}; }};\n" 20113 "} // namespace bar"); 20114 verifyFormat("namespace bar {\n" 20115 "// broken:\n" 20116 "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n" 20117 "} // namespace bar"); 20118 verifyFormat("namespace bar {\n" 20119 "// broken:\n" 20120 "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n" 20121 "} // namespace bar"); 20122 verifyFormat("namespace bar {\n" 20123 "// broken:\n" 20124 "auto foo{[]() -> foo<5 < 2> { return {}; }};\n" 20125 "} // namespace bar"); 20126 verifyFormat("namespace bar {\n" 20127 "// broken:\n" 20128 "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n" 20129 "} // namespace bar"); 20130 verifyFormat("[]() -> a<1> {};"); 20131 verifyFormat("[]() -> a<1> { ; };"); 20132 verifyFormat("[]() -> a<1> { ; }();"); 20133 verifyFormat("[a, a]() -> a<true> {};"); 20134 verifyFormat("[]() -> a<true> {};"); 20135 verifyFormat("[]() -> a<true> { ; };"); 20136 verifyFormat("[]() -> a<true> { ; }();"); 20137 verifyFormat("[a, a]() -> a<false> {};"); 20138 verifyFormat("[]() -> a<false> {};"); 20139 verifyFormat("[]() -> a<false> { ; };"); 20140 verifyFormat("[]() -> a<false> { ; }();"); 20141 verifyFormat("auto foo{[]() -> foo<false> { ; }};"); 20142 verifyFormat("namespace bar {\n" 20143 "auto foo{[]() -> foo<false> { ; }};\n" 20144 "} // namespace bar"); 20145 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 20146 " int j) -> int {\n" 20147 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 20148 "};"); 20149 verifyFormat( 20150 "aaaaaaaaaaaaaaaaaaaaaa(\n" 20151 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 20152 " return aaaaaaaaaaaaaaaaa;\n" 20153 " });", 20154 getLLVMStyleWithColumns(70)); 20155 verifyFormat("[]() //\n" 20156 " -> int {\n" 20157 " return 1; //\n" 20158 "};"); 20159 verifyFormat("[]() -> Void<T...> {};"); 20160 verifyFormat("[a, b]() -> Tuple<T...> { return {}; };"); 20161 20162 // Lambdas with explicit template argument lists. 20163 verifyFormat( 20164 "auto L = []<template <typename> class T, class U>(T<U> &&a) {};\n"); 20165 20166 // Multiple lambdas in the same parentheses change indentation rules. These 20167 // lambdas are forced to start on new lines. 20168 verifyFormat("SomeFunction(\n" 20169 " []() {\n" 20170 " //\n" 20171 " },\n" 20172 " []() {\n" 20173 " //\n" 20174 " });"); 20175 20176 // A lambda passed as arg0 is always pushed to the next line. 20177 verifyFormat("SomeFunction(\n" 20178 " [this] {\n" 20179 " //\n" 20180 " },\n" 20181 " 1);\n"); 20182 20183 // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like 20184 // the arg0 case above. 20185 auto Style = getGoogleStyle(); 20186 Style.BinPackArguments = false; 20187 verifyFormat("SomeFunction(\n" 20188 " a,\n" 20189 " [this] {\n" 20190 " //\n" 20191 " },\n" 20192 " b);\n", 20193 Style); 20194 verifyFormat("SomeFunction(\n" 20195 " a,\n" 20196 " [this] {\n" 20197 " //\n" 20198 " },\n" 20199 " b);\n"); 20200 20201 // A lambda with a very long line forces arg0 to be pushed out irrespective of 20202 // the BinPackArguments value (as long as the code is wide enough). 20203 verifyFormat( 20204 "something->SomeFunction(\n" 20205 " a,\n" 20206 " [this] {\n" 20207 " " 20208 "D0000000000000000000000000000000000000000000000000000000000001();\n" 20209 " },\n" 20210 " b);\n"); 20211 20212 // A multi-line lambda is pulled up as long as the introducer fits on the 20213 // previous line and there are no further args. 20214 verifyFormat("function(1, [this, that] {\n" 20215 " //\n" 20216 "});\n"); 20217 verifyFormat("function([this, that] {\n" 20218 " //\n" 20219 "});\n"); 20220 // FIXME: this format is not ideal and we should consider forcing the first 20221 // arg onto its own line. 20222 verifyFormat("function(a, b, c, //\n" 20223 " d, [this, that] {\n" 20224 " //\n" 20225 " });\n"); 20226 20227 // Multiple lambdas are treated correctly even when there is a short arg0. 20228 verifyFormat("SomeFunction(\n" 20229 " 1,\n" 20230 " [this] {\n" 20231 " //\n" 20232 " },\n" 20233 " [this] {\n" 20234 " //\n" 20235 " },\n" 20236 " 1);\n"); 20237 20238 // More complex introducers. 20239 verifyFormat("return [i, args...] {};"); 20240 20241 // Not lambdas. 20242 verifyFormat("constexpr char hello[]{\"hello\"};"); 20243 verifyFormat("double &operator[](int i) { return 0; }\n" 20244 "int i;"); 20245 verifyFormat("std::unique_ptr<int[]> foo() {}"); 20246 verifyFormat("int i = a[a][a]->f();"); 20247 verifyFormat("int i = (*b)[a]->f();"); 20248 20249 // Other corner cases. 20250 verifyFormat("void f() {\n" 20251 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 20252 " );\n" 20253 "}"); 20254 20255 // Lambdas created through weird macros. 20256 verifyFormat("void f() {\n" 20257 " MACRO((const AA &a) { return 1; });\n" 20258 " MACRO((AA &a) { return 1; });\n" 20259 "}"); 20260 20261 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 20262 " doo_dah();\n" 20263 " doo_dah();\n" 20264 " })) {\n" 20265 "}"); 20266 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n" 20267 " doo_dah();\n" 20268 " doo_dah();\n" 20269 " })) {\n" 20270 "}"); 20271 verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n" 20272 " doo_dah();\n" 20273 " doo_dah();\n" 20274 " })) {\n" 20275 "}"); 20276 verifyFormat("auto lambda = []() {\n" 20277 " int a = 2\n" 20278 "#if A\n" 20279 " + 2\n" 20280 "#endif\n" 20281 " ;\n" 20282 "};"); 20283 20284 // Lambdas with complex multiline introducers. 20285 verifyFormat( 20286 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 20287 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n" 20288 " -> ::std::unordered_set<\n" 20289 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 20290 " //\n" 20291 " });"); 20292 20293 FormatStyle DoNotMerge = getLLVMStyle(); 20294 DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 20295 verifyFormat("auto c = []() {\n" 20296 " return b;\n" 20297 "};", 20298 "auto c = []() { return b; };", DoNotMerge); 20299 verifyFormat("auto c = []() {\n" 20300 "};", 20301 " auto c = []() {};", DoNotMerge); 20302 20303 FormatStyle MergeEmptyOnly = getLLVMStyle(); 20304 MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty; 20305 verifyFormat("auto c = []() {\n" 20306 " return b;\n" 20307 "};", 20308 "auto c = []() {\n" 20309 " return b;\n" 20310 " };", 20311 MergeEmptyOnly); 20312 verifyFormat("auto c = []() {};", 20313 "auto c = []() {\n" 20314 "};", 20315 MergeEmptyOnly); 20316 20317 FormatStyle MergeInline = getLLVMStyle(); 20318 MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline; 20319 verifyFormat("auto c = []() {\n" 20320 " return b;\n" 20321 "};", 20322 "auto c = []() { return b; };", MergeInline); 20323 verifyFormat("function([]() { return b; })", "function([]() { return b; })", 20324 MergeInline); 20325 verifyFormat("function([]() { return b; }, a)", 20326 "function([]() { return b; }, a)", MergeInline); 20327 verifyFormat("function(a, []() { return b; })", 20328 "function(a, []() { return b; })", MergeInline); 20329 20330 // Check option "BraceWrapping.BeforeLambdaBody" and different state of 20331 // AllowShortLambdasOnASingleLine 20332 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle(); 20333 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 20334 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 20335 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20336 FormatStyle::ShortLambdaStyle::SLS_None; 20337 verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n" 20338 " []()\n" 20339 " {\n" 20340 " return 17;\n" 20341 " });", 20342 LLVMWithBeforeLambdaBody); 20343 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n" 20344 " []()\n" 20345 " {\n" 20346 " });", 20347 LLVMWithBeforeLambdaBody); 20348 verifyFormat("auto fct_SLS_None = []()\n" 20349 "{\n" 20350 " return 17;\n" 20351 "};", 20352 LLVMWithBeforeLambdaBody); 20353 verifyFormat("TwoNestedLambdas_SLS_None(\n" 20354 " []()\n" 20355 " {\n" 20356 " return Call(\n" 20357 " []()\n" 20358 " {\n" 20359 " return 17;\n" 20360 " });\n" 20361 " });", 20362 LLVMWithBeforeLambdaBody); 20363 verifyFormat("void Fct() {\n" 20364 " return {[]()\n" 20365 " {\n" 20366 " return 17;\n" 20367 " }};\n" 20368 "}", 20369 LLVMWithBeforeLambdaBody); 20370 20371 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20372 FormatStyle::ShortLambdaStyle::SLS_Empty; 20373 verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n" 20374 " []()\n" 20375 " {\n" 20376 " return 17;\n" 20377 " });", 20378 LLVMWithBeforeLambdaBody); 20379 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});", 20380 LLVMWithBeforeLambdaBody); 20381 verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL" 20382 "ongFunctionName_SLS_Empty(\n" 20383 " []() {});", 20384 LLVMWithBeforeLambdaBody); 20385 verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n" 20386 " []()\n" 20387 " {\n" 20388 " return 17;\n" 20389 " });", 20390 LLVMWithBeforeLambdaBody); 20391 verifyFormat("auto fct_SLS_Empty = []()\n" 20392 "{\n" 20393 " return 17;\n" 20394 "};", 20395 LLVMWithBeforeLambdaBody); 20396 verifyFormat("TwoNestedLambdas_SLS_Empty(\n" 20397 " []()\n" 20398 " {\n" 20399 " return Call([]() {});\n" 20400 " });", 20401 LLVMWithBeforeLambdaBody); 20402 verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n" 20403 " []()\n" 20404 " {\n" 20405 " return Call([]() {});\n" 20406 " });", 20407 LLVMWithBeforeLambdaBody); 20408 verifyFormat( 20409 "FctWithLongLineInLambda_SLS_Empty(\n" 20410 " []()\n" 20411 " {\n" 20412 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 20413 " AndShouldNotBeConsiderAsInline,\n" 20414 " LambdaBodyMustBeBreak);\n" 20415 " });", 20416 LLVMWithBeforeLambdaBody); 20417 20418 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20419 FormatStyle::ShortLambdaStyle::SLS_Inline; 20420 verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });", 20421 LLVMWithBeforeLambdaBody); 20422 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});", 20423 LLVMWithBeforeLambdaBody); 20424 verifyFormat("auto fct_SLS_Inline = []()\n" 20425 "{\n" 20426 " return 17;\n" 20427 "};", 20428 LLVMWithBeforeLambdaBody); 20429 verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return " 20430 "17; }); });", 20431 LLVMWithBeforeLambdaBody); 20432 verifyFormat( 20433 "FctWithLongLineInLambda_SLS_Inline(\n" 20434 " []()\n" 20435 " {\n" 20436 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 20437 " AndShouldNotBeConsiderAsInline,\n" 20438 " LambdaBodyMustBeBreak);\n" 20439 " });", 20440 LLVMWithBeforeLambdaBody); 20441 verifyFormat("FctWithMultipleParams_SLS_Inline(" 20442 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n" 20443 " []() { return 17; });", 20444 LLVMWithBeforeLambdaBody); 20445 verifyFormat( 20446 "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });", 20447 LLVMWithBeforeLambdaBody); 20448 20449 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20450 FormatStyle::ShortLambdaStyle::SLS_All; 20451 verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });", 20452 LLVMWithBeforeLambdaBody); 20453 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});", 20454 LLVMWithBeforeLambdaBody); 20455 verifyFormat("auto fct_SLS_All = []() { return 17; };", 20456 LLVMWithBeforeLambdaBody); 20457 verifyFormat("FctWithOneParam_SLS_All(\n" 20458 " []()\n" 20459 " {\n" 20460 " // A cool function...\n" 20461 " return 43;\n" 20462 " });", 20463 LLVMWithBeforeLambdaBody); 20464 verifyFormat("FctWithMultipleParams_SLS_All(" 20465 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n" 20466 " []() { return 17; });", 20467 LLVMWithBeforeLambdaBody); 20468 verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });", 20469 LLVMWithBeforeLambdaBody); 20470 verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });", 20471 LLVMWithBeforeLambdaBody); 20472 verifyFormat( 20473 "FctWithLongLineInLambda_SLS_All(\n" 20474 " []()\n" 20475 " {\n" 20476 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 20477 " AndShouldNotBeConsiderAsInline,\n" 20478 " LambdaBodyMustBeBreak);\n" 20479 " });", 20480 LLVMWithBeforeLambdaBody); 20481 verifyFormat( 20482 "auto fct_SLS_All = []()\n" 20483 "{\n" 20484 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 20485 " AndShouldNotBeConsiderAsInline,\n" 20486 " LambdaBodyMustBeBreak);\n" 20487 "};", 20488 LLVMWithBeforeLambdaBody); 20489 LLVMWithBeforeLambdaBody.BinPackParameters = false; 20490 verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);", 20491 LLVMWithBeforeLambdaBody); 20492 verifyFormat( 20493 "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n" 20494 " FirstParam,\n" 20495 " SecondParam,\n" 20496 " ThirdParam,\n" 20497 " FourthParam);", 20498 LLVMWithBeforeLambdaBody); 20499 verifyFormat("FctWithLongLineInLambda_SLS_All(\n" 20500 " []() { return " 20501 "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n" 20502 " FirstParam,\n" 20503 " SecondParam,\n" 20504 " ThirdParam,\n" 20505 " FourthParam);", 20506 LLVMWithBeforeLambdaBody); 20507 verifyFormat( 20508 "FctWithLongLineInLambda_SLS_All(FirstParam,\n" 20509 " SecondParam,\n" 20510 " ThirdParam,\n" 20511 " FourthParam,\n" 20512 " []() { return SomeValueNotSoLong; });", 20513 LLVMWithBeforeLambdaBody); 20514 verifyFormat("FctWithLongLineInLambda_SLS_All(\n" 20515 " []()\n" 20516 " {\n" 20517 " return " 20518 "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB" 20519 "eConsiderAsInline;\n" 20520 " });", 20521 LLVMWithBeforeLambdaBody); 20522 verifyFormat( 20523 "FctWithLongLineInLambda_SLS_All(\n" 20524 " []()\n" 20525 " {\n" 20526 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 20527 " AndShouldNotBeConsiderAsInline,\n" 20528 " LambdaBodyMustBeBreak);\n" 20529 " });", 20530 LLVMWithBeforeLambdaBody); 20531 verifyFormat("FctWithTwoParams_SLS_All(\n" 20532 " []()\n" 20533 " {\n" 20534 " // A cool function...\n" 20535 " return 43;\n" 20536 " },\n" 20537 " 87);", 20538 LLVMWithBeforeLambdaBody); 20539 verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);", 20540 LLVMWithBeforeLambdaBody); 20541 verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });", 20542 LLVMWithBeforeLambdaBody); 20543 verifyFormat( 20544 "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });", 20545 LLVMWithBeforeLambdaBody); 20546 verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; " 20547 "}); }, x);", 20548 LLVMWithBeforeLambdaBody); 20549 verifyFormat("TwoNestedLambdas_SLS_All(\n" 20550 " []()\n" 20551 " {\n" 20552 " // A cool function...\n" 20553 " return Call([]() { return 17; });\n" 20554 " });", 20555 LLVMWithBeforeLambdaBody); 20556 verifyFormat("TwoNestedLambdas_SLS_All(\n" 20557 " []()\n" 20558 " {\n" 20559 " return Call(\n" 20560 " []()\n" 20561 " {\n" 20562 " // A cool function...\n" 20563 " return 17;\n" 20564 " });\n" 20565 " });", 20566 LLVMWithBeforeLambdaBody); 20567 20568 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20569 FormatStyle::ShortLambdaStyle::SLS_None; 20570 20571 verifyFormat("auto select = [this]() -> const Library::Object *\n" 20572 "{\n" 20573 " return MyAssignment::SelectFromList(this);\n" 20574 "};\n", 20575 LLVMWithBeforeLambdaBody); 20576 20577 verifyFormat("auto select = [this]() -> const Library::Object &\n" 20578 "{\n" 20579 " return MyAssignment::SelectFromList(this);\n" 20580 "};\n", 20581 LLVMWithBeforeLambdaBody); 20582 20583 verifyFormat("auto select = [this]() -> std::unique_ptr<Object>\n" 20584 "{\n" 20585 " return MyAssignment::SelectFromList(this);\n" 20586 "};\n", 20587 LLVMWithBeforeLambdaBody); 20588 20589 verifyFormat("namespace test {\n" 20590 "class Test {\n" 20591 "public:\n" 20592 " Test() = default;\n" 20593 "};\n" 20594 "} // namespace test", 20595 LLVMWithBeforeLambdaBody); 20596 20597 // Lambdas with different indentation styles. 20598 Style = getLLVMStyleWithColumns(100); 20599 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 20600 " return promise.then(\n" 20601 " [this, &someVariable, someObject = " 20602 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 20603 " return someObject.startAsyncAction().then(\n" 20604 " [this, &someVariable](AsyncActionResult result) " 20605 "mutable { result.processMore(); });\n" 20606 " });\n" 20607 "}\n", 20608 format("SomeResult doSomething(SomeObject promise) {\n" 20609 " return promise.then([this, &someVariable, someObject = " 20610 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 20611 " return someObject.startAsyncAction().then([this, " 20612 "&someVariable](AsyncActionResult result) mutable {\n" 20613 " result.processMore();\n" 20614 " });\n" 20615 " });\n" 20616 "}\n", 20617 Style)); 20618 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope; 20619 verifyFormat("test() {\n" 20620 " ([]() -> {\n" 20621 " int b = 32;\n" 20622 " return 3;\n" 20623 " }).foo();\n" 20624 "}", 20625 Style); 20626 verifyFormat("test() {\n" 20627 " []() -> {\n" 20628 " int b = 32;\n" 20629 " return 3;\n" 20630 " }\n" 20631 "}", 20632 Style); 20633 verifyFormat("std::sort(v.begin(), v.end(),\n" 20634 " [](const auto &someLongArgumentName, const auto " 20635 "&someOtherLongArgumentName) {\n" 20636 " return someLongArgumentName.someMemberVariable < " 20637 "someOtherLongArgumentName.someMemberVariable;\n" 20638 "});", 20639 Style); 20640 verifyFormat("test() {\n" 20641 " (\n" 20642 " []() -> {\n" 20643 " int b = 32;\n" 20644 " return 3;\n" 20645 " },\n" 20646 " foo, bar)\n" 20647 " .foo();\n" 20648 "}", 20649 Style); 20650 verifyFormat("test() {\n" 20651 " ([]() -> {\n" 20652 " int b = 32;\n" 20653 " return 3;\n" 20654 " })\n" 20655 " .foo()\n" 20656 " .bar();\n" 20657 "}", 20658 Style); 20659 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 20660 " return promise.then(\n" 20661 " [this, &someVariable, someObject = " 20662 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 20663 " return someObject.startAsyncAction().then(\n" 20664 " [this, &someVariable](AsyncActionResult result) mutable { " 20665 "result.processMore(); });\n" 20666 " });\n" 20667 "}\n", 20668 format("SomeResult doSomething(SomeObject promise) {\n" 20669 " return promise.then([this, &someVariable, someObject = " 20670 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 20671 " return someObject.startAsyncAction().then([this, " 20672 "&someVariable](AsyncActionResult result) mutable {\n" 20673 " result.processMore();\n" 20674 " });\n" 20675 " });\n" 20676 "}\n", 20677 Style)); 20678 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 20679 " return promise.then([this, &someVariable] {\n" 20680 " return someObject.startAsyncAction().then(\n" 20681 " [this, &someVariable](AsyncActionResult result) mutable { " 20682 "result.processMore(); });\n" 20683 " });\n" 20684 "}\n", 20685 format("SomeResult doSomething(SomeObject promise) {\n" 20686 " return promise.then([this, &someVariable] {\n" 20687 " return someObject.startAsyncAction().then([this, " 20688 "&someVariable](AsyncActionResult result) mutable {\n" 20689 " result.processMore();\n" 20690 " });\n" 20691 " });\n" 20692 "}\n", 20693 Style)); 20694 Style = getGoogleStyle(); 20695 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope; 20696 EXPECT_EQ("#define A \\\n" 20697 " [] { \\\n" 20698 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 20699 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 20700 " }", 20701 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 20702 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 20703 Style)); 20704 // TODO: The current formatting has a minor issue that's not worth fixing 20705 // right now whereby the closing brace is indented relative to the signature 20706 // instead of being aligned. This only happens with macros. 20707 } 20708 20709 TEST_F(FormatTest, LambdaWithLineComments) { 20710 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle(); 20711 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 20712 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 20713 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20714 FormatStyle::ShortLambdaStyle::SLS_All; 20715 20716 verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody); 20717 verifyFormat("auto k = []() // comment\n" 20718 "{ return; }", 20719 LLVMWithBeforeLambdaBody); 20720 verifyFormat("auto k = []() /* comment */ { return; }", 20721 LLVMWithBeforeLambdaBody); 20722 verifyFormat("auto k = []() /* comment */ /* comment */ { return; }", 20723 LLVMWithBeforeLambdaBody); 20724 verifyFormat("auto k = []() // X\n" 20725 "{ return; }", 20726 LLVMWithBeforeLambdaBody); 20727 verifyFormat( 20728 "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n" 20729 "{ return; }", 20730 LLVMWithBeforeLambdaBody); 20731 } 20732 20733 TEST_F(FormatTest, EmptyLinesInLambdas) { 20734 verifyFormat("auto lambda = []() {\n" 20735 " x(); //\n" 20736 "};", 20737 "auto lambda = []() {\n" 20738 "\n" 20739 " x(); //\n" 20740 "\n" 20741 "};"); 20742 } 20743 20744 TEST_F(FormatTest, FormatsBlocks) { 20745 FormatStyle ShortBlocks = getLLVMStyle(); 20746 ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 20747 verifyFormat("int (^Block)(int, int);", ShortBlocks); 20748 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 20749 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 20750 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 20751 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 20752 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 20753 20754 verifyFormat("foo(^{ bar(); });", ShortBlocks); 20755 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 20756 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 20757 20758 verifyFormat("[operation setCompletionBlock:^{\n" 20759 " [self onOperationDone];\n" 20760 "}];"); 20761 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 20762 " [self onOperationDone];\n" 20763 "}]};"); 20764 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 20765 " f();\n" 20766 "}];"); 20767 verifyFormat("int a = [operation block:^int(int *i) {\n" 20768 " return 1;\n" 20769 "}];"); 20770 verifyFormat("[myObject doSomethingWith:arg1\n" 20771 " aaa:^int(int *a) {\n" 20772 " return 1;\n" 20773 " }\n" 20774 " bbb:f(a * bbbbbbbb)];"); 20775 20776 verifyFormat("[operation setCompletionBlock:^{\n" 20777 " [self.delegate newDataAvailable];\n" 20778 "}];", 20779 getLLVMStyleWithColumns(60)); 20780 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 20781 " NSString *path = [self sessionFilePath];\n" 20782 " if (path) {\n" 20783 " // ...\n" 20784 " }\n" 20785 "});"); 20786 verifyFormat("[[SessionService sharedService]\n" 20787 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 20788 " if (window) {\n" 20789 " [self windowDidLoad:window];\n" 20790 " } else {\n" 20791 " [self errorLoadingWindow];\n" 20792 " }\n" 20793 " }];"); 20794 verifyFormat("void (^largeBlock)(void) = ^{\n" 20795 " // ...\n" 20796 "};\n", 20797 getLLVMStyleWithColumns(40)); 20798 verifyFormat("[[SessionService sharedService]\n" 20799 " loadWindowWithCompletionBlock: //\n" 20800 " ^(SessionWindow *window) {\n" 20801 " if (window) {\n" 20802 " [self windowDidLoad:window];\n" 20803 " } else {\n" 20804 " [self errorLoadingWindow];\n" 20805 " }\n" 20806 " }];", 20807 getLLVMStyleWithColumns(60)); 20808 verifyFormat("[myObject doSomethingWith:arg1\n" 20809 " firstBlock:^(Foo *a) {\n" 20810 " // ...\n" 20811 " int i;\n" 20812 " }\n" 20813 " secondBlock:^(Bar *b) {\n" 20814 " // ...\n" 20815 " int i;\n" 20816 " }\n" 20817 " thirdBlock:^Foo(Bar *b) {\n" 20818 " // ...\n" 20819 " int i;\n" 20820 " }];"); 20821 verifyFormat("[myObject doSomethingWith:arg1\n" 20822 " firstBlock:-1\n" 20823 " secondBlock:^(Bar *b) {\n" 20824 " // ...\n" 20825 " int i;\n" 20826 " }];"); 20827 20828 verifyFormat("f(^{\n" 20829 " @autoreleasepool {\n" 20830 " if (a) {\n" 20831 " g();\n" 20832 " }\n" 20833 " }\n" 20834 "});"); 20835 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 20836 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" 20837 "};"); 20838 20839 FormatStyle FourIndent = getLLVMStyle(); 20840 FourIndent.ObjCBlockIndentWidth = 4; 20841 verifyFormat("[operation setCompletionBlock:^{\n" 20842 " [self onOperationDone];\n" 20843 "}];", 20844 FourIndent); 20845 } 20846 20847 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 20848 FormatStyle ZeroColumn = getLLVMStyle(); 20849 ZeroColumn.ColumnLimit = 0; 20850 20851 verifyFormat("[[SessionService sharedService] " 20852 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 20853 " if (window) {\n" 20854 " [self windowDidLoad:window];\n" 20855 " } else {\n" 20856 " [self errorLoadingWindow];\n" 20857 " }\n" 20858 "}];", 20859 ZeroColumn); 20860 EXPECT_EQ("[[SessionService sharedService]\n" 20861 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 20862 " if (window) {\n" 20863 " [self windowDidLoad:window];\n" 20864 " } else {\n" 20865 " [self errorLoadingWindow];\n" 20866 " }\n" 20867 " }];", 20868 format("[[SessionService sharedService]\n" 20869 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 20870 " if (window) {\n" 20871 " [self windowDidLoad:window];\n" 20872 " } else {\n" 20873 " [self errorLoadingWindow];\n" 20874 " }\n" 20875 "}];", 20876 ZeroColumn)); 20877 verifyFormat("[myObject doSomethingWith:arg1\n" 20878 " firstBlock:^(Foo *a) {\n" 20879 " // ...\n" 20880 " int i;\n" 20881 " }\n" 20882 " secondBlock:^(Bar *b) {\n" 20883 " // ...\n" 20884 " int i;\n" 20885 " }\n" 20886 " thirdBlock:^Foo(Bar *b) {\n" 20887 " // ...\n" 20888 " int i;\n" 20889 " }];", 20890 ZeroColumn); 20891 verifyFormat("f(^{\n" 20892 " @autoreleasepool {\n" 20893 " if (a) {\n" 20894 " g();\n" 20895 " }\n" 20896 " }\n" 20897 "});", 20898 ZeroColumn); 20899 verifyFormat("void (^largeBlock)(void) = ^{\n" 20900 " // ...\n" 20901 "};", 20902 ZeroColumn); 20903 20904 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 20905 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 20906 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 20907 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 20908 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 20909 " int i;\n" 20910 "};", 20911 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 20912 } 20913 20914 TEST_F(FormatTest, SupportsCRLF) { 20915 EXPECT_EQ("int a;\r\n" 20916 "int b;\r\n" 20917 "int c;\r\n", 20918 format("int a;\r\n" 20919 " int b;\r\n" 20920 " int c;\r\n", 20921 getLLVMStyle())); 20922 EXPECT_EQ("int a;\r\n" 20923 "int b;\r\n" 20924 "int c;\r\n", 20925 format("int a;\r\n" 20926 " int b;\n" 20927 " int c;\r\n", 20928 getLLVMStyle())); 20929 EXPECT_EQ("int a;\n" 20930 "int b;\n" 20931 "int c;\n", 20932 format("int a;\r\n" 20933 " int b;\n" 20934 " int c;\n", 20935 getLLVMStyle())); 20936 EXPECT_EQ("\"aaaaaaa \"\r\n" 20937 "\"bbbbbbb\";\r\n", 20938 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 20939 EXPECT_EQ("#define A \\\r\n" 20940 " b; \\\r\n" 20941 " c; \\\r\n" 20942 " d;\r\n", 20943 format("#define A \\\r\n" 20944 " b; \\\r\n" 20945 " c; d; \r\n", 20946 getGoogleStyle())); 20947 20948 EXPECT_EQ("/*\r\n" 20949 "multi line block comments\r\n" 20950 "should not introduce\r\n" 20951 "an extra carriage return\r\n" 20952 "*/\r\n", 20953 format("/*\r\n" 20954 "multi line block comments\r\n" 20955 "should not introduce\r\n" 20956 "an extra carriage return\r\n" 20957 "*/\r\n")); 20958 EXPECT_EQ("/*\r\n" 20959 "\r\n" 20960 "*/", 20961 format("/*\r\n" 20962 " \r\r\r\n" 20963 "*/")); 20964 20965 FormatStyle style = getLLVMStyle(); 20966 20967 style.DeriveLineEnding = true; 20968 style.UseCRLF = false; 20969 EXPECT_EQ("union FooBarBazQux {\n" 20970 " int foo;\n" 20971 " int bar;\n" 20972 " int baz;\n" 20973 "};", 20974 format("union FooBarBazQux {\r\n" 20975 " int foo;\n" 20976 " int bar;\r\n" 20977 " int baz;\n" 20978 "};", 20979 style)); 20980 style.UseCRLF = true; 20981 EXPECT_EQ("union FooBarBazQux {\r\n" 20982 " int foo;\r\n" 20983 " int bar;\r\n" 20984 " int baz;\r\n" 20985 "};", 20986 format("union FooBarBazQux {\r\n" 20987 " int foo;\n" 20988 " int bar;\r\n" 20989 " int baz;\n" 20990 "};", 20991 style)); 20992 20993 style.DeriveLineEnding = false; 20994 style.UseCRLF = false; 20995 EXPECT_EQ("union FooBarBazQux {\n" 20996 " int foo;\n" 20997 " int bar;\n" 20998 " int baz;\n" 20999 " int qux;\n" 21000 "};", 21001 format("union FooBarBazQux {\r\n" 21002 " int foo;\n" 21003 " int bar;\r\n" 21004 " int baz;\n" 21005 " int qux;\r\n" 21006 "};", 21007 style)); 21008 style.UseCRLF = true; 21009 EXPECT_EQ("union FooBarBazQux {\r\n" 21010 " int foo;\r\n" 21011 " int bar;\r\n" 21012 " int baz;\r\n" 21013 " int qux;\r\n" 21014 "};", 21015 format("union FooBarBazQux {\r\n" 21016 " int foo;\n" 21017 " int bar;\r\n" 21018 " int baz;\n" 21019 " int qux;\n" 21020 "};", 21021 style)); 21022 21023 style.DeriveLineEnding = true; 21024 style.UseCRLF = false; 21025 EXPECT_EQ("union FooBarBazQux {\r\n" 21026 " int foo;\r\n" 21027 " int bar;\r\n" 21028 " int baz;\r\n" 21029 " int qux;\r\n" 21030 "};", 21031 format("union FooBarBazQux {\r\n" 21032 " int foo;\n" 21033 " int bar;\r\n" 21034 " int baz;\n" 21035 " int qux;\r\n" 21036 "};", 21037 style)); 21038 style.UseCRLF = true; 21039 EXPECT_EQ("union FooBarBazQux {\n" 21040 " int foo;\n" 21041 " int bar;\n" 21042 " int baz;\n" 21043 " int qux;\n" 21044 "};", 21045 format("union FooBarBazQux {\r\n" 21046 " int foo;\n" 21047 " int bar;\r\n" 21048 " int baz;\n" 21049 " int qux;\n" 21050 "};", 21051 style)); 21052 } 21053 21054 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 21055 verifyFormat("MY_CLASS(C) {\n" 21056 " int i;\n" 21057 " int j;\n" 21058 "};"); 21059 } 21060 21061 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 21062 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 21063 TwoIndent.ContinuationIndentWidth = 2; 21064 21065 EXPECT_EQ("int i =\n" 21066 " longFunction(\n" 21067 " arg);", 21068 format("int i = longFunction(arg);", TwoIndent)); 21069 21070 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 21071 SixIndent.ContinuationIndentWidth = 6; 21072 21073 EXPECT_EQ("int i =\n" 21074 " longFunction(\n" 21075 " arg);", 21076 format("int i = longFunction(arg);", SixIndent)); 21077 } 21078 21079 TEST_F(FormatTest, WrappedClosingParenthesisIndent) { 21080 FormatStyle Style = getLLVMStyle(); 21081 verifyFormat("int Foo::getter(\n" 21082 " //\n" 21083 ") const {\n" 21084 " return foo;\n" 21085 "}", 21086 Style); 21087 verifyFormat("void Foo::setter(\n" 21088 " //\n" 21089 ") {\n" 21090 " foo = 1;\n" 21091 "}", 21092 Style); 21093 } 21094 21095 TEST_F(FormatTest, SpacesInAngles) { 21096 FormatStyle Spaces = getLLVMStyle(); 21097 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 21098 21099 verifyFormat("vector< ::std::string > x1;", Spaces); 21100 verifyFormat("Foo< int, Bar > x2;", Spaces); 21101 verifyFormat("Foo< ::int, ::Bar > x3;", Spaces); 21102 21103 verifyFormat("static_cast< int >(arg);", Spaces); 21104 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 21105 verifyFormat("f< int, float >();", Spaces); 21106 verifyFormat("template <> g() {}", Spaces); 21107 verifyFormat("template < std::vector< int > > f() {}", Spaces); 21108 verifyFormat("std::function< void(int, int) > fct;", Spaces); 21109 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 21110 Spaces); 21111 21112 Spaces.Standard = FormatStyle::LS_Cpp03; 21113 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 21114 verifyFormat("A< A< int > >();", Spaces); 21115 21116 Spaces.SpacesInAngles = FormatStyle::SIAS_Never; 21117 verifyFormat("A<A<int> >();", Spaces); 21118 21119 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave; 21120 verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;", 21121 Spaces); 21122 verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;", 21123 Spaces); 21124 21125 verifyFormat("A<A<int> >();", Spaces); 21126 verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces); 21127 verifyFormat("A< A< int > >();", Spaces); 21128 21129 Spaces.Standard = FormatStyle::LS_Cpp11; 21130 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 21131 verifyFormat("A< A< int > >();", Spaces); 21132 21133 Spaces.SpacesInAngles = FormatStyle::SIAS_Never; 21134 verifyFormat("vector<::std::string> x4;", Spaces); 21135 verifyFormat("vector<int> x5;", Spaces); 21136 verifyFormat("Foo<int, Bar> x6;", Spaces); 21137 verifyFormat("Foo<::int, ::Bar> x7;", Spaces); 21138 21139 verifyFormat("A<A<int>>();", Spaces); 21140 21141 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave; 21142 verifyFormat("vector<::std::string> x4;", Spaces); 21143 verifyFormat("vector< ::std::string > x4;", Spaces); 21144 verifyFormat("vector<int> x5;", Spaces); 21145 verifyFormat("vector< int > x5;", Spaces); 21146 verifyFormat("Foo<int, Bar> x6;", Spaces); 21147 verifyFormat("Foo< int, Bar > x6;", Spaces); 21148 verifyFormat("Foo<::int, ::Bar> x7;", Spaces); 21149 verifyFormat("Foo< ::int, ::Bar > x7;", Spaces); 21150 21151 verifyFormat("A<A<int>>();", Spaces); 21152 verifyFormat("A< A< int > >();", Spaces); 21153 verifyFormat("A<A<int > >();", Spaces); 21154 verifyFormat("A< A< int>>();", Spaces); 21155 } 21156 21157 TEST_F(FormatTest, SpaceAfterTemplateKeyword) { 21158 FormatStyle Style = getLLVMStyle(); 21159 Style.SpaceAfterTemplateKeyword = false; 21160 verifyFormat("template<int> void foo();", Style); 21161 } 21162 21163 TEST_F(FormatTest, TripleAngleBrackets) { 21164 verifyFormat("f<<<1, 1>>>();"); 21165 verifyFormat("f<<<1, 1, 1, s>>>();"); 21166 verifyFormat("f<<<a, b, c, d>>>();"); 21167 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 21168 verifyFormat("f<param><<<1, 1>>>();"); 21169 verifyFormat("f<1><<<1, 1>>>();"); 21170 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 21171 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 21172 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 21173 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n" 21174 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();"); 21175 } 21176 21177 TEST_F(FormatTest, MergeLessLessAtEnd) { 21178 verifyFormat("<<"); 21179 EXPECT_EQ("< < <", format("\\\n<<<")); 21180 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 21181 "aaallvm::outs() <<"); 21182 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 21183 "aaaallvm::outs()\n <<"); 21184 } 21185 21186 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 21187 std::string code = "#if A\n" 21188 "#if B\n" 21189 "a.\n" 21190 "#endif\n" 21191 " a = 1;\n" 21192 "#else\n" 21193 "#endif\n" 21194 "#if C\n" 21195 "#else\n" 21196 "#endif\n"; 21197 EXPECT_EQ(code, format(code)); 21198 } 21199 21200 TEST_F(FormatTest, HandleConflictMarkers) { 21201 // Git/SVN conflict markers. 21202 EXPECT_EQ("int a;\n" 21203 "void f() {\n" 21204 " callme(some(parameter1,\n" 21205 "<<<<<<< text by the vcs\n" 21206 " parameter2),\n" 21207 "||||||| text by the vcs\n" 21208 " parameter2),\n" 21209 " parameter3,\n" 21210 "======= text by the vcs\n" 21211 " parameter2, parameter3),\n" 21212 ">>>>>>> text by the vcs\n" 21213 " otherparameter);\n", 21214 format("int a;\n" 21215 "void f() {\n" 21216 " callme(some(parameter1,\n" 21217 "<<<<<<< text by the vcs\n" 21218 " parameter2),\n" 21219 "||||||| text by the vcs\n" 21220 " parameter2),\n" 21221 " parameter3,\n" 21222 "======= text by the vcs\n" 21223 " parameter2,\n" 21224 " parameter3),\n" 21225 ">>>>>>> text by the vcs\n" 21226 " otherparameter);\n")); 21227 21228 // Perforce markers. 21229 EXPECT_EQ("void f() {\n" 21230 " function(\n" 21231 ">>>> text by the vcs\n" 21232 " parameter,\n" 21233 "==== text by the vcs\n" 21234 " parameter,\n" 21235 "==== text by the vcs\n" 21236 " parameter,\n" 21237 "<<<< text by the vcs\n" 21238 " parameter);\n", 21239 format("void f() {\n" 21240 " function(\n" 21241 ">>>> text by the vcs\n" 21242 " parameter,\n" 21243 "==== text by the vcs\n" 21244 " parameter,\n" 21245 "==== text by the vcs\n" 21246 " parameter,\n" 21247 "<<<< text by the vcs\n" 21248 " parameter);\n")); 21249 21250 EXPECT_EQ("<<<<<<<\n" 21251 "|||||||\n" 21252 "=======\n" 21253 ">>>>>>>", 21254 format("<<<<<<<\n" 21255 "|||||||\n" 21256 "=======\n" 21257 ">>>>>>>")); 21258 21259 EXPECT_EQ("<<<<<<<\n" 21260 "|||||||\n" 21261 "int i;\n" 21262 "=======\n" 21263 ">>>>>>>", 21264 format("<<<<<<<\n" 21265 "|||||||\n" 21266 "int i;\n" 21267 "=======\n" 21268 ">>>>>>>")); 21269 21270 // FIXME: Handle parsing of macros around conflict markers correctly: 21271 EXPECT_EQ("#define Macro \\\n" 21272 "<<<<<<<\n" 21273 "Something \\\n" 21274 "|||||||\n" 21275 "Else \\\n" 21276 "=======\n" 21277 "Other \\\n" 21278 ">>>>>>>\n" 21279 " End int i;\n", 21280 format("#define Macro \\\n" 21281 "<<<<<<<\n" 21282 " Something \\\n" 21283 "|||||||\n" 21284 " Else \\\n" 21285 "=======\n" 21286 " Other \\\n" 21287 ">>>>>>>\n" 21288 " End\n" 21289 "int i;\n")); 21290 21291 verifyFormat(R"(==== 21292 #ifdef A 21293 a 21294 #else 21295 b 21296 #endif 21297 )"); 21298 } 21299 21300 TEST_F(FormatTest, DisableRegions) { 21301 EXPECT_EQ("int i;\n" 21302 "// clang-format off\n" 21303 " int j;\n" 21304 "// clang-format on\n" 21305 "int k;", 21306 format(" int i;\n" 21307 " // clang-format off\n" 21308 " int j;\n" 21309 " // clang-format on\n" 21310 " int k;")); 21311 EXPECT_EQ("int i;\n" 21312 "/* clang-format off */\n" 21313 " int j;\n" 21314 "/* clang-format on */\n" 21315 "int k;", 21316 format(" int i;\n" 21317 " /* clang-format off */\n" 21318 " int j;\n" 21319 " /* clang-format on */\n" 21320 " int k;")); 21321 21322 // Don't reflow comments within disabled regions. 21323 EXPECT_EQ("// clang-format off\n" 21324 "// long long long long long long line\n" 21325 "/* clang-format on */\n" 21326 "/* long long long\n" 21327 " * long long long\n" 21328 " * line */\n" 21329 "int i;\n" 21330 "/* clang-format off */\n" 21331 "/* long long long long long long line */\n", 21332 format("// clang-format off\n" 21333 "// long long long long long long line\n" 21334 "/* clang-format on */\n" 21335 "/* long long long long long long line */\n" 21336 "int i;\n" 21337 "/* clang-format off */\n" 21338 "/* long long long long long long line */\n", 21339 getLLVMStyleWithColumns(20))); 21340 } 21341 21342 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 21343 format("? ) ="); 21344 verifyNoCrash("#define a\\\n /**/}"); 21345 } 21346 21347 TEST_F(FormatTest, FormatsTableGenCode) { 21348 FormatStyle Style = getLLVMStyle(); 21349 Style.Language = FormatStyle::LK_TableGen; 21350 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 21351 } 21352 21353 TEST_F(FormatTest, ArrayOfTemplates) { 21354 EXPECT_EQ("auto a = new unique_ptr<int>[10];", 21355 format("auto a = new unique_ptr<int > [ 10];")); 21356 21357 FormatStyle Spaces = getLLVMStyle(); 21358 Spaces.SpacesInSquareBrackets = true; 21359 EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];", 21360 format("auto a = new unique_ptr<int > [10];", Spaces)); 21361 } 21362 21363 TEST_F(FormatTest, ArrayAsTemplateType) { 21364 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;", 21365 format("auto a = unique_ptr < Foo < Bar>[ 10]> ;")); 21366 21367 FormatStyle Spaces = getLLVMStyle(); 21368 Spaces.SpacesInSquareBrackets = true; 21369 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;", 21370 format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces)); 21371 } 21372 21373 TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); } 21374 21375 TEST(FormatStyle, GetStyleWithEmptyFileName) { 21376 llvm::vfs::InMemoryFileSystem FS; 21377 auto Style1 = getStyle("file", "", "Google", "", &FS); 21378 ASSERT_TRUE((bool)Style1); 21379 ASSERT_EQ(*Style1, getGoogleStyle()); 21380 } 21381 21382 TEST(FormatStyle, GetStyleOfFile) { 21383 llvm::vfs::InMemoryFileSystem FS; 21384 // Test 1: format file in the same directory. 21385 ASSERT_TRUE( 21386 FS.addFile("/a/.clang-format", 0, 21387 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 21388 ASSERT_TRUE( 21389 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 21390 auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS); 21391 ASSERT_TRUE((bool)Style1); 21392 ASSERT_EQ(*Style1, getLLVMStyle()); 21393 21394 // Test 2.1: fallback to default. 21395 ASSERT_TRUE( 21396 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 21397 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS); 21398 ASSERT_TRUE((bool)Style2); 21399 ASSERT_EQ(*Style2, getMozillaStyle()); 21400 21401 // Test 2.2: no format on 'none' fallback style. 21402 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 21403 ASSERT_TRUE((bool)Style2); 21404 ASSERT_EQ(*Style2, getNoStyle()); 21405 21406 // Test 2.3: format if config is found with no based style while fallback is 21407 // 'none'. 21408 ASSERT_TRUE(FS.addFile("/b/.clang-format", 0, 21409 llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2"))); 21410 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 21411 ASSERT_TRUE((bool)Style2); 21412 ASSERT_EQ(*Style2, getLLVMStyle()); 21413 21414 // Test 2.4: format if yaml with no based style, while fallback is 'none'. 21415 Style2 = getStyle("{}", "a.h", "none", "", &FS); 21416 ASSERT_TRUE((bool)Style2); 21417 ASSERT_EQ(*Style2, getLLVMStyle()); 21418 21419 // Test 3: format file in parent directory. 21420 ASSERT_TRUE( 21421 FS.addFile("/c/.clang-format", 0, 21422 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 21423 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, 21424 llvm::MemoryBuffer::getMemBuffer("int i;"))); 21425 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS); 21426 ASSERT_TRUE((bool)Style3); 21427 ASSERT_EQ(*Style3, getGoogleStyle()); 21428 21429 // Test 4: error on invalid fallback style 21430 auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS); 21431 ASSERT_FALSE((bool)Style4); 21432 llvm::consumeError(Style4.takeError()); 21433 21434 // Test 5: error on invalid yaml on command line 21435 auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS); 21436 ASSERT_FALSE((bool)Style5); 21437 llvm::consumeError(Style5.takeError()); 21438 21439 // Test 6: error on invalid style 21440 auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS); 21441 ASSERT_FALSE((bool)Style6); 21442 llvm::consumeError(Style6.takeError()); 21443 21444 // Test 7: found config file, error on parsing it 21445 ASSERT_TRUE( 21446 FS.addFile("/d/.clang-format", 0, 21447 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n" 21448 "InvalidKey: InvalidValue"))); 21449 ASSERT_TRUE( 21450 FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 21451 auto Style7a = getStyle("file", "/d/.clang-format", "LLVM", "", &FS); 21452 ASSERT_FALSE((bool)Style7a); 21453 llvm::consumeError(Style7a.takeError()); 21454 21455 auto Style7b = getStyle("file", "/d/.clang-format", "LLVM", "", &FS, true); 21456 ASSERT_TRUE((bool)Style7b); 21457 21458 // Test 8: inferred per-language defaults apply. 21459 auto StyleTd = getStyle("file", "x.td", "llvm", "", &FS); 21460 ASSERT_TRUE((bool)StyleTd); 21461 ASSERT_EQ(*StyleTd, getLLVMStyle(FormatStyle::LK_TableGen)); 21462 21463 // Test 9.1: overwriting a file style, when parent no file exists with no 21464 // fallback style 21465 ASSERT_TRUE(FS.addFile( 21466 "/e/sub/.clang-format", 0, 21467 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: InheritParentConfig\n" 21468 "ColumnLimit: 20"))); 21469 ASSERT_TRUE(FS.addFile("/e/sub/code.cpp", 0, 21470 llvm::MemoryBuffer::getMemBuffer("int i;"))); 21471 auto Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS); 21472 ASSERT_TRUE(static_cast<bool>(Style9)); 21473 ASSERT_EQ(*Style9, [] { 21474 auto Style = getNoStyle(); 21475 Style.ColumnLimit = 20; 21476 return Style; 21477 }()); 21478 21479 // Test 9.2: with LLVM fallback style 21480 Style9 = getStyle("file", "/e/sub/code.cpp", "LLVM", "", &FS); 21481 ASSERT_TRUE(static_cast<bool>(Style9)); 21482 ASSERT_EQ(*Style9, [] { 21483 auto Style = getLLVMStyle(); 21484 Style.ColumnLimit = 20; 21485 return Style; 21486 }()); 21487 21488 // Test 9.3: with a parent file 21489 ASSERT_TRUE( 21490 FS.addFile("/e/.clang-format", 0, 21491 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google\n" 21492 "UseTab: Always"))); 21493 Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS); 21494 ASSERT_TRUE(static_cast<bool>(Style9)); 21495 ASSERT_EQ(*Style9, [] { 21496 auto Style = getGoogleStyle(); 21497 Style.ColumnLimit = 20; 21498 Style.UseTab = FormatStyle::UT_Always; 21499 return Style; 21500 }()); 21501 21502 // Test 9.4: propagate more than one level 21503 ASSERT_TRUE(FS.addFile("/e/sub/sub/code.cpp", 0, 21504 llvm::MemoryBuffer::getMemBuffer("int i;"))); 21505 ASSERT_TRUE(FS.addFile("/e/sub/sub/.clang-format", 0, 21506 llvm::MemoryBuffer::getMemBuffer( 21507 "BasedOnStyle: InheritParentConfig\n" 21508 "WhitespaceSensitiveMacros: ['FOO', 'BAR']"))); 21509 std::vector<std::string> NonDefaultWhiteSpaceMacros{"FOO", "BAR"}; 21510 21511 const auto SubSubStyle = [&NonDefaultWhiteSpaceMacros] { 21512 auto Style = getGoogleStyle(); 21513 Style.ColumnLimit = 20; 21514 Style.UseTab = FormatStyle::UT_Always; 21515 Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros; 21516 return Style; 21517 }(); 21518 21519 ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros); 21520 Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS); 21521 ASSERT_TRUE(static_cast<bool>(Style9)); 21522 ASSERT_EQ(*Style9, SubSubStyle); 21523 21524 // Test 9.5: use InheritParentConfig as style name 21525 Style9 = 21526 getStyle("inheritparentconfig", "/e/sub/sub/code.cpp", "none", "", &FS); 21527 ASSERT_TRUE(static_cast<bool>(Style9)); 21528 ASSERT_EQ(*Style9, SubSubStyle); 21529 21530 // Test 9.6: use command line style with inheritance 21531 Style9 = getStyle("{BasedOnStyle: InheritParentConfig}", "/e/sub/code.cpp", 21532 "none", "", &FS); 21533 ASSERT_TRUE(static_cast<bool>(Style9)); 21534 ASSERT_EQ(*Style9, SubSubStyle); 21535 21536 // Test 9.7: use command line style with inheritance and own config 21537 Style9 = getStyle("{BasedOnStyle: InheritParentConfig, " 21538 "WhitespaceSensitiveMacros: ['FOO', 'BAR']}", 21539 "/e/sub/code.cpp", "none", "", &FS); 21540 ASSERT_TRUE(static_cast<bool>(Style9)); 21541 ASSERT_EQ(*Style9, SubSubStyle); 21542 21543 // Test 9.8: use inheritance from a file without BasedOnStyle 21544 ASSERT_TRUE(FS.addFile("/e/withoutbase/.clang-format", 0, 21545 llvm::MemoryBuffer::getMemBuffer("ColumnLimit: 123"))); 21546 ASSERT_TRUE( 21547 FS.addFile("/e/withoutbase/sub/.clang-format", 0, 21548 llvm::MemoryBuffer::getMemBuffer( 21549 "BasedOnStyle: InheritParentConfig\nIndentWidth: 7"))); 21550 // Make sure we do not use the fallback style 21551 Style9 = getStyle("file", "/e/withoutbase/code.cpp", "google", "", &FS); 21552 ASSERT_TRUE(static_cast<bool>(Style9)); 21553 ASSERT_EQ(*Style9, [] { 21554 auto Style = getLLVMStyle(); 21555 Style.ColumnLimit = 123; 21556 return Style; 21557 }()); 21558 21559 Style9 = getStyle("file", "/e/withoutbase/sub/code.cpp", "google", "", &FS); 21560 ASSERT_TRUE(static_cast<bool>(Style9)); 21561 ASSERT_EQ(*Style9, [] { 21562 auto Style = getLLVMStyle(); 21563 Style.ColumnLimit = 123; 21564 Style.IndentWidth = 7; 21565 return Style; 21566 }()); 21567 } 21568 21569 TEST_F(ReplacementTest, FormatCodeAfterReplacements) { 21570 // Column limit is 20. 21571 std::string Code = "Type *a =\n" 21572 " new Type();\n" 21573 "g(iiiii, 0, jjjjj,\n" 21574 " 0, kkkkk, 0, mm);\n" 21575 "int bad = format ;"; 21576 std::string Expected = "auto a = new Type();\n" 21577 "g(iiiii, nullptr,\n" 21578 " jjjjj, nullptr,\n" 21579 " kkkkk, nullptr,\n" 21580 " mm);\n" 21581 "int bad = format ;"; 21582 FileID ID = Context.createInMemoryFile("format.cpp", Code); 21583 tooling::Replacements Replaces = toReplacements( 21584 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6, 21585 "auto "), 21586 tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1, 21587 "nullptr"), 21588 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1, 21589 "nullptr"), 21590 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1, 21591 "nullptr")}); 21592 21593 FormatStyle Style = getLLVMStyle(); 21594 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. 21595 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 21596 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 21597 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 21598 auto Result = applyAllReplacements(Code, *FormattedReplaces); 21599 EXPECT_TRUE(static_cast<bool>(Result)); 21600 EXPECT_EQ(Expected, *Result); 21601 } 21602 21603 TEST_F(ReplacementTest, SortIncludesAfterReplacement) { 21604 std::string Code = "#include \"a.h\"\n" 21605 "#include \"c.h\"\n" 21606 "\n" 21607 "int main() {\n" 21608 " return 0;\n" 21609 "}"; 21610 std::string Expected = "#include \"a.h\"\n" 21611 "#include \"b.h\"\n" 21612 "#include \"c.h\"\n" 21613 "\n" 21614 "int main() {\n" 21615 " return 0;\n" 21616 "}"; 21617 FileID ID = Context.createInMemoryFile("fix.cpp", Code); 21618 tooling::Replacements Replaces = toReplacements( 21619 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0, 21620 "#include \"b.h\"\n")}); 21621 21622 FormatStyle Style = getLLVMStyle(); 21623 Style.SortIncludes = FormatStyle::SI_CaseSensitive; 21624 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 21625 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 21626 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 21627 auto Result = applyAllReplacements(Code, *FormattedReplaces); 21628 EXPECT_TRUE(static_cast<bool>(Result)); 21629 EXPECT_EQ(Expected, *Result); 21630 } 21631 21632 TEST_F(FormatTest, FormatSortsUsingDeclarations) { 21633 EXPECT_EQ("using std::cin;\n" 21634 "using std::cout;", 21635 format("using std::cout;\n" 21636 "using std::cin;", 21637 getGoogleStyle())); 21638 } 21639 21640 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) { 21641 FormatStyle Style = getLLVMStyle(); 21642 Style.Standard = FormatStyle::LS_Cpp03; 21643 // cpp03 recognize this string as identifier u8 and literal character 'a' 21644 EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style)); 21645 } 21646 21647 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) { 21648 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers 21649 // all modes, including C++11, C++14 and C++17 21650 EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';")); 21651 } 21652 21653 TEST_F(FormatTest, DoNotFormatLikelyXml) { 21654 EXPECT_EQ("<!-- ;> -->", format("<!-- ;> -->", getGoogleStyle())); 21655 EXPECT_EQ(" <!-- >; -->", format(" <!-- >; -->", getGoogleStyle())); 21656 } 21657 21658 TEST_F(FormatTest, StructuredBindings) { 21659 // Structured bindings is a C++17 feature. 21660 // all modes, including C++11, C++14 and C++17 21661 verifyFormat("auto [a, b] = f();"); 21662 EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();")); 21663 EXPECT_EQ("const auto [a, b] = f();", format("const auto[a, b] = f();")); 21664 EXPECT_EQ("auto const [a, b] = f();", format("auto const[a, b] = f();")); 21665 EXPECT_EQ("auto const volatile [a, b] = f();", 21666 format("auto const volatile[a, b] = f();")); 21667 EXPECT_EQ("auto [a, b, c] = f();", format("auto [ a , b,c ] = f();")); 21668 EXPECT_EQ("auto &[a, b, c] = f();", 21669 format("auto &[ a , b,c ] = f();")); 21670 EXPECT_EQ("auto &&[a, b, c] = f();", 21671 format("auto &&[ a , b,c ] = f();")); 21672 EXPECT_EQ("auto const &[a, b] = f();", format("auto const&[a, b] = f();")); 21673 EXPECT_EQ("auto const volatile &&[a, b] = f();", 21674 format("auto const volatile &&[a, b] = f();")); 21675 EXPECT_EQ("auto const &&[a, b] = f();", 21676 format("auto const && [a, b] = f();")); 21677 EXPECT_EQ("const auto &[a, b] = f();", 21678 format("const auto & [a, b] = f();")); 21679 EXPECT_EQ("const auto volatile &&[a, b] = f();", 21680 format("const auto volatile &&[a, b] = f();")); 21681 EXPECT_EQ("volatile const auto &&[a, b] = f();", 21682 format("volatile const auto &&[a, b] = f();")); 21683 EXPECT_EQ("const auto &&[a, b] = f();", 21684 format("const auto && [a, b] = f();")); 21685 21686 // Make sure we don't mistake structured bindings for lambdas. 21687 FormatStyle PointerMiddle = getLLVMStyle(); 21688 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 21689 verifyFormat("auto [a1, b]{A * i};", getGoogleStyle()); 21690 verifyFormat("auto [a2, b]{A * i};", getLLVMStyle()); 21691 verifyFormat("auto [a3, b]{A * i};", PointerMiddle); 21692 verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle()); 21693 verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle()); 21694 verifyFormat("auto const [a3, b]{A * i};", PointerMiddle); 21695 verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle()); 21696 verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle()); 21697 verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle); 21698 verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle()); 21699 verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle()); 21700 verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle); 21701 21702 EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}", 21703 format("for (const auto && [a, b] : some_range) {\n}")); 21704 EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}", 21705 format("for (const auto & [a, b] : some_range) {\n}")); 21706 EXPECT_EQ("for (const auto [a, b] : some_range) {\n}", 21707 format("for (const auto[a, b] : some_range) {\n}")); 21708 EXPECT_EQ("auto [x, y](expr);", format("auto[x,y] (expr);")); 21709 EXPECT_EQ("auto &[x, y](expr);", format("auto & [x,y] (expr);")); 21710 EXPECT_EQ("auto &&[x, y](expr);", format("auto && [x,y] (expr);")); 21711 EXPECT_EQ("auto const &[x, y](expr);", 21712 format("auto const & [x,y] (expr);")); 21713 EXPECT_EQ("auto const &&[x, y](expr);", 21714 format("auto const && [x,y] (expr);")); 21715 EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y] {expr};")); 21716 EXPECT_EQ("auto const &[x, y]{expr};", 21717 format("auto const & [x,y] {expr};")); 21718 EXPECT_EQ("auto const &&[x, y]{expr};", 21719 format("auto const && [x,y] {expr};")); 21720 21721 FormatStyle Spaces = getLLVMStyle(); 21722 Spaces.SpacesInSquareBrackets = true; 21723 verifyFormat("auto [ a, b ] = f();", Spaces); 21724 verifyFormat("auto &&[ a, b ] = f();", Spaces); 21725 verifyFormat("auto &[ a, b ] = f();", Spaces); 21726 verifyFormat("auto const &&[ a, b ] = f();", Spaces); 21727 verifyFormat("auto const &[ a, b ] = f();", Spaces); 21728 } 21729 21730 TEST_F(FormatTest, FileAndCode) { 21731 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", "")); 21732 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", "")); 21733 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", "")); 21734 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "")); 21735 EXPECT_EQ(FormatStyle::LK_ObjC, 21736 guessLanguage("foo.h", "@interface Foo\n@end\n")); 21737 EXPECT_EQ( 21738 FormatStyle::LK_ObjC, 21739 guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }")); 21740 EXPECT_EQ(FormatStyle::LK_ObjC, 21741 guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))")); 21742 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;")); 21743 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", "")); 21744 EXPECT_EQ(FormatStyle::LK_ObjC, 21745 guessLanguage("foo", "@interface Foo\n@end\n")); 21746 EXPECT_EQ(FormatStyle::LK_ObjC, 21747 guessLanguage("foo.h", "int DoStuff(CGRect rect);\n")); 21748 EXPECT_EQ( 21749 FormatStyle::LK_ObjC, 21750 guessLanguage("foo.h", 21751 "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n")); 21752 EXPECT_EQ( 21753 FormatStyle::LK_Cpp, 21754 guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;")); 21755 } 21756 21757 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) { 21758 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];")); 21759 EXPECT_EQ(FormatStyle::LK_ObjC, 21760 guessLanguage("foo.h", "array[[calculator getIndex]];")); 21761 EXPECT_EQ(FormatStyle::LK_Cpp, 21762 guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];")); 21763 EXPECT_EQ( 21764 FormatStyle::LK_Cpp, 21765 guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];")); 21766 EXPECT_EQ(FormatStyle::LK_ObjC, 21767 guessLanguage("foo.h", "[[noreturn foo] bar];")); 21768 EXPECT_EQ(FormatStyle::LK_Cpp, 21769 guessLanguage("foo.h", "[[clang::fallthrough]];")); 21770 EXPECT_EQ(FormatStyle::LK_ObjC, 21771 guessLanguage("foo.h", "[[clang:fallthrough] foo];")); 21772 EXPECT_EQ(FormatStyle::LK_Cpp, 21773 guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];")); 21774 EXPECT_EQ(FormatStyle::LK_Cpp, 21775 guessLanguage("foo.h", "[[using clang: fallthrough]];")); 21776 EXPECT_EQ(FormatStyle::LK_ObjC, 21777 guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];")); 21778 EXPECT_EQ(FormatStyle::LK_Cpp, 21779 guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];")); 21780 EXPECT_EQ( 21781 FormatStyle::LK_Cpp, 21782 guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)")); 21783 EXPECT_EQ( 21784 FormatStyle::LK_Cpp, 21785 guessLanguage("foo.h", 21786 "[[clang::callable_when(\"unconsumed\", \"unknown\")]]")); 21787 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]")); 21788 } 21789 21790 TEST_F(FormatTest, GuessLanguageWithCaret) { 21791 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);")); 21792 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);")); 21793 EXPECT_EQ(FormatStyle::LK_ObjC, 21794 guessLanguage("foo.h", "int(^)(char, float);")); 21795 EXPECT_EQ(FormatStyle::LK_ObjC, 21796 guessLanguage("foo.h", "int(^foo)(char, float);")); 21797 EXPECT_EQ(FormatStyle::LK_ObjC, 21798 guessLanguage("foo.h", "int(^foo[10])(char, float);")); 21799 EXPECT_EQ(FormatStyle::LK_ObjC, 21800 guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);")); 21801 EXPECT_EQ( 21802 FormatStyle::LK_ObjC, 21803 guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);")); 21804 } 21805 21806 TEST_F(FormatTest, GuessLanguageWithPragmas) { 21807 EXPECT_EQ(FormatStyle::LK_Cpp, 21808 guessLanguage("foo.h", "__pragma(warning(disable:))")); 21809 EXPECT_EQ(FormatStyle::LK_Cpp, 21810 guessLanguage("foo.h", "#pragma(warning(disable:))")); 21811 EXPECT_EQ(FormatStyle::LK_Cpp, 21812 guessLanguage("foo.h", "_Pragma(warning(disable:))")); 21813 } 21814 21815 TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) { 21816 // ASM symbolic names are identifiers that must be surrounded by [] without 21817 // space in between: 21818 // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands 21819 21820 // Example from https://bugs.llvm.org/show_bug.cgi?id=45108. 21821 verifyFormat(R"(// 21822 asm volatile("mrs %x[result], FPCR" : [result] "=r"(result)); 21823 )"); 21824 21825 // A list of several ASM symbolic names. 21826 verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)"); 21827 21828 // ASM symbolic names in inline ASM with inputs and outputs. 21829 verifyFormat(R"(// 21830 asm("cmoveq %1, %2, %[result]" 21831 : [result] "=r"(result) 21832 : "r"(test), "r"(new), "[result]"(old)); 21833 )"); 21834 21835 // ASM symbolic names in inline ASM with no outputs. 21836 verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)"); 21837 } 21838 21839 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) { 21840 EXPECT_EQ(FormatStyle::LK_Cpp, 21841 guessLanguage("foo.h", "void f() {\n" 21842 " asm (\"mov %[e], %[d]\"\n" 21843 " : [d] \"=rm\" (d)\n" 21844 " [e] \"rm\" (*e));\n" 21845 "}")); 21846 EXPECT_EQ(FormatStyle::LK_Cpp, 21847 guessLanguage("foo.h", "void f() {\n" 21848 " _asm (\"mov %[e], %[d]\"\n" 21849 " : [d] \"=rm\" (d)\n" 21850 " [e] \"rm\" (*e));\n" 21851 "}")); 21852 EXPECT_EQ(FormatStyle::LK_Cpp, 21853 guessLanguage("foo.h", "void f() {\n" 21854 " __asm (\"mov %[e], %[d]\"\n" 21855 " : [d] \"=rm\" (d)\n" 21856 " [e] \"rm\" (*e));\n" 21857 "}")); 21858 EXPECT_EQ(FormatStyle::LK_Cpp, 21859 guessLanguage("foo.h", "void f() {\n" 21860 " __asm__ (\"mov %[e], %[d]\"\n" 21861 " : [d] \"=rm\" (d)\n" 21862 " [e] \"rm\" (*e));\n" 21863 "}")); 21864 EXPECT_EQ(FormatStyle::LK_Cpp, 21865 guessLanguage("foo.h", "void f() {\n" 21866 " asm (\"mov %[e], %[d]\"\n" 21867 " : [d] \"=rm\" (d),\n" 21868 " [e] \"rm\" (*e));\n" 21869 "}")); 21870 EXPECT_EQ(FormatStyle::LK_Cpp, 21871 guessLanguage("foo.h", "void f() {\n" 21872 " asm volatile (\"mov %[e], %[d]\"\n" 21873 " : [d] \"=rm\" (d)\n" 21874 " [e] \"rm\" (*e));\n" 21875 "}")); 21876 } 21877 21878 TEST_F(FormatTest, GuessLanguageWithChildLines) { 21879 EXPECT_EQ(FormatStyle::LK_Cpp, 21880 guessLanguage("foo.h", "#define FOO ({ std::string s; })")); 21881 EXPECT_EQ(FormatStyle::LK_ObjC, 21882 guessLanguage("foo.h", "#define FOO ({ NSString *s; })")); 21883 EXPECT_EQ( 21884 FormatStyle::LK_Cpp, 21885 guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })")); 21886 EXPECT_EQ( 21887 FormatStyle::LK_ObjC, 21888 guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })")); 21889 } 21890 21891 TEST_F(FormatTest, TypenameMacros) { 21892 std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"}; 21893 21894 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353 21895 FormatStyle Google = getGoogleStyleWithColumns(0); 21896 Google.TypenameMacros = TypenameMacros; 21897 verifyFormat("struct foo {\n" 21898 " int bar;\n" 21899 " TAILQ_ENTRY(a) bleh;\n" 21900 "};", 21901 Google); 21902 21903 FormatStyle Macros = getLLVMStyle(); 21904 Macros.TypenameMacros = TypenameMacros; 21905 21906 verifyFormat("STACK_OF(int) a;", Macros); 21907 verifyFormat("STACK_OF(int) *a;", Macros); 21908 verifyFormat("STACK_OF(int const *) *a;", Macros); 21909 verifyFormat("STACK_OF(int *const) *a;", Macros); 21910 verifyFormat("STACK_OF(int, string) a;", Macros); 21911 verifyFormat("STACK_OF(LIST(int)) a;", Macros); 21912 verifyFormat("STACK_OF(LIST(int)) a, b;", Macros); 21913 verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros); 21914 verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros); 21915 verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros); 21916 verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros); 21917 21918 Macros.PointerAlignment = FormatStyle::PAS_Left; 21919 verifyFormat("STACK_OF(int)* a;", Macros); 21920 verifyFormat("STACK_OF(int*)* a;", Macros); 21921 verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros); 21922 verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros); 21923 verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros); 21924 } 21925 21926 TEST_F(FormatTest, AtomicQualifier) { 21927 // Check that we treate _Atomic as a type and not a function call 21928 FormatStyle Google = getGoogleStyleWithColumns(0); 21929 verifyFormat("struct foo {\n" 21930 " int a1;\n" 21931 " _Atomic(a) a2;\n" 21932 " _Atomic(_Atomic(int) *const) a3;\n" 21933 "};", 21934 Google); 21935 verifyFormat("_Atomic(uint64_t) a;"); 21936 verifyFormat("_Atomic(uint64_t) *a;"); 21937 verifyFormat("_Atomic(uint64_t const *) *a;"); 21938 verifyFormat("_Atomic(uint64_t *const) *a;"); 21939 verifyFormat("_Atomic(const uint64_t *) *a;"); 21940 verifyFormat("_Atomic(uint64_t) a;"); 21941 verifyFormat("_Atomic(_Atomic(uint64_t)) a;"); 21942 verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;"); 21943 verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}"); 21944 verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);"); 21945 21946 verifyFormat("_Atomic(uint64_t) *s(InitValue);"); 21947 verifyFormat("_Atomic(uint64_t) *s{InitValue};"); 21948 FormatStyle Style = getLLVMStyle(); 21949 Style.PointerAlignment = FormatStyle::PAS_Left; 21950 verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style); 21951 verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style); 21952 verifyFormat("_Atomic(int)* a;", Style); 21953 verifyFormat("_Atomic(int*)* a;", Style); 21954 verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style); 21955 21956 Style.SpacesInCStyleCastParentheses = true; 21957 Style.SpacesInParentheses = false; 21958 verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style); 21959 Style.SpacesInCStyleCastParentheses = false; 21960 Style.SpacesInParentheses = true; 21961 verifyFormat("x = (_Atomic( uint64_t ))*a;", Style); 21962 verifyFormat("x = (_Atomic( uint64_t ))&a;", Style); 21963 } 21964 21965 TEST_F(FormatTest, AmbersandInLamda) { 21966 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899 21967 FormatStyle AlignStyle = getLLVMStyle(); 21968 AlignStyle.PointerAlignment = FormatStyle::PAS_Left; 21969 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle); 21970 AlignStyle.PointerAlignment = FormatStyle::PAS_Right; 21971 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle); 21972 } 21973 21974 TEST_F(FormatTest, SpacesInConditionalStatement) { 21975 FormatStyle Spaces = getLLVMStyle(); 21976 Spaces.IfMacros.clear(); 21977 Spaces.IfMacros.push_back("MYIF"); 21978 Spaces.SpacesInConditionalStatement = true; 21979 verifyFormat("for ( int i = 0; i; i++ )\n continue;", Spaces); 21980 verifyFormat("if ( !a )\n return;", Spaces); 21981 verifyFormat("if ( a )\n return;", Spaces); 21982 verifyFormat("if constexpr ( a )\n return;", Spaces); 21983 verifyFormat("MYIF ( a )\n return;", Spaces); 21984 verifyFormat("MYIF ( a )\n return;\nelse MYIF ( b )\n return;", Spaces); 21985 verifyFormat("MYIF ( a )\n return;\nelse\n return;", Spaces); 21986 verifyFormat("switch ( a )\ncase 1:\n return;", Spaces); 21987 verifyFormat("while ( a )\n return;", Spaces); 21988 verifyFormat("while ( (a && b) )\n return;", Spaces); 21989 verifyFormat("do {\n} while ( 1 != 0 );", Spaces); 21990 verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces); 21991 // Check that space on the left of "::" is inserted as expected at beginning 21992 // of condition. 21993 verifyFormat("while ( ::func() )\n return;", Spaces); 21994 21995 // Check impact of ControlStatementsExceptControlMacros is honored. 21996 Spaces.SpaceBeforeParens = 21997 FormatStyle::SBPO_ControlStatementsExceptControlMacros; 21998 verifyFormat("MYIF( a )\n return;", Spaces); 21999 verifyFormat("MYIF( a )\n return;\nelse MYIF( b )\n return;", Spaces); 22000 verifyFormat("MYIF( a )\n return;\nelse\n return;", Spaces); 22001 } 22002 22003 TEST_F(FormatTest, AlternativeOperators) { 22004 // Test case for ensuring alternate operators are not 22005 // combined with their right most neighbour. 22006 verifyFormat("int a and b;"); 22007 verifyFormat("int a and_eq b;"); 22008 verifyFormat("int a bitand b;"); 22009 verifyFormat("int a bitor b;"); 22010 verifyFormat("int a compl b;"); 22011 verifyFormat("int a not b;"); 22012 verifyFormat("int a not_eq b;"); 22013 verifyFormat("int a or b;"); 22014 verifyFormat("int a xor b;"); 22015 verifyFormat("int a xor_eq b;"); 22016 verifyFormat("return this not_eq bitand other;"); 22017 verifyFormat("bool operator not_eq(const X bitand other)"); 22018 22019 verifyFormat("int a and 5;"); 22020 verifyFormat("int a and_eq 5;"); 22021 verifyFormat("int a bitand 5;"); 22022 verifyFormat("int a bitor 5;"); 22023 verifyFormat("int a compl 5;"); 22024 verifyFormat("int a not 5;"); 22025 verifyFormat("int a not_eq 5;"); 22026 verifyFormat("int a or 5;"); 22027 verifyFormat("int a xor 5;"); 22028 verifyFormat("int a xor_eq 5;"); 22029 22030 verifyFormat("int a compl(5);"); 22031 verifyFormat("int a not(5);"); 22032 22033 /* FIXME handle alternate tokens 22034 * https://en.cppreference.com/w/cpp/language/operator_alternative 22035 // alternative tokens 22036 verifyFormat("compl foo();"); // ~foo(); 22037 verifyFormat("foo() <%%>;"); // foo(); 22038 verifyFormat("void foo() <%%>;"); // void foo(){} 22039 verifyFormat("int a <:1:>;"); // int a[1];[ 22040 verifyFormat("%:define ABC abc"); // #define ABC abc 22041 verifyFormat("%:%:"); // ## 22042 */ 22043 } 22044 22045 TEST_F(FormatTest, STLWhileNotDefineChed) { 22046 verifyFormat("#if defined(while)\n" 22047 "#define while EMIT WARNING C4005\n" 22048 "#endif // while"); 22049 } 22050 22051 TEST_F(FormatTest, OperatorSpacing) { 22052 FormatStyle Style = getLLVMStyle(); 22053 Style.PointerAlignment = FormatStyle::PAS_Right; 22054 verifyFormat("Foo::operator*();", Style); 22055 verifyFormat("Foo::operator void *();", Style); 22056 verifyFormat("Foo::operator void **();", Style); 22057 verifyFormat("Foo::operator void *&();", Style); 22058 verifyFormat("Foo::operator void *&&();", Style); 22059 verifyFormat("Foo::operator void const *();", Style); 22060 verifyFormat("Foo::operator void const **();", Style); 22061 verifyFormat("Foo::operator void const *&();", Style); 22062 verifyFormat("Foo::operator void const *&&();", Style); 22063 verifyFormat("Foo::operator()(void *);", Style); 22064 verifyFormat("Foo::operator*(void *);", Style); 22065 verifyFormat("Foo::operator*();", Style); 22066 verifyFormat("Foo::operator**();", Style); 22067 verifyFormat("Foo::operator&();", Style); 22068 verifyFormat("Foo::operator<int> *();", Style); 22069 verifyFormat("Foo::operator<Foo> *();", Style); 22070 verifyFormat("Foo::operator<int> **();", Style); 22071 verifyFormat("Foo::operator<Foo> **();", Style); 22072 verifyFormat("Foo::operator<int> &();", Style); 22073 verifyFormat("Foo::operator<Foo> &();", Style); 22074 verifyFormat("Foo::operator<int> &&();", Style); 22075 verifyFormat("Foo::operator<Foo> &&();", Style); 22076 verifyFormat("Foo::operator<int> *&();", Style); 22077 verifyFormat("Foo::operator<Foo> *&();", Style); 22078 verifyFormat("Foo::operator<int> *&&();", Style); 22079 verifyFormat("Foo::operator<Foo> *&&();", Style); 22080 verifyFormat("operator*(int (*)(), class Foo);", Style); 22081 22082 verifyFormat("Foo::operator&();", Style); 22083 verifyFormat("Foo::operator void &();", Style); 22084 verifyFormat("Foo::operator void const &();", Style); 22085 verifyFormat("Foo::operator()(void &);", Style); 22086 verifyFormat("Foo::operator&(void &);", Style); 22087 verifyFormat("Foo::operator&();", Style); 22088 verifyFormat("operator&(int (&)(), class Foo);", Style); 22089 verifyFormat("operator&&(int (&)(), class Foo);", Style); 22090 22091 verifyFormat("Foo::operator&&();", Style); 22092 verifyFormat("Foo::operator**();", Style); 22093 verifyFormat("Foo::operator void &&();", Style); 22094 verifyFormat("Foo::operator void const &&();", Style); 22095 verifyFormat("Foo::operator()(void &&);", Style); 22096 verifyFormat("Foo::operator&&(void &&);", Style); 22097 verifyFormat("Foo::operator&&();", Style); 22098 verifyFormat("operator&&(int (&&)(), class Foo);", Style); 22099 verifyFormat("operator const nsTArrayRight<E> &()", Style); 22100 verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()", 22101 Style); 22102 verifyFormat("operator void **()", Style); 22103 verifyFormat("operator const FooRight<Object> &()", Style); 22104 verifyFormat("operator const FooRight<Object> *()", Style); 22105 verifyFormat("operator const FooRight<Object> **()", Style); 22106 verifyFormat("operator const FooRight<Object> *&()", Style); 22107 verifyFormat("operator const FooRight<Object> *&&()", Style); 22108 22109 Style.PointerAlignment = FormatStyle::PAS_Left; 22110 verifyFormat("Foo::operator*();", Style); 22111 verifyFormat("Foo::operator**();", Style); 22112 verifyFormat("Foo::operator void*();", Style); 22113 verifyFormat("Foo::operator void**();", Style); 22114 verifyFormat("Foo::operator void*&();", Style); 22115 verifyFormat("Foo::operator void*&&();", Style); 22116 verifyFormat("Foo::operator void const*();", Style); 22117 verifyFormat("Foo::operator void const**();", Style); 22118 verifyFormat("Foo::operator void const*&();", Style); 22119 verifyFormat("Foo::operator void const*&&();", Style); 22120 verifyFormat("Foo::operator/*comment*/ void*();", Style); 22121 verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style); 22122 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style); 22123 verifyFormat("Foo::operator()(void*);", Style); 22124 verifyFormat("Foo::operator*(void*);", Style); 22125 verifyFormat("Foo::operator*();", Style); 22126 verifyFormat("Foo::operator<int>*();", Style); 22127 verifyFormat("Foo::operator<Foo>*();", Style); 22128 verifyFormat("Foo::operator<int>**();", Style); 22129 verifyFormat("Foo::operator<Foo>**();", Style); 22130 verifyFormat("Foo::operator<Foo>*&();", Style); 22131 verifyFormat("Foo::operator<int>&();", Style); 22132 verifyFormat("Foo::operator<Foo>&();", Style); 22133 verifyFormat("Foo::operator<int>&&();", Style); 22134 verifyFormat("Foo::operator<Foo>&&();", Style); 22135 verifyFormat("Foo::operator<int>*&();", Style); 22136 verifyFormat("Foo::operator<Foo>*&();", Style); 22137 verifyFormat("operator*(int (*)(), class Foo);", Style); 22138 22139 verifyFormat("Foo::operator&();", Style); 22140 verifyFormat("Foo::operator void&();", Style); 22141 verifyFormat("Foo::operator void const&();", Style); 22142 verifyFormat("Foo::operator/*comment*/ void&();", Style); 22143 verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style); 22144 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style); 22145 verifyFormat("Foo::operator()(void&);", Style); 22146 verifyFormat("Foo::operator&(void&);", Style); 22147 verifyFormat("Foo::operator&();", Style); 22148 verifyFormat("operator&(int (&)(), class Foo);", Style); 22149 verifyFormat("operator&(int (&&)(), class Foo);", Style); 22150 verifyFormat("operator&&(int (&&)(), class Foo);", Style); 22151 22152 verifyFormat("Foo::operator&&();", Style); 22153 verifyFormat("Foo::operator void&&();", Style); 22154 verifyFormat("Foo::operator void const&&();", Style); 22155 verifyFormat("Foo::operator/*comment*/ void&&();", Style); 22156 verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style); 22157 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style); 22158 verifyFormat("Foo::operator()(void&&);", Style); 22159 verifyFormat("Foo::operator&&(void&&);", Style); 22160 verifyFormat("Foo::operator&&();", Style); 22161 verifyFormat("operator&&(int (&&)(), class Foo);", Style); 22162 verifyFormat("operator const nsTArrayLeft<E>&()", Style); 22163 verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()", 22164 Style); 22165 verifyFormat("operator void**()", Style); 22166 verifyFormat("operator const FooLeft<Object>&()", Style); 22167 verifyFormat("operator const FooLeft<Object>*()", Style); 22168 verifyFormat("operator const FooLeft<Object>**()", Style); 22169 verifyFormat("operator const FooLeft<Object>*&()", Style); 22170 verifyFormat("operator const FooLeft<Object>*&&()", Style); 22171 22172 // PR45107 22173 verifyFormat("operator Vector<String>&();", Style); 22174 verifyFormat("operator const Vector<String>&();", Style); 22175 verifyFormat("operator foo::Bar*();", Style); 22176 verifyFormat("operator const Foo<X>::Bar<Y>*();", Style); 22177 verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();", 22178 Style); 22179 22180 Style.PointerAlignment = FormatStyle::PAS_Middle; 22181 verifyFormat("Foo::operator*();", Style); 22182 verifyFormat("Foo::operator void *();", Style); 22183 verifyFormat("Foo::operator()(void *);", Style); 22184 verifyFormat("Foo::operator*(void *);", Style); 22185 verifyFormat("Foo::operator*();", Style); 22186 verifyFormat("operator*(int (*)(), class Foo);", Style); 22187 22188 verifyFormat("Foo::operator&();", Style); 22189 verifyFormat("Foo::operator void &();", Style); 22190 verifyFormat("Foo::operator void const &();", Style); 22191 verifyFormat("Foo::operator()(void &);", Style); 22192 verifyFormat("Foo::operator&(void &);", Style); 22193 verifyFormat("Foo::operator&();", Style); 22194 verifyFormat("operator&(int (&)(), class Foo);", Style); 22195 22196 verifyFormat("Foo::operator&&();", Style); 22197 verifyFormat("Foo::operator void &&();", Style); 22198 verifyFormat("Foo::operator void const &&();", Style); 22199 verifyFormat("Foo::operator()(void &&);", Style); 22200 verifyFormat("Foo::operator&&(void &&);", Style); 22201 verifyFormat("Foo::operator&&();", Style); 22202 verifyFormat("operator&&(int (&&)(), class Foo);", Style); 22203 } 22204 22205 TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) { 22206 FormatStyle Style = getLLVMStyle(); 22207 // PR46157 22208 verifyFormat("foo(operator+, -42);", Style); 22209 verifyFormat("foo(operator++, -42);", Style); 22210 verifyFormat("foo(operator--, -42);", Style); 22211 verifyFormat("foo(-42, operator--);", Style); 22212 verifyFormat("foo(-42, operator, );", Style); 22213 verifyFormat("foo(operator, , -42);", Style); 22214 } 22215 22216 TEST_F(FormatTest, WhitespaceSensitiveMacros) { 22217 FormatStyle Style = getLLVMStyle(); 22218 Style.WhitespaceSensitiveMacros.push_back("FOO"); 22219 22220 // Don't use the helpers here, since 'mess up' will change the whitespace 22221 // and these are all whitespace sensitive by definition 22222 EXPECT_EQ("FOO(String-ized&Messy+But(: :Still)=Intentional);", 22223 format("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style)); 22224 EXPECT_EQ( 22225 "FOO(String-ized&Messy+But\\(: :Still)=Intentional);", 22226 format("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style)); 22227 EXPECT_EQ("FOO(String-ized&Messy+But,: :Still=Intentional);", 22228 format("FOO(String-ized&Messy+But,: :Still=Intentional);", Style)); 22229 EXPECT_EQ("FOO(String-ized&Messy+But,: :\n" 22230 " Still=Intentional);", 22231 format("FOO(String-ized&Messy+But,: :\n" 22232 " Still=Intentional);", 22233 Style)); 22234 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 22235 EXPECT_EQ("FOO(String-ized=&Messy+But,: :\n" 22236 " Still=Intentional);", 22237 format("FOO(String-ized=&Messy+But,: :\n" 22238 " Still=Intentional);", 22239 Style)); 22240 22241 Style.ColumnLimit = 21; 22242 EXPECT_EQ("FOO(String-ized&Messy+But: :Still=Intentional);", 22243 format("FOO(String-ized&Messy+But: :Still=Intentional);", Style)); 22244 } 22245 22246 TEST_F(FormatTest, VeryLongNamespaceCommentSplit) { 22247 // These tests are not in NamespaceFixer because that doesn't 22248 // test its interaction with line wrapping 22249 FormatStyle Style = getLLVMStyle(); 22250 Style.ColumnLimit = 80; 22251 verifyFormat("namespace {\n" 22252 "int i;\n" 22253 "int j;\n" 22254 "} // namespace", 22255 Style); 22256 22257 verifyFormat("namespace AAA {\n" 22258 "int i;\n" 22259 "int j;\n" 22260 "} // namespace AAA", 22261 Style); 22262 22263 EXPECT_EQ("namespace Averyveryveryverylongnamespace {\n" 22264 "int i;\n" 22265 "int j;\n" 22266 "} // namespace Averyveryveryverylongnamespace", 22267 format("namespace Averyveryveryverylongnamespace {\n" 22268 "int i;\n" 22269 "int j;\n" 22270 "}", 22271 Style)); 22272 22273 EXPECT_EQ( 22274 "namespace " 22275 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" 22276 " went::mad::now {\n" 22277 "int i;\n" 22278 "int j;\n" 22279 "} // namespace\n" 22280 " // " 22281 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" 22282 "went::mad::now", 22283 format("namespace " 22284 "would::it::save::you::a::lot::of::time::if_::i::" 22285 "just::gave::up::and_::went::mad::now {\n" 22286 "int i;\n" 22287 "int j;\n" 22288 "}", 22289 Style)); 22290 22291 // This used to duplicate the comment again and again on subsequent runs 22292 EXPECT_EQ( 22293 "namespace " 22294 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" 22295 " went::mad::now {\n" 22296 "int i;\n" 22297 "int j;\n" 22298 "} // namespace\n" 22299 " // " 22300 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" 22301 "went::mad::now", 22302 format("namespace " 22303 "would::it::save::you::a::lot::of::time::if_::i::" 22304 "just::gave::up::and_::went::mad::now {\n" 22305 "int i;\n" 22306 "int j;\n" 22307 "} // namespace\n" 22308 " // " 22309 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::" 22310 "and_::went::mad::now", 22311 Style)); 22312 } 22313 22314 TEST_F(FormatTest, LikelyUnlikely) { 22315 FormatStyle Style = getLLVMStyle(); 22316 22317 verifyFormat("if (argc > 5) [[unlikely]] {\n" 22318 " return 29;\n" 22319 "}", 22320 Style); 22321 22322 verifyFormat("if (argc > 5) [[likely]] {\n" 22323 " return 29;\n" 22324 "}", 22325 Style); 22326 22327 verifyFormat("if (argc > 5) [[unlikely]] {\n" 22328 " return 29;\n" 22329 "} else [[likely]] {\n" 22330 " return 42;\n" 22331 "}\n", 22332 Style); 22333 22334 verifyFormat("if (argc > 5) [[unlikely]] {\n" 22335 " return 29;\n" 22336 "} else if (argc > 10) [[likely]] {\n" 22337 " return 99;\n" 22338 "} else {\n" 22339 " return 42;\n" 22340 "}\n", 22341 Style); 22342 22343 verifyFormat("if (argc > 5) [[gnu::unused]] {\n" 22344 " return 29;\n" 22345 "}", 22346 Style); 22347 22348 verifyFormat("if (argc > 5) [[unlikely]]\n" 22349 " return 29;\n", 22350 Style); 22351 verifyFormat("if (argc > 5) [[likely]]\n" 22352 " return 29;\n", 22353 Style); 22354 22355 Style.AttributeMacros.push_back("UNLIKELY"); 22356 Style.AttributeMacros.push_back("LIKELY"); 22357 verifyFormat("if (argc > 5) UNLIKELY\n" 22358 " return 29;\n", 22359 Style); 22360 22361 verifyFormat("if (argc > 5) UNLIKELY {\n" 22362 " return 29;\n" 22363 "}", 22364 Style); 22365 verifyFormat("if (argc > 5) UNLIKELY {\n" 22366 " return 29;\n" 22367 "} else [[likely]] {\n" 22368 " return 42;\n" 22369 "}\n", 22370 Style); 22371 verifyFormat("if (argc > 5) UNLIKELY {\n" 22372 " return 29;\n" 22373 "} else LIKELY {\n" 22374 " return 42;\n" 22375 "}\n", 22376 Style); 22377 verifyFormat("if (argc > 5) [[unlikely]] {\n" 22378 " return 29;\n" 22379 "} else LIKELY {\n" 22380 " return 42;\n" 22381 "}\n", 22382 Style); 22383 } 22384 22385 TEST_F(FormatTest, PenaltyIndentedWhitespace) { 22386 verifyFormat("Constructor()\n" 22387 " : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 22388 " aaaa(aaaaaaaaaaaaaaaaaa, " 22389 "aaaaaaaaaaaaaaaaaat))"); 22390 verifyFormat("Constructor()\n" 22391 " : aaaaaaaaaaaaa(aaaaaa), " 22392 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)"); 22393 22394 FormatStyle StyleWithWhitespacePenalty = getLLVMStyle(); 22395 StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5; 22396 verifyFormat("Constructor()\n" 22397 " : aaaaaa(aaaaaa),\n" 22398 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 22399 " aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))", 22400 StyleWithWhitespacePenalty); 22401 verifyFormat("Constructor()\n" 22402 " : aaaaaaaaaaaaa(aaaaaa), " 22403 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)", 22404 StyleWithWhitespacePenalty); 22405 } 22406 22407 TEST_F(FormatTest, LLVMDefaultStyle) { 22408 FormatStyle Style = getLLVMStyle(); 22409 verifyFormat("extern \"C\" {\n" 22410 "int foo();\n" 22411 "}", 22412 Style); 22413 } 22414 TEST_F(FormatTest, GNUDefaultStyle) { 22415 FormatStyle Style = getGNUStyle(); 22416 verifyFormat("extern \"C\"\n" 22417 "{\n" 22418 " int foo ();\n" 22419 "}", 22420 Style); 22421 } 22422 TEST_F(FormatTest, MozillaDefaultStyle) { 22423 FormatStyle Style = getMozillaStyle(); 22424 verifyFormat("extern \"C\"\n" 22425 "{\n" 22426 " int foo();\n" 22427 "}", 22428 Style); 22429 } 22430 TEST_F(FormatTest, GoogleDefaultStyle) { 22431 FormatStyle Style = getGoogleStyle(); 22432 verifyFormat("extern \"C\" {\n" 22433 "int foo();\n" 22434 "}", 22435 Style); 22436 } 22437 TEST_F(FormatTest, ChromiumDefaultStyle) { 22438 FormatStyle Style = getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp); 22439 verifyFormat("extern \"C\" {\n" 22440 "int foo();\n" 22441 "}", 22442 Style); 22443 } 22444 TEST_F(FormatTest, MicrosoftDefaultStyle) { 22445 FormatStyle Style = getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp); 22446 verifyFormat("extern \"C\"\n" 22447 "{\n" 22448 " int foo();\n" 22449 "}", 22450 Style); 22451 } 22452 TEST_F(FormatTest, WebKitDefaultStyle) { 22453 FormatStyle Style = getWebKitStyle(); 22454 verifyFormat("extern \"C\" {\n" 22455 "int foo();\n" 22456 "}", 22457 Style); 22458 } 22459 22460 TEST_F(FormatTest, ConceptsAndRequires) { 22461 FormatStyle Style = getLLVMStyle(); 22462 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 22463 22464 verifyFormat("template <typename T>\n" 22465 "concept Hashable = requires(T a) {\n" 22466 " { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n" 22467 "};", 22468 Style); 22469 verifyFormat("template <typename T>\n" 22470 "concept EqualityComparable = requires(T a, T b) {\n" 22471 " { a == b } -> bool;\n" 22472 "};", 22473 Style); 22474 verifyFormat("template <typename T>\n" 22475 "concept EqualityComparable = requires(T a, T b) {\n" 22476 " { a == b } -> bool;\n" 22477 " { a != b } -> bool;\n" 22478 "};", 22479 Style); 22480 verifyFormat("template <typename T>\n" 22481 "concept EqualityComparable = requires(T a, T b) {\n" 22482 " { a == b } -> bool;\n" 22483 " { a != b } -> bool;\n" 22484 "};", 22485 Style); 22486 22487 verifyFormat("template <typename It>\n" 22488 "requires Iterator<It>\n" 22489 "void sort(It begin, It end) {\n" 22490 " //....\n" 22491 "}", 22492 Style); 22493 22494 verifyFormat("template <typename T>\n" 22495 "concept Large = sizeof(T) > 10;", 22496 Style); 22497 22498 verifyFormat("template <typename T, typename U>\n" 22499 "concept FooableWith = requires(T t, U u) {\n" 22500 " typename T::foo_type;\n" 22501 " { t.foo(u) } -> typename T::foo_type;\n" 22502 " t++;\n" 22503 "};\n" 22504 "void doFoo(FooableWith<int> auto t) {\n" 22505 " t.foo(3);\n" 22506 "}", 22507 Style); 22508 verifyFormat("template <typename T>\n" 22509 "concept Context = sizeof(T) == 1;", 22510 Style); 22511 verifyFormat("template <typename T>\n" 22512 "concept Context = is_specialization_of_v<context, T>;", 22513 Style); 22514 verifyFormat("template <typename T>\n" 22515 "concept Node = std::is_object_v<T>;", 22516 Style); 22517 verifyFormat("template <typename T>\n" 22518 "concept Tree = true;", 22519 Style); 22520 22521 verifyFormat("template <typename T> int g(T i) requires Concept1<I> {\n" 22522 " //...\n" 22523 "}", 22524 Style); 22525 22526 verifyFormat( 22527 "template <typename T> int g(T i) requires Concept1<I> && Concept2<I> {\n" 22528 " //...\n" 22529 "}", 22530 Style); 22531 22532 verifyFormat( 22533 "template <typename T> int g(T i) requires Concept1<I> || Concept2<I> {\n" 22534 " //...\n" 22535 "}", 22536 Style); 22537 22538 verifyFormat("template <typename T>\n" 22539 "veryveryvery_long_return_type g(T i) requires Concept1<I> || " 22540 "Concept2<I> {\n" 22541 " //...\n" 22542 "}", 22543 Style); 22544 22545 verifyFormat("template <typename T>\n" 22546 "veryveryvery_long_return_type g(T i) requires Concept1<I> && " 22547 "Concept2<I> {\n" 22548 " //...\n" 22549 "}", 22550 Style); 22551 22552 verifyFormat( 22553 "template <typename T>\n" 22554 "veryveryvery_long_return_type g(T i) requires Concept1 && Concept2 {\n" 22555 " //...\n" 22556 "}", 22557 Style); 22558 22559 verifyFormat( 22560 "template <typename T>\n" 22561 "veryveryvery_long_return_type g(T i) requires Concept1 || Concept2 {\n" 22562 " //...\n" 22563 "}", 22564 Style); 22565 22566 verifyFormat("template <typename It>\n" 22567 "requires Foo<It>() && Bar<It> {\n" 22568 " //....\n" 22569 "}", 22570 Style); 22571 22572 verifyFormat("template <typename It>\n" 22573 "requires Foo<Bar<It>>() && Bar<Foo<It, It>> {\n" 22574 " //....\n" 22575 "}", 22576 Style); 22577 22578 verifyFormat("template <typename It>\n" 22579 "requires Foo<Bar<It, It>>() && Bar<Foo<It, It>> {\n" 22580 " //....\n" 22581 "}", 22582 Style); 22583 22584 verifyFormat( 22585 "template <typename It>\n" 22586 "requires Foo<Bar<It>, Baz<It>>() && Bar<Foo<It>, Baz<It, It>> {\n" 22587 " //....\n" 22588 "}", 22589 Style); 22590 22591 Style.IndentRequires = true; 22592 verifyFormat("template <typename It>\n" 22593 " requires Iterator<It>\n" 22594 "void sort(It begin, It end) {\n" 22595 " //....\n" 22596 "}", 22597 Style); 22598 verifyFormat("template <std::size index_>\n" 22599 " requires(index_ < sizeof...(Children_))\n" 22600 "Tree auto &child() {\n" 22601 " // ...\n" 22602 "}", 22603 Style); 22604 22605 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 22606 verifyFormat("template <typename T>\n" 22607 "concept Hashable = requires (T a) {\n" 22608 " { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n" 22609 "};", 22610 Style); 22611 22612 verifyFormat("template <class T = void>\n" 22613 " requires EqualityComparable<T> || Same<T, void>\n" 22614 "struct equal_to;", 22615 Style); 22616 22617 verifyFormat("template <class T>\n" 22618 " requires requires {\n" 22619 " T{};\n" 22620 " T (int);\n" 22621 " }\n", 22622 Style); 22623 22624 Style.ColumnLimit = 78; 22625 verifyFormat("template <typename T>\n" 22626 "concept Context = Traits<typename T::traits_type> and\n" 22627 " Interface<typename T::interface_type> and\n" 22628 " Request<typename T::request_type> and\n" 22629 " Response<typename T::response_type> and\n" 22630 " ContextExtension<typename T::extension_type> and\n" 22631 " ::std::is_copy_constructable<T> and " 22632 "::std::is_move_constructable<T> and\n" 22633 " requires (T c) {\n" 22634 " { c.response; } -> Response;\n" 22635 "} and requires (T c) {\n" 22636 " { c.request; } -> Request;\n" 22637 "}\n", 22638 Style); 22639 22640 verifyFormat("template <typename T>\n" 22641 "concept Context = Traits<typename T::traits_type> or\n" 22642 " Interface<typename T::interface_type> or\n" 22643 " Request<typename T::request_type> or\n" 22644 " Response<typename T::response_type> or\n" 22645 " ContextExtension<typename T::extension_type> or\n" 22646 " ::std::is_copy_constructable<T> or " 22647 "::std::is_move_constructable<T> or\n" 22648 " requires (T c) {\n" 22649 " { c.response; } -> Response;\n" 22650 "} or requires (T c) {\n" 22651 " { c.request; } -> Request;\n" 22652 "}\n", 22653 Style); 22654 22655 verifyFormat("template <typename T>\n" 22656 "concept Context = Traits<typename T::traits_type> &&\n" 22657 " Interface<typename T::interface_type> &&\n" 22658 " Request<typename T::request_type> &&\n" 22659 " Response<typename T::response_type> &&\n" 22660 " ContextExtension<typename T::extension_type> &&\n" 22661 " ::std::is_copy_constructable<T> && " 22662 "::std::is_move_constructable<T> &&\n" 22663 " requires (T c) {\n" 22664 " { c.response; } -> Response;\n" 22665 "} && requires (T c) {\n" 22666 " { c.request; } -> Request;\n" 22667 "}\n", 22668 Style); 22669 22670 verifyFormat("template <typename T>\nconcept someConcept = Constraint1<T> && " 22671 "Constraint2<T>;"); 22672 22673 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 22674 Style.BraceWrapping.AfterFunction = true; 22675 Style.BraceWrapping.AfterClass = true; 22676 Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 22677 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 22678 verifyFormat("void Foo () requires (std::copyable<T>)\n" 22679 "{\n" 22680 " return\n" 22681 "}\n", 22682 Style); 22683 22684 verifyFormat("void Foo () requires std::copyable<T>\n" 22685 "{\n" 22686 " return\n" 22687 "}\n", 22688 Style); 22689 22690 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 22691 " requires (std::invocable<F, std::invoke_result_t<Args>...>)\n" 22692 "struct constant;", 22693 Style); 22694 22695 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 22696 " requires std::invocable<F, std::invoke_result_t<Args>...>\n" 22697 "struct constant;", 22698 Style); 22699 22700 verifyFormat("template <class T>\n" 22701 "class plane_with_very_very_very_long_name\n" 22702 "{\n" 22703 " constexpr plane_with_very_very_very_long_name () requires " 22704 "std::copyable<T>\n" 22705 " : plane_with_very_very_very_long_name (1)\n" 22706 " {\n" 22707 " }\n" 22708 "}\n", 22709 Style); 22710 22711 verifyFormat("template <class T>\n" 22712 "class plane_with_long_name\n" 22713 "{\n" 22714 " constexpr plane_with_long_name () requires std::copyable<T>\n" 22715 " : plane_with_long_name (1)\n" 22716 " {\n" 22717 " }\n" 22718 "}\n", 22719 Style); 22720 22721 Style.BreakBeforeConceptDeclarations = false; 22722 verifyFormat("template <typename T> concept Tree = true;", Style); 22723 22724 Style.IndentRequires = false; 22725 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 22726 "requires (std::invocable<F, std::invoke_result_t<Args>...>) " 22727 "struct constant;", 22728 Style); 22729 } 22730 22731 TEST_F(FormatTest, StatementAttributeLikeMacros) { 22732 FormatStyle Style = getLLVMStyle(); 22733 StringRef Source = "void Foo::slot() {\n" 22734 " unsigned char MyChar = 'x';\n" 22735 " emit signal(MyChar);\n" 22736 " Q_EMIT signal(MyChar);\n" 22737 "}"; 22738 22739 EXPECT_EQ(Source, format(Source, Style)); 22740 22741 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 22742 EXPECT_EQ("void Foo::slot() {\n" 22743 " unsigned char MyChar = 'x';\n" 22744 " emit signal(MyChar);\n" 22745 " Q_EMIT signal(MyChar);\n" 22746 "}", 22747 format(Source, Style)); 22748 22749 Style.StatementAttributeLikeMacros.push_back("emit"); 22750 EXPECT_EQ(Source, format(Source, Style)); 22751 22752 Style.StatementAttributeLikeMacros = {}; 22753 EXPECT_EQ("void Foo::slot() {\n" 22754 " unsigned char MyChar = 'x';\n" 22755 " emit signal(MyChar);\n" 22756 " Q_EMIT signal(MyChar);\n" 22757 "}", 22758 format(Source, Style)); 22759 } 22760 22761 TEST_F(FormatTest, IndentAccessModifiers) { 22762 FormatStyle Style = getLLVMStyle(); 22763 Style.IndentAccessModifiers = true; 22764 // Members are *two* levels below the record; 22765 // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation. 22766 verifyFormat("class C {\n" 22767 " int i;\n" 22768 "};\n", 22769 Style); 22770 verifyFormat("union C {\n" 22771 " int i;\n" 22772 " unsigned u;\n" 22773 "};\n", 22774 Style); 22775 // Access modifiers should be indented one level below the record. 22776 verifyFormat("class C {\n" 22777 " public:\n" 22778 " int i;\n" 22779 "};\n", 22780 Style); 22781 verifyFormat("struct S {\n" 22782 " private:\n" 22783 " class C {\n" 22784 " int j;\n" 22785 "\n" 22786 " public:\n" 22787 " C();\n" 22788 " };\n" 22789 "\n" 22790 " public:\n" 22791 " int i;\n" 22792 "};\n", 22793 Style); 22794 // Enumerations are not records and should be unaffected. 22795 Style.AllowShortEnumsOnASingleLine = false; 22796 verifyFormat("enum class E {\n" 22797 " A,\n" 22798 " B\n" 22799 "};\n", 22800 Style); 22801 // Test with a different indentation width; 22802 // also proves that the result is Style.AccessModifierOffset agnostic. 22803 Style.IndentWidth = 3; 22804 verifyFormat("class C {\n" 22805 " public:\n" 22806 " int i;\n" 22807 "};\n", 22808 Style); 22809 } 22810 22811 TEST_F(FormatTest, LimitlessStringsAndComments) { 22812 auto Style = getLLVMStyleWithColumns(0); 22813 constexpr StringRef Code = 22814 "/**\n" 22815 " * This is a multiline comment with quite some long lines, at least for " 22816 "the LLVM Style.\n" 22817 " * We will redo this with strings and line comments. Just to check if " 22818 "everything is working.\n" 22819 " */\n" 22820 "bool foo() {\n" 22821 " /* Single line multi line comment. */\n" 22822 " const std::string String = \"This is a multiline string with quite " 22823 "some long lines, at least for the LLVM Style.\"\n" 22824 " \"We already did it with multi line " 22825 "comments, and we will do it with line comments. Just to check if " 22826 "everything is working.\";\n" 22827 " // This is a line comment (block) with quite some long lines, at " 22828 "least for the LLVM Style.\n" 22829 " // We already did this with multi line comments and strings. Just to " 22830 "check if everything is working.\n" 22831 " const std::string SmallString = \"Hello World\";\n" 22832 " // Small line comment\n" 22833 " return String.size() > SmallString.size();\n" 22834 "}"; 22835 EXPECT_EQ(Code, format(Code, Style)); 22836 } 22837 22838 TEST_F(FormatTest, FormatDecayCopy) { 22839 // error cases from unit tests 22840 verifyFormat("foo(auto())"); 22841 verifyFormat("foo(auto{})"); 22842 verifyFormat("foo(auto({}))"); 22843 verifyFormat("foo(auto{{}})"); 22844 22845 verifyFormat("foo(auto(1))"); 22846 verifyFormat("foo(auto{1})"); 22847 verifyFormat("foo(new auto(1))"); 22848 verifyFormat("foo(new auto{1})"); 22849 verifyFormat("decltype(auto(1)) x;"); 22850 verifyFormat("decltype(auto{1}) x;"); 22851 verifyFormat("auto(x);"); 22852 verifyFormat("auto{x};"); 22853 verifyFormat("new auto{x};"); 22854 verifyFormat("auto{x} = y;"); 22855 verifyFormat("auto(x) = y;"); // actually a declaration, but this is clearly 22856 // the user's own fault 22857 verifyFormat("integral auto(x) = y;"); // actually a declaration, but this is 22858 // clearly the user's own fault 22859 verifyFormat("auto(*p)() = f;"); // actually a declaration; TODO FIXME 22860 } 22861 22862 TEST_F(FormatTest, Cpp20ModulesSupport) { 22863 FormatStyle Style = getLLVMStyle(); 22864 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 22865 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 22866 22867 verifyFormat("export import foo;", Style); 22868 verifyFormat("export import foo:bar;", Style); 22869 verifyFormat("export import foo.bar;", Style); 22870 verifyFormat("export import foo.bar:baz;", Style); 22871 verifyFormat("export import :bar;", Style); 22872 verifyFormat("export module foo:bar;", Style); 22873 verifyFormat("export module foo;", Style); 22874 verifyFormat("export module foo.bar;", Style); 22875 verifyFormat("export module foo.bar:baz;", Style); 22876 verifyFormat("export import <string_view>;", Style); 22877 22878 verifyFormat("export type_name var;", Style); 22879 verifyFormat("template <class T> export using A = B<T>;", Style); 22880 verifyFormat("export using A = B;", Style); 22881 verifyFormat("export int func() {\n" 22882 " foo();\n" 22883 "}", 22884 Style); 22885 verifyFormat("export struct {\n" 22886 " int foo;\n" 22887 "};", 22888 Style); 22889 verifyFormat("export {\n" 22890 " int foo;\n" 22891 "};", 22892 Style); 22893 verifyFormat("export export char const *hello() { return \"hello\"; }"); 22894 22895 verifyFormat("import bar;", Style); 22896 verifyFormat("import foo.bar;", Style); 22897 verifyFormat("import foo:bar;", Style); 22898 verifyFormat("import :bar;", Style); 22899 verifyFormat("import <ctime>;", Style); 22900 verifyFormat("import \"header\";", Style); 22901 22902 verifyFormat("module foo;", Style); 22903 verifyFormat("module foo:bar;", Style); 22904 verifyFormat("module foo.bar;", Style); 22905 verifyFormat("module;", Style); 22906 22907 verifyFormat("export namespace hi {\n" 22908 "const char *sayhi();\n" 22909 "}", 22910 Style); 22911 22912 verifyFormat("module :private;", Style); 22913 verifyFormat("import <foo/bar.h>;", Style); 22914 verifyFormat("import foo...bar;", Style); 22915 verifyFormat("import ..........;", Style); 22916 verifyFormat("module foo:private;", Style); 22917 verifyFormat("import a", Style); 22918 verifyFormat("module a", Style); 22919 verifyFormat("export import a", Style); 22920 verifyFormat("export module a", Style); 22921 22922 verifyFormat("import", Style); 22923 verifyFormat("module", Style); 22924 verifyFormat("export", Style); 22925 } 22926 22927 TEST_F(FormatTest, CoroutineForCoawait) { 22928 FormatStyle Style = getLLVMStyle(); 22929 verifyFormat("for co_await (auto x : range())\n ;"); 22930 verifyFormat("for (auto i : arr) {\n" 22931 "}", 22932 Style); 22933 verifyFormat("for co_await (auto i : arr) {\n" 22934 "}", 22935 Style); 22936 verifyFormat("for co_await (auto i : foo(T{})) {\n" 22937 "}", 22938 Style); 22939 } 22940 22941 TEST_F(FormatTest, CoroutineCoAwait) { 22942 verifyFormat("int x = co_await foo();"); 22943 verifyFormat("int x = (co_await foo());"); 22944 verifyFormat("co_await (42);"); 22945 verifyFormat("void operator co_await(int);"); 22946 verifyFormat("void operator co_await(a);"); 22947 verifyFormat("co_await a;"); 22948 verifyFormat("co_await missing_await_resume{};"); 22949 verifyFormat("co_await a; // comment"); 22950 verifyFormat("void test0() { co_await a; }"); 22951 verifyFormat("co_await co_await co_await foo();"); 22952 verifyFormat("co_await foo().bar();"); 22953 verifyFormat("co_await [this]() -> Task { co_return x; }"); 22954 verifyFormat("co_await [this](int a, int b) -> Task { co_return co_await " 22955 "foo(); }(x, y);"); 22956 22957 FormatStyle Style = getLLVMStyle(); 22958 Style.ColumnLimit = 40; 22959 verifyFormat("co_await [this](int a, int b) -> Task {\n" 22960 " co_return co_await foo();\n" 22961 "}(x, y);", 22962 Style); 22963 verifyFormat("co_await;"); 22964 } 22965 22966 TEST_F(FormatTest, CoroutineCoYield) { 22967 verifyFormat("int x = co_yield foo();"); 22968 verifyFormat("int x = (co_yield foo());"); 22969 verifyFormat("co_yield (42);"); 22970 verifyFormat("co_yield {42};"); 22971 verifyFormat("co_yield 42;"); 22972 verifyFormat("co_yield n++;"); 22973 verifyFormat("co_yield ++n;"); 22974 verifyFormat("co_yield;"); 22975 } 22976 22977 TEST_F(FormatTest, CoroutineCoReturn) { 22978 verifyFormat("co_return (42);"); 22979 verifyFormat("co_return;"); 22980 verifyFormat("co_return {};"); 22981 verifyFormat("co_return x;"); 22982 verifyFormat("co_return co_await foo();"); 22983 verifyFormat("co_return co_yield foo();"); 22984 } 22985 22986 TEST_F(FormatTest, EmptyShortBlock) { 22987 auto Style = getLLVMStyle(); 22988 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 22989 22990 verifyFormat("try {\n" 22991 " doA();\n" 22992 "} catch (Exception &e) {\n" 22993 " e.printStackTrace();\n" 22994 "}\n", 22995 Style); 22996 22997 verifyFormat("try {\n" 22998 " doA();\n" 22999 "} catch (Exception &e) {}\n", 23000 Style); 23001 } 23002 23003 } // namespace 23004 } // namespace format 23005 } // namespace clang 23006