1 //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "clang/Format/Format.h" 10 11 #include "../Tooling/ReplacementTest.h" 12 #include "FormatTestUtils.h" 13 14 #include "llvm/Support/Debug.h" 15 #include "llvm/Support/MemoryBuffer.h" 16 #include "gtest/gtest.h" 17 18 #define DEBUG_TYPE "format-test" 19 20 using clang::tooling::ReplacementTest; 21 using clang::tooling::toReplacements; 22 using testing::ScopedTrace; 23 24 namespace clang { 25 namespace format { 26 namespace { 27 28 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); } 29 30 class FormatTest : public ::testing::Test { 31 protected: 32 enum StatusCheck { SC_ExpectComplete, SC_ExpectIncomplete, SC_DoNotCheck }; 33 34 std::string format(llvm::StringRef Code, 35 const FormatStyle &Style = getLLVMStyle(), 36 StatusCheck CheckComplete = SC_ExpectComplete) { 37 LLVM_DEBUG(llvm::errs() << "---\n"); 38 LLVM_DEBUG(llvm::errs() << Code << "\n\n"); 39 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size())); 40 FormattingAttemptStatus Status; 41 tooling::Replacements Replaces = 42 reformat(Style, Code, Ranges, "<stdin>", &Status); 43 if (CheckComplete != SC_DoNotCheck) { 44 bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete; 45 EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete) 46 << Code << "\n\n"; 47 } 48 ReplacementCount = Replaces.size(); 49 auto Result = applyAllReplacements(Code, Replaces); 50 EXPECT_TRUE(static_cast<bool>(Result)); 51 LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n"); 52 return *Result; 53 } 54 55 FormatStyle getStyleWithColumns(FormatStyle Style, unsigned ColumnLimit) { 56 Style.ColumnLimit = ColumnLimit; 57 return Style; 58 } 59 60 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { 61 return getStyleWithColumns(getLLVMStyle(), ColumnLimit); 62 } 63 64 FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) { 65 return getStyleWithColumns(getGoogleStyle(), ColumnLimit); 66 } 67 68 void _verifyFormat(const char *File, int Line, llvm::StringRef Expected, 69 llvm::StringRef Code, 70 const FormatStyle &Style = getLLVMStyle()) { 71 ScopedTrace t(File, Line, ::testing::Message() << Code.str()); 72 EXPECT_EQ(Expected.str(), format(Expected, Style)) 73 << "Expected code is not stable"; 74 EXPECT_EQ(Expected.str(), format(Code, Style)); 75 if (Style.Language == FormatStyle::LK_Cpp) { 76 // Objective-C++ is a superset of C++, so everything checked for C++ 77 // needs to be checked for Objective-C++ as well. 78 FormatStyle ObjCStyle = Style; 79 ObjCStyle.Language = FormatStyle::LK_ObjC; 80 EXPECT_EQ(Expected.str(), format(test::messUp(Code), ObjCStyle)); 81 } 82 } 83 84 void _verifyFormat(const char *File, int Line, llvm::StringRef Code, 85 const FormatStyle &Style = getLLVMStyle()) { 86 _verifyFormat(File, Line, Code, test::messUp(Code), Style); 87 } 88 89 void _verifyIncompleteFormat(const char *File, int Line, llvm::StringRef Code, 90 const FormatStyle &Style = getLLVMStyle()) { 91 ScopedTrace t(File, Line, ::testing::Message() << Code.str()); 92 EXPECT_EQ(Code.str(), 93 format(test::messUp(Code), Style, SC_ExpectIncomplete)); 94 } 95 96 void _verifyIndependentOfContext(const char *File, int Line, 97 llvm::StringRef Text, 98 const FormatStyle &Style = getLLVMStyle()) { 99 _verifyFormat(File, Line, Text, Style); 100 _verifyFormat(File, Line, llvm::Twine("void f() { " + Text + " }").str(), 101 Style); 102 } 103 104 /// \brief Verify that clang-format does not crash on the given input. 105 void verifyNoCrash(llvm::StringRef Code, 106 const FormatStyle &Style = getLLVMStyle()) { 107 format(Code, Style, SC_DoNotCheck); 108 } 109 110 int ReplacementCount; 111 }; 112 113 #define verifyIndependentOfContext(...) \ 114 _verifyIndependentOfContext(__FILE__, __LINE__, __VA_ARGS__) 115 #define verifyIncompleteFormat(...) \ 116 _verifyIncompleteFormat(__FILE__, __LINE__, __VA_ARGS__) 117 #define verifyFormat(...) _verifyFormat(__FILE__, __LINE__, __VA_ARGS__) 118 #define verifyGoogleFormat(Code) verifyFormat(Code, getGoogleStyle()) 119 120 TEST_F(FormatTest, MessUp) { 121 EXPECT_EQ("1 2 3", test::messUp("1 2 3")); 122 EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n")); 123 EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc")); 124 EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc")); 125 EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne")); 126 } 127 128 TEST_F(FormatTest, DefaultLLVMStyleIsCpp) { 129 EXPECT_EQ(FormatStyle::LK_Cpp, getLLVMStyle().Language); 130 } 131 132 TEST_F(FormatTest, LLVMStyleOverride) { 133 EXPECT_EQ(FormatStyle::LK_Proto, 134 getLLVMStyle(FormatStyle::LK_Proto).Language); 135 } 136 137 //===----------------------------------------------------------------------===// 138 // Basic function tests. 139 //===----------------------------------------------------------------------===// 140 141 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) { 142 EXPECT_EQ(";", format(";")); 143 } 144 145 TEST_F(FormatTest, FormatsGlobalStatementsAt0) { 146 EXPECT_EQ("int i;", format(" int i;")); 147 EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;")); 148 EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); 149 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); 150 } 151 152 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { 153 EXPECT_EQ("int i;", format("int\ni;")); 154 } 155 156 TEST_F(FormatTest, FormatsNestedBlockStatements) { 157 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}")); 158 } 159 160 TEST_F(FormatTest, FormatsNestedCall) { 161 verifyFormat("Method(f1, f2(f3));"); 162 verifyFormat("Method(f1(f2, f3()));"); 163 verifyFormat("Method(f1(f2, (f3())));"); 164 } 165 166 TEST_F(FormatTest, NestedNameSpecifiers) { 167 verifyFormat("vector<::Type> v;"); 168 verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())"); 169 verifyFormat("static constexpr bool Bar = decltype(bar())::value;"); 170 verifyFormat("static constexpr bool Bar = typeof(bar())::value;"); 171 verifyFormat("static constexpr bool Bar = __underlying_type(bar())::value;"); 172 verifyFormat("static constexpr bool Bar = _Atomic(bar())::value;"); 173 verifyFormat("bool a = 2 < ::SomeFunction();"); 174 verifyFormat("ALWAYS_INLINE ::std::string getName();"); 175 verifyFormat("some::string getName();"); 176 } 177 178 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) { 179 EXPECT_EQ("if (a) {\n" 180 " f();\n" 181 "}", 182 format("if(a){f();}")); 183 EXPECT_EQ(4, ReplacementCount); 184 EXPECT_EQ("if (a) {\n" 185 " f();\n" 186 "}", 187 format("if (a) {\n" 188 " f();\n" 189 "}")); 190 EXPECT_EQ(0, ReplacementCount); 191 EXPECT_EQ("/*\r\n" 192 "\r\n" 193 "*/\r\n", 194 format("/*\r\n" 195 "\r\n" 196 "*/\r\n")); 197 EXPECT_EQ(0, ReplacementCount); 198 } 199 200 TEST_F(FormatTest, RemovesEmptyLines) { 201 EXPECT_EQ("class C {\n" 202 " int i;\n" 203 "};", 204 format("class C {\n" 205 " int i;\n" 206 "\n" 207 "};")); 208 209 // Don't remove empty lines at the start of namespaces or extern "C" blocks. 210 EXPECT_EQ("namespace N {\n" 211 "\n" 212 "int i;\n" 213 "}", 214 format("namespace N {\n" 215 "\n" 216 "int i;\n" 217 "}", 218 getGoogleStyle())); 219 EXPECT_EQ("/* something */ namespace N {\n" 220 "\n" 221 "int i;\n" 222 "}", 223 format("/* something */ namespace N {\n" 224 "\n" 225 "int i;\n" 226 "}", 227 getGoogleStyle())); 228 EXPECT_EQ("inline namespace N {\n" 229 "\n" 230 "int i;\n" 231 "}", 232 format("inline namespace N {\n" 233 "\n" 234 "int i;\n" 235 "}", 236 getGoogleStyle())); 237 EXPECT_EQ("/* something */ inline namespace N {\n" 238 "\n" 239 "int i;\n" 240 "}", 241 format("/* something */ inline namespace N {\n" 242 "\n" 243 "int i;\n" 244 "}", 245 getGoogleStyle())); 246 EXPECT_EQ("export namespace N {\n" 247 "\n" 248 "int i;\n" 249 "}", 250 format("export namespace N {\n" 251 "\n" 252 "int i;\n" 253 "}", 254 getGoogleStyle())); 255 EXPECT_EQ("extern /**/ \"C\" /**/ {\n" 256 "\n" 257 "int i;\n" 258 "}", 259 format("extern /**/ \"C\" /**/ {\n" 260 "\n" 261 "int i;\n" 262 "}", 263 getGoogleStyle())); 264 265 auto CustomStyle = clang::format::getLLVMStyle(); 266 CustomStyle.BreakBeforeBraces = clang::format::FormatStyle::BS_Custom; 267 CustomStyle.BraceWrapping.AfterNamespace = true; 268 CustomStyle.KeepEmptyLinesAtTheStartOfBlocks = false; 269 EXPECT_EQ("namespace N\n" 270 "{\n" 271 "\n" 272 "int i;\n" 273 "}", 274 format("namespace N\n" 275 "{\n" 276 "\n" 277 "\n" 278 "int i;\n" 279 "}", 280 CustomStyle)); 281 EXPECT_EQ("/* something */ namespace N\n" 282 "{\n" 283 "\n" 284 "int i;\n" 285 "}", 286 format("/* something */ namespace N {\n" 287 "\n" 288 "\n" 289 "int i;\n" 290 "}", 291 CustomStyle)); 292 EXPECT_EQ("inline namespace N\n" 293 "{\n" 294 "\n" 295 "int i;\n" 296 "}", 297 format("inline namespace N\n" 298 "{\n" 299 "\n" 300 "\n" 301 "int i;\n" 302 "}", 303 CustomStyle)); 304 EXPECT_EQ("/* something */ inline namespace N\n" 305 "{\n" 306 "\n" 307 "int i;\n" 308 "}", 309 format("/* something */ inline namespace N\n" 310 "{\n" 311 "\n" 312 "int i;\n" 313 "}", 314 CustomStyle)); 315 EXPECT_EQ("export namespace N\n" 316 "{\n" 317 "\n" 318 "int i;\n" 319 "}", 320 format("export namespace N\n" 321 "{\n" 322 "\n" 323 "int i;\n" 324 "}", 325 CustomStyle)); 326 EXPECT_EQ("namespace a\n" 327 "{\n" 328 "namespace b\n" 329 "{\n" 330 "\n" 331 "class AA {};\n" 332 "\n" 333 "} // namespace b\n" 334 "} // namespace a\n", 335 format("namespace a\n" 336 "{\n" 337 "namespace b\n" 338 "{\n" 339 "\n" 340 "\n" 341 "class AA {};\n" 342 "\n" 343 "\n" 344 "}\n" 345 "}\n", 346 CustomStyle)); 347 EXPECT_EQ("namespace A /* comment */\n" 348 "{\n" 349 "class B {}\n" 350 "} // namespace A", 351 format("namespace A /* comment */ { class B {} }", CustomStyle)); 352 EXPECT_EQ("namespace A\n" 353 "{ /* comment */\n" 354 "class B {}\n" 355 "} // namespace A", 356 format("namespace A {/* comment */ class B {} }", CustomStyle)); 357 EXPECT_EQ("namespace A\n" 358 "{ /* comment */\n" 359 "\n" 360 "class B {}\n" 361 "\n" 362 "" 363 "} // namespace A", 364 format("namespace A { /* comment */\n" 365 "\n" 366 "\n" 367 "class B {}\n" 368 "\n" 369 "\n" 370 "}", 371 CustomStyle)); 372 EXPECT_EQ("namespace A /* comment */\n" 373 "{\n" 374 "\n" 375 "class B {}\n" 376 "\n" 377 "} // namespace A", 378 format("namespace A/* comment */ {\n" 379 "\n" 380 "\n" 381 "class B {}\n" 382 "\n" 383 "\n" 384 "}", 385 CustomStyle)); 386 387 // ...but do keep inlining and removing empty lines for non-block extern "C" 388 // functions. 389 verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle()); 390 EXPECT_EQ("extern \"C\" int f() {\n" 391 " int i = 42;\n" 392 " return i;\n" 393 "}", 394 format("extern \"C\" int f() {\n" 395 "\n" 396 " int i = 42;\n" 397 " return i;\n" 398 "}", 399 getGoogleStyle())); 400 401 // Remove empty lines at the beginning and end of blocks. 402 EXPECT_EQ("void f() {\n" 403 "\n" 404 " if (a) {\n" 405 "\n" 406 " f();\n" 407 " }\n" 408 "}", 409 format("void f() {\n" 410 "\n" 411 " if (a) {\n" 412 "\n" 413 " f();\n" 414 "\n" 415 " }\n" 416 "\n" 417 "}", 418 getLLVMStyle())); 419 EXPECT_EQ("void f() {\n" 420 " if (a) {\n" 421 " f();\n" 422 " }\n" 423 "}", 424 format("void f() {\n" 425 "\n" 426 " if (a) {\n" 427 "\n" 428 " f();\n" 429 "\n" 430 " }\n" 431 "\n" 432 "}", 433 getGoogleStyle())); 434 435 // Don't remove empty lines in more complex control statements. 436 EXPECT_EQ("void f() {\n" 437 " if (a) {\n" 438 " f();\n" 439 "\n" 440 " } else if (b) {\n" 441 " f();\n" 442 " }\n" 443 "}", 444 format("void f() {\n" 445 " if (a) {\n" 446 " f();\n" 447 "\n" 448 " } else if (b) {\n" 449 " f();\n" 450 "\n" 451 " }\n" 452 "\n" 453 "}")); 454 455 // Don't remove empty lines before namespace endings. 456 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 457 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 458 EXPECT_EQ("namespace {\n" 459 "int i;\n" 460 "\n" 461 "}", 462 format("namespace {\n" 463 "int i;\n" 464 "\n" 465 "}", 466 LLVMWithNoNamespaceFix)); 467 EXPECT_EQ("namespace {\n" 468 "int i;\n" 469 "}", 470 format("namespace {\n" 471 "int i;\n" 472 "}", 473 LLVMWithNoNamespaceFix)); 474 EXPECT_EQ("namespace {\n" 475 "int i;\n" 476 "\n" 477 "};", 478 format("namespace {\n" 479 "int i;\n" 480 "\n" 481 "};", 482 LLVMWithNoNamespaceFix)); 483 EXPECT_EQ("namespace {\n" 484 "int i;\n" 485 "};", 486 format("namespace {\n" 487 "int i;\n" 488 "};", 489 LLVMWithNoNamespaceFix)); 490 EXPECT_EQ("namespace {\n" 491 "int i;\n" 492 "\n" 493 "}", 494 format("namespace {\n" 495 "int i;\n" 496 "\n" 497 "}")); 498 EXPECT_EQ("namespace {\n" 499 "int i;\n" 500 "\n" 501 "} // namespace", 502 format("namespace {\n" 503 "int i;\n" 504 "\n" 505 "} // namespace")); 506 507 FormatStyle Style = getLLVMStyle(); 508 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 509 Style.MaxEmptyLinesToKeep = 2; 510 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 511 Style.BraceWrapping.AfterClass = true; 512 Style.BraceWrapping.AfterFunction = true; 513 Style.KeepEmptyLinesAtTheStartOfBlocks = false; 514 515 EXPECT_EQ("class Foo\n" 516 "{\n" 517 " Foo() {}\n" 518 "\n" 519 " void funk() {}\n" 520 "};", 521 format("class Foo\n" 522 "{\n" 523 " Foo()\n" 524 " {\n" 525 " }\n" 526 "\n" 527 " void funk() {}\n" 528 "};", 529 Style)); 530 } 531 532 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) { 533 verifyFormat("x = (a) and (b);"); 534 verifyFormat("x = (a) or (b);"); 535 verifyFormat("x = (a) bitand (b);"); 536 verifyFormat("x = (a) bitor (b);"); 537 verifyFormat("x = (a) not_eq (b);"); 538 verifyFormat("x = (a) and_eq (b);"); 539 verifyFormat("x = (a) or_eq (b);"); 540 verifyFormat("x = (a) xor (b);"); 541 } 542 543 TEST_F(FormatTest, RecognizesUnaryOperatorKeywords) { 544 verifyFormat("x = compl(a);"); 545 verifyFormat("x = not(a);"); 546 verifyFormat("x = bitand(a);"); 547 // Unary operator must not be merged with the next identifier 548 verifyFormat("x = compl a;"); 549 verifyFormat("x = not a;"); 550 verifyFormat("x = bitand a;"); 551 } 552 553 //===----------------------------------------------------------------------===// 554 // Tests for control statements. 555 //===----------------------------------------------------------------------===// 556 557 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) { 558 verifyFormat("if (true)\n f();\ng();"); 559 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); 560 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); 561 verifyFormat("if constexpr (true)\n" 562 " f();\ng();"); 563 verifyFormat("if CONSTEXPR (true)\n" 564 " f();\ng();"); 565 verifyFormat("if constexpr (a)\n" 566 " if constexpr (b)\n" 567 " if constexpr (c)\n" 568 " g();\n" 569 "h();"); 570 verifyFormat("if CONSTEXPR (a)\n" 571 " if CONSTEXPR (b)\n" 572 " if CONSTEXPR (c)\n" 573 " g();\n" 574 "h();"); 575 verifyFormat("if constexpr (a)\n" 576 " if constexpr (b) {\n" 577 " f();\n" 578 " }\n" 579 "g();"); 580 verifyFormat("if CONSTEXPR (a)\n" 581 " if CONSTEXPR (b) {\n" 582 " f();\n" 583 " }\n" 584 "g();"); 585 586 verifyFormat("if (a)\n" 587 " g();"); 588 verifyFormat("if (a) {\n" 589 " g()\n" 590 "};"); 591 verifyFormat("if (a)\n" 592 " g();\n" 593 "else\n" 594 " g();"); 595 verifyFormat("if (a) {\n" 596 " g();\n" 597 "} else\n" 598 " g();"); 599 verifyFormat("if (a)\n" 600 " g();\n" 601 "else {\n" 602 " g();\n" 603 "}"); 604 verifyFormat("if (a) {\n" 605 " g();\n" 606 "} else {\n" 607 " g();\n" 608 "}"); 609 verifyFormat("if (a)\n" 610 " g();\n" 611 "else if (b)\n" 612 " g();\n" 613 "else\n" 614 " g();"); 615 verifyFormat("if (a) {\n" 616 " g();\n" 617 "} else if (b)\n" 618 " g();\n" 619 "else\n" 620 " g();"); 621 verifyFormat("if (a)\n" 622 " g();\n" 623 "else if (b) {\n" 624 " g();\n" 625 "} else\n" 626 " g();"); 627 verifyFormat("if (a)\n" 628 " g();\n" 629 "else if (b)\n" 630 " g();\n" 631 "else {\n" 632 " g();\n" 633 "}"); 634 verifyFormat("if (a)\n" 635 " g();\n" 636 "else if (b) {\n" 637 " g();\n" 638 "} else {\n" 639 " g();\n" 640 "}"); 641 verifyFormat("if (a) {\n" 642 " g();\n" 643 "} else if (b) {\n" 644 " g();\n" 645 "} else {\n" 646 " g();\n" 647 "}"); 648 649 FormatStyle AllowsMergedIf = getLLVMStyle(); 650 AllowsMergedIf.IfMacros.push_back("MYIF"); 651 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left; 652 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 653 FormatStyle::SIS_WithoutElse; 654 verifyFormat("if (a)\n" 655 " // comment\n" 656 " f();", 657 AllowsMergedIf); 658 verifyFormat("{\n" 659 " if (a)\n" 660 " label:\n" 661 " f();\n" 662 "}", 663 AllowsMergedIf); 664 verifyFormat("#define A \\\n" 665 " if (a) \\\n" 666 " label: \\\n" 667 " f()", 668 AllowsMergedIf); 669 verifyFormat("if (a)\n" 670 " ;", 671 AllowsMergedIf); 672 verifyFormat("if (a)\n" 673 " if (b) return;", 674 AllowsMergedIf); 675 676 verifyFormat("if (a) // Can't merge this\n" 677 " f();\n", 678 AllowsMergedIf); 679 verifyFormat("if (a) /* still don't merge */\n" 680 " f();", 681 AllowsMergedIf); 682 verifyFormat("if (a) { // Never merge this\n" 683 " f();\n" 684 "}", 685 AllowsMergedIf); 686 verifyFormat("if (a) { /* Never merge this */\n" 687 " f();\n" 688 "}", 689 AllowsMergedIf); 690 verifyFormat("MYIF (a)\n" 691 " // comment\n" 692 " f();", 693 AllowsMergedIf); 694 verifyFormat("{\n" 695 " MYIF (a)\n" 696 " label:\n" 697 " f();\n" 698 "}", 699 AllowsMergedIf); 700 verifyFormat("#define A \\\n" 701 " MYIF (a) \\\n" 702 " label: \\\n" 703 " f()", 704 AllowsMergedIf); 705 verifyFormat("MYIF (a)\n" 706 " ;", 707 AllowsMergedIf); 708 verifyFormat("MYIF (a)\n" 709 " MYIF (b) return;", 710 AllowsMergedIf); 711 712 verifyFormat("MYIF (a) // Can't merge this\n" 713 " f();\n", 714 AllowsMergedIf); 715 verifyFormat("MYIF (a) /* still don't merge */\n" 716 " f();", 717 AllowsMergedIf); 718 verifyFormat("MYIF (a) { // Never merge this\n" 719 " f();\n" 720 "}", 721 AllowsMergedIf); 722 verifyFormat("MYIF (a) { /* Never merge this */\n" 723 " f();\n" 724 "}", 725 AllowsMergedIf); 726 727 AllowsMergedIf.ColumnLimit = 14; 728 // Where line-lengths matter, a 2-letter synonym that maintains line length. 729 // Not IF to avoid any confusion that IF is somehow special. 730 AllowsMergedIf.IfMacros.push_back("FI"); 731 verifyFormat("if (a) return;", AllowsMergedIf); 732 verifyFormat("if (aaaaaaaaa)\n" 733 " return;", 734 AllowsMergedIf); 735 verifyFormat("FI (a) return;", AllowsMergedIf); 736 verifyFormat("FI (aaaaaaaaa)\n" 737 " return;", 738 AllowsMergedIf); 739 740 AllowsMergedIf.ColumnLimit = 13; 741 verifyFormat("if (a)\n return;", AllowsMergedIf); 742 verifyFormat("FI (a)\n return;", AllowsMergedIf); 743 744 FormatStyle AllowsMergedIfElse = getLLVMStyle(); 745 AllowsMergedIfElse.IfMacros.push_back("MYIF"); 746 AllowsMergedIfElse.AllowShortIfStatementsOnASingleLine = 747 FormatStyle::SIS_AllIfsAndElse; 748 verifyFormat("if (a)\n" 749 " // comment\n" 750 " f();\n" 751 "else\n" 752 " // comment\n" 753 " f();", 754 AllowsMergedIfElse); 755 verifyFormat("{\n" 756 " if (a)\n" 757 " label:\n" 758 " f();\n" 759 " else\n" 760 " label:\n" 761 " f();\n" 762 "}", 763 AllowsMergedIfElse); 764 verifyFormat("if (a)\n" 765 " ;\n" 766 "else\n" 767 " ;", 768 AllowsMergedIfElse); 769 verifyFormat("if (a) {\n" 770 "} else {\n" 771 "}", 772 AllowsMergedIfElse); 773 verifyFormat("if (a) return;\n" 774 "else if (b) return;\n" 775 "else return;", 776 AllowsMergedIfElse); 777 verifyFormat("if (a) {\n" 778 "} else return;", 779 AllowsMergedIfElse); 780 verifyFormat("if (a) {\n" 781 "} else if (b) return;\n" 782 "else return;", 783 AllowsMergedIfElse); 784 verifyFormat("if (a) return;\n" 785 "else if (b) {\n" 786 "} else return;", 787 AllowsMergedIfElse); 788 verifyFormat("if (a)\n" 789 " if (b) return;\n" 790 " else return;", 791 AllowsMergedIfElse); 792 verifyFormat("if constexpr (a)\n" 793 " if constexpr (b) return;\n" 794 " else if constexpr (c) return;\n" 795 " else return;", 796 AllowsMergedIfElse); 797 verifyFormat("MYIF (a)\n" 798 " // comment\n" 799 " f();\n" 800 "else\n" 801 " // comment\n" 802 " f();", 803 AllowsMergedIfElse); 804 verifyFormat("{\n" 805 " MYIF (a)\n" 806 " label:\n" 807 " f();\n" 808 " else\n" 809 " label:\n" 810 " f();\n" 811 "}", 812 AllowsMergedIfElse); 813 verifyFormat("MYIF (a)\n" 814 " ;\n" 815 "else\n" 816 " ;", 817 AllowsMergedIfElse); 818 verifyFormat("MYIF (a) {\n" 819 "} else {\n" 820 "}", 821 AllowsMergedIfElse); 822 verifyFormat("MYIF (a) return;\n" 823 "else MYIF (b) return;\n" 824 "else return;", 825 AllowsMergedIfElse); 826 verifyFormat("MYIF (a) {\n" 827 "} else return;", 828 AllowsMergedIfElse); 829 verifyFormat("MYIF (a) {\n" 830 "} else MYIF (b) return;\n" 831 "else return;", 832 AllowsMergedIfElse); 833 verifyFormat("MYIF (a) return;\n" 834 "else MYIF (b) {\n" 835 "} else return;", 836 AllowsMergedIfElse); 837 verifyFormat("MYIF (a)\n" 838 " MYIF (b) return;\n" 839 " else return;", 840 AllowsMergedIfElse); 841 verifyFormat("MYIF constexpr (a)\n" 842 " MYIF constexpr (b) return;\n" 843 " else MYIF constexpr (c) return;\n" 844 " else return;", 845 AllowsMergedIfElse); 846 } 847 848 TEST_F(FormatTest, FormatIfWithoutCompoundStatementButElseWith) { 849 FormatStyle AllowsMergedIf = getLLVMStyle(); 850 AllowsMergedIf.IfMacros.push_back("MYIF"); 851 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left; 852 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 853 FormatStyle::SIS_WithoutElse; 854 verifyFormat("if (a)\n" 855 " f();\n" 856 "else {\n" 857 " g();\n" 858 "}", 859 AllowsMergedIf); 860 verifyFormat("if (a)\n" 861 " f();\n" 862 "else\n" 863 " g();\n", 864 AllowsMergedIf); 865 866 verifyFormat("if (a) g();", AllowsMergedIf); 867 verifyFormat("if (a) {\n" 868 " g()\n" 869 "};", 870 AllowsMergedIf); 871 verifyFormat("if (a)\n" 872 " g();\n" 873 "else\n" 874 " g();", 875 AllowsMergedIf); 876 verifyFormat("if (a) {\n" 877 " g();\n" 878 "} else\n" 879 " g();", 880 AllowsMergedIf); 881 verifyFormat("if (a)\n" 882 " g();\n" 883 "else {\n" 884 " g();\n" 885 "}", 886 AllowsMergedIf); 887 verifyFormat("if (a) {\n" 888 " g();\n" 889 "} else {\n" 890 " g();\n" 891 "}", 892 AllowsMergedIf); 893 verifyFormat("if (a)\n" 894 " g();\n" 895 "else if (b)\n" 896 " g();\n" 897 "else\n" 898 " g();", 899 AllowsMergedIf); 900 verifyFormat("if (a) {\n" 901 " g();\n" 902 "} else if (b)\n" 903 " g();\n" 904 "else\n" 905 " g();", 906 AllowsMergedIf); 907 verifyFormat("if (a)\n" 908 " g();\n" 909 "else if (b) {\n" 910 " g();\n" 911 "} else\n" 912 " g();", 913 AllowsMergedIf); 914 verifyFormat("if (a)\n" 915 " g();\n" 916 "else if (b)\n" 917 " g();\n" 918 "else {\n" 919 " g();\n" 920 "}", 921 AllowsMergedIf); 922 verifyFormat("if (a)\n" 923 " g();\n" 924 "else if (b) {\n" 925 " g();\n" 926 "} else {\n" 927 " g();\n" 928 "}", 929 AllowsMergedIf); 930 verifyFormat("if (a) {\n" 931 " g();\n" 932 "} else if (b) {\n" 933 " g();\n" 934 "} else {\n" 935 " g();\n" 936 "}", 937 AllowsMergedIf); 938 verifyFormat("MYIF (a)\n" 939 " f();\n" 940 "else {\n" 941 " g();\n" 942 "}", 943 AllowsMergedIf); 944 verifyFormat("MYIF (a)\n" 945 " f();\n" 946 "else\n" 947 " g();\n", 948 AllowsMergedIf); 949 950 verifyFormat("MYIF (a) g();", AllowsMergedIf); 951 verifyFormat("MYIF (a) {\n" 952 " g()\n" 953 "};", 954 AllowsMergedIf); 955 verifyFormat("MYIF (a)\n" 956 " g();\n" 957 "else\n" 958 " g();", 959 AllowsMergedIf); 960 verifyFormat("MYIF (a) {\n" 961 " g();\n" 962 "} else\n" 963 " g();", 964 AllowsMergedIf); 965 verifyFormat("MYIF (a)\n" 966 " g();\n" 967 "else {\n" 968 " g();\n" 969 "}", 970 AllowsMergedIf); 971 verifyFormat("MYIF (a) {\n" 972 " g();\n" 973 "} else {\n" 974 " g();\n" 975 "}", 976 AllowsMergedIf); 977 verifyFormat("MYIF (a)\n" 978 " g();\n" 979 "else MYIF (b)\n" 980 " g();\n" 981 "else\n" 982 " g();", 983 AllowsMergedIf); 984 verifyFormat("MYIF (a)\n" 985 " g();\n" 986 "else if (b)\n" 987 " g();\n" 988 "else\n" 989 " g();", 990 AllowsMergedIf); 991 verifyFormat("MYIF (a) {\n" 992 " g();\n" 993 "} else MYIF (b)\n" 994 " g();\n" 995 "else\n" 996 " g();", 997 AllowsMergedIf); 998 verifyFormat("MYIF (a) {\n" 999 " g();\n" 1000 "} else if (b)\n" 1001 " g();\n" 1002 "else\n" 1003 " g();", 1004 AllowsMergedIf); 1005 verifyFormat("MYIF (a)\n" 1006 " g();\n" 1007 "else MYIF (b) {\n" 1008 " g();\n" 1009 "} else\n" 1010 " g();", 1011 AllowsMergedIf); 1012 verifyFormat("MYIF (a)\n" 1013 " g();\n" 1014 "else if (b) {\n" 1015 " g();\n" 1016 "} else\n" 1017 " g();", 1018 AllowsMergedIf); 1019 verifyFormat("MYIF (a)\n" 1020 " g();\n" 1021 "else MYIF (b)\n" 1022 " g();\n" 1023 "else {\n" 1024 " g();\n" 1025 "}", 1026 AllowsMergedIf); 1027 verifyFormat("MYIF (a)\n" 1028 " g();\n" 1029 "else if (b)\n" 1030 " g();\n" 1031 "else {\n" 1032 " g();\n" 1033 "}", 1034 AllowsMergedIf); 1035 verifyFormat("MYIF (a)\n" 1036 " g();\n" 1037 "else MYIF (b) {\n" 1038 " g();\n" 1039 "} else {\n" 1040 " g();\n" 1041 "}", 1042 AllowsMergedIf); 1043 verifyFormat("MYIF (a)\n" 1044 " g();\n" 1045 "else if (b) {\n" 1046 " g();\n" 1047 "} else {\n" 1048 " g();\n" 1049 "}", 1050 AllowsMergedIf); 1051 verifyFormat("MYIF (a) {\n" 1052 " g();\n" 1053 "} else MYIF (b) {\n" 1054 " g();\n" 1055 "} else {\n" 1056 " g();\n" 1057 "}", 1058 AllowsMergedIf); 1059 verifyFormat("MYIF (a) {\n" 1060 " g();\n" 1061 "} else if (b) {\n" 1062 " g();\n" 1063 "} else {\n" 1064 " g();\n" 1065 "}", 1066 AllowsMergedIf); 1067 1068 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 1069 FormatStyle::SIS_OnlyFirstIf; 1070 1071 verifyFormat("if (a) f();\n" 1072 "else {\n" 1073 " g();\n" 1074 "}", 1075 AllowsMergedIf); 1076 verifyFormat("if (a) f();\n" 1077 "else {\n" 1078 " if (a) f();\n" 1079 " else {\n" 1080 " g();\n" 1081 " }\n" 1082 " g();\n" 1083 "}", 1084 AllowsMergedIf); 1085 1086 verifyFormat("if (a) g();", AllowsMergedIf); 1087 verifyFormat("if (a) {\n" 1088 " g()\n" 1089 "};", 1090 AllowsMergedIf); 1091 verifyFormat("if (a) g();\n" 1092 "else\n" 1093 " g();", 1094 AllowsMergedIf); 1095 verifyFormat("if (a) {\n" 1096 " g();\n" 1097 "} else\n" 1098 " g();", 1099 AllowsMergedIf); 1100 verifyFormat("if (a) g();\n" 1101 "else {\n" 1102 " g();\n" 1103 "}", 1104 AllowsMergedIf); 1105 verifyFormat("if (a) {\n" 1106 " g();\n" 1107 "} else {\n" 1108 " g();\n" 1109 "}", 1110 AllowsMergedIf); 1111 verifyFormat("if (a) g();\n" 1112 "else if (b)\n" 1113 " g();\n" 1114 "else\n" 1115 " g();", 1116 AllowsMergedIf); 1117 verifyFormat("if (a) {\n" 1118 " g();\n" 1119 "} else if (b)\n" 1120 " g();\n" 1121 "else\n" 1122 " g();", 1123 AllowsMergedIf); 1124 verifyFormat("if (a) g();\n" 1125 "else if (b) {\n" 1126 " g();\n" 1127 "} else\n" 1128 " g();", 1129 AllowsMergedIf); 1130 verifyFormat("if (a) g();\n" 1131 "else if (b)\n" 1132 " g();\n" 1133 "else {\n" 1134 " g();\n" 1135 "}", 1136 AllowsMergedIf); 1137 verifyFormat("if (a) g();\n" 1138 "else if (b) {\n" 1139 " g();\n" 1140 "} else {\n" 1141 " g();\n" 1142 "}", 1143 AllowsMergedIf); 1144 verifyFormat("if (a) {\n" 1145 " g();\n" 1146 "} else if (b) {\n" 1147 " g();\n" 1148 "} else {\n" 1149 " g();\n" 1150 "}", 1151 AllowsMergedIf); 1152 verifyFormat("MYIF (a) f();\n" 1153 "else {\n" 1154 " g();\n" 1155 "}", 1156 AllowsMergedIf); 1157 verifyFormat("MYIF (a) f();\n" 1158 "else {\n" 1159 " if (a) f();\n" 1160 " else {\n" 1161 " g();\n" 1162 " }\n" 1163 " g();\n" 1164 "}", 1165 AllowsMergedIf); 1166 1167 verifyFormat("MYIF (a) g();", AllowsMergedIf); 1168 verifyFormat("MYIF (a) {\n" 1169 " g()\n" 1170 "};", 1171 AllowsMergedIf); 1172 verifyFormat("MYIF (a) g();\n" 1173 "else\n" 1174 " g();", 1175 AllowsMergedIf); 1176 verifyFormat("MYIF (a) {\n" 1177 " g();\n" 1178 "} else\n" 1179 " g();", 1180 AllowsMergedIf); 1181 verifyFormat("MYIF (a) g();\n" 1182 "else {\n" 1183 " g();\n" 1184 "}", 1185 AllowsMergedIf); 1186 verifyFormat("MYIF (a) {\n" 1187 " g();\n" 1188 "} else {\n" 1189 " g();\n" 1190 "}", 1191 AllowsMergedIf); 1192 verifyFormat("MYIF (a) g();\n" 1193 "else MYIF (b)\n" 1194 " g();\n" 1195 "else\n" 1196 " g();", 1197 AllowsMergedIf); 1198 verifyFormat("MYIF (a) g();\n" 1199 "else if (b)\n" 1200 " g();\n" 1201 "else\n" 1202 " g();", 1203 AllowsMergedIf); 1204 verifyFormat("MYIF (a) {\n" 1205 " g();\n" 1206 "} else MYIF (b)\n" 1207 " g();\n" 1208 "else\n" 1209 " g();", 1210 AllowsMergedIf); 1211 verifyFormat("MYIF (a) {\n" 1212 " g();\n" 1213 "} else if (b)\n" 1214 " g();\n" 1215 "else\n" 1216 " g();", 1217 AllowsMergedIf); 1218 verifyFormat("MYIF (a) g();\n" 1219 "else MYIF (b) {\n" 1220 " g();\n" 1221 "} else\n" 1222 " g();", 1223 AllowsMergedIf); 1224 verifyFormat("MYIF (a) g();\n" 1225 "else if (b) {\n" 1226 " g();\n" 1227 "} else\n" 1228 " g();", 1229 AllowsMergedIf); 1230 verifyFormat("MYIF (a) g();\n" 1231 "else MYIF (b)\n" 1232 " g();\n" 1233 "else {\n" 1234 " g();\n" 1235 "}", 1236 AllowsMergedIf); 1237 verifyFormat("MYIF (a) g();\n" 1238 "else if (b)\n" 1239 " g();\n" 1240 "else {\n" 1241 " g();\n" 1242 "}", 1243 AllowsMergedIf); 1244 verifyFormat("MYIF (a) g();\n" 1245 "else MYIF (b) {\n" 1246 " g();\n" 1247 "} else {\n" 1248 " g();\n" 1249 "}", 1250 AllowsMergedIf); 1251 verifyFormat("MYIF (a) g();\n" 1252 "else if (b) {\n" 1253 " g();\n" 1254 "} else {\n" 1255 " g();\n" 1256 "}", 1257 AllowsMergedIf); 1258 verifyFormat("MYIF (a) {\n" 1259 " g();\n" 1260 "} else MYIF (b) {\n" 1261 " g();\n" 1262 "} else {\n" 1263 " g();\n" 1264 "}", 1265 AllowsMergedIf); 1266 verifyFormat("MYIF (a) {\n" 1267 " g();\n" 1268 "} else if (b) {\n" 1269 " g();\n" 1270 "} else {\n" 1271 " g();\n" 1272 "}", 1273 AllowsMergedIf); 1274 1275 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 1276 FormatStyle::SIS_AllIfsAndElse; 1277 1278 verifyFormat("if (a) f();\n" 1279 "else {\n" 1280 " g();\n" 1281 "}", 1282 AllowsMergedIf); 1283 verifyFormat("if (a) f();\n" 1284 "else {\n" 1285 " if (a) f();\n" 1286 " else {\n" 1287 " g();\n" 1288 " }\n" 1289 " g();\n" 1290 "}", 1291 AllowsMergedIf); 1292 1293 verifyFormat("if (a) g();", AllowsMergedIf); 1294 verifyFormat("if (a) {\n" 1295 " g()\n" 1296 "};", 1297 AllowsMergedIf); 1298 verifyFormat("if (a) g();\n" 1299 "else g();", 1300 AllowsMergedIf); 1301 verifyFormat("if (a) {\n" 1302 " g();\n" 1303 "} else g();", 1304 AllowsMergedIf); 1305 verifyFormat("if (a) g();\n" 1306 "else {\n" 1307 " g();\n" 1308 "}", 1309 AllowsMergedIf); 1310 verifyFormat("if (a) {\n" 1311 " g();\n" 1312 "} else {\n" 1313 " g();\n" 1314 "}", 1315 AllowsMergedIf); 1316 verifyFormat("if (a) g();\n" 1317 "else if (b) g();\n" 1318 "else g();", 1319 AllowsMergedIf); 1320 verifyFormat("if (a) {\n" 1321 " g();\n" 1322 "} else if (b) g();\n" 1323 "else g();", 1324 AllowsMergedIf); 1325 verifyFormat("if (a) g();\n" 1326 "else if (b) {\n" 1327 " g();\n" 1328 "} else g();", 1329 AllowsMergedIf); 1330 verifyFormat("if (a) g();\n" 1331 "else if (b) g();\n" 1332 "else {\n" 1333 " g();\n" 1334 "}", 1335 AllowsMergedIf); 1336 verifyFormat("if (a) g();\n" 1337 "else if (b) {\n" 1338 " g();\n" 1339 "} else {\n" 1340 " g();\n" 1341 "}", 1342 AllowsMergedIf); 1343 verifyFormat("if (a) {\n" 1344 " g();\n" 1345 "} else if (b) {\n" 1346 " g();\n" 1347 "} else {\n" 1348 " g();\n" 1349 "}", 1350 AllowsMergedIf); 1351 verifyFormat("MYIF (a) f();\n" 1352 "else {\n" 1353 " g();\n" 1354 "}", 1355 AllowsMergedIf); 1356 verifyFormat("MYIF (a) f();\n" 1357 "else {\n" 1358 " if (a) f();\n" 1359 " else {\n" 1360 " g();\n" 1361 " }\n" 1362 " g();\n" 1363 "}", 1364 AllowsMergedIf); 1365 1366 verifyFormat("MYIF (a) g();", AllowsMergedIf); 1367 verifyFormat("MYIF (a) {\n" 1368 " g()\n" 1369 "};", 1370 AllowsMergedIf); 1371 verifyFormat("MYIF (a) g();\n" 1372 "else g();", 1373 AllowsMergedIf); 1374 verifyFormat("MYIF (a) {\n" 1375 " g();\n" 1376 "} else g();", 1377 AllowsMergedIf); 1378 verifyFormat("MYIF (a) g();\n" 1379 "else {\n" 1380 " g();\n" 1381 "}", 1382 AllowsMergedIf); 1383 verifyFormat("MYIF (a) {\n" 1384 " g();\n" 1385 "} else {\n" 1386 " g();\n" 1387 "}", 1388 AllowsMergedIf); 1389 verifyFormat("MYIF (a) g();\n" 1390 "else MYIF (b) g();\n" 1391 "else g();", 1392 AllowsMergedIf); 1393 verifyFormat("MYIF (a) g();\n" 1394 "else if (b) g();\n" 1395 "else g();", 1396 AllowsMergedIf); 1397 verifyFormat("MYIF (a) {\n" 1398 " g();\n" 1399 "} else MYIF (b) g();\n" 1400 "else g();", 1401 AllowsMergedIf); 1402 verifyFormat("MYIF (a) {\n" 1403 " g();\n" 1404 "} else if (b) g();\n" 1405 "else g();", 1406 AllowsMergedIf); 1407 verifyFormat("MYIF (a) g();\n" 1408 "else MYIF (b) {\n" 1409 " g();\n" 1410 "} else g();", 1411 AllowsMergedIf); 1412 verifyFormat("MYIF (a) g();\n" 1413 "else if (b) {\n" 1414 " g();\n" 1415 "} else g();", 1416 AllowsMergedIf); 1417 verifyFormat("MYIF (a) g();\n" 1418 "else MYIF (b) g();\n" 1419 "else {\n" 1420 " g();\n" 1421 "}", 1422 AllowsMergedIf); 1423 verifyFormat("MYIF (a) g();\n" 1424 "else if (b) g();\n" 1425 "else {\n" 1426 " g();\n" 1427 "}", 1428 AllowsMergedIf); 1429 verifyFormat("MYIF (a) g();\n" 1430 "else MYIF (b) {\n" 1431 " g();\n" 1432 "} else {\n" 1433 " g();\n" 1434 "}", 1435 AllowsMergedIf); 1436 verifyFormat("MYIF (a) g();\n" 1437 "else if (b) {\n" 1438 " g();\n" 1439 "} else {\n" 1440 " g();\n" 1441 "}", 1442 AllowsMergedIf); 1443 verifyFormat("MYIF (a) {\n" 1444 " g();\n" 1445 "} else MYIF (b) {\n" 1446 " g();\n" 1447 "} else {\n" 1448 " g();\n" 1449 "}", 1450 AllowsMergedIf); 1451 verifyFormat("MYIF (a) {\n" 1452 " g();\n" 1453 "} else if (b) {\n" 1454 " g();\n" 1455 "} else {\n" 1456 " g();\n" 1457 "}", 1458 AllowsMergedIf); 1459 } 1460 1461 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) { 1462 FormatStyle AllowsMergedLoops = getLLVMStyle(); 1463 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true; 1464 verifyFormat("while (true) continue;", AllowsMergedLoops); 1465 verifyFormat("for (;;) continue;", AllowsMergedLoops); 1466 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops); 1467 verifyFormat("while (true)\n" 1468 " ;", 1469 AllowsMergedLoops); 1470 verifyFormat("for (;;)\n" 1471 " ;", 1472 AllowsMergedLoops); 1473 verifyFormat("for (;;)\n" 1474 " for (;;) continue;", 1475 AllowsMergedLoops); 1476 verifyFormat("for (;;) // Can't merge this\n" 1477 " continue;", 1478 AllowsMergedLoops); 1479 verifyFormat("for (;;) /* still don't merge */\n" 1480 " continue;", 1481 AllowsMergedLoops); 1482 verifyFormat("do a++;\n" 1483 "while (true);", 1484 AllowsMergedLoops); 1485 verifyFormat("do /* Don't merge */\n" 1486 " a++;\n" 1487 "while (true);", 1488 AllowsMergedLoops); 1489 verifyFormat("do // Don't merge\n" 1490 " a++;\n" 1491 "while (true);", 1492 AllowsMergedLoops); 1493 verifyFormat("do\n" 1494 " // Don't merge\n" 1495 " a++;\n" 1496 "while (true);", 1497 AllowsMergedLoops); 1498 // Without braces labels are interpreted differently. 1499 verifyFormat("{\n" 1500 " do\n" 1501 " label:\n" 1502 " a++;\n" 1503 " while (true);\n" 1504 "}", 1505 AllowsMergedLoops); 1506 } 1507 1508 TEST_F(FormatTest, FormatShortBracedStatements) { 1509 FormatStyle AllowSimpleBracedStatements = getLLVMStyle(); 1510 AllowSimpleBracedStatements.IfMacros.push_back("MYIF"); 1511 // Where line-lengths matter, a 2-letter synonym that maintains line length. 1512 // Not IF to avoid any confusion that IF is somehow special. 1513 AllowSimpleBracedStatements.IfMacros.push_back("FI"); 1514 AllowSimpleBracedStatements.ColumnLimit = 40; 1515 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = 1516 FormatStyle::SBS_Always; 1517 1518 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1519 FormatStyle::SIS_WithoutElse; 1520 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 1521 1522 AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom; 1523 AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true; 1524 AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false; 1525 1526 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1527 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 1528 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1529 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1530 verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements); 1531 verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1532 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1533 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1534 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 1535 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 1536 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1537 verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements); 1538 verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements); 1539 verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1540 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 1541 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 1542 verifyFormat("if (true) { fffffffffffffffffffffff(); }", 1543 AllowSimpleBracedStatements); 1544 verifyFormat("if (true) {\n" 1545 " ffffffffffffffffffffffff();\n" 1546 "}", 1547 AllowSimpleBracedStatements); 1548 verifyFormat("if (true) {\n" 1549 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1550 "}", 1551 AllowSimpleBracedStatements); 1552 verifyFormat("if (true) { //\n" 1553 " f();\n" 1554 "}", 1555 AllowSimpleBracedStatements); 1556 verifyFormat("if (true) {\n" 1557 " f();\n" 1558 " f();\n" 1559 "}", 1560 AllowSimpleBracedStatements); 1561 verifyFormat("if (true) {\n" 1562 " f();\n" 1563 "} else {\n" 1564 " f();\n" 1565 "}", 1566 AllowSimpleBracedStatements); 1567 verifyFormat("FI (true) { fffffffffffffffffffffff(); }", 1568 AllowSimpleBracedStatements); 1569 verifyFormat("MYIF (true) {\n" 1570 " ffffffffffffffffffffffff();\n" 1571 "}", 1572 AllowSimpleBracedStatements); 1573 verifyFormat("MYIF (true) {\n" 1574 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1575 "}", 1576 AllowSimpleBracedStatements); 1577 verifyFormat("MYIF (true) { //\n" 1578 " f();\n" 1579 "}", 1580 AllowSimpleBracedStatements); 1581 verifyFormat("MYIF (true) {\n" 1582 " f();\n" 1583 " f();\n" 1584 "}", 1585 AllowSimpleBracedStatements); 1586 verifyFormat("MYIF (true) {\n" 1587 " f();\n" 1588 "} else {\n" 1589 " f();\n" 1590 "}", 1591 AllowSimpleBracedStatements); 1592 1593 verifyFormat("struct A2 {\n" 1594 " int X;\n" 1595 "};", 1596 AllowSimpleBracedStatements); 1597 verifyFormat("typedef struct A2 {\n" 1598 " int X;\n" 1599 "} A2_t;", 1600 AllowSimpleBracedStatements); 1601 verifyFormat("template <int> struct A2 {\n" 1602 " struct B {};\n" 1603 "};", 1604 AllowSimpleBracedStatements); 1605 1606 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1607 FormatStyle::SIS_Never; 1608 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1609 verifyFormat("if (true) {\n" 1610 " f();\n" 1611 "}", 1612 AllowSimpleBracedStatements); 1613 verifyFormat("if (true) {\n" 1614 " f();\n" 1615 "} else {\n" 1616 " f();\n" 1617 "}", 1618 AllowSimpleBracedStatements); 1619 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1620 verifyFormat("MYIF (true) {\n" 1621 " f();\n" 1622 "}", 1623 AllowSimpleBracedStatements); 1624 verifyFormat("MYIF (true) {\n" 1625 " f();\n" 1626 "} else {\n" 1627 " f();\n" 1628 "}", 1629 AllowSimpleBracedStatements); 1630 1631 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 1632 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1633 verifyFormat("while (true) {\n" 1634 " f();\n" 1635 "}", 1636 AllowSimpleBracedStatements); 1637 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1638 verifyFormat("for (;;) {\n" 1639 " f();\n" 1640 "}", 1641 AllowSimpleBracedStatements); 1642 1643 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1644 FormatStyle::SIS_WithoutElse; 1645 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 1646 AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement = 1647 FormatStyle::BWACS_Always; 1648 1649 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1650 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 1651 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1652 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1653 verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements); 1654 verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1655 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1656 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1657 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 1658 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 1659 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1660 verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements); 1661 verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements); 1662 verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1663 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 1664 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 1665 verifyFormat("if (true) { fffffffffffffffffffffff(); }", 1666 AllowSimpleBracedStatements); 1667 verifyFormat("if (true)\n" 1668 "{\n" 1669 " ffffffffffffffffffffffff();\n" 1670 "}", 1671 AllowSimpleBracedStatements); 1672 verifyFormat("if (true)\n" 1673 "{\n" 1674 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1675 "}", 1676 AllowSimpleBracedStatements); 1677 verifyFormat("if (true)\n" 1678 "{ //\n" 1679 " f();\n" 1680 "}", 1681 AllowSimpleBracedStatements); 1682 verifyFormat("if (true)\n" 1683 "{\n" 1684 " f();\n" 1685 " f();\n" 1686 "}", 1687 AllowSimpleBracedStatements); 1688 verifyFormat("if (true)\n" 1689 "{\n" 1690 " f();\n" 1691 "} else\n" 1692 "{\n" 1693 " f();\n" 1694 "}", 1695 AllowSimpleBracedStatements); 1696 verifyFormat("FI (true) { fffffffffffffffffffffff(); }", 1697 AllowSimpleBracedStatements); 1698 verifyFormat("MYIF (true)\n" 1699 "{\n" 1700 " ffffffffffffffffffffffff();\n" 1701 "}", 1702 AllowSimpleBracedStatements); 1703 verifyFormat("MYIF (true)\n" 1704 "{\n" 1705 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1706 "}", 1707 AllowSimpleBracedStatements); 1708 verifyFormat("MYIF (true)\n" 1709 "{ //\n" 1710 " f();\n" 1711 "}", 1712 AllowSimpleBracedStatements); 1713 verifyFormat("MYIF (true)\n" 1714 "{\n" 1715 " f();\n" 1716 " f();\n" 1717 "}", 1718 AllowSimpleBracedStatements); 1719 verifyFormat("MYIF (true)\n" 1720 "{\n" 1721 " f();\n" 1722 "} else\n" 1723 "{\n" 1724 " f();\n" 1725 "}", 1726 AllowSimpleBracedStatements); 1727 1728 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1729 FormatStyle::SIS_Never; 1730 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1731 verifyFormat("if (true)\n" 1732 "{\n" 1733 " f();\n" 1734 "}", 1735 AllowSimpleBracedStatements); 1736 verifyFormat("if (true)\n" 1737 "{\n" 1738 " f();\n" 1739 "} else\n" 1740 "{\n" 1741 " f();\n" 1742 "}", 1743 AllowSimpleBracedStatements); 1744 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1745 verifyFormat("MYIF (true)\n" 1746 "{\n" 1747 " f();\n" 1748 "}", 1749 AllowSimpleBracedStatements); 1750 verifyFormat("MYIF (true)\n" 1751 "{\n" 1752 " f();\n" 1753 "} else\n" 1754 "{\n" 1755 " f();\n" 1756 "}", 1757 AllowSimpleBracedStatements); 1758 1759 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 1760 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1761 verifyFormat("while (true)\n" 1762 "{\n" 1763 " f();\n" 1764 "}", 1765 AllowSimpleBracedStatements); 1766 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1767 verifyFormat("for (;;)\n" 1768 "{\n" 1769 " f();\n" 1770 "}", 1771 AllowSimpleBracedStatements); 1772 } 1773 1774 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) { 1775 FormatStyle Style = getLLVMStyleWithColumns(60); 1776 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 1777 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; 1778 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 1779 EXPECT_EQ("#define A \\\n" 1780 " if (HANDLEwernufrnuLwrmviferuvnierv) \\\n" 1781 " { \\\n" 1782 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" 1783 " }\n" 1784 "X;", 1785 format("#define A \\\n" 1786 " if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n" 1787 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" 1788 " }\n" 1789 "X;", 1790 Style)); 1791 } 1792 1793 TEST_F(FormatTest, ParseIfElse) { 1794 verifyFormat("if (true)\n" 1795 " if (true)\n" 1796 " if (true)\n" 1797 " f();\n" 1798 " else\n" 1799 " g();\n" 1800 " else\n" 1801 " h();\n" 1802 "else\n" 1803 " i();"); 1804 verifyFormat("if (true)\n" 1805 " if (true)\n" 1806 " if (true) {\n" 1807 " if (true)\n" 1808 " f();\n" 1809 " } else {\n" 1810 " g();\n" 1811 " }\n" 1812 " else\n" 1813 " h();\n" 1814 "else {\n" 1815 " i();\n" 1816 "}"); 1817 verifyFormat("if (true)\n" 1818 " if constexpr (true)\n" 1819 " if (true) {\n" 1820 " if constexpr (true)\n" 1821 " f();\n" 1822 " } else {\n" 1823 " g();\n" 1824 " }\n" 1825 " else\n" 1826 " h();\n" 1827 "else {\n" 1828 " i();\n" 1829 "}"); 1830 verifyFormat("if (true)\n" 1831 " if CONSTEXPR (true)\n" 1832 " if (true) {\n" 1833 " if CONSTEXPR (true)\n" 1834 " f();\n" 1835 " } else {\n" 1836 " g();\n" 1837 " }\n" 1838 " else\n" 1839 " h();\n" 1840 "else {\n" 1841 " i();\n" 1842 "}"); 1843 verifyFormat("void f() {\n" 1844 " if (a) {\n" 1845 " } else {\n" 1846 " }\n" 1847 "}"); 1848 } 1849 1850 TEST_F(FormatTest, ElseIf) { 1851 verifyFormat("if (a) {\n} else if (b) {\n}"); 1852 verifyFormat("if (a)\n" 1853 " f();\n" 1854 "else if (b)\n" 1855 " g();\n" 1856 "else\n" 1857 " h();"); 1858 verifyFormat("if (a)\n" 1859 " f();\n" 1860 "else // comment\n" 1861 " if (b) {\n" 1862 " g();\n" 1863 " h();\n" 1864 " }"); 1865 verifyFormat("if constexpr (a)\n" 1866 " f();\n" 1867 "else if constexpr (b)\n" 1868 " g();\n" 1869 "else\n" 1870 " h();"); 1871 verifyFormat("if CONSTEXPR (a)\n" 1872 " f();\n" 1873 "else if CONSTEXPR (b)\n" 1874 " g();\n" 1875 "else\n" 1876 " h();"); 1877 verifyFormat("if (a) {\n" 1878 " f();\n" 1879 "}\n" 1880 "// or else ..\n" 1881 "else {\n" 1882 " g()\n" 1883 "}"); 1884 1885 verifyFormat("if (a) {\n" 1886 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1887 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 1888 "}"); 1889 verifyFormat("if (a) {\n" 1890 "} else if constexpr (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1891 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 1892 "}"); 1893 verifyFormat("if (a) {\n" 1894 "} else if CONSTEXPR (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1895 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 1896 "}"); 1897 verifyFormat("if (a) {\n" 1898 "} else if (\n" 1899 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 1900 "}", 1901 getLLVMStyleWithColumns(62)); 1902 verifyFormat("if (a) {\n" 1903 "} else if constexpr (\n" 1904 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 1905 "}", 1906 getLLVMStyleWithColumns(62)); 1907 verifyFormat("if (a) {\n" 1908 "} else if CONSTEXPR (\n" 1909 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 1910 "}", 1911 getLLVMStyleWithColumns(62)); 1912 } 1913 1914 TEST_F(FormatTest, SeparatePointerReferenceAlignment) { 1915 FormatStyle Style = getLLVMStyle(); 1916 // Check first the default LLVM style 1917 // Style.PointerAlignment = FormatStyle::PAS_Right; 1918 // Style.ReferenceAlignment = FormatStyle::RAS_Pointer; 1919 verifyFormat("int *f1(int *a, int &b, int &&c);", Style); 1920 verifyFormat("int &f2(int &&c, int *a, int &b);", Style); 1921 verifyFormat("int &&f3(int &b, int &&c, int *a);", Style); 1922 verifyFormat("int *f1(int &a) const &;", Style); 1923 verifyFormat("int *f1(int &a) const & = 0;", Style); 1924 verifyFormat("int *a = f1();", Style); 1925 verifyFormat("int &b = f2();", Style); 1926 verifyFormat("int &&c = f3();", Style); 1927 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 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) { 12204 FormatStyle Style = getLLVMStyle(); 12205 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 12206 verifyFormat("#ifdef A\n" 12207 "int f() {}\n" 12208 "#else\n" 12209 "int g() {}\n" 12210 "#endif", 12211 Style); 12212 } 12213 12214 TEST_F(FormatTest, SplitEmptyClass) { 12215 FormatStyle Style = getLLVMStyle(); 12216 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12217 Style.BraceWrapping.AfterClass = true; 12218 Style.BraceWrapping.SplitEmptyRecord = false; 12219 12220 verifyFormat("class Foo\n" 12221 "{};", 12222 Style); 12223 verifyFormat("/* something */ class Foo\n" 12224 "{};", 12225 Style); 12226 verifyFormat("template <typename X> class Foo\n" 12227 "{};", 12228 Style); 12229 verifyFormat("class Foo\n" 12230 "{\n" 12231 " Foo();\n" 12232 "};", 12233 Style); 12234 verifyFormat("typedef class Foo\n" 12235 "{\n" 12236 "} Foo_t;", 12237 Style); 12238 12239 Style.BraceWrapping.SplitEmptyRecord = true; 12240 Style.BraceWrapping.AfterStruct = true; 12241 verifyFormat("class rep\n" 12242 "{\n" 12243 "};", 12244 Style); 12245 verifyFormat("struct rep\n" 12246 "{\n" 12247 "};", 12248 Style); 12249 verifyFormat("template <typename T> class rep\n" 12250 "{\n" 12251 "};", 12252 Style); 12253 verifyFormat("template <typename T> struct rep\n" 12254 "{\n" 12255 "};", 12256 Style); 12257 verifyFormat("class rep\n" 12258 "{\n" 12259 " int x;\n" 12260 "};", 12261 Style); 12262 verifyFormat("struct rep\n" 12263 "{\n" 12264 " int x;\n" 12265 "};", 12266 Style); 12267 verifyFormat("template <typename T> class rep\n" 12268 "{\n" 12269 " int x;\n" 12270 "};", 12271 Style); 12272 verifyFormat("template <typename T> struct rep\n" 12273 "{\n" 12274 " int x;\n" 12275 "};", 12276 Style); 12277 verifyFormat("template <typename T> class rep // Foo\n" 12278 "{\n" 12279 " int x;\n" 12280 "};", 12281 Style); 12282 verifyFormat("template <typename T> struct rep // Bar\n" 12283 "{\n" 12284 " int x;\n" 12285 "};", 12286 Style); 12287 12288 verifyFormat("template <typename T> class rep<T>\n" 12289 "{\n" 12290 " int x;\n" 12291 "};", 12292 Style); 12293 12294 verifyFormat("template <typename T> class rep<std::complex<T>>\n" 12295 "{\n" 12296 " int x;\n" 12297 "};", 12298 Style); 12299 verifyFormat("template <typename T> class rep<std::complex<T>>\n" 12300 "{\n" 12301 "};", 12302 Style); 12303 12304 verifyFormat("#include \"stdint.h\"\n" 12305 "namespace rep {}", 12306 Style); 12307 verifyFormat("#include <stdint.h>\n" 12308 "namespace rep {}", 12309 Style); 12310 verifyFormat("#include <stdint.h>\n" 12311 "namespace rep {}", 12312 "#include <stdint.h>\n" 12313 "namespace rep {\n" 12314 "\n" 12315 "\n" 12316 "}", 12317 Style); 12318 } 12319 12320 TEST_F(FormatTest, SplitEmptyStruct) { 12321 FormatStyle Style = getLLVMStyle(); 12322 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12323 Style.BraceWrapping.AfterStruct = true; 12324 Style.BraceWrapping.SplitEmptyRecord = false; 12325 12326 verifyFormat("struct Foo\n" 12327 "{};", 12328 Style); 12329 verifyFormat("/* something */ struct Foo\n" 12330 "{};", 12331 Style); 12332 verifyFormat("template <typename X> struct Foo\n" 12333 "{};", 12334 Style); 12335 verifyFormat("struct Foo\n" 12336 "{\n" 12337 " Foo();\n" 12338 "};", 12339 Style); 12340 verifyFormat("typedef struct Foo\n" 12341 "{\n" 12342 "} Foo_t;", 12343 Style); 12344 // typedef struct Bar {} Bar_t; 12345 } 12346 12347 TEST_F(FormatTest, SplitEmptyUnion) { 12348 FormatStyle Style = getLLVMStyle(); 12349 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12350 Style.BraceWrapping.AfterUnion = true; 12351 Style.BraceWrapping.SplitEmptyRecord = false; 12352 12353 verifyFormat("union Foo\n" 12354 "{};", 12355 Style); 12356 verifyFormat("/* something */ union Foo\n" 12357 "{};", 12358 Style); 12359 verifyFormat("union Foo\n" 12360 "{\n" 12361 " A,\n" 12362 "};", 12363 Style); 12364 verifyFormat("typedef union Foo\n" 12365 "{\n" 12366 "} Foo_t;", 12367 Style); 12368 } 12369 12370 TEST_F(FormatTest, SplitEmptyNamespace) { 12371 FormatStyle Style = getLLVMStyle(); 12372 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12373 Style.BraceWrapping.AfterNamespace = true; 12374 Style.BraceWrapping.SplitEmptyNamespace = false; 12375 12376 verifyFormat("namespace Foo\n" 12377 "{};", 12378 Style); 12379 verifyFormat("/* something */ namespace Foo\n" 12380 "{};", 12381 Style); 12382 verifyFormat("inline namespace Foo\n" 12383 "{};", 12384 Style); 12385 verifyFormat("/* something */ inline namespace Foo\n" 12386 "{};", 12387 Style); 12388 verifyFormat("export namespace Foo\n" 12389 "{};", 12390 Style); 12391 verifyFormat("namespace Foo\n" 12392 "{\n" 12393 "void Bar();\n" 12394 "};", 12395 Style); 12396 } 12397 12398 TEST_F(FormatTest, NeverMergeShortRecords) { 12399 FormatStyle Style = getLLVMStyle(); 12400 12401 verifyFormat("class Foo {\n" 12402 " Foo();\n" 12403 "};", 12404 Style); 12405 verifyFormat("typedef class Foo {\n" 12406 " Foo();\n" 12407 "} Foo_t;", 12408 Style); 12409 verifyFormat("struct Foo {\n" 12410 " Foo();\n" 12411 "};", 12412 Style); 12413 verifyFormat("typedef struct Foo {\n" 12414 " Foo();\n" 12415 "} Foo_t;", 12416 Style); 12417 verifyFormat("union Foo {\n" 12418 " A,\n" 12419 "};", 12420 Style); 12421 verifyFormat("typedef union Foo {\n" 12422 " A,\n" 12423 "} Foo_t;", 12424 Style); 12425 verifyFormat("namespace Foo {\n" 12426 "void Bar();\n" 12427 "};", 12428 Style); 12429 12430 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12431 Style.BraceWrapping.AfterClass = true; 12432 Style.BraceWrapping.AfterStruct = true; 12433 Style.BraceWrapping.AfterUnion = true; 12434 Style.BraceWrapping.AfterNamespace = true; 12435 verifyFormat("class Foo\n" 12436 "{\n" 12437 " Foo();\n" 12438 "};", 12439 Style); 12440 verifyFormat("typedef class Foo\n" 12441 "{\n" 12442 " Foo();\n" 12443 "} Foo_t;", 12444 Style); 12445 verifyFormat("struct Foo\n" 12446 "{\n" 12447 " Foo();\n" 12448 "};", 12449 Style); 12450 verifyFormat("typedef struct Foo\n" 12451 "{\n" 12452 " Foo();\n" 12453 "} Foo_t;", 12454 Style); 12455 verifyFormat("union Foo\n" 12456 "{\n" 12457 " A,\n" 12458 "};", 12459 Style); 12460 verifyFormat("typedef union Foo\n" 12461 "{\n" 12462 " A,\n" 12463 "} Foo_t;", 12464 Style); 12465 verifyFormat("namespace Foo\n" 12466 "{\n" 12467 "void Bar();\n" 12468 "};", 12469 Style); 12470 } 12471 12472 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 12473 // Elaborate type variable declarations. 12474 verifyFormat("struct foo a = {bar};\nint n;"); 12475 verifyFormat("class foo a = {bar};\nint n;"); 12476 verifyFormat("union foo a = {bar};\nint n;"); 12477 12478 // Elaborate types inside function definitions. 12479 verifyFormat("struct foo f() {}\nint n;"); 12480 verifyFormat("class foo f() {}\nint n;"); 12481 verifyFormat("union foo f() {}\nint n;"); 12482 12483 // Templates. 12484 verifyFormat("template <class X> void f() {}\nint n;"); 12485 verifyFormat("template <struct X> void f() {}\nint n;"); 12486 verifyFormat("template <union X> void f() {}\nint n;"); 12487 12488 // Actual definitions... 12489 verifyFormat("struct {\n} n;"); 12490 verifyFormat( 12491 "template <template <class T, class Y>, class Z> class X {\n} n;"); 12492 verifyFormat("union Z {\n int n;\n} x;"); 12493 verifyFormat("class MACRO Z {\n} n;"); 12494 verifyFormat("class MACRO(X) Z {\n} n;"); 12495 verifyFormat("class __attribute__(X) Z {\n} n;"); 12496 verifyFormat("class __declspec(X) Z {\n} n;"); 12497 verifyFormat("class A##B##C {\n} n;"); 12498 verifyFormat("class alignas(16) Z {\n} n;"); 12499 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 12500 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 12501 12502 // Redefinition from nested context: 12503 verifyFormat("class A::B::C {\n} n;"); 12504 12505 // Template definitions. 12506 verifyFormat( 12507 "template <typename F>\n" 12508 "Matcher(const Matcher<F> &Other,\n" 12509 " typename enable_if_c<is_base_of<F, T>::value &&\n" 12510 " !is_same<F, T>::value>::type * = 0)\n" 12511 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 12512 12513 // FIXME: This is still incorrectly handled at the formatter side. 12514 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 12515 verifyFormat("int i = SomeFunction(a<b, a> b);"); 12516 12517 // FIXME: 12518 // This now gets parsed incorrectly as class definition. 12519 // verifyFormat("class A<int> f() {\n}\nint n;"); 12520 12521 // Elaborate types where incorrectly parsing the structural element would 12522 // break the indent. 12523 verifyFormat("if (true)\n" 12524 " class X x;\n" 12525 "else\n" 12526 " f();\n"); 12527 12528 // This is simply incomplete. Formatting is not important, but must not crash. 12529 verifyFormat("class A:"); 12530 } 12531 12532 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 12533 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 12534 format("#error Leave all white!!!!! space* alone!\n")); 12535 EXPECT_EQ( 12536 "#warning Leave all white!!!!! space* alone!\n", 12537 format("#warning Leave all white!!!!! space* alone!\n")); 12538 EXPECT_EQ("#error 1", format(" # error 1")); 12539 EXPECT_EQ("#warning 1", format(" # warning 1")); 12540 } 12541 12542 TEST_F(FormatTest, FormatHashIfExpressions) { 12543 verifyFormat("#if AAAA && BBBB"); 12544 verifyFormat("#if (AAAA && BBBB)"); 12545 verifyFormat("#elif (AAAA && BBBB)"); 12546 // FIXME: Come up with a better indentation for #elif. 12547 verifyFormat( 12548 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 12549 " defined(BBBBBBBB)\n" 12550 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 12551 " defined(BBBBBBBB)\n" 12552 "#endif", 12553 getLLVMStyleWithColumns(65)); 12554 } 12555 12556 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 12557 FormatStyle AllowsMergedIf = getGoogleStyle(); 12558 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 12559 FormatStyle::SIS_WithoutElse; 12560 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 12561 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 12562 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 12563 EXPECT_EQ("if (true) return 42;", 12564 format("if (true)\nreturn 42;", AllowsMergedIf)); 12565 FormatStyle ShortMergedIf = AllowsMergedIf; 12566 ShortMergedIf.ColumnLimit = 25; 12567 verifyFormat("#define A \\\n" 12568 " if (true) return 42;", 12569 ShortMergedIf); 12570 verifyFormat("#define A \\\n" 12571 " f(); \\\n" 12572 " if (true)\n" 12573 "#define B", 12574 ShortMergedIf); 12575 verifyFormat("#define A \\\n" 12576 " f(); \\\n" 12577 " if (true)\n" 12578 "g();", 12579 ShortMergedIf); 12580 verifyFormat("{\n" 12581 "#ifdef A\n" 12582 " // Comment\n" 12583 " if (true) continue;\n" 12584 "#endif\n" 12585 " // Comment\n" 12586 " if (true) continue;\n" 12587 "}", 12588 ShortMergedIf); 12589 ShortMergedIf.ColumnLimit = 33; 12590 verifyFormat("#define A \\\n" 12591 " if constexpr (true) return 42;", 12592 ShortMergedIf); 12593 verifyFormat("#define A \\\n" 12594 " if CONSTEXPR (true) return 42;", 12595 ShortMergedIf); 12596 ShortMergedIf.ColumnLimit = 29; 12597 verifyFormat("#define A \\\n" 12598 " if (aaaaaaaaaa) return 1; \\\n" 12599 " return 2;", 12600 ShortMergedIf); 12601 ShortMergedIf.ColumnLimit = 28; 12602 verifyFormat("#define A \\\n" 12603 " if (aaaaaaaaaa) \\\n" 12604 " return 1; \\\n" 12605 " return 2;", 12606 ShortMergedIf); 12607 verifyFormat("#define A \\\n" 12608 " if constexpr (aaaaaaa) \\\n" 12609 " return 1; \\\n" 12610 " return 2;", 12611 ShortMergedIf); 12612 verifyFormat("#define A \\\n" 12613 " if CONSTEXPR (aaaaaaa) \\\n" 12614 " return 1; \\\n" 12615 " return 2;", 12616 ShortMergedIf); 12617 } 12618 12619 TEST_F(FormatTest, FormatStarDependingOnContext) { 12620 verifyFormat("void f(int *a);"); 12621 verifyFormat("void f() { f(fint * b); }"); 12622 verifyFormat("class A {\n void f(int *a);\n};"); 12623 verifyFormat("class A {\n int *a;\n};"); 12624 verifyFormat("namespace a {\n" 12625 "namespace b {\n" 12626 "class A {\n" 12627 " void f() {}\n" 12628 " int *a;\n" 12629 "};\n" 12630 "} // namespace b\n" 12631 "} // namespace a"); 12632 } 12633 12634 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 12635 verifyFormat("while"); 12636 verifyFormat("operator"); 12637 } 12638 12639 TEST_F(FormatTest, SkipsDeeplyNestedLines) { 12640 // This code would be painfully slow to format if we didn't skip it. 12641 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 12642 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12643 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12644 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12645 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12646 "A(1, 1)\n" 12647 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x 12648 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12649 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12650 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12651 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12652 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12653 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12654 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12655 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12656 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n"); 12657 // Deeply nested part is untouched, rest is formatted. 12658 EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n", 12659 format(std::string("int i;\n") + Code + "int j;\n", 12660 getLLVMStyle(), SC_ExpectIncomplete)); 12661 } 12662 12663 //===----------------------------------------------------------------------===// 12664 // Objective-C tests. 12665 //===----------------------------------------------------------------------===// 12666 12667 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 12668 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 12669 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 12670 format("-(NSUInteger)indexOfObject:(id)anObject;")); 12671 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 12672 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 12673 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 12674 format("-(NSInteger)Method3:(id)anObject;")); 12675 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 12676 format("-(NSInteger)Method4:(id)anObject;")); 12677 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 12678 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 12679 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 12680 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 12681 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 12682 "forAllCells:(BOOL)flag;", 12683 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 12684 "forAllCells:(BOOL)flag;")); 12685 12686 // Very long objectiveC method declaration. 12687 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 12688 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 12689 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 12690 " inRange:(NSRange)range\n" 12691 " outRange:(NSRange)out_range\n" 12692 " outRange1:(NSRange)out_range1\n" 12693 " outRange2:(NSRange)out_range2\n" 12694 " outRange3:(NSRange)out_range3\n" 12695 " outRange4:(NSRange)out_range4\n" 12696 " outRange5:(NSRange)out_range5\n" 12697 " outRange6:(NSRange)out_range6\n" 12698 " outRange7:(NSRange)out_range7\n" 12699 " outRange8:(NSRange)out_range8\n" 12700 " outRange9:(NSRange)out_range9;"); 12701 12702 // When the function name has to be wrapped. 12703 FormatStyle Style = getLLVMStyle(); 12704 // ObjC ignores IndentWrappedFunctionNames when wrapping methods 12705 // and always indents instead. 12706 Style.IndentWrappedFunctionNames = false; 12707 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 12708 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 12709 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 12710 "}", 12711 Style); 12712 Style.IndentWrappedFunctionNames = true; 12713 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 12714 " veryLooooooooooongName:(NSString)cccccccccccccc\n" 12715 " anotherName:(NSString)dddddddddddddd {\n" 12716 "}", 12717 Style); 12718 12719 verifyFormat("- (int)sum:(vector<int>)numbers;"); 12720 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 12721 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 12722 // protocol lists (but not for template classes): 12723 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 12724 12725 verifyFormat("- (int (*)())foo:(int (*)())f;"); 12726 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 12727 12728 // If there's no return type (very rare in practice!), LLVM and Google style 12729 // agree. 12730 verifyFormat("- foo;"); 12731 verifyFormat("- foo:(int)f;"); 12732 verifyGoogleFormat("- foo:(int)foo;"); 12733 } 12734 12735 TEST_F(FormatTest, BreaksStringLiterals) { 12736 EXPECT_EQ("\"some text \"\n" 12737 "\"other\";", 12738 format("\"some text other\";", getLLVMStyleWithColumns(12))); 12739 EXPECT_EQ("\"some text \"\n" 12740 "\"other\";", 12741 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 12742 EXPECT_EQ( 12743 "#define A \\\n" 12744 " \"some \" \\\n" 12745 " \"text \" \\\n" 12746 " \"other\";", 12747 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 12748 EXPECT_EQ( 12749 "#define A \\\n" 12750 " \"so \" \\\n" 12751 " \"text \" \\\n" 12752 " \"other\";", 12753 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 12754 12755 EXPECT_EQ("\"some text\"", 12756 format("\"some text\"", getLLVMStyleWithColumns(1))); 12757 EXPECT_EQ("\"some text\"", 12758 format("\"some text\"", getLLVMStyleWithColumns(11))); 12759 EXPECT_EQ("\"some \"\n" 12760 "\"text\"", 12761 format("\"some text\"", getLLVMStyleWithColumns(10))); 12762 EXPECT_EQ("\"some \"\n" 12763 "\"text\"", 12764 format("\"some text\"", getLLVMStyleWithColumns(7))); 12765 EXPECT_EQ("\"some\"\n" 12766 "\" tex\"\n" 12767 "\"t\"", 12768 format("\"some text\"", getLLVMStyleWithColumns(6))); 12769 EXPECT_EQ("\"some\"\n" 12770 "\" tex\"\n" 12771 "\" and\"", 12772 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 12773 EXPECT_EQ("\"some\"\n" 12774 "\"/tex\"\n" 12775 "\"/and\"", 12776 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 12777 12778 EXPECT_EQ("variable =\n" 12779 " \"long string \"\n" 12780 " \"literal\";", 12781 format("variable = \"long string literal\";", 12782 getLLVMStyleWithColumns(20))); 12783 12784 EXPECT_EQ("variable = f(\n" 12785 " \"long string \"\n" 12786 " \"literal\",\n" 12787 " short,\n" 12788 " loooooooooooooooooooong);", 12789 format("variable = f(\"long string literal\", short, " 12790 "loooooooooooooooooooong);", 12791 getLLVMStyleWithColumns(20))); 12792 12793 EXPECT_EQ( 12794 "f(g(\"long string \"\n" 12795 " \"literal\"),\n" 12796 " b);", 12797 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 12798 EXPECT_EQ("f(g(\"long string \"\n" 12799 " \"literal\",\n" 12800 " a),\n" 12801 " b);", 12802 format("f(g(\"long string literal\", a), b);", 12803 getLLVMStyleWithColumns(20))); 12804 EXPECT_EQ( 12805 "f(\"one two\".split(\n" 12806 " variable));", 12807 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 12808 EXPECT_EQ("f(\"one two three four five six \"\n" 12809 " \"seven\".split(\n" 12810 " really_looooong_variable));", 12811 format("f(\"one two three four five six seven\"." 12812 "split(really_looooong_variable));", 12813 getLLVMStyleWithColumns(33))); 12814 12815 EXPECT_EQ("f(\"some \"\n" 12816 " \"text\",\n" 12817 " other);", 12818 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 12819 12820 // Only break as a last resort. 12821 verifyFormat( 12822 "aaaaaaaaaaaaaaaaaaaa(\n" 12823 " aaaaaaaaaaaaaaaaaaaa,\n" 12824 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 12825 12826 EXPECT_EQ("\"splitmea\"\n" 12827 "\"trandomp\"\n" 12828 "\"oint\"", 12829 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 12830 12831 EXPECT_EQ("\"split/\"\n" 12832 "\"pathat/\"\n" 12833 "\"slashes\"", 12834 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 12835 12836 EXPECT_EQ("\"split/\"\n" 12837 "\"pathat/\"\n" 12838 "\"slashes\"", 12839 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 12840 EXPECT_EQ("\"split at \"\n" 12841 "\"spaces/at/\"\n" 12842 "\"slashes.at.any$\"\n" 12843 "\"non-alphanumeric%\"\n" 12844 "\"1111111111characte\"\n" 12845 "\"rs\"", 12846 format("\"split at " 12847 "spaces/at/" 12848 "slashes.at." 12849 "any$non-" 12850 "alphanumeric%" 12851 "1111111111characte" 12852 "rs\"", 12853 getLLVMStyleWithColumns(20))); 12854 12855 // Verify that splitting the strings understands 12856 // Style::AlwaysBreakBeforeMultilineStrings. 12857 EXPECT_EQ("aaaaaaaaaaaa(\n" 12858 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 12859 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 12860 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 12861 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 12862 "aaaaaaaaaaaaaaaaaaaaaa\");", 12863 getGoogleStyle())); 12864 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 12865 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 12866 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 12867 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 12868 "aaaaaaaaaaaaaaaaaaaaaa\";", 12869 getGoogleStyle())); 12870 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 12871 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 12872 format("llvm::outs() << " 12873 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 12874 "aaaaaaaaaaaaaaaaaaa\";")); 12875 EXPECT_EQ("ffff(\n" 12876 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 12877 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 12878 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 12879 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 12880 getGoogleStyle())); 12881 12882 FormatStyle Style = getLLVMStyleWithColumns(12); 12883 Style.BreakStringLiterals = false; 12884 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 12885 12886 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 12887 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 12888 EXPECT_EQ("#define A \\\n" 12889 " \"some \" \\\n" 12890 " \"text \" \\\n" 12891 " \"other\";", 12892 format("#define A \"some text other\";", AlignLeft)); 12893 } 12894 12895 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) { 12896 EXPECT_EQ("C a = \"some more \"\n" 12897 " \"text\";", 12898 format("C a = \"some more text\";", getLLVMStyleWithColumns(18))); 12899 } 12900 12901 TEST_F(FormatTest, FullyRemoveEmptyLines) { 12902 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 12903 NoEmptyLines.MaxEmptyLinesToKeep = 0; 12904 EXPECT_EQ("int i = a(b());", 12905 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 12906 } 12907 12908 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 12909 EXPECT_EQ( 12910 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 12911 "(\n" 12912 " \"x\t\");", 12913 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 12914 "aaaaaaa(" 12915 "\"x\t\");")); 12916 } 12917 12918 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 12919 EXPECT_EQ( 12920 "u8\"utf8 string \"\n" 12921 "u8\"literal\";", 12922 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 12923 EXPECT_EQ( 12924 "u\"utf16 string \"\n" 12925 "u\"literal\";", 12926 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 12927 EXPECT_EQ( 12928 "U\"utf32 string \"\n" 12929 "U\"literal\";", 12930 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 12931 EXPECT_EQ("L\"wide string \"\n" 12932 "L\"literal\";", 12933 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 12934 EXPECT_EQ("@\"NSString \"\n" 12935 "@\"literal\";", 12936 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 12937 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26)); 12938 12939 // This input makes clang-format try to split the incomplete unicode escape 12940 // sequence, which used to lead to a crasher. 12941 verifyNoCrash( 12942 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 12943 getLLVMStyleWithColumns(60)); 12944 } 12945 12946 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 12947 FormatStyle Style = getGoogleStyleWithColumns(15); 12948 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 12949 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 12950 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 12951 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 12952 EXPECT_EQ("u8R\"x(raw literal)x\";", 12953 format("u8R\"x(raw literal)x\";", Style)); 12954 } 12955 12956 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 12957 FormatStyle Style = getLLVMStyleWithColumns(20); 12958 EXPECT_EQ( 12959 "_T(\"aaaaaaaaaaaaaa\")\n" 12960 "_T(\"aaaaaaaaaaaaaa\")\n" 12961 "_T(\"aaaaaaaaaaaa\")", 12962 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 12963 EXPECT_EQ("f(x,\n" 12964 " _T(\"aaaaaaaaaaaa\")\n" 12965 " _T(\"aaa\"),\n" 12966 " z);", 12967 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 12968 12969 // FIXME: Handle embedded spaces in one iteration. 12970 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 12971 // "_T(\"aaaaaaaaaaaaa\")\n" 12972 // "_T(\"aaaaaaaaaaaaa\")\n" 12973 // "_T(\"a\")", 12974 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 12975 // getLLVMStyleWithColumns(20))); 12976 EXPECT_EQ( 12977 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 12978 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 12979 EXPECT_EQ("f(\n" 12980 "#if !TEST\n" 12981 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 12982 "#endif\n" 12983 ");", 12984 format("f(\n" 12985 "#if !TEST\n" 12986 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 12987 "#endif\n" 12988 ");")); 12989 EXPECT_EQ("f(\n" 12990 "\n" 12991 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 12992 format("f(\n" 12993 "\n" 12994 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 12995 // Regression test for accessing tokens past the end of a vector in the 12996 // TokenLexer. 12997 verifyNoCrash(R"(_T( 12998 " 12999 ) 13000 )"); 13001 } 13002 13003 TEST_F(FormatTest, BreaksStringLiteralOperands) { 13004 // In a function call with two operands, the second can be broken with no line 13005 // break before it. 13006 EXPECT_EQ( 13007 "func(a, \"long long \"\n" 13008 " \"long long\");", 13009 format("func(a, \"long long long long\");", getLLVMStyleWithColumns(24))); 13010 // In a function call with three operands, the second must be broken with a 13011 // line break before it. 13012 EXPECT_EQ("func(a,\n" 13013 " \"long long long \"\n" 13014 " \"long\",\n" 13015 " c);", 13016 format("func(a, \"long long long long\", c);", 13017 getLLVMStyleWithColumns(24))); 13018 // In a function call with three operands, the third must be broken with a 13019 // line break before it. 13020 EXPECT_EQ("func(a, b,\n" 13021 " \"long long long \"\n" 13022 " \"long\");", 13023 format("func(a, b, \"long long long long\");", 13024 getLLVMStyleWithColumns(24))); 13025 // In a function call with three operands, both the second and the third must 13026 // be broken with a line break before them. 13027 EXPECT_EQ("func(a,\n" 13028 " \"long long long \"\n" 13029 " \"long\",\n" 13030 " \"long long long \"\n" 13031 " \"long\");", 13032 format("func(a, \"long long long long\", \"long long long long\");", 13033 getLLVMStyleWithColumns(24))); 13034 // In a chain of << with two operands, the second can be broken with no line 13035 // break before it. 13036 EXPECT_EQ("a << \"line line \"\n" 13037 " \"line\";", 13038 format("a << \"line line line\";", getLLVMStyleWithColumns(20))); 13039 // In a chain of << with three operands, the second can be broken with no line 13040 // break before it. 13041 EXPECT_EQ( 13042 "abcde << \"line \"\n" 13043 " \"line line\"\n" 13044 " << c;", 13045 format("abcde << \"line line line\" << c;", getLLVMStyleWithColumns(20))); 13046 // In a chain of << with three operands, the third must be broken with a line 13047 // break before it. 13048 EXPECT_EQ( 13049 "a << b\n" 13050 " << \"line line \"\n" 13051 " \"line\";", 13052 format("a << b << \"line line line\";", getLLVMStyleWithColumns(20))); 13053 // In a chain of << with three operands, the second can be broken with no line 13054 // break before it and the third must be broken with a line break before it. 13055 EXPECT_EQ("abcd << \"line line \"\n" 13056 " \"line\"\n" 13057 " << \"line line \"\n" 13058 " \"line\";", 13059 format("abcd << \"line line line\" << \"line line line\";", 13060 getLLVMStyleWithColumns(20))); 13061 // In a chain of binary operators with two operands, the second can be broken 13062 // with no line break before it. 13063 EXPECT_EQ( 13064 "abcd + \"line line \"\n" 13065 " \"line line\";", 13066 format("abcd + \"line line line line\";", getLLVMStyleWithColumns(20))); 13067 // In a chain of binary operators with three operands, the second must be 13068 // broken with a line break before it. 13069 EXPECT_EQ("abcd +\n" 13070 " \"line line \"\n" 13071 " \"line line\" +\n" 13072 " e;", 13073 format("abcd + \"line line line line\" + e;", 13074 getLLVMStyleWithColumns(20))); 13075 // In a function call with two operands, with AlignAfterOpenBracket enabled, 13076 // the first must be broken with a line break before it. 13077 FormatStyle Style = getLLVMStyleWithColumns(25); 13078 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 13079 EXPECT_EQ("someFunction(\n" 13080 " \"long long long \"\n" 13081 " \"long\",\n" 13082 " a);", 13083 format("someFunction(\"long long long long\", a);", Style)); 13084 } 13085 13086 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 13087 EXPECT_EQ( 13088 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 13089 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 13090 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 13091 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 13092 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 13093 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 13094 } 13095 13096 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 13097 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 13098 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 13099 EXPECT_EQ("fffffffffff(g(R\"x(\n" 13100 "multiline raw string literal xxxxxxxxxxxxxx\n" 13101 ")x\",\n" 13102 " a),\n" 13103 " b);", 13104 format("fffffffffff(g(R\"x(\n" 13105 "multiline raw string literal xxxxxxxxxxxxxx\n" 13106 ")x\", a), b);", 13107 getGoogleStyleWithColumns(20))); 13108 EXPECT_EQ("fffffffffff(\n" 13109 " g(R\"x(qqq\n" 13110 "multiline raw string literal xxxxxxxxxxxxxx\n" 13111 ")x\",\n" 13112 " a),\n" 13113 " b);", 13114 format("fffffffffff(g(R\"x(qqq\n" 13115 "multiline raw string literal xxxxxxxxxxxxxx\n" 13116 ")x\", a), b);", 13117 getGoogleStyleWithColumns(20))); 13118 13119 EXPECT_EQ("fffffffffff(R\"x(\n" 13120 "multiline raw string literal xxxxxxxxxxxxxx\n" 13121 ")x\");", 13122 format("fffffffffff(R\"x(\n" 13123 "multiline raw string literal xxxxxxxxxxxxxx\n" 13124 ")x\");", 13125 getGoogleStyleWithColumns(20))); 13126 EXPECT_EQ("fffffffffff(R\"x(\n" 13127 "multiline raw string literal xxxxxxxxxxxxxx\n" 13128 ")x\" + bbbbbb);", 13129 format("fffffffffff(R\"x(\n" 13130 "multiline raw string literal xxxxxxxxxxxxxx\n" 13131 ")x\" + bbbbbb);", 13132 getGoogleStyleWithColumns(20))); 13133 EXPECT_EQ("fffffffffff(\n" 13134 " R\"x(\n" 13135 "multiline raw string literal xxxxxxxxxxxxxx\n" 13136 ")x\" +\n" 13137 " bbbbbb);", 13138 format("fffffffffff(\n" 13139 " R\"x(\n" 13140 "multiline raw string literal xxxxxxxxxxxxxx\n" 13141 ")x\" + bbbbbb);", 13142 getGoogleStyleWithColumns(20))); 13143 EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);", 13144 format("fffffffffff(\n" 13145 " R\"(single line raw string)\" + bbbbbb);")); 13146 } 13147 13148 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 13149 verifyFormat("string a = \"unterminated;"); 13150 EXPECT_EQ("function(\"unterminated,\n" 13151 " OtherParameter);", 13152 format("function( \"unterminated,\n" 13153 " OtherParameter);")); 13154 } 13155 13156 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 13157 FormatStyle Style = getLLVMStyle(); 13158 Style.Standard = FormatStyle::LS_Cpp03; 13159 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 13160 format("#define x(_a) printf(\"foo\"_a);", Style)); 13161 } 13162 13163 TEST_F(FormatTest, CppLexVersion) { 13164 FormatStyle Style = getLLVMStyle(); 13165 // Formatting of x * y differs if x is a type. 13166 verifyFormat("void foo() { MACRO(a * b); }", Style); 13167 verifyFormat("void foo() { MACRO(int *b); }", Style); 13168 13169 // LLVM style uses latest lexer. 13170 verifyFormat("void foo() { MACRO(char8_t *b); }", Style); 13171 Style.Standard = FormatStyle::LS_Cpp17; 13172 // But in c++17, char8_t isn't a keyword. 13173 verifyFormat("void foo() { MACRO(char8_t * b); }", Style); 13174 } 13175 13176 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 13177 13178 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 13179 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 13180 " \"ddeeefff\");", 13181 format("someFunction(\"aaabbbcccdddeeefff\");", 13182 getLLVMStyleWithColumns(25))); 13183 EXPECT_EQ("someFunction1234567890(\n" 13184 " \"aaabbbcccdddeeefff\");", 13185 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 13186 getLLVMStyleWithColumns(26))); 13187 EXPECT_EQ("someFunction1234567890(\n" 13188 " \"aaabbbcccdddeeeff\"\n" 13189 " \"f\");", 13190 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 13191 getLLVMStyleWithColumns(25))); 13192 EXPECT_EQ("someFunction1234567890(\n" 13193 " \"aaabbbcccdddeeeff\"\n" 13194 " \"f\");", 13195 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 13196 getLLVMStyleWithColumns(24))); 13197 EXPECT_EQ("someFunction(\n" 13198 " \"aaabbbcc ddde \"\n" 13199 " \"efff\");", 13200 format("someFunction(\"aaabbbcc ddde efff\");", 13201 getLLVMStyleWithColumns(25))); 13202 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 13203 " \"ddeeefff\");", 13204 format("someFunction(\"aaabbbccc ddeeefff\");", 13205 getLLVMStyleWithColumns(25))); 13206 EXPECT_EQ("someFunction1234567890(\n" 13207 " \"aaabb \"\n" 13208 " \"cccdddeeefff\");", 13209 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 13210 getLLVMStyleWithColumns(25))); 13211 EXPECT_EQ("#define A \\\n" 13212 " string s = \\\n" 13213 " \"123456789\" \\\n" 13214 " \"0\"; \\\n" 13215 " int i;", 13216 format("#define A string s = \"1234567890\"; int i;", 13217 getLLVMStyleWithColumns(20))); 13218 EXPECT_EQ("someFunction(\n" 13219 " \"aaabbbcc \"\n" 13220 " \"dddeeefff\");", 13221 format("someFunction(\"aaabbbcc dddeeefff\");", 13222 getLLVMStyleWithColumns(25))); 13223 } 13224 13225 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 13226 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 13227 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 13228 EXPECT_EQ("\"test\"\n" 13229 "\"\\n\"", 13230 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 13231 EXPECT_EQ("\"tes\\\\\"\n" 13232 "\"n\"", 13233 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 13234 EXPECT_EQ("\"\\\\\\\\\"\n" 13235 "\"\\n\"", 13236 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 13237 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 13238 EXPECT_EQ("\"\\uff01\"\n" 13239 "\"test\"", 13240 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 13241 EXPECT_EQ("\"\\Uff01ff02\"", 13242 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 13243 EXPECT_EQ("\"\\x000000000001\"\n" 13244 "\"next\"", 13245 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 13246 EXPECT_EQ("\"\\x000000000001next\"", 13247 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 13248 EXPECT_EQ("\"\\x000000000001\"", 13249 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 13250 EXPECT_EQ("\"test\"\n" 13251 "\"\\000000\"\n" 13252 "\"000001\"", 13253 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 13254 EXPECT_EQ("\"test\\000\"\n" 13255 "\"00000000\"\n" 13256 "\"1\"", 13257 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 13258 } 13259 13260 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 13261 verifyFormat("void f() {\n" 13262 " return g() {}\n" 13263 " void h() {}"); 13264 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 13265 "g();\n" 13266 "}"); 13267 } 13268 13269 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 13270 verifyFormat( 13271 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 13272 } 13273 13274 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 13275 verifyFormat("class X {\n" 13276 " void f() {\n" 13277 " }\n" 13278 "};", 13279 getLLVMStyleWithColumns(12)); 13280 } 13281 13282 TEST_F(FormatTest, ConfigurableIndentWidth) { 13283 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 13284 EightIndent.IndentWidth = 8; 13285 EightIndent.ContinuationIndentWidth = 8; 13286 verifyFormat("void f() {\n" 13287 " someFunction();\n" 13288 " if (true) {\n" 13289 " f();\n" 13290 " }\n" 13291 "}", 13292 EightIndent); 13293 verifyFormat("class X {\n" 13294 " void f() {\n" 13295 " }\n" 13296 "};", 13297 EightIndent); 13298 verifyFormat("int x[] = {\n" 13299 " call(),\n" 13300 " call()};", 13301 EightIndent); 13302 } 13303 13304 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 13305 verifyFormat("double\n" 13306 "f();", 13307 getLLVMStyleWithColumns(8)); 13308 } 13309 13310 TEST_F(FormatTest, ConfigurableUseOfTab) { 13311 FormatStyle Tab = getLLVMStyleWithColumns(42); 13312 Tab.IndentWidth = 8; 13313 Tab.UseTab = FormatStyle::UT_Always; 13314 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left; 13315 13316 EXPECT_EQ("if (aaaaaaaa && // q\n" 13317 " bb)\t\t// w\n" 13318 "\t;", 13319 format("if (aaaaaaaa &&// q\n" 13320 "bb)// w\n" 13321 ";", 13322 Tab)); 13323 EXPECT_EQ("if (aaa && bbb) // w\n" 13324 "\t;", 13325 format("if(aaa&&bbb)// w\n" 13326 ";", 13327 Tab)); 13328 13329 verifyFormat("class X {\n" 13330 "\tvoid f() {\n" 13331 "\t\tsomeFunction(parameter1,\n" 13332 "\t\t\t parameter2);\n" 13333 "\t}\n" 13334 "};", 13335 Tab); 13336 verifyFormat("#define A \\\n" 13337 "\tvoid f() { \\\n" 13338 "\t\tsomeFunction( \\\n" 13339 "\t\t parameter1, \\\n" 13340 "\t\t parameter2); \\\n" 13341 "\t}", 13342 Tab); 13343 verifyFormat("int a;\t // x\n" 13344 "int bbbbbbbb; // x\n", 13345 Tab); 13346 13347 Tab.TabWidth = 4; 13348 Tab.IndentWidth = 8; 13349 verifyFormat("class TabWidth4Indent8 {\n" 13350 "\t\tvoid f() {\n" 13351 "\t\t\t\tsomeFunction(parameter1,\n" 13352 "\t\t\t\t\t\t\t parameter2);\n" 13353 "\t\t}\n" 13354 "};", 13355 Tab); 13356 13357 Tab.TabWidth = 4; 13358 Tab.IndentWidth = 4; 13359 verifyFormat("class TabWidth4Indent4 {\n" 13360 "\tvoid f() {\n" 13361 "\t\tsomeFunction(parameter1,\n" 13362 "\t\t\t\t\t parameter2);\n" 13363 "\t}\n" 13364 "};", 13365 Tab); 13366 13367 Tab.TabWidth = 8; 13368 Tab.IndentWidth = 4; 13369 verifyFormat("class TabWidth8Indent4 {\n" 13370 " void f() {\n" 13371 "\tsomeFunction(parameter1,\n" 13372 "\t\t parameter2);\n" 13373 " }\n" 13374 "};", 13375 Tab); 13376 13377 Tab.TabWidth = 8; 13378 Tab.IndentWidth = 8; 13379 EXPECT_EQ("/*\n" 13380 "\t a\t\tcomment\n" 13381 "\t in multiple lines\n" 13382 " */", 13383 format(" /*\t \t \n" 13384 " \t \t a\t\tcomment\t \t\n" 13385 " \t \t in multiple lines\t\n" 13386 " \t */", 13387 Tab)); 13388 13389 Tab.UseTab = FormatStyle::UT_ForIndentation; 13390 verifyFormat("{\n" 13391 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13392 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13393 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13394 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13395 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13396 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13397 "};", 13398 Tab); 13399 verifyFormat("enum AA {\n" 13400 "\ta1, // Force multiple lines\n" 13401 "\ta2,\n" 13402 "\ta3\n" 13403 "};", 13404 Tab); 13405 EXPECT_EQ("if (aaaaaaaa && // q\n" 13406 " bb) // w\n" 13407 "\t;", 13408 format("if (aaaaaaaa &&// q\n" 13409 "bb)// w\n" 13410 ";", 13411 Tab)); 13412 verifyFormat("class X {\n" 13413 "\tvoid f() {\n" 13414 "\t\tsomeFunction(parameter1,\n" 13415 "\t\t parameter2);\n" 13416 "\t}\n" 13417 "};", 13418 Tab); 13419 verifyFormat("{\n" 13420 "\tQ(\n" 13421 "\t {\n" 13422 "\t\t int a;\n" 13423 "\t\t someFunction(aaaaaaaa,\n" 13424 "\t\t bbbbbbb);\n" 13425 "\t },\n" 13426 "\t p);\n" 13427 "}", 13428 Tab); 13429 EXPECT_EQ("{\n" 13430 "\t/* aaaa\n" 13431 "\t bbbb */\n" 13432 "}", 13433 format("{\n" 13434 "/* aaaa\n" 13435 " bbbb */\n" 13436 "}", 13437 Tab)); 13438 EXPECT_EQ("{\n" 13439 "\t/*\n" 13440 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13441 "\t bbbbbbbbbbbbb\n" 13442 "\t*/\n" 13443 "}", 13444 format("{\n" 13445 "/*\n" 13446 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13447 "*/\n" 13448 "}", 13449 Tab)); 13450 EXPECT_EQ("{\n" 13451 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13452 "\t// bbbbbbbbbbbbb\n" 13453 "}", 13454 format("{\n" 13455 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13456 "}", 13457 Tab)); 13458 EXPECT_EQ("{\n" 13459 "\t/*\n" 13460 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13461 "\t bbbbbbbbbbbbb\n" 13462 "\t*/\n" 13463 "}", 13464 format("{\n" 13465 "\t/*\n" 13466 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13467 "\t*/\n" 13468 "}", 13469 Tab)); 13470 EXPECT_EQ("{\n" 13471 "\t/*\n" 13472 "\n" 13473 "\t*/\n" 13474 "}", 13475 format("{\n" 13476 "\t/*\n" 13477 "\n" 13478 "\t*/\n" 13479 "}", 13480 Tab)); 13481 EXPECT_EQ("{\n" 13482 "\t/*\n" 13483 " asdf\n" 13484 "\t*/\n" 13485 "}", 13486 format("{\n" 13487 "\t/*\n" 13488 " asdf\n" 13489 "\t*/\n" 13490 "}", 13491 Tab)); 13492 13493 verifyFormat("void f() {\n" 13494 "\treturn true ? aaaaaaaaaaaaaaaaaa\n" 13495 "\t : bbbbbbbbbbbbbbbbbb\n" 13496 "}", 13497 Tab); 13498 FormatStyle TabNoBreak = Tab; 13499 TabNoBreak.BreakBeforeTernaryOperators = false; 13500 verifyFormat("void f() {\n" 13501 "\treturn true ? aaaaaaaaaaaaaaaaaa :\n" 13502 "\t bbbbbbbbbbbbbbbbbb\n" 13503 "}", 13504 TabNoBreak); 13505 verifyFormat("void f() {\n" 13506 "\treturn true ?\n" 13507 "\t aaaaaaaaaaaaaaaaaaaa :\n" 13508 "\t bbbbbbbbbbbbbbbbbbbb\n" 13509 "}", 13510 TabNoBreak); 13511 13512 Tab.UseTab = FormatStyle::UT_Never; 13513 EXPECT_EQ("/*\n" 13514 " a\t\tcomment\n" 13515 " in multiple lines\n" 13516 " */", 13517 format(" /*\t \t \n" 13518 " \t \t a\t\tcomment\t \t\n" 13519 " \t \t in multiple lines\t\n" 13520 " \t */", 13521 Tab)); 13522 EXPECT_EQ("/* some\n" 13523 " comment */", 13524 format(" \t \t /* some\n" 13525 " \t \t comment */", 13526 Tab)); 13527 EXPECT_EQ("int a; /* some\n" 13528 " comment */", 13529 format(" \t \t int a; /* some\n" 13530 " \t \t comment */", 13531 Tab)); 13532 13533 EXPECT_EQ("int a; /* some\n" 13534 "comment */", 13535 format(" \t \t int\ta; /* some\n" 13536 " \t \t comment */", 13537 Tab)); 13538 EXPECT_EQ("f(\"\t\t\"); /* some\n" 13539 " comment */", 13540 format(" \t \t f(\"\t\t\"); /* some\n" 13541 " \t \t comment */", 13542 Tab)); 13543 EXPECT_EQ("{\n" 13544 " /*\n" 13545 " * Comment\n" 13546 " */\n" 13547 " int i;\n" 13548 "}", 13549 format("{\n" 13550 "\t/*\n" 13551 "\t * Comment\n" 13552 "\t */\n" 13553 "\t int i;\n" 13554 "}", 13555 Tab)); 13556 13557 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 13558 Tab.TabWidth = 8; 13559 Tab.IndentWidth = 8; 13560 EXPECT_EQ("if (aaaaaaaa && // q\n" 13561 " bb) // w\n" 13562 "\t;", 13563 format("if (aaaaaaaa &&// q\n" 13564 "bb)// w\n" 13565 ";", 13566 Tab)); 13567 EXPECT_EQ("if (aaa && bbb) // w\n" 13568 "\t;", 13569 format("if(aaa&&bbb)// w\n" 13570 ";", 13571 Tab)); 13572 verifyFormat("class X {\n" 13573 "\tvoid f() {\n" 13574 "\t\tsomeFunction(parameter1,\n" 13575 "\t\t\t parameter2);\n" 13576 "\t}\n" 13577 "};", 13578 Tab); 13579 verifyFormat("#define A \\\n" 13580 "\tvoid f() { \\\n" 13581 "\t\tsomeFunction( \\\n" 13582 "\t\t parameter1, \\\n" 13583 "\t\t parameter2); \\\n" 13584 "\t}", 13585 Tab); 13586 Tab.TabWidth = 4; 13587 Tab.IndentWidth = 8; 13588 verifyFormat("class TabWidth4Indent8 {\n" 13589 "\t\tvoid f() {\n" 13590 "\t\t\t\tsomeFunction(parameter1,\n" 13591 "\t\t\t\t\t\t\t parameter2);\n" 13592 "\t\t}\n" 13593 "};", 13594 Tab); 13595 Tab.TabWidth = 4; 13596 Tab.IndentWidth = 4; 13597 verifyFormat("class TabWidth4Indent4 {\n" 13598 "\tvoid f() {\n" 13599 "\t\tsomeFunction(parameter1,\n" 13600 "\t\t\t\t\t parameter2);\n" 13601 "\t}\n" 13602 "};", 13603 Tab); 13604 Tab.TabWidth = 8; 13605 Tab.IndentWidth = 4; 13606 verifyFormat("class TabWidth8Indent4 {\n" 13607 " void f() {\n" 13608 "\tsomeFunction(parameter1,\n" 13609 "\t\t parameter2);\n" 13610 " }\n" 13611 "};", 13612 Tab); 13613 Tab.TabWidth = 8; 13614 Tab.IndentWidth = 8; 13615 EXPECT_EQ("/*\n" 13616 "\t a\t\tcomment\n" 13617 "\t in multiple lines\n" 13618 " */", 13619 format(" /*\t \t \n" 13620 " \t \t a\t\tcomment\t \t\n" 13621 " \t \t in multiple lines\t\n" 13622 " \t */", 13623 Tab)); 13624 verifyFormat("{\n" 13625 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13626 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13627 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13628 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13629 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13630 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13631 "};", 13632 Tab); 13633 verifyFormat("enum AA {\n" 13634 "\ta1, // Force multiple lines\n" 13635 "\ta2,\n" 13636 "\ta3\n" 13637 "};", 13638 Tab); 13639 EXPECT_EQ("if (aaaaaaaa && // q\n" 13640 " bb) // w\n" 13641 "\t;", 13642 format("if (aaaaaaaa &&// q\n" 13643 "bb)// w\n" 13644 ";", 13645 Tab)); 13646 verifyFormat("class X {\n" 13647 "\tvoid f() {\n" 13648 "\t\tsomeFunction(parameter1,\n" 13649 "\t\t\t parameter2);\n" 13650 "\t}\n" 13651 "};", 13652 Tab); 13653 verifyFormat("{\n" 13654 "\tQ(\n" 13655 "\t {\n" 13656 "\t\t int a;\n" 13657 "\t\t someFunction(aaaaaaaa,\n" 13658 "\t\t\t\t bbbbbbb);\n" 13659 "\t },\n" 13660 "\t p);\n" 13661 "}", 13662 Tab); 13663 EXPECT_EQ("{\n" 13664 "\t/* aaaa\n" 13665 "\t bbbb */\n" 13666 "}", 13667 format("{\n" 13668 "/* aaaa\n" 13669 " bbbb */\n" 13670 "}", 13671 Tab)); 13672 EXPECT_EQ("{\n" 13673 "\t/*\n" 13674 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13675 "\t bbbbbbbbbbbbb\n" 13676 "\t*/\n" 13677 "}", 13678 format("{\n" 13679 "/*\n" 13680 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13681 "*/\n" 13682 "}", 13683 Tab)); 13684 EXPECT_EQ("{\n" 13685 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13686 "\t// bbbbbbbbbbbbb\n" 13687 "}", 13688 format("{\n" 13689 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13690 "}", 13691 Tab)); 13692 EXPECT_EQ("{\n" 13693 "\t/*\n" 13694 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13695 "\t bbbbbbbbbbbbb\n" 13696 "\t*/\n" 13697 "}", 13698 format("{\n" 13699 "\t/*\n" 13700 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13701 "\t*/\n" 13702 "}", 13703 Tab)); 13704 EXPECT_EQ("{\n" 13705 "\t/*\n" 13706 "\n" 13707 "\t*/\n" 13708 "}", 13709 format("{\n" 13710 "\t/*\n" 13711 "\n" 13712 "\t*/\n" 13713 "}", 13714 Tab)); 13715 EXPECT_EQ("{\n" 13716 "\t/*\n" 13717 " asdf\n" 13718 "\t*/\n" 13719 "}", 13720 format("{\n" 13721 "\t/*\n" 13722 " asdf\n" 13723 "\t*/\n" 13724 "}", 13725 Tab)); 13726 EXPECT_EQ("/* some\n" 13727 " comment */", 13728 format(" \t \t /* some\n" 13729 " \t \t comment */", 13730 Tab)); 13731 EXPECT_EQ("int a; /* some\n" 13732 " comment */", 13733 format(" \t \t int a; /* some\n" 13734 " \t \t comment */", 13735 Tab)); 13736 EXPECT_EQ("int a; /* some\n" 13737 "comment */", 13738 format(" \t \t int\ta; /* some\n" 13739 " \t \t comment */", 13740 Tab)); 13741 EXPECT_EQ("f(\"\t\t\"); /* some\n" 13742 " comment */", 13743 format(" \t \t f(\"\t\t\"); /* some\n" 13744 " \t \t comment */", 13745 Tab)); 13746 EXPECT_EQ("{\n" 13747 "\t/*\n" 13748 "\t * Comment\n" 13749 "\t */\n" 13750 "\tint i;\n" 13751 "}", 13752 format("{\n" 13753 "\t/*\n" 13754 "\t * Comment\n" 13755 "\t */\n" 13756 "\t int i;\n" 13757 "}", 13758 Tab)); 13759 Tab.TabWidth = 2; 13760 Tab.IndentWidth = 2; 13761 EXPECT_EQ("{\n" 13762 "\t/* aaaa\n" 13763 "\t\t bbbb */\n" 13764 "}", 13765 format("{\n" 13766 "/* aaaa\n" 13767 "\t bbbb */\n" 13768 "}", 13769 Tab)); 13770 EXPECT_EQ("{\n" 13771 "\t/*\n" 13772 "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13773 "\t\tbbbbbbbbbbbbb\n" 13774 "\t*/\n" 13775 "}", 13776 format("{\n" 13777 "/*\n" 13778 "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13779 "*/\n" 13780 "}", 13781 Tab)); 13782 Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 13783 Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 13784 Tab.TabWidth = 4; 13785 Tab.IndentWidth = 4; 13786 verifyFormat("class Assign {\n" 13787 "\tvoid f() {\n" 13788 "\t\tint x = 123;\n" 13789 "\t\tint random = 4;\n" 13790 "\t\tstd::string alphabet =\n" 13791 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 13792 "\t}\n" 13793 "};", 13794 Tab); 13795 13796 Tab.UseTab = FormatStyle::UT_AlignWithSpaces; 13797 Tab.TabWidth = 8; 13798 Tab.IndentWidth = 8; 13799 EXPECT_EQ("if (aaaaaaaa && // q\n" 13800 " bb) // w\n" 13801 "\t;", 13802 format("if (aaaaaaaa &&// q\n" 13803 "bb)// w\n" 13804 ";", 13805 Tab)); 13806 EXPECT_EQ("if (aaa && bbb) // w\n" 13807 "\t;", 13808 format("if(aaa&&bbb)// w\n" 13809 ";", 13810 Tab)); 13811 verifyFormat("class X {\n" 13812 "\tvoid f() {\n" 13813 "\t\tsomeFunction(parameter1,\n" 13814 "\t\t parameter2);\n" 13815 "\t}\n" 13816 "};", 13817 Tab); 13818 verifyFormat("#define A \\\n" 13819 "\tvoid f() { \\\n" 13820 "\t\tsomeFunction( \\\n" 13821 "\t\t parameter1, \\\n" 13822 "\t\t parameter2); \\\n" 13823 "\t}", 13824 Tab); 13825 Tab.TabWidth = 4; 13826 Tab.IndentWidth = 8; 13827 verifyFormat("class TabWidth4Indent8 {\n" 13828 "\t\tvoid f() {\n" 13829 "\t\t\t\tsomeFunction(parameter1,\n" 13830 "\t\t\t\t parameter2);\n" 13831 "\t\t}\n" 13832 "};", 13833 Tab); 13834 Tab.TabWidth = 4; 13835 Tab.IndentWidth = 4; 13836 verifyFormat("class TabWidth4Indent4 {\n" 13837 "\tvoid f() {\n" 13838 "\t\tsomeFunction(parameter1,\n" 13839 "\t\t parameter2);\n" 13840 "\t}\n" 13841 "};", 13842 Tab); 13843 Tab.TabWidth = 8; 13844 Tab.IndentWidth = 4; 13845 verifyFormat("class TabWidth8Indent4 {\n" 13846 " void f() {\n" 13847 "\tsomeFunction(parameter1,\n" 13848 "\t parameter2);\n" 13849 " }\n" 13850 "};", 13851 Tab); 13852 Tab.TabWidth = 8; 13853 Tab.IndentWidth = 8; 13854 EXPECT_EQ("/*\n" 13855 " a\t\tcomment\n" 13856 " in multiple lines\n" 13857 " */", 13858 format(" /*\t \t \n" 13859 " \t \t a\t\tcomment\t \t\n" 13860 " \t \t in multiple lines\t\n" 13861 " \t */", 13862 Tab)); 13863 verifyFormat("{\n" 13864 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13865 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13866 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13867 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13868 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13869 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13870 "};", 13871 Tab); 13872 verifyFormat("enum AA {\n" 13873 "\ta1, // Force multiple lines\n" 13874 "\ta2,\n" 13875 "\ta3\n" 13876 "};", 13877 Tab); 13878 EXPECT_EQ("if (aaaaaaaa && // q\n" 13879 " bb) // w\n" 13880 "\t;", 13881 format("if (aaaaaaaa &&// q\n" 13882 "bb)// w\n" 13883 ";", 13884 Tab)); 13885 verifyFormat("class X {\n" 13886 "\tvoid f() {\n" 13887 "\t\tsomeFunction(parameter1,\n" 13888 "\t\t parameter2);\n" 13889 "\t}\n" 13890 "};", 13891 Tab); 13892 verifyFormat("{\n" 13893 "\tQ(\n" 13894 "\t {\n" 13895 "\t\t int a;\n" 13896 "\t\t someFunction(aaaaaaaa,\n" 13897 "\t\t bbbbbbb);\n" 13898 "\t },\n" 13899 "\t p);\n" 13900 "}", 13901 Tab); 13902 EXPECT_EQ("{\n" 13903 "\t/* aaaa\n" 13904 "\t bbbb */\n" 13905 "}", 13906 format("{\n" 13907 "/* aaaa\n" 13908 " bbbb */\n" 13909 "}", 13910 Tab)); 13911 EXPECT_EQ("{\n" 13912 "\t/*\n" 13913 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13914 "\t bbbbbbbbbbbbb\n" 13915 "\t*/\n" 13916 "}", 13917 format("{\n" 13918 "/*\n" 13919 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13920 "*/\n" 13921 "}", 13922 Tab)); 13923 EXPECT_EQ("{\n" 13924 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13925 "\t// bbbbbbbbbbbbb\n" 13926 "}", 13927 format("{\n" 13928 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13929 "}", 13930 Tab)); 13931 EXPECT_EQ("{\n" 13932 "\t/*\n" 13933 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13934 "\t bbbbbbbbbbbbb\n" 13935 "\t*/\n" 13936 "}", 13937 format("{\n" 13938 "\t/*\n" 13939 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13940 "\t*/\n" 13941 "}", 13942 Tab)); 13943 EXPECT_EQ("{\n" 13944 "\t/*\n" 13945 "\n" 13946 "\t*/\n" 13947 "}", 13948 format("{\n" 13949 "\t/*\n" 13950 "\n" 13951 "\t*/\n" 13952 "}", 13953 Tab)); 13954 EXPECT_EQ("{\n" 13955 "\t/*\n" 13956 " asdf\n" 13957 "\t*/\n" 13958 "}", 13959 format("{\n" 13960 "\t/*\n" 13961 " asdf\n" 13962 "\t*/\n" 13963 "}", 13964 Tab)); 13965 EXPECT_EQ("/* some\n" 13966 " comment */", 13967 format(" \t \t /* some\n" 13968 " \t \t comment */", 13969 Tab)); 13970 EXPECT_EQ("int a; /* some\n" 13971 " comment */", 13972 format(" \t \t int a; /* some\n" 13973 " \t \t comment */", 13974 Tab)); 13975 EXPECT_EQ("int a; /* some\n" 13976 "comment */", 13977 format(" \t \t int\ta; /* some\n" 13978 " \t \t comment */", 13979 Tab)); 13980 EXPECT_EQ("f(\"\t\t\"); /* some\n" 13981 " comment */", 13982 format(" \t \t f(\"\t\t\"); /* some\n" 13983 " \t \t comment */", 13984 Tab)); 13985 EXPECT_EQ("{\n" 13986 "\t/*\n" 13987 "\t * Comment\n" 13988 "\t */\n" 13989 "\tint i;\n" 13990 "}", 13991 format("{\n" 13992 "\t/*\n" 13993 "\t * Comment\n" 13994 "\t */\n" 13995 "\t int i;\n" 13996 "}", 13997 Tab)); 13998 Tab.TabWidth = 2; 13999 Tab.IndentWidth = 2; 14000 EXPECT_EQ("{\n" 14001 "\t/* aaaa\n" 14002 "\t bbbb */\n" 14003 "}", 14004 format("{\n" 14005 "/* aaaa\n" 14006 " bbbb */\n" 14007 "}", 14008 Tab)); 14009 EXPECT_EQ("{\n" 14010 "\t/*\n" 14011 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14012 "\t bbbbbbbbbbbbb\n" 14013 "\t*/\n" 14014 "}", 14015 format("{\n" 14016 "/*\n" 14017 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14018 "*/\n" 14019 "}", 14020 Tab)); 14021 Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 14022 Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 14023 Tab.TabWidth = 4; 14024 Tab.IndentWidth = 4; 14025 verifyFormat("class Assign {\n" 14026 "\tvoid f() {\n" 14027 "\t\tint x = 123;\n" 14028 "\t\tint random = 4;\n" 14029 "\t\tstd::string alphabet =\n" 14030 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 14031 "\t}\n" 14032 "};", 14033 Tab); 14034 Tab.AlignOperands = FormatStyle::OAS_Align; 14035 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n" 14036 " cccccccccccccccccccc;", 14037 Tab); 14038 // no alignment 14039 verifyFormat("int aaaaaaaaaa =\n" 14040 "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 14041 Tab); 14042 verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n" 14043 " : bbbbbbbbbbbbbb ? 222222222222222\n" 14044 " : 333333333333333;", 14045 Tab); 14046 Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 14047 Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator; 14048 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n" 14049 " + cccccccccccccccccccc;", 14050 Tab); 14051 } 14052 14053 TEST_F(FormatTest, ZeroTabWidth) { 14054 FormatStyle Tab = getLLVMStyleWithColumns(42); 14055 Tab.IndentWidth = 8; 14056 Tab.UseTab = FormatStyle::UT_Never; 14057 Tab.TabWidth = 0; 14058 EXPECT_EQ("void a(){\n" 14059 " // line starts with '\t'\n" 14060 "};", 14061 format("void a(){\n" 14062 "\t// line starts with '\t'\n" 14063 "};", 14064 Tab)); 14065 14066 EXPECT_EQ("void a(){\n" 14067 " // line starts with '\t'\n" 14068 "};", 14069 format("void a(){\n" 14070 "\t\t// line starts with '\t'\n" 14071 "};", 14072 Tab)); 14073 14074 Tab.UseTab = FormatStyle::UT_ForIndentation; 14075 EXPECT_EQ("void a(){\n" 14076 " // line starts with '\t'\n" 14077 "};", 14078 format("void a(){\n" 14079 "\t// line starts with '\t'\n" 14080 "};", 14081 Tab)); 14082 14083 EXPECT_EQ("void a(){\n" 14084 " // line starts with '\t'\n" 14085 "};", 14086 format("void a(){\n" 14087 "\t\t// line starts with '\t'\n" 14088 "};", 14089 Tab)); 14090 14091 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 14092 EXPECT_EQ("void a(){\n" 14093 " // line starts with '\t'\n" 14094 "};", 14095 format("void a(){\n" 14096 "\t// line starts with '\t'\n" 14097 "};", 14098 Tab)); 14099 14100 EXPECT_EQ("void a(){\n" 14101 " // line starts with '\t'\n" 14102 "};", 14103 format("void a(){\n" 14104 "\t\t// line starts with '\t'\n" 14105 "};", 14106 Tab)); 14107 14108 Tab.UseTab = FormatStyle::UT_AlignWithSpaces; 14109 EXPECT_EQ("void a(){\n" 14110 " // line starts with '\t'\n" 14111 "};", 14112 format("void a(){\n" 14113 "\t// line starts with '\t'\n" 14114 "};", 14115 Tab)); 14116 14117 EXPECT_EQ("void a(){\n" 14118 " // line starts with '\t'\n" 14119 "};", 14120 format("void a(){\n" 14121 "\t\t// line starts with '\t'\n" 14122 "};", 14123 Tab)); 14124 14125 Tab.UseTab = FormatStyle::UT_Always; 14126 EXPECT_EQ("void a(){\n" 14127 "// line starts with '\t'\n" 14128 "};", 14129 format("void a(){\n" 14130 "\t// line starts with '\t'\n" 14131 "};", 14132 Tab)); 14133 14134 EXPECT_EQ("void a(){\n" 14135 "// line starts with '\t'\n" 14136 "};", 14137 format("void a(){\n" 14138 "\t\t// line starts with '\t'\n" 14139 "};", 14140 Tab)); 14141 } 14142 14143 TEST_F(FormatTest, CalculatesOriginalColumn) { 14144 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14145 "q\"; /* some\n" 14146 " comment */", 14147 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14148 "q\"; /* some\n" 14149 " comment */", 14150 getLLVMStyle())); 14151 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 14152 "/* some\n" 14153 " comment */", 14154 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 14155 " /* some\n" 14156 " comment */", 14157 getLLVMStyle())); 14158 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14159 "qqq\n" 14160 "/* some\n" 14161 " comment */", 14162 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14163 "qqq\n" 14164 " /* some\n" 14165 " comment */", 14166 getLLVMStyle())); 14167 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14168 "wwww; /* some\n" 14169 " comment */", 14170 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14171 "wwww; /* some\n" 14172 " comment */", 14173 getLLVMStyle())); 14174 } 14175 14176 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 14177 FormatStyle NoSpace = getLLVMStyle(); 14178 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 14179 14180 verifyFormat("while(true)\n" 14181 " continue;", 14182 NoSpace); 14183 verifyFormat("for(;;)\n" 14184 " continue;", 14185 NoSpace); 14186 verifyFormat("if(true)\n" 14187 " f();\n" 14188 "else if(true)\n" 14189 " f();", 14190 NoSpace); 14191 verifyFormat("do {\n" 14192 " do_something();\n" 14193 "} while(something());", 14194 NoSpace); 14195 verifyFormat("switch(x) {\n" 14196 "default:\n" 14197 " break;\n" 14198 "}", 14199 NoSpace); 14200 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 14201 verifyFormat("size_t x = sizeof(x);", NoSpace); 14202 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 14203 verifyFormat("auto f(int x) -> typeof(x);", NoSpace); 14204 verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace); 14205 verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace); 14206 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 14207 verifyFormat("alignas(128) char a[128];", NoSpace); 14208 verifyFormat("size_t x = alignof(MyType);", NoSpace); 14209 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 14210 verifyFormat("int f() throw(Deprecated);", NoSpace); 14211 verifyFormat("typedef void (*cb)(int);", NoSpace); 14212 verifyFormat("T A::operator()();", NoSpace); 14213 verifyFormat("X A::operator++(T);", NoSpace); 14214 verifyFormat("auto lambda = []() { return 0; };", NoSpace); 14215 14216 FormatStyle Space = getLLVMStyle(); 14217 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 14218 14219 verifyFormat("int f ();", Space); 14220 verifyFormat("void f (int a, T b) {\n" 14221 " while (true)\n" 14222 " continue;\n" 14223 "}", 14224 Space); 14225 verifyFormat("if (true)\n" 14226 " f ();\n" 14227 "else if (true)\n" 14228 " f ();", 14229 Space); 14230 verifyFormat("do {\n" 14231 " do_something ();\n" 14232 "} while (something ());", 14233 Space); 14234 verifyFormat("switch (x) {\n" 14235 "default:\n" 14236 " break;\n" 14237 "}", 14238 Space); 14239 verifyFormat("A::A () : a (1) {}", Space); 14240 verifyFormat("void f () __attribute__ ((asdf));", Space); 14241 verifyFormat("*(&a + 1);\n" 14242 "&((&a)[1]);\n" 14243 "a[(b + c) * d];\n" 14244 "(((a + 1) * 2) + 3) * 4;", 14245 Space); 14246 verifyFormat("#define A(x) x", Space); 14247 verifyFormat("#define A (x) x", Space); 14248 verifyFormat("#if defined(x)\n" 14249 "#endif", 14250 Space); 14251 verifyFormat("auto i = std::make_unique<int> (5);", Space); 14252 verifyFormat("size_t x = sizeof (x);", Space); 14253 verifyFormat("auto f (int x) -> decltype (x);", Space); 14254 verifyFormat("auto f (int x) -> typeof (x);", Space); 14255 verifyFormat("auto f (int x) -> _Atomic (x);", Space); 14256 verifyFormat("auto f (int x) -> __underlying_type (x);", Space); 14257 verifyFormat("int f (T x) noexcept (x.create ());", Space); 14258 verifyFormat("alignas (128) char a[128];", Space); 14259 verifyFormat("size_t x = alignof (MyType);", Space); 14260 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 14261 verifyFormat("int f () throw (Deprecated);", Space); 14262 verifyFormat("typedef void (*cb) (int);", Space); 14263 // FIXME these tests regressed behaviour. 14264 // verifyFormat("T A::operator() ();", Space); 14265 // verifyFormat("X A::operator++ (T);", Space); 14266 verifyFormat("auto lambda = [] () { return 0; };", Space); 14267 verifyFormat("int x = int (y);", Space); 14268 14269 FormatStyle SomeSpace = getLLVMStyle(); 14270 SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses; 14271 14272 verifyFormat("[]() -> float {}", SomeSpace); 14273 verifyFormat("[] (auto foo) {}", SomeSpace); 14274 verifyFormat("[foo]() -> int {}", SomeSpace); 14275 verifyFormat("int f();", SomeSpace); 14276 verifyFormat("void f (int a, T b) {\n" 14277 " while (true)\n" 14278 " continue;\n" 14279 "}", 14280 SomeSpace); 14281 verifyFormat("if (true)\n" 14282 " f();\n" 14283 "else if (true)\n" 14284 " f();", 14285 SomeSpace); 14286 verifyFormat("do {\n" 14287 " do_something();\n" 14288 "} while (something());", 14289 SomeSpace); 14290 verifyFormat("switch (x) {\n" 14291 "default:\n" 14292 " break;\n" 14293 "}", 14294 SomeSpace); 14295 verifyFormat("A::A() : a (1) {}", SomeSpace); 14296 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace); 14297 verifyFormat("*(&a + 1);\n" 14298 "&((&a)[1]);\n" 14299 "a[(b + c) * d];\n" 14300 "(((a + 1) * 2) + 3) * 4;", 14301 SomeSpace); 14302 verifyFormat("#define A(x) x", SomeSpace); 14303 verifyFormat("#define A (x) x", SomeSpace); 14304 verifyFormat("#if defined(x)\n" 14305 "#endif", 14306 SomeSpace); 14307 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace); 14308 verifyFormat("size_t x = sizeof (x);", SomeSpace); 14309 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace); 14310 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace); 14311 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace); 14312 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace); 14313 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace); 14314 verifyFormat("alignas (128) char a[128];", SomeSpace); 14315 verifyFormat("size_t x = alignof (MyType);", SomeSpace); 14316 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", 14317 SomeSpace); 14318 verifyFormat("int f() throw (Deprecated);", SomeSpace); 14319 verifyFormat("typedef void (*cb) (int);", SomeSpace); 14320 verifyFormat("T A::operator()();", SomeSpace); 14321 // FIXME these tests regressed behaviour. 14322 // verifyFormat("X A::operator++ (T);", SomeSpace); 14323 verifyFormat("int x = int (y);", SomeSpace); 14324 verifyFormat("auto lambda = []() { return 0; };", SomeSpace); 14325 14326 FormatStyle SpaceControlStatements = getLLVMStyle(); 14327 SpaceControlStatements.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14328 SpaceControlStatements.SpaceBeforeParensOptions.AfterControlStatements = true; 14329 14330 verifyFormat("while (true)\n" 14331 " continue;", 14332 SpaceControlStatements); 14333 verifyFormat("if (true)\n" 14334 " f();\n" 14335 "else if (true)\n" 14336 " f();", 14337 SpaceControlStatements); 14338 verifyFormat("for (;;) {\n" 14339 " do_something();\n" 14340 "}", 14341 SpaceControlStatements); 14342 verifyFormat("do {\n" 14343 " do_something();\n" 14344 "} while (something());", 14345 SpaceControlStatements); 14346 verifyFormat("switch (x) {\n" 14347 "default:\n" 14348 " break;\n" 14349 "}", 14350 SpaceControlStatements); 14351 14352 FormatStyle SpaceFuncDecl = getLLVMStyle(); 14353 SpaceFuncDecl.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14354 SpaceFuncDecl.SpaceBeforeParensOptions.AfterFunctionDeclarationName = true; 14355 14356 verifyFormat("int f ();", SpaceFuncDecl); 14357 verifyFormat("void f(int a, T b) {}", SpaceFuncDecl); 14358 verifyFormat("A::A() : a(1) {}", SpaceFuncDecl); 14359 verifyFormat("void f () __attribute__((asdf));", SpaceFuncDecl); 14360 verifyFormat("#define A(x) x", SpaceFuncDecl); 14361 verifyFormat("#define A (x) x", SpaceFuncDecl); 14362 verifyFormat("#if defined(x)\n" 14363 "#endif", 14364 SpaceFuncDecl); 14365 verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDecl); 14366 verifyFormat("size_t x = sizeof(x);", SpaceFuncDecl); 14367 verifyFormat("auto f (int x) -> decltype(x);", SpaceFuncDecl); 14368 verifyFormat("auto f (int x) -> typeof(x);", SpaceFuncDecl); 14369 verifyFormat("auto f (int x) -> _Atomic(x);", SpaceFuncDecl); 14370 verifyFormat("auto f (int x) -> __underlying_type(x);", SpaceFuncDecl); 14371 verifyFormat("int f (T x) noexcept(x.create());", SpaceFuncDecl); 14372 verifyFormat("alignas(128) char a[128];", SpaceFuncDecl); 14373 verifyFormat("size_t x = alignof(MyType);", SpaceFuncDecl); 14374 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", 14375 SpaceFuncDecl); 14376 verifyFormat("int f () throw(Deprecated);", SpaceFuncDecl); 14377 verifyFormat("typedef void (*cb)(int);", SpaceFuncDecl); 14378 // FIXME these tests regressed behaviour. 14379 // verifyFormat("T A::operator() ();", SpaceFuncDecl); 14380 // verifyFormat("X A::operator++ (T);", SpaceFuncDecl); 14381 verifyFormat("T A::operator()() {}", SpaceFuncDecl); 14382 verifyFormat("auto lambda = []() { return 0; };", SpaceFuncDecl); 14383 verifyFormat("int x = int(y);", SpaceFuncDecl); 14384 verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}", 14385 SpaceFuncDecl); 14386 14387 FormatStyle SpaceFuncDef = getLLVMStyle(); 14388 SpaceFuncDef.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14389 SpaceFuncDef.SpaceBeforeParensOptions.AfterFunctionDefinitionName = true; 14390 14391 verifyFormat("int f();", SpaceFuncDef); 14392 verifyFormat("void f (int a, T b) {}", SpaceFuncDef); 14393 verifyFormat("A::A() : a(1) {}", SpaceFuncDef); 14394 verifyFormat("void f() __attribute__((asdf));", SpaceFuncDef); 14395 verifyFormat("#define A(x) x", SpaceFuncDef); 14396 verifyFormat("#define A (x) x", SpaceFuncDef); 14397 verifyFormat("#if defined(x)\n" 14398 "#endif", 14399 SpaceFuncDef); 14400 verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDef); 14401 verifyFormat("size_t x = sizeof(x);", SpaceFuncDef); 14402 verifyFormat("auto f(int x) -> decltype(x);", SpaceFuncDef); 14403 verifyFormat("auto f(int x) -> typeof(x);", SpaceFuncDef); 14404 verifyFormat("auto f(int x) -> _Atomic(x);", SpaceFuncDef); 14405 verifyFormat("auto f(int x) -> __underlying_type(x);", SpaceFuncDef); 14406 verifyFormat("int f(T x) noexcept(x.create());", SpaceFuncDef); 14407 verifyFormat("alignas(128) char a[128];", SpaceFuncDef); 14408 verifyFormat("size_t x = alignof(MyType);", SpaceFuncDef); 14409 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", 14410 SpaceFuncDef); 14411 verifyFormat("int f() throw(Deprecated);", SpaceFuncDef); 14412 verifyFormat("typedef void (*cb)(int);", SpaceFuncDef); 14413 verifyFormat("T A::operator()();", SpaceFuncDef); 14414 verifyFormat("X A::operator++(T);", SpaceFuncDef); 14415 // verifyFormat("T A::operator() () {}", SpaceFuncDef); 14416 verifyFormat("auto lambda = [] () { return 0; };", SpaceFuncDef); 14417 verifyFormat("int x = int(y);", SpaceFuncDef); 14418 verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}", 14419 SpaceFuncDef); 14420 14421 FormatStyle SpaceIfMacros = getLLVMStyle(); 14422 SpaceIfMacros.IfMacros.clear(); 14423 SpaceIfMacros.IfMacros.push_back("MYIF"); 14424 SpaceIfMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14425 SpaceIfMacros.SpaceBeforeParensOptions.AfterIfMacros = true; 14426 verifyFormat("MYIF (a)\n return;", SpaceIfMacros); 14427 verifyFormat("MYIF (a)\n return;\nelse MYIF (b)\n return;", SpaceIfMacros); 14428 verifyFormat("MYIF (a)\n return;\nelse\n return;", SpaceIfMacros); 14429 14430 FormatStyle SpaceForeachMacros = getLLVMStyle(); 14431 SpaceForeachMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14432 SpaceForeachMacros.SpaceBeforeParensOptions.AfterForeachMacros = true; 14433 verifyFormat("foreach (Item *item, itemlist) {}", SpaceForeachMacros); 14434 verifyFormat("Q_FOREACH (Item *item, itemlist) {}", SpaceForeachMacros); 14435 verifyFormat("BOOST_FOREACH (Item *item, itemlist) {}", SpaceForeachMacros); 14436 verifyFormat("UNKNOWN_FOREACH(Item *item, itemlist) {}", SpaceForeachMacros); 14437 14438 FormatStyle SomeSpace2 = getLLVMStyle(); 14439 SomeSpace2.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14440 SomeSpace2.SpaceBeforeParensOptions.BeforeNonEmptyParentheses = true; 14441 verifyFormat("[]() -> float {}", SomeSpace2); 14442 verifyFormat("[] (auto foo) {}", SomeSpace2); 14443 verifyFormat("[foo]() -> int {}", SomeSpace2); 14444 verifyFormat("int f();", SomeSpace2); 14445 verifyFormat("void f (int a, T b) {\n" 14446 " while (true)\n" 14447 " continue;\n" 14448 "}", 14449 SomeSpace2); 14450 verifyFormat("if (true)\n" 14451 " f();\n" 14452 "else if (true)\n" 14453 " f();", 14454 SomeSpace2); 14455 verifyFormat("do {\n" 14456 " do_something();\n" 14457 "} while (something());", 14458 SomeSpace2); 14459 verifyFormat("switch (x) {\n" 14460 "default:\n" 14461 " break;\n" 14462 "}", 14463 SomeSpace2); 14464 verifyFormat("A::A() : a (1) {}", SomeSpace2); 14465 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace2); 14466 verifyFormat("*(&a + 1);\n" 14467 "&((&a)[1]);\n" 14468 "a[(b + c) * d];\n" 14469 "(((a + 1) * 2) + 3) * 4;", 14470 SomeSpace2); 14471 verifyFormat("#define A(x) x", SomeSpace2); 14472 verifyFormat("#define A (x) x", SomeSpace2); 14473 verifyFormat("#if defined(x)\n" 14474 "#endif", 14475 SomeSpace2); 14476 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace2); 14477 verifyFormat("size_t x = sizeof (x);", SomeSpace2); 14478 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace2); 14479 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace2); 14480 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace2); 14481 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace2); 14482 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace2); 14483 verifyFormat("alignas (128) char a[128];", SomeSpace2); 14484 verifyFormat("size_t x = alignof (MyType);", SomeSpace2); 14485 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", 14486 SomeSpace2); 14487 verifyFormat("int f() throw (Deprecated);", SomeSpace2); 14488 verifyFormat("typedef void (*cb) (int);", SomeSpace2); 14489 verifyFormat("T A::operator()();", SomeSpace2); 14490 // verifyFormat("X A::operator++ (T);", SomeSpace2); 14491 verifyFormat("int x = int (y);", SomeSpace2); 14492 verifyFormat("auto lambda = []() { return 0; };", SomeSpace2); 14493 } 14494 14495 TEST_F(FormatTest, SpaceAfterLogicalNot) { 14496 FormatStyle Spaces = getLLVMStyle(); 14497 Spaces.SpaceAfterLogicalNot = true; 14498 14499 verifyFormat("bool x = ! y", Spaces); 14500 verifyFormat("if (! isFailure())", Spaces); 14501 verifyFormat("if (! (a && b))", Spaces); 14502 verifyFormat("\"Error!\"", Spaces); 14503 verifyFormat("! ! x", Spaces); 14504 } 14505 14506 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 14507 FormatStyle Spaces = getLLVMStyle(); 14508 14509 Spaces.SpacesInParentheses = true; 14510 verifyFormat("do_something( ::globalVar );", Spaces); 14511 verifyFormat("call( x, y, z );", Spaces); 14512 verifyFormat("call();", Spaces); 14513 verifyFormat("std::function<void( int, int )> callback;", Spaces); 14514 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 14515 Spaces); 14516 verifyFormat("while ( (bool)1 )\n" 14517 " continue;", 14518 Spaces); 14519 verifyFormat("for ( ;; )\n" 14520 " continue;", 14521 Spaces); 14522 verifyFormat("if ( true )\n" 14523 " f();\n" 14524 "else if ( true )\n" 14525 " f();", 14526 Spaces); 14527 verifyFormat("do {\n" 14528 " do_something( (int)i );\n" 14529 "} while ( something() );", 14530 Spaces); 14531 verifyFormat("switch ( x ) {\n" 14532 "default:\n" 14533 " break;\n" 14534 "}", 14535 Spaces); 14536 14537 Spaces.SpacesInParentheses = false; 14538 Spaces.SpacesInCStyleCastParentheses = true; 14539 verifyFormat("Type *A = ( Type * )P;", Spaces); 14540 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 14541 verifyFormat("x = ( int32 )y;", Spaces); 14542 verifyFormat("int a = ( int )(2.0f);", Spaces); 14543 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 14544 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 14545 verifyFormat("#define x (( int )-1)", Spaces); 14546 14547 // Run the first set of tests again with: 14548 Spaces.SpacesInParentheses = false; 14549 Spaces.SpaceInEmptyParentheses = true; 14550 Spaces.SpacesInCStyleCastParentheses = true; 14551 verifyFormat("call(x, y, z);", Spaces); 14552 verifyFormat("call( );", Spaces); 14553 verifyFormat("std::function<void(int, int)> callback;", Spaces); 14554 verifyFormat("while (( bool )1)\n" 14555 " continue;", 14556 Spaces); 14557 verifyFormat("for (;;)\n" 14558 " continue;", 14559 Spaces); 14560 verifyFormat("if (true)\n" 14561 " f( );\n" 14562 "else if (true)\n" 14563 " f( );", 14564 Spaces); 14565 verifyFormat("do {\n" 14566 " do_something(( int )i);\n" 14567 "} while (something( ));", 14568 Spaces); 14569 verifyFormat("switch (x) {\n" 14570 "default:\n" 14571 " break;\n" 14572 "}", 14573 Spaces); 14574 14575 // Run the first set of tests again with: 14576 Spaces.SpaceAfterCStyleCast = true; 14577 verifyFormat("call(x, y, z);", Spaces); 14578 verifyFormat("call( );", Spaces); 14579 verifyFormat("std::function<void(int, int)> callback;", Spaces); 14580 verifyFormat("while (( bool ) 1)\n" 14581 " continue;", 14582 Spaces); 14583 verifyFormat("for (;;)\n" 14584 " continue;", 14585 Spaces); 14586 verifyFormat("if (true)\n" 14587 " f( );\n" 14588 "else if (true)\n" 14589 " f( );", 14590 Spaces); 14591 verifyFormat("do {\n" 14592 " do_something(( int ) i);\n" 14593 "} while (something( ));", 14594 Spaces); 14595 verifyFormat("switch (x) {\n" 14596 "default:\n" 14597 " break;\n" 14598 "}", 14599 Spaces); 14600 14601 // Run subset of tests again with: 14602 Spaces.SpacesInCStyleCastParentheses = false; 14603 Spaces.SpaceAfterCStyleCast = true; 14604 verifyFormat("while ((bool) 1)\n" 14605 " continue;", 14606 Spaces); 14607 verifyFormat("do {\n" 14608 " do_something((int) i);\n" 14609 "} while (something( ));", 14610 Spaces); 14611 14612 verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces); 14613 verifyFormat("size_t idx = (size_t) a;", Spaces); 14614 verifyFormat("size_t idx = (size_t) (a - 1);", Spaces); 14615 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); 14616 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); 14617 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); 14618 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); 14619 Spaces.ColumnLimit = 80; 14620 Spaces.IndentWidth = 4; 14621 Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 14622 verifyFormat("void foo( ) {\n" 14623 " size_t foo = (*(function))(\n" 14624 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " 14625 "BarrrrrrrrrrrrLong,\n" 14626 " FoooooooooLooooong);\n" 14627 "}", 14628 Spaces); 14629 Spaces.SpaceAfterCStyleCast = false; 14630 verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces); 14631 verifyFormat("size_t idx = (size_t)a;", Spaces); 14632 verifyFormat("size_t idx = (size_t)(a - 1);", Spaces); 14633 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); 14634 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); 14635 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); 14636 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); 14637 14638 verifyFormat("void foo( ) {\n" 14639 " size_t foo = (*(function))(\n" 14640 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " 14641 "BarrrrrrrrrrrrLong,\n" 14642 " FoooooooooLooooong);\n" 14643 "}", 14644 Spaces); 14645 } 14646 14647 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 14648 verifyFormat("int a[5];"); 14649 verifyFormat("a[3] += 42;"); 14650 14651 FormatStyle Spaces = getLLVMStyle(); 14652 Spaces.SpacesInSquareBrackets = true; 14653 // Not lambdas. 14654 verifyFormat("int a[ 5 ];", Spaces); 14655 verifyFormat("a[ 3 ] += 42;", Spaces); 14656 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 14657 verifyFormat("double &operator[](int i) { return 0; }\n" 14658 "int i;", 14659 Spaces); 14660 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 14661 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 14662 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 14663 // Lambdas. 14664 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 14665 verifyFormat("return [ i, args... ] {};", Spaces); 14666 verifyFormat("int foo = [ &bar ]() {};", Spaces); 14667 verifyFormat("int foo = [ = ]() {};", Spaces); 14668 verifyFormat("int foo = [ & ]() {};", Spaces); 14669 verifyFormat("int foo = [ =, &bar ]() {};", Spaces); 14670 verifyFormat("int foo = [ &bar, = ]() {};", Spaces); 14671 } 14672 14673 TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) { 14674 FormatStyle NoSpaceStyle = getLLVMStyle(); 14675 verifyFormat("int a[5];", NoSpaceStyle); 14676 verifyFormat("a[3] += 42;", NoSpaceStyle); 14677 14678 verifyFormat("int a[1];", NoSpaceStyle); 14679 verifyFormat("int 1 [a];", NoSpaceStyle); 14680 verifyFormat("int a[1][2];", NoSpaceStyle); 14681 verifyFormat("a[7] = 5;", NoSpaceStyle); 14682 verifyFormat("int a = (f())[23];", NoSpaceStyle); 14683 verifyFormat("f([] {})", NoSpaceStyle); 14684 14685 FormatStyle Space = getLLVMStyle(); 14686 Space.SpaceBeforeSquareBrackets = true; 14687 verifyFormat("int c = []() -> int { return 2; }();\n", Space); 14688 verifyFormat("return [i, args...] {};", Space); 14689 14690 verifyFormat("int a [5];", Space); 14691 verifyFormat("a [3] += 42;", Space); 14692 verifyFormat("constexpr char hello []{\"hello\"};", Space); 14693 verifyFormat("double &operator[](int i) { return 0; }\n" 14694 "int i;", 14695 Space); 14696 verifyFormat("std::unique_ptr<int []> foo() {}", Space); 14697 verifyFormat("int i = a [a][a]->f();", Space); 14698 verifyFormat("int i = (*b) [a]->f();", Space); 14699 14700 verifyFormat("int a [1];", Space); 14701 verifyFormat("int 1 [a];", Space); 14702 verifyFormat("int a [1][2];", Space); 14703 verifyFormat("a [7] = 5;", Space); 14704 verifyFormat("int a = (f()) [23];", Space); 14705 verifyFormat("f([] {})", Space); 14706 } 14707 14708 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 14709 verifyFormat("int a = 5;"); 14710 verifyFormat("a += 42;"); 14711 verifyFormat("a or_eq 8;"); 14712 14713 FormatStyle Spaces = getLLVMStyle(); 14714 Spaces.SpaceBeforeAssignmentOperators = false; 14715 verifyFormat("int a= 5;", Spaces); 14716 verifyFormat("a+= 42;", Spaces); 14717 verifyFormat("a or_eq 8;", Spaces); 14718 } 14719 14720 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) { 14721 verifyFormat("class Foo : public Bar {};"); 14722 verifyFormat("Foo::Foo() : foo(1) {}"); 14723 verifyFormat("for (auto a : b) {\n}"); 14724 verifyFormat("int x = a ? b : c;"); 14725 verifyFormat("{\n" 14726 "label0:\n" 14727 " int x = 0;\n" 14728 "}"); 14729 verifyFormat("switch (x) {\n" 14730 "case 1:\n" 14731 "default:\n" 14732 "}"); 14733 verifyFormat("switch (allBraces) {\n" 14734 "case 1: {\n" 14735 " break;\n" 14736 "}\n" 14737 "case 2: {\n" 14738 " [[fallthrough]];\n" 14739 "}\n" 14740 "default: {\n" 14741 " break;\n" 14742 "}\n" 14743 "}"); 14744 14745 FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30); 14746 CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false; 14747 verifyFormat("class Foo : public Bar {};", CtorInitializerStyle); 14748 verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle); 14749 verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle); 14750 verifyFormat("int x = a ? b : c;", CtorInitializerStyle); 14751 verifyFormat("{\n" 14752 "label1:\n" 14753 " int x = 0;\n" 14754 "}", 14755 CtorInitializerStyle); 14756 verifyFormat("switch (x) {\n" 14757 "case 1:\n" 14758 "default:\n" 14759 "}", 14760 CtorInitializerStyle); 14761 verifyFormat("switch (allBraces) {\n" 14762 "case 1: {\n" 14763 " break;\n" 14764 "}\n" 14765 "case 2: {\n" 14766 " [[fallthrough]];\n" 14767 "}\n" 14768 "default: {\n" 14769 " break;\n" 14770 "}\n" 14771 "}", 14772 CtorInitializerStyle); 14773 CtorInitializerStyle.BreakConstructorInitializers = 14774 FormatStyle::BCIS_AfterColon; 14775 verifyFormat("Fooooooooooo::Fooooooooooo():\n" 14776 " aaaaaaaaaaaaaaaa(1),\n" 14777 " bbbbbbbbbbbbbbbb(2) {}", 14778 CtorInitializerStyle); 14779 CtorInitializerStyle.BreakConstructorInitializers = 14780 FormatStyle::BCIS_BeforeComma; 14781 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 14782 " : aaaaaaaaaaaaaaaa(1)\n" 14783 " , bbbbbbbbbbbbbbbb(2) {}", 14784 CtorInitializerStyle); 14785 CtorInitializerStyle.BreakConstructorInitializers = 14786 FormatStyle::BCIS_BeforeColon; 14787 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 14788 " : aaaaaaaaaaaaaaaa(1),\n" 14789 " bbbbbbbbbbbbbbbb(2) {}", 14790 CtorInitializerStyle); 14791 CtorInitializerStyle.ConstructorInitializerIndentWidth = 0; 14792 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 14793 ": aaaaaaaaaaaaaaaa(1),\n" 14794 " bbbbbbbbbbbbbbbb(2) {}", 14795 CtorInitializerStyle); 14796 14797 FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30); 14798 InheritanceStyle.SpaceBeforeInheritanceColon = false; 14799 verifyFormat("class Foo: public Bar {};", InheritanceStyle); 14800 verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle); 14801 verifyFormat("for (auto a : b) {\n}", InheritanceStyle); 14802 verifyFormat("int x = a ? b : c;", InheritanceStyle); 14803 verifyFormat("{\n" 14804 "label2:\n" 14805 " int x = 0;\n" 14806 "}", 14807 InheritanceStyle); 14808 verifyFormat("switch (x) {\n" 14809 "case 1:\n" 14810 "default:\n" 14811 "}", 14812 InheritanceStyle); 14813 verifyFormat("switch (allBraces) {\n" 14814 "case 1: {\n" 14815 " break;\n" 14816 "}\n" 14817 "case 2: {\n" 14818 " [[fallthrough]];\n" 14819 "}\n" 14820 "default: {\n" 14821 " break;\n" 14822 "}\n" 14823 "}", 14824 InheritanceStyle); 14825 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma; 14826 verifyFormat("class Foooooooooooooooooooooo\n" 14827 " : public aaaaaaaaaaaaaaaaaa,\n" 14828 " public bbbbbbbbbbbbbbbbbb {\n" 14829 "}", 14830 InheritanceStyle); 14831 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon; 14832 verifyFormat("class Foooooooooooooooooooooo:\n" 14833 " public aaaaaaaaaaaaaaaaaa,\n" 14834 " public bbbbbbbbbbbbbbbbbb {\n" 14835 "}", 14836 InheritanceStyle); 14837 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 14838 verifyFormat("class Foooooooooooooooooooooo\n" 14839 " : public aaaaaaaaaaaaaaaaaa\n" 14840 " , public bbbbbbbbbbbbbbbbbb {\n" 14841 "}", 14842 InheritanceStyle); 14843 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 14844 verifyFormat("class Foooooooooooooooooooooo\n" 14845 " : public aaaaaaaaaaaaaaaaaa,\n" 14846 " public bbbbbbbbbbbbbbbbbb {\n" 14847 "}", 14848 InheritanceStyle); 14849 InheritanceStyle.ConstructorInitializerIndentWidth = 0; 14850 verifyFormat("class Foooooooooooooooooooooo\n" 14851 ": public aaaaaaaaaaaaaaaaaa,\n" 14852 " public bbbbbbbbbbbbbbbbbb {}", 14853 InheritanceStyle); 14854 14855 FormatStyle ForLoopStyle = getLLVMStyle(); 14856 ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false; 14857 verifyFormat("class Foo : public Bar {};", ForLoopStyle); 14858 verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle); 14859 verifyFormat("for (auto a: b) {\n}", ForLoopStyle); 14860 verifyFormat("int x = a ? b : c;", ForLoopStyle); 14861 verifyFormat("{\n" 14862 "label2:\n" 14863 " int x = 0;\n" 14864 "}", 14865 ForLoopStyle); 14866 verifyFormat("switch (x) {\n" 14867 "case 1:\n" 14868 "default:\n" 14869 "}", 14870 ForLoopStyle); 14871 verifyFormat("switch (allBraces) {\n" 14872 "case 1: {\n" 14873 " break;\n" 14874 "}\n" 14875 "case 2: {\n" 14876 " [[fallthrough]];\n" 14877 "}\n" 14878 "default: {\n" 14879 " break;\n" 14880 "}\n" 14881 "}", 14882 ForLoopStyle); 14883 14884 FormatStyle CaseStyle = getLLVMStyle(); 14885 CaseStyle.SpaceBeforeCaseColon = true; 14886 verifyFormat("class Foo : public Bar {};", CaseStyle); 14887 verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle); 14888 verifyFormat("for (auto a : b) {\n}", CaseStyle); 14889 verifyFormat("int x = a ? b : c;", CaseStyle); 14890 verifyFormat("switch (x) {\n" 14891 "case 1 :\n" 14892 "default :\n" 14893 "}", 14894 CaseStyle); 14895 verifyFormat("switch (allBraces) {\n" 14896 "case 1 : {\n" 14897 " break;\n" 14898 "}\n" 14899 "case 2 : {\n" 14900 " [[fallthrough]];\n" 14901 "}\n" 14902 "default : {\n" 14903 " break;\n" 14904 "}\n" 14905 "}", 14906 CaseStyle); 14907 14908 FormatStyle NoSpaceStyle = getLLVMStyle(); 14909 EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false); 14910 NoSpaceStyle.SpaceBeforeCtorInitializerColon = false; 14911 NoSpaceStyle.SpaceBeforeInheritanceColon = false; 14912 NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 14913 verifyFormat("class Foo: public Bar {};", NoSpaceStyle); 14914 verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle); 14915 verifyFormat("for (auto a: b) {\n}", NoSpaceStyle); 14916 verifyFormat("int x = a ? b : c;", NoSpaceStyle); 14917 verifyFormat("{\n" 14918 "label3:\n" 14919 " int x = 0;\n" 14920 "}", 14921 NoSpaceStyle); 14922 verifyFormat("switch (x) {\n" 14923 "case 1:\n" 14924 "default:\n" 14925 "}", 14926 NoSpaceStyle); 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 NoSpaceStyle); 14939 14940 FormatStyle InvertedSpaceStyle = getLLVMStyle(); 14941 InvertedSpaceStyle.SpaceBeforeCaseColon = true; 14942 InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false; 14943 InvertedSpaceStyle.SpaceBeforeInheritanceColon = false; 14944 InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 14945 verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle); 14946 verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle); 14947 verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle); 14948 verifyFormat("int x = a ? b : c;", InvertedSpaceStyle); 14949 verifyFormat("{\n" 14950 "label3:\n" 14951 " int x = 0;\n" 14952 "}", 14953 InvertedSpaceStyle); 14954 verifyFormat("switch (x) {\n" 14955 "case 1 :\n" 14956 "case 2 : {\n" 14957 " break;\n" 14958 "}\n" 14959 "default :\n" 14960 " break;\n" 14961 "}", 14962 InvertedSpaceStyle); 14963 verifyFormat("switch (allBraces) {\n" 14964 "case 1 : {\n" 14965 " break;\n" 14966 "}\n" 14967 "case 2 : {\n" 14968 " [[fallthrough]];\n" 14969 "}\n" 14970 "default : {\n" 14971 " break;\n" 14972 "}\n" 14973 "}", 14974 InvertedSpaceStyle); 14975 } 14976 14977 TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) { 14978 FormatStyle Style = getLLVMStyle(); 14979 14980 Style.PointerAlignment = FormatStyle::PAS_Left; 14981 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; 14982 verifyFormat("void* const* x = NULL;", Style); 14983 14984 #define verifyQualifierSpaces(Code, Pointers, Qualifiers) \ 14985 do { \ 14986 Style.PointerAlignment = FormatStyle::Pointers; \ 14987 Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers; \ 14988 verifyFormat(Code, Style); \ 14989 } while (false) 14990 14991 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default); 14992 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default); 14993 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default); 14994 14995 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before); 14996 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before); 14997 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before); 14998 14999 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After); 15000 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After); 15001 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After); 15002 15003 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both); 15004 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both); 15005 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both); 15006 15007 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default); 15008 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, 15009 SAPQ_Default); 15010 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 15011 SAPQ_Default); 15012 15013 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before); 15014 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, 15015 SAPQ_Before); 15016 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 15017 SAPQ_Before); 15018 15019 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After); 15020 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After); 15021 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 15022 SAPQ_After); 15023 15024 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both); 15025 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both); 15026 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both); 15027 15028 #undef verifyQualifierSpaces 15029 15030 FormatStyle Spaces = getLLVMStyle(); 15031 Spaces.AttributeMacros.push_back("qualified"); 15032 Spaces.PointerAlignment = FormatStyle::PAS_Right; 15033 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; 15034 verifyFormat("SomeType *volatile *a = NULL;", Spaces); 15035 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces); 15036 verifyFormat("std::vector<SomeType *const *> x;", Spaces); 15037 verifyFormat("std::vector<SomeType *qualified *> x;", Spaces); 15038 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 15039 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before; 15040 verifyFormat("SomeType * volatile *a = NULL;", Spaces); 15041 verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces); 15042 verifyFormat("std::vector<SomeType * const *> x;", Spaces); 15043 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces); 15044 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 15045 15046 // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left. 15047 Spaces.PointerAlignment = FormatStyle::PAS_Left; 15048 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before; 15049 verifyFormat("SomeType* volatile* a = NULL;", Spaces); 15050 verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces); 15051 verifyFormat("std::vector<SomeType* const*> x;", Spaces); 15052 verifyFormat("std::vector<SomeType* qualified*> x;", Spaces); 15053 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 15054 // However, setting it to SAPQ_After should add spaces after __attribute, etc. 15055 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After; 15056 verifyFormat("SomeType* volatile * a = NULL;", Spaces); 15057 verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces); 15058 verifyFormat("std::vector<SomeType* const *> x;", Spaces); 15059 verifyFormat("std::vector<SomeType* qualified *> x;", Spaces); 15060 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 15061 15062 // PAS_Middle should not have any noticeable changes even for SAPQ_Both 15063 Spaces.PointerAlignment = FormatStyle::PAS_Middle; 15064 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After; 15065 verifyFormat("SomeType * volatile * a = NULL;", Spaces); 15066 verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces); 15067 verifyFormat("std::vector<SomeType * const *> x;", Spaces); 15068 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces); 15069 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 15070 } 15071 15072 TEST_F(FormatTest, AlignConsecutiveMacros) { 15073 FormatStyle Style = getLLVMStyle(); 15074 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 15075 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 15076 Style.AlignConsecutiveMacros = FormatStyle::ACS_None; 15077 15078 verifyFormat("#define a 3\n" 15079 "#define bbbb 4\n" 15080 "#define ccc (5)", 15081 Style); 15082 15083 verifyFormat("#define f(x) (x * x)\n" 15084 "#define fff(x, y, z) (x * y + z)\n" 15085 "#define ffff(x, y) (x - y)", 15086 Style); 15087 15088 verifyFormat("#define foo(x, y) (x + y)\n" 15089 "#define bar (5, 6)(2 + 2)", 15090 Style); 15091 15092 verifyFormat("#define a 3\n" 15093 "#define bbbb 4\n" 15094 "#define ccc (5)\n" 15095 "#define f(x) (x * x)\n" 15096 "#define fff(x, y, z) (x * y + z)\n" 15097 "#define ffff(x, y) (x - y)", 15098 Style); 15099 15100 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15101 verifyFormat("#define a 3\n" 15102 "#define bbbb 4\n" 15103 "#define ccc (5)", 15104 Style); 15105 15106 verifyFormat("#define f(x) (x * x)\n" 15107 "#define fff(x, y, z) (x * y + z)\n" 15108 "#define ffff(x, y) (x - y)", 15109 Style); 15110 15111 verifyFormat("#define foo(x, y) (x + y)\n" 15112 "#define bar (5, 6)(2 + 2)", 15113 Style); 15114 15115 verifyFormat("#define a 3\n" 15116 "#define bbbb 4\n" 15117 "#define ccc (5)\n" 15118 "#define f(x) (x * x)\n" 15119 "#define fff(x, y, z) (x * y + z)\n" 15120 "#define ffff(x, y) (x - y)", 15121 Style); 15122 15123 verifyFormat("#define a 5\n" 15124 "#define foo(x, y) (x + y)\n" 15125 "#define CCC (6)\n" 15126 "auto lambda = []() {\n" 15127 " auto ii = 0;\n" 15128 " float j = 0;\n" 15129 " return 0;\n" 15130 "};\n" 15131 "int i = 0;\n" 15132 "float i2 = 0;\n" 15133 "auto v = type{\n" 15134 " i = 1, //\n" 15135 " (i = 2), //\n" 15136 " i = 3 //\n" 15137 "};", 15138 Style); 15139 15140 Style.AlignConsecutiveMacros = FormatStyle::ACS_None; 15141 Style.ColumnLimit = 20; 15142 15143 verifyFormat("#define a \\\n" 15144 " \"aabbbbbbbbbbbb\"\n" 15145 "#define D \\\n" 15146 " \"aabbbbbbbbbbbb\" \\\n" 15147 " \"ccddeeeeeeeee\"\n" 15148 "#define B \\\n" 15149 " \"QQQQQQQQQQQQQ\" \\\n" 15150 " \"FFFFFFFFFFFFF\" \\\n" 15151 " \"LLLLLLLL\"\n", 15152 Style); 15153 15154 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15155 verifyFormat("#define a \\\n" 15156 " \"aabbbbbbbbbbbb\"\n" 15157 "#define D \\\n" 15158 " \"aabbbbbbbbbbbb\" \\\n" 15159 " \"ccddeeeeeeeee\"\n" 15160 "#define B \\\n" 15161 " \"QQQQQQQQQQQQQ\" \\\n" 15162 " \"FFFFFFFFFFFFF\" \\\n" 15163 " \"LLLLLLLL\"\n", 15164 Style); 15165 15166 // Test across comments 15167 Style.MaxEmptyLinesToKeep = 10; 15168 Style.ReflowComments = false; 15169 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossComments; 15170 EXPECT_EQ("#define a 3\n" 15171 "// line comment\n" 15172 "#define bbbb 4\n" 15173 "#define ccc (5)", 15174 format("#define a 3\n" 15175 "// line comment\n" 15176 "#define bbbb 4\n" 15177 "#define ccc (5)", 15178 Style)); 15179 15180 EXPECT_EQ("#define a 3\n" 15181 "/* block comment */\n" 15182 "#define bbbb 4\n" 15183 "#define ccc (5)", 15184 format("#define a 3\n" 15185 "/* block comment */\n" 15186 "#define bbbb 4\n" 15187 "#define ccc (5)", 15188 Style)); 15189 15190 EXPECT_EQ("#define a 3\n" 15191 "/* multi-line *\n" 15192 " * block comment */\n" 15193 "#define bbbb 4\n" 15194 "#define ccc (5)", 15195 format("#define a 3\n" 15196 "/* multi-line *\n" 15197 " * block comment */\n" 15198 "#define bbbb 4\n" 15199 "#define ccc (5)", 15200 Style)); 15201 15202 EXPECT_EQ("#define a 3\n" 15203 "// multi-line line comment\n" 15204 "//\n" 15205 "#define bbbb 4\n" 15206 "#define ccc (5)", 15207 format("#define a 3\n" 15208 "// multi-line line comment\n" 15209 "//\n" 15210 "#define bbbb 4\n" 15211 "#define ccc (5)", 15212 Style)); 15213 15214 EXPECT_EQ("#define a 3\n" 15215 "// empty lines still break.\n" 15216 "\n" 15217 "#define bbbb 4\n" 15218 "#define ccc (5)", 15219 format("#define a 3\n" 15220 "// empty lines still break.\n" 15221 "\n" 15222 "#define bbbb 4\n" 15223 "#define ccc (5)", 15224 Style)); 15225 15226 // Test across empty lines 15227 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLines; 15228 EXPECT_EQ("#define a 3\n" 15229 "\n" 15230 "#define bbbb 4\n" 15231 "#define ccc (5)", 15232 format("#define a 3\n" 15233 "\n" 15234 "#define bbbb 4\n" 15235 "#define ccc (5)", 15236 Style)); 15237 15238 EXPECT_EQ("#define a 3\n" 15239 "\n" 15240 "\n" 15241 "\n" 15242 "#define bbbb 4\n" 15243 "#define ccc (5)", 15244 format("#define a 3\n" 15245 "\n" 15246 "\n" 15247 "\n" 15248 "#define bbbb 4\n" 15249 "#define ccc (5)", 15250 Style)); 15251 15252 EXPECT_EQ("#define a 3\n" 15253 "// comments should break alignment\n" 15254 "//\n" 15255 "#define bbbb 4\n" 15256 "#define ccc (5)", 15257 format("#define a 3\n" 15258 "// comments should break alignment\n" 15259 "//\n" 15260 "#define bbbb 4\n" 15261 "#define ccc (5)", 15262 Style)); 15263 15264 // Test across empty lines and comments 15265 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLinesAndComments; 15266 verifyFormat("#define a 3\n" 15267 "\n" 15268 "// line comment\n" 15269 "#define bbbb 4\n" 15270 "#define ccc (5)", 15271 Style); 15272 15273 EXPECT_EQ("#define a 3\n" 15274 "\n" 15275 "\n" 15276 "/* multi-line *\n" 15277 " * block comment */\n" 15278 "\n" 15279 "\n" 15280 "#define bbbb 4\n" 15281 "#define ccc (5)", 15282 format("#define a 3\n" 15283 "\n" 15284 "\n" 15285 "/* multi-line *\n" 15286 " * block comment */\n" 15287 "\n" 15288 "\n" 15289 "#define bbbb 4\n" 15290 "#define ccc (5)", 15291 Style)); 15292 15293 EXPECT_EQ("#define a 3\n" 15294 "\n" 15295 "\n" 15296 "/* multi-line *\n" 15297 " * block comment */\n" 15298 "\n" 15299 "\n" 15300 "#define bbbb 4\n" 15301 "#define ccc (5)", 15302 format("#define a 3\n" 15303 "\n" 15304 "\n" 15305 "/* multi-line *\n" 15306 " * block comment */\n" 15307 "\n" 15308 "\n" 15309 "#define bbbb 4\n" 15310 "#define ccc (5)", 15311 Style)); 15312 } 15313 15314 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) { 15315 FormatStyle Alignment = getLLVMStyle(); 15316 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15317 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossEmptyLines; 15318 15319 Alignment.MaxEmptyLinesToKeep = 10; 15320 /* Test alignment across empty lines */ 15321 EXPECT_EQ("int a = 5;\n" 15322 "\n" 15323 "int oneTwoThree = 123;", 15324 format("int a = 5;\n" 15325 "\n" 15326 "int oneTwoThree= 123;", 15327 Alignment)); 15328 EXPECT_EQ("int a = 5;\n" 15329 "int one = 1;\n" 15330 "\n" 15331 "int oneTwoThree = 123;", 15332 format("int a = 5;\n" 15333 "int one = 1;\n" 15334 "\n" 15335 "int oneTwoThree = 123;", 15336 Alignment)); 15337 EXPECT_EQ("int a = 5;\n" 15338 "int one = 1;\n" 15339 "\n" 15340 "int oneTwoThree = 123;\n" 15341 "int oneTwo = 12;", 15342 format("int a = 5;\n" 15343 "int one = 1;\n" 15344 "\n" 15345 "int oneTwoThree = 123;\n" 15346 "int oneTwo = 12;", 15347 Alignment)); 15348 15349 /* Test across comments */ 15350 EXPECT_EQ("int a = 5;\n" 15351 "/* block comment */\n" 15352 "int oneTwoThree = 123;", 15353 format("int a = 5;\n" 15354 "/* block comment */\n" 15355 "int oneTwoThree=123;", 15356 Alignment)); 15357 15358 EXPECT_EQ("int a = 5;\n" 15359 "// line comment\n" 15360 "int oneTwoThree = 123;", 15361 format("int a = 5;\n" 15362 "// line comment\n" 15363 "int oneTwoThree=123;", 15364 Alignment)); 15365 15366 /* Test across comments and newlines */ 15367 EXPECT_EQ("int a = 5;\n" 15368 "\n" 15369 "/* block comment */\n" 15370 "int oneTwoThree = 123;", 15371 format("int a = 5;\n" 15372 "\n" 15373 "/* block comment */\n" 15374 "int oneTwoThree=123;", 15375 Alignment)); 15376 15377 EXPECT_EQ("int a = 5;\n" 15378 "\n" 15379 "// line comment\n" 15380 "int oneTwoThree = 123;", 15381 format("int a = 5;\n" 15382 "\n" 15383 "// line comment\n" 15384 "int oneTwoThree=123;", 15385 Alignment)); 15386 } 15387 15388 TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) { 15389 FormatStyle Alignment = getLLVMStyle(); 15390 Alignment.AlignConsecutiveDeclarations = 15391 FormatStyle::ACS_AcrossEmptyLinesAndComments; 15392 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 15393 15394 Alignment.MaxEmptyLinesToKeep = 10; 15395 /* Test alignment across empty lines */ 15396 EXPECT_EQ("int a = 5;\n" 15397 "\n" 15398 "float const oneTwoThree = 123;", 15399 format("int a = 5;\n" 15400 "\n" 15401 "float const oneTwoThree = 123;", 15402 Alignment)); 15403 EXPECT_EQ("int a = 5;\n" 15404 "float const one = 1;\n" 15405 "\n" 15406 "int oneTwoThree = 123;", 15407 format("int a = 5;\n" 15408 "float const one = 1;\n" 15409 "\n" 15410 "int oneTwoThree = 123;", 15411 Alignment)); 15412 15413 /* Test across comments */ 15414 EXPECT_EQ("float const a = 5;\n" 15415 "/* block comment */\n" 15416 "int oneTwoThree = 123;", 15417 format("float const a = 5;\n" 15418 "/* block comment */\n" 15419 "int oneTwoThree=123;", 15420 Alignment)); 15421 15422 EXPECT_EQ("float const a = 5;\n" 15423 "// line comment\n" 15424 "int oneTwoThree = 123;", 15425 format("float const a = 5;\n" 15426 "// line comment\n" 15427 "int oneTwoThree=123;", 15428 Alignment)); 15429 15430 /* Test across comments and newlines */ 15431 EXPECT_EQ("float const a = 5;\n" 15432 "\n" 15433 "/* block comment */\n" 15434 "int oneTwoThree = 123;", 15435 format("float const a = 5;\n" 15436 "\n" 15437 "/* block comment */\n" 15438 "int oneTwoThree=123;", 15439 Alignment)); 15440 15441 EXPECT_EQ("float const a = 5;\n" 15442 "\n" 15443 "// line comment\n" 15444 "int oneTwoThree = 123;", 15445 format("float const a = 5;\n" 15446 "\n" 15447 "// line comment\n" 15448 "int oneTwoThree=123;", 15449 Alignment)); 15450 } 15451 15452 TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) { 15453 FormatStyle Alignment = getLLVMStyle(); 15454 Alignment.AlignConsecutiveBitFields = 15455 FormatStyle::ACS_AcrossEmptyLinesAndComments; 15456 15457 Alignment.MaxEmptyLinesToKeep = 10; 15458 /* Test alignment across empty lines */ 15459 EXPECT_EQ("int a : 5;\n" 15460 "\n" 15461 "int longbitfield : 6;", 15462 format("int a : 5;\n" 15463 "\n" 15464 "int longbitfield : 6;", 15465 Alignment)); 15466 EXPECT_EQ("int a : 5;\n" 15467 "int one : 1;\n" 15468 "\n" 15469 "int longbitfield : 6;", 15470 format("int a : 5;\n" 15471 "int one : 1;\n" 15472 "\n" 15473 "int longbitfield : 6;", 15474 Alignment)); 15475 15476 /* Test across comments */ 15477 EXPECT_EQ("int a : 5;\n" 15478 "/* block comment */\n" 15479 "int longbitfield : 6;", 15480 format("int a : 5;\n" 15481 "/* block comment */\n" 15482 "int longbitfield : 6;", 15483 Alignment)); 15484 EXPECT_EQ("int a : 5;\n" 15485 "int one : 1;\n" 15486 "// line comment\n" 15487 "int longbitfield : 6;", 15488 format("int a : 5;\n" 15489 "int one : 1;\n" 15490 "// line comment\n" 15491 "int longbitfield : 6;", 15492 Alignment)); 15493 15494 /* Test across comments and newlines */ 15495 EXPECT_EQ("int a : 5;\n" 15496 "/* block comment */\n" 15497 "\n" 15498 "int longbitfield : 6;", 15499 format("int a : 5;\n" 15500 "/* block comment */\n" 15501 "\n" 15502 "int longbitfield : 6;", 15503 Alignment)); 15504 EXPECT_EQ("int a : 5;\n" 15505 "int one : 1;\n" 15506 "\n" 15507 "// line comment\n" 15508 "\n" 15509 "int longbitfield : 6;", 15510 format("int a : 5;\n" 15511 "int one : 1;\n" 15512 "\n" 15513 "// line comment \n" 15514 "\n" 15515 "int longbitfield : 6;", 15516 Alignment)); 15517 } 15518 15519 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) { 15520 FormatStyle Alignment = getLLVMStyle(); 15521 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15522 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossComments; 15523 15524 Alignment.MaxEmptyLinesToKeep = 10; 15525 /* Test alignment across empty lines */ 15526 EXPECT_EQ("int a = 5;\n" 15527 "\n" 15528 "int oneTwoThree = 123;", 15529 format("int a = 5;\n" 15530 "\n" 15531 "int oneTwoThree= 123;", 15532 Alignment)); 15533 EXPECT_EQ("int a = 5;\n" 15534 "int one = 1;\n" 15535 "\n" 15536 "int oneTwoThree = 123;", 15537 format("int a = 5;\n" 15538 "int one = 1;\n" 15539 "\n" 15540 "int oneTwoThree = 123;", 15541 Alignment)); 15542 15543 /* Test across comments */ 15544 EXPECT_EQ("int a = 5;\n" 15545 "/* block comment */\n" 15546 "int oneTwoThree = 123;", 15547 format("int a = 5;\n" 15548 "/* block comment */\n" 15549 "int oneTwoThree=123;", 15550 Alignment)); 15551 15552 EXPECT_EQ("int a = 5;\n" 15553 "// line comment\n" 15554 "int oneTwoThree = 123;", 15555 format("int a = 5;\n" 15556 "// line comment\n" 15557 "int oneTwoThree=123;", 15558 Alignment)); 15559 15560 EXPECT_EQ("int a = 5;\n" 15561 "/*\n" 15562 " * multi-line block comment\n" 15563 " */\n" 15564 "int oneTwoThree = 123;", 15565 format("int a = 5;\n" 15566 "/*\n" 15567 " * multi-line block comment\n" 15568 " */\n" 15569 "int oneTwoThree=123;", 15570 Alignment)); 15571 15572 EXPECT_EQ("int a = 5;\n" 15573 "//\n" 15574 "// multi-line line comment\n" 15575 "//\n" 15576 "int oneTwoThree = 123;", 15577 format("int a = 5;\n" 15578 "//\n" 15579 "// multi-line line comment\n" 15580 "//\n" 15581 "int oneTwoThree=123;", 15582 Alignment)); 15583 15584 /* Test across comments and newlines */ 15585 EXPECT_EQ("int a = 5;\n" 15586 "\n" 15587 "/* block comment */\n" 15588 "int oneTwoThree = 123;", 15589 format("int a = 5;\n" 15590 "\n" 15591 "/* block comment */\n" 15592 "int oneTwoThree=123;", 15593 Alignment)); 15594 15595 EXPECT_EQ("int a = 5;\n" 15596 "\n" 15597 "// line comment\n" 15598 "int oneTwoThree = 123;", 15599 format("int a = 5;\n" 15600 "\n" 15601 "// line comment\n" 15602 "int oneTwoThree=123;", 15603 Alignment)); 15604 } 15605 15606 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) { 15607 FormatStyle Alignment = getLLVMStyle(); 15608 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15609 Alignment.AlignConsecutiveAssignments = 15610 FormatStyle::ACS_AcrossEmptyLinesAndComments; 15611 verifyFormat("int a = 5;\n" 15612 "int oneTwoThree = 123;", 15613 Alignment); 15614 verifyFormat("int a = method();\n" 15615 "int oneTwoThree = 133;", 15616 Alignment); 15617 verifyFormat("a &= 5;\n" 15618 "bcd *= 5;\n" 15619 "ghtyf += 5;\n" 15620 "dvfvdb -= 5;\n" 15621 "a /= 5;\n" 15622 "vdsvsv %= 5;\n" 15623 "sfdbddfbdfbb ^= 5;\n" 15624 "dvsdsv |= 5;\n" 15625 "int dsvvdvsdvvv = 123;", 15626 Alignment); 15627 verifyFormat("int i = 1, j = 10;\n" 15628 "something = 2000;", 15629 Alignment); 15630 verifyFormat("something = 2000;\n" 15631 "int i = 1, j = 10;\n", 15632 Alignment); 15633 verifyFormat("something = 2000;\n" 15634 "another = 911;\n" 15635 "int i = 1, j = 10;\n" 15636 "oneMore = 1;\n" 15637 "i = 2;", 15638 Alignment); 15639 verifyFormat("int a = 5;\n" 15640 "int one = 1;\n" 15641 "method();\n" 15642 "int oneTwoThree = 123;\n" 15643 "int oneTwo = 12;", 15644 Alignment); 15645 verifyFormat("int oneTwoThree = 123;\n" 15646 "int oneTwo = 12;\n" 15647 "method();\n", 15648 Alignment); 15649 verifyFormat("int oneTwoThree = 123; // comment\n" 15650 "int oneTwo = 12; // comment", 15651 Alignment); 15652 15653 // Bug 25167 15654 /* Uncomment when fixed 15655 verifyFormat("#if A\n" 15656 "#else\n" 15657 "int aaaaaaaa = 12;\n" 15658 "#endif\n" 15659 "#if B\n" 15660 "#else\n" 15661 "int a = 12;\n" 15662 "#endif\n", 15663 Alignment); 15664 verifyFormat("enum foo {\n" 15665 "#if A\n" 15666 "#else\n" 15667 " aaaaaaaa = 12;\n" 15668 "#endif\n" 15669 "#if B\n" 15670 "#else\n" 15671 " a = 12;\n" 15672 "#endif\n" 15673 "};\n", 15674 Alignment); 15675 */ 15676 15677 Alignment.MaxEmptyLinesToKeep = 10; 15678 /* Test alignment across empty lines */ 15679 EXPECT_EQ("int a = 5;\n" 15680 "\n" 15681 "int oneTwoThree = 123;", 15682 format("int a = 5;\n" 15683 "\n" 15684 "int oneTwoThree= 123;", 15685 Alignment)); 15686 EXPECT_EQ("int a = 5;\n" 15687 "int one = 1;\n" 15688 "\n" 15689 "int oneTwoThree = 123;", 15690 format("int a = 5;\n" 15691 "int one = 1;\n" 15692 "\n" 15693 "int oneTwoThree = 123;", 15694 Alignment)); 15695 EXPECT_EQ("int a = 5;\n" 15696 "int one = 1;\n" 15697 "\n" 15698 "int oneTwoThree = 123;\n" 15699 "int oneTwo = 12;", 15700 format("int a = 5;\n" 15701 "int one = 1;\n" 15702 "\n" 15703 "int oneTwoThree = 123;\n" 15704 "int oneTwo = 12;", 15705 Alignment)); 15706 15707 /* Test across comments */ 15708 EXPECT_EQ("int a = 5;\n" 15709 "/* block comment */\n" 15710 "int oneTwoThree = 123;", 15711 format("int a = 5;\n" 15712 "/* block comment */\n" 15713 "int oneTwoThree=123;", 15714 Alignment)); 15715 15716 EXPECT_EQ("int a = 5;\n" 15717 "// line comment\n" 15718 "int oneTwoThree = 123;", 15719 format("int a = 5;\n" 15720 "// line comment\n" 15721 "int oneTwoThree=123;", 15722 Alignment)); 15723 15724 /* Test across comments and newlines */ 15725 EXPECT_EQ("int a = 5;\n" 15726 "\n" 15727 "/* block comment */\n" 15728 "int oneTwoThree = 123;", 15729 format("int a = 5;\n" 15730 "\n" 15731 "/* block comment */\n" 15732 "int oneTwoThree=123;", 15733 Alignment)); 15734 15735 EXPECT_EQ("int a = 5;\n" 15736 "\n" 15737 "// line comment\n" 15738 "int oneTwoThree = 123;", 15739 format("int a = 5;\n" 15740 "\n" 15741 "// line comment\n" 15742 "int oneTwoThree=123;", 15743 Alignment)); 15744 15745 EXPECT_EQ("int a = 5;\n" 15746 "//\n" 15747 "// multi-line line comment\n" 15748 "//\n" 15749 "int oneTwoThree = 123;", 15750 format("int a = 5;\n" 15751 "//\n" 15752 "// multi-line line comment\n" 15753 "//\n" 15754 "int oneTwoThree=123;", 15755 Alignment)); 15756 15757 EXPECT_EQ("int a = 5;\n" 15758 "/*\n" 15759 " * multi-line block comment\n" 15760 " */\n" 15761 "int oneTwoThree = 123;", 15762 format("int a = 5;\n" 15763 "/*\n" 15764 " * multi-line block comment\n" 15765 " */\n" 15766 "int oneTwoThree=123;", 15767 Alignment)); 15768 15769 EXPECT_EQ("int a = 5;\n" 15770 "\n" 15771 "/* block comment */\n" 15772 "\n" 15773 "\n" 15774 "\n" 15775 "int oneTwoThree = 123;", 15776 format("int a = 5;\n" 15777 "\n" 15778 "/* block comment */\n" 15779 "\n" 15780 "\n" 15781 "\n" 15782 "int oneTwoThree=123;", 15783 Alignment)); 15784 15785 EXPECT_EQ("int a = 5;\n" 15786 "\n" 15787 "// line comment\n" 15788 "\n" 15789 "\n" 15790 "\n" 15791 "int oneTwoThree = 123;", 15792 format("int a = 5;\n" 15793 "\n" 15794 "// line comment\n" 15795 "\n" 15796 "\n" 15797 "\n" 15798 "int oneTwoThree=123;", 15799 Alignment)); 15800 15801 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 15802 verifyFormat("#define A \\\n" 15803 " int aaaa = 12; \\\n" 15804 " int b = 23; \\\n" 15805 " int ccc = 234; \\\n" 15806 " int dddddddddd = 2345;", 15807 Alignment); 15808 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 15809 verifyFormat("#define A \\\n" 15810 " int aaaa = 12; \\\n" 15811 " int b = 23; \\\n" 15812 " int ccc = 234; \\\n" 15813 " int dddddddddd = 2345;", 15814 Alignment); 15815 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 15816 verifyFormat("#define A " 15817 " \\\n" 15818 " int aaaa = 12; " 15819 " \\\n" 15820 " int b = 23; " 15821 " \\\n" 15822 " int ccc = 234; " 15823 " \\\n" 15824 " int dddddddddd = 2345;", 15825 Alignment); 15826 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 15827 "k = 4, int l = 5,\n" 15828 " int m = 6) {\n" 15829 " int j = 10;\n" 15830 " otherThing = 1;\n" 15831 "}", 15832 Alignment); 15833 verifyFormat("void SomeFunction(int parameter = 0) {\n" 15834 " int i = 1;\n" 15835 " int j = 2;\n" 15836 " int big = 10000;\n" 15837 "}", 15838 Alignment); 15839 verifyFormat("class C {\n" 15840 "public:\n" 15841 " int i = 1;\n" 15842 " virtual void f() = 0;\n" 15843 "};", 15844 Alignment); 15845 verifyFormat("int i = 1;\n" 15846 "if (SomeType t = getSomething()) {\n" 15847 "}\n" 15848 "int j = 2;\n" 15849 "int big = 10000;", 15850 Alignment); 15851 verifyFormat("int j = 7;\n" 15852 "for (int k = 0; k < N; ++k) {\n" 15853 "}\n" 15854 "int j = 2;\n" 15855 "int big = 10000;\n" 15856 "}", 15857 Alignment); 15858 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 15859 verifyFormat("int i = 1;\n" 15860 "LooooooooooongType loooooooooooooooooooooongVariable\n" 15861 " = someLooooooooooooooooongFunction();\n" 15862 "int j = 2;", 15863 Alignment); 15864 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 15865 verifyFormat("int i = 1;\n" 15866 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 15867 " someLooooooooooooooooongFunction();\n" 15868 "int j = 2;", 15869 Alignment); 15870 15871 verifyFormat("auto lambda = []() {\n" 15872 " auto i = 0;\n" 15873 " return 0;\n" 15874 "};\n" 15875 "int i = 0;\n" 15876 "auto v = type{\n" 15877 " i = 1, //\n" 15878 " (i = 2), //\n" 15879 " i = 3 //\n" 15880 "};", 15881 Alignment); 15882 15883 verifyFormat( 15884 "int i = 1;\n" 15885 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 15886 " loooooooooooooooooooooongParameterB);\n" 15887 "int j = 2;", 15888 Alignment); 15889 15890 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 15891 " typename B = very_long_type_name_1,\n" 15892 " typename T_2 = very_long_type_name_2>\n" 15893 "auto foo() {}\n", 15894 Alignment); 15895 verifyFormat("int a, b = 1;\n" 15896 "int c = 2;\n" 15897 "int dd = 3;\n", 15898 Alignment); 15899 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 15900 "float b[1][] = {{3.f}};\n", 15901 Alignment); 15902 verifyFormat("for (int i = 0; i < 1; i++)\n" 15903 " int x = 1;\n", 15904 Alignment); 15905 verifyFormat("for (i = 0; i < 1; i++)\n" 15906 " x = 1;\n" 15907 "y = 1;\n", 15908 Alignment); 15909 15910 Alignment.ReflowComments = true; 15911 Alignment.ColumnLimit = 50; 15912 EXPECT_EQ("int x = 0;\n" 15913 "int yy = 1; /// specificlennospace\n" 15914 "int zzz = 2;\n", 15915 format("int x = 0;\n" 15916 "int yy = 1; ///specificlennospace\n" 15917 "int zzz = 2;\n", 15918 Alignment)); 15919 } 15920 15921 TEST_F(FormatTest, AlignConsecutiveAssignments) { 15922 FormatStyle Alignment = getLLVMStyle(); 15923 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15924 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 15925 verifyFormat("int a = 5;\n" 15926 "int oneTwoThree = 123;", 15927 Alignment); 15928 verifyFormat("int a = 5;\n" 15929 "int oneTwoThree = 123;", 15930 Alignment); 15931 15932 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 15933 verifyFormat("int a = 5;\n" 15934 "int oneTwoThree = 123;", 15935 Alignment); 15936 verifyFormat("int a = method();\n" 15937 "int oneTwoThree = 133;", 15938 Alignment); 15939 verifyFormat("a &= 5;\n" 15940 "bcd *= 5;\n" 15941 "ghtyf += 5;\n" 15942 "dvfvdb -= 5;\n" 15943 "a /= 5;\n" 15944 "vdsvsv %= 5;\n" 15945 "sfdbddfbdfbb ^= 5;\n" 15946 "dvsdsv |= 5;\n" 15947 "int dsvvdvsdvvv = 123;", 15948 Alignment); 15949 verifyFormat("int i = 1, j = 10;\n" 15950 "something = 2000;", 15951 Alignment); 15952 verifyFormat("something = 2000;\n" 15953 "int i = 1, j = 10;\n", 15954 Alignment); 15955 verifyFormat("something = 2000;\n" 15956 "another = 911;\n" 15957 "int i = 1, j = 10;\n" 15958 "oneMore = 1;\n" 15959 "i = 2;", 15960 Alignment); 15961 verifyFormat("int a = 5;\n" 15962 "int one = 1;\n" 15963 "method();\n" 15964 "int oneTwoThree = 123;\n" 15965 "int oneTwo = 12;", 15966 Alignment); 15967 verifyFormat("int oneTwoThree = 123;\n" 15968 "int oneTwo = 12;\n" 15969 "method();\n", 15970 Alignment); 15971 verifyFormat("int oneTwoThree = 123; // comment\n" 15972 "int oneTwo = 12; // comment", 15973 Alignment); 15974 15975 // Bug 25167 15976 /* Uncomment when fixed 15977 verifyFormat("#if A\n" 15978 "#else\n" 15979 "int aaaaaaaa = 12;\n" 15980 "#endif\n" 15981 "#if B\n" 15982 "#else\n" 15983 "int a = 12;\n" 15984 "#endif\n", 15985 Alignment); 15986 verifyFormat("enum foo {\n" 15987 "#if A\n" 15988 "#else\n" 15989 " aaaaaaaa = 12;\n" 15990 "#endif\n" 15991 "#if B\n" 15992 "#else\n" 15993 " a = 12;\n" 15994 "#endif\n" 15995 "};\n", 15996 Alignment); 15997 */ 15998 15999 EXPECT_EQ("int a = 5;\n" 16000 "\n" 16001 "int oneTwoThree = 123;", 16002 format("int a = 5;\n" 16003 "\n" 16004 "int oneTwoThree= 123;", 16005 Alignment)); 16006 EXPECT_EQ("int a = 5;\n" 16007 "int one = 1;\n" 16008 "\n" 16009 "int oneTwoThree = 123;", 16010 format("int a = 5;\n" 16011 "int one = 1;\n" 16012 "\n" 16013 "int oneTwoThree = 123;", 16014 Alignment)); 16015 EXPECT_EQ("int a = 5;\n" 16016 "int one = 1;\n" 16017 "\n" 16018 "int oneTwoThree = 123;\n" 16019 "int oneTwo = 12;", 16020 format("int a = 5;\n" 16021 "int one = 1;\n" 16022 "\n" 16023 "int oneTwoThree = 123;\n" 16024 "int oneTwo = 12;", 16025 Alignment)); 16026 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 16027 verifyFormat("#define A \\\n" 16028 " int aaaa = 12; \\\n" 16029 " int b = 23; \\\n" 16030 " int ccc = 234; \\\n" 16031 " int dddddddddd = 2345;", 16032 Alignment); 16033 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 16034 verifyFormat("#define A \\\n" 16035 " int aaaa = 12; \\\n" 16036 " int b = 23; \\\n" 16037 " int ccc = 234; \\\n" 16038 " int dddddddddd = 2345;", 16039 Alignment); 16040 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 16041 verifyFormat("#define A " 16042 " \\\n" 16043 " int aaaa = 12; " 16044 " \\\n" 16045 " int b = 23; " 16046 " \\\n" 16047 " int ccc = 234; " 16048 " \\\n" 16049 " int dddddddddd = 2345;", 16050 Alignment); 16051 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 16052 "k = 4, int l = 5,\n" 16053 " int m = 6) {\n" 16054 " int j = 10;\n" 16055 " otherThing = 1;\n" 16056 "}", 16057 Alignment); 16058 verifyFormat("void SomeFunction(int parameter = 0) {\n" 16059 " int i = 1;\n" 16060 " int j = 2;\n" 16061 " int big = 10000;\n" 16062 "}", 16063 Alignment); 16064 verifyFormat("class C {\n" 16065 "public:\n" 16066 " int i = 1;\n" 16067 " virtual void f() = 0;\n" 16068 "};", 16069 Alignment); 16070 verifyFormat("int i = 1;\n" 16071 "if (SomeType t = getSomething()) {\n" 16072 "}\n" 16073 "int j = 2;\n" 16074 "int big = 10000;", 16075 Alignment); 16076 verifyFormat("int j = 7;\n" 16077 "for (int k = 0; k < N; ++k) {\n" 16078 "}\n" 16079 "int j = 2;\n" 16080 "int big = 10000;\n" 16081 "}", 16082 Alignment); 16083 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 16084 verifyFormat("int i = 1;\n" 16085 "LooooooooooongType loooooooooooooooooooooongVariable\n" 16086 " = someLooooooooooooooooongFunction();\n" 16087 "int j = 2;", 16088 Alignment); 16089 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 16090 verifyFormat("int i = 1;\n" 16091 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 16092 " someLooooooooooooooooongFunction();\n" 16093 "int j = 2;", 16094 Alignment); 16095 16096 verifyFormat("auto lambda = []() {\n" 16097 " auto i = 0;\n" 16098 " return 0;\n" 16099 "};\n" 16100 "int i = 0;\n" 16101 "auto v = type{\n" 16102 " i = 1, //\n" 16103 " (i = 2), //\n" 16104 " i = 3 //\n" 16105 "};", 16106 Alignment); 16107 16108 verifyFormat( 16109 "int i = 1;\n" 16110 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 16111 " loooooooooooooooooooooongParameterB);\n" 16112 "int j = 2;", 16113 Alignment); 16114 16115 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 16116 " typename B = very_long_type_name_1,\n" 16117 " typename T_2 = very_long_type_name_2>\n" 16118 "auto foo() {}\n", 16119 Alignment); 16120 verifyFormat("int a, b = 1;\n" 16121 "int c = 2;\n" 16122 "int dd = 3;\n", 16123 Alignment); 16124 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 16125 "float b[1][] = {{3.f}};\n", 16126 Alignment); 16127 verifyFormat("for (int i = 0; i < 1; i++)\n" 16128 " int x = 1;\n", 16129 Alignment); 16130 verifyFormat("for (i = 0; i < 1; i++)\n" 16131 " x = 1;\n" 16132 "y = 1;\n", 16133 Alignment); 16134 16135 Alignment.ReflowComments = true; 16136 Alignment.ColumnLimit = 50; 16137 EXPECT_EQ("int x = 0;\n" 16138 "int yy = 1; /// specificlennospace\n" 16139 "int zzz = 2;\n", 16140 format("int x = 0;\n" 16141 "int yy = 1; ///specificlennospace\n" 16142 "int zzz = 2;\n", 16143 Alignment)); 16144 } 16145 16146 TEST_F(FormatTest, AlignConsecutiveBitFields) { 16147 FormatStyle Alignment = getLLVMStyle(); 16148 Alignment.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive; 16149 verifyFormat("int const a : 5;\n" 16150 "int oneTwoThree : 23;", 16151 Alignment); 16152 16153 // Initializers are allowed starting with c++2a 16154 verifyFormat("int const a : 5 = 1;\n" 16155 "int oneTwoThree : 23 = 0;", 16156 Alignment); 16157 16158 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16159 verifyFormat("int const a : 5;\n" 16160 "int oneTwoThree : 23;", 16161 Alignment); 16162 16163 verifyFormat("int const a : 5; // comment\n" 16164 "int oneTwoThree : 23; // comment", 16165 Alignment); 16166 16167 verifyFormat("int const a : 5 = 1;\n" 16168 "int oneTwoThree : 23 = 0;", 16169 Alignment); 16170 16171 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16172 verifyFormat("int const a : 5 = 1;\n" 16173 "int oneTwoThree : 23 = 0;", 16174 Alignment); 16175 verifyFormat("int const a : 5 = {1};\n" 16176 "int oneTwoThree : 23 = 0;", 16177 Alignment); 16178 16179 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None; 16180 verifyFormat("int const a :5;\n" 16181 "int oneTwoThree:23;", 16182 Alignment); 16183 16184 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before; 16185 verifyFormat("int const a :5;\n" 16186 "int oneTwoThree :23;", 16187 Alignment); 16188 16189 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After; 16190 verifyFormat("int const a : 5;\n" 16191 "int oneTwoThree: 23;", 16192 Alignment); 16193 16194 // Known limitations: ':' is only recognized as a bitfield colon when 16195 // followed by a number. 16196 /* 16197 verifyFormat("int oneTwoThree : SOME_CONSTANT;\n" 16198 "int a : 5;", 16199 Alignment); 16200 */ 16201 } 16202 16203 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 16204 FormatStyle Alignment = getLLVMStyle(); 16205 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 16206 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_None; 16207 Alignment.PointerAlignment = FormatStyle::PAS_Right; 16208 verifyFormat("float const a = 5;\n" 16209 "int oneTwoThree = 123;", 16210 Alignment); 16211 verifyFormat("int a = 5;\n" 16212 "float const oneTwoThree = 123;", 16213 Alignment); 16214 16215 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16216 verifyFormat("float const a = 5;\n" 16217 "int oneTwoThree = 123;", 16218 Alignment); 16219 verifyFormat("int a = method();\n" 16220 "float const oneTwoThree = 133;", 16221 Alignment); 16222 verifyFormat("int i = 1, j = 10;\n" 16223 "something = 2000;", 16224 Alignment); 16225 verifyFormat("something = 2000;\n" 16226 "int i = 1, j = 10;\n", 16227 Alignment); 16228 verifyFormat("float something = 2000;\n" 16229 "double another = 911;\n" 16230 "int i = 1, j = 10;\n" 16231 "const int *oneMore = 1;\n" 16232 "unsigned i = 2;", 16233 Alignment); 16234 verifyFormat("float a = 5;\n" 16235 "int one = 1;\n" 16236 "method();\n" 16237 "const double oneTwoThree = 123;\n" 16238 "const unsigned int oneTwo = 12;", 16239 Alignment); 16240 verifyFormat("int oneTwoThree{0}; // comment\n" 16241 "unsigned oneTwo; // comment", 16242 Alignment); 16243 verifyFormat("unsigned int *a;\n" 16244 "int *b;\n" 16245 "unsigned int Const *c;\n" 16246 "unsigned int const *d;\n" 16247 "unsigned int Const &e;\n" 16248 "unsigned int const &f;", 16249 Alignment); 16250 verifyFormat("Const unsigned int *c;\n" 16251 "const unsigned int *d;\n" 16252 "Const unsigned int &e;\n" 16253 "const unsigned int &f;\n" 16254 "const unsigned g;\n" 16255 "Const unsigned h;", 16256 Alignment); 16257 EXPECT_EQ("float const a = 5;\n" 16258 "\n" 16259 "int oneTwoThree = 123;", 16260 format("float const a = 5;\n" 16261 "\n" 16262 "int oneTwoThree= 123;", 16263 Alignment)); 16264 EXPECT_EQ("float a = 5;\n" 16265 "int one = 1;\n" 16266 "\n" 16267 "unsigned oneTwoThree = 123;", 16268 format("float a = 5;\n" 16269 "int one = 1;\n" 16270 "\n" 16271 "unsigned oneTwoThree = 123;", 16272 Alignment)); 16273 EXPECT_EQ("float a = 5;\n" 16274 "int one = 1;\n" 16275 "\n" 16276 "unsigned oneTwoThree = 123;\n" 16277 "int oneTwo = 12;", 16278 format("float a = 5;\n" 16279 "int one = 1;\n" 16280 "\n" 16281 "unsigned oneTwoThree = 123;\n" 16282 "int oneTwo = 12;", 16283 Alignment)); 16284 // Function prototype alignment 16285 verifyFormat("int a();\n" 16286 "double b();", 16287 Alignment); 16288 verifyFormat("int a(int x);\n" 16289 "double b();", 16290 Alignment); 16291 unsigned OldColumnLimit = Alignment.ColumnLimit; 16292 // We need to set ColumnLimit to zero, in order to stress nested alignments, 16293 // otherwise the function parameters will be re-flowed onto a single line. 16294 Alignment.ColumnLimit = 0; 16295 EXPECT_EQ("int a(int x,\n" 16296 " float y);\n" 16297 "double b(int x,\n" 16298 " double y);", 16299 format("int a(int x,\n" 16300 " float y);\n" 16301 "double b(int x,\n" 16302 " double y);", 16303 Alignment)); 16304 // This ensures that function parameters of function declarations are 16305 // correctly indented when their owning functions are indented. 16306 // The failure case here is for 'double y' to not be indented enough. 16307 EXPECT_EQ("double a(int x);\n" 16308 "int b(int y,\n" 16309 " double z);", 16310 format("double a(int x);\n" 16311 "int b(int y,\n" 16312 " double z);", 16313 Alignment)); 16314 // Set ColumnLimit low so that we induce wrapping immediately after 16315 // the function name and opening paren. 16316 Alignment.ColumnLimit = 13; 16317 verifyFormat("int function(\n" 16318 " int x,\n" 16319 " bool y);", 16320 Alignment); 16321 Alignment.ColumnLimit = OldColumnLimit; 16322 // Ensure function pointers don't screw up recursive alignment 16323 verifyFormat("int a(int x, void (*fp)(int y));\n" 16324 "double b();", 16325 Alignment); 16326 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16327 // Ensure recursive alignment is broken by function braces, so that the 16328 // "a = 1" does not align with subsequent assignments inside the function 16329 // body. 16330 verifyFormat("int func(int a = 1) {\n" 16331 " int b = 2;\n" 16332 " int cc = 3;\n" 16333 "}", 16334 Alignment); 16335 verifyFormat("float something = 2000;\n" 16336 "double another = 911;\n" 16337 "int i = 1, j = 10;\n" 16338 "const int *oneMore = 1;\n" 16339 "unsigned i = 2;", 16340 Alignment); 16341 verifyFormat("int oneTwoThree = {0}; // comment\n" 16342 "unsigned oneTwo = 0; // comment", 16343 Alignment); 16344 // Make sure that scope is correctly tracked, in the absence of braces 16345 verifyFormat("for (int i = 0; i < n; i++)\n" 16346 " j = i;\n" 16347 "double x = 1;\n", 16348 Alignment); 16349 verifyFormat("if (int i = 0)\n" 16350 " j = i;\n" 16351 "double x = 1;\n", 16352 Alignment); 16353 // Ensure operator[] and operator() are comprehended 16354 verifyFormat("struct test {\n" 16355 " long long int foo();\n" 16356 " int operator[](int a);\n" 16357 " double bar();\n" 16358 "};\n", 16359 Alignment); 16360 verifyFormat("struct test {\n" 16361 " long long int foo();\n" 16362 " int operator()(int a);\n" 16363 " double bar();\n" 16364 "};\n", 16365 Alignment); 16366 16367 // PAS_Right 16368 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16369 " int const i = 1;\n" 16370 " int *j = 2;\n" 16371 " int big = 10000;\n" 16372 "\n" 16373 " unsigned oneTwoThree = 123;\n" 16374 " int oneTwo = 12;\n" 16375 " method();\n" 16376 " float k = 2;\n" 16377 " int ll = 10000;\n" 16378 "}", 16379 format("void SomeFunction(int parameter= 0) {\n" 16380 " int const i= 1;\n" 16381 " int *j=2;\n" 16382 " int big = 10000;\n" 16383 "\n" 16384 "unsigned oneTwoThree =123;\n" 16385 "int oneTwo = 12;\n" 16386 " method();\n" 16387 "float k= 2;\n" 16388 "int ll=10000;\n" 16389 "}", 16390 Alignment)); 16391 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16392 " int const i = 1;\n" 16393 " int **j = 2, ***k;\n" 16394 " int &k = i;\n" 16395 " int &&l = i + j;\n" 16396 " int big = 10000;\n" 16397 "\n" 16398 " unsigned oneTwoThree = 123;\n" 16399 " int oneTwo = 12;\n" 16400 " method();\n" 16401 " float k = 2;\n" 16402 " int ll = 10000;\n" 16403 "}", 16404 format("void SomeFunction(int parameter= 0) {\n" 16405 " int const i= 1;\n" 16406 " int **j=2,***k;\n" 16407 "int &k=i;\n" 16408 "int &&l=i+j;\n" 16409 " int big = 10000;\n" 16410 "\n" 16411 "unsigned oneTwoThree =123;\n" 16412 "int oneTwo = 12;\n" 16413 " method();\n" 16414 "float k= 2;\n" 16415 "int ll=10000;\n" 16416 "}", 16417 Alignment)); 16418 // variables are aligned at their name, pointers are at the right most 16419 // position 16420 verifyFormat("int *a;\n" 16421 "int **b;\n" 16422 "int ***c;\n" 16423 "int foobar;\n", 16424 Alignment); 16425 16426 // PAS_Left 16427 FormatStyle AlignmentLeft = Alignment; 16428 AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left; 16429 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16430 " int const i = 1;\n" 16431 " int* j = 2;\n" 16432 " int big = 10000;\n" 16433 "\n" 16434 " unsigned oneTwoThree = 123;\n" 16435 " int oneTwo = 12;\n" 16436 " method();\n" 16437 " float k = 2;\n" 16438 " int ll = 10000;\n" 16439 "}", 16440 format("void SomeFunction(int parameter= 0) {\n" 16441 " int const i= 1;\n" 16442 " int *j=2;\n" 16443 " int big = 10000;\n" 16444 "\n" 16445 "unsigned oneTwoThree =123;\n" 16446 "int oneTwo = 12;\n" 16447 " method();\n" 16448 "float k= 2;\n" 16449 "int ll=10000;\n" 16450 "}", 16451 AlignmentLeft)); 16452 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16453 " int const i = 1;\n" 16454 " int** j = 2;\n" 16455 " int& k = i;\n" 16456 " int&& l = i + j;\n" 16457 " int big = 10000;\n" 16458 "\n" 16459 " unsigned oneTwoThree = 123;\n" 16460 " int oneTwo = 12;\n" 16461 " method();\n" 16462 " float k = 2;\n" 16463 " int ll = 10000;\n" 16464 "}", 16465 format("void SomeFunction(int parameter= 0) {\n" 16466 " int const i= 1;\n" 16467 " int **j=2;\n" 16468 "int &k=i;\n" 16469 "int &&l=i+j;\n" 16470 " int big = 10000;\n" 16471 "\n" 16472 "unsigned oneTwoThree =123;\n" 16473 "int oneTwo = 12;\n" 16474 " method();\n" 16475 "float k= 2;\n" 16476 "int ll=10000;\n" 16477 "}", 16478 AlignmentLeft)); 16479 // variables are aligned at their name, pointers are at the left most position 16480 verifyFormat("int* a;\n" 16481 "int** b;\n" 16482 "int*** c;\n" 16483 "int foobar;\n", 16484 AlignmentLeft); 16485 16486 // PAS_Middle 16487 FormatStyle AlignmentMiddle = Alignment; 16488 AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle; 16489 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16490 " int const i = 1;\n" 16491 " int * j = 2;\n" 16492 " int big = 10000;\n" 16493 "\n" 16494 " unsigned oneTwoThree = 123;\n" 16495 " int oneTwo = 12;\n" 16496 " method();\n" 16497 " float k = 2;\n" 16498 " int ll = 10000;\n" 16499 "}", 16500 format("void SomeFunction(int parameter= 0) {\n" 16501 " int const i= 1;\n" 16502 " int *j=2;\n" 16503 " int big = 10000;\n" 16504 "\n" 16505 "unsigned oneTwoThree =123;\n" 16506 "int oneTwo = 12;\n" 16507 " method();\n" 16508 "float k= 2;\n" 16509 "int ll=10000;\n" 16510 "}", 16511 AlignmentMiddle)); 16512 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16513 " int const i = 1;\n" 16514 " int ** j = 2, ***k;\n" 16515 " int & k = i;\n" 16516 " int && l = i + j;\n" 16517 " int big = 10000;\n" 16518 "\n" 16519 " unsigned oneTwoThree = 123;\n" 16520 " int oneTwo = 12;\n" 16521 " method();\n" 16522 " float k = 2;\n" 16523 " int ll = 10000;\n" 16524 "}", 16525 format("void SomeFunction(int parameter= 0) {\n" 16526 " int const i= 1;\n" 16527 " int **j=2,***k;\n" 16528 "int &k=i;\n" 16529 "int &&l=i+j;\n" 16530 " int big = 10000;\n" 16531 "\n" 16532 "unsigned oneTwoThree =123;\n" 16533 "int oneTwo = 12;\n" 16534 " method();\n" 16535 "float k= 2;\n" 16536 "int ll=10000;\n" 16537 "}", 16538 AlignmentMiddle)); 16539 // variables are aligned at their name, pointers are in the middle 16540 verifyFormat("int * a;\n" 16541 "int * b;\n" 16542 "int *** c;\n" 16543 "int foobar;\n", 16544 AlignmentMiddle); 16545 16546 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16547 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 16548 verifyFormat("#define A \\\n" 16549 " int aaaa = 12; \\\n" 16550 " float b = 23; \\\n" 16551 " const int ccc = 234; \\\n" 16552 " unsigned dddddddddd = 2345;", 16553 Alignment); 16554 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 16555 verifyFormat("#define A \\\n" 16556 " int aaaa = 12; \\\n" 16557 " float b = 23; \\\n" 16558 " const int ccc = 234; \\\n" 16559 " unsigned dddddddddd = 2345;", 16560 Alignment); 16561 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 16562 Alignment.ColumnLimit = 30; 16563 verifyFormat("#define A \\\n" 16564 " int aaaa = 12; \\\n" 16565 " float b = 23; \\\n" 16566 " const int ccc = 234; \\\n" 16567 " int dddddddddd = 2345;", 16568 Alignment); 16569 Alignment.ColumnLimit = 80; 16570 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 16571 "k = 4, int l = 5,\n" 16572 " int m = 6) {\n" 16573 " const int j = 10;\n" 16574 " otherThing = 1;\n" 16575 "}", 16576 Alignment); 16577 verifyFormat("void SomeFunction(int parameter = 0) {\n" 16578 " int const i = 1;\n" 16579 " int *j = 2;\n" 16580 " int big = 10000;\n" 16581 "}", 16582 Alignment); 16583 verifyFormat("class C {\n" 16584 "public:\n" 16585 " int i = 1;\n" 16586 " virtual void f() = 0;\n" 16587 "};", 16588 Alignment); 16589 verifyFormat("float i = 1;\n" 16590 "if (SomeType t = getSomething()) {\n" 16591 "}\n" 16592 "const unsigned j = 2;\n" 16593 "int big = 10000;", 16594 Alignment); 16595 verifyFormat("float j = 7;\n" 16596 "for (int k = 0; k < N; ++k) {\n" 16597 "}\n" 16598 "unsigned j = 2;\n" 16599 "int big = 10000;\n" 16600 "}", 16601 Alignment); 16602 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 16603 verifyFormat("float i = 1;\n" 16604 "LooooooooooongType loooooooooooooooooooooongVariable\n" 16605 " = someLooooooooooooooooongFunction();\n" 16606 "int j = 2;", 16607 Alignment); 16608 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 16609 verifyFormat("int i = 1;\n" 16610 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 16611 " someLooooooooooooooooongFunction();\n" 16612 "int j = 2;", 16613 Alignment); 16614 16615 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16616 verifyFormat("auto lambda = []() {\n" 16617 " auto ii = 0;\n" 16618 " float j = 0;\n" 16619 " return 0;\n" 16620 "};\n" 16621 "int i = 0;\n" 16622 "float i2 = 0;\n" 16623 "auto v = type{\n" 16624 " i = 1, //\n" 16625 " (i = 2), //\n" 16626 " i = 3 //\n" 16627 "};", 16628 Alignment); 16629 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16630 16631 verifyFormat( 16632 "int i = 1;\n" 16633 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 16634 " loooooooooooooooooooooongParameterB);\n" 16635 "int j = 2;", 16636 Alignment); 16637 16638 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 16639 // We expect declarations and assignments to align, as long as it doesn't 16640 // exceed the column limit, starting a new alignment sequence whenever it 16641 // happens. 16642 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16643 Alignment.ColumnLimit = 30; 16644 verifyFormat("float ii = 1;\n" 16645 "unsigned j = 2;\n" 16646 "int someVerylongVariable = 1;\n" 16647 "AnotherLongType ll = 123456;\n" 16648 "VeryVeryLongType k = 2;\n" 16649 "int myvar = 1;", 16650 Alignment); 16651 Alignment.ColumnLimit = 80; 16652 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16653 16654 verifyFormat( 16655 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 16656 " typename LongType, typename B>\n" 16657 "auto foo() {}\n", 16658 Alignment); 16659 verifyFormat("float a, b = 1;\n" 16660 "int c = 2;\n" 16661 "int dd = 3;\n", 16662 Alignment); 16663 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 16664 "float b[1][] = {{3.f}};\n", 16665 Alignment); 16666 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16667 verifyFormat("float a, b = 1;\n" 16668 "int c = 2;\n" 16669 "int dd = 3;\n", 16670 Alignment); 16671 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 16672 "float b[1][] = {{3.f}};\n", 16673 Alignment); 16674 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16675 16676 Alignment.ColumnLimit = 30; 16677 Alignment.BinPackParameters = false; 16678 verifyFormat("void foo(float a,\n" 16679 " float b,\n" 16680 " int c,\n" 16681 " uint32_t *d) {\n" 16682 " int *e = 0;\n" 16683 " float f = 0;\n" 16684 " double g = 0;\n" 16685 "}\n" 16686 "void bar(ino_t a,\n" 16687 " int b,\n" 16688 " uint32_t *c,\n" 16689 " bool d) {}\n", 16690 Alignment); 16691 Alignment.BinPackParameters = true; 16692 Alignment.ColumnLimit = 80; 16693 16694 // Bug 33507 16695 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 16696 verifyFormat( 16697 "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n" 16698 " static const Version verVs2017;\n" 16699 " return true;\n" 16700 "});\n", 16701 Alignment); 16702 Alignment.PointerAlignment = FormatStyle::PAS_Right; 16703 16704 // See llvm.org/PR35641 16705 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16706 verifyFormat("int func() { //\n" 16707 " int b;\n" 16708 " unsigned c;\n" 16709 "}", 16710 Alignment); 16711 16712 // See PR37175 16713 FormatStyle Style = getMozillaStyle(); 16714 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16715 EXPECT_EQ("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n" 16716 "foo(int a);", 16717 format("DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style)); 16718 16719 Alignment.PointerAlignment = FormatStyle::PAS_Left; 16720 verifyFormat("unsigned int* a;\n" 16721 "int* b;\n" 16722 "unsigned int Const* c;\n" 16723 "unsigned int const* d;\n" 16724 "unsigned int Const& e;\n" 16725 "unsigned int const& f;", 16726 Alignment); 16727 verifyFormat("Const unsigned int* c;\n" 16728 "const unsigned int* d;\n" 16729 "Const unsigned int& e;\n" 16730 "const unsigned int& f;\n" 16731 "const unsigned g;\n" 16732 "Const unsigned h;", 16733 Alignment); 16734 16735 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 16736 verifyFormat("unsigned int * a;\n" 16737 "int * b;\n" 16738 "unsigned int Const * c;\n" 16739 "unsigned int const * d;\n" 16740 "unsigned int Const & e;\n" 16741 "unsigned int const & f;", 16742 Alignment); 16743 verifyFormat("Const unsigned int * c;\n" 16744 "const unsigned int * d;\n" 16745 "Const unsigned int & e;\n" 16746 "const unsigned int & f;\n" 16747 "const unsigned g;\n" 16748 "Const unsigned h;", 16749 Alignment); 16750 } 16751 16752 TEST_F(FormatTest, AlignWithLineBreaks) { 16753 auto Style = getLLVMStyleWithColumns(120); 16754 16755 EXPECT_EQ(Style.AlignConsecutiveAssignments, FormatStyle::ACS_None); 16756 EXPECT_EQ(Style.AlignConsecutiveDeclarations, FormatStyle::ACS_None); 16757 verifyFormat("void foo() {\n" 16758 " int myVar = 5;\n" 16759 " double x = 3.14;\n" 16760 " auto str = \"Hello \"\n" 16761 " \"World\";\n" 16762 " auto s = \"Hello \"\n" 16763 " \"Again\";\n" 16764 "}", 16765 Style); 16766 16767 // clang-format off 16768 verifyFormat("void foo() {\n" 16769 " const int capacityBefore = Entries.capacity();\n" 16770 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16771 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16772 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16773 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16774 "}", 16775 Style); 16776 // clang-format on 16777 16778 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16779 verifyFormat("void foo() {\n" 16780 " int myVar = 5;\n" 16781 " double x = 3.14;\n" 16782 " auto str = \"Hello \"\n" 16783 " \"World\";\n" 16784 " auto s = \"Hello \"\n" 16785 " \"Again\";\n" 16786 "}", 16787 Style); 16788 16789 // clang-format off 16790 verifyFormat("void foo() {\n" 16791 " const int capacityBefore = Entries.capacity();\n" 16792 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16793 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16794 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16795 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16796 "}", 16797 Style); 16798 // clang-format on 16799 16800 Style.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16801 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16802 verifyFormat("void foo() {\n" 16803 " int myVar = 5;\n" 16804 " double x = 3.14;\n" 16805 " auto str = \"Hello \"\n" 16806 " \"World\";\n" 16807 " auto s = \"Hello \"\n" 16808 " \"Again\";\n" 16809 "}", 16810 Style); 16811 16812 // clang-format off 16813 verifyFormat("void foo() {\n" 16814 " const int capacityBefore = Entries.capacity();\n" 16815 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16816 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16817 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16818 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16819 "}", 16820 Style); 16821 // clang-format on 16822 16823 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16824 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16825 16826 verifyFormat("void foo() {\n" 16827 " int myVar = 5;\n" 16828 " double x = 3.14;\n" 16829 " auto str = \"Hello \"\n" 16830 " \"World\";\n" 16831 " auto s = \"Hello \"\n" 16832 " \"Again\";\n" 16833 "}", 16834 Style); 16835 16836 // clang-format off 16837 verifyFormat("void foo() {\n" 16838 " const int capacityBefore = Entries.capacity();\n" 16839 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16840 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16841 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16842 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16843 "}", 16844 Style); 16845 // clang-format on 16846 16847 Style = getLLVMStyleWithColumns(120); 16848 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16849 Style.ContinuationIndentWidth = 4; 16850 Style.IndentWidth = 4; 16851 16852 // clang-format off 16853 verifyFormat("void SomeFunc() {\n" 16854 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 16855 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16856 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 16857 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16858 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 16859 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16860 "}", 16861 Style); 16862 // clang-format on 16863 16864 Style.BinPackArguments = false; 16865 16866 // clang-format off 16867 verifyFormat("void SomeFunc() {\n" 16868 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(\n" 16869 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16870 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(\n" 16871 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16872 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(\n" 16873 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16874 "}", 16875 Style); 16876 // clang-format on 16877 } 16878 16879 TEST_F(FormatTest, AlignWithInitializerPeriods) { 16880 auto Style = getLLVMStyleWithColumns(60); 16881 16882 verifyFormat("void foo1(void) {\n" 16883 " BYTE p[1] = 1;\n" 16884 " A B = {.one_foooooooooooooooo = 2,\n" 16885 " .two_fooooooooooooo = 3,\n" 16886 " .three_fooooooooooooo = 4};\n" 16887 " BYTE payload = 2;\n" 16888 "}", 16889 Style); 16890 16891 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16892 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_None; 16893 verifyFormat("void foo2(void) {\n" 16894 " BYTE p[1] = 1;\n" 16895 " A B = {.one_foooooooooooooooo = 2,\n" 16896 " .two_fooooooooooooo = 3,\n" 16897 " .three_fooooooooooooo = 4};\n" 16898 " BYTE payload = 2;\n" 16899 "}", 16900 Style); 16901 16902 Style.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16903 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16904 verifyFormat("void foo3(void) {\n" 16905 " BYTE p[1] = 1;\n" 16906 " A B = {.one_foooooooooooooooo = 2,\n" 16907 " .two_fooooooooooooo = 3,\n" 16908 " .three_fooooooooooooo = 4};\n" 16909 " BYTE payload = 2;\n" 16910 "}", 16911 Style); 16912 16913 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16914 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16915 verifyFormat("void foo4(void) {\n" 16916 " BYTE p[1] = 1;\n" 16917 " A B = {.one_foooooooooooooooo = 2,\n" 16918 " .two_fooooooooooooo = 3,\n" 16919 " .three_fooooooooooooo = 4};\n" 16920 " BYTE payload = 2;\n" 16921 "}", 16922 Style); 16923 } 16924 16925 TEST_F(FormatTest, LinuxBraceBreaking) { 16926 FormatStyle LinuxBraceStyle = getLLVMStyle(); 16927 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 16928 verifyFormat("namespace a\n" 16929 "{\n" 16930 "class A\n" 16931 "{\n" 16932 " void f()\n" 16933 " {\n" 16934 " if (true) {\n" 16935 " a();\n" 16936 " b();\n" 16937 " } else {\n" 16938 " a();\n" 16939 " }\n" 16940 " }\n" 16941 " void g() { return; }\n" 16942 "};\n" 16943 "struct B {\n" 16944 " int x;\n" 16945 "};\n" 16946 "} // namespace a\n", 16947 LinuxBraceStyle); 16948 verifyFormat("enum X {\n" 16949 " Y = 0,\n" 16950 "}\n", 16951 LinuxBraceStyle); 16952 verifyFormat("struct S {\n" 16953 " int Type;\n" 16954 " union {\n" 16955 " int x;\n" 16956 " double y;\n" 16957 " } Value;\n" 16958 " class C\n" 16959 " {\n" 16960 " MyFavoriteType Value;\n" 16961 " } Class;\n" 16962 "}\n", 16963 LinuxBraceStyle); 16964 } 16965 16966 TEST_F(FormatTest, MozillaBraceBreaking) { 16967 FormatStyle MozillaBraceStyle = getLLVMStyle(); 16968 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 16969 MozillaBraceStyle.FixNamespaceComments = false; 16970 verifyFormat("namespace a {\n" 16971 "class A\n" 16972 "{\n" 16973 " void f()\n" 16974 " {\n" 16975 " if (true) {\n" 16976 " a();\n" 16977 " b();\n" 16978 " }\n" 16979 " }\n" 16980 " void g() { return; }\n" 16981 "};\n" 16982 "enum E\n" 16983 "{\n" 16984 " A,\n" 16985 " // foo\n" 16986 " B,\n" 16987 " C\n" 16988 "};\n" 16989 "struct B\n" 16990 "{\n" 16991 " int x;\n" 16992 "};\n" 16993 "}\n", 16994 MozillaBraceStyle); 16995 verifyFormat("struct S\n" 16996 "{\n" 16997 " int Type;\n" 16998 " union\n" 16999 " {\n" 17000 " int x;\n" 17001 " double y;\n" 17002 " } Value;\n" 17003 " class C\n" 17004 " {\n" 17005 " MyFavoriteType Value;\n" 17006 " } Class;\n" 17007 "}\n", 17008 MozillaBraceStyle); 17009 } 17010 17011 TEST_F(FormatTest, StroustrupBraceBreaking) { 17012 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 17013 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 17014 verifyFormat("namespace a {\n" 17015 "class A {\n" 17016 " void f()\n" 17017 " {\n" 17018 " if (true) {\n" 17019 " a();\n" 17020 " b();\n" 17021 " }\n" 17022 " }\n" 17023 " void g() { return; }\n" 17024 "};\n" 17025 "struct B {\n" 17026 " int x;\n" 17027 "};\n" 17028 "} // namespace a\n", 17029 StroustrupBraceStyle); 17030 17031 verifyFormat("void foo()\n" 17032 "{\n" 17033 " if (a) {\n" 17034 " a();\n" 17035 " }\n" 17036 " else {\n" 17037 " b();\n" 17038 " }\n" 17039 "}\n", 17040 StroustrupBraceStyle); 17041 17042 verifyFormat("#ifdef _DEBUG\n" 17043 "int foo(int i = 0)\n" 17044 "#else\n" 17045 "int foo(int i = 5)\n" 17046 "#endif\n" 17047 "{\n" 17048 " return i;\n" 17049 "}", 17050 StroustrupBraceStyle); 17051 17052 verifyFormat("void foo() {}\n" 17053 "void bar()\n" 17054 "#ifdef _DEBUG\n" 17055 "{\n" 17056 " foo();\n" 17057 "}\n" 17058 "#else\n" 17059 "{\n" 17060 "}\n" 17061 "#endif", 17062 StroustrupBraceStyle); 17063 17064 verifyFormat("void foobar() { int i = 5; }\n" 17065 "#ifdef _DEBUG\n" 17066 "void bar() {}\n" 17067 "#else\n" 17068 "void bar() { foobar(); }\n" 17069 "#endif", 17070 StroustrupBraceStyle); 17071 } 17072 17073 TEST_F(FormatTest, AllmanBraceBreaking) { 17074 FormatStyle AllmanBraceStyle = getLLVMStyle(); 17075 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 17076 17077 EXPECT_EQ("namespace a\n" 17078 "{\n" 17079 "void f();\n" 17080 "void g();\n" 17081 "} // namespace a\n", 17082 format("namespace a\n" 17083 "{\n" 17084 "void f();\n" 17085 "void g();\n" 17086 "}\n", 17087 AllmanBraceStyle)); 17088 17089 verifyFormat("namespace a\n" 17090 "{\n" 17091 "class A\n" 17092 "{\n" 17093 " void f()\n" 17094 " {\n" 17095 " if (true)\n" 17096 " {\n" 17097 " a();\n" 17098 " b();\n" 17099 " }\n" 17100 " }\n" 17101 " void g() { return; }\n" 17102 "};\n" 17103 "struct B\n" 17104 "{\n" 17105 " int x;\n" 17106 "};\n" 17107 "union C\n" 17108 "{\n" 17109 "};\n" 17110 "} // namespace a", 17111 AllmanBraceStyle); 17112 17113 verifyFormat("void f()\n" 17114 "{\n" 17115 " if (true)\n" 17116 " {\n" 17117 " a();\n" 17118 " }\n" 17119 " else if (false)\n" 17120 " {\n" 17121 " b();\n" 17122 " }\n" 17123 " else\n" 17124 " {\n" 17125 " c();\n" 17126 " }\n" 17127 "}\n", 17128 AllmanBraceStyle); 17129 17130 verifyFormat("void f()\n" 17131 "{\n" 17132 " for (int i = 0; i < 10; ++i)\n" 17133 " {\n" 17134 " a();\n" 17135 " }\n" 17136 " while (false)\n" 17137 " {\n" 17138 " b();\n" 17139 " }\n" 17140 " do\n" 17141 " {\n" 17142 " c();\n" 17143 " } while (false)\n" 17144 "}\n", 17145 AllmanBraceStyle); 17146 17147 verifyFormat("void f(int a)\n" 17148 "{\n" 17149 " switch (a)\n" 17150 " {\n" 17151 " case 0:\n" 17152 " break;\n" 17153 " case 1:\n" 17154 " {\n" 17155 " break;\n" 17156 " }\n" 17157 " case 2:\n" 17158 " {\n" 17159 " }\n" 17160 " break;\n" 17161 " default:\n" 17162 " break;\n" 17163 " }\n" 17164 "}\n", 17165 AllmanBraceStyle); 17166 17167 verifyFormat("enum X\n" 17168 "{\n" 17169 " Y = 0,\n" 17170 "}\n", 17171 AllmanBraceStyle); 17172 verifyFormat("enum X\n" 17173 "{\n" 17174 " Y = 0\n" 17175 "}\n", 17176 AllmanBraceStyle); 17177 17178 verifyFormat("@interface BSApplicationController ()\n" 17179 "{\n" 17180 "@private\n" 17181 " id _extraIvar;\n" 17182 "}\n" 17183 "@end\n", 17184 AllmanBraceStyle); 17185 17186 verifyFormat("#ifdef _DEBUG\n" 17187 "int foo(int i = 0)\n" 17188 "#else\n" 17189 "int foo(int i = 5)\n" 17190 "#endif\n" 17191 "{\n" 17192 " return i;\n" 17193 "}", 17194 AllmanBraceStyle); 17195 17196 verifyFormat("void foo() {}\n" 17197 "void bar()\n" 17198 "#ifdef _DEBUG\n" 17199 "{\n" 17200 " foo();\n" 17201 "}\n" 17202 "#else\n" 17203 "{\n" 17204 "}\n" 17205 "#endif", 17206 AllmanBraceStyle); 17207 17208 verifyFormat("void foobar() { int i = 5; }\n" 17209 "#ifdef _DEBUG\n" 17210 "void bar() {}\n" 17211 "#else\n" 17212 "void bar() { foobar(); }\n" 17213 "#endif", 17214 AllmanBraceStyle); 17215 17216 EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine, 17217 FormatStyle::SLS_All); 17218 17219 verifyFormat("[](int i) { return i + 2; };\n" 17220 "[](int i, int j)\n" 17221 "{\n" 17222 " auto x = i + j;\n" 17223 " auto y = i * j;\n" 17224 " return x ^ y;\n" 17225 "};\n" 17226 "void foo()\n" 17227 "{\n" 17228 " auto shortLambda = [](int i) { return i + 2; };\n" 17229 " auto longLambda = [](int i, int j)\n" 17230 " {\n" 17231 " auto x = i + j;\n" 17232 " auto y = i * j;\n" 17233 " return x ^ y;\n" 17234 " };\n" 17235 "}", 17236 AllmanBraceStyle); 17237 17238 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 17239 17240 verifyFormat("[](int i)\n" 17241 "{\n" 17242 " return i + 2;\n" 17243 "};\n" 17244 "[](int i, int j)\n" 17245 "{\n" 17246 " auto x = i + j;\n" 17247 " auto y = i * j;\n" 17248 " return x ^ y;\n" 17249 "};\n" 17250 "void foo()\n" 17251 "{\n" 17252 " auto shortLambda = [](int i)\n" 17253 " {\n" 17254 " return i + 2;\n" 17255 " };\n" 17256 " auto longLambda = [](int i, int j)\n" 17257 " {\n" 17258 " auto x = i + j;\n" 17259 " auto y = i * j;\n" 17260 " return x ^ y;\n" 17261 " };\n" 17262 "}", 17263 AllmanBraceStyle); 17264 17265 // Reset 17266 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All; 17267 17268 // This shouldn't affect ObjC blocks.. 17269 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 17270 " // ...\n" 17271 " int i;\n" 17272 "}];", 17273 AllmanBraceStyle); 17274 verifyFormat("void (^block)(void) = ^{\n" 17275 " // ...\n" 17276 " int i;\n" 17277 "};", 17278 AllmanBraceStyle); 17279 // .. or dict literals. 17280 verifyFormat("void f()\n" 17281 "{\n" 17282 " // ...\n" 17283 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 17284 "}", 17285 AllmanBraceStyle); 17286 verifyFormat("void f()\n" 17287 "{\n" 17288 " // ...\n" 17289 " [object someMethod:@{a : @\"b\"}];\n" 17290 "}", 17291 AllmanBraceStyle); 17292 verifyFormat("int f()\n" 17293 "{ // comment\n" 17294 " return 42;\n" 17295 "}", 17296 AllmanBraceStyle); 17297 17298 AllmanBraceStyle.ColumnLimit = 19; 17299 verifyFormat("void f() { int i; }", AllmanBraceStyle); 17300 AllmanBraceStyle.ColumnLimit = 18; 17301 verifyFormat("void f()\n" 17302 "{\n" 17303 " int i;\n" 17304 "}", 17305 AllmanBraceStyle); 17306 AllmanBraceStyle.ColumnLimit = 80; 17307 17308 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 17309 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = 17310 FormatStyle::SIS_WithoutElse; 17311 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 17312 verifyFormat("void f(bool b)\n" 17313 "{\n" 17314 " if (b)\n" 17315 " {\n" 17316 " return;\n" 17317 " }\n" 17318 "}\n", 17319 BreakBeforeBraceShortIfs); 17320 verifyFormat("void f(bool b)\n" 17321 "{\n" 17322 " if constexpr (b)\n" 17323 " {\n" 17324 " return;\n" 17325 " }\n" 17326 "}\n", 17327 BreakBeforeBraceShortIfs); 17328 verifyFormat("void f(bool b)\n" 17329 "{\n" 17330 " if CONSTEXPR (b)\n" 17331 " {\n" 17332 " return;\n" 17333 " }\n" 17334 "}\n", 17335 BreakBeforeBraceShortIfs); 17336 verifyFormat("void f(bool b)\n" 17337 "{\n" 17338 " if (b) return;\n" 17339 "}\n", 17340 BreakBeforeBraceShortIfs); 17341 verifyFormat("void f(bool b)\n" 17342 "{\n" 17343 " if constexpr (b) return;\n" 17344 "}\n", 17345 BreakBeforeBraceShortIfs); 17346 verifyFormat("void f(bool b)\n" 17347 "{\n" 17348 " if CONSTEXPR (b) return;\n" 17349 "}\n", 17350 BreakBeforeBraceShortIfs); 17351 verifyFormat("void f(bool b)\n" 17352 "{\n" 17353 " while (b)\n" 17354 " {\n" 17355 " return;\n" 17356 " }\n" 17357 "}\n", 17358 BreakBeforeBraceShortIfs); 17359 } 17360 17361 TEST_F(FormatTest, WhitesmithsBraceBreaking) { 17362 FormatStyle WhitesmithsBraceStyle = getLLVMStyle(); 17363 WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 17364 17365 // Make a few changes to the style for testing purposes 17366 WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine = 17367 FormatStyle::SFS_Empty; 17368 WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 17369 WhitesmithsBraceStyle.ColumnLimit = 0; 17370 17371 // FIXME: this test case can't decide whether there should be a blank line 17372 // after the ~D() line or not. It adds one if one doesn't exist in the test 17373 // and it removes the line if one exists. 17374 /* 17375 verifyFormat("class A;\n" 17376 "namespace B\n" 17377 " {\n" 17378 "class C;\n" 17379 "// Comment\n" 17380 "class D\n" 17381 " {\n" 17382 "public:\n" 17383 " D();\n" 17384 " ~D() {}\n" 17385 "private:\n" 17386 " enum E\n" 17387 " {\n" 17388 " F\n" 17389 " }\n" 17390 " };\n" 17391 " } // namespace B\n", 17392 WhitesmithsBraceStyle); 17393 */ 17394 17395 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None; 17396 verifyFormat("namespace a\n" 17397 " {\n" 17398 "class A\n" 17399 " {\n" 17400 " void f()\n" 17401 " {\n" 17402 " if (true)\n" 17403 " {\n" 17404 " a();\n" 17405 " b();\n" 17406 " }\n" 17407 " }\n" 17408 " void g()\n" 17409 " {\n" 17410 " return;\n" 17411 " }\n" 17412 " };\n" 17413 "struct B\n" 17414 " {\n" 17415 " int x;\n" 17416 " };\n" 17417 " } // namespace a", 17418 WhitesmithsBraceStyle); 17419 17420 verifyFormat("namespace a\n" 17421 " {\n" 17422 "namespace b\n" 17423 " {\n" 17424 "class A\n" 17425 " {\n" 17426 " void f()\n" 17427 " {\n" 17428 " if (true)\n" 17429 " {\n" 17430 " a();\n" 17431 " b();\n" 17432 " }\n" 17433 " }\n" 17434 " void g()\n" 17435 " {\n" 17436 " return;\n" 17437 " }\n" 17438 " };\n" 17439 "struct B\n" 17440 " {\n" 17441 " int x;\n" 17442 " };\n" 17443 " } // namespace b\n" 17444 " } // namespace a", 17445 WhitesmithsBraceStyle); 17446 17447 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner; 17448 verifyFormat("namespace a\n" 17449 " {\n" 17450 "namespace b\n" 17451 " {\n" 17452 " class A\n" 17453 " {\n" 17454 " void f()\n" 17455 " {\n" 17456 " if (true)\n" 17457 " {\n" 17458 " a();\n" 17459 " b();\n" 17460 " }\n" 17461 " }\n" 17462 " void g()\n" 17463 " {\n" 17464 " return;\n" 17465 " }\n" 17466 " };\n" 17467 " struct B\n" 17468 " {\n" 17469 " int x;\n" 17470 " };\n" 17471 " } // namespace b\n" 17472 " } // namespace a", 17473 WhitesmithsBraceStyle); 17474 17475 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All; 17476 verifyFormat("namespace a\n" 17477 " {\n" 17478 " namespace b\n" 17479 " {\n" 17480 " class A\n" 17481 " {\n" 17482 " void f()\n" 17483 " {\n" 17484 " if (true)\n" 17485 " {\n" 17486 " a();\n" 17487 " b();\n" 17488 " }\n" 17489 " }\n" 17490 " void g()\n" 17491 " {\n" 17492 " return;\n" 17493 " }\n" 17494 " };\n" 17495 " struct B\n" 17496 " {\n" 17497 " int x;\n" 17498 " };\n" 17499 " } // namespace b\n" 17500 " } // namespace a", 17501 WhitesmithsBraceStyle); 17502 17503 verifyFormat("void f()\n" 17504 " {\n" 17505 " if (true)\n" 17506 " {\n" 17507 " a();\n" 17508 " }\n" 17509 " else if (false)\n" 17510 " {\n" 17511 " b();\n" 17512 " }\n" 17513 " else\n" 17514 " {\n" 17515 " c();\n" 17516 " }\n" 17517 " }\n", 17518 WhitesmithsBraceStyle); 17519 17520 verifyFormat("void f()\n" 17521 " {\n" 17522 " for (int i = 0; i < 10; ++i)\n" 17523 " {\n" 17524 " a();\n" 17525 " }\n" 17526 " while (false)\n" 17527 " {\n" 17528 " b();\n" 17529 " }\n" 17530 " do\n" 17531 " {\n" 17532 " c();\n" 17533 " } while (false)\n" 17534 " }\n", 17535 WhitesmithsBraceStyle); 17536 17537 WhitesmithsBraceStyle.IndentCaseLabels = true; 17538 verifyFormat("void switchTest1(int a)\n" 17539 " {\n" 17540 " switch (a)\n" 17541 " {\n" 17542 " case 2:\n" 17543 " {\n" 17544 " }\n" 17545 " break;\n" 17546 " }\n" 17547 " }\n", 17548 WhitesmithsBraceStyle); 17549 17550 verifyFormat("void switchTest2(int a)\n" 17551 " {\n" 17552 " switch (a)\n" 17553 " {\n" 17554 " case 0:\n" 17555 " break;\n" 17556 " case 1:\n" 17557 " {\n" 17558 " break;\n" 17559 " }\n" 17560 " case 2:\n" 17561 " {\n" 17562 " }\n" 17563 " break;\n" 17564 " default:\n" 17565 " break;\n" 17566 " }\n" 17567 " }\n", 17568 WhitesmithsBraceStyle); 17569 17570 verifyFormat("void switchTest3(int a)\n" 17571 " {\n" 17572 " switch (a)\n" 17573 " {\n" 17574 " case 0:\n" 17575 " {\n" 17576 " foo(x);\n" 17577 " }\n" 17578 " break;\n" 17579 " default:\n" 17580 " {\n" 17581 " foo(1);\n" 17582 " }\n" 17583 " break;\n" 17584 " }\n" 17585 " }\n", 17586 WhitesmithsBraceStyle); 17587 17588 WhitesmithsBraceStyle.IndentCaseLabels = false; 17589 17590 verifyFormat("void switchTest4(int a)\n" 17591 " {\n" 17592 " switch (a)\n" 17593 " {\n" 17594 " case 2:\n" 17595 " {\n" 17596 " }\n" 17597 " break;\n" 17598 " }\n" 17599 " }\n", 17600 WhitesmithsBraceStyle); 17601 17602 verifyFormat("void switchTest5(int a)\n" 17603 " {\n" 17604 " switch (a)\n" 17605 " {\n" 17606 " case 0:\n" 17607 " break;\n" 17608 " case 1:\n" 17609 " {\n" 17610 " foo();\n" 17611 " break;\n" 17612 " }\n" 17613 " case 2:\n" 17614 " {\n" 17615 " }\n" 17616 " break;\n" 17617 " default:\n" 17618 " break;\n" 17619 " }\n" 17620 " }\n", 17621 WhitesmithsBraceStyle); 17622 17623 verifyFormat("void switchTest6(int a)\n" 17624 " {\n" 17625 " switch (a)\n" 17626 " {\n" 17627 " case 0:\n" 17628 " {\n" 17629 " foo(x);\n" 17630 " }\n" 17631 " break;\n" 17632 " default:\n" 17633 " {\n" 17634 " foo(1);\n" 17635 " }\n" 17636 " break;\n" 17637 " }\n" 17638 " }\n", 17639 WhitesmithsBraceStyle); 17640 17641 verifyFormat("enum X\n" 17642 " {\n" 17643 " Y = 0, // testing\n" 17644 " }\n", 17645 WhitesmithsBraceStyle); 17646 17647 verifyFormat("enum X\n" 17648 " {\n" 17649 " Y = 0\n" 17650 " }\n", 17651 WhitesmithsBraceStyle); 17652 verifyFormat("enum X\n" 17653 " {\n" 17654 " Y = 0,\n" 17655 " Z = 1\n" 17656 " };\n", 17657 WhitesmithsBraceStyle); 17658 17659 verifyFormat("@interface BSApplicationController ()\n" 17660 " {\n" 17661 "@private\n" 17662 " id _extraIvar;\n" 17663 " }\n" 17664 "@end\n", 17665 WhitesmithsBraceStyle); 17666 17667 verifyFormat("#ifdef _DEBUG\n" 17668 "int foo(int i = 0)\n" 17669 "#else\n" 17670 "int foo(int i = 5)\n" 17671 "#endif\n" 17672 " {\n" 17673 " return i;\n" 17674 " }", 17675 WhitesmithsBraceStyle); 17676 17677 verifyFormat("void foo() {}\n" 17678 "void bar()\n" 17679 "#ifdef _DEBUG\n" 17680 " {\n" 17681 " foo();\n" 17682 " }\n" 17683 "#else\n" 17684 " {\n" 17685 " }\n" 17686 "#endif", 17687 WhitesmithsBraceStyle); 17688 17689 verifyFormat("void foobar()\n" 17690 " {\n" 17691 " int i = 5;\n" 17692 " }\n" 17693 "#ifdef _DEBUG\n" 17694 "void bar()\n" 17695 " {\n" 17696 " }\n" 17697 "#else\n" 17698 "void bar()\n" 17699 " {\n" 17700 " foobar();\n" 17701 " }\n" 17702 "#endif", 17703 WhitesmithsBraceStyle); 17704 17705 // This shouldn't affect ObjC blocks.. 17706 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 17707 " // ...\n" 17708 " int i;\n" 17709 "}];", 17710 WhitesmithsBraceStyle); 17711 verifyFormat("void (^block)(void) = ^{\n" 17712 " // ...\n" 17713 " int i;\n" 17714 "};", 17715 WhitesmithsBraceStyle); 17716 // .. or dict literals. 17717 verifyFormat("void f()\n" 17718 " {\n" 17719 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 17720 " }", 17721 WhitesmithsBraceStyle); 17722 17723 verifyFormat("int f()\n" 17724 " { // comment\n" 17725 " return 42;\n" 17726 " }", 17727 WhitesmithsBraceStyle); 17728 17729 FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle; 17730 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = 17731 FormatStyle::SIS_OnlyFirstIf; 17732 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 17733 verifyFormat("void f(bool b)\n" 17734 " {\n" 17735 " if (b)\n" 17736 " {\n" 17737 " return;\n" 17738 " }\n" 17739 " }\n", 17740 BreakBeforeBraceShortIfs); 17741 verifyFormat("void f(bool b)\n" 17742 " {\n" 17743 " if (b) return;\n" 17744 " }\n", 17745 BreakBeforeBraceShortIfs); 17746 verifyFormat("void f(bool b)\n" 17747 " {\n" 17748 " while (b)\n" 17749 " {\n" 17750 " return;\n" 17751 " }\n" 17752 " }\n", 17753 BreakBeforeBraceShortIfs); 17754 } 17755 17756 TEST_F(FormatTest, GNUBraceBreaking) { 17757 FormatStyle GNUBraceStyle = getLLVMStyle(); 17758 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 17759 verifyFormat("namespace a\n" 17760 "{\n" 17761 "class A\n" 17762 "{\n" 17763 " void f()\n" 17764 " {\n" 17765 " int a;\n" 17766 " {\n" 17767 " int b;\n" 17768 " }\n" 17769 " if (true)\n" 17770 " {\n" 17771 " a();\n" 17772 " b();\n" 17773 " }\n" 17774 " }\n" 17775 " void g() { return; }\n" 17776 "}\n" 17777 "} // namespace a", 17778 GNUBraceStyle); 17779 17780 verifyFormat("void f()\n" 17781 "{\n" 17782 " if (true)\n" 17783 " {\n" 17784 " a();\n" 17785 " }\n" 17786 " else if (false)\n" 17787 " {\n" 17788 " b();\n" 17789 " }\n" 17790 " else\n" 17791 " {\n" 17792 " c();\n" 17793 " }\n" 17794 "}\n", 17795 GNUBraceStyle); 17796 17797 verifyFormat("void f()\n" 17798 "{\n" 17799 " for (int i = 0; i < 10; ++i)\n" 17800 " {\n" 17801 " a();\n" 17802 " }\n" 17803 " while (false)\n" 17804 " {\n" 17805 " b();\n" 17806 " }\n" 17807 " do\n" 17808 " {\n" 17809 " c();\n" 17810 " }\n" 17811 " while (false);\n" 17812 "}\n", 17813 GNUBraceStyle); 17814 17815 verifyFormat("void f(int a)\n" 17816 "{\n" 17817 " switch (a)\n" 17818 " {\n" 17819 " case 0:\n" 17820 " break;\n" 17821 " case 1:\n" 17822 " {\n" 17823 " break;\n" 17824 " }\n" 17825 " case 2:\n" 17826 " {\n" 17827 " }\n" 17828 " break;\n" 17829 " default:\n" 17830 " break;\n" 17831 " }\n" 17832 "}\n", 17833 GNUBraceStyle); 17834 17835 verifyFormat("enum X\n" 17836 "{\n" 17837 " Y = 0,\n" 17838 "}\n", 17839 GNUBraceStyle); 17840 17841 verifyFormat("@interface BSApplicationController ()\n" 17842 "{\n" 17843 "@private\n" 17844 " id _extraIvar;\n" 17845 "}\n" 17846 "@end\n", 17847 GNUBraceStyle); 17848 17849 verifyFormat("#ifdef _DEBUG\n" 17850 "int foo(int i = 0)\n" 17851 "#else\n" 17852 "int foo(int i = 5)\n" 17853 "#endif\n" 17854 "{\n" 17855 " return i;\n" 17856 "}", 17857 GNUBraceStyle); 17858 17859 verifyFormat("void foo() {}\n" 17860 "void bar()\n" 17861 "#ifdef _DEBUG\n" 17862 "{\n" 17863 " foo();\n" 17864 "}\n" 17865 "#else\n" 17866 "{\n" 17867 "}\n" 17868 "#endif", 17869 GNUBraceStyle); 17870 17871 verifyFormat("void foobar() { int i = 5; }\n" 17872 "#ifdef _DEBUG\n" 17873 "void bar() {}\n" 17874 "#else\n" 17875 "void bar() { foobar(); }\n" 17876 "#endif", 17877 GNUBraceStyle); 17878 } 17879 17880 TEST_F(FormatTest, WebKitBraceBreaking) { 17881 FormatStyle WebKitBraceStyle = getLLVMStyle(); 17882 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 17883 WebKitBraceStyle.FixNamespaceComments = false; 17884 verifyFormat("namespace a {\n" 17885 "class A {\n" 17886 " void f()\n" 17887 " {\n" 17888 " if (true) {\n" 17889 " a();\n" 17890 " b();\n" 17891 " }\n" 17892 " }\n" 17893 " void g() { return; }\n" 17894 "};\n" 17895 "enum E {\n" 17896 " A,\n" 17897 " // foo\n" 17898 " B,\n" 17899 " C\n" 17900 "};\n" 17901 "struct B {\n" 17902 " int x;\n" 17903 "};\n" 17904 "}\n", 17905 WebKitBraceStyle); 17906 verifyFormat("struct S {\n" 17907 " int Type;\n" 17908 " union {\n" 17909 " int x;\n" 17910 " double y;\n" 17911 " } Value;\n" 17912 " class C {\n" 17913 " MyFavoriteType Value;\n" 17914 " } Class;\n" 17915 "};\n", 17916 WebKitBraceStyle); 17917 } 17918 17919 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 17920 verifyFormat("void f() {\n" 17921 " try {\n" 17922 " } catch (const Exception &e) {\n" 17923 " }\n" 17924 "}\n", 17925 getLLVMStyle()); 17926 } 17927 17928 TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) { 17929 auto Style = getLLVMStyle(); 17930 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 17931 Style.AlignConsecutiveAssignments = 17932 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 17933 Style.AlignConsecutiveDeclarations = 17934 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 17935 verifyFormat("struct test demo[] = {\n" 17936 " {56, 23, \"hello\"},\n" 17937 " {-1, 93463, \"world\"},\n" 17938 " { 7, 5, \"!!\"}\n" 17939 "};\n", 17940 Style); 17941 17942 verifyFormat("struct test demo[] = {\n" 17943 " {56, 23, \"hello\"}, // first line\n" 17944 " {-1, 93463, \"world\"}, // second line\n" 17945 " { 7, 5, \"!!\"} // third line\n" 17946 "};\n", 17947 Style); 17948 17949 verifyFormat("struct test demo[4] = {\n" 17950 " { 56, 23, 21, \"oh\"}, // first line\n" 17951 " { -1, 93463, 22, \"my\"}, // second line\n" 17952 " { 7, 5, 1, \"goodness\"} // third line\n" 17953 " {234, 5, 1, \"gracious\"} // fourth line\n" 17954 "};\n", 17955 Style); 17956 17957 verifyFormat("struct test demo[3] = {\n" 17958 " {56, 23, \"hello\"},\n" 17959 " {-1, 93463, \"world\"},\n" 17960 " { 7, 5, \"!!\"}\n" 17961 "};\n", 17962 Style); 17963 17964 verifyFormat("struct test demo[3] = {\n" 17965 " {int{56}, 23, \"hello\"},\n" 17966 " {int{-1}, 93463, \"world\"},\n" 17967 " { int{7}, 5, \"!!\"}\n" 17968 "};\n", 17969 Style); 17970 17971 verifyFormat("struct test demo[] = {\n" 17972 " {56, 23, \"hello\"},\n" 17973 " {-1, 93463, \"world\"},\n" 17974 " { 7, 5, \"!!\"},\n" 17975 "};\n", 17976 Style); 17977 17978 verifyFormat("test demo[] = {\n" 17979 " {56, 23, \"hello\"},\n" 17980 " {-1, 93463, \"world\"},\n" 17981 " { 7, 5, \"!!\"},\n" 17982 "};\n", 17983 Style); 17984 17985 verifyFormat("demo = std::array<struct test, 3>{\n" 17986 " test{56, 23, \"hello\"},\n" 17987 " test{-1, 93463, \"world\"},\n" 17988 " test{ 7, 5, \"!!\"},\n" 17989 "};\n", 17990 Style); 17991 17992 verifyFormat("test demo[] = {\n" 17993 " {56, 23, \"hello\"},\n" 17994 "#if X\n" 17995 " {-1, 93463, \"world\"},\n" 17996 "#endif\n" 17997 " { 7, 5, \"!!\"}\n" 17998 "};\n", 17999 Style); 18000 18001 verifyFormat( 18002 "test demo[] = {\n" 18003 " { 7, 23,\n" 18004 " \"hello world i am a very long line that really, in any\"\n" 18005 " \"just world, ought to be split over multiple lines\"},\n" 18006 " {-1, 93463, \"world\"},\n" 18007 " {56, 5, \"!!\"}\n" 18008 "};\n", 18009 Style); 18010 18011 verifyFormat("return GradForUnaryCwise(g, {\n" 18012 " {{\"sign\"}, \"Sign\", " 18013 " {\"x\", \"dy\"}},\n" 18014 " { {\"dx\"}, \"Mul\", {\"dy\"" 18015 ", \"sign\"}},\n" 18016 "});\n", 18017 Style); 18018 18019 Style.ColumnLimit = 0; 18020 EXPECT_EQ( 18021 "test demo[] = {\n" 18022 " {56, 23, \"hello world i am a very long line that really, " 18023 "in any just world, ought to be split over multiple lines\"},\n" 18024 " {-1, 93463, " 18025 " \"world\"},\n" 18026 " { 7, 5, " 18027 " \"!!\"},\n" 18028 "};", 18029 format("test demo[] = {{56, 23, \"hello world i am a very long line " 18030 "that really, in any just world, ought to be split over multiple " 18031 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 18032 Style)); 18033 18034 Style.ColumnLimit = 80; 18035 verifyFormat("test demo[] = {\n" 18036 " {56, 23, /* a comment */ \"hello\"},\n" 18037 " {-1, 93463, \"world\"},\n" 18038 " { 7, 5, \"!!\"}\n" 18039 "};\n", 18040 Style); 18041 18042 verifyFormat("test demo[] = {\n" 18043 " {56, 23, \"hello\"},\n" 18044 " {-1, 93463, \"world\" /* comment here */},\n" 18045 " { 7, 5, \"!!\"}\n" 18046 "};\n", 18047 Style); 18048 18049 verifyFormat("test demo[] = {\n" 18050 " {56, /* a comment */ 23, \"hello\"},\n" 18051 " {-1, 93463, \"world\"},\n" 18052 " { 7, 5, \"!!\"}\n" 18053 "};\n", 18054 Style); 18055 18056 Style.ColumnLimit = 20; 18057 EXPECT_EQ( 18058 "demo = std::array<\n" 18059 " struct test, 3>{\n" 18060 " test{\n" 18061 " 56, 23,\n" 18062 " \"hello \"\n" 18063 " \"world i \"\n" 18064 " \"am a very \"\n" 18065 " \"long line \"\n" 18066 " \"that \"\n" 18067 " \"really, \"\n" 18068 " \"in any \"\n" 18069 " \"just \"\n" 18070 " \"world, \"\n" 18071 " \"ought to \"\n" 18072 " \"be split \"\n" 18073 " \"over \"\n" 18074 " \"multiple \"\n" 18075 " \"lines\"},\n" 18076 " test{-1, 93463,\n" 18077 " \"world\"},\n" 18078 " test{ 7, 5,\n" 18079 " \"!!\" },\n" 18080 "};", 18081 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world " 18082 "i am a very long line that really, in any just world, ought " 18083 "to be split over multiple lines\"},test{-1, 93463, \"world\"}," 18084 "test{7, 5, \"!!\"},};", 18085 Style)); 18086 // This caused a core dump by enabling Alignment in the LLVMStyle globally 18087 Style = getLLVMStyleWithColumns(50); 18088 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 18089 verifyFormat("static A x = {\n" 18090 " {{init1, init2, init3, init4},\n" 18091 " {init1, init2, init3, init4}}\n" 18092 "};", 18093 Style); 18094 Style.ColumnLimit = 100; 18095 EXPECT_EQ( 18096 "test demo[] = {\n" 18097 " {56, 23,\n" 18098 " \"hello world i am a very long line that really, in any just world" 18099 ", ought to be split over \"\n" 18100 " \"multiple lines\" },\n" 18101 " {-1, 93463, \"world\"},\n" 18102 " { 7, 5, \"!!\"},\n" 18103 "};", 18104 format("test demo[] = {{56, 23, \"hello world i am a very long line " 18105 "that really, in any just world, ought to be split over multiple " 18106 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 18107 Style)); 18108 18109 Style = getLLVMStyleWithColumns(50); 18110 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 18111 Style.AlignConsecutiveAssignments = 18112 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 18113 Style.AlignConsecutiveDeclarations = 18114 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 18115 verifyFormat("struct test demo[] = {\n" 18116 " {56, 23, \"hello\"},\n" 18117 " {-1, 93463, \"world\"},\n" 18118 " { 7, 5, \"!!\"}\n" 18119 "};\n" 18120 "static A x = {\n" 18121 " {{init1, init2, init3, init4},\n" 18122 " {init1, init2, init3, init4}}\n" 18123 "};", 18124 Style); 18125 Style.ColumnLimit = 100; 18126 Style.AlignConsecutiveAssignments = 18127 FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments; 18128 Style.AlignConsecutiveDeclarations = 18129 FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments; 18130 verifyFormat("struct test demo[] = {\n" 18131 " {56, 23, \"hello\"},\n" 18132 " {-1, 93463, \"world\"},\n" 18133 " { 7, 5, \"!!\"}\n" 18134 "};\n" 18135 "struct test demo[4] = {\n" 18136 " { 56, 23, 21, \"oh\"}, // first line\n" 18137 " { -1, 93463, 22, \"my\"}, // second line\n" 18138 " { 7, 5, 1, \"goodness\"} // third line\n" 18139 " {234, 5, 1, \"gracious\"} // fourth line\n" 18140 "};\n", 18141 Style); 18142 EXPECT_EQ( 18143 "test demo[] = {\n" 18144 " {56,\n" 18145 " \"hello world i am a very long line that really, in any just world" 18146 ", ought to be split over \"\n" 18147 " \"multiple lines\", 23},\n" 18148 " {-1, \"world\", 93463},\n" 18149 " { 7, \"!!\", 5},\n" 18150 "};", 18151 format("test demo[] = {{56, \"hello world i am a very long line " 18152 "that really, in any just world, ought to be split over multiple " 18153 "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};", 18154 Style)); 18155 } 18156 18157 TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) { 18158 auto Style = getLLVMStyle(); 18159 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; 18160 /* FIXME: This case gets misformatted. 18161 verifyFormat("auto foo = Items{\n" 18162 " Section{0, bar(), },\n" 18163 " Section{1, boo() }\n" 18164 "};\n", 18165 Style); 18166 */ 18167 verifyFormat("auto foo = Items{\n" 18168 " Section{\n" 18169 " 0, bar(),\n" 18170 " }\n" 18171 "};\n", 18172 Style); 18173 verifyFormat("struct test demo[] = {\n" 18174 " {56, 23, \"hello\"},\n" 18175 " {-1, 93463, \"world\"},\n" 18176 " {7, 5, \"!!\" }\n" 18177 "};\n", 18178 Style); 18179 verifyFormat("struct test demo[] = {\n" 18180 " {56, 23, \"hello\"}, // first line\n" 18181 " {-1, 93463, \"world\"}, // second line\n" 18182 " {7, 5, \"!!\" } // third line\n" 18183 "};\n", 18184 Style); 18185 verifyFormat("struct test demo[4] = {\n" 18186 " {56, 23, 21, \"oh\" }, // first line\n" 18187 " {-1, 93463, 22, \"my\" }, // second line\n" 18188 " {7, 5, 1, \"goodness\"} // third line\n" 18189 " {234, 5, 1, \"gracious\"} // fourth line\n" 18190 "};\n", 18191 Style); 18192 verifyFormat("struct test demo[3] = {\n" 18193 " {56, 23, \"hello\"},\n" 18194 " {-1, 93463, \"world\"},\n" 18195 " {7, 5, \"!!\" }\n" 18196 "};\n", 18197 Style); 18198 18199 verifyFormat("struct test demo[3] = {\n" 18200 " {int{56}, 23, \"hello\"},\n" 18201 " {int{-1}, 93463, \"world\"},\n" 18202 " {int{7}, 5, \"!!\" }\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("test demo[] = {\n" 18212 " {56, 23, \"hello\"},\n" 18213 " {-1, 93463, \"world\"},\n" 18214 " {7, 5, \"!!\" },\n" 18215 "};\n", 18216 Style); 18217 verifyFormat("demo = std::array<struct test, 3>{\n" 18218 " test{56, 23, \"hello\"},\n" 18219 " test{-1, 93463, \"world\"},\n" 18220 " test{7, 5, \"!!\" },\n" 18221 "};\n", 18222 Style); 18223 verifyFormat("test demo[] = {\n" 18224 " {56, 23, \"hello\"},\n" 18225 "#if X\n" 18226 " {-1, 93463, \"world\"},\n" 18227 "#endif\n" 18228 " {7, 5, \"!!\" }\n" 18229 "};\n", 18230 Style); 18231 verifyFormat( 18232 "test demo[] = {\n" 18233 " {7, 23,\n" 18234 " \"hello world i am a very long line that really, in any\"\n" 18235 " \"just world, ought to be split over multiple lines\"},\n" 18236 " {-1, 93463, \"world\" },\n" 18237 " {56, 5, \"!!\" }\n" 18238 "};\n", 18239 Style); 18240 18241 verifyFormat("return GradForUnaryCwise(g, {\n" 18242 " {{\"sign\"}, \"Sign\", {\"x\", " 18243 "\"dy\"} },\n" 18244 " {{\"dx\"}, \"Mul\", " 18245 "{\"dy\", \"sign\"}},\n" 18246 "});\n", 18247 Style); 18248 18249 Style.ColumnLimit = 0; 18250 EXPECT_EQ( 18251 "test demo[] = {\n" 18252 " {56, 23, \"hello world i am a very long line that really, in any " 18253 "just world, ought to be split over multiple lines\"},\n" 18254 " {-1, 93463, \"world\" " 18255 " },\n" 18256 " {7, 5, \"!!\" " 18257 " },\n" 18258 "};", 18259 format("test demo[] = {{56, 23, \"hello world i am a very long line " 18260 "that really, in any just world, ought to be split over multiple " 18261 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 18262 Style)); 18263 18264 Style.ColumnLimit = 80; 18265 verifyFormat("test demo[] = {\n" 18266 " {56, 23, /* a comment */ \"hello\"},\n" 18267 " {-1, 93463, \"world\" },\n" 18268 " {7, 5, \"!!\" }\n" 18269 "};\n", 18270 Style); 18271 18272 verifyFormat("test demo[] = {\n" 18273 " {56, 23, \"hello\" },\n" 18274 " {-1, 93463, \"world\" /* comment here */},\n" 18275 " {7, 5, \"!!\" }\n" 18276 "};\n", 18277 Style); 18278 18279 verifyFormat("test demo[] = {\n" 18280 " {56, /* a comment */ 23, \"hello\"},\n" 18281 " {-1, 93463, \"world\"},\n" 18282 " {7, 5, \"!!\" }\n" 18283 "};\n", 18284 Style); 18285 18286 Style.ColumnLimit = 20; 18287 EXPECT_EQ( 18288 "demo = std::array<\n" 18289 " struct test, 3>{\n" 18290 " test{\n" 18291 " 56, 23,\n" 18292 " \"hello \"\n" 18293 " \"world i \"\n" 18294 " \"am a very \"\n" 18295 " \"long line \"\n" 18296 " \"that \"\n" 18297 " \"really, \"\n" 18298 " \"in any \"\n" 18299 " \"just \"\n" 18300 " \"world, \"\n" 18301 " \"ought to \"\n" 18302 " \"be split \"\n" 18303 " \"over \"\n" 18304 " \"multiple \"\n" 18305 " \"lines\"},\n" 18306 " test{-1, 93463,\n" 18307 " \"world\"},\n" 18308 " test{7, 5,\n" 18309 " \"!!\" },\n" 18310 "};", 18311 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world " 18312 "i am a very long line that really, in any just world, ought " 18313 "to be split over multiple lines\"},test{-1, 93463, \"world\"}," 18314 "test{7, 5, \"!!\"},};", 18315 Style)); 18316 18317 // This caused a core dump by enabling Alignment in the LLVMStyle globally 18318 Style = getLLVMStyleWithColumns(50); 18319 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; 18320 verifyFormat("static A x = {\n" 18321 " {{init1, init2, init3, init4},\n" 18322 " {init1, init2, init3, init4}}\n" 18323 "};", 18324 Style); 18325 Style.ColumnLimit = 100; 18326 EXPECT_EQ( 18327 "test demo[] = {\n" 18328 " {56, 23,\n" 18329 " \"hello world i am a very long line that really, in any just world" 18330 ", ought to be split over \"\n" 18331 " \"multiple lines\" },\n" 18332 " {-1, 93463, \"world\"},\n" 18333 " {7, 5, \"!!\" },\n" 18334 "};", 18335 format("test demo[] = {{56, 23, \"hello world i am a very long line " 18336 "that really, in any just world, ought to be split over multiple " 18337 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 18338 Style)); 18339 } 18340 18341 TEST_F(FormatTest, UnderstandsPragmas) { 18342 verifyFormat("#pragma omp reduction(| : var)"); 18343 verifyFormat("#pragma omp reduction(+ : var)"); 18344 18345 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 18346 "(including parentheses).", 18347 format("#pragma mark Any non-hyphenated or hyphenated string " 18348 "(including parentheses).")); 18349 } 18350 18351 TEST_F(FormatTest, UnderstandPragmaOption) { 18352 verifyFormat("#pragma option -C -A"); 18353 18354 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 18355 } 18356 18357 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) { 18358 FormatStyle Style = getLLVMStyle(); 18359 Style.ColumnLimit = 20; 18360 18361 // See PR41213 18362 EXPECT_EQ("/*\n" 18363 " *\t9012345\n" 18364 " * /8901\n" 18365 " */", 18366 format("/*\n" 18367 " *\t9012345 /8901\n" 18368 " */", 18369 Style)); 18370 EXPECT_EQ("/*\n" 18371 " *345678\n" 18372 " *\t/8901\n" 18373 " */", 18374 format("/*\n" 18375 " *345678\t/8901\n" 18376 " */", 18377 Style)); 18378 18379 verifyFormat("int a; // the\n" 18380 " // comment", 18381 Style); 18382 EXPECT_EQ("int a; /* first line\n" 18383 " * second\n" 18384 " * line third\n" 18385 " * line\n" 18386 " */", 18387 format("int a; /* first line\n" 18388 " * second\n" 18389 " * line third\n" 18390 " * line\n" 18391 " */", 18392 Style)); 18393 EXPECT_EQ("int a; // first line\n" 18394 " // second\n" 18395 " // line third\n" 18396 " // line", 18397 format("int a; // first line\n" 18398 " // second line\n" 18399 " // third line", 18400 Style)); 18401 18402 Style.PenaltyExcessCharacter = 90; 18403 verifyFormat("int a; // the comment", Style); 18404 EXPECT_EQ("int a; // the comment\n" 18405 " // aaa", 18406 format("int a; // the comment aaa", Style)); 18407 EXPECT_EQ("int a; /* first line\n" 18408 " * second line\n" 18409 " * third line\n" 18410 " */", 18411 format("int a; /* first line\n" 18412 " * second line\n" 18413 " * third line\n" 18414 " */", 18415 Style)); 18416 EXPECT_EQ("int a; // first line\n" 18417 " // second line\n" 18418 " // third line", 18419 format("int a; // first line\n" 18420 " // second line\n" 18421 " // third line", 18422 Style)); 18423 // FIXME: Investigate why this is not getting the same layout as the test 18424 // above. 18425 EXPECT_EQ("int a; /* first line\n" 18426 " * second line\n" 18427 " * third line\n" 18428 " */", 18429 format("int a; /* first line second line third line" 18430 "\n*/", 18431 Style)); 18432 18433 EXPECT_EQ("// foo bar baz bazfoo\n" 18434 "// foo bar foo bar\n", 18435 format("// foo bar baz bazfoo\n" 18436 "// foo bar foo bar\n", 18437 Style)); 18438 EXPECT_EQ("// foo bar baz bazfoo\n" 18439 "// foo bar foo bar\n", 18440 format("// foo bar baz bazfoo\n" 18441 "// foo bar foo bar\n", 18442 Style)); 18443 18444 // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the 18445 // next one. 18446 EXPECT_EQ("// foo bar baz bazfoo\n" 18447 "// bar foo bar\n", 18448 format("// foo bar baz bazfoo bar\n" 18449 "// foo bar\n", 18450 Style)); 18451 18452 EXPECT_EQ("// foo bar baz bazfoo\n" 18453 "// foo bar baz bazfoo\n" 18454 "// bar foo bar\n", 18455 format("// foo bar baz bazfoo\n" 18456 "// foo bar baz bazfoo bar\n" 18457 "// foo bar\n", 18458 Style)); 18459 18460 EXPECT_EQ("// foo bar baz bazfoo\n" 18461 "// foo bar baz bazfoo\n" 18462 "// bar foo bar\n", 18463 format("// foo bar baz bazfoo\n" 18464 "// foo bar baz bazfoo bar\n" 18465 "// foo bar\n", 18466 Style)); 18467 18468 // Make sure we do not keep protruding characters if strict mode reflow is 18469 // cheaper than keeping protruding characters. 18470 Style.ColumnLimit = 21; 18471 EXPECT_EQ( 18472 "// foo foo foo foo\n" 18473 "// foo foo foo foo\n" 18474 "// foo foo foo foo\n", 18475 format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", Style)); 18476 18477 EXPECT_EQ("int a = /* long block\n" 18478 " comment */\n" 18479 " 42;", 18480 format("int a = /* long block comment */ 42;", Style)); 18481 } 18482 18483 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 18484 for (size_t i = 1; i < Styles.size(); ++i) \ 18485 EXPECT_EQ(Styles[0], Styles[i]) \ 18486 << "Style #" << i << " of " << Styles.size() << " differs from Style #0" 18487 18488 TEST_F(FormatTest, GetsPredefinedStyleByName) { 18489 SmallVector<FormatStyle, 3> Styles; 18490 Styles.resize(3); 18491 18492 Styles[0] = getLLVMStyle(); 18493 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 18494 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 18495 EXPECT_ALL_STYLES_EQUAL(Styles); 18496 18497 Styles[0] = getGoogleStyle(); 18498 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 18499 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 18500 EXPECT_ALL_STYLES_EQUAL(Styles); 18501 18502 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 18503 EXPECT_TRUE( 18504 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 18505 EXPECT_TRUE( 18506 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 18507 EXPECT_ALL_STYLES_EQUAL(Styles); 18508 18509 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 18510 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 18511 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 18512 EXPECT_ALL_STYLES_EQUAL(Styles); 18513 18514 Styles[0] = getMozillaStyle(); 18515 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 18516 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 18517 EXPECT_ALL_STYLES_EQUAL(Styles); 18518 18519 Styles[0] = getWebKitStyle(); 18520 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 18521 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 18522 EXPECT_ALL_STYLES_EQUAL(Styles); 18523 18524 Styles[0] = getGNUStyle(); 18525 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 18526 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 18527 EXPECT_ALL_STYLES_EQUAL(Styles); 18528 18529 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 18530 } 18531 18532 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 18533 SmallVector<FormatStyle, 8> Styles; 18534 Styles.resize(2); 18535 18536 Styles[0] = getGoogleStyle(); 18537 Styles[1] = getLLVMStyle(); 18538 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 18539 EXPECT_ALL_STYLES_EQUAL(Styles); 18540 18541 Styles.resize(5); 18542 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 18543 Styles[1] = getLLVMStyle(); 18544 Styles[1].Language = FormatStyle::LK_JavaScript; 18545 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 18546 18547 Styles[2] = getLLVMStyle(); 18548 Styles[2].Language = FormatStyle::LK_JavaScript; 18549 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 18550 "BasedOnStyle: Google", 18551 &Styles[2]) 18552 .value()); 18553 18554 Styles[3] = getLLVMStyle(); 18555 Styles[3].Language = FormatStyle::LK_JavaScript; 18556 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 18557 "Language: JavaScript", 18558 &Styles[3]) 18559 .value()); 18560 18561 Styles[4] = getLLVMStyle(); 18562 Styles[4].Language = FormatStyle::LK_JavaScript; 18563 EXPECT_EQ(0, parseConfiguration("---\n" 18564 "BasedOnStyle: LLVM\n" 18565 "IndentWidth: 123\n" 18566 "---\n" 18567 "BasedOnStyle: Google\n" 18568 "Language: JavaScript", 18569 &Styles[4]) 18570 .value()); 18571 EXPECT_ALL_STYLES_EQUAL(Styles); 18572 } 18573 18574 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 18575 Style.FIELD = false; \ 18576 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 18577 EXPECT_TRUE(Style.FIELD); \ 18578 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 18579 EXPECT_FALSE(Style.FIELD); 18580 18581 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 18582 18583 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 18584 Style.STRUCT.FIELD = false; \ 18585 EXPECT_EQ(0, \ 18586 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 18587 .value()); \ 18588 EXPECT_TRUE(Style.STRUCT.FIELD); \ 18589 EXPECT_EQ(0, \ 18590 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 18591 .value()); \ 18592 EXPECT_FALSE(Style.STRUCT.FIELD); 18593 18594 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 18595 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 18596 18597 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 18598 EXPECT_NE(VALUE, Style.FIELD) << "Initial value already the same!"; \ 18599 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 18600 EXPECT_EQ(VALUE, Style.FIELD) << "Unexpected value after parsing!" 18601 18602 TEST_F(FormatTest, ParsesConfigurationBools) { 18603 FormatStyle Style = {}; 18604 Style.Language = FormatStyle::LK_Cpp; 18605 CHECK_PARSE_BOOL(AlignTrailingComments); 18606 CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine); 18607 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 18608 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 18609 CHECK_PARSE_BOOL(AllowShortEnumsOnASingleLine); 18610 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 18611 CHECK_PARSE_BOOL(BinPackArguments); 18612 CHECK_PARSE_BOOL(BinPackParameters); 18613 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 18614 CHECK_PARSE_BOOL(BreakBeforeConceptDeclarations); 18615 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 18616 CHECK_PARSE_BOOL(BreakStringLiterals); 18617 CHECK_PARSE_BOOL(CompactNamespaces); 18618 CHECK_PARSE_BOOL(DeriveLineEnding); 18619 CHECK_PARSE_BOOL(DerivePointerAlignment); 18620 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 18621 CHECK_PARSE_BOOL(DisableFormat); 18622 CHECK_PARSE_BOOL(IndentAccessModifiers); 18623 CHECK_PARSE_BOOL(IndentCaseLabels); 18624 CHECK_PARSE_BOOL(IndentCaseBlocks); 18625 CHECK_PARSE_BOOL(IndentGotoLabels); 18626 CHECK_PARSE_BOOL(IndentRequires); 18627 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 18628 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 18629 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 18630 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 18631 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 18632 CHECK_PARSE_BOOL(ReflowComments); 18633 CHECK_PARSE_BOOL(SortUsingDeclarations); 18634 CHECK_PARSE_BOOL(SpacesInParentheses); 18635 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 18636 CHECK_PARSE_BOOL(SpacesInConditionalStatement); 18637 CHECK_PARSE_BOOL(SpaceInEmptyBlock); 18638 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 18639 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 18640 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 18641 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 18642 CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); 18643 CHECK_PARSE_BOOL(SpaceAfterLogicalNot); 18644 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 18645 CHECK_PARSE_BOOL(SpaceBeforeCaseColon); 18646 CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList); 18647 CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon); 18648 CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon); 18649 CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon); 18650 CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets); 18651 CHECK_PARSE_BOOL(UseCRLF); 18652 18653 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel); 18654 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 18655 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 18656 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 18657 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 18658 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 18659 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 18660 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 18661 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock); 18662 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 18663 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 18664 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeLambdaBody); 18665 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeWhile); 18666 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 18667 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction); 18668 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord); 18669 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace); 18670 } 18671 18672 #undef CHECK_PARSE_BOOL 18673 18674 TEST_F(FormatTest, ParsesConfiguration) { 18675 FormatStyle Style = {}; 18676 Style.Language = FormatStyle::LK_Cpp; 18677 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 18678 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 18679 ConstructorInitializerIndentWidth, 1234u); 18680 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 18681 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 18682 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 18683 CHECK_PARSE("PenaltyBreakAssignment: 1234", PenaltyBreakAssignment, 1234u); 18684 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 18685 PenaltyBreakBeforeFirstCallParameter, 1234u); 18686 CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234", 18687 PenaltyBreakTemplateDeclaration, 1234u); 18688 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 18689 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 18690 PenaltyReturnTypeOnItsOwnLine, 1234u); 18691 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 18692 SpacesBeforeTrailingComments, 1234u); 18693 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 18694 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 18695 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$"); 18696 18697 Style.QualifierAlignment = FormatStyle::QAS_Right; 18698 CHECK_PARSE("QualifierAlignment: Leave", QualifierAlignment, 18699 FormatStyle::QAS_Leave); 18700 CHECK_PARSE("QualifierAlignment: Right", QualifierAlignment, 18701 FormatStyle::QAS_Right); 18702 CHECK_PARSE("QualifierAlignment: Left", QualifierAlignment, 18703 FormatStyle::QAS_Left); 18704 CHECK_PARSE("QualifierAlignment: Custom", QualifierAlignment, 18705 FormatStyle::QAS_Custom); 18706 18707 Style.QualifierOrder.clear(); 18708 CHECK_PARSE("QualifierOrder: [ const, volatile, type ]", QualifierOrder, 18709 std::vector<std::string>({"const", "volatile", "type"})); 18710 Style.QualifierOrder.clear(); 18711 CHECK_PARSE("QualifierOrder: [const, type]", QualifierOrder, 18712 std::vector<std::string>({"const", "type"})); 18713 Style.QualifierOrder.clear(); 18714 CHECK_PARSE("QualifierOrder: [volatile, type]", QualifierOrder, 18715 std::vector<std::string>({"volatile", "type"})); 18716 18717 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 18718 CHECK_PARSE("AlignConsecutiveAssignments: None", AlignConsecutiveAssignments, 18719 FormatStyle::ACS_None); 18720 CHECK_PARSE("AlignConsecutiveAssignments: Consecutive", 18721 AlignConsecutiveAssignments, FormatStyle::ACS_Consecutive); 18722 CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLines", 18723 AlignConsecutiveAssignments, FormatStyle::ACS_AcrossEmptyLines); 18724 CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLinesAndComments", 18725 AlignConsecutiveAssignments, 18726 FormatStyle::ACS_AcrossEmptyLinesAndComments); 18727 // For backwards compability, false / true should still parse 18728 CHECK_PARSE("AlignConsecutiveAssignments: false", AlignConsecutiveAssignments, 18729 FormatStyle::ACS_None); 18730 CHECK_PARSE("AlignConsecutiveAssignments: true", AlignConsecutiveAssignments, 18731 FormatStyle::ACS_Consecutive); 18732 18733 Style.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive; 18734 CHECK_PARSE("AlignConsecutiveBitFields: None", AlignConsecutiveBitFields, 18735 FormatStyle::ACS_None); 18736 CHECK_PARSE("AlignConsecutiveBitFields: Consecutive", 18737 AlignConsecutiveBitFields, FormatStyle::ACS_Consecutive); 18738 CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLines", 18739 AlignConsecutiveBitFields, FormatStyle::ACS_AcrossEmptyLines); 18740 CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLinesAndComments", 18741 AlignConsecutiveBitFields, 18742 FormatStyle::ACS_AcrossEmptyLinesAndComments); 18743 // For backwards compability, false / true should still parse 18744 CHECK_PARSE("AlignConsecutiveBitFields: false", AlignConsecutiveBitFields, 18745 FormatStyle::ACS_None); 18746 CHECK_PARSE("AlignConsecutiveBitFields: true", AlignConsecutiveBitFields, 18747 FormatStyle::ACS_Consecutive); 18748 18749 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 18750 CHECK_PARSE("AlignConsecutiveMacros: None", AlignConsecutiveMacros, 18751 FormatStyle::ACS_None); 18752 CHECK_PARSE("AlignConsecutiveMacros: Consecutive", AlignConsecutiveMacros, 18753 FormatStyle::ACS_Consecutive); 18754 CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLines", 18755 AlignConsecutiveMacros, FormatStyle::ACS_AcrossEmptyLines); 18756 CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLinesAndComments", 18757 AlignConsecutiveMacros, 18758 FormatStyle::ACS_AcrossEmptyLinesAndComments); 18759 // For backwards compability, false / true should still parse 18760 CHECK_PARSE("AlignConsecutiveMacros: false", AlignConsecutiveMacros, 18761 FormatStyle::ACS_None); 18762 CHECK_PARSE("AlignConsecutiveMacros: true", AlignConsecutiveMacros, 18763 FormatStyle::ACS_Consecutive); 18764 18765 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 18766 CHECK_PARSE("AlignConsecutiveDeclarations: None", 18767 AlignConsecutiveDeclarations, FormatStyle::ACS_None); 18768 CHECK_PARSE("AlignConsecutiveDeclarations: Consecutive", 18769 AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive); 18770 CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLines", 18771 AlignConsecutiveDeclarations, FormatStyle::ACS_AcrossEmptyLines); 18772 CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLinesAndComments", 18773 AlignConsecutiveDeclarations, 18774 FormatStyle::ACS_AcrossEmptyLinesAndComments); 18775 // For backwards compability, false / true should still parse 18776 CHECK_PARSE("AlignConsecutiveDeclarations: false", 18777 AlignConsecutiveDeclarations, FormatStyle::ACS_None); 18778 CHECK_PARSE("AlignConsecutiveDeclarations: true", 18779 AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive); 18780 18781 Style.PointerAlignment = FormatStyle::PAS_Middle; 18782 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 18783 FormatStyle::PAS_Left); 18784 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 18785 FormatStyle::PAS_Right); 18786 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 18787 FormatStyle::PAS_Middle); 18788 Style.ReferenceAlignment = FormatStyle::RAS_Middle; 18789 CHECK_PARSE("ReferenceAlignment: Pointer", ReferenceAlignment, 18790 FormatStyle::RAS_Pointer); 18791 CHECK_PARSE("ReferenceAlignment: Left", ReferenceAlignment, 18792 FormatStyle::RAS_Left); 18793 CHECK_PARSE("ReferenceAlignment: Right", ReferenceAlignment, 18794 FormatStyle::RAS_Right); 18795 CHECK_PARSE("ReferenceAlignment: Middle", ReferenceAlignment, 18796 FormatStyle::RAS_Middle); 18797 // For backward compatibility: 18798 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 18799 FormatStyle::PAS_Left); 18800 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 18801 FormatStyle::PAS_Right); 18802 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 18803 FormatStyle::PAS_Middle); 18804 18805 Style.Standard = FormatStyle::LS_Auto; 18806 CHECK_PARSE("Standard: c++03", Standard, FormatStyle::LS_Cpp03); 18807 CHECK_PARSE("Standard: c++11", Standard, FormatStyle::LS_Cpp11); 18808 CHECK_PARSE("Standard: c++14", Standard, FormatStyle::LS_Cpp14); 18809 CHECK_PARSE("Standard: c++17", Standard, FormatStyle::LS_Cpp17); 18810 CHECK_PARSE("Standard: c++20", Standard, FormatStyle::LS_Cpp20); 18811 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 18812 CHECK_PARSE("Standard: Latest", Standard, FormatStyle::LS_Latest); 18813 // Legacy aliases: 18814 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 18815 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Latest); 18816 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 18817 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 18818 18819 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 18820 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 18821 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 18822 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 18823 FormatStyle::BOS_None); 18824 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 18825 FormatStyle::BOS_All); 18826 // For backward compatibility: 18827 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 18828 FormatStyle::BOS_None); 18829 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 18830 FormatStyle::BOS_All); 18831 18832 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 18833 CHECK_PARSE("BreakConstructorInitializers: BeforeComma", 18834 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 18835 CHECK_PARSE("BreakConstructorInitializers: AfterColon", 18836 BreakConstructorInitializers, FormatStyle::BCIS_AfterColon); 18837 CHECK_PARSE("BreakConstructorInitializers: BeforeColon", 18838 BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon); 18839 // For backward compatibility: 18840 CHECK_PARSE("BreakConstructorInitializersBeforeComma: true", 18841 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 18842 18843 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 18844 CHECK_PARSE("BreakInheritanceList: AfterComma", BreakInheritanceList, 18845 FormatStyle::BILS_AfterComma); 18846 CHECK_PARSE("BreakInheritanceList: BeforeComma", BreakInheritanceList, 18847 FormatStyle::BILS_BeforeComma); 18848 CHECK_PARSE("BreakInheritanceList: AfterColon", BreakInheritanceList, 18849 FormatStyle::BILS_AfterColon); 18850 CHECK_PARSE("BreakInheritanceList: BeforeColon", BreakInheritanceList, 18851 FormatStyle::BILS_BeforeColon); 18852 // For backward compatibility: 18853 CHECK_PARSE("BreakBeforeInheritanceComma: true", BreakInheritanceList, 18854 FormatStyle::BILS_BeforeComma); 18855 18856 Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack; 18857 CHECK_PARSE("PackConstructorInitializers: Never", PackConstructorInitializers, 18858 FormatStyle::PCIS_Never); 18859 CHECK_PARSE("PackConstructorInitializers: BinPack", 18860 PackConstructorInitializers, FormatStyle::PCIS_BinPack); 18861 CHECK_PARSE("PackConstructorInitializers: CurrentLine", 18862 PackConstructorInitializers, FormatStyle::PCIS_CurrentLine); 18863 CHECK_PARSE("PackConstructorInitializers: NextLine", 18864 PackConstructorInitializers, FormatStyle::PCIS_NextLine); 18865 // For backward compatibility: 18866 CHECK_PARSE("BasedOnStyle: Google\n" 18867 "ConstructorInitializerAllOnOneLineOrOnePerLine: true\n" 18868 "AllowAllConstructorInitializersOnNextLine: false", 18869 PackConstructorInitializers, FormatStyle::PCIS_CurrentLine); 18870 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 18871 CHECK_PARSE("BasedOnStyle: Google\n" 18872 "ConstructorInitializerAllOnOneLineOrOnePerLine: false", 18873 PackConstructorInitializers, FormatStyle::PCIS_BinPack); 18874 CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n" 18875 "AllowAllConstructorInitializersOnNextLine: true", 18876 PackConstructorInitializers, FormatStyle::PCIS_NextLine); 18877 Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack; 18878 CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n" 18879 "AllowAllConstructorInitializersOnNextLine: false", 18880 PackConstructorInitializers, FormatStyle::PCIS_CurrentLine); 18881 18882 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 18883 CHECK_PARSE("EmptyLineBeforeAccessModifier: Never", 18884 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Never); 18885 CHECK_PARSE("EmptyLineBeforeAccessModifier: Leave", 18886 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Leave); 18887 CHECK_PARSE("EmptyLineBeforeAccessModifier: LogicalBlock", 18888 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_LogicalBlock); 18889 CHECK_PARSE("EmptyLineBeforeAccessModifier: Always", 18890 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Always); 18891 18892 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 18893 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 18894 FormatStyle::BAS_Align); 18895 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 18896 FormatStyle::BAS_DontAlign); 18897 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 18898 FormatStyle::BAS_AlwaysBreak); 18899 // For backward compatibility: 18900 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 18901 FormatStyle::BAS_DontAlign); 18902 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 18903 FormatStyle::BAS_Align); 18904 18905 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left; 18906 CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines, 18907 FormatStyle::ENAS_DontAlign); 18908 CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines, 18909 FormatStyle::ENAS_Left); 18910 CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines, 18911 FormatStyle::ENAS_Right); 18912 // For backward compatibility: 18913 CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines, 18914 FormatStyle::ENAS_Left); 18915 CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines, 18916 FormatStyle::ENAS_Right); 18917 18918 Style.AlignOperands = FormatStyle::OAS_Align; 18919 CHECK_PARSE("AlignOperands: DontAlign", AlignOperands, 18920 FormatStyle::OAS_DontAlign); 18921 CHECK_PARSE("AlignOperands: Align", AlignOperands, FormatStyle::OAS_Align); 18922 CHECK_PARSE("AlignOperands: AlignAfterOperator", AlignOperands, 18923 FormatStyle::OAS_AlignAfterOperator); 18924 // For backward compatibility: 18925 CHECK_PARSE("AlignOperands: false", AlignOperands, 18926 FormatStyle::OAS_DontAlign); 18927 CHECK_PARSE("AlignOperands: true", AlignOperands, FormatStyle::OAS_Align); 18928 18929 Style.UseTab = FormatStyle::UT_ForIndentation; 18930 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 18931 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 18932 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 18933 CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab, 18934 FormatStyle::UT_ForContinuationAndIndentation); 18935 CHECK_PARSE("UseTab: AlignWithSpaces", UseTab, 18936 FormatStyle::UT_AlignWithSpaces); 18937 // For backward compatibility: 18938 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 18939 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 18940 18941 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 18942 CHECK_PARSE("AllowShortBlocksOnASingleLine: Never", 18943 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 18944 CHECK_PARSE("AllowShortBlocksOnASingleLine: Empty", 18945 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Empty); 18946 CHECK_PARSE("AllowShortBlocksOnASingleLine: Always", 18947 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always); 18948 // For backward compatibility: 18949 CHECK_PARSE("AllowShortBlocksOnASingleLine: false", 18950 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 18951 CHECK_PARSE("AllowShortBlocksOnASingleLine: true", 18952 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always); 18953 18954 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 18955 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 18956 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 18957 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 18958 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 18959 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 18960 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 18961 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 18962 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 18963 // For backward compatibility: 18964 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 18965 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 18966 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 18967 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 18968 18969 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Both; 18970 CHECK_PARSE("SpaceAroundPointerQualifiers: Default", 18971 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Default); 18972 CHECK_PARSE("SpaceAroundPointerQualifiers: Before", 18973 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Before); 18974 CHECK_PARSE("SpaceAroundPointerQualifiers: After", 18975 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_After); 18976 CHECK_PARSE("SpaceAroundPointerQualifiers: Both", 18977 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Both); 18978 18979 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 18980 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 18981 FormatStyle::SBPO_Never); 18982 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 18983 FormatStyle::SBPO_Always); 18984 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 18985 FormatStyle::SBPO_ControlStatements); 18986 CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptControlMacros", 18987 SpaceBeforeParens, 18988 FormatStyle::SBPO_ControlStatementsExceptControlMacros); 18989 CHECK_PARSE("SpaceBeforeParens: NonEmptyParentheses", SpaceBeforeParens, 18990 FormatStyle::SBPO_NonEmptyParentheses); 18991 CHECK_PARSE("SpaceBeforeParens: Custom", SpaceBeforeParens, 18992 FormatStyle::SBPO_Custom); 18993 // For backward compatibility: 18994 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 18995 FormatStyle::SBPO_Never); 18996 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 18997 FormatStyle::SBPO_ControlStatements); 18998 CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptForEachMacros", 18999 SpaceBeforeParens, 19000 FormatStyle::SBPO_ControlStatementsExceptControlMacros); 19001 19002 Style.ColumnLimit = 123; 19003 FormatStyle BaseStyle = getLLVMStyle(); 19004 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 19005 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 19006 19007 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 19008 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 19009 FormatStyle::BS_Attach); 19010 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 19011 FormatStyle::BS_Linux); 19012 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 19013 FormatStyle::BS_Mozilla); 19014 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 19015 FormatStyle::BS_Stroustrup); 19016 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 19017 FormatStyle::BS_Allman); 19018 CHECK_PARSE("BreakBeforeBraces: Whitesmiths", BreakBeforeBraces, 19019 FormatStyle::BS_Whitesmiths); 19020 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 19021 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 19022 FormatStyle::BS_WebKit); 19023 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 19024 FormatStyle::BS_Custom); 19025 19026 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never; 19027 CHECK_PARSE("BraceWrapping:\n" 19028 " AfterControlStatement: MultiLine", 19029 BraceWrapping.AfterControlStatement, 19030 FormatStyle::BWACS_MultiLine); 19031 CHECK_PARSE("BraceWrapping:\n" 19032 " AfterControlStatement: Always", 19033 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always); 19034 CHECK_PARSE("BraceWrapping:\n" 19035 " AfterControlStatement: Never", 19036 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never); 19037 // For backward compatibility: 19038 CHECK_PARSE("BraceWrapping:\n" 19039 " AfterControlStatement: true", 19040 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always); 19041 CHECK_PARSE("BraceWrapping:\n" 19042 " AfterControlStatement: false", 19043 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never); 19044 19045 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 19046 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 19047 FormatStyle::RTBS_None); 19048 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 19049 FormatStyle::RTBS_All); 19050 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 19051 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 19052 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 19053 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 19054 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 19055 AlwaysBreakAfterReturnType, 19056 FormatStyle::RTBS_TopLevelDefinitions); 19057 19058 Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 19059 CHECK_PARSE("AlwaysBreakTemplateDeclarations: No", 19060 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_No); 19061 CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine", 19062 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine); 19063 CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes", 19064 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes); 19065 CHECK_PARSE("AlwaysBreakTemplateDeclarations: false", 19066 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine); 19067 CHECK_PARSE("AlwaysBreakTemplateDeclarations: true", 19068 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes); 19069 19070 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 19071 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 19072 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 19073 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 19074 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 19075 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 19076 AlwaysBreakAfterDefinitionReturnType, 19077 FormatStyle::DRTBS_TopLevel); 19078 19079 Style.NamespaceIndentation = FormatStyle::NI_All; 19080 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 19081 FormatStyle::NI_None); 19082 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 19083 FormatStyle::NI_Inner); 19084 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 19085 FormatStyle::NI_All); 19086 19087 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_OnlyFirstIf; 19088 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Never", 19089 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never); 19090 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: WithoutElse", 19091 AllowShortIfStatementsOnASingleLine, 19092 FormatStyle::SIS_WithoutElse); 19093 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: OnlyFirstIf", 19094 AllowShortIfStatementsOnASingleLine, 19095 FormatStyle::SIS_OnlyFirstIf); 19096 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: AllIfsAndElse", 19097 AllowShortIfStatementsOnASingleLine, 19098 FormatStyle::SIS_AllIfsAndElse); 19099 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Always", 19100 AllowShortIfStatementsOnASingleLine, 19101 FormatStyle::SIS_OnlyFirstIf); 19102 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: false", 19103 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never); 19104 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: true", 19105 AllowShortIfStatementsOnASingleLine, 19106 FormatStyle::SIS_WithoutElse); 19107 19108 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; 19109 CHECK_PARSE("IndentExternBlock: AfterExternBlock", IndentExternBlock, 19110 FormatStyle::IEBS_AfterExternBlock); 19111 CHECK_PARSE("IndentExternBlock: Indent", IndentExternBlock, 19112 FormatStyle::IEBS_Indent); 19113 CHECK_PARSE("IndentExternBlock: NoIndent", IndentExternBlock, 19114 FormatStyle::IEBS_NoIndent); 19115 CHECK_PARSE("IndentExternBlock: true", IndentExternBlock, 19116 FormatStyle::IEBS_Indent); 19117 CHECK_PARSE("IndentExternBlock: false", IndentExternBlock, 19118 FormatStyle::IEBS_NoIndent); 19119 19120 Style.BitFieldColonSpacing = FormatStyle::BFCS_None; 19121 CHECK_PARSE("BitFieldColonSpacing: Both", BitFieldColonSpacing, 19122 FormatStyle::BFCS_Both); 19123 CHECK_PARSE("BitFieldColonSpacing: None", BitFieldColonSpacing, 19124 FormatStyle::BFCS_None); 19125 CHECK_PARSE("BitFieldColonSpacing: Before", BitFieldColonSpacing, 19126 FormatStyle::BFCS_Before); 19127 CHECK_PARSE("BitFieldColonSpacing: After", BitFieldColonSpacing, 19128 FormatStyle::BFCS_After); 19129 19130 Style.SortJavaStaticImport = FormatStyle::SJSIO_Before; 19131 CHECK_PARSE("SortJavaStaticImport: After", SortJavaStaticImport, 19132 FormatStyle::SJSIO_After); 19133 CHECK_PARSE("SortJavaStaticImport: Before", SortJavaStaticImport, 19134 FormatStyle::SJSIO_Before); 19135 19136 // FIXME: This is required because parsing a configuration simply overwrites 19137 // the first N elements of the list instead of resetting it. 19138 Style.ForEachMacros.clear(); 19139 std::vector<std::string> BoostForeach; 19140 BoostForeach.push_back("BOOST_FOREACH"); 19141 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 19142 std::vector<std::string> BoostAndQForeach; 19143 BoostAndQForeach.push_back("BOOST_FOREACH"); 19144 BoostAndQForeach.push_back("Q_FOREACH"); 19145 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 19146 BoostAndQForeach); 19147 19148 Style.IfMacros.clear(); 19149 std::vector<std::string> CustomIfs; 19150 CustomIfs.push_back("MYIF"); 19151 CHECK_PARSE("IfMacros: [MYIF]", IfMacros, CustomIfs); 19152 19153 Style.AttributeMacros.clear(); 19154 CHECK_PARSE("BasedOnStyle: LLVM", AttributeMacros, 19155 std::vector<std::string>{"__capability"}); 19156 CHECK_PARSE("AttributeMacros: [attr1, attr2]", AttributeMacros, 19157 std::vector<std::string>({"attr1", "attr2"})); 19158 19159 Style.StatementAttributeLikeMacros.clear(); 19160 CHECK_PARSE("StatementAttributeLikeMacros: [emit,Q_EMIT]", 19161 StatementAttributeLikeMacros, 19162 std::vector<std::string>({"emit", "Q_EMIT"})); 19163 19164 Style.StatementMacros.clear(); 19165 CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros, 19166 std::vector<std::string>{"QUNUSED"}); 19167 CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros, 19168 std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"})); 19169 19170 Style.NamespaceMacros.clear(); 19171 CHECK_PARSE("NamespaceMacros: [TESTSUITE]", NamespaceMacros, 19172 std::vector<std::string>{"TESTSUITE"}); 19173 CHECK_PARSE("NamespaceMacros: [TESTSUITE, SUITE]", NamespaceMacros, 19174 std::vector<std::string>({"TESTSUITE", "SUITE"})); 19175 19176 Style.WhitespaceSensitiveMacros.clear(); 19177 CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE]", 19178 WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"}); 19179 CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE, ASSERT]", 19180 WhitespaceSensitiveMacros, 19181 std::vector<std::string>({"STRINGIZE", "ASSERT"})); 19182 Style.WhitespaceSensitiveMacros.clear(); 19183 CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE']", 19184 WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"}); 19185 CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE', 'ASSERT']", 19186 WhitespaceSensitiveMacros, 19187 std::vector<std::string>({"STRINGIZE", "ASSERT"})); 19188 19189 Style.IncludeStyle.IncludeCategories.clear(); 19190 std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = { 19191 {"abc/.*", 2, 0, false}, {".*", 1, 0, true}}; 19192 CHECK_PARSE("IncludeCategories:\n" 19193 " - Regex: abc/.*\n" 19194 " Priority: 2\n" 19195 " - Regex: .*\n" 19196 " Priority: 1\n" 19197 " CaseSensitive: true\n", 19198 IncludeStyle.IncludeCategories, ExpectedCategories); 19199 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex, 19200 "abc$"); 19201 CHECK_PARSE("IncludeIsMainSourceRegex: 'abc$'", 19202 IncludeStyle.IncludeIsMainSourceRegex, "abc$"); 19203 19204 Style.SortIncludes = FormatStyle::SI_Never; 19205 CHECK_PARSE("SortIncludes: true", SortIncludes, 19206 FormatStyle::SI_CaseSensitive); 19207 CHECK_PARSE("SortIncludes: false", SortIncludes, FormatStyle::SI_Never); 19208 CHECK_PARSE("SortIncludes: CaseInsensitive", SortIncludes, 19209 FormatStyle::SI_CaseInsensitive); 19210 CHECK_PARSE("SortIncludes: CaseSensitive", SortIncludes, 19211 FormatStyle::SI_CaseSensitive); 19212 CHECK_PARSE("SortIncludes: Never", SortIncludes, FormatStyle::SI_Never); 19213 19214 Style.RawStringFormats.clear(); 19215 std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = { 19216 { 19217 FormatStyle::LK_TextProto, 19218 {"pb", "proto"}, 19219 {"PARSE_TEXT_PROTO"}, 19220 /*CanonicalDelimiter=*/"", 19221 "llvm", 19222 }, 19223 { 19224 FormatStyle::LK_Cpp, 19225 {"cc", "cpp"}, 19226 {"C_CODEBLOCK", "CPPEVAL"}, 19227 /*CanonicalDelimiter=*/"cc", 19228 /*BasedOnStyle=*/"", 19229 }, 19230 }; 19231 19232 CHECK_PARSE("RawStringFormats:\n" 19233 " - Language: TextProto\n" 19234 " Delimiters:\n" 19235 " - 'pb'\n" 19236 " - 'proto'\n" 19237 " EnclosingFunctions:\n" 19238 " - 'PARSE_TEXT_PROTO'\n" 19239 " BasedOnStyle: llvm\n" 19240 " - Language: Cpp\n" 19241 " Delimiters:\n" 19242 " - 'cc'\n" 19243 " - 'cpp'\n" 19244 " EnclosingFunctions:\n" 19245 " - 'C_CODEBLOCK'\n" 19246 " - 'CPPEVAL'\n" 19247 " CanonicalDelimiter: 'cc'", 19248 RawStringFormats, ExpectedRawStringFormats); 19249 19250 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 19251 " Minimum: 0\n" 19252 " Maximum: 0", 19253 SpacesInLineCommentPrefix.Minimum, 0u); 19254 EXPECT_EQ(Style.SpacesInLineCommentPrefix.Maximum, 0u); 19255 Style.SpacesInLineCommentPrefix.Minimum = 1; 19256 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 19257 " Minimum: 2", 19258 SpacesInLineCommentPrefix.Minimum, 0u); 19259 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 19260 " Maximum: -1", 19261 SpacesInLineCommentPrefix.Maximum, -1u); 19262 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 19263 " Minimum: 2", 19264 SpacesInLineCommentPrefix.Minimum, 2u); 19265 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 19266 " Maximum: 1", 19267 SpacesInLineCommentPrefix.Maximum, 1u); 19268 EXPECT_EQ(Style.SpacesInLineCommentPrefix.Minimum, 1u); 19269 19270 Style.SpacesInAngles = FormatStyle::SIAS_Always; 19271 CHECK_PARSE("SpacesInAngles: Never", SpacesInAngles, FormatStyle::SIAS_Never); 19272 CHECK_PARSE("SpacesInAngles: Always", SpacesInAngles, 19273 FormatStyle::SIAS_Always); 19274 CHECK_PARSE("SpacesInAngles: Leave", SpacesInAngles, FormatStyle::SIAS_Leave); 19275 // For backward compatibility: 19276 CHECK_PARSE("SpacesInAngles: false", SpacesInAngles, FormatStyle::SIAS_Never); 19277 CHECK_PARSE("SpacesInAngles: true", SpacesInAngles, FormatStyle::SIAS_Always); 19278 } 19279 19280 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 19281 FormatStyle Style = {}; 19282 Style.Language = FormatStyle::LK_Cpp; 19283 CHECK_PARSE("Language: Cpp\n" 19284 "IndentWidth: 12", 19285 IndentWidth, 12u); 19286 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 19287 "IndentWidth: 34", 19288 &Style), 19289 ParseError::Unsuitable); 19290 FormatStyle BinPackedTCS = {}; 19291 BinPackedTCS.Language = FormatStyle::LK_JavaScript; 19292 EXPECT_EQ(parseConfiguration("BinPackArguments: true\n" 19293 "InsertTrailingCommas: Wrapped", 19294 &BinPackedTCS), 19295 ParseError::BinPackTrailingCommaConflict); 19296 EXPECT_EQ(12u, Style.IndentWidth); 19297 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 19298 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 19299 19300 Style.Language = FormatStyle::LK_JavaScript; 19301 CHECK_PARSE("Language: JavaScript\n" 19302 "IndentWidth: 12", 19303 IndentWidth, 12u); 19304 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 19305 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 19306 "IndentWidth: 34", 19307 &Style), 19308 ParseError::Unsuitable); 19309 EXPECT_EQ(23u, Style.IndentWidth); 19310 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 19311 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 19312 19313 CHECK_PARSE("BasedOnStyle: LLVM\n" 19314 "IndentWidth: 67", 19315 IndentWidth, 67u); 19316 19317 CHECK_PARSE("---\n" 19318 "Language: JavaScript\n" 19319 "IndentWidth: 12\n" 19320 "---\n" 19321 "Language: Cpp\n" 19322 "IndentWidth: 34\n" 19323 "...\n", 19324 IndentWidth, 12u); 19325 19326 Style.Language = FormatStyle::LK_Cpp; 19327 CHECK_PARSE("---\n" 19328 "Language: JavaScript\n" 19329 "IndentWidth: 12\n" 19330 "---\n" 19331 "Language: Cpp\n" 19332 "IndentWidth: 34\n" 19333 "...\n", 19334 IndentWidth, 34u); 19335 CHECK_PARSE("---\n" 19336 "IndentWidth: 78\n" 19337 "---\n" 19338 "Language: JavaScript\n" 19339 "IndentWidth: 56\n" 19340 "...\n", 19341 IndentWidth, 78u); 19342 19343 Style.ColumnLimit = 123; 19344 Style.IndentWidth = 234; 19345 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 19346 Style.TabWidth = 345; 19347 EXPECT_FALSE(parseConfiguration("---\n" 19348 "IndentWidth: 456\n" 19349 "BreakBeforeBraces: Allman\n" 19350 "---\n" 19351 "Language: JavaScript\n" 19352 "IndentWidth: 111\n" 19353 "TabWidth: 111\n" 19354 "---\n" 19355 "Language: Cpp\n" 19356 "BreakBeforeBraces: Stroustrup\n" 19357 "TabWidth: 789\n" 19358 "...\n", 19359 &Style)); 19360 EXPECT_EQ(123u, Style.ColumnLimit); 19361 EXPECT_EQ(456u, Style.IndentWidth); 19362 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 19363 EXPECT_EQ(789u, Style.TabWidth); 19364 19365 EXPECT_EQ(parseConfiguration("---\n" 19366 "Language: JavaScript\n" 19367 "IndentWidth: 56\n" 19368 "---\n" 19369 "IndentWidth: 78\n" 19370 "...\n", 19371 &Style), 19372 ParseError::Error); 19373 EXPECT_EQ(parseConfiguration("---\n" 19374 "Language: JavaScript\n" 19375 "IndentWidth: 56\n" 19376 "---\n" 19377 "Language: JavaScript\n" 19378 "IndentWidth: 78\n" 19379 "...\n", 19380 &Style), 19381 ParseError::Error); 19382 19383 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 19384 } 19385 19386 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 19387 FormatStyle Style = {}; 19388 Style.Language = FormatStyle::LK_JavaScript; 19389 Style.BreakBeforeTernaryOperators = true; 19390 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 19391 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 19392 19393 Style.BreakBeforeTernaryOperators = true; 19394 EXPECT_EQ(0, parseConfiguration("---\n" 19395 "BasedOnStyle: Google\n" 19396 "---\n" 19397 "Language: JavaScript\n" 19398 "IndentWidth: 76\n" 19399 "...\n", 19400 &Style) 19401 .value()); 19402 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 19403 EXPECT_EQ(76u, Style.IndentWidth); 19404 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 19405 } 19406 19407 TEST_F(FormatTest, ConfigurationRoundTripTest) { 19408 FormatStyle Style = getLLVMStyle(); 19409 std::string YAML = configurationAsText(Style); 19410 FormatStyle ParsedStyle = {}; 19411 ParsedStyle.Language = FormatStyle::LK_Cpp; 19412 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 19413 EXPECT_EQ(Style, ParsedStyle); 19414 } 19415 19416 TEST_F(FormatTest, WorksFor8bitEncodings) { 19417 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 19418 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 19419 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 19420 "\"\xef\xee\xf0\xf3...\"", 19421 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 19422 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 19423 "\xef\xee\xf0\xf3...\"", 19424 getLLVMStyleWithColumns(12))); 19425 } 19426 19427 TEST_F(FormatTest, HandlesUTF8BOM) { 19428 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 19429 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 19430 format("\xef\xbb\xbf#include <iostream>")); 19431 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 19432 format("\xef\xbb\xbf\n#include <iostream>")); 19433 } 19434 19435 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 19436 #if !defined(_MSC_VER) 19437 19438 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 19439 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 19440 getLLVMStyleWithColumns(35)); 19441 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 19442 getLLVMStyleWithColumns(31)); 19443 verifyFormat("// Однажды в студёную зимнюю пору...", 19444 getLLVMStyleWithColumns(36)); 19445 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 19446 verifyFormat("/* Однажды в студёную зимнюю пору... */", 19447 getLLVMStyleWithColumns(39)); 19448 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 19449 getLLVMStyleWithColumns(35)); 19450 } 19451 19452 TEST_F(FormatTest, SplitsUTF8Strings) { 19453 // Non-printable characters' width is currently considered to be the length in 19454 // bytes in UTF8. The characters can be displayed in very different manner 19455 // (zero-width, single width with a substitution glyph, expanded to their code 19456 // (e.g. "<8d>"), so there's no single correct way to handle them. 19457 EXPECT_EQ("\"aaaaÄ\"\n" 19458 "\"\xc2\x8d\";", 19459 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 19460 EXPECT_EQ("\"aaaaaaaÄ\"\n" 19461 "\"\xc2\x8d\";", 19462 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 19463 EXPECT_EQ("\"Однажды, в \"\n" 19464 "\"студёную \"\n" 19465 "\"зимнюю \"\n" 19466 "\"пору,\"", 19467 format("\"Однажды, в студёную зимнюю пору,\"", 19468 getLLVMStyleWithColumns(13))); 19469 EXPECT_EQ( 19470 "\"一 二 三 \"\n" 19471 "\"四 五六 \"\n" 19472 "\"七 八 九 \"\n" 19473 "\"十\"", 19474 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 19475 EXPECT_EQ("\"一\t\"\n" 19476 "\"二 \t\"\n" 19477 "\"三 四 \"\n" 19478 "\"五\t\"\n" 19479 "\"六 \t\"\n" 19480 "\"七 \"\n" 19481 "\"八九十\tqq\"", 19482 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 19483 getLLVMStyleWithColumns(11))); 19484 19485 // UTF8 character in an escape sequence. 19486 EXPECT_EQ("\"aaaaaa\"\n" 19487 "\"\\\xC2\x8D\"", 19488 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 19489 } 19490 19491 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 19492 EXPECT_EQ("const char *sssss =\n" 19493 " \"一二三四五六七八\\\n" 19494 " 九 十\";", 19495 format("const char *sssss = \"一二三四五六七八\\\n" 19496 " 九 十\";", 19497 getLLVMStyleWithColumns(30))); 19498 } 19499 19500 TEST_F(FormatTest, SplitsUTF8LineComments) { 19501 EXPECT_EQ("// aaaaÄ\xc2\x8d", 19502 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 19503 EXPECT_EQ("// Я из лесу\n" 19504 "// вышел; был\n" 19505 "// сильный\n" 19506 "// мороз.", 19507 format("// Я из лесу вышел; был сильный мороз.", 19508 getLLVMStyleWithColumns(13))); 19509 EXPECT_EQ("// 一二三\n" 19510 "// 四五六七\n" 19511 "// 八 九\n" 19512 "// 十", 19513 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 19514 } 19515 19516 TEST_F(FormatTest, SplitsUTF8BlockComments) { 19517 EXPECT_EQ("/* Гляжу,\n" 19518 " * поднимается\n" 19519 " * медленно в\n" 19520 " * гору\n" 19521 " * Лошадка,\n" 19522 " * везущая\n" 19523 " * хворосту\n" 19524 " * воз. */", 19525 format("/* Гляжу, поднимается медленно в гору\n" 19526 " * Лошадка, везущая хворосту воз. */", 19527 getLLVMStyleWithColumns(13))); 19528 EXPECT_EQ( 19529 "/* 一二三\n" 19530 " * 四五六七\n" 19531 " * 八 九\n" 19532 " * 十 */", 19533 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 19534 EXPECT_EQ("/* \n" 19535 " * \n" 19536 " * - */", 19537 format("/* - */", getLLVMStyleWithColumns(12))); 19538 } 19539 19540 #endif // _MSC_VER 19541 19542 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 19543 FormatStyle Style = getLLVMStyle(); 19544 19545 Style.ConstructorInitializerIndentWidth = 4; 19546 verifyFormat( 19547 "SomeClass::Constructor()\n" 19548 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 19549 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 19550 Style); 19551 19552 Style.ConstructorInitializerIndentWidth = 2; 19553 verifyFormat( 19554 "SomeClass::Constructor()\n" 19555 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 19556 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 19557 Style); 19558 19559 Style.ConstructorInitializerIndentWidth = 0; 19560 verifyFormat( 19561 "SomeClass::Constructor()\n" 19562 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 19563 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 19564 Style); 19565 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 19566 verifyFormat( 19567 "SomeLongTemplateVariableName<\n" 19568 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 19569 Style); 19570 verifyFormat("bool smaller = 1 < " 19571 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 19572 " " 19573 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 19574 Style); 19575 19576 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 19577 verifyFormat("SomeClass::Constructor() :\n" 19578 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n" 19579 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}", 19580 Style); 19581 } 19582 19583 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 19584 FormatStyle Style = getLLVMStyle(); 19585 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 19586 Style.ConstructorInitializerIndentWidth = 4; 19587 verifyFormat("SomeClass::Constructor()\n" 19588 " : a(a)\n" 19589 " , b(b)\n" 19590 " , c(c) {}", 19591 Style); 19592 verifyFormat("SomeClass::Constructor()\n" 19593 " : a(a) {}", 19594 Style); 19595 19596 Style.ColumnLimit = 0; 19597 verifyFormat("SomeClass::Constructor()\n" 19598 " : a(a) {}", 19599 Style); 19600 verifyFormat("SomeClass::Constructor() noexcept\n" 19601 " : a(a) {}", 19602 Style); 19603 verifyFormat("SomeClass::Constructor()\n" 19604 " : a(a)\n" 19605 " , b(b)\n" 19606 " , c(c) {}", 19607 Style); 19608 verifyFormat("SomeClass::Constructor()\n" 19609 " : a(a) {\n" 19610 " foo();\n" 19611 " bar();\n" 19612 "}", 19613 Style); 19614 19615 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 19616 verifyFormat("SomeClass::Constructor()\n" 19617 " : a(a)\n" 19618 " , b(b)\n" 19619 " , c(c) {\n}", 19620 Style); 19621 verifyFormat("SomeClass::Constructor()\n" 19622 " : a(a) {\n}", 19623 Style); 19624 19625 Style.ColumnLimit = 80; 19626 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 19627 Style.ConstructorInitializerIndentWidth = 2; 19628 verifyFormat("SomeClass::Constructor()\n" 19629 " : a(a)\n" 19630 " , b(b)\n" 19631 " , c(c) {}", 19632 Style); 19633 19634 Style.ConstructorInitializerIndentWidth = 0; 19635 verifyFormat("SomeClass::Constructor()\n" 19636 ": a(a)\n" 19637 ", b(b)\n" 19638 ", c(c) {}", 19639 Style); 19640 19641 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 19642 Style.ConstructorInitializerIndentWidth = 4; 19643 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 19644 verifyFormat( 19645 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 19646 Style); 19647 verifyFormat( 19648 "SomeClass::Constructor()\n" 19649 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 19650 Style); 19651 Style.ConstructorInitializerIndentWidth = 4; 19652 Style.ColumnLimit = 60; 19653 verifyFormat("SomeClass::Constructor()\n" 19654 " : aaaaaaaa(aaaaaaaa)\n" 19655 " , aaaaaaaa(aaaaaaaa)\n" 19656 " , aaaaaaaa(aaaaaaaa) {}", 19657 Style); 19658 } 19659 19660 TEST_F(FormatTest, ConstructorInitializersWithPreprocessorDirective) { 19661 FormatStyle Style = getLLVMStyle(); 19662 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 19663 Style.ConstructorInitializerIndentWidth = 4; 19664 verifyFormat("SomeClass::Constructor()\n" 19665 " : a{a}\n" 19666 " , b{b} {}", 19667 Style); 19668 verifyFormat("SomeClass::Constructor()\n" 19669 " : a{a}\n" 19670 "#if CONDITION\n" 19671 " , b{b}\n" 19672 "#endif\n" 19673 "{\n}", 19674 Style); 19675 Style.ConstructorInitializerIndentWidth = 2; 19676 verifyFormat("SomeClass::Constructor()\n" 19677 "#if CONDITION\n" 19678 " : a{a}\n" 19679 "#endif\n" 19680 " , b{b}\n" 19681 " , c{c} {\n}", 19682 Style); 19683 Style.ConstructorInitializerIndentWidth = 0; 19684 verifyFormat("SomeClass::Constructor()\n" 19685 ": a{a}\n" 19686 "#ifdef CONDITION\n" 19687 ", b{b}\n" 19688 "#else\n" 19689 ", c{c}\n" 19690 "#endif\n" 19691 ", d{d} {\n}", 19692 Style); 19693 Style.ConstructorInitializerIndentWidth = 4; 19694 verifyFormat("SomeClass::Constructor()\n" 19695 " : a{a}\n" 19696 "#if WINDOWS\n" 19697 "#if DEBUG\n" 19698 " , b{0}\n" 19699 "#else\n" 19700 " , b{1}\n" 19701 "#endif\n" 19702 "#else\n" 19703 "#if DEBUG\n" 19704 " , b{2}\n" 19705 "#else\n" 19706 " , b{3}\n" 19707 "#endif\n" 19708 "#endif\n" 19709 "{\n}", 19710 Style); 19711 verifyFormat("SomeClass::Constructor()\n" 19712 " : a{a}\n" 19713 "#if WINDOWS\n" 19714 " , b{0}\n" 19715 "#if DEBUG\n" 19716 " , c{0}\n" 19717 "#else\n" 19718 " , c{1}\n" 19719 "#endif\n" 19720 "#else\n" 19721 "#if DEBUG\n" 19722 " , c{2}\n" 19723 "#else\n" 19724 " , c{3}\n" 19725 "#endif\n" 19726 " , b{1}\n" 19727 "#endif\n" 19728 "{\n}", 19729 Style); 19730 } 19731 19732 TEST_F(FormatTest, Destructors) { 19733 verifyFormat("void F(int &i) { i.~int(); }"); 19734 verifyFormat("void F(int &i) { i->~int(); }"); 19735 } 19736 19737 TEST_F(FormatTest, FormatsWithWebKitStyle) { 19738 FormatStyle Style = getWebKitStyle(); 19739 19740 // Don't indent in outer namespaces. 19741 verifyFormat("namespace outer {\n" 19742 "int i;\n" 19743 "namespace inner {\n" 19744 " int i;\n" 19745 "} // namespace inner\n" 19746 "} // namespace outer\n" 19747 "namespace other_outer {\n" 19748 "int i;\n" 19749 "}", 19750 Style); 19751 19752 // Don't indent case labels. 19753 verifyFormat("switch (variable) {\n" 19754 "case 1:\n" 19755 "case 2:\n" 19756 " doSomething();\n" 19757 " break;\n" 19758 "default:\n" 19759 " ++variable;\n" 19760 "}", 19761 Style); 19762 19763 // Wrap before binary operators. 19764 EXPECT_EQ("void f()\n" 19765 "{\n" 19766 " if (aaaaaaaaaaaaaaaa\n" 19767 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 19768 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 19769 " return;\n" 19770 "}", 19771 format("void f() {\n" 19772 "if (aaaaaaaaaaaaaaaa\n" 19773 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 19774 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 19775 "return;\n" 19776 "}", 19777 Style)); 19778 19779 // Allow functions on a single line. 19780 verifyFormat("void f() { return; }", Style); 19781 19782 // Allow empty blocks on a single line and insert a space in empty blocks. 19783 EXPECT_EQ("void f() { }", format("void f() {}", Style)); 19784 EXPECT_EQ("while (true) { }", format("while (true) {}", Style)); 19785 // However, don't merge non-empty short loops. 19786 EXPECT_EQ("while (true) {\n" 19787 " continue;\n" 19788 "}", 19789 format("while (true) { continue; }", Style)); 19790 19791 // Constructor initializers are formatted one per line with the "," on the 19792 // new line. 19793 verifyFormat("Constructor()\n" 19794 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 19795 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 19796 " aaaaaaaaaaaaaa)\n" 19797 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 19798 "{\n" 19799 "}", 19800 Style); 19801 verifyFormat("SomeClass::Constructor()\n" 19802 " : a(a)\n" 19803 "{\n" 19804 "}", 19805 Style); 19806 EXPECT_EQ("SomeClass::Constructor()\n" 19807 " : a(a)\n" 19808 "{\n" 19809 "}", 19810 format("SomeClass::Constructor():a(a){}", Style)); 19811 verifyFormat("SomeClass::Constructor()\n" 19812 " : a(a)\n" 19813 " , b(b)\n" 19814 " , c(c)\n" 19815 "{\n" 19816 "}", 19817 Style); 19818 verifyFormat("SomeClass::Constructor()\n" 19819 " : a(a)\n" 19820 "{\n" 19821 " foo();\n" 19822 " bar();\n" 19823 "}", 19824 Style); 19825 19826 // Access specifiers should be aligned left. 19827 verifyFormat("class C {\n" 19828 "public:\n" 19829 " int i;\n" 19830 "};", 19831 Style); 19832 19833 // Do not align comments. 19834 verifyFormat("int a; // Do not\n" 19835 "double b; // align comments.", 19836 Style); 19837 19838 // Do not align operands. 19839 EXPECT_EQ("ASSERT(aaaa\n" 19840 " || bbbb);", 19841 format("ASSERT ( aaaa\n||bbbb);", Style)); 19842 19843 // Accept input's line breaks. 19844 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 19845 " || bbbbbbbbbbbbbbb) {\n" 19846 " i++;\n" 19847 "}", 19848 format("if (aaaaaaaaaaaaaaa\n" 19849 "|| bbbbbbbbbbbbbbb) { i++; }", 19850 Style)); 19851 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 19852 " i++;\n" 19853 "}", 19854 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 19855 19856 // Don't automatically break all macro definitions (llvm.org/PR17842). 19857 verifyFormat("#define aNumber 10", Style); 19858 // However, generally keep the line breaks that the user authored. 19859 EXPECT_EQ("#define aNumber \\\n" 19860 " 10", 19861 format("#define aNumber \\\n" 19862 " 10", 19863 Style)); 19864 19865 // Keep empty and one-element array literals on a single line. 19866 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 19867 " copyItems:YES];", 19868 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 19869 "copyItems:YES];", 19870 Style)); 19871 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 19872 " copyItems:YES];", 19873 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 19874 " copyItems:YES];", 19875 Style)); 19876 // FIXME: This does not seem right, there should be more indentation before 19877 // the array literal's entries. Nested blocks have the same problem. 19878 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 19879 " @\"a\",\n" 19880 " @\"a\"\n" 19881 "]\n" 19882 " copyItems:YES];", 19883 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 19884 " @\"a\",\n" 19885 " @\"a\"\n" 19886 " ]\n" 19887 " copyItems:YES];", 19888 Style)); 19889 EXPECT_EQ( 19890 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 19891 " copyItems:YES];", 19892 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 19893 " copyItems:YES];", 19894 Style)); 19895 19896 verifyFormat("[self.a b:c c:d];", Style); 19897 EXPECT_EQ("[self.a b:c\n" 19898 " c:d];", 19899 format("[self.a b:c\n" 19900 "c:d];", 19901 Style)); 19902 } 19903 19904 TEST_F(FormatTest, FormatsLambdas) { 19905 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 19906 verifyFormat( 19907 "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();\n"); 19908 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 19909 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 19910 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 19911 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 19912 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 19913 verifyFormat("auto c = [a = [b = 42] {}] {};\n"); 19914 verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n"); 19915 verifyFormat("int x = f(*+[] {});"); 19916 verifyFormat("void f() {\n" 19917 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 19918 "}\n"); 19919 verifyFormat("void f() {\n" 19920 " other(x.begin(), //\n" 19921 " x.end(), //\n" 19922 " [&](int, int) { return 1; });\n" 19923 "}\n"); 19924 verifyFormat("void f() {\n" 19925 " other.other.other.other.other(\n" 19926 " x.begin(), x.end(),\n" 19927 " [something, rather](int, int, int, int, int, int, int) { " 19928 "return 1; });\n" 19929 "}\n"); 19930 verifyFormat( 19931 "void f() {\n" 19932 " other.other.other.other.other(\n" 19933 " x.begin(), x.end(),\n" 19934 " [something, rather](int, int, int, int, int, int, int) {\n" 19935 " //\n" 19936 " });\n" 19937 "}\n"); 19938 verifyFormat("SomeFunction([]() { // A cool function...\n" 19939 " return 43;\n" 19940 "});"); 19941 EXPECT_EQ("SomeFunction([]() {\n" 19942 "#define A a\n" 19943 " return 43;\n" 19944 "});", 19945 format("SomeFunction([](){\n" 19946 "#define A a\n" 19947 "return 43;\n" 19948 "});")); 19949 verifyFormat("void f() {\n" 19950 " SomeFunction([](decltype(x), A *a) {});\n" 19951 " SomeFunction([](typeof(x), A *a) {});\n" 19952 " SomeFunction([](_Atomic(x), A *a) {});\n" 19953 " SomeFunction([](__underlying_type(x), A *a) {});\n" 19954 "}"); 19955 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 19956 " [](const aaaaaaaaaa &a) { return a; });"); 19957 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 19958 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 19959 "});"); 19960 verifyFormat("Constructor()\n" 19961 " : Field([] { // comment\n" 19962 " int i;\n" 19963 " }) {}"); 19964 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 19965 " return some_parameter.size();\n" 19966 "};"); 19967 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 19968 " [](const string &s) { return s; };"); 19969 verifyFormat("int i = aaaaaa ? 1 //\n" 19970 " : [] {\n" 19971 " return 2; //\n" 19972 " }();"); 19973 verifyFormat("llvm::errs() << \"number of twos is \"\n" 19974 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 19975 " return x == 2; // force break\n" 19976 " });"); 19977 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 19978 " [=](int iiiiiiiiiiii) {\n" 19979 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n" 19980 " aaaaaaaaaaaaaaaaaaaaaaa;\n" 19981 " });", 19982 getLLVMStyleWithColumns(60)); 19983 19984 verifyFormat("SomeFunction({[&] {\n" 19985 " // comment\n" 19986 " },\n" 19987 " [&] {\n" 19988 " // comment\n" 19989 " }});"); 19990 verifyFormat("SomeFunction({[&] {\n" 19991 " // comment\n" 19992 "}});"); 19993 verifyFormat( 19994 "virtual aaaaaaaaaaaaaaaa(\n" 19995 " std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n" 19996 " aaaaa aaaaaaaaa);"); 19997 19998 // Lambdas with return types. 19999 verifyFormat("int c = []() -> int { return 2; }();\n"); 20000 verifyFormat("int c = []() -> int * { return 2; }();\n"); 20001 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 20002 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 20003 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 20004 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 20005 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 20006 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 20007 verifyFormat("[a, a]() -> a<1> {};"); 20008 verifyFormat("[]() -> foo<5 + 2> { return {}; };"); 20009 verifyFormat("[]() -> foo<5 - 2> { return {}; };"); 20010 verifyFormat("[]() -> foo<5 / 2> { return {}; };"); 20011 verifyFormat("[]() -> foo<5 * 2> { return {}; };"); 20012 verifyFormat("[]() -> foo<5 % 2> { return {}; };"); 20013 verifyFormat("[]() -> foo<5 << 2> { return {}; };"); 20014 verifyFormat("[]() -> foo<!5> { return {}; };"); 20015 verifyFormat("[]() -> foo<~5> { return {}; };"); 20016 verifyFormat("[]() -> foo<5 | 2> { return {}; };"); 20017 verifyFormat("[]() -> foo<5 || 2> { return {}; };"); 20018 verifyFormat("[]() -> foo<5 & 2> { return {}; };"); 20019 verifyFormat("[]() -> foo<5 && 2> { return {}; };"); 20020 verifyFormat("[]() -> foo<5 == 2> { return {}; };"); 20021 verifyFormat("[]() -> foo<5 != 2> { return {}; };"); 20022 verifyFormat("[]() -> foo<5 >= 2> { return {}; };"); 20023 verifyFormat("[]() -> foo<5 <= 2> { return {}; };"); 20024 verifyFormat("[]() -> foo<5 < 2> { return {}; };"); 20025 verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };"); 20026 verifyFormat("namespace bar {\n" 20027 "// broken:\n" 20028 "auto foo{[]() -> foo<5 + 2> { return {}; }};\n" 20029 "} // namespace bar"); 20030 verifyFormat("namespace bar {\n" 20031 "// broken:\n" 20032 "auto foo{[]() -> foo<5 - 2> { return {}; }};\n" 20033 "} // namespace bar"); 20034 verifyFormat("namespace bar {\n" 20035 "// broken:\n" 20036 "auto foo{[]() -> foo<5 / 2> { return {}; }};\n" 20037 "} // namespace bar"); 20038 verifyFormat("namespace bar {\n" 20039 "// broken:\n" 20040 "auto foo{[]() -> foo<5 * 2> { return {}; }};\n" 20041 "} // namespace bar"); 20042 verifyFormat("namespace bar {\n" 20043 "// broken:\n" 20044 "auto foo{[]() -> foo<5 % 2> { return {}; }};\n" 20045 "} // namespace bar"); 20046 verifyFormat("namespace bar {\n" 20047 "// broken:\n" 20048 "auto foo{[]() -> foo<5 << 2> { return {}; }};\n" 20049 "} // namespace bar"); 20050 verifyFormat("namespace bar {\n" 20051 "// broken:\n" 20052 "auto foo{[]() -> foo<!5> { return {}; }};\n" 20053 "} // namespace bar"); 20054 verifyFormat("namespace bar {\n" 20055 "// broken:\n" 20056 "auto foo{[]() -> foo<~5> { return {}; }};\n" 20057 "} // namespace bar"); 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 >= 2> { return {}; }};\n" 20085 "} // namespace bar"); 20086 verifyFormat("namespace bar {\n" 20087 "// broken:\n" 20088 "auto foo{[]() -> foo<5 <= 2> { 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<2 ? 1 : 0> { return {}; }};\n" 20097 "} // namespace bar"); 20098 verifyFormat("[]() -> a<1> {};"); 20099 verifyFormat("[]() -> a<1> { ; };"); 20100 verifyFormat("[]() -> a<1> { ; }();"); 20101 verifyFormat("[a, a]() -> a<true> {};"); 20102 verifyFormat("[]() -> a<true> {};"); 20103 verifyFormat("[]() -> a<true> { ; };"); 20104 verifyFormat("[]() -> a<true> { ; }();"); 20105 verifyFormat("[a, a]() -> a<false> {};"); 20106 verifyFormat("[]() -> a<false> {};"); 20107 verifyFormat("[]() -> a<false> { ; };"); 20108 verifyFormat("[]() -> a<false> { ; }();"); 20109 verifyFormat("auto foo{[]() -> foo<false> { ; }};"); 20110 verifyFormat("namespace bar {\n" 20111 "auto foo{[]() -> foo<false> { ; }};\n" 20112 "} // namespace bar"); 20113 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 20114 " int j) -> int {\n" 20115 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 20116 "};"); 20117 verifyFormat( 20118 "aaaaaaaaaaaaaaaaaaaaaa(\n" 20119 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 20120 " return aaaaaaaaaaaaaaaaa;\n" 20121 " });", 20122 getLLVMStyleWithColumns(70)); 20123 verifyFormat("[]() //\n" 20124 " -> int {\n" 20125 " return 1; //\n" 20126 "};"); 20127 verifyFormat("[]() -> Void<T...> {};"); 20128 verifyFormat("[a, b]() -> Tuple<T...> { return {}; };"); 20129 20130 // Lambdas with explicit template argument lists. 20131 verifyFormat( 20132 "auto L = []<template <typename> class T, class U>(T<U> &&a) {};\n"); 20133 20134 // Multiple lambdas in the same parentheses change indentation rules. These 20135 // lambdas are forced to start on new lines. 20136 verifyFormat("SomeFunction(\n" 20137 " []() {\n" 20138 " //\n" 20139 " },\n" 20140 " []() {\n" 20141 " //\n" 20142 " });"); 20143 20144 // A lambda passed as arg0 is always pushed to the next line. 20145 verifyFormat("SomeFunction(\n" 20146 " [this] {\n" 20147 " //\n" 20148 " },\n" 20149 " 1);\n"); 20150 20151 // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like 20152 // the arg0 case above. 20153 auto Style = getGoogleStyle(); 20154 Style.BinPackArguments = false; 20155 verifyFormat("SomeFunction(\n" 20156 " a,\n" 20157 " [this] {\n" 20158 " //\n" 20159 " },\n" 20160 " b);\n", 20161 Style); 20162 verifyFormat("SomeFunction(\n" 20163 " a,\n" 20164 " [this] {\n" 20165 " //\n" 20166 " },\n" 20167 " b);\n"); 20168 20169 // A lambda with a very long line forces arg0 to be pushed out irrespective of 20170 // the BinPackArguments value (as long as the code is wide enough). 20171 verifyFormat( 20172 "something->SomeFunction(\n" 20173 " a,\n" 20174 " [this] {\n" 20175 " " 20176 "D0000000000000000000000000000000000000000000000000000000000001();\n" 20177 " },\n" 20178 " b);\n"); 20179 20180 // A multi-line lambda is pulled up as long as the introducer fits on the 20181 // previous line and there are no further args. 20182 verifyFormat("function(1, [this, that] {\n" 20183 " //\n" 20184 "});\n"); 20185 verifyFormat("function([this, that] {\n" 20186 " //\n" 20187 "});\n"); 20188 // FIXME: this format is not ideal and we should consider forcing the first 20189 // arg onto its own line. 20190 verifyFormat("function(a, b, c, //\n" 20191 " d, [this, that] {\n" 20192 " //\n" 20193 " });\n"); 20194 20195 // Multiple lambdas are treated correctly even when there is a short arg0. 20196 verifyFormat("SomeFunction(\n" 20197 " 1,\n" 20198 " [this] {\n" 20199 " //\n" 20200 " },\n" 20201 " [this] {\n" 20202 " //\n" 20203 " },\n" 20204 " 1);\n"); 20205 20206 // More complex introducers. 20207 verifyFormat("return [i, args...] {};"); 20208 20209 // Not lambdas. 20210 verifyFormat("constexpr char hello[]{\"hello\"};"); 20211 verifyFormat("double &operator[](int i) { return 0; }\n" 20212 "int i;"); 20213 verifyFormat("std::unique_ptr<int[]> foo() {}"); 20214 verifyFormat("int i = a[a][a]->f();"); 20215 verifyFormat("int i = (*b)[a]->f();"); 20216 20217 // Other corner cases. 20218 verifyFormat("void f() {\n" 20219 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 20220 " );\n" 20221 "}"); 20222 20223 // Lambdas created through weird macros. 20224 verifyFormat("void f() {\n" 20225 " MACRO((const AA &a) { return 1; });\n" 20226 " MACRO((AA &a) { return 1; });\n" 20227 "}"); 20228 20229 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 20230 " doo_dah();\n" 20231 " doo_dah();\n" 20232 " })) {\n" 20233 "}"); 20234 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n" 20235 " doo_dah();\n" 20236 " doo_dah();\n" 20237 " })) {\n" 20238 "}"); 20239 verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n" 20240 " doo_dah();\n" 20241 " doo_dah();\n" 20242 " })) {\n" 20243 "}"); 20244 verifyFormat("auto lambda = []() {\n" 20245 " int a = 2\n" 20246 "#if A\n" 20247 " + 2\n" 20248 "#endif\n" 20249 " ;\n" 20250 "};"); 20251 20252 // Lambdas with complex multiline introducers. 20253 verifyFormat( 20254 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 20255 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n" 20256 " -> ::std::unordered_set<\n" 20257 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 20258 " //\n" 20259 " });"); 20260 20261 FormatStyle DoNotMerge = getLLVMStyle(); 20262 DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 20263 verifyFormat("auto c = []() {\n" 20264 " return b;\n" 20265 "};", 20266 "auto c = []() { return b; };", DoNotMerge); 20267 verifyFormat("auto c = []() {\n" 20268 "};", 20269 " auto c = []() {};", DoNotMerge); 20270 20271 FormatStyle MergeEmptyOnly = getLLVMStyle(); 20272 MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty; 20273 verifyFormat("auto c = []() {\n" 20274 " return b;\n" 20275 "};", 20276 "auto c = []() {\n" 20277 " return b;\n" 20278 " };", 20279 MergeEmptyOnly); 20280 verifyFormat("auto c = []() {};", 20281 "auto c = []() {\n" 20282 "};", 20283 MergeEmptyOnly); 20284 20285 FormatStyle MergeInline = getLLVMStyle(); 20286 MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline; 20287 verifyFormat("auto c = []() {\n" 20288 " return b;\n" 20289 "};", 20290 "auto c = []() { return b; };", MergeInline); 20291 verifyFormat("function([]() { return b; })", "function([]() { return b; })", 20292 MergeInline); 20293 verifyFormat("function([]() { return b; }, a)", 20294 "function([]() { return b; }, a)", MergeInline); 20295 verifyFormat("function(a, []() { return b; })", 20296 "function(a, []() { return b; })", MergeInline); 20297 20298 // Check option "BraceWrapping.BeforeLambdaBody" and different state of 20299 // AllowShortLambdasOnASingleLine 20300 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle(); 20301 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 20302 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 20303 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20304 FormatStyle::ShortLambdaStyle::SLS_None; 20305 verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n" 20306 " []()\n" 20307 " {\n" 20308 " return 17;\n" 20309 " });", 20310 LLVMWithBeforeLambdaBody); 20311 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n" 20312 " []()\n" 20313 " {\n" 20314 " });", 20315 LLVMWithBeforeLambdaBody); 20316 verifyFormat("auto fct_SLS_None = []()\n" 20317 "{\n" 20318 " return 17;\n" 20319 "};", 20320 LLVMWithBeforeLambdaBody); 20321 verifyFormat("TwoNestedLambdas_SLS_None(\n" 20322 " []()\n" 20323 " {\n" 20324 " return Call(\n" 20325 " []()\n" 20326 " {\n" 20327 " return 17;\n" 20328 " });\n" 20329 " });", 20330 LLVMWithBeforeLambdaBody); 20331 verifyFormat("void Fct() {\n" 20332 " return {[]()\n" 20333 " {\n" 20334 " return 17;\n" 20335 " }};\n" 20336 "}", 20337 LLVMWithBeforeLambdaBody); 20338 20339 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20340 FormatStyle::ShortLambdaStyle::SLS_Empty; 20341 verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n" 20342 " []()\n" 20343 " {\n" 20344 " return 17;\n" 20345 " });", 20346 LLVMWithBeforeLambdaBody); 20347 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});", 20348 LLVMWithBeforeLambdaBody); 20349 verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL" 20350 "ongFunctionName_SLS_Empty(\n" 20351 " []() {});", 20352 LLVMWithBeforeLambdaBody); 20353 verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n" 20354 " []()\n" 20355 " {\n" 20356 " return 17;\n" 20357 " });", 20358 LLVMWithBeforeLambdaBody); 20359 verifyFormat("auto fct_SLS_Empty = []()\n" 20360 "{\n" 20361 " return 17;\n" 20362 "};", 20363 LLVMWithBeforeLambdaBody); 20364 verifyFormat("TwoNestedLambdas_SLS_Empty(\n" 20365 " []()\n" 20366 " {\n" 20367 " return Call([]() {});\n" 20368 " });", 20369 LLVMWithBeforeLambdaBody); 20370 verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n" 20371 " []()\n" 20372 " {\n" 20373 " return Call([]() {});\n" 20374 " });", 20375 LLVMWithBeforeLambdaBody); 20376 verifyFormat( 20377 "FctWithLongLineInLambda_SLS_Empty(\n" 20378 " []()\n" 20379 " {\n" 20380 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 20381 " AndShouldNotBeConsiderAsInline,\n" 20382 " LambdaBodyMustBeBreak);\n" 20383 " });", 20384 LLVMWithBeforeLambdaBody); 20385 20386 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20387 FormatStyle::ShortLambdaStyle::SLS_Inline; 20388 verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });", 20389 LLVMWithBeforeLambdaBody); 20390 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});", 20391 LLVMWithBeforeLambdaBody); 20392 verifyFormat("auto fct_SLS_Inline = []()\n" 20393 "{\n" 20394 " return 17;\n" 20395 "};", 20396 LLVMWithBeforeLambdaBody); 20397 verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return " 20398 "17; }); });", 20399 LLVMWithBeforeLambdaBody); 20400 verifyFormat( 20401 "FctWithLongLineInLambda_SLS_Inline(\n" 20402 " []()\n" 20403 " {\n" 20404 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 20405 " AndShouldNotBeConsiderAsInline,\n" 20406 " LambdaBodyMustBeBreak);\n" 20407 " });", 20408 LLVMWithBeforeLambdaBody); 20409 verifyFormat("FctWithMultipleParams_SLS_Inline(" 20410 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n" 20411 " []() { return 17; });", 20412 LLVMWithBeforeLambdaBody); 20413 verifyFormat( 20414 "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });", 20415 LLVMWithBeforeLambdaBody); 20416 20417 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20418 FormatStyle::ShortLambdaStyle::SLS_All; 20419 verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });", 20420 LLVMWithBeforeLambdaBody); 20421 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});", 20422 LLVMWithBeforeLambdaBody); 20423 verifyFormat("auto fct_SLS_All = []() { return 17; };", 20424 LLVMWithBeforeLambdaBody); 20425 verifyFormat("FctWithOneParam_SLS_All(\n" 20426 " []()\n" 20427 " {\n" 20428 " // A cool function...\n" 20429 " return 43;\n" 20430 " });", 20431 LLVMWithBeforeLambdaBody); 20432 verifyFormat("FctWithMultipleParams_SLS_All(" 20433 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n" 20434 " []() { return 17; });", 20435 LLVMWithBeforeLambdaBody); 20436 verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });", 20437 LLVMWithBeforeLambdaBody); 20438 verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });", 20439 LLVMWithBeforeLambdaBody); 20440 verifyFormat( 20441 "FctWithLongLineInLambda_SLS_All(\n" 20442 " []()\n" 20443 " {\n" 20444 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 20445 " AndShouldNotBeConsiderAsInline,\n" 20446 " LambdaBodyMustBeBreak);\n" 20447 " });", 20448 LLVMWithBeforeLambdaBody); 20449 verifyFormat( 20450 "auto fct_SLS_All = []()\n" 20451 "{\n" 20452 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 20453 " AndShouldNotBeConsiderAsInline,\n" 20454 " LambdaBodyMustBeBreak);\n" 20455 "};", 20456 LLVMWithBeforeLambdaBody); 20457 LLVMWithBeforeLambdaBody.BinPackParameters = false; 20458 verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);", 20459 LLVMWithBeforeLambdaBody); 20460 verifyFormat( 20461 "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n" 20462 " FirstParam,\n" 20463 " SecondParam,\n" 20464 " ThirdParam,\n" 20465 " FourthParam);", 20466 LLVMWithBeforeLambdaBody); 20467 verifyFormat("FctWithLongLineInLambda_SLS_All(\n" 20468 " []() { return " 20469 "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n" 20470 " FirstParam,\n" 20471 " SecondParam,\n" 20472 " ThirdParam,\n" 20473 " FourthParam);", 20474 LLVMWithBeforeLambdaBody); 20475 verifyFormat( 20476 "FctWithLongLineInLambda_SLS_All(FirstParam,\n" 20477 " SecondParam,\n" 20478 " ThirdParam,\n" 20479 " FourthParam,\n" 20480 " []() { return SomeValueNotSoLong; });", 20481 LLVMWithBeforeLambdaBody); 20482 verifyFormat("FctWithLongLineInLambda_SLS_All(\n" 20483 " []()\n" 20484 " {\n" 20485 " return " 20486 "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB" 20487 "eConsiderAsInline;\n" 20488 " });", 20489 LLVMWithBeforeLambdaBody); 20490 verifyFormat( 20491 "FctWithLongLineInLambda_SLS_All(\n" 20492 " []()\n" 20493 " {\n" 20494 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 20495 " AndShouldNotBeConsiderAsInline,\n" 20496 " LambdaBodyMustBeBreak);\n" 20497 " });", 20498 LLVMWithBeforeLambdaBody); 20499 verifyFormat("FctWithTwoParams_SLS_All(\n" 20500 " []()\n" 20501 " {\n" 20502 " // A cool function...\n" 20503 " return 43;\n" 20504 " },\n" 20505 " 87);", 20506 LLVMWithBeforeLambdaBody); 20507 verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);", 20508 LLVMWithBeforeLambdaBody); 20509 verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });", 20510 LLVMWithBeforeLambdaBody); 20511 verifyFormat( 20512 "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });", 20513 LLVMWithBeforeLambdaBody); 20514 verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; " 20515 "}); }, x);", 20516 LLVMWithBeforeLambdaBody); 20517 verifyFormat("TwoNestedLambdas_SLS_All(\n" 20518 " []()\n" 20519 " {\n" 20520 " // A cool function...\n" 20521 " return Call([]() { return 17; });\n" 20522 " });", 20523 LLVMWithBeforeLambdaBody); 20524 verifyFormat("TwoNestedLambdas_SLS_All(\n" 20525 " []()\n" 20526 " {\n" 20527 " return Call(\n" 20528 " []()\n" 20529 " {\n" 20530 " // A cool function...\n" 20531 " return 17;\n" 20532 " });\n" 20533 " });", 20534 LLVMWithBeforeLambdaBody); 20535 20536 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20537 FormatStyle::ShortLambdaStyle::SLS_None; 20538 20539 verifyFormat("auto select = [this]() -> const Library::Object *\n" 20540 "{\n" 20541 " return MyAssignment::SelectFromList(this);\n" 20542 "};\n", 20543 LLVMWithBeforeLambdaBody); 20544 20545 verifyFormat("auto select = [this]() -> const Library::Object &\n" 20546 "{\n" 20547 " return MyAssignment::SelectFromList(this);\n" 20548 "};\n", 20549 LLVMWithBeforeLambdaBody); 20550 20551 verifyFormat("auto select = [this]() -> std::unique_ptr<Object>\n" 20552 "{\n" 20553 " return MyAssignment::SelectFromList(this);\n" 20554 "};\n", 20555 LLVMWithBeforeLambdaBody); 20556 20557 verifyFormat("namespace test {\n" 20558 "class Test {\n" 20559 "public:\n" 20560 " Test() = default;\n" 20561 "};\n" 20562 "} // namespace test", 20563 LLVMWithBeforeLambdaBody); 20564 20565 // Lambdas with different indentation styles. 20566 Style = getLLVMStyleWithColumns(100); 20567 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 20568 " return promise.then(\n" 20569 " [this, &someVariable, someObject = " 20570 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 20571 " return someObject.startAsyncAction().then(\n" 20572 " [this, &someVariable](AsyncActionResult result) " 20573 "mutable { result.processMore(); });\n" 20574 " });\n" 20575 "}\n", 20576 format("SomeResult doSomething(SomeObject promise) {\n" 20577 " return promise.then([this, &someVariable, someObject = " 20578 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 20579 " return someObject.startAsyncAction().then([this, " 20580 "&someVariable](AsyncActionResult result) mutable {\n" 20581 " result.processMore();\n" 20582 " });\n" 20583 " });\n" 20584 "}\n", 20585 Style)); 20586 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope; 20587 verifyFormat("test() {\n" 20588 " ([]() -> {\n" 20589 " int b = 32;\n" 20590 " return 3;\n" 20591 " }).foo();\n" 20592 "}", 20593 Style); 20594 verifyFormat("test() {\n" 20595 " []() -> {\n" 20596 " int b = 32;\n" 20597 " return 3;\n" 20598 " }\n" 20599 "}", 20600 Style); 20601 verifyFormat("std::sort(v.begin(), v.end(),\n" 20602 " [](const auto &someLongArgumentName, const auto " 20603 "&someOtherLongArgumentName) {\n" 20604 " return someLongArgumentName.someMemberVariable < " 20605 "someOtherLongArgumentName.someMemberVariable;\n" 20606 "});", 20607 Style); 20608 verifyFormat("test() {\n" 20609 " (\n" 20610 " []() -> {\n" 20611 " int b = 32;\n" 20612 " return 3;\n" 20613 " },\n" 20614 " foo, bar)\n" 20615 " .foo();\n" 20616 "}", 20617 Style); 20618 verifyFormat("test() {\n" 20619 " ([]() -> {\n" 20620 " int b = 32;\n" 20621 " return 3;\n" 20622 " })\n" 20623 " .foo()\n" 20624 " .bar();\n" 20625 "}", 20626 Style); 20627 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 20628 " return promise.then(\n" 20629 " [this, &someVariable, someObject = " 20630 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 20631 " return someObject.startAsyncAction().then(\n" 20632 " [this, &someVariable](AsyncActionResult result) mutable { " 20633 "result.processMore(); });\n" 20634 " });\n" 20635 "}\n", 20636 format("SomeResult doSomething(SomeObject promise) {\n" 20637 " return promise.then([this, &someVariable, someObject = " 20638 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 20639 " return someObject.startAsyncAction().then([this, " 20640 "&someVariable](AsyncActionResult result) mutable {\n" 20641 " result.processMore();\n" 20642 " });\n" 20643 " });\n" 20644 "}\n", 20645 Style)); 20646 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 20647 " return promise.then([this, &someVariable] {\n" 20648 " return someObject.startAsyncAction().then(\n" 20649 " [this, &someVariable](AsyncActionResult result) mutable { " 20650 "result.processMore(); });\n" 20651 " });\n" 20652 "}\n", 20653 format("SomeResult doSomething(SomeObject promise) {\n" 20654 " return promise.then([this, &someVariable] {\n" 20655 " return someObject.startAsyncAction().then([this, " 20656 "&someVariable](AsyncActionResult result) mutable {\n" 20657 " result.processMore();\n" 20658 " });\n" 20659 " });\n" 20660 "}\n", 20661 Style)); 20662 Style = getGoogleStyle(); 20663 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope; 20664 EXPECT_EQ("#define A \\\n" 20665 " [] { \\\n" 20666 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 20667 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 20668 " }", 20669 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 20670 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 20671 Style)); 20672 // TODO: The current formatting has a minor issue that's not worth fixing 20673 // right now whereby the closing brace is indented relative to the signature 20674 // instead of being aligned. This only happens with macros. 20675 } 20676 20677 TEST_F(FormatTest, LambdaWithLineComments) { 20678 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle(); 20679 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 20680 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 20681 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20682 FormatStyle::ShortLambdaStyle::SLS_All; 20683 20684 verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody); 20685 verifyFormat("auto k = []() // comment\n" 20686 "{ return; }", 20687 LLVMWithBeforeLambdaBody); 20688 verifyFormat("auto k = []() /* comment */ { return; }", 20689 LLVMWithBeforeLambdaBody); 20690 verifyFormat("auto k = []() /* comment */ /* comment */ { return; }", 20691 LLVMWithBeforeLambdaBody); 20692 verifyFormat("auto k = []() // X\n" 20693 "{ return; }", 20694 LLVMWithBeforeLambdaBody); 20695 verifyFormat( 20696 "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n" 20697 "{ return; }", 20698 LLVMWithBeforeLambdaBody); 20699 } 20700 20701 TEST_F(FormatTest, EmptyLinesInLambdas) { 20702 verifyFormat("auto lambda = []() {\n" 20703 " x(); //\n" 20704 "};", 20705 "auto lambda = []() {\n" 20706 "\n" 20707 " x(); //\n" 20708 "\n" 20709 "};"); 20710 } 20711 20712 TEST_F(FormatTest, FormatsBlocks) { 20713 FormatStyle ShortBlocks = getLLVMStyle(); 20714 ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 20715 verifyFormat("int (^Block)(int, int);", ShortBlocks); 20716 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 20717 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 20718 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 20719 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 20720 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 20721 20722 verifyFormat("foo(^{ bar(); });", ShortBlocks); 20723 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 20724 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 20725 20726 verifyFormat("[operation setCompletionBlock:^{\n" 20727 " [self onOperationDone];\n" 20728 "}];"); 20729 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 20730 " [self onOperationDone];\n" 20731 "}]};"); 20732 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 20733 " f();\n" 20734 "}];"); 20735 verifyFormat("int a = [operation block:^int(int *i) {\n" 20736 " return 1;\n" 20737 "}];"); 20738 verifyFormat("[myObject doSomethingWith:arg1\n" 20739 " aaa:^int(int *a) {\n" 20740 " return 1;\n" 20741 " }\n" 20742 " bbb:f(a * bbbbbbbb)];"); 20743 20744 verifyFormat("[operation setCompletionBlock:^{\n" 20745 " [self.delegate newDataAvailable];\n" 20746 "}];", 20747 getLLVMStyleWithColumns(60)); 20748 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 20749 " NSString *path = [self sessionFilePath];\n" 20750 " if (path) {\n" 20751 " // ...\n" 20752 " }\n" 20753 "});"); 20754 verifyFormat("[[SessionService sharedService]\n" 20755 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 20756 " if (window) {\n" 20757 " [self windowDidLoad:window];\n" 20758 " } else {\n" 20759 " [self errorLoadingWindow];\n" 20760 " }\n" 20761 " }];"); 20762 verifyFormat("void (^largeBlock)(void) = ^{\n" 20763 " // ...\n" 20764 "};\n", 20765 getLLVMStyleWithColumns(40)); 20766 verifyFormat("[[SessionService sharedService]\n" 20767 " loadWindowWithCompletionBlock: //\n" 20768 " ^(SessionWindow *window) {\n" 20769 " if (window) {\n" 20770 " [self windowDidLoad:window];\n" 20771 " } else {\n" 20772 " [self errorLoadingWindow];\n" 20773 " }\n" 20774 " }];", 20775 getLLVMStyleWithColumns(60)); 20776 verifyFormat("[myObject doSomethingWith:arg1\n" 20777 " firstBlock:^(Foo *a) {\n" 20778 " // ...\n" 20779 " int i;\n" 20780 " }\n" 20781 " secondBlock:^(Bar *b) {\n" 20782 " // ...\n" 20783 " int i;\n" 20784 " }\n" 20785 " thirdBlock:^Foo(Bar *b) {\n" 20786 " // ...\n" 20787 " int i;\n" 20788 " }];"); 20789 verifyFormat("[myObject doSomethingWith:arg1\n" 20790 " firstBlock:-1\n" 20791 " secondBlock:^(Bar *b) {\n" 20792 " // ...\n" 20793 " int i;\n" 20794 " }];"); 20795 20796 verifyFormat("f(^{\n" 20797 " @autoreleasepool {\n" 20798 " if (a) {\n" 20799 " g();\n" 20800 " }\n" 20801 " }\n" 20802 "});"); 20803 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 20804 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" 20805 "};"); 20806 20807 FormatStyle FourIndent = getLLVMStyle(); 20808 FourIndent.ObjCBlockIndentWidth = 4; 20809 verifyFormat("[operation setCompletionBlock:^{\n" 20810 " [self onOperationDone];\n" 20811 "}];", 20812 FourIndent); 20813 } 20814 20815 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 20816 FormatStyle ZeroColumn = getLLVMStyle(); 20817 ZeroColumn.ColumnLimit = 0; 20818 20819 verifyFormat("[[SessionService sharedService] " 20820 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 20821 " if (window) {\n" 20822 " [self windowDidLoad:window];\n" 20823 " } else {\n" 20824 " [self errorLoadingWindow];\n" 20825 " }\n" 20826 "}];", 20827 ZeroColumn); 20828 EXPECT_EQ("[[SessionService sharedService]\n" 20829 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 20830 " if (window) {\n" 20831 " [self windowDidLoad:window];\n" 20832 " } else {\n" 20833 " [self errorLoadingWindow];\n" 20834 " }\n" 20835 " }];", 20836 format("[[SessionService sharedService]\n" 20837 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 20838 " if (window) {\n" 20839 " [self windowDidLoad:window];\n" 20840 " } else {\n" 20841 " [self errorLoadingWindow];\n" 20842 " }\n" 20843 "}];", 20844 ZeroColumn)); 20845 verifyFormat("[myObject doSomethingWith:arg1\n" 20846 " firstBlock:^(Foo *a) {\n" 20847 " // ...\n" 20848 " int i;\n" 20849 " }\n" 20850 " secondBlock:^(Bar *b) {\n" 20851 " // ...\n" 20852 " int i;\n" 20853 " }\n" 20854 " thirdBlock:^Foo(Bar *b) {\n" 20855 " // ...\n" 20856 " int i;\n" 20857 " }];", 20858 ZeroColumn); 20859 verifyFormat("f(^{\n" 20860 " @autoreleasepool {\n" 20861 " if (a) {\n" 20862 " g();\n" 20863 " }\n" 20864 " }\n" 20865 "});", 20866 ZeroColumn); 20867 verifyFormat("void (^largeBlock)(void) = ^{\n" 20868 " // ...\n" 20869 "};", 20870 ZeroColumn); 20871 20872 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 20873 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 20874 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 20875 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 20876 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 20877 " int i;\n" 20878 "};", 20879 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 20880 } 20881 20882 TEST_F(FormatTest, SupportsCRLF) { 20883 EXPECT_EQ("int a;\r\n" 20884 "int b;\r\n" 20885 "int c;\r\n", 20886 format("int a;\r\n" 20887 " int b;\r\n" 20888 " int c;\r\n", 20889 getLLVMStyle())); 20890 EXPECT_EQ("int a;\r\n" 20891 "int b;\r\n" 20892 "int c;\r\n", 20893 format("int a;\r\n" 20894 " int b;\n" 20895 " int c;\r\n", 20896 getLLVMStyle())); 20897 EXPECT_EQ("int a;\n" 20898 "int b;\n" 20899 "int c;\n", 20900 format("int a;\r\n" 20901 " int b;\n" 20902 " int c;\n", 20903 getLLVMStyle())); 20904 EXPECT_EQ("\"aaaaaaa \"\r\n" 20905 "\"bbbbbbb\";\r\n", 20906 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 20907 EXPECT_EQ("#define A \\\r\n" 20908 " b; \\\r\n" 20909 " c; \\\r\n" 20910 " d;\r\n", 20911 format("#define A \\\r\n" 20912 " b; \\\r\n" 20913 " c; d; \r\n", 20914 getGoogleStyle())); 20915 20916 EXPECT_EQ("/*\r\n" 20917 "multi line block comments\r\n" 20918 "should not introduce\r\n" 20919 "an extra carriage return\r\n" 20920 "*/\r\n", 20921 format("/*\r\n" 20922 "multi line block comments\r\n" 20923 "should not introduce\r\n" 20924 "an extra carriage return\r\n" 20925 "*/\r\n")); 20926 EXPECT_EQ("/*\r\n" 20927 "\r\n" 20928 "*/", 20929 format("/*\r\n" 20930 " \r\r\r\n" 20931 "*/")); 20932 20933 FormatStyle style = getLLVMStyle(); 20934 20935 style.DeriveLineEnding = true; 20936 style.UseCRLF = false; 20937 EXPECT_EQ("union FooBarBazQux {\n" 20938 " int foo;\n" 20939 " int bar;\n" 20940 " int baz;\n" 20941 "};", 20942 format("union FooBarBazQux {\r\n" 20943 " int foo;\n" 20944 " int bar;\r\n" 20945 " int baz;\n" 20946 "};", 20947 style)); 20948 style.UseCRLF = true; 20949 EXPECT_EQ("union FooBarBazQux {\r\n" 20950 " int foo;\r\n" 20951 " int bar;\r\n" 20952 " int baz;\r\n" 20953 "};", 20954 format("union FooBarBazQux {\r\n" 20955 " int foo;\n" 20956 " int bar;\r\n" 20957 " int baz;\n" 20958 "};", 20959 style)); 20960 20961 style.DeriveLineEnding = false; 20962 style.UseCRLF = false; 20963 EXPECT_EQ("union FooBarBazQux {\n" 20964 " int foo;\n" 20965 " int bar;\n" 20966 " int baz;\n" 20967 " int qux;\n" 20968 "};", 20969 format("union FooBarBazQux {\r\n" 20970 " int foo;\n" 20971 " int bar;\r\n" 20972 " int baz;\n" 20973 " int qux;\r\n" 20974 "};", 20975 style)); 20976 style.UseCRLF = true; 20977 EXPECT_EQ("union FooBarBazQux {\r\n" 20978 " int foo;\r\n" 20979 " int bar;\r\n" 20980 " int baz;\r\n" 20981 " int qux;\r\n" 20982 "};", 20983 format("union FooBarBazQux {\r\n" 20984 " int foo;\n" 20985 " int bar;\r\n" 20986 " int baz;\n" 20987 " int qux;\n" 20988 "};", 20989 style)); 20990 20991 style.DeriveLineEnding = true; 20992 style.UseCRLF = false; 20993 EXPECT_EQ("union FooBarBazQux {\r\n" 20994 " int foo;\r\n" 20995 " int bar;\r\n" 20996 " int baz;\r\n" 20997 " int qux;\r\n" 20998 "};", 20999 format("union FooBarBazQux {\r\n" 21000 " int foo;\n" 21001 " int bar;\r\n" 21002 " int baz;\n" 21003 " int qux;\r\n" 21004 "};", 21005 style)); 21006 style.UseCRLF = true; 21007 EXPECT_EQ("union FooBarBazQux {\n" 21008 " int foo;\n" 21009 " int bar;\n" 21010 " int baz;\n" 21011 " int qux;\n" 21012 "};", 21013 format("union FooBarBazQux {\r\n" 21014 " int foo;\n" 21015 " int bar;\r\n" 21016 " int baz;\n" 21017 " int qux;\n" 21018 "};", 21019 style)); 21020 } 21021 21022 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 21023 verifyFormat("MY_CLASS(C) {\n" 21024 " int i;\n" 21025 " int j;\n" 21026 "};"); 21027 } 21028 21029 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 21030 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 21031 TwoIndent.ContinuationIndentWidth = 2; 21032 21033 EXPECT_EQ("int i =\n" 21034 " longFunction(\n" 21035 " arg);", 21036 format("int i = longFunction(arg);", TwoIndent)); 21037 21038 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 21039 SixIndent.ContinuationIndentWidth = 6; 21040 21041 EXPECT_EQ("int i =\n" 21042 " longFunction(\n" 21043 " arg);", 21044 format("int i = longFunction(arg);", SixIndent)); 21045 } 21046 21047 TEST_F(FormatTest, WrappedClosingParenthesisIndent) { 21048 FormatStyle Style = getLLVMStyle(); 21049 verifyFormat("int Foo::getter(\n" 21050 " //\n" 21051 ") const {\n" 21052 " return foo;\n" 21053 "}", 21054 Style); 21055 verifyFormat("void Foo::setter(\n" 21056 " //\n" 21057 ") {\n" 21058 " foo = 1;\n" 21059 "}", 21060 Style); 21061 } 21062 21063 TEST_F(FormatTest, SpacesInAngles) { 21064 FormatStyle Spaces = getLLVMStyle(); 21065 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 21066 21067 verifyFormat("vector< ::std::string > x1;", Spaces); 21068 verifyFormat("Foo< int, Bar > x2;", Spaces); 21069 verifyFormat("Foo< ::int, ::Bar > x3;", Spaces); 21070 21071 verifyFormat("static_cast< int >(arg);", Spaces); 21072 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 21073 verifyFormat("f< int, float >();", Spaces); 21074 verifyFormat("template <> g() {}", Spaces); 21075 verifyFormat("template < std::vector< int > > f() {}", Spaces); 21076 verifyFormat("std::function< void(int, int) > fct;", Spaces); 21077 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 21078 Spaces); 21079 21080 Spaces.Standard = FormatStyle::LS_Cpp03; 21081 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 21082 verifyFormat("A< A< int > >();", Spaces); 21083 21084 Spaces.SpacesInAngles = FormatStyle::SIAS_Never; 21085 verifyFormat("A<A<int> >();", Spaces); 21086 21087 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave; 21088 verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;", 21089 Spaces); 21090 verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;", 21091 Spaces); 21092 21093 verifyFormat("A<A<int> >();", Spaces); 21094 verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces); 21095 verifyFormat("A< A< int > >();", Spaces); 21096 21097 Spaces.Standard = FormatStyle::LS_Cpp11; 21098 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 21099 verifyFormat("A< A< int > >();", Spaces); 21100 21101 Spaces.SpacesInAngles = FormatStyle::SIAS_Never; 21102 verifyFormat("vector<::std::string> x4;", Spaces); 21103 verifyFormat("vector<int> x5;", Spaces); 21104 verifyFormat("Foo<int, Bar> x6;", Spaces); 21105 verifyFormat("Foo<::int, ::Bar> x7;", Spaces); 21106 21107 verifyFormat("A<A<int>>();", Spaces); 21108 21109 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave; 21110 verifyFormat("vector<::std::string> x4;", Spaces); 21111 verifyFormat("vector< ::std::string > x4;", Spaces); 21112 verifyFormat("vector<int> x5;", Spaces); 21113 verifyFormat("vector< int > x5;", Spaces); 21114 verifyFormat("Foo<int, Bar> x6;", Spaces); 21115 verifyFormat("Foo< int, Bar > x6;", Spaces); 21116 verifyFormat("Foo<::int, ::Bar> x7;", Spaces); 21117 verifyFormat("Foo< ::int, ::Bar > x7;", Spaces); 21118 21119 verifyFormat("A<A<int>>();", Spaces); 21120 verifyFormat("A< A< int > >();", Spaces); 21121 verifyFormat("A<A<int > >();", Spaces); 21122 verifyFormat("A< A< int>>();", Spaces); 21123 } 21124 21125 TEST_F(FormatTest, SpaceAfterTemplateKeyword) { 21126 FormatStyle Style = getLLVMStyle(); 21127 Style.SpaceAfterTemplateKeyword = false; 21128 verifyFormat("template<int> void foo();", Style); 21129 } 21130 21131 TEST_F(FormatTest, TripleAngleBrackets) { 21132 verifyFormat("f<<<1, 1>>>();"); 21133 verifyFormat("f<<<1, 1, 1, s>>>();"); 21134 verifyFormat("f<<<a, b, c, d>>>();"); 21135 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 21136 verifyFormat("f<param><<<1, 1>>>();"); 21137 verifyFormat("f<1><<<1, 1>>>();"); 21138 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 21139 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 21140 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 21141 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n" 21142 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();"); 21143 } 21144 21145 TEST_F(FormatTest, MergeLessLessAtEnd) { 21146 verifyFormat("<<"); 21147 EXPECT_EQ("< < <", format("\\\n<<<")); 21148 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 21149 "aaallvm::outs() <<"); 21150 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 21151 "aaaallvm::outs()\n <<"); 21152 } 21153 21154 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 21155 std::string code = "#if A\n" 21156 "#if B\n" 21157 "a.\n" 21158 "#endif\n" 21159 " a = 1;\n" 21160 "#else\n" 21161 "#endif\n" 21162 "#if C\n" 21163 "#else\n" 21164 "#endif\n"; 21165 EXPECT_EQ(code, format(code)); 21166 } 21167 21168 TEST_F(FormatTest, HandleConflictMarkers) { 21169 // Git/SVN conflict markers. 21170 EXPECT_EQ("int a;\n" 21171 "void f() {\n" 21172 " callme(some(parameter1,\n" 21173 "<<<<<<< text by the vcs\n" 21174 " parameter2),\n" 21175 "||||||| text by the vcs\n" 21176 " parameter2),\n" 21177 " parameter3,\n" 21178 "======= text by the vcs\n" 21179 " parameter2, parameter3),\n" 21180 ">>>>>>> text by the vcs\n" 21181 " otherparameter);\n", 21182 format("int a;\n" 21183 "void f() {\n" 21184 " callme(some(parameter1,\n" 21185 "<<<<<<< text by the vcs\n" 21186 " parameter2),\n" 21187 "||||||| text by the vcs\n" 21188 " parameter2),\n" 21189 " parameter3,\n" 21190 "======= text by the vcs\n" 21191 " parameter2,\n" 21192 " parameter3),\n" 21193 ">>>>>>> text by the vcs\n" 21194 " otherparameter);\n")); 21195 21196 // Perforce markers. 21197 EXPECT_EQ("void f() {\n" 21198 " function(\n" 21199 ">>>> text by the vcs\n" 21200 " parameter,\n" 21201 "==== text by the vcs\n" 21202 " parameter,\n" 21203 "==== text by the vcs\n" 21204 " parameter,\n" 21205 "<<<< text by the vcs\n" 21206 " parameter);\n", 21207 format("void f() {\n" 21208 " function(\n" 21209 ">>>> text by the vcs\n" 21210 " parameter,\n" 21211 "==== text by the vcs\n" 21212 " parameter,\n" 21213 "==== text by the vcs\n" 21214 " parameter,\n" 21215 "<<<< text by the vcs\n" 21216 " parameter);\n")); 21217 21218 EXPECT_EQ("<<<<<<<\n" 21219 "|||||||\n" 21220 "=======\n" 21221 ">>>>>>>", 21222 format("<<<<<<<\n" 21223 "|||||||\n" 21224 "=======\n" 21225 ">>>>>>>")); 21226 21227 EXPECT_EQ("<<<<<<<\n" 21228 "|||||||\n" 21229 "int i;\n" 21230 "=======\n" 21231 ">>>>>>>", 21232 format("<<<<<<<\n" 21233 "|||||||\n" 21234 "int i;\n" 21235 "=======\n" 21236 ">>>>>>>")); 21237 21238 // FIXME: Handle parsing of macros around conflict markers correctly: 21239 EXPECT_EQ("#define Macro \\\n" 21240 "<<<<<<<\n" 21241 "Something \\\n" 21242 "|||||||\n" 21243 "Else \\\n" 21244 "=======\n" 21245 "Other \\\n" 21246 ">>>>>>>\n" 21247 " End int i;\n", 21248 format("#define Macro \\\n" 21249 "<<<<<<<\n" 21250 " Something \\\n" 21251 "|||||||\n" 21252 " Else \\\n" 21253 "=======\n" 21254 " Other \\\n" 21255 ">>>>>>>\n" 21256 " End\n" 21257 "int i;\n")); 21258 21259 verifyFormat(R"(==== 21260 #ifdef A 21261 a 21262 #else 21263 b 21264 #endif 21265 )"); 21266 } 21267 21268 TEST_F(FormatTest, DisableRegions) { 21269 EXPECT_EQ("int i;\n" 21270 "// clang-format off\n" 21271 " int j;\n" 21272 "// clang-format on\n" 21273 "int k;", 21274 format(" int i;\n" 21275 " // clang-format off\n" 21276 " int j;\n" 21277 " // clang-format on\n" 21278 " int k;")); 21279 EXPECT_EQ("int i;\n" 21280 "/* clang-format off */\n" 21281 " int j;\n" 21282 "/* clang-format on */\n" 21283 "int k;", 21284 format(" int i;\n" 21285 " /* clang-format off */\n" 21286 " int j;\n" 21287 " /* clang-format on */\n" 21288 " int k;")); 21289 21290 // Don't reflow comments within disabled regions. 21291 EXPECT_EQ("// clang-format off\n" 21292 "// long long long long long long line\n" 21293 "/* clang-format on */\n" 21294 "/* long long long\n" 21295 " * long long long\n" 21296 " * line */\n" 21297 "int i;\n" 21298 "/* clang-format off */\n" 21299 "/* long long long long long long line */\n", 21300 format("// clang-format off\n" 21301 "// long long long long long long line\n" 21302 "/* clang-format on */\n" 21303 "/* long long long long long long line */\n" 21304 "int i;\n" 21305 "/* clang-format off */\n" 21306 "/* long long long long long long line */\n", 21307 getLLVMStyleWithColumns(20))); 21308 } 21309 21310 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 21311 format("? ) ="); 21312 verifyNoCrash("#define a\\\n /**/}"); 21313 } 21314 21315 TEST_F(FormatTest, FormatsTableGenCode) { 21316 FormatStyle Style = getLLVMStyle(); 21317 Style.Language = FormatStyle::LK_TableGen; 21318 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 21319 } 21320 21321 TEST_F(FormatTest, ArrayOfTemplates) { 21322 EXPECT_EQ("auto a = new unique_ptr<int>[10];", 21323 format("auto a = new unique_ptr<int > [ 10];")); 21324 21325 FormatStyle Spaces = getLLVMStyle(); 21326 Spaces.SpacesInSquareBrackets = true; 21327 EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];", 21328 format("auto a = new unique_ptr<int > [10];", Spaces)); 21329 } 21330 21331 TEST_F(FormatTest, ArrayAsTemplateType) { 21332 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;", 21333 format("auto a = unique_ptr < Foo < Bar>[ 10]> ;")); 21334 21335 FormatStyle Spaces = getLLVMStyle(); 21336 Spaces.SpacesInSquareBrackets = true; 21337 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;", 21338 format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces)); 21339 } 21340 21341 TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); } 21342 21343 TEST(FormatStyle, GetStyleWithEmptyFileName) { 21344 llvm::vfs::InMemoryFileSystem FS; 21345 auto Style1 = getStyle("file", "", "Google", "", &FS); 21346 ASSERT_TRUE((bool)Style1); 21347 ASSERT_EQ(*Style1, getGoogleStyle()); 21348 } 21349 21350 TEST(FormatStyle, GetStyleOfFile) { 21351 llvm::vfs::InMemoryFileSystem FS; 21352 // Test 1: format file in the same directory. 21353 ASSERT_TRUE( 21354 FS.addFile("/a/.clang-format", 0, 21355 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 21356 ASSERT_TRUE( 21357 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 21358 auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS); 21359 ASSERT_TRUE((bool)Style1); 21360 ASSERT_EQ(*Style1, getLLVMStyle()); 21361 21362 // Test 2.1: fallback to default. 21363 ASSERT_TRUE( 21364 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 21365 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS); 21366 ASSERT_TRUE((bool)Style2); 21367 ASSERT_EQ(*Style2, getMozillaStyle()); 21368 21369 // Test 2.2: no format on 'none' fallback style. 21370 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 21371 ASSERT_TRUE((bool)Style2); 21372 ASSERT_EQ(*Style2, getNoStyle()); 21373 21374 // Test 2.3: format if config is found with no based style while fallback is 21375 // 'none'. 21376 ASSERT_TRUE(FS.addFile("/b/.clang-format", 0, 21377 llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2"))); 21378 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 21379 ASSERT_TRUE((bool)Style2); 21380 ASSERT_EQ(*Style2, getLLVMStyle()); 21381 21382 // Test 2.4: format if yaml with no based style, while fallback is 'none'. 21383 Style2 = getStyle("{}", "a.h", "none", "", &FS); 21384 ASSERT_TRUE((bool)Style2); 21385 ASSERT_EQ(*Style2, getLLVMStyle()); 21386 21387 // Test 3: format file in parent directory. 21388 ASSERT_TRUE( 21389 FS.addFile("/c/.clang-format", 0, 21390 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 21391 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, 21392 llvm::MemoryBuffer::getMemBuffer("int i;"))); 21393 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS); 21394 ASSERT_TRUE((bool)Style3); 21395 ASSERT_EQ(*Style3, getGoogleStyle()); 21396 21397 // Test 4: error on invalid fallback style 21398 auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS); 21399 ASSERT_FALSE((bool)Style4); 21400 llvm::consumeError(Style4.takeError()); 21401 21402 // Test 5: error on invalid yaml on command line 21403 auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS); 21404 ASSERT_FALSE((bool)Style5); 21405 llvm::consumeError(Style5.takeError()); 21406 21407 // Test 6: error on invalid style 21408 auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS); 21409 ASSERT_FALSE((bool)Style6); 21410 llvm::consumeError(Style6.takeError()); 21411 21412 // Test 7: found config file, error on parsing it 21413 ASSERT_TRUE( 21414 FS.addFile("/d/.clang-format", 0, 21415 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n" 21416 "InvalidKey: InvalidValue"))); 21417 ASSERT_TRUE( 21418 FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 21419 auto Style7a = getStyle("file", "/d/.clang-format", "LLVM", "", &FS); 21420 ASSERT_FALSE((bool)Style7a); 21421 llvm::consumeError(Style7a.takeError()); 21422 21423 auto Style7b = getStyle("file", "/d/.clang-format", "LLVM", "", &FS, true); 21424 ASSERT_TRUE((bool)Style7b); 21425 21426 // Test 8: inferred per-language defaults apply. 21427 auto StyleTd = getStyle("file", "x.td", "llvm", "", &FS); 21428 ASSERT_TRUE((bool)StyleTd); 21429 ASSERT_EQ(*StyleTd, getLLVMStyle(FormatStyle::LK_TableGen)); 21430 21431 // Test 9.1: overwriting a file style, when parent no file exists with no 21432 // fallback style 21433 ASSERT_TRUE(FS.addFile( 21434 "/e/sub/.clang-format", 0, 21435 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: InheritParentConfig\n" 21436 "ColumnLimit: 20"))); 21437 ASSERT_TRUE(FS.addFile("/e/sub/code.cpp", 0, 21438 llvm::MemoryBuffer::getMemBuffer("int i;"))); 21439 auto Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS); 21440 ASSERT_TRUE(static_cast<bool>(Style9)); 21441 ASSERT_EQ(*Style9, [] { 21442 auto Style = getNoStyle(); 21443 Style.ColumnLimit = 20; 21444 return Style; 21445 }()); 21446 21447 // Test 9.2: with LLVM fallback style 21448 Style9 = getStyle("file", "/e/sub/code.cpp", "LLVM", "", &FS); 21449 ASSERT_TRUE(static_cast<bool>(Style9)); 21450 ASSERT_EQ(*Style9, [] { 21451 auto Style = getLLVMStyle(); 21452 Style.ColumnLimit = 20; 21453 return Style; 21454 }()); 21455 21456 // Test 9.3: with a parent file 21457 ASSERT_TRUE( 21458 FS.addFile("/e/.clang-format", 0, 21459 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google\n" 21460 "UseTab: Always"))); 21461 Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS); 21462 ASSERT_TRUE(static_cast<bool>(Style9)); 21463 ASSERT_EQ(*Style9, [] { 21464 auto Style = getGoogleStyle(); 21465 Style.ColumnLimit = 20; 21466 Style.UseTab = FormatStyle::UT_Always; 21467 return Style; 21468 }()); 21469 21470 // Test 9.4: propagate more than one level 21471 ASSERT_TRUE(FS.addFile("/e/sub/sub/code.cpp", 0, 21472 llvm::MemoryBuffer::getMemBuffer("int i;"))); 21473 ASSERT_TRUE(FS.addFile("/e/sub/sub/.clang-format", 0, 21474 llvm::MemoryBuffer::getMemBuffer( 21475 "BasedOnStyle: InheritParentConfig\n" 21476 "WhitespaceSensitiveMacros: ['FOO', 'BAR']"))); 21477 std::vector<std::string> NonDefaultWhiteSpaceMacros{"FOO", "BAR"}; 21478 21479 const auto SubSubStyle = [&NonDefaultWhiteSpaceMacros] { 21480 auto Style = getGoogleStyle(); 21481 Style.ColumnLimit = 20; 21482 Style.UseTab = FormatStyle::UT_Always; 21483 Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros; 21484 return Style; 21485 }(); 21486 21487 ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros); 21488 Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS); 21489 ASSERT_TRUE(static_cast<bool>(Style9)); 21490 ASSERT_EQ(*Style9, SubSubStyle); 21491 21492 // Test 9.5: use InheritParentConfig as style name 21493 Style9 = 21494 getStyle("inheritparentconfig", "/e/sub/sub/code.cpp", "none", "", &FS); 21495 ASSERT_TRUE(static_cast<bool>(Style9)); 21496 ASSERT_EQ(*Style9, SubSubStyle); 21497 21498 // Test 9.6: use command line style with inheritance 21499 Style9 = getStyle("{BasedOnStyle: InheritParentConfig}", "/e/sub/code.cpp", 21500 "none", "", &FS); 21501 ASSERT_TRUE(static_cast<bool>(Style9)); 21502 ASSERT_EQ(*Style9, SubSubStyle); 21503 21504 // Test 9.7: use command line style with inheritance and own config 21505 Style9 = getStyle("{BasedOnStyle: InheritParentConfig, " 21506 "WhitespaceSensitiveMacros: ['FOO', 'BAR']}", 21507 "/e/sub/code.cpp", "none", "", &FS); 21508 ASSERT_TRUE(static_cast<bool>(Style9)); 21509 ASSERT_EQ(*Style9, SubSubStyle); 21510 21511 // Test 9.8: use inheritance from a file without BasedOnStyle 21512 ASSERT_TRUE(FS.addFile("/e/withoutbase/.clang-format", 0, 21513 llvm::MemoryBuffer::getMemBuffer("ColumnLimit: 123"))); 21514 ASSERT_TRUE( 21515 FS.addFile("/e/withoutbase/sub/.clang-format", 0, 21516 llvm::MemoryBuffer::getMemBuffer( 21517 "BasedOnStyle: InheritParentConfig\nIndentWidth: 7"))); 21518 // Make sure we do not use the fallback style 21519 Style9 = getStyle("file", "/e/withoutbase/code.cpp", "google", "", &FS); 21520 ASSERT_TRUE(static_cast<bool>(Style9)); 21521 ASSERT_EQ(*Style9, [] { 21522 auto Style = getLLVMStyle(); 21523 Style.ColumnLimit = 123; 21524 return Style; 21525 }()); 21526 21527 Style9 = getStyle("file", "/e/withoutbase/sub/code.cpp", "google", "", &FS); 21528 ASSERT_TRUE(static_cast<bool>(Style9)); 21529 ASSERT_EQ(*Style9, [] { 21530 auto Style = getLLVMStyle(); 21531 Style.ColumnLimit = 123; 21532 Style.IndentWidth = 7; 21533 return Style; 21534 }()); 21535 } 21536 21537 TEST_F(ReplacementTest, FormatCodeAfterReplacements) { 21538 // Column limit is 20. 21539 std::string Code = "Type *a =\n" 21540 " new Type();\n" 21541 "g(iiiii, 0, jjjjj,\n" 21542 " 0, kkkkk, 0, mm);\n" 21543 "int bad = format ;"; 21544 std::string Expected = "auto a = new Type();\n" 21545 "g(iiiii, nullptr,\n" 21546 " jjjjj, nullptr,\n" 21547 " kkkkk, nullptr,\n" 21548 " mm);\n" 21549 "int bad = format ;"; 21550 FileID ID = Context.createInMemoryFile("format.cpp", Code); 21551 tooling::Replacements Replaces = toReplacements( 21552 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6, 21553 "auto "), 21554 tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1, 21555 "nullptr"), 21556 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1, 21557 "nullptr"), 21558 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1, 21559 "nullptr")}); 21560 21561 format::FormatStyle Style = format::getLLVMStyle(); 21562 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. 21563 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 21564 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 21565 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 21566 auto Result = applyAllReplacements(Code, *FormattedReplaces); 21567 EXPECT_TRUE(static_cast<bool>(Result)); 21568 EXPECT_EQ(Expected, *Result); 21569 } 21570 21571 TEST_F(ReplacementTest, SortIncludesAfterReplacement) { 21572 std::string Code = "#include \"a.h\"\n" 21573 "#include \"c.h\"\n" 21574 "\n" 21575 "int main() {\n" 21576 " return 0;\n" 21577 "}"; 21578 std::string Expected = "#include \"a.h\"\n" 21579 "#include \"b.h\"\n" 21580 "#include \"c.h\"\n" 21581 "\n" 21582 "int main() {\n" 21583 " return 0;\n" 21584 "}"; 21585 FileID ID = Context.createInMemoryFile("fix.cpp", Code); 21586 tooling::Replacements Replaces = toReplacements( 21587 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0, 21588 "#include \"b.h\"\n")}); 21589 21590 format::FormatStyle Style = format::getLLVMStyle(); 21591 Style.SortIncludes = FormatStyle::SI_CaseSensitive; 21592 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 21593 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 21594 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 21595 auto Result = applyAllReplacements(Code, *FormattedReplaces); 21596 EXPECT_TRUE(static_cast<bool>(Result)); 21597 EXPECT_EQ(Expected, *Result); 21598 } 21599 21600 TEST_F(FormatTest, FormatSortsUsingDeclarations) { 21601 EXPECT_EQ("using std::cin;\n" 21602 "using std::cout;", 21603 format("using std::cout;\n" 21604 "using std::cin;", 21605 getGoogleStyle())); 21606 } 21607 21608 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) { 21609 format::FormatStyle Style = format::getLLVMStyle(); 21610 Style.Standard = FormatStyle::LS_Cpp03; 21611 // cpp03 recognize this string as identifier u8 and literal character 'a' 21612 EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style)); 21613 } 21614 21615 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) { 21616 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers 21617 // all modes, including C++11, C++14 and C++17 21618 EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';")); 21619 } 21620 21621 TEST_F(FormatTest, DoNotFormatLikelyXml) { 21622 EXPECT_EQ("<!-- ;> -->", format("<!-- ;> -->", getGoogleStyle())); 21623 EXPECT_EQ(" <!-- >; -->", format(" <!-- >; -->", getGoogleStyle())); 21624 } 21625 21626 TEST_F(FormatTest, StructuredBindings) { 21627 // Structured bindings is a C++17 feature. 21628 // all modes, including C++11, C++14 and C++17 21629 verifyFormat("auto [a, b] = f();"); 21630 EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();")); 21631 EXPECT_EQ("const auto [a, b] = f();", format("const auto[a, b] = f();")); 21632 EXPECT_EQ("auto const [a, b] = f();", format("auto const[a, b] = f();")); 21633 EXPECT_EQ("auto const volatile [a, b] = f();", 21634 format("auto const volatile[a, b] = f();")); 21635 EXPECT_EQ("auto [a, b, c] = f();", format("auto [ a , b,c ] = f();")); 21636 EXPECT_EQ("auto &[a, b, c] = f();", 21637 format("auto &[ a , b,c ] = f();")); 21638 EXPECT_EQ("auto &&[a, b, c] = f();", 21639 format("auto &&[ a , b,c ] = f();")); 21640 EXPECT_EQ("auto const &[a, b] = f();", format("auto const&[a, b] = f();")); 21641 EXPECT_EQ("auto const volatile &&[a, b] = f();", 21642 format("auto const volatile &&[a, b] = f();")); 21643 EXPECT_EQ("auto const &&[a, b] = f();", 21644 format("auto const && [a, b] = f();")); 21645 EXPECT_EQ("const auto &[a, b] = f();", 21646 format("const auto & [a, b] = f();")); 21647 EXPECT_EQ("const auto volatile &&[a, b] = f();", 21648 format("const auto volatile &&[a, b] = f();")); 21649 EXPECT_EQ("volatile const auto &&[a, b] = f();", 21650 format("volatile const auto &&[a, b] = f();")); 21651 EXPECT_EQ("const auto &&[a, b] = f();", 21652 format("const auto && [a, b] = f();")); 21653 21654 // Make sure we don't mistake structured bindings for lambdas. 21655 FormatStyle PointerMiddle = getLLVMStyle(); 21656 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 21657 verifyFormat("auto [a1, b]{A * i};", getGoogleStyle()); 21658 verifyFormat("auto [a2, b]{A * i};", getLLVMStyle()); 21659 verifyFormat("auto [a3, b]{A * i};", PointerMiddle); 21660 verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle()); 21661 verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle()); 21662 verifyFormat("auto const [a3, b]{A * i};", PointerMiddle); 21663 verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle()); 21664 verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle()); 21665 verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle); 21666 verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle()); 21667 verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle()); 21668 verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle); 21669 21670 EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}", 21671 format("for (const auto && [a, b] : some_range) {\n}")); 21672 EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}", 21673 format("for (const auto & [a, b] : some_range) {\n}")); 21674 EXPECT_EQ("for (const auto [a, b] : some_range) {\n}", 21675 format("for (const auto[a, b] : some_range) {\n}")); 21676 EXPECT_EQ("auto [x, y](expr);", format("auto[x,y] (expr);")); 21677 EXPECT_EQ("auto &[x, y](expr);", format("auto & [x,y] (expr);")); 21678 EXPECT_EQ("auto &&[x, y](expr);", format("auto && [x,y] (expr);")); 21679 EXPECT_EQ("auto const &[x, y](expr);", 21680 format("auto const & [x,y] (expr);")); 21681 EXPECT_EQ("auto const &&[x, y](expr);", 21682 format("auto const && [x,y] (expr);")); 21683 EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y] {expr};")); 21684 EXPECT_EQ("auto const &[x, y]{expr};", 21685 format("auto const & [x,y] {expr};")); 21686 EXPECT_EQ("auto const &&[x, y]{expr};", 21687 format("auto const && [x,y] {expr};")); 21688 21689 format::FormatStyle Spaces = format::getLLVMStyle(); 21690 Spaces.SpacesInSquareBrackets = true; 21691 verifyFormat("auto [ a, b ] = f();", Spaces); 21692 verifyFormat("auto &&[ a, b ] = f();", Spaces); 21693 verifyFormat("auto &[ a, b ] = f();", Spaces); 21694 verifyFormat("auto const &&[ a, b ] = f();", Spaces); 21695 verifyFormat("auto const &[ a, b ] = f();", Spaces); 21696 } 21697 21698 TEST_F(FormatTest, FileAndCode) { 21699 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", "")); 21700 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", "")); 21701 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", "")); 21702 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "")); 21703 EXPECT_EQ(FormatStyle::LK_ObjC, 21704 guessLanguage("foo.h", "@interface Foo\n@end\n")); 21705 EXPECT_EQ( 21706 FormatStyle::LK_ObjC, 21707 guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }")); 21708 EXPECT_EQ(FormatStyle::LK_ObjC, 21709 guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))")); 21710 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;")); 21711 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", "")); 21712 EXPECT_EQ(FormatStyle::LK_ObjC, 21713 guessLanguage("foo", "@interface Foo\n@end\n")); 21714 EXPECT_EQ(FormatStyle::LK_ObjC, 21715 guessLanguage("foo.h", "int DoStuff(CGRect rect);\n")); 21716 EXPECT_EQ( 21717 FormatStyle::LK_ObjC, 21718 guessLanguage("foo.h", 21719 "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n")); 21720 EXPECT_EQ( 21721 FormatStyle::LK_Cpp, 21722 guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;")); 21723 } 21724 21725 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) { 21726 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];")); 21727 EXPECT_EQ(FormatStyle::LK_ObjC, 21728 guessLanguage("foo.h", "array[[calculator getIndex]];")); 21729 EXPECT_EQ(FormatStyle::LK_Cpp, 21730 guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];")); 21731 EXPECT_EQ( 21732 FormatStyle::LK_Cpp, 21733 guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];")); 21734 EXPECT_EQ(FormatStyle::LK_ObjC, 21735 guessLanguage("foo.h", "[[noreturn foo] bar];")); 21736 EXPECT_EQ(FormatStyle::LK_Cpp, 21737 guessLanguage("foo.h", "[[clang::fallthrough]];")); 21738 EXPECT_EQ(FormatStyle::LK_ObjC, 21739 guessLanguage("foo.h", "[[clang:fallthrough] foo];")); 21740 EXPECT_EQ(FormatStyle::LK_Cpp, 21741 guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];")); 21742 EXPECT_EQ(FormatStyle::LK_Cpp, 21743 guessLanguage("foo.h", "[[using clang: fallthrough]];")); 21744 EXPECT_EQ(FormatStyle::LK_ObjC, 21745 guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];")); 21746 EXPECT_EQ(FormatStyle::LK_Cpp, 21747 guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];")); 21748 EXPECT_EQ( 21749 FormatStyle::LK_Cpp, 21750 guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)")); 21751 EXPECT_EQ( 21752 FormatStyle::LK_Cpp, 21753 guessLanguage("foo.h", 21754 "[[clang::callable_when(\"unconsumed\", \"unknown\")]]")); 21755 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]")); 21756 } 21757 21758 TEST_F(FormatTest, GuessLanguageWithCaret) { 21759 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);")); 21760 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);")); 21761 EXPECT_EQ(FormatStyle::LK_ObjC, 21762 guessLanguage("foo.h", "int(^)(char, float);")); 21763 EXPECT_EQ(FormatStyle::LK_ObjC, 21764 guessLanguage("foo.h", "int(^foo)(char, float);")); 21765 EXPECT_EQ(FormatStyle::LK_ObjC, 21766 guessLanguage("foo.h", "int(^foo[10])(char, float);")); 21767 EXPECT_EQ(FormatStyle::LK_ObjC, 21768 guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);")); 21769 EXPECT_EQ( 21770 FormatStyle::LK_ObjC, 21771 guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);")); 21772 } 21773 21774 TEST_F(FormatTest, GuessLanguageWithPragmas) { 21775 EXPECT_EQ(FormatStyle::LK_Cpp, 21776 guessLanguage("foo.h", "__pragma(warning(disable:))")); 21777 EXPECT_EQ(FormatStyle::LK_Cpp, 21778 guessLanguage("foo.h", "#pragma(warning(disable:))")); 21779 EXPECT_EQ(FormatStyle::LK_Cpp, 21780 guessLanguage("foo.h", "_Pragma(warning(disable:))")); 21781 } 21782 21783 TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) { 21784 // ASM symbolic names are identifiers that must be surrounded by [] without 21785 // space in between: 21786 // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands 21787 21788 // Example from https://bugs.llvm.org/show_bug.cgi?id=45108. 21789 verifyFormat(R"(// 21790 asm volatile("mrs %x[result], FPCR" : [result] "=r"(result)); 21791 )"); 21792 21793 // A list of several ASM symbolic names. 21794 verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)"); 21795 21796 // ASM symbolic names in inline ASM with inputs and outputs. 21797 verifyFormat(R"(// 21798 asm("cmoveq %1, %2, %[result]" 21799 : [result] "=r"(result) 21800 : "r"(test), "r"(new), "[result]"(old)); 21801 )"); 21802 21803 // ASM symbolic names in inline ASM with no outputs. 21804 verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)"); 21805 } 21806 21807 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) { 21808 EXPECT_EQ(FormatStyle::LK_Cpp, 21809 guessLanguage("foo.h", "void f() {\n" 21810 " asm (\"mov %[e], %[d]\"\n" 21811 " : [d] \"=rm\" (d)\n" 21812 " [e] \"rm\" (*e));\n" 21813 "}")); 21814 EXPECT_EQ(FormatStyle::LK_Cpp, 21815 guessLanguage("foo.h", "void f() {\n" 21816 " _asm (\"mov %[e], %[d]\"\n" 21817 " : [d] \"=rm\" (d)\n" 21818 " [e] \"rm\" (*e));\n" 21819 "}")); 21820 EXPECT_EQ(FormatStyle::LK_Cpp, 21821 guessLanguage("foo.h", "void f() {\n" 21822 " __asm (\"mov %[e], %[d]\"\n" 21823 " : [d] \"=rm\" (d)\n" 21824 " [e] \"rm\" (*e));\n" 21825 "}")); 21826 EXPECT_EQ(FormatStyle::LK_Cpp, 21827 guessLanguage("foo.h", "void f() {\n" 21828 " __asm__ (\"mov %[e], %[d]\"\n" 21829 " : [d] \"=rm\" (d)\n" 21830 " [e] \"rm\" (*e));\n" 21831 "}")); 21832 EXPECT_EQ(FormatStyle::LK_Cpp, 21833 guessLanguage("foo.h", "void f() {\n" 21834 " asm (\"mov %[e], %[d]\"\n" 21835 " : [d] \"=rm\" (d),\n" 21836 " [e] \"rm\" (*e));\n" 21837 "}")); 21838 EXPECT_EQ(FormatStyle::LK_Cpp, 21839 guessLanguage("foo.h", "void f() {\n" 21840 " asm volatile (\"mov %[e], %[d]\"\n" 21841 " : [d] \"=rm\" (d)\n" 21842 " [e] \"rm\" (*e));\n" 21843 "}")); 21844 } 21845 21846 TEST_F(FormatTest, GuessLanguageWithChildLines) { 21847 EXPECT_EQ(FormatStyle::LK_Cpp, 21848 guessLanguage("foo.h", "#define FOO ({ std::string s; })")); 21849 EXPECT_EQ(FormatStyle::LK_ObjC, 21850 guessLanguage("foo.h", "#define FOO ({ NSString *s; })")); 21851 EXPECT_EQ( 21852 FormatStyle::LK_Cpp, 21853 guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })")); 21854 EXPECT_EQ( 21855 FormatStyle::LK_ObjC, 21856 guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })")); 21857 } 21858 21859 TEST_F(FormatTest, TypenameMacros) { 21860 std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"}; 21861 21862 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353 21863 FormatStyle Google = getGoogleStyleWithColumns(0); 21864 Google.TypenameMacros = TypenameMacros; 21865 verifyFormat("struct foo {\n" 21866 " int bar;\n" 21867 " TAILQ_ENTRY(a) bleh;\n" 21868 "};", 21869 Google); 21870 21871 FormatStyle Macros = getLLVMStyle(); 21872 Macros.TypenameMacros = TypenameMacros; 21873 21874 verifyFormat("STACK_OF(int) a;", Macros); 21875 verifyFormat("STACK_OF(int) *a;", Macros); 21876 verifyFormat("STACK_OF(int const *) *a;", Macros); 21877 verifyFormat("STACK_OF(int *const) *a;", Macros); 21878 verifyFormat("STACK_OF(int, string) a;", Macros); 21879 verifyFormat("STACK_OF(LIST(int)) a;", Macros); 21880 verifyFormat("STACK_OF(LIST(int)) a, b;", Macros); 21881 verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros); 21882 verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros); 21883 verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros); 21884 verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros); 21885 21886 Macros.PointerAlignment = FormatStyle::PAS_Left; 21887 verifyFormat("STACK_OF(int)* a;", Macros); 21888 verifyFormat("STACK_OF(int*)* a;", Macros); 21889 verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros); 21890 verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros); 21891 verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros); 21892 } 21893 21894 TEST_F(FormatTest, AtomicQualifier) { 21895 // Check that we treate _Atomic as a type and not a function call 21896 FormatStyle Google = getGoogleStyleWithColumns(0); 21897 verifyFormat("struct foo {\n" 21898 " int a1;\n" 21899 " _Atomic(a) a2;\n" 21900 " _Atomic(_Atomic(int) *const) a3;\n" 21901 "};", 21902 Google); 21903 verifyFormat("_Atomic(uint64_t) a;"); 21904 verifyFormat("_Atomic(uint64_t) *a;"); 21905 verifyFormat("_Atomic(uint64_t const *) *a;"); 21906 verifyFormat("_Atomic(uint64_t *const) *a;"); 21907 verifyFormat("_Atomic(const uint64_t *) *a;"); 21908 verifyFormat("_Atomic(uint64_t) a;"); 21909 verifyFormat("_Atomic(_Atomic(uint64_t)) a;"); 21910 verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;"); 21911 verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}"); 21912 verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);"); 21913 21914 verifyFormat("_Atomic(uint64_t) *s(InitValue);"); 21915 verifyFormat("_Atomic(uint64_t) *s{InitValue};"); 21916 FormatStyle Style = getLLVMStyle(); 21917 Style.PointerAlignment = FormatStyle::PAS_Left; 21918 verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style); 21919 verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style); 21920 verifyFormat("_Atomic(int)* a;", Style); 21921 verifyFormat("_Atomic(int*)* a;", Style); 21922 verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style); 21923 21924 Style.SpacesInCStyleCastParentheses = true; 21925 Style.SpacesInParentheses = false; 21926 verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style); 21927 Style.SpacesInCStyleCastParentheses = false; 21928 Style.SpacesInParentheses = true; 21929 verifyFormat("x = (_Atomic( uint64_t ))*a;", Style); 21930 verifyFormat("x = (_Atomic( uint64_t ))&a;", Style); 21931 } 21932 21933 TEST_F(FormatTest, AmbersandInLamda) { 21934 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899 21935 FormatStyle AlignStyle = getLLVMStyle(); 21936 AlignStyle.PointerAlignment = FormatStyle::PAS_Left; 21937 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle); 21938 AlignStyle.PointerAlignment = FormatStyle::PAS_Right; 21939 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle); 21940 } 21941 21942 TEST_F(FormatTest, SpacesInConditionalStatement) { 21943 FormatStyle Spaces = getLLVMStyle(); 21944 Spaces.IfMacros.clear(); 21945 Spaces.IfMacros.push_back("MYIF"); 21946 Spaces.SpacesInConditionalStatement = true; 21947 verifyFormat("for ( int i = 0; i; i++ )\n continue;", Spaces); 21948 verifyFormat("if ( !a )\n return;", Spaces); 21949 verifyFormat("if ( a )\n return;", Spaces); 21950 verifyFormat("if constexpr ( a )\n return;", Spaces); 21951 verifyFormat("MYIF ( a )\n return;", Spaces); 21952 verifyFormat("MYIF ( a )\n return;\nelse MYIF ( b )\n return;", Spaces); 21953 verifyFormat("MYIF ( a )\n return;\nelse\n return;", Spaces); 21954 verifyFormat("switch ( a )\ncase 1:\n return;", Spaces); 21955 verifyFormat("while ( a )\n return;", Spaces); 21956 verifyFormat("while ( (a && b) )\n return;", Spaces); 21957 verifyFormat("do {\n} while ( 1 != 0 );", Spaces); 21958 verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces); 21959 // Check that space on the left of "::" is inserted as expected at beginning 21960 // of condition. 21961 verifyFormat("while ( ::func() )\n return;", Spaces); 21962 21963 // Check impact of ControlStatementsExceptControlMacros is honored. 21964 Spaces.SpaceBeforeParens = 21965 FormatStyle::SBPO_ControlStatementsExceptControlMacros; 21966 verifyFormat("MYIF( a )\n return;", Spaces); 21967 verifyFormat("MYIF( a )\n return;\nelse MYIF( b )\n return;", Spaces); 21968 verifyFormat("MYIF( a )\n return;\nelse\n return;", Spaces); 21969 } 21970 21971 TEST_F(FormatTest, AlternativeOperators) { 21972 // Test case for ensuring alternate operators are not 21973 // combined with their right most neighbour. 21974 verifyFormat("int a and b;"); 21975 verifyFormat("int a and_eq b;"); 21976 verifyFormat("int a bitand b;"); 21977 verifyFormat("int a bitor b;"); 21978 verifyFormat("int a compl b;"); 21979 verifyFormat("int a not b;"); 21980 verifyFormat("int a not_eq b;"); 21981 verifyFormat("int a or b;"); 21982 verifyFormat("int a xor b;"); 21983 verifyFormat("int a xor_eq b;"); 21984 verifyFormat("return this not_eq bitand other;"); 21985 verifyFormat("bool operator not_eq(const X bitand other)"); 21986 21987 verifyFormat("int a and 5;"); 21988 verifyFormat("int a and_eq 5;"); 21989 verifyFormat("int a bitand 5;"); 21990 verifyFormat("int a bitor 5;"); 21991 verifyFormat("int a compl 5;"); 21992 verifyFormat("int a not 5;"); 21993 verifyFormat("int a not_eq 5;"); 21994 verifyFormat("int a or 5;"); 21995 verifyFormat("int a xor 5;"); 21996 verifyFormat("int a xor_eq 5;"); 21997 21998 verifyFormat("int a compl(5);"); 21999 verifyFormat("int a not(5);"); 22000 22001 /* FIXME handle alternate tokens 22002 * https://en.cppreference.com/w/cpp/language/operator_alternative 22003 // alternative tokens 22004 verifyFormat("compl foo();"); // ~foo(); 22005 verifyFormat("foo() <%%>;"); // foo(); 22006 verifyFormat("void foo() <%%>;"); // void foo(){} 22007 verifyFormat("int a <:1:>;"); // int a[1];[ 22008 verifyFormat("%:define ABC abc"); // #define ABC abc 22009 verifyFormat("%:%:"); // ## 22010 */ 22011 } 22012 22013 TEST_F(FormatTest, STLWhileNotDefineChed) { 22014 verifyFormat("#if defined(while)\n" 22015 "#define while EMIT WARNING C4005\n" 22016 "#endif // while"); 22017 } 22018 22019 TEST_F(FormatTest, OperatorSpacing) { 22020 FormatStyle Style = getLLVMStyle(); 22021 Style.PointerAlignment = FormatStyle::PAS_Right; 22022 verifyFormat("Foo::operator*();", Style); 22023 verifyFormat("Foo::operator void *();", Style); 22024 verifyFormat("Foo::operator void **();", Style); 22025 verifyFormat("Foo::operator void *&();", Style); 22026 verifyFormat("Foo::operator void *&&();", Style); 22027 verifyFormat("Foo::operator void const *();", Style); 22028 verifyFormat("Foo::operator void const **();", Style); 22029 verifyFormat("Foo::operator void const *&();", Style); 22030 verifyFormat("Foo::operator void const *&&();", Style); 22031 verifyFormat("Foo::operator()(void *);", Style); 22032 verifyFormat("Foo::operator*(void *);", Style); 22033 verifyFormat("Foo::operator*();", Style); 22034 verifyFormat("Foo::operator**();", Style); 22035 verifyFormat("Foo::operator&();", Style); 22036 verifyFormat("Foo::operator<int> *();", Style); 22037 verifyFormat("Foo::operator<Foo> *();", Style); 22038 verifyFormat("Foo::operator<int> **();", Style); 22039 verifyFormat("Foo::operator<Foo> **();", Style); 22040 verifyFormat("Foo::operator<int> &();", Style); 22041 verifyFormat("Foo::operator<Foo> &();", Style); 22042 verifyFormat("Foo::operator<int> &&();", Style); 22043 verifyFormat("Foo::operator<Foo> &&();", Style); 22044 verifyFormat("Foo::operator<int> *&();", Style); 22045 verifyFormat("Foo::operator<Foo> *&();", Style); 22046 verifyFormat("Foo::operator<int> *&&();", Style); 22047 verifyFormat("Foo::operator<Foo> *&&();", Style); 22048 verifyFormat("operator*(int (*)(), class Foo);", Style); 22049 22050 verifyFormat("Foo::operator&();", Style); 22051 verifyFormat("Foo::operator void &();", Style); 22052 verifyFormat("Foo::operator void const &();", Style); 22053 verifyFormat("Foo::operator()(void &);", Style); 22054 verifyFormat("Foo::operator&(void &);", Style); 22055 verifyFormat("Foo::operator&();", Style); 22056 verifyFormat("operator&(int (&)(), class Foo);", Style); 22057 verifyFormat("operator&&(int (&)(), class Foo);", Style); 22058 22059 verifyFormat("Foo::operator&&();", Style); 22060 verifyFormat("Foo::operator**();", Style); 22061 verifyFormat("Foo::operator void &&();", 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("operator&&(int (&&)(), class Foo);", Style); 22067 verifyFormat("operator const nsTArrayRight<E> &()", Style); 22068 verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()", 22069 Style); 22070 verifyFormat("operator void **()", Style); 22071 verifyFormat("operator const FooRight<Object> &()", Style); 22072 verifyFormat("operator const FooRight<Object> *()", Style); 22073 verifyFormat("operator const FooRight<Object> **()", Style); 22074 verifyFormat("operator const FooRight<Object> *&()", Style); 22075 verifyFormat("operator const FooRight<Object> *&&()", Style); 22076 22077 Style.PointerAlignment = FormatStyle::PAS_Left; 22078 verifyFormat("Foo::operator*();", Style); 22079 verifyFormat("Foo::operator**();", Style); 22080 verifyFormat("Foo::operator void*();", Style); 22081 verifyFormat("Foo::operator void**();", Style); 22082 verifyFormat("Foo::operator void*&();", Style); 22083 verifyFormat("Foo::operator void*&&();", Style); 22084 verifyFormat("Foo::operator void const*();", Style); 22085 verifyFormat("Foo::operator void const**();", Style); 22086 verifyFormat("Foo::operator void const*&();", Style); 22087 verifyFormat("Foo::operator void const*&&();", Style); 22088 verifyFormat("Foo::operator/*comment*/ void*();", Style); 22089 verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style); 22090 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style); 22091 verifyFormat("Foo::operator()(void*);", Style); 22092 verifyFormat("Foo::operator*(void*);", Style); 22093 verifyFormat("Foo::operator*();", Style); 22094 verifyFormat("Foo::operator<int>*();", Style); 22095 verifyFormat("Foo::operator<Foo>*();", Style); 22096 verifyFormat("Foo::operator<int>**();", Style); 22097 verifyFormat("Foo::operator<Foo>**();", Style); 22098 verifyFormat("Foo::operator<Foo>*&();", Style); 22099 verifyFormat("Foo::operator<int>&();", Style); 22100 verifyFormat("Foo::operator<Foo>&();", Style); 22101 verifyFormat("Foo::operator<int>&&();", Style); 22102 verifyFormat("Foo::operator<Foo>&&();", Style); 22103 verifyFormat("Foo::operator<int>*&();", Style); 22104 verifyFormat("Foo::operator<Foo>*&();", Style); 22105 verifyFormat("operator*(int (*)(), class Foo);", Style); 22106 22107 verifyFormat("Foo::operator&();", Style); 22108 verifyFormat("Foo::operator void&();", Style); 22109 verifyFormat("Foo::operator void const&();", Style); 22110 verifyFormat("Foo::operator/*comment*/ void&();", Style); 22111 verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style); 22112 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style); 22113 verifyFormat("Foo::operator()(void&);", Style); 22114 verifyFormat("Foo::operator&(void&);", Style); 22115 verifyFormat("Foo::operator&();", Style); 22116 verifyFormat("operator&(int (&)(), class Foo);", Style); 22117 verifyFormat("operator&(int (&&)(), class Foo);", Style); 22118 verifyFormat("operator&&(int (&&)(), class Foo);", Style); 22119 22120 verifyFormat("Foo::operator&&();", Style); 22121 verifyFormat("Foo::operator void&&();", Style); 22122 verifyFormat("Foo::operator void const&&();", Style); 22123 verifyFormat("Foo::operator/*comment*/ void&&();", Style); 22124 verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style); 22125 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style); 22126 verifyFormat("Foo::operator()(void&&);", Style); 22127 verifyFormat("Foo::operator&&(void&&);", Style); 22128 verifyFormat("Foo::operator&&();", Style); 22129 verifyFormat("operator&&(int (&&)(), class Foo);", Style); 22130 verifyFormat("operator const nsTArrayLeft<E>&()", Style); 22131 verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()", 22132 Style); 22133 verifyFormat("operator void**()", Style); 22134 verifyFormat("operator const FooLeft<Object>&()", Style); 22135 verifyFormat("operator const FooLeft<Object>*()", Style); 22136 verifyFormat("operator const FooLeft<Object>**()", Style); 22137 verifyFormat("operator const FooLeft<Object>*&()", Style); 22138 verifyFormat("operator const FooLeft<Object>*&&()", Style); 22139 22140 // PR45107 22141 verifyFormat("operator Vector<String>&();", Style); 22142 verifyFormat("operator const Vector<String>&();", Style); 22143 verifyFormat("operator foo::Bar*();", Style); 22144 verifyFormat("operator const Foo<X>::Bar<Y>*();", Style); 22145 verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();", 22146 Style); 22147 22148 Style.PointerAlignment = FormatStyle::PAS_Middle; 22149 verifyFormat("Foo::operator*();", Style); 22150 verifyFormat("Foo::operator void *();", Style); 22151 verifyFormat("Foo::operator()(void *);", Style); 22152 verifyFormat("Foo::operator*(void *);", Style); 22153 verifyFormat("Foo::operator*();", Style); 22154 verifyFormat("operator*(int (*)(), class Foo);", Style); 22155 22156 verifyFormat("Foo::operator&();", Style); 22157 verifyFormat("Foo::operator void &();", Style); 22158 verifyFormat("Foo::operator void const &();", Style); 22159 verifyFormat("Foo::operator()(void &);", Style); 22160 verifyFormat("Foo::operator&(void &);", Style); 22161 verifyFormat("Foo::operator&();", Style); 22162 verifyFormat("operator&(int (&)(), class Foo);", Style); 22163 22164 verifyFormat("Foo::operator&&();", Style); 22165 verifyFormat("Foo::operator void &&();", Style); 22166 verifyFormat("Foo::operator void const &&();", Style); 22167 verifyFormat("Foo::operator()(void &&);", Style); 22168 verifyFormat("Foo::operator&&(void &&);", Style); 22169 verifyFormat("Foo::operator&&();", Style); 22170 verifyFormat("operator&&(int (&&)(), class Foo);", Style); 22171 } 22172 22173 TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) { 22174 FormatStyle Style = getLLVMStyle(); 22175 // PR46157 22176 verifyFormat("foo(operator+, -42);", Style); 22177 verifyFormat("foo(operator++, -42);", Style); 22178 verifyFormat("foo(operator--, -42);", Style); 22179 verifyFormat("foo(-42, operator--);", Style); 22180 verifyFormat("foo(-42, operator, );", Style); 22181 verifyFormat("foo(operator, , -42);", Style); 22182 } 22183 22184 TEST_F(FormatTest, WhitespaceSensitiveMacros) { 22185 FormatStyle Style = getLLVMStyle(); 22186 Style.WhitespaceSensitiveMacros.push_back("FOO"); 22187 22188 // Don't use the helpers here, since 'mess up' will change the whitespace 22189 // and these are all whitespace sensitive by definition 22190 EXPECT_EQ("FOO(String-ized&Messy+But(: :Still)=Intentional);", 22191 format("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style)); 22192 EXPECT_EQ( 22193 "FOO(String-ized&Messy+But\\(: :Still)=Intentional);", 22194 format("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style)); 22195 EXPECT_EQ("FOO(String-ized&Messy+But,: :Still=Intentional);", 22196 format("FOO(String-ized&Messy+But,: :Still=Intentional);", Style)); 22197 EXPECT_EQ("FOO(String-ized&Messy+But,: :\n" 22198 " Still=Intentional);", 22199 format("FOO(String-ized&Messy+But,: :\n" 22200 " Still=Intentional);", 22201 Style)); 22202 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 22203 EXPECT_EQ("FOO(String-ized=&Messy+But,: :\n" 22204 " Still=Intentional);", 22205 format("FOO(String-ized=&Messy+But,: :\n" 22206 " Still=Intentional);", 22207 Style)); 22208 22209 Style.ColumnLimit = 21; 22210 EXPECT_EQ("FOO(String-ized&Messy+But: :Still=Intentional);", 22211 format("FOO(String-ized&Messy+But: :Still=Intentional);", Style)); 22212 } 22213 22214 TEST_F(FormatTest, VeryLongNamespaceCommentSplit) { 22215 // These tests are not in NamespaceFixer because that doesn't 22216 // test its interaction with line wrapping 22217 FormatStyle Style = getLLVMStyle(); 22218 Style.ColumnLimit = 80; 22219 verifyFormat("namespace {\n" 22220 "int i;\n" 22221 "int j;\n" 22222 "} // namespace", 22223 Style); 22224 22225 verifyFormat("namespace AAA {\n" 22226 "int i;\n" 22227 "int j;\n" 22228 "} // namespace AAA", 22229 Style); 22230 22231 EXPECT_EQ("namespace Averyveryveryverylongnamespace {\n" 22232 "int i;\n" 22233 "int j;\n" 22234 "} // namespace Averyveryveryverylongnamespace", 22235 format("namespace Averyveryveryverylongnamespace {\n" 22236 "int i;\n" 22237 "int j;\n" 22238 "}", 22239 Style)); 22240 22241 EXPECT_EQ( 22242 "namespace " 22243 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" 22244 " went::mad::now {\n" 22245 "int i;\n" 22246 "int j;\n" 22247 "} // namespace\n" 22248 " // " 22249 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" 22250 "went::mad::now", 22251 format("namespace " 22252 "would::it::save::you::a::lot::of::time::if_::i::" 22253 "just::gave::up::and_::went::mad::now {\n" 22254 "int i;\n" 22255 "int j;\n" 22256 "}", 22257 Style)); 22258 22259 // This used to duplicate the comment again and again on subsequent runs 22260 EXPECT_EQ( 22261 "namespace " 22262 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" 22263 " went::mad::now {\n" 22264 "int i;\n" 22265 "int j;\n" 22266 "} // namespace\n" 22267 " // " 22268 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" 22269 "went::mad::now", 22270 format("namespace " 22271 "would::it::save::you::a::lot::of::time::if_::i::" 22272 "just::gave::up::and_::went::mad::now {\n" 22273 "int i;\n" 22274 "int j;\n" 22275 "} // namespace\n" 22276 " // " 22277 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::" 22278 "and_::went::mad::now", 22279 Style)); 22280 } 22281 22282 TEST_F(FormatTest, LikelyUnlikely) { 22283 FormatStyle Style = getLLVMStyle(); 22284 22285 verifyFormat("if (argc > 5) [[unlikely]] {\n" 22286 " return 29;\n" 22287 "}", 22288 Style); 22289 22290 verifyFormat("if (argc > 5) [[likely]] {\n" 22291 " return 29;\n" 22292 "}", 22293 Style); 22294 22295 verifyFormat("if (argc > 5) [[unlikely]] {\n" 22296 " return 29;\n" 22297 "} else [[likely]] {\n" 22298 " return 42;\n" 22299 "}\n", 22300 Style); 22301 22302 verifyFormat("if (argc > 5) [[unlikely]] {\n" 22303 " return 29;\n" 22304 "} else if (argc > 10) [[likely]] {\n" 22305 " return 99;\n" 22306 "} else {\n" 22307 " return 42;\n" 22308 "}\n", 22309 Style); 22310 22311 verifyFormat("if (argc > 5) [[gnu::unused]] {\n" 22312 " return 29;\n" 22313 "}", 22314 Style); 22315 22316 verifyFormat("if (argc > 5) [[unlikely]]\n" 22317 " return 29;\n", 22318 Style); 22319 verifyFormat("if (argc > 5) [[likely]]\n" 22320 " return 29;\n", 22321 Style); 22322 22323 Style.AttributeMacros.push_back("UNLIKELY"); 22324 Style.AttributeMacros.push_back("LIKELY"); 22325 verifyFormat("if (argc > 5) UNLIKELY\n" 22326 " return 29;\n", 22327 Style); 22328 22329 verifyFormat("if (argc > 5) UNLIKELY {\n" 22330 " return 29;\n" 22331 "}", 22332 Style); 22333 verifyFormat("if (argc > 5) UNLIKELY {\n" 22334 " return 29;\n" 22335 "} else [[likely]] {\n" 22336 " return 42;\n" 22337 "}\n", 22338 Style); 22339 verifyFormat("if (argc > 5) UNLIKELY {\n" 22340 " return 29;\n" 22341 "} else LIKELY {\n" 22342 " return 42;\n" 22343 "}\n", 22344 Style); 22345 verifyFormat("if (argc > 5) [[unlikely]] {\n" 22346 " return 29;\n" 22347 "} else LIKELY {\n" 22348 " return 42;\n" 22349 "}\n", 22350 Style); 22351 } 22352 22353 TEST_F(FormatTest, PenaltyIndentedWhitespace) { 22354 verifyFormat("Constructor()\n" 22355 " : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 22356 " aaaa(aaaaaaaaaaaaaaaaaa, " 22357 "aaaaaaaaaaaaaaaaaat))"); 22358 verifyFormat("Constructor()\n" 22359 " : aaaaaaaaaaaaa(aaaaaa), " 22360 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)"); 22361 22362 FormatStyle StyleWithWhitespacePenalty = getLLVMStyle(); 22363 StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5; 22364 verifyFormat("Constructor()\n" 22365 " : aaaaaa(aaaaaa),\n" 22366 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 22367 " aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))", 22368 StyleWithWhitespacePenalty); 22369 verifyFormat("Constructor()\n" 22370 " : aaaaaaaaaaaaa(aaaaaa), " 22371 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)", 22372 StyleWithWhitespacePenalty); 22373 } 22374 22375 TEST_F(FormatTest, LLVMDefaultStyle) { 22376 FormatStyle Style = getLLVMStyle(); 22377 verifyFormat("extern \"C\" {\n" 22378 "int foo();\n" 22379 "}", 22380 Style); 22381 } 22382 TEST_F(FormatTest, GNUDefaultStyle) { 22383 FormatStyle Style = getGNUStyle(); 22384 verifyFormat("extern \"C\"\n" 22385 "{\n" 22386 " int foo ();\n" 22387 "}", 22388 Style); 22389 } 22390 TEST_F(FormatTest, MozillaDefaultStyle) { 22391 FormatStyle Style = getMozillaStyle(); 22392 verifyFormat("extern \"C\"\n" 22393 "{\n" 22394 " int foo();\n" 22395 "}", 22396 Style); 22397 } 22398 TEST_F(FormatTest, GoogleDefaultStyle) { 22399 FormatStyle Style = getGoogleStyle(); 22400 verifyFormat("extern \"C\" {\n" 22401 "int foo();\n" 22402 "}", 22403 Style); 22404 } 22405 TEST_F(FormatTest, ChromiumDefaultStyle) { 22406 FormatStyle Style = getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp); 22407 verifyFormat("extern \"C\" {\n" 22408 "int foo();\n" 22409 "}", 22410 Style); 22411 } 22412 TEST_F(FormatTest, MicrosoftDefaultStyle) { 22413 FormatStyle Style = getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp); 22414 verifyFormat("extern \"C\"\n" 22415 "{\n" 22416 " int foo();\n" 22417 "}", 22418 Style); 22419 } 22420 TEST_F(FormatTest, WebKitDefaultStyle) { 22421 FormatStyle Style = getWebKitStyle(); 22422 verifyFormat("extern \"C\" {\n" 22423 "int foo();\n" 22424 "}", 22425 Style); 22426 } 22427 22428 TEST_F(FormatTest, ConceptsAndRequires) { 22429 FormatStyle Style = getLLVMStyle(); 22430 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 22431 22432 verifyFormat("template <typename T>\n" 22433 "concept Hashable = requires(T a) {\n" 22434 " { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n" 22435 "};", 22436 Style); 22437 verifyFormat("template <typename T>\n" 22438 "concept EqualityComparable = requires(T a, T b) {\n" 22439 " { a == b } -> bool;\n" 22440 "};", 22441 Style); 22442 verifyFormat("template <typename T>\n" 22443 "concept EqualityComparable = requires(T a, T b) {\n" 22444 " { a == b } -> bool;\n" 22445 " { a != b } -> bool;\n" 22446 "};", 22447 Style); 22448 verifyFormat("template <typename T>\n" 22449 "concept EqualityComparable = requires(T a, T b) {\n" 22450 " { a == b } -> bool;\n" 22451 " { a != b } -> bool;\n" 22452 "};", 22453 Style); 22454 22455 verifyFormat("template <typename It>\n" 22456 "requires Iterator<It>\n" 22457 "void sort(It begin, It end) {\n" 22458 " //....\n" 22459 "}", 22460 Style); 22461 22462 verifyFormat("template <typename T>\n" 22463 "concept Large = sizeof(T) > 10;", 22464 Style); 22465 22466 verifyFormat("template <typename T, typename U>\n" 22467 "concept FooableWith = requires(T t, U u) {\n" 22468 " typename T::foo_type;\n" 22469 " { t.foo(u) } -> typename T::foo_type;\n" 22470 " t++;\n" 22471 "};\n" 22472 "void doFoo(FooableWith<int> auto t) {\n" 22473 " t.foo(3);\n" 22474 "}", 22475 Style); 22476 verifyFormat("template <typename T>\n" 22477 "concept Context = sizeof(T) == 1;", 22478 Style); 22479 verifyFormat("template <typename T>\n" 22480 "concept Context = is_specialization_of_v<context, T>;", 22481 Style); 22482 verifyFormat("template <typename T>\n" 22483 "concept Node = std::is_object_v<T>;", 22484 Style); 22485 verifyFormat("template <typename T>\n" 22486 "concept Tree = true;", 22487 Style); 22488 22489 verifyFormat("template <typename T> int g(T i) requires Concept1<I> {\n" 22490 " //...\n" 22491 "}", 22492 Style); 22493 22494 verifyFormat( 22495 "template <typename T> int g(T i) requires Concept1<I> && Concept2<I> {\n" 22496 " //...\n" 22497 "}", 22498 Style); 22499 22500 verifyFormat( 22501 "template <typename T> int g(T i) requires Concept1<I> || Concept2<I> {\n" 22502 " //...\n" 22503 "}", 22504 Style); 22505 22506 verifyFormat("template <typename T>\n" 22507 "veryveryvery_long_return_type g(T i) requires Concept1<I> || " 22508 "Concept2<I> {\n" 22509 " //...\n" 22510 "}", 22511 Style); 22512 22513 verifyFormat("template <typename T>\n" 22514 "veryveryvery_long_return_type g(T i) requires Concept1<I> && " 22515 "Concept2<I> {\n" 22516 " //...\n" 22517 "}", 22518 Style); 22519 22520 verifyFormat( 22521 "template <typename T>\n" 22522 "veryveryvery_long_return_type g(T i) requires Concept1 && Concept2 {\n" 22523 " //...\n" 22524 "}", 22525 Style); 22526 22527 verifyFormat( 22528 "template <typename T>\n" 22529 "veryveryvery_long_return_type g(T i) requires Concept1 || Concept2 {\n" 22530 " //...\n" 22531 "}", 22532 Style); 22533 22534 verifyFormat("template <typename It>\n" 22535 "requires Foo<It>() && Bar<It> {\n" 22536 " //....\n" 22537 "}", 22538 Style); 22539 22540 verifyFormat("template <typename It>\n" 22541 "requires Foo<Bar<It>>() && Bar<Foo<It, It>> {\n" 22542 " //....\n" 22543 "}", 22544 Style); 22545 22546 verifyFormat("template <typename It>\n" 22547 "requires Foo<Bar<It, It>>() && Bar<Foo<It, It>> {\n" 22548 " //....\n" 22549 "}", 22550 Style); 22551 22552 verifyFormat( 22553 "template <typename It>\n" 22554 "requires Foo<Bar<It>, Baz<It>>() && Bar<Foo<It>, Baz<It, It>> {\n" 22555 " //....\n" 22556 "}", 22557 Style); 22558 22559 Style.IndentRequires = true; 22560 verifyFormat("template <typename It>\n" 22561 " requires Iterator<It>\n" 22562 "void sort(It begin, It end) {\n" 22563 " //....\n" 22564 "}", 22565 Style); 22566 verifyFormat("template <std::size index_>\n" 22567 " requires(index_ < sizeof...(Children_))\n" 22568 "Tree auto &child() {\n" 22569 " // ...\n" 22570 "}", 22571 Style); 22572 22573 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 22574 verifyFormat("template <typename T>\n" 22575 "concept Hashable = requires (T a) {\n" 22576 " { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n" 22577 "};", 22578 Style); 22579 22580 verifyFormat("template <class T = void>\n" 22581 " requires EqualityComparable<T> || Same<T, void>\n" 22582 "struct equal_to;", 22583 Style); 22584 22585 verifyFormat("template <class T>\n" 22586 " requires requires {\n" 22587 " T{};\n" 22588 " T (int);\n" 22589 " }\n", 22590 Style); 22591 22592 Style.ColumnLimit = 78; 22593 verifyFormat("template <typename T>\n" 22594 "concept Context = Traits<typename T::traits_type> and\n" 22595 " Interface<typename T::interface_type> and\n" 22596 " Request<typename T::request_type> and\n" 22597 " Response<typename T::response_type> and\n" 22598 " ContextExtension<typename T::extension_type> and\n" 22599 " ::std::is_copy_constructable<T> and " 22600 "::std::is_move_constructable<T> and\n" 22601 " requires (T c) {\n" 22602 " { c.response; } -> Response;\n" 22603 "} and requires (T c) {\n" 22604 " { c.request; } -> Request;\n" 22605 "}\n", 22606 Style); 22607 22608 verifyFormat("template <typename T>\n" 22609 "concept Context = Traits<typename T::traits_type> or\n" 22610 " Interface<typename T::interface_type> or\n" 22611 " Request<typename T::request_type> or\n" 22612 " Response<typename T::response_type> or\n" 22613 " ContextExtension<typename T::extension_type> or\n" 22614 " ::std::is_copy_constructable<T> or " 22615 "::std::is_move_constructable<T> or\n" 22616 " requires (T c) {\n" 22617 " { c.response; } -> Response;\n" 22618 "} or requires (T c) {\n" 22619 " { c.request; } -> Request;\n" 22620 "}\n", 22621 Style); 22622 22623 verifyFormat("template <typename T>\n" 22624 "concept Context = Traits<typename T::traits_type> &&\n" 22625 " Interface<typename T::interface_type> &&\n" 22626 " Request<typename T::request_type> &&\n" 22627 " Response<typename T::response_type> &&\n" 22628 " ContextExtension<typename T::extension_type> &&\n" 22629 " ::std::is_copy_constructable<T> && " 22630 "::std::is_move_constructable<T> &&\n" 22631 " requires (T c) {\n" 22632 " { c.response; } -> Response;\n" 22633 "} && requires (T c) {\n" 22634 " { c.request; } -> Request;\n" 22635 "}\n", 22636 Style); 22637 22638 verifyFormat("template <typename T>\nconcept someConcept = Constraint1<T> && " 22639 "Constraint2<T>;"); 22640 22641 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 22642 Style.BraceWrapping.AfterFunction = true; 22643 Style.BraceWrapping.AfterClass = true; 22644 Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 22645 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 22646 verifyFormat("void Foo () requires (std::copyable<T>)\n" 22647 "{\n" 22648 " return\n" 22649 "}\n", 22650 Style); 22651 22652 verifyFormat("void Foo () requires std::copyable<T>\n" 22653 "{\n" 22654 " return\n" 22655 "}\n", 22656 Style); 22657 22658 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 22659 " requires (std::invocable<F, std::invoke_result_t<Args>...>)\n" 22660 "struct constant;", 22661 Style); 22662 22663 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 22664 " requires std::invocable<F, std::invoke_result_t<Args>...>\n" 22665 "struct constant;", 22666 Style); 22667 22668 verifyFormat("template <class T>\n" 22669 "class plane_with_very_very_very_long_name\n" 22670 "{\n" 22671 " constexpr plane_with_very_very_very_long_name () requires " 22672 "std::copyable<T>\n" 22673 " : plane_with_very_very_very_long_name (1)\n" 22674 " {\n" 22675 " }\n" 22676 "}\n", 22677 Style); 22678 22679 verifyFormat("template <class T>\n" 22680 "class plane_with_long_name\n" 22681 "{\n" 22682 " constexpr plane_with_long_name () requires std::copyable<T>\n" 22683 " : plane_with_long_name (1)\n" 22684 " {\n" 22685 " }\n" 22686 "}\n", 22687 Style); 22688 22689 Style.BreakBeforeConceptDeclarations = false; 22690 verifyFormat("template <typename T> concept Tree = true;", Style); 22691 22692 Style.IndentRequires = false; 22693 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 22694 "requires (std::invocable<F, std::invoke_result_t<Args>...>) " 22695 "struct constant;", 22696 Style); 22697 } 22698 22699 TEST_F(FormatTest, StatementAttributeLikeMacros) { 22700 FormatStyle Style = getLLVMStyle(); 22701 StringRef Source = "void Foo::slot() {\n" 22702 " unsigned char MyChar = 'x';\n" 22703 " emit signal(MyChar);\n" 22704 " Q_EMIT signal(MyChar);\n" 22705 "}"; 22706 22707 EXPECT_EQ(Source, format(Source, Style)); 22708 22709 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 22710 EXPECT_EQ("void Foo::slot() {\n" 22711 " unsigned char MyChar = 'x';\n" 22712 " emit signal(MyChar);\n" 22713 " Q_EMIT signal(MyChar);\n" 22714 "}", 22715 format(Source, Style)); 22716 22717 Style.StatementAttributeLikeMacros.push_back("emit"); 22718 EXPECT_EQ(Source, format(Source, Style)); 22719 22720 Style.StatementAttributeLikeMacros = {}; 22721 EXPECT_EQ("void Foo::slot() {\n" 22722 " unsigned char MyChar = 'x';\n" 22723 " emit signal(MyChar);\n" 22724 " Q_EMIT signal(MyChar);\n" 22725 "}", 22726 format(Source, Style)); 22727 } 22728 22729 TEST_F(FormatTest, IndentAccessModifiers) { 22730 FormatStyle Style = getLLVMStyle(); 22731 Style.IndentAccessModifiers = true; 22732 // Members are *two* levels below the record; 22733 // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation. 22734 verifyFormat("class C {\n" 22735 " int i;\n" 22736 "};\n", 22737 Style); 22738 verifyFormat("union C {\n" 22739 " int i;\n" 22740 " unsigned u;\n" 22741 "};\n", 22742 Style); 22743 // Access modifiers should be indented one level below the record. 22744 verifyFormat("class C {\n" 22745 " public:\n" 22746 " int i;\n" 22747 "};\n", 22748 Style); 22749 verifyFormat("struct S {\n" 22750 " private:\n" 22751 " class C {\n" 22752 " int j;\n" 22753 "\n" 22754 " public:\n" 22755 " C();\n" 22756 " };\n" 22757 "\n" 22758 " public:\n" 22759 " int i;\n" 22760 "};\n", 22761 Style); 22762 // Enumerations are not records and should be unaffected. 22763 Style.AllowShortEnumsOnASingleLine = false; 22764 verifyFormat("enum class E {\n" 22765 " A,\n" 22766 " B\n" 22767 "};\n", 22768 Style); 22769 // Test with a different indentation width; 22770 // also proves that the result is Style.AccessModifierOffset agnostic. 22771 Style.IndentWidth = 3; 22772 verifyFormat("class C {\n" 22773 " public:\n" 22774 " int i;\n" 22775 "};\n", 22776 Style); 22777 } 22778 22779 TEST_F(FormatTest, LimitlessStringsAndComments) { 22780 auto Style = getLLVMStyleWithColumns(0); 22781 constexpr StringRef Code = 22782 "/**\n" 22783 " * This is a multiline comment with quite some long lines, at least for " 22784 "the LLVM Style.\n" 22785 " * We will redo this with strings and line comments. Just to check if " 22786 "everything is working.\n" 22787 " */\n" 22788 "bool foo() {\n" 22789 " /* Single line multi line comment. */\n" 22790 " const std::string String = \"This is a multiline string with quite " 22791 "some long lines, at least for the LLVM Style.\"\n" 22792 " \"We already did it with multi line " 22793 "comments, and we will do it with line comments. Just to check if " 22794 "everything is working.\";\n" 22795 " // This is a line comment (block) with quite some long lines, at " 22796 "least for the LLVM Style.\n" 22797 " // We already did this with multi line comments and strings. Just to " 22798 "check if everything is working.\n" 22799 " const std::string SmallString = \"Hello World\";\n" 22800 " // Small line comment\n" 22801 " return String.size() > SmallString.size();\n" 22802 "}"; 22803 EXPECT_EQ(Code, format(Code, Style)); 22804 } 22805 22806 TEST_F(FormatTest, FormatDecayCopy) { 22807 // error cases from unit tests 22808 verifyFormat("foo(auto())"); 22809 verifyFormat("foo(auto{})"); 22810 verifyFormat("foo(auto({}))"); 22811 verifyFormat("foo(auto{{}})"); 22812 22813 verifyFormat("foo(auto(1))"); 22814 verifyFormat("foo(auto{1})"); 22815 verifyFormat("foo(new auto(1))"); 22816 verifyFormat("foo(new auto{1})"); 22817 verifyFormat("decltype(auto(1)) x;"); 22818 verifyFormat("decltype(auto{1}) x;"); 22819 verifyFormat("auto(x);"); 22820 verifyFormat("auto{x};"); 22821 verifyFormat("new auto{x};"); 22822 verifyFormat("auto{x} = y;"); 22823 verifyFormat("auto(x) = y;"); // actually a declaration, but this is clearly 22824 // the user's own fault 22825 verifyFormat("integral auto(x) = y;"); // actually a declaration, but this is 22826 // clearly the user's own fault 22827 verifyFormat("auto(*p)() = f;"); // actually a declaration; TODO FIXME 22828 } 22829 22830 TEST_F(FormatTest, Cpp20ModulesSupport) { 22831 FormatStyle Style = getLLVMStyle(); 22832 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 22833 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 22834 22835 verifyFormat("export import foo;", Style); 22836 verifyFormat("export import foo:bar;", Style); 22837 verifyFormat("export import foo.bar;", Style); 22838 verifyFormat("export import foo.bar:baz;", Style); 22839 verifyFormat("export import :bar;", Style); 22840 verifyFormat("export module foo:bar;", Style); 22841 verifyFormat("export module foo;", Style); 22842 verifyFormat("export module foo.bar;", Style); 22843 verifyFormat("export module foo.bar:baz;", Style); 22844 verifyFormat("export import <string_view>;", Style); 22845 22846 verifyFormat("export type_name var;", Style); 22847 verifyFormat("template <class T> export using A = B<T>;", Style); 22848 verifyFormat("export using A = B;", Style); 22849 verifyFormat("export int func() {\n" 22850 " foo();\n" 22851 "}", 22852 Style); 22853 verifyFormat("export struct {\n" 22854 " int foo;\n" 22855 "};", 22856 Style); 22857 verifyFormat("export {\n" 22858 " int foo;\n" 22859 "};", 22860 Style); 22861 verifyFormat("export export char const *hello() { return \"hello\"; }"); 22862 22863 verifyFormat("import bar;", Style); 22864 verifyFormat("import foo.bar;", Style); 22865 verifyFormat("import foo:bar;", Style); 22866 verifyFormat("import :bar;", Style); 22867 verifyFormat("import <ctime>;", Style); 22868 verifyFormat("import \"header\";", Style); 22869 22870 verifyFormat("module foo;", Style); 22871 verifyFormat("module foo:bar;", Style); 22872 verifyFormat("module foo.bar;", Style); 22873 verifyFormat("module;", Style); 22874 22875 verifyFormat("export namespace hi {\n" 22876 "const char *sayhi();\n" 22877 "}", 22878 Style); 22879 22880 verifyFormat("module :private;", Style); 22881 verifyFormat("import <foo/bar.h>;", Style); 22882 verifyFormat("import foo...bar;", Style); 22883 verifyFormat("import ..........;", Style); 22884 verifyFormat("module foo:private;", Style); 22885 verifyFormat("import a", Style); 22886 verifyFormat("module a", Style); 22887 verifyFormat("export import a", Style); 22888 verifyFormat("export module a", Style); 22889 22890 verifyFormat("import", Style); 22891 verifyFormat("module", Style); 22892 verifyFormat("export", Style); 22893 } 22894 22895 TEST_F(FormatTest, CoroutineForCoawait) { 22896 FormatStyle Style = getLLVMStyle(); 22897 verifyFormat("for co_await (auto x : range())\n ;"); 22898 verifyFormat("for (auto i : arr) {\n" 22899 "}", 22900 Style); 22901 verifyFormat("for co_await (auto i : arr) {\n" 22902 "}", 22903 Style); 22904 verifyFormat("for co_await (auto i : foo(T{})) {\n" 22905 "}", 22906 Style); 22907 } 22908 22909 TEST_F(FormatTest, CoroutineCoAwait) { 22910 verifyFormat("int x = co_await foo();"); 22911 verifyFormat("int x = (co_await foo());"); 22912 verifyFormat("co_await (42);"); 22913 verifyFormat("void operator co_await(int);"); 22914 verifyFormat("void operator co_await(a);"); 22915 verifyFormat("co_await a;"); 22916 verifyFormat("co_await missing_await_resume{};"); 22917 verifyFormat("co_await a; // comment"); 22918 verifyFormat("void test0() { co_await a; }"); 22919 verifyFormat("co_await co_await co_await foo();"); 22920 verifyFormat("co_await foo().bar();"); 22921 verifyFormat("co_await [this]() -> Task { co_return x; }"); 22922 verifyFormat("co_await [this](int a, int b) -> Task { co_return co_await " 22923 "foo(); }(x, y);"); 22924 22925 FormatStyle Style = getLLVMStyle(); 22926 Style.ColumnLimit = 40; 22927 verifyFormat("co_await [this](int a, int b) -> Task {\n" 22928 " co_return co_await foo();\n" 22929 "}(x, y);", 22930 Style); 22931 verifyFormat("co_await;"); 22932 } 22933 22934 TEST_F(FormatTest, CoroutineCoYield) { 22935 verifyFormat("int x = co_yield foo();"); 22936 verifyFormat("int x = (co_yield foo());"); 22937 verifyFormat("co_yield (42);"); 22938 verifyFormat("co_yield {42};"); 22939 verifyFormat("co_yield 42;"); 22940 verifyFormat("co_yield n++;"); 22941 verifyFormat("co_yield ++n;"); 22942 verifyFormat("co_yield;"); 22943 } 22944 22945 TEST_F(FormatTest, CoroutineCoReturn) { 22946 verifyFormat("co_return (42);"); 22947 verifyFormat("co_return;"); 22948 verifyFormat("co_return {};"); 22949 verifyFormat("co_return x;"); 22950 verifyFormat("co_return co_await foo();"); 22951 verifyFormat("co_return co_yield foo();"); 22952 } 22953 22954 TEST_F(FormatTest, EmptyShortBlock) { 22955 auto Style = getLLVMStyle(); 22956 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 22957 22958 verifyFormat("try {\n" 22959 " doA();\n" 22960 "} catch (Exception &e) {\n" 22961 " e.printStackTrace();\n" 22962 "}\n", 22963 Style); 22964 22965 verifyFormat("try {\n" 22966 " doA();\n" 22967 "} catch (Exception &e) {}\n", 22968 Style); 22969 } 22970 22971 } // namespace 22972 } // namespace format 22973 } // namespace clang 22974