1 //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "clang/Format/Format.h" 10 11 #include "../Tooling/ReplacementTest.h" 12 #include "FormatTestUtils.h" 13 14 #include "llvm/Support/Debug.h" 15 #include "llvm/Support/MemoryBuffer.h" 16 #include "gtest/gtest.h" 17 18 #define DEBUG_TYPE "format-test" 19 20 using clang::tooling::ReplacementTest; 21 using clang::tooling::toReplacements; 22 using testing::ScopedTrace; 23 24 namespace clang { 25 namespace format { 26 namespace { 27 28 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); } 29 30 class FormatTest : public ::testing::Test { 31 protected: 32 enum StatusCheck { SC_ExpectComplete, SC_ExpectIncomplete, SC_DoNotCheck }; 33 34 std::string format(llvm::StringRef Code, 35 const FormatStyle &Style = getLLVMStyle(), 36 StatusCheck CheckComplete = SC_ExpectComplete) { 37 LLVM_DEBUG(llvm::errs() << "---\n"); 38 LLVM_DEBUG(llvm::errs() << Code << "\n\n"); 39 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size())); 40 FormattingAttemptStatus Status; 41 tooling::Replacements Replaces = 42 reformat(Style, Code, Ranges, "<stdin>", &Status); 43 if (CheckComplete != SC_DoNotCheck) { 44 bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete; 45 EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete) 46 << Code << "\n\n"; 47 } 48 ReplacementCount = Replaces.size(); 49 auto Result = applyAllReplacements(Code, Replaces); 50 EXPECT_TRUE(static_cast<bool>(Result)); 51 LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n"); 52 return *Result; 53 } 54 55 FormatStyle getStyleWithColumns(FormatStyle Style, unsigned ColumnLimit) { 56 Style.ColumnLimit = ColumnLimit; 57 return Style; 58 } 59 60 FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { 61 return getStyleWithColumns(getLLVMStyle(), ColumnLimit); 62 } 63 64 FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) { 65 return getStyleWithColumns(getGoogleStyle(), ColumnLimit); 66 } 67 68 void _verifyFormat(const char *File, int Line, llvm::StringRef Expected, 69 llvm::StringRef Code, 70 const FormatStyle &Style = getLLVMStyle()) { 71 ScopedTrace t(File, Line, ::testing::Message() << Code.str()); 72 EXPECT_EQ(Expected.str(), format(Expected, Style)) 73 << "Expected code is not stable"; 74 EXPECT_EQ(Expected.str(), format(Code, Style)); 75 if (Style.Language == FormatStyle::LK_Cpp) { 76 // Objective-C++ is a superset of C++, so everything checked for C++ 77 // needs to be checked for Objective-C++ as well. 78 FormatStyle ObjCStyle = Style; 79 ObjCStyle.Language = FormatStyle::LK_ObjC; 80 EXPECT_EQ(Expected.str(), format(test::messUp(Code), ObjCStyle)); 81 } 82 } 83 84 void _verifyFormat(const char *File, int Line, llvm::StringRef Code, 85 const FormatStyle &Style = getLLVMStyle()) { 86 _verifyFormat(File, Line, Code, test::messUp(Code), Style); 87 } 88 89 void _verifyIncompleteFormat(const char *File, int Line, llvm::StringRef Code, 90 const FormatStyle &Style = getLLVMStyle()) { 91 ScopedTrace t(File, Line, ::testing::Message() << Code.str()); 92 EXPECT_EQ(Code.str(), 93 format(test::messUp(Code), Style, SC_ExpectIncomplete)); 94 } 95 96 void _verifyIndependentOfContext(const char *File, int Line, 97 llvm::StringRef Text, 98 const FormatStyle &Style = getLLVMStyle()) { 99 _verifyFormat(File, Line, Text, Style); 100 _verifyFormat(File, Line, llvm::Twine("void f() { " + Text + " }").str(), 101 Style); 102 } 103 104 /// \brief Verify that clang-format does not crash on the given input. 105 void verifyNoCrash(llvm::StringRef Code, 106 const FormatStyle &Style = getLLVMStyle()) { 107 format(Code, Style, SC_DoNotCheck); 108 } 109 110 int ReplacementCount; 111 }; 112 113 #define verifyIndependentOfContext(...) \ 114 _verifyIndependentOfContext(__FILE__, __LINE__, __VA_ARGS__) 115 #define verifyIncompleteFormat(...) \ 116 _verifyIncompleteFormat(__FILE__, __LINE__, __VA_ARGS__) 117 #define verifyFormat(...) _verifyFormat(__FILE__, __LINE__, __VA_ARGS__) 118 #define verifyGoogleFormat(Code) verifyFormat(Code, getGoogleStyle()) 119 120 TEST_F(FormatTest, MessUp) { 121 EXPECT_EQ("1 2 3", test::messUp("1 2 3")); 122 EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n")); 123 EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc")); 124 EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc")); 125 EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne")); 126 } 127 128 TEST_F(FormatTest, DefaultLLVMStyleIsCpp) { 129 EXPECT_EQ(FormatStyle::LK_Cpp, getLLVMStyle().Language); 130 } 131 132 TEST_F(FormatTest, LLVMStyleOverride) { 133 EXPECT_EQ(FormatStyle::LK_Proto, 134 getLLVMStyle(FormatStyle::LK_Proto).Language); 135 } 136 137 //===----------------------------------------------------------------------===// 138 // Basic function tests. 139 //===----------------------------------------------------------------------===// 140 141 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) { 142 EXPECT_EQ(";", format(";")); 143 } 144 145 TEST_F(FormatTest, FormatsGlobalStatementsAt0) { 146 EXPECT_EQ("int i;", format(" int i;")); 147 EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;")); 148 EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); 149 EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); 150 } 151 152 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { 153 EXPECT_EQ("int i;", format("int\ni;")); 154 } 155 156 TEST_F(FormatTest, FormatsNestedBlockStatements) { 157 EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}")); 158 } 159 160 TEST_F(FormatTest, FormatsNestedCall) { 161 verifyFormat("Method(f1, f2(f3));"); 162 verifyFormat("Method(f1(f2, f3()));"); 163 verifyFormat("Method(f1(f2, (f3())));"); 164 } 165 166 TEST_F(FormatTest, NestedNameSpecifiers) { 167 verifyFormat("vector<::Type> v;"); 168 verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())"); 169 verifyFormat("static constexpr bool Bar = decltype(bar())::value;"); 170 verifyFormat("static constexpr bool Bar = typeof(bar())::value;"); 171 verifyFormat("static constexpr bool Bar = __underlying_type(bar())::value;"); 172 verifyFormat("static constexpr bool Bar = _Atomic(bar())::value;"); 173 verifyFormat("bool a = 2 < ::SomeFunction();"); 174 verifyFormat("ALWAYS_INLINE ::std::string getName();"); 175 verifyFormat("some::string getName();"); 176 } 177 178 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) { 179 EXPECT_EQ("if (a) {\n" 180 " f();\n" 181 "}", 182 format("if(a){f();}")); 183 EXPECT_EQ(4, ReplacementCount); 184 EXPECT_EQ("if (a) {\n" 185 " f();\n" 186 "}", 187 format("if (a) {\n" 188 " f();\n" 189 "}")); 190 EXPECT_EQ(0, ReplacementCount); 191 EXPECT_EQ("/*\r\n" 192 "\r\n" 193 "*/\r\n", 194 format("/*\r\n" 195 "\r\n" 196 "*/\r\n")); 197 EXPECT_EQ(0, ReplacementCount); 198 } 199 200 TEST_F(FormatTest, RemovesEmptyLines) { 201 EXPECT_EQ("class C {\n" 202 " int i;\n" 203 "};", 204 format("class C {\n" 205 " int i;\n" 206 "\n" 207 "};")); 208 209 // Don't remove empty lines at the start of namespaces or extern "C" blocks. 210 EXPECT_EQ("namespace N {\n" 211 "\n" 212 "int i;\n" 213 "}", 214 format("namespace N {\n" 215 "\n" 216 "int i;\n" 217 "}", 218 getGoogleStyle())); 219 EXPECT_EQ("/* something */ namespace N {\n" 220 "\n" 221 "int i;\n" 222 "}", 223 format("/* something */ namespace N {\n" 224 "\n" 225 "int i;\n" 226 "}", 227 getGoogleStyle())); 228 EXPECT_EQ("inline namespace N {\n" 229 "\n" 230 "int i;\n" 231 "}", 232 format("inline namespace N {\n" 233 "\n" 234 "int i;\n" 235 "}", 236 getGoogleStyle())); 237 EXPECT_EQ("/* something */ inline namespace N {\n" 238 "\n" 239 "int i;\n" 240 "}", 241 format("/* something */ inline namespace N {\n" 242 "\n" 243 "int i;\n" 244 "}", 245 getGoogleStyle())); 246 EXPECT_EQ("export namespace N {\n" 247 "\n" 248 "int i;\n" 249 "}", 250 format("export namespace N {\n" 251 "\n" 252 "int i;\n" 253 "}", 254 getGoogleStyle())); 255 EXPECT_EQ("extern /**/ \"C\" /**/ {\n" 256 "\n" 257 "int i;\n" 258 "}", 259 format("extern /**/ \"C\" /**/ {\n" 260 "\n" 261 "int i;\n" 262 "}", 263 getGoogleStyle())); 264 265 auto CustomStyle = clang::format::getLLVMStyle(); 266 CustomStyle.BreakBeforeBraces = clang::format::FormatStyle::BS_Custom; 267 CustomStyle.BraceWrapping.AfterNamespace = true; 268 CustomStyle.KeepEmptyLinesAtTheStartOfBlocks = false; 269 EXPECT_EQ("namespace N\n" 270 "{\n" 271 "\n" 272 "int i;\n" 273 "}", 274 format("namespace N\n" 275 "{\n" 276 "\n" 277 "\n" 278 "int i;\n" 279 "}", 280 CustomStyle)); 281 EXPECT_EQ("/* something */ namespace N\n" 282 "{\n" 283 "\n" 284 "int i;\n" 285 "}", 286 format("/* something */ namespace N {\n" 287 "\n" 288 "\n" 289 "int i;\n" 290 "}", 291 CustomStyle)); 292 EXPECT_EQ("inline namespace N\n" 293 "{\n" 294 "\n" 295 "int i;\n" 296 "}", 297 format("inline namespace N\n" 298 "{\n" 299 "\n" 300 "\n" 301 "int i;\n" 302 "}", 303 CustomStyle)); 304 EXPECT_EQ("/* something */ inline namespace N\n" 305 "{\n" 306 "\n" 307 "int i;\n" 308 "}", 309 format("/* something */ inline namespace N\n" 310 "{\n" 311 "\n" 312 "int i;\n" 313 "}", 314 CustomStyle)); 315 EXPECT_EQ("export namespace N\n" 316 "{\n" 317 "\n" 318 "int i;\n" 319 "}", 320 format("export namespace N\n" 321 "{\n" 322 "\n" 323 "int i;\n" 324 "}", 325 CustomStyle)); 326 EXPECT_EQ("namespace a\n" 327 "{\n" 328 "namespace b\n" 329 "{\n" 330 "\n" 331 "class AA {};\n" 332 "\n" 333 "} // namespace b\n" 334 "} // namespace a\n", 335 format("namespace a\n" 336 "{\n" 337 "namespace b\n" 338 "{\n" 339 "\n" 340 "\n" 341 "class AA {};\n" 342 "\n" 343 "\n" 344 "}\n" 345 "}\n", 346 CustomStyle)); 347 EXPECT_EQ("namespace A /* comment */\n" 348 "{\n" 349 "class B {}\n" 350 "} // namespace A", 351 format("namespace A /* comment */ { class B {} }", CustomStyle)); 352 EXPECT_EQ("namespace A\n" 353 "{ /* comment */\n" 354 "class B {}\n" 355 "} // namespace A", 356 format("namespace A {/* comment */ class B {} }", CustomStyle)); 357 EXPECT_EQ("namespace A\n" 358 "{ /* comment */\n" 359 "\n" 360 "class B {}\n" 361 "\n" 362 "" 363 "} // namespace A", 364 format("namespace A { /* comment */\n" 365 "\n" 366 "\n" 367 "class B {}\n" 368 "\n" 369 "\n" 370 "}", 371 CustomStyle)); 372 EXPECT_EQ("namespace A /* comment */\n" 373 "{\n" 374 "\n" 375 "class B {}\n" 376 "\n" 377 "} // namespace A", 378 format("namespace A/* comment */ {\n" 379 "\n" 380 "\n" 381 "class B {}\n" 382 "\n" 383 "\n" 384 "}", 385 CustomStyle)); 386 387 // ...but do keep inlining and removing empty lines for non-block extern "C" 388 // functions. 389 verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle()); 390 EXPECT_EQ("extern \"C\" int f() {\n" 391 " int i = 42;\n" 392 " return i;\n" 393 "}", 394 format("extern \"C\" int f() {\n" 395 "\n" 396 " int i = 42;\n" 397 " return i;\n" 398 "}", 399 getGoogleStyle())); 400 401 // Remove empty lines at the beginning and end of blocks. 402 EXPECT_EQ("void f() {\n" 403 "\n" 404 " if (a) {\n" 405 "\n" 406 " f();\n" 407 " }\n" 408 "}", 409 format("void f() {\n" 410 "\n" 411 " if (a) {\n" 412 "\n" 413 " f();\n" 414 "\n" 415 " }\n" 416 "\n" 417 "}", 418 getLLVMStyle())); 419 EXPECT_EQ("void f() {\n" 420 " if (a) {\n" 421 " f();\n" 422 " }\n" 423 "}", 424 format("void f() {\n" 425 "\n" 426 " if (a) {\n" 427 "\n" 428 " f();\n" 429 "\n" 430 " }\n" 431 "\n" 432 "}", 433 getGoogleStyle())); 434 435 // Don't remove empty lines in more complex control statements. 436 EXPECT_EQ("void f() {\n" 437 " if (a) {\n" 438 " f();\n" 439 "\n" 440 " } else if (b) {\n" 441 " f();\n" 442 " }\n" 443 "}", 444 format("void f() {\n" 445 " if (a) {\n" 446 " f();\n" 447 "\n" 448 " } else if (b) {\n" 449 " f();\n" 450 "\n" 451 " }\n" 452 "\n" 453 "}")); 454 455 // Don't remove empty lines before namespace endings. 456 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 457 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 458 EXPECT_EQ("namespace {\n" 459 "int i;\n" 460 "\n" 461 "}", 462 format("namespace {\n" 463 "int i;\n" 464 "\n" 465 "}", 466 LLVMWithNoNamespaceFix)); 467 EXPECT_EQ("namespace {\n" 468 "int i;\n" 469 "}", 470 format("namespace {\n" 471 "int i;\n" 472 "}", 473 LLVMWithNoNamespaceFix)); 474 EXPECT_EQ("namespace {\n" 475 "int i;\n" 476 "\n" 477 "};", 478 format("namespace {\n" 479 "int i;\n" 480 "\n" 481 "};", 482 LLVMWithNoNamespaceFix)); 483 EXPECT_EQ("namespace {\n" 484 "int i;\n" 485 "};", 486 format("namespace {\n" 487 "int i;\n" 488 "};", 489 LLVMWithNoNamespaceFix)); 490 EXPECT_EQ("namespace {\n" 491 "int i;\n" 492 "\n" 493 "}", 494 format("namespace {\n" 495 "int i;\n" 496 "\n" 497 "}")); 498 EXPECT_EQ("namespace {\n" 499 "int i;\n" 500 "\n" 501 "} // namespace", 502 format("namespace {\n" 503 "int i;\n" 504 "\n" 505 "} // namespace")); 506 507 FormatStyle Style = getLLVMStyle(); 508 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 509 Style.MaxEmptyLinesToKeep = 2; 510 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 511 Style.BraceWrapping.AfterClass = true; 512 Style.BraceWrapping.AfterFunction = true; 513 Style.KeepEmptyLinesAtTheStartOfBlocks = false; 514 515 EXPECT_EQ("class Foo\n" 516 "{\n" 517 " Foo() {}\n" 518 "\n" 519 " void funk() {}\n" 520 "};", 521 format("class Foo\n" 522 "{\n" 523 " Foo()\n" 524 " {\n" 525 " }\n" 526 "\n" 527 " void funk() {}\n" 528 "};", 529 Style)); 530 } 531 532 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) { 533 verifyFormat("x = (a) and (b);"); 534 verifyFormat("x = (a) or (b);"); 535 verifyFormat("x = (a) bitand (b);"); 536 verifyFormat("x = (a) bitor (b);"); 537 verifyFormat("x = (a) not_eq (b);"); 538 verifyFormat("x = (a) and_eq (b);"); 539 verifyFormat("x = (a) or_eq (b);"); 540 verifyFormat("x = (a) xor (b);"); 541 } 542 543 TEST_F(FormatTest, RecognizesUnaryOperatorKeywords) { 544 verifyFormat("x = compl(a);"); 545 verifyFormat("x = not(a);"); 546 verifyFormat("x = bitand(a);"); 547 // Unary operator must not be merged with the next identifier 548 verifyFormat("x = compl a;"); 549 verifyFormat("x = not a;"); 550 verifyFormat("x = bitand a;"); 551 } 552 553 //===----------------------------------------------------------------------===// 554 // Tests for control statements. 555 //===----------------------------------------------------------------------===// 556 557 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) { 558 verifyFormat("if (true)\n f();\ng();"); 559 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); 560 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); 561 verifyFormat("if constexpr (true)\n" 562 " f();\ng();"); 563 verifyFormat("if CONSTEXPR (true)\n" 564 " f();\ng();"); 565 verifyFormat("if constexpr (a)\n" 566 " if constexpr (b)\n" 567 " if constexpr (c)\n" 568 " g();\n" 569 "h();"); 570 verifyFormat("if CONSTEXPR (a)\n" 571 " if CONSTEXPR (b)\n" 572 " if CONSTEXPR (c)\n" 573 " g();\n" 574 "h();"); 575 verifyFormat("if constexpr (a)\n" 576 " if constexpr (b) {\n" 577 " f();\n" 578 " }\n" 579 "g();"); 580 verifyFormat("if CONSTEXPR (a)\n" 581 " if CONSTEXPR (b) {\n" 582 " f();\n" 583 " }\n" 584 "g();"); 585 586 verifyFormat("if (a)\n" 587 " g();"); 588 verifyFormat("if (a) {\n" 589 " g()\n" 590 "};"); 591 verifyFormat("if (a)\n" 592 " g();\n" 593 "else\n" 594 " g();"); 595 verifyFormat("if (a) {\n" 596 " g();\n" 597 "} else\n" 598 " g();"); 599 verifyFormat("if (a)\n" 600 " g();\n" 601 "else {\n" 602 " g();\n" 603 "}"); 604 verifyFormat("if (a) {\n" 605 " g();\n" 606 "} else {\n" 607 " g();\n" 608 "}"); 609 verifyFormat("if (a)\n" 610 " g();\n" 611 "else if (b)\n" 612 " g();\n" 613 "else\n" 614 " g();"); 615 verifyFormat("if (a) {\n" 616 " g();\n" 617 "} else if (b)\n" 618 " g();\n" 619 "else\n" 620 " g();"); 621 verifyFormat("if (a)\n" 622 " g();\n" 623 "else if (b) {\n" 624 " g();\n" 625 "} else\n" 626 " g();"); 627 verifyFormat("if (a)\n" 628 " g();\n" 629 "else if (b)\n" 630 " g();\n" 631 "else {\n" 632 " g();\n" 633 "}"); 634 verifyFormat("if (a)\n" 635 " g();\n" 636 "else if (b) {\n" 637 " g();\n" 638 "} else {\n" 639 " g();\n" 640 "}"); 641 verifyFormat("if (a) {\n" 642 " g();\n" 643 "} else if (b) {\n" 644 " g();\n" 645 "} else {\n" 646 " g();\n" 647 "}"); 648 649 FormatStyle AllowsMergedIf = getLLVMStyle(); 650 AllowsMergedIf.IfMacros.push_back("MYIF"); 651 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left; 652 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 653 FormatStyle::SIS_WithoutElse; 654 verifyFormat("if (a)\n" 655 " // comment\n" 656 " f();", 657 AllowsMergedIf); 658 verifyFormat("{\n" 659 " if (a)\n" 660 " label:\n" 661 " f();\n" 662 "}", 663 AllowsMergedIf); 664 verifyFormat("#define A \\\n" 665 " if (a) \\\n" 666 " label: \\\n" 667 " f()", 668 AllowsMergedIf); 669 verifyFormat("if (a)\n" 670 " ;", 671 AllowsMergedIf); 672 verifyFormat("if (a)\n" 673 " if (b) return;", 674 AllowsMergedIf); 675 676 verifyFormat("if (a) // Can't merge this\n" 677 " f();\n", 678 AllowsMergedIf); 679 verifyFormat("if (a) /* still don't merge */\n" 680 " f();", 681 AllowsMergedIf); 682 verifyFormat("if (a) { // Never merge this\n" 683 " f();\n" 684 "}", 685 AllowsMergedIf); 686 verifyFormat("if (a) { /* Never merge this */\n" 687 " f();\n" 688 "}", 689 AllowsMergedIf); 690 verifyFormat("MYIF (a)\n" 691 " // comment\n" 692 " f();", 693 AllowsMergedIf); 694 verifyFormat("{\n" 695 " MYIF (a)\n" 696 " label:\n" 697 " f();\n" 698 "}", 699 AllowsMergedIf); 700 verifyFormat("#define A \\\n" 701 " MYIF (a) \\\n" 702 " label: \\\n" 703 " f()", 704 AllowsMergedIf); 705 verifyFormat("MYIF (a)\n" 706 " ;", 707 AllowsMergedIf); 708 verifyFormat("MYIF (a)\n" 709 " MYIF (b) return;", 710 AllowsMergedIf); 711 712 verifyFormat("MYIF (a) // Can't merge this\n" 713 " f();\n", 714 AllowsMergedIf); 715 verifyFormat("MYIF (a) /* still don't merge */\n" 716 " f();", 717 AllowsMergedIf); 718 verifyFormat("MYIF (a) { // Never merge this\n" 719 " f();\n" 720 "}", 721 AllowsMergedIf); 722 verifyFormat("MYIF (a) { /* Never merge this */\n" 723 " f();\n" 724 "}", 725 AllowsMergedIf); 726 727 AllowsMergedIf.ColumnLimit = 14; 728 // Where line-lengths matter, a 2-letter synonym that maintains line length. 729 // Not IF to avoid any confusion that IF is somehow special. 730 AllowsMergedIf.IfMacros.push_back("FI"); 731 verifyFormat("if (a) return;", AllowsMergedIf); 732 verifyFormat("if (aaaaaaaaa)\n" 733 " return;", 734 AllowsMergedIf); 735 verifyFormat("FI (a) return;", AllowsMergedIf); 736 verifyFormat("FI (aaaaaaaaa)\n" 737 " return;", 738 AllowsMergedIf); 739 740 AllowsMergedIf.ColumnLimit = 13; 741 verifyFormat("if (a)\n return;", AllowsMergedIf); 742 verifyFormat("FI (a)\n return;", AllowsMergedIf); 743 744 FormatStyle AllowsMergedIfElse = getLLVMStyle(); 745 AllowsMergedIfElse.IfMacros.push_back("MYIF"); 746 AllowsMergedIfElse.AllowShortIfStatementsOnASingleLine = 747 FormatStyle::SIS_AllIfsAndElse; 748 verifyFormat("if (a)\n" 749 " // comment\n" 750 " f();\n" 751 "else\n" 752 " // comment\n" 753 " f();", 754 AllowsMergedIfElse); 755 verifyFormat("{\n" 756 " if (a)\n" 757 " label:\n" 758 " f();\n" 759 " else\n" 760 " label:\n" 761 " f();\n" 762 "}", 763 AllowsMergedIfElse); 764 verifyFormat("if (a)\n" 765 " ;\n" 766 "else\n" 767 " ;", 768 AllowsMergedIfElse); 769 verifyFormat("if (a) {\n" 770 "} else {\n" 771 "}", 772 AllowsMergedIfElse); 773 verifyFormat("if (a) return;\n" 774 "else if (b) return;\n" 775 "else return;", 776 AllowsMergedIfElse); 777 verifyFormat("if (a) {\n" 778 "} else return;", 779 AllowsMergedIfElse); 780 verifyFormat("if (a) {\n" 781 "} else if (b) return;\n" 782 "else return;", 783 AllowsMergedIfElse); 784 verifyFormat("if (a) return;\n" 785 "else if (b) {\n" 786 "} else return;", 787 AllowsMergedIfElse); 788 verifyFormat("if (a)\n" 789 " if (b) return;\n" 790 " else return;", 791 AllowsMergedIfElse); 792 verifyFormat("if constexpr (a)\n" 793 " if constexpr (b) return;\n" 794 " else if constexpr (c) return;\n" 795 " else return;", 796 AllowsMergedIfElse); 797 verifyFormat("MYIF (a)\n" 798 " // comment\n" 799 " f();\n" 800 "else\n" 801 " // comment\n" 802 " f();", 803 AllowsMergedIfElse); 804 verifyFormat("{\n" 805 " MYIF (a)\n" 806 " label:\n" 807 " f();\n" 808 " else\n" 809 " label:\n" 810 " f();\n" 811 "}", 812 AllowsMergedIfElse); 813 verifyFormat("MYIF (a)\n" 814 " ;\n" 815 "else\n" 816 " ;", 817 AllowsMergedIfElse); 818 verifyFormat("MYIF (a) {\n" 819 "} else {\n" 820 "}", 821 AllowsMergedIfElse); 822 verifyFormat("MYIF (a) return;\n" 823 "else MYIF (b) return;\n" 824 "else return;", 825 AllowsMergedIfElse); 826 verifyFormat("MYIF (a) {\n" 827 "} else return;", 828 AllowsMergedIfElse); 829 verifyFormat("MYIF (a) {\n" 830 "} else MYIF (b) return;\n" 831 "else return;", 832 AllowsMergedIfElse); 833 verifyFormat("MYIF (a) return;\n" 834 "else MYIF (b) {\n" 835 "} else return;", 836 AllowsMergedIfElse); 837 verifyFormat("MYIF (a)\n" 838 " MYIF (b) return;\n" 839 " else return;", 840 AllowsMergedIfElse); 841 verifyFormat("MYIF constexpr (a)\n" 842 " MYIF constexpr (b) return;\n" 843 " else MYIF constexpr (c) return;\n" 844 " else return;", 845 AllowsMergedIfElse); 846 } 847 848 TEST_F(FormatTest, FormatIfWithoutCompoundStatementButElseWith) { 849 FormatStyle AllowsMergedIf = getLLVMStyle(); 850 AllowsMergedIf.IfMacros.push_back("MYIF"); 851 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left; 852 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 853 FormatStyle::SIS_WithoutElse; 854 verifyFormat("if (a)\n" 855 " f();\n" 856 "else {\n" 857 " g();\n" 858 "}", 859 AllowsMergedIf); 860 verifyFormat("if (a)\n" 861 " f();\n" 862 "else\n" 863 " g();\n", 864 AllowsMergedIf); 865 866 verifyFormat("if (a) g();", AllowsMergedIf); 867 verifyFormat("if (a) {\n" 868 " g()\n" 869 "};", 870 AllowsMergedIf); 871 verifyFormat("if (a)\n" 872 " g();\n" 873 "else\n" 874 " g();", 875 AllowsMergedIf); 876 verifyFormat("if (a) {\n" 877 " g();\n" 878 "} else\n" 879 " g();", 880 AllowsMergedIf); 881 verifyFormat("if (a)\n" 882 " g();\n" 883 "else {\n" 884 " g();\n" 885 "}", 886 AllowsMergedIf); 887 verifyFormat("if (a) {\n" 888 " g();\n" 889 "} else {\n" 890 " g();\n" 891 "}", 892 AllowsMergedIf); 893 verifyFormat("if (a)\n" 894 " g();\n" 895 "else if (b)\n" 896 " g();\n" 897 "else\n" 898 " g();", 899 AllowsMergedIf); 900 verifyFormat("if (a) {\n" 901 " g();\n" 902 "} else if (b)\n" 903 " g();\n" 904 "else\n" 905 " g();", 906 AllowsMergedIf); 907 verifyFormat("if (a)\n" 908 " g();\n" 909 "else if (b) {\n" 910 " g();\n" 911 "} else\n" 912 " g();", 913 AllowsMergedIf); 914 verifyFormat("if (a)\n" 915 " g();\n" 916 "else if (b)\n" 917 " g();\n" 918 "else {\n" 919 " g();\n" 920 "}", 921 AllowsMergedIf); 922 verifyFormat("if (a)\n" 923 " g();\n" 924 "else if (b) {\n" 925 " g();\n" 926 "} else {\n" 927 " g();\n" 928 "}", 929 AllowsMergedIf); 930 verifyFormat("if (a) {\n" 931 " g();\n" 932 "} else if (b) {\n" 933 " g();\n" 934 "} else {\n" 935 " g();\n" 936 "}", 937 AllowsMergedIf); 938 verifyFormat("MYIF (a)\n" 939 " f();\n" 940 "else {\n" 941 " g();\n" 942 "}", 943 AllowsMergedIf); 944 verifyFormat("MYIF (a)\n" 945 " f();\n" 946 "else\n" 947 " g();\n", 948 AllowsMergedIf); 949 950 verifyFormat("MYIF (a) g();", AllowsMergedIf); 951 verifyFormat("MYIF (a) {\n" 952 " g()\n" 953 "};", 954 AllowsMergedIf); 955 verifyFormat("MYIF (a)\n" 956 " g();\n" 957 "else\n" 958 " g();", 959 AllowsMergedIf); 960 verifyFormat("MYIF (a) {\n" 961 " g();\n" 962 "} else\n" 963 " g();", 964 AllowsMergedIf); 965 verifyFormat("MYIF (a)\n" 966 " g();\n" 967 "else {\n" 968 " g();\n" 969 "}", 970 AllowsMergedIf); 971 verifyFormat("MYIF (a) {\n" 972 " g();\n" 973 "} else {\n" 974 " g();\n" 975 "}", 976 AllowsMergedIf); 977 verifyFormat("MYIF (a)\n" 978 " g();\n" 979 "else MYIF (b)\n" 980 " g();\n" 981 "else\n" 982 " g();", 983 AllowsMergedIf); 984 verifyFormat("MYIF (a)\n" 985 " g();\n" 986 "else if (b)\n" 987 " g();\n" 988 "else\n" 989 " g();", 990 AllowsMergedIf); 991 verifyFormat("MYIF (a) {\n" 992 " g();\n" 993 "} else MYIF (b)\n" 994 " g();\n" 995 "else\n" 996 " g();", 997 AllowsMergedIf); 998 verifyFormat("MYIF (a) {\n" 999 " g();\n" 1000 "} else if (b)\n" 1001 " g();\n" 1002 "else\n" 1003 " g();", 1004 AllowsMergedIf); 1005 verifyFormat("MYIF (a)\n" 1006 " g();\n" 1007 "else MYIF (b) {\n" 1008 " g();\n" 1009 "} else\n" 1010 " g();", 1011 AllowsMergedIf); 1012 verifyFormat("MYIF (a)\n" 1013 " g();\n" 1014 "else if (b) {\n" 1015 " g();\n" 1016 "} else\n" 1017 " g();", 1018 AllowsMergedIf); 1019 verifyFormat("MYIF (a)\n" 1020 " g();\n" 1021 "else MYIF (b)\n" 1022 " g();\n" 1023 "else {\n" 1024 " g();\n" 1025 "}", 1026 AllowsMergedIf); 1027 verifyFormat("MYIF (a)\n" 1028 " g();\n" 1029 "else if (b)\n" 1030 " g();\n" 1031 "else {\n" 1032 " g();\n" 1033 "}", 1034 AllowsMergedIf); 1035 verifyFormat("MYIF (a)\n" 1036 " g();\n" 1037 "else MYIF (b) {\n" 1038 " g();\n" 1039 "} else {\n" 1040 " g();\n" 1041 "}", 1042 AllowsMergedIf); 1043 verifyFormat("MYIF (a)\n" 1044 " g();\n" 1045 "else if (b) {\n" 1046 " g();\n" 1047 "} else {\n" 1048 " g();\n" 1049 "}", 1050 AllowsMergedIf); 1051 verifyFormat("MYIF (a) {\n" 1052 " g();\n" 1053 "} else MYIF (b) {\n" 1054 " g();\n" 1055 "} else {\n" 1056 " g();\n" 1057 "}", 1058 AllowsMergedIf); 1059 verifyFormat("MYIF (a) {\n" 1060 " g();\n" 1061 "} else if (b) {\n" 1062 " g();\n" 1063 "} else {\n" 1064 " g();\n" 1065 "}", 1066 AllowsMergedIf); 1067 1068 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 1069 FormatStyle::SIS_OnlyFirstIf; 1070 1071 verifyFormat("if (a) f();\n" 1072 "else {\n" 1073 " g();\n" 1074 "}", 1075 AllowsMergedIf); 1076 verifyFormat("if (a) f();\n" 1077 "else {\n" 1078 " if (a) f();\n" 1079 " else {\n" 1080 " g();\n" 1081 " }\n" 1082 " g();\n" 1083 "}", 1084 AllowsMergedIf); 1085 1086 verifyFormat("if (a) g();", AllowsMergedIf); 1087 verifyFormat("if (a) {\n" 1088 " g()\n" 1089 "};", 1090 AllowsMergedIf); 1091 verifyFormat("if (a) g();\n" 1092 "else\n" 1093 " g();", 1094 AllowsMergedIf); 1095 verifyFormat("if (a) {\n" 1096 " g();\n" 1097 "} else\n" 1098 " g();", 1099 AllowsMergedIf); 1100 verifyFormat("if (a) g();\n" 1101 "else {\n" 1102 " g();\n" 1103 "}", 1104 AllowsMergedIf); 1105 verifyFormat("if (a) {\n" 1106 " g();\n" 1107 "} else {\n" 1108 " g();\n" 1109 "}", 1110 AllowsMergedIf); 1111 verifyFormat("if (a) g();\n" 1112 "else if (b)\n" 1113 " g();\n" 1114 "else\n" 1115 " g();", 1116 AllowsMergedIf); 1117 verifyFormat("if (a) {\n" 1118 " g();\n" 1119 "} else if (b)\n" 1120 " g();\n" 1121 "else\n" 1122 " g();", 1123 AllowsMergedIf); 1124 verifyFormat("if (a) g();\n" 1125 "else if (b) {\n" 1126 " g();\n" 1127 "} else\n" 1128 " g();", 1129 AllowsMergedIf); 1130 verifyFormat("if (a) g();\n" 1131 "else if (b)\n" 1132 " g();\n" 1133 "else {\n" 1134 " g();\n" 1135 "}", 1136 AllowsMergedIf); 1137 verifyFormat("if (a) g();\n" 1138 "else if (b) {\n" 1139 " g();\n" 1140 "} else {\n" 1141 " g();\n" 1142 "}", 1143 AllowsMergedIf); 1144 verifyFormat("if (a) {\n" 1145 " g();\n" 1146 "} else if (b) {\n" 1147 " g();\n" 1148 "} else {\n" 1149 " g();\n" 1150 "}", 1151 AllowsMergedIf); 1152 verifyFormat("MYIF (a) f();\n" 1153 "else {\n" 1154 " g();\n" 1155 "}", 1156 AllowsMergedIf); 1157 verifyFormat("MYIF (a) f();\n" 1158 "else {\n" 1159 " if (a) f();\n" 1160 " else {\n" 1161 " g();\n" 1162 " }\n" 1163 " g();\n" 1164 "}", 1165 AllowsMergedIf); 1166 1167 verifyFormat("MYIF (a) g();", AllowsMergedIf); 1168 verifyFormat("MYIF (a) {\n" 1169 " g()\n" 1170 "};", 1171 AllowsMergedIf); 1172 verifyFormat("MYIF (a) g();\n" 1173 "else\n" 1174 " g();", 1175 AllowsMergedIf); 1176 verifyFormat("MYIF (a) {\n" 1177 " g();\n" 1178 "} else\n" 1179 " g();", 1180 AllowsMergedIf); 1181 verifyFormat("MYIF (a) g();\n" 1182 "else {\n" 1183 " g();\n" 1184 "}", 1185 AllowsMergedIf); 1186 verifyFormat("MYIF (a) {\n" 1187 " g();\n" 1188 "} else {\n" 1189 " g();\n" 1190 "}", 1191 AllowsMergedIf); 1192 verifyFormat("MYIF (a) g();\n" 1193 "else MYIF (b)\n" 1194 " g();\n" 1195 "else\n" 1196 " g();", 1197 AllowsMergedIf); 1198 verifyFormat("MYIF (a) g();\n" 1199 "else if (b)\n" 1200 " g();\n" 1201 "else\n" 1202 " g();", 1203 AllowsMergedIf); 1204 verifyFormat("MYIF (a) {\n" 1205 " g();\n" 1206 "} else MYIF (b)\n" 1207 " g();\n" 1208 "else\n" 1209 " g();", 1210 AllowsMergedIf); 1211 verifyFormat("MYIF (a) {\n" 1212 " g();\n" 1213 "} else if (b)\n" 1214 " g();\n" 1215 "else\n" 1216 " g();", 1217 AllowsMergedIf); 1218 verifyFormat("MYIF (a) g();\n" 1219 "else MYIF (b) {\n" 1220 " g();\n" 1221 "} else\n" 1222 " g();", 1223 AllowsMergedIf); 1224 verifyFormat("MYIF (a) g();\n" 1225 "else if (b) {\n" 1226 " g();\n" 1227 "} else\n" 1228 " g();", 1229 AllowsMergedIf); 1230 verifyFormat("MYIF (a) g();\n" 1231 "else MYIF (b)\n" 1232 " g();\n" 1233 "else {\n" 1234 " g();\n" 1235 "}", 1236 AllowsMergedIf); 1237 verifyFormat("MYIF (a) g();\n" 1238 "else if (b)\n" 1239 " g();\n" 1240 "else {\n" 1241 " g();\n" 1242 "}", 1243 AllowsMergedIf); 1244 verifyFormat("MYIF (a) g();\n" 1245 "else MYIF (b) {\n" 1246 " g();\n" 1247 "} else {\n" 1248 " g();\n" 1249 "}", 1250 AllowsMergedIf); 1251 verifyFormat("MYIF (a) g();\n" 1252 "else if (b) {\n" 1253 " g();\n" 1254 "} else {\n" 1255 " g();\n" 1256 "}", 1257 AllowsMergedIf); 1258 verifyFormat("MYIF (a) {\n" 1259 " g();\n" 1260 "} else MYIF (b) {\n" 1261 " g();\n" 1262 "} else {\n" 1263 " g();\n" 1264 "}", 1265 AllowsMergedIf); 1266 verifyFormat("MYIF (a) {\n" 1267 " g();\n" 1268 "} else if (b) {\n" 1269 " g();\n" 1270 "} else {\n" 1271 " g();\n" 1272 "}", 1273 AllowsMergedIf); 1274 1275 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 1276 FormatStyle::SIS_AllIfsAndElse; 1277 1278 verifyFormat("if (a) f();\n" 1279 "else {\n" 1280 " g();\n" 1281 "}", 1282 AllowsMergedIf); 1283 verifyFormat("if (a) f();\n" 1284 "else {\n" 1285 " if (a) f();\n" 1286 " else {\n" 1287 " g();\n" 1288 " }\n" 1289 " g();\n" 1290 "}", 1291 AllowsMergedIf); 1292 1293 verifyFormat("if (a) g();", AllowsMergedIf); 1294 verifyFormat("if (a) {\n" 1295 " g()\n" 1296 "};", 1297 AllowsMergedIf); 1298 verifyFormat("if (a) g();\n" 1299 "else g();", 1300 AllowsMergedIf); 1301 verifyFormat("if (a) {\n" 1302 " g();\n" 1303 "} else g();", 1304 AllowsMergedIf); 1305 verifyFormat("if (a) g();\n" 1306 "else {\n" 1307 " g();\n" 1308 "}", 1309 AllowsMergedIf); 1310 verifyFormat("if (a) {\n" 1311 " g();\n" 1312 "} else {\n" 1313 " g();\n" 1314 "}", 1315 AllowsMergedIf); 1316 verifyFormat("if (a) g();\n" 1317 "else if (b) g();\n" 1318 "else g();", 1319 AllowsMergedIf); 1320 verifyFormat("if (a) {\n" 1321 " g();\n" 1322 "} else if (b) g();\n" 1323 "else g();", 1324 AllowsMergedIf); 1325 verifyFormat("if (a) g();\n" 1326 "else if (b) {\n" 1327 " g();\n" 1328 "} else g();", 1329 AllowsMergedIf); 1330 verifyFormat("if (a) g();\n" 1331 "else if (b) g();\n" 1332 "else {\n" 1333 " g();\n" 1334 "}", 1335 AllowsMergedIf); 1336 verifyFormat("if (a) g();\n" 1337 "else if (b) {\n" 1338 " g();\n" 1339 "} else {\n" 1340 " g();\n" 1341 "}", 1342 AllowsMergedIf); 1343 verifyFormat("if (a) {\n" 1344 " g();\n" 1345 "} else if (b) {\n" 1346 " g();\n" 1347 "} else {\n" 1348 " g();\n" 1349 "}", 1350 AllowsMergedIf); 1351 verifyFormat("MYIF (a) f();\n" 1352 "else {\n" 1353 " g();\n" 1354 "}", 1355 AllowsMergedIf); 1356 verifyFormat("MYIF (a) f();\n" 1357 "else {\n" 1358 " if (a) f();\n" 1359 " else {\n" 1360 " g();\n" 1361 " }\n" 1362 " g();\n" 1363 "}", 1364 AllowsMergedIf); 1365 1366 verifyFormat("MYIF (a) g();", AllowsMergedIf); 1367 verifyFormat("MYIF (a) {\n" 1368 " g()\n" 1369 "};", 1370 AllowsMergedIf); 1371 verifyFormat("MYIF (a) g();\n" 1372 "else g();", 1373 AllowsMergedIf); 1374 verifyFormat("MYIF (a) {\n" 1375 " g();\n" 1376 "} else g();", 1377 AllowsMergedIf); 1378 verifyFormat("MYIF (a) g();\n" 1379 "else {\n" 1380 " g();\n" 1381 "}", 1382 AllowsMergedIf); 1383 verifyFormat("MYIF (a) {\n" 1384 " g();\n" 1385 "} else {\n" 1386 " g();\n" 1387 "}", 1388 AllowsMergedIf); 1389 verifyFormat("MYIF (a) g();\n" 1390 "else MYIF (b) g();\n" 1391 "else g();", 1392 AllowsMergedIf); 1393 verifyFormat("MYIF (a) g();\n" 1394 "else if (b) g();\n" 1395 "else g();", 1396 AllowsMergedIf); 1397 verifyFormat("MYIF (a) {\n" 1398 " g();\n" 1399 "} else MYIF (b) g();\n" 1400 "else g();", 1401 AllowsMergedIf); 1402 verifyFormat("MYIF (a) {\n" 1403 " g();\n" 1404 "} else if (b) g();\n" 1405 "else g();", 1406 AllowsMergedIf); 1407 verifyFormat("MYIF (a) g();\n" 1408 "else MYIF (b) {\n" 1409 " g();\n" 1410 "} else g();", 1411 AllowsMergedIf); 1412 verifyFormat("MYIF (a) g();\n" 1413 "else if (b) {\n" 1414 " g();\n" 1415 "} else g();", 1416 AllowsMergedIf); 1417 verifyFormat("MYIF (a) g();\n" 1418 "else MYIF (b) g();\n" 1419 "else {\n" 1420 " g();\n" 1421 "}", 1422 AllowsMergedIf); 1423 verifyFormat("MYIF (a) g();\n" 1424 "else if (b) g();\n" 1425 "else {\n" 1426 " g();\n" 1427 "}", 1428 AllowsMergedIf); 1429 verifyFormat("MYIF (a) g();\n" 1430 "else MYIF (b) {\n" 1431 " g();\n" 1432 "} else {\n" 1433 " g();\n" 1434 "}", 1435 AllowsMergedIf); 1436 verifyFormat("MYIF (a) g();\n" 1437 "else if (b) {\n" 1438 " g();\n" 1439 "} else {\n" 1440 " g();\n" 1441 "}", 1442 AllowsMergedIf); 1443 verifyFormat("MYIF (a) {\n" 1444 " g();\n" 1445 "} else MYIF (b) {\n" 1446 " g();\n" 1447 "} else {\n" 1448 " g();\n" 1449 "}", 1450 AllowsMergedIf); 1451 verifyFormat("MYIF (a) {\n" 1452 " g();\n" 1453 "} else if (b) {\n" 1454 " g();\n" 1455 "} else {\n" 1456 " g();\n" 1457 "}", 1458 AllowsMergedIf); 1459 } 1460 1461 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) { 1462 FormatStyle AllowsMergedLoops = getLLVMStyle(); 1463 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true; 1464 verifyFormat("while (true) continue;", AllowsMergedLoops); 1465 verifyFormat("for (;;) continue;", AllowsMergedLoops); 1466 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops); 1467 verifyFormat("while (true)\n" 1468 " ;", 1469 AllowsMergedLoops); 1470 verifyFormat("for (;;)\n" 1471 " ;", 1472 AllowsMergedLoops); 1473 verifyFormat("for (;;)\n" 1474 " for (;;) continue;", 1475 AllowsMergedLoops); 1476 verifyFormat("for (;;) // Can't merge this\n" 1477 " continue;", 1478 AllowsMergedLoops); 1479 verifyFormat("for (;;) /* still don't merge */\n" 1480 " continue;", 1481 AllowsMergedLoops); 1482 verifyFormat("do a++;\n" 1483 "while (true);", 1484 AllowsMergedLoops); 1485 verifyFormat("do /* Don't merge */\n" 1486 " a++;\n" 1487 "while (true);", 1488 AllowsMergedLoops); 1489 verifyFormat("do // Don't merge\n" 1490 " a++;\n" 1491 "while (true);", 1492 AllowsMergedLoops); 1493 verifyFormat("do\n" 1494 " // Don't merge\n" 1495 " a++;\n" 1496 "while (true);", 1497 AllowsMergedLoops); 1498 // Without braces labels are interpreted differently. 1499 verifyFormat("{\n" 1500 " do\n" 1501 " label:\n" 1502 " a++;\n" 1503 " while (true);\n" 1504 "}", 1505 AllowsMergedLoops); 1506 } 1507 1508 TEST_F(FormatTest, FormatShortBracedStatements) { 1509 FormatStyle AllowSimpleBracedStatements = getLLVMStyle(); 1510 AllowSimpleBracedStatements.IfMacros.push_back("MYIF"); 1511 // Where line-lengths matter, a 2-letter synonym that maintains line length. 1512 // Not IF to avoid any confusion that IF is somehow special. 1513 AllowSimpleBracedStatements.IfMacros.push_back("FI"); 1514 AllowSimpleBracedStatements.ColumnLimit = 40; 1515 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = 1516 FormatStyle::SBS_Always; 1517 1518 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1519 FormatStyle::SIS_WithoutElse; 1520 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 1521 1522 AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom; 1523 AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true; 1524 AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false; 1525 1526 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1527 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 1528 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1529 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1530 verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements); 1531 verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1532 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1533 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1534 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 1535 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 1536 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1537 verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements); 1538 verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements); 1539 verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1540 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 1541 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 1542 verifyFormat("if (true) { fffffffffffffffffffffff(); }", 1543 AllowSimpleBracedStatements); 1544 verifyFormat("if (true) {\n" 1545 " ffffffffffffffffffffffff();\n" 1546 "}", 1547 AllowSimpleBracedStatements); 1548 verifyFormat("if (true) {\n" 1549 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1550 "}", 1551 AllowSimpleBracedStatements); 1552 verifyFormat("if (true) { //\n" 1553 " f();\n" 1554 "}", 1555 AllowSimpleBracedStatements); 1556 verifyFormat("if (true) {\n" 1557 " f();\n" 1558 " f();\n" 1559 "}", 1560 AllowSimpleBracedStatements); 1561 verifyFormat("if (true) {\n" 1562 " f();\n" 1563 "} else {\n" 1564 " f();\n" 1565 "}", 1566 AllowSimpleBracedStatements); 1567 verifyFormat("FI (true) { fffffffffffffffffffffff(); }", 1568 AllowSimpleBracedStatements); 1569 verifyFormat("MYIF (true) {\n" 1570 " ffffffffffffffffffffffff();\n" 1571 "}", 1572 AllowSimpleBracedStatements); 1573 verifyFormat("MYIF (true) {\n" 1574 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1575 "}", 1576 AllowSimpleBracedStatements); 1577 verifyFormat("MYIF (true) { //\n" 1578 " f();\n" 1579 "}", 1580 AllowSimpleBracedStatements); 1581 verifyFormat("MYIF (true) {\n" 1582 " f();\n" 1583 " f();\n" 1584 "}", 1585 AllowSimpleBracedStatements); 1586 verifyFormat("MYIF (true) {\n" 1587 " f();\n" 1588 "} else {\n" 1589 " f();\n" 1590 "}", 1591 AllowSimpleBracedStatements); 1592 1593 verifyFormat("struct A2 {\n" 1594 " int X;\n" 1595 "};", 1596 AllowSimpleBracedStatements); 1597 verifyFormat("typedef struct A2 {\n" 1598 " int X;\n" 1599 "} A2_t;", 1600 AllowSimpleBracedStatements); 1601 verifyFormat("template <int> struct A2 {\n" 1602 " struct B {};\n" 1603 "};", 1604 AllowSimpleBracedStatements); 1605 1606 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1607 FormatStyle::SIS_Never; 1608 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1609 verifyFormat("if (true) {\n" 1610 " f();\n" 1611 "}", 1612 AllowSimpleBracedStatements); 1613 verifyFormat("if (true) {\n" 1614 " f();\n" 1615 "} else {\n" 1616 " f();\n" 1617 "}", 1618 AllowSimpleBracedStatements); 1619 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1620 verifyFormat("MYIF (true) {\n" 1621 " f();\n" 1622 "}", 1623 AllowSimpleBracedStatements); 1624 verifyFormat("MYIF (true) {\n" 1625 " f();\n" 1626 "} else {\n" 1627 " f();\n" 1628 "}", 1629 AllowSimpleBracedStatements); 1630 1631 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 1632 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1633 verifyFormat("while (true) {\n" 1634 " f();\n" 1635 "}", 1636 AllowSimpleBracedStatements); 1637 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1638 verifyFormat("for (;;) {\n" 1639 " f();\n" 1640 "}", 1641 AllowSimpleBracedStatements); 1642 1643 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1644 FormatStyle::SIS_WithoutElse; 1645 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 1646 AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement = 1647 FormatStyle::BWACS_Always; 1648 1649 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1650 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 1651 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1652 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1653 verifyFormat("MYIF constexpr (true) {}", AllowSimpleBracedStatements); 1654 verifyFormat("MYIF CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1655 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1656 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1657 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 1658 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 1659 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1660 verifyFormat("MYIF (true) { f(); }", AllowSimpleBracedStatements); 1661 verifyFormat("MYIF constexpr (true) { f(); }", AllowSimpleBracedStatements); 1662 verifyFormat("MYIF CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1663 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 1664 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 1665 verifyFormat("if (true) { fffffffffffffffffffffff(); }", 1666 AllowSimpleBracedStatements); 1667 verifyFormat("if (true)\n" 1668 "{\n" 1669 " ffffffffffffffffffffffff();\n" 1670 "}", 1671 AllowSimpleBracedStatements); 1672 verifyFormat("if (true)\n" 1673 "{\n" 1674 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1675 "}", 1676 AllowSimpleBracedStatements); 1677 verifyFormat("if (true)\n" 1678 "{ //\n" 1679 " f();\n" 1680 "}", 1681 AllowSimpleBracedStatements); 1682 verifyFormat("if (true)\n" 1683 "{\n" 1684 " f();\n" 1685 " f();\n" 1686 "}", 1687 AllowSimpleBracedStatements); 1688 verifyFormat("if (true)\n" 1689 "{\n" 1690 " f();\n" 1691 "} else\n" 1692 "{\n" 1693 " f();\n" 1694 "}", 1695 AllowSimpleBracedStatements); 1696 verifyFormat("FI (true) { fffffffffffffffffffffff(); }", 1697 AllowSimpleBracedStatements); 1698 verifyFormat("MYIF (true)\n" 1699 "{\n" 1700 " ffffffffffffffffffffffff();\n" 1701 "}", 1702 AllowSimpleBracedStatements); 1703 verifyFormat("MYIF (true)\n" 1704 "{\n" 1705 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1706 "}", 1707 AllowSimpleBracedStatements); 1708 verifyFormat("MYIF (true)\n" 1709 "{ //\n" 1710 " f();\n" 1711 "}", 1712 AllowSimpleBracedStatements); 1713 verifyFormat("MYIF (true)\n" 1714 "{\n" 1715 " f();\n" 1716 " f();\n" 1717 "}", 1718 AllowSimpleBracedStatements); 1719 verifyFormat("MYIF (true)\n" 1720 "{\n" 1721 " f();\n" 1722 "} else\n" 1723 "{\n" 1724 " f();\n" 1725 "}", 1726 AllowSimpleBracedStatements); 1727 1728 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1729 FormatStyle::SIS_Never; 1730 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1731 verifyFormat("if (true)\n" 1732 "{\n" 1733 " f();\n" 1734 "}", 1735 AllowSimpleBracedStatements); 1736 verifyFormat("if (true)\n" 1737 "{\n" 1738 " f();\n" 1739 "} else\n" 1740 "{\n" 1741 " f();\n" 1742 "}", 1743 AllowSimpleBracedStatements); 1744 verifyFormat("MYIF (true) {}", AllowSimpleBracedStatements); 1745 verifyFormat("MYIF (true)\n" 1746 "{\n" 1747 " f();\n" 1748 "}", 1749 AllowSimpleBracedStatements); 1750 verifyFormat("MYIF (true)\n" 1751 "{\n" 1752 " f();\n" 1753 "} else\n" 1754 "{\n" 1755 " f();\n" 1756 "}", 1757 AllowSimpleBracedStatements); 1758 1759 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 1760 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1761 verifyFormat("while (true)\n" 1762 "{\n" 1763 " f();\n" 1764 "}", 1765 AllowSimpleBracedStatements); 1766 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1767 verifyFormat("for (;;)\n" 1768 "{\n" 1769 " f();\n" 1770 "}", 1771 AllowSimpleBracedStatements); 1772 } 1773 1774 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) { 1775 FormatStyle Style = getLLVMStyleWithColumns(60); 1776 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 1777 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; 1778 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 1779 EXPECT_EQ("#define A \\\n" 1780 " if (HANDLEwernufrnuLwrmviferuvnierv) \\\n" 1781 " { \\\n" 1782 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" 1783 " }\n" 1784 "X;", 1785 format("#define A \\\n" 1786 " if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n" 1787 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" 1788 " }\n" 1789 "X;", 1790 Style)); 1791 } 1792 1793 TEST_F(FormatTest, ParseIfElse) { 1794 verifyFormat("if (true)\n" 1795 " if (true)\n" 1796 " if (true)\n" 1797 " f();\n" 1798 " else\n" 1799 " g();\n" 1800 " else\n" 1801 " h();\n" 1802 "else\n" 1803 " i();"); 1804 verifyFormat("if (true)\n" 1805 " if (true)\n" 1806 " if (true) {\n" 1807 " if (true)\n" 1808 " f();\n" 1809 " } else {\n" 1810 " g();\n" 1811 " }\n" 1812 " else\n" 1813 " h();\n" 1814 "else {\n" 1815 " i();\n" 1816 "}"); 1817 verifyFormat("if (true)\n" 1818 " if constexpr (true)\n" 1819 " if (true) {\n" 1820 " if constexpr (true)\n" 1821 " f();\n" 1822 " } else {\n" 1823 " g();\n" 1824 " }\n" 1825 " else\n" 1826 " h();\n" 1827 "else {\n" 1828 " i();\n" 1829 "}"); 1830 verifyFormat("if (true)\n" 1831 " if CONSTEXPR (true)\n" 1832 " if (true) {\n" 1833 " if CONSTEXPR (true)\n" 1834 " f();\n" 1835 " } else {\n" 1836 " g();\n" 1837 " }\n" 1838 " else\n" 1839 " h();\n" 1840 "else {\n" 1841 " i();\n" 1842 "}"); 1843 verifyFormat("void f() {\n" 1844 " if (a) {\n" 1845 " } else {\n" 1846 " }\n" 1847 "}"); 1848 } 1849 1850 TEST_F(FormatTest, ElseIf) { 1851 verifyFormat("if (a) {\n} else if (b) {\n}"); 1852 verifyFormat("if (a)\n" 1853 " f();\n" 1854 "else if (b)\n" 1855 " g();\n" 1856 "else\n" 1857 " h();"); 1858 verifyFormat("if (a)\n" 1859 " f();\n" 1860 "else // comment\n" 1861 " if (b) {\n" 1862 " g();\n" 1863 " h();\n" 1864 " }"); 1865 verifyFormat("if constexpr (a)\n" 1866 " f();\n" 1867 "else if constexpr (b)\n" 1868 " g();\n" 1869 "else\n" 1870 " h();"); 1871 verifyFormat("if CONSTEXPR (a)\n" 1872 " f();\n" 1873 "else if CONSTEXPR (b)\n" 1874 " g();\n" 1875 "else\n" 1876 " h();"); 1877 verifyFormat("if (a) {\n" 1878 " f();\n" 1879 "}\n" 1880 "// or else ..\n" 1881 "else {\n" 1882 " g()\n" 1883 "}"); 1884 1885 verifyFormat("if (a) {\n" 1886 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1887 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 1888 "}"); 1889 verifyFormat("if (a) {\n" 1890 "} else if constexpr (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1891 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 1892 "}"); 1893 verifyFormat("if (a) {\n" 1894 "} else if CONSTEXPR (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1895 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 1896 "}"); 1897 verifyFormat("if (a) {\n" 1898 "} else if (\n" 1899 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 1900 "}", 1901 getLLVMStyleWithColumns(62)); 1902 verifyFormat("if (a) {\n" 1903 "} else if constexpr (\n" 1904 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 1905 "}", 1906 getLLVMStyleWithColumns(62)); 1907 verifyFormat("if (a) {\n" 1908 "} else if CONSTEXPR (\n" 1909 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 1910 "}", 1911 getLLVMStyleWithColumns(62)); 1912 } 1913 1914 TEST_F(FormatTest, SeparatePointerReferenceAlignment) { 1915 FormatStyle Style = getLLVMStyle(); 1916 // Check first the default LLVM style 1917 // Style.PointerAlignment = FormatStyle::PAS_Right; 1918 // Style.ReferenceAlignment = FormatStyle::RAS_Pointer; 1919 verifyFormat("int *f1(int *a, int &b, int &&c);", Style); 1920 verifyFormat("int &f2(int &&c, int *a, int &b);", Style); 1921 verifyFormat("int &&f3(int &b, int &&c, int *a);", Style); 1922 verifyFormat("int *f1(int &a) const &;", Style); 1923 verifyFormat("int *f1(int &a) const & = 0;", Style); 1924 verifyFormat("int *a = f1();", Style); 1925 verifyFormat("int &b = f2();", Style); 1926 verifyFormat("int &&c = f3();", Style); 1927 1928 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 1929 verifyFormat("Const unsigned int *c;\n" 1930 "const unsigned int *d;\n" 1931 "Const unsigned int &e;\n" 1932 "const unsigned int &f;\n" 1933 "const unsigned &&g;\n" 1934 "Const unsigned h;", 1935 Style); 1936 1937 Style.PointerAlignment = FormatStyle::PAS_Left; 1938 Style.ReferenceAlignment = FormatStyle::RAS_Pointer; 1939 verifyFormat("int* f1(int* a, int& b, int&& c);", Style); 1940 verifyFormat("int& f2(int&& c, int* a, int& b);", Style); 1941 verifyFormat("int&& f3(int& b, int&& c, int* a);", Style); 1942 verifyFormat("int* f1(int& a) const& = 0;", Style); 1943 verifyFormat("int* a = f1();", Style); 1944 verifyFormat("int& b = f2();", Style); 1945 verifyFormat("int&& c = f3();", Style); 1946 1947 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 1948 verifyFormat("Const unsigned int* c;\n" 1949 "const unsigned int* d;\n" 1950 "Const unsigned int& e;\n" 1951 "const unsigned int& f;\n" 1952 "const unsigned&& g;\n" 1953 "Const unsigned h;", 1954 Style); 1955 1956 Style.PointerAlignment = FormatStyle::PAS_Right; 1957 Style.ReferenceAlignment = FormatStyle::RAS_Left; 1958 verifyFormat("int *f1(int *a, int& b, int&& c);", Style); 1959 verifyFormat("int& f2(int&& c, int *a, int& b);", Style); 1960 verifyFormat("int&& f3(int& b, int&& c, int *a);", Style); 1961 verifyFormat("int *a = f1();", Style); 1962 verifyFormat("int& b = f2();", Style); 1963 verifyFormat("int&& c = f3();", Style); 1964 1965 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 1966 verifyFormat("Const unsigned int *c;\n" 1967 "const unsigned int *d;\n" 1968 "Const unsigned int& e;\n" 1969 "const unsigned int& f;\n" 1970 "const unsigned g;\n" 1971 "Const unsigned h;", 1972 Style); 1973 1974 Style.PointerAlignment = FormatStyle::PAS_Left; 1975 Style.ReferenceAlignment = FormatStyle::RAS_Middle; 1976 verifyFormat("int* f1(int* a, int & b, int && c);", Style); 1977 verifyFormat("int & f2(int && c, int* a, int & b);", Style); 1978 verifyFormat("int && f3(int & b, int && c, int* a);", Style); 1979 verifyFormat("int* a = f1();", Style); 1980 verifyFormat("int & b = f2();", Style); 1981 verifyFormat("int && c = f3();", Style); 1982 1983 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 1984 verifyFormat("Const unsigned int* c;\n" 1985 "const unsigned int* d;\n" 1986 "Const unsigned int & e;\n" 1987 "const unsigned int & f;\n" 1988 "const unsigned && g;\n" 1989 "Const unsigned h;", 1990 Style); 1991 1992 Style.PointerAlignment = FormatStyle::PAS_Middle; 1993 Style.ReferenceAlignment = FormatStyle::RAS_Right; 1994 verifyFormat("int * f1(int * a, int &b, int &&c);", Style); 1995 verifyFormat("int &f2(int &&c, int * a, int &b);", Style); 1996 verifyFormat("int &&f3(int &b, int &&c, int * a);", Style); 1997 verifyFormat("int * a = f1();", Style); 1998 verifyFormat("int &b = f2();", Style); 1999 verifyFormat("int &&c = f3();", Style); 2000 2001 // FIXME: we don't handle this yet, so output may be arbitrary until it's 2002 // specifically handled 2003 // verifyFormat("int Add2(BTree * &Root, char * szToAdd)", Style); 2004 } 2005 2006 TEST_F(FormatTest, FormatsForLoop) { 2007 verifyFormat( 2008 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 2009 " ++VeryVeryLongLoopVariable)\n" 2010 " ;"); 2011 verifyFormat("for (;;)\n" 2012 " f();"); 2013 verifyFormat("for (;;) {\n}"); 2014 verifyFormat("for (;;) {\n" 2015 " f();\n" 2016 "}"); 2017 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}"); 2018 2019 verifyFormat( 2020 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 2021 " E = UnwrappedLines.end();\n" 2022 " I != E; ++I) {\n}"); 2023 2024 verifyFormat( 2025 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" 2026 " ++IIIII) {\n}"); 2027 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" 2028 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" 2029 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); 2030 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n" 2031 " I = FD->getDeclsInPrototypeScope().begin(),\n" 2032 " E = FD->getDeclsInPrototypeScope().end();\n" 2033 " I != E; ++I) {\n}"); 2034 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n" 2035 " I = Container.begin(),\n" 2036 " E = Container.end();\n" 2037 " I != E; ++I) {\n}", 2038 getLLVMStyleWithColumns(76)); 2039 2040 verifyFormat( 2041 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 2042 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" 2043 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2044 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 2045 " ++aaaaaaaaaaa) {\n}"); 2046 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2047 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n" 2048 " ++i) {\n}"); 2049 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" 2050 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 2051 "}"); 2052 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n" 2053 " aaaaaaaaaa);\n" 2054 " iter; ++iter) {\n" 2055 "}"); 2056 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2057 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 2058 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n" 2059 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {"); 2060 2061 // These should not be formatted as Objective-C for-in loops. 2062 verifyFormat("for (Foo *x = 0; x != in; x++) {\n}"); 2063 verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}"); 2064 verifyFormat("Foo *x;\nfor (x in y) {\n}"); 2065 verifyFormat( 2066 "for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}"); 2067 2068 FormatStyle NoBinPacking = getLLVMStyle(); 2069 NoBinPacking.BinPackParameters = false; 2070 verifyFormat("for (int aaaaaaaaaaa = 1;\n" 2071 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" 2072 " aaaaaaaaaaaaaaaa,\n" 2073 " aaaaaaaaaaaaaaaa,\n" 2074 " aaaaaaaaaaaaaaaa);\n" 2075 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 2076 "}", 2077 NoBinPacking); 2078 verifyFormat( 2079 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 2080 " E = UnwrappedLines.end();\n" 2081 " I != E;\n" 2082 " ++I) {\n}", 2083 NoBinPacking); 2084 2085 FormatStyle AlignLeft = getLLVMStyle(); 2086 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 2087 verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft); 2088 } 2089 2090 TEST_F(FormatTest, RangeBasedForLoops) { 2091 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 2092 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 2093 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" 2094 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); 2095 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" 2096 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 2097 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n" 2098 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}"); 2099 } 2100 2101 TEST_F(FormatTest, ForEachLoops) { 2102 verifyFormat("void f() {\n" 2103 " foreach (Item *item, itemlist) {}\n" 2104 " Q_FOREACH (Item *item, itemlist) {}\n" 2105 " BOOST_FOREACH (Item *item, itemlist) {}\n" 2106 " UNKNOWN_FORACH(Item * item, itemlist) {}\n" 2107 "}"); 2108 2109 FormatStyle Style = getLLVMStyle(); 2110 Style.SpaceBeforeParens = 2111 FormatStyle::SBPO_ControlStatementsExceptControlMacros; 2112 verifyFormat("void f() {\n" 2113 " foreach(Item *item, itemlist) {}\n" 2114 " Q_FOREACH(Item *item, itemlist) {}\n" 2115 " BOOST_FOREACH(Item *item, itemlist) {}\n" 2116 " UNKNOWN_FORACH(Item * item, itemlist) {}\n" 2117 "}", 2118 Style); 2119 2120 // As function-like macros. 2121 verifyFormat("#define foreach(x, y)\n" 2122 "#define Q_FOREACH(x, y)\n" 2123 "#define BOOST_FOREACH(x, y)\n" 2124 "#define UNKNOWN_FOREACH(x, y)\n"); 2125 2126 // Not as function-like macros. 2127 verifyFormat("#define foreach (x, y)\n" 2128 "#define Q_FOREACH (x, y)\n" 2129 "#define BOOST_FOREACH (x, y)\n" 2130 "#define UNKNOWN_FOREACH (x, y)\n"); 2131 2132 // handle microsoft non standard extension 2133 verifyFormat("for each (char c in x->MyStringProperty)"); 2134 } 2135 2136 TEST_F(FormatTest, FormatsWhileLoop) { 2137 verifyFormat("while (true) {\n}"); 2138 verifyFormat("while (true)\n" 2139 " f();"); 2140 verifyFormat("while () {\n}"); 2141 verifyFormat("while () {\n" 2142 " f();\n" 2143 "}"); 2144 } 2145 2146 TEST_F(FormatTest, FormatsDoWhile) { 2147 verifyFormat("do {\n" 2148 " do_something();\n" 2149 "} while (something());"); 2150 verifyFormat("do\n" 2151 " do_something();\n" 2152 "while (something());"); 2153 } 2154 2155 TEST_F(FormatTest, FormatsSwitchStatement) { 2156 verifyFormat("switch (x) {\n" 2157 "case 1:\n" 2158 " f();\n" 2159 " break;\n" 2160 "case kFoo:\n" 2161 "case ns::kBar:\n" 2162 "case kBaz:\n" 2163 " break;\n" 2164 "default:\n" 2165 " g();\n" 2166 " break;\n" 2167 "}"); 2168 verifyFormat("switch (x) {\n" 2169 "case 1: {\n" 2170 " f();\n" 2171 " break;\n" 2172 "}\n" 2173 "case 2: {\n" 2174 " break;\n" 2175 "}\n" 2176 "}"); 2177 verifyFormat("switch (x) {\n" 2178 "case 1: {\n" 2179 " f();\n" 2180 " {\n" 2181 " g();\n" 2182 " h();\n" 2183 " }\n" 2184 " break;\n" 2185 "}\n" 2186 "}"); 2187 verifyFormat("switch (x) {\n" 2188 "case 1: {\n" 2189 " f();\n" 2190 " if (foo) {\n" 2191 " g();\n" 2192 " h();\n" 2193 " }\n" 2194 " break;\n" 2195 "}\n" 2196 "}"); 2197 verifyFormat("switch (x) {\n" 2198 "case 1: {\n" 2199 " f();\n" 2200 " g();\n" 2201 "} break;\n" 2202 "}"); 2203 verifyFormat("switch (test)\n" 2204 " ;"); 2205 verifyFormat("switch (x) {\n" 2206 "default: {\n" 2207 " // Do nothing.\n" 2208 "}\n" 2209 "}"); 2210 verifyFormat("switch (x) {\n" 2211 "// comment\n" 2212 "// if 1, do f()\n" 2213 "case 1:\n" 2214 " f();\n" 2215 "}"); 2216 verifyFormat("switch (x) {\n" 2217 "case 1:\n" 2218 " // Do amazing stuff\n" 2219 " {\n" 2220 " f();\n" 2221 " g();\n" 2222 " }\n" 2223 " break;\n" 2224 "}"); 2225 verifyFormat("#define A \\\n" 2226 " switch (x) { \\\n" 2227 " case a: \\\n" 2228 " foo = b; \\\n" 2229 " }", 2230 getLLVMStyleWithColumns(20)); 2231 verifyFormat("#define OPERATION_CASE(name) \\\n" 2232 " case OP_name: \\\n" 2233 " return operations::Operation##name\n", 2234 getLLVMStyleWithColumns(40)); 2235 verifyFormat("switch (x) {\n" 2236 "case 1:;\n" 2237 "default:;\n" 2238 " int i;\n" 2239 "}"); 2240 2241 verifyGoogleFormat("switch (x) {\n" 2242 " case 1:\n" 2243 " f();\n" 2244 " break;\n" 2245 " case kFoo:\n" 2246 " case ns::kBar:\n" 2247 " case kBaz:\n" 2248 " break;\n" 2249 " default:\n" 2250 " g();\n" 2251 " break;\n" 2252 "}"); 2253 verifyGoogleFormat("switch (x) {\n" 2254 " case 1: {\n" 2255 " f();\n" 2256 " break;\n" 2257 " }\n" 2258 "}"); 2259 verifyGoogleFormat("switch (test)\n" 2260 " ;"); 2261 2262 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n" 2263 " case OP_name: \\\n" 2264 " return operations::Operation##name\n"); 2265 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n" 2266 " // Get the correction operation class.\n" 2267 " switch (OpCode) {\n" 2268 " CASE(Add);\n" 2269 " CASE(Subtract);\n" 2270 " default:\n" 2271 " return operations::Unknown;\n" 2272 " }\n" 2273 "#undef OPERATION_CASE\n" 2274 "}"); 2275 verifyFormat("DEBUG({\n" 2276 " switch (x) {\n" 2277 " case A:\n" 2278 " f();\n" 2279 " break;\n" 2280 " // fallthrough\n" 2281 " case B:\n" 2282 " g();\n" 2283 " break;\n" 2284 " }\n" 2285 "});"); 2286 EXPECT_EQ("DEBUG({\n" 2287 " switch (x) {\n" 2288 " case A:\n" 2289 " f();\n" 2290 " break;\n" 2291 " // On B:\n" 2292 " case B:\n" 2293 " g();\n" 2294 " break;\n" 2295 " }\n" 2296 "});", 2297 format("DEBUG({\n" 2298 " switch (x) {\n" 2299 " case A:\n" 2300 " f();\n" 2301 " break;\n" 2302 " // On B:\n" 2303 " case B:\n" 2304 " g();\n" 2305 " break;\n" 2306 " }\n" 2307 "});", 2308 getLLVMStyle())); 2309 EXPECT_EQ("switch (n) {\n" 2310 "case 0: {\n" 2311 " return false;\n" 2312 "}\n" 2313 "default: {\n" 2314 " return true;\n" 2315 "}\n" 2316 "}", 2317 format("switch (n)\n" 2318 "{\n" 2319 "case 0: {\n" 2320 " return false;\n" 2321 "}\n" 2322 "default: {\n" 2323 " return true;\n" 2324 "}\n" 2325 "}", 2326 getLLVMStyle())); 2327 verifyFormat("switch (a) {\n" 2328 "case (b):\n" 2329 " return;\n" 2330 "}"); 2331 2332 verifyFormat("switch (a) {\n" 2333 "case some_namespace::\n" 2334 " some_constant:\n" 2335 " return;\n" 2336 "}", 2337 getLLVMStyleWithColumns(34)); 2338 2339 FormatStyle Style = getLLVMStyle(); 2340 Style.IndentCaseLabels = true; 2341 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 2342 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2343 Style.BraceWrapping.AfterCaseLabel = true; 2344 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 2345 EXPECT_EQ("switch (n)\n" 2346 "{\n" 2347 " case 0:\n" 2348 " {\n" 2349 " return false;\n" 2350 " }\n" 2351 " default:\n" 2352 " {\n" 2353 " return true;\n" 2354 " }\n" 2355 "}", 2356 format("switch (n) {\n" 2357 " case 0: {\n" 2358 " return false;\n" 2359 " }\n" 2360 " default: {\n" 2361 " return true;\n" 2362 " }\n" 2363 "}", 2364 Style)); 2365 Style.BraceWrapping.AfterCaseLabel = false; 2366 EXPECT_EQ("switch (n)\n" 2367 "{\n" 2368 " case 0: {\n" 2369 " return false;\n" 2370 " }\n" 2371 " default: {\n" 2372 " return true;\n" 2373 " }\n" 2374 "}", 2375 format("switch (n) {\n" 2376 " case 0:\n" 2377 " {\n" 2378 " return false;\n" 2379 " }\n" 2380 " default:\n" 2381 " {\n" 2382 " return true;\n" 2383 " }\n" 2384 "}", 2385 Style)); 2386 Style.IndentCaseLabels = false; 2387 Style.IndentCaseBlocks = true; 2388 EXPECT_EQ("switch (n)\n" 2389 "{\n" 2390 "case 0:\n" 2391 " {\n" 2392 " return false;\n" 2393 " }\n" 2394 "case 1:\n" 2395 " break;\n" 2396 "default:\n" 2397 " {\n" 2398 " return true;\n" 2399 " }\n" 2400 "}", 2401 format("switch (n) {\n" 2402 "case 0: {\n" 2403 " return false;\n" 2404 "}\n" 2405 "case 1:\n" 2406 " break;\n" 2407 "default: {\n" 2408 " return true;\n" 2409 "}\n" 2410 "}", 2411 Style)); 2412 Style.IndentCaseLabels = true; 2413 Style.IndentCaseBlocks = true; 2414 EXPECT_EQ("switch (n)\n" 2415 "{\n" 2416 " case 0:\n" 2417 " {\n" 2418 " return false;\n" 2419 " }\n" 2420 " case 1:\n" 2421 " break;\n" 2422 " default:\n" 2423 " {\n" 2424 " return true;\n" 2425 " }\n" 2426 "}", 2427 format("switch (n) {\n" 2428 "case 0: {\n" 2429 " return false;\n" 2430 "}\n" 2431 "case 1:\n" 2432 " break;\n" 2433 "default: {\n" 2434 " return true;\n" 2435 "}\n" 2436 "}", 2437 Style)); 2438 } 2439 2440 TEST_F(FormatTest, CaseRanges) { 2441 verifyFormat("switch (x) {\n" 2442 "case 'A' ... 'Z':\n" 2443 "case 1 ... 5:\n" 2444 "case a ... b:\n" 2445 " break;\n" 2446 "}"); 2447 } 2448 2449 TEST_F(FormatTest, ShortEnums) { 2450 FormatStyle Style = getLLVMStyle(); 2451 Style.AllowShortEnumsOnASingleLine = true; 2452 verifyFormat("enum { A, B, C } ShortEnum1, ShortEnum2;", Style); 2453 Style.AllowShortEnumsOnASingleLine = false; 2454 verifyFormat("enum {\n" 2455 " A,\n" 2456 " B,\n" 2457 " C\n" 2458 "} ShortEnum1, ShortEnum2;", 2459 Style); 2460 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2461 Style.BraceWrapping.AfterEnum = true; 2462 verifyFormat("enum\n" 2463 "{\n" 2464 " A,\n" 2465 " B,\n" 2466 " C\n" 2467 "} ShortEnum1, ShortEnum2;", 2468 Style); 2469 } 2470 2471 TEST_F(FormatTest, ShortCaseLabels) { 2472 FormatStyle Style = getLLVMStyle(); 2473 Style.AllowShortCaseLabelsOnASingleLine = true; 2474 verifyFormat("switch (a) {\n" 2475 "case 1: x = 1; break;\n" 2476 "case 2: return;\n" 2477 "case 3:\n" 2478 "case 4:\n" 2479 "case 5: return;\n" 2480 "case 6: // comment\n" 2481 " return;\n" 2482 "case 7:\n" 2483 " // comment\n" 2484 " return;\n" 2485 "case 8:\n" 2486 " x = 8; // comment\n" 2487 " break;\n" 2488 "default: y = 1; break;\n" 2489 "}", 2490 Style); 2491 verifyFormat("switch (a) {\n" 2492 "case 0: return; // comment\n" 2493 "case 1: break; // comment\n" 2494 "case 2: return;\n" 2495 "// comment\n" 2496 "case 3: return;\n" 2497 "// comment 1\n" 2498 "// comment 2\n" 2499 "// comment 3\n" 2500 "case 4: break; /* comment */\n" 2501 "case 5:\n" 2502 " // comment\n" 2503 " break;\n" 2504 "case 6: /* comment */ x = 1; break;\n" 2505 "case 7: x = /* comment */ 1; break;\n" 2506 "case 8:\n" 2507 " x = 1; /* comment */\n" 2508 " break;\n" 2509 "case 9:\n" 2510 " break; // comment line 1\n" 2511 " // comment line 2\n" 2512 "}", 2513 Style); 2514 EXPECT_EQ("switch (a) {\n" 2515 "case 1:\n" 2516 " x = 8;\n" 2517 " // fall through\n" 2518 "case 2: x = 8;\n" 2519 "// comment\n" 2520 "case 3:\n" 2521 " return; /* comment line 1\n" 2522 " * comment line 2 */\n" 2523 "case 4: i = 8;\n" 2524 "// something else\n" 2525 "#if FOO\n" 2526 "case 5: break;\n" 2527 "#endif\n" 2528 "}", 2529 format("switch (a) {\n" 2530 "case 1: x = 8;\n" 2531 " // fall through\n" 2532 "case 2:\n" 2533 " x = 8;\n" 2534 "// comment\n" 2535 "case 3:\n" 2536 " return; /* comment line 1\n" 2537 " * comment line 2 */\n" 2538 "case 4:\n" 2539 " i = 8;\n" 2540 "// something else\n" 2541 "#if FOO\n" 2542 "case 5: break;\n" 2543 "#endif\n" 2544 "}", 2545 Style)); 2546 EXPECT_EQ("switch (a) {\n" 2547 "case 0:\n" 2548 " return; // long long long long long long long long long long " 2549 "long long comment\n" 2550 " // line\n" 2551 "}", 2552 format("switch (a) {\n" 2553 "case 0: return; // long long long long long long long long " 2554 "long long long long comment line\n" 2555 "}", 2556 Style)); 2557 EXPECT_EQ("switch (a) {\n" 2558 "case 0:\n" 2559 " return; /* long long long long long long long long long long " 2560 "long long comment\n" 2561 " line */\n" 2562 "}", 2563 format("switch (a) {\n" 2564 "case 0: return; /* long long long long long long long long " 2565 "long long long long comment line */\n" 2566 "}", 2567 Style)); 2568 verifyFormat("switch (a) {\n" 2569 "#if FOO\n" 2570 "case 0: return 0;\n" 2571 "#endif\n" 2572 "}", 2573 Style); 2574 verifyFormat("switch (a) {\n" 2575 "case 1: {\n" 2576 "}\n" 2577 "case 2: {\n" 2578 " return;\n" 2579 "}\n" 2580 "case 3: {\n" 2581 " x = 1;\n" 2582 " return;\n" 2583 "}\n" 2584 "case 4:\n" 2585 " if (x)\n" 2586 " return;\n" 2587 "}", 2588 Style); 2589 Style.ColumnLimit = 21; 2590 verifyFormat("switch (a) {\n" 2591 "case 1: x = 1; break;\n" 2592 "case 2: return;\n" 2593 "case 3:\n" 2594 "case 4:\n" 2595 "case 5: return;\n" 2596 "default:\n" 2597 " y = 1;\n" 2598 " break;\n" 2599 "}", 2600 Style); 2601 Style.ColumnLimit = 80; 2602 Style.AllowShortCaseLabelsOnASingleLine = false; 2603 Style.IndentCaseLabels = true; 2604 EXPECT_EQ("switch (n) {\n" 2605 " default /*comments*/:\n" 2606 " return true;\n" 2607 " case 0:\n" 2608 " return false;\n" 2609 "}", 2610 format("switch (n) {\n" 2611 "default/*comments*/:\n" 2612 " return true;\n" 2613 "case 0:\n" 2614 " return false;\n" 2615 "}", 2616 Style)); 2617 Style.AllowShortCaseLabelsOnASingleLine = true; 2618 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2619 Style.BraceWrapping.AfterCaseLabel = true; 2620 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 2621 EXPECT_EQ("switch (n)\n" 2622 "{\n" 2623 " case 0:\n" 2624 " {\n" 2625 " return false;\n" 2626 " }\n" 2627 " default:\n" 2628 " {\n" 2629 " return true;\n" 2630 " }\n" 2631 "}", 2632 format("switch (n) {\n" 2633 " case 0: {\n" 2634 " return false;\n" 2635 " }\n" 2636 " default:\n" 2637 " {\n" 2638 " return true;\n" 2639 " }\n" 2640 "}", 2641 Style)); 2642 } 2643 2644 TEST_F(FormatTest, FormatsLabels) { 2645 verifyFormat("void f() {\n" 2646 " some_code();\n" 2647 "test_label:\n" 2648 " some_other_code();\n" 2649 " {\n" 2650 " some_more_code();\n" 2651 " another_label:\n" 2652 " some_more_code();\n" 2653 " }\n" 2654 "}"); 2655 verifyFormat("{\n" 2656 " some_code();\n" 2657 "test_label:\n" 2658 " some_other_code();\n" 2659 "}"); 2660 verifyFormat("{\n" 2661 " some_code();\n" 2662 "test_label:;\n" 2663 " int i = 0;\n" 2664 "}"); 2665 FormatStyle Style = getLLVMStyle(); 2666 Style.IndentGotoLabels = false; 2667 verifyFormat("void f() {\n" 2668 " some_code();\n" 2669 "test_label:\n" 2670 " some_other_code();\n" 2671 " {\n" 2672 " some_more_code();\n" 2673 "another_label:\n" 2674 " some_more_code();\n" 2675 " }\n" 2676 "}", 2677 Style); 2678 verifyFormat("{\n" 2679 " some_code();\n" 2680 "test_label:\n" 2681 " some_other_code();\n" 2682 "}", 2683 Style); 2684 verifyFormat("{\n" 2685 " some_code();\n" 2686 "test_label:;\n" 2687 " int i = 0;\n" 2688 "}"); 2689 } 2690 2691 TEST_F(FormatTest, MultiLineControlStatements) { 2692 FormatStyle Style = getLLVMStyle(); 2693 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom; 2694 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine; 2695 Style.ColumnLimit = 20; 2696 // Short lines should keep opening brace on same line. 2697 EXPECT_EQ("if (foo) {\n" 2698 " bar();\n" 2699 "}", 2700 format("if(foo){bar();}", Style)); 2701 EXPECT_EQ("if (foo) {\n" 2702 " bar();\n" 2703 "} else {\n" 2704 " baz();\n" 2705 "}", 2706 format("if(foo){bar();}else{baz();}", Style)); 2707 EXPECT_EQ("if (foo && bar) {\n" 2708 " baz();\n" 2709 "}", 2710 format("if(foo&&bar){baz();}", Style)); 2711 EXPECT_EQ("if (foo) {\n" 2712 " bar();\n" 2713 "} else if (baz) {\n" 2714 " quux();\n" 2715 "}", 2716 format("if(foo){bar();}else if(baz){quux();}", Style)); 2717 EXPECT_EQ( 2718 "if (foo) {\n" 2719 " bar();\n" 2720 "} else if (baz) {\n" 2721 " quux();\n" 2722 "} else {\n" 2723 " foobar();\n" 2724 "}", 2725 format("if(foo){bar();}else if(baz){quux();}else{foobar();}", Style)); 2726 EXPECT_EQ("for (;;) {\n" 2727 " foo();\n" 2728 "}", 2729 format("for(;;){foo();}")); 2730 EXPECT_EQ("while (1) {\n" 2731 " foo();\n" 2732 "}", 2733 format("while(1){foo();}", Style)); 2734 EXPECT_EQ("switch (foo) {\n" 2735 "case bar:\n" 2736 " return;\n" 2737 "}", 2738 format("switch(foo){case bar:return;}", Style)); 2739 EXPECT_EQ("try {\n" 2740 " foo();\n" 2741 "} catch (...) {\n" 2742 " bar();\n" 2743 "}", 2744 format("try{foo();}catch(...){bar();}", Style)); 2745 EXPECT_EQ("do {\n" 2746 " foo();\n" 2747 "} while (bar &&\n" 2748 " baz);", 2749 format("do{foo();}while(bar&&baz);", Style)); 2750 // Long lines should put opening brace on new line. 2751 EXPECT_EQ("if (foo && bar &&\n" 2752 " baz)\n" 2753 "{\n" 2754 " quux();\n" 2755 "}", 2756 format("if(foo&&bar&&baz){quux();}", Style)); 2757 EXPECT_EQ("if (foo && bar &&\n" 2758 " baz)\n" 2759 "{\n" 2760 " quux();\n" 2761 "}", 2762 format("if (foo && bar &&\n" 2763 " baz) {\n" 2764 " quux();\n" 2765 "}", 2766 Style)); 2767 EXPECT_EQ("if (foo) {\n" 2768 " bar();\n" 2769 "} else if (baz ||\n" 2770 " quux)\n" 2771 "{\n" 2772 " foobar();\n" 2773 "}", 2774 format("if(foo){bar();}else if(baz||quux){foobar();}", Style)); 2775 EXPECT_EQ( 2776 "if (foo) {\n" 2777 " bar();\n" 2778 "} else if (baz ||\n" 2779 " quux)\n" 2780 "{\n" 2781 " foobar();\n" 2782 "} else {\n" 2783 " barbaz();\n" 2784 "}", 2785 format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}", 2786 Style)); 2787 EXPECT_EQ("for (int i = 0;\n" 2788 " i < 10; ++i)\n" 2789 "{\n" 2790 " foo();\n" 2791 "}", 2792 format("for(int i=0;i<10;++i){foo();}", Style)); 2793 EXPECT_EQ("foreach (int i,\n" 2794 " list)\n" 2795 "{\n" 2796 " foo();\n" 2797 "}", 2798 format("foreach(int i, list){foo();}", Style)); 2799 Style.ColumnLimit = 2800 40; // to concentrate at brace wrapping, not line wrap due to column limit 2801 EXPECT_EQ("foreach (int i, list) {\n" 2802 " foo();\n" 2803 "}", 2804 format("foreach(int i, list){foo();}", Style)); 2805 Style.ColumnLimit = 2806 20; // to concentrate at brace wrapping, not line wrap due to column limit 2807 EXPECT_EQ("while (foo || bar ||\n" 2808 " baz)\n" 2809 "{\n" 2810 " quux();\n" 2811 "}", 2812 format("while(foo||bar||baz){quux();}", Style)); 2813 EXPECT_EQ("switch (\n" 2814 " foo = barbaz)\n" 2815 "{\n" 2816 "case quux:\n" 2817 " return;\n" 2818 "}", 2819 format("switch(foo=barbaz){case quux:return;}", Style)); 2820 EXPECT_EQ("try {\n" 2821 " foo();\n" 2822 "} catch (\n" 2823 " Exception &bar)\n" 2824 "{\n" 2825 " baz();\n" 2826 "}", 2827 format("try{foo();}catch(Exception&bar){baz();}", Style)); 2828 Style.ColumnLimit = 2829 40; // to concentrate at brace wrapping, not line wrap due to column limit 2830 EXPECT_EQ("try {\n" 2831 " foo();\n" 2832 "} catch (Exception &bar) {\n" 2833 " baz();\n" 2834 "}", 2835 format("try{foo();}catch(Exception&bar){baz();}", Style)); 2836 Style.ColumnLimit = 2837 20; // to concentrate at brace wrapping, not line wrap due to column limit 2838 2839 Style.BraceWrapping.BeforeElse = true; 2840 EXPECT_EQ( 2841 "if (foo) {\n" 2842 " bar();\n" 2843 "}\n" 2844 "else if (baz ||\n" 2845 " quux)\n" 2846 "{\n" 2847 " foobar();\n" 2848 "}\n" 2849 "else {\n" 2850 " barbaz();\n" 2851 "}", 2852 format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}", 2853 Style)); 2854 2855 Style.BraceWrapping.BeforeCatch = true; 2856 EXPECT_EQ("try {\n" 2857 " foo();\n" 2858 "}\n" 2859 "catch (...) {\n" 2860 " baz();\n" 2861 "}", 2862 format("try{foo();}catch(...){baz();}", Style)); 2863 } 2864 2865 TEST_F(FormatTest, BeforeWhile) { 2866 FormatStyle Style = getLLVMStyle(); 2867 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom; 2868 2869 verifyFormat("do {\n" 2870 " foo();\n" 2871 "} while (1);", 2872 Style); 2873 Style.BraceWrapping.BeforeWhile = true; 2874 verifyFormat("do {\n" 2875 " foo();\n" 2876 "}\n" 2877 "while (1);", 2878 Style); 2879 } 2880 2881 //===----------------------------------------------------------------------===// 2882 // Tests for classes, namespaces, etc. 2883 //===----------------------------------------------------------------------===// 2884 2885 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 2886 verifyFormat("class A {};"); 2887 } 2888 2889 TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 2890 verifyFormat("class A {\n" 2891 "public:\n" 2892 "public: // comment\n" 2893 "protected:\n" 2894 "private:\n" 2895 " void f() {}\n" 2896 "};"); 2897 verifyFormat("export class A {\n" 2898 "public:\n" 2899 "public: // comment\n" 2900 "protected:\n" 2901 "private:\n" 2902 " void f() {}\n" 2903 "};"); 2904 verifyGoogleFormat("class A {\n" 2905 " public:\n" 2906 " protected:\n" 2907 " private:\n" 2908 " void f() {}\n" 2909 "};"); 2910 verifyGoogleFormat("export class A {\n" 2911 " public:\n" 2912 " protected:\n" 2913 " private:\n" 2914 " void f() {}\n" 2915 "};"); 2916 verifyFormat("class A {\n" 2917 "public slots:\n" 2918 " void f1() {}\n" 2919 "public Q_SLOTS:\n" 2920 " void f2() {}\n" 2921 "protected slots:\n" 2922 " void f3() {}\n" 2923 "protected Q_SLOTS:\n" 2924 " void f4() {}\n" 2925 "private slots:\n" 2926 " void f5() {}\n" 2927 "private Q_SLOTS:\n" 2928 " void f6() {}\n" 2929 "signals:\n" 2930 " void g1();\n" 2931 "Q_SIGNALS:\n" 2932 " void g2();\n" 2933 "};"); 2934 2935 // Don't interpret 'signals' the wrong way. 2936 verifyFormat("signals.set();"); 2937 verifyFormat("for (Signals signals : f()) {\n}"); 2938 verifyFormat("{\n" 2939 " signals.set(); // This needs indentation.\n" 2940 "}"); 2941 verifyFormat("void f() {\n" 2942 "label:\n" 2943 " signals.baz();\n" 2944 "}"); 2945 } 2946 2947 TEST_F(FormatTest, SeparatesLogicalBlocks) { 2948 EXPECT_EQ("class A {\n" 2949 "public:\n" 2950 " void f();\n" 2951 "\n" 2952 "private:\n" 2953 " void g() {}\n" 2954 " // test\n" 2955 "protected:\n" 2956 " int h;\n" 2957 "};", 2958 format("class A {\n" 2959 "public:\n" 2960 "void f();\n" 2961 "private:\n" 2962 "void g() {}\n" 2963 "// test\n" 2964 "protected:\n" 2965 "int h;\n" 2966 "};")); 2967 EXPECT_EQ("class A {\n" 2968 "protected:\n" 2969 "public:\n" 2970 " void f();\n" 2971 "};", 2972 format("class A {\n" 2973 "protected:\n" 2974 "\n" 2975 "public:\n" 2976 "\n" 2977 " void f();\n" 2978 "};")); 2979 2980 // Even ensure proper spacing inside macros. 2981 EXPECT_EQ("#define B \\\n" 2982 " class A { \\\n" 2983 " protected: \\\n" 2984 " public: \\\n" 2985 " void f(); \\\n" 2986 " };", 2987 format("#define B \\\n" 2988 " class A { \\\n" 2989 " protected: \\\n" 2990 " \\\n" 2991 " public: \\\n" 2992 " \\\n" 2993 " void f(); \\\n" 2994 " };", 2995 getGoogleStyle())); 2996 // But don't remove empty lines after macros ending in access specifiers. 2997 EXPECT_EQ("#define A private:\n" 2998 "\n" 2999 "int i;", 3000 format("#define A private:\n" 3001 "\n" 3002 "int i;")); 3003 } 3004 3005 TEST_F(FormatTest, FormatsClasses) { 3006 verifyFormat("class A : public B {};"); 3007 verifyFormat("class A : public ::B {};"); 3008 3009 verifyFormat( 3010 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3011 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 3012 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 3013 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3014 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 3015 verifyFormat( 3016 "class A : public B, public C, public D, public E, public F {};"); 3017 verifyFormat("class AAAAAAAAAAAA : public B,\n" 3018 " public C,\n" 3019 " public D,\n" 3020 " public E,\n" 3021 " public F,\n" 3022 " public G {};"); 3023 3024 verifyFormat("class\n" 3025 " ReallyReallyLongClassName {\n" 3026 " int i;\n" 3027 "};", 3028 getLLVMStyleWithColumns(32)); 3029 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 3030 " aaaaaaaaaaaaaaaa> {};"); 3031 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" 3032 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n" 3033 " aaaaaaaaaaaaaaaaaaaaaa> {};"); 3034 verifyFormat("template <class R, class C>\n" 3035 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n" 3036 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};"); 3037 verifyFormat("class ::A::B {};"); 3038 } 3039 3040 TEST_F(FormatTest, BreakInheritanceStyle) { 3041 FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle(); 3042 StyleWithInheritanceBreakBeforeComma.BreakInheritanceList = 3043 FormatStyle::BILS_BeforeComma; 3044 verifyFormat("class MyClass : public X {};", 3045 StyleWithInheritanceBreakBeforeComma); 3046 verifyFormat("class MyClass\n" 3047 " : public X\n" 3048 " , public Y {};", 3049 StyleWithInheritanceBreakBeforeComma); 3050 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n" 3051 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n" 3052 " , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", 3053 StyleWithInheritanceBreakBeforeComma); 3054 verifyFormat("struct aaaaaaaaaaaaa\n" 3055 " : public aaaaaaaaaaaaaaaaaaa< // break\n" 3056 " aaaaaaaaaaaaaaaa> {};", 3057 StyleWithInheritanceBreakBeforeComma); 3058 3059 FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle(); 3060 StyleWithInheritanceBreakAfterColon.BreakInheritanceList = 3061 FormatStyle::BILS_AfterColon; 3062 verifyFormat("class MyClass : public X {};", 3063 StyleWithInheritanceBreakAfterColon); 3064 verifyFormat("class MyClass : public X, public Y {};", 3065 StyleWithInheritanceBreakAfterColon); 3066 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n" 3067 " public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3068 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", 3069 StyleWithInheritanceBreakAfterColon); 3070 verifyFormat("struct aaaaaaaaaaaaa :\n" 3071 " public aaaaaaaaaaaaaaaaaaa< // break\n" 3072 " aaaaaaaaaaaaaaaa> {};", 3073 StyleWithInheritanceBreakAfterColon); 3074 3075 FormatStyle StyleWithInheritanceBreakAfterComma = getLLVMStyle(); 3076 StyleWithInheritanceBreakAfterComma.BreakInheritanceList = 3077 FormatStyle::BILS_AfterComma; 3078 verifyFormat("class MyClass : public X {};", 3079 StyleWithInheritanceBreakAfterComma); 3080 verifyFormat("class MyClass : public X,\n" 3081 " public Y {};", 3082 StyleWithInheritanceBreakAfterComma); 3083 verifyFormat( 3084 "class AAAAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3085 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC " 3086 "{};", 3087 StyleWithInheritanceBreakAfterComma); 3088 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 3089 " aaaaaaaaaaaaaaaa> {};", 3090 StyleWithInheritanceBreakAfterComma); 3091 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 3092 " : public OnceBreak,\n" 3093 " public AlwaysBreak,\n" 3094 " EvenBasesFitInOneLine {};", 3095 StyleWithInheritanceBreakAfterComma); 3096 } 3097 3098 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { 3099 verifyFormat("class A {\n} a, b;"); 3100 verifyFormat("struct A {\n} a, b;"); 3101 verifyFormat("union A {\n} a;"); 3102 } 3103 3104 TEST_F(FormatTest, FormatsEnum) { 3105 verifyFormat("enum {\n" 3106 " Zero,\n" 3107 " One = 1,\n" 3108 " Two = One + 1,\n" 3109 " Three = (One + Two),\n" 3110 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3111 " Five = (One, Two, Three, Four, 5)\n" 3112 "};"); 3113 verifyGoogleFormat("enum {\n" 3114 " Zero,\n" 3115 " One = 1,\n" 3116 " Two = One + 1,\n" 3117 " Three = (One + Two),\n" 3118 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3119 " Five = (One, Two, Three, Four, 5)\n" 3120 "};"); 3121 verifyFormat("enum Enum {};"); 3122 verifyFormat("enum {};"); 3123 verifyFormat("enum X E {} d;"); 3124 verifyFormat("enum __attribute__((...)) E {} d;"); 3125 verifyFormat("enum __declspec__((...)) E {} d;"); 3126 verifyFormat("enum {\n" 3127 " Bar = Foo<int, int>::value\n" 3128 "};", 3129 getLLVMStyleWithColumns(30)); 3130 3131 verifyFormat("enum ShortEnum { A, B, C };"); 3132 verifyGoogleFormat("enum ShortEnum { A, B, C };"); 3133 3134 EXPECT_EQ("enum KeepEmptyLines {\n" 3135 " ONE,\n" 3136 "\n" 3137 " TWO,\n" 3138 "\n" 3139 " THREE\n" 3140 "}", 3141 format("enum KeepEmptyLines {\n" 3142 " ONE,\n" 3143 "\n" 3144 " TWO,\n" 3145 "\n" 3146 "\n" 3147 " THREE\n" 3148 "}")); 3149 verifyFormat("enum E { // comment\n" 3150 " ONE,\n" 3151 " TWO\n" 3152 "};\n" 3153 "int i;"); 3154 3155 FormatStyle EightIndent = getLLVMStyle(); 3156 EightIndent.IndentWidth = 8; 3157 verifyFormat("enum {\n" 3158 " VOID,\n" 3159 " CHAR,\n" 3160 " SHORT,\n" 3161 " INT,\n" 3162 " LONG,\n" 3163 " SIGNED,\n" 3164 " UNSIGNED,\n" 3165 " BOOL,\n" 3166 " FLOAT,\n" 3167 " DOUBLE,\n" 3168 " COMPLEX\n" 3169 "};", 3170 EightIndent); 3171 3172 // Not enums. 3173 verifyFormat("enum X f() {\n" 3174 " a();\n" 3175 " return 42;\n" 3176 "}"); 3177 verifyFormat("enum X Type::f() {\n" 3178 " a();\n" 3179 " return 42;\n" 3180 "}"); 3181 verifyFormat("enum ::X f() {\n" 3182 " a();\n" 3183 " return 42;\n" 3184 "}"); 3185 verifyFormat("enum ns::X f() {\n" 3186 " a();\n" 3187 " return 42;\n" 3188 "}"); 3189 } 3190 3191 TEST_F(FormatTest, FormatsEnumsWithErrors) { 3192 verifyFormat("enum Type {\n" 3193 " One = 0; // These semicolons should be commas.\n" 3194 " Two = 1;\n" 3195 "};"); 3196 verifyFormat("namespace n {\n" 3197 "enum Type {\n" 3198 " One,\n" 3199 " Two, // missing };\n" 3200 " int i;\n" 3201 "}\n" 3202 "void g() {}"); 3203 } 3204 3205 TEST_F(FormatTest, FormatsEnumStruct) { 3206 verifyFormat("enum struct {\n" 3207 " Zero,\n" 3208 " One = 1,\n" 3209 " Two = One + 1,\n" 3210 " Three = (One + Two),\n" 3211 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3212 " Five = (One, Two, Three, Four, 5)\n" 3213 "};"); 3214 verifyFormat("enum struct Enum {};"); 3215 verifyFormat("enum struct {};"); 3216 verifyFormat("enum struct X E {} d;"); 3217 verifyFormat("enum struct __attribute__((...)) E {} d;"); 3218 verifyFormat("enum struct __declspec__((...)) E {} d;"); 3219 verifyFormat("enum struct X f() {\n a();\n return 42;\n}"); 3220 } 3221 3222 TEST_F(FormatTest, FormatsEnumClass) { 3223 verifyFormat("enum class {\n" 3224 " Zero,\n" 3225 " One = 1,\n" 3226 " Two = One + 1,\n" 3227 " Three = (One + Two),\n" 3228 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3229 " Five = (One, Two, Three, Four, 5)\n" 3230 "};"); 3231 verifyFormat("enum class Enum {};"); 3232 verifyFormat("enum class {};"); 3233 verifyFormat("enum class X E {} d;"); 3234 verifyFormat("enum class __attribute__((...)) E {} d;"); 3235 verifyFormat("enum class __declspec__((...)) E {} d;"); 3236 verifyFormat("enum class X f() {\n a();\n return 42;\n}"); 3237 } 3238 3239 TEST_F(FormatTest, FormatsEnumTypes) { 3240 verifyFormat("enum X : int {\n" 3241 " A, // Force multiple lines.\n" 3242 " B\n" 3243 "};"); 3244 verifyFormat("enum X : int { A, B };"); 3245 verifyFormat("enum X : std::uint32_t { A, B };"); 3246 } 3247 3248 TEST_F(FormatTest, FormatsTypedefEnum) { 3249 FormatStyle Style = getLLVMStyle(); 3250 Style.ColumnLimit = 40; 3251 verifyFormat("typedef enum {} EmptyEnum;"); 3252 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 3253 verifyFormat("typedef enum {\n" 3254 " ZERO = 0,\n" 3255 " ONE = 1,\n" 3256 " TWO = 2,\n" 3257 " THREE = 3\n" 3258 "} LongEnum;", 3259 Style); 3260 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3261 Style.BraceWrapping.AfterEnum = true; 3262 verifyFormat("typedef enum {} EmptyEnum;"); 3263 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 3264 verifyFormat("typedef enum\n" 3265 "{\n" 3266 " ZERO = 0,\n" 3267 " ONE = 1,\n" 3268 " TWO = 2,\n" 3269 " THREE = 3\n" 3270 "} LongEnum;", 3271 Style); 3272 } 3273 3274 TEST_F(FormatTest, FormatsNSEnums) { 3275 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }"); 3276 verifyGoogleFormat( 3277 "typedef NS_CLOSED_ENUM(NSInteger, SomeName) { AAA, BBB }"); 3278 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n" 3279 " // Information about someDecentlyLongValue.\n" 3280 " someDecentlyLongValue,\n" 3281 " // Information about anotherDecentlyLongValue.\n" 3282 " anotherDecentlyLongValue,\n" 3283 " // Information about aThirdDecentlyLongValue.\n" 3284 " aThirdDecentlyLongValue\n" 3285 "};"); 3286 verifyGoogleFormat("typedef NS_CLOSED_ENUM(NSInteger, MyType) {\n" 3287 " // Information about someDecentlyLongValue.\n" 3288 " someDecentlyLongValue,\n" 3289 " // Information about anotherDecentlyLongValue.\n" 3290 " anotherDecentlyLongValue,\n" 3291 " // Information about aThirdDecentlyLongValue.\n" 3292 " aThirdDecentlyLongValue\n" 3293 "};"); 3294 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n" 3295 " a = 1,\n" 3296 " b = 2,\n" 3297 " c = 3,\n" 3298 "};"); 3299 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n" 3300 " a = 1,\n" 3301 " b = 2,\n" 3302 " c = 3,\n" 3303 "};"); 3304 verifyGoogleFormat("typedef CF_CLOSED_ENUM(NSInteger, MyType) {\n" 3305 " a = 1,\n" 3306 " b = 2,\n" 3307 " c = 3,\n" 3308 "};"); 3309 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n" 3310 " a = 1,\n" 3311 " b = 2,\n" 3312 " c = 3,\n" 3313 "};"); 3314 } 3315 3316 TEST_F(FormatTest, FormatsBitfields) { 3317 verifyFormat("struct Bitfields {\n" 3318 " unsigned sClass : 8;\n" 3319 " unsigned ValueKind : 2;\n" 3320 "};"); 3321 verifyFormat("struct A {\n" 3322 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" 3323 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" 3324 "};"); 3325 verifyFormat("struct MyStruct {\n" 3326 " uchar data;\n" 3327 " uchar : 8;\n" 3328 " uchar : 8;\n" 3329 " uchar other;\n" 3330 "};"); 3331 FormatStyle Style = getLLVMStyle(); 3332 Style.BitFieldColonSpacing = FormatStyle::BFCS_None; 3333 verifyFormat("struct Bitfields {\n" 3334 " unsigned sClass:8;\n" 3335 " unsigned ValueKind:2;\n" 3336 " uchar other;\n" 3337 "};", 3338 Style); 3339 verifyFormat("struct A {\n" 3340 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1,\n" 3341 " bbbbbbbbbbbbbbbbbbbbbbbbb:2;\n" 3342 "};", 3343 Style); 3344 Style.BitFieldColonSpacing = FormatStyle::BFCS_Before; 3345 verifyFormat("struct Bitfields {\n" 3346 " unsigned sClass :8;\n" 3347 " unsigned ValueKind :2;\n" 3348 " uchar other;\n" 3349 "};", 3350 Style); 3351 Style.BitFieldColonSpacing = FormatStyle::BFCS_After; 3352 verifyFormat("struct Bitfields {\n" 3353 " unsigned sClass: 8;\n" 3354 " unsigned ValueKind: 2;\n" 3355 " uchar other;\n" 3356 "};", 3357 Style); 3358 } 3359 3360 TEST_F(FormatTest, FormatsNamespaces) { 3361 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 3362 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 3363 3364 verifyFormat("namespace some_namespace {\n" 3365 "class A {};\n" 3366 "void f() { f(); }\n" 3367 "}", 3368 LLVMWithNoNamespaceFix); 3369 verifyFormat("namespace N::inline D {\n" 3370 "class A {};\n" 3371 "void f() { f(); }\n" 3372 "}", 3373 LLVMWithNoNamespaceFix); 3374 verifyFormat("namespace N::inline D::E {\n" 3375 "class A {};\n" 3376 "void f() { f(); }\n" 3377 "}", 3378 LLVMWithNoNamespaceFix); 3379 verifyFormat("namespace [[deprecated(\"foo[bar\")]] some_namespace {\n" 3380 "class A {};\n" 3381 "void f() { f(); }\n" 3382 "}", 3383 LLVMWithNoNamespaceFix); 3384 verifyFormat("/* something */ namespace some_namespace {\n" 3385 "class A {};\n" 3386 "void f() { f(); }\n" 3387 "}", 3388 LLVMWithNoNamespaceFix); 3389 verifyFormat("namespace {\n" 3390 "class A {};\n" 3391 "void f() { f(); }\n" 3392 "}", 3393 LLVMWithNoNamespaceFix); 3394 verifyFormat("/* something */ namespace {\n" 3395 "class A {};\n" 3396 "void f() { f(); }\n" 3397 "}", 3398 LLVMWithNoNamespaceFix); 3399 verifyFormat("inline namespace X {\n" 3400 "class A {};\n" 3401 "void f() { f(); }\n" 3402 "}", 3403 LLVMWithNoNamespaceFix); 3404 verifyFormat("/* something */ inline namespace X {\n" 3405 "class A {};\n" 3406 "void f() { f(); }\n" 3407 "}", 3408 LLVMWithNoNamespaceFix); 3409 verifyFormat("export namespace X {\n" 3410 "class A {};\n" 3411 "void f() { f(); }\n" 3412 "}", 3413 LLVMWithNoNamespaceFix); 3414 verifyFormat("using namespace some_namespace;\n" 3415 "class A {};\n" 3416 "void f() { f(); }", 3417 LLVMWithNoNamespaceFix); 3418 3419 // This code is more common than we thought; if we 3420 // layout this correctly the semicolon will go into 3421 // its own line, which is undesirable. 3422 verifyFormat("namespace {};", LLVMWithNoNamespaceFix); 3423 verifyFormat("namespace {\n" 3424 "class A {};\n" 3425 "};", 3426 LLVMWithNoNamespaceFix); 3427 3428 verifyFormat("namespace {\n" 3429 "int SomeVariable = 0; // comment\n" 3430 "} // namespace", 3431 LLVMWithNoNamespaceFix); 3432 EXPECT_EQ("#ifndef HEADER_GUARD\n" 3433 "#define HEADER_GUARD\n" 3434 "namespace my_namespace {\n" 3435 "int i;\n" 3436 "} // my_namespace\n" 3437 "#endif // HEADER_GUARD", 3438 format("#ifndef HEADER_GUARD\n" 3439 " #define HEADER_GUARD\n" 3440 " namespace my_namespace {\n" 3441 "int i;\n" 3442 "} // my_namespace\n" 3443 "#endif // HEADER_GUARD", 3444 LLVMWithNoNamespaceFix)); 3445 3446 EXPECT_EQ("namespace A::B {\n" 3447 "class C {};\n" 3448 "}", 3449 format("namespace A::B {\n" 3450 "class C {};\n" 3451 "}", 3452 LLVMWithNoNamespaceFix)); 3453 3454 FormatStyle Style = getLLVMStyle(); 3455 Style.NamespaceIndentation = FormatStyle::NI_All; 3456 EXPECT_EQ("namespace out {\n" 3457 " int i;\n" 3458 " namespace in {\n" 3459 " int i;\n" 3460 " } // namespace in\n" 3461 "} // namespace out", 3462 format("namespace out {\n" 3463 "int i;\n" 3464 "namespace in {\n" 3465 "int i;\n" 3466 "} // namespace in\n" 3467 "} // namespace out", 3468 Style)); 3469 3470 Style.NamespaceIndentation = FormatStyle::NI_Inner; 3471 EXPECT_EQ("namespace out {\n" 3472 "int i;\n" 3473 "namespace in {\n" 3474 " int i;\n" 3475 "} // namespace in\n" 3476 "} // namespace out", 3477 format("namespace out {\n" 3478 "int i;\n" 3479 "namespace in {\n" 3480 "int i;\n" 3481 "} // namespace in\n" 3482 "} // namespace out", 3483 Style)); 3484 } 3485 3486 TEST_F(FormatTest, NamespaceMacros) { 3487 FormatStyle Style = getLLVMStyle(); 3488 Style.NamespaceMacros.push_back("TESTSUITE"); 3489 3490 verifyFormat("TESTSUITE(A) {\n" 3491 "int foo();\n" 3492 "} // TESTSUITE(A)", 3493 Style); 3494 3495 verifyFormat("TESTSUITE(A, B) {\n" 3496 "int foo();\n" 3497 "} // TESTSUITE(A)", 3498 Style); 3499 3500 // Properly indent according to NamespaceIndentation style 3501 Style.NamespaceIndentation = FormatStyle::NI_All; 3502 verifyFormat("TESTSUITE(A) {\n" 3503 " int foo();\n" 3504 "} // TESTSUITE(A)", 3505 Style); 3506 verifyFormat("TESTSUITE(A) {\n" 3507 " namespace B {\n" 3508 " int foo();\n" 3509 " } // namespace B\n" 3510 "} // TESTSUITE(A)", 3511 Style); 3512 verifyFormat("namespace A {\n" 3513 " TESTSUITE(B) {\n" 3514 " int foo();\n" 3515 " } // TESTSUITE(B)\n" 3516 "} // namespace A", 3517 Style); 3518 3519 Style.NamespaceIndentation = FormatStyle::NI_Inner; 3520 verifyFormat("TESTSUITE(A) {\n" 3521 "TESTSUITE(B) {\n" 3522 " int foo();\n" 3523 "} // TESTSUITE(B)\n" 3524 "} // TESTSUITE(A)", 3525 Style); 3526 verifyFormat("TESTSUITE(A) {\n" 3527 "namespace B {\n" 3528 " int foo();\n" 3529 "} // namespace B\n" 3530 "} // TESTSUITE(A)", 3531 Style); 3532 verifyFormat("namespace A {\n" 3533 "TESTSUITE(B) {\n" 3534 " int foo();\n" 3535 "} // TESTSUITE(B)\n" 3536 "} // namespace A", 3537 Style); 3538 3539 // Properly merge namespace-macros blocks in CompactNamespaces mode 3540 Style.NamespaceIndentation = FormatStyle::NI_None; 3541 Style.CompactNamespaces = true; 3542 verifyFormat("TESTSUITE(A) { TESTSUITE(B) {\n" 3543 "}} // TESTSUITE(A::B)", 3544 Style); 3545 3546 EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n" 3547 "}} // TESTSUITE(out::in)", 3548 format("TESTSUITE(out) {\n" 3549 "TESTSUITE(in) {\n" 3550 "} // TESTSUITE(in)\n" 3551 "} // TESTSUITE(out)", 3552 Style)); 3553 3554 EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n" 3555 "}} // TESTSUITE(out::in)", 3556 format("TESTSUITE(out) {\n" 3557 "TESTSUITE(in) {\n" 3558 "} // TESTSUITE(in)\n" 3559 "} // TESTSUITE(out)", 3560 Style)); 3561 3562 // Do not merge different namespaces/macros 3563 EXPECT_EQ("namespace out {\n" 3564 "TESTSUITE(in) {\n" 3565 "} // TESTSUITE(in)\n" 3566 "} // namespace out", 3567 format("namespace out {\n" 3568 "TESTSUITE(in) {\n" 3569 "} // TESTSUITE(in)\n" 3570 "} // namespace out", 3571 Style)); 3572 EXPECT_EQ("TESTSUITE(out) {\n" 3573 "namespace in {\n" 3574 "} // namespace in\n" 3575 "} // TESTSUITE(out)", 3576 format("TESTSUITE(out) {\n" 3577 "namespace in {\n" 3578 "} // namespace in\n" 3579 "} // TESTSUITE(out)", 3580 Style)); 3581 Style.NamespaceMacros.push_back("FOOBAR"); 3582 EXPECT_EQ("TESTSUITE(out) {\n" 3583 "FOOBAR(in) {\n" 3584 "} // FOOBAR(in)\n" 3585 "} // TESTSUITE(out)", 3586 format("TESTSUITE(out) {\n" 3587 "FOOBAR(in) {\n" 3588 "} // FOOBAR(in)\n" 3589 "} // TESTSUITE(out)", 3590 Style)); 3591 } 3592 3593 TEST_F(FormatTest, FormatsCompactNamespaces) { 3594 FormatStyle Style = getLLVMStyle(); 3595 Style.CompactNamespaces = true; 3596 Style.NamespaceMacros.push_back("TESTSUITE"); 3597 3598 verifyFormat("namespace A { namespace B {\n" 3599 "}} // namespace A::B", 3600 Style); 3601 3602 EXPECT_EQ("namespace out { namespace in {\n" 3603 "}} // namespace out::in", 3604 format("namespace out {\n" 3605 "namespace in {\n" 3606 "} // namespace in\n" 3607 "} // namespace out", 3608 Style)); 3609 3610 // Only namespaces which have both consecutive opening and end get compacted 3611 EXPECT_EQ("namespace out {\n" 3612 "namespace in1 {\n" 3613 "} // namespace in1\n" 3614 "namespace in2 {\n" 3615 "} // namespace in2\n" 3616 "} // namespace out", 3617 format("namespace out {\n" 3618 "namespace in1 {\n" 3619 "} // namespace in1\n" 3620 "namespace in2 {\n" 3621 "} // namespace in2\n" 3622 "} // namespace out", 3623 Style)); 3624 3625 EXPECT_EQ("namespace out {\n" 3626 "int i;\n" 3627 "namespace in {\n" 3628 "int j;\n" 3629 "} // namespace in\n" 3630 "int k;\n" 3631 "} // namespace out", 3632 format("namespace out { int i;\n" 3633 "namespace in { int j; } // namespace in\n" 3634 "int k; } // namespace out", 3635 Style)); 3636 3637 EXPECT_EQ("namespace A { namespace B { namespace C {\n" 3638 "}}} // namespace A::B::C\n", 3639 format("namespace A { namespace B {\n" 3640 "namespace C {\n" 3641 "}} // namespace B::C\n" 3642 "} // namespace A\n", 3643 Style)); 3644 3645 Style.ColumnLimit = 40; 3646 EXPECT_EQ("namespace aaaaaaaaaa {\n" 3647 "namespace bbbbbbbbbb {\n" 3648 "}} // namespace aaaaaaaaaa::bbbbbbbbbb", 3649 format("namespace aaaaaaaaaa {\n" 3650 "namespace bbbbbbbbbb {\n" 3651 "} // namespace bbbbbbbbbb\n" 3652 "} // namespace aaaaaaaaaa", 3653 Style)); 3654 3655 EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n" 3656 "namespace cccccc {\n" 3657 "}}} // namespace aaaaaa::bbbbbb::cccccc", 3658 format("namespace aaaaaa {\n" 3659 "namespace bbbbbb {\n" 3660 "namespace cccccc {\n" 3661 "} // namespace cccccc\n" 3662 "} // namespace bbbbbb\n" 3663 "} // namespace aaaaaa", 3664 Style)); 3665 Style.ColumnLimit = 80; 3666 3667 // Extra semicolon after 'inner' closing brace prevents merging 3668 EXPECT_EQ("namespace out { namespace in {\n" 3669 "}; } // namespace out::in", 3670 format("namespace out {\n" 3671 "namespace in {\n" 3672 "}; // namespace in\n" 3673 "} // namespace out", 3674 Style)); 3675 3676 // Extra semicolon after 'outer' closing brace is conserved 3677 EXPECT_EQ("namespace out { namespace in {\n" 3678 "}}; // namespace out::in", 3679 format("namespace out {\n" 3680 "namespace in {\n" 3681 "} // namespace in\n" 3682 "}; // namespace out", 3683 Style)); 3684 3685 Style.NamespaceIndentation = FormatStyle::NI_All; 3686 EXPECT_EQ("namespace out { namespace in {\n" 3687 " int i;\n" 3688 "}} // namespace out::in", 3689 format("namespace out {\n" 3690 "namespace in {\n" 3691 "int i;\n" 3692 "} // namespace in\n" 3693 "} // namespace out", 3694 Style)); 3695 EXPECT_EQ("namespace out { namespace mid {\n" 3696 " namespace in {\n" 3697 " int j;\n" 3698 " } // namespace in\n" 3699 " int k;\n" 3700 "}} // namespace out::mid", 3701 format("namespace out { namespace mid {\n" 3702 "namespace in { int j; } // namespace in\n" 3703 "int k; }} // namespace out::mid", 3704 Style)); 3705 3706 Style.NamespaceIndentation = FormatStyle::NI_Inner; 3707 EXPECT_EQ("namespace out { namespace in {\n" 3708 " int i;\n" 3709 "}} // namespace out::in", 3710 format("namespace out {\n" 3711 "namespace in {\n" 3712 "int i;\n" 3713 "} // namespace in\n" 3714 "} // namespace out", 3715 Style)); 3716 EXPECT_EQ("namespace out { namespace mid { namespace in {\n" 3717 " int i;\n" 3718 "}}} // namespace out::mid::in", 3719 format("namespace out {\n" 3720 "namespace mid {\n" 3721 "namespace in {\n" 3722 "int i;\n" 3723 "} // namespace in\n" 3724 "} // namespace mid\n" 3725 "} // namespace out", 3726 Style)); 3727 } 3728 3729 TEST_F(FormatTest, FormatsExternC) { 3730 verifyFormat("extern \"C\" {\nint a;"); 3731 verifyFormat("extern \"C\" {}"); 3732 verifyFormat("extern \"C\" {\n" 3733 "int foo();\n" 3734 "}"); 3735 verifyFormat("extern \"C\" int foo() {}"); 3736 verifyFormat("extern \"C\" int foo();"); 3737 verifyFormat("extern \"C\" int foo() {\n" 3738 " int i = 42;\n" 3739 " return i;\n" 3740 "}"); 3741 3742 FormatStyle Style = getLLVMStyle(); 3743 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3744 Style.BraceWrapping.AfterFunction = true; 3745 verifyFormat("extern \"C\" int foo() {}", Style); 3746 verifyFormat("extern \"C\" int foo();", Style); 3747 verifyFormat("extern \"C\" int foo()\n" 3748 "{\n" 3749 " int i = 42;\n" 3750 " return i;\n" 3751 "}", 3752 Style); 3753 3754 Style.BraceWrapping.AfterExternBlock = true; 3755 Style.BraceWrapping.SplitEmptyRecord = false; 3756 verifyFormat("extern \"C\"\n" 3757 "{}", 3758 Style); 3759 verifyFormat("extern \"C\"\n" 3760 "{\n" 3761 " int foo();\n" 3762 "}", 3763 Style); 3764 } 3765 3766 TEST_F(FormatTest, IndentExternBlockStyle) { 3767 FormatStyle Style = getLLVMStyle(); 3768 Style.IndentWidth = 2; 3769 3770 Style.IndentExternBlock = FormatStyle::IEBS_Indent; 3771 verifyFormat("extern \"C\" { /*9*/\n}", Style); 3772 verifyFormat("extern \"C\" {\n" 3773 " int foo10();\n" 3774 "}", 3775 Style); 3776 3777 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; 3778 verifyFormat("extern \"C\" { /*11*/\n}", Style); 3779 verifyFormat("extern \"C\" {\n" 3780 "int foo12();\n" 3781 "}", 3782 Style); 3783 3784 Style.IndentExternBlock = FormatStyle::IEBS_AfterExternBlock; 3785 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3786 Style.BraceWrapping.AfterExternBlock = true; 3787 verifyFormat("extern \"C\"\n{ /*13*/\n}", Style); 3788 verifyFormat("extern \"C\"\n{\n" 3789 " int foo14();\n" 3790 "}", 3791 Style); 3792 3793 Style.IndentExternBlock = FormatStyle::IEBS_AfterExternBlock; 3794 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3795 Style.BraceWrapping.AfterExternBlock = false; 3796 verifyFormat("extern \"C\" { /*15*/\n}", Style); 3797 verifyFormat("extern \"C\" {\n" 3798 "int foo16();\n" 3799 "}", 3800 Style); 3801 } 3802 3803 TEST_F(FormatTest, FormatsInlineASM) { 3804 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); 3805 verifyFormat("asm(\"nop\" ::: \"memory\");"); 3806 verifyFormat( 3807 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 3808 " \"cpuid\\n\\t\"\n" 3809 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 3810 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" 3811 " : \"a\"(value));"); 3812 EXPECT_EQ( 3813 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 3814 " __asm {\n" 3815 " mov edx,[that] // vtable in edx\n" 3816 " mov eax,methodIndex\n" 3817 " call [edx][eax*4] // stdcall\n" 3818 " }\n" 3819 "}", 3820 format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 3821 " __asm {\n" 3822 " mov edx,[that] // vtable in edx\n" 3823 " mov eax,methodIndex\n" 3824 " call [edx][eax*4] // stdcall\n" 3825 " }\n" 3826 "}")); 3827 EXPECT_EQ("_asm {\n" 3828 " xor eax, eax;\n" 3829 " cpuid;\n" 3830 "}", 3831 format("_asm {\n" 3832 " xor eax, eax;\n" 3833 " cpuid;\n" 3834 "}")); 3835 verifyFormat("void function() {\n" 3836 " // comment\n" 3837 " asm(\"\");\n" 3838 "}"); 3839 EXPECT_EQ("__asm {\n" 3840 "}\n" 3841 "int i;", 3842 format("__asm {\n" 3843 "}\n" 3844 "int i;")); 3845 } 3846 3847 TEST_F(FormatTest, FormatTryCatch) { 3848 verifyFormat("try {\n" 3849 " throw a * b;\n" 3850 "} catch (int a) {\n" 3851 " // Do nothing.\n" 3852 "} catch (...) {\n" 3853 " exit(42);\n" 3854 "}"); 3855 3856 // Function-level try statements. 3857 verifyFormat("int f() try { return 4; } catch (...) {\n" 3858 " return 5;\n" 3859 "}"); 3860 verifyFormat("class A {\n" 3861 " int a;\n" 3862 " A() try : a(0) {\n" 3863 " } catch (...) {\n" 3864 " throw;\n" 3865 " }\n" 3866 "};\n"); 3867 verifyFormat("class A {\n" 3868 " int a;\n" 3869 " A() try : a(0), b{1} {\n" 3870 " } catch (...) {\n" 3871 " throw;\n" 3872 " }\n" 3873 "};\n"); 3874 verifyFormat("class A {\n" 3875 " int a;\n" 3876 " A() try : a(0), b{1}, c{2} {\n" 3877 " } catch (...) {\n" 3878 " throw;\n" 3879 " }\n" 3880 "};\n"); 3881 verifyFormat("class A {\n" 3882 " int a;\n" 3883 " A() try : a(0), b{1}, c{2} {\n" 3884 " { // New scope.\n" 3885 " }\n" 3886 " } catch (...) {\n" 3887 " throw;\n" 3888 " }\n" 3889 "};\n"); 3890 3891 // Incomplete try-catch blocks. 3892 verifyIncompleteFormat("try {} catch ("); 3893 } 3894 3895 TEST_F(FormatTest, FormatTryAsAVariable) { 3896 verifyFormat("int try;"); 3897 verifyFormat("int try, size;"); 3898 verifyFormat("try = foo();"); 3899 verifyFormat("if (try < size) {\n return true;\n}"); 3900 3901 verifyFormat("int catch;"); 3902 verifyFormat("int catch, size;"); 3903 verifyFormat("catch = foo();"); 3904 verifyFormat("if (catch < size) {\n return true;\n}"); 3905 3906 FormatStyle Style = getLLVMStyle(); 3907 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3908 Style.BraceWrapping.AfterFunction = true; 3909 Style.BraceWrapping.BeforeCatch = true; 3910 verifyFormat("try {\n" 3911 " int bar = 1;\n" 3912 "}\n" 3913 "catch (...) {\n" 3914 " int bar = 1;\n" 3915 "}", 3916 Style); 3917 verifyFormat("#if NO_EX\n" 3918 "try\n" 3919 "#endif\n" 3920 "{\n" 3921 "}\n" 3922 "#if NO_EX\n" 3923 "catch (...) {\n" 3924 "}", 3925 Style); 3926 verifyFormat("try /* abc */ {\n" 3927 " int bar = 1;\n" 3928 "}\n" 3929 "catch (...) {\n" 3930 " int bar = 1;\n" 3931 "}", 3932 Style); 3933 verifyFormat("try\n" 3934 "// abc\n" 3935 "{\n" 3936 " int bar = 1;\n" 3937 "}\n" 3938 "catch (...) {\n" 3939 " int bar = 1;\n" 3940 "}", 3941 Style); 3942 } 3943 3944 TEST_F(FormatTest, FormatSEHTryCatch) { 3945 verifyFormat("__try {\n" 3946 " int a = b * c;\n" 3947 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n" 3948 " // Do nothing.\n" 3949 "}"); 3950 3951 verifyFormat("__try {\n" 3952 " int a = b * c;\n" 3953 "} __finally {\n" 3954 " // Do nothing.\n" 3955 "}"); 3956 3957 verifyFormat("DEBUG({\n" 3958 " __try {\n" 3959 " } __finally {\n" 3960 " }\n" 3961 "});\n"); 3962 } 3963 3964 TEST_F(FormatTest, IncompleteTryCatchBlocks) { 3965 verifyFormat("try {\n" 3966 " f();\n" 3967 "} catch {\n" 3968 " g();\n" 3969 "}"); 3970 verifyFormat("try {\n" 3971 " f();\n" 3972 "} catch (A a) MACRO(x) {\n" 3973 " g();\n" 3974 "} catch (B b) MACRO(x) {\n" 3975 " g();\n" 3976 "}"); 3977 } 3978 3979 TEST_F(FormatTest, FormatTryCatchBraceStyles) { 3980 FormatStyle Style = getLLVMStyle(); 3981 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, 3982 FormatStyle::BS_WebKit}) { 3983 Style.BreakBeforeBraces = BraceStyle; 3984 verifyFormat("try {\n" 3985 " // something\n" 3986 "} catch (...) {\n" 3987 " // something\n" 3988 "}", 3989 Style); 3990 } 3991 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 3992 verifyFormat("try {\n" 3993 " // something\n" 3994 "}\n" 3995 "catch (...) {\n" 3996 " // something\n" 3997 "}", 3998 Style); 3999 verifyFormat("__try {\n" 4000 " // something\n" 4001 "}\n" 4002 "__finally {\n" 4003 " // something\n" 4004 "}", 4005 Style); 4006 verifyFormat("@try {\n" 4007 " // something\n" 4008 "}\n" 4009 "@finally {\n" 4010 " // something\n" 4011 "}", 4012 Style); 4013 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 4014 verifyFormat("try\n" 4015 "{\n" 4016 " // something\n" 4017 "}\n" 4018 "catch (...)\n" 4019 "{\n" 4020 " // something\n" 4021 "}", 4022 Style); 4023 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 4024 verifyFormat("try\n" 4025 " {\n" 4026 " // something white\n" 4027 " }\n" 4028 "catch (...)\n" 4029 " {\n" 4030 " // something white\n" 4031 " }", 4032 Style); 4033 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 4034 verifyFormat("try\n" 4035 " {\n" 4036 " // something\n" 4037 " }\n" 4038 "catch (...)\n" 4039 " {\n" 4040 " // something\n" 4041 " }", 4042 Style); 4043 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4044 Style.BraceWrapping.BeforeCatch = true; 4045 verifyFormat("try {\n" 4046 " // something\n" 4047 "}\n" 4048 "catch (...) {\n" 4049 " // something\n" 4050 "}", 4051 Style); 4052 } 4053 4054 TEST_F(FormatTest, StaticInitializers) { 4055 verifyFormat("static SomeClass SC = {1, 'a'};"); 4056 4057 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" 4058 " 100000000, " 4059 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); 4060 4061 // Here, everything other than the "}" would fit on a line. 4062 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" 4063 " 10000000000000000000000000};"); 4064 EXPECT_EQ("S s = {a,\n" 4065 "\n" 4066 " b};", 4067 format("S s = {\n" 4068 " a,\n" 4069 "\n" 4070 " b\n" 4071 "};")); 4072 4073 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first 4074 // line. However, the formatting looks a bit off and this probably doesn't 4075 // happen often in practice. 4076 verifyFormat("static int Variable[1] = {\n" 4077 " {1000000000000000000000000000000000000}};", 4078 getLLVMStyleWithColumns(40)); 4079 } 4080 4081 TEST_F(FormatTest, DesignatedInitializers) { 4082 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 4083 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" 4084 " .bbbbbbbbbb = 2,\n" 4085 " .cccccccccc = 3,\n" 4086 " .dddddddddd = 4,\n" 4087 " .eeeeeeeeee = 5};"); 4088 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 4089 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" 4090 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" 4091 " .ccccccccccccccccccccccccccc = 3,\n" 4092 " .ddddddddddddddddddddddddddd = 4,\n" 4093 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); 4094 4095 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); 4096 4097 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 4098 verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n" 4099 " [2] = bbbbbbbbbb,\n" 4100 " [3] = cccccccccc,\n" 4101 " [4] = dddddddddd,\n" 4102 " [5] = eeeeeeeeee};"); 4103 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 4104 " [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4105 " [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 4106 " [3] = cccccccccccccccccccccccccccccccccccccc,\n" 4107 " [4] = dddddddddddddddddddddddddddddddddddddd,\n" 4108 " [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};"); 4109 } 4110 4111 TEST_F(FormatTest, NestedStaticInitializers) { 4112 verifyFormat("static A x = {{{}}};\n"); 4113 verifyFormat("static A x = {{{init1, init2, init3, init4},\n" 4114 " {init1, init2, init3, init4}}};", 4115 getLLVMStyleWithColumns(50)); 4116 4117 verifyFormat("somes Status::global_reps[3] = {\n" 4118 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 4119 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 4120 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", 4121 getLLVMStyleWithColumns(60)); 4122 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n" 4123 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 4124 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 4125 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};"); 4126 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" 4127 " {rect.fRight - rect.fLeft, rect.fBottom - " 4128 "rect.fTop}};"); 4129 4130 verifyFormat( 4131 "SomeArrayOfSomeType a = {\n" 4132 " {{1, 2, 3},\n" 4133 " {1, 2, 3},\n" 4134 " {111111111111111111111111111111, 222222222222222222222222222222,\n" 4135 " 333333333333333333333333333333},\n" 4136 " {1, 2, 3},\n" 4137 " {1, 2, 3}}};"); 4138 verifyFormat( 4139 "SomeArrayOfSomeType a = {\n" 4140 " {{1, 2, 3}},\n" 4141 " {{1, 2, 3}},\n" 4142 " {{111111111111111111111111111111, 222222222222222222222222222222,\n" 4143 " 333333333333333333333333333333}},\n" 4144 " {{1, 2, 3}},\n" 4145 " {{1, 2, 3}}};"); 4146 4147 verifyFormat("struct {\n" 4148 " unsigned bit;\n" 4149 " const char *const name;\n" 4150 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" 4151 " {kOsWin, \"Windows\"},\n" 4152 " {kOsLinux, \"Linux\"},\n" 4153 " {kOsCrOS, \"Chrome OS\"}};"); 4154 verifyFormat("struct {\n" 4155 " unsigned bit;\n" 4156 " const char *const name;\n" 4157 "} kBitsToOs[] = {\n" 4158 " {kOsMac, \"Mac\"},\n" 4159 " {kOsWin, \"Windows\"},\n" 4160 " {kOsLinux, \"Linux\"},\n" 4161 " {kOsCrOS, \"Chrome OS\"},\n" 4162 "};"); 4163 } 4164 4165 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 4166 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 4167 " \\\n" 4168 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 4169 } 4170 4171 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 4172 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 4173 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 4174 4175 // Do break defaulted and deleted functions. 4176 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 4177 " default;", 4178 getLLVMStyleWithColumns(40)); 4179 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 4180 " delete;", 4181 getLLVMStyleWithColumns(40)); 4182 } 4183 4184 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { 4185 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3", 4186 getLLVMStyleWithColumns(40)); 4187 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 4188 getLLVMStyleWithColumns(40)); 4189 EXPECT_EQ("#define Q \\\n" 4190 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" 4191 " \"aaaaaaaa.cpp\"", 4192 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 4193 getLLVMStyleWithColumns(40))); 4194 } 4195 4196 TEST_F(FormatTest, UnderstandsLinePPDirective) { 4197 EXPECT_EQ("# 123 \"A string literal\"", 4198 format(" # 123 \"A string literal\"")); 4199 } 4200 4201 TEST_F(FormatTest, LayoutUnknownPPDirective) { 4202 EXPECT_EQ("#;", format("#;")); 4203 verifyFormat("#\n;\n;\n;"); 4204 } 4205 4206 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 4207 EXPECT_EQ("#line 42 \"test\"\n", 4208 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 4209 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", 4210 getLLVMStyleWithColumns(12))); 4211 } 4212 4213 TEST_F(FormatTest, EndOfFileEndsPPDirective) { 4214 EXPECT_EQ("#line 42 \"test\"", 4215 format("# \\\n line \\\n 42 \\\n \"test\"")); 4216 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); 4217 } 4218 4219 TEST_F(FormatTest, DoesntRemoveUnknownTokens) { 4220 verifyFormat("#define A \\x20"); 4221 verifyFormat("#define A \\ x20"); 4222 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20")); 4223 verifyFormat("#define A ''"); 4224 verifyFormat("#define A ''qqq"); 4225 verifyFormat("#define A `qqq"); 4226 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); 4227 EXPECT_EQ("const char *c = STRINGIFY(\n" 4228 "\\na : b);", 4229 format("const char * c = STRINGIFY(\n" 4230 "\\na : b);")); 4231 4232 verifyFormat("a\r\\"); 4233 verifyFormat("a\v\\"); 4234 verifyFormat("a\f\\"); 4235 } 4236 4237 TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) { 4238 FormatStyle style = getChromiumStyle(FormatStyle::LK_Cpp); 4239 style.IndentWidth = 4; 4240 style.PPIndentWidth = 1; 4241 4242 style.IndentPPDirectives = FormatStyle::PPDIS_None; 4243 verifyFormat("#ifdef __linux__\n" 4244 "void foo() {\n" 4245 " int x = 0;\n" 4246 "}\n" 4247 "#define FOO\n" 4248 "#endif\n" 4249 "void bar() {\n" 4250 " int y = 0;\n" 4251 "}\n", 4252 style); 4253 4254 style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 4255 verifyFormat("#ifdef __linux__\n" 4256 "void foo() {\n" 4257 " int x = 0;\n" 4258 "}\n" 4259 "# define FOO foo\n" 4260 "#endif\n" 4261 "void bar() {\n" 4262 " int y = 0;\n" 4263 "}\n", 4264 style); 4265 4266 style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash; 4267 verifyFormat("#ifdef __linux__\n" 4268 "void foo() {\n" 4269 " int x = 0;\n" 4270 "}\n" 4271 " #define FOO foo\n" 4272 "#endif\n" 4273 "void bar() {\n" 4274 " int y = 0;\n" 4275 "}\n", 4276 style); 4277 } 4278 4279 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 4280 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 4281 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 4282 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 4283 // FIXME: We never break before the macro name. 4284 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12)); 4285 4286 verifyFormat("#define A A\n#define A A"); 4287 verifyFormat("#define A(X) A\n#define A A"); 4288 4289 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 4290 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 4291 } 4292 4293 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 4294 EXPECT_EQ("// somecomment\n" 4295 "#include \"a.h\"\n" 4296 "#define A( \\\n" 4297 " A, B)\n" 4298 "#include \"b.h\"\n" 4299 "// somecomment\n", 4300 format(" // somecomment\n" 4301 " #include \"a.h\"\n" 4302 "#define A(A,\\\n" 4303 " B)\n" 4304 " #include \"b.h\"\n" 4305 " // somecomment\n", 4306 getLLVMStyleWithColumns(13))); 4307 } 4308 4309 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } 4310 4311 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 4312 EXPECT_EQ("#define A \\\n" 4313 " c; \\\n" 4314 " e;\n" 4315 "f;", 4316 format("#define A c; e;\n" 4317 "f;", 4318 getLLVMStyleWithColumns(14))); 4319 } 4320 4321 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } 4322 4323 TEST_F(FormatTest, MacroDefinitionInsideStatement) { 4324 EXPECT_EQ("int x,\n" 4325 "#define A\n" 4326 " y;", 4327 format("int x,\n#define A\ny;")); 4328 } 4329 4330 TEST_F(FormatTest, HashInMacroDefinition) { 4331 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle())); 4332 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 4333 verifyFormat("#define A \\\n" 4334 " { \\\n" 4335 " f(#c); \\\n" 4336 " }", 4337 getLLVMStyleWithColumns(11)); 4338 4339 verifyFormat("#define A(X) \\\n" 4340 " void function##X()", 4341 getLLVMStyleWithColumns(22)); 4342 4343 verifyFormat("#define A(a, b, c) \\\n" 4344 " void a##b##c()", 4345 getLLVMStyleWithColumns(22)); 4346 4347 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 4348 } 4349 4350 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 4351 EXPECT_EQ("#define A (x)", format("#define A (x)")); 4352 EXPECT_EQ("#define A(x)", format("#define A(x)")); 4353 4354 FormatStyle Style = getLLVMStyle(); 4355 Style.SpaceBeforeParens = FormatStyle::SBPO_Never; 4356 verifyFormat("#define true ((foo)1)", Style); 4357 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 4358 verifyFormat("#define false((foo)0)", Style); 4359 } 4360 4361 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 4362 EXPECT_EQ("#define A b;", format("#define A \\\n" 4363 " \\\n" 4364 " b;", 4365 getLLVMStyleWithColumns(25))); 4366 EXPECT_EQ("#define A \\\n" 4367 " \\\n" 4368 " a; \\\n" 4369 " b;", 4370 format("#define A \\\n" 4371 " \\\n" 4372 " a; \\\n" 4373 " b;", 4374 getLLVMStyleWithColumns(11))); 4375 EXPECT_EQ("#define A \\\n" 4376 " a; \\\n" 4377 " \\\n" 4378 " b;", 4379 format("#define A \\\n" 4380 " a; \\\n" 4381 " \\\n" 4382 " b;", 4383 getLLVMStyleWithColumns(11))); 4384 } 4385 4386 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 4387 verifyIncompleteFormat("#define A :"); 4388 verifyFormat("#define SOMECASES \\\n" 4389 " case 1: \\\n" 4390 " case 2\n", 4391 getLLVMStyleWithColumns(20)); 4392 verifyFormat("#define MACRO(a) \\\n" 4393 " if (a) \\\n" 4394 " f(); \\\n" 4395 " else \\\n" 4396 " g()", 4397 getLLVMStyleWithColumns(18)); 4398 verifyFormat("#define A template <typename T>"); 4399 verifyIncompleteFormat("#define STR(x) #x\n" 4400 "f(STR(this_is_a_string_literal{));"); 4401 verifyFormat("#pragma omp threadprivate( \\\n" 4402 " y)), // expected-warning", 4403 getLLVMStyleWithColumns(28)); 4404 verifyFormat("#d, = };"); 4405 verifyFormat("#if \"a"); 4406 verifyIncompleteFormat("({\n" 4407 "#define b \\\n" 4408 " } \\\n" 4409 " a\n" 4410 "a", 4411 getLLVMStyleWithColumns(15)); 4412 verifyFormat("#define A \\\n" 4413 " { \\\n" 4414 " {\n" 4415 "#define B \\\n" 4416 " } \\\n" 4417 " }", 4418 getLLVMStyleWithColumns(15)); 4419 verifyNoCrash("#if a\na(\n#else\n#endif\n{a"); 4420 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}"); 4421 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};"); 4422 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}"); 4423 } 4424 4425 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { 4426 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. 4427 EXPECT_EQ("class A : public QObject {\n" 4428 " Q_OBJECT\n" 4429 "\n" 4430 " A() {}\n" 4431 "};", 4432 format("class A : public QObject {\n" 4433 " Q_OBJECT\n" 4434 "\n" 4435 " A() {\n}\n" 4436 "} ;")); 4437 EXPECT_EQ("MACRO\n" 4438 "/*static*/ int i;", 4439 format("MACRO\n" 4440 " /*static*/ int i;")); 4441 EXPECT_EQ("SOME_MACRO\n" 4442 "namespace {\n" 4443 "void f();\n" 4444 "} // namespace", 4445 format("SOME_MACRO\n" 4446 " namespace {\n" 4447 "void f( );\n" 4448 "} // namespace")); 4449 // Only if the identifier contains at least 5 characters. 4450 EXPECT_EQ("HTTP f();", format("HTTP\nf();")); 4451 EXPECT_EQ("MACRO\nf();", format("MACRO\nf();")); 4452 // Only if everything is upper case. 4453 EXPECT_EQ("class A : public QObject {\n" 4454 " Q_Object A() {}\n" 4455 "};", 4456 format("class A : public QObject {\n" 4457 " Q_Object\n" 4458 " A() {\n}\n" 4459 "} ;")); 4460 4461 // Only if the next line can actually start an unwrapped line. 4462 EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;", 4463 format("SOME_WEIRD_LOG_MACRO\n" 4464 "<< SomeThing;")); 4465 4466 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " 4467 "(n, buffers))\n", 4468 getChromiumStyle(FormatStyle::LK_Cpp)); 4469 4470 // See PR41483 4471 EXPECT_EQ("/**/ FOO(a)\n" 4472 "FOO(b)", 4473 format("/**/ FOO(a)\n" 4474 "FOO(b)")); 4475 } 4476 4477 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { 4478 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 4479 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 4480 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 4481 "class X {};\n" 4482 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 4483 "int *createScopDetectionPass() { return 0; }", 4484 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 4485 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 4486 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 4487 " class X {};\n" 4488 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 4489 " int *createScopDetectionPass() { return 0; }")); 4490 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as 4491 // braces, so that inner block is indented one level more. 4492 EXPECT_EQ("int q() {\n" 4493 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 4494 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 4495 " IPC_END_MESSAGE_MAP()\n" 4496 "}", 4497 format("int q() {\n" 4498 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 4499 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 4500 " IPC_END_MESSAGE_MAP()\n" 4501 "}")); 4502 4503 // Same inside macros. 4504 EXPECT_EQ("#define LIST(L) \\\n" 4505 " L(A) \\\n" 4506 " L(B) \\\n" 4507 " L(C)", 4508 format("#define LIST(L) \\\n" 4509 " L(A) \\\n" 4510 " L(B) \\\n" 4511 " L(C)", 4512 getGoogleStyle())); 4513 4514 // These must not be recognized as macros. 4515 EXPECT_EQ("int q() {\n" 4516 " f(x);\n" 4517 " f(x) {}\n" 4518 " f(x)->g();\n" 4519 " f(x)->*g();\n" 4520 " f(x).g();\n" 4521 " f(x) = x;\n" 4522 " f(x) += x;\n" 4523 " f(x) -= x;\n" 4524 " f(x) *= x;\n" 4525 " f(x) /= x;\n" 4526 " f(x) %= x;\n" 4527 " f(x) &= x;\n" 4528 " f(x) |= x;\n" 4529 " f(x) ^= x;\n" 4530 " f(x) >>= x;\n" 4531 " f(x) <<= x;\n" 4532 " f(x)[y].z();\n" 4533 " LOG(INFO) << x;\n" 4534 " ifstream(x) >> x;\n" 4535 "}\n", 4536 format("int q() {\n" 4537 " f(x)\n;\n" 4538 " f(x)\n {}\n" 4539 " f(x)\n->g();\n" 4540 " f(x)\n->*g();\n" 4541 " f(x)\n.g();\n" 4542 " f(x)\n = x;\n" 4543 " f(x)\n += x;\n" 4544 " f(x)\n -= x;\n" 4545 " f(x)\n *= x;\n" 4546 " f(x)\n /= x;\n" 4547 " f(x)\n %= x;\n" 4548 " f(x)\n &= x;\n" 4549 " f(x)\n |= x;\n" 4550 " f(x)\n ^= x;\n" 4551 " f(x)\n >>= x;\n" 4552 " f(x)\n <<= x;\n" 4553 " f(x)\n[y].z();\n" 4554 " LOG(INFO)\n << x;\n" 4555 " ifstream(x)\n >> x;\n" 4556 "}\n")); 4557 EXPECT_EQ("int q() {\n" 4558 " F(x)\n" 4559 " if (1) {\n" 4560 " }\n" 4561 " F(x)\n" 4562 " while (1) {\n" 4563 " }\n" 4564 " F(x)\n" 4565 " G(x);\n" 4566 " F(x)\n" 4567 " try {\n" 4568 " Q();\n" 4569 " } catch (...) {\n" 4570 " }\n" 4571 "}\n", 4572 format("int q() {\n" 4573 "F(x)\n" 4574 "if (1) {}\n" 4575 "F(x)\n" 4576 "while (1) {}\n" 4577 "F(x)\n" 4578 "G(x);\n" 4579 "F(x)\n" 4580 "try { Q(); } catch (...) {}\n" 4581 "}\n")); 4582 EXPECT_EQ("class A {\n" 4583 " A() : t(0) {}\n" 4584 " A(int i) noexcept() : {}\n" 4585 " A(X x)\n" // FIXME: function-level try blocks are broken. 4586 " try : t(0) {\n" 4587 " } catch (...) {\n" 4588 " }\n" 4589 "};", 4590 format("class A {\n" 4591 " A()\n : t(0) {}\n" 4592 " A(int i)\n noexcept() : {}\n" 4593 " A(X x)\n" 4594 " try : t(0) {} catch (...) {}\n" 4595 "};")); 4596 FormatStyle Style = getLLVMStyle(); 4597 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4598 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 4599 Style.BraceWrapping.AfterFunction = true; 4600 EXPECT_EQ("void f()\n" 4601 "try\n" 4602 "{\n" 4603 "}", 4604 format("void f() try {\n" 4605 "}", 4606 Style)); 4607 EXPECT_EQ("class SomeClass {\n" 4608 "public:\n" 4609 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 4610 "};", 4611 format("class SomeClass {\n" 4612 "public:\n" 4613 " SomeClass()\n" 4614 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 4615 "};")); 4616 EXPECT_EQ("class SomeClass {\n" 4617 "public:\n" 4618 " SomeClass()\n" 4619 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 4620 "};", 4621 format("class SomeClass {\n" 4622 "public:\n" 4623 " SomeClass()\n" 4624 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 4625 "};", 4626 getLLVMStyleWithColumns(40))); 4627 4628 verifyFormat("MACRO(>)"); 4629 4630 // Some macros contain an implicit semicolon. 4631 Style = getLLVMStyle(); 4632 Style.StatementMacros.push_back("FOO"); 4633 verifyFormat("FOO(a) int b = 0;"); 4634 verifyFormat("FOO(a)\n" 4635 "int b = 0;", 4636 Style); 4637 verifyFormat("FOO(a);\n" 4638 "int b = 0;", 4639 Style); 4640 verifyFormat("FOO(argc, argv, \"4.0.2\")\n" 4641 "int b = 0;", 4642 Style); 4643 verifyFormat("FOO()\n" 4644 "int b = 0;", 4645 Style); 4646 verifyFormat("FOO\n" 4647 "int b = 0;", 4648 Style); 4649 verifyFormat("void f() {\n" 4650 " FOO(a)\n" 4651 " return a;\n" 4652 "}", 4653 Style); 4654 verifyFormat("FOO(a)\n" 4655 "FOO(b)", 4656 Style); 4657 verifyFormat("int a = 0;\n" 4658 "FOO(b)\n" 4659 "int c = 0;", 4660 Style); 4661 verifyFormat("int a = 0;\n" 4662 "int x = FOO(a)\n" 4663 "int b = 0;", 4664 Style); 4665 verifyFormat("void foo(int a) { FOO(a) }\n" 4666 "uint32_t bar() {}", 4667 Style); 4668 } 4669 4670 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { 4671 verifyFormat("#define A \\\n" 4672 " f({ \\\n" 4673 " g(); \\\n" 4674 " });", 4675 getLLVMStyleWithColumns(11)); 4676 } 4677 4678 TEST_F(FormatTest, IndentPreprocessorDirectives) { 4679 FormatStyle Style = getLLVMStyle(); 4680 Style.IndentPPDirectives = FormatStyle::PPDIS_None; 4681 Style.ColumnLimit = 40; 4682 verifyFormat("#ifdef _WIN32\n" 4683 "#define A 0\n" 4684 "#ifdef VAR2\n" 4685 "#define B 1\n" 4686 "#include <someheader.h>\n" 4687 "#define MACRO \\\n" 4688 " some_very_long_func_aaaaaaaaaa();\n" 4689 "#endif\n" 4690 "#else\n" 4691 "#define A 1\n" 4692 "#endif", 4693 Style); 4694 Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 4695 verifyFormat("#ifdef _WIN32\n" 4696 "# define A 0\n" 4697 "# ifdef VAR2\n" 4698 "# define B 1\n" 4699 "# include <someheader.h>\n" 4700 "# define MACRO \\\n" 4701 " some_very_long_func_aaaaaaaaaa();\n" 4702 "# endif\n" 4703 "#else\n" 4704 "# define A 1\n" 4705 "#endif", 4706 Style); 4707 verifyFormat("#if A\n" 4708 "# define MACRO \\\n" 4709 " void a(int x) { \\\n" 4710 " b(); \\\n" 4711 " c(); \\\n" 4712 " d(); \\\n" 4713 " e(); \\\n" 4714 " f(); \\\n" 4715 " }\n" 4716 "#endif", 4717 Style); 4718 // Comments before include guard. 4719 verifyFormat("// file comment\n" 4720 "// file comment\n" 4721 "#ifndef HEADER_H\n" 4722 "#define HEADER_H\n" 4723 "code();\n" 4724 "#endif", 4725 Style); 4726 // Test with include guards. 4727 verifyFormat("#ifndef HEADER_H\n" 4728 "#define HEADER_H\n" 4729 "code();\n" 4730 "#endif", 4731 Style); 4732 // Include guards must have a #define with the same variable immediately 4733 // after #ifndef. 4734 verifyFormat("#ifndef NOT_GUARD\n" 4735 "# define FOO\n" 4736 "code();\n" 4737 "#endif", 4738 Style); 4739 4740 // Include guards must cover the entire file. 4741 verifyFormat("code();\n" 4742 "code();\n" 4743 "#ifndef NOT_GUARD\n" 4744 "# define NOT_GUARD\n" 4745 "code();\n" 4746 "#endif", 4747 Style); 4748 verifyFormat("#ifndef NOT_GUARD\n" 4749 "# define NOT_GUARD\n" 4750 "code();\n" 4751 "#endif\n" 4752 "code();", 4753 Style); 4754 // Test with trailing blank lines. 4755 verifyFormat("#ifndef HEADER_H\n" 4756 "#define HEADER_H\n" 4757 "code();\n" 4758 "#endif\n", 4759 Style); 4760 // Include guards don't have #else. 4761 verifyFormat("#ifndef NOT_GUARD\n" 4762 "# define NOT_GUARD\n" 4763 "code();\n" 4764 "#else\n" 4765 "#endif", 4766 Style); 4767 verifyFormat("#ifndef NOT_GUARD\n" 4768 "# define NOT_GUARD\n" 4769 "code();\n" 4770 "#elif FOO\n" 4771 "#endif", 4772 Style); 4773 // Non-identifier #define after potential include guard. 4774 verifyFormat("#ifndef FOO\n" 4775 "# define 1\n" 4776 "#endif\n", 4777 Style); 4778 // #if closes past last non-preprocessor line. 4779 verifyFormat("#ifndef FOO\n" 4780 "#define FOO\n" 4781 "#if 1\n" 4782 "int i;\n" 4783 "# define A 0\n" 4784 "#endif\n" 4785 "#endif\n", 4786 Style); 4787 // Don't crash if there is an #elif directive without a condition. 4788 verifyFormat("#if 1\n" 4789 "int x;\n" 4790 "#elif\n" 4791 "int y;\n" 4792 "#else\n" 4793 "int z;\n" 4794 "#endif", 4795 Style); 4796 // FIXME: This doesn't handle the case where there's code between the 4797 // #ifndef and #define but all other conditions hold. This is because when 4798 // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the 4799 // previous code line yet, so we can't detect it. 4800 EXPECT_EQ("#ifndef NOT_GUARD\n" 4801 "code();\n" 4802 "#define NOT_GUARD\n" 4803 "code();\n" 4804 "#endif", 4805 format("#ifndef NOT_GUARD\n" 4806 "code();\n" 4807 "# define NOT_GUARD\n" 4808 "code();\n" 4809 "#endif", 4810 Style)); 4811 // FIXME: This doesn't handle cases where legitimate preprocessor lines may 4812 // be outside an include guard. Examples are #pragma once and 4813 // #pragma GCC diagnostic, or anything else that does not change the meaning 4814 // of the file if it's included multiple times. 4815 EXPECT_EQ("#ifdef WIN32\n" 4816 "# pragma once\n" 4817 "#endif\n" 4818 "#ifndef HEADER_H\n" 4819 "# define HEADER_H\n" 4820 "code();\n" 4821 "#endif", 4822 format("#ifdef WIN32\n" 4823 "# pragma once\n" 4824 "#endif\n" 4825 "#ifndef HEADER_H\n" 4826 "#define HEADER_H\n" 4827 "code();\n" 4828 "#endif", 4829 Style)); 4830 // FIXME: This does not detect when there is a single non-preprocessor line 4831 // in front of an include-guard-like structure where other conditions hold 4832 // because ScopedLineState hides the line. 4833 EXPECT_EQ("code();\n" 4834 "#ifndef HEADER_H\n" 4835 "#define HEADER_H\n" 4836 "code();\n" 4837 "#endif", 4838 format("code();\n" 4839 "#ifndef HEADER_H\n" 4840 "# define HEADER_H\n" 4841 "code();\n" 4842 "#endif", 4843 Style)); 4844 // Keep comments aligned with #, otherwise indent comments normally. These 4845 // tests cannot use verifyFormat because messUp manipulates leading 4846 // whitespace. 4847 { 4848 const char *Expected = "" 4849 "void f() {\n" 4850 "#if 1\n" 4851 "// Preprocessor aligned.\n" 4852 "# define A 0\n" 4853 " // Code. Separated by blank line.\n" 4854 "\n" 4855 "# define B 0\n" 4856 " // Code. Not aligned with #\n" 4857 "# define C 0\n" 4858 "#endif"; 4859 const char *ToFormat = "" 4860 "void f() {\n" 4861 "#if 1\n" 4862 "// Preprocessor aligned.\n" 4863 "# define A 0\n" 4864 "// Code. Separated by blank line.\n" 4865 "\n" 4866 "# define B 0\n" 4867 " // Code. Not aligned with #\n" 4868 "# define C 0\n" 4869 "#endif"; 4870 EXPECT_EQ(Expected, format(ToFormat, Style)); 4871 EXPECT_EQ(Expected, format(Expected, Style)); 4872 } 4873 // Keep block quotes aligned. 4874 { 4875 const char *Expected = "" 4876 "void f() {\n" 4877 "#if 1\n" 4878 "/* Preprocessor aligned. */\n" 4879 "# define A 0\n" 4880 " /* Code. Separated by blank line. */\n" 4881 "\n" 4882 "# define B 0\n" 4883 " /* Code. Not aligned with # */\n" 4884 "# define C 0\n" 4885 "#endif"; 4886 const char *ToFormat = "" 4887 "void f() {\n" 4888 "#if 1\n" 4889 "/* Preprocessor aligned. */\n" 4890 "# define A 0\n" 4891 "/* Code. Separated by blank line. */\n" 4892 "\n" 4893 "# define B 0\n" 4894 " /* Code. Not aligned with # */\n" 4895 "# define C 0\n" 4896 "#endif"; 4897 EXPECT_EQ(Expected, format(ToFormat, Style)); 4898 EXPECT_EQ(Expected, format(Expected, Style)); 4899 } 4900 // Keep comments aligned with un-indented directives. 4901 { 4902 const char *Expected = "" 4903 "void f() {\n" 4904 "// Preprocessor aligned.\n" 4905 "#define A 0\n" 4906 " // Code. Separated by blank line.\n" 4907 "\n" 4908 "#define B 0\n" 4909 " // Code. Not aligned with #\n" 4910 "#define C 0\n"; 4911 const char *ToFormat = "" 4912 "void f() {\n" 4913 "// Preprocessor aligned.\n" 4914 "#define A 0\n" 4915 "// Code. Separated by blank line.\n" 4916 "\n" 4917 "#define B 0\n" 4918 " // Code. Not aligned with #\n" 4919 "#define C 0\n"; 4920 EXPECT_EQ(Expected, format(ToFormat, Style)); 4921 EXPECT_EQ(Expected, format(Expected, Style)); 4922 } 4923 // Test AfterHash with tabs. 4924 { 4925 FormatStyle Tabbed = Style; 4926 Tabbed.UseTab = FormatStyle::UT_Always; 4927 Tabbed.IndentWidth = 8; 4928 Tabbed.TabWidth = 8; 4929 verifyFormat("#ifdef _WIN32\n" 4930 "#\tdefine A 0\n" 4931 "#\tifdef VAR2\n" 4932 "#\t\tdefine B 1\n" 4933 "#\t\tinclude <someheader.h>\n" 4934 "#\t\tdefine MACRO \\\n" 4935 "\t\t\tsome_very_long_func_aaaaaaaaaa();\n" 4936 "#\tendif\n" 4937 "#else\n" 4938 "#\tdefine A 1\n" 4939 "#endif", 4940 Tabbed); 4941 } 4942 4943 // Regression test: Multiline-macro inside include guards. 4944 verifyFormat("#ifndef HEADER_H\n" 4945 "#define HEADER_H\n" 4946 "#define A() \\\n" 4947 " int i; \\\n" 4948 " int j;\n" 4949 "#endif // HEADER_H", 4950 getLLVMStyleWithColumns(20)); 4951 4952 Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash; 4953 // Basic before hash indent tests 4954 verifyFormat("#ifdef _WIN32\n" 4955 " #define A 0\n" 4956 " #ifdef VAR2\n" 4957 " #define B 1\n" 4958 " #include <someheader.h>\n" 4959 " #define MACRO \\\n" 4960 " some_very_long_func_aaaaaaaaaa();\n" 4961 " #endif\n" 4962 "#else\n" 4963 " #define A 1\n" 4964 "#endif", 4965 Style); 4966 verifyFormat("#if A\n" 4967 " #define MACRO \\\n" 4968 " void a(int x) { \\\n" 4969 " b(); \\\n" 4970 " c(); \\\n" 4971 " d(); \\\n" 4972 " e(); \\\n" 4973 " f(); \\\n" 4974 " }\n" 4975 "#endif", 4976 Style); 4977 // Keep comments aligned with indented directives. These 4978 // tests cannot use verifyFormat because messUp manipulates leading 4979 // whitespace. 4980 { 4981 const char *Expected = "void f() {\n" 4982 "// Aligned to preprocessor.\n" 4983 "#if 1\n" 4984 " // Aligned to code.\n" 4985 " int a;\n" 4986 " #if 1\n" 4987 " // Aligned to preprocessor.\n" 4988 " #define A 0\n" 4989 " // Aligned to code.\n" 4990 " int b;\n" 4991 " #endif\n" 4992 "#endif\n" 4993 "}"; 4994 const char *ToFormat = "void f() {\n" 4995 "// Aligned to preprocessor.\n" 4996 "#if 1\n" 4997 "// Aligned to code.\n" 4998 "int a;\n" 4999 "#if 1\n" 5000 "// Aligned to preprocessor.\n" 5001 "#define A 0\n" 5002 "// Aligned to code.\n" 5003 "int b;\n" 5004 "#endif\n" 5005 "#endif\n" 5006 "}"; 5007 EXPECT_EQ(Expected, format(ToFormat, Style)); 5008 EXPECT_EQ(Expected, format(Expected, Style)); 5009 } 5010 { 5011 const char *Expected = "void f() {\n" 5012 "/* Aligned to preprocessor. */\n" 5013 "#if 1\n" 5014 " /* Aligned to code. */\n" 5015 " int a;\n" 5016 " #if 1\n" 5017 " /* Aligned to preprocessor. */\n" 5018 " #define A 0\n" 5019 " /* Aligned to code. */\n" 5020 " int b;\n" 5021 " #endif\n" 5022 "#endif\n" 5023 "}"; 5024 const char *ToFormat = "void f() {\n" 5025 "/* Aligned to preprocessor. */\n" 5026 "#if 1\n" 5027 "/* Aligned to code. */\n" 5028 "int a;\n" 5029 "#if 1\n" 5030 "/* Aligned to preprocessor. */\n" 5031 "#define A 0\n" 5032 "/* Aligned to code. */\n" 5033 "int b;\n" 5034 "#endif\n" 5035 "#endif\n" 5036 "}"; 5037 EXPECT_EQ(Expected, format(ToFormat, Style)); 5038 EXPECT_EQ(Expected, format(Expected, Style)); 5039 } 5040 5041 // Test single comment before preprocessor 5042 verifyFormat("// Comment\n" 5043 "\n" 5044 "#if 1\n" 5045 "#endif", 5046 Style); 5047 } 5048 5049 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 5050 verifyFormat("{\n { a #c; }\n}"); 5051 } 5052 5053 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 5054 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 5055 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 5056 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 5057 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 5058 } 5059 5060 TEST_F(FormatTest, EscapedNewlines) { 5061 FormatStyle Narrow = getLLVMStyleWithColumns(11); 5062 EXPECT_EQ("#define A \\\n int i; \\\n int j;", 5063 format("#define A \\\nint i;\\\n int j;", Narrow)); 5064 EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;")); 5065 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 5066 EXPECT_EQ("/* \\ \\ \\\n */", format("\\\n/* \\ \\ \\\n */")); 5067 EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>")); 5068 5069 FormatStyle AlignLeft = getLLVMStyle(); 5070 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 5071 EXPECT_EQ("#define MACRO(x) \\\n" 5072 "private: \\\n" 5073 " int x(int a);\n", 5074 format("#define MACRO(x) \\\n" 5075 "private: \\\n" 5076 " int x(int a);\n", 5077 AlignLeft)); 5078 5079 // CRLF line endings 5080 EXPECT_EQ("#define A \\\r\n int i; \\\r\n int j;", 5081 format("#define A \\\r\nint i;\\\r\n int j;", Narrow)); 5082 EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;")); 5083 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 5084 EXPECT_EQ("/* \\ \\ \\\r\n */", format("\\\r\n/* \\ \\ \\\r\n */")); 5085 EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>")); 5086 EXPECT_EQ("#define MACRO(x) \\\r\n" 5087 "private: \\\r\n" 5088 " int x(int a);\r\n", 5089 format("#define MACRO(x) \\\r\n" 5090 "private: \\\r\n" 5091 " int x(int a);\r\n", 5092 AlignLeft)); 5093 5094 FormatStyle DontAlign = getLLVMStyle(); 5095 DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 5096 DontAlign.MaxEmptyLinesToKeep = 3; 5097 // FIXME: can't use verifyFormat here because the newline before 5098 // "public:" is not inserted the first time it's reformatted 5099 EXPECT_EQ("#define A \\\n" 5100 " class Foo { \\\n" 5101 " void bar(); \\\n" 5102 "\\\n" 5103 "\\\n" 5104 "\\\n" 5105 " public: \\\n" 5106 " void baz(); \\\n" 5107 " };", 5108 format("#define A \\\n" 5109 " class Foo { \\\n" 5110 " void bar(); \\\n" 5111 "\\\n" 5112 "\\\n" 5113 "\\\n" 5114 " public: \\\n" 5115 " void baz(); \\\n" 5116 " };", 5117 DontAlign)); 5118 } 5119 5120 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 5121 verifyFormat("#define A \\\n" 5122 " int v( \\\n" 5123 " a); \\\n" 5124 " int i;", 5125 getLLVMStyleWithColumns(11)); 5126 } 5127 5128 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 5129 EXPECT_EQ( 5130 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 5131 " \\\n" 5132 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 5133 "\n" 5134 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 5135 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 5136 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 5137 "\\\n" 5138 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 5139 " \n" 5140 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 5141 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 5142 } 5143 5144 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 5145 EXPECT_EQ("int\n" 5146 "#define A\n" 5147 " a;", 5148 format("int\n#define A\na;")); 5149 verifyFormat("functionCallTo(\n" 5150 " someOtherFunction(\n" 5151 " withSomeParameters, whichInSequence,\n" 5152 " areLongerThanALine(andAnotherCall,\n" 5153 "#define A B\n" 5154 " withMoreParamters,\n" 5155 " whichStronglyInfluenceTheLayout),\n" 5156 " andMoreParameters),\n" 5157 " trailing);", 5158 getLLVMStyleWithColumns(69)); 5159 verifyFormat("Foo::Foo()\n" 5160 "#ifdef BAR\n" 5161 " : baz(0)\n" 5162 "#endif\n" 5163 "{\n" 5164 "}"); 5165 verifyFormat("void f() {\n" 5166 " if (true)\n" 5167 "#ifdef A\n" 5168 " f(42);\n" 5169 " x();\n" 5170 "#else\n" 5171 " g();\n" 5172 " x();\n" 5173 "#endif\n" 5174 "}"); 5175 verifyFormat("void f(param1, param2,\n" 5176 " param3,\n" 5177 "#ifdef A\n" 5178 " param4(param5,\n" 5179 "#ifdef A1\n" 5180 " param6,\n" 5181 "#ifdef A2\n" 5182 " param7),\n" 5183 "#else\n" 5184 " param8),\n" 5185 " param9,\n" 5186 "#endif\n" 5187 " param10,\n" 5188 "#endif\n" 5189 " param11)\n" 5190 "#else\n" 5191 " param12)\n" 5192 "#endif\n" 5193 "{\n" 5194 " x();\n" 5195 "}", 5196 getLLVMStyleWithColumns(28)); 5197 verifyFormat("#if 1\n" 5198 "int i;"); 5199 verifyFormat("#if 1\n" 5200 "#endif\n" 5201 "#if 1\n" 5202 "#else\n" 5203 "#endif\n"); 5204 verifyFormat("DEBUG({\n" 5205 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5206 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 5207 "});\n" 5208 "#if a\n" 5209 "#else\n" 5210 "#endif"); 5211 5212 verifyIncompleteFormat("void f(\n" 5213 "#if A\n" 5214 ");\n" 5215 "#else\n" 5216 "#endif"); 5217 } 5218 5219 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { 5220 verifyFormat("#endif\n" 5221 "#if B"); 5222 } 5223 5224 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { 5225 FormatStyle SingleLine = getLLVMStyle(); 5226 SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; 5227 verifyFormat("#if 0\n" 5228 "#elif 1\n" 5229 "#endif\n" 5230 "void foo() {\n" 5231 " if (test) foo2();\n" 5232 "}", 5233 SingleLine); 5234 } 5235 5236 TEST_F(FormatTest, LayoutBlockInsideParens) { 5237 verifyFormat("functionCall({ int i; });"); 5238 verifyFormat("functionCall({\n" 5239 " int i;\n" 5240 " int j;\n" 5241 "});"); 5242 verifyFormat("functionCall(\n" 5243 " {\n" 5244 " int i;\n" 5245 " int j;\n" 5246 " },\n" 5247 " aaaa, bbbb, cccc);"); 5248 verifyFormat("functionA(functionB({\n" 5249 " int i;\n" 5250 " int j;\n" 5251 " }),\n" 5252 " aaaa, bbbb, cccc);"); 5253 verifyFormat("functionCall(\n" 5254 " {\n" 5255 " int i;\n" 5256 " int j;\n" 5257 " },\n" 5258 " aaaa, bbbb, // comment\n" 5259 " cccc);"); 5260 verifyFormat("functionA(functionB({\n" 5261 " int i;\n" 5262 " int j;\n" 5263 " }),\n" 5264 " aaaa, bbbb, // comment\n" 5265 " cccc);"); 5266 verifyFormat("functionCall(aaaa, bbbb, { int i; });"); 5267 verifyFormat("functionCall(aaaa, bbbb, {\n" 5268 " int i;\n" 5269 " int j;\n" 5270 "});"); 5271 verifyFormat( 5272 "Aaa(\n" // FIXME: There shouldn't be a linebreak here. 5273 " {\n" 5274 " int i; // break\n" 5275 " },\n" 5276 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 5277 " ccccccccccccccccc));"); 5278 verifyFormat("DEBUG({\n" 5279 " if (a)\n" 5280 " f();\n" 5281 "});"); 5282 } 5283 5284 TEST_F(FormatTest, LayoutBlockInsideStatement) { 5285 EXPECT_EQ("SOME_MACRO { int i; }\n" 5286 "int i;", 5287 format(" SOME_MACRO {int i;} int i;")); 5288 } 5289 5290 TEST_F(FormatTest, LayoutNestedBlocks) { 5291 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 5292 " struct s {\n" 5293 " int i;\n" 5294 " };\n" 5295 " s kBitsToOs[] = {{10}};\n" 5296 " for (int i = 0; i < 10; ++i)\n" 5297 " return;\n" 5298 "}"); 5299 verifyFormat("call(parameter, {\n" 5300 " something();\n" 5301 " // Comment using all columns.\n" 5302 " somethingelse();\n" 5303 "});", 5304 getLLVMStyleWithColumns(40)); 5305 verifyFormat("DEBUG( //\n" 5306 " { f(); }, a);"); 5307 verifyFormat("DEBUG( //\n" 5308 " {\n" 5309 " f(); //\n" 5310 " },\n" 5311 " a);"); 5312 5313 EXPECT_EQ("call(parameter, {\n" 5314 " something();\n" 5315 " // Comment too\n" 5316 " // looooooooooong.\n" 5317 " somethingElse();\n" 5318 "});", 5319 format("call(parameter, {\n" 5320 " something();\n" 5321 " // Comment too looooooooooong.\n" 5322 " somethingElse();\n" 5323 "});", 5324 getLLVMStyleWithColumns(29))); 5325 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });")); 5326 EXPECT_EQ("DEBUG({ // comment\n" 5327 " int i;\n" 5328 "});", 5329 format("DEBUG({ // comment\n" 5330 "int i;\n" 5331 "});")); 5332 EXPECT_EQ("DEBUG({\n" 5333 " int i;\n" 5334 "\n" 5335 " // comment\n" 5336 " int j;\n" 5337 "});", 5338 format("DEBUG({\n" 5339 " int i;\n" 5340 "\n" 5341 " // comment\n" 5342 " int j;\n" 5343 "});")); 5344 5345 verifyFormat("DEBUG({\n" 5346 " if (a)\n" 5347 " return;\n" 5348 "});"); 5349 verifyGoogleFormat("DEBUG({\n" 5350 " if (a) return;\n" 5351 "});"); 5352 FormatStyle Style = getGoogleStyle(); 5353 Style.ColumnLimit = 45; 5354 verifyFormat("Debug(\n" 5355 " aaaaa,\n" 5356 " {\n" 5357 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" 5358 " },\n" 5359 " a);", 5360 Style); 5361 5362 verifyFormat("SomeFunction({MACRO({ return output; }), b});"); 5363 5364 verifyNoCrash("^{v^{a}}"); 5365 } 5366 5367 TEST_F(FormatTest, FormatNestedBlocksInMacros) { 5368 EXPECT_EQ("#define MACRO() \\\n" 5369 " Debug(aaa, /* force line break */ \\\n" 5370 " { \\\n" 5371 " int i; \\\n" 5372 " int j; \\\n" 5373 " })", 5374 format("#define MACRO() Debug(aaa, /* force line break */ \\\n" 5375 " { int i; int j; })", 5376 getGoogleStyle())); 5377 5378 EXPECT_EQ("#define A \\\n" 5379 " [] { \\\n" 5380 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 5381 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 5382 " }", 5383 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 5384 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 5385 getGoogleStyle())); 5386 } 5387 5388 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 5389 EXPECT_EQ("{}", format("{}")); 5390 verifyFormat("enum E {};"); 5391 verifyFormat("enum E {}"); 5392 FormatStyle Style = getLLVMStyle(); 5393 Style.SpaceInEmptyBlock = true; 5394 EXPECT_EQ("void f() { }", format("void f() {}", Style)); 5395 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 5396 EXPECT_EQ("while (true) { }", format("while (true) {}", Style)); 5397 } 5398 5399 TEST_F(FormatTest, FormatBeginBlockEndMacros) { 5400 FormatStyle Style = getLLVMStyle(); 5401 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$"; 5402 Style.MacroBlockEnd = "^[A-Z_]+_END$"; 5403 verifyFormat("FOO_BEGIN\n" 5404 " FOO_ENTRY\n" 5405 "FOO_END", 5406 Style); 5407 verifyFormat("FOO_BEGIN\n" 5408 " NESTED_FOO_BEGIN\n" 5409 " NESTED_FOO_ENTRY\n" 5410 " NESTED_FOO_END\n" 5411 "FOO_END", 5412 Style); 5413 verifyFormat("FOO_BEGIN(Foo, Bar)\n" 5414 " int x;\n" 5415 " x = 1;\n" 5416 "FOO_END(Baz)", 5417 Style); 5418 } 5419 5420 //===----------------------------------------------------------------------===// 5421 // Line break tests. 5422 //===----------------------------------------------------------------------===// 5423 5424 TEST_F(FormatTest, PreventConfusingIndents) { 5425 verifyFormat( 5426 "void f() {\n" 5427 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 5428 " parameter, parameter, parameter)),\n" 5429 " SecondLongCall(parameter));\n" 5430 "}"); 5431 verifyFormat( 5432 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5433 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 5434 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5435 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 5436 verifyFormat( 5437 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5438 " [aaaaaaaaaaaaaaaaaaaaaaaa\n" 5439 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 5440 " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); 5441 verifyFormat( 5442 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 5443 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 5444 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 5445 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 5446 verifyFormat("int a = bbbb && ccc &&\n" 5447 " fffff(\n" 5448 "#define A Just forcing a new line\n" 5449 " ddd);"); 5450 } 5451 5452 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 5453 verifyFormat( 5454 "bool aaaaaaa =\n" 5455 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 5456 " bbbbbbbb();"); 5457 verifyFormat( 5458 "bool aaaaaaa =\n" 5459 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" 5460 " bbbbbbbb();"); 5461 5462 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 5463 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 5464 " ccccccccc == ddddddddddd;"); 5465 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 5466 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" 5467 " ccccccccc == ddddddddddd;"); 5468 verifyFormat( 5469 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 5470 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" 5471 " ccccccccc == ddddddddddd;"); 5472 5473 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 5474 " aaaaaa) &&\n" 5475 " bbbbbb && cccccc;"); 5476 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 5477 " aaaaaa) >>\n" 5478 " bbbbbb;"); 5479 verifyFormat("aa = Whitespaces.addUntouchableComment(\n" 5480 " SourceMgr.getSpellingColumnNumber(\n" 5481 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 5482 " 1);"); 5483 5484 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5485 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 5486 " cccccc) {\n}"); 5487 verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5488 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 5489 " cccccc) {\n}"); 5490 verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5491 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 5492 " cccccc) {\n}"); 5493 verifyFormat("b = a &&\n" 5494 " // Comment\n" 5495 " b.c && d;"); 5496 5497 // If the LHS of a comparison is not a binary expression itself, the 5498 // additional linebreak confuses many people. 5499 verifyFormat( 5500 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5501 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 5502 "}"); 5503 verifyFormat( 5504 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5505 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 5506 "}"); 5507 verifyFormat( 5508 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 5509 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 5510 "}"); 5511 verifyFormat( 5512 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5513 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n" 5514 "}"); 5515 // Even explicit parentheses stress the precedence enough to make the 5516 // additional break unnecessary. 5517 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5518 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 5519 "}"); 5520 // This cases is borderline, but with the indentation it is still readable. 5521 verifyFormat( 5522 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5523 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5524 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 5525 "}", 5526 getLLVMStyleWithColumns(75)); 5527 5528 // If the LHS is a binary expression, we should still use the additional break 5529 // as otherwise the formatting hides the operator precedence. 5530 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5531 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5532 " 5) {\n" 5533 "}"); 5534 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5535 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n" 5536 " 5) {\n" 5537 "}"); 5538 5539 FormatStyle OnePerLine = getLLVMStyle(); 5540 OnePerLine.BinPackParameters = false; 5541 verifyFormat( 5542 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5543 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5544 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 5545 OnePerLine); 5546 5547 verifyFormat("int i = someFunction(aaaaaaa, 0)\n" 5548 " .aaa(aaaaaaaaaaaaa) *\n" 5549 " aaaaaaa +\n" 5550 " aaaaaaa;", 5551 getLLVMStyleWithColumns(40)); 5552 } 5553 5554 TEST_F(FormatTest, ExpressionIndentation) { 5555 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5556 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5557 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5558 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5559 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 5560 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 5561 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5562 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 5563 " ccccccccccccccccccccccccccccccccccccccccc;"); 5564 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5565 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5566 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5567 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 5568 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5569 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5570 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5571 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 5572 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5573 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5574 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5575 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 5576 verifyFormat("if () {\n" 5577 "} else if (aaaaa && bbbbb > // break\n" 5578 " ccccc) {\n" 5579 "}"); 5580 verifyFormat("if () {\n" 5581 "} else if constexpr (aaaaa && bbbbb > // break\n" 5582 " ccccc) {\n" 5583 "}"); 5584 verifyFormat("if () {\n" 5585 "} else if CONSTEXPR (aaaaa && bbbbb > // break\n" 5586 " ccccc) {\n" 5587 "}"); 5588 verifyFormat("if () {\n" 5589 "} else if (aaaaa &&\n" 5590 " bbbbb > // break\n" 5591 " ccccc &&\n" 5592 " ddddd) {\n" 5593 "}"); 5594 5595 // Presence of a trailing comment used to change indentation of b. 5596 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 5597 " b;\n" 5598 "return aaaaaaaaaaaaaaaaaaa +\n" 5599 " b; //", 5600 getLLVMStyleWithColumns(30)); 5601 } 5602 5603 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 5604 // Not sure what the best system is here. Like this, the LHS can be found 5605 // immediately above an operator (everything with the same or a higher 5606 // indent). The RHS is aligned right of the operator and so compasses 5607 // everything until something with the same indent as the operator is found. 5608 // FIXME: Is this a good system? 5609 FormatStyle Style = getLLVMStyle(); 5610 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 5611 verifyFormat( 5612 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5613 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5614 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5615 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5616 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5617 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5618 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5619 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5620 " > ccccccccccccccccccccccccccccccccccccccccc;", 5621 Style); 5622 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5623 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5624 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5625 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5626 Style); 5627 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5628 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5629 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5630 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5631 Style); 5632 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5633 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5634 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5635 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5636 Style); 5637 verifyFormat("if () {\n" 5638 "} else if (aaaaa\n" 5639 " && bbbbb // break\n" 5640 " > ccccc) {\n" 5641 "}", 5642 Style); 5643 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5644 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 5645 Style); 5646 verifyFormat("return (a)\n" 5647 " // comment\n" 5648 " + b;", 5649 Style); 5650 verifyFormat( 5651 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5652 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5653 " + cc;", 5654 Style); 5655 5656 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5657 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5658 Style); 5659 5660 // Forced by comments. 5661 verifyFormat( 5662 "unsigned ContentSize =\n" 5663 " sizeof(int16_t) // DWARF ARange version number\n" 5664 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 5665 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 5666 " + sizeof(int8_t); // Segment Size (in bytes)"); 5667 5668 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 5669 " == boost::fusion::at_c<1>(iiii).second;", 5670 Style); 5671 5672 Style.ColumnLimit = 60; 5673 verifyFormat("zzzzzzzzzz\n" 5674 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5675 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 5676 Style); 5677 5678 Style.ColumnLimit = 80; 5679 Style.IndentWidth = 4; 5680 Style.TabWidth = 4; 5681 Style.UseTab = FormatStyle::UT_Always; 5682 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5683 Style.AlignOperands = FormatStyle::OAS_DontAlign; 5684 EXPECT_EQ("return someVeryVeryLongConditionThatBarelyFitsOnALine\n" 5685 "\t&& (someOtherLongishConditionPart1\n" 5686 "\t\t|| someOtherEvenLongerNestedConditionPart2);", 5687 format("return someVeryVeryLongConditionThatBarelyFitsOnALine && " 5688 "(someOtherLongishConditionPart1 || " 5689 "someOtherEvenLongerNestedConditionPart2);", 5690 Style)); 5691 } 5692 5693 TEST_F(FormatTest, ExpressionIndentationStrictAlign) { 5694 FormatStyle Style = getLLVMStyle(); 5695 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 5696 Style.AlignOperands = FormatStyle::OAS_AlignAfterOperator; 5697 5698 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5699 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5700 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5701 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5702 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5703 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5704 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5705 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5706 " > ccccccccccccccccccccccccccccccccccccccccc;", 5707 Style); 5708 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5709 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5710 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5711 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5712 Style); 5713 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5714 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5715 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5716 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5717 Style); 5718 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5719 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5720 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5721 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5722 Style); 5723 verifyFormat("if () {\n" 5724 "} else if (aaaaa\n" 5725 " && bbbbb // break\n" 5726 " > ccccc) {\n" 5727 "}", 5728 Style); 5729 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5730 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 5731 Style); 5732 verifyFormat("return (a)\n" 5733 " // comment\n" 5734 " + b;", 5735 Style); 5736 verifyFormat( 5737 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5738 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5739 " + cc;", 5740 Style); 5741 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 5742 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 5743 " : 3333333333333333;", 5744 Style); 5745 verifyFormat( 5746 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 5747 " : ccccccccccccccc ? dddddddddddddddddd\n" 5748 " : eeeeeeeeeeeeeeeeee)\n" 5749 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 5750 " : 3333333333333333;", 5751 Style); 5752 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5753 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5754 Style); 5755 5756 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 5757 " == boost::fusion::at_c<1>(iiii).second;", 5758 Style); 5759 5760 Style.ColumnLimit = 60; 5761 verifyFormat("zzzzzzzzzzzzz\n" 5762 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5763 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 5764 Style); 5765 5766 // Forced by comments. 5767 Style.ColumnLimit = 80; 5768 verifyFormat( 5769 "unsigned ContentSize\n" 5770 " = sizeof(int16_t) // DWARF ARange version number\n" 5771 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 5772 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 5773 " + sizeof(int8_t); // Segment Size (in bytes)", 5774 Style); 5775 5776 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 5777 verifyFormat( 5778 "unsigned ContentSize =\n" 5779 " sizeof(int16_t) // DWARF ARange version number\n" 5780 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 5781 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 5782 " + sizeof(int8_t); // Segment Size (in bytes)", 5783 Style); 5784 5785 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 5786 verifyFormat( 5787 "unsigned ContentSize =\n" 5788 " sizeof(int16_t) // DWARF ARange version number\n" 5789 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 5790 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 5791 " + sizeof(int8_t); // Segment Size (in bytes)", 5792 Style); 5793 } 5794 5795 TEST_F(FormatTest, EnforcedOperatorWraps) { 5796 // Here we'd like to wrap after the || operators, but a comment is forcing an 5797 // earlier wrap. 5798 verifyFormat("bool x = aaaaa //\n" 5799 " || bbbbb\n" 5800 " //\n" 5801 " || cccc;"); 5802 } 5803 5804 TEST_F(FormatTest, NoOperandAlignment) { 5805 FormatStyle Style = getLLVMStyle(); 5806 Style.AlignOperands = FormatStyle::OAS_DontAlign; 5807 verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n" 5808 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5809 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5810 Style); 5811 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 5812 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5813 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5814 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5815 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5816 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5817 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5818 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5819 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5820 " > ccccccccccccccccccccccccccccccccccccccccc;", 5821 Style); 5822 5823 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5824 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5825 " + cc;", 5826 Style); 5827 verifyFormat("int a = aa\n" 5828 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5829 " * cccccccccccccccccccccccccccccccccccc;\n", 5830 Style); 5831 5832 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5833 verifyFormat("return (a > b\n" 5834 " // comment1\n" 5835 " // comment2\n" 5836 " || c);", 5837 Style); 5838 } 5839 5840 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { 5841 FormatStyle Style = getLLVMStyle(); 5842 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 5843 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5844 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5845 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 5846 Style); 5847 } 5848 5849 TEST_F(FormatTest, AllowBinPackingInsideArguments) { 5850 FormatStyle Style = getLLVMStyle(); 5851 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 5852 Style.BinPackArguments = false; 5853 Style.ColumnLimit = 40; 5854 verifyFormat("void test() {\n" 5855 " someFunction(\n" 5856 " this + argument + is + quite\n" 5857 " + long + so + it + gets + wrapped\n" 5858 " + but + remains + bin - packed);\n" 5859 "}", 5860 Style); 5861 verifyFormat("void test() {\n" 5862 " someFunction(arg1,\n" 5863 " this + argument + is\n" 5864 " + quite + long + so\n" 5865 " + it + gets + wrapped\n" 5866 " + but + remains + bin\n" 5867 " - packed,\n" 5868 " arg3);\n" 5869 "}", 5870 Style); 5871 verifyFormat("void test() {\n" 5872 " someFunction(\n" 5873 " arg1,\n" 5874 " this + argument + has\n" 5875 " + anotherFunc(nested,\n" 5876 " calls + whose\n" 5877 " + arguments\n" 5878 " + are + also\n" 5879 " + wrapped,\n" 5880 " in + addition)\n" 5881 " + to + being + bin - packed,\n" 5882 " arg3);\n" 5883 "}", 5884 Style); 5885 5886 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 5887 verifyFormat("void test() {\n" 5888 " someFunction(\n" 5889 " arg1,\n" 5890 " this + argument + has +\n" 5891 " anotherFunc(nested,\n" 5892 " calls + whose +\n" 5893 " arguments +\n" 5894 " are + also +\n" 5895 " wrapped,\n" 5896 " in + addition) +\n" 5897 " to + being + bin - packed,\n" 5898 " arg3);\n" 5899 "}", 5900 Style); 5901 } 5902 5903 TEST_F(FormatTest, ConstructorInitializers) { 5904 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 5905 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 5906 getLLVMStyleWithColumns(45)); 5907 verifyFormat("Constructor()\n" 5908 " : Inttializer(FitsOnTheLine) {}", 5909 getLLVMStyleWithColumns(44)); 5910 verifyFormat("Constructor()\n" 5911 " : Inttializer(FitsOnTheLine) {}", 5912 getLLVMStyleWithColumns(43)); 5913 5914 verifyFormat("template <typename T>\n" 5915 "Constructor() : Initializer(FitsOnTheLine) {}", 5916 getLLVMStyleWithColumns(45)); 5917 5918 verifyFormat( 5919 "SomeClass::Constructor()\n" 5920 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 5921 5922 verifyFormat( 5923 "SomeClass::Constructor()\n" 5924 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 5925 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 5926 verifyFormat( 5927 "SomeClass::Constructor()\n" 5928 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5929 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 5930 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5931 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5932 " : aaaaaaaaaa(aaaaaa) {}"); 5933 5934 verifyFormat("Constructor()\n" 5935 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5936 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5937 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5938 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 5939 5940 verifyFormat("Constructor()\n" 5941 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5942 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 5943 5944 verifyFormat("Constructor(int Parameter = 0)\n" 5945 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 5946 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 5947 verifyFormat("Constructor()\n" 5948 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 5949 "}", 5950 getLLVMStyleWithColumns(60)); 5951 verifyFormat("Constructor()\n" 5952 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5953 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 5954 5955 // Here a line could be saved by splitting the second initializer onto two 5956 // lines, but that is not desirable. 5957 verifyFormat("Constructor()\n" 5958 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 5959 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 5960 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 5961 5962 FormatStyle OnePerLine = getLLVMStyle(); 5963 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 5964 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 5965 verifyFormat("SomeClass::Constructor()\n" 5966 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 5967 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 5968 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 5969 OnePerLine); 5970 verifyFormat("SomeClass::Constructor()\n" 5971 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 5972 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 5973 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 5974 OnePerLine); 5975 verifyFormat("MyClass::MyClass(int var)\n" 5976 " : some_var_(var), // 4 space indent\n" 5977 " some_other_var_(var + 1) { // lined up\n" 5978 "}", 5979 OnePerLine); 5980 verifyFormat("Constructor()\n" 5981 " : aaaaa(aaaaaa),\n" 5982 " aaaaa(aaaaaa),\n" 5983 " aaaaa(aaaaaa),\n" 5984 " aaaaa(aaaaaa),\n" 5985 " aaaaa(aaaaaa) {}", 5986 OnePerLine); 5987 verifyFormat("Constructor()\n" 5988 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 5989 " aaaaaaaaaaaaaaaaaaaaaa) {}", 5990 OnePerLine); 5991 OnePerLine.BinPackParameters = false; 5992 verifyFormat( 5993 "Constructor()\n" 5994 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n" 5995 " aaaaaaaaaaa().aaa(),\n" 5996 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 5997 OnePerLine); 5998 OnePerLine.ColumnLimit = 60; 5999 verifyFormat("Constructor()\n" 6000 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6001 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 6002 OnePerLine); 6003 6004 EXPECT_EQ("Constructor()\n" 6005 " : // Comment forcing unwanted break.\n" 6006 " aaaa(aaaa) {}", 6007 format("Constructor() :\n" 6008 " // Comment forcing unwanted break.\n" 6009 " aaaa(aaaa) {}")); 6010 } 6011 6012 TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) { 6013 FormatStyle Style = getLLVMStyle(); 6014 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 6015 Style.ColumnLimit = 60; 6016 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 6017 Style.AllowAllConstructorInitializersOnNextLine = true; 6018 Style.BinPackParameters = false; 6019 6020 for (int i = 0; i < 4; ++i) { 6021 // Test all combinations of parameters that should not have an effect. 6022 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1; 6023 Style.AllowAllArgumentsOnNextLine = i & 2; 6024 6025 Style.AllowAllConstructorInitializersOnNextLine = true; 6026 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 6027 verifyFormat("Constructor()\n" 6028 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6029 Style); 6030 verifyFormat("Constructor() : a(a), b(b) {}", Style); 6031 6032 Style.AllowAllConstructorInitializersOnNextLine = false; 6033 verifyFormat("Constructor()\n" 6034 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 6035 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 6036 Style); 6037 verifyFormat("Constructor() : a(a), b(b) {}", Style); 6038 6039 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 6040 Style.AllowAllConstructorInitializersOnNextLine = true; 6041 verifyFormat("Constructor()\n" 6042 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6043 Style); 6044 6045 Style.AllowAllConstructorInitializersOnNextLine = false; 6046 verifyFormat("Constructor()\n" 6047 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6048 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6049 Style); 6050 6051 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 6052 Style.AllowAllConstructorInitializersOnNextLine = true; 6053 verifyFormat("Constructor() :\n" 6054 " aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6055 Style); 6056 6057 Style.AllowAllConstructorInitializersOnNextLine = false; 6058 verifyFormat("Constructor() :\n" 6059 " aaaaaaaaaaaaaaaaaa(a),\n" 6060 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6061 Style); 6062 } 6063 6064 // Test interactions between AllowAllParametersOfDeclarationOnNextLine and 6065 // AllowAllConstructorInitializersOnNextLine in all 6066 // BreakConstructorInitializers modes 6067 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 6068 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6069 Style.AllowAllConstructorInitializersOnNextLine = false; 6070 verifyFormat("SomeClassWithALongName::Constructor(\n" 6071 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n" 6072 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 6073 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 6074 Style); 6075 6076 Style.AllowAllConstructorInitializersOnNextLine = true; 6077 verifyFormat("SomeClassWithALongName::Constructor(\n" 6078 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6079 " int bbbbbbbbbbbbb,\n" 6080 " int cccccccccccccccc)\n" 6081 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6082 Style); 6083 6084 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6085 Style.AllowAllConstructorInitializersOnNextLine = false; 6086 verifyFormat("SomeClassWithALongName::Constructor(\n" 6087 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6088 " int bbbbbbbbbbbbb)\n" 6089 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 6090 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 6091 Style); 6092 6093 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 6094 6095 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6096 verifyFormat("SomeClassWithALongName::Constructor(\n" 6097 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n" 6098 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6099 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6100 Style); 6101 6102 Style.AllowAllConstructorInitializersOnNextLine = true; 6103 verifyFormat("SomeClassWithALongName::Constructor(\n" 6104 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6105 " int bbbbbbbbbbbbb,\n" 6106 " int cccccccccccccccc)\n" 6107 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6108 Style); 6109 6110 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6111 Style.AllowAllConstructorInitializersOnNextLine = false; 6112 verifyFormat("SomeClassWithALongName::Constructor(\n" 6113 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6114 " int bbbbbbbbbbbbb)\n" 6115 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6116 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6117 Style); 6118 6119 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 6120 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6121 verifyFormat("SomeClassWithALongName::Constructor(\n" 6122 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n" 6123 " aaaaaaaaaaaaaaaaaaaa(a),\n" 6124 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6125 Style); 6126 6127 Style.AllowAllConstructorInitializersOnNextLine = true; 6128 verifyFormat("SomeClassWithALongName::Constructor(\n" 6129 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6130 " int bbbbbbbbbbbbb,\n" 6131 " int cccccccccccccccc) :\n" 6132 " aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6133 Style); 6134 6135 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6136 Style.AllowAllConstructorInitializersOnNextLine = false; 6137 verifyFormat("SomeClassWithALongName::Constructor(\n" 6138 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6139 " int bbbbbbbbbbbbb) :\n" 6140 " aaaaaaaaaaaaaaaaaaaa(a),\n" 6141 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6142 Style); 6143 } 6144 6145 TEST_F(FormatTest, AllowAllArgumentsOnNextLine) { 6146 FormatStyle Style = getLLVMStyle(); 6147 Style.ColumnLimit = 60; 6148 Style.BinPackArguments = false; 6149 for (int i = 0; i < 4; ++i) { 6150 // Test all combinations of parameters that should not have an effect. 6151 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1; 6152 Style.AllowAllConstructorInitializersOnNextLine = i & 2; 6153 6154 Style.AllowAllArgumentsOnNextLine = true; 6155 verifyFormat("void foo() {\n" 6156 " FunctionCallWithReallyLongName(\n" 6157 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n" 6158 "}", 6159 Style); 6160 Style.AllowAllArgumentsOnNextLine = false; 6161 verifyFormat("void foo() {\n" 6162 " FunctionCallWithReallyLongName(\n" 6163 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6164 " bbbbbbbbbbbb);\n" 6165 "}", 6166 Style); 6167 6168 Style.AllowAllArgumentsOnNextLine = true; 6169 verifyFormat("void foo() {\n" 6170 " auto VariableWithReallyLongName = {\n" 6171 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n" 6172 "}", 6173 Style); 6174 Style.AllowAllArgumentsOnNextLine = false; 6175 verifyFormat("void foo() {\n" 6176 " auto VariableWithReallyLongName = {\n" 6177 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6178 " bbbbbbbbbbbb};\n" 6179 "}", 6180 Style); 6181 } 6182 6183 // This parameter should not affect declarations. 6184 Style.BinPackParameters = false; 6185 Style.AllowAllArgumentsOnNextLine = false; 6186 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6187 verifyFormat("void FunctionCallWithReallyLongName(\n" 6188 " int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);", 6189 Style); 6190 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6191 verifyFormat("void FunctionCallWithReallyLongName(\n" 6192 " int aaaaaaaaaaaaaaaaaaaaaaa,\n" 6193 " int bbbbbbbbbbbb);", 6194 Style); 6195 } 6196 6197 TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) { 6198 // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign 6199 // and BAS_Align. 6200 auto Style = getLLVMStyle(); 6201 Style.ColumnLimit = 35; 6202 StringRef Input = "functionCall(paramA, paramB, paramC);\n" 6203 "void functionDecl(int A, int B, int C);"; 6204 Style.AllowAllArgumentsOnNextLine = false; 6205 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6206 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 6207 " paramC);\n" 6208 "void functionDecl(int A, int B,\n" 6209 " int C);"), 6210 format(Input, Style)); 6211 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 6212 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 6213 " paramC);\n" 6214 "void functionDecl(int A, int B,\n" 6215 " int C);"), 6216 format(Input, Style)); 6217 // However, BAS_AlwaysBreak should take precedence over 6218 // AllowAllArgumentsOnNextLine. 6219 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 6220 EXPECT_EQ(StringRef("functionCall(\n" 6221 " paramA, paramB, paramC);\n" 6222 "void functionDecl(\n" 6223 " int A, int B, int C);"), 6224 format(Input, Style)); 6225 6226 // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the 6227 // first argument. 6228 Style.AllowAllArgumentsOnNextLine = true; 6229 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 6230 EXPECT_EQ(StringRef("functionCall(\n" 6231 " paramA, paramB, paramC);\n" 6232 "void functionDecl(\n" 6233 " int A, int B, int C);"), 6234 format(Input, Style)); 6235 // It wouldn't fit on one line with aligned parameters so this setting 6236 // doesn't change anything for BAS_Align. 6237 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 6238 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 6239 " paramC);\n" 6240 "void functionDecl(int A, int B,\n" 6241 " int C);"), 6242 format(Input, Style)); 6243 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6244 EXPECT_EQ(StringRef("functionCall(\n" 6245 " paramA, paramB, paramC);\n" 6246 "void functionDecl(\n" 6247 " int A, int B, int C);"), 6248 format(Input, Style)); 6249 } 6250 6251 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) { 6252 FormatStyle Style = getLLVMStyle(); 6253 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 6254 6255 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 6256 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}", 6257 getStyleWithColumns(Style, 45)); 6258 verifyFormat("Constructor() :\n" 6259 " Initializer(FitsOnTheLine) {}", 6260 getStyleWithColumns(Style, 44)); 6261 verifyFormat("Constructor() :\n" 6262 " Initializer(FitsOnTheLine) {}", 6263 getStyleWithColumns(Style, 43)); 6264 6265 verifyFormat("template <typename T>\n" 6266 "Constructor() : Initializer(FitsOnTheLine) {}", 6267 getStyleWithColumns(Style, 50)); 6268 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 6269 verifyFormat( 6270 "SomeClass::Constructor() :\n" 6271 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 6272 Style); 6273 6274 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = false; 6275 verifyFormat( 6276 "SomeClass::Constructor() :\n" 6277 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 6278 Style); 6279 6280 verifyFormat( 6281 "SomeClass::Constructor() :\n" 6282 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6283 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6284 Style); 6285 verifyFormat( 6286 "SomeClass::Constructor() :\n" 6287 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6288 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 6289 Style); 6290 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6291 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 6292 " aaaaaaaaaa(aaaaaa) {}", 6293 Style); 6294 6295 verifyFormat("Constructor() :\n" 6296 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6297 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6298 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6299 " aaaaaaaaaaaaaaaaaaaaaaa() {}", 6300 Style); 6301 6302 verifyFormat("Constructor() :\n" 6303 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6304 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6305 Style); 6306 6307 verifyFormat("Constructor(int Parameter = 0) :\n" 6308 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 6309 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}", 6310 Style); 6311 verifyFormat("Constructor() :\n" 6312 " aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 6313 "}", 6314 getStyleWithColumns(Style, 60)); 6315 verifyFormat("Constructor() :\n" 6316 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6317 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}", 6318 Style); 6319 6320 // Here a line could be saved by splitting the second initializer onto two 6321 // lines, but that is not desirable. 6322 verifyFormat("Constructor() :\n" 6323 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 6324 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 6325 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6326 Style); 6327 6328 FormatStyle OnePerLine = Style; 6329 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 6330 OnePerLine.AllowAllConstructorInitializersOnNextLine = false; 6331 verifyFormat("SomeClass::Constructor() :\n" 6332 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6333 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6334 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6335 OnePerLine); 6336 verifyFormat("SomeClass::Constructor() :\n" 6337 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 6338 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6339 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6340 OnePerLine); 6341 verifyFormat("MyClass::MyClass(int var) :\n" 6342 " some_var_(var), // 4 space indent\n" 6343 " some_other_var_(var + 1) { // lined up\n" 6344 "}", 6345 OnePerLine); 6346 verifyFormat("Constructor() :\n" 6347 " aaaaa(aaaaaa),\n" 6348 " aaaaa(aaaaaa),\n" 6349 " aaaaa(aaaaaa),\n" 6350 " aaaaa(aaaaaa),\n" 6351 " aaaaa(aaaaaa) {}", 6352 OnePerLine); 6353 verifyFormat("Constructor() :\n" 6354 " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 6355 " aaaaaaaaaaaaaaaaaaaaaa) {}", 6356 OnePerLine); 6357 OnePerLine.BinPackParameters = false; 6358 verifyFormat("Constructor() :\n" 6359 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 6360 " aaaaaaaaaaa().aaa(),\n" 6361 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6362 OnePerLine); 6363 OnePerLine.ColumnLimit = 60; 6364 verifyFormat("Constructor() :\n" 6365 " aaaaaaaaaaaaaaaaaaaa(a),\n" 6366 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 6367 OnePerLine); 6368 6369 EXPECT_EQ("Constructor() :\n" 6370 " // Comment forcing unwanted break.\n" 6371 " aaaa(aaaa) {}", 6372 format("Constructor() :\n" 6373 " // Comment forcing unwanted break.\n" 6374 " aaaa(aaaa) {}", 6375 Style)); 6376 6377 Style.ColumnLimit = 0; 6378 verifyFormat("SomeClass::Constructor() :\n" 6379 " a(a) {}", 6380 Style); 6381 verifyFormat("SomeClass::Constructor() noexcept :\n" 6382 " a(a) {}", 6383 Style); 6384 verifyFormat("SomeClass::Constructor() :\n" 6385 " a(a), b(b), c(c) {}", 6386 Style); 6387 verifyFormat("SomeClass::Constructor() :\n" 6388 " a(a) {\n" 6389 " foo();\n" 6390 " bar();\n" 6391 "}", 6392 Style); 6393 6394 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 6395 verifyFormat("SomeClass::Constructor() :\n" 6396 " a(a), b(b), c(c) {\n" 6397 "}", 6398 Style); 6399 verifyFormat("SomeClass::Constructor() :\n" 6400 " a(a) {\n" 6401 "}", 6402 Style); 6403 6404 Style.ColumnLimit = 80; 6405 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 6406 Style.ConstructorInitializerIndentWidth = 2; 6407 verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style); 6408 verifyFormat("SomeClass::Constructor() :\n" 6409 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6410 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}", 6411 Style); 6412 6413 // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as 6414 // well 6415 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 6416 verifyFormat( 6417 "class SomeClass\n" 6418 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6419 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 6420 Style); 6421 Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 6422 verifyFormat( 6423 "class SomeClass\n" 6424 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6425 " , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 6426 Style); 6427 Style.BreakInheritanceList = FormatStyle::BILS_AfterColon; 6428 verifyFormat( 6429 "class SomeClass :\n" 6430 " public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6431 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 6432 Style); 6433 Style.BreakInheritanceList = FormatStyle::BILS_AfterComma; 6434 verifyFormat( 6435 "class SomeClass\n" 6436 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6437 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 6438 Style); 6439 } 6440 6441 #ifndef EXPENSIVE_CHECKS 6442 // Expensive checks enables libstdc++ checking which includes validating the 6443 // state of ranges used in std::priority_queue - this blows out the 6444 // runtime/scalability of the function and makes this test unacceptably slow. 6445 TEST_F(FormatTest, MemoizationTests) { 6446 // This breaks if the memoization lookup does not take \c Indent and 6447 // \c LastSpace into account. 6448 verifyFormat( 6449 "extern CFRunLoopTimerRef\n" 6450 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 6451 " CFTimeInterval interval, CFOptionFlags flags,\n" 6452 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 6453 " CFRunLoopTimerContext *context) {}"); 6454 6455 // Deep nesting somewhat works around our memoization. 6456 verifyFormat( 6457 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 6458 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 6459 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 6460 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 6461 " aaaaa())))))))))))))))))))))))))))))))))))))));", 6462 getLLVMStyleWithColumns(65)); 6463 verifyFormat( 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(\n" 6481 " aaaaa,\n" 6482 " aaaaa(\n" 6483 " aaaaa,\n" 6484 " aaaaa(\n" 6485 " aaaaa,\n" 6486 " aaaaa(\n" 6487 " aaaaa,\n" 6488 " aaaaa))))))))))));", 6489 getLLVMStyleWithColumns(65)); 6490 verifyFormat( 6491 "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" 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),\n" 6501 " a),\n" 6502 " a),\n" 6503 " a),\n" 6504 " a),\n" 6505 " a),\n" 6506 " a),\n" 6507 " a),\n" 6508 " a)", 6509 getLLVMStyleWithColumns(65)); 6510 6511 // This test takes VERY long when memoization is broken. 6512 FormatStyle OnePerLine = getLLVMStyle(); 6513 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 6514 OnePerLine.BinPackParameters = false; 6515 std::string input = "Constructor()\n" 6516 " : aaaa(a,\n"; 6517 for (unsigned i = 0, e = 80; i != e; ++i) { 6518 input += " a,\n"; 6519 } 6520 input += " a) {}"; 6521 verifyFormat(input, OnePerLine); 6522 } 6523 #endif 6524 6525 TEST_F(FormatTest, BreaksAsHighAsPossible) { 6526 verifyFormat( 6527 "void f() {\n" 6528 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 6529 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 6530 " f();\n" 6531 "}"); 6532 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 6533 " Intervals[i - 1].getRange().getLast()) {\n}"); 6534 } 6535 6536 TEST_F(FormatTest, BreaksFunctionDeclarations) { 6537 // Principially, we break function declarations in a certain order: 6538 // 1) break amongst arguments. 6539 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 6540 " Cccccccccccccc cccccccccccccc);"); 6541 verifyFormat("template <class TemplateIt>\n" 6542 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 6543 " TemplateIt *stop) {}"); 6544 6545 // 2) break after return type. 6546 verifyFormat( 6547 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6548 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", 6549 getGoogleStyle()); 6550 6551 // 3) break after (. 6552 verifyFormat( 6553 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 6554 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", 6555 getGoogleStyle()); 6556 6557 // 4) break before after nested name specifiers. 6558 verifyFormat( 6559 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6560 "SomeClasssssssssssssssssssssssssssssssssssssss::\n" 6561 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", 6562 getGoogleStyle()); 6563 6564 // However, there are exceptions, if a sufficient amount of lines can be 6565 // saved. 6566 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 6567 // more adjusting. 6568 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 6569 " Cccccccccccccc cccccccccc,\n" 6570 " Cccccccccccccc cccccccccc,\n" 6571 " Cccccccccccccc cccccccccc,\n" 6572 " Cccccccccccccc cccccccccc);"); 6573 verifyFormat( 6574 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6575 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6576 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6577 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", 6578 getGoogleStyle()); 6579 verifyFormat( 6580 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 6581 " Cccccccccccccc cccccccccc,\n" 6582 " Cccccccccccccc cccccccccc,\n" 6583 " Cccccccccccccc cccccccccc,\n" 6584 " Cccccccccccccc cccccccccc,\n" 6585 " Cccccccccccccc cccccccccc,\n" 6586 " Cccccccccccccc cccccccccc);"); 6587 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 6588 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6589 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6590 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6591 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 6592 6593 // Break after multi-line parameters. 6594 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6595 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6596 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6597 " bbbb bbbb);"); 6598 verifyFormat("void SomeLoooooooooooongFunction(\n" 6599 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6600 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6601 " int bbbbbbbbbbbbb);"); 6602 6603 // Treat overloaded operators like other functions. 6604 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 6605 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 6606 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 6607 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 6608 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 6609 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 6610 verifyGoogleFormat( 6611 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 6612 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 6613 verifyGoogleFormat( 6614 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 6615 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 6616 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6617 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 6618 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 6619 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 6620 verifyGoogleFormat( 6621 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" 6622 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6623 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); 6624 verifyGoogleFormat("template <typename T>\n" 6625 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6626 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n" 6627 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);"); 6628 6629 FormatStyle Style = getLLVMStyle(); 6630 Style.PointerAlignment = FormatStyle::PAS_Left; 6631 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6632 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", 6633 Style); 6634 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 6635 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6636 Style); 6637 } 6638 6639 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) { 6640 // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516: 6641 // Prefer keeping `::` followed by `operator` together. 6642 EXPECT_EQ("const aaaa::bbbbbbb &\n" 6643 "ccccccccc::operator++() {\n" 6644 " stuff();\n" 6645 "}", 6646 format("const aaaa::bbbbbbb\n" 6647 "&ccccccccc::operator++() { stuff(); }", 6648 getLLVMStyleWithColumns(40))); 6649 } 6650 6651 TEST_F(FormatTest, TrailingReturnType) { 6652 verifyFormat("auto foo() -> int;\n"); 6653 // correct trailing return type spacing 6654 verifyFormat("auto operator->() -> int;\n"); 6655 verifyFormat("auto operator++(int) -> int;\n"); 6656 6657 verifyFormat("struct S {\n" 6658 " auto bar() const -> int;\n" 6659 "};"); 6660 verifyFormat("template <size_t Order, typename T>\n" 6661 "auto load_img(const std::string &filename)\n" 6662 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 6663 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 6664 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 6665 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 6666 verifyFormat("template <typename T>\n" 6667 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" 6668 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); 6669 6670 // Not trailing return types. 6671 verifyFormat("void f() { auto a = b->c(); }"); 6672 } 6673 6674 TEST_F(FormatTest, DeductionGuides) { 6675 verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;"); 6676 verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;"); 6677 verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;"); 6678 verifyFormat( 6679 "template <class... T>\n" 6680 "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;"); 6681 verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;"); 6682 verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;"); 6683 verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;"); 6684 verifyFormat("template <class T> A() -> A<(3 < 2)>;"); 6685 verifyFormat("template <class T> A() -> A<((3) < (2))>;"); 6686 verifyFormat("template <class T> x() -> x<1>;"); 6687 verifyFormat("template <class T> explicit x(T &) -> x<1>;"); 6688 6689 // Ensure not deduction guides. 6690 verifyFormat("c()->f<int>();"); 6691 verifyFormat("x()->foo<1>;"); 6692 verifyFormat("x = p->foo<3>();"); 6693 verifyFormat("x()->x<1>();"); 6694 verifyFormat("x()->x<1>;"); 6695 } 6696 6697 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 6698 // Avoid breaking before trailing 'const' or other trailing annotations, if 6699 // they are not function-like. 6700 FormatStyle Style = getGoogleStyle(); 6701 Style.ColumnLimit = 47; 6702 verifyFormat("void someLongFunction(\n" 6703 " int someLoooooooooooooongParameter) const {\n}", 6704 getLLVMStyleWithColumns(47)); 6705 verifyFormat("LoooooongReturnType\n" 6706 "someLoooooooongFunction() const {}", 6707 getLLVMStyleWithColumns(47)); 6708 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 6709 " const {}", 6710 Style); 6711 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 6712 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 6713 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 6714 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 6715 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 6716 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 6717 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 6718 " aaaaaaaaaaa aaaaa) const override;"); 6719 verifyGoogleFormat( 6720 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 6721 " const override;"); 6722 6723 // Even if the first parameter has to be wrapped. 6724 verifyFormat("void someLongFunction(\n" 6725 " int someLongParameter) const {}", 6726 getLLVMStyleWithColumns(46)); 6727 verifyFormat("void someLongFunction(\n" 6728 " int someLongParameter) const {}", 6729 Style); 6730 verifyFormat("void someLongFunction(\n" 6731 " int someLongParameter) override {}", 6732 Style); 6733 verifyFormat("void someLongFunction(\n" 6734 " int someLongParameter) OVERRIDE {}", 6735 Style); 6736 verifyFormat("void someLongFunction(\n" 6737 " int someLongParameter) final {}", 6738 Style); 6739 verifyFormat("void someLongFunction(\n" 6740 " int someLongParameter) FINAL {}", 6741 Style); 6742 verifyFormat("void someLongFunction(\n" 6743 " int parameter) const override {}", 6744 Style); 6745 6746 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 6747 verifyFormat("void someLongFunction(\n" 6748 " int someLongParameter) const\n" 6749 "{\n" 6750 "}", 6751 Style); 6752 6753 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 6754 verifyFormat("void someLongFunction(\n" 6755 " int someLongParameter) const\n" 6756 " {\n" 6757 " }", 6758 Style); 6759 6760 // Unless these are unknown annotations. 6761 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 6762 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6763 " LONG_AND_UGLY_ANNOTATION;"); 6764 6765 // Breaking before function-like trailing annotations is fine to keep them 6766 // close to their arguments. 6767 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6768 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 6769 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 6770 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 6771 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 6772 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 6773 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 6774 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 6775 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 6776 6777 verifyFormat( 6778 "void aaaaaaaaaaaaaaaaaa()\n" 6779 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 6780 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 6781 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6782 " __attribute__((unused));"); 6783 verifyGoogleFormat( 6784 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6785 " GUARDED_BY(aaaaaaaaaaaa);"); 6786 verifyGoogleFormat( 6787 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6788 " GUARDED_BY(aaaaaaaaaaaa);"); 6789 verifyGoogleFormat( 6790 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 6791 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6792 verifyGoogleFormat( 6793 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 6794 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 6795 } 6796 6797 TEST_F(FormatTest, FunctionAnnotations) { 6798 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 6799 "int OldFunction(const string ¶meter) {}"); 6800 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 6801 "string OldFunction(const string ¶meter) {}"); 6802 verifyFormat("template <typename T>\n" 6803 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 6804 "string OldFunction(const string ¶meter) {}"); 6805 6806 // Not function annotations. 6807 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6808 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 6809 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" 6810 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); 6811 verifyFormat("MACRO(abc).function() // wrap\n" 6812 " << abc;"); 6813 verifyFormat("MACRO(abc)->function() // wrap\n" 6814 " << abc;"); 6815 verifyFormat("MACRO(abc)::function() // wrap\n" 6816 " << abc;"); 6817 } 6818 6819 TEST_F(FormatTest, BreaksDesireably) { 6820 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 6821 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 6822 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 6823 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6824 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 6825 "}"); 6826 6827 verifyFormat( 6828 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6829 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 6830 6831 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6832 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6833 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 6834 6835 verifyFormat( 6836 "aaaaaaaa(aaaaaaaaaaaaa,\n" 6837 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6838 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 6839 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6840 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 6841 6842 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 6843 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6844 6845 verifyFormat( 6846 "void f() {\n" 6847 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 6848 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 6849 "}"); 6850 verifyFormat( 6851 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6852 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 6853 verifyFormat( 6854 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6855 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 6856 verifyFormat( 6857 "aaaaaa(aaa,\n" 6858 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6859 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6860 " aaaa);"); 6861 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6862 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6863 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6864 6865 // Indent consistently independent of call expression and unary operator. 6866 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 6867 " dddddddddddddddddddddddddddddd));"); 6868 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 6869 " dddddddddddddddddddddddddddddd));"); 6870 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 6871 " dddddddddddddddddddddddddddddd));"); 6872 6873 // This test case breaks on an incorrect memoization, i.e. an optimization not 6874 // taking into account the StopAt value. 6875 verifyFormat( 6876 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 6877 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 6878 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 6879 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6880 6881 verifyFormat("{\n {\n {\n" 6882 " Annotation.SpaceRequiredBefore =\n" 6883 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 6884 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 6885 " }\n }\n}"); 6886 6887 // Break on an outer level if there was a break on an inner level. 6888 EXPECT_EQ("f(g(h(a, // comment\n" 6889 " b, c),\n" 6890 " d, e),\n" 6891 " x, y);", 6892 format("f(g(h(a, // comment\n" 6893 " b, c), d, e), x, y);")); 6894 6895 // Prefer breaking similar line breaks. 6896 verifyFormat( 6897 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 6898 " NSTrackingMouseEnteredAndExited |\n" 6899 " NSTrackingActiveAlways;"); 6900 } 6901 6902 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 6903 FormatStyle NoBinPacking = getGoogleStyle(); 6904 NoBinPacking.BinPackParameters = false; 6905 NoBinPacking.BinPackArguments = true; 6906 verifyFormat("void f() {\n" 6907 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 6908 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 6909 "}", 6910 NoBinPacking); 6911 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 6912 " int aaaaaaaaaaaaaaaaaaaa,\n" 6913 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6914 NoBinPacking); 6915 6916 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 6917 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6918 " vector<int> bbbbbbbbbbbbbbb);", 6919 NoBinPacking); 6920 // FIXME: This behavior difference is probably not wanted. However, currently 6921 // we cannot distinguish BreakBeforeParameter being set because of the wrapped 6922 // template arguments from BreakBeforeParameter being set because of the 6923 // one-per-line formatting. 6924 verifyFormat( 6925 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n" 6926 " aaaaaaaaaa> aaaaaaaaaa);", 6927 NoBinPacking); 6928 verifyFormat( 6929 "void fffffffffff(\n" 6930 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n" 6931 " aaaaaaaaaa);"); 6932 } 6933 6934 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 6935 FormatStyle NoBinPacking = getGoogleStyle(); 6936 NoBinPacking.BinPackParameters = false; 6937 NoBinPacking.BinPackArguments = false; 6938 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 6939 " aaaaaaaaaaaaaaaaaaaa,\n" 6940 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 6941 NoBinPacking); 6942 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 6943 " aaaaaaaaaaaaa,\n" 6944 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 6945 NoBinPacking); 6946 verifyFormat( 6947 "aaaaaaaa(aaaaaaaaaaaaa,\n" 6948 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6949 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 6950 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6951 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 6952 NoBinPacking); 6953 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 6954 " .aaaaaaaaaaaaaaaaaa();", 6955 NoBinPacking); 6956 verifyFormat("void f() {\n" 6957 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6958 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 6959 "}", 6960 NoBinPacking); 6961 6962 verifyFormat( 6963 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6964 " aaaaaaaaaaaa,\n" 6965 " aaaaaaaaaaaa);", 6966 NoBinPacking); 6967 verifyFormat( 6968 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 6969 " ddddddddddddddddddddddddddddd),\n" 6970 " test);", 6971 NoBinPacking); 6972 6973 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 6974 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 6975 " aaaaaaaaaaaaaaaaaaaaaaa>\n" 6976 " aaaaaaaaaaaaaaaaaa;", 6977 NoBinPacking); 6978 verifyFormat("a(\"a\"\n" 6979 " \"a\",\n" 6980 " a);"); 6981 6982 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 6983 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 6984 " aaaaaaaaa,\n" 6985 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 6986 NoBinPacking); 6987 verifyFormat( 6988 "void f() {\n" 6989 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 6990 " .aaaaaaa();\n" 6991 "}", 6992 NoBinPacking); 6993 verifyFormat( 6994 "template <class SomeType, class SomeOtherType>\n" 6995 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 6996 NoBinPacking); 6997 } 6998 6999 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 7000 FormatStyle Style = getLLVMStyleWithColumns(15); 7001 Style.ExperimentalAutoDetectBinPacking = true; 7002 EXPECT_EQ("aaa(aaaa,\n" 7003 " aaaa,\n" 7004 " aaaa);\n" 7005 "aaa(aaaa,\n" 7006 " aaaa,\n" 7007 " aaaa);", 7008 format("aaa(aaaa,\n" // one-per-line 7009 " aaaa,\n" 7010 " aaaa );\n" 7011 "aaa(aaaa, aaaa, aaaa);", // inconclusive 7012 Style)); 7013 EXPECT_EQ("aaa(aaaa, aaaa,\n" 7014 " aaaa);\n" 7015 "aaa(aaaa, aaaa,\n" 7016 " aaaa);", 7017 format("aaa(aaaa, aaaa,\n" // bin-packed 7018 " aaaa );\n" 7019 "aaa(aaaa, aaaa, aaaa);", // inconclusive 7020 Style)); 7021 } 7022 7023 TEST_F(FormatTest, FormatsBuilderPattern) { 7024 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 7025 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 7026 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 7027 " .StartsWith(\".init\", ORDER_INIT)\n" 7028 " .StartsWith(\".fini\", ORDER_FINI)\n" 7029 " .StartsWith(\".hash\", ORDER_HASH)\n" 7030 " .Default(ORDER_TEXT);\n"); 7031 7032 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 7033 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 7034 verifyFormat("aaaaaaa->aaaaaaa\n" 7035 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7036 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7037 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 7038 verifyFormat( 7039 "aaaaaaa->aaaaaaa\n" 7040 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7041 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 7042 verifyFormat( 7043 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 7044 " aaaaaaaaaaaaaa);"); 7045 verifyFormat( 7046 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 7047 " aaaaaa->aaaaaaaaaaaa()\n" 7048 " ->aaaaaaaaaaaaaaaa(\n" 7049 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7050 " ->aaaaaaaaaaaaaaaaa();"); 7051 verifyGoogleFormat( 7052 "void f() {\n" 7053 " someo->Add((new util::filetools::Handler(dir))\n" 7054 " ->OnEvent1(NewPermanentCallback(\n" 7055 " this, &HandlerHolderClass::EventHandlerCBA))\n" 7056 " ->OnEvent2(NewPermanentCallback(\n" 7057 " this, &HandlerHolderClass::EventHandlerCBB))\n" 7058 " ->OnEvent3(NewPermanentCallback(\n" 7059 " this, &HandlerHolderClass::EventHandlerCBC))\n" 7060 " ->OnEvent5(NewPermanentCallback(\n" 7061 " this, &HandlerHolderClass::EventHandlerCBD))\n" 7062 " ->OnEvent6(NewPermanentCallback(\n" 7063 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 7064 "}"); 7065 7066 verifyFormat( 7067 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 7068 verifyFormat("aaaaaaaaaaaaaaa()\n" 7069 " .aaaaaaaaaaaaaaa()\n" 7070 " .aaaaaaaaaaaaaaa()\n" 7071 " .aaaaaaaaaaaaaaa()\n" 7072 " .aaaaaaaaaaaaaaa();"); 7073 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 7074 " .aaaaaaaaaaaaaaa()\n" 7075 " .aaaaaaaaaaaaaaa()\n" 7076 " .aaaaaaaaaaaaaaa();"); 7077 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 7078 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 7079 " .aaaaaaaaaaaaaaa();"); 7080 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 7081 " ->aaaaaaaaaaaaaae(0)\n" 7082 " ->aaaaaaaaaaaaaaa();"); 7083 7084 // Don't linewrap after very short segments. 7085 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7086 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7087 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7088 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7089 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7090 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7091 verifyFormat("aaa()\n" 7092 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7093 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7094 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7095 7096 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 7097 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7098 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 7099 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 7100 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 7101 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 7102 7103 // Prefer not to break after empty parentheses. 7104 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 7105 " First->LastNewlineOffset);"); 7106 7107 // Prefer not to create "hanging" indents. 7108 verifyFormat( 7109 "return !soooooooooooooome_map\n" 7110 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7111 " .second;"); 7112 verifyFormat( 7113 "return aaaaaaaaaaaaaaaa\n" 7114 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n" 7115 " .aaaa(aaaaaaaaaaaaaa);"); 7116 // No hanging indent here. 7117 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n" 7118 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7119 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n" 7120 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7121 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 7122 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7123 getLLVMStyleWithColumns(60)); 7124 verifyFormat("aaaaaaaaaaaaaaaaaa\n" 7125 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 7126 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7127 getLLVMStyleWithColumns(59)); 7128 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7129 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7130 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7131 7132 // Dont break if only closing statements before member call 7133 verifyFormat("test() {\n" 7134 " ([]() -> {\n" 7135 " int b = 32;\n" 7136 " return 3;\n" 7137 " }).foo();\n" 7138 "}"); 7139 verifyFormat("test() {\n" 7140 " (\n" 7141 " []() -> {\n" 7142 " int b = 32;\n" 7143 " return 3;\n" 7144 " },\n" 7145 " foo, bar)\n" 7146 " .foo();\n" 7147 "}"); 7148 verifyFormat("test() {\n" 7149 " ([]() -> {\n" 7150 " int b = 32;\n" 7151 " return 3;\n" 7152 " })\n" 7153 " .foo()\n" 7154 " .bar();\n" 7155 "}"); 7156 verifyFormat("test() {\n" 7157 " ([]() -> {\n" 7158 " int b = 32;\n" 7159 " return 3;\n" 7160 " })\n" 7161 " .foo(\"aaaaaaaaaaaaaaaaa\"\n" 7162 " \"bbbb\");\n" 7163 "}", 7164 getLLVMStyleWithColumns(30)); 7165 } 7166 7167 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 7168 verifyFormat( 7169 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7170 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 7171 verifyFormat( 7172 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 7173 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 7174 7175 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 7176 " ccccccccccccccccccccccccc) {\n}"); 7177 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 7178 " ccccccccccccccccccccccccc) {\n}"); 7179 7180 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 7181 " ccccccccccccccccccccccccc) {\n}"); 7182 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 7183 " ccccccccccccccccccccccccc) {\n}"); 7184 7185 verifyFormat( 7186 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 7187 " ccccccccccccccccccccccccc) {\n}"); 7188 verifyFormat( 7189 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 7190 " ccccccccccccccccccccccccc) {\n}"); 7191 7192 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 7193 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 7194 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 7195 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 7196 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 7197 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 7198 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 7199 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 7200 7201 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 7202 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 7203 " aaaaaaaaaaaaaaa != aa) {\n}"); 7204 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 7205 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 7206 " aaaaaaaaaaaaaaa != aa) {\n}"); 7207 } 7208 7209 TEST_F(FormatTest, BreaksAfterAssignments) { 7210 verifyFormat( 7211 "unsigned Cost =\n" 7212 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 7213 " SI->getPointerAddressSpaceee());\n"); 7214 verifyFormat( 7215 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 7216 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 7217 7218 verifyFormat( 7219 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 7220 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 7221 verifyFormat("unsigned OriginalStartColumn =\n" 7222 " SourceMgr.getSpellingColumnNumber(\n" 7223 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 7224 " 1;"); 7225 } 7226 7227 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) { 7228 FormatStyle Style = getLLVMStyle(); 7229 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 7230 " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;", 7231 Style); 7232 7233 Style.PenaltyBreakAssignment = 20; 7234 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 7235 " cccccccccccccccccccccccccc;", 7236 Style); 7237 } 7238 7239 TEST_F(FormatTest, AlignsAfterAssignments) { 7240 verifyFormat( 7241 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7242 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7243 verifyFormat( 7244 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7245 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7246 verifyFormat( 7247 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7248 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7249 verifyFormat( 7250 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7251 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 7252 verifyFormat( 7253 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 7254 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 7255 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 7256 } 7257 7258 TEST_F(FormatTest, AlignsAfterReturn) { 7259 verifyFormat( 7260 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7261 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7262 verifyFormat( 7263 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7264 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 7265 verifyFormat( 7266 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 7267 " aaaaaaaaaaaaaaaaaaaaaa();"); 7268 verifyFormat( 7269 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 7270 " aaaaaaaaaaaaaaaaaaaaaa());"); 7271 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7272 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7273 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7274 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 7275 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7276 verifyFormat("return\n" 7277 " // true if code is one of a or b.\n" 7278 " code == a || code == b;"); 7279 } 7280 7281 TEST_F(FormatTest, AlignsAfterOpenBracket) { 7282 verifyFormat( 7283 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 7284 " aaaaaaaaa aaaaaaa) {}"); 7285 verifyFormat( 7286 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 7287 " aaaaaaaaaaa aaaaaaaaa);"); 7288 verifyFormat( 7289 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 7290 " aaaaaaaaaaaaaaaaaaaaa));"); 7291 FormatStyle Style = getLLVMStyle(); 7292 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7293 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7294 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 7295 Style); 7296 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 7297 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 7298 Style); 7299 verifyFormat("SomeLongVariableName->someFunction(\n" 7300 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 7301 Style); 7302 verifyFormat( 7303 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 7304 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 7305 Style); 7306 verifyFormat( 7307 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 7308 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7309 Style); 7310 verifyFormat( 7311 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 7312 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 7313 Style); 7314 7315 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n" 7316 " ccccccc(aaaaaaaaaaaaaaaaa, //\n" 7317 " b));", 7318 Style); 7319 7320 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 7321 Style.BinPackArguments = false; 7322 Style.BinPackParameters = false; 7323 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7324 " aaaaaaaaaaa aaaaaaaa,\n" 7325 " aaaaaaaaa aaaaaaa,\n" 7326 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 7327 Style); 7328 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 7329 " aaaaaaaaaaa aaaaaaaaa,\n" 7330 " aaaaaaaaaaa aaaaaaaaa,\n" 7331 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7332 Style); 7333 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" 7334 " aaaaaaaaaaaaaaa,\n" 7335 " aaaaaaaaaaaaaaaaaaaaa,\n" 7336 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 7337 Style); 7338 verifyFormat( 7339 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" 7340 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 7341 Style); 7342 verifyFormat( 7343 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" 7344 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 7345 Style); 7346 verifyFormat( 7347 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 7348 " aaaaaaaaaaaaaaaaaaaaa(\n" 7349 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n" 7350 " aaaaaaaaaaaaaaaa);", 7351 Style); 7352 verifyFormat( 7353 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 7354 " aaaaaaaaaaaaaaaaaaaaa(\n" 7355 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n" 7356 " aaaaaaaaaaaaaaaa);", 7357 Style); 7358 } 7359 7360 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 7361 FormatStyle Style = getLLVMStyleWithColumns(40); 7362 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7363 " bbbbbbbbbbbbbbbbbbbbbb);", 7364 Style); 7365 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 7366 Style.AlignOperands = FormatStyle::OAS_DontAlign; 7367 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7368 " bbbbbbbbbbbbbbbbbbbbbb);", 7369 Style); 7370 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7371 Style.AlignOperands = FormatStyle::OAS_Align; 7372 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7373 " bbbbbbbbbbbbbbbbbbbbbb);", 7374 Style); 7375 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7376 Style.AlignOperands = FormatStyle::OAS_DontAlign; 7377 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7378 " bbbbbbbbbbbbbbbbbbbbbb);", 7379 Style); 7380 } 7381 7382 TEST_F(FormatTest, BreaksConditionalExpressions) { 7383 verifyFormat( 7384 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7385 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7386 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7387 verifyFormat( 7388 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 7389 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7390 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7391 verifyFormat( 7392 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7393 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7394 verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n" 7395 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7396 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7397 verifyFormat( 7398 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 7399 " : aaaaaaaaaaaaa);"); 7400 verifyFormat( 7401 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7402 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7403 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7404 " aaaaaaaaaaaaa);"); 7405 verifyFormat( 7406 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7407 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7408 " aaaaaaaaaaaaa);"); 7409 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7410 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7411 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7412 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7413 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7414 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7415 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7416 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7417 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7418 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7419 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7420 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7421 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7422 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7423 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7424 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7425 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7426 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7427 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7428 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7429 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 7430 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7431 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7432 " : aaaaaaaaaaaaaaaa;"); 7433 verifyFormat( 7434 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7435 " ? aaaaaaaaaaaaaaa\n" 7436 " : aaaaaaaaaaaaaaa;"); 7437 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 7438 " aaaaaaaaa\n" 7439 " ? b\n" 7440 " : c);"); 7441 verifyFormat("return aaaa == bbbb\n" 7442 " // comment\n" 7443 " ? aaaa\n" 7444 " : bbbb;"); 7445 verifyFormat("unsigned Indent =\n" 7446 " format(TheLine.First,\n" 7447 " IndentForLevel[TheLine.Level] >= 0\n" 7448 " ? IndentForLevel[TheLine.Level]\n" 7449 " : TheLine * 2,\n" 7450 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 7451 getLLVMStyleWithColumns(60)); 7452 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 7453 " ? aaaaaaaaaaaaaaa\n" 7454 " : bbbbbbbbbbbbbbb //\n" 7455 " ? ccccccccccccccc\n" 7456 " : ddddddddddddddd;"); 7457 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 7458 " ? aaaaaaaaaaaaaaa\n" 7459 " : (bbbbbbbbbbbbbbb //\n" 7460 " ? ccccccccccccccc\n" 7461 " : ddddddddddddddd);"); 7462 verifyFormat( 7463 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7464 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7465 " aaaaaaaaaaaaaaaaaaaaa +\n" 7466 " aaaaaaaaaaaaaaaaaaaaa\n" 7467 " : aaaaaaaaaa;"); 7468 verifyFormat( 7469 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7470 " : aaaaaaaaaaaaaaaaaaaaaa\n" 7471 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7472 7473 FormatStyle NoBinPacking = getLLVMStyle(); 7474 NoBinPacking.BinPackArguments = false; 7475 verifyFormat( 7476 "void f() {\n" 7477 " g(aaa,\n" 7478 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 7479 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7480 " ? aaaaaaaaaaaaaaa\n" 7481 " : aaaaaaaaaaaaaaa);\n" 7482 "}", 7483 NoBinPacking); 7484 verifyFormat( 7485 "void f() {\n" 7486 " g(aaa,\n" 7487 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 7488 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7489 " ?: aaaaaaaaaaaaaaa);\n" 7490 "}", 7491 NoBinPacking); 7492 7493 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 7494 " // comment.\n" 7495 " ccccccccccccccccccccccccccccccccccccccc\n" 7496 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7497 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 7498 7499 // Assignments in conditional expressions. Apparently not uncommon :-(. 7500 verifyFormat("return a != b\n" 7501 " // comment\n" 7502 " ? a = b\n" 7503 " : a = b;"); 7504 verifyFormat("return a != b\n" 7505 " // comment\n" 7506 " ? a = a != b\n" 7507 " // comment\n" 7508 " ? a = b\n" 7509 " : a\n" 7510 " : a;\n"); 7511 verifyFormat("return a != b\n" 7512 " // comment\n" 7513 " ? a\n" 7514 " : a = a != b\n" 7515 " // comment\n" 7516 " ? a = b\n" 7517 " : a;"); 7518 7519 // Chained conditionals 7520 FormatStyle Style = getLLVMStyle(); 7521 Style.ColumnLimit = 70; 7522 Style.AlignOperands = FormatStyle::OAS_Align; 7523 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7524 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7525 " : 3333333333333333;", 7526 Style); 7527 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7528 " : bbbbbbbbbb ? 2222222222222222\n" 7529 " : 3333333333333333;", 7530 Style); 7531 verifyFormat("return aaaaaaaaaa ? 1111111111111111\n" 7532 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 7533 " : 3333333333333333;", 7534 Style); 7535 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7536 " : bbbbbbbbbbbbbb ? 222222\n" 7537 " : 333333;", 7538 Style); 7539 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7540 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7541 " : cccccccccccccc ? 3333333333333333\n" 7542 " : 4444444444444444;", 7543 Style); 7544 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc)\n" 7545 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7546 " : 3333333333333333;", 7547 Style); 7548 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7549 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7550 " : (aaa ? bbb : ccc);", 7551 Style); 7552 verifyFormat( 7553 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7554 " : cccccccccccccccccc)\n" 7555 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7556 " : 3333333333333333;", 7557 Style); 7558 verifyFormat( 7559 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7560 " : cccccccccccccccccc)\n" 7561 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7562 " : 3333333333333333;", 7563 Style); 7564 verifyFormat( 7565 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7566 " : dddddddddddddddddd)\n" 7567 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7568 " : 3333333333333333;", 7569 Style); 7570 verifyFormat( 7571 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7572 " : dddddddddddddddddd)\n" 7573 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7574 " : 3333333333333333;", 7575 Style); 7576 verifyFormat( 7577 "return aaaaaaaaa ? 1111111111111111\n" 7578 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7579 " : a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7580 " : dddddddddddddddddd)\n", 7581 Style); 7582 verifyFormat( 7583 "return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7584 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7585 " : (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7586 " : cccccccccccccccccc);", 7587 Style); 7588 verifyFormat( 7589 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7590 " : ccccccccccccccc ? dddddddddddddddddd\n" 7591 " : eeeeeeeeeeeeeeeeee)\n" 7592 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7593 " : 3333333333333333;", 7594 Style); 7595 verifyFormat( 7596 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7597 " : ccccccccccccccc ? dddddddddddddddddd\n" 7598 " : eeeeeeeeeeeeeeeeee)\n" 7599 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7600 " : 3333333333333333;", 7601 Style); 7602 verifyFormat( 7603 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7604 " : cccccccccccc ? dddddddddddddddddd\n" 7605 " : eeeeeeeeeeeeeeeeee)\n" 7606 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7607 " : 3333333333333333;", 7608 Style); 7609 verifyFormat( 7610 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7611 " : cccccccccccccccccc\n" 7612 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7613 " : 3333333333333333;", 7614 Style); 7615 verifyFormat( 7616 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7617 " : cccccccccccccccc ? dddddddddddddddddd\n" 7618 " : eeeeeeeeeeeeeeeeee\n" 7619 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7620 " : 3333333333333333;", 7621 Style); 7622 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa\n" 7623 " ? (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7624 " : cccccccccccccccccc ? dddddddddddddddddd\n" 7625 " : eeeeeeeeeeeeeeeeee)\n" 7626 " : bbbbbbbbbbbbbbbbbbb ? 2222222222222222\n" 7627 " : 3333333333333333;", 7628 Style); 7629 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaa\n" 7630 " ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7631 " : cccccccccccccccc ? dddddddddddddddddd\n" 7632 " : eeeeeeeeeeeeeeeeee\n" 7633 " : bbbbbbbbbbbbbbbbbbbbbbb ? 2222222222222222\n" 7634 " : 3333333333333333;", 7635 Style); 7636 7637 Style.AlignOperands = FormatStyle::OAS_DontAlign; 7638 Style.BreakBeforeTernaryOperators = false; 7639 // FIXME: Aligning the question marks is weird given DontAlign. 7640 // Consider disabling this alignment in this case. Also check whether this 7641 // will render the adjustment from https://reviews.llvm.org/D82199 7642 // unnecessary. 7643 verifyFormat("int x = aaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa :\n" 7644 " bbbb ? cccccccccccccccccc :\n" 7645 " ddddd;\n", 7646 Style); 7647 7648 EXPECT_EQ( 7649 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n" 7650 " /*\n" 7651 " */\n" 7652 " function() {\n" 7653 " try {\n" 7654 " return JJJJJJJJJJJJJJ(\n" 7655 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n" 7656 " }\n" 7657 " } :\n" 7658 " function() {};", 7659 format( 7660 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n" 7661 " /*\n" 7662 " */\n" 7663 " function() {\n" 7664 " try {\n" 7665 " return JJJJJJJJJJJJJJ(\n" 7666 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n" 7667 " }\n" 7668 " } :\n" 7669 " function() {};", 7670 getGoogleStyle(FormatStyle::LK_JavaScript))); 7671 } 7672 7673 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 7674 FormatStyle Style = getLLVMStyle(); 7675 Style.BreakBeforeTernaryOperators = false; 7676 Style.ColumnLimit = 70; 7677 verifyFormat( 7678 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7679 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7680 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7681 Style); 7682 verifyFormat( 7683 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 7684 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7685 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7686 Style); 7687 verifyFormat( 7688 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7689 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7690 Style); 7691 verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n" 7692 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7693 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7694 Style); 7695 verifyFormat( 7696 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 7697 " aaaaaaaaaaaaa);", 7698 Style); 7699 verifyFormat( 7700 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7701 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7702 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7703 " aaaaaaaaaaaaa);", 7704 Style); 7705 verifyFormat( 7706 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7707 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7708 " aaaaaaaaaaaaa);", 7709 Style); 7710 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7711 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7712 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 7713 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7714 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7715 Style); 7716 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7717 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7718 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7719 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 7720 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7721 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7722 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7723 Style); 7724 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7725 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 7726 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7727 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7728 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7729 Style); 7730 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7731 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7732 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 7733 Style); 7734 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 7735 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7736 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7737 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 7738 Style); 7739 verifyFormat( 7740 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7741 " aaaaaaaaaaaaaaa :\n" 7742 " aaaaaaaaaaaaaaa;", 7743 Style); 7744 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 7745 " aaaaaaaaa ?\n" 7746 " b :\n" 7747 " c);", 7748 Style); 7749 verifyFormat("unsigned Indent =\n" 7750 " format(TheLine.First,\n" 7751 " IndentForLevel[TheLine.Level] >= 0 ?\n" 7752 " IndentForLevel[TheLine.Level] :\n" 7753 " TheLine * 2,\n" 7754 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 7755 Style); 7756 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 7757 " aaaaaaaaaaaaaaa :\n" 7758 " bbbbbbbbbbbbbbb ? //\n" 7759 " ccccccccccccccc :\n" 7760 " ddddddddddddddd;", 7761 Style); 7762 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 7763 " aaaaaaaaaaaaaaa :\n" 7764 " (bbbbbbbbbbbbbbb ? //\n" 7765 " ccccccccccccccc :\n" 7766 " ddddddddddddddd);", 7767 Style); 7768 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7769 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n" 7770 " ccccccccccccccccccccccccccc;", 7771 Style); 7772 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7773 " aaaaa :\n" 7774 " bbbbbbbbbbbbbbb + cccccccccccccccc;", 7775 Style); 7776 7777 // Chained conditionals 7778 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7779 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7780 " 3333333333333333;", 7781 Style); 7782 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7783 " bbbbbbbbbb ? 2222222222222222 :\n" 7784 " 3333333333333333;", 7785 Style); 7786 verifyFormat("return aaaaaaaaaa ? 1111111111111111 :\n" 7787 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7788 " 3333333333333333;", 7789 Style); 7790 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7791 " bbbbbbbbbbbbbbbb ? 222222 :\n" 7792 " 333333;", 7793 Style); 7794 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7795 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7796 " cccccccccccccccc ? 3333333333333333 :\n" 7797 " 4444444444444444;", 7798 Style); 7799 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc) :\n" 7800 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7801 " 3333333333333333;", 7802 Style); 7803 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7804 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7805 " (aaa ? bbb : ccc);", 7806 Style); 7807 verifyFormat( 7808 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7809 " cccccccccccccccccc) :\n" 7810 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7811 " 3333333333333333;", 7812 Style); 7813 verifyFormat( 7814 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7815 " cccccccccccccccccc) :\n" 7816 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7817 " 3333333333333333;", 7818 Style); 7819 verifyFormat( 7820 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7821 " dddddddddddddddddd) :\n" 7822 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7823 " 3333333333333333;", 7824 Style); 7825 verifyFormat( 7826 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7827 " dddddddddddddddddd) :\n" 7828 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7829 " 3333333333333333;", 7830 Style); 7831 verifyFormat( 7832 "return aaaaaaaaa ? 1111111111111111 :\n" 7833 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7834 " a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7835 " dddddddddddddddddd)\n", 7836 Style); 7837 verifyFormat( 7838 "return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7839 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7840 " (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7841 " cccccccccccccccccc);", 7842 Style); 7843 verifyFormat( 7844 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7845 " ccccccccccccccccc ? dddddddddddddddddd :\n" 7846 " eeeeeeeeeeeeeeeeee) :\n" 7847 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7848 " 3333333333333333;", 7849 Style); 7850 verifyFormat( 7851 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7852 " ccccccccccccc ? dddddddddddddddddd :\n" 7853 " eeeeeeeeeeeeeeeeee) :\n" 7854 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7855 " 3333333333333333;", 7856 Style); 7857 verifyFormat( 7858 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7859 " ccccccccccccccccc ? dddddddddddddddddd :\n" 7860 " eeeeeeeeeeeeeeeeee) :\n" 7861 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7862 " 3333333333333333;", 7863 Style); 7864 verifyFormat( 7865 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7866 " cccccccccccccccccc :\n" 7867 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7868 " 3333333333333333;", 7869 Style); 7870 verifyFormat( 7871 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7872 " cccccccccccccccccc ? dddddddddddddddddd :\n" 7873 " eeeeeeeeeeeeeeeeee :\n" 7874 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7875 " 3333333333333333;", 7876 Style); 7877 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n" 7878 " (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7879 " cccccccccccccccccc ? dddddddddddddddddd :\n" 7880 " eeeeeeeeeeeeeeeeee) :\n" 7881 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7882 " 3333333333333333;", 7883 Style); 7884 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n" 7885 " aaaaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7886 " cccccccccccccccccccc ? dddddddddddddddddd :\n" 7887 " eeeeeeeeeeeeeeeeee :\n" 7888 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7889 " 3333333333333333;", 7890 Style); 7891 } 7892 7893 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 7894 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 7895 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 7896 verifyFormat("bool a = true, b = false;"); 7897 7898 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 7899 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 7900 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 7901 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 7902 verifyFormat( 7903 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 7904 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 7905 " d = e && f;"); 7906 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 7907 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 7908 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 7909 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 7910 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 7911 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 7912 7913 FormatStyle Style = getGoogleStyle(); 7914 Style.PointerAlignment = FormatStyle::PAS_Left; 7915 Style.DerivePointerAlignment = false; 7916 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7917 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 7918 " *b = bbbbbbbbbbbbbbbbbbb;", 7919 Style); 7920 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 7921 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 7922 Style); 7923 verifyFormat("vector<int*> a, b;", Style); 7924 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style); 7925 } 7926 7927 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 7928 verifyFormat("arr[foo ? bar : baz];"); 7929 verifyFormat("f()[foo ? bar : baz];"); 7930 verifyFormat("(a + b)[foo ? bar : baz];"); 7931 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 7932 } 7933 7934 TEST_F(FormatTest, AlignsStringLiterals) { 7935 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 7936 " \"short literal\");"); 7937 verifyFormat( 7938 "looooooooooooooooooooooooongFunction(\n" 7939 " \"short literal\"\n" 7940 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 7941 verifyFormat("someFunction(\"Always break between multi-line\"\n" 7942 " \" string literals\",\n" 7943 " and, other, parameters);"); 7944 EXPECT_EQ("fun + \"1243\" /* comment */\n" 7945 " \"5678\";", 7946 format("fun + \"1243\" /* comment */\n" 7947 " \"5678\";", 7948 getLLVMStyleWithColumns(28))); 7949 EXPECT_EQ( 7950 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7951 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 7952 " \"aaaaaaaaaaaaaaaa\";", 7953 format("aaaaaa =" 7954 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 7955 "aaaaaaaaaaaaaaaaaaaaa\" " 7956 "\"aaaaaaaaaaaaaaaa\";")); 7957 verifyFormat("a = a + \"a\"\n" 7958 " \"a\"\n" 7959 " \"a\";"); 7960 verifyFormat("f(\"a\", \"b\"\n" 7961 " \"c\");"); 7962 7963 verifyFormat( 7964 "#define LL_FORMAT \"ll\"\n" 7965 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 7966 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 7967 7968 verifyFormat("#define A(X) \\\n" 7969 " \"aaaaa\" #X \"bbbbbb\" \\\n" 7970 " \"ccccc\"", 7971 getLLVMStyleWithColumns(23)); 7972 verifyFormat("#define A \"def\"\n" 7973 "f(\"abc\" A \"ghi\"\n" 7974 " \"jkl\");"); 7975 7976 verifyFormat("f(L\"a\"\n" 7977 " L\"b\");"); 7978 verifyFormat("#define A(X) \\\n" 7979 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 7980 " L\"ccccc\"", 7981 getLLVMStyleWithColumns(25)); 7982 7983 verifyFormat("f(@\"a\"\n" 7984 " @\"b\");"); 7985 verifyFormat("NSString s = @\"a\"\n" 7986 " @\"b\"\n" 7987 " @\"c\";"); 7988 verifyFormat("NSString s = @\"a\"\n" 7989 " \"b\"\n" 7990 " \"c\";"); 7991 } 7992 7993 TEST_F(FormatTest, ReturnTypeBreakingStyle) { 7994 FormatStyle Style = getLLVMStyle(); 7995 // No declarations or definitions should be moved to own line. 7996 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; 7997 verifyFormat("class A {\n" 7998 " int f() { return 1; }\n" 7999 " int g();\n" 8000 "};\n" 8001 "int f() { return 1; }\n" 8002 "int g();\n", 8003 Style); 8004 8005 // All declarations and definitions should have the return type moved to its 8006 // own line. 8007 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 8008 Style.TypenameMacros = {"LIST"}; 8009 verifyFormat("SomeType\n" 8010 "funcdecl(LIST(uint64_t));", 8011 Style); 8012 verifyFormat("class E {\n" 8013 " int\n" 8014 " f() {\n" 8015 " return 1;\n" 8016 " }\n" 8017 " int\n" 8018 " g();\n" 8019 "};\n" 8020 "int\n" 8021 "f() {\n" 8022 " return 1;\n" 8023 "}\n" 8024 "int\n" 8025 "g();\n", 8026 Style); 8027 8028 // Top-level definitions, and no kinds of declarations should have the 8029 // return type moved to its own line. 8030 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; 8031 verifyFormat("class B {\n" 8032 " int f() { return 1; }\n" 8033 " int g();\n" 8034 "};\n" 8035 "int\n" 8036 "f() {\n" 8037 " return 1;\n" 8038 "}\n" 8039 "int g();\n", 8040 Style); 8041 8042 // Top-level definitions and declarations should have the return type moved 8043 // to its own line. 8044 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel; 8045 verifyFormat("class C {\n" 8046 " int f() { return 1; }\n" 8047 " int g();\n" 8048 "};\n" 8049 "int\n" 8050 "f() {\n" 8051 " return 1;\n" 8052 "}\n" 8053 "int\n" 8054 "g();\n", 8055 Style); 8056 8057 // All definitions should have the return type moved to its own line, but no 8058 // kinds of declarations. 8059 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 8060 verifyFormat("class D {\n" 8061 " int\n" 8062 " f() {\n" 8063 " return 1;\n" 8064 " }\n" 8065 " int g();\n" 8066 "};\n" 8067 "int\n" 8068 "f() {\n" 8069 " return 1;\n" 8070 "}\n" 8071 "int g();\n", 8072 Style); 8073 verifyFormat("const char *\n" 8074 "f(void) {\n" // Break here. 8075 " return \"\";\n" 8076 "}\n" 8077 "const char *bar(void);\n", // No break here. 8078 Style); 8079 verifyFormat("template <class T>\n" 8080 "T *\n" 8081 "f(T &c) {\n" // Break here. 8082 " return NULL;\n" 8083 "}\n" 8084 "template <class T> T *f(T &c);\n", // No break here. 8085 Style); 8086 verifyFormat("class C {\n" 8087 " int\n" 8088 " operator+() {\n" 8089 " return 1;\n" 8090 " }\n" 8091 " int\n" 8092 " operator()() {\n" 8093 " return 1;\n" 8094 " }\n" 8095 "};\n", 8096 Style); 8097 verifyFormat("void\n" 8098 "A::operator()() {}\n" 8099 "void\n" 8100 "A::operator>>() {}\n" 8101 "void\n" 8102 "A::operator+() {}\n" 8103 "void\n" 8104 "A::operator*() {}\n" 8105 "void\n" 8106 "A::operator->() {}\n" 8107 "void\n" 8108 "A::operator void *() {}\n" 8109 "void\n" 8110 "A::operator void &() {}\n" 8111 "void\n" 8112 "A::operator void &&() {}\n" 8113 "void\n" 8114 "A::operator char *() {}\n" 8115 "void\n" 8116 "A::operator[]() {}\n" 8117 "void\n" 8118 "A::operator!() {}\n" 8119 "void\n" 8120 "A::operator**() {}\n" 8121 "void\n" 8122 "A::operator<Foo> *() {}\n" 8123 "void\n" 8124 "A::operator<Foo> **() {}\n" 8125 "void\n" 8126 "A::operator<Foo> &() {}\n" 8127 "void\n" 8128 "A::operator void **() {}\n", 8129 Style); 8130 verifyFormat("constexpr auto\n" 8131 "operator()() const -> reference {}\n" 8132 "constexpr auto\n" 8133 "operator>>() const -> reference {}\n" 8134 "constexpr auto\n" 8135 "operator+() const -> reference {}\n" 8136 "constexpr auto\n" 8137 "operator*() const -> reference {}\n" 8138 "constexpr auto\n" 8139 "operator->() const -> reference {}\n" 8140 "constexpr auto\n" 8141 "operator++() const -> reference {}\n" 8142 "constexpr auto\n" 8143 "operator void *() const -> reference {}\n" 8144 "constexpr auto\n" 8145 "operator void **() const -> reference {}\n" 8146 "constexpr auto\n" 8147 "operator void *() const -> reference {}\n" 8148 "constexpr auto\n" 8149 "operator void &() const -> reference {}\n" 8150 "constexpr auto\n" 8151 "operator void &&() const -> reference {}\n" 8152 "constexpr auto\n" 8153 "operator char *() const -> reference {}\n" 8154 "constexpr auto\n" 8155 "operator!() const -> reference {}\n" 8156 "constexpr auto\n" 8157 "operator[]() const -> reference {}\n", 8158 Style); 8159 verifyFormat("void *operator new(std::size_t s);", // No break here. 8160 Style); 8161 verifyFormat("void *\n" 8162 "operator new(std::size_t s) {}", 8163 Style); 8164 verifyFormat("void *\n" 8165 "operator delete[](void *ptr) {}", 8166 Style); 8167 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 8168 verifyFormat("const char *\n" 8169 "f(void)\n" // Break here. 8170 "{\n" 8171 " return \"\";\n" 8172 "}\n" 8173 "const char *bar(void);\n", // No break here. 8174 Style); 8175 verifyFormat("template <class T>\n" 8176 "T *\n" // Problem here: no line break 8177 "f(T &c)\n" // Break here. 8178 "{\n" 8179 " return NULL;\n" 8180 "}\n" 8181 "template <class T> T *f(T &c);\n", // No break here. 8182 Style); 8183 verifyFormat("int\n" 8184 "foo(A<bool> a)\n" 8185 "{\n" 8186 " return a;\n" 8187 "}\n", 8188 Style); 8189 verifyFormat("int\n" 8190 "foo(A<8> a)\n" 8191 "{\n" 8192 " return a;\n" 8193 "}\n", 8194 Style); 8195 verifyFormat("int\n" 8196 "foo(A<B<bool>, 8> a)\n" 8197 "{\n" 8198 " return a;\n" 8199 "}\n", 8200 Style); 8201 verifyFormat("int\n" 8202 "foo(A<B<8>, bool> a)\n" 8203 "{\n" 8204 " return a;\n" 8205 "}\n", 8206 Style); 8207 verifyFormat("int\n" 8208 "foo(A<B<bool>, bool> a)\n" 8209 "{\n" 8210 " return a;\n" 8211 "}\n", 8212 Style); 8213 verifyFormat("int\n" 8214 "foo(A<B<8>, 8> a)\n" 8215 "{\n" 8216 " return a;\n" 8217 "}\n", 8218 Style); 8219 8220 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 8221 Style.BraceWrapping.AfterFunction = true; 8222 verifyFormat("int f(i);\n" // No break here. 8223 "int\n" // Break here. 8224 "f(i)\n" 8225 "{\n" 8226 " return i + 1;\n" 8227 "}", 8228 Style); 8229 verifyFormat("int f(a, b, c);\n" // No break here. 8230 "int\n" // Break here. 8231 "f(a, b, c)\n" // Break here. 8232 "short a, b;\n" 8233 "float c;\n" 8234 "{\n" 8235 " return a + b < c;\n" 8236 "}", 8237 Style); 8238 8239 Style = getGNUStyle(); 8240 8241 // Test for comments at the end of function declarations. 8242 verifyFormat("void\n" 8243 "foo (int a, /*abc*/ int b) // def\n" 8244 "{\n" 8245 "}\n", 8246 Style); 8247 8248 verifyFormat("void\n" 8249 "foo (int a, /* abc */ int b) /* def */\n" 8250 "{\n" 8251 "}\n", 8252 Style); 8253 8254 // Definitions that should not break after return type 8255 verifyFormat("void foo (int a, int b); // def\n", Style); 8256 verifyFormat("void foo (int a, int b); /* def */\n", Style); 8257 verifyFormat("void foo (int a, int b);\n", Style); 8258 } 8259 8260 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 8261 FormatStyle NoBreak = getLLVMStyle(); 8262 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 8263 FormatStyle Break = getLLVMStyle(); 8264 Break.AlwaysBreakBeforeMultilineStrings = true; 8265 verifyFormat("aaaa = \"bbbb\"\n" 8266 " \"cccc\";", 8267 NoBreak); 8268 verifyFormat("aaaa =\n" 8269 " \"bbbb\"\n" 8270 " \"cccc\";", 8271 Break); 8272 verifyFormat("aaaa(\"bbbb\"\n" 8273 " \"cccc\");", 8274 NoBreak); 8275 verifyFormat("aaaa(\n" 8276 " \"bbbb\"\n" 8277 " \"cccc\");", 8278 Break); 8279 verifyFormat("aaaa(qqq, \"bbbb\"\n" 8280 " \"cccc\");", 8281 NoBreak); 8282 verifyFormat("aaaa(qqq,\n" 8283 " \"bbbb\"\n" 8284 " \"cccc\");", 8285 Break); 8286 verifyFormat("aaaa(qqq,\n" 8287 " L\"bbbb\"\n" 8288 " L\"cccc\");", 8289 Break); 8290 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 8291 " \"bbbb\"));", 8292 Break); 8293 verifyFormat("string s = someFunction(\n" 8294 " \"abc\"\n" 8295 " \"abc\");", 8296 Break); 8297 8298 // As we break before unary operators, breaking right after them is bad. 8299 verifyFormat("string foo = abc ? \"x\"\n" 8300 " \"blah blah blah blah blah blah\"\n" 8301 " : \"y\";", 8302 Break); 8303 8304 // Don't break if there is no column gain. 8305 verifyFormat("f(\"aaaa\"\n" 8306 " \"bbbb\");", 8307 Break); 8308 8309 // Treat literals with escaped newlines like multi-line string literals. 8310 EXPECT_EQ("x = \"a\\\n" 8311 "b\\\n" 8312 "c\";", 8313 format("x = \"a\\\n" 8314 "b\\\n" 8315 "c\";", 8316 NoBreak)); 8317 EXPECT_EQ("xxxx =\n" 8318 " \"a\\\n" 8319 "b\\\n" 8320 "c\";", 8321 format("xxxx = \"a\\\n" 8322 "b\\\n" 8323 "c\";", 8324 Break)); 8325 8326 EXPECT_EQ("NSString *const kString =\n" 8327 " @\"aaaa\"\n" 8328 " @\"bbbb\";", 8329 format("NSString *const kString = @\"aaaa\"\n" 8330 "@\"bbbb\";", 8331 Break)); 8332 8333 Break.ColumnLimit = 0; 8334 verifyFormat("const char *hello = \"hello llvm\";", Break); 8335 } 8336 8337 TEST_F(FormatTest, AlignsPipes) { 8338 verifyFormat( 8339 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8340 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8341 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8342 verifyFormat( 8343 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 8344 " << aaaaaaaaaaaaaaaaaaaa;"); 8345 verifyFormat( 8346 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8347 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8348 verifyFormat( 8349 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 8350 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8351 verifyFormat( 8352 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 8353 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 8354 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 8355 verifyFormat( 8356 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8357 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8358 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8359 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8360 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8361 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8362 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 8363 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 8364 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 8365 verifyFormat( 8366 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8367 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8368 verifyFormat( 8369 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n" 8370 " aaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8371 8372 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 8373 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 8374 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8375 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8376 " aaaaaaaaaaaaaaaaaaaaa)\n" 8377 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8378 verifyFormat("LOG_IF(aaa == //\n" 8379 " bbb)\n" 8380 " << a << b;"); 8381 8382 // But sometimes, breaking before the first "<<" is desirable. 8383 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 8384 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 8385 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 8386 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8387 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8388 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 8389 " << BEF << IsTemplate << Description << E->getType();"); 8390 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 8391 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8392 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8393 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 8394 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8395 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8396 " << aaa;"); 8397 8398 verifyFormat( 8399 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8400 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8401 8402 // Incomplete string literal. 8403 EXPECT_EQ("llvm::errs() << \"\n" 8404 " << a;", 8405 format("llvm::errs() << \"\n<<a;")); 8406 8407 verifyFormat("void f() {\n" 8408 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 8409 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 8410 "}"); 8411 8412 // Handle 'endl'. 8413 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 8414 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 8415 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 8416 8417 // Handle '\n'. 8418 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 8419 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 8420 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 8421 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 8422 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 8423 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 8424 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 8425 } 8426 8427 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) { 8428 verifyFormat("return out << \"somepacket = {\\n\"\n" 8429 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 8430 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 8431 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 8432 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 8433 " << \"}\";"); 8434 8435 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 8436 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 8437 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 8438 verifyFormat( 8439 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 8440 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 8441 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 8442 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 8443 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 8444 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 8445 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 8446 verifyFormat( 8447 "void f() {\n" 8448 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 8449 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 8450 "}"); 8451 8452 // Breaking before the first "<<" is generally not desirable. 8453 verifyFormat( 8454 "llvm::errs()\n" 8455 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8456 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8457 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8458 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 8459 getLLVMStyleWithColumns(70)); 8460 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 8461 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8462 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 8463 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8464 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 8465 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 8466 getLLVMStyleWithColumns(70)); 8467 8468 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 8469 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 8470 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;"); 8471 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 8472 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 8473 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);"); 8474 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n" 8475 " (aaaa + aaaa);", 8476 getLLVMStyleWithColumns(40)); 8477 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n" 8478 " (aaaaaaa + aaaaa));", 8479 getLLVMStyleWithColumns(40)); 8480 verifyFormat( 8481 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n" 8482 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n" 8483 " bbbbbbbbbbbbbbbbbbbbbbb);"); 8484 } 8485 8486 TEST_F(FormatTest, UnderstandsEquals) { 8487 verifyFormat( 8488 "aaaaaaaaaaaaaaaaa =\n" 8489 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8490 verifyFormat( 8491 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8492 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 8493 verifyFormat( 8494 "if (a) {\n" 8495 " f();\n" 8496 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8497 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 8498 "}"); 8499 8500 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8501 " 100000000 + 10000000) {\n}"); 8502 } 8503 8504 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 8505 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 8506 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 8507 8508 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 8509 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 8510 8511 verifyFormat( 8512 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 8513 " Parameter2);"); 8514 8515 verifyFormat( 8516 "ShortObject->shortFunction(\n" 8517 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 8518 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 8519 8520 verifyFormat("loooooooooooooongFunction(\n" 8521 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 8522 8523 verifyFormat( 8524 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 8525 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 8526 8527 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 8528 " .WillRepeatedly(Return(SomeValue));"); 8529 verifyFormat("void f() {\n" 8530 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 8531 " .Times(2)\n" 8532 " .WillRepeatedly(Return(SomeValue));\n" 8533 "}"); 8534 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 8535 " ccccccccccccccccccccccc);"); 8536 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8537 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8538 " .aaaaa(aaaaa),\n" 8539 " aaaaaaaaaaaaaaaaaaaaa);"); 8540 verifyFormat("void f() {\n" 8541 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8542 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 8543 "}"); 8544 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8545 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8546 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8547 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8548 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 8549 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8550 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8551 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8552 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 8553 "}"); 8554 8555 // Here, it is not necessary to wrap at "." or "->". 8556 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 8557 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 8558 verifyFormat( 8559 "aaaaaaaaaaa->aaaaaaaaa(\n" 8560 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8561 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 8562 8563 verifyFormat( 8564 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8565 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 8566 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 8567 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 8568 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 8569 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 8570 8571 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8572 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8573 " .a();"); 8574 8575 FormatStyle NoBinPacking = getLLVMStyle(); 8576 NoBinPacking.BinPackParameters = false; 8577 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 8578 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 8579 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 8580 " aaaaaaaaaaaaaaaaaaa,\n" 8581 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8582 NoBinPacking); 8583 8584 // If there is a subsequent call, change to hanging indentation. 8585 verifyFormat( 8586 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8587 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 8588 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8589 verifyFormat( 8590 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8591 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 8592 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8593 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8594 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8595 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8596 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8597 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 8598 } 8599 8600 TEST_F(FormatTest, WrapsTemplateDeclarations) { 8601 verifyFormat("template <typename T>\n" 8602 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 8603 verifyFormat("template <typename T>\n" 8604 "// T should be one of {A, B}.\n" 8605 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 8606 verifyFormat( 8607 "template <typename T>\n" 8608 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 8609 verifyFormat("template <typename T>\n" 8610 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 8611 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 8612 verifyFormat( 8613 "template <typename T>\n" 8614 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 8615 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 8616 verifyFormat( 8617 "template <typename T>\n" 8618 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 8619 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 8620 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8621 verifyFormat("template <typename T>\n" 8622 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8623 " int aaaaaaaaaaaaaaaaaaaaaa);"); 8624 verifyFormat( 8625 "template <typename T1, typename T2 = char, typename T3 = char,\n" 8626 " typename T4 = char>\n" 8627 "void f();"); 8628 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 8629 " template <typename> class cccccccccccccccccccccc,\n" 8630 " typename ddddddddddddd>\n" 8631 "class C {};"); 8632 verifyFormat( 8633 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 8634 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8635 8636 verifyFormat("void f() {\n" 8637 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 8638 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 8639 "}"); 8640 8641 verifyFormat("template <typename T> class C {};"); 8642 verifyFormat("template <typename T> void f();"); 8643 verifyFormat("template <typename T> void f() {}"); 8644 verifyFormat( 8645 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 8646 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8647 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 8648 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 8649 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8650 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 8651 " bbbbbbbbbbbbbbbbbbbbbbbb);", 8652 getLLVMStyleWithColumns(72)); 8653 EXPECT_EQ("static_cast<A< //\n" 8654 " B> *>(\n" 8655 "\n" 8656 ");", 8657 format("static_cast<A<//\n" 8658 " B>*>(\n" 8659 "\n" 8660 " );")); 8661 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8662 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 8663 8664 FormatStyle AlwaysBreak = getLLVMStyle(); 8665 AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 8666 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 8667 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 8668 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 8669 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8670 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 8671 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 8672 verifyFormat("template <template <typename> class Fooooooo,\n" 8673 " template <typename> class Baaaaaaar>\n" 8674 "struct C {};", 8675 AlwaysBreak); 8676 verifyFormat("template <typename T> // T can be A, B or C.\n" 8677 "struct C {};", 8678 AlwaysBreak); 8679 verifyFormat("template <enum E> class A {\n" 8680 "public:\n" 8681 " E *f();\n" 8682 "};"); 8683 8684 FormatStyle NeverBreak = getLLVMStyle(); 8685 NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No; 8686 verifyFormat("template <typename T> class C {};", NeverBreak); 8687 verifyFormat("template <typename T> void f();", NeverBreak); 8688 verifyFormat("template <typename T> void f() {}", NeverBreak); 8689 verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa " 8690 "bbbbbbbbbbbbbbbbbbbb) {}", 8691 NeverBreak); 8692 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8693 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 8694 " ccccccccccccccccccccccccccccccccccccccccccccccc);", 8695 NeverBreak); 8696 verifyFormat("template <template <typename> class Fooooooo,\n" 8697 " template <typename> class Baaaaaaar>\n" 8698 "struct C {};", 8699 NeverBreak); 8700 verifyFormat("template <typename T> // T can be A, B or C.\n" 8701 "struct C {};", 8702 NeverBreak); 8703 verifyFormat("template <enum E> class A {\n" 8704 "public:\n" 8705 " E *f();\n" 8706 "};", 8707 NeverBreak); 8708 NeverBreak.PenaltyBreakTemplateDeclaration = 100; 8709 verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa " 8710 "bbbbbbbbbbbbbbbbbbbb) {}", 8711 NeverBreak); 8712 } 8713 8714 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) { 8715 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 8716 Style.ColumnLimit = 60; 8717 EXPECT_EQ("// Baseline - no comments.\n" 8718 "template <\n" 8719 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 8720 "void f() {}", 8721 format("// Baseline - no comments.\n" 8722 "template <\n" 8723 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 8724 "void f() {}", 8725 Style)); 8726 8727 EXPECT_EQ("template <\n" 8728 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 8729 "void f() {}", 8730 format("template <\n" 8731 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 8732 "void f() {}", 8733 Style)); 8734 8735 EXPECT_EQ( 8736 "template <\n" 8737 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 8738 "void f() {}", 8739 format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 8740 "void f() {}", 8741 Style)); 8742 8743 EXPECT_EQ( 8744 "template <\n" 8745 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 8746 " // multiline\n" 8747 "void f() {}", 8748 format("template <\n" 8749 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 8750 " // multiline\n" 8751 "void f() {}", 8752 Style)); 8753 8754 EXPECT_EQ( 8755 "template <typename aaaaaaaaaa<\n" 8756 " bbbbbbbbbbbb>::value> // trailing loooong\n" 8757 "void f() {}", 8758 format( 8759 "template <\n" 8760 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n" 8761 "void f() {}", 8762 Style)); 8763 } 8764 8765 TEST_F(FormatTest, WrapsTemplateParameters) { 8766 FormatStyle Style = getLLVMStyle(); 8767 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 8768 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 8769 verifyFormat( 8770 "template <typename... a> struct q {};\n" 8771 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 8772 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 8773 " y;", 8774 Style); 8775 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 8776 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 8777 verifyFormat( 8778 "template <typename... a> struct r {};\n" 8779 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 8780 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 8781 " y;", 8782 Style); 8783 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 8784 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 8785 verifyFormat("template <typename... a> struct s {};\n" 8786 "extern s<\n" 8787 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 8788 "aaaaaaaaaaaaaaaaaaaaaa,\n" 8789 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 8790 "aaaaaaaaaaaaaaaaaaaaaa>\n" 8791 " y;", 8792 Style); 8793 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 8794 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 8795 verifyFormat("template <typename... a> struct t {};\n" 8796 "extern t<\n" 8797 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 8798 "aaaaaaaaaaaaaaaaaaaaaa,\n" 8799 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 8800 "aaaaaaaaaaaaaaaaaaaaaa>\n" 8801 " y;", 8802 Style); 8803 } 8804 8805 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 8806 verifyFormat( 8807 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8808 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8809 verifyFormat( 8810 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8811 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8812 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 8813 8814 // FIXME: Should we have the extra indent after the second break? 8815 verifyFormat( 8816 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8817 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8818 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8819 8820 verifyFormat( 8821 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 8822 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 8823 8824 // Breaking at nested name specifiers is generally not desirable. 8825 verifyFormat( 8826 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8827 " aaaaaaaaaaaaaaaaaaaaaaa);"); 8828 8829 verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n" 8830 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8831 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8832 " aaaaaaaaaaaaaaaaaaaaa);", 8833 getLLVMStyleWithColumns(74)); 8834 8835 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8836 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8837 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8838 } 8839 8840 TEST_F(FormatTest, UnderstandsTemplateParameters) { 8841 verifyFormat("A<int> a;"); 8842 verifyFormat("A<A<A<int>>> a;"); 8843 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 8844 verifyFormat("bool x = a < 1 || 2 > a;"); 8845 verifyFormat("bool x = 5 < f<int>();"); 8846 verifyFormat("bool x = f<int>() > 5;"); 8847 verifyFormat("bool x = 5 < a<int>::x;"); 8848 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 8849 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 8850 8851 verifyGoogleFormat("A<A<int>> a;"); 8852 verifyGoogleFormat("A<A<A<int>>> a;"); 8853 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 8854 verifyGoogleFormat("A<A<int> > a;"); 8855 verifyGoogleFormat("A<A<A<int> > > a;"); 8856 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 8857 verifyGoogleFormat("A<::A<int>> a;"); 8858 verifyGoogleFormat("A<::A> a;"); 8859 verifyGoogleFormat("A< ::A> a;"); 8860 verifyGoogleFormat("A< ::A<int> > a;"); 8861 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 8862 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 8863 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 8864 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 8865 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 8866 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 8867 8868 verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 8869 8870 // template closer followed by a token that starts with > or = 8871 verifyFormat("bool b = a<1> > 1;"); 8872 verifyFormat("bool b = a<1> >= 1;"); 8873 verifyFormat("int i = a<1> >> 1;"); 8874 FormatStyle Style = getLLVMStyle(); 8875 Style.SpaceBeforeAssignmentOperators = false; 8876 verifyFormat("bool b= a<1> == 1;", Style); 8877 verifyFormat("a<int> = 1;", Style); 8878 verifyFormat("a<int> >>= 1;", Style); 8879 8880 verifyFormat("test < a | b >> c;"); 8881 verifyFormat("test<test<a | b>> c;"); 8882 verifyFormat("test >> a >> b;"); 8883 verifyFormat("test << a >> b;"); 8884 8885 verifyFormat("f<int>();"); 8886 verifyFormat("template <typename T> void f() {}"); 8887 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 8888 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 8889 "sizeof(char)>::type>;"); 8890 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 8891 verifyFormat("f(a.operator()<A>());"); 8892 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8893 " .template operator()<A>());", 8894 getLLVMStyleWithColumns(35)); 8895 8896 // Not template parameters. 8897 verifyFormat("return a < b && c > d;"); 8898 verifyFormat("void f() {\n" 8899 " while (a < b && c > d) {\n" 8900 " }\n" 8901 "}"); 8902 verifyFormat("template <typename... Types>\n" 8903 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 8904 8905 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8906 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 8907 getLLVMStyleWithColumns(60)); 8908 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 8909 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 8910 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 8911 verifyFormat("some_templated_type<decltype([](int i) { return i; })>"); 8912 } 8913 8914 TEST_F(FormatTest, UnderstandsShiftOperators) { 8915 verifyFormat("if (i < x >> 1)"); 8916 verifyFormat("while (i < x >> 1)"); 8917 verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)"); 8918 verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)"); 8919 verifyFormat( 8920 "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)"); 8921 verifyFormat("Foo.call<Bar<Function>>()"); 8922 verifyFormat("if (Foo.call<Bar<Function>>() == 0)"); 8923 verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; " 8924 "++i, v = v >> 1)"); 8925 verifyFormat("if (w<u<v<x>>, 1>::t)"); 8926 } 8927 8928 TEST_F(FormatTest, BitshiftOperatorWidth) { 8929 EXPECT_EQ("int a = 1 << 2; /* foo\n" 8930 " bar */", 8931 format("int a=1<<2; /* foo\n" 8932 " bar */")); 8933 8934 EXPECT_EQ("int b = 256 >> 1; /* foo\n" 8935 " bar */", 8936 format("int b =256>>1 ; /* foo\n" 8937 " bar */")); 8938 } 8939 8940 TEST_F(FormatTest, UnderstandsBinaryOperators) { 8941 verifyFormat("COMPARE(a, ==, b);"); 8942 verifyFormat("auto s = sizeof...(Ts) - 1;"); 8943 } 8944 8945 TEST_F(FormatTest, UnderstandsPointersToMembers) { 8946 verifyFormat("int A::*x;"); 8947 verifyFormat("int (S::*func)(void *);"); 8948 verifyFormat("void f() { int (S::*func)(void *); }"); 8949 verifyFormat("typedef bool *(Class::*Member)() const;"); 8950 verifyFormat("void f() {\n" 8951 " (a->*f)();\n" 8952 " a->*x;\n" 8953 " (a.*f)();\n" 8954 " ((*a).*f)();\n" 8955 " a.*x;\n" 8956 "}"); 8957 verifyFormat("void f() {\n" 8958 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 8959 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 8960 "}"); 8961 verifyFormat( 8962 "(aaaaaaaaaa->*bbbbbbb)(\n" 8963 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 8964 FormatStyle Style = getLLVMStyle(); 8965 Style.PointerAlignment = FormatStyle::PAS_Left; 8966 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 8967 } 8968 8969 TEST_F(FormatTest, UnderstandsUnaryOperators) { 8970 verifyFormat("int a = -2;"); 8971 verifyFormat("f(-1, -2, -3);"); 8972 verifyFormat("a[-1] = 5;"); 8973 verifyFormat("int a = 5 + -2;"); 8974 verifyFormat("if (i == -1) {\n}"); 8975 verifyFormat("if (i != -1) {\n}"); 8976 verifyFormat("if (i > -1) {\n}"); 8977 verifyFormat("if (i < -1) {\n}"); 8978 verifyFormat("++(a->f());"); 8979 verifyFormat("--(a->f());"); 8980 verifyFormat("(a->f())++;"); 8981 verifyFormat("a[42]++;"); 8982 verifyFormat("if (!(a->f())) {\n}"); 8983 verifyFormat("if (!+i) {\n}"); 8984 verifyFormat("~&a;"); 8985 8986 verifyFormat("a-- > b;"); 8987 verifyFormat("b ? -a : c;"); 8988 verifyFormat("n * sizeof char16;"); 8989 verifyFormat("n * alignof char16;", getGoogleStyle()); 8990 verifyFormat("sizeof(char);"); 8991 verifyFormat("alignof(char);", getGoogleStyle()); 8992 8993 verifyFormat("return -1;"); 8994 verifyFormat("throw -1;"); 8995 verifyFormat("switch (a) {\n" 8996 "case -1:\n" 8997 " break;\n" 8998 "}"); 8999 verifyFormat("#define X -1"); 9000 verifyFormat("#define X -kConstant"); 9001 9002 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 9003 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 9004 9005 verifyFormat("int a = /* confusing comment */ -1;"); 9006 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 9007 verifyFormat("int a = i /* confusing comment */++;"); 9008 9009 verifyFormat("co_yield -1;"); 9010 verifyFormat("co_return -1;"); 9011 9012 // Check that * is not treated as a binary operator when we set 9013 // PointerAlignment as PAS_Left after a keyword and not a declaration. 9014 FormatStyle PASLeftStyle = getLLVMStyle(); 9015 PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left; 9016 verifyFormat("co_return *a;", PASLeftStyle); 9017 verifyFormat("co_await *a;", PASLeftStyle); 9018 verifyFormat("co_yield *a", PASLeftStyle); 9019 verifyFormat("return *a;", PASLeftStyle); 9020 } 9021 9022 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 9023 verifyFormat("if (!aaaaaaaaaa( // break\n" 9024 " aaaaa)) {\n" 9025 "}"); 9026 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 9027 " aaaaa));"); 9028 verifyFormat("*aaa = aaaaaaa( // break\n" 9029 " bbbbbb);"); 9030 } 9031 9032 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 9033 verifyFormat("bool operator<();"); 9034 verifyFormat("bool operator>();"); 9035 verifyFormat("bool operator=();"); 9036 verifyFormat("bool operator==();"); 9037 verifyFormat("bool operator!=();"); 9038 verifyFormat("int operator+();"); 9039 verifyFormat("int operator++();"); 9040 verifyFormat("int operator++(int) volatile noexcept;"); 9041 verifyFormat("bool operator,();"); 9042 verifyFormat("bool operator();"); 9043 verifyFormat("bool operator()();"); 9044 verifyFormat("bool operator[]();"); 9045 verifyFormat("operator bool();"); 9046 verifyFormat("operator int();"); 9047 verifyFormat("operator void *();"); 9048 verifyFormat("operator SomeType<int>();"); 9049 verifyFormat("operator SomeType<int, int>();"); 9050 verifyFormat("operator SomeType<SomeType<int>>();"); 9051 verifyFormat("void *operator new(std::size_t size);"); 9052 verifyFormat("void *operator new[](std::size_t size);"); 9053 verifyFormat("void operator delete(void *ptr);"); 9054 verifyFormat("void operator delete[](void *ptr);"); 9055 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 9056 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 9057 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 9058 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 9059 9060 verifyFormat( 9061 "ostream &operator<<(ostream &OutputStream,\n" 9062 " SomeReallyLongType WithSomeReallyLongValue);"); 9063 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 9064 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 9065 " return left.group < right.group;\n" 9066 "}"); 9067 verifyFormat("SomeType &operator=(const SomeType &S);"); 9068 verifyFormat("f.template operator()<int>();"); 9069 9070 verifyGoogleFormat("operator void*();"); 9071 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 9072 verifyGoogleFormat("operator ::A();"); 9073 9074 verifyFormat("using A::operator+;"); 9075 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 9076 "int i;"); 9077 9078 // Calling an operator as a member function. 9079 verifyFormat("void f() { a.operator*(); }"); 9080 verifyFormat("void f() { a.operator*(b & b); }"); 9081 verifyFormat("void f() { a->operator&(a * b); }"); 9082 verifyFormat("void f() { NS::a.operator+(*b * *b); }"); 9083 // TODO: Calling an operator as a non-member function is hard to distinguish. 9084 // https://llvm.org/PR50629 9085 // verifyFormat("void f() { operator*(a & a); }"); 9086 // verifyFormat("void f() { operator&(a, b * b); }"); 9087 } 9088 9089 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 9090 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 9091 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 9092 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 9093 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 9094 verifyFormat("Deleted &operator=(const Deleted &) &;"); 9095 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 9096 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 9097 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 9098 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 9099 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 9100 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 9101 verifyFormat("void Fn(T const &) const &;"); 9102 verifyFormat("void Fn(T const volatile &&) const volatile &&;"); 9103 verifyFormat("template <typename T>\n" 9104 "void F(T) && = delete;", 9105 getGoogleStyle()); 9106 9107 FormatStyle AlignLeft = getLLVMStyle(); 9108 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 9109 verifyFormat("void A::b() && {}", AlignLeft); 9110 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 9111 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 9112 AlignLeft); 9113 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 9114 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 9115 verifyFormat("auto Function(T t) & -> void {}", AlignLeft); 9116 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft); 9117 verifyFormat("auto Function(T) & -> void {}", AlignLeft); 9118 verifyFormat("auto Function(T) & -> void;", AlignLeft); 9119 verifyFormat("void Fn(T const&) const&;", AlignLeft); 9120 verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft); 9121 9122 FormatStyle Spaces = getLLVMStyle(); 9123 Spaces.SpacesInCStyleCastParentheses = true; 9124 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 9125 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 9126 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 9127 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 9128 9129 Spaces.SpacesInCStyleCastParentheses = false; 9130 Spaces.SpacesInParentheses = true; 9131 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 9132 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", 9133 Spaces); 9134 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 9135 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 9136 9137 FormatStyle BreakTemplate = getLLVMStyle(); 9138 BreakTemplate.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 9139 9140 verifyFormat("struct f {\n" 9141 " template <class T>\n" 9142 " int &foo(const std::string &str) &noexcept {}\n" 9143 "};", 9144 BreakTemplate); 9145 9146 verifyFormat("struct f {\n" 9147 " template <class T>\n" 9148 " int &foo(const std::string &str) &&noexcept {}\n" 9149 "};", 9150 BreakTemplate); 9151 9152 verifyFormat("struct f {\n" 9153 " template <class T>\n" 9154 " int &foo(const std::string &str) const &noexcept {}\n" 9155 "};", 9156 BreakTemplate); 9157 9158 verifyFormat("struct f {\n" 9159 " template <class T>\n" 9160 " int &foo(const std::string &str) const &noexcept {}\n" 9161 "};", 9162 BreakTemplate); 9163 9164 verifyFormat("struct f {\n" 9165 " template <class T>\n" 9166 " auto foo(const std::string &str) &&noexcept -> int & {}\n" 9167 "};", 9168 BreakTemplate); 9169 9170 FormatStyle AlignLeftBreakTemplate = getLLVMStyle(); 9171 AlignLeftBreakTemplate.AlwaysBreakTemplateDeclarations = 9172 FormatStyle::BTDS_Yes; 9173 AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left; 9174 9175 verifyFormat("struct f {\n" 9176 " template <class T>\n" 9177 " int& foo(const std::string& str) & noexcept {}\n" 9178 "};", 9179 AlignLeftBreakTemplate); 9180 9181 verifyFormat("struct f {\n" 9182 " template <class T>\n" 9183 " int& foo(const std::string& str) && noexcept {}\n" 9184 "};", 9185 AlignLeftBreakTemplate); 9186 9187 verifyFormat("struct f {\n" 9188 " template <class T>\n" 9189 " int& foo(const std::string& str) const& noexcept {}\n" 9190 "};", 9191 AlignLeftBreakTemplate); 9192 9193 verifyFormat("struct f {\n" 9194 " template <class T>\n" 9195 " int& foo(const std::string& str) const&& noexcept {}\n" 9196 "};", 9197 AlignLeftBreakTemplate); 9198 9199 verifyFormat("struct f {\n" 9200 " template <class T>\n" 9201 " auto foo(const std::string& str) && noexcept -> int& {}\n" 9202 "};", 9203 AlignLeftBreakTemplate); 9204 9205 // The `&` in `Type&` should not be confused with a trailing `&` of 9206 // DEPRECATED(reason) member function. 9207 verifyFormat("struct f {\n" 9208 " template <class T>\n" 9209 " DEPRECATED(reason)\n" 9210 " Type &foo(arguments) {}\n" 9211 "};", 9212 BreakTemplate); 9213 9214 verifyFormat("struct f {\n" 9215 " template <class T>\n" 9216 " DEPRECATED(reason)\n" 9217 " Type& foo(arguments) {}\n" 9218 "};", 9219 AlignLeftBreakTemplate); 9220 9221 verifyFormat("void (*foopt)(int) = &func;"); 9222 } 9223 9224 TEST_F(FormatTest, UnderstandsNewAndDelete) { 9225 verifyFormat("void f() {\n" 9226 " A *a = new A;\n" 9227 " A *a = new (placement) A;\n" 9228 " delete a;\n" 9229 " delete (A *)a;\n" 9230 "}"); 9231 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 9232 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 9233 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 9234 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 9235 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 9236 verifyFormat("delete[] h->p;"); 9237 } 9238 9239 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 9240 verifyFormat("int *f(int *a) {}"); 9241 verifyFormat("int main(int argc, char **argv) {}"); 9242 verifyFormat("Test::Test(int b) : a(b * b) {}"); 9243 verifyIndependentOfContext("f(a, *a);"); 9244 verifyFormat("void g() { f(*a); }"); 9245 verifyIndependentOfContext("int a = b * 10;"); 9246 verifyIndependentOfContext("int a = 10 * b;"); 9247 verifyIndependentOfContext("int a = b * c;"); 9248 verifyIndependentOfContext("int a += b * c;"); 9249 verifyIndependentOfContext("int a -= b * c;"); 9250 verifyIndependentOfContext("int a *= b * c;"); 9251 verifyIndependentOfContext("int a /= b * c;"); 9252 verifyIndependentOfContext("int a = *b;"); 9253 verifyIndependentOfContext("int a = *b * c;"); 9254 verifyIndependentOfContext("int a = b * *c;"); 9255 verifyIndependentOfContext("int a = b * (10);"); 9256 verifyIndependentOfContext("S << b * (10);"); 9257 verifyIndependentOfContext("return 10 * b;"); 9258 verifyIndependentOfContext("return *b * *c;"); 9259 verifyIndependentOfContext("return a & ~b;"); 9260 verifyIndependentOfContext("f(b ? *c : *d);"); 9261 verifyIndependentOfContext("int a = b ? *c : *d;"); 9262 verifyIndependentOfContext("*b = a;"); 9263 verifyIndependentOfContext("a * ~b;"); 9264 verifyIndependentOfContext("a * !b;"); 9265 verifyIndependentOfContext("a * +b;"); 9266 verifyIndependentOfContext("a * -b;"); 9267 verifyIndependentOfContext("a * ++b;"); 9268 verifyIndependentOfContext("a * --b;"); 9269 verifyIndependentOfContext("a[4] * b;"); 9270 verifyIndependentOfContext("a[a * a] = 1;"); 9271 verifyIndependentOfContext("f() * b;"); 9272 verifyIndependentOfContext("a * [self dostuff];"); 9273 verifyIndependentOfContext("int x = a * (a + b);"); 9274 verifyIndependentOfContext("(a *)(a + b);"); 9275 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 9276 verifyIndependentOfContext("int *pa = (int *)&a;"); 9277 verifyIndependentOfContext("return sizeof(int **);"); 9278 verifyIndependentOfContext("return sizeof(int ******);"); 9279 verifyIndependentOfContext("return (int **&)a;"); 9280 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 9281 verifyFormat("void f(Type (*parameter)[10]) {}"); 9282 verifyFormat("void f(Type (¶meter)[10]) {}"); 9283 verifyGoogleFormat("return sizeof(int**);"); 9284 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 9285 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 9286 verifyFormat("auto a = [](int **&, int ***) {};"); 9287 verifyFormat("auto PointerBinding = [](const char *S) {};"); 9288 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 9289 verifyFormat("[](const decltype(*a) &value) {}"); 9290 verifyFormat("[](const typeof(*a) &value) {}"); 9291 verifyFormat("[](const _Atomic(a *) &value) {}"); 9292 verifyFormat("[](const __underlying_type(a) &value) {}"); 9293 verifyFormat("decltype(a * b) F();"); 9294 verifyFormat("typeof(a * b) F();"); 9295 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 9296 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 9297 verifyIndependentOfContext("typedef void (*f)(int *a);"); 9298 verifyIndependentOfContext("int i{a * b};"); 9299 verifyIndependentOfContext("aaa && aaa->f();"); 9300 verifyIndependentOfContext("int x = ~*p;"); 9301 verifyFormat("Constructor() : a(a), area(width * height) {}"); 9302 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 9303 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 9304 verifyFormat("void f() { f(a, c * d); }"); 9305 verifyFormat("void f() { f(new a(), c * d); }"); 9306 verifyFormat("void f(const MyOverride &override);"); 9307 verifyFormat("void f(const MyFinal &final);"); 9308 verifyIndependentOfContext("bool a = f() && override.f();"); 9309 verifyIndependentOfContext("bool a = f() && final.f();"); 9310 9311 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 9312 9313 verifyIndependentOfContext("A<int *> a;"); 9314 verifyIndependentOfContext("A<int **> a;"); 9315 verifyIndependentOfContext("A<int *, int *> a;"); 9316 verifyIndependentOfContext("A<int *[]> a;"); 9317 verifyIndependentOfContext( 9318 "const char *const p = reinterpret_cast<const char *const>(q);"); 9319 verifyIndependentOfContext("A<int **, int **> a;"); 9320 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 9321 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 9322 verifyFormat("for (; a && b;) {\n}"); 9323 verifyFormat("bool foo = true && [] { return false; }();"); 9324 9325 verifyFormat( 9326 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9327 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9328 9329 verifyGoogleFormat("int const* a = &b;"); 9330 verifyGoogleFormat("**outparam = 1;"); 9331 verifyGoogleFormat("*outparam = a * b;"); 9332 verifyGoogleFormat("int main(int argc, char** argv) {}"); 9333 verifyGoogleFormat("A<int*> a;"); 9334 verifyGoogleFormat("A<int**> a;"); 9335 verifyGoogleFormat("A<int*, int*> a;"); 9336 verifyGoogleFormat("A<int**, int**> a;"); 9337 verifyGoogleFormat("f(b ? *c : *d);"); 9338 verifyGoogleFormat("int a = b ? *c : *d;"); 9339 verifyGoogleFormat("Type* t = **x;"); 9340 verifyGoogleFormat("Type* t = *++*x;"); 9341 verifyGoogleFormat("*++*x;"); 9342 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 9343 verifyGoogleFormat("Type* t = x++ * y;"); 9344 verifyGoogleFormat( 9345 "const char* const p = reinterpret_cast<const char* const>(q);"); 9346 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 9347 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 9348 verifyGoogleFormat("template <typename T>\n" 9349 "void f(int i = 0, SomeType** temps = NULL);"); 9350 9351 FormatStyle Left = getLLVMStyle(); 9352 Left.PointerAlignment = FormatStyle::PAS_Left; 9353 verifyFormat("x = *a(x) = *a(y);", Left); 9354 verifyFormat("for (;; *a = b) {\n}", Left); 9355 verifyFormat("return *this += 1;", Left); 9356 verifyFormat("throw *x;", Left); 9357 verifyFormat("delete *x;", Left); 9358 verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left); 9359 verifyFormat("[](const decltype(*a)* ptr) {}", Left); 9360 verifyFormat("[](const typeof(*a)* ptr) {}", Left); 9361 verifyFormat("[](const _Atomic(a*)* ptr) {}", Left); 9362 verifyFormat("[](const __underlying_type(a)* ptr) {}", Left); 9363 verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left); 9364 verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left); 9365 verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left); 9366 verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left); 9367 9368 verifyIndependentOfContext("a = *(x + y);"); 9369 verifyIndependentOfContext("a = &(x + y);"); 9370 verifyIndependentOfContext("*(x + y).call();"); 9371 verifyIndependentOfContext("&(x + y)->call();"); 9372 verifyFormat("void f() { &(*I).first; }"); 9373 9374 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 9375 verifyFormat( 9376 "int *MyValues = {\n" 9377 " *A, // Operator detection might be confused by the '{'\n" 9378 " *BB // Operator detection might be confused by previous comment\n" 9379 "};"); 9380 9381 verifyIndependentOfContext("if (int *a = &b)"); 9382 verifyIndependentOfContext("if (int &a = *b)"); 9383 verifyIndependentOfContext("if (a & b[i])"); 9384 verifyIndependentOfContext("if constexpr (a & b[i])"); 9385 verifyIndependentOfContext("if CONSTEXPR (a & b[i])"); 9386 verifyIndependentOfContext("if (a * (b * c))"); 9387 verifyIndependentOfContext("if constexpr (a * (b * c))"); 9388 verifyIndependentOfContext("if CONSTEXPR (a * (b * c))"); 9389 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 9390 verifyIndependentOfContext("if (*b[i])"); 9391 verifyIndependentOfContext("if (int *a = (&b))"); 9392 verifyIndependentOfContext("while (int *a = &b)"); 9393 verifyIndependentOfContext("while (a * (b * c))"); 9394 verifyIndependentOfContext("size = sizeof *a;"); 9395 verifyIndependentOfContext("if (a && (b = c))"); 9396 verifyFormat("void f() {\n" 9397 " for (const int &v : Values) {\n" 9398 " }\n" 9399 "}"); 9400 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 9401 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 9402 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 9403 9404 verifyFormat("#define A (!a * b)"); 9405 verifyFormat("#define MACRO \\\n" 9406 " int *i = a * b; \\\n" 9407 " void f(a *b);", 9408 getLLVMStyleWithColumns(19)); 9409 9410 verifyIndependentOfContext("A = new SomeType *[Length];"); 9411 verifyIndependentOfContext("A = new SomeType *[Length]();"); 9412 verifyIndependentOfContext("T **t = new T *;"); 9413 verifyIndependentOfContext("T **t = new T *();"); 9414 verifyGoogleFormat("A = new SomeType*[Length]();"); 9415 verifyGoogleFormat("A = new SomeType*[Length];"); 9416 verifyGoogleFormat("T** t = new T*;"); 9417 verifyGoogleFormat("T** t = new T*();"); 9418 9419 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 9420 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 9421 verifyFormat("template <bool a, bool b> " 9422 "typename t::if<x && y>::type f() {}"); 9423 verifyFormat("template <int *y> f() {}"); 9424 verifyFormat("vector<int *> v;"); 9425 verifyFormat("vector<int *const> v;"); 9426 verifyFormat("vector<int *const **const *> v;"); 9427 verifyFormat("vector<int *volatile> v;"); 9428 verifyFormat("vector<a *_Nonnull> v;"); 9429 verifyFormat("vector<a *_Nullable> v;"); 9430 verifyFormat("vector<a *_Null_unspecified> v;"); 9431 verifyFormat("vector<a *__ptr32> v;"); 9432 verifyFormat("vector<a *__ptr64> v;"); 9433 verifyFormat("vector<a *__capability> v;"); 9434 FormatStyle TypeMacros = getLLVMStyle(); 9435 TypeMacros.TypenameMacros = {"LIST"}; 9436 verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros); 9437 verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros); 9438 verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros); 9439 verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros); 9440 verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros); // multiplication 9441 9442 FormatStyle CustomQualifier = getLLVMStyle(); 9443 // Add identifiers that should not be parsed as a qualifier by default. 9444 CustomQualifier.AttributeMacros.push_back("__my_qualifier"); 9445 CustomQualifier.AttributeMacros.push_back("_My_qualifier"); 9446 CustomQualifier.AttributeMacros.push_back("my_other_qualifier"); 9447 verifyFormat("vector<a * __my_qualifier> parse_as_multiply;"); 9448 verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier); 9449 verifyFormat("vector<a * _My_qualifier> parse_as_multiply;"); 9450 verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier); 9451 verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;"); 9452 verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier); 9453 verifyFormat("vector<a * _NotAQualifier> v;"); 9454 verifyFormat("vector<a * __not_a_qualifier> v;"); 9455 verifyFormat("vector<a * b> v;"); 9456 verifyFormat("foo<b && false>();"); 9457 verifyFormat("foo<b & 1>();"); 9458 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 9459 verifyFormat("typeof(*::std::declval<const T &>()) void F();"); 9460 verifyFormat("_Atomic(*::std::declval<const T &>()) void F();"); 9461 verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();"); 9462 verifyFormat( 9463 "template <class T, class = typename std::enable_if<\n" 9464 " std::is_integral<T>::value &&\n" 9465 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 9466 "void F();", 9467 getLLVMStyleWithColumns(70)); 9468 verifyFormat("template <class T,\n" 9469 " class = typename std::enable_if<\n" 9470 " std::is_integral<T>::value &&\n" 9471 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n" 9472 " class U>\n" 9473 "void F();", 9474 getLLVMStyleWithColumns(70)); 9475 verifyFormat( 9476 "template <class T,\n" 9477 " class = typename ::std::enable_if<\n" 9478 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 9479 "void F();", 9480 getGoogleStyleWithColumns(68)); 9481 9482 verifyIndependentOfContext("MACRO(int *i);"); 9483 verifyIndependentOfContext("MACRO(auto *a);"); 9484 verifyIndependentOfContext("MACRO(const A *a);"); 9485 verifyIndependentOfContext("MACRO(_Atomic(A) *a);"); 9486 verifyIndependentOfContext("MACRO(decltype(A) *a);"); 9487 verifyIndependentOfContext("MACRO(typeof(A) *a);"); 9488 verifyIndependentOfContext("MACRO(__underlying_type(A) *a);"); 9489 verifyIndependentOfContext("MACRO(A *const a);"); 9490 verifyIndependentOfContext("MACRO(A *restrict a);"); 9491 verifyIndependentOfContext("MACRO(A *__restrict__ a);"); 9492 verifyIndependentOfContext("MACRO(A *__restrict a);"); 9493 verifyIndependentOfContext("MACRO(A *volatile a);"); 9494 verifyIndependentOfContext("MACRO(A *__volatile a);"); 9495 verifyIndependentOfContext("MACRO(A *__volatile__ a);"); 9496 verifyIndependentOfContext("MACRO(A *_Nonnull a);"); 9497 verifyIndependentOfContext("MACRO(A *_Nullable a);"); 9498 verifyIndependentOfContext("MACRO(A *_Null_unspecified a);"); 9499 verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);"); 9500 verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);"); 9501 verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);"); 9502 verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);"); 9503 verifyIndependentOfContext("MACRO(A *__ptr32 a);"); 9504 verifyIndependentOfContext("MACRO(A *__ptr64 a);"); 9505 verifyIndependentOfContext("MACRO(A *__capability);"); 9506 verifyIndependentOfContext("MACRO(A &__capability);"); 9507 verifyFormat("MACRO(A *__my_qualifier);"); // type declaration 9508 verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication 9509 // If we add __my_qualifier to AttributeMacros it should always be parsed as 9510 // a type declaration: 9511 verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier); 9512 verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier); 9513 // Also check that TypenameMacros prevents parsing it as multiplication: 9514 verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication 9515 verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros); // type 9516 9517 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 9518 verifyFormat("void f() { f(float{1}, a * a); }"); 9519 verifyFormat("void f() { f(float(1), a * a); }"); 9520 9521 verifyFormat("f((void (*)(int))g);"); 9522 verifyFormat("f((void (&)(int))g);"); 9523 verifyFormat("f((void (^)(int))g);"); 9524 9525 // FIXME: Is there a way to make this work? 9526 // verifyIndependentOfContext("MACRO(A *a);"); 9527 verifyFormat("MACRO(A &B);"); 9528 verifyFormat("MACRO(A *B);"); 9529 verifyFormat("void f() { MACRO(A * B); }"); 9530 verifyFormat("void f() { MACRO(A & B); }"); 9531 9532 // This lambda was mis-formatted after D88956 (treating it as a binop): 9533 verifyFormat("auto x = [](const decltype(x) &ptr) {};"); 9534 verifyFormat("auto x = [](const decltype(x) *ptr) {};"); 9535 verifyFormat("#define lambda [](const decltype(x) &ptr) {}"); 9536 verifyFormat("#define lambda [](const decltype(x) *ptr) {}"); 9537 9538 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 9539 verifyFormat("return options != nullptr && operator==(*options);"); 9540 9541 EXPECT_EQ("#define OP(x) \\\n" 9542 " ostream &operator<<(ostream &s, const A &a) { \\\n" 9543 " return s << a.DebugString(); \\\n" 9544 " }", 9545 format("#define OP(x) \\\n" 9546 " ostream &operator<<(ostream &s, const A &a) { \\\n" 9547 " return s << a.DebugString(); \\\n" 9548 " }", 9549 getLLVMStyleWithColumns(50))); 9550 9551 // FIXME: We cannot handle this case yet; we might be able to figure out that 9552 // foo<x> d > v; doesn't make sense. 9553 verifyFormat("foo<a<b && c> d> v;"); 9554 9555 FormatStyle PointerMiddle = getLLVMStyle(); 9556 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 9557 verifyFormat("delete *x;", PointerMiddle); 9558 verifyFormat("int * x;", PointerMiddle); 9559 verifyFormat("int *[] x;", PointerMiddle); 9560 verifyFormat("template <int * y> f() {}", PointerMiddle); 9561 verifyFormat("int * f(int * a) {}", PointerMiddle); 9562 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 9563 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 9564 verifyFormat("A<int *> a;", PointerMiddle); 9565 verifyFormat("A<int **> a;", PointerMiddle); 9566 verifyFormat("A<int *, int *> a;", PointerMiddle); 9567 verifyFormat("A<int *[]> a;", PointerMiddle); 9568 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 9569 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 9570 verifyFormat("T ** t = new T *;", PointerMiddle); 9571 9572 // Member function reference qualifiers aren't binary operators. 9573 verifyFormat("string // break\n" 9574 "operator()() & {}"); 9575 verifyFormat("string // break\n" 9576 "operator()() && {}"); 9577 verifyGoogleFormat("template <typename T>\n" 9578 "auto x() & -> int {}"); 9579 9580 // Should be binary operators when used as an argument expression (overloaded 9581 // operator invoked as a member function). 9582 verifyFormat("void f() { a.operator()(a * a); }"); 9583 verifyFormat("void f() { a->operator()(a & a); }"); 9584 verifyFormat("void f() { a.operator()(*a & *a); }"); 9585 verifyFormat("void f() { a->operator()(*a * *a); }"); 9586 } 9587 9588 TEST_F(FormatTest, UnderstandsAttributes) { 9589 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 9590 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 9591 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 9592 FormatStyle AfterType = getLLVMStyle(); 9593 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 9594 verifyFormat("__attribute__((nodebug)) void\n" 9595 "foo() {}\n", 9596 AfterType); 9597 verifyFormat("__unused void\n" 9598 "foo() {}", 9599 AfterType); 9600 9601 FormatStyle CustomAttrs = getLLVMStyle(); 9602 CustomAttrs.AttributeMacros.push_back("__unused"); 9603 CustomAttrs.AttributeMacros.push_back("__attr1"); 9604 CustomAttrs.AttributeMacros.push_back("__attr2"); 9605 CustomAttrs.AttributeMacros.push_back("no_underscore_attr"); 9606 verifyFormat("vector<SomeType *__attribute((foo))> v;"); 9607 verifyFormat("vector<SomeType *__attribute__((foo))> v;"); 9608 verifyFormat("vector<SomeType * __not_attribute__((foo))> v;"); 9609 // Check that it is parsed as a multiplication without AttributeMacros and 9610 // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros. 9611 verifyFormat("vector<SomeType * __attr1> v;"); 9612 verifyFormat("vector<SomeType __attr1 *> v;"); 9613 verifyFormat("vector<SomeType __attr1 *const> v;"); 9614 verifyFormat("vector<SomeType __attr1 * __attr2> v;"); 9615 verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs); 9616 verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs); 9617 verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs); 9618 verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs); 9619 verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs); 9620 verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs); 9621 verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs); 9622 9623 // Check that these are not parsed as function declarations: 9624 CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 9625 CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman; 9626 verifyFormat("SomeType s(InitValue);", CustomAttrs); 9627 verifyFormat("SomeType s{InitValue};", CustomAttrs); 9628 verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs); 9629 verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs); 9630 verifyFormat("SomeType s __unused(InitValue);", CustomAttrs); 9631 verifyFormat("SomeType s __unused{InitValue};", CustomAttrs); 9632 verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs); 9633 verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs); 9634 } 9635 9636 TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) { 9637 // Check that qualifiers on pointers don't break parsing of casts. 9638 verifyFormat("x = (foo *const)*v;"); 9639 verifyFormat("x = (foo *volatile)*v;"); 9640 verifyFormat("x = (foo *restrict)*v;"); 9641 verifyFormat("x = (foo *__attribute__((foo)))*v;"); 9642 verifyFormat("x = (foo *_Nonnull)*v;"); 9643 verifyFormat("x = (foo *_Nullable)*v;"); 9644 verifyFormat("x = (foo *_Null_unspecified)*v;"); 9645 verifyFormat("x = (foo *_Nonnull)*v;"); 9646 verifyFormat("x = (foo *[[clang::attr]])*v;"); 9647 verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;"); 9648 verifyFormat("x = (foo *__ptr32)*v;"); 9649 verifyFormat("x = (foo *__ptr64)*v;"); 9650 verifyFormat("x = (foo *__capability)*v;"); 9651 9652 // Check that we handle multiple trailing qualifiers and skip them all to 9653 // determine that the expression is a cast to a pointer type. 9654 FormatStyle LongPointerRight = getLLVMStyleWithColumns(999); 9655 FormatStyle LongPointerLeft = getLLVMStyleWithColumns(999); 9656 LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left; 9657 StringRef AllQualifiers = 9658 "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified " 9659 "_Nonnull [[clang::attr]] __ptr32 __ptr64 __capability"; 9660 verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight); 9661 verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft); 9662 9663 // Also check that address-of is not parsed as a binary bitwise-and: 9664 verifyFormat("x = (foo *const)&v;"); 9665 verifyFormat(("x = (foo *" + AllQualifiers + ")&v;").str(), LongPointerRight); 9666 verifyFormat(("x = (foo* " + AllQualifiers + ")&v;").str(), LongPointerLeft); 9667 9668 // Check custom qualifiers: 9669 FormatStyle CustomQualifier = getLLVMStyleWithColumns(999); 9670 CustomQualifier.AttributeMacros.push_back("__my_qualifier"); 9671 verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier. 9672 verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier); 9673 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)*v;").str(), 9674 CustomQualifier); 9675 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)&v;").str(), 9676 CustomQualifier); 9677 9678 // Check that unknown identifiers result in binary operator parsing: 9679 verifyFormat("x = (foo * __unknown_qualifier) * v;"); 9680 verifyFormat("x = (foo * __unknown_qualifier) & v;"); 9681 } 9682 9683 TEST_F(FormatTest, UnderstandsSquareAttributes) { 9684 verifyFormat("SomeType s [[unused]] (InitValue);"); 9685 verifyFormat("SomeType s [[gnu::unused]] (InitValue);"); 9686 verifyFormat("SomeType s [[using gnu: unused]] (InitValue);"); 9687 verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}"); 9688 verifyFormat("void f() [[deprecated(\"so sorry\")]];"); 9689 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9690 " [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 9691 verifyFormat("[[nodiscard]] bool f() { return false; }"); 9692 verifyFormat("class [[nodiscard]] f {\npublic:\n f() {}\n}"); 9693 verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n f() {}\n}"); 9694 verifyFormat("class [[gnu::unused]] f {\npublic:\n f() {}\n}"); 9695 9696 // Make sure we do not mistake attributes for array subscripts. 9697 verifyFormat("int a() {}\n" 9698 "[[unused]] int b() {}\n"); 9699 verifyFormat("NSArray *arr;\n" 9700 "arr[[Foo() bar]];"); 9701 9702 // On the other hand, we still need to correctly find array subscripts. 9703 verifyFormat("int a = std::vector<int>{1, 2, 3}[0];"); 9704 9705 // Make sure that we do not mistake Objective-C method inside array literals 9706 // as attributes, even if those method names are also keywords. 9707 verifyFormat("@[ [foo bar] ];"); 9708 verifyFormat("@[ [NSArray class] ];"); 9709 verifyFormat("@[ [foo enum] ];"); 9710 9711 verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }"); 9712 9713 // Make sure we do not parse attributes as lambda introducers. 9714 FormatStyle MultiLineFunctions = getLLVMStyle(); 9715 MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 9716 verifyFormat("[[unused]] int b() {\n" 9717 " return 42;\n" 9718 "}\n", 9719 MultiLineFunctions); 9720 } 9721 9722 TEST_F(FormatTest, AttributeClass) { 9723 FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp); 9724 verifyFormat("class S {\n" 9725 " S(S&&) = default;\n" 9726 "};", 9727 Style); 9728 verifyFormat("class [[nodiscard]] S {\n" 9729 " S(S&&) = default;\n" 9730 "};", 9731 Style); 9732 verifyFormat("class __attribute((maybeunused)) S {\n" 9733 " S(S&&) = default;\n" 9734 "};", 9735 Style); 9736 verifyFormat("struct S {\n" 9737 " S(S&&) = default;\n" 9738 "};", 9739 Style); 9740 verifyFormat("struct [[nodiscard]] S {\n" 9741 " S(S&&) = default;\n" 9742 "};", 9743 Style); 9744 } 9745 9746 TEST_F(FormatTest, AttributesAfterMacro) { 9747 FormatStyle Style = getLLVMStyle(); 9748 verifyFormat("MACRO;\n" 9749 "__attribute__((maybe_unused)) int foo() {\n" 9750 " //...\n" 9751 "}"); 9752 9753 verifyFormat("MACRO;\n" 9754 "[[nodiscard]] int foo() {\n" 9755 " //...\n" 9756 "}"); 9757 9758 EXPECT_EQ("MACRO\n\n" 9759 "__attribute__((maybe_unused)) int foo() {\n" 9760 " //...\n" 9761 "}", 9762 format("MACRO\n\n" 9763 "__attribute__((maybe_unused)) int foo() {\n" 9764 " //...\n" 9765 "}")); 9766 9767 EXPECT_EQ("MACRO\n\n" 9768 "[[nodiscard]] int foo() {\n" 9769 " //...\n" 9770 "}", 9771 format("MACRO\n\n" 9772 "[[nodiscard]] int foo() {\n" 9773 " //...\n" 9774 "}")); 9775 } 9776 9777 TEST_F(FormatTest, AttributePenaltyBreaking) { 9778 FormatStyle Style = getLLVMStyle(); 9779 verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n" 9780 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", 9781 Style); 9782 verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n" 9783 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", 9784 Style); 9785 verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const " 9786 "shared_ptr<ALongTypeName> &C d) {\n}", 9787 Style); 9788 } 9789 9790 TEST_F(FormatTest, UnderstandsEllipsis) { 9791 FormatStyle Style = getLLVMStyle(); 9792 verifyFormat("int printf(const char *fmt, ...);"); 9793 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 9794 verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}"); 9795 9796 verifyFormat("template <int *...PP> a;", Style); 9797 9798 Style.PointerAlignment = FormatStyle::PAS_Left; 9799 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style); 9800 9801 verifyFormat("template <int*... PP> a;", Style); 9802 9803 Style.PointerAlignment = FormatStyle::PAS_Middle; 9804 verifyFormat("template <int *... PP> a;", Style); 9805 } 9806 9807 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 9808 EXPECT_EQ("int *a;\n" 9809 "int *a;\n" 9810 "int *a;", 9811 format("int *a;\n" 9812 "int* a;\n" 9813 "int *a;", 9814 getGoogleStyle())); 9815 EXPECT_EQ("int* a;\n" 9816 "int* a;\n" 9817 "int* a;", 9818 format("int* a;\n" 9819 "int* a;\n" 9820 "int *a;", 9821 getGoogleStyle())); 9822 EXPECT_EQ("int *a;\n" 9823 "int *a;\n" 9824 "int *a;", 9825 format("int *a;\n" 9826 "int * a;\n" 9827 "int * a;", 9828 getGoogleStyle())); 9829 EXPECT_EQ("auto x = [] {\n" 9830 " int *a;\n" 9831 " int *a;\n" 9832 " int *a;\n" 9833 "};", 9834 format("auto x=[]{int *a;\n" 9835 "int * a;\n" 9836 "int * a;};", 9837 getGoogleStyle())); 9838 } 9839 9840 TEST_F(FormatTest, UnderstandsRvalueReferences) { 9841 verifyFormat("int f(int &&a) {}"); 9842 verifyFormat("int f(int a, char &&b) {}"); 9843 verifyFormat("void f() { int &&a = b; }"); 9844 verifyGoogleFormat("int f(int a, char&& b) {}"); 9845 verifyGoogleFormat("void f() { int&& a = b; }"); 9846 9847 verifyIndependentOfContext("A<int &&> a;"); 9848 verifyIndependentOfContext("A<int &&, int &&> a;"); 9849 verifyGoogleFormat("A<int&&> a;"); 9850 verifyGoogleFormat("A<int&&, int&&> a;"); 9851 9852 // Not rvalue references: 9853 verifyFormat("template <bool B, bool C> class A {\n" 9854 " static_assert(B && C, \"Something is wrong\");\n" 9855 "};"); 9856 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 9857 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 9858 verifyFormat("#define A(a, b) (a && b)"); 9859 } 9860 9861 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 9862 verifyFormat("void f() {\n" 9863 " x[aaaaaaaaa -\n" 9864 " b] = 23;\n" 9865 "}", 9866 getLLVMStyleWithColumns(15)); 9867 } 9868 9869 TEST_F(FormatTest, FormatsCasts) { 9870 verifyFormat("Type *A = static_cast<Type *>(P);"); 9871 verifyFormat("Type *A = (Type *)P;"); 9872 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 9873 verifyFormat("int a = (int)(2.0f);"); 9874 verifyFormat("int a = (int)2.0f;"); 9875 verifyFormat("x[(int32)y];"); 9876 verifyFormat("x = (int32)y;"); 9877 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 9878 verifyFormat("int a = (int)*b;"); 9879 verifyFormat("int a = (int)2.0f;"); 9880 verifyFormat("int a = (int)~0;"); 9881 verifyFormat("int a = (int)++a;"); 9882 verifyFormat("int a = (int)sizeof(int);"); 9883 verifyFormat("int a = (int)+2;"); 9884 verifyFormat("my_int a = (my_int)2.0f;"); 9885 verifyFormat("my_int a = (my_int)sizeof(int);"); 9886 verifyFormat("return (my_int)aaa;"); 9887 verifyFormat("#define x ((int)-1)"); 9888 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 9889 verifyFormat("#define p(q) ((int *)&q)"); 9890 verifyFormat("fn(a)(b) + 1;"); 9891 9892 verifyFormat("void f() { my_int a = (my_int)*b; }"); 9893 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 9894 verifyFormat("my_int a = (my_int)~0;"); 9895 verifyFormat("my_int a = (my_int)++a;"); 9896 verifyFormat("my_int a = (my_int)-2;"); 9897 verifyFormat("my_int a = (my_int)1;"); 9898 verifyFormat("my_int a = (my_int *)1;"); 9899 verifyFormat("my_int a = (const my_int)-1;"); 9900 verifyFormat("my_int a = (const my_int *)-1;"); 9901 verifyFormat("my_int a = (my_int)(my_int)-1;"); 9902 verifyFormat("my_int a = (ns::my_int)-2;"); 9903 verifyFormat("case (my_int)ONE:"); 9904 verifyFormat("auto x = (X)this;"); 9905 // Casts in Obj-C style calls used to not be recognized as such. 9906 verifyFormat("int a = [(type*)[((type*)val) arg] arg];", getGoogleStyle()); 9907 9908 // FIXME: single value wrapped with paren will be treated as cast. 9909 verifyFormat("void f(int i = (kValue)*kMask) {}"); 9910 9911 verifyFormat("{ (void)F; }"); 9912 9913 // Don't break after a cast's 9914 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 9915 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 9916 " bbbbbbbbbbbbbbbbbbbbbb);"); 9917 9918 // These are not casts. 9919 verifyFormat("void f(int *) {}"); 9920 verifyFormat("f(foo)->b;"); 9921 verifyFormat("f(foo).b;"); 9922 verifyFormat("f(foo)(b);"); 9923 verifyFormat("f(foo)[b];"); 9924 verifyFormat("[](foo) { return 4; }(bar);"); 9925 verifyFormat("(*funptr)(foo)[4];"); 9926 verifyFormat("funptrs[4](foo)[4];"); 9927 verifyFormat("void f(int *);"); 9928 verifyFormat("void f(int *) = 0;"); 9929 verifyFormat("void f(SmallVector<int>) {}"); 9930 verifyFormat("void f(SmallVector<int>);"); 9931 verifyFormat("void f(SmallVector<int>) = 0;"); 9932 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 9933 verifyFormat("int a = sizeof(int) * b;"); 9934 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 9935 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 9936 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 9937 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 9938 9939 // These are not casts, but at some point were confused with casts. 9940 verifyFormat("virtual void foo(int *) override;"); 9941 verifyFormat("virtual void foo(char &) const;"); 9942 verifyFormat("virtual void foo(int *a, char *) const;"); 9943 verifyFormat("int a = sizeof(int *) + b;"); 9944 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 9945 verifyFormat("bool b = f(g<int>) && c;"); 9946 verifyFormat("typedef void (*f)(int i) func;"); 9947 verifyFormat("void operator++(int) noexcept;"); 9948 verifyFormat("void operator++(int &) noexcept;"); 9949 verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t " 9950 "&) noexcept;"); 9951 verifyFormat( 9952 "void operator delete(std::size_t, const std::nothrow_t &) noexcept;"); 9953 verifyFormat("void operator delete(const std::nothrow_t &) noexcept;"); 9954 verifyFormat("void operator delete(std::nothrow_t &) noexcept;"); 9955 verifyFormat("void operator delete(nothrow_t &) noexcept;"); 9956 verifyFormat("void operator delete(foo &) noexcept;"); 9957 verifyFormat("void operator delete(foo) noexcept;"); 9958 verifyFormat("void operator delete(int) noexcept;"); 9959 verifyFormat("void operator delete(int &) noexcept;"); 9960 verifyFormat("void operator delete(int &) volatile noexcept;"); 9961 verifyFormat("void operator delete(int &) const"); 9962 verifyFormat("void operator delete(int &) = default"); 9963 verifyFormat("void operator delete(int &) = delete"); 9964 verifyFormat("void operator delete(int &) [[noreturn]]"); 9965 verifyFormat("void operator delete(int &) throw();"); 9966 verifyFormat("void operator delete(int &) throw(int);"); 9967 verifyFormat("auto operator delete(int &) -> int;"); 9968 verifyFormat("auto operator delete(int &) override"); 9969 verifyFormat("auto operator delete(int &) final"); 9970 9971 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 9972 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 9973 // FIXME: The indentation here is not ideal. 9974 verifyFormat( 9975 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9976 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 9977 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 9978 } 9979 9980 TEST_F(FormatTest, FormatsFunctionTypes) { 9981 verifyFormat("A<bool()> a;"); 9982 verifyFormat("A<SomeType()> a;"); 9983 verifyFormat("A<void (*)(int, std::string)> a;"); 9984 verifyFormat("A<void *(int)>;"); 9985 verifyFormat("void *(*a)(int *, SomeType *);"); 9986 verifyFormat("int (*func)(void *);"); 9987 verifyFormat("void f() { int (*func)(void *); }"); 9988 verifyFormat("template <class CallbackClass>\n" 9989 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 9990 9991 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 9992 verifyGoogleFormat("void* (*a)(int);"); 9993 verifyGoogleFormat( 9994 "template <class CallbackClass>\n" 9995 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 9996 9997 // Other constructs can look somewhat like function types: 9998 verifyFormat("A<sizeof(*x)> a;"); 9999 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 10000 verifyFormat("some_var = function(*some_pointer_var)[0];"); 10001 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 10002 verifyFormat("int x = f(&h)();"); 10003 verifyFormat("returnsFunction(¶m1, ¶m2)(param);"); 10004 verifyFormat("std::function<\n" 10005 " LooooooooooongTemplatedType<\n" 10006 " SomeType>*(\n" 10007 " LooooooooooooooooongType type)>\n" 10008 " function;", 10009 getGoogleStyleWithColumns(40)); 10010 } 10011 10012 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 10013 verifyFormat("A (*foo_)[6];"); 10014 verifyFormat("vector<int> (*foo_)[6];"); 10015 } 10016 10017 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 10018 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10019 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 10020 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 10021 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 10022 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10023 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 10024 10025 // Different ways of ()-initializiation. 10026 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10027 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 10028 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10029 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 10030 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10031 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 10032 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10033 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 10034 10035 // Lambdas should not confuse the variable declaration heuristic. 10036 verifyFormat("LooooooooooooooooongType\n" 10037 " variable(nullptr, [](A *a) {});", 10038 getLLVMStyleWithColumns(40)); 10039 } 10040 10041 TEST_F(FormatTest, BreaksLongDeclarations) { 10042 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 10043 " AnotherNameForTheLongType;"); 10044 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 10045 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 10046 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10047 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 10048 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 10049 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 10050 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10051 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10052 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 10053 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10054 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 10055 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10056 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 10057 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10058 verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n" 10059 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10060 verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n" 10061 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10062 verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n" 10063 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10064 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10065 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);"); 10066 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10067 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}"); 10068 FormatStyle Indented = getLLVMStyle(); 10069 Indented.IndentWrappedFunctionNames = true; 10070 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10071 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 10072 Indented); 10073 verifyFormat( 10074 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10075 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 10076 Indented); 10077 verifyFormat( 10078 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 10079 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 10080 Indented); 10081 verifyFormat( 10082 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 10083 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 10084 Indented); 10085 10086 // FIXME: Without the comment, this breaks after "(". 10087 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 10088 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 10089 getGoogleStyle()); 10090 10091 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 10092 " int LoooooooooooooooooooongParam2) {}"); 10093 verifyFormat( 10094 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 10095 " SourceLocation L, IdentifierIn *II,\n" 10096 " Type *T) {}"); 10097 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 10098 "ReallyReaaallyLongFunctionName(\n" 10099 " const std::string &SomeParameter,\n" 10100 " const SomeType<string, SomeOtherTemplateParameter>\n" 10101 " &ReallyReallyLongParameterName,\n" 10102 " const SomeType<string, SomeOtherTemplateParameter>\n" 10103 " &AnotherLongParameterName) {}"); 10104 verifyFormat("template <typename A>\n" 10105 "SomeLoooooooooooooooooooooongType<\n" 10106 " typename some_namespace::SomeOtherType<A>::Type>\n" 10107 "Function() {}"); 10108 10109 verifyGoogleFormat( 10110 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 10111 " aaaaaaaaaaaaaaaaaaaaaaa;"); 10112 verifyGoogleFormat( 10113 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 10114 " SourceLocation L) {}"); 10115 verifyGoogleFormat( 10116 "some_namespace::LongReturnType\n" 10117 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 10118 " int first_long_parameter, int second_parameter) {}"); 10119 10120 verifyGoogleFormat("template <typename T>\n" 10121 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 10122 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 10123 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 10124 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 10125 10126 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 10127 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10128 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 10129 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10130 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 10131 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 10132 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10133 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 10134 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 10135 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 10136 10137 verifyFormat("template <typename T> // Templates on own line.\n" 10138 "static int // Some comment.\n" 10139 "MyFunction(int a);", 10140 getLLVMStyle()); 10141 } 10142 10143 TEST_F(FormatTest, FormatsAccessModifiers) { 10144 FormatStyle Style = getLLVMStyle(); 10145 EXPECT_EQ(Style.EmptyLineBeforeAccessModifier, 10146 FormatStyle::ELBAMS_LogicalBlock); 10147 verifyFormat("struct foo {\n" 10148 "private:\n" 10149 " void f() {}\n" 10150 "\n" 10151 "private:\n" 10152 " int i;\n" 10153 "\n" 10154 "protected:\n" 10155 " int j;\n" 10156 "};\n", 10157 Style); 10158 verifyFormat("struct foo {\n" 10159 "private:\n" 10160 " void f() {}\n" 10161 "\n" 10162 "private:\n" 10163 " int i;\n" 10164 "\n" 10165 "protected:\n" 10166 " int j;\n" 10167 "};\n", 10168 "struct foo {\n" 10169 "private:\n" 10170 " void f() {}\n" 10171 "private:\n" 10172 " int i;\n" 10173 "protected:\n" 10174 " int j;\n" 10175 "};\n", 10176 Style); 10177 verifyFormat("struct foo { /* comment */\n" 10178 "private:\n" 10179 " int i;\n" 10180 " // comment\n" 10181 "private:\n" 10182 " int j;\n" 10183 "};\n", 10184 Style); 10185 verifyFormat("struct foo {\n" 10186 "#ifdef FOO\n" 10187 "#endif\n" 10188 "private:\n" 10189 " int i;\n" 10190 "#ifdef FOO\n" 10191 "private:\n" 10192 "#endif\n" 10193 " int j;\n" 10194 "};\n", 10195 Style); 10196 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10197 verifyFormat("struct foo {\n" 10198 "private:\n" 10199 " void f() {}\n" 10200 "private:\n" 10201 " int i;\n" 10202 "protected:\n" 10203 " int j;\n" 10204 "};\n", 10205 Style); 10206 verifyFormat("struct foo {\n" 10207 "private:\n" 10208 " void f() {}\n" 10209 "private:\n" 10210 " int i;\n" 10211 "protected:\n" 10212 " int j;\n" 10213 "};\n", 10214 "struct foo {\n" 10215 "\n" 10216 "private:\n" 10217 " void f() {}\n" 10218 "\n" 10219 "private:\n" 10220 " int i;\n" 10221 "\n" 10222 "protected:\n" 10223 " int j;\n" 10224 "};\n", 10225 Style); 10226 verifyFormat("struct foo { /* comment */\n" 10227 "private:\n" 10228 " int i;\n" 10229 " // comment\n" 10230 "private:\n" 10231 " int j;\n" 10232 "};\n", 10233 "struct foo { /* comment */\n" 10234 "\n" 10235 "private:\n" 10236 " int i;\n" 10237 " // comment\n" 10238 "\n" 10239 "private:\n" 10240 " int j;\n" 10241 "};\n", 10242 Style); 10243 verifyFormat("struct foo {\n" 10244 "#ifdef FOO\n" 10245 "#endif\n" 10246 "private:\n" 10247 " int i;\n" 10248 "#ifdef FOO\n" 10249 "private:\n" 10250 "#endif\n" 10251 " int j;\n" 10252 "};\n", 10253 "struct foo {\n" 10254 "#ifdef FOO\n" 10255 "#endif\n" 10256 "\n" 10257 "private:\n" 10258 " int i;\n" 10259 "#ifdef FOO\n" 10260 "\n" 10261 "private:\n" 10262 "#endif\n" 10263 " int j;\n" 10264 "};\n", 10265 Style); 10266 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10267 verifyFormat("struct foo {\n" 10268 "private:\n" 10269 " void f() {}\n" 10270 "\n" 10271 "private:\n" 10272 " int i;\n" 10273 "\n" 10274 "protected:\n" 10275 " int j;\n" 10276 "};\n", 10277 Style); 10278 verifyFormat("struct foo {\n" 10279 "private:\n" 10280 " void f() {}\n" 10281 "\n" 10282 "private:\n" 10283 " int i;\n" 10284 "\n" 10285 "protected:\n" 10286 " int j;\n" 10287 "};\n", 10288 "struct foo {\n" 10289 "private:\n" 10290 " void f() {}\n" 10291 "private:\n" 10292 " int i;\n" 10293 "protected:\n" 10294 " int j;\n" 10295 "};\n", 10296 Style); 10297 verifyFormat("struct foo { /* comment */\n" 10298 "private:\n" 10299 " int i;\n" 10300 " // comment\n" 10301 "\n" 10302 "private:\n" 10303 " int j;\n" 10304 "};\n", 10305 "struct foo { /* comment */\n" 10306 "private:\n" 10307 " int i;\n" 10308 " // comment\n" 10309 "\n" 10310 "private:\n" 10311 " int j;\n" 10312 "};\n", 10313 Style); 10314 verifyFormat("struct foo {\n" 10315 "#ifdef FOO\n" 10316 "#endif\n" 10317 "\n" 10318 "private:\n" 10319 " int i;\n" 10320 "#ifdef FOO\n" 10321 "\n" 10322 "private:\n" 10323 "#endif\n" 10324 " int j;\n" 10325 "};\n", 10326 "struct foo {\n" 10327 "#ifdef FOO\n" 10328 "#endif\n" 10329 "private:\n" 10330 " int i;\n" 10331 "#ifdef FOO\n" 10332 "private:\n" 10333 "#endif\n" 10334 " int j;\n" 10335 "};\n", 10336 Style); 10337 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 10338 EXPECT_EQ("struct foo {\n" 10339 "\n" 10340 "private:\n" 10341 " void f() {}\n" 10342 "\n" 10343 "private:\n" 10344 " int i;\n" 10345 "\n" 10346 "protected:\n" 10347 " int j;\n" 10348 "};\n", 10349 format("struct foo {\n" 10350 "\n" 10351 "private:\n" 10352 " void f() {}\n" 10353 "\n" 10354 "private:\n" 10355 " int i;\n" 10356 "\n" 10357 "protected:\n" 10358 " int j;\n" 10359 "};\n", 10360 Style)); 10361 verifyFormat("struct foo {\n" 10362 "private:\n" 10363 " void f() {}\n" 10364 "private:\n" 10365 " int i;\n" 10366 "protected:\n" 10367 " int j;\n" 10368 "};\n", 10369 Style); 10370 EXPECT_EQ("struct foo { /* comment */\n" 10371 "\n" 10372 "private:\n" 10373 " int i;\n" 10374 " // comment\n" 10375 "\n" 10376 "private:\n" 10377 " int j;\n" 10378 "};\n", 10379 format("struct foo { /* comment */\n" 10380 "\n" 10381 "private:\n" 10382 " int i;\n" 10383 " // comment\n" 10384 "\n" 10385 "private:\n" 10386 " int j;\n" 10387 "};\n", 10388 Style)); 10389 verifyFormat("struct foo { /* comment */\n" 10390 "private:\n" 10391 " int i;\n" 10392 " // comment\n" 10393 "private:\n" 10394 " int j;\n" 10395 "};\n", 10396 Style); 10397 EXPECT_EQ("struct foo {\n" 10398 "#ifdef FOO\n" 10399 "#endif\n" 10400 "\n" 10401 "private:\n" 10402 " int i;\n" 10403 "#ifdef FOO\n" 10404 "\n" 10405 "private:\n" 10406 "#endif\n" 10407 " int j;\n" 10408 "};\n", 10409 format("struct foo {\n" 10410 "#ifdef FOO\n" 10411 "#endif\n" 10412 "\n" 10413 "private:\n" 10414 " int i;\n" 10415 "#ifdef FOO\n" 10416 "\n" 10417 "private:\n" 10418 "#endif\n" 10419 " int j;\n" 10420 "};\n", 10421 Style)); 10422 verifyFormat("struct foo {\n" 10423 "#ifdef FOO\n" 10424 "#endif\n" 10425 "private:\n" 10426 " int i;\n" 10427 "#ifdef FOO\n" 10428 "private:\n" 10429 "#endif\n" 10430 " int j;\n" 10431 "};\n", 10432 Style); 10433 10434 FormatStyle NoEmptyLines = getLLVMStyle(); 10435 NoEmptyLines.MaxEmptyLinesToKeep = 0; 10436 verifyFormat("struct foo {\n" 10437 "private:\n" 10438 " void f() {}\n" 10439 "\n" 10440 "private:\n" 10441 " int i;\n" 10442 "\n" 10443 "public:\n" 10444 "protected:\n" 10445 " int j;\n" 10446 "};\n", 10447 NoEmptyLines); 10448 10449 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10450 verifyFormat("struct foo {\n" 10451 "private:\n" 10452 " void f() {}\n" 10453 "private:\n" 10454 " int i;\n" 10455 "public:\n" 10456 "protected:\n" 10457 " int j;\n" 10458 "};\n", 10459 NoEmptyLines); 10460 10461 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10462 verifyFormat("struct foo {\n" 10463 "private:\n" 10464 " void f() {}\n" 10465 "\n" 10466 "private:\n" 10467 " int i;\n" 10468 "\n" 10469 "public:\n" 10470 "\n" 10471 "protected:\n" 10472 " int j;\n" 10473 "};\n", 10474 NoEmptyLines); 10475 } 10476 10477 TEST_F(FormatTest, FormatsAfterAccessModifiers) { 10478 10479 FormatStyle Style = getLLVMStyle(); 10480 EXPECT_EQ(Style.EmptyLineAfterAccessModifier, FormatStyle::ELAAMS_Never); 10481 verifyFormat("struct foo {\n" 10482 "private:\n" 10483 " void f() {}\n" 10484 "\n" 10485 "private:\n" 10486 " int i;\n" 10487 "\n" 10488 "protected:\n" 10489 " int j;\n" 10490 "};\n", 10491 Style); 10492 10493 // Check if lines are removed. 10494 verifyFormat("struct foo {\n" 10495 "private:\n" 10496 " void f() {}\n" 10497 "\n" 10498 "private:\n" 10499 " int i;\n" 10500 "\n" 10501 "protected:\n" 10502 " int j;\n" 10503 "};\n", 10504 "struct foo {\n" 10505 "private:\n" 10506 "\n" 10507 " void f() {}\n" 10508 "\n" 10509 "private:\n" 10510 "\n" 10511 " int i;\n" 10512 "\n" 10513 "protected:\n" 10514 "\n" 10515 " int j;\n" 10516 "};\n", 10517 Style); 10518 10519 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10520 verifyFormat("struct foo {\n" 10521 "private:\n" 10522 "\n" 10523 " void f() {}\n" 10524 "\n" 10525 "private:\n" 10526 "\n" 10527 " int i;\n" 10528 "\n" 10529 "protected:\n" 10530 "\n" 10531 " int j;\n" 10532 "};\n", 10533 Style); 10534 10535 // Check if lines are added. 10536 verifyFormat("struct foo {\n" 10537 "private:\n" 10538 "\n" 10539 " void f() {}\n" 10540 "\n" 10541 "private:\n" 10542 "\n" 10543 " int i;\n" 10544 "\n" 10545 "protected:\n" 10546 "\n" 10547 " int j;\n" 10548 "};\n", 10549 "struct foo {\n" 10550 "private:\n" 10551 " void f() {}\n" 10552 "\n" 10553 "private:\n" 10554 " int i;\n" 10555 "\n" 10556 "protected:\n" 10557 " int j;\n" 10558 "};\n", 10559 Style); 10560 10561 // Leave tests rely on the code layout, test::messUp can not be used. 10562 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 10563 Style.MaxEmptyLinesToKeep = 0u; 10564 verifyFormat("struct foo {\n" 10565 "private:\n" 10566 " void f() {}\n" 10567 "\n" 10568 "private:\n" 10569 " int i;\n" 10570 "\n" 10571 "protected:\n" 10572 " int j;\n" 10573 "};\n", 10574 Style); 10575 10576 // Check if MaxEmptyLinesToKeep is respected. 10577 EXPECT_EQ("struct foo {\n" 10578 "private:\n" 10579 " void f() {}\n" 10580 "\n" 10581 "private:\n" 10582 " int i;\n" 10583 "\n" 10584 "protected:\n" 10585 " int j;\n" 10586 "};\n", 10587 format("struct foo {\n" 10588 "private:\n" 10589 "\n\n\n" 10590 " void f() {}\n" 10591 "\n" 10592 "private:\n" 10593 "\n\n\n" 10594 " int i;\n" 10595 "\n" 10596 "protected:\n" 10597 "\n\n\n" 10598 " int j;\n" 10599 "};\n", 10600 Style)); 10601 10602 Style.MaxEmptyLinesToKeep = 1u; 10603 EXPECT_EQ("struct foo {\n" 10604 "private:\n" 10605 "\n" 10606 " void f() {}\n" 10607 "\n" 10608 "private:\n" 10609 "\n" 10610 " int i;\n" 10611 "\n" 10612 "protected:\n" 10613 "\n" 10614 " int j;\n" 10615 "};\n", 10616 format("struct foo {\n" 10617 "private:\n" 10618 "\n" 10619 " void f() {}\n" 10620 "\n" 10621 "private:\n" 10622 "\n" 10623 " int i;\n" 10624 "\n" 10625 "protected:\n" 10626 "\n" 10627 " int j;\n" 10628 "};\n", 10629 Style)); 10630 // Check if no lines are kept. 10631 EXPECT_EQ("struct foo {\n" 10632 "private:\n" 10633 " void f() {}\n" 10634 "\n" 10635 "private:\n" 10636 " int i;\n" 10637 "\n" 10638 "protected:\n" 10639 " int j;\n" 10640 "};\n", 10641 format("struct foo {\n" 10642 "private:\n" 10643 " void f() {}\n" 10644 "\n" 10645 "private:\n" 10646 " int i;\n" 10647 "\n" 10648 "protected:\n" 10649 " int j;\n" 10650 "};\n", 10651 Style)); 10652 // Check if MaxEmptyLinesToKeep is respected. 10653 EXPECT_EQ("struct foo {\n" 10654 "private:\n" 10655 "\n" 10656 " void f() {}\n" 10657 "\n" 10658 "private:\n" 10659 "\n" 10660 " int i;\n" 10661 "\n" 10662 "protected:\n" 10663 "\n" 10664 " int j;\n" 10665 "};\n", 10666 format("struct foo {\n" 10667 "private:\n" 10668 "\n\n\n" 10669 " void f() {}\n" 10670 "\n" 10671 "private:\n" 10672 "\n\n\n" 10673 " int i;\n" 10674 "\n" 10675 "protected:\n" 10676 "\n\n\n" 10677 " int j;\n" 10678 "};\n", 10679 Style)); 10680 10681 Style.MaxEmptyLinesToKeep = 10u; 10682 EXPECT_EQ("struct foo {\n" 10683 "private:\n" 10684 "\n\n\n" 10685 " void f() {}\n" 10686 "\n" 10687 "private:\n" 10688 "\n\n\n" 10689 " int i;\n" 10690 "\n" 10691 "protected:\n" 10692 "\n\n\n" 10693 " int j;\n" 10694 "};\n", 10695 format("struct foo {\n" 10696 "private:\n" 10697 "\n\n\n" 10698 " void f() {}\n" 10699 "\n" 10700 "private:\n" 10701 "\n\n\n" 10702 " int i;\n" 10703 "\n" 10704 "protected:\n" 10705 "\n\n\n" 10706 " int j;\n" 10707 "};\n", 10708 Style)); 10709 10710 // Test with comments. 10711 Style = getLLVMStyle(); 10712 verifyFormat("struct foo {\n" 10713 "private:\n" 10714 " // comment\n" 10715 " void f() {}\n" 10716 "\n" 10717 "private: /* comment */\n" 10718 " int i;\n" 10719 "};\n", 10720 Style); 10721 verifyFormat("struct foo {\n" 10722 "private:\n" 10723 " // comment\n" 10724 " void f() {}\n" 10725 "\n" 10726 "private: /* comment */\n" 10727 " int i;\n" 10728 "};\n", 10729 "struct foo {\n" 10730 "private:\n" 10731 "\n" 10732 " // comment\n" 10733 " void f() {}\n" 10734 "\n" 10735 "private: /* comment */\n" 10736 "\n" 10737 " int i;\n" 10738 "};\n", 10739 Style); 10740 10741 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10742 verifyFormat("struct foo {\n" 10743 "private:\n" 10744 "\n" 10745 " // comment\n" 10746 " void f() {}\n" 10747 "\n" 10748 "private: /* comment */\n" 10749 "\n" 10750 " int i;\n" 10751 "};\n", 10752 "struct foo {\n" 10753 "private:\n" 10754 " // comment\n" 10755 " void f() {}\n" 10756 "\n" 10757 "private: /* comment */\n" 10758 " int i;\n" 10759 "};\n", 10760 Style); 10761 verifyFormat("struct foo {\n" 10762 "private:\n" 10763 "\n" 10764 " // comment\n" 10765 " void f() {}\n" 10766 "\n" 10767 "private: /* comment */\n" 10768 "\n" 10769 " int i;\n" 10770 "};\n", 10771 Style); 10772 10773 // Test with preprocessor defines. 10774 Style = getLLVMStyle(); 10775 verifyFormat("struct foo {\n" 10776 "private:\n" 10777 "#ifdef FOO\n" 10778 "#endif\n" 10779 " void f() {}\n" 10780 "};\n", 10781 Style); 10782 verifyFormat("struct foo {\n" 10783 "private:\n" 10784 "#ifdef FOO\n" 10785 "#endif\n" 10786 " void f() {}\n" 10787 "};\n", 10788 "struct foo {\n" 10789 "private:\n" 10790 "\n" 10791 "#ifdef FOO\n" 10792 "#endif\n" 10793 " void f() {}\n" 10794 "};\n", 10795 Style); 10796 10797 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10798 verifyFormat("struct foo {\n" 10799 "private:\n" 10800 "\n" 10801 "#ifdef FOO\n" 10802 "#endif\n" 10803 " void f() {}\n" 10804 "};\n", 10805 "struct foo {\n" 10806 "private:\n" 10807 "#ifdef FOO\n" 10808 "#endif\n" 10809 " void f() {}\n" 10810 "};\n", 10811 Style); 10812 verifyFormat("struct foo {\n" 10813 "private:\n" 10814 "\n" 10815 "#ifdef FOO\n" 10816 "#endif\n" 10817 " void f() {}\n" 10818 "};\n", 10819 Style); 10820 } 10821 10822 TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) { 10823 // Combined tests of EmptyLineAfterAccessModifier and 10824 // EmptyLineBeforeAccessModifier. 10825 FormatStyle Style = getLLVMStyle(); 10826 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10827 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10828 verifyFormat("struct foo {\n" 10829 "private:\n" 10830 "\n" 10831 "protected:\n" 10832 "};\n", 10833 Style); 10834 10835 Style.MaxEmptyLinesToKeep = 10u; 10836 // Both remove all new lines. 10837 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10838 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 10839 verifyFormat("struct foo {\n" 10840 "private:\n" 10841 "protected:\n" 10842 "};\n", 10843 "struct foo {\n" 10844 "private:\n" 10845 "\n\n\n" 10846 "protected:\n" 10847 "};\n", 10848 Style); 10849 10850 // Leave tests rely on the code layout, test::messUp can not be used. 10851 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 10852 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 10853 Style.MaxEmptyLinesToKeep = 10u; 10854 EXPECT_EQ("struct foo {\n" 10855 "private:\n" 10856 "\n\n\n" 10857 "protected:\n" 10858 "};\n", 10859 format("struct foo {\n" 10860 "private:\n" 10861 "\n\n\n" 10862 "protected:\n" 10863 "};\n", 10864 Style)); 10865 Style.MaxEmptyLinesToKeep = 3u; 10866 EXPECT_EQ("struct foo {\n" 10867 "private:\n" 10868 "\n\n\n" 10869 "protected:\n" 10870 "};\n", 10871 format("struct foo {\n" 10872 "private:\n" 10873 "\n\n\n" 10874 "protected:\n" 10875 "};\n", 10876 Style)); 10877 Style.MaxEmptyLinesToKeep = 1u; 10878 EXPECT_EQ("struct foo {\n" 10879 "private:\n" 10880 "\n\n\n" 10881 "protected:\n" 10882 "};\n", 10883 format("struct foo {\n" 10884 "private:\n" 10885 "\n\n\n" 10886 "protected:\n" 10887 "};\n", 10888 Style)); // Based on new lines in original document and not 10889 // on the setting. 10890 10891 Style.MaxEmptyLinesToKeep = 10u; 10892 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10893 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 10894 // Newlines are kept if they are greater than zero, 10895 // test::messUp removes all new lines which changes the logic 10896 EXPECT_EQ("struct foo {\n" 10897 "private:\n" 10898 "\n\n\n" 10899 "protected:\n" 10900 "};\n", 10901 format("struct foo {\n" 10902 "private:\n" 10903 "\n\n\n" 10904 "protected:\n" 10905 "};\n", 10906 Style)); 10907 10908 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 10909 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10910 // test::messUp removes all new lines which changes the logic 10911 EXPECT_EQ("struct foo {\n" 10912 "private:\n" 10913 "\n\n\n" 10914 "protected:\n" 10915 "};\n", 10916 format("struct foo {\n" 10917 "private:\n" 10918 "\n\n\n" 10919 "protected:\n" 10920 "};\n", 10921 Style)); 10922 10923 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 10924 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 10925 EXPECT_EQ("struct foo {\n" 10926 "private:\n" 10927 "\n\n\n" 10928 "protected:\n" 10929 "};\n", 10930 format("struct foo {\n" 10931 "private:\n" 10932 "\n\n\n" 10933 "protected:\n" 10934 "};\n", 10935 Style)); // test::messUp removes all new lines which changes 10936 // the logic. 10937 10938 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10939 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 10940 verifyFormat("struct foo {\n" 10941 "private:\n" 10942 "protected:\n" 10943 "};\n", 10944 "struct foo {\n" 10945 "private:\n" 10946 "\n\n\n" 10947 "protected:\n" 10948 "};\n", 10949 Style); 10950 10951 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10952 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 10953 EXPECT_EQ("struct foo {\n" 10954 "private:\n" 10955 "\n\n\n" 10956 "protected:\n" 10957 "};\n", 10958 format("struct foo {\n" 10959 "private:\n" 10960 "\n\n\n" 10961 "protected:\n" 10962 "};\n", 10963 Style)); // test::messUp removes all new lines which changes 10964 // the logic. 10965 10966 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10967 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10968 verifyFormat("struct foo {\n" 10969 "private:\n" 10970 "protected:\n" 10971 "};\n", 10972 "struct foo {\n" 10973 "private:\n" 10974 "\n\n\n" 10975 "protected:\n" 10976 "};\n", 10977 Style); 10978 10979 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 10980 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10981 verifyFormat("struct foo {\n" 10982 "private:\n" 10983 "protected:\n" 10984 "};\n", 10985 "struct foo {\n" 10986 "private:\n" 10987 "\n\n\n" 10988 "protected:\n" 10989 "};\n", 10990 Style); 10991 10992 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 10993 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 10994 verifyFormat("struct foo {\n" 10995 "private:\n" 10996 "protected:\n" 10997 "};\n", 10998 "struct foo {\n" 10999 "private:\n" 11000 "\n\n\n" 11001 "protected:\n" 11002 "};\n", 11003 Style); 11004 11005 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 11006 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 11007 verifyFormat("struct foo {\n" 11008 "private:\n" 11009 "protected:\n" 11010 "};\n", 11011 "struct foo {\n" 11012 "private:\n" 11013 "\n\n\n" 11014 "protected:\n" 11015 "};\n", 11016 Style); 11017 } 11018 11019 TEST_F(FormatTest, FormatsArrays) { 11020 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 11021 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 11022 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 11023 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 11024 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 11025 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 11026 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11027 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 11028 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11029 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 11030 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11031 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 11032 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 11033 verifyFormat( 11034 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 11035 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 11036 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 11037 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" 11038 " .aaaaaaaaaaaaaaaaaaaaaa();"); 11039 11040 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 11041 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 11042 verifyFormat( 11043 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 11044 " .aaaaaaa[0]\n" 11045 " .aaaaaaaaaaaaaaaaaaaaaa();"); 11046 verifyFormat("a[::b::c];"); 11047 11048 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 11049 11050 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 11051 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 11052 } 11053 11054 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 11055 verifyFormat("(a)->b();"); 11056 verifyFormat("--a;"); 11057 } 11058 11059 TEST_F(FormatTest, HandlesIncludeDirectives) { 11060 verifyFormat("#include <string>\n" 11061 "#include <a/b/c.h>\n" 11062 "#include \"a/b/string\"\n" 11063 "#include \"string.h\"\n" 11064 "#include \"string.h\"\n" 11065 "#include <a-a>\n" 11066 "#include < path with space >\n" 11067 "#include_next <test.h>" 11068 "#include \"abc.h\" // this is included for ABC\n" 11069 "#include \"some long include\" // with a comment\n" 11070 "#include \"some very long include path\"\n" 11071 "#include <some/very/long/include/path>\n", 11072 getLLVMStyleWithColumns(35)); 11073 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 11074 EXPECT_EQ("#include <a>", format("#include<a>")); 11075 11076 verifyFormat("#import <string>"); 11077 verifyFormat("#import <a/b/c.h>"); 11078 verifyFormat("#import \"a/b/string\""); 11079 verifyFormat("#import \"string.h\""); 11080 verifyFormat("#import \"string.h\""); 11081 verifyFormat("#if __has_include(<strstream>)\n" 11082 "#include <strstream>\n" 11083 "#endif"); 11084 11085 verifyFormat("#define MY_IMPORT <a/b>"); 11086 11087 verifyFormat("#if __has_include(<a/b>)"); 11088 verifyFormat("#if __has_include_next(<a/b>)"); 11089 verifyFormat("#define F __has_include(<a/b>)"); 11090 verifyFormat("#define F __has_include_next(<a/b>)"); 11091 11092 // Protocol buffer definition or missing "#". 11093 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 11094 getLLVMStyleWithColumns(30)); 11095 11096 FormatStyle Style = getLLVMStyle(); 11097 Style.AlwaysBreakBeforeMultilineStrings = true; 11098 Style.ColumnLimit = 0; 11099 verifyFormat("#import \"abc.h\"", Style); 11100 11101 // But 'import' might also be a regular C++ namespace. 11102 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11103 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 11104 } 11105 11106 //===----------------------------------------------------------------------===// 11107 // Error recovery tests. 11108 //===----------------------------------------------------------------------===// 11109 11110 TEST_F(FormatTest, IncompleteParameterLists) { 11111 FormatStyle NoBinPacking = getLLVMStyle(); 11112 NoBinPacking.BinPackParameters = false; 11113 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 11114 " double *min_x,\n" 11115 " double *max_x,\n" 11116 " double *min_y,\n" 11117 " double *max_y,\n" 11118 " double *min_z,\n" 11119 " double *max_z, ) {}", 11120 NoBinPacking); 11121 } 11122 11123 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 11124 verifyFormat("void f() { return; }\n42"); 11125 verifyFormat("void f() {\n" 11126 " if (0)\n" 11127 " return;\n" 11128 "}\n" 11129 "42"); 11130 verifyFormat("void f() { return }\n42"); 11131 verifyFormat("void f() {\n" 11132 " if (0)\n" 11133 " return\n" 11134 "}\n" 11135 "42"); 11136 } 11137 11138 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 11139 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 11140 EXPECT_EQ("void f() {\n" 11141 " if (a)\n" 11142 " return\n" 11143 "}", 11144 format("void f ( ) { if ( a ) return }")); 11145 EXPECT_EQ("namespace N {\n" 11146 "void f()\n" 11147 "}", 11148 format("namespace N { void f() }")); 11149 EXPECT_EQ("namespace N {\n" 11150 "void f() {}\n" 11151 "void g()\n" 11152 "} // namespace N", 11153 format("namespace N { void f( ) { } void g( ) }")); 11154 } 11155 11156 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 11157 verifyFormat("int aaaaaaaa =\n" 11158 " // Overlylongcomment\n" 11159 " b;", 11160 getLLVMStyleWithColumns(20)); 11161 verifyFormat("function(\n" 11162 " ShortArgument,\n" 11163 " LoooooooooooongArgument);\n", 11164 getLLVMStyleWithColumns(20)); 11165 } 11166 11167 TEST_F(FormatTest, IncorrectAccessSpecifier) { 11168 verifyFormat("public:"); 11169 verifyFormat("class A {\n" 11170 "public\n" 11171 " void f() {}\n" 11172 "};"); 11173 verifyFormat("public\n" 11174 "int qwerty;"); 11175 verifyFormat("public\n" 11176 "B {}"); 11177 verifyFormat("public\n" 11178 "{}"); 11179 verifyFormat("public\n" 11180 "B { int x; }"); 11181 } 11182 11183 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 11184 verifyFormat("{"); 11185 verifyFormat("#})"); 11186 verifyNoCrash("(/**/[:!] ?[)."); 11187 } 11188 11189 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) { 11190 // Found by oss-fuzz: 11191 // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212 11192 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 11193 Style.ColumnLimit = 60; 11194 verifyNoCrash( 11195 "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20" 11196 "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20" 11197 "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a", 11198 Style); 11199 } 11200 11201 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 11202 verifyFormat("do {\n}"); 11203 verifyFormat("do {\n}\n" 11204 "f();"); 11205 verifyFormat("do {\n}\n" 11206 "wheeee(fun);"); 11207 verifyFormat("do {\n" 11208 " f();\n" 11209 "}"); 11210 } 11211 11212 TEST_F(FormatTest, IncorrectCodeMissingParens) { 11213 verifyFormat("if {\n foo;\n foo();\n}"); 11214 verifyFormat("switch {\n foo;\n foo();\n}"); 11215 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 11216 verifyFormat("while {\n foo;\n foo();\n}"); 11217 verifyFormat("do {\n foo;\n foo();\n} while;"); 11218 } 11219 11220 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 11221 verifyIncompleteFormat("namespace {\n" 11222 "class Foo { Foo (\n" 11223 "};\n" 11224 "} // namespace"); 11225 } 11226 11227 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 11228 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 11229 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 11230 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 11231 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 11232 11233 EXPECT_EQ("{\n" 11234 " {\n" 11235 " breakme(\n" 11236 " qwe);\n" 11237 " }\n", 11238 format("{\n" 11239 " {\n" 11240 " breakme(qwe);\n" 11241 "}\n", 11242 getLLVMStyleWithColumns(10))); 11243 } 11244 11245 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 11246 verifyFormat("int x = {\n" 11247 " avariable,\n" 11248 " b(alongervariable)};", 11249 getLLVMStyleWithColumns(25)); 11250 } 11251 11252 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 11253 verifyFormat("return (a)(b){1, 2, 3};"); 11254 } 11255 11256 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 11257 verifyFormat("vector<int> x{1, 2, 3, 4};"); 11258 verifyFormat("vector<int> x{\n" 11259 " 1,\n" 11260 " 2,\n" 11261 " 3,\n" 11262 " 4,\n" 11263 "};"); 11264 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 11265 verifyFormat("f({1, 2});"); 11266 verifyFormat("auto v = Foo{-1};"); 11267 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 11268 verifyFormat("Class::Class : member{1, 2, 3} {}"); 11269 verifyFormat("new vector<int>{1, 2, 3};"); 11270 verifyFormat("new int[3]{1, 2, 3};"); 11271 verifyFormat("new int{1};"); 11272 verifyFormat("return {arg1, arg2};"); 11273 verifyFormat("return {arg1, SomeType{parameter}};"); 11274 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 11275 verifyFormat("new T{arg1, arg2};"); 11276 verifyFormat("f(MyMap[{composite, key}]);"); 11277 verifyFormat("class Class {\n" 11278 " T member = {arg1, arg2};\n" 11279 "};"); 11280 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 11281 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 11282 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 11283 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 11284 verifyFormat("int a = std::is_integral<int>{} + 0;"); 11285 11286 verifyFormat("int foo(int i) { return fo1{}(i); }"); 11287 verifyFormat("int foo(int i) { return fo1{}(i); }"); 11288 verifyFormat("auto i = decltype(x){};"); 11289 verifyFormat("auto i = typeof(x){};"); 11290 verifyFormat("auto i = _Atomic(x){};"); 11291 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 11292 verifyFormat("Node n{1, Node{1000}, //\n" 11293 " 2};"); 11294 verifyFormat("Aaaa aaaaaaa{\n" 11295 " {\n" 11296 " aaaa,\n" 11297 " },\n" 11298 "};"); 11299 verifyFormat("class C : public D {\n" 11300 " SomeClass SC{2};\n" 11301 "};"); 11302 verifyFormat("class C : public A {\n" 11303 " class D : public B {\n" 11304 " void f() { int i{2}; }\n" 11305 " };\n" 11306 "};"); 11307 verifyFormat("#define A {a, a},"); 11308 11309 // Avoid breaking between equal sign and opening brace 11310 FormatStyle AvoidBreakingFirstArgument = getLLVMStyle(); 11311 AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200; 11312 verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n" 11313 " {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n" 11314 " {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n" 11315 " {\"ccccccccccccccccccccc\", 2}};", 11316 AvoidBreakingFirstArgument); 11317 11318 // Binpacking only if there is no trailing comma 11319 verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n" 11320 " cccccccccc, dddddddddd};", 11321 getLLVMStyleWithColumns(50)); 11322 verifyFormat("const Aaaaaa aaaaa = {\n" 11323 " aaaaaaaaaaa,\n" 11324 " bbbbbbbbbbb,\n" 11325 " ccccccccccc,\n" 11326 " ddddddddddd,\n" 11327 "};", 11328 getLLVMStyleWithColumns(50)); 11329 11330 // Cases where distinguising braced lists and blocks is hard. 11331 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);"); 11332 verifyFormat("void f() {\n" 11333 " return; // comment\n" 11334 "}\n" 11335 "SomeType t;"); 11336 verifyFormat("void f() {\n" 11337 " if (a) {\n" 11338 " f();\n" 11339 " }\n" 11340 "}\n" 11341 "SomeType t;"); 11342 11343 // In combination with BinPackArguments = false. 11344 FormatStyle NoBinPacking = getLLVMStyle(); 11345 NoBinPacking.BinPackArguments = false; 11346 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 11347 " bbbbb,\n" 11348 " ccccc,\n" 11349 " ddddd,\n" 11350 " eeeee,\n" 11351 " ffffff,\n" 11352 " ggggg,\n" 11353 " hhhhhh,\n" 11354 " iiiiii,\n" 11355 " jjjjjj,\n" 11356 " kkkkkk};", 11357 NoBinPacking); 11358 verifyFormat("const Aaaaaa aaaaa = {\n" 11359 " aaaaa,\n" 11360 " bbbbb,\n" 11361 " ccccc,\n" 11362 " ddddd,\n" 11363 " eeeee,\n" 11364 " ffffff,\n" 11365 " ggggg,\n" 11366 " hhhhhh,\n" 11367 " iiiiii,\n" 11368 " jjjjjj,\n" 11369 " kkkkkk,\n" 11370 "};", 11371 NoBinPacking); 11372 verifyFormat( 11373 "const Aaaaaa aaaaa = {\n" 11374 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 11375 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 11376 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 11377 "};", 11378 NoBinPacking); 11379 11380 NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 11381 EXPECT_EQ("static uint8 CddDp83848Reg[] = {\n" 11382 " CDDDP83848_BMCR_REGISTER,\n" 11383 " CDDDP83848_BMSR_REGISTER,\n" 11384 " CDDDP83848_RBR_REGISTER};", 11385 format("static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n" 11386 " CDDDP83848_BMSR_REGISTER,\n" 11387 " CDDDP83848_RBR_REGISTER};", 11388 NoBinPacking)); 11389 11390 // FIXME: The alignment of these trailing comments might be bad. Then again, 11391 // this might be utterly useless in real code. 11392 verifyFormat("Constructor::Constructor()\n" 11393 " : some_value{ //\n" 11394 " aaaaaaa, //\n" 11395 " bbbbbbb} {}"); 11396 11397 // In braced lists, the first comment is always assumed to belong to the 11398 // first element. Thus, it can be moved to the next or previous line as 11399 // appropriate. 11400 EXPECT_EQ("function({// First element:\n" 11401 " 1,\n" 11402 " // Second element:\n" 11403 " 2});", 11404 format("function({\n" 11405 " // First element:\n" 11406 " 1,\n" 11407 " // Second element:\n" 11408 " 2});")); 11409 EXPECT_EQ("std::vector<int> MyNumbers{\n" 11410 " // First element:\n" 11411 " 1,\n" 11412 " // Second element:\n" 11413 " 2};", 11414 format("std::vector<int> MyNumbers{// First element:\n" 11415 " 1,\n" 11416 " // Second element:\n" 11417 " 2};", 11418 getLLVMStyleWithColumns(30))); 11419 // A trailing comma should still lead to an enforced line break and no 11420 // binpacking. 11421 EXPECT_EQ("vector<int> SomeVector = {\n" 11422 " // aaa\n" 11423 " 1,\n" 11424 " 2,\n" 11425 "};", 11426 format("vector<int> SomeVector = { // aaa\n" 11427 " 1, 2, };")); 11428 11429 // C++11 brace initializer list l-braces should not be treated any differently 11430 // when breaking before lambda bodies is enabled 11431 FormatStyle BreakBeforeLambdaBody = getLLVMStyle(); 11432 BreakBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 11433 BreakBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 11434 BreakBeforeLambdaBody.AlwaysBreakBeforeMultilineStrings = true; 11435 verifyFormat( 11436 "std::runtime_error{\n" 11437 " \"Long string which will force a break onto the next line...\"};", 11438 BreakBeforeLambdaBody); 11439 11440 FormatStyle ExtraSpaces = getLLVMStyle(); 11441 ExtraSpaces.Cpp11BracedListStyle = false; 11442 ExtraSpaces.ColumnLimit = 75; 11443 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 11444 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 11445 verifyFormat("f({ 1, 2 });", ExtraSpaces); 11446 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 11447 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 11448 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 11449 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 11450 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 11451 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 11452 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 11453 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 11454 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 11455 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 11456 verifyFormat("class Class {\n" 11457 " T member = { arg1, arg2 };\n" 11458 "};", 11459 ExtraSpaces); 11460 verifyFormat( 11461 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11462 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 11463 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 11464 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 11465 ExtraSpaces); 11466 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 11467 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 11468 ExtraSpaces); 11469 verifyFormat( 11470 "someFunction(OtherParam,\n" 11471 " BracedList{ // comment 1 (Forcing interesting break)\n" 11472 " param1, param2,\n" 11473 " // comment 2\n" 11474 " param3, param4 });", 11475 ExtraSpaces); 11476 verifyFormat( 11477 "std::this_thread::sleep_for(\n" 11478 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 11479 ExtraSpaces); 11480 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n" 11481 " aaaaaaa,\n" 11482 " aaaaaaaaaa,\n" 11483 " aaaaa,\n" 11484 " aaaaaaaaaaaaaaa,\n" 11485 " aaa,\n" 11486 " aaaaaaaaaa,\n" 11487 " a,\n" 11488 " aaaaaaaaaaaaaaaaaaaaa,\n" 11489 " aaaaaaaaaaaa,\n" 11490 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 11491 " aaaaaaa,\n" 11492 " a};"); 11493 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 11494 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces); 11495 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces); 11496 11497 // Avoid breaking between initializer/equal sign and opening brace 11498 ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200; 11499 verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n" 11500 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 11501 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 11502 " { \"ccccccccccccccccccccc\", 2 }\n" 11503 "};", 11504 ExtraSpaces); 11505 verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n" 11506 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 11507 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 11508 " { \"ccccccccccccccccccccc\", 2 }\n" 11509 "};", 11510 ExtraSpaces); 11511 11512 FormatStyle SpaceBeforeBrace = getLLVMStyle(); 11513 SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true; 11514 verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace); 11515 verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace); 11516 11517 FormatStyle SpaceBetweenBraces = getLLVMStyle(); 11518 SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always; 11519 SpaceBetweenBraces.SpacesInParentheses = true; 11520 SpaceBetweenBraces.SpacesInSquareBrackets = true; 11521 verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces); 11522 verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces); 11523 verifyFormat("vector< int > x{ // comment 1\n" 11524 " 1, 2, 3, 4 };", 11525 SpaceBetweenBraces); 11526 SpaceBetweenBraces.ColumnLimit = 20; 11527 EXPECT_EQ("vector< int > x{\n" 11528 " 1, 2, 3, 4 };", 11529 format("vector<int>x{1,2,3,4};", SpaceBetweenBraces)); 11530 SpaceBetweenBraces.ColumnLimit = 24; 11531 EXPECT_EQ("vector< int > x{ 1, 2,\n" 11532 " 3, 4 };", 11533 format("vector<int>x{1,2,3,4};", SpaceBetweenBraces)); 11534 EXPECT_EQ("vector< int > x{\n" 11535 " 1,\n" 11536 " 2,\n" 11537 " 3,\n" 11538 " 4,\n" 11539 "};", 11540 format("vector<int>x{1,2,3,4,};", SpaceBetweenBraces)); 11541 verifyFormat("vector< int > x{};", SpaceBetweenBraces); 11542 SpaceBetweenBraces.SpaceInEmptyParentheses = true; 11543 verifyFormat("vector< int > x{ };", SpaceBetweenBraces); 11544 } 11545 11546 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 11547 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11548 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11549 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11550 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11551 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11552 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 11553 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 11554 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11555 " 1, 22, 333, 4444, 55555, //\n" 11556 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11557 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 11558 verifyFormat( 11559 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11560 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11561 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 11562 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 11563 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 11564 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 11565 " 7777777};"); 11566 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 11567 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 11568 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 11569 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 11570 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 11571 " // Separating comment.\n" 11572 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 11573 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 11574 " // Leading comment\n" 11575 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 11576 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 11577 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 11578 " 1, 1, 1, 1};", 11579 getLLVMStyleWithColumns(39)); 11580 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 11581 " 1, 1, 1, 1};", 11582 getLLVMStyleWithColumns(38)); 11583 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 11584 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 11585 getLLVMStyleWithColumns(43)); 11586 verifyFormat( 11587 "static unsigned SomeValues[10][3] = {\n" 11588 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 11589 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 11590 verifyFormat("static auto fields = new vector<string>{\n" 11591 " \"aaaaaaaaaaaaa\",\n" 11592 " \"aaaaaaaaaaaaa\",\n" 11593 " \"aaaaaaaaaaaa\",\n" 11594 " \"aaaaaaaaaaaaaa\",\n" 11595 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 11596 " \"aaaaaaaaaaaa\",\n" 11597 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 11598 "};"); 11599 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 11600 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 11601 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 11602 " 3, cccccccccccccccccccccc};", 11603 getLLVMStyleWithColumns(60)); 11604 11605 // Trailing commas. 11606 verifyFormat("vector<int> x = {\n" 11607 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 11608 "};", 11609 getLLVMStyleWithColumns(39)); 11610 verifyFormat("vector<int> x = {\n" 11611 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 11612 "};", 11613 getLLVMStyleWithColumns(39)); 11614 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 11615 " 1, 1, 1, 1,\n" 11616 " /**/ /**/};", 11617 getLLVMStyleWithColumns(39)); 11618 11619 // Trailing comment in the first line. 11620 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 11621 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 11622 " 111111111, 222222222, 3333333333, 444444444, //\n" 11623 " 11111111, 22222222, 333333333, 44444444};"); 11624 // Trailing comment in the last line. 11625 verifyFormat("int aaaaa[] = {\n" 11626 " 1, 2, 3, // comment\n" 11627 " 4, 5, 6 // comment\n" 11628 "};"); 11629 11630 // With nested lists, we should either format one item per line or all nested 11631 // lists one on line. 11632 // FIXME: For some nested lists, we can do better. 11633 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 11634 " {aaaaaaaaaaaaaaaaaaa},\n" 11635 " {aaaaaaaaaaaaaaaaaaaaa},\n" 11636 " {aaaaaaaaaaaaaaaaa}};", 11637 getLLVMStyleWithColumns(60)); 11638 verifyFormat( 11639 "SomeStruct my_struct_array = {\n" 11640 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 11641 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 11642 " {aaa, aaa},\n" 11643 " {aaa, aaa},\n" 11644 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 11645 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 11646 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 11647 11648 // No column layout should be used here. 11649 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 11650 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 11651 11652 verifyNoCrash("a<,"); 11653 11654 // No braced initializer here. 11655 verifyFormat("void f() {\n" 11656 " struct Dummy {};\n" 11657 " f(v);\n" 11658 "}"); 11659 11660 // Long lists should be formatted in columns even if they are nested. 11661 verifyFormat( 11662 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11663 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11664 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11665 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11666 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11667 " 1, 22, 333, 4444, 55555, 666666, 7777777});"); 11668 11669 // Allow "single-column" layout even if that violates the column limit. There 11670 // isn't going to be a better way. 11671 verifyFormat("std::vector<int> a = {\n" 11672 " aaaaaaaa,\n" 11673 " aaaaaaaa,\n" 11674 " aaaaaaaa,\n" 11675 " aaaaaaaa,\n" 11676 " aaaaaaaaaa,\n" 11677 " aaaaaaaa,\n" 11678 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};", 11679 getLLVMStyleWithColumns(30)); 11680 verifyFormat("vector<int> aaaa = {\n" 11681 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11682 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11683 " aaaaaa.aaaaaaa,\n" 11684 " aaaaaa.aaaaaaa,\n" 11685 " aaaaaa.aaaaaaa,\n" 11686 " aaaaaa.aaaaaaa,\n" 11687 "};"); 11688 11689 // Don't create hanging lists. 11690 verifyFormat("someFunction(Param, {List1, List2,\n" 11691 " List3});", 11692 getLLVMStyleWithColumns(35)); 11693 verifyFormat("someFunction(Param, Param,\n" 11694 " {List1, List2,\n" 11695 " List3});", 11696 getLLVMStyleWithColumns(35)); 11697 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n" 11698 " aaaaaaaaaaaaaaaaaaaaaaa);"); 11699 } 11700 11701 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 11702 FormatStyle DoNotMerge = getLLVMStyle(); 11703 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 11704 11705 verifyFormat("void f() { return 42; }"); 11706 verifyFormat("void f() {\n" 11707 " return 42;\n" 11708 "}", 11709 DoNotMerge); 11710 verifyFormat("void f() {\n" 11711 " // Comment\n" 11712 "}"); 11713 verifyFormat("{\n" 11714 "#error {\n" 11715 " int a;\n" 11716 "}"); 11717 verifyFormat("{\n" 11718 " int a;\n" 11719 "#error {\n" 11720 "}"); 11721 verifyFormat("void f() {} // comment"); 11722 verifyFormat("void f() { int a; } // comment"); 11723 verifyFormat("void f() {\n" 11724 "} // comment", 11725 DoNotMerge); 11726 verifyFormat("void f() {\n" 11727 " int a;\n" 11728 "} // comment", 11729 DoNotMerge); 11730 verifyFormat("void f() {\n" 11731 "} // comment", 11732 getLLVMStyleWithColumns(15)); 11733 11734 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 11735 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 11736 11737 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 11738 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 11739 verifyFormat("class C {\n" 11740 " C()\n" 11741 " : iiiiiiii(nullptr),\n" 11742 " kkkkkkk(nullptr),\n" 11743 " mmmmmmm(nullptr),\n" 11744 " nnnnnnn(nullptr) {}\n" 11745 "};", 11746 getGoogleStyle()); 11747 11748 FormatStyle NoColumnLimit = getLLVMStyle(); 11749 NoColumnLimit.ColumnLimit = 0; 11750 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 11751 EXPECT_EQ("class C {\n" 11752 " A() : b(0) {}\n" 11753 "};", 11754 format("class C{A():b(0){}};", NoColumnLimit)); 11755 EXPECT_EQ("A()\n" 11756 " : b(0) {\n" 11757 "}", 11758 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 11759 11760 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 11761 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 11762 FormatStyle::SFS_None; 11763 EXPECT_EQ("A()\n" 11764 " : b(0) {\n" 11765 "}", 11766 format("A():b(0){}", DoNotMergeNoColumnLimit)); 11767 EXPECT_EQ("A()\n" 11768 " : b(0) {\n" 11769 "}", 11770 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 11771 11772 verifyFormat("#define A \\\n" 11773 " void f() { \\\n" 11774 " int i; \\\n" 11775 " }", 11776 getLLVMStyleWithColumns(20)); 11777 verifyFormat("#define A \\\n" 11778 " void f() { int i; }", 11779 getLLVMStyleWithColumns(21)); 11780 verifyFormat("#define A \\\n" 11781 " void f() { \\\n" 11782 " int i; \\\n" 11783 " } \\\n" 11784 " int j;", 11785 getLLVMStyleWithColumns(22)); 11786 verifyFormat("#define A \\\n" 11787 " void f() { int i; } \\\n" 11788 " int j;", 11789 getLLVMStyleWithColumns(23)); 11790 } 11791 11792 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) { 11793 FormatStyle MergeEmptyOnly = getLLVMStyle(); 11794 MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 11795 verifyFormat("class C {\n" 11796 " int f() {}\n" 11797 "};", 11798 MergeEmptyOnly); 11799 verifyFormat("class C {\n" 11800 " int f() {\n" 11801 " return 42;\n" 11802 " }\n" 11803 "};", 11804 MergeEmptyOnly); 11805 verifyFormat("int f() {}", MergeEmptyOnly); 11806 verifyFormat("int f() {\n" 11807 " return 42;\n" 11808 "}", 11809 MergeEmptyOnly); 11810 11811 // Also verify behavior when BraceWrapping.AfterFunction = true 11812 MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 11813 MergeEmptyOnly.BraceWrapping.AfterFunction = true; 11814 verifyFormat("int f() {}", MergeEmptyOnly); 11815 verifyFormat("class C {\n" 11816 " int f() {}\n" 11817 "};", 11818 MergeEmptyOnly); 11819 } 11820 11821 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 11822 FormatStyle MergeInlineOnly = getLLVMStyle(); 11823 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 11824 verifyFormat("class C {\n" 11825 " int f() { return 42; }\n" 11826 "};", 11827 MergeInlineOnly); 11828 verifyFormat("int f() {\n" 11829 " return 42;\n" 11830 "}", 11831 MergeInlineOnly); 11832 11833 // SFS_Inline implies SFS_Empty 11834 verifyFormat("class C {\n" 11835 " int f() {}\n" 11836 "};", 11837 MergeInlineOnly); 11838 verifyFormat("int f() {}", MergeInlineOnly); 11839 11840 // Also verify behavior when BraceWrapping.AfterFunction = true 11841 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 11842 MergeInlineOnly.BraceWrapping.AfterFunction = true; 11843 verifyFormat("class C {\n" 11844 " int f() { return 42; }\n" 11845 "};", 11846 MergeInlineOnly); 11847 verifyFormat("int f()\n" 11848 "{\n" 11849 " return 42;\n" 11850 "}", 11851 MergeInlineOnly); 11852 11853 // SFS_Inline implies SFS_Empty 11854 verifyFormat("int f() {}", MergeInlineOnly); 11855 verifyFormat("class C {\n" 11856 " int f() {}\n" 11857 "};", 11858 MergeInlineOnly); 11859 } 11860 11861 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) { 11862 FormatStyle MergeInlineOnly = getLLVMStyle(); 11863 MergeInlineOnly.AllowShortFunctionsOnASingleLine = 11864 FormatStyle::SFS_InlineOnly; 11865 verifyFormat("class C {\n" 11866 " int f() { return 42; }\n" 11867 "};", 11868 MergeInlineOnly); 11869 verifyFormat("int f() {\n" 11870 " return 42;\n" 11871 "}", 11872 MergeInlineOnly); 11873 11874 // SFS_InlineOnly does not imply SFS_Empty 11875 verifyFormat("class C {\n" 11876 " int f() {}\n" 11877 "};", 11878 MergeInlineOnly); 11879 verifyFormat("int f() {\n" 11880 "}", 11881 MergeInlineOnly); 11882 11883 // Also verify behavior when BraceWrapping.AfterFunction = true 11884 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 11885 MergeInlineOnly.BraceWrapping.AfterFunction = true; 11886 verifyFormat("class C {\n" 11887 " int f() { return 42; }\n" 11888 "};", 11889 MergeInlineOnly); 11890 verifyFormat("int f()\n" 11891 "{\n" 11892 " return 42;\n" 11893 "}", 11894 MergeInlineOnly); 11895 11896 // SFS_InlineOnly does not imply SFS_Empty 11897 verifyFormat("int f()\n" 11898 "{\n" 11899 "}", 11900 MergeInlineOnly); 11901 verifyFormat("class C {\n" 11902 " int f() {}\n" 11903 "};", 11904 MergeInlineOnly); 11905 } 11906 11907 TEST_F(FormatTest, SplitEmptyFunction) { 11908 FormatStyle Style = getLLVMStyle(); 11909 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 11910 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 11911 Style.BraceWrapping.AfterFunction = true; 11912 Style.BraceWrapping.SplitEmptyFunction = false; 11913 Style.ColumnLimit = 40; 11914 11915 verifyFormat("int f()\n" 11916 "{}", 11917 Style); 11918 verifyFormat("int f()\n" 11919 "{\n" 11920 " return 42;\n" 11921 "}", 11922 Style); 11923 verifyFormat("int f()\n" 11924 "{\n" 11925 " // some comment\n" 11926 "}", 11927 Style); 11928 11929 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 11930 verifyFormat("int f() {}", Style); 11931 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 11932 "{}", 11933 Style); 11934 verifyFormat("int f()\n" 11935 "{\n" 11936 " return 0;\n" 11937 "}", 11938 Style); 11939 11940 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 11941 verifyFormat("class Foo {\n" 11942 " int f() {}\n" 11943 "};\n", 11944 Style); 11945 verifyFormat("class Foo {\n" 11946 " int f() { return 0; }\n" 11947 "};\n", 11948 Style); 11949 verifyFormat("class Foo {\n" 11950 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 11951 " {}\n" 11952 "};\n", 11953 Style); 11954 verifyFormat("class Foo {\n" 11955 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 11956 " {\n" 11957 " return 0;\n" 11958 " }\n" 11959 "};\n", 11960 Style); 11961 11962 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 11963 verifyFormat("int f() {}", Style); 11964 verifyFormat("int f() { return 0; }", Style); 11965 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 11966 "{}", 11967 Style); 11968 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 11969 "{\n" 11970 " return 0;\n" 11971 "}", 11972 Style); 11973 } 11974 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) { 11975 FormatStyle Style = getLLVMStyle(); 11976 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 11977 verifyFormat("#ifdef A\n" 11978 "int f() {}\n" 11979 "#else\n" 11980 "int g() {}\n" 11981 "#endif", 11982 Style); 11983 } 11984 11985 TEST_F(FormatTest, SplitEmptyClass) { 11986 FormatStyle Style = getLLVMStyle(); 11987 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 11988 Style.BraceWrapping.AfterClass = true; 11989 Style.BraceWrapping.SplitEmptyRecord = false; 11990 11991 verifyFormat("class Foo\n" 11992 "{};", 11993 Style); 11994 verifyFormat("/* something */ class Foo\n" 11995 "{};", 11996 Style); 11997 verifyFormat("template <typename X> class Foo\n" 11998 "{};", 11999 Style); 12000 verifyFormat("class Foo\n" 12001 "{\n" 12002 " Foo();\n" 12003 "};", 12004 Style); 12005 verifyFormat("typedef class Foo\n" 12006 "{\n" 12007 "} Foo_t;", 12008 Style); 12009 12010 Style.BraceWrapping.SplitEmptyRecord = true; 12011 Style.BraceWrapping.AfterStruct = true; 12012 verifyFormat("class rep\n" 12013 "{\n" 12014 "};", 12015 Style); 12016 verifyFormat("struct rep\n" 12017 "{\n" 12018 "};", 12019 Style); 12020 verifyFormat("template <typename T> class rep\n" 12021 "{\n" 12022 "};", 12023 Style); 12024 verifyFormat("template <typename T> struct rep\n" 12025 "{\n" 12026 "};", 12027 Style); 12028 verifyFormat("class rep\n" 12029 "{\n" 12030 " int x;\n" 12031 "};", 12032 Style); 12033 verifyFormat("struct rep\n" 12034 "{\n" 12035 " int x;\n" 12036 "};", 12037 Style); 12038 verifyFormat("template <typename T> class rep\n" 12039 "{\n" 12040 " int x;\n" 12041 "};", 12042 Style); 12043 verifyFormat("template <typename T> struct rep\n" 12044 "{\n" 12045 " int x;\n" 12046 "};", 12047 Style); 12048 verifyFormat("template <typename T> class rep // Foo\n" 12049 "{\n" 12050 " int x;\n" 12051 "};", 12052 Style); 12053 verifyFormat("template <typename T> struct rep // Bar\n" 12054 "{\n" 12055 " int x;\n" 12056 "};", 12057 Style); 12058 12059 verifyFormat("template <typename T> class rep<T>\n" 12060 "{\n" 12061 " int x;\n" 12062 "};", 12063 Style); 12064 12065 verifyFormat("template <typename T> class rep<std::complex<T>>\n" 12066 "{\n" 12067 " int x;\n" 12068 "};", 12069 Style); 12070 verifyFormat("template <typename T> class rep<std::complex<T>>\n" 12071 "{\n" 12072 "};", 12073 Style); 12074 12075 verifyFormat("#include \"stdint.h\"\n" 12076 "namespace rep {}", 12077 Style); 12078 verifyFormat("#include <stdint.h>\n" 12079 "namespace rep {}", 12080 Style); 12081 verifyFormat("#include <stdint.h>\n" 12082 "namespace rep {}", 12083 "#include <stdint.h>\n" 12084 "namespace rep {\n" 12085 "\n" 12086 "\n" 12087 "}", 12088 Style); 12089 } 12090 12091 TEST_F(FormatTest, SplitEmptyStruct) { 12092 FormatStyle Style = getLLVMStyle(); 12093 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12094 Style.BraceWrapping.AfterStruct = true; 12095 Style.BraceWrapping.SplitEmptyRecord = false; 12096 12097 verifyFormat("struct Foo\n" 12098 "{};", 12099 Style); 12100 verifyFormat("/* something */ struct Foo\n" 12101 "{};", 12102 Style); 12103 verifyFormat("template <typename X> struct Foo\n" 12104 "{};", 12105 Style); 12106 verifyFormat("struct Foo\n" 12107 "{\n" 12108 " Foo();\n" 12109 "};", 12110 Style); 12111 verifyFormat("typedef struct Foo\n" 12112 "{\n" 12113 "} Foo_t;", 12114 Style); 12115 // typedef struct Bar {} Bar_t; 12116 } 12117 12118 TEST_F(FormatTest, SplitEmptyUnion) { 12119 FormatStyle Style = getLLVMStyle(); 12120 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12121 Style.BraceWrapping.AfterUnion = true; 12122 Style.BraceWrapping.SplitEmptyRecord = false; 12123 12124 verifyFormat("union Foo\n" 12125 "{};", 12126 Style); 12127 verifyFormat("/* something */ union Foo\n" 12128 "{};", 12129 Style); 12130 verifyFormat("union Foo\n" 12131 "{\n" 12132 " A,\n" 12133 "};", 12134 Style); 12135 verifyFormat("typedef union Foo\n" 12136 "{\n" 12137 "} Foo_t;", 12138 Style); 12139 } 12140 12141 TEST_F(FormatTest, SplitEmptyNamespace) { 12142 FormatStyle Style = getLLVMStyle(); 12143 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12144 Style.BraceWrapping.AfterNamespace = true; 12145 Style.BraceWrapping.SplitEmptyNamespace = false; 12146 12147 verifyFormat("namespace Foo\n" 12148 "{};", 12149 Style); 12150 verifyFormat("/* something */ namespace Foo\n" 12151 "{};", 12152 Style); 12153 verifyFormat("inline namespace Foo\n" 12154 "{};", 12155 Style); 12156 verifyFormat("/* something */ inline namespace Foo\n" 12157 "{};", 12158 Style); 12159 verifyFormat("export namespace Foo\n" 12160 "{};", 12161 Style); 12162 verifyFormat("namespace Foo\n" 12163 "{\n" 12164 "void Bar();\n" 12165 "};", 12166 Style); 12167 } 12168 12169 TEST_F(FormatTest, NeverMergeShortRecords) { 12170 FormatStyle Style = getLLVMStyle(); 12171 12172 verifyFormat("class Foo {\n" 12173 " Foo();\n" 12174 "};", 12175 Style); 12176 verifyFormat("typedef class Foo {\n" 12177 " Foo();\n" 12178 "} Foo_t;", 12179 Style); 12180 verifyFormat("struct Foo {\n" 12181 " Foo();\n" 12182 "};", 12183 Style); 12184 verifyFormat("typedef struct Foo {\n" 12185 " Foo();\n" 12186 "} Foo_t;", 12187 Style); 12188 verifyFormat("union Foo {\n" 12189 " A,\n" 12190 "};", 12191 Style); 12192 verifyFormat("typedef union Foo {\n" 12193 " A,\n" 12194 "} Foo_t;", 12195 Style); 12196 verifyFormat("namespace Foo {\n" 12197 "void Bar();\n" 12198 "};", 12199 Style); 12200 12201 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12202 Style.BraceWrapping.AfterClass = true; 12203 Style.BraceWrapping.AfterStruct = true; 12204 Style.BraceWrapping.AfterUnion = true; 12205 Style.BraceWrapping.AfterNamespace = true; 12206 verifyFormat("class Foo\n" 12207 "{\n" 12208 " Foo();\n" 12209 "};", 12210 Style); 12211 verifyFormat("typedef class Foo\n" 12212 "{\n" 12213 " Foo();\n" 12214 "} Foo_t;", 12215 Style); 12216 verifyFormat("struct Foo\n" 12217 "{\n" 12218 " Foo();\n" 12219 "};", 12220 Style); 12221 verifyFormat("typedef struct Foo\n" 12222 "{\n" 12223 " Foo();\n" 12224 "} Foo_t;", 12225 Style); 12226 verifyFormat("union Foo\n" 12227 "{\n" 12228 " A,\n" 12229 "};", 12230 Style); 12231 verifyFormat("typedef union Foo\n" 12232 "{\n" 12233 " A,\n" 12234 "} Foo_t;", 12235 Style); 12236 verifyFormat("namespace Foo\n" 12237 "{\n" 12238 "void Bar();\n" 12239 "};", 12240 Style); 12241 } 12242 12243 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 12244 // Elaborate type variable declarations. 12245 verifyFormat("struct foo a = {bar};\nint n;"); 12246 verifyFormat("class foo a = {bar};\nint n;"); 12247 verifyFormat("union foo a = {bar};\nint n;"); 12248 12249 // Elaborate types inside function definitions. 12250 verifyFormat("struct foo f() {}\nint n;"); 12251 verifyFormat("class foo f() {}\nint n;"); 12252 verifyFormat("union foo f() {}\nint n;"); 12253 12254 // Templates. 12255 verifyFormat("template <class X> void f() {}\nint n;"); 12256 verifyFormat("template <struct X> void f() {}\nint n;"); 12257 verifyFormat("template <union X> void f() {}\nint n;"); 12258 12259 // Actual definitions... 12260 verifyFormat("struct {\n} n;"); 12261 verifyFormat( 12262 "template <template <class T, class Y>, class Z> class X {\n} n;"); 12263 verifyFormat("union Z {\n int n;\n} x;"); 12264 verifyFormat("class MACRO Z {\n} n;"); 12265 verifyFormat("class MACRO(X) Z {\n} n;"); 12266 verifyFormat("class __attribute__(X) Z {\n} n;"); 12267 verifyFormat("class __declspec(X) Z {\n} n;"); 12268 verifyFormat("class A##B##C {\n} n;"); 12269 verifyFormat("class alignas(16) Z {\n} n;"); 12270 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 12271 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 12272 12273 // Redefinition from nested context: 12274 verifyFormat("class A::B::C {\n} n;"); 12275 12276 // Template definitions. 12277 verifyFormat( 12278 "template <typename F>\n" 12279 "Matcher(const Matcher<F> &Other,\n" 12280 " typename enable_if_c<is_base_of<F, T>::value &&\n" 12281 " !is_same<F, T>::value>::type * = 0)\n" 12282 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 12283 12284 // FIXME: This is still incorrectly handled at the formatter side. 12285 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 12286 verifyFormat("int i = SomeFunction(a<b, a> b);"); 12287 12288 // FIXME: 12289 // This now gets parsed incorrectly as class definition. 12290 // verifyFormat("class A<int> f() {\n}\nint n;"); 12291 12292 // Elaborate types where incorrectly parsing the structural element would 12293 // break the indent. 12294 verifyFormat("if (true)\n" 12295 " class X x;\n" 12296 "else\n" 12297 " f();\n"); 12298 12299 // This is simply incomplete. Formatting is not important, but must not crash. 12300 verifyFormat("class A:"); 12301 } 12302 12303 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 12304 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 12305 format("#error Leave all white!!!!! space* alone!\n")); 12306 EXPECT_EQ( 12307 "#warning Leave all white!!!!! space* alone!\n", 12308 format("#warning Leave all white!!!!! space* alone!\n")); 12309 EXPECT_EQ("#error 1", format(" # error 1")); 12310 EXPECT_EQ("#warning 1", format(" # warning 1")); 12311 } 12312 12313 TEST_F(FormatTest, FormatHashIfExpressions) { 12314 verifyFormat("#if AAAA && BBBB"); 12315 verifyFormat("#if (AAAA && BBBB)"); 12316 verifyFormat("#elif (AAAA && BBBB)"); 12317 // FIXME: Come up with a better indentation for #elif. 12318 verifyFormat( 12319 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 12320 " defined(BBBBBBBB)\n" 12321 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 12322 " defined(BBBBBBBB)\n" 12323 "#endif", 12324 getLLVMStyleWithColumns(65)); 12325 } 12326 12327 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 12328 FormatStyle AllowsMergedIf = getGoogleStyle(); 12329 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 12330 FormatStyle::SIS_WithoutElse; 12331 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 12332 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 12333 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 12334 EXPECT_EQ("if (true) return 42;", 12335 format("if (true)\nreturn 42;", AllowsMergedIf)); 12336 FormatStyle ShortMergedIf = AllowsMergedIf; 12337 ShortMergedIf.ColumnLimit = 25; 12338 verifyFormat("#define A \\\n" 12339 " if (true) return 42;", 12340 ShortMergedIf); 12341 verifyFormat("#define A \\\n" 12342 " f(); \\\n" 12343 " if (true)\n" 12344 "#define B", 12345 ShortMergedIf); 12346 verifyFormat("#define A \\\n" 12347 " f(); \\\n" 12348 " if (true)\n" 12349 "g();", 12350 ShortMergedIf); 12351 verifyFormat("{\n" 12352 "#ifdef A\n" 12353 " // Comment\n" 12354 " if (true) continue;\n" 12355 "#endif\n" 12356 " // Comment\n" 12357 " if (true) continue;\n" 12358 "}", 12359 ShortMergedIf); 12360 ShortMergedIf.ColumnLimit = 33; 12361 verifyFormat("#define A \\\n" 12362 " if constexpr (true) return 42;", 12363 ShortMergedIf); 12364 verifyFormat("#define A \\\n" 12365 " if CONSTEXPR (true) return 42;", 12366 ShortMergedIf); 12367 ShortMergedIf.ColumnLimit = 29; 12368 verifyFormat("#define A \\\n" 12369 " if (aaaaaaaaaa) return 1; \\\n" 12370 " return 2;", 12371 ShortMergedIf); 12372 ShortMergedIf.ColumnLimit = 28; 12373 verifyFormat("#define A \\\n" 12374 " if (aaaaaaaaaa) \\\n" 12375 " return 1; \\\n" 12376 " return 2;", 12377 ShortMergedIf); 12378 verifyFormat("#define A \\\n" 12379 " if constexpr (aaaaaaa) \\\n" 12380 " return 1; \\\n" 12381 " return 2;", 12382 ShortMergedIf); 12383 verifyFormat("#define A \\\n" 12384 " if CONSTEXPR (aaaaaaa) \\\n" 12385 " return 1; \\\n" 12386 " return 2;", 12387 ShortMergedIf); 12388 } 12389 12390 TEST_F(FormatTest, FormatStarDependingOnContext) { 12391 verifyFormat("void f(int *a);"); 12392 verifyFormat("void f() { f(fint * b); }"); 12393 verifyFormat("class A {\n void f(int *a);\n};"); 12394 verifyFormat("class A {\n int *a;\n};"); 12395 verifyFormat("namespace a {\n" 12396 "namespace b {\n" 12397 "class A {\n" 12398 " void f() {}\n" 12399 " int *a;\n" 12400 "};\n" 12401 "} // namespace b\n" 12402 "} // namespace a"); 12403 } 12404 12405 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 12406 verifyFormat("while"); 12407 verifyFormat("operator"); 12408 } 12409 12410 TEST_F(FormatTest, SkipsDeeplyNestedLines) { 12411 // This code would be painfully slow to format if we didn't skip it. 12412 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 12413 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12414 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12415 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12416 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12417 "A(1, 1)\n" 12418 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x 12419 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12420 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12421 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12422 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12423 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12424 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12425 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12426 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12427 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n"); 12428 // Deeply nested part is untouched, rest is formatted. 12429 EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n", 12430 format(std::string("int i;\n") + Code + "int j;\n", 12431 getLLVMStyle(), SC_ExpectIncomplete)); 12432 } 12433 12434 //===----------------------------------------------------------------------===// 12435 // Objective-C tests. 12436 //===----------------------------------------------------------------------===// 12437 12438 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 12439 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 12440 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 12441 format("-(NSUInteger)indexOfObject:(id)anObject;")); 12442 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 12443 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 12444 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 12445 format("-(NSInteger)Method3:(id)anObject;")); 12446 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 12447 format("-(NSInteger)Method4:(id)anObject;")); 12448 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 12449 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 12450 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 12451 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 12452 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 12453 "forAllCells:(BOOL)flag;", 12454 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 12455 "forAllCells:(BOOL)flag;")); 12456 12457 // Very long objectiveC method declaration. 12458 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 12459 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 12460 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 12461 " inRange:(NSRange)range\n" 12462 " outRange:(NSRange)out_range\n" 12463 " outRange1:(NSRange)out_range1\n" 12464 " outRange2:(NSRange)out_range2\n" 12465 " outRange3:(NSRange)out_range3\n" 12466 " outRange4:(NSRange)out_range4\n" 12467 " outRange5:(NSRange)out_range5\n" 12468 " outRange6:(NSRange)out_range6\n" 12469 " outRange7:(NSRange)out_range7\n" 12470 " outRange8:(NSRange)out_range8\n" 12471 " outRange9:(NSRange)out_range9;"); 12472 12473 // When the function name has to be wrapped. 12474 FormatStyle Style = getLLVMStyle(); 12475 // ObjC ignores IndentWrappedFunctionNames when wrapping methods 12476 // and always indents instead. 12477 Style.IndentWrappedFunctionNames = false; 12478 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 12479 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 12480 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 12481 "}", 12482 Style); 12483 Style.IndentWrappedFunctionNames = true; 12484 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 12485 " veryLooooooooooongName:(NSString)cccccccccccccc\n" 12486 " anotherName:(NSString)dddddddddddddd {\n" 12487 "}", 12488 Style); 12489 12490 verifyFormat("- (int)sum:(vector<int>)numbers;"); 12491 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 12492 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 12493 // protocol lists (but not for template classes): 12494 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 12495 12496 verifyFormat("- (int (*)())foo:(int (*)())f;"); 12497 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 12498 12499 // If there's no return type (very rare in practice!), LLVM and Google style 12500 // agree. 12501 verifyFormat("- foo;"); 12502 verifyFormat("- foo:(int)f;"); 12503 verifyGoogleFormat("- foo:(int)foo;"); 12504 } 12505 12506 TEST_F(FormatTest, BreaksStringLiterals) { 12507 EXPECT_EQ("\"some text \"\n" 12508 "\"other\";", 12509 format("\"some text other\";", getLLVMStyleWithColumns(12))); 12510 EXPECT_EQ("\"some text \"\n" 12511 "\"other\";", 12512 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 12513 EXPECT_EQ( 12514 "#define A \\\n" 12515 " \"some \" \\\n" 12516 " \"text \" \\\n" 12517 " \"other\";", 12518 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 12519 EXPECT_EQ( 12520 "#define A \\\n" 12521 " \"so \" \\\n" 12522 " \"text \" \\\n" 12523 " \"other\";", 12524 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 12525 12526 EXPECT_EQ("\"some text\"", 12527 format("\"some text\"", getLLVMStyleWithColumns(1))); 12528 EXPECT_EQ("\"some text\"", 12529 format("\"some text\"", getLLVMStyleWithColumns(11))); 12530 EXPECT_EQ("\"some \"\n" 12531 "\"text\"", 12532 format("\"some text\"", getLLVMStyleWithColumns(10))); 12533 EXPECT_EQ("\"some \"\n" 12534 "\"text\"", 12535 format("\"some text\"", getLLVMStyleWithColumns(7))); 12536 EXPECT_EQ("\"some\"\n" 12537 "\" tex\"\n" 12538 "\"t\"", 12539 format("\"some text\"", getLLVMStyleWithColumns(6))); 12540 EXPECT_EQ("\"some\"\n" 12541 "\" tex\"\n" 12542 "\" and\"", 12543 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 12544 EXPECT_EQ("\"some\"\n" 12545 "\"/tex\"\n" 12546 "\"/and\"", 12547 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 12548 12549 EXPECT_EQ("variable =\n" 12550 " \"long string \"\n" 12551 " \"literal\";", 12552 format("variable = \"long string literal\";", 12553 getLLVMStyleWithColumns(20))); 12554 12555 EXPECT_EQ("variable = f(\n" 12556 " \"long string \"\n" 12557 " \"literal\",\n" 12558 " short,\n" 12559 " loooooooooooooooooooong);", 12560 format("variable = f(\"long string literal\", short, " 12561 "loooooooooooooooooooong);", 12562 getLLVMStyleWithColumns(20))); 12563 12564 EXPECT_EQ( 12565 "f(g(\"long string \"\n" 12566 " \"literal\"),\n" 12567 " b);", 12568 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 12569 EXPECT_EQ("f(g(\"long string \"\n" 12570 " \"literal\",\n" 12571 " a),\n" 12572 " b);", 12573 format("f(g(\"long string literal\", a), b);", 12574 getLLVMStyleWithColumns(20))); 12575 EXPECT_EQ( 12576 "f(\"one two\".split(\n" 12577 " variable));", 12578 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 12579 EXPECT_EQ("f(\"one two three four five six \"\n" 12580 " \"seven\".split(\n" 12581 " really_looooong_variable));", 12582 format("f(\"one two three four five six seven\"." 12583 "split(really_looooong_variable));", 12584 getLLVMStyleWithColumns(33))); 12585 12586 EXPECT_EQ("f(\"some \"\n" 12587 " \"text\",\n" 12588 " other);", 12589 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 12590 12591 // Only break as a last resort. 12592 verifyFormat( 12593 "aaaaaaaaaaaaaaaaaaaa(\n" 12594 " aaaaaaaaaaaaaaaaaaaa,\n" 12595 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 12596 12597 EXPECT_EQ("\"splitmea\"\n" 12598 "\"trandomp\"\n" 12599 "\"oint\"", 12600 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 12601 12602 EXPECT_EQ("\"split/\"\n" 12603 "\"pathat/\"\n" 12604 "\"slashes\"", 12605 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 12606 12607 EXPECT_EQ("\"split/\"\n" 12608 "\"pathat/\"\n" 12609 "\"slashes\"", 12610 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 12611 EXPECT_EQ("\"split at \"\n" 12612 "\"spaces/at/\"\n" 12613 "\"slashes.at.any$\"\n" 12614 "\"non-alphanumeric%\"\n" 12615 "\"1111111111characte\"\n" 12616 "\"rs\"", 12617 format("\"split at " 12618 "spaces/at/" 12619 "slashes.at." 12620 "any$non-" 12621 "alphanumeric%" 12622 "1111111111characte" 12623 "rs\"", 12624 getLLVMStyleWithColumns(20))); 12625 12626 // Verify that splitting the strings understands 12627 // Style::AlwaysBreakBeforeMultilineStrings. 12628 EXPECT_EQ("aaaaaaaaaaaa(\n" 12629 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 12630 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 12631 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 12632 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 12633 "aaaaaaaaaaaaaaaaaaaaaa\");", 12634 getGoogleStyle())); 12635 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 12636 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 12637 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 12638 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 12639 "aaaaaaaaaaaaaaaaaaaaaa\";", 12640 getGoogleStyle())); 12641 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 12642 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 12643 format("llvm::outs() << " 12644 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 12645 "aaaaaaaaaaaaaaaaaaa\";")); 12646 EXPECT_EQ("ffff(\n" 12647 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 12648 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 12649 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 12650 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 12651 getGoogleStyle())); 12652 12653 FormatStyle Style = getLLVMStyleWithColumns(12); 12654 Style.BreakStringLiterals = false; 12655 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 12656 12657 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 12658 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 12659 EXPECT_EQ("#define A \\\n" 12660 " \"some \" \\\n" 12661 " \"text \" \\\n" 12662 " \"other\";", 12663 format("#define A \"some text other\";", AlignLeft)); 12664 } 12665 12666 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) { 12667 EXPECT_EQ("C a = \"some more \"\n" 12668 " \"text\";", 12669 format("C a = \"some more text\";", getLLVMStyleWithColumns(18))); 12670 } 12671 12672 TEST_F(FormatTest, FullyRemoveEmptyLines) { 12673 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 12674 NoEmptyLines.MaxEmptyLinesToKeep = 0; 12675 EXPECT_EQ("int i = a(b());", 12676 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 12677 } 12678 12679 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 12680 EXPECT_EQ( 12681 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 12682 "(\n" 12683 " \"x\t\");", 12684 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 12685 "aaaaaaa(" 12686 "\"x\t\");")); 12687 } 12688 12689 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 12690 EXPECT_EQ( 12691 "u8\"utf8 string \"\n" 12692 "u8\"literal\";", 12693 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 12694 EXPECT_EQ( 12695 "u\"utf16 string \"\n" 12696 "u\"literal\";", 12697 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 12698 EXPECT_EQ( 12699 "U\"utf32 string \"\n" 12700 "U\"literal\";", 12701 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 12702 EXPECT_EQ("L\"wide string \"\n" 12703 "L\"literal\";", 12704 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 12705 EXPECT_EQ("@\"NSString \"\n" 12706 "@\"literal\";", 12707 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 12708 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26)); 12709 12710 // This input makes clang-format try to split the incomplete unicode escape 12711 // sequence, which used to lead to a crasher. 12712 verifyNoCrash( 12713 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 12714 getLLVMStyleWithColumns(60)); 12715 } 12716 12717 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 12718 FormatStyle Style = getGoogleStyleWithColumns(15); 12719 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 12720 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 12721 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 12722 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 12723 EXPECT_EQ("u8R\"x(raw literal)x\";", 12724 format("u8R\"x(raw literal)x\";", Style)); 12725 } 12726 12727 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 12728 FormatStyle Style = getLLVMStyleWithColumns(20); 12729 EXPECT_EQ( 12730 "_T(\"aaaaaaaaaaaaaa\")\n" 12731 "_T(\"aaaaaaaaaaaaaa\")\n" 12732 "_T(\"aaaaaaaaaaaa\")", 12733 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 12734 EXPECT_EQ("f(x,\n" 12735 " _T(\"aaaaaaaaaaaa\")\n" 12736 " _T(\"aaa\"),\n" 12737 " z);", 12738 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 12739 12740 // FIXME: Handle embedded spaces in one iteration. 12741 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 12742 // "_T(\"aaaaaaaaaaaaa\")\n" 12743 // "_T(\"aaaaaaaaaaaaa\")\n" 12744 // "_T(\"a\")", 12745 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 12746 // getLLVMStyleWithColumns(20))); 12747 EXPECT_EQ( 12748 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 12749 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 12750 EXPECT_EQ("f(\n" 12751 "#if !TEST\n" 12752 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 12753 "#endif\n" 12754 ");", 12755 format("f(\n" 12756 "#if !TEST\n" 12757 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 12758 "#endif\n" 12759 ");")); 12760 EXPECT_EQ("f(\n" 12761 "\n" 12762 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 12763 format("f(\n" 12764 "\n" 12765 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 12766 } 12767 12768 TEST_F(FormatTest, BreaksStringLiteralOperands) { 12769 // In a function call with two operands, the second can be broken with no line 12770 // break before it. 12771 EXPECT_EQ( 12772 "func(a, \"long long \"\n" 12773 " \"long long\");", 12774 format("func(a, \"long long long long\");", getLLVMStyleWithColumns(24))); 12775 // In a function call with three operands, the second must be broken with a 12776 // line break before it. 12777 EXPECT_EQ("func(a,\n" 12778 " \"long long long \"\n" 12779 " \"long\",\n" 12780 " c);", 12781 format("func(a, \"long long long long\", c);", 12782 getLLVMStyleWithColumns(24))); 12783 // In a function call with three operands, the third must be broken with a 12784 // line break before it. 12785 EXPECT_EQ("func(a, b,\n" 12786 " \"long long long \"\n" 12787 " \"long\");", 12788 format("func(a, b, \"long long long long\");", 12789 getLLVMStyleWithColumns(24))); 12790 // In a function call with three operands, both the second and the third must 12791 // be broken with a line break before them. 12792 EXPECT_EQ("func(a,\n" 12793 " \"long long long \"\n" 12794 " \"long\",\n" 12795 " \"long long long \"\n" 12796 " \"long\");", 12797 format("func(a, \"long long long long\", \"long long long long\");", 12798 getLLVMStyleWithColumns(24))); 12799 // In a chain of << with two operands, the second can be broken with no line 12800 // break before it. 12801 EXPECT_EQ("a << \"line line \"\n" 12802 " \"line\";", 12803 format("a << \"line line line\";", getLLVMStyleWithColumns(20))); 12804 // In a chain of << with three operands, the second can be broken with no line 12805 // break before it. 12806 EXPECT_EQ( 12807 "abcde << \"line \"\n" 12808 " \"line line\"\n" 12809 " << c;", 12810 format("abcde << \"line line line\" << c;", getLLVMStyleWithColumns(20))); 12811 // In a chain of << with three operands, the third must be broken with a line 12812 // break before it. 12813 EXPECT_EQ( 12814 "a << b\n" 12815 " << \"line line \"\n" 12816 " \"line\";", 12817 format("a << b << \"line line line\";", getLLVMStyleWithColumns(20))); 12818 // In a chain of << with three operands, the second can be broken with no line 12819 // break before it and the third must be broken with a line break before it. 12820 EXPECT_EQ("abcd << \"line line \"\n" 12821 " \"line\"\n" 12822 " << \"line line \"\n" 12823 " \"line\";", 12824 format("abcd << \"line line line\" << \"line line line\";", 12825 getLLVMStyleWithColumns(20))); 12826 // In a chain of binary operators with two operands, the second can be broken 12827 // with no line break before it. 12828 EXPECT_EQ( 12829 "abcd + \"line line \"\n" 12830 " \"line line\";", 12831 format("abcd + \"line line line line\";", getLLVMStyleWithColumns(20))); 12832 // In a chain of binary operators with three operands, the second must be 12833 // broken with a line break before it. 12834 EXPECT_EQ("abcd +\n" 12835 " \"line line \"\n" 12836 " \"line line\" +\n" 12837 " e;", 12838 format("abcd + \"line line line line\" + e;", 12839 getLLVMStyleWithColumns(20))); 12840 // In a function call with two operands, with AlignAfterOpenBracket enabled, 12841 // the first must be broken with a line break before it. 12842 FormatStyle Style = getLLVMStyleWithColumns(25); 12843 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 12844 EXPECT_EQ("someFunction(\n" 12845 " \"long long long \"\n" 12846 " \"long\",\n" 12847 " a);", 12848 format("someFunction(\"long long long long\", a);", Style)); 12849 } 12850 12851 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 12852 EXPECT_EQ( 12853 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 12854 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 12855 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 12856 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 12857 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 12858 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 12859 } 12860 12861 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 12862 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 12863 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 12864 EXPECT_EQ("fffffffffff(g(R\"x(\n" 12865 "multiline raw string literal xxxxxxxxxxxxxx\n" 12866 ")x\",\n" 12867 " a),\n" 12868 " b);", 12869 format("fffffffffff(g(R\"x(\n" 12870 "multiline raw string literal xxxxxxxxxxxxxx\n" 12871 ")x\", a), b);", 12872 getGoogleStyleWithColumns(20))); 12873 EXPECT_EQ("fffffffffff(\n" 12874 " g(R\"x(qqq\n" 12875 "multiline raw string literal xxxxxxxxxxxxxx\n" 12876 ")x\",\n" 12877 " a),\n" 12878 " b);", 12879 format("fffffffffff(g(R\"x(qqq\n" 12880 "multiline raw string literal xxxxxxxxxxxxxx\n" 12881 ")x\", a), b);", 12882 getGoogleStyleWithColumns(20))); 12883 12884 EXPECT_EQ("fffffffffff(R\"x(\n" 12885 "multiline raw string literal xxxxxxxxxxxxxx\n" 12886 ")x\");", 12887 format("fffffffffff(R\"x(\n" 12888 "multiline raw string literal xxxxxxxxxxxxxx\n" 12889 ")x\");", 12890 getGoogleStyleWithColumns(20))); 12891 EXPECT_EQ("fffffffffff(R\"x(\n" 12892 "multiline raw string literal xxxxxxxxxxxxxx\n" 12893 ")x\" + bbbbbb);", 12894 format("fffffffffff(R\"x(\n" 12895 "multiline raw string literal xxxxxxxxxxxxxx\n" 12896 ")x\" + bbbbbb);", 12897 getGoogleStyleWithColumns(20))); 12898 EXPECT_EQ("fffffffffff(\n" 12899 " R\"x(\n" 12900 "multiline raw string literal xxxxxxxxxxxxxx\n" 12901 ")x\" +\n" 12902 " bbbbbb);", 12903 format("fffffffffff(\n" 12904 " R\"x(\n" 12905 "multiline raw string literal xxxxxxxxxxxxxx\n" 12906 ")x\" + bbbbbb);", 12907 getGoogleStyleWithColumns(20))); 12908 EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);", 12909 format("fffffffffff(\n" 12910 " R\"(single line raw string)\" + bbbbbb);")); 12911 } 12912 12913 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 12914 verifyFormat("string a = \"unterminated;"); 12915 EXPECT_EQ("function(\"unterminated,\n" 12916 " OtherParameter);", 12917 format("function( \"unterminated,\n" 12918 " OtherParameter);")); 12919 } 12920 12921 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 12922 FormatStyle Style = getLLVMStyle(); 12923 Style.Standard = FormatStyle::LS_Cpp03; 12924 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 12925 format("#define x(_a) printf(\"foo\"_a);", Style)); 12926 } 12927 12928 TEST_F(FormatTest, CppLexVersion) { 12929 FormatStyle Style = getLLVMStyle(); 12930 // Formatting of x * y differs if x is a type. 12931 verifyFormat("void foo() { MACRO(a * b); }", Style); 12932 verifyFormat("void foo() { MACRO(int *b); }", Style); 12933 12934 // LLVM style uses latest lexer. 12935 verifyFormat("void foo() { MACRO(char8_t *b); }", Style); 12936 Style.Standard = FormatStyle::LS_Cpp17; 12937 // But in c++17, char8_t isn't a keyword. 12938 verifyFormat("void foo() { MACRO(char8_t * b); }", Style); 12939 } 12940 12941 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 12942 12943 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 12944 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 12945 " \"ddeeefff\");", 12946 format("someFunction(\"aaabbbcccdddeeefff\");", 12947 getLLVMStyleWithColumns(25))); 12948 EXPECT_EQ("someFunction1234567890(\n" 12949 " \"aaabbbcccdddeeefff\");", 12950 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 12951 getLLVMStyleWithColumns(26))); 12952 EXPECT_EQ("someFunction1234567890(\n" 12953 " \"aaabbbcccdddeeeff\"\n" 12954 " \"f\");", 12955 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 12956 getLLVMStyleWithColumns(25))); 12957 EXPECT_EQ("someFunction1234567890(\n" 12958 " \"aaabbbcccdddeeeff\"\n" 12959 " \"f\");", 12960 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 12961 getLLVMStyleWithColumns(24))); 12962 EXPECT_EQ("someFunction(\n" 12963 " \"aaabbbcc ddde \"\n" 12964 " \"efff\");", 12965 format("someFunction(\"aaabbbcc ddde efff\");", 12966 getLLVMStyleWithColumns(25))); 12967 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 12968 " \"ddeeefff\");", 12969 format("someFunction(\"aaabbbccc ddeeefff\");", 12970 getLLVMStyleWithColumns(25))); 12971 EXPECT_EQ("someFunction1234567890(\n" 12972 " \"aaabb \"\n" 12973 " \"cccdddeeefff\");", 12974 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 12975 getLLVMStyleWithColumns(25))); 12976 EXPECT_EQ("#define A \\\n" 12977 " string s = \\\n" 12978 " \"123456789\" \\\n" 12979 " \"0\"; \\\n" 12980 " int i;", 12981 format("#define A string s = \"1234567890\"; int i;", 12982 getLLVMStyleWithColumns(20))); 12983 EXPECT_EQ("someFunction(\n" 12984 " \"aaabbbcc \"\n" 12985 " \"dddeeefff\");", 12986 format("someFunction(\"aaabbbcc dddeeefff\");", 12987 getLLVMStyleWithColumns(25))); 12988 } 12989 12990 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 12991 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 12992 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 12993 EXPECT_EQ("\"test\"\n" 12994 "\"\\n\"", 12995 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 12996 EXPECT_EQ("\"tes\\\\\"\n" 12997 "\"n\"", 12998 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 12999 EXPECT_EQ("\"\\\\\\\\\"\n" 13000 "\"\\n\"", 13001 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 13002 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 13003 EXPECT_EQ("\"\\uff01\"\n" 13004 "\"test\"", 13005 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 13006 EXPECT_EQ("\"\\Uff01ff02\"", 13007 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 13008 EXPECT_EQ("\"\\x000000000001\"\n" 13009 "\"next\"", 13010 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 13011 EXPECT_EQ("\"\\x000000000001next\"", 13012 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 13013 EXPECT_EQ("\"\\x000000000001\"", 13014 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 13015 EXPECT_EQ("\"test\"\n" 13016 "\"\\000000\"\n" 13017 "\"000001\"", 13018 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 13019 EXPECT_EQ("\"test\\000\"\n" 13020 "\"00000000\"\n" 13021 "\"1\"", 13022 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 13023 } 13024 13025 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 13026 verifyFormat("void f() {\n" 13027 " return g() {}\n" 13028 " void h() {}"); 13029 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 13030 "g();\n" 13031 "}"); 13032 } 13033 13034 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 13035 verifyFormat( 13036 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 13037 } 13038 13039 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 13040 verifyFormat("class X {\n" 13041 " void f() {\n" 13042 " }\n" 13043 "};", 13044 getLLVMStyleWithColumns(12)); 13045 } 13046 13047 TEST_F(FormatTest, ConfigurableIndentWidth) { 13048 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 13049 EightIndent.IndentWidth = 8; 13050 EightIndent.ContinuationIndentWidth = 8; 13051 verifyFormat("void f() {\n" 13052 " someFunction();\n" 13053 " if (true) {\n" 13054 " f();\n" 13055 " }\n" 13056 "}", 13057 EightIndent); 13058 verifyFormat("class X {\n" 13059 " void f() {\n" 13060 " }\n" 13061 "};", 13062 EightIndent); 13063 verifyFormat("int x[] = {\n" 13064 " call(),\n" 13065 " call()};", 13066 EightIndent); 13067 } 13068 13069 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 13070 verifyFormat("double\n" 13071 "f();", 13072 getLLVMStyleWithColumns(8)); 13073 } 13074 13075 TEST_F(FormatTest, ConfigurableUseOfTab) { 13076 FormatStyle Tab = getLLVMStyleWithColumns(42); 13077 Tab.IndentWidth = 8; 13078 Tab.UseTab = FormatStyle::UT_Always; 13079 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left; 13080 13081 EXPECT_EQ("if (aaaaaaaa && // q\n" 13082 " bb)\t\t// w\n" 13083 "\t;", 13084 format("if (aaaaaaaa &&// q\n" 13085 "bb)// w\n" 13086 ";", 13087 Tab)); 13088 EXPECT_EQ("if (aaa && bbb) // w\n" 13089 "\t;", 13090 format("if(aaa&&bbb)// w\n" 13091 ";", 13092 Tab)); 13093 13094 verifyFormat("class X {\n" 13095 "\tvoid f() {\n" 13096 "\t\tsomeFunction(parameter1,\n" 13097 "\t\t\t parameter2);\n" 13098 "\t}\n" 13099 "};", 13100 Tab); 13101 verifyFormat("#define A \\\n" 13102 "\tvoid f() { \\\n" 13103 "\t\tsomeFunction( \\\n" 13104 "\t\t parameter1, \\\n" 13105 "\t\t parameter2); \\\n" 13106 "\t}", 13107 Tab); 13108 verifyFormat("int a;\t // x\n" 13109 "int bbbbbbbb; // x\n", 13110 Tab); 13111 13112 Tab.TabWidth = 4; 13113 Tab.IndentWidth = 8; 13114 verifyFormat("class TabWidth4Indent8 {\n" 13115 "\t\tvoid f() {\n" 13116 "\t\t\t\tsomeFunction(parameter1,\n" 13117 "\t\t\t\t\t\t\t parameter2);\n" 13118 "\t\t}\n" 13119 "};", 13120 Tab); 13121 13122 Tab.TabWidth = 4; 13123 Tab.IndentWidth = 4; 13124 verifyFormat("class TabWidth4Indent4 {\n" 13125 "\tvoid f() {\n" 13126 "\t\tsomeFunction(parameter1,\n" 13127 "\t\t\t\t\t parameter2);\n" 13128 "\t}\n" 13129 "};", 13130 Tab); 13131 13132 Tab.TabWidth = 8; 13133 Tab.IndentWidth = 4; 13134 verifyFormat("class TabWidth8Indent4 {\n" 13135 " void f() {\n" 13136 "\tsomeFunction(parameter1,\n" 13137 "\t\t parameter2);\n" 13138 " }\n" 13139 "};", 13140 Tab); 13141 13142 Tab.TabWidth = 8; 13143 Tab.IndentWidth = 8; 13144 EXPECT_EQ("/*\n" 13145 "\t a\t\tcomment\n" 13146 "\t in multiple lines\n" 13147 " */", 13148 format(" /*\t \t \n" 13149 " \t \t a\t\tcomment\t \t\n" 13150 " \t \t in multiple lines\t\n" 13151 " \t */", 13152 Tab)); 13153 13154 Tab.UseTab = FormatStyle::UT_ForIndentation; 13155 verifyFormat("{\n" 13156 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13157 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13158 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13159 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13160 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13161 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13162 "};", 13163 Tab); 13164 verifyFormat("enum AA {\n" 13165 "\ta1, // Force multiple lines\n" 13166 "\ta2,\n" 13167 "\ta3\n" 13168 "};", 13169 Tab); 13170 EXPECT_EQ("if (aaaaaaaa && // q\n" 13171 " bb) // w\n" 13172 "\t;", 13173 format("if (aaaaaaaa &&// q\n" 13174 "bb)// w\n" 13175 ";", 13176 Tab)); 13177 verifyFormat("class X {\n" 13178 "\tvoid f() {\n" 13179 "\t\tsomeFunction(parameter1,\n" 13180 "\t\t parameter2);\n" 13181 "\t}\n" 13182 "};", 13183 Tab); 13184 verifyFormat("{\n" 13185 "\tQ(\n" 13186 "\t {\n" 13187 "\t\t int a;\n" 13188 "\t\t someFunction(aaaaaaaa,\n" 13189 "\t\t bbbbbbb);\n" 13190 "\t },\n" 13191 "\t p);\n" 13192 "}", 13193 Tab); 13194 EXPECT_EQ("{\n" 13195 "\t/* aaaa\n" 13196 "\t bbbb */\n" 13197 "}", 13198 format("{\n" 13199 "/* aaaa\n" 13200 " bbbb */\n" 13201 "}", 13202 Tab)); 13203 EXPECT_EQ("{\n" 13204 "\t/*\n" 13205 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13206 "\t bbbbbbbbbbbbb\n" 13207 "\t*/\n" 13208 "}", 13209 format("{\n" 13210 "/*\n" 13211 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13212 "*/\n" 13213 "}", 13214 Tab)); 13215 EXPECT_EQ("{\n" 13216 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13217 "\t// bbbbbbbbbbbbb\n" 13218 "}", 13219 format("{\n" 13220 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13221 "}", 13222 Tab)); 13223 EXPECT_EQ("{\n" 13224 "\t/*\n" 13225 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13226 "\t bbbbbbbbbbbbb\n" 13227 "\t*/\n" 13228 "}", 13229 format("{\n" 13230 "\t/*\n" 13231 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13232 "\t*/\n" 13233 "}", 13234 Tab)); 13235 EXPECT_EQ("{\n" 13236 "\t/*\n" 13237 "\n" 13238 "\t*/\n" 13239 "}", 13240 format("{\n" 13241 "\t/*\n" 13242 "\n" 13243 "\t*/\n" 13244 "}", 13245 Tab)); 13246 EXPECT_EQ("{\n" 13247 "\t/*\n" 13248 " asdf\n" 13249 "\t*/\n" 13250 "}", 13251 format("{\n" 13252 "\t/*\n" 13253 " asdf\n" 13254 "\t*/\n" 13255 "}", 13256 Tab)); 13257 13258 Tab.UseTab = FormatStyle::UT_Never; 13259 EXPECT_EQ("/*\n" 13260 " a\t\tcomment\n" 13261 " in multiple lines\n" 13262 " */", 13263 format(" /*\t \t \n" 13264 " \t \t a\t\tcomment\t \t\n" 13265 " \t \t in multiple lines\t\n" 13266 " \t */", 13267 Tab)); 13268 EXPECT_EQ("/* some\n" 13269 " comment */", 13270 format(" \t \t /* some\n" 13271 " \t \t comment */", 13272 Tab)); 13273 EXPECT_EQ("int a; /* some\n" 13274 " comment */", 13275 format(" \t \t int a; /* some\n" 13276 " \t \t comment */", 13277 Tab)); 13278 13279 EXPECT_EQ("int a; /* some\n" 13280 "comment */", 13281 format(" \t \t int\ta; /* some\n" 13282 " \t \t comment */", 13283 Tab)); 13284 EXPECT_EQ("f(\"\t\t\"); /* some\n" 13285 " comment */", 13286 format(" \t \t f(\"\t\t\"); /* some\n" 13287 " \t \t comment */", 13288 Tab)); 13289 EXPECT_EQ("{\n" 13290 " /*\n" 13291 " * Comment\n" 13292 " */\n" 13293 " int i;\n" 13294 "}", 13295 format("{\n" 13296 "\t/*\n" 13297 "\t * Comment\n" 13298 "\t */\n" 13299 "\t int i;\n" 13300 "}", 13301 Tab)); 13302 13303 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 13304 Tab.TabWidth = 8; 13305 Tab.IndentWidth = 8; 13306 EXPECT_EQ("if (aaaaaaaa && // q\n" 13307 " bb) // w\n" 13308 "\t;", 13309 format("if (aaaaaaaa &&// q\n" 13310 "bb)// w\n" 13311 ";", 13312 Tab)); 13313 EXPECT_EQ("if (aaa && bbb) // w\n" 13314 "\t;", 13315 format("if(aaa&&bbb)// w\n" 13316 ";", 13317 Tab)); 13318 verifyFormat("class X {\n" 13319 "\tvoid f() {\n" 13320 "\t\tsomeFunction(parameter1,\n" 13321 "\t\t\t parameter2);\n" 13322 "\t}\n" 13323 "};", 13324 Tab); 13325 verifyFormat("#define A \\\n" 13326 "\tvoid f() { \\\n" 13327 "\t\tsomeFunction( \\\n" 13328 "\t\t parameter1, \\\n" 13329 "\t\t parameter2); \\\n" 13330 "\t}", 13331 Tab); 13332 Tab.TabWidth = 4; 13333 Tab.IndentWidth = 8; 13334 verifyFormat("class TabWidth4Indent8 {\n" 13335 "\t\tvoid f() {\n" 13336 "\t\t\t\tsomeFunction(parameter1,\n" 13337 "\t\t\t\t\t\t\t parameter2);\n" 13338 "\t\t}\n" 13339 "};", 13340 Tab); 13341 Tab.TabWidth = 4; 13342 Tab.IndentWidth = 4; 13343 verifyFormat("class TabWidth4Indent4 {\n" 13344 "\tvoid f() {\n" 13345 "\t\tsomeFunction(parameter1,\n" 13346 "\t\t\t\t\t parameter2);\n" 13347 "\t}\n" 13348 "};", 13349 Tab); 13350 Tab.TabWidth = 8; 13351 Tab.IndentWidth = 4; 13352 verifyFormat("class TabWidth8Indent4 {\n" 13353 " void f() {\n" 13354 "\tsomeFunction(parameter1,\n" 13355 "\t\t parameter2);\n" 13356 " }\n" 13357 "};", 13358 Tab); 13359 Tab.TabWidth = 8; 13360 Tab.IndentWidth = 8; 13361 EXPECT_EQ("/*\n" 13362 "\t a\t\tcomment\n" 13363 "\t in multiple lines\n" 13364 " */", 13365 format(" /*\t \t \n" 13366 " \t \t a\t\tcomment\t \t\n" 13367 " \t \t in multiple lines\t\n" 13368 " \t */", 13369 Tab)); 13370 verifyFormat("{\n" 13371 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13372 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13373 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13374 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13375 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13376 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13377 "};", 13378 Tab); 13379 verifyFormat("enum AA {\n" 13380 "\ta1, // Force multiple lines\n" 13381 "\ta2,\n" 13382 "\ta3\n" 13383 "};", 13384 Tab); 13385 EXPECT_EQ("if (aaaaaaaa && // q\n" 13386 " bb) // w\n" 13387 "\t;", 13388 format("if (aaaaaaaa &&// q\n" 13389 "bb)// w\n" 13390 ";", 13391 Tab)); 13392 verifyFormat("class X {\n" 13393 "\tvoid f() {\n" 13394 "\t\tsomeFunction(parameter1,\n" 13395 "\t\t\t parameter2);\n" 13396 "\t}\n" 13397 "};", 13398 Tab); 13399 verifyFormat("{\n" 13400 "\tQ(\n" 13401 "\t {\n" 13402 "\t\t int a;\n" 13403 "\t\t someFunction(aaaaaaaa,\n" 13404 "\t\t\t\t bbbbbbb);\n" 13405 "\t },\n" 13406 "\t p);\n" 13407 "}", 13408 Tab); 13409 EXPECT_EQ("{\n" 13410 "\t/* aaaa\n" 13411 "\t bbbb */\n" 13412 "}", 13413 format("{\n" 13414 "/* aaaa\n" 13415 " bbbb */\n" 13416 "}", 13417 Tab)); 13418 EXPECT_EQ("{\n" 13419 "\t/*\n" 13420 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13421 "\t bbbbbbbbbbbbb\n" 13422 "\t*/\n" 13423 "}", 13424 format("{\n" 13425 "/*\n" 13426 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13427 "*/\n" 13428 "}", 13429 Tab)); 13430 EXPECT_EQ("{\n" 13431 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13432 "\t// bbbbbbbbbbbbb\n" 13433 "}", 13434 format("{\n" 13435 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13436 "}", 13437 Tab)); 13438 EXPECT_EQ("{\n" 13439 "\t/*\n" 13440 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13441 "\t bbbbbbbbbbbbb\n" 13442 "\t*/\n" 13443 "}", 13444 format("{\n" 13445 "\t/*\n" 13446 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13447 "\t*/\n" 13448 "}", 13449 Tab)); 13450 EXPECT_EQ("{\n" 13451 "\t/*\n" 13452 "\n" 13453 "\t*/\n" 13454 "}", 13455 format("{\n" 13456 "\t/*\n" 13457 "\n" 13458 "\t*/\n" 13459 "}", 13460 Tab)); 13461 EXPECT_EQ("{\n" 13462 "\t/*\n" 13463 " asdf\n" 13464 "\t*/\n" 13465 "}", 13466 format("{\n" 13467 "\t/*\n" 13468 " asdf\n" 13469 "\t*/\n" 13470 "}", 13471 Tab)); 13472 EXPECT_EQ("/* some\n" 13473 " comment */", 13474 format(" \t \t /* some\n" 13475 " \t \t comment */", 13476 Tab)); 13477 EXPECT_EQ("int a; /* some\n" 13478 " comment */", 13479 format(" \t \t int a; /* some\n" 13480 " \t \t comment */", 13481 Tab)); 13482 EXPECT_EQ("int a; /* some\n" 13483 "comment */", 13484 format(" \t \t int\ta; /* some\n" 13485 " \t \t comment */", 13486 Tab)); 13487 EXPECT_EQ("f(\"\t\t\"); /* some\n" 13488 " comment */", 13489 format(" \t \t f(\"\t\t\"); /* some\n" 13490 " \t \t comment */", 13491 Tab)); 13492 EXPECT_EQ("{\n" 13493 "\t/*\n" 13494 "\t * Comment\n" 13495 "\t */\n" 13496 "\tint i;\n" 13497 "}", 13498 format("{\n" 13499 "\t/*\n" 13500 "\t * Comment\n" 13501 "\t */\n" 13502 "\t int i;\n" 13503 "}", 13504 Tab)); 13505 Tab.TabWidth = 2; 13506 Tab.IndentWidth = 2; 13507 EXPECT_EQ("{\n" 13508 "\t/* aaaa\n" 13509 "\t\t bbbb */\n" 13510 "}", 13511 format("{\n" 13512 "/* aaaa\n" 13513 "\t bbbb */\n" 13514 "}", 13515 Tab)); 13516 EXPECT_EQ("{\n" 13517 "\t/*\n" 13518 "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13519 "\t\tbbbbbbbbbbbbb\n" 13520 "\t*/\n" 13521 "}", 13522 format("{\n" 13523 "/*\n" 13524 "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13525 "*/\n" 13526 "}", 13527 Tab)); 13528 Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 13529 Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 13530 Tab.TabWidth = 4; 13531 Tab.IndentWidth = 4; 13532 verifyFormat("class Assign {\n" 13533 "\tvoid f() {\n" 13534 "\t\tint x = 123;\n" 13535 "\t\tint random = 4;\n" 13536 "\t\tstd::string alphabet =\n" 13537 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 13538 "\t}\n" 13539 "};", 13540 Tab); 13541 13542 Tab.UseTab = FormatStyle::UT_AlignWithSpaces; 13543 Tab.TabWidth = 8; 13544 Tab.IndentWidth = 8; 13545 EXPECT_EQ("if (aaaaaaaa && // q\n" 13546 " bb) // w\n" 13547 "\t;", 13548 format("if (aaaaaaaa &&// q\n" 13549 "bb)// w\n" 13550 ";", 13551 Tab)); 13552 EXPECT_EQ("if (aaa && bbb) // w\n" 13553 "\t;", 13554 format("if(aaa&&bbb)// w\n" 13555 ";", 13556 Tab)); 13557 verifyFormat("class X {\n" 13558 "\tvoid f() {\n" 13559 "\t\tsomeFunction(parameter1,\n" 13560 "\t\t parameter2);\n" 13561 "\t}\n" 13562 "};", 13563 Tab); 13564 verifyFormat("#define A \\\n" 13565 "\tvoid f() { \\\n" 13566 "\t\tsomeFunction( \\\n" 13567 "\t\t parameter1, \\\n" 13568 "\t\t parameter2); \\\n" 13569 "\t}", 13570 Tab); 13571 Tab.TabWidth = 4; 13572 Tab.IndentWidth = 8; 13573 verifyFormat("class TabWidth4Indent8 {\n" 13574 "\t\tvoid f() {\n" 13575 "\t\t\t\tsomeFunction(parameter1,\n" 13576 "\t\t\t\t parameter2);\n" 13577 "\t\t}\n" 13578 "};", 13579 Tab); 13580 Tab.TabWidth = 4; 13581 Tab.IndentWidth = 4; 13582 verifyFormat("class TabWidth4Indent4 {\n" 13583 "\tvoid f() {\n" 13584 "\t\tsomeFunction(parameter1,\n" 13585 "\t\t parameter2);\n" 13586 "\t}\n" 13587 "};", 13588 Tab); 13589 Tab.TabWidth = 8; 13590 Tab.IndentWidth = 4; 13591 verifyFormat("class TabWidth8Indent4 {\n" 13592 " void f() {\n" 13593 "\tsomeFunction(parameter1,\n" 13594 "\t parameter2);\n" 13595 " }\n" 13596 "};", 13597 Tab); 13598 Tab.TabWidth = 8; 13599 Tab.IndentWidth = 8; 13600 EXPECT_EQ("/*\n" 13601 " a\t\tcomment\n" 13602 " in multiple lines\n" 13603 " */", 13604 format(" /*\t \t \n" 13605 " \t \t a\t\tcomment\t \t\n" 13606 " \t \t in multiple lines\t\n" 13607 " \t */", 13608 Tab)); 13609 verifyFormat("{\n" 13610 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13611 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13612 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13613 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13614 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13615 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13616 "};", 13617 Tab); 13618 verifyFormat("enum AA {\n" 13619 "\ta1, // Force multiple lines\n" 13620 "\ta2,\n" 13621 "\ta3\n" 13622 "};", 13623 Tab); 13624 EXPECT_EQ("if (aaaaaaaa && // q\n" 13625 " bb) // w\n" 13626 "\t;", 13627 format("if (aaaaaaaa &&// q\n" 13628 "bb)// w\n" 13629 ";", 13630 Tab)); 13631 verifyFormat("class X {\n" 13632 "\tvoid f() {\n" 13633 "\t\tsomeFunction(parameter1,\n" 13634 "\t\t parameter2);\n" 13635 "\t}\n" 13636 "};", 13637 Tab); 13638 verifyFormat("{\n" 13639 "\tQ(\n" 13640 "\t {\n" 13641 "\t\t int a;\n" 13642 "\t\t someFunction(aaaaaaaa,\n" 13643 "\t\t bbbbbbb);\n" 13644 "\t },\n" 13645 "\t p);\n" 13646 "}", 13647 Tab); 13648 EXPECT_EQ("{\n" 13649 "\t/* aaaa\n" 13650 "\t bbbb */\n" 13651 "}", 13652 format("{\n" 13653 "/* aaaa\n" 13654 " bbbb */\n" 13655 "}", 13656 Tab)); 13657 EXPECT_EQ("{\n" 13658 "\t/*\n" 13659 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13660 "\t bbbbbbbbbbbbb\n" 13661 "\t*/\n" 13662 "}", 13663 format("{\n" 13664 "/*\n" 13665 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13666 "*/\n" 13667 "}", 13668 Tab)); 13669 EXPECT_EQ("{\n" 13670 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13671 "\t// bbbbbbbbbbbbb\n" 13672 "}", 13673 format("{\n" 13674 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13675 "}", 13676 Tab)); 13677 EXPECT_EQ("{\n" 13678 "\t/*\n" 13679 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13680 "\t bbbbbbbbbbbbb\n" 13681 "\t*/\n" 13682 "}", 13683 format("{\n" 13684 "\t/*\n" 13685 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13686 "\t*/\n" 13687 "}", 13688 Tab)); 13689 EXPECT_EQ("{\n" 13690 "\t/*\n" 13691 "\n" 13692 "\t*/\n" 13693 "}", 13694 format("{\n" 13695 "\t/*\n" 13696 "\n" 13697 "\t*/\n" 13698 "}", 13699 Tab)); 13700 EXPECT_EQ("{\n" 13701 "\t/*\n" 13702 " asdf\n" 13703 "\t*/\n" 13704 "}", 13705 format("{\n" 13706 "\t/*\n" 13707 " asdf\n" 13708 "\t*/\n" 13709 "}", 13710 Tab)); 13711 EXPECT_EQ("/* some\n" 13712 " comment */", 13713 format(" \t \t /* some\n" 13714 " \t \t comment */", 13715 Tab)); 13716 EXPECT_EQ("int a; /* some\n" 13717 " comment */", 13718 format(" \t \t int a; /* some\n" 13719 " \t \t comment */", 13720 Tab)); 13721 EXPECT_EQ("int a; /* some\n" 13722 "comment */", 13723 format(" \t \t int\ta; /* some\n" 13724 " \t \t comment */", 13725 Tab)); 13726 EXPECT_EQ("f(\"\t\t\"); /* some\n" 13727 " comment */", 13728 format(" \t \t f(\"\t\t\"); /* some\n" 13729 " \t \t comment */", 13730 Tab)); 13731 EXPECT_EQ("{\n" 13732 "\t/*\n" 13733 "\t * Comment\n" 13734 "\t */\n" 13735 "\tint i;\n" 13736 "}", 13737 format("{\n" 13738 "\t/*\n" 13739 "\t * Comment\n" 13740 "\t */\n" 13741 "\t int i;\n" 13742 "}", 13743 Tab)); 13744 Tab.TabWidth = 2; 13745 Tab.IndentWidth = 2; 13746 EXPECT_EQ("{\n" 13747 "\t/* aaaa\n" 13748 "\t bbbb */\n" 13749 "}", 13750 format("{\n" 13751 "/* aaaa\n" 13752 " bbbb */\n" 13753 "}", 13754 Tab)); 13755 EXPECT_EQ("{\n" 13756 "\t/*\n" 13757 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13758 "\t bbbbbbbbbbbbb\n" 13759 "\t*/\n" 13760 "}", 13761 format("{\n" 13762 "/*\n" 13763 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13764 "*/\n" 13765 "}", 13766 Tab)); 13767 Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 13768 Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 13769 Tab.TabWidth = 4; 13770 Tab.IndentWidth = 4; 13771 verifyFormat("class Assign {\n" 13772 "\tvoid f() {\n" 13773 "\t\tint x = 123;\n" 13774 "\t\tint random = 4;\n" 13775 "\t\tstd::string alphabet =\n" 13776 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 13777 "\t}\n" 13778 "};", 13779 Tab); 13780 Tab.AlignOperands = FormatStyle::OAS_Align; 13781 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n" 13782 " cccccccccccccccccccc;", 13783 Tab); 13784 // no alignment 13785 verifyFormat("int aaaaaaaaaa =\n" 13786 "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 13787 Tab); 13788 verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n" 13789 " : bbbbbbbbbbbbbb ? 222222222222222\n" 13790 " : 333333333333333;", 13791 Tab); 13792 Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 13793 Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator; 13794 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n" 13795 " + cccccccccccccccccccc;", 13796 Tab); 13797 } 13798 13799 TEST_F(FormatTest, ZeroTabWidth) { 13800 FormatStyle Tab = getLLVMStyleWithColumns(42); 13801 Tab.IndentWidth = 8; 13802 Tab.UseTab = FormatStyle::UT_Never; 13803 Tab.TabWidth = 0; 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_ForIndentation; 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_ForContinuationAndIndentation; 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_AlignWithSpaces; 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 Tab.UseTab = FormatStyle::UT_Always; 13872 EXPECT_EQ("void a(){\n" 13873 "// line starts with '\t'\n" 13874 "};", 13875 format("void a(){\n" 13876 "\t// line starts with '\t'\n" 13877 "};", 13878 Tab)); 13879 13880 EXPECT_EQ("void a(){\n" 13881 "// line starts with '\t'\n" 13882 "};", 13883 format("void a(){\n" 13884 "\t\t// line starts with '\t'\n" 13885 "};", 13886 Tab)); 13887 } 13888 13889 TEST_F(FormatTest, CalculatesOriginalColumn) { 13890 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13891 "q\"; /* some\n" 13892 " comment */", 13893 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13894 "q\"; /* some\n" 13895 " comment */", 13896 getLLVMStyle())); 13897 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 13898 "/* some\n" 13899 " comment */", 13900 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 13901 " /* some\n" 13902 " comment */", 13903 getLLVMStyle())); 13904 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13905 "qqq\n" 13906 "/* some\n" 13907 " comment */", 13908 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13909 "qqq\n" 13910 " /* some\n" 13911 " comment */", 13912 getLLVMStyle())); 13913 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13914 "wwww; /* some\n" 13915 " comment */", 13916 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13917 "wwww; /* some\n" 13918 " comment */", 13919 getLLVMStyle())); 13920 } 13921 13922 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 13923 FormatStyle NoSpace = getLLVMStyle(); 13924 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 13925 13926 verifyFormat("while(true)\n" 13927 " continue;", 13928 NoSpace); 13929 verifyFormat("for(;;)\n" 13930 " continue;", 13931 NoSpace); 13932 verifyFormat("if(true)\n" 13933 " f();\n" 13934 "else if(true)\n" 13935 " f();", 13936 NoSpace); 13937 verifyFormat("do {\n" 13938 " do_something();\n" 13939 "} while(something());", 13940 NoSpace); 13941 verifyFormat("switch(x) {\n" 13942 "default:\n" 13943 " break;\n" 13944 "}", 13945 NoSpace); 13946 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 13947 verifyFormat("size_t x = sizeof(x);", NoSpace); 13948 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 13949 verifyFormat("auto f(int x) -> typeof(x);", NoSpace); 13950 verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace); 13951 verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace); 13952 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 13953 verifyFormat("alignas(128) char a[128];", NoSpace); 13954 verifyFormat("size_t x = alignof(MyType);", NoSpace); 13955 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 13956 verifyFormat("int f() throw(Deprecated);", NoSpace); 13957 verifyFormat("typedef void (*cb)(int);", NoSpace); 13958 verifyFormat("T A::operator()();", NoSpace); 13959 verifyFormat("X A::operator++(T);", NoSpace); 13960 verifyFormat("auto lambda = []() { return 0; };", NoSpace); 13961 13962 FormatStyle Space = getLLVMStyle(); 13963 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 13964 13965 verifyFormat("int f ();", Space); 13966 verifyFormat("void f (int a, T b) {\n" 13967 " while (true)\n" 13968 " continue;\n" 13969 "}", 13970 Space); 13971 verifyFormat("if (true)\n" 13972 " f ();\n" 13973 "else if (true)\n" 13974 " f ();", 13975 Space); 13976 verifyFormat("do {\n" 13977 " do_something ();\n" 13978 "} while (something ());", 13979 Space); 13980 verifyFormat("switch (x) {\n" 13981 "default:\n" 13982 " break;\n" 13983 "}", 13984 Space); 13985 verifyFormat("A::A () : a (1) {}", Space); 13986 verifyFormat("void f () __attribute__ ((asdf));", Space); 13987 verifyFormat("*(&a + 1);\n" 13988 "&((&a)[1]);\n" 13989 "a[(b + c) * d];\n" 13990 "(((a + 1) * 2) + 3) * 4;", 13991 Space); 13992 verifyFormat("#define A(x) x", Space); 13993 verifyFormat("#define A (x) x", Space); 13994 verifyFormat("#if defined(x)\n" 13995 "#endif", 13996 Space); 13997 verifyFormat("auto i = std::make_unique<int> (5);", Space); 13998 verifyFormat("size_t x = sizeof (x);", Space); 13999 verifyFormat("auto f (int x) -> decltype (x);", Space); 14000 verifyFormat("auto f (int x) -> typeof (x);", Space); 14001 verifyFormat("auto f (int x) -> _Atomic (x);", Space); 14002 verifyFormat("auto f (int x) -> __underlying_type (x);", Space); 14003 verifyFormat("int f (T x) noexcept (x.create ());", Space); 14004 verifyFormat("alignas (128) char a[128];", Space); 14005 verifyFormat("size_t x = alignof (MyType);", Space); 14006 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 14007 verifyFormat("int f () throw (Deprecated);", Space); 14008 verifyFormat("typedef void (*cb) (int);", Space); 14009 verifyFormat("T A::operator() ();", Space); 14010 verifyFormat("X A::operator++ (T);", Space); 14011 verifyFormat("auto lambda = [] () { return 0; };", Space); 14012 verifyFormat("int x = int (y);", Space); 14013 14014 FormatStyle SomeSpace = getLLVMStyle(); 14015 SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses; 14016 14017 verifyFormat("[]() -> float {}", SomeSpace); 14018 verifyFormat("[] (auto foo) {}", SomeSpace); 14019 verifyFormat("[foo]() -> int {}", SomeSpace); 14020 verifyFormat("int f();", SomeSpace); 14021 verifyFormat("void f (int a, T b) {\n" 14022 " while (true)\n" 14023 " continue;\n" 14024 "}", 14025 SomeSpace); 14026 verifyFormat("if (true)\n" 14027 " f();\n" 14028 "else if (true)\n" 14029 " f();", 14030 SomeSpace); 14031 verifyFormat("do {\n" 14032 " do_something();\n" 14033 "} while (something());", 14034 SomeSpace); 14035 verifyFormat("switch (x) {\n" 14036 "default:\n" 14037 " break;\n" 14038 "}", 14039 SomeSpace); 14040 verifyFormat("A::A() : a (1) {}", SomeSpace); 14041 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace); 14042 verifyFormat("*(&a + 1);\n" 14043 "&((&a)[1]);\n" 14044 "a[(b + c) * d];\n" 14045 "(((a + 1) * 2) + 3) * 4;", 14046 SomeSpace); 14047 verifyFormat("#define A(x) x", SomeSpace); 14048 verifyFormat("#define A (x) x", SomeSpace); 14049 verifyFormat("#if defined(x)\n" 14050 "#endif", 14051 SomeSpace); 14052 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace); 14053 verifyFormat("size_t x = sizeof (x);", SomeSpace); 14054 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace); 14055 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace); 14056 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace); 14057 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace); 14058 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace); 14059 verifyFormat("alignas (128) char a[128];", SomeSpace); 14060 verifyFormat("size_t x = alignof (MyType);", SomeSpace); 14061 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", 14062 SomeSpace); 14063 verifyFormat("int f() throw (Deprecated);", SomeSpace); 14064 verifyFormat("typedef void (*cb) (int);", SomeSpace); 14065 verifyFormat("T A::operator()();", SomeSpace); 14066 verifyFormat("X A::operator++ (T);", SomeSpace); 14067 verifyFormat("int x = int (y);", SomeSpace); 14068 verifyFormat("auto lambda = []() { return 0; };", SomeSpace); 14069 } 14070 14071 TEST_F(FormatTest, SpaceAfterLogicalNot) { 14072 FormatStyle Spaces = getLLVMStyle(); 14073 Spaces.SpaceAfterLogicalNot = true; 14074 14075 verifyFormat("bool x = ! y", Spaces); 14076 verifyFormat("if (! isFailure())", Spaces); 14077 verifyFormat("if (! (a && b))", Spaces); 14078 verifyFormat("\"Error!\"", Spaces); 14079 verifyFormat("! ! x", Spaces); 14080 } 14081 14082 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 14083 FormatStyle Spaces = getLLVMStyle(); 14084 14085 Spaces.SpacesInParentheses = true; 14086 verifyFormat("do_something( ::globalVar );", Spaces); 14087 verifyFormat("call( x, y, z );", Spaces); 14088 verifyFormat("call();", Spaces); 14089 verifyFormat("std::function<void( int, int )> callback;", Spaces); 14090 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 14091 Spaces); 14092 verifyFormat("while ( (bool)1 )\n" 14093 " continue;", 14094 Spaces); 14095 verifyFormat("for ( ;; )\n" 14096 " continue;", 14097 Spaces); 14098 verifyFormat("if ( true )\n" 14099 " f();\n" 14100 "else if ( true )\n" 14101 " f();", 14102 Spaces); 14103 verifyFormat("do {\n" 14104 " do_something( (int)i );\n" 14105 "} while ( something() );", 14106 Spaces); 14107 verifyFormat("switch ( x ) {\n" 14108 "default:\n" 14109 " break;\n" 14110 "}", 14111 Spaces); 14112 14113 Spaces.SpacesInParentheses = false; 14114 Spaces.SpacesInCStyleCastParentheses = true; 14115 verifyFormat("Type *A = ( Type * )P;", Spaces); 14116 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 14117 verifyFormat("x = ( int32 )y;", Spaces); 14118 verifyFormat("int a = ( int )(2.0f);", Spaces); 14119 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 14120 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 14121 verifyFormat("#define x (( int )-1)", Spaces); 14122 14123 // Run the first set of tests again with: 14124 Spaces.SpacesInParentheses = false; 14125 Spaces.SpaceInEmptyParentheses = true; 14126 Spaces.SpacesInCStyleCastParentheses = true; 14127 verifyFormat("call(x, y, z);", Spaces); 14128 verifyFormat("call( );", Spaces); 14129 verifyFormat("std::function<void(int, int)> callback;", Spaces); 14130 verifyFormat("while (( bool )1)\n" 14131 " continue;", 14132 Spaces); 14133 verifyFormat("for (;;)\n" 14134 " continue;", 14135 Spaces); 14136 verifyFormat("if (true)\n" 14137 " f( );\n" 14138 "else if (true)\n" 14139 " f( );", 14140 Spaces); 14141 verifyFormat("do {\n" 14142 " do_something(( int )i);\n" 14143 "} while (something( ));", 14144 Spaces); 14145 verifyFormat("switch (x) {\n" 14146 "default:\n" 14147 " break;\n" 14148 "}", 14149 Spaces); 14150 14151 // Run the first set of tests again with: 14152 Spaces.SpaceAfterCStyleCast = true; 14153 verifyFormat("call(x, y, z);", Spaces); 14154 verifyFormat("call( );", Spaces); 14155 verifyFormat("std::function<void(int, int)> callback;", Spaces); 14156 verifyFormat("while (( bool ) 1)\n" 14157 " continue;", 14158 Spaces); 14159 verifyFormat("for (;;)\n" 14160 " continue;", 14161 Spaces); 14162 verifyFormat("if (true)\n" 14163 " f( );\n" 14164 "else if (true)\n" 14165 " f( );", 14166 Spaces); 14167 verifyFormat("do {\n" 14168 " do_something(( int ) i);\n" 14169 "} while (something( ));", 14170 Spaces); 14171 verifyFormat("switch (x) {\n" 14172 "default:\n" 14173 " break;\n" 14174 "}", 14175 Spaces); 14176 14177 // Run subset of tests again with: 14178 Spaces.SpacesInCStyleCastParentheses = false; 14179 Spaces.SpaceAfterCStyleCast = true; 14180 verifyFormat("while ((bool) 1)\n" 14181 " continue;", 14182 Spaces); 14183 verifyFormat("do {\n" 14184 " do_something((int) i);\n" 14185 "} while (something( ));", 14186 Spaces); 14187 14188 verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces); 14189 verifyFormat("size_t idx = (size_t) a;", Spaces); 14190 verifyFormat("size_t idx = (size_t) (a - 1);", Spaces); 14191 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); 14192 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); 14193 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); 14194 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); 14195 Spaces.ColumnLimit = 80; 14196 Spaces.IndentWidth = 4; 14197 Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 14198 verifyFormat("void foo( ) {\n" 14199 " size_t foo = (*(function))(\n" 14200 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " 14201 "BarrrrrrrrrrrrLong,\n" 14202 " FoooooooooLooooong);\n" 14203 "}", 14204 Spaces); 14205 Spaces.SpaceAfterCStyleCast = false; 14206 verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces); 14207 verifyFormat("size_t idx = (size_t)a;", Spaces); 14208 verifyFormat("size_t idx = (size_t)(a - 1);", Spaces); 14209 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); 14210 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); 14211 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); 14212 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); 14213 14214 verifyFormat("void foo( ) {\n" 14215 " size_t foo = (*(function))(\n" 14216 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " 14217 "BarrrrrrrrrrrrLong,\n" 14218 " FoooooooooLooooong);\n" 14219 "}", 14220 Spaces); 14221 } 14222 14223 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 14224 verifyFormat("int a[5];"); 14225 verifyFormat("a[3] += 42;"); 14226 14227 FormatStyle Spaces = getLLVMStyle(); 14228 Spaces.SpacesInSquareBrackets = true; 14229 // Not lambdas. 14230 verifyFormat("int a[ 5 ];", Spaces); 14231 verifyFormat("a[ 3 ] += 42;", Spaces); 14232 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 14233 verifyFormat("double &operator[](int i) { return 0; }\n" 14234 "int i;", 14235 Spaces); 14236 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 14237 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 14238 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 14239 // Lambdas. 14240 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 14241 verifyFormat("return [ i, args... ] {};", Spaces); 14242 verifyFormat("int foo = [ &bar ]() {};", Spaces); 14243 verifyFormat("int foo = [ = ]() {};", Spaces); 14244 verifyFormat("int foo = [ & ]() {};", Spaces); 14245 verifyFormat("int foo = [ =, &bar ]() {};", Spaces); 14246 verifyFormat("int foo = [ &bar, = ]() {};", Spaces); 14247 } 14248 14249 TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) { 14250 FormatStyle NoSpaceStyle = getLLVMStyle(); 14251 verifyFormat("int a[5];", NoSpaceStyle); 14252 verifyFormat("a[3] += 42;", NoSpaceStyle); 14253 14254 verifyFormat("int a[1];", NoSpaceStyle); 14255 verifyFormat("int 1 [a];", NoSpaceStyle); 14256 verifyFormat("int a[1][2];", NoSpaceStyle); 14257 verifyFormat("a[7] = 5;", NoSpaceStyle); 14258 verifyFormat("int a = (f())[23];", NoSpaceStyle); 14259 verifyFormat("f([] {})", NoSpaceStyle); 14260 14261 FormatStyle Space = getLLVMStyle(); 14262 Space.SpaceBeforeSquareBrackets = true; 14263 verifyFormat("int c = []() -> int { return 2; }();\n", Space); 14264 verifyFormat("return [i, args...] {};", Space); 14265 14266 verifyFormat("int a [5];", Space); 14267 verifyFormat("a [3] += 42;", Space); 14268 verifyFormat("constexpr char hello []{\"hello\"};", Space); 14269 verifyFormat("double &operator[](int i) { return 0; }\n" 14270 "int i;", 14271 Space); 14272 verifyFormat("std::unique_ptr<int []> foo() {}", Space); 14273 verifyFormat("int i = a [a][a]->f();", Space); 14274 verifyFormat("int i = (*b) [a]->f();", Space); 14275 14276 verifyFormat("int a [1];", Space); 14277 verifyFormat("int 1 [a];", Space); 14278 verifyFormat("int a [1][2];", Space); 14279 verifyFormat("a [7] = 5;", Space); 14280 verifyFormat("int a = (f()) [23];", Space); 14281 verifyFormat("f([] {})", Space); 14282 } 14283 14284 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 14285 verifyFormat("int a = 5;"); 14286 verifyFormat("a += 42;"); 14287 verifyFormat("a or_eq 8;"); 14288 14289 FormatStyle Spaces = getLLVMStyle(); 14290 Spaces.SpaceBeforeAssignmentOperators = false; 14291 verifyFormat("int a= 5;", Spaces); 14292 verifyFormat("a+= 42;", Spaces); 14293 verifyFormat("a or_eq 8;", Spaces); 14294 } 14295 14296 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) { 14297 verifyFormat("class Foo : public Bar {};"); 14298 verifyFormat("Foo::Foo() : foo(1) {}"); 14299 verifyFormat("for (auto a : b) {\n}"); 14300 verifyFormat("int x = a ? b : c;"); 14301 verifyFormat("{\n" 14302 "label0:\n" 14303 " int x = 0;\n" 14304 "}"); 14305 verifyFormat("switch (x) {\n" 14306 "case 1:\n" 14307 "default:\n" 14308 "}"); 14309 verifyFormat("switch (allBraces) {\n" 14310 "case 1: {\n" 14311 " break;\n" 14312 "}\n" 14313 "case 2: {\n" 14314 " [[fallthrough]];\n" 14315 "}\n" 14316 "default: {\n" 14317 " break;\n" 14318 "}\n" 14319 "}"); 14320 14321 FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30); 14322 CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false; 14323 verifyFormat("class Foo : public Bar {};", CtorInitializerStyle); 14324 verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle); 14325 verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle); 14326 verifyFormat("int x = a ? b : c;", CtorInitializerStyle); 14327 verifyFormat("{\n" 14328 "label1:\n" 14329 " int x = 0;\n" 14330 "}", 14331 CtorInitializerStyle); 14332 verifyFormat("switch (x) {\n" 14333 "case 1:\n" 14334 "default:\n" 14335 "}", 14336 CtorInitializerStyle); 14337 verifyFormat("switch (allBraces) {\n" 14338 "case 1: {\n" 14339 " break;\n" 14340 "}\n" 14341 "case 2: {\n" 14342 " [[fallthrough]];\n" 14343 "}\n" 14344 "default: {\n" 14345 " break;\n" 14346 "}\n" 14347 "}", 14348 CtorInitializerStyle); 14349 CtorInitializerStyle.BreakConstructorInitializers = 14350 FormatStyle::BCIS_AfterColon; 14351 verifyFormat("Fooooooooooo::Fooooooooooo():\n" 14352 " aaaaaaaaaaaaaaaa(1),\n" 14353 " bbbbbbbbbbbbbbbb(2) {}", 14354 CtorInitializerStyle); 14355 CtorInitializerStyle.BreakConstructorInitializers = 14356 FormatStyle::BCIS_BeforeComma; 14357 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 14358 " : aaaaaaaaaaaaaaaa(1)\n" 14359 " , bbbbbbbbbbbbbbbb(2) {}", 14360 CtorInitializerStyle); 14361 CtorInitializerStyle.BreakConstructorInitializers = 14362 FormatStyle::BCIS_BeforeColon; 14363 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 14364 " : aaaaaaaaaaaaaaaa(1),\n" 14365 " bbbbbbbbbbbbbbbb(2) {}", 14366 CtorInitializerStyle); 14367 CtorInitializerStyle.ConstructorInitializerIndentWidth = 0; 14368 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 14369 ": aaaaaaaaaaaaaaaa(1),\n" 14370 " bbbbbbbbbbbbbbbb(2) {}", 14371 CtorInitializerStyle); 14372 14373 FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30); 14374 InheritanceStyle.SpaceBeforeInheritanceColon = false; 14375 verifyFormat("class Foo: public Bar {};", InheritanceStyle); 14376 verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle); 14377 verifyFormat("for (auto a : b) {\n}", InheritanceStyle); 14378 verifyFormat("int x = a ? b : c;", InheritanceStyle); 14379 verifyFormat("{\n" 14380 "label2:\n" 14381 " int x = 0;\n" 14382 "}", 14383 InheritanceStyle); 14384 verifyFormat("switch (x) {\n" 14385 "case 1:\n" 14386 "default:\n" 14387 "}", 14388 InheritanceStyle); 14389 verifyFormat("switch (allBraces) {\n" 14390 "case 1: {\n" 14391 " break;\n" 14392 "}\n" 14393 "case 2: {\n" 14394 " [[fallthrough]];\n" 14395 "}\n" 14396 "default: {\n" 14397 " break;\n" 14398 "}\n" 14399 "}", 14400 InheritanceStyle); 14401 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma; 14402 verifyFormat("class Foooooooooooooooooooooo\n" 14403 " : public aaaaaaaaaaaaaaaaaa,\n" 14404 " public bbbbbbbbbbbbbbbbbb {\n" 14405 "}", 14406 InheritanceStyle); 14407 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon; 14408 verifyFormat("class Foooooooooooooooooooooo:\n" 14409 " public aaaaaaaaaaaaaaaaaa,\n" 14410 " public bbbbbbbbbbbbbbbbbb {\n" 14411 "}", 14412 InheritanceStyle); 14413 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 14414 verifyFormat("class Foooooooooooooooooooooo\n" 14415 " : public aaaaaaaaaaaaaaaaaa\n" 14416 " , public bbbbbbbbbbbbbbbbbb {\n" 14417 "}", 14418 InheritanceStyle); 14419 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 14420 verifyFormat("class Foooooooooooooooooooooo\n" 14421 " : public aaaaaaaaaaaaaaaaaa,\n" 14422 " public bbbbbbbbbbbbbbbbbb {\n" 14423 "}", 14424 InheritanceStyle); 14425 InheritanceStyle.ConstructorInitializerIndentWidth = 0; 14426 verifyFormat("class Foooooooooooooooooooooo\n" 14427 ": public aaaaaaaaaaaaaaaaaa,\n" 14428 " public bbbbbbbbbbbbbbbbbb {}", 14429 InheritanceStyle); 14430 14431 FormatStyle ForLoopStyle = getLLVMStyle(); 14432 ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false; 14433 verifyFormat("class Foo : public Bar {};", ForLoopStyle); 14434 verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle); 14435 verifyFormat("for (auto a: b) {\n}", ForLoopStyle); 14436 verifyFormat("int x = a ? b : c;", ForLoopStyle); 14437 verifyFormat("{\n" 14438 "label2:\n" 14439 " int x = 0;\n" 14440 "}", 14441 ForLoopStyle); 14442 verifyFormat("switch (x) {\n" 14443 "case 1:\n" 14444 "default:\n" 14445 "}", 14446 ForLoopStyle); 14447 verifyFormat("switch (allBraces) {\n" 14448 "case 1: {\n" 14449 " break;\n" 14450 "}\n" 14451 "case 2: {\n" 14452 " [[fallthrough]];\n" 14453 "}\n" 14454 "default: {\n" 14455 " break;\n" 14456 "}\n" 14457 "}", 14458 ForLoopStyle); 14459 14460 FormatStyle CaseStyle = getLLVMStyle(); 14461 CaseStyle.SpaceBeforeCaseColon = true; 14462 verifyFormat("class Foo : public Bar {};", CaseStyle); 14463 verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle); 14464 verifyFormat("for (auto a : b) {\n}", CaseStyle); 14465 verifyFormat("int x = a ? b : c;", CaseStyle); 14466 verifyFormat("switch (x) {\n" 14467 "case 1 :\n" 14468 "default :\n" 14469 "}", 14470 CaseStyle); 14471 verifyFormat("switch (allBraces) {\n" 14472 "case 1 : {\n" 14473 " break;\n" 14474 "}\n" 14475 "case 2 : {\n" 14476 " [[fallthrough]];\n" 14477 "}\n" 14478 "default : {\n" 14479 " break;\n" 14480 "}\n" 14481 "}", 14482 CaseStyle); 14483 14484 FormatStyle NoSpaceStyle = getLLVMStyle(); 14485 EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false); 14486 NoSpaceStyle.SpaceBeforeCtorInitializerColon = false; 14487 NoSpaceStyle.SpaceBeforeInheritanceColon = false; 14488 NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 14489 verifyFormat("class Foo: public Bar {};", NoSpaceStyle); 14490 verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle); 14491 verifyFormat("for (auto a: b) {\n}", NoSpaceStyle); 14492 verifyFormat("int x = a ? b : c;", NoSpaceStyle); 14493 verifyFormat("{\n" 14494 "label3:\n" 14495 " int x = 0;\n" 14496 "}", 14497 NoSpaceStyle); 14498 verifyFormat("switch (x) {\n" 14499 "case 1:\n" 14500 "default:\n" 14501 "}", 14502 NoSpaceStyle); 14503 verifyFormat("switch (allBraces) {\n" 14504 "case 1: {\n" 14505 " break;\n" 14506 "}\n" 14507 "case 2: {\n" 14508 " [[fallthrough]];\n" 14509 "}\n" 14510 "default: {\n" 14511 " break;\n" 14512 "}\n" 14513 "}", 14514 NoSpaceStyle); 14515 14516 FormatStyle InvertedSpaceStyle = getLLVMStyle(); 14517 InvertedSpaceStyle.SpaceBeforeCaseColon = true; 14518 InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false; 14519 InvertedSpaceStyle.SpaceBeforeInheritanceColon = false; 14520 InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 14521 verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle); 14522 verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle); 14523 verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle); 14524 verifyFormat("int x = a ? b : c;", InvertedSpaceStyle); 14525 verifyFormat("{\n" 14526 "label3:\n" 14527 " int x = 0;\n" 14528 "}", 14529 InvertedSpaceStyle); 14530 verifyFormat("switch (x) {\n" 14531 "case 1 :\n" 14532 "case 2 : {\n" 14533 " break;\n" 14534 "}\n" 14535 "default :\n" 14536 " break;\n" 14537 "}", 14538 InvertedSpaceStyle); 14539 verifyFormat("switch (allBraces) {\n" 14540 "case 1 : {\n" 14541 " break;\n" 14542 "}\n" 14543 "case 2 : {\n" 14544 " [[fallthrough]];\n" 14545 "}\n" 14546 "default : {\n" 14547 " break;\n" 14548 "}\n" 14549 "}", 14550 InvertedSpaceStyle); 14551 } 14552 14553 TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) { 14554 FormatStyle Style = getLLVMStyle(); 14555 14556 Style.PointerAlignment = FormatStyle::PAS_Left; 14557 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; 14558 verifyFormat("void* const* x = NULL;", Style); 14559 14560 #define verifyQualifierSpaces(Code, Pointers, Qualifiers) \ 14561 do { \ 14562 Style.PointerAlignment = FormatStyle::Pointers; \ 14563 Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers; \ 14564 verifyFormat(Code, Style); \ 14565 } while (false) 14566 14567 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default); 14568 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default); 14569 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default); 14570 14571 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before); 14572 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before); 14573 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before); 14574 14575 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After); 14576 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After); 14577 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After); 14578 14579 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both); 14580 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both); 14581 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both); 14582 14583 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default); 14584 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, 14585 SAPQ_Default); 14586 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 14587 SAPQ_Default); 14588 14589 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before); 14590 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, 14591 SAPQ_Before); 14592 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 14593 SAPQ_Before); 14594 14595 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After); 14596 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After); 14597 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 14598 SAPQ_After); 14599 14600 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both); 14601 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both); 14602 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both); 14603 14604 #undef verifyQualifierSpaces 14605 14606 FormatStyle Spaces = getLLVMStyle(); 14607 Spaces.AttributeMacros.push_back("qualified"); 14608 Spaces.PointerAlignment = FormatStyle::PAS_Right; 14609 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; 14610 verifyFormat("SomeType *volatile *a = NULL;", Spaces); 14611 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces); 14612 verifyFormat("std::vector<SomeType *const *> x;", Spaces); 14613 verifyFormat("std::vector<SomeType *qualified *> x;", Spaces); 14614 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 14615 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before; 14616 verifyFormat("SomeType * volatile *a = NULL;", Spaces); 14617 verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces); 14618 verifyFormat("std::vector<SomeType * const *> x;", Spaces); 14619 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces); 14620 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 14621 14622 // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left. 14623 Spaces.PointerAlignment = FormatStyle::PAS_Left; 14624 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before; 14625 verifyFormat("SomeType* volatile* a = NULL;", Spaces); 14626 verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces); 14627 verifyFormat("std::vector<SomeType* const*> x;", Spaces); 14628 verifyFormat("std::vector<SomeType* qualified*> x;", Spaces); 14629 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 14630 // However, setting it to SAPQ_After should add spaces after __attribute, etc. 14631 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After; 14632 verifyFormat("SomeType* volatile * a = NULL;", Spaces); 14633 verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces); 14634 verifyFormat("std::vector<SomeType* const *> x;", Spaces); 14635 verifyFormat("std::vector<SomeType* qualified *> x;", Spaces); 14636 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 14637 14638 // PAS_Middle should not have any noticeable changes even for SAPQ_Both 14639 Spaces.PointerAlignment = FormatStyle::PAS_Middle; 14640 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After; 14641 verifyFormat("SomeType * volatile * a = NULL;", Spaces); 14642 verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces); 14643 verifyFormat("std::vector<SomeType * const *> x;", Spaces); 14644 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces); 14645 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 14646 } 14647 14648 TEST_F(FormatTest, AlignConsecutiveMacros) { 14649 FormatStyle Style = getLLVMStyle(); 14650 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 14651 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 14652 Style.AlignConsecutiveMacros = FormatStyle::ACS_None; 14653 14654 verifyFormat("#define a 3\n" 14655 "#define bbbb 4\n" 14656 "#define ccc (5)", 14657 Style); 14658 14659 verifyFormat("#define f(x) (x * x)\n" 14660 "#define fff(x, y, z) (x * y + z)\n" 14661 "#define ffff(x, y) (x - y)", 14662 Style); 14663 14664 verifyFormat("#define foo(x, y) (x + y)\n" 14665 "#define bar (5, 6)(2 + 2)", 14666 Style); 14667 14668 verifyFormat("#define a 3\n" 14669 "#define bbbb 4\n" 14670 "#define ccc (5)\n" 14671 "#define f(x) (x * x)\n" 14672 "#define fff(x, y, z) (x * y + z)\n" 14673 "#define ffff(x, y) (x - y)", 14674 Style); 14675 14676 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 14677 verifyFormat("#define a 3\n" 14678 "#define bbbb 4\n" 14679 "#define ccc (5)", 14680 Style); 14681 14682 verifyFormat("#define f(x) (x * x)\n" 14683 "#define fff(x, y, z) (x * y + z)\n" 14684 "#define ffff(x, y) (x - y)", 14685 Style); 14686 14687 verifyFormat("#define foo(x, y) (x + y)\n" 14688 "#define bar (5, 6)(2 + 2)", 14689 Style); 14690 14691 verifyFormat("#define a 3\n" 14692 "#define bbbb 4\n" 14693 "#define ccc (5)\n" 14694 "#define f(x) (x * x)\n" 14695 "#define fff(x, y, z) (x * y + z)\n" 14696 "#define ffff(x, y) (x - y)", 14697 Style); 14698 14699 verifyFormat("#define a 5\n" 14700 "#define foo(x, y) (x + y)\n" 14701 "#define CCC (6)\n" 14702 "auto lambda = []() {\n" 14703 " auto ii = 0;\n" 14704 " float j = 0;\n" 14705 " return 0;\n" 14706 "};\n" 14707 "int i = 0;\n" 14708 "float i2 = 0;\n" 14709 "auto v = type{\n" 14710 " i = 1, //\n" 14711 " (i = 2), //\n" 14712 " i = 3 //\n" 14713 "};", 14714 Style); 14715 14716 Style.AlignConsecutiveMacros = FormatStyle::ACS_None; 14717 Style.ColumnLimit = 20; 14718 14719 verifyFormat("#define a \\\n" 14720 " \"aabbbbbbbbbbbb\"\n" 14721 "#define D \\\n" 14722 " \"aabbbbbbbbbbbb\" \\\n" 14723 " \"ccddeeeeeeeee\"\n" 14724 "#define B \\\n" 14725 " \"QQQQQQQQQQQQQ\" \\\n" 14726 " \"FFFFFFFFFFFFF\" \\\n" 14727 " \"LLLLLLLL\"\n", 14728 Style); 14729 14730 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 14731 verifyFormat("#define a \\\n" 14732 " \"aabbbbbbbbbbbb\"\n" 14733 "#define D \\\n" 14734 " \"aabbbbbbbbbbbb\" \\\n" 14735 " \"ccddeeeeeeeee\"\n" 14736 "#define B \\\n" 14737 " \"QQQQQQQQQQQQQ\" \\\n" 14738 " \"FFFFFFFFFFFFF\" \\\n" 14739 " \"LLLLLLLL\"\n", 14740 Style); 14741 14742 // Test across comments 14743 Style.MaxEmptyLinesToKeep = 10; 14744 Style.ReflowComments = false; 14745 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossComments; 14746 EXPECT_EQ("#define a 3\n" 14747 "// line comment\n" 14748 "#define bbbb 4\n" 14749 "#define ccc (5)", 14750 format("#define a 3\n" 14751 "// line comment\n" 14752 "#define bbbb 4\n" 14753 "#define ccc (5)", 14754 Style)); 14755 14756 EXPECT_EQ("#define a 3\n" 14757 "/* block comment */\n" 14758 "#define bbbb 4\n" 14759 "#define ccc (5)", 14760 format("#define a 3\n" 14761 "/* block comment */\n" 14762 "#define bbbb 4\n" 14763 "#define ccc (5)", 14764 Style)); 14765 14766 EXPECT_EQ("#define a 3\n" 14767 "/* multi-line *\n" 14768 " * block comment */\n" 14769 "#define bbbb 4\n" 14770 "#define ccc (5)", 14771 format("#define a 3\n" 14772 "/* multi-line *\n" 14773 " * block comment */\n" 14774 "#define bbbb 4\n" 14775 "#define ccc (5)", 14776 Style)); 14777 14778 EXPECT_EQ("#define a 3\n" 14779 "// multi-line line comment\n" 14780 "//\n" 14781 "#define bbbb 4\n" 14782 "#define ccc (5)", 14783 format("#define a 3\n" 14784 "// multi-line line comment\n" 14785 "//\n" 14786 "#define bbbb 4\n" 14787 "#define ccc (5)", 14788 Style)); 14789 14790 EXPECT_EQ("#define a 3\n" 14791 "// empty lines still break.\n" 14792 "\n" 14793 "#define bbbb 4\n" 14794 "#define ccc (5)", 14795 format("#define a 3\n" 14796 "// empty lines still break.\n" 14797 "\n" 14798 "#define bbbb 4\n" 14799 "#define ccc (5)", 14800 Style)); 14801 14802 // Test across empty lines 14803 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLines; 14804 EXPECT_EQ("#define a 3\n" 14805 "\n" 14806 "#define bbbb 4\n" 14807 "#define ccc (5)", 14808 format("#define a 3\n" 14809 "\n" 14810 "#define bbbb 4\n" 14811 "#define ccc (5)", 14812 Style)); 14813 14814 EXPECT_EQ("#define a 3\n" 14815 "\n" 14816 "\n" 14817 "\n" 14818 "#define bbbb 4\n" 14819 "#define ccc (5)", 14820 format("#define a 3\n" 14821 "\n" 14822 "\n" 14823 "\n" 14824 "#define bbbb 4\n" 14825 "#define ccc (5)", 14826 Style)); 14827 14828 EXPECT_EQ("#define a 3\n" 14829 "// comments should break alignment\n" 14830 "//\n" 14831 "#define bbbb 4\n" 14832 "#define ccc (5)", 14833 format("#define a 3\n" 14834 "// comments should break alignment\n" 14835 "//\n" 14836 "#define bbbb 4\n" 14837 "#define ccc (5)", 14838 Style)); 14839 14840 // Test across empty lines and comments 14841 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLinesAndComments; 14842 verifyFormat("#define a 3\n" 14843 "\n" 14844 "// line comment\n" 14845 "#define bbbb 4\n" 14846 "#define ccc (5)", 14847 Style); 14848 14849 EXPECT_EQ("#define a 3\n" 14850 "\n" 14851 "\n" 14852 "/* multi-line *\n" 14853 " * block comment */\n" 14854 "\n" 14855 "\n" 14856 "#define bbbb 4\n" 14857 "#define ccc (5)", 14858 format("#define a 3\n" 14859 "\n" 14860 "\n" 14861 "/* multi-line *\n" 14862 " * block comment */\n" 14863 "\n" 14864 "\n" 14865 "#define bbbb 4\n" 14866 "#define ccc (5)", 14867 Style)); 14868 14869 EXPECT_EQ("#define a 3\n" 14870 "\n" 14871 "\n" 14872 "/* multi-line *\n" 14873 " * block comment */\n" 14874 "\n" 14875 "\n" 14876 "#define bbbb 4\n" 14877 "#define ccc (5)", 14878 format("#define a 3\n" 14879 "\n" 14880 "\n" 14881 "/* multi-line *\n" 14882 " * block comment */\n" 14883 "\n" 14884 "\n" 14885 "#define bbbb 4\n" 14886 "#define ccc (5)", 14887 Style)); 14888 } 14889 14890 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) { 14891 FormatStyle Alignment = getLLVMStyle(); 14892 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 14893 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossEmptyLines; 14894 14895 Alignment.MaxEmptyLinesToKeep = 10; 14896 /* Test alignment across empty lines */ 14897 EXPECT_EQ("int a = 5;\n" 14898 "\n" 14899 "int oneTwoThree = 123;", 14900 format("int a = 5;\n" 14901 "\n" 14902 "int oneTwoThree= 123;", 14903 Alignment)); 14904 EXPECT_EQ("int a = 5;\n" 14905 "int one = 1;\n" 14906 "\n" 14907 "int oneTwoThree = 123;", 14908 format("int a = 5;\n" 14909 "int one = 1;\n" 14910 "\n" 14911 "int oneTwoThree = 123;", 14912 Alignment)); 14913 EXPECT_EQ("int a = 5;\n" 14914 "int one = 1;\n" 14915 "\n" 14916 "int oneTwoThree = 123;\n" 14917 "int oneTwo = 12;", 14918 format("int a = 5;\n" 14919 "int one = 1;\n" 14920 "\n" 14921 "int oneTwoThree = 123;\n" 14922 "int oneTwo = 12;", 14923 Alignment)); 14924 14925 /* Test across comments */ 14926 EXPECT_EQ("int a = 5;\n" 14927 "/* block comment */\n" 14928 "int oneTwoThree = 123;", 14929 format("int a = 5;\n" 14930 "/* block comment */\n" 14931 "int oneTwoThree=123;", 14932 Alignment)); 14933 14934 EXPECT_EQ("int a = 5;\n" 14935 "// line comment\n" 14936 "int oneTwoThree = 123;", 14937 format("int a = 5;\n" 14938 "// line comment\n" 14939 "int oneTwoThree=123;", 14940 Alignment)); 14941 14942 /* Test across comments and newlines */ 14943 EXPECT_EQ("int a = 5;\n" 14944 "\n" 14945 "/* block comment */\n" 14946 "int oneTwoThree = 123;", 14947 format("int a = 5;\n" 14948 "\n" 14949 "/* block comment */\n" 14950 "int oneTwoThree=123;", 14951 Alignment)); 14952 14953 EXPECT_EQ("int a = 5;\n" 14954 "\n" 14955 "// line comment\n" 14956 "int oneTwoThree = 123;", 14957 format("int a = 5;\n" 14958 "\n" 14959 "// line comment\n" 14960 "int oneTwoThree=123;", 14961 Alignment)); 14962 } 14963 14964 TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) { 14965 FormatStyle Alignment = getLLVMStyle(); 14966 Alignment.AlignConsecutiveDeclarations = 14967 FormatStyle::ACS_AcrossEmptyLinesAndComments; 14968 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 14969 14970 Alignment.MaxEmptyLinesToKeep = 10; 14971 /* Test alignment across empty lines */ 14972 EXPECT_EQ("int a = 5;\n" 14973 "\n" 14974 "float const oneTwoThree = 123;", 14975 format("int a = 5;\n" 14976 "\n" 14977 "float const oneTwoThree = 123;", 14978 Alignment)); 14979 EXPECT_EQ("int a = 5;\n" 14980 "float const one = 1;\n" 14981 "\n" 14982 "int oneTwoThree = 123;", 14983 format("int a = 5;\n" 14984 "float const one = 1;\n" 14985 "\n" 14986 "int oneTwoThree = 123;", 14987 Alignment)); 14988 14989 /* Test across comments */ 14990 EXPECT_EQ("float const a = 5;\n" 14991 "/* block comment */\n" 14992 "int oneTwoThree = 123;", 14993 format("float const a = 5;\n" 14994 "/* block comment */\n" 14995 "int oneTwoThree=123;", 14996 Alignment)); 14997 14998 EXPECT_EQ("float const a = 5;\n" 14999 "// line comment\n" 15000 "int oneTwoThree = 123;", 15001 format("float const a = 5;\n" 15002 "// line comment\n" 15003 "int oneTwoThree=123;", 15004 Alignment)); 15005 15006 /* Test across comments and newlines */ 15007 EXPECT_EQ("float const a = 5;\n" 15008 "\n" 15009 "/* block comment */\n" 15010 "int oneTwoThree = 123;", 15011 format("float const a = 5;\n" 15012 "\n" 15013 "/* block comment */\n" 15014 "int oneTwoThree=123;", 15015 Alignment)); 15016 15017 EXPECT_EQ("float const a = 5;\n" 15018 "\n" 15019 "// line comment\n" 15020 "int oneTwoThree = 123;", 15021 format("float const a = 5;\n" 15022 "\n" 15023 "// line comment\n" 15024 "int oneTwoThree=123;", 15025 Alignment)); 15026 } 15027 15028 TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) { 15029 FormatStyle Alignment = getLLVMStyle(); 15030 Alignment.AlignConsecutiveBitFields = 15031 FormatStyle::ACS_AcrossEmptyLinesAndComments; 15032 15033 Alignment.MaxEmptyLinesToKeep = 10; 15034 /* Test alignment across empty lines */ 15035 EXPECT_EQ("int a : 5;\n" 15036 "\n" 15037 "int longbitfield : 6;", 15038 format("int a : 5;\n" 15039 "\n" 15040 "int longbitfield : 6;", 15041 Alignment)); 15042 EXPECT_EQ("int a : 5;\n" 15043 "int one : 1;\n" 15044 "\n" 15045 "int longbitfield : 6;", 15046 format("int a : 5;\n" 15047 "int one : 1;\n" 15048 "\n" 15049 "int longbitfield : 6;", 15050 Alignment)); 15051 15052 /* Test across comments */ 15053 EXPECT_EQ("int a : 5;\n" 15054 "/* block comment */\n" 15055 "int longbitfield : 6;", 15056 format("int a : 5;\n" 15057 "/* block comment */\n" 15058 "int longbitfield : 6;", 15059 Alignment)); 15060 EXPECT_EQ("int a : 5;\n" 15061 "int one : 1;\n" 15062 "// line comment\n" 15063 "int longbitfield : 6;", 15064 format("int a : 5;\n" 15065 "int one : 1;\n" 15066 "// line comment\n" 15067 "int longbitfield : 6;", 15068 Alignment)); 15069 15070 /* Test across comments and newlines */ 15071 EXPECT_EQ("int a : 5;\n" 15072 "/* block comment */\n" 15073 "\n" 15074 "int longbitfield : 6;", 15075 format("int a : 5;\n" 15076 "/* block comment */\n" 15077 "\n" 15078 "int longbitfield : 6;", 15079 Alignment)); 15080 EXPECT_EQ("int a : 5;\n" 15081 "int one : 1;\n" 15082 "\n" 15083 "// line comment\n" 15084 "\n" 15085 "int longbitfield : 6;", 15086 format("int a : 5;\n" 15087 "int one : 1;\n" 15088 "\n" 15089 "// line comment \n" 15090 "\n" 15091 "int longbitfield : 6;", 15092 Alignment)); 15093 } 15094 15095 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) { 15096 FormatStyle Alignment = getLLVMStyle(); 15097 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15098 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossComments; 15099 15100 Alignment.MaxEmptyLinesToKeep = 10; 15101 /* Test alignment across empty lines */ 15102 EXPECT_EQ("int a = 5;\n" 15103 "\n" 15104 "int oneTwoThree = 123;", 15105 format("int a = 5;\n" 15106 "\n" 15107 "int oneTwoThree= 123;", 15108 Alignment)); 15109 EXPECT_EQ("int a = 5;\n" 15110 "int one = 1;\n" 15111 "\n" 15112 "int oneTwoThree = 123;", 15113 format("int a = 5;\n" 15114 "int one = 1;\n" 15115 "\n" 15116 "int oneTwoThree = 123;", 15117 Alignment)); 15118 15119 /* Test across comments */ 15120 EXPECT_EQ("int a = 5;\n" 15121 "/* block comment */\n" 15122 "int oneTwoThree = 123;", 15123 format("int a = 5;\n" 15124 "/* block comment */\n" 15125 "int oneTwoThree=123;", 15126 Alignment)); 15127 15128 EXPECT_EQ("int a = 5;\n" 15129 "// line comment\n" 15130 "int oneTwoThree = 123;", 15131 format("int a = 5;\n" 15132 "// line comment\n" 15133 "int oneTwoThree=123;", 15134 Alignment)); 15135 15136 EXPECT_EQ("int a = 5;\n" 15137 "/*\n" 15138 " * multi-line block comment\n" 15139 " */\n" 15140 "int oneTwoThree = 123;", 15141 format("int a = 5;\n" 15142 "/*\n" 15143 " * multi-line block comment\n" 15144 " */\n" 15145 "int oneTwoThree=123;", 15146 Alignment)); 15147 15148 EXPECT_EQ("int a = 5;\n" 15149 "//\n" 15150 "// multi-line line comment\n" 15151 "//\n" 15152 "int oneTwoThree = 123;", 15153 format("int a = 5;\n" 15154 "//\n" 15155 "// multi-line line comment\n" 15156 "//\n" 15157 "int oneTwoThree=123;", 15158 Alignment)); 15159 15160 /* Test across comments and newlines */ 15161 EXPECT_EQ("int a = 5;\n" 15162 "\n" 15163 "/* block comment */\n" 15164 "int oneTwoThree = 123;", 15165 format("int a = 5;\n" 15166 "\n" 15167 "/* block comment */\n" 15168 "int oneTwoThree=123;", 15169 Alignment)); 15170 15171 EXPECT_EQ("int a = 5;\n" 15172 "\n" 15173 "// line comment\n" 15174 "int oneTwoThree = 123;", 15175 format("int a = 5;\n" 15176 "\n" 15177 "// line comment\n" 15178 "int oneTwoThree=123;", 15179 Alignment)); 15180 } 15181 15182 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) { 15183 FormatStyle Alignment = getLLVMStyle(); 15184 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15185 Alignment.AlignConsecutiveAssignments = 15186 FormatStyle::ACS_AcrossEmptyLinesAndComments; 15187 verifyFormat("int a = 5;\n" 15188 "int oneTwoThree = 123;", 15189 Alignment); 15190 verifyFormat("int a = method();\n" 15191 "int oneTwoThree = 133;", 15192 Alignment); 15193 verifyFormat("a &= 5;\n" 15194 "bcd *= 5;\n" 15195 "ghtyf += 5;\n" 15196 "dvfvdb -= 5;\n" 15197 "a /= 5;\n" 15198 "vdsvsv %= 5;\n" 15199 "sfdbddfbdfbb ^= 5;\n" 15200 "dvsdsv |= 5;\n" 15201 "int dsvvdvsdvvv = 123;", 15202 Alignment); 15203 verifyFormat("int i = 1, j = 10;\n" 15204 "something = 2000;", 15205 Alignment); 15206 verifyFormat("something = 2000;\n" 15207 "int i = 1, j = 10;\n", 15208 Alignment); 15209 verifyFormat("something = 2000;\n" 15210 "another = 911;\n" 15211 "int i = 1, j = 10;\n" 15212 "oneMore = 1;\n" 15213 "i = 2;", 15214 Alignment); 15215 verifyFormat("int a = 5;\n" 15216 "int one = 1;\n" 15217 "method();\n" 15218 "int oneTwoThree = 123;\n" 15219 "int oneTwo = 12;", 15220 Alignment); 15221 verifyFormat("int oneTwoThree = 123;\n" 15222 "int oneTwo = 12;\n" 15223 "method();\n", 15224 Alignment); 15225 verifyFormat("int oneTwoThree = 123; // comment\n" 15226 "int oneTwo = 12; // comment", 15227 Alignment); 15228 15229 // Bug 25167 15230 /* Uncomment when fixed 15231 verifyFormat("#if A\n" 15232 "#else\n" 15233 "int aaaaaaaa = 12;\n" 15234 "#endif\n" 15235 "#if B\n" 15236 "#else\n" 15237 "int a = 12;\n" 15238 "#endif\n", 15239 Alignment); 15240 verifyFormat("enum foo {\n" 15241 "#if A\n" 15242 "#else\n" 15243 " aaaaaaaa = 12;\n" 15244 "#endif\n" 15245 "#if B\n" 15246 "#else\n" 15247 " a = 12;\n" 15248 "#endif\n" 15249 "};\n", 15250 Alignment); 15251 */ 15252 15253 Alignment.MaxEmptyLinesToKeep = 10; 15254 /* Test alignment across empty lines */ 15255 EXPECT_EQ("int a = 5;\n" 15256 "\n" 15257 "int oneTwoThree = 123;", 15258 format("int a = 5;\n" 15259 "\n" 15260 "int oneTwoThree= 123;", 15261 Alignment)); 15262 EXPECT_EQ("int a = 5;\n" 15263 "int one = 1;\n" 15264 "\n" 15265 "int oneTwoThree = 123;", 15266 format("int a = 5;\n" 15267 "int one = 1;\n" 15268 "\n" 15269 "int oneTwoThree = 123;", 15270 Alignment)); 15271 EXPECT_EQ("int a = 5;\n" 15272 "int one = 1;\n" 15273 "\n" 15274 "int oneTwoThree = 123;\n" 15275 "int oneTwo = 12;", 15276 format("int a = 5;\n" 15277 "int one = 1;\n" 15278 "\n" 15279 "int oneTwoThree = 123;\n" 15280 "int oneTwo = 12;", 15281 Alignment)); 15282 15283 /* Test across comments */ 15284 EXPECT_EQ("int a = 5;\n" 15285 "/* block comment */\n" 15286 "int oneTwoThree = 123;", 15287 format("int a = 5;\n" 15288 "/* block comment */\n" 15289 "int oneTwoThree=123;", 15290 Alignment)); 15291 15292 EXPECT_EQ("int a = 5;\n" 15293 "// line comment\n" 15294 "int oneTwoThree = 123;", 15295 format("int a = 5;\n" 15296 "// line comment\n" 15297 "int oneTwoThree=123;", 15298 Alignment)); 15299 15300 /* Test across comments and newlines */ 15301 EXPECT_EQ("int a = 5;\n" 15302 "\n" 15303 "/* block comment */\n" 15304 "int oneTwoThree = 123;", 15305 format("int a = 5;\n" 15306 "\n" 15307 "/* block comment */\n" 15308 "int oneTwoThree=123;", 15309 Alignment)); 15310 15311 EXPECT_EQ("int a = 5;\n" 15312 "\n" 15313 "// line comment\n" 15314 "int oneTwoThree = 123;", 15315 format("int a = 5;\n" 15316 "\n" 15317 "// line comment\n" 15318 "int oneTwoThree=123;", 15319 Alignment)); 15320 15321 EXPECT_EQ("int a = 5;\n" 15322 "//\n" 15323 "// multi-line line comment\n" 15324 "//\n" 15325 "int oneTwoThree = 123;", 15326 format("int a = 5;\n" 15327 "//\n" 15328 "// multi-line line comment\n" 15329 "//\n" 15330 "int oneTwoThree=123;", 15331 Alignment)); 15332 15333 EXPECT_EQ("int a = 5;\n" 15334 "/*\n" 15335 " * multi-line block comment\n" 15336 " */\n" 15337 "int oneTwoThree = 123;", 15338 format("int a = 5;\n" 15339 "/*\n" 15340 " * multi-line block comment\n" 15341 " */\n" 15342 "int oneTwoThree=123;", 15343 Alignment)); 15344 15345 EXPECT_EQ("int a = 5;\n" 15346 "\n" 15347 "/* block comment */\n" 15348 "\n" 15349 "\n" 15350 "\n" 15351 "int oneTwoThree = 123;", 15352 format("int a = 5;\n" 15353 "\n" 15354 "/* block comment */\n" 15355 "\n" 15356 "\n" 15357 "\n" 15358 "int oneTwoThree=123;", 15359 Alignment)); 15360 15361 EXPECT_EQ("int a = 5;\n" 15362 "\n" 15363 "// line comment\n" 15364 "\n" 15365 "\n" 15366 "\n" 15367 "int oneTwoThree = 123;", 15368 format("int a = 5;\n" 15369 "\n" 15370 "// line comment\n" 15371 "\n" 15372 "\n" 15373 "\n" 15374 "int oneTwoThree=123;", 15375 Alignment)); 15376 15377 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 15378 verifyFormat("#define A \\\n" 15379 " int aaaa = 12; \\\n" 15380 " int b = 23; \\\n" 15381 " int ccc = 234; \\\n" 15382 " int dddddddddd = 2345;", 15383 Alignment); 15384 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 15385 verifyFormat("#define A \\\n" 15386 " int aaaa = 12; \\\n" 15387 " int b = 23; \\\n" 15388 " int ccc = 234; \\\n" 15389 " int dddddddddd = 2345;", 15390 Alignment); 15391 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 15392 verifyFormat("#define A " 15393 " \\\n" 15394 " int aaaa = 12; " 15395 " \\\n" 15396 " int b = 23; " 15397 " \\\n" 15398 " int ccc = 234; " 15399 " \\\n" 15400 " int dddddddddd = 2345;", 15401 Alignment); 15402 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 15403 "k = 4, int l = 5,\n" 15404 " int m = 6) {\n" 15405 " int j = 10;\n" 15406 " otherThing = 1;\n" 15407 "}", 15408 Alignment); 15409 verifyFormat("void SomeFunction(int parameter = 0) {\n" 15410 " int i = 1;\n" 15411 " int j = 2;\n" 15412 " int big = 10000;\n" 15413 "}", 15414 Alignment); 15415 verifyFormat("class C {\n" 15416 "public:\n" 15417 " int i = 1;\n" 15418 " virtual void f() = 0;\n" 15419 "};", 15420 Alignment); 15421 verifyFormat("int i = 1;\n" 15422 "if (SomeType t = getSomething()) {\n" 15423 "}\n" 15424 "int j = 2;\n" 15425 "int big = 10000;", 15426 Alignment); 15427 verifyFormat("int j = 7;\n" 15428 "for (int k = 0; k < N; ++k) {\n" 15429 "}\n" 15430 "int j = 2;\n" 15431 "int big = 10000;\n" 15432 "}", 15433 Alignment); 15434 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 15435 verifyFormat("int i = 1;\n" 15436 "LooooooooooongType loooooooooooooooooooooongVariable\n" 15437 " = someLooooooooooooooooongFunction();\n" 15438 "int j = 2;", 15439 Alignment); 15440 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 15441 verifyFormat("int i = 1;\n" 15442 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 15443 " someLooooooooooooooooongFunction();\n" 15444 "int j = 2;", 15445 Alignment); 15446 15447 verifyFormat("auto lambda = []() {\n" 15448 " auto i = 0;\n" 15449 " return 0;\n" 15450 "};\n" 15451 "int i = 0;\n" 15452 "auto v = type{\n" 15453 " i = 1, //\n" 15454 " (i = 2), //\n" 15455 " i = 3 //\n" 15456 "};", 15457 Alignment); 15458 15459 verifyFormat( 15460 "int i = 1;\n" 15461 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 15462 " loooooooooooooooooooooongParameterB);\n" 15463 "int j = 2;", 15464 Alignment); 15465 15466 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 15467 " typename B = very_long_type_name_1,\n" 15468 " typename T_2 = very_long_type_name_2>\n" 15469 "auto foo() {}\n", 15470 Alignment); 15471 verifyFormat("int a, b = 1;\n" 15472 "int c = 2;\n" 15473 "int dd = 3;\n", 15474 Alignment); 15475 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 15476 "float b[1][] = {{3.f}};\n", 15477 Alignment); 15478 verifyFormat("for (int i = 0; i < 1; i++)\n" 15479 " int x = 1;\n", 15480 Alignment); 15481 verifyFormat("for (i = 0; i < 1; i++)\n" 15482 " x = 1;\n" 15483 "y = 1;\n", 15484 Alignment); 15485 15486 Alignment.ReflowComments = true; 15487 Alignment.ColumnLimit = 50; 15488 EXPECT_EQ("int x = 0;\n" 15489 "int yy = 1; /// specificlennospace\n" 15490 "int zzz = 2;\n", 15491 format("int x = 0;\n" 15492 "int yy = 1; ///specificlennospace\n" 15493 "int zzz = 2;\n", 15494 Alignment)); 15495 } 15496 15497 TEST_F(FormatTest, AlignConsecutiveAssignments) { 15498 FormatStyle Alignment = getLLVMStyle(); 15499 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15500 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 15501 verifyFormat("int a = 5;\n" 15502 "int oneTwoThree = 123;", 15503 Alignment); 15504 verifyFormat("int a = 5;\n" 15505 "int oneTwoThree = 123;", 15506 Alignment); 15507 15508 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 15509 verifyFormat("int a = 5;\n" 15510 "int oneTwoThree = 123;", 15511 Alignment); 15512 verifyFormat("int a = method();\n" 15513 "int oneTwoThree = 133;", 15514 Alignment); 15515 verifyFormat("a &= 5;\n" 15516 "bcd *= 5;\n" 15517 "ghtyf += 5;\n" 15518 "dvfvdb -= 5;\n" 15519 "a /= 5;\n" 15520 "vdsvsv %= 5;\n" 15521 "sfdbddfbdfbb ^= 5;\n" 15522 "dvsdsv |= 5;\n" 15523 "int dsvvdvsdvvv = 123;", 15524 Alignment); 15525 verifyFormat("int i = 1, j = 10;\n" 15526 "something = 2000;", 15527 Alignment); 15528 verifyFormat("something = 2000;\n" 15529 "int i = 1, j = 10;\n", 15530 Alignment); 15531 verifyFormat("something = 2000;\n" 15532 "another = 911;\n" 15533 "int i = 1, j = 10;\n" 15534 "oneMore = 1;\n" 15535 "i = 2;", 15536 Alignment); 15537 verifyFormat("int a = 5;\n" 15538 "int one = 1;\n" 15539 "method();\n" 15540 "int oneTwoThree = 123;\n" 15541 "int oneTwo = 12;", 15542 Alignment); 15543 verifyFormat("int oneTwoThree = 123;\n" 15544 "int oneTwo = 12;\n" 15545 "method();\n", 15546 Alignment); 15547 verifyFormat("int oneTwoThree = 123; // comment\n" 15548 "int oneTwo = 12; // comment", 15549 Alignment); 15550 15551 // Bug 25167 15552 /* Uncomment when fixed 15553 verifyFormat("#if A\n" 15554 "#else\n" 15555 "int aaaaaaaa = 12;\n" 15556 "#endif\n" 15557 "#if B\n" 15558 "#else\n" 15559 "int a = 12;\n" 15560 "#endif\n", 15561 Alignment); 15562 verifyFormat("enum foo {\n" 15563 "#if A\n" 15564 "#else\n" 15565 " aaaaaaaa = 12;\n" 15566 "#endif\n" 15567 "#if B\n" 15568 "#else\n" 15569 " a = 12;\n" 15570 "#endif\n" 15571 "};\n", 15572 Alignment); 15573 */ 15574 15575 EXPECT_EQ("int a = 5;\n" 15576 "\n" 15577 "int oneTwoThree = 123;", 15578 format("int a = 5;\n" 15579 "\n" 15580 "int oneTwoThree= 123;", 15581 Alignment)); 15582 EXPECT_EQ("int a = 5;\n" 15583 "int one = 1;\n" 15584 "\n" 15585 "int oneTwoThree = 123;", 15586 format("int a = 5;\n" 15587 "int one = 1;\n" 15588 "\n" 15589 "int oneTwoThree = 123;", 15590 Alignment)); 15591 EXPECT_EQ("int a = 5;\n" 15592 "int one = 1;\n" 15593 "\n" 15594 "int oneTwoThree = 123;\n" 15595 "int oneTwo = 12;", 15596 format("int a = 5;\n" 15597 "int one = 1;\n" 15598 "\n" 15599 "int oneTwoThree = 123;\n" 15600 "int oneTwo = 12;", 15601 Alignment)); 15602 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 15603 verifyFormat("#define A \\\n" 15604 " int aaaa = 12; \\\n" 15605 " int b = 23; \\\n" 15606 " int ccc = 234; \\\n" 15607 " int dddddddddd = 2345;", 15608 Alignment); 15609 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 15610 verifyFormat("#define A \\\n" 15611 " int aaaa = 12; \\\n" 15612 " int b = 23; \\\n" 15613 " int ccc = 234; \\\n" 15614 " int dddddddddd = 2345;", 15615 Alignment); 15616 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 15617 verifyFormat("#define A " 15618 " \\\n" 15619 " int aaaa = 12; " 15620 " \\\n" 15621 " int b = 23; " 15622 " \\\n" 15623 " int ccc = 234; " 15624 " \\\n" 15625 " int dddddddddd = 2345;", 15626 Alignment); 15627 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 15628 "k = 4, int l = 5,\n" 15629 " int m = 6) {\n" 15630 " int j = 10;\n" 15631 " otherThing = 1;\n" 15632 "}", 15633 Alignment); 15634 verifyFormat("void SomeFunction(int parameter = 0) {\n" 15635 " int i = 1;\n" 15636 " int j = 2;\n" 15637 " int big = 10000;\n" 15638 "}", 15639 Alignment); 15640 verifyFormat("class C {\n" 15641 "public:\n" 15642 " int i = 1;\n" 15643 " virtual void f() = 0;\n" 15644 "};", 15645 Alignment); 15646 verifyFormat("int i = 1;\n" 15647 "if (SomeType t = getSomething()) {\n" 15648 "}\n" 15649 "int j = 2;\n" 15650 "int big = 10000;", 15651 Alignment); 15652 verifyFormat("int j = 7;\n" 15653 "for (int k = 0; k < N; ++k) {\n" 15654 "}\n" 15655 "int j = 2;\n" 15656 "int big = 10000;\n" 15657 "}", 15658 Alignment); 15659 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 15660 verifyFormat("int i = 1;\n" 15661 "LooooooooooongType loooooooooooooooooooooongVariable\n" 15662 " = someLooooooooooooooooongFunction();\n" 15663 "int j = 2;", 15664 Alignment); 15665 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 15666 verifyFormat("int i = 1;\n" 15667 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 15668 " someLooooooooooooooooongFunction();\n" 15669 "int j = 2;", 15670 Alignment); 15671 15672 verifyFormat("auto lambda = []() {\n" 15673 " auto i = 0;\n" 15674 " return 0;\n" 15675 "};\n" 15676 "int i = 0;\n" 15677 "auto v = type{\n" 15678 " i = 1, //\n" 15679 " (i = 2), //\n" 15680 " i = 3 //\n" 15681 "};", 15682 Alignment); 15683 15684 verifyFormat( 15685 "int i = 1;\n" 15686 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 15687 " loooooooooooooooooooooongParameterB);\n" 15688 "int j = 2;", 15689 Alignment); 15690 15691 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 15692 " typename B = very_long_type_name_1,\n" 15693 " typename T_2 = very_long_type_name_2>\n" 15694 "auto foo() {}\n", 15695 Alignment); 15696 verifyFormat("int a, b = 1;\n" 15697 "int c = 2;\n" 15698 "int dd = 3;\n", 15699 Alignment); 15700 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 15701 "float b[1][] = {{3.f}};\n", 15702 Alignment); 15703 verifyFormat("for (int i = 0; i < 1; i++)\n" 15704 " int x = 1;\n", 15705 Alignment); 15706 verifyFormat("for (i = 0; i < 1; i++)\n" 15707 " x = 1;\n" 15708 "y = 1;\n", 15709 Alignment); 15710 15711 Alignment.ReflowComments = true; 15712 Alignment.ColumnLimit = 50; 15713 EXPECT_EQ("int x = 0;\n" 15714 "int yy = 1; /// specificlennospace\n" 15715 "int zzz = 2;\n", 15716 format("int x = 0;\n" 15717 "int yy = 1; ///specificlennospace\n" 15718 "int zzz = 2;\n", 15719 Alignment)); 15720 } 15721 15722 TEST_F(FormatTest, AlignConsecutiveBitFields) { 15723 FormatStyle Alignment = getLLVMStyle(); 15724 Alignment.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive; 15725 verifyFormat("int const a : 5;\n" 15726 "int oneTwoThree : 23;", 15727 Alignment); 15728 15729 // Initializers are allowed starting with c++2a 15730 verifyFormat("int const a : 5 = 1;\n" 15731 "int oneTwoThree : 23 = 0;", 15732 Alignment); 15733 15734 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 15735 verifyFormat("int const a : 5;\n" 15736 "int oneTwoThree : 23;", 15737 Alignment); 15738 15739 verifyFormat("int const a : 5; // comment\n" 15740 "int oneTwoThree : 23; // comment", 15741 Alignment); 15742 15743 verifyFormat("int const a : 5 = 1;\n" 15744 "int oneTwoThree : 23 = 0;", 15745 Alignment); 15746 15747 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 15748 verifyFormat("int const a : 5 = 1;\n" 15749 "int oneTwoThree : 23 = 0;", 15750 Alignment); 15751 verifyFormat("int const a : 5 = {1};\n" 15752 "int oneTwoThree : 23 = 0;", 15753 Alignment); 15754 15755 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None; 15756 verifyFormat("int const a :5;\n" 15757 "int oneTwoThree:23;", 15758 Alignment); 15759 15760 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before; 15761 verifyFormat("int const a :5;\n" 15762 "int oneTwoThree :23;", 15763 Alignment); 15764 15765 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After; 15766 verifyFormat("int const a : 5;\n" 15767 "int oneTwoThree: 23;", 15768 Alignment); 15769 15770 // Known limitations: ':' is only recognized as a bitfield colon when 15771 // followed by a number. 15772 /* 15773 verifyFormat("int oneTwoThree : SOME_CONSTANT;\n" 15774 "int a : 5;", 15775 Alignment); 15776 */ 15777 } 15778 15779 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 15780 FormatStyle Alignment = getLLVMStyle(); 15781 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15782 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_None; 15783 Alignment.PointerAlignment = FormatStyle::PAS_Right; 15784 verifyFormat("float const a = 5;\n" 15785 "int oneTwoThree = 123;", 15786 Alignment); 15787 verifyFormat("int a = 5;\n" 15788 "float const oneTwoThree = 123;", 15789 Alignment); 15790 15791 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 15792 verifyFormat("float const a = 5;\n" 15793 "int oneTwoThree = 123;", 15794 Alignment); 15795 verifyFormat("int a = method();\n" 15796 "float const oneTwoThree = 133;", 15797 Alignment); 15798 verifyFormat("int i = 1, j = 10;\n" 15799 "something = 2000;", 15800 Alignment); 15801 verifyFormat("something = 2000;\n" 15802 "int i = 1, j = 10;\n", 15803 Alignment); 15804 verifyFormat("float something = 2000;\n" 15805 "double another = 911;\n" 15806 "int i = 1, j = 10;\n" 15807 "const int *oneMore = 1;\n" 15808 "unsigned i = 2;", 15809 Alignment); 15810 verifyFormat("float a = 5;\n" 15811 "int one = 1;\n" 15812 "method();\n" 15813 "const double oneTwoThree = 123;\n" 15814 "const unsigned int oneTwo = 12;", 15815 Alignment); 15816 verifyFormat("int oneTwoThree{0}; // comment\n" 15817 "unsigned oneTwo; // comment", 15818 Alignment); 15819 verifyFormat("unsigned int *a;\n" 15820 "int *b;\n" 15821 "unsigned int Const *c;\n" 15822 "unsigned int const *d;\n" 15823 "unsigned int Const &e;\n" 15824 "unsigned int const &f;", 15825 Alignment); 15826 verifyFormat("Const unsigned int *c;\n" 15827 "const unsigned int *d;\n" 15828 "Const unsigned int &e;\n" 15829 "const unsigned int &f;\n" 15830 "const unsigned g;\n" 15831 "Const unsigned h;", 15832 Alignment); 15833 EXPECT_EQ("float const a = 5;\n" 15834 "\n" 15835 "int oneTwoThree = 123;", 15836 format("float const a = 5;\n" 15837 "\n" 15838 "int oneTwoThree= 123;", 15839 Alignment)); 15840 EXPECT_EQ("float a = 5;\n" 15841 "int one = 1;\n" 15842 "\n" 15843 "unsigned oneTwoThree = 123;", 15844 format("float a = 5;\n" 15845 "int one = 1;\n" 15846 "\n" 15847 "unsigned oneTwoThree = 123;", 15848 Alignment)); 15849 EXPECT_EQ("float a = 5;\n" 15850 "int one = 1;\n" 15851 "\n" 15852 "unsigned oneTwoThree = 123;\n" 15853 "int oneTwo = 12;", 15854 format("float a = 5;\n" 15855 "int one = 1;\n" 15856 "\n" 15857 "unsigned oneTwoThree = 123;\n" 15858 "int oneTwo = 12;", 15859 Alignment)); 15860 // Function prototype alignment 15861 verifyFormat("int a();\n" 15862 "double b();", 15863 Alignment); 15864 verifyFormat("int a(int x);\n" 15865 "double b();", 15866 Alignment); 15867 unsigned OldColumnLimit = Alignment.ColumnLimit; 15868 // We need to set ColumnLimit to zero, in order to stress nested alignments, 15869 // otherwise the function parameters will be re-flowed onto a single line. 15870 Alignment.ColumnLimit = 0; 15871 EXPECT_EQ("int a(int x,\n" 15872 " float y);\n" 15873 "double b(int x,\n" 15874 " double y);", 15875 format("int a(int x,\n" 15876 " float y);\n" 15877 "double b(int x,\n" 15878 " double y);", 15879 Alignment)); 15880 // This ensures that function parameters of function declarations are 15881 // correctly indented when their owning functions are indented. 15882 // The failure case here is for 'double y' to not be indented enough. 15883 EXPECT_EQ("double a(int x);\n" 15884 "int b(int y,\n" 15885 " double z);", 15886 format("double a(int x);\n" 15887 "int b(int y,\n" 15888 " double z);", 15889 Alignment)); 15890 // Set ColumnLimit low so that we induce wrapping immediately after 15891 // the function name and opening paren. 15892 Alignment.ColumnLimit = 13; 15893 verifyFormat("int function(\n" 15894 " int x,\n" 15895 " bool y);", 15896 Alignment); 15897 Alignment.ColumnLimit = OldColumnLimit; 15898 // Ensure function pointers don't screw up recursive alignment 15899 verifyFormat("int a(int x, void (*fp)(int y));\n" 15900 "double b();", 15901 Alignment); 15902 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 15903 // Ensure recursive alignment is broken by function braces, so that the 15904 // "a = 1" does not align with subsequent assignments inside the function 15905 // body. 15906 verifyFormat("int func(int a = 1) {\n" 15907 " int b = 2;\n" 15908 " int cc = 3;\n" 15909 "}", 15910 Alignment); 15911 verifyFormat("float something = 2000;\n" 15912 "double another = 911;\n" 15913 "int i = 1, j = 10;\n" 15914 "const int *oneMore = 1;\n" 15915 "unsigned i = 2;", 15916 Alignment); 15917 verifyFormat("int oneTwoThree = {0}; // comment\n" 15918 "unsigned oneTwo = 0; // comment", 15919 Alignment); 15920 // Make sure that scope is correctly tracked, in the absence of braces 15921 verifyFormat("for (int i = 0; i < n; i++)\n" 15922 " j = i;\n" 15923 "double x = 1;\n", 15924 Alignment); 15925 verifyFormat("if (int i = 0)\n" 15926 " j = i;\n" 15927 "double x = 1;\n", 15928 Alignment); 15929 // Ensure operator[] and operator() are comprehended 15930 verifyFormat("struct test {\n" 15931 " long long int foo();\n" 15932 " int operator[](int a);\n" 15933 " double bar();\n" 15934 "};\n", 15935 Alignment); 15936 verifyFormat("struct test {\n" 15937 " long long int foo();\n" 15938 " int operator()(int a);\n" 15939 " double bar();\n" 15940 "};\n", 15941 Alignment); 15942 15943 // PAS_Right 15944 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 15945 " int const i = 1;\n" 15946 " int *j = 2;\n" 15947 " int big = 10000;\n" 15948 "\n" 15949 " unsigned oneTwoThree = 123;\n" 15950 " int oneTwo = 12;\n" 15951 " method();\n" 15952 " float k = 2;\n" 15953 " int ll = 10000;\n" 15954 "}", 15955 format("void SomeFunction(int parameter= 0) {\n" 15956 " int const i= 1;\n" 15957 " int *j=2;\n" 15958 " int big = 10000;\n" 15959 "\n" 15960 "unsigned oneTwoThree =123;\n" 15961 "int oneTwo = 12;\n" 15962 " method();\n" 15963 "float k= 2;\n" 15964 "int ll=10000;\n" 15965 "}", 15966 Alignment)); 15967 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 15968 " int const i = 1;\n" 15969 " int **j = 2, ***k;\n" 15970 " int &k = i;\n" 15971 " int &&l = i + j;\n" 15972 " int big = 10000;\n" 15973 "\n" 15974 " unsigned oneTwoThree = 123;\n" 15975 " int oneTwo = 12;\n" 15976 " method();\n" 15977 " float k = 2;\n" 15978 " int ll = 10000;\n" 15979 "}", 15980 format("void SomeFunction(int parameter= 0) {\n" 15981 " int const i= 1;\n" 15982 " int **j=2,***k;\n" 15983 "int &k=i;\n" 15984 "int &&l=i+j;\n" 15985 " int big = 10000;\n" 15986 "\n" 15987 "unsigned oneTwoThree =123;\n" 15988 "int oneTwo = 12;\n" 15989 " method();\n" 15990 "float k= 2;\n" 15991 "int ll=10000;\n" 15992 "}", 15993 Alignment)); 15994 // variables are aligned at their name, pointers are at the right most 15995 // position 15996 verifyFormat("int *a;\n" 15997 "int **b;\n" 15998 "int ***c;\n" 15999 "int foobar;\n", 16000 Alignment); 16001 16002 // PAS_Left 16003 FormatStyle AlignmentLeft = Alignment; 16004 AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left; 16005 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16006 " int const i = 1;\n" 16007 " int* j = 2;\n" 16008 " int big = 10000;\n" 16009 "\n" 16010 " unsigned oneTwoThree = 123;\n" 16011 " int oneTwo = 12;\n" 16012 " method();\n" 16013 " float k = 2;\n" 16014 " int ll = 10000;\n" 16015 "}", 16016 format("void SomeFunction(int parameter= 0) {\n" 16017 " int const i= 1;\n" 16018 " int *j=2;\n" 16019 " int big = 10000;\n" 16020 "\n" 16021 "unsigned oneTwoThree =123;\n" 16022 "int oneTwo = 12;\n" 16023 " method();\n" 16024 "float k= 2;\n" 16025 "int ll=10000;\n" 16026 "}", 16027 AlignmentLeft)); 16028 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16029 " int const i = 1;\n" 16030 " int** j = 2;\n" 16031 " int& k = i;\n" 16032 " int&& l = i + j;\n" 16033 " int big = 10000;\n" 16034 "\n" 16035 " unsigned oneTwoThree = 123;\n" 16036 " int oneTwo = 12;\n" 16037 " method();\n" 16038 " float k = 2;\n" 16039 " int ll = 10000;\n" 16040 "}", 16041 format("void SomeFunction(int parameter= 0) {\n" 16042 " int const i= 1;\n" 16043 " int **j=2;\n" 16044 "int &k=i;\n" 16045 "int &&l=i+j;\n" 16046 " int big = 10000;\n" 16047 "\n" 16048 "unsigned oneTwoThree =123;\n" 16049 "int oneTwo = 12;\n" 16050 " method();\n" 16051 "float k= 2;\n" 16052 "int ll=10000;\n" 16053 "}", 16054 AlignmentLeft)); 16055 // variables are aligned at their name, pointers are at the left most position 16056 verifyFormat("int* a;\n" 16057 "int** b;\n" 16058 "int*** c;\n" 16059 "int foobar;\n", 16060 AlignmentLeft); 16061 16062 // PAS_Middle 16063 FormatStyle AlignmentMiddle = Alignment; 16064 AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle; 16065 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16066 " int const i = 1;\n" 16067 " int * j = 2;\n" 16068 " int big = 10000;\n" 16069 "\n" 16070 " unsigned oneTwoThree = 123;\n" 16071 " int oneTwo = 12;\n" 16072 " method();\n" 16073 " float k = 2;\n" 16074 " int ll = 10000;\n" 16075 "}", 16076 format("void SomeFunction(int parameter= 0) {\n" 16077 " int const i= 1;\n" 16078 " int *j=2;\n" 16079 " int big = 10000;\n" 16080 "\n" 16081 "unsigned oneTwoThree =123;\n" 16082 "int oneTwo = 12;\n" 16083 " method();\n" 16084 "float k= 2;\n" 16085 "int ll=10000;\n" 16086 "}", 16087 AlignmentMiddle)); 16088 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16089 " int const i = 1;\n" 16090 " int ** j = 2, ***k;\n" 16091 " int & k = i;\n" 16092 " int && l = i + j;\n" 16093 " int big = 10000;\n" 16094 "\n" 16095 " unsigned oneTwoThree = 123;\n" 16096 " int oneTwo = 12;\n" 16097 " method();\n" 16098 " float k = 2;\n" 16099 " int ll = 10000;\n" 16100 "}", 16101 format("void SomeFunction(int parameter= 0) {\n" 16102 " int const i= 1;\n" 16103 " int **j=2,***k;\n" 16104 "int &k=i;\n" 16105 "int &&l=i+j;\n" 16106 " int big = 10000;\n" 16107 "\n" 16108 "unsigned oneTwoThree =123;\n" 16109 "int oneTwo = 12;\n" 16110 " method();\n" 16111 "float k= 2;\n" 16112 "int ll=10000;\n" 16113 "}", 16114 AlignmentMiddle)); 16115 // variables are aligned at their name, pointers are in the middle 16116 verifyFormat("int * a;\n" 16117 "int * b;\n" 16118 "int *** c;\n" 16119 "int foobar;\n", 16120 AlignmentMiddle); 16121 16122 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16123 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 16124 verifyFormat("#define A \\\n" 16125 " int aaaa = 12; \\\n" 16126 " float b = 23; \\\n" 16127 " const int ccc = 234; \\\n" 16128 " unsigned dddddddddd = 2345;", 16129 Alignment); 16130 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 16131 verifyFormat("#define A \\\n" 16132 " int aaaa = 12; \\\n" 16133 " float b = 23; \\\n" 16134 " const int ccc = 234; \\\n" 16135 " unsigned dddddddddd = 2345;", 16136 Alignment); 16137 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 16138 Alignment.ColumnLimit = 30; 16139 verifyFormat("#define A \\\n" 16140 " int aaaa = 12; \\\n" 16141 " float b = 23; \\\n" 16142 " const int ccc = 234; \\\n" 16143 " int dddddddddd = 2345;", 16144 Alignment); 16145 Alignment.ColumnLimit = 80; 16146 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 16147 "k = 4, int l = 5,\n" 16148 " int m = 6) {\n" 16149 " const int j = 10;\n" 16150 " otherThing = 1;\n" 16151 "}", 16152 Alignment); 16153 verifyFormat("void SomeFunction(int parameter = 0) {\n" 16154 " int const i = 1;\n" 16155 " int *j = 2;\n" 16156 " int big = 10000;\n" 16157 "}", 16158 Alignment); 16159 verifyFormat("class C {\n" 16160 "public:\n" 16161 " int i = 1;\n" 16162 " virtual void f() = 0;\n" 16163 "};", 16164 Alignment); 16165 verifyFormat("float i = 1;\n" 16166 "if (SomeType t = getSomething()) {\n" 16167 "}\n" 16168 "const unsigned j = 2;\n" 16169 "int big = 10000;", 16170 Alignment); 16171 verifyFormat("float j = 7;\n" 16172 "for (int k = 0; k < N; ++k) {\n" 16173 "}\n" 16174 "unsigned j = 2;\n" 16175 "int big = 10000;\n" 16176 "}", 16177 Alignment); 16178 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 16179 verifyFormat("float i = 1;\n" 16180 "LooooooooooongType loooooooooooooooooooooongVariable\n" 16181 " = someLooooooooooooooooongFunction();\n" 16182 "int j = 2;", 16183 Alignment); 16184 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 16185 verifyFormat("int i = 1;\n" 16186 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 16187 " someLooooooooooooooooongFunction();\n" 16188 "int j = 2;", 16189 Alignment); 16190 16191 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16192 verifyFormat("auto lambda = []() {\n" 16193 " auto ii = 0;\n" 16194 " float j = 0;\n" 16195 " return 0;\n" 16196 "};\n" 16197 "int i = 0;\n" 16198 "float i2 = 0;\n" 16199 "auto v = type{\n" 16200 " i = 1, //\n" 16201 " (i = 2), //\n" 16202 " i = 3 //\n" 16203 "};", 16204 Alignment); 16205 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16206 16207 verifyFormat( 16208 "int i = 1;\n" 16209 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 16210 " loooooooooooooooooooooongParameterB);\n" 16211 "int j = 2;", 16212 Alignment); 16213 16214 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 16215 // We expect declarations and assignments to align, as long as it doesn't 16216 // exceed the column limit, starting a new alignment sequence whenever it 16217 // happens. 16218 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16219 Alignment.ColumnLimit = 30; 16220 verifyFormat("float ii = 1;\n" 16221 "unsigned j = 2;\n" 16222 "int someVerylongVariable = 1;\n" 16223 "AnotherLongType ll = 123456;\n" 16224 "VeryVeryLongType k = 2;\n" 16225 "int myvar = 1;", 16226 Alignment); 16227 Alignment.ColumnLimit = 80; 16228 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16229 16230 verifyFormat( 16231 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 16232 " typename LongType, typename B>\n" 16233 "auto foo() {}\n", 16234 Alignment); 16235 verifyFormat("float a, b = 1;\n" 16236 "int c = 2;\n" 16237 "int dd = 3;\n", 16238 Alignment); 16239 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 16240 "float b[1][] = {{3.f}};\n", 16241 Alignment); 16242 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16243 verifyFormat("float a, b = 1;\n" 16244 "int c = 2;\n" 16245 "int dd = 3;\n", 16246 Alignment); 16247 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 16248 "float b[1][] = {{3.f}};\n", 16249 Alignment); 16250 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16251 16252 Alignment.ColumnLimit = 30; 16253 Alignment.BinPackParameters = false; 16254 verifyFormat("void foo(float a,\n" 16255 " float b,\n" 16256 " int c,\n" 16257 " uint32_t *d) {\n" 16258 " int *e = 0;\n" 16259 " float f = 0;\n" 16260 " double g = 0;\n" 16261 "}\n" 16262 "void bar(ino_t a,\n" 16263 " int b,\n" 16264 " uint32_t *c,\n" 16265 " bool d) {}\n", 16266 Alignment); 16267 Alignment.BinPackParameters = true; 16268 Alignment.ColumnLimit = 80; 16269 16270 // Bug 33507 16271 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 16272 verifyFormat( 16273 "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n" 16274 " static const Version verVs2017;\n" 16275 " return true;\n" 16276 "});\n", 16277 Alignment); 16278 Alignment.PointerAlignment = FormatStyle::PAS_Right; 16279 16280 // See llvm.org/PR35641 16281 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16282 verifyFormat("int func() { //\n" 16283 " int b;\n" 16284 " unsigned c;\n" 16285 "}", 16286 Alignment); 16287 16288 // See PR37175 16289 FormatStyle Style = getMozillaStyle(); 16290 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16291 EXPECT_EQ("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n" 16292 "foo(int a);", 16293 format("DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style)); 16294 16295 Alignment.PointerAlignment = FormatStyle::PAS_Left; 16296 verifyFormat("unsigned int* a;\n" 16297 "int* b;\n" 16298 "unsigned int Const* c;\n" 16299 "unsigned int const* d;\n" 16300 "unsigned int Const& e;\n" 16301 "unsigned int const& f;", 16302 Alignment); 16303 verifyFormat("Const unsigned int* c;\n" 16304 "const unsigned int* d;\n" 16305 "Const unsigned int& e;\n" 16306 "const unsigned int& f;\n" 16307 "const unsigned g;\n" 16308 "Const unsigned h;", 16309 Alignment); 16310 16311 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 16312 verifyFormat("unsigned int * a;\n" 16313 "int * b;\n" 16314 "unsigned int Const * c;\n" 16315 "unsigned int const * d;\n" 16316 "unsigned int Const & e;\n" 16317 "unsigned int const & f;", 16318 Alignment); 16319 verifyFormat("Const unsigned int * c;\n" 16320 "const unsigned int * d;\n" 16321 "Const unsigned int & e;\n" 16322 "const unsigned int & f;\n" 16323 "const unsigned g;\n" 16324 "Const unsigned h;", 16325 Alignment); 16326 } 16327 16328 TEST_F(FormatTest, AlignWithLineBreaks) { 16329 auto Style = getLLVMStyleWithColumns(120); 16330 16331 EXPECT_EQ(Style.AlignConsecutiveAssignments, FormatStyle::ACS_None); 16332 EXPECT_EQ(Style.AlignConsecutiveDeclarations, FormatStyle::ACS_None); 16333 verifyFormat("void foo() {\n" 16334 " int myVar = 5;\n" 16335 " double x = 3.14;\n" 16336 " auto str = \"Hello \"\n" 16337 " \"World\";\n" 16338 " auto s = \"Hello \"\n" 16339 " \"Again\";\n" 16340 "}", 16341 Style); 16342 16343 // clang-format off 16344 verifyFormat("void foo() {\n" 16345 " const int capacityBefore = Entries.capacity();\n" 16346 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16347 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16348 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16349 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16350 "}", 16351 Style); 16352 // clang-format on 16353 16354 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16355 verifyFormat("void foo() {\n" 16356 " int myVar = 5;\n" 16357 " double x = 3.14;\n" 16358 " auto str = \"Hello \"\n" 16359 " \"World\";\n" 16360 " auto s = \"Hello \"\n" 16361 " \"Again\";\n" 16362 "}", 16363 Style); 16364 16365 // clang-format off 16366 verifyFormat("void foo() {\n" 16367 " const int capacityBefore = Entries.capacity();\n" 16368 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16369 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16370 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16371 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16372 "}", 16373 Style); 16374 // clang-format on 16375 16376 Style.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16377 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16378 verifyFormat("void foo() {\n" 16379 " int myVar = 5;\n" 16380 " double x = 3.14;\n" 16381 " auto str = \"Hello \"\n" 16382 " \"World\";\n" 16383 " auto s = \"Hello \"\n" 16384 " \"Again\";\n" 16385 "}", 16386 Style); 16387 16388 // clang-format off 16389 verifyFormat("void foo() {\n" 16390 " const int capacityBefore = Entries.capacity();\n" 16391 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16392 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16393 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16394 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16395 "}", 16396 Style); 16397 // clang-format on 16398 16399 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16400 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16401 16402 verifyFormat("void foo() {\n" 16403 " int myVar = 5;\n" 16404 " double x = 3.14;\n" 16405 " auto str = \"Hello \"\n" 16406 " \"World\";\n" 16407 " auto s = \"Hello \"\n" 16408 " \"Again\";\n" 16409 "}", 16410 Style); 16411 16412 // clang-format off 16413 verifyFormat("void foo() {\n" 16414 " const int capacityBefore = Entries.capacity();\n" 16415 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16416 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16417 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16418 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16419 "}", 16420 Style); 16421 // clang-format on 16422 16423 Style = getLLVMStyleWithColumns(120); 16424 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16425 Style.ContinuationIndentWidth = 4; 16426 Style.IndentWidth = 4; 16427 16428 // clang-format off 16429 verifyFormat("void SomeFunc() {\n" 16430 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 16431 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16432 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 16433 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16434 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 16435 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16436 "}", 16437 Style); 16438 // clang-format on 16439 16440 Style.BinPackArguments = false; 16441 16442 // clang-format off 16443 verifyFormat("void SomeFunc() {\n" 16444 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(\n" 16445 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16446 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(\n" 16447 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16448 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(\n" 16449 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16450 "}", 16451 Style); 16452 // clang-format on 16453 } 16454 16455 TEST_F(FormatTest, AlignWithInitializerPeriods) { 16456 auto Style = getLLVMStyleWithColumns(60); 16457 16458 verifyFormat("void foo1(void) {\n" 16459 " BYTE p[1] = 1;\n" 16460 " A B = {.one_foooooooooooooooo = 2,\n" 16461 " .two_fooooooooooooo = 3,\n" 16462 " .three_fooooooooooooo = 4};\n" 16463 " BYTE payload = 2;\n" 16464 "}", 16465 Style); 16466 16467 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16468 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_None; 16469 verifyFormat("void foo2(void) {\n" 16470 " BYTE p[1] = 1;\n" 16471 " A B = {.one_foooooooooooooooo = 2,\n" 16472 " .two_fooooooooooooo = 3,\n" 16473 " .three_fooooooooooooo = 4};\n" 16474 " BYTE payload = 2;\n" 16475 "}", 16476 Style); 16477 16478 Style.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16479 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16480 verifyFormat("void foo3(void) {\n" 16481 " BYTE p[1] = 1;\n" 16482 " A B = {.one_foooooooooooooooo = 2,\n" 16483 " .two_fooooooooooooo = 3,\n" 16484 " .three_fooooooooooooo = 4};\n" 16485 " BYTE payload = 2;\n" 16486 "}", 16487 Style); 16488 16489 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16490 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16491 verifyFormat("void foo4(void) {\n" 16492 " BYTE p[1] = 1;\n" 16493 " A B = {.one_foooooooooooooooo = 2,\n" 16494 " .two_fooooooooooooo = 3,\n" 16495 " .three_fooooooooooooo = 4};\n" 16496 " BYTE payload = 2;\n" 16497 "}", 16498 Style); 16499 } 16500 16501 TEST_F(FormatTest, LinuxBraceBreaking) { 16502 FormatStyle LinuxBraceStyle = getLLVMStyle(); 16503 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 16504 verifyFormat("namespace a\n" 16505 "{\n" 16506 "class A\n" 16507 "{\n" 16508 " void f()\n" 16509 " {\n" 16510 " if (true) {\n" 16511 " a();\n" 16512 " b();\n" 16513 " } else {\n" 16514 " a();\n" 16515 " }\n" 16516 " }\n" 16517 " void g() { return; }\n" 16518 "};\n" 16519 "struct B {\n" 16520 " int x;\n" 16521 "};\n" 16522 "} // namespace a\n", 16523 LinuxBraceStyle); 16524 verifyFormat("enum X {\n" 16525 " Y = 0,\n" 16526 "}\n", 16527 LinuxBraceStyle); 16528 verifyFormat("struct S {\n" 16529 " int Type;\n" 16530 " union {\n" 16531 " int x;\n" 16532 " double y;\n" 16533 " } Value;\n" 16534 " class C\n" 16535 " {\n" 16536 " MyFavoriteType Value;\n" 16537 " } Class;\n" 16538 "}\n", 16539 LinuxBraceStyle); 16540 } 16541 16542 TEST_F(FormatTest, MozillaBraceBreaking) { 16543 FormatStyle MozillaBraceStyle = getLLVMStyle(); 16544 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 16545 MozillaBraceStyle.FixNamespaceComments = false; 16546 verifyFormat("namespace a {\n" 16547 "class A\n" 16548 "{\n" 16549 " void f()\n" 16550 " {\n" 16551 " if (true) {\n" 16552 " a();\n" 16553 " b();\n" 16554 " }\n" 16555 " }\n" 16556 " void g() { return; }\n" 16557 "};\n" 16558 "enum E\n" 16559 "{\n" 16560 " A,\n" 16561 " // foo\n" 16562 " B,\n" 16563 " C\n" 16564 "};\n" 16565 "struct B\n" 16566 "{\n" 16567 " int x;\n" 16568 "};\n" 16569 "}\n", 16570 MozillaBraceStyle); 16571 verifyFormat("struct S\n" 16572 "{\n" 16573 " int Type;\n" 16574 " union\n" 16575 " {\n" 16576 " int x;\n" 16577 " double y;\n" 16578 " } Value;\n" 16579 " class C\n" 16580 " {\n" 16581 " MyFavoriteType Value;\n" 16582 " } Class;\n" 16583 "}\n", 16584 MozillaBraceStyle); 16585 } 16586 16587 TEST_F(FormatTest, StroustrupBraceBreaking) { 16588 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 16589 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 16590 verifyFormat("namespace a {\n" 16591 "class A {\n" 16592 " void f()\n" 16593 " {\n" 16594 " if (true) {\n" 16595 " a();\n" 16596 " b();\n" 16597 " }\n" 16598 " }\n" 16599 " void g() { return; }\n" 16600 "};\n" 16601 "struct B {\n" 16602 " int x;\n" 16603 "};\n" 16604 "} // namespace a\n", 16605 StroustrupBraceStyle); 16606 16607 verifyFormat("void foo()\n" 16608 "{\n" 16609 " if (a) {\n" 16610 " a();\n" 16611 " }\n" 16612 " else {\n" 16613 " b();\n" 16614 " }\n" 16615 "}\n", 16616 StroustrupBraceStyle); 16617 16618 verifyFormat("#ifdef _DEBUG\n" 16619 "int foo(int i = 0)\n" 16620 "#else\n" 16621 "int foo(int i = 5)\n" 16622 "#endif\n" 16623 "{\n" 16624 " return i;\n" 16625 "}", 16626 StroustrupBraceStyle); 16627 16628 verifyFormat("void foo() {}\n" 16629 "void bar()\n" 16630 "#ifdef _DEBUG\n" 16631 "{\n" 16632 " foo();\n" 16633 "}\n" 16634 "#else\n" 16635 "{\n" 16636 "}\n" 16637 "#endif", 16638 StroustrupBraceStyle); 16639 16640 verifyFormat("void foobar() { int i = 5; }\n" 16641 "#ifdef _DEBUG\n" 16642 "void bar() {}\n" 16643 "#else\n" 16644 "void bar() { foobar(); }\n" 16645 "#endif", 16646 StroustrupBraceStyle); 16647 } 16648 16649 TEST_F(FormatTest, AllmanBraceBreaking) { 16650 FormatStyle AllmanBraceStyle = getLLVMStyle(); 16651 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 16652 16653 EXPECT_EQ("namespace a\n" 16654 "{\n" 16655 "void f();\n" 16656 "void g();\n" 16657 "} // namespace a\n", 16658 format("namespace a\n" 16659 "{\n" 16660 "void f();\n" 16661 "void g();\n" 16662 "}\n", 16663 AllmanBraceStyle)); 16664 16665 verifyFormat("namespace a\n" 16666 "{\n" 16667 "class A\n" 16668 "{\n" 16669 " void f()\n" 16670 " {\n" 16671 " if (true)\n" 16672 " {\n" 16673 " a();\n" 16674 " b();\n" 16675 " }\n" 16676 " }\n" 16677 " void g() { return; }\n" 16678 "};\n" 16679 "struct B\n" 16680 "{\n" 16681 " int x;\n" 16682 "};\n" 16683 "union C\n" 16684 "{\n" 16685 "};\n" 16686 "} // namespace a", 16687 AllmanBraceStyle); 16688 16689 verifyFormat("void f()\n" 16690 "{\n" 16691 " if (true)\n" 16692 " {\n" 16693 " a();\n" 16694 " }\n" 16695 " else if (false)\n" 16696 " {\n" 16697 " b();\n" 16698 " }\n" 16699 " else\n" 16700 " {\n" 16701 " c();\n" 16702 " }\n" 16703 "}\n", 16704 AllmanBraceStyle); 16705 16706 verifyFormat("void f()\n" 16707 "{\n" 16708 " for (int i = 0; i < 10; ++i)\n" 16709 " {\n" 16710 " a();\n" 16711 " }\n" 16712 " while (false)\n" 16713 " {\n" 16714 " b();\n" 16715 " }\n" 16716 " do\n" 16717 " {\n" 16718 " c();\n" 16719 " } while (false)\n" 16720 "}\n", 16721 AllmanBraceStyle); 16722 16723 verifyFormat("void f(int a)\n" 16724 "{\n" 16725 " switch (a)\n" 16726 " {\n" 16727 " case 0:\n" 16728 " break;\n" 16729 " case 1:\n" 16730 " {\n" 16731 " break;\n" 16732 " }\n" 16733 " case 2:\n" 16734 " {\n" 16735 " }\n" 16736 " break;\n" 16737 " default:\n" 16738 " break;\n" 16739 " }\n" 16740 "}\n", 16741 AllmanBraceStyle); 16742 16743 verifyFormat("enum X\n" 16744 "{\n" 16745 " Y = 0,\n" 16746 "}\n", 16747 AllmanBraceStyle); 16748 verifyFormat("enum X\n" 16749 "{\n" 16750 " Y = 0\n" 16751 "}\n", 16752 AllmanBraceStyle); 16753 16754 verifyFormat("@interface BSApplicationController ()\n" 16755 "{\n" 16756 "@private\n" 16757 " id _extraIvar;\n" 16758 "}\n" 16759 "@end\n", 16760 AllmanBraceStyle); 16761 16762 verifyFormat("#ifdef _DEBUG\n" 16763 "int foo(int i = 0)\n" 16764 "#else\n" 16765 "int foo(int i = 5)\n" 16766 "#endif\n" 16767 "{\n" 16768 " return i;\n" 16769 "}", 16770 AllmanBraceStyle); 16771 16772 verifyFormat("void foo() {}\n" 16773 "void bar()\n" 16774 "#ifdef _DEBUG\n" 16775 "{\n" 16776 " foo();\n" 16777 "}\n" 16778 "#else\n" 16779 "{\n" 16780 "}\n" 16781 "#endif", 16782 AllmanBraceStyle); 16783 16784 verifyFormat("void foobar() { int i = 5; }\n" 16785 "#ifdef _DEBUG\n" 16786 "void bar() {}\n" 16787 "#else\n" 16788 "void bar() { foobar(); }\n" 16789 "#endif", 16790 AllmanBraceStyle); 16791 16792 EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine, 16793 FormatStyle::SLS_All); 16794 16795 verifyFormat("[](int i) { return i + 2; };\n" 16796 "[](int i, int j)\n" 16797 "{\n" 16798 " auto x = i + j;\n" 16799 " auto y = i * j;\n" 16800 " return x ^ y;\n" 16801 "};\n" 16802 "void foo()\n" 16803 "{\n" 16804 " auto shortLambda = [](int i) { return i + 2; };\n" 16805 " auto longLambda = [](int i, int j)\n" 16806 " {\n" 16807 " auto x = i + j;\n" 16808 " auto y = i * j;\n" 16809 " return x ^ y;\n" 16810 " };\n" 16811 "}", 16812 AllmanBraceStyle); 16813 16814 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 16815 16816 verifyFormat("[](int i)\n" 16817 "{\n" 16818 " return i + 2;\n" 16819 "};\n" 16820 "[](int i, int j)\n" 16821 "{\n" 16822 " auto x = i + j;\n" 16823 " auto y = i * j;\n" 16824 " return x ^ y;\n" 16825 "};\n" 16826 "void foo()\n" 16827 "{\n" 16828 " auto shortLambda = [](int i)\n" 16829 " {\n" 16830 " return i + 2;\n" 16831 " };\n" 16832 " auto longLambda = [](int i, int j)\n" 16833 " {\n" 16834 " auto x = i + j;\n" 16835 " auto y = i * j;\n" 16836 " return x ^ y;\n" 16837 " };\n" 16838 "}", 16839 AllmanBraceStyle); 16840 16841 // Reset 16842 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All; 16843 16844 // This shouldn't affect ObjC blocks.. 16845 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 16846 " // ...\n" 16847 " int i;\n" 16848 "}];", 16849 AllmanBraceStyle); 16850 verifyFormat("void (^block)(void) = ^{\n" 16851 " // ...\n" 16852 " int i;\n" 16853 "};", 16854 AllmanBraceStyle); 16855 // .. or dict literals. 16856 verifyFormat("void f()\n" 16857 "{\n" 16858 " // ...\n" 16859 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 16860 "}", 16861 AllmanBraceStyle); 16862 verifyFormat("void f()\n" 16863 "{\n" 16864 " // ...\n" 16865 " [object someMethod:@{a : @\"b\"}];\n" 16866 "}", 16867 AllmanBraceStyle); 16868 verifyFormat("int f()\n" 16869 "{ // comment\n" 16870 " return 42;\n" 16871 "}", 16872 AllmanBraceStyle); 16873 16874 AllmanBraceStyle.ColumnLimit = 19; 16875 verifyFormat("void f() { int i; }", AllmanBraceStyle); 16876 AllmanBraceStyle.ColumnLimit = 18; 16877 verifyFormat("void f()\n" 16878 "{\n" 16879 " int i;\n" 16880 "}", 16881 AllmanBraceStyle); 16882 AllmanBraceStyle.ColumnLimit = 80; 16883 16884 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 16885 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = 16886 FormatStyle::SIS_WithoutElse; 16887 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 16888 verifyFormat("void f(bool b)\n" 16889 "{\n" 16890 " if (b)\n" 16891 " {\n" 16892 " return;\n" 16893 " }\n" 16894 "}\n", 16895 BreakBeforeBraceShortIfs); 16896 verifyFormat("void f(bool b)\n" 16897 "{\n" 16898 " if constexpr (b)\n" 16899 " {\n" 16900 " return;\n" 16901 " }\n" 16902 "}\n", 16903 BreakBeforeBraceShortIfs); 16904 verifyFormat("void f(bool b)\n" 16905 "{\n" 16906 " if CONSTEXPR (b)\n" 16907 " {\n" 16908 " return;\n" 16909 " }\n" 16910 "}\n", 16911 BreakBeforeBraceShortIfs); 16912 verifyFormat("void f(bool b)\n" 16913 "{\n" 16914 " if (b) return;\n" 16915 "}\n", 16916 BreakBeforeBraceShortIfs); 16917 verifyFormat("void f(bool b)\n" 16918 "{\n" 16919 " if constexpr (b) return;\n" 16920 "}\n", 16921 BreakBeforeBraceShortIfs); 16922 verifyFormat("void f(bool b)\n" 16923 "{\n" 16924 " if CONSTEXPR (b) return;\n" 16925 "}\n", 16926 BreakBeforeBraceShortIfs); 16927 verifyFormat("void f(bool b)\n" 16928 "{\n" 16929 " while (b)\n" 16930 " {\n" 16931 " return;\n" 16932 " }\n" 16933 "}\n", 16934 BreakBeforeBraceShortIfs); 16935 } 16936 16937 TEST_F(FormatTest, WhitesmithsBraceBreaking) { 16938 FormatStyle WhitesmithsBraceStyle = getLLVMStyle(); 16939 WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 16940 16941 // Make a few changes to the style for testing purposes 16942 WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine = 16943 FormatStyle::SFS_Empty; 16944 WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 16945 WhitesmithsBraceStyle.ColumnLimit = 0; 16946 16947 // FIXME: this test case can't decide whether there should be a blank line 16948 // after the ~D() line or not. It adds one if one doesn't exist in the test 16949 // and it removes the line if one exists. 16950 /* 16951 verifyFormat("class A;\n" 16952 "namespace B\n" 16953 " {\n" 16954 "class C;\n" 16955 "// Comment\n" 16956 "class D\n" 16957 " {\n" 16958 "public:\n" 16959 " D();\n" 16960 " ~D() {}\n" 16961 "private:\n" 16962 " enum E\n" 16963 " {\n" 16964 " F\n" 16965 " }\n" 16966 " };\n" 16967 " } // namespace B\n", 16968 WhitesmithsBraceStyle); 16969 */ 16970 16971 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None; 16972 verifyFormat("namespace a\n" 16973 " {\n" 16974 "class A\n" 16975 " {\n" 16976 " void f()\n" 16977 " {\n" 16978 " if (true)\n" 16979 " {\n" 16980 " a();\n" 16981 " b();\n" 16982 " }\n" 16983 " }\n" 16984 " void g()\n" 16985 " {\n" 16986 " return;\n" 16987 " }\n" 16988 " };\n" 16989 "struct B\n" 16990 " {\n" 16991 " int x;\n" 16992 " };\n" 16993 " } // namespace a", 16994 WhitesmithsBraceStyle); 16995 16996 verifyFormat("namespace a\n" 16997 " {\n" 16998 "namespace b\n" 16999 " {\n" 17000 "class A\n" 17001 " {\n" 17002 " void f()\n" 17003 " {\n" 17004 " if (true)\n" 17005 " {\n" 17006 " a();\n" 17007 " b();\n" 17008 " }\n" 17009 " }\n" 17010 " void g()\n" 17011 " {\n" 17012 " return;\n" 17013 " }\n" 17014 " };\n" 17015 "struct B\n" 17016 " {\n" 17017 " int x;\n" 17018 " };\n" 17019 " } // namespace b\n" 17020 " } // namespace a", 17021 WhitesmithsBraceStyle); 17022 17023 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner; 17024 verifyFormat("namespace a\n" 17025 " {\n" 17026 "namespace b\n" 17027 " {\n" 17028 " class A\n" 17029 " {\n" 17030 " void f()\n" 17031 " {\n" 17032 " if (true)\n" 17033 " {\n" 17034 " a();\n" 17035 " b();\n" 17036 " }\n" 17037 " }\n" 17038 " void g()\n" 17039 " {\n" 17040 " return;\n" 17041 " }\n" 17042 " };\n" 17043 " struct B\n" 17044 " {\n" 17045 " int x;\n" 17046 " };\n" 17047 " } // namespace b\n" 17048 " } // namespace a", 17049 WhitesmithsBraceStyle); 17050 17051 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All; 17052 verifyFormat("namespace a\n" 17053 " {\n" 17054 " namespace b\n" 17055 " {\n" 17056 " class A\n" 17057 " {\n" 17058 " void f()\n" 17059 " {\n" 17060 " if (true)\n" 17061 " {\n" 17062 " a();\n" 17063 " b();\n" 17064 " }\n" 17065 " }\n" 17066 " void g()\n" 17067 " {\n" 17068 " return;\n" 17069 " }\n" 17070 " };\n" 17071 " struct B\n" 17072 " {\n" 17073 " int x;\n" 17074 " };\n" 17075 " } // namespace b\n" 17076 " } // namespace a", 17077 WhitesmithsBraceStyle); 17078 17079 verifyFormat("void f()\n" 17080 " {\n" 17081 " if (true)\n" 17082 " {\n" 17083 " a();\n" 17084 " }\n" 17085 " else if (false)\n" 17086 " {\n" 17087 " b();\n" 17088 " }\n" 17089 " else\n" 17090 " {\n" 17091 " c();\n" 17092 " }\n" 17093 " }\n", 17094 WhitesmithsBraceStyle); 17095 17096 verifyFormat("void f()\n" 17097 " {\n" 17098 " for (int i = 0; i < 10; ++i)\n" 17099 " {\n" 17100 " a();\n" 17101 " }\n" 17102 " while (false)\n" 17103 " {\n" 17104 " b();\n" 17105 " }\n" 17106 " do\n" 17107 " {\n" 17108 " c();\n" 17109 " } while (false)\n" 17110 " }\n", 17111 WhitesmithsBraceStyle); 17112 17113 WhitesmithsBraceStyle.IndentCaseLabels = true; 17114 verifyFormat("void switchTest1(int a)\n" 17115 " {\n" 17116 " switch (a)\n" 17117 " {\n" 17118 " case 2:\n" 17119 " {\n" 17120 " }\n" 17121 " break;\n" 17122 " }\n" 17123 " }\n", 17124 WhitesmithsBraceStyle); 17125 17126 verifyFormat("void switchTest2(int a)\n" 17127 " {\n" 17128 " switch (a)\n" 17129 " {\n" 17130 " case 0:\n" 17131 " break;\n" 17132 " case 1:\n" 17133 " {\n" 17134 " break;\n" 17135 " }\n" 17136 " case 2:\n" 17137 " {\n" 17138 " }\n" 17139 " break;\n" 17140 " default:\n" 17141 " break;\n" 17142 " }\n" 17143 " }\n", 17144 WhitesmithsBraceStyle); 17145 17146 verifyFormat("void switchTest3(int a)\n" 17147 " {\n" 17148 " switch (a)\n" 17149 " {\n" 17150 " case 0:\n" 17151 " {\n" 17152 " foo(x);\n" 17153 " }\n" 17154 " break;\n" 17155 " default:\n" 17156 " {\n" 17157 " foo(1);\n" 17158 " }\n" 17159 " break;\n" 17160 " }\n" 17161 " }\n", 17162 WhitesmithsBraceStyle); 17163 17164 WhitesmithsBraceStyle.IndentCaseLabels = false; 17165 17166 verifyFormat("void switchTest4(int a)\n" 17167 " {\n" 17168 " switch (a)\n" 17169 " {\n" 17170 " case 2:\n" 17171 " {\n" 17172 " }\n" 17173 " break;\n" 17174 " }\n" 17175 " }\n", 17176 WhitesmithsBraceStyle); 17177 17178 verifyFormat("void switchTest5(int a)\n" 17179 " {\n" 17180 " switch (a)\n" 17181 " {\n" 17182 " case 0:\n" 17183 " break;\n" 17184 " case 1:\n" 17185 " {\n" 17186 " foo();\n" 17187 " break;\n" 17188 " }\n" 17189 " case 2:\n" 17190 " {\n" 17191 " }\n" 17192 " break;\n" 17193 " default:\n" 17194 " break;\n" 17195 " }\n" 17196 " }\n", 17197 WhitesmithsBraceStyle); 17198 17199 verifyFormat("void switchTest6(int a)\n" 17200 " {\n" 17201 " switch (a)\n" 17202 " {\n" 17203 " case 0:\n" 17204 " {\n" 17205 " foo(x);\n" 17206 " }\n" 17207 " break;\n" 17208 " default:\n" 17209 " {\n" 17210 " foo(1);\n" 17211 " }\n" 17212 " break;\n" 17213 " }\n" 17214 " }\n", 17215 WhitesmithsBraceStyle); 17216 17217 verifyFormat("enum X\n" 17218 " {\n" 17219 " Y = 0, // testing\n" 17220 " }\n", 17221 WhitesmithsBraceStyle); 17222 17223 verifyFormat("enum X\n" 17224 " {\n" 17225 " Y = 0\n" 17226 " }\n", 17227 WhitesmithsBraceStyle); 17228 verifyFormat("enum X\n" 17229 " {\n" 17230 " Y = 0,\n" 17231 " Z = 1\n" 17232 " };\n", 17233 WhitesmithsBraceStyle); 17234 17235 verifyFormat("@interface BSApplicationController ()\n" 17236 " {\n" 17237 "@private\n" 17238 " id _extraIvar;\n" 17239 " }\n" 17240 "@end\n", 17241 WhitesmithsBraceStyle); 17242 17243 verifyFormat("#ifdef _DEBUG\n" 17244 "int foo(int i = 0)\n" 17245 "#else\n" 17246 "int foo(int i = 5)\n" 17247 "#endif\n" 17248 " {\n" 17249 " return i;\n" 17250 " }", 17251 WhitesmithsBraceStyle); 17252 17253 verifyFormat("void foo() {}\n" 17254 "void bar()\n" 17255 "#ifdef _DEBUG\n" 17256 " {\n" 17257 " foo();\n" 17258 " }\n" 17259 "#else\n" 17260 " {\n" 17261 " }\n" 17262 "#endif", 17263 WhitesmithsBraceStyle); 17264 17265 verifyFormat("void foobar()\n" 17266 " {\n" 17267 " int i = 5;\n" 17268 " }\n" 17269 "#ifdef _DEBUG\n" 17270 "void bar()\n" 17271 " {\n" 17272 " }\n" 17273 "#else\n" 17274 "void bar()\n" 17275 " {\n" 17276 " foobar();\n" 17277 " }\n" 17278 "#endif", 17279 WhitesmithsBraceStyle); 17280 17281 // This shouldn't affect ObjC blocks.. 17282 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 17283 " // ...\n" 17284 " int i;\n" 17285 "}];", 17286 WhitesmithsBraceStyle); 17287 verifyFormat("void (^block)(void) = ^{\n" 17288 " // ...\n" 17289 " int i;\n" 17290 "};", 17291 WhitesmithsBraceStyle); 17292 // .. or dict literals. 17293 verifyFormat("void f()\n" 17294 " {\n" 17295 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 17296 " }", 17297 WhitesmithsBraceStyle); 17298 17299 verifyFormat("int f()\n" 17300 " { // comment\n" 17301 " return 42;\n" 17302 " }", 17303 WhitesmithsBraceStyle); 17304 17305 FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle; 17306 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = 17307 FormatStyle::SIS_OnlyFirstIf; 17308 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 17309 verifyFormat("void f(bool b)\n" 17310 " {\n" 17311 " if (b)\n" 17312 " {\n" 17313 " return;\n" 17314 " }\n" 17315 " }\n", 17316 BreakBeforeBraceShortIfs); 17317 verifyFormat("void f(bool b)\n" 17318 " {\n" 17319 " if (b) return;\n" 17320 " }\n", 17321 BreakBeforeBraceShortIfs); 17322 verifyFormat("void f(bool b)\n" 17323 " {\n" 17324 " while (b)\n" 17325 " {\n" 17326 " return;\n" 17327 " }\n" 17328 " }\n", 17329 BreakBeforeBraceShortIfs); 17330 } 17331 17332 TEST_F(FormatTest, GNUBraceBreaking) { 17333 FormatStyle GNUBraceStyle = getLLVMStyle(); 17334 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 17335 verifyFormat("namespace a\n" 17336 "{\n" 17337 "class A\n" 17338 "{\n" 17339 " void f()\n" 17340 " {\n" 17341 " int a;\n" 17342 " {\n" 17343 " int b;\n" 17344 " }\n" 17345 " if (true)\n" 17346 " {\n" 17347 " a();\n" 17348 " b();\n" 17349 " }\n" 17350 " }\n" 17351 " void g() { return; }\n" 17352 "}\n" 17353 "} // namespace a", 17354 GNUBraceStyle); 17355 17356 verifyFormat("void f()\n" 17357 "{\n" 17358 " if (true)\n" 17359 " {\n" 17360 " a();\n" 17361 " }\n" 17362 " else if (false)\n" 17363 " {\n" 17364 " b();\n" 17365 " }\n" 17366 " else\n" 17367 " {\n" 17368 " c();\n" 17369 " }\n" 17370 "}\n", 17371 GNUBraceStyle); 17372 17373 verifyFormat("void f()\n" 17374 "{\n" 17375 " for (int i = 0; i < 10; ++i)\n" 17376 " {\n" 17377 " a();\n" 17378 " }\n" 17379 " while (false)\n" 17380 " {\n" 17381 " b();\n" 17382 " }\n" 17383 " do\n" 17384 " {\n" 17385 " c();\n" 17386 " }\n" 17387 " while (false);\n" 17388 "}\n", 17389 GNUBraceStyle); 17390 17391 verifyFormat("void f(int a)\n" 17392 "{\n" 17393 " switch (a)\n" 17394 " {\n" 17395 " case 0:\n" 17396 " break;\n" 17397 " case 1:\n" 17398 " {\n" 17399 " break;\n" 17400 " }\n" 17401 " case 2:\n" 17402 " {\n" 17403 " }\n" 17404 " break;\n" 17405 " default:\n" 17406 " break;\n" 17407 " }\n" 17408 "}\n", 17409 GNUBraceStyle); 17410 17411 verifyFormat("enum X\n" 17412 "{\n" 17413 " Y = 0,\n" 17414 "}\n", 17415 GNUBraceStyle); 17416 17417 verifyFormat("@interface BSApplicationController ()\n" 17418 "{\n" 17419 "@private\n" 17420 " id _extraIvar;\n" 17421 "}\n" 17422 "@end\n", 17423 GNUBraceStyle); 17424 17425 verifyFormat("#ifdef _DEBUG\n" 17426 "int foo(int i = 0)\n" 17427 "#else\n" 17428 "int foo(int i = 5)\n" 17429 "#endif\n" 17430 "{\n" 17431 " return i;\n" 17432 "}", 17433 GNUBraceStyle); 17434 17435 verifyFormat("void foo() {}\n" 17436 "void bar()\n" 17437 "#ifdef _DEBUG\n" 17438 "{\n" 17439 " foo();\n" 17440 "}\n" 17441 "#else\n" 17442 "{\n" 17443 "}\n" 17444 "#endif", 17445 GNUBraceStyle); 17446 17447 verifyFormat("void foobar() { int i = 5; }\n" 17448 "#ifdef _DEBUG\n" 17449 "void bar() {}\n" 17450 "#else\n" 17451 "void bar() { foobar(); }\n" 17452 "#endif", 17453 GNUBraceStyle); 17454 } 17455 17456 TEST_F(FormatTest, WebKitBraceBreaking) { 17457 FormatStyle WebKitBraceStyle = getLLVMStyle(); 17458 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 17459 WebKitBraceStyle.FixNamespaceComments = false; 17460 verifyFormat("namespace a {\n" 17461 "class A {\n" 17462 " void f()\n" 17463 " {\n" 17464 " if (true) {\n" 17465 " a();\n" 17466 " b();\n" 17467 " }\n" 17468 " }\n" 17469 " void g() { return; }\n" 17470 "};\n" 17471 "enum E {\n" 17472 " A,\n" 17473 " // foo\n" 17474 " B,\n" 17475 " C\n" 17476 "};\n" 17477 "struct B {\n" 17478 " int x;\n" 17479 "};\n" 17480 "}\n", 17481 WebKitBraceStyle); 17482 verifyFormat("struct S {\n" 17483 " int Type;\n" 17484 " union {\n" 17485 " int x;\n" 17486 " double y;\n" 17487 " } Value;\n" 17488 " class C {\n" 17489 " MyFavoriteType Value;\n" 17490 " } Class;\n" 17491 "};\n", 17492 WebKitBraceStyle); 17493 } 17494 17495 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 17496 verifyFormat("void f() {\n" 17497 " try {\n" 17498 " } catch (const Exception &e) {\n" 17499 " }\n" 17500 "}\n", 17501 getLLVMStyle()); 17502 } 17503 17504 TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) { 17505 auto Style = getLLVMStyle(); 17506 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 17507 Style.AlignConsecutiveAssignments = 17508 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 17509 Style.AlignConsecutiveDeclarations = 17510 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 17511 verifyFormat("struct test demo[] = {\n" 17512 " {56, 23, \"hello\"},\n" 17513 " {-1, 93463, \"world\"},\n" 17514 " { 7, 5, \"!!\"}\n" 17515 "};\n", 17516 Style); 17517 17518 verifyFormat("struct test demo[] = {\n" 17519 " {56, 23, \"hello\"}, // first line\n" 17520 " {-1, 93463, \"world\"}, // second line\n" 17521 " { 7, 5, \"!!\"} // third line\n" 17522 "};\n", 17523 Style); 17524 17525 verifyFormat("struct test demo[4] = {\n" 17526 " { 56, 23, 21, \"oh\"}, // first line\n" 17527 " { -1, 93463, 22, \"my\"}, // second line\n" 17528 " { 7, 5, 1, \"goodness\"} // third line\n" 17529 " {234, 5, 1, \"gracious\"} // fourth line\n" 17530 "};\n", 17531 Style); 17532 17533 verifyFormat("struct test demo[3] = {\n" 17534 " {56, 23, \"hello\"},\n" 17535 " {-1, 93463, \"world\"},\n" 17536 " { 7, 5, \"!!\"}\n" 17537 "};\n", 17538 Style); 17539 17540 verifyFormat("struct test demo[3] = {\n" 17541 " {int{56}, 23, \"hello\"},\n" 17542 " {int{-1}, 93463, \"world\"},\n" 17543 " { int{7}, 5, \"!!\"}\n" 17544 "};\n", 17545 Style); 17546 17547 verifyFormat("struct test demo[] = {\n" 17548 " {56, 23, \"hello\"},\n" 17549 " {-1, 93463, \"world\"},\n" 17550 " { 7, 5, \"!!\"},\n" 17551 "};\n", 17552 Style); 17553 17554 verifyFormat("test demo[] = {\n" 17555 " {56, 23, \"hello\"},\n" 17556 " {-1, 93463, \"world\"},\n" 17557 " { 7, 5, \"!!\"},\n" 17558 "};\n", 17559 Style); 17560 17561 verifyFormat("demo = std::array<struct test, 3>{\n" 17562 " test{56, 23, \"hello\"},\n" 17563 " test{-1, 93463, \"world\"},\n" 17564 " test{ 7, 5, \"!!\"},\n" 17565 "};\n", 17566 Style); 17567 17568 verifyFormat("test demo[] = {\n" 17569 " {56, 23, \"hello\"},\n" 17570 "#if X\n" 17571 " {-1, 93463, \"world\"},\n" 17572 "#endif\n" 17573 " { 7, 5, \"!!\"}\n" 17574 "};\n", 17575 Style); 17576 17577 verifyFormat( 17578 "test demo[] = {\n" 17579 " { 7, 23,\n" 17580 " \"hello world i am a very long line that really, in any\"\n" 17581 " \"just world, ought to be split over multiple lines\"},\n" 17582 " {-1, 93463, \"world\"},\n" 17583 " {56, 5, \"!!\"}\n" 17584 "};\n", 17585 Style); 17586 17587 verifyFormat("return GradForUnaryCwise(g, {\n" 17588 " {{\"sign\"}, \"Sign\", " 17589 " {\"x\", \"dy\"}},\n" 17590 " { {\"dx\"}, \"Mul\", {\"dy\"" 17591 ", \"sign\"}},\n" 17592 "});\n", 17593 Style); 17594 17595 Style.ColumnLimit = 0; 17596 EXPECT_EQ( 17597 "test demo[] = {\n" 17598 " {56, 23, \"hello world i am a very long line that really, " 17599 "in any just world, ought to be split over multiple lines\"},\n" 17600 " {-1, 93463, " 17601 " \"world\"},\n" 17602 " { 7, 5, " 17603 " \"!!\"},\n" 17604 "};", 17605 format("test demo[] = {{56, 23, \"hello world i am a very long line " 17606 "that really, in any just world, ought to be split over multiple " 17607 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 17608 Style)); 17609 17610 Style.ColumnLimit = 80; 17611 verifyFormat("test demo[] = {\n" 17612 " {56, 23, /* a comment */ \"hello\"},\n" 17613 " {-1, 93463, \"world\"},\n" 17614 " { 7, 5, \"!!\"}\n" 17615 "};\n", 17616 Style); 17617 17618 verifyFormat("test demo[] = {\n" 17619 " {56, 23, \"hello\"},\n" 17620 " {-1, 93463, \"world\" /* comment here */},\n" 17621 " { 7, 5, \"!!\"}\n" 17622 "};\n", 17623 Style); 17624 17625 verifyFormat("test demo[] = {\n" 17626 " {56, /* a comment */ 23, \"hello\"},\n" 17627 " {-1, 93463, \"world\"},\n" 17628 " { 7, 5, \"!!\"}\n" 17629 "};\n", 17630 Style); 17631 17632 Style.ColumnLimit = 20; 17633 EXPECT_EQ( 17634 "demo = std::array<\n" 17635 " struct test, 3>{\n" 17636 " test{\n" 17637 " 56, 23,\n" 17638 " \"hello \"\n" 17639 " \"world i \"\n" 17640 " \"am a very \"\n" 17641 " \"long line \"\n" 17642 " \"that \"\n" 17643 " \"really, \"\n" 17644 " \"in any \"\n" 17645 " \"just \"\n" 17646 " \"world, \"\n" 17647 " \"ought to \"\n" 17648 " \"be split \"\n" 17649 " \"over \"\n" 17650 " \"multiple \"\n" 17651 " \"lines\"},\n" 17652 " test{-1, 93463,\n" 17653 " \"world\"},\n" 17654 " test{ 7, 5,\n" 17655 " \"!!\" },\n" 17656 "};", 17657 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world " 17658 "i am a very long line that really, in any just world, ought " 17659 "to be split over multiple lines\"},test{-1, 93463, \"world\"}," 17660 "test{7, 5, \"!!\"},};", 17661 Style)); 17662 // This caused a core dump by enabling Alignment in the LLVMStyle globally 17663 Style = getLLVMStyleWithColumns(50); 17664 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 17665 verifyFormat("static A x = {\n" 17666 " {{init1, init2, init3, init4},\n" 17667 " {init1, init2, init3, init4}}\n" 17668 "};", 17669 Style); 17670 Style.ColumnLimit = 100; 17671 EXPECT_EQ( 17672 "test demo[] = {\n" 17673 " {56, 23,\n" 17674 " \"hello world i am a very long line that really, in any just world" 17675 ", ought to be split over \"\n" 17676 " \"multiple lines\" },\n" 17677 " {-1, 93463, \"world\"},\n" 17678 " { 7, 5, \"!!\"},\n" 17679 "};", 17680 format("test demo[] = {{56, 23, \"hello world i am a very long line " 17681 "that really, in any just world, ought to be split over multiple " 17682 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 17683 Style)); 17684 17685 Style = getLLVMStyleWithColumns(50); 17686 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 17687 Style.AlignConsecutiveAssignments = 17688 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 17689 Style.AlignConsecutiveDeclarations = 17690 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 17691 verifyFormat("struct test demo[] = {\n" 17692 " {56, 23, \"hello\"},\n" 17693 " {-1, 93463, \"world\"},\n" 17694 " { 7, 5, \"!!\"}\n" 17695 "};\n" 17696 "static A x = {\n" 17697 " {{init1, init2, init3, init4},\n" 17698 " {init1, init2, init3, init4}}\n" 17699 "};", 17700 Style); 17701 Style.ColumnLimit = 100; 17702 Style.AlignConsecutiveAssignments = 17703 FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments; 17704 Style.AlignConsecutiveDeclarations = 17705 FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments; 17706 verifyFormat("struct test demo[] = {\n" 17707 " {56, 23, \"hello\"},\n" 17708 " {-1, 93463, \"world\"},\n" 17709 " { 7, 5, \"!!\"}\n" 17710 "};\n" 17711 "struct test demo[4] = {\n" 17712 " { 56, 23, 21, \"oh\"}, // first line\n" 17713 " { -1, 93463, 22, \"my\"}, // second line\n" 17714 " { 7, 5, 1, \"goodness\"} // third line\n" 17715 " {234, 5, 1, \"gracious\"} // fourth line\n" 17716 "};\n", 17717 Style); 17718 EXPECT_EQ( 17719 "test demo[] = {\n" 17720 " {56,\n" 17721 " \"hello world i am a very long line that really, in any just world" 17722 ", ought to be split over \"\n" 17723 " \"multiple lines\", 23},\n" 17724 " {-1, \"world\", 93463},\n" 17725 " { 7, \"!!\", 5},\n" 17726 "};", 17727 format("test demo[] = {{56, \"hello world i am a very long line " 17728 "that really, in any just world, ought to be split over multiple " 17729 "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};", 17730 Style)); 17731 } 17732 17733 TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) { 17734 auto Style = getLLVMStyle(); 17735 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; 17736 verifyFormat("struct test demo[] = {\n" 17737 " {56, 23, \"hello\"},\n" 17738 " {-1, 93463, \"world\"},\n" 17739 " {7, 5, \"!!\" }\n" 17740 "};\n", 17741 Style); 17742 17743 verifyFormat("struct test demo[] = {\n" 17744 " {56, 23, \"hello\"}, // first line\n" 17745 " {-1, 93463, \"world\"}, // second line\n" 17746 " {7, 5, \"!!\" } // third line\n" 17747 "};\n", 17748 Style); 17749 verifyFormat("struct test demo[4] = {\n" 17750 " {56, 23, 21, \"oh\" }, // first line\n" 17751 " {-1, 93463, 22, \"my\" }, // second line\n" 17752 " {7, 5, 1, \"goodness\"} // third line\n" 17753 " {234, 5, 1, \"gracious\"} // fourth line\n" 17754 "};\n", 17755 Style); 17756 verifyFormat("struct test demo[3] = {\n" 17757 " {56, 23, \"hello\"},\n" 17758 " {-1, 93463, \"world\"},\n" 17759 " {7, 5, \"!!\" }\n" 17760 "};\n", 17761 Style); 17762 17763 verifyFormat("struct test demo[3] = {\n" 17764 " {int{56}, 23, \"hello\"},\n" 17765 " {int{-1}, 93463, \"world\"},\n" 17766 " {int{7}, 5, \"!!\" }\n" 17767 "};\n", 17768 Style); 17769 verifyFormat("struct test demo[] = {\n" 17770 " {56, 23, \"hello\"},\n" 17771 " {-1, 93463, \"world\"},\n" 17772 " {7, 5, \"!!\" },\n" 17773 "};\n", 17774 Style); 17775 verifyFormat("test demo[] = {\n" 17776 " {56, 23, \"hello\"},\n" 17777 " {-1, 93463, \"world\"},\n" 17778 " {7, 5, \"!!\" },\n" 17779 "};\n", 17780 Style); 17781 verifyFormat("demo = std::array<struct test, 3>{\n" 17782 " test{56, 23, \"hello\"},\n" 17783 " test{-1, 93463, \"world\"},\n" 17784 " test{7, 5, \"!!\" },\n" 17785 "};\n", 17786 Style); 17787 verifyFormat("test demo[] = {\n" 17788 " {56, 23, \"hello\"},\n" 17789 "#if X\n" 17790 " {-1, 93463, \"world\"},\n" 17791 "#endif\n" 17792 " {7, 5, \"!!\" }\n" 17793 "};\n", 17794 Style); 17795 verifyFormat( 17796 "test demo[] = {\n" 17797 " {7, 23,\n" 17798 " \"hello world i am a very long line that really, in any\"\n" 17799 " \"just world, ought to be split over multiple lines\"},\n" 17800 " {-1, 93463, \"world\" },\n" 17801 " {56, 5, \"!!\" }\n" 17802 "};\n", 17803 Style); 17804 17805 verifyFormat("return GradForUnaryCwise(g, {\n" 17806 " {{\"sign\"}, \"Sign\", {\"x\", " 17807 "\"dy\"} },\n" 17808 " {{\"dx\"}, \"Mul\", " 17809 "{\"dy\", \"sign\"}},\n" 17810 "});\n", 17811 Style); 17812 17813 Style.ColumnLimit = 0; 17814 EXPECT_EQ( 17815 "test demo[] = {\n" 17816 " {56, 23, \"hello world i am a very long line that really, in any " 17817 "just world, ought to be split over multiple lines\"},\n" 17818 " {-1, 93463, \"world\" " 17819 " },\n" 17820 " {7, 5, \"!!\" " 17821 " },\n" 17822 "};", 17823 format("test demo[] = {{56, 23, \"hello world i am a very long line " 17824 "that really, in any just world, ought to be split over multiple " 17825 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 17826 Style)); 17827 17828 Style.ColumnLimit = 80; 17829 verifyFormat("test demo[] = {\n" 17830 " {56, 23, /* a comment */ \"hello\"},\n" 17831 " {-1, 93463, \"world\" },\n" 17832 " {7, 5, \"!!\" }\n" 17833 "};\n", 17834 Style); 17835 17836 verifyFormat("test demo[] = {\n" 17837 " {56, 23, \"hello\" },\n" 17838 " {-1, 93463, \"world\" /* comment here */},\n" 17839 " {7, 5, \"!!\" }\n" 17840 "};\n", 17841 Style); 17842 17843 verifyFormat("test demo[] = {\n" 17844 " {56, /* a comment */ 23, \"hello\"},\n" 17845 " {-1, 93463, \"world\"},\n" 17846 " {7, 5, \"!!\" }\n" 17847 "};\n", 17848 Style); 17849 17850 Style.ColumnLimit = 20; 17851 EXPECT_EQ( 17852 "demo = std::array<\n" 17853 " struct test, 3>{\n" 17854 " test{\n" 17855 " 56, 23,\n" 17856 " \"hello \"\n" 17857 " \"world i \"\n" 17858 " \"am a very \"\n" 17859 " \"long line \"\n" 17860 " \"that \"\n" 17861 " \"really, \"\n" 17862 " \"in any \"\n" 17863 " \"just \"\n" 17864 " \"world, \"\n" 17865 " \"ought to \"\n" 17866 " \"be split \"\n" 17867 " \"over \"\n" 17868 " \"multiple \"\n" 17869 " \"lines\"},\n" 17870 " test{-1, 93463,\n" 17871 " \"world\"},\n" 17872 " test{7, 5,\n" 17873 " \"!!\" },\n" 17874 "};", 17875 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world " 17876 "i am a very long line that really, in any just world, ought " 17877 "to be split over multiple lines\"},test{-1, 93463, \"world\"}," 17878 "test{7, 5, \"!!\"},};", 17879 Style)); 17880 17881 // This caused a core dump by enabling Alignment in the LLVMStyle globally 17882 Style = getLLVMStyleWithColumns(50); 17883 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; 17884 verifyFormat("static A x = {\n" 17885 " {{init1, init2, init3, init4},\n" 17886 " {init1, init2, init3, init4}}\n" 17887 "};", 17888 Style); 17889 Style.ColumnLimit = 100; 17890 EXPECT_EQ( 17891 "test demo[] = {\n" 17892 " {56, 23,\n" 17893 " \"hello world i am a very long line that really, in any just world" 17894 ", ought to be split over \"\n" 17895 " \"multiple lines\" },\n" 17896 " {-1, 93463, \"world\"},\n" 17897 " {7, 5, \"!!\" },\n" 17898 "};", 17899 format("test demo[] = {{56, 23, \"hello world i am a very long line " 17900 "that really, in any just world, ought to be split over multiple " 17901 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 17902 Style)); 17903 } 17904 17905 TEST_F(FormatTest, UnderstandsPragmas) { 17906 verifyFormat("#pragma omp reduction(| : var)"); 17907 verifyFormat("#pragma omp reduction(+ : var)"); 17908 17909 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 17910 "(including parentheses).", 17911 format("#pragma mark Any non-hyphenated or hyphenated string " 17912 "(including parentheses).")); 17913 } 17914 17915 TEST_F(FormatTest, UnderstandPragmaOption) { 17916 verifyFormat("#pragma option -C -A"); 17917 17918 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 17919 } 17920 17921 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) { 17922 FormatStyle Style = getLLVMStyle(); 17923 Style.ColumnLimit = 20; 17924 17925 // See PR41213 17926 EXPECT_EQ("/*\n" 17927 " *\t9012345\n" 17928 " * /8901\n" 17929 " */", 17930 format("/*\n" 17931 " *\t9012345 /8901\n" 17932 " */", 17933 Style)); 17934 EXPECT_EQ("/*\n" 17935 " *345678\n" 17936 " *\t/8901\n" 17937 " */", 17938 format("/*\n" 17939 " *345678\t/8901\n" 17940 " */", 17941 Style)); 17942 17943 verifyFormat("int a; // the\n" 17944 " // comment", 17945 Style); 17946 EXPECT_EQ("int a; /* first line\n" 17947 " * second\n" 17948 " * line third\n" 17949 " * line\n" 17950 " */", 17951 format("int a; /* first line\n" 17952 " * second\n" 17953 " * line third\n" 17954 " * line\n" 17955 " */", 17956 Style)); 17957 EXPECT_EQ("int a; // first line\n" 17958 " // second\n" 17959 " // line third\n" 17960 " // line", 17961 format("int a; // first line\n" 17962 " // second line\n" 17963 " // third line", 17964 Style)); 17965 17966 Style.PenaltyExcessCharacter = 90; 17967 verifyFormat("int a; // the comment", Style); 17968 EXPECT_EQ("int a; // the comment\n" 17969 " // aaa", 17970 format("int a; // the comment aaa", Style)); 17971 EXPECT_EQ("int a; /* first line\n" 17972 " * second line\n" 17973 " * third line\n" 17974 " */", 17975 format("int a; /* first line\n" 17976 " * second line\n" 17977 " * third line\n" 17978 " */", 17979 Style)); 17980 EXPECT_EQ("int a; // first line\n" 17981 " // second line\n" 17982 " // third line", 17983 format("int a; // first line\n" 17984 " // second line\n" 17985 " // third line", 17986 Style)); 17987 // FIXME: Investigate why this is not getting the same layout as the test 17988 // above. 17989 EXPECT_EQ("int a; /* first line\n" 17990 " * second line\n" 17991 " * third line\n" 17992 " */", 17993 format("int a; /* first line second line third line" 17994 "\n*/", 17995 Style)); 17996 17997 EXPECT_EQ("// foo bar baz bazfoo\n" 17998 "// foo bar foo bar\n", 17999 format("// foo bar baz bazfoo\n" 18000 "// foo bar foo bar\n", 18001 Style)); 18002 EXPECT_EQ("// foo bar baz bazfoo\n" 18003 "// foo bar foo bar\n", 18004 format("// foo bar baz bazfoo\n" 18005 "// foo bar foo bar\n", 18006 Style)); 18007 18008 // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the 18009 // next one. 18010 EXPECT_EQ("// foo bar baz bazfoo\n" 18011 "// bar foo bar\n", 18012 format("// foo bar baz bazfoo bar\n" 18013 "// foo bar\n", 18014 Style)); 18015 18016 EXPECT_EQ("// foo bar baz bazfoo\n" 18017 "// foo bar baz bazfoo\n" 18018 "// bar foo bar\n", 18019 format("// foo bar baz bazfoo\n" 18020 "// foo bar baz bazfoo bar\n" 18021 "// foo bar\n", 18022 Style)); 18023 18024 EXPECT_EQ("// foo bar baz bazfoo\n" 18025 "// foo bar baz bazfoo\n" 18026 "// bar foo bar\n", 18027 format("// foo bar baz bazfoo\n" 18028 "// foo bar baz bazfoo bar\n" 18029 "// foo bar\n", 18030 Style)); 18031 18032 // Make sure we do not keep protruding characters if strict mode reflow is 18033 // cheaper than keeping protruding characters. 18034 Style.ColumnLimit = 21; 18035 EXPECT_EQ( 18036 "// foo foo foo foo\n" 18037 "// foo foo foo foo\n" 18038 "// foo foo foo foo\n", 18039 format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", Style)); 18040 18041 EXPECT_EQ("int a = /* long block\n" 18042 " comment */\n" 18043 " 42;", 18044 format("int a = /* long block comment */ 42;", Style)); 18045 } 18046 18047 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 18048 for (size_t i = 1; i < Styles.size(); ++i) \ 18049 EXPECT_EQ(Styles[0], Styles[i]) \ 18050 << "Style #" << i << " of " << Styles.size() << " differs from Style #0" 18051 18052 TEST_F(FormatTest, GetsPredefinedStyleByName) { 18053 SmallVector<FormatStyle, 3> Styles; 18054 Styles.resize(3); 18055 18056 Styles[0] = getLLVMStyle(); 18057 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 18058 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 18059 EXPECT_ALL_STYLES_EQUAL(Styles); 18060 18061 Styles[0] = getGoogleStyle(); 18062 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 18063 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 18064 EXPECT_ALL_STYLES_EQUAL(Styles); 18065 18066 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 18067 EXPECT_TRUE( 18068 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 18069 EXPECT_TRUE( 18070 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 18071 EXPECT_ALL_STYLES_EQUAL(Styles); 18072 18073 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 18074 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 18075 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 18076 EXPECT_ALL_STYLES_EQUAL(Styles); 18077 18078 Styles[0] = getMozillaStyle(); 18079 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 18080 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 18081 EXPECT_ALL_STYLES_EQUAL(Styles); 18082 18083 Styles[0] = getWebKitStyle(); 18084 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 18085 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 18086 EXPECT_ALL_STYLES_EQUAL(Styles); 18087 18088 Styles[0] = getGNUStyle(); 18089 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 18090 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 18091 EXPECT_ALL_STYLES_EQUAL(Styles); 18092 18093 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 18094 } 18095 18096 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 18097 SmallVector<FormatStyle, 8> Styles; 18098 Styles.resize(2); 18099 18100 Styles[0] = getGoogleStyle(); 18101 Styles[1] = getLLVMStyle(); 18102 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 18103 EXPECT_ALL_STYLES_EQUAL(Styles); 18104 18105 Styles.resize(5); 18106 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 18107 Styles[1] = getLLVMStyle(); 18108 Styles[1].Language = FormatStyle::LK_JavaScript; 18109 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 18110 18111 Styles[2] = getLLVMStyle(); 18112 Styles[2].Language = FormatStyle::LK_JavaScript; 18113 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 18114 "BasedOnStyle: Google", 18115 &Styles[2]) 18116 .value()); 18117 18118 Styles[3] = getLLVMStyle(); 18119 Styles[3].Language = FormatStyle::LK_JavaScript; 18120 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 18121 "Language: JavaScript", 18122 &Styles[3]) 18123 .value()); 18124 18125 Styles[4] = getLLVMStyle(); 18126 Styles[4].Language = FormatStyle::LK_JavaScript; 18127 EXPECT_EQ(0, parseConfiguration("---\n" 18128 "BasedOnStyle: LLVM\n" 18129 "IndentWidth: 123\n" 18130 "---\n" 18131 "BasedOnStyle: Google\n" 18132 "Language: JavaScript", 18133 &Styles[4]) 18134 .value()); 18135 EXPECT_ALL_STYLES_EQUAL(Styles); 18136 } 18137 18138 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 18139 Style.FIELD = false; \ 18140 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 18141 EXPECT_TRUE(Style.FIELD); \ 18142 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 18143 EXPECT_FALSE(Style.FIELD); 18144 18145 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 18146 18147 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 18148 Style.STRUCT.FIELD = false; \ 18149 EXPECT_EQ(0, \ 18150 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 18151 .value()); \ 18152 EXPECT_TRUE(Style.STRUCT.FIELD); \ 18153 EXPECT_EQ(0, \ 18154 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 18155 .value()); \ 18156 EXPECT_FALSE(Style.STRUCT.FIELD); 18157 18158 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 18159 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 18160 18161 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 18162 EXPECT_NE(VALUE, Style.FIELD) << "Initial value already the same!"; \ 18163 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 18164 EXPECT_EQ(VALUE, Style.FIELD) << "Unexpected value after parsing!" 18165 18166 TEST_F(FormatTest, ParsesConfigurationBools) { 18167 FormatStyle Style = {}; 18168 Style.Language = FormatStyle::LK_Cpp; 18169 CHECK_PARSE_BOOL(AlignTrailingComments); 18170 CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine); 18171 CHECK_PARSE_BOOL(AllowAllConstructorInitializersOnNextLine); 18172 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 18173 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 18174 CHECK_PARSE_BOOL(AllowShortEnumsOnASingleLine); 18175 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 18176 CHECK_PARSE_BOOL(BinPackArguments); 18177 CHECK_PARSE_BOOL(BinPackParameters); 18178 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 18179 CHECK_PARSE_BOOL(BreakBeforeConceptDeclarations); 18180 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 18181 CHECK_PARSE_BOOL(BreakStringLiterals); 18182 CHECK_PARSE_BOOL(CompactNamespaces); 18183 CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); 18184 CHECK_PARSE_BOOL(DeriveLineEnding); 18185 CHECK_PARSE_BOOL(DerivePointerAlignment); 18186 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 18187 CHECK_PARSE_BOOL(DisableFormat); 18188 CHECK_PARSE_BOOL(IndentAccessModifiers); 18189 CHECK_PARSE_BOOL(IndentCaseLabels); 18190 CHECK_PARSE_BOOL(IndentCaseBlocks); 18191 CHECK_PARSE_BOOL(IndentGotoLabels); 18192 CHECK_PARSE_BOOL(IndentRequires); 18193 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 18194 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 18195 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 18196 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 18197 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 18198 CHECK_PARSE_BOOL(ReflowComments); 18199 CHECK_PARSE_BOOL(SortUsingDeclarations); 18200 CHECK_PARSE_BOOL(SpacesInParentheses); 18201 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 18202 CHECK_PARSE_BOOL(SpacesInConditionalStatement); 18203 CHECK_PARSE_BOOL(SpaceInEmptyBlock); 18204 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 18205 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 18206 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 18207 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 18208 CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); 18209 CHECK_PARSE_BOOL(SpaceAfterLogicalNot); 18210 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 18211 CHECK_PARSE_BOOL(SpaceBeforeCaseColon); 18212 CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList); 18213 CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon); 18214 CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon); 18215 CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon); 18216 CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets); 18217 CHECK_PARSE_BOOL(UseCRLF); 18218 18219 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel); 18220 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 18221 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 18222 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 18223 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 18224 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 18225 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 18226 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 18227 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock); 18228 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 18229 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 18230 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeLambdaBody); 18231 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeWhile); 18232 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 18233 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction); 18234 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord); 18235 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace); 18236 } 18237 18238 #undef CHECK_PARSE_BOOL 18239 18240 TEST_F(FormatTest, ParsesConfiguration) { 18241 FormatStyle Style = {}; 18242 Style.Language = FormatStyle::LK_Cpp; 18243 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 18244 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 18245 ConstructorInitializerIndentWidth, 1234u); 18246 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 18247 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 18248 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 18249 CHECK_PARSE("PenaltyBreakAssignment: 1234", PenaltyBreakAssignment, 1234u); 18250 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 18251 PenaltyBreakBeforeFirstCallParameter, 1234u); 18252 CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234", 18253 PenaltyBreakTemplateDeclaration, 1234u); 18254 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 18255 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 18256 PenaltyReturnTypeOnItsOwnLine, 1234u); 18257 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 18258 SpacesBeforeTrailingComments, 1234u); 18259 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 18260 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 18261 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$"); 18262 18263 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 18264 CHECK_PARSE("AlignConsecutiveAssignments: None", AlignConsecutiveAssignments, 18265 FormatStyle::ACS_None); 18266 CHECK_PARSE("AlignConsecutiveAssignments: Consecutive", 18267 AlignConsecutiveAssignments, FormatStyle::ACS_Consecutive); 18268 CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLines", 18269 AlignConsecutiveAssignments, FormatStyle::ACS_AcrossEmptyLines); 18270 CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLinesAndComments", 18271 AlignConsecutiveAssignments, 18272 FormatStyle::ACS_AcrossEmptyLinesAndComments); 18273 // For backwards compability, false / true should still parse 18274 CHECK_PARSE("AlignConsecutiveAssignments: false", AlignConsecutiveAssignments, 18275 FormatStyle::ACS_None); 18276 CHECK_PARSE("AlignConsecutiveAssignments: true", AlignConsecutiveAssignments, 18277 FormatStyle::ACS_Consecutive); 18278 18279 Style.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive; 18280 CHECK_PARSE("AlignConsecutiveBitFields: None", AlignConsecutiveBitFields, 18281 FormatStyle::ACS_None); 18282 CHECK_PARSE("AlignConsecutiveBitFields: Consecutive", 18283 AlignConsecutiveBitFields, FormatStyle::ACS_Consecutive); 18284 CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLines", 18285 AlignConsecutiveBitFields, FormatStyle::ACS_AcrossEmptyLines); 18286 CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLinesAndComments", 18287 AlignConsecutiveBitFields, 18288 FormatStyle::ACS_AcrossEmptyLinesAndComments); 18289 // For backwards compability, false / true should still parse 18290 CHECK_PARSE("AlignConsecutiveBitFields: false", AlignConsecutiveBitFields, 18291 FormatStyle::ACS_None); 18292 CHECK_PARSE("AlignConsecutiveBitFields: true", AlignConsecutiveBitFields, 18293 FormatStyle::ACS_Consecutive); 18294 18295 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 18296 CHECK_PARSE("AlignConsecutiveMacros: None", AlignConsecutiveMacros, 18297 FormatStyle::ACS_None); 18298 CHECK_PARSE("AlignConsecutiveMacros: Consecutive", AlignConsecutiveMacros, 18299 FormatStyle::ACS_Consecutive); 18300 CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLines", 18301 AlignConsecutiveMacros, FormatStyle::ACS_AcrossEmptyLines); 18302 CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLinesAndComments", 18303 AlignConsecutiveMacros, 18304 FormatStyle::ACS_AcrossEmptyLinesAndComments); 18305 // For backwards compability, false / true should still parse 18306 CHECK_PARSE("AlignConsecutiveMacros: false", AlignConsecutiveMacros, 18307 FormatStyle::ACS_None); 18308 CHECK_PARSE("AlignConsecutiveMacros: true", AlignConsecutiveMacros, 18309 FormatStyle::ACS_Consecutive); 18310 18311 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 18312 CHECK_PARSE("AlignConsecutiveDeclarations: None", 18313 AlignConsecutiveDeclarations, FormatStyle::ACS_None); 18314 CHECK_PARSE("AlignConsecutiveDeclarations: Consecutive", 18315 AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive); 18316 CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLines", 18317 AlignConsecutiveDeclarations, FormatStyle::ACS_AcrossEmptyLines); 18318 CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLinesAndComments", 18319 AlignConsecutiveDeclarations, 18320 FormatStyle::ACS_AcrossEmptyLinesAndComments); 18321 // For backwards compability, false / true should still parse 18322 CHECK_PARSE("AlignConsecutiveDeclarations: false", 18323 AlignConsecutiveDeclarations, FormatStyle::ACS_None); 18324 CHECK_PARSE("AlignConsecutiveDeclarations: true", 18325 AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive); 18326 18327 Style.PointerAlignment = FormatStyle::PAS_Middle; 18328 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 18329 FormatStyle::PAS_Left); 18330 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 18331 FormatStyle::PAS_Right); 18332 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 18333 FormatStyle::PAS_Middle); 18334 Style.ReferenceAlignment = FormatStyle::RAS_Middle; 18335 CHECK_PARSE("ReferenceAlignment: Pointer", ReferenceAlignment, 18336 FormatStyle::RAS_Pointer); 18337 CHECK_PARSE("ReferenceAlignment: Left", ReferenceAlignment, 18338 FormatStyle::RAS_Left); 18339 CHECK_PARSE("ReferenceAlignment: Right", ReferenceAlignment, 18340 FormatStyle::RAS_Right); 18341 CHECK_PARSE("ReferenceAlignment: Middle", ReferenceAlignment, 18342 FormatStyle::RAS_Middle); 18343 // For backward compatibility: 18344 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 18345 FormatStyle::PAS_Left); 18346 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 18347 FormatStyle::PAS_Right); 18348 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 18349 FormatStyle::PAS_Middle); 18350 18351 Style.Standard = FormatStyle::LS_Auto; 18352 CHECK_PARSE("Standard: c++03", Standard, FormatStyle::LS_Cpp03); 18353 CHECK_PARSE("Standard: c++11", Standard, FormatStyle::LS_Cpp11); 18354 CHECK_PARSE("Standard: c++14", Standard, FormatStyle::LS_Cpp14); 18355 CHECK_PARSE("Standard: c++17", Standard, FormatStyle::LS_Cpp17); 18356 CHECK_PARSE("Standard: c++20", Standard, FormatStyle::LS_Cpp20); 18357 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 18358 CHECK_PARSE("Standard: Latest", Standard, FormatStyle::LS_Latest); 18359 // Legacy aliases: 18360 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 18361 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Latest); 18362 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 18363 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 18364 18365 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 18366 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 18367 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 18368 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 18369 FormatStyle::BOS_None); 18370 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 18371 FormatStyle::BOS_All); 18372 // For backward compatibility: 18373 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 18374 FormatStyle::BOS_None); 18375 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 18376 FormatStyle::BOS_All); 18377 18378 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 18379 CHECK_PARSE("BreakConstructorInitializers: BeforeComma", 18380 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 18381 CHECK_PARSE("BreakConstructorInitializers: AfterColon", 18382 BreakConstructorInitializers, FormatStyle::BCIS_AfterColon); 18383 CHECK_PARSE("BreakConstructorInitializers: BeforeColon", 18384 BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon); 18385 // For backward compatibility: 18386 CHECK_PARSE("BreakConstructorInitializersBeforeComma: true", 18387 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 18388 18389 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 18390 CHECK_PARSE("BreakInheritanceList: AfterComma", BreakInheritanceList, 18391 FormatStyle::BILS_AfterComma); 18392 CHECK_PARSE("BreakInheritanceList: BeforeComma", BreakInheritanceList, 18393 FormatStyle::BILS_BeforeComma); 18394 CHECK_PARSE("BreakInheritanceList: AfterColon", BreakInheritanceList, 18395 FormatStyle::BILS_AfterColon); 18396 CHECK_PARSE("BreakInheritanceList: BeforeColon", BreakInheritanceList, 18397 FormatStyle::BILS_BeforeColon); 18398 // For backward compatibility: 18399 CHECK_PARSE("BreakBeforeInheritanceComma: true", BreakInheritanceList, 18400 FormatStyle::BILS_BeforeComma); 18401 18402 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 18403 CHECK_PARSE("EmptyLineBeforeAccessModifier: Never", 18404 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Never); 18405 CHECK_PARSE("EmptyLineBeforeAccessModifier: Leave", 18406 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Leave); 18407 CHECK_PARSE("EmptyLineBeforeAccessModifier: LogicalBlock", 18408 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_LogicalBlock); 18409 CHECK_PARSE("EmptyLineBeforeAccessModifier: Always", 18410 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Always); 18411 18412 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 18413 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 18414 FormatStyle::BAS_Align); 18415 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 18416 FormatStyle::BAS_DontAlign); 18417 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 18418 FormatStyle::BAS_AlwaysBreak); 18419 // For backward compatibility: 18420 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 18421 FormatStyle::BAS_DontAlign); 18422 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 18423 FormatStyle::BAS_Align); 18424 18425 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left; 18426 CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines, 18427 FormatStyle::ENAS_DontAlign); 18428 CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines, 18429 FormatStyle::ENAS_Left); 18430 CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines, 18431 FormatStyle::ENAS_Right); 18432 // For backward compatibility: 18433 CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines, 18434 FormatStyle::ENAS_Left); 18435 CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines, 18436 FormatStyle::ENAS_Right); 18437 18438 Style.AlignOperands = FormatStyle::OAS_Align; 18439 CHECK_PARSE("AlignOperands: DontAlign", AlignOperands, 18440 FormatStyle::OAS_DontAlign); 18441 CHECK_PARSE("AlignOperands: Align", AlignOperands, FormatStyle::OAS_Align); 18442 CHECK_PARSE("AlignOperands: AlignAfterOperator", AlignOperands, 18443 FormatStyle::OAS_AlignAfterOperator); 18444 // For backward compatibility: 18445 CHECK_PARSE("AlignOperands: false", AlignOperands, 18446 FormatStyle::OAS_DontAlign); 18447 CHECK_PARSE("AlignOperands: true", AlignOperands, FormatStyle::OAS_Align); 18448 18449 Style.UseTab = FormatStyle::UT_ForIndentation; 18450 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 18451 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 18452 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 18453 CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab, 18454 FormatStyle::UT_ForContinuationAndIndentation); 18455 CHECK_PARSE("UseTab: AlignWithSpaces", UseTab, 18456 FormatStyle::UT_AlignWithSpaces); 18457 // For backward compatibility: 18458 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 18459 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 18460 18461 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 18462 CHECK_PARSE("AllowShortBlocksOnASingleLine: Never", 18463 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 18464 CHECK_PARSE("AllowShortBlocksOnASingleLine: Empty", 18465 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Empty); 18466 CHECK_PARSE("AllowShortBlocksOnASingleLine: Always", 18467 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always); 18468 // For backward compatibility: 18469 CHECK_PARSE("AllowShortBlocksOnASingleLine: false", 18470 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 18471 CHECK_PARSE("AllowShortBlocksOnASingleLine: true", 18472 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always); 18473 18474 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 18475 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 18476 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 18477 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 18478 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 18479 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 18480 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 18481 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 18482 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 18483 // For backward compatibility: 18484 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 18485 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 18486 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 18487 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 18488 18489 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Both; 18490 CHECK_PARSE("SpaceAroundPointerQualifiers: Default", 18491 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Default); 18492 CHECK_PARSE("SpaceAroundPointerQualifiers: Before", 18493 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Before); 18494 CHECK_PARSE("SpaceAroundPointerQualifiers: After", 18495 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_After); 18496 CHECK_PARSE("SpaceAroundPointerQualifiers: Both", 18497 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Both); 18498 18499 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 18500 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 18501 FormatStyle::SBPO_Never); 18502 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 18503 FormatStyle::SBPO_Always); 18504 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 18505 FormatStyle::SBPO_ControlStatements); 18506 CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptControlMacros", 18507 SpaceBeforeParens, 18508 FormatStyle::SBPO_ControlStatementsExceptControlMacros); 18509 CHECK_PARSE("SpaceBeforeParens: NonEmptyParentheses", SpaceBeforeParens, 18510 FormatStyle::SBPO_NonEmptyParentheses); 18511 // For backward compatibility: 18512 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 18513 FormatStyle::SBPO_Never); 18514 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 18515 FormatStyle::SBPO_ControlStatements); 18516 CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptForEachMacros", 18517 SpaceBeforeParens, 18518 FormatStyle::SBPO_ControlStatementsExceptControlMacros); 18519 18520 Style.ColumnLimit = 123; 18521 FormatStyle BaseStyle = getLLVMStyle(); 18522 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 18523 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 18524 18525 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 18526 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 18527 FormatStyle::BS_Attach); 18528 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 18529 FormatStyle::BS_Linux); 18530 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 18531 FormatStyle::BS_Mozilla); 18532 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 18533 FormatStyle::BS_Stroustrup); 18534 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 18535 FormatStyle::BS_Allman); 18536 CHECK_PARSE("BreakBeforeBraces: Whitesmiths", BreakBeforeBraces, 18537 FormatStyle::BS_Whitesmiths); 18538 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 18539 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 18540 FormatStyle::BS_WebKit); 18541 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 18542 FormatStyle::BS_Custom); 18543 18544 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never; 18545 CHECK_PARSE("BraceWrapping:\n" 18546 " AfterControlStatement: MultiLine", 18547 BraceWrapping.AfterControlStatement, 18548 FormatStyle::BWACS_MultiLine); 18549 CHECK_PARSE("BraceWrapping:\n" 18550 " AfterControlStatement: Always", 18551 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always); 18552 CHECK_PARSE("BraceWrapping:\n" 18553 " AfterControlStatement: Never", 18554 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never); 18555 // For backward compatibility: 18556 CHECK_PARSE("BraceWrapping:\n" 18557 " AfterControlStatement: true", 18558 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always); 18559 CHECK_PARSE("BraceWrapping:\n" 18560 " AfterControlStatement: false", 18561 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never); 18562 18563 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 18564 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 18565 FormatStyle::RTBS_None); 18566 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 18567 FormatStyle::RTBS_All); 18568 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 18569 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 18570 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 18571 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 18572 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 18573 AlwaysBreakAfterReturnType, 18574 FormatStyle::RTBS_TopLevelDefinitions); 18575 18576 Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 18577 CHECK_PARSE("AlwaysBreakTemplateDeclarations: No", 18578 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_No); 18579 CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine", 18580 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine); 18581 CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes", 18582 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes); 18583 CHECK_PARSE("AlwaysBreakTemplateDeclarations: false", 18584 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine); 18585 CHECK_PARSE("AlwaysBreakTemplateDeclarations: true", 18586 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes); 18587 18588 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 18589 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 18590 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 18591 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 18592 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 18593 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 18594 AlwaysBreakAfterDefinitionReturnType, 18595 FormatStyle::DRTBS_TopLevel); 18596 18597 Style.NamespaceIndentation = FormatStyle::NI_All; 18598 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 18599 FormatStyle::NI_None); 18600 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 18601 FormatStyle::NI_Inner); 18602 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 18603 FormatStyle::NI_All); 18604 18605 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_OnlyFirstIf; 18606 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Never", 18607 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never); 18608 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: WithoutElse", 18609 AllowShortIfStatementsOnASingleLine, 18610 FormatStyle::SIS_WithoutElse); 18611 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: OnlyFirstIf", 18612 AllowShortIfStatementsOnASingleLine, 18613 FormatStyle::SIS_OnlyFirstIf); 18614 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: AllIfsAndElse", 18615 AllowShortIfStatementsOnASingleLine, 18616 FormatStyle::SIS_AllIfsAndElse); 18617 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Always", 18618 AllowShortIfStatementsOnASingleLine, 18619 FormatStyle::SIS_OnlyFirstIf); 18620 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: false", 18621 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never); 18622 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: true", 18623 AllowShortIfStatementsOnASingleLine, 18624 FormatStyle::SIS_WithoutElse); 18625 18626 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; 18627 CHECK_PARSE("IndentExternBlock: AfterExternBlock", IndentExternBlock, 18628 FormatStyle::IEBS_AfterExternBlock); 18629 CHECK_PARSE("IndentExternBlock: Indent", IndentExternBlock, 18630 FormatStyle::IEBS_Indent); 18631 CHECK_PARSE("IndentExternBlock: NoIndent", IndentExternBlock, 18632 FormatStyle::IEBS_NoIndent); 18633 CHECK_PARSE("IndentExternBlock: true", IndentExternBlock, 18634 FormatStyle::IEBS_Indent); 18635 CHECK_PARSE("IndentExternBlock: false", IndentExternBlock, 18636 FormatStyle::IEBS_NoIndent); 18637 18638 Style.BitFieldColonSpacing = FormatStyle::BFCS_None; 18639 CHECK_PARSE("BitFieldColonSpacing: Both", BitFieldColonSpacing, 18640 FormatStyle::BFCS_Both); 18641 CHECK_PARSE("BitFieldColonSpacing: None", BitFieldColonSpacing, 18642 FormatStyle::BFCS_None); 18643 CHECK_PARSE("BitFieldColonSpacing: Before", BitFieldColonSpacing, 18644 FormatStyle::BFCS_Before); 18645 CHECK_PARSE("BitFieldColonSpacing: After", BitFieldColonSpacing, 18646 FormatStyle::BFCS_After); 18647 18648 Style.SortJavaStaticImport = FormatStyle::SJSIO_Before; 18649 CHECK_PARSE("SortJavaStaticImport: After", SortJavaStaticImport, 18650 FormatStyle::SJSIO_After); 18651 CHECK_PARSE("SortJavaStaticImport: Before", SortJavaStaticImport, 18652 FormatStyle::SJSIO_Before); 18653 18654 // FIXME: This is required because parsing a configuration simply overwrites 18655 // the first N elements of the list instead of resetting it. 18656 Style.ForEachMacros.clear(); 18657 std::vector<std::string> BoostForeach; 18658 BoostForeach.push_back("BOOST_FOREACH"); 18659 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 18660 std::vector<std::string> BoostAndQForeach; 18661 BoostAndQForeach.push_back("BOOST_FOREACH"); 18662 BoostAndQForeach.push_back("Q_FOREACH"); 18663 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 18664 BoostAndQForeach); 18665 18666 Style.IfMacros.clear(); 18667 std::vector<std::string> CustomIfs; 18668 CustomIfs.push_back("MYIF"); 18669 CHECK_PARSE("IfMacros: [MYIF]", IfMacros, CustomIfs); 18670 18671 Style.AttributeMacros.clear(); 18672 CHECK_PARSE("BasedOnStyle: LLVM", AttributeMacros, 18673 std::vector<std::string>{"__capability"}); 18674 CHECK_PARSE("AttributeMacros: [attr1, attr2]", AttributeMacros, 18675 std::vector<std::string>({"attr1", "attr2"})); 18676 18677 Style.StatementAttributeLikeMacros.clear(); 18678 CHECK_PARSE("StatementAttributeLikeMacros: [emit,Q_EMIT]", 18679 StatementAttributeLikeMacros, 18680 std::vector<std::string>({"emit", "Q_EMIT"})); 18681 18682 Style.StatementMacros.clear(); 18683 CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros, 18684 std::vector<std::string>{"QUNUSED"}); 18685 CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros, 18686 std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"})); 18687 18688 Style.NamespaceMacros.clear(); 18689 CHECK_PARSE("NamespaceMacros: [TESTSUITE]", NamespaceMacros, 18690 std::vector<std::string>{"TESTSUITE"}); 18691 CHECK_PARSE("NamespaceMacros: [TESTSUITE, SUITE]", NamespaceMacros, 18692 std::vector<std::string>({"TESTSUITE", "SUITE"})); 18693 18694 Style.WhitespaceSensitiveMacros.clear(); 18695 CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE]", 18696 WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"}); 18697 CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE, ASSERT]", 18698 WhitespaceSensitiveMacros, 18699 std::vector<std::string>({"STRINGIZE", "ASSERT"})); 18700 Style.WhitespaceSensitiveMacros.clear(); 18701 CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE']", 18702 WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"}); 18703 CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE', 'ASSERT']", 18704 WhitespaceSensitiveMacros, 18705 std::vector<std::string>({"STRINGIZE", "ASSERT"})); 18706 18707 Style.IncludeStyle.IncludeCategories.clear(); 18708 std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = { 18709 {"abc/.*", 2, 0, false}, {".*", 1, 0, true}}; 18710 CHECK_PARSE("IncludeCategories:\n" 18711 " - Regex: abc/.*\n" 18712 " Priority: 2\n" 18713 " - Regex: .*\n" 18714 " Priority: 1\n" 18715 " CaseSensitive: true\n", 18716 IncludeStyle.IncludeCategories, ExpectedCategories); 18717 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex, 18718 "abc$"); 18719 CHECK_PARSE("IncludeIsMainSourceRegex: 'abc$'", 18720 IncludeStyle.IncludeIsMainSourceRegex, "abc$"); 18721 18722 Style.SortIncludes = FormatStyle::SI_Never; 18723 CHECK_PARSE("SortIncludes: true", SortIncludes, 18724 FormatStyle::SI_CaseSensitive); 18725 CHECK_PARSE("SortIncludes: false", SortIncludes, FormatStyle::SI_Never); 18726 CHECK_PARSE("SortIncludes: CaseInsensitive", SortIncludes, 18727 FormatStyle::SI_CaseInsensitive); 18728 CHECK_PARSE("SortIncludes: CaseSensitive", SortIncludes, 18729 FormatStyle::SI_CaseSensitive); 18730 CHECK_PARSE("SortIncludes: Never", SortIncludes, FormatStyle::SI_Never); 18731 18732 Style.RawStringFormats.clear(); 18733 std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = { 18734 { 18735 FormatStyle::LK_TextProto, 18736 {"pb", "proto"}, 18737 {"PARSE_TEXT_PROTO"}, 18738 /*CanonicalDelimiter=*/"", 18739 "llvm", 18740 }, 18741 { 18742 FormatStyle::LK_Cpp, 18743 {"cc", "cpp"}, 18744 {"C_CODEBLOCK", "CPPEVAL"}, 18745 /*CanonicalDelimiter=*/"cc", 18746 /*BasedOnStyle=*/"", 18747 }, 18748 }; 18749 18750 CHECK_PARSE("RawStringFormats:\n" 18751 " - Language: TextProto\n" 18752 " Delimiters:\n" 18753 " - 'pb'\n" 18754 " - 'proto'\n" 18755 " EnclosingFunctions:\n" 18756 " - 'PARSE_TEXT_PROTO'\n" 18757 " BasedOnStyle: llvm\n" 18758 " - Language: Cpp\n" 18759 " Delimiters:\n" 18760 " - 'cc'\n" 18761 " - 'cpp'\n" 18762 " EnclosingFunctions:\n" 18763 " - 'C_CODEBLOCK'\n" 18764 " - 'CPPEVAL'\n" 18765 " CanonicalDelimiter: 'cc'", 18766 RawStringFormats, ExpectedRawStringFormats); 18767 18768 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 18769 " Minimum: 0\n" 18770 " Maximum: 0", 18771 SpacesInLineCommentPrefix.Minimum, 0u); 18772 EXPECT_EQ(Style.SpacesInLineCommentPrefix.Maximum, 0u); 18773 Style.SpacesInLineCommentPrefix.Minimum = 1; 18774 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 18775 " Minimum: 2", 18776 SpacesInLineCommentPrefix.Minimum, 0u); 18777 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 18778 " Maximum: -1", 18779 SpacesInLineCommentPrefix.Maximum, -1u); 18780 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 18781 " Minimum: 2", 18782 SpacesInLineCommentPrefix.Minimum, 2u); 18783 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 18784 " Maximum: 1", 18785 SpacesInLineCommentPrefix.Maximum, 1u); 18786 EXPECT_EQ(Style.SpacesInLineCommentPrefix.Minimum, 1u); 18787 18788 Style.SpacesInAngles = FormatStyle::SIAS_Always; 18789 CHECK_PARSE("SpacesInAngles: Never", SpacesInAngles, FormatStyle::SIAS_Never); 18790 CHECK_PARSE("SpacesInAngles: Always", SpacesInAngles, 18791 FormatStyle::SIAS_Always); 18792 CHECK_PARSE("SpacesInAngles: Leave", SpacesInAngles, FormatStyle::SIAS_Leave); 18793 // For backward compatibility: 18794 CHECK_PARSE("SpacesInAngles: false", SpacesInAngles, FormatStyle::SIAS_Never); 18795 CHECK_PARSE("SpacesInAngles: true", SpacesInAngles, FormatStyle::SIAS_Always); 18796 } 18797 18798 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 18799 FormatStyle Style = {}; 18800 Style.Language = FormatStyle::LK_Cpp; 18801 CHECK_PARSE("Language: Cpp\n" 18802 "IndentWidth: 12", 18803 IndentWidth, 12u); 18804 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 18805 "IndentWidth: 34", 18806 &Style), 18807 ParseError::Unsuitable); 18808 FormatStyle BinPackedTCS = {}; 18809 BinPackedTCS.Language = FormatStyle::LK_JavaScript; 18810 EXPECT_EQ(parseConfiguration("BinPackArguments: true\n" 18811 "InsertTrailingCommas: Wrapped", 18812 &BinPackedTCS), 18813 ParseError::BinPackTrailingCommaConflict); 18814 EXPECT_EQ(12u, Style.IndentWidth); 18815 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 18816 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 18817 18818 Style.Language = FormatStyle::LK_JavaScript; 18819 CHECK_PARSE("Language: JavaScript\n" 18820 "IndentWidth: 12", 18821 IndentWidth, 12u); 18822 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 18823 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 18824 "IndentWidth: 34", 18825 &Style), 18826 ParseError::Unsuitable); 18827 EXPECT_EQ(23u, Style.IndentWidth); 18828 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 18829 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 18830 18831 CHECK_PARSE("BasedOnStyle: LLVM\n" 18832 "IndentWidth: 67", 18833 IndentWidth, 67u); 18834 18835 CHECK_PARSE("---\n" 18836 "Language: JavaScript\n" 18837 "IndentWidth: 12\n" 18838 "---\n" 18839 "Language: Cpp\n" 18840 "IndentWidth: 34\n" 18841 "...\n", 18842 IndentWidth, 12u); 18843 18844 Style.Language = FormatStyle::LK_Cpp; 18845 CHECK_PARSE("---\n" 18846 "Language: JavaScript\n" 18847 "IndentWidth: 12\n" 18848 "---\n" 18849 "Language: Cpp\n" 18850 "IndentWidth: 34\n" 18851 "...\n", 18852 IndentWidth, 34u); 18853 CHECK_PARSE("---\n" 18854 "IndentWidth: 78\n" 18855 "---\n" 18856 "Language: JavaScript\n" 18857 "IndentWidth: 56\n" 18858 "...\n", 18859 IndentWidth, 78u); 18860 18861 Style.ColumnLimit = 123; 18862 Style.IndentWidth = 234; 18863 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 18864 Style.TabWidth = 345; 18865 EXPECT_FALSE(parseConfiguration("---\n" 18866 "IndentWidth: 456\n" 18867 "BreakBeforeBraces: Allman\n" 18868 "---\n" 18869 "Language: JavaScript\n" 18870 "IndentWidth: 111\n" 18871 "TabWidth: 111\n" 18872 "---\n" 18873 "Language: Cpp\n" 18874 "BreakBeforeBraces: Stroustrup\n" 18875 "TabWidth: 789\n" 18876 "...\n", 18877 &Style)); 18878 EXPECT_EQ(123u, Style.ColumnLimit); 18879 EXPECT_EQ(456u, Style.IndentWidth); 18880 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 18881 EXPECT_EQ(789u, Style.TabWidth); 18882 18883 EXPECT_EQ(parseConfiguration("---\n" 18884 "Language: JavaScript\n" 18885 "IndentWidth: 56\n" 18886 "---\n" 18887 "IndentWidth: 78\n" 18888 "...\n", 18889 &Style), 18890 ParseError::Error); 18891 EXPECT_EQ(parseConfiguration("---\n" 18892 "Language: JavaScript\n" 18893 "IndentWidth: 56\n" 18894 "---\n" 18895 "Language: JavaScript\n" 18896 "IndentWidth: 78\n" 18897 "...\n", 18898 &Style), 18899 ParseError::Error); 18900 18901 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 18902 } 18903 18904 #undef CHECK_PARSE 18905 18906 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 18907 FormatStyle Style = {}; 18908 Style.Language = FormatStyle::LK_JavaScript; 18909 Style.BreakBeforeTernaryOperators = true; 18910 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 18911 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 18912 18913 Style.BreakBeforeTernaryOperators = true; 18914 EXPECT_EQ(0, parseConfiguration("---\n" 18915 "BasedOnStyle: Google\n" 18916 "---\n" 18917 "Language: JavaScript\n" 18918 "IndentWidth: 76\n" 18919 "...\n", 18920 &Style) 18921 .value()); 18922 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 18923 EXPECT_EQ(76u, Style.IndentWidth); 18924 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 18925 } 18926 18927 TEST_F(FormatTest, ConfigurationRoundTripTest) { 18928 FormatStyle Style = getLLVMStyle(); 18929 std::string YAML = configurationAsText(Style); 18930 FormatStyle ParsedStyle = {}; 18931 ParsedStyle.Language = FormatStyle::LK_Cpp; 18932 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 18933 EXPECT_EQ(Style, ParsedStyle); 18934 } 18935 18936 TEST_F(FormatTest, WorksFor8bitEncodings) { 18937 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 18938 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 18939 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 18940 "\"\xef\xee\xf0\xf3...\"", 18941 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 18942 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 18943 "\xef\xee\xf0\xf3...\"", 18944 getLLVMStyleWithColumns(12))); 18945 } 18946 18947 TEST_F(FormatTest, HandlesUTF8BOM) { 18948 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 18949 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 18950 format("\xef\xbb\xbf#include <iostream>")); 18951 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 18952 format("\xef\xbb\xbf\n#include <iostream>")); 18953 } 18954 18955 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 18956 #if !defined(_MSC_VER) 18957 18958 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 18959 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 18960 getLLVMStyleWithColumns(35)); 18961 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 18962 getLLVMStyleWithColumns(31)); 18963 verifyFormat("// Однажды в студёную зимнюю пору...", 18964 getLLVMStyleWithColumns(36)); 18965 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 18966 verifyFormat("/* Однажды в студёную зимнюю пору... */", 18967 getLLVMStyleWithColumns(39)); 18968 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 18969 getLLVMStyleWithColumns(35)); 18970 } 18971 18972 TEST_F(FormatTest, SplitsUTF8Strings) { 18973 // Non-printable characters' width is currently considered to be the length in 18974 // bytes in UTF8. The characters can be displayed in very different manner 18975 // (zero-width, single width with a substitution glyph, expanded to their code 18976 // (e.g. "<8d>"), so there's no single correct way to handle them. 18977 EXPECT_EQ("\"aaaaÄ\"\n" 18978 "\"\xc2\x8d\";", 18979 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 18980 EXPECT_EQ("\"aaaaaaaÄ\"\n" 18981 "\"\xc2\x8d\";", 18982 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 18983 EXPECT_EQ("\"Однажды, в \"\n" 18984 "\"студёную \"\n" 18985 "\"зимнюю \"\n" 18986 "\"пору,\"", 18987 format("\"Однажды, в студёную зимнюю пору,\"", 18988 getLLVMStyleWithColumns(13))); 18989 EXPECT_EQ( 18990 "\"一 二 三 \"\n" 18991 "\"四 五六 \"\n" 18992 "\"七 八 九 \"\n" 18993 "\"十\"", 18994 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 18995 EXPECT_EQ("\"一\t\"\n" 18996 "\"二 \t\"\n" 18997 "\"三 四 \"\n" 18998 "\"五\t\"\n" 18999 "\"六 \t\"\n" 19000 "\"七 \"\n" 19001 "\"八九十\tqq\"", 19002 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 19003 getLLVMStyleWithColumns(11))); 19004 19005 // UTF8 character in an escape sequence. 19006 EXPECT_EQ("\"aaaaaa\"\n" 19007 "\"\\\xC2\x8D\"", 19008 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 19009 } 19010 19011 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 19012 EXPECT_EQ("const char *sssss =\n" 19013 " \"一二三四五六七八\\\n" 19014 " 九 十\";", 19015 format("const char *sssss = \"一二三四五六七八\\\n" 19016 " 九 十\";", 19017 getLLVMStyleWithColumns(30))); 19018 } 19019 19020 TEST_F(FormatTest, SplitsUTF8LineComments) { 19021 EXPECT_EQ("// aaaaÄ\xc2\x8d", 19022 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 19023 EXPECT_EQ("// Я из лесу\n" 19024 "// вышел; был\n" 19025 "// сильный\n" 19026 "// мороз.", 19027 format("// Я из лесу вышел; был сильный мороз.", 19028 getLLVMStyleWithColumns(13))); 19029 EXPECT_EQ("// 一二三\n" 19030 "// 四五六七\n" 19031 "// 八 九\n" 19032 "// 十", 19033 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 19034 } 19035 19036 TEST_F(FormatTest, SplitsUTF8BlockComments) { 19037 EXPECT_EQ("/* Гляжу,\n" 19038 " * поднимается\n" 19039 " * медленно в\n" 19040 " * гору\n" 19041 " * Лошадка,\n" 19042 " * везущая\n" 19043 " * хворосту\n" 19044 " * воз. */", 19045 format("/* Гляжу, поднимается медленно в гору\n" 19046 " * Лошадка, везущая хворосту воз. */", 19047 getLLVMStyleWithColumns(13))); 19048 EXPECT_EQ( 19049 "/* 一二三\n" 19050 " * 四五六七\n" 19051 " * 八 九\n" 19052 " * 十 */", 19053 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 19054 EXPECT_EQ("/* \n" 19055 " * \n" 19056 " * - */", 19057 format("/* - */", getLLVMStyleWithColumns(12))); 19058 } 19059 19060 #endif // _MSC_VER 19061 19062 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 19063 FormatStyle Style = getLLVMStyle(); 19064 19065 Style.ConstructorInitializerIndentWidth = 4; 19066 verifyFormat( 19067 "SomeClass::Constructor()\n" 19068 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 19069 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 19070 Style); 19071 19072 Style.ConstructorInitializerIndentWidth = 2; 19073 verifyFormat( 19074 "SomeClass::Constructor()\n" 19075 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 19076 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 19077 Style); 19078 19079 Style.ConstructorInitializerIndentWidth = 0; 19080 verifyFormat( 19081 "SomeClass::Constructor()\n" 19082 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 19083 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 19084 Style); 19085 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 19086 verifyFormat( 19087 "SomeLongTemplateVariableName<\n" 19088 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 19089 Style); 19090 verifyFormat("bool smaller = 1 < " 19091 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 19092 " " 19093 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 19094 Style); 19095 19096 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 19097 verifyFormat("SomeClass::Constructor() :\n" 19098 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n" 19099 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}", 19100 Style); 19101 } 19102 19103 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 19104 FormatStyle Style = getLLVMStyle(); 19105 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 19106 Style.ConstructorInitializerIndentWidth = 4; 19107 verifyFormat("SomeClass::Constructor()\n" 19108 " : a(a)\n" 19109 " , b(b)\n" 19110 " , c(c) {}", 19111 Style); 19112 verifyFormat("SomeClass::Constructor()\n" 19113 " : a(a) {}", 19114 Style); 19115 19116 Style.ColumnLimit = 0; 19117 verifyFormat("SomeClass::Constructor()\n" 19118 " : a(a) {}", 19119 Style); 19120 verifyFormat("SomeClass::Constructor() noexcept\n" 19121 " : a(a) {}", 19122 Style); 19123 verifyFormat("SomeClass::Constructor()\n" 19124 " : a(a)\n" 19125 " , b(b)\n" 19126 " , c(c) {}", 19127 Style); 19128 verifyFormat("SomeClass::Constructor()\n" 19129 " : a(a) {\n" 19130 " foo();\n" 19131 " bar();\n" 19132 "}", 19133 Style); 19134 19135 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 19136 verifyFormat("SomeClass::Constructor()\n" 19137 " : a(a)\n" 19138 " , b(b)\n" 19139 " , c(c) {\n}", 19140 Style); 19141 verifyFormat("SomeClass::Constructor()\n" 19142 " : a(a) {\n}", 19143 Style); 19144 19145 Style.ColumnLimit = 80; 19146 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 19147 Style.ConstructorInitializerIndentWidth = 2; 19148 verifyFormat("SomeClass::Constructor()\n" 19149 " : a(a)\n" 19150 " , b(b)\n" 19151 " , c(c) {}", 19152 Style); 19153 19154 Style.ConstructorInitializerIndentWidth = 0; 19155 verifyFormat("SomeClass::Constructor()\n" 19156 ": a(a)\n" 19157 ", b(b)\n" 19158 ", c(c) {}", 19159 Style); 19160 19161 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 19162 Style.ConstructorInitializerIndentWidth = 4; 19163 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 19164 verifyFormat( 19165 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 19166 Style); 19167 verifyFormat( 19168 "SomeClass::Constructor()\n" 19169 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 19170 Style); 19171 Style.ConstructorInitializerIndentWidth = 4; 19172 Style.ColumnLimit = 60; 19173 verifyFormat("SomeClass::Constructor()\n" 19174 " : aaaaaaaa(aaaaaaaa)\n" 19175 " , aaaaaaaa(aaaaaaaa)\n" 19176 " , aaaaaaaa(aaaaaaaa) {}", 19177 Style); 19178 } 19179 19180 TEST_F(FormatTest, Destructors) { 19181 verifyFormat("void F(int &i) { i.~int(); }"); 19182 verifyFormat("void F(int &i) { i->~int(); }"); 19183 } 19184 19185 TEST_F(FormatTest, FormatsWithWebKitStyle) { 19186 FormatStyle Style = getWebKitStyle(); 19187 19188 // Don't indent in outer namespaces. 19189 verifyFormat("namespace outer {\n" 19190 "int i;\n" 19191 "namespace inner {\n" 19192 " int i;\n" 19193 "} // namespace inner\n" 19194 "} // namespace outer\n" 19195 "namespace other_outer {\n" 19196 "int i;\n" 19197 "}", 19198 Style); 19199 19200 // Don't indent case labels. 19201 verifyFormat("switch (variable) {\n" 19202 "case 1:\n" 19203 "case 2:\n" 19204 " doSomething();\n" 19205 " break;\n" 19206 "default:\n" 19207 " ++variable;\n" 19208 "}", 19209 Style); 19210 19211 // Wrap before binary operators. 19212 EXPECT_EQ("void f()\n" 19213 "{\n" 19214 " if (aaaaaaaaaaaaaaaa\n" 19215 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 19216 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 19217 " return;\n" 19218 "}", 19219 format("void f() {\n" 19220 "if (aaaaaaaaaaaaaaaa\n" 19221 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 19222 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 19223 "return;\n" 19224 "}", 19225 Style)); 19226 19227 // Allow functions on a single line. 19228 verifyFormat("void f() { return; }", Style); 19229 19230 // Allow empty blocks on a single line and insert a space in empty blocks. 19231 EXPECT_EQ("void f() { }", format("void f() {}", Style)); 19232 EXPECT_EQ("while (true) { }", format("while (true) {}", Style)); 19233 // However, don't merge non-empty short loops. 19234 EXPECT_EQ("while (true) {\n" 19235 " continue;\n" 19236 "}", 19237 format("while (true) { continue; }", Style)); 19238 19239 // Constructor initializers are formatted one per line with the "," on the 19240 // new line. 19241 verifyFormat("Constructor()\n" 19242 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 19243 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 19244 " aaaaaaaaaaaaaa)\n" 19245 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 19246 "{\n" 19247 "}", 19248 Style); 19249 verifyFormat("SomeClass::Constructor()\n" 19250 " : a(a)\n" 19251 "{\n" 19252 "}", 19253 Style); 19254 EXPECT_EQ("SomeClass::Constructor()\n" 19255 " : a(a)\n" 19256 "{\n" 19257 "}", 19258 format("SomeClass::Constructor():a(a){}", Style)); 19259 verifyFormat("SomeClass::Constructor()\n" 19260 " : a(a)\n" 19261 " , b(b)\n" 19262 " , c(c)\n" 19263 "{\n" 19264 "}", 19265 Style); 19266 verifyFormat("SomeClass::Constructor()\n" 19267 " : a(a)\n" 19268 "{\n" 19269 " foo();\n" 19270 " bar();\n" 19271 "}", 19272 Style); 19273 19274 // Access specifiers should be aligned left. 19275 verifyFormat("class C {\n" 19276 "public:\n" 19277 " int i;\n" 19278 "};", 19279 Style); 19280 19281 // Do not align comments. 19282 verifyFormat("int a; // Do not\n" 19283 "double b; // align comments.", 19284 Style); 19285 19286 // Do not align operands. 19287 EXPECT_EQ("ASSERT(aaaa\n" 19288 " || bbbb);", 19289 format("ASSERT ( aaaa\n||bbbb);", Style)); 19290 19291 // Accept input's line breaks. 19292 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 19293 " || bbbbbbbbbbbbbbb) {\n" 19294 " i++;\n" 19295 "}", 19296 format("if (aaaaaaaaaaaaaaa\n" 19297 "|| bbbbbbbbbbbbbbb) { i++; }", 19298 Style)); 19299 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 19300 " i++;\n" 19301 "}", 19302 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 19303 19304 // Don't automatically break all macro definitions (llvm.org/PR17842). 19305 verifyFormat("#define aNumber 10", Style); 19306 // However, generally keep the line breaks that the user authored. 19307 EXPECT_EQ("#define aNumber \\\n" 19308 " 10", 19309 format("#define aNumber \\\n" 19310 " 10", 19311 Style)); 19312 19313 // Keep empty and one-element array literals on a single line. 19314 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 19315 " copyItems:YES];", 19316 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 19317 "copyItems:YES];", 19318 Style)); 19319 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 19320 " copyItems:YES];", 19321 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 19322 " copyItems:YES];", 19323 Style)); 19324 // FIXME: This does not seem right, there should be more indentation before 19325 // the array literal's entries. Nested blocks have the same problem. 19326 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 19327 " @\"a\",\n" 19328 " @\"a\"\n" 19329 "]\n" 19330 " copyItems:YES];", 19331 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 19332 " @\"a\",\n" 19333 " @\"a\"\n" 19334 " ]\n" 19335 " copyItems:YES];", 19336 Style)); 19337 EXPECT_EQ( 19338 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 19339 " copyItems:YES];", 19340 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 19341 " copyItems:YES];", 19342 Style)); 19343 19344 verifyFormat("[self.a b:c c:d];", Style); 19345 EXPECT_EQ("[self.a b:c\n" 19346 " c:d];", 19347 format("[self.a b:c\n" 19348 "c:d];", 19349 Style)); 19350 } 19351 19352 TEST_F(FormatTest, FormatsLambdas) { 19353 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 19354 verifyFormat( 19355 "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();\n"); 19356 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 19357 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 19358 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 19359 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 19360 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 19361 verifyFormat("auto c = [a = [b = 42] {}] {};\n"); 19362 verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n"); 19363 verifyFormat("int x = f(*+[] {});"); 19364 verifyFormat("void f() {\n" 19365 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 19366 "}\n"); 19367 verifyFormat("void f() {\n" 19368 " other(x.begin(), //\n" 19369 " x.end(), //\n" 19370 " [&](int, int) { return 1; });\n" 19371 "}\n"); 19372 verifyFormat("void f() {\n" 19373 " other.other.other.other.other(\n" 19374 " x.begin(), x.end(),\n" 19375 " [something, rather](int, int, int, int, int, int, int) { " 19376 "return 1; });\n" 19377 "}\n"); 19378 verifyFormat( 19379 "void f() {\n" 19380 " other.other.other.other.other(\n" 19381 " x.begin(), x.end(),\n" 19382 " [something, rather](int, int, int, int, int, int, int) {\n" 19383 " //\n" 19384 " });\n" 19385 "}\n"); 19386 verifyFormat("SomeFunction([]() { // A cool function...\n" 19387 " return 43;\n" 19388 "});"); 19389 EXPECT_EQ("SomeFunction([]() {\n" 19390 "#define A a\n" 19391 " return 43;\n" 19392 "});", 19393 format("SomeFunction([](){\n" 19394 "#define A a\n" 19395 "return 43;\n" 19396 "});")); 19397 verifyFormat("void f() {\n" 19398 " SomeFunction([](decltype(x), A *a) {});\n" 19399 " SomeFunction([](typeof(x), A *a) {});\n" 19400 " SomeFunction([](_Atomic(x), A *a) {});\n" 19401 " SomeFunction([](__underlying_type(x), A *a) {});\n" 19402 "}"); 19403 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 19404 " [](const aaaaaaaaaa &a) { return a; });"); 19405 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 19406 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 19407 "});"); 19408 verifyFormat("Constructor()\n" 19409 " : Field([] { // comment\n" 19410 " int i;\n" 19411 " }) {}"); 19412 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 19413 " return some_parameter.size();\n" 19414 "};"); 19415 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 19416 " [](const string &s) { return s; };"); 19417 verifyFormat("int i = aaaaaa ? 1 //\n" 19418 " : [] {\n" 19419 " return 2; //\n" 19420 " }();"); 19421 verifyFormat("llvm::errs() << \"number of twos is \"\n" 19422 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 19423 " return x == 2; // force break\n" 19424 " });"); 19425 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 19426 " [=](int iiiiiiiiiiii) {\n" 19427 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n" 19428 " aaaaaaaaaaaaaaaaaaaaaaa;\n" 19429 " });", 19430 getLLVMStyleWithColumns(60)); 19431 19432 verifyFormat("SomeFunction({[&] {\n" 19433 " // comment\n" 19434 " },\n" 19435 " [&] {\n" 19436 " // comment\n" 19437 " }});"); 19438 verifyFormat("SomeFunction({[&] {\n" 19439 " // comment\n" 19440 "}});"); 19441 verifyFormat( 19442 "virtual aaaaaaaaaaaaaaaa(\n" 19443 " std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n" 19444 " aaaaa aaaaaaaaa);"); 19445 19446 // Lambdas with return types. 19447 verifyFormat("int c = []() -> int { return 2; }();\n"); 19448 verifyFormat("int c = []() -> int * { return 2; }();\n"); 19449 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 19450 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 19451 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 19452 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 19453 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 19454 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 19455 verifyFormat("[a, a]() -> a<1> {};"); 19456 verifyFormat("[]() -> foo<5 + 2> { return {}; };"); 19457 verifyFormat("[]() -> foo<5 - 2> { return {}; };"); 19458 verifyFormat("[]() -> foo<5 / 2> { return {}; };"); 19459 verifyFormat("[]() -> foo<5 * 2> { return {}; };"); 19460 verifyFormat("[]() -> foo<5 % 2> { return {}; };"); 19461 verifyFormat("[]() -> foo<5 << 2> { return {}; };"); 19462 verifyFormat("[]() -> foo<!5> { return {}; };"); 19463 verifyFormat("[]() -> foo<~5> { return {}; };"); 19464 verifyFormat("[]() -> foo<5 | 2> { return {}; };"); 19465 verifyFormat("[]() -> foo<5 || 2> { return {}; };"); 19466 verifyFormat("[]() -> foo<5 & 2> { return {}; };"); 19467 verifyFormat("[]() -> foo<5 && 2> { return {}; };"); 19468 verifyFormat("[]() -> foo<5 == 2> { return {}; };"); 19469 verifyFormat("[]() -> foo<5 != 2> { return {}; };"); 19470 verifyFormat("[]() -> foo<5 >= 2> { return {}; };"); 19471 verifyFormat("[]() -> foo<5 <= 2> { return {}; };"); 19472 verifyFormat("[]() -> foo<5 < 2> { return {}; };"); 19473 verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };"); 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<5 << 2> { return {}; }};\n" 19497 "} // namespace bar"); 19498 verifyFormat("namespace bar {\n" 19499 "// broken:\n" 19500 "auto foo{[]() -> foo<!5> { return {}; }};\n" 19501 "} // namespace bar"); 19502 verifyFormat("namespace bar {\n" 19503 "// broken:\n" 19504 "auto foo{[]() -> foo<~5> { return {}; }};\n" 19505 "} // namespace bar"); 19506 verifyFormat("namespace bar {\n" 19507 "// broken:\n" 19508 "auto foo{[]() -> foo<5 | 2> { return {}; }};\n" 19509 "} // namespace bar"); 19510 verifyFormat("namespace bar {\n" 19511 "// broken:\n" 19512 "auto foo{[]() -> foo<5 || 2> { return {}; }};\n" 19513 "} // namespace bar"); 19514 verifyFormat("namespace bar {\n" 19515 "// broken:\n" 19516 "auto foo{[]() -> foo<5 & 2> { return {}; }};\n" 19517 "} // namespace bar"); 19518 verifyFormat("namespace bar {\n" 19519 "// broken:\n" 19520 "auto foo{[]() -> foo<5 && 2> { return {}; }};\n" 19521 "} // namespace bar"); 19522 verifyFormat("namespace bar {\n" 19523 "// broken:\n" 19524 "auto foo{[]() -> foo<5 == 2> { return {}; }};\n" 19525 "} // namespace bar"); 19526 verifyFormat("namespace bar {\n" 19527 "// broken:\n" 19528 "auto foo{[]() -> foo<5 != 2> { return {}; }};\n" 19529 "} // namespace bar"); 19530 verifyFormat("namespace bar {\n" 19531 "// broken:\n" 19532 "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n" 19533 "} // namespace bar"); 19534 verifyFormat("namespace bar {\n" 19535 "// broken:\n" 19536 "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n" 19537 "} // namespace bar"); 19538 verifyFormat("namespace bar {\n" 19539 "// broken:\n" 19540 "auto foo{[]() -> foo<5 < 2> { return {}; }};\n" 19541 "} // namespace bar"); 19542 verifyFormat("namespace bar {\n" 19543 "// broken:\n" 19544 "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n" 19545 "} // namespace bar"); 19546 verifyFormat("[]() -> a<1> {};"); 19547 verifyFormat("[]() -> a<1> { ; };"); 19548 verifyFormat("[]() -> a<1> { ; }();"); 19549 verifyFormat("[a, a]() -> a<true> {};"); 19550 verifyFormat("[]() -> a<true> {};"); 19551 verifyFormat("[]() -> a<true> { ; };"); 19552 verifyFormat("[]() -> a<true> { ; }();"); 19553 verifyFormat("[a, a]() -> a<false> {};"); 19554 verifyFormat("[]() -> a<false> {};"); 19555 verifyFormat("[]() -> a<false> { ; };"); 19556 verifyFormat("[]() -> a<false> { ; }();"); 19557 verifyFormat("auto foo{[]() -> foo<false> { ; }};"); 19558 verifyFormat("namespace bar {\n" 19559 "auto foo{[]() -> foo<false> { ; }};\n" 19560 "} // namespace bar"); 19561 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 19562 " int j) -> int {\n" 19563 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 19564 "};"); 19565 verifyFormat( 19566 "aaaaaaaaaaaaaaaaaaaaaa(\n" 19567 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 19568 " return aaaaaaaaaaaaaaaaa;\n" 19569 " });", 19570 getLLVMStyleWithColumns(70)); 19571 verifyFormat("[]() //\n" 19572 " -> int {\n" 19573 " return 1; //\n" 19574 "};"); 19575 verifyFormat("[]() -> Void<T...> {};"); 19576 verifyFormat("[a, b]() -> Tuple<T...> { return {}; };"); 19577 19578 // Lambdas with explicit template argument lists. 19579 verifyFormat( 19580 "auto L = []<template <typename> class T, class U>(T<U> &&a) {};\n"); 19581 19582 // Multiple lambdas in the same parentheses change indentation rules. These 19583 // lambdas are forced to start on new lines. 19584 verifyFormat("SomeFunction(\n" 19585 " []() {\n" 19586 " //\n" 19587 " },\n" 19588 " []() {\n" 19589 " //\n" 19590 " });"); 19591 19592 // A lambda passed as arg0 is always pushed to the next line. 19593 verifyFormat("SomeFunction(\n" 19594 " [this] {\n" 19595 " //\n" 19596 " },\n" 19597 " 1);\n"); 19598 19599 // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like 19600 // the arg0 case above. 19601 auto Style = getGoogleStyle(); 19602 Style.BinPackArguments = false; 19603 verifyFormat("SomeFunction(\n" 19604 " a,\n" 19605 " [this] {\n" 19606 " //\n" 19607 " },\n" 19608 " b);\n", 19609 Style); 19610 verifyFormat("SomeFunction(\n" 19611 " a,\n" 19612 " [this] {\n" 19613 " //\n" 19614 " },\n" 19615 " b);\n"); 19616 19617 // A lambda with a very long line forces arg0 to be pushed out irrespective of 19618 // the BinPackArguments value (as long as the code is wide enough). 19619 verifyFormat( 19620 "something->SomeFunction(\n" 19621 " a,\n" 19622 " [this] {\n" 19623 " " 19624 "D0000000000000000000000000000000000000000000000000000000000001();\n" 19625 " },\n" 19626 " b);\n"); 19627 19628 // A multi-line lambda is pulled up as long as the introducer fits on the 19629 // previous line and there are no further args. 19630 verifyFormat("function(1, [this, that] {\n" 19631 " //\n" 19632 "});\n"); 19633 verifyFormat("function([this, that] {\n" 19634 " //\n" 19635 "});\n"); 19636 // FIXME: this format is not ideal and we should consider forcing the first 19637 // arg onto its own line. 19638 verifyFormat("function(a, b, c, //\n" 19639 " d, [this, that] {\n" 19640 " //\n" 19641 " });\n"); 19642 19643 // Multiple lambdas are treated correctly even when there is a short arg0. 19644 verifyFormat("SomeFunction(\n" 19645 " 1,\n" 19646 " [this] {\n" 19647 " //\n" 19648 " },\n" 19649 " [this] {\n" 19650 " //\n" 19651 " },\n" 19652 " 1);\n"); 19653 19654 // More complex introducers. 19655 verifyFormat("return [i, args...] {};"); 19656 19657 // Not lambdas. 19658 verifyFormat("constexpr char hello[]{\"hello\"};"); 19659 verifyFormat("double &operator[](int i) { return 0; }\n" 19660 "int i;"); 19661 verifyFormat("std::unique_ptr<int[]> foo() {}"); 19662 verifyFormat("int i = a[a][a]->f();"); 19663 verifyFormat("int i = (*b)[a]->f();"); 19664 19665 // Other corner cases. 19666 verifyFormat("void f() {\n" 19667 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 19668 " );\n" 19669 "}"); 19670 19671 // Lambdas created through weird macros. 19672 verifyFormat("void f() {\n" 19673 " MACRO((const AA &a) { return 1; });\n" 19674 " MACRO((AA &a) { return 1; });\n" 19675 "}"); 19676 19677 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 19678 " doo_dah();\n" 19679 " doo_dah();\n" 19680 " })) {\n" 19681 "}"); 19682 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n" 19683 " doo_dah();\n" 19684 " doo_dah();\n" 19685 " })) {\n" 19686 "}"); 19687 verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n" 19688 " doo_dah();\n" 19689 " doo_dah();\n" 19690 " })) {\n" 19691 "}"); 19692 verifyFormat("auto lambda = []() {\n" 19693 " int a = 2\n" 19694 "#if A\n" 19695 " + 2\n" 19696 "#endif\n" 19697 " ;\n" 19698 "};"); 19699 19700 // Lambdas with complex multiline introducers. 19701 verifyFormat( 19702 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 19703 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n" 19704 " -> ::std::unordered_set<\n" 19705 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 19706 " //\n" 19707 " });"); 19708 19709 FormatStyle DoNotMerge = getLLVMStyle(); 19710 DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 19711 verifyFormat("auto c = []() {\n" 19712 " return b;\n" 19713 "};", 19714 "auto c = []() { return b; };", DoNotMerge); 19715 verifyFormat("auto c = []() {\n" 19716 "};", 19717 " auto c = []() {};", DoNotMerge); 19718 19719 FormatStyle MergeEmptyOnly = getLLVMStyle(); 19720 MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty; 19721 verifyFormat("auto c = []() {\n" 19722 " return b;\n" 19723 "};", 19724 "auto c = []() {\n" 19725 " return b;\n" 19726 " };", 19727 MergeEmptyOnly); 19728 verifyFormat("auto c = []() {};", 19729 "auto c = []() {\n" 19730 "};", 19731 MergeEmptyOnly); 19732 19733 FormatStyle MergeInline = getLLVMStyle(); 19734 MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline; 19735 verifyFormat("auto c = []() {\n" 19736 " return b;\n" 19737 "};", 19738 "auto c = []() { return b; };", MergeInline); 19739 verifyFormat("function([]() { return b; })", "function([]() { return b; })", 19740 MergeInline); 19741 verifyFormat("function([]() { return b; }, a)", 19742 "function([]() { return b; }, a)", MergeInline); 19743 verifyFormat("function(a, []() { return b; })", 19744 "function(a, []() { return b; })", MergeInline); 19745 19746 // Check option "BraceWrapping.BeforeLambdaBody" and different state of 19747 // AllowShortLambdasOnASingleLine 19748 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle(); 19749 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 19750 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 19751 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 19752 FormatStyle::ShortLambdaStyle::SLS_None; 19753 verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n" 19754 " []()\n" 19755 " {\n" 19756 " return 17;\n" 19757 " });", 19758 LLVMWithBeforeLambdaBody); 19759 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n" 19760 " []()\n" 19761 " {\n" 19762 " });", 19763 LLVMWithBeforeLambdaBody); 19764 verifyFormat("auto fct_SLS_None = []()\n" 19765 "{\n" 19766 " return 17;\n" 19767 "};", 19768 LLVMWithBeforeLambdaBody); 19769 verifyFormat("TwoNestedLambdas_SLS_None(\n" 19770 " []()\n" 19771 " {\n" 19772 " return Call(\n" 19773 " []()\n" 19774 " {\n" 19775 " return 17;\n" 19776 " });\n" 19777 " });", 19778 LLVMWithBeforeLambdaBody); 19779 verifyFormat("void Fct() {\n" 19780 " return {[]()\n" 19781 " {\n" 19782 " return 17;\n" 19783 " }};\n" 19784 "}", 19785 LLVMWithBeforeLambdaBody); 19786 19787 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 19788 FormatStyle::ShortLambdaStyle::SLS_Empty; 19789 verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n" 19790 " []()\n" 19791 " {\n" 19792 " return 17;\n" 19793 " });", 19794 LLVMWithBeforeLambdaBody); 19795 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});", 19796 LLVMWithBeforeLambdaBody); 19797 verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL" 19798 "ongFunctionName_SLS_Empty(\n" 19799 " []() {});", 19800 LLVMWithBeforeLambdaBody); 19801 verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n" 19802 " []()\n" 19803 " {\n" 19804 " return 17;\n" 19805 " });", 19806 LLVMWithBeforeLambdaBody); 19807 verifyFormat("auto fct_SLS_Empty = []()\n" 19808 "{\n" 19809 " return 17;\n" 19810 "};", 19811 LLVMWithBeforeLambdaBody); 19812 verifyFormat("TwoNestedLambdas_SLS_Empty(\n" 19813 " []()\n" 19814 " {\n" 19815 " return Call([]() {});\n" 19816 " });", 19817 LLVMWithBeforeLambdaBody); 19818 verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n" 19819 " []()\n" 19820 " {\n" 19821 " return Call([]() {});\n" 19822 " });", 19823 LLVMWithBeforeLambdaBody); 19824 verifyFormat( 19825 "FctWithLongLineInLambda_SLS_Empty(\n" 19826 " []()\n" 19827 " {\n" 19828 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 19829 " AndShouldNotBeConsiderAsInline,\n" 19830 " LambdaBodyMustBeBreak);\n" 19831 " });", 19832 LLVMWithBeforeLambdaBody); 19833 19834 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 19835 FormatStyle::ShortLambdaStyle::SLS_Inline; 19836 verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });", 19837 LLVMWithBeforeLambdaBody); 19838 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});", 19839 LLVMWithBeforeLambdaBody); 19840 verifyFormat("auto fct_SLS_Inline = []()\n" 19841 "{\n" 19842 " return 17;\n" 19843 "};", 19844 LLVMWithBeforeLambdaBody); 19845 verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return " 19846 "17; }); });", 19847 LLVMWithBeforeLambdaBody); 19848 verifyFormat( 19849 "FctWithLongLineInLambda_SLS_Inline(\n" 19850 " []()\n" 19851 " {\n" 19852 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 19853 " AndShouldNotBeConsiderAsInline,\n" 19854 " LambdaBodyMustBeBreak);\n" 19855 " });", 19856 LLVMWithBeforeLambdaBody); 19857 verifyFormat("FctWithMultipleParams_SLS_Inline(" 19858 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n" 19859 " []() { return 17; });", 19860 LLVMWithBeforeLambdaBody); 19861 verifyFormat( 19862 "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });", 19863 LLVMWithBeforeLambdaBody); 19864 19865 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 19866 FormatStyle::ShortLambdaStyle::SLS_All; 19867 verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });", 19868 LLVMWithBeforeLambdaBody); 19869 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});", 19870 LLVMWithBeforeLambdaBody); 19871 verifyFormat("auto fct_SLS_All = []() { return 17; };", 19872 LLVMWithBeforeLambdaBody); 19873 verifyFormat("FctWithOneParam_SLS_All(\n" 19874 " []()\n" 19875 " {\n" 19876 " // A cool function...\n" 19877 " return 43;\n" 19878 " });", 19879 LLVMWithBeforeLambdaBody); 19880 verifyFormat("FctWithMultipleParams_SLS_All(" 19881 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n" 19882 " []() { return 17; });", 19883 LLVMWithBeforeLambdaBody); 19884 verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });", 19885 LLVMWithBeforeLambdaBody); 19886 verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });", 19887 LLVMWithBeforeLambdaBody); 19888 verifyFormat( 19889 "FctWithLongLineInLambda_SLS_All(\n" 19890 " []()\n" 19891 " {\n" 19892 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 19893 " AndShouldNotBeConsiderAsInline,\n" 19894 " LambdaBodyMustBeBreak);\n" 19895 " });", 19896 LLVMWithBeforeLambdaBody); 19897 verifyFormat( 19898 "auto fct_SLS_All = []()\n" 19899 "{\n" 19900 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 19901 " AndShouldNotBeConsiderAsInline,\n" 19902 " LambdaBodyMustBeBreak);\n" 19903 "};", 19904 LLVMWithBeforeLambdaBody); 19905 LLVMWithBeforeLambdaBody.BinPackParameters = false; 19906 verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);", 19907 LLVMWithBeforeLambdaBody); 19908 verifyFormat( 19909 "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n" 19910 " FirstParam,\n" 19911 " SecondParam,\n" 19912 " ThirdParam,\n" 19913 " FourthParam);", 19914 LLVMWithBeforeLambdaBody); 19915 verifyFormat("FctWithLongLineInLambda_SLS_All(\n" 19916 " []() { return " 19917 "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n" 19918 " FirstParam,\n" 19919 " SecondParam,\n" 19920 " ThirdParam,\n" 19921 " FourthParam);", 19922 LLVMWithBeforeLambdaBody); 19923 verifyFormat( 19924 "FctWithLongLineInLambda_SLS_All(FirstParam,\n" 19925 " SecondParam,\n" 19926 " ThirdParam,\n" 19927 " FourthParam,\n" 19928 " []() { return SomeValueNotSoLong; });", 19929 LLVMWithBeforeLambdaBody); 19930 verifyFormat("FctWithLongLineInLambda_SLS_All(\n" 19931 " []()\n" 19932 " {\n" 19933 " return " 19934 "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB" 19935 "eConsiderAsInline;\n" 19936 " });", 19937 LLVMWithBeforeLambdaBody); 19938 verifyFormat( 19939 "FctWithLongLineInLambda_SLS_All(\n" 19940 " []()\n" 19941 " {\n" 19942 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 19943 " AndShouldNotBeConsiderAsInline,\n" 19944 " LambdaBodyMustBeBreak);\n" 19945 " });", 19946 LLVMWithBeforeLambdaBody); 19947 verifyFormat("FctWithTwoParams_SLS_All(\n" 19948 " []()\n" 19949 " {\n" 19950 " // A cool function...\n" 19951 " return 43;\n" 19952 " },\n" 19953 " 87);", 19954 LLVMWithBeforeLambdaBody); 19955 verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);", 19956 LLVMWithBeforeLambdaBody); 19957 verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });", 19958 LLVMWithBeforeLambdaBody); 19959 verifyFormat( 19960 "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });", 19961 LLVMWithBeforeLambdaBody); 19962 verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; " 19963 "}); }, x);", 19964 LLVMWithBeforeLambdaBody); 19965 verifyFormat("TwoNestedLambdas_SLS_All(\n" 19966 " []()\n" 19967 " {\n" 19968 " // A cool function...\n" 19969 " return Call([]() { return 17; });\n" 19970 " });", 19971 LLVMWithBeforeLambdaBody); 19972 verifyFormat("TwoNestedLambdas_SLS_All(\n" 19973 " []()\n" 19974 " {\n" 19975 " return Call(\n" 19976 " []()\n" 19977 " {\n" 19978 " // A cool function...\n" 19979 " return 17;\n" 19980 " });\n" 19981 " });", 19982 LLVMWithBeforeLambdaBody); 19983 19984 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 19985 FormatStyle::ShortLambdaStyle::SLS_None; 19986 19987 verifyFormat("auto select = [this]() -> const Library::Object *\n" 19988 "{\n" 19989 " return MyAssignment::SelectFromList(this);\n" 19990 "};\n", 19991 LLVMWithBeforeLambdaBody); 19992 19993 verifyFormat("auto select = [this]() -> const Library::Object &\n" 19994 "{\n" 19995 " return MyAssignment::SelectFromList(this);\n" 19996 "};\n", 19997 LLVMWithBeforeLambdaBody); 19998 19999 verifyFormat("auto select = [this]() -> std::unique_ptr<Object>\n" 20000 "{\n" 20001 " return MyAssignment::SelectFromList(this);\n" 20002 "};\n", 20003 LLVMWithBeforeLambdaBody); 20004 20005 verifyFormat("namespace test {\n" 20006 "class Test {\n" 20007 "public:\n" 20008 " Test() = default;\n" 20009 "};\n" 20010 "} // namespace test", 20011 LLVMWithBeforeLambdaBody); 20012 20013 // Lambdas with different indentation styles. 20014 Style = getLLVMStyleWithColumns(100); 20015 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 20016 " return promise.then(\n" 20017 " [this, &someVariable, someObject = " 20018 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 20019 " return someObject.startAsyncAction().then(\n" 20020 " [this, &someVariable](AsyncActionResult result) " 20021 "mutable { result.processMore(); });\n" 20022 " });\n" 20023 "}\n", 20024 format("SomeResult doSomething(SomeObject promise) {\n" 20025 " return promise.then([this, &someVariable, someObject = " 20026 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 20027 " return someObject.startAsyncAction().then([this, " 20028 "&someVariable](AsyncActionResult result) mutable {\n" 20029 " result.processMore();\n" 20030 " });\n" 20031 " });\n" 20032 "}\n", 20033 Style)); 20034 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope; 20035 verifyFormat("test() {\n" 20036 " ([]() -> {\n" 20037 " int b = 32;\n" 20038 " return 3;\n" 20039 " }).foo();\n" 20040 "}", 20041 Style); 20042 verifyFormat("test() {\n" 20043 " []() -> {\n" 20044 " int b = 32;\n" 20045 " return 3;\n" 20046 " }\n" 20047 "}", 20048 Style); 20049 verifyFormat("std::sort(v.begin(), v.end(),\n" 20050 " [](const auto &someLongArgumentName, const auto " 20051 "&someOtherLongArgumentName) {\n" 20052 " return someLongArgumentName.someMemberVariable < " 20053 "someOtherLongArgumentName.someMemberVariable;\n" 20054 "});", 20055 Style); 20056 verifyFormat("test() {\n" 20057 " (\n" 20058 " []() -> {\n" 20059 " int b = 32;\n" 20060 " return 3;\n" 20061 " },\n" 20062 " foo, bar)\n" 20063 " .foo();\n" 20064 "}", 20065 Style); 20066 verifyFormat("test() {\n" 20067 " ([]() -> {\n" 20068 " int b = 32;\n" 20069 " return 3;\n" 20070 " })\n" 20071 " .foo()\n" 20072 " .bar();\n" 20073 "}", 20074 Style); 20075 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 20076 " return promise.then(\n" 20077 " [this, &someVariable, someObject = " 20078 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 20079 " return someObject.startAsyncAction().then(\n" 20080 " [this, &someVariable](AsyncActionResult result) mutable { " 20081 "result.processMore(); });\n" 20082 " });\n" 20083 "}\n", 20084 format("SomeResult doSomething(SomeObject promise) {\n" 20085 " return promise.then([this, &someVariable, someObject = " 20086 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 20087 " return someObject.startAsyncAction().then([this, " 20088 "&someVariable](AsyncActionResult result) mutable {\n" 20089 " result.processMore();\n" 20090 " });\n" 20091 " });\n" 20092 "}\n", 20093 Style)); 20094 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 20095 " return promise.then([this, &someVariable] {\n" 20096 " return someObject.startAsyncAction().then(\n" 20097 " [this, &someVariable](AsyncActionResult result) mutable { " 20098 "result.processMore(); });\n" 20099 " });\n" 20100 "}\n", 20101 format("SomeResult doSomething(SomeObject promise) {\n" 20102 " return promise.then([this, &someVariable] {\n" 20103 " return someObject.startAsyncAction().then([this, " 20104 "&someVariable](AsyncActionResult result) mutable {\n" 20105 " result.processMore();\n" 20106 " });\n" 20107 " });\n" 20108 "}\n", 20109 Style)); 20110 Style = getGoogleStyle(); 20111 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope; 20112 EXPECT_EQ("#define A \\\n" 20113 " [] { \\\n" 20114 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 20115 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 20116 " }", 20117 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 20118 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 20119 Style)); 20120 // TODO: The current formatting has a minor issue that's not worth fixing 20121 // right now whereby the closing brace is indented relative to the signature 20122 // instead of being aligned. This only happens with macros. 20123 } 20124 20125 TEST_F(FormatTest, LambdaWithLineComments) { 20126 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle(); 20127 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 20128 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 20129 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20130 FormatStyle::ShortLambdaStyle::SLS_All; 20131 20132 verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody); 20133 verifyFormat("auto k = []() // comment\n" 20134 "{ return; }", 20135 LLVMWithBeforeLambdaBody); 20136 verifyFormat("auto k = []() /* comment */ { return; }", 20137 LLVMWithBeforeLambdaBody); 20138 verifyFormat("auto k = []() /* comment */ /* comment */ { return; }", 20139 LLVMWithBeforeLambdaBody); 20140 verifyFormat("auto k = []() // X\n" 20141 "{ return; }", 20142 LLVMWithBeforeLambdaBody); 20143 verifyFormat( 20144 "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n" 20145 "{ return; }", 20146 LLVMWithBeforeLambdaBody); 20147 } 20148 20149 TEST_F(FormatTest, EmptyLinesInLambdas) { 20150 verifyFormat("auto lambda = []() {\n" 20151 " x(); //\n" 20152 "};", 20153 "auto lambda = []() {\n" 20154 "\n" 20155 " x(); //\n" 20156 "\n" 20157 "};"); 20158 } 20159 20160 TEST_F(FormatTest, FormatsBlocks) { 20161 FormatStyle ShortBlocks = getLLVMStyle(); 20162 ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 20163 verifyFormat("int (^Block)(int, int);", ShortBlocks); 20164 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 20165 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 20166 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 20167 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 20168 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 20169 20170 verifyFormat("foo(^{ bar(); });", ShortBlocks); 20171 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 20172 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 20173 20174 verifyFormat("[operation setCompletionBlock:^{\n" 20175 " [self onOperationDone];\n" 20176 "}];"); 20177 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 20178 " [self onOperationDone];\n" 20179 "}]};"); 20180 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 20181 " f();\n" 20182 "}];"); 20183 verifyFormat("int a = [operation block:^int(int *i) {\n" 20184 " return 1;\n" 20185 "}];"); 20186 verifyFormat("[myObject doSomethingWith:arg1\n" 20187 " aaa:^int(int *a) {\n" 20188 " return 1;\n" 20189 " }\n" 20190 " bbb:f(a * bbbbbbbb)];"); 20191 20192 verifyFormat("[operation setCompletionBlock:^{\n" 20193 " [self.delegate newDataAvailable];\n" 20194 "}];", 20195 getLLVMStyleWithColumns(60)); 20196 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 20197 " NSString *path = [self sessionFilePath];\n" 20198 " if (path) {\n" 20199 " // ...\n" 20200 " }\n" 20201 "});"); 20202 verifyFormat("[[SessionService sharedService]\n" 20203 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 20204 " if (window) {\n" 20205 " [self windowDidLoad:window];\n" 20206 " } else {\n" 20207 " [self errorLoadingWindow];\n" 20208 " }\n" 20209 " }];"); 20210 verifyFormat("void (^largeBlock)(void) = ^{\n" 20211 " // ...\n" 20212 "};\n", 20213 getLLVMStyleWithColumns(40)); 20214 verifyFormat("[[SessionService sharedService]\n" 20215 " loadWindowWithCompletionBlock: //\n" 20216 " ^(SessionWindow *window) {\n" 20217 " if (window) {\n" 20218 " [self windowDidLoad:window];\n" 20219 " } else {\n" 20220 " [self errorLoadingWindow];\n" 20221 " }\n" 20222 " }];", 20223 getLLVMStyleWithColumns(60)); 20224 verifyFormat("[myObject doSomethingWith:arg1\n" 20225 " firstBlock:^(Foo *a) {\n" 20226 " // ...\n" 20227 " int i;\n" 20228 " }\n" 20229 " secondBlock:^(Bar *b) {\n" 20230 " // ...\n" 20231 " int i;\n" 20232 " }\n" 20233 " thirdBlock:^Foo(Bar *b) {\n" 20234 " // ...\n" 20235 " int i;\n" 20236 " }];"); 20237 verifyFormat("[myObject doSomethingWith:arg1\n" 20238 " firstBlock:-1\n" 20239 " secondBlock:^(Bar *b) {\n" 20240 " // ...\n" 20241 " int i;\n" 20242 " }];"); 20243 20244 verifyFormat("f(^{\n" 20245 " @autoreleasepool {\n" 20246 " if (a) {\n" 20247 " g();\n" 20248 " }\n" 20249 " }\n" 20250 "});"); 20251 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 20252 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" 20253 "};"); 20254 20255 FormatStyle FourIndent = getLLVMStyle(); 20256 FourIndent.ObjCBlockIndentWidth = 4; 20257 verifyFormat("[operation setCompletionBlock:^{\n" 20258 " [self onOperationDone];\n" 20259 "}];", 20260 FourIndent); 20261 } 20262 20263 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 20264 FormatStyle ZeroColumn = getLLVMStyle(); 20265 ZeroColumn.ColumnLimit = 0; 20266 20267 verifyFormat("[[SessionService sharedService] " 20268 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 20269 " if (window) {\n" 20270 " [self windowDidLoad:window];\n" 20271 " } else {\n" 20272 " [self errorLoadingWindow];\n" 20273 " }\n" 20274 "}];", 20275 ZeroColumn); 20276 EXPECT_EQ("[[SessionService sharedService]\n" 20277 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 20278 " if (window) {\n" 20279 " [self windowDidLoad:window];\n" 20280 " } else {\n" 20281 " [self errorLoadingWindow];\n" 20282 " }\n" 20283 " }];", 20284 format("[[SessionService sharedService]\n" 20285 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 20286 " if (window) {\n" 20287 " [self windowDidLoad:window];\n" 20288 " } else {\n" 20289 " [self errorLoadingWindow];\n" 20290 " }\n" 20291 "}];", 20292 ZeroColumn)); 20293 verifyFormat("[myObject doSomethingWith:arg1\n" 20294 " firstBlock:^(Foo *a) {\n" 20295 " // ...\n" 20296 " int i;\n" 20297 " }\n" 20298 " secondBlock:^(Bar *b) {\n" 20299 " // ...\n" 20300 " int i;\n" 20301 " }\n" 20302 " thirdBlock:^Foo(Bar *b) {\n" 20303 " // ...\n" 20304 " int i;\n" 20305 " }];", 20306 ZeroColumn); 20307 verifyFormat("f(^{\n" 20308 " @autoreleasepool {\n" 20309 " if (a) {\n" 20310 " g();\n" 20311 " }\n" 20312 " }\n" 20313 "});", 20314 ZeroColumn); 20315 verifyFormat("void (^largeBlock)(void) = ^{\n" 20316 " // ...\n" 20317 "};", 20318 ZeroColumn); 20319 20320 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 20321 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 20322 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 20323 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 20324 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 20325 " int i;\n" 20326 "};", 20327 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 20328 } 20329 20330 TEST_F(FormatTest, SupportsCRLF) { 20331 EXPECT_EQ("int a;\r\n" 20332 "int b;\r\n" 20333 "int c;\r\n", 20334 format("int a;\r\n" 20335 " int b;\r\n" 20336 " int c;\r\n", 20337 getLLVMStyle())); 20338 EXPECT_EQ("int a;\r\n" 20339 "int b;\r\n" 20340 "int c;\r\n", 20341 format("int a;\r\n" 20342 " int b;\n" 20343 " int c;\r\n", 20344 getLLVMStyle())); 20345 EXPECT_EQ("int a;\n" 20346 "int b;\n" 20347 "int c;\n", 20348 format("int a;\r\n" 20349 " int b;\n" 20350 " int c;\n", 20351 getLLVMStyle())); 20352 EXPECT_EQ("\"aaaaaaa \"\r\n" 20353 "\"bbbbbbb\";\r\n", 20354 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 20355 EXPECT_EQ("#define A \\\r\n" 20356 " b; \\\r\n" 20357 " c; \\\r\n" 20358 " d;\r\n", 20359 format("#define A \\\r\n" 20360 " b; \\\r\n" 20361 " c; d; \r\n", 20362 getGoogleStyle())); 20363 20364 EXPECT_EQ("/*\r\n" 20365 "multi line block comments\r\n" 20366 "should not introduce\r\n" 20367 "an extra carriage return\r\n" 20368 "*/\r\n", 20369 format("/*\r\n" 20370 "multi line block comments\r\n" 20371 "should not introduce\r\n" 20372 "an extra carriage return\r\n" 20373 "*/\r\n")); 20374 EXPECT_EQ("/*\r\n" 20375 "\r\n" 20376 "*/", 20377 format("/*\r\n" 20378 " \r\r\r\n" 20379 "*/")); 20380 20381 FormatStyle style = getLLVMStyle(); 20382 20383 style.DeriveLineEnding = true; 20384 style.UseCRLF = false; 20385 EXPECT_EQ("union FooBarBazQux {\n" 20386 " int foo;\n" 20387 " int bar;\n" 20388 " int baz;\n" 20389 "};", 20390 format("union FooBarBazQux {\r\n" 20391 " int foo;\n" 20392 " int bar;\r\n" 20393 " int baz;\n" 20394 "};", 20395 style)); 20396 style.UseCRLF = true; 20397 EXPECT_EQ("union FooBarBazQux {\r\n" 20398 " int foo;\r\n" 20399 " int bar;\r\n" 20400 " int baz;\r\n" 20401 "};", 20402 format("union FooBarBazQux {\r\n" 20403 " int foo;\n" 20404 " int bar;\r\n" 20405 " int baz;\n" 20406 "};", 20407 style)); 20408 20409 style.DeriveLineEnding = false; 20410 style.UseCRLF = false; 20411 EXPECT_EQ("union FooBarBazQux {\n" 20412 " int foo;\n" 20413 " int bar;\n" 20414 " int baz;\n" 20415 " int qux;\n" 20416 "};", 20417 format("union FooBarBazQux {\r\n" 20418 " int foo;\n" 20419 " int bar;\r\n" 20420 " int baz;\n" 20421 " int qux;\r\n" 20422 "};", 20423 style)); 20424 style.UseCRLF = true; 20425 EXPECT_EQ("union FooBarBazQux {\r\n" 20426 " int foo;\r\n" 20427 " int bar;\r\n" 20428 " int baz;\r\n" 20429 " int qux;\r\n" 20430 "};", 20431 format("union FooBarBazQux {\r\n" 20432 " int foo;\n" 20433 " int bar;\r\n" 20434 " int baz;\n" 20435 " int qux;\n" 20436 "};", 20437 style)); 20438 20439 style.DeriveLineEnding = true; 20440 style.UseCRLF = false; 20441 EXPECT_EQ("union FooBarBazQux {\r\n" 20442 " int foo;\r\n" 20443 " int bar;\r\n" 20444 " int baz;\r\n" 20445 " int qux;\r\n" 20446 "};", 20447 format("union FooBarBazQux {\r\n" 20448 " int foo;\n" 20449 " int bar;\r\n" 20450 " int baz;\n" 20451 " int qux;\r\n" 20452 "};", 20453 style)); 20454 style.UseCRLF = true; 20455 EXPECT_EQ("union FooBarBazQux {\n" 20456 " int foo;\n" 20457 " int bar;\n" 20458 " int baz;\n" 20459 " int qux;\n" 20460 "};", 20461 format("union FooBarBazQux {\r\n" 20462 " int foo;\n" 20463 " int bar;\r\n" 20464 " int baz;\n" 20465 " int qux;\n" 20466 "};", 20467 style)); 20468 } 20469 20470 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 20471 verifyFormat("MY_CLASS(C) {\n" 20472 " int i;\n" 20473 " int j;\n" 20474 "};"); 20475 } 20476 20477 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 20478 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 20479 TwoIndent.ContinuationIndentWidth = 2; 20480 20481 EXPECT_EQ("int i =\n" 20482 " longFunction(\n" 20483 " arg);", 20484 format("int i = longFunction(arg);", TwoIndent)); 20485 20486 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 20487 SixIndent.ContinuationIndentWidth = 6; 20488 20489 EXPECT_EQ("int i =\n" 20490 " longFunction(\n" 20491 " arg);", 20492 format("int i = longFunction(arg);", SixIndent)); 20493 } 20494 20495 TEST_F(FormatTest, WrappedClosingParenthesisIndent) { 20496 FormatStyle Style = getLLVMStyle(); 20497 verifyFormat("int Foo::getter(\n" 20498 " //\n" 20499 ") const {\n" 20500 " return foo;\n" 20501 "}", 20502 Style); 20503 verifyFormat("void Foo::setter(\n" 20504 " //\n" 20505 ") {\n" 20506 " foo = 1;\n" 20507 "}", 20508 Style); 20509 } 20510 20511 TEST_F(FormatTest, SpacesInAngles) { 20512 FormatStyle Spaces = getLLVMStyle(); 20513 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 20514 20515 verifyFormat("vector< ::std::string > x1;", Spaces); 20516 verifyFormat("Foo< int, Bar > x2;", Spaces); 20517 verifyFormat("Foo< ::int, ::Bar > x3;", Spaces); 20518 20519 verifyFormat("static_cast< int >(arg);", Spaces); 20520 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 20521 verifyFormat("f< int, float >();", Spaces); 20522 verifyFormat("template <> g() {}", Spaces); 20523 verifyFormat("template < std::vector< int > > f() {}", Spaces); 20524 verifyFormat("std::function< void(int, int) > fct;", Spaces); 20525 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 20526 Spaces); 20527 20528 Spaces.Standard = FormatStyle::LS_Cpp03; 20529 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 20530 verifyFormat("A< A< int > >();", Spaces); 20531 20532 Spaces.SpacesInAngles = FormatStyle::SIAS_Never; 20533 verifyFormat("A<A<int> >();", Spaces); 20534 20535 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave; 20536 verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;", 20537 Spaces); 20538 verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;", 20539 Spaces); 20540 20541 verifyFormat("A<A<int> >();", Spaces); 20542 verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces); 20543 verifyFormat("A< A< int > >();", Spaces); 20544 20545 Spaces.Standard = FormatStyle::LS_Cpp11; 20546 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 20547 verifyFormat("A< A< int > >();", Spaces); 20548 20549 Spaces.SpacesInAngles = FormatStyle::SIAS_Never; 20550 verifyFormat("vector<::std::string> x4;", Spaces); 20551 verifyFormat("vector<int> x5;", Spaces); 20552 verifyFormat("Foo<int, Bar> x6;", Spaces); 20553 verifyFormat("Foo<::int, ::Bar> x7;", Spaces); 20554 20555 verifyFormat("A<A<int>>();", Spaces); 20556 20557 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave; 20558 verifyFormat("vector<::std::string> x4;", Spaces); 20559 verifyFormat("vector< ::std::string > x4;", Spaces); 20560 verifyFormat("vector<int> x5;", Spaces); 20561 verifyFormat("vector< int > x5;", Spaces); 20562 verifyFormat("Foo<int, Bar> x6;", Spaces); 20563 verifyFormat("Foo< int, Bar > x6;", Spaces); 20564 verifyFormat("Foo<::int, ::Bar> x7;", Spaces); 20565 verifyFormat("Foo< ::int, ::Bar > x7;", Spaces); 20566 20567 verifyFormat("A<A<int>>();", Spaces); 20568 verifyFormat("A< A< int > >();", Spaces); 20569 verifyFormat("A<A<int > >();", Spaces); 20570 verifyFormat("A< A< int>>();", Spaces); 20571 } 20572 20573 TEST_F(FormatTest, SpaceAfterTemplateKeyword) { 20574 FormatStyle Style = getLLVMStyle(); 20575 Style.SpaceAfterTemplateKeyword = false; 20576 verifyFormat("template<int> void foo();", Style); 20577 } 20578 20579 TEST_F(FormatTest, TripleAngleBrackets) { 20580 verifyFormat("f<<<1, 1>>>();"); 20581 verifyFormat("f<<<1, 1, 1, s>>>();"); 20582 verifyFormat("f<<<a, b, c, d>>>();"); 20583 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 20584 verifyFormat("f<param><<<1, 1>>>();"); 20585 verifyFormat("f<1><<<1, 1>>>();"); 20586 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 20587 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 20588 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 20589 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n" 20590 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();"); 20591 } 20592 20593 TEST_F(FormatTest, MergeLessLessAtEnd) { 20594 verifyFormat("<<"); 20595 EXPECT_EQ("< < <", format("\\\n<<<")); 20596 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 20597 "aaallvm::outs() <<"); 20598 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 20599 "aaaallvm::outs()\n <<"); 20600 } 20601 20602 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 20603 std::string code = "#if A\n" 20604 "#if B\n" 20605 "a.\n" 20606 "#endif\n" 20607 " a = 1;\n" 20608 "#else\n" 20609 "#endif\n" 20610 "#if C\n" 20611 "#else\n" 20612 "#endif\n"; 20613 EXPECT_EQ(code, format(code)); 20614 } 20615 20616 TEST_F(FormatTest, HandleConflictMarkers) { 20617 // Git/SVN conflict markers. 20618 EXPECT_EQ("int a;\n" 20619 "void f() {\n" 20620 " callme(some(parameter1,\n" 20621 "<<<<<<< text by the vcs\n" 20622 " parameter2),\n" 20623 "||||||| text by the vcs\n" 20624 " parameter2),\n" 20625 " parameter3,\n" 20626 "======= text by the vcs\n" 20627 " parameter2, parameter3),\n" 20628 ">>>>>>> text by the vcs\n" 20629 " otherparameter);\n", 20630 format("int a;\n" 20631 "void f() {\n" 20632 " callme(some(parameter1,\n" 20633 "<<<<<<< text by the vcs\n" 20634 " parameter2),\n" 20635 "||||||| text by the vcs\n" 20636 " parameter2),\n" 20637 " parameter3,\n" 20638 "======= text by the vcs\n" 20639 " parameter2,\n" 20640 " parameter3),\n" 20641 ">>>>>>> text by the vcs\n" 20642 " otherparameter);\n")); 20643 20644 // Perforce markers. 20645 EXPECT_EQ("void f() {\n" 20646 " function(\n" 20647 ">>>> text by the vcs\n" 20648 " parameter,\n" 20649 "==== text by the vcs\n" 20650 " parameter,\n" 20651 "==== text by the vcs\n" 20652 " parameter,\n" 20653 "<<<< text by the vcs\n" 20654 " parameter);\n", 20655 format("void f() {\n" 20656 " function(\n" 20657 ">>>> text by the vcs\n" 20658 " parameter,\n" 20659 "==== text by the vcs\n" 20660 " parameter,\n" 20661 "==== text by the vcs\n" 20662 " parameter,\n" 20663 "<<<< text by the vcs\n" 20664 " parameter);\n")); 20665 20666 EXPECT_EQ("<<<<<<<\n" 20667 "|||||||\n" 20668 "=======\n" 20669 ">>>>>>>", 20670 format("<<<<<<<\n" 20671 "|||||||\n" 20672 "=======\n" 20673 ">>>>>>>")); 20674 20675 EXPECT_EQ("<<<<<<<\n" 20676 "|||||||\n" 20677 "int i;\n" 20678 "=======\n" 20679 ">>>>>>>", 20680 format("<<<<<<<\n" 20681 "|||||||\n" 20682 "int i;\n" 20683 "=======\n" 20684 ">>>>>>>")); 20685 20686 // FIXME: Handle parsing of macros around conflict markers correctly: 20687 EXPECT_EQ("#define Macro \\\n" 20688 "<<<<<<<\n" 20689 "Something \\\n" 20690 "|||||||\n" 20691 "Else \\\n" 20692 "=======\n" 20693 "Other \\\n" 20694 ">>>>>>>\n" 20695 " End int i;\n", 20696 format("#define Macro \\\n" 20697 "<<<<<<<\n" 20698 " Something \\\n" 20699 "|||||||\n" 20700 " Else \\\n" 20701 "=======\n" 20702 " Other \\\n" 20703 ">>>>>>>\n" 20704 " End\n" 20705 "int i;\n")); 20706 } 20707 20708 TEST_F(FormatTest, DisableRegions) { 20709 EXPECT_EQ("int i;\n" 20710 "// clang-format off\n" 20711 " int j;\n" 20712 "// clang-format on\n" 20713 "int k;", 20714 format(" int i;\n" 20715 " // clang-format off\n" 20716 " int j;\n" 20717 " // clang-format on\n" 20718 " int k;")); 20719 EXPECT_EQ("int i;\n" 20720 "/* clang-format off */\n" 20721 " int j;\n" 20722 "/* clang-format on */\n" 20723 "int k;", 20724 format(" int i;\n" 20725 " /* clang-format off */\n" 20726 " int j;\n" 20727 " /* clang-format on */\n" 20728 " int k;")); 20729 20730 // Don't reflow comments within disabled regions. 20731 EXPECT_EQ("// clang-format off\n" 20732 "// long long long long long long line\n" 20733 "/* clang-format on */\n" 20734 "/* long long long\n" 20735 " * long long long\n" 20736 " * line */\n" 20737 "int i;\n" 20738 "/* clang-format off */\n" 20739 "/* long long long long long long line */\n", 20740 format("// clang-format off\n" 20741 "// long long long long long long line\n" 20742 "/* clang-format on */\n" 20743 "/* long long long long long long line */\n" 20744 "int i;\n" 20745 "/* clang-format off */\n" 20746 "/* long long long long long long line */\n", 20747 getLLVMStyleWithColumns(20))); 20748 } 20749 20750 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 20751 format("? ) ="); 20752 verifyNoCrash("#define a\\\n /**/}"); 20753 } 20754 20755 TEST_F(FormatTest, FormatsTableGenCode) { 20756 FormatStyle Style = getLLVMStyle(); 20757 Style.Language = FormatStyle::LK_TableGen; 20758 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 20759 } 20760 20761 TEST_F(FormatTest, ArrayOfTemplates) { 20762 EXPECT_EQ("auto a = new unique_ptr<int>[10];", 20763 format("auto a = new unique_ptr<int > [ 10];")); 20764 20765 FormatStyle Spaces = getLLVMStyle(); 20766 Spaces.SpacesInSquareBrackets = true; 20767 EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];", 20768 format("auto a = new unique_ptr<int > [10];", Spaces)); 20769 } 20770 20771 TEST_F(FormatTest, ArrayAsTemplateType) { 20772 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;", 20773 format("auto a = unique_ptr < Foo < Bar>[ 10]> ;")); 20774 20775 FormatStyle Spaces = getLLVMStyle(); 20776 Spaces.SpacesInSquareBrackets = true; 20777 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;", 20778 format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces)); 20779 } 20780 20781 TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); } 20782 20783 TEST(FormatStyle, GetStyleWithEmptyFileName) { 20784 llvm::vfs::InMemoryFileSystem FS; 20785 auto Style1 = getStyle("file", "", "Google", "", &FS); 20786 ASSERT_TRUE((bool)Style1); 20787 ASSERT_EQ(*Style1, getGoogleStyle()); 20788 } 20789 20790 TEST(FormatStyle, GetStyleOfFile) { 20791 llvm::vfs::InMemoryFileSystem FS; 20792 // Test 1: format file in the same directory. 20793 ASSERT_TRUE( 20794 FS.addFile("/a/.clang-format", 0, 20795 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 20796 ASSERT_TRUE( 20797 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 20798 auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS); 20799 ASSERT_TRUE((bool)Style1); 20800 ASSERT_EQ(*Style1, getLLVMStyle()); 20801 20802 // Test 2.1: fallback to default. 20803 ASSERT_TRUE( 20804 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 20805 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS); 20806 ASSERT_TRUE((bool)Style2); 20807 ASSERT_EQ(*Style2, getMozillaStyle()); 20808 20809 // Test 2.2: no format on 'none' fallback style. 20810 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 20811 ASSERT_TRUE((bool)Style2); 20812 ASSERT_EQ(*Style2, getNoStyle()); 20813 20814 // Test 2.3: format if config is found with no based style while fallback is 20815 // 'none'. 20816 ASSERT_TRUE(FS.addFile("/b/.clang-format", 0, 20817 llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2"))); 20818 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 20819 ASSERT_TRUE((bool)Style2); 20820 ASSERT_EQ(*Style2, getLLVMStyle()); 20821 20822 // Test 2.4: format if yaml with no based style, while fallback is 'none'. 20823 Style2 = getStyle("{}", "a.h", "none", "", &FS); 20824 ASSERT_TRUE((bool)Style2); 20825 ASSERT_EQ(*Style2, getLLVMStyle()); 20826 20827 // Test 3: format file in parent directory. 20828 ASSERT_TRUE( 20829 FS.addFile("/c/.clang-format", 0, 20830 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 20831 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, 20832 llvm::MemoryBuffer::getMemBuffer("int i;"))); 20833 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS); 20834 ASSERT_TRUE((bool)Style3); 20835 ASSERT_EQ(*Style3, getGoogleStyle()); 20836 20837 // Test 4: error on invalid fallback style 20838 auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS); 20839 ASSERT_FALSE((bool)Style4); 20840 llvm::consumeError(Style4.takeError()); 20841 20842 // Test 5: error on invalid yaml on command line 20843 auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS); 20844 ASSERT_FALSE((bool)Style5); 20845 llvm::consumeError(Style5.takeError()); 20846 20847 // Test 6: error on invalid style 20848 auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS); 20849 ASSERT_FALSE((bool)Style6); 20850 llvm::consumeError(Style6.takeError()); 20851 20852 // Test 7: found config file, error on parsing it 20853 ASSERT_TRUE( 20854 FS.addFile("/d/.clang-format", 0, 20855 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n" 20856 "InvalidKey: InvalidValue"))); 20857 ASSERT_TRUE( 20858 FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 20859 auto Style7a = getStyle("file", "/d/.clang-format", "LLVM", "", &FS); 20860 ASSERT_FALSE((bool)Style7a); 20861 llvm::consumeError(Style7a.takeError()); 20862 20863 auto Style7b = getStyle("file", "/d/.clang-format", "LLVM", "", &FS, true); 20864 ASSERT_TRUE((bool)Style7b); 20865 20866 // Test 8: inferred per-language defaults apply. 20867 auto StyleTd = getStyle("file", "x.td", "llvm", "", &FS); 20868 ASSERT_TRUE((bool)StyleTd); 20869 ASSERT_EQ(*StyleTd, getLLVMStyle(FormatStyle::LK_TableGen)); 20870 20871 // Test 9.1: overwriting a file style, when parent no file exists with no 20872 // fallback style 20873 ASSERT_TRUE(FS.addFile( 20874 "/e/sub/.clang-format", 0, 20875 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: InheritParentConfig\n" 20876 "ColumnLimit: 20"))); 20877 ASSERT_TRUE(FS.addFile("/e/sub/code.cpp", 0, 20878 llvm::MemoryBuffer::getMemBuffer("int i;"))); 20879 auto Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS); 20880 ASSERT_TRUE(static_cast<bool>(Style9)); 20881 ASSERT_EQ(*Style9, [] { 20882 auto Style = getNoStyle(); 20883 Style.ColumnLimit = 20; 20884 return Style; 20885 }()); 20886 20887 // Test 9.2: with LLVM fallback style 20888 Style9 = getStyle("file", "/e/sub/code.cpp", "LLVM", "", &FS); 20889 ASSERT_TRUE(static_cast<bool>(Style9)); 20890 ASSERT_EQ(*Style9, [] { 20891 auto Style = getLLVMStyle(); 20892 Style.ColumnLimit = 20; 20893 return Style; 20894 }()); 20895 20896 // Test 9.3: with a parent file 20897 ASSERT_TRUE( 20898 FS.addFile("/e/.clang-format", 0, 20899 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google\n" 20900 "UseTab: Always"))); 20901 Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS); 20902 ASSERT_TRUE(static_cast<bool>(Style9)); 20903 ASSERT_EQ(*Style9, [] { 20904 auto Style = getGoogleStyle(); 20905 Style.ColumnLimit = 20; 20906 Style.UseTab = FormatStyle::UT_Always; 20907 return Style; 20908 }()); 20909 20910 // Test 9.4: propagate more than one level 20911 ASSERT_TRUE(FS.addFile("/e/sub/sub/code.cpp", 0, 20912 llvm::MemoryBuffer::getMemBuffer("int i;"))); 20913 ASSERT_TRUE(FS.addFile("/e/sub/sub/.clang-format", 0, 20914 llvm::MemoryBuffer::getMemBuffer( 20915 "BasedOnStyle: InheritParentConfig\n" 20916 "WhitespaceSensitiveMacros: ['FOO', 'BAR']"))); 20917 std::vector<std::string> NonDefaultWhiteSpaceMacros{"FOO", "BAR"}; 20918 20919 const auto SubSubStyle = [&NonDefaultWhiteSpaceMacros] { 20920 auto Style = getGoogleStyle(); 20921 Style.ColumnLimit = 20; 20922 Style.UseTab = FormatStyle::UT_Always; 20923 Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros; 20924 return Style; 20925 }(); 20926 20927 ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros); 20928 Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS); 20929 ASSERT_TRUE(static_cast<bool>(Style9)); 20930 ASSERT_EQ(*Style9, SubSubStyle); 20931 20932 // Test 9.5: use InheritParentConfig as style name 20933 Style9 = 20934 getStyle("inheritparentconfig", "/e/sub/sub/code.cpp", "none", "", &FS); 20935 ASSERT_TRUE(static_cast<bool>(Style9)); 20936 ASSERT_EQ(*Style9, SubSubStyle); 20937 20938 // Test 9.6: use command line style with inheritance 20939 Style9 = getStyle("{BasedOnStyle: InheritParentConfig}", "/e/sub/code.cpp", 20940 "none", "", &FS); 20941 ASSERT_TRUE(static_cast<bool>(Style9)); 20942 ASSERT_EQ(*Style9, SubSubStyle); 20943 20944 // Test 9.7: use command line style with inheritance and own config 20945 Style9 = getStyle("{BasedOnStyle: InheritParentConfig, " 20946 "WhitespaceSensitiveMacros: ['FOO', 'BAR']}", 20947 "/e/sub/code.cpp", "none", "", &FS); 20948 ASSERT_TRUE(static_cast<bool>(Style9)); 20949 ASSERT_EQ(*Style9, SubSubStyle); 20950 20951 // Test 9.8: use inheritance from a file without BasedOnStyle 20952 ASSERT_TRUE(FS.addFile("/e/withoutbase/.clang-format", 0, 20953 llvm::MemoryBuffer::getMemBuffer("ColumnLimit: 123"))); 20954 ASSERT_TRUE( 20955 FS.addFile("/e/withoutbase/sub/.clang-format", 0, 20956 llvm::MemoryBuffer::getMemBuffer( 20957 "BasedOnStyle: InheritParentConfig\nIndentWidth: 7"))); 20958 // Make sure we do not use the fallback style 20959 Style9 = getStyle("file", "/e/withoutbase/code.cpp", "google", "", &FS); 20960 ASSERT_TRUE(static_cast<bool>(Style9)); 20961 ASSERT_EQ(*Style9, [] { 20962 auto Style = getLLVMStyle(); 20963 Style.ColumnLimit = 123; 20964 return Style; 20965 }()); 20966 20967 Style9 = getStyle("file", "/e/withoutbase/sub/code.cpp", "google", "", &FS); 20968 ASSERT_TRUE(static_cast<bool>(Style9)); 20969 ASSERT_EQ(*Style9, [] { 20970 auto Style = getLLVMStyle(); 20971 Style.ColumnLimit = 123; 20972 Style.IndentWidth = 7; 20973 return Style; 20974 }()); 20975 } 20976 20977 TEST_F(ReplacementTest, FormatCodeAfterReplacements) { 20978 // Column limit is 20. 20979 std::string Code = "Type *a =\n" 20980 " new Type();\n" 20981 "g(iiiii, 0, jjjjj,\n" 20982 " 0, kkkkk, 0, mm);\n" 20983 "int bad = format ;"; 20984 std::string Expected = "auto a = new Type();\n" 20985 "g(iiiii, nullptr,\n" 20986 " jjjjj, nullptr,\n" 20987 " kkkkk, nullptr,\n" 20988 " mm);\n" 20989 "int bad = format ;"; 20990 FileID ID = Context.createInMemoryFile("format.cpp", Code); 20991 tooling::Replacements Replaces = toReplacements( 20992 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6, 20993 "auto "), 20994 tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1, 20995 "nullptr"), 20996 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1, 20997 "nullptr"), 20998 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1, 20999 "nullptr")}); 21000 21001 format::FormatStyle Style = format::getLLVMStyle(); 21002 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. 21003 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 21004 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 21005 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 21006 auto Result = applyAllReplacements(Code, *FormattedReplaces); 21007 EXPECT_TRUE(static_cast<bool>(Result)); 21008 EXPECT_EQ(Expected, *Result); 21009 } 21010 21011 TEST_F(ReplacementTest, SortIncludesAfterReplacement) { 21012 std::string Code = "#include \"a.h\"\n" 21013 "#include \"c.h\"\n" 21014 "\n" 21015 "int main() {\n" 21016 " return 0;\n" 21017 "}"; 21018 std::string Expected = "#include \"a.h\"\n" 21019 "#include \"b.h\"\n" 21020 "#include \"c.h\"\n" 21021 "\n" 21022 "int main() {\n" 21023 " return 0;\n" 21024 "}"; 21025 FileID ID = Context.createInMemoryFile("fix.cpp", Code); 21026 tooling::Replacements Replaces = toReplacements( 21027 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0, 21028 "#include \"b.h\"\n")}); 21029 21030 format::FormatStyle Style = format::getLLVMStyle(); 21031 Style.SortIncludes = FormatStyle::SI_CaseSensitive; 21032 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 21033 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 21034 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 21035 auto Result = applyAllReplacements(Code, *FormattedReplaces); 21036 EXPECT_TRUE(static_cast<bool>(Result)); 21037 EXPECT_EQ(Expected, *Result); 21038 } 21039 21040 TEST_F(FormatTest, FormatSortsUsingDeclarations) { 21041 EXPECT_EQ("using std::cin;\n" 21042 "using std::cout;", 21043 format("using std::cout;\n" 21044 "using std::cin;", 21045 getGoogleStyle())); 21046 } 21047 21048 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) { 21049 format::FormatStyle Style = format::getLLVMStyle(); 21050 Style.Standard = FormatStyle::LS_Cpp03; 21051 // cpp03 recognize this string as identifier u8 and literal character 'a' 21052 EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style)); 21053 } 21054 21055 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) { 21056 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers 21057 // all modes, including C++11, C++14 and C++17 21058 EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';")); 21059 } 21060 21061 TEST_F(FormatTest, DoNotFormatLikelyXml) { 21062 EXPECT_EQ("<!-- ;> -->", format("<!-- ;> -->", getGoogleStyle())); 21063 EXPECT_EQ(" <!-- >; -->", format(" <!-- >; -->", getGoogleStyle())); 21064 } 21065 21066 TEST_F(FormatTest, StructuredBindings) { 21067 // Structured bindings is a C++17 feature. 21068 // all modes, including C++11, C++14 and C++17 21069 verifyFormat("auto [a, b] = f();"); 21070 EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();")); 21071 EXPECT_EQ("const auto [a, b] = f();", format("const auto[a, b] = f();")); 21072 EXPECT_EQ("auto const [a, b] = f();", format("auto const[a, b] = f();")); 21073 EXPECT_EQ("auto const volatile [a, b] = f();", 21074 format("auto const volatile[a, b] = f();")); 21075 EXPECT_EQ("auto [a, b, c] = f();", format("auto [ a , b,c ] = f();")); 21076 EXPECT_EQ("auto &[a, b, c] = f();", 21077 format("auto &[ a , b,c ] = f();")); 21078 EXPECT_EQ("auto &&[a, b, c] = f();", 21079 format("auto &&[ a , b,c ] = f();")); 21080 EXPECT_EQ("auto const &[a, b] = f();", format("auto const&[a, b] = f();")); 21081 EXPECT_EQ("auto const volatile &&[a, b] = f();", 21082 format("auto const volatile &&[a, b] = f();")); 21083 EXPECT_EQ("auto const &&[a, b] = f();", 21084 format("auto const && [a, b] = f();")); 21085 EXPECT_EQ("const auto &[a, b] = f();", 21086 format("const auto & [a, b] = f();")); 21087 EXPECT_EQ("const auto volatile &&[a, b] = f();", 21088 format("const auto volatile &&[a, b] = f();")); 21089 EXPECT_EQ("volatile const auto &&[a, b] = f();", 21090 format("volatile const auto &&[a, b] = f();")); 21091 EXPECT_EQ("const auto &&[a, b] = f();", 21092 format("const auto && [a, b] = f();")); 21093 21094 // Make sure we don't mistake structured bindings for lambdas. 21095 FormatStyle PointerMiddle = getLLVMStyle(); 21096 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 21097 verifyFormat("auto [a1, b]{A * i};", getGoogleStyle()); 21098 verifyFormat("auto [a2, b]{A * i};", getLLVMStyle()); 21099 verifyFormat("auto [a3, b]{A * i};", PointerMiddle); 21100 verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle()); 21101 verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle()); 21102 verifyFormat("auto const [a3, b]{A * i};", PointerMiddle); 21103 verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle()); 21104 verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle()); 21105 verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle); 21106 verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle()); 21107 verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle()); 21108 verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle); 21109 21110 EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}", 21111 format("for (const auto && [a, b] : some_range) {\n}")); 21112 EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}", 21113 format("for (const auto & [a, b] : some_range) {\n}")); 21114 EXPECT_EQ("for (const auto [a, b] : some_range) {\n}", 21115 format("for (const auto[a, b] : some_range) {\n}")); 21116 EXPECT_EQ("auto [x, y](expr);", format("auto[x,y] (expr);")); 21117 EXPECT_EQ("auto &[x, y](expr);", format("auto & [x,y] (expr);")); 21118 EXPECT_EQ("auto &&[x, y](expr);", format("auto && [x,y] (expr);")); 21119 EXPECT_EQ("auto const &[x, y](expr);", 21120 format("auto const & [x,y] (expr);")); 21121 EXPECT_EQ("auto const &&[x, y](expr);", 21122 format("auto const && [x,y] (expr);")); 21123 EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y] {expr};")); 21124 EXPECT_EQ("auto const &[x, y]{expr};", 21125 format("auto const & [x,y] {expr};")); 21126 EXPECT_EQ("auto const &&[x, y]{expr};", 21127 format("auto const && [x,y] {expr};")); 21128 21129 format::FormatStyle Spaces = format::getLLVMStyle(); 21130 Spaces.SpacesInSquareBrackets = true; 21131 verifyFormat("auto [ a, b ] = f();", Spaces); 21132 verifyFormat("auto &&[ a, b ] = f();", Spaces); 21133 verifyFormat("auto &[ a, b ] = f();", Spaces); 21134 verifyFormat("auto const &&[ a, b ] = f();", Spaces); 21135 verifyFormat("auto const &[ a, b ] = f();", Spaces); 21136 } 21137 21138 TEST_F(FormatTest, FileAndCode) { 21139 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", "")); 21140 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", "")); 21141 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", "")); 21142 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "")); 21143 EXPECT_EQ(FormatStyle::LK_ObjC, 21144 guessLanguage("foo.h", "@interface Foo\n@end\n")); 21145 EXPECT_EQ( 21146 FormatStyle::LK_ObjC, 21147 guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }")); 21148 EXPECT_EQ(FormatStyle::LK_ObjC, 21149 guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))")); 21150 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;")); 21151 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", "")); 21152 EXPECT_EQ(FormatStyle::LK_ObjC, 21153 guessLanguage("foo", "@interface Foo\n@end\n")); 21154 EXPECT_EQ(FormatStyle::LK_ObjC, 21155 guessLanguage("foo.h", "int DoStuff(CGRect rect);\n")); 21156 EXPECT_EQ( 21157 FormatStyle::LK_ObjC, 21158 guessLanguage("foo.h", 21159 "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n")); 21160 EXPECT_EQ( 21161 FormatStyle::LK_Cpp, 21162 guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;")); 21163 } 21164 21165 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) { 21166 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];")); 21167 EXPECT_EQ(FormatStyle::LK_ObjC, 21168 guessLanguage("foo.h", "array[[calculator getIndex]];")); 21169 EXPECT_EQ(FormatStyle::LK_Cpp, 21170 guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];")); 21171 EXPECT_EQ( 21172 FormatStyle::LK_Cpp, 21173 guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];")); 21174 EXPECT_EQ(FormatStyle::LK_ObjC, 21175 guessLanguage("foo.h", "[[noreturn foo] bar];")); 21176 EXPECT_EQ(FormatStyle::LK_Cpp, 21177 guessLanguage("foo.h", "[[clang::fallthrough]];")); 21178 EXPECT_EQ(FormatStyle::LK_ObjC, 21179 guessLanguage("foo.h", "[[clang:fallthrough] foo];")); 21180 EXPECT_EQ(FormatStyle::LK_Cpp, 21181 guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];")); 21182 EXPECT_EQ(FormatStyle::LK_Cpp, 21183 guessLanguage("foo.h", "[[using clang: fallthrough]];")); 21184 EXPECT_EQ(FormatStyle::LK_ObjC, 21185 guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];")); 21186 EXPECT_EQ(FormatStyle::LK_Cpp, 21187 guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];")); 21188 EXPECT_EQ( 21189 FormatStyle::LK_Cpp, 21190 guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)")); 21191 EXPECT_EQ( 21192 FormatStyle::LK_Cpp, 21193 guessLanguage("foo.h", 21194 "[[clang::callable_when(\"unconsumed\", \"unknown\")]]")); 21195 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]")); 21196 } 21197 21198 TEST_F(FormatTest, GuessLanguageWithCaret) { 21199 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);")); 21200 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);")); 21201 EXPECT_EQ(FormatStyle::LK_ObjC, 21202 guessLanguage("foo.h", "int(^)(char, float);")); 21203 EXPECT_EQ(FormatStyle::LK_ObjC, 21204 guessLanguage("foo.h", "int(^foo)(char, float);")); 21205 EXPECT_EQ(FormatStyle::LK_ObjC, 21206 guessLanguage("foo.h", "int(^foo[10])(char, float);")); 21207 EXPECT_EQ(FormatStyle::LK_ObjC, 21208 guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);")); 21209 EXPECT_EQ( 21210 FormatStyle::LK_ObjC, 21211 guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);")); 21212 } 21213 21214 TEST_F(FormatTest, GuessLanguageWithPragmas) { 21215 EXPECT_EQ(FormatStyle::LK_Cpp, 21216 guessLanguage("foo.h", "__pragma(warning(disable:))")); 21217 EXPECT_EQ(FormatStyle::LK_Cpp, 21218 guessLanguage("foo.h", "#pragma(warning(disable:))")); 21219 EXPECT_EQ(FormatStyle::LK_Cpp, 21220 guessLanguage("foo.h", "_Pragma(warning(disable:))")); 21221 } 21222 21223 TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) { 21224 // ASM symbolic names are identifiers that must be surrounded by [] without 21225 // space in between: 21226 // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands 21227 21228 // Example from https://bugs.llvm.org/show_bug.cgi?id=45108. 21229 verifyFormat(R"(// 21230 asm volatile("mrs %x[result], FPCR" : [result] "=r"(result)); 21231 )"); 21232 21233 // A list of several ASM symbolic names. 21234 verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)"); 21235 21236 // ASM symbolic names in inline ASM with inputs and outputs. 21237 verifyFormat(R"(// 21238 asm("cmoveq %1, %2, %[result]" 21239 : [result] "=r"(result) 21240 : "r"(test), "r"(new), "[result]"(old)); 21241 )"); 21242 21243 // ASM symbolic names in inline ASM with no outputs. 21244 verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)"); 21245 } 21246 21247 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) { 21248 EXPECT_EQ(FormatStyle::LK_Cpp, 21249 guessLanguage("foo.h", "void f() {\n" 21250 " asm (\"mov %[e], %[d]\"\n" 21251 " : [d] \"=rm\" (d)\n" 21252 " [e] \"rm\" (*e));\n" 21253 "}")); 21254 EXPECT_EQ(FormatStyle::LK_Cpp, 21255 guessLanguage("foo.h", "void f() {\n" 21256 " _asm (\"mov %[e], %[d]\"\n" 21257 " : [d] \"=rm\" (d)\n" 21258 " [e] \"rm\" (*e));\n" 21259 "}")); 21260 EXPECT_EQ(FormatStyle::LK_Cpp, 21261 guessLanguage("foo.h", "void f() {\n" 21262 " __asm (\"mov %[e], %[d]\"\n" 21263 " : [d] \"=rm\" (d)\n" 21264 " [e] \"rm\" (*e));\n" 21265 "}")); 21266 EXPECT_EQ(FormatStyle::LK_Cpp, 21267 guessLanguage("foo.h", "void f() {\n" 21268 " __asm__ (\"mov %[e], %[d]\"\n" 21269 " : [d] \"=rm\" (d)\n" 21270 " [e] \"rm\" (*e));\n" 21271 "}")); 21272 EXPECT_EQ(FormatStyle::LK_Cpp, 21273 guessLanguage("foo.h", "void f() {\n" 21274 " asm (\"mov %[e], %[d]\"\n" 21275 " : [d] \"=rm\" (d),\n" 21276 " [e] \"rm\" (*e));\n" 21277 "}")); 21278 EXPECT_EQ(FormatStyle::LK_Cpp, 21279 guessLanguage("foo.h", "void f() {\n" 21280 " asm volatile (\"mov %[e], %[d]\"\n" 21281 " : [d] \"=rm\" (d)\n" 21282 " [e] \"rm\" (*e));\n" 21283 "}")); 21284 } 21285 21286 TEST_F(FormatTest, GuessLanguageWithChildLines) { 21287 EXPECT_EQ(FormatStyle::LK_Cpp, 21288 guessLanguage("foo.h", "#define FOO ({ std::string s; })")); 21289 EXPECT_EQ(FormatStyle::LK_ObjC, 21290 guessLanguage("foo.h", "#define FOO ({ NSString *s; })")); 21291 EXPECT_EQ( 21292 FormatStyle::LK_Cpp, 21293 guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })")); 21294 EXPECT_EQ( 21295 FormatStyle::LK_ObjC, 21296 guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })")); 21297 } 21298 21299 TEST_F(FormatTest, TypenameMacros) { 21300 std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"}; 21301 21302 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353 21303 FormatStyle Google = getGoogleStyleWithColumns(0); 21304 Google.TypenameMacros = TypenameMacros; 21305 verifyFormat("struct foo {\n" 21306 " int bar;\n" 21307 " TAILQ_ENTRY(a) bleh;\n" 21308 "};", 21309 Google); 21310 21311 FormatStyle Macros = getLLVMStyle(); 21312 Macros.TypenameMacros = TypenameMacros; 21313 21314 verifyFormat("STACK_OF(int) a;", Macros); 21315 verifyFormat("STACK_OF(int) *a;", Macros); 21316 verifyFormat("STACK_OF(int const *) *a;", Macros); 21317 verifyFormat("STACK_OF(int *const) *a;", Macros); 21318 verifyFormat("STACK_OF(int, string) a;", Macros); 21319 verifyFormat("STACK_OF(LIST(int)) a;", Macros); 21320 verifyFormat("STACK_OF(LIST(int)) a, b;", Macros); 21321 verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros); 21322 verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros); 21323 verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros); 21324 verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros); 21325 21326 Macros.PointerAlignment = FormatStyle::PAS_Left; 21327 verifyFormat("STACK_OF(int)* a;", Macros); 21328 verifyFormat("STACK_OF(int*)* a;", Macros); 21329 verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros); 21330 verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros); 21331 verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros); 21332 } 21333 21334 TEST_F(FormatTest, AtomicQualifier) { 21335 // Check that we treate _Atomic as a type and not a function call 21336 FormatStyle Google = getGoogleStyleWithColumns(0); 21337 verifyFormat("struct foo {\n" 21338 " int a1;\n" 21339 " _Atomic(a) a2;\n" 21340 " _Atomic(_Atomic(int) *const) a3;\n" 21341 "};", 21342 Google); 21343 verifyFormat("_Atomic(uint64_t) a;"); 21344 verifyFormat("_Atomic(uint64_t) *a;"); 21345 verifyFormat("_Atomic(uint64_t const *) *a;"); 21346 verifyFormat("_Atomic(uint64_t *const) *a;"); 21347 verifyFormat("_Atomic(const uint64_t *) *a;"); 21348 verifyFormat("_Atomic(uint64_t) a;"); 21349 verifyFormat("_Atomic(_Atomic(uint64_t)) a;"); 21350 verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;"); 21351 verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}"); 21352 verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);"); 21353 21354 verifyFormat("_Atomic(uint64_t) *s(InitValue);"); 21355 verifyFormat("_Atomic(uint64_t) *s{InitValue};"); 21356 FormatStyle Style = getLLVMStyle(); 21357 Style.PointerAlignment = FormatStyle::PAS_Left; 21358 verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style); 21359 verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style); 21360 verifyFormat("_Atomic(int)* a;", Style); 21361 verifyFormat("_Atomic(int*)* a;", Style); 21362 verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style); 21363 21364 Style.SpacesInCStyleCastParentheses = true; 21365 Style.SpacesInParentheses = false; 21366 verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style); 21367 Style.SpacesInCStyleCastParentheses = false; 21368 Style.SpacesInParentheses = true; 21369 verifyFormat("x = (_Atomic( uint64_t ))*a;", Style); 21370 verifyFormat("x = (_Atomic( uint64_t ))&a;", Style); 21371 } 21372 21373 TEST_F(FormatTest, AmbersandInLamda) { 21374 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899 21375 FormatStyle AlignStyle = getLLVMStyle(); 21376 AlignStyle.PointerAlignment = FormatStyle::PAS_Left; 21377 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle); 21378 AlignStyle.PointerAlignment = FormatStyle::PAS_Right; 21379 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle); 21380 } 21381 21382 TEST_F(FormatTest, SpacesInConditionalStatement) { 21383 FormatStyle Spaces = getLLVMStyle(); 21384 Spaces.IfMacros.clear(); 21385 Spaces.IfMacros.push_back("MYIF"); 21386 Spaces.SpacesInConditionalStatement = true; 21387 verifyFormat("for ( int i = 0; i; i++ )\n continue;", Spaces); 21388 verifyFormat("if ( !a )\n return;", Spaces); 21389 verifyFormat("if ( a )\n return;", Spaces); 21390 verifyFormat("if constexpr ( a )\n return;", Spaces); 21391 verifyFormat("MYIF ( a )\n return;", Spaces); 21392 verifyFormat("MYIF ( a )\n return;\nelse MYIF ( b )\n return;", Spaces); 21393 verifyFormat("MYIF ( a )\n return;\nelse\n return;", Spaces); 21394 verifyFormat("switch ( a )\ncase 1:\n return;", Spaces); 21395 verifyFormat("while ( a )\n return;", Spaces); 21396 verifyFormat("while ( (a && b) )\n return;", Spaces); 21397 verifyFormat("do {\n} while ( 1 != 0 );", Spaces); 21398 verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces); 21399 // Check that space on the left of "::" is inserted as expected at beginning 21400 // of condition. 21401 verifyFormat("while ( ::func() )\n return;", Spaces); 21402 21403 // Check impact of ControlStatementsExceptControlMacros is honored. 21404 Spaces.SpaceBeforeParens = 21405 FormatStyle::SBPO_ControlStatementsExceptControlMacros; 21406 verifyFormat("MYIF( a )\n return;", Spaces); 21407 verifyFormat("MYIF( a )\n return;\nelse MYIF( b )\n return;", Spaces); 21408 verifyFormat("MYIF( a )\n return;\nelse\n return;", Spaces); 21409 } 21410 21411 TEST_F(FormatTest, AlternativeOperators) { 21412 // Test case for ensuring alternate operators are not 21413 // combined with their right most neighbour. 21414 verifyFormat("int a and b;"); 21415 verifyFormat("int a and_eq b;"); 21416 verifyFormat("int a bitand b;"); 21417 verifyFormat("int a bitor b;"); 21418 verifyFormat("int a compl b;"); 21419 verifyFormat("int a not b;"); 21420 verifyFormat("int a not_eq b;"); 21421 verifyFormat("int a or b;"); 21422 verifyFormat("int a xor b;"); 21423 verifyFormat("int a xor_eq b;"); 21424 verifyFormat("return this not_eq bitand other;"); 21425 verifyFormat("bool operator not_eq(const X bitand other)"); 21426 21427 verifyFormat("int a and 5;"); 21428 verifyFormat("int a and_eq 5;"); 21429 verifyFormat("int a bitand 5;"); 21430 verifyFormat("int a bitor 5;"); 21431 verifyFormat("int a compl 5;"); 21432 verifyFormat("int a not 5;"); 21433 verifyFormat("int a not_eq 5;"); 21434 verifyFormat("int a or 5;"); 21435 verifyFormat("int a xor 5;"); 21436 verifyFormat("int a xor_eq 5;"); 21437 21438 verifyFormat("int a compl(5);"); 21439 verifyFormat("int a not(5);"); 21440 21441 /* FIXME handle alternate tokens 21442 * https://en.cppreference.com/w/cpp/language/operator_alternative 21443 // alternative tokens 21444 verifyFormat("compl foo();"); // ~foo(); 21445 verifyFormat("foo() <%%>;"); // foo(); 21446 verifyFormat("void foo() <%%>;"); // void foo(){} 21447 verifyFormat("int a <:1:>;"); // int a[1];[ 21448 verifyFormat("%:define ABC abc"); // #define ABC abc 21449 verifyFormat("%:%:"); // ## 21450 */ 21451 } 21452 21453 TEST_F(FormatTest, STLWhileNotDefineChed) { 21454 verifyFormat("#if defined(while)\n" 21455 "#define while EMIT WARNING C4005\n" 21456 "#endif // while"); 21457 } 21458 21459 TEST_F(FormatTest, OperatorSpacing) { 21460 FormatStyle Style = getLLVMStyle(); 21461 Style.PointerAlignment = FormatStyle::PAS_Right; 21462 verifyFormat("Foo::operator*();", Style); 21463 verifyFormat("Foo::operator void *();", Style); 21464 verifyFormat("Foo::operator void **();", Style); 21465 verifyFormat("Foo::operator void *&();", Style); 21466 verifyFormat("Foo::operator void *&&();", Style); 21467 verifyFormat("Foo::operator void const *();", Style); 21468 verifyFormat("Foo::operator void const **();", Style); 21469 verifyFormat("Foo::operator void const *&();", Style); 21470 verifyFormat("Foo::operator void const *&&();", Style); 21471 verifyFormat("Foo::operator()(void *);", Style); 21472 verifyFormat("Foo::operator*(void *);", Style); 21473 verifyFormat("Foo::operator*();", Style); 21474 verifyFormat("Foo::operator**();", Style); 21475 verifyFormat("Foo::operator&();", Style); 21476 verifyFormat("Foo::operator<int> *();", Style); 21477 verifyFormat("Foo::operator<Foo> *();", Style); 21478 verifyFormat("Foo::operator<int> **();", Style); 21479 verifyFormat("Foo::operator<Foo> **();", Style); 21480 verifyFormat("Foo::operator<int> &();", Style); 21481 verifyFormat("Foo::operator<Foo> &();", Style); 21482 verifyFormat("Foo::operator<int> &&();", Style); 21483 verifyFormat("Foo::operator<Foo> &&();", Style); 21484 verifyFormat("Foo::operator<int> *&();", Style); 21485 verifyFormat("Foo::operator<Foo> *&();", Style); 21486 verifyFormat("Foo::operator<int> *&&();", Style); 21487 verifyFormat("Foo::operator<Foo> *&&();", Style); 21488 verifyFormat("operator*(int (*)(), class Foo);", Style); 21489 21490 verifyFormat("Foo::operator&();", Style); 21491 verifyFormat("Foo::operator void &();", Style); 21492 verifyFormat("Foo::operator void const &();", Style); 21493 verifyFormat("Foo::operator()(void &);", Style); 21494 verifyFormat("Foo::operator&(void &);", Style); 21495 verifyFormat("Foo::operator&();", Style); 21496 verifyFormat("operator&(int (&)(), class Foo);", Style); 21497 21498 verifyFormat("Foo::operator&&();", Style); 21499 verifyFormat("Foo::operator**();", Style); 21500 verifyFormat("Foo::operator void &&();", Style); 21501 verifyFormat("Foo::operator void const &&();", Style); 21502 verifyFormat("Foo::operator()(void &&);", Style); 21503 verifyFormat("Foo::operator&&(void &&);", Style); 21504 verifyFormat("Foo::operator&&();", Style); 21505 verifyFormat("operator&&(int(&&)(), class Foo);", Style); 21506 verifyFormat("operator const nsTArrayRight<E> &()", Style); 21507 verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()", 21508 Style); 21509 verifyFormat("operator void **()", Style); 21510 verifyFormat("operator const FooRight<Object> &()", Style); 21511 verifyFormat("operator const FooRight<Object> *()", Style); 21512 verifyFormat("operator const FooRight<Object> **()", Style); 21513 verifyFormat("operator const FooRight<Object> *&()", Style); 21514 verifyFormat("operator const FooRight<Object> *&&()", Style); 21515 21516 Style.PointerAlignment = FormatStyle::PAS_Left; 21517 verifyFormat("Foo::operator*();", Style); 21518 verifyFormat("Foo::operator**();", Style); 21519 verifyFormat("Foo::operator void*();", Style); 21520 verifyFormat("Foo::operator void**();", Style); 21521 verifyFormat("Foo::operator void*&();", Style); 21522 verifyFormat("Foo::operator void*&&();", Style); 21523 verifyFormat("Foo::operator void const*();", Style); 21524 verifyFormat("Foo::operator void const**();", Style); 21525 verifyFormat("Foo::operator void const*&();", Style); 21526 verifyFormat("Foo::operator void const*&&();", Style); 21527 verifyFormat("Foo::operator/*comment*/ void*();", Style); 21528 verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style); 21529 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style); 21530 verifyFormat("Foo::operator()(void*);", Style); 21531 verifyFormat("Foo::operator*(void*);", Style); 21532 verifyFormat("Foo::operator*();", Style); 21533 verifyFormat("Foo::operator<int>*();", Style); 21534 verifyFormat("Foo::operator<Foo>*();", Style); 21535 verifyFormat("Foo::operator<int>**();", Style); 21536 verifyFormat("Foo::operator<Foo>**();", Style); 21537 verifyFormat("Foo::operator<Foo>*&();", Style); 21538 verifyFormat("Foo::operator<int>&();", Style); 21539 verifyFormat("Foo::operator<Foo>&();", Style); 21540 verifyFormat("Foo::operator<int>&&();", Style); 21541 verifyFormat("Foo::operator<Foo>&&();", Style); 21542 verifyFormat("Foo::operator<int>*&();", Style); 21543 verifyFormat("Foo::operator<Foo>*&();", Style); 21544 verifyFormat("operator*(int (*)(), class Foo);", Style); 21545 21546 verifyFormat("Foo::operator&();", Style); 21547 verifyFormat("Foo::operator void&();", Style); 21548 verifyFormat("Foo::operator void const&();", Style); 21549 verifyFormat("Foo::operator/*comment*/ void&();", Style); 21550 verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style); 21551 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style); 21552 verifyFormat("Foo::operator()(void&);", Style); 21553 verifyFormat("Foo::operator&(void&);", Style); 21554 verifyFormat("Foo::operator&();", Style); 21555 verifyFormat("operator&(int (&)(), class Foo);", Style); 21556 21557 verifyFormat("Foo::operator&&();", Style); 21558 verifyFormat("Foo::operator void&&();", Style); 21559 verifyFormat("Foo::operator void const&&();", Style); 21560 verifyFormat("Foo::operator/*comment*/ void&&();", Style); 21561 verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style); 21562 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style); 21563 verifyFormat("Foo::operator()(void&&);", Style); 21564 verifyFormat("Foo::operator&&(void&&);", Style); 21565 verifyFormat("Foo::operator&&();", Style); 21566 verifyFormat("operator&&(int(&&)(), class Foo);", Style); 21567 verifyFormat("operator const nsTArrayLeft<E>&()", Style); 21568 verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()", 21569 Style); 21570 verifyFormat("operator void**()", Style); 21571 verifyFormat("operator const FooLeft<Object>&()", Style); 21572 verifyFormat("operator const FooLeft<Object>*()", Style); 21573 verifyFormat("operator const FooLeft<Object>**()", Style); 21574 verifyFormat("operator const FooLeft<Object>*&()", Style); 21575 verifyFormat("operator const FooLeft<Object>*&&()", Style); 21576 21577 // PR45107 21578 verifyFormat("operator Vector<String>&();", Style); 21579 verifyFormat("operator const Vector<String>&();", Style); 21580 verifyFormat("operator foo::Bar*();", Style); 21581 verifyFormat("operator const Foo<X>::Bar<Y>*();", Style); 21582 verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();", 21583 Style); 21584 21585 Style.PointerAlignment = FormatStyle::PAS_Middle; 21586 verifyFormat("Foo::operator*();", Style); 21587 verifyFormat("Foo::operator void *();", Style); 21588 verifyFormat("Foo::operator()(void *);", Style); 21589 verifyFormat("Foo::operator*(void *);", Style); 21590 verifyFormat("Foo::operator*();", Style); 21591 verifyFormat("operator*(int (*)(), class Foo);", Style); 21592 21593 verifyFormat("Foo::operator&();", Style); 21594 verifyFormat("Foo::operator void &();", Style); 21595 verifyFormat("Foo::operator void const &();", Style); 21596 verifyFormat("Foo::operator()(void &);", Style); 21597 verifyFormat("Foo::operator&(void &);", Style); 21598 verifyFormat("Foo::operator&();", Style); 21599 verifyFormat("operator&(int (&)(), class Foo);", Style); 21600 21601 verifyFormat("Foo::operator&&();", Style); 21602 verifyFormat("Foo::operator void &&();", Style); 21603 verifyFormat("Foo::operator void const &&();", Style); 21604 verifyFormat("Foo::operator()(void &&);", Style); 21605 verifyFormat("Foo::operator&&(void &&);", Style); 21606 verifyFormat("Foo::operator&&();", Style); 21607 verifyFormat("operator&&(int(&&)(), class Foo);", Style); 21608 } 21609 21610 TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) { 21611 FormatStyle Style = getLLVMStyle(); 21612 // PR46157 21613 verifyFormat("foo(operator+, -42);", Style); 21614 verifyFormat("foo(operator++, -42);", Style); 21615 verifyFormat("foo(operator--, -42);", Style); 21616 verifyFormat("foo(-42, operator--);", Style); 21617 verifyFormat("foo(-42, operator, );", Style); 21618 verifyFormat("foo(operator, , -42);", Style); 21619 } 21620 21621 TEST_F(FormatTest, WhitespaceSensitiveMacros) { 21622 FormatStyle Style = getLLVMStyle(); 21623 Style.WhitespaceSensitiveMacros.push_back("FOO"); 21624 21625 // Don't use the helpers here, since 'mess up' will change the whitespace 21626 // and these are all whitespace sensitive by definition 21627 EXPECT_EQ("FOO(String-ized&Messy+But(: :Still)=Intentional);", 21628 format("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style)); 21629 EXPECT_EQ( 21630 "FOO(String-ized&Messy+But\\(: :Still)=Intentional);", 21631 format("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style)); 21632 EXPECT_EQ("FOO(String-ized&Messy+But,: :Still=Intentional);", 21633 format("FOO(String-ized&Messy+But,: :Still=Intentional);", Style)); 21634 EXPECT_EQ("FOO(String-ized&Messy+But,: :\n" 21635 " Still=Intentional);", 21636 format("FOO(String-ized&Messy+But,: :\n" 21637 " Still=Intentional);", 21638 Style)); 21639 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 21640 EXPECT_EQ("FOO(String-ized=&Messy+But,: :\n" 21641 " Still=Intentional);", 21642 format("FOO(String-ized=&Messy+But,: :\n" 21643 " Still=Intentional);", 21644 Style)); 21645 21646 Style.ColumnLimit = 21; 21647 EXPECT_EQ("FOO(String-ized&Messy+But: :Still=Intentional);", 21648 format("FOO(String-ized&Messy+But: :Still=Intentional);", Style)); 21649 } 21650 21651 TEST_F(FormatTest, VeryLongNamespaceCommentSplit) { 21652 // These tests are not in NamespaceFixer because that doesn't 21653 // test its interaction with line wrapping 21654 FormatStyle Style = getLLVMStyle(); 21655 Style.ColumnLimit = 80; 21656 verifyFormat("namespace {\n" 21657 "int i;\n" 21658 "int j;\n" 21659 "} // namespace", 21660 Style); 21661 21662 verifyFormat("namespace AAA {\n" 21663 "int i;\n" 21664 "int j;\n" 21665 "} // namespace AAA", 21666 Style); 21667 21668 EXPECT_EQ("namespace Averyveryveryverylongnamespace {\n" 21669 "int i;\n" 21670 "int j;\n" 21671 "} // namespace Averyveryveryverylongnamespace", 21672 format("namespace Averyveryveryverylongnamespace {\n" 21673 "int i;\n" 21674 "int j;\n" 21675 "}", 21676 Style)); 21677 21678 EXPECT_EQ( 21679 "namespace " 21680 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" 21681 " went::mad::now {\n" 21682 "int i;\n" 21683 "int j;\n" 21684 "} // namespace\n" 21685 " // " 21686 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" 21687 "went::mad::now", 21688 format("namespace " 21689 "would::it::save::you::a::lot::of::time::if_::i::" 21690 "just::gave::up::and_::went::mad::now {\n" 21691 "int i;\n" 21692 "int j;\n" 21693 "}", 21694 Style)); 21695 21696 // This used to duplicate the comment again and again on subsequent runs 21697 EXPECT_EQ( 21698 "namespace " 21699 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" 21700 " went::mad::now {\n" 21701 "int i;\n" 21702 "int j;\n" 21703 "} // namespace\n" 21704 " // " 21705 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" 21706 "went::mad::now", 21707 format("namespace " 21708 "would::it::save::you::a::lot::of::time::if_::i::" 21709 "just::gave::up::and_::went::mad::now {\n" 21710 "int i;\n" 21711 "int j;\n" 21712 "} // namespace\n" 21713 " // " 21714 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::" 21715 "and_::went::mad::now", 21716 Style)); 21717 } 21718 21719 TEST_F(FormatTest, LikelyUnlikely) { 21720 FormatStyle Style = getLLVMStyle(); 21721 21722 verifyFormat("if (argc > 5) [[unlikely]] {\n" 21723 " return 29;\n" 21724 "}", 21725 Style); 21726 21727 verifyFormat("if (argc > 5) [[likely]] {\n" 21728 " return 29;\n" 21729 "}", 21730 Style); 21731 21732 verifyFormat("if (argc > 5) [[unlikely]] {\n" 21733 " return 29;\n" 21734 "} else [[likely]] {\n" 21735 " return 42;\n" 21736 "}\n", 21737 Style); 21738 21739 verifyFormat("if (argc > 5) [[unlikely]] {\n" 21740 " return 29;\n" 21741 "} else if (argc > 10) [[likely]] {\n" 21742 " return 99;\n" 21743 "} else {\n" 21744 " return 42;\n" 21745 "}\n", 21746 Style); 21747 21748 verifyFormat("if (argc > 5) [[gnu::unused]] {\n" 21749 " return 29;\n" 21750 "}", 21751 Style); 21752 } 21753 21754 TEST_F(FormatTest, PenaltyIndentedWhitespace) { 21755 verifyFormat("Constructor()\n" 21756 " : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 21757 " aaaa(aaaaaaaaaaaaaaaaaa, " 21758 "aaaaaaaaaaaaaaaaaat))"); 21759 verifyFormat("Constructor()\n" 21760 " : aaaaaaaaaaaaa(aaaaaa), " 21761 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)"); 21762 21763 FormatStyle StyleWithWhitespacePenalty = getLLVMStyle(); 21764 StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5; 21765 verifyFormat("Constructor()\n" 21766 " : aaaaaa(aaaaaa),\n" 21767 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 21768 " aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))", 21769 StyleWithWhitespacePenalty); 21770 verifyFormat("Constructor()\n" 21771 " : aaaaaaaaaaaaa(aaaaaa), " 21772 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)", 21773 StyleWithWhitespacePenalty); 21774 } 21775 21776 TEST_F(FormatTest, LLVMDefaultStyle) { 21777 FormatStyle Style = getLLVMStyle(); 21778 verifyFormat("extern \"C\" {\n" 21779 "int foo();\n" 21780 "}", 21781 Style); 21782 } 21783 TEST_F(FormatTest, GNUDefaultStyle) { 21784 FormatStyle Style = getGNUStyle(); 21785 verifyFormat("extern \"C\"\n" 21786 "{\n" 21787 " int foo ();\n" 21788 "}", 21789 Style); 21790 } 21791 TEST_F(FormatTest, MozillaDefaultStyle) { 21792 FormatStyle Style = getMozillaStyle(); 21793 verifyFormat("extern \"C\"\n" 21794 "{\n" 21795 " int foo();\n" 21796 "}", 21797 Style); 21798 } 21799 TEST_F(FormatTest, GoogleDefaultStyle) { 21800 FormatStyle Style = getGoogleStyle(); 21801 verifyFormat("extern \"C\" {\n" 21802 "int foo();\n" 21803 "}", 21804 Style); 21805 } 21806 TEST_F(FormatTest, ChromiumDefaultStyle) { 21807 FormatStyle Style = getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp); 21808 verifyFormat("extern \"C\" {\n" 21809 "int foo();\n" 21810 "}", 21811 Style); 21812 } 21813 TEST_F(FormatTest, MicrosoftDefaultStyle) { 21814 FormatStyle Style = getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp); 21815 verifyFormat("extern \"C\"\n" 21816 "{\n" 21817 " int foo();\n" 21818 "}", 21819 Style); 21820 } 21821 TEST_F(FormatTest, WebKitDefaultStyle) { 21822 FormatStyle Style = getWebKitStyle(); 21823 verifyFormat("extern \"C\" {\n" 21824 "int foo();\n" 21825 "}", 21826 Style); 21827 } 21828 21829 TEST_F(FormatTest, ConceptsAndRequires) { 21830 FormatStyle Style = getLLVMStyle(); 21831 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 21832 21833 verifyFormat("template <typename T>\n" 21834 "concept Hashable = requires(T a) {\n" 21835 " { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n" 21836 "};", 21837 Style); 21838 verifyFormat("template <typename T>\n" 21839 "concept EqualityComparable = requires(T a, T b) {\n" 21840 " { a == b } -> bool;\n" 21841 "};", 21842 Style); 21843 verifyFormat("template <typename T>\n" 21844 "concept EqualityComparable = requires(T a, T b) {\n" 21845 " { a == b } -> bool;\n" 21846 " { a != b } -> bool;\n" 21847 "};", 21848 Style); 21849 verifyFormat("template <typename T>\n" 21850 "concept EqualityComparable = requires(T a, T b) {\n" 21851 " { a == b } -> bool;\n" 21852 " { a != b } -> bool;\n" 21853 "};", 21854 Style); 21855 21856 verifyFormat("template <typename It>\n" 21857 "requires Iterator<It>\n" 21858 "void sort(It begin, It end) {\n" 21859 " //....\n" 21860 "}", 21861 Style); 21862 21863 verifyFormat("template <typename T>\n" 21864 "concept Large = sizeof(T) > 10;", 21865 Style); 21866 21867 verifyFormat("template <typename T, typename U>\n" 21868 "concept FooableWith = requires(T t, U u) {\n" 21869 " typename T::foo_type;\n" 21870 " { t.foo(u) } -> typename T::foo_type;\n" 21871 " t++;\n" 21872 "};\n" 21873 "void doFoo(FooableWith<int> auto t) {\n" 21874 " t.foo(3);\n" 21875 "}", 21876 Style); 21877 verifyFormat("template <typename T>\n" 21878 "concept Context = sizeof(T) == 1;", 21879 Style); 21880 verifyFormat("template <typename T>\n" 21881 "concept Context = is_specialization_of_v<context, T>;", 21882 Style); 21883 verifyFormat("template <typename T>\n" 21884 "concept Node = std::is_object_v<T>;", 21885 Style); 21886 verifyFormat("template <typename T>\n" 21887 "concept Tree = true;", 21888 Style); 21889 21890 verifyFormat("template <typename T> int g(T i) requires Concept1<I> {\n" 21891 " //...\n" 21892 "}", 21893 Style); 21894 21895 verifyFormat( 21896 "template <typename T> int g(T i) requires Concept1<I> && Concept2<I> {\n" 21897 " //...\n" 21898 "}", 21899 Style); 21900 21901 verifyFormat( 21902 "template <typename T> int g(T i) requires Concept1<I> || Concept2<I> {\n" 21903 " //...\n" 21904 "}", 21905 Style); 21906 21907 verifyFormat("template <typename T>\n" 21908 "veryveryvery_long_return_type g(T i) requires Concept1<I> || " 21909 "Concept2<I> {\n" 21910 " //...\n" 21911 "}", 21912 Style); 21913 21914 verifyFormat("template <typename T>\n" 21915 "veryveryvery_long_return_type g(T i) requires Concept1<I> && " 21916 "Concept2<I> {\n" 21917 " //...\n" 21918 "}", 21919 Style); 21920 21921 verifyFormat( 21922 "template <typename T>\n" 21923 "veryveryvery_long_return_type g(T i) requires Concept1 && Concept2 {\n" 21924 " //...\n" 21925 "}", 21926 Style); 21927 21928 verifyFormat( 21929 "template <typename T>\n" 21930 "veryveryvery_long_return_type g(T i) requires Concept1 || Concept2 {\n" 21931 " //...\n" 21932 "}", 21933 Style); 21934 21935 verifyFormat("template <typename It>\n" 21936 "requires Foo<It>() && Bar<It> {\n" 21937 " //....\n" 21938 "}", 21939 Style); 21940 21941 verifyFormat("template <typename It>\n" 21942 "requires Foo<Bar<It>>() && Bar<Foo<It, It>> {\n" 21943 " //....\n" 21944 "}", 21945 Style); 21946 21947 verifyFormat("template <typename It>\n" 21948 "requires Foo<Bar<It, It>>() && Bar<Foo<It, It>> {\n" 21949 " //....\n" 21950 "}", 21951 Style); 21952 21953 verifyFormat( 21954 "template <typename It>\n" 21955 "requires Foo<Bar<It>, Baz<It>>() && Bar<Foo<It>, Baz<It, It>> {\n" 21956 " //....\n" 21957 "}", 21958 Style); 21959 21960 Style.IndentRequires = true; 21961 verifyFormat("template <typename It>\n" 21962 " requires Iterator<It>\n" 21963 "void sort(It begin, It end) {\n" 21964 " //....\n" 21965 "}", 21966 Style); 21967 verifyFormat("template <std::size index_>\n" 21968 " requires(index_ < sizeof...(Children_))\n" 21969 "Tree auto &child() {\n" 21970 " // ...\n" 21971 "}", 21972 Style); 21973 21974 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 21975 verifyFormat("template <typename T>\n" 21976 "concept Hashable = requires (T a) {\n" 21977 " { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n" 21978 "};", 21979 Style); 21980 21981 verifyFormat("template <class T = void>\n" 21982 " requires EqualityComparable<T> || Same<T, void>\n" 21983 "struct equal_to;", 21984 Style); 21985 21986 verifyFormat("template <class T>\n" 21987 " requires requires {\n" 21988 " T{};\n" 21989 " T (int);\n" 21990 " }\n", 21991 Style); 21992 21993 Style.ColumnLimit = 78; 21994 verifyFormat("template <typename T>\n" 21995 "concept Context = Traits<typename T::traits_type> and\n" 21996 " Interface<typename T::interface_type> and\n" 21997 " Request<typename T::request_type> and\n" 21998 " Response<typename T::response_type> and\n" 21999 " ContextExtension<typename T::extension_type> and\n" 22000 " ::std::is_copy_constructable<T> and " 22001 "::std::is_move_constructable<T> and\n" 22002 " requires (T c) {\n" 22003 " { c.response; } -> Response;\n" 22004 "} and requires (T c) {\n" 22005 " { c.request; } -> Request;\n" 22006 "}\n", 22007 Style); 22008 22009 verifyFormat("template <typename T>\n" 22010 "concept Context = Traits<typename T::traits_type> or\n" 22011 " Interface<typename T::interface_type> or\n" 22012 " Request<typename T::request_type> or\n" 22013 " Response<typename T::response_type> or\n" 22014 " ContextExtension<typename T::extension_type> or\n" 22015 " ::std::is_copy_constructable<T> or " 22016 "::std::is_move_constructable<T> or\n" 22017 " requires (T c) {\n" 22018 " { c.response; } -> Response;\n" 22019 "} or requires (T c) {\n" 22020 " { c.request; } -> Request;\n" 22021 "}\n", 22022 Style); 22023 22024 verifyFormat("template <typename T>\n" 22025 "concept Context = Traits<typename T::traits_type> &&\n" 22026 " Interface<typename T::interface_type> &&\n" 22027 " Request<typename T::request_type> &&\n" 22028 " Response<typename T::response_type> &&\n" 22029 " ContextExtension<typename T::extension_type> &&\n" 22030 " ::std::is_copy_constructable<T> && " 22031 "::std::is_move_constructable<T> &&\n" 22032 " requires (T c) {\n" 22033 " { c.response; } -> Response;\n" 22034 "} && requires (T c) {\n" 22035 " { c.request; } -> Request;\n" 22036 "}\n", 22037 Style); 22038 22039 verifyFormat("template <typename T>\nconcept someConcept = Constraint1<T> && " 22040 "Constraint2<T>;"); 22041 22042 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 22043 Style.BraceWrapping.AfterFunction = true; 22044 Style.BraceWrapping.AfterClass = true; 22045 Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 22046 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 22047 verifyFormat("void Foo () requires (std::copyable<T>)\n" 22048 "{\n" 22049 " return\n" 22050 "}\n", 22051 Style); 22052 22053 verifyFormat("void Foo () requires std::copyable<T>\n" 22054 "{\n" 22055 " return\n" 22056 "}\n", 22057 Style); 22058 22059 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 22060 " requires (std::invocable<F, std::invoke_result_t<Args>...>)\n" 22061 "struct constant;", 22062 Style); 22063 22064 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 22065 " requires std::invocable<F, std::invoke_result_t<Args>...>\n" 22066 "struct constant;", 22067 Style); 22068 22069 verifyFormat("template <class T>\n" 22070 "class plane_with_very_very_very_long_name\n" 22071 "{\n" 22072 " constexpr plane_with_very_very_very_long_name () requires " 22073 "std::copyable<T>\n" 22074 " : plane_with_very_very_very_long_name (1)\n" 22075 " {\n" 22076 " }\n" 22077 "}\n", 22078 Style); 22079 22080 verifyFormat("template <class T>\n" 22081 "class plane_with_long_name\n" 22082 "{\n" 22083 " constexpr plane_with_long_name () requires std::copyable<T>\n" 22084 " : plane_with_long_name (1)\n" 22085 " {\n" 22086 " }\n" 22087 "}\n", 22088 Style); 22089 22090 Style.BreakBeforeConceptDeclarations = false; 22091 verifyFormat("template <typename T> concept Tree = true;", Style); 22092 22093 Style.IndentRequires = false; 22094 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 22095 "requires (std::invocable<F, std::invoke_result_t<Args>...>) " 22096 "struct constant;", 22097 Style); 22098 } 22099 22100 TEST_F(FormatTest, StatementAttributeLikeMacros) { 22101 FormatStyle Style = getLLVMStyle(); 22102 StringRef Source = "void Foo::slot() {\n" 22103 " unsigned char MyChar = 'x';\n" 22104 " emit signal(MyChar);\n" 22105 " Q_EMIT signal(MyChar);\n" 22106 "}"; 22107 22108 EXPECT_EQ(Source, format(Source, Style)); 22109 22110 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 22111 EXPECT_EQ("void Foo::slot() {\n" 22112 " unsigned char MyChar = 'x';\n" 22113 " emit signal(MyChar);\n" 22114 " Q_EMIT signal(MyChar);\n" 22115 "}", 22116 format(Source, Style)); 22117 22118 Style.StatementAttributeLikeMacros.push_back("emit"); 22119 EXPECT_EQ(Source, format(Source, Style)); 22120 22121 Style.StatementAttributeLikeMacros = {}; 22122 EXPECT_EQ("void Foo::slot() {\n" 22123 " unsigned char MyChar = 'x';\n" 22124 " emit signal(MyChar);\n" 22125 " Q_EMIT signal(MyChar);\n" 22126 "}", 22127 format(Source, Style)); 22128 } 22129 22130 TEST_F(FormatTest, IndentAccessModifiers) { 22131 FormatStyle Style = getLLVMStyle(); 22132 Style.IndentAccessModifiers = true; 22133 // Members are *two* levels below the record; 22134 // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation. 22135 verifyFormat("class C {\n" 22136 " int i;\n" 22137 "};\n", 22138 Style); 22139 verifyFormat("union C {\n" 22140 " int i;\n" 22141 " unsigned u;\n" 22142 "};\n", 22143 Style); 22144 // Access modifiers should be indented one level below the record. 22145 verifyFormat("class C {\n" 22146 " public:\n" 22147 " int i;\n" 22148 "};\n", 22149 Style); 22150 verifyFormat("struct S {\n" 22151 " private:\n" 22152 " class C {\n" 22153 " int j;\n" 22154 "\n" 22155 " public:\n" 22156 " C();\n" 22157 " };\n" 22158 "\n" 22159 " public:\n" 22160 " int i;\n" 22161 "};\n", 22162 Style); 22163 // Enumerations are not records and should be unaffected. 22164 Style.AllowShortEnumsOnASingleLine = false; 22165 verifyFormat("enum class E {\n" 22166 " A,\n" 22167 " B\n" 22168 "};\n", 22169 Style); 22170 // Test with a different indentation width; 22171 // also proves that the result is Style.AccessModifierOffset agnostic. 22172 Style.IndentWidth = 3; 22173 verifyFormat("class C {\n" 22174 " public:\n" 22175 " int i;\n" 22176 "};\n", 22177 Style); 22178 } 22179 22180 TEST_F(FormatTest, LimitlessStringsAndComments) { 22181 auto Style = getLLVMStyleWithColumns(0); 22182 constexpr StringRef Code = 22183 "/**\n" 22184 " * This is a multiline comment with quite some long lines, at least for " 22185 "the LLVM Style.\n" 22186 " * We will redo this with strings and line comments. Just to check if " 22187 "everything is working.\n" 22188 " */\n" 22189 "bool foo() {\n" 22190 " /* Single line multi line comment. */\n" 22191 " const std::string String = \"This is a multiline string with quite " 22192 "some long lines, at least for the LLVM Style.\"\n" 22193 " \"We already did it with multi line " 22194 "comments, and we will do it with line comments. Just to check if " 22195 "everything is working.\";\n" 22196 " // This is a line comment (block) with quite some long lines, at " 22197 "least for the LLVM Style.\n" 22198 " // We already did this with multi line comments and strings. Just to " 22199 "check if everything is working.\n" 22200 " const std::string SmallString = \"Hello World\";\n" 22201 " // Small line comment\n" 22202 " return String.size() > SmallString.size();\n" 22203 "}"; 22204 EXPECT_EQ(Code, format(Code, Style)); 22205 } 22206 } // namespace 22207 } // namespace format 22208 } // namespace clang 22209