1 //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "clang/Format/Format.h" 10 11 #include "../Tooling/ReplacementTest.h" 12 #include "FormatTestUtils.h" 13 14 #include "llvm/Support/Debug.h" 15 #include "llvm/Support/MemoryBuffer.h" 16 #include "gtest/gtest.h" 17 18 #define DEBUG_TYPE "format-test" 19 20 using clang::tooling::ReplacementTest; 21 using clang::tooling::toReplacements; 22 using testing::ScopedTrace; 23 24 namespace clang { 25 namespace format { 26 namespace { 27 28 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); } 29 30 class FormatTest : public ::testing::Test { 31 protected: 32 enum StatusCheck { SC_ExpectComplete, SC_ExpectIncomplete, SC_DoNotCheck }; 33 34 std::string format(llvm::StringRef Code, 35 const FormatStyle &Style = getLLVMStyle(), 36 StatusCheck CheckComplete = SC_ExpectComplete) { 37 LLVM_DEBUG(llvm::errs() << "---\n"); 38 LLVM_DEBUG(llvm::errs() << Code << "\n\n"); 39 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size())); 40 FormattingAttemptStatus Status; 41 tooling::Replacements Replaces = 42 reformat(Style, Code, Ranges, "<stdin>", &Status); 43 if (CheckComplete != SC_DoNotCheck) { 44 bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete; 45 EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete) 46 << Code << "\n\n"; 47 } 48 ReplacementCount = Replaces.size(); 49 auto Result = applyAllReplacements(Code, Replaces); 50 EXPECT_TRUE(static_cast<bool>(Result)); 51 LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n"); 52 return *Result; 53 } 54 55 FormatStyle getStyleWithColumns(FormatStyle Style, unsigned ColumnLimit) { 56 Style.ColumnLimit = ColumnLimit; 57 return Style; 58 } 59 60 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { 61 return getStyleWithColumns(getLLVMStyle(), ColumnLimit); 62 } 63 64 FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) { 65 return getStyleWithColumns(getGoogleStyle(), ColumnLimit); 66 } 67 68 void _verifyFormat(const char *File, int Line, llvm::StringRef Expected, 69 llvm::StringRef Code, 70 const FormatStyle &Style = getLLVMStyle()) { 71 ScopedTrace t(File, Line, ::testing::Message() << Code.str()); 72 EXPECT_EQ(Expected.str(), format(Expected, Style)) 73 << "Expected code is not stable"; 74 EXPECT_EQ(Expected.str(), format(Code, Style)); 75 if (Style.Language == FormatStyle::LK_Cpp) { 76 // Objective-C++ is a superset of C++, so everything checked for C++ 77 // needs to be checked for Objective-C++ as well. 78 FormatStyle ObjCStyle = Style; 79 ObjCStyle.Language = FormatStyle::LK_ObjC; 80 EXPECT_EQ(Expected.str(), format(test::messUp(Code), ObjCStyle)); 81 } 82 } 83 84 void _verifyFormat(const char *File, int Line, llvm::StringRef Code, 85 const FormatStyle &Style = getLLVMStyle()) { 86 _verifyFormat(File, Line, Code, test::messUp(Code), Style); 87 } 88 89 void _verifyIncompleteFormat(const char *File, int Line, llvm::StringRef Code, 90 const FormatStyle &Style = getLLVMStyle()) { 91 ScopedTrace t(File, Line, ::testing::Message() << Code.str()); 92 EXPECT_EQ(Code.str(), 93 format(test::messUp(Code), Style, SC_ExpectIncomplete)); 94 } 95 96 void _verifyIndependentOfContext(const char *File, int Line, 97 llvm::StringRef Text, 98 const FormatStyle &Style = getLLVMStyle()) { 99 _verifyFormat(File, Line, Text, Style); 100 _verifyFormat(File, Line, llvm::Twine("void f() { " + Text + " }").str(), 101 Style); 102 } 103 104 /// \brief Verify that clang-format does not crash on the given input. 105 void verifyNoCrash(llvm::StringRef Code, 106 const FormatStyle &Style = getLLVMStyle()) { 107 format(Code, Style, SC_DoNotCheck); 108 } 109 110 int ReplacementCount; 111 }; 112 113 #define verifyIndependentOfContext(...) \ 114 _verifyIndependentOfContext(__FILE__, __LINE__, __VA_ARGS__) 115 #define verifyIncompleteFormat(...) \ 116 _verifyIncompleteFormat(__FILE__, __LINE__, __VA_ARGS__) 117 #define verifyFormat(...) _verifyFormat(__FILE__, __LINE__, __VA_ARGS__) 118 #define verifyGoogleFormat(Code) verifyFormat(Code, getGoogleStyle()) 119 120 TEST_F(FormatTest, MessUp) { 121 EXPECT_EQ("1 2 3", test::messUp("1 2 3")); 122 EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n")); 123 EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc")); 124 EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc")); 125 EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne")); 126 } 127 128 TEST_F(FormatTest, DefaultLLVMStyleIsCpp) { 129 EXPECT_EQ(FormatStyle::LK_Cpp, getLLVMStyle().Language); 130 } 131 132 TEST_F(FormatTest, LLVMStyleOverride) { 133 EXPECT_EQ(FormatStyle::LK_Proto, 134 getLLVMStyle(FormatStyle::LK_Proto).Language); 135 } 136 137 //===----------------------------------------------------------------------===// 138 // Basic function tests. 139 //===----------------------------------------------------------------------===// 140 141 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) { 142 EXPECT_EQ(";", format(";")); 143 } 144 145 TEST_F(FormatTest, FormatsGlobalStatementsAt0) { 146 EXPECT_EQ("int i;", format(" int i;")); 147 EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;")); 148 EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); 149 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); 150 } 151 152 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { 153 EXPECT_EQ("int i;", format("int\ni;")); 154 } 155 156 TEST_F(FormatTest, FormatsNestedBlockStatements) { 157 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}")); 158 } 159 160 TEST_F(FormatTest, FormatsNestedCall) { 161 verifyFormat("Method(f1, f2(f3));"); 162 verifyFormat("Method(f1(f2, f3()));"); 163 verifyFormat("Method(f1(f2, (f3())));"); 164 } 165 166 TEST_F(FormatTest, NestedNameSpecifiers) { 167 verifyFormat("vector<::Type> v;"); 168 verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())"); 169 verifyFormat("static constexpr bool Bar = decltype(bar())::value;"); 170 verifyFormat("static constexpr bool Bar = typeof(bar())::value;"); 171 verifyFormat("static constexpr bool Bar = __underlying_type(bar())::value;"); 172 verifyFormat("static constexpr bool Bar = _Atomic(bar())::value;"); 173 verifyFormat("bool a = 2 < ::SomeFunction();"); 174 verifyFormat("ALWAYS_INLINE ::std::string getName();"); 175 verifyFormat("some::string getName();"); 176 } 177 178 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) { 179 EXPECT_EQ("if (a) {\n" 180 " f();\n" 181 "}", 182 format("if(a){f();}")); 183 EXPECT_EQ(4, ReplacementCount); 184 EXPECT_EQ("if (a) {\n" 185 " f();\n" 186 "}", 187 format("if (a) {\n" 188 " f();\n" 189 "}")); 190 EXPECT_EQ(0, ReplacementCount); 191 EXPECT_EQ("/*\r\n" 192 "\r\n" 193 "*/\r\n", 194 format("/*\r\n" 195 "\r\n" 196 "*/\r\n")); 197 EXPECT_EQ(0, ReplacementCount); 198 } 199 200 TEST_F(FormatTest, RemovesEmptyLines) { 201 EXPECT_EQ("class C {\n" 202 " int i;\n" 203 "};", 204 format("class C {\n" 205 " int i;\n" 206 "\n" 207 "};")); 208 209 // Don't remove empty lines at the start of namespaces or extern "C" blocks. 210 EXPECT_EQ("namespace N {\n" 211 "\n" 212 "int i;\n" 213 "}", 214 format("namespace N {\n" 215 "\n" 216 "int i;\n" 217 "}", 218 getGoogleStyle())); 219 EXPECT_EQ("/* something */ namespace N {\n" 220 "\n" 221 "int i;\n" 222 "}", 223 format("/* something */ namespace N {\n" 224 "\n" 225 "int i;\n" 226 "}", 227 getGoogleStyle())); 228 EXPECT_EQ("inline namespace N {\n" 229 "\n" 230 "int i;\n" 231 "}", 232 format("inline namespace N {\n" 233 "\n" 234 "int i;\n" 235 "}", 236 getGoogleStyle())); 237 EXPECT_EQ("/* something */ inline namespace N {\n" 238 "\n" 239 "int i;\n" 240 "}", 241 format("/* something */ inline namespace N {\n" 242 "\n" 243 "int i;\n" 244 "}", 245 getGoogleStyle())); 246 EXPECT_EQ("export namespace N {\n" 247 "\n" 248 "int i;\n" 249 "}", 250 format("export namespace N {\n" 251 "\n" 252 "int i;\n" 253 "}", 254 getGoogleStyle())); 255 EXPECT_EQ("extern /**/ \"C\" /**/ {\n" 256 "\n" 257 "int i;\n" 258 "}", 259 format("extern /**/ \"C\" /**/ {\n" 260 "\n" 261 "int i;\n" 262 "}", 263 getGoogleStyle())); 264 265 auto CustomStyle = clang::format::getLLVMStyle(); 266 CustomStyle.BreakBeforeBraces = clang::format::FormatStyle::BS_Custom; 267 CustomStyle.BraceWrapping.AfterNamespace = true; 268 CustomStyle.KeepEmptyLinesAtTheStartOfBlocks = false; 269 EXPECT_EQ("namespace N\n" 270 "{\n" 271 "\n" 272 "int i;\n" 273 "}", 274 format("namespace N\n" 275 "{\n" 276 "\n" 277 "\n" 278 "int i;\n" 279 "}", 280 CustomStyle)); 281 EXPECT_EQ("/* something */ namespace N\n" 282 "{\n" 283 "\n" 284 "int i;\n" 285 "}", 286 format("/* something */ namespace N {\n" 287 "\n" 288 "\n" 289 "int i;\n" 290 "}", 291 CustomStyle)); 292 EXPECT_EQ("inline namespace N\n" 293 "{\n" 294 "\n" 295 "int i;\n" 296 "}", 297 format("inline namespace N\n" 298 "{\n" 299 "\n" 300 "\n" 301 "int i;\n" 302 "}", 303 CustomStyle)); 304 EXPECT_EQ("/* something */ inline namespace N\n" 305 "{\n" 306 "\n" 307 "int i;\n" 308 "}", 309 format("/* something */ inline namespace N\n" 310 "{\n" 311 "\n" 312 "int i;\n" 313 "}", 314 CustomStyle)); 315 EXPECT_EQ("export namespace N\n" 316 "{\n" 317 "\n" 318 "int i;\n" 319 "}", 320 format("export namespace N\n" 321 "{\n" 322 "\n" 323 "int i;\n" 324 "}", 325 CustomStyle)); 326 EXPECT_EQ("namespace a\n" 327 "{\n" 328 "namespace b\n" 329 "{\n" 330 "\n" 331 "class AA {};\n" 332 "\n" 333 "} // namespace b\n" 334 "} // namespace a\n", 335 format("namespace a\n" 336 "{\n" 337 "namespace b\n" 338 "{\n" 339 "\n" 340 "\n" 341 "class AA {};\n" 342 "\n" 343 "\n" 344 "}\n" 345 "}\n", 346 CustomStyle)); 347 EXPECT_EQ("namespace A /* comment */\n" 348 "{\n" 349 "class B {}\n" 350 "} // namespace A", 351 format("namespace A /* comment */ { class B {} }", CustomStyle)); 352 EXPECT_EQ("namespace A\n" 353 "{ /* comment */\n" 354 "class B {}\n" 355 "} // namespace A", 356 format("namespace A {/* comment */ class B {} }", CustomStyle)); 357 EXPECT_EQ("namespace A\n" 358 "{ /* comment */\n" 359 "\n" 360 "class B {}\n" 361 "\n" 362 "" 363 "} // namespace A", 364 format("namespace A { /* comment */\n" 365 "\n" 366 "\n" 367 "class B {}\n" 368 "\n" 369 "\n" 370 "}", 371 CustomStyle)); 372 EXPECT_EQ("namespace A /* comment */\n" 373 "{\n" 374 "\n" 375 "class B {}\n" 376 "\n" 377 "} // namespace A", 378 format("namespace A/* comment */ {\n" 379 "\n" 380 "\n" 381 "class B {}\n" 382 "\n" 383 "\n" 384 "}", 385 CustomStyle)); 386 387 // ...but do keep inlining and removing empty lines for non-block extern "C" 388 // functions. 389 verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle()); 390 EXPECT_EQ("extern \"C\" int f() {\n" 391 " int i = 42;\n" 392 " return i;\n" 393 "}", 394 format("extern \"C\" int f() {\n" 395 "\n" 396 " int i = 42;\n" 397 " return i;\n" 398 "}", 399 getGoogleStyle())); 400 401 // Remove empty lines at the beginning and end of blocks. 402 EXPECT_EQ("void f() {\n" 403 "\n" 404 " if (a) {\n" 405 "\n" 406 " f();\n" 407 " }\n" 408 "}", 409 format("void f() {\n" 410 "\n" 411 " if (a) {\n" 412 "\n" 413 " f();\n" 414 "\n" 415 " }\n" 416 "\n" 417 "}", 418 getLLVMStyle())); 419 EXPECT_EQ("void f() {\n" 420 " if (a) {\n" 421 " f();\n" 422 " }\n" 423 "}", 424 format("void f() {\n" 425 "\n" 426 " if (a) {\n" 427 "\n" 428 " f();\n" 429 "\n" 430 " }\n" 431 "\n" 432 "}", 433 getGoogleStyle())); 434 435 // Don't remove empty lines in more complex control statements. 436 EXPECT_EQ("void f() {\n" 437 " if (a) {\n" 438 " f();\n" 439 "\n" 440 " } else if (b) {\n" 441 " f();\n" 442 " }\n" 443 "}", 444 format("void f() {\n" 445 " if (a) {\n" 446 " f();\n" 447 "\n" 448 " } else if (b) {\n" 449 " f();\n" 450 "\n" 451 " }\n" 452 "\n" 453 "}")); 454 455 // Don't remove empty lines before namespace endings. 456 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 457 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 458 EXPECT_EQ("namespace {\n" 459 "int i;\n" 460 "\n" 461 "}", 462 format("namespace {\n" 463 "int i;\n" 464 "\n" 465 "}", 466 LLVMWithNoNamespaceFix)); 467 EXPECT_EQ("namespace {\n" 468 "int i;\n" 469 "}", 470 format("namespace {\n" 471 "int i;\n" 472 "}", 473 LLVMWithNoNamespaceFix)); 474 EXPECT_EQ("namespace {\n" 475 "int i;\n" 476 "\n" 477 "};", 478 format("namespace {\n" 479 "int i;\n" 480 "\n" 481 "};", 482 LLVMWithNoNamespaceFix)); 483 EXPECT_EQ("namespace {\n" 484 "int i;\n" 485 "};", 486 format("namespace {\n" 487 "int i;\n" 488 "};", 489 LLVMWithNoNamespaceFix)); 490 EXPECT_EQ("namespace {\n" 491 "int i;\n" 492 "\n" 493 "}", 494 format("namespace {\n" 495 "int i;\n" 496 "\n" 497 "}")); 498 EXPECT_EQ("namespace {\n" 499 "int i;\n" 500 "\n" 501 "} // namespace", 502 format("namespace {\n" 503 "int i;\n" 504 "\n" 505 "} // namespace")); 506 507 FormatStyle Style = getLLVMStyle(); 508 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 509 Style.MaxEmptyLinesToKeep = 2; 510 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 511 Style.BraceWrapping.AfterClass = true; 512 Style.BraceWrapping.AfterFunction = true; 513 Style.KeepEmptyLinesAtTheStartOfBlocks = false; 514 515 EXPECT_EQ("class Foo\n" 516 "{\n" 517 " Foo() {}\n" 518 "\n" 519 " void funk() {}\n" 520 "};", 521 format("class Foo\n" 522 "{\n" 523 " Foo()\n" 524 " {\n" 525 " }\n" 526 "\n" 527 " void funk() {}\n" 528 "};", 529 Style)); 530 } 531 532 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) { 533 verifyFormat("x = (a) and (b);"); 534 verifyFormat("x = (a) or (b);"); 535 verifyFormat("x = (a) bitand (b);"); 536 verifyFormat("x = (a) bitor (b);"); 537 verifyFormat("x = (a) not_eq (b);"); 538 verifyFormat("x = (a) and_eq (b);"); 539 verifyFormat("x = (a) or_eq (b);"); 540 verifyFormat("x = (a) xor (b);"); 541 } 542 543 TEST_F(FormatTest, RecognizesUnaryOperatorKeywords) { 544 verifyFormat("x = compl(a);"); 545 verifyFormat("x = not(a);"); 546 verifyFormat("x = bitand(a);"); 547 // Unary operator must not be merged with the next identifier 548 verifyFormat("x = compl a;"); 549 verifyFormat("x = not a;"); 550 verifyFormat("x = bitand a;"); 551 } 552 553 //===----------------------------------------------------------------------===// 554 // Tests for control statements. 555 //===----------------------------------------------------------------------===// 556 557 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) { 558 verifyFormat("if (true)\n f();\ng();"); 559 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); 560 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); 561 verifyFormat("if constexpr (true)\n" 562 " f();\ng();"); 563 verifyFormat("if CONSTEXPR (true)\n" 564 " f();\ng();"); 565 verifyFormat("if constexpr (a)\n" 566 " if constexpr (b)\n" 567 " if constexpr (c)\n" 568 " g();\n" 569 "h();"); 570 verifyFormat("if CONSTEXPR (a)\n" 571 " if CONSTEXPR (b)\n" 572 " if CONSTEXPR (c)\n" 573 " g();\n" 574 "h();"); 575 verifyFormat("if constexpr (a)\n" 576 " if constexpr (b) {\n" 577 " f();\n" 578 " }\n" 579 "g();"); 580 verifyFormat("if CONSTEXPR (a)\n" 581 " if CONSTEXPR (b) {\n" 582 " f();\n" 583 " }\n" 584 "g();"); 585 586 verifyFormat("if (a)\n" 587 " g();"); 588 verifyFormat("if (a) {\n" 589 " g()\n" 590 "};"); 591 verifyFormat("if (a)\n" 592 " g();\n" 593 "else\n" 594 " g();"); 595 verifyFormat("if (a) {\n" 596 " g();\n" 597 "} else\n" 598 " g();"); 599 verifyFormat("if (a)\n" 600 " g();\n" 601 "else {\n" 602 " g();\n" 603 "}"); 604 verifyFormat("if (a) {\n" 605 " g();\n" 606 "} else {\n" 607 " g();\n" 608 "}"); 609 verifyFormat("if (a)\n" 610 " g();\n" 611 "else if (b)\n" 612 " g();\n" 613 "else\n" 614 " g();"); 615 verifyFormat("if (a) {\n" 616 " g();\n" 617 "} else if (b)\n" 618 " g();\n" 619 "else\n" 620 " g();"); 621 verifyFormat("if (a)\n" 622 " g();\n" 623 "else if (b) {\n" 624 " g();\n" 625 "} else\n" 626 " g();"); 627 verifyFormat("if (a)\n" 628 " g();\n" 629 "else if (b)\n" 630 " g();\n" 631 "else {\n" 632 " g();\n" 633 "}"); 634 verifyFormat("if (a)\n" 635 " g();\n" 636 "else if (b) {\n" 637 " g();\n" 638 "} else {\n" 639 " g();\n" 640 "}"); 641 verifyFormat("if (a) {\n" 642 " g();\n" 643 "} else if (b) {\n" 644 " g();\n" 645 "} else {\n" 646 " g();\n" 647 "}"); 648 649 FormatStyle AllowsMergedIf = getLLVMStyle(); 650 AllowsMergedIf.IfMacros.push_back("MYIF"); 651 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left; 652 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 653 FormatStyle::SIS_WithoutElse; 654 verifyFormat("if (a)\n" 655 " // comment\n" 656 " f();", 657 AllowsMergedIf); 658 verifyFormat("{\n" 659 " if (a)\n" 660 " label:\n" 661 " f();\n" 662 "}", 663 AllowsMergedIf); 664 verifyFormat("#define A \\\n" 665 " if (a) \\\n" 666 " label: \\\n" 667 " f()", 668 AllowsMergedIf); 669 verifyFormat("if (a)\n" 670 " ;", 671 AllowsMergedIf); 672 verifyFormat("if (a)\n" 673 " if (b) return;", 674 AllowsMergedIf); 675 676 verifyFormat("if (a) // Can't merge this\n" 677 " f();\n", 678 AllowsMergedIf); 679 verifyFormat("if (a) /* still don't merge */\n" 680 " f();", 681 AllowsMergedIf); 682 verifyFormat("if (a) { // Never merge this\n" 683 " f();\n" 684 "}", 685 AllowsMergedIf); 686 verifyFormat("if (a) { /* Never merge this */\n" 687 " f();\n" 688 "}", 689 AllowsMergedIf); 690 verifyFormat("MYIF (a)\n" 691 " // comment\n" 692 " f();", 693 AllowsMergedIf); 694 verifyFormat("{\n" 695 " MYIF (a)\n" 696 " label:\n" 697 " f();\n" 698 "}", 699 AllowsMergedIf); 700 verifyFormat("#define A \\\n" 701 " MYIF (a) \\\n" 702 " label: \\\n" 703 " f()", 704 AllowsMergedIf); 705 verifyFormat("MYIF (a)\n" 706 " ;", 707 AllowsMergedIf); 708 verifyFormat("MYIF (a)\n" 709 " MYIF (b) return;", 710 AllowsMergedIf); 711 712 verifyFormat("MYIF (a) // Can't merge this\n" 713 " f();\n", 714 AllowsMergedIf); 715 verifyFormat("MYIF (a) /* still don't merge */\n" 716 " f();", 717 AllowsMergedIf); 718 verifyFormat("MYIF (a) { // Never merge this\n" 719 " f();\n" 720 "}", 721 AllowsMergedIf); 722 verifyFormat("MYIF (a) { /* Never merge this */\n" 723 " f();\n" 724 "}", 725 AllowsMergedIf); 726 727 AllowsMergedIf.ColumnLimit = 14; 728 // Where line-lengths matter, a 2-letter synonym that maintains line length. 729 // Not IF to avoid any confusion that IF is somehow special. 730 AllowsMergedIf.IfMacros.push_back("FI"); 731 verifyFormat("if (a) return;", AllowsMergedIf); 732 verifyFormat("if (aaaaaaaaa)\n" 733 " return;", 734 AllowsMergedIf); 735 verifyFormat("FI (a) return;", AllowsMergedIf); 736 verifyFormat("FI (aaaaaaaaa)\n" 737 " return;", 738 AllowsMergedIf); 739 740 AllowsMergedIf.ColumnLimit = 13; 741 verifyFormat("if (a)\n return;", AllowsMergedIf); 742 verifyFormat("FI (a)\n return;", AllowsMergedIf); 743 744 FormatStyle AllowsMergedIfElse = getLLVMStyle(); 745 AllowsMergedIfElse.IfMacros.push_back("MYIF"); 746 AllowsMergedIfElse.AllowShortIfStatementsOnASingleLine = 747 FormatStyle::SIS_AllIfsAndElse; 748 verifyFormat("if (a)\n" 749 " // comment\n" 750 " f();\n" 751 "else\n" 752 " // comment\n" 753 " f();", 754 AllowsMergedIfElse); 755 verifyFormat("{\n" 756 " if (a)\n" 757 " label:\n" 758 " f();\n" 759 " else\n" 760 " label:\n" 761 " f();\n" 762 "}", 763 AllowsMergedIfElse); 764 verifyFormat("if (a)\n" 765 " ;\n" 766 "else\n" 767 " ;", 768 AllowsMergedIfElse); 769 verifyFormat("if (a) {\n" 770 "} else {\n" 771 "}", 772 AllowsMergedIfElse); 773 verifyFormat("if (a) return;\n" 774 "else if (b) return;\n" 775 "else return;", 776 AllowsMergedIfElse); 777 verifyFormat("if (a) {\n" 778 "} else return;", 779 AllowsMergedIfElse); 780 verifyFormat("if (a) {\n" 781 "} else if (b) return;\n" 782 "else return;", 783 AllowsMergedIfElse); 784 verifyFormat("if (a) return;\n" 785 "else if (b) {\n" 786 "} else return;", 787 AllowsMergedIfElse); 788 verifyFormat("if (a)\n" 789 " if (b) return;\n" 790 " else return;", 791 AllowsMergedIfElse); 792 verifyFormat("if constexpr (a)\n" 793 " if constexpr (b) return;\n" 794 " else if constexpr (c) return;\n" 795 " else return;", 796 AllowsMergedIfElse); 797 verifyFormat("MYIF (a)\n" 798 " // comment\n" 799 " f();\n" 800 "else\n" 801 " // comment\n" 802 " f();", 803 AllowsMergedIfElse); 804 verifyFormat("{\n" 805 " MYIF (a)\n" 806 " label:\n" 807 " f();\n" 808 " else\n" 809 " label:\n" 810 " f();\n" 811 "}", 812 AllowsMergedIfElse); 813 verifyFormat("MYIF (a)\n" 814 " ;\n" 815 "else\n" 816 " ;", 817 AllowsMergedIfElse); 818 verifyFormat("MYIF (a) {\n" 819 "} else {\n" 820 "}", 821 AllowsMergedIfElse); 822 verifyFormat("MYIF (a) return;\n" 823 "else MYIF (b) return;\n" 824 "else return;", 825 AllowsMergedIfElse); 826 verifyFormat("MYIF (a) {\n" 827 "} else return;", 828 AllowsMergedIfElse); 829 verifyFormat("MYIF (a) {\n" 830 "} else MYIF (b) return;\n" 831 "else return;", 832 AllowsMergedIfElse); 833 verifyFormat("MYIF (a) return;\n" 834 "else MYIF (b) {\n" 835 "} else return;", 836 AllowsMergedIfElse); 837 verifyFormat("MYIF (a)\n" 838 " MYIF (b) return;\n" 839 " else return;", 840 AllowsMergedIfElse); 841 verifyFormat("MYIF constexpr (a)\n" 842 " MYIF constexpr (b) return;\n" 843 " else MYIF constexpr (c) return;\n" 844 " else return;", 845 AllowsMergedIfElse); 846 } 847 848 TEST_F(FormatTest, FormatIfWithoutCompoundStatementButElseWith) { 849 FormatStyle AllowsMergedIf = getLLVMStyle(); 850 AllowsMergedIf.IfMacros.push_back("MYIF"); 851 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left; 852 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 853 FormatStyle::SIS_WithoutElse; 854 verifyFormat("if (a)\n" 855 " f();\n" 856 "else {\n" 857 " g();\n" 858 "}", 859 AllowsMergedIf); 860 verifyFormat("if (a)\n" 861 " f();\n" 862 "else\n" 863 " g();\n", 864 AllowsMergedIf); 865 866 verifyFormat("if (a) g();", AllowsMergedIf); 867 verifyFormat("if (a) {\n" 868 " g()\n" 869 "};", 870 AllowsMergedIf); 871 verifyFormat("if (a)\n" 872 " g();\n" 873 "else\n" 874 " g();", 875 AllowsMergedIf); 876 verifyFormat("if (a) {\n" 877 " g();\n" 878 "} else\n" 879 " g();", 880 AllowsMergedIf); 881 verifyFormat("if (a)\n" 882 " g();\n" 883 "else {\n" 884 " g();\n" 885 "}", 886 AllowsMergedIf); 887 verifyFormat("if (a) {\n" 888 " g();\n" 889 "} else {\n" 890 " g();\n" 891 "}", 892 AllowsMergedIf); 893 verifyFormat("if (a)\n" 894 " g();\n" 895 "else if (b)\n" 896 " g();\n" 897 "else\n" 898 " g();", 899 AllowsMergedIf); 900 verifyFormat("if (a) {\n" 901 " g();\n" 902 "} else if (b)\n" 903 " g();\n" 904 "else\n" 905 " g();", 906 AllowsMergedIf); 907 verifyFormat("if (a)\n" 908 " g();\n" 909 "else if (b) {\n" 910 " g();\n" 911 "} else\n" 912 " g();", 913 AllowsMergedIf); 914 verifyFormat("if (a)\n" 915 " g();\n" 916 "else if (b)\n" 917 " g();\n" 918 "else {\n" 919 " g();\n" 920 "}", 921 AllowsMergedIf); 922 verifyFormat("if (a)\n" 923 " g();\n" 924 "else if (b) {\n" 925 " g();\n" 926 "} else {\n" 927 " g();\n" 928 "}", 929 AllowsMergedIf); 930 verifyFormat("if (a) {\n" 931 " g();\n" 932 "} else if (b) {\n" 933 " g();\n" 934 "} else {\n" 935 " g();\n" 936 "}", 937 AllowsMergedIf); 938 verifyFormat("MYIF (a)\n" 939 " f();\n" 940 "else {\n" 941 " g();\n" 942 "}", 943 AllowsMergedIf); 944 verifyFormat("MYIF (a)\n" 945 " f();\n" 946 "else\n" 947 " g();\n", 948 AllowsMergedIf); 949 950 verifyFormat("MYIF (a) g();", AllowsMergedIf); 951 verifyFormat("MYIF (a) {\n" 952 " g()\n" 953 "};", 954 AllowsMergedIf); 955 verifyFormat("MYIF (a)\n" 956 " g();\n" 957 "else\n" 958 " g();", 959 AllowsMergedIf); 960 verifyFormat("MYIF (a) {\n" 961 " g();\n" 962 "} else\n" 963 " g();", 964 AllowsMergedIf); 965 verifyFormat("MYIF (a)\n" 966 " g();\n" 967 "else {\n" 968 " g();\n" 969 "}", 970 AllowsMergedIf); 971 verifyFormat("MYIF (a) {\n" 972 " g();\n" 973 "} else {\n" 974 " g();\n" 975 "}", 976 AllowsMergedIf); 977 verifyFormat("MYIF (a)\n" 978 " g();\n" 979 "else MYIF (b)\n" 980 " g();\n" 981 "else\n" 982 " g();", 983 AllowsMergedIf); 984 verifyFormat("MYIF (a)\n" 985 " g();\n" 986 "else if (b)\n" 987 " g();\n" 988 "else\n" 989 " g();", 990 AllowsMergedIf); 991 verifyFormat("MYIF (a) {\n" 992 " g();\n" 993 "} else MYIF (b)\n" 994 " g();\n" 995 "else\n" 996 " g();", 997 AllowsMergedIf); 998 verifyFormat("MYIF (a) {\n" 999 " g();\n" 1000 "} else if (b)\n" 1001 " g();\n" 1002 "else\n" 1003 " g();", 1004 AllowsMergedIf); 1005 verifyFormat("MYIF (a)\n" 1006 " g();\n" 1007 "else MYIF (b) {\n" 1008 " g();\n" 1009 "} else\n" 1010 " g();", 1011 AllowsMergedIf); 1012 verifyFormat("MYIF (a)\n" 1013 " g();\n" 1014 "else if (b) {\n" 1015 " g();\n" 1016 "} else\n" 1017 " g();", 1018 AllowsMergedIf); 1019 verifyFormat("MYIF (a)\n" 1020 " g();\n" 1021 "else MYIF (b)\n" 1022 " g();\n" 1023 "else {\n" 1024 " g();\n" 1025 "}", 1026 AllowsMergedIf); 1027 verifyFormat("MYIF (a)\n" 1028 " g();\n" 1029 "else if (b)\n" 1030 " g();\n" 1031 "else {\n" 1032 " g();\n" 1033 "}", 1034 AllowsMergedIf); 1035 verifyFormat("MYIF (a)\n" 1036 " g();\n" 1037 "else MYIF (b) {\n" 1038 " g();\n" 1039 "} else {\n" 1040 " g();\n" 1041 "}", 1042 AllowsMergedIf); 1043 verifyFormat("MYIF (a)\n" 1044 " g();\n" 1045 "else if (b) {\n" 1046 " g();\n" 1047 "} else {\n" 1048 " g();\n" 1049 "}", 1050 AllowsMergedIf); 1051 verifyFormat("MYIF (a) {\n" 1052 " g();\n" 1053 "} else MYIF (b) {\n" 1054 " g();\n" 1055 "} else {\n" 1056 " g();\n" 1057 "}", 1058 AllowsMergedIf); 1059 verifyFormat("MYIF (a) {\n" 1060 " g();\n" 1061 "} else if (b) {\n" 1062 " g();\n" 1063 "} else {\n" 1064 " g();\n" 1065 "}", 1066 AllowsMergedIf); 1067 1068 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 1069 FormatStyle::SIS_OnlyFirstIf; 1070 1071 verifyFormat("if (a) f();\n" 1072 "else {\n" 1073 " g();\n" 1074 "}", 1075 AllowsMergedIf); 1076 verifyFormat("if (a) f();\n" 1077 "else {\n" 1078 " if (a) f();\n" 1079 " else {\n" 1080 " g();\n" 1081 " }\n" 1082 " g();\n" 1083 "}", 1084 AllowsMergedIf); 1085 1086 verifyFormat("if (a) g();", AllowsMergedIf); 1087 verifyFormat("if (a) {\n" 1088 " g()\n" 1089 "};", 1090 AllowsMergedIf); 1091 verifyFormat("if (a) g();\n" 1092 "else\n" 1093 " g();", 1094 AllowsMergedIf); 1095 verifyFormat("if (a) {\n" 1096 " g();\n" 1097 "} else\n" 1098 " g();", 1099 AllowsMergedIf); 1100 verifyFormat("if (a) g();\n" 1101 "else {\n" 1102 " g();\n" 1103 "}", 1104 AllowsMergedIf); 1105 verifyFormat("if (a) {\n" 1106 " g();\n" 1107 "} else {\n" 1108 " g();\n" 1109 "}", 1110 AllowsMergedIf); 1111 verifyFormat("if (a) g();\n" 1112 "else if (b)\n" 1113 " g();\n" 1114 "else\n" 1115 " g();", 1116 AllowsMergedIf); 1117 verifyFormat("if (a) {\n" 1118 " g();\n" 1119 "} else if (b)\n" 1120 " g();\n" 1121 "else\n" 1122 " g();", 1123 AllowsMergedIf); 1124 verifyFormat("if (a) g();\n" 1125 "else if (b) {\n" 1126 " g();\n" 1127 "} else\n" 1128 " g();", 1129 AllowsMergedIf); 1130 verifyFormat("if (a) g();\n" 1131 "else if (b)\n" 1132 " g();\n" 1133 "else {\n" 1134 " g();\n" 1135 "}", 1136 AllowsMergedIf); 1137 verifyFormat("if (a) g();\n" 1138 "else if (b) {\n" 1139 " g();\n" 1140 "} else {\n" 1141 " g();\n" 1142 "}", 1143 AllowsMergedIf); 1144 verifyFormat("if (a) {\n" 1145 " g();\n" 1146 "} else if (b) {\n" 1147 " g();\n" 1148 "} else {\n" 1149 " g();\n" 1150 "}", 1151 AllowsMergedIf); 1152 verifyFormat("MYIF (a) f();\n" 1153 "else {\n" 1154 " g();\n" 1155 "}", 1156 AllowsMergedIf); 1157 verifyFormat("MYIF (a) f();\n" 1158 "else {\n" 1159 " if (a) f();\n" 1160 " else {\n" 1161 " g();\n" 1162 " }\n" 1163 " g();\n" 1164 "}", 1165 AllowsMergedIf); 1166 1167 verifyFormat("MYIF (a) g();", AllowsMergedIf); 1168 verifyFormat("MYIF (a) {\n" 1169 " g()\n" 1170 "};", 1171 AllowsMergedIf); 1172 verifyFormat("MYIF (a) g();\n" 1173 "else\n" 1174 " g();", 1175 AllowsMergedIf); 1176 verifyFormat("MYIF (a) {\n" 1177 " g();\n" 1178 "} else\n" 1179 " g();", 1180 AllowsMergedIf); 1181 verifyFormat("MYIF (a) g();\n" 1182 "else {\n" 1183 " g();\n" 1184 "}", 1185 AllowsMergedIf); 1186 verifyFormat("MYIF (a) {\n" 1187 " g();\n" 1188 "} else {\n" 1189 " g();\n" 1190 "}", 1191 AllowsMergedIf); 1192 verifyFormat("MYIF (a) g();\n" 1193 "else MYIF (b)\n" 1194 " g();\n" 1195 "else\n" 1196 " g();", 1197 AllowsMergedIf); 1198 verifyFormat("MYIF (a) g();\n" 1199 "else if (b)\n" 1200 " g();\n" 1201 "else\n" 1202 " g();", 1203 AllowsMergedIf); 1204 verifyFormat("MYIF (a) {\n" 1205 " g();\n" 1206 "} else MYIF (b)\n" 1207 " g();\n" 1208 "else\n" 1209 " g();", 1210 AllowsMergedIf); 1211 verifyFormat("MYIF (a) {\n" 1212 " g();\n" 1213 "} else if (b)\n" 1214 " g();\n" 1215 "else\n" 1216 " g();", 1217 AllowsMergedIf); 1218 verifyFormat("MYIF (a) g();\n" 1219 "else MYIF (b) {\n" 1220 " g();\n" 1221 "} else\n" 1222 " g();", 1223 AllowsMergedIf); 1224 verifyFormat("MYIF (a) g();\n" 1225 "else if (b) {\n" 1226 " g();\n" 1227 "} else\n" 1228 " g();", 1229 AllowsMergedIf); 1230 verifyFormat("MYIF (a) g();\n" 1231 "else MYIF (b)\n" 1232 " g();\n" 1233 "else {\n" 1234 " g();\n" 1235 "}", 1236 AllowsMergedIf); 1237 verifyFormat("MYIF (a) g();\n" 1238 "else if (b)\n" 1239 " g();\n" 1240 "else {\n" 1241 " g();\n" 1242 "}", 1243 AllowsMergedIf); 1244 verifyFormat("MYIF (a) g();\n" 1245 "else MYIF (b) {\n" 1246 " g();\n" 1247 "} else {\n" 1248 " g();\n" 1249 "}", 1250 AllowsMergedIf); 1251 verifyFormat("MYIF (a) g();\n" 1252 "else if (b) {\n" 1253 " g();\n" 1254 "} else {\n" 1255 " g();\n" 1256 "}", 1257 AllowsMergedIf); 1258 verifyFormat("MYIF (a) {\n" 1259 " g();\n" 1260 "} else MYIF (b) {\n" 1261 " g();\n" 1262 "} else {\n" 1263 " g();\n" 1264 "}", 1265 AllowsMergedIf); 1266 verifyFormat("MYIF (a) {\n" 1267 " g();\n" 1268 "} else if (b) {\n" 1269 " g();\n" 1270 "} else {\n" 1271 " g();\n" 1272 "}", 1273 AllowsMergedIf); 1274 1275 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 1276 FormatStyle::SIS_AllIfsAndElse; 1277 1278 verifyFormat("if (a) f();\n" 1279 "else {\n" 1280 " g();\n" 1281 "}", 1282 AllowsMergedIf); 1283 verifyFormat("if (a) f();\n" 1284 "else {\n" 1285 " if (a) f();\n" 1286 " else {\n" 1287 " g();\n" 1288 " }\n" 1289 " g();\n" 1290 "}", 1291 AllowsMergedIf); 1292 1293 verifyFormat("if (a) g();", AllowsMergedIf); 1294 verifyFormat("if (a) {\n" 1295 " g()\n" 1296 "};", 1297 AllowsMergedIf); 1298 verifyFormat("if (a) g();\n" 1299 "else g();", 1300 AllowsMergedIf); 1301 verifyFormat("if (a) {\n" 1302 " g();\n" 1303 "} else g();", 1304 AllowsMergedIf); 1305 verifyFormat("if (a) g();\n" 1306 "else {\n" 1307 " g();\n" 1308 "}", 1309 AllowsMergedIf); 1310 verifyFormat("if (a) {\n" 1311 " g();\n" 1312 "} else {\n" 1313 " g();\n" 1314 "}", 1315 AllowsMergedIf); 1316 verifyFormat("if (a) g();\n" 1317 "else if (b) g();\n" 1318 "else g();", 1319 AllowsMergedIf); 1320 verifyFormat("if (a) {\n" 1321 " g();\n" 1322 "} else if (b) g();\n" 1323 "else g();", 1324 AllowsMergedIf); 1325 verifyFormat("if (a) g();\n" 1326 "else if (b) {\n" 1327 " g();\n" 1328 "} else g();", 1329 AllowsMergedIf); 1330 verifyFormat("if (a) g();\n" 1331 "else if (b) g();\n" 1332 "else {\n" 1333 " g();\n" 1334 "}", 1335 AllowsMergedIf); 1336 verifyFormat("if (a) g();\n" 1337 "else if (b) {\n" 1338 " g();\n" 1339 "} else {\n" 1340 " g();\n" 1341 "}", 1342 AllowsMergedIf); 1343 verifyFormat("if (a) {\n" 1344 " g();\n" 1345 "} else if (b) {\n" 1346 " g();\n" 1347 "} else {\n" 1348 " g();\n" 1349 "}", 1350 AllowsMergedIf); 1351 verifyFormat("MYIF (a) f();\n" 1352 "else {\n" 1353 " g();\n" 1354 "}", 1355 AllowsMergedIf); 1356 verifyFormat("MYIF (a) f();\n" 1357 "else {\n" 1358 " if (a) f();\n" 1359 " else {\n" 1360 " g();\n" 1361 " }\n" 1362 " g();\n" 1363 "}", 1364 AllowsMergedIf); 1365 1366 verifyFormat("MYIF (a) g();", AllowsMergedIf); 1367 verifyFormat("MYIF (a) {\n" 1368 " g()\n" 1369 "};", 1370 AllowsMergedIf); 1371 verifyFormat("MYIF (a) g();\n" 1372 "else g();", 1373 AllowsMergedIf); 1374 verifyFormat("MYIF (a) {\n" 1375 " g();\n" 1376 "} else g();", 1377 AllowsMergedIf); 1378 verifyFormat("MYIF (a) g();\n" 1379 "else {\n" 1380 " g();\n" 1381 "}", 1382 AllowsMergedIf); 1383 verifyFormat("MYIF (a) {\n" 1384 " g();\n" 1385 "} else {\n" 1386 " g();\n" 1387 "}", 1388 AllowsMergedIf); 1389 verifyFormat("MYIF (a) g();\n" 1390 "else MYIF (b) g();\n" 1391 "else g();", 1392 AllowsMergedIf); 1393 verifyFormat("MYIF (a) g();\n" 1394 "else if (b) g();\n" 1395 "else g();", 1396 AllowsMergedIf); 1397 verifyFormat("MYIF (a) {\n" 1398 " g();\n" 1399 "} else MYIF (b) g();\n" 1400 "else g();", 1401 AllowsMergedIf); 1402 verifyFormat("MYIF (a) {\n" 1403 " g();\n" 1404 "} else if (b) g();\n" 1405 "else g();", 1406 AllowsMergedIf); 1407 verifyFormat("MYIF (a) g();\n" 1408 "else MYIF (b) {\n" 1409 " g();\n" 1410 "} else g();", 1411 AllowsMergedIf); 1412 verifyFormat("MYIF (a) g();\n" 1413 "else if (b) {\n" 1414 " g();\n" 1415 "} else g();", 1416 AllowsMergedIf); 1417 verifyFormat("MYIF (a) g();\n" 1418 "else MYIF (b) g();\n" 1419 "else {\n" 1420 " g();\n" 1421 "}", 1422 AllowsMergedIf); 1423 verifyFormat("MYIF (a) g();\n" 1424 "else if (b) g();\n" 1425 "else {\n" 1426 " g();\n" 1427 "}", 1428 AllowsMergedIf); 1429 verifyFormat("MYIF (a) g();\n" 1430 "else MYIF (b) {\n" 1431 " g();\n" 1432 "} else {\n" 1433 " g();\n" 1434 "}", 1435 AllowsMergedIf); 1436 verifyFormat("MYIF (a) g();\n" 1437 "else if (b) {\n" 1438 " g();\n" 1439 "} else {\n" 1440 " g();\n" 1441 "}", 1442 AllowsMergedIf); 1443 verifyFormat("MYIF (a) {\n" 1444 " g();\n" 1445 "} else MYIF (b) {\n" 1446 " g();\n" 1447 "} else {\n" 1448 " g();\n" 1449 "}", 1450 AllowsMergedIf); 1451 verifyFormat("MYIF (a) {\n" 1452 " g();\n" 1453 "} else if (b) {\n" 1454 " g();\n" 1455 "} else {\n" 1456 " g();\n" 1457 "}", 1458 AllowsMergedIf); 1459 } 1460 1461 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) { 1462 FormatStyle AllowsMergedLoops = getLLVMStyle(); 1463 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true; 1464 verifyFormat("while (true) continue;", AllowsMergedLoops); 1465 verifyFormat("for (;;) continue;", AllowsMergedLoops); 1466 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops); 1467 verifyFormat("while (true)\n" 1468 " ;", 1469 AllowsMergedLoops); 1470 verifyFormat("for (;;)\n" 1471 " ;", 1472 AllowsMergedLoops); 1473 verifyFormat("for (;;)\n" 1474 " for (;;) continue;", 1475 AllowsMergedLoops); 1476 verifyFormat("for (;;) // Can't merge this\n" 1477 " continue;", 1478 AllowsMergedLoops); 1479 verifyFormat("for (;;) /* still don't merge */\n" 1480 " continue;", 1481 AllowsMergedLoops); 1482 verifyFormat("do a++;\n" 1483 "while (true);", 1484 AllowsMergedLoops); 1485 verifyFormat("do /* Don't merge */\n" 1486 " a++;\n" 1487 "while (true);", 1488 AllowsMergedLoops); 1489 verifyFormat("do // Don't merge\n" 1490 " a++;\n" 1491 "while (true);", 1492 AllowsMergedLoops); 1493 verifyFormat("do\n" 1494 " // Don't merge\n" 1495 " a++;\n" 1496 "while (true);", 1497 AllowsMergedLoops); 1498 // Without braces labels are interpreted differently. 1499 verifyFormat("{\n" 1500 " do\n" 1501 " label:\n" 1502 " a++;\n" 1503 " while (true);\n" 1504 "}", 1505 AllowsMergedLoops); 1506 } 1507 1508 TEST_F(FormatTest, FormatShortBracedStatements) { 1509 FormatStyle AllowSimpleBracedStatements = getLLVMStyle(); 1510 AllowSimpleBracedStatements.IfMacros.push_back("MYIF"); 1511 // Where line-lengths matter, a 2-letter synonym that maintains line length. 1512 // Not IF to avoid any confusion that IF is somehow special. 1513 AllowSimpleBracedStatements.IfMacros.push_back("FI"); 1514 AllowSimpleBracedStatements.ColumnLimit = 40; 1515 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = 1516 FormatStyle::SBS_Always; 1517 1518 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1519 FormatStyle::SIS_WithoutElse; 1520 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 1521 1522 AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom; 1523 AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true; 1524 AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false; 1525 1526 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1527 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 1528 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1529 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1530 verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements); 1531 verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1532 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1533 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1534 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 1535 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 1536 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1537 verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements); 1538 verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements); 1539 verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1540 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 1541 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 1542 verifyFormat("if (true) { fffffffffffffffffffffff(); }", 1543 AllowSimpleBracedStatements); 1544 verifyFormat("if (true) {\n" 1545 " ffffffffffffffffffffffff();\n" 1546 "}", 1547 AllowSimpleBracedStatements); 1548 verifyFormat("if (true) {\n" 1549 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1550 "}", 1551 AllowSimpleBracedStatements); 1552 verifyFormat("if (true) { //\n" 1553 " f();\n" 1554 "}", 1555 AllowSimpleBracedStatements); 1556 verifyFormat("if (true) {\n" 1557 " f();\n" 1558 " f();\n" 1559 "}", 1560 AllowSimpleBracedStatements); 1561 verifyFormat("if (true) {\n" 1562 " f();\n" 1563 "} else {\n" 1564 " f();\n" 1565 "}", 1566 AllowSimpleBracedStatements); 1567 verifyFormat("FI (true) { fffffffffffffffffffffff(); }", 1568 AllowSimpleBracedStatements); 1569 verifyFormat("MYIF (true) {\n" 1570 " ffffffffffffffffffffffff();\n" 1571 "}", 1572 AllowSimpleBracedStatements); 1573 verifyFormat("MYIF (true) {\n" 1574 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1575 "}", 1576 AllowSimpleBracedStatements); 1577 verifyFormat("MYIF (true) { //\n" 1578 " f();\n" 1579 "}", 1580 AllowSimpleBracedStatements); 1581 verifyFormat("MYIF (true) {\n" 1582 " f();\n" 1583 " f();\n" 1584 "}", 1585 AllowSimpleBracedStatements); 1586 verifyFormat("MYIF (true) {\n" 1587 " f();\n" 1588 "} else {\n" 1589 " f();\n" 1590 "}", 1591 AllowSimpleBracedStatements); 1592 1593 verifyFormat("struct A2 {\n" 1594 " int X;\n" 1595 "};", 1596 AllowSimpleBracedStatements); 1597 verifyFormat("typedef struct A2 {\n" 1598 " int X;\n" 1599 "} A2_t;", 1600 AllowSimpleBracedStatements); 1601 verifyFormat("template <int> struct A2 {\n" 1602 " struct B {};\n" 1603 "};", 1604 AllowSimpleBracedStatements); 1605 1606 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1607 FormatStyle::SIS_Never; 1608 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1609 verifyFormat("if (true) {\n" 1610 " f();\n" 1611 "}", 1612 AllowSimpleBracedStatements); 1613 verifyFormat("if (true) {\n" 1614 " f();\n" 1615 "} else {\n" 1616 " f();\n" 1617 "}", 1618 AllowSimpleBracedStatements); 1619 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1620 verifyFormat("MYIF (true) {\n" 1621 " f();\n" 1622 "}", 1623 AllowSimpleBracedStatements); 1624 verifyFormat("MYIF (true) {\n" 1625 " f();\n" 1626 "} else {\n" 1627 " f();\n" 1628 "}", 1629 AllowSimpleBracedStatements); 1630 1631 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 1632 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1633 verifyFormat("while (true) {\n" 1634 " f();\n" 1635 "}", 1636 AllowSimpleBracedStatements); 1637 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1638 verifyFormat("for (;;) {\n" 1639 " f();\n" 1640 "}", 1641 AllowSimpleBracedStatements); 1642 1643 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1644 FormatStyle::SIS_WithoutElse; 1645 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 1646 AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement = 1647 FormatStyle::BWACS_Always; 1648 1649 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1650 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 1651 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1652 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1653 verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements); 1654 verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1655 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1656 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1657 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 1658 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 1659 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1660 verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements); 1661 verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements); 1662 verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1663 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 1664 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 1665 verifyFormat("if (true) { fffffffffffffffffffffff(); }", 1666 AllowSimpleBracedStatements); 1667 verifyFormat("if (true)\n" 1668 "{\n" 1669 " ffffffffffffffffffffffff();\n" 1670 "}", 1671 AllowSimpleBracedStatements); 1672 verifyFormat("if (true)\n" 1673 "{\n" 1674 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1675 "}", 1676 AllowSimpleBracedStatements); 1677 verifyFormat("if (true)\n" 1678 "{ //\n" 1679 " f();\n" 1680 "}", 1681 AllowSimpleBracedStatements); 1682 verifyFormat("if (true)\n" 1683 "{\n" 1684 " f();\n" 1685 " f();\n" 1686 "}", 1687 AllowSimpleBracedStatements); 1688 verifyFormat("if (true)\n" 1689 "{\n" 1690 " f();\n" 1691 "} else\n" 1692 "{\n" 1693 " f();\n" 1694 "}", 1695 AllowSimpleBracedStatements); 1696 verifyFormat("FI (true) { fffffffffffffffffffffff(); }", 1697 AllowSimpleBracedStatements); 1698 verifyFormat("MYIF (true)\n" 1699 "{\n" 1700 " ffffffffffffffffffffffff();\n" 1701 "}", 1702 AllowSimpleBracedStatements); 1703 verifyFormat("MYIF (true)\n" 1704 "{\n" 1705 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1706 "}", 1707 AllowSimpleBracedStatements); 1708 verifyFormat("MYIF (true)\n" 1709 "{ //\n" 1710 " f();\n" 1711 "}", 1712 AllowSimpleBracedStatements); 1713 verifyFormat("MYIF (true)\n" 1714 "{\n" 1715 " f();\n" 1716 " f();\n" 1717 "}", 1718 AllowSimpleBracedStatements); 1719 verifyFormat("MYIF (true)\n" 1720 "{\n" 1721 " f();\n" 1722 "} else\n" 1723 "{\n" 1724 " f();\n" 1725 "}", 1726 AllowSimpleBracedStatements); 1727 1728 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1729 FormatStyle::SIS_Never; 1730 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1731 verifyFormat("if (true)\n" 1732 "{\n" 1733 " f();\n" 1734 "}", 1735 AllowSimpleBracedStatements); 1736 verifyFormat("if (true)\n" 1737 "{\n" 1738 " f();\n" 1739 "} else\n" 1740 "{\n" 1741 " f();\n" 1742 "}", 1743 AllowSimpleBracedStatements); 1744 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1745 verifyFormat("MYIF (true)\n" 1746 "{\n" 1747 " f();\n" 1748 "}", 1749 AllowSimpleBracedStatements); 1750 verifyFormat("MYIF (true)\n" 1751 "{\n" 1752 " f();\n" 1753 "} else\n" 1754 "{\n" 1755 " f();\n" 1756 "}", 1757 AllowSimpleBracedStatements); 1758 1759 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 1760 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1761 verifyFormat("while (true)\n" 1762 "{\n" 1763 " f();\n" 1764 "}", 1765 AllowSimpleBracedStatements); 1766 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1767 verifyFormat("for (;;)\n" 1768 "{\n" 1769 " f();\n" 1770 "}", 1771 AllowSimpleBracedStatements); 1772 } 1773 1774 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) { 1775 FormatStyle Style = getLLVMStyleWithColumns(60); 1776 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 1777 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; 1778 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 1779 EXPECT_EQ("#define A \\\n" 1780 " if (HANDLEwernufrnuLwrmviferuvnierv) \\\n" 1781 " { \\\n" 1782 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" 1783 " }\n" 1784 "X;", 1785 format("#define A \\\n" 1786 " if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n" 1787 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" 1788 " }\n" 1789 "X;", 1790 Style)); 1791 } 1792 1793 TEST_F(FormatTest, ParseIfElse) { 1794 verifyFormat("if (true)\n" 1795 " if (true)\n" 1796 " if (true)\n" 1797 " f();\n" 1798 " else\n" 1799 " g();\n" 1800 " else\n" 1801 " h();\n" 1802 "else\n" 1803 " i();"); 1804 verifyFormat("if (true)\n" 1805 " if (true)\n" 1806 " if (true) {\n" 1807 " if (true)\n" 1808 " f();\n" 1809 " } else {\n" 1810 " g();\n" 1811 " }\n" 1812 " else\n" 1813 " h();\n" 1814 "else {\n" 1815 " i();\n" 1816 "}"); 1817 verifyFormat("if (true)\n" 1818 " if constexpr (true)\n" 1819 " if (true) {\n" 1820 " if constexpr (true)\n" 1821 " f();\n" 1822 " } else {\n" 1823 " g();\n" 1824 " }\n" 1825 " else\n" 1826 " h();\n" 1827 "else {\n" 1828 " i();\n" 1829 "}"); 1830 verifyFormat("if (true)\n" 1831 " if CONSTEXPR (true)\n" 1832 " if (true) {\n" 1833 " if CONSTEXPR (true)\n" 1834 " f();\n" 1835 " } else {\n" 1836 " g();\n" 1837 " }\n" 1838 " else\n" 1839 " h();\n" 1840 "else {\n" 1841 " i();\n" 1842 "}"); 1843 verifyFormat("void f() {\n" 1844 " if (a) {\n" 1845 " } else {\n" 1846 " }\n" 1847 "}"); 1848 } 1849 1850 TEST_F(FormatTest, ElseIf) { 1851 verifyFormat("if (a) {\n} else if (b) {\n}"); 1852 verifyFormat("if (a)\n" 1853 " f();\n" 1854 "else if (b)\n" 1855 " g();\n" 1856 "else\n" 1857 " h();"); 1858 verifyFormat("if (a)\n" 1859 " f();\n" 1860 "else // comment\n" 1861 " if (b) {\n" 1862 " g();\n" 1863 " h();\n" 1864 " }"); 1865 verifyFormat("if constexpr (a)\n" 1866 " f();\n" 1867 "else if constexpr (b)\n" 1868 " g();\n" 1869 "else\n" 1870 " h();"); 1871 verifyFormat("if CONSTEXPR (a)\n" 1872 " f();\n" 1873 "else if CONSTEXPR (b)\n" 1874 " g();\n" 1875 "else\n" 1876 " h();"); 1877 verifyFormat("if (a) {\n" 1878 " f();\n" 1879 "}\n" 1880 "// or else ..\n" 1881 "else {\n" 1882 " g()\n" 1883 "}"); 1884 1885 verifyFormat("if (a) {\n" 1886 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1887 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 1888 "}"); 1889 verifyFormat("if (a) {\n" 1890 "} else if constexpr (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1891 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 1892 "}"); 1893 verifyFormat("if (a) {\n" 1894 "} else if CONSTEXPR (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1895 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 1896 "}"); 1897 verifyFormat("if (a) {\n" 1898 "} else if (\n" 1899 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 1900 "}", 1901 getLLVMStyleWithColumns(62)); 1902 verifyFormat("if (a) {\n" 1903 "} else if constexpr (\n" 1904 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 1905 "}", 1906 getLLVMStyleWithColumns(62)); 1907 verifyFormat("if (a) {\n" 1908 "} else if CONSTEXPR (\n" 1909 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 1910 "}", 1911 getLLVMStyleWithColumns(62)); 1912 } 1913 1914 TEST_F(FormatTest, SeparatePointerReferenceAlignment) { 1915 FormatStyle Style = getLLVMStyle(); 1916 // Check first the default LLVM style 1917 // Style.PointerAlignment = FormatStyle::PAS_Right; 1918 // Style.ReferenceAlignment = FormatStyle::RAS_Pointer; 1919 verifyFormat("int *f1(int *a, int &b, int &&c);", Style); 1920 verifyFormat("int &f2(int &&c, int *a, int &b);", Style); 1921 verifyFormat("int &&f3(int &b, int &&c, int *a);", Style); 1922 verifyFormat("int *f1(int &a) const &;", Style); 1923 verifyFormat("int *f1(int &a) const & = 0;", Style); 1924 verifyFormat("int *a = f1();", Style); 1925 verifyFormat("int &b = f2();", Style); 1926 verifyFormat("int &&c = f3();", Style); 1927 1928 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 1929 verifyFormat("Const unsigned int *c;\n" 1930 "const unsigned int *d;\n" 1931 "Const unsigned int &e;\n" 1932 "const unsigned int &f;\n" 1933 "const unsigned &&g;\n" 1934 "Const unsigned h;", 1935 Style); 1936 1937 Style.PointerAlignment = FormatStyle::PAS_Left; 1938 Style.ReferenceAlignment = FormatStyle::RAS_Pointer; 1939 verifyFormat("int* f1(int* a, int& b, int&& c);", Style); 1940 verifyFormat("int& f2(int&& c, int* a, int& b);", Style); 1941 verifyFormat("int&& f3(int& b, int&& c, int* a);", Style); 1942 verifyFormat("int* f1(int& a) const& = 0;", Style); 1943 verifyFormat("int* a = f1();", Style); 1944 verifyFormat("int& b = f2();", Style); 1945 verifyFormat("int&& c = f3();", Style); 1946 1947 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 1948 verifyFormat("Const unsigned int* c;\n" 1949 "const unsigned int* d;\n" 1950 "Const unsigned int& e;\n" 1951 "const unsigned int& f;\n" 1952 "const unsigned&& g;\n" 1953 "Const unsigned h;", 1954 Style); 1955 1956 Style.PointerAlignment = FormatStyle::PAS_Right; 1957 Style.ReferenceAlignment = FormatStyle::RAS_Left; 1958 verifyFormat("int *f1(int *a, int& b, int&& c);", Style); 1959 verifyFormat("int& f2(int&& c, int *a, int& b);", Style); 1960 verifyFormat("int&& f3(int& b, int&& c, int *a);", Style); 1961 verifyFormat("int *a = f1();", Style); 1962 verifyFormat("int& b = f2();", Style); 1963 verifyFormat("int&& c = f3();", Style); 1964 1965 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 1966 verifyFormat("Const unsigned int *c;\n" 1967 "const unsigned int *d;\n" 1968 "Const unsigned int& e;\n" 1969 "const unsigned int& f;\n" 1970 "const unsigned g;\n" 1971 "Const unsigned h;", 1972 Style); 1973 1974 Style.PointerAlignment = FormatStyle::PAS_Left; 1975 Style.ReferenceAlignment = FormatStyle::RAS_Middle; 1976 verifyFormat("int* f1(int* a, int & b, int && c);", Style); 1977 verifyFormat("int & f2(int && c, int* a, int & b);", Style); 1978 verifyFormat("int && f3(int & b, int && c, int* a);", Style); 1979 verifyFormat("int* a = f1();", Style); 1980 verifyFormat("int & b = f2();", Style); 1981 verifyFormat("int && c = f3();", Style); 1982 1983 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 1984 verifyFormat("Const unsigned int* c;\n" 1985 "const unsigned int* d;\n" 1986 "Const unsigned int & e;\n" 1987 "const unsigned int & f;\n" 1988 "const unsigned && g;\n" 1989 "Const unsigned h;", 1990 Style); 1991 1992 Style.PointerAlignment = FormatStyle::PAS_Middle; 1993 Style.ReferenceAlignment = FormatStyle::RAS_Right; 1994 verifyFormat("int * f1(int * a, int &b, int &&c);", Style); 1995 verifyFormat("int &f2(int &&c, int * a, int &b);", Style); 1996 verifyFormat("int &&f3(int &b, int &&c, int * a);", Style); 1997 verifyFormat("int * a = f1();", Style); 1998 verifyFormat("int &b = f2();", Style); 1999 verifyFormat("int &&c = f3();", Style); 2000 2001 // FIXME: we don't handle this yet, so output may be arbitrary until it's 2002 // specifically handled 2003 // verifyFormat("int Add2(BTree * &Root, char * szToAdd)", Style); 2004 } 2005 2006 TEST_F(FormatTest, FormatsForLoop) { 2007 verifyFormat( 2008 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 2009 " ++VeryVeryLongLoopVariable)\n" 2010 " ;"); 2011 verifyFormat("for (;;)\n" 2012 " f();"); 2013 verifyFormat("for (;;) {\n}"); 2014 verifyFormat("for (;;) {\n" 2015 " f();\n" 2016 "}"); 2017 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}"); 2018 2019 verifyFormat( 2020 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 2021 " E = UnwrappedLines.end();\n" 2022 " I != E; ++I) {\n}"); 2023 2024 verifyFormat( 2025 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" 2026 " ++IIIII) {\n}"); 2027 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" 2028 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" 2029 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); 2030 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n" 2031 " I = FD->getDeclsInPrototypeScope().begin(),\n" 2032 " E = FD->getDeclsInPrototypeScope().end();\n" 2033 " I != E; ++I) {\n}"); 2034 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n" 2035 " I = Container.begin(),\n" 2036 " E = Container.end();\n" 2037 " I != E; ++I) {\n}", 2038 getLLVMStyleWithColumns(76)); 2039 2040 verifyFormat( 2041 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 2042 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" 2043 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2044 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 2045 " ++aaaaaaaaaaa) {\n}"); 2046 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2047 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n" 2048 " ++i) {\n}"); 2049 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" 2050 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 2051 "}"); 2052 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n" 2053 " aaaaaaaaaa);\n" 2054 " iter; ++iter) {\n" 2055 "}"); 2056 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2057 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 2058 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n" 2059 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {"); 2060 2061 // These should not be formatted as Objective-C for-in loops. 2062 verifyFormat("for (Foo *x = 0; x != in; x++) {\n}"); 2063 verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}"); 2064 verifyFormat("Foo *x;\nfor (x in y) {\n}"); 2065 verifyFormat( 2066 "for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}"); 2067 2068 FormatStyle NoBinPacking = getLLVMStyle(); 2069 NoBinPacking.BinPackParameters = false; 2070 verifyFormat("for (int aaaaaaaaaaa = 1;\n" 2071 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" 2072 " aaaaaaaaaaaaaaaa,\n" 2073 " aaaaaaaaaaaaaaaa,\n" 2074 " aaaaaaaaaaaaaaaa);\n" 2075 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 2076 "}", 2077 NoBinPacking); 2078 verifyFormat( 2079 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 2080 " E = UnwrappedLines.end();\n" 2081 " I != E;\n" 2082 " ++I) {\n}", 2083 NoBinPacking); 2084 2085 FormatStyle AlignLeft = getLLVMStyle(); 2086 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 2087 verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft); 2088 } 2089 2090 TEST_F(FormatTest, RangeBasedForLoops) { 2091 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 2092 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 2093 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" 2094 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); 2095 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" 2096 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 2097 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n" 2098 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}"); 2099 } 2100 2101 TEST_F(FormatTest, ForEachLoops) { 2102 verifyFormat("void f() {\n" 2103 " foreach (Item *item, itemlist) {}\n" 2104 " Q_FOREACH (Item *item, itemlist) {}\n" 2105 " BOOST_FOREACH (Item *item, itemlist) {}\n" 2106 " UNKNOWN_FORACH(Item * item, itemlist) {}\n" 2107 "}"); 2108 2109 FormatStyle Style = getLLVMStyle(); 2110 Style.SpaceBeforeParens = 2111 FormatStyle::SBPO_ControlStatementsExceptControlMacros; 2112 verifyFormat("void f() {\n" 2113 " foreach(Item *item, itemlist) {}\n" 2114 " Q_FOREACH(Item *item, itemlist) {}\n" 2115 " BOOST_FOREACH(Item *item, itemlist) {}\n" 2116 " UNKNOWN_FORACH(Item * item, itemlist) {}\n" 2117 "}", 2118 Style); 2119 2120 // As function-like macros. 2121 verifyFormat("#define foreach(x, y)\n" 2122 "#define Q_FOREACH(x, y)\n" 2123 "#define BOOST_FOREACH(x, y)\n" 2124 "#define UNKNOWN_FOREACH(x, y)\n"); 2125 2126 // Not as function-like macros. 2127 verifyFormat("#define foreach (x, y)\n" 2128 "#define Q_FOREACH (x, y)\n" 2129 "#define BOOST_FOREACH (x, y)\n" 2130 "#define UNKNOWN_FOREACH (x, y)\n"); 2131 2132 // handle microsoft non standard extension 2133 verifyFormat("for each (char c in x->MyStringProperty)"); 2134 } 2135 2136 TEST_F(FormatTest, FormatsWhileLoop) { 2137 verifyFormat("while (true) {\n}"); 2138 verifyFormat("while (true)\n" 2139 " f();"); 2140 verifyFormat("while () {\n}"); 2141 verifyFormat("while () {\n" 2142 " f();\n" 2143 "}"); 2144 } 2145 2146 TEST_F(FormatTest, FormatsDoWhile) { 2147 verifyFormat("do {\n" 2148 " do_something();\n" 2149 "} while (something());"); 2150 verifyFormat("do\n" 2151 " do_something();\n" 2152 "while (something());"); 2153 } 2154 2155 TEST_F(FormatTest, FormatsSwitchStatement) { 2156 verifyFormat("switch (x) {\n" 2157 "case 1:\n" 2158 " f();\n" 2159 " break;\n" 2160 "case kFoo:\n" 2161 "case ns::kBar:\n" 2162 "case kBaz:\n" 2163 " break;\n" 2164 "default:\n" 2165 " g();\n" 2166 " break;\n" 2167 "}"); 2168 verifyFormat("switch (x) {\n" 2169 "case 1: {\n" 2170 " f();\n" 2171 " break;\n" 2172 "}\n" 2173 "case 2: {\n" 2174 " break;\n" 2175 "}\n" 2176 "}"); 2177 verifyFormat("switch (x) {\n" 2178 "case 1: {\n" 2179 " f();\n" 2180 " {\n" 2181 " g();\n" 2182 " h();\n" 2183 " }\n" 2184 " break;\n" 2185 "}\n" 2186 "}"); 2187 verifyFormat("switch (x) {\n" 2188 "case 1: {\n" 2189 " f();\n" 2190 " if (foo) {\n" 2191 " g();\n" 2192 " h();\n" 2193 " }\n" 2194 " break;\n" 2195 "}\n" 2196 "}"); 2197 verifyFormat("switch (x) {\n" 2198 "case 1: {\n" 2199 " f();\n" 2200 " g();\n" 2201 "} break;\n" 2202 "}"); 2203 verifyFormat("switch (test)\n" 2204 " ;"); 2205 verifyFormat("switch (x) {\n" 2206 "default: {\n" 2207 " // Do nothing.\n" 2208 "}\n" 2209 "}"); 2210 verifyFormat("switch (x) {\n" 2211 "// comment\n" 2212 "// if 1, do f()\n" 2213 "case 1:\n" 2214 " f();\n" 2215 "}"); 2216 verifyFormat("switch (x) {\n" 2217 "case 1:\n" 2218 " // Do amazing stuff\n" 2219 " {\n" 2220 " f();\n" 2221 " g();\n" 2222 " }\n" 2223 " break;\n" 2224 "}"); 2225 verifyFormat("#define A \\\n" 2226 " switch (x) { \\\n" 2227 " case a: \\\n" 2228 " foo = b; \\\n" 2229 " }", 2230 getLLVMStyleWithColumns(20)); 2231 verifyFormat("#define OPERATION_CASE(name) \\\n" 2232 " case OP_name: \\\n" 2233 " return operations::Operation##name\n", 2234 getLLVMStyleWithColumns(40)); 2235 verifyFormat("switch (x) {\n" 2236 "case 1:;\n" 2237 "default:;\n" 2238 " int i;\n" 2239 "}"); 2240 2241 verifyGoogleFormat("switch (x) {\n" 2242 " case 1:\n" 2243 " f();\n" 2244 " break;\n" 2245 " case kFoo:\n" 2246 " case ns::kBar:\n" 2247 " case kBaz:\n" 2248 " break;\n" 2249 " default:\n" 2250 " g();\n" 2251 " break;\n" 2252 "}"); 2253 verifyGoogleFormat("switch (x) {\n" 2254 " case 1: {\n" 2255 " f();\n" 2256 " break;\n" 2257 " }\n" 2258 "}"); 2259 verifyGoogleFormat("switch (test)\n" 2260 " ;"); 2261 2262 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n" 2263 " case OP_name: \\\n" 2264 " return operations::Operation##name\n"); 2265 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n" 2266 " // Get the correction operation class.\n" 2267 " switch (OpCode) {\n" 2268 " CASE(Add);\n" 2269 " CASE(Subtract);\n" 2270 " default:\n" 2271 " return operations::Unknown;\n" 2272 " }\n" 2273 "#undef OPERATION_CASE\n" 2274 "}"); 2275 verifyFormat("DEBUG({\n" 2276 " switch (x) {\n" 2277 " case A:\n" 2278 " f();\n" 2279 " break;\n" 2280 " // fallthrough\n" 2281 " case B:\n" 2282 " g();\n" 2283 " break;\n" 2284 " }\n" 2285 "});"); 2286 EXPECT_EQ("DEBUG({\n" 2287 " switch (x) {\n" 2288 " case A:\n" 2289 " f();\n" 2290 " break;\n" 2291 " // On B:\n" 2292 " case B:\n" 2293 " g();\n" 2294 " break;\n" 2295 " }\n" 2296 "});", 2297 format("DEBUG({\n" 2298 " switch (x) {\n" 2299 " case A:\n" 2300 " f();\n" 2301 " break;\n" 2302 " // On B:\n" 2303 " case B:\n" 2304 " g();\n" 2305 " break;\n" 2306 " }\n" 2307 "});", 2308 getLLVMStyle())); 2309 EXPECT_EQ("switch (n) {\n" 2310 "case 0: {\n" 2311 " return false;\n" 2312 "}\n" 2313 "default: {\n" 2314 " return true;\n" 2315 "}\n" 2316 "}", 2317 format("switch (n)\n" 2318 "{\n" 2319 "case 0: {\n" 2320 " return false;\n" 2321 "}\n" 2322 "default: {\n" 2323 " return true;\n" 2324 "}\n" 2325 "}", 2326 getLLVMStyle())); 2327 verifyFormat("switch (a) {\n" 2328 "case (b):\n" 2329 " return;\n" 2330 "}"); 2331 2332 verifyFormat("switch (a) {\n" 2333 "case some_namespace::\n" 2334 " some_constant:\n" 2335 " return;\n" 2336 "}", 2337 getLLVMStyleWithColumns(34)); 2338 2339 FormatStyle Style = getLLVMStyle(); 2340 Style.IndentCaseLabels = true; 2341 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 2342 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2343 Style.BraceWrapping.AfterCaseLabel = true; 2344 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 2345 EXPECT_EQ("switch (n)\n" 2346 "{\n" 2347 " case 0:\n" 2348 " {\n" 2349 " return false;\n" 2350 " }\n" 2351 " default:\n" 2352 " {\n" 2353 " return true;\n" 2354 " }\n" 2355 "}", 2356 format("switch (n) {\n" 2357 " case 0: {\n" 2358 " return false;\n" 2359 " }\n" 2360 " default: {\n" 2361 " return true;\n" 2362 " }\n" 2363 "}", 2364 Style)); 2365 Style.BraceWrapping.AfterCaseLabel = false; 2366 EXPECT_EQ("switch (n)\n" 2367 "{\n" 2368 " case 0: {\n" 2369 " return false;\n" 2370 " }\n" 2371 " default: {\n" 2372 " return true;\n" 2373 " }\n" 2374 "}", 2375 format("switch (n) {\n" 2376 " case 0:\n" 2377 " {\n" 2378 " return false;\n" 2379 " }\n" 2380 " default:\n" 2381 " {\n" 2382 " return true;\n" 2383 " }\n" 2384 "}", 2385 Style)); 2386 Style.IndentCaseLabels = false; 2387 Style.IndentCaseBlocks = true; 2388 EXPECT_EQ("switch (n)\n" 2389 "{\n" 2390 "case 0:\n" 2391 " {\n" 2392 " return false;\n" 2393 " }\n" 2394 "case 1:\n" 2395 " break;\n" 2396 "default:\n" 2397 " {\n" 2398 " return true;\n" 2399 " }\n" 2400 "}", 2401 format("switch (n) {\n" 2402 "case 0: {\n" 2403 " return false;\n" 2404 "}\n" 2405 "case 1:\n" 2406 " break;\n" 2407 "default: {\n" 2408 " return true;\n" 2409 "}\n" 2410 "}", 2411 Style)); 2412 Style.IndentCaseLabels = true; 2413 Style.IndentCaseBlocks = true; 2414 EXPECT_EQ("switch (n)\n" 2415 "{\n" 2416 " case 0:\n" 2417 " {\n" 2418 " return false;\n" 2419 " }\n" 2420 " case 1:\n" 2421 " break;\n" 2422 " default:\n" 2423 " {\n" 2424 " return true;\n" 2425 " }\n" 2426 "}", 2427 format("switch (n) {\n" 2428 "case 0: {\n" 2429 " return false;\n" 2430 "}\n" 2431 "case 1:\n" 2432 " break;\n" 2433 "default: {\n" 2434 " return true;\n" 2435 "}\n" 2436 "}", 2437 Style)); 2438 } 2439 2440 TEST_F(FormatTest, CaseRanges) { 2441 verifyFormat("switch (x) {\n" 2442 "case 'A' ... 'Z':\n" 2443 "case 1 ... 5:\n" 2444 "case a ... b:\n" 2445 " break;\n" 2446 "}"); 2447 } 2448 2449 TEST_F(FormatTest, ShortEnums) { 2450 FormatStyle Style = getLLVMStyle(); 2451 Style.AllowShortEnumsOnASingleLine = true; 2452 verifyFormat("enum { A, B, C } ShortEnum1, ShortEnum2;", Style); 2453 Style.AllowShortEnumsOnASingleLine = false; 2454 verifyFormat("enum\n" 2455 "{\n" 2456 " A,\n" 2457 " B,\n" 2458 " C\n" 2459 "} ShortEnum1, ShortEnum2;", 2460 Style); 2461 } 2462 2463 TEST_F(FormatTest, ShortCaseLabels) { 2464 FormatStyle Style = getLLVMStyle(); 2465 Style.AllowShortCaseLabelsOnASingleLine = true; 2466 verifyFormat("switch (a) {\n" 2467 "case 1: x = 1; break;\n" 2468 "case 2: return;\n" 2469 "case 3:\n" 2470 "case 4:\n" 2471 "case 5: return;\n" 2472 "case 6: // comment\n" 2473 " return;\n" 2474 "case 7:\n" 2475 " // comment\n" 2476 " return;\n" 2477 "case 8:\n" 2478 " x = 8; // comment\n" 2479 " break;\n" 2480 "default: y = 1; break;\n" 2481 "}", 2482 Style); 2483 verifyFormat("switch (a) {\n" 2484 "case 0: return; // comment\n" 2485 "case 1: break; // comment\n" 2486 "case 2: return;\n" 2487 "// comment\n" 2488 "case 3: return;\n" 2489 "// comment 1\n" 2490 "// comment 2\n" 2491 "// comment 3\n" 2492 "case 4: break; /* comment */\n" 2493 "case 5:\n" 2494 " // comment\n" 2495 " break;\n" 2496 "case 6: /* comment */ x = 1; break;\n" 2497 "case 7: x = /* comment */ 1; break;\n" 2498 "case 8:\n" 2499 " x = 1; /* comment */\n" 2500 " break;\n" 2501 "case 9:\n" 2502 " break; // comment line 1\n" 2503 " // comment line 2\n" 2504 "}", 2505 Style); 2506 EXPECT_EQ("switch (a) {\n" 2507 "case 1:\n" 2508 " x = 8;\n" 2509 " // fall through\n" 2510 "case 2: x = 8;\n" 2511 "// comment\n" 2512 "case 3:\n" 2513 " return; /* comment line 1\n" 2514 " * comment line 2 */\n" 2515 "case 4: i = 8;\n" 2516 "// something else\n" 2517 "#if FOO\n" 2518 "case 5: break;\n" 2519 "#endif\n" 2520 "}", 2521 format("switch (a) {\n" 2522 "case 1: x = 8;\n" 2523 " // fall through\n" 2524 "case 2:\n" 2525 " x = 8;\n" 2526 "// comment\n" 2527 "case 3:\n" 2528 " return; /* comment line 1\n" 2529 " * comment line 2 */\n" 2530 "case 4:\n" 2531 " i = 8;\n" 2532 "// something else\n" 2533 "#if FOO\n" 2534 "case 5: break;\n" 2535 "#endif\n" 2536 "}", 2537 Style)); 2538 EXPECT_EQ("switch (a) {\n" 2539 "case 0:\n" 2540 " return; // long long long long long long long long long long " 2541 "long long comment\n" 2542 " // line\n" 2543 "}", 2544 format("switch (a) {\n" 2545 "case 0: return; // long long long long long long long long " 2546 "long long long long comment line\n" 2547 "}", 2548 Style)); 2549 EXPECT_EQ("switch (a) {\n" 2550 "case 0:\n" 2551 " return; /* long long long long long long long long long long " 2552 "long long comment\n" 2553 " line */\n" 2554 "}", 2555 format("switch (a) {\n" 2556 "case 0: return; /* long long long long long long long long " 2557 "long long long long comment line */\n" 2558 "}", 2559 Style)); 2560 verifyFormat("switch (a) {\n" 2561 "#if FOO\n" 2562 "case 0: return 0;\n" 2563 "#endif\n" 2564 "}", 2565 Style); 2566 verifyFormat("switch (a) {\n" 2567 "case 1: {\n" 2568 "}\n" 2569 "case 2: {\n" 2570 " return;\n" 2571 "}\n" 2572 "case 3: {\n" 2573 " x = 1;\n" 2574 " return;\n" 2575 "}\n" 2576 "case 4:\n" 2577 " if (x)\n" 2578 " return;\n" 2579 "}", 2580 Style); 2581 Style.ColumnLimit = 21; 2582 verifyFormat("switch (a) {\n" 2583 "case 1: x = 1; break;\n" 2584 "case 2: return;\n" 2585 "case 3:\n" 2586 "case 4:\n" 2587 "case 5: return;\n" 2588 "default:\n" 2589 " y = 1;\n" 2590 " break;\n" 2591 "}", 2592 Style); 2593 Style.ColumnLimit = 80; 2594 Style.AllowShortCaseLabelsOnASingleLine = false; 2595 Style.IndentCaseLabels = true; 2596 EXPECT_EQ("switch (n) {\n" 2597 " default /*comments*/:\n" 2598 " return true;\n" 2599 " case 0:\n" 2600 " return false;\n" 2601 "}", 2602 format("switch (n) {\n" 2603 "default/*comments*/:\n" 2604 " return true;\n" 2605 "case 0:\n" 2606 " return false;\n" 2607 "}", 2608 Style)); 2609 Style.AllowShortCaseLabelsOnASingleLine = true; 2610 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2611 Style.BraceWrapping.AfterCaseLabel = true; 2612 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 2613 EXPECT_EQ("switch (n)\n" 2614 "{\n" 2615 " case 0:\n" 2616 " {\n" 2617 " return false;\n" 2618 " }\n" 2619 " default:\n" 2620 " {\n" 2621 " return true;\n" 2622 " }\n" 2623 "}", 2624 format("switch (n) {\n" 2625 " case 0: {\n" 2626 " return false;\n" 2627 " }\n" 2628 " default:\n" 2629 " {\n" 2630 " return true;\n" 2631 " }\n" 2632 "}", 2633 Style)); 2634 } 2635 2636 TEST_F(FormatTest, FormatsLabels) { 2637 verifyFormat("void f() {\n" 2638 " some_code();\n" 2639 "test_label:\n" 2640 " some_other_code();\n" 2641 " {\n" 2642 " some_more_code();\n" 2643 " another_label:\n" 2644 " some_more_code();\n" 2645 " }\n" 2646 "}"); 2647 verifyFormat("{\n" 2648 " some_code();\n" 2649 "test_label:\n" 2650 " some_other_code();\n" 2651 "}"); 2652 verifyFormat("{\n" 2653 " some_code();\n" 2654 "test_label:;\n" 2655 " int i = 0;\n" 2656 "}"); 2657 FormatStyle Style = getLLVMStyle(); 2658 Style.IndentGotoLabels = false; 2659 verifyFormat("void f() {\n" 2660 " some_code();\n" 2661 "test_label:\n" 2662 " some_other_code();\n" 2663 " {\n" 2664 " some_more_code();\n" 2665 "another_label:\n" 2666 " some_more_code();\n" 2667 " }\n" 2668 "}", 2669 Style); 2670 verifyFormat("{\n" 2671 " some_code();\n" 2672 "test_label:\n" 2673 " some_other_code();\n" 2674 "}", 2675 Style); 2676 verifyFormat("{\n" 2677 " some_code();\n" 2678 "test_label:;\n" 2679 " int i = 0;\n" 2680 "}"); 2681 } 2682 2683 TEST_F(FormatTest, MultiLineControlStatements) { 2684 FormatStyle Style = getLLVMStyle(); 2685 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom; 2686 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine; 2687 Style.ColumnLimit = 20; 2688 // Short lines should keep opening brace on same line. 2689 EXPECT_EQ("if (foo) {\n" 2690 " bar();\n" 2691 "}", 2692 format("if(foo){bar();}", Style)); 2693 EXPECT_EQ("if (foo) {\n" 2694 " bar();\n" 2695 "} else {\n" 2696 " baz();\n" 2697 "}", 2698 format("if(foo){bar();}else{baz();}", Style)); 2699 EXPECT_EQ("if (foo && bar) {\n" 2700 " baz();\n" 2701 "}", 2702 format("if(foo&&bar){baz();}", Style)); 2703 EXPECT_EQ("if (foo) {\n" 2704 " bar();\n" 2705 "} else if (baz) {\n" 2706 " quux();\n" 2707 "}", 2708 format("if(foo){bar();}else if(baz){quux();}", Style)); 2709 EXPECT_EQ( 2710 "if (foo) {\n" 2711 " bar();\n" 2712 "} else if (baz) {\n" 2713 " quux();\n" 2714 "} else {\n" 2715 " foobar();\n" 2716 "}", 2717 format("if(foo){bar();}else if(baz){quux();}else{foobar();}", Style)); 2718 EXPECT_EQ("for (;;) {\n" 2719 " foo();\n" 2720 "}", 2721 format("for(;;){foo();}")); 2722 EXPECT_EQ("while (1) {\n" 2723 " foo();\n" 2724 "}", 2725 format("while(1){foo();}", Style)); 2726 EXPECT_EQ("switch (foo) {\n" 2727 "case bar:\n" 2728 " return;\n" 2729 "}", 2730 format("switch(foo){case bar:return;}", Style)); 2731 EXPECT_EQ("try {\n" 2732 " foo();\n" 2733 "} catch (...) {\n" 2734 " bar();\n" 2735 "}", 2736 format("try{foo();}catch(...){bar();}", Style)); 2737 EXPECT_EQ("do {\n" 2738 " foo();\n" 2739 "} while (bar &&\n" 2740 " baz);", 2741 format("do{foo();}while(bar&&baz);", Style)); 2742 // Long lines should put opening brace on new line. 2743 EXPECT_EQ("if (foo && bar &&\n" 2744 " baz)\n" 2745 "{\n" 2746 " quux();\n" 2747 "}", 2748 format("if(foo&&bar&&baz){quux();}", Style)); 2749 EXPECT_EQ("if (foo && bar &&\n" 2750 " baz)\n" 2751 "{\n" 2752 " quux();\n" 2753 "}", 2754 format("if (foo && bar &&\n" 2755 " baz) {\n" 2756 " quux();\n" 2757 "}", 2758 Style)); 2759 EXPECT_EQ("if (foo) {\n" 2760 " bar();\n" 2761 "} else if (baz ||\n" 2762 " quux)\n" 2763 "{\n" 2764 " foobar();\n" 2765 "}", 2766 format("if(foo){bar();}else if(baz||quux){foobar();}", Style)); 2767 EXPECT_EQ( 2768 "if (foo) {\n" 2769 " bar();\n" 2770 "} else if (baz ||\n" 2771 " quux)\n" 2772 "{\n" 2773 " foobar();\n" 2774 "} else {\n" 2775 " barbaz();\n" 2776 "}", 2777 format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}", 2778 Style)); 2779 EXPECT_EQ("for (int i = 0;\n" 2780 " i < 10; ++i)\n" 2781 "{\n" 2782 " foo();\n" 2783 "}", 2784 format("for(int i=0;i<10;++i){foo();}", Style)); 2785 EXPECT_EQ("foreach (int i,\n" 2786 " list)\n" 2787 "{\n" 2788 " foo();\n" 2789 "}", 2790 format("foreach(int i, list){foo();}", Style)); 2791 Style.ColumnLimit = 2792 40; // to concentrate at brace wrapping, not line wrap due to column limit 2793 EXPECT_EQ("foreach (int i, list) {\n" 2794 " foo();\n" 2795 "}", 2796 format("foreach(int i, list){foo();}", Style)); 2797 Style.ColumnLimit = 2798 20; // to concentrate at brace wrapping, not line wrap due to column limit 2799 EXPECT_EQ("while (foo || bar ||\n" 2800 " baz)\n" 2801 "{\n" 2802 " quux();\n" 2803 "}", 2804 format("while(foo||bar||baz){quux();}", Style)); 2805 EXPECT_EQ("switch (\n" 2806 " foo = barbaz)\n" 2807 "{\n" 2808 "case quux:\n" 2809 " return;\n" 2810 "}", 2811 format("switch(foo=barbaz){case quux:return;}", Style)); 2812 EXPECT_EQ("try {\n" 2813 " foo();\n" 2814 "} catch (\n" 2815 " Exception &bar)\n" 2816 "{\n" 2817 " baz();\n" 2818 "}", 2819 format("try{foo();}catch(Exception&bar){baz();}", Style)); 2820 Style.ColumnLimit = 2821 40; // to concentrate at brace wrapping, not line wrap due to column limit 2822 EXPECT_EQ("try {\n" 2823 " foo();\n" 2824 "} catch (Exception &bar) {\n" 2825 " baz();\n" 2826 "}", 2827 format("try{foo();}catch(Exception&bar){baz();}", Style)); 2828 Style.ColumnLimit = 2829 20; // to concentrate at brace wrapping, not line wrap due to column limit 2830 2831 Style.BraceWrapping.BeforeElse = true; 2832 EXPECT_EQ( 2833 "if (foo) {\n" 2834 " bar();\n" 2835 "}\n" 2836 "else if (baz ||\n" 2837 " quux)\n" 2838 "{\n" 2839 " foobar();\n" 2840 "}\n" 2841 "else {\n" 2842 " barbaz();\n" 2843 "}", 2844 format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}", 2845 Style)); 2846 2847 Style.BraceWrapping.BeforeCatch = true; 2848 EXPECT_EQ("try {\n" 2849 " foo();\n" 2850 "}\n" 2851 "catch (...) {\n" 2852 " baz();\n" 2853 "}", 2854 format("try{foo();}catch(...){baz();}", Style)); 2855 } 2856 2857 TEST_F(FormatTest, BeforeWhile) { 2858 FormatStyle Style = getLLVMStyle(); 2859 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom; 2860 2861 verifyFormat("do {\n" 2862 " foo();\n" 2863 "} while (1);", 2864 Style); 2865 Style.BraceWrapping.BeforeWhile = true; 2866 verifyFormat("do {\n" 2867 " foo();\n" 2868 "}\n" 2869 "while (1);", 2870 Style); 2871 } 2872 2873 //===----------------------------------------------------------------------===// 2874 // Tests for classes, namespaces, etc. 2875 //===----------------------------------------------------------------------===// 2876 2877 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 2878 verifyFormat("class A {};"); 2879 } 2880 2881 TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 2882 verifyFormat("class A {\n" 2883 "public:\n" 2884 "public: // comment\n" 2885 "protected:\n" 2886 "private:\n" 2887 " void f() {}\n" 2888 "};"); 2889 verifyFormat("export class A {\n" 2890 "public:\n" 2891 "public: // comment\n" 2892 "protected:\n" 2893 "private:\n" 2894 " void f() {}\n" 2895 "};"); 2896 verifyGoogleFormat("class A {\n" 2897 " public:\n" 2898 " protected:\n" 2899 " private:\n" 2900 " void f() {}\n" 2901 "};"); 2902 verifyGoogleFormat("export class A {\n" 2903 " public:\n" 2904 " protected:\n" 2905 " private:\n" 2906 " void f() {}\n" 2907 "};"); 2908 verifyFormat("class A {\n" 2909 "public slots:\n" 2910 " void f1() {}\n" 2911 "public Q_SLOTS:\n" 2912 " void f2() {}\n" 2913 "protected slots:\n" 2914 " void f3() {}\n" 2915 "protected Q_SLOTS:\n" 2916 " void f4() {}\n" 2917 "private slots:\n" 2918 " void f5() {}\n" 2919 "private Q_SLOTS:\n" 2920 " void f6() {}\n" 2921 "signals:\n" 2922 " void g1();\n" 2923 "Q_SIGNALS:\n" 2924 " void g2();\n" 2925 "};"); 2926 2927 // Don't interpret 'signals' the wrong way. 2928 verifyFormat("signals.set();"); 2929 verifyFormat("for (Signals signals : f()) {\n}"); 2930 verifyFormat("{\n" 2931 " signals.set(); // This needs indentation.\n" 2932 "}"); 2933 verifyFormat("void f() {\n" 2934 "label:\n" 2935 " signals.baz();\n" 2936 "}"); 2937 } 2938 2939 TEST_F(FormatTest, SeparatesLogicalBlocks) { 2940 EXPECT_EQ("class A {\n" 2941 "public:\n" 2942 " void f();\n" 2943 "\n" 2944 "private:\n" 2945 " void g() {}\n" 2946 " // test\n" 2947 "protected:\n" 2948 " int h;\n" 2949 "};", 2950 format("class A {\n" 2951 "public:\n" 2952 "void f();\n" 2953 "private:\n" 2954 "void g() {}\n" 2955 "// test\n" 2956 "protected:\n" 2957 "int h;\n" 2958 "};")); 2959 EXPECT_EQ("class A {\n" 2960 "protected:\n" 2961 "public:\n" 2962 " void f();\n" 2963 "};", 2964 format("class A {\n" 2965 "protected:\n" 2966 "\n" 2967 "public:\n" 2968 "\n" 2969 " void f();\n" 2970 "};")); 2971 2972 // Even ensure proper spacing inside macros. 2973 EXPECT_EQ("#define B \\\n" 2974 " class A { \\\n" 2975 " protected: \\\n" 2976 " public: \\\n" 2977 " void f(); \\\n" 2978 " };", 2979 format("#define B \\\n" 2980 " class A { \\\n" 2981 " protected: \\\n" 2982 " \\\n" 2983 " public: \\\n" 2984 " \\\n" 2985 " void f(); \\\n" 2986 " };", 2987 getGoogleStyle())); 2988 // But don't remove empty lines after macros ending in access specifiers. 2989 EXPECT_EQ("#define A private:\n" 2990 "\n" 2991 "int i;", 2992 format("#define A private:\n" 2993 "\n" 2994 "int i;")); 2995 } 2996 2997 TEST_F(FormatTest, FormatsClasses) { 2998 verifyFormat("class A : public B {};"); 2999 verifyFormat("class A : public ::B {};"); 3000 3001 verifyFormat( 3002 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3003 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 3004 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 3005 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3006 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 3007 verifyFormat( 3008 "class A : public B, public C, public D, public E, public F {};"); 3009 verifyFormat("class AAAAAAAAAAAA : public B,\n" 3010 " public C,\n" 3011 " public D,\n" 3012 " public E,\n" 3013 " public F,\n" 3014 " public G {};"); 3015 3016 verifyFormat("class\n" 3017 " ReallyReallyLongClassName {\n" 3018 " int i;\n" 3019 "};", 3020 getLLVMStyleWithColumns(32)); 3021 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 3022 " aaaaaaaaaaaaaaaa> {};"); 3023 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" 3024 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n" 3025 " aaaaaaaaaaaaaaaaaaaaaa> {};"); 3026 verifyFormat("template <class R, class C>\n" 3027 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n" 3028 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};"); 3029 verifyFormat("class ::A::B {};"); 3030 } 3031 3032 TEST_F(FormatTest, BreakInheritanceStyle) { 3033 FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle(); 3034 StyleWithInheritanceBreakBeforeComma.BreakInheritanceList = 3035 FormatStyle::BILS_BeforeComma; 3036 verifyFormat("class MyClass : public X {};", 3037 StyleWithInheritanceBreakBeforeComma); 3038 verifyFormat("class MyClass\n" 3039 " : public X\n" 3040 " , public Y {};", 3041 StyleWithInheritanceBreakBeforeComma); 3042 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n" 3043 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n" 3044 " , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", 3045 StyleWithInheritanceBreakBeforeComma); 3046 verifyFormat("struct aaaaaaaaaaaaa\n" 3047 " : public aaaaaaaaaaaaaaaaaaa< // break\n" 3048 " aaaaaaaaaaaaaaaa> {};", 3049 StyleWithInheritanceBreakBeforeComma); 3050 3051 FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle(); 3052 StyleWithInheritanceBreakAfterColon.BreakInheritanceList = 3053 FormatStyle::BILS_AfterColon; 3054 verifyFormat("class MyClass : public X {};", 3055 StyleWithInheritanceBreakAfterColon); 3056 verifyFormat("class MyClass : public X, public Y {};", 3057 StyleWithInheritanceBreakAfterColon); 3058 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n" 3059 " public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3060 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", 3061 StyleWithInheritanceBreakAfterColon); 3062 verifyFormat("struct aaaaaaaaaaaaa :\n" 3063 " public aaaaaaaaaaaaaaaaaaa< // break\n" 3064 " aaaaaaaaaaaaaaaa> {};", 3065 StyleWithInheritanceBreakAfterColon); 3066 3067 FormatStyle StyleWithInheritanceBreakAfterComma = getLLVMStyle(); 3068 StyleWithInheritanceBreakAfterComma.BreakInheritanceList = 3069 FormatStyle::BILS_AfterComma; 3070 verifyFormat("class MyClass : public X {};", 3071 StyleWithInheritanceBreakAfterComma); 3072 verifyFormat("class MyClass : public X,\n" 3073 " public Y {};", 3074 StyleWithInheritanceBreakAfterComma); 3075 verifyFormat( 3076 "class AAAAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3077 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC " 3078 "{};", 3079 StyleWithInheritanceBreakAfterComma); 3080 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 3081 " aaaaaaaaaaaaaaaa> {};", 3082 StyleWithInheritanceBreakAfterComma); 3083 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 3084 " : public OnceBreak,\n" 3085 " public AlwaysBreak,\n" 3086 " EvenBasesFitInOneLine {};", 3087 StyleWithInheritanceBreakAfterComma); 3088 } 3089 3090 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { 3091 verifyFormat("class A {\n} a, b;"); 3092 verifyFormat("struct A {\n} a, b;"); 3093 verifyFormat("union A {\n} a;"); 3094 } 3095 3096 TEST_F(FormatTest, FormatsEnum) { 3097 verifyFormat("enum {\n" 3098 " Zero,\n" 3099 " One = 1,\n" 3100 " Two = One + 1,\n" 3101 " Three = (One + Two),\n" 3102 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3103 " Five = (One, Two, Three, Four, 5)\n" 3104 "};"); 3105 verifyGoogleFormat("enum {\n" 3106 " Zero,\n" 3107 " One = 1,\n" 3108 " Two = One + 1,\n" 3109 " Three = (One + Two),\n" 3110 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3111 " Five = (One, Two, Three, Four, 5)\n" 3112 "};"); 3113 verifyFormat("enum Enum {};"); 3114 verifyFormat("enum {};"); 3115 verifyFormat("enum X E {} d;"); 3116 verifyFormat("enum __attribute__((...)) E {} d;"); 3117 verifyFormat("enum __declspec__((...)) E {} d;"); 3118 verifyFormat("enum {\n" 3119 " Bar = Foo<int, int>::value\n" 3120 "};", 3121 getLLVMStyleWithColumns(30)); 3122 3123 verifyFormat("enum ShortEnum { A, B, C };"); 3124 verifyGoogleFormat("enum ShortEnum { A, B, C };"); 3125 3126 EXPECT_EQ("enum KeepEmptyLines {\n" 3127 " ONE,\n" 3128 "\n" 3129 " TWO,\n" 3130 "\n" 3131 " THREE\n" 3132 "}", 3133 format("enum KeepEmptyLines {\n" 3134 " ONE,\n" 3135 "\n" 3136 " TWO,\n" 3137 "\n" 3138 "\n" 3139 " THREE\n" 3140 "}")); 3141 verifyFormat("enum E { // comment\n" 3142 " ONE,\n" 3143 " TWO\n" 3144 "};\n" 3145 "int i;"); 3146 3147 FormatStyle EightIndent = getLLVMStyle(); 3148 EightIndent.IndentWidth = 8; 3149 verifyFormat("enum {\n" 3150 " VOID,\n" 3151 " CHAR,\n" 3152 " SHORT,\n" 3153 " INT,\n" 3154 " LONG,\n" 3155 " SIGNED,\n" 3156 " UNSIGNED,\n" 3157 " BOOL,\n" 3158 " FLOAT,\n" 3159 " DOUBLE,\n" 3160 " COMPLEX\n" 3161 "};", 3162 EightIndent); 3163 3164 // Not enums. 3165 verifyFormat("enum X f() {\n" 3166 " a();\n" 3167 " return 42;\n" 3168 "}"); 3169 verifyFormat("enum X Type::f() {\n" 3170 " a();\n" 3171 " return 42;\n" 3172 "}"); 3173 verifyFormat("enum ::X f() {\n" 3174 " a();\n" 3175 " return 42;\n" 3176 "}"); 3177 verifyFormat("enum ns::X f() {\n" 3178 " a();\n" 3179 " return 42;\n" 3180 "}"); 3181 } 3182 3183 TEST_F(FormatTest, FormatsEnumsWithErrors) { 3184 verifyFormat("enum Type {\n" 3185 " One = 0; // These semicolons should be commas.\n" 3186 " Two = 1;\n" 3187 "};"); 3188 verifyFormat("namespace n {\n" 3189 "enum Type {\n" 3190 " One,\n" 3191 " Two, // missing };\n" 3192 " int i;\n" 3193 "}\n" 3194 "void g() {}"); 3195 } 3196 3197 TEST_F(FormatTest, FormatsEnumStruct) { 3198 verifyFormat("enum struct {\n" 3199 " Zero,\n" 3200 " One = 1,\n" 3201 " Two = One + 1,\n" 3202 " Three = (One + Two),\n" 3203 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3204 " Five = (One, Two, Three, Four, 5)\n" 3205 "};"); 3206 verifyFormat("enum struct Enum {};"); 3207 verifyFormat("enum struct {};"); 3208 verifyFormat("enum struct X E {} d;"); 3209 verifyFormat("enum struct __attribute__((...)) E {} d;"); 3210 verifyFormat("enum struct __declspec__((...)) E {} d;"); 3211 verifyFormat("enum struct X f() {\n a();\n return 42;\n}"); 3212 } 3213 3214 TEST_F(FormatTest, FormatsEnumClass) { 3215 verifyFormat("enum class {\n" 3216 " Zero,\n" 3217 " One = 1,\n" 3218 " Two = One + 1,\n" 3219 " Three = (One + Two),\n" 3220 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3221 " Five = (One, Two, Three, Four, 5)\n" 3222 "};"); 3223 verifyFormat("enum class Enum {};"); 3224 verifyFormat("enum class {};"); 3225 verifyFormat("enum class X E {} d;"); 3226 verifyFormat("enum class __attribute__((...)) E {} d;"); 3227 verifyFormat("enum class __declspec__((...)) E {} d;"); 3228 verifyFormat("enum class X f() {\n a();\n return 42;\n}"); 3229 } 3230 3231 TEST_F(FormatTest, FormatsEnumTypes) { 3232 verifyFormat("enum X : int {\n" 3233 " A, // Force multiple lines.\n" 3234 " B\n" 3235 "};"); 3236 verifyFormat("enum X : int { A, B };"); 3237 verifyFormat("enum X : std::uint32_t { A, B };"); 3238 } 3239 3240 TEST_F(FormatTest, FormatsTypedefEnum) { 3241 FormatStyle Style = getLLVMStyle(); 3242 Style.ColumnLimit = 40; 3243 verifyFormat("typedef enum {} EmptyEnum;"); 3244 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 3245 verifyFormat("typedef enum {\n" 3246 " ZERO = 0,\n" 3247 " ONE = 1,\n" 3248 " TWO = 2,\n" 3249 " THREE = 3\n" 3250 "} LongEnum;", 3251 Style); 3252 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3253 Style.BraceWrapping.AfterEnum = true; 3254 verifyFormat("typedef enum {} EmptyEnum;"); 3255 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 3256 verifyFormat("typedef enum\n" 3257 "{\n" 3258 " ZERO = 0,\n" 3259 " ONE = 1,\n" 3260 " TWO = 2,\n" 3261 " THREE = 3\n" 3262 "} LongEnum;", 3263 Style); 3264 } 3265 3266 TEST_F(FormatTest, FormatsNSEnums) { 3267 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }"); 3268 verifyGoogleFormat( 3269 "typedef NS_CLOSED_ENUM(NSInteger, SomeName) { AAA, BBB }"); 3270 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n" 3271 " // Information about someDecentlyLongValue.\n" 3272 " someDecentlyLongValue,\n" 3273 " // Information about anotherDecentlyLongValue.\n" 3274 " anotherDecentlyLongValue,\n" 3275 " // Information about aThirdDecentlyLongValue.\n" 3276 " aThirdDecentlyLongValue\n" 3277 "};"); 3278 verifyGoogleFormat("typedef NS_CLOSED_ENUM(NSInteger, MyType) {\n" 3279 " // Information about someDecentlyLongValue.\n" 3280 " someDecentlyLongValue,\n" 3281 " // Information about anotherDecentlyLongValue.\n" 3282 " anotherDecentlyLongValue,\n" 3283 " // Information about aThirdDecentlyLongValue.\n" 3284 " aThirdDecentlyLongValue\n" 3285 "};"); 3286 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n" 3287 " a = 1,\n" 3288 " b = 2,\n" 3289 " c = 3,\n" 3290 "};"); 3291 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n" 3292 " a = 1,\n" 3293 " b = 2,\n" 3294 " c = 3,\n" 3295 "};"); 3296 verifyGoogleFormat("typedef CF_CLOSED_ENUM(NSInteger, MyType) {\n" 3297 " a = 1,\n" 3298 " b = 2,\n" 3299 " c = 3,\n" 3300 "};"); 3301 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n" 3302 " a = 1,\n" 3303 " b = 2,\n" 3304 " c = 3,\n" 3305 "};"); 3306 } 3307 3308 TEST_F(FormatTest, FormatsBitfields) { 3309 verifyFormat("struct Bitfields {\n" 3310 " unsigned sClass : 8;\n" 3311 " unsigned ValueKind : 2;\n" 3312 "};"); 3313 verifyFormat("struct A {\n" 3314 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" 3315 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" 3316 "};"); 3317 verifyFormat("struct MyStruct {\n" 3318 " uchar data;\n" 3319 " uchar : 8;\n" 3320 " uchar : 8;\n" 3321 " uchar other;\n" 3322 "};"); 3323 FormatStyle Style = getLLVMStyle(); 3324 Style.BitFieldColonSpacing = FormatStyle::BFCS_None; 3325 verifyFormat("struct Bitfields {\n" 3326 " unsigned sClass:8;\n" 3327 " unsigned ValueKind:2;\n" 3328 " uchar other;\n" 3329 "};", 3330 Style); 3331 verifyFormat("struct A {\n" 3332 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1,\n" 3333 " bbbbbbbbbbbbbbbbbbbbbbbbb:2;\n" 3334 "};", 3335 Style); 3336 Style.BitFieldColonSpacing = FormatStyle::BFCS_Before; 3337 verifyFormat("struct Bitfields {\n" 3338 " unsigned sClass :8;\n" 3339 " unsigned ValueKind :2;\n" 3340 " uchar other;\n" 3341 "};", 3342 Style); 3343 Style.BitFieldColonSpacing = FormatStyle::BFCS_After; 3344 verifyFormat("struct Bitfields {\n" 3345 " unsigned sClass: 8;\n" 3346 " unsigned ValueKind: 2;\n" 3347 " uchar other;\n" 3348 "};", 3349 Style); 3350 } 3351 3352 TEST_F(FormatTest, FormatsNamespaces) { 3353 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 3354 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 3355 3356 verifyFormat("namespace some_namespace {\n" 3357 "class A {};\n" 3358 "void f() { f(); }\n" 3359 "}", 3360 LLVMWithNoNamespaceFix); 3361 verifyFormat("namespace N::inline D {\n" 3362 "class A {};\n" 3363 "void f() { f(); }\n" 3364 "}", 3365 LLVMWithNoNamespaceFix); 3366 verifyFormat("namespace N::inline D::E {\n" 3367 "class A {};\n" 3368 "void f() { f(); }\n" 3369 "}", 3370 LLVMWithNoNamespaceFix); 3371 verifyFormat("namespace [[deprecated(\"foo[bar\")]] some_namespace {\n" 3372 "class A {};\n" 3373 "void f() { f(); }\n" 3374 "}", 3375 LLVMWithNoNamespaceFix); 3376 verifyFormat("/* something */ namespace some_namespace {\n" 3377 "class A {};\n" 3378 "void f() { f(); }\n" 3379 "}", 3380 LLVMWithNoNamespaceFix); 3381 verifyFormat("namespace {\n" 3382 "class A {};\n" 3383 "void f() { f(); }\n" 3384 "}", 3385 LLVMWithNoNamespaceFix); 3386 verifyFormat("/* something */ namespace {\n" 3387 "class A {};\n" 3388 "void f() { f(); }\n" 3389 "}", 3390 LLVMWithNoNamespaceFix); 3391 verifyFormat("inline namespace X {\n" 3392 "class A {};\n" 3393 "void f() { f(); }\n" 3394 "}", 3395 LLVMWithNoNamespaceFix); 3396 verifyFormat("/* something */ inline namespace X {\n" 3397 "class A {};\n" 3398 "void f() { f(); }\n" 3399 "}", 3400 LLVMWithNoNamespaceFix); 3401 verifyFormat("export namespace X {\n" 3402 "class A {};\n" 3403 "void f() { f(); }\n" 3404 "}", 3405 LLVMWithNoNamespaceFix); 3406 verifyFormat("using namespace some_namespace;\n" 3407 "class A {};\n" 3408 "void f() { f(); }", 3409 LLVMWithNoNamespaceFix); 3410 3411 // This code is more common than we thought; if we 3412 // layout this correctly the semicolon will go into 3413 // its own line, which is undesirable. 3414 verifyFormat("namespace {};", LLVMWithNoNamespaceFix); 3415 verifyFormat("namespace {\n" 3416 "class A {};\n" 3417 "};", 3418 LLVMWithNoNamespaceFix); 3419 3420 verifyFormat("namespace {\n" 3421 "int SomeVariable = 0; // comment\n" 3422 "} // namespace", 3423 LLVMWithNoNamespaceFix); 3424 EXPECT_EQ("#ifndef HEADER_GUARD\n" 3425 "#define HEADER_GUARD\n" 3426 "namespace my_namespace {\n" 3427 "int i;\n" 3428 "} // my_namespace\n" 3429 "#endif // HEADER_GUARD", 3430 format("#ifndef HEADER_GUARD\n" 3431 " #define HEADER_GUARD\n" 3432 " namespace my_namespace {\n" 3433 "int i;\n" 3434 "} // my_namespace\n" 3435 "#endif // HEADER_GUARD", 3436 LLVMWithNoNamespaceFix)); 3437 3438 EXPECT_EQ("namespace A::B {\n" 3439 "class C {};\n" 3440 "}", 3441 format("namespace A::B {\n" 3442 "class C {};\n" 3443 "}", 3444 LLVMWithNoNamespaceFix)); 3445 3446 FormatStyle Style = getLLVMStyle(); 3447 Style.NamespaceIndentation = FormatStyle::NI_All; 3448 EXPECT_EQ("namespace out {\n" 3449 " int i;\n" 3450 " namespace in {\n" 3451 " int i;\n" 3452 " } // namespace in\n" 3453 "} // namespace out", 3454 format("namespace out {\n" 3455 "int i;\n" 3456 "namespace in {\n" 3457 "int i;\n" 3458 "} // namespace in\n" 3459 "} // namespace out", 3460 Style)); 3461 3462 Style.NamespaceIndentation = FormatStyle::NI_Inner; 3463 EXPECT_EQ("namespace out {\n" 3464 "int i;\n" 3465 "namespace in {\n" 3466 " int i;\n" 3467 "} // namespace in\n" 3468 "} // namespace out", 3469 format("namespace out {\n" 3470 "int i;\n" 3471 "namespace in {\n" 3472 "int i;\n" 3473 "} // namespace in\n" 3474 "} // namespace out", 3475 Style)); 3476 } 3477 3478 TEST_F(FormatTest, NamespaceMacros) { 3479 FormatStyle Style = getLLVMStyle(); 3480 Style.NamespaceMacros.push_back("TESTSUITE"); 3481 3482 verifyFormat("TESTSUITE(A) {\n" 3483 "int foo();\n" 3484 "} // TESTSUITE(A)", 3485 Style); 3486 3487 verifyFormat("TESTSUITE(A, B) {\n" 3488 "int foo();\n" 3489 "} // TESTSUITE(A)", 3490 Style); 3491 3492 // Properly indent according to NamespaceIndentation style 3493 Style.NamespaceIndentation = FormatStyle::NI_All; 3494 verifyFormat("TESTSUITE(A) {\n" 3495 " int foo();\n" 3496 "} // TESTSUITE(A)", 3497 Style); 3498 verifyFormat("TESTSUITE(A) {\n" 3499 " namespace B {\n" 3500 " int foo();\n" 3501 " } // namespace B\n" 3502 "} // TESTSUITE(A)", 3503 Style); 3504 verifyFormat("namespace A {\n" 3505 " TESTSUITE(B) {\n" 3506 " int foo();\n" 3507 " } // TESTSUITE(B)\n" 3508 "} // namespace A", 3509 Style); 3510 3511 Style.NamespaceIndentation = FormatStyle::NI_Inner; 3512 verifyFormat("TESTSUITE(A) {\n" 3513 "TESTSUITE(B) {\n" 3514 " int foo();\n" 3515 "} // TESTSUITE(B)\n" 3516 "} // TESTSUITE(A)", 3517 Style); 3518 verifyFormat("TESTSUITE(A) {\n" 3519 "namespace B {\n" 3520 " int foo();\n" 3521 "} // namespace B\n" 3522 "} // TESTSUITE(A)", 3523 Style); 3524 verifyFormat("namespace A {\n" 3525 "TESTSUITE(B) {\n" 3526 " int foo();\n" 3527 "} // TESTSUITE(B)\n" 3528 "} // namespace A", 3529 Style); 3530 3531 // Properly merge namespace-macros blocks in CompactNamespaces mode 3532 Style.NamespaceIndentation = FormatStyle::NI_None; 3533 Style.CompactNamespaces = true; 3534 verifyFormat("TESTSUITE(A) { TESTSUITE(B) {\n" 3535 "}} // TESTSUITE(A::B)", 3536 Style); 3537 3538 EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n" 3539 "}} // TESTSUITE(out::in)", 3540 format("TESTSUITE(out) {\n" 3541 "TESTSUITE(in) {\n" 3542 "} // TESTSUITE(in)\n" 3543 "} // TESTSUITE(out)", 3544 Style)); 3545 3546 EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n" 3547 "}} // TESTSUITE(out::in)", 3548 format("TESTSUITE(out) {\n" 3549 "TESTSUITE(in) {\n" 3550 "} // TESTSUITE(in)\n" 3551 "} // TESTSUITE(out)", 3552 Style)); 3553 3554 // Do not merge different namespaces/macros 3555 EXPECT_EQ("namespace out {\n" 3556 "TESTSUITE(in) {\n" 3557 "} // TESTSUITE(in)\n" 3558 "} // namespace out", 3559 format("namespace out {\n" 3560 "TESTSUITE(in) {\n" 3561 "} // TESTSUITE(in)\n" 3562 "} // namespace out", 3563 Style)); 3564 EXPECT_EQ("TESTSUITE(out) {\n" 3565 "namespace in {\n" 3566 "} // namespace in\n" 3567 "} // TESTSUITE(out)", 3568 format("TESTSUITE(out) {\n" 3569 "namespace in {\n" 3570 "} // namespace in\n" 3571 "} // TESTSUITE(out)", 3572 Style)); 3573 Style.NamespaceMacros.push_back("FOOBAR"); 3574 EXPECT_EQ("TESTSUITE(out) {\n" 3575 "FOOBAR(in) {\n" 3576 "} // FOOBAR(in)\n" 3577 "} // TESTSUITE(out)", 3578 format("TESTSUITE(out) {\n" 3579 "FOOBAR(in) {\n" 3580 "} // FOOBAR(in)\n" 3581 "} // TESTSUITE(out)", 3582 Style)); 3583 } 3584 3585 TEST_F(FormatTest, FormatsCompactNamespaces) { 3586 FormatStyle Style = getLLVMStyle(); 3587 Style.CompactNamespaces = true; 3588 Style.NamespaceMacros.push_back("TESTSUITE"); 3589 3590 verifyFormat("namespace A { namespace B {\n" 3591 "}} // namespace A::B", 3592 Style); 3593 3594 EXPECT_EQ("namespace out { namespace in {\n" 3595 "}} // namespace out::in", 3596 format("namespace out {\n" 3597 "namespace in {\n" 3598 "} // namespace in\n" 3599 "} // namespace out", 3600 Style)); 3601 3602 // Only namespaces which have both consecutive opening and end get compacted 3603 EXPECT_EQ("namespace out {\n" 3604 "namespace in1 {\n" 3605 "} // namespace in1\n" 3606 "namespace in2 {\n" 3607 "} // namespace in2\n" 3608 "} // namespace out", 3609 format("namespace out {\n" 3610 "namespace in1 {\n" 3611 "} // namespace in1\n" 3612 "namespace in2 {\n" 3613 "} // namespace in2\n" 3614 "} // namespace out", 3615 Style)); 3616 3617 EXPECT_EQ("namespace out {\n" 3618 "int i;\n" 3619 "namespace in {\n" 3620 "int j;\n" 3621 "} // namespace in\n" 3622 "int k;\n" 3623 "} // namespace out", 3624 format("namespace out { int i;\n" 3625 "namespace in { int j; } // namespace in\n" 3626 "int k; } // namespace out", 3627 Style)); 3628 3629 EXPECT_EQ("namespace A { namespace B { namespace C {\n" 3630 "}}} // namespace A::B::C\n", 3631 format("namespace A { namespace B {\n" 3632 "namespace C {\n" 3633 "}} // namespace B::C\n" 3634 "} // namespace A\n", 3635 Style)); 3636 3637 Style.ColumnLimit = 40; 3638 EXPECT_EQ("namespace aaaaaaaaaa {\n" 3639 "namespace bbbbbbbbbb {\n" 3640 "}} // namespace aaaaaaaaaa::bbbbbbbbbb", 3641 format("namespace aaaaaaaaaa {\n" 3642 "namespace bbbbbbbbbb {\n" 3643 "} // namespace bbbbbbbbbb\n" 3644 "} // namespace aaaaaaaaaa", 3645 Style)); 3646 3647 EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n" 3648 "namespace cccccc {\n" 3649 "}}} // namespace aaaaaa::bbbbbb::cccccc", 3650 format("namespace aaaaaa {\n" 3651 "namespace bbbbbb {\n" 3652 "namespace cccccc {\n" 3653 "} // namespace cccccc\n" 3654 "} // namespace bbbbbb\n" 3655 "} // namespace aaaaaa", 3656 Style)); 3657 Style.ColumnLimit = 80; 3658 3659 // Extra semicolon after 'inner' closing brace prevents merging 3660 EXPECT_EQ("namespace out { namespace in {\n" 3661 "}; } // namespace out::in", 3662 format("namespace out {\n" 3663 "namespace in {\n" 3664 "}; // namespace in\n" 3665 "} // namespace out", 3666 Style)); 3667 3668 // Extra semicolon after 'outer' closing brace is conserved 3669 EXPECT_EQ("namespace out { namespace in {\n" 3670 "}}; // namespace out::in", 3671 format("namespace out {\n" 3672 "namespace in {\n" 3673 "} // namespace in\n" 3674 "}; // namespace out", 3675 Style)); 3676 3677 Style.NamespaceIndentation = FormatStyle::NI_All; 3678 EXPECT_EQ("namespace out { namespace in {\n" 3679 " int i;\n" 3680 "}} // namespace out::in", 3681 format("namespace out {\n" 3682 "namespace in {\n" 3683 "int i;\n" 3684 "} // namespace in\n" 3685 "} // namespace out", 3686 Style)); 3687 EXPECT_EQ("namespace out { namespace mid {\n" 3688 " namespace in {\n" 3689 " int j;\n" 3690 " } // namespace in\n" 3691 " int k;\n" 3692 "}} // namespace out::mid", 3693 format("namespace out { namespace mid {\n" 3694 "namespace in { int j; } // namespace in\n" 3695 "int k; }} // namespace out::mid", 3696 Style)); 3697 3698 Style.NamespaceIndentation = FormatStyle::NI_Inner; 3699 EXPECT_EQ("namespace out { namespace in {\n" 3700 " int i;\n" 3701 "}} // namespace out::in", 3702 format("namespace out {\n" 3703 "namespace in {\n" 3704 "int i;\n" 3705 "} // namespace in\n" 3706 "} // namespace out", 3707 Style)); 3708 EXPECT_EQ("namespace out { namespace mid { namespace in {\n" 3709 " int i;\n" 3710 "}}} // namespace out::mid::in", 3711 format("namespace out {\n" 3712 "namespace mid {\n" 3713 "namespace in {\n" 3714 "int i;\n" 3715 "} // namespace in\n" 3716 "} // namespace mid\n" 3717 "} // namespace out", 3718 Style)); 3719 } 3720 3721 TEST_F(FormatTest, FormatsExternC) { 3722 verifyFormat("extern \"C\" {\nint a;"); 3723 verifyFormat("extern \"C\" {}"); 3724 verifyFormat("extern \"C\" {\n" 3725 "int foo();\n" 3726 "}"); 3727 verifyFormat("extern \"C\" int foo() {}"); 3728 verifyFormat("extern \"C\" int foo();"); 3729 verifyFormat("extern \"C\" int foo() {\n" 3730 " int i = 42;\n" 3731 " return i;\n" 3732 "}"); 3733 3734 FormatStyle Style = getLLVMStyle(); 3735 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3736 Style.BraceWrapping.AfterFunction = true; 3737 verifyFormat("extern \"C\" int foo() {}", Style); 3738 verifyFormat("extern \"C\" int foo();", Style); 3739 verifyFormat("extern \"C\" int foo()\n" 3740 "{\n" 3741 " int i = 42;\n" 3742 " return i;\n" 3743 "}", 3744 Style); 3745 3746 Style.BraceWrapping.AfterExternBlock = true; 3747 Style.BraceWrapping.SplitEmptyRecord = false; 3748 verifyFormat("extern \"C\"\n" 3749 "{}", 3750 Style); 3751 verifyFormat("extern \"C\"\n" 3752 "{\n" 3753 " int foo();\n" 3754 "}", 3755 Style); 3756 } 3757 3758 TEST_F(FormatTest, IndentExternBlockStyle) { 3759 FormatStyle Style = getLLVMStyle(); 3760 Style.IndentWidth = 2; 3761 3762 Style.IndentExternBlock = FormatStyle::IEBS_Indent; 3763 verifyFormat("extern \"C\" { /*9*/\n}", Style); 3764 verifyFormat("extern \"C\" {\n" 3765 " int foo10();\n" 3766 "}", 3767 Style); 3768 3769 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; 3770 verifyFormat("extern \"C\" { /*11*/\n}", Style); 3771 verifyFormat("extern \"C\" {\n" 3772 "int foo12();\n" 3773 "}", 3774 Style); 3775 3776 Style.IndentExternBlock = FormatStyle::IEBS_AfterExternBlock; 3777 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3778 Style.BraceWrapping.AfterExternBlock = true; 3779 verifyFormat("extern \"C\"\n{ /*13*/\n}", Style); 3780 verifyFormat("extern \"C\"\n{\n" 3781 " int foo14();\n" 3782 "}", 3783 Style); 3784 3785 Style.IndentExternBlock = FormatStyle::IEBS_AfterExternBlock; 3786 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3787 Style.BraceWrapping.AfterExternBlock = false; 3788 verifyFormat("extern \"C\" { /*15*/\n}", Style); 3789 verifyFormat("extern \"C\" {\n" 3790 "int foo16();\n" 3791 "}", 3792 Style); 3793 } 3794 3795 TEST_F(FormatTest, FormatsInlineASM) { 3796 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); 3797 verifyFormat("asm(\"nop\" ::: \"memory\");"); 3798 verifyFormat( 3799 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 3800 " \"cpuid\\n\\t\"\n" 3801 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 3802 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" 3803 " : \"a\"(value));"); 3804 EXPECT_EQ( 3805 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 3806 " __asm {\n" 3807 " mov edx,[that] // vtable in edx\n" 3808 " mov eax,methodIndex\n" 3809 " call [edx][eax*4] // stdcall\n" 3810 " }\n" 3811 "}", 3812 format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 3813 " __asm {\n" 3814 " mov edx,[that] // vtable in edx\n" 3815 " mov eax,methodIndex\n" 3816 " call [edx][eax*4] // stdcall\n" 3817 " }\n" 3818 "}")); 3819 EXPECT_EQ("_asm {\n" 3820 " xor eax, eax;\n" 3821 " cpuid;\n" 3822 "}", 3823 format("_asm {\n" 3824 " xor eax, eax;\n" 3825 " cpuid;\n" 3826 "}")); 3827 verifyFormat("void function() {\n" 3828 " // comment\n" 3829 " asm(\"\");\n" 3830 "}"); 3831 EXPECT_EQ("__asm {\n" 3832 "}\n" 3833 "int i;", 3834 format("__asm {\n" 3835 "}\n" 3836 "int i;")); 3837 } 3838 3839 TEST_F(FormatTest, FormatTryCatch) { 3840 verifyFormat("try {\n" 3841 " throw a * b;\n" 3842 "} catch (int a) {\n" 3843 " // Do nothing.\n" 3844 "} catch (...) {\n" 3845 " exit(42);\n" 3846 "}"); 3847 3848 // Function-level try statements. 3849 verifyFormat("int f() try { return 4; } catch (...) {\n" 3850 " return 5;\n" 3851 "}"); 3852 verifyFormat("class A {\n" 3853 " int a;\n" 3854 " A() try : a(0) {\n" 3855 " } catch (...) {\n" 3856 " throw;\n" 3857 " }\n" 3858 "};\n"); 3859 verifyFormat("class A {\n" 3860 " int a;\n" 3861 " A() try : a(0), b{1} {\n" 3862 " } catch (...) {\n" 3863 " throw;\n" 3864 " }\n" 3865 "};\n"); 3866 verifyFormat("class A {\n" 3867 " int a;\n" 3868 " A() try : a(0), b{1}, c{2} {\n" 3869 " } catch (...) {\n" 3870 " throw;\n" 3871 " }\n" 3872 "};\n"); 3873 verifyFormat("class A {\n" 3874 " int a;\n" 3875 " A() try : a(0), b{1}, c{2} {\n" 3876 " { // New scope.\n" 3877 " }\n" 3878 " } catch (...) {\n" 3879 " throw;\n" 3880 " }\n" 3881 "};\n"); 3882 3883 // Incomplete try-catch blocks. 3884 verifyIncompleteFormat("try {} catch ("); 3885 } 3886 3887 TEST_F(FormatTest, FormatTryAsAVariable) { 3888 verifyFormat("int try;"); 3889 verifyFormat("int try, size;"); 3890 verifyFormat("try = foo();"); 3891 verifyFormat("if (try < size) {\n return true;\n}"); 3892 3893 verifyFormat("int catch;"); 3894 verifyFormat("int catch, size;"); 3895 verifyFormat("catch = foo();"); 3896 verifyFormat("if (catch < size) {\n return true;\n}"); 3897 3898 FormatStyle Style = getLLVMStyle(); 3899 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3900 Style.BraceWrapping.AfterFunction = true; 3901 Style.BraceWrapping.BeforeCatch = true; 3902 verifyFormat("try {\n" 3903 " int bar = 1;\n" 3904 "}\n" 3905 "catch (...) {\n" 3906 " int bar = 1;\n" 3907 "}", 3908 Style); 3909 verifyFormat("#if NO_EX\n" 3910 "try\n" 3911 "#endif\n" 3912 "{\n" 3913 "}\n" 3914 "#if NO_EX\n" 3915 "catch (...) {\n" 3916 "}", 3917 Style); 3918 verifyFormat("try /* abc */ {\n" 3919 " int bar = 1;\n" 3920 "}\n" 3921 "catch (...) {\n" 3922 " int bar = 1;\n" 3923 "}", 3924 Style); 3925 verifyFormat("try\n" 3926 "// abc\n" 3927 "{\n" 3928 " int bar = 1;\n" 3929 "}\n" 3930 "catch (...) {\n" 3931 " int bar = 1;\n" 3932 "}", 3933 Style); 3934 } 3935 3936 TEST_F(FormatTest, FormatSEHTryCatch) { 3937 verifyFormat("__try {\n" 3938 " int a = b * c;\n" 3939 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n" 3940 " // Do nothing.\n" 3941 "}"); 3942 3943 verifyFormat("__try {\n" 3944 " int a = b * c;\n" 3945 "} __finally {\n" 3946 " // Do nothing.\n" 3947 "}"); 3948 3949 verifyFormat("DEBUG({\n" 3950 " __try {\n" 3951 " } __finally {\n" 3952 " }\n" 3953 "});\n"); 3954 } 3955 3956 TEST_F(FormatTest, IncompleteTryCatchBlocks) { 3957 verifyFormat("try {\n" 3958 " f();\n" 3959 "} catch {\n" 3960 " g();\n" 3961 "}"); 3962 verifyFormat("try {\n" 3963 " f();\n" 3964 "} catch (A a) MACRO(x) {\n" 3965 " g();\n" 3966 "} catch (B b) MACRO(x) {\n" 3967 " g();\n" 3968 "}"); 3969 } 3970 3971 TEST_F(FormatTest, FormatTryCatchBraceStyles) { 3972 FormatStyle Style = getLLVMStyle(); 3973 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, 3974 FormatStyle::BS_WebKit}) { 3975 Style.BreakBeforeBraces = BraceStyle; 3976 verifyFormat("try {\n" 3977 " // something\n" 3978 "} catch (...) {\n" 3979 " // something\n" 3980 "}", 3981 Style); 3982 } 3983 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 3984 verifyFormat("try {\n" 3985 " // something\n" 3986 "}\n" 3987 "catch (...) {\n" 3988 " // something\n" 3989 "}", 3990 Style); 3991 verifyFormat("__try {\n" 3992 " // something\n" 3993 "}\n" 3994 "__finally {\n" 3995 " // something\n" 3996 "}", 3997 Style); 3998 verifyFormat("@try {\n" 3999 " // something\n" 4000 "}\n" 4001 "@finally {\n" 4002 " // something\n" 4003 "}", 4004 Style); 4005 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 4006 verifyFormat("try\n" 4007 "{\n" 4008 " // something\n" 4009 "}\n" 4010 "catch (...)\n" 4011 "{\n" 4012 " // something\n" 4013 "}", 4014 Style); 4015 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 4016 verifyFormat("try\n" 4017 " {\n" 4018 " // something white\n" 4019 " }\n" 4020 "catch (...)\n" 4021 " {\n" 4022 " // something white\n" 4023 " }", 4024 Style); 4025 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 4026 verifyFormat("try\n" 4027 " {\n" 4028 " // something\n" 4029 " }\n" 4030 "catch (...)\n" 4031 " {\n" 4032 " // something\n" 4033 " }", 4034 Style); 4035 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4036 Style.BraceWrapping.BeforeCatch = true; 4037 verifyFormat("try {\n" 4038 " // something\n" 4039 "}\n" 4040 "catch (...) {\n" 4041 " // something\n" 4042 "}", 4043 Style); 4044 } 4045 4046 TEST_F(FormatTest, StaticInitializers) { 4047 verifyFormat("static SomeClass SC = {1, 'a'};"); 4048 4049 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" 4050 " 100000000, " 4051 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); 4052 4053 // Here, everything other than the "}" would fit on a line. 4054 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" 4055 " 10000000000000000000000000};"); 4056 EXPECT_EQ("S s = {a,\n" 4057 "\n" 4058 " b};", 4059 format("S s = {\n" 4060 " a,\n" 4061 "\n" 4062 " b\n" 4063 "};")); 4064 4065 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first 4066 // line. However, the formatting looks a bit off and this probably doesn't 4067 // happen often in practice. 4068 verifyFormat("static int Variable[1] = {\n" 4069 " {1000000000000000000000000000000000000}};", 4070 getLLVMStyleWithColumns(40)); 4071 } 4072 4073 TEST_F(FormatTest, DesignatedInitializers) { 4074 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 4075 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" 4076 " .bbbbbbbbbb = 2,\n" 4077 " .cccccccccc = 3,\n" 4078 " .dddddddddd = 4,\n" 4079 " .eeeeeeeeee = 5};"); 4080 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 4081 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" 4082 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" 4083 " .ccccccccccccccccccccccccccc = 3,\n" 4084 " .ddddddddddddddddddddddddddd = 4,\n" 4085 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); 4086 4087 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); 4088 4089 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 4090 verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n" 4091 " [2] = bbbbbbbbbb,\n" 4092 " [3] = cccccccccc,\n" 4093 " [4] = dddddddddd,\n" 4094 " [5] = eeeeeeeeee};"); 4095 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 4096 " [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4097 " [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 4098 " [3] = cccccccccccccccccccccccccccccccccccccc,\n" 4099 " [4] = dddddddddddddddddddddddddddddddddddddd,\n" 4100 " [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};"); 4101 } 4102 4103 TEST_F(FormatTest, NestedStaticInitializers) { 4104 verifyFormat("static A x = {{{}}};\n"); 4105 verifyFormat("static A x = {{{init1, init2, init3, init4},\n" 4106 " {init1, init2, init3, init4}}};", 4107 getLLVMStyleWithColumns(50)); 4108 4109 verifyFormat("somes Status::global_reps[3] = {\n" 4110 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 4111 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 4112 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", 4113 getLLVMStyleWithColumns(60)); 4114 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n" 4115 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 4116 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 4117 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};"); 4118 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" 4119 " {rect.fRight - rect.fLeft, rect.fBottom - " 4120 "rect.fTop}};"); 4121 4122 verifyFormat( 4123 "SomeArrayOfSomeType a = {\n" 4124 " {{1, 2, 3},\n" 4125 " {1, 2, 3},\n" 4126 " {111111111111111111111111111111, 222222222222222222222222222222,\n" 4127 " 333333333333333333333333333333},\n" 4128 " {1, 2, 3},\n" 4129 " {1, 2, 3}}};"); 4130 verifyFormat( 4131 "SomeArrayOfSomeType a = {\n" 4132 " {{1, 2, 3}},\n" 4133 " {{1, 2, 3}},\n" 4134 " {{111111111111111111111111111111, 222222222222222222222222222222,\n" 4135 " 333333333333333333333333333333}},\n" 4136 " {{1, 2, 3}},\n" 4137 " {{1, 2, 3}}};"); 4138 4139 verifyFormat("struct {\n" 4140 " unsigned bit;\n" 4141 " const char *const name;\n" 4142 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" 4143 " {kOsWin, \"Windows\"},\n" 4144 " {kOsLinux, \"Linux\"},\n" 4145 " {kOsCrOS, \"Chrome OS\"}};"); 4146 verifyFormat("struct {\n" 4147 " unsigned bit;\n" 4148 " const char *const name;\n" 4149 "} kBitsToOs[] = {\n" 4150 " {kOsMac, \"Mac\"},\n" 4151 " {kOsWin, \"Windows\"},\n" 4152 " {kOsLinux, \"Linux\"},\n" 4153 " {kOsCrOS, \"Chrome OS\"},\n" 4154 "};"); 4155 } 4156 4157 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 4158 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 4159 " \\\n" 4160 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 4161 } 4162 4163 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 4164 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 4165 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 4166 4167 // Do break defaulted and deleted functions. 4168 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 4169 " default;", 4170 getLLVMStyleWithColumns(40)); 4171 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 4172 " delete;", 4173 getLLVMStyleWithColumns(40)); 4174 } 4175 4176 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { 4177 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3", 4178 getLLVMStyleWithColumns(40)); 4179 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 4180 getLLVMStyleWithColumns(40)); 4181 EXPECT_EQ("#define Q \\\n" 4182 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" 4183 " \"aaaaaaaa.cpp\"", 4184 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 4185 getLLVMStyleWithColumns(40))); 4186 } 4187 4188 TEST_F(FormatTest, UnderstandsLinePPDirective) { 4189 EXPECT_EQ("# 123 \"A string literal\"", 4190 format(" # 123 \"A string literal\"")); 4191 } 4192 4193 TEST_F(FormatTest, LayoutUnknownPPDirective) { 4194 EXPECT_EQ("#;", format("#;")); 4195 verifyFormat("#\n;\n;\n;"); 4196 } 4197 4198 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 4199 EXPECT_EQ("#line 42 \"test\"\n", 4200 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 4201 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", 4202 getLLVMStyleWithColumns(12))); 4203 } 4204 4205 TEST_F(FormatTest, EndOfFileEndsPPDirective) { 4206 EXPECT_EQ("#line 42 \"test\"", 4207 format("# \\\n line \\\n 42 \\\n \"test\"")); 4208 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); 4209 } 4210 4211 TEST_F(FormatTest, DoesntRemoveUnknownTokens) { 4212 verifyFormat("#define A \\x20"); 4213 verifyFormat("#define A \\ x20"); 4214 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20")); 4215 verifyFormat("#define A ''"); 4216 verifyFormat("#define A ''qqq"); 4217 verifyFormat("#define A `qqq"); 4218 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); 4219 EXPECT_EQ("const char *c = STRINGIFY(\n" 4220 "\\na : b);", 4221 format("const char * c = STRINGIFY(\n" 4222 "\\na : b);")); 4223 4224 verifyFormat("a\r\\"); 4225 verifyFormat("a\v\\"); 4226 verifyFormat("a\f\\"); 4227 } 4228 4229 TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) { 4230 FormatStyle style = getChromiumStyle(FormatStyle::LK_Cpp); 4231 style.IndentWidth = 4; 4232 style.PPIndentWidth = 1; 4233 4234 style.IndentPPDirectives = FormatStyle::PPDIS_None; 4235 verifyFormat("#ifdef __linux__\n" 4236 "void foo() {\n" 4237 " int x = 0;\n" 4238 "}\n" 4239 "#define FOO\n" 4240 "#endif\n" 4241 "void bar() {\n" 4242 " int y = 0;\n" 4243 "}\n", 4244 style); 4245 4246 style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 4247 verifyFormat("#ifdef __linux__\n" 4248 "void foo() {\n" 4249 " int x = 0;\n" 4250 "}\n" 4251 "# define FOO foo\n" 4252 "#endif\n" 4253 "void bar() {\n" 4254 " int y = 0;\n" 4255 "}\n", 4256 style); 4257 4258 style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash; 4259 verifyFormat("#ifdef __linux__\n" 4260 "void foo() {\n" 4261 " int x = 0;\n" 4262 "}\n" 4263 " #define FOO foo\n" 4264 "#endif\n" 4265 "void bar() {\n" 4266 " int y = 0;\n" 4267 "}\n", 4268 style); 4269 } 4270 4271 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 4272 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 4273 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 4274 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 4275 // FIXME: We never break before the macro name. 4276 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12)); 4277 4278 verifyFormat("#define A A\n#define A A"); 4279 verifyFormat("#define A(X) A\n#define A A"); 4280 4281 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 4282 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 4283 } 4284 4285 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 4286 EXPECT_EQ("// somecomment\n" 4287 "#include \"a.h\"\n" 4288 "#define A( \\\n" 4289 " A, B)\n" 4290 "#include \"b.h\"\n" 4291 "// somecomment\n", 4292 format(" // somecomment\n" 4293 " #include \"a.h\"\n" 4294 "#define A(A,\\\n" 4295 " B)\n" 4296 " #include \"b.h\"\n" 4297 " // somecomment\n", 4298 getLLVMStyleWithColumns(13))); 4299 } 4300 4301 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } 4302 4303 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 4304 EXPECT_EQ("#define A \\\n" 4305 " c; \\\n" 4306 " e;\n" 4307 "f;", 4308 format("#define A c; e;\n" 4309 "f;", 4310 getLLVMStyleWithColumns(14))); 4311 } 4312 4313 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } 4314 4315 TEST_F(FormatTest, MacroDefinitionInsideStatement) { 4316 EXPECT_EQ("int x,\n" 4317 "#define A\n" 4318 " y;", 4319 format("int x,\n#define A\ny;")); 4320 } 4321 4322 TEST_F(FormatTest, HashInMacroDefinition) { 4323 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle())); 4324 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 4325 verifyFormat("#define A \\\n" 4326 " { \\\n" 4327 " f(#c); \\\n" 4328 " }", 4329 getLLVMStyleWithColumns(11)); 4330 4331 verifyFormat("#define A(X) \\\n" 4332 " void function##X()", 4333 getLLVMStyleWithColumns(22)); 4334 4335 verifyFormat("#define A(a, b, c) \\\n" 4336 " void a##b##c()", 4337 getLLVMStyleWithColumns(22)); 4338 4339 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 4340 } 4341 4342 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 4343 EXPECT_EQ("#define A (x)", format("#define A (x)")); 4344 EXPECT_EQ("#define A(x)", format("#define A(x)")); 4345 4346 FormatStyle Style = getLLVMStyle(); 4347 Style.SpaceBeforeParens = FormatStyle::SBPO_Never; 4348 verifyFormat("#define true ((foo)1)", Style); 4349 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 4350 verifyFormat("#define false((foo)0)", Style); 4351 } 4352 4353 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 4354 EXPECT_EQ("#define A b;", format("#define A \\\n" 4355 " \\\n" 4356 " b;", 4357 getLLVMStyleWithColumns(25))); 4358 EXPECT_EQ("#define A \\\n" 4359 " \\\n" 4360 " a; \\\n" 4361 " b;", 4362 format("#define A \\\n" 4363 " \\\n" 4364 " a; \\\n" 4365 " b;", 4366 getLLVMStyleWithColumns(11))); 4367 EXPECT_EQ("#define A \\\n" 4368 " a; \\\n" 4369 " \\\n" 4370 " b;", 4371 format("#define A \\\n" 4372 " a; \\\n" 4373 " \\\n" 4374 " b;", 4375 getLLVMStyleWithColumns(11))); 4376 } 4377 4378 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 4379 verifyIncompleteFormat("#define A :"); 4380 verifyFormat("#define SOMECASES \\\n" 4381 " case 1: \\\n" 4382 " case 2\n", 4383 getLLVMStyleWithColumns(20)); 4384 verifyFormat("#define MACRO(a) \\\n" 4385 " if (a) \\\n" 4386 " f(); \\\n" 4387 " else \\\n" 4388 " g()", 4389 getLLVMStyleWithColumns(18)); 4390 verifyFormat("#define A template <typename T>"); 4391 verifyIncompleteFormat("#define STR(x) #x\n" 4392 "f(STR(this_is_a_string_literal{));"); 4393 verifyFormat("#pragma omp threadprivate( \\\n" 4394 " y)), // expected-warning", 4395 getLLVMStyleWithColumns(28)); 4396 verifyFormat("#d, = };"); 4397 verifyFormat("#if \"a"); 4398 verifyIncompleteFormat("({\n" 4399 "#define b \\\n" 4400 " } \\\n" 4401 " a\n" 4402 "a", 4403 getLLVMStyleWithColumns(15)); 4404 verifyFormat("#define A \\\n" 4405 " { \\\n" 4406 " {\n" 4407 "#define B \\\n" 4408 " } \\\n" 4409 " }", 4410 getLLVMStyleWithColumns(15)); 4411 verifyNoCrash("#if a\na(\n#else\n#endif\n{a"); 4412 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}"); 4413 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};"); 4414 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}"); 4415 } 4416 4417 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { 4418 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. 4419 EXPECT_EQ("class A : public QObject {\n" 4420 " Q_OBJECT\n" 4421 "\n" 4422 " A() {}\n" 4423 "};", 4424 format("class A : public QObject {\n" 4425 " Q_OBJECT\n" 4426 "\n" 4427 " A() {\n}\n" 4428 "} ;")); 4429 EXPECT_EQ("MACRO\n" 4430 "/*static*/ int i;", 4431 format("MACRO\n" 4432 " /*static*/ int i;")); 4433 EXPECT_EQ("SOME_MACRO\n" 4434 "namespace {\n" 4435 "void f();\n" 4436 "} // namespace", 4437 format("SOME_MACRO\n" 4438 " namespace {\n" 4439 "void f( );\n" 4440 "} // namespace")); 4441 // Only if the identifier contains at least 5 characters. 4442 EXPECT_EQ("HTTP f();", format("HTTP\nf();")); 4443 EXPECT_EQ("MACRO\nf();", format("MACRO\nf();")); 4444 // Only if everything is upper case. 4445 EXPECT_EQ("class A : public QObject {\n" 4446 " Q_Object A() {}\n" 4447 "};", 4448 format("class A : public QObject {\n" 4449 " Q_Object\n" 4450 " A() {\n}\n" 4451 "} ;")); 4452 4453 // Only if the next line can actually start an unwrapped line. 4454 EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;", 4455 format("SOME_WEIRD_LOG_MACRO\n" 4456 "<< SomeThing;")); 4457 4458 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " 4459 "(n, buffers))\n", 4460 getChromiumStyle(FormatStyle::LK_Cpp)); 4461 4462 // See PR41483 4463 EXPECT_EQ("/**/ FOO(a)\n" 4464 "FOO(b)", 4465 format("/**/ FOO(a)\n" 4466 "FOO(b)")); 4467 } 4468 4469 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { 4470 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 4471 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 4472 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 4473 "class X {};\n" 4474 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 4475 "int *createScopDetectionPass() { return 0; }", 4476 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 4477 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 4478 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 4479 " class X {};\n" 4480 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 4481 " int *createScopDetectionPass() { return 0; }")); 4482 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as 4483 // braces, so that inner block is indented one level more. 4484 EXPECT_EQ("int q() {\n" 4485 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 4486 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 4487 " IPC_END_MESSAGE_MAP()\n" 4488 "}", 4489 format("int q() {\n" 4490 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 4491 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 4492 " IPC_END_MESSAGE_MAP()\n" 4493 "}")); 4494 4495 // Same inside macros. 4496 EXPECT_EQ("#define LIST(L) \\\n" 4497 " L(A) \\\n" 4498 " L(B) \\\n" 4499 " L(C)", 4500 format("#define LIST(L) \\\n" 4501 " L(A) \\\n" 4502 " L(B) \\\n" 4503 " L(C)", 4504 getGoogleStyle())); 4505 4506 // These must not be recognized as macros. 4507 EXPECT_EQ("int q() {\n" 4508 " f(x);\n" 4509 " f(x) {}\n" 4510 " f(x)->g();\n" 4511 " f(x)->*g();\n" 4512 " f(x).g();\n" 4513 " f(x) = x;\n" 4514 " f(x) += x;\n" 4515 " f(x) -= x;\n" 4516 " f(x) *= x;\n" 4517 " f(x) /= x;\n" 4518 " f(x) %= x;\n" 4519 " f(x) &= x;\n" 4520 " f(x) |= x;\n" 4521 " f(x) ^= x;\n" 4522 " f(x) >>= x;\n" 4523 " f(x) <<= x;\n" 4524 " f(x)[y].z();\n" 4525 " LOG(INFO) << x;\n" 4526 " ifstream(x) >> x;\n" 4527 "}\n", 4528 format("int q() {\n" 4529 " f(x)\n;\n" 4530 " f(x)\n {}\n" 4531 " f(x)\n->g();\n" 4532 " f(x)\n->*g();\n" 4533 " f(x)\n.g();\n" 4534 " f(x)\n = x;\n" 4535 " f(x)\n += x;\n" 4536 " f(x)\n -= x;\n" 4537 " f(x)\n *= x;\n" 4538 " f(x)\n /= x;\n" 4539 " f(x)\n %= x;\n" 4540 " f(x)\n &= x;\n" 4541 " f(x)\n |= x;\n" 4542 " f(x)\n ^= x;\n" 4543 " f(x)\n >>= x;\n" 4544 " f(x)\n <<= x;\n" 4545 " f(x)\n[y].z();\n" 4546 " LOG(INFO)\n << x;\n" 4547 " ifstream(x)\n >> x;\n" 4548 "}\n")); 4549 EXPECT_EQ("int q() {\n" 4550 " F(x)\n" 4551 " if (1) {\n" 4552 " }\n" 4553 " F(x)\n" 4554 " while (1) {\n" 4555 " }\n" 4556 " F(x)\n" 4557 " G(x);\n" 4558 " F(x)\n" 4559 " try {\n" 4560 " Q();\n" 4561 " } catch (...) {\n" 4562 " }\n" 4563 "}\n", 4564 format("int q() {\n" 4565 "F(x)\n" 4566 "if (1) {}\n" 4567 "F(x)\n" 4568 "while (1) {}\n" 4569 "F(x)\n" 4570 "G(x);\n" 4571 "F(x)\n" 4572 "try { Q(); } catch (...) {}\n" 4573 "}\n")); 4574 EXPECT_EQ("class A {\n" 4575 " A() : t(0) {}\n" 4576 " A(int i) noexcept() : {}\n" 4577 " A(X x)\n" // FIXME: function-level try blocks are broken. 4578 " try : t(0) {\n" 4579 " } catch (...) {\n" 4580 " }\n" 4581 "};", 4582 format("class A {\n" 4583 " A()\n : t(0) {}\n" 4584 " A(int i)\n noexcept() : {}\n" 4585 " A(X x)\n" 4586 " try : t(0) {} catch (...) {}\n" 4587 "};")); 4588 FormatStyle Style = getLLVMStyle(); 4589 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4590 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 4591 Style.BraceWrapping.AfterFunction = true; 4592 EXPECT_EQ("void f()\n" 4593 "try\n" 4594 "{\n" 4595 "}", 4596 format("void f() try {\n" 4597 "}", 4598 Style)); 4599 EXPECT_EQ("class SomeClass {\n" 4600 "public:\n" 4601 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 4602 "};", 4603 format("class SomeClass {\n" 4604 "public:\n" 4605 " SomeClass()\n" 4606 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 4607 "};")); 4608 EXPECT_EQ("class SomeClass {\n" 4609 "public:\n" 4610 " SomeClass()\n" 4611 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 4612 "};", 4613 format("class SomeClass {\n" 4614 "public:\n" 4615 " SomeClass()\n" 4616 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 4617 "};", 4618 getLLVMStyleWithColumns(40))); 4619 4620 verifyFormat("MACRO(>)"); 4621 4622 // Some macros contain an implicit semicolon. 4623 Style = getLLVMStyle(); 4624 Style.StatementMacros.push_back("FOO"); 4625 verifyFormat("FOO(a) int b = 0;"); 4626 verifyFormat("FOO(a)\n" 4627 "int b = 0;", 4628 Style); 4629 verifyFormat("FOO(a);\n" 4630 "int b = 0;", 4631 Style); 4632 verifyFormat("FOO(argc, argv, \"4.0.2\")\n" 4633 "int b = 0;", 4634 Style); 4635 verifyFormat("FOO()\n" 4636 "int b = 0;", 4637 Style); 4638 verifyFormat("FOO\n" 4639 "int b = 0;", 4640 Style); 4641 verifyFormat("void f() {\n" 4642 " FOO(a)\n" 4643 " return a;\n" 4644 "}", 4645 Style); 4646 verifyFormat("FOO(a)\n" 4647 "FOO(b)", 4648 Style); 4649 verifyFormat("int a = 0;\n" 4650 "FOO(b)\n" 4651 "int c = 0;", 4652 Style); 4653 verifyFormat("int a = 0;\n" 4654 "int x = FOO(a)\n" 4655 "int b = 0;", 4656 Style); 4657 verifyFormat("void foo(int a) { FOO(a) }\n" 4658 "uint32_t bar() {}", 4659 Style); 4660 } 4661 4662 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { 4663 verifyFormat("#define A \\\n" 4664 " f({ \\\n" 4665 " g(); \\\n" 4666 " });", 4667 getLLVMStyleWithColumns(11)); 4668 } 4669 4670 TEST_F(FormatTest, IndentPreprocessorDirectives) { 4671 FormatStyle Style = getLLVMStyle(); 4672 Style.IndentPPDirectives = FormatStyle::PPDIS_None; 4673 Style.ColumnLimit = 40; 4674 verifyFormat("#ifdef _WIN32\n" 4675 "#define A 0\n" 4676 "#ifdef VAR2\n" 4677 "#define B 1\n" 4678 "#include <someheader.h>\n" 4679 "#define MACRO \\\n" 4680 " some_very_long_func_aaaaaaaaaa();\n" 4681 "#endif\n" 4682 "#else\n" 4683 "#define A 1\n" 4684 "#endif", 4685 Style); 4686 Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 4687 verifyFormat("#ifdef _WIN32\n" 4688 "# define A 0\n" 4689 "# ifdef VAR2\n" 4690 "# define B 1\n" 4691 "# include <someheader.h>\n" 4692 "# define MACRO \\\n" 4693 " some_very_long_func_aaaaaaaaaa();\n" 4694 "# endif\n" 4695 "#else\n" 4696 "# define A 1\n" 4697 "#endif", 4698 Style); 4699 verifyFormat("#if A\n" 4700 "# define MACRO \\\n" 4701 " void a(int x) { \\\n" 4702 " b(); \\\n" 4703 " c(); \\\n" 4704 " d(); \\\n" 4705 " e(); \\\n" 4706 " f(); \\\n" 4707 " }\n" 4708 "#endif", 4709 Style); 4710 // Comments before include guard. 4711 verifyFormat("// file comment\n" 4712 "// file comment\n" 4713 "#ifndef HEADER_H\n" 4714 "#define HEADER_H\n" 4715 "code();\n" 4716 "#endif", 4717 Style); 4718 // Test with include guards. 4719 verifyFormat("#ifndef HEADER_H\n" 4720 "#define HEADER_H\n" 4721 "code();\n" 4722 "#endif", 4723 Style); 4724 // Include guards must have a #define with the same variable immediately 4725 // after #ifndef. 4726 verifyFormat("#ifndef NOT_GUARD\n" 4727 "# define FOO\n" 4728 "code();\n" 4729 "#endif", 4730 Style); 4731 4732 // Include guards must cover the entire file. 4733 verifyFormat("code();\n" 4734 "code();\n" 4735 "#ifndef NOT_GUARD\n" 4736 "# define NOT_GUARD\n" 4737 "code();\n" 4738 "#endif", 4739 Style); 4740 verifyFormat("#ifndef NOT_GUARD\n" 4741 "# define NOT_GUARD\n" 4742 "code();\n" 4743 "#endif\n" 4744 "code();", 4745 Style); 4746 // Test with trailing blank lines. 4747 verifyFormat("#ifndef HEADER_H\n" 4748 "#define HEADER_H\n" 4749 "code();\n" 4750 "#endif\n", 4751 Style); 4752 // Include guards don't have #else. 4753 verifyFormat("#ifndef NOT_GUARD\n" 4754 "# define NOT_GUARD\n" 4755 "code();\n" 4756 "#else\n" 4757 "#endif", 4758 Style); 4759 verifyFormat("#ifndef NOT_GUARD\n" 4760 "# define NOT_GUARD\n" 4761 "code();\n" 4762 "#elif FOO\n" 4763 "#endif", 4764 Style); 4765 // Non-identifier #define after potential include guard. 4766 verifyFormat("#ifndef FOO\n" 4767 "# define 1\n" 4768 "#endif\n", 4769 Style); 4770 // #if closes past last non-preprocessor line. 4771 verifyFormat("#ifndef FOO\n" 4772 "#define FOO\n" 4773 "#if 1\n" 4774 "int i;\n" 4775 "# define A 0\n" 4776 "#endif\n" 4777 "#endif\n", 4778 Style); 4779 // Don't crash if there is an #elif directive without a condition. 4780 verifyFormat("#if 1\n" 4781 "int x;\n" 4782 "#elif\n" 4783 "int y;\n" 4784 "#else\n" 4785 "int z;\n" 4786 "#endif", 4787 Style); 4788 // FIXME: This doesn't handle the case where there's code between the 4789 // #ifndef and #define but all other conditions hold. This is because when 4790 // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the 4791 // previous code line yet, so we can't detect it. 4792 EXPECT_EQ("#ifndef NOT_GUARD\n" 4793 "code();\n" 4794 "#define NOT_GUARD\n" 4795 "code();\n" 4796 "#endif", 4797 format("#ifndef NOT_GUARD\n" 4798 "code();\n" 4799 "# define NOT_GUARD\n" 4800 "code();\n" 4801 "#endif", 4802 Style)); 4803 // FIXME: This doesn't handle cases where legitimate preprocessor lines may 4804 // be outside an include guard. Examples are #pragma once and 4805 // #pragma GCC diagnostic, or anything else that does not change the meaning 4806 // of the file if it's included multiple times. 4807 EXPECT_EQ("#ifdef WIN32\n" 4808 "# pragma once\n" 4809 "#endif\n" 4810 "#ifndef HEADER_H\n" 4811 "# define HEADER_H\n" 4812 "code();\n" 4813 "#endif", 4814 format("#ifdef WIN32\n" 4815 "# pragma once\n" 4816 "#endif\n" 4817 "#ifndef HEADER_H\n" 4818 "#define HEADER_H\n" 4819 "code();\n" 4820 "#endif", 4821 Style)); 4822 // FIXME: This does not detect when there is a single non-preprocessor line 4823 // in front of an include-guard-like structure where other conditions hold 4824 // because ScopedLineState hides the line. 4825 EXPECT_EQ("code();\n" 4826 "#ifndef HEADER_H\n" 4827 "#define HEADER_H\n" 4828 "code();\n" 4829 "#endif", 4830 format("code();\n" 4831 "#ifndef HEADER_H\n" 4832 "# define HEADER_H\n" 4833 "code();\n" 4834 "#endif", 4835 Style)); 4836 // Keep comments aligned with #, otherwise indent comments normally. These 4837 // tests cannot use verifyFormat because messUp manipulates leading 4838 // whitespace. 4839 { 4840 const char *Expected = "" 4841 "void f() {\n" 4842 "#if 1\n" 4843 "// Preprocessor aligned.\n" 4844 "# define A 0\n" 4845 " // Code. Separated by blank line.\n" 4846 "\n" 4847 "# define B 0\n" 4848 " // Code. Not aligned with #\n" 4849 "# define C 0\n" 4850 "#endif"; 4851 const char *ToFormat = "" 4852 "void f() {\n" 4853 "#if 1\n" 4854 "// Preprocessor aligned.\n" 4855 "# define A 0\n" 4856 "// Code. Separated by blank line.\n" 4857 "\n" 4858 "# define B 0\n" 4859 " // Code. Not aligned with #\n" 4860 "# define C 0\n" 4861 "#endif"; 4862 EXPECT_EQ(Expected, format(ToFormat, Style)); 4863 EXPECT_EQ(Expected, format(Expected, Style)); 4864 } 4865 // Keep block quotes aligned. 4866 { 4867 const char *Expected = "" 4868 "void f() {\n" 4869 "#if 1\n" 4870 "/* Preprocessor aligned. */\n" 4871 "# define A 0\n" 4872 " /* Code. Separated by blank line. */\n" 4873 "\n" 4874 "# define B 0\n" 4875 " /* Code. Not aligned with # */\n" 4876 "# define C 0\n" 4877 "#endif"; 4878 const char *ToFormat = "" 4879 "void f() {\n" 4880 "#if 1\n" 4881 "/* Preprocessor aligned. */\n" 4882 "# define A 0\n" 4883 "/* Code. Separated by blank line. */\n" 4884 "\n" 4885 "# define B 0\n" 4886 " /* Code. Not aligned with # */\n" 4887 "# define C 0\n" 4888 "#endif"; 4889 EXPECT_EQ(Expected, format(ToFormat, Style)); 4890 EXPECT_EQ(Expected, format(Expected, Style)); 4891 } 4892 // Keep comments aligned with un-indented directives. 4893 { 4894 const char *Expected = "" 4895 "void f() {\n" 4896 "// Preprocessor aligned.\n" 4897 "#define A 0\n" 4898 " // Code. Separated by blank line.\n" 4899 "\n" 4900 "#define B 0\n" 4901 " // Code. Not aligned with #\n" 4902 "#define C 0\n"; 4903 const char *ToFormat = "" 4904 "void f() {\n" 4905 "// Preprocessor aligned.\n" 4906 "#define A 0\n" 4907 "// Code. Separated by blank line.\n" 4908 "\n" 4909 "#define B 0\n" 4910 " // Code. Not aligned with #\n" 4911 "#define C 0\n"; 4912 EXPECT_EQ(Expected, format(ToFormat, Style)); 4913 EXPECT_EQ(Expected, format(Expected, Style)); 4914 } 4915 // Test AfterHash with tabs. 4916 { 4917 FormatStyle Tabbed = Style; 4918 Tabbed.UseTab = FormatStyle::UT_Always; 4919 Tabbed.IndentWidth = 8; 4920 Tabbed.TabWidth = 8; 4921 verifyFormat("#ifdef _WIN32\n" 4922 "#\tdefine A 0\n" 4923 "#\tifdef VAR2\n" 4924 "#\t\tdefine B 1\n" 4925 "#\t\tinclude <someheader.h>\n" 4926 "#\t\tdefine MACRO \\\n" 4927 "\t\t\tsome_very_long_func_aaaaaaaaaa();\n" 4928 "#\tendif\n" 4929 "#else\n" 4930 "#\tdefine A 1\n" 4931 "#endif", 4932 Tabbed); 4933 } 4934 4935 // Regression test: Multiline-macro inside include guards. 4936 verifyFormat("#ifndef HEADER_H\n" 4937 "#define HEADER_H\n" 4938 "#define A() \\\n" 4939 " int i; \\\n" 4940 " int j;\n" 4941 "#endif // HEADER_H", 4942 getLLVMStyleWithColumns(20)); 4943 4944 Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash; 4945 // Basic before hash indent tests 4946 verifyFormat("#ifdef _WIN32\n" 4947 " #define A 0\n" 4948 " #ifdef VAR2\n" 4949 " #define B 1\n" 4950 " #include <someheader.h>\n" 4951 " #define MACRO \\\n" 4952 " some_very_long_func_aaaaaaaaaa();\n" 4953 " #endif\n" 4954 "#else\n" 4955 " #define A 1\n" 4956 "#endif", 4957 Style); 4958 verifyFormat("#if A\n" 4959 " #define MACRO \\\n" 4960 " void a(int x) { \\\n" 4961 " b(); \\\n" 4962 " c(); \\\n" 4963 " d(); \\\n" 4964 " e(); \\\n" 4965 " f(); \\\n" 4966 " }\n" 4967 "#endif", 4968 Style); 4969 // Keep comments aligned with indented directives. These 4970 // tests cannot use verifyFormat because messUp manipulates leading 4971 // whitespace. 4972 { 4973 const char *Expected = "void f() {\n" 4974 "// Aligned to preprocessor.\n" 4975 "#if 1\n" 4976 " // Aligned to code.\n" 4977 " int a;\n" 4978 " #if 1\n" 4979 " // Aligned to preprocessor.\n" 4980 " #define A 0\n" 4981 " // Aligned to code.\n" 4982 " int b;\n" 4983 " #endif\n" 4984 "#endif\n" 4985 "}"; 4986 const char *ToFormat = "void f() {\n" 4987 "// Aligned to preprocessor.\n" 4988 "#if 1\n" 4989 "// Aligned to code.\n" 4990 "int a;\n" 4991 "#if 1\n" 4992 "// Aligned to preprocessor.\n" 4993 "#define A 0\n" 4994 "// Aligned to code.\n" 4995 "int b;\n" 4996 "#endif\n" 4997 "#endif\n" 4998 "}"; 4999 EXPECT_EQ(Expected, format(ToFormat, Style)); 5000 EXPECT_EQ(Expected, format(Expected, Style)); 5001 } 5002 { 5003 const char *Expected = "void f() {\n" 5004 "/* Aligned to preprocessor. */\n" 5005 "#if 1\n" 5006 " /* Aligned to code. */\n" 5007 " int a;\n" 5008 " #if 1\n" 5009 " /* Aligned to preprocessor. */\n" 5010 " #define A 0\n" 5011 " /* Aligned to code. */\n" 5012 " int b;\n" 5013 " #endif\n" 5014 "#endif\n" 5015 "}"; 5016 const char *ToFormat = "void f() {\n" 5017 "/* Aligned to preprocessor. */\n" 5018 "#if 1\n" 5019 "/* Aligned to code. */\n" 5020 "int a;\n" 5021 "#if 1\n" 5022 "/* Aligned to preprocessor. */\n" 5023 "#define A 0\n" 5024 "/* Aligned to code. */\n" 5025 "int b;\n" 5026 "#endif\n" 5027 "#endif\n" 5028 "}"; 5029 EXPECT_EQ(Expected, format(ToFormat, Style)); 5030 EXPECT_EQ(Expected, format(Expected, Style)); 5031 } 5032 5033 // Test single comment before preprocessor 5034 verifyFormat("// Comment\n" 5035 "\n" 5036 "#if 1\n" 5037 "#endif", 5038 Style); 5039 } 5040 5041 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 5042 verifyFormat("{\n { a #c; }\n}"); 5043 } 5044 5045 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 5046 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 5047 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 5048 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 5049 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 5050 } 5051 5052 TEST_F(FormatTest, EscapedNewlines) { 5053 FormatStyle Narrow = getLLVMStyleWithColumns(11); 5054 EXPECT_EQ("#define A \\\n int i; \\\n int j;", 5055 format("#define A \\\nint i;\\\n int j;", Narrow)); 5056 EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;")); 5057 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 5058 EXPECT_EQ("/* \\ \\ \\\n */", format("\\\n/* \\ \\ \\\n */")); 5059 EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>")); 5060 5061 FormatStyle AlignLeft = getLLVMStyle(); 5062 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 5063 EXPECT_EQ("#define MACRO(x) \\\n" 5064 "private: \\\n" 5065 " int x(int a);\n", 5066 format("#define MACRO(x) \\\n" 5067 "private: \\\n" 5068 " int x(int a);\n", 5069 AlignLeft)); 5070 5071 // CRLF line endings 5072 EXPECT_EQ("#define A \\\r\n int i; \\\r\n int j;", 5073 format("#define A \\\r\nint i;\\\r\n int j;", Narrow)); 5074 EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;")); 5075 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 5076 EXPECT_EQ("/* \\ \\ \\\r\n */", format("\\\r\n/* \\ \\ \\\r\n */")); 5077 EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>")); 5078 EXPECT_EQ("#define MACRO(x) \\\r\n" 5079 "private: \\\r\n" 5080 " int x(int a);\r\n", 5081 format("#define MACRO(x) \\\r\n" 5082 "private: \\\r\n" 5083 " int x(int a);\r\n", 5084 AlignLeft)); 5085 5086 FormatStyle DontAlign = getLLVMStyle(); 5087 DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 5088 DontAlign.MaxEmptyLinesToKeep = 3; 5089 // FIXME: can't use verifyFormat here because the newline before 5090 // "public:" is not inserted the first time it's reformatted 5091 EXPECT_EQ("#define A \\\n" 5092 " class Foo { \\\n" 5093 " void bar(); \\\n" 5094 "\\\n" 5095 "\\\n" 5096 "\\\n" 5097 " public: \\\n" 5098 " void baz(); \\\n" 5099 " };", 5100 format("#define A \\\n" 5101 " class Foo { \\\n" 5102 " void bar(); \\\n" 5103 "\\\n" 5104 "\\\n" 5105 "\\\n" 5106 " public: \\\n" 5107 " void baz(); \\\n" 5108 " };", 5109 DontAlign)); 5110 } 5111 5112 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 5113 verifyFormat("#define A \\\n" 5114 " int v( \\\n" 5115 " a); \\\n" 5116 " int i;", 5117 getLLVMStyleWithColumns(11)); 5118 } 5119 5120 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 5121 EXPECT_EQ( 5122 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 5123 " \\\n" 5124 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 5125 "\n" 5126 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 5127 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 5128 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 5129 "\\\n" 5130 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 5131 " \n" 5132 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 5133 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 5134 } 5135 5136 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 5137 EXPECT_EQ("int\n" 5138 "#define A\n" 5139 " a;", 5140 format("int\n#define A\na;")); 5141 verifyFormat("functionCallTo(\n" 5142 " someOtherFunction(\n" 5143 " withSomeParameters, whichInSequence,\n" 5144 " areLongerThanALine(andAnotherCall,\n" 5145 "#define A B\n" 5146 " withMoreParamters,\n" 5147 " whichStronglyInfluenceTheLayout),\n" 5148 " andMoreParameters),\n" 5149 " trailing);", 5150 getLLVMStyleWithColumns(69)); 5151 verifyFormat("Foo::Foo()\n" 5152 "#ifdef BAR\n" 5153 " : baz(0)\n" 5154 "#endif\n" 5155 "{\n" 5156 "}"); 5157 verifyFormat("void f() {\n" 5158 " if (true)\n" 5159 "#ifdef A\n" 5160 " f(42);\n" 5161 " x();\n" 5162 "#else\n" 5163 " g();\n" 5164 " x();\n" 5165 "#endif\n" 5166 "}"); 5167 verifyFormat("void f(param1, param2,\n" 5168 " param3,\n" 5169 "#ifdef A\n" 5170 " param4(param5,\n" 5171 "#ifdef A1\n" 5172 " param6,\n" 5173 "#ifdef A2\n" 5174 " param7),\n" 5175 "#else\n" 5176 " param8),\n" 5177 " param9,\n" 5178 "#endif\n" 5179 " param10,\n" 5180 "#endif\n" 5181 " param11)\n" 5182 "#else\n" 5183 " param12)\n" 5184 "#endif\n" 5185 "{\n" 5186 " x();\n" 5187 "}", 5188 getLLVMStyleWithColumns(28)); 5189 verifyFormat("#if 1\n" 5190 "int i;"); 5191 verifyFormat("#if 1\n" 5192 "#endif\n" 5193 "#if 1\n" 5194 "#else\n" 5195 "#endif\n"); 5196 verifyFormat("DEBUG({\n" 5197 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5198 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 5199 "});\n" 5200 "#if a\n" 5201 "#else\n" 5202 "#endif"); 5203 5204 verifyIncompleteFormat("void f(\n" 5205 "#if A\n" 5206 ");\n" 5207 "#else\n" 5208 "#endif"); 5209 } 5210 5211 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { 5212 verifyFormat("#endif\n" 5213 "#if B"); 5214 } 5215 5216 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { 5217 FormatStyle SingleLine = getLLVMStyle(); 5218 SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; 5219 verifyFormat("#if 0\n" 5220 "#elif 1\n" 5221 "#endif\n" 5222 "void foo() {\n" 5223 " if (test) foo2();\n" 5224 "}", 5225 SingleLine); 5226 } 5227 5228 TEST_F(FormatTest, LayoutBlockInsideParens) { 5229 verifyFormat("functionCall({ int i; });"); 5230 verifyFormat("functionCall({\n" 5231 " int i;\n" 5232 " int j;\n" 5233 "});"); 5234 verifyFormat("functionCall(\n" 5235 " {\n" 5236 " int i;\n" 5237 " int j;\n" 5238 " },\n" 5239 " aaaa, bbbb, cccc);"); 5240 verifyFormat("functionA(functionB({\n" 5241 " int i;\n" 5242 " int j;\n" 5243 " }),\n" 5244 " aaaa, bbbb, cccc);"); 5245 verifyFormat("functionCall(\n" 5246 " {\n" 5247 " int i;\n" 5248 " int j;\n" 5249 " },\n" 5250 " aaaa, bbbb, // comment\n" 5251 " cccc);"); 5252 verifyFormat("functionA(functionB({\n" 5253 " int i;\n" 5254 " int j;\n" 5255 " }),\n" 5256 " aaaa, bbbb, // comment\n" 5257 " cccc);"); 5258 verifyFormat("functionCall(aaaa, bbbb, { int i; });"); 5259 verifyFormat("functionCall(aaaa, bbbb, {\n" 5260 " int i;\n" 5261 " int j;\n" 5262 "});"); 5263 verifyFormat( 5264 "Aaa(\n" // FIXME: There shouldn't be a linebreak here. 5265 " {\n" 5266 " int i; // break\n" 5267 " },\n" 5268 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 5269 " ccccccccccccccccc));"); 5270 verifyFormat("DEBUG({\n" 5271 " if (a)\n" 5272 " f();\n" 5273 "});"); 5274 } 5275 5276 TEST_F(FormatTest, LayoutBlockInsideStatement) { 5277 EXPECT_EQ("SOME_MACRO { int i; }\n" 5278 "int i;", 5279 format(" SOME_MACRO {int i;} int i;")); 5280 } 5281 5282 TEST_F(FormatTest, LayoutNestedBlocks) { 5283 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 5284 " struct s {\n" 5285 " int i;\n" 5286 " };\n" 5287 " s kBitsToOs[] = {{10}};\n" 5288 " for (int i = 0; i < 10; ++i)\n" 5289 " return;\n" 5290 "}"); 5291 verifyFormat("call(parameter, {\n" 5292 " something();\n" 5293 " // Comment using all columns.\n" 5294 " somethingelse();\n" 5295 "});", 5296 getLLVMStyleWithColumns(40)); 5297 verifyFormat("DEBUG( //\n" 5298 " { f(); }, a);"); 5299 verifyFormat("DEBUG( //\n" 5300 " {\n" 5301 " f(); //\n" 5302 " },\n" 5303 " a);"); 5304 5305 EXPECT_EQ("call(parameter, {\n" 5306 " something();\n" 5307 " // Comment too\n" 5308 " // looooooooooong.\n" 5309 " somethingElse();\n" 5310 "});", 5311 format("call(parameter, {\n" 5312 " something();\n" 5313 " // Comment too looooooooooong.\n" 5314 " somethingElse();\n" 5315 "});", 5316 getLLVMStyleWithColumns(29))); 5317 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });")); 5318 EXPECT_EQ("DEBUG({ // comment\n" 5319 " int i;\n" 5320 "});", 5321 format("DEBUG({ // comment\n" 5322 "int i;\n" 5323 "});")); 5324 EXPECT_EQ("DEBUG({\n" 5325 " int i;\n" 5326 "\n" 5327 " // comment\n" 5328 " int j;\n" 5329 "});", 5330 format("DEBUG({\n" 5331 " int i;\n" 5332 "\n" 5333 " // comment\n" 5334 " int j;\n" 5335 "});")); 5336 5337 verifyFormat("DEBUG({\n" 5338 " if (a)\n" 5339 " return;\n" 5340 "});"); 5341 verifyGoogleFormat("DEBUG({\n" 5342 " if (a) return;\n" 5343 "});"); 5344 FormatStyle Style = getGoogleStyle(); 5345 Style.ColumnLimit = 45; 5346 verifyFormat("Debug(\n" 5347 " aaaaa,\n" 5348 " {\n" 5349 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" 5350 " },\n" 5351 " a);", 5352 Style); 5353 5354 verifyFormat("SomeFunction({MACRO({ return output; }), b});"); 5355 5356 verifyNoCrash("^{v^{a}}"); 5357 } 5358 5359 TEST_F(FormatTest, FormatNestedBlocksInMacros) { 5360 EXPECT_EQ("#define MACRO() \\\n" 5361 " Debug(aaa, /* force line break */ \\\n" 5362 " { \\\n" 5363 " int i; \\\n" 5364 " int j; \\\n" 5365 " })", 5366 format("#define MACRO() Debug(aaa, /* force line break */ \\\n" 5367 " { int i; int j; })", 5368 getGoogleStyle())); 5369 5370 EXPECT_EQ("#define A \\\n" 5371 " [] { \\\n" 5372 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 5373 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 5374 " }", 5375 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 5376 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 5377 getGoogleStyle())); 5378 } 5379 5380 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 5381 EXPECT_EQ("{}", format("{}")); 5382 verifyFormat("enum E {};"); 5383 verifyFormat("enum E {}"); 5384 FormatStyle Style = getLLVMStyle(); 5385 Style.SpaceInEmptyBlock = true; 5386 EXPECT_EQ("void f() { }", format("void f() {}", Style)); 5387 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 5388 EXPECT_EQ("while (true) { }", format("while (true) {}", Style)); 5389 } 5390 5391 TEST_F(FormatTest, FormatBeginBlockEndMacros) { 5392 FormatStyle Style = getLLVMStyle(); 5393 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$"; 5394 Style.MacroBlockEnd = "^[A-Z_]+_END$"; 5395 verifyFormat("FOO_BEGIN\n" 5396 " FOO_ENTRY\n" 5397 "FOO_END", 5398 Style); 5399 verifyFormat("FOO_BEGIN\n" 5400 " NESTED_FOO_BEGIN\n" 5401 " NESTED_FOO_ENTRY\n" 5402 " NESTED_FOO_END\n" 5403 "FOO_END", 5404 Style); 5405 verifyFormat("FOO_BEGIN(Foo, Bar)\n" 5406 " int x;\n" 5407 " x = 1;\n" 5408 "FOO_END(Baz)", 5409 Style); 5410 } 5411 5412 //===----------------------------------------------------------------------===// 5413 // Line break tests. 5414 //===----------------------------------------------------------------------===// 5415 5416 TEST_F(FormatTest, PreventConfusingIndents) { 5417 verifyFormat( 5418 "void f() {\n" 5419 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 5420 " parameter, parameter, parameter)),\n" 5421 " SecondLongCall(parameter));\n" 5422 "}"); 5423 verifyFormat( 5424 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5425 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 5426 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5427 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 5428 verifyFormat( 5429 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5430 " [aaaaaaaaaaaaaaaaaaaaaaaa\n" 5431 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 5432 " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); 5433 verifyFormat( 5434 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 5435 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 5436 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 5437 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 5438 verifyFormat("int a = bbbb && ccc &&\n" 5439 " fffff(\n" 5440 "#define A Just forcing a new line\n" 5441 " ddd);"); 5442 } 5443 5444 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 5445 verifyFormat( 5446 "bool aaaaaaa =\n" 5447 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 5448 " bbbbbbbb();"); 5449 verifyFormat( 5450 "bool aaaaaaa =\n" 5451 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" 5452 " bbbbbbbb();"); 5453 5454 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 5455 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 5456 " ccccccccc == ddddddddddd;"); 5457 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 5458 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" 5459 " ccccccccc == ddddddddddd;"); 5460 verifyFormat( 5461 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 5462 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" 5463 " ccccccccc == ddddddddddd;"); 5464 5465 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 5466 " aaaaaa) &&\n" 5467 " bbbbbb && cccccc;"); 5468 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 5469 " aaaaaa) >>\n" 5470 " bbbbbb;"); 5471 verifyFormat("aa = Whitespaces.addUntouchableComment(\n" 5472 " SourceMgr.getSpellingColumnNumber(\n" 5473 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 5474 " 1);"); 5475 5476 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5477 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 5478 " cccccc) {\n}"); 5479 verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5480 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 5481 " cccccc) {\n}"); 5482 verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5483 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 5484 " cccccc) {\n}"); 5485 verifyFormat("b = a &&\n" 5486 " // Comment\n" 5487 " b.c && d;"); 5488 5489 // If the LHS of a comparison is not a binary expression itself, the 5490 // additional linebreak confuses many people. 5491 verifyFormat( 5492 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5493 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 5494 "}"); 5495 verifyFormat( 5496 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5497 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 5498 "}"); 5499 verifyFormat( 5500 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 5501 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 5502 "}"); 5503 verifyFormat( 5504 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5505 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n" 5506 "}"); 5507 // Even explicit parentheses stress the precedence enough to make the 5508 // additional break unnecessary. 5509 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5510 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 5511 "}"); 5512 // This cases is borderline, but with the indentation it is still readable. 5513 verifyFormat( 5514 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5515 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5516 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 5517 "}", 5518 getLLVMStyleWithColumns(75)); 5519 5520 // If the LHS is a binary expression, we should still use the additional break 5521 // as otherwise the formatting hides the operator precedence. 5522 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5523 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5524 " 5) {\n" 5525 "}"); 5526 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5527 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n" 5528 " 5) {\n" 5529 "}"); 5530 5531 FormatStyle OnePerLine = getLLVMStyle(); 5532 OnePerLine.BinPackParameters = false; 5533 verifyFormat( 5534 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5535 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5536 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 5537 OnePerLine); 5538 5539 verifyFormat("int i = someFunction(aaaaaaa, 0)\n" 5540 " .aaa(aaaaaaaaaaaaa) *\n" 5541 " aaaaaaa +\n" 5542 " aaaaaaa;", 5543 getLLVMStyleWithColumns(40)); 5544 } 5545 5546 TEST_F(FormatTest, ExpressionIndentation) { 5547 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5548 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5549 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5550 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5551 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 5552 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 5553 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5554 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 5555 " ccccccccccccccccccccccccccccccccccccccccc;"); 5556 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5557 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5558 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5559 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 5560 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5561 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5562 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5563 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 5564 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5565 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5566 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5567 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 5568 verifyFormat("if () {\n" 5569 "} else if (aaaaa && bbbbb > // break\n" 5570 " ccccc) {\n" 5571 "}"); 5572 verifyFormat("if () {\n" 5573 "} else if constexpr (aaaaa && bbbbb > // break\n" 5574 " ccccc) {\n" 5575 "}"); 5576 verifyFormat("if () {\n" 5577 "} else if CONSTEXPR (aaaaa && bbbbb > // break\n" 5578 " ccccc) {\n" 5579 "}"); 5580 verifyFormat("if () {\n" 5581 "} else if (aaaaa &&\n" 5582 " bbbbb > // break\n" 5583 " ccccc &&\n" 5584 " ddddd) {\n" 5585 "}"); 5586 5587 // Presence of a trailing comment used to change indentation of b. 5588 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 5589 " b;\n" 5590 "return aaaaaaaaaaaaaaaaaaa +\n" 5591 " b; //", 5592 getLLVMStyleWithColumns(30)); 5593 } 5594 5595 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 5596 // Not sure what the best system is here. Like this, the LHS can be found 5597 // immediately above an operator (everything with the same or a higher 5598 // indent). The RHS is aligned right of the operator and so compasses 5599 // everything until something with the same indent as the operator is found. 5600 // FIXME: Is this a good system? 5601 FormatStyle Style = getLLVMStyle(); 5602 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 5603 verifyFormat( 5604 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5605 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5606 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5607 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5608 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5609 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5610 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5611 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5612 " > ccccccccccccccccccccccccccccccccccccccccc;", 5613 Style); 5614 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5615 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5616 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5617 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5618 Style); 5619 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5620 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5621 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5622 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5623 Style); 5624 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5625 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5626 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5627 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5628 Style); 5629 verifyFormat("if () {\n" 5630 "} else if (aaaaa\n" 5631 " && bbbbb // break\n" 5632 " > ccccc) {\n" 5633 "}", 5634 Style); 5635 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5636 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 5637 Style); 5638 verifyFormat("return (a)\n" 5639 " // comment\n" 5640 " + b;", 5641 Style); 5642 verifyFormat( 5643 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5644 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5645 " + cc;", 5646 Style); 5647 5648 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5649 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5650 Style); 5651 5652 // Forced by comments. 5653 verifyFormat( 5654 "unsigned ContentSize =\n" 5655 " sizeof(int16_t) // DWARF ARange version number\n" 5656 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 5657 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 5658 " + sizeof(int8_t); // Segment Size (in bytes)"); 5659 5660 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 5661 " == boost::fusion::at_c<1>(iiii).second;", 5662 Style); 5663 5664 Style.ColumnLimit = 60; 5665 verifyFormat("zzzzzzzzzz\n" 5666 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5667 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 5668 Style); 5669 5670 Style.ColumnLimit = 80; 5671 Style.IndentWidth = 4; 5672 Style.TabWidth = 4; 5673 Style.UseTab = FormatStyle::UT_Always; 5674 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5675 Style.AlignOperands = FormatStyle::OAS_DontAlign; 5676 EXPECT_EQ("return someVeryVeryLongConditionThatBarelyFitsOnALine\n" 5677 "\t&& (someOtherLongishConditionPart1\n" 5678 "\t\t|| someOtherEvenLongerNestedConditionPart2);", 5679 format("return someVeryVeryLongConditionThatBarelyFitsOnALine && " 5680 "(someOtherLongishConditionPart1 || " 5681 "someOtherEvenLongerNestedConditionPart2);", 5682 Style)); 5683 } 5684 5685 TEST_F(FormatTest, ExpressionIndentationStrictAlign) { 5686 FormatStyle Style = getLLVMStyle(); 5687 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 5688 Style.AlignOperands = FormatStyle::OAS_AlignAfterOperator; 5689 5690 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5691 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5692 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5693 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5694 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5695 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5696 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5697 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5698 " > ccccccccccccccccccccccccccccccccccccccccc;", 5699 Style); 5700 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5701 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5702 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5703 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5704 Style); 5705 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5706 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5707 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5708 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5709 Style); 5710 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5711 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5712 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5713 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5714 Style); 5715 verifyFormat("if () {\n" 5716 "} else if (aaaaa\n" 5717 " && bbbbb // break\n" 5718 " > ccccc) {\n" 5719 "}", 5720 Style); 5721 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5722 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 5723 Style); 5724 verifyFormat("return (a)\n" 5725 " // comment\n" 5726 " + b;", 5727 Style); 5728 verifyFormat( 5729 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5730 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5731 " + cc;", 5732 Style); 5733 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 5734 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 5735 " : 3333333333333333;", 5736 Style); 5737 verifyFormat( 5738 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 5739 " : ccccccccccccccc ? dddddddddddddddddd\n" 5740 " : eeeeeeeeeeeeeeeeee)\n" 5741 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 5742 " : 3333333333333333;", 5743 Style); 5744 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5745 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5746 Style); 5747 5748 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 5749 " == boost::fusion::at_c<1>(iiii).second;", 5750 Style); 5751 5752 Style.ColumnLimit = 60; 5753 verifyFormat("zzzzzzzzzzzzz\n" 5754 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5755 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 5756 Style); 5757 5758 // Forced by comments. 5759 Style.ColumnLimit = 80; 5760 verifyFormat( 5761 "unsigned ContentSize\n" 5762 " = sizeof(int16_t) // DWARF ARange version number\n" 5763 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 5764 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 5765 " + sizeof(int8_t); // Segment Size (in bytes)", 5766 Style); 5767 5768 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 5769 verifyFormat( 5770 "unsigned ContentSize =\n" 5771 " sizeof(int16_t) // DWARF ARange version number\n" 5772 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 5773 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 5774 " + sizeof(int8_t); // Segment Size (in bytes)", 5775 Style); 5776 5777 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 5778 verifyFormat( 5779 "unsigned ContentSize =\n" 5780 " sizeof(int16_t) // DWARF ARange version number\n" 5781 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 5782 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 5783 " + sizeof(int8_t); // Segment Size (in bytes)", 5784 Style); 5785 } 5786 5787 TEST_F(FormatTest, EnforcedOperatorWraps) { 5788 // Here we'd like to wrap after the || operators, but a comment is forcing an 5789 // earlier wrap. 5790 verifyFormat("bool x = aaaaa //\n" 5791 " || bbbbb\n" 5792 " //\n" 5793 " || cccc;"); 5794 } 5795 5796 TEST_F(FormatTest, NoOperandAlignment) { 5797 FormatStyle Style = getLLVMStyle(); 5798 Style.AlignOperands = FormatStyle::OAS_DontAlign; 5799 verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n" 5800 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5801 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5802 Style); 5803 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 5804 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5805 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5806 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5807 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5808 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5809 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5810 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5811 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5812 " > ccccccccccccccccccccccccccccccccccccccccc;", 5813 Style); 5814 5815 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5816 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5817 " + cc;", 5818 Style); 5819 verifyFormat("int a = aa\n" 5820 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5821 " * cccccccccccccccccccccccccccccccccccc;\n", 5822 Style); 5823 5824 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5825 verifyFormat("return (a > b\n" 5826 " // comment1\n" 5827 " // comment2\n" 5828 " || c);", 5829 Style); 5830 } 5831 5832 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { 5833 FormatStyle Style = getLLVMStyle(); 5834 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 5835 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5836 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5837 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 5838 Style); 5839 } 5840 5841 TEST_F(FormatTest, AllowBinPackingInsideArguments) { 5842 FormatStyle Style = getLLVMStyle(); 5843 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 5844 Style.BinPackArguments = false; 5845 Style.ColumnLimit = 40; 5846 verifyFormat("void test() {\n" 5847 " someFunction(\n" 5848 " this + argument + is + quite\n" 5849 " + long + so + it + gets + wrapped\n" 5850 " + but + remains + bin - packed);\n" 5851 "}", 5852 Style); 5853 verifyFormat("void test() {\n" 5854 " someFunction(arg1,\n" 5855 " this + argument + is\n" 5856 " + quite + long + so\n" 5857 " + it + gets + wrapped\n" 5858 " + but + remains + bin\n" 5859 " - packed,\n" 5860 " arg3);\n" 5861 "}", 5862 Style); 5863 verifyFormat("void test() {\n" 5864 " someFunction(\n" 5865 " arg1,\n" 5866 " this + argument + has\n" 5867 " + anotherFunc(nested,\n" 5868 " calls + whose\n" 5869 " + arguments\n" 5870 " + are + also\n" 5871 " + wrapped,\n" 5872 " in + addition)\n" 5873 " + to + being + bin - packed,\n" 5874 " arg3);\n" 5875 "}", 5876 Style); 5877 5878 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 5879 verifyFormat("void test() {\n" 5880 " someFunction(\n" 5881 " arg1,\n" 5882 " this + argument + has +\n" 5883 " anotherFunc(nested,\n" 5884 " calls + whose +\n" 5885 " arguments +\n" 5886 " are + also +\n" 5887 " wrapped,\n" 5888 " in + addition) +\n" 5889 " to + being + bin - packed,\n" 5890 " arg3);\n" 5891 "}", 5892 Style); 5893 } 5894 5895 TEST_F(FormatTest, ConstructorInitializers) { 5896 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 5897 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 5898 getLLVMStyleWithColumns(45)); 5899 verifyFormat("Constructor()\n" 5900 " : Inttializer(FitsOnTheLine) {}", 5901 getLLVMStyleWithColumns(44)); 5902 verifyFormat("Constructor()\n" 5903 " : Inttializer(FitsOnTheLine) {}", 5904 getLLVMStyleWithColumns(43)); 5905 5906 verifyFormat("template <typename T>\n" 5907 "Constructor() : Initializer(FitsOnTheLine) {}", 5908 getLLVMStyleWithColumns(45)); 5909 5910 verifyFormat( 5911 "SomeClass::Constructor()\n" 5912 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 5913 5914 verifyFormat( 5915 "SomeClass::Constructor()\n" 5916 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 5917 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 5918 verifyFormat( 5919 "SomeClass::Constructor()\n" 5920 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5921 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 5922 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5923 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5924 " : aaaaaaaaaa(aaaaaa) {}"); 5925 5926 verifyFormat("Constructor()\n" 5927 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5928 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5929 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5930 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 5931 5932 verifyFormat("Constructor()\n" 5933 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5934 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 5935 5936 verifyFormat("Constructor(int Parameter = 0)\n" 5937 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 5938 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 5939 verifyFormat("Constructor()\n" 5940 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 5941 "}", 5942 getLLVMStyleWithColumns(60)); 5943 verifyFormat("Constructor()\n" 5944 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5945 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 5946 5947 // Here a line could be saved by splitting the second initializer onto two 5948 // lines, but that is not desirable. 5949 verifyFormat("Constructor()\n" 5950 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 5951 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 5952 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 5953 5954 FormatStyle OnePerLine = getLLVMStyle(); 5955 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 5956 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 5957 verifyFormat("SomeClass::Constructor()\n" 5958 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 5959 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 5960 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 5961 OnePerLine); 5962 verifyFormat("SomeClass::Constructor()\n" 5963 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 5964 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 5965 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 5966 OnePerLine); 5967 verifyFormat("MyClass::MyClass(int var)\n" 5968 " : some_var_(var), // 4 space indent\n" 5969 " some_other_var_(var + 1) { // lined up\n" 5970 "}", 5971 OnePerLine); 5972 verifyFormat("Constructor()\n" 5973 " : aaaaa(aaaaaa),\n" 5974 " aaaaa(aaaaaa),\n" 5975 " aaaaa(aaaaaa),\n" 5976 " aaaaa(aaaaaa),\n" 5977 " aaaaa(aaaaaa) {}", 5978 OnePerLine); 5979 verifyFormat("Constructor()\n" 5980 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 5981 " aaaaaaaaaaaaaaaaaaaaaa) {}", 5982 OnePerLine); 5983 OnePerLine.BinPackParameters = false; 5984 verifyFormat( 5985 "Constructor()\n" 5986 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n" 5987 " aaaaaaaaaaa().aaa(),\n" 5988 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 5989 OnePerLine); 5990 OnePerLine.ColumnLimit = 60; 5991 verifyFormat("Constructor()\n" 5992 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 5993 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 5994 OnePerLine); 5995 5996 EXPECT_EQ("Constructor()\n" 5997 " : // Comment forcing unwanted break.\n" 5998 " aaaa(aaaa) {}", 5999 format("Constructor() :\n" 6000 " // Comment forcing unwanted break.\n" 6001 " aaaa(aaaa) {}")); 6002 } 6003 6004 TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) { 6005 FormatStyle Style = getLLVMStyle(); 6006 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 6007 Style.ColumnLimit = 60; 6008 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 6009 Style.AllowAllConstructorInitializersOnNextLine = true; 6010 Style.BinPackParameters = false; 6011 6012 for (int i = 0; i < 4; ++i) { 6013 // Test all combinations of parameters that should not have an effect. 6014 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1; 6015 Style.AllowAllArgumentsOnNextLine = i & 2; 6016 6017 Style.AllowAllConstructorInitializersOnNextLine = true; 6018 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 6019 verifyFormat("Constructor()\n" 6020 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6021 Style); 6022 verifyFormat("Constructor() : a(a), b(b) {}", Style); 6023 6024 Style.AllowAllConstructorInitializersOnNextLine = false; 6025 verifyFormat("Constructor()\n" 6026 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 6027 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 6028 Style); 6029 verifyFormat("Constructor() : a(a), b(b) {}", Style); 6030 6031 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 6032 Style.AllowAllConstructorInitializersOnNextLine = true; 6033 verifyFormat("Constructor()\n" 6034 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6035 Style); 6036 6037 Style.AllowAllConstructorInitializersOnNextLine = false; 6038 verifyFormat("Constructor()\n" 6039 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6040 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6041 Style); 6042 6043 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 6044 Style.AllowAllConstructorInitializersOnNextLine = true; 6045 verifyFormat("Constructor() :\n" 6046 " aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6047 Style); 6048 6049 Style.AllowAllConstructorInitializersOnNextLine = false; 6050 verifyFormat("Constructor() :\n" 6051 " aaaaaaaaaaaaaaaaaa(a),\n" 6052 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6053 Style); 6054 } 6055 6056 // Test interactions between AllowAllParametersOfDeclarationOnNextLine and 6057 // AllowAllConstructorInitializersOnNextLine in all 6058 // BreakConstructorInitializers modes 6059 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 6060 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6061 Style.AllowAllConstructorInitializersOnNextLine = false; 6062 verifyFormat("SomeClassWithALongName::Constructor(\n" 6063 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n" 6064 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 6065 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 6066 Style); 6067 6068 Style.AllowAllConstructorInitializersOnNextLine = true; 6069 verifyFormat("SomeClassWithALongName::Constructor(\n" 6070 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6071 " int bbbbbbbbbbbbb,\n" 6072 " int cccccccccccccccc)\n" 6073 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6074 Style); 6075 6076 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6077 Style.AllowAllConstructorInitializersOnNextLine = false; 6078 verifyFormat("SomeClassWithALongName::Constructor(\n" 6079 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6080 " int bbbbbbbbbbbbb)\n" 6081 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 6082 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 6083 Style); 6084 6085 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 6086 6087 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6088 verifyFormat("SomeClassWithALongName::Constructor(\n" 6089 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n" 6090 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6091 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6092 Style); 6093 6094 Style.AllowAllConstructorInitializersOnNextLine = true; 6095 verifyFormat("SomeClassWithALongName::Constructor(\n" 6096 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6097 " int bbbbbbbbbbbbb,\n" 6098 " int cccccccccccccccc)\n" 6099 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6100 Style); 6101 6102 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6103 Style.AllowAllConstructorInitializersOnNextLine = false; 6104 verifyFormat("SomeClassWithALongName::Constructor(\n" 6105 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6106 " int bbbbbbbbbbbbb)\n" 6107 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6108 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6109 Style); 6110 6111 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 6112 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6113 verifyFormat("SomeClassWithALongName::Constructor(\n" 6114 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n" 6115 " aaaaaaaaaaaaaaaaaaaa(a),\n" 6116 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6117 Style); 6118 6119 Style.AllowAllConstructorInitializersOnNextLine = true; 6120 verifyFormat("SomeClassWithALongName::Constructor(\n" 6121 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6122 " int bbbbbbbbbbbbb,\n" 6123 " int cccccccccccccccc) :\n" 6124 " aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6125 Style); 6126 6127 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6128 Style.AllowAllConstructorInitializersOnNextLine = false; 6129 verifyFormat("SomeClassWithALongName::Constructor(\n" 6130 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6131 " int bbbbbbbbbbbbb) :\n" 6132 " aaaaaaaaaaaaaaaaaaaa(a),\n" 6133 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6134 Style); 6135 } 6136 6137 TEST_F(FormatTest, AllowAllArgumentsOnNextLine) { 6138 FormatStyle Style = getLLVMStyle(); 6139 Style.ColumnLimit = 60; 6140 Style.BinPackArguments = false; 6141 for (int i = 0; i < 4; ++i) { 6142 // Test all combinations of parameters that should not have an effect. 6143 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1; 6144 Style.AllowAllConstructorInitializersOnNextLine = i & 2; 6145 6146 Style.AllowAllArgumentsOnNextLine = true; 6147 verifyFormat("void foo() {\n" 6148 " FunctionCallWithReallyLongName(\n" 6149 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n" 6150 "}", 6151 Style); 6152 Style.AllowAllArgumentsOnNextLine = false; 6153 verifyFormat("void foo() {\n" 6154 " FunctionCallWithReallyLongName(\n" 6155 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6156 " bbbbbbbbbbbb);\n" 6157 "}", 6158 Style); 6159 6160 Style.AllowAllArgumentsOnNextLine = true; 6161 verifyFormat("void foo() {\n" 6162 " auto VariableWithReallyLongName = {\n" 6163 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n" 6164 "}", 6165 Style); 6166 Style.AllowAllArgumentsOnNextLine = false; 6167 verifyFormat("void foo() {\n" 6168 " auto VariableWithReallyLongName = {\n" 6169 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6170 " bbbbbbbbbbbb};\n" 6171 "}", 6172 Style); 6173 } 6174 6175 // This parameter should not affect declarations. 6176 Style.BinPackParameters = false; 6177 Style.AllowAllArgumentsOnNextLine = false; 6178 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6179 verifyFormat("void FunctionCallWithReallyLongName(\n" 6180 " int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);", 6181 Style); 6182 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6183 verifyFormat("void FunctionCallWithReallyLongName(\n" 6184 " int aaaaaaaaaaaaaaaaaaaaaaa,\n" 6185 " int bbbbbbbbbbbb);", 6186 Style); 6187 } 6188 6189 TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) { 6190 // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign 6191 // and BAS_Align. 6192 auto Style = getLLVMStyle(); 6193 Style.ColumnLimit = 35; 6194 StringRef Input = "functionCall(paramA, paramB, paramC);\n" 6195 "void functionDecl(int A, int B, int C);"; 6196 Style.AllowAllArgumentsOnNextLine = false; 6197 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6198 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 6199 " paramC);\n" 6200 "void functionDecl(int A, int B,\n" 6201 " int C);"), 6202 format(Input, Style)); 6203 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 6204 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 6205 " paramC);\n" 6206 "void functionDecl(int A, int B,\n" 6207 " int C);"), 6208 format(Input, Style)); 6209 // However, BAS_AlwaysBreak should take precedence over 6210 // AllowAllArgumentsOnNextLine. 6211 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 6212 EXPECT_EQ(StringRef("functionCall(\n" 6213 " paramA, paramB, paramC);\n" 6214 "void functionDecl(\n" 6215 " int A, int B, int C);"), 6216 format(Input, Style)); 6217 6218 // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the 6219 // first argument. 6220 Style.AllowAllArgumentsOnNextLine = true; 6221 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 6222 EXPECT_EQ(StringRef("functionCall(\n" 6223 " paramA, paramB, paramC);\n" 6224 "void functionDecl(\n" 6225 " int A, int B, int C);"), 6226 format(Input, Style)); 6227 // It wouldn't fit on one line with aligned parameters so this setting 6228 // doesn't change anything for BAS_Align. 6229 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 6230 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 6231 " paramC);\n" 6232 "void functionDecl(int A, int B,\n" 6233 " int C);"), 6234 format(Input, Style)); 6235 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6236 EXPECT_EQ(StringRef("functionCall(\n" 6237 " paramA, paramB, paramC);\n" 6238 "void functionDecl(\n" 6239 " int A, int B, int C);"), 6240 format(Input, Style)); 6241 } 6242 6243 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) { 6244 FormatStyle Style = getLLVMStyle(); 6245 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 6246 6247 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 6248 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}", 6249 getStyleWithColumns(Style, 45)); 6250 verifyFormat("Constructor() :\n" 6251 " Initializer(FitsOnTheLine) {}", 6252 getStyleWithColumns(Style, 44)); 6253 verifyFormat("Constructor() :\n" 6254 " Initializer(FitsOnTheLine) {}", 6255 getStyleWithColumns(Style, 43)); 6256 6257 verifyFormat("template <typename T>\n" 6258 "Constructor() : Initializer(FitsOnTheLine) {}", 6259 getStyleWithColumns(Style, 50)); 6260 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 6261 verifyFormat( 6262 "SomeClass::Constructor() :\n" 6263 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 6264 Style); 6265 6266 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = false; 6267 verifyFormat( 6268 "SomeClass::Constructor() :\n" 6269 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 6270 Style); 6271 6272 verifyFormat( 6273 "SomeClass::Constructor() :\n" 6274 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6275 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6276 Style); 6277 verifyFormat( 6278 "SomeClass::Constructor() :\n" 6279 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6280 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 6281 Style); 6282 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6283 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 6284 " aaaaaaaaaa(aaaaaa) {}", 6285 Style); 6286 6287 verifyFormat("Constructor() :\n" 6288 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6289 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6290 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6291 " aaaaaaaaaaaaaaaaaaaaaaa() {}", 6292 Style); 6293 6294 verifyFormat("Constructor() :\n" 6295 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6296 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6297 Style); 6298 6299 verifyFormat("Constructor(int Parameter = 0) :\n" 6300 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 6301 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}", 6302 Style); 6303 verifyFormat("Constructor() :\n" 6304 " aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 6305 "}", 6306 getStyleWithColumns(Style, 60)); 6307 verifyFormat("Constructor() :\n" 6308 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6309 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}", 6310 Style); 6311 6312 // Here a line could be saved by splitting the second initializer onto two 6313 // lines, but that is not desirable. 6314 verifyFormat("Constructor() :\n" 6315 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 6316 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 6317 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6318 Style); 6319 6320 FormatStyle OnePerLine = Style; 6321 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 6322 OnePerLine.AllowAllConstructorInitializersOnNextLine = false; 6323 verifyFormat("SomeClass::Constructor() :\n" 6324 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6325 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6326 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6327 OnePerLine); 6328 verifyFormat("SomeClass::Constructor() :\n" 6329 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 6330 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6331 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6332 OnePerLine); 6333 verifyFormat("MyClass::MyClass(int var) :\n" 6334 " some_var_(var), // 4 space indent\n" 6335 " some_other_var_(var + 1) { // lined up\n" 6336 "}", 6337 OnePerLine); 6338 verifyFormat("Constructor() :\n" 6339 " aaaaa(aaaaaa),\n" 6340 " aaaaa(aaaaaa),\n" 6341 " aaaaa(aaaaaa),\n" 6342 " aaaaa(aaaaaa),\n" 6343 " aaaaa(aaaaaa) {}", 6344 OnePerLine); 6345 verifyFormat("Constructor() :\n" 6346 " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 6347 " aaaaaaaaaaaaaaaaaaaaaa) {}", 6348 OnePerLine); 6349 OnePerLine.BinPackParameters = false; 6350 verifyFormat("Constructor() :\n" 6351 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 6352 " aaaaaaaaaaa().aaa(),\n" 6353 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6354 OnePerLine); 6355 OnePerLine.ColumnLimit = 60; 6356 verifyFormat("Constructor() :\n" 6357 " aaaaaaaaaaaaaaaaaaaa(a),\n" 6358 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 6359 OnePerLine); 6360 6361 EXPECT_EQ("Constructor() :\n" 6362 " // Comment forcing unwanted break.\n" 6363 " aaaa(aaaa) {}", 6364 format("Constructor() :\n" 6365 " // Comment forcing unwanted break.\n" 6366 " aaaa(aaaa) {}", 6367 Style)); 6368 6369 Style.ColumnLimit = 0; 6370 verifyFormat("SomeClass::Constructor() :\n" 6371 " a(a) {}", 6372 Style); 6373 verifyFormat("SomeClass::Constructor() noexcept :\n" 6374 " a(a) {}", 6375 Style); 6376 verifyFormat("SomeClass::Constructor() :\n" 6377 " a(a), b(b), c(c) {}", 6378 Style); 6379 verifyFormat("SomeClass::Constructor() :\n" 6380 " a(a) {\n" 6381 " foo();\n" 6382 " bar();\n" 6383 "}", 6384 Style); 6385 6386 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 6387 verifyFormat("SomeClass::Constructor() :\n" 6388 " a(a), b(b), c(c) {\n" 6389 "}", 6390 Style); 6391 verifyFormat("SomeClass::Constructor() :\n" 6392 " a(a) {\n" 6393 "}", 6394 Style); 6395 6396 Style.ColumnLimit = 80; 6397 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 6398 Style.ConstructorInitializerIndentWidth = 2; 6399 verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style); 6400 verifyFormat("SomeClass::Constructor() :\n" 6401 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6402 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}", 6403 Style); 6404 6405 // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as 6406 // well 6407 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 6408 verifyFormat( 6409 "class SomeClass\n" 6410 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6411 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 6412 Style); 6413 Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 6414 verifyFormat( 6415 "class SomeClass\n" 6416 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6417 " , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 6418 Style); 6419 Style.BreakInheritanceList = FormatStyle::BILS_AfterColon; 6420 verifyFormat( 6421 "class SomeClass :\n" 6422 " public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6423 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 6424 Style); 6425 Style.BreakInheritanceList = FormatStyle::BILS_AfterComma; 6426 verifyFormat( 6427 "class SomeClass\n" 6428 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6429 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 6430 Style); 6431 } 6432 6433 #ifndef EXPENSIVE_CHECKS 6434 // Expensive checks enables libstdc++ checking which includes validating the 6435 // state of ranges used in std::priority_queue - this blows out the 6436 // runtime/scalability of the function and makes this test unacceptably slow. 6437 TEST_F(FormatTest, MemoizationTests) { 6438 // This breaks if the memoization lookup does not take \c Indent and 6439 // \c LastSpace into account. 6440 verifyFormat( 6441 "extern CFRunLoopTimerRef\n" 6442 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 6443 " CFTimeInterval interval, CFOptionFlags flags,\n" 6444 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 6445 " CFRunLoopTimerContext *context) {}"); 6446 6447 // Deep nesting somewhat works around our memoization. 6448 verifyFormat( 6449 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 6450 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 6451 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 6452 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 6453 " aaaaa())))))))))))))))))))))))))))))))))))))));", 6454 getLLVMStyleWithColumns(65)); 6455 verifyFormat( 6456 "aaaaa(\n" 6457 " aaaaa,\n" 6458 " aaaaa(\n" 6459 " aaaaa,\n" 6460 " aaaaa(\n" 6461 " aaaaa,\n" 6462 " aaaaa(\n" 6463 " aaaaa,\n" 6464 " aaaaa(\n" 6465 " aaaaa,\n" 6466 " aaaaa(\n" 6467 " aaaaa,\n" 6468 " aaaaa(\n" 6469 " aaaaa,\n" 6470 " aaaaa(\n" 6471 " aaaaa,\n" 6472 " aaaaa(\n" 6473 " aaaaa,\n" 6474 " aaaaa(\n" 6475 " aaaaa,\n" 6476 " aaaaa(\n" 6477 " aaaaa,\n" 6478 " aaaaa(\n" 6479 " aaaaa,\n" 6480 " aaaaa))))))))))));", 6481 getLLVMStyleWithColumns(65)); 6482 verifyFormat( 6483 "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" 6484 " a),\n" 6485 " a),\n" 6486 " a),\n" 6487 " a),\n" 6488 " a),\n" 6489 " a),\n" 6490 " a),\n" 6491 " a),\n" 6492 " a),\n" 6493 " a),\n" 6494 " a),\n" 6495 " a),\n" 6496 " a),\n" 6497 " a),\n" 6498 " a),\n" 6499 " a),\n" 6500 " a)", 6501 getLLVMStyleWithColumns(65)); 6502 6503 // This test takes VERY long when memoization is broken. 6504 FormatStyle OnePerLine = getLLVMStyle(); 6505 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 6506 OnePerLine.BinPackParameters = false; 6507 std::string input = "Constructor()\n" 6508 " : aaaa(a,\n"; 6509 for (unsigned i = 0, e = 80; i != e; ++i) { 6510 input += " a,\n"; 6511 } 6512 input += " a) {}"; 6513 verifyFormat(input, OnePerLine); 6514 } 6515 #endif 6516 6517 TEST_F(FormatTest, BreaksAsHighAsPossible) { 6518 verifyFormat( 6519 "void f() {\n" 6520 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 6521 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 6522 " f();\n" 6523 "}"); 6524 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 6525 " Intervals[i - 1].getRange().getLast()) {\n}"); 6526 } 6527 6528 TEST_F(FormatTest, BreaksFunctionDeclarations) { 6529 // Principially, we break function declarations in a certain order: 6530 // 1) break amongst arguments. 6531 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 6532 " Cccccccccccccc cccccccccccccc);"); 6533 verifyFormat("template <class TemplateIt>\n" 6534 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 6535 " TemplateIt *stop) {}"); 6536 6537 // 2) break after return type. 6538 verifyFormat( 6539 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6540 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", 6541 getGoogleStyle()); 6542 6543 // 3) break after (. 6544 verifyFormat( 6545 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 6546 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", 6547 getGoogleStyle()); 6548 6549 // 4) break before after nested name specifiers. 6550 verifyFormat( 6551 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6552 "SomeClasssssssssssssssssssssssssssssssssssssss::\n" 6553 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", 6554 getGoogleStyle()); 6555 6556 // However, there are exceptions, if a sufficient amount of lines can be 6557 // saved. 6558 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 6559 // more adjusting. 6560 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 6561 " Cccccccccccccc cccccccccc,\n" 6562 " Cccccccccccccc cccccccccc,\n" 6563 " Cccccccccccccc cccccccccc,\n" 6564 " Cccccccccccccc cccccccccc);"); 6565 verifyFormat( 6566 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6567 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6568 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6569 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", 6570 getGoogleStyle()); 6571 verifyFormat( 6572 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 6573 " Cccccccccccccc cccccccccc,\n" 6574 " Cccccccccccccc cccccccccc,\n" 6575 " Cccccccccccccc cccccccccc,\n" 6576 " Cccccccccccccc cccccccccc,\n" 6577 " Cccccccccccccc cccccccccc,\n" 6578 " Cccccccccccccc cccccccccc);"); 6579 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 6580 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6581 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6582 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6583 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 6584 6585 // Break after multi-line parameters. 6586 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6587 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6588 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6589 " bbbb bbbb);"); 6590 verifyFormat("void SomeLoooooooooooongFunction(\n" 6591 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6592 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6593 " int bbbbbbbbbbbbb);"); 6594 6595 // Treat overloaded operators like other functions. 6596 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 6597 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 6598 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 6599 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 6600 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 6601 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 6602 verifyGoogleFormat( 6603 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 6604 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 6605 verifyGoogleFormat( 6606 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 6607 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 6608 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6609 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 6610 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 6611 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 6612 verifyGoogleFormat( 6613 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" 6614 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6615 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); 6616 verifyGoogleFormat("template <typename T>\n" 6617 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6618 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n" 6619 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);"); 6620 6621 FormatStyle Style = getLLVMStyle(); 6622 Style.PointerAlignment = FormatStyle::PAS_Left; 6623 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6624 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", 6625 Style); 6626 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 6627 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6628 Style); 6629 } 6630 6631 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) { 6632 // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516: 6633 // Prefer keeping `::` followed by `operator` together. 6634 EXPECT_EQ("const aaaa::bbbbbbb &\n" 6635 "ccccccccc::operator++() {\n" 6636 " stuff();\n" 6637 "}", 6638 format("const aaaa::bbbbbbb\n" 6639 "&ccccccccc::operator++() { stuff(); }", 6640 getLLVMStyleWithColumns(40))); 6641 } 6642 6643 TEST_F(FormatTest, TrailingReturnType) { 6644 verifyFormat("auto foo() -> int;\n"); 6645 // correct trailing return type spacing 6646 verifyFormat("auto operator->() -> int;\n"); 6647 verifyFormat("auto operator++(int) -> int;\n"); 6648 6649 verifyFormat("struct S {\n" 6650 " auto bar() const -> int;\n" 6651 "};"); 6652 verifyFormat("template <size_t Order, typename T>\n" 6653 "auto load_img(const std::string &filename)\n" 6654 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 6655 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 6656 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 6657 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 6658 verifyFormat("template <typename T>\n" 6659 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" 6660 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); 6661 6662 // Not trailing return types. 6663 verifyFormat("void f() { auto a = b->c(); }"); 6664 } 6665 6666 TEST_F(FormatTest, DeductionGuides) { 6667 verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;"); 6668 verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;"); 6669 verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;"); 6670 verifyFormat( 6671 "template <class... T>\n" 6672 "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;"); 6673 verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;"); 6674 verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;"); 6675 verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;"); 6676 verifyFormat("template <class T> A() -> A<(3 < 2)>;"); 6677 verifyFormat("template <class T> A() -> A<((3) < (2))>;"); 6678 verifyFormat("template <class T> x() -> x<1>;"); 6679 verifyFormat("template <class T> explicit x(T &) -> x<1>;"); 6680 6681 // Ensure not deduction guides. 6682 verifyFormat("c()->f<int>();"); 6683 verifyFormat("x()->foo<1>;"); 6684 verifyFormat("x = p->foo<3>();"); 6685 verifyFormat("x()->x<1>();"); 6686 verifyFormat("x()->x<1>;"); 6687 } 6688 6689 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 6690 // Avoid breaking before trailing 'const' or other trailing annotations, if 6691 // they are not function-like. 6692 FormatStyle Style = getGoogleStyle(); 6693 Style.ColumnLimit = 47; 6694 verifyFormat("void someLongFunction(\n" 6695 " int someLoooooooooooooongParameter) const {\n}", 6696 getLLVMStyleWithColumns(47)); 6697 verifyFormat("LoooooongReturnType\n" 6698 "someLoooooooongFunction() const {}", 6699 getLLVMStyleWithColumns(47)); 6700 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 6701 " const {}", 6702 Style); 6703 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 6704 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 6705 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 6706 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 6707 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 6708 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 6709 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 6710 " aaaaaaaaaaa aaaaa) const override;"); 6711 verifyGoogleFormat( 6712 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 6713 " const override;"); 6714 6715 // Even if the first parameter has to be wrapped. 6716 verifyFormat("void someLongFunction(\n" 6717 " int someLongParameter) const {}", 6718 getLLVMStyleWithColumns(46)); 6719 verifyFormat("void someLongFunction(\n" 6720 " int someLongParameter) const {}", 6721 Style); 6722 verifyFormat("void someLongFunction(\n" 6723 " int someLongParameter) override {}", 6724 Style); 6725 verifyFormat("void someLongFunction(\n" 6726 " int someLongParameter) OVERRIDE {}", 6727 Style); 6728 verifyFormat("void someLongFunction(\n" 6729 " int someLongParameter) final {}", 6730 Style); 6731 verifyFormat("void someLongFunction(\n" 6732 " int someLongParameter) FINAL {}", 6733 Style); 6734 verifyFormat("void someLongFunction(\n" 6735 " int parameter) const override {}", 6736 Style); 6737 6738 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 6739 verifyFormat("void someLongFunction(\n" 6740 " int someLongParameter) const\n" 6741 "{\n" 6742 "}", 6743 Style); 6744 6745 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 6746 verifyFormat("void someLongFunction(\n" 6747 " int someLongParameter) const\n" 6748 " {\n" 6749 " }", 6750 Style); 6751 6752 // Unless these are unknown annotations. 6753 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 6754 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6755 " LONG_AND_UGLY_ANNOTATION;"); 6756 6757 // Breaking before function-like trailing annotations is fine to keep them 6758 // close to their arguments. 6759 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6760 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 6761 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 6762 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 6763 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 6764 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 6765 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 6766 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 6767 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 6768 6769 verifyFormat( 6770 "void aaaaaaaaaaaaaaaaaa()\n" 6771 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 6772 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 6773 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6774 " __attribute__((unused));"); 6775 verifyGoogleFormat( 6776 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6777 " GUARDED_BY(aaaaaaaaaaaa);"); 6778 verifyGoogleFormat( 6779 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6780 " GUARDED_BY(aaaaaaaaaaaa);"); 6781 verifyGoogleFormat( 6782 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 6783 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6784 verifyGoogleFormat( 6785 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 6786 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 6787 } 6788 6789 TEST_F(FormatTest, FunctionAnnotations) { 6790 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 6791 "int OldFunction(const string ¶meter) {}"); 6792 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 6793 "string OldFunction(const string ¶meter) {}"); 6794 verifyFormat("template <typename T>\n" 6795 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 6796 "string OldFunction(const string ¶meter) {}"); 6797 6798 // Not function annotations. 6799 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6800 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 6801 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" 6802 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); 6803 verifyFormat("MACRO(abc).function() // wrap\n" 6804 " << abc;"); 6805 verifyFormat("MACRO(abc)->function() // wrap\n" 6806 " << abc;"); 6807 verifyFormat("MACRO(abc)::function() // wrap\n" 6808 " << abc;"); 6809 } 6810 6811 TEST_F(FormatTest, BreaksDesireably) { 6812 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 6813 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 6814 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 6815 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6816 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 6817 "}"); 6818 6819 verifyFormat( 6820 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6821 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 6822 6823 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6824 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6825 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 6826 6827 verifyFormat( 6828 "aaaaaaaa(aaaaaaaaaaaaa,\n" 6829 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6830 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 6831 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6832 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 6833 6834 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 6835 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6836 6837 verifyFormat( 6838 "void f() {\n" 6839 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 6840 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 6841 "}"); 6842 verifyFormat( 6843 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6844 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 6845 verifyFormat( 6846 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6847 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 6848 verifyFormat( 6849 "aaaaaa(aaa,\n" 6850 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6851 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6852 " aaaa);"); 6853 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6854 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6855 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6856 6857 // Indent consistently independent of call expression and unary operator. 6858 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 6859 " dddddddddddddddddddddddddddddd));"); 6860 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 6861 " dddddddddddddddddddddddddddddd));"); 6862 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 6863 " dddddddddddddddddddddddddddddd));"); 6864 6865 // This test case breaks on an incorrect memoization, i.e. an optimization not 6866 // taking into account the StopAt value. 6867 verifyFormat( 6868 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 6869 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 6870 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 6871 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6872 6873 verifyFormat("{\n {\n {\n" 6874 " Annotation.SpaceRequiredBefore =\n" 6875 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 6876 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 6877 " }\n }\n}"); 6878 6879 // Break on an outer level if there was a break on an inner level. 6880 EXPECT_EQ("f(g(h(a, // comment\n" 6881 " b, c),\n" 6882 " d, e),\n" 6883 " x, y);", 6884 format("f(g(h(a, // comment\n" 6885 " b, c), d, e), x, y);")); 6886 6887 // Prefer breaking similar line breaks. 6888 verifyFormat( 6889 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 6890 " NSTrackingMouseEnteredAndExited |\n" 6891 " NSTrackingActiveAlways;"); 6892 } 6893 6894 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 6895 FormatStyle NoBinPacking = getGoogleStyle(); 6896 NoBinPacking.BinPackParameters = false; 6897 NoBinPacking.BinPackArguments = true; 6898 verifyFormat("void f() {\n" 6899 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 6900 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 6901 "}", 6902 NoBinPacking); 6903 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 6904 " int aaaaaaaaaaaaaaaaaaaa,\n" 6905 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6906 NoBinPacking); 6907 6908 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 6909 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6910 " vector<int> bbbbbbbbbbbbbbb);", 6911 NoBinPacking); 6912 // FIXME: This behavior difference is probably not wanted. However, currently 6913 // we cannot distinguish BreakBeforeParameter being set because of the wrapped 6914 // template arguments from BreakBeforeParameter being set because of the 6915 // one-per-line formatting. 6916 verifyFormat( 6917 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n" 6918 " aaaaaaaaaa> aaaaaaaaaa);", 6919 NoBinPacking); 6920 verifyFormat( 6921 "void fffffffffff(\n" 6922 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n" 6923 " aaaaaaaaaa);"); 6924 } 6925 6926 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 6927 FormatStyle NoBinPacking = getGoogleStyle(); 6928 NoBinPacking.BinPackParameters = false; 6929 NoBinPacking.BinPackArguments = false; 6930 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 6931 " aaaaaaaaaaaaaaaaaaaa,\n" 6932 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 6933 NoBinPacking); 6934 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 6935 " aaaaaaaaaaaaa,\n" 6936 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 6937 NoBinPacking); 6938 verifyFormat( 6939 "aaaaaaaa(aaaaaaaaaaaaa,\n" 6940 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6941 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 6942 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6943 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 6944 NoBinPacking); 6945 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 6946 " .aaaaaaaaaaaaaaaaaa();", 6947 NoBinPacking); 6948 verifyFormat("void f() {\n" 6949 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6950 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 6951 "}", 6952 NoBinPacking); 6953 6954 verifyFormat( 6955 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6956 " aaaaaaaaaaaa,\n" 6957 " aaaaaaaaaaaa);", 6958 NoBinPacking); 6959 verifyFormat( 6960 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 6961 " ddddddddddddddddddddddddddddd),\n" 6962 " test);", 6963 NoBinPacking); 6964 6965 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 6966 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 6967 " aaaaaaaaaaaaaaaaaaaaaaa>\n" 6968 " aaaaaaaaaaaaaaaaaa;", 6969 NoBinPacking); 6970 verifyFormat("a(\"a\"\n" 6971 " \"a\",\n" 6972 " a);"); 6973 6974 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 6975 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 6976 " aaaaaaaaa,\n" 6977 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 6978 NoBinPacking); 6979 verifyFormat( 6980 "void f() {\n" 6981 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 6982 " .aaaaaaa();\n" 6983 "}", 6984 NoBinPacking); 6985 verifyFormat( 6986 "template <class SomeType, class SomeOtherType>\n" 6987 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 6988 NoBinPacking); 6989 } 6990 6991 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 6992 FormatStyle Style = getLLVMStyleWithColumns(15); 6993 Style.ExperimentalAutoDetectBinPacking = true; 6994 EXPECT_EQ("aaa(aaaa,\n" 6995 " aaaa,\n" 6996 " aaaa);\n" 6997 "aaa(aaaa,\n" 6998 " aaaa,\n" 6999 " aaaa);", 7000 format("aaa(aaaa,\n" // one-per-line 7001 " aaaa,\n" 7002 " aaaa );\n" 7003 "aaa(aaaa, aaaa, aaaa);", // inconclusive 7004 Style)); 7005 EXPECT_EQ("aaa(aaaa, aaaa,\n" 7006 " aaaa);\n" 7007 "aaa(aaaa, aaaa,\n" 7008 " aaaa);", 7009 format("aaa(aaaa, aaaa,\n" // bin-packed 7010 " aaaa );\n" 7011 "aaa(aaaa, aaaa, aaaa);", // inconclusive 7012 Style)); 7013 } 7014 7015 TEST_F(FormatTest, FormatsBuilderPattern) { 7016 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 7017 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 7018 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 7019 " .StartsWith(\".init\", ORDER_INIT)\n" 7020 " .StartsWith(\".fini\", ORDER_FINI)\n" 7021 " .StartsWith(\".hash\", ORDER_HASH)\n" 7022 " .Default(ORDER_TEXT);\n"); 7023 7024 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 7025 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 7026 verifyFormat("aaaaaaa->aaaaaaa\n" 7027 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7028 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7029 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 7030 verifyFormat( 7031 "aaaaaaa->aaaaaaa\n" 7032 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7033 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 7034 verifyFormat( 7035 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 7036 " aaaaaaaaaaaaaa);"); 7037 verifyFormat( 7038 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 7039 " aaaaaa->aaaaaaaaaaaa()\n" 7040 " ->aaaaaaaaaaaaaaaa(\n" 7041 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7042 " ->aaaaaaaaaaaaaaaaa();"); 7043 verifyGoogleFormat( 7044 "void f() {\n" 7045 " someo->Add((new util::filetools::Handler(dir))\n" 7046 " ->OnEvent1(NewPermanentCallback(\n" 7047 " this, &HandlerHolderClass::EventHandlerCBA))\n" 7048 " ->OnEvent2(NewPermanentCallback(\n" 7049 " this, &HandlerHolderClass::EventHandlerCBB))\n" 7050 " ->OnEvent3(NewPermanentCallback(\n" 7051 " this, &HandlerHolderClass::EventHandlerCBC))\n" 7052 " ->OnEvent5(NewPermanentCallback(\n" 7053 " this, &HandlerHolderClass::EventHandlerCBD))\n" 7054 " ->OnEvent6(NewPermanentCallback(\n" 7055 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 7056 "}"); 7057 7058 verifyFormat( 7059 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 7060 verifyFormat("aaaaaaaaaaaaaaa()\n" 7061 " .aaaaaaaaaaaaaaa()\n" 7062 " .aaaaaaaaaaaaaaa()\n" 7063 " .aaaaaaaaaaaaaaa()\n" 7064 " .aaaaaaaaaaaaaaa();"); 7065 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 7066 " .aaaaaaaaaaaaaaa()\n" 7067 " .aaaaaaaaaaaaaaa()\n" 7068 " .aaaaaaaaaaaaaaa();"); 7069 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 7070 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 7071 " .aaaaaaaaaaaaaaa();"); 7072 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 7073 " ->aaaaaaaaaaaaaae(0)\n" 7074 " ->aaaaaaaaaaaaaaa();"); 7075 7076 // Don't linewrap after very short segments. 7077 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7078 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7079 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7080 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7081 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7082 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7083 verifyFormat("aaa()\n" 7084 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7085 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7086 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7087 7088 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 7089 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7090 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 7091 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 7092 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 7093 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 7094 7095 // Prefer not to break after empty parentheses. 7096 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 7097 " First->LastNewlineOffset);"); 7098 7099 // Prefer not to create "hanging" indents. 7100 verifyFormat( 7101 "return !soooooooooooooome_map\n" 7102 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7103 " .second;"); 7104 verifyFormat( 7105 "return aaaaaaaaaaaaaaaa\n" 7106 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n" 7107 " .aaaa(aaaaaaaaaaaaaa);"); 7108 // No hanging indent here. 7109 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n" 7110 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7111 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n" 7112 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7113 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 7114 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7115 getLLVMStyleWithColumns(60)); 7116 verifyFormat("aaaaaaaaaaaaaaaaaa\n" 7117 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 7118 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7119 getLLVMStyleWithColumns(59)); 7120 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7121 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7122 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7123 7124 // Dont break if only closing statements before member call 7125 verifyFormat("test() {\n" 7126 " ([]() -> {\n" 7127 " int b = 32;\n" 7128 " return 3;\n" 7129 " }).foo();\n" 7130 "}"); 7131 verifyFormat("test() {\n" 7132 " (\n" 7133 " []() -> {\n" 7134 " int b = 32;\n" 7135 " return 3;\n" 7136 " },\n" 7137 " foo, bar)\n" 7138 " .foo();\n" 7139 "}"); 7140 verifyFormat("test() {\n" 7141 " ([]() -> {\n" 7142 " int b = 32;\n" 7143 " return 3;\n" 7144 " })\n" 7145 " .foo()\n" 7146 " .bar();\n" 7147 "}"); 7148 verifyFormat("test() {\n" 7149 " ([]() -> {\n" 7150 " int b = 32;\n" 7151 " return 3;\n" 7152 " })\n" 7153 " .foo(\"aaaaaaaaaaaaaaaaa\"\n" 7154 " \"bbbb\");\n" 7155 "}", 7156 getLLVMStyleWithColumns(30)); 7157 } 7158 7159 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 7160 verifyFormat( 7161 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7162 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 7163 verifyFormat( 7164 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 7165 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 7166 7167 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 7168 " ccccccccccccccccccccccccc) {\n}"); 7169 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 7170 " ccccccccccccccccccccccccc) {\n}"); 7171 7172 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 7173 " ccccccccccccccccccccccccc) {\n}"); 7174 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 7175 " ccccccccccccccccccccccccc) {\n}"); 7176 7177 verifyFormat( 7178 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 7179 " ccccccccccccccccccccccccc) {\n}"); 7180 verifyFormat( 7181 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 7182 " ccccccccccccccccccccccccc) {\n}"); 7183 7184 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 7185 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 7186 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 7187 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 7188 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 7189 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 7190 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 7191 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 7192 7193 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 7194 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 7195 " aaaaaaaaaaaaaaa != aa) {\n}"); 7196 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 7197 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 7198 " aaaaaaaaaaaaaaa != aa) {\n}"); 7199 } 7200 7201 TEST_F(FormatTest, BreaksAfterAssignments) { 7202 verifyFormat( 7203 "unsigned Cost =\n" 7204 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 7205 " SI->getPointerAddressSpaceee());\n"); 7206 verifyFormat( 7207 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 7208 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 7209 7210 verifyFormat( 7211 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 7212 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 7213 verifyFormat("unsigned OriginalStartColumn =\n" 7214 " SourceMgr.getSpellingColumnNumber(\n" 7215 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 7216 " 1;"); 7217 } 7218 7219 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) { 7220 FormatStyle Style = getLLVMStyle(); 7221 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 7222 " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;", 7223 Style); 7224 7225 Style.PenaltyBreakAssignment = 20; 7226 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 7227 " cccccccccccccccccccccccccc;", 7228 Style); 7229 } 7230 7231 TEST_F(FormatTest, AlignsAfterAssignments) { 7232 verifyFormat( 7233 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7234 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7235 verifyFormat( 7236 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7237 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7238 verifyFormat( 7239 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7240 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7241 verifyFormat( 7242 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7243 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 7244 verifyFormat( 7245 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 7246 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 7247 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 7248 } 7249 7250 TEST_F(FormatTest, AlignsAfterReturn) { 7251 verifyFormat( 7252 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7253 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7254 verifyFormat( 7255 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7256 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 7257 verifyFormat( 7258 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 7259 " aaaaaaaaaaaaaaaaaaaaaa();"); 7260 verifyFormat( 7261 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 7262 " aaaaaaaaaaaaaaaaaaaaaa());"); 7263 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7264 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7265 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7266 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 7267 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7268 verifyFormat("return\n" 7269 " // true if code is one of a or b.\n" 7270 " code == a || code == b;"); 7271 } 7272 7273 TEST_F(FormatTest, AlignsAfterOpenBracket) { 7274 verifyFormat( 7275 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 7276 " aaaaaaaaa aaaaaaa) {}"); 7277 verifyFormat( 7278 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 7279 " aaaaaaaaaaa aaaaaaaaa);"); 7280 verifyFormat( 7281 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 7282 " aaaaaaaaaaaaaaaaaaaaa));"); 7283 FormatStyle Style = getLLVMStyle(); 7284 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7285 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7286 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 7287 Style); 7288 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 7289 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 7290 Style); 7291 verifyFormat("SomeLongVariableName->someFunction(\n" 7292 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 7293 Style); 7294 verifyFormat( 7295 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 7296 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 7297 Style); 7298 verifyFormat( 7299 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 7300 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7301 Style); 7302 verifyFormat( 7303 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 7304 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 7305 Style); 7306 7307 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n" 7308 " ccccccc(aaaaaaaaaaaaaaaaa, //\n" 7309 " b));", 7310 Style); 7311 7312 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 7313 Style.BinPackArguments = false; 7314 Style.BinPackParameters = false; 7315 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7316 " aaaaaaaaaaa aaaaaaaa,\n" 7317 " aaaaaaaaa aaaaaaa,\n" 7318 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 7319 Style); 7320 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 7321 " aaaaaaaaaaa aaaaaaaaa,\n" 7322 " aaaaaaaaaaa aaaaaaaaa,\n" 7323 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7324 Style); 7325 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" 7326 " aaaaaaaaaaaaaaa,\n" 7327 " aaaaaaaaaaaaaaaaaaaaa,\n" 7328 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 7329 Style); 7330 verifyFormat( 7331 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" 7332 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 7333 Style); 7334 verifyFormat( 7335 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" 7336 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 7337 Style); 7338 verifyFormat( 7339 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 7340 " aaaaaaaaaaaaaaaaaaaaa(\n" 7341 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n" 7342 " aaaaaaaaaaaaaaaa);", 7343 Style); 7344 verifyFormat( 7345 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 7346 " aaaaaaaaaaaaaaaaaaaaa(\n" 7347 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n" 7348 " aaaaaaaaaaaaaaaa);", 7349 Style); 7350 } 7351 7352 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 7353 FormatStyle Style = getLLVMStyleWithColumns(40); 7354 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7355 " bbbbbbbbbbbbbbbbbbbbbb);", 7356 Style); 7357 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 7358 Style.AlignOperands = FormatStyle::OAS_DontAlign; 7359 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7360 " bbbbbbbbbbbbbbbbbbbbbb);", 7361 Style); 7362 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7363 Style.AlignOperands = FormatStyle::OAS_Align; 7364 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7365 " bbbbbbbbbbbbbbbbbbbbbb);", 7366 Style); 7367 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7368 Style.AlignOperands = FormatStyle::OAS_DontAlign; 7369 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7370 " bbbbbbbbbbbbbbbbbbbbbb);", 7371 Style); 7372 } 7373 7374 TEST_F(FormatTest, BreaksConditionalExpressions) { 7375 verifyFormat( 7376 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7377 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7378 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7379 verifyFormat( 7380 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 7381 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7382 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7383 verifyFormat( 7384 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7385 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7386 verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n" 7387 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7388 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7389 verifyFormat( 7390 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 7391 " : aaaaaaaaaaaaa);"); 7392 verifyFormat( 7393 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7394 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7395 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7396 " aaaaaaaaaaaaa);"); 7397 verifyFormat( 7398 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7399 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7400 " aaaaaaaaaaaaa);"); 7401 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7402 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7403 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7404 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7405 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7406 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7407 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7408 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7409 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7410 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7411 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7412 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7413 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7414 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7415 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7416 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7417 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7418 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7419 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7420 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7421 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 7422 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7423 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7424 " : aaaaaaaaaaaaaaaa;"); 7425 verifyFormat( 7426 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7427 " ? aaaaaaaaaaaaaaa\n" 7428 " : aaaaaaaaaaaaaaa;"); 7429 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 7430 " aaaaaaaaa\n" 7431 " ? b\n" 7432 " : c);"); 7433 verifyFormat("return aaaa == bbbb\n" 7434 " // comment\n" 7435 " ? aaaa\n" 7436 " : bbbb;"); 7437 verifyFormat("unsigned Indent =\n" 7438 " format(TheLine.First,\n" 7439 " IndentForLevel[TheLine.Level] >= 0\n" 7440 " ? IndentForLevel[TheLine.Level]\n" 7441 " : TheLine * 2,\n" 7442 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 7443 getLLVMStyleWithColumns(60)); 7444 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 7445 " ? aaaaaaaaaaaaaaa\n" 7446 " : bbbbbbbbbbbbbbb //\n" 7447 " ? ccccccccccccccc\n" 7448 " : ddddddddddddddd;"); 7449 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 7450 " ? aaaaaaaaaaaaaaa\n" 7451 " : (bbbbbbbbbbbbbbb //\n" 7452 " ? ccccccccccccccc\n" 7453 " : ddddddddddddddd);"); 7454 verifyFormat( 7455 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7456 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7457 " aaaaaaaaaaaaaaaaaaaaa +\n" 7458 " aaaaaaaaaaaaaaaaaaaaa\n" 7459 " : aaaaaaaaaa;"); 7460 verifyFormat( 7461 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7462 " : aaaaaaaaaaaaaaaaaaaaaa\n" 7463 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7464 7465 FormatStyle NoBinPacking = getLLVMStyle(); 7466 NoBinPacking.BinPackArguments = false; 7467 verifyFormat( 7468 "void f() {\n" 7469 " g(aaa,\n" 7470 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 7471 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7472 " ? aaaaaaaaaaaaaaa\n" 7473 " : aaaaaaaaaaaaaaa);\n" 7474 "}", 7475 NoBinPacking); 7476 verifyFormat( 7477 "void f() {\n" 7478 " g(aaa,\n" 7479 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 7480 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7481 " ?: aaaaaaaaaaaaaaa);\n" 7482 "}", 7483 NoBinPacking); 7484 7485 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 7486 " // comment.\n" 7487 " ccccccccccccccccccccccccccccccccccccccc\n" 7488 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7489 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 7490 7491 // Assignments in conditional expressions. Apparently not uncommon :-(. 7492 verifyFormat("return a != b\n" 7493 " // comment\n" 7494 " ? a = b\n" 7495 " : a = b;"); 7496 verifyFormat("return a != b\n" 7497 " // comment\n" 7498 " ? a = a != b\n" 7499 " // comment\n" 7500 " ? a = b\n" 7501 " : a\n" 7502 " : a;\n"); 7503 verifyFormat("return a != b\n" 7504 " // comment\n" 7505 " ? a\n" 7506 " : a = a != b\n" 7507 " // comment\n" 7508 " ? a = b\n" 7509 " : a;"); 7510 7511 // Chained conditionals 7512 FormatStyle Style = getLLVMStyle(); 7513 Style.ColumnLimit = 70; 7514 Style.AlignOperands = FormatStyle::OAS_Align; 7515 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7516 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7517 " : 3333333333333333;", 7518 Style); 7519 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7520 " : bbbbbbbbbb ? 2222222222222222\n" 7521 " : 3333333333333333;", 7522 Style); 7523 verifyFormat("return aaaaaaaaaa ? 1111111111111111\n" 7524 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 7525 " : 3333333333333333;", 7526 Style); 7527 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7528 " : bbbbbbbbbbbbbb ? 222222\n" 7529 " : 333333;", 7530 Style); 7531 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7532 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7533 " : cccccccccccccc ? 3333333333333333\n" 7534 " : 4444444444444444;", 7535 Style); 7536 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc)\n" 7537 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7538 " : 3333333333333333;", 7539 Style); 7540 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7541 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7542 " : (aaa ? bbb : ccc);", 7543 Style); 7544 verifyFormat( 7545 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7546 " : cccccccccccccccccc)\n" 7547 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7548 " : 3333333333333333;", 7549 Style); 7550 verifyFormat( 7551 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7552 " : cccccccccccccccccc)\n" 7553 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7554 " : 3333333333333333;", 7555 Style); 7556 verifyFormat( 7557 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7558 " : dddddddddddddddddd)\n" 7559 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7560 " : 3333333333333333;", 7561 Style); 7562 verifyFormat( 7563 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7564 " : dddddddddddddddddd)\n" 7565 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7566 " : 3333333333333333;", 7567 Style); 7568 verifyFormat( 7569 "return aaaaaaaaa ? 1111111111111111\n" 7570 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7571 " : a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7572 " : dddddddddddddddddd)\n", 7573 Style); 7574 verifyFormat( 7575 "return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7576 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7577 " : (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7578 " : cccccccccccccccccc);", 7579 Style); 7580 verifyFormat( 7581 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7582 " : ccccccccccccccc ? dddddddddddddddddd\n" 7583 " : eeeeeeeeeeeeeeeeee)\n" 7584 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7585 " : 3333333333333333;", 7586 Style); 7587 verifyFormat( 7588 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7589 " : ccccccccccccccc ? dddddddddddddddddd\n" 7590 " : eeeeeeeeeeeeeeeeee)\n" 7591 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7592 " : 3333333333333333;", 7593 Style); 7594 verifyFormat( 7595 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7596 " : cccccccccccc ? dddddddddddddddddd\n" 7597 " : eeeeeeeeeeeeeeeeee)\n" 7598 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7599 " : 3333333333333333;", 7600 Style); 7601 verifyFormat( 7602 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7603 " : cccccccccccccccccc\n" 7604 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7605 " : 3333333333333333;", 7606 Style); 7607 verifyFormat( 7608 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7609 " : cccccccccccccccc ? dddddddddddddddddd\n" 7610 " : eeeeeeeeeeeeeeeeee\n" 7611 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7612 " : 3333333333333333;", 7613 Style); 7614 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa\n" 7615 " ? (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7616 " : cccccccccccccccccc ? dddddddddddddddddd\n" 7617 " : eeeeeeeeeeeeeeeeee)\n" 7618 " : bbbbbbbbbbbbbbbbbbb ? 2222222222222222\n" 7619 " : 3333333333333333;", 7620 Style); 7621 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaa\n" 7622 " ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7623 " : cccccccccccccccc ? dddddddddddddddddd\n" 7624 " : eeeeeeeeeeeeeeeeee\n" 7625 " : bbbbbbbbbbbbbbbbbbbbbbb ? 2222222222222222\n" 7626 " : 3333333333333333;", 7627 Style); 7628 7629 Style.AlignOperands = FormatStyle::OAS_DontAlign; 7630 Style.BreakBeforeTernaryOperators = false; 7631 // FIXME: Aligning the question marks is weird given DontAlign. 7632 // Consider disabling this alignment in this case. Also check whether this 7633 // will render the adjustment from https://reviews.llvm.org/D82199 7634 // unnecessary. 7635 verifyFormat("int x = aaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa :\n" 7636 " bbbb ? cccccccccccccccccc :\n" 7637 " ddddd;\n", 7638 Style); 7639 7640 EXPECT_EQ( 7641 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n" 7642 " /*\n" 7643 " */\n" 7644 " function() {\n" 7645 " try {\n" 7646 " return JJJJJJJJJJJJJJ(\n" 7647 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n" 7648 " }\n" 7649 " } :\n" 7650 " function() {};", 7651 format( 7652 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n" 7653 " /*\n" 7654 " */\n" 7655 " function() {\n" 7656 " try {\n" 7657 " return JJJJJJJJJJJJJJ(\n" 7658 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n" 7659 " }\n" 7660 " } :\n" 7661 " function() {};", 7662 getGoogleStyle(FormatStyle::LK_JavaScript))); 7663 } 7664 7665 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 7666 FormatStyle Style = getLLVMStyle(); 7667 Style.BreakBeforeTernaryOperators = false; 7668 Style.ColumnLimit = 70; 7669 verifyFormat( 7670 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7671 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7672 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7673 Style); 7674 verifyFormat( 7675 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 7676 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7677 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7678 Style); 7679 verifyFormat( 7680 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7681 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7682 Style); 7683 verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n" 7684 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7685 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7686 Style); 7687 verifyFormat( 7688 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 7689 " aaaaaaaaaaaaa);", 7690 Style); 7691 verifyFormat( 7692 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7693 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7694 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7695 " aaaaaaaaaaaaa);", 7696 Style); 7697 verifyFormat( 7698 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7699 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7700 " aaaaaaaaaaaaa);", 7701 Style); 7702 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7703 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7704 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 7705 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7706 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7707 Style); 7708 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7709 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7710 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7711 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 7712 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7713 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7714 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7715 Style); 7716 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7717 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 7718 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7719 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7720 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7721 Style); 7722 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7723 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7724 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 7725 Style); 7726 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 7727 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7728 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7729 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 7730 Style); 7731 verifyFormat( 7732 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7733 " aaaaaaaaaaaaaaa :\n" 7734 " aaaaaaaaaaaaaaa;", 7735 Style); 7736 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 7737 " aaaaaaaaa ?\n" 7738 " b :\n" 7739 " c);", 7740 Style); 7741 verifyFormat("unsigned Indent =\n" 7742 " format(TheLine.First,\n" 7743 " IndentForLevel[TheLine.Level] >= 0 ?\n" 7744 " IndentForLevel[TheLine.Level] :\n" 7745 " TheLine * 2,\n" 7746 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 7747 Style); 7748 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 7749 " aaaaaaaaaaaaaaa :\n" 7750 " bbbbbbbbbbbbbbb ? //\n" 7751 " ccccccccccccccc :\n" 7752 " ddddddddddddddd;", 7753 Style); 7754 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 7755 " aaaaaaaaaaaaaaa :\n" 7756 " (bbbbbbbbbbbbbbb ? //\n" 7757 " ccccccccccccccc :\n" 7758 " ddddddddddddddd);", 7759 Style); 7760 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7761 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n" 7762 " ccccccccccccccccccccccccccc;", 7763 Style); 7764 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7765 " aaaaa :\n" 7766 " bbbbbbbbbbbbbbb + cccccccccccccccc;", 7767 Style); 7768 7769 // Chained conditionals 7770 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7771 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7772 " 3333333333333333;", 7773 Style); 7774 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7775 " bbbbbbbbbb ? 2222222222222222 :\n" 7776 " 3333333333333333;", 7777 Style); 7778 verifyFormat("return aaaaaaaaaa ? 1111111111111111 :\n" 7779 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7780 " 3333333333333333;", 7781 Style); 7782 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7783 " bbbbbbbbbbbbbbbb ? 222222 :\n" 7784 " 333333;", 7785 Style); 7786 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7787 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7788 " cccccccccccccccc ? 3333333333333333 :\n" 7789 " 4444444444444444;", 7790 Style); 7791 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc) :\n" 7792 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7793 " 3333333333333333;", 7794 Style); 7795 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7796 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7797 " (aaa ? bbb : ccc);", 7798 Style); 7799 verifyFormat( 7800 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7801 " cccccccccccccccccc) :\n" 7802 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7803 " 3333333333333333;", 7804 Style); 7805 verifyFormat( 7806 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7807 " cccccccccccccccccc) :\n" 7808 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7809 " 3333333333333333;", 7810 Style); 7811 verifyFormat( 7812 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7813 " dddddddddddddddddd) :\n" 7814 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7815 " 3333333333333333;", 7816 Style); 7817 verifyFormat( 7818 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7819 " dddddddddddddddddd) :\n" 7820 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7821 " 3333333333333333;", 7822 Style); 7823 verifyFormat( 7824 "return aaaaaaaaa ? 1111111111111111 :\n" 7825 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7826 " a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7827 " dddddddddddddddddd)\n", 7828 Style); 7829 verifyFormat( 7830 "return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7831 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7832 " (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7833 " cccccccccccccccccc);", 7834 Style); 7835 verifyFormat( 7836 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7837 " ccccccccccccccccc ? dddddddddddddddddd :\n" 7838 " eeeeeeeeeeeeeeeeee) :\n" 7839 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7840 " 3333333333333333;", 7841 Style); 7842 verifyFormat( 7843 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7844 " ccccccccccccc ? dddddddddddddddddd :\n" 7845 " eeeeeeeeeeeeeeeeee) :\n" 7846 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7847 " 3333333333333333;", 7848 Style); 7849 verifyFormat( 7850 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7851 " ccccccccccccccccc ? dddddddddddddddddd :\n" 7852 " eeeeeeeeeeeeeeeeee) :\n" 7853 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7854 " 3333333333333333;", 7855 Style); 7856 verifyFormat( 7857 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7858 " cccccccccccccccccc :\n" 7859 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7860 " 3333333333333333;", 7861 Style); 7862 verifyFormat( 7863 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7864 " cccccccccccccccccc ? dddddddddddddddddd :\n" 7865 " eeeeeeeeeeeeeeeeee :\n" 7866 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7867 " 3333333333333333;", 7868 Style); 7869 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n" 7870 " (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7871 " cccccccccccccccccc ? dddddddddddddddddd :\n" 7872 " eeeeeeeeeeeeeeeeee) :\n" 7873 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7874 " 3333333333333333;", 7875 Style); 7876 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n" 7877 " aaaaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7878 " cccccccccccccccccccc ? dddddddddddddddddd :\n" 7879 " eeeeeeeeeeeeeeeeee :\n" 7880 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7881 " 3333333333333333;", 7882 Style); 7883 } 7884 7885 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 7886 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 7887 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 7888 verifyFormat("bool a = true, b = false;"); 7889 7890 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 7891 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 7892 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 7893 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 7894 verifyFormat( 7895 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 7896 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 7897 " d = e && f;"); 7898 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 7899 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 7900 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 7901 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 7902 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 7903 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 7904 7905 FormatStyle Style = getGoogleStyle(); 7906 Style.PointerAlignment = FormatStyle::PAS_Left; 7907 Style.DerivePointerAlignment = false; 7908 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7909 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 7910 " *b = bbbbbbbbbbbbbbbbbbb;", 7911 Style); 7912 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 7913 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 7914 Style); 7915 verifyFormat("vector<int*> a, b;", Style); 7916 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style); 7917 } 7918 7919 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 7920 verifyFormat("arr[foo ? bar : baz];"); 7921 verifyFormat("f()[foo ? bar : baz];"); 7922 verifyFormat("(a + b)[foo ? bar : baz];"); 7923 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 7924 } 7925 7926 TEST_F(FormatTest, AlignsStringLiterals) { 7927 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 7928 " \"short literal\");"); 7929 verifyFormat( 7930 "looooooooooooooooooooooooongFunction(\n" 7931 " \"short literal\"\n" 7932 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 7933 verifyFormat("someFunction(\"Always break between multi-line\"\n" 7934 " \" string literals\",\n" 7935 " and, other, parameters);"); 7936 EXPECT_EQ("fun + \"1243\" /* comment */\n" 7937 " \"5678\";", 7938 format("fun + \"1243\" /* comment */\n" 7939 " \"5678\";", 7940 getLLVMStyleWithColumns(28))); 7941 EXPECT_EQ( 7942 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7943 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 7944 " \"aaaaaaaaaaaaaaaa\";", 7945 format("aaaaaa =" 7946 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 7947 "aaaaaaaaaaaaaaaaaaaaa\" " 7948 "\"aaaaaaaaaaaaaaaa\";")); 7949 verifyFormat("a = a + \"a\"\n" 7950 " \"a\"\n" 7951 " \"a\";"); 7952 verifyFormat("f(\"a\", \"b\"\n" 7953 " \"c\");"); 7954 7955 verifyFormat( 7956 "#define LL_FORMAT \"ll\"\n" 7957 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 7958 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 7959 7960 verifyFormat("#define A(X) \\\n" 7961 " \"aaaaa\" #X \"bbbbbb\" \\\n" 7962 " \"ccccc\"", 7963 getLLVMStyleWithColumns(23)); 7964 verifyFormat("#define A \"def\"\n" 7965 "f(\"abc\" A \"ghi\"\n" 7966 " \"jkl\");"); 7967 7968 verifyFormat("f(L\"a\"\n" 7969 " L\"b\");"); 7970 verifyFormat("#define A(X) \\\n" 7971 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 7972 " L\"ccccc\"", 7973 getLLVMStyleWithColumns(25)); 7974 7975 verifyFormat("f(@\"a\"\n" 7976 " @\"b\");"); 7977 verifyFormat("NSString s = @\"a\"\n" 7978 " @\"b\"\n" 7979 " @\"c\";"); 7980 verifyFormat("NSString s = @\"a\"\n" 7981 " \"b\"\n" 7982 " \"c\";"); 7983 } 7984 7985 TEST_F(FormatTest, ReturnTypeBreakingStyle) { 7986 FormatStyle Style = getLLVMStyle(); 7987 // No declarations or definitions should be moved to own line. 7988 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; 7989 verifyFormat("class A {\n" 7990 " int f() { return 1; }\n" 7991 " int g();\n" 7992 "};\n" 7993 "int f() { return 1; }\n" 7994 "int g();\n", 7995 Style); 7996 7997 // All declarations and definitions should have the return type moved to its 7998 // own line. 7999 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 8000 Style.TypenameMacros = {"LIST"}; 8001 verifyFormat("SomeType\n" 8002 "funcdecl(LIST(uint64_t));", 8003 Style); 8004 verifyFormat("class E {\n" 8005 " int\n" 8006 " f() {\n" 8007 " return 1;\n" 8008 " }\n" 8009 " int\n" 8010 " g();\n" 8011 "};\n" 8012 "int\n" 8013 "f() {\n" 8014 " return 1;\n" 8015 "}\n" 8016 "int\n" 8017 "g();\n", 8018 Style); 8019 8020 // Top-level definitions, and no kinds of declarations should have the 8021 // return type moved to its own line. 8022 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; 8023 verifyFormat("class B {\n" 8024 " int f() { return 1; }\n" 8025 " int g();\n" 8026 "};\n" 8027 "int\n" 8028 "f() {\n" 8029 " return 1;\n" 8030 "}\n" 8031 "int g();\n", 8032 Style); 8033 8034 // Top-level definitions and declarations should have the return type moved 8035 // to its own line. 8036 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel; 8037 verifyFormat("class C {\n" 8038 " int f() { return 1; }\n" 8039 " int g();\n" 8040 "};\n" 8041 "int\n" 8042 "f() {\n" 8043 " return 1;\n" 8044 "}\n" 8045 "int\n" 8046 "g();\n", 8047 Style); 8048 8049 // All definitions should have the return type moved to its own line, but no 8050 // kinds of declarations. 8051 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 8052 verifyFormat("class D {\n" 8053 " int\n" 8054 " f() {\n" 8055 " return 1;\n" 8056 " }\n" 8057 " int g();\n" 8058 "};\n" 8059 "int\n" 8060 "f() {\n" 8061 " return 1;\n" 8062 "}\n" 8063 "int g();\n", 8064 Style); 8065 verifyFormat("const char *\n" 8066 "f(void) {\n" // Break here. 8067 " return \"\";\n" 8068 "}\n" 8069 "const char *bar(void);\n", // No break here. 8070 Style); 8071 verifyFormat("template <class T>\n" 8072 "T *\n" 8073 "f(T &c) {\n" // Break here. 8074 " return NULL;\n" 8075 "}\n" 8076 "template <class T> T *f(T &c);\n", // No break here. 8077 Style); 8078 verifyFormat("class C {\n" 8079 " int\n" 8080 " operator+() {\n" 8081 " return 1;\n" 8082 " }\n" 8083 " int\n" 8084 " operator()() {\n" 8085 " return 1;\n" 8086 " }\n" 8087 "};\n", 8088 Style); 8089 verifyFormat("void\n" 8090 "A::operator()() {}\n" 8091 "void\n" 8092 "A::operator>>() {}\n" 8093 "void\n" 8094 "A::operator+() {}\n" 8095 "void\n" 8096 "A::operator*() {}\n" 8097 "void\n" 8098 "A::operator->() {}\n" 8099 "void\n" 8100 "A::operator void *() {}\n" 8101 "void\n" 8102 "A::operator void &() {}\n" 8103 "void\n" 8104 "A::operator void &&() {}\n" 8105 "void\n" 8106 "A::operator char *() {}\n" 8107 "void\n" 8108 "A::operator[]() {}\n" 8109 "void\n" 8110 "A::operator!() {}\n" 8111 "void\n" 8112 "A::operator**() {}\n" 8113 "void\n" 8114 "A::operator<Foo> *() {}\n" 8115 "void\n" 8116 "A::operator<Foo> **() {}\n" 8117 "void\n" 8118 "A::operator<Foo> &() {}\n" 8119 "void\n" 8120 "A::operator void **() {}\n", 8121 Style); 8122 verifyFormat("constexpr auto\n" 8123 "operator()() const -> reference {}\n" 8124 "constexpr auto\n" 8125 "operator>>() const -> reference {}\n" 8126 "constexpr auto\n" 8127 "operator+() const -> reference {}\n" 8128 "constexpr auto\n" 8129 "operator*() const -> reference {}\n" 8130 "constexpr auto\n" 8131 "operator->() const -> reference {}\n" 8132 "constexpr auto\n" 8133 "operator++() const -> reference {}\n" 8134 "constexpr auto\n" 8135 "operator void *() const -> reference {}\n" 8136 "constexpr auto\n" 8137 "operator void **() const -> reference {}\n" 8138 "constexpr auto\n" 8139 "operator void *() const -> reference {}\n" 8140 "constexpr auto\n" 8141 "operator void &() const -> reference {}\n" 8142 "constexpr auto\n" 8143 "operator void &&() const -> reference {}\n" 8144 "constexpr auto\n" 8145 "operator char *() const -> reference {}\n" 8146 "constexpr auto\n" 8147 "operator!() const -> reference {}\n" 8148 "constexpr auto\n" 8149 "operator[]() const -> reference {}\n", 8150 Style); 8151 verifyFormat("void *operator new(std::size_t s);", // No break here. 8152 Style); 8153 verifyFormat("void *\n" 8154 "operator new(std::size_t s) {}", 8155 Style); 8156 verifyFormat("void *\n" 8157 "operator delete[](void *ptr) {}", 8158 Style); 8159 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 8160 verifyFormat("const char *\n" 8161 "f(void)\n" // Break here. 8162 "{\n" 8163 " return \"\";\n" 8164 "}\n" 8165 "const char *bar(void);\n", // No break here. 8166 Style); 8167 verifyFormat("template <class T>\n" 8168 "T *\n" // Problem here: no line break 8169 "f(T &c)\n" // Break here. 8170 "{\n" 8171 " return NULL;\n" 8172 "}\n" 8173 "template <class T> T *f(T &c);\n", // No break here. 8174 Style); 8175 verifyFormat("int\n" 8176 "foo(A<bool> a)\n" 8177 "{\n" 8178 " return a;\n" 8179 "}\n", 8180 Style); 8181 verifyFormat("int\n" 8182 "foo(A<8> a)\n" 8183 "{\n" 8184 " return a;\n" 8185 "}\n", 8186 Style); 8187 verifyFormat("int\n" 8188 "foo(A<B<bool>, 8> a)\n" 8189 "{\n" 8190 " return a;\n" 8191 "}\n", 8192 Style); 8193 verifyFormat("int\n" 8194 "foo(A<B<8>, bool> a)\n" 8195 "{\n" 8196 " return a;\n" 8197 "}\n", 8198 Style); 8199 verifyFormat("int\n" 8200 "foo(A<B<bool>, bool> a)\n" 8201 "{\n" 8202 " return a;\n" 8203 "}\n", 8204 Style); 8205 verifyFormat("int\n" 8206 "foo(A<B<8>, 8> a)\n" 8207 "{\n" 8208 " return a;\n" 8209 "}\n", 8210 Style); 8211 8212 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 8213 Style.BraceWrapping.AfterFunction = true; 8214 verifyFormat("int f(i);\n" // No break here. 8215 "int\n" // Break here. 8216 "f(i)\n" 8217 "{\n" 8218 " return i + 1;\n" 8219 "}\n", 8220 Style); 8221 8222 Style = getGNUStyle(); 8223 8224 // Test for comments at the end of function declarations. 8225 verifyFormat("void\n" 8226 "foo (int a, /*abc*/ int b) // def\n" 8227 "{\n" 8228 "}\n", 8229 Style); 8230 8231 verifyFormat("void\n" 8232 "foo (int a, /* abc */ int b) /* def */\n" 8233 "{\n" 8234 "}\n", 8235 Style); 8236 8237 // Definitions that should not break after return type 8238 verifyFormat("void foo (int a, int b); // def\n", Style); 8239 verifyFormat("void foo (int a, int b); /* def */\n", Style); 8240 verifyFormat("void foo (int a, int b);\n", Style); 8241 } 8242 8243 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 8244 FormatStyle NoBreak = getLLVMStyle(); 8245 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 8246 FormatStyle Break = getLLVMStyle(); 8247 Break.AlwaysBreakBeforeMultilineStrings = true; 8248 verifyFormat("aaaa = \"bbbb\"\n" 8249 " \"cccc\";", 8250 NoBreak); 8251 verifyFormat("aaaa =\n" 8252 " \"bbbb\"\n" 8253 " \"cccc\";", 8254 Break); 8255 verifyFormat("aaaa(\"bbbb\"\n" 8256 " \"cccc\");", 8257 NoBreak); 8258 verifyFormat("aaaa(\n" 8259 " \"bbbb\"\n" 8260 " \"cccc\");", 8261 Break); 8262 verifyFormat("aaaa(qqq, \"bbbb\"\n" 8263 " \"cccc\");", 8264 NoBreak); 8265 verifyFormat("aaaa(qqq,\n" 8266 " \"bbbb\"\n" 8267 " \"cccc\");", 8268 Break); 8269 verifyFormat("aaaa(qqq,\n" 8270 " L\"bbbb\"\n" 8271 " L\"cccc\");", 8272 Break); 8273 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 8274 " \"bbbb\"));", 8275 Break); 8276 verifyFormat("string s = someFunction(\n" 8277 " \"abc\"\n" 8278 " \"abc\");", 8279 Break); 8280 8281 // As we break before unary operators, breaking right after them is bad. 8282 verifyFormat("string foo = abc ? \"x\"\n" 8283 " \"blah blah blah blah blah blah\"\n" 8284 " : \"y\";", 8285 Break); 8286 8287 // Don't break if there is no column gain. 8288 verifyFormat("f(\"aaaa\"\n" 8289 " \"bbbb\");", 8290 Break); 8291 8292 // Treat literals with escaped newlines like multi-line string literals. 8293 EXPECT_EQ("x = \"a\\\n" 8294 "b\\\n" 8295 "c\";", 8296 format("x = \"a\\\n" 8297 "b\\\n" 8298 "c\";", 8299 NoBreak)); 8300 EXPECT_EQ("xxxx =\n" 8301 " \"a\\\n" 8302 "b\\\n" 8303 "c\";", 8304 format("xxxx = \"a\\\n" 8305 "b\\\n" 8306 "c\";", 8307 Break)); 8308 8309 EXPECT_EQ("NSString *const kString =\n" 8310 " @\"aaaa\"\n" 8311 " @\"bbbb\";", 8312 format("NSString *const kString = @\"aaaa\"\n" 8313 "@\"bbbb\";", 8314 Break)); 8315 8316 Break.ColumnLimit = 0; 8317 verifyFormat("const char *hello = \"hello llvm\";", Break); 8318 } 8319 8320 TEST_F(FormatTest, AlignsPipes) { 8321 verifyFormat( 8322 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8323 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8324 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8325 verifyFormat( 8326 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 8327 " << aaaaaaaaaaaaaaaaaaaa;"); 8328 verifyFormat( 8329 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8330 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8331 verifyFormat( 8332 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 8333 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8334 verifyFormat( 8335 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 8336 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 8337 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 8338 verifyFormat( 8339 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8340 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8341 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8342 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8343 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8344 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8345 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 8346 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 8347 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 8348 verifyFormat( 8349 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8350 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8351 verifyFormat( 8352 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n" 8353 " aaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8354 8355 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 8356 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 8357 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8358 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8359 " aaaaaaaaaaaaaaaaaaaaa)\n" 8360 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8361 verifyFormat("LOG_IF(aaa == //\n" 8362 " bbb)\n" 8363 " << a << b;"); 8364 8365 // But sometimes, breaking before the first "<<" is desirable. 8366 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 8367 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 8368 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 8369 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8370 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8371 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 8372 " << BEF << IsTemplate << Description << E->getType();"); 8373 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 8374 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8375 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8376 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 8377 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8378 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8379 " << aaa;"); 8380 8381 verifyFormat( 8382 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8383 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8384 8385 // Incomplete string literal. 8386 EXPECT_EQ("llvm::errs() << \"\n" 8387 " << a;", 8388 format("llvm::errs() << \"\n<<a;")); 8389 8390 verifyFormat("void f() {\n" 8391 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 8392 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 8393 "}"); 8394 8395 // Handle 'endl'. 8396 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 8397 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 8398 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 8399 8400 // Handle '\n'. 8401 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 8402 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 8403 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 8404 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 8405 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 8406 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 8407 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 8408 } 8409 8410 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) { 8411 verifyFormat("return out << \"somepacket = {\\n\"\n" 8412 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 8413 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 8414 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 8415 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 8416 " << \"}\";"); 8417 8418 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 8419 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 8420 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 8421 verifyFormat( 8422 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 8423 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 8424 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 8425 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 8426 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 8427 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 8428 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 8429 verifyFormat( 8430 "void f() {\n" 8431 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 8432 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 8433 "}"); 8434 8435 // Breaking before the first "<<" is generally not desirable. 8436 verifyFormat( 8437 "llvm::errs()\n" 8438 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8439 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8440 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8441 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 8442 getLLVMStyleWithColumns(70)); 8443 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 8444 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8445 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 8446 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8447 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 8448 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 8449 getLLVMStyleWithColumns(70)); 8450 8451 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 8452 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 8453 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;"); 8454 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 8455 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 8456 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);"); 8457 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n" 8458 " (aaaa + aaaa);", 8459 getLLVMStyleWithColumns(40)); 8460 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n" 8461 " (aaaaaaa + aaaaa));", 8462 getLLVMStyleWithColumns(40)); 8463 verifyFormat( 8464 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n" 8465 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n" 8466 " bbbbbbbbbbbbbbbbbbbbbbb);"); 8467 } 8468 8469 TEST_F(FormatTest, UnderstandsEquals) { 8470 verifyFormat( 8471 "aaaaaaaaaaaaaaaaa =\n" 8472 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8473 verifyFormat( 8474 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8475 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 8476 verifyFormat( 8477 "if (a) {\n" 8478 " f();\n" 8479 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8480 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 8481 "}"); 8482 8483 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8484 " 100000000 + 10000000) {\n}"); 8485 } 8486 8487 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 8488 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 8489 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 8490 8491 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 8492 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 8493 8494 verifyFormat( 8495 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 8496 " Parameter2);"); 8497 8498 verifyFormat( 8499 "ShortObject->shortFunction(\n" 8500 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 8501 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 8502 8503 verifyFormat("loooooooooooooongFunction(\n" 8504 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 8505 8506 verifyFormat( 8507 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 8508 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 8509 8510 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 8511 " .WillRepeatedly(Return(SomeValue));"); 8512 verifyFormat("void f() {\n" 8513 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 8514 " .Times(2)\n" 8515 " .WillRepeatedly(Return(SomeValue));\n" 8516 "}"); 8517 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 8518 " ccccccccccccccccccccccc);"); 8519 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8520 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8521 " .aaaaa(aaaaa),\n" 8522 " aaaaaaaaaaaaaaaaaaaaa);"); 8523 verifyFormat("void f() {\n" 8524 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8525 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 8526 "}"); 8527 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8528 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8529 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8530 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8531 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 8532 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8533 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8534 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8535 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 8536 "}"); 8537 8538 // Here, it is not necessary to wrap at "." or "->". 8539 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 8540 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 8541 verifyFormat( 8542 "aaaaaaaaaaa->aaaaaaaaa(\n" 8543 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8544 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 8545 8546 verifyFormat( 8547 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8548 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 8549 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 8550 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 8551 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 8552 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 8553 8554 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8555 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8556 " .a();"); 8557 8558 FormatStyle NoBinPacking = getLLVMStyle(); 8559 NoBinPacking.BinPackParameters = false; 8560 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 8561 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 8562 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 8563 " aaaaaaaaaaaaaaaaaaa,\n" 8564 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8565 NoBinPacking); 8566 8567 // If there is a subsequent call, change to hanging indentation. 8568 verifyFormat( 8569 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8570 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 8571 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8572 verifyFormat( 8573 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8574 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 8575 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8576 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8577 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8578 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8579 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8580 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 8581 } 8582 8583 TEST_F(FormatTest, WrapsTemplateDeclarations) { 8584 verifyFormat("template <typename T>\n" 8585 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 8586 verifyFormat("template <typename T>\n" 8587 "// T should be one of {A, B}.\n" 8588 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 8589 verifyFormat( 8590 "template <typename T>\n" 8591 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 8592 verifyFormat("template <typename T>\n" 8593 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 8594 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 8595 verifyFormat( 8596 "template <typename T>\n" 8597 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 8598 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 8599 verifyFormat( 8600 "template <typename T>\n" 8601 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 8602 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 8603 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8604 verifyFormat("template <typename T>\n" 8605 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8606 " int aaaaaaaaaaaaaaaaaaaaaa);"); 8607 verifyFormat( 8608 "template <typename T1, typename T2 = char, typename T3 = char,\n" 8609 " typename T4 = char>\n" 8610 "void f();"); 8611 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 8612 " template <typename> class cccccccccccccccccccccc,\n" 8613 " typename ddddddddddddd>\n" 8614 "class C {};"); 8615 verifyFormat( 8616 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 8617 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8618 8619 verifyFormat("void f() {\n" 8620 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 8621 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 8622 "}"); 8623 8624 verifyFormat("template <typename T> class C {};"); 8625 verifyFormat("template <typename T> void f();"); 8626 verifyFormat("template <typename T> void f() {}"); 8627 verifyFormat( 8628 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 8629 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8630 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 8631 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 8632 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8633 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 8634 " bbbbbbbbbbbbbbbbbbbbbbbb);", 8635 getLLVMStyleWithColumns(72)); 8636 EXPECT_EQ("static_cast<A< //\n" 8637 " B> *>(\n" 8638 "\n" 8639 ");", 8640 format("static_cast<A<//\n" 8641 " B>*>(\n" 8642 "\n" 8643 " );")); 8644 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8645 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 8646 8647 FormatStyle AlwaysBreak = getLLVMStyle(); 8648 AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 8649 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 8650 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 8651 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 8652 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8653 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 8654 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 8655 verifyFormat("template <template <typename> class Fooooooo,\n" 8656 " template <typename> class Baaaaaaar>\n" 8657 "struct C {};", 8658 AlwaysBreak); 8659 verifyFormat("template <typename T> // T can be A, B or C.\n" 8660 "struct C {};", 8661 AlwaysBreak); 8662 verifyFormat("template <enum E> class A {\n" 8663 "public:\n" 8664 " E *f();\n" 8665 "};"); 8666 8667 FormatStyle NeverBreak = getLLVMStyle(); 8668 NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No; 8669 verifyFormat("template <typename T> class C {};", NeverBreak); 8670 verifyFormat("template <typename T> void f();", NeverBreak); 8671 verifyFormat("template <typename T> void f() {}", NeverBreak); 8672 verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa " 8673 "bbbbbbbbbbbbbbbbbbbb) {}", 8674 NeverBreak); 8675 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8676 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 8677 " ccccccccccccccccccccccccccccccccccccccccccccccc);", 8678 NeverBreak); 8679 verifyFormat("template <template <typename> class Fooooooo,\n" 8680 " template <typename> class Baaaaaaar>\n" 8681 "struct C {};", 8682 NeverBreak); 8683 verifyFormat("template <typename T> // T can be A, B or C.\n" 8684 "struct C {};", 8685 NeverBreak); 8686 verifyFormat("template <enum E> class A {\n" 8687 "public:\n" 8688 " E *f();\n" 8689 "};", 8690 NeverBreak); 8691 NeverBreak.PenaltyBreakTemplateDeclaration = 100; 8692 verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa " 8693 "bbbbbbbbbbbbbbbbbbbb) {}", 8694 NeverBreak); 8695 } 8696 8697 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) { 8698 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 8699 Style.ColumnLimit = 60; 8700 EXPECT_EQ("// Baseline - no comments.\n" 8701 "template <\n" 8702 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 8703 "void f() {}", 8704 format("// Baseline - no comments.\n" 8705 "template <\n" 8706 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 8707 "void f() {}", 8708 Style)); 8709 8710 EXPECT_EQ("template <\n" 8711 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 8712 "void f() {}", 8713 format("template <\n" 8714 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 8715 "void f() {}", 8716 Style)); 8717 8718 EXPECT_EQ( 8719 "template <\n" 8720 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 8721 "void f() {}", 8722 format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 8723 "void f() {}", 8724 Style)); 8725 8726 EXPECT_EQ( 8727 "template <\n" 8728 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 8729 " // multiline\n" 8730 "void f() {}", 8731 format("template <\n" 8732 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 8733 " // multiline\n" 8734 "void f() {}", 8735 Style)); 8736 8737 EXPECT_EQ( 8738 "template <typename aaaaaaaaaa<\n" 8739 " bbbbbbbbbbbb>::value> // trailing loooong\n" 8740 "void f() {}", 8741 format( 8742 "template <\n" 8743 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n" 8744 "void f() {}", 8745 Style)); 8746 } 8747 8748 TEST_F(FormatTest, WrapsTemplateParameters) { 8749 FormatStyle Style = getLLVMStyle(); 8750 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 8751 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 8752 verifyFormat( 8753 "template <typename... a> struct q {};\n" 8754 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 8755 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 8756 " y;", 8757 Style); 8758 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 8759 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 8760 verifyFormat( 8761 "template <typename... a> struct r {};\n" 8762 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 8763 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 8764 " y;", 8765 Style); 8766 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 8767 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 8768 verifyFormat("template <typename... a> struct s {};\n" 8769 "extern s<\n" 8770 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 8771 "aaaaaaaaaaaaaaaaaaaaaa,\n" 8772 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 8773 "aaaaaaaaaaaaaaaaaaaaaa>\n" 8774 " y;", 8775 Style); 8776 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 8777 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 8778 verifyFormat("template <typename... a> struct t {};\n" 8779 "extern t<\n" 8780 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 8781 "aaaaaaaaaaaaaaaaaaaaaa,\n" 8782 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 8783 "aaaaaaaaaaaaaaaaaaaaaa>\n" 8784 " y;", 8785 Style); 8786 } 8787 8788 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 8789 verifyFormat( 8790 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8791 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8792 verifyFormat( 8793 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8794 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8795 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 8796 8797 // FIXME: Should we have the extra indent after the second break? 8798 verifyFormat( 8799 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8800 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8801 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8802 8803 verifyFormat( 8804 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 8805 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 8806 8807 // Breaking at nested name specifiers is generally not desirable. 8808 verifyFormat( 8809 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8810 " aaaaaaaaaaaaaaaaaaaaaaa);"); 8811 8812 verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n" 8813 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8814 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8815 " aaaaaaaaaaaaaaaaaaaaa);", 8816 getLLVMStyleWithColumns(74)); 8817 8818 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8819 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8820 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8821 } 8822 8823 TEST_F(FormatTest, UnderstandsTemplateParameters) { 8824 verifyFormat("A<int> a;"); 8825 verifyFormat("A<A<A<int>>> a;"); 8826 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 8827 verifyFormat("bool x = a < 1 || 2 > a;"); 8828 verifyFormat("bool x = 5 < f<int>();"); 8829 verifyFormat("bool x = f<int>() > 5;"); 8830 verifyFormat("bool x = 5 < a<int>::x;"); 8831 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 8832 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 8833 8834 verifyGoogleFormat("A<A<int>> a;"); 8835 verifyGoogleFormat("A<A<A<int>>> a;"); 8836 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 8837 verifyGoogleFormat("A<A<int> > a;"); 8838 verifyGoogleFormat("A<A<A<int> > > a;"); 8839 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 8840 verifyGoogleFormat("A<::A<int>> a;"); 8841 verifyGoogleFormat("A<::A> a;"); 8842 verifyGoogleFormat("A< ::A> a;"); 8843 verifyGoogleFormat("A< ::A<int> > a;"); 8844 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 8845 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 8846 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 8847 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 8848 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 8849 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 8850 8851 verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 8852 8853 // template closer followed by a token that starts with > or = 8854 verifyFormat("bool b = a<1> > 1;"); 8855 verifyFormat("bool b = a<1> >= 1;"); 8856 verifyFormat("int i = a<1> >> 1;"); 8857 FormatStyle Style = getLLVMStyle(); 8858 Style.SpaceBeforeAssignmentOperators = false; 8859 verifyFormat("bool b= a<1> == 1;", Style); 8860 verifyFormat("a<int> = 1;", Style); 8861 verifyFormat("a<int> >>= 1;", Style); 8862 8863 verifyFormat("test < a | b >> c;"); 8864 verifyFormat("test<test<a | b>> c;"); 8865 verifyFormat("test >> a >> b;"); 8866 verifyFormat("test << a >> b;"); 8867 8868 verifyFormat("f<int>();"); 8869 verifyFormat("template <typename T> void f() {}"); 8870 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 8871 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 8872 "sizeof(char)>::type>;"); 8873 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 8874 verifyFormat("f(a.operator()<A>());"); 8875 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8876 " .template operator()<A>());", 8877 getLLVMStyleWithColumns(35)); 8878 8879 // Not template parameters. 8880 verifyFormat("return a < b && c > d;"); 8881 verifyFormat("void f() {\n" 8882 " while (a < b && c > d) {\n" 8883 " }\n" 8884 "}"); 8885 verifyFormat("template <typename... Types>\n" 8886 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 8887 8888 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8889 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 8890 getLLVMStyleWithColumns(60)); 8891 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 8892 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 8893 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 8894 verifyFormat("some_templated_type<decltype([](int i) { return i; })>"); 8895 } 8896 8897 TEST_F(FormatTest, UnderstandsShiftOperators) { 8898 verifyFormat("if (i < x >> 1)"); 8899 verifyFormat("while (i < x >> 1)"); 8900 verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)"); 8901 verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)"); 8902 verifyFormat( 8903 "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)"); 8904 verifyFormat("Foo.call<Bar<Function>>()"); 8905 verifyFormat("if (Foo.call<Bar<Function>>() == 0)"); 8906 verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; " 8907 "++i, v = v >> 1)"); 8908 verifyFormat("if (w<u<v<x>>, 1>::t)"); 8909 } 8910 8911 TEST_F(FormatTest, BitshiftOperatorWidth) { 8912 EXPECT_EQ("int a = 1 << 2; /* foo\n" 8913 " bar */", 8914 format("int a=1<<2; /* foo\n" 8915 " bar */")); 8916 8917 EXPECT_EQ("int b = 256 >> 1; /* foo\n" 8918 " bar */", 8919 format("int b =256>>1 ; /* foo\n" 8920 " bar */")); 8921 } 8922 8923 TEST_F(FormatTest, UnderstandsBinaryOperators) { 8924 verifyFormat("COMPARE(a, ==, b);"); 8925 verifyFormat("auto s = sizeof...(Ts) - 1;"); 8926 } 8927 8928 TEST_F(FormatTest, UnderstandsPointersToMembers) { 8929 verifyFormat("int A::*x;"); 8930 verifyFormat("int (S::*func)(void *);"); 8931 verifyFormat("void f() { int (S::*func)(void *); }"); 8932 verifyFormat("typedef bool *(Class::*Member)() const;"); 8933 verifyFormat("void f() {\n" 8934 " (a->*f)();\n" 8935 " a->*x;\n" 8936 " (a.*f)();\n" 8937 " ((*a).*f)();\n" 8938 " a.*x;\n" 8939 "}"); 8940 verifyFormat("void f() {\n" 8941 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 8942 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 8943 "}"); 8944 verifyFormat( 8945 "(aaaaaaaaaa->*bbbbbbb)(\n" 8946 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 8947 FormatStyle Style = getLLVMStyle(); 8948 Style.PointerAlignment = FormatStyle::PAS_Left; 8949 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 8950 } 8951 8952 TEST_F(FormatTest, UnderstandsUnaryOperators) { 8953 verifyFormat("int a = -2;"); 8954 verifyFormat("f(-1, -2, -3);"); 8955 verifyFormat("a[-1] = 5;"); 8956 verifyFormat("int a = 5 + -2;"); 8957 verifyFormat("if (i == -1) {\n}"); 8958 verifyFormat("if (i != -1) {\n}"); 8959 verifyFormat("if (i > -1) {\n}"); 8960 verifyFormat("if (i < -1) {\n}"); 8961 verifyFormat("++(a->f());"); 8962 verifyFormat("--(a->f());"); 8963 verifyFormat("(a->f())++;"); 8964 verifyFormat("a[42]++;"); 8965 verifyFormat("if (!(a->f())) {\n}"); 8966 verifyFormat("if (!+i) {\n}"); 8967 verifyFormat("~&a;"); 8968 8969 verifyFormat("a-- > b;"); 8970 verifyFormat("b ? -a : c;"); 8971 verifyFormat("n * sizeof char16;"); 8972 verifyFormat("n * alignof char16;", getGoogleStyle()); 8973 verifyFormat("sizeof(char);"); 8974 verifyFormat("alignof(char);", getGoogleStyle()); 8975 8976 verifyFormat("return -1;"); 8977 verifyFormat("throw -1;"); 8978 verifyFormat("switch (a) {\n" 8979 "case -1:\n" 8980 " break;\n" 8981 "}"); 8982 verifyFormat("#define X -1"); 8983 verifyFormat("#define X -kConstant"); 8984 8985 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 8986 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 8987 8988 verifyFormat("int a = /* confusing comment */ -1;"); 8989 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 8990 verifyFormat("int a = i /* confusing comment */++;"); 8991 8992 verifyFormat("co_yield -1;"); 8993 verifyFormat("co_return -1;"); 8994 8995 // Check that * is not treated as a binary operator when we set 8996 // PointerAlignment as PAS_Left after a keyword and not a declaration. 8997 FormatStyle PASLeftStyle = getLLVMStyle(); 8998 PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left; 8999 verifyFormat("co_return *a;", PASLeftStyle); 9000 verifyFormat("co_await *a;", PASLeftStyle); 9001 verifyFormat("co_yield *a", PASLeftStyle); 9002 verifyFormat("return *a;", PASLeftStyle); 9003 } 9004 9005 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 9006 verifyFormat("if (!aaaaaaaaaa( // break\n" 9007 " aaaaa)) {\n" 9008 "}"); 9009 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 9010 " aaaaa));"); 9011 verifyFormat("*aaa = aaaaaaa( // break\n" 9012 " bbbbbb);"); 9013 } 9014 9015 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 9016 verifyFormat("bool operator<();"); 9017 verifyFormat("bool operator>();"); 9018 verifyFormat("bool operator=();"); 9019 verifyFormat("bool operator==();"); 9020 verifyFormat("bool operator!=();"); 9021 verifyFormat("int operator+();"); 9022 verifyFormat("int operator++();"); 9023 verifyFormat("int operator++(int) volatile noexcept;"); 9024 verifyFormat("bool operator,();"); 9025 verifyFormat("bool operator();"); 9026 verifyFormat("bool operator()();"); 9027 verifyFormat("bool operator[]();"); 9028 verifyFormat("operator bool();"); 9029 verifyFormat("operator int();"); 9030 verifyFormat("operator void *();"); 9031 verifyFormat("operator SomeType<int>();"); 9032 verifyFormat("operator SomeType<int, int>();"); 9033 verifyFormat("operator SomeType<SomeType<int>>();"); 9034 verifyFormat("void *operator new(std::size_t size);"); 9035 verifyFormat("void *operator new[](std::size_t size);"); 9036 verifyFormat("void operator delete(void *ptr);"); 9037 verifyFormat("void operator delete[](void *ptr);"); 9038 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 9039 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 9040 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 9041 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 9042 9043 verifyFormat( 9044 "ostream &operator<<(ostream &OutputStream,\n" 9045 " SomeReallyLongType WithSomeReallyLongValue);"); 9046 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 9047 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 9048 " return left.group < right.group;\n" 9049 "}"); 9050 verifyFormat("SomeType &operator=(const SomeType &S);"); 9051 verifyFormat("f.template operator()<int>();"); 9052 9053 verifyGoogleFormat("operator void*();"); 9054 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 9055 verifyGoogleFormat("operator ::A();"); 9056 9057 verifyFormat("using A::operator+;"); 9058 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 9059 "int i;"); 9060 9061 // Calling an operator as a member function. 9062 verifyFormat("void f() { a.operator*(); }"); 9063 verifyFormat("void f() { a.operator*(b & b); }"); 9064 verifyFormat("void f() { a->operator&(a * b); }"); 9065 verifyFormat("void f() { NS::a.operator+(*b * *b); }"); 9066 // TODO: Calling an operator as a non-member function is hard to distinguish. 9067 // https://llvm.org/PR50629 9068 // verifyFormat("void f() { operator*(a & a); }"); 9069 // verifyFormat("void f() { operator&(a, b * b); }"); 9070 } 9071 9072 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 9073 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 9074 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 9075 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 9076 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 9077 verifyFormat("Deleted &operator=(const Deleted &) &;"); 9078 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 9079 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 9080 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 9081 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 9082 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 9083 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 9084 verifyFormat("void Fn(T const &) const &;"); 9085 verifyFormat("void Fn(T const volatile &&) const volatile &&;"); 9086 verifyFormat("template <typename T>\n" 9087 "void F(T) && = delete;", 9088 getGoogleStyle()); 9089 9090 FormatStyle AlignLeft = getLLVMStyle(); 9091 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 9092 verifyFormat("void A::b() && {}", AlignLeft); 9093 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 9094 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 9095 AlignLeft); 9096 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 9097 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 9098 verifyFormat("auto Function(T t) & -> void {}", AlignLeft); 9099 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft); 9100 verifyFormat("auto Function(T) & -> void {}", AlignLeft); 9101 verifyFormat("auto Function(T) & -> void;", AlignLeft); 9102 verifyFormat("void Fn(T const&) const&;", AlignLeft); 9103 verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft); 9104 9105 FormatStyle Spaces = getLLVMStyle(); 9106 Spaces.SpacesInCStyleCastParentheses = true; 9107 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 9108 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 9109 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 9110 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 9111 9112 Spaces.SpacesInCStyleCastParentheses = false; 9113 Spaces.SpacesInParentheses = true; 9114 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 9115 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", 9116 Spaces); 9117 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 9118 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 9119 9120 FormatStyle BreakTemplate = getLLVMStyle(); 9121 BreakTemplate.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 9122 9123 verifyFormat("struct f {\n" 9124 " template <class T>\n" 9125 " int &foo(const std::string &str) &noexcept {}\n" 9126 "};", 9127 BreakTemplate); 9128 9129 verifyFormat("struct f {\n" 9130 " template <class T>\n" 9131 " int &foo(const std::string &str) &&noexcept {}\n" 9132 "};", 9133 BreakTemplate); 9134 9135 verifyFormat("struct f {\n" 9136 " template <class T>\n" 9137 " int &foo(const std::string &str) const &noexcept {}\n" 9138 "};", 9139 BreakTemplate); 9140 9141 verifyFormat("struct f {\n" 9142 " template <class T>\n" 9143 " int &foo(const std::string &str) const &noexcept {}\n" 9144 "};", 9145 BreakTemplate); 9146 9147 verifyFormat("struct f {\n" 9148 " template <class T>\n" 9149 " auto foo(const std::string &str) &&noexcept -> int & {}\n" 9150 "};", 9151 BreakTemplate); 9152 9153 FormatStyle AlignLeftBreakTemplate = getLLVMStyle(); 9154 AlignLeftBreakTemplate.AlwaysBreakTemplateDeclarations = 9155 FormatStyle::BTDS_Yes; 9156 AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left; 9157 9158 verifyFormat("struct f {\n" 9159 " template <class T>\n" 9160 " int& foo(const std::string& str) & noexcept {}\n" 9161 "};", 9162 AlignLeftBreakTemplate); 9163 9164 verifyFormat("struct f {\n" 9165 " template <class T>\n" 9166 " int& foo(const std::string& str) && noexcept {}\n" 9167 "};", 9168 AlignLeftBreakTemplate); 9169 9170 verifyFormat("struct f {\n" 9171 " template <class T>\n" 9172 " int& foo(const std::string& str) const& noexcept {}\n" 9173 "};", 9174 AlignLeftBreakTemplate); 9175 9176 verifyFormat("struct f {\n" 9177 " template <class T>\n" 9178 " int& foo(const std::string& str) const&& noexcept {}\n" 9179 "};", 9180 AlignLeftBreakTemplate); 9181 9182 verifyFormat("struct f {\n" 9183 " template <class T>\n" 9184 " auto foo(const std::string& str) && noexcept -> int& {}\n" 9185 "};", 9186 AlignLeftBreakTemplate); 9187 9188 // The `&` in `Type&` should not be confused with a trailing `&` of 9189 // DEPRECATED(reason) member function. 9190 verifyFormat("struct f {\n" 9191 " template <class T>\n" 9192 " DEPRECATED(reason)\n" 9193 " Type &foo(arguments) {}\n" 9194 "};", 9195 BreakTemplate); 9196 9197 verifyFormat("struct f {\n" 9198 " template <class T>\n" 9199 " DEPRECATED(reason)\n" 9200 " Type& foo(arguments) {}\n" 9201 "};", 9202 AlignLeftBreakTemplate); 9203 9204 verifyFormat("void (*foopt)(int) = &func;"); 9205 } 9206 9207 TEST_F(FormatTest, UnderstandsNewAndDelete) { 9208 verifyFormat("void f() {\n" 9209 " A *a = new A;\n" 9210 " A *a = new (placement) A;\n" 9211 " delete a;\n" 9212 " delete (A *)a;\n" 9213 "}"); 9214 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 9215 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 9216 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 9217 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 9218 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 9219 verifyFormat("delete[] h->p;"); 9220 } 9221 9222 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 9223 verifyFormat("int *f(int *a) {}"); 9224 verifyFormat("int main(int argc, char **argv) {}"); 9225 verifyFormat("Test::Test(int b) : a(b * b) {}"); 9226 verifyIndependentOfContext("f(a, *a);"); 9227 verifyFormat("void g() { f(*a); }"); 9228 verifyIndependentOfContext("int a = b * 10;"); 9229 verifyIndependentOfContext("int a = 10 * b;"); 9230 verifyIndependentOfContext("int a = b * c;"); 9231 verifyIndependentOfContext("int a += b * c;"); 9232 verifyIndependentOfContext("int a -= b * c;"); 9233 verifyIndependentOfContext("int a *= b * c;"); 9234 verifyIndependentOfContext("int a /= b * c;"); 9235 verifyIndependentOfContext("int a = *b;"); 9236 verifyIndependentOfContext("int a = *b * c;"); 9237 verifyIndependentOfContext("int a = b * *c;"); 9238 verifyIndependentOfContext("int a = b * (10);"); 9239 verifyIndependentOfContext("S << b * (10);"); 9240 verifyIndependentOfContext("return 10 * b;"); 9241 verifyIndependentOfContext("return *b * *c;"); 9242 verifyIndependentOfContext("return a & ~b;"); 9243 verifyIndependentOfContext("f(b ? *c : *d);"); 9244 verifyIndependentOfContext("int a = b ? *c : *d;"); 9245 verifyIndependentOfContext("*b = a;"); 9246 verifyIndependentOfContext("a * ~b;"); 9247 verifyIndependentOfContext("a * !b;"); 9248 verifyIndependentOfContext("a * +b;"); 9249 verifyIndependentOfContext("a * -b;"); 9250 verifyIndependentOfContext("a * ++b;"); 9251 verifyIndependentOfContext("a * --b;"); 9252 verifyIndependentOfContext("a[4] * b;"); 9253 verifyIndependentOfContext("a[a * a] = 1;"); 9254 verifyIndependentOfContext("f() * b;"); 9255 verifyIndependentOfContext("a * [self dostuff];"); 9256 verifyIndependentOfContext("int x = a * (a + b);"); 9257 verifyIndependentOfContext("(a *)(a + b);"); 9258 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 9259 verifyIndependentOfContext("int *pa = (int *)&a;"); 9260 verifyIndependentOfContext("return sizeof(int **);"); 9261 verifyIndependentOfContext("return sizeof(int ******);"); 9262 verifyIndependentOfContext("return (int **&)a;"); 9263 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 9264 verifyFormat("void f(Type (*parameter)[10]) {}"); 9265 verifyFormat("void f(Type (¶meter)[10]) {}"); 9266 verifyGoogleFormat("return sizeof(int**);"); 9267 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 9268 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 9269 verifyFormat("auto a = [](int **&, int ***) {};"); 9270 verifyFormat("auto PointerBinding = [](const char *S) {};"); 9271 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 9272 verifyFormat("[](const decltype(*a) &value) {}"); 9273 verifyFormat("[](const typeof(*a) &value) {}"); 9274 verifyFormat("[](const _Atomic(a *) &value) {}"); 9275 verifyFormat("[](const __underlying_type(a) &value) {}"); 9276 verifyFormat("decltype(a * b) F();"); 9277 verifyFormat("typeof(a * b) F();"); 9278 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 9279 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 9280 verifyIndependentOfContext("typedef void (*f)(int *a);"); 9281 verifyIndependentOfContext("int i{a * b};"); 9282 verifyIndependentOfContext("aaa && aaa->f();"); 9283 verifyIndependentOfContext("int x = ~*p;"); 9284 verifyFormat("Constructor() : a(a), area(width * height) {}"); 9285 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 9286 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 9287 verifyFormat("void f() { f(a, c * d); }"); 9288 verifyFormat("void f() { f(new a(), c * d); }"); 9289 verifyFormat("void f(const MyOverride &override);"); 9290 verifyFormat("void f(const MyFinal &final);"); 9291 verifyIndependentOfContext("bool a = f() && override.f();"); 9292 verifyIndependentOfContext("bool a = f() && final.f();"); 9293 9294 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 9295 9296 verifyIndependentOfContext("A<int *> a;"); 9297 verifyIndependentOfContext("A<int **> a;"); 9298 verifyIndependentOfContext("A<int *, int *> a;"); 9299 verifyIndependentOfContext("A<int *[]> a;"); 9300 verifyIndependentOfContext( 9301 "const char *const p = reinterpret_cast<const char *const>(q);"); 9302 verifyIndependentOfContext("A<int **, int **> a;"); 9303 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 9304 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 9305 verifyFormat("for (; a && b;) {\n}"); 9306 verifyFormat("bool foo = true && [] { return false; }();"); 9307 9308 verifyFormat( 9309 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9310 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9311 9312 verifyGoogleFormat("int const* a = &b;"); 9313 verifyGoogleFormat("**outparam = 1;"); 9314 verifyGoogleFormat("*outparam = a * b;"); 9315 verifyGoogleFormat("int main(int argc, char** argv) {}"); 9316 verifyGoogleFormat("A<int*> a;"); 9317 verifyGoogleFormat("A<int**> a;"); 9318 verifyGoogleFormat("A<int*, int*> a;"); 9319 verifyGoogleFormat("A<int**, int**> a;"); 9320 verifyGoogleFormat("f(b ? *c : *d);"); 9321 verifyGoogleFormat("int a = b ? *c : *d;"); 9322 verifyGoogleFormat("Type* t = **x;"); 9323 verifyGoogleFormat("Type* t = *++*x;"); 9324 verifyGoogleFormat("*++*x;"); 9325 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 9326 verifyGoogleFormat("Type* t = x++ * y;"); 9327 verifyGoogleFormat( 9328 "const char* const p = reinterpret_cast<const char* const>(q);"); 9329 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 9330 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 9331 verifyGoogleFormat("template <typename T>\n" 9332 "void f(int i = 0, SomeType** temps = NULL);"); 9333 9334 FormatStyle Left = getLLVMStyle(); 9335 Left.PointerAlignment = FormatStyle::PAS_Left; 9336 verifyFormat("x = *a(x) = *a(y);", Left); 9337 verifyFormat("for (;; *a = b) {\n}", Left); 9338 verifyFormat("return *this += 1;", Left); 9339 verifyFormat("throw *x;", Left); 9340 verifyFormat("delete *x;", Left); 9341 verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left); 9342 verifyFormat("[](const decltype(*a)* ptr) {}", Left); 9343 verifyFormat("[](const typeof(*a)* ptr) {}", Left); 9344 verifyFormat("[](const _Atomic(a*)* ptr) {}", Left); 9345 verifyFormat("[](const __underlying_type(a)* ptr) {}", Left); 9346 verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left); 9347 verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left); 9348 verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left); 9349 verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left); 9350 9351 verifyIndependentOfContext("a = *(x + y);"); 9352 verifyIndependentOfContext("a = &(x + y);"); 9353 verifyIndependentOfContext("*(x + y).call();"); 9354 verifyIndependentOfContext("&(x + y)->call();"); 9355 verifyFormat("void f() { &(*I).first; }"); 9356 9357 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 9358 verifyFormat( 9359 "int *MyValues = {\n" 9360 " *A, // Operator detection might be confused by the '{'\n" 9361 " *BB // Operator detection might be confused by previous comment\n" 9362 "};"); 9363 9364 verifyIndependentOfContext("if (int *a = &b)"); 9365 verifyIndependentOfContext("if (int &a = *b)"); 9366 verifyIndependentOfContext("if (a & b[i])"); 9367 verifyIndependentOfContext("if constexpr (a & b[i])"); 9368 verifyIndependentOfContext("if CONSTEXPR (a & b[i])"); 9369 verifyIndependentOfContext("if (a * (b * c))"); 9370 verifyIndependentOfContext("if constexpr (a * (b * c))"); 9371 verifyIndependentOfContext("if CONSTEXPR (a * (b * c))"); 9372 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 9373 verifyIndependentOfContext("if (*b[i])"); 9374 verifyIndependentOfContext("if (int *a = (&b))"); 9375 verifyIndependentOfContext("while (int *a = &b)"); 9376 verifyIndependentOfContext("while (a * (b * c))"); 9377 verifyIndependentOfContext("size = sizeof *a;"); 9378 verifyIndependentOfContext("if (a && (b = c))"); 9379 verifyFormat("void f() {\n" 9380 " for (const int &v : Values) {\n" 9381 " }\n" 9382 "}"); 9383 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 9384 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 9385 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 9386 9387 verifyFormat("#define A (!a * b)"); 9388 verifyFormat("#define MACRO \\\n" 9389 " int *i = a * b; \\\n" 9390 " void f(a *b);", 9391 getLLVMStyleWithColumns(19)); 9392 9393 verifyIndependentOfContext("A = new SomeType *[Length];"); 9394 verifyIndependentOfContext("A = new SomeType *[Length]();"); 9395 verifyIndependentOfContext("T **t = new T *;"); 9396 verifyIndependentOfContext("T **t = new T *();"); 9397 verifyGoogleFormat("A = new SomeType*[Length]();"); 9398 verifyGoogleFormat("A = new SomeType*[Length];"); 9399 verifyGoogleFormat("T** t = new T*;"); 9400 verifyGoogleFormat("T** t = new T*();"); 9401 9402 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 9403 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 9404 verifyFormat("template <bool a, bool b> " 9405 "typename t::if<x && y>::type f() {}"); 9406 verifyFormat("template <int *y> f() {}"); 9407 verifyFormat("vector<int *> v;"); 9408 verifyFormat("vector<int *const> v;"); 9409 verifyFormat("vector<int *const **const *> v;"); 9410 verifyFormat("vector<int *volatile> v;"); 9411 verifyFormat("vector<a *_Nonnull> v;"); 9412 verifyFormat("vector<a *_Nullable> v;"); 9413 verifyFormat("vector<a *_Null_unspecified> v;"); 9414 verifyFormat("vector<a *__ptr32> v;"); 9415 verifyFormat("vector<a *__ptr64> v;"); 9416 verifyFormat("vector<a *__capability> v;"); 9417 FormatStyle TypeMacros = getLLVMStyle(); 9418 TypeMacros.TypenameMacros = {"LIST"}; 9419 verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros); 9420 verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros); 9421 verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros); 9422 verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros); 9423 verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros); // multiplication 9424 9425 FormatStyle CustomQualifier = getLLVMStyle(); 9426 // Add indentifers that should not be parsed as a qualifier by default. 9427 CustomQualifier.AttributeMacros.push_back("__my_qualifier"); 9428 CustomQualifier.AttributeMacros.push_back("_My_qualifier"); 9429 CustomQualifier.AttributeMacros.push_back("my_other_qualifier"); 9430 verifyFormat("vector<a * __my_qualifier> parse_as_multiply;"); 9431 verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier); 9432 verifyFormat("vector<a * _My_qualifier> parse_as_multiply;"); 9433 verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier); 9434 verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;"); 9435 verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier); 9436 verifyFormat("vector<a * _NotAQualifier> v;"); 9437 verifyFormat("vector<a * __not_a_qualifier> v;"); 9438 verifyFormat("vector<a * b> v;"); 9439 verifyFormat("foo<b && false>();"); 9440 verifyFormat("foo<b & 1>();"); 9441 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 9442 verifyFormat("typeof(*::std::declval<const T &>()) void F();"); 9443 verifyFormat("_Atomic(*::std::declval<const T &>()) void F();"); 9444 verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();"); 9445 verifyFormat( 9446 "template <class T, class = typename std::enable_if<\n" 9447 " std::is_integral<T>::value &&\n" 9448 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 9449 "void F();", 9450 getLLVMStyleWithColumns(70)); 9451 verifyFormat("template <class T,\n" 9452 " class = typename std::enable_if<\n" 9453 " std::is_integral<T>::value &&\n" 9454 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n" 9455 " class U>\n" 9456 "void F();", 9457 getLLVMStyleWithColumns(70)); 9458 verifyFormat( 9459 "template <class T,\n" 9460 " class = typename ::std::enable_if<\n" 9461 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 9462 "void F();", 9463 getGoogleStyleWithColumns(68)); 9464 9465 verifyIndependentOfContext("MACRO(int *i);"); 9466 verifyIndependentOfContext("MACRO(auto *a);"); 9467 verifyIndependentOfContext("MACRO(const A *a);"); 9468 verifyIndependentOfContext("MACRO(_Atomic(A) *a);"); 9469 verifyIndependentOfContext("MACRO(decltype(A) *a);"); 9470 verifyIndependentOfContext("MACRO(typeof(A) *a);"); 9471 verifyIndependentOfContext("MACRO(__underlying_type(A) *a);"); 9472 verifyIndependentOfContext("MACRO(A *const a);"); 9473 verifyIndependentOfContext("MACRO(A *restrict a);"); 9474 verifyIndependentOfContext("MACRO(A *__restrict__ a);"); 9475 verifyIndependentOfContext("MACRO(A *__restrict a);"); 9476 verifyIndependentOfContext("MACRO(A *volatile a);"); 9477 verifyIndependentOfContext("MACRO(A *__volatile a);"); 9478 verifyIndependentOfContext("MACRO(A *__volatile__ a);"); 9479 verifyIndependentOfContext("MACRO(A *_Nonnull a);"); 9480 verifyIndependentOfContext("MACRO(A *_Nullable a);"); 9481 verifyIndependentOfContext("MACRO(A *_Null_unspecified a);"); 9482 verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);"); 9483 verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);"); 9484 verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);"); 9485 verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);"); 9486 verifyIndependentOfContext("MACRO(A *__ptr32 a);"); 9487 verifyIndependentOfContext("MACRO(A *__ptr64 a);"); 9488 verifyIndependentOfContext("MACRO(A *__capability);"); 9489 verifyIndependentOfContext("MACRO(A &__capability);"); 9490 verifyFormat("MACRO(A *__my_qualifier);"); // type declaration 9491 verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication 9492 // If we add __my_qualifier to AttributeMacros it should always be parsed as 9493 // a type declaration: 9494 verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier); 9495 verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier); 9496 // Also check that TypenameMacros prevents parsing it as multiplication: 9497 verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication 9498 verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros); // type 9499 9500 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 9501 verifyFormat("void f() { f(float{1}, a * a); }"); 9502 verifyFormat("void f() { f(float(1), a * a); }"); 9503 9504 verifyFormat("f((void (*)(int))g);"); 9505 verifyFormat("f((void (&)(int))g);"); 9506 verifyFormat("f((void (^)(int))g);"); 9507 9508 // FIXME: Is there a way to make this work? 9509 // verifyIndependentOfContext("MACRO(A *a);"); 9510 verifyFormat("MACRO(A &B);"); 9511 verifyFormat("MACRO(A *B);"); 9512 verifyFormat("void f() { MACRO(A * B); }"); 9513 verifyFormat("void f() { MACRO(A & B); }"); 9514 9515 // This lambda was mis-formatted after D88956 (treating it as a binop): 9516 verifyFormat("auto x = [](const decltype(x) &ptr) {};"); 9517 verifyFormat("auto x = [](const decltype(x) *ptr) {};"); 9518 verifyFormat("#define lambda [](const decltype(x) &ptr) {}"); 9519 verifyFormat("#define lambda [](const decltype(x) *ptr) {}"); 9520 9521 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 9522 verifyFormat("return options != nullptr && operator==(*options);"); 9523 9524 EXPECT_EQ("#define OP(x) \\\n" 9525 " ostream &operator<<(ostream &s, const A &a) { \\\n" 9526 " return s << a.DebugString(); \\\n" 9527 " }", 9528 format("#define OP(x) \\\n" 9529 " ostream &operator<<(ostream &s, const A &a) { \\\n" 9530 " return s << a.DebugString(); \\\n" 9531 " }", 9532 getLLVMStyleWithColumns(50))); 9533 9534 // FIXME: We cannot handle this case yet; we might be able to figure out that 9535 // foo<x> d > v; doesn't make sense. 9536 verifyFormat("foo<a<b && c> d> v;"); 9537 9538 FormatStyle PointerMiddle = getLLVMStyle(); 9539 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 9540 verifyFormat("delete *x;", PointerMiddle); 9541 verifyFormat("int * x;", PointerMiddle); 9542 verifyFormat("int *[] x;", PointerMiddle); 9543 verifyFormat("template <int * y> f() {}", PointerMiddle); 9544 verifyFormat("int * f(int * a) {}", PointerMiddle); 9545 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 9546 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 9547 verifyFormat("A<int *> a;", PointerMiddle); 9548 verifyFormat("A<int **> a;", PointerMiddle); 9549 verifyFormat("A<int *, int *> a;", PointerMiddle); 9550 verifyFormat("A<int *[]> a;", PointerMiddle); 9551 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 9552 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 9553 verifyFormat("T ** t = new T *;", PointerMiddle); 9554 9555 // Member function reference qualifiers aren't binary operators. 9556 verifyFormat("string // break\n" 9557 "operator()() & {}"); 9558 verifyFormat("string // break\n" 9559 "operator()() && {}"); 9560 verifyGoogleFormat("template <typename T>\n" 9561 "auto x() & -> int {}"); 9562 9563 // Should be binary operators when used as an argument expression (overloaded 9564 // operator invoked as a member function). 9565 verifyFormat("void f() { a.operator()(a * a); }"); 9566 verifyFormat("void f() { a->operator()(a & a); }"); 9567 verifyFormat("void f() { a.operator()(*a & *a); }"); 9568 verifyFormat("void f() { a->operator()(*a * *a); }"); 9569 } 9570 9571 TEST_F(FormatTest, UnderstandsAttributes) { 9572 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 9573 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 9574 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 9575 FormatStyle AfterType = getLLVMStyle(); 9576 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 9577 verifyFormat("__attribute__((nodebug)) void\n" 9578 "foo() {}\n", 9579 AfterType); 9580 verifyFormat("__unused void\n" 9581 "foo() {}", 9582 AfterType); 9583 9584 FormatStyle CustomAttrs = getLLVMStyle(); 9585 CustomAttrs.AttributeMacros.push_back("__unused"); 9586 CustomAttrs.AttributeMacros.push_back("__attr1"); 9587 CustomAttrs.AttributeMacros.push_back("__attr2"); 9588 CustomAttrs.AttributeMacros.push_back("no_underscore_attr"); 9589 verifyFormat("vector<SomeType *__attribute((foo))> v;"); 9590 verifyFormat("vector<SomeType *__attribute__((foo))> v;"); 9591 verifyFormat("vector<SomeType * __not_attribute__((foo))> v;"); 9592 // Check that it is parsed as a multiplication without AttributeMacros and 9593 // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros. 9594 verifyFormat("vector<SomeType * __attr1> v;"); 9595 verifyFormat("vector<SomeType __attr1 *> v;"); 9596 verifyFormat("vector<SomeType __attr1 *const> v;"); 9597 verifyFormat("vector<SomeType __attr1 * __attr2> v;"); 9598 verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs); 9599 verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs); 9600 verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs); 9601 verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs); 9602 verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs); 9603 verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs); 9604 verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs); 9605 9606 // Check that these are not parsed as function declarations: 9607 CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 9608 CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman; 9609 verifyFormat("SomeType s(InitValue);", CustomAttrs); 9610 verifyFormat("SomeType s{InitValue};", CustomAttrs); 9611 verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs); 9612 verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs); 9613 verifyFormat("SomeType s __unused(InitValue);", CustomAttrs); 9614 verifyFormat("SomeType s __unused{InitValue};", CustomAttrs); 9615 verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs); 9616 verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs); 9617 } 9618 9619 TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) { 9620 // Check that qualifiers on pointers don't break parsing of casts. 9621 verifyFormat("x = (foo *const)*v;"); 9622 verifyFormat("x = (foo *volatile)*v;"); 9623 verifyFormat("x = (foo *restrict)*v;"); 9624 verifyFormat("x = (foo *__attribute__((foo)))*v;"); 9625 verifyFormat("x = (foo *_Nonnull)*v;"); 9626 verifyFormat("x = (foo *_Nullable)*v;"); 9627 verifyFormat("x = (foo *_Null_unspecified)*v;"); 9628 verifyFormat("x = (foo *_Nonnull)*v;"); 9629 verifyFormat("x = (foo *[[clang::attr]])*v;"); 9630 verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;"); 9631 verifyFormat("x = (foo *__ptr32)*v;"); 9632 verifyFormat("x = (foo *__ptr64)*v;"); 9633 verifyFormat("x = (foo *__capability)*v;"); 9634 9635 // Check that we handle multiple trailing qualifiers and skip them all to 9636 // determine that the expression is a cast to a pointer type. 9637 FormatStyle LongPointerRight = getLLVMStyleWithColumns(999); 9638 FormatStyle LongPointerLeft = getLLVMStyleWithColumns(999); 9639 LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left; 9640 StringRef AllQualifiers = 9641 "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified " 9642 "_Nonnull [[clang::attr]] __ptr32 __ptr64 __capability"; 9643 verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight); 9644 verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft); 9645 9646 // Also check that address-of is not parsed as a binary bitwise-and: 9647 verifyFormat("x = (foo *const)&v;"); 9648 verifyFormat(("x = (foo *" + AllQualifiers + ")&v;").str(), LongPointerRight); 9649 verifyFormat(("x = (foo* " + AllQualifiers + ")&v;").str(), LongPointerLeft); 9650 9651 // Check custom qualifiers: 9652 FormatStyle CustomQualifier = getLLVMStyleWithColumns(999); 9653 CustomQualifier.AttributeMacros.push_back("__my_qualifier"); 9654 verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier. 9655 verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier); 9656 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)*v;").str(), 9657 CustomQualifier); 9658 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)&v;").str(), 9659 CustomQualifier); 9660 9661 // Check that unknown identifiers result in binary operator parsing: 9662 verifyFormat("x = (foo * __unknown_qualifier) * v;"); 9663 verifyFormat("x = (foo * __unknown_qualifier) & v;"); 9664 } 9665 9666 TEST_F(FormatTest, UnderstandsSquareAttributes) { 9667 verifyFormat("SomeType s [[unused]] (InitValue);"); 9668 verifyFormat("SomeType s [[gnu::unused]] (InitValue);"); 9669 verifyFormat("SomeType s [[using gnu: unused]] (InitValue);"); 9670 verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}"); 9671 verifyFormat("void f() [[deprecated(\"so sorry\")]];"); 9672 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9673 " [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 9674 verifyFormat("[[nodiscard]] bool f() { return false; }"); 9675 verifyFormat("class [[nodiscard]] f {\npublic:\n f() {}\n}"); 9676 verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n f() {}\n}"); 9677 verifyFormat("class [[gnu::unused]] f {\npublic:\n f() {}\n}"); 9678 9679 // Make sure we do not mistake attributes for array subscripts. 9680 verifyFormat("int a() {}\n" 9681 "[[unused]] int b() {}\n"); 9682 verifyFormat("NSArray *arr;\n" 9683 "arr[[Foo() bar]];"); 9684 9685 // On the other hand, we still need to correctly find array subscripts. 9686 verifyFormat("int a = std::vector<int>{1, 2, 3}[0];"); 9687 9688 // Make sure that we do not mistake Objective-C method inside array literals 9689 // as attributes, even if those method names are also keywords. 9690 verifyFormat("@[ [foo bar] ];"); 9691 verifyFormat("@[ [NSArray class] ];"); 9692 verifyFormat("@[ [foo enum] ];"); 9693 9694 verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }"); 9695 9696 // Make sure we do not parse attributes as lambda introducers. 9697 FormatStyle MultiLineFunctions = getLLVMStyle(); 9698 MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 9699 verifyFormat("[[unused]] int b() {\n" 9700 " return 42;\n" 9701 "}\n", 9702 MultiLineFunctions); 9703 } 9704 9705 TEST_F(FormatTest, AttributeClass) { 9706 FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp); 9707 verifyFormat("class S {\n" 9708 " S(S&&) = default;\n" 9709 "};", 9710 Style); 9711 verifyFormat("class [[nodiscard]] S {\n" 9712 " S(S&&) = default;\n" 9713 "};", 9714 Style); 9715 verifyFormat("class __attribute((maybeunused)) S {\n" 9716 " S(S&&) = default;\n" 9717 "};", 9718 Style); 9719 verifyFormat("struct S {\n" 9720 " S(S&&) = default;\n" 9721 "};", 9722 Style); 9723 verifyFormat("struct [[nodiscard]] S {\n" 9724 " S(S&&) = default;\n" 9725 "};", 9726 Style); 9727 } 9728 9729 TEST_F(FormatTest, AttributesAfterMacro) { 9730 FormatStyle Style = getLLVMStyle(); 9731 verifyFormat("MACRO;\n" 9732 "__attribute__((maybe_unused)) int foo() {\n" 9733 " //...\n" 9734 "}"); 9735 9736 verifyFormat("MACRO;\n" 9737 "[[nodiscard]] int foo() {\n" 9738 " //...\n" 9739 "}"); 9740 9741 EXPECT_EQ("MACRO\n\n" 9742 "__attribute__((maybe_unused)) int foo() {\n" 9743 " //...\n" 9744 "}", 9745 format("MACRO\n\n" 9746 "__attribute__((maybe_unused)) int foo() {\n" 9747 " //...\n" 9748 "}")); 9749 9750 EXPECT_EQ("MACRO\n\n" 9751 "[[nodiscard]] int foo() {\n" 9752 " //...\n" 9753 "}", 9754 format("MACRO\n\n" 9755 "[[nodiscard]] int foo() {\n" 9756 " //...\n" 9757 "}")); 9758 } 9759 9760 TEST_F(FormatTest, AttributePenaltyBreaking) { 9761 FormatStyle Style = getLLVMStyle(); 9762 verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n" 9763 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", 9764 Style); 9765 verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n" 9766 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", 9767 Style); 9768 verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const " 9769 "shared_ptr<ALongTypeName> &C d) {\n}", 9770 Style); 9771 } 9772 9773 TEST_F(FormatTest, UnderstandsEllipsis) { 9774 FormatStyle Style = getLLVMStyle(); 9775 verifyFormat("int printf(const char *fmt, ...);"); 9776 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 9777 verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}"); 9778 9779 verifyFormat("template <int *...PP> a;", Style); 9780 9781 Style.PointerAlignment = FormatStyle::PAS_Left; 9782 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style); 9783 9784 verifyFormat("template <int*... PP> a;", Style); 9785 9786 Style.PointerAlignment = FormatStyle::PAS_Middle; 9787 verifyFormat("template <int *... PP> a;", Style); 9788 } 9789 9790 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 9791 EXPECT_EQ("int *a;\n" 9792 "int *a;\n" 9793 "int *a;", 9794 format("int *a;\n" 9795 "int* a;\n" 9796 "int *a;", 9797 getGoogleStyle())); 9798 EXPECT_EQ("int* a;\n" 9799 "int* a;\n" 9800 "int* a;", 9801 format("int* a;\n" 9802 "int* a;\n" 9803 "int *a;", 9804 getGoogleStyle())); 9805 EXPECT_EQ("int *a;\n" 9806 "int *a;\n" 9807 "int *a;", 9808 format("int *a;\n" 9809 "int * a;\n" 9810 "int * a;", 9811 getGoogleStyle())); 9812 EXPECT_EQ("auto x = [] {\n" 9813 " int *a;\n" 9814 " int *a;\n" 9815 " int *a;\n" 9816 "};", 9817 format("auto x=[]{int *a;\n" 9818 "int * a;\n" 9819 "int * a;};", 9820 getGoogleStyle())); 9821 } 9822 9823 TEST_F(FormatTest, UnderstandsRvalueReferences) { 9824 verifyFormat("int f(int &&a) {}"); 9825 verifyFormat("int f(int a, char &&b) {}"); 9826 verifyFormat("void f() { int &&a = b; }"); 9827 verifyGoogleFormat("int f(int a, char&& b) {}"); 9828 verifyGoogleFormat("void f() { int&& a = b; }"); 9829 9830 verifyIndependentOfContext("A<int &&> a;"); 9831 verifyIndependentOfContext("A<int &&, int &&> a;"); 9832 verifyGoogleFormat("A<int&&> a;"); 9833 verifyGoogleFormat("A<int&&, int&&> a;"); 9834 9835 // Not rvalue references: 9836 verifyFormat("template <bool B, bool C> class A {\n" 9837 " static_assert(B && C, \"Something is wrong\");\n" 9838 "};"); 9839 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 9840 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 9841 verifyFormat("#define A(a, b) (a && b)"); 9842 } 9843 9844 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 9845 verifyFormat("void f() {\n" 9846 " x[aaaaaaaaa -\n" 9847 " b] = 23;\n" 9848 "}", 9849 getLLVMStyleWithColumns(15)); 9850 } 9851 9852 TEST_F(FormatTest, FormatsCasts) { 9853 verifyFormat("Type *A = static_cast<Type *>(P);"); 9854 verifyFormat("Type *A = (Type *)P;"); 9855 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 9856 verifyFormat("int a = (int)(2.0f);"); 9857 verifyFormat("int a = (int)2.0f;"); 9858 verifyFormat("x[(int32)y];"); 9859 verifyFormat("x = (int32)y;"); 9860 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 9861 verifyFormat("int a = (int)*b;"); 9862 verifyFormat("int a = (int)2.0f;"); 9863 verifyFormat("int a = (int)~0;"); 9864 verifyFormat("int a = (int)++a;"); 9865 verifyFormat("int a = (int)sizeof(int);"); 9866 verifyFormat("int a = (int)+2;"); 9867 verifyFormat("my_int a = (my_int)2.0f;"); 9868 verifyFormat("my_int a = (my_int)sizeof(int);"); 9869 verifyFormat("return (my_int)aaa;"); 9870 verifyFormat("#define x ((int)-1)"); 9871 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 9872 verifyFormat("#define p(q) ((int *)&q)"); 9873 verifyFormat("fn(a)(b) + 1;"); 9874 9875 verifyFormat("void f() { my_int a = (my_int)*b; }"); 9876 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 9877 verifyFormat("my_int a = (my_int)~0;"); 9878 verifyFormat("my_int a = (my_int)++a;"); 9879 verifyFormat("my_int a = (my_int)-2;"); 9880 verifyFormat("my_int a = (my_int)1;"); 9881 verifyFormat("my_int a = (my_int *)1;"); 9882 verifyFormat("my_int a = (const my_int)-1;"); 9883 verifyFormat("my_int a = (const my_int *)-1;"); 9884 verifyFormat("my_int a = (my_int)(my_int)-1;"); 9885 verifyFormat("my_int a = (ns::my_int)-2;"); 9886 verifyFormat("case (my_int)ONE:"); 9887 verifyFormat("auto x = (X)this;"); 9888 // Casts in Obj-C style calls used to not be recognized as such. 9889 verifyFormat("int a = [(type*)[((type*)val) arg] arg];", getGoogleStyle()); 9890 9891 // FIXME: single value wrapped with paren will be treated as cast. 9892 verifyFormat("void f(int i = (kValue)*kMask) {}"); 9893 9894 verifyFormat("{ (void)F; }"); 9895 9896 // Don't break after a cast's 9897 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 9898 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 9899 " bbbbbbbbbbbbbbbbbbbbbb);"); 9900 9901 // These are not casts. 9902 verifyFormat("void f(int *) {}"); 9903 verifyFormat("f(foo)->b;"); 9904 verifyFormat("f(foo).b;"); 9905 verifyFormat("f(foo)(b);"); 9906 verifyFormat("f(foo)[b];"); 9907 verifyFormat("[](foo) { return 4; }(bar);"); 9908 verifyFormat("(*funptr)(foo)[4];"); 9909 verifyFormat("funptrs[4](foo)[4];"); 9910 verifyFormat("void f(int *);"); 9911 verifyFormat("void f(int *) = 0;"); 9912 verifyFormat("void f(SmallVector<int>) {}"); 9913 verifyFormat("void f(SmallVector<int>);"); 9914 verifyFormat("void f(SmallVector<int>) = 0;"); 9915 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 9916 verifyFormat("int a = sizeof(int) * b;"); 9917 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 9918 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 9919 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 9920 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 9921 9922 // These are not casts, but at some point were confused with casts. 9923 verifyFormat("virtual void foo(int *) override;"); 9924 verifyFormat("virtual void foo(char &) const;"); 9925 verifyFormat("virtual void foo(int *a, char *) const;"); 9926 verifyFormat("int a = sizeof(int *) + b;"); 9927 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 9928 verifyFormat("bool b = f(g<int>) && c;"); 9929 verifyFormat("typedef void (*f)(int i) func;"); 9930 verifyFormat("void operator++(int) noexcept;"); 9931 verifyFormat("void operator++(int &) noexcept;"); 9932 verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t " 9933 "&) noexcept;"); 9934 verifyFormat( 9935 "void operator delete(std::size_t, const std::nothrow_t &) noexcept;"); 9936 verifyFormat("void operator delete(const std::nothrow_t &) noexcept;"); 9937 verifyFormat("void operator delete(std::nothrow_t &) noexcept;"); 9938 verifyFormat("void operator delete(nothrow_t &) noexcept;"); 9939 verifyFormat("void operator delete(foo &) noexcept;"); 9940 verifyFormat("void operator delete(foo) noexcept;"); 9941 verifyFormat("void operator delete(int) noexcept;"); 9942 verifyFormat("void operator delete(int &) noexcept;"); 9943 verifyFormat("void operator delete(int &) volatile noexcept;"); 9944 verifyFormat("void operator delete(int &) const"); 9945 verifyFormat("void operator delete(int &) = default"); 9946 verifyFormat("void operator delete(int &) = delete"); 9947 verifyFormat("void operator delete(int &) [[noreturn]]"); 9948 verifyFormat("void operator delete(int &) throw();"); 9949 verifyFormat("void operator delete(int &) throw(int);"); 9950 verifyFormat("auto operator delete(int &) -> int;"); 9951 verifyFormat("auto operator delete(int &) override"); 9952 verifyFormat("auto operator delete(int &) final"); 9953 9954 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 9955 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 9956 // FIXME: The indentation here is not ideal. 9957 verifyFormat( 9958 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9959 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 9960 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 9961 } 9962 9963 TEST_F(FormatTest, FormatsFunctionTypes) { 9964 verifyFormat("A<bool()> a;"); 9965 verifyFormat("A<SomeType()> a;"); 9966 verifyFormat("A<void (*)(int, std::string)> a;"); 9967 verifyFormat("A<void *(int)>;"); 9968 verifyFormat("void *(*a)(int *, SomeType *);"); 9969 verifyFormat("int (*func)(void *);"); 9970 verifyFormat("void f() { int (*func)(void *); }"); 9971 verifyFormat("template <class CallbackClass>\n" 9972 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 9973 9974 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 9975 verifyGoogleFormat("void* (*a)(int);"); 9976 verifyGoogleFormat( 9977 "template <class CallbackClass>\n" 9978 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 9979 9980 // Other constructs can look somewhat like function types: 9981 verifyFormat("A<sizeof(*x)> a;"); 9982 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 9983 verifyFormat("some_var = function(*some_pointer_var)[0];"); 9984 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 9985 verifyFormat("int x = f(&h)();"); 9986 verifyFormat("returnsFunction(¶m1, ¶m2)(param);"); 9987 verifyFormat("std::function<\n" 9988 " LooooooooooongTemplatedType<\n" 9989 " SomeType>*(\n" 9990 " LooooooooooooooooongType type)>\n" 9991 " function;", 9992 getGoogleStyleWithColumns(40)); 9993 } 9994 9995 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 9996 verifyFormat("A (*foo_)[6];"); 9997 verifyFormat("vector<int> (*foo_)[6];"); 9998 } 9999 10000 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 10001 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10002 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 10003 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 10004 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 10005 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10006 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 10007 10008 // Different ways of ()-initializiation. 10009 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10010 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 10011 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10012 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 10013 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10014 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 10015 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10016 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 10017 10018 // Lambdas should not confuse the variable declaration heuristic. 10019 verifyFormat("LooooooooooooooooongType\n" 10020 " variable(nullptr, [](A *a) {});", 10021 getLLVMStyleWithColumns(40)); 10022 } 10023 10024 TEST_F(FormatTest, BreaksLongDeclarations) { 10025 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 10026 " AnotherNameForTheLongType;"); 10027 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 10028 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 10029 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10030 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 10031 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 10032 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 10033 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10034 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10035 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 10036 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10037 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 10038 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10039 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 10040 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10041 verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n" 10042 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10043 verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n" 10044 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10045 verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n" 10046 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10047 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10048 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);"); 10049 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10050 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}"); 10051 FormatStyle Indented = getLLVMStyle(); 10052 Indented.IndentWrappedFunctionNames = true; 10053 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10054 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 10055 Indented); 10056 verifyFormat( 10057 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10058 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 10059 Indented); 10060 verifyFormat( 10061 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 10062 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 10063 Indented); 10064 verifyFormat( 10065 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 10066 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 10067 Indented); 10068 10069 // FIXME: Without the comment, this breaks after "(". 10070 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 10071 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 10072 getGoogleStyle()); 10073 10074 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 10075 " int LoooooooooooooooooooongParam2) {}"); 10076 verifyFormat( 10077 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 10078 " SourceLocation L, IdentifierIn *II,\n" 10079 " Type *T) {}"); 10080 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 10081 "ReallyReaaallyLongFunctionName(\n" 10082 " const std::string &SomeParameter,\n" 10083 " const SomeType<string, SomeOtherTemplateParameter>\n" 10084 " &ReallyReallyLongParameterName,\n" 10085 " const SomeType<string, SomeOtherTemplateParameter>\n" 10086 " &AnotherLongParameterName) {}"); 10087 verifyFormat("template <typename A>\n" 10088 "SomeLoooooooooooooooooooooongType<\n" 10089 " typename some_namespace::SomeOtherType<A>::Type>\n" 10090 "Function() {}"); 10091 10092 verifyGoogleFormat( 10093 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 10094 " aaaaaaaaaaaaaaaaaaaaaaa;"); 10095 verifyGoogleFormat( 10096 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 10097 " SourceLocation L) {}"); 10098 verifyGoogleFormat( 10099 "some_namespace::LongReturnType\n" 10100 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 10101 " int first_long_parameter, int second_parameter) {}"); 10102 10103 verifyGoogleFormat("template <typename T>\n" 10104 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 10105 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 10106 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 10107 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 10108 10109 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 10110 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10111 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 10112 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10113 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 10114 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 10115 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10116 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 10117 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 10118 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 10119 10120 verifyFormat("template <typename T> // Templates on own line.\n" 10121 "static int // Some comment.\n" 10122 "MyFunction(int a);", 10123 getLLVMStyle()); 10124 } 10125 10126 TEST_F(FormatTest, FormatsAccessModifiers) { 10127 FormatStyle Style = getLLVMStyle(); 10128 EXPECT_EQ(Style.EmptyLineBeforeAccessModifier, 10129 FormatStyle::ELBAMS_LogicalBlock); 10130 verifyFormat("struct foo {\n" 10131 "private:\n" 10132 " void f() {}\n" 10133 "\n" 10134 "private:\n" 10135 " int i;\n" 10136 "\n" 10137 "protected:\n" 10138 " int j;\n" 10139 "};\n", 10140 Style); 10141 verifyFormat("struct foo {\n" 10142 "private:\n" 10143 " void f() {}\n" 10144 "\n" 10145 "private:\n" 10146 " int i;\n" 10147 "\n" 10148 "protected:\n" 10149 " int j;\n" 10150 "};\n", 10151 "struct foo {\n" 10152 "private:\n" 10153 " void f() {}\n" 10154 "private:\n" 10155 " int i;\n" 10156 "protected:\n" 10157 " int j;\n" 10158 "};\n", 10159 Style); 10160 verifyFormat("struct foo { /* comment */\n" 10161 "private:\n" 10162 " int i;\n" 10163 " // comment\n" 10164 "private:\n" 10165 " int j;\n" 10166 "};\n", 10167 Style); 10168 verifyFormat("struct foo {\n" 10169 "#ifdef FOO\n" 10170 "#endif\n" 10171 "private:\n" 10172 " int i;\n" 10173 "#ifdef FOO\n" 10174 "private:\n" 10175 "#endif\n" 10176 " int j;\n" 10177 "};\n", 10178 Style); 10179 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10180 verifyFormat("struct foo {\n" 10181 "private:\n" 10182 " void f() {}\n" 10183 "private:\n" 10184 " int i;\n" 10185 "protected:\n" 10186 " int j;\n" 10187 "};\n", 10188 Style); 10189 verifyFormat("struct foo {\n" 10190 "private:\n" 10191 " void f() {}\n" 10192 "private:\n" 10193 " int i;\n" 10194 "protected:\n" 10195 " int j;\n" 10196 "};\n", 10197 "struct foo {\n" 10198 "\n" 10199 "private:\n" 10200 " void f() {}\n" 10201 "\n" 10202 "private:\n" 10203 " int i;\n" 10204 "\n" 10205 "protected:\n" 10206 " int j;\n" 10207 "};\n", 10208 Style); 10209 verifyFormat("struct foo { /* comment */\n" 10210 "private:\n" 10211 " int i;\n" 10212 " // comment\n" 10213 "private:\n" 10214 " int j;\n" 10215 "};\n", 10216 "struct foo { /* comment */\n" 10217 "\n" 10218 "private:\n" 10219 " int i;\n" 10220 " // comment\n" 10221 "\n" 10222 "private:\n" 10223 " int j;\n" 10224 "};\n", 10225 Style); 10226 verifyFormat("struct foo {\n" 10227 "#ifdef FOO\n" 10228 "#endif\n" 10229 "private:\n" 10230 " int i;\n" 10231 "#ifdef FOO\n" 10232 "private:\n" 10233 "#endif\n" 10234 " int j;\n" 10235 "};\n", 10236 "struct foo {\n" 10237 "#ifdef FOO\n" 10238 "#endif\n" 10239 "\n" 10240 "private:\n" 10241 " int i;\n" 10242 "#ifdef FOO\n" 10243 "\n" 10244 "private:\n" 10245 "#endif\n" 10246 " int j;\n" 10247 "};\n", 10248 Style); 10249 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10250 verifyFormat("struct foo {\n" 10251 "private:\n" 10252 " void f() {}\n" 10253 "\n" 10254 "private:\n" 10255 " int i;\n" 10256 "\n" 10257 "protected:\n" 10258 " int j;\n" 10259 "};\n", 10260 Style); 10261 verifyFormat("struct foo {\n" 10262 "private:\n" 10263 " void f() {}\n" 10264 "\n" 10265 "private:\n" 10266 " int i;\n" 10267 "\n" 10268 "protected:\n" 10269 " int j;\n" 10270 "};\n", 10271 "struct foo {\n" 10272 "private:\n" 10273 " void f() {}\n" 10274 "private:\n" 10275 " int i;\n" 10276 "protected:\n" 10277 " int j;\n" 10278 "};\n", 10279 Style); 10280 verifyFormat("struct foo { /* comment */\n" 10281 "private:\n" 10282 " int i;\n" 10283 " // comment\n" 10284 "\n" 10285 "private:\n" 10286 " int j;\n" 10287 "};\n", 10288 "struct foo { /* comment */\n" 10289 "private:\n" 10290 " int i;\n" 10291 " // comment\n" 10292 "\n" 10293 "private:\n" 10294 " int j;\n" 10295 "};\n", 10296 Style); 10297 verifyFormat("struct foo {\n" 10298 "#ifdef FOO\n" 10299 "#endif\n" 10300 "\n" 10301 "private:\n" 10302 " int i;\n" 10303 "#ifdef FOO\n" 10304 "\n" 10305 "private:\n" 10306 "#endif\n" 10307 " int j;\n" 10308 "};\n", 10309 "struct foo {\n" 10310 "#ifdef FOO\n" 10311 "#endif\n" 10312 "private:\n" 10313 " int i;\n" 10314 "#ifdef FOO\n" 10315 "private:\n" 10316 "#endif\n" 10317 " int j;\n" 10318 "};\n", 10319 Style); 10320 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 10321 EXPECT_EQ("struct foo {\n" 10322 "\n" 10323 "private:\n" 10324 " void f() {}\n" 10325 "\n" 10326 "private:\n" 10327 " int i;\n" 10328 "\n" 10329 "protected:\n" 10330 " int j;\n" 10331 "};\n", 10332 format("struct foo {\n" 10333 "\n" 10334 "private:\n" 10335 " void f() {}\n" 10336 "\n" 10337 "private:\n" 10338 " int i;\n" 10339 "\n" 10340 "protected:\n" 10341 " int j;\n" 10342 "};\n", 10343 Style)); 10344 verifyFormat("struct foo {\n" 10345 "private:\n" 10346 " void f() {}\n" 10347 "private:\n" 10348 " int i;\n" 10349 "protected:\n" 10350 " int j;\n" 10351 "};\n", 10352 Style); 10353 EXPECT_EQ("struct foo { /* comment */\n" 10354 "\n" 10355 "private:\n" 10356 " int i;\n" 10357 " // comment\n" 10358 "\n" 10359 "private:\n" 10360 " int j;\n" 10361 "};\n", 10362 format("struct foo { /* comment */\n" 10363 "\n" 10364 "private:\n" 10365 " int i;\n" 10366 " // comment\n" 10367 "\n" 10368 "private:\n" 10369 " int j;\n" 10370 "};\n", 10371 Style)); 10372 verifyFormat("struct foo { /* comment */\n" 10373 "private:\n" 10374 " int i;\n" 10375 " // comment\n" 10376 "private:\n" 10377 " int j;\n" 10378 "};\n", 10379 Style); 10380 EXPECT_EQ("struct foo {\n" 10381 "#ifdef FOO\n" 10382 "#endif\n" 10383 "\n" 10384 "private:\n" 10385 " int i;\n" 10386 "#ifdef FOO\n" 10387 "\n" 10388 "private:\n" 10389 "#endif\n" 10390 " int j;\n" 10391 "};\n", 10392 format("struct foo {\n" 10393 "#ifdef FOO\n" 10394 "#endif\n" 10395 "\n" 10396 "private:\n" 10397 " int i;\n" 10398 "#ifdef FOO\n" 10399 "\n" 10400 "private:\n" 10401 "#endif\n" 10402 " int j;\n" 10403 "};\n", 10404 Style)); 10405 verifyFormat("struct foo {\n" 10406 "#ifdef FOO\n" 10407 "#endif\n" 10408 "private:\n" 10409 " int i;\n" 10410 "#ifdef FOO\n" 10411 "private:\n" 10412 "#endif\n" 10413 " int j;\n" 10414 "};\n", 10415 Style); 10416 10417 FormatStyle NoEmptyLines = getLLVMStyle(); 10418 NoEmptyLines.MaxEmptyLinesToKeep = 0; 10419 verifyFormat("struct foo {\n" 10420 "private:\n" 10421 " void f() {}\n" 10422 "\n" 10423 "private:\n" 10424 " int i;\n" 10425 "\n" 10426 "public:\n" 10427 "protected:\n" 10428 " int j;\n" 10429 "};\n", 10430 NoEmptyLines); 10431 10432 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10433 verifyFormat("struct foo {\n" 10434 "private:\n" 10435 " void f() {}\n" 10436 "private:\n" 10437 " int i;\n" 10438 "public:\n" 10439 "protected:\n" 10440 " int j;\n" 10441 "};\n", 10442 NoEmptyLines); 10443 10444 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10445 verifyFormat("struct foo {\n" 10446 "private:\n" 10447 " void f() {}\n" 10448 "\n" 10449 "private:\n" 10450 " int i;\n" 10451 "\n" 10452 "public:\n" 10453 "\n" 10454 "protected:\n" 10455 " int j;\n" 10456 "};\n", 10457 NoEmptyLines); 10458 } 10459 10460 TEST_F(FormatTest, FormatsAfterAccessModifiers) { 10461 10462 FormatStyle Style = getLLVMStyle(); 10463 EXPECT_EQ(Style.EmptyLineAfterAccessModifier, FormatStyle::ELAAMS_Never); 10464 verifyFormat("struct foo {\n" 10465 "private:\n" 10466 " void f() {}\n" 10467 "\n" 10468 "private:\n" 10469 " int i;\n" 10470 "\n" 10471 "protected:\n" 10472 " int j;\n" 10473 "};\n", 10474 Style); 10475 10476 // Check if lines are removed. 10477 verifyFormat("struct foo {\n" 10478 "private:\n" 10479 " void f() {}\n" 10480 "\n" 10481 "private:\n" 10482 " int i;\n" 10483 "\n" 10484 "protected:\n" 10485 " int j;\n" 10486 "};\n", 10487 "struct foo {\n" 10488 "private:\n" 10489 "\n" 10490 " void f() {}\n" 10491 "\n" 10492 "private:\n" 10493 "\n" 10494 " int i;\n" 10495 "\n" 10496 "protected:\n" 10497 "\n" 10498 " int j;\n" 10499 "};\n", 10500 Style); 10501 10502 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10503 verifyFormat("struct foo {\n" 10504 "private:\n" 10505 "\n" 10506 " void f() {}\n" 10507 "\n" 10508 "private:\n" 10509 "\n" 10510 " int i;\n" 10511 "\n" 10512 "protected:\n" 10513 "\n" 10514 " int j;\n" 10515 "};\n", 10516 Style); 10517 10518 // Check if lines are added. 10519 verifyFormat("struct foo {\n" 10520 "private:\n" 10521 "\n" 10522 " void f() {}\n" 10523 "\n" 10524 "private:\n" 10525 "\n" 10526 " int i;\n" 10527 "\n" 10528 "protected:\n" 10529 "\n" 10530 " int j;\n" 10531 "};\n", 10532 "struct foo {\n" 10533 "private:\n" 10534 " void f() {}\n" 10535 "\n" 10536 "private:\n" 10537 " int i;\n" 10538 "\n" 10539 "protected:\n" 10540 " int j;\n" 10541 "};\n", 10542 Style); 10543 10544 // Leave tests rely on the code layout, test::messUp can not be used. 10545 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 10546 Style.MaxEmptyLinesToKeep = 0u; 10547 verifyFormat("struct foo {\n" 10548 "private:\n" 10549 " void f() {}\n" 10550 "\n" 10551 "private:\n" 10552 " int i;\n" 10553 "\n" 10554 "protected:\n" 10555 " int j;\n" 10556 "};\n", 10557 Style); 10558 10559 // Check if MaxEmptyLinesToKeep is respected. 10560 EXPECT_EQ("struct foo {\n" 10561 "private:\n" 10562 " void f() {}\n" 10563 "\n" 10564 "private:\n" 10565 " int i;\n" 10566 "\n" 10567 "protected:\n" 10568 " int j;\n" 10569 "};\n", 10570 format("struct foo {\n" 10571 "private:\n" 10572 "\n\n\n" 10573 " void f() {}\n" 10574 "\n" 10575 "private:\n" 10576 "\n\n\n" 10577 " int i;\n" 10578 "\n" 10579 "protected:\n" 10580 "\n\n\n" 10581 " int j;\n" 10582 "};\n", 10583 Style)); 10584 10585 Style.MaxEmptyLinesToKeep = 1u; 10586 EXPECT_EQ("struct foo {\n" 10587 "private:\n" 10588 "\n" 10589 " void f() {}\n" 10590 "\n" 10591 "private:\n" 10592 "\n" 10593 " int i;\n" 10594 "\n" 10595 "protected:\n" 10596 "\n" 10597 " int j;\n" 10598 "};\n", 10599 format("struct foo {\n" 10600 "private:\n" 10601 "\n" 10602 " void f() {}\n" 10603 "\n" 10604 "private:\n" 10605 "\n" 10606 " int i;\n" 10607 "\n" 10608 "protected:\n" 10609 "\n" 10610 " int j;\n" 10611 "};\n", 10612 Style)); 10613 // Check if no lines are kept. 10614 EXPECT_EQ("struct foo {\n" 10615 "private:\n" 10616 " void f() {}\n" 10617 "\n" 10618 "private:\n" 10619 " int i;\n" 10620 "\n" 10621 "protected:\n" 10622 " int j;\n" 10623 "};\n", 10624 format("struct foo {\n" 10625 "private:\n" 10626 " void f() {}\n" 10627 "\n" 10628 "private:\n" 10629 " int i;\n" 10630 "\n" 10631 "protected:\n" 10632 " int j;\n" 10633 "};\n", 10634 Style)); 10635 // Check if MaxEmptyLinesToKeep is respected. 10636 EXPECT_EQ("struct foo {\n" 10637 "private:\n" 10638 "\n" 10639 " void f() {}\n" 10640 "\n" 10641 "private:\n" 10642 "\n" 10643 " int i;\n" 10644 "\n" 10645 "protected:\n" 10646 "\n" 10647 " int j;\n" 10648 "};\n", 10649 format("struct foo {\n" 10650 "private:\n" 10651 "\n\n\n" 10652 " void f() {}\n" 10653 "\n" 10654 "private:\n" 10655 "\n\n\n" 10656 " int i;\n" 10657 "\n" 10658 "protected:\n" 10659 "\n\n\n" 10660 " int j;\n" 10661 "};\n", 10662 Style)); 10663 10664 Style.MaxEmptyLinesToKeep = 10u; 10665 EXPECT_EQ("struct foo {\n" 10666 "private:\n" 10667 "\n\n\n" 10668 " void f() {}\n" 10669 "\n" 10670 "private:\n" 10671 "\n\n\n" 10672 " int i;\n" 10673 "\n" 10674 "protected:\n" 10675 "\n\n\n" 10676 " int j;\n" 10677 "};\n", 10678 format("struct foo {\n" 10679 "private:\n" 10680 "\n\n\n" 10681 " void f() {}\n" 10682 "\n" 10683 "private:\n" 10684 "\n\n\n" 10685 " int i;\n" 10686 "\n" 10687 "protected:\n" 10688 "\n\n\n" 10689 " int j;\n" 10690 "};\n", 10691 Style)); 10692 10693 // Test with comments. 10694 Style = getLLVMStyle(); 10695 verifyFormat("struct foo {\n" 10696 "private:\n" 10697 " // comment\n" 10698 " void f() {}\n" 10699 "\n" 10700 "private: /* comment */\n" 10701 " int i;\n" 10702 "};\n", 10703 Style); 10704 verifyFormat("struct foo {\n" 10705 "private:\n" 10706 " // comment\n" 10707 " void f() {}\n" 10708 "\n" 10709 "private: /* comment */\n" 10710 " int i;\n" 10711 "};\n", 10712 "struct foo {\n" 10713 "private:\n" 10714 "\n" 10715 " // comment\n" 10716 " void f() {}\n" 10717 "\n" 10718 "private: /* comment */\n" 10719 "\n" 10720 " int i;\n" 10721 "};\n", 10722 Style); 10723 10724 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10725 verifyFormat("struct foo {\n" 10726 "private:\n" 10727 "\n" 10728 " // comment\n" 10729 " void f() {}\n" 10730 "\n" 10731 "private: /* comment */\n" 10732 "\n" 10733 " int i;\n" 10734 "};\n", 10735 "struct foo {\n" 10736 "private:\n" 10737 " // comment\n" 10738 " void f() {}\n" 10739 "\n" 10740 "private: /* comment */\n" 10741 " int i;\n" 10742 "};\n", 10743 Style); 10744 verifyFormat("struct foo {\n" 10745 "private:\n" 10746 "\n" 10747 " // comment\n" 10748 " void f() {}\n" 10749 "\n" 10750 "private: /* comment */\n" 10751 "\n" 10752 " int i;\n" 10753 "};\n", 10754 Style); 10755 10756 // Test with preprocessor defines. 10757 Style = getLLVMStyle(); 10758 verifyFormat("struct foo {\n" 10759 "private:\n" 10760 "#ifdef FOO\n" 10761 "#endif\n" 10762 " void f() {}\n" 10763 "};\n", 10764 Style); 10765 verifyFormat("struct foo {\n" 10766 "private:\n" 10767 "#ifdef FOO\n" 10768 "#endif\n" 10769 " void f() {}\n" 10770 "};\n", 10771 "struct foo {\n" 10772 "private:\n" 10773 "\n" 10774 "#ifdef FOO\n" 10775 "#endif\n" 10776 " void f() {}\n" 10777 "};\n", 10778 Style); 10779 10780 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10781 verifyFormat("struct foo {\n" 10782 "private:\n" 10783 "\n" 10784 "#ifdef FOO\n" 10785 "#endif\n" 10786 " void f() {}\n" 10787 "};\n", 10788 "struct foo {\n" 10789 "private:\n" 10790 "#ifdef FOO\n" 10791 "#endif\n" 10792 " void f() {}\n" 10793 "};\n", 10794 Style); 10795 verifyFormat("struct foo {\n" 10796 "private:\n" 10797 "\n" 10798 "#ifdef FOO\n" 10799 "#endif\n" 10800 " void f() {}\n" 10801 "};\n", 10802 Style); 10803 } 10804 10805 TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) { 10806 // Combined tests of EmptyLineAfterAccessModifier and 10807 // EmptyLineBeforeAccessModifier. 10808 FormatStyle Style = getLLVMStyle(); 10809 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10810 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10811 verifyFormat("struct foo {\n" 10812 "private:\n" 10813 "\n" 10814 "protected:\n" 10815 "};\n", 10816 Style); 10817 10818 Style.MaxEmptyLinesToKeep = 10u; 10819 // Both remove all new lines. 10820 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10821 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 10822 verifyFormat("struct foo {\n" 10823 "private:\n" 10824 "protected:\n" 10825 "};\n", 10826 "struct foo {\n" 10827 "private:\n" 10828 "\n\n\n" 10829 "protected:\n" 10830 "};\n", 10831 Style); 10832 10833 // Leave tests rely on the code layout, test::messUp can not be used. 10834 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 10835 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 10836 Style.MaxEmptyLinesToKeep = 10u; 10837 EXPECT_EQ("struct foo {\n" 10838 "private:\n" 10839 "\n\n\n" 10840 "protected:\n" 10841 "};\n", 10842 format("struct foo {\n" 10843 "private:\n" 10844 "\n\n\n" 10845 "protected:\n" 10846 "};\n", 10847 Style)); 10848 Style.MaxEmptyLinesToKeep = 3u; 10849 EXPECT_EQ("struct foo {\n" 10850 "private:\n" 10851 "\n\n\n" 10852 "protected:\n" 10853 "};\n", 10854 format("struct foo {\n" 10855 "private:\n" 10856 "\n\n\n" 10857 "protected:\n" 10858 "};\n", 10859 Style)); 10860 Style.MaxEmptyLinesToKeep = 1u; 10861 EXPECT_EQ("struct foo {\n" 10862 "private:\n" 10863 "\n\n\n" 10864 "protected:\n" 10865 "};\n", 10866 format("struct foo {\n" 10867 "private:\n" 10868 "\n\n\n" 10869 "protected:\n" 10870 "};\n", 10871 Style)); // Based on new lines in original document and not 10872 // on the setting. 10873 10874 Style.MaxEmptyLinesToKeep = 10u; 10875 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10876 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 10877 // Newlines are kept if they are greater than zero, 10878 // test::messUp removes all new lines which changes the logic 10879 EXPECT_EQ("struct foo {\n" 10880 "private:\n" 10881 "\n\n\n" 10882 "protected:\n" 10883 "};\n", 10884 format("struct foo {\n" 10885 "private:\n" 10886 "\n\n\n" 10887 "protected:\n" 10888 "};\n", 10889 Style)); 10890 10891 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 10892 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10893 // test::messUp removes all new lines which changes the logic 10894 EXPECT_EQ("struct foo {\n" 10895 "private:\n" 10896 "\n\n\n" 10897 "protected:\n" 10898 "};\n", 10899 format("struct foo {\n" 10900 "private:\n" 10901 "\n\n\n" 10902 "protected:\n" 10903 "};\n", 10904 Style)); 10905 10906 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 10907 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 10908 EXPECT_EQ("struct foo {\n" 10909 "private:\n" 10910 "\n\n\n" 10911 "protected:\n" 10912 "};\n", 10913 format("struct foo {\n" 10914 "private:\n" 10915 "\n\n\n" 10916 "protected:\n" 10917 "};\n", 10918 Style)); // test::messUp removes all new lines which changes 10919 // the logic. 10920 10921 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10922 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 10923 verifyFormat("struct foo {\n" 10924 "private:\n" 10925 "protected:\n" 10926 "};\n", 10927 "struct foo {\n" 10928 "private:\n" 10929 "\n\n\n" 10930 "protected:\n" 10931 "};\n", 10932 Style); 10933 10934 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10935 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 10936 EXPECT_EQ("struct foo {\n" 10937 "private:\n" 10938 "\n\n\n" 10939 "protected:\n" 10940 "};\n", 10941 format("struct foo {\n" 10942 "private:\n" 10943 "\n\n\n" 10944 "protected:\n" 10945 "};\n", 10946 Style)); // test::messUp removes all new lines which changes 10947 // the logic. 10948 10949 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10950 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10951 verifyFormat("struct foo {\n" 10952 "private:\n" 10953 "protected:\n" 10954 "};\n", 10955 "struct foo {\n" 10956 "private:\n" 10957 "\n\n\n" 10958 "protected:\n" 10959 "};\n", 10960 Style); 10961 10962 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 10963 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10964 verifyFormat("struct foo {\n" 10965 "private:\n" 10966 "protected:\n" 10967 "};\n", 10968 "struct foo {\n" 10969 "private:\n" 10970 "\n\n\n" 10971 "protected:\n" 10972 "};\n", 10973 Style); 10974 10975 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 10976 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 10977 verifyFormat("struct foo {\n" 10978 "private:\n" 10979 "protected:\n" 10980 "};\n", 10981 "struct foo {\n" 10982 "private:\n" 10983 "\n\n\n" 10984 "protected:\n" 10985 "};\n", 10986 Style); 10987 10988 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 10989 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 10990 verifyFormat("struct foo {\n" 10991 "private:\n" 10992 "protected:\n" 10993 "};\n", 10994 "struct foo {\n" 10995 "private:\n" 10996 "\n\n\n" 10997 "protected:\n" 10998 "};\n", 10999 Style); 11000 } 11001 11002 TEST_F(FormatTest, FormatsArrays) { 11003 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 11004 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 11005 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 11006 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 11007 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 11008 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 11009 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11010 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 11011 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11012 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 11013 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11014 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 11015 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 11016 verifyFormat( 11017 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 11018 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 11019 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 11020 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" 11021 " .aaaaaaaaaaaaaaaaaaaaaa();"); 11022 11023 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 11024 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 11025 verifyFormat( 11026 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 11027 " .aaaaaaa[0]\n" 11028 " .aaaaaaaaaaaaaaaaaaaaaa();"); 11029 verifyFormat("a[::b::c];"); 11030 11031 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 11032 11033 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 11034 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 11035 } 11036 11037 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 11038 verifyFormat("(a)->b();"); 11039 verifyFormat("--a;"); 11040 } 11041 11042 TEST_F(FormatTest, HandlesIncludeDirectives) { 11043 verifyFormat("#include <string>\n" 11044 "#include <a/b/c.h>\n" 11045 "#include \"a/b/string\"\n" 11046 "#include \"string.h\"\n" 11047 "#include \"string.h\"\n" 11048 "#include <a-a>\n" 11049 "#include < path with space >\n" 11050 "#include_next <test.h>" 11051 "#include \"abc.h\" // this is included for ABC\n" 11052 "#include \"some long include\" // with a comment\n" 11053 "#include \"some very long include path\"\n" 11054 "#include <some/very/long/include/path>\n", 11055 getLLVMStyleWithColumns(35)); 11056 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 11057 EXPECT_EQ("#include <a>", format("#include<a>")); 11058 11059 verifyFormat("#import <string>"); 11060 verifyFormat("#import <a/b/c.h>"); 11061 verifyFormat("#import \"a/b/string\""); 11062 verifyFormat("#import \"string.h\""); 11063 verifyFormat("#import \"string.h\""); 11064 verifyFormat("#if __has_include(<strstream>)\n" 11065 "#include <strstream>\n" 11066 "#endif"); 11067 11068 verifyFormat("#define MY_IMPORT <a/b>"); 11069 11070 verifyFormat("#if __has_include(<a/b>)"); 11071 verifyFormat("#if __has_include_next(<a/b>)"); 11072 verifyFormat("#define F __has_include(<a/b>)"); 11073 verifyFormat("#define F __has_include_next(<a/b>)"); 11074 11075 // Protocol buffer definition or missing "#". 11076 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 11077 getLLVMStyleWithColumns(30)); 11078 11079 FormatStyle Style = getLLVMStyle(); 11080 Style.AlwaysBreakBeforeMultilineStrings = true; 11081 Style.ColumnLimit = 0; 11082 verifyFormat("#import \"abc.h\"", Style); 11083 11084 // But 'import' might also be a regular C++ namespace. 11085 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11086 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 11087 } 11088 11089 //===----------------------------------------------------------------------===// 11090 // Error recovery tests. 11091 //===----------------------------------------------------------------------===// 11092 11093 TEST_F(FormatTest, IncompleteParameterLists) { 11094 FormatStyle NoBinPacking = getLLVMStyle(); 11095 NoBinPacking.BinPackParameters = false; 11096 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 11097 " double *min_x,\n" 11098 " double *max_x,\n" 11099 " double *min_y,\n" 11100 " double *max_y,\n" 11101 " double *min_z,\n" 11102 " double *max_z, ) {}", 11103 NoBinPacking); 11104 } 11105 11106 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 11107 verifyFormat("void f() { return; }\n42"); 11108 verifyFormat("void f() {\n" 11109 " if (0)\n" 11110 " return;\n" 11111 "}\n" 11112 "42"); 11113 verifyFormat("void f() { return }\n42"); 11114 verifyFormat("void f() {\n" 11115 " if (0)\n" 11116 " return\n" 11117 "}\n" 11118 "42"); 11119 } 11120 11121 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 11122 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 11123 EXPECT_EQ("void f() {\n" 11124 " if (a)\n" 11125 " return\n" 11126 "}", 11127 format("void f ( ) { if ( a ) return }")); 11128 EXPECT_EQ("namespace N {\n" 11129 "void f()\n" 11130 "}", 11131 format("namespace N { void f() }")); 11132 EXPECT_EQ("namespace N {\n" 11133 "void f() {}\n" 11134 "void g()\n" 11135 "} // namespace N", 11136 format("namespace N { void f( ) { } void g( ) }")); 11137 } 11138 11139 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 11140 verifyFormat("int aaaaaaaa =\n" 11141 " // Overlylongcomment\n" 11142 " b;", 11143 getLLVMStyleWithColumns(20)); 11144 verifyFormat("function(\n" 11145 " ShortArgument,\n" 11146 " LoooooooooooongArgument);\n", 11147 getLLVMStyleWithColumns(20)); 11148 } 11149 11150 TEST_F(FormatTest, IncorrectAccessSpecifier) { 11151 verifyFormat("public:"); 11152 verifyFormat("class A {\n" 11153 "public\n" 11154 " void f() {}\n" 11155 "};"); 11156 verifyFormat("public\n" 11157 "int qwerty;"); 11158 verifyFormat("public\n" 11159 "B {}"); 11160 verifyFormat("public\n" 11161 "{}"); 11162 verifyFormat("public\n" 11163 "B { int x; }"); 11164 } 11165 11166 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 11167 verifyFormat("{"); 11168 verifyFormat("#})"); 11169 verifyNoCrash("(/**/[:!] ?[)."); 11170 } 11171 11172 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) { 11173 // Found by oss-fuzz: 11174 // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212 11175 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 11176 Style.ColumnLimit = 60; 11177 verifyNoCrash( 11178 "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20" 11179 "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20" 11180 "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a", 11181 Style); 11182 } 11183 11184 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 11185 verifyFormat("do {\n}"); 11186 verifyFormat("do {\n}\n" 11187 "f();"); 11188 verifyFormat("do {\n}\n" 11189 "wheeee(fun);"); 11190 verifyFormat("do {\n" 11191 " f();\n" 11192 "}"); 11193 } 11194 11195 TEST_F(FormatTest, IncorrectCodeMissingParens) { 11196 verifyFormat("if {\n foo;\n foo();\n}"); 11197 verifyFormat("switch {\n foo;\n foo();\n}"); 11198 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 11199 verifyFormat("while {\n foo;\n foo();\n}"); 11200 verifyFormat("do {\n foo;\n foo();\n} while;"); 11201 } 11202 11203 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 11204 verifyIncompleteFormat("namespace {\n" 11205 "class Foo { Foo (\n" 11206 "};\n" 11207 "} // namespace"); 11208 } 11209 11210 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 11211 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 11212 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 11213 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 11214 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 11215 11216 EXPECT_EQ("{\n" 11217 " {\n" 11218 " breakme(\n" 11219 " qwe);\n" 11220 " }\n", 11221 format("{\n" 11222 " {\n" 11223 " breakme(qwe);\n" 11224 "}\n", 11225 getLLVMStyleWithColumns(10))); 11226 } 11227 11228 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 11229 verifyFormat("int x = {\n" 11230 " avariable,\n" 11231 " b(alongervariable)};", 11232 getLLVMStyleWithColumns(25)); 11233 } 11234 11235 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 11236 verifyFormat("return (a)(b){1, 2, 3};"); 11237 } 11238 11239 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 11240 verifyFormat("vector<int> x{1, 2, 3, 4};"); 11241 verifyFormat("vector<int> x{\n" 11242 " 1,\n" 11243 " 2,\n" 11244 " 3,\n" 11245 " 4,\n" 11246 "};"); 11247 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 11248 verifyFormat("f({1, 2});"); 11249 verifyFormat("auto v = Foo{-1};"); 11250 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 11251 verifyFormat("Class::Class : member{1, 2, 3} {}"); 11252 verifyFormat("new vector<int>{1, 2, 3};"); 11253 verifyFormat("new int[3]{1, 2, 3};"); 11254 verifyFormat("new int{1};"); 11255 verifyFormat("return {arg1, arg2};"); 11256 verifyFormat("return {arg1, SomeType{parameter}};"); 11257 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 11258 verifyFormat("new T{arg1, arg2};"); 11259 verifyFormat("f(MyMap[{composite, key}]);"); 11260 verifyFormat("class Class {\n" 11261 " T member = {arg1, arg2};\n" 11262 "};"); 11263 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 11264 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 11265 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 11266 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 11267 verifyFormat("int a = std::is_integral<int>{} + 0;"); 11268 11269 verifyFormat("int foo(int i) { return fo1{}(i); }"); 11270 verifyFormat("int foo(int i) { return fo1{}(i); }"); 11271 verifyFormat("auto i = decltype(x){};"); 11272 verifyFormat("auto i = typeof(x){};"); 11273 verifyFormat("auto i = _Atomic(x){};"); 11274 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 11275 verifyFormat("Node n{1, Node{1000}, //\n" 11276 " 2};"); 11277 verifyFormat("Aaaa aaaaaaa{\n" 11278 " {\n" 11279 " aaaa,\n" 11280 " },\n" 11281 "};"); 11282 verifyFormat("class C : public D {\n" 11283 " SomeClass SC{2};\n" 11284 "};"); 11285 verifyFormat("class C : public A {\n" 11286 " class D : public B {\n" 11287 " void f() { int i{2}; }\n" 11288 " };\n" 11289 "};"); 11290 verifyFormat("#define A {a, a},"); 11291 11292 // Avoid breaking between equal sign and opening brace 11293 FormatStyle AvoidBreakingFirstArgument = getLLVMStyle(); 11294 AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200; 11295 verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n" 11296 " {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n" 11297 " {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n" 11298 " {\"ccccccccccccccccccccc\", 2}};", 11299 AvoidBreakingFirstArgument); 11300 11301 // Binpacking only if there is no trailing comma 11302 verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n" 11303 " cccccccccc, dddddddddd};", 11304 getLLVMStyleWithColumns(50)); 11305 verifyFormat("const Aaaaaa aaaaa = {\n" 11306 " aaaaaaaaaaa,\n" 11307 " bbbbbbbbbbb,\n" 11308 " ccccccccccc,\n" 11309 " ddddddddddd,\n" 11310 "};", 11311 getLLVMStyleWithColumns(50)); 11312 11313 // Cases where distinguising braced lists and blocks is hard. 11314 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);"); 11315 verifyFormat("void f() {\n" 11316 " return; // comment\n" 11317 "}\n" 11318 "SomeType t;"); 11319 verifyFormat("void f() {\n" 11320 " if (a) {\n" 11321 " f();\n" 11322 " }\n" 11323 "}\n" 11324 "SomeType t;"); 11325 11326 // In combination with BinPackArguments = false. 11327 FormatStyle NoBinPacking = getLLVMStyle(); 11328 NoBinPacking.BinPackArguments = false; 11329 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 11330 " bbbbb,\n" 11331 " ccccc,\n" 11332 " ddddd,\n" 11333 " eeeee,\n" 11334 " ffffff,\n" 11335 " ggggg,\n" 11336 " hhhhhh,\n" 11337 " iiiiii,\n" 11338 " jjjjjj,\n" 11339 " kkkkkk};", 11340 NoBinPacking); 11341 verifyFormat("const Aaaaaa aaaaa = {\n" 11342 " aaaaa,\n" 11343 " bbbbb,\n" 11344 " ccccc,\n" 11345 " ddddd,\n" 11346 " eeeee,\n" 11347 " ffffff,\n" 11348 " ggggg,\n" 11349 " hhhhhh,\n" 11350 " iiiiii,\n" 11351 " jjjjjj,\n" 11352 " kkkkkk,\n" 11353 "};", 11354 NoBinPacking); 11355 verifyFormat( 11356 "const Aaaaaa aaaaa = {\n" 11357 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 11358 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 11359 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 11360 "};", 11361 NoBinPacking); 11362 11363 NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 11364 EXPECT_EQ("static uint8 CddDp83848Reg[] = {\n" 11365 " CDDDP83848_BMCR_REGISTER,\n" 11366 " CDDDP83848_BMSR_REGISTER,\n" 11367 " CDDDP83848_RBR_REGISTER};", 11368 format("static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n" 11369 " CDDDP83848_BMSR_REGISTER,\n" 11370 " CDDDP83848_RBR_REGISTER};", 11371 NoBinPacking)); 11372 11373 // FIXME: The alignment of these trailing comments might be bad. Then again, 11374 // this might be utterly useless in real code. 11375 verifyFormat("Constructor::Constructor()\n" 11376 " : some_value{ //\n" 11377 " aaaaaaa, //\n" 11378 " bbbbbbb} {}"); 11379 11380 // In braced lists, the first comment is always assumed to belong to the 11381 // first element. Thus, it can be moved to the next or previous line as 11382 // appropriate. 11383 EXPECT_EQ("function({// First element:\n" 11384 " 1,\n" 11385 " // Second element:\n" 11386 " 2});", 11387 format("function({\n" 11388 " // First element:\n" 11389 " 1,\n" 11390 " // Second element:\n" 11391 " 2});")); 11392 EXPECT_EQ("std::vector<int> MyNumbers{\n" 11393 " // First element:\n" 11394 " 1,\n" 11395 " // Second element:\n" 11396 " 2};", 11397 format("std::vector<int> MyNumbers{// First element:\n" 11398 " 1,\n" 11399 " // Second element:\n" 11400 " 2};", 11401 getLLVMStyleWithColumns(30))); 11402 // A trailing comma should still lead to an enforced line break and no 11403 // binpacking. 11404 EXPECT_EQ("vector<int> SomeVector = {\n" 11405 " // aaa\n" 11406 " 1,\n" 11407 " 2,\n" 11408 "};", 11409 format("vector<int> SomeVector = { // aaa\n" 11410 " 1, 2, };")); 11411 11412 // C++11 brace initializer list l-braces should not be treated any differently 11413 // when breaking before lambda bodies is enabled 11414 FormatStyle BreakBeforeLambdaBody = getLLVMStyle(); 11415 BreakBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 11416 BreakBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 11417 BreakBeforeLambdaBody.AlwaysBreakBeforeMultilineStrings = true; 11418 verifyFormat( 11419 "std::runtime_error{\n" 11420 " \"Long string which will force a break onto the next line...\"};", 11421 BreakBeforeLambdaBody); 11422 11423 FormatStyle ExtraSpaces = getLLVMStyle(); 11424 ExtraSpaces.Cpp11BracedListStyle = false; 11425 ExtraSpaces.ColumnLimit = 75; 11426 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 11427 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 11428 verifyFormat("f({ 1, 2 });", ExtraSpaces); 11429 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 11430 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 11431 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 11432 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 11433 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 11434 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 11435 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 11436 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 11437 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 11438 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 11439 verifyFormat("class Class {\n" 11440 " T member = { arg1, arg2 };\n" 11441 "};", 11442 ExtraSpaces); 11443 verifyFormat( 11444 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11445 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 11446 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 11447 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 11448 ExtraSpaces); 11449 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 11450 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 11451 ExtraSpaces); 11452 verifyFormat( 11453 "someFunction(OtherParam,\n" 11454 " BracedList{ // comment 1 (Forcing interesting break)\n" 11455 " param1, param2,\n" 11456 " // comment 2\n" 11457 " param3, param4 });", 11458 ExtraSpaces); 11459 verifyFormat( 11460 "std::this_thread::sleep_for(\n" 11461 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 11462 ExtraSpaces); 11463 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n" 11464 " aaaaaaa,\n" 11465 " aaaaaaaaaa,\n" 11466 " aaaaa,\n" 11467 " aaaaaaaaaaaaaaa,\n" 11468 " aaa,\n" 11469 " aaaaaaaaaa,\n" 11470 " a,\n" 11471 " aaaaaaaaaaaaaaaaaaaaa,\n" 11472 " aaaaaaaaaaaa,\n" 11473 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 11474 " aaaaaaa,\n" 11475 " a};"); 11476 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 11477 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces); 11478 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces); 11479 11480 // Avoid breaking between initializer/equal sign and opening brace 11481 ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200; 11482 verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n" 11483 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 11484 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 11485 " { \"ccccccccccccccccccccc\", 2 }\n" 11486 "};", 11487 ExtraSpaces); 11488 verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n" 11489 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 11490 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 11491 " { \"ccccccccccccccccccccc\", 2 }\n" 11492 "};", 11493 ExtraSpaces); 11494 11495 FormatStyle SpaceBeforeBrace = getLLVMStyle(); 11496 SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true; 11497 verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace); 11498 verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace); 11499 11500 FormatStyle SpaceBetweenBraces = getLLVMStyle(); 11501 SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always; 11502 SpaceBetweenBraces.SpacesInParentheses = true; 11503 SpaceBetweenBraces.SpacesInSquareBrackets = true; 11504 verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces); 11505 verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces); 11506 verifyFormat("vector< int > x{ // comment 1\n" 11507 " 1, 2, 3, 4 };", 11508 SpaceBetweenBraces); 11509 SpaceBetweenBraces.ColumnLimit = 20; 11510 EXPECT_EQ("vector< int > x{\n" 11511 " 1, 2, 3, 4 };", 11512 format("vector<int>x{1,2,3,4};", SpaceBetweenBraces)); 11513 SpaceBetweenBraces.ColumnLimit = 24; 11514 EXPECT_EQ("vector< int > x{ 1, 2,\n" 11515 " 3, 4 };", 11516 format("vector<int>x{1,2,3,4};", SpaceBetweenBraces)); 11517 EXPECT_EQ("vector< int > x{\n" 11518 " 1,\n" 11519 " 2,\n" 11520 " 3,\n" 11521 " 4,\n" 11522 "};", 11523 format("vector<int>x{1,2,3,4,};", SpaceBetweenBraces)); 11524 verifyFormat("vector< int > x{};", SpaceBetweenBraces); 11525 SpaceBetweenBraces.SpaceInEmptyParentheses = true; 11526 verifyFormat("vector< int > x{ };", SpaceBetweenBraces); 11527 } 11528 11529 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 11530 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11531 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11532 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11533 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11534 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11535 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 11536 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 11537 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11538 " 1, 22, 333, 4444, 55555, //\n" 11539 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11540 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 11541 verifyFormat( 11542 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11543 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11544 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 11545 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 11546 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 11547 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 11548 " 7777777};"); 11549 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 11550 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 11551 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 11552 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 11553 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 11554 " // Separating comment.\n" 11555 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 11556 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 11557 " // Leading comment\n" 11558 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 11559 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 11560 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 11561 " 1, 1, 1, 1};", 11562 getLLVMStyleWithColumns(39)); 11563 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 11564 " 1, 1, 1, 1};", 11565 getLLVMStyleWithColumns(38)); 11566 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 11567 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 11568 getLLVMStyleWithColumns(43)); 11569 verifyFormat( 11570 "static unsigned SomeValues[10][3] = {\n" 11571 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 11572 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 11573 verifyFormat("static auto fields = new vector<string>{\n" 11574 " \"aaaaaaaaaaaaa\",\n" 11575 " \"aaaaaaaaaaaaa\",\n" 11576 " \"aaaaaaaaaaaa\",\n" 11577 " \"aaaaaaaaaaaaaa\",\n" 11578 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 11579 " \"aaaaaaaaaaaa\",\n" 11580 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 11581 "};"); 11582 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 11583 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 11584 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 11585 " 3, cccccccccccccccccccccc};", 11586 getLLVMStyleWithColumns(60)); 11587 11588 // Trailing commas. 11589 verifyFormat("vector<int> x = {\n" 11590 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 11591 "};", 11592 getLLVMStyleWithColumns(39)); 11593 verifyFormat("vector<int> x = {\n" 11594 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 11595 "};", 11596 getLLVMStyleWithColumns(39)); 11597 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 11598 " 1, 1, 1, 1,\n" 11599 " /**/ /**/};", 11600 getLLVMStyleWithColumns(39)); 11601 11602 // Trailing comment in the first line. 11603 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 11604 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 11605 " 111111111, 222222222, 3333333333, 444444444, //\n" 11606 " 11111111, 22222222, 333333333, 44444444};"); 11607 // Trailing comment in the last line. 11608 verifyFormat("int aaaaa[] = {\n" 11609 " 1, 2, 3, // comment\n" 11610 " 4, 5, 6 // comment\n" 11611 "};"); 11612 11613 // With nested lists, we should either format one item per line or all nested 11614 // lists one on line. 11615 // FIXME: For some nested lists, we can do better. 11616 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 11617 " {aaaaaaaaaaaaaaaaaaa},\n" 11618 " {aaaaaaaaaaaaaaaaaaaaa},\n" 11619 " {aaaaaaaaaaaaaaaaa}};", 11620 getLLVMStyleWithColumns(60)); 11621 verifyFormat( 11622 "SomeStruct my_struct_array = {\n" 11623 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 11624 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 11625 " {aaa, aaa},\n" 11626 " {aaa, aaa},\n" 11627 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 11628 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 11629 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 11630 11631 // No column layout should be used here. 11632 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 11633 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 11634 11635 verifyNoCrash("a<,"); 11636 11637 // No braced initializer here. 11638 verifyFormat("void f() {\n" 11639 " struct Dummy {};\n" 11640 " f(v);\n" 11641 "}"); 11642 11643 // Long lists should be formatted in columns even if they are nested. 11644 verifyFormat( 11645 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11646 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11647 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11648 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11649 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11650 " 1, 22, 333, 4444, 55555, 666666, 7777777});"); 11651 11652 // Allow "single-column" layout even if that violates the column limit. There 11653 // isn't going to be a better way. 11654 verifyFormat("std::vector<int> a = {\n" 11655 " aaaaaaaa,\n" 11656 " aaaaaaaa,\n" 11657 " aaaaaaaa,\n" 11658 " aaaaaaaa,\n" 11659 " aaaaaaaaaa,\n" 11660 " aaaaaaaa,\n" 11661 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};", 11662 getLLVMStyleWithColumns(30)); 11663 verifyFormat("vector<int> aaaa = {\n" 11664 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11665 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11666 " aaaaaa.aaaaaaa,\n" 11667 " aaaaaa.aaaaaaa,\n" 11668 " aaaaaa.aaaaaaa,\n" 11669 " aaaaaa.aaaaaaa,\n" 11670 "};"); 11671 11672 // Don't create hanging lists. 11673 verifyFormat("someFunction(Param, {List1, List2,\n" 11674 " List3});", 11675 getLLVMStyleWithColumns(35)); 11676 verifyFormat("someFunction(Param, Param,\n" 11677 " {List1, List2,\n" 11678 " List3});", 11679 getLLVMStyleWithColumns(35)); 11680 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n" 11681 " aaaaaaaaaaaaaaaaaaaaaaa);"); 11682 } 11683 11684 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 11685 FormatStyle DoNotMerge = getLLVMStyle(); 11686 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 11687 11688 verifyFormat("void f() { return 42; }"); 11689 verifyFormat("void f() {\n" 11690 " return 42;\n" 11691 "}", 11692 DoNotMerge); 11693 verifyFormat("void f() {\n" 11694 " // Comment\n" 11695 "}"); 11696 verifyFormat("{\n" 11697 "#error {\n" 11698 " int a;\n" 11699 "}"); 11700 verifyFormat("{\n" 11701 " int a;\n" 11702 "#error {\n" 11703 "}"); 11704 verifyFormat("void f() {} // comment"); 11705 verifyFormat("void f() { int a; } // comment"); 11706 verifyFormat("void f() {\n" 11707 "} // comment", 11708 DoNotMerge); 11709 verifyFormat("void f() {\n" 11710 " int a;\n" 11711 "} // comment", 11712 DoNotMerge); 11713 verifyFormat("void f() {\n" 11714 "} // comment", 11715 getLLVMStyleWithColumns(15)); 11716 11717 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 11718 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 11719 11720 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 11721 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 11722 verifyFormat("class C {\n" 11723 " C()\n" 11724 " : iiiiiiii(nullptr),\n" 11725 " kkkkkkk(nullptr),\n" 11726 " mmmmmmm(nullptr),\n" 11727 " nnnnnnn(nullptr) {}\n" 11728 "};", 11729 getGoogleStyle()); 11730 11731 FormatStyle NoColumnLimit = getLLVMStyle(); 11732 NoColumnLimit.ColumnLimit = 0; 11733 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 11734 EXPECT_EQ("class C {\n" 11735 " A() : b(0) {}\n" 11736 "};", 11737 format("class C{A():b(0){}};", NoColumnLimit)); 11738 EXPECT_EQ("A()\n" 11739 " : b(0) {\n" 11740 "}", 11741 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 11742 11743 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 11744 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 11745 FormatStyle::SFS_None; 11746 EXPECT_EQ("A()\n" 11747 " : b(0) {\n" 11748 "}", 11749 format("A():b(0){}", DoNotMergeNoColumnLimit)); 11750 EXPECT_EQ("A()\n" 11751 " : b(0) {\n" 11752 "}", 11753 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 11754 11755 verifyFormat("#define A \\\n" 11756 " void f() { \\\n" 11757 " int i; \\\n" 11758 " }", 11759 getLLVMStyleWithColumns(20)); 11760 verifyFormat("#define A \\\n" 11761 " void f() { int i; }", 11762 getLLVMStyleWithColumns(21)); 11763 verifyFormat("#define A \\\n" 11764 " void f() { \\\n" 11765 " int i; \\\n" 11766 " } \\\n" 11767 " int j;", 11768 getLLVMStyleWithColumns(22)); 11769 verifyFormat("#define A \\\n" 11770 " void f() { int i; } \\\n" 11771 " int j;", 11772 getLLVMStyleWithColumns(23)); 11773 } 11774 11775 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) { 11776 FormatStyle MergeEmptyOnly = getLLVMStyle(); 11777 MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 11778 verifyFormat("class C {\n" 11779 " int f() {}\n" 11780 "};", 11781 MergeEmptyOnly); 11782 verifyFormat("class C {\n" 11783 " int f() {\n" 11784 " return 42;\n" 11785 " }\n" 11786 "};", 11787 MergeEmptyOnly); 11788 verifyFormat("int f() {}", MergeEmptyOnly); 11789 verifyFormat("int f() {\n" 11790 " return 42;\n" 11791 "}", 11792 MergeEmptyOnly); 11793 11794 // Also verify behavior when BraceWrapping.AfterFunction = true 11795 MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 11796 MergeEmptyOnly.BraceWrapping.AfterFunction = true; 11797 verifyFormat("int f() {}", MergeEmptyOnly); 11798 verifyFormat("class C {\n" 11799 " int f() {}\n" 11800 "};", 11801 MergeEmptyOnly); 11802 } 11803 11804 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 11805 FormatStyle MergeInlineOnly = getLLVMStyle(); 11806 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 11807 verifyFormat("class C {\n" 11808 " int f() { return 42; }\n" 11809 "};", 11810 MergeInlineOnly); 11811 verifyFormat("int f() {\n" 11812 " return 42;\n" 11813 "}", 11814 MergeInlineOnly); 11815 11816 // SFS_Inline implies SFS_Empty 11817 verifyFormat("class C {\n" 11818 " int f() {}\n" 11819 "};", 11820 MergeInlineOnly); 11821 verifyFormat("int f() {}", MergeInlineOnly); 11822 11823 // Also verify behavior when BraceWrapping.AfterFunction = true 11824 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 11825 MergeInlineOnly.BraceWrapping.AfterFunction = true; 11826 verifyFormat("class C {\n" 11827 " int f() { return 42; }\n" 11828 "};", 11829 MergeInlineOnly); 11830 verifyFormat("int f()\n" 11831 "{\n" 11832 " return 42;\n" 11833 "}", 11834 MergeInlineOnly); 11835 11836 // SFS_Inline implies SFS_Empty 11837 verifyFormat("int f() {}", MergeInlineOnly); 11838 verifyFormat("class C {\n" 11839 " int f() {}\n" 11840 "};", 11841 MergeInlineOnly); 11842 } 11843 11844 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) { 11845 FormatStyle MergeInlineOnly = getLLVMStyle(); 11846 MergeInlineOnly.AllowShortFunctionsOnASingleLine = 11847 FormatStyle::SFS_InlineOnly; 11848 verifyFormat("class C {\n" 11849 " int f() { return 42; }\n" 11850 "};", 11851 MergeInlineOnly); 11852 verifyFormat("int f() {\n" 11853 " return 42;\n" 11854 "}", 11855 MergeInlineOnly); 11856 11857 // SFS_InlineOnly does not imply SFS_Empty 11858 verifyFormat("class C {\n" 11859 " int f() {}\n" 11860 "};", 11861 MergeInlineOnly); 11862 verifyFormat("int f() {\n" 11863 "}", 11864 MergeInlineOnly); 11865 11866 // Also verify behavior when BraceWrapping.AfterFunction = true 11867 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 11868 MergeInlineOnly.BraceWrapping.AfterFunction = true; 11869 verifyFormat("class C {\n" 11870 " int f() { return 42; }\n" 11871 "};", 11872 MergeInlineOnly); 11873 verifyFormat("int f()\n" 11874 "{\n" 11875 " return 42;\n" 11876 "}", 11877 MergeInlineOnly); 11878 11879 // SFS_InlineOnly does not imply SFS_Empty 11880 verifyFormat("int f()\n" 11881 "{\n" 11882 "}", 11883 MergeInlineOnly); 11884 verifyFormat("class C {\n" 11885 " int f() {}\n" 11886 "};", 11887 MergeInlineOnly); 11888 } 11889 11890 TEST_F(FormatTest, SplitEmptyFunction) { 11891 FormatStyle Style = getLLVMStyle(); 11892 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 11893 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 11894 Style.BraceWrapping.AfterFunction = true; 11895 Style.BraceWrapping.SplitEmptyFunction = false; 11896 Style.ColumnLimit = 40; 11897 11898 verifyFormat("int f()\n" 11899 "{}", 11900 Style); 11901 verifyFormat("int f()\n" 11902 "{\n" 11903 " return 42;\n" 11904 "}", 11905 Style); 11906 verifyFormat("int f()\n" 11907 "{\n" 11908 " // some comment\n" 11909 "}", 11910 Style); 11911 11912 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 11913 verifyFormat("int f() {}", Style); 11914 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 11915 "{}", 11916 Style); 11917 verifyFormat("int f()\n" 11918 "{\n" 11919 " return 0;\n" 11920 "}", 11921 Style); 11922 11923 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 11924 verifyFormat("class Foo {\n" 11925 " int f() {}\n" 11926 "};\n", 11927 Style); 11928 verifyFormat("class Foo {\n" 11929 " int f() { return 0; }\n" 11930 "};\n", 11931 Style); 11932 verifyFormat("class Foo {\n" 11933 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 11934 " {}\n" 11935 "};\n", 11936 Style); 11937 verifyFormat("class Foo {\n" 11938 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 11939 " {\n" 11940 " return 0;\n" 11941 " }\n" 11942 "};\n", 11943 Style); 11944 11945 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 11946 verifyFormat("int f() {}", Style); 11947 verifyFormat("int f() { return 0; }", Style); 11948 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 11949 "{}", 11950 Style); 11951 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 11952 "{\n" 11953 " return 0;\n" 11954 "}", 11955 Style); 11956 } 11957 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) { 11958 FormatStyle Style = getLLVMStyle(); 11959 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 11960 verifyFormat("#ifdef A\n" 11961 "int f() {}\n" 11962 "#else\n" 11963 "int g() {}\n" 11964 "#endif", 11965 Style); 11966 } 11967 11968 TEST_F(FormatTest, SplitEmptyClass) { 11969 FormatStyle Style = getLLVMStyle(); 11970 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 11971 Style.BraceWrapping.AfterClass = true; 11972 Style.BraceWrapping.SplitEmptyRecord = false; 11973 11974 verifyFormat("class Foo\n" 11975 "{};", 11976 Style); 11977 verifyFormat("/* something */ class Foo\n" 11978 "{};", 11979 Style); 11980 verifyFormat("template <typename X> class Foo\n" 11981 "{};", 11982 Style); 11983 verifyFormat("class Foo\n" 11984 "{\n" 11985 " Foo();\n" 11986 "};", 11987 Style); 11988 verifyFormat("typedef class Foo\n" 11989 "{\n" 11990 "} Foo_t;", 11991 Style); 11992 11993 Style.BraceWrapping.SplitEmptyRecord = true; 11994 Style.BraceWrapping.AfterStruct = true; 11995 verifyFormat("class rep\n" 11996 "{\n" 11997 "};", 11998 Style); 11999 verifyFormat("struct rep\n" 12000 "{\n" 12001 "};", 12002 Style); 12003 verifyFormat("template <typename T> class rep\n" 12004 "{\n" 12005 "};", 12006 Style); 12007 verifyFormat("template <typename T> struct rep\n" 12008 "{\n" 12009 "};", 12010 Style); 12011 verifyFormat("class rep\n" 12012 "{\n" 12013 " int x;\n" 12014 "};", 12015 Style); 12016 verifyFormat("struct rep\n" 12017 "{\n" 12018 " int x;\n" 12019 "};", 12020 Style); 12021 verifyFormat("template <typename T> class rep\n" 12022 "{\n" 12023 " int x;\n" 12024 "};", 12025 Style); 12026 verifyFormat("template <typename T> struct rep\n" 12027 "{\n" 12028 " int x;\n" 12029 "};", 12030 Style); 12031 verifyFormat("template <typename T> class rep // Foo\n" 12032 "{\n" 12033 " int x;\n" 12034 "};", 12035 Style); 12036 verifyFormat("template <typename T> struct rep // Bar\n" 12037 "{\n" 12038 " int x;\n" 12039 "};", 12040 Style); 12041 12042 verifyFormat("template <typename T> class rep<T>\n" 12043 "{\n" 12044 " int x;\n" 12045 "};", 12046 Style); 12047 12048 verifyFormat("template <typename T> class rep<std::complex<T>>\n" 12049 "{\n" 12050 " int x;\n" 12051 "};", 12052 Style); 12053 verifyFormat("template <typename T> class rep<std::complex<T>>\n" 12054 "{\n" 12055 "};", 12056 Style); 12057 12058 verifyFormat("#include \"stdint.h\"\n" 12059 "namespace rep {}", 12060 Style); 12061 verifyFormat("#include <stdint.h>\n" 12062 "namespace rep {}", 12063 Style); 12064 verifyFormat("#include <stdint.h>\n" 12065 "namespace rep {}", 12066 "#include <stdint.h>\n" 12067 "namespace rep {\n" 12068 "\n" 12069 "\n" 12070 "}", 12071 Style); 12072 } 12073 12074 TEST_F(FormatTest, SplitEmptyStruct) { 12075 FormatStyle Style = getLLVMStyle(); 12076 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12077 Style.BraceWrapping.AfterStruct = true; 12078 Style.BraceWrapping.SplitEmptyRecord = false; 12079 12080 verifyFormat("struct Foo\n" 12081 "{};", 12082 Style); 12083 verifyFormat("/* something */ struct Foo\n" 12084 "{};", 12085 Style); 12086 verifyFormat("template <typename X> struct Foo\n" 12087 "{};", 12088 Style); 12089 verifyFormat("struct Foo\n" 12090 "{\n" 12091 " Foo();\n" 12092 "};", 12093 Style); 12094 verifyFormat("typedef struct Foo\n" 12095 "{\n" 12096 "} Foo_t;", 12097 Style); 12098 // typedef struct Bar {} Bar_t; 12099 } 12100 12101 TEST_F(FormatTest, SplitEmptyUnion) { 12102 FormatStyle Style = getLLVMStyle(); 12103 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12104 Style.BraceWrapping.AfterUnion = true; 12105 Style.BraceWrapping.SplitEmptyRecord = false; 12106 12107 verifyFormat("union Foo\n" 12108 "{};", 12109 Style); 12110 verifyFormat("/* something */ union Foo\n" 12111 "{};", 12112 Style); 12113 verifyFormat("union Foo\n" 12114 "{\n" 12115 " A,\n" 12116 "};", 12117 Style); 12118 verifyFormat("typedef union Foo\n" 12119 "{\n" 12120 "} Foo_t;", 12121 Style); 12122 } 12123 12124 TEST_F(FormatTest, SplitEmptyNamespace) { 12125 FormatStyle Style = getLLVMStyle(); 12126 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12127 Style.BraceWrapping.AfterNamespace = true; 12128 Style.BraceWrapping.SplitEmptyNamespace = false; 12129 12130 verifyFormat("namespace Foo\n" 12131 "{};", 12132 Style); 12133 verifyFormat("/* something */ namespace Foo\n" 12134 "{};", 12135 Style); 12136 verifyFormat("inline namespace Foo\n" 12137 "{};", 12138 Style); 12139 verifyFormat("/* something */ inline namespace Foo\n" 12140 "{};", 12141 Style); 12142 verifyFormat("export namespace Foo\n" 12143 "{};", 12144 Style); 12145 verifyFormat("namespace Foo\n" 12146 "{\n" 12147 "void Bar();\n" 12148 "};", 12149 Style); 12150 } 12151 12152 TEST_F(FormatTest, NeverMergeShortRecords) { 12153 FormatStyle Style = getLLVMStyle(); 12154 12155 verifyFormat("class Foo {\n" 12156 " Foo();\n" 12157 "};", 12158 Style); 12159 verifyFormat("typedef class Foo {\n" 12160 " Foo();\n" 12161 "} Foo_t;", 12162 Style); 12163 verifyFormat("struct Foo {\n" 12164 " Foo();\n" 12165 "};", 12166 Style); 12167 verifyFormat("typedef struct Foo {\n" 12168 " Foo();\n" 12169 "} Foo_t;", 12170 Style); 12171 verifyFormat("union Foo {\n" 12172 " A,\n" 12173 "};", 12174 Style); 12175 verifyFormat("typedef union Foo {\n" 12176 " A,\n" 12177 "} Foo_t;", 12178 Style); 12179 verifyFormat("namespace Foo {\n" 12180 "void Bar();\n" 12181 "};", 12182 Style); 12183 12184 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12185 Style.BraceWrapping.AfterClass = true; 12186 Style.BraceWrapping.AfterStruct = true; 12187 Style.BraceWrapping.AfterUnion = true; 12188 Style.BraceWrapping.AfterNamespace = true; 12189 verifyFormat("class Foo\n" 12190 "{\n" 12191 " Foo();\n" 12192 "};", 12193 Style); 12194 verifyFormat("typedef class Foo\n" 12195 "{\n" 12196 " Foo();\n" 12197 "} Foo_t;", 12198 Style); 12199 verifyFormat("struct Foo\n" 12200 "{\n" 12201 " Foo();\n" 12202 "};", 12203 Style); 12204 verifyFormat("typedef struct Foo\n" 12205 "{\n" 12206 " Foo();\n" 12207 "} Foo_t;", 12208 Style); 12209 verifyFormat("union Foo\n" 12210 "{\n" 12211 " A,\n" 12212 "};", 12213 Style); 12214 verifyFormat("typedef union Foo\n" 12215 "{\n" 12216 " A,\n" 12217 "} Foo_t;", 12218 Style); 12219 verifyFormat("namespace Foo\n" 12220 "{\n" 12221 "void Bar();\n" 12222 "};", 12223 Style); 12224 } 12225 12226 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 12227 // Elaborate type variable declarations. 12228 verifyFormat("struct foo a = {bar};\nint n;"); 12229 verifyFormat("class foo a = {bar};\nint n;"); 12230 verifyFormat("union foo a = {bar};\nint n;"); 12231 12232 // Elaborate types inside function definitions. 12233 verifyFormat("struct foo f() {}\nint n;"); 12234 verifyFormat("class foo f() {}\nint n;"); 12235 verifyFormat("union foo f() {}\nint n;"); 12236 12237 // Templates. 12238 verifyFormat("template <class X> void f() {}\nint n;"); 12239 verifyFormat("template <struct X> void f() {}\nint n;"); 12240 verifyFormat("template <union X> void f() {}\nint n;"); 12241 12242 // Actual definitions... 12243 verifyFormat("struct {\n} n;"); 12244 verifyFormat( 12245 "template <template <class T, class Y>, class Z> class X {\n} n;"); 12246 verifyFormat("union Z {\n int n;\n} x;"); 12247 verifyFormat("class MACRO Z {\n} n;"); 12248 verifyFormat("class MACRO(X) Z {\n} n;"); 12249 verifyFormat("class __attribute__(X) Z {\n} n;"); 12250 verifyFormat("class __declspec(X) Z {\n} n;"); 12251 verifyFormat("class A##B##C {\n} n;"); 12252 verifyFormat("class alignas(16) Z {\n} n;"); 12253 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 12254 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 12255 12256 // Redefinition from nested context: 12257 verifyFormat("class A::B::C {\n} n;"); 12258 12259 // Template definitions. 12260 verifyFormat( 12261 "template <typename F>\n" 12262 "Matcher(const Matcher<F> &Other,\n" 12263 " typename enable_if_c<is_base_of<F, T>::value &&\n" 12264 " !is_same<F, T>::value>::type * = 0)\n" 12265 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 12266 12267 // FIXME: This is still incorrectly handled at the formatter side. 12268 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 12269 verifyFormat("int i = SomeFunction(a<b, a> b);"); 12270 12271 // FIXME: 12272 // This now gets parsed incorrectly as class definition. 12273 // verifyFormat("class A<int> f() {\n}\nint n;"); 12274 12275 // Elaborate types where incorrectly parsing the structural element would 12276 // break the indent. 12277 verifyFormat("if (true)\n" 12278 " class X x;\n" 12279 "else\n" 12280 " f();\n"); 12281 12282 // This is simply incomplete. Formatting is not important, but must not crash. 12283 verifyFormat("class A:"); 12284 } 12285 12286 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 12287 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 12288 format("#error Leave all white!!!!! space* alone!\n")); 12289 EXPECT_EQ( 12290 "#warning Leave all white!!!!! space* alone!\n", 12291 format("#warning Leave all white!!!!! space* alone!\n")); 12292 EXPECT_EQ("#error 1", format(" # error 1")); 12293 EXPECT_EQ("#warning 1", format(" # warning 1")); 12294 } 12295 12296 TEST_F(FormatTest, FormatHashIfExpressions) { 12297 verifyFormat("#if AAAA && BBBB"); 12298 verifyFormat("#if (AAAA && BBBB)"); 12299 verifyFormat("#elif (AAAA && BBBB)"); 12300 // FIXME: Come up with a better indentation for #elif. 12301 verifyFormat( 12302 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 12303 " defined(BBBBBBBB)\n" 12304 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 12305 " defined(BBBBBBBB)\n" 12306 "#endif", 12307 getLLVMStyleWithColumns(65)); 12308 } 12309 12310 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 12311 FormatStyle AllowsMergedIf = getGoogleStyle(); 12312 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 12313 FormatStyle::SIS_WithoutElse; 12314 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 12315 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 12316 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 12317 EXPECT_EQ("if (true) return 42;", 12318 format("if (true)\nreturn 42;", AllowsMergedIf)); 12319 FormatStyle ShortMergedIf = AllowsMergedIf; 12320 ShortMergedIf.ColumnLimit = 25; 12321 verifyFormat("#define A \\\n" 12322 " if (true) return 42;", 12323 ShortMergedIf); 12324 verifyFormat("#define A \\\n" 12325 " f(); \\\n" 12326 " if (true)\n" 12327 "#define B", 12328 ShortMergedIf); 12329 verifyFormat("#define A \\\n" 12330 " f(); \\\n" 12331 " if (true)\n" 12332 "g();", 12333 ShortMergedIf); 12334 verifyFormat("{\n" 12335 "#ifdef A\n" 12336 " // Comment\n" 12337 " if (true) continue;\n" 12338 "#endif\n" 12339 " // Comment\n" 12340 " if (true) continue;\n" 12341 "}", 12342 ShortMergedIf); 12343 ShortMergedIf.ColumnLimit = 33; 12344 verifyFormat("#define A \\\n" 12345 " if constexpr (true) return 42;", 12346 ShortMergedIf); 12347 verifyFormat("#define A \\\n" 12348 " if CONSTEXPR (true) return 42;", 12349 ShortMergedIf); 12350 ShortMergedIf.ColumnLimit = 29; 12351 verifyFormat("#define A \\\n" 12352 " if (aaaaaaaaaa) return 1; \\\n" 12353 " return 2;", 12354 ShortMergedIf); 12355 ShortMergedIf.ColumnLimit = 28; 12356 verifyFormat("#define A \\\n" 12357 " if (aaaaaaaaaa) \\\n" 12358 " return 1; \\\n" 12359 " return 2;", 12360 ShortMergedIf); 12361 verifyFormat("#define A \\\n" 12362 " if constexpr (aaaaaaa) \\\n" 12363 " return 1; \\\n" 12364 " return 2;", 12365 ShortMergedIf); 12366 verifyFormat("#define A \\\n" 12367 " if CONSTEXPR (aaaaaaa) \\\n" 12368 " return 1; \\\n" 12369 " return 2;", 12370 ShortMergedIf); 12371 } 12372 12373 TEST_F(FormatTest, FormatStarDependingOnContext) { 12374 verifyFormat("void f(int *a);"); 12375 verifyFormat("void f() { f(fint * b); }"); 12376 verifyFormat("class A {\n void f(int *a);\n};"); 12377 verifyFormat("class A {\n int *a;\n};"); 12378 verifyFormat("namespace a {\n" 12379 "namespace b {\n" 12380 "class A {\n" 12381 " void f() {}\n" 12382 " int *a;\n" 12383 "};\n" 12384 "} // namespace b\n" 12385 "} // namespace a"); 12386 } 12387 12388 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 12389 verifyFormat("while"); 12390 verifyFormat("operator"); 12391 } 12392 12393 TEST_F(FormatTest, SkipsDeeplyNestedLines) { 12394 // This code would be painfully slow to format if we didn't skip it. 12395 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 12396 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12397 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12398 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12399 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12400 "A(1, 1)\n" 12401 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x 12402 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12403 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12404 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12405 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12406 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12407 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12408 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12409 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12410 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n"); 12411 // Deeply nested part is untouched, rest is formatted. 12412 EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n", 12413 format(std::string("int i;\n") + Code + "int j;\n", 12414 getLLVMStyle(), SC_ExpectIncomplete)); 12415 } 12416 12417 //===----------------------------------------------------------------------===// 12418 // Objective-C tests. 12419 //===----------------------------------------------------------------------===// 12420 12421 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 12422 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 12423 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 12424 format("-(NSUInteger)indexOfObject:(id)anObject;")); 12425 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 12426 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 12427 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 12428 format("-(NSInteger)Method3:(id)anObject;")); 12429 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 12430 format("-(NSInteger)Method4:(id)anObject;")); 12431 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 12432 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 12433 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 12434 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 12435 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 12436 "forAllCells:(BOOL)flag;", 12437 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 12438 "forAllCells:(BOOL)flag;")); 12439 12440 // Very long objectiveC method declaration. 12441 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 12442 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 12443 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 12444 " inRange:(NSRange)range\n" 12445 " outRange:(NSRange)out_range\n" 12446 " outRange1:(NSRange)out_range1\n" 12447 " outRange2:(NSRange)out_range2\n" 12448 " outRange3:(NSRange)out_range3\n" 12449 " outRange4:(NSRange)out_range4\n" 12450 " outRange5:(NSRange)out_range5\n" 12451 " outRange6:(NSRange)out_range6\n" 12452 " outRange7:(NSRange)out_range7\n" 12453 " outRange8:(NSRange)out_range8\n" 12454 " outRange9:(NSRange)out_range9;"); 12455 12456 // When the function name has to be wrapped. 12457 FormatStyle Style = getLLVMStyle(); 12458 // ObjC ignores IndentWrappedFunctionNames when wrapping methods 12459 // and always indents instead. 12460 Style.IndentWrappedFunctionNames = false; 12461 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 12462 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 12463 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 12464 "}", 12465 Style); 12466 Style.IndentWrappedFunctionNames = true; 12467 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 12468 " veryLooooooooooongName:(NSString)cccccccccccccc\n" 12469 " anotherName:(NSString)dddddddddddddd {\n" 12470 "}", 12471 Style); 12472 12473 verifyFormat("- (int)sum:(vector<int>)numbers;"); 12474 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 12475 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 12476 // protocol lists (but not for template classes): 12477 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 12478 12479 verifyFormat("- (int (*)())foo:(int (*)())f;"); 12480 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 12481 12482 // If there's no return type (very rare in practice!), LLVM and Google style 12483 // agree. 12484 verifyFormat("- foo;"); 12485 verifyFormat("- foo:(int)f;"); 12486 verifyGoogleFormat("- foo:(int)foo;"); 12487 } 12488 12489 TEST_F(FormatTest, BreaksStringLiterals) { 12490 EXPECT_EQ("\"some text \"\n" 12491 "\"other\";", 12492 format("\"some text other\";", getLLVMStyleWithColumns(12))); 12493 EXPECT_EQ("\"some text \"\n" 12494 "\"other\";", 12495 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 12496 EXPECT_EQ( 12497 "#define A \\\n" 12498 " \"some \" \\\n" 12499 " \"text \" \\\n" 12500 " \"other\";", 12501 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 12502 EXPECT_EQ( 12503 "#define A \\\n" 12504 " \"so \" \\\n" 12505 " \"text \" \\\n" 12506 " \"other\";", 12507 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 12508 12509 EXPECT_EQ("\"some text\"", 12510 format("\"some text\"", getLLVMStyleWithColumns(1))); 12511 EXPECT_EQ("\"some text\"", 12512 format("\"some text\"", getLLVMStyleWithColumns(11))); 12513 EXPECT_EQ("\"some \"\n" 12514 "\"text\"", 12515 format("\"some text\"", getLLVMStyleWithColumns(10))); 12516 EXPECT_EQ("\"some \"\n" 12517 "\"text\"", 12518 format("\"some text\"", getLLVMStyleWithColumns(7))); 12519 EXPECT_EQ("\"some\"\n" 12520 "\" tex\"\n" 12521 "\"t\"", 12522 format("\"some text\"", getLLVMStyleWithColumns(6))); 12523 EXPECT_EQ("\"some\"\n" 12524 "\" tex\"\n" 12525 "\" and\"", 12526 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 12527 EXPECT_EQ("\"some\"\n" 12528 "\"/tex\"\n" 12529 "\"/and\"", 12530 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 12531 12532 EXPECT_EQ("variable =\n" 12533 " \"long string \"\n" 12534 " \"literal\";", 12535 format("variable = \"long string literal\";", 12536 getLLVMStyleWithColumns(20))); 12537 12538 EXPECT_EQ("variable = f(\n" 12539 " \"long string \"\n" 12540 " \"literal\",\n" 12541 " short,\n" 12542 " loooooooooooooooooooong);", 12543 format("variable = f(\"long string literal\", short, " 12544 "loooooooooooooooooooong);", 12545 getLLVMStyleWithColumns(20))); 12546 12547 EXPECT_EQ( 12548 "f(g(\"long string \"\n" 12549 " \"literal\"),\n" 12550 " b);", 12551 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 12552 EXPECT_EQ("f(g(\"long string \"\n" 12553 " \"literal\",\n" 12554 " a),\n" 12555 " b);", 12556 format("f(g(\"long string literal\", a), b);", 12557 getLLVMStyleWithColumns(20))); 12558 EXPECT_EQ( 12559 "f(\"one two\".split(\n" 12560 " variable));", 12561 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 12562 EXPECT_EQ("f(\"one two three four five six \"\n" 12563 " \"seven\".split(\n" 12564 " really_looooong_variable));", 12565 format("f(\"one two three four five six seven\"." 12566 "split(really_looooong_variable));", 12567 getLLVMStyleWithColumns(33))); 12568 12569 EXPECT_EQ("f(\"some \"\n" 12570 " \"text\",\n" 12571 " other);", 12572 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 12573 12574 // Only break as a last resort. 12575 verifyFormat( 12576 "aaaaaaaaaaaaaaaaaaaa(\n" 12577 " aaaaaaaaaaaaaaaaaaaa,\n" 12578 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 12579 12580 EXPECT_EQ("\"splitmea\"\n" 12581 "\"trandomp\"\n" 12582 "\"oint\"", 12583 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 12584 12585 EXPECT_EQ("\"split/\"\n" 12586 "\"pathat/\"\n" 12587 "\"slashes\"", 12588 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 12589 12590 EXPECT_EQ("\"split/\"\n" 12591 "\"pathat/\"\n" 12592 "\"slashes\"", 12593 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 12594 EXPECT_EQ("\"split at \"\n" 12595 "\"spaces/at/\"\n" 12596 "\"slashes.at.any$\"\n" 12597 "\"non-alphanumeric%\"\n" 12598 "\"1111111111characte\"\n" 12599 "\"rs\"", 12600 format("\"split at " 12601 "spaces/at/" 12602 "slashes.at." 12603 "any$non-" 12604 "alphanumeric%" 12605 "1111111111characte" 12606 "rs\"", 12607 getLLVMStyleWithColumns(20))); 12608 12609 // Verify that splitting the strings understands 12610 // Style::AlwaysBreakBeforeMultilineStrings. 12611 EXPECT_EQ("aaaaaaaaaaaa(\n" 12612 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 12613 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 12614 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 12615 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 12616 "aaaaaaaaaaaaaaaaaaaaaa\");", 12617 getGoogleStyle())); 12618 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 12619 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 12620 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 12621 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 12622 "aaaaaaaaaaaaaaaaaaaaaa\";", 12623 getGoogleStyle())); 12624 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 12625 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 12626 format("llvm::outs() << " 12627 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 12628 "aaaaaaaaaaaaaaaaaaa\";")); 12629 EXPECT_EQ("ffff(\n" 12630 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 12631 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 12632 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 12633 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 12634 getGoogleStyle())); 12635 12636 FormatStyle Style = getLLVMStyleWithColumns(12); 12637 Style.BreakStringLiterals = false; 12638 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 12639 12640 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 12641 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 12642 EXPECT_EQ("#define A \\\n" 12643 " \"some \" \\\n" 12644 " \"text \" \\\n" 12645 " \"other\";", 12646 format("#define A \"some text other\";", AlignLeft)); 12647 } 12648 12649 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) { 12650 EXPECT_EQ("C a = \"some more \"\n" 12651 " \"text\";", 12652 format("C a = \"some more text\";", getLLVMStyleWithColumns(18))); 12653 } 12654 12655 TEST_F(FormatTest, FullyRemoveEmptyLines) { 12656 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 12657 NoEmptyLines.MaxEmptyLinesToKeep = 0; 12658 EXPECT_EQ("int i = a(b());", 12659 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 12660 } 12661 12662 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 12663 EXPECT_EQ( 12664 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 12665 "(\n" 12666 " \"x\t\");", 12667 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 12668 "aaaaaaa(" 12669 "\"x\t\");")); 12670 } 12671 12672 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 12673 EXPECT_EQ( 12674 "u8\"utf8 string \"\n" 12675 "u8\"literal\";", 12676 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 12677 EXPECT_EQ( 12678 "u\"utf16 string \"\n" 12679 "u\"literal\";", 12680 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 12681 EXPECT_EQ( 12682 "U\"utf32 string \"\n" 12683 "U\"literal\";", 12684 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 12685 EXPECT_EQ("L\"wide string \"\n" 12686 "L\"literal\";", 12687 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 12688 EXPECT_EQ("@\"NSString \"\n" 12689 "@\"literal\";", 12690 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 12691 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26)); 12692 12693 // This input makes clang-format try to split the incomplete unicode escape 12694 // sequence, which used to lead to a crasher. 12695 verifyNoCrash( 12696 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 12697 getLLVMStyleWithColumns(60)); 12698 } 12699 12700 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 12701 FormatStyle Style = getGoogleStyleWithColumns(15); 12702 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 12703 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 12704 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 12705 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 12706 EXPECT_EQ("u8R\"x(raw literal)x\";", 12707 format("u8R\"x(raw literal)x\";", Style)); 12708 } 12709 12710 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 12711 FormatStyle Style = getLLVMStyleWithColumns(20); 12712 EXPECT_EQ( 12713 "_T(\"aaaaaaaaaaaaaa\")\n" 12714 "_T(\"aaaaaaaaaaaaaa\")\n" 12715 "_T(\"aaaaaaaaaaaa\")", 12716 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 12717 EXPECT_EQ("f(x,\n" 12718 " _T(\"aaaaaaaaaaaa\")\n" 12719 " _T(\"aaa\"),\n" 12720 " z);", 12721 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 12722 12723 // FIXME: Handle embedded spaces in one iteration. 12724 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 12725 // "_T(\"aaaaaaaaaaaaa\")\n" 12726 // "_T(\"aaaaaaaaaaaaa\")\n" 12727 // "_T(\"a\")", 12728 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 12729 // getLLVMStyleWithColumns(20))); 12730 EXPECT_EQ( 12731 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 12732 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 12733 EXPECT_EQ("f(\n" 12734 "#if !TEST\n" 12735 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 12736 "#endif\n" 12737 ");", 12738 format("f(\n" 12739 "#if !TEST\n" 12740 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 12741 "#endif\n" 12742 ");")); 12743 EXPECT_EQ("f(\n" 12744 "\n" 12745 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 12746 format("f(\n" 12747 "\n" 12748 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 12749 } 12750 12751 TEST_F(FormatTest, BreaksStringLiteralOperands) { 12752 // In a function call with two operands, the second can be broken with no line 12753 // break before it. 12754 EXPECT_EQ( 12755 "func(a, \"long long \"\n" 12756 " \"long long\");", 12757 format("func(a, \"long long long long\");", getLLVMStyleWithColumns(24))); 12758 // In a function call with three operands, the second must be broken with a 12759 // line break before it. 12760 EXPECT_EQ("func(a,\n" 12761 " \"long long long \"\n" 12762 " \"long\",\n" 12763 " c);", 12764 format("func(a, \"long long long long\", c);", 12765 getLLVMStyleWithColumns(24))); 12766 // In a function call with three operands, the third must be broken with a 12767 // line break before it. 12768 EXPECT_EQ("func(a, b,\n" 12769 " \"long long long \"\n" 12770 " \"long\");", 12771 format("func(a, b, \"long long long long\");", 12772 getLLVMStyleWithColumns(24))); 12773 // In a function call with three operands, both the second and the third must 12774 // be broken with a line break before them. 12775 EXPECT_EQ("func(a,\n" 12776 " \"long long long \"\n" 12777 " \"long\",\n" 12778 " \"long long long \"\n" 12779 " \"long\");", 12780 format("func(a, \"long long long long\", \"long long long long\");", 12781 getLLVMStyleWithColumns(24))); 12782 // In a chain of << with two operands, the second can be broken with no line 12783 // break before it. 12784 EXPECT_EQ("a << \"line line \"\n" 12785 " \"line\";", 12786 format("a << \"line line line\";", getLLVMStyleWithColumns(20))); 12787 // In a chain of << with three operands, the second can be broken with no line 12788 // break before it. 12789 EXPECT_EQ( 12790 "abcde << \"line \"\n" 12791 " \"line line\"\n" 12792 " << c;", 12793 format("abcde << \"line line line\" << c;", getLLVMStyleWithColumns(20))); 12794 // In a chain of << with three operands, the third must be broken with a line 12795 // break before it. 12796 EXPECT_EQ( 12797 "a << b\n" 12798 " << \"line line \"\n" 12799 " \"line\";", 12800 format("a << b << \"line line line\";", getLLVMStyleWithColumns(20))); 12801 // In a chain of << with three operands, the second can be broken with no line 12802 // break before it and the third must be broken with a line break before it. 12803 EXPECT_EQ("abcd << \"line line \"\n" 12804 " \"line\"\n" 12805 " << \"line line \"\n" 12806 " \"line\";", 12807 format("abcd << \"line line line\" << \"line line line\";", 12808 getLLVMStyleWithColumns(20))); 12809 // In a chain of binary operators with two operands, the second can be broken 12810 // with no line break before it. 12811 EXPECT_EQ( 12812 "abcd + \"line line \"\n" 12813 " \"line line\";", 12814 format("abcd + \"line line line line\";", getLLVMStyleWithColumns(20))); 12815 // In a chain of binary operators with three operands, the second must be 12816 // broken with a line break before it. 12817 EXPECT_EQ("abcd +\n" 12818 " \"line line \"\n" 12819 " \"line line\" +\n" 12820 " e;", 12821 format("abcd + \"line line line line\" + e;", 12822 getLLVMStyleWithColumns(20))); 12823 // In a function call with two operands, with AlignAfterOpenBracket enabled, 12824 // the first must be broken with a line break before it. 12825 FormatStyle Style = getLLVMStyleWithColumns(25); 12826 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 12827 EXPECT_EQ("someFunction(\n" 12828 " \"long long long \"\n" 12829 " \"long\",\n" 12830 " a);", 12831 format("someFunction(\"long long long long\", a);", Style)); 12832 } 12833 12834 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 12835 EXPECT_EQ( 12836 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 12837 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 12838 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 12839 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 12840 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 12841 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 12842 } 12843 12844 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 12845 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 12846 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 12847 EXPECT_EQ("fffffffffff(g(R\"x(\n" 12848 "multiline raw string literal xxxxxxxxxxxxxx\n" 12849 ")x\",\n" 12850 " a),\n" 12851 " b);", 12852 format("fffffffffff(g(R\"x(\n" 12853 "multiline raw string literal xxxxxxxxxxxxxx\n" 12854 ")x\", a), b);", 12855 getGoogleStyleWithColumns(20))); 12856 EXPECT_EQ("fffffffffff(\n" 12857 " g(R\"x(qqq\n" 12858 "multiline raw string literal xxxxxxxxxxxxxx\n" 12859 ")x\",\n" 12860 " a),\n" 12861 " b);", 12862 format("fffffffffff(g(R\"x(qqq\n" 12863 "multiline raw string literal xxxxxxxxxxxxxx\n" 12864 ")x\", a), b);", 12865 getGoogleStyleWithColumns(20))); 12866 12867 EXPECT_EQ("fffffffffff(R\"x(\n" 12868 "multiline raw string literal xxxxxxxxxxxxxx\n" 12869 ")x\");", 12870 format("fffffffffff(R\"x(\n" 12871 "multiline raw string literal xxxxxxxxxxxxxx\n" 12872 ")x\");", 12873 getGoogleStyleWithColumns(20))); 12874 EXPECT_EQ("fffffffffff(R\"x(\n" 12875 "multiline raw string literal xxxxxxxxxxxxxx\n" 12876 ")x\" + bbbbbb);", 12877 format("fffffffffff(R\"x(\n" 12878 "multiline raw string literal xxxxxxxxxxxxxx\n" 12879 ")x\" + bbbbbb);", 12880 getGoogleStyleWithColumns(20))); 12881 EXPECT_EQ("fffffffffff(\n" 12882 " R\"x(\n" 12883 "multiline raw string literal xxxxxxxxxxxxxx\n" 12884 ")x\" +\n" 12885 " bbbbbb);", 12886 format("fffffffffff(\n" 12887 " R\"x(\n" 12888 "multiline raw string literal xxxxxxxxxxxxxx\n" 12889 ")x\" + bbbbbb);", 12890 getGoogleStyleWithColumns(20))); 12891 EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);", 12892 format("fffffffffff(\n" 12893 " R\"(single line raw string)\" + bbbbbb);")); 12894 } 12895 12896 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 12897 verifyFormat("string a = \"unterminated;"); 12898 EXPECT_EQ("function(\"unterminated,\n" 12899 " OtherParameter);", 12900 format("function( \"unterminated,\n" 12901 " OtherParameter);")); 12902 } 12903 12904 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 12905 FormatStyle Style = getLLVMStyle(); 12906 Style.Standard = FormatStyle::LS_Cpp03; 12907 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 12908 format("#define x(_a) printf(\"foo\"_a);", Style)); 12909 } 12910 12911 TEST_F(FormatTest, CppLexVersion) { 12912 FormatStyle Style = getLLVMStyle(); 12913 // Formatting of x * y differs if x is a type. 12914 verifyFormat("void foo() { MACRO(a * b); }", Style); 12915 verifyFormat("void foo() { MACRO(int *b); }", Style); 12916 12917 // LLVM style uses latest lexer. 12918 verifyFormat("void foo() { MACRO(char8_t *b); }", Style); 12919 Style.Standard = FormatStyle::LS_Cpp17; 12920 // But in c++17, char8_t isn't a keyword. 12921 verifyFormat("void foo() { MACRO(char8_t * b); }", Style); 12922 } 12923 12924 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 12925 12926 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 12927 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 12928 " \"ddeeefff\");", 12929 format("someFunction(\"aaabbbcccdddeeefff\");", 12930 getLLVMStyleWithColumns(25))); 12931 EXPECT_EQ("someFunction1234567890(\n" 12932 " \"aaabbbcccdddeeefff\");", 12933 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 12934 getLLVMStyleWithColumns(26))); 12935 EXPECT_EQ("someFunction1234567890(\n" 12936 " \"aaabbbcccdddeeeff\"\n" 12937 " \"f\");", 12938 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 12939 getLLVMStyleWithColumns(25))); 12940 EXPECT_EQ("someFunction1234567890(\n" 12941 " \"aaabbbcccdddeeeff\"\n" 12942 " \"f\");", 12943 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 12944 getLLVMStyleWithColumns(24))); 12945 EXPECT_EQ("someFunction(\n" 12946 " \"aaabbbcc ddde \"\n" 12947 " \"efff\");", 12948 format("someFunction(\"aaabbbcc ddde efff\");", 12949 getLLVMStyleWithColumns(25))); 12950 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 12951 " \"ddeeefff\");", 12952 format("someFunction(\"aaabbbccc ddeeefff\");", 12953 getLLVMStyleWithColumns(25))); 12954 EXPECT_EQ("someFunction1234567890(\n" 12955 " \"aaabb \"\n" 12956 " \"cccdddeeefff\");", 12957 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 12958 getLLVMStyleWithColumns(25))); 12959 EXPECT_EQ("#define A \\\n" 12960 " string s = \\\n" 12961 " \"123456789\" \\\n" 12962 " \"0\"; \\\n" 12963 " int i;", 12964 format("#define A string s = \"1234567890\"; int i;", 12965 getLLVMStyleWithColumns(20))); 12966 EXPECT_EQ("someFunction(\n" 12967 " \"aaabbbcc \"\n" 12968 " \"dddeeefff\");", 12969 format("someFunction(\"aaabbbcc dddeeefff\");", 12970 getLLVMStyleWithColumns(25))); 12971 } 12972 12973 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 12974 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 12975 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 12976 EXPECT_EQ("\"test\"\n" 12977 "\"\\n\"", 12978 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 12979 EXPECT_EQ("\"tes\\\\\"\n" 12980 "\"n\"", 12981 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 12982 EXPECT_EQ("\"\\\\\\\\\"\n" 12983 "\"\\n\"", 12984 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 12985 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 12986 EXPECT_EQ("\"\\uff01\"\n" 12987 "\"test\"", 12988 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 12989 EXPECT_EQ("\"\\Uff01ff02\"", 12990 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 12991 EXPECT_EQ("\"\\x000000000001\"\n" 12992 "\"next\"", 12993 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 12994 EXPECT_EQ("\"\\x000000000001next\"", 12995 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 12996 EXPECT_EQ("\"\\x000000000001\"", 12997 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 12998 EXPECT_EQ("\"test\"\n" 12999 "\"\\000000\"\n" 13000 "\"000001\"", 13001 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 13002 EXPECT_EQ("\"test\\000\"\n" 13003 "\"00000000\"\n" 13004 "\"1\"", 13005 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 13006 } 13007 13008 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 13009 verifyFormat("void f() {\n" 13010 " return g() {}\n" 13011 " void h() {}"); 13012 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 13013 "g();\n" 13014 "}"); 13015 } 13016 13017 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 13018 verifyFormat( 13019 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 13020 } 13021 13022 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 13023 verifyFormat("class X {\n" 13024 " void f() {\n" 13025 " }\n" 13026 "};", 13027 getLLVMStyleWithColumns(12)); 13028 } 13029 13030 TEST_F(FormatTest, ConfigurableIndentWidth) { 13031 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 13032 EightIndent.IndentWidth = 8; 13033 EightIndent.ContinuationIndentWidth = 8; 13034 verifyFormat("void f() {\n" 13035 " someFunction();\n" 13036 " if (true) {\n" 13037 " f();\n" 13038 " }\n" 13039 "}", 13040 EightIndent); 13041 verifyFormat("class X {\n" 13042 " void f() {\n" 13043 " }\n" 13044 "};", 13045 EightIndent); 13046 verifyFormat("int x[] = {\n" 13047 " call(),\n" 13048 " call()};", 13049 EightIndent); 13050 } 13051 13052 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 13053 verifyFormat("double\n" 13054 "f();", 13055 getLLVMStyleWithColumns(8)); 13056 } 13057 13058 TEST_F(FormatTest, ConfigurableUseOfTab) { 13059 FormatStyle Tab = getLLVMStyleWithColumns(42); 13060 Tab.IndentWidth = 8; 13061 Tab.UseTab = FormatStyle::UT_Always; 13062 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left; 13063 13064 EXPECT_EQ("if (aaaaaaaa && // q\n" 13065 " bb)\t\t// w\n" 13066 "\t;", 13067 format("if (aaaaaaaa &&// q\n" 13068 "bb)// w\n" 13069 ";", 13070 Tab)); 13071 EXPECT_EQ("if (aaa && bbb) // w\n" 13072 "\t;", 13073 format("if(aaa&&bbb)// w\n" 13074 ";", 13075 Tab)); 13076 13077 verifyFormat("class X {\n" 13078 "\tvoid f() {\n" 13079 "\t\tsomeFunction(parameter1,\n" 13080 "\t\t\t parameter2);\n" 13081 "\t}\n" 13082 "};", 13083 Tab); 13084 verifyFormat("#define A \\\n" 13085 "\tvoid f() { \\\n" 13086 "\t\tsomeFunction( \\\n" 13087 "\t\t parameter1, \\\n" 13088 "\t\t parameter2); \\\n" 13089 "\t}", 13090 Tab); 13091 verifyFormat("int a;\t // x\n" 13092 "int bbbbbbbb; // x\n", 13093 Tab); 13094 13095 Tab.TabWidth = 4; 13096 Tab.IndentWidth = 8; 13097 verifyFormat("class TabWidth4Indent8 {\n" 13098 "\t\tvoid f() {\n" 13099 "\t\t\t\tsomeFunction(parameter1,\n" 13100 "\t\t\t\t\t\t\t parameter2);\n" 13101 "\t\t}\n" 13102 "};", 13103 Tab); 13104 13105 Tab.TabWidth = 4; 13106 Tab.IndentWidth = 4; 13107 verifyFormat("class TabWidth4Indent4 {\n" 13108 "\tvoid f() {\n" 13109 "\t\tsomeFunction(parameter1,\n" 13110 "\t\t\t\t\t parameter2);\n" 13111 "\t}\n" 13112 "};", 13113 Tab); 13114 13115 Tab.TabWidth = 8; 13116 Tab.IndentWidth = 4; 13117 verifyFormat("class TabWidth8Indent4 {\n" 13118 " void f() {\n" 13119 "\tsomeFunction(parameter1,\n" 13120 "\t\t parameter2);\n" 13121 " }\n" 13122 "};", 13123 Tab); 13124 13125 Tab.TabWidth = 8; 13126 Tab.IndentWidth = 8; 13127 EXPECT_EQ("/*\n" 13128 "\t a\t\tcomment\n" 13129 "\t in multiple lines\n" 13130 " */", 13131 format(" /*\t \t \n" 13132 " \t \t a\t\tcomment\t \t\n" 13133 " \t \t in multiple lines\t\n" 13134 " \t */", 13135 Tab)); 13136 13137 Tab.UseTab = FormatStyle::UT_ForIndentation; 13138 verifyFormat("{\n" 13139 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13140 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13141 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13142 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13143 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13144 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13145 "};", 13146 Tab); 13147 verifyFormat("enum AA {\n" 13148 "\ta1, // Force multiple lines\n" 13149 "\ta2,\n" 13150 "\ta3\n" 13151 "};", 13152 Tab); 13153 EXPECT_EQ("if (aaaaaaaa && // q\n" 13154 " bb) // w\n" 13155 "\t;", 13156 format("if (aaaaaaaa &&// q\n" 13157 "bb)// w\n" 13158 ";", 13159 Tab)); 13160 verifyFormat("class X {\n" 13161 "\tvoid f() {\n" 13162 "\t\tsomeFunction(parameter1,\n" 13163 "\t\t parameter2);\n" 13164 "\t}\n" 13165 "};", 13166 Tab); 13167 verifyFormat("{\n" 13168 "\tQ(\n" 13169 "\t {\n" 13170 "\t\t int a;\n" 13171 "\t\t someFunction(aaaaaaaa,\n" 13172 "\t\t bbbbbbb);\n" 13173 "\t },\n" 13174 "\t p);\n" 13175 "}", 13176 Tab); 13177 EXPECT_EQ("{\n" 13178 "\t/* aaaa\n" 13179 "\t bbbb */\n" 13180 "}", 13181 format("{\n" 13182 "/* aaaa\n" 13183 " bbbb */\n" 13184 "}", 13185 Tab)); 13186 EXPECT_EQ("{\n" 13187 "\t/*\n" 13188 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13189 "\t bbbbbbbbbbbbb\n" 13190 "\t*/\n" 13191 "}", 13192 format("{\n" 13193 "/*\n" 13194 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13195 "*/\n" 13196 "}", 13197 Tab)); 13198 EXPECT_EQ("{\n" 13199 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13200 "\t// bbbbbbbbbbbbb\n" 13201 "}", 13202 format("{\n" 13203 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13204 "}", 13205 Tab)); 13206 EXPECT_EQ("{\n" 13207 "\t/*\n" 13208 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13209 "\t bbbbbbbbbbbbb\n" 13210 "\t*/\n" 13211 "}", 13212 format("{\n" 13213 "\t/*\n" 13214 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13215 "\t*/\n" 13216 "}", 13217 Tab)); 13218 EXPECT_EQ("{\n" 13219 "\t/*\n" 13220 "\n" 13221 "\t*/\n" 13222 "}", 13223 format("{\n" 13224 "\t/*\n" 13225 "\n" 13226 "\t*/\n" 13227 "}", 13228 Tab)); 13229 EXPECT_EQ("{\n" 13230 "\t/*\n" 13231 " asdf\n" 13232 "\t*/\n" 13233 "}", 13234 format("{\n" 13235 "\t/*\n" 13236 " asdf\n" 13237 "\t*/\n" 13238 "}", 13239 Tab)); 13240 13241 Tab.UseTab = FormatStyle::UT_Never; 13242 EXPECT_EQ("/*\n" 13243 " a\t\tcomment\n" 13244 " in multiple lines\n" 13245 " */", 13246 format(" /*\t \t \n" 13247 " \t \t a\t\tcomment\t \t\n" 13248 " \t \t in multiple lines\t\n" 13249 " \t */", 13250 Tab)); 13251 EXPECT_EQ("/* some\n" 13252 " comment */", 13253 format(" \t \t /* some\n" 13254 " \t \t comment */", 13255 Tab)); 13256 EXPECT_EQ("int a; /* some\n" 13257 " comment */", 13258 format(" \t \t int a; /* some\n" 13259 " \t \t comment */", 13260 Tab)); 13261 13262 EXPECT_EQ("int a; /* some\n" 13263 "comment */", 13264 format(" \t \t int\ta; /* some\n" 13265 " \t \t comment */", 13266 Tab)); 13267 EXPECT_EQ("f(\"\t\t\"); /* some\n" 13268 " comment */", 13269 format(" \t \t f(\"\t\t\"); /* some\n" 13270 " \t \t comment */", 13271 Tab)); 13272 EXPECT_EQ("{\n" 13273 " /*\n" 13274 " * Comment\n" 13275 " */\n" 13276 " int i;\n" 13277 "}", 13278 format("{\n" 13279 "\t/*\n" 13280 "\t * Comment\n" 13281 "\t */\n" 13282 "\t int i;\n" 13283 "}", 13284 Tab)); 13285 13286 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 13287 Tab.TabWidth = 8; 13288 Tab.IndentWidth = 8; 13289 EXPECT_EQ("if (aaaaaaaa && // q\n" 13290 " bb) // w\n" 13291 "\t;", 13292 format("if (aaaaaaaa &&// q\n" 13293 "bb)// w\n" 13294 ";", 13295 Tab)); 13296 EXPECT_EQ("if (aaa && bbb) // w\n" 13297 "\t;", 13298 format("if(aaa&&bbb)// w\n" 13299 ";", 13300 Tab)); 13301 verifyFormat("class X {\n" 13302 "\tvoid f() {\n" 13303 "\t\tsomeFunction(parameter1,\n" 13304 "\t\t\t parameter2);\n" 13305 "\t}\n" 13306 "};", 13307 Tab); 13308 verifyFormat("#define A \\\n" 13309 "\tvoid f() { \\\n" 13310 "\t\tsomeFunction( \\\n" 13311 "\t\t parameter1, \\\n" 13312 "\t\t parameter2); \\\n" 13313 "\t}", 13314 Tab); 13315 Tab.TabWidth = 4; 13316 Tab.IndentWidth = 8; 13317 verifyFormat("class TabWidth4Indent8 {\n" 13318 "\t\tvoid f() {\n" 13319 "\t\t\t\tsomeFunction(parameter1,\n" 13320 "\t\t\t\t\t\t\t parameter2);\n" 13321 "\t\t}\n" 13322 "};", 13323 Tab); 13324 Tab.TabWidth = 4; 13325 Tab.IndentWidth = 4; 13326 verifyFormat("class TabWidth4Indent4 {\n" 13327 "\tvoid f() {\n" 13328 "\t\tsomeFunction(parameter1,\n" 13329 "\t\t\t\t\t parameter2);\n" 13330 "\t}\n" 13331 "};", 13332 Tab); 13333 Tab.TabWidth = 8; 13334 Tab.IndentWidth = 4; 13335 verifyFormat("class TabWidth8Indent4 {\n" 13336 " void f() {\n" 13337 "\tsomeFunction(parameter1,\n" 13338 "\t\t parameter2);\n" 13339 " }\n" 13340 "};", 13341 Tab); 13342 Tab.TabWidth = 8; 13343 Tab.IndentWidth = 8; 13344 EXPECT_EQ("/*\n" 13345 "\t a\t\tcomment\n" 13346 "\t in multiple lines\n" 13347 " */", 13348 format(" /*\t \t \n" 13349 " \t \t a\t\tcomment\t \t\n" 13350 " \t \t in multiple lines\t\n" 13351 " \t */", 13352 Tab)); 13353 verifyFormat("{\n" 13354 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13355 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13356 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13357 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13358 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13359 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13360 "};", 13361 Tab); 13362 verifyFormat("enum AA {\n" 13363 "\ta1, // Force multiple lines\n" 13364 "\ta2,\n" 13365 "\ta3\n" 13366 "};", 13367 Tab); 13368 EXPECT_EQ("if (aaaaaaaa && // q\n" 13369 " bb) // w\n" 13370 "\t;", 13371 format("if (aaaaaaaa &&// q\n" 13372 "bb)// w\n" 13373 ";", 13374 Tab)); 13375 verifyFormat("class X {\n" 13376 "\tvoid f() {\n" 13377 "\t\tsomeFunction(parameter1,\n" 13378 "\t\t\t parameter2);\n" 13379 "\t}\n" 13380 "};", 13381 Tab); 13382 verifyFormat("{\n" 13383 "\tQ(\n" 13384 "\t {\n" 13385 "\t\t int a;\n" 13386 "\t\t someFunction(aaaaaaaa,\n" 13387 "\t\t\t\t bbbbbbb);\n" 13388 "\t },\n" 13389 "\t p);\n" 13390 "}", 13391 Tab); 13392 EXPECT_EQ("{\n" 13393 "\t/* aaaa\n" 13394 "\t bbbb */\n" 13395 "}", 13396 format("{\n" 13397 "/* aaaa\n" 13398 " bbbb */\n" 13399 "}", 13400 Tab)); 13401 EXPECT_EQ("{\n" 13402 "\t/*\n" 13403 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13404 "\t bbbbbbbbbbbbb\n" 13405 "\t*/\n" 13406 "}", 13407 format("{\n" 13408 "/*\n" 13409 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13410 "*/\n" 13411 "}", 13412 Tab)); 13413 EXPECT_EQ("{\n" 13414 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13415 "\t// bbbbbbbbbbbbb\n" 13416 "}", 13417 format("{\n" 13418 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13419 "}", 13420 Tab)); 13421 EXPECT_EQ("{\n" 13422 "\t/*\n" 13423 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13424 "\t bbbbbbbbbbbbb\n" 13425 "\t*/\n" 13426 "}", 13427 format("{\n" 13428 "\t/*\n" 13429 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13430 "\t*/\n" 13431 "}", 13432 Tab)); 13433 EXPECT_EQ("{\n" 13434 "\t/*\n" 13435 "\n" 13436 "\t*/\n" 13437 "}", 13438 format("{\n" 13439 "\t/*\n" 13440 "\n" 13441 "\t*/\n" 13442 "}", 13443 Tab)); 13444 EXPECT_EQ("{\n" 13445 "\t/*\n" 13446 " asdf\n" 13447 "\t*/\n" 13448 "}", 13449 format("{\n" 13450 "\t/*\n" 13451 " asdf\n" 13452 "\t*/\n" 13453 "}", 13454 Tab)); 13455 EXPECT_EQ("/* some\n" 13456 " comment */", 13457 format(" \t \t /* some\n" 13458 " \t \t comment */", 13459 Tab)); 13460 EXPECT_EQ("int a; /* some\n" 13461 " comment */", 13462 format(" \t \t int a; /* some\n" 13463 " \t \t comment */", 13464 Tab)); 13465 EXPECT_EQ("int a; /* some\n" 13466 "comment */", 13467 format(" \t \t int\ta; /* some\n" 13468 " \t \t comment */", 13469 Tab)); 13470 EXPECT_EQ("f(\"\t\t\"); /* some\n" 13471 " comment */", 13472 format(" \t \t f(\"\t\t\"); /* some\n" 13473 " \t \t comment */", 13474 Tab)); 13475 EXPECT_EQ("{\n" 13476 "\t/*\n" 13477 "\t * Comment\n" 13478 "\t */\n" 13479 "\tint i;\n" 13480 "}", 13481 format("{\n" 13482 "\t/*\n" 13483 "\t * Comment\n" 13484 "\t */\n" 13485 "\t int i;\n" 13486 "}", 13487 Tab)); 13488 Tab.TabWidth = 2; 13489 Tab.IndentWidth = 2; 13490 EXPECT_EQ("{\n" 13491 "\t/* aaaa\n" 13492 "\t\t bbbb */\n" 13493 "}", 13494 format("{\n" 13495 "/* aaaa\n" 13496 "\t bbbb */\n" 13497 "}", 13498 Tab)); 13499 EXPECT_EQ("{\n" 13500 "\t/*\n" 13501 "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13502 "\t\tbbbbbbbbbbbbb\n" 13503 "\t*/\n" 13504 "}", 13505 format("{\n" 13506 "/*\n" 13507 "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13508 "*/\n" 13509 "}", 13510 Tab)); 13511 Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 13512 Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 13513 Tab.TabWidth = 4; 13514 Tab.IndentWidth = 4; 13515 verifyFormat("class Assign {\n" 13516 "\tvoid f() {\n" 13517 "\t\tint x = 123;\n" 13518 "\t\tint random = 4;\n" 13519 "\t\tstd::string alphabet =\n" 13520 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 13521 "\t}\n" 13522 "};", 13523 Tab); 13524 13525 Tab.UseTab = FormatStyle::UT_AlignWithSpaces; 13526 Tab.TabWidth = 8; 13527 Tab.IndentWidth = 8; 13528 EXPECT_EQ("if (aaaaaaaa && // q\n" 13529 " bb) // w\n" 13530 "\t;", 13531 format("if (aaaaaaaa &&// q\n" 13532 "bb)// w\n" 13533 ";", 13534 Tab)); 13535 EXPECT_EQ("if (aaa && bbb) // w\n" 13536 "\t;", 13537 format("if(aaa&&bbb)// w\n" 13538 ";", 13539 Tab)); 13540 verifyFormat("class X {\n" 13541 "\tvoid f() {\n" 13542 "\t\tsomeFunction(parameter1,\n" 13543 "\t\t parameter2);\n" 13544 "\t}\n" 13545 "};", 13546 Tab); 13547 verifyFormat("#define A \\\n" 13548 "\tvoid f() { \\\n" 13549 "\t\tsomeFunction( \\\n" 13550 "\t\t parameter1, \\\n" 13551 "\t\t parameter2); \\\n" 13552 "\t}", 13553 Tab); 13554 Tab.TabWidth = 4; 13555 Tab.IndentWidth = 8; 13556 verifyFormat("class TabWidth4Indent8 {\n" 13557 "\t\tvoid f() {\n" 13558 "\t\t\t\tsomeFunction(parameter1,\n" 13559 "\t\t\t\t parameter2);\n" 13560 "\t\t}\n" 13561 "};", 13562 Tab); 13563 Tab.TabWidth = 4; 13564 Tab.IndentWidth = 4; 13565 verifyFormat("class TabWidth4Indent4 {\n" 13566 "\tvoid f() {\n" 13567 "\t\tsomeFunction(parameter1,\n" 13568 "\t\t parameter2);\n" 13569 "\t}\n" 13570 "};", 13571 Tab); 13572 Tab.TabWidth = 8; 13573 Tab.IndentWidth = 4; 13574 verifyFormat("class TabWidth8Indent4 {\n" 13575 " void f() {\n" 13576 "\tsomeFunction(parameter1,\n" 13577 "\t parameter2);\n" 13578 " }\n" 13579 "};", 13580 Tab); 13581 Tab.TabWidth = 8; 13582 Tab.IndentWidth = 8; 13583 EXPECT_EQ("/*\n" 13584 " a\t\tcomment\n" 13585 " in multiple lines\n" 13586 " */", 13587 format(" /*\t \t \n" 13588 " \t \t a\t\tcomment\t \t\n" 13589 " \t \t in multiple lines\t\n" 13590 " \t */", 13591 Tab)); 13592 verifyFormat("{\n" 13593 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13594 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13595 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13596 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13597 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13598 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13599 "};", 13600 Tab); 13601 verifyFormat("enum AA {\n" 13602 "\ta1, // Force multiple lines\n" 13603 "\ta2,\n" 13604 "\ta3\n" 13605 "};", 13606 Tab); 13607 EXPECT_EQ("if (aaaaaaaa && // q\n" 13608 " bb) // w\n" 13609 "\t;", 13610 format("if (aaaaaaaa &&// q\n" 13611 "bb)// w\n" 13612 ";", 13613 Tab)); 13614 verifyFormat("class X {\n" 13615 "\tvoid f() {\n" 13616 "\t\tsomeFunction(parameter1,\n" 13617 "\t\t parameter2);\n" 13618 "\t}\n" 13619 "};", 13620 Tab); 13621 verifyFormat("{\n" 13622 "\tQ(\n" 13623 "\t {\n" 13624 "\t\t int a;\n" 13625 "\t\t someFunction(aaaaaaaa,\n" 13626 "\t\t bbbbbbb);\n" 13627 "\t },\n" 13628 "\t p);\n" 13629 "}", 13630 Tab); 13631 EXPECT_EQ("{\n" 13632 "\t/* aaaa\n" 13633 "\t bbbb */\n" 13634 "}", 13635 format("{\n" 13636 "/* aaaa\n" 13637 " bbbb */\n" 13638 "}", 13639 Tab)); 13640 EXPECT_EQ("{\n" 13641 "\t/*\n" 13642 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13643 "\t bbbbbbbbbbbbb\n" 13644 "\t*/\n" 13645 "}", 13646 format("{\n" 13647 "/*\n" 13648 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13649 "*/\n" 13650 "}", 13651 Tab)); 13652 EXPECT_EQ("{\n" 13653 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13654 "\t// bbbbbbbbbbbbb\n" 13655 "}", 13656 format("{\n" 13657 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13658 "}", 13659 Tab)); 13660 EXPECT_EQ("{\n" 13661 "\t/*\n" 13662 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13663 "\t bbbbbbbbbbbbb\n" 13664 "\t*/\n" 13665 "}", 13666 format("{\n" 13667 "\t/*\n" 13668 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13669 "\t*/\n" 13670 "}", 13671 Tab)); 13672 EXPECT_EQ("{\n" 13673 "\t/*\n" 13674 "\n" 13675 "\t*/\n" 13676 "}", 13677 format("{\n" 13678 "\t/*\n" 13679 "\n" 13680 "\t*/\n" 13681 "}", 13682 Tab)); 13683 EXPECT_EQ("{\n" 13684 "\t/*\n" 13685 " asdf\n" 13686 "\t*/\n" 13687 "}", 13688 format("{\n" 13689 "\t/*\n" 13690 " asdf\n" 13691 "\t*/\n" 13692 "}", 13693 Tab)); 13694 EXPECT_EQ("/* some\n" 13695 " comment */", 13696 format(" \t \t /* some\n" 13697 " \t \t comment */", 13698 Tab)); 13699 EXPECT_EQ("int a; /* some\n" 13700 " comment */", 13701 format(" \t \t int a; /* some\n" 13702 " \t \t comment */", 13703 Tab)); 13704 EXPECT_EQ("int a; /* some\n" 13705 "comment */", 13706 format(" \t \t int\ta; /* some\n" 13707 " \t \t comment */", 13708 Tab)); 13709 EXPECT_EQ("f(\"\t\t\"); /* some\n" 13710 " comment */", 13711 format(" \t \t f(\"\t\t\"); /* some\n" 13712 " \t \t comment */", 13713 Tab)); 13714 EXPECT_EQ("{\n" 13715 "\t/*\n" 13716 "\t * Comment\n" 13717 "\t */\n" 13718 "\tint i;\n" 13719 "}", 13720 format("{\n" 13721 "\t/*\n" 13722 "\t * Comment\n" 13723 "\t */\n" 13724 "\t int i;\n" 13725 "}", 13726 Tab)); 13727 Tab.TabWidth = 2; 13728 Tab.IndentWidth = 2; 13729 EXPECT_EQ("{\n" 13730 "\t/* aaaa\n" 13731 "\t bbbb */\n" 13732 "}", 13733 format("{\n" 13734 "/* aaaa\n" 13735 " bbbb */\n" 13736 "}", 13737 Tab)); 13738 EXPECT_EQ("{\n" 13739 "\t/*\n" 13740 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13741 "\t bbbbbbbbbbbbb\n" 13742 "\t*/\n" 13743 "}", 13744 format("{\n" 13745 "/*\n" 13746 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13747 "*/\n" 13748 "}", 13749 Tab)); 13750 Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 13751 Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 13752 Tab.TabWidth = 4; 13753 Tab.IndentWidth = 4; 13754 verifyFormat("class Assign {\n" 13755 "\tvoid f() {\n" 13756 "\t\tint x = 123;\n" 13757 "\t\tint random = 4;\n" 13758 "\t\tstd::string alphabet =\n" 13759 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 13760 "\t}\n" 13761 "};", 13762 Tab); 13763 Tab.AlignOperands = FormatStyle::OAS_Align; 13764 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n" 13765 " cccccccccccccccccccc;", 13766 Tab); 13767 // no alignment 13768 verifyFormat("int aaaaaaaaaa =\n" 13769 "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 13770 Tab); 13771 verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n" 13772 " : bbbbbbbbbbbbbb ? 222222222222222\n" 13773 " : 333333333333333;", 13774 Tab); 13775 Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 13776 Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator; 13777 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n" 13778 " + cccccccccccccccccccc;", 13779 Tab); 13780 } 13781 13782 TEST_F(FormatTest, ZeroTabWidth) { 13783 FormatStyle Tab = getLLVMStyleWithColumns(42); 13784 Tab.IndentWidth = 8; 13785 Tab.UseTab = FormatStyle::UT_Never; 13786 Tab.TabWidth = 0; 13787 EXPECT_EQ("void a(){\n" 13788 " // line starts with '\t'\n" 13789 "};", 13790 format("void a(){\n" 13791 "\t// line starts with '\t'\n" 13792 "};", 13793 Tab)); 13794 13795 EXPECT_EQ("void a(){\n" 13796 " // line starts with '\t'\n" 13797 "};", 13798 format("void a(){\n" 13799 "\t\t// line starts with '\t'\n" 13800 "};", 13801 Tab)); 13802 13803 Tab.UseTab = FormatStyle::UT_ForIndentation; 13804 EXPECT_EQ("void a(){\n" 13805 " // line starts with '\t'\n" 13806 "};", 13807 format("void a(){\n" 13808 "\t// line starts with '\t'\n" 13809 "};", 13810 Tab)); 13811 13812 EXPECT_EQ("void a(){\n" 13813 " // line starts with '\t'\n" 13814 "};", 13815 format("void a(){\n" 13816 "\t\t// line starts with '\t'\n" 13817 "};", 13818 Tab)); 13819 13820 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 13821 EXPECT_EQ("void a(){\n" 13822 " // line starts with '\t'\n" 13823 "};", 13824 format("void a(){\n" 13825 "\t// line starts with '\t'\n" 13826 "};", 13827 Tab)); 13828 13829 EXPECT_EQ("void a(){\n" 13830 " // line starts with '\t'\n" 13831 "};", 13832 format("void a(){\n" 13833 "\t\t// line starts with '\t'\n" 13834 "};", 13835 Tab)); 13836 13837 Tab.UseTab = FormatStyle::UT_AlignWithSpaces; 13838 EXPECT_EQ("void a(){\n" 13839 " // line starts with '\t'\n" 13840 "};", 13841 format("void a(){\n" 13842 "\t// line starts with '\t'\n" 13843 "};", 13844 Tab)); 13845 13846 EXPECT_EQ("void a(){\n" 13847 " // line starts with '\t'\n" 13848 "};", 13849 format("void a(){\n" 13850 "\t\t// line starts with '\t'\n" 13851 "};", 13852 Tab)); 13853 13854 Tab.UseTab = FormatStyle::UT_Always; 13855 EXPECT_EQ("void a(){\n" 13856 "// line starts with '\t'\n" 13857 "};", 13858 format("void a(){\n" 13859 "\t// line starts with '\t'\n" 13860 "};", 13861 Tab)); 13862 13863 EXPECT_EQ("void a(){\n" 13864 "// line starts with '\t'\n" 13865 "};", 13866 format("void a(){\n" 13867 "\t\t// line starts with '\t'\n" 13868 "};", 13869 Tab)); 13870 } 13871 13872 TEST_F(FormatTest, CalculatesOriginalColumn) { 13873 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13874 "q\"; /* some\n" 13875 " comment */", 13876 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13877 "q\"; /* some\n" 13878 " comment */", 13879 getLLVMStyle())); 13880 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 13881 "/* some\n" 13882 " comment */", 13883 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 13884 " /* some\n" 13885 " comment */", 13886 getLLVMStyle())); 13887 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13888 "qqq\n" 13889 "/* some\n" 13890 " comment */", 13891 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13892 "qqq\n" 13893 " /* some\n" 13894 " comment */", 13895 getLLVMStyle())); 13896 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13897 "wwww; /* some\n" 13898 " comment */", 13899 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13900 "wwww; /* some\n" 13901 " comment */", 13902 getLLVMStyle())); 13903 } 13904 13905 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 13906 FormatStyle NoSpace = getLLVMStyle(); 13907 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 13908 13909 verifyFormat("while(true)\n" 13910 " continue;", 13911 NoSpace); 13912 verifyFormat("for(;;)\n" 13913 " continue;", 13914 NoSpace); 13915 verifyFormat("if(true)\n" 13916 " f();\n" 13917 "else if(true)\n" 13918 " f();", 13919 NoSpace); 13920 verifyFormat("do {\n" 13921 " do_something();\n" 13922 "} while(something());", 13923 NoSpace); 13924 verifyFormat("switch(x) {\n" 13925 "default:\n" 13926 " break;\n" 13927 "}", 13928 NoSpace); 13929 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 13930 verifyFormat("size_t x = sizeof(x);", NoSpace); 13931 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 13932 verifyFormat("auto f(int x) -> typeof(x);", NoSpace); 13933 verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace); 13934 verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace); 13935 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 13936 verifyFormat("alignas(128) char a[128];", NoSpace); 13937 verifyFormat("size_t x = alignof(MyType);", NoSpace); 13938 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 13939 verifyFormat("int f() throw(Deprecated);", NoSpace); 13940 verifyFormat("typedef void (*cb)(int);", NoSpace); 13941 verifyFormat("T A::operator()();", NoSpace); 13942 verifyFormat("X A::operator++(T);", NoSpace); 13943 verifyFormat("auto lambda = []() { return 0; };", NoSpace); 13944 13945 FormatStyle Space = getLLVMStyle(); 13946 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 13947 13948 verifyFormat("int f ();", Space); 13949 verifyFormat("void f (int a, T b) {\n" 13950 " while (true)\n" 13951 " continue;\n" 13952 "}", 13953 Space); 13954 verifyFormat("if (true)\n" 13955 " f ();\n" 13956 "else if (true)\n" 13957 " f ();", 13958 Space); 13959 verifyFormat("do {\n" 13960 " do_something ();\n" 13961 "} while (something ());", 13962 Space); 13963 verifyFormat("switch (x) {\n" 13964 "default:\n" 13965 " break;\n" 13966 "}", 13967 Space); 13968 verifyFormat("A::A () : a (1) {}", Space); 13969 verifyFormat("void f () __attribute__ ((asdf));", Space); 13970 verifyFormat("*(&a + 1);\n" 13971 "&((&a)[1]);\n" 13972 "a[(b + c) * d];\n" 13973 "(((a + 1) * 2) + 3) * 4;", 13974 Space); 13975 verifyFormat("#define A(x) x", Space); 13976 verifyFormat("#define A (x) x", Space); 13977 verifyFormat("#if defined(x)\n" 13978 "#endif", 13979 Space); 13980 verifyFormat("auto i = std::make_unique<int> (5);", Space); 13981 verifyFormat("size_t x = sizeof (x);", Space); 13982 verifyFormat("auto f (int x) -> decltype (x);", Space); 13983 verifyFormat("auto f (int x) -> typeof (x);", Space); 13984 verifyFormat("auto f (int x) -> _Atomic (x);", Space); 13985 verifyFormat("auto f (int x) -> __underlying_type (x);", Space); 13986 verifyFormat("int f (T x) noexcept (x.create ());", Space); 13987 verifyFormat("alignas (128) char a[128];", Space); 13988 verifyFormat("size_t x = alignof (MyType);", Space); 13989 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 13990 verifyFormat("int f () throw (Deprecated);", Space); 13991 verifyFormat("typedef void (*cb) (int);", Space); 13992 verifyFormat("T A::operator() ();", Space); 13993 verifyFormat("X A::operator++ (T);", Space); 13994 verifyFormat("auto lambda = [] () { return 0; };", Space); 13995 verifyFormat("int x = int (y);", Space); 13996 13997 FormatStyle SomeSpace = getLLVMStyle(); 13998 SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses; 13999 14000 verifyFormat("[]() -> float {}", SomeSpace); 14001 verifyFormat("[] (auto foo) {}", SomeSpace); 14002 verifyFormat("[foo]() -> int {}", SomeSpace); 14003 verifyFormat("int f();", SomeSpace); 14004 verifyFormat("void f (int a, T b) {\n" 14005 " while (true)\n" 14006 " continue;\n" 14007 "}", 14008 SomeSpace); 14009 verifyFormat("if (true)\n" 14010 " f();\n" 14011 "else if (true)\n" 14012 " f();", 14013 SomeSpace); 14014 verifyFormat("do {\n" 14015 " do_something();\n" 14016 "} while (something());", 14017 SomeSpace); 14018 verifyFormat("switch (x) {\n" 14019 "default:\n" 14020 " break;\n" 14021 "}", 14022 SomeSpace); 14023 verifyFormat("A::A() : a (1) {}", SomeSpace); 14024 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace); 14025 verifyFormat("*(&a + 1);\n" 14026 "&((&a)[1]);\n" 14027 "a[(b + c) * d];\n" 14028 "(((a + 1) * 2) + 3) * 4;", 14029 SomeSpace); 14030 verifyFormat("#define A(x) x", SomeSpace); 14031 verifyFormat("#define A (x) x", SomeSpace); 14032 verifyFormat("#if defined(x)\n" 14033 "#endif", 14034 SomeSpace); 14035 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace); 14036 verifyFormat("size_t x = sizeof (x);", SomeSpace); 14037 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace); 14038 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace); 14039 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace); 14040 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace); 14041 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace); 14042 verifyFormat("alignas (128) char a[128];", SomeSpace); 14043 verifyFormat("size_t x = alignof (MyType);", SomeSpace); 14044 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", 14045 SomeSpace); 14046 verifyFormat("int f() throw (Deprecated);", SomeSpace); 14047 verifyFormat("typedef void (*cb) (int);", SomeSpace); 14048 verifyFormat("T A::operator()();", SomeSpace); 14049 verifyFormat("X A::operator++ (T);", SomeSpace); 14050 verifyFormat("int x = int (y);", SomeSpace); 14051 verifyFormat("auto lambda = []() { return 0; };", SomeSpace); 14052 } 14053 14054 TEST_F(FormatTest, SpaceAfterLogicalNot) { 14055 FormatStyle Spaces = getLLVMStyle(); 14056 Spaces.SpaceAfterLogicalNot = true; 14057 14058 verifyFormat("bool x = ! y", Spaces); 14059 verifyFormat("if (! isFailure())", Spaces); 14060 verifyFormat("if (! (a && b))", Spaces); 14061 verifyFormat("\"Error!\"", Spaces); 14062 verifyFormat("! ! x", Spaces); 14063 } 14064 14065 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 14066 FormatStyle Spaces = getLLVMStyle(); 14067 14068 Spaces.SpacesInParentheses = true; 14069 verifyFormat("do_something( ::globalVar );", Spaces); 14070 verifyFormat("call( x, y, z );", Spaces); 14071 verifyFormat("call();", Spaces); 14072 verifyFormat("std::function<void( int, int )> callback;", Spaces); 14073 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 14074 Spaces); 14075 verifyFormat("while ( (bool)1 )\n" 14076 " continue;", 14077 Spaces); 14078 verifyFormat("for ( ;; )\n" 14079 " continue;", 14080 Spaces); 14081 verifyFormat("if ( true )\n" 14082 " f();\n" 14083 "else if ( true )\n" 14084 " f();", 14085 Spaces); 14086 verifyFormat("do {\n" 14087 " do_something( (int)i );\n" 14088 "} while ( something() );", 14089 Spaces); 14090 verifyFormat("switch ( x ) {\n" 14091 "default:\n" 14092 " break;\n" 14093 "}", 14094 Spaces); 14095 14096 Spaces.SpacesInParentheses = false; 14097 Spaces.SpacesInCStyleCastParentheses = true; 14098 verifyFormat("Type *A = ( Type * )P;", Spaces); 14099 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 14100 verifyFormat("x = ( int32 )y;", Spaces); 14101 verifyFormat("int a = ( int )(2.0f);", Spaces); 14102 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 14103 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 14104 verifyFormat("#define x (( int )-1)", Spaces); 14105 14106 // Run the first set of tests again with: 14107 Spaces.SpacesInParentheses = false; 14108 Spaces.SpaceInEmptyParentheses = true; 14109 Spaces.SpacesInCStyleCastParentheses = true; 14110 verifyFormat("call(x, y, z);", Spaces); 14111 verifyFormat("call( );", Spaces); 14112 verifyFormat("std::function<void(int, int)> callback;", Spaces); 14113 verifyFormat("while (( bool )1)\n" 14114 " continue;", 14115 Spaces); 14116 verifyFormat("for (;;)\n" 14117 " continue;", 14118 Spaces); 14119 verifyFormat("if (true)\n" 14120 " f( );\n" 14121 "else if (true)\n" 14122 " f( );", 14123 Spaces); 14124 verifyFormat("do {\n" 14125 " do_something(( int )i);\n" 14126 "} while (something( ));", 14127 Spaces); 14128 verifyFormat("switch (x) {\n" 14129 "default:\n" 14130 " break;\n" 14131 "}", 14132 Spaces); 14133 14134 // Run the first set of tests again with: 14135 Spaces.SpaceAfterCStyleCast = true; 14136 verifyFormat("call(x, y, z);", Spaces); 14137 verifyFormat("call( );", Spaces); 14138 verifyFormat("std::function<void(int, int)> callback;", Spaces); 14139 verifyFormat("while (( bool ) 1)\n" 14140 " continue;", 14141 Spaces); 14142 verifyFormat("for (;;)\n" 14143 " continue;", 14144 Spaces); 14145 verifyFormat("if (true)\n" 14146 " f( );\n" 14147 "else if (true)\n" 14148 " f( );", 14149 Spaces); 14150 verifyFormat("do {\n" 14151 " do_something(( int ) i);\n" 14152 "} while (something( ));", 14153 Spaces); 14154 verifyFormat("switch (x) {\n" 14155 "default:\n" 14156 " break;\n" 14157 "}", 14158 Spaces); 14159 14160 // Run subset of tests again with: 14161 Spaces.SpacesInCStyleCastParentheses = false; 14162 Spaces.SpaceAfterCStyleCast = true; 14163 verifyFormat("while ((bool) 1)\n" 14164 " continue;", 14165 Spaces); 14166 verifyFormat("do {\n" 14167 " do_something((int) i);\n" 14168 "} while (something( ));", 14169 Spaces); 14170 14171 verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces); 14172 verifyFormat("size_t idx = (size_t) a;", Spaces); 14173 verifyFormat("size_t idx = (size_t) (a - 1);", Spaces); 14174 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); 14175 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); 14176 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); 14177 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); 14178 Spaces.ColumnLimit = 80; 14179 Spaces.IndentWidth = 4; 14180 Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 14181 verifyFormat("void foo( ) {\n" 14182 " size_t foo = (*(function))(\n" 14183 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " 14184 "BarrrrrrrrrrrrLong,\n" 14185 " FoooooooooLooooong);\n" 14186 "}", 14187 Spaces); 14188 Spaces.SpaceAfterCStyleCast = false; 14189 verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces); 14190 verifyFormat("size_t idx = (size_t)a;", Spaces); 14191 verifyFormat("size_t idx = (size_t)(a - 1);", Spaces); 14192 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); 14193 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); 14194 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); 14195 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); 14196 14197 verifyFormat("void foo( ) {\n" 14198 " size_t foo = (*(function))(\n" 14199 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " 14200 "BarrrrrrrrrrrrLong,\n" 14201 " FoooooooooLooooong);\n" 14202 "}", 14203 Spaces); 14204 } 14205 14206 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 14207 verifyFormat("int a[5];"); 14208 verifyFormat("a[3] += 42;"); 14209 14210 FormatStyle Spaces = getLLVMStyle(); 14211 Spaces.SpacesInSquareBrackets = true; 14212 // Not lambdas. 14213 verifyFormat("int a[ 5 ];", Spaces); 14214 verifyFormat("a[ 3 ] += 42;", Spaces); 14215 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 14216 verifyFormat("double &operator[](int i) { return 0; }\n" 14217 "int i;", 14218 Spaces); 14219 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 14220 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 14221 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 14222 // Lambdas. 14223 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 14224 verifyFormat("return [ i, args... ] {};", Spaces); 14225 verifyFormat("int foo = [ &bar ]() {};", Spaces); 14226 verifyFormat("int foo = [ = ]() {};", Spaces); 14227 verifyFormat("int foo = [ & ]() {};", Spaces); 14228 verifyFormat("int foo = [ =, &bar ]() {};", Spaces); 14229 verifyFormat("int foo = [ &bar, = ]() {};", Spaces); 14230 } 14231 14232 TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) { 14233 FormatStyle NoSpaceStyle = getLLVMStyle(); 14234 verifyFormat("int a[5];", NoSpaceStyle); 14235 verifyFormat("a[3] += 42;", NoSpaceStyle); 14236 14237 verifyFormat("int a[1];", NoSpaceStyle); 14238 verifyFormat("int 1 [a];", NoSpaceStyle); 14239 verifyFormat("int a[1][2];", NoSpaceStyle); 14240 verifyFormat("a[7] = 5;", NoSpaceStyle); 14241 verifyFormat("int a = (f())[23];", NoSpaceStyle); 14242 verifyFormat("f([] {})", NoSpaceStyle); 14243 14244 FormatStyle Space = getLLVMStyle(); 14245 Space.SpaceBeforeSquareBrackets = true; 14246 verifyFormat("int c = []() -> int { return 2; }();\n", Space); 14247 verifyFormat("return [i, args...] {};", Space); 14248 14249 verifyFormat("int a [5];", Space); 14250 verifyFormat("a [3] += 42;", Space); 14251 verifyFormat("constexpr char hello []{\"hello\"};", Space); 14252 verifyFormat("double &operator[](int i) { return 0; }\n" 14253 "int i;", 14254 Space); 14255 verifyFormat("std::unique_ptr<int []> foo() {}", Space); 14256 verifyFormat("int i = a [a][a]->f();", Space); 14257 verifyFormat("int i = (*b) [a]->f();", Space); 14258 14259 verifyFormat("int a [1];", Space); 14260 verifyFormat("int 1 [a];", Space); 14261 verifyFormat("int a [1][2];", Space); 14262 verifyFormat("a [7] = 5;", Space); 14263 verifyFormat("int a = (f()) [23];", Space); 14264 verifyFormat("f([] {})", Space); 14265 } 14266 14267 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 14268 verifyFormat("int a = 5;"); 14269 verifyFormat("a += 42;"); 14270 verifyFormat("a or_eq 8;"); 14271 14272 FormatStyle Spaces = getLLVMStyle(); 14273 Spaces.SpaceBeforeAssignmentOperators = false; 14274 verifyFormat("int a= 5;", Spaces); 14275 verifyFormat("a+= 42;", Spaces); 14276 verifyFormat("a or_eq 8;", Spaces); 14277 } 14278 14279 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) { 14280 verifyFormat("class Foo : public Bar {};"); 14281 verifyFormat("Foo::Foo() : foo(1) {}"); 14282 verifyFormat("for (auto a : b) {\n}"); 14283 verifyFormat("int x = a ? b : c;"); 14284 verifyFormat("{\n" 14285 "label0:\n" 14286 " int x = 0;\n" 14287 "}"); 14288 verifyFormat("switch (x) {\n" 14289 "case 1:\n" 14290 "default:\n" 14291 "}"); 14292 verifyFormat("switch (allBraces) {\n" 14293 "case 1: {\n" 14294 " break;\n" 14295 "}\n" 14296 "case 2: {\n" 14297 " [[fallthrough]];\n" 14298 "}\n" 14299 "default: {\n" 14300 " break;\n" 14301 "}\n" 14302 "}"); 14303 14304 FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30); 14305 CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false; 14306 verifyFormat("class Foo : public Bar {};", CtorInitializerStyle); 14307 verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle); 14308 verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle); 14309 verifyFormat("int x = a ? b : c;", CtorInitializerStyle); 14310 verifyFormat("{\n" 14311 "label1:\n" 14312 " int x = 0;\n" 14313 "}", 14314 CtorInitializerStyle); 14315 verifyFormat("switch (x) {\n" 14316 "case 1:\n" 14317 "default:\n" 14318 "}", 14319 CtorInitializerStyle); 14320 verifyFormat("switch (allBraces) {\n" 14321 "case 1: {\n" 14322 " break;\n" 14323 "}\n" 14324 "case 2: {\n" 14325 " [[fallthrough]];\n" 14326 "}\n" 14327 "default: {\n" 14328 " break;\n" 14329 "}\n" 14330 "}", 14331 CtorInitializerStyle); 14332 CtorInitializerStyle.BreakConstructorInitializers = 14333 FormatStyle::BCIS_AfterColon; 14334 verifyFormat("Fooooooooooo::Fooooooooooo():\n" 14335 " aaaaaaaaaaaaaaaa(1),\n" 14336 " bbbbbbbbbbbbbbbb(2) {}", 14337 CtorInitializerStyle); 14338 CtorInitializerStyle.BreakConstructorInitializers = 14339 FormatStyle::BCIS_BeforeComma; 14340 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 14341 " : aaaaaaaaaaaaaaaa(1)\n" 14342 " , bbbbbbbbbbbbbbbb(2) {}", 14343 CtorInitializerStyle); 14344 CtorInitializerStyle.BreakConstructorInitializers = 14345 FormatStyle::BCIS_BeforeColon; 14346 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 14347 " : aaaaaaaaaaaaaaaa(1),\n" 14348 " bbbbbbbbbbbbbbbb(2) {}", 14349 CtorInitializerStyle); 14350 CtorInitializerStyle.ConstructorInitializerIndentWidth = 0; 14351 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 14352 ": aaaaaaaaaaaaaaaa(1),\n" 14353 " bbbbbbbbbbbbbbbb(2) {}", 14354 CtorInitializerStyle); 14355 14356 FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30); 14357 InheritanceStyle.SpaceBeforeInheritanceColon = false; 14358 verifyFormat("class Foo: public Bar {};", InheritanceStyle); 14359 verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle); 14360 verifyFormat("for (auto a : b) {\n}", InheritanceStyle); 14361 verifyFormat("int x = a ? b : c;", InheritanceStyle); 14362 verifyFormat("{\n" 14363 "label2:\n" 14364 " int x = 0;\n" 14365 "}", 14366 InheritanceStyle); 14367 verifyFormat("switch (x) {\n" 14368 "case 1:\n" 14369 "default:\n" 14370 "}", 14371 InheritanceStyle); 14372 verifyFormat("switch (allBraces) {\n" 14373 "case 1: {\n" 14374 " break;\n" 14375 "}\n" 14376 "case 2: {\n" 14377 " [[fallthrough]];\n" 14378 "}\n" 14379 "default: {\n" 14380 " break;\n" 14381 "}\n" 14382 "}", 14383 InheritanceStyle); 14384 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma; 14385 verifyFormat("class Foooooooooooooooooooooo\n" 14386 " : public aaaaaaaaaaaaaaaaaa,\n" 14387 " public bbbbbbbbbbbbbbbbbb {\n" 14388 "}", 14389 InheritanceStyle); 14390 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon; 14391 verifyFormat("class Foooooooooooooooooooooo:\n" 14392 " public aaaaaaaaaaaaaaaaaa,\n" 14393 " public bbbbbbbbbbbbbbbbbb {\n" 14394 "}", 14395 InheritanceStyle); 14396 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 14397 verifyFormat("class Foooooooooooooooooooooo\n" 14398 " : public aaaaaaaaaaaaaaaaaa\n" 14399 " , public bbbbbbbbbbbbbbbbbb {\n" 14400 "}", 14401 InheritanceStyle); 14402 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 14403 verifyFormat("class Foooooooooooooooooooooo\n" 14404 " : public aaaaaaaaaaaaaaaaaa,\n" 14405 " public bbbbbbbbbbbbbbbbbb {\n" 14406 "}", 14407 InheritanceStyle); 14408 InheritanceStyle.ConstructorInitializerIndentWidth = 0; 14409 verifyFormat("class Foooooooooooooooooooooo\n" 14410 ": public aaaaaaaaaaaaaaaaaa,\n" 14411 " public bbbbbbbbbbbbbbbbbb {}", 14412 InheritanceStyle); 14413 14414 FormatStyle ForLoopStyle = getLLVMStyle(); 14415 ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false; 14416 verifyFormat("class Foo : public Bar {};", ForLoopStyle); 14417 verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle); 14418 verifyFormat("for (auto a: b) {\n}", ForLoopStyle); 14419 verifyFormat("int x = a ? b : c;", ForLoopStyle); 14420 verifyFormat("{\n" 14421 "label2:\n" 14422 " int x = 0;\n" 14423 "}", 14424 ForLoopStyle); 14425 verifyFormat("switch (x) {\n" 14426 "case 1:\n" 14427 "default:\n" 14428 "}", 14429 ForLoopStyle); 14430 verifyFormat("switch (allBraces) {\n" 14431 "case 1: {\n" 14432 " break;\n" 14433 "}\n" 14434 "case 2: {\n" 14435 " [[fallthrough]];\n" 14436 "}\n" 14437 "default: {\n" 14438 " break;\n" 14439 "}\n" 14440 "}", 14441 ForLoopStyle); 14442 14443 FormatStyle CaseStyle = getLLVMStyle(); 14444 CaseStyle.SpaceBeforeCaseColon = true; 14445 verifyFormat("class Foo : public Bar {};", CaseStyle); 14446 verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle); 14447 verifyFormat("for (auto a : b) {\n}", CaseStyle); 14448 verifyFormat("int x = a ? b : c;", CaseStyle); 14449 verifyFormat("switch (x) {\n" 14450 "case 1 :\n" 14451 "default :\n" 14452 "}", 14453 CaseStyle); 14454 verifyFormat("switch (allBraces) {\n" 14455 "case 1 : {\n" 14456 " break;\n" 14457 "}\n" 14458 "case 2 : {\n" 14459 " [[fallthrough]];\n" 14460 "}\n" 14461 "default : {\n" 14462 " break;\n" 14463 "}\n" 14464 "}", 14465 CaseStyle); 14466 14467 FormatStyle NoSpaceStyle = getLLVMStyle(); 14468 EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false); 14469 NoSpaceStyle.SpaceBeforeCtorInitializerColon = false; 14470 NoSpaceStyle.SpaceBeforeInheritanceColon = false; 14471 NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 14472 verifyFormat("class Foo: public Bar {};", NoSpaceStyle); 14473 verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle); 14474 verifyFormat("for (auto a: b) {\n}", NoSpaceStyle); 14475 verifyFormat("int x = a ? b : c;", NoSpaceStyle); 14476 verifyFormat("{\n" 14477 "label3:\n" 14478 " int x = 0;\n" 14479 "}", 14480 NoSpaceStyle); 14481 verifyFormat("switch (x) {\n" 14482 "case 1:\n" 14483 "default:\n" 14484 "}", 14485 NoSpaceStyle); 14486 verifyFormat("switch (allBraces) {\n" 14487 "case 1: {\n" 14488 " break;\n" 14489 "}\n" 14490 "case 2: {\n" 14491 " [[fallthrough]];\n" 14492 "}\n" 14493 "default: {\n" 14494 " break;\n" 14495 "}\n" 14496 "}", 14497 NoSpaceStyle); 14498 14499 FormatStyle InvertedSpaceStyle = getLLVMStyle(); 14500 InvertedSpaceStyle.SpaceBeforeCaseColon = true; 14501 InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false; 14502 InvertedSpaceStyle.SpaceBeforeInheritanceColon = false; 14503 InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 14504 verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle); 14505 verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle); 14506 verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle); 14507 verifyFormat("int x = a ? b : c;", InvertedSpaceStyle); 14508 verifyFormat("{\n" 14509 "label3:\n" 14510 " int x = 0;\n" 14511 "}", 14512 InvertedSpaceStyle); 14513 verifyFormat("switch (x) {\n" 14514 "case 1 :\n" 14515 "case 2 : {\n" 14516 " break;\n" 14517 "}\n" 14518 "default :\n" 14519 " break;\n" 14520 "}", 14521 InvertedSpaceStyle); 14522 verifyFormat("switch (allBraces) {\n" 14523 "case 1 : {\n" 14524 " break;\n" 14525 "}\n" 14526 "case 2 : {\n" 14527 " [[fallthrough]];\n" 14528 "}\n" 14529 "default : {\n" 14530 " break;\n" 14531 "}\n" 14532 "}", 14533 InvertedSpaceStyle); 14534 } 14535 14536 TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) { 14537 FormatStyle Style = getLLVMStyle(); 14538 14539 Style.PointerAlignment = FormatStyle::PAS_Left; 14540 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; 14541 verifyFormat("void* const* x = NULL;", Style); 14542 14543 #define verifyQualifierSpaces(Code, Pointers, Qualifiers) \ 14544 do { \ 14545 Style.PointerAlignment = FormatStyle::Pointers; \ 14546 Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers; \ 14547 verifyFormat(Code, Style); \ 14548 } while (false) 14549 14550 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default); 14551 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default); 14552 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default); 14553 14554 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before); 14555 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before); 14556 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before); 14557 14558 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After); 14559 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After); 14560 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After); 14561 14562 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both); 14563 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both); 14564 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both); 14565 14566 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default); 14567 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, 14568 SAPQ_Default); 14569 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 14570 SAPQ_Default); 14571 14572 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before); 14573 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, 14574 SAPQ_Before); 14575 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 14576 SAPQ_Before); 14577 14578 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After); 14579 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After); 14580 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 14581 SAPQ_After); 14582 14583 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both); 14584 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both); 14585 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both); 14586 14587 #undef verifyQualifierSpaces 14588 14589 FormatStyle Spaces = getLLVMStyle(); 14590 Spaces.AttributeMacros.push_back("qualified"); 14591 Spaces.PointerAlignment = FormatStyle::PAS_Right; 14592 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; 14593 verifyFormat("SomeType *volatile *a = NULL;", Spaces); 14594 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces); 14595 verifyFormat("std::vector<SomeType *const *> x;", Spaces); 14596 verifyFormat("std::vector<SomeType *qualified *> x;", Spaces); 14597 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 14598 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before; 14599 verifyFormat("SomeType * volatile *a = NULL;", Spaces); 14600 verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces); 14601 verifyFormat("std::vector<SomeType * const *> x;", Spaces); 14602 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces); 14603 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 14604 14605 // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left. 14606 Spaces.PointerAlignment = FormatStyle::PAS_Left; 14607 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before; 14608 verifyFormat("SomeType* volatile* a = NULL;", Spaces); 14609 verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces); 14610 verifyFormat("std::vector<SomeType* const*> x;", Spaces); 14611 verifyFormat("std::vector<SomeType* qualified*> x;", Spaces); 14612 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 14613 // However, setting it to SAPQ_After should add spaces after __attribute, etc. 14614 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After; 14615 verifyFormat("SomeType* volatile * a = NULL;", Spaces); 14616 verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces); 14617 verifyFormat("std::vector<SomeType* const *> x;", Spaces); 14618 verifyFormat("std::vector<SomeType* qualified *> x;", Spaces); 14619 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 14620 14621 // PAS_Middle should not have any noticeable changes even for SAPQ_Both 14622 Spaces.PointerAlignment = FormatStyle::PAS_Middle; 14623 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After; 14624 verifyFormat("SomeType * volatile * a = NULL;", Spaces); 14625 verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces); 14626 verifyFormat("std::vector<SomeType * const *> x;", Spaces); 14627 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces); 14628 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 14629 } 14630 14631 TEST_F(FormatTest, AlignConsecutiveMacros) { 14632 FormatStyle Style = getLLVMStyle(); 14633 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 14634 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 14635 Style.AlignConsecutiveMacros = FormatStyle::ACS_None; 14636 14637 verifyFormat("#define a 3\n" 14638 "#define bbbb 4\n" 14639 "#define ccc (5)", 14640 Style); 14641 14642 verifyFormat("#define f(x) (x * x)\n" 14643 "#define fff(x, y, z) (x * y + z)\n" 14644 "#define ffff(x, y) (x - y)", 14645 Style); 14646 14647 verifyFormat("#define foo(x, y) (x + y)\n" 14648 "#define bar (5, 6)(2 + 2)", 14649 Style); 14650 14651 verifyFormat("#define a 3\n" 14652 "#define bbbb 4\n" 14653 "#define ccc (5)\n" 14654 "#define f(x) (x * x)\n" 14655 "#define fff(x, y, z) (x * y + z)\n" 14656 "#define ffff(x, y) (x - y)", 14657 Style); 14658 14659 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 14660 verifyFormat("#define a 3\n" 14661 "#define bbbb 4\n" 14662 "#define ccc (5)", 14663 Style); 14664 14665 verifyFormat("#define f(x) (x * x)\n" 14666 "#define fff(x, y, z) (x * y + z)\n" 14667 "#define ffff(x, y) (x - y)", 14668 Style); 14669 14670 verifyFormat("#define foo(x, y) (x + y)\n" 14671 "#define bar (5, 6)(2 + 2)", 14672 Style); 14673 14674 verifyFormat("#define a 3\n" 14675 "#define bbbb 4\n" 14676 "#define ccc (5)\n" 14677 "#define f(x) (x * x)\n" 14678 "#define fff(x, y, z) (x * y + z)\n" 14679 "#define ffff(x, y) (x - y)", 14680 Style); 14681 14682 verifyFormat("#define a 5\n" 14683 "#define foo(x, y) (x + y)\n" 14684 "#define CCC (6)\n" 14685 "auto lambda = []() {\n" 14686 " auto ii = 0;\n" 14687 " float j = 0;\n" 14688 " return 0;\n" 14689 "};\n" 14690 "int i = 0;\n" 14691 "float i2 = 0;\n" 14692 "auto v = type{\n" 14693 " i = 1, //\n" 14694 " (i = 2), //\n" 14695 " i = 3 //\n" 14696 "};", 14697 Style); 14698 14699 Style.AlignConsecutiveMacros = FormatStyle::ACS_None; 14700 Style.ColumnLimit = 20; 14701 14702 verifyFormat("#define a \\\n" 14703 " \"aabbbbbbbbbbbb\"\n" 14704 "#define D \\\n" 14705 " \"aabbbbbbbbbbbb\" \\\n" 14706 " \"ccddeeeeeeeee\"\n" 14707 "#define B \\\n" 14708 " \"QQQQQQQQQQQQQ\" \\\n" 14709 " \"FFFFFFFFFFFFF\" \\\n" 14710 " \"LLLLLLLL\"\n", 14711 Style); 14712 14713 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 14714 verifyFormat("#define a \\\n" 14715 " \"aabbbbbbbbbbbb\"\n" 14716 "#define D \\\n" 14717 " \"aabbbbbbbbbbbb\" \\\n" 14718 " \"ccddeeeeeeeee\"\n" 14719 "#define B \\\n" 14720 " \"QQQQQQQQQQQQQ\" \\\n" 14721 " \"FFFFFFFFFFFFF\" \\\n" 14722 " \"LLLLLLLL\"\n", 14723 Style); 14724 14725 // Test across comments 14726 Style.MaxEmptyLinesToKeep = 10; 14727 Style.ReflowComments = false; 14728 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossComments; 14729 EXPECT_EQ("#define a 3\n" 14730 "// line comment\n" 14731 "#define bbbb 4\n" 14732 "#define ccc (5)", 14733 format("#define a 3\n" 14734 "// line comment\n" 14735 "#define bbbb 4\n" 14736 "#define ccc (5)", 14737 Style)); 14738 14739 EXPECT_EQ("#define a 3\n" 14740 "/* block comment */\n" 14741 "#define bbbb 4\n" 14742 "#define ccc (5)", 14743 format("#define a 3\n" 14744 "/* block comment */\n" 14745 "#define bbbb 4\n" 14746 "#define ccc (5)", 14747 Style)); 14748 14749 EXPECT_EQ("#define a 3\n" 14750 "/* multi-line *\n" 14751 " * block comment */\n" 14752 "#define bbbb 4\n" 14753 "#define ccc (5)", 14754 format("#define a 3\n" 14755 "/* multi-line *\n" 14756 " * block comment */\n" 14757 "#define bbbb 4\n" 14758 "#define ccc (5)", 14759 Style)); 14760 14761 EXPECT_EQ("#define a 3\n" 14762 "// multi-line line comment\n" 14763 "//\n" 14764 "#define bbbb 4\n" 14765 "#define ccc (5)", 14766 format("#define a 3\n" 14767 "// multi-line line comment\n" 14768 "//\n" 14769 "#define bbbb 4\n" 14770 "#define ccc (5)", 14771 Style)); 14772 14773 EXPECT_EQ("#define a 3\n" 14774 "// empty lines still break.\n" 14775 "\n" 14776 "#define bbbb 4\n" 14777 "#define ccc (5)", 14778 format("#define a 3\n" 14779 "// empty lines still break.\n" 14780 "\n" 14781 "#define bbbb 4\n" 14782 "#define ccc (5)", 14783 Style)); 14784 14785 // Test across empty lines 14786 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLines; 14787 EXPECT_EQ("#define a 3\n" 14788 "\n" 14789 "#define bbbb 4\n" 14790 "#define ccc (5)", 14791 format("#define a 3\n" 14792 "\n" 14793 "#define bbbb 4\n" 14794 "#define ccc (5)", 14795 Style)); 14796 14797 EXPECT_EQ("#define a 3\n" 14798 "\n" 14799 "\n" 14800 "\n" 14801 "#define bbbb 4\n" 14802 "#define ccc (5)", 14803 format("#define a 3\n" 14804 "\n" 14805 "\n" 14806 "\n" 14807 "#define bbbb 4\n" 14808 "#define ccc (5)", 14809 Style)); 14810 14811 EXPECT_EQ("#define a 3\n" 14812 "// comments should break alignment\n" 14813 "//\n" 14814 "#define bbbb 4\n" 14815 "#define ccc (5)", 14816 format("#define a 3\n" 14817 "// comments should break alignment\n" 14818 "//\n" 14819 "#define bbbb 4\n" 14820 "#define ccc (5)", 14821 Style)); 14822 14823 // Test across empty lines and comments 14824 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLinesAndComments; 14825 verifyFormat("#define a 3\n" 14826 "\n" 14827 "// line comment\n" 14828 "#define bbbb 4\n" 14829 "#define ccc (5)", 14830 Style); 14831 14832 EXPECT_EQ("#define a 3\n" 14833 "\n" 14834 "\n" 14835 "/* multi-line *\n" 14836 " * block comment */\n" 14837 "\n" 14838 "\n" 14839 "#define bbbb 4\n" 14840 "#define ccc (5)", 14841 format("#define a 3\n" 14842 "\n" 14843 "\n" 14844 "/* multi-line *\n" 14845 " * block comment */\n" 14846 "\n" 14847 "\n" 14848 "#define bbbb 4\n" 14849 "#define ccc (5)", 14850 Style)); 14851 14852 EXPECT_EQ("#define a 3\n" 14853 "\n" 14854 "\n" 14855 "/* multi-line *\n" 14856 " * block comment */\n" 14857 "\n" 14858 "\n" 14859 "#define bbbb 4\n" 14860 "#define ccc (5)", 14861 format("#define a 3\n" 14862 "\n" 14863 "\n" 14864 "/* multi-line *\n" 14865 " * block comment */\n" 14866 "\n" 14867 "\n" 14868 "#define bbbb 4\n" 14869 "#define ccc (5)", 14870 Style)); 14871 } 14872 14873 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) { 14874 FormatStyle Alignment = getLLVMStyle(); 14875 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 14876 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossEmptyLines; 14877 14878 Alignment.MaxEmptyLinesToKeep = 10; 14879 /* Test alignment across empty lines */ 14880 EXPECT_EQ("int a = 5;\n" 14881 "\n" 14882 "int oneTwoThree = 123;", 14883 format("int a = 5;\n" 14884 "\n" 14885 "int oneTwoThree= 123;", 14886 Alignment)); 14887 EXPECT_EQ("int a = 5;\n" 14888 "int one = 1;\n" 14889 "\n" 14890 "int oneTwoThree = 123;", 14891 format("int a = 5;\n" 14892 "int one = 1;\n" 14893 "\n" 14894 "int oneTwoThree = 123;", 14895 Alignment)); 14896 EXPECT_EQ("int a = 5;\n" 14897 "int one = 1;\n" 14898 "\n" 14899 "int oneTwoThree = 123;\n" 14900 "int oneTwo = 12;", 14901 format("int a = 5;\n" 14902 "int one = 1;\n" 14903 "\n" 14904 "int oneTwoThree = 123;\n" 14905 "int oneTwo = 12;", 14906 Alignment)); 14907 14908 /* Test across comments */ 14909 EXPECT_EQ("int a = 5;\n" 14910 "/* block comment */\n" 14911 "int oneTwoThree = 123;", 14912 format("int a = 5;\n" 14913 "/* block comment */\n" 14914 "int oneTwoThree=123;", 14915 Alignment)); 14916 14917 EXPECT_EQ("int a = 5;\n" 14918 "// line comment\n" 14919 "int oneTwoThree = 123;", 14920 format("int a = 5;\n" 14921 "// line comment\n" 14922 "int oneTwoThree=123;", 14923 Alignment)); 14924 14925 /* Test across comments and newlines */ 14926 EXPECT_EQ("int a = 5;\n" 14927 "\n" 14928 "/* block comment */\n" 14929 "int oneTwoThree = 123;", 14930 format("int a = 5;\n" 14931 "\n" 14932 "/* block comment */\n" 14933 "int oneTwoThree=123;", 14934 Alignment)); 14935 14936 EXPECT_EQ("int a = 5;\n" 14937 "\n" 14938 "// line comment\n" 14939 "int oneTwoThree = 123;", 14940 format("int a = 5;\n" 14941 "\n" 14942 "// line comment\n" 14943 "int oneTwoThree=123;", 14944 Alignment)); 14945 } 14946 14947 TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) { 14948 FormatStyle Alignment = getLLVMStyle(); 14949 Alignment.AlignConsecutiveDeclarations = 14950 FormatStyle::ACS_AcrossEmptyLinesAndComments; 14951 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 14952 14953 Alignment.MaxEmptyLinesToKeep = 10; 14954 /* Test alignment across empty lines */ 14955 EXPECT_EQ("int a = 5;\n" 14956 "\n" 14957 "float const oneTwoThree = 123;", 14958 format("int a = 5;\n" 14959 "\n" 14960 "float const oneTwoThree = 123;", 14961 Alignment)); 14962 EXPECT_EQ("int a = 5;\n" 14963 "float const one = 1;\n" 14964 "\n" 14965 "int oneTwoThree = 123;", 14966 format("int a = 5;\n" 14967 "float const one = 1;\n" 14968 "\n" 14969 "int oneTwoThree = 123;", 14970 Alignment)); 14971 14972 /* Test across comments */ 14973 EXPECT_EQ("float const a = 5;\n" 14974 "/* block comment */\n" 14975 "int oneTwoThree = 123;", 14976 format("float const a = 5;\n" 14977 "/* block comment */\n" 14978 "int oneTwoThree=123;", 14979 Alignment)); 14980 14981 EXPECT_EQ("float const a = 5;\n" 14982 "// line comment\n" 14983 "int oneTwoThree = 123;", 14984 format("float const a = 5;\n" 14985 "// line comment\n" 14986 "int oneTwoThree=123;", 14987 Alignment)); 14988 14989 /* Test across comments and newlines */ 14990 EXPECT_EQ("float const a = 5;\n" 14991 "\n" 14992 "/* block comment */\n" 14993 "int oneTwoThree = 123;", 14994 format("float const a = 5;\n" 14995 "\n" 14996 "/* block comment */\n" 14997 "int oneTwoThree=123;", 14998 Alignment)); 14999 15000 EXPECT_EQ("float const a = 5;\n" 15001 "\n" 15002 "// line comment\n" 15003 "int oneTwoThree = 123;", 15004 format("float const a = 5;\n" 15005 "\n" 15006 "// line comment\n" 15007 "int oneTwoThree=123;", 15008 Alignment)); 15009 } 15010 15011 TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) { 15012 FormatStyle Alignment = getLLVMStyle(); 15013 Alignment.AlignConsecutiveBitFields = 15014 FormatStyle::ACS_AcrossEmptyLinesAndComments; 15015 15016 Alignment.MaxEmptyLinesToKeep = 10; 15017 /* Test alignment across empty lines */ 15018 EXPECT_EQ("int a : 5;\n" 15019 "\n" 15020 "int longbitfield : 6;", 15021 format("int a : 5;\n" 15022 "\n" 15023 "int longbitfield : 6;", 15024 Alignment)); 15025 EXPECT_EQ("int a : 5;\n" 15026 "int one : 1;\n" 15027 "\n" 15028 "int longbitfield : 6;", 15029 format("int a : 5;\n" 15030 "int one : 1;\n" 15031 "\n" 15032 "int longbitfield : 6;", 15033 Alignment)); 15034 15035 /* Test across comments */ 15036 EXPECT_EQ("int a : 5;\n" 15037 "/* block comment */\n" 15038 "int longbitfield : 6;", 15039 format("int a : 5;\n" 15040 "/* block comment */\n" 15041 "int longbitfield : 6;", 15042 Alignment)); 15043 EXPECT_EQ("int a : 5;\n" 15044 "int one : 1;\n" 15045 "// line comment\n" 15046 "int longbitfield : 6;", 15047 format("int a : 5;\n" 15048 "int one : 1;\n" 15049 "// line comment\n" 15050 "int longbitfield : 6;", 15051 Alignment)); 15052 15053 /* Test across comments and newlines */ 15054 EXPECT_EQ("int a : 5;\n" 15055 "/* block comment */\n" 15056 "\n" 15057 "int longbitfield : 6;", 15058 format("int a : 5;\n" 15059 "/* block comment */\n" 15060 "\n" 15061 "int longbitfield : 6;", 15062 Alignment)); 15063 EXPECT_EQ("int a : 5;\n" 15064 "int one : 1;\n" 15065 "\n" 15066 "// line comment\n" 15067 "\n" 15068 "int longbitfield : 6;", 15069 format("int a : 5;\n" 15070 "int one : 1;\n" 15071 "\n" 15072 "// line comment \n" 15073 "\n" 15074 "int longbitfield : 6;", 15075 Alignment)); 15076 } 15077 15078 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) { 15079 FormatStyle Alignment = getLLVMStyle(); 15080 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15081 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossComments; 15082 15083 Alignment.MaxEmptyLinesToKeep = 10; 15084 /* Test alignment across empty lines */ 15085 EXPECT_EQ("int a = 5;\n" 15086 "\n" 15087 "int oneTwoThree = 123;", 15088 format("int a = 5;\n" 15089 "\n" 15090 "int oneTwoThree= 123;", 15091 Alignment)); 15092 EXPECT_EQ("int a = 5;\n" 15093 "int one = 1;\n" 15094 "\n" 15095 "int oneTwoThree = 123;", 15096 format("int a = 5;\n" 15097 "int one = 1;\n" 15098 "\n" 15099 "int oneTwoThree = 123;", 15100 Alignment)); 15101 15102 /* Test across comments */ 15103 EXPECT_EQ("int a = 5;\n" 15104 "/* block comment */\n" 15105 "int oneTwoThree = 123;", 15106 format("int a = 5;\n" 15107 "/* block comment */\n" 15108 "int oneTwoThree=123;", 15109 Alignment)); 15110 15111 EXPECT_EQ("int a = 5;\n" 15112 "// line comment\n" 15113 "int oneTwoThree = 123;", 15114 format("int a = 5;\n" 15115 "// line comment\n" 15116 "int oneTwoThree=123;", 15117 Alignment)); 15118 15119 EXPECT_EQ("int a = 5;\n" 15120 "/*\n" 15121 " * multi-line block comment\n" 15122 " */\n" 15123 "int oneTwoThree = 123;", 15124 format("int a = 5;\n" 15125 "/*\n" 15126 " * multi-line block comment\n" 15127 " */\n" 15128 "int oneTwoThree=123;", 15129 Alignment)); 15130 15131 EXPECT_EQ("int a = 5;\n" 15132 "//\n" 15133 "// multi-line line comment\n" 15134 "//\n" 15135 "int oneTwoThree = 123;", 15136 format("int a = 5;\n" 15137 "//\n" 15138 "// multi-line line comment\n" 15139 "//\n" 15140 "int oneTwoThree=123;", 15141 Alignment)); 15142 15143 /* Test across comments and newlines */ 15144 EXPECT_EQ("int a = 5;\n" 15145 "\n" 15146 "/* block comment */\n" 15147 "int oneTwoThree = 123;", 15148 format("int a = 5;\n" 15149 "\n" 15150 "/* block comment */\n" 15151 "int oneTwoThree=123;", 15152 Alignment)); 15153 15154 EXPECT_EQ("int a = 5;\n" 15155 "\n" 15156 "// line comment\n" 15157 "int oneTwoThree = 123;", 15158 format("int a = 5;\n" 15159 "\n" 15160 "// line comment\n" 15161 "int oneTwoThree=123;", 15162 Alignment)); 15163 } 15164 15165 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) { 15166 FormatStyle Alignment = getLLVMStyle(); 15167 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15168 Alignment.AlignConsecutiveAssignments = 15169 FormatStyle::ACS_AcrossEmptyLinesAndComments; 15170 verifyFormat("int a = 5;\n" 15171 "int oneTwoThree = 123;", 15172 Alignment); 15173 verifyFormat("int a = method();\n" 15174 "int oneTwoThree = 133;", 15175 Alignment); 15176 verifyFormat("a &= 5;\n" 15177 "bcd *= 5;\n" 15178 "ghtyf += 5;\n" 15179 "dvfvdb -= 5;\n" 15180 "a /= 5;\n" 15181 "vdsvsv %= 5;\n" 15182 "sfdbddfbdfbb ^= 5;\n" 15183 "dvsdsv |= 5;\n" 15184 "int dsvvdvsdvvv = 123;", 15185 Alignment); 15186 verifyFormat("int i = 1, j = 10;\n" 15187 "something = 2000;", 15188 Alignment); 15189 verifyFormat("something = 2000;\n" 15190 "int i = 1, j = 10;\n", 15191 Alignment); 15192 verifyFormat("something = 2000;\n" 15193 "another = 911;\n" 15194 "int i = 1, j = 10;\n" 15195 "oneMore = 1;\n" 15196 "i = 2;", 15197 Alignment); 15198 verifyFormat("int a = 5;\n" 15199 "int one = 1;\n" 15200 "method();\n" 15201 "int oneTwoThree = 123;\n" 15202 "int oneTwo = 12;", 15203 Alignment); 15204 verifyFormat("int oneTwoThree = 123;\n" 15205 "int oneTwo = 12;\n" 15206 "method();\n", 15207 Alignment); 15208 verifyFormat("int oneTwoThree = 123; // comment\n" 15209 "int oneTwo = 12; // comment", 15210 Alignment); 15211 15212 // Bug 25167 15213 /* Uncomment when fixed 15214 verifyFormat("#if A\n" 15215 "#else\n" 15216 "int aaaaaaaa = 12;\n" 15217 "#endif\n" 15218 "#if B\n" 15219 "#else\n" 15220 "int a = 12;\n" 15221 "#endif\n", 15222 Alignment); 15223 verifyFormat("enum foo {\n" 15224 "#if A\n" 15225 "#else\n" 15226 " aaaaaaaa = 12;\n" 15227 "#endif\n" 15228 "#if B\n" 15229 "#else\n" 15230 " a = 12;\n" 15231 "#endif\n" 15232 "};\n", 15233 Alignment); 15234 */ 15235 15236 Alignment.MaxEmptyLinesToKeep = 10; 15237 /* Test alignment across empty lines */ 15238 EXPECT_EQ("int a = 5;\n" 15239 "\n" 15240 "int oneTwoThree = 123;", 15241 format("int a = 5;\n" 15242 "\n" 15243 "int oneTwoThree= 123;", 15244 Alignment)); 15245 EXPECT_EQ("int a = 5;\n" 15246 "int one = 1;\n" 15247 "\n" 15248 "int oneTwoThree = 123;", 15249 format("int a = 5;\n" 15250 "int one = 1;\n" 15251 "\n" 15252 "int oneTwoThree = 123;", 15253 Alignment)); 15254 EXPECT_EQ("int a = 5;\n" 15255 "int one = 1;\n" 15256 "\n" 15257 "int oneTwoThree = 123;\n" 15258 "int oneTwo = 12;", 15259 format("int a = 5;\n" 15260 "int one = 1;\n" 15261 "\n" 15262 "int oneTwoThree = 123;\n" 15263 "int oneTwo = 12;", 15264 Alignment)); 15265 15266 /* Test across comments */ 15267 EXPECT_EQ("int a = 5;\n" 15268 "/* block comment */\n" 15269 "int oneTwoThree = 123;", 15270 format("int a = 5;\n" 15271 "/* block comment */\n" 15272 "int oneTwoThree=123;", 15273 Alignment)); 15274 15275 EXPECT_EQ("int a = 5;\n" 15276 "// line comment\n" 15277 "int oneTwoThree = 123;", 15278 format("int a = 5;\n" 15279 "// line comment\n" 15280 "int oneTwoThree=123;", 15281 Alignment)); 15282 15283 /* Test across comments and newlines */ 15284 EXPECT_EQ("int a = 5;\n" 15285 "\n" 15286 "/* block comment */\n" 15287 "int oneTwoThree = 123;", 15288 format("int a = 5;\n" 15289 "\n" 15290 "/* block comment */\n" 15291 "int oneTwoThree=123;", 15292 Alignment)); 15293 15294 EXPECT_EQ("int a = 5;\n" 15295 "\n" 15296 "// line comment\n" 15297 "int oneTwoThree = 123;", 15298 format("int a = 5;\n" 15299 "\n" 15300 "// line comment\n" 15301 "int oneTwoThree=123;", 15302 Alignment)); 15303 15304 EXPECT_EQ("int a = 5;\n" 15305 "//\n" 15306 "// multi-line line comment\n" 15307 "//\n" 15308 "int oneTwoThree = 123;", 15309 format("int a = 5;\n" 15310 "//\n" 15311 "// multi-line line comment\n" 15312 "//\n" 15313 "int oneTwoThree=123;", 15314 Alignment)); 15315 15316 EXPECT_EQ("int a = 5;\n" 15317 "/*\n" 15318 " * multi-line block comment\n" 15319 " */\n" 15320 "int oneTwoThree = 123;", 15321 format("int a = 5;\n" 15322 "/*\n" 15323 " * multi-line block comment\n" 15324 " */\n" 15325 "int oneTwoThree=123;", 15326 Alignment)); 15327 15328 EXPECT_EQ("int a = 5;\n" 15329 "\n" 15330 "/* block comment */\n" 15331 "\n" 15332 "\n" 15333 "\n" 15334 "int oneTwoThree = 123;", 15335 format("int a = 5;\n" 15336 "\n" 15337 "/* block comment */\n" 15338 "\n" 15339 "\n" 15340 "\n" 15341 "int oneTwoThree=123;", 15342 Alignment)); 15343 15344 EXPECT_EQ("int a = 5;\n" 15345 "\n" 15346 "// line comment\n" 15347 "\n" 15348 "\n" 15349 "\n" 15350 "int oneTwoThree = 123;", 15351 format("int a = 5;\n" 15352 "\n" 15353 "// line comment\n" 15354 "\n" 15355 "\n" 15356 "\n" 15357 "int oneTwoThree=123;", 15358 Alignment)); 15359 15360 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 15361 verifyFormat("#define A \\\n" 15362 " int aaaa = 12; \\\n" 15363 " int b = 23; \\\n" 15364 " int ccc = 234; \\\n" 15365 " int dddddddddd = 2345;", 15366 Alignment); 15367 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 15368 verifyFormat("#define A \\\n" 15369 " int aaaa = 12; \\\n" 15370 " int b = 23; \\\n" 15371 " int ccc = 234; \\\n" 15372 " int dddddddddd = 2345;", 15373 Alignment); 15374 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 15375 verifyFormat("#define A " 15376 " \\\n" 15377 " int aaaa = 12; " 15378 " \\\n" 15379 " int b = 23; " 15380 " \\\n" 15381 " int ccc = 234; " 15382 " \\\n" 15383 " int dddddddddd = 2345;", 15384 Alignment); 15385 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 15386 "k = 4, int l = 5,\n" 15387 " int m = 6) {\n" 15388 " int j = 10;\n" 15389 " otherThing = 1;\n" 15390 "}", 15391 Alignment); 15392 verifyFormat("void SomeFunction(int parameter = 0) {\n" 15393 " int i = 1;\n" 15394 " int j = 2;\n" 15395 " int big = 10000;\n" 15396 "}", 15397 Alignment); 15398 verifyFormat("class C {\n" 15399 "public:\n" 15400 " int i = 1;\n" 15401 " virtual void f() = 0;\n" 15402 "};", 15403 Alignment); 15404 verifyFormat("int i = 1;\n" 15405 "if (SomeType t = getSomething()) {\n" 15406 "}\n" 15407 "int j = 2;\n" 15408 "int big = 10000;", 15409 Alignment); 15410 verifyFormat("int j = 7;\n" 15411 "for (int k = 0; k < N; ++k) {\n" 15412 "}\n" 15413 "int j = 2;\n" 15414 "int big = 10000;\n" 15415 "}", 15416 Alignment); 15417 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 15418 verifyFormat("int i = 1;\n" 15419 "LooooooooooongType loooooooooooooooooooooongVariable\n" 15420 " = someLooooooooooooooooongFunction();\n" 15421 "int j = 2;", 15422 Alignment); 15423 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 15424 verifyFormat("int i = 1;\n" 15425 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 15426 " someLooooooooooooooooongFunction();\n" 15427 "int j = 2;", 15428 Alignment); 15429 15430 verifyFormat("auto lambda = []() {\n" 15431 " auto i = 0;\n" 15432 " return 0;\n" 15433 "};\n" 15434 "int i = 0;\n" 15435 "auto v = type{\n" 15436 " i = 1, //\n" 15437 " (i = 2), //\n" 15438 " i = 3 //\n" 15439 "};", 15440 Alignment); 15441 15442 verifyFormat( 15443 "int i = 1;\n" 15444 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 15445 " loooooooooooooooooooooongParameterB);\n" 15446 "int j = 2;", 15447 Alignment); 15448 15449 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 15450 " typename B = very_long_type_name_1,\n" 15451 " typename T_2 = very_long_type_name_2>\n" 15452 "auto foo() {}\n", 15453 Alignment); 15454 verifyFormat("int a, b = 1;\n" 15455 "int c = 2;\n" 15456 "int dd = 3;\n", 15457 Alignment); 15458 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 15459 "float b[1][] = {{3.f}};\n", 15460 Alignment); 15461 verifyFormat("for (int i = 0; i < 1; i++)\n" 15462 " int x = 1;\n", 15463 Alignment); 15464 verifyFormat("for (i = 0; i < 1; i++)\n" 15465 " x = 1;\n" 15466 "y = 1;\n", 15467 Alignment); 15468 15469 Alignment.ReflowComments = true; 15470 Alignment.ColumnLimit = 50; 15471 EXPECT_EQ("int x = 0;\n" 15472 "int yy = 1; /// specificlennospace\n" 15473 "int zzz = 2;\n", 15474 format("int x = 0;\n" 15475 "int yy = 1; ///specificlennospace\n" 15476 "int zzz = 2;\n", 15477 Alignment)); 15478 } 15479 15480 TEST_F(FormatTest, AlignConsecutiveAssignments) { 15481 FormatStyle Alignment = getLLVMStyle(); 15482 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15483 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 15484 verifyFormat("int a = 5;\n" 15485 "int oneTwoThree = 123;", 15486 Alignment); 15487 verifyFormat("int a = 5;\n" 15488 "int oneTwoThree = 123;", 15489 Alignment); 15490 15491 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 15492 verifyFormat("int a = 5;\n" 15493 "int oneTwoThree = 123;", 15494 Alignment); 15495 verifyFormat("int a = method();\n" 15496 "int oneTwoThree = 133;", 15497 Alignment); 15498 verifyFormat("a &= 5;\n" 15499 "bcd *= 5;\n" 15500 "ghtyf += 5;\n" 15501 "dvfvdb -= 5;\n" 15502 "a /= 5;\n" 15503 "vdsvsv %= 5;\n" 15504 "sfdbddfbdfbb ^= 5;\n" 15505 "dvsdsv |= 5;\n" 15506 "int dsvvdvsdvvv = 123;", 15507 Alignment); 15508 verifyFormat("int i = 1, j = 10;\n" 15509 "something = 2000;", 15510 Alignment); 15511 verifyFormat("something = 2000;\n" 15512 "int i = 1, j = 10;\n", 15513 Alignment); 15514 verifyFormat("something = 2000;\n" 15515 "another = 911;\n" 15516 "int i = 1, j = 10;\n" 15517 "oneMore = 1;\n" 15518 "i = 2;", 15519 Alignment); 15520 verifyFormat("int a = 5;\n" 15521 "int one = 1;\n" 15522 "method();\n" 15523 "int oneTwoThree = 123;\n" 15524 "int oneTwo = 12;", 15525 Alignment); 15526 verifyFormat("int oneTwoThree = 123;\n" 15527 "int oneTwo = 12;\n" 15528 "method();\n", 15529 Alignment); 15530 verifyFormat("int oneTwoThree = 123; // comment\n" 15531 "int oneTwo = 12; // comment", 15532 Alignment); 15533 15534 // Bug 25167 15535 /* Uncomment when fixed 15536 verifyFormat("#if A\n" 15537 "#else\n" 15538 "int aaaaaaaa = 12;\n" 15539 "#endif\n" 15540 "#if B\n" 15541 "#else\n" 15542 "int a = 12;\n" 15543 "#endif\n", 15544 Alignment); 15545 verifyFormat("enum foo {\n" 15546 "#if A\n" 15547 "#else\n" 15548 " aaaaaaaa = 12;\n" 15549 "#endif\n" 15550 "#if B\n" 15551 "#else\n" 15552 " a = 12;\n" 15553 "#endif\n" 15554 "};\n", 15555 Alignment); 15556 */ 15557 15558 EXPECT_EQ("int a = 5;\n" 15559 "\n" 15560 "int oneTwoThree = 123;", 15561 format("int a = 5;\n" 15562 "\n" 15563 "int oneTwoThree= 123;", 15564 Alignment)); 15565 EXPECT_EQ("int a = 5;\n" 15566 "int one = 1;\n" 15567 "\n" 15568 "int oneTwoThree = 123;", 15569 format("int a = 5;\n" 15570 "int one = 1;\n" 15571 "\n" 15572 "int oneTwoThree = 123;", 15573 Alignment)); 15574 EXPECT_EQ("int a = 5;\n" 15575 "int one = 1;\n" 15576 "\n" 15577 "int oneTwoThree = 123;\n" 15578 "int oneTwo = 12;", 15579 format("int a = 5;\n" 15580 "int one = 1;\n" 15581 "\n" 15582 "int oneTwoThree = 123;\n" 15583 "int oneTwo = 12;", 15584 Alignment)); 15585 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 15586 verifyFormat("#define A \\\n" 15587 " int aaaa = 12; \\\n" 15588 " int b = 23; \\\n" 15589 " int ccc = 234; \\\n" 15590 " int dddddddddd = 2345;", 15591 Alignment); 15592 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 15593 verifyFormat("#define A \\\n" 15594 " int aaaa = 12; \\\n" 15595 " int b = 23; \\\n" 15596 " int ccc = 234; \\\n" 15597 " int dddddddddd = 2345;", 15598 Alignment); 15599 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 15600 verifyFormat("#define A " 15601 " \\\n" 15602 " int aaaa = 12; " 15603 " \\\n" 15604 " int b = 23; " 15605 " \\\n" 15606 " int ccc = 234; " 15607 " \\\n" 15608 " int dddddddddd = 2345;", 15609 Alignment); 15610 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 15611 "k = 4, int l = 5,\n" 15612 " int m = 6) {\n" 15613 " int j = 10;\n" 15614 " otherThing = 1;\n" 15615 "}", 15616 Alignment); 15617 verifyFormat("void SomeFunction(int parameter = 0) {\n" 15618 " int i = 1;\n" 15619 " int j = 2;\n" 15620 " int big = 10000;\n" 15621 "}", 15622 Alignment); 15623 verifyFormat("class C {\n" 15624 "public:\n" 15625 " int i = 1;\n" 15626 " virtual void f() = 0;\n" 15627 "};", 15628 Alignment); 15629 verifyFormat("int i = 1;\n" 15630 "if (SomeType t = getSomething()) {\n" 15631 "}\n" 15632 "int j = 2;\n" 15633 "int big = 10000;", 15634 Alignment); 15635 verifyFormat("int j = 7;\n" 15636 "for (int k = 0; k < N; ++k) {\n" 15637 "}\n" 15638 "int j = 2;\n" 15639 "int big = 10000;\n" 15640 "}", 15641 Alignment); 15642 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 15643 verifyFormat("int i = 1;\n" 15644 "LooooooooooongType loooooooooooooooooooooongVariable\n" 15645 " = someLooooooooooooooooongFunction();\n" 15646 "int j = 2;", 15647 Alignment); 15648 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 15649 verifyFormat("int i = 1;\n" 15650 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 15651 " someLooooooooooooooooongFunction();\n" 15652 "int j = 2;", 15653 Alignment); 15654 15655 verifyFormat("auto lambda = []() {\n" 15656 " auto i = 0;\n" 15657 " return 0;\n" 15658 "};\n" 15659 "int i = 0;\n" 15660 "auto v = type{\n" 15661 " i = 1, //\n" 15662 " (i = 2), //\n" 15663 " i = 3 //\n" 15664 "};", 15665 Alignment); 15666 15667 verifyFormat( 15668 "int i = 1;\n" 15669 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 15670 " loooooooooooooooooooooongParameterB);\n" 15671 "int j = 2;", 15672 Alignment); 15673 15674 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 15675 " typename B = very_long_type_name_1,\n" 15676 " typename T_2 = very_long_type_name_2>\n" 15677 "auto foo() {}\n", 15678 Alignment); 15679 verifyFormat("int a, b = 1;\n" 15680 "int c = 2;\n" 15681 "int dd = 3;\n", 15682 Alignment); 15683 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 15684 "float b[1][] = {{3.f}};\n", 15685 Alignment); 15686 verifyFormat("for (int i = 0; i < 1; i++)\n" 15687 " int x = 1;\n", 15688 Alignment); 15689 verifyFormat("for (i = 0; i < 1; i++)\n" 15690 " x = 1;\n" 15691 "y = 1;\n", 15692 Alignment); 15693 15694 Alignment.ReflowComments = true; 15695 Alignment.ColumnLimit = 50; 15696 EXPECT_EQ("int x = 0;\n" 15697 "int yy = 1; /// specificlennospace\n" 15698 "int zzz = 2;\n", 15699 format("int x = 0;\n" 15700 "int yy = 1; ///specificlennospace\n" 15701 "int zzz = 2;\n", 15702 Alignment)); 15703 } 15704 15705 TEST_F(FormatTest, AlignConsecutiveBitFields) { 15706 FormatStyle Alignment = getLLVMStyle(); 15707 Alignment.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive; 15708 verifyFormat("int const a : 5;\n" 15709 "int oneTwoThree : 23;", 15710 Alignment); 15711 15712 // Initializers are allowed starting with c++2a 15713 verifyFormat("int const a : 5 = 1;\n" 15714 "int oneTwoThree : 23 = 0;", 15715 Alignment); 15716 15717 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 15718 verifyFormat("int const a : 5;\n" 15719 "int oneTwoThree : 23;", 15720 Alignment); 15721 15722 verifyFormat("int const a : 5; // comment\n" 15723 "int oneTwoThree : 23; // comment", 15724 Alignment); 15725 15726 verifyFormat("int const a : 5 = 1;\n" 15727 "int oneTwoThree : 23 = 0;", 15728 Alignment); 15729 15730 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 15731 verifyFormat("int const a : 5 = 1;\n" 15732 "int oneTwoThree : 23 = 0;", 15733 Alignment); 15734 verifyFormat("int const a : 5 = {1};\n" 15735 "int oneTwoThree : 23 = 0;", 15736 Alignment); 15737 15738 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None; 15739 verifyFormat("int const a :5;\n" 15740 "int oneTwoThree:23;", 15741 Alignment); 15742 15743 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before; 15744 verifyFormat("int const a :5;\n" 15745 "int oneTwoThree :23;", 15746 Alignment); 15747 15748 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After; 15749 verifyFormat("int const a : 5;\n" 15750 "int oneTwoThree: 23;", 15751 Alignment); 15752 15753 // Known limitations: ':' is only recognized as a bitfield colon when 15754 // followed by a number. 15755 /* 15756 verifyFormat("int oneTwoThree : SOME_CONSTANT;\n" 15757 "int a : 5;", 15758 Alignment); 15759 */ 15760 } 15761 15762 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 15763 FormatStyle Alignment = getLLVMStyle(); 15764 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15765 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_None; 15766 Alignment.PointerAlignment = FormatStyle::PAS_Right; 15767 verifyFormat("float const a = 5;\n" 15768 "int oneTwoThree = 123;", 15769 Alignment); 15770 verifyFormat("int a = 5;\n" 15771 "float const oneTwoThree = 123;", 15772 Alignment); 15773 15774 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 15775 verifyFormat("float const a = 5;\n" 15776 "int oneTwoThree = 123;", 15777 Alignment); 15778 verifyFormat("int a = method();\n" 15779 "float const oneTwoThree = 133;", 15780 Alignment); 15781 verifyFormat("int i = 1, j = 10;\n" 15782 "something = 2000;", 15783 Alignment); 15784 verifyFormat("something = 2000;\n" 15785 "int i = 1, j = 10;\n", 15786 Alignment); 15787 verifyFormat("float something = 2000;\n" 15788 "double another = 911;\n" 15789 "int i = 1, j = 10;\n" 15790 "const int *oneMore = 1;\n" 15791 "unsigned i = 2;", 15792 Alignment); 15793 verifyFormat("float a = 5;\n" 15794 "int one = 1;\n" 15795 "method();\n" 15796 "const double oneTwoThree = 123;\n" 15797 "const unsigned int oneTwo = 12;", 15798 Alignment); 15799 verifyFormat("int oneTwoThree{0}; // comment\n" 15800 "unsigned oneTwo; // comment", 15801 Alignment); 15802 verifyFormat("unsigned int *a;\n" 15803 "int *b;\n" 15804 "unsigned int Const *c;\n" 15805 "unsigned int const *d;\n" 15806 "unsigned int Const &e;\n" 15807 "unsigned int const &f;", 15808 Alignment); 15809 verifyFormat("Const unsigned int *c;\n" 15810 "const unsigned int *d;\n" 15811 "Const unsigned int &e;\n" 15812 "const unsigned int &f;\n" 15813 "const unsigned g;\n" 15814 "Const unsigned h;", 15815 Alignment); 15816 EXPECT_EQ("float const a = 5;\n" 15817 "\n" 15818 "int oneTwoThree = 123;", 15819 format("float const a = 5;\n" 15820 "\n" 15821 "int oneTwoThree= 123;", 15822 Alignment)); 15823 EXPECT_EQ("float a = 5;\n" 15824 "int one = 1;\n" 15825 "\n" 15826 "unsigned oneTwoThree = 123;", 15827 format("float a = 5;\n" 15828 "int one = 1;\n" 15829 "\n" 15830 "unsigned oneTwoThree = 123;", 15831 Alignment)); 15832 EXPECT_EQ("float a = 5;\n" 15833 "int one = 1;\n" 15834 "\n" 15835 "unsigned oneTwoThree = 123;\n" 15836 "int oneTwo = 12;", 15837 format("float a = 5;\n" 15838 "int one = 1;\n" 15839 "\n" 15840 "unsigned oneTwoThree = 123;\n" 15841 "int oneTwo = 12;", 15842 Alignment)); 15843 // Function prototype alignment 15844 verifyFormat("int a();\n" 15845 "double b();", 15846 Alignment); 15847 verifyFormat("int a(int x);\n" 15848 "double b();", 15849 Alignment); 15850 unsigned OldColumnLimit = Alignment.ColumnLimit; 15851 // We need to set ColumnLimit to zero, in order to stress nested alignments, 15852 // otherwise the function parameters will be re-flowed onto a single line. 15853 Alignment.ColumnLimit = 0; 15854 EXPECT_EQ("int a(int x,\n" 15855 " float y);\n" 15856 "double b(int x,\n" 15857 " double y);", 15858 format("int a(int x,\n" 15859 " float y);\n" 15860 "double b(int x,\n" 15861 " double y);", 15862 Alignment)); 15863 // This ensures that function parameters of function declarations are 15864 // correctly indented when their owning functions are indented. 15865 // The failure case here is for 'double y' to not be indented enough. 15866 EXPECT_EQ("double a(int x);\n" 15867 "int b(int y,\n" 15868 " double z);", 15869 format("double a(int x);\n" 15870 "int b(int y,\n" 15871 " double z);", 15872 Alignment)); 15873 // Set ColumnLimit low so that we induce wrapping immediately after 15874 // the function name and opening paren. 15875 Alignment.ColumnLimit = 13; 15876 verifyFormat("int function(\n" 15877 " int x,\n" 15878 " bool y);", 15879 Alignment); 15880 Alignment.ColumnLimit = OldColumnLimit; 15881 // Ensure function pointers don't screw up recursive alignment 15882 verifyFormat("int a(int x, void (*fp)(int y));\n" 15883 "double b();", 15884 Alignment); 15885 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 15886 // Ensure recursive alignment is broken by function braces, so that the 15887 // "a = 1" does not align with subsequent assignments inside the function 15888 // body. 15889 verifyFormat("int func(int a = 1) {\n" 15890 " int b = 2;\n" 15891 " int cc = 3;\n" 15892 "}", 15893 Alignment); 15894 verifyFormat("float something = 2000;\n" 15895 "double another = 911;\n" 15896 "int i = 1, j = 10;\n" 15897 "const int *oneMore = 1;\n" 15898 "unsigned i = 2;", 15899 Alignment); 15900 verifyFormat("int oneTwoThree = {0}; // comment\n" 15901 "unsigned oneTwo = 0; // comment", 15902 Alignment); 15903 // Make sure that scope is correctly tracked, in the absence of braces 15904 verifyFormat("for (int i = 0; i < n; i++)\n" 15905 " j = i;\n" 15906 "double x = 1;\n", 15907 Alignment); 15908 verifyFormat("if (int i = 0)\n" 15909 " j = i;\n" 15910 "double x = 1;\n", 15911 Alignment); 15912 // Ensure operator[] and operator() are comprehended 15913 verifyFormat("struct test {\n" 15914 " long long int foo();\n" 15915 " int operator[](int a);\n" 15916 " double bar();\n" 15917 "};\n", 15918 Alignment); 15919 verifyFormat("struct test {\n" 15920 " long long int foo();\n" 15921 " int operator()(int a);\n" 15922 " double bar();\n" 15923 "};\n", 15924 Alignment); 15925 15926 // PAS_Right 15927 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 15928 " int const i = 1;\n" 15929 " int *j = 2;\n" 15930 " int big = 10000;\n" 15931 "\n" 15932 " unsigned oneTwoThree = 123;\n" 15933 " int oneTwo = 12;\n" 15934 " method();\n" 15935 " float k = 2;\n" 15936 " int ll = 10000;\n" 15937 "}", 15938 format("void SomeFunction(int parameter= 0) {\n" 15939 " int const i= 1;\n" 15940 " int *j=2;\n" 15941 " int big = 10000;\n" 15942 "\n" 15943 "unsigned oneTwoThree =123;\n" 15944 "int oneTwo = 12;\n" 15945 " method();\n" 15946 "float k= 2;\n" 15947 "int ll=10000;\n" 15948 "}", 15949 Alignment)); 15950 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 15951 " int const i = 1;\n" 15952 " int **j = 2, ***k;\n" 15953 " int &k = i;\n" 15954 " int &&l = i + j;\n" 15955 " int big = 10000;\n" 15956 "\n" 15957 " unsigned oneTwoThree = 123;\n" 15958 " int oneTwo = 12;\n" 15959 " method();\n" 15960 " float k = 2;\n" 15961 " int ll = 10000;\n" 15962 "}", 15963 format("void SomeFunction(int parameter= 0) {\n" 15964 " int const i= 1;\n" 15965 " int **j=2,***k;\n" 15966 "int &k=i;\n" 15967 "int &&l=i+j;\n" 15968 " int big = 10000;\n" 15969 "\n" 15970 "unsigned oneTwoThree =123;\n" 15971 "int oneTwo = 12;\n" 15972 " method();\n" 15973 "float k= 2;\n" 15974 "int ll=10000;\n" 15975 "}", 15976 Alignment)); 15977 // variables are aligned at their name, pointers are at the right most 15978 // position 15979 verifyFormat("int *a;\n" 15980 "int **b;\n" 15981 "int ***c;\n" 15982 "int foobar;\n", 15983 Alignment); 15984 15985 // PAS_Left 15986 FormatStyle AlignmentLeft = Alignment; 15987 AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left; 15988 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 15989 " int const i = 1;\n" 15990 " int* j = 2;\n" 15991 " int big = 10000;\n" 15992 "\n" 15993 " unsigned oneTwoThree = 123;\n" 15994 " int oneTwo = 12;\n" 15995 " method();\n" 15996 " float k = 2;\n" 15997 " int ll = 10000;\n" 15998 "}", 15999 format("void SomeFunction(int parameter= 0) {\n" 16000 " int const i= 1;\n" 16001 " int *j=2;\n" 16002 " int big = 10000;\n" 16003 "\n" 16004 "unsigned oneTwoThree =123;\n" 16005 "int oneTwo = 12;\n" 16006 " method();\n" 16007 "float k= 2;\n" 16008 "int ll=10000;\n" 16009 "}", 16010 AlignmentLeft)); 16011 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16012 " int const i = 1;\n" 16013 " int** j = 2;\n" 16014 " int& k = i;\n" 16015 " int&& l = i + j;\n" 16016 " int big = 10000;\n" 16017 "\n" 16018 " unsigned oneTwoThree = 123;\n" 16019 " int oneTwo = 12;\n" 16020 " method();\n" 16021 " float k = 2;\n" 16022 " int ll = 10000;\n" 16023 "}", 16024 format("void SomeFunction(int parameter= 0) {\n" 16025 " int const i= 1;\n" 16026 " int **j=2;\n" 16027 "int &k=i;\n" 16028 "int &&l=i+j;\n" 16029 " int big = 10000;\n" 16030 "\n" 16031 "unsigned oneTwoThree =123;\n" 16032 "int oneTwo = 12;\n" 16033 " method();\n" 16034 "float k= 2;\n" 16035 "int ll=10000;\n" 16036 "}", 16037 AlignmentLeft)); 16038 // variables are aligned at their name, pointers are at the left most position 16039 verifyFormat("int* a;\n" 16040 "int** b;\n" 16041 "int*** c;\n" 16042 "int foobar;\n", 16043 AlignmentLeft); 16044 16045 // PAS_Middle 16046 FormatStyle AlignmentMiddle = Alignment; 16047 AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle; 16048 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16049 " int const i = 1;\n" 16050 " int * j = 2;\n" 16051 " int big = 10000;\n" 16052 "\n" 16053 " unsigned oneTwoThree = 123;\n" 16054 " int oneTwo = 12;\n" 16055 " method();\n" 16056 " float k = 2;\n" 16057 " int ll = 10000;\n" 16058 "}", 16059 format("void SomeFunction(int parameter= 0) {\n" 16060 " int const i= 1;\n" 16061 " int *j=2;\n" 16062 " int big = 10000;\n" 16063 "\n" 16064 "unsigned oneTwoThree =123;\n" 16065 "int oneTwo = 12;\n" 16066 " method();\n" 16067 "float k= 2;\n" 16068 "int ll=10000;\n" 16069 "}", 16070 AlignmentMiddle)); 16071 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16072 " int const i = 1;\n" 16073 " int ** j = 2, ***k;\n" 16074 " int & k = i;\n" 16075 " int && l = i + j;\n" 16076 " int big = 10000;\n" 16077 "\n" 16078 " unsigned oneTwoThree = 123;\n" 16079 " int oneTwo = 12;\n" 16080 " method();\n" 16081 " float k = 2;\n" 16082 " int ll = 10000;\n" 16083 "}", 16084 format("void SomeFunction(int parameter= 0) {\n" 16085 " int const i= 1;\n" 16086 " int **j=2,***k;\n" 16087 "int &k=i;\n" 16088 "int &&l=i+j;\n" 16089 " int big = 10000;\n" 16090 "\n" 16091 "unsigned oneTwoThree =123;\n" 16092 "int oneTwo = 12;\n" 16093 " method();\n" 16094 "float k= 2;\n" 16095 "int ll=10000;\n" 16096 "}", 16097 AlignmentMiddle)); 16098 // variables are aligned at their name, pointers are in the middle 16099 verifyFormat("int * a;\n" 16100 "int * b;\n" 16101 "int *** c;\n" 16102 "int foobar;\n", 16103 AlignmentMiddle); 16104 16105 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16106 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 16107 verifyFormat("#define A \\\n" 16108 " int aaaa = 12; \\\n" 16109 " float b = 23; \\\n" 16110 " const int ccc = 234; \\\n" 16111 " unsigned dddddddddd = 2345;", 16112 Alignment); 16113 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 16114 verifyFormat("#define A \\\n" 16115 " int aaaa = 12; \\\n" 16116 " float b = 23; \\\n" 16117 " const int ccc = 234; \\\n" 16118 " unsigned dddddddddd = 2345;", 16119 Alignment); 16120 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 16121 Alignment.ColumnLimit = 30; 16122 verifyFormat("#define A \\\n" 16123 " int aaaa = 12; \\\n" 16124 " float b = 23; \\\n" 16125 " const int ccc = 234; \\\n" 16126 " int dddddddddd = 2345;", 16127 Alignment); 16128 Alignment.ColumnLimit = 80; 16129 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 16130 "k = 4, int l = 5,\n" 16131 " int m = 6) {\n" 16132 " const int j = 10;\n" 16133 " otherThing = 1;\n" 16134 "}", 16135 Alignment); 16136 verifyFormat("void SomeFunction(int parameter = 0) {\n" 16137 " int const i = 1;\n" 16138 " int *j = 2;\n" 16139 " int big = 10000;\n" 16140 "}", 16141 Alignment); 16142 verifyFormat("class C {\n" 16143 "public:\n" 16144 " int i = 1;\n" 16145 " virtual void f() = 0;\n" 16146 "};", 16147 Alignment); 16148 verifyFormat("float i = 1;\n" 16149 "if (SomeType t = getSomething()) {\n" 16150 "}\n" 16151 "const unsigned j = 2;\n" 16152 "int big = 10000;", 16153 Alignment); 16154 verifyFormat("float j = 7;\n" 16155 "for (int k = 0; k < N; ++k) {\n" 16156 "}\n" 16157 "unsigned j = 2;\n" 16158 "int big = 10000;\n" 16159 "}", 16160 Alignment); 16161 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 16162 verifyFormat("float i = 1;\n" 16163 "LooooooooooongType loooooooooooooooooooooongVariable\n" 16164 " = someLooooooooooooooooongFunction();\n" 16165 "int j = 2;", 16166 Alignment); 16167 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 16168 verifyFormat("int i = 1;\n" 16169 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 16170 " someLooooooooooooooooongFunction();\n" 16171 "int j = 2;", 16172 Alignment); 16173 16174 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16175 verifyFormat("auto lambda = []() {\n" 16176 " auto ii = 0;\n" 16177 " float j = 0;\n" 16178 " return 0;\n" 16179 "};\n" 16180 "int i = 0;\n" 16181 "float i2 = 0;\n" 16182 "auto v = type{\n" 16183 " i = 1, //\n" 16184 " (i = 2), //\n" 16185 " i = 3 //\n" 16186 "};", 16187 Alignment); 16188 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16189 16190 verifyFormat( 16191 "int i = 1;\n" 16192 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 16193 " loooooooooooooooooooooongParameterB);\n" 16194 "int j = 2;", 16195 Alignment); 16196 16197 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 16198 // We expect declarations and assignments to align, as long as it doesn't 16199 // exceed the column limit, starting a new alignment sequence whenever it 16200 // happens. 16201 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16202 Alignment.ColumnLimit = 30; 16203 verifyFormat("float ii = 1;\n" 16204 "unsigned j = 2;\n" 16205 "int someVerylongVariable = 1;\n" 16206 "AnotherLongType ll = 123456;\n" 16207 "VeryVeryLongType k = 2;\n" 16208 "int myvar = 1;", 16209 Alignment); 16210 Alignment.ColumnLimit = 80; 16211 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16212 16213 verifyFormat( 16214 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 16215 " typename LongType, typename B>\n" 16216 "auto foo() {}\n", 16217 Alignment); 16218 verifyFormat("float a, b = 1;\n" 16219 "int c = 2;\n" 16220 "int dd = 3;\n", 16221 Alignment); 16222 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 16223 "float b[1][] = {{3.f}};\n", 16224 Alignment); 16225 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16226 verifyFormat("float a, b = 1;\n" 16227 "int c = 2;\n" 16228 "int dd = 3;\n", 16229 Alignment); 16230 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 16231 "float b[1][] = {{3.f}};\n", 16232 Alignment); 16233 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16234 16235 Alignment.ColumnLimit = 30; 16236 Alignment.BinPackParameters = false; 16237 verifyFormat("void foo(float a,\n" 16238 " float b,\n" 16239 " int c,\n" 16240 " uint32_t *d) {\n" 16241 " int *e = 0;\n" 16242 " float f = 0;\n" 16243 " double g = 0;\n" 16244 "}\n" 16245 "void bar(ino_t a,\n" 16246 " int b,\n" 16247 " uint32_t *c,\n" 16248 " bool d) {}\n", 16249 Alignment); 16250 Alignment.BinPackParameters = true; 16251 Alignment.ColumnLimit = 80; 16252 16253 // Bug 33507 16254 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 16255 verifyFormat( 16256 "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n" 16257 " static const Version verVs2017;\n" 16258 " return true;\n" 16259 "});\n", 16260 Alignment); 16261 Alignment.PointerAlignment = FormatStyle::PAS_Right; 16262 16263 // See llvm.org/PR35641 16264 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16265 verifyFormat("int func() { //\n" 16266 " int b;\n" 16267 " unsigned c;\n" 16268 "}", 16269 Alignment); 16270 16271 // See PR37175 16272 FormatStyle Style = getMozillaStyle(); 16273 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16274 EXPECT_EQ("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n" 16275 "foo(int a);", 16276 format("DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style)); 16277 16278 Alignment.PointerAlignment = FormatStyle::PAS_Left; 16279 verifyFormat("unsigned int* a;\n" 16280 "int* b;\n" 16281 "unsigned int Const* c;\n" 16282 "unsigned int const* d;\n" 16283 "unsigned int Const& e;\n" 16284 "unsigned int const& f;", 16285 Alignment); 16286 verifyFormat("Const unsigned int* c;\n" 16287 "const unsigned int* d;\n" 16288 "Const unsigned int& e;\n" 16289 "const unsigned int& f;\n" 16290 "const unsigned g;\n" 16291 "Const unsigned h;", 16292 Alignment); 16293 16294 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 16295 verifyFormat("unsigned int * a;\n" 16296 "int * b;\n" 16297 "unsigned int Const * c;\n" 16298 "unsigned int const * d;\n" 16299 "unsigned int Const & e;\n" 16300 "unsigned int const & f;", 16301 Alignment); 16302 verifyFormat("Const unsigned int * c;\n" 16303 "const unsigned int * d;\n" 16304 "Const unsigned int & e;\n" 16305 "const unsigned int & f;\n" 16306 "const unsigned g;\n" 16307 "Const unsigned h;", 16308 Alignment); 16309 } 16310 16311 TEST_F(FormatTest, AlignWithLineBreaks) { 16312 auto Style = getLLVMStyleWithColumns(120); 16313 16314 EXPECT_EQ(Style.AlignConsecutiveAssignments, FormatStyle::ACS_None); 16315 EXPECT_EQ(Style.AlignConsecutiveDeclarations, FormatStyle::ACS_None); 16316 verifyFormat("void foo() {\n" 16317 " int myVar = 5;\n" 16318 " double x = 3.14;\n" 16319 " auto str = \"Hello \"\n" 16320 " \"World\";\n" 16321 " auto s = \"Hello \"\n" 16322 " \"Again\";\n" 16323 "}", 16324 Style); 16325 16326 // clang-format off 16327 verifyFormat("void foo() {\n" 16328 " const int capacityBefore = Entries.capacity();\n" 16329 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16330 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16331 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16332 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16333 "}", 16334 Style); 16335 // clang-format on 16336 16337 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16338 verifyFormat("void foo() {\n" 16339 " int myVar = 5;\n" 16340 " double x = 3.14;\n" 16341 " auto str = \"Hello \"\n" 16342 " \"World\";\n" 16343 " auto s = \"Hello \"\n" 16344 " \"Again\";\n" 16345 "}", 16346 Style); 16347 16348 // clang-format off 16349 verifyFormat("void foo() {\n" 16350 " const int capacityBefore = Entries.capacity();\n" 16351 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16352 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16353 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16354 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16355 "}", 16356 Style); 16357 // clang-format on 16358 16359 Style.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16360 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16361 verifyFormat("void foo() {\n" 16362 " int myVar = 5;\n" 16363 " double x = 3.14;\n" 16364 " auto str = \"Hello \"\n" 16365 " \"World\";\n" 16366 " auto s = \"Hello \"\n" 16367 " \"Again\";\n" 16368 "}", 16369 Style); 16370 16371 // clang-format off 16372 verifyFormat("void foo() {\n" 16373 " const int capacityBefore = Entries.capacity();\n" 16374 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16375 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16376 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16377 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16378 "}", 16379 Style); 16380 // clang-format on 16381 16382 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16383 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16384 16385 verifyFormat("void foo() {\n" 16386 " int myVar = 5;\n" 16387 " double x = 3.14;\n" 16388 " auto str = \"Hello \"\n" 16389 " \"World\";\n" 16390 " auto s = \"Hello \"\n" 16391 " \"Again\";\n" 16392 "}", 16393 Style); 16394 16395 // clang-format off 16396 verifyFormat("void foo() {\n" 16397 " const int capacityBefore = Entries.capacity();\n" 16398 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16399 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16400 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16401 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16402 "}", 16403 Style); 16404 // clang-format on 16405 } 16406 16407 TEST_F(FormatTest, AlignWithInitializerPeriods) { 16408 auto Style = getLLVMStyleWithColumns(60); 16409 16410 verifyFormat("void foo1(void) {\n" 16411 " BYTE p[1] = 1;\n" 16412 " A B = {.one_foooooooooooooooo = 2,\n" 16413 " .two_fooooooooooooo = 3,\n" 16414 " .three_fooooooooooooo = 4};\n" 16415 " BYTE payload = 2;\n" 16416 "}", 16417 Style); 16418 16419 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16420 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_None; 16421 verifyFormat("void foo2(void) {\n" 16422 " BYTE p[1] = 1;\n" 16423 " A B = {.one_foooooooooooooooo = 2,\n" 16424 " .two_fooooooooooooo = 3,\n" 16425 " .three_fooooooooooooo = 4};\n" 16426 " BYTE payload = 2;\n" 16427 "}", 16428 Style); 16429 16430 Style.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16431 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16432 verifyFormat("void foo3(void) {\n" 16433 " BYTE p[1] = 1;\n" 16434 " A B = {.one_foooooooooooooooo = 2,\n" 16435 " .two_fooooooooooooo = 3,\n" 16436 " .three_fooooooooooooo = 4};\n" 16437 " BYTE payload = 2;\n" 16438 "}", 16439 Style); 16440 16441 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16442 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16443 verifyFormat("void foo4(void) {\n" 16444 " BYTE p[1] = 1;\n" 16445 " A B = {.one_foooooooooooooooo = 2,\n" 16446 " .two_fooooooooooooo = 3,\n" 16447 " .three_fooooooooooooo = 4};\n" 16448 " BYTE payload = 2;\n" 16449 "}", 16450 Style); 16451 } 16452 16453 TEST_F(FormatTest, LinuxBraceBreaking) { 16454 FormatStyle LinuxBraceStyle = getLLVMStyle(); 16455 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 16456 verifyFormat("namespace a\n" 16457 "{\n" 16458 "class A\n" 16459 "{\n" 16460 " void f()\n" 16461 " {\n" 16462 " if (true) {\n" 16463 " a();\n" 16464 " b();\n" 16465 " } else {\n" 16466 " a();\n" 16467 " }\n" 16468 " }\n" 16469 " void g() { return; }\n" 16470 "};\n" 16471 "struct B {\n" 16472 " int x;\n" 16473 "};\n" 16474 "} // namespace a\n", 16475 LinuxBraceStyle); 16476 verifyFormat("enum X {\n" 16477 " Y = 0,\n" 16478 "}\n", 16479 LinuxBraceStyle); 16480 verifyFormat("struct S {\n" 16481 " int Type;\n" 16482 " union {\n" 16483 " int x;\n" 16484 " double y;\n" 16485 " } Value;\n" 16486 " class C\n" 16487 " {\n" 16488 " MyFavoriteType Value;\n" 16489 " } Class;\n" 16490 "}\n", 16491 LinuxBraceStyle); 16492 } 16493 16494 TEST_F(FormatTest, MozillaBraceBreaking) { 16495 FormatStyle MozillaBraceStyle = getLLVMStyle(); 16496 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 16497 MozillaBraceStyle.FixNamespaceComments = false; 16498 verifyFormat("namespace a {\n" 16499 "class A\n" 16500 "{\n" 16501 " void f()\n" 16502 " {\n" 16503 " if (true) {\n" 16504 " a();\n" 16505 " b();\n" 16506 " }\n" 16507 " }\n" 16508 " void g() { return; }\n" 16509 "};\n" 16510 "enum E\n" 16511 "{\n" 16512 " A,\n" 16513 " // foo\n" 16514 " B,\n" 16515 " C\n" 16516 "};\n" 16517 "struct B\n" 16518 "{\n" 16519 " int x;\n" 16520 "};\n" 16521 "}\n", 16522 MozillaBraceStyle); 16523 verifyFormat("struct S\n" 16524 "{\n" 16525 " int Type;\n" 16526 " union\n" 16527 " {\n" 16528 " int x;\n" 16529 " double y;\n" 16530 " } Value;\n" 16531 " class C\n" 16532 " {\n" 16533 " MyFavoriteType Value;\n" 16534 " } Class;\n" 16535 "}\n", 16536 MozillaBraceStyle); 16537 } 16538 16539 TEST_F(FormatTest, StroustrupBraceBreaking) { 16540 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 16541 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 16542 verifyFormat("namespace a {\n" 16543 "class A {\n" 16544 " void f()\n" 16545 " {\n" 16546 " if (true) {\n" 16547 " a();\n" 16548 " b();\n" 16549 " }\n" 16550 " }\n" 16551 " void g() { return; }\n" 16552 "};\n" 16553 "struct B {\n" 16554 " int x;\n" 16555 "};\n" 16556 "} // namespace a\n", 16557 StroustrupBraceStyle); 16558 16559 verifyFormat("void foo()\n" 16560 "{\n" 16561 " if (a) {\n" 16562 " a();\n" 16563 " }\n" 16564 " else {\n" 16565 " b();\n" 16566 " }\n" 16567 "}\n", 16568 StroustrupBraceStyle); 16569 16570 verifyFormat("#ifdef _DEBUG\n" 16571 "int foo(int i = 0)\n" 16572 "#else\n" 16573 "int foo(int i = 5)\n" 16574 "#endif\n" 16575 "{\n" 16576 " return i;\n" 16577 "}", 16578 StroustrupBraceStyle); 16579 16580 verifyFormat("void foo() {}\n" 16581 "void bar()\n" 16582 "#ifdef _DEBUG\n" 16583 "{\n" 16584 " foo();\n" 16585 "}\n" 16586 "#else\n" 16587 "{\n" 16588 "}\n" 16589 "#endif", 16590 StroustrupBraceStyle); 16591 16592 verifyFormat("void foobar() { int i = 5; }\n" 16593 "#ifdef _DEBUG\n" 16594 "void bar() {}\n" 16595 "#else\n" 16596 "void bar() { foobar(); }\n" 16597 "#endif", 16598 StroustrupBraceStyle); 16599 } 16600 16601 TEST_F(FormatTest, AllmanBraceBreaking) { 16602 FormatStyle AllmanBraceStyle = getLLVMStyle(); 16603 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 16604 16605 EXPECT_EQ("namespace a\n" 16606 "{\n" 16607 "void f();\n" 16608 "void g();\n" 16609 "} // namespace a\n", 16610 format("namespace a\n" 16611 "{\n" 16612 "void f();\n" 16613 "void g();\n" 16614 "}\n", 16615 AllmanBraceStyle)); 16616 16617 verifyFormat("namespace a\n" 16618 "{\n" 16619 "class A\n" 16620 "{\n" 16621 " void f()\n" 16622 " {\n" 16623 " if (true)\n" 16624 " {\n" 16625 " a();\n" 16626 " b();\n" 16627 " }\n" 16628 " }\n" 16629 " void g() { return; }\n" 16630 "};\n" 16631 "struct B\n" 16632 "{\n" 16633 " int x;\n" 16634 "};\n" 16635 "union C\n" 16636 "{\n" 16637 "};\n" 16638 "} // namespace a", 16639 AllmanBraceStyle); 16640 16641 verifyFormat("void f()\n" 16642 "{\n" 16643 " if (true)\n" 16644 " {\n" 16645 " a();\n" 16646 " }\n" 16647 " else if (false)\n" 16648 " {\n" 16649 " b();\n" 16650 " }\n" 16651 " else\n" 16652 " {\n" 16653 " c();\n" 16654 " }\n" 16655 "}\n", 16656 AllmanBraceStyle); 16657 16658 verifyFormat("void f()\n" 16659 "{\n" 16660 " for (int i = 0; i < 10; ++i)\n" 16661 " {\n" 16662 " a();\n" 16663 " }\n" 16664 " while (false)\n" 16665 " {\n" 16666 " b();\n" 16667 " }\n" 16668 " do\n" 16669 " {\n" 16670 " c();\n" 16671 " } while (false)\n" 16672 "}\n", 16673 AllmanBraceStyle); 16674 16675 verifyFormat("void f(int a)\n" 16676 "{\n" 16677 " switch (a)\n" 16678 " {\n" 16679 " case 0:\n" 16680 " break;\n" 16681 " case 1:\n" 16682 " {\n" 16683 " break;\n" 16684 " }\n" 16685 " case 2:\n" 16686 " {\n" 16687 " }\n" 16688 " break;\n" 16689 " default:\n" 16690 " break;\n" 16691 " }\n" 16692 "}\n", 16693 AllmanBraceStyle); 16694 16695 verifyFormat("enum X\n" 16696 "{\n" 16697 " Y = 0,\n" 16698 "}\n", 16699 AllmanBraceStyle); 16700 verifyFormat("enum X\n" 16701 "{\n" 16702 " Y = 0\n" 16703 "}\n", 16704 AllmanBraceStyle); 16705 16706 verifyFormat("@interface BSApplicationController ()\n" 16707 "{\n" 16708 "@private\n" 16709 " id _extraIvar;\n" 16710 "}\n" 16711 "@end\n", 16712 AllmanBraceStyle); 16713 16714 verifyFormat("#ifdef _DEBUG\n" 16715 "int foo(int i = 0)\n" 16716 "#else\n" 16717 "int foo(int i = 5)\n" 16718 "#endif\n" 16719 "{\n" 16720 " return i;\n" 16721 "}", 16722 AllmanBraceStyle); 16723 16724 verifyFormat("void foo() {}\n" 16725 "void bar()\n" 16726 "#ifdef _DEBUG\n" 16727 "{\n" 16728 " foo();\n" 16729 "}\n" 16730 "#else\n" 16731 "{\n" 16732 "}\n" 16733 "#endif", 16734 AllmanBraceStyle); 16735 16736 verifyFormat("void foobar() { int i = 5; }\n" 16737 "#ifdef _DEBUG\n" 16738 "void bar() {}\n" 16739 "#else\n" 16740 "void bar() { foobar(); }\n" 16741 "#endif", 16742 AllmanBraceStyle); 16743 16744 EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine, 16745 FormatStyle::SLS_All); 16746 16747 verifyFormat("[](int i) { return i + 2; };\n" 16748 "[](int i, int j)\n" 16749 "{\n" 16750 " auto x = i + j;\n" 16751 " auto y = i * j;\n" 16752 " return x ^ y;\n" 16753 "};\n" 16754 "void foo()\n" 16755 "{\n" 16756 " auto shortLambda = [](int i) { return i + 2; };\n" 16757 " auto longLambda = [](int i, int j)\n" 16758 " {\n" 16759 " auto x = i + j;\n" 16760 " auto y = i * j;\n" 16761 " return x ^ y;\n" 16762 " };\n" 16763 "}", 16764 AllmanBraceStyle); 16765 16766 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 16767 16768 verifyFormat("[](int i)\n" 16769 "{\n" 16770 " return i + 2;\n" 16771 "};\n" 16772 "[](int i, int j)\n" 16773 "{\n" 16774 " auto x = i + j;\n" 16775 " auto y = i * j;\n" 16776 " return x ^ y;\n" 16777 "};\n" 16778 "void foo()\n" 16779 "{\n" 16780 " auto shortLambda = [](int i)\n" 16781 " {\n" 16782 " return i + 2;\n" 16783 " };\n" 16784 " auto longLambda = [](int i, int j)\n" 16785 " {\n" 16786 " auto x = i + j;\n" 16787 " auto y = i * j;\n" 16788 " return x ^ y;\n" 16789 " };\n" 16790 "}", 16791 AllmanBraceStyle); 16792 16793 // Reset 16794 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All; 16795 16796 // This shouldn't affect ObjC blocks.. 16797 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 16798 " // ...\n" 16799 " int i;\n" 16800 "}];", 16801 AllmanBraceStyle); 16802 verifyFormat("void (^block)(void) = ^{\n" 16803 " // ...\n" 16804 " int i;\n" 16805 "};", 16806 AllmanBraceStyle); 16807 // .. or dict literals. 16808 verifyFormat("void f()\n" 16809 "{\n" 16810 " // ...\n" 16811 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 16812 "}", 16813 AllmanBraceStyle); 16814 verifyFormat("void f()\n" 16815 "{\n" 16816 " // ...\n" 16817 " [object someMethod:@{a : @\"b\"}];\n" 16818 "}", 16819 AllmanBraceStyle); 16820 verifyFormat("int f()\n" 16821 "{ // comment\n" 16822 " return 42;\n" 16823 "}", 16824 AllmanBraceStyle); 16825 16826 AllmanBraceStyle.ColumnLimit = 19; 16827 verifyFormat("void f() { int i; }", AllmanBraceStyle); 16828 AllmanBraceStyle.ColumnLimit = 18; 16829 verifyFormat("void f()\n" 16830 "{\n" 16831 " int i;\n" 16832 "}", 16833 AllmanBraceStyle); 16834 AllmanBraceStyle.ColumnLimit = 80; 16835 16836 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 16837 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = 16838 FormatStyle::SIS_WithoutElse; 16839 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 16840 verifyFormat("void f(bool b)\n" 16841 "{\n" 16842 " if (b)\n" 16843 " {\n" 16844 " return;\n" 16845 " }\n" 16846 "}\n", 16847 BreakBeforeBraceShortIfs); 16848 verifyFormat("void f(bool b)\n" 16849 "{\n" 16850 " if constexpr (b)\n" 16851 " {\n" 16852 " return;\n" 16853 " }\n" 16854 "}\n", 16855 BreakBeforeBraceShortIfs); 16856 verifyFormat("void f(bool b)\n" 16857 "{\n" 16858 " if CONSTEXPR (b)\n" 16859 " {\n" 16860 " return;\n" 16861 " }\n" 16862 "}\n", 16863 BreakBeforeBraceShortIfs); 16864 verifyFormat("void f(bool b)\n" 16865 "{\n" 16866 " if (b) return;\n" 16867 "}\n", 16868 BreakBeforeBraceShortIfs); 16869 verifyFormat("void f(bool b)\n" 16870 "{\n" 16871 " if constexpr (b) return;\n" 16872 "}\n", 16873 BreakBeforeBraceShortIfs); 16874 verifyFormat("void f(bool b)\n" 16875 "{\n" 16876 " if CONSTEXPR (b) return;\n" 16877 "}\n", 16878 BreakBeforeBraceShortIfs); 16879 verifyFormat("void f(bool b)\n" 16880 "{\n" 16881 " while (b)\n" 16882 " {\n" 16883 " return;\n" 16884 " }\n" 16885 "}\n", 16886 BreakBeforeBraceShortIfs); 16887 } 16888 16889 TEST_F(FormatTest, WhitesmithsBraceBreaking) { 16890 FormatStyle WhitesmithsBraceStyle = getLLVMStyle(); 16891 WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 16892 16893 // Make a few changes to the style for testing purposes 16894 WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine = 16895 FormatStyle::SFS_Empty; 16896 WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 16897 WhitesmithsBraceStyle.ColumnLimit = 0; 16898 16899 // FIXME: this test case can't decide whether there should be a blank line 16900 // after the ~D() line or not. It adds one if one doesn't exist in the test 16901 // and it removes the line if one exists. 16902 /* 16903 verifyFormat("class A;\n" 16904 "namespace B\n" 16905 " {\n" 16906 "class C;\n" 16907 "// Comment\n" 16908 "class D\n" 16909 " {\n" 16910 "public:\n" 16911 " D();\n" 16912 " ~D() {}\n" 16913 "private:\n" 16914 " enum E\n" 16915 " {\n" 16916 " F\n" 16917 " }\n" 16918 " };\n" 16919 " } // namespace B\n", 16920 WhitesmithsBraceStyle); 16921 */ 16922 16923 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None; 16924 verifyFormat("namespace a\n" 16925 " {\n" 16926 "class A\n" 16927 " {\n" 16928 " void f()\n" 16929 " {\n" 16930 " if (true)\n" 16931 " {\n" 16932 " a();\n" 16933 " b();\n" 16934 " }\n" 16935 " }\n" 16936 " void g()\n" 16937 " {\n" 16938 " return;\n" 16939 " }\n" 16940 " };\n" 16941 "struct B\n" 16942 " {\n" 16943 " int x;\n" 16944 " };\n" 16945 " } // namespace a", 16946 WhitesmithsBraceStyle); 16947 16948 verifyFormat("namespace a\n" 16949 " {\n" 16950 "namespace b\n" 16951 " {\n" 16952 "class A\n" 16953 " {\n" 16954 " void f()\n" 16955 " {\n" 16956 " if (true)\n" 16957 " {\n" 16958 " a();\n" 16959 " b();\n" 16960 " }\n" 16961 " }\n" 16962 " void g()\n" 16963 " {\n" 16964 " return;\n" 16965 " }\n" 16966 " };\n" 16967 "struct B\n" 16968 " {\n" 16969 " int x;\n" 16970 " };\n" 16971 " } // namespace b\n" 16972 " } // namespace a", 16973 WhitesmithsBraceStyle); 16974 16975 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner; 16976 verifyFormat("namespace a\n" 16977 " {\n" 16978 "namespace b\n" 16979 " {\n" 16980 " class A\n" 16981 " {\n" 16982 " void f()\n" 16983 " {\n" 16984 " if (true)\n" 16985 " {\n" 16986 " a();\n" 16987 " b();\n" 16988 " }\n" 16989 " }\n" 16990 " void g()\n" 16991 " {\n" 16992 " return;\n" 16993 " }\n" 16994 " };\n" 16995 " struct B\n" 16996 " {\n" 16997 " int x;\n" 16998 " };\n" 16999 " } // namespace b\n" 17000 " } // namespace a", 17001 WhitesmithsBraceStyle); 17002 17003 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All; 17004 verifyFormat("namespace a\n" 17005 " {\n" 17006 " namespace b\n" 17007 " {\n" 17008 " class A\n" 17009 " {\n" 17010 " void f()\n" 17011 " {\n" 17012 " if (true)\n" 17013 " {\n" 17014 " a();\n" 17015 " b();\n" 17016 " }\n" 17017 " }\n" 17018 " void g()\n" 17019 " {\n" 17020 " return;\n" 17021 " }\n" 17022 " };\n" 17023 " struct B\n" 17024 " {\n" 17025 " int x;\n" 17026 " };\n" 17027 " } // namespace b\n" 17028 " } // namespace a", 17029 WhitesmithsBraceStyle); 17030 17031 verifyFormat("void f()\n" 17032 " {\n" 17033 " if (true)\n" 17034 " {\n" 17035 " a();\n" 17036 " }\n" 17037 " else if (false)\n" 17038 " {\n" 17039 " b();\n" 17040 " }\n" 17041 " else\n" 17042 " {\n" 17043 " c();\n" 17044 " }\n" 17045 " }\n", 17046 WhitesmithsBraceStyle); 17047 17048 verifyFormat("void f()\n" 17049 " {\n" 17050 " for (int i = 0; i < 10; ++i)\n" 17051 " {\n" 17052 " a();\n" 17053 " }\n" 17054 " while (false)\n" 17055 " {\n" 17056 " b();\n" 17057 " }\n" 17058 " do\n" 17059 " {\n" 17060 " c();\n" 17061 " } while (false)\n" 17062 " }\n", 17063 WhitesmithsBraceStyle); 17064 17065 WhitesmithsBraceStyle.IndentCaseLabels = true; 17066 verifyFormat("void switchTest1(int a)\n" 17067 " {\n" 17068 " switch (a)\n" 17069 " {\n" 17070 " case 2:\n" 17071 " {\n" 17072 " }\n" 17073 " break;\n" 17074 " }\n" 17075 " }\n", 17076 WhitesmithsBraceStyle); 17077 17078 verifyFormat("void switchTest2(int a)\n" 17079 " {\n" 17080 " switch (a)\n" 17081 " {\n" 17082 " case 0:\n" 17083 " break;\n" 17084 " case 1:\n" 17085 " {\n" 17086 " break;\n" 17087 " }\n" 17088 " case 2:\n" 17089 " {\n" 17090 " }\n" 17091 " break;\n" 17092 " default:\n" 17093 " break;\n" 17094 " }\n" 17095 " }\n", 17096 WhitesmithsBraceStyle); 17097 17098 verifyFormat("void switchTest3(int a)\n" 17099 " {\n" 17100 " switch (a)\n" 17101 " {\n" 17102 " case 0:\n" 17103 " {\n" 17104 " foo(x);\n" 17105 " }\n" 17106 " break;\n" 17107 " default:\n" 17108 " {\n" 17109 " foo(1);\n" 17110 " }\n" 17111 " break;\n" 17112 " }\n" 17113 " }\n", 17114 WhitesmithsBraceStyle); 17115 17116 WhitesmithsBraceStyle.IndentCaseLabels = false; 17117 17118 verifyFormat("void switchTest4(int a)\n" 17119 " {\n" 17120 " switch (a)\n" 17121 " {\n" 17122 " case 2:\n" 17123 " {\n" 17124 " }\n" 17125 " break;\n" 17126 " }\n" 17127 " }\n", 17128 WhitesmithsBraceStyle); 17129 17130 verifyFormat("void switchTest5(int a)\n" 17131 " {\n" 17132 " switch (a)\n" 17133 " {\n" 17134 " case 0:\n" 17135 " break;\n" 17136 " case 1:\n" 17137 " {\n" 17138 " foo();\n" 17139 " break;\n" 17140 " }\n" 17141 " case 2:\n" 17142 " {\n" 17143 " }\n" 17144 " break;\n" 17145 " default:\n" 17146 " break;\n" 17147 " }\n" 17148 " }\n", 17149 WhitesmithsBraceStyle); 17150 17151 verifyFormat("void switchTest6(int a)\n" 17152 " {\n" 17153 " switch (a)\n" 17154 " {\n" 17155 " case 0:\n" 17156 " {\n" 17157 " foo(x);\n" 17158 " }\n" 17159 " break;\n" 17160 " default:\n" 17161 " {\n" 17162 " foo(1);\n" 17163 " }\n" 17164 " break;\n" 17165 " }\n" 17166 " }\n", 17167 WhitesmithsBraceStyle); 17168 17169 verifyFormat("enum X\n" 17170 " {\n" 17171 " Y = 0, // testing\n" 17172 " }\n", 17173 WhitesmithsBraceStyle); 17174 17175 verifyFormat("enum X\n" 17176 " {\n" 17177 " Y = 0\n" 17178 " }\n", 17179 WhitesmithsBraceStyle); 17180 verifyFormat("enum X\n" 17181 " {\n" 17182 " Y = 0,\n" 17183 " Z = 1\n" 17184 " };\n", 17185 WhitesmithsBraceStyle); 17186 17187 verifyFormat("@interface BSApplicationController ()\n" 17188 " {\n" 17189 "@private\n" 17190 " id _extraIvar;\n" 17191 " }\n" 17192 "@end\n", 17193 WhitesmithsBraceStyle); 17194 17195 verifyFormat("#ifdef _DEBUG\n" 17196 "int foo(int i = 0)\n" 17197 "#else\n" 17198 "int foo(int i = 5)\n" 17199 "#endif\n" 17200 " {\n" 17201 " return i;\n" 17202 " }", 17203 WhitesmithsBraceStyle); 17204 17205 verifyFormat("void foo() {}\n" 17206 "void bar()\n" 17207 "#ifdef _DEBUG\n" 17208 " {\n" 17209 " foo();\n" 17210 " }\n" 17211 "#else\n" 17212 " {\n" 17213 " }\n" 17214 "#endif", 17215 WhitesmithsBraceStyle); 17216 17217 verifyFormat("void foobar()\n" 17218 " {\n" 17219 " int i = 5;\n" 17220 " }\n" 17221 "#ifdef _DEBUG\n" 17222 "void bar()\n" 17223 " {\n" 17224 " }\n" 17225 "#else\n" 17226 "void bar()\n" 17227 " {\n" 17228 " foobar();\n" 17229 " }\n" 17230 "#endif", 17231 WhitesmithsBraceStyle); 17232 17233 // This shouldn't affect ObjC blocks.. 17234 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 17235 " // ...\n" 17236 " int i;\n" 17237 "}];", 17238 WhitesmithsBraceStyle); 17239 verifyFormat("void (^block)(void) = ^{\n" 17240 " // ...\n" 17241 " int i;\n" 17242 "};", 17243 WhitesmithsBraceStyle); 17244 // .. or dict literals. 17245 verifyFormat("void f()\n" 17246 " {\n" 17247 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 17248 " }", 17249 WhitesmithsBraceStyle); 17250 17251 verifyFormat("int f()\n" 17252 " { // comment\n" 17253 " return 42;\n" 17254 " }", 17255 WhitesmithsBraceStyle); 17256 17257 FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle; 17258 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = 17259 FormatStyle::SIS_OnlyFirstIf; 17260 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 17261 verifyFormat("void f(bool b)\n" 17262 " {\n" 17263 " if (b)\n" 17264 " {\n" 17265 " return;\n" 17266 " }\n" 17267 " }\n", 17268 BreakBeforeBraceShortIfs); 17269 verifyFormat("void f(bool b)\n" 17270 " {\n" 17271 " if (b) return;\n" 17272 " }\n", 17273 BreakBeforeBraceShortIfs); 17274 verifyFormat("void f(bool b)\n" 17275 " {\n" 17276 " while (b)\n" 17277 " {\n" 17278 " return;\n" 17279 " }\n" 17280 " }\n", 17281 BreakBeforeBraceShortIfs); 17282 } 17283 17284 TEST_F(FormatTest, GNUBraceBreaking) { 17285 FormatStyle GNUBraceStyle = getLLVMStyle(); 17286 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 17287 verifyFormat("namespace a\n" 17288 "{\n" 17289 "class A\n" 17290 "{\n" 17291 " void f()\n" 17292 " {\n" 17293 " int a;\n" 17294 " {\n" 17295 " int b;\n" 17296 " }\n" 17297 " if (true)\n" 17298 " {\n" 17299 " a();\n" 17300 " b();\n" 17301 " }\n" 17302 " }\n" 17303 " void g() { return; }\n" 17304 "}\n" 17305 "} // namespace a", 17306 GNUBraceStyle); 17307 17308 verifyFormat("void f()\n" 17309 "{\n" 17310 " if (true)\n" 17311 " {\n" 17312 " a();\n" 17313 " }\n" 17314 " else if (false)\n" 17315 " {\n" 17316 " b();\n" 17317 " }\n" 17318 " else\n" 17319 " {\n" 17320 " c();\n" 17321 " }\n" 17322 "}\n", 17323 GNUBraceStyle); 17324 17325 verifyFormat("void f()\n" 17326 "{\n" 17327 " for (int i = 0; i < 10; ++i)\n" 17328 " {\n" 17329 " a();\n" 17330 " }\n" 17331 " while (false)\n" 17332 " {\n" 17333 " b();\n" 17334 " }\n" 17335 " do\n" 17336 " {\n" 17337 " c();\n" 17338 " }\n" 17339 " while (false);\n" 17340 "}\n", 17341 GNUBraceStyle); 17342 17343 verifyFormat("void f(int a)\n" 17344 "{\n" 17345 " switch (a)\n" 17346 " {\n" 17347 " case 0:\n" 17348 " break;\n" 17349 " case 1:\n" 17350 " {\n" 17351 " break;\n" 17352 " }\n" 17353 " case 2:\n" 17354 " {\n" 17355 " }\n" 17356 " break;\n" 17357 " default:\n" 17358 " break;\n" 17359 " }\n" 17360 "}\n", 17361 GNUBraceStyle); 17362 17363 verifyFormat("enum X\n" 17364 "{\n" 17365 " Y = 0,\n" 17366 "}\n", 17367 GNUBraceStyle); 17368 17369 verifyFormat("@interface BSApplicationController ()\n" 17370 "{\n" 17371 "@private\n" 17372 " id _extraIvar;\n" 17373 "}\n" 17374 "@end\n", 17375 GNUBraceStyle); 17376 17377 verifyFormat("#ifdef _DEBUG\n" 17378 "int foo(int i = 0)\n" 17379 "#else\n" 17380 "int foo(int i = 5)\n" 17381 "#endif\n" 17382 "{\n" 17383 " return i;\n" 17384 "}", 17385 GNUBraceStyle); 17386 17387 verifyFormat("void foo() {}\n" 17388 "void bar()\n" 17389 "#ifdef _DEBUG\n" 17390 "{\n" 17391 " foo();\n" 17392 "}\n" 17393 "#else\n" 17394 "{\n" 17395 "}\n" 17396 "#endif", 17397 GNUBraceStyle); 17398 17399 verifyFormat("void foobar() { int i = 5; }\n" 17400 "#ifdef _DEBUG\n" 17401 "void bar() {}\n" 17402 "#else\n" 17403 "void bar() { foobar(); }\n" 17404 "#endif", 17405 GNUBraceStyle); 17406 } 17407 17408 TEST_F(FormatTest, WebKitBraceBreaking) { 17409 FormatStyle WebKitBraceStyle = getLLVMStyle(); 17410 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 17411 WebKitBraceStyle.FixNamespaceComments = false; 17412 verifyFormat("namespace a {\n" 17413 "class A {\n" 17414 " void f()\n" 17415 " {\n" 17416 " if (true) {\n" 17417 " a();\n" 17418 " b();\n" 17419 " }\n" 17420 " }\n" 17421 " void g() { return; }\n" 17422 "};\n" 17423 "enum E {\n" 17424 " A,\n" 17425 " // foo\n" 17426 " B,\n" 17427 " C\n" 17428 "};\n" 17429 "struct B {\n" 17430 " int x;\n" 17431 "};\n" 17432 "}\n", 17433 WebKitBraceStyle); 17434 verifyFormat("struct S {\n" 17435 " int Type;\n" 17436 " union {\n" 17437 " int x;\n" 17438 " double y;\n" 17439 " } Value;\n" 17440 " class C {\n" 17441 " MyFavoriteType Value;\n" 17442 " } Class;\n" 17443 "};\n", 17444 WebKitBraceStyle); 17445 } 17446 17447 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 17448 verifyFormat("void f() {\n" 17449 " try {\n" 17450 " } catch (const Exception &e) {\n" 17451 " }\n" 17452 "}\n", 17453 getLLVMStyle()); 17454 } 17455 17456 TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) { 17457 auto Style = getLLVMStyle(); 17458 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 17459 Style.AlignConsecutiveAssignments = 17460 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 17461 Style.AlignConsecutiveDeclarations = 17462 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 17463 verifyFormat("struct test demo[] = {\n" 17464 " {56, 23, \"hello\"},\n" 17465 " {-1, 93463, \"world\"},\n" 17466 " { 7, 5, \"!!\"}\n" 17467 "};\n", 17468 Style); 17469 17470 verifyFormat("struct test demo[] = {\n" 17471 " {56, 23, \"hello\"}, // first line\n" 17472 " {-1, 93463, \"world\"}, // second line\n" 17473 " { 7, 5, \"!!\"} // third line\n" 17474 "};\n", 17475 Style); 17476 17477 verifyFormat("struct test demo[4] = {\n" 17478 " { 56, 23, 21, \"oh\"}, // first line\n" 17479 " { -1, 93463, 22, \"my\"}, // second line\n" 17480 " { 7, 5, 1, \"goodness\"} // third line\n" 17481 " {234, 5, 1, \"gracious\"} // fourth line\n" 17482 "};\n", 17483 Style); 17484 17485 verifyFormat("struct test demo[3] = {\n" 17486 " {56, 23, \"hello\"},\n" 17487 " {-1, 93463, \"world\"},\n" 17488 " { 7, 5, \"!!\"}\n" 17489 "};\n", 17490 Style); 17491 17492 verifyFormat("struct test demo[3] = {\n" 17493 " {int{56}, 23, \"hello\"},\n" 17494 " {int{-1}, 93463, \"world\"},\n" 17495 " { int{7}, 5, \"!!\"}\n" 17496 "};\n", 17497 Style); 17498 17499 verifyFormat("struct test demo[] = {\n" 17500 " {56, 23, \"hello\"},\n" 17501 " {-1, 93463, \"world\"},\n" 17502 " { 7, 5, \"!!\"},\n" 17503 "};\n", 17504 Style); 17505 17506 verifyFormat("test demo[] = {\n" 17507 " {56, 23, \"hello\"},\n" 17508 " {-1, 93463, \"world\"},\n" 17509 " { 7, 5, \"!!\"},\n" 17510 "};\n", 17511 Style); 17512 17513 verifyFormat("demo = std::array<struct test, 3>{\n" 17514 " test{56, 23, \"hello\"},\n" 17515 " test{-1, 93463, \"world\"},\n" 17516 " test{ 7, 5, \"!!\"},\n" 17517 "};\n", 17518 Style); 17519 17520 verifyFormat("test demo[] = {\n" 17521 " {56, 23, \"hello\"},\n" 17522 "#if X\n" 17523 " {-1, 93463, \"world\"},\n" 17524 "#endif\n" 17525 " { 7, 5, \"!!\"}\n" 17526 "};\n", 17527 Style); 17528 17529 verifyFormat( 17530 "test demo[] = {\n" 17531 " { 7, 23,\n" 17532 " \"hello world i am a very long line that really, in any\"\n" 17533 " \"just world, ought to be split over multiple lines\"},\n" 17534 " {-1, 93463, \"world\"},\n" 17535 " {56, 5, \"!!\"}\n" 17536 "};\n", 17537 Style); 17538 17539 verifyFormat("return GradForUnaryCwise(g, {\n" 17540 " {{\"sign\"}, \"Sign\", " 17541 " {\"x\", \"dy\"}},\n" 17542 " { {\"dx\"}, \"Mul\", {\"dy\"" 17543 ", \"sign\"}},\n" 17544 "});\n", 17545 Style); 17546 17547 Style.ColumnLimit = 0; 17548 EXPECT_EQ( 17549 "test demo[] = {\n" 17550 " {56, 23, \"hello world i am a very long line that really, " 17551 "in any just world, ought to be split over multiple lines\"},\n" 17552 " {-1, 93463, " 17553 " \"world\"},\n" 17554 " { 7, 5, " 17555 " \"!!\"},\n" 17556 "};", 17557 format("test demo[] = {{56, 23, \"hello world i am a very long line " 17558 "that really, in any just world, ought to be split over multiple " 17559 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 17560 Style)); 17561 17562 Style.ColumnLimit = 80; 17563 verifyFormat("test demo[] = {\n" 17564 " {56, 23, /* a comment */ \"hello\"},\n" 17565 " {-1, 93463, \"world\"},\n" 17566 " { 7, 5, \"!!\"}\n" 17567 "};\n", 17568 Style); 17569 17570 verifyFormat("test demo[] = {\n" 17571 " {56, 23, \"hello\"},\n" 17572 " {-1, 93463, \"world\" /* comment here */},\n" 17573 " { 7, 5, \"!!\"}\n" 17574 "};\n", 17575 Style); 17576 17577 verifyFormat("test demo[] = {\n" 17578 " {56, /* a comment */ 23, \"hello\"},\n" 17579 " {-1, 93463, \"world\"},\n" 17580 " { 7, 5, \"!!\"}\n" 17581 "};\n", 17582 Style); 17583 17584 Style.ColumnLimit = 20; 17585 EXPECT_EQ( 17586 "demo = std::array<\n" 17587 " struct test, 3>{\n" 17588 " test{\n" 17589 " 56, 23,\n" 17590 " \"hello \"\n" 17591 " \"world i \"\n" 17592 " \"am a very \"\n" 17593 " \"long line \"\n" 17594 " \"that \"\n" 17595 " \"really, \"\n" 17596 " \"in any \"\n" 17597 " \"just \"\n" 17598 " \"world, \"\n" 17599 " \"ought to \"\n" 17600 " \"be split \"\n" 17601 " \"over \"\n" 17602 " \"multiple \"\n" 17603 " \"lines\"},\n" 17604 " test{-1, 93463,\n" 17605 " \"world\"},\n" 17606 " test{ 7, 5,\n" 17607 " \"!!\" },\n" 17608 "};", 17609 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world " 17610 "i am a very long line that really, in any just world, ought " 17611 "to be split over multiple lines\"},test{-1, 93463, \"world\"}," 17612 "test{7, 5, \"!!\"},};", 17613 Style)); 17614 // This caused a core dump by enabling Alignment in the LLVMStyle globally 17615 Style = getLLVMStyleWithColumns(50); 17616 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 17617 verifyFormat("static A x = {\n" 17618 " {{init1, init2, init3, init4},\n" 17619 " {init1, init2, init3, init4}}\n" 17620 "};", 17621 Style); 17622 Style.ColumnLimit = 100; 17623 EXPECT_EQ( 17624 "test demo[] = {\n" 17625 " {56, 23,\n" 17626 " \"hello world i am a very long line that really, in any just world" 17627 ", ought to be split over \"\n" 17628 " \"multiple lines\" },\n" 17629 " {-1, 93463, \"world\"},\n" 17630 " { 7, 5, \"!!\"},\n" 17631 "};", 17632 format("test demo[] = {{56, 23, \"hello world i am a very long line " 17633 "that really, in any just world, ought to be split over multiple " 17634 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 17635 Style)); 17636 17637 Style = getLLVMStyleWithColumns(50); 17638 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 17639 Style.AlignConsecutiveAssignments = 17640 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 17641 Style.AlignConsecutiveDeclarations = 17642 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 17643 verifyFormat("struct test demo[] = {\n" 17644 " {56, 23, \"hello\"},\n" 17645 " {-1, 93463, \"world\"},\n" 17646 " { 7, 5, \"!!\"}\n" 17647 "};\n" 17648 "static A x = {\n" 17649 " {{init1, init2, init3, init4},\n" 17650 " {init1, init2, init3, init4}}\n" 17651 "};", 17652 Style); 17653 Style.ColumnLimit = 100; 17654 Style.AlignConsecutiveAssignments = 17655 FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments; 17656 Style.AlignConsecutiveDeclarations = 17657 FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments; 17658 verifyFormat("struct test demo[] = {\n" 17659 " {56, 23, \"hello\"},\n" 17660 " {-1, 93463, \"world\"},\n" 17661 " { 7, 5, \"!!\"}\n" 17662 "};\n" 17663 "struct test demo[4] = {\n" 17664 " { 56, 23, 21, \"oh\"}, // first line\n" 17665 " { -1, 93463, 22, \"my\"}, // second line\n" 17666 " { 7, 5, 1, \"goodness\"} // third line\n" 17667 " {234, 5, 1, \"gracious\"} // fourth line\n" 17668 "};\n", 17669 Style); 17670 EXPECT_EQ( 17671 "test demo[] = {\n" 17672 " {56,\n" 17673 " \"hello world i am a very long line that really, in any just world" 17674 ", ought to be split over \"\n" 17675 " \"multiple lines\", 23},\n" 17676 " {-1, \"world\", 93463},\n" 17677 " { 7, \"!!\", 5},\n" 17678 "};", 17679 format("test demo[] = {{56, \"hello world i am a very long line " 17680 "that really, in any just world, ought to be split over multiple " 17681 "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};", 17682 Style)); 17683 } 17684 17685 TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) { 17686 auto Style = getLLVMStyle(); 17687 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; 17688 verifyFormat("struct test demo[] = {\n" 17689 " {56, 23, \"hello\"},\n" 17690 " {-1, 93463, \"world\"},\n" 17691 " {7, 5, \"!!\" }\n" 17692 "};\n", 17693 Style); 17694 17695 verifyFormat("struct test demo[] = {\n" 17696 " {56, 23, \"hello\"}, // first line\n" 17697 " {-1, 93463, \"world\"}, // second line\n" 17698 " {7, 5, \"!!\" } // third line\n" 17699 "};\n", 17700 Style); 17701 verifyFormat("struct test demo[4] = {\n" 17702 " {56, 23, 21, \"oh\" }, // first line\n" 17703 " {-1, 93463, 22, \"my\" }, // second line\n" 17704 " {7, 5, 1, \"goodness\"} // third line\n" 17705 " {234, 5, 1, \"gracious\"} // fourth line\n" 17706 "};\n", 17707 Style); 17708 verifyFormat("struct test demo[3] = {\n" 17709 " {56, 23, \"hello\"},\n" 17710 " {-1, 93463, \"world\"},\n" 17711 " {7, 5, \"!!\" }\n" 17712 "};\n", 17713 Style); 17714 17715 verifyFormat("struct test demo[3] = {\n" 17716 " {int{56}, 23, \"hello\"},\n" 17717 " {int{-1}, 93463, \"world\"},\n" 17718 " {int{7}, 5, \"!!\" }\n" 17719 "};\n", 17720 Style); 17721 verifyFormat("struct test demo[] = {\n" 17722 " {56, 23, \"hello\"},\n" 17723 " {-1, 93463, \"world\"},\n" 17724 " {7, 5, \"!!\" },\n" 17725 "};\n", 17726 Style); 17727 verifyFormat("test demo[] = {\n" 17728 " {56, 23, \"hello\"},\n" 17729 " {-1, 93463, \"world\"},\n" 17730 " {7, 5, \"!!\" },\n" 17731 "};\n", 17732 Style); 17733 verifyFormat("demo = std::array<struct test, 3>{\n" 17734 " test{56, 23, \"hello\"},\n" 17735 " test{-1, 93463, \"world\"},\n" 17736 " test{7, 5, \"!!\" },\n" 17737 "};\n", 17738 Style); 17739 verifyFormat("test demo[] = {\n" 17740 " {56, 23, \"hello\"},\n" 17741 "#if X\n" 17742 " {-1, 93463, \"world\"},\n" 17743 "#endif\n" 17744 " {7, 5, \"!!\" }\n" 17745 "};\n", 17746 Style); 17747 verifyFormat( 17748 "test demo[] = {\n" 17749 " {7, 23,\n" 17750 " \"hello world i am a very long line that really, in any\"\n" 17751 " \"just world, ought to be split over multiple lines\"},\n" 17752 " {-1, 93463, \"world\" },\n" 17753 " {56, 5, \"!!\" }\n" 17754 "};\n", 17755 Style); 17756 17757 verifyFormat("return GradForUnaryCwise(g, {\n" 17758 " {{\"sign\"}, \"Sign\", {\"x\", " 17759 "\"dy\"} },\n" 17760 " {{\"dx\"}, \"Mul\", " 17761 "{\"dy\", \"sign\"}},\n" 17762 "});\n", 17763 Style); 17764 17765 Style.ColumnLimit = 0; 17766 EXPECT_EQ( 17767 "test demo[] = {\n" 17768 " {56, 23, \"hello world i am a very long line that really, in any " 17769 "just world, ought to be split over multiple lines\"},\n" 17770 " {-1, 93463, \"world\" " 17771 " },\n" 17772 " {7, 5, \"!!\" " 17773 " },\n" 17774 "};", 17775 format("test demo[] = {{56, 23, \"hello world i am a very long line " 17776 "that really, in any just world, ought to be split over multiple " 17777 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 17778 Style)); 17779 17780 Style.ColumnLimit = 80; 17781 verifyFormat("test demo[] = {\n" 17782 " {56, 23, /* a comment */ \"hello\"},\n" 17783 " {-1, 93463, \"world\" },\n" 17784 " {7, 5, \"!!\" }\n" 17785 "};\n", 17786 Style); 17787 17788 verifyFormat("test demo[] = {\n" 17789 " {56, 23, \"hello\" },\n" 17790 " {-1, 93463, \"world\" /* comment here */},\n" 17791 " {7, 5, \"!!\" }\n" 17792 "};\n", 17793 Style); 17794 17795 verifyFormat("test demo[] = {\n" 17796 " {56, /* a comment */ 23, \"hello\"},\n" 17797 " {-1, 93463, \"world\"},\n" 17798 " {7, 5, \"!!\" }\n" 17799 "};\n", 17800 Style); 17801 17802 Style.ColumnLimit = 20; 17803 EXPECT_EQ( 17804 "demo = std::array<\n" 17805 " struct test, 3>{\n" 17806 " test{\n" 17807 " 56, 23,\n" 17808 " \"hello \"\n" 17809 " \"world i \"\n" 17810 " \"am a very \"\n" 17811 " \"long line \"\n" 17812 " \"that \"\n" 17813 " \"really, \"\n" 17814 " \"in any \"\n" 17815 " \"just \"\n" 17816 " \"world, \"\n" 17817 " \"ought to \"\n" 17818 " \"be split \"\n" 17819 " \"over \"\n" 17820 " \"multiple \"\n" 17821 " \"lines\"},\n" 17822 " test{-1, 93463,\n" 17823 " \"world\"},\n" 17824 " test{7, 5,\n" 17825 " \"!!\" },\n" 17826 "};", 17827 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world " 17828 "i am a very long line that really, in any just world, ought " 17829 "to be split over multiple lines\"},test{-1, 93463, \"world\"}," 17830 "test{7, 5, \"!!\"},};", 17831 Style)); 17832 17833 // This caused a core dump by enabling Alignment in the LLVMStyle globally 17834 Style = getLLVMStyleWithColumns(50); 17835 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; 17836 verifyFormat("static A x = {\n" 17837 " {{init1, init2, init3, init4},\n" 17838 " {init1, init2, init3, init4}}\n" 17839 "};", 17840 Style); 17841 Style.ColumnLimit = 100; 17842 EXPECT_EQ( 17843 "test demo[] = {\n" 17844 " {56, 23,\n" 17845 " \"hello world i am a very long line that really, in any just world" 17846 ", ought to be split over \"\n" 17847 " \"multiple lines\" },\n" 17848 " {-1, 93463, \"world\"},\n" 17849 " {7, 5, \"!!\" },\n" 17850 "};", 17851 format("test demo[] = {{56, 23, \"hello world i am a very long line " 17852 "that really, in any just world, ought to be split over multiple " 17853 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 17854 Style)); 17855 } 17856 17857 TEST_F(FormatTest, UnderstandsPragmas) { 17858 verifyFormat("#pragma omp reduction(| : var)"); 17859 verifyFormat("#pragma omp reduction(+ : var)"); 17860 17861 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 17862 "(including parentheses).", 17863 format("#pragma mark Any non-hyphenated or hyphenated string " 17864 "(including parentheses).")); 17865 } 17866 17867 TEST_F(FormatTest, UnderstandPragmaOption) { 17868 verifyFormat("#pragma option -C -A"); 17869 17870 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 17871 } 17872 17873 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) { 17874 FormatStyle Style = getLLVMStyle(); 17875 Style.ColumnLimit = 20; 17876 17877 // See PR41213 17878 EXPECT_EQ("/*\n" 17879 " *\t9012345\n" 17880 " * /8901\n" 17881 " */", 17882 format("/*\n" 17883 " *\t9012345 /8901\n" 17884 " */", 17885 Style)); 17886 EXPECT_EQ("/*\n" 17887 " *345678\n" 17888 " *\t/8901\n" 17889 " */", 17890 format("/*\n" 17891 " *345678\t/8901\n" 17892 " */", 17893 Style)); 17894 17895 verifyFormat("int a; // the\n" 17896 " // comment", 17897 Style); 17898 EXPECT_EQ("int a; /* first line\n" 17899 " * second\n" 17900 " * line third\n" 17901 " * line\n" 17902 " */", 17903 format("int a; /* first line\n" 17904 " * second\n" 17905 " * line third\n" 17906 " * line\n" 17907 " */", 17908 Style)); 17909 EXPECT_EQ("int a; // first line\n" 17910 " // second\n" 17911 " // line third\n" 17912 " // line", 17913 format("int a; // first line\n" 17914 " // second line\n" 17915 " // third line", 17916 Style)); 17917 17918 Style.PenaltyExcessCharacter = 90; 17919 verifyFormat("int a; // the comment", Style); 17920 EXPECT_EQ("int a; // the comment\n" 17921 " // aaa", 17922 format("int a; // the comment aaa", Style)); 17923 EXPECT_EQ("int a; /* first line\n" 17924 " * second line\n" 17925 " * third line\n" 17926 " */", 17927 format("int a; /* first line\n" 17928 " * second line\n" 17929 " * third line\n" 17930 " */", 17931 Style)); 17932 EXPECT_EQ("int a; // first line\n" 17933 " // second line\n" 17934 " // third line", 17935 format("int a; // first line\n" 17936 " // second line\n" 17937 " // third line", 17938 Style)); 17939 // FIXME: Investigate why this is not getting the same layout as the test 17940 // above. 17941 EXPECT_EQ("int a; /* first line\n" 17942 " * second line\n" 17943 " * third line\n" 17944 " */", 17945 format("int a; /* first line second line third line" 17946 "\n*/", 17947 Style)); 17948 17949 EXPECT_EQ("// foo bar baz bazfoo\n" 17950 "// foo bar foo bar\n", 17951 format("// foo bar baz bazfoo\n" 17952 "// foo bar foo bar\n", 17953 Style)); 17954 EXPECT_EQ("// foo bar baz bazfoo\n" 17955 "// foo bar foo bar\n", 17956 format("// foo bar baz bazfoo\n" 17957 "// foo bar foo bar\n", 17958 Style)); 17959 17960 // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the 17961 // next one. 17962 EXPECT_EQ("// foo bar baz bazfoo\n" 17963 "// bar foo bar\n", 17964 format("// foo bar baz bazfoo bar\n" 17965 "// foo bar\n", 17966 Style)); 17967 17968 EXPECT_EQ("// foo bar baz bazfoo\n" 17969 "// foo bar baz bazfoo\n" 17970 "// bar foo bar\n", 17971 format("// foo bar baz bazfoo\n" 17972 "// foo bar baz bazfoo bar\n" 17973 "// foo bar\n", 17974 Style)); 17975 17976 EXPECT_EQ("// foo bar baz bazfoo\n" 17977 "// foo bar baz bazfoo\n" 17978 "// bar foo bar\n", 17979 format("// foo bar baz bazfoo\n" 17980 "// foo bar baz bazfoo bar\n" 17981 "// foo bar\n", 17982 Style)); 17983 17984 // Make sure we do not keep protruding characters if strict mode reflow is 17985 // cheaper than keeping protruding characters. 17986 Style.ColumnLimit = 21; 17987 EXPECT_EQ( 17988 "// foo foo foo foo\n" 17989 "// foo foo foo foo\n" 17990 "// foo foo foo foo\n", 17991 format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", Style)); 17992 17993 EXPECT_EQ("int a = /* long block\n" 17994 " comment */\n" 17995 " 42;", 17996 format("int a = /* long block comment */ 42;", Style)); 17997 } 17998 17999 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 18000 for (size_t i = 1; i < Styles.size(); ++i) \ 18001 EXPECT_EQ(Styles[0], Styles[i]) \ 18002 << "Style #" << i << " of " << Styles.size() << " differs from Style #0" 18003 18004 TEST_F(FormatTest, GetsPredefinedStyleByName) { 18005 SmallVector<FormatStyle, 3> Styles; 18006 Styles.resize(3); 18007 18008 Styles[0] = getLLVMStyle(); 18009 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 18010 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 18011 EXPECT_ALL_STYLES_EQUAL(Styles); 18012 18013 Styles[0] = getGoogleStyle(); 18014 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 18015 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 18016 EXPECT_ALL_STYLES_EQUAL(Styles); 18017 18018 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 18019 EXPECT_TRUE( 18020 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 18021 EXPECT_TRUE( 18022 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 18023 EXPECT_ALL_STYLES_EQUAL(Styles); 18024 18025 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 18026 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 18027 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 18028 EXPECT_ALL_STYLES_EQUAL(Styles); 18029 18030 Styles[0] = getMozillaStyle(); 18031 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 18032 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 18033 EXPECT_ALL_STYLES_EQUAL(Styles); 18034 18035 Styles[0] = getWebKitStyle(); 18036 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 18037 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 18038 EXPECT_ALL_STYLES_EQUAL(Styles); 18039 18040 Styles[0] = getGNUStyle(); 18041 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 18042 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 18043 EXPECT_ALL_STYLES_EQUAL(Styles); 18044 18045 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 18046 } 18047 18048 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 18049 SmallVector<FormatStyle, 8> Styles; 18050 Styles.resize(2); 18051 18052 Styles[0] = getGoogleStyle(); 18053 Styles[1] = getLLVMStyle(); 18054 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 18055 EXPECT_ALL_STYLES_EQUAL(Styles); 18056 18057 Styles.resize(5); 18058 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 18059 Styles[1] = getLLVMStyle(); 18060 Styles[1].Language = FormatStyle::LK_JavaScript; 18061 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 18062 18063 Styles[2] = getLLVMStyle(); 18064 Styles[2].Language = FormatStyle::LK_JavaScript; 18065 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 18066 "BasedOnStyle: Google", 18067 &Styles[2]) 18068 .value()); 18069 18070 Styles[3] = getLLVMStyle(); 18071 Styles[3].Language = FormatStyle::LK_JavaScript; 18072 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 18073 "Language: JavaScript", 18074 &Styles[3]) 18075 .value()); 18076 18077 Styles[4] = getLLVMStyle(); 18078 Styles[4].Language = FormatStyle::LK_JavaScript; 18079 EXPECT_EQ(0, parseConfiguration("---\n" 18080 "BasedOnStyle: LLVM\n" 18081 "IndentWidth: 123\n" 18082 "---\n" 18083 "BasedOnStyle: Google\n" 18084 "Language: JavaScript", 18085 &Styles[4]) 18086 .value()); 18087 EXPECT_ALL_STYLES_EQUAL(Styles); 18088 } 18089 18090 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 18091 Style.FIELD = false; \ 18092 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 18093 EXPECT_TRUE(Style.FIELD); \ 18094 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 18095 EXPECT_FALSE(Style.FIELD); 18096 18097 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 18098 18099 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 18100 Style.STRUCT.FIELD = false; \ 18101 EXPECT_EQ(0, \ 18102 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 18103 .value()); \ 18104 EXPECT_TRUE(Style.STRUCT.FIELD); \ 18105 EXPECT_EQ(0, \ 18106 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 18107 .value()); \ 18108 EXPECT_FALSE(Style.STRUCT.FIELD); 18109 18110 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 18111 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 18112 18113 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 18114 EXPECT_NE(VALUE, Style.FIELD) << "Initial value already the same!"; \ 18115 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 18116 EXPECT_EQ(VALUE, Style.FIELD) << "Unexpected value after parsing!" 18117 18118 TEST_F(FormatTest, ParsesConfigurationBools) { 18119 FormatStyle Style = {}; 18120 Style.Language = FormatStyle::LK_Cpp; 18121 CHECK_PARSE_BOOL(AlignTrailingComments); 18122 CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine); 18123 CHECK_PARSE_BOOL(AllowAllConstructorInitializersOnNextLine); 18124 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 18125 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 18126 CHECK_PARSE_BOOL(AllowShortEnumsOnASingleLine); 18127 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 18128 CHECK_PARSE_BOOL(BinPackArguments); 18129 CHECK_PARSE_BOOL(BinPackParameters); 18130 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 18131 CHECK_PARSE_BOOL(BreakBeforeConceptDeclarations); 18132 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 18133 CHECK_PARSE_BOOL(BreakStringLiterals); 18134 CHECK_PARSE_BOOL(CompactNamespaces); 18135 CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); 18136 CHECK_PARSE_BOOL(DeriveLineEnding); 18137 CHECK_PARSE_BOOL(DerivePointerAlignment); 18138 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 18139 CHECK_PARSE_BOOL(DisableFormat); 18140 CHECK_PARSE_BOOL(IndentAccessModifiers); 18141 CHECK_PARSE_BOOL(IndentCaseLabels); 18142 CHECK_PARSE_BOOL(IndentCaseBlocks); 18143 CHECK_PARSE_BOOL(IndentGotoLabels); 18144 CHECK_PARSE_BOOL(IndentRequires); 18145 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 18146 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 18147 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 18148 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 18149 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 18150 CHECK_PARSE_BOOL(ReflowComments); 18151 CHECK_PARSE_BOOL(SortUsingDeclarations); 18152 CHECK_PARSE_BOOL(SpacesInParentheses); 18153 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 18154 CHECK_PARSE_BOOL(SpacesInConditionalStatement); 18155 CHECK_PARSE_BOOL(SpaceInEmptyBlock); 18156 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 18157 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 18158 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 18159 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 18160 CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); 18161 CHECK_PARSE_BOOL(SpaceAfterLogicalNot); 18162 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 18163 CHECK_PARSE_BOOL(SpaceBeforeCaseColon); 18164 CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList); 18165 CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon); 18166 CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon); 18167 CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon); 18168 CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets); 18169 CHECK_PARSE_BOOL(UseCRLF); 18170 18171 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel); 18172 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 18173 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 18174 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 18175 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 18176 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 18177 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 18178 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 18179 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock); 18180 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 18181 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 18182 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeLambdaBody); 18183 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeWhile); 18184 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 18185 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction); 18186 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord); 18187 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace); 18188 } 18189 18190 #undef CHECK_PARSE_BOOL 18191 18192 TEST_F(FormatTest, ParsesConfiguration) { 18193 FormatStyle Style = {}; 18194 Style.Language = FormatStyle::LK_Cpp; 18195 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 18196 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 18197 ConstructorInitializerIndentWidth, 1234u); 18198 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 18199 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 18200 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 18201 CHECK_PARSE("PenaltyBreakAssignment: 1234", PenaltyBreakAssignment, 1234u); 18202 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 18203 PenaltyBreakBeforeFirstCallParameter, 1234u); 18204 CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234", 18205 PenaltyBreakTemplateDeclaration, 1234u); 18206 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 18207 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 18208 PenaltyReturnTypeOnItsOwnLine, 1234u); 18209 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 18210 SpacesBeforeTrailingComments, 1234u); 18211 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 18212 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 18213 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$"); 18214 18215 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 18216 CHECK_PARSE("AlignConsecutiveAssignments: None", AlignConsecutiveAssignments, 18217 FormatStyle::ACS_None); 18218 CHECK_PARSE("AlignConsecutiveAssignments: Consecutive", 18219 AlignConsecutiveAssignments, FormatStyle::ACS_Consecutive); 18220 CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLines", 18221 AlignConsecutiveAssignments, FormatStyle::ACS_AcrossEmptyLines); 18222 CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLinesAndComments", 18223 AlignConsecutiveAssignments, 18224 FormatStyle::ACS_AcrossEmptyLinesAndComments); 18225 // For backwards compability, false / true should still parse 18226 CHECK_PARSE("AlignConsecutiveAssignments: false", AlignConsecutiveAssignments, 18227 FormatStyle::ACS_None); 18228 CHECK_PARSE("AlignConsecutiveAssignments: true", AlignConsecutiveAssignments, 18229 FormatStyle::ACS_Consecutive); 18230 18231 Style.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive; 18232 CHECK_PARSE("AlignConsecutiveBitFields: None", AlignConsecutiveBitFields, 18233 FormatStyle::ACS_None); 18234 CHECK_PARSE("AlignConsecutiveBitFields: Consecutive", 18235 AlignConsecutiveBitFields, FormatStyle::ACS_Consecutive); 18236 CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLines", 18237 AlignConsecutiveBitFields, FormatStyle::ACS_AcrossEmptyLines); 18238 CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLinesAndComments", 18239 AlignConsecutiveBitFields, 18240 FormatStyle::ACS_AcrossEmptyLinesAndComments); 18241 // For backwards compability, false / true should still parse 18242 CHECK_PARSE("AlignConsecutiveBitFields: false", AlignConsecutiveBitFields, 18243 FormatStyle::ACS_None); 18244 CHECK_PARSE("AlignConsecutiveBitFields: true", AlignConsecutiveBitFields, 18245 FormatStyle::ACS_Consecutive); 18246 18247 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 18248 CHECK_PARSE("AlignConsecutiveMacros: None", AlignConsecutiveMacros, 18249 FormatStyle::ACS_None); 18250 CHECK_PARSE("AlignConsecutiveMacros: Consecutive", AlignConsecutiveMacros, 18251 FormatStyle::ACS_Consecutive); 18252 CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLines", 18253 AlignConsecutiveMacros, FormatStyle::ACS_AcrossEmptyLines); 18254 CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLinesAndComments", 18255 AlignConsecutiveMacros, 18256 FormatStyle::ACS_AcrossEmptyLinesAndComments); 18257 // For backwards compability, false / true should still parse 18258 CHECK_PARSE("AlignConsecutiveMacros: false", AlignConsecutiveMacros, 18259 FormatStyle::ACS_None); 18260 CHECK_PARSE("AlignConsecutiveMacros: true", AlignConsecutiveMacros, 18261 FormatStyle::ACS_Consecutive); 18262 18263 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 18264 CHECK_PARSE("AlignConsecutiveDeclarations: None", 18265 AlignConsecutiveDeclarations, FormatStyle::ACS_None); 18266 CHECK_PARSE("AlignConsecutiveDeclarations: Consecutive", 18267 AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive); 18268 CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLines", 18269 AlignConsecutiveDeclarations, FormatStyle::ACS_AcrossEmptyLines); 18270 CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLinesAndComments", 18271 AlignConsecutiveDeclarations, 18272 FormatStyle::ACS_AcrossEmptyLinesAndComments); 18273 // For backwards compability, false / true should still parse 18274 CHECK_PARSE("AlignConsecutiveDeclarations: false", 18275 AlignConsecutiveDeclarations, FormatStyle::ACS_None); 18276 CHECK_PARSE("AlignConsecutiveDeclarations: true", 18277 AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive); 18278 18279 Style.PointerAlignment = FormatStyle::PAS_Middle; 18280 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 18281 FormatStyle::PAS_Left); 18282 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 18283 FormatStyle::PAS_Right); 18284 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 18285 FormatStyle::PAS_Middle); 18286 Style.ReferenceAlignment = FormatStyle::RAS_Middle; 18287 CHECK_PARSE("ReferenceAlignment: Pointer", ReferenceAlignment, 18288 FormatStyle::RAS_Pointer); 18289 CHECK_PARSE("ReferenceAlignment: Left", ReferenceAlignment, 18290 FormatStyle::RAS_Left); 18291 CHECK_PARSE("ReferenceAlignment: Right", ReferenceAlignment, 18292 FormatStyle::RAS_Right); 18293 CHECK_PARSE("ReferenceAlignment: Middle", ReferenceAlignment, 18294 FormatStyle::RAS_Middle); 18295 // For backward compatibility: 18296 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 18297 FormatStyle::PAS_Left); 18298 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 18299 FormatStyle::PAS_Right); 18300 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 18301 FormatStyle::PAS_Middle); 18302 18303 Style.Standard = FormatStyle::LS_Auto; 18304 CHECK_PARSE("Standard: c++03", Standard, FormatStyle::LS_Cpp03); 18305 CHECK_PARSE("Standard: c++11", Standard, FormatStyle::LS_Cpp11); 18306 CHECK_PARSE("Standard: c++14", Standard, FormatStyle::LS_Cpp14); 18307 CHECK_PARSE("Standard: c++17", Standard, FormatStyle::LS_Cpp17); 18308 CHECK_PARSE("Standard: c++20", Standard, FormatStyle::LS_Cpp20); 18309 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 18310 CHECK_PARSE("Standard: Latest", Standard, FormatStyle::LS_Latest); 18311 // Legacy aliases: 18312 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 18313 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Latest); 18314 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 18315 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 18316 18317 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 18318 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 18319 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 18320 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 18321 FormatStyle::BOS_None); 18322 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 18323 FormatStyle::BOS_All); 18324 // For backward compatibility: 18325 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 18326 FormatStyle::BOS_None); 18327 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 18328 FormatStyle::BOS_All); 18329 18330 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 18331 CHECK_PARSE("BreakConstructorInitializers: BeforeComma", 18332 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 18333 CHECK_PARSE("BreakConstructorInitializers: AfterColon", 18334 BreakConstructorInitializers, FormatStyle::BCIS_AfterColon); 18335 CHECK_PARSE("BreakConstructorInitializers: BeforeColon", 18336 BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon); 18337 // For backward compatibility: 18338 CHECK_PARSE("BreakConstructorInitializersBeforeComma: true", 18339 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 18340 18341 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 18342 CHECK_PARSE("BreakInheritanceList: AfterComma", BreakInheritanceList, 18343 FormatStyle::BILS_AfterComma); 18344 CHECK_PARSE("BreakInheritanceList: BeforeComma", BreakInheritanceList, 18345 FormatStyle::BILS_BeforeComma); 18346 CHECK_PARSE("BreakInheritanceList: AfterColon", BreakInheritanceList, 18347 FormatStyle::BILS_AfterColon); 18348 CHECK_PARSE("BreakInheritanceList: BeforeColon", BreakInheritanceList, 18349 FormatStyle::BILS_BeforeColon); 18350 // For backward compatibility: 18351 CHECK_PARSE("BreakBeforeInheritanceComma: true", BreakInheritanceList, 18352 FormatStyle::BILS_BeforeComma); 18353 18354 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 18355 CHECK_PARSE("EmptyLineBeforeAccessModifier: Never", 18356 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Never); 18357 CHECK_PARSE("EmptyLineBeforeAccessModifier: Leave", 18358 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Leave); 18359 CHECK_PARSE("EmptyLineBeforeAccessModifier: LogicalBlock", 18360 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_LogicalBlock); 18361 CHECK_PARSE("EmptyLineBeforeAccessModifier: Always", 18362 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Always); 18363 18364 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 18365 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 18366 FormatStyle::BAS_Align); 18367 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 18368 FormatStyle::BAS_DontAlign); 18369 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 18370 FormatStyle::BAS_AlwaysBreak); 18371 // For backward compatibility: 18372 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 18373 FormatStyle::BAS_DontAlign); 18374 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 18375 FormatStyle::BAS_Align); 18376 18377 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left; 18378 CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines, 18379 FormatStyle::ENAS_DontAlign); 18380 CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines, 18381 FormatStyle::ENAS_Left); 18382 CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines, 18383 FormatStyle::ENAS_Right); 18384 // For backward compatibility: 18385 CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines, 18386 FormatStyle::ENAS_Left); 18387 CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines, 18388 FormatStyle::ENAS_Right); 18389 18390 Style.AlignOperands = FormatStyle::OAS_Align; 18391 CHECK_PARSE("AlignOperands: DontAlign", AlignOperands, 18392 FormatStyle::OAS_DontAlign); 18393 CHECK_PARSE("AlignOperands: Align", AlignOperands, FormatStyle::OAS_Align); 18394 CHECK_PARSE("AlignOperands: AlignAfterOperator", AlignOperands, 18395 FormatStyle::OAS_AlignAfterOperator); 18396 // For backward compatibility: 18397 CHECK_PARSE("AlignOperands: false", AlignOperands, 18398 FormatStyle::OAS_DontAlign); 18399 CHECK_PARSE("AlignOperands: true", AlignOperands, FormatStyle::OAS_Align); 18400 18401 Style.UseTab = FormatStyle::UT_ForIndentation; 18402 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 18403 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 18404 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 18405 CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab, 18406 FormatStyle::UT_ForContinuationAndIndentation); 18407 CHECK_PARSE("UseTab: AlignWithSpaces", UseTab, 18408 FormatStyle::UT_AlignWithSpaces); 18409 // For backward compatibility: 18410 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 18411 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 18412 18413 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 18414 CHECK_PARSE("AllowShortBlocksOnASingleLine: Never", 18415 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 18416 CHECK_PARSE("AllowShortBlocksOnASingleLine: Empty", 18417 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Empty); 18418 CHECK_PARSE("AllowShortBlocksOnASingleLine: Always", 18419 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always); 18420 // For backward compatibility: 18421 CHECK_PARSE("AllowShortBlocksOnASingleLine: false", 18422 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 18423 CHECK_PARSE("AllowShortBlocksOnASingleLine: true", 18424 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always); 18425 18426 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 18427 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 18428 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 18429 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 18430 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 18431 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 18432 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 18433 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 18434 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 18435 // For backward compatibility: 18436 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 18437 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 18438 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 18439 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 18440 18441 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Both; 18442 CHECK_PARSE("SpaceAroundPointerQualifiers: Default", 18443 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Default); 18444 CHECK_PARSE("SpaceAroundPointerQualifiers: Before", 18445 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Before); 18446 CHECK_PARSE("SpaceAroundPointerQualifiers: After", 18447 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_After); 18448 CHECK_PARSE("SpaceAroundPointerQualifiers: Both", 18449 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Both); 18450 18451 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 18452 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 18453 FormatStyle::SBPO_Never); 18454 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 18455 FormatStyle::SBPO_Always); 18456 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 18457 FormatStyle::SBPO_ControlStatements); 18458 CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptControlMacros", 18459 SpaceBeforeParens, 18460 FormatStyle::SBPO_ControlStatementsExceptControlMacros); 18461 CHECK_PARSE("SpaceBeforeParens: NonEmptyParentheses", SpaceBeforeParens, 18462 FormatStyle::SBPO_NonEmptyParentheses); 18463 // For backward compatibility: 18464 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 18465 FormatStyle::SBPO_Never); 18466 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 18467 FormatStyle::SBPO_ControlStatements); 18468 CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptForEachMacros", 18469 SpaceBeforeParens, 18470 FormatStyle::SBPO_ControlStatementsExceptControlMacros); 18471 18472 Style.ColumnLimit = 123; 18473 FormatStyle BaseStyle = getLLVMStyle(); 18474 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 18475 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 18476 18477 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 18478 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 18479 FormatStyle::BS_Attach); 18480 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 18481 FormatStyle::BS_Linux); 18482 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 18483 FormatStyle::BS_Mozilla); 18484 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 18485 FormatStyle::BS_Stroustrup); 18486 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 18487 FormatStyle::BS_Allman); 18488 CHECK_PARSE("BreakBeforeBraces: Whitesmiths", BreakBeforeBraces, 18489 FormatStyle::BS_Whitesmiths); 18490 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 18491 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 18492 FormatStyle::BS_WebKit); 18493 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 18494 FormatStyle::BS_Custom); 18495 18496 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never; 18497 CHECK_PARSE("BraceWrapping:\n" 18498 " AfterControlStatement: MultiLine", 18499 BraceWrapping.AfterControlStatement, 18500 FormatStyle::BWACS_MultiLine); 18501 CHECK_PARSE("BraceWrapping:\n" 18502 " AfterControlStatement: Always", 18503 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always); 18504 CHECK_PARSE("BraceWrapping:\n" 18505 " AfterControlStatement: Never", 18506 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never); 18507 // For backward compatibility: 18508 CHECK_PARSE("BraceWrapping:\n" 18509 " AfterControlStatement: true", 18510 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always); 18511 CHECK_PARSE("BraceWrapping:\n" 18512 " AfterControlStatement: false", 18513 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never); 18514 18515 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 18516 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 18517 FormatStyle::RTBS_None); 18518 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 18519 FormatStyle::RTBS_All); 18520 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 18521 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 18522 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 18523 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 18524 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 18525 AlwaysBreakAfterReturnType, 18526 FormatStyle::RTBS_TopLevelDefinitions); 18527 18528 Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 18529 CHECK_PARSE("AlwaysBreakTemplateDeclarations: No", 18530 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_No); 18531 CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine", 18532 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine); 18533 CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes", 18534 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes); 18535 CHECK_PARSE("AlwaysBreakTemplateDeclarations: false", 18536 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine); 18537 CHECK_PARSE("AlwaysBreakTemplateDeclarations: true", 18538 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes); 18539 18540 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 18541 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 18542 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 18543 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 18544 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 18545 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 18546 AlwaysBreakAfterDefinitionReturnType, 18547 FormatStyle::DRTBS_TopLevel); 18548 18549 Style.NamespaceIndentation = FormatStyle::NI_All; 18550 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 18551 FormatStyle::NI_None); 18552 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 18553 FormatStyle::NI_Inner); 18554 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 18555 FormatStyle::NI_All); 18556 18557 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_OnlyFirstIf; 18558 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Never", 18559 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never); 18560 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: WithoutElse", 18561 AllowShortIfStatementsOnASingleLine, 18562 FormatStyle::SIS_WithoutElse); 18563 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: OnlyFirstIf", 18564 AllowShortIfStatementsOnASingleLine, 18565 FormatStyle::SIS_OnlyFirstIf); 18566 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: AllIfsAndElse", 18567 AllowShortIfStatementsOnASingleLine, 18568 FormatStyle::SIS_AllIfsAndElse); 18569 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Always", 18570 AllowShortIfStatementsOnASingleLine, 18571 FormatStyle::SIS_OnlyFirstIf); 18572 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: false", 18573 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never); 18574 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: true", 18575 AllowShortIfStatementsOnASingleLine, 18576 FormatStyle::SIS_WithoutElse); 18577 18578 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; 18579 CHECK_PARSE("IndentExternBlock: AfterExternBlock", IndentExternBlock, 18580 FormatStyle::IEBS_AfterExternBlock); 18581 CHECK_PARSE("IndentExternBlock: Indent", IndentExternBlock, 18582 FormatStyle::IEBS_Indent); 18583 CHECK_PARSE("IndentExternBlock: NoIndent", IndentExternBlock, 18584 FormatStyle::IEBS_NoIndent); 18585 CHECK_PARSE("IndentExternBlock: true", IndentExternBlock, 18586 FormatStyle::IEBS_Indent); 18587 CHECK_PARSE("IndentExternBlock: false", IndentExternBlock, 18588 FormatStyle::IEBS_NoIndent); 18589 18590 Style.BitFieldColonSpacing = FormatStyle::BFCS_None; 18591 CHECK_PARSE("BitFieldColonSpacing: Both", BitFieldColonSpacing, 18592 FormatStyle::BFCS_Both); 18593 CHECK_PARSE("BitFieldColonSpacing: None", BitFieldColonSpacing, 18594 FormatStyle::BFCS_None); 18595 CHECK_PARSE("BitFieldColonSpacing: Before", BitFieldColonSpacing, 18596 FormatStyle::BFCS_Before); 18597 CHECK_PARSE("BitFieldColonSpacing: After", BitFieldColonSpacing, 18598 FormatStyle::BFCS_After); 18599 18600 Style.SortJavaStaticImport = FormatStyle::SJSIO_Before; 18601 CHECK_PARSE("SortJavaStaticImport: After", SortJavaStaticImport, 18602 FormatStyle::SJSIO_After); 18603 CHECK_PARSE("SortJavaStaticImport: Before", SortJavaStaticImport, 18604 FormatStyle::SJSIO_Before); 18605 18606 // FIXME: This is required because parsing a configuration simply overwrites 18607 // the first N elements of the list instead of resetting it. 18608 Style.ForEachMacros.clear(); 18609 std::vector<std::string> BoostForeach; 18610 BoostForeach.push_back("BOOST_FOREACH"); 18611 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 18612 std::vector<std::string> BoostAndQForeach; 18613 BoostAndQForeach.push_back("BOOST_FOREACH"); 18614 BoostAndQForeach.push_back("Q_FOREACH"); 18615 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 18616 BoostAndQForeach); 18617 18618 Style.IfMacros.clear(); 18619 std::vector<std::string> CustomIfs; 18620 CustomIfs.push_back("MYIF"); 18621 CHECK_PARSE("IfMacros: [MYIF]", IfMacros, CustomIfs); 18622 18623 Style.AttributeMacros.clear(); 18624 CHECK_PARSE("BasedOnStyle: LLVM", AttributeMacros, 18625 std::vector<std::string>{"__capability"}); 18626 CHECK_PARSE("AttributeMacros: [attr1, attr2]", AttributeMacros, 18627 std::vector<std::string>({"attr1", "attr2"})); 18628 18629 Style.StatementAttributeLikeMacros.clear(); 18630 CHECK_PARSE("StatementAttributeLikeMacros: [emit,Q_EMIT]", 18631 StatementAttributeLikeMacros, 18632 std::vector<std::string>({"emit", "Q_EMIT"})); 18633 18634 Style.StatementMacros.clear(); 18635 CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros, 18636 std::vector<std::string>{"QUNUSED"}); 18637 CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros, 18638 std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"})); 18639 18640 Style.NamespaceMacros.clear(); 18641 CHECK_PARSE("NamespaceMacros: [TESTSUITE]", NamespaceMacros, 18642 std::vector<std::string>{"TESTSUITE"}); 18643 CHECK_PARSE("NamespaceMacros: [TESTSUITE, SUITE]", NamespaceMacros, 18644 std::vector<std::string>({"TESTSUITE", "SUITE"})); 18645 18646 Style.WhitespaceSensitiveMacros.clear(); 18647 CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE]", 18648 WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"}); 18649 CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE, ASSERT]", 18650 WhitespaceSensitiveMacros, 18651 std::vector<std::string>({"STRINGIZE", "ASSERT"})); 18652 Style.WhitespaceSensitiveMacros.clear(); 18653 CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE']", 18654 WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"}); 18655 CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE', 'ASSERT']", 18656 WhitespaceSensitiveMacros, 18657 std::vector<std::string>({"STRINGIZE", "ASSERT"})); 18658 18659 Style.IncludeStyle.IncludeCategories.clear(); 18660 std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = { 18661 {"abc/.*", 2, 0, false}, {".*", 1, 0, true}}; 18662 CHECK_PARSE("IncludeCategories:\n" 18663 " - Regex: abc/.*\n" 18664 " Priority: 2\n" 18665 " - Regex: .*\n" 18666 " Priority: 1\n" 18667 " CaseSensitive: true\n", 18668 IncludeStyle.IncludeCategories, ExpectedCategories); 18669 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex, 18670 "abc$"); 18671 CHECK_PARSE("IncludeIsMainSourceRegex: 'abc$'", 18672 IncludeStyle.IncludeIsMainSourceRegex, "abc$"); 18673 18674 Style.SortIncludes = FormatStyle::SI_Never; 18675 CHECK_PARSE("SortIncludes: true", SortIncludes, 18676 FormatStyle::SI_CaseSensitive); 18677 CHECK_PARSE("SortIncludes: false", SortIncludes, FormatStyle::SI_Never); 18678 CHECK_PARSE("SortIncludes: CaseInsensitive", SortIncludes, 18679 FormatStyle::SI_CaseInsensitive); 18680 CHECK_PARSE("SortIncludes: CaseSensitive", SortIncludes, 18681 FormatStyle::SI_CaseSensitive); 18682 CHECK_PARSE("SortIncludes: Never", SortIncludes, FormatStyle::SI_Never); 18683 18684 Style.RawStringFormats.clear(); 18685 std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = { 18686 { 18687 FormatStyle::LK_TextProto, 18688 {"pb", "proto"}, 18689 {"PARSE_TEXT_PROTO"}, 18690 /*CanonicalDelimiter=*/"", 18691 "llvm", 18692 }, 18693 { 18694 FormatStyle::LK_Cpp, 18695 {"cc", "cpp"}, 18696 {"C_CODEBLOCK", "CPPEVAL"}, 18697 /*CanonicalDelimiter=*/"cc", 18698 /*BasedOnStyle=*/"", 18699 }, 18700 }; 18701 18702 CHECK_PARSE("RawStringFormats:\n" 18703 " - Language: TextProto\n" 18704 " Delimiters:\n" 18705 " - 'pb'\n" 18706 " - 'proto'\n" 18707 " EnclosingFunctions:\n" 18708 " - 'PARSE_TEXT_PROTO'\n" 18709 " BasedOnStyle: llvm\n" 18710 " - Language: Cpp\n" 18711 " Delimiters:\n" 18712 " - 'cc'\n" 18713 " - 'cpp'\n" 18714 " EnclosingFunctions:\n" 18715 " - 'C_CODEBLOCK'\n" 18716 " - 'CPPEVAL'\n" 18717 " CanonicalDelimiter: 'cc'", 18718 RawStringFormats, ExpectedRawStringFormats); 18719 18720 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 18721 " Minimum: 0\n" 18722 " Maximum: 0", 18723 SpacesInLineCommentPrefix.Minimum, 0u); 18724 EXPECT_EQ(Style.SpacesInLineCommentPrefix.Maximum, 0u); 18725 Style.SpacesInLineCommentPrefix.Minimum = 1; 18726 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 18727 " Minimum: 2", 18728 SpacesInLineCommentPrefix.Minimum, 0u); 18729 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 18730 " Maximum: -1", 18731 SpacesInLineCommentPrefix.Maximum, -1u); 18732 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 18733 " Minimum: 2", 18734 SpacesInLineCommentPrefix.Minimum, 2u); 18735 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 18736 " Maximum: 1", 18737 SpacesInLineCommentPrefix.Maximum, 1u); 18738 EXPECT_EQ(Style.SpacesInLineCommentPrefix.Minimum, 1u); 18739 18740 Style.SpacesInAngles = FormatStyle::SIAS_Always; 18741 CHECK_PARSE("SpacesInAngles: Never", SpacesInAngles, FormatStyle::SIAS_Never); 18742 CHECK_PARSE("SpacesInAngles: Always", SpacesInAngles, 18743 FormatStyle::SIAS_Always); 18744 CHECK_PARSE("SpacesInAngles: Leave", SpacesInAngles, FormatStyle::SIAS_Leave); 18745 // For backward compatibility: 18746 CHECK_PARSE("SpacesInAngles: false", SpacesInAngles, FormatStyle::SIAS_Never); 18747 CHECK_PARSE("SpacesInAngles: true", SpacesInAngles, FormatStyle::SIAS_Always); 18748 } 18749 18750 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 18751 FormatStyle Style = {}; 18752 Style.Language = FormatStyle::LK_Cpp; 18753 CHECK_PARSE("Language: Cpp\n" 18754 "IndentWidth: 12", 18755 IndentWidth, 12u); 18756 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 18757 "IndentWidth: 34", 18758 &Style), 18759 ParseError::Unsuitable); 18760 FormatStyle BinPackedTCS = {}; 18761 BinPackedTCS.Language = FormatStyle::LK_JavaScript; 18762 EXPECT_EQ(parseConfiguration("BinPackArguments: true\n" 18763 "InsertTrailingCommas: Wrapped", 18764 &BinPackedTCS), 18765 ParseError::BinPackTrailingCommaConflict); 18766 EXPECT_EQ(12u, Style.IndentWidth); 18767 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 18768 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 18769 18770 Style.Language = FormatStyle::LK_JavaScript; 18771 CHECK_PARSE("Language: JavaScript\n" 18772 "IndentWidth: 12", 18773 IndentWidth, 12u); 18774 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 18775 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 18776 "IndentWidth: 34", 18777 &Style), 18778 ParseError::Unsuitable); 18779 EXPECT_EQ(23u, Style.IndentWidth); 18780 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 18781 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 18782 18783 CHECK_PARSE("BasedOnStyle: LLVM\n" 18784 "IndentWidth: 67", 18785 IndentWidth, 67u); 18786 18787 CHECK_PARSE("---\n" 18788 "Language: JavaScript\n" 18789 "IndentWidth: 12\n" 18790 "---\n" 18791 "Language: Cpp\n" 18792 "IndentWidth: 34\n" 18793 "...\n", 18794 IndentWidth, 12u); 18795 18796 Style.Language = FormatStyle::LK_Cpp; 18797 CHECK_PARSE("---\n" 18798 "Language: JavaScript\n" 18799 "IndentWidth: 12\n" 18800 "---\n" 18801 "Language: Cpp\n" 18802 "IndentWidth: 34\n" 18803 "...\n", 18804 IndentWidth, 34u); 18805 CHECK_PARSE("---\n" 18806 "IndentWidth: 78\n" 18807 "---\n" 18808 "Language: JavaScript\n" 18809 "IndentWidth: 56\n" 18810 "...\n", 18811 IndentWidth, 78u); 18812 18813 Style.ColumnLimit = 123; 18814 Style.IndentWidth = 234; 18815 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 18816 Style.TabWidth = 345; 18817 EXPECT_FALSE(parseConfiguration("---\n" 18818 "IndentWidth: 456\n" 18819 "BreakBeforeBraces: Allman\n" 18820 "---\n" 18821 "Language: JavaScript\n" 18822 "IndentWidth: 111\n" 18823 "TabWidth: 111\n" 18824 "---\n" 18825 "Language: Cpp\n" 18826 "BreakBeforeBraces: Stroustrup\n" 18827 "TabWidth: 789\n" 18828 "...\n", 18829 &Style)); 18830 EXPECT_EQ(123u, Style.ColumnLimit); 18831 EXPECT_EQ(456u, Style.IndentWidth); 18832 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 18833 EXPECT_EQ(789u, Style.TabWidth); 18834 18835 EXPECT_EQ(parseConfiguration("---\n" 18836 "Language: JavaScript\n" 18837 "IndentWidth: 56\n" 18838 "---\n" 18839 "IndentWidth: 78\n" 18840 "...\n", 18841 &Style), 18842 ParseError::Error); 18843 EXPECT_EQ(parseConfiguration("---\n" 18844 "Language: JavaScript\n" 18845 "IndentWidth: 56\n" 18846 "---\n" 18847 "Language: JavaScript\n" 18848 "IndentWidth: 78\n" 18849 "...\n", 18850 &Style), 18851 ParseError::Error); 18852 18853 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 18854 } 18855 18856 #undef CHECK_PARSE 18857 18858 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 18859 FormatStyle Style = {}; 18860 Style.Language = FormatStyle::LK_JavaScript; 18861 Style.BreakBeforeTernaryOperators = true; 18862 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 18863 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 18864 18865 Style.BreakBeforeTernaryOperators = true; 18866 EXPECT_EQ(0, parseConfiguration("---\n" 18867 "BasedOnStyle: Google\n" 18868 "---\n" 18869 "Language: JavaScript\n" 18870 "IndentWidth: 76\n" 18871 "...\n", 18872 &Style) 18873 .value()); 18874 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 18875 EXPECT_EQ(76u, Style.IndentWidth); 18876 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 18877 } 18878 18879 TEST_F(FormatTest, ConfigurationRoundTripTest) { 18880 FormatStyle Style = getLLVMStyle(); 18881 std::string YAML = configurationAsText(Style); 18882 FormatStyle ParsedStyle = {}; 18883 ParsedStyle.Language = FormatStyle::LK_Cpp; 18884 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 18885 EXPECT_EQ(Style, ParsedStyle); 18886 } 18887 18888 TEST_F(FormatTest, WorksFor8bitEncodings) { 18889 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 18890 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 18891 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 18892 "\"\xef\xee\xf0\xf3...\"", 18893 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 18894 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 18895 "\xef\xee\xf0\xf3...\"", 18896 getLLVMStyleWithColumns(12))); 18897 } 18898 18899 TEST_F(FormatTest, HandlesUTF8BOM) { 18900 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 18901 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 18902 format("\xef\xbb\xbf#include <iostream>")); 18903 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 18904 format("\xef\xbb\xbf\n#include <iostream>")); 18905 } 18906 18907 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 18908 #if !defined(_MSC_VER) 18909 18910 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 18911 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 18912 getLLVMStyleWithColumns(35)); 18913 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 18914 getLLVMStyleWithColumns(31)); 18915 verifyFormat("// Однажды в студёную зимнюю пору...", 18916 getLLVMStyleWithColumns(36)); 18917 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 18918 verifyFormat("/* Однажды в студёную зимнюю пору... */", 18919 getLLVMStyleWithColumns(39)); 18920 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 18921 getLLVMStyleWithColumns(35)); 18922 } 18923 18924 TEST_F(FormatTest, SplitsUTF8Strings) { 18925 // Non-printable characters' width is currently considered to be the length in 18926 // bytes in UTF8. The characters can be displayed in very different manner 18927 // (zero-width, single width with a substitution glyph, expanded to their code 18928 // (e.g. "<8d>"), so there's no single correct way to handle them. 18929 EXPECT_EQ("\"aaaaÄ\"\n" 18930 "\"\xc2\x8d\";", 18931 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 18932 EXPECT_EQ("\"aaaaaaaÄ\"\n" 18933 "\"\xc2\x8d\";", 18934 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 18935 EXPECT_EQ("\"Однажды, в \"\n" 18936 "\"студёную \"\n" 18937 "\"зимнюю \"\n" 18938 "\"пору,\"", 18939 format("\"Однажды, в студёную зимнюю пору,\"", 18940 getLLVMStyleWithColumns(13))); 18941 EXPECT_EQ( 18942 "\"一 二 三 \"\n" 18943 "\"四 五六 \"\n" 18944 "\"七 八 九 \"\n" 18945 "\"十\"", 18946 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 18947 EXPECT_EQ("\"一\t\"\n" 18948 "\"二 \t\"\n" 18949 "\"三 四 \"\n" 18950 "\"五\t\"\n" 18951 "\"六 \t\"\n" 18952 "\"七 \"\n" 18953 "\"八九十\tqq\"", 18954 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 18955 getLLVMStyleWithColumns(11))); 18956 18957 // UTF8 character in an escape sequence. 18958 EXPECT_EQ("\"aaaaaa\"\n" 18959 "\"\\\xC2\x8D\"", 18960 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 18961 } 18962 18963 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 18964 EXPECT_EQ("const char *sssss =\n" 18965 " \"一二三四五六七八\\\n" 18966 " 九 十\";", 18967 format("const char *sssss = \"一二三四五六七八\\\n" 18968 " 九 十\";", 18969 getLLVMStyleWithColumns(30))); 18970 } 18971 18972 TEST_F(FormatTest, SplitsUTF8LineComments) { 18973 EXPECT_EQ("// aaaaÄ\xc2\x8d", 18974 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 18975 EXPECT_EQ("// Я из лесу\n" 18976 "// вышел; был\n" 18977 "// сильный\n" 18978 "// мороз.", 18979 format("// Я из лесу вышел; был сильный мороз.", 18980 getLLVMStyleWithColumns(13))); 18981 EXPECT_EQ("// 一二三\n" 18982 "// 四五六七\n" 18983 "// 八 九\n" 18984 "// 十", 18985 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 18986 } 18987 18988 TEST_F(FormatTest, SplitsUTF8BlockComments) { 18989 EXPECT_EQ("/* Гляжу,\n" 18990 " * поднимается\n" 18991 " * медленно в\n" 18992 " * гору\n" 18993 " * Лошадка,\n" 18994 " * везущая\n" 18995 " * хворосту\n" 18996 " * воз. */", 18997 format("/* Гляжу, поднимается медленно в гору\n" 18998 " * Лошадка, везущая хворосту воз. */", 18999 getLLVMStyleWithColumns(13))); 19000 EXPECT_EQ( 19001 "/* 一二三\n" 19002 " * 四五六七\n" 19003 " * 八 九\n" 19004 " * 十 */", 19005 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 19006 EXPECT_EQ("/* \n" 19007 " * \n" 19008 " * - */", 19009 format("/* - */", getLLVMStyleWithColumns(12))); 19010 } 19011 19012 #endif // _MSC_VER 19013 19014 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 19015 FormatStyle Style = getLLVMStyle(); 19016 19017 Style.ConstructorInitializerIndentWidth = 4; 19018 verifyFormat( 19019 "SomeClass::Constructor()\n" 19020 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 19021 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 19022 Style); 19023 19024 Style.ConstructorInitializerIndentWidth = 2; 19025 verifyFormat( 19026 "SomeClass::Constructor()\n" 19027 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 19028 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 19029 Style); 19030 19031 Style.ConstructorInitializerIndentWidth = 0; 19032 verifyFormat( 19033 "SomeClass::Constructor()\n" 19034 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 19035 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 19036 Style); 19037 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 19038 verifyFormat( 19039 "SomeLongTemplateVariableName<\n" 19040 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 19041 Style); 19042 verifyFormat("bool smaller = 1 < " 19043 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 19044 " " 19045 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 19046 Style); 19047 19048 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 19049 verifyFormat("SomeClass::Constructor() :\n" 19050 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n" 19051 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}", 19052 Style); 19053 } 19054 19055 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 19056 FormatStyle Style = getLLVMStyle(); 19057 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 19058 Style.ConstructorInitializerIndentWidth = 4; 19059 verifyFormat("SomeClass::Constructor()\n" 19060 " : a(a)\n" 19061 " , b(b)\n" 19062 " , c(c) {}", 19063 Style); 19064 verifyFormat("SomeClass::Constructor()\n" 19065 " : a(a) {}", 19066 Style); 19067 19068 Style.ColumnLimit = 0; 19069 verifyFormat("SomeClass::Constructor()\n" 19070 " : a(a) {}", 19071 Style); 19072 verifyFormat("SomeClass::Constructor() noexcept\n" 19073 " : a(a) {}", 19074 Style); 19075 verifyFormat("SomeClass::Constructor()\n" 19076 " : a(a)\n" 19077 " , b(b)\n" 19078 " , c(c) {}", 19079 Style); 19080 verifyFormat("SomeClass::Constructor()\n" 19081 " : a(a) {\n" 19082 " foo();\n" 19083 " bar();\n" 19084 "}", 19085 Style); 19086 19087 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 19088 verifyFormat("SomeClass::Constructor()\n" 19089 " : a(a)\n" 19090 " , b(b)\n" 19091 " , c(c) {\n}", 19092 Style); 19093 verifyFormat("SomeClass::Constructor()\n" 19094 " : a(a) {\n}", 19095 Style); 19096 19097 Style.ColumnLimit = 80; 19098 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 19099 Style.ConstructorInitializerIndentWidth = 2; 19100 verifyFormat("SomeClass::Constructor()\n" 19101 " : a(a)\n" 19102 " , b(b)\n" 19103 " , c(c) {}", 19104 Style); 19105 19106 Style.ConstructorInitializerIndentWidth = 0; 19107 verifyFormat("SomeClass::Constructor()\n" 19108 ": a(a)\n" 19109 ", b(b)\n" 19110 ", c(c) {}", 19111 Style); 19112 19113 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 19114 Style.ConstructorInitializerIndentWidth = 4; 19115 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 19116 verifyFormat( 19117 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 19118 Style); 19119 verifyFormat( 19120 "SomeClass::Constructor()\n" 19121 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 19122 Style); 19123 Style.ConstructorInitializerIndentWidth = 4; 19124 Style.ColumnLimit = 60; 19125 verifyFormat("SomeClass::Constructor()\n" 19126 " : aaaaaaaa(aaaaaaaa)\n" 19127 " , aaaaaaaa(aaaaaaaa)\n" 19128 " , aaaaaaaa(aaaaaaaa) {}", 19129 Style); 19130 } 19131 19132 TEST_F(FormatTest, Destructors) { 19133 verifyFormat("void F(int &i) { i.~int(); }"); 19134 verifyFormat("void F(int &i) { i->~int(); }"); 19135 } 19136 19137 TEST_F(FormatTest, FormatsWithWebKitStyle) { 19138 FormatStyle Style = getWebKitStyle(); 19139 19140 // Don't indent in outer namespaces. 19141 verifyFormat("namespace outer {\n" 19142 "int i;\n" 19143 "namespace inner {\n" 19144 " int i;\n" 19145 "} // namespace inner\n" 19146 "} // namespace outer\n" 19147 "namespace other_outer {\n" 19148 "int i;\n" 19149 "}", 19150 Style); 19151 19152 // Don't indent case labels. 19153 verifyFormat("switch (variable) {\n" 19154 "case 1:\n" 19155 "case 2:\n" 19156 " doSomething();\n" 19157 " break;\n" 19158 "default:\n" 19159 " ++variable;\n" 19160 "}", 19161 Style); 19162 19163 // Wrap before binary operators. 19164 EXPECT_EQ("void f()\n" 19165 "{\n" 19166 " if (aaaaaaaaaaaaaaaa\n" 19167 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 19168 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 19169 " return;\n" 19170 "}", 19171 format("void f() {\n" 19172 "if (aaaaaaaaaaaaaaaa\n" 19173 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 19174 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 19175 "return;\n" 19176 "}", 19177 Style)); 19178 19179 // Allow functions on a single line. 19180 verifyFormat("void f() { return; }", Style); 19181 19182 // Allow empty blocks on a single line and insert a space in empty blocks. 19183 EXPECT_EQ("void f() { }", format("void f() {}", Style)); 19184 EXPECT_EQ("while (true) { }", format("while (true) {}", Style)); 19185 // However, don't merge non-empty short loops. 19186 EXPECT_EQ("while (true) {\n" 19187 " continue;\n" 19188 "}", 19189 format("while (true) { continue; }", Style)); 19190 19191 // Constructor initializers are formatted one per line with the "," on the 19192 // new line. 19193 verifyFormat("Constructor()\n" 19194 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 19195 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 19196 " aaaaaaaaaaaaaa)\n" 19197 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 19198 "{\n" 19199 "}", 19200 Style); 19201 verifyFormat("SomeClass::Constructor()\n" 19202 " : a(a)\n" 19203 "{\n" 19204 "}", 19205 Style); 19206 EXPECT_EQ("SomeClass::Constructor()\n" 19207 " : a(a)\n" 19208 "{\n" 19209 "}", 19210 format("SomeClass::Constructor():a(a){}", Style)); 19211 verifyFormat("SomeClass::Constructor()\n" 19212 " : a(a)\n" 19213 " , b(b)\n" 19214 " , c(c)\n" 19215 "{\n" 19216 "}", 19217 Style); 19218 verifyFormat("SomeClass::Constructor()\n" 19219 " : a(a)\n" 19220 "{\n" 19221 " foo();\n" 19222 " bar();\n" 19223 "}", 19224 Style); 19225 19226 // Access specifiers should be aligned left. 19227 verifyFormat("class C {\n" 19228 "public:\n" 19229 " int i;\n" 19230 "};", 19231 Style); 19232 19233 // Do not align comments. 19234 verifyFormat("int a; // Do not\n" 19235 "double b; // align comments.", 19236 Style); 19237 19238 // Do not align operands. 19239 EXPECT_EQ("ASSERT(aaaa\n" 19240 " || bbbb);", 19241 format("ASSERT ( aaaa\n||bbbb);", Style)); 19242 19243 // Accept input's line breaks. 19244 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 19245 " || bbbbbbbbbbbbbbb) {\n" 19246 " i++;\n" 19247 "}", 19248 format("if (aaaaaaaaaaaaaaa\n" 19249 "|| bbbbbbbbbbbbbbb) { i++; }", 19250 Style)); 19251 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 19252 " i++;\n" 19253 "}", 19254 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 19255 19256 // Don't automatically break all macro definitions (llvm.org/PR17842). 19257 verifyFormat("#define aNumber 10", Style); 19258 // However, generally keep the line breaks that the user authored. 19259 EXPECT_EQ("#define aNumber \\\n" 19260 " 10", 19261 format("#define aNumber \\\n" 19262 " 10", 19263 Style)); 19264 19265 // Keep empty and one-element array literals on a single line. 19266 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 19267 " copyItems:YES];", 19268 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 19269 "copyItems:YES];", 19270 Style)); 19271 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 19272 " copyItems:YES];", 19273 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 19274 " copyItems:YES];", 19275 Style)); 19276 // FIXME: This does not seem right, there should be more indentation before 19277 // the array literal's entries. Nested blocks have the same problem. 19278 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 19279 " @\"a\",\n" 19280 " @\"a\"\n" 19281 "]\n" 19282 " copyItems:YES];", 19283 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 19284 " @\"a\",\n" 19285 " @\"a\"\n" 19286 " ]\n" 19287 " copyItems:YES];", 19288 Style)); 19289 EXPECT_EQ( 19290 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 19291 " copyItems:YES];", 19292 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 19293 " copyItems:YES];", 19294 Style)); 19295 19296 verifyFormat("[self.a b:c c:d];", Style); 19297 EXPECT_EQ("[self.a b:c\n" 19298 " c:d];", 19299 format("[self.a b:c\n" 19300 "c:d];", 19301 Style)); 19302 } 19303 19304 TEST_F(FormatTest, FormatsLambdas) { 19305 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 19306 verifyFormat( 19307 "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();\n"); 19308 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 19309 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 19310 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 19311 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 19312 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 19313 verifyFormat("auto c = [a = [b = 42] {}] {};\n"); 19314 verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n"); 19315 verifyFormat("int x = f(*+[] {});"); 19316 verifyFormat("void f() {\n" 19317 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 19318 "}\n"); 19319 verifyFormat("void f() {\n" 19320 " other(x.begin(), //\n" 19321 " x.end(), //\n" 19322 " [&](int, int) { return 1; });\n" 19323 "}\n"); 19324 verifyFormat("void f() {\n" 19325 " other.other.other.other.other(\n" 19326 " x.begin(), x.end(),\n" 19327 " [something, rather](int, int, int, int, int, int, int) { " 19328 "return 1; });\n" 19329 "}\n"); 19330 verifyFormat( 19331 "void f() {\n" 19332 " other.other.other.other.other(\n" 19333 " x.begin(), x.end(),\n" 19334 " [something, rather](int, int, int, int, int, int, int) {\n" 19335 " //\n" 19336 " });\n" 19337 "}\n"); 19338 verifyFormat("SomeFunction([]() { // A cool function...\n" 19339 " return 43;\n" 19340 "});"); 19341 EXPECT_EQ("SomeFunction([]() {\n" 19342 "#define A a\n" 19343 " return 43;\n" 19344 "});", 19345 format("SomeFunction([](){\n" 19346 "#define A a\n" 19347 "return 43;\n" 19348 "});")); 19349 verifyFormat("void f() {\n" 19350 " SomeFunction([](decltype(x), A *a) {});\n" 19351 " SomeFunction([](typeof(x), A *a) {});\n" 19352 " SomeFunction([](_Atomic(x), A *a) {});\n" 19353 " SomeFunction([](__underlying_type(x), A *a) {});\n" 19354 "}"); 19355 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 19356 " [](const aaaaaaaaaa &a) { return a; });"); 19357 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 19358 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 19359 "});"); 19360 verifyFormat("Constructor()\n" 19361 " : Field([] { // comment\n" 19362 " int i;\n" 19363 " }) {}"); 19364 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 19365 " return some_parameter.size();\n" 19366 "};"); 19367 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 19368 " [](const string &s) { return s; };"); 19369 verifyFormat("int i = aaaaaa ? 1 //\n" 19370 " : [] {\n" 19371 " return 2; //\n" 19372 " }();"); 19373 verifyFormat("llvm::errs() << \"number of twos is \"\n" 19374 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 19375 " return x == 2; // force break\n" 19376 " });"); 19377 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 19378 " [=](int iiiiiiiiiiii) {\n" 19379 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n" 19380 " aaaaaaaaaaaaaaaaaaaaaaa;\n" 19381 " });", 19382 getLLVMStyleWithColumns(60)); 19383 19384 verifyFormat("SomeFunction({[&] {\n" 19385 " // comment\n" 19386 " },\n" 19387 " [&] {\n" 19388 " // comment\n" 19389 " }});"); 19390 verifyFormat("SomeFunction({[&] {\n" 19391 " // comment\n" 19392 "}});"); 19393 verifyFormat( 19394 "virtual aaaaaaaaaaaaaaaa(\n" 19395 " std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n" 19396 " aaaaa aaaaaaaaa);"); 19397 19398 // Lambdas with return types. 19399 verifyFormat("int c = []() -> int { return 2; }();\n"); 19400 verifyFormat("int c = []() -> int * { return 2; }();\n"); 19401 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 19402 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 19403 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 19404 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 19405 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 19406 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 19407 verifyFormat("[a, a]() -> a<1> {};"); 19408 verifyFormat("[]() -> foo<5 + 2> { return {}; };"); 19409 verifyFormat("[]() -> foo<5 - 2> { return {}; };"); 19410 verifyFormat("[]() -> foo<5 / 2> { return {}; };"); 19411 verifyFormat("[]() -> foo<5 * 2> { return {}; };"); 19412 verifyFormat("[]() -> foo<5 % 2> { return {}; };"); 19413 verifyFormat("[]() -> foo<5 << 2> { return {}; };"); 19414 verifyFormat("[]() -> foo<!5> { return {}; };"); 19415 verifyFormat("[]() -> foo<~5> { return {}; };"); 19416 verifyFormat("[]() -> foo<5 | 2> { return {}; };"); 19417 verifyFormat("[]() -> foo<5 || 2> { return {}; };"); 19418 verifyFormat("[]() -> foo<5 & 2> { return {}; };"); 19419 verifyFormat("[]() -> foo<5 && 2> { return {}; };"); 19420 verifyFormat("[]() -> foo<5 == 2> { return {}; };"); 19421 verifyFormat("[]() -> foo<5 != 2> { return {}; };"); 19422 verifyFormat("[]() -> foo<5 >= 2> { return {}; };"); 19423 verifyFormat("[]() -> foo<5 <= 2> { return {}; };"); 19424 verifyFormat("[]() -> foo<5 < 2> { return {}; };"); 19425 verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };"); 19426 verifyFormat("namespace bar {\n" 19427 "// broken:\n" 19428 "auto foo{[]() -> foo<5 + 2> { return {}; }};\n" 19429 "} // namespace bar"); 19430 verifyFormat("namespace bar {\n" 19431 "// broken:\n" 19432 "auto foo{[]() -> foo<5 - 2> { return {}; }};\n" 19433 "} // namespace bar"); 19434 verifyFormat("namespace bar {\n" 19435 "// broken:\n" 19436 "auto foo{[]() -> foo<5 / 2> { return {}; }};\n" 19437 "} // namespace bar"); 19438 verifyFormat("namespace bar {\n" 19439 "// broken:\n" 19440 "auto foo{[]() -> foo<5 * 2> { return {}; }};\n" 19441 "} // namespace bar"); 19442 verifyFormat("namespace bar {\n" 19443 "// broken:\n" 19444 "auto foo{[]() -> foo<5 % 2> { return {}; }};\n" 19445 "} // namespace bar"); 19446 verifyFormat("namespace bar {\n" 19447 "// broken:\n" 19448 "auto foo{[]() -> foo<5 << 2> { return {}; }};\n" 19449 "} // namespace bar"); 19450 verifyFormat("namespace bar {\n" 19451 "// broken:\n" 19452 "auto foo{[]() -> foo<!5> { return {}; }};\n" 19453 "} // namespace bar"); 19454 verifyFormat("namespace bar {\n" 19455 "// broken:\n" 19456 "auto foo{[]() -> foo<~5> { return {}; }};\n" 19457 "} // namespace bar"); 19458 verifyFormat("namespace bar {\n" 19459 "// broken:\n" 19460 "auto foo{[]() -> foo<5 | 2> { return {}; }};\n" 19461 "} // namespace bar"); 19462 verifyFormat("namespace bar {\n" 19463 "// broken:\n" 19464 "auto foo{[]() -> foo<5 || 2> { return {}; }};\n" 19465 "} // namespace bar"); 19466 verifyFormat("namespace bar {\n" 19467 "// broken:\n" 19468 "auto foo{[]() -> foo<5 & 2> { return {}; }};\n" 19469 "} // namespace bar"); 19470 verifyFormat("namespace bar {\n" 19471 "// broken:\n" 19472 "auto foo{[]() -> foo<5 && 2> { return {}; }};\n" 19473 "} // namespace bar"); 19474 verifyFormat("namespace bar {\n" 19475 "// broken:\n" 19476 "auto foo{[]() -> foo<5 == 2> { return {}; }};\n" 19477 "} // namespace bar"); 19478 verifyFormat("namespace bar {\n" 19479 "// broken:\n" 19480 "auto foo{[]() -> foo<5 != 2> { return {}; }};\n" 19481 "} // namespace bar"); 19482 verifyFormat("namespace bar {\n" 19483 "// broken:\n" 19484 "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n" 19485 "} // namespace bar"); 19486 verifyFormat("namespace bar {\n" 19487 "// broken:\n" 19488 "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n" 19489 "} // namespace bar"); 19490 verifyFormat("namespace bar {\n" 19491 "// broken:\n" 19492 "auto foo{[]() -> foo<5 < 2> { return {}; }};\n" 19493 "} // namespace bar"); 19494 verifyFormat("namespace bar {\n" 19495 "// broken:\n" 19496 "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n" 19497 "} // namespace bar"); 19498 verifyFormat("[]() -> a<1> {};"); 19499 verifyFormat("[]() -> a<1> { ; };"); 19500 verifyFormat("[]() -> a<1> { ; }();"); 19501 verifyFormat("[a, a]() -> a<true> {};"); 19502 verifyFormat("[]() -> a<true> {};"); 19503 verifyFormat("[]() -> a<true> { ; };"); 19504 verifyFormat("[]() -> a<true> { ; }();"); 19505 verifyFormat("[a, a]() -> a<false> {};"); 19506 verifyFormat("[]() -> a<false> {};"); 19507 verifyFormat("[]() -> a<false> { ; };"); 19508 verifyFormat("[]() -> a<false> { ; }();"); 19509 verifyFormat("auto foo{[]() -> foo<false> { ; }};"); 19510 verifyFormat("namespace bar {\n" 19511 "auto foo{[]() -> foo<false> { ; }};\n" 19512 "} // namespace bar"); 19513 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 19514 " int j) -> int {\n" 19515 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 19516 "};"); 19517 verifyFormat( 19518 "aaaaaaaaaaaaaaaaaaaaaa(\n" 19519 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 19520 " return aaaaaaaaaaaaaaaaa;\n" 19521 " });", 19522 getLLVMStyleWithColumns(70)); 19523 verifyFormat("[]() //\n" 19524 " -> int {\n" 19525 " return 1; //\n" 19526 "};"); 19527 verifyFormat("[]() -> Void<T...> {};"); 19528 verifyFormat("[a, b]() -> Tuple<T...> { return {}; };"); 19529 19530 // Lambdas with explicit template argument lists. 19531 verifyFormat( 19532 "auto L = []<template <typename> class T, class U>(T<U> &&a) {};\n"); 19533 19534 // Multiple lambdas in the same parentheses change indentation rules. These 19535 // lambdas are forced to start on new lines. 19536 verifyFormat("SomeFunction(\n" 19537 " []() {\n" 19538 " //\n" 19539 " },\n" 19540 " []() {\n" 19541 " //\n" 19542 " });"); 19543 19544 // A lambda passed as arg0 is always pushed to the next line. 19545 verifyFormat("SomeFunction(\n" 19546 " [this] {\n" 19547 " //\n" 19548 " },\n" 19549 " 1);\n"); 19550 19551 // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like 19552 // the arg0 case above. 19553 auto Style = getGoogleStyle(); 19554 Style.BinPackArguments = false; 19555 verifyFormat("SomeFunction(\n" 19556 " a,\n" 19557 " [this] {\n" 19558 " //\n" 19559 " },\n" 19560 " b);\n", 19561 Style); 19562 verifyFormat("SomeFunction(\n" 19563 " a,\n" 19564 " [this] {\n" 19565 " //\n" 19566 " },\n" 19567 " b);\n"); 19568 19569 // A lambda with a very long line forces arg0 to be pushed out irrespective of 19570 // the BinPackArguments value (as long as the code is wide enough). 19571 verifyFormat( 19572 "something->SomeFunction(\n" 19573 " a,\n" 19574 " [this] {\n" 19575 " " 19576 "D0000000000000000000000000000000000000000000000000000000000001();\n" 19577 " },\n" 19578 " b);\n"); 19579 19580 // A multi-line lambda is pulled up as long as the introducer fits on the 19581 // previous line and there are no further args. 19582 verifyFormat("function(1, [this, that] {\n" 19583 " //\n" 19584 "});\n"); 19585 verifyFormat("function([this, that] {\n" 19586 " //\n" 19587 "});\n"); 19588 // FIXME: this format is not ideal and we should consider forcing the first 19589 // arg onto its own line. 19590 verifyFormat("function(a, b, c, //\n" 19591 " d, [this, that] {\n" 19592 " //\n" 19593 " });\n"); 19594 19595 // Multiple lambdas are treated correctly even when there is a short arg0. 19596 verifyFormat("SomeFunction(\n" 19597 " 1,\n" 19598 " [this] {\n" 19599 " //\n" 19600 " },\n" 19601 " [this] {\n" 19602 " //\n" 19603 " },\n" 19604 " 1);\n"); 19605 19606 // More complex introducers. 19607 verifyFormat("return [i, args...] {};"); 19608 19609 // Not lambdas. 19610 verifyFormat("constexpr char hello[]{\"hello\"};"); 19611 verifyFormat("double &operator[](int i) { return 0; }\n" 19612 "int i;"); 19613 verifyFormat("std::unique_ptr<int[]> foo() {}"); 19614 verifyFormat("int i = a[a][a]->f();"); 19615 verifyFormat("int i = (*b)[a]->f();"); 19616 19617 // Other corner cases. 19618 verifyFormat("void f() {\n" 19619 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 19620 " );\n" 19621 "}"); 19622 19623 // Lambdas created through weird macros. 19624 verifyFormat("void f() {\n" 19625 " MACRO((const AA &a) { return 1; });\n" 19626 " MACRO((AA &a) { return 1; });\n" 19627 "}"); 19628 19629 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 19630 " doo_dah();\n" 19631 " doo_dah();\n" 19632 " })) {\n" 19633 "}"); 19634 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n" 19635 " doo_dah();\n" 19636 " doo_dah();\n" 19637 " })) {\n" 19638 "}"); 19639 verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n" 19640 " doo_dah();\n" 19641 " doo_dah();\n" 19642 " })) {\n" 19643 "}"); 19644 verifyFormat("auto lambda = []() {\n" 19645 " int a = 2\n" 19646 "#if A\n" 19647 " + 2\n" 19648 "#endif\n" 19649 " ;\n" 19650 "};"); 19651 19652 // Lambdas with complex multiline introducers. 19653 verifyFormat( 19654 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 19655 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n" 19656 " -> ::std::unordered_set<\n" 19657 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 19658 " //\n" 19659 " });"); 19660 19661 FormatStyle DoNotMerge = getLLVMStyle(); 19662 DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 19663 verifyFormat("auto c = []() {\n" 19664 " return b;\n" 19665 "};", 19666 "auto c = []() { return b; };", DoNotMerge); 19667 verifyFormat("auto c = []() {\n" 19668 "};", 19669 " auto c = []() {};", DoNotMerge); 19670 19671 FormatStyle MergeEmptyOnly = getLLVMStyle(); 19672 MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty; 19673 verifyFormat("auto c = []() {\n" 19674 " return b;\n" 19675 "};", 19676 "auto c = []() {\n" 19677 " return b;\n" 19678 " };", 19679 MergeEmptyOnly); 19680 verifyFormat("auto c = []() {};", 19681 "auto c = []() {\n" 19682 "};", 19683 MergeEmptyOnly); 19684 19685 FormatStyle MergeInline = getLLVMStyle(); 19686 MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline; 19687 verifyFormat("auto c = []() {\n" 19688 " return b;\n" 19689 "};", 19690 "auto c = []() { return b; };", MergeInline); 19691 verifyFormat("function([]() { return b; })", "function([]() { return b; })", 19692 MergeInline); 19693 verifyFormat("function([]() { return b; }, a)", 19694 "function([]() { return b; }, a)", MergeInline); 19695 verifyFormat("function(a, []() { return b; })", 19696 "function(a, []() { return b; })", MergeInline); 19697 19698 // Check option "BraceWrapping.BeforeLambdaBody" and different state of 19699 // AllowShortLambdasOnASingleLine 19700 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle(); 19701 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 19702 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 19703 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 19704 FormatStyle::ShortLambdaStyle::SLS_None; 19705 verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n" 19706 " []()\n" 19707 " {\n" 19708 " return 17;\n" 19709 " });", 19710 LLVMWithBeforeLambdaBody); 19711 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n" 19712 " []()\n" 19713 " {\n" 19714 " });", 19715 LLVMWithBeforeLambdaBody); 19716 verifyFormat("auto fct_SLS_None = []()\n" 19717 "{\n" 19718 " return 17;\n" 19719 "};", 19720 LLVMWithBeforeLambdaBody); 19721 verifyFormat("TwoNestedLambdas_SLS_None(\n" 19722 " []()\n" 19723 " {\n" 19724 " return Call(\n" 19725 " []()\n" 19726 " {\n" 19727 " return 17;\n" 19728 " });\n" 19729 " });", 19730 LLVMWithBeforeLambdaBody); 19731 verifyFormat("void Fct() {\n" 19732 " return {[]()\n" 19733 " {\n" 19734 " return 17;\n" 19735 " }};\n" 19736 "}", 19737 LLVMWithBeforeLambdaBody); 19738 19739 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 19740 FormatStyle::ShortLambdaStyle::SLS_Empty; 19741 verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n" 19742 " []()\n" 19743 " {\n" 19744 " return 17;\n" 19745 " });", 19746 LLVMWithBeforeLambdaBody); 19747 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});", 19748 LLVMWithBeforeLambdaBody); 19749 verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL" 19750 "ongFunctionName_SLS_Empty(\n" 19751 " []() {});", 19752 LLVMWithBeforeLambdaBody); 19753 verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n" 19754 " []()\n" 19755 " {\n" 19756 " return 17;\n" 19757 " });", 19758 LLVMWithBeforeLambdaBody); 19759 verifyFormat("auto fct_SLS_Empty = []()\n" 19760 "{\n" 19761 " return 17;\n" 19762 "};", 19763 LLVMWithBeforeLambdaBody); 19764 verifyFormat("TwoNestedLambdas_SLS_Empty(\n" 19765 " []()\n" 19766 " {\n" 19767 " return Call([]() {});\n" 19768 " });", 19769 LLVMWithBeforeLambdaBody); 19770 verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n" 19771 " []()\n" 19772 " {\n" 19773 " return Call([]() {});\n" 19774 " });", 19775 LLVMWithBeforeLambdaBody); 19776 verifyFormat( 19777 "FctWithLongLineInLambda_SLS_Empty(\n" 19778 " []()\n" 19779 " {\n" 19780 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 19781 " AndShouldNotBeConsiderAsInline,\n" 19782 " LambdaBodyMustBeBreak);\n" 19783 " });", 19784 LLVMWithBeforeLambdaBody); 19785 19786 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 19787 FormatStyle::ShortLambdaStyle::SLS_Inline; 19788 verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });", 19789 LLVMWithBeforeLambdaBody); 19790 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});", 19791 LLVMWithBeforeLambdaBody); 19792 verifyFormat("auto fct_SLS_Inline = []()\n" 19793 "{\n" 19794 " return 17;\n" 19795 "};", 19796 LLVMWithBeforeLambdaBody); 19797 verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return " 19798 "17; }); });", 19799 LLVMWithBeforeLambdaBody); 19800 verifyFormat( 19801 "FctWithLongLineInLambda_SLS_Inline(\n" 19802 " []()\n" 19803 " {\n" 19804 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 19805 " AndShouldNotBeConsiderAsInline,\n" 19806 " LambdaBodyMustBeBreak);\n" 19807 " });", 19808 LLVMWithBeforeLambdaBody); 19809 verifyFormat("FctWithMultipleParams_SLS_Inline(" 19810 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n" 19811 " []() { return 17; });", 19812 LLVMWithBeforeLambdaBody); 19813 verifyFormat( 19814 "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });", 19815 LLVMWithBeforeLambdaBody); 19816 19817 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 19818 FormatStyle::ShortLambdaStyle::SLS_All; 19819 verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });", 19820 LLVMWithBeforeLambdaBody); 19821 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});", 19822 LLVMWithBeforeLambdaBody); 19823 verifyFormat("auto fct_SLS_All = []() { return 17; };", 19824 LLVMWithBeforeLambdaBody); 19825 verifyFormat("FctWithOneParam_SLS_All(\n" 19826 " []()\n" 19827 " {\n" 19828 " // A cool function...\n" 19829 " return 43;\n" 19830 " });", 19831 LLVMWithBeforeLambdaBody); 19832 verifyFormat("FctWithMultipleParams_SLS_All(" 19833 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n" 19834 " []() { return 17; });", 19835 LLVMWithBeforeLambdaBody); 19836 verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });", 19837 LLVMWithBeforeLambdaBody); 19838 verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });", 19839 LLVMWithBeforeLambdaBody); 19840 verifyFormat( 19841 "FctWithLongLineInLambda_SLS_All(\n" 19842 " []()\n" 19843 " {\n" 19844 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 19845 " AndShouldNotBeConsiderAsInline,\n" 19846 " LambdaBodyMustBeBreak);\n" 19847 " });", 19848 LLVMWithBeforeLambdaBody); 19849 verifyFormat( 19850 "auto fct_SLS_All = []()\n" 19851 "{\n" 19852 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 19853 " AndShouldNotBeConsiderAsInline,\n" 19854 " LambdaBodyMustBeBreak);\n" 19855 "};", 19856 LLVMWithBeforeLambdaBody); 19857 LLVMWithBeforeLambdaBody.BinPackParameters = false; 19858 verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);", 19859 LLVMWithBeforeLambdaBody); 19860 verifyFormat( 19861 "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n" 19862 " FirstParam,\n" 19863 " SecondParam,\n" 19864 " ThirdParam,\n" 19865 " FourthParam);", 19866 LLVMWithBeforeLambdaBody); 19867 verifyFormat("FctWithLongLineInLambda_SLS_All(\n" 19868 " []() { return " 19869 "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n" 19870 " FirstParam,\n" 19871 " SecondParam,\n" 19872 " ThirdParam,\n" 19873 " FourthParam);", 19874 LLVMWithBeforeLambdaBody); 19875 verifyFormat( 19876 "FctWithLongLineInLambda_SLS_All(FirstParam,\n" 19877 " SecondParam,\n" 19878 " ThirdParam,\n" 19879 " FourthParam,\n" 19880 " []() { return SomeValueNotSoLong; });", 19881 LLVMWithBeforeLambdaBody); 19882 verifyFormat("FctWithLongLineInLambda_SLS_All(\n" 19883 " []()\n" 19884 " {\n" 19885 " return " 19886 "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB" 19887 "eConsiderAsInline;\n" 19888 " });", 19889 LLVMWithBeforeLambdaBody); 19890 verifyFormat( 19891 "FctWithLongLineInLambda_SLS_All(\n" 19892 " []()\n" 19893 " {\n" 19894 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 19895 " AndShouldNotBeConsiderAsInline,\n" 19896 " LambdaBodyMustBeBreak);\n" 19897 " });", 19898 LLVMWithBeforeLambdaBody); 19899 verifyFormat("FctWithTwoParams_SLS_All(\n" 19900 " []()\n" 19901 " {\n" 19902 " // A cool function...\n" 19903 " return 43;\n" 19904 " },\n" 19905 " 87);", 19906 LLVMWithBeforeLambdaBody); 19907 verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);", 19908 LLVMWithBeforeLambdaBody); 19909 verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });", 19910 LLVMWithBeforeLambdaBody); 19911 verifyFormat( 19912 "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });", 19913 LLVMWithBeforeLambdaBody); 19914 verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; " 19915 "}); }, x);", 19916 LLVMWithBeforeLambdaBody); 19917 verifyFormat("TwoNestedLambdas_SLS_All(\n" 19918 " []()\n" 19919 " {\n" 19920 " // A cool function...\n" 19921 " return Call([]() { return 17; });\n" 19922 " });", 19923 LLVMWithBeforeLambdaBody); 19924 verifyFormat("TwoNestedLambdas_SLS_All(\n" 19925 " []()\n" 19926 " {\n" 19927 " return Call(\n" 19928 " []()\n" 19929 " {\n" 19930 " // A cool function...\n" 19931 " return 17;\n" 19932 " });\n" 19933 " });", 19934 LLVMWithBeforeLambdaBody); 19935 19936 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 19937 FormatStyle::ShortLambdaStyle::SLS_None; 19938 19939 verifyFormat("auto select = [this]() -> const Library::Object *\n" 19940 "{\n" 19941 " return MyAssignment::SelectFromList(this);\n" 19942 "};\n", 19943 LLVMWithBeforeLambdaBody); 19944 19945 verifyFormat("auto select = [this]() -> const Library::Object &\n" 19946 "{\n" 19947 " return MyAssignment::SelectFromList(this);\n" 19948 "};\n", 19949 LLVMWithBeforeLambdaBody); 19950 19951 verifyFormat("auto select = [this]() -> std::unique_ptr<Object>\n" 19952 "{\n" 19953 " return MyAssignment::SelectFromList(this);\n" 19954 "};\n", 19955 LLVMWithBeforeLambdaBody); 19956 19957 verifyFormat("namespace test {\n" 19958 "class Test {\n" 19959 "public:\n" 19960 " Test() = default;\n" 19961 "};\n" 19962 "} // namespace test", 19963 LLVMWithBeforeLambdaBody); 19964 19965 // Lambdas with different indentation styles. 19966 Style = getLLVMStyleWithColumns(100); 19967 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 19968 " return promise.then(\n" 19969 " [this, &someVariable, someObject = " 19970 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 19971 " return someObject.startAsyncAction().then(\n" 19972 " [this, &someVariable](AsyncActionResult result) " 19973 "mutable { result.processMore(); });\n" 19974 " });\n" 19975 "}\n", 19976 format("SomeResult doSomething(SomeObject promise) {\n" 19977 " return promise.then([this, &someVariable, someObject = " 19978 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 19979 " return someObject.startAsyncAction().then([this, " 19980 "&someVariable](AsyncActionResult result) mutable {\n" 19981 " result.processMore();\n" 19982 " });\n" 19983 " });\n" 19984 "}\n", 19985 Style)); 19986 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope; 19987 verifyFormat("test() {\n" 19988 " ([]() -> {\n" 19989 " int b = 32;\n" 19990 " return 3;\n" 19991 " }).foo();\n" 19992 "}", 19993 Style); 19994 verifyFormat("test() {\n" 19995 " []() -> {\n" 19996 " int b = 32;\n" 19997 " return 3;\n" 19998 " }\n" 19999 "}", 20000 Style); 20001 verifyFormat("std::sort(v.begin(), v.end(),\n" 20002 " [](const auto &someLongArgumentName, const auto " 20003 "&someOtherLongArgumentName) {\n" 20004 " return someLongArgumentName.someMemberVariable < " 20005 "someOtherLongArgumentName.someMemberVariable;\n" 20006 "});", 20007 Style); 20008 verifyFormat("test() {\n" 20009 " (\n" 20010 " []() -> {\n" 20011 " int b = 32;\n" 20012 " return 3;\n" 20013 " },\n" 20014 " foo, bar)\n" 20015 " .foo();\n" 20016 "}", 20017 Style); 20018 verifyFormat("test() {\n" 20019 " ([]() -> {\n" 20020 " int b = 32;\n" 20021 " return 3;\n" 20022 " })\n" 20023 " .foo()\n" 20024 " .bar();\n" 20025 "}", 20026 Style); 20027 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 20028 " return promise.then(\n" 20029 " [this, &someVariable, someObject = " 20030 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 20031 " return someObject.startAsyncAction().then(\n" 20032 " [this, &someVariable](AsyncActionResult result) mutable { " 20033 "result.processMore(); });\n" 20034 " });\n" 20035 "}\n", 20036 format("SomeResult doSomething(SomeObject promise) {\n" 20037 " return promise.then([this, &someVariable, someObject = " 20038 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 20039 " return someObject.startAsyncAction().then([this, " 20040 "&someVariable](AsyncActionResult result) mutable {\n" 20041 " result.processMore();\n" 20042 " });\n" 20043 " });\n" 20044 "}\n", 20045 Style)); 20046 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 20047 " return promise.then([this, &someVariable] {\n" 20048 " return someObject.startAsyncAction().then(\n" 20049 " [this, &someVariable](AsyncActionResult result) mutable { " 20050 "result.processMore(); });\n" 20051 " });\n" 20052 "}\n", 20053 format("SomeResult doSomething(SomeObject promise) {\n" 20054 " return promise.then([this, &someVariable] {\n" 20055 " return someObject.startAsyncAction().then([this, " 20056 "&someVariable](AsyncActionResult result) mutable {\n" 20057 " result.processMore();\n" 20058 " });\n" 20059 " });\n" 20060 "}\n", 20061 Style)); 20062 Style = getGoogleStyle(); 20063 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope; 20064 EXPECT_EQ("#define A \\\n" 20065 " [] { \\\n" 20066 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 20067 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 20068 " }", 20069 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 20070 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 20071 Style)); 20072 // TODO: The current formatting has a minor issue that's not worth fixing 20073 // right now whereby the closing brace is indented relative to the signature 20074 // instead of being aligned. This only happens with macros. 20075 } 20076 20077 TEST_F(FormatTest, LambdaWithLineComments) { 20078 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle(); 20079 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 20080 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 20081 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20082 FormatStyle::ShortLambdaStyle::SLS_All; 20083 20084 verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody); 20085 verifyFormat("auto k = []() // comment\n" 20086 "{ return; }", 20087 LLVMWithBeforeLambdaBody); 20088 verifyFormat("auto k = []() /* comment */ { return; }", 20089 LLVMWithBeforeLambdaBody); 20090 verifyFormat("auto k = []() /* comment */ /* comment */ { return; }", 20091 LLVMWithBeforeLambdaBody); 20092 verifyFormat("auto k = []() // X\n" 20093 "{ return; }", 20094 LLVMWithBeforeLambdaBody); 20095 verifyFormat( 20096 "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n" 20097 "{ return; }", 20098 LLVMWithBeforeLambdaBody); 20099 } 20100 20101 TEST_F(FormatTest, EmptyLinesInLambdas) { 20102 verifyFormat("auto lambda = []() {\n" 20103 " x(); //\n" 20104 "};", 20105 "auto lambda = []() {\n" 20106 "\n" 20107 " x(); //\n" 20108 "\n" 20109 "};"); 20110 } 20111 20112 TEST_F(FormatTest, FormatsBlocks) { 20113 FormatStyle ShortBlocks = getLLVMStyle(); 20114 ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 20115 verifyFormat("int (^Block)(int, int);", ShortBlocks); 20116 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 20117 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 20118 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 20119 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 20120 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 20121 20122 verifyFormat("foo(^{ bar(); });", ShortBlocks); 20123 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 20124 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 20125 20126 verifyFormat("[operation setCompletionBlock:^{\n" 20127 " [self onOperationDone];\n" 20128 "}];"); 20129 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 20130 " [self onOperationDone];\n" 20131 "}]};"); 20132 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 20133 " f();\n" 20134 "}];"); 20135 verifyFormat("int a = [operation block:^int(int *i) {\n" 20136 " return 1;\n" 20137 "}];"); 20138 verifyFormat("[myObject doSomethingWith:arg1\n" 20139 " aaa:^int(int *a) {\n" 20140 " return 1;\n" 20141 " }\n" 20142 " bbb:f(a * bbbbbbbb)];"); 20143 20144 verifyFormat("[operation setCompletionBlock:^{\n" 20145 " [self.delegate newDataAvailable];\n" 20146 "}];", 20147 getLLVMStyleWithColumns(60)); 20148 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 20149 " NSString *path = [self sessionFilePath];\n" 20150 " if (path) {\n" 20151 " // ...\n" 20152 " }\n" 20153 "});"); 20154 verifyFormat("[[SessionService sharedService]\n" 20155 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 20156 " if (window) {\n" 20157 " [self windowDidLoad:window];\n" 20158 " } else {\n" 20159 " [self errorLoadingWindow];\n" 20160 " }\n" 20161 " }];"); 20162 verifyFormat("void (^largeBlock)(void) = ^{\n" 20163 " // ...\n" 20164 "};\n", 20165 getLLVMStyleWithColumns(40)); 20166 verifyFormat("[[SessionService sharedService]\n" 20167 " loadWindowWithCompletionBlock: //\n" 20168 " ^(SessionWindow *window) {\n" 20169 " if (window) {\n" 20170 " [self windowDidLoad:window];\n" 20171 " } else {\n" 20172 " [self errorLoadingWindow];\n" 20173 " }\n" 20174 " }];", 20175 getLLVMStyleWithColumns(60)); 20176 verifyFormat("[myObject doSomethingWith:arg1\n" 20177 " firstBlock:^(Foo *a) {\n" 20178 " // ...\n" 20179 " int i;\n" 20180 " }\n" 20181 " secondBlock:^(Bar *b) {\n" 20182 " // ...\n" 20183 " int i;\n" 20184 " }\n" 20185 " thirdBlock:^Foo(Bar *b) {\n" 20186 " // ...\n" 20187 " int i;\n" 20188 " }];"); 20189 verifyFormat("[myObject doSomethingWith:arg1\n" 20190 " firstBlock:-1\n" 20191 " secondBlock:^(Bar *b) {\n" 20192 " // ...\n" 20193 " int i;\n" 20194 " }];"); 20195 20196 verifyFormat("f(^{\n" 20197 " @autoreleasepool {\n" 20198 " if (a) {\n" 20199 " g();\n" 20200 " }\n" 20201 " }\n" 20202 "});"); 20203 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 20204 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" 20205 "};"); 20206 20207 FormatStyle FourIndent = getLLVMStyle(); 20208 FourIndent.ObjCBlockIndentWidth = 4; 20209 verifyFormat("[operation setCompletionBlock:^{\n" 20210 " [self onOperationDone];\n" 20211 "}];", 20212 FourIndent); 20213 } 20214 20215 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 20216 FormatStyle ZeroColumn = getLLVMStyle(); 20217 ZeroColumn.ColumnLimit = 0; 20218 20219 verifyFormat("[[SessionService sharedService] " 20220 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 20221 " if (window) {\n" 20222 " [self windowDidLoad:window];\n" 20223 " } else {\n" 20224 " [self errorLoadingWindow];\n" 20225 " }\n" 20226 "}];", 20227 ZeroColumn); 20228 EXPECT_EQ("[[SessionService sharedService]\n" 20229 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 20230 " if (window) {\n" 20231 " [self windowDidLoad:window];\n" 20232 " } else {\n" 20233 " [self errorLoadingWindow];\n" 20234 " }\n" 20235 " }];", 20236 format("[[SessionService sharedService]\n" 20237 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 20238 " if (window) {\n" 20239 " [self windowDidLoad:window];\n" 20240 " } else {\n" 20241 " [self errorLoadingWindow];\n" 20242 " }\n" 20243 "}];", 20244 ZeroColumn)); 20245 verifyFormat("[myObject doSomethingWith:arg1\n" 20246 " firstBlock:^(Foo *a) {\n" 20247 " // ...\n" 20248 " int i;\n" 20249 " }\n" 20250 " secondBlock:^(Bar *b) {\n" 20251 " // ...\n" 20252 " int i;\n" 20253 " }\n" 20254 " thirdBlock:^Foo(Bar *b) {\n" 20255 " // ...\n" 20256 " int i;\n" 20257 " }];", 20258 ZeroColumn); 20259 verifyFormat("f(^{\n" 20260 " @autoreleasepool {\n" 20261 " if (a) {\n" 20262 " g();\n" 20263 " }\n" 20264 " }\n" 20265 "});", 20266 ZeroColumn); 20267 verifyFormat("void (^largeBlock)(void) = ^{\n" 20268 " // ...\n" 20269 "};", 20270 ZeroColumn); 20271 20272 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 20273 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 20274 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 20275 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 20276 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 20277 " int i;\n" 20278 "};", 20279 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 20280 } 20281 20282 TEST_F(FormatTest, SupportsCRLF) { 20283 EXPECT_EQ("int a;\r\n" 20284 "int b;\r\n" 20285 "int c;\r\n", 20286 format("int a;\r\n" 20287 " int b;\r\n" 20288 " int c;\r\n", 20289 getLLVMStyle())); 20290 EXPECT_EQ("int a;\r\n" 20291 "int b;\r\n" 20292 "int c;\r\n", 20293 format("int a;\r\n" 20294 " int b;\n" 20295 " int c;\r\n", 20296 getLLVMStyle())); 20297 EXPECT_EQ("int a;\n" 20298 "int b;\n" 20299 "int c;\n", 20300 format("int a;\r\n" 20301 " int b;\n" 20302 " int c;\n", 20303 getLLVMStyle())); 20304 EXPECT_EQ("\"aaaaaaa \"\r\n" 20305 "\"bbbbbbb\";\r\n", 20306 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 20307 EXPECT_EQ("#define A \\\r\n" 20308 " b; \\\r\n" 20309 " c; \\\r\n" 20310 " d;\r\n", 20311 format("#define A \\\r\n" 20312 " b; \\\r\n" 20313 " c; d; \r\n", 20314 getGoogleStyle())); 20315 20316 EXPECT_EQ("/*\r\n" 20317 "multi line block comments\r\n" 20318 "should not introduce\r\n" 20319 "an extra carriage return\r\n" 20320 "*/\r\n", 20321 format("/*\r\n" 20322 "multi line block comments\r\n" 20323 "should not introduce\r\n" 20324 "an extra carriage return\r\n" 20325 "*/\r\n")); 20326 EXPECT_EQ("/*\r\n" 20327 "\r\n" 20328 "*/", 20329 format("/*\r\n" 20330 " \r\r\r\n" 20331 "*/")); 20332 20333 FormatStyle style = getLLVMStyle(); 20334 20335 style.DeriveLineEnding = true; 20336 style.UseCRLF = false; 20337 EXPECT_EQ("union FooBarBazQux {\n" 20338 " int foo;\n" 20339 " int bar;\n" 20340 " int baz;\n" 20341 "};", 20342 format("union FooBarBazQux {\r\n" 20343 " int foo;\n" 20344 " int bar;\r\n" 20345 " int baz;\n" 20346 "};", 20347 style)); 20348 style.UseCRLF = true; 20349 EXPECT_EQ("union FooBarBazQux {\r\n" 20350 " int foo;\r\n" 20351 " int bar;\r\n" 20352 " int baz;\r\n" 20353 "};", 20354 format("union FooBarBazQux {\r\n" 20355 " int foo;\n" 20356 " int bar;\r\n" 20357 " int baz;\n" 20358 "};", 20359 style)); 20360 20361 style.DeriveLineEnding = false; 20362 style.UseCRLF = false; 20363 EXPECT_EQ("union FooBarBazQux {\n" 20364 " int foo;\n" 20365 " int bar;\n" 20366 " int baz;\n" 20367 " int qux;\n" 20368 "};", 20369 format("union FooBarBazQux {\r\n" 20370 " int foo;\n" 20371 " int bar;\r\n" 20372 " int baz;\n" 20373 " int qux;\r\n" 20374 "};", 20375 style)); 20376 style.UseCRLF = true; 20377 EXPECT_EQ("union FooBarBazQux {\r\n" 20378 " int foo;\r\n" 20379 " int bar;\r\n" 20380 " int baz;\r\n" 20381 " int qux;\r\n" 20382 "};", 20383 format("union FooBarBazQux {\r\n" 20384 " int foo;\n" 20385 " int bar;\r\n" 20386 " int baz;\n" 20387 " int qux;\n" 20388 "};", 20389 style)); 20390 20391 style.DeriveLineEnding = true; 20392 style.UseCRLF = false; 20393 EXPECT_EQ("union FooBarBazQux {\r\n" 20394 " int foo;\r\n" 20395 " int bar;\r\n" 20396 " int baz;\r\n" 20397 " int qux;\r\n" 20398 "};", 20399 format("union FooBarBazQux {\r\n" 20400 " int foo;\n" 20401 " int bar;\r\n" 20402 " int baz;\n" 20403 " int qux;\r\n" 20404 "};", 20405 style)); 20406 style.UseCRLF = true; 20407 EXPECT_EQ("union FooBarBazQux {\n" 20408 " int foo;\n" 20409 " int bar;\n" 20410 " int baz;\n" 20411 " int qux;\n" 20412 "};", 20413 format("union FooBarBazQux {\r\n" 20414 " int foo;\n" 20415 " int bar;\r\n" 20416 " int baz;\n" 20417 " int qux;\n" 20418 "};", 20419 style)); 20420 } 20421 20422 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 20423 verifyFormat("MY_CLASS(C) {\n" 20424 " int i;\n" 20425 " int j;\n" 20426 "};"); 20427 } 20428 20429 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 20430 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 20431 TwoIndent.ContinuationIndentWidth = 2; 20432 20433 EXPECT_EQ("int i =\n" 20434 " longFunction(\n" 20435 " arg);", 20436 format("int i = longFunction(arg);", TwoIndent)); 20437 20438 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 20439 SixIndent.ContinuationIndentWidth = 6; 20440 20441 EXPECT_EQ("int i =\n" 20442 " longFunction(\n" 20443 " arg);", 20444 format("int i = longFunction(arg);", SixIndent)); 20445 } 20446 20447 TEST_F(FormatTest, WrappedClosingParenthesisIndent) { 20448 FormatStyle Style = getLLVMStyle(); 20449 verifyFormat("int Foo::getter(\n" 20450 " //\n" 20451 ") const {\n" 20452 " return foo;\n" 20453 "}", 20454 Style); 20455 verifyFormat("void Foo::setter(\n" 20456 " //\n" 20457 ") {\n" 20458 " foo = 1;\n" 20459 "}", 20460 Style); 20461 } 20462 20463 TEST_F(FormatTest, SpacesInAngles) { 20464 FormatStyle Spaces = getLLVMStyle(); 20465 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 20466 20467 verifyFormat("vector< ::std::string > x1;", Spaces); 20468 verifyFormat("Foo< int, Bar > x2;", Spaces); 20469 verifyFormat("Foo< ::int, ::Bar > x3;", Spaces); 20470 20471 verifyFormat("static_cast< int >(arg);", Spaces); 20472 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 20473 verifyFormat("f< int, float >();", Spaces); 20474 verifyFormat("template <> g() {}", Spaces); 20475 verifyFormat("template < std::vector< int > > f() {}", Spaces); 20476 verifyFormat("std::function< void(int, int) > fct;", Spaces); 20477 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 20478 Spaces); 20479 20480 Spaces.Standard = FormatStyle::LS_Cpp03; 20481 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 20482 verifyFormat("A< A< int > >();", Spaces); 20483 20484 Spaces.SpacesInAngles = FormatStyle::SIAS_Never; 20485 verifyFormat("A<A<int> >();", Spaces); 20486 20487 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave; 20488 verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;", 20489 Spaces); 20490 verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;", 20491 Spaces); 20492 20493 verifyFormat("A<A<int> >();", Spaces); 20494 verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces); 20495 verifyFormat("A< A< int > >();", Spaces); 20496 20497 Spaces.Standard = FormatStyle::LS_Cpp11; 20498 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 20499 verifyFormat("A< A< int > >();", Spaces); 20500 20501 Spaces.SpacesInAngles = FormatStyle::SIAS_Never; 20502 verifyFormat("vector<::std::string> x4;", Spaces); 20503 verifyFormat("vector<int> x5;", Spaces); 20504 verifyFormat("Foo<int, Bar> x6;", Spaces); 20505 verifyFormat("Foo<::int, ::Bar> x7;", Spaces); 20506 20507 verifyFormat("A<A<int>>();", Spaces); 20508 20509 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave; 20510 verifyFormat("vector<::std::string> x4;", Spaces); 20511 verifyFormat("vector< ::std::string > x4;", Spaces); 20512 verifyFormat("vector<int> x5;", Spaces); 20513 verifyFormat("vector< int > x5;", Spaces); 20514 verifyFormat("Foo<int, Bar> x6;", Spaces); 20515 verifyFormat("Foo< int, Bar > x6;", Spaces); 20516 verifyFormat("Foo<::int, ::Bar> x7;", Spaces); 20517 verifyFormat("Foo< ::int, ::Bar > x7;", Spaces); 20518 20519 verifyFormat("A<A<int>>();", Spaces); 20520 verifyFormat("A< A< int > >();", Spaces); 20521 verifyFormat("A<A<int > >();", Spaces); 20522 verifyFormat("A< A< int>>();", Spaces); 20523 } 20524 20525 TEST_F(FormatTest, SpaceAfterTemplateKeyword) { 20526 FormatStyle Style = getLLVMStyle(); 20527 Style.SpaceAfterTemplateKeyword = false; 20528 verifyFormat("template<int> void foo();", Style); 20529 } 20530 20531 TEST_F(FormatTest, TripleAngleBrackets) { 20532 verifyFormat("f<<<1, 1>>>();"); 20533 verifyFormat("f<<<1, 1, 1, s>>>();"); 20534 verifyFormat("f<<<a, b, c, d>>>();"); 20535 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 20536 verifyFormat("f<param><<<1, 1>>>();"); 20537 verifyFormat("f<1><<<1, 1>>>();"); 20538 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 20539 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 20540 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 20541 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n" 20542 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();"); 20543 } 20544 20545 TEST_F(FormatTest, MergeLessLessAtEnd) { 20546 verifyFormat("<<"); 20547 EXPECT_EQ("< < <", format("\\\n<<<")); 20548 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 20549 "aaallvm::outs() <<"); 20550 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 20551 "aaaallvm::outs()\n <<"); 20552 } 20553 20554 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 20555 std::string code = "#if A\n" 20556 "#if B\n" 20557 "a.\n" 20558 "#endif\n" 20559 " a = 1;\n" 20560 "#else\n" 20561 "#endif\n" 20562 "#if C\n" 20563 "#else\n" 20564 "#endif\n"; 20565 EXPECT_EQ(code, format(code)); 20566 } 20567 20568 TEST_F(FormatTest, HandleConflictMarkers) { 20569 // Git/SVN conflict markers. 20570 EXPECT_EQ("int a;\n" 20571 "void f() {\n" 20572 " callme(some(parameter1,\n" 20573 "<<<<<<< text by the vcs\n" 20574 " parameter2),\n" 20575 "||||||| text by the vcs\n" 20576 " parameter2),\n" 20577 " parameter3,\n" 20578 "======= text by the vcs\n" 20579 " parameter2, parameter3),\n" 20580 ">>>>>>> text by the vcs\n" 20581 " otherparameter);\n", 20582 format("int a;\n" 20583 "void f() {\n" 20584 " callme(some(parameter1,\n" 20585 "<<<<<<< text by the vcs\n" 20586 " parameter2),\n" 20587 "||||||| text by the vcs\n" 20588 " parameter2),\n" 20589 " parameter3,\n" 20590 "======= text by the vcs\n" 20591 " parameter2,\n" 20592 " parameter3),\n" 20593 ">>>>>>> text by the vcs\n" 20594 " otherparameter);\n")); 20595 20596 // Perforce markers. 20597 EXPECT_EQ("void f() {\n" 20598 " function(\n" 20599 ">>>> text by the vcs\n" 20600 " parameter,\n" 20601 "==== text by the vcs\n" 20602 " parameter,\n" 20603 "==== text by the vcs\n" 20604 " parameter,\n" 20605 "<<<< text by the vcs\n" 20606 " parameter);\n", 20607 format("void f() {\n" 20608 " function(\n" 20609 ">>>> text by the vcs\n" 20610 " parameter,\n" 20611 "==== text by the vcs\n" 20612 " parameter,\n" 20613 "==== text by the vcs\n" 20614 " parameter,\n" 20615 "<<<< text by the vcs\n" 20616 " parameter);\n")); 20617 20618 EXPECT_EQ("<<<<<<<\n" 20619 "|||||||\n" 20620 "=======\n" 20621 ">>>>>>>", 20622 format("<<<<<<<\n" 20623 "|||||||\n" 20624 "=======\n" 20625 ">>>>>>>")); 20626 20627 EXPECT_EQ("<<<<<<<\n" 20628 "|||||||\n" 20629 "int i;\n" 20630 "=======\n" 20631 ">>>>>>>", 20632 format("<<<<<<<\n" 20633 "|||||||\n" 20634 "int i;\n" 20635 "=======\n" 20636 ">>>>>>>")); 20637 20638 // FIXME: Handle parsing of macros around conflict markers correctly: 20639 EXPECT_EQ("#define Macro \\\n" 20640 "<<<<<<<\n" 20641 "Something \\\n" 20642 "|||||||\n" 20643 "Else \\\n" 20644 "=======\n" 20645 "Other \\\n" 20646 ">>>>>>>\n" 20647 " End int i;\n", 20648 format("#define Macro \\\n" 20649 "<<<<<<<\n" 20650 " Something \\\n" 20651 "|||||||\n" 20652 " Else \\\n" 20653 "=======\n" 20654 " Other \\\n" 20655 ">>>>>>>\n" 20656 " End\n" 20657 "int i;\n")); 20658 } 20659 20660 TEST_F(FormatTest, DisableRegions) { 20661 EXPECT_EQ("int i;\n" 20662 "// clang-format off\n" 20663 " int j;\n" 20664 "// clang-format on\n" 20665 "int k;", 20666 format(" int i;\n" 20667 " // clang-format off\n" 20668 " int j;\n" 20669 " // clang-format on\n" 20670 " int k;")); 20671 EXPECT_EQ("int i;\n" 20672 "/* clang-format off */\n" 20673 " int j;\n" 20674 "/* clang-format on */\n" 20675 "int k;", 20676 format(" int i;\n" 20677 " /* clang-format off */\n" 20678 " int j;\n" 20679 " /* clang-format on */\n" 20680 " int k;")); 20681 20682 // Don't reflow comments within disabled regions. 20683 EXPECT_EQ("// clang-format off\n" 20684 "// long long long long long long line\n" 20685 "/* clang-format on */\n" 20686 "/* long long long\n" 20687 " * long long long\n" 20688 " * line */\n" 20689 "int i;\n" 20690 "/* clang-format off */\n" 20691 "/* long long long long long long line */\n", 20692 format("// clang-format off\n" 20693 "// long long long long long long line\n" 20694 "/* clang-format on */\n" 20695 "/* long long long long long long line */\n" 20696 "int i;\n" 20697 "/* clang-format off */\n" 20698 "/* long long long long long long line */\n", 20699 getLLVMStyleWithColumns(20))); 20700 } 20701 20702 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 20703 format("? ) ="); 20704 verifyNoCrash("#define a\\\n /**/}"); 20705 } 20706 20707 TEST_F(FormatTest, FormatsTableGenCode) { 20708 FormatStyle Style = getLLVMStyle(); 20709 Style.Language = FormatStyle::LK_TableGen; 20710 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 20711 } 20712 20713 TEST_F(FormatTest, ArrayOfTemplates) { 20714 EXPECT_EQ("auto a = new unique_ptr<int>[10];", 20715 format("auto a = new unique_ptr<int > [ 10];")); 20716 20717 FormatStyle Spaces = getLLVMStyle(); 20718 Spaces.SpacesInSquareBrackets = true; 20719 EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];", 20720 format("auto a = new unique_ptr<int > [10];", Spaces)); 20721 } 20722 20723 TEST_F(FormatTest, ArrayAsTemplateType) { 20724 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;", 20725 format("auto a = unique_ptr < Foo < Bar>[ 10]> ;")); 20726 20727 FormatStyle Spaces = getLLVMStyle(); 20728 Spaces.SpacesInSquareBrackets = true; 20729 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;", 20730 format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces)); 20731 } 20732 20733 TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); } 20734 20735 TEST(FormatStyle, GetStyleWithEmptyFileName) { 20736 llvm::vfs::InMemoryFileSystem FS; 20737 auto Style1 = getStyle("file", "", "Google", "", &FS); 20738 ASSERT_TRUE((bool)Style1); 20739 ASSERT_EQ(*Style1, getGoogleStyle()); 20740 } 20741 20742 TEST(FormatStyle, GetStyleOfFile) { 20743 llvm::vfs::InMemoryFileSystem FS; 20744 // Test 1: format file in the same directory. 20745 ASSERT_TRUE( 20746 FS.addFile("/a/.clang-format", 0, 20747 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 20748 ASSERT_TRUE( 20749 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 20750 auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS); 20751 ASSERT_TRUE((bool)Style1); 20752 ASSERT_EQ(*Style1, getLLVMStyle()); 20753 20754 // Test 2.1: fallback to default. 20755 ASSERT_TRUE( 20756 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 20757 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS); 20758 ASSERT_TRUE((bool)Style2); 20759 ASSERT_EQ(*Style2, getMozillaStyle()); 20760 20761 // Test 2.2: no format on 'none' fallback style. 20762 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 20763 ASSERT_TRUE((bool)Style2); 20764 ASSERT_EQ(*Style2, getNoStyle()); 20765 20766 // Test 2.3: format if config is found with no based style while fallback is 20767 // 'none'. 20768 ASSERT_TRUE(FS.addFile("/b/.clang-format", 0, 20769 llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2"))); 20770 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 20771 ASSERT_TRUE((bool)Style2); 20772 ASSERT_EQ(*Style2, getLLVMStyle()); 20773 20774 // Test 2.4: format if yaml with no based style, while fallback is 'none'. 20775 Style2 = getStyle("{}", "a.h", "none", "", &FS); 20776 ASSERT_TRUE((bool)Style2); 20777 ASSERT_EQ(*Style2, getLLVMStyle()); 20778 20779 // Test 3: format file in parent directory. 20780 ASSERT_TRUE( 20781 FS.addFile("/c/.clang-format", 0, 20782 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 20783 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, 20784 llvm::MemoryBuffer::getMemBuffer("int i;"))); 20785 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS); 20786 ASSERT_TRUE((bool)Style3); 20787 ASSERT_EQ(*Style3, getGoogleStyle()); 20788 20789 // Test 4: error on invalid fallback style 20790 auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS); 20791 ASSERT_FALSE((bool)Style4); 20792 llvm::consumeError(Style4.takeError()); 20793 20794 // Test 5: error on invalid yaml on command line 20795 auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS); 20796 ASSERT_FALSE((bool)Style5); 20797 llvm::consumeError(Style5.takeError()); 20798 20799 // Test 6: error on invalid style 20800 auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS); 20801 ASSERT_FALSE((bool)Style6); 20802 llvm::consumeError(Style6.takeError()); 20803 20804 // Test 7: found config file, error on parsing it 20805 ASSERT_TRUE( 20806 FS.addFile("/d/.clang-format", 0, 20807 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n" 20808 "InvalidKey: InvalidValue"))); 20809 ASSERT_TRUE( 20810 FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 20811 auto Style7a = getStyle("file", "/d/.clang-format", "LLVM", "", &FS); 20812 ASSERT_FALSE((bool)Style7a); 20813 llvm::consumeError(Style7a.takeError()); 20814 20815 auto Style7b = getStyle("file", "/d/.clang-format", "LLVM", "", &FS, true); 20816 ASSERT_TRUE((bool)Style7b); 20817 20818 // Test 8: inferred per-language defaults apply. 20819 auto StyleTd = getStyle("file", "x.td", "llvm", "", &FS); 20820 ASSERT_TRUE((bool)StyleTd); 20821 ASSERT_EQ(*StyleTd, getLLVMStyle(FormatStyle::LK_TableGen)); 20822 20823 // Test 9.1: overwriting a file style, when parent no file exists with no 20824 // fallback style 20825 ASSERT_TRUE(FS.addFile( 20826 "/e/sub/.clang-format", 0, 20827 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: InheritParentConfig\n" 20828 "ColumnLimit: 20"))); 20829 ASSERT_TRUE(FS.addFile("/e/sub/code.cpp", 0, 20830 llvm::MemoryBuffer::getMemBuffer("int i;"))); 20831 auto Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS); 20832 ASSERT_TRUE(static_cast<bool>(Style9)); 20833 ASSERT_EQ(*Style9, [] { 20834 auto Style = getNoStyle(); 20835 Style.ColumnLimit = 20; 20836 return Style; 20837 }()); 20838 20839 // Test 9.2: with LLVM fallback style 20840 Style9 = getStyle("file", "/e/sub/code.cpp", "LLVM", "", &FS); 20841 ASSERT_TRUE(static_cast<bool>(Style9)); 20842 ASSERT_EQ(*Style9, [] { 20843 auto Style = getLLVMStyle(); 20844 Style.ColumnLimit = 20; 20845 return Style; 20846 }()); 20847 20848 // Test 9.3: with a parent file 20849 ASSERT_TRUE( 20850 FS.addFile("/e/.clang-format", 0, 20851 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google\n" 20852 "UseTab: Always"))); 20853 Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS); 20854 ASSERT_TRUE(static_cast<bool>(Style9)); 20855 ASSERT_EQ(*Style9, [] { 20856 auto Style = getGoogleStyle(); 20857 Style.ColumnLimit = 20; 20858 Style.UseTab = FormatStyle::UT_Always; 20859 return Style; 20860 }()); 20861 20862 // Test 9.4: propagate more than one level 20863 ASSERT_TRUE(FS.addFile("/e/sub/sub/code.cpp", 0, 20864 llvm::MemoryBuffer::getMemBuffer("int i;"))); 20865 ASSERT_TRUE(FS.addFile("/e/sub/sub/.clang-format", 0, 20866 llvm::MemoryBuffer::getMemBuffer( 20867 "BasedOnStyle: InheritParentConfig\n" 20868 "WhitespaceSensitiveMacros: ['FOO', 'BAR']"))); 20869 std::vector<std::string> NonDefaultWhiteSpaceMacros{"FOO", "BAR"}; 20870 20871 const auto SubSubStyle = [&NonDefaultWhiteSpaceMacros] { 20872 auto Style = getGoogleStyle(); 20873 Style.ColumnLimit = 20; 20874 Style.UseTab = FormatStyle::UT_Always; 20875 Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros; 20876 return Style; 20877 }(); 20878 20879 ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros); 20880 Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS); 20881 ASSERT_TRUE(static_cast<bool>(Style9)); 20882 ASSERT_EQ(*Style9, SubSubStyle); 20883 20884 // Test 9.5: use InheritParentConfig as style name 20885 Style9 = 20886 getStyle("inheritparentconfig", "/e/sub/sub/code.cpp", "none", "", &FS); 20887 ASSERT_TRUE(static_cast<bool>(Style9)); 20888 ASSERT_EQ(*Style9, SubSubStyle); 20889 20890 // Test 9.6: use command line style with inheritance 20891 Style9 = getStyle("{BasedOnStyle: InheritParentConfig}", "/e/sub/code.cpp", 20892 "none", "", &FS); 20893 ASSERT_TRUE(static_cast<bool>(Style9)); 20894 ASSERT_EQ(*Style9, SubSubStyle); 20895 20896 // Test 9.7: use command line style with inheritance and own config 20897 Style9 = getStyle("{BasedOnStyle: InheritParentConfig, " 20898 "WhitespaceSensitiveMacros: ['FOO', 'BAR']}", 20899 "/e/sub/code.cpp", "none", "", &FS); 20900 ASSERT_TRUE(static_cast<bool>(Style9)); 20901 ASSERT_EQ(*Style9, SubSubStyle); 20902 20903 // Test 9.8: use inheritance from a file without BasedOnStyle 20904 ASSERT_TRUE(FS.addFile("/e/withoutbase/.clang-format", 0, 20905 llvm::MemoryBuffer::getMemBuffer("ColumnLimit: 123"))); 20906 ASSERT_TRUE( 20907 FS.addFile("/e/withoutbase/sub/.clang-format", 0, 20908 llvm::MemoryBuffer::getMemBuffer( 20909 "BasedOnStyle: InheritParentConfig\nIndentWidth: 7"))); 20910 // Make sure we do not use the fallback style 20911 Style9 = getStyle("file", "/e/withoutbase/code.cpp", "google", "", &FS); 20912 ASSERT_TRUE(static_cast<bool>(Style9)); 20913 ASSERT_EQ(*Style9, [] { 20914 auto Style = getLLVMStyle(); 20915 Style.ColumnLimit = 123; 20916 return Style; 20917 }()); 20918 20919 Style9 = getStyle("file", "/e/withoutbase/sub/code.cpp", "google", "", &FS); 20920 ASSERT_TRUE(static_cast<bool>(Style9)); 20921 ASSERT_EQ(*Style9, [] { 20922 auto Style = getLLVMStyle(); 20923 Style.ColumnLimit = 123; 20924 Style.IndentWidth = 7; 20925 return Style; 20926 }()); 20927 } 20928 20929 TEST_F(ReplacementTest, FormatCodeAfterReplacements) { 20930 // Column limit is 20. 20931 std::string Code = "Type *a =\n" 20932 " new Type();\n" 20933 "g(iiiii, 0, jjjjj,\n" 20934 " 0, kkkkk, 0, mm);\n" 20935 "int bad = format ;"; 20936 std::string Expected = "auto a = new Type();\n" 20937 "g(iiiii, nullptr,\n" 20938 " jjjjj, nullptr,\n" 20939 " kkkkk, nullptr,\n" 20940 " mm);\n" 20941 "int bad = format ;"; 20942 FileID ID = Context.createInMemoryFile("format.cpp", Code); 20943 tooling::Replacements Replaces = toReplacements( 20944 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6, 20945 "auto "), 20946 tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1, 20947 "nullptr"), 20948 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1, 20949 "nullptr"), 20950 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1, 20951 "nullptr")}); 20952 20953 format::FormatStyle Style = format::getLLVMStyle(); 20954 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. 20955 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 20956 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 20957 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 20958 auto Result = applyAllReplacements(Code, *FormattedReplaces); 20959 EXPECT_TRUE(static_cast<bool>(Result)); 20960 EXPECT_EQ(Expected, *Result); 20961 } 20962 20963 TEST_F(ReplacementTest, SortIncludesAfterReplacement) { 20964 std::string Code = "#include \"a.h\"\n" 20965 "#include \"c.h\"\n" 20966 "\n" 20967 "int main() {\n" 20968 " return 0;\n" 20969 "}"; 20970 std::string Expected = "#include \"a.h\"\n" 20971 "#include \"b.h\"\n" 20972 "#include \"c.h\"\n" 20973 "\n" 20974 "int main() {\n" 20975 " return 0;\n" 20976 "}"; 20977 FileID ID = Context.createInMemoryFile("fix.cpp", Code); 20978 tooling::Replacements Replaces = toReplacements( 20979 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0, 20980 "#include \"b.h\"\n")}); 20981 20982 format::FormatStyle Style = format::getLLVMStyle(); 20983 Style.SortIncludes = FormatStyle::SI_CaseSensitive; 20984 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 20985 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 20986 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 20987 auto Result = applyAllReplacements(Code, *FormattedReplaces); 20988 EXPECT_TRUE(static_cast<bool>(Result)); 20989 EXPECT_EQ(Expected, *Result); 20990 } 20991 20992 TEST_F(FormatTest, FormatSortsUsingDeclarations) { 20993 EXPECT_EQ("using std::cin;\n" 20994 "using std::cout;", 20995 format("using std::cout;\n" 20996 "using std::cin;", 20997 getGoogleStyle())); 20998 } 20999 21000 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) { 21001 format::FormatStyle Style = format::getLLVMStyle(); 21002 Style.Standard = FormatStyle::LS_Cpp03; 21003 // cpp03 recognize this string as identifier u8 and literal character 'a' 21004 EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style)); 21005 } 21006 21007 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) { 21008 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers 21009 // all modes, including C++11, C++14 and C++17 21010 EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';")); 21011 } 21012 21013 TEST_F(FormatTest, DoNotFormatLikelyXml) { 21014 EXPECT_EQ("<!-- ;> -->", format("<!-- ;> -->", getGoogleStyle())); 21015 EXPECT_EQ(" <!-- >; -->", format(" <!-- >; -->", getGoogleStyle())); 21016 } 21017 21018 TEST_F(FormatTest, StructuredBindings) { 21019 // Structured bindings is a C++17 feature. 21020 // all modes, including C++11, C++14 and C++17 21021 verifyFormat("auto [a, b] = f();"); 21022 EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();")); 21023 EXPECT_EQ("const auto [a, b] = f();", format("const auto[a, b] = f();")); 21024 EXPECT_EQ("auto const [a, b] = f();", format("auto const[a, b] = f();")); 21025 EXPECT_EQ("auto const volatile [a, b] = f();", 21026 format("auto const volatile[a, b] = f();")); 21027 EXPECT_EQ("auto [a, b, c] = f();", format("auto [ a , b,c ] = f();")); 21028 EXPECT_EQ("auto &[a, b, c] = f();", 21029 format("auto &[ a , b,c ] = f();")); 21030 EXPECT_EQ("auto &&[a, b, c] = f();", 21031 format("auto &&[ a , b,c ] = f();")); 21032 EXPECT_EQ("auto const &[a, b] = f();", format("auto const&[a, b] = f();")); 21033 EXPECT_EQ("auto const volatile &&[a, b] = f();", 21034 format("auto const volatile &&[a, b] = f();")); 21035 EXPECT_EQ("auto const &&[a, b] = f();", 21036 format("auto const && [a, b] = f();")); 21037 EXPECT_EQ("const auto &[a, b] = f();", 21038 format("const auto & [a, b] = f();")); 21039 EXPECT_EQ("const auto volatile &&[a, b] = f();", 21040 format("const auto volatile &&[a, b] = f();")); 21041 EXPECT_EQ("volatile const auto &&[a, b] = f();", 21042 format("volatile const auto &&[a, b] = f();")); 21043 EXPECT_EQ("const auto &&[a, b] = f();", 21044 format("const auto && [a, b] = f();")); 21045 21046 // Make sure we don't mistake structured bindings for lambdas. 21047 FormatStyle PointerMiddle = getLLVMStyle(); 21048 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 21049 verifyFormat("auto [a1, b]{A * i};", getGoogleStyle()); 21050 verifyFormat("auto [a2, b]{A * i};", getLLVMStyle()); 21051 verifyFormat("auto [a3, b]{A * i};", PointerMiddle); 21052 verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle()); 21053 verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle()); 21054 verifyFormat("auto const [a3, b]{A * i};", PointerMiddle); 21055 verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle()); 21056 verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle()); 21057 verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle); 21058 verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle()); 21059 verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle()); 21060 verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle); 21061 21062 EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}", 21063 format("for (const auto && [a, b] : some_range) {\n}")); 21064 EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}", 21065 format("for (const auto & [a, b] : some_range) {\n}")); 21066 EXPECT_EQ("for (const auto [a, b] : some_range) {\n}", 21067 format("for (const auto[a, b] : some_range) {\n}")); 21068 EXPECT_EQ("auto [x, y](expr);", format("auto[x,y] (expr);")); 21069 EXPECT_EQ("auto &[x, y](expr);", format("auto & [x,y] (expr);")); 21070 EXPECT_EQ("auto &&[x, y](expr);", format("auto && [x,y] (expr);")); 21071 EXPECT_EQ("auto const &[x, y](expr);", 21072 format("auto const & [x,y] (expr);")); 21073 EXPECT_EQ("auto const &&[x, y](expr);", 21074 format("auto const && [x,y] (expr);")); 21075 EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y] {expr};")); 21076 EXPECT_EQ("auto const &[x, y]{expr};", 21077 format("auto const & [x,y] {expr};")); 21078 EXPECT_EQ("auto const &&[x, y]{expr};", 21079 format("auto const && [x,y] {expr};")); 21080 21081 format::FormatStyle Spaces = format::getLLVMStyle(); 21082 Spaces.SpacesInSquareBrackets = true; 21083 verifyFormat("auto [ a, b ] = f();", Spaces); 21084 verifyFormat("auto &&[ a, b ] = f();", Spaces); 21085 verifyFormat("auto &[ a, b ] = f();", Spaces); 21086 verifyFormat("auto const &&[ a, b ] = f();", Spaces); 21087 verifyFormat("auto const &[ a, b ] = f();", Spaces); 21088 } 21089 21090 TEST_F(FormatTest, FileAndCode) { 21091 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", "")); 21092 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", "")); 21093 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", "")); 21094 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "")); 21095 EXPECT_EQ(FormatStyle::LK_ObjC, 21096 guessLanguage("foo.h", "@interface Foo\n@end\n")); 21097 EXPECT_EQ( 21098 FormatStyle::LK_ObjC, 21099 guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }")); 21100 EXPECT_EQ(FormatStyle::LK_ObjC, 21101 guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))")); 21102 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;")); 21103 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", "")); 21104 EXPECT_EQ(FormatStyle::LK_ObjC, 21105 guessLanguage("foo", "@interface Foo\n@end\n")); 21106 EXPECT_EQ(FormatStyle::LK_ObjC, 21107 guessLanguage("foo.h", "int DoStuff(CGRect rect);\n")); 21108 EXPECT_EQ( 21109 FormatStyle::LK_ObjC, 21110 guessLanguage("foo.h", 21111 "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n")); 21112 EXPECT_EQ( 21113 FormatStyle::LK_Cpp, 21114 guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;")); 21115 } 21116 21117 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) { 21118 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];")); 21119 EXPECT_EQ(FormatStyle::LK_ObjC, 21120 guessLanguage("foo.h", "array[[calculator getIndex]];")); 21121 EXPECT_EQ(FormatStyle::LK_Cpp, 21122 guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];")); 21123 EXPECT_EQ( 21124 FormatStyle::LK_Cpp, 21125 guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];")); 21126 EXPECT_EQ(FormatStyle::LK_ObjC, 21127 guessLanguage("foo.h", "[[noreturn foo] bar];")); 21128 EXPECT_EQ(FormatStyle::LK_Cpp, 21129 guessLanguage("foo.h", "[[clang::fallthrough]];")); 21130 EXPECT_EQ(FormatStyle::LK_ObjC, 21131 guessLanguage("foo.h", "[[clang:fallthrough] foo];")); 21132 EXPECT_EQ(FormatStyle::LK_Cpp, 21133 guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];")); 21134 EXPECT_EQ(FormatStyle::LK_Cpp, 21135 guessLanguage("foo.h", "[[using clang: fallthrough]];")); 21136 EXPECT_EQ(FormatStyle::LK_ObjC, 21137 guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];")); 21138 EXPECT_EQ(FormatStyle::LK_Cpp, 21139 guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];")); 21140 EXPECT_EQ( 21141 FormatStyle::LK_Cpp, 21142 guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)")); 21143 EXPECT_EQ( 21144 FormatStyle::LK_Cpp, 21145 guessLanguage("foo.h", 21146 "[[clang::callable_when(\"unconsumed\", \"unknown\")]]")); 21147 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]")); 21148 } 21149 21150 TEST_F(FormatTest, GuessLanguageWithCaret) { 21151 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);")); 21152 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);")); 21153 EXPECT_EQ(FormatStyle::LK_ObjC, 21154 guessLanguage("foo.h", "int(^)(char, float);")); 21155 EXPECT_EQ(FormatStyle::LK_ObjC, 21156 guessLanguage("foo.h", "int(^foo)(char, float);")); 21157 EXPECT_EQ(FormatStyle::LK_ObjC, 21158 guessLanguage("foo.h", "int(^foo[10])(char, float);")); 21159 EXPECT_EQ(FormatStyle::LK_ObjC, 21160 guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);")); 21161 EXPECT_EQ( 21162 FormatStyle::LK_ObjC, 21163 guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);")); 21164 } 21165 21166 TEST_F(FormatTest, GuessLanguageWithPragmas) { 21167 EXPECT_EQ(FormatStyle::LK_Cpp, 21168 guessLanguage("foo.h", "__pragma(warning(disable:))")); 21169 EXPECT_EQ(FormatStyle::LK_Cpp, 21170 guessLanguage("foo.h", "#pragma(warning(disable:))")); 21171 EXPECT_EQ(FormatStyle::LK_Cpp, 21172 guessLanguage("foo.h", "_Pragma(warning(disable:))")); 21173 } 21174 21175 TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) { 21176 // ASM symbolic names are identifiers that must be surrounded by [] without 21177 // space in between: 21178 // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands 21179 21180 // Example from https://bugs.llvm.org/show_bug.cgi?id=45108. 21181 verifyFormat(R"(// 21182 asm volatile("mrs %x[result], FPCR" : [result] "=r"(result)); 21183 )"); 21184 21185 // A list of several ASM symbolic names. 21186 verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)"); 21187 21188 // ASM symbolic names in inline ASM with inputs and outputs. 21189 verifyFormat(R"(// 21190 asm("cmoveq %1, %2, %[result]" 21191 : [result] "=r"(result) 21192 : "r"(test), "r"(new), "[result]"(old)); 21193 )"); 21194 21195 // ASM symbolic names in inline ASM with no outputs. 21196 verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)"); 21197 } 21198 21199 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) { 21200 EXPECT_EQ(FormatStyle::LK_Cpp, 21201 guessLanguage("foo.h", "void f() {\n" 21202 " asm (\"mov %[e], %[d]\"\n" 21203 " : [d] \"=rm\" (d)\n" 21204 " [e] \"rm\" (*e));\n" 21205 "}")); 21206 EXPECT_EQ(FormatStyle::LK_Cpp, 21207 guessLanguage("foo.h", "void f() {\n" 21208 " _asm (\"mov %[e], %[d]\"\n" 21209 " : [d] \"=rm\" (d)\n" 21210 " [e] \"rm\" (*e));\n" 21211 "}")); 21212 EXPECT_EQ(FormatStyle::LK_Cpp, 21213 guessLanguage("foo.h", "void f() {\n" 21214 " __asm (\"mov %[e], %[d]\"\n" 21215 " : [d] \"=rm\" (d)\n" 21216 " [e] \"rm\" (*e));\n" 21217 "}")); 21218 EXPECT_EQ(FormatStyle::LK_Cpp, 21219 guessLanguage("foo.h", "void f() {\n" 21220 " __asm__ (\"mov %[e], %[d]\"\n" 21221 " : [d] \"=rm\" (d)\n" 21222 " [e] \"rm\" (*e));\n" 21223 "}")); 21224 EXPECT_EQ(FormatStyle::LK_Cpp, 21225 guessLanguage("foo.h", "void f() {\n" 21226 " asm (\"mov %[e], %[d]\"\n" 21227 " : [d] \"=rm\" (d),\n" 21228 " [e] \"rm\" (*e));\n" 21229 "}")); 21230 EXPECT_EQ(FormatStyle::LK_Cpp, 21231 guessLanguage("foo.h", "void f() {\n" 21232 " asm volatile (\"mov %[e], %[d]\"\n" 21233 " : [d] \"=rm\" (d)\n" 21234 " [e] \"rm\" (*e));\n" 21235 "}")); 21236 } 21237 21238 TEST_F(FormatTest, GuessLanguageWithChildLines) { 21239 EXPECT_EQ(FormatStyle::LK_Cpp, 21240 guessLanguage("foo.h", "#define FOO ({ std::string s; })")); 21241 EXPECT_EQ(FormatStyle::LK_ObjC, 21242 guessLanguage("foo.h", "#define FOO ({ NSString *s; })")); 21243 EXPECT_EQ( 21244 FormatStyle::LK_Cpp, 21245 guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })")); 21246 EXPECT_EQ( 21247 FormatStyle::LK_ObjC, 21248 guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })")); 21249 } 21250 21251 TEST_F(FormatTest, TypenameMacros) { 21252 std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"}; 21253 21254 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353 21255 FormatStyle Google = getGoogleStyleWithColumns(0); 21256 Google.TypenameMacros = TypenameMacros; 21257 verifyFormat("struct foo {\n" 21258 " int bar;\n" 21259 " TAILQ_ENTRY(a) bleh;\n" 21260 "};", 21261 Google); 21262 21263 FormatStyle Macros = getLLVMStyle(); 21264 Macros.TypenameMacros = TypenameMacros; 21265 21266 verifyFormat("STACK_OF(int) a;", Macros); 21267 verifyFormat("STACK_OF(int) *a;", Macros); 21268 verifyFormat("STACK_OF(int const *) *a;", Macros); 21269 verifyFormat("STACK_OF(int *const) *a;", Macros); 21270 verifyFormat("STACK_OF(int, string) a;", Macros); 21271 verifyFormat("STACK_OF(LIST(int)) a;", Macros); 21272 verifyFormat("STACK_OF(LIST(int)) a, b;", Macros); 21273 verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros); 21274 verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros); 21275 verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros); 21276 verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros); 21277 21278 Macros.PointerAlignment = FormatStyle::PAS_Left; 21279 verifyFormat("STACK_OF(int)* a;", Macros); 21280 verifyFormat("STACK_OF(int*)* a;", Macros); 21281 verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros); 21282 verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros); 21283 verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros); 21284 } 21285 21286 TEST_F(FormatTest, AtomicQualifier) { 21287 // Check that we treate _Atomic as a type and not a function call 21288 FormatStyle Google = getGoogleStyleWithColumns(0); 21289 verifyFormat("struct foo {\n" 21290 " int a1;\n" 21291 " _Atomic(a) a2;\n" 21292 " _Atomic(_Atomic(int) *const) a3;\n" 21293 "};", 21294 Google); 21295 verifyFormat("_Atomic(uint64_t) a;"); 21296 verifyFormat("_Atomic(uint64_t) *a;"); 21297 verifyFormat("_Atomic(uint64_t const *) *a;"); 21298 verifyFormat("_Atomic(uint64_t *const) *a;"); 21299 verifyFormat("_Atomic(const uint64_t *) *a;"); 21300 verifyFormat("_Atomic(uint64_t) a;"); 21301 verifyFormat("_Atomic(_Atomic(uint64_t)) a;"); 21302 verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;"); 21303 verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}"); 21304 verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);"); 21305 21306 verifyFormat("_Atomic(uint64_t) *s(InitValue);"); 21307 verifyFormat("_Atomic(uint64_t) *s{InitValue};"); 21308 FormatStyle Style = getLLVMStyle(); 21309 Style.PointerAlignment = FormatStyle::PAS_Left; 21310 verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style); 21311 verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style); 21312 verifyFormat("_Atomic(int)* a;", Style); 21313 verifyFormat("_Atomic(int*)* a;", Style); 21314 verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style); 21315 21316 Style.SpacesInCStyleCastParentheses = true; 21317 Style.SpacesInParentheses = false; 21318 verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style); 21319 Style.SpacesInCStyleCastParentheses = false; 21320 Style.SpacesInParentheses = true; 21321 verifyFormat("x = (_Atomic( uint64_t ))*a;", Style); 21322 verifyFormat("x = (_Atomic( uint64_t ))&a;", Style); 21323 } 21324 21325 TEST_F(FormatTest, AmbersandInLamda) { 21326 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899 21327 FormatStyle AlignStyle = getLLVMStyle(); 21328 AlignStyle.PointerAlignment = FormatStyle::PAS_Left; 21329 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle); 21330 AlignStyle.PointerAlignment = FormatStyle::PAS_Right; 21331 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle); 21332 } 21333 21334 TEST_F(FormatTest, SpacesInConditionalStatement) { 21335 FormatStyle Spaces = getLLVMStyle(); 21336 Spaces.IfMacros.clear(); 21337 Spaces.IfMacros.push_back("MYIF"); 21338 Spaces.SpacesInConditionalStatement = true; 21339 verifyFormat("for ( int i = 0; i; i++ )\n continue;", Spaces); 21340 verifyFormat("if ( !a )\n return;", Spaces); 21341 verifyFormat("if ( a )\n return;", Spaces); 21342 verifyFormat("if constexpr ( a )\n return;", Spaces); 21343 verifyFormat("MYIF ( a )\n return;", Spaces); 21344 verifyFormat("MYIF ( a )\n return;\nelse MYIF ( b )\n return;", Spaces); 21345 verifyFormat("MYIF ( a )\n return;\nelse\n return;", Spaces); 21346 verifyFormat("switch ( a )\ncase 1:\n return;", Spaces); 21347 verifyFormat("while ( a )\n return;", Spaces); 21348 verifyFormat("while ( (a && b) )\n return;", Spaces); 21349 verifyFormat("do {\n} while ( 1 != 0 );", Spaces); 21350 verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces); 21351 // Check that space on the left of "::" is inserted as expected at beginning 21352 // of condition. 21353 verifyFormat("while ( ::func() )\n return;", Spaces); 21354 21355 // Check impact of ControlStatementsExceptControlMacros is honored. 21356 Spaces.SpaceBeforeParens = 21357 FormatStyle::SBPO_ControlStatementsExceptControlMacros; 21358 verifyFormat("MYIF( a )\n return;", Spaces); 21359 verifyFormat("MYIF( a )\n return;\nelse MYIF( b )\n return;", Spaces); 21360 verifyFormat("MYIF( a )\n return;\nelse\n return;", Spaces); 21361 } 21362 21363 TEST_F(FormatTest, AlternativeOperators) { 21364 // Test case for ensuring alternate operators are not 21365 // combined with their right most neighbour. 21366 verifyFormat("int a and b;"); 21367 verifyFormat("int a and_eq b;"); 21368 verifyFormat("int a bitand b;"); 21369 verifyFormat("int a bitor b;"); 21370 verifyFormat("int a compl b;"); 21371 verifyFormat("int a not b;"); 21372 verifyFormat("int a not_eq b;"); 21373 verifyFormat("int a or b;"); 21374 verifyFormat("int a xor b;"); 21375 verifyFormat("int a xor_eq b;"); 21376 verifyFormat("return this not_eq bitand other;"); 21377 verifyFormat("bool operator not_eq(const X bitand other)"); 21378 21379 verifyFormat("int a and 5;"); 21380 verifyFormat("int a and_eq 5;"); 21381 verifyFormat("int a bitand 5;"); 21382 verifyFormat("int a bitor 5;"); 21383 verifyFormat("int a compl 5;"); 21384 verifyFormat("int a not 5;"); 21385 verifyFormat("int a not_eq 5;"); 21386 verifyFormat("int a or 5;"); 21387 verifyFormat("int a xor 5;"); 21388 verifyFormat("int a xor_eq 5;"); 21389 21390 verifyFormat("int a compl(5);"); 21391 verifyFormat("int a not(5);"); 21392 21393 /* FIXME handle alternate tokens 21394 * https://en.cppreference.com/w/cpp/language/operator_alternative 21395 // alternative tokens 21396 verifyFormat("compl foo();"); // ~foo(); 21397 verifyFormat("foo() <%%>;"); // foo(); 21398 verifyFormat("void foo() <%%>;"); // void foo(){} 21399 verifyFormat("int a <:1:>;"); // int a[1];[ 21400 verifyFormat("%:define ABC abc"); // #define ABC abc 21401 verifyFormat("%:%:"); // ## 21402 */ 21403 } 21404 21405 TEST_F(FormatTest, STLWhileNotDefineChed) { 21406 verifyFormat("#if defined(while)\n" 21407 "#define while EMIT WARNING C4005\n" 21408 "#endif // while"); 21409 } 21410 21411 TEST_F(FormatTest, OperatorSpacing) { 21412 FormatStyle Style = getLLVMStyle(); 21413 Style.PointerAlignment = FormatStyle::PAS_Right; 21414 verifyFormat("Foo::operator*();", Style); 21415 verifyFormat("Foo::operator void *();", Style); 21416 verifyFormat("Foo::operator void **();", Style); 21417 verifyFormat("Foo::operator void *&();", Style); 21418 verifyFormat("Foo::operator void *&&();", Style); 21419 verifyFormat("Foo::operator void const *();", Style); 21420 verifyFormat("Foo::operator void const **();", Style); 21421 verifyFormat("Foo::operator void const *&();", Style); 21422 verifyFormat("Foo::operator void const *&&();", Style); 21423 verifyFormat("Foo::operator()(void *);", Style); 21424 verifyFormat("Foo::operator*(void *);", Style); 21425 verifyFormat("Foo::operator*();", Style); 21426 verifyFormat("Foo::operator**();", Style); 21427 verifyFormat("Foo::operator&();", Style); 21428 verifyFormat("Foo::operator<int> *();", Style); 21429 verifyFormat("Foo::operator<Foo> *();", Style); 21430 verifyFormat("Foo::operator<int> **();", Style); 21431 verifyFormat("Foo::operator<Foo> **();", Style); 21432 verifyFormat("Foo::operator<int> &();", Style); 21433 verifyFormat("Foo::operator<Foo> &();", Style); 21434 verifyFormat("Foo::operator<int> &&();", Style); 21435 verifyFormat("Foo::operator<Foo> &&();", Style); 21436 verifyFormat("Foo::operator<int> *&();", Style); 21437 verifyFormat("Foo::operator<Foo> *&();", Style); 21438 verifyFormat("Foo::operator<int> *&&();", Style); 21439 verifyFormat("Foo::operator<Foo> *&&();", Style); 21440 verifyFormat("operator*(int (*)(), class Foo);", Style); 21441 21442 verifyFormat("Foo::operator&();", Style); 21443 verifyFormat("Foo::operator void &();", Style); 21444 verifyFormat("Foo::operator void const &();", Style); 21445 verifyFormat("Foo::operator()(void &);", Style); 21446 verifyFormat("Foo::operator&(void &);", Style); 21447 verifyFormat("Foo::operator&();", Style); 21448 verifyFormat("operator&(int (&)(), class Foo);", Style); 21449 21450 verifyFormat("Foo::operator&&();", Style); 21451 verifyFormat("Foo::operator**();", Style); 21452 verifyFormat("Foo::operator void &&();", Style); 21453 verifyFormat("Foo::operator void const &&();", Style); 21454 verifyFormat("Foo::operator()(void &&);", Style); 21455 verifyFormat("Foo::operator&&(void &&);", Style); 21456 verifyFormat("Foo::operator&&();", Style); 21457 verifyFormat("operator&&(int(&&)(), class Foo);", Style); 21458 verifyFormat("operator const nsTArrayRight<E> &()", Style); 21459 verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()", 21460 Style); 21461 verifyFormat("operator void **()", Style); 21462 verifyFormat("operator const FooRight<Object> &()", Style); 21463 verifyFormat("operator const FooRight<Object> *()", Style); 21464 verifyFormat("operator const FooRight<Object> **()", Style); 21465 verifyFormat("operator const FooRight<Object> *&()", Style); 21466 verifyFormat("operator const FooRight<Object> *&&()", Style); 21467 21468 Style.PointerAlignment = FormatStyle::PAS_Left; 21469 verifyFormat("Foo::operator*();", Style); 21470 verifyFormat("Foo::operator**();", Style); 21471 verifyFormat("Foo::operator void*();", Style); 21472 verifyFormat("Foo::operator void**();", Style); 21473 verifyFormat("Foo::operator void*&();", Style); 21474 verifyFormat("Foo::operator void*&&();", Style); 21475 verifyFormat("Foo::operator void const*();", Style); 21476 verifyFormat("Foo::operator void const**();", Style); 21477 verifyFormat("Foo::operator void const*&();", Style); 21478 verifyFormat("Foo::operator void const*&&();", Style); 21479 verifyFormat("Foo::operator/*comment*/ void*();", Style); 21480 verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style); 21481 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style); 21482 verifyFormat("Foo::operator()(void*);", Style); 21483 verifyFormat("Foo::operator*(void*);", Style); 21484 verifyFormat("Foo::operator*();", Style); 21485 verifyFormat("Foo::operator<int>*();", Style); 21486 verifyFormat("Foo::operator<Foo>*();", Style); 21487 verifyFormat("Foo::operator<int>**();", Style); 21488 verifyFormat("Foo::operator<Foo>**();", Style); 21489 verifyFormat("Foo::operator<Foo>*&();", Style); 21490 verifyFormat("Foo::operator<int>&();", Style); 21491 verifyFormat("Foo::operator<Foo>&();", Style); 21492 verifyFormat("Foo::operator<int>&&();", Style); 21493 verifyFormat("Foo::operator<Foo>&&();", Style); 21494 verifyFormat("Foo::operator<int>*&();", Style); 21495 verifyFormat("Foo::operator<Foo>*&();", Style); 21496 verifyFormat("operator*(int (*)(), class Foo);", Style); 21497 21498 verifyFormat("Foo::operator&();", Style); 21499 verifyFormat("Foo::operator void&();", Style); 21500 verifyFormat("Foo::operator void const&();", Style); 21501 verifyFormat("Foo::operator/*comment*/ void&();", Style); 21502 verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style); 21503 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style); 21504 verifyFormat("Foo::operator()(void&);", Style); 21505 verifyFormat("Foo::operator&(void&);", Style); 21506 verifyFormat("Foo::operator&();", Style); 21507 verifyFormat("operator&(int (&)(), class Foo);", Style); 21508 21509 verifyFormat("Foo::operator&&();", Style); 21510 verifyFormat("Foo::operator void&&();", Style); 21511 verifyFormat("Foo::operator void const&&();", Style); 21512 verifyFormat("Foo::operator/*comment*/ void&&();", Style); 21513 verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style); 21514 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style); 21515 verifyFormat("Foo::operator()(void&&);", Style); 21516 verifyFormat("Foo::operator&&(void&&);", Style); 21517 verifyFormat("Foo::operator&&();", Style); 21518 verifyFormat("operator&&(int(&&)(), class Foo);", Style); 21519 verifyFormat("operator const nsTArrayLeft<E>&()", Style); 21520 verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()", 21521 Style); 21522 verifyFormat("operator void**()", Style); 21523 verifyFormat("operator const FooLeft<Object>&()", Style); 21524 verifyFormat("operator const FooLeft<Object>*()", Style); 21525 verifyFormat("operator const FooLeft<Object>**()", Style); 21526 verifyFormat("operator const FooLeft<Object>*&()", Style); 21527 verifyFormat("operator const FooLeft<Object>*&&()", Style); 21528 21529 // PR45107 21530 verifyFormat("operator Vector<String>&();", Style); 21531 verifyFormat("operator const Vector<String>&();", Style); 21532 verifyFormat("operator foo::Bar*();", Style); 21533 verifyFormat("operator const Foo<X>::Bar<Y>*();", Style); 21534 verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();", 21535 Style); 21536 21537 Style.PointerAlignment = FormatStyle::PAS_Middle; 21538 verifyFormat("Foo::operator*();", Style); 21539 verifyFormat("Foo::operator void *();", Style); 21540 verifyFormat("Foo::operator()(void *);", Style); 21541 verifyFormat("Foo::operator*(void *);", Style); 21542 verifyFormat("Foo::operator*();", Style); 21543 verifyFormat("operator*(int (*)(), class Foo);", Style); 21544 21545 verifyFormat("Foo::operator&();", Style); 21546 verifyFormat("Foo::operator void &();", Style); 21547 verifyFormat("Foo::operator void const &();", Style); 21548 verifyFormat("Foo::operator()(void &);", Style); 21549 verifyFormat("Foo::operator&(void &);", Style); 21550 verifyFormat("Foo::operator&();", Style); 21551 verifyFormat("operator&(int (&)(), class Foo);", Style); 21552 21553 verifyFormat("Foo::operator&&();", Style); 21554 verifyFormat("Foo::operator void &&();", Style); 21555 verifyFormat("Foo::operator void const &&();", Style); 21556 verifyFormat("Foo::operator()(void &&);", Style); 21557 verifyFormat("Foo::operator&&(void &&);", Style); 21558 verifyFormat("Foo::operator&&();", Style); 21559 verifyFormat("operator&&(int(&&)(), class Foo);", Style); 21560 } 21561 21562 TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) { 21563 FormatStyle Style = getLLVMStyle(); 21564 // PR46157 21565 verifyFormat("foo(operator+, -42);", Style); 21566 verifyFormat("foo(operator++, -42);", Style); 21567 verifyFormat("foo(operator--, -42);", Style); 21568 verifyFormat("foo(-42, operator--);", Style); 21569 verifyFormat("foo(-42, operator, );", Style); 21570 verifyFormat("foo(operator, , -42);", Style); 21571 } 21572 21573 TEST_F(FormatTest, WhitespaceSensitiveMacros) { 21574 FormatStyle Style = getLLVMStyle(); 21575 Style.WhitespaceSensitiveMacros.push_back("FOO"); 21576 21577 // Don't use the helpers here, since 'mess up' will change the whitespace 21578 // and these are all whitespace sensitive by definition 21579 EXPECT_EQ("FOO(String-ized&Messy+But(: :Still)=Intentional);", 21580 format("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style)); 21581 EXPECT_EQ( 21582 "FOO(String-ized&Messy+But\\(: :Still)=Intentional);", 21583 format("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style)); 21584 EXPECT_EQ("FOO(String-ized&Messy+But,: :Still=Intentional);", 21585 format("FOO(String-ized&Messy+But,: :Still=Intentional);", Style)); 21586 EXPECT_EQ("FOO(String-ized&Messy+But,: :\n" 21587 " Still=Intentional);", 21588 format("FOO(String-ized&Messy+But,: :\n" 21589 " Still=Intentional);", 21590 Style)); 21591 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 21592 EXPECT_EQ("FOO(String-ized=&Messy+But,: :\n" 21593 " Still=Intentional);", 21594 format("FOO(String-ized=&Messy+But,: :\n" 21595 " Still=Intentional);", 21596 Style)); 21597 21598 Style.ColumnLimit = 21; 21599 EXPECT_EQ("FOO(String-ized&Messy+But: :Still=Intentional);", 21600 format("FOO(String-ized&Messy+But: :Still=Intentional);", Style)); 21601 } 21602 21603 TEST_F(FormatTest, VeryLongNamespaceCommentSplit) { 21604 // These tests are not in NamespaceFixer because that doesn't 21605 // test its interaction with line wrapping 21606 FormatStyle Style = getLLVMStyle(); 21607 Style.ColumnLimit = 80; 21608 verifyFormat("namespace {\n" 21609 "int i;\n" 21610 "int j;\n" 21611 "} // namespace", 21612 Style); 21613 21614 verifyFormat("namespace AAA {\n" 21615 "int i;\n" 21616 "int j;\n" 21617 "} // namespace AAA", 21618 Style); 21619 21620 EXPECT_EQ("namespace Averyveryveryverylongnamespace {\n" 21621 "int i;\n" 21622 "int j;\n" 21623 "} // namespace Averyveryveryverylongnamespace", 21624 format("namespace Averyveryveryverylongnamespace {\n" 21625 "int i;\n" 21626 "int j;\n" 21627 "}", 21628 Style)); 21629 21630 EXPECT_EQ( 21631 "namespace " 21632 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" 21633 " went::mad::now {\n" 21634 "int i;\n" 21635 "int j;\n" 21636 "} // namespace\n" 21637 " // " 21638 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" 21639 "went::mad::now", 21640 format("namespace " 21641 "would::it::save::you::a::lot::of::time::if_::i::" 21642 "just::gave::up::and_::went::mad::now {\n" 21643 "int i;\n" 21644 "int j;\n" 21645 "}", 21646 Style)); 21647 21648 // This used to duplicate the comment again and again on subsequent runs 21649 EXPECT_EQ( 21650 "namespace " 21651 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" 21652 " went::mad::now {\n" 21653 "int i;\n" 21654 "int j;\n" 21655 "} // namespace\n" 21656 " // " 21657 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" 21658 "went::mad::now", 21659 format("namespace " 21660 "would::it::save::you::a::lot::of::time::if_::i::" 21661 "just::gave::up::and_::went::mad::now {\n" 21662 "int i;\n" 21663 "int j;\n" 21664 "} // namespace\n" 21665 " // " 21666 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::" 21667 "and_::went::mad::now", 21668 Style)); 21669 } 21670 21671 TEST_F(FormatTest, LikelyUnlikely) { 21672 FormatStyle Style = getLLVMStyle(); 21673 21674 verifyFormat("if (argc > 5) [[unlikely]] {\n" 21675 " return 29;\n" 21676 "}", 21677 Style); 21678 21679 verifyFormat("if (argc > 5) [[likely]] {\n" 21680 " return 29;\n" 21681 "}", 21682 Style); 21683 21684 verifyFormat("if (argc > 5) [[unlikely]] {\n" 21685 " return 29;\n" 21686 "} else [[likely]] {\n" 21687 " return 42;\n" 21688 "}\n", 21689 Style); 21690 21691 verifyFormat("if (argc > 5) [[unlikely]] {\n" 21692 " return 29;\n" 21693 "} else if (argc > 10) [[likely]] {\n" 21694 " return 99;\n" 21695 "} else {\n" 21696 " return 42;\n" 21697 "}\n", 21698 Style); 21699 21700 verifyFormat("if (argc > 5) [[gnu::unused]] {\n" 21701 " return 29;\n" 21702 "}", 21703 Style); 21704 } 21705 21706 TEST_F(FormatTest, PenaltyIndentedWhitespace) { 21707 verifyFormat("Constructor()\n" 21708 " : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 21709 " aaaa(aaaaaaaaaaaaaaaaaa, " 21710 "aaaaaaaaaaaaaaaaaat))"); 21711 verifyFormat("Constructor()\n" 21712 " : aaaaaaaaaaaaa(aaaaaa), " 21713 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)"); 21714 21715 FormatStyle StyleWithWhitespacePenalty = getLLVMStyle(); 21716 StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5; 21717 verifyFormat("Constructor()\n" 21718 " : aaaaaa(aaaaaa),\n" 21719 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 21720 " aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))", 21721 StyleWithWhitespacePenalty); 21722 verifyFormat("Constructor()\n" 21723 " : aaaaaaaaaaaaa(aaaaaa), " 21724 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)", 21725 StyleWithWhitespacePenalty); 21726 } 21727 21728 TEST_F(FormatTest, LLVMDefaultStyle) { 21729 FormatStyle Style = getLLVMStyle(); 21730 verifyFormat("extern \"C\" {\n" 21731 "int foo();\n" 21732 "}", 21733 Style); 21734 } 21735 TEST_F(FormatTest, GNUDefaultStyle) { 21736 FormatStyle Style = getGNUStyle(); 21737 verifyFormat("extern \"C\"\n" 21738 "{\n" 21739 " int foo ();\n" 21740 "}", 21741 Style); 21742 } 21743 TEST_F(FormatTest, MozillaDefaultStyle) { 21744 FormatStyle Style = getMozillaStyle(); 21745 verifyFormat("extern \"C\"\n" 21746 "{\n" 21747 " int foo();\n" 21748 "}", 21749 Style); 21750 } 21751 TEST_F(FormatTest, GoogleDefaultStyle) { 21752 FormatStyle Style = getGoogleStyle(); 21753 verifyFormat("extern \"C\" {\n" 21754 "int foo();\n" 21755 "}", 21756 Style); 21757 } 21758 TEST_F(FormatTest, ChromiumDefaultStyle) { 21759 FormatStyle Style = getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp); 21760 verifyFormat("extern \"C\" {\n" 21761 "int foo();\n" 21762 "}", 21763 Style); 21764 } 21765 TEST_F(FormatTest, MicrosoftDefaultStyle) { 21766 FormatStyle Style = getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp); 21767 verifyFormat("extern \"C\"\n" 21768 "{\n" 21769 " int foo();\n" 21770 "}", 21771 Style); 21772 } 21773 TEST_F(FormatTest, WebKitDefaultStyle) { 21774 FormatStyle Style = getWebKitStyle(); 21775 verifyFormat("extern \"C\" {\n" 21776 "int foo();\n" 21777 "}", 21778 Style); 21779 } 21780 21781 TEST_F(FormatTest, ConceptsAndRequires) { 21782 FormatStyle Style = getLLVMStyle(); 21783 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 21784 21785 verifyFormat("template <typename T>\n" 21786 "concept Hashable = requires(T a) {\n" 21787 " { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n" 21788 "};", 21789 Style); 21790 verifyFormat("template <typename T>\n" 21791 "concept EqualityComparable = requires(T a, T b) {\n" 21792 " { a == b } -> bool;\n" 21793 "};", 21794 Style); 21795 verifyFormat("template <typename T>\n" 21796 "concept EqualityComparable = requires(T a, T b) {\n" 21797 " { a == b } -> bool;\n" 21798 " { a != b } -> bool;\n" 21799 "};", 21800 Style); 21801 verifyFormat("template <typename T>\n" 21802 "concept EqualityComparable = requires(T a, T b) {\n" 21803 " { a == b } -> bool;\n" 21804 " { a != b } -> bool;\n" 21805 "};", 21806 Style); 21807 21808 verifyFormat("template <typename It>\n" 21809 "requires Iterator<It>\n" 21810 "void sort(It begin, It end) {\n" 21811 " //....\n" 21812 "}", 21813 Style); 21814 21815 verifyFormat("template <typename T>\n" 21816 "concept Large = sizeof(T) > 10;", 21817 Style); 21818 21819 verifyFormat("template <typename T, typename U>\n" 21820 "concept FooableWith = requires(T t, U u) {\n" 21821 " typename T::foo_type;\n" 21822 " { t.foo(u) } -> typename T::foo_type;\n" 21823 " t++;\n" 21824 "};\n" 21825 "void doFoo(FooableWith<int> auto t) {\n" 21826 " t.foo(3);\n" 21827 "}", 21828 Style); 21829 verifyFormat("template <typename T>\n" 21830 "concept Context = sizeof(T) == 1;", 21831 Style); 21832 verifyFormat("template <typename T>\n" 21833 "concept Context = is_specialization_of_v<context, T>;", 21834 Style); 21835 verifyFormat("template <typename T>\n" 21836 "concept Node = std::is_object_v<T>;", 21837 Style); 21838 verifyFormat("template <typename T>\n" 21839 "concept Tree = true;", 21840 Style); 21841 21842 verifyFormat("template <typename T> int g(T i) requires Concept1<I> {\n" 21843 " //...\n" 21844 "}", 21845 Style); 21846 21847 verifyFormat( 21848 "template <typename T> int g(T i) requires Concept1<I> && Concept2<I> {\n" 21849 " //...\n" 21850 "}", 21851 Style); 21852 21853 verifyFormat( 21854 "template <typename T> int g(T i) requires Concept1<I> || Concept2<I> {\n" 21855 " //...\n" 21856 "}", 21857 Style); 21858 21859 verifyFormat("template <typename T>\n" 21860 "veryveryvery_long_return_type g(T i) requires Concept1<I> || " 21861 "Concept2<I> {\n" 21862 " //...\n" 21863 "}", 21864 Style); 21865 21866 verifyFormat("template <typename T>\n" 21867 "veryveryvery_long_return_type g(T i) requires Concept1<I> && " 21868 "Concept2<I> {\n" 21869 " //...\n" 21870 "}", 21871 Style); 21872 21873 verifyFormat( 21874 "template <typename T>\n" 21875 "veryveryvery_long_return_type g(T i) requires Concept1 && Concept2 {\n" 21876 " //...\n" 21877 "}", 21878 Style); 21879 21880 verifyFormat( 21881 "template <typename T>\n" 21882 "veryveryvery_long_return_type g(T i) requires Concept1 || Concept2 {\n" 21883 " //...\n" 21884 "}", 21885 Style); 21886 21887 verifyFormat("template <typename It>\n" 21888 "requires Foo<It>() && Bar<It> {\n" 21889 " //....\n" 21890 "}", 21891 Style); 21892 21893 verifyFormat("template <typename It>\n" 21894 "requires Foo<Bar<It>>() && Bar<Foo<It, It>> {\n" 21895 " //....\n" 21896 "}", 21897 Style); 21898 21899 verifyFormat("template <typename It>\n" 21900 "requires Foo<Bar<It, It>>() && Bar<Foo<It, It>> {\n" 21901 " //....\n" 21902 "}", 21903 Style); 21904 21905 verifyFormat( 21906 "template <typename It>\n" 21907 "requires Foo<Bar<It>, Baz<It>>() && Bar<Foo<It>, Baz<It, It>> {\n" 21908 " //....\n" 21909 "}", 21910 Style); 21911 21912 Style.IndentRequires = true; 21913 verifyFormat("template <typename It>\n" 21914 " requires Iterator<It>\n" 21915 "void sort(It begin, It end) {\n" 21916 " //....\n" 21917 "}", 21918 Style); 21919 verifyFormat("template <std::size index_>\n" 21920 " requires(index_ < sizeof...(Children_))\n" 21921 "Tree auto &child() {\n" 21922 " // ...\n" 21923 "}", 21924 Style); 21925 21926 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 21927 verifyFormat("template <typename T>\n" 21928 "concept Hashable = requires (T a) {\n" 21929 " { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n" 21930 "};", 21931 Style); 21932 21933 verifyFormat("template <class T = void>\n" 21934 " requires EqualityComparable<T> || Same<T, void>\n" 21935 "struct equal_to;", 21936 Style); 21937 21938 verifyFormat("template <class T>\n" 21939 " requires requires {\n" 21940 " T{};\n" 21941 " T (int);\n" 21942 " }\n", 21943 Style); 21944 21945 Style.ColumnLimit = 78; 21946 verifyFormat("template <typename T>\n" 21947 "concept Context = Traits<typename T::traits_type> and\n" 21948 " Interface<typename T::interface_type> and\n" 21949 " Request<typename T::request_type> and\n" 21950 " Response<typename T::response_type> and\n" 21951 " ContextExtension<typename T::extension_type> and\n" 21952 " ::std::is_copy_constructable<T> and " 21953 "::std::is_move_constructable<T> and\n" 21954 " requires (T c) {\n" 21955 " { c.response; } -> Response;\n" 21956 "} and requires (T c) {\n" 21957 " { c.request; } -> Request;\n" 21958 "}\n", 21959 Style); 21960 21961 verifyFormat("template <typename T>\n" 21962 "concept Context = Traits<typename T::traits_type> or\n" 21963 " Interface<typename T::interface_type> or\n" 21964 " Request<typename T::request_type> or\n" 21965 " Response<typename T::response_type> or\n" 21966 " ContextExtension<typename T::extension_type> or\n" 21967 " ::std::is_copy_constructable<T> or " 21968 "::std::is_move_constructable<T> or\n" 21969 " requires (T c) {\n" 21970 " { c.response; } -> Response;\n" 21971 "} or requires (T c) {\n" 21972 " { c.request; } -> Request;\n" 21973 "}\n", 21974 Style); 21975 21976 verifyFormat("template <typename T>\n" 21977 "concept Context = Traits<typename T::traits_type> &&\n" 21978 " Interface<typename T::interface_type> &&\n" 21979 " Request<typename T::request_type> &&\n" 21980 " Response<typename T::response_type> &&\n" 21981 " ContextExtension<typename T::extension_type> &&\n" 21982 " ::std::is_copy_constructable<T> && " 21983 "::std::is_move_constructable<T> &&\n" 21984 " requires (T c) {\n" 21985 " { c.response; } -> Response;\n" 21986 "} && requires (T c) {\n" 21987 " { c.request; } -> Request;\n" 21988 "}\n", 21989 Style); 21990 21991 verifyFormat("template <typename T>\nconcept someConcept = Constraint1<T> && " 21992 "Constraint2<T>;"); 21993 21994 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 21995 Style.BraceWrapping.AfterFunction = true; 21996 Style.BraceWrapping.AfterClass = true; 21997 Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 21998 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 21999 verifyFormat("void Foo () requires (std::copyable<T>)\n" 22000 "{\n" 22001 " return\n" 22002 "}\n", 22003 Style); 22004 22005 verifyFormat("void Foo () requires std::copyable<T>\n" 22006 "{\n" 22007 " return\n" 22008 "}\n", 22009 Style); 22010 22011 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 22012 " requires (std::invocable<F, std::invoke_result_t<Args>...>)\n" 22013 "struct constant;", 22014 Style); 22015 22016 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 22017 " requires std::invocable<F, std::invoke_result_t<Args>...>\n" 22018 "struct constant;", 22019 Style); 22020 22021 verifyFormat("template <class T>\n" 22022 "class plane_with_very_very_very_long_name\n" 22023 "{\n" 22024 " constexpr plane_with_very_very_very_long_name () requires " 22025 "std::copyable<T>\n" 22026 " : plane_with_very_very_very_long_name (1)\n" 22027 " {\n" 22028 " }\n" 22029 "}\n", 22030 Style); 22031 22032 verifyFormat("template <class T>\n" 22033 "class plane_with_long_name\n" 22034 "{\n" 22035 " constexpr plane_with_long_name () requires std::copyable<T>\n" 22036 " : plane_with_long_name (1)\n" 22037 " {\n" 22038 " }\n" 22039 "}\n", 22040 Style); 22041 22042 Style.BreakBeforeConceptDeclarations = false; 22043 verifyFormat("template <typename T> concept Tree = true;", Style); 22044 22045 Style.IndentRequires = false; 22046 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 22047 "requires (std::invocable<F, std::invoke_result_t<Args>...>) " 22048 "struct constant;", 22049 Style); 22050 } 22051 22052 TEST_F(FormatTest, StatementAttributeLikeMacros) { 22053 FormatStyle Style = getLLVMStyle(); 22054 StringRef Source = "void Foo::slot() {\n" 22055 " unsigned char MyChar = 'x';\n" 22056 " emit signal(MyChar);\n" 22057 " Q_EMIT signal(MyChar);\n" 22058 "}"; 22059 22060 EXPECT_EQ(Source, format(Source, Style)); 22061 22062 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 22063 EXPECT_EQ("void Foo::slot() {\n" 22064 " unsigned char MyChar = 'x';\n" 22065 " emit signal(MyChar);\n" 22066 " Q_EMIT signal(MyChar);\n" 22067 "}", 22068 format(Source, Style)); 22069 22070 Style.StatementAttributeLikeMacros.push_back("emit"); 22071 EXPECT_EQ(Source, format(Source, Style)); 22072 22073 Style.StatementAttributeLikeMacros = {}; 22074 EXPECT_EQ("void Foo::slot() {\n" 22075 " unsigned char MyChar = 'x';\n" 22076 " emit signal(MyChar);\n" 22077 " Q_EMIT signal(MyChar);\n" 22078 "}", 22079 format(Source, Style)); 22080 } 22081 22082 TEST_F(FormatTest, IndentAccessModifiers) { 22083 FormatStyle Style = getLLVMStyle(); 22084 Style.IndentAccessModifiers = true; 22085 // Members are *two* levels below the record; 22086 // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation. 22087 verifyFormat("class C {\n" 22088 " int i;\n" 22089 "};\n", 22090 Style); 22091 verifyFormat("union C {\n" 22092 " int i;\n" 22093 " unsigned u;\n" 22094 "};\n", 22095 Style); 22096 // Access modifiers should be indented one level below the record. 22097 verifyFormat("class C {\n" 22098 " public:\n" 22099 " int i;\n" 22100 "};\n", 22101 Style); 22102 verifyFormat("struct S {\n" 22103 " private:\n" 22104 " class C {\n" 22105 " int j;\n" 22106 "\n" 22107 " public:\n" 22108 " C();\n" 22109 " };\n" 22110 "\n" 22111 " public:\n" 22112 " int i;\n" 22113 "};\n", 22114 Style); 22115 // Enumerations are not records and should be unaffected. 22116 Style.AllowShortEnumsOnASingleLine = false; 22117 verifyFormat("enum class E\n" 22118 "{\n" 22119 " A,\n" 22120 " B\n" 22121 "};\n", 22122 Style); 22123 // Test with a different indentation width; 22124 // also proves that the result is Style.AccessModifierOffset agnostic. 22125 Style.IndentWidth = 3; 22126 verifyFormat("class C {\n" 22127 " public:\n" 22128 " int i;\n" 22129 "};\n", 22130 Style); 22131 } 22132 22133 TEST_F(FormatTest, LimitlessStringsAndComments) { 22134 auto Style = getLLVMStyleWithColumns(0); 22135 constexpr StringRef Code = 22136 "/**\n" 22137 " * This is a multiline comment with quite some long lines, at least for " 22138 "the LLVM Style.\n" 22139 " * We will redo this with strings and line comments. Just to check if " 22140 "everything is working.\n" 22141 " */\n" 22142 "bool foo() {\n" 22143 " /* Single line multi line comment. */\n" 22144 " const std::string String = \"This is a multiline string with quite " 22145 "some long lines, at least for the LLVM Style.\"\n" 22146 " \"We already did it with multi line " 22147 "comments, and we will do it with line comments. Just to check if " 22148 "everything is working.\";\n" 22149 " // This is a line comment (block) with quite some long lines, at " 22150 "least for the LLVM Style.\n" 22151 " // We already did this with multi line comments and strings. Just to " 22152 "check if everything is working.\n" 22153 " const std::string SmallString = \"Hello World\";\n" 22154 " // Small line comment\n" 22155 " return String.size() > SmallString.size();\n" 22156 "}"; 22157 EXPECT_EQ(Code, format(Code, Style)); 22158 } 22159 } // namespace 22160 } // namespace format 22161 } // namespace clang 22162