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 "}\n" 8228 "int\n" // Break here. 8229 "f(i)\n" 8230 "{\n" 8231 " return i + 1;\n" 8232 "};", 8233 Style); 8234 verifyFormat("int f(a, b, c);\n" // No break here. 8235 "int\n" // Break here. 8236 "f(a, b, c)\n" // Break here. 8237 "short a, b;\n" 8238 "float c;\n" 8239 "{\n" 8240 " return a + b < c;\n" 8241 "}\n" 8242 "int\n" // Break here. 8243 "f(a, b, c)\n" // Break here. 8244 "short a, b;\n" 8245 "float c;\n" 8246 "{\n" 8247 " return a + b < c;\n" 8248 "};", 8249 Style); 8250 verifyFormat("byte *\n" // Break here. 8251 "f(a)\n" // Break here. 8252 "byte a[];\n" 8253 "{\n" 8254 " return a;\n" 8255 "}", 8256 Style); 8257 verifyFormat("bool f(int a, int) override;\n" 8258 "Bar g(int a, Bar) final; // comment", 8259 Style); 8260 8261 // The return breaking style doesn't affect: 8262 // * function and object definitions with attribute-like macros 8263 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n" 8264 " ABSL_GUARDED_BY(mutex) = {};", 8265 getGoogleStyleWithColumns(40)); 8266 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n" 8267 " ABSL_GUARDED_BY(mutex); // comment", 8268 getGoogleStyleWithColumns(40)); 8269 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n" 8270 " ABSL_GUARDED_BY(mutex1)\n" 8271 " ABSL_GUARDED_BY(mutex2);", 8272 getGoogleStyleWithColumns(40)); 8273 verifyFormat("Tttttt f(int a, int b)\n" 8274 " ABSL_GUARDED_BY(mutex1)\n" 8275 " ABSL_GUARDED_BY(mutex2);", 8276 getGoogleStyleWithColumns(40)); 8277 // * typedefs 8278 verifyFormat("typedef ATTR(X) char x;", getGoogleStyle()); 8279 8280 Style = getGNUStyle(); 8281 8282 // Test for comments at the end of function declarations. 8283 verifyFormat("void\n" 8284 "foo (int a, /*abc*/ int b) // def\n" 8285 "{\n" 8286 "}\n", 8287 Style); 8288 8289 verifyFormat("void\n" 8290 "foo (int a, /* abc */ int b) /* def */\n" 8291 "{\n" 8292 "}\n", 8293 Style); 8294 8295 // Definitions that should not break after return type 8296 verifyFormat("void foo (int a, int b); // def\n", Style); 8297 verifyFormat("void foo (int a, int b); /* def */\n", Style); 8298 verifyFormat("void foo (int a, int b);\n", Style); 8299 } 8300 8301 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 8302 FormatStyle NoBreak = getLLVMStyle(); 8303 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 8304 FormatStyle Break = getLLVMStyle(); 8305 Break.AlwaysBreakBeforeMultilineStrings = true; 8306 verifyFormat("aaaa = \"bbbb\"\n" 8307 " \"cccc\";", 8308 NoBreak); 8309 verifyFormat("aaaa =\n" 8310 " \"bbbb\"\n" 8311 " \"cccc\";", 8312 Break); 8313 verifyFormat("aaaa(\"bbbb\"\n" 8314 " \"cccc\");", 8315 NoBreak); 8316 verifyFormat("aaaa(\n" 8317 " \"bbbb\"\n" 8318 " \"cccc\");", 8319 Break); 8320 verifyFormat("aaaa(qqq, \"bbbb\"\n" 8321 " \"cccc\");", 8322 NoBreak); 8323 verifyFormat("aaaa(qqq,\n" 8324 " \"bbbb\"\n" 8325 " \"cccc\");", 8326 Break); 8327 verifyFormat("aaaa(qqq,\n" 8328 " L\"bbbb\"\n" 8329 " L\"cccc\");", 8330 Break); 8331 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 8332 " \"bbbb\"));", 8333 Break); 8334 verifyFormat("string s = someFunction(\n" 8335 " \"abc\"\n" 8336 " \"abc\");", 8337 Break); 8338 8339 // As we break before unary operators, breaking right after them is bad. 8340 verifyFormat("string foo = abc ? \"x\"\n" 8341 " \"blah blah blah blah blah blah\"\n" 8342 " : \"y\";", 8343 Break); 8344 8345 // Don't break if there is no column gain. 8346 verifyFormat("f(\"aaaa\"\n" 8347 " \"bbbb\");", 8348 Break); 8349 8350 // Treat literals with escaped newlines like multi-line string literals. 8351 EXPECT_EQ("x = \"a\\\n" 8352 "b\\\n" 8353 "c\";", 8354 format("x = \"a\\\n" 8355 "b\\\n" 8356 "c\";", 8357 NoBreak)); 8358 EXPECT_EQ("xxxx =\n" 8359 " \"a\\\n" 8360 "b\\\n" 8361 "c\";", 8362 format("xxxx = \"a\\\n" 8363 "b\\\n" 8364 "c\";", 8365 Break)); 8366 8367 EXPECT_EQ("NSString *const kString =\n" 8368 " @\"aaaa\"\n" 8369 " @\"bbbb\";", 8370 format("NSString *const kString = @\"aaaa\"\n" 8371 "@\"bbbb\";", 8372 Break)); 8373 8374 Break.ColumnLimit = 0; 8375 verifyFormat("const char *hello = \"hello llvm\";", Break); 8376 } 8377 8378 TEST_F(FormatTest, AlignsPipes) { 8379 verifyFormat( 8380 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8381 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8382 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8383 verifyFormat( 8384 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 8385 " << aaaaaaaaaaaaaaaaaaaa;"); 8386 verifyFormat( 8387 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8388 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8389 verifyFormat( 8390 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 8391 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8392 verifyFormat( 8393 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 8394 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 8395 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 8396 verifyFormat( 8397 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8398 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8399 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8400 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8401 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8402 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8403 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 8404 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 8405 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 8406 verifyFormat( 8407 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8408 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8409 verifyFormat( 8410 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n" 8411 " aaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8412 8413 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 8414 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 8415 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8416 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8417 " aaaaaaaaaaaaaaaaaaaaa)\n" 8418 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8419 verifyFormat("LOG_IF(aaa == //\n" 8420 " bbb)\n" 8421 " << a << b;"); 8422 8423 // But sometimes, breaking before the first "<<" is desirable. 8424 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 8425 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 8426 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 8427 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8428 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8429 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 8430 " << BEF << IsTemplate << Description << E->getType();"); 8431 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 8432 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8433 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8434 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 8435 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8436 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8437 " << aaa;"); 8438 8439 verifyFormat( 8440 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8441 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8442 8443 // Incomplete string literal. 8444 EXPECT_EQ("llvm::errs() << \"\n" 8445 " << a;", 8446 format("llvm::errs() << \"\n<<a;")); 8447 8448 verifyFormat("void f() {\n" 8449 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 8450 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 8451 "}"); 8452 8453 // Handle 'endl'. 8454 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 8455 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 8456 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 8457 8458 // Handle '\n'. 8459 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 8460 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 8461 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 8462 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 8463 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 8464 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 8465 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 8466 } 8467 8468 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) { 8469 verifyFormat("return out << \"somepacket = {\\n\"\n" 8470 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 8471 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 8472 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 8473 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 8474 " << \"}\";"); 8475 8476 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 8477 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 8478 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 8479 verifyFormat( 8480 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 8481 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 8482 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 8483 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 8484 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 8485 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 8486 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 8487 verifyFormat( 8488 "void f() {\n" 8489 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 8490 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 8491 "}"); 8492 8493 // Breaking before the first "<<" is generally not desirable. 8494 verifyFormat( 8495 "llvm::errs()\n" 8496 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8497 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8498 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8499 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 8500 getLLVMStyleWithColumns(70)); 8501 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 8502 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8503 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 8504 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8505 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 8506 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 8507 getLLVMStyleWithColumns(70)); 8508 8509 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 8510 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 8511 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;"); 8512 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 8513 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 8514 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);"); 8515 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n" 8516 " (aaaa + aaaa);", 8517 getLLVMStyleWithColumns(40)); 8518 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n" 8519 " (aaaaaaa + aaaaa));", 8520 getLLVMStyleWithColumns(40)); 8521 verifyFormat( 8522 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n" 8523 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n" 8524 " bbbbbbbbbbbbbbbbbbbbbbb);"); 8525 } 8526 8527 TEST_F(FormatTest, UnderstandsEquals) { 8528 verifyFormat( 8529 "aaaaaaaaaaaaaaaaa =\n" 8530 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8531 verifyFormat( 8532 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8533 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 8534 verifyFormat( 8535 "if (a) {\n" 8536 " f();\n" 8537 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8538 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 8539 "}"); 8540 8541 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8542 " 100000000 + 10000000) {\n}"); 8543 } 8544 8545 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 8546 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 8547 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 8548 8549 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 8550 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 8551 8552 verifyFormat( 8553 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 8554 " Parameter2);"); 8555 8556 verifyFormat( 8557 "ShortObject->shortFunction(\n" 8558 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 8559 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 8560 8561 verifyFormat("loooooooooooooongFunction(\n" 8562 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 8563 8564 verifyFormat( 8565 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 8566 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 8567 8568 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 8569 " .WillRepeatedly(Return(SomeValue));"); 8570 verifyFormat("void f() {\n" 8571 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 8572 " .Times(2)\n" 8573 " .WillRepeatedly(Return(SomeValue));\n" 8574 "}"); 8575 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 8576 " ccccccccccccccccccccccc);"); 8577 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8578 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8579 " .aaaaa(aaaaa),\n" 8580 " aaaaaaaaaaaaaaaaaaaaa);"); 8581 verifyFormat("void f() {\n" 8582 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8583 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 8584 "}"); 8585 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8586 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8587 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8588 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8589 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 8590 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8591 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8592 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8593 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 8594 "}"); 8595 8596 // Here, it is not necessary to wrap at "." or "->". 8597 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 8598 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 8599 verifyFormat( 8600 "aaaaaaaaaaa->aaaaaaaaa(\n" 8601 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8602 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 8603 8604 verifyFormat( 8605 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8606 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 8607 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 8608 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 8609 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 8610 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 8611 8612 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8613 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8614 " .a();"); 8615 8616 FormatStyle NoBinPacking = getLLVMStyle(); 8617 NoBinPacking.BinPackParameters = false; 8618 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 8619 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 8620 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 8621 " aaaaaaaaaaaaaaaaaaa,\n" 8622 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8623 NoBinPacking); 8624 8625 // If there is a subsequent call, change to hanging indentation. 8626 verifyFormat( 8627 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8628 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 8629 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8630 verifyFormat( 8631 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8632 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 8633 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8634 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8635 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8636 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8637 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8638 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 8639 } 8640 8641 TEST_F(FormatTest, WrapsTemplateDeclarations) { 8642 verifyFormat("template <typename T>\n" 8643 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 8644 verifyFormat("template <typename T>\n" 8645 "// T should be one of {A, B}.\n" 8646 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 8647 verifyFormat( 8648 "template <typename T>\n" 8649 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 8650 verifyFormat("template <typename T>\n" 8651 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 8652 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 8653 verifyFormat( 8654 "template <typename T>\n" 8655 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 8656 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 8657 verifyFormat( 8658 "template <typename T>\n" 8659 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 8660 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 8661 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8662 verifyFormat("template <typename T>\n" 8663 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8664 " int aaaaaaaaaaaaaaaaaaaaaa);"); 8665 verifyFormat( 8666 "template <typename T1, typename T2 = char, typename T3 = char,\n" 8667 " typename T4 = char>\n" 8668 "void f();"); 8669 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 8670 " template <typename> class cccccccccccccccccccccc,\n" 8671 " typename ddddddddddddd>\n" 8672 "class C {};"); 8673 verifyFormat( 8674 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 8675 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8676 8677 verifyFormat("void f() {\n" 8678 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 8679 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 8680 "}"); 8681 8682 verifyFormat("template <typename T> class C {};"); 8683 verifyFormat("template <typename T> void f();"); 8684 verifyFormat("template <typename T> void f() {}"); 8685 verifyFormat( 8686 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 8687 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8688 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 8689 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 8690 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8691 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 8692 " bbbbbbbbbbbbbbbbbbbbbbbb);", 8693 getLLVMStyleWithColumns(72)); 8694 EXPECT_EQ("static_cast<A< //\n" 8695 " B> *>(\n" 8696 "\n" 8697 ");", 8698 format("static_cast<A<//\n" 8699 " B>*>(\n" 8700 "\n" 8701 " );")); 8702 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8703 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 8704 8705 FormatStyle AlwaysBreak = getLLVMStyle(); 8706 AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 8707 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 8708 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 8709 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 8710 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8711 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 8712 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 8713 verifyFormat("template <template <typename> class Fooooooo,\n" 8714 " template <typename> class Baaaaaaar>\n" 8715 "struct C {};", 8716 AlwaysBreak); 8717 verifyFormat("template <typename T> // T can be A, B or C.\n" 8718 "struct C {};", 8719 AlwaysBreak); 8720 verifyFormat("template <enum E> class A {\n" 8721 "public:\n" 8722 " E *f();\n" 8723 "};"); 8724 8725 FormatStyle NeverBreak = getLLVMStyle(); 8726 NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No; 8727 verifyFormat("template <typename T> class C {};", NeverBreak); 8728 verifyFormat("template <typename T> void f();", NeverBreak); 8729 verifyFormat("template <typename T> void f() {}", NeverBreak); 8730 verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa " 8731 "bbbbbbbbbbbbbbbbbbbb) {}", 8732 NeverBreak); 8733 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8734 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 8735 " ccccccccccccccccccccccccccccccccccccccccccccccc);", 8736 NeverBreak); 8737 verifyFormat("template <template <typename> class Fooooooo,\n" 8738 " template <typename> class Baaaaaaar>\n" 8739 "struct C {};", 8740 NeverBreak); 8741 verifyFormat("template <typename T> // T can be A, B or C.\n" 8742 "struct C {};", 8743 NeverBreak); 8744 verifyFormat("template <enum E> class A {\n" 8745 "public:\n" 8746 " E *f();\n" 8747 "};", 8748 NeverBreak); 8749 NeverBreak.PenaltyBreakTemplateDeclaration = 100; 8750 verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa " 8751 "bbbbbbbbbbbbbbbbbbbb) {}", 8752 NeverBreak); 8753 } 8754 8755 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) { 8756 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 8757 Style.ColumnLimit = 60; 8758 EXPECT_EQ("// Baseline - no comments.\n" 8759 "template <\n" 8760 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 8761 "void f() {}", 8762 format("// Baseline - no comments.\n" 8763 "template <\n" 8764 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 8765 "void f() {}", 8766 Style)); 8767 8768 EXPECT_EQ("template <\n" 8769 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 8770 "void f() {}", 8771 format("template <\n" 8772 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 8773 "void f() {}", 8774 Style)); 8775 8776 EXPECT_EQ( 8777 "template <\n" 8778 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 8779 "void f() {}", 8780 format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 8781 "void f() {}", 8782 Style)); 8783 8784 EXPECT_EQ( 8785 "template <\n" 8786 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 8787 " // multiline\n" 8788 "void f() {}", 8789 format("template <\n" 8790 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 8791 " // multiline\n" 8792 "void f() {}", 8793 Style)); 8794 8795 EXPECT_EQ( 8796 "template <typename aaaaaaaaaa<\n" 8797 " bbbbbbbbbbbb>::value> // trailing loooong\n" 8798 "void f() {}", 8799 format( 8800 "template <\n" 8801 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n" 8802 "void f() {}", 8803 Style)); 8804 } 8805 8806 TEST_F(FormatTest, WrapsTemplateParameters) { 8807 FormatStyle Style = getLLVMStyle(); 8808 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 8809 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 8810 verifyFormat( 8811 "template <typename... a> struct q {};\n" 8812 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 8813 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 8814 " y;", 8815 Style); 8816 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 8817 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 8818 verifyFormat( 8819 "template <typename... a> struct r {};\n" 8820 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 8821 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 8822 " y;", 8823 Style); 8824 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 8825 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 8826 verifyFormat("template <typename... a> struct s {};\n" 8827 "extern s<\n" 8828 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 8829 "aaaaaaaaaaaaaaaaaaaaaa,\n" 8830 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 8831 "aaaaaaaaaaaaaaaaaaaaaa>\n" 8832 " y;", 8833 Style); 8834 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 8835 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 8836 verifyFormat("template <typename... a> struct t {};\n" 8837 "extern t<\n" 8838 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 8839 "aaaaaaaaaaaaaaaaaaaaaa,\n" 8840 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 8841 "aaaaaaaaaaaaaaaaaaaaaa>\n" 8842 " y;", 8843 Style); 8844 } 8845 8846 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 8847 verifyFormat( 8848 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8849 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8850 verifyFormat( 8851 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8852 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8853 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 8854 8855 // FIXME: Should we have the extra indent after the second break? 8856 verifyFormat( 8857 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8858 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8859 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8860 8861 verifyFormat( 8862 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 8863 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 8864 8865 // Breaking at nested name specifiers is generally not desirable. 8866 verifyFormat( 8867 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8868 " aaaaaaaaaaaaaaaaaaaaaaa);"); 8869 8870 verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n" 8871 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8872 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8873 " aaaaaaaaaaaaaaaaaaaaa);", 8874 getLLVMStyleWithColumns(74)); 8875 8876 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8877 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8878 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8879 } 8880 8881 TEST_F(FormatTest, UnderstandsTemplateParameters) { 8882 verifyFormat("A<int> a;"); 8883 verifyFormat("A<A<A<int>>> a;"); 8884 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 8885 verifyFormat("bool x = a < 1 || 2 > a;"); 8886 verifyFormat("bool x = 5 < f<int>();"); 8887 verifyFormat("bool x = f<int>() > 5;"); 8888 verifyFormat("bool x = 5 < a<int>::x;"); 8889 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 8890 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 8891 8892 verifyGoogleFormat("A<A<int>> a;"); 8893 verifyGoogleFormat("A<A<A<int>>> a;"); 8894 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 8895 verifyGoogleFormat("A<A<int> > a;"); 8896 verifyGoogleFormat("A<A<A<int> > > a;"); 8897 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 8898 verifyGoogleFormat("A<::A<int>> a;"); 8899 verifyGoogleFormat("A<::A> a;"); 8900 verifyGoogleFormat("A< ::A> a;"); 8901 verifyGoogleFormat("A< ::A<int> > a;"); 8902 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 8903 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 8904 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 8905 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 8906 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 8907 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 8908 8909 verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 8910 8911 // template closer followed by a token that starts with > or = 8912 verifyFormat("bool b = a<1> > 1;"); 8913 verifyFormat("bool b = a<1> >= 1;"); 8914 verifyFormat("int i = a<1> >> 1;"); 8915 FormatStyle Style = getLLVMStyle(); 8916 Style.SpaceBeforeAssignmentOperators = false; 8917 verifyFormat("bool b= a<1> == 1;", Style); 8918 verifyFormat("a<int> = 1;", Style); 8919 verifyFormat("a<int> >>= 1;", Style); 8920 8921 verifyFormat("test < a | b >> c;"); 8922 verifyFormat("test<test<a | b>> c;"); 8923 verifyFormat("test >> a >> b;"); 8924 verifyFormat("test << a >> b;"); 8925 8926 verifyFormat("f<int>();"); 8927 verifyFormat("template <typename T> void f() {}"); 8928 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 8929 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 8930 "sizeof(char)>::type>;"); 8931 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 8932 verifyFormat("f(a.operator()<A>());"); 8933 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8934 " .template operator()<A>());", 8935 getLLVMStyleWithColumns(35)); 8936 8937 // Not template parameters. 8938 verifyFormat("return a < b && c > d;"); 8939 verifyFormat("void f() {\n" 8940 " while (a < b && c > d) {\n" 8941 " }\n" 8942 "}"); 8943 verifyFormat("template <typename... Types>\n" 8944 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 8945 8946 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8947 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 8948 getLLVMStyleWithColumns(60)); 8949 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 8950 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 8951 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 8952 verifyFormat("some_templated_type<decltype([](int i) { return i; })>"); 8953 } 8954 8955 TEST_F(FormatTest, UnderstandsShiftOperators) { 8956 verifyFormat("if (i < x >> 1)"); 8957 verifyFormat("while (i < x >> 1)"); 8958 verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)"); 8959 verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)"); 8960 verifyFormat( 8961 "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)"); 8962 verifyFormat("Foo.call<Bar<Function>>()"); 8963 verifyFormat("if (Foo.call<Bar<Function>>() == 0)"); 8964 verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; " 8965 "++i, v = v >> 1)"); 8966 verifyFormat("if (w<u<v<x>>, 1>::t)"); 8967 } 8968 8969 TEST_F(FormatTest, BitshiftOperatorWidth) { 8970 EXPECT_EQ("int a = 1 << 2; /* foo\n" 8971 " bar */", 8972 format("int a=1<<2; /* foo\n" 8973 " bar */")); 8974 8975 EXPECT_EQ("int b = 256 >> 1; /* foo\n" 8976 " bar */", 8977 format("int b =256>>1 ; /* foo\n" 8978 " bar */")); 8979 } 8980 8981 TEST_F(FormatTest, UnderstandsBinaryOperators) { 8982 verifyFormat("COMPARE(a, ==, b);"); 8983 verifyFormat("auto s = sizeof...(Ts) - 1;"); 8984 } 8985 8986 TEST_F(FormatTest, UnderstandsPointersToMembers) { 8987 verifyFormat("int A::*x;"); 8988 verifyFormat("int (S::*func)(void *);"); 8989 verifyFormat("void f() { int (S::*func)(void *); }"); 8990 verifyFormat("typedef bool *(Class::*Member)() const;"); 8991 verifyFormat("void f() {\n" 8992 " (a->*f)();\n" 8993 " a->*x;\n" 8994 " (a.*f)();\n" 8995 " ((*a).*f)();\n" 8996 " a.*x;\n" 8997 "}"); 8998 verifyFormat("void f() {\n" 8999 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 9000 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 9001 "}"); 9002 verifyFormat( 9003 "(aaaaaaaaaa->*bbbbbbb)(\n" 9004 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 9005 FormatStyle Style = getLLVMStyle(); 9006 Style.PointerAlignment = FormatStyle::PAS_Left; 9007 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 9008 } 9009 9010 TEST_F(FormatTest, UnderstandsUnaryOperators) { 9011 verifyFormat("int a = -2;"); 9012 verifyFormat("f(-1, -2, -3);"); 9013 verifyFormat("a[-1] = 5;"); 9014 verifyFormat("int a = 5 + -2;"); 9015 verifyFormat("if (i == -1) {\n}"); 9016 verifyFormat("if (i != -1) {\n}"); 9017 verifyFormat("if (i > -1) {\n}"); 9018 verifyFormat("if (i < -1) {\n}"); 9019 verifyFormat("++(a->f());"); 9020 verifyFormat("--(a->f());"); 9021 verifyFormat("(a->f())++;"); 9022 verifyFormat("a[42]++;"); 9023 verifyFormat("if (!(a->f())) {\n}"); 9024 verifyFormat("if (!+i) {\n}"); 9025 verifyFormat("~&a;"); 9026 9027 verifyFormat("a-- > b;"); 9028 verifyFormat("b ? -a : c;"); 9029 verifyFormat("n * sizeof char16;"); 9030 verifyFormat("n * alignof char16;", getGoogleStyle()); 9031 verifyFormat("sizeof(char);"); 9032 verifyFormat("alignof(char);", getGoogleStyle()); 9033 9034 verifyFormat("return -1;"); 9035 verifyFormat("throw -1;"); 9036 verifyFormat("switch (a) {\n" 9037 "case -1:\n" 9038 " break;\n" 9039 "}"); 9040 verifyFormat("#define X -1"); 9041 verifyFormat("#define X -kConstant"); 9042 9043 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 9044 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 9045 9046 verifyFormat("int a = /* confusing comment */ -1;"); 9047 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 9048 verifyFormat("int a = i /* confusing comment */++;"); 9049 9050 verifyFormat("co_yield -1;"); 9051 verifyFormat("co_return -1;"); 9052 9053 // Check that * is not treated as a binary operator when we set 9054 // PointerAlignment as PAS_Left after a keyword and not a declaration. 9055 FormatStyle PASLeftStyle = getLLVMStyle(); 9056 PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left; 9057 verifyFormat("co_return *a;", PASLeftStyle); 9058 verifyFormat("co_await *a;", PASLeftStyle); 9059 verifyFormat("co_yield *a", PASLeftStyle); 9060 verifyFormat("return *a;", PASLeftStyle); 9061 } 9062 9063 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 9064 verifyFormat("if (!aaaaaaaaaa( // break\n" 9065 " aaaaa)) {\n" 9066 "}"); 9067 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 9068 " aaaaa));"); 9069 verifyFormat("*aaa = aaaaaaa( // break\n" 9070 " bbbbbb);"); 9071 } 9072 9073 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 9074 verifyFormat("bool operator<();"); 9075 verifyFormat("bool operator>();"); 9076 verifyFormat("bool operator=();"); 9077 verifyFormat("bool operator==();"); 9078 verifyFormat("bool operator!=();"); 9079 verifyFormat("int operator+();"); 9080 verifyFormat("int operator++();"); 9081 verifyFormat("int operator++(int) volatile noexcept;"); 9082 verifyFormat("bool operator,();"); 9083 verifyFormat("bool operator();"); 9084 verifyFormat("bool operator()();"); 9085 verifyFormat("bool operator[]();"); 9086 verifyFormat("operator bool();"); 9087 verifyFormat("operator int();"); 9088 verifyFormat("operator void *();"); 9089 verifyFormat("operator SomeType<int>();"); 9090 verifyFormat("operator SomeType<int, int>();"); 9091 verifyFormat("operator SomeType<SomeType<int>>();"); 9092 verifyFormat("void *operator new(std::size_t size);"); 9093 verifyFormat("void *operator new[](std::size_t size);"); 9094 verifyFormat("void operator delete(void *ptr);"); 9095 verifyFormat("void operator delete[](void *ptr);"); 9096 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 9097 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 9098 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 9099 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 9100 9101 verifyFormat( 9102 "ostream &operator<<(ostream &OutputStream,\n" 9103 " SomeReallyLongType WithSomeReallyLongValue);"); 9104 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 9105 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 9106 " return left.group < right.group;\n" 9107 "}"); 9108 verifyFormat("SomeType &operator=(const SomeType &S);"); 9109 verifyFormat("f.template operator()<int>();"); 9110 9111 verifyGoogleFormat("operator void*();"); 9112 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 9113 verifyGoogleFormat("operator ::A();"); 9114 9115 verifyFormat("using A::operator+;"); 9116 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 9117 "int i;"); 9118 9119 // Calling an operator as a member function. 9120 verifyFormat("void f() { a.operator*(); }"); 9121 verifyFormat("void f() { a.operator*(b & b); }"); 9122 verifyFormat("void f() { a->operator&(a * b); }"); 9123 verifyFormat("void f() { NS::a.operator+(*b * *b); }"); 9124 // TODO: Calling an operator as a non-member function is hard to distinguish. 9125 // https://llvm.org/PR50629 9126 // verifyFormat("void f() { operator*(a & a); }"); 9127 // verifyFormat("void f() { operator&(a, b * b); }"); 9128 } 9129 9130 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 9131 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 9132 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 9133 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 9134 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 9135 verifyFormat("Deleted &operator=(const Deleted &) &;"); 9136 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 9137 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 9138 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 9139 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 9140 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 9141 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 9142 verifyFormat("void Fn(T const &) const &;"); 9143 verifyFormat("void Fn(T const volatile &&) const volatile &&;"); 9144 verifyFormat("template <typename T>\n" 9145 "void F(T) && = delete;", 9146 getGoogleStyle()); 9147 9148 FormatStyle AlignLeft = getLLVMStyle(); 9149 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 9150 verifyFormat("void A::b() && {}", AlignLeft); 9151 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 9152 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 9153 AlignLeft); 9154 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 9155 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 9156 verifyFormat("auto Function(T t) & -> void {}", AlignLeft); 9157 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft); 9158 verifyFormat("auto Function(T) & -> void {}", AlignLeft); 9159 verifyFormat("auto Function(T) & -> void;", AlignLeft); 9160 verifyFormat("void Fn(T const&) const&;", AlignLeft); 9161 verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft); 9162 9163 FormatStyle Spaces = getLLVMStyle(); 9164 Spaces.SpacesInCStyleCastParentheses = true; 9165 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 9166 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 9167 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 9168 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 9169 9170 Spaces.SpacesInCStyleCastParentheses = false; 9171 Spaces.SpacesInParentheses = true; 9172 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 9173 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", 9174 Spaces); 9175 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 9176 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 9177 9178 FormatStyle BreakTemplate = getLLVMStyle(); 9179 BreakTemplate.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 9180 9181 verifyFormat("struct f {\n" 9182 " template <class T>\n" 9183 " int &foo(const std::string &str) &noexcept {}\n" 9184 "};", 9185 BreakTemplate); 9186 9187 verifyFormat("struct f {\n" 9188 " template <class T>\n" 9189 " int &foo(const std::string &str) &&noexcept {}\n" 9190 "};", 9191 BreakTemplate); 9192 9193 verifyFormat("struct f {\n" 9194 " template <class T>\n" 9195 " int &foo(const std::string &str) const &noexcept {}\n" 9196 "};", 9197 BreakTemplate); 9198 9199 verifyFormat("struct f {\n" 9200 " template <class T>\n" 9201 " int &foo(const std::string &str) const &noexcept {}\n" 9202 "};", 9203 BreakTemplate); 9204 9205 verifyFormat("struct f {\n" 9206 " template <class T>\n" 9207 " auto foo(const std::string &str) &&noexcept -> int & {}\n" 9208 "};", 9209 BreakTemplate); 9210 9211 FormatStyle AlignLeftBreakTemplate = getLLVMStyle(); 9212 AlignLeftBreakTemplate.AlwaysBreakTemplateDeclarations = 9213 FormatStyle::BTDS_Yes; 9214 AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left; 9215 9216 verifyFormat("struct f {\n" 9217 " template <class T>\n" 9218 " int& foo(const std::string& str) & noexcept {}\n" 9219 "};", 9220 AlignLeftBreakTemplate); 9221 9222 verifyFormat("struct f {\n" 9223 " template <class T>\n" 9224 " int& foo(const std::string& str) && noexcept {}\n" 9225 "};", 9226 AlignLeftBreakTemplate); 9227 9228 verifyFormat("struct f {\n" 9229 " template <class T>\n" 9230 " int& foo(const std::string& str) const& noexcept {}\n" 9231 "};", 9232 AlignLeftBreakTemplate); 9233 9234 verifyFormat("struct f {\n" 9235 " template <class T>\n" 9236 " int& foo(const std::string& str) const&& noexcept {}\n" 9237 "};", 9238 AlignLeftBreakTemplate); 9239 9240 verifyFormat("struct f {\n" 9241 " template <class T>\n" 9242 " auto foo(const std::string& str) && noexcept -> int& {}\n" 9243 "};", 9244 AlignLeftBreakTemplate); 9245 9246 // The `&` in `Type&` should not be confused with a trailing `&` of 9247 // DEPRECATED(reason) member function. 9248 verifyFormat("struct f {\n" 9249 " template <class T>\n" 9250 " DEPRECATED(reason)\n" 9251 " Type &foo(arguments) {}\n" 9252 "};", 9253 BreakTemplate); 9254 9255 verifyFormat("struct f {\n" 9256 " template <class T>\n" 9257 " DEPRECATED(reason)\n" 9258 " Type& foo(arguments) {}\n" 9259 "};", 9260 AlignLeftBreakTemplate); 9261 9262 verifyFormat("void (*foopt)(int) = &func;"); 9263 } 9264 9265 TEST_F(FormatTest, UnderstandsNewAndDelete) { 9266 verifyFormat("void f() {\n" 9267 " A *a = new A;\n" 9268 " A *a = new (placement) A;\n" 9269 " delete a;\n" 9270 " delete (A *)a;\n" 9271 "}"); 9272 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 9273 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 9274 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 9275 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 9276 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 9277 verifyFormat("delete[] h->p;"); 9278 } 9279 9280 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 9281 verifyFormat("int *f(int *a) {}"); 9282 verifyFormat("int main(int argc, char **argv) {}"); 9283 verifyFormat("Test::Test(int b) : a(b * b) {}"); 9284 verifyIndependentOfContext("f(a, *a);"); 9285 verifyFormat("void g() { f(*a); }"); 9286 verifyIndependentOfContext("int a = b * 10;"); 9287 verifyIndependentOfContext("int a = 10 * b;"); 9288 verifyIndependentOfContext("int a = b * c;"); 9289 verifyIndependentOfContext("int a += b * c;"); 9290 verifyIndependentOfContext("int a -= b * c;"); 9291 verifyIndependentOfContext("int a *= b * c;"); 9292 verifyIndependentOfContext("int a /= b * c;"); 9293 verifyIndependentOfContext("int a = *b;"); 9294 verifyIndependentOfContext("int a = *b * c;"); 9295 verifyIndependentOfContext("int a = b * *c;"); 9296 verifyIndependentOfContext("int a = b * (10);"); 9297 verifyIndependentOfContext("S << b * (10);"); 9298 verifyIndependentOfContext("return 10 * b;"); 9299 verifyIndependentOfContext("return *b * *c;"); 9300 verifyIndependentOfContext("return a & ~b;"); 9301 verifyIndependentOfContext("f(b ? *c : *d);"); 9302 verifyIndependentOfContext("int a = b ? *c : *d;"); 9303 verifyIndependentOfContext("*b = a;"); 9304 verifyIndependentOfContext("a * ~b;"); 9305 verifyIndependentOfContext("a * !b;"); 9306 verifyIndependentOfContext("a * +b;"); 9307 verifyIndependentOfContext("a * -b;"); 9308 verifyIndependentOfContext("a * ++b;"); 9309 verifyIndependentOfContext("a * --b;"); 9310 verifyIndependentOfContext("a[4] * b;"); 9311 verifyIndependentOfContext("a[a * a] = 1;"); 9312 verifyIndependentOfContext("f() * b;"); 9313 verifyIndependentOfContext("a * [self dostuff];"); 9314 verifyIndependentOfContext("int x = a * (a + b);"); 9315 verifyIndependentOfContext("(a *)(a + b);"); 9316 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 9317 verifyIndependentOfContext("int *pa = (int *)&a;"); 9318 verifyIndependentOfContext("return sizeof(int **);"); 9319 verifyIndependentOfContext("return sizeof(int ******);"); 9320 verifyIndependentOfContext("return (int **&)a;"); 9321 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 9322 verifyFormat("void f(Type (*parameter)[10]) {}"); 9323 verifyFormat("void f(Type (¶meter)[10]) {}"); 9324 verifyGoogleFormat("return sizeof(int**);"); 9325 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 9326 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 9327 verifyFormat("auto a = [](int **&, int ***) {};"); 9328 verifyFormat("auto PointerBinding = [](const char *S) {};"); 9329 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 9330 verifyFormat("[](const decltype(*a) &value) {}"); 9331 verifyFormat("[](const typeof(*a) &value) {}"); 9332 verifyFormat("[](const _Atomic(a *) &value) {}"); 9333 verifyFormat("[](const __underlying_type(a) &value) {}"); 9334 verifyFormat("decltype(a * b) F();"); 9335 verifyFormat("typeof(a * b) F();"); 9336 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 9337 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 9338 verifyIndependentOfContext("typedef void (*f)(int *a);"); 9339 verifyIndependentOfContext("int i{a * b};"); 9340 verifyIndependentOfContext("aaa && aaa->f();"); 9341 verifyIndependentOfContext("int x = ~*p;"); 9342 verifyFormat("Constructor() : a(a), area(width * height) {}"); 9343 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 9344 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 9345 verifyFormat("void f() { f(a, c * d); }"); 9346 verifyFormat("void f() { f(new a(), c * d); }"); 9347 verifyFormat("void f(const MyOverride &override);"); 9348 verifyFormat("void f(const MyFinal &final);"); 9349 verifyIndependentOfContext("bool a = f() && override.f();"); 9350 verifyIndependentOfContext("bool a = f() && final.f();"); 9351 9352 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 9353 9354 verifyIndependentOfContext("A<int *> a;"); 9355 verifyIndependentOfContext("A<int **> a;"); 9356 verifyIndependentOfContext("A<int *, int *> a;"); 9357 verifyIndependentOfContext("A<int *[]> a;"); 9358 verifyIndependentOfContext( 9359 "const char *const p = reinterpret_cast<const char *const>(q);"); 9360 verifyIndependentOfContext("A<int **, int **> a;"); 9361 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 9362 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 9363 verifyFormat("for (; a && b;) {\n}"); 9364 verifyFormat("bool foo = true && [] { return false; }();"); 9365 9366 verifyFormat( 9367 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9368 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9369 9370 verifyGoogleFormat("int const* a = &b;"); 9371 verifyGoogleFormat("**outparam = 1;"); 9372 verifyGoogleFormat("*outparam = a * b;"); 9373 verifyGoogleFormat("int main(int argc, char** argv) {}"); 9374 verifyGoogleFormat("A<int*> a;"); 9375 verifyGoogleFormat("A<int**> a;"); 9376 verifyGoogleFormat("A<int*, int*> a;"); 9377 verifyGoogleFormat("A<int**, int**> a;"); 9378 verifyGoogleFormat("f(b ? *c : *d);"); 9379 verifyGoogleFormat("int a = b ? *c : *d;"); 9380 verifyGoogleFormat("Type* t = **x;"); 9381 verifyGoogleFormat("Type* t = *++*x;"); 9382 verifyGoogleFormat("*++*x;"); 9383 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 9384 verifyGoogleFormat("Type* t = x++ * y;"); 9385 verifyGoogleFormat( 9386 "const char* const p = reinterpret_cast<const char* const>(q);"); 9387 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 9388 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 9389 verifyGoogleFormat("template <typename T>\n" 9390 "void f(int i = 0, SomeType** temps = NULL);"); 9391 9392 FormatStyle Left = getLLVMStyle(); 9393 Left.PointerAlignment = FormatStyle::PAS_Left; 9394 verifyFormat("x = *a(x) = *a(y);", Left); 9395 verifyFormat("for (;; *a = b) {\n}", Left); 9396 verifyFormat("return *this += 1;", Left); 9397 verifyFormat("throw *x;", Left); 9398 verifyFormat("delete *x;", Left); 9399 verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left); 9400 verifyFormat("[](const decltype(*a)* ptr) {}", Left); 9401 verifyFormat("[](const typeof(*a)* ptr) {}", Left); 9402 verifyFormat("[](const _Atomic(a*)* ptr) {}", Left); 9403 verifyFormat("[](const __underlying_type(a)* ptr) {}", Left); 9404 verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left); 9405 verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left); 9406 verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left); 9407 verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left); 9408 9409 verifyIndependentOfContext("a = *(x + y);"); 9410 verifyIndependentOfContext("a = &(x + y);"); 9411 verifyIndependentOfContext("*(x + y).call();"); 9412 verifyIndependentOfContext("&(x + y)->call();"); 9413 verifyFormat("void f() { &(*I).first; }"); 9414 9415 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 9416 verifyFormat( 9417 "int *MyValues = {\n" 9418 " *A, // Operator detection might be confused by the '{'\n" 9419 " *BB // Operator detection might be confused by previous comment\n" 9420 "};"); 9421 9422 verifyIndependentOfContext("if (int *a = &b)"); 9423 verifyIndependentOfContext("if (int &a = *b)"); 9424 verifyIndependentOfContext("if (a & b[i])"); 9425 verifyIndependentOfContext("if constexpr (a & b[i])"); 9426 verifyIndependentOfContext("if CONSTEXPR (a & b[i])"); 9427 verifyIndependentOfContext("if (a * (b * c))"); 9428 verifyIndependentOfContext("if constexpr (a * (b * c))"); 9429 verifyIndependentOfContext("if CONSTEXPR (a * (b * c))"); 9430 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 9431 verifyIndependentOfContext("if (*b[i])"); 9432 verifyIndependentOfContext("if (int *a = (&b))"); 9433 verifyIndependentOfContext("while (int *a = &b)"); 9434 verifyIndependentOfContext("while (a * (b * c))"); 9435 verifyIndependentOfContext("size = sizeof *a;"); 9436 verifyIndependentOfContext("if (a && (b = c))"); 9437 verifyFormat("void f() {\n" 9438 " for (const int &v : Values) {\n" 9439 " }\n" 9440 "}"); 9441 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 9442 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 9443 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 9444 9445 verifyFormat("#define A (!a * b)"); 9446 verifyFormat("#define MACRO \\\n" 9447 " int *i = a * b; \\\n" 9448 " void f(a *b);", 9449 getLLVMStyleWithColumns(19)); 9450 9451 verifyIndependentOfContext("A = new SomeType *[Length];"); 9452 verifyIndependentOfContext("A = new SomeType *[Length]();"); 9453 verifyIndependentOfContext("T **t = new T *;"); 9454 verifyIndependentOfContext("T **t = new T *();"); 9455 verifyGoogleFormat("A = new SomeType*[Length]();"); 9456 verifyGoogleFormat("A = new SomeType*[Length];"); 9457 verifyGoogleFormat("T** t = new T*;"); 9458 verifyGoogleFormat("T** t = new T*();"); 9459 9460 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 9461 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 9462 verifyFormat("template <bool a, bool b> " 9463 "typename t::if<x && y>::type f() {}"); 9464 verifyFormat("template <int *y> f() {}"); 9465 verifyFormat("vector<int *> v;"); 9466 verifyFormat("vector<int *const> v;"); 9467 verifyFormat("vector<int *const **const *> v;"); 9468 verifyFormat("vector<int *volatile> v;"); 9469 verifyFormat("vector<a *_Nonnull> v;"); 9470 verifyFormat("vector<a *_Nullable> v;"); 9471 verifyFormat("vector<a *_Null_unspecified> v;"); 9472 verifyFormat("vector<a *__ptr32> v;"); 9473 verifyFormat("vector<a *__ptr64> v;"); 9474 verifyFormat("vector<a *__capability> v;"); 9475 FormatStyle TypeMacros = getLLVMStyle(); 9476 TypeMacros.TypenameMacros = {"LIST"}; 9477 verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros); 9478 verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros); 9479 verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros); 9480 verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros); 9481 verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros); // multiplication 9482 9483 FormatStyle CustomQualifier = getLLVMStyle(); 9484 // Add identifiers that should not be parsed as a qualifier by default. 9485 CustomQualifier.AttributeMacros.push_back("__my_qualifier"); 9486 CustomQualifier.AttributeMacros.push_back("_My_qualifier"); 9487 CustomQualifier.AttributeMacros.push_back("my_other_qualifier"); 9488 verifyFormat("vector<a * __my_qualifier> parse_as_multiply;"); 9489 verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier); 9490 verifyFormat("vector<a * _My_qualifier> parse_as_multiply;"); 9491 verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier); 9492 verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;"); 9493 verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier); 9494 verifyFormat("vector<a * _NotAQualifier> v;"); 9495 verifyFormat("vector<a * __not_a_qualifier> v;"); 9496 verifyFormat("vector<a * b> v;"); 9497 verifyFormat("foo<b && false>();"); 9498 verifyFormat("foo<b & 1>();"); 9499 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 9500 verifyFormat("typeof(*::std::declval<const T &>()) void F();"); 9501 verifyFormat("_Atomic(*::std::declval<const T &>()) void F();"); 9502 verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();"); 9503 verifyFormat( 9504 "template <class T, class = typename std::enable_if<\n" 9505 " std::is_integral<T>::value &&\n" 9506 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 9507 "void F();", 9508 getLLVMStyleWithColumns(70)); 9509 verifyFormat("template <class T,\n" 9510 " class = typename std::enable_if<\n" 9511 " std::is_integral<T>::value &&\n" 9512 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n" 9513 " class U>\n" 9514 "void F();", 9515 getLLVMStyleWithColumns(70)); 9516 verifyFormat( 9517 "template <class T,\n" 9518 " class = typename ::std::enable_if<\n" 9519 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 9520 "void F();", 9521 getGoogleStyleWithColumns(68)); 9522 9523 verifyIndependentOfContext("MACRO(int *i);"); 9524 verifyIndependentOfContext("MACRO(auto *a);"); 9525 verifyIndependentOfContext("MACRO(const A *a);"); 9526 verifyIndependentOfContext("MACRO(_Atomic(A) *a);"); 9527 verifyIndependentOfContext("MACRO(decltype(A) *a);"); 9528 verifyIndependentOfContext("MACRO(typeof(A) *a);"); 9529 verifyIndependentOfContext("MACRO(__underlying_type(A) *a);"); 9530 verifyIndependentOfContext("MACRO(A *const a);"); 9531 verifyIndependentOfContext("MACRO(A *restrict a);"); 9532 verifyIndependentOfContext("MACRO(A *__restrict__ a);"); 9533 verifyIndependentOfContext("MACRO(A *__restrict a);"); 9534 verifyIndependentOfContext("MACRO(A *volatile a);"); 9535 verifyIndependentOfContext("MACRO(A *__volatile a);"); 9536 verifyIndependentOfContext("MACRO(A *__volatile__ a);"); 9537 verifyIndependentOfContext("MACRO(A *_Nonnull a);"); 9538 verifyIndependentOfContext("MACRO(A *_Nullable a);"); 9539 verifyIndependentOfContext("MACRO(A *_Null_unspecified a);"); 9540 verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);"); 9541 verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);"); 9542 verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);"); 9543 verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);"); 9544 verifyIndependentOfContext("MACRO(A *__ptr32 a);"); 9545 verifyIndependentOfContext("MACRO(A *__ptr64 a);"); 9546 verifyIndependentOfContext("MACRO(A *__capability);"); 9547 verifyIndependentOfContext("MACRO(A &__capability);"); 9548 verifyFormat("MACRO(A *__my_qualifier);"); // type declaration 9549 verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication 9550 // If we add __my_qualifier to AttributeMacros it should always be parsed as 9551 // a type declaration: 9552 verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier); 9553 verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier); 9554 // Also check that TypenameMacros prevents parsing it as multiplication: 9555 verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication 9556 verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros); // type 9557 9558 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 9559 verifyFormat("void f() { f(float{1}, a * a); }"); 9560 verifyFormat("void f() { f(float(1), a * a); }"); 9561 9562 verifyFormat("f((void (*)(int))g);"); 9563 verifyFormat("f((void (&)(int))g);"); 9564 verifyFormat("f((void (^)(int))g);"); 9565 9566 // FIXME: Is there a way to make this work? 9567 // verifyIndependentOfContext("MACRO(A *a);"); 9568 verifyFormat("MACRO(A &B);"); 9569 verifyFormat("MACRO(A *B);"); 9570 verifyFormat("void f() { MACRO(A * B); }"); 9571 verifyFormat("void f() { MACRO(A & B); }"); 9572 9573 // This lambda was mis-formatted after D88956 (treating it as a binop): 9574 verifyFormat("auto x = [](const decltype(x) &ptr) {};"); 9575 verifyFormat("auto x = [](const decltype(x) *ptr) {};"); 9576 verifyFormat("#define lambda [](const decltype(x) &ptr) {}"); 9577 verifyFormat("#define lambda [](const decltype(x) *ptr) {}"); 9578 9579 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 9580 verifyFormat("return options != nullptr && operator==(*options);"); 9581 9582 EXPECT_EQ("#define OP(x) \\\n" 9583 " ostream &operator<<(ostream &s, const A &a) { \\\n" 9584 " return s << a.DebugString(); \\\n" 9585 " }", 9586 format("#define OP(x) \\\n" 9587 " ostream &operator<<(ostream &s, const A &a) { \\\n" 9588 " return s << a.DebugString(); \\\n" 9589 " }", 9590 getLLVMStyleWithColumns(50))); 9591 9592 // FIXME: We cannot handle this case yet; we might be able to figure out that 9593 // foo<x> d > v; doesn't make sense. 9594 verifyFormat("foo<a<b && c> d> v;"); 9595 9596 FormatStyle PointerMiddle = getLLVMStyle(); 9597 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 9598 verifyFormat("delete *x;", PointerMiddle); 9599 verifyFormat("int * x;", PointerMiddle); 9600 verifyFormat("int *[] x;", PointerMiddle); 9601 verifyFormat("template <int * y> f() {}", PointerMiddle); 9602 verifyFormat("int * f(int * a) {}", PointerMiddle); 9603 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 9604 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 9605 verifyFormat("A<int *> a;", PointerMiddle); 9606 verifyFormat("A<int **> a;", PointerMiddle); 9607 verifyFormat("A<int *, int *> a;", PointerMiddle); 9608 verifyFormat("A<int *[]> a;", PointerMiddle); 9609 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 9610 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 9611 verifyFormat("T ** t = new T *;", PointerMiddle); 9612 9613 // Member function reference qualifiers aren't binary operators. 9614 verifyFormat("string // break\n" 9615 "operator()() & {}"); 9616 verifyFormat("string // break\n" 9617 "operator()() && {}"); 9618 verifyGoogleFormat("template <typename T>\n" 9619 "auto x() & -> int {}"); 9620 9621 // Should be binary operators when used as an argument expression (overloaded 9622 // operator invoked as a member function). 9623 verifyFormat("void f() { a.operator()(a * a); }"); 9624 verifyFormat("void f() { a->operator()(a & a); }"); 9625 verifyFormat("void f() { a.operator()(*a & *a); }"); 9626 verifyFormat("void f() { a->operator()(*a * *a); }"); 9627 } 9628 9629 TEST_F(FormatTest, UnderstandsAttributes) { 9630 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 9631 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 9632 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 9633 FormatStyle AfterType = getLLVMStyle(); 9634 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 9635 verifyFormat("__attribute__((nodebug)) void\n" 9636 "foo() {}\n", 9637 AfterType); 9638 verifyFormat("__unused void\n" 9639 "foo() {}", 9640 AfterType); 9641 9642 FormatStyle CustomAttrs = getLLVMStyle(); 9643 CustomAttrs.AttributeMacros.push_back("__unused"); 9644 CustomAttrs.AttributeMacros.push_back("__attr1"); 9645 CustomAttrs.AttributeMacros.push_back("__attr2"); 9646 CustomAttrs.AttributeMacros.push_back("no_underscore_attr"); 9647 verifyFormat("vector<SomeType *__attribute((foo))> v;"); 9648 verifyFormat("vector<SomeType *__attribute__((foo))> v;"); 9649 verifyFormat("vector<SomeType * __not_attribute__((foo))> v;"); 9650 // Check that it is parsed as a multiplication without AttributeMacros and 9651 // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros. 9652 verifyFormat("vector<SomeType * __attr1> v;"); 9653 verifyFormat("vector<SomeType __attr1 *> v;"); 9654 verifyFormat("vector<SomeType __attr1 *const> v;"); 9655 verifyFormat("vector<SomeType __attr1 * __attr2> v;"); 9656 verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs); 9657 verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs); 9658 verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs); 9659 verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs); 9660 verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs); 9661 verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs); 9662 verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs); 9663 9664 // Check that these are not parsed as function declarations: 9665 CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 9666 CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman; 9667 verifyFormat("SomeType s(InitValue);", CustomAttrs); 9668 verifyFormat("SomeType s{InitValue};", CustomAttrs); 9669 verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs); 9670 verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs); 9671 verifyFormat("SomeType s __unused(InitValue);", CustomAttrs); 9672 verifyFormat("SomeType s __unused{InitValue};", CustomAttrs); 9673 verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs); 9674 verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs); 9675 } 9676 9677 TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) { 9678 // Check that qualifiers on pointers don't break parsing of casts. 9679 verifyFormat("x = (foo *const)*v;"); 9680 verifyFormat("x = (foo *volatile)*v;"); 9681 verifyFormat("x = (foo *restrict)*v;"); 9682 verifyFormat("x = (foo *__attribute__((foo)))*v;"); 9683 verifyFormat("x = (foo *_Nonnull)*v;"); 9684 verifyFormat("x = (foo *_Nullable)*v;"); 9685 verifyFormat("x = (foo *_Null_unspecified)*v;"); 9686 verifyFormat("x = (foo *_Nonnull)*v;"); 9687 verifyFormat("x = (foo *[[clang::attr]])*v;"); 9688 verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;"); 9689 verifyFormat("x = (foo *__ptr32)*v;"); 9690 verifyFormat("x = (foo *__ptr64)*v;"); 9691 verifyFormat("x = (foo *__capability)*v;"); 9692 9693 // Check that we handle multiple trailing qualifiers and skip them all to 9694 // determine that the expression is a cast to a pointer type. 9695 FormatStyle LongPointerRight = getLLVMStyleWithColumns(999); 9696 FormatStyle LongPointerLeft = getLLVMStyleWithColumns(999); 9697 LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left; 9698 StringRef AllQualifiers = 9699 "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified " 9700 "_Nonnull [[clang::attr]] __ptr32 __ptr64 __capability"; 9701 verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight); 9702 verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft); 9703 9704 // Also check that address-of is not parsed as a binary bitwise-and: 9705 verifyFormat("x = (foo *const)&v;"); 9706 verifyFormat(("x = (foo *" + AllQualifiers + ")&v;").str(), LongPointerRight); 9707 verifyFormat(("x = (foo* " + AllQualifiers + ")&v;").str(), LongPointerLeft); 9708 9709 // Check custom qualifiers: 9710 FormatStyle CustomQualifier = getLLVMStyleWithColumns(999); 9711 CustomQualifier.AttributeMacros.push_back("__my_qualifier"); 9712 verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier. 9713 verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier); 9714 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)*v;").str(), 9715 CustomQualifier); 9716 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)&v;").str(), 9717 CustomQualifier); 9718 9719 // Check that unknown identifiers result in binary operator parsing: 9720 verifyFormat("x = (foo * __unknown_qualifier) * v;"); 9721 verifyFormat("x = (foo * __unknown_qualifier) & v;"); 9722 } 9723 9724 TEST_F(FormatTest, UnderstandsSquareAttributes) { 9725 verifyFormat("SomeType s [[unused]] (InitValue);"); 9726 verifyFormat("SomeType s [[gnu::unused]] (InitValue);"); 9727 verifyFormat("SomeType s [[using gnu: unused]] (InitValue);"); 9728 verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}"); 9729 verifyFormat("void f() [[deprecated(\"so sorry\")]];"); 9730 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9731 " [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 9732 verifyFormat("[[nodiscard]] bool f() { return false; }"); 9733 verifyFormat("class [[nodiscard]] f {\npublic:\n f() {}\n}"); 9734 verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n f() {}\n}"); 9735 verifyFormat("class [[gnu::unused]] f {\npublic:\n f() {}\n}"); 9736 9737 // Make sure we do not mistake attributes for array subscripts. 9738 verifyFormat("int a() {}\n" 9739 "[[unused]] int b() {}\n"); 9740 verifyFormat("NSArray *arr;\n" 9741 "arr[[Foo() bar]];"); 9742 9743 // On the other hand, we still need to correctly find array subscripts. 9744 verifyFormat("int a = std::vector<int>{1, 2, 3}[0];"); 9745 9746 // Make sure that we do not mistake Objective-C method inside array literals 9747 // as attributes, even if those method names are also keywords. 9748 verifyFormat("@[ [foo bar] ];"); 9749 verifyFormat("@[ [NSArray class] ];"); 9750 verifyFormat("@[ [foo enum] ];"); 9751 9752 verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }"); 9753 9754 // Make sure we do not parse attributes as lambda introducers. 9755 FormatStyle MultiLineFunctions = getLLVMStyle(); 9756 MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 9757 verifyFormat("[[unused]] int b() {\n" 9758 " return 42;\n" 9759 "}\n", 9760 MultiLineFunctions); 9761 } 9762 9763 TEST_F(FormatTest, AttributeClass) { 9764 FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp); 9765 verifyFormat("class S {\n" 9766 " S(S&&) = default;\n" 9767 "};", 9768 Style); 9769 verifyFormat("class [[nodiscard]] S {\n" 9770 " S(S&&) = default;\n" 9771 "};", 9772 Style); 9773 verifyFormat("class __attribute((maybeunused)) S {\n" 9774 " S(S&&) = default;\n" 9775 "};", 9776 Style); 9777 verifyFormat("struct S {\n" 9778 " S(S&&) = default;\n" 9779 "};", 9780 Style); 9781 verifyFormat("struct [[nodiscard]] S {\n" 9782 " S(S&&) = default;\n" 9783 "};", 9784 Style); 9785 } 9786 9787 TEST_F(FormatTest, AttributesAfterMacro) { 9788 FormatStyle Style = getLLVMStyle(); 9789 verifyFormat("MACRO;\n" 9790 "__attribute__((maybe_unused)) int foo() {\n" 9791 " //...\n" 9792 "}"); 9793 9794 verifyFormat("MACRO;\n" 9795 "[[nodiscard]] int foo() {\n" 9796 " //...\n" 9797 "}"); 9798 9799 EXPECT_EQ("MACRO\n\n" 9800 "__attribute__((maybe_unused)) int foo() {\n" 9801 " //...\n" 9802 "}", 9803 format("MACRO\n\n" 9804 "__attribute__((maybe_unused)) int foo() {\n" 9805 " //...\n" 9806 "}")); 9807 9808 EXPECT_EQ("MACRO\n\n" 9809 "[[nodiscard]] int foo() {\n" 9810 " //...\n" 9811 "}", 9812 format("MACRO\n\n" 9813 "[[nodiscard]] int foo() {\n" 9814 " //...\n" 9815 "}")); 9816 } 9817 9818 TEST_F(FormatTest, AttributePenaltyBreaking) { 9819 FormatStyle Style = getLLVMStyle(); 9820 verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n" 9821 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", 9822 Style); 9823 verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n" 9824 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", 9825 Style); 9826 verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const " 9827 "shared_ptr<ALongTypeName> &C d) {\n}", 9828 Style); 9829 } 9830 9831 TEST_F(FormatTest, UnderstandsEllipsis) { 9832 FormatStyle Style = getLLVMStyle(); 9833 verifyFormat("int printf(const char *fmt, ...);"); 9834 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 9835 verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}"); 9836 9837 verifyFormat("template <int *...PP> a;", Style); 9838 9839 Style.PointerAlignment = FormatStyle::PAS_Left; 9840 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style); 9841 9842 verifyFormat("template <int*... PP> a;", Style); 9843 9844 Style.PointerAlignment = FormatStyle::PAS_Middle; 9845 verifyFormat("template <int *... PP> a;", Style); 9846 } 9847 9848 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 9849 EXPECT_EQ("int *a;\n" 9850 "int *a;\n" 9851 "int *a;", 9852 format("int *a;\n" 9853 "int* a;\n" 9854 "int *a;", 9855 getGoogleStyle())); 9856 EXPECT_EQ("int* a;\n" 9857 "int* a;\n" 9858 "int* a;", 9859 format("int* a;\n" 9860 "int* a;\n" 9861 "int *a;", 9862 getGoogleStyle())); 9863 EXPECT_EQ("int *a;\n" 9864 "int *a;\n" 9865 "int *a;", 9866 format("int *a;\n" 9867 "int * a;\n" 9868 "int * a;", 9869 getGoogleStyle())); 9870 EXPECT_EQ("auto x = [] {\n" 9871 " int *a;\n" 9872 " int *a;\n" 9873 " int *a;\n" 9874 "};", 9875 format("auto x=[]{int *a;\n" 9876 "int * a;\n" 9877 "int * a;};", 9878 getGoogleStyle())); 9879 } 9880 9881 TEST_F(FormatTest, UnderstandsRvalueReferences) { 9882 verifyFormat("int f(int &&a) {}"); 9883 verifyFormat("int f(int a, char &&b) {}"); 9884 verifyFormat("void f() { int &&a = b; }"); 9885 verifyGoogleFormat("int f(int a, char&& b) {}"); 9886 verifyGoogleFormat("void f() { int&& a = b; }"); 9887 9888 verifyIndependentOfContext("A<int &&> a;"); 9889 verifyIndependentOfContext("A<int &&, int &&> a;"); 9890 verifyGoogleFormat("A<int&&> a;"); 9891 verifyGoogleFormat("A<int&&, int&&> a;"); 9892 9893 // Not rvalue references: 9894 verifyFormat("template <bool B, bool C> class A {\n" 9895 " static_assert(B && C, \"Something is wrong\");\n" 9896 "};"); 9897 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 9898 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 9899 verifyFormat("#define A(a, b) (a && b)"); 9900 } 9901 9902 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 9903 verifyFormat("void f() {\n" 9904 " x[aaaaaaaaa -\n" 9905 " b] = 23;\n" 9906 "}", 9907 getLLVMStyleWithColumns(15)); 9908 } 9909 9910 TEST_F(FormatTest, FormatsCasts) { 9911 verifyFormat("Type *A = static_cast<Type *>(P);"); 9912 verifyFormat("Type *A = (Type *)P;"); 9913 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 9914 verifyFormat("int a = (int)(2.0f);"); 9915 verifyFormat("int a = (int)2.0f;"); 9916 verifyFormat("x[(int32)y];"); 9917 verifyFormat("x = (int32)y;"); 9918 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 9919 verifyFormat("int a = (int)*b;"); 9920 verifyFormat("int a = (int)2.0f;"); 9921 verifyFormat("int a = (int)~0;"); 9922 verifyFormat("int a = (int)++a;"); 9923 verifyFormat("int a = (int)sizeof(int);"); 9924 verifyFormat("int a = (int)+2;"); 9925 verifyFormat("my_int a = (my_int)2.0f;"); 9926 verifyFormat("my_int a = (my_int)sizeof(int);"); 9927 verifyFormat("return (my_int)aaa;"); 9928 verifyFormat("#define x ((int)-1)"); 9929 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 9930 verifyFormat("#define p(q) ((int *)&q)"); 9931 verifyFormat("fn(a)(b) + 1;"); 9932 9933 verifyFormat("void f() { my_int a = (my_int)*b; }"); 9934 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 9935 verifyFormat("my_int a = (my_int)~0;"); 9936 verifyFormat("my_int a = (my_int)++a;"); 9937 verifyFormat("my_int a = (my_int)-2;"); 9938 verifyFormat("my_int a = (my_int)1;"); 9939 verifyFormat("my_int a = (my_int *)1;"); 9940 verifyFormat("my_int a = (const my_int)-1;"); 9941 verifyFormat("my_int a = (const my_int *)-1;"); 9942 verifyFormat("my_int a = (my_int)(my_int)-1;"); 9943 verifyFormat("my_int a = (ns::my_int)-2;"); 9944 verifyFormat("case (my_int)ONE:"); 9945 verifyFormat("auto x = (X)this;"); 9946 // Casts in Obj-C style calls used to not be recognized as such. 9947 verifyFormat("int a = [(type*)[((type*)val) arg] arg];", getGoogleStyle()); 9948 9949 // FIXME: single value wrapped with paren will be treated as cast. 9950 verifyFormat("void f(int i = (kValue)*kMask) {}"); 9951 9952 verifyFormat("{ (void)F; }"); 9953 9954 // Don't break after a cast's 9955 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 9956 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 9957 " bbbbbbbbbbbbbbbbbbbbbb);"); 9958 9959 // These are not casts. 9960 verifyFormat("void f(int *) {}"); 9961 verifyFormat("f(foo)->b;"); 9962 verifyFormat("f(foo).b;"); 9963 verifyFormat("f(foo)(b);"); 9964 verifyFormat("f(foo)[b];"); 9965 verifyFormat("[](foo) { return 4; }(bar);"); 9966 verifyFormat("(*funptr)(foo)[4];"); 9967 verifyFormat("funptrs[4](foo)[4];"); 9968 verifyFormat("void f(int *);"); 9969 verifyFormat("void f(int *) = 0;"); 9970 verifyFormat("void f(SmallVector<int>) {}"); 9971 verifyFormat("void f(SmallVector<int>);"); 9972 verifyFormat("void f(SmallVector<int>) = 0;"); 9973 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 9974 verifyFormat("int a = sizeof(int) * b;"); 9975 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 9976 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 9977 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 9978 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 9979 9980 // These are not casts, but at some point were confused with casts. 9981 verifyFormat("virtual void foo(int *) override;"); 9982 verifyFormat("virtual void foo(char &) const;"); 9983 verifyFormat("virtual void foo(int *a, char *) const;"); 9984 verifyFormat("int a = sizeof(int *) + b;"); 9985 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 9986 verifyFormat("bool b = f(g<int>) && c;"); 9987 verifyFormat("typedef void (*f)(int i) func;"); 9988 verifyFormat("void operator++(int) noexcept;"); 9989 verifyFormat("void operator++(int &) noexcept;"); 9990 verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t " 9991 "&) noexcept;"); 9992 verifyFormat( 9993 "void operator delete(std::size_t, const std::nothrow_t &) noexcept;"); 9994 verifyFormat("void operator delete(const std::nothrow_t &) noexcept;"); 9995 verifyFormat("void operator delete(std::nothrow_t &) noexcept;"); 9996 verifyFormat("void operator delete(nothrow_t &) noexcept;"); 9997 verifyFormat("void operator delete(foo &) noexcept;"); 9998 verifyFormat("void operator delete(foo) noexcept;"); 9999 verifyFormat("void operator delete(int) noexcept;"); 10000 verifyFormat("void operator delete(int &) noexcept;"); 10001 verifyFormat("void operator delete(int &) volatile noexcept;"); 10002 verifyFormat("void operator delete(int &) const"); 10003 verifyFormat("void operator delete(int &) = default"); 10004 verifyFormat("void operator delete(int &) = delete"); 10005 verifyFormat("void operator delete(int &) [[noreturn]]"); 10006 verifyFormat("void operator delete(int &) throw();"); 10007 verifyFormat("void operator delete(int &) throw(int);"); 10008 verifyFormat("auto operator delete(int &) -> int;"); 10009 verifyFormat("auto operator delete(int &) override"); 10010 verifyFormat("auto operator delete(int &) final"); 10011 10012 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 10013 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 10014 // FIXME: The indentation here is not ideal. 10015 verifyFormat( 10016 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10017 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 10018 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 10019 } 10020 10021 TEST_F(FormatTest, FormatsFunctionTypes) { 10022 verifyFormat("A<bool()> a;"); 10023 verifyFormat("A<SomeType()> a;"); 10024 verifyFormat("A<void (*)(int, std::string)> a;"); 10025 verifyFormat("A<void *(int)>;"); 10026 verifyFormat("void *(*a)(int *, SomeType *);"); 10027 verifyFormat("int (*func)(void *);"); 10028 verifyFormat("void f() { int (*func)(void *); }"); 10029 verifyFormat("template <class CallbackClass>\n" 10030 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 10031 10032 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 10033 verifyGoogleFormat("void* (*a)(int);"); 10034 verifyGoogleFormat( 10035 "template <class CallbackClass>\n" 10036 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 10037 10038 // Other constructs can look somewhat like function types: 10039 verifyFormat("A<sizeof(*x)> a;"); 10040 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 10041 verifyFormat("some_var = function(*some_pointer_var)[0];"); 10042 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 10043 verifyFormat("int x = f(&h)();"); 10044 verifyFormat("returnsFunction(¶m1, ¶m2)(param);"); 10045 verifyFormat("std::function<\n" 10046 " LooooooooooongTemplatedType<\n" 10047 " SomeType>*(\n" 10048 " LooooooooooooooooongType type)>\n" 10049 " function;", 10050 getGoogleStyleWithColumns(40)); 10051 } 10052 10053 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 10054 verifyFormat("A (*foo_)[6];"); 10055 verifyFormat("vector<int> (*foo_)[6];"); 10056 } 10057 10058 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 10059 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10060 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 10061 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 10062 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 10063 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10064 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 10065 10066 // Different ways of ()-initializiation. 10067 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10068 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 10069 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10070 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 10071 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10072 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 10073 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10074 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 10075 10076 // Lambdas should not confuse the variable declaration heuristic. 10077 verifyFormat("LooooooooooooooooongType\n" 10078 " variable(nullptr, [](A *a) {});", 10079 getLLVMStyleWithColumns(40)); 10080 } 10081 10082 TEST_F(FormatTest, BreaksLongDeclarations) { 10083 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 10084 " AnotherNameForTheLongType;"); 10085 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 10086 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 10087 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10088 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 10089 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 10090 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 10091 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10092 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10093 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 10094 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10095 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 10096 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10097 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 10098 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10099 verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n" 10100 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10101 verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n" 10102 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10103 verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n" 10104 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10105 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10106 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);"); 10107 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10108 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}"); 10109 FormatStyle Indented = getLLVMStyle(); 10110 Indented.IndentWrappedFunctionNames = true; 10111 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10112 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 10113 Indented); 10114 verifyFormat( 10115 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10116 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 10117 Indented); 10118 verifyFormat( 10119 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 10120 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 10121 Indented); 10122 verifyFormat( 10123 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 10124 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 10125 Indented); 10126 10127 // FIXME: Without the comment, this breaks after "(". 10128 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 10129 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 10130 getGoogleStyle()); 10131 10132 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 10133 " int LoooooooooooooooooooongParam2) {}"); 10134 verifyFormat( 10135 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 10136 " SourceLocation L, IdentifierIn *II,\n" 10137 " Type *T) {}"); 10138 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 10139 "ReallyReaaallyLongFunctionName(\n" 10140 " const std::string &SomeParameter,\n" 10141 " const SomeType<string, SomeOtherTemplateParameter>\n" 10142 " &ReallyReallyLongParameterName,\n" 10143 " const SomeType<string, SomeOtherTemplateParameter>\n" 10144 " &AnotherLongParameterName) {}"); 10145 verifyFormat("template <typename A>\n" 10146 "SomeLoooooooooooooooooooooongType<\n" 10147 " typename some_namespace::SomeOtherType<A>::Type>\n" 10148 "Function() {}"); 10149 10150 verifyGoogleFormat( 10151 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 10152 " aaaaaaaaaaaaaaaaaaaaaaa;"); 10153 verifyGoogleFormat( 10154 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 10155 " SourceLocation L) {}"); 10156 verifyGoogleFormat( 10157 "some_namespace::LongReturnType\n" 10158 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 10159 " int first_long_parameter, int second_parameter) {}"); 10160 10161 verifyGoogleFormat("template <typename T>\n" 10162 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 10163 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 10164 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 10165 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 10166 10167 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 10168 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10169 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 10170 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10171 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 10172 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 10173 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10174 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 10175 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 10176 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 10177 10178 verifyFormat("template <typename T> // Templates on own line.\n" 10179 "static int // Some comment.\n" 10180 "MyFunction(int a);", 10181 getLLVMStyle()); 10182 } 10183 10184 TEST_F(FormatTest, FormatsAccessModifiers) { 10185 FormatStyle Style = getLLVMStyle(); 10186 EXPECT_EQ(Style.EmptyLineBeforeAccessModifier, 10187 FormatStyle::ELBAMS_LogicalBlock); 10188 verifyFormat("struct foo {\n" 10189 "private:\n" 10190 " void f() {}\n" 10191 "\n" 10192 "private:\n" 10193 " int i;\n" 10194 "\n" 10195 "protected:\n" 10196 " int j;\n" 10197 "};\n", 10198 Style); 10199 verifyFormat("struct foo {\n" 10200 "private:\n" 10201 " void f() {}\n" 10202 "\n" 10203 "private:\n" 10204 " int i;\n" 10205 "\n" 10206 "protected:\n" 10207 " int j;\n" 10208 "};\n", 10209 "struct foo {\n" 10210 "private:\n" 10211 " void f() {}\n" 10212 "private:\n" 10213 " int i;\n" 10214 "protected:\n" 10215 " int j;\n" 10216 "};\n", 10217 Style); 10218 verifyFormat("struct foo { /* comment */\n" 10219 "private:\n" 10220 " int i;\n" 10221 " // comment\n" 10222 "private:\n" 10223 " int j;\n" 10224 "};\n", 10225 Style); 10226 verifyFormat("struct foo {\n" 10227 "#ifdef FOO\n" 10228 "#endif\n" 10229 "private:\n" 10230 " int i;\n" 10231 "#ifdef FOO\n" 10232 "private:\n" 10233 "#endif\n" 10234 " int j;\n" 10235 "};\n", 10236 Style); 10237 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10238 verifyFormat("struct foo {\n" 10239 "private:\n" 10240 " void f() {}\n" 10241 "private:\n" 10242 " int i;\n" 10243 "protected:\n" 10244 " int j;\n" 10245 "};\n", 10246 Style); 10247 verifyFormat("struct foo {\n" 10248 "private:\n" 10249 " void f() {}\n" 10250 "private:\n" 10251 " int i;\n" 10252 "protected:\n" 10253 " int j;\n" 10254 "};\n", 10255 "struct foo {\n" 10256 "\n" 10257 "private:\n" 10258 " void f() {}\n" 10259 "\n" 10260 "private:\n" 10261 " int i;\n" 10262 "\n" 10263 "protected:\n" 10264 " int j;\n" 10265 "};\n", 10266 Style); 10267 verifyFormat("struct foo { /* comment */\n" 10268 "private:\n" 10269 " int i;\n" 10270 " // comment\n" 10271 "private:\n" 10272 " int j;\n" 10273 "};\n", 10274 "struct foo { /* comment */\n" 10275 "\n" 10276 "private:\n" 10277 " int i;\n" 10278 " // comment\n" 10279 "\n" 10280 "private:\n" 10281 " int j;\n" 10282 "};\n", 10283 Style); 10284 verifyFormat("struct foo {\n" 10285 "#ifdef FOO\n" 10286 "#endif\n" 10287 "private:\n" 10288 " int i;\n" 10289 "#ifdef FOO\n" 10290 "private:\n" 10291 "#endif\n" 10292 " int j;\n" 10293 "};\n", 10294 "struct foo {\n" 10295 "#ifdef FOO\n" 10296 "#endif\n" 10297 "\n" 10298 "private:\n" 10299 " int i;\n" 10300 "#ifdef FOO\n" 10301 "\n" 10302 "private:\n" 10303 "#endif\n" 10304 " int j;\n" 10305 "};\n", 10306 Style); 10307 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10308 verifyFormat("struct foo {\n" 10309 "private:\n" 10310 " void f() {}\n" 10311 "\n" 10312 "private:\n" 10313 " int i;\n" 10314 "\n" 10315 "protected:\n" 10316 " int j;\n" 10317 "};\n", 10318 Style); 10319 verifyFormat("struct foo {\n" 10320 "private:\n" 10321 " void f() {}\n" 10322 "\n" 10323 "private:\n" 10324 " int i;\n" 10325 "\n" 10326 "protected:\n" 10327 " int j;\n" 10328 "};\n", 10329 "struct foo {\n" 10330 "private:\n" 10331 " void f() {}\n" 10332 "private:\n" 10333 " int i;\n" 10334 "protected:\n" 10335 " int j;\n" 10336 "};\n", 10337 Style); 10338 verifyFormat("struct foo { /* comment */\n" 10339 "private:\n" 10340 " int i;\n" 10341 " // comment\n" 10342 "\n" 10343 "private:\n" 10344 " int j;\n" 10345 "};\n", 10346 "struct foo { /* comment */\n" 10347 "private:\n" 10348 " int i;\n" 10349 " // comment\n" 10350 "\n" 10351 "private:\n" 10352 " int j;\n" 10353 "};\n", 10354 Style); 10355 verifyFormat("struct foo {\n" 10356 "#ifdef FOO\n" 10357 "#endif\n" 10358 "\n" 10359 "private:\n" 10360 " int i;\n" 10361 "#ifdef FOO\n" 10362 "\n" 10363 "private:\n" 10364 "#endif\n" 10365 " int j;\n" 10366 "};\n", 10367 "struct foo {\n" 10368 "#ifdef FOO\n" 10369 "#endif\n" 10370 "private:\n" 10371 " int i;\n" 10372 "#ifdef FOO\n" 10373 "private:\n" 10374 "#endif\n" 10375 " int j;\n" 10376 "};\n", 10377 Style); 10378 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 10379 EXPECT_EQ("struct foo {\n" 10380 "\n" 10381 "private:\n" 10382 " void f() {}\n" 10383 "\n" 10384 "private:\n" 10385 " int i;\n" 10386 "\n" 10387 "protected:\n" 10388 " int j;\n" 10389 "};\n", 10390 format("struct foo {\n" 10391 "\n" 10392 "private:\n" 10393 " void f() {}\n" 10394 "\n" 10395 "private:\n" 10396 " int i;\n" 10397 "\n" 10398 "protected:\n" 10399 " int j;\n" 10400 "};\n", 10401 Style)); 10402 verifyFormat("struct foo {\n" 10403 "private:\n" 10404 " void f() {}\n" 10405 "private:\n" 10406 " int i;\n" 10407 "protected:\n" 10408 " int j;\n" 10409 "};\n", 10410 Style); 10411 EXPECT_EQ("struct foo { /* comment */\n" 10412 "\n" 10413 "private:\n" 10414 " int i;\n" 10415 " // comment\n" 10416 "\n" 10417 "private:\n" 10418 " int j;\n" 10419 "};\n", 10420 format("struct foo { /* comment */\n" 10421 "\n" 10422 "private:\n" 10423 " int i;\n" 10424 " // comment\n" 10425 "\n" 10426 "private:\n" 10427 " int j;\n" 10428 "};\n", 10429 Style)); 10430 verifyFormat("struct foo { /* comment */\n" 10431 "private:\n" 10432 " int i;\n" 10433 " // comment\n" 10434 "private:\n" 10435 " int j;\n" 10436 "};\n", 10437 Style); 10438 EXPECT_EQ("struct foo {\n" 10439 "#ifdef FOO\n" 10440 "#endif\n" 10441 "\n" 10442 "private:\n" 10443 " int i;\n" 10444 "#ifdef FOO\n" 10445 "\n" 10446 "private:\n" 10447 "#endif\n" 10448 " int j;\n" 10449 "};\n", 10450 format("struct foo {\n" 10451 "#ifdef FOO\n" 10452 "#endif\n" 10453 "\n" 10454 "private:\n" 10455 " int i;\n" 10456 "#ifdef FOO\n" 10457 "\n" 10458 "private:\n" 10459 "#endif\n" 10460 " int j;\n" 10461 "};\n", 10462 Style)); 10463 verifyFormat("struct foo {\n" 10464 "#ifdef FOO\n" 10465 "#endif\n" 10466 "private:\n" 10467 " int i;\n" 10468 "#ifdef FOO\n" 10469 "private:\n" 10470 "#endif\n" 10471 " int j;\n" 10472 "};\n", 10473 Style); 10474 10475 FormatStyle NoEmptyLines = getLLVMStyle(); 10476 NoEmptyLines.MaxEmptyLinesToKeep = 0; 10477 verifyFormat("struct foo {\n" 10478 "private:\n" 10479 " void f() {}\n" 10480 "\n" 10481 "private:\n" 10482 " int i;\n" 10483 "\n" 10484 "public:\n" 10485 "protected:\n" 10486 " int j;\n" 10487 "};\n", 10488 NoEmptyLines); 10489 10490 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10491 verifyFormat("struct foo {\n" 10492 "private:\n" 10493 " void f() {}\n" 10494 "private:\n" 10495 " int i;\n" 10496 "public:\n" 10497 "protected:\n" 10498 " int j;\n" 10499 "};\n", 10500 NoEmptyLines); 10501 10502 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10503 verifyFormat("struct foo {\n" 10504 "private:\n" 10505 " void f() {}\n" 10506 "\n" 10507 "private:\n" 10508 " int i;\n" 10509 "\n" 10510 "public:\n" 10511 "\n" 10512 "protected:\n" 10513 " int j;\n" 10514 "};\n", 10515 NoEmptyLines); 10516 } 10517 10518 TEST_F(FormatTest, FormatsAfterAccessModifiers) { 10519 10520 FormatStyle Style = getLLVMStyle(); 10521 EXPECT_EQ(Style.EmptyLineAfterAccessModifier, FormatStyle::ELAAMS_Never); 10522 verifyFormat("struct foo {\n" 10523 "private:\n" 10524 " void f() {}\n" 10525 "\n" 10526 "private:\n" 10527 " int i;\n" 10528 "\n" 10529 "protected:\n" 10530 " int j;\n" 10531 "};\n", 10532 Style); 10533 10534 // Check if lines are removed. 10535 verifyFormat("struct foo {\n" 10536 "private:\n" 10537 " void f() {}\n" 10538 "\n" 10539 "private:\n" 10540 " int i;\n" 10541 "\n" 10542 "protected:\n" 10543 " int j;\n" 10544 "};\n", 10545 "struct foo {\n" 10546 "private:\n" 10547 "\n" 10548 " void f() {}\n" 10549 "\n" 10550 "private:\n" 10551 "\n" 10552 " int i;\n" 10553 "\n" 10554 "protected:\n" 10555 "\n" 10556 " int j;\n" 10557 "};\n", 10558 Style); 10559 10560 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10561 verifyFormat("struct foo {\n" 10562 "private:\n" 10563 "\n" 10564 " void f() {}\n" 10565 "\n" 10566 "private:\n" 10567 "\n" 10568 " int i;\n" 10569 "\n" 10570 "protected:\n" 10571 "\n" 10572 " int j;\n" 10573 "};\n", 10574 Style); 10575 10576 // Check if lines are added. 10577 verifyFormat("struct foo {\n" 10578 "private:\n" 10579 "\n" 10580 " void f() {}\n" 10581 "\n" 10582 "private:\n" 10583 "\n" 10584 " int i;\n" 10585 "\n" 10586 "protected:\n" 10587 "\n" 10588 " int j;\n" 10589 "};\n", 10590 "struct foo {\n" 10591 "private:\n" 10592 " void f() {}\n" 10593 "\n" 10594 "private:\n" 10595 " int i;\n" 10596 "\n" 10597 "protected:\n" 10598 " int j;\n" 10599 "};\n", 10600 Style); 10601 10602 // Leave tests rely on the code layout, test::messUp can not be used. 10603 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 10604 Style.MaxEmptyLinesToKeep = 0u; 10605 verifyFormat("struct foo {\n" 10606 "private:\n" 10607 " void f() {}\n" 10608 "\n" 10609 "private:\n" 10610 " int i;\n" 10611 "\n" 10612 "protected:\n" 10613 " int j;\n" 10614 "};\n", 10615 Style); 10616 10617 // Check if MaxEmptyLinesToKeep is respected. 10618 EXPECT_EQ("struct foo {\n" 10619 "private:\n" 10620 " void f() {}\n" 10621 "\n" 10622 "private:\n" 10623 " int i;\n" 10624 "\n" 10625 "protected:\n" 10626 " int j;\n" 10627 "};\n", 10628 format("struct foo {\n" 10629 "private:\n" 10630 "\n\n\n" 10631 " void f() {}\n" 10632 "\n" 10633 "private:\n" 10634 "\n\n\n" 10635 " int i;\n" 10636 "\n" 10637 "protected:\n" 10638 "\n\n\n" 10639 " int j;\n" 10640 "};\n", 10641 Style)); 10642 10643 Style.MaxEmptyLinesToKeep = 1u; 10644 EXPECT_EQ("struct foo {\n" 10645 "private:\n" 10646 "\n" 10647 " void f() {}\n" 10648 "\n" 10649 "private:\n" 10650 "\n" 10651 " int i;\n" 10652 "\n" 10653 "protected:\n" 10654 "\n" 10655 " int j;\n" 10656 "};\n", 10657 format("struct foo {\n" 10658 "private:\n" 10659 "\n" 10660 " void f() {}\n" 10661 "\n" 10662 "private:\n" 10663 "\n" 10664 " int i;\n" 10665 "\n" 10666 "protected:\n" 10667 "\n" 10668 " int j;\n" 10669 "};\n", 10670 Style)); 10671 // Check if no lines are kept. 10672 EXPECT_EQ("struct foo {\n" 10673 "private:\n" 10674 " void f() {}\n" 10675 "\n" 10676 "private:\n" 10677 " int i;\n" 10678 "\n" 10679 "protected:\n" 10680 " int j;\n" 10681 "};\n", 10682 format("struct foo {\n" 10683 "private:\n" 10684 " void f() {}\n" 10685 "\n" 10686 "private:\n" 10687 " int i;\n" 10688 "\n" 10689 "protected:\n" 10690 " int j;\n" 10691 "};\n", 10692 Style)); 10693 // Check if MaxEmptyLinesToKeep is respected. 10694 EXPECT_EQ("struct foo {\n" 10695 "private:\n" 10696 "\n" 10697 " void f() {}\n" 10698 "\n" 10699 "private:\n" 10700 "\n" 10701 " int i;\n" 10702 "\n" 10703 "protected:\n" 10704 "\n" 10705 " int j;\n" 10706 "};\n", 10707 format("struct foo {\n" 10708 "private:\n" 10709 "\n\n\n" 10710 " void f() {}\n" 10711 "\n" 10712 "private:\n" 10713 "\n\n\n" 10714 " int i;\n" 10715 "\n" 10716 "protected:\n" 10717 "\n\n\n" 10718 " int j;\n" 10719 "};\n", 10720 Style)); 10721 10722 Style.MaxEmptyLinesToKeep = 10u; 10723 EXPECT_EQ("struct foo {\n" 10724 "private:\n" 10725 "\n\n\n" 10726 " void f() {}\n" 10727 "\n" 10728 "private:\n" 10729 "\n\n\n" 10730 " int i;\n" 10731 "\n" 10732 "protected:\n" 10733 "\n\n\n" 10734 " int j;\n" 10735 "};\n", 10736 format("struct foo {\n" 10737 "private:\n" 10738 "\n\n\n" 10739 " void f() {}\n" 10740 "\n" 10741 "private:\n" 10742 "\n\n\n" 10743 " int i;\n" 10744 "\n" 10745 "protected:\n" 10746 "\n\n\n" 10747 " int j;\n" 10748 "};\n", 10749 Style)); 10750 10751 // Test with comments. 10752 Style = getLLVMStyle(); 10753 verifyFormat("struct foo {\n" 10754 "private:\n" 10755 " // comment\n" 10756 " void f() {}\n" 10757 "\n" 10758 "private: /* comment */\n" 10759 " int i;\n" 10760 "};\n", 10761 Style); 10762 verifyFormat("struct foo {\n" 10763 "private:\n" 10764 " // comment\n" 10765 " void f() {}\n" 10766 "\n" 10767 "private: /* comment */\n" 10768 " int i;\n" 10769 "};\n", 10770 "struct foo {\n" 10771 "private:\n" 10772 "\n" 10773 " // comment\n" 10774 " void f() {}\n" 10775 "\n" 10776 "private: /* comment */\n" 10777 "\n" 10778 " int i;\n" 10779 "};\n", 10780 Style); 10781 10782 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10783 verifyFormat("struct foo {\n" 10784 "private:\n" 10785 "\n" 10786 " // comment\n" 10787 " void f() {}\n" 10788 "\n" 10789 "private: /* comment */\n" 10790 "\n" 10791 " int i;\n" 10792 "};\n", 10793 "struct foo {\n" 10794 "private:\n" 10795 " // comment\n" 10796 " void f() {}\n" 10797 "\n" 10798 "private: /* comment */\n" 10799 " int i;\n" 10800 "};\n", 10801 Style); 10802 verifyFormat("struct foo {\n" 10803 "private:\n" 10804 "\n" 10805 " // comment\n" 10806 " void f() {}\n" 10807 "\n" 10808 "private: /* comment */\n" 10809 "\n" 10810 " int i;\n" 10811 "};\n", 10812 Style); 10813 10814 // Test with preprocessor defines. 10815 Style = getLLVMStyle(); 10816 verifyFormat("struct foo {\n" 10817 "private:\n" 10818 "#ifdef FOO\n" 10819 "#endif\n" 10820 " void f() {}\n" 10821 "};\n", 10822 Style); 10823 verifyFormat("struct foo {\n" 10824 "private:\n" 10825 "#ifdef FOO\n" 10826 "#endif\n" 10827 " void f() {}\n" 10828 "};\n", 10829 "struct foo {\n" 10830 "private:\n" 10831 "\n" 10832 "#ifdef FOO\n" 10833 "#endif\n" 10834 " void f() {}\n" 10835 "};\n", 10836 Style); 10837 10838 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10839 verifyFormat("struct foo {\n" 10840 "private:\n" 10841 "\n" 10842 "#ifdef FOO\n" 10843 "#endif\n" 10844 " void f() {}\n" 10845 "};\n", 10846 "struct foo {\n" 10847 "private:\n" 10848 "#ifdef FOO\n" 10849 "#endif\n" 10850 " void f() {}\n" 10851 "};\n", 10852 Style); 10853 verifyFormat("struct foo {\n" 10854 "private:\n" 10855 "\n" 10856 "#ifdef FOO\n" 10857 "#endif\n" 10858 " void f() {}\n" 10859 "};\n", 10860 Style); 10861 } 10862 10863 TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) { 10864 // Combined tests of EmptyLineAfterAccessModifier and 10865 // EmptyLineBeforeAccessModifier. 10866 FormatStyle Style = getLLVMStyle(); 10867 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10868 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10869 verifyFormat("struct foo {\n" 10870 "private:\n" 10871 "\n" 10872 "protected:\n" 10873 "};\n", 10874 Style); 10875 10876 Style.MaxEmptyLinesToKeep = 10u; 10877 // Both remove all new lines. 10878 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10879 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 10880 verifyFormat("struct foo {\n" 10881 "private:\n" 10882 "protected:\n" 10883 "};\n", 10884 "struct foo {\n" 10885 "private:\n" 10886 "\n\n\n" 10887 "protected:\n" 10888 "};\n", 10889 Style); 10890 10891 // Leave tests rely on the code layout, test::messUp can not be used. 10892 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 10893 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 10894 Style.MaxEmptyLinesToKeep = 10u; 10895 EXPECT_EQ("struct foo {\n" 10896 "private:\n" 10897 "\n\n\n" 10898 "protected:\n" 10899 "};\n", 10900 format("struct foo {\n" 10901 "private:\n" 10902 "\n\n\n" 10903 "protected:\n" 10904 "};\n", 10905 Style)); 10906 Style.MaxEmptyLinesToKeep = 3u; 10907 EXPECT_EQ("struct foo {\n" 10908 "private:\n" 10909 "\n\n\n" 10910 "protected:\n" 10911 "};\n", 10912 format("struct foo {\n" 10913 "private:\n" 10914 "\n\n\n" 10915 "protected:\n" 10916 "};\n", 10917 Style)); 10918 Style.MaxEmptyLinesToKeep = 1u; 10919 EXPECT_EQ("struct foo {\n" 10920 "private:\n" 10921 "\n\n\n" 10922 "protected:\n" 10923 "};\n", 10924 format("struct foo {\n" 10925 "private:\n" 10926 "\n\n\n" 10927 "protected:\n" 10928 "};\n", 10929 Style)); // Based on new lines in original document and not 10930 // on the setting. 10931 10932 Style.MaxEmptyLinesToKeep = 10u; 10933 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10934 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 10935 // Newlines are kept if they are greater than zero, 10936 // test::messUp removes all new lines which changes the logic 10937 EXPECT_EQ("struct foo {\n" 10938 "private:\n" 10939 "\n\n\n" 10940 "protected:\n" 10941 "};\n", 10942 format("struct foo {\n" 10943 "private:\n" 10944 "\n\n\n" 10945 "protected:\n" 10946 "};\n", 10947 Style)); 10948 10949 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 10950 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10951 // test::messUp removes all new lines which changes the logic 10952 EXPECT_EQ("struct foo {\n" 10953 "private:\n" 10954 "\n\n\n" 10955 "protected:\n" 10956 "};\n", 10957 format("struct foo {\n" 10958 "private:\n" 10959 "\n\n\n" 10960 "protected:\n" 10961 "};\n", 10962 Style)); 10963 10964 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 10965 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 10966 EXPECT_EQ("struct foo {\n" 10967 "private:\n" 10968 "\n\n\n" 10969 "protected:\n" 10970 "};\n", 10971 format("struct foo {\n" 10972 "private:\n" 10973 "\n\n\n" 10974 "protected:\n" 10975 "};\n", 10976 Style)); // test::messUp removes all new lines which changes 10977 // the logic. 10978 10979 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10980 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 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_Always; 10993 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 10994 EXPECT_EQ("struct foo {\n" 10995 "private:\n" 10996 "\n\n\n" 10997 "protected:\n" 10998 "};\n", 10999 format("struct foo {\n" 11000 "private:\n" 11001 "\n\n\n" 11002 "protected:\n" 11003 "};\n", 11004 Style)); // test::messUp removes all new lines which changes 11005 // the logic. 11006 11007 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 11008 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11009 verifyFormat("struct foo {\n" 11010 "private:\n" 11011 "protected:\n" 11012 "};\n", 11013 "struct foo {\n" 11014 "private:\n" 11015 "\n\n\n" 11016 "protected:\n" 11017 "};\n", 11018 Style); 11019 11020 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 11021 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11022 verifyFormat("struct foo {\n" 11023 "private:\n" 11024 "protected:\n" 11025 "};\n", 11026 "struct foo {\n" 11027 "private:\n" 11028 "\n\n\n" 11029 "protected:\n" 11030 "};\n", 11031 Style); 11032 11033 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 11034 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 11035 verifyFormat("struct foo {\n" 11036 "private:\n" 11037 "protected:\n" 11038 "};\n", 11039 "struct foo {\n" 11040 "private:\n" 11041 "\n\n\n" 11042 "protected:\n" 11043 "};\n", 11044 Style); 11045 11046 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 11047 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 11048 verifyFormat("struct foo {\n" 11049 "private:\n" 11050 "protected:\n" 11051 "};\n", 11052 "struct foo {\n" 11053 "private:\n" 11054 "\n\n\n" 11055 "protected:\n" 11056 "};\n", 11057 Style); 11058 } 11059 11060 TEST_F(FormatTest, FormatsArrays) { 11061 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 11062 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 11063 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 11064 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 11065 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 11066 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 11067 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11068 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 11069 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11070 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 11071 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11072 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 11073 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 11074 verifyFormat( 11075 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 11076 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 11077 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 11078 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" 11079 " .aaaaaaaaaaaaaaaaaaaaaa();"); 11080 11081 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 11082 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 11083 verifyFormat( 11084 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 11085 " .aaaaaaa[0]\n" 11086 " .aaaaaaaaaaaaaaaaaaaaaa();"); 11087 verifyFormat("a[::b::c];"); 11088 11089 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 11090 11091 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 11092 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 11093 } 11094 11095 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 11096 verifyFormat("(a)->b();"); 11097 verifyFormat("--a;"); 11098 } 11099 11100 TEST_F(FormatTest, HandlesIncludeDirectives) { 11101 verifyFormat("#include <string>\n" 11102 "#include <a/b/c.h>\n" 11103 "#include \"a/b/string\"\n" 11104 "#include \"string.h\"\n" 11105 "#include \"string.h\"\n" 11106 "#include <a-a>\n" 11107 "#include < path with space >\n" 11108 "#include_next <test.h>" 11109 "#include \"abc.h\" // this is included for ABC\n" 11110 "#include \"some long include\" // with a comment\n" 11111 "#include \"some very long include path\"\n" 11112 "#include <some/very/long/include/path>\n", 11113 getLLVMStyleWithColumns(35)); 11114 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 11115 EXPECT_EQ("#include <a>", format("#include<a>")); 11116 11117 verifyFormat("#import <string>"); 11118 verifyFormat("#import <a/b/c.h>"); 11119 verifyFormat("#import \"a/b/string\""); 11120 verifyFormat("#import \"string.h\""); 11121 verifyFormat("#import \"string.h\""); 11122 verifyFormat("#if __has_include(<strstream>)\n" 11123 "#include <strstream>\n" 11124 "#endif"); 11125 11126 verifyFormat("#define MY_IMPORT <a/b>"); 11127 11128 verifyFormat("#if __has_include(<a/b>)"); 11129 verifyFormat("#if __has_include_next(<a/b>)"); 11130 verifyFormat("#define F __has_include(<a/b>)"); 11131 verifyFormat("#define F __has_include_next(<a/b>)"); 11132 11133 // Protocol buffer definition or missing "#". 11134 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 11135 getLLVMStyleWithColumns(30)); 11136 11137 FormatStyle Style = getLLVMStyle(); 11138 Style.AlwaysBreakBeforeMultilineStrings = true; 11139 Style.ColumnLimit = 0; 11140 verifyFormat("#import \"abc.h\"", Style); 11141 11142 // But 'import' might also be a regular C++ namespace. 11143 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11144 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 11145 } 11146 11147 //===----------------------------------------------------------------------===// 11148 // Error recovery tests. 11149 //===----------------------------------------------------------------------===// 11150 11151 TEST_F(FormatTest, IncompleteParameterLists) { 11152 FormatStyle NoBinPacking = getLLVMStyle(); 11153 NoBinPacking.BinPackParameters = false; 11154 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 11155 " double *min_x,\n" 11156 " double *max_x,\n" 11157 " double *min_y,\n" 11158 " double *max_y,\n" 11159 " double *min_z,\n" 11160 " double *max_z, ) {}", 11161 NoBinPacking); 11162 } 11163 11164 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 11165 verifyFormat("void f() { return; }\n42"); 11166 verifyFormat("void f() {\n" 11167 " if (0)\n" 11168 " return;\n" 11169 "}\n" 11170 "42"); 11171 verifyFormat("void f() { return }\n42"); 11172 verifyFormat("void f() {\n" 11173 " if (0)\n" 11174 " return\n" 11175 "}\n" 11176 "42"); 11177 } 11178 11179 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 11180 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 11181 EXPECT_EQ("void f() {\n" 11182 " if (a)\n" 11183 " return\n" 11184 "}", 11185 format("void f ( ) { if ( a ) return }")); 11186 EXPECT_EQ("namespace N {\n" 11187 "void f()\n" 11188 "}", 11189 format("namespace N { void f() }")); 11190 EXPECT_EQ("namespace N {\n" 11191 "void f() {}\n" 11192 "void g()\n" 11193 "} // namespace N", 11194 format("namespace N { void f( ) { } void g( ) }")); 11195 } 11196 11197 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 11198 verifyFormat("int aaaaaaaa =\n" 11199 " // Overlylongcomment\n" 11200 " b;", 11201 getLLVMStyleWithColumns(20)); 11202 verifyFormat("function(\n" 11203 " ShortArgument,\n" 11204 " LoooooooooooongArgument);\n", 11205 getLLVMStyleWithColumns(20)); 11206 } 11207 11208 TEST_F(FormatTest, IncorrectAccessSpecifier) { 11209 verifyFormat("public:"); 11210 verifyFormat("class A {\n" 11211 "public\n" 11212 " void f() {}\n" 11213 "};"); 11214 verifyFormat("public\n" 11215 "int qwerty;"); 11216 verifyFormat("public\n" 11217 "B {}"); 11218 verifyFormat("public\n" 11219 "{}"); 11220 verifyFormat("public\n" 11221 "B { int x; }"); 11222 } 11223 11224 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 11225 verifyFormat("{"); 11226 verifyFormat("#})"); 11227 verifyNoCrash("(/**/[:!] ?[)."); 11228 } 11229 11230 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) { 11231 // Found by oss-fuzz: 11232 // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212 11233 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 11234 Style.ColumnLimit = 60; 11235 verifyNoCrash( 11236 "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20" 11237 "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20" 11238 "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a", 11239 Style); 11240 } 11241 11242 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 11243 verifyFormat("do {\n}"); 11244 verifyFormat("do {\n}\n" 11245 "f();"); 11246 verifyFormat("do {\n}\n" 11247 "wheeee(fun);"); 11248 verifyFormat("do {\n" 11249 " f();\n" 11250 "}"); 11251 } 11252 11253 TEST_F(FormatTest, IncorrectCodeMissingParens) { 11254 verifyFormat("if {\n foo;\n foo();\n}"); 11255 verifyFormat("switch {\n foo;\n foo();\n}"); 11256 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 11257 verifyFormat("while {\n foo;\n foo();\n}"); 11258 verifyFormat("do {\n foo;\n foo();\n} while;"); 11259 } 11260 11261 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 11262 verifyIncompleteFormat("namespace {\n" 11263 "class Foo { Foo (\n" 11264 "};\n" 11265 "} // namespace"); 11266 } 11267 11268 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 11269 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 11270 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 11271 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 11272 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 11273 11274 EXPECT_EQ("{\n" 11275 " {\n" 11276 " breakme(\n" 11277 " qwe);\n" 11278 " }\n", 11279 format("{\n" 11280 " {\n" 11281 " breakme(qwe);\n" 11282 "}\n", 11283 getLLVMStyleWithColumns(10))); 11284 } 11285 11286 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 11287 verifyFormat("int x = {\n" 11288 " avariable,\n" 11289 " b(alongervariable)};", 11290 getLLVMStyleWithColumns(25)); 11291 } 11292 11293 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 11294 verifyFormat("return (a)(b){1, 2, 3};"); 11295 } 11296 11297 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 11298 verifyFormat("vector<int> x{1, 2, 3, 4};"); 11299 verifyFormat("vector<int> x{\n" 11300 " 1,\n" 11301 " 2,\n" 11302 " 3,\n" 11303 " 4,\n" 11304 "};"); 11305 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 11306 verifyFormat("f({1, 2});"); 11307 verifyFormat("auto v = Foo{-1};"); 11308 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 11309 verifyFormat("Class::Class : member{1, 2, 3} {}"); 11310 verifyFormat("new vector<int>{1, 2, 3};"); 11311 verifyFormat("new int[3]{1, 2, 3};"); 11312 verifyFormat("new int{1};"); 11313 verifyFormat("return {arg1, arg2};"); 11314 verifyFormat("return {arg1, SomeType{parameter}};"); 11315 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 11316 verifyFormat("new T{arg1, arg2};"); 11317 verifyFormat("f(MyMap[{composite, key}]);"); 11318 verifyFormat("class Class {\n" 11319 " T member = {arg1, arg2};\n" 11320 "};"); 11321 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 11322 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 11323 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 11324 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 11325 verifyFormat("int a = std::is_integral<int>{} + 0;"); 11326 11327 verifyFormat("int foo(int i) { return fo1{}(i); }"); 11328 verifyFormat("int foo(int i) { return fo1{}(i); }"); 11329 verifyFormat("auto i = decltype(x){};"); 11330 verifyFormat("auto i = typeof(x){};"); 11331 verifyFormat("auto i = _Atomic(x){};"); 11332 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 11333 verifyFormat("Node n{1, Node{1000}, //\n" 11334 " 2};"); 11335 verifyFormat("Aaaa aaaaaaa{\n" 11336 " {\n" 11337 " aaaa,\n" 11338 " },\n" 11339 "};"); 11340 verifyFormat("class C : public D {\n" 11341 " SomeClass SC{2};\n" 11342 "};"); 11343 verifyFormat("class C : public A {\n" 11344 " class D : public B {\n" 11345 " void f() { int i{2}; }\n" 11346 " };\n" 11347 "};"); 11348 verifyFormat("#define A {a, a},"); 11349 11350 // Avoid breaking between equal sign and opening brace 11351 FormatStyle AvoidBreakingFirstArgument = getLLVMStyle(); 11352 AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200; 11353 verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n" 11354 " {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n" 11355 " {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n" 11356 " {\"ccccccccccccccccccccc\", 2}};", 11357 AvoidBreakingFirstArgument); 11358 11359 // Binpacking only if there is no trailing comma 11360 verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n" 11361 " cccccccccc, dddddddddd};", 11362 getLLVMStyleWithColumns(50)); 11363 verifyFormat("const Aaaaaa aaaaa = {\n" 11364 " aaaaaaaaaaa,\n" 11365 " bbbbbbbbbbb,\n" 11366 " ccccccccccc,\n" 11367 " ddddddddddd,\n" 11368 "};", 11369 getLLVMStyleWithColumns(50)); 11370 11371 // Cases where distinguising braced lists and blocks is hard. 11372 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);"); 11373 verifyFormat("void f() {\n" 11374 " return; // comment\n" 11375 "}\n" 11376 "SomeType t;"); 11377 verifyFormat("void f() {\n" 11378 " if (a) {\n" 11379 " f();\n" 11380 " }\n" 11381 "}\n" 11382 "SomeType t;"); 11383 11384 // In combination with BinPackArguments = false. 11385 FormatStyle NoBinPacking = getLLVMStyle(); 11386 NoBinPacking.BinPackArguments = false; 11387 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 11388 " bbbbb,\n" 11389 " ccccc,\n" 11390 " ddddd,\n" 11391 " eeeee,\n" 11392 " ffffff,\n" 11393 " ggggg,\n" 11394 " hhhhhh,\n" 11395 " iiiiii,\n" 11396 " jjjjjj,\n" 11397 " kkkkkk};", 11398 NoBinPacking); 11399 verifyFormat("const Aaaaaa aaaaa = {\n" 11400 " aaaaa,\n" 11401 " bbbbb,\n" 11402 " ccccc,\n" 11403 " ddddd,\n" 11404 " eeeee,\n" 11405 " ffffff,\n" 11406 " ggggg,\n" 11407 " hhhhhh,\n" 11408 " iiiiii,\n" 11409 " jjjjjj,\n" 11410 " kkkkkk,\n" 11411 "};", 11412 NoBinPacking); 11413 verifyFormat( 11414 "const Aaaaaa aaaaa = {\n" 11415 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 11416 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 11417 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 11418 "};", 11419 NoBinPacking); 11420 11421 NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 11422 EXPECT_EQ("static uint8 CddDp83848Reg[] = {\n" 11423 " CDDDP83848_BMCR_REGISTER,\n" 11424 " CDDDP83848_BMSR_REGISTER,\n" 11425 " CDDDP83848_RBR_REGISTER};", 11426 format("static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n" 11427 " CDDDP83848_BMSR_REGISTER,\n" 11428 " CDDDP83848_RBR_REGISTER};", 11429 NoBinPacking)); 11430 11431 // FIXME: The alignment of these trailing comments might be bad. Then again, 11432 // this might be utterly useless in real code. 11433 verifyFormat("Constructor::Constructor()\n" 11434 " : some_value{ //\n" 11435 " aaaaaaa, //\n" 11436 " bbbbbbb} {}"); 11437 11438 // In braced lists, the first comment is always assumed to belong to the 11439 // first element. Thus, it can be moved to the next or previous line as 11440 // appropriate. 11441 EXPECT_EQ("function({// First element:\n" 11442 " 1,\n" 11443 " // Second element:\n" 11444 " 2});", 11445 format("function({\n" 11446 " // First element:\n" 11447 " 1,\n" 11448 " // Second element:\n" 11449 " 2});")); 11450 EXPECT_EQ("std::vector<int> MyNumbers{\n" 11451 " // First element:\n" 11452 " 1,\n" 11453 " // Second element:\n" 11454 " 2};", 11455 format("std::vector<int> MyNumbers{// First element:\n" 11456 " 1,\n" 11457 " // Second element:\n" 11458 " 2};", 11459 getLLVMStyleWithColumns(30))); 11460 // A trailing comma should still lead to an enforced line break and no 11461 // binpacking. 11462 EXPECT_EQ("vector<int> SomeVector = {\n" 11463 " // aaa\n" 11464 " 1,\n" 11465 " 2,\n" 11466 "};", 11467 format("vector<int> SomeVector = { // aaa\n" 11468 " 1, 2, };")); 11469 11470 // C++11 brace initializer list l-braces should not be treated any differently 11471 // when breaking before lambda bodies is enabled 11472 FormatStyle BreakBeforeLambdaBody = getLLVMStyle(); 11473 BreakBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 11474 BreakBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 11475 BreakBeforeLambdaBody.AlwaysBreakBeforeMultilineStrings = true; 11476 verifyFormat( 11477 "std::runtime_error{\n" 11478 " \"Long string which will force a break onto the next line...\"};", 11479 BreakBeforeLambdaBody); 11480 11481 FormatStyle ExtraSpaces = getLLVMStyle(); 11482 ExtraSpaces.Cpp11BracedListStyle = false; 11483 ExtraSpaces.ColumnLimit = 75; 11484 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 11485 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 11486 verifyFormat("f({ 1, 2 });", ExtraSpaces); 11487 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 11488 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 11489 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 11490 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 11491 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 11492 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 11493 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 11494 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 11495 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 11496 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 11497 verifyFormat("class Class {\n" 11498 " T member = { arg1, arg2 };\n" 11499 "};", 11500 ExtraSpaces); 11501 verifyFormat( 11502 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11503 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 11504 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 11505 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 11506 ExtraSpaces); 11507 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 11508 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 11509 ExtraSpaces); 11510 verifyFormat( 11511 "someFunction(OtherParam,\n" 11512 " BracedList{ // comment 1 (Forcing interesting break)\n" 11513 " param1, param2,\n" 11514 " // comment 2\n" 11515 " param3, param4 });", 11516 ExtraSpaces); 11517 verifyFormat( 11518 "std::this_thread::sleep_for(\n" 11519 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 11520 ExtraSpaces); 11521 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n" 11522 " aaaaaaa,\n" 11523 " aaaaaaaaaa,\n" 11524 " aaaaa,\n" 11525 " aaaaaaaaaaaaaaa,\n" 11526 " aaa,\n" 11527 " aaaaaaaaaa,\n" 11528 " a,\n" 11529 " aaaaaaaaaaaaaaaaaaaaa,\n" 11530 " aaaaaaaaaaaa,\n" 11531 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 11532 " aaaaaaa,\n" 11533 " a};"); 11534 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 11535 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces); 11536 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces); 11537 11538 // Avoid breaking between initializer/equal sign and opening brace 11539 ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200; 11540 verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n" 11541 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 11542 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 11543 " { \"ccccccccccccccccccccc\", 2 }\n" 11544 "};", 11545 ExtraSpaces); 11546 verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n" 11547 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 11548 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 11549 " { \"ccccccccccccccccccccc\", 2 }\n" 11550 "};", 11551 ExtraSpaces); 11552 11553 FormatStyle SpaceBeforeBrace = getLLVMStyle(); 11554 SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true; 11555 verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace); 11556 verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace); 11557 11558 FormatStyle SpaceBetweenBraces = getLLVMStyle(); 11559 SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always; 11560 SpaceBetweenBraces.SpacesInParentheses = true; 11561 SpaceBetweenBraces.SpacesInSquareBrackets = true; 11562 verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces); 11563 verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces); 11564 verifyFormat("vector< int > x{ // comment 1\n" 11565 " 1, 2, 3, 4 };", 11566 SpaceBetweenBraces); 11567 SpaceBetweenBraces.ColumnLimit = 20; 11568 EXPECT_EQ("vector< int > x{\n" 11569 " 1, 2, 3, 4 };", 11570 format("vector<int>x{1,2,3,4};", SpaceBetweenBraces)); 11571 SpaceBetweenBraces.ColumnLimit = 24; 11572 EXPECT_EQ("vector< int > x{ 1, 2,\n" 11573 " 3, 4 };", 11574 format("vector<int>x{1,2,3,4};", SpaceBetweenBraces)); 11575 EXPECT_EQ("vector< int > x{\n" 11576 " 1,\n" 11577 " 2,\n" 11578 " 3,\n" 11579 " 4,\n" 11580 "};", 11581 format("vector<int>x{1,2,3,4,};", SpaceBetweenBraces)); 11582 verifyFormat("vector< int > x{};", SpaceBetweenBraces); 11583 SpaceBetweenBraces.SpaceInEmptyParentheses = true; 11584 verifyFormat("vector< int > x{ };", SpaceBetweenBraces); 11585 } 11586 11587 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 11588 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11589 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11590 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11591 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11592 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11593 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 11594 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 11595 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11596 " 1, 22, 333, 4444, 55555, //\n" 11597 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11598 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 11599 verifyFormat( 11600 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11601 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11602 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 11603 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 11604 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 11605 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 11606 " 7777777};"); 11607 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 11608 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 11609 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 11610 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 11611 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 11612 " // Separating comment.\n" 11613 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 11614 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 11615 " // Leading comment\n" 11616 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 11617 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 11618 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 11619 " 1, 1, 1, 1};", 11620 getLLVMStyleWithColumns(39)); 11621 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 11622 " 1, 1, 1, 1};", 11623 getLLVMStyleWithColumns(38)); 11624 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 11625 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 11626 getLLVMStyleWithColumns(43)); 11627 verifyFormat( 11628 "static unsigned SomeValues[10][3] = {\n" 11629 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 11630 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 11631 verifyFormat("static auto fields = new vector<string>{\n" 11632 " \"aaaaaaaaaaaaa\",\n" 11633 " \"aaaaaaaaaaaaa\",\n" 11634 " \"aaaaaaaaaaaa\",\n" 11635 " \"aaaaaaaaaaaaaa\",\n" 11636 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 11637 " \"aaaaaaaaaaaa\",\n" 11638 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 11639 "};"); 11640 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 11641 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 11642 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 11643 " 3, cccccccccccccccccccccc};", 11644 getLLVMStyleWithColumns(60)); 11645 11646 // Trailing commas. 11647 verifyFormat("vector<int> x = {\n" 11648 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 11649 "};", 11650 getLLVMStyleWithColumns(39)); 11651 verifyFormat("vector<int> x = {\n" 11652 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 11653 "};", 11654 getLLVMStyleWithColumns(39)); 11655 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 11656 " 1, 1, 1, 1,\n" 11657 " /**/ /**/};", 11658 getLLVMStyleWithColumns(39)); 11659 11660 // Trailing comment in the first line. 11661 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 11662 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 11663 " 111111111, 222222222, 3333333333, 444444444, //\n" 11664 " 11111111, 22222222, 333333333, 44444444};"); 11665 // Trailing comment in the last line. 11666 verifyFormat("int aaaaa[] = {\n" 11667 " 1, 2, 3, // comment\n" 11668 " 4, 5, 6 // comment\n" 11669 "};"); 11670 11671 // With nested lists, we should either format one item per line or all nested 11672 // lists one on line. 11673 // FIXME: For some nested lists, we can do better. 11674 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 11675 " {aaaaaaaaaaaaaaaaaaa},\n" 11676 " {aaaaaaaaaaaaaaaaaaaaa},\n" 11677 " {aaaaaaaaaaaaaaaaa}};", 11678 getLLVMStyleWithColumns(60)); 11679 verifyFormat( 11680 "SomeStruct my_struct_array = {\n" 11681 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 11682 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 11683 " {aaa, aaa},\n" 11684 " {aaa, aaa},\n" 11685 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 11686 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 11687 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 11688 11689 // No column layout should be used here. 11690 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 11691 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 11692 11693 verifyNoCrash("a<,"); 11694 11695 // No braced initializer here. 11696 verifyFormat("void f() {\n" 11697 " struct Dummy {};\n" 11698 " f(v);\n" 11699 "}"); 11700 11701 // Long lists should be formatted in columns even if they are nested. 11702 verifyFormat( 11703 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11704 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11705 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11706 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11707 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11708 " 1, 22, 333, 4444, 55555, 666666, 7777777});"); 11709 11710 // Allow "single-column" layout even if that violates the column limit. There 11711 // isn't going to be a better way. 11712 verifyFormat("std::vector<int> a = {\n" 11713 " aaaaaaaa,\n" 11714 " aaaaaaaa,\n" 11715 " aaaaaaaa,\n" 11716 " aaaaaaaa,\n" 11717 " aaaaaaaaaa,\n" 11718 " aaaaaaaa,\n" 11719 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};", 11720 getLLVMStyleWithColumns(30)); 11721 verifyFormat("vector<int> aaaa = {\n" 11722 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11723 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11724 " aaaaaa.aaaaaaa,\n" 11725 " aaaaaa.aaaaaaa,\n" 11726 " aaaaaa.aaaaaaa,\n" 11727 " aaaaaa.aaaaaaa,\n" 11728 "};"); 11729 11730 // Don't create hanging lists. 11731 verifyFormat("someFunction(Param, {List1, List2,\n" 11732 " List3});", 11733 getLLVMStyleWithColumns(35)); 11734 verifyFormat("someFunction(Param, Param,\n" 11735 " {List1, List2,\n" 11736 " List3});", 11737 getLLVMStyleWithColumns(35)); 11738 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n" 11739 " aaaaaaaaaaaaaaaaaaaaaaa);"); 11740 } 11741 11742 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 11743 FormatStyle DoNotMerge = getLLVMStyle(); 11744 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 11745 11746 verifyFormat("void f() { return 42; }"); 11747 verifyFormat("void f() {\n" 11748 " return 42;\n" 11749 "}", 11750 DoNotMerge); 11751 verifyFormat("void f() {\n" 11752 " // Comment\n" 11753 "}"); 11754 verifyFormat("{\n" 11755 "#error {\n" 11756 " int a;\n" 11757 "}"); 11758 verifyFormat("{\n" 11759 " int a;\n" 11760 "#error {\n" 11761 "}"); 11762 verifyFormat("void f() {} // comment"); 11763 verifyFormat("void f() { int a; } // comment"); 11764 verifyFormat("void f() {\n" 11765 "} // comment", 11766 DoNotMerge); 11767 verifyFormat("void f() {\n" 11768 " int a;\n" 11769 "} // comment", 11770 DoNotMerge); 11771 verifyFormat("void f() {\n" 11772 "} // comment", 11773 getLLVMStyleWithColumns(15)); 11774 11775 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 11776 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 11777 11778 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 11779 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 11780 verifyFormat("class C {\n" 11781 " C()\n" 11782 " : iiiiiiii(nullptr),\n" 11783 " kkkkkkk(nullptr),\n" 11784 " mmmmmmm(nullptr),\n" 11785 " nnnnnnn(nullptr) {}\n" 11786 "};", 11787 getGoogleStyle()); 11788 11789 FormatStyle NoColumnLimit = getLLVMStyle(); 11790 NoColumnLimit.ColumnLimit = 0; 11791 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 11792 EXPECT_EQ("class C {\n" 11793 " A() : b(0) {}\n" 11794 "};", 11795 format("class C{A():b(0){}};", NoColumnLimit)); 11796 EXPECT_EQ("A()\n" 11797 " : b(0) {\n" 11798 "}", 11799 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 11800 11801 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 11802 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 11803 FormatStyle::SFS_None; 11804 EXPECT_EQ("A()\n" 11805 " : b(0) {\n" 11806 "}", 11807 format("A():b(0){}", DoNotMergeNoColumnLimit)); 11808 EXPECT_EQ("A()\n" 11809 " : b(0) {\n" 11810 "}", 11811 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 11812 11813 verifyFormat("#define A \\\n" 11814 " void f() { \\\n" 11815 " int i; \\\n" 11816 " }", 11817 getLLVMStyleWithColumns(20)); 11818 verifyFormat("#define A \\\n" 11819 " void f() { int i; }", 11820 getLLVMStyleWithColumns(21)); 11821 verifyFormat("#define A \\\n" 11822 " void f() { \\\n" 11823 " int i; \\\n" 11824 " } \\\n" 11825 " int j;", 11826 getLLVMStyleWithColumns(22)); 11827 verifyFormat("#define A \\\n" 11828 " void f() { int i; } \\\n" 11829 " int j;", 11830 getLLVMStyleWithColumns(23)); 11831 } 11832 11833 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) { 11834 FormatStyle MergeEmptyOnly = getLLVMStyle(); 11835 MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 11836 verifyFormat("class C {\n" 11837 " int f() {}\n" 11838 "};", 11839 MergeEmptyOnly); 11840 verifyFormat("class C {\n" 11841 " int f() {\n" 11842 " return 42;\n" 11843 " }\n" 11844 "};", 11845 MergeEmptyOnly); 11846 verifyFormat("int f() {}", MergeEmptyOnly); 11847 verifyFormat("int f() {\n" 11848 " return 42;\n" 11849 "}", 11850 MergeEmptyOnly); 11851 11852 // Also verify behavior when BraceWrapping.AfterFunction = true 11853 MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 11854 MergeEmptyOnly.BraceWrapping.AfterFunction = true; 11855 verifyFormat("int f() {}", MergeEmptyOnly); 11856 verifyFormat("class C {\n" 11857 " int f() {}\n" 11858 "};", 11859 MergeEmptyOnly); 11860 } 11861 11862 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 11863 FormatStyle MergeInlineOnly = getLLVMStyle(); 11864 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 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_Inline implies SFS_Empty 11875 verifyFormat("class C {\n" 11876 " int f() {}\n" 11877 "};", 11878 MergeInlineOnly); 11879 verifyFormat("int f() {}", MergeInlineOnly); 11880 11881 // Also verify behavior when BraceWrapping.AfterFunction = true 11882 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 11883 MergeInlineOnly.BraceWrapping.AfterFunction = true; 11884 verifyFormat("class C {\n" 11885 " int f() { return 42; }\n" 11886 "};", 11887 MergeInlineOnly); 11888 verifyFormat("int f()\n" 11889 "{\n" 11890 " return 42;\n" 11891 "}", 11892 MergeInlineOnly); 11893 11894 // SFS_Inline implies SFS_Empty 11895 verifyFormat("int f() {}", MergeInlineOnly); 11896 verifyFormat("class C {\n" 11897 " int f() {}\n" 11898 "};", 11899 MergeInlineOnly); 11900 } 11901 11902 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) { 11903 FormatStyle MergeInlineOnly = getLLVMStyle(); 11904 MergeInlineOnly.AllowShortFunctionsOnASingleLine = 11905 FormatStyle::SFS_InlineOnly; 11906 verifyFormat("class C {\n" 11907 " int f() { return 42; }\n" 11908 "};", 11909 MergeInlineOnly); 11910 verifyFormat("int f() {\n" 11911 " return 42;\n" 11912 "}", 11913 MergeInlineOnly); 11914 11915 // SFS_InlineOnly does not imply SFS_Empty 11916 verifyFormat("class C {\n" 11917 " int f() {}\n" 11918 "};", 11919 MergeInlineOnly); 11920 verifyFormat("int f() {\n" 11921 "}", 11922 MergeInlineOnly); 11923 11924 // Also verify behavior when BraceWrapping.AfterFunction = true 11925 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 11926 MergeInlineOnly.BraceWrapping.AfterFunction = true; 11927 verifyFormat("class C {\n" 11928 " int f() { return 42; }\n" 11929 "};", 11930 MergeInlineOnly); 11931 verifyFormat("int f()\n" 11932 "{\n" 11933 " return 42;\n" 11934 "}", 11935 MergeInlineOnly); 11936 11937 // SFS_InlineOnly does not imply SFS_Empty 11938 verifyFormat("int f()\n" 11939 "{\n" 11940 "}", 11941 MergeInlineOnly); 11942 verifyFormat("class C {\n" 11943 " int f() {}\n" 11944 "};", 11945 MergeInlineOnly); 11946 } 11947 11948 TEST_F(FormatTest, SplitEmptyFunction) { 11949 FormatStyle Style = getLLVMStyle(); 11950 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 11951 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 11952 Style.BraceWrapping.AfterFunction = true; 11953 Style.BraceWrapping.SplitEmptyFunction = false; 11954 Style.ColumnLimit = 40; 11955 11956 verifyFormat("int f()\n" 11957 "{}", 11958 Style); 11959 verifyFormat("int f()\n" 11960 "{\n" 11961 " return 42;\n" 11962 "}", 11963 Style); 11964 verifyFormat("int f()\n" 11965 "{\n" 11966 " // some comment\n" 11967 "}", 11968 Style); 11969 11970 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 11971 verifyFormat("int f() {}", Style); 11972 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 11973 "{}", 11974 Style); 11975 verifyFormat("int f()\n" 11976 "{\n" 11977 " return 0;\n" 11978 "}", 11979 Style); 11980 11981 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 11982 verifyFormat("class Foo {\n" 11983 " int f() {}\n" 11984 "};\n", 11985 Style); 11986 verifyFormat("class Foo {\n" 11987 " int f() { return 0; }\n" 11988 "};\n", 11989 Style); 11990 verifyFormat("class Foo {\n" 11991 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 11992 " {}\n" 11993 "};\n", 11994 Style); 11995 verifyFormat("class Foo {\n" 11996 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 11997 " {\n" 11998 " return 0;\n" 11999 " }\n" 12000 "};\n", 12001 Style); 12002 12003 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 12004 verifyFormat("int f() {}", Style); 12005 verifyFormat("int f() { return 0; }", Style); 12006 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12007 "{}", 12008 Style); 12009 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12010 "{\n" 12011 " return 0;\n" 12012 "}", 12013 Style); 12014 } 12015 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) { 12016 FormatStyle Style = getLLVMStyle(); 12017 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 12018 verifyFormat("#ifdef A\n" 12019 "int f() {}\n" 12020 "#else\n" 12021 "int g() {}\n" 12022 "#endif", 12023 Style); 12024 } 12025 12026 TEST_F(FormatTest, SplitEmptyClass) { 12027 FormatStyle Style = getLLVMStyle(); 12028 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12029 Style.BraceWrapping.AfterClass = true; 12030 Style.BraceWrapping.SplitEmptyRecord = false; 12031 12032 verifyFormat("class Foo\n" 12033 "{};", 12034 Style); 12035 verifyFormat("/* something */ class Foo\n" 12036 "{};", 12037 Style); 12038 verifyFormat("template <typename X> class Foo\n" 12039 "{};", 12040 Style); 12041 verifyFormat("class Foo\n" 12042 "{\n" 12043 " Foo();\n" 12044 "};", 12045 Style); 12046 verifyFormat("typedef class Foo\n" 12047 "{\n" 12048 "} Foo_t;", 12049 Style); 12050 12051 Style.BraceWrapping.SplitEmptyRecord = true; 12052 Style.BraceWrapping.AfterStruct = true; 12053 verifyFormat("class rep\n" 12054 "{\n" 12055 "};", 12056 Style); 12057 verifyFormat("struct rep\n" 12058 "{\n" 12059 "};", 12060 Style); 12061 verifyFormat("template <typename T> class rep\n" 12062 "{\n" 12063 "};", 12064 Style); 12065 verifyFormat("template <typename T> struct rep\n" 12066 "{\n" 12067 "};", 12068 Style); 12069 verifyFormat("class rep\n" 12070 "{\n" 12071 " int x;\n" 12072 "};", 12073 Style); 12074 verifyFormat("struct rep\n" 12075 "{\n" 12076 " int x;\n" 12077 "};", 12078 Style); 12079 verifyFormat("template <typename T> class rep\n" 12080 "{\n" 12081 " int x;\n" 12082 "};", 12083 Style); 12084 verifyFormat("template <typename T> struct rep\n" 12085 "{\n" 12086 " int x;\n" 12087 "};", 12088 Style); 12089 verifyFormat("template <typename T> class rep // Foo\n" 12090 "{\n" 12091 " int x;\n" 12092 "};", 12093 Style); 12094 verifyFormat("template <typename T> struct rep // Bar\n" 12095 "{\n" 12096 " int x;\n" 12097 "};", 12098 Style); 12099 12100 verifyFormat("template <typename T> class rep<T>\n" 12101 "{\n" 12102 " int x;\n" 12103 "};", 12104 Style); 12105 12106 verifyFormat("template <typename T> class rep<std::complex<T>>\n" 12107 "{\n" 12108 " int x;\n" 12109 "};", 12110 Style); 12111 verifyFormat("template <typename T> class rep<std::complex<T>>\n" 12112 "{\n" 12113 "};", 12114 Style); 12115 12116 verifyFormat("#include \"stdint.h\"\n" 12117 "namespace rep {}", 12118 Style); 12119 verifyFormat("#include <stdint.h>\n" 12120 "namespace rep {}", 12121 Style); 12122 verifyFormat("#include <stdint.h>\n" 12123 "namespace rep {}", 12124 "#include <stdint.h>\n" 12125 "namespace rep {\n" 12126 "\n" 12127 "\n" 12128 "}", 12129 Style); 12130 } 12131 12132 TEST_F(FormatTest, SplitEmptyStruct) { 12133 FormatStyle Style = getLLVMStyle(); 12134 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12135 Style.BraceWrapping.AfterStruct = true; 12136 Style.BraceWrapping.SplitEmptyRecord = false; 12137 12138 verifyFormat("struct Foo\n" 12139 "{};", 12140 Style); 12141 verifyFormat("/* something */ struct Foo\n" 12142 "{};", 12143 Style); 12144 verifyFormat("template <typename X> struct Foo\n" 12145 "{};", 12146 Style); 12147 verifyFormat("struct Foo\n" 12148 "{\n" 12149 " Foo();\n" 12150 "};", 12151 Style); 12152 verifyFormat("typedef struct Foo\n" 12153 "{\n" 12154 "} Foo_t;", 12155 Style); 12156 // typedef struct Bar {} Bar_t; 12157 } 12158 12159 TEST_F(FormatTest, SplitEmptyUnion) { 12160 FormatStyle Style = getLLVMStyle(); 12161 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12162 Style.BraceWrapping.AfterUnion = true; 12163 Style.BraceWrapping.SplitEmptyRecord = false; 12164 12165 verifyFormat("union Foo\n" 12166 "{};", 12167 Style); 12168 verifyFormat("/* something */ union Foo\n" 12169 "{};", 12170 Style); 12171 verifyFormat("union Foo\n" 12172 "{\n" 12173 " A,\n" 12174 "};", 12175 Style); 12176 verifyFormat("typedef union Foo\n" 12177 "{\n" 12178 "} Foo_t;", 12179 Style); 12180 } 12181 12182 TEST_F(FormatTest, SplitEmptyNamespace) { 12183 FormatStyle Style = getLLVMStyle(); 12184 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12185 Style.BraceWrapping.AfterNamespace = true; 12186 Style.BraceWrapping.SplitEmptyNamespace = false; 12187 12188 verifyFormat("namespace Foo\n" 12189 "{};", 12190 Style); 12191 verifyFormat("/* something */ namespace Foo\n" 12192 "{};", 12193 Style); 12194 verifyFormat("inline namespace Foo\n" 12195 "{};", 12196 Style); 12197 verifyFormat("/* something */ inline namespace Foo\n" 12198 "{};", 12199 Style); 12200 verifyFormat("export namespace Foo\n" 12201 "{};", 12202 Style); 12203 verifyFormat("namespace Foo\n" 12204 "{\n" 12205 "void Bar();\n" 12206 "};", 12207 Style); 12208 } 12209 12210 TEST_F(FormatTest, NeverMergeShortRecords) { 12211 FormatStyle Style = getLLVMStyle(); 12212 12213 verifyFormat("class Foo {\n" 12214 " Foo();\n" 12215 "};", 12216 Style); 12217 verifyFormat("typedef class Foo {\n" 12218 " Foo();\n" 12219 "} Foo_t;", 12220 Style); 12221 verifyFormat("struct Foo {\n" 12222 " Foo();\n" 12223 "};", 12224 Style); 12225 verifyFormat("typedef struct Foo {\n" 12226 " Foo();\n" 12227 "} Foo_t;", 12228 Style); 12229 verifyFormat("union Foo {\n" 12230 " A,\n" 12231 "};", 12232 Style); 12233 verifyFormat("typedef union Foo {\n" 12234 " A,\n" 12235 "} Foo_t;", 12236 Style); 12237 verifyFormat("namespace Foo {\n" 12238 "void Bar();\n" 12239 "};", 12240 Style); 12241 12242 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12243 Style.BraceWrapping.AfterClass = true; 12244 Style.BraceWrapping.AfterStruct = true; 12245 Style.BraceWrapping.AfterUnion = true; 12246 Style.BraceWrapping.AfterNamespace = true; 12247 verifyFormat("class Foo\n" 12248 "{\n" 12249 " Foo();\n" 12250 "};", 12251 Style); 12252 verifyFormat("typedef class Foo\n" 12253 "{\n" 12254 " Foo();\n" 12255 "} Foo_t;", 12256 Style); 12257 verifyFormat("struct Foo\n" 12258 "{\n" 12259 " Foo();\n" 12260 "};", 12261 Style); 12262 verifyFormat("typedef struct Foo\n" 12263 "{\n" 12264 " Foo();\n" 12265 "} Foo_t;", 12266 Style); 12267 verifyFormat("union Foo\n" 12268 "{\n" 12269 " A,\n" 12270 "};", 12271 Style); 12272 verifyFormat("typedef union Foo\n" 12273 "{\n" 12274 " A,\n" 12275 "} Foo_t;", 12276 Style); 12277 verifyFormat("namespace Foo\n" 12278 "{\n" 12279 "void Bar();\n" 12280 "};", 12281 Style); 12282 } 12283 12284 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 12285 // Elaborate type variable declarations. 12286 verifyFormat("struct foo a = {bar};\nint n;"); 12287 verifyFormat("class foo a = {bar};\nint n;"); 12288 verifyFormat("union foo a = {bar};\nint n;"); 12289 12290 // Elaborate types inside function definitions. 12291 verifyFormat("struct foo f() {}\nint n;"); 12292 verifyFormat("class foo f() {}\nint n;"); 12293 verifyFormat("union foo f() {}\nint n;"); 12294 12295 // Templates. 12296 verifyFormat("template <class X> void f() {}\nint n;"); 12297 verifyFormat("template <struct X> void f() {}\nint n;"); 12298 verifyFormat("template <union X> void f() {}\nint n;"); 12299 12300 // Actual definitions... 12301 verifyFormat("struct {\n} n;"); 12302 verifyFormat( 12303 "template <template <class T, class Y>, class Z> class X {\n} n;"); 12304 verifyFormat("union Z {\n int n;\n} x;"); 12305 verifyFormat("class MACRO Z {\n} n;"); 12306 verifyFormat("class MACRO(X) Z {\n} n;"); 12307 verifyFormat("class __attribute__(X) Z {\n} n;"); 12308 verifyFormat("class __declspec(X) Z {\n} n;"); 12309 verifyFormat("class A##B##C {\n} n;"); 12310 verifyFormat("class alignas(16) Z {\n} n;"); 12311 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 12312 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 12313 12314 // Redefinition from nested context: 12315 verifyFormat("class A::B::C {\n} n;"); 12316 12317 // Template definitions. 12318 verifyFormat( 12319 "template <typename F>\n" 12320 "Matcher(const Matcher<F> &Other,\n" 12321 " typename enable_if_c<is_base_of<F, T>::value &&\n" 12322 " !is_same<F, T>::value>::type * = 0)\n" 12323 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 12324 12325 // FIXME: This is still incorrectly handled at the formatter side. 12326 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 12327 verifyFormat("int i = SomeFunction(a<b, a> b);"); 12328 12329 // FIXME: 12330 // This now gets parsed incorrectly as class definition. 12331 // verifyFormat("class A<int> f() {\n}\nint n;"); 12332 12333 // Elaborate types where incorrectly parsing the structural element would 12334 // break the indent. 12335 verifyFormat("if (true)\n" 12336 " class X x;\n" 12337 "else\n" 12338 " f();\n"); 12339 12340 // This is simply incomplete. Formatting is not important, but must not crash. 12341 verifyFormat("class A:"); 12342 } 12343 12344 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 12345 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 12346 format("#error Leave all white!!!!! space* alone!\n")); 12347 EXPECT_EQ( 12348 "#warning Leave all white!!!!! space* alone!\n", 12349 format("#warning Leave all white!!!!! space* alone!\n")); 12350 EXPECT_EQ("#error 1", format(" # error 1")); 12351 EXPECT_EQ("#warning 1", format(" # warning 1")); 12352 } 12353 12354 TEST_F(FormatTest, FormatHashIfExpressions) { 12355 verifyFormat("#if AAAA && BBBB"); 12356 verifyFormat("#if (AAAA && BBBB)"); 12357 verifyFormat("#elif (AAAA && BBBB)"); 12358 // FIXME: Come up with a better indentation for #elif. 12359 verifyFormat( 12360 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 12361 " defined(BBBBBBBB)\n" 12362 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 12363 " defined(BBBBBBBB)\n" 12364 "#endif", 12365 getLLVMStyleWithColumns(65)); 12366 } 12367 12368 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 12369 FormatStyle AllowsMergedIf = getGoogleStyle(); 12370 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 12371 FormatStyle::SIS_WithoutElse; 12372 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 12373 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 12374 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 12375 EXPECT_EQ("if (true) return 42;", 12376 format("if (true)\nreturn 42;", AllowsMergedIf)); 12377 FormatStyle ShortMergedIf = AllowsMergedIf; 12378 ShortMergedIf.ColumnLimit = 25; 12379 verifyFormat("#define A \\\n" 12380 " if (true) return 42;", 12381 ShortMergedIf); 12382 verifyFormat("#define A \\\n" 12383 " f(); \\\n" 12384 " if (true)\n" 12385 "#define B", 12386 ShortMergedIf); 12387 verifyFormat("#define A \\\n" 12388 " f(); \\\n" 12389 " if (true)\n" 12390 "g();", 12391 ShortMergedIf); 12392 verifyFormat("{\n" 12393 "#ifdef A\n" 12394 " // Comment\n" 12395 " if (true) continue;\n" 12396 "#endif\n" 12397 " // Comment\n" 12398 " if (true) continue;\n" 12399 "}", 12400 ShortMergedIf); 12401 ShortMergedIf.ColumnLimit = 33; 12402 verifyFormat("#define A \\\n" 12403 " if constexpr (true) return 42;", 12404 ShortMergedIf); 12405 verifyFormat("#define A \\\n" 12406 " if CONSTEXPR (true) return 42;", 12407 ShortMergedIf); 12408 ShortMergedIf.ColumnLimit = 29; 12409 verifyFormat("#define A \\\n" 12410 " if (aaaaaaaaaa) return 1; \\\n" 12411 " return 2;", 12412 ShortMergedIf); 12413 ShortMergedIf.ColumnLimit = 28; 12414 verifyFormat("#define A \\\n" 12415 " if (aaaaaaaaaa) \\\n" 12416 " return 1; \\\n" 12417 " return 2;", 12418 ShortMergedIf); 12419 verifyFormat("#define A \\\n" 12420 " if constexpr (aaaaaaa) \\\n" 12421 " return 1; \\\n" 12422 " return 2;", 12423 ShortMergedIf); 12424 verifyFormat("#define A \\\n" 12425 " if CONSTEXPR (aaaaaaa) \\\n" 12426 " return 1; \\\n" 12427 " return 2;", 12428 ShortMergedIf); 12429 } 12430 12431 TEST_F(FormatTest, FormatStarDependingOnContext) { 12432 verifyFormat("void f(int *a);"); 12433 verifyFormat("void f() { f(fint * b); }"); 12434 verifyFormat("class A {\n void f(int *a);\n};"); 12435 verifyFormat("class A {\n int *a;\n};"); 12436 verifyFormat("namespace a {\n" 12437 "namespace b {\n" 12438 "class A {\n" 12439 " void f() {}\n" 12440 " int *a;\n" 12441 "};\n" 12442 "} // namespace b\n" 12443 "} // namespace a"); 12444 } 12445 12446 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 12447 verifyFormat("while"); 12448 verifyFormat("operator"); 12449 } 12450 12451 TEST_F(FormatTest, SkipsDeeplyNestedLines) { 12452 // This code would be painfully slow to format if we didn't skip it. 12453 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 12454 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12455 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12456 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12457 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12458 "A(1, 1)\n" 12459 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x 12460 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12461 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12462 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12463 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12464 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12465 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12466 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12467 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12468 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n"); 12469 // Deeply nested part is untouched, rest is formatted. 12470 EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n", 12471 format(std::string("int i;\n") + Code + "int j;\n", 12472 getLLVMStyle(), SC_ExpectIncomplete)); 12473 } 12474 12475 //===----------------------------------------------------------------------===// 12476 // Objective-C tests. 12477 //===----------------------------------------------------------------------===// 12478 12479 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 12480 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 12481 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 12482 format("-(NSUInteger)indexOfObject:(id)anObject;")); 12483 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 12484 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 12485 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 12486 format("-(NSInteger)Method3:(id)anObject;")); 12487 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 12488 format("-(NSInteger)Method4:(id)anObject;")); 12489 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 12490 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 12491 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 12492 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 12493 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 12494 "forAllCells:(BOOL)flag;", 12495 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 12496 "forAllCells:(BOOL)flag;")); 12497 12498 // Very long objectiveC method declaration. 12499 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 12500 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 12501 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 12502 " inRange:(NSRange)range\n" 12503 " outRange:(NSRange)out_range\n" 12504 " outRange1:(NSRange)out_range1\n" 12505 " outRange2:(NSRange)out_range2\n" 12506 " outRange3:(NSRange)out_range3\n" 12507 " outRange4:(NSRange)out_range4\n" 12508 " outRange5:(NSRange)out_range5\n" 12509 " outRange6:(NSRange)out_range6\n" 12510 " outRange7:(NSRange)out_range7\n" 12511 " outRange8:(NSRange)out_range8\n" 12512 " outRange9:(NSRange)out_range9;"); 12513 12514 // When the function name has to be wrapped. 12515 FormatStyle Style = getLLVMStyle(); 12516 // ObjC ignores IndentWrappedFunctionNames when wrapping methods 12517 // and always indents instead. 12518 Style.IndentWrappedFunctionNames = false; 12519 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 12520 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 12521 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 12522 "}", 12523 Style); 12524 Style.IndentWrappedFunctionNames = true; 12525 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 12526 " veryLooooooooooongName:(NSString)cccccccccccccc\n" 12527 " anotherName:(NSString)dddddddddddddd {\n" 12528 "}", 12529 Style); 12530 12531 verifyFormat("- (int)sum:(vector<int>)numbers;"); 12532 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 12533 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 12534 // protocol lists (but not for template classes): 12535 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 12536 12537 verifyFormat("- (int (*)())foo:(int (*)())f;"); 12538 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 12539 12540 // If there's no return type (very rare in practice!), LLVM and Google style 12541 // agree. 12542 verifyFormat("- foo;"); 12543 verifyFormat("- foo:(int)f;"); 12544 verifyGoogleFormat("- foo:(int)foo;"); 12545 } 12546 12547 TEST_F(FormatTest, BreaksStringLiterals) { 12548 EXPECT_EQ("\"some text \"\n" 12549 "\"other\";", 12550 format("\"some text other\";", getLLVMStyleWithColumns(12))); 12551 EXPECT_EQ("\"some text \"\n" 12552 "\"other\";", 12553 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 12554 EXPECT_EQ( 12555 "#define A \\\n" 12556 " \"some \" \\\n" 12557 " \"text \" \\\n" 12558 " \"other\";", 12559 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 12560 EXPECT_EQ( 12561 "#define A \\\n" 12562 " \"so \" \\\n" 12563 " \"text \" \\\n" 12564 " \"other\";", 12565 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 12566 12567 EXPECT_EQ("\"some text\"", 12568 format("\"some text\"", getLLVMStyleWithColumns(1))); 12569 EXPECT_EQ("\"some text\"", 12570 format("\"some text\"", getLLVMStyleWithColumns(11))); 12571 EXPECT_EQ("\"some \"\n" 12572 "\"text\"", 12573 format("\"some text\"", getLLVMStyleWithColumns(10))); 12574 EXPECT_EQ("\"some \"\n" 12575 "\"text\"", 12576 format("\"some text\"", getLLVMStyleWithColumns(7))); 12577 EXPECT_EQ("\"some\"\n" 12578 "\" tex\"\n" 12579 "\"t\"", 12580 format("\"some text\"", getLLVMStyleWithColumns(6))); 12581 EXPECT_EQ("\"some\"\n" 12582 "\" tex\"\n" 12583 "\" and\"", 12584 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 12585 EXPECT_EQ("\"some\"\n" 12586 "\"/tex\"\n" 12587 "\"/and\"", 12588 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 12589 12590 EXPECT_EQ("variable =\n" 12591 " \"long string \"\n" 12592 " \"literal\";", 12593 format("variable = \"long string literal\";", 12594 getLLVMStyleWithColumns(20))); 12595 12596 EXPECT_EQ("variable = f(\n" 12597 " \"long string \"\n" 12598 " \"literal\",\n" 12599 " short,\n" 12600 " loooooooooooooooooooong);", 12601 format("variable = f(\"long string literal\", short, " 12602 "loooooooooooooooooooong);", 12603 getLLVMStyleWithColumns(20))); 12604 12605 EXPECT_EQ( 12606 "f(g(\"long string \"\n" 12607 " \"literal\"),\n" 12608 " b);", 12609 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 12610 EXPECT_EQ("f(g(\"long string \"\n" 12611 " \"literal\",\n" 12612 " a),\n" 12613 " b);", 12614 format("f(g(\"long string literal\", a), b);", 12615 getLLVMStyleWithColumns(20))); 12616 EXPECT_EQ( 12617 "f(\"one two\".split(\n" 12618 " variable));", 12619 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 12620 EXPECT_EQ("f(\"one two three four five six \"\n" 12621 " \"seven\".split(\n" 12622 " really_looooong_variable));", 12623 format("f(\"one two three four five six seven\"." 12624 "split(really_looooong_variable));", 12625 getLLVMStyleWithColumns(33))); 12626 12627 EXPECT_EQ("f(\"some \"\n" 12628 " \"text\",\n" 12629 " other);", 12630 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 12631 12632 // Only break as a last resort. 12633 verifyFormat( 12634 "aaaaaaaaaaaaaaaaaaaa(\n" 12635 " aaaaaaaaaaaaaaaaaaaa,\n" 12636 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 12637 12638 EXPECT_EQ("\"splitmea\"\n" 12639 "\"trandomp\"\n" 12640 "\"oint\"", 12641 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 12642 12643 EXPECT_EQ("\"split/\"\n" 12644 "\"pathat/\"\n" 12645 "\"slashes\"", 12646 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 12647 12648 EXPECT_EQ("\"split/\"\n" 12649 "\"pathat/\"\n" 12650 "\"slashes\"", 12651 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 12652 EXPECT_EQ("\"split at \"\n" 12653 "\"spaces/at/\"\n" 12654 "\"slashes.at.any$\"\n" 12655 "\"non-alphanumeric%\"\n" 12656 "\"1111111111characte\"\n" 12657 "\"rs\"", 12658 format("\"split at " 12659 "spaces/at/" 12660 "slashes.at." 12661 "any$non-" 12662 "alphanumeric%" 12663 "1111111111characte" 12664 "rs\"", 12665 getLLVMStyleWithColumns(20))); 12666 12667 // Verify that splitting the strings understands 12668 // Style::AlwaysBreakBeforeMultilineStrings. 12669 EXPECT_EQ("aaaaaaaaaaaa(\n" 12670 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 12671 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 12672 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 12673 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 12674 "aaaaaaaaaaaaaaaaaaaaaa\");", 12675 getGoogleStyle())); 12676 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 12677 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 12678 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 12679 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 12680 "aaaaaaaaaaaaaaaaaaaaaa\";", 12681 getGoogleStyle())); 12682 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 12683 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 12684 format("llvm::outs() << " 12685 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 12686 "aaaaaaaaaaaaaaaaaaa\";")); 12687 EXPECT_EQ("ffff(\n" 12688 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 12689 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 12690 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 12691 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 12692 getGoogleStyle())); 12693 12694 FormatStyle Style = getLLVMStyleWithColumns(12); 12695 Style.BreakStringLiterals = false; 12696 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 12697 12698 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 12699 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 12700 EXPECT_EQ("#define A \\\n" 12701 " \"some \" \\\n" 12702 " \"text \" \\\n" 12703 " \"other\";", 12704 format("#define A \"some text other\";", AlignLeft)); 12705 } 12706 12707 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) { 12708 EXPECT_EQ("C a = \"some more \"\n" 12709 " \"text\";", 12710 format("C a = \"some more text\";", getLLVMStyleWithColumns(18))); 12711 } 12712 12713 TEST_F(FormatTest, FullyRemoveEmptyLines) { 12714 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 12715 NoEmptyLines.MaxEmptyLinesToKeep = 0; 12716 EXPECT_EQ("int i = a(b());", 12717 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 12718 } 12719 12720 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 12721 EXPECT_EQ( 12722 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 12723 "(\n" 12724 " \"x\t\");", 12725 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 12726 "aaaaaaa(" 12727 "\"x\t\");")); 12728 } 12729 12730 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 12731 EXPECT_EQ( 12732 "u8\"utf8 string \"\n" 12733 "u8\"literal\";", 12734 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 12735 EXPECT_EQ( 12736 "u\"utf16 string \"\n" 12737 "u\"literal\";", 12738 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 12739 EXPECT_EQ( 12740 "U\"utf32 string \"\n" 12741 "U\"literal\";", 12742 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 12743 EXPECT_EQ("L\"wide string \"\n" 12744 "L\"literal\";", 12745 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 12746 EXPECT_EQ("@\"NSString \"\n" 12747 "@\"literal\";", 12748 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 12749 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26)); 12750 12751 // This input makes clang-format try to split the incomplete unicode escape 12752 // sequence, which used to lead to a crasher. 12753 verifyNoCrash( 12754 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 12755 getLLVMStyleWithColumns(60)); 12756 } 12757 12758 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 12759 FormatStyle Style = getGoogleStyleWithColumns(15); 12760 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 12761 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 12762 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 12763 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 12764 EXPECT_EQ("u8R\"x(raw literal)x\";", 12765 format("u8R\"x(raw literal)x\";", Style)); 12766 } 12767 12768 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 12769 FormatStyle Style = getLLVMStyleWithColumns(20); 12770 EXPECT_EQ( 12771 "_T(\"aaaaaaaaaaaaaa\")\n" 12772 "_T(\"aaaaaaaaaaaaaa\")\n" 12773 "_T(\"aaaaaaaaaaaa\")", 12774 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 12775 EXPECT_EQ("f(x,\n" 12776 " _T(\"aaaaaaaaaaaa\")\n" 12777 " _T(\"aaa\"),\n" 12778 " z);", 12779 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 12780 12781 // FIXME: Handle embedded spaces in one iteration. 12782 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 12783 // "_T(\"aaaaaaaaaaaaa\")\n" 12784 // "_T(\"aaaaaaaaaaaaa\")\n" 12785 // "_T(\"a\")", 12786 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 12787 // getLLVMStyleWithColumns(20))); 12788 EXPECT_EQ( 12789 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 12790 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 12791 EXPECT_EQ("f(\n" 12792 "#if !TEST\n" 12793 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 12794 "#endif\n" 12795 ");", 12796 format("f(\n" 12797 "#if !TEST\n" 12798 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 12799 "#endif\n" 12800 ");")); 12801 EXPECT_EQ("f(\n" 12802 "\n" 12803 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 12804 format("f(\n" 12805 "\n" 12806 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 12807 } 12808 12809 TEST_F(FormatTest, BreaksStringLiteralOperands) { 12810 // In a function call with two operands, the second can be broken with no line 12811 // break before it. 12812 EXPECT_EQ( 12813 "func(a, \"long long \"\n" 12814 " \"long long\");", 12815 format("func(a, \"long long long long\");", getLLVMStyleWithColumns(24))); 12816 // In a function call with three operands, the second must be broken with a 12817 // line break before it. 12818 EXPECT_EQ("func(a,\n" 12819 " \"long long long \"\n" 12820 " \"long\",\n" 12821 " c);", 12822 format("func(a, \"long long long long\", c);", 12823 getLLVMStyleWithColumns(24))); 12824 // In a function call with three operands, the third must be broken with a 12825 // line break before it. 12826 EXPECT_EQ("func(a, b,\n" 12827 " \"long long long \"\n" 12828 " \"long\");", 12829 format("func(a, b, \"long long long long\");", 12830 getLLVMStyleWithColumns(24))); 12831 // In a function call with three operands, both the second and the third must 12832 // be broken with a line break before them. 12833 EXPECT_EQ("func(a,\n" 12834 " \"long long long \"\n" 12835 " \"long\",\n" 12836 " \"long long long \"\n" 12837 " \"long\");", 12838 format("func(a, \"long long long long\", \"long long long long\");", 12839 getLLVMStyleWithColumns(24))); 12840 // In a chain of << with two operands, the second can be broken with no line 12841 // break before it. 12842 EXPECT_EQ("a << \"line line \"\n" 12843 " \"line\";", 12844 format("a << \"line line line\";", getLLVMStyleWithColumns(20))); 12845 // In a chain of << with three operands, the second can be broken with no line 12846 // break before it. 12847 EXPECT_EQ( 12848 "abcde << \"line \"\n" 12849 " \"line line\"\n" 12850 " << c;", 12851 format("abcde << \"line line line\" << c;", getLLVMStyleWithColumns(20))); 12852 // In a chain of << with three operands, the third must be broken with a line 12853 // break before it. 12854 EXPECT_EQ( 12855 "a << b\n" 12856 " << \"line line \"\n" 12857 " \"line\";", 12858 format("a << b << \"line line line\";", getLLVMStyleWithColumns(20))); 12859 // In a chain of << with three operands, the second can be broken with no line 12860 // break before it and the third must be broken with a line break before it. 12861 EXPECT_EQ("abcd << \"line line \"\n" 12862 " \"line\"\n" 12863 " << \"line line \"\n" 12864 " \"line\";", 12865 format("abcd << \"line line line\" << \"line line line\";", 12866 getLLVMStyleWithColumns(20))); 12867 // In a chain of binary operators with two operands, the second can be broken 12868 // with no line break before it. 12869 EXPECT_EQ( 12870 "abcd + \"line line \"\n" 12871 " \"line line\";", 12872 format("abcd + \"line line line line\";", getLLVMStyleWithColumns(20))); 12873 // In a chain of binary operators with three operands, the second must be 12874 // broken with a line break before it. 12875 EXPECT_EQ("abcd +\n" 12876 " \"line line \"\n" 12877 " \"line line\" +\n" 12878 " e;", 12879 format("abcd + \"line line line line\" + e;", 12880 getLLVMStyleWithColumns(20))); 12881 // In a function call with two operands, with AlignAfterOpenBracket enabled, 12882 // the first must be broken with a line break before it. 12883 FormatStyle Style = getLLVMStyleWithColumns(25); 12884 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 12885 EXPECT_EQ("someFunction(\n" 12886 " \"long long long \"\n" 12887 " \"long\",\n" 12888 " a);", 12889 format("someFunction(\"long long long long\", a);", Style)); 12890 } 12891 12892 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 12893 EXPECT_EQ( 12894 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 12895 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 12896 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 12897 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 12898 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 12899 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 12900 } 12901 12902 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 12903 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 12904 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 12905 EXPECT_EQ("fffffffffff(g(R\"x(\n" 12906 "multiline raw string literal xxxxxxxxxxxxxx\n" 12907 ")x\",\n" 12908 " a),\n" 12909 " b);", 12910 format("fffffffffff(g(R\"x(\n" 12911 "multiline raw string literal xxxxxxxxxxxxxx\n" 12912 ")x\", a), b);", 12913 getGoogleStyleWithColumns(20))); 12914 EXPECT_EQ("fffffffffff(\n" 12915 " g(R\"x(qqq\n" 12916 "multiline raw string literal xxxxxxxxxxxxxx\n" 12917 ")x\",\n" 12918 " a),\n" 12919 " b);", 12920 format("fffffffffff(g(R\"x(qqq\n" 12921 "multiline raw string literal xxxxxxxxxxxxxx\n" 12922 ")x\", a), b);", 12923 getGoogleStyleWithColumns(20))); 12924 12925 EXPECT_EQ("fffffffffff(R\"x(\n" 12926 "multiline raw string literal xxxxxxxxxxxxxx\n" 12927 ")x\");", 12928 format("fffffffffff(R\"x(\n" 12929 "multiline raw string literal xxxxxxxxxxxxxx\n" 12930 ")x\");", 12931 getGoogleStyleWithColumns(20))); 12932 EXPECT_EQ("fffffffffff(R\"x(\n" 12933 "multiline raw string literal xxxxxxxxxxxxxx\n" 12934 ")x\" + bbbbbb);", 12935 format("fffffffffff(R\"x(\n" 12936 "multiline raw string literal xxxxxxxxxxxxxx\n" 12937 ")x\" + bbbbbb);", 12938 getGoogleStyleWithColumns(20))); 12939 EXPECT_EQ("fffffffffff(\n" 12940 " R\"x(\n" 12941 "multiline raw string literal xxxxxxxxxxxxxx\n" 12942 ")x\" +\n" 12943 " bbbbbb);", 12944 format("fffffffffff(\n" 12945 " R\"x(\n" 12946 "multiline raw string literal xxxxxxxxxxxxxx\n" 12947 ")x\" + bbbbbb);", 12948 getGoogleStyleWithColumns(20))); 12949 EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);", 12950 format("fffffffffff(\n" 12951 " R\"(single line raw string)\" + bbbbbb);")); 12952 } 12953 12954 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 12955 verifyFormat("string a = \"unterminated;"); 12956 EXPECT_EQ("function(\"unterminated,\n" 12957 " OtherParameter);", 12958 format("function( \"unterminated,\n" 12959 " OtherParameter);")); 12960 } 12961 12962 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 12963 FormatStyle Style = getLLVMStyle(); 12964 Style.Standard = FormatStyle::LS_Cpp03; 12965 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 12966 format("#define x(_a) printf(\"foo\"_a);", Style)); 12967 } 12968 12969 TEST_F(FormatTest, CppLexVersion) { 12970 FormatStyle Style = getLLVMStyle(); 12971 // Formatting of x * y differs if x is a type. 12972 verifyFormat("void foo() { MACRO(a * b); }", Style); 12973 verifyFormat("void foo() { MACRO(int *b); }", Style); 12974 12975 // LLVM style uses latest lexer. 12976 verifyFormat("void foo() { MACRO(char8_t *b); }", Style); 12977 Style.Standard = FormatStyle::LS_Cpp17; 12978 // But in c++17, char8_t isn't a keyword. 12979 verifyFormat("void foo() { MACRO(char8_t * b); }", Style); 12980 } 12981 12982 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 12983 12984 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 12985 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 12986 " \"ddeeefff\");", 12987 format("someFunction(\"aaabbbcccdddeeefff\");", 12988 getLLVMStyleWithColumns(25))); 12989 EXPECT_EQ("someFunction1234567890(\n" 12990 " \"aaabbbcccdddeeefff\");", 12991 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 12992 getLLVMStyleWithColumns(26))); 12993 EXPECT_EQ("someFunction1234567890(\n" 12994 " \"aaabbbcccdddeeeff\"\n" 12995 " \"f\");", 12996 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 12997 getLLVMStyleWithColumns(25))); 12998 EXPECT_EQ("someFunction1234567890(\n" 12999 " \"aaabbbcccdddeeeff\"\n" 13000 " \"f\");", 13001 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 13002 getLLVMStyleWithColumns(24))); 13003 EXPECT_EQ("someFunction(\n" 13004 " \"aaabbbcc ddde \"\n" 13005 " \"efff\");", 13006 format("someFunction(\"aaabbbcc ddde efff\");", 13007 getLLVMStyleWithColumns(25))); 13008 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 13009 " \"ddeeefff\");", 13010 format("someFunction(\"aaabbbccc ddeeefff\");", 13011 getLLVMStyleWithColumns(25))); 13012 EXPECT_EQ("someFunction1234567890(\n" 13013 " \"aaabb \"\n" 13014 " \"cccdddeeefff\");", 13015 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 13016 getLLVMStyleWithColumns(25))); 13017 EXPECT_EQ("#define A \\\n" 13018 " string s = \\\n" 13019 " \"123456789\" \\\n" 13020 " \"0\"; \\\n" 13021 " int i;", 13022 format("#define A string s = \"1234567890\"; int i;", 13023 getLLVMStyleWithColumns(20))); 13024 EXPECT_EQ("someFunction(\n" 13025 " \"aaabbbcc \"\n" 13026 " \"dddeeefff\");", 13027 format("someFunction(\"aaabbbcc dddeeefff\");", 13028 getLLVMStyleWithColumns(25))); 13029 } 13030 13031 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 13032 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 13033 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 13034 EXPECT_EQ("\"test\"\n" 13035 "\"\\n\"", 13036 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 13037 EXPECT_EQ("\"tes\\\\\"\n" 13038 "\"n\"", 13039 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 13040 EXPECT_EQ("\"\\\\\\\\\"\n" 13041 "\"\\n\"", 13042 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 13043 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 13044 EXPECT_EQ("\"\\uff01\"\n" 13045 "\"test\"", 13046 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 13047 EXPECT_EQ("\"\\Uff01ff02\"", 13048 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 13049 EXPECT_EQ("\"\\x000000000001\"\n" 13050 "\"next\"", 13051 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 13052 EXPECT_EQ("\"\\x000000000001next\"", 13053 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 13054 EXPECT_EQ("\"\\x000000000001\"", 13055 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 13056 EXPECT_EQ("\"test\"\n" 13057 "\"\\000000\"\n" 13058 "\"000001\"", 13059 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 13060 EXPECT_EQ("\"test\\000\"\n" 13061 "\"00000000\"\n" 13062 "\"1\"", 13063 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 13064 } 13065 13066 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 13067 verifyFormat("void f() {\n" 13068 " return g() {}\n" 13069 " void h() {}"); 13070 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 13071 "g();\n" 13072 "}"); 13073 } 13074 13075 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 13076 verifyFormat( 13077 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 13078 } 13079 13080 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 13081 verifyFormat("class X {\n" 13082 " void f() {\n" 13083 " }\n" 13084 "};", 13085 getLLVMStyleWithColumns(12)); 13086 } 13087 13088 TEST_F(FormatTest, ConfigurableIndentWidth) { 13089 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 13090 EightIndent.IndentWidth = 8; 13091 EightIndent.ContinuationIndentWidth = 8; 13092 verifyFormat("void f() {\n" 13093 " someFunction();\n" 13094 " if (true) {\n" 13095 " f();\n" 13096 " }\n" 13097 "}", 13098 EightIndent); 13099 verifyFormat("class X {\n" 13100 " void f() {\n" 13101 " }\n" 13102 "};", 13103 EightIndent); 13104 verifyFormat("int x[] = {\n" 13105 " call(),\n" 13106 " call()};", 13107 EightIndent); 13108 } 13109 13110 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 13111 verifyFormat("double\n" 13112 "f();", 13113 getLLVMStyleWithColumns(8)); 13114 } 13115 13116 TEST_F(FormatTest, ConfigurableUseOfTab) { 13117 FormatStyle Tab = getLLVMStyleWithColumns(42); 13118 Tab.IndentWidth = 8; 13119 Tab.UseTab = FormatStyle::UT_Always; 13120 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left; 13121 13122 EXPECT_EQ("if (aaaaaaaa && // q\n" 13123 " bb)\t\t// w\n" 13124 "\t;", 13125 format("if (aaaaaaaa &&// q\n" 13126 "bb)// w\n" 13127 ";", 13128 Tab)); 13129 EXPECT_EQ("if (aaa && bbb) // w\n" 13130 "\t;", 13131 format("if(aaa&&bbb)// w\n" 13132 ";", 13133 Tab)); 13134 13135 verifyFormat("class X {\n" 13136 "\tvoid f() {\n" 13137 "\t\tsomeFunction(parameter1,\n" 13138 "\t\t\t parameter2);\n" 13139 "\t}\n" 13140 "};", 13141 Tab); 13142 verifyFormat("#define A \\\n" 13143 "\tvoid f() { \\\n" 13144 "\t\tsomeFunction( \\\n" 13145 "\t\t parameter1, \\\n" 13146 "\t\t parameter2); \\\n" 13147 "\t}", 13148 Tab); 13149 verifyFormat("int a;\t // x\n" 13150 "int bbbbbbbb; // x\n", 13151 Tab); 13152 13153 Tab.TabWidth = 4; 13154 Tab.IndentWidth = 8; 13155 verifyFormat("class TabWidth4Indent8 {\n" 13156 "\t\tvoid f() {\n" 13157 "\t\t\t\tsomeFunction(parameter1,\n" 13158 "\t\t\t\t\t\t\t parameter2);\n" 13159 "\t\t}\n" 13160 "};", 13161 Tab); 13162 13163 Tab.TabWidth = 4; 13164 Tab.IndentWidth = 4; 13165 verifyFormat("class TabWidth4Indent4 {\n" 13166 "\tvoid f() {\n" 13167 "\t\tsomeFunction(parameter1,\n" 13168 "\t\t\t\t\t parameter2);\n" 13169 "\t}\n" 13170 "};", 13171 Tab); 13172 13173 Tab.TabWidth = 8; 13174 Tab.IndentWidth = 4; 13175 verifyFormat("class TabWidth8Indent4 {\n" 13176 " void f() {\n" 13177 "\tsomeFunction(parameter1,\n" 13178 "\t\t parameter2);\n" 13179 " }\n" 13180 "};", 13181 Tab); 13182 13183 Tab.TabWidth = 8; 13184 Tab.IndentWidth = 8; 13185 EXPECT_EQ("/*\n" 13186 "\t a\t\tcomment\n" 13187 "\t in multiple lines\n" 13188 " */", 13189 format(" /*\t \t \n" 13190 " \t \t a\t\tcomment\t \t\n" 13191 " \t \t in multiple lines\t\n" 13192 " \t */", 13193 Tab)); 13194 13195 Tab.UseTab = FormatStyle::UT_ForIndentation; 13196 verifyFormat("{\n" 13197 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13198 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13199 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13200 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13201 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13202 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13203 "};", 13204 Tab); 13205 verifyFormat("enum AA {\n" 13206 "\ta1, // Force multiple lines\n" 13207 "\ta2,\n" 13208 "\ta3\n" 13209 "};", 13210 Tab); 13211 EXPECT_EQ("if (aaaaaaaa && // q\n" 13212 " bb) // w\n" 13213 "\t;", 13214 format("if (aaaaaaaa &&// q\n" 13215 "bb)// w\n" 13216 ";", 13217 Tab)); 13218 verifyFormat("class X {\n" 13219 "\tvoid f() {\n" 13220 "\t\tsomeFunction(parameter1,\n" 13221 "\t\t parameter2);\n" 13222 "\t}\n" 13223 "};", 13224 Tab); 13225 verifyFormat("{\n" 13226 "\tQ(\n" 13227 "\t {\n" 13228 "\t\t int a;\n" 13229 "\t\t someFunction(aaaaaaaa,\n" 13230 "\t\t bbbbbbb);\n" 13231 "\t },\n" 13232 "\t p);\n" 13233 "}", 13234 Tab); 13235 EXPECT_EQ("{\n" 13236 "\t/* aaaa\n" 13237 "\t bbbb */\n" 13238 "}", 13239 format("{\n" 13240 "/* aaaa\n" 13241 " bbbb */\n" 13242 "}", 13243 Tab)); 13244 EXPECT_EQ("{\n" 13245 "\t/*\n" 13246 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13247 "\t bbbbbbbbbbbbb\n" 13248 "\t*/\n" 13249 "}", 13250 format("{\n" 13251 "/*\n" 13252 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13253 "*/\n" 13254 "}", 13255 Tab)); 13256 EXPECT_EQ("{\n" 13257 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13258 "\t// bbbbbbbbbbbbb\n" 13259 "}", 13260 format("{\n" 13261 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13262 "}", 13263 Tab)); 13264 EXPECT_EQ("{\n" 13265 "\t/*\n" 13266 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13267 "\t bbbbbbbbbbbbb\n" 13268 "\t*/\n" 13269 "}", 13270 format("{\n" 13271 "\t/*\n" 13272 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13273 "\t*/\n" 13274 "}", 13275 Tab)); 13276 EXPECT_EQ("{\n" 13277 "\t/*\n" 13278 "\n" 13279 "\t*/\n" 13280 "}", 13281 format("{\n" 13282 "\t/*\n" 13283 "\n" 13284 "\t*/\n" 13285 "}", 13286 Tab)); 13287 EXPECT_EQ("{\n" 13288 "\t/*\n" 13289 " asdf\n" 13290 "\t*/\n" 13291 "}", 13292 format("{\n" 13293 "\t/*\n" 13294 " asdf\n" 13295 "\t*/\n" 13296 "}", 13297 Tab)); 13298 13299 Tab.UseTab = FormatStyle::UT_Never; 13300 EXPECT_EQ("/*\n" 13301 " a\t\tcomment\n" 13302 " in multiple lines\n" 13303 " */", 13304 format(" /*\t \t \n" 13305 " \t \t a\t\tcomment\t \t\n" 13306 " \t \t in multiple lines\t\n" 13307 " \t */", 13308 Tab)); 13309 EXPECT_EQ("/* some\n" 13310 " comment */", 13311 format(" \t \t /* some\n" 13312 " \t \t comment */", 13313 Tab)); 13314 EXPECT_EQ("int a; /* some\n" 13315 " comment */", 13316 format(" \t \t int a; /* some\n" 13317 " \t \t comment */", 13318 Tab)); 13319 13320 EXPECT_EQ("int a; /* some\n" 13321 "comment */", 13322 format(" \t \t int\ta; /* some\n" 13323 " \t \t comment */", 13324 Tab)); 13325 EXPECT_EQ("f(\"\t\t\"); /* some\n" 13326 " comment */", 13327 format(" \t \t f(\"\t\t\"); /* some\n" 13328 " \t \t comment */", 13329 Tab)); 13330 EXPECT_EQ("{\n" 13331 " /*\n" 13332 " * Comment\n" 13333 " */\n" 13334 " int i;\n" 13335 "}", 13336 format("{\n" 13337 "\t/*\n" 13338 "\t * Comment\n" 13339 "\t */\n" 13340 "\t int i;\n" 13341 "}", 13342 Tab)); 13343 13344 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 13345 Tab.TabWidth = 8; 13346 Tab.IndentWidth = 8; 13347 EXPECT_EQ("if (aaaaaaaa && // q\n" 13348 " bb) // w\n" 13349 "\t;", 13350 format("if (aaaaaaaa &&// q\n" 13351 "bb)// w\n" 13352 ";", 13353 Tab)); 13354 EXPECT_EQ("if (aaa && bbb) // w\n" 13355 "\t;", 13356 format("if(aaa&&bbb)// w\n" 13357 ";", 13358 Tab)); 13359 verifyFormat("class X {\n" 13360 "\tvoid f() {\n" 13361 "\t\tsomeFunction(parameter1,\n" 13362 "\t\t\t parameter2);\n" 13363 "\t}\n" 13364 "};", 13365 Tab); 13366 verifyFormat("#define A \\\n" 13367 "\tvoid f() { \\\n" 13368 "\t\tsomeFunction( \\\n" 13369 "\t\t parameter1, \\\n" 13370 "\t\t parameter2); \\\n" 13371 "\t}", 13372 Tab); 13373 Tab.TabWidth = 4; 13374 Tab.IndentWidth = 8; 13375 verifyFormat("class TabWidth4Indent8 {\n" 13376 "\t\tvoid f() {\n" 13377 "\t\t\t\tsomeFunction(parameter1,\n" 13378 "\t\t\t\t\t\t\t parameter2);\n" 13379 "\t\t}\n" 13380 "};", 13381 Tab); 13382 Tab.TabWidth = 4; 13383 Tab.IndentWidth = 4; 13384 verifyFormat("class TabWidth4Indent4 {\n" 13385 "\tvoid f() {\n" 13386 "\t\tsomeFunction(parameter1,\n" 13387 "\t\t\t\t\t parameter2);\n" 13388 "\t}\n" 13389 "};", 13390 Tab); 13391 Tab.TabWidth = 8; 13392 Tab.IndentWidth = 4; 13393 verifyFormat("class TabWidth8Indent4 {\n" 13394 " void f() {\n" 13395 "\tsomeFunction(parameter1,\n" 13396 "\t\t parameter2);\n" 13397 " }\n" 13398 "};", 13399 Tab); 13400 Tab.TabWidth = 8; 13401 Tab.IndentWidth = 8; 13402 EXPECT_EQ("/*\n" 13403 "\t a\t\tcomment\n" 13404 "\t in multiple lines\n" 13405 " */", 13406 format(" /*\t \t \n" 13407 " \t \t a\t\tcomment\t \t\n" 13408 " \t \t in multiple lines\t\n" 13409 " \t */", 13410 Tab)); 13411 verifyFormat("{\n" 13412 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13413 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13414 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13415 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13416 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13417 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13418 "};", 13419 Tab); 13420 verifyFormat("enum AA {\n" 13421 "\ta1, // Force multiple lines\n" 13422 "\ta2,\n" 13423 "\ta3\n" 13424 "};", 13425 Tab); 13426 EXPECT_EQ("if (aaaaaaaa && // q\n" 13427 " bb) // w\n" 13428 "\t;", 13429 format("if (aaaaaaaa &&// q\n" 13430 "bb)// w\n" 13431 ";", 13432 Tab)); 13433 verifyFormat("class X {\n" 13434 "\tvoid f() {\n" 13435 "\t\tsomeFunction(parameter1,\n" 13436 "\t\t\t parameter2);\n" 13437 "\t}\n" 13438 "};", 13439 Tab); 13440 verifyFormat("{\n" 13441 "\tQ(\n" 13442 "\t {\n" 13443 "\t\t int a;\n" 13444 "\t\t someFunction(aaaaaaaa,\n" 13445 "\t\t\t\t bbbbbbb);\n" 13446 "\t },\n" 13447 "\t p);\n" 13448 "}", 13449 Tab); 13450 EXPECT_EQ("{\n" 13451 "\t/* aaaa\n" 13452 "\t bbbb */\n" 13453 "}", 13454 format("{\n" 13455 "/* aaaa\n" 13456 " bbbb */\n" 13457 "}", 13458 Tab)); 13459 EXPECT_EQ("{\n" 13460 "\t/*\n" 13461 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13462 "\t bbbbbbbbbbbbb\n" 13463 "\t*/\n" 13464 "}", 13465 format("{\n" 13466 "/*\n" 13467 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13468 "*/\n" 13469 "}", 13470 Tab)); 13471 EXPECT_EQ("{\n" 13472 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13473 "\t// bbbbbbbbbbbbb\n" 13474 "}", 13475 format("{\n" 13476 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13477 "}", 13478 Tab)); 13479 EXPECT_EQ("{\n" 13480 "\t/*\n" 13481 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13482 "\t bbbbbbbbbbbbb\n" 13483 "\t*/\n" 13484 "}", 13485 format("{\n" 13486 "\t/*\n" 13487 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13488 "\t*/\n" 13489 "}", 13490 Tab)); 13491 EXPECT_EQ("{\n" 13492 "\t/*\n" 13493 "\n" 13494 "\t*/\n" 13495 "}", 13496 format("{\n" 13497 "\t/*\n" 13498 "\n" 13499 "\t*/\n" 13500 "}", 13501 Tab)); 13502 EXPECT_EQ("{\n" 13503 "\t/*\n" 13504 " asdf\n" 13505 "\t*/\n" 13506 "}", 13507 format("{\n" 13508 "\t/*\n" 13509 " asdf\n" 13510 "\t*/\n" 13511 "}", 13512 Tab)); 13513 EXPECT_EQ("/* some\n" 13514 " comment */", 13515 format(" \t \t /* some\n" 13516 " \t \t comment */", 13517 Tab)); 13518 EXPECT_EQ("int a; /* some\n" 13519 " comment */", 13520 format(" \t \t int a; /* some\n" 13521 " \t \t comment */", 13522 Tab)); 13523 EXPECT_EQ("int a; /* some\n" 13524 "comment */", 13525 format(" \t \t int\ta; /* some\n" 13526 " \t \t comment */", 13527 Tab)); 13528 EXPECT_EQ("f(\"\t\t\"); /* some\n" 13529 " comment */", 13530 format(" \t \t f(\"\t\t\"); /* some\n" 13531 " \t \t comment */", 13532 Tab)); 13533 EXPECT_EQ("{\n" 13534 "\t/*\n" 13535 "\t * Comment\n" 13536 "\t */\n" 13537 "\tint i;\n" 13538 "}", 13539 format("{\n" 13540 "\t/*\n" 13541 "\t * Comment\n" 13542 "\t */\n" 13543 "\t int i;\n" 13544 "}", 13545 Tab)); 13546 Tab.TabWidth = 2; 13547 Tab.IndentWidth = 2; 13548 EXPECT_EQ("{\n" 13549 "\t/* aaaa\n" 13550 "\t\t bbbb */\n" 13551 "}", 13552 format("{\n" 13553 "/* aaaa\n" 13554 "\t bbbb */\n" 13555 "}", 13556 Tab)); 13557 EXPECT_EQ("{\n" 13558 "\t/*\n" 13559 "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13560 "\t\tbbbbbbbbbbbbb\n" 13561 "\t*/\n" 13562 "}", 13563 format("{\n" 13564 "/*\n" 13565 "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13566 "*/\n" 13567 "}", 13568 Tab)); 13569 Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 13570 Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 13571 Tab.TabWidth = 4; 13572 Tab.IndentWidth = 4; 13573 verifyFormat("class Assign {\n" 13574 "\tvoid f() {\n" 13575 "\t\tint x = 123;\n" 13576 "\t\tint random = 4;\n" 13577 "\t\tstd::string alphabet =\n" 13578 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 13579 "\t}\n" 13580 "};", 13581 Tab); 13582 13583 Tab.UseTab = FormatStyle::UT_AlignWithSpaces; 13584 Tab.TabWidth = 8; 13585 Tab.IndentWidth = 8; 13586 EXPECT_EQ("if (aaaaaaaa && // q\n" 13587 " bb) // w\n" 13588 "\t;", 13589 format("if (aaaaaaaa &&// q\n" 13590 "bb)// w\n" 13591 ";", 13592 Tab)); 13593 EXPECT_EQ("if (aaa && bbb) // w\n" 13594 "\t;", 13595 format("if(aaa&&bbb)// w\n" 13596 ";", 13597 Tab)); 13598 verifyFormat("class X {\n" 13599 "\tvoid f() {\n" 13600 "\t\tsomeFunction(parameter1,\n" 13601 "\t\t parameter2);\n" 13602 "\t}\n" 13603 "};", 13604 Tab); 13605 verifyFormat("#define A \\\n" 13606 "\tvoid f() { \\\n" 13607 "\t\tsomeFunction( \\\n" 13608 "\t\t parameter1, \\\n" 13609 "\t\t parameter2); \\\n" 13610 "\t}", 13611 Tab); 13612 Tab.TabWidth = 4; 13613 Tab.IndentWidth = 8; 13614 verifyFormat("class TabWidth4Indent8 {\n" 13615 "\t\tvoid f() {\n" 13616 "\t\t\t\tsomeFunction(parameter1,\n" 13617 "\t\t\t\t parameter2);\n" 13618 "\t\t}\n" 13619 "};", 13620 Tab); 13621 Tab.TabWidth = 4; 13622 Tab.IndentWidth = 4; 13623 verifyFormat("class TabWidth4Indent4 {\n" 13624 "\tvoid f() {\n" 13625 "\t\tsomeFunction(parameter1,\n" 13626 "\t\t parameter2);\n" 13627 "\t}\n" 13628 "};", 13629 Tab); 13630 Tab.TabWidth = 8; 13631 Tab.IndentWidth = 4; 13632 verifyFormat("class TabWidth8Indent4 {\n" 13633 " void f() {\n" 13634 "\tsomeFunction(parameter1,\n" 13635 "\t parameter2);\n" 13636 " }\n" 13637 "};", 13638 Tab); 13639 Tab.TabWidth = 8; 13640 Tab.IndentWidth = 8; 13641 EXPECT_EQ("/*\n" 13642 " a\t\tcomment\n" 13643 " in multiple lines\n" 13644 " */", 13645 format(" /*\t \t \n" 13646 " \t \t a\t\tcomment\t \t\n" 13647 " \t \t in multiple lines\t\n" 13648 " \t */", 13649 Tab)); 13650 verifyFormat("{\n" 13651 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13652 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13653 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13654 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13655 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13656 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13657 "};", 13658 Tab); 13659 verifyFormat("enum AA {\n" 13660 "\ta1, // Force multiple lines\n" 13661 "\ta2,\n" 13662 "\ta3\n" 13663 "};", 13664 Tab); 13665 EXPECT_EQ("if (aaaaaaaa && // q\n" 13666 " bb) // w\n" 13667 "\t;", 13668 format("if (aaaaaaaa &&// q\n" 13669 "bb)// w\n" 13670 ";", 13671 Tab)); 13672 verifyFormat("class X {\n" 13673 "\tvoid f() {\n" 13674 "\t\tsomeFunction(parameter1,\n" 13675 "\t\t parameter2);\n" 13676 "\t}\n" 13677 "};", 13678 Tab); 13679 verifyFormat("{\n" 13680 "\tQ(\n" 13681 "\t {\n" 13682 "\t\t int a;\n" 13683 "\t\t someFunction(aaaaaaaa,\n" 13684 "\t\t bbbbbbb);\n" 13685 "\t },\n" 13686 "\t p);\n" 13687 "}", 13688 Tab); 13689 EXPECT_EQ("{\n" 13690 "\t/* aaaa\n" 13691 "\t bbbb */\n" 13692 "}", 13693 format("{\n" 13694 "/* aaaa\n" 13695 " bbbb */\n" 13696 "}", 13697 Tab)); 13698 EXPECT_EQ("{\n" 13699 "\t/*\n" 13700 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13701 "\t bbbbbbbbbbbbb\n" 13702 "\t*/\n" 13703 "}", 13704 format("{\n" 13705 "/*\n" 13706 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13707 "*/\n" 13708 "}", 13709 Tab)); 13710 EXPECT_EQ("{\n" 13711 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13712 "\t// bbbbbbbbbbbbb\n" 13713 "}", 13714 format("{\n" 13715 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13716 "}", 13717 Tab)); 13718 EXPECT_EQ("{\n" 13719 "\t/*\n" 13720 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13721 "\t bbbbbbbbbbbbb\n" 13722 "\t*/\n" 13723 "}", 13724 format("{\n" 13725 "\t/*\n" 13726 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13727 "\t*/\n" 13728 "}", 13729 Tab)); 13730 EXPECT_EQ("{\n" 13731 "\t/*\n" 13732 "\n" 13733 "\t*/\n" 13734 "}", 13735 format("{\n" 13736 "\t/*\n" 13737 "\n" 13738 "\t*/\n" 13739 "}", 13740 Tab)); 13741 EXPECT_EQ("{\n" 13742 "\t/*\n" 13743 " asdf\n" 13744 "\t*/\n" 13745 "}", 13746 format("{\n" 13747 "\t/*\n" 13748 " asdf\n" 13749 "\t*/\n" 13750 "}", 13751 Tab)); 13752 EXPECT_EQ("/* some\n" 13753 " comment */", 13754 format(" \t \t /* some\n" 13755 " \t \t comment */", 13756 Tab)); 13757 EXPECT_EQ("int a; /* some\n" 13758 " comment */", 13759 format(" \t \t int a; /* some\n" 13760 " \t \t comment */", 13761 Tab)); 13762 EXPECT_EQ("int a; /* some\n" 13763 "comment */", 13764 format(" \t \t int\ta; /* some\n" 13765 " \t \t comment */", 13766 Tab)); 13767 EXPECT_EQ("f(\"\t\t\"); /* some\n" 13768 " comment */", 13769 format(" \t \t f(\"\t\t\"); /* some\n" 13770 " \t \t comment */", 13771 Tab)); 13772 EXPECT_EQ("{\n" 13773 "\t/*\n" 13774 "\t * Comment\n" 13775 "\t */\n" 13776 "\tint i;\n" 13777 "}", 13778 format("{\n" 13779 "\t/*\n" 13780 "\t * Comment\n" 13781 "\t */\n" 13782 "\t int i;\n" 13783 "}", 13784 Tab)); 13785 Tab.TabWidth = 2; 13786 Tab.IndentWidth = 2; 13787 EXPECT_EQ("{\n" 13788 "\t/* aaaa\n" 13789 "\t bbbb */\n" 13790 "}", 13791 format("{\n" 13792 "/* aaaa\n" 13793 " bbbb */\n" 13794 "}", 13795 Tab)); 13796 EXPECT_EQ("{\n" 13797 "\t/*\n" 13798 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13799 "\t bbbbbbbbbbbbb\n" 13800 "\t*/\n" 13801 "}", 13802 format("{\n" 13803 "/*\n" 13804 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13805 "*/\n" 13806 "}", 13807 Tab)); 13808 Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 13809 Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 13810 Tab.TabWidth = 4; 13811 Tab.IndentWidth = 4; 13812 verifyFormat("class Assign {\n" 13813 "\tvoid f() {\n" 13814 "\t\tint x = 123;\n" 13815 "\t\tint random = 4;\n" 13816 "\t\tstd::string alphabet =\n" 13817 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 13818 "\t}\n" 13819 "};", 13820 Tab); 13821 Tab.AlignOperands = FormatStyle::OAS_Align; 13822 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n" 13823 " cccccccccccccccccccc;", 13824 Tab); 13825 // no alignment 13826 verifyFormat("int aaaaaaaaaa =\n" 13827 "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 13828 Tab); 13829 verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n" 13830 " : bbbbbbbbbbbbbb ? 222222222222222\n" 13831 " : 333333333333333;", 13832 Tab); 13833 Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 13834 Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator; 13835 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n" 13836 " + cccccccccccccccccccc;", 13837 Tab); 13838 } 13839 13840 TEST_F(FormatTest, ZeroTabWidth) { 13841 FormatStyle Tab = getLLVMStyleWithColumns(42); 13842 Tab.IndentWidth = 8; 13843 Tab.UseTab = FormatStyle::UT_Never; 13844 Tab.TabWidth = 0; 13845 EXPECT_EQ("void a(){\n" 13846 " // line starts with '\t'\n" 13847 "};", 13848 format("void a(){\n" 13849 "\t// line starts with '\t'\n" 13850 "};", 13851 Tab)); 13852 13853 EXPECT_EQ("void a(){\n" 13854 " // line starts with '\t'\n" 13855 "};", 13856 format("void a(){\n" 13857 "\t\t// line starts with '\t'\n" 13858 "};", 13859 Tab)); 13860 13861 Tab.UseTab = FormatStyle::UT_ForIndentation; 13862 EXPECT_EQ("void a(){\n" 13863 " // line starts with '\t'\n" 13864 "};", 13865 format("void a(){\n" 13866 "\t// line starts with '\t'\n" 13867 "};", 13868 Tab)); 13869 13870 EXPECT_EQ("void a(){\n" 13871 " // line starts with '\t'\n" 13872 "};", 13873 format("void a(){\n" 13874 "\t\t// line starts with '\t'\n" 13875 "};", 13876 Tab)); 13877 13878 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 13879 EXPECT_EQ("void a(){\n" 13880 " // line starts with '\t'\n" 13881 "};", 13882 format("void a(){\n" 13883 "\t// line starts with '\t'\n" 13884 "};", 13885 Tab)); 13886 13887 EXPECT_EQ("void a(){\n" 13888 " // line starts with '\t'\n" 13889 "};", 13890 format("void a(){\n" 13891 "\t\t// line starts with '\t'\n" 13892 "};", 13893 Tab)); 13894 13895 Tab.UseTab = FormatStyle::UT_AlignWithSpaces; 13896 EXPECT_EQ("void a(){\n" 13897 " // line starts with '\t'\n" 13898 "};", 13899 format("void a(){\n" 13900 "\t// line starts with '\t'\n" 13901 "};", 13902 Tab)); 13903 13904 EXPECT_EQ("void a(){\n" 13905 " // line starts with '\t'\n" 13906 "};", 13907 format("void a(){\n" 13908 "\t\t// line starts with '\t'\n" 13909 "};", 13910 Tab)); 13911 13912 Tab.UseTab = FormatStyle::UT_Always; 13913 EXPECT_EQ("void a(){\n" 13914 "// line starts with '\t'\n" 13915 "};", 13916 format("void a(){\n" 13917 "\t// line starts with '\t'\n" 13918 "};", 13919 Tab)); 13920 13921 EXPECT_EQ("void a(){\n" 13922 "// line starts with '\t'\n" 13923 "};", 13924 format("void a(){\n" 13925 "\t\t// line starts with '\t'\n" 13926 "};", 13927 Tab)); 13928 } 13929 13930 TEST_F(FormatTest, CalculatesOriginalColumn) { 13931 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13932 "q\"; /* some\n" 13933 " comment */", 13934 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13935 "q\"; /* some\n" 13936 " comment */", 13937 getLLVMStyle())); 13938 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 13939 "/* some\n" 13940 " comment */", 13941 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 13942 " /* some\n" 13943 " comment */", 13944 getLLVMStyle())); 13945 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13946 "qqq\n" 13947 "/* some\n" 13948 " comment */", 13949 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13950 "qqq\n" 13951 " /* some\n" 13952 " comment */", 13953 getLLVMStyle())); 13954 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13955 "wwww; /* some\n" 13956 " comment */", 13957 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13958 "wwww; /* some\n" 13959 " comment */", 13960 getLLVMStyle())); 13961 } 13962 13963 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 13964 FormatStyle NoSpace = getLLVMStyle(); 13965 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 13966 13967 verifyFormat("while(true)\n" 13968 " continue;", 13969 NoSpace); 13970 verifyFormat("for(;;)\n" 13971 " continue;", 13972 NoSpace); 13973 verifyFormat("if(true)\n" 13974 " f();\n" 13975 "else if(true)\n" 13976 " f();", 13977 NoSpace); 13978 verifyFormat("do {\n" 13979 " do_something();\n" 13980 "} while(something());", 13981 NoSpace); 13982 verifyFormat("switch(x) {\n" 13983 "default:\n" 13984 " break;\n" 13985 "}", 13986 NoSpace); 13987 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 13988 verifyFormat("size_t x = sizeof(x);", NoSpace); 13989 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 13990 verifyFormat("auto f(int x) -> typeof(x);", NoSpace); 13991 verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace); 13992 verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace); 13993 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 13994 verifyFormat("alignas(128) char a[128];", NoSpace); 13995 verifyFormat("size_t x = alignof(MyType);", NoSpace); 13996 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 13997 verifyFormat("int f() throw(Deprecated);", NoSpace); 13998 verifyFormat("typedef void (*cb)(int);", NoSpace); 13999 verifyFormat("T A::operator()();", NoSpace); 14000 verifyFormat("X A::operator++(T);", NoSpace); 14001 verifyFormat("auto lambda = []() { return 0; };", NoSpace); 14002 14003 FormatStyle Space = getLLVMStyle(); 14004 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 14005 14006 verifyFormat("int f ();", Space); 14007 verifyFormat("void f (int a, T b) {\n" 14008 " while (true)\n" 14009 " continue;\n" 14010 "}", 14011 Space); 14012 verifyFormat("if (true)\n" 14013 " f ();\n" 14014 "else if (true)\n" 14015 " f ();", 14016 Space); 14017 verifyFormat("do {\n" 14018 " do_something ();\n" 14019 "} while (something ());", 14020 Space); 14021 verifyFormat("switch (x) {\n" 14022 "default:\n" 14023 " break;\n" 14024 "}", 14025 Space); 14026 verifyFormat("A::A () : a (1) {}", Space); 14027 verifyFormat("void f () __attribute__ ((asdf));", Space); 14028 verifyFormat("*(&a + 1);\n" 14029 "&((&a)[1]);\n" 14030 "a[(b + c) * d];\n" 14031 "(((a + 1) * 2) + 3) * 4;", 14032 Space); 14033 verifyFormat("#define A(x) x", Space); 14034 verifyFormat("#define A (x) x", Space); 14035 verifyFormat("#if defined(x)\n" 14036 "#endif", 14037 Space); 14038 verifyFormat("auto i = std::make_unique<int> (5);", Space); 14039 verifyFormat("size_t x = sizeof (x);", Space); 14040 verifyFormat("auto f (int x) -> decltype (x);", Space); 14041 verifyFormat("auto f (int x) -> typeof (x);", Space); 14042 verifyFormat("auto f (int x) -> _Atomic (x);", Space); 14043 verifyFormat("auto f (int x) -> __underlying_type (x);", Space); 14044 verifyFormat("int f (T x) noexcept (x.create ());", Space); 14045 verifyFormat("alignas (128) char a[128];", Space); 14046 verifyFormat("size_t x = alignof (MyType);", Space); 14047 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 14048 verifyFormat("int f () throw (Deprecated);", Space); 14049 verifyFormat("typedef void (*cb) (int);", Space); 14050 verifyFormat("T A::operator() ();", Space); 14051 verifyFormat("X A::operator++ (T);", Space); 14052 verifyFormat("auto lambda = [] () { return 0; };", Space); 14053 verifyFormat("int x = int (y);", Space); 14054 14055 FormatStyle SomeSpace = getLLVMStyle(); 14056 SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses; 14057 14058 verifyFormat("[]() -> float {}", SomeSpace); 14059 verifyFormat("[] (auto foo) {}", SomeSpace); 14060 verifyFormat("[foo]() -> int {}", SomeSpace); 14061 verifyFormat("int f();", SomeSpace); 14062 verifyFormat("void f (int a, T b) {\n" 14063 " while (true)\n" 14064 " continue;\n" 14065 "}", 14066 SomeSpace); 14067 verifyFormat("if (true)\n" 14068 " f();\n" 14069 "else if (true)\n" 14070 " f();", 14071 SomeSpace); 14072 verifyFormat("do {\n" 14073 " do_something();\n" 14074 "} while (something());", 14075 SomeSpace); 14076 verifyFormat("switch (x) {\n" 14077 "default:\n" 14078 " break;\n" 14079 "}", 14080 SomeSpace); 14081 verifyFormat("A::A() : a (1) {}", SomeSpace); 14082 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace); 14083 verifyFormat("*(&a + 1);\n" 14084 "&((&a)[1]);\n" 14085 "a[(b + c) * d];\n" 14086 "(((a + 1) * 2) + 3) * 4;", 14087 SomeSpace); 14088 verifyFormat("#define A(x) x", SomeSpace); 14089 verifyFormat("#define A (x) x", SomeSpace); 14090 verifyFormat("#if defined(x)\n" 14091 "#endif", 14092 SomeSpace); 14093 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace); 14094 verifyFormat("size_t x = sizeof (x);", SomeSpace); 14095 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace); 14096 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace); 14097 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace); 14098 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace); 14099 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace); 14100 verifyFormat("alignas (128) char a[128];", SomeSpace); 14101 verifyFormat("size_t x = alignof (MyType);", SomeSpace); 14102 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", 14103 SomeSpace); 14104 verifyFormat("int f() throw (Deprecated);", SomeSpace); 14105 verifyFormat("typedef void (*cb) (int);", SomeSpace); 14106 verifyFormat("T A::operator()();", SomeSpace); 14107 verifyFormat("X A::operator++ (T);", SomeSpace); 14108 verifyFormat("int x = int (y);", SomeSpace); 14109 verifyFormat("auto lambda = []() { return 0; };", SomeSpace); 14110 } 14111 14112 TEST_F(FormatTest, SpaceAfterLogicalNot) { 14113 FormatStyle Spaces = getLLVMStyle(); 14114 Spaces.SpaceAfterLogicalNot = true; 14115 14116 verifyFormat("bool x = ! y", Spaces); 14117 verifyFormat("if (! isFailure())", Spaces); 14118 verifyFormat("if (! (a && b))", Spaces); 14119 verifyFormat("\"Error!\"", Spaces); 14120 verifyFormat("! ! x", Spaces); 14121 } 14122 14123 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 14124 FormatStyle Spaces = getLLVMStyle(); 14125 14126 Spaces.SpacesInParentheses = true; 14127 verifyFormat("do_something( ::globalVar );", Spaces); 14128 verifyFormat("call( x, y, z );", Spaces); 14129 verifyFormat("call();", Spaces); 14130 verifyFormat("std::function<void( int, int )> callback;", Spaces); 14131 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 14132 Spaces); 14133 verifyFormat("while ( (bool)1 )\n" 14134 " continue;", 14135 Spaces); 14136 verifyFormat("for ( ;; )\n" 14137 " continue;", 14138 Spaces); 14139 verifyFormat("if ( true )\n" 14140 " f();\n" 14141 "else if ( true )\n" 14142 " f();", 14143 Spaces); 14144 verifyFormat("do {\n" 14145 " do_something( (int)i );\n" 14146 "} while ( something() );", 14147 Spaces); 14148 verifyFormat("switch ( x ) {\n" 14149 "default:\n" 14150 " break;\n" 14151 "}", 14152 Spaces); 14153 14154 Spaces.SpacesInParentheses = false; 14155 Spaces.SpacesInCStyleCastParentheses = true; 14156 verifyFormat("Type *A = ( Type * )P;", Spaces); 14157 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 14158 verifyFormat("x = ( int32 )y;", Spaces); 14159 verifyFormat("int a = ( int )(2.0f);", Spaces); 14160 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 14161 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 14162 verifyFormat("#define x (( int )-1)", Spaces); 14163 14164 // Run the first set of tests again with: 14165 Spaces.SpacesInParentheses = false; 14166 Spaces.SpaceInEmptyParentheses = true; 14167 Spaces.SpacesInCStyleCastParentheses = true; 14168 verifyFormat("call(x, y, z);", Spaces); 14169 verifyFormat("call( );", Spaces); 14170 verifyFormat("std::function<void(int, int)> callback;", Spaces); 14171 verifyFormat("while (( bool )1)\n" 14172 " continue;", 14173 Spaces); 14174 verifyFormat("for (;;)\n" 14175 " continue;", 14176 Spaces); 14177 verifyFormat("if (true)\n" 14178 " f( );\n" 14179 "else if (true)\n" 14180 " f( );", 14181 Spaces); 14182 verifyFormat("do {\n" 14183 " do_something(( int )i);\n" 14184 "} while (something( ));", 14185 Spaces); 14186 verifyFormat("switch (x) {\n" 14187 "default:\n" 14188 " break;\n" 14189 "}", 14190 Spaces); 14191 14192 // Run the first set of tests again with: 14193 Spaces.SpaceAfterCStyleCast = true; 14194 verifyFormat("call(x, y, z);", Spaces); 14195 verifyFormat("call( );", Spaces); 14196 verifyFormat("std::function<void(int, int)> callback;", Spaces); 14197 verifyFormat("while (( bool ) 1)\n" 14198 " continue;", 14199 Spaces); 14200 verifyFormat("for (;;)\n" 14201 " continue;", 14202 Spaces); 14203 verifyFormat("if (true)\n" 14204 " f( );\n" 14205 "else if (true)\n" 14206 " f( );", 14207 Spaces); 14208 verifyFormat("do {\n" 14209 " do_something(( int ) i);\n" 14210 "} while (something( ));", 14211 Spaces); 14212 verifyFormat("switch (x) {\n" 14213 "default:\n" 14214 " break;\n" 14215 "}", 14216 Spaces); 14217 14218 // Run subset of tests again with: 14219 Spaces.SpacesInCStyleCastParentheses = false; 14220 Spaces.SpaceAfterCStyleCast = true; 14221 verifyFormat("while ((bool) 1)\n" 14222 " continue;", 14223 Spaces); 14224 verifyFormat("do {\n" 14225 " do_something((int) i);\n" 14226 "} while (something( ));", 14227 Spaces); 14228 14229 verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces); 14230 verifyFormat("size_t idx = (size_t) a;", Spaces); 14231 verifyFormat("size_t idx = (size_t) (a - 1);", Spaces); 14232 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); 14233 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); 14234 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); 14235 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); 14236 Spaces.ColumnLimit = 80; 14237 Spaces.IndentWidth = 4; 14238 Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 14239 verifyFormat("void foo( ) {\n" 14240 " size_t foo = (*(function))(\n" 14241 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " 14242 "BarrrrrrrrrrrrLong,\n" 14243 " FoooooooooLooooong);\n" 14244 "}", 14245 Spaces); 14246 Spaces.SpaceAfterCStyleCast = false; 14247 verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces); 14248 verifyFormat("size_t idx = (size_t)a;", Spaces); 14249 verifyFormat("size_t idx = (size_t)(a - 1);", Spaces); 14250 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); 14251 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); 14252 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); 14253 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); 14254 14255 verifyFormat("void foo( ) {\n" 14256 " size_t foo = (*(function))(\n" 14257 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " 14258 "BarrrrrrrrrrrrLong,\n" 14259 " FoooooooooLooooong);\n" 14260 "}", 14261 Spaces); 14262 } 14263 14264 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 14265 verifyFormat("int a[5];"); 14266 verifyFormat("a[3] += 42;"); 14267 14268 FormatStyle Spaces = getLLVMStyle(); 14269 Spaces.SpacesInSquareBrackets = true; 14270 // Not lambdas. 14271 verifyFormat("int a[ 5 ];", Spaces); 14272 verifyFormat("a[ 3 ] += 42;", Spaces); 14273 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 14274 verifyFormat("double &operator[](int i) { return 0; }\n" 14275 "int i;", 14276 Spaces); 14277 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 14278 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 14279 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 14280 // Lambdas. 14281 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 14282 verifyFormat("return [ i, args... ] {};", Spaces); 14283 verifyFormat("int foo = [ &bar ]() {};", Spaces); 14284 verifyFormat("int foo = [ = ]() {};", Spaces); 14285 verifyFormat("int foo = [ & ]() {};", Spaces); 14286 verifyFormat("int foo = [ =, &bar ]() {};", Spaces); 14287 verifyFormat("int foo = [ &bar, = ]() {};", Spaces); 14288 } 14289 14290 TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) { 14291 FormatStyle NoSpaceStyle = getLLVMStyle(); 14292 verifyFormat("int a[5];", NoSpaceStyle); 14293 verifyFormat("a[3] += 42;", NoSpaceStyle); 14294 14295 verifyFormat("int a[1];", NoSpaceStyle); 14296 verifyFormat("int 1 [a];", NoSpaceStyle); 14297 verifyFormat("int a[1][2];", NoSpaceStyle); 14298 verifyFormat("a[7] = 5;", NoSpaceStyle); 14299 verifyFormat("int a = (f())[23];", NoSpaceStyle); 14300 verifyFormat("f([] {})", NoSpaceStyle); 14301 14302 FormatStyle Space = getLLVMStyle(); 14303 Space.SpaceBeforeSquareBrackets = true; 14304 verifyFormat("int c = []() -> int { return 2; }();\n", Space); 14305 verifyFormat("return [i, args...] {};", Space); 14306 14307 verifyFormat("int a [5];", Space); 14308 verifyFormat("a [3] += 42;", Space); 14309 verifyFormat("constexpr char hello []{\"hello\"};", Space); 14310 verifyFormat("double &operator[](int i) { return 0; }\n" 14311 "int i;", 14312 Space); 14313 verifyFormat("std::unique_ptr<int []> foo() {}", Space); 14314 verifyFormat("int i = a [a][a]->f();", Space); 14315 verifyFormat("int i = (*b) [a]->f();", Space); 14316 14317 verifyFormat("int a [1];", Space); 14318 verifyFormat("int 1 [a];", Space); 14319 verifyFormat("int a [1][2];", Space); 14320 verifyFormat("a [7] = 5;", Space); 14321 verifyFormat("int a = (f()) [23];", Space); 14322 verifyFormat("f([] {})", Space); 14323 } 14324 14325 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 14326 verifyFormat("int a = 5;"); 14327 verifyFormat("a += 42;"); 14328 verifyFormat("a or_eq 8;"); 14329 14330 FormatStyle Spaces = getLLVMStyle(); 14331 Spaces.SpaceBeforeAssignmentOperators = false; 14332 verifyFormat("int a= 5;", Spaces); 14333 verifyFormat("a+= 42;", Spaces); 14334 verifyFormat("a or_eq 8;", Spaces); 14335 } 14336 14337 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) { 14338 verifyFormat("class Foo : public Bar {};"); 14339 verifyFormat("Foo::Foo() : foo(1) {}"); 14340 verifyFormat("for (auto a : b) {\n}"); 14341 verifyFormat("int x = a ? b : c;"); 14342 verifyFormat("{\n" 14343 "label0:\n" 14344 " int x = 0;\n" 14345 "}"); 14346 verifyFormat("switch (x) {\n" 14347 "case 1:\n" 14348 "default:\n" 14349 "}"); 14350 verifyFormat("switch (allBraces) {\n" 14351 "case 1: {\n" 14352 " break;\n" 14353 "}\n" 14354 "case 2: {\n" 14355 " [[fallthrough]];\n" 14356 "}\n" 14357 "default: {\n" 14358 " break;\n" 14359 "}\n" 14360 "}"); 14361 14362 FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30); 14363 CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false; 14364 verifyFormat("class Foo : public Bar {};", CtorInitializerStyle); 14365 verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle); 14366 verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle); 14367 verifyFormat("int x = a ? b : c;", CtorInitializerStyle); 14368 verifyFormat("{\n" 14369 "label1:\n" 14370 " int x = 0;\n" 14371 "}", 14372 CtorInitializerStyle); 14373 verifyFormat("switch (x) {\n" 14374 "case 1:\n" 14375 "default:\n" 14376 "}", 14377 CtorInitializerStyle); 14378 verifyFormat("switch (allBraces) {\n" 14379 "case 1: {\n" 14380 " break;\n" 14381 "}\n" 14382 "case 2: {\n" 14383 " [[fallthrough]];\n" 14384 "}\n" 14385 "default: {\n" 14386 " break;\n" 14387 "}\n" 14388 "}", 14389 CtorInitializerStyle); 14390 CtorInitializerStyle.BreakConstructorInitializers = 14391 FormatStyle::BCIS_AfterColon; 14392 verifyFormat("Fooooooooooo::Fooooooooooo():\n" 14393 " aaaaaaaaaaaaaaaa(1),\n" 14394 " bbbbbbbbbbbbbbbb(2) {}", 14395 CtorInitializerStyle); 14396 CtorInitializerStyle.BreakConstructorInitializers = 14397 FormatStyle::BCIS_BeforeComma; 14398 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 14399 " : aaaaaaaaaaaaaaaa(1)\n" 14400 " , bbbbbbbbbbbbbbbb(2) {}", 14401 CtorInitializerStyle); 14402 CtorInitializerStyle.BreakConstructorInitializers = 14403 FormatStyle::BCIS_BeforeColon; 14404 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 14405 " : aaaaaaaaaaaaaaaa(1),\n" 14406 " bbbbbbbbbbbbbbbb(2) {}", 14407 CtorInitializerStyle); 14408 CtorInitializerStyle.ConstructorInitializerIndentWidth = 0; 14409 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 14410 ": aaaaaaaaaaaaaaaa(1),\n" 14411 " bbbbbbbbbbbbbbbb(2) {}", 14412 CtorInitializerStyle); 14413 14414 FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30); 14415 InheritanceStyle.SpaceBeforeInheritanceColon = false; 14416 verifyFormat("class Foo: public Bar {};", InheritanceStyle); 14417 verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle); 14418 verifyFormat("for (auto a : b) {\n}", InheritanceStyle); 14419 verifyFormat("int x = a ? b : c;", InheritanceStyle); 14420 verifyFormat("{\n" 14421 "label2:\n" 14422 " int x = 0;\n" 14423 "}", 14424 InheritanceStyle); 14425 verifyFormat("switch (x) {\n" 14426 "case 1:\n" 14427 "default:\n" 14428 "}", 14429 InheritanceStyle); 14430 verifyFormat("switch (allBraces) {\n" 14431 "case 1: {\n" 14432 " break;\n" 14433 "}\n" 14434 "case 2: {\n" 14435 " [[fallthrough]];\n" 14436 "}\n" 14437 "default: {\n" 14438 " break;\n" 14439 "}\n" 14440 "}", 14441 InheritanceStyle); 14442 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma; 14443 verifyFormat("class Foooooooooooooooooooooo\n" 14444 " : public aaaaaaaaaaaaaaaaaa,\n" 14445 " public bbbbbbbbbbbbbbbbbb {\n" 14446 "}", 14447 InheritanceStyle); 14448 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon; 14449 verifyFormat("class Foooooooooooooooooooooo:\n" 14450 " public aaaaaaaaaaaaaaaaaa,\n" 14451 " public bbbbbbbbbbbbbbbbbb {\n" 14452 "}", 14453 InheritanceStyle); 14454 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 14455 verifyFormat("class Foooooooooooooooooooooo\n" 14456 " : public aaaaaaaaaaaaaaaaaa\n" 14457 " , public bbbbbbbbbbbbbbbbbb {\n" 14458 "}", 14459 InheritanceStyle); 14460 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 14461 verifyFormat("class Foooooooooooooooooooooo\n" 14462 " : public aaaaaaaaaaaaaaaaaa,\n" 14463 " public bbbbbbbbbbbbbbbbbb {\n" 14464 "}", 14465 InheritanceStyle); 14466 InheritanceStyle.ConstructorInitializerIndentWidth = 0; 14467 verifyFormat("class Foooooooooooooooooooooo\n" 14468 ": public aaaaaaaaaaaaaaaaaa,\n" 14469 " public bbbbbbbbbbbbbbbbbb {}", 14470 InheritanceStyle); 14471 14472 FormatStyle ForLoopStyle = getLLVMStyle(); 14473 ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false; 14474 verifyFormat("class Foo : public Bar {};", ForLoopStyle); 14475 verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle); 14476 verifyFormat("for (auto a: b) {\n}", ForLoopStyle); 14477 verifyFormat("int x = a ? b : c;", ForLoopStyle); 14478 verifyFormat("{\n" 14479 "label2:\n" 14480 " int x = 0;\n" 14481 "}", 14482 ForLoopStyle); 14483 verifyFormat("switch (x) {\n" 14484 "case 1:\n" 14485 "default:\n" 14486 "}", 14487 ForLoopStyle); 14488 verifyFormat("switch (allBraces) {\n" 14489 "case 1: {\n" 14490 " break;\n" 14491 "}\n" 14492 "case 2: {\n" 14493 " [[fallthrough]];\n" 14494 "}\n" 14495 "default: {\n" 14496 " break;\n" 14497 "}\n" 14498 "}", 14499 ForLoopStyle); 14500 14501 FormatStyle CaseStyle = getLLVMStyle(); 14502 CaseStyle.SpaceBeforeCaseColon = true; 14503 verifyFormat("class Foo : public Bar {};", CaseStyle); 14504 verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle); 14505 verifyFormat("for (auto a : b) {\n}", CaseStyle); 14506 verifyFormat("int x = a ? b : c;", CaseStyle); 14507 verifyFormat("switch (x) {\n" 14508 "case 1 :\n" 14509 "default :\n" 14510 "}", 14511 CaseStyle); 14512 verifyFormat("switch (allBraces) {\n" 14513 "case 1 : {\n" 14514 " break;\n" 14515 "}\n" 14516 "case 2 : {\n" 14517 " [[fallthrough]];\n" 14518 "}\n" 14519 "default : {\n" 14520 " break;\n" 14521 "}\n" 14522 "}", 14523 CaseStyle); 14524 14525 FormatStyle NoSpaceStyle = getLLVMStyle(); 14526 EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false); 14527 NoSpaceStyle.SpaceBeforeCtorInitializerColon = false; 14528 NoSpaceStyle.SpaceBeforeInheritanceColon = false; 14529 NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 14530 verifyFormat("class Foo: public Bar {};", NoSpaceStyle); 14531 verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle); 14532 verifyFormat("for (auto a: b) {\n}", NoSpaceStyle); 14533 verifyFormat("int x = a ? b : c;", NoSpaceStyle); 14534 verifyFormat("{\n" 14535 "label3:\n" 14536 " int x = 0;\n" 14537 "}", 14538 NoSpaceStyle); 14539 verifyFormat("switch (x) {\n" 14540 "case 1:\n" 14541 "default:\n" 14542 "}", 14543 NoSpaceStyle); 14544 verifyFormat("switch (allBraces) {\n" 14545 "case 1: {\n" 14546 " break;\n" 14547 "}\n" 14548 "case 2: {\n" 14549 " [[fallthrough]];\n" 14550 "}\n" 14551 "default: {\n" 14552 " break;\n" 14553 "}\n" 14554 "}", 14555 NoSpaceStyle); 14556 14557 FormatStyle InvertedSpaceStyle = getLLVMStyle(); 14558 InvertedSpaceStyle.SpaceBeforeCaseColon = true; 14559 InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false; 14560 InvertedSpaceStyle.SpaceBeforeInheritanceColon = false; 14561 InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 14562 verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle); 14563 verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle); 14564 verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle); 14565 verifyFormat("int x = a ? b : c;", InvertedSpaceStyle); 14566 verifyFormat("{\n" 14567 "label3:\n" 14568 " int x = 0;\n" 14569 "}", 14570 InvertedSpaceStyle); 14571 verifyFormat("switch (x) {\n" 14572 "case 1 :\n" 14573 "case 2 : {\n" 14574 " break;\n" 14575 "}\n" 14576 "default :\n" 14577 " break;\n" 14578 "}", 14579 InvertedSpaceStyle); 14580 verifyFormat("switch (allBraces) {\n" 14581 "case 1 : {\n" 14582 " break;\n" 14583 "}\n" 14584 "case 2 : {\n" 14585 " [[fallthrough]];\n" 14586 "}\n" 14587 "default : {\n" 14588 " break;\n" 14589 "}\n" 14590 "}", 14591 InvertedSpaceStyle); 14592 } 14593 14594 TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) { 14595 FormatStyle Style = getLLVMStyle(); 14596 14597 Style.PointerAlignment = FormatStyle::PAS_Left; 14598 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; 14599 verifyFormat("void* const* x = NULL;", Style); 14600 14601 #define verifyQualifierSpaces(Code, Pointers, Qualifiers) \ 14602 do { \ 14603 Style.PointerAlignment = FormatStyle::Pointers; \ 14604 Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers; \ 14605 verifyFormat(Code, Style); \ 14606 } while (false) 14607 14608 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default); 14609 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default); 14610 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default); 14611 14612 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before); 14613 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before); 14614 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before); 14615 14616 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After); 14617 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After); 14618 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After); 14619 14620 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both); 14621 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both); 14622 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both); 14623 14624 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default); 14625 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, 14626 SAPQ_Default); 14627 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 14628 SAPQ_Default); 14629 14630 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before); 14631 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, 14632 SAPQ_Before); 14633 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 14634 SAPQ_Before); 14635 14636 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After); 14637 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After); 14638 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 14639 SAPQ_After); 14640 14641 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both); 14642 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both); 14643 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both); 14644 14645 #undef verifyQualifierSpaces 14646 14647 FormatStyle Spaces = getLLVMStyle(); 14648 Spaces.AttributeMacros.push_back("qualified"); 14649 Spaces.PointerAlignment = FormatStyle::PAS_Right; 14650 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; 14651 verifyFormat("SomeType *volatile *a = NULL;", Spaces); 14652 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces); 14653 verifyFormat("std::vector<SomeType *const *> x;", Spaces); 14654 verifyFormat("std::vector<SomeType *qualified *> x;", Spaces); 14655 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 14656 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before; 14657 verifyFormat("SomeType * volatile *a = NULL;", Spaces); 14658 verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces); 14659 verifyFormat("std::vector<SomeType * const *> x;", Spaces); 14660 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces); 14661 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 14662 14663 // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left. 14664 Spaces.PointerAlignment = FormatStyle::PAS_Left; 14665 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before; 14666 verifyFormat("SomeType* volatile* a = NULL;", Spaces); 14667 verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces); 14668 verifyFormat("std::vector<SomeType* const*> x;", Spaces); 14669 verifyFormat("std::vector<SomeType* qualified*> x;", Spaces); 14670 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 14671 // However, setting it to SAPQ_After should add spaces after __attribute, etc. 14672 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After; 14673 verifyFormat("SomeType* volatile * a = NULL;", Spaces); 14674 verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces); 14675 verifyFormat("std::vector<SomeType* const *> x;", Spaces); 14676 verifyFormat("std::vector<SomeType* qualified *> x;", Spaces); 14677 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 14678 14679 // PAS_Middle should not have any noticeable changes even for SAPQ_Both 14680 Spaces.PointerAlignment = FormatStyle::PAS_Middle; 14681 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After; 14682 verifyFormat("SomeType * volatile * a = NULL;", Spaces); 14683 verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces); 14684 verifyFormat("std::vector<SomeType * const *> x;", Spaces); 14685 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces); 14686 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 14687 } 14688 14689 TEST_F(FormatTest, AlignConsecutiveMacros) { 14690 FormatStyle Style = getLLVMStyle(); 14691 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 14692 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 14693 Style.AlignConsecutiveMacros = FormatStyle::ACS_None; 14694 14695 verifyFormat("#define a 3\n" 14696 "#define bbbb 4\n" 14697 "#define ccc (5)", 14698 Style); 14699 14700 verifyFormat("#define f(x) (x * x)\n" 14701 "#define fff(x, y, z) (x * y + z)\n" 14702 "#define ffff(x, y) (x - y)", 14703 Style); 14704 14705 verifyFormat("#define foo(x, y) (x + y)\n" 14706 "#define bar (5, 6)(2 + 2)", 14707 Style); 14708 14709 verifyFormat("#define a 3\n" 14710 "#define bbbb 4\n" 14711 "#define ccc (5)\n" 14712 "#define f(x) (x * x)\n" 14713 "#define fff(x, y, z) (x * y + z)\n" 14714 "#define ffff(x, y) (x - y)", 14715 Style); 14716 14717 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 14718 verifyFormat("#define a 3\n" 14719 "#define bbbb 4\n" 14720 "#define ccc (5)", 14721 Style); 14722 14723 verifyFormat("#define f(x) (x * x)\n" 14724 "#define fff(x, y, z) (x * y + z)\n" 14725 "#define ffff(x, y) (x - y)", 14726 Style); 14727 14728 verifyFormat("#define foo(x, y) (x + y)\n" 14729 "#define bar (5, 6)(2 + 2)", 14730 Style); 14731 14732 verifyFormat("#define a 3\n" 14733 "#define bbbb 4\n" 14734 "#define ccc (5)\n" 14735 "#define f(x) (x * x)\n" 14736 "#define fff(x, y, z) (x * y + z)\n" 14737 "#define ffff(x, y) (x - y)", 14738 Style); 14739 14740 verifyFormat("#define a 5\n" 14741 "#define foo(x, y) (x + y)\n" 14742 "#define CCC (6)\n" 14743 "auto lambda = []() {\n" 14744 " auto ii = 0;\n" 14745 " float j = 0;\n" 14746 " return 0;\n" 14747 "};\n" 14748 "int i = 0;\n" 14749 "float i2 = 0;\n" 14750 "auto v = type{\n" 14751 " i = 1, //\n" 14752 " (i = 2), //\n" 14753 " i = 3 //\n" 14754 "};", 14755 Style); 14756 14757 Style.AlignConsecutiveMacros = FormatStyle::ACS_None; 14758 Style.ColumnLimit = 20; 14759 14760 verifyFormat("#define a \\\n" 14761 " \"aabbbbbbbbbbbb\"\n" 14762 "#define D \\\n" 14763 " \"aabbbbbbbbbbbb\" \\\n" 14764 " \"ccddeeeeeeeee\"\n" 14765 "#define B \\\n" 14766 " \"QQQQQQQQQQQQQ\" \\\n" 14767 " \"FFFFFFFFFFFFF\" \\\n" 14768 " \"LLLLLLLL\"\n", 14769 Style); 14770 14771 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 14772 verifyFormat("#define a \\\n" 14773 " \"aabbbbbbbbbbbb\"\n" 14774 "#define D \\\n" 14775 " \"aabbbbbbbbbbbb\" \\\n" 14776 " \"ccddeeeeeeeee\"\n" 14777 "#define B \\\n" 14778 " \"QQQQQQQQQQQQQ\" \\\n" 14779 " \"FFFFFFFFFFFFF\" \\\n" 14780 " \"LLLLLLLL\"\n", 14781 Style); 14782 14783 // Test across comments 14784 Style.MaxEmptyLinesToKeep = 10; 14785 Style.ReflowComments = false; 14786 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossComments; 14787 EXPECT_EQ("#define a 3\n" 14788 "// line comment\n" 14789 "#define bbbb 4\n" 14790 "#define ccc (5)", 14791 format("#define a 3\n" 14792 "// line comment\n" 14793 "#define bbbb 4\n" 14794 "#define ccc (5)", 14795 Style)); 14796 14797 EXPECT_EQ("#define a 3\n" 14798 "/* block comment */\n" 14799 "#define bbbb 4\n" 14800 "#define ccc (5)", 14801 format("#define a 3\n" 14802 "/* block comment */\n" 14803 "#define bbbb 4\n" 14804 "#define ccc (5)", 14805 Style)); 14806 14807 EXPECT_EQ("#define a 3\n" 14808 "/* multi-line *\n" 14809 " * block comment */\n" 14810 "#define bbbb 4\n" 14811 "#define ccc (5)", 14812 format("#define a 3\n" 14813 "/* multi-line *\n" 14814 " * block comment */\n" 14815 "#define bbbb 4\n" 14816 "#define ccc (5)", 14817 Style)); 14818 14819 EXPECT_EQ("#define a 3\n" 14820 "// multi-line line comment\n" 14821 "//\n" 14822 "#define bbbb 4\n" 14823 "#define ccc (5)", 14824 format("#define a 3\n" 14825 "// multi-line line comment\n" 14826 "//\n" 14827 "#define bbbb 4\n" 14828 "#define ccc (5)", 14829 Style)); 14830 14831 EXPECT_EQ("#define a 3\n" 14832 "// empty lines still break.\n" 14833 "\n" 14834 "#define bbbb 4\n" 14835 "#define ccc (5)", 14836 format("#define a 3\n" 14837 "// empty lines still break.\n" 14838 "\n" 14839 "#define bbbb 4\n" 14840 "#define ccc (5)", 14841 Style)); 14842 14843 // Test across empty lines 14844 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLines; 14845 EXPECT_EQ("#define a 3\n" 14846 "\n" 14847 "#define bbbb 4\n" 14848 "#define ccc (5)", 14849 format("#define a 3\n" 14850 "\n" 14851 "#define bbbb 4\n" 14852 "#define ccc (5)", 14853 Style)); 14854 14855 EXPECT_EQ("#define a 3\n" 14856 "\n" 14857 "\n" 14858 "\n" 14859 "#define bbbb 4\n" 14860 "#define ccc (5)", 14861 format("#define a 3\n" 14862 "\n" 14863 "\n" 14864 "\n" 14865 "#define bbbb 4\n" 14866 "#define ccc (5)", 14867 Style)); 14868 14869 EXPECT_EQ("#define a 3\n" 14870 "// comments should break alignment\n" 14871 "//\n" 14872 "#define bbbb 4\n" 14873 "#define ccc (5)", 14874 format("#define a 3\n" 14875 "// comments should break alignment\n" 14876 "//\n" 14877 "#define bbbb 4\n" 14878 "#define ccc (5)", 14879 Style)); 14880 14881 // Test across empty lines and comments 14882 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLinesAndComments; 14883 verifyFormat("#define a 3\n" 14884 "\n" 14885 "// line comment\n" 14886 "#define bbbb 4\n" 14887 "#define ccc (5)", 14888 Style); 14889 14890 EXPECT_EQ("#define a 3\n" 14891 "\n" 14892 "\n" 14893 "/* multi-line *\n" 14894 " * block comment */\n" 14895 "\n" 14896 "\n" 14897 "#define bbbb 4\n" 14898 "#define ccc (5)", 14899 format("#define a 3\n" 14900 "\n" 14901 "\n" 14902 "/* multi-line *\n" 14903 " * block comment */\n" 14904 "\n" 14905 "\n" 14906 "#define bbbb 4\n" 14907 "#define ccc (5)", 14908 Style)); 14909 14910 EXPECT_EQ("#define a 3\n" 14911 "\n" 14912 "\n" 14913 "/* multi-line *\n" 14914 " * block comment */\n" 14915 "\n" 14916 "\n" 14917 "#define bbbb 4\n" 14918 "#define ccc (5)", 14919 format("#define a 3\n" 14920 "\n" 14921 "\n" 14922 "/* multi-line *\n" 14923 " * block comment */\n" 14924 "\n" 14925 "\n" 14926 "#define bbbb 4\n" 14927 "#define ccc (5)", 14928 Style)); 14929 } 14930 14931 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) { 14932 FormatStyle Alignment = getLLVMStyle(); 14933 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 14934 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossEmptyLines; 14935 14936 Alignment.MaxEmptyLinesToKeep = 10; 14937 /* Test alignment across empty lines */ 14938 EXPECT_EQ("int a = 5;\n" 14939 "\n" 14940 "int oneTwoThree = 123;", 14941 format("int a = 5;\n" 14942 "\n" 14943 "int oneTwoThree= 123;", 14944 Alignment)); 14945 EXPECT_EQ("int a = 5;\n" 14946 "int one = 1;\n" 14947 "\n" 14948 "int oneTwoThree = 123;", 14949 format("int a = 5;\n" 14950 "int one = 1;\n" 14951 "\n" 14952 "int oneTwoThree = 123;", 14953 Alignment)); 14954 EXPECT_EQ("int a = 5;\n" 14955 "int one = 1;\n" 14956 "\n" 14957 "int oneTwoThree = 123;\n" 14958 "int oneTwo = 12;", 14959 format("int a = 5;\n" 14960 "int one = 1;\n" 14961 "\n" 14962 "int oneTwoThree = 123;\n" 14963 "int oneTwo = 12;", 14964 Alignment)); 14965 14966 /* Test across comments */ 14967 EXPECT_EQ("int a = 5;\n" 14968 "/* block comment */\n" 14969 "int oneTwoThree = 123;", 14970 format("int a = 5;\n" 14971 "/* block comment */\n" 14972 "int oneTwoThree=123;", 14973 Alignment)); 14974 14975 EXPECT_EQ("int a = 5;\n" 14976 "// line comment\n" 14977 "int oneTwoThree = 123;", 14978 format("int a = 5;\n" 14979 "// line comment\n" 14980 "int oneTwoThree=123;", 14981 Alignment)); 14982 14983 /* Test across comments and newlines */ 14984 EXPECT_EQ("int a = 5;\n" 14985 "\n" 14986 "/* block comment */\n" 14987 "int oneTwoThree = 123;", 14988 format("int a = 5;\n" 14989 "\n" 14990 "/* block comment */\n" 14991 "int oneTwoThree=123;", 14992 Alignment)); 14993 14994 EXPECT_EQ("int a = 5;\n" 14995 "\n" 14996 "// line comment\n" 14997 "int oneTwoThree = 123;", 14998 format("int a = 5;\n" 14999 "\n" 15000 "// line comment\n" 15001 "int oneTwoThree=123;", 15002 Alignment)); 15003 } 15004 15005 TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) { 15006 FormatStyle Alignment = getLLVMStyle(); 15007 Alignment.AlignConsecutiveDeclarations = 15008 FormatStyle::ACS_AcrossEmptyLinesAndComments; 15009 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 15010 15011 Alignment.MaxEmptyLinesToKeep = 10; 15012 /* Test alignment across empty lines */ 15013 EXPECT_EQ("int a = 5;\n" 15014 "\n" 15015 "float const oneTwoThree = 123;", 15016 format("int a = 5;\n" 15017 "\n" 15018 "float const oneTwoThree = 123;", 15019 Alignment)); 15020 EXPECT_EQ("int a = 5;\n" 15021 "float const one = 1;\n" 15022 "\n" 15023 "int oneTwoThree = 123;", 15024 format("int a = 5;\n" 15025 "float const one = 1;\n" 15026 "\n" 15027 "int oneTwoThree = 123;", 15028 Alignment)); 15029 15030 /* Test across comments */ 15031 EXPECT_EQ("float const a = 5;\n" 15032 "/* block comment */\n" 15033 "int oneTwoThree = 123;", 15034 format("float const a = 5;\n" 15035 "/* block comment */\n" 15036 "int oneTwoThree=123;", 15037 Alignment)); 15038 15039 EXPECT_EQ("float const a = 5;\n" 15040 "// line comment\n" 15041 "int oneTwoThree = 123;", 15042 format("float const a = 5;\n" 15043 "// line comment\n" 15044 "int oneTwoThree=123;", 15045 Alignment)); 15046 15047 /* Test across comments and newlines */ 15048 EXPECT_EQ("float const a = 5;\n" 15049 "\n" 15050 "/* block comment */\n" 15051 "int oneTwoThree = 123;", 15052 format("float const a = 5;\n" 15053 "\n" 15054 "/* block comment */\n" 15055 "int oneTwoThree=123;", 15056 Alignment)); 15057 15058 EXPECT_EQ("float const a = 5;\n" 15059 "\n" 15060 "// line comment\n" 15061 "int oneTwoThree = 123;", 15062 format("float const a = 5;\n" 15063 "\n" 15064 "// line comment\n" 15065 "int oneTwoThree=123;", 15066 Alignment)); 15067 } 15068 15069 TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) { 15070 FormatStyle Alignment = getLLVMStyle(); 15071 Alignment.AlignConsecutiveBitFields = 15072 FormatStyle::ACS_AcrossEmptyLinesAndComments; 15073 15074 Alignment.MaxEmptyLinesToKeep = 10; 15075 /* Test alignment across empty lines */ 15076 EXPECT_EQ("int a : 5;\n" 15077 "\n" 15078 "int longbitfield : 6;", 15079 format("int a : 5;\n" 15080 "\n" 15081 "int longbitfield : 6;", 15082 Alignment)); 15083 EXPECT_EQ("int a : 5;\n" 15084 "int one : 1;\n" 15085 "\n" 15086 "int longbitfield : 6;", 15087 format("int a : 5;\n" 15088 "int one : 1;\n" 15089 "\n" 15090 "int longbitfield : 6;", 15091 Alignment)); 15092 15093 /* Test across comments */ 15094 EXPECT_EQ("int a : 5;\n" 15095 "/* block comment */\n" 15096 "int longbitfield : 6;", 15097 format("int a : 5;\n" 15098 "/* block comment */\n" 15099 "int longbitfield : 6;", 15100 Alignment)); 15101 EXPECT_EQ("int a : 5;\n" 15102 "int one : 1;\n" 15103 "// line comment\n" 15104 "int longbitfield : 6;", 15105 format("int a : 5;\n" 15106 "int one : 1;\n" 15107 "// line comment\n" 15108 "int longbitfield : 6;", 15109 Alignment)); 15110 15111 /* Test across comments and newlines */ 15112 EXPECT_EQ("int a : 5;\n" 15113 "/* block comment */\n" 15114 "\n" 15115 "int longbitfield : 6;", 15116 format("int a : 5;\n" 15117 "/* block comment */\n" 15118 "\n" 15119 "int longbitfield : 6;", 15120 Alignment)); 15121 EXPECT_EQ("int a : 5;\n" 15122 "int one : 1;\n" 15123 "\n" 15124 "// line comment\n" 15125 "\n" 15126 "int longbitfield : 6;", 15127 format("int a : 5;\n" 15128 "int one : 1;\n" 15129 "\n" 15130 "// line comment \n" 15131 "\n" 15132 "int longbitfield : 6;", 15133 Alignment)); 15134 } 15135 15136 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) { 15137 FormatStyle Alignment = getLLVMStyle(); 15138 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15139 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossComments; 15140 15141 Alignment.MaxEmptyLinesToKeep = 10; 15142 /* Test alignment across empty lines */ 15143 EXPECT_EQ("int a = 5;\n" 15144 "\n" 15145 "int oneTwoThree = 123;", 15146 format("int a = 5;\n" 15147 "\n" 15148 "int oneTwoThree= 123;", 15149 Alignment)); 15150 EXPECT_EQ("int a = 5;\n" 15151 "int one = 1;\n" 15152 "\n" 15153 "int oneTwoThree = 123;", 15154 format("int a = 5;\n" 15155 "int one = 1;\n" 15156 "\n" 15157 "int oneTwoThree = 123;", 15158 Alignment)); 15159 15160 /* Test across comments */ 15161 EXPECT_EQ("int a = 5;\n" 15162 "/* block comment */\n" 15163 "int oneTwoThree = 123;", 15164 format("int a = 5;\n" 15165 "/* block comment */\n" 15166 "int oneTwoThree=123;", 15167 Alignment)); 15168 15169 EXPECT_EQ("int a = 5;\n" 15170 "// line comment\n" 15171 "int oneTwoThree = 123;", 15172 format("int a = 5;\n" 15173 "// line comment\n" 15174 "int oneTwoThree=123;", 15175 Alignment)); 15176 15177 EXPECT_EQ("int a = 5;\n" 15178 "/*\n" 15179 " * multi-line block comment\n" 15180 " */\n" 15181 "int oneTwoThree = 123;", 15182 format("int a = 5;\n" 15183 "/*\n" 15184 " * multi-line block comment\n" 15185 " */\n" 15186 "int oneTwoThree=123;", 15187 Alignment)); 15188 15189 EXPECT_EQ("int a = 5;\n" 15190 "//\n" 15191 "// multi-line line comment\n" 15192 "//\n" 15193 "int oneTwoThree = 123;", 15194 format("int a = 5;\n" 15195 "//\n" 15196 "// multi-line line comment\n" 15197 "//\n" 15198 "int oneTwoThree=123;", 15199 Alignment)); 15200 15201 /* Test across comments and newlines */ 15202 EXPECT_EQ("int a = 5;\n" 15203 "\n" 15204 "/* block comment */\n" 15205 "int oneTwoThree = 123;", 15206 format("int a = 5;\n" 15207 "\n" 15208 "/* block comment */\n" 15209 "int oneTwoThree=123;", 15210 Alignment)); 15211 15212 EXPECT_EQ("int a = 5;\n" 15213 "\n" 15214 "// line comment\n" 15215 "int oneTwoThree = 123;", 15216 format("int a = 5;\n" 15217 "\n" 15218 "// line comment\n" 15219 "int oneTwoThree=123;", 15220 Alignment)); 15221 } 15222 15223 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) { 15224 FormatStyle Alignment = getLLVMStyle(); 15225 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15226 Alignment.AlignConsecutiveAssignments = 15227 FormatStyle::ACS_AcrossEmptyLinesAndComments; 15228 verifyFormat("int a = 5;\n" 15229 "int oneTwoThree = 123;", 15230 Alignment); 15231 verifyFormat("int a = method();\n" 15232 "int oneTwoThree = 133;", 15233 Alignment); 15234 verifyFormat("a &= 5;\n" 15235 "bcd *= 5;\n" 15236 "ghtyf += 5;\n" 15237 "dvfvdb -= 5;\n" 15238 "a /= 5;\n" 15239 "vdsvsv %= 5;\n" 15240 "sfdbddfbdfbb ^= 5;\n" 15241 "dvsdsv |= 5;\n" 15242 "int dsvvdvsdvvv = 123;", 15243 Alignment); 15244 verifyFormat("int i = 1, j = 10;\n" 15245 "something = 2000;", 15246 Alignment); 15247 verifyFormat("something = 2000;\n" 15248 "int i = 1, j = 10;\n", 15249 Alignment); 15250 verifyFormat("something = 2000;\n" 15251 "another = 911;\n" 15252 "int i = 1, j = 10;\n" 15253 "oneMore = 1;\n" 15254 "i = 2;", 15255 Alignment); 15256 verifyFormat("int a = 5;\n" 15257 "int one = 1;\n" 15258 "method();\n" 15259 "int oneTwoThree = 123;\n" 15260 "int oneTwo = 12;", 15261 Alignment); 15262 verifyFormat("int oneTwoThree = 123;\n" 15263 "int oneTwo = 12;\n" 15264 "method();\n", 15265 Alignment); 15266 verifyFormat("int oneTwoThree = 123; // comment\n" 15267 "int oneTwo = 12; // comment", 15268 Alignment); 15269 15270 // Bug 25167 15271 /* Uncomment when fixed 15272 verifyFormat("#if A\n" 15273 "#else\n" 15274 "int aaaaaaaa = 12;\n" 15275 "#endif\n" 15276 "#if B\n" 15277 "#else\n" 15278 "int a = 12;\n" 15279 "#endif\n", 15280 Alignment); 15281 verifyFormat("enum foo {\n" 15282 "#if A\n" 15283 "#else\n" 15284 " aaaaaaaa = 12;\n" 15285 "#endif\n" 15286 "#if B\n" 15287 "#else\n" 15288 " a = 12;\n" 15289 "#endif\n" 15290 "};\n", 15291 Alignment); 15292 */ 15293 15294 Alignment.MaxEmptyLinesToKeep = 10; 15295 /* Test alignment across empty lines */ 15296 EXPECT_EQ("int a = 5;\n" 15297 "\n" 15298 "int oneTwoThree = 123;", 15299 format("int a = 5;\n" 15300 "\n" 15301 "int oneTwoThree= 123;", 15302 Alignment)); 15303 EXPECT_EQ("int a = 5;\n" 15304 "int one = 1;\n" 15305 "\n" 15306 "int oneTwoThree = 123;", 15307 format("int a = 5;\n" 15308 "int one = 1;\n" 15309 "\n" 15310 "int oneTwoThree = 123;", 15311 Alignment)); 15312 EXPECT_EQ("int a = 5;\n" 15313 "int one = 1;\n" 15314 "\n" 15315 "int oneTwoThree = 123;\n" 15316 "int oneTwo = 12;", 15317 format("int a = 5;\n" 15318 "int one = 1;\n" 15319 "\n" 15320 "int oneTwoThree = 123;\n" 15321 "int oneTwo = 12;", 15322 Alignment)); 15323 15324 /* Test across comments */ 15325 EXPECT_EQ("int a = 5;\n" 15326 "/* block comment */\n" 15327 "int oneTwoThree = 123;", 15328 format("int a = 5;\n" 15329 "/* block comment */\n" 15330 "int oneTwoThree=123;", 15331 Alignment)); 15332 15333 EXPECT_EQ("int a = 5;\n" 15334 "// line comment\n" 15335 "int oneTwoThree = 123;", 15336 format("int a = 5;\n" 15337 "// line comment\n" 15338 "int oneTwoThree=123;", 15339 Alignment)); 15340 15341 /* Test across comments and newlines */ 15342 EXPECT_EQ("int a = 5;\n" 15343 "\n" 15344 "/* block comment */\n" 15345 "int oneTwoThree = 123;", 15346 format("int a = 5;\n" 15347 "\n" 15348 "/* block comment */\n" 15349 "int oneTwoThree=123;", 15350 Alignment)); 15351 15352 EXPECT_EQ("int a = 5;\n" 15353 "\n" 15354 "// line comment\n" 15355 "int oneTwoThree = 123;", 15356 format("int a = 5;\n" 15357 "\n" 15358 "// line comment\n" 15359 "int oneTwoThree=123;", 15360 Alignment)); 15361 15362 EXPECT_EQ("int a = 5;\n" 15363 "//\n" 15364 "// multi-line line comment\n" 15365 "//\n" 15366 "int oneTwoThree = 123;", 15367 format("int a = 5;\n" 15368 "//\n" 15369 "// multi-line line comment\n" 15370 "//\n" 15371 "int oneTwoThree=123;", 15372 Alignment)); 15373 15374 EXPECT_EQ("int a = 5;\n" 15375 "/*\n" 15376 " * multi-line block comment\n" 15377 " */\n" 15378 "int oneTwoThree = 123;", 15379 format("int a = 5;\n" 15380 "/*\n" 15381 " * multi-line block comment\n" 15382 " */\n" 15383 "int oneTwoThree=123;", 15384 Alignment)); 15385 15386 EXPECT_EQ("int a = 5;\n" 15387 "\n" 15388 "/* block comment */\n" 15389 "\n" 15390 "\n" 15391 "\n" 15392 "int oneTwoThree = 123;", 15393 format("int a = 5;\n" 15394 "\n" 15395 "/* block comment */\n" 15396 "\n" 15397 "\n" 15398 "\n" 15399 "int oneTwoThree=123;", 15400 Alignment)); 15401 15402 EXPECT_EQ("int a = 5;\n" 15403 "\n" 15404 "// line comment\n" 15405 "\n" 15406 "\n" 15407 "\n" 15408 "int oneTwoThree = 123;", 15409 format("int a = 5;\n" 15410 "\n" 15411 "// line comment\n" 15412 "\n" 15413 "\n" 15414 "\n" 15415 "int oneTwoThree=123;", 15416 Alignment)); 15417 15418 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 15419 verifyFormat("#define A \\\n" 15420 " int aaaa = 12; \\\n" 15421 " int b = 23; \\\n" 15422 " int ccc = 234; \\\n" 15423 " int dddddddddd = 2345;", 15424 Alignment); 15425 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 15426 verifyFormat("#define A \\\n" 15427 " int aaaa = 12; \\\n" 15428 " int b = 23; \\\n" 15429 " int ccc = 234; \\\n" 15430 " int dddddddddd = 2345;", 15431 Alignment); 15432 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 15433 verifyFormat("#define A " 15434 " \\\n" 15435 " int aaaa = 12; " 15436 " \\\n" 15437 " int b = 23; " 15438 " \\\n" 15439 " int ccc = 234; " 15440 " \\\n" 15441 " int dddddddddd = 2345;", 15442 Alignment); 15443 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 15444 "k = 4, int l = 5,\n" 15445 " int m = 6) {\n" 15446 " int j = 10;\n" 15447 " otherThing = 1;\n" 15448 "}", 15449 Alignment); 15450 verifyFormat("void SomeFunction(int parameter = 0) {\n" 15451 " int i = 1;\n" 15452 " int j = 2;\n" 15453 " int big = 10000;\n" 15454 "}", 15455 Alignment); 15456 verifyFormat("class C {\n" 15457 "public:\n" 15458 " int i = 1;\n" 15459 " virtual void f() = 0;\n" 15460 "};", 15461 Alignment); 15462 verifyFormat("int i = 1;\n" 15463 "if (SomeType t = getSomething()) {\n" 15464 "}\n" 15465 "int j = 2;\n" 15466 "int big = 10000;", 15467 Alignment); 15468 verifyFormat("int j = 7;\n" 15469 "for (int k = 0; k < N; ++k) {\n" 15470 "}\n" 15471 "int j = 2;\n" 15472 "int big = 10000;\n" 15473 "}", 15474 Alignment); 15475 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 15476 verifyFormat("int i = 1;\n" 15477 "LooooooooooongType loooooooooooooooooooooongVariable\n" 15478 " = someLooooooooooooooooongFunction();\n" 15479 "int j = 2;", 15480 Alignment); 15481 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 15482 verifyFormat("int i = 1;\n" 15483 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 15484 " someLooooooooooooooooongFunction();\n" 15485 "int j = 2;", 15486 Alignment); 15487 15488 verifyFormat("auto lambda = []() {\n" 15489 " auto i = 0;\n" 15490 " return 0;\n" 15491 "};\n" 15492 "int i = 0;\n" 15493 "auto v = type{\n" 15494 " i = 1, //\n" 15495 " (i = 2), //\n" 15496 " i = 3 //\n" 15497 "};", 15498 Alignment); 15499 15500 verifyFormat( 15501 "int i = 1;\n" 15502 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 15503 " loooooooooooooooooooooongParameterB);\n" 15504 "int j = 2;", 15505 Alignment); 15506 15507 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 15508 " typename B = very_long_type_name_1,\n" 15509 " typename T_2 = very_long_type_name_2>\n" 15510 "auto foo() {}\n", 15511 Alignment); 15512 verifyFormat("int a, b = 1;\n" 15513 "int c = 2;\n" 15514 "int dd = 3;\n", 15515 Alignment); 15516 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 15517 "float b[1][] = {{3.f}};\n", 15518 Alignment); 15519 verifyFormat("for (int i = 0; i < 1; i++)\n" 15520 " int x = 1;\n", 15521 Alignment); 15522 verifyFormat("for (i = 0; i < 1; i++)\n" 15523 " x = 1;\n" 15524 "y = 1;\n", 15525 Alignment); 15526 15527 Alignment.ReflowComments = true; 15528 Alignment.ColumnLimit = 50; 15529 EXPECT_EQ("int x = 0;\n" 15530 "int yy = 1; /// specificlennospace\n" 15531 "int zzz = 2;\n", 15532 format("int x = 0;\n" 15533 "int yy = 1; ///specificlennospace\n" 15534 "int zzz = 2;\n", 15535 Alignment)); 15536 } 15537 15538 TEST_F(FormatTest, AlignConsecutiveAssignments) { 15539 FormatStyle Alignment = getLLVMStyle(); 15540 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15541 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 15542 verifyFormat("int a = 5;\n" 15543 "int oneTwoThree = 123;", 15544 Alignment); 15545 verifyFormat("int a = 5;\n" 15546 "int oneTwoThree = 123;", 15547 Alignment); 15548 15549 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 15550 verifyFormat("int a = 5;\n" 15551 "int oneTwoThree = 123;", 15552 Alignment); 15553 verifyFormat("int a = method();\n" 15554 "int oneTwoThree = 133;", 15555 Alignment); 15556 verifyFormat("a &= 5;\n" 15557 "bcd *= 5;\n" 15558 "ghtyf += 5;\n" 15559 "dvfvdb -= 5;\n" 15560 "a /= 5;\n" 15561 "vdsvsv %= 5;\n" 15562 "sfdbddfbdfbb ^= 5;\n" 15563 "dvsdsv |= 5;\n" 15564 "int dsvvdvsdvvv = 123;", 15565 Alignment); 15566 verifyFormat("int i = 1, j = 10;\n" 15567 "something = 2000;", 15568 Alignment); 15569 verifyFormat("something = 2000;\n" 15570 "int i = 1, j = 10;\n", 15571 Alignment); 15572 verifyFormat("something = 2000;\n" 15573 "another = 911;\n" 15574 "int i = 1, j = 10;\n" 15575 "oneMore = 1;\n" 15576 "i = 2;", 15577 Alignment); 15578 verifyFormat("int a = 5;\n" 15579 "int one = 1;\n" 15580 "method();\n" 15581 "int oneTwoThree = 123;\n" 15582 "int oneTwo = 12;", 15583 Alignment); 15584 verifyFormat("int oneTwoThree = 123;\n" 15585 "int oneTwo = 12;\n" 15586 "method();\n", 15587 Alignment); 15588 verifyFormat("int oneTwoThree = 123; // comment\n" 15589 "int oneTwo = 12; // comment", 15590 Alignment); 15591 15592 // Bug 25167 15593 /* Uncomment when fixed 15594 verifyFormat("#if A\n" 15595 "#else\n" 15596 "int aaaaaaaa = 12;\n" 15597 "#endif\n" 15598 "#if B\n" 15599 "#else\n" 15600 "int a = 12;\n" 15601 "#endif\n", 15602 Alignment); 15603 verifyFormat("enum foo {\n" 15604 "#if A\n" 15605 "#else\n" 15606 " aaaaaaaa = 12;\n" 15607 "#endif\n" 15608 "#if B\n" 15609 "#else\n" 15610 " a = 12;\n" 15611 "#endif\n" 15612 "};\n", 15613 Alignment); 15614 */ 15615 15616 EXPECT_EQ("int a = 5;\n" 15617 "\n" 15618 "int oneTwoThree = 123;", 15619 format("int a = 5;\n" 15620 "\n" 15621 "int oneTwoThree= 123;", 15622 Alignment)); 15623 EXPECT_EQ("int a = 5;\n" 15624 "int one = 1;\n" 15625 "\n" 15626 "int oneTwoThree = 123;", 15627 format("int a = 5;\n" 15628 "int one = 1;\n" 15629 "\n" 15630 "int oneTwoThree = 123;", 15631 Alignment)); 15632 EXPECT_EQ("int a = 5;\n" 15633 "int one = 1;\n" 15634 "\n" 15635 "int oneTwoThree = 123;\n" 15636 "int oneTwo = 12;", 15637 format("int a = 5;\n" 15638 "int one = 1;\n" 15639 "\n" 15640 "int oneTwoThree = 123;\n" 15641 "int oneTwo = 12;", 15642 Alignment)); 15643 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 15644 verifyFormat("#define A \\\n" 15645 " int aaaa = 12; \\\n" 15646 " int b = 23; \\\n" 15647 " int ccc = 234; \\\n" 15648 " int dddddddddd = 2345;", 15649 Alignment); 15650 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 15651 verifyFormat("#define A \\\n" 15652 " int aaaa = 12; \\\n" 15653 " int b = 23; \\\n" 15654 " int ccc = 234; \\\n" 15655 " int dddddddddd = 2345;", 15656 Alignment); 15657 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 15658 verifyFormat("#define A " 15659 " \\\n" 15660 " int aaaa = 12; " 15661 " \\\n" 15662 " int b = 23; " 15663 " \\\n" 15664 " int ccc = 234; " 15665 " \\\n" 15666 " int dddddddddd = 2345;", 15667 Alignment); 15668 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 15669 "k = 4, int l = 5,\n" 15670 " int m = 6) {\n" 15671 " int j = 10;\n" 15672 " otherThing = 1;\n" 15673 "}", 15674 Alignment); 15675 verifyFormat("void SomeFunction(int parameter = 0) {\n" 15676 " int i = 1;\n" 15677 " int j = 2;\n" 15678 " int big = 10000;\n" 15679 "}", 15680 Alignment); 15681 verifyFormat("class C {\n" 15682 "public:\n" 15683 " int i = 1;\n" 15684 " virtual void f() = 0;\n" 15685 "};", 15686 Alignment); 15687 verifyFormat("int i = 1;\n" 15688 "if (SomeType t = getSomething()) {\n" 15689 "}\n" 15690 "int j = 2;\n" 15691 "int big = 10000;", 15692 Alignment); 15693 verifyFormat("int j = 7;\n" 15694 "for (int k = 0; k < N; ++k) {\n" 15695 "}\n" 15696 "int j = 2;\n" 15697 "int big = 10000;\n" 15698 "}", 15699 Alignment); 15700 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 15701 verifyFormat("int i = 1;\n" 15702 "LooooooooooongType loooooooooooooooooooooongVariable\n" 15703 " = someLooooooooooooooooongFunction();\n" 15704 "int j = 2;", 15705 Alignment); 15706 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 15707 verifyFormat("int i = 1;\n" 15708 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 15709 " someLooooooooooooooooongFunction();\n" 15710 "int j = 2;", 15711 Alignment); 15712 15713 verifyFormat("auto lambda = []() {\n" 15714 " auto i = 0;\n" 15715 " return 0;\n" 15716 "};\n" 15717 "int i = 0;\n" 15718 "auto v = type{\n" 15719 " i = 1, //\n" 15720 " (i = 2), //\n" 15721 " i = 3 //\n" 15722 "};", 15723 Alignment); 15724 15725 verifyFormat( 15726 "int i = 1;\n" 15727 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 15728 " loooooooooooooooooooooongParameterB);\n" 15729 "int j = 2;", 15730 Alignment); 15731 15732 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 15733 " typename B = very_long_type_name_1,\n" 15734 " typename T_2 = very_long_type_name_2>\n" 15735 "auto foo() {}\n", 15736 Alignment); 15737 verifyFormat("int a, b = 1;\n" 15738 "int c = 2;\n" 15739 "int dd = 3;\n", 15740 Alignment); 15741 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 15742 "float b[1][] = {{3.f}};\n", 15743 Alignment); 15744 verifyFormat("for (int i = 0; i < 1; i++)\n" 15745 " int x = 1;\n", 15746 Alignment); 15747 verifyFormat("for (i = 0; i < 1; i++)\n" 15748 " x = 1;\n" 15749 "y = 1;\n", 15750 Alignment); 15751 15752 Alignment.ReflowComments = true; 15753 Alignment.ColumnLimit = 50; 15754 EXPECT_EQ("int x = 0;\n" 15755 "int yy = 1; /// specificlennospace\n" 15756 "int zzz = 2;\n", 15757 format("int x = 0;\n" 15758 "int yy = 1; ///specificlennospace\n" 15759 "int zzz = 2;\n", 15760 Alignment)); 15761 } 15762 15763 TEST_F(FormatTest, AlignConsecutiveBitFields) { 15764 FormatStyle Alignment = getLLVMStyle(); 15765 Alignment.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive; 15766 verifyFormat("int const a : 5;\n" 15767 "int oneTwoThree : 23;", 15768 Alignment); 15769 15770 // Initializers are allowed starting with c++2a 15771 verifyFormat("int const a : 5 = 1;\n" 15772 "int oneTwoThree : 23 = 0;", 15773 Alignment); 15774 15775 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 15776 verifyFormat("int const a : 5;\n" 15777 "int oneTwoThree : 23;", 15778 Alignment); 15779 15780 verifyFormat("int const a : 5; // comment\n" 15781 "int oneTwoThree : 23; // comment", 15782 Alignment); 15783 15784 verifyFormat("int const a : 5 = 1;\n" 15785 "int oneTwoThree : 23 = 0;", 15786 Alignment); 15787 15788 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 15789 verifyFormat("int const a : 5 = 1;\n" 15790 "int oneTwoThree : 23 = 0;", 15791 Alignment); 15792 verifyFormat("int const a : 5 = {1};\n" 15793 "int oneTwoThree : 23 = 0;", 15794 Alignment); 15795 15796 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None; 15797 verifyFormat("int const a :5;\n" 15798 "int oneTwoThree:23;", 15799 Alignment); 15800 15801 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before; 15802 verifyFormat("int const a :5;\n" 15803 "int oneTwoThree :23;", 15804 Alignment); 15805 15806 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After; 15807 verifyFormat("int const a : 5;\n" 15808 "int oneTwoThree: 23;", 15809 Alignment); 15810 15811 // Known limitations: ':' is only recognized as a bitfield colon when 15812 // followed by a number. 15813 /* 15814 verifyFormat("int oneTwoThree : SOME_CONSTANT;\n" 15815 "int a : 5;", 15816 Alignment); 15817 */ 15818 } 15819 15820 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 15821 FormatStyle Alignment = getLLVMStyle(); 15822 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15823 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_None; 15824 Alignment.PointerAlignment = FormatStyle::PAS_Right; 15825 verifyFormat("float const a = 5;\n" 15826 "int oneTwoThree = 123;", 15827 Alignment); 15828 verifyFormat("int a = 5;\n" 15829 "float const oneTwoThree = 123;", 15830 Alignment); 15831 15832 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 15833 verifyFormat("float const a = 5;\n" 15834 "int oneTwoThree = 123;", 15835 Alignment); 15836 verifyFormat("int a = method();\n" 15837 "float const oneTwoThree = 133;", 15838 Alignment); 15839 verifyFormat("int i = 1, j = 10;\n" 15840 "something = 2000;", 15841 Alignment); 15842 verifyFormat("something = 2000;\n" 15843 "int i = 1, j = 10;\n", 15844 Alignment); 15845 verifyFormat("float something = 2000;\n" 15846 "double another = 911;\n" 15847 "int i = 1, j = 10;\n" 15848 "const int *oneMore = 1;\n" 15849 "unsigned i = 2;", 15850 Alignment); 15851 verifyFormat("float a = 5;\n" 15852 "int one = 1;\n" 15853 "method();\n" 15854 "const double oneTwoThree = 123;\n" 15855 "const unsigned int oneTwo = 12;", 15856 Alignment); 15857 verifyFormat("int oneTwoThree{0}; // comment\n" 15858 "unsigned oneTwo; // comment", 15859 Alignment); 15860 verifyFormat("unsigned int *a;\n" 15861 "int *b;\n" 15862 "unsigned int Const *c;\n" 15863 "unsigned int const *d;\n" 15864 "unsigned int Const &e;\n" 15865 "unsigned int const &f;", 15866 Alignment); 15867 verifyFormat("Const unsigned int *c;\n" 15868 "const unsigned int *d;\n" 15869 "Const unsigned int &e;\n" 15870 "const unsigned int &f;\n" 15871 "const unsigned g;\n" 15872 "Const unsigned h;", 15873 Alignment); 15874 EXPECT_EQ("float const a = 5;\n" 15875 "\n" 15876 "int oneTwoThree = 123;", 15877 format("float const a = 5;\n" 15878 "\n" 15879 "int oneTwoThree= 123;", 15880 Alignment)); 15881 EXPECT_EQ("float a = 5;\n" 15882 "int one = 1;\n" 15883 "\n" 15884 "unsigned oneTwoThree = 123;", 15885 format("float a = 5;\n" 15886 "int one = 1;\n" 15887 "\n" 15888 "unsigned oneTwoThree = 123;", 15889 Alignment)); 15890 EXPECT_EQ("float a = 5;\n" 15891 "int one = 1;\n" 15892 "\n" 15893 "unsigned oneTwoThree = 123;\n" 15894 "int oneTwo = 12;", 15895 format("float a = 5;\n" 15896 "int one = 1;\n" 15897 "\n" 15898 "unsigned oneTwoThree = 123;\n" 15899 "int oneTwo = 12;", 15900 Alignment)); 15901 // Function prototype alignment 15902 verifyFormat("int a();\n" 15903 "double b();", 15904 Alignment); 15905 verifyFormat("int a(int x);\n" 15906 "double b();", 15907 Alignment); 15908 unsigned OldColumnLimit = Alignment.ColumnLimit; 15909 // We need to set ColumnLimit to zero, in order to stress nested alignments, 15910 // otherwise the function parameters will be re-flowed onto a single line. 15911 Alignment.ColumnLimit = 0; 15912 EXPECT_EQ("int a(int x,\n" 15913 " float y);\n" 15914 "double b(int x,\n" 15915 " double y);", 15916 format("int a(int x,\n" 15917 " float y);\n" 15918 "double b(int x,\n" 15919 " double y);", 15920 Alignment)); 15921 // This ensures that function parameters of function declarations are 15922 // correctly indented when their owning functions are indented. 15923 // The failure case here is for 'double y' to not be indented enough. 15924 EXPECT_EQ("double a(int x);\n" 15925 "int b(int y,\n" 15926 " double z);", 15927 format("double a(int x);\n" 15928 "int b(int y,\n" 15929 " double z);", 15930 Alignment)); 15931 // Set ColumnLimit low so that we induce wrapping immediately after 15932 // the function name and opening paren. 15933 Alignment.ColumnLimit = 13; 15934 verifyFormat("int function(\n" 15935 " int x,\n" 15936 " bool y);", 15937 Alignment); 15938 Alignment.ColumnLimit = OldColumnLimit; 15939 // Ensure function pointers don't screw up recursive alignment 15940 verifyFormat("int a(int x, void (*fp)(int y));\n" 15941 "double b();", 15942 Alignment); 15943 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 15944 // Ensure recursive alignment is broken by function braces, so that the 15945 // "a = 1" does not align with subsequent assignments inside the function 15946 // body. 15947 verifyFormat("int func(int a = 1) {\n" 15948 " int b = 2;\n" 15949 " int cc = 3;\n" 15950 "}", 15951 Alignment); 15952 verifyFormat("float something = 2000;\n" 15953 "double another = 911;\n" 15954 "int i = 1, j = 10;\n" 15955 "const int *oneMore = 1;\n" 15956 "unsigned i = 2;", 15957 Alignment); 15958 verifyFormat("int oneTwoThree = {0}; // comment\n" 15959 "unsigned oneTwo = 0; // comment", 15960 Alignment); 15961 // Make sure that scope is correctly tracked, in the absence of braces 15962 verifyFormat("for (int i = 0; i < n; i++)\n" 15963 " j = i;\n" 15964 "double x = 1;\n", 15965 Alignment); 15966 verifyFormat("if (int i = 0)\n" 15967 " j = i;\n" 15968 "double x = 1;\n", 15969 Alignment); 15970 // Ensure operator[] and operator() are comprehended 15971 verifyFormat("struct test {\n" 15972 " long long int foo();\n" 15973 " int operator[](int a);\n" 15974 " double bar();\n" 15975 "};\n", 15976 Alignment); 15977 verifyFormat("struct test {\n" 15978 " long long int foo();\n" 15979 " int operator()(int a);\n" 15980 " double bar();\n" 15981 "};\n", 15982 Alignment); 15983 15984 // PAS_Right 15985 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 15986 " int const i = 1;\n" 15987 " int *j = 2;\n" 15988 " int big = 10000;\n" 15989 "\n" 15990 " unsigned oneTwoThree = 123;\n" 15991 " int oneTwo = 12;\n" 15992 " method();\n" 15993 " float k = 2;\n" 15994 " int ll = 10000;\n" 15995 "}", 15996 format("void SomeFunction(int parameter= 0) {\n" 15997 " int const i= 1;\n" 15998 " int *j=2;\n" 15999 " int big = 10000;\n" 16000 "\n" 16001 "unsigned oneTwoThree =123;\n" 16002 "int oneTwo = 12;\n" 16003 " method();\n" 16004 "float k= 2;\n" 16005 "int ll=10000;\n" 16006 "}", 16007 Alignment)); 16008 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16009 " int const i = 1;\n" 16010 " int **j = 2, ***k;\n" 16011 " int &k = i;\n" 16012 " int &&l = i + j;\n" 16013 " int big = 10000;\n" 16014 "\n" 16015 " unsigned oneTwoThree = 123;\n" 16016 " int oneTwo = 12;\n" 16017 " method();\n" 16018 " float k = 2;\n" 16019 " int ll = 10000;\n" 16020 "}", 16021 format("void SomeFunction(int parameter= 0) {\n" 16022 " int const i= 1;\n" 16023 " int **j=2,***k;\n" 16024 "int &k=i;\n" 16025 "int &&l=i+j;\n" 16026 " int big = 10000;\n" 16027 "\n" 16028 "unsigned oneTwoThree =123;\n" 16029 "int oneTwo = 12;\n" 16030 " method();\n" 16031 "float k= 2;\n" 16032 "int ll=10000;\n" 16033 "}", 16034 Alignment)); 16035 // variables are aligned at their name, pointers are at the right most 16036 // position 16037 verifyFormat("int *a;\n" 16038 "int **b;\n" 16039 "int ***c;\n" 16040 "int foobar;\n", 16041 Alignment); 16042 16043 // PAS_Left 16044 FormatStyle AlignmentLeft = Alignment; 16045 AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left; 16046 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16047 " int const i = 1;\n" 16048 " int* j = 2;\n" 16049 " int big = 10000;\n" 16050 "\n" 16051 " unsigned oneTwoThree = 123;\n" 16052 " int oneTwo = 12;\n" 16053 " method();\n" 16054 " float k = 2;\n" 16055 " int ll = 10000;\n" 16056 "}", 16057 format("void SomeFunction(int parameter= 0) {\n" 16058 " int const i= 1;\n" 16059 " int *j=2;\n" 16060 " int big = 10000;\n" 16061 "\n" 16062 "unsigned oneTwoThree =123;\n" 16063 "int oneTwo = 12;\n" 16064 " method();\n" 16065 "float k= 2;\n" 16066 "int ll=10000;\n" 16067 "}", 16068 AlignmentLeft)); 16069 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16070 " int const i = 1;\n" 16071 " int** j = 2;\n" 16072 " int& k = i;\n" 16073 " int&& l = i + j;\n" 16074 " int big = 10000;\n" 16075 "\n" 16076 " unsigned oneTwoThree = 123;\n" 16077 " int oneTwo = 12;\n" 16078 " method();\n" 16079 " float k = 2;\n" 16080 " int ll = 10000;\n" 16081 "}", 16082 format("void SomeFunction(int parameter= 0) {\n" 16083 " int const i= 1;\n" 16084 " int **j=2;\n" 16085 "int &k=i;\n" 16086 "int &&l=i+j;\n" 16087 " int big = 10000;\n" 16088 "\n" 16089 "unsigned oneTwoThree =123;\n" 16090 "int oneTwo = 12;\n" 16091 " method();\n" 16092 "float k= 2;\n" 16093 "int ll=10000;\n" 16094 "}", 16095 AlignmentLeft)); 16096 // variables are aligned at their name, pointers are at the left most position 16097 verifyFormat("int* a;\n" 16098 "int** b;\n" 16099 "int*** c;\n" 16100 "int foobar;\n", 16101 AlignmentLeft); 16102 16103 // PAS_Middle 16104 FormatStyle AlignmentMiddle = Alignment; 16105 AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle; 16106 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16107 " int const i = 1;\n" 16108 " int * j = 2;\n" 16109 " int big = 10000;\n" 16110 "\n" 16111 " unsigned oneTwoThree = 123;\n" 16112 " int oneTwo = 12;\n" 16113 " method();\n" 16114 " float k = 2;\n" 16115 " int ll = 10000;\n" 16116 "}", 16117 format("void SomeFunction(int parameter= 0) {\n" 16118 " int const i= 1;\n" 16119 " int *j=2;\n" 16120 " int big = 10000;\n" 16121 "\n" 16122 "unsigned oneTwoThree =123;\n" 16123 "int oneTwo = 12;\n" 16124 " method();\n" 16125 "float k= 2;\n" 16126 "int ll=10000;\n" 16127 "}", 16128 AlignmentMiddle)); 16129 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16130 " int const i = 1;\n" 16131 " int ** j = 2, ***k;\n" 16132 " int & k = i;\n" 16133 " int && l = i + j;\n" 16134 " int big = 10000;\n" 16135 "\n" 16136 " unsigned oneTwoThree = 123;\n" 16137 " int oneTwo = 12;\n" 16138 " method();\n" 16139 " float k = 2;\n" 16140 " int ll = 10000;\n" 16141 "}", 16142 format("void SomeFunction(int parameter= 0) {\n" 16143 " int const i= 1;\n" 16144 " int **j=2,***k;\n" 16145 "int &k=i;\n" 16146 "int &&l=i+j;\n" 16147 " int big = 10000;\n" 16148 "\n" 16149 "unsigned oneTwoThree =123;\n" 16150 "int oneTwo = 12;\n" 16151 " method();\n" 16152 "float k= 2;\n" 16153 "int ll=10000;\n" 16154 "}", 16155 AlignmentMiddle)); 16156 // variables are aligned at their name, pointers are in the middle 16157 verifyFormat("int * a;\n" 16158 "int * b;\n" 16159 "int *** c;\n" 16160 "int foobar;\n", 16161 AlignmentMiddle); 16162 16163 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16164 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 16165 verifyFormat("#define A \\\n" 16166 " int aaaa = 12; \\\n" 16167 " float b = 23; \\\n" 16168 " const int ccc = 234; \\\n" 16169 " unsigned dddddddddd = 2345;", 16170 Alignment); 16171 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 16172 verifyFormat("#define A \\\n" 16173 " int aaaa = 12; \\\n" 16174 " float b = 23; \\\n" 16175 " const int ccc = 234; \\\n" 16176 " unsigned dddddddddd = 2345;", 16177 Alignment); 16178 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 16179 Alignment.ColumnLimit = 30; 16180 verifyFormat("#define A \\\n" 16181 " int aaaa = 12; \\\n" 16182 " float b = 23; \\\n" 16183 " const int ccc = 234; \\\n" 16184 " int dddddddddd = 2345;", 16185 Alignment); 16186 Alignment.ColumnLimit = 80; 16187 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 16188 "k = 4, int l = 5,\n" 16189 " int m = 6) {\n" 16190 " const int j = 10;\n" 16191 " otherThing = 1;\n" 16192 "}", 16193 Alignment); 16194 verifyFormat("void SomeFunction(int parameter = 0) {\n" 16195 " int const i = 1;\n" 16196 " int *j = 2;\n" 16197 " int big = 10000;\n" 16198 "}", 16199 Alignment); 16200 verifyFormat("class C {\n" 16201 "public:\n" 16202 " int i = 1;\n" 16203 " virtual void f() = 0;\n" 16204 "};", 16205 Alignment); 16206 verifyFormat("float i = 1;\n" 16207 "if (SomeType t = getSomething()) {\n" 16208 "}\n" 16209 "const unsigned j = 2;\n" 16210 "int big = 10000;", 16211 Alignment); 16212 verifyFormat("float j = 7;\n" 16213 "for (int k = 0; k < N; ++k) {\n" 16214 "}\n" 16215 "unsigned j = 2;\n" 16216 "int big = 10000;\n" 16217 "}", 16218 Alignment); 16219 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 16220 verifyFormat("float i = 1;\n" 16221 "LooooooooooongType loooooooooooooooooooooongVariable\n" 16222 " = someLooooooooooooooooongFunction();\n" 16223 "int j = 2;", 16224 Alignment); 16225 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 16226 verifyFormat("int i = 1;\n" 16227 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 16228 " someLooooooooooooooooongFunction();\n" 16229 "int j = 2;", 16230 Alignment); 16231 16232 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16233 verifyFormat("auto lambda = []() {\n" 16234 " auto ii = 0;\n" 16235 " float j = 0;\n" 16236 " return 0;\n" 16237 "};\n" 16238 "int i = 0;\n" 16239 "float i2 = 0;\n" 16240 "auto v = type{\n" 16241 " i = 1, //\n" 16242 " (i = 2), //\n" 16243 " i = 3 //\n" 16244 "};", 16245 Alignment); 16246 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16247 16248 verifyFormat( 16249 "int i = 1;\n" 16250 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 16251 " loooooooooooooooooooooongParameterB);\n" 16252 "int j = 2;", 16253 Alignment); 16254 16255 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 16256 // We expect declarations and assignments to align, as long as it doesn't 16257 // exceed the column limit, starting a new alignment sequence whenever it 16258 // happens. 16259 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16260 Alignment.ColumnLimit = 30; 16261 verifyFormat("float ii = 1;\n" 16262 "unsigned j = 2;\n" 16263 "int someVerylongVariable = 1;\n" 16264 "AnotherLongType ll = 123456;\n" 16265 "VeryVeryLongType k = 2;\n" 16266 "int myvar = 1;", 16267 Alignment); 16268 Alignment.ColumnLimit = 80; 16269 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16270 16271 verifyFormat( 16272 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 16273 " typename LongType, typename B>\n" 16274 "auto foo() {}\n", 16275 Alignment); 16276 verifyFormat("float a, b = 1;\n" 16277 "int c = 2;\n" 16278 "int dd = 3;\n", 16279 Alignment); 16280 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 16281 "float b[1][] = {{3.f}};\n", 16282 Alignment); 16283 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16284 verifyFormat("float a, b = 1;\n" 16285 "int c = 2;\n" 16286 "int dd = 3;\n", 16287 Alignment); 16288 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 16289 "float b[1][] = {{3.f}};\n", 16290 Alignment); 16291 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16292 16293 Alignment.ColumnLimit = 30; 16294 Alignment.BinPackParameters = false; 16295 verifyFormat("void foo(float a,\n" 16296 " float b,\n" 16297 " int c,\n" 16298 " uint32_t *d) {\n" 16299 " int *e = 0;\n" 16300 " float f = 0;\n" 16301 " double g = 0;\n" 16302 "}\n" 16303 "void bar(ino_t a,\n" 16304 " int b,\n" 16305 " uint32_t *c,\n" 16306 " bool d) {}\n", 16307 Alignment); 16308 Alignment.BinPackParameters = true; 16309 Alignment.ColumnLimit = 80; 16310 16311 // Bug 33507 16312 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 16313 verifyFormat( 16314 "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n" 16315 " static const Version verVs2017;\n" 16316 " return true;\n" 16317 "});\n", 16318 Alignment); 16319 Alignment.PointerAlignment = FormatStyle::PAS_Right; 16320 16321 // See llvm.org/PR35641 16322 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16323 verifyFormat("int func() { //\n" 16324 " int b;\n" 16325 " unsigned c;\n" 16326 "}", 16327 Alignment); 16328 16329 // See PR37175 16330 FormatStyle Style = getMozillaStyle(); 16331 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16332 EXPECT_EQ("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n" 16333 "foo(int a);", 16334 format("DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style)); 16335 16336 Alignment.PointerAlignment = FormatStyle::PAS_Left; 16337 verifyFormat("unsigned int* a;\n" 16338 "int* b;\n" 16339 "unsigned int Const* c;\n" 16340 "unsigned int const* d;\n" 16341 "unsigned int Const& e;\n" 16342 "unsigned int const& f;", 16343 Alignment); 16344 verifyFormat("Const unsigned int* c;\n" 16345 "const unsigned int* d;\n" 16346 "Const unsigned int& e;\n" 16347 "const unsigned int& f;\n" 16348 "const unsigned g;\n" 16349 "Const unsigned h;", 16350 Alignment); 16351 16352 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 16353 verifyFormat("unsigned int * a;\n" 16354 "int * b;\n" 16355 "unsigned int Const * c;\n" 16356 "unsigned int const * d;\n" 16357 "unsigned int Const & e;\n" 16358 "unsigned int const & f;", 16359 Alignment); 16360 verifyFormat("Const unsigned int * c;\n" 16361 "const unsigned int * d;\n" 16362 "Const unsigned int & e;\n" 16363 "const unsigned int & f;\n" 16364 "const unsigned g;\n" 16365 "Const unsigned h;", 16366 Alignment); 16367 } 16368 16369 TEST_F(FormatTest, AlignWithLineBreaks) { 16370 auto Style = getLLVMStyleWithColumns(120); 16371 16372 EXPECT_EQ(Style.AlignConsecutiveAssignments, FormatStyle::ACS_None); 16373 EXPECT_EQ(Style.AlignConsecutiveDeclarations, FormatStyle::ACS_None); 16374 verifyFormat("void foo() {\n" 16375 " int myVar = 5;\n" 16376 " double x = 3.14;\n" 16377 " auto str = \"Hello \"\n" 16378 " \"World\";\n" 16379 " auto s = \"Hello \"\n" 16380 " \"Again\";\n" 16381 "}", 16382 Style); 16383 16384 // clang-format off 16385 verifyFormat("void foo() {\n" 16386 " const int capacityBefore = Entries.capacity();\n" 16387 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16388 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16389 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16390 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16391 "}", 16392 Style); 16393 // clang-format on 16394 16395 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16396 verifyFormat("void foo() {\n" 16397 " int myVar = 5;\n" 16398 " double x = 3.14;\n" 16399 " auto str = \"Hello \"\n" 16400 " \"World\";\n" 16401 " auto s = \"Hello \"\n" 16402 " \"Again\";\n" 16403 "}", 16404 Style); 16405 16406 // clang-format off 16407 verifyFormat("void foo() {\n" 16408 " const int capacityBefore = Entries.capacity();\n" 16409 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16410 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16411 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16412 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16413 "}", 16414 Style); 16415 // clang-format on 16416 16417 Style.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16418 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16419 verifyFormat("void foo() {\n" 16420 " int myVar = 5;\n" 16421 " double x = 3.14;\n" 16422 " auto str = \"Hello \"\n" 16423 " \"World\";\n" 16424 " auto s = \"Hello \"\n" 16425 " \"Again\";\n" 16426 "}", 16427 Style); 16428 16429 // clang-format off 16430 verifyFormat("void foo() {\n" 16431 " const int capacityBefore = Entries.capacity();\n" 16432 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16433 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16434 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16435 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16436 "}", 16437 Style); 16438 // clang-format on 16439 16440 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16441 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16442 16443 verifyFormat("void foo() {\n" 16444 " int myVar = 5;\n" 16445 " double x = 3.14;\n" 16446 " auto str = \"Hello \"\n" 16447 " \"World\";\n" 16448 " auto s = \"Hello \"\n" 16449 " \"Again\";\n" 16450 "}", 16451 Style); 16452 16453 // clang-format off 16454 verifyFormat("void foo() {\n" 16455 " const int capacityBefore = Entries.capacity();\n" 16456 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16457 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16458 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16459 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16460 "}", 16461 Style); 16462 // clang-format on 16463 16464 Style = getLLVMStyleWithColumns(120); 16465 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16466 Style.ContinuationIndentWidth = 4; 16467 Style.IndentWidth = 4; 16468 16469 // clang-format off 16470 verifyFormat("void SomeFunc() {\n" 16471 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 16472 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16473 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 16474 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16475 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 16476 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16477 "}", 16478 Style); 16479 // clang-format on 16480 16481 Style.BinPackArguments = false; 16482 16483 // clang-format off 16484 verifyFormat("void SomeFunc() {\n" 16485 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(\n" 16486 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16487 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(\n" 16488 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16489 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(\n" 16490 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16491 "}", 16492 Style); 16493 // clang-format on 16494 } 16495 16496 TEST_F(FormatTest, AlignWithInitializerPeriods) { 16497 auto Style = getLLVMStyleWithColumns(60); 16498 16499 verifyFormat("void foo1(void) {\n" 16500 " BYTE p[1] = 1;\n" 16501 " A B = {.one_foooooooooooooooo = 2,\n" 16502 " .two_fooooooooooooo = 3,\n" 16503 " .three_fooooooooooooo = 4};\n" 16504 " BYTE payload = 2;\n" 16505 "}", 16506 Style); 16507 16508 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16509 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_None; 16510 verifyFormat("void foo2(void) {\n" 16511 " BYTE p[1] = 1;\n" 16512 " A B = {.one_foooooooooooooooo = 2,\n" 16513 " .two_fooooooooooooo = 3,\n" 16514 " .three_fooooooooooooo = 4};\n" 16515 " BYTE payload = 2;\n" 16516 "}", 16517 Style); 16518 16519 Style.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16520 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16521 verifyFormat("void foo3(void) {\n" 16522 " BYTE p[1] = 1;\n" 16523 " A B = {.one_foooooooooooooooo = 2,\n" 16524 " .two_fooooooooooooo = 3,\n" 16525 " .three_fooooooooooooo = 4};\n" 16526 " BYTE payload = 2;\n" 16527 "}", 16528 Style); 16529 16530 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16531 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16532 verifyFormat("void foo4(void) {\n" 16533 " BYTE p[1] = 1;\n" 16534 " A B = {.one_foooooooooooooooo = 2,\n" 16535 " .two_fooooooooooooo = 3,\n" 16536 " .three_fooooooooooooo = 4};\n" 16537 " BYTE payload = 2;\n" 16538 "}", 16539 Style); 16540 } 16541 16542 TEST_F(FormatTest, LinuxBraceBreaking) { 16543 FormatStyle LinuxBraceStyle = getLLVMStyle(); 16544 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 16545 verifyFormat("namespace a\n" 16546 "{\n" 16547 "class A\n" 16548 "{\n" 16549 " void f()\n" 16550 " {\n" 16551 " if (true) {\n" 16552 " a();\n" 16553 " b();\n" 16554 " } else {\n" 16555 " a();\n" 16556 " }\n" 16557 " }\n" 16558 " void g() { return; }\n" 16559 "};\n" 16560 "struct B {\n" 16561 " int x;\n" 16562 "};\n" 16563 "} // namespace a\n", 16564 LinuxBraceStyle); 16565 verifyFormat("enum X {\n" 16566 " Y = 0,\n" 16567 "}\n", 16568 LinuxBraceStyle); 16569 verifyFormat("struct S {\n" 16570 " int Type;\n" 16571 " union {\n" 16572 " int x;\n" 16573 " double y;\n" 16574 " } Value;\n" 16575 " class C\n" 16576 " {\n" 16577 " MyFavoriteType Value;\n" 16578 " } Class;\n" 16579 "}\n", 16580 LinuxBraceStyle); 16581 } 16582 16583 TEST_F(FormatTest, MozillaBraceBreaking) { 16584 FormatStyle MozillaBraceStyle = getLLVMStyle(); 16585 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 16586 MozillaBraceStyle.FixNamespaceComments = false; 16587 verifyFormat("namespace a {\n" 16588 "class A\n" 16589 "{\n" 16590 " void f()\n" 16591 " {\n" 16592 " if (true) {\n" 16593 " a();\n" 16594 " b();\n" 16595 " }\n" 16596 " }\n" 16597 " void g() { return; }\n" 16598 "};\n" 16599 "enum E\n" 16600 "{\n" 16601 " A,\n" 16602 " // foo\n" 16603 " B,\n" 16604 " C\n" 16605 "};\n" 16606 "struct B\n" 16607 "{\n" 16608 " int x;\n" 16609 "};\n" 16610 "}\n", 16611 MozillaBraceStyle); 16612 verifyFormat("struct S\n" 16613 "{\n" 16614 " int Type;\n" 16615 " union\n" 16616 " {\n" 16617 " int x;\n" 16618 " double y;\n" 16619 " } Value;\n" 16620 " class C\n" 16621 " {\n" 16622 " MyFavoriteType Value;\n" 16623 " } Class;\n" 16624 "}\n", 16625 MozillaBraceStyle); 16626 } 16627 16628 TEST_F(FormatTest, StroustrupBraceBreaking) { 16629 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 16630 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 16631 verifyFormat("namespace a {\n" 16632 "class A {\n" 16633 " void f()\n" 16634 " {\n" 16635 " if (true) {\n" 16636 " a();\n" 16637 " b();\n" 16638 " }\n" 16639 " }\n" 16640 " void g() { return; }\n" 16641 "};\n" 16642 "struct B {\n" 16643 " int x;\n" 16644 "};\n" 16645 "} // namespace a\n", 16646 StroustrupBraceStyle); 16647 16648 verifyFormat("void foo()\n" 16649 "{\n" 16650 " if (a) {\n" 16651 " a();\n" 16652 " }\n" 16653 " else {\n" 16654 " b();\n" 16655 " }\n" 16656 "}\n", 16657 StroustrupBraceStyle); 16658 16659 verifyFormat("#ifdef _DEBUG\n" 16660 "int foo(int i = 0)\n" 16661 "#else\n" 16662 "int foo(int i = 5)\n" 16663 "#endif\n" 16664 "{\n" 16665 " return i;\n" 16666 "}", 16667 StroustrupBraceStyle); 16668 16669 verifyFormat("void foo() {}\n" 16670 "void bar()\n" 16671 "#ifdef _DEBUG\n" 16672 "{\n" 16673 " foo();\n" 16674 "}\n" 16675 "#else\n" 16676 "{\n" 16677 "}\n" 16678 "#endif", 16679 StroustrupBraceStyle); 16680 16681 verifyFormat("void foobar() { int i = 5; }\n" 16682 "#ifdef _DEBUG\n" 16683 "void bar() {}\n" 16684 "#else\n" 16685 "void bar() { foobar(); }\n" 16686 "#endif", 16687 StroustrupBraceStyle); 16688 } 16689 16690 TEST_F(FormatTest, AllmanBraceBreaking) { 16691 FormatStyle AllmanBraceStyle = getLLVMStyle(); 16692 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 16693 16694 EXPECT_EQ("namespace a\n" 16695 "{\n" 16696 "void f();\n" 16697 "void g();\n" 16698 "} // namespace a\n", 16699 format("namespace a\n" 16700 "{\n" 16701 "void f();\n" 16702 "void g();\n" 16703 "}\n", 16704 AllmanBraceStyle)); 16705 16706 verifyFormat("namespace a\n" 16707 "{\n" 16708 "class A\n" 16709 "{\n" 16710 " void f()\n" 16711 " {\n" 16712 " if (true)\n" 16713 " {\n" 16714 " a();\n" 16715 " b();\n" 16716 " }\n" 16717 " }\n" 16718 " void g() { return; }\n" 16719 "};\n" 16720 "struct B\n" 16721 "{\n" 16722 " int x;\n" 16723 "};\n" 16724 "union C\n" 16725 "{\n" 16726 "};\n" 16727 "} // namespace a", 16728 AllmanBraceStyle); 16729 16730 verifyFormat("void f()\n" 16731 "{\n" 16732 " if (true)\n" 16733 " {\n" 16734 " a();\n" 16735 " }\n" 16736 " else if (false)\n" 16737 " {\n" 16738 " b();\n" 16739 " }\n" 16740 " else\n" 16741 " {\n" 16742 " c();\n" 16743 " }\n" 16744 "}\n", 16745 AllmanBraceStyle); 16746 16747 verifyFormat("void f()\n" 16748 "{\n" 16749 " for (int i = 0; i < 10; ++i)\n" 16750 " {\n" 16751 " a();\n" 16752 " }\n" 16753 " while (false)\n" 16754 " {\n" 16755 " b();\n" 16756 " }\n" 16757 " do\n" 16758 " {\n" 16759 " c();\n" 16760 " } while (false)\n" 16761 "}\n", 16762 AllmanBraceStyle); 16763 16764 verifyFormat("void f(int a)\n" 16765 "{\n" 16766 " switch (a)\n" 16767 " {\n" 16768 " case 0:\n" 16769 " break;\n" 16770 " case 1:\n" 16771 " {\n" 16772 " break;\n" 16773 " }\n" 16774 " case 2:\n" 16775 " {\n" 16776 " }\n" 16777 " break;\n" 16778 " default:\n" 16779 " break;\n" 16780 " }\n" 16781 "}\n", 16782 AllmanBraceStyle); 16783 16784 verifyFormat("enum X\n" 16785 "{\n" 16786 " Y = 0,\n" 16787 "}\n", 16788 AllmanBraceStyle); 16789 verifyFormat("enum X\n" 16790 "{\n" 16791 " Y = 0\n" 16792 "}\n", 16793 AllmanBraceStyle); 16794 16795 verifyFormat("@interface BSApplicationController ()\n" 16796 "{\n" 16797 "@private\n" 16798 " id _extraIvar;\n" 16799 "}\n" 16800 "@end\n", 16801 AllmanBraceStyle); 16802 16803 verifyFormat("#ifdef _DEBUG\n" 16804 "int foo(int i = 0)\n" 16805 "#else\n" 16806 "int foo(int i = 5)\n" 16807 "#endif\n" 16808 "{\n" 16809 " return i;\n" 16810 "}", 16811 AllmanBraceStyle); 16812 16813 verifyFormat("void foo() {}\n" 16814 "void bar()\n" 16815 "#ifdef _DEBUG\n" 16816 "{\n" 16817 " foo();\n" 16818 "}\n" 16819 "#else\n" 16820 "{\n" 16821 "}\n" 16822 "#endif", 16823 AllmanBraceStyle); 16824 16825 verifyFormat("void foobar() { int i = 5; }\n" 16826 "#ifdef _DEBUG\n" 16827 "void bar() {}\n" 16828 "#else\n" 16829 "void bar() { foobar(); }\n" 16830 "#endif", 16831 AllmanBraceStyle); 16832 16833 EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine, 16834 FormatStyle::SLS_All); 16835 16836 verifyFormat("[](int i) { return i + 2; };\n" 16837 "[](int i, int j)\n" 16838 "{\n" 16839 " auto x = i + j;\n" 16840 " auto y = i * j;\n" 16841 " return x ^ y;\n" 16842 "};\n" 16843 "void foo()\n" 16844 "{\n" 16845 " auto shortLambda = [](int i) { return i + 2; };\n" 16846 " auto longLambda = [](int i, int j)\n" 16847 " {\n" 16848 " auto x = i + j;\n" 16849 " auto y = i * j;\n" 16850 " return x ^ y;\n" 16851 " };\n" 16852 "}", 16853 AllmanBraceStyle); 16854 16855 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 16856 16857 verifyFormat("[](int i)\n" 16858 "{\n" 16859 " return i + 2;\n" 16860 "};\n" 16861 "[](int i, int j)\n" 16862 "{\n" 16863 " auto x = i + j;\n" 16864 " auto y = i * j;\n" 16865 " return x ^ y;\n" 16866 "};\n" 16867 "void foo()\n" 16868 "{\n" 16869 " auto shortLambda = [](int i)\n" 16870 " {\n" 16871 " return i + 2;\n" 16872 " };\n" 16873 " auto longLambda = [](int i, int j)\n" 16874 " {\n" 16875 " auto x = i + j;\n" 16876 " auto y = i * j;\n" 16877 " return x ^ y;\n" 16878 " };\n" 16879 "}", 16880 AllmanBraceStyle); 16881 16882 // Reset 16883 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All; 16884 16885 // This shouldn't affect ObjC blocks.. 16886 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 16887 " // ...\n" 16888 " int i;\n" 16889 "}];", 16890 AllmanBraceStyle); 16891 verifyFormat("void (^block)(void) = ^{\n" 16892 " // ...\n" 16893 " int i;\n" 16894 "};", 16895 AllmanBraceStyle); 16896 // .. or dict literals. 16897 verifyFormat("void f()\n" 16898 "{\n" 16899 " // ...\n" 16900 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 16901 "}", 16902 AllmanBraceStyle); 16903 verifyFormat("void f()\n" 16904 "{\n" 16905 " // ...\n" 16906 " [object someMethod:@{a : @\"b\"}];\n" 16907 "}", 16908 AllmanBraceStyle); 16909 verifyFormat("int f()\n" 16910 "{ // comment\n" 16911 " return 42;\n" 16912 "}", 16913 AllmanBraceStyle); 16914 16915 AllmanBraceStyle.ColumnLimit = 19; 16916 verifyFormat("void f() { int i; }", AllmanBraceStyle); 16917 AllmanBraceStyle.ColumnLimit = 18; 16918 verifyFormat("void f()\n" 16919 "{\n" 16920 " int i;\n" 16921 "}", 16922 AllmanBraceStyle); 16923 AllmanBraceStyle.ColumnLimit = 80; 16924 16925 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 16926 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = 16927 FormatStyle::SIS_WithoutElse; 16928 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 16929 verifyFormat("void f(bool b)\n" 16930 "{\n" 16931 " if (b)\n" 16932 " {\n" 16933 " return;\n" 16934 " }\n" 16935 "}\n", 16936 BreakBeforeBraceShortIfs); 16937 verifyFormat("void f(bool b)\n" 16938 "{\n" 16939 " if constexpr (b)\n" 16940 " {\n" 16941 " return;\n" 16942 " }\n" 16943 "}\n", 16944 BreakBeforeBraceShortIfs); 16945 verifyFormat("void f(bool b)\n" 16946 "{\n" 16947 " if CONSTEXPR (b)\n" 16948 " {\n" 16949 " return;\n" 16950 " }\n" 16951 "}\n", 16952 BreakBeforeBraceShortIfs); 16953 verifyFormat("void f(bool b)\n" 16954 "{\n" 16955 " if (b) return;\n" 16956 "}\n", 16957 BreakBeforeBraceShortIfs); 16958 verifyFormat("void f(bool b)\n" 16959 "{\n" 16960 " if constexpr (b) return;\n" 16961 "}\n", 16962 BreakBeforeBraceShortIfs); 16963 verifyFormat("void f(bool b)\n" 16964 "{\n" 16965 " if CONSTEXPR (b) return;\n" 16966 "}\n", 16967 BreakBeforeBraceShortIfs); 16968 verifyFormat("void f(bool b)\n" 16969 "{\n" 16970 " while (b)\n" 16971 " {\n" 16972 " return;\n" 16973 " }\n" 16974 "}\n", 16975 BreakBeforeBraceShortIfs); 16976 } 16977 16978 TEST_F(FormatTest, WhitesmithsBraceBreaking) { 16979 FormatStyle WhitesmithsBraceStyle = getLLVMStyle(); 16980 WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 16981 16982 // Make a few changes to the style for testing purposes 16983 WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine = 16984 FormatStyle::SFS_Empty; 16985 WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 16986 WhitesmithsBraceStyle.ColumnLimit = 0; 16987 16988 // FIXME: this test case can't decide whether there should be a blank line 16989 // after the ~D() line or not. It adds one if one doesn't exist in the test 16990 // and it removes the line if one exists. 16991 /* 16992 verifyFormat("class A;\n" 16993 "namespace B\n" 16994 " {\n" 16995 "class C;\n" 16996 "// Comment\n" 16997 "class D\n" 16998 " {\n" 16999 "public:\n" 17000 " D();\n" 17001 " ~D() {}\n" 17002 "private:\n" 17003 " enum E\n" 17004 " {\n" 17005 " F\n" 17006 " }\n" 17007 " };\n" 17008 " } // namespace B\n", 17009 WhitesmithsBraceStyle); 17010 */ 17011 17012 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None; 17013 verifyFormat("namespace a\n" 17014 " {\n" 17015 "class A\n" 17016 " {\n" 17017 " void f()\n" 17018 " {\n" 17019 " if (true)\n" 17020 " {\n" 17021 " a();\n" 17022 " b();\n" 17023 " }\n" 17024 " }\n" 17025 " void g()\n" 17026 " {\n" 17027 " return;\n" 17028 " }\n" 17029 " };\n" 17030 "struct B\n" 17031 " {\n" 17032 " int x;\n" 17033 " };\n" 17034 " } // namespace a", 17035 WhitesmithsBraceStyle); 17036 17037 verifyFormat("namespace a\n" 17038 " {\n" 17039 "namespace b\n" 17040 " {\n" 17041 "class A\n" 17042 " {\n" 17043 " void f()\n" 17044 " {\n" 17045 " if (true)\n" 17046 " {\n" 17047 " a();\n" 17048 " b();\n" 17049 " }\n" 17050 " }\n" 17051 " void g()\n" 17052 " {\n" 17053 " return;\n" 17054 " }\n" 17055 " };\n" 17056 "struct B\n" 17057 " {\n" 17058 " int x;\n" 17059 " };\n" 17060 " } // namespace b\n" 17061 " } // namespace a", 17062 WhitesmithsBraceStyle); 17063 17064 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner; 17065 verifyFormat("namespace a\n" 17066 " {\n" 17067 "namespace b\n" 17068 " {\n" 17069 " class A\n" 17070 " {\n" 17071 " void f()\n" 17072 " {\n" 17073 " if (true)\n" 17074 " {\n" 17075 " a();\n" 17076 " b();\n" 17077 " }\n" 17078 " }\n" 17079 " void g()\n" 17080 " {\n" 17081 " return;\n" 17082 " }\n" 17083 " };\n" 17084 " struct B\n" 17085 " {\n" 17086 " int x;\n" 17087 " };\n" 17088 " } // namespace b\n" 17089 " } // namespace a", 17090 WhitesmithsBraceStyle); 17091 17092 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All; 17093 verifyFormat("namespace a\n" 17094 " {\n" 17095 " namespace b\n" 17096 " {\n" 17097 " class A\n" 17098 " {\n" 17099 " void f()\n" 17100 " {\n" 17101 " if (true)\n" 17102 " {\n" 17103 " a();\n" 17104 " b();\n" 17105 " }\n" 17106 " }\n" 17107 " void g()\n" 17108 " {\n" 17109 " return;\n" 17110 " }\n" 17111 " };\n" 17112 " struct B\n" 17113 " {\n" 17114 " int x;\n" 17115 " };\n" 17116 " } // namespace b\n" 17117 " } // namespace a", 17118 WhitesmithsBraceStyle); 17119 17120 verifyFormat("void f()\n" 17121 " {\n" 17122 " if (true)\n" 17123 " {\n" 17124 " a();\n" 17125 " }\n" 17126 " else if (false)\n" 17127 " {\n" 17128 " b();\n" 17129 " }\n" 17130 " else\n" 17131 " {\n" 17132 " c();\n" 17133 " }\n" 17134 " }\n", 17135 WhitesmithsBraceStyle); 17136 17137 verifyFormat("void f()\n" 17138 " {\n" 17139 " for (int i = 0; i < 10; ++i)\n" 17140 " {\n" 17141 " a();\n" 17142 " }\n" 17143 " while (false)\n" 17144 " {\n" 17145 " b();\n" 17146 " }\n" 17147 " do\n" 17148 " {\n" 17149 " c();\n" 17150 " } while (false)\n" 17151 " }\n", 17152 WhitesmithsBraceStyle); 17153 17154 WhitesmithsBraceStyle.IndentCaseLabels = true; 17155 verifyFormat("void switchTest1(int a)\n" 17156 " {\n" 17157 " switch (a)\n" 17158 " {\n" 17159 " case 2:\n" 17160 " {\n" 17161 " }\n" 17162 " break;\n" 17163 " }\n" 17164 " }\n", 17165 WhitesmithsBraceStyle); 17166 17167 verifyFormat("void switchTest2(int a)\n" 17168 " {\n" 17169 " switch (a)\n" 17170 " {\n" 17171 " case 0:\n" 17172 " break;\n" 17173 " case 1:\n" 17174 " {\n" 17175 " break;\n" 17176 " }\n" 17177 " case 2:\n" 17178 " {\n" 17179 " }\n" 17180 " break;\n" 17181 " default:\n" 17182 " break;\n" 17183 " }\n" 17184 " }\n", 17185 WhitesmithsBraceStyle); 17186 17187 verifyFormat("void switchTest3(int a)\n" 17188 " {\n" 17189 " switch (a)\n" 17190 " {\n" 17191 " case 0:\n" 17192 " {\n" 17193 " foo(x);\n" 17194 " }\n" 17195 " break;\n" 17196 " default:\n" 17197 " {\n" 17198 " foo(1);\n" 17199 " }\n" 17200 " break;\n" 17201 " }\n" 17202 " }\n", 17203 WhitesmithsBraceStyle); 17204 17205 WhitesmithsBraceStyle.IndentCaseLabels = false; 17206 17207 verifyFormat("void switchTest4(int a)\n" 17208 " {\n" 17209 " switch (a)\n" 17210 " {\n" 17211 " case 2:\n" 17212 " {\n" 17213 " }\n" 17214 " break;\n" 17215 " }\n" 17216 " }\n", 17217 WhitesmithsBraceStyle); 17218 17219 verifyFormat("void switchTest5(int a)\n" 17220 " {\n" 17221 " switch (a)\n" 17222 " {\n" 17223 " case 0:\n" 17224 " break;\n" 17225 " case 1:\n" 17226 " {\n" 17227 " foo();\n" 17228 " break;\n" 17229 " }\n" 17230 " case 2:\n" 17231 " {\n" 17232 " }\n" 17233 " break;\n" 17234 " default:\n" 17235 " break;\n" 17236 " }\n" 17237 " }\n", 17238 WhitesmithsBraceStyle); 17239 17240 verifyFormat("void switchTest6(int a)\n" 17241 " {\n" 17242 " switch (a)\n" 17243 " {\n" 17244 " case 0:\n" 17245 " {\n" 17246 " foo(x);\n" 17247 " }\n" 17248 " break;\n" 17249 " default:\n" 17250 " {\n" 17251 " foo(1);\n" 17252 " }\n" 17253 " break;\n" 17254 " }\n" 17255 " }\n", 17256 WhitesmithsBraceStyle); 17257 17258 verifyFormat("enum X\n" 17259 " {\n" 17260 " Y = 0, // testing\n" 17261 " }\n", 17262 WhitesmithsBraceStyle); 17263 17264 verifyFormat("enum X\n" 17265 " {\n" 17266 " Y = 0\n" 17267 " }\n", 17268 WhitesmithsBraceStyle); 17269 verifyFormat("enum X\n" 17270 " {\n" 17271 " Y = 0,\n" 17272 " Z = 1\n" 17273 " };\n", 17274 WhitesmithsBraceStyle); 17275 17276 verifyFormat("@interface BSApplicationController ()\n" 17277 " {\n" 17278 "@private\n" 17279 " id _extraIvar;\n" 17280 " }\n" 17281 "@end\n", 17282 WhitesmithsBraceStyle); 17283 17284 verifyFormat("#ifdef _DEBUG\n" 17285 "int foo(int i = 0)\n" 17286 "#else\n" 17287 "int foo(int i = 5)\n" 17288 "#endif\n" 17289 " {\n" 17290 " return i;\n" 17291 " }", 17292 WhitesmithsBraceStyle); 17293 17294 verifyFormat("void foo() {}\n" 17295 "void bar()\n" 17296 "#ifdef _DEBUG\n" 17297 " {\n" 17298 " foo();\n" 17299 " }\n" 17300 "#else\n" 17301 " {\n" 17302 " }\n" 17303 "#endif", 17304 WhitesmithsBraceStyle); 17305 17306 verifyFormat("void foobar()\n" 17307 " {\n" 17308 " int i = 5;\n" 17309 " }\n" 17310 "#ifdef _DEBUG\n" 17311 "void bar()\n" 17312 " {\n" 17313 " }\n" 17314 "#else\n" 17315 "void bar()\n" 17316 " {\n" 17317 " foobar();\n" 17318 " }\n" 17319 "#endif", 17320 WhitesmithsBraceStyle); 17321 17322 // This shouldn't affect ObjC blocks.. 17323 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 17324 " // ...\n" 17325 " int i;\n" 17326 "}];", 17327 WhitesmithsBraceStyle); 17328 verifyFormat("void (^block)(void) = ^{\n" 17329 " // ...\n" 17330 " int i;\n" 17331 "};", 17332 WhitesmithsBraceStyle); 17333 // .. or dict literals. 17334 verifyFormat("void f()\n" 17335 " {\n" 17336 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 17337 " }", 17338 WhitesmithsBraceStyle); 17339 17340 verifyFormat("int f()\n" 17341 " { // comment\n" 17342 " return 42;\n" 17343 " }", 17344 WhitesmithsBraceStyle); 17345 17346 FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle; 17347 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = 17348 FormatStyle::SIS_OnlyFirstIf; 17349 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 17350 verifyFormat("void f(bool b)\n" 17351 " {\n" 17352 " if (b)\n" 17353 " {\n" 17354 " return;\n" 17355 " }\n" 17356 " }\n", 17357 BreakBeforeBraceShortIfs); 17358 verifyFormat("void f(bool b)\n" 17359 " {\n" 17360 " if (b) return;\n" 17361 " }\n", 17362 BreakBeforeBraceShortIfs); 17363 verifyFormat("void f(bool b)\n" 17364 " {\n" 17365 " while (b)\n" 17366 " {\n" 17367 " return;\n" 17368 " }\n" 17369 " }\n", 17370 BreakBeforeBraceShortIfs); 17371 } 17372 17373 TEST_F(FormatTest, GNUBraceBreaking) { 17374 FormatStyle GNUBraceStyle = getLLVMStyle(); 17375 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 17376 verifyFormat("namespace a\n" 17377 "{\n" 17378 "class A\n" 17379 "{\n" 17380 " void f()\n" 17381 " {\n" 17382 " int a;\n" 17383 " {\n" 17384 " int b;\n" 17385 " }\n" 17386 " if (true)\n" 17387 " {\n" 17388 " a();\n" 17389 " b();\n" 17390 " }\n" 17391 " }\n" 17392 " void g() { return; }\n" 17393 "}\n" 17394 "} // namespace a", 17395 GNUBraceStyle); 17396 17397 verifyFormat("void f()\n" 17398 "{\n" 17399 " if (true)\n" 17400 " {\n" 17401 " a();\n" 17402 " }\n" 17403 " else if (false)\n" 17404 " {\n" 17405 " b();\n" 17406 " }\n" 17407 " else\n" 17408 " {\n" 17409 " c();\n" 17410 " }\n" 17411 "}\n", 17412 GNUBraceStyle); 17413 17414 verifyFormat("void f()\n" 17415 "{\n" 17416 " for (int i = 0; i < 10; ++i)\n" 17417 " {\n" 17418 " a();\n" 17419 " }\n" 17420 " while (false)\n" 17421 " {\n" 17422 " b();\n" 17423 " }\n" 17424 " do\n" 17425 " {\n" 17426 " c();\n" 17427 " }\n" 17428 " while (false);\n" 17429 "}\n", 17430 GNUBraceStyle); 17431 17432 verifyFormat("void f(int a)\n" 17433 "{\n" 17434 " switch (a)\n" 17435 " {\n" 17436 " case 0:\n" 17437 " break;\n" 17438 " case 1:\n" 17439 " {\n" 17440 " break;\n" 17441 " }\n" 17442 " case 2:\n" 17443 " {\n" 17444 " }\n" 17445 " break;\n" 17446 " default:\n" 17447 " break;\n" 17448 " }\n" 17449 "}\n", 17450 GNUBraceStyle); 17451 17452 verifyFormat("enum X\n" 17453 "{\n" 17454 " Y = 0,\n" 17455 "}\n", 17456 GNUBraceStyle); 17457 17458 verifyFormat("@interface BSApplicationController ()\n" 17459 "{\n" 17460 "@private\n" 17461 " id _extraIvar;\n" 17462 "}\n" 17463 "@end\n", 17464 GNUBraceStyle); 17465 17466 verifyFormat("#ifdef _DEBUG\n" 17467 "int foo(int i = 0)\n" 17468 "#else\n" 17469 "int foo(int i = 5)\n" 17470 "#endif\n" 17471 "{\n" 17472 " return i;\n" 17473 "}", 17474 GNUBraceStyle); 17475 17476 verifyFormat("void foo() {}\n" 17477 "void bar()\n" 17478 "#ifdef _DEBUG\n" 17479 "{\n" 17480 " foo();\n" 17481 "}\n" 17482 "#else\n" 17483 "{\n" 17484 "}\n" 17485 "#endif", 17486 GNUBraceStyle); 17487 17488 verifyFormat("void foobar() { int i = 5; }\n" 17489 "#ifdef _DEBUG\n" 17490 "void bar() {}\n" 17491 "#else\n" 17492 "void bar() { foobar(); }\n" 17493 "#endif", 17494 GNUBraceStyle); 17495 } 17496 17497 TEST_F(FormatTest, WebKitBraceBreaking) { 17498 FormatStyle WebKitBraceStyle = getLLVMStyle(); 17499 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 17500 WebKitBraceStyle.FixNamespaceComments = false; 17501 verifyFormat("namespace a {\n" 17502 "class A {\n" 17503 " void f()\n" 17504 " {\n" 17505 " if (true) {\n" 17506 " a();\n" 17507 " b();\n" 17508 " }\n" 17509 " }\n" 17510 " void g() { return; }\n" 17511 "};\n" 17512 "enum E {\n" 17513 " A,\n" 17514 " // foo\n" 17515 " B,\n" 17516 " C\n" 17517 "};\n" 17518 "struct B {\n" 17519 " int x;\n" 17520 "};\n" 17521 "}\n", 17522 WebKitBraceStyle); 17523 verifyFormat("struct S {\n" 17524 " int Type;\n" 17525 " union {\n" 17526 " int x;\n" 17527 " double y;\n" 17528 " } Value;\n" 17529 " class C {\n" 17530 " MyFavoriteType Value;\n" 17531 " } Class;\n" 17532 "};\n", 17533 WebKitBraceStyle); 17534 } 17535 17536 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 17537 verifyFormat("void f() {\n" 17538 " try {\n" 17539 " } catch (const Exception &e) {\n" 17540 " }\n" 17541 "}\n", 17542 getLLVMStyle()); 17543 } 17544 17545 TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) { 17546 auto Style = getLLVMStyle(); 17547 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 17548 Style.AlignConsecutiveAssignments = 17549 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 17550 Style.AlignConsecutiveDeclarations = 17551 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 17552 verifyFormat("struct test demo[] = {\n" 17553 " {56, 23, \"hello\"},\n" 17554 " {-1, 93463, \"world\"},\n" 17555 " { 7, 5, \"!!\"}\n" 17556 "};\n", 17557 Style); 17558 17559 verifyFormat("struct test demo[] = {\n" 17560 " {56, 23, \"hello\"}, // first line\n" 17561 " {-1, 93463, \"world\"}, // second line\n" 17562 " { 7, 5, \"!!\"} // third line\n" 17563 "};\n", 17564 Style); 17565 17566 verifyFormat("struct test demo[4] = {\n" 17567 " { 56, 23, 21, \"oh\"}, // first line\n" 17568 " { -1, 93463, 22, \"my\"}, // second line\n" 17569 " { 7, 5, 1, \"goodness\"} // third line\n" 17570 " {234, 5, 1, \"gracious\"} // fourth line\n" 17571 "};\n", 17572 Style); 17573 17574 verifyFormat("struct test demo[3] = {\n" 17575 " {56, 23, \"hello\"},\n" 17576 " {-1, 93463, \"world\"},\n" 17577 " { 7, 5, \"!!\"}\n" 17578 "};\n", 17579 Style); 17580 17581 verifyFormat("struct test demo[3] = {\n" 17582 " {int{56}, 23, \"hello\"},\n" 17583 " {int{-1}, 93463, \"world\"},\n" 17584 " { int{7}, 5, \"!!\"}\n" 17585 "};\n", 17586 Style); 17587 17588 verifyFormat("struct test demo[] = {\n" 17589 " {56, 23, \"hello\"},\n" 17590 " {-1, 93463, \"world\"},\n" 17591 " { 7, 5, \"!!\"},\n" 17592 "};\n", 17593 Style); 17594 17595 verifyFormat("test demo[] = {\n" 17596 " {56, 23, \"hello\"},\n" 17597 " {-1, 93463, \"world\"},\n" 17598 " { 7, 5, \"!!\"},\n" 17599 "};\n", 17600 Style); 17601 17602 verifyFormat("demo = std::array<struct test, 3>{\n" 17603 " test{56, 23, \"hello\"},\n" 17604 " test{-1, 93463, \"world\"},\n" 17605 " test{ 7, 5, \"!!\"},\n" 17606 "};\n", 17607 Style); 17608 17609 verifyFormat("test demo[] = {\n" 17610 " {56, 23, \"hello\"},\n" 17611 "#if X\n" 17612 " {-1, 93463, \"world\"},\n" 17613 "#endif\n" 17614 " { 7, 5, \"!!\"}\n" 17615 "};\n", 17616 Style); 17617 17618 verifyFormat( 17619 "test demo[] = {\n" 17620 " { 7, 23,\n" 17621 " \"hello world i am a very long line that really, in any\"\n" 17622 " \"just world, ought to be split over multiple lines\"},\n" 17623 " {-1, 93463, \"world\"},\n" 17624 " {56, 5, \"!!\"}\n" 17625 "};\n", 17626 Style); 17627 17628 verifyFormat("return GradForUnaryCwise(g, {\n" 17629 " {{\"sign\"}, \"Sign\", " 17630 " {\"x\", \"dy\"}},\n" 17631 " { {\"dx\"}, \"Mul\", {\"dy\"" 17632 ", \"sign\"}},\n" 17633 "});\n", 17634 Style); 17635 17636 Style.ColumnLimit = 0; 17637 EXPECT_EQ( 17638 "test demo[] = {\n" 17639 " {56, 23, \"hello world i am a very long line that really, " 17640 "in any just world, ought to be split over multiple lines\"},\n" 17641 " {-1, 93463, " 17642 " \"world\"},\n" 17643 " { 7, 5, " 17644 " \"!!\"},\n" 17645 "};", 17646 format("test demo[] = {{56, 23, \"hello world i am a very long line " 17647 "that really, in any just world, ought to be split over multiple " 17648 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 17649 Style)); 17650 17651 Style.ColumnLimit = 80; 17652 verifyFormat("test demo[] = {\n" 17653 " {56, 23, /* a comment */ \"hello\"},\n" 17654 " {-1, 93463, \"world\"},\n" 17655 " { 7, 5, \"!!\"}\n" 17656 "};\n", 17657 Style); 17658 17659 verifyFormat("test demo[] = {\n" 17660 " {56, 23, \"hello\"},\n" 17661 " {-1, 93463, \"world\" /* comment here */},\n" 17662 " { 7, 5, \"!!\"}\n" 17663 "};\n", 17664 Style); 17665 17666 verifyFormat("test demo[] = {\n" 17667 " {56, /* a comment */ 23, \"hello\"},\n" 17668 " {-1, 93463, \"world\"},\n" 17669 " { 7, 5, \"!!\"}\n" 17670 "};\n", 17671 Style); 17672 17673 Style.ColumnLimit = 20; 17674 EXPECT_EQ( 17675 "demo = std::array<\n" 17676 " struct test, 3>{\n" 17677 " test{\n" 17678 " 56, 23,\n" 17679 " \"hello \"\n" 17680 " \"world i \"\n" 17681 " \"am a very \"\n" 17682 " \"long line \"\n" 17683 " \"that \"\n" 17684 " \"really, \"\n" 17685 " \"in any \"\n" 17686 " \"just \"\n" 17687 " \"world, \"\n" 17688 " \"ought to \"\n" 17689 " \"be split \"\n" 17690 " \"over \"\n" 17691 " \"multiple \"\n" 17692 " \"lines\"},\n" 17693 " test{-1, 93463,\n" 17694 " \"world\"},\n" 17695 " test{ 7, 5,\n" 17696 " \"!!\" },\n" 17697 "};", 17698 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world " 17699 "i am a very long line that really, in any just world, ought " 17700 "to be split over multiple lines\"},test{-1, 93463, \"world\"}," 17701 "test{7, 5, \"!!\"},};", 17702 Style)); 17703 // This caused a core dump by enabling Alignment in the LLVMStyle globally 17704 Style = getLLVMStyleWithColumns(50); 17705 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 17706 verifyFormat("static A x = {\n" 17707 " {{init1, init2, init3, init4},\n" 17708 " {init1, init2, init3, init4}}\n" 17709 "};", 17710 Style); 17711 Style.ColumnLimit = 100; 17712 EXPECT_EQ( 17713 "test demo[] = {\n" 17714 " {56, 23,\n" 17715 " \"hello world i am a very long line that really, in any just world" 17716 ", ought to be split over \"\n" 17717 " \"multiple lines\" },\n" 17718 " {-1, 93463, \"world\"},\n" 17719 " { 7, 5, \"!!\"},\n" 17720 "};", 17721 format("test demo[] = {{56, 23, \"hello world i am a very long line " 17722 "that really, in any just world, ought to be split over multiple " 17723 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 17724 Style)); 17725 17726 Style = getLLVMStyleWithColumns(50); 17727 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 17728 Style.AlignConsecutiveAssignments = 17729 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 17730 Style.AlignConsecutiveDeclarations = 17731 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 17732 verifyFormat("struct test demo[] = {\n" 17733 " {56, 23, \"hello\"},\n" 17734 " {-1, 93463, \"world\"},\n" 17735 " { 7, 5, \"!!\"}\n" 17736 "};\n" 17737 "static A x = {\n" 17738 " {{init1, init2, init3, init4},\n" 17739 " {init1, init2, init3, init4}}\n" 17740 "};", 17741 Style); 17742 Style.ColumnLimit = 100; 17743 Style.AlignConsecutiveAssignments = 17744 FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments; 17745 Style.AlignConsecutiveDeclarations = 17746 FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments; 17747 verifyFormat("struct test demo[] = {\n" 17748 " {56, 23, \"hello\"},\n" 17749 " {-1, 93463, \"world\"},\n" 17750 " { 7, 5, \"!!\"}\n" 17751 "};\n" 17752 "struct test demo[4] = {\n" 17753 " { 56, 23, 21, \"oh\"}, // first line\n" 17754 " { -1, 93463, 22, \"my\"}, // second line\n" 17755 " { 7, 5, 1, \"goodness\"} // third line\n" 17756 " {234, 5, 1, \"gracious\"} // fourth line\n" 17757 "};\n", 17758 Style); 17759 EXPECT_EQ( 17760 "test demo[] = {\n" 17761 " {56,\n" 17762 " \"hello world i am a very long line that really, in any just world" 17763 ", ought to be split over \"\n" 17764 " \"multiple lines\", 23},\n" 17765 " {-1, \"world\", 93463},\n" 17766 " { 7, \"!!\", 5},\n" 17767 "};", 17768 format("test demo[] = {{56, \"hello world i am a very long line " 17769 "that really, in any just world, ought to be split over multiple " 17770 "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};", 17771 Style)); 17772 } 17773 17774 TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) { 17775 auto Style = getLLVMStyle(); 17776 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; 17777 verifyFormat("struct test demo[] = {\n" 17778 " {56, 23, \"hello\"},\n" 17779 " {-1, 93463, \"world\"},\n" 17780 " {7, 5, \"!!\" }\n" 17781 "};\n", 17782 Style); 17783 17784 verifyFormat("struct test demo[] = {\n" 17785 " {56, 23, \"hello\"}, // first line\n" 17786 " {-1, 93463, \"world\"}, // second line\n" 17787 " {7, 5, \"!!\" } // third line\n" 17788 "};\n", 17789 Style); 17790 verifyFormat("struct test demo[4] = {\n" 17791 " {56, 23, 21, \"oh\" }, // first line\n" 17792 " {-1, 93463, 22, \"my\" }, // second line\n" 17793 " {7, 5, 1, \"goodness\"} // third line\n" 17794 " {234, 5, 1, \"gracious\"} // fourth line\n" 17795 "};\n", 17796 Style); 17797 verifyFormat("struct test demo[3] = {\n" 17798 " {56, 23, \"hello\"},\n" 17799 " {-1, 93463, \"world\"},\n" 17800 " {7, 5, \"!!\" }\n" 17801 "};\n", 17802 Style); 17803 17804 verifyFormat("struct test demo[3] = {\n" 17805 " {int{56}, 23, \"hello\"},\n" 17806 " {int{-1}, 93463, \"world\"},\n" 17807 " {int{7}, 5, \"!!\" }\n" 17808 "};\n", 17809 Style); 17810 verifyFormat("struct test demo[] = {\n" 17811 " {56, 23, \"hello\"},\n" 17812 " {-1, 93463, \"world\"},\n" 17813 " {7, 5, \"!!\" },\n" 17814 "};\n", 17815 Style); 17816 verifyFormat("test demo[] = {\n" 17817 " {56, 23, \"hello\"},\n" 17818 " {-1, 93463, \"world\"},\n" 17819 " {7, 5, \"!!\" },\n" 17820 "};\n", 17821 Style); 17822 verifyFormat("demo = std::array<struct test, 3>{\n" 17823 " test{56, 23, \"hello\"},\n" 17824 " test{-1, 93463, \"world\"},\n" 17825 " test{7, 5, \"!!\" },\n" 17826 "};\n", 17827 Style); 17828 verifyFormat("test demo[] = {\n" 17829 " {56, 23, \"hello\"},\n" 17830 "#if X\n" 17831 " {-1, 93463, \"world\"},\n" 17832 "#endif\n" 17833 " {7, 5, \"!!\" }\n" 17834 "};\n", 17835 Style); 17836 verifyFormat( 17837 "test demo[] = {\n" 17838 " {7, 23,\n" 17839 " \"hello world i am a very long line that really, in any\"\n" 17840 " \"just world, ought to be split over multiple lines\"},\n" 17841 " {-1, 93463, \"world\" },\n" 17842 " {56, 5, \"!!\" }\n" 17843 "};\n", 17844 Style); 17845 17846 verifyFormat("return GradForUnaryCwise(g, {\n" 17847 " {{\"sign\"}, \"Sign\", {\"x\", " 17848 "\"dy\"} },\n" 17849 " {{\"dx\"}, \"Mul\", " 17850 "{\"dy\", \"sign\"}},\n" 17851 "});\n", 17852 Style); 17853 17854 Style.ColumnLimit = 0; 17855 EXPECT_EQ( 17856 "test demo[] = {\n" 17857 " {56, 23, \"hello world i am a very long line that really, in any " 17858 "just world, ought to be split over multiple lines\"},\n" 17859 " {-1, 93463, \"world\" " 17860 " },\n" 17861 " {7, 5, \"!!\" " 17862 " },\n" 17863 "};", 17864 format("test demo[] = {{56, 23, \"hello world i am a very long line " 17865 "that really, in any just world, ought to be split over multiple " 17866 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 17867 Style)); 17868 17869 Style.ColumnLimit = 80; 17870 verifyFormat("test demo[] = {\n" 17871 " {56, 23, /* a comment */ \"hello\"},\n" 17872 " {-1, 93463, \"world\" },\n" 17873 " {7, 5, \"!!\" }\n" 17874 "};\n", 17875 Style); 17876 17877 verifyFormat("test demo[] = {\n" 17878 " {56, 23, \"hello\" },\n" 17879 " {-1, 93463, \"world\" /* comment here */},\n" 17880 " {7, 5, \"!!\" }\n" 17881 "};\n", 17882 Style); 17883 17884 verifyFormat("test demo[] = {\n" 17885 " {56, /* a comment */ 23, \"hello\"},\n" 17886 " {-1, 93463, \"world\"},\n" 17887 " {7, 5, \"!!\" }\n" 17888 "};\n", 17889 Style); 17890 17891 Style.ColumnLimit = 20; 17892 EXPECT_EQ( 17893 "demo = std::array<\n" 17894 " struct test, 3>{\n" 17895 " test{\n" 17896 " 56, 23,\n" 17897 " \"hello \"\n" 17898 " \"world i \"\n" 17899 " \"am a very \"\n" 17900 " \"long line \"\n" 17901 " \"that \"\n" 17902 " \"really, \"\n" 17903 " \"in any \"\n" 17904 " \"just \"\n" 17905 " \"world, \"\n" 17906 " \"ought to \"\n" 17907 " \"be split \"\n" 17908 " \"over \"\n" 17909 " \"multiple \"\n" 17910 " \"lines\"},\n" 17911 " test{-1, 93463,\n" 17912 " \"world\"},\n" 17913 " test{7, 5,\n" 17914 " \"!!\" },\n" 17915 "};", 17916 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world " 17917 "i am a very long line that really, in any just world, ought " 17918 "to be split over multiple lines\"},test{-1, 93463, \"world\"}," 17919 "test{7, 5, \"!!\"},};", 17920 Style)); 17921 17922 // This caused a core dump by enabling Alignment in the LLVMStyle globally 17923 Style = getLLVMStyleWithColumns(50); 17924 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; 17925 verifyFormat("static A x = {\n" 17926 " {{init1, init2, init3, init4},\n" 17927 " {init1, init2, init3, init4}}\n" 17928 "};", 17929 Style); 17930 Style.ColumnLimit = 100; 17931 EXPECT_EQ( 17932 "test demo[] = {\n" 17933 " {56, 23,\n" 17934 " \"hello world i am a very long line that really, in any just world" 17935 ", ought to be split over \"\n" 17936 " \"multiple lines\" },\n" 17937 " {-1, 93463, \"world\"},\n" 17938 " {7, 5, \"!!\" },\n" 17939 "};", 17940 format("test demo[] = {{56, 23, \"hello world i am a very long line " 17941 "that really, in any just world, ought to be split over multiple " 17942 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 17943 Style)); 17944 } 17945 17946 TEST_F(FormatTest, UnderstandsPragmas) { 17947 verifyFormat("#pragma omp reduction(| : var)"); 17948 verifyFormat("#pragma omp reduction(+ : var)"); 17949 17950 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 17951 "(including parentheses).", 17952 format("#pragma mark Any non-hyphenated or hyphenated string " 17953 "(including parentheses).")); 17954 } 17955 17956 TEST_F(FormatTest, UnderstandPragmaOption) { 17957 verifyFormat("#pragma option -C -A"); 17958 17959 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 17960 } 17961 17962 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) { 17963 FormatStyle Style = getLLVMStyle(); 17964 Style.ColumnLimit = 20; 17965 17966 // See PR41213 17967 EXPECT_EQ("/*\n" 17968 " *\t9012345\n" 17969 " * /8901\n" 17970 " */", 17971 format("/*\n" 17972 " *\t9012345 /8901\n" 17973 " */", 17974 Style)); 17975 EXPECT_EQ("/*\n" 17976 " *345678\n" 17977 " *\t/8901\n" 17978 " */", 17979 format("/*\n" 17980 " *345678\t/8901\n" 17981 " */", 17982 Style)); 17983 17984 verifyFormat("int a; // the\n" 17985 " // comment", 17986 Style); 17987 EXPECT_EQ("int a; /* first line\n" 17988 " * second\n" 17989 " * line third\n" 17990 " * line\n" 17991 " */", 17992 format("int a; /* first line\n" 17993 " * second\n" 17994 " * line third\n" 17995 " * line\n" 17996 " */", 17997 Style)); 17998 EXPECT_EQ("int a; // first line\n" 17999 " // second\n" 18000 " // line third\n" 18001 " // line", 18002 format("int a; // first line\n" 18003 " // second line\n" 18004 " // third line", 18005 Style)); 18006 18007 Style.PenaltyExcessCharacter = 90; 18008 verifyFormat("int a; // the comment", Style); 18009 EXPECT_EQ("int a; // the comment\n" 18010 " // aaa", 18011 format("int a; // the comment aaa", Style)); 18012 EXPECT_EQ("int a; /* first line\n" 18013 " * second line\n" 18014 " * third line\n" 18015 " */", 18016 format("int a; /* first line\n" 18017 " * second line\n" 18018 " * third line\n" 18019 " */", 18020 Style)); 18021 EXPECT_EQ("int a; // first line\n" 18022 " // second line\n" 18023 " // third line", 18024 format("int a; // first line\n" 18025 " // second line\n" 18026 " // third line", 18027 Style)); 18028 // FIXME: Investigate why this is not getting the same layout as the test 18029 // above. 18030 EXPECT_EQ("int a; /* first line\n" 18031 " * second line\n" 18032 " * third line\n" 18033 " */", 18034 format("int a; /* first line second line third line" 18035 "\n*/", 18036 Style)); 18037 18038 EXPECT_EQ("// foo bar baz bazfoo\n" 18039 "// foo bar foo bar\n", 18040 format("// foo bar baz bazfoo\n" 18041 "// foo bar foo bar\n", 18042 Style)); 18043 EXPECT_EQ("// foo bar baz bazfoo\n" 18044 "// foo bar foo bar\n", 18045 format("// foo bar baz bazfoo\n" 18046 "// foo bar foo bar\n", 18047 Style)); 18048 18049 // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the 18050 // next one. 18051 EXPECT_EQ("// foo bar baz bazfoo\n" 18052 "// bar foo bar\n", 18053 format("// foo bar baz bazfoo bar\n" 18054 "// foo bar\n", 18055 Style)); 18056 18057 EXPECT_EQ("// foo bar baz bazfoo\n" 18058 "// foo bar baz bazfoo\n" 18059 "// bar foo bar\n", 18060 format("// foo bar baz bazfoo\n" 18061 "// foo bar baz bazfoo bar\n" 18062 "// foo bar\n", 18063 Style)); 18064 18065 EXPECT_EQ("// foo bar baz bazfoo\n" 18066 "// foo bar baz bazfoo\n" 18067 "// bar foo bar\n", 18068 format("// foo bar baz bazfoo\n" 18069 "// foo bar baz bazfoo bar\n" 18070 "// foo bar\n", 18071 Style)); 18072 18073 // Make sure we do not keep protruding characters if strict mode reflow is 18074 // cheaper than keeping protruding characters. 18075 Style.ColumnLimit = 21; 18076 EXPECT_EQ( 18077 "// foo foo foo foo\n" 18078 "// foo foo foo foo\n" 18079 "// foo foo foo foo\n", 18080 format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", Style)); 18081 18082 EXPECT_EQ("int a = /* long block\n" 18083 " comment */\n" 18084 " 42;", 18085 format("int a = /* long block comment */ 42;", Style)); 18086 } 18087 18088 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 18089 for (size_t i = 1; i < Styles.size(); ++i) \ 18090 EXPECT_EQ(Styles[0], Styles[i]) \ 18091 << "Style #" << i << " of " << Styles.size() << " differs from Style #0" 18092 18093 TEST_F(FormatTest, GetsPredefinedStyleByName) { 18094 SmallVector<FormatStyle, 3> Styles; 18095 Styles.resize(3); 18096 18097 Styles[0] = getLLVMStyle(); 18098 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 18099 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 18100 EXPECT_ALL_STYLES_EQUAL(Styles); 18101 18102 Styles[0] = getGoogleStyle(); 18103 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 18104 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 18105 EXPECT_ALL_STYLES_EQUAL(Styles); 18106 18107 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 18108 EXPECT_TRUE( 18109 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 18110 EXPECT_TRUE( 18111 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 18112 EXPECT_ALL_STYLES_EQUAL(Styles); 18113 18114 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 18115 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 18116 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 18117 EXPECT_ALL_STYLES_EQUAL(Styles); 18118 18119 Styles[0] = getMozillaStyle(); 18120 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 18121 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 18122 EXPECT_ALL_STYLES_EQUAL(Styles); 18123 18124 Styles[0] = getWebKitStyle(); 18125 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 18126 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 18127 EXPECT_ALL_STYLES_EQUAL(Styles); 18128 18129 Styles[0] = getGNUStyle(); 18130 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 18131 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 18132 EXPECT_ALL_STYLES_EQUAL(Styles); 18133 18134 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 18135 } 18136 18137 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 18138 SmallVector<FormatStyle, 8> Styles; 18139 Styles.resize(2); 18140 18141 Styles[0] = getGoogleStyle(); 18142 Styles[1] = getLLVMStyle(); 18143 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 18144 EXPECT_ALL_STYLES_EQUAL(Styles); 18145 18146 Styles.resize(5); 18147 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 18148 Styles[1] = getLLVMStyle(); 18149 Styles[1].Language = FormatStyle::LK_JavaScript; 18150 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 18151 18152 Styles[2] = getLLVMStyle(); 18153 Styles[2].Language = FormatStyle::LK_JavaScript; 18154 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 18155 "BasedOnStyle: Google", 18156 &Styles[2]) 18157 .value()); 18158 18159 Styles[3] = getLLVMStyle(); 18160 Styles[3].Language = FormatStyle::LK_JavaScript; 18161 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 18162 "Language: JavaScript", 18163 &Styles[3]) 18164 .value()); 18165 18166 Styles[4] = getLLVMStyle(); 18167 Styles[4].Language = FormatStyle::LK_JavaScript; 18168 EXPECT_EQ(0, parseConfiguration("---\n" 18169 "BasedOnStyle: LLVM\n" 18170 "IndentWidth: 123\n" 18171 "---\n" 18172 "BasedOnStyle: Google\n" 18173 "Language: JavaScript", 18174 &Styles[4]) 18175 .value()); 18176 EXPECT_ALL_STYLES_EQUAL(Styles); 18177 } 18178 18179 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 18180 Style.FIELD = false; \ 18181 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 18182 EXPECT_TRUE(Style.FIELD); \ 18183 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 18184 EXPECT_FALSE(Style.FIELD); 18185 18186 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 18187 18188 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 18189 Style.STRUCT.FIELD = false; \ 18190 EXPECT_EQ(0, \ 18191 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 18192 .value()); \ 18193 EXPECT_TRUE(Style.STRUCT.FIELD); \ 18194 EXPECT_EQ(0, \ 18195 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 18196 .value()); \ 18197 EXPECT_FALSE(Style.STRUCT.FIELD); 18198 18199 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 18200 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 18201 18202 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 18203 EXPECT_NE(VALUE, Style.FIELD) << "Initial value already the same!"; \ 18204 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 18205 EXPECT_EQ(VALUE, Style.FIELD) << "Unexpected value after parsing!" 18206 18207 TEST_F(FormatTest, ParsesConfigurationBools) { 18208 FormatStyle Style = {}; 18209 Style.Language = FormatStyle::LK_Cpp; 18210 CHECK_PARSE_BOOL(AlignTrailingComments); 18211 CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine); 18212 CHECK_PARSE_BOOL(AllowAllConstructorInitializersOnNextLine); 18213 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 18214 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 18215 CHECK_PARSE_BOOL(AllowShortEnumsOnASingleLine); 18216 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 18217 CHECK_PARSE_BOOL(BinPackArguments); 18218 CHECK_PARSE_BOOL(BinPackParameters); 18219 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 18220 CHECK_PARSE_BOOL(BreakBeforeConceptDeclarations); 18221 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 18222 CHECK_PARSE_BOOL(BreakStringLiterals); 18223 CHECK_PARSE_BOOL(CompactNamespaces); 18224 CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); 18225 CHECK_PARSE_BOOL(DeriveLineEnding); 18226 CHECK_PARSE_BOOL(DerivePointerAlignment); 18227 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 18228 CHECK_PARSE_BOOL(DisableFormat); 18229 CHECK_PARSE_BOOL(IndentAccessModifiers); 18230 CHECK_PARSE_BOOL(IndentCaseLabels); 18231 CHECK_PARSE_BOOL(IndentCaseBlocks); 18232 CHECK_PARSE_BOOL(IndentGotoLabels); 18233 CHECK_PARSE_BOOL(IndentRequires); 18234 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 18235 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 18236 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 18237 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 18238 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 18239 CHECK_PARSE_BOOL(ReflowComments); 18240 CHECK_PARSE_BOOL(SortUsingDeclarations); 18241 CHECK_PARSE_BOOL(SpacesInParentheses); 18242 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 18243 CHECK_PARSE_BOOL(SpacesInConditionalStatement); 18244 CHECK_PARSE_BOOL(SpaceInEmptyBlock); 18245 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 18246 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 18247 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 18248 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 18249 CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); 18250 CHECK_PARSE_BOOL(SpaceAfterLogicalNot); 18251 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 18252 CHECK_PARSE_BOOL(SpaceBeforeCaseColon); 18253 CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList); 18254 CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon); 18255 CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon); 18256 CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon); 18257 CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets); 18258 CHECK_PARSE_BOOL(UseCRLF); 18259 18260 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel); 18261 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 18262 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 18263 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 18264 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 18265 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 18266 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 18267 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 18268 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock); 18269 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 18270 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 18271 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeLambdaBody); 18272 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeWhile); 18273 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 18274 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction); 18275 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord); 18276 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace); 18277 } 18278 18279 #undef CHECK_PARSE_BOOL 18280 18281 TEST_F(FormatTest, ParsesConfiguration) { 18282 FormatStyle Style = {}; 18283 Style.Language = FormatStyle::LK_Cpp; 18284 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 18285 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 18286 ConstructorInitializerIndentWidth, 1234u); 18287 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 18288 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 18289 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 18290 CHECK_PARSE("PenaltyBreakAssignment: 1234", PenaltyBreakAssignment, 1234u); 18291 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 18292 PenaltyBreakBeforeFirstCallParameter, 1234u); 18293 CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234", 18294 PenaltyBreakTemplateDeclaration, 1234u); 18295 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 18296 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 18297 PenaltyReturnTypeOnItsOwnLine, 1234u); 18298 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 18299 SpacesBeforeTrailingComments, 1234u); 18300 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 18301 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 18302 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$"); 18303 18304 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 18305 CHECK_PARSE("AlignConsecutiveAssignments: None", AlignConsecutiveAssignments, 18306 FormatStyle::ACS_None); 18307 CHECK_PARSE("AlignConsecutiveAssignments: Consecutive", 18308 AlignConsecutiveAssignments, FormatStyle::ACS_Consecutive); 18309 CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLines", 18310 AlignConsecutiveAssignments, FormatStyle::ACS_AcrossEmptyLines); 18311 CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLinesAndComments", 18312 AlignConsecutiveAssignments, 18313 FormatStyle::ACS_AcrossEmptyLinesAndComments); 18314 // For backwards compability, false / true should still parse 18315 CHECK_PARSE("AlignConsecutiveAssignments: false", AlignConsecutiveAssignments, 18316 FormatStyle::ACS_None); 18317 CHECK_PARSE("AlignConsecutiveAssignments: true", AlignConsecutiveAssignments, 18318 FormatStyle::ACS_Consecutive); 18319 18320 Style.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive; 18321 CHECK_PARSE("AlignConsecutiveBitFields: None", AlignConsecutiveBitFields, 18322 FormatStyle::ACS_None); 18323 CHECK_PARSE("AlignConsecutiveBitFields: Consecutive", 18324 AlignConsecutiveBitFields, FormatStyle::ACS_Consecutive); 18325 CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLines", 18326 AlignConsecutiveBitFields, FormatStyle::ACS_AcrossEmptyLines); 18327 CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLinesAndComments", 18328 AlignConsecutiveBitFields, 18329 FormatStyle::ACS_AcrossEmptyLinesAndComments); 18330 // For backwards compability, false / true should still parse 18331 CHECK_PARSE("AlignConsecutiveBitFields: false", AlignConsecutiveBitFields, 18332 FormatStyle::ACS_None); 18333 CHECK_PARSE("AlignConsecutiveBitFields: true", AlignConsecutiveBitFields, 18334 FormatStyle::ACS_Consecutive); 18335 18336 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 18337 CHECK_PARSE("AlignConsecutiveMacros: None", AlignConsecutiveMacros, 18338 FormatStyle::ACS_None); 18339 CHECK_PARSE("AlignConsecutiveMacros: Consecutive", AlignConsecutiveMacros, 18340 FormatStyle::ACS_Consecutive); 18341 CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLines", 18342 AlignConsecutiveMacros, FormatStyle::ACS_AcrossEmptyLines); 18343 CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLinesAndComments", 18344 AlignConsecutiveMacros, 18345 FormatStyle::ACS_AcrossEmptyLinesAndComments); 18346 // For backwards compability, false / true should still parse 18347 CHECK_PARSE("AlignConsecutiveMacros: false", AlignConsecutiveMacros, 18348 FormatStyle::ACS_None); 18349 CHECK_PARSE("AlignConsecutiveMacros: true", AlignConsecutiveMacros, 18350 FormatStyle::ACS_Consecutive); 18351 18352 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 18353 CHECK_PARSE("AlignConsecutiveDeclarations: None", 18354 AlignConsecutiveDeclarations, FormatStyle::ACS_None); 18355 CHECK_PARSE("AlignConsecutiveDeclarations: Consecutive", 18356 AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive); 18357 CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLines", 18358 AlignConsecutiveDeclarations, FormatStyle::ACS_AcrossEmptyLines); 18359 CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLinesAndComments", 18360 AlignConsecutiveDeclarations, 18361 FormatStyle::ACS_AcrossEmptyLinesAndComments); 18362 // For backwards compability, false / true should still parse 18363 CHECK_PARSE("AlignConsecutiveDeclarations: false", 18364 AlignConsecutiveDeclarations, FormatStyle::ACS_None); 18365 CHECK_PARSE("AlignConsecutiveDeclarations: true", 18366 AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive); 18367 18368 Style.PointerAlignment = FormatStyle::PAS_Middle; 18369 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 18370 FormatStyle::PAS_Left); 18371 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 18372 FormatStyle::PAS_Right); 18373 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 18374 FormatStyle::PAS_Middle); 18375 Style.ReferenceAlignment = FormatStyle::RAS_Middle; 18376 CHECK_PARSE("ReferenceAlignment: Pointer", ReferenceAlignment, 18377 FormatStyle::RAS_Pointer); 18378 CHECK_PARSE("ReferenceAlignment: Left", ReferenceAlignment, 18379 FormatStyle::RAS_Left); 18380 CHECK_PARSE("ReferenceAlignment: Right", ReferenceAlignment, 18381 FormatStyle::RAS_Right); 18382 CHECK_PARSE("ReferenceAlignment: Middle", ReferenceAlignment, 18383 FormatStyle::RAS_Middle); 18384 // For backward compatibility: 18385 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 18386 FormatStyle::PAS_Left); 18387 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 18388 FormatStyle::PAS_Right); 18389 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 18390 FormatStyle::PAS_Middle); 18391 18392 Style.Standard = FormatStyle::LS_Auto; 18393 CHECK_PARSE("Standard: c++03", Standard, FormatStyle::LS_Cpp03); 18394 CHECK_PARSE("Standard: c++11", Standard, FormatStyle::LS_Cpp11); 18395 CHECK_PARSE("Standard: c++14", Standard, FormatStyle::LS_Cpp14); 18396 CHECK_PARSE("Standard: c++17", Standard, FormatStyle::LS_Cpp17); 18397 CHECK_PARSE("Standard: c++20", Standard, FormatStyle::LS_Cpp20); 18398 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 18399 CHECK_PARSE("Standard: Latest", Standard, FormatStyle::LS_Latest); 18400 // Legacy aliases: 18401 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 18402 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Latest); 18403 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 18404 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 18405 18406 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 18407 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 18408 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 18409 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 18410 FormatStyle::BOS_None); 18411 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 18412 FormatStyle::BOS_All); 18413 // For backward compatibility: 18414 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 18415 FormatStyle::BOS_None); 18416 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 18417 FormatStyle::BOS_All); 18418 18419 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 18420 CHECK_PARSE("BreakConstructorInitializers: BeforeComma", 18421 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 18422 CHECK_PARSE("BreakConstructorInitializers: AfterColon", 18423 BreakConstructorInitializers, FormatStyle::BCIS_AfterColon); 18424 CHECK_PARSE("BreakConstructorInitializers: BeforeColon", 18425 BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon); 18426 // For backward compatibility: 18427 CHECK_PARSE("BreakConstructorInitializersBeforeComma: true", 18428 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 18429 18430 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 18431 CHECK_PARSE("BreakInheritanceList: AfterComma", BreakInheritanceList, 18432 FormatStyle::BILS_AfterComma); 18433 CHECK_PARSE("BreakInheritanceList: BeforeComma", BreakInheritanceList, 18434 FormatStyle::BILS_BeforeComma); 18435 CHECK_PARSE("BreakInheritanceList: AfterColon", BreakInheritanceList, 18436 FormatStyle::BILS_AfterColon); 18437 CHECK_PARSE("BreakInheritanceList: BeforeColon", BreakInheritanceList, 18438 FormatStyle::BILS_BeforeColon); 18439 // For backward compatibility: 18440 CHECK_PARSE("BreakBeforeInheritanceComma: true", BreakInheritanceList, 18441 FormatStyle::BILS_BeforeComma); 18442 18443 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 18444 CHECK_PARSE("EmptyLineBeforeAccessModifier: Never", 18445 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Never); 18446 CHECK_PARSE("EmptyLineBeforeAccessModifier: Leave", 18447 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Leave); 18448 CHECK_PARSE("EmptyLineBeforeAccessModifier: LogicalBlock", 18449 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_LogicalBlock); 18450 CHECK_PARSE("EmptyLineBeforeAccessModifier: Always", 18451 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Always); 18452 18453 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 18454 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 18455 FormatStyle::BAS_Align); 18456 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 18457 FormatStyle::BAS_DontAlign); 18458 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 18459 FormatStyle::BAS_AlwaysBreak); 18460 // For backward compatibility: 18461 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 18462 FormatStyle::BAS_DontAlign); 18463 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 18464 FormatStyle::BAS_Align); 18465 18466 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left; 18467 CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines, 18468 FormatStyle::ENAS_DontAlign); 18469 CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines, 18470 FormatStyle::ENAS_Left); 18471 CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines, 18472 FormatStyle::ENAS_Right); 18473 // For backward compatibility: 18474 CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines, 18475 FormatStyle::ENAS_Left); 18476 CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines, 18477 FormatStyle::ENAS_Right); 18478 18479 Style.AlignOperands = FormatStyle::OAS_Align; 18480 CHECK_PARSE("AlignOperands: DontAlign", AlignOperands, 18481 FormatStyle::OAS_DontAlign); 18482 CHECK_PARSE("AlignOperands: Align", AlignOperands, FormatStyle::OAS_Align); 18483 CHECK_PARSE("AlignOperands: AlignAfterOperator", AlignOperands, 18484 FormatStyle::OAS_AlignAfterOperator); 18485 // For backward compatibility: 18486 CHECK_PARSE("AlignOperands: false", AlignOperands, 18487 FormatStyle::OAS_DontAlign); 18488 CHECK_PARSE("AlignOperands: true", AlignOperands, FormatStyle::OAS_Align); 18489 18490 Style.UseTab = FormatStyle::UT_ForIndentation; 18491 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 18492 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 18493 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 18494 CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab, 18495 FormatStyle::UT_ForContinuationAndIndentation); 18496 CHECK_PARSE("UseTab: AlignWithSpaces", UseTab, 18497 FormatStyle::UT_AlignWithSpaces); 18498 // For backward compatibility: 18499 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 18500 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 18501 18502 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 18503 CHECK_PARSE("AllowShortBlocksOnASingleLine: Never", 18504 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 18505 CHECK_PARSE("AllowShortBlocksOnASingleLine: Empty", 18506 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Empty); 18507 CHECK_PARSE("AllowShortBlocksOnASingleLine: Always", 18508 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always); 18509 // For backward compatibility: 18510 CHECK_PARSE("AllowShortBlocksOnASingleLine: false", 18511 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 18512 CHECK_PARSE("AllowShortBlocksOnASingleLine: true", 18513 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always); 18514 18515 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 18516 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 18517 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 18518 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 18519 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 18520 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 18521 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 18522 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 18523 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 18524 // For backward compatibility: 18525 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 18526 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 18527 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 18528 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 18529 18530 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Both; 18531 CHECK_PARSE("SpaceAroundPointerQualifiers: Default", 18532 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Default); 18533 CHECK_PARSE("SpaceAroundPointerQualifiers: Before", 18534 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Before); 18535 CHECK_PARSE("SpaceAroundPointerQualifiers: After", 18536 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_After); 18537 CHECK_PARSE("SpaceAroundPointerQualifiers: Both", 18538 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Both); 18539 18540 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 18541 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 18542 FormatStyle::SBPO_Never); 18543 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 18544 FormatStyle::SBPO_Always); 18545 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 18546 FormatStyle::SBPO_ControlStatements); 18547 CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptControlMacros", 18548 SpaceBeforeParens, 18549 FormatStyle::SBPO_ControlStatementsExceptControlMacros); 18550 CHECK_PARSE("SpaceBeforeParens: NonEmptyParentheses", SpaceBeforeParens, 18551 FormatStyle::SBPO_NonEmptyParentheses); 18552 // For backward compatibility: 18553 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 18554 FormatStyle::SBPO_Never); 18555 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 18556 FormatStyle::SBPO_ControlStatements); 18557 CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptForEachMacros", 18558 SpaceBeforeParens, 18559 FormatStyle::SBPO_ControlStatementsExceptControlMacros); 18560 18561 Style.ColumnLimit = 123; 18562 FormatStyle BaseStyle = getLLVMStyle(); 18563 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 18564 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 18565 18566 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 18567 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 18568 FormatStyle::BS_Attach); 18569 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 18570 FormatStyle::BS_Linux); 18571 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 18572 FormatStyle::BS_Mozilla); 18573 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 18574 FormatStyle::BS_Stroustrup); 18575 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 18576 FormatStyle::BS_Allman); 18577 CHECK_PARSE("BreakBeforeBraces: Whitesmiths", BreakBeforeBraces, 18578 FormatStyle::BS_Whitesmiths); 18579 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 18580 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 18581 FormatStyle::BS_WebKit); 18582 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 18583 FormatStyle::BS_Custom); 18584 18585 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never; 18586 CHECK_PARSE("BraceWrapping:\n" 18587 " AfterControlStatement: MultiLine", 18588 BraceWrapping.AfterControlStatement, 18589 FormatStyle::BWACS_MultiLine); 18590 CHECK_PARSE("BraceWrapping:\n" 18591 " AfterControlStatement: Always", 18592 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always); 18593 CHECK_PARSE("BraceWrapping:\n" 18594 " AfterControlStatement: Never", 18595 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never); 18596 // For backward compatibility: 18597 CHECK_PARSE("BraceWrapping:\n" 18598 " AfterControlStatement: true", 18599 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always); 18600 CHECK_PARSE("BraceWrapping:\n" 18601 " AfterControlStatement: false", 18602 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never); 18603 18604 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 18605 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 18606 FormatStyle::RTBS_None); 18607 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 18608 FormatStyle::RTBS_All); 18609 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 18610 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 18611 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 18612 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 18613 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 18614 AlwaysBreakAfterReturnType, 18615 FormatStyle::RTBS_TopLevelDefinitions); 18616 18617 Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 18618 CHECK_PARSE("AlwaysBreakTemplateDeclarations: No", 18619 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_No); 18620 CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine", 18621 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine); 18622 CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes", 18623 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes); 18624 CHECK_PARSE("AlwaysBreakTemplateDeclarations: false", 18625 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine); 18626 CHECK_PARSE("AlwaysBreakTemplateDeclarations: true", 18627 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes); 18628 18629 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 18630 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 18631 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 18632 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 18633 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 18634 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 18635 AlwaysBreakAfterDefinitionReturnType, 18636 FormatStyle::DRTBS_TopLevel); 18637 18638 Style.NamespaceIndentation = FormatStyle::NI_All; 18639 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 18640 FormatStyle::NI_None); 18641 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 18642 FormatStyle::NI_Inner); 18643 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 18644 FormatStyle::NI_All); 18645 18646 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_OnlyFirstIf; 18647 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Never", 18648 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never); 18649 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: WithoutElse", 18650 AllowShortIfStatementsOnASingleLine, 18651 FormatStyle::SIS_WithoutElse); 18652 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: OnlyFirstIf", 18653 AllowShortIfStatementsOnASingleLine, 18654 FormatStyle::SIS_OnlyFirstIf); 18655 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: AllIfsAndElse", 18656 AllowShortIfStatementsOnASingleLine, 18657 FormatStyle::SIS_AllIfsAndElse); 18658 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Always", 18659 AllowShortIfStatementsOnASingleLine, 18660 FormatStyle::SIS_OnlyFirstIf); 18661 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: false", 18662 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never); 18663 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: true", 18664 AllowShortIfStatementsOnASingleLine, 18665 FormatStyle::SIS_WithoutElse); 18666 18667 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; 18668 CHECK_PARSE("IndentExternBlock: AfterExternBlock", IndentExternBlock, 18669 FormatStyle::IEBS_AfterExternBlock); 18670 CHECK_PARSE("IndentExternBlock: Indent", IndentExternBlock, 18671 FormatStyle::IEBS_Indent); 18672 CHECK_PARSE("IndentExternBlock: NoIndent", IndentExternBlock, 18673 FormatStyle::IEBS_NoIndent); 18674 CHECK_PARSE("IndentExternBlock: true", IndentExternBlock, 18675 FormatStyle::IEBS_Indent); 18676 CHECK_PARSE("IndentExternBlock: false", IndentExternBlock, 18677 FormatStyle::IEBS_NoIndent); 18678 18679 Style.BitFieldColonSpacing = FormatStyle::BFCS_None; 18680 CHECK_PARSE("BitFieldColonSpacing: Both", BitFieldColonSpacing, 18681 FormatStyle::BFCS_Both); 18682 CHECK_PARSE("BitFieldColonSpacing: None", BitFieldColonSpacing, 18683 FormatStyle::BFCS_None); 18684 CHECK_PARSE("BitFieldColonSpacing: Before", BitFieldColonSpacing, 18685 FormatStyle::BFCS_Before); 18686 CHECK_PARSE("BitFieldColonSpacing: After", BitFieldColonSpacing, 18687 FormatStyle::BFCS_After); 18688 18689 Style.SortJavaStaticImport = FormatStyle::SJSIO_Before; 18690 CHECK_PARSE("SortJavaStaticImport: After", SortJavaStaticImport, 18691 FormatStyle::SJSIO_After); 18692 CHECK_PARSE("SortJavaStaticImport: Before", SortJavaStaticImport, 18693 FormatStyle::SJSIO_Before); 18694 18695 // FIXME: This is required because parsing a configuration simply overwrites 18696 // the first N elements of the list instead of resetting it. 18697 Style.ForEachMacros.clear(); 18698 std::vector<std::string> BoostForeach; 18699 BoostForeach.push_back("BOOST_FOREACH"); 18700 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 18701 std::vector<std::string> BoostAndQForeach; 18702 BoostAndQForeach.push_back("BOOST_FOREACH"); 18703 BoostAndQForeach.push_back("Q_FOREACH"); 18704 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 18705 BoostAndQForeach); 18706 18707 Style.IfMacros.clear(); 18708 std::vector<std::string> CustomIfs; 18709 CustomIfs.push_back("MYIF"); 18710 CHECK_PARSE("IfMacros: [MYIF]", IfMacros, CustomIfs); 18711 18712 Style.AttributeMacros.clear(); 18713 CHECK_PARSE("BasedOnStyle: LLVM", AttributeMacros, 18714 std::vector<std::string>{"__capability"}); 18715 CHECK_PARSE("AttributeMacros: [attr1, attr2]", AttributeMacros, 18716 std::vector<std::string>({"attr1", "attr2"})); 18717 18718 Style.StatementAttributeLikeMacros.clear(); 18719 CHECK_PARSE("StatementAttributeLikeMacros: [emit,Q_EMIT]", 18720 StatementAttributeLikeMacros, 18721 std::vector<std::string>({"emit", "Q_EMIT"})); 18722 18723 Style.StatementMacros.clear(); 18724 CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros, 18725 std::vector<std::string>{"QUNUSED"}); 18726 CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros, 18727 std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"})); 18728 18729 Style.NamespaceMacros.clear(); 18730 CHECK_PARSE("NamespaceMacros: [TESTSUITE]", NamespaceMacros, 18731 std::vector<std::string>{"TESTSUITE"}); 18732 CHECK_PARSE("NamespaceMacros: [TESTSUITE, SUITE]", NamespaceMacros, 18733 std::vector<std::string>({"TESTSUITE", "SUITE"})); 18734 18735 Style.WhitespaceSensitiveMacros.clear(); 18736 CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE]", 18737 WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"}); 18738 CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE, ASSERT]", 18739 WhitespaceSensitiveMacros, 18740 std::vector<std::string>({"STRINGIZE", "ASSERT"})); 18741 Style.WhitespaceSensitiveMacros.clear(); 18742 CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE']", 18743 WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"}); 18744 CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE', 'ASSERT']", 18745 WhitespaceSensitiveMacros, 18746 std::vector<std::string>({"STRINGIZE", "ASSERT"})); 18747 18748 Style.IncludeStyle.IncludeCategories.clear(); 18749 std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = { 18750 {"abc/.*", 2, 0, false}, {".*", 1, 0, true}}; 18751 CHECK_PARSE("IncludeCategories:\n" 18752 " - Regex: abc/.*\n" 18753 " Priority: 2\n" 18754 " - Regex: .*\n" 18755 " Priority: 1\n" 18756 " CaseSensitive: true\n", 18757 IncludeStyle.IncludeCategories, ExpectedCategories); 18758 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex, 18759 "abc$"); 18760 CHECK_PARSE("IncludeIsMainSourceRegex: 'abc$'", 18761 IncludeStyle.IncludeIsMainSourceRegex, "abc$"); 18762 18763 Style.SortIncludes = FormatStyle::SI_Never; 18764 CHECK_PARSE("SortIncludes: true", SortIncludes, 18765 FormatStyle::SI_CaseSensitive); 18766 CHECK_PARSE("SortIncludes: false", SortIncludes, FormatStyle::SI_Never); 18767 CHECK_PARSE("SortIncludes: CaseInsensitive", SortIncludes, 18768 FormatStyle::SI_CaseInsensitive); 18769 CHECK_PARSE("SortIncludes: CaseSensitive", SortIncludes, 18770 FormatStyle::SI_CaseSensitive); 18771 CHECK_PARSE("SortIncludes: Never", SortIncludes, FormatStyle::SI_Never); 18772 18773 Style.RawStringFormats.clear(); 18774 std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = { 18775 { 18776 FormatStyle::LK_TextProto, 18777 {"pb", "proto"}, 18778 {"PARSE_TEXT_PROTO"}, 18779 /*CanonicalDelimiter=*/"", 18780 "llvm", 18781 }, 18782 { 18783 FormatStyle::LK_Cpp, 18784 {"cc", "cpp"}, 18785 {"C_CODEBLOCK", "CPPEVAL"}, 18786 /*CanonicalDelimiter=*/"cc", 18787 /*BasedOnStyle=*/"", 18788 }, 18789 }; 18790 18791 CHECK_PARSE("RawStringFormats:\n" 18792 " - Language: TextProto\n" 18793 " Delimiters:\n" 18794 " - 'pb'\n" 18795 " - 'proto'\n" 18796 " EnclosingFunctions:\n" 18797 " - 'PARSE_TEXT_PROTO'\n" 18798 " BasedOnStyle: llvm\n" 18799 " - Language: Cpp\n" 18800 " Delimiters:\n" 18801 " - 'cc'\n" 18802 " - 'cpp'\n" 18803 " EnclosingFunctions:\n" 18804 " - 'C_CODEBLOCK'\n" 18805 " - 'CPPEVAL'\n" 18806 " CanonicalDelimiter: 'cc'", 18807 RawStringFormats, ExpectedRawStringFormats); 18808 18809 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 18810 " Minimum: 0\n" 18811 " Maximum: 0", 18812 SpacesInLineCommentPrefix.Minimum, 0u); 18813 EXPECT_EQ(Style.SpacesInLineCommentPrefix.Maximum, 0u); 18814 Style.SpacesInLineCommentPrefix.Minimum = 1; 18815 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 18816 " Minimum: 2", 18817 SpacesInLineCommentPrefix.Minimum, 0u); 18818 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 18819 " Maximum: -1", 18820 SpacesInLineCommentPrefix.Maximum, -1u); 18821 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 18822 " Minimum: 2", 18823 SpacesInLineCommentPrefix.Minimum, 2u); 18824 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 18825 " Maximum: 1", 18826 SpacesInLineCommentPrefix.Maximum, 1u); 18827 EXPECT_EQ(Style.SpacesInLineCommentPrefix.Minimum, 1u); 18828 18829 Style.SpacesInAngles = FormatStyle::SIAS_Always; 18830 CHECK_PARSE("SpacesInAngles: Never", SpacesInAngles, FormatStyle::SIAS_Never); 18831 CHECK_PARSE("SpacesInAngles: Always", SpacesInAngles, 18832 FormatStyle::SIAS_Always); 18833 CHECK_PARSE("SpacesInAngles: Leave", SpacesInAngles, FormatStyle::SIAS_Leave); 18834 // For backward compatibility: 18835 CHECK_PARSE("SpacesInAngles: false", SpacesInAngles, FormatStyle::SIAS_Never); 18836 CHECK_PARSE("SpacesInAngles: true", SpacesInAngles, FormatStyle::SIAS_Always); 18837 } 18838 18839 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 18840 FormatStyle Style = {}; 18841 Style.Language = FormatStyle::LK_Cpp; 18842 CHECK_PARSE("Language: Cpp\n" 18843 "IndentWidth: 12", 18844 IndentWidth, 12u); 18845 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 18846 "IndentWidth: 34", 18847 &Style), 18848 ParseError::Unsuitable); 18849 FormatStyle BinPackedTCS = {}; 18850 BinPackedTCS.Language = FormatStyle::LK_JavaScript; 18851 EXPECT_EQ(parseConfiguration("BinPackArguments: true\n" 18852 "InsertTrailingCommas: Wrapped", 18853 &BinPackedTCS), 18854 ParseError::BinPackTrailingCommaConflict); 18855 EXPECT_EQ(12u, Style.IndentWidth); 18856 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 18857 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 18858 18859 Style.Language = FormatStyle::LK_JavaScript; 18860 CHECK_PARSE("Language: JavaScript\n" 18861 "IndentWidth: 12", 18862 IndentWidth, 12u); 18863 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 18864 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 18865 "IndentWidth: 34", 18866 &Style), 18867 ParseError::Unsuitable); 18868 EXPECT_EQ(23u, Style.IndentWidth); 18869 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 18870 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 18871 18872 CHECK_PARSE("BasedOnStyle: LLVM\n" 18873 "IndentWidth: 67", 18874 IndentWidth, 67u); 18875 18876 CHECK_PARSE("---\n" 18877 "Language: JavaScript\n" 18878 "IndentWidth: 12\n" 18879 "---\n" 18880 "Language: Cpp\n" 18881 "IndentWidth: 34\n" 18882 "...\n", 18883 IndentWidth, 12u); 18884 18885 Style.Language = FormatStyle::LK_Cpp; 18886 CHECK_PARSE("---\n" 18887 "Language: JavaScript\n" 18888 "IndentWidth: 12\n" 18889 "---\n" 18890 "Language: Cpp\n" 18891 "IndentWidth: 34\n" 18892 "...\n", 18893 IndentWidth, 34u); 18894 CHECK_PARSE("---\n" 18895 "IndentWidth: 78\n" 18896 "---\n" 18897 "Language: JavaScript\n" 18898 "IndentWidth: 56\n" 18899 "...\n", 18900 IndentWidth, 78u); 18901 18902 Style.ColumnLimit = 123; 18903 Style.IndentWidth = 234; 18904 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 18905 Style.TabWidth = 345; 18906 EXPECT_FALSE(parseConfiguration("---\n" 18907 "IndentWidth: 456\n" 18908 "BreakBeforeBraces: Allman\n" 18909 "---\n" 18910 "Language: JavaScript\n" 18911 "IndentWidth: 111\n" 18912 "TabWidth: 111\n" 18913 "---\n" 18914 "Language: Cpp\n" 18915 "BreakBeforeBraces: Stroustrup\n" 18916 "TabWidth: 789\n" 18917 "...\n", 18918 &Style)); 18919 EXPECT_EQ(123u, Style.ColumnLimit); 18920 EXPECT_EQ(456u, Style.IndentWidth); 18921 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 18922 EXPECT_EQ(789u, Style.TabWidth); 18923 18924 EXPECT_EQ(parseConfiguration("---\n" 18925 "Language: JavaScript\n" 18926 "IndentWidth: 56\n" 18927 "---\n" 18928 "IndentWidth: 78\n" 18929 "...\n", 18930 &Style), 18931 ParseError::Error); 18932 EXPECT_EQ(parseConfiguration("---\n" 18933 "Language: JavaScript\n" 18934 "IndentWidth: 56\n" 18935 "---\n" 18936 "Language: JavaScript\n" 18937 "IndentWidth: 78\n" 18938 "...\n", 18939 &Style), 18940 ParseError::Error); 18941 18942 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 18943 } 18944 18945 #undef CHECK_PARSE 18946 18947 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 18948 FormatStyle Style = {}; 18949 Style.Language = FormatStyle::LK_JavaScript; 18950 Style.BreakBeforeTernaryOperators = true; 18951 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 18952 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 18953 18954 Style.BreakBeforeTernaryOperators = true; 18955 EXPECT_EQ(0, parseConfiguration("---\n" 18956 "BasedOnStyle: Google\n" 18957 "---\n" 18958 "Language: JavaScript\n" 18959 "IndentWidth: 76\n" 18960 "...\n", 18961 &Style) 18962 .value()); 18963 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 18964 EXPECT_EQ(76u, Style.IndentWidth); 18965 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 18966 } 18967 18968 TEST_F(FormatTest, ConfigurationRoundTripTest) { 18969 FormatStyle Style = getLLVMStyle(); 18970 std::string YAML = configurationAsText(Style); 18971 FormatStyle ParsedStyle = {}; 18972 ParsedStyle.Language = FormatStyle::LK_Cpp; 18973 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 18974 EXPECT_EQ(Style, ParsedStyle); 18975 } 18976 18977 TEST_F(FormatTest, WorksFor8bitEncodings) { 18978 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 18979 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 18980 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 18981 "\"\xef\xee\xf0\xf3...\"", 18982 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 18983 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 18984 "\xef\xee\xf0\xf3...\"", 18985 getLLVMStyleWithColumns(12))); 18986 } 18987 18988 TEST_F(FormatTest, HandlesUTF8BOM) { 18989 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 18990 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 18991 format("\xef\xbb\xbf#include <iostream>")); 18992 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 18993 format("\xef\xbb\xbf\n#include <iostream>")); 18994 } 18995 18996 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 18997 #if !defined(_MSC_VER) 18998 18999 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 19000 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 19001 getLLVMStyleWithColumns(35)); 19002 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 19003 getLLVMStyleWithColumns(31)); 19004 verifyFormat("// Однажды в студёную зимнюю пору...", 19005 getLLVMStyleWithColumns(36)); 19006 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 19007 verifyFormat("/* Однажды в студёную зимнюю пору... */", 19008 getLLVMStyleWithColumns(39)); 19009 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 19010 getLLVMStyleWithColumns(35)); 19011 } 19012 19013 TEST_F(FormatTest, SplitsUTF8Strings) { 19014 // Non-printable characters' width is currently considered to be the length in 19015 // bytes in UTF8. The characters can be displayed in very different manner 19016 // (zero-width, single width with a substitution glyph, expanded to their code 19017 // (e.g. "<8d>"), so there's no single correct way to handle them. 19018 EXPECT_EQ("\"aaaaÄ\"\n" 19019 "\"\xc2\x8d\";", 19020 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 19021 EXPECT_EQ("\"aaaaaaaÄ\"\n" 19022 "\"\xc2\x8d\";", 19023 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 19024 EXPECT_EQ("\"Однажды, в \"\n" 19025 "\"студёную \"\n" 19026 "\"зимнюю \"\n" 19027 "\"пору,\"", 19028 format("\"Однажды, в студёную зимнюю пору,\"", 19029 getLLVMStyleWithColumns(13))); 19030 EXPECT_EQ( 19031 "\"一 二 三 \"\n" 19032 "\"四 五六 \"\n" 19033 "\"七 八 九 \"\n" 19034 "\"十\"", 19035 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 19036 EXPECT_EQ("\"一\t\"\n" 19037 "\"二 \t\"\n" 19038 "\"三 四 \"\n" 19039 "\"五\t\"\n" 19040 "\"六 \t\"\n" 19041 "\"七 \"\n" 19042 "\"八九十\tqq\"", 19043 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 19044 getLLVMStyleWithColumns(11))); 19045 19046 // UTF8 character in an escape sequence. 19047 EXPECT_EQ("\"aaaaaa\"\n" 19048 "\"\\\xC2\x8D\"", 19049 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 19050 } 19051 19052 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 19053 EXPECT_EQ("const char *sssss =\n" 19054 " \"一二三四五六七八\\\n" 19055 " 九 十\";", 19056 format("const char *sssss = \"一二三四五六七八\\\n" 19057 " 九 十\";", 19058 getLLVMStyleWithColumns(30))); 19059 } 19060 19061 TEST_F(FormatTest, SplitsUTF8LineComments) { 19062 EXPECT_EQ("// aaaaÄ\xc2\x8d", 19063 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 19064 EXPECT_EQ("// Я из лесу\n" 19065 "// вышел; был\n" 19066 "// сильный\n" 19067 "// мороз.", 19068 format("// Я из лесу вышел; был сильный мороз.", 19069 getLLVMStyleWithColumns(13))); 19070 EXPECT_EQ("// 一二三\n" 19071 "// 四五六七\n" 19072 "// 八 九\n" 19073 "// 十", 19074 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 19075 } 19076 19077 TEST_F(FormatTest, SplitsUTF8BlockComments) { 19078 EXPECT_EQ("/* Гляжу,\n" 19079 " * поднимается\n" 19080 " * медленно в\n" 19081 " * гору\n" 19082 " * Лошадка,\n" 19083 " * везущая\n" 19084 " * хворосту\n" 19085 " * воз. */", 19086 format("/* Гляжу, поднимается медленно в гору\n" 19087 " * Лошадка, везущая хворосту воз. */", 19088 getLLVMStyleWithColumns(13))); 19089 EXPECT_EQ( 19090 "/* 一二三\n" 19091 " * 四五六七\n" 19092 " * 八 九\n" 19093 " * 十 */", 19094 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 19095 EXPECT_EQ("/* \n" 19096 " * \n" 19097 " * - */", 19098 format("/* - */", getLLVMStyleWithColumns(12))); 19099 } 19100 19101 #endif // _MSC_VER 19102 19103 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 19104 FormatStyle Style = getLLVMStyle(); 19105 19106 Style.ConstructorInitializerIndentWidth = 4; 19107 verifyFormat( 19108 "SomeClass::Constructor()\n" 19109 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 19110 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 19111 Style); 19112 19113 Style.ConstructorInitializerIndentWidth = 2; 19114 verifyFormat( 19115 "SomeClass::Constructor()\n" 19116 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 19117 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 19118 Style); 19119 19120 Style.ConstructorInitializerIndentWidth = 0; 19121 verifyFormat( 19122 "SomeClass::Constructor()\n" 19123 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 19124 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 19125 Style); 19126 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 19127 verifyFormat( 19128 "SomeLongTemplateVariableName<\n" 19129 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 19130 Style); 19131 verifyFormat("bool smaller = 1 < " 19132 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 19133 " " 19134 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 19135 Style); 19136 19137 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 19138 verifyFormat("SomeClass::Constructor() :\n" 19139 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n" 19140 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}", 19141 Style); 19142 } 19143 19144 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 19145 FormatStyle Style = getLLVMStyle(); 19146 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 19147 Style.ConstructorInitializerIndentWidth = 4; 19148 verifyFormat("SomeClass::Constructor()\n" 19149 " : a(a)\n" 19150 " , b(b)\n" 19151 " , c(c) {}", 19152 Style); 19153 verifyFormat("SomeClass::Constructor()\n" 19154 " : a(a) {}", 19155 Style); 19156 19157 Style.ColumnLimit = 0; 19158 verifyFormat("SomeClass::Constructor()\n" 19159 " : a(a) {}", 19160 Style); 19161 verifyFormat("SomeClass::Constructor() noexcept\n" 19162 " : a(a) {}", 19163 Style); 19164 verifyFormat("SomeClass::Constructor()\n" 19165 " : a(a)\n" 19166 " , b(b)\n" 19167 " , c(c) {}", 19168 Style); 19169 verifyFormat("SomeClass::Constructor()\n" 19170 " : a(a) {\n" 19171 " foo();\n" 19172 " bar();\n" 19173 "}", 19174 Style); 19175 19176 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 19177 verifyFormat("SomeClass::Constructor()\n" 19178 " : a(a)\n" 19179 " , b(b)\n" 19180 " , c(c) {\n}", 19181 Style); 19182 verifyFormat("SomeClass::Constructor()\n" 19183 " : a(a) {\n}", 19184 Style); 19185 19186 Style.ColumnLimit = 80; 19187 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 19188 Style.ConstructorInitializerIndentWidth = 2; 19189 verifyFormat("SomeClass::Constructor()\n" 19190 " : a(a)\n" 19191 " , b(b)\n" 19192 " , c(c) {}", 19193 Style); 19194 19195 Style.ConstructorInitializerIndentWidth = 0; 19196 verifyFormat("SomeClass::Constructor()\n" 19197 ": a(a)\n" 19198 ", b(b)\n" 19199 ", c(c) {}", 19200 Style); 19201 19202 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 19203 Style.ConstructorInitializerIndentWidth = 4; 19204 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 19205 verifyFormat( 19206 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 19207 Style); 19208 verifyFormat( 19209 "SomeClass::Constructor()\n" 19210 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 19211 Style); 19212 Style.ConstructorInitializerIndentWidth = 4; 19213 Style.ColumnLimit = 60; 19214 verifyFormat("SomeClass::Constructor()\n" 19215 " : aaaaaaaa(aaaaaaaa)\n" 19216 " , aaaaaaaa(aaaaaaaa)\n" 19217 " , aaaaaaaa(aaaaaaaa) {}", 19218 Style); 19219 } 19220 19221 TEST_F(FormatTest, Destructors) { 19222 verifyFormat("void F(int &i) { i.~int(); }"); 19223 verifyFormat("void F(int &i) { i->~int(); }"); 19224 } 19225 19226 TEST_F(FormatTest, FormatsWithWebKitStyle) { 19227 FormatStyle Style = getWebKitStyle(); 19228 19229 // Don't indent in outer namespaces. 19230 verifyFormat("namespace outer {\n" 19231 "int i;\n" 19232 "namespace inner {\n" 19233 " int i;\n" 19234 "} // namespace inner\n" 19235 "} // namespace outer\n" 19236 "namespace other_outer {\n" 19237 "int i;\n" 19238 "}", 19239 Style); 19240 19241 // Don't indent case labels. 19242 verifyFormat("switch (variable) {\n" 19243 "case 1:\n" 19244 "case 2:\n" 19245 " doSomething();\n" 19246 " break;\n" 19247 "default:\n" 19248 " ++variable;\n" 19249 "}", 19250 Style); 19251 19252 // Wrap before binary operators. 19253 EXPECT_EQ("void f()\n" 19254 "{\n" 19255 " if (aaaaaaaaaaaaaaaa\n" 19256 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 19257 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 19258 " return;\n" 19259 "}", 19260 format("void f() {\n" 19261 "if (aaaaaaaaaaaaaaaa\n" 19262 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 19263 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 19264 "return;\n" 19265 "}", 19266 Style)); 19267 19268 // Allow functions on a single line. 19269 verifyFormat("void f() { return; }", Style); 19270 19271 // Allow empty blocks on a single line and insert a space in empty blocks. 19272 EXPECT_EQ("void f() { }", format("void f() {}", Style)); 19273 EXPECT_EQ("while (true) { }", format("while (true) {}", Style)); 19274 // However, don't merge non-empty short loops. 19275 EXPECT_EQ("while (true) {\n" 19276 " continue;\n" 19277 "}", 19278 format("while (true) { continue; }", Style)); 19279 19280 // Constructor initializers are formatted one per line with the "," on the 19281 // new line. 19282 verifyFormat("Constructor()\n" 19283 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 19284 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 19285 " aaaaaaaaaaaaaa)\n" 19286 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 19287 "{\n" 19288 "}", 19289 Style); 19290 verifyFormat("SomeClass::Constructor()\n" 19291 " : a(a)\n" 19292 "{\n" 19293 "}", 19294 Style); 19295 EXPECT_EQ("SomeClass::Constructor()\n" 19296 " : a(a)\n" 19297 "{\n" 19298 "}", 19299 format("SomeClass::Constructor():a(a){}", Style)); 19300 verifyFormat("SomeClass::Constructor()\n" 19301 " : a(a)\n" 19302 " , b(b)\n" 19303 " , c(c)\n" 19304 "{\n" 19305 "}", 19306 Style); 19307 verifyFormat("SomeClass::Constructor()\n" 19308 " : a(a)\n" 19309 "{\n" 19310 " foo();\n" 19311 " bar();\n" 19312 "}", 19313 Style); 19314 19315 // Access specifiers should be aligned left. 19316 verifyFormat("class C {\n" 19317 "public:\n" 19318 " int i;\n" 19319 "};", 19320 Style); 19321 19322 // Do not align comments. 19323 verifyFormat("int a; // Do not\n" 19324 "double b; // align comments.", 19325 Style); 19326 19327 // Do not align operands. 19328 EXPECT_EQ("ASSERT(aaaa\n" 19329 " || bbbb);", 19330 format("ASSERT ( aaaa\n||bbbb);", Style)); 19331 19332 // Accept input's line breaks. 19333 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 19334 " || bbbbbbbbbbbbbbb) {\n" 19335 " i++;\n" 19336 "}", 19337 format("if (aaaaaaaaaaaaaaa\n" 19338 "|| bbbbbbbbbbbbbbb) { i++; }", 19339 Style)); 19340 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 19341 " i++;\n" 19342 "}", 19343 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 19344 19345 // Don't automatically break all macro definitions (llvm.org/PR17842). 19346 verifyFormat("#define aNumber 10", Style); 19347 // However, generally keep the line breaks that the user authored. 19348 EXPECT_EQ("#define aNumber \\\n" 19349 " 10", 19350 format("#define aNumber \\\n" 19351 " 10", 19352 Style)); 19353 19354 // Keep empty and one-element array literals on a single line. 19355 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 19356 " copyItems:YES];", 19357 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 19358 "copyItems:YES];", 19359 Style)); 19360 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 19361 " copyItems:YES];", 19362 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 19363 " copyItems:YES];", 19364 Style)); 19365 // FIXME: This does not seem right, there should be more indentation before 19366 // the array literal's entries. Nested blocks have the same problem. 19367 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 19368 " @\"a\",\n" 19369 " @\"a\"\n" 19370 "]\n" 19371 " copyItems:YES];", 19372 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 19373 " @\"a\",\n" 19374 " @\"a\"\n" 19375 " ]\n" 19376 " copyItems:YES];", 19377 Style)); 19378 EXPECT_EQ( 19379 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 19380 " copyItems:YES];", 19381 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 19382 " copyItems:YES];", 19383 Style)); 19384 19385 verifyFormat("[self.a b:c c:d];", Style); 19386 EXPECT_EQ("[self.a b:c\n" 19387 " c:d];", 19388 format("[self.a b:c\n" 19389 "c:d];", 19390 Style)); 19391 } 19392 19393 TEST_F(FormatTest, FormatsLambdas) { 19394 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 19395 verifyFormat( 19396 "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();\n"); 19397 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 19398 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 19399 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 19400 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 19401 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 19402 verifyFormat("auto c = [a = [b = 42] {}] {};\n"); 19403 verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n"); 19404 verifyFormat("int x = f(*+[] {});"); 19405 verifyFormat("void f() {\n" 19406 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 19407 "}\n"); 19408 verifyFormat("void f() {\n" 19409 " other(x.begin(), //\n" 19410 " x.end(), //\n" 19411 " [&](int, int) { return 1; });\n" 19412 "}\n"); 19413 verifyFormat("void f() {\n" 19414 " other.other.other.other.other(\n" 19415 " x.begin(), x.end(),\n" 19416 " [something, rather](int, int, int, int, int, int, int) { " 19417 "return 1; });\n" 19418 "}\n"); 19419 verifyFormat( 19420 "void f() {\n" 19421 " other.other.other.other.other(\n" 19422 " x.begin(), x.end(),\n" 19423 " [something, rather](int, int, int, int, int, int, int) {\n" 19424 " //\n" 19425 " });\n" 19426 "}\n"); 19427 verifyFormat("SomeFunction([]() { // A cool function...\n" 19428 " return 43;\n" 19429 "});"); 19430 EXPECT_EQ("SomeFunction([]() {\n" 19431 "#define A a\n" 19432 " return 43;\n" 19433 "});", 19434 format("SomeFunction([](){\n" 19435 "#define A a\n" 19436 "return 43;\n" 19437 "});")); 19438 verifyFormat("void f() {\n" 19439 " SomeFunction([](decltype(x), A *a) {});\n" 19440 " SomeFunction([](typeof(x), A *a) {});\n" 19441 " SomeFunction([](_Atomic(x), A *a) {});\n" 19442 " SomeFunction([](__underlying_type(x), A *a) {});\n" 19443 "}"); 19444 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 19445 " [](const aaaaaaaaaa &a) { return a; });"); 19446 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 19447 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 19448 "});"); 19449 verifyFormat("Constructor()\n" 19450 " : Field([] { // comment\n" 19451 " int i;\n" 19452 " }) {}"); 19453 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 19454 " return some_parameter.size();\n" 19455 "};"); 19456 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 19457 " [](const string &s) { return s; };"); 19458 verifyFormat("int i = aaaaaa ? 1 //\n" 19459 " : [] {\n" 19460 " return 2; //\n" 19461 " }();"); 19462 verifyFormat("llvm::errs() << \"number of twos is \"\n" 19463 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 19464 " return x == 2; // force break\n" 19465 " });"); 19466 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 19467 " [=](int iiiiiiiiiiii) {\n" 19468 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n" 19469 " aaaaaaaaaaaaaaaaaaaaaaa;\n" 19470 " });", 19471 getLLVMStyleWithColumns(60)); 19472 19473 verifyFormat("SomeFunction({[&] {\n" 19474 " // comment\n" 19475 " },\n" 19476 " [&] {\n" 19477 " // comment\n" 19478 " }});"); 19479 verifyFormat("SomeFunction({[&] {\n" 19480 " // comment\n" 19481 "}});"); 19482 verifyFormat( 19483 "virtual aaaaaaaaaaaaaaaa(\n" 19484 " std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n" 19485 " aaaaa aaaaaaaaa);"); 19486 19487 // Lambdas with return types. 19488 verifyFormat("int c = []() -> int { return 2; }();\n"); 19489 verifyFormat("int c = []() -> int * { return 2; }();\n"); 19490 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 19491 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 19492 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 19493 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 19494 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 19495 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 19496 verifyFormat("[a, a]() -> a<1> {};"); 19497 verifyFormat("[]() -> foo<5 + 2> { return {}; };"); 19498 verifyFormat("[]() -> foo<5 - 2> { return {}; };"); 19499 verifyFormat("[]() -> foo<5 / 2> { return {}; };"); 19500 verifyFormat("[]() -> foo<5 * 2> { return {}; };"); 19501 verifyFormat("[]() -> foo<5 % 2> { return {}; };"); 19502 verifyFormat("[]() -> foo<5 << 2> { return {}; };"); 19503 verifyFormat("[]() -> foo<!5> { return {}; };"); 19504 verifyFormat("[]() -> foo<~5> { return {}; };"); 19505 verifyFormat("[]() -> foo<5 | 2> { return {}; };"); 19506 verifyFormat("[]() -> foo<5 || 2> { return {}; };"); 19507 verifyFormat("[]() -> foo<5 & 2> { return {}; };"); 19508 verifyFormat("[]() -> foo<5 && 2> { return {}; };"); 19509 verifyFormat("[]() -> foo<5 == 2> { return {}; };"); 19510 verifyFormat("[]() -> foo<5 != 2> { return {}; };"); 19511 verifyFormat("[]() -> foo<5 >= 2> { return {}; };"); 19512 verifyFormat("[]() -> foo<5 <= 2> { return {}; };"); 19513 verifyFormat("[]() -> foo<5 < 2> { return {}; };"); 19514 verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };"); 19515 verifyFormat("namespace bar {\n" 19516 "// broken:\n" 19517 "auto foo{[]() -> foo<5 + 2> { return {}; }};\n" 19518 "} // namespace bar"); 19519 verifyFormat("namespace bar {\n" 19520 "// broken:\n" 19521 "auto foo{[]() -> foo<5 - 2> { return {}; }};\n" 19522 "} // namespace bar"); 19523 verifyFormat("namespace bar {\n" 19524 "// broken:\n" 19525 "auto foo{[]() -> foo<5 / 2> { return {}; }};\n" 19526 "} // namespace bar"); 19527 verifyFormat("namespace bar {\n" 19528 "// broken:\n" 19529 "auto foo{[]() -> foo<5 * 2> { return {}; }};\n" 19530 "} // namespace bar"); 19531 verifyFormat("namespace bar {\n" 19532 "// broken:\n" 19533 "auto foo{[]() -> foo<5 % 2> { return {}; }};\n" 19534 "} // namespace bar"); 19535 verifyFormat("namespace bar {\n" 19536 "// broken:\n" 19537 "auto foo{[]() -> foo<5 << 2> { return {}; }};\n" 19538 "} // namespace bar"); 19539 verifyFormat("namespace bar {\n" 19540 "// broken:\n" 19541 "auto foo{[]() -> foo<!5> { return {}; }};\n" 19542 "} // namespace bar"); 19543 verifyFormat("namespace bar {\n" 19544 "// broken:\n" 19545 "auto foo{[]() -> foo<~5> { return {}; }};\n" 19546 "} // namespace bar"); 19547 verifyFormat("namespace bar {\n" 19548 "// broken:\n" 19549 "auto foo{[]() -> foo<5 | 2> { return {}; }};\n" 19550 "} // namespace bar"); 19551 verifyFormat("namespace bar {\n" 19552 "// broken:\n" 19553 "auto foo{[]() -> foo<5 || 2> { return {}; }};\n" 19554 "} // namespace bar"); 19555 verifyFormat("namespace bar {\n" 19556 "// broken:\n" 19557 "auto foo{[]() -> foo<5 & 2> { return {}; }};\n" 19558 "} // namespace bar"); 19559 verifyFormat("namespace bar {\n" 19560 "// broken:\n" 19561 "auto foo{[]() -> foo<5 && 2> { return {}; }};\n" 19562 "} // namespace bar"); 19563 verifyFormat("namespace bar {\n" 19564 "// broken:\n" 19565 "auto foo{[]() -> foo<5 == 2> { return {}; }};\n" 19566 "} // namespace bar"); 19567 verifyFormat("namespace bar {\n" 19568 "// broken:\n" 19569 "auto foo{[]() -> foo<5 != 2> { return {}; }};\n" 19570 "} // namespace bar"); 19571 verifyFormat("namespace bar {\n" 19572 "// broken:\n" 19573 "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n" 19574 "} // namespace bar"); 19575 verifyFormat("namespace bar {\n" 19576 "// broken:\n" 19577 "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n" 19578 "} // namespace bar"); 19579 verifyFormat("namespace bar {\n" 19580 "// broken:\n" 19581 "auto foo{[]() -> foo<5 < 2> { return {}; }};\n" 19582 "} // namespace bar"); 19583 verifyFormat("namespace bar {\n" 19584 "// broken:\n" 19585 "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n" 19586 "} // namespace bar"); 19587 verifyFormat("[]() -> a<1> {};"); 19588 verifyFormat("[]() -> a<1> { ; };"); 19589 verifyFormat("[]() -> a<1> { ; }();"); 19590 verifyFormat("[a, a]() -> a<true> {};"); 19591 verifyFormat("[]() -> a<true> {};"); 19592 verifyFormat("[]() -> a<true> { ; };"); 19593 verifyFormat("[]() -> a<true> { ; }();"); 19594 verifyFormat("[a, a]() -> a<false> {};"); 19595 verifyFormat("[]() -> a<false> {};"); 19596 verifyFormat("[]() -> a<false> { ; };"); 19597 verifyFormat("[]() -> a<false> { ; }();"); 19598 verifyFormat("auto foo{[]() -> foo<false> { ; }};"); 19599 verifyFormat("namespace bar {\n" 19600 "auto foo{[]() -> foo<false> { ; }};\n" 19601 "} // namespace bar"); 19602 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 19603 " int j) -> int {\n" 19604 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 19605 "};"); 19606 verifyFormat( 19607 "aaaaaaaaaaaaaaaaaaaaaa(\n" 19608 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 19609 " return aaaaaaaaaaaaaaaaa;\n" 19610 " });", 19611 getLLVMStyleWithColumns(70)); 19612 verifyFormat("[]() //\n" 19613 " -> int {\n" 19614 " return 1; //\n" 19615 "};"); 19616 verifyFormat("[]() -> Void<T...> {};"); 19617 verifyFormat("[a, b]() -> Tuple<T...> { return {}; };"); 19618 19619 // Lambdas with explicit template argument lists. 19620 verifyFormat( 19621 "auto L = []<template <typename> class T, class U>(T<U> &&a) {};\n"); 19622 19623 // Multiple lambdas in the same parentheses change indentation rules. These 19624 // lambdas are forced to start on new lines. 19625 verifyFormat("SomeFunction(\n" 19626 " []() {\n" 19627 " //\n" 19628 " },\n" 19629 " []() {\n" 19630 " //\n" 19631 " });"); 19632 19633 // A lambda passed as arg0 is always pushed to the next line. 19634 verifyFormat("SomeFunction(\n" 19635 " [this] {\n" 19636 " //\n" 19637 " },\n" 19638 " 1);\n"); 19639 19640 // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like 19641 // the arg0 case above. 19642 auto Style = getGoogleStyle(); 19643 Style.BinPackArguments = false; 19644 verifyFormat("SomeFunction(\n" 19645 " a,\n" 19646 " [this] {\n" 19647 " //\n" 19648 " },\n" 19649 " b);\n", 19650 Style); 19651 verifyFormat("SomeFunction(\n" 19652 " a,\n" 19653 " [this] {\n" 19654 " //\n" 19655 " },\n" 19656 " b);\n"); 19657 19658 // A lambda with a very long line forces arg0 to be pushed out irrespective of 19659 // the BinPackArguments value (as long as the code is wide enough). 19660 verifyFormat( 19661 "something->SomeFunction(\n" 19662 " a,\n" 19663 " [this] {\n" 19664 " " 19665 "D0000000000000000000000000000000000000000000000000000000000001();\n" 19666 " },\n" 19667 " b);\n"); 19668 19669 // A multi-line lambda is pulled up as long as the introducer fits on the 19670 // previous line and there are no further args. 19671 verifyFormat("function(1, [this, that] {\n" 19672 " //\n" 19673 "});\n"); 19674 verifyFormat("function([this, that] {\n" 19675 " //\n" 19676 "});\n"); 19677 // FIXME: this format is not ideal and we should consider forcing the first 19678 // arg onto its own line. 19679 verifyFormat("function(a, b, c, //\n" 19680 " d, [this, that] {\n" 19681 " //\n" 19682 " });\n"); 19683 19684 // Multiple lambdas are treated correctly even when there is a short arg0. 19685 verifyFormat("SomeFunction(\n" 19686 " 1,\n" 19687 " [this] {\n" 19688 " //\n" 19689 " },\n" 19690 " [this] {\n" 19691 " //\n" 19692 " },\n" 19693 " 1);\n"); 19694 19695 // More complex introducers. 19696 verifyFormat("return [i, args...] {};"); 19697 19698 // Not lambdas. 19699 verifyFormat("constexpr char hello[]{\"hello\"};"); 19700 verifyFormat("double &operator[](int i) { return 0; }\n" 19701 "int i;"); 19702 verifyFormat("std::unique_ptr<int[]> foo() {}"); 19703 verifyFormat("int i = a[a][a]->f();"); 19704 verifyFormat("int i = (*b)[a]->f();"); 19705 19706 // Other corner cases. 19707 verifyFormat("void f() {\n" 19708 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 19709 " );\n" 19710 "}"); 19711 19712 // Lambdas created through weird macros. 19713 verifyFormat("void f() {\n" 19714 " MACRO((const AA &a) { return 1; });\n" 19715 " MACRO((AA &a) { return 1; });\n" 19716 "}"); 19717 19718 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 19719 " doo_dah();\n" 19720 " doo_dah();\n" 19721 " })) {\n" 19722 "}"); 19723 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n" 19724 " doo_dah();\n" 19725 " doo_dah();\n" 19726 " })) {\n" 19727 "}"); 19728 verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n" 19729 " doo_dah();\n" 19730 " doo_dah();\n" 19731 " })) {\n" 19732 "}"); 19733 verifyFormat("auto lambda = []() {\n" 19734 " int a = 2\n" 19735 "#if A\n" 19736 " + 2\n" 19737 "#endif\n" 19738 " ;\n" 19739 "};"); 19740 19741 // Lambdas with complex multiline introducers. 19742 verifyFormat( 19743 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 19744 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n" 19745 " -> ::std::unordered_set<\n" 19746 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 19747 " //\n" 19748 " });"); 19749 19750 FormatStyle DoNotMerge = getLLVMStyle(); 19751 DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 19752 verifyFormat("auto c = []() {\n" 19753 " return b;\n" 19754 "};", 19755 "auto c = []() { return b; };", DoNotMerge); 19756 verifyFormat("auto c = []() {\n" 19757 "};", 19758 " auto c = []() {};", DoNotMerge); 19759 19760 FormatStyle MergeEmptyOnly = getLLVMStyle(); 19761 MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty; 19762 verifyFormat("auto c = []() {\n" 19763 " return b;\n" 19764 "};", 19765 "auto c = []() {\n" 19766 " return b;\n" 19767 " };", 19768 MergeEmptyOnly); 19769 verifyFormat("auto c = []() {};", 19770 "auto c = []() {\n" 19771 "};", 19772 MergeEmptyOnly); 19773 19774 FormatStyle MergeInline = getLLVMStyle(); 19775 MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline; 19776 verifyFormat("auto c = []() {\n" 19777 " return b;\n" 19778 "};", 19779 "auto c = []() { return b; };", MergeInline); 19780 verifyFormat("function([]() { return b; })", "function([]() { return b; })", 19781 MergeInline); 19782 verifyFormat("function([]() { return b; }, a)", 19783 "function([]() { return b; }, a)", MergeInline); 19784 verifyFormat("function(a, []() { return b; })", 19785 "function(a, []() { return b; })", MergeInline); 19786 19787 // Check option "BraceWrapping.BeforeLambdaBody" and different state of 19788 // AllowShortLambdasOnASingleLine 19789 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle(); 19790 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 19791 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 19792 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 19793 FormatStyle::ShortLambdaStyle::SLS_None; 19794 verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n" 19795 " []()\n" 19796 " {\n" 19797 " return 17;\n" 19798 " });", 19799 LLVMWithBeforeLambdaBody); 19800 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n" 19801 " []()\n" 19802 " {\n" 19803 " });", 19804 LLVMWithBeforeLambdaBody); 19805 verifyFormat("auto fct_SLS_None = []()\n" 19806 "{\n" 19807 " return 17;\n" 19808 "};", 19809 LLVMWithBeforeLambdaBody); 19810 verifyFormat("TwoNestedLambdas_SLS_None(\n" 19811 " []()\n" 19812 " {\n" 19813 " return Call(\n" 19814 " []()\n" 19815 " {\n" 19816 " return 17;\n" 19817 " });\n" 19818 " });", 19819 LLVMWithBeforeLambdaBody); 19820 verifyFormat("void Fct() {\n" 19821 " return {[]()\n" 19822 " {\n" 19823 " return 17;\n" 19824 " }};\n" 19825 "}", 19826 LLVMWithBeforeLambdaBody); 19827 19828 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 19829 FormatStyle::ShortLambdaStyle::SLS_Empty; 19830 verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n" 19831 " []()\n" 19832 " {\n" 19833 " return 17;\n" 19834 " });", 19835 LLVMWithBeforeLambdaBody); 19836 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});", 19837 LLVMWithBeforeLambdaBody); 19838 verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL" 19839 "ongFunctionName_SLS_Empty(\n" 19840 " []() {});", 19841 LLVMWithBeforeLambdaBody); 19842 verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n" 19843 " []()\n" 19844 " {\n" 19845 " return 17;\n" 19846 " });", 19847 LLVMWithBeforeLambdaBody); 19848 verifyFormat("auto fct_SLS_Empty = []()\n" 19849 "{\n" 19850 " return 17;\n" 19851 "};", 19852 LLVMWithBeforeLambdaBody); 19853 verifyFormat("TwoNestedLambdas_SLS_Empty(\n" 19854 " []()\n" 19855 " {\n" 19856 " return Call([]() {});\n" 19857 " });", 19858 LLVMWithBeforeLambdaBody); 19859 verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n" 19860 " []()\n" 19861 " {\n" 19862 " return Call([]() {});\n" 19863 " });", 19864 LLVMWithBeforeLambdaBody); 19865 verifyFormat( 19866 "FctWithLongLineInLambda_SLS_Empty(\n" 19867 " []()\n" 19868 " {\n" 19869 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 19870 " AndShouldNotBeConsiderAsInline,\n" 19871 " LambdaBodyMustBeBreak);\n" 19872 " });", 19873 LLVMWithBeforeLambdaBody); 19874 19875 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 19876 FormatStyle::ShortLambdaStyle::SLS_Inline; 19877 verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });", 19878 LLVMWithBeforeLambdaBody); 19879 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});", 19880 LLVMWithBeforeLambdaBody); 19881 verifyFormat("auto fct_SLS_Inline = []()\n" 19882 "{\n" 19883 " return 17;\n" 19884 "};", 19885 LLVMWithBeforeLambdaBody); 19886 verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return " 19887 "17; }); });", 19888 LLVMWithBeforeLambdaBody); 19889 verifyFormat( 19890 "FctWithLongLineInLambda_SLS_Inline(\n" 19891 " []()\n" 19892 " {\n" 19893 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 19894 " AndShouldNotBeConsiderAsInline,\n" 19895 " LambdaBodyMustBeBreak);\n" 19896 " });", 19897 LLVMWithBeforeLambdaBody); 19898 verifyFormat("FctWithMultipleParams_SLS_Inline(" 19899 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n" 19900 " []() { return 17; });", 19901 LLVMWithBeforeLambdaBody); 19902 verifyFormat( 19903 "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });", 19904 LLVMWithBeforeLambdaBody); 19905 19906 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 19907 FormatStyle::ShortLambdaStyle::SLS_All; 19908 verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });", 19909 LLVMWithBeforeLambdaBody); 19910 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});", 19911 LLVMWithBeforeLambdaBody); 19912 verifyFormat("auto fct_SLS_All = []() { return 17; };", 19913 LLVMWithBeforeLambdaBody); 19914 verifyFormat("FctWithOneParam_SLS_All(\n" 19915 " []()\n" 19916 " {\n" 19917 " // A cool function...\n" 19918 " return 43;\n" 19919 " });", 19920 LLVMWithBeforeLambdaBody); 19921 verifyFormat("FctWithMultipleParams_SLS_All(" 19922 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n" 19923 " []() { return 17; });", 19924 LLVMWithBeforeLambdaBody); 19925 verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });", 19926 LLVMWithBeforeLambdaBody); 19927 verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });", 19928 LLVMWithBeforeLambdaBody); 19929 verifyFormat( 19930 "FctWithLongLineInLambda_SLS_All(\n" 19931 " []()\n" 19932 " {\n" 19933 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 19934 " AndShouldNotBeConsiderAsInline,\n" 19935 " LambdaBodyMustBeBreak);\n" 19936 " });", 19937 LLVMWithBeforeLambdaBody); 19938 verifyFormat( 19939 "auto fct_SLS_All = []()\n" 19940 "{\n" 19941 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 19942 " AndShouldNotBeConsiderAsInline,\n" 19943 " LambdaBodyMustBeBreak);\n" 19944 "};", 19945 LLVMWithBeforeLambdaBody); 19946 LLVMWithBeforeLambdaBody.BinPackParameters = false; 19947 verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);", 19948 LLVMWithBeforeLambdaBody); 19949 verifyFormat( 19950 "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n" 19951 " FirstParam,\n" 19952 " SecondParam,\n" 19953 " ThirdParam,\n" 19954 " FourthParam);", 19955 LLVMWithBeforeLambdaBody); 19956 verifyFormat("FctWithLongLineInLambda_SLS_All(\n" 19957 " []() { return " 19958 "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n" 19959 " FirstParam,\n" 19960 " SecondParam,\n" 19961 " ThirdParam,\n" 19962 " FourthParam);", 19963 LLVMWithBeforeLambdaBody); 19964 verifyFormat( 19965 "FctWithLongLineInLambda_SLS_All(FirstParam,\n" 19966 " SecondParam,\n" 19967 " ThirdParam,\n" 19968 " FourthParam,\n" 19969 " []() { return SomeValueNotSoLong; });", 19970 LLVMWithBeforeLambdaBody); 19971 verifyFormat("FctWithLongLineInLambda_SLS_All(\n" 19972 " []()\n" 19973 " {\n" 19974 " return " 19975 "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB" 19976 "eConsiderAsInline;\n" 19977 " });", 19978 LLVMWithBeforeLambdaBody); 19979 verifyFormat( 19980 "FctWithLongLineInLambda_SLS_All(\n" 19981 " []()\n" 19982 " {\n" 19983 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 19984 " AndShouldNotBeConsiderAsInline,\n" 19985 " LambdaBodyMustBeBreak);\n" 19986 " });", 19987 LLVMWithBeforeLambdaBody); 19988 verifyFormat("FctWithTwoParams_SLS_All(\n" 19989 " []()\n" 19990 " {\n" 19991 " // A cool function...\n" 19992 " return 43;\n" 19993 " },\n" 19994 " 87);", 19995 LLVMWithBeforeLambdaBody); 19996 verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);", 19997 LLVMWithBeforeLambdaBody); 19998 verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });", 19999 LLVMWithBeforeLambdaBody); 20000 verifyFormat( 20001 "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });", 20002 LLVMWithBeforeLambdaBody); 20003 verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; " 20004 "}); }, x);", 20005 LLVMWithBeforeLambdaBody); 20006 verifyFormat("TwoNestedLambdas_SLS_All(\n" 20007 " []()\n" 20008 " {\n" 20009 " // A cool function...\n" 20010 " return Call([]() { return 17; });\n" 20011 " });", 20012 LLVMWithBeforeLambdaBody); 20013 verifyFormat("TwoNestedLambdas_SLS_All(\n" 20014 " []()\n" 20015 " {\n" 20016 " return Call(\n" 20017 " []()\n" 20018 " {\n" 20019 " // A cool function...\n" 20020 " return 17;\n" 20021 " });\n" 20022 " });", 20023 LLVMWithBeforeLambdaBody); 20024 20025 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20026 FormatStyle::ShortLambdaStyle::SLS_None; 20027 20028 verifyFormat("auto select = [this]() -> const Library::Object *\n" 20029 "{\n" 20030 " return MyAssignment::SelectFromList(this);\n" 20031 "};\n", 20032 LLVMWithBeforeLambdaBody); 20033 20034 verifyFormat("auto select = [this]() -> const Library::Object &\n" 20035 "{\n" 20036 " return MyAssignment::SelectFromList(this);\n" 20037 "};\n", 20038 LLVMWithBeforeLambdaBody); 20039 20040 verifyFormat("auto select = [this]() -> std::unique_ptr<Object>\n" 20041 "{\n" 20042 " return MyAssignment::SelectFromList(this);\n" 20043 "};\n", 20044 LLVMWithBeforeLambdaBody); 20045 20046 verifyFormat("namespace test {\n" 20047 "class Test {\n" 20048 "public:\n" 20049 " Test() = default;\n" 20050 "};\n" 20051 "} // namespace test", 20052 LLVMWithBeforeLambdaBody); 20053 20054 // Lambdas with different indentation styles. 20055 Style = getLLVMStyleWithColumns(100); 20056 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 20057 " return promise.then(\n" 20058 " [this, &someVariable, someObject = " 20059 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 20060 " return someObject.startAsyncAction().then(\n" 20061 " [this, &someVariable](AsyncActionResult result) " 20062 "mutable { result.processMore(); });\n" 20063 " });\n" 20064 "}\n", 20065 format("SomeResult doSomething(SomeObject promise) {\n" 20066 " return promise.then([this, &someVariable, someObject = " 20067 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 20068 " return someObject.startAsyncAction().then([this, " 20069 "&someVariable](AsyncActionResult result) mutable {\n" 20070 " result.processMore();\n" 20071 " });\n" 20072 " });\n" 20073 "}\n", 20074 Style)); 20075 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope; 20076 verifyFormat("test() {\n" 20077 " ([]() -> {\n" 20078 " int b = 32;\n" 20079 " return 3;\n" 20080 " }).foo();\n" 20081 "}", 20082 Style); 20083 verifyFormat("test() {\n" 20084 " []() -> {\n" 20085 " int b = 32;\n" 20086 " return 3;\n" 20087 " }\n" 20088 "}", 20089 Style); 20090 verifyFormat("std::sort(v.begin(), v.end(),\n" 20091 " [](const auto &someLongArgumentName, const auto " 20092 "&someOtherLongArgumentName) {\n" 20093 " return someLongArgumentName.someMemberVariable < " 20094 "someOtherLongArgumentName.someMemberVariable;\n" 20095 "});", 20096 Style); 20097 verifyFormat("test() {\n" 20098 " (\n" 20099 " []() -> {\n" 20100 " int b = 32;\n" 20101 " return 3;\n" 20102 " },\n" 20103 " foo, bar)\n" 20104 " .foo();\n" 20105 "}", 20106 Style); 20107 verifyFormat("test() {\n" 20108 " ([]() -> {\n" 20109 " int b = 32;\n" 20110 " return 3;\n" 20111 " })\n" 20112 " .foo()\n" 20113 " .bar();\n" 20114 "}", 20115 Style); 20116 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 20117 " return promise.then(\n" 20118 " [this, &someVariable, someObject = " 20119 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 20120 " return someObject.startAsyncAction().then(\n" 20121 " [this, &someVariable](AsyncActionResult result) mutable { " 20122 "result.processMore(); });\n" 20123 " });\n" 20124 "}\n", 20125 format("SomeResult doSomething(SomeObject promise) {\n" 20126 " return promise.then([this, &someVariable, someObject = " 20127 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 20128 " return someObject.startAsyncAction().then([this, " 20129 "&someVariable](AsyncActionResult result) mutable {\n" 20130 " result.processMore();\n" 20131 " });\n" 20132 " });\n" 20133 "}\n", 20134 Style)); 20135 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 20136 " return promise.then([this, &someVariable] {\n" 20137 " return someObject.startAsyncAction().then(\n" 20138 " [this, &someVariable](AsyncActionResult result) mutable { " 20139 "result.processMore(); });\n" 20140 " });\n" 20141 "}\n", 20142 format("SomeResult doSomething(SomeObject promise) {\n" 20143 " return promise.then([this, &someVariable] {\n" 20144 " return someObject.startAsyncAction().then([this, " 20145 "&someVariable](AsyncActionResult result) mutable {\n" 20146 " result.processMore();\n" 20147 " });\n" 20148 " });\n" 20149 "}\n", 20150 Style)); 20151 Style = getGoogleStyle(); 20152 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope; 20153 EXPECT_EQ("#define A \\\n" 20154 " [] { \\\n" 20155 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 20156 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 20157 " }", 20158 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 20159 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 20160 Style)); 20161 // TODO: The current formatting has a minor issue that's not worth fixing 20162 // right now whereby the closing brace is indented relative to the signature 20163 // instead of being aligned. This only happens with macros. 20164 } 20165 20166 TEST_F(FormatTest, LambdaWithLineComments) { 20167 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle(); 20168 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 20169 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 20170 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20171 FormatStyle::ShortLambdaStyle::SLS_All; 20172 20173 verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody); 20174 verifyFormat("auto k = []() // comment\n" 20175 "{ return; }", 20176 LLVMWithBeforeLambdaBody); 20177 verifyFormat("auto k = []() /* comment */ { return; }", 20178 LLVMWithBeforeLambdaBody); 20179 verifyFormat("auto k = []() /* comment */ /* comment */ { return; }", 20180 LLVMWithBeforeLambdaBody); 20181 verifyFormat("auto k = []() // X\n" 20182 "{ return; }", 20183 LLVMWithBeforeLambdaBody); 20184 verifyFormat( 20185 "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n" 20186 "{ return; }", 20187 LLVMWithBeforeLambdaBody); 20188 } 20189 20190 TEST_F(FormatTest, EmptyLinesInLambdas) { 20191 verifyFormat("auto lambda = []() {\n" 20192 " x(); //\n" 20193 "};", 20194 "auto lambda = []() {\n" 20195 "\n" 20196 " x(); //\n" 20197 "\n" 20198 "};"); 20199 } 20200 20201 TEST_F(FormatTest, FormatsBlocks) { 20202 FormatStyle ShortBlocks = getLLVMStyle(); 20203 ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 20204 verifyFormat("int (^Block)(int, int);", ShortBlocks); 20205 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 20206 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 20207 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 20208 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 20209 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 20210 20211 verifyFormat("foo(^{ bar(); });", ShortBlocks); 20212 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 20213 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 20214 20215 verifyFormat("[operation setCompletionBlock:^{\n" 20216 " [self onOperationDone];\n" 20217 "}];"); 20218 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 20219 " [self onOperationDone];\n" 20220 "}]};"); 20221 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 20222 " f();\n" 20223 "}];"); 20224 verifyFormat("int a = [operation block:^int(int *i) {\n" 20225 " return 1;\n" 20226 "}];"); 20227 verifyFormat("[myObject doSomethingWith:arg1\n" 20228 " aaa:^int(int *a) {\n" 20229 " return 1;\n" 20230 " }\n" 20231 " bbb:f(a * bbbbbbbb)];"); 20232 20233 verifyFormat("[operation setCompletionBlock:^{\n" 20234 " [self.delegate newDataAvailable];\n" 20235 "}];", 20236 getLLVMStyleWithColumns(60)); 20237 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 20238 " NSString *path = [self sessionFilePath];\n" 20239 " if (path) {\n" 20240 " // ...\n" 20241 " }\n" 20242 "});"); 20243 verifyFormat("[[SessionService sharedService]\n" 20244 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 20245 " if (window) {\n" 20246 " [self windowDidLoad:window];\n" 20247 " } else {\n" 20248 " [self errorLoadingWindow];\n" 20249 " }\n" 20250 " }];"); 20251 verifyFormat("void (^largeBlock)(void) = ^{\n" 20252 " // ...\n" 20253 "};\n", 20254 getLLVMStyleWithColumns(40)); 20255 verifyFormat("[[SessionService sharedService]\n" 20256 " loadWindowWithCompletionBlock: //\n" 20257 " ^(SessionWindow *window) {\n" 20258 " if (window) {\n" 20259 " [self windowDidLoad:window];\n" 20260 " } else {\n" 20261 " [self errorLoadingWindow];\n" 20262 " }\n" 20263 " }];", 20264 getLLVMStyleWithColumns(60)); 20265 verifyFormat("[myObject doSomethingWith:arg1\n" 20266 " firstBlock:^(Foo *a) {\n" 20267 " // ...\n" 20268 " int i;\n" 20269 " }\n" 20270 " secondBlock:^(Bar *b) {\n" 20271 " // ...\n" 20272 " int i;\n" 20273 " }\n" 20274 " thirdBlock:^Foo(Bar *b) {\n" 20275 " // ...\n" 20276 " int i;\n" 20277 " }];"); 20278 verifyFormat("[myObject doSomethingWith:arg1\n" 20279 " firstBlock:-1\n" 20280 " secondBlock:^(Bar *b) {\n" 20281 " // ...\n" 20282 " int i;\n" 20283 " }];"); 20284 20285 verifyFormat("f(^{\n" 20286 " @autoreleasepool {\n" 20287 " if (a) {\n" 20288 " g();\n" 20289 " }\n" 20290 " }\n" 20291 "});"); 20292 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 20293 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" 20294 "};"); 20295 20296 FormatStyle FourIndent = getLLVMStyle(); 20297 FourIndent.ObjCBlockIndentWidth = 4; 20298 verifyFormat("[operation setCompletionBlock:^{\n" 20299 " [self onOperationDone];\n" 20300 "}];", 20301 FourIndent); 20302 } 20303 20304 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 20305 FormatStyle ZeroColumn = getLLVMStyle(); 20306 ZeroColumn.ColumnLimit = 0; 20307 20308 verifyFormat("[[SessionService sharedService] " 20309 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 20310 " if (window) {\n" 20311 " [self windowDidLoad:window];\n" 20312 " } else {\n" 20313 " [self errorLoadingWindow];\n" 20314 " }\n" 20315 "}];", 20316 ZeroColumn); 20317 EXPECT_EQ("[[SessionService sharedService]\n" 20318 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 20319 " if (window) {\n" 20320 " [self windowDidLoad:window];\n" 20321 " } else {\n" 20322 " [self errorLoadingWindow];\n" 20323 " }\n" 20324 " }];", 20325 format("[[SessionService sharedService]\n" 20326 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 20327 " if (window) {\n" 20328 " [self windowDidLoad:window];\n" 20329 " } else {\n" 20330 " [self errorLoadingWindow];\n" 20331 " }\n" 20332 "}];", 20333 ZeroColumn)); 20334 verifyFormat("[myObject doSomethingWith:arg1\n" 20335 " firstBlock:^(Foo *a) {\n" 20336 " // ...\n" 20337 " int i;\n" 20338 " }\n" 20339 " secondBlock:^(Bar *b) {\n" 20340 " // ...\n" 20341 " int i;\n" 20342 " }\n" 20343 " thirdBlock:^Foo(Bar *b) {\n" 20344 " // ...\n" 20345 " int i;\n" 20346 " }];", 20347 ZeroColumn); 20348 verifyFormat("f(^{\n" 20349 " @autoreleasepool {\n" 20350 " if (a) {\n" 20351 " g();\n" 20352 " }\n" 20353 " }\n" 20354 "});", 20355 ZeroColumn); 20356 verifyFormat("void (^largeBlock)(void) = ^{\n" 20357 " // ...\n" 20358 "};", 20359 ZeroColumn); 20360 20361 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 20362 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 20363 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 20364 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 20365 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 20366 " int i;\n" 20367 "};", 20368 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 20369 } 20370 20371 TEST_F(FormatTest, SupportsCRLF) { 20372 EXPECT_EQ("int a;\r\n" 20373 "int b;\r\n" 20374 "int c;\r\n", 20375 format("int a;\r\n" 20376 " int b;\r\n" 20377 " int c;\r\n", 20378 getLLVMStyle())); 20379 EXPECT_EQ("int a;\r\n" 20380 "int b;\r\n" 20381 "int c;\r\n", 20382 format("int a;\r\n" 20383 " int b;\n" 20384 " int c;\r\n", 20385 getLLVMStyle())); 20386 EXPECT_EQ("int a;\n" 20387 "int b;\n" 20388 "int c;\n", 20389 format("int a;\r\n" 20390 " int b;\n" 20391 " int c;\n", 20392 getLLVMStyle())); 20393 EXPECT_EQ("\"aaaaaaa \"\r\n" 20394 "\"bbbbbbb\";\r\n", 20395 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 20396 EXPECT_EQ("#define A \\\r\n" 20397 " b; \\\r\n" 20398 " c; \\\r\n" 20399 " d;\r\n", 20400 format("#define A \\\r\n" 20401 " b; \\\r\n" 20402 " c; d; \r\n", 20403 getGoogleStyle())); 20404 20405 EXPECT_EQ("/*\r\n" 20406 "multi line block comments\r\n" 20407 "should not introduce\r\n" 20408 "an extra carriage return\r\n" 20409 "*/\r\n", 20410 format("/*\r\n" 20411 "multi line block comments\r\n" 20412 "should not introduce\r\n" 20413 "an extra carriage return\r\n" 20414 "*/\r\n")); 20415 EXPECT_EQ("/*\r\n" 20416 "\r\n" 20417 "*/", 20418 format("/*\r\n" 20419 " \r\r\r\n" 20420 "*/")); 20421 20422 FormatStyle style = getLLVMStyle(); 20423 20424 style.DeriveLineEnding = true; 20425 style.UseCRLF = false; 20426 EXPECT_EQ("union FooBarBazQux {\n" 20427 " int foo;\n" 20428 " int bar;\n" 20429 " int baz;\n" 20430 "};", 20431 format("union FooBarBazQux {\r\n" 20432 " int foo;\n" 20433 " int bar;\r\n" 20434 " int baz;\n" 20435 "};", 20436 style)); 20437 style.UseCRLF = true; 20438 EXPECT_EQ("union FooBarBazQux {\r\n" 20439 " int foo;\r\n" 20440 " int bar;\r\n" 20441 " int baz;\r\n" 20442 "};", 20443 format("union FooBarBazQux {\r\n" 20444 " int foo;\n" 20445 " int bar;\r\n" 20446 " int baz;\n" 20447 "};", 20448 style)); 20449 20450 style.DeriveLineEnding = false; 20451 style.UseCRLF = false; 20452 EXPECT_EQ("union FooBarBazQux {\n" 20453 " int foo;\n" 20454 " int bar;\n" 20455 " int baz;\n" 20456 " int qux;\n" 20457 "};", 20458 format("union FooBarBazQux {\r\n" 20459 " int foo;\n" 20460 " int bar;\r\n" 20461 " int baz;\n" 20462 " int qux;\r\n" 20463 "};", 20464 style)); 20465 style.UseCRLF = true; 20466 EXPECT_EQ("union FooBarBazQux {\r\n" 20467 " int foo;\r\n" 20468 " int bar;\r\n" 20469 " int baz;\r\n" 20470 " int qux;\r\n" 20471 "};", 20472 format("union FooBarBazQux {\r\n" 20473 " int foo;\n" 20474 " int bar;\r\n" 20475 " int baz;\n" 20476 " int qux;\n" 20477 "};", 20478 style)); 20479 20480 style.DeriveLineEnding = true; 20481 style.UseCRLF = false; 20482 EXPECT_EQ("union FooBarBazQux {\r\n" 20483 " int foo;\r\n" 20484 " int bar;\r\n" 20485 " int baz;\r\n" 20486 " int qux;\r\n" 20487 "};", 20488 format("union FooBarBazQux {\r\n" 20489 " int foo;\n" 20490 " int bar;\r\n" 20491 " int baz;\n" 20492 " int qux;\r\n" 20493 "};", 20494 style)); 20495 style.UseCRLF = true; 20496 EXPECT_EQ("union FooBarBazQux {\n" 20497 " int foo;\n" 20498 " int bar;\n" 20499 " int baz;\n" 20500 " int qux;\n" 20501 "};", 20502 format("union FooBarBazQux {\r\n" 20503 " int foo;\n" 20504 " int bar;\r\n" 20505 " int baz;\n" 20506 " int qux;\n" 20507 "};", 20508 style)); 20509 } 20510 20511 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 20512 verifyFormat("MY_CLASS(C) {\n" 20513 " int i;\n" 20514 " int j;\n" 20515 "};"); 20516 } 20517 20518 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 20519 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 20520 TwoIndent.ContinuationIndentWidth = 2; 20521 20522 EXPECT_EQ("int i =\n" 20523 " longFunction(\n" 20524 " arg);", 20525 format("int i = longFunction(arg);", TwoIndent)); 20526 20527 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 20528 SixIndent.ContinuationIndentWidth = 6; 20529 20530 EXPECT_EQ("int i =\n" 20531 " longFunction(\n" 20532 " arg);", 20533 format("int i = longFunction(arg);", SixIndent)); 20534 } 20535 20536 TEST_F(FormatTest, WrappedClosingParenthesisIndent) { 20537 FormatStyle Style = getLLVMStyle(); 20538 verifyFormat("int Foo::getter(\n" 20539 " //\n" 20540 ") const {\n" 20541 " return foo;\n" 20542 "}", 20543 Style); 20544 verifyFormat("void Foo::setter(\n" 20545 " //\n" 20546 ") {\n" 20547 " foo = 1;\n" 20548 "}", 20549 Style); 20550 } 20551 20552 TEST_F(FormatTest, SpacesInAngles) { 20553 FormatStyle Spaces = getLLVMStyle(); 20554 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 20555 20556 verifyFormat("vector< ::std::string > x1;", Spaces); 20557 verifyFormat("Foo< int, Bar > x2;", Spaces); 20558 verifyFormat("Foo< ::int, ::Bar > x3;", Spaces); 20559 20560 verifyFormat("static_cast< int >(arg);", Spaces); 20561 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 20562 verifyFormat("f< int, float >();", Spaces); 20563 verifyFormat("template <> g() {}", Spaces); 20564 verifyFormat("template < std::vector< int > > f() {}", Spaces); 20565 verifyFormat("std::function< void(int, int) > fct;", Spaces); 20566 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 20567 Spaces); 20568 20569 Spaces.Standard = FormatStyle::LS_Cpp03; 20570 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 20571 verifyFormat("A< A< int > >();", Spaces); 20572 20573 Spaces.SpacesInAngles = FormatStyle::SIAS_Never; 20574 verifyFormat("A<A<int> >();", Spaces); 20575 20576 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave; 20577 verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;", 20578 Spaces); 20579 verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;", 20580 Spaces); 20581 20582 verifyFormat("A<A<int> >();", Spaces); 20583 verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces); 20584 verifyFormat("A< A< int > >();", Spaces); 20585 20586 Spaces.Standard = FormatStyle::LS_Cpp11; 20587 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 20588 verifyFormat("A< A< int > >();", Spaces); 20589 20590 Spaces.SpacesInAngles = FormatStyle::SIAS_Never; 20591 verifyFormat("vector<::std::string> x4;", Spaces); 20592 verifyFormat("vector<int> x5;", Spaces); 20593 verifyFormat("Foo<int, Bar> x6;", Spaces); 20594 verifyFormat("Foo<::int, ::Bar> x7;", Spaces); 20595 20596 verifyFormat("A<A<int>>();", Spaces); 20597 20598 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave; 20599 verifyFormat("vector<::std::string> x4;", Spaces); 20600 verifyFormat("vector< ::std::string > x4;", Spaces); 20601 verifyFormat("vector<int> x5;", Spaces); 20602 verifyFormat("vector< int > x5;", Spaces); 20603 verifyFormat("Foo<int, Bar> x6;", Spaces); 20604 verifyFormat("Foo< int, Bar > x6;", Spaces); 20605 verifyFormat("Foo<::int, ::Bar> x7;", Spaces); 20606 verifyFormat("Foo< ::int, ::Bar > x7;", Spaces); 20607 20608 verifyFormat("A<A<int>>();", Spaces); 20609 verifyFormat("A< A< int > >();", Spaces); 20610 verifyFormat("A<A<int > >();", Spaces); 20611 verifyFormat("A< A< int>>();", Spaces); 20612 } 20613 20614 TEST_F(FormatTest, SpaceAfterTemplateKeyword) { 20615 FormatStyle Style = getLLVMStyle(); 20616 Style.SpaceAfterTemplateKeyword = false; 20617 verifyFormat("template<int> void foo();", Style); 20618 } 20619 20620 TEST_F(FormatTest, TripleAngleBrackets) { 20621 verifyFormat("f<<<1, 1>>>();"); 20622 verifyFormat("f<<<1, 1, 1, s>>>();"); 20623 verifyFormat("f<<<a, b, c, d>>>();"); 20624 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 20625 verifyFormat("f<param><<<1, 1>>>();"); 20626 verifyFormat("f<1><<<1, 1>>>();"); 20627 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 20628 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 20629 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 20630 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n" 20631 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();"); 20632 } 20633 20634 TEST_F(FormatTest, MergeLessLessAtEnd) { 20635 verifyFormat("<<"); 20636 EXPECT_EQ("< < <", format("\\\n<<<")); 20637 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 20638 "aaallvm::outs() <<"); 20639 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 20640 "aaaallvm::outs()\n <<"); 20641 } 20642 20643 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 20644 std::string code = "#if A\n" 20645 "#if B\n" 20646 "a.\n" 20647 "#endif\n" 20648 " a = 1;\n" 20649 "#else\n" 20650 "#endif\n" 20651 "#if C\n" 20652 "#else\n" 20653 "#endif\n"; 20654 EXPECT_EQ(code, format(code)); 20655 } 20656 20657 TEST_F(FormatTest, HandleConflictMarkers) { 20658 // Git/SVN conflict markers. 20659 EXPECT_EQ("int a;\n" 20660 "void f() {\n" 20661 " callme(some(parameter1,\n" 20662 "<<<<<<< text by the vcs\n" 20663 " parameter2),\n" 20664 "||||||| text by the vcs\n" 20665 " parameter2),\n" 20666 " parameter3,\n" 20667 "======= text by the vcs\n" 20668 " parameter2, parameter3),\n" 20669 ">>>>>>> text by the vcs\n" 20670 " otherparameter);\n", 20671 format("int a;\n" 20672 "void f() {\n" 20673 " callme(some(parameter1,\n" 20674 "<<<<<<< text by the vcs\n" 20675 " parameter2),\n" 20676 "||||||| text by the vcs\n" 20677 " parameter2),\n" 20678 " parameter3,\n" 20679 "======= text by the vcs\n" 20680 " parameter2,\n" 20681 " parameter3),\n" 20682 ">>>>>>> text by the vcs\n" 20683 " otherparameter);\n")); 20684 20685 // Perforce markers. 20686 EXPECT_EQ("void f() {\n" 20687 " function(\n" 20688 ">>>> text by the vcs\n" 20689 " parameter,\n" 20690 "==== text by the vcs\n" 20691 " parameter,\n" 20692 "==== text by the vcs\n" 20693 " parameter,\n" 20694 "<<<< text by the vcs\n" 20695 " parameter);\n", 20696 format("void f() {\n" 20697 " function(\n" 20698 ">>>> text by the vcs\n" 20699 " parameter,\n" 20700 "==== text by the vcs\n" 20701 " parameter,\n" 20702 "==== text by the vcs\n" 20703 " parameter,\n" 20704 "<<<< text by the vcs\n" 20705 " parameter);\n")); 20706 20707 EXPECT_EQ("<<<<<<<\n" 20708 "|||||||\n" 20709 "=======\n" 20710 ">>>>>>>", 20711 format("<<<<<<<\n" 20712 "|||||||\n" 20713 "=======\n" 20714 ">>>>>>>")); 20715 20716 EXPECT_EQ("<<<<<<<\n" 20717 "|||||||\n" 20718 "int i;\n" 20719 "=======\n" 20720 ">>>>>>>", 20721 format("<<<<<<<\n" 20722 "|||||||\n" 20723 "int i;\n" 20724 "=======\n" 20725 ">>>>>>>")); 20726 20727 // FIXME: Handle parsing of macros around conflict markers correctly: 20728 EXPECT_EQ("#define Macro \\\n" 20729 "<<<<<<<\n" 20730 "Something \\\n" 20731 "|||||||\n" 20732 "Else \\\n" 20733 "=======\n" 20734 "Other \\\n" 20735 ">>>>>>>\n" 20736 " End int i;\n", 20737 format("#define Macro \\\n" 20738 "<<<<<<<\n" 20739 " Something \\\n" 20740 "|||||||\n" 20741 " Else \\\n" 20742 "=======\n" 20743 " Other \\\n" 20744 ">>>>>>>\n" 20745 " End\n" 20746 "int i;\n")); 20747 } 20748 20749 TEST_F(FormatTest, DisableRegions) { 20750 EXPECT_EQ("int i;\n" 20751 "// clang-format off\n" 20752 " int j;\n" 20753 "// clang-format on\n" 20754 "int k;", 20755 format(" int i;\n" 20756 " // clang-format off\n" 20757 " int j;\n" 20758 " // clang-format on\n" 20759 " int k;")); 20760 EXPECT_EQ("int i;\n" 20761 "/* clang-format off */\n" 20762 " int j;\n" 20763 "/* clang-format on */\n" 20764 "int k;", 20765 format(" int i;\n" 20766 " /* clang-format off */\n" 20767 " int j;\n" 20768 " /* clang-format on */\n" 20769 " int k;")); 20770 20771 // Don't reflow comments within disabled regions. 20772 EXPECT_EQ("// clang-format off\n" 20773 "// long long long long long long line\n" 20774 "/* clang-format on */\n" 20775 "/* long long long\n" 20776 " * long long long\n" 20777 " * line */\n" 20778 "int i;\n" 20779 "/* clang-format off */\n" 20780 "/* long long long long long long line */\n", 20781 format("// clang-format off\n" 20782 "// long long long long long long line\n" 20783 "/* clang-format on */\n" 20784 "/* long long long long long long line */\n" 20785 "int i;\n" 20786 "/* clang-format off */\n" 20787 "/* long long long long long long line */\n", 20788 getLLVMStyleWithColumns(20))); 20789 } 20790 20791 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 20792 format("? ) ="); 20793 verifyNoCrash("#define a\\\n /**/}"); 20794 } 20795 20796 TEST_F(FormatTest, FormatsTableGenCode) { 20797 FormatStyle Style = getLLVMStyle(); 20798 Style.Language = FormatStyle::LK_TableGen; 20799 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 20800 } 20801 20802 TEST_F(FormatTest, ArrayOfTemplates) { 20803 EXPECT_EQ("auto a = new unique_ptr<int>[10];", 20804 format("auto a = new unique_ptr<int > [ 10];")); 20805 20806 FormatStyle Spaces = getLLVMStyle(); 20807 Spaces.SpacesInSquareBrackets = true; 20808 EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];", 20809 format("auto a = new unique_ptr<int > [10];", Spaces)); 20810 } 20811 20812 TEST_F(FormatTest, ArrayAsTemplateType) { 20813 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;", 20814 format("auto a = unique_ptr < Foo < Bar>[ 10]> ;")); 20815 20816 FormatStyle Spaces = getLLVMStyle(); 20817 Spaces.SpacesInSquareBrackets = true; 20818 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;", 20819 format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces)); 20820 } 20821 20822 TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); } 20823 20824 TEST(FormatStyle, GetStyleWithEmptyFileName) { 20825 llvm::vfs::InMemoryFileSystem FS; 20826 auto Style1 = getStyle("file", "", "Google", "", &FS); 20827 ASSERT_TRUE((bool)Style1); 20828 ASSERT_EQ(*Style1, getGoogleStyle()); 20829 } 20830 20831 TEST(FormatStyle, GetStyleOfFile) { 20832 llvm::vfs::InMemoryFileSystem FS; 20833 // Test 1: format file in the same directory. 20834 ASSERT_TRUE( 20835 FS.addFile("/a/.clang-format", 0, 20836 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 20837 ASSERT_TRUE( 20838 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 20839 auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS); 20840 ASSERT_TRUE((bool)Style1); 20841 ASSERT_EQ(*Style1, getLLVMStyle()); 20842 20843 // Test 2.1: fallback to default. 20844 ASSERT_TRUE( 20845 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 20846 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS); 20847 ASSERT_TRUE((bool)Style2); 20848 ASSERT_EQ(*Style2, getMozillaStyle()); 20849 20850 // Test 2.2: no format on 'none' fallback style. 20851 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 20852 ASSERT_TRUE((bool)Style2); 20853 ASSERT_EQ(*Style2, getNoStyle()); 20854 20855 // Test 2.3: format if config is found with no based style while fallback is 20856 // 'none'. 20857 ASSERT_TRUE(FS.addFile("/b/.clang-format", 0, 20858 llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2"))); 20859 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 20860 ASSERT_TRUE((bool)Style2); 20861 ASSERT_EQ(*Style2, getLLVMStyle()); 20862 20863 // Test 2.4: format if yaml with no based style, while fallback is 'none'. 20864 Style2 = getStyle("{}", "a.h", "none", "", &FS); 20865 ASSERT_TRUE((bool)Style2); 20866 ASSERT_EQ(*Style2, getLLVMStyle()); 20867 20868 // Test 3: format file in parent directory. 20869 ASSERT_TRUE( 20870 FS.addFile("/c/.clang-format", 0, 20871 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 20872 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, 20873 llvm::MemoryBuffer::getMemBuffer("int i;"))); 20874 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS); 20875 ASSERT_TRUE((bool)Style3); 20876 ASSERT_EQ(*Style3, getGoogleStyle()); 20877 20878 // Test 4: error on invalid fallback style 20879 auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS); 20880 ASSERT_FALSE((bool)Style4); 20881 llvm::consumeError(Style4.takeError()); 20882 20883 // Test 5: error on invalid yaml on command line 20884 auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS); 20885 ASSERT_FALSE((bool)Style5); 20886 llvm::consumeError(Style5.takeError()); 20887 20888 // Test 6: error on invalid style 20889 auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS); 20890 ASSERT_FALSE((bool)Style6); 20891 llvm::consumeError(Style6.takeError()); 20892 20893 // Test 7: found config file, error on parsing it 20894 ASSERT_TRUE( 20895 FS.addFile("/d/.clang-format", 0, 20896 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n" 20897 "InvalidKey: InvalidValue"))); 20898 ASSERT_TRUE( 20899 FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 20900 auto Style7a = getStyle("file", "/d/.clang-format", "LLVM", "", &FS); 20901 ASSERT_FALSE((bool)Style7a); 20902 llvm::consumeError(Style7a.takeError()); 20903 20904 auto Style7b = getStyle("file", "/d/.clang-format", "LLVM", "", &FS, true); 20905 ASSERT_TRUE((bool)Style7b); 20906 20907 // Test 8: inferred per-language defaults apply. 20908 auto StyleTd = getStyle("file", "x.td", "llvm", "", &FS); 20909 ASSERT_TRUE((bool)StyleTd); 20910 ASSERT_EQ(*StyleTd, getLLVMStyle(FormatStyle::LK_TableGen)); 20911 20912 // Test 9.1: overwriting a file style, when parent no file exists with no 20913 // fallback style 20914 ASSERT_TRUE(FS.addFile( 20915 "/e/sub/.clang-format", 0, 20916 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: InheritParentConfig\n" 20917 "ColumnLimit: 20"))); 20918 ASSERT_TRUE(FS.addFile("/e/sub/code.cpp", 0, 20919 llvm::MemoryBuffer::getMemBuffer("int i;"))); 20920 auto Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS); 20921 ASSERT_TRUE(static_cast<bool>(Style9)); 20922 ASSERT_EQ(*Style9, [] { 20923 auto Style = getNoStyle(); 20924 Style.ColumnLimit = 20; 20925 return Style; 20926 }()); 20927 20928 // Test 9.2: with LLVM fallback style 20929 Style9 = getStyle("file", "/e/sub/code.cpp", "LLVM", "", &FS); 20930 ASSERT_TRUE(static_cast<bool>(Style9)); 20931 ASSERT_EQ(*Style9, [] { 20932 auto Style = getLLVMStyle(); 20933 Style.ColumnLimit = 20; 20934 return Style; 20935 }()); 20936 20937 // Test 9.3: with a parent file 20938 ASSERT_TRUE( 20939 FS.addFile("/e/.clang-format", 0, 20940 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google\n" 20941 "UseTab: Always"))); 20942 Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS); 20943 ASSERT_TRUE(static_cast<bool>(Style9)); 20944 ASSERT_EQ(*Style9, [] { 20945 auto Style = getGoogleStyle(); 20946 Style.ColumnLimit = 20; 20947 Style.UseTab = FormatStyle::UT_Always; 20948 return Style; 20949 }()); 20950 20951 // Test 9.4: propagate more than one level 20952 ASSERT_TRUE(FS.addFile("/e/sub/sub/code.cpp", 0, 20953 llvm::MemoryBuffer::getMemBuffer("int i;"))); 20954 ASSERT_TRUE(FS.addFile("/e/sub/sub/.clang-format", 0, 20955 llvm::MemoryBuffer::getMemBuffer( 20956 "BasedOnStyle: InheritParentConfig\n" 20957 "WhitespaceSensitiveMacros: ['FOO', 'BAR']"))); 20958 std::vector<std::string> NonDefaultWhiteSpaceMacros{"FOO", "BAR"}; 20959 20960 const auto SubSubStyle = [&NonDefaultWhiteSpaceMacros] { 20961 auto Style = getGoogleStyle(); 20962 Style.ColumnLimit = 20; 20963 Style.UseTab = FormatStyle::UT_Always; 20964 Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros; 20965 return Style; 20966 }(); 20967 20968 ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros); 20969 Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS); 20970 ASSERT_TRUE(static_cast<bool>(Style9)); 20971 ASSERT_EQ(*Style9, SubSubStyle); 20972 20973 // Test 9.5: use InheritParentConfig as style name 20974 Style9 = 20975 getStyle("inheritparentconfig", "/e/sub/sub/code.cpp", "none", "", &FS); 20976 ASSERT_TRUE(static_cast<bool>(Style9)); 20977 ASSERT_EQ(*Style9, SubSubStyle); 20978 20979 // Test 9.6: use command line style with inheritance 20980 Style9 = getStyle("{BasedOnStyle: InheritParentConfig}", "/e/sub/code.cpp", 20981 "none", "", &FS); 20982 ASSERT_TRUE(static_cast<bool>(Style9)); 20983 ASSERT_EQ(*Style9, SubSubStyle); 20984 20985 // Test 9.7: use command line style with inheritance and own config 20986 Style9 = getStyle("{BasedOnStyle: InheritParentConfig, " 20987 "WhitespaceSensitiveMacros: ['FOO', 'BAR']}", 20988 "/e/sub/code.cpp", "none", "", &FS); 20989 ASSERT_TRUE(static_cast<bool>(Style9)); 20990 ASSERT_EQ(*Style9, SubSubStyle); 20991 20992 // Test 9.8: use inheritance from a file without BasedOnStyle 20993 ASSERT_TRUE(FS.addFile("/e/withoutbase/.clang-format", 0, 20994 llvm::MemoryBuffer::getMemBuffer("ColumnLimit: 123"))); 20995 ASSERT_TRUE( 20996 FS.addFile("/e/withoutbase/sub/.clang-format", 0, 20997 llvm::MemoryBuffer::getMemBuffer( 20998 "BasedOnStyle: InheritParentConfig\nIndentWidth: 7"))); 20999 // Make sure we do not use the fallback style 21000 Style9 = getStyle("file", "/e/withoutbase/code.cpp", "google", "", &FS); 21001 ASSERT_TRUE(static_cast<bool>(Style9)); 21002 ASSERT_EQ(*Style9, [] { 21003 auto Style = getLLVMStyle(); 21004 Style.ColumnLimit = 123; 21005 return Style; 21006 }()); 21007 21008 Style9 = getStyle("file", "/e/withoutbase/sub/code.cpp", "google", "", &FS); 21009 ASSERT_TRUE(static_cast<bool>(Style9)); 21010 ASSERT_EQ(*Style9, [] { 21011 auto Style = getLLVMStyle(); 21012 Style.ColumnLimit = 123; 21013 Style.IndentWidth = 7; 21014 return Style; 21015 }()); 21016 } 21017 21018 TEST_F(ReplacementTest, FormatCodeAfterReplacements) { 21019 // Column limit is 20. 21020 std::string Code = "Type *a =\n" 21021 " new Type();\n" 21022 "g(iiiii, 0, jjjjj,\n" 21023 " 0, kkkkk, 0, mm);\n" 21024 "int bad = format ;"; 21025 std::string Expected = "auto a = new Type();\n" 21026 "g(iiiii, nullptr,\n" 21027 " jjjjj, nullptr,\n" 21028 " kkkkk, nullptr,\n" 21029 " mm);\n" 21030 "int bad = format ;"; 21031 FileID ID = Context.createInMemoryFile("format.cpp", Code); 21032 tooling::Replacements Replaces = toReplacements( 21033 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6, 21034 "auto "), 21035 tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1, 21036 "nullptr"), 21037 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1, 21038 "nullptr"), 21039 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1, 21040 "nullptr")}); 21041 21042 format::FormatStyle Style = format::getLLVMStyle(); 21043 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. 21044 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 21045 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 21046 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 21047 auto Result = applyAllReplacements(Code, *FormattedReplaces); 21048 EXPECT_TRUE(static_cast<bool>(Result)); 21049 EXPECT_EQ(Expected, *Result); 21050 } 21051 21052 TEST_F(ReplacementTest, SortIncludesAfterReplacement) { 21053 std::string Code = "#include \"a.h\"\n" 21054 "#include \"c.h\"\n" 21055 "\n" 21056 "int main() {\n" 21057 " return 0;\n" 21058 "}"; 21059 std::string Expected = "#include \"a.h\"\n" 21060 "#include \"b.h\"\n" 21061 "#include \"c.h\"\n" 21062 "\n" 21063 "int main() {\n" 21064 " return 0;\n" 21065 "}"; 21066 FileID ID = Context.createInMemoryFile("fix.cpp", Code); 21067 tooling::Replacements Replaces = toReplacements( 21068 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0, 21069 "#include \"b.h\"\n")}); 21070 21071 format::FormatStyle Style = format::getLLVMStyle(); 21072 Style.SortIncludes = FormatStyle::SI_CaseSensitive; 21073 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 21074 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 21075 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 21076 auto Result = applyAllReplacements(Code, *FormattedReplaces); 21077 EXPECT_TRUE(static_cast<bool>(Result)); 21078 EXPECT_EQ(Expected, *Result); 21079 } 21080 21081 TEST_F(FormatTest, FormatSortsUsingDeclarations) { 21082 EXPECT_EQ("using std::cin;\n" 21083 "using std::cout;", 21084 format("using std::cout;\n" 21085 "using std::cin;", 21086 getGoogleStyle())); 21087 } 21088 21089 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) { 21090 format::FormatStyle Style = format::getLLVMStyle(); 21091 Style.Standard = FormatStyle::LS_Cpp03; 21092 // cpp03 recognize this string as identifier u8 and literal character 'a' 21093 EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style)); 21094 } 21095 21096 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) { 21097 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers 21098 // all modes, including C++11, C++14 and C++17 21099 EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';")); 21100 } 21101 21102 TEST_F(FormatTest, DoNotFormatLikelyXml) { 21103 EXPECT_EQ("<!-- ;> -->", format("<!-- ;> -->", getGoogleStyle())); 21104 EXPECT_EQ(" <!-- >; -->", format(" <!-- >; -->", getGoogleStyle())); 21105 } 21106 21107 TEST_F(FormatTest, StructuredBindings) { 21108 // Structured bindings is a C++17 feature. 21109 // all modes, including C++11, C++14 and C++17 21110 verifyFormat("auto [a, b] = f();"); 21111 EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();")); 21112 EXPECT_EQ("const auto [a, b] = f();", format("const auto[a, b] = f();")); 21113 EXPECT_EQ("auto const [a, b] = f();", format("auto const[a, b] = f();")); 21114 EXPECT_EQ("auto const volatile [a, b] = f();", 21115 format("auto const volatile[a, b] = f();")); 21116 EXPECT_EQ("auto [a, b, c] = f();", format("auto [ a , b,c ] = f();")); 21117 EXPECT_EQ("auto &[a, b, c] = f();", 21118 format("auto &[ a , b,c ] = f();")); 21119 EXPECT_EQ("auto &&[a, b, c] = f();", 21120 format("auto &&[ a , b,c ] = f();")); 21121 EXPECT_EQ("auto const &[a, b] = f();", format("auto const&[a, b] = f();")); 21122 EXPECT_EQ("auto const volatile &&[a, b] = f();", 21123 format("auto const volatile &&[a, b] = f();")); 21124 EXPECT_EQ("auto const &&[a, b] = f();", 21125 format("auto const && [a, b] = f();")); 21126 EXPECT_EQ("const auto &[a, b] = f();", 21127 format("const auto & [a, b] = f();")); 21128 EXPECT_EQ("const auto volatile &&[a, b] = f();", 21129 format("const auto volatile &&[a, b] = f();")); 21130 EXPECT_EQ("volatile const auto &&[a, b] = f();", 21131 format("volatile const auto &&[a, b] = f();")); 21132 EXPECT_EQ("const auto &&[a, b] = f();", 21133 format("const auto && [a, b] = f();")); 21134 21135 // Make sure we don't mistake structured bindings for lambdas. 21136 FormatStyle PointerMiddle = getLLVMStyle(); 21137 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 21138 verifyFormat("auto [a1, b]{A * i};", getGoogleStyle()); 21139 verifyFormat("auto [a2, b]{A * i};", getLLVMStyle()); 21140 verifyFormat("auto [a3, b]{A * i};", PointerMiddle); 21141 verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle()); 21142 verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle()); 21143 verifyFormat("auto const [a3, b]{A * i};", PointerMiddle); 21144 verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle()); 21145 verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle()); 21146 verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle); 21147 verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle()); 21148 verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle()); 21149 verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle); 21150 21151 EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}", 21152 format("for (const auto && [a, b] : some_range) {\n}")); 21153 EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}", 21154 format("for (const auto & [a, b] : some_range) {\n}")); 21155 EXPECT_EQ("for (const auto [a, b] : some_range) {\n}", 21156 format("for (const auto[a, b] : some_range) {\n}")); 21157 EXPECT_EQ("auto [x, y](expr);", format("auto[x,y] (expr);")); 21158 EXPECT_EQ("auto &[x, y](expr);", format("auto & [x,y] (expr);")); 21159 EXPECT_EQ("auto &&[x, y](expr);", format("auto && [x,y] (expr);")); 21160 EXPECT_EQ("auto const &[x, y](expr);", 21161 format("auto const & [x,y] (expr);")); 21162 EXPECT_EQ("auto const &&[x, y](expr);", 21163 format("auto const && [x,y] (expr);")); 21164 EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y] {expr};")); 21165 EXPECT_EQ("auto const &[x, y]{expr};", 21166 format("auto const & [x,y] {expr};")); 21167 EXPECT_EQ("auto const &&[x, y]{expr};", 21168 format("auto const && [x,y] {expr};")); 21169 21170 format::FormatStyle Spaces = format::getLLVMStyle(); 21171 Spaces.SpacesInSquareBrackets = true; 21172 verifyFormat("auto [ a, b ] = f();", Spaces); 21173 verifyFormat("auto &&[ a, b ] = f();", Spaces); 21174 verifyFormat("auto &[ a, b ] = f();", Spaces); 21175 verifyFormat("auto const &&[ a, b ] = f();", Spaces); 21176 verifyFormat("auto const &[ a, b ] = f();", Spaces); 21177 } 21178 21179 TEST_F(FormatTest, FileAndCode) { 21180 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", "")); 21181 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", "")); 21182 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", "")); 21183 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "")); 21184 EXPECT_EQ(FormatStyle::LK_ObjC, 21185 guessLanguage("foo.h", "@interface Foo\n@end\n")); 21186 EXPECT_EQ( 21187 FormatStyle::LK_ObjC, 21188 guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }")); 21189 EXPECT_EQ(FormatStyle::LK_ObjC, 21190 guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))")); 21191 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;")); 21192 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", "")); 21193 EXPECT_EQ(FormatStyle::LK_ObjC, 21194 guessLanguage("foo", "@interface Foo\n@end\n")); 21195 EXPECT_EQ(FormatStyle::LK_ObjC, 21196 guessLanguage("foo.h", "int DoStuff(CGRect rect);\n")); 21197 EXPECT_EQ( 21198 FormatStyle::LK_ObjC, 21199 guessLanguage("foo.h", 21200 "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n")); 21201 EXPECT_EQ( 21202 FormatStyle::LK_Cpp, 21203 guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;")); 21204 } 21205 21206 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) { 21207 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];")); 21208 EXPECT_EQ(FormatStyle::LK_ObjC, 21209 guessLanguage("foo.h", "array[[calculator getIndex]];")); 21210 EXPECT_EQ(FormatStyle::LK_Cpp, 21211 guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];")); 21212 EXPECT_EQ( 21213 FormatStyle::LK_Cpp, 21214 guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];")); 21215 EXPECT_EQ(FormatStyle::LK_ObjC, 21216 guessLanguage("foo.h", "[[noreturn foo] bar];")); 21217 EXPECT_EQ(FormatStyle::LK_Cpp, 21218 guessLanguage("foo.h", "[[clang::fallthrough]];")); 21219 EXPECT_EQ(FormatStyle::LK_ObjC, 21220 guessLanguage("foo.h", "[[clang:fallthrough] foo];")); 21221 EXPECT_EQ(FormatStyle::LK_Cpp, 21222 guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];")); 21223 EXPECT_EQ(FormatStyle::LK_Cpp, 21224 guessLanguage("foo.h", "[[using clang: fallthrough]];")); 21225 EXPECT_EQ(FormatStyle::LK_ObjC, 21226 guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];")); 21227 EXPECT_EQ(FormatStyle::LK_Cpp, 21228 guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];")); 21229 EXPECT_EQ( 21230 FormatStyle::LK_Cpp, 21231 guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)")); 21232 EXPECT_EQ( 21233 FormatStyle::LK_Cpp, 21234 guessLanguage("foo.h", 21235 "[[clang::callable_when(\"unconsumed\", \"unknown\")]]")); 21236 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]")); 21237 } 21238 21239 TEST_F(FormatTest, GuessLanguageWithCaret) { 21240 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);")); 21241 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);")); 21242 EXPECT_EQ(FormatStyle::LK_ObjC, 21243 guessLanguage("foo.h", "int(^)(char, float);")); 21244 EXPECT_EQ(FormatStyle::LK_ObjC, 21245 guessLanguage("foo.h", "int(^foo)(char, float);")); 21246 EXPECT_EQ(FormatStyle::LK_ObjC, 21247 guessLanguage("foo.h", "int(^foo[10])(char, float);")); 21248 EXPECT_EQ(FormatStyle::LK_ObjC, 21249 guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);")); 21250 EXPECT_EQ( 21251 FormatStyle::LK_ObjC, 21252 guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);")); 21253 } 21254 21255 TEST_F(FormatTest, GuessLanguageWithPragmas) { 21256 EXPECT_EQ(FormatStyle::LK_Cpp, 21257 guessLanguage("foo.h", "__pragma(warning(disable:))")); 21258 EXPECT_EQ(FormatStyle::LK_Cpp, 21259 guessLanguage("foo.h", "#pragma(warning(disable:))")); 21260 EXPECT_EQ(FormatStyle::LK_Cpp, 21261 guessLanguage("foo.h", "_Pragma(warning(disable:))")); 21262 } 21263 21264 TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) { 21265 // ASM symbolic names are identifiers that must be surrounded by [] without 21266 // space in between: 21267 // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands 21268 21269 // Example from https://bugs.llvm.org/show_bug.cgi?id=45108. 21270 verifyFormat(R"(// 21271 asm volatile("mrs %x[result], FPCR" : [result] "=r"(result)); 21272 )"); 21273 21274 // A list of several ASM symbolic names. 21275 verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)"); 21276 21277 // ASM symbolic names in inline ASM with inputs and outputs. 21278 verifyFormat(R"(// 21279 asm("cmoveq %1, %2, %[result]" 21280 : [result] "=r"(result) 21281 : "r"(test), "r"(new), "[result]"(old)); 21282 )"); 21283 21284 // ASM symbolic names in inline ASM with no outputs. 21285 verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)"); 21286 } 21287 21288 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) { 21289 EXPECT_EQ(FormatStyle::LK_Cpp, 21290 guessLanguage("foo.h", "void f() {\n" 21291 " asm (\"mov %[e], %[d]\"\n" 21292 " : [d] \"=rm\" (d)\n" 21293 " [e] \"rm\" (*e));\n" 21294 "}")); 21295 EXPECT_EQ(FormatStyle::LK_Cpp, 21296 guessLanguage("foo.h", "void f() {\n" 21297 " _asm (\"mov %[e], %[d]\"\n" 21298 " : [d] \"=rm\" (d)\n" 21299 " [e] \"rm\" (*e));\n" 21300 "}")); 21301 EXPECT_EQ(FormatStyle::LK_Cpp, 21302 guessLanguage("foo.h", "void f() {\n" 21303 " __asm (\"mov %[e], %[d]\"\n" 21304 " : [d] \"=rm\" (d)\n" 21305 " [e] \"rm\" (*e));\n" 21306 "}")); 21307 EXPECT_EQ(FormatStyle::LK_Cpp, 21308 guessLanguage("foo.h", "void f() {\n" 21309 " __asm__ (\"mov %[e], %[d]\"\n" 21310 " : [d] \"=rm\" (d)\n" 21311 " [e] \"rm\" (*e));\n" 21312 "}")); 21313 EXPECT_EQ(FormatStyle::LK_Cpp, 21314 guessLanguage("foo.h", "void f() {\n" 21315 " asm (\"mov %[e], %[d]\"\n" 21316 " : [d] \"=rm\" (d),\n" 21317 " [e] \"rm\" (*e));\n" 21318 "}")); 21319 EXPECT_EQ(FormatStyle::LK_Cpp, 21320 guessLanguage("foo.h", "void f() {\n" 21321 " asm volatile (\"mov %[e], %[d]\"\n" 21322 " : [d] \"=rm\" (d)\n" 21323 " [e] \"rm\" (*e));\n" 21324 "}")); 21325 } 21326 21327 TEST_F(FormatTest, GuessLanguageWithChildLines) { 21328 EXPECT_EQ(FormatStyle::LK_Cpp, 21329 guessLanguage("foo.h", "#define FOO ({ std::string s; })")); 21330 EXPECT_EQ(FormatStyle::LK_ObjC, 21331 guessLanguage("foo.h", "#define FOO ({ NSString *s; })")); 21332 EXPECT_EQ( 21333 FormatStyle::LK_Cpp, 21334 guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })")); 21335 EXPECT_EQ( 21336 FormatStyle::LK_ObjC, 21337 guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })")); 21338 } 21339 21340 TEST_F(FormatTest, TypenameMacros) { 21341 std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"}; 21342 21343 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353 21344 FormatStyle Google = getGoogleStyleWithColumns(0); 21345 Google.TypenameMacros = TypenameMacros; 21346 verifyFormat("struct foo {\n" 21347 " int bar;\n" 21348 " TAILQ_ENTRY(a) bleh;\n" 21349 "};", 21350 Google); 21351 21352 FormatStyle Macros = getLLVMStyle(); 21353 Macros.TypenameMacros = TypenameMacros; 21354 21355 verifyFormat("STACK_OF(int) a;", Macros); 21356 verifyFormat("STACK_OF(int) *a;", Macros); 21357 verifyFormat("STACK_OF(int const *) *a;", Macros); 21358 verifyFormat("STACK_OF(int *const) *a;", Macros); 21359 verifyFormat("STACK_OF(int, string) a;", Macros); 21360 verifyFormat("STACK_OF(LIST(int)) a;", Macros); 21361 verifyFormat("STACK_OF(LIST(int)) a, b;", Macros); 21362 verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros); 21363 verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros); 21364 verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros); 21365 verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros); 21366 21367 Macros.PointerAlignment = FormatStyle::PAS_Left; 21368 verifyFormat("STACK_OF(int)* a;", Macros); 21369 verifyFormat("STACK_OF(int*)* a;", Macros); 21370 verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros); 21371 verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros); 21372 verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros); 21373 } 21374 21375 TEST_F(FormatTest, AtomicQualifier) { 21376 // Check that we treate _Atomic as a type and not a function call 21377 FormatStyle Google = getGoogleStyleWithColumns(0); 21378 verifyFormat("struct foo {\n" 21379 " int a1;\n" 21380 " _Atomic(a) a2;\n" 21381 " _Atomic(_Atomic(int) *const) a3;\n" 21382 "};", 21383 Google); 21384 verifyFormat("_Atomic(uint64_t) a;"); 21385 verifyFormat("_Atomic(uint64_t) *a;"); 21386 verifyFormat("_Atomic(uint64_t const *) *a;"); 21387 verifyFormat("_Atomic(uint64_t *const) *a;"); 21388 verifyFormat("_Atomic(const uint64_t *) *a;"); 21389 verifyFormat("_Atomic(uint64_t) a;"); 21390 verifyFormat("_Atomic(_Atomic(uint64_t)) a;"); 21391 verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;"); 21392 verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}"); 21393 verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);"); 21394 21395 verifyFormat("_Atomic(uint64_t) *s(InitValue);"); 21396 verifyFormat("_Atomic(uint64_t) *s{InitValue};"); 21397 FormatStyle Style = getLLVMStyle(); 21398 Style.PointerAlignment = FormatStyle::PAS_Left; 21399 verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style); 21400 verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style); 21401 verifyFormat("_Atomic(int)* a;", Style); 21402 verifyFormat("_Atomic(int*)* a;", Style); 21403 verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style); 21404 21405 Style.SpacesInCStyleCastParentheses = true; 21406 Style.SpacesInParentheses = false; 21407 verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style); 21408 Style.SpacesInCStyleCastParentheses = false; 21409 Style.SpacesInParentheses = true; 21410 verifyFormat("x = (_Atomic( uint64_t ))*a;", Style); 21411 verifyFormat("x = (_Atomic( uint64_t ))&a;", Style); 21412 } 21413 21414 TEST_F(FormatTest, AmbersandInLamda) { 21415 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899 21416 FormatStyle AlignStyle = getLLVMStyle(); 21417 AlignStyle.PointerAlignment = FormatStyle::PAS_Left; 21418 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle); 21419 AlignStyle.PointerAlignment = FormatStyle::PAS_Right; 21420 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle); 21421 } 21422 21423 TEST_F(FormatTest, SpacesInConditionalStatement) { 21424 FormatStyle Spaces = getLLVMStyle(); 21425 Spaces.IfMacros.clear(); 21426 Spaces.IfMacros.push_back("MYIF"); 21427 Spaces.SpacesInConditionalStatement = true; 21428 verifyFormat("for ( int i = 0; i; i++ )\n continue;", Spaces); 21429 verifyFormat("if ( !a )\n return;", Spaces); 21430 verifyFormat("if ( a )\n return;", Spaces); 21431 verifyFormat("if constexpr ( a )\n return;", Spaces); 21432 verifyFormat("MYIF ( a )\n return;", Spaces); 21433 verifyFormat("MYIF ( a )\n return;\nelse MYIF ( b )\n return;", Spaces); 21434 verifyFormat("MYIF ( a )\n return;\nelse\n return;", Spaces); 21435 verifyFormat("switch ( a )\ncase 1:\n return;", Spaces); 21436 verifyFormat("while ( a )\n return;", Spaces); 21437 verifyFormat("while ( (a && b) )\n return;", Spaces); 21438 verifyFormat("do {\n} while ( 1 != 0 );", Spaces); 21439 verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces); 21440 // Check that space on the left of "::" is inserted as expected at beginning 21441 // of condition. 21442 verifyFormat("while ( ::func() )\n return;", Spaces); 21443 21444 // Check impact of ControlStatementsExceptControlMacros is honored. 21445 Spaces.SpaceBeforeParens = 21446 FormatStyle::SBPO_ControlStatementsExceptControlMacros; 21447 verifyFormat("MYIF( a )\n return;", Spaces); 21448 verifyFormat("MYIF( a )\n return;\nelse MYIF( b )\n return;", Spaces); 21449 verifyFormat("MYIF( a )\n return;\nelse\n return;", Spaces); 21450 } 21451 21452 TEST_F(FormatTest, AlternativeOperators) { 21453 // Test case for ensuring alternate operators are not 21454 // combined with their right most neighbour. 21455 verifyFormat("int a and b;"); 21456 verifyFormat("int a and_eq b;"); 21457 verifyFormat("int a bitand b;"); 21458 verifyFormat("int a bitor b;"); 21459 verifyFormat("int a compl b;"); 21460 verifyFormat("int a not b;"); 21461 verifyFormat("int a not_eq b;"); 21462 verifyFormat("int a or b;"); 21463 verifyFormat("int a xor b;"); 21464 verifyFormat("int a xor_eq b;"); 21465 verifyFormat("return this not_eq bitand other;"); 21466 verifyFormat("bool operator not_eq(const X bitand other)"); 21467 21468 verifyFormat("int a and 5;"); 21469 verifyFormat("int a and_eq 5;"); 21470 verifyFormat("int a bitand 5;"); 21471 verifyFormat("int a bitor 5;"); 21472 verifyFormat("int a compl 5;"); 21473 verifyFormat("int a not 5;"); 21474 verifyFormat("int a not_eq 5;"); 21475 verifyFormat("int a or 5;"); 21476 verifyFormat("int a xor 5;"); 21477 verifyFormat("int a xor_eq 5;"); 21478 21479 verifyFormat("int a compl(5);"); 21480 verifyFormat("int a not(5);"); 21481 21482 /* FIXME handle alternate tokens 21483 * https://en.cppreference.com/w/cpp/language/operator_alternative 21484 // alternative tokens 21485 verifyFormat("compl foo();"); // ~foo(); 21486 verifyFormat("foo() <%%>;"); // foo(); 21487 verifyFormat("void foo() <%%>;"); // void foo(){} 21488 verifyFormat("int a <:1:>;"); // int a[1];[ 21489 verifyFormat("%:define ABC abc"); // #define ABC abc 21490 verifyFormat("%:%:"); // ## 21491 */ 21492 } 21493 21494 TEST_F(FormatTest, STLWhileNotDefineChed) { 21495 verifyFormat("#if defined(while)\n" 21496 "#define while EMIT WARNING C4005\n" 21497 "#endif // while"); 21498 } 21499 21500 TEST_F(FormatTest, OperatorSpacing) { 21501 FormatStyle Style = getLLVMStyle(); 21502 Style.PointerAlignment = FormatStyle::PAS_Right; 21503 verifyFormat("Foo::operator*();", Style); 21504 verifyFormat("Foo::operator void *();", Style); 21505 verifyFormat("Foo::operator void **();", Style); 21506 verifyFormat("Foo::operator void *&();", Style); 21507 verifyFormat("Foo::operator void *&&();", Style); 21508 verifyFormat("Foo::operator void const *();", Style); 21509 verifyFormat("Foo::operator void const **();", Style); 21510 verifyFormat("Foo::operator void const *&();", Style); 21511 verifyFormat("Foo::operator void const *&&();", Style); 21512 verifyFormat("Foo::operator()(void *);", Style); 21513 verifyFormat("Foo::operator*(void *);", Style); 21514 verifyFormat("Foo::operator*();", Style); 21515 verifyFormat("Foo::operator**();", Style); 21516 verifyFormat("Foo::operator&();", Style); 21517 verifyFormat("Foo::operator<int> *();", Style); 21518 verifyFormat("Foo::operator<Foo> *();", Style); 21519 verifyFormat("Foo::operator<int> **();", Style); 21520 verifyFormat("Foo::operator<Foo> **();", Style); 21521 verifyFormat("Foo::operator<int> &();", Style); 21522 verifyFormat("Foo::operator<Foo> &();", Style); 21523 verifyFormat("Foo::operator<int> &&();", Style); 21524 verifyFormat("Foo::operator<Foo> &&();", Style); 21525 verifyFormat("Foo::operator<int> *&();", Style); 21526 verifyFormat("Foo::operator<Foo> *&();", Style); 21527 verifyFormat("Foo::operator<int> *&&();", Style); 21528 verifyFormat("Foo::operator<Foo> *&&();", Style); 21529 verifyFormat("operator*(int (*)(), class Foo);", Style); 21530 21531 verifyFormat("Foo::operator&();", Style); 21532 verifyFormat("Foo::operator void &();", Style); 21533 verifyFormat("Foo::operator void const &();", Style); 21534 verifyFormat("Foo::operator()(void &);", Style); 21535 verifyFormat("Foo::operator&(void &);", Style); 21536 verifyFormat("Foo::operator&();", Style); 21537 verifyFormat("operator&(int (&)(), class Foo);", Style); 21538 21539 verifyFormat("Foo::operator&&();", Style); 21540 verifyFormat("Foo::operator**();", Style); 21541 verifyFormat("Foo::operator void &&();", Style); 21542 verifyFormat("Foo::operator void const &&();", Style); 21543 verifyFormat("Foo::operator()(void &&);", Style); 21544 verifyFormat("Foo::operator&&(void &&);", Style); 21545 verifyFormat("Foo::operator&&();", Style); 21546 verifyFormat("operator&&(int(&&)(), class Foo);", Style); 21547 verifyFormat("operator const nsTArrayRight<E> &()", Style); 21548 verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()", 21549 Style); 21550 verifyFormat("operator void **()", Style); 21551 verifyFormat("operator const FooRight<Object> &()", Style); 21552 verifyFormat("operator const FooRight<Object> *()", Style); 21553 verifyFormat("operator const FooRight<Object> **()", Style); 21554 verifyFormat("operator const FooRight<Object> *&()", Style); 21555 verifyFormat("operator const FooRight<Object> *&&()", Style); 21556 21557 Style.PointerAlignment = FormatStyle::PAS_Left; 21558 verifyFormat("Foo::operator*();", Style); 21559 verifyFormat("Foo::operator**();", Style); 21560 verifyFormat("Foo::operator void*();", Style); 21561 verifyFormat("Foo::operator void**();", Style); 21562 verifyFormat("Foo::operator void*&();", Style); 21563 verifyFormat("Foo::operator void*&&();", Style); 21564 verifyFormat("Foo::operator void const*();", Style); 21565 verifyFormat("Foo::operator void const**();", Style); 21566 verifyFormat("Foo::operator void const*&();", Style); 21567 verifyFormat("Foo::operator void const*&&();", Style); 21568 verifyFormat("Foo::operator/*comment*/ void*();", Style); 21569 verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style); 21570 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style); 21571 verifyFormat("Foo::operator()(void*);", Style); 21572 verifyFormat("Foo::operator*(void*);", Style); 21573 verifyFormat("Foo::operator*();", Style); 21574 verifyFormat("Foo::operator<int>*();", Style); 21575 verifyFormat("Foo::operator<Foo>*();", Style); 21576 verifyFormat("Foo::operator<int>**();", Style); 21577 verifyFormat("Foo::operator<Foo>**();", Style); 21578 verifyFormat("Foo::operator<Foo>*&();", Style); 21579 verifyFormat("Foo::operator<int>&();", Style); 21580 verifyFormat("Foo::operator<Foo>&();", Style); 21581 verifyFormat("Foo::operator<int>&&();", Style); 21582 verifyFormat("Foo::operator<Foo>&&();", Style); 21583 verifyFormat("Foo::operator<int>*&();", Style); 21584 verifyFormat("Foo::operator<Foo>*&();", Style); 21585 verifyFormat("operator*(int (*)(), class Foo);", Style); 21586 21587 verifyFormat("Foo::operator&();", Style); 21588 verifyFormat("Foo::operator void&();", Style); 21589 verifyFormat("Foo::operator void const&();", Style); 21590 verifyFormat("Foo::operator/*comment*/ void&();", Style); 21591 verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style); 21592 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style); 21593 verifyFormat("Foo::operator()(void&);", Style); 21594 verifyFormat("Foo::operator&(void&);", Style); 21595 verifyFormat("Foo::operator&();", Style); 21596 verifyFormat("operator&(int (&)(), class Foo);", Style); 21597 21598 verifyFormat("Foo::operator&&();", Style); 21599 verifyFormat("Foo::operator void&&();", Style); 21600 verifyFormat("Foo::operator void const&&();", Style); 21601 verifyFormat("Foo::operator/*comment*/ void&&();", Style); 21602 verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style); 21603 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", 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 verifyFormat("operator const nsTArrayLeft<E>&()", Style); 21609 verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()", 21610 Style); 21611 verifyFormat("operator void**()", Style); 21612 verifyFormat("operator const FooLeft<Object>&()", Style); 21613 verifyFormat("operator const FooLeft<Object>*()", Style); 21614 verifyFormat("operator const FooLeft<Object>**()", Style); 21615 verifyFormat("operator const FooLeft<Object>*&()", Style); 21616 verifyFormat("operator const FooLeft<Object>*&&()", Style); 21617 21618 // PR45107 21619 verifyFormat("operator Vector<String>&();", Style); 21620 verifyFormat("operator const Vector<String>&();", Style); 21621 verifyFormat("operator foo::Bar*();", Style); 21622 verifyFormat("operator const Foo<X>::Bar<Y>*();", Style); 21623 verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();", 21624 Style); 21625 21626 Style.PointerAlignment = FormatStyle::PAS_Middle; 21627 verifyFormat("Foo::operator*();", Style); 21628 verifyFormat("Foo::operator void *();", Style); 21629 verifyFormat("Foo::operator()(void *);", Style); 21630 verifyFormat("Foo::operator*(void *);", Style); 21631 verifyFormat("Foo::operator*();", Style); 21632 verifyFormat("operator*(int (*)(), class Foo);", Style); 21633 21634 verifyFormat("Foo::operator&();", Style); 21635 verifyFormat("Foo::operator void &();", Style); 21636 verifyFormat("Foo::operator void const &();", Style); 21637 verifyFormat("Foo::operator()(void &);", Style); 21638 verifyFormat("Foo::operator&(void &);", Style); 21639 verifyFormat("Foo::operator&();", Style); 21640 verifyFormat("operator&(int (&)(), class Foo);", Style); 21641 21642 verifyFormat("Foo::operator&&();", Style); 21643 verifyFormat("Foo::operator void &&();", Style); 21644 verifyFormat("Foo::operator void const &&();", Style); 21645 verifyFormat("Foo::operator()(void &&);", Style); 21646 verifyFormat("Foo::operator&&(void &&);", Style); 21647 verifyFormat("Foo::operator&&();", Style); 21648 verifyFormat("operator&&(int(&&)(), class Foo);", Style); 21649 } 21650 21651 TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) { 21652 FormatStyle Style = getLLVMStyle(); 21653 // PR46157 21654 verifyFormat("foo(operator+, -42);", Style); 21655 verifyFormat("foo(operator++, -42);", Style); 21656 verifyFormat("foo(operator--, -42);", Style); 21657 verifyFormat("foo(-42, operator--);", Style); 21658 verifyFormat("foo(-42, operator, );", Style); 21659 verifyFormat("foo(operator, , -42);", Style); 21660 } 21661 21662 TEST_F(FormatTest, WhitespaceSensitiveMacros) { 21663 FormatStyle Style = getLLVMStyle(); 21664 Style.WhitespaceSensitiveMacros.push_back("FOO"); 21665 21666 // Don't use the helpers here, since 'mess up' will change the whitespace 21667 // and these are all whitespace sensitive by definition 21668 EXPECT_EQ("FOO(String-ized&Messy+But(: :Still)=Intentional);", 21669 format("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style)); 21670 EXPECT_EQ( 21671 "FOO(String-ized&Messy+But\\(: :Still)=Intentional);", 21672 format("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style)); 21673 EXPECT_EQ("FOO(String-ized&Messy+But,: :Still=Intentional);", 21674 format("FOO(String-ized&Messy+But,: :Still=Intentional);", Style)); 21675 EXPECT_EQ("FOO(String-ized&Messy+But,: :\n" 21676 " Still=Intentional);", 21677 format("FOO(String-ized&Messy+But,: :\n" 21678 " Still=Intentional);", 21679 Style)); 21680 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 21681 EXPECT_EQ("FOO(String-ized=&Messy+But,: :\n" 21682 " Still=Intentional);", 21683 format("FOO(String-ized=&Messy+But,: :\n" 21684 " Still=Intentional);", 21685 Style)); 21686 21687 Style.ColumnLimit = 21; 21688 EXPECT_EQ("FOO(String-ized&Messy+But: :Still=Intentional);", 21689 format("FOO(String-ized&Messy+But: :Still=Intentional);", Style)); 21690 } 21691 21692 TEST_F(FormatTest, VeryLongNamespaceCommentSplit) { 21693 // These tests are not in NamespaceFixer because that doesn't 21694 // test its interaction with line wrapping 21695 FormatStyle Style = getLLVMStyle(); 21696 Style.ColumnLimit = 80; 21697 verifyFormat("namespace {\n" 21698 "int i;\n" 21699 "int j;\n" 21700 "} // namespace", 21701 Style); 21702 21703 verifyFormat("namespace AAA {\n" 21704 "int i;\n" 21705 "int j;\n" 21706 "} // namespace AAA", 21707 Style); 21708 21709 EXPECT_EQ("namespace Averyveryveryverylongnamespace {\n" 21710 "int i;\n" 21711 "int j;\n" 21712 "} // namespace Averyveryveryverylongnamespace", 21713 format("namespace Averyveryveryverylongnamespace {\n" 21714 "int i;\n" 21715 "int j;\n" 21716 "}", 21717 Style)); 21718 21719 EXPECT_EQ( 21720 "namespace " 21721 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" 21722 " went::mad::now {\n" 21723 "int i;\n" 21724 "int j;\n" 21725 "} // namespace\n" 21726 " // " 21727 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" 21728 "went::mad::now", 21729 format("namespace " 21730 "would::it::save::you::a::lot::of::time::if_::i::" 21731 "just::gave::up::and_::went::mad::now {\n" 21732 "int i;\n" 21733 "int j;\n" 21734 "}", 21735 Style)); 21736 21737 // This used to duplicate the comment again and again on subsequent runs 21738 EXPECT_EQ( 21739 "namespace " 21740 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" 21741 " went::mad::now {\n" 21742 "int i;\n" 21743 "int j;\n" 21744 "} // namespace\n" 21745 " // " 21746 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" 21747 "went::mad::now", 21748 format("namespace " 21749 "would::it::save::you::a::lot::of::time::if_::i::" 21750 "just::gave::up::and_::went::mad::now {\n" 21751 "int i;\n" 21752 "int j;\n" 21753 "} // namespace\n" 21754 " // " 21755 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::" 21756 "and_::went::mad::now", 21757 Style)); 21758 } 21759 21760 TEST_F(FormatTest, LikelyUnlikely) { 21761 FormatStyle Style = getLLVMStyle(); 21762 21763 verifyFormat("if (argc > 5) [[unlikely]] {\n" 21764 " return 29;\n" 21765 "}", 21766 Style); 21767 21768 verifyFormat("if (argc > 5) [[likely]] {\n" 21769 " return 29;\n" 21770 "}", 21771 Style); 21772 21773 verifyFormat("if (argc > 5) [[unlikely]] {\n" 21774 " return 29;\n" 21775 "} else [[likely]] {\n" 21776 " return 42;\n" 21777 "}\n", 21778 Style); 21779 21780 verifyFormat("if (argc > 5) [[unlikely]] {\n" 21781 " return 29;\n" 21782 "} else if (argc > 10) [[likely]] {\n" 21783 " return 99;\n" 21784 "} else {\n" 21785 " return 42;\n" 21786 "}\n", 21787 Style); 21788 21789 verifyFormat("if (argc > 5) [[gnu::unused]] {\n" 21790 " return 29;\n" 21791 "}", 21792 Style); 21793 } 21794 21795 TEST_F(FormatTest, PenaltyIndentedWhitespace) { 21796 verifyFormat("Constructor()\n" 21797 " : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 21798 " aaaa(aaaaaaaaaaaaaaaaaa, " 21799 "aaaaaaaaaaaaaaaaaat))"); 21800 verifyFormat("Constructor()\n" 21801 " : aaaaaaaaaaaaa(aaaaaa), " 21802 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)"); 21803 21804 FormatStyle StyleWithWhitespacePenalty = getLLVMStyle(); 21805 StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5; 21806 verifyFormat("Constructor()\n" 21807 " : aaaaaa(aaaaaa),\n" 21808 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 21809 " aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))", 21810 StyleWithWhitespacePenalty); 21811 verifyFormat("Constructor()\n" 21812 " : aaaaaaaaaaaaa(aaaaaa), " 21813 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)", 21814 StyleWithWhitespacePenalty); 21815 } 21816 21817 TEST_F(FormatTest, LLVMDefaultStyle) { 21818 FormatStyle Style = getLLVMStyle(); 21819 verifyFormat("extern \"C\" {\n" 21820 "int foo();\n" 21821 "}", 21822 Style); 21823 } 21824 TEST_F(FormatTest, GNUDefaultStyle) { 21825 FormatStyle Style = getGNUStyle(); 21826 verifyFormat("extern \"C\"\n" 21827 "{\n" 21828 " int foo ();\n" 21829 "}", 21830 Style); 21831 } 21832 TEST_F(FormatTest, MozillaDefaultStyle) { 21833 FormatStyle Style = getMozillaStyle(); 21834 verifyFormat("extern \"C\"\n" 21835 "{\n" 21836 " int foo();\n" 21837 "}", 21838 Style); 21839 } 21840 TEST_F(FormatTest, GoogleDefaultStyle) { 21841 FormatStyle Style = getGoogleStyle(); 21842 verifyFormat("extern \"C\" {\n" 21843 "int foo();\n" 21844 "}", 21845 Style); 21846 } 21847 TEST_F(FormatTest, ChromiumDefaultStyle) { 21848 FormatStyle Style = getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp); 21849 verifyFormat("extern \"C\" {\n" 21850 "int foo();\n" 21851 "}", 21852 Style); 21853 } 21854 TEST_F(FormatTest, MicrosoftDefaultStyle) { 21855 FormatStyle Style = getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp); 21856 verifyFormat("extern \"C\"\n" 21857 "{\n" 21858 " int foo();\n" 21859 "}", 21860 Style); 21861 } 21862 TEST_F(FormatTest, WebKitDefaultStyle) { 21863 FormatStyle Style = getWebKitStyle(); 21864 verifyFormat("extern \"C\" {\n" 21865 "int foo();\n" 21866 "}", 21867 Style); 21868 } 21869 21870 TEST_F(FormatTest, ConceptsAndRequires) { 21871 FormatStyle Style = getLLVMStyle(); 21872 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 21873 21874 verifyFormat("template <typename T>\n" 21875 "concept Hashable = requires(T a) {\n" 21876 " { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n" 21877 "};", 21878 Style); 21879 verifyFormat("template <typename T>\n" 21880 "concept EqualityComparable = requires(T a, T b) {\n" 21881 " { a == b } -> bool;\n" 21882 "};", 21883 Style); 21884 verifyFormat("template <typename T>\n" 21885 "concept EqualityComparable = requires(T a, T b) {\n" 21886 " { a == b } -> bool;\n" 21887 " { a != b } -> bool;\n" 21888 "};", 21889 Style); 21890 verifyFormat("template <typename T>\n" 21891 "concept EqualityComparable = requires(T a, T b) {\n" 21892 " { a == b } -> bool;\n" 21893 " { a != b } -> bool;\n" 21894 "};", 21895 Style); 21896 21897 verifyFormat("template <typename It>\n" 21898 "requires Iterator<It>\n" 21899 "void sort(It begin, It end) {\n" 21900 " //....\n" 21901 "}", 21902 Style); 21903 21904 verifyFormat("template <typename T>\n" 21905 "concept Large = sizeof(T) > 10;", 21906 Style); 21907 21908 verifyFormat("template <typename T, typename U>\n" 21909 "concept FooableWith = requires(T t, U u) {\n" 21910 " typename T::foo_type;\n" 21911 " { t.foo(u) } -> typename T::foo_type;\n" 21912 " t++;\n" 21913 "};\n" 21914 "void doFoo(FooableWith<int> auto t) {\n" 21915 " t.foo(3);\n" 21916 "}", 21917 Style); 21918 verifyFormat("template <typename T>\n" 21919 "concept Context = sizeof(T) == 1;", 21920 Style); 21921 verifyFormat("template <typename T>\n" 21922 "concept Context = is_specialization_of_v<context, T>;", 21923 Style); 21924 verifyFormat("template <typename T>\n" 21925 "concept Node = std::is_object_v<T>;", 21926 Style); 21927 verifyFormat("template <typename T>\n" 21928 "concept Tree = true;", 21929 Style); 21930 21931 verifyFormat("template <typename T> int g(T i) requires Concept1<I> {\n" 21932 " //...\n" 21933 "}", 21934 Style); 21935 21936 verifyFormat( 21937 "template <typename T> int g(T i) requires Concept1<I> && Concept2<I> {\n" 21938 " //...\n" 21939 "}", 21940 Style); 21941 21942 verifyFormat( 21943 "template <typename T> int g(T i) requires Concept1<I> || Concept2<I> {\n" 21944 " //...\n" 21945 "}", 21946 Style); 21947 21948 verifyFormat("template <typename T>\n" 21949 "veryveryvery_long_return_type g(T i) requires Concept1<I> || " 21950 "Concept2<I> {\n" 21951 " //...\n" 21952 "}", 21953 Style); 21954 21955 verifyFormat("template <typename T>\n" 21956 "veryveryvery_long_return_type g(T i) requires Concept1<I> && " 21957 "Concept2<I> {\n" 21958 " //...\n" 21959 "}", 21960 Style); 21961 21962 verifyFormat( 21963 "template <typename T>\n" 21964 "veryveryvery_long_return_type g(T i) requires Concept1 && Concept2 {\n" 21965 " //...\n" 21966 "}", 21967 Style); 21968 21969 verifyFormat( 21970 "template <typename T>\n" 21971 "veryveryvery_long_return_type g(T i) requires Concept1 || Concept2 {\n" 21972 " //...\n" 21973 "}", 21974 Style); 21975 21976 verifyFormat("template <typename It>\n" 21977 "requires Foo<It>() && Bar<It> {\n" 21978 " //....\n" 21979 "}", 21980 Style); 21981 21982 verifyFormat("template <typename It>\n" 21983 "requires Foo<Bar<It>>() && Bar<Foo<It, It>> {\n" 21984 " //....\n" 21985 "}", 21986 Style); 21987 21988 verifyFormat("template <typename It>\n" 21989 "requires Foo<Bar<It, It>>() && Bar<Foo<It, It>> {\n" 21990 " //....\n" 21991 "}", 21992 Style); 21993 21994 verifyFormat( 21995 "template <typename It>\n" 21996 "requires Foo<Bar<It>, Baz<It>>() && Bar<Foo<It>, Baz<It, It>> {\n" 21997 " //....\n" 21998 "}", 21999 Style); 22000 22001 Style.IndentRequires = true; 22002 verifyFormat("template <typename It>\n" 22003 " requires Iterator<It>\n" 22004 "void sort(It begin, It end) {\n" 22005 " //....\n" 22006 "}", 22007 Style); 22008 verifyFormat("template <std::size index_>\n" 22009 " requires(index_ < sizeof...(Children_))\n" 22010 "Tree auto &child() {\n" 22011 " // ...\n" 22012 "}", 22013 Style); 22014 22015 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 22016 verifyFormat("template <typename T>\n" 22017 "concept Hashable = requires (T a) {\n" 22018 " { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n" 22019 "};", 22020 Style); 22021 22022 verifyFormat("template <class T = void>\n" 22023 " requires EqualityComparable<T> || Same<T, void>\n" 22024 "struct equal_to;", 22025 Style); 22026 22027 verifyFormat("template <class T>\n" 22028 " requires requires {\n" 22029 " T{};\n" 22030 " T (int);\n" 22031 " }\n", 22032 Style); 22033 22034 Style.ColumnLimit = 78; 22035 verifyFormat("template <typename T>\n" 22036 "concept Context = Traits<typename T::traits_type> and\n" 22037 " Interface<typename T::interface_type> and\n" 22038 " Request<typename T::request_type> and\n" 22039 " Response<typename T::response_type> and\n" 22040 " ContextExtension<typename T::extension_type> and\n" 22041 " ::std::is_copy_constructable<T> and " 22042 "::std::is_move_constructable<T> and\n" 22043 " requires (T c) {\n" 22044 " { c.response; } -> Response;\n" 22045 "} and requires (T c) {\n" 22046 " { c.request; } -> Request;\n" 22047 "}\n", 22048 Style); 22049 22050 verifyFormat("template <typename T>\n" 22051 "concept Context = Traits<typename T::traits_type> or\n" 22052 " Interface<typename T::interface_type> or\n" 22053 " Request<typename T::request_type> or\n" 22054 " Response<typename T::response_type> or\n" 22055 " ContextExtension<typename T::extension_type> or\n" 22056 " ::std::is_copy_constructable<T> or " 22057 "::std::is_move_constructable<T> or\n" 22058 " requires (T c) {\n" 22059 " { c.response; } -> Response;\n" 22060 "} or requires (T c) {\n" 22061 " { c.request; } -> Request;\n" 22062 "}\n", 22063 Style); 22064 22065 verifyFormat("template <typename T>\n" 22066 "concept Context = Traits<typename T::traits_type> &&\n" 22067 " Interface<typename T::interface_type> &&\n" 22068 " Request<typename T::request_type> &&\n" 22069 " Response<typename T::response_type> &&\n" 22070 " ContextExtension<typename T::extension_type> &&\n" 22071 " ::std::is_copy_constructable<T> && " 22072 "::std::is_move_constructable<T> &&\n" 22073 " requires (T c) {\n" 22074 " { c.response; } -> Response;\n" 22075 "} && requires (T c) {\n" 22076 " { c.request; } -> Request;\n" 22077 "}\n", 22078 Style); 22079 22080 verifyFormat("template <typename T>\nconcept someConcept = Constraint1<T> && " 22081 "Constraint2<T>;"); 22082 22083 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 22084 Style.BraceWrapping.AfterFunction = true; 22085 Style.BraceWrapping.AfterClass = true; 22086 Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 22087 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 22088 verifyFormat("void Foo () requires (std::copyable<T>)\n" 22089 "{\n" 22090 " return\n" 22091 "}\n", 22092 Style); 22093 22094 verifyFormat("void Foo () requires std::copyable<T>\n" 22095 "{\n" 22096 " return\n" 22097 "}\n", 22098 Style); 22099 22100 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 22101 " requires (std::invocable<F, std::invoke_result_t<Args>...>)\n" 22102 "struct constant;", 22103 Style); 22104 22105 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 22106 " requires std::invocable<F, std::invoke_result_t<Args>...>\n" 22107 "struct constant;", 22108 Style); 22109 22110 verifyFormat("template <class T>\n" 22111 "class plane_with_very_very_very_long_name\n" 22112 "{\n" 22113 " constexpr plane_with_very_very_very_long_name () requires " 22114 "std::copyable<T>\n" 22115 " : plane_with_very_very_very_long_name (1)\n" 22116 " {\n" 22117 " }\n" 22118 "}\n", 22119 Style); 22120 22121 verifyFormat("template <class T>\n" 22122 "class plane_with_long_name\n" 22123 "{\n" 22124 " constexpr plane_with_long_name () requires std::copyable<T>\n" 22125 " : plane_with_long_name (1)\n" 22126 " {\n" 22127 " }\n" 22128 "}\n", 22129 Style); 22130 22131 Style.BreakBeforeConceptDeclarations = false; 22132 verifyFormat("template <typename T> concept Tree = true;", Style); 22133 22134 Style.IndentRequires = false; 22135 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 22136 "requires (std::invocable<F, std::invoke_result_t<Args>...>) " 22137 "struct constant;", 22138 Style); 22139 } 22140 22141 TEST_F(FormatTest, StatementAttributeLikeMacros) { 22142 FormatStyle Style = getLLVMStyle(); 22143 StringRef Source = "void Foo::slot() {\n" 22144 " unsigned char MyChar = 'x';\n" 22145 " emit signal(MyChar);\n" 22146 " Q_EMIT signal(MyChar);\n" 22147 "}"; 22148 22149 EXPECT_EQ(Source, format(Source, Style)); 22150 22151 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 22152 EXPECT_EQ("void Foo::slot() {\n" 22153 " unsigned char MyChar = 'x';\n" 22154 " emit signal(MyChar);\n" 22155 " Q_EMIT signal(MyChar);\n" 22156 "}", 22157 format(Source, Style)); 22158 22159 Style.StatementAttributeLikeMacros.push_back("emit"); 22160 EXPECT_EQ(Source, format(Source, Style)); 22161 22162 Style.StatementAttributeLikeMacros = {}; 22163 EXPECT_EQ("void Foo::slot() {\n" 22164 " unsigned char MyChar = 'x';\n" 22165 " emit signal(MyChar);\n" 22166 " Q_EMIT signal(MyChar);\n" 22167 "}", 22168 format(Source, Style)); 22169 } 22170 22171 TEST_F(FormatTest, IndentAccessModifiers) { 22172 FormatStyle Style = getLLVMStyle(); 22173 Style.IndentAccessModifiers = true; 22174 // Members are *two* levels below the record; 22175 // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation. 22176 verifyFormat("class C {\n" 22177 " int i;\n" 22178 "};\n", 22179 Style); 22180 verifyFormat("union C {\n" 22181 " int i;\n" 22182 " unsigned u;\n" 22183 "};\n", 22184 Style); 22185 // Access modifiers should be indented one level below the record. 22186 verifyFormat("class C {\n" 22187 " public:\n" 22188 " int i;\n" 22189 "};\n", 22190 Style); 22191 verifyFormat("struct S {\n" 22192 " private:\n" 22193 " class C {\n" 22194 " int j;\n" 22195 "\n" 22196 " public:\n" 22197 " C();\n" 22198 " };\n" 22199 "\n" 22200 " public:\n" 22201 " int i;\n" 22202 "};\n", 22203 Style); 22204 // Enumerations are not records and should be unaffected. 22205 Style.AllowShortEnumsOnASingleLine = false; 22206 verifyFormat("enum class E {\n" 22207 " A,\n" 22208 " B\n" 22209 "};\n", 22210 Style); 22211 // Test with a different indentation width; 22212 // also proves that the result is Style.AccessModifierOffset agnostic. 22213 Style.IndentWidth = 3; 22214 verifyFormat("class C {\n" 22215 " public:\n" 22216 " int i;\n" 22217 "};\n", 22218 Style); 22219 } 22220 22221 TEST_F(FormatTest, LimitlessStringsAndComments) { 22222 auto Style = getLLVMStyleWithColumns(0); 22223 constexpr StringRef Code = 22224 "/**\n" 22225 " * This is a multiline comment with quite some long lines, at least for " 22226 "the LLVM Style.\n" 22227 " * We will redo this with strings and line comments. Just to check if " 22228 "everything is working.\n" 22229 " */\n" 22230 "bool foo() {\n" 22231 " /* Single line multi line comment. */\n" 22232 " const std::string String = \"This is a multiline string with quite " 22233 "some long lines, at least for the LLVM Style.\"\n" 22234 " \"We already did it with multi line " 22235 "comments, and we will do it with line comments. Just to check if " 22236 "everything is working.\";\n" 22237 " // This is a line comment (block) with quite some long lines, at " 22238 "least for the LLVM Style.\n" 22239 " // We already did this with multi line comments and strings. Just to " 22240 "check if everything is working.\n" 22241 " const std::string SmallString = \"Hello World\";\n" 22242 " // Small line comment\n" 22243 " return String.size() > SmallString.size();\n" 22244 "}"; 22245 EXPECT_EQ(Code, format(Code, Style)); 22246 } 22247 } // namespace 22248 } // namespace format 22249 } // namespace clang 22250