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 8251 // The return breaking style doesn't affect: 8252 // * function and object definitions with attribute-like macros 8253 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n" 8254 " ABSL_GUARDED_BY(mutex) = {};", 8255 getGoogleStyleWithColumns(40)); 8256 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n" 8257 " ABSL_GUARDED_BY(mutex); // comment", 8258 getGoogleStyleWithColumns(40)); 8259 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n" 8260 " ABSL_GUARDED_BY(mutex1)\n" 8261 " ABSL_GUARDED_BY(mutex2);", 8262 getGoogleStyleWithColumns(40)); 8263 verifyFormat("Tttttt f(int a, int b)\n" 8264 " ABSL_GUARDED_BY(mutex1)\n" 8265 " ABSL_GUARDED_BY(mutex2);", 8266 getGoogleStyleWithColumns(40)); 8267 // * typedefs 8268 verifyFormat("typedef ATTR(X) char x;", getGoogleStyle()); 8269 8270 Style = getGNUStyle(); 8271 8272 // Test for comments at the end of function declarations. 8273 verifyFormat("void\n" 8274 "foo (int a, /*abc*/ int b) // def\n" 8275 "{\n" 8276 "}\n", 8277 Style); 8278 8279 verifyFormat("void\n" 8280 "foo (int a, /* abc */ int b) /* def */\n" 8281 "{\n" 8282 "}\n", 8283 Style); 8284 8285 // Definitions that should not break after return type 8286 verifyFormat("void foo (int a, int b); // def\n", Style); 8287 verifyFormat("void foo (int a, int b); /* def */\n", Style); 8288 verifyFormat("void foo (int a, int b);\n", Style); 8289 } 8290 8291 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 8292 FormatStyle NoBreak = getLLVMStyle(); 8293 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 8294 FormatStyle Break = getLLVMStyle(); 8295 Break.AlwaysBreakBeforeMultilineStrings = true; 8296 verifyFormat("aaaa = \"bbbb\"\n" 8297 " \"cccc\";", 8298 NoBreak); 8299 verifyFormat("aaaa =\n" 8300 " \"bbbb\"\n" 8301 " \"cccc\";", 8302 Break); 8303 verifyFormat("aaaa(\"bbbb\"\n" 8304 " \"cccc\");", 8305 NoBreak); 8306 verifyFormat("aaaa(\n" 8307 " \"bbbb\"\n" 8308 " \"cccc\");", 8309 Break); 8310 verifyFormat("aaaa(qqq, \"bbbb\"\n" 8311 " \"cccc\");", 8312 NoBreak); 8313 verifyFormat("aaaa(qqq,\n" 8314 " \"bbbb\"\n" 8315 " \"cccc\");", 8316 Break); 8317 verifyFormat("aaaa(qqq,\n" 8318 " L\"bbbb\"\n" 8319 " L\"cccc\");", 8320 Break); 8321 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 8322 " \"bbbb\"));", 8323 Break); 8324 verifyFormat("string s = someFunction(\n" 8325 " \"abc\"\n" 8326 " \"abc\");", 8327 Break); 8328 8329 // As we break before unary operators, breaking right after them is bad. 8330 verifyFormat("string foo = abc ? \"x\"\n" 8331 " \"blah blah blah blah blah blah\"\n" 8332 " : \"y\";", 8333 Break); 8334 8335 // Don't break if there is no column gain. 8336 verifyFormat("f(\"aaaa\"\n" 8337 " \"bbbb\");", 8338 Break); 8339 8340 // Treat literals with escaped newlines like multi-line string literals. 8341 EXPECT_EQ("x = \"a\\\n" 8342 "b\\\n" 8343 "c\";", 8344 format("x = \"a\\\n" 8345 "b\\\n" 8346 "c\";", 8347 NoBreak)); 8348 EXPECT_EQ("xxxx =\n" 8349 " \"a\\\n" 8350 "b\\\n" 8351 "c\";", 8352 format("xxxx = \"a\\\n" 8353 "b\\\n" 8354 "c\";", 8355 Break)); 8356 8357 EXPECT_EQ("NSString *const kString =\n" 8358 " @\"aaaa\"\n" 8359 " @\"bbbb\";", 8360 format("NSString *const kString = @\"aaaa\"\n" 8361 "@\"bbbb\";", 8362 Break)); 8363 8364 Break.ColumnLimit = 0; 8365 verifyFormat("const char *hello = \"hello llvm\";", Break); 8366 } 8367 8368 TEST_F(FormatTest, AlignsPipes) { 8369 verifyFormat( 8370 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8371 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8372 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8373 verifyFormat( 8374 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 8375 " << aaaaaaaaaaaaaaaaaaaa;"); 8376 verifyFormat( 8377 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8378 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8379 verifyFormat( 8380 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 8381 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8382 verifyFormat( 8383 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 8384 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 8385 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 8386 verifyFormat( 8387 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8388 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8389 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8390 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8391 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8392 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8393 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 8394 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 8395 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 8396 verifyFormat( 8397 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8398 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8399 verifyFormat( 8400 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n" 8401 " aaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8402 8403 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 8404 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 8405 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8406 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8407 " aaaaaaaaaaaaaaaaaaaaa)\n" 8408 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8409 verifyFormat("LOG_IF(aaa == //\n" 8410 " bbb)\n" 8411 " << a << b;"); 8412 8413 // But sometimes, breaking before the first "<<" is desirable. 8414 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 8415 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 8416 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 8417 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8418 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8419 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 8420 " << BEF << IsTemplate << Description << E->getType();"); 8421 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 8422 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8423 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8424 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 8425 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8426 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8427 " << aaa;"); 8428 8429 verifyFormat( 8430 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8431 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8432 8433 // Incomplete string literal. 8434 EXPECT_EQ("llvm::errs() << \"\n" 8435 " << a;", 8436 format("llvm::errs() << \"\n<<a;")); 8437 8438 verifyFormat("void f() {\n" 8439 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 8440 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 8441 "}"); 8442 8443 // Handle 'endl'. 8444 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 8445 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 8446 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 8447 8448 // Handle '\n'. 8449 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 8450 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 8451 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 8452 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 8453 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 8454 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 8455 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 8456 } 8457 8458 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) { 8459 verifyFormat("return out << \"somepacket = {\\n\"\n" 8460 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 8461 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 8462 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 8463 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 8464 " << \"}\";"); 8465 8466 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 8467 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 8468 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 8469 verifyFormat( 8470 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 8471 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 8472 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 8473 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 8474 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 8475 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 8476 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 8477 verifyFormat( 8478 "void f() {\n" 8479 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 8480 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 8481 "}"); 8482 8483 // Breaking before the first "<<" is generally not desirable. 8484 verifyFormat( 8485 "llvm::errs()\n" 8486 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8487 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8488 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8489 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 8490 getLLVMStyleWithColumns(70)); 8491 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 8492 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8493 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 8494 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8495 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 8496 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 8497 getLLVMStyleWithColumns(70)); 8498 8499 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 8500 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 8501 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;"); 8502 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 8503 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 8504 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);"); 8505 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n" 8506 " (aaaa + aaaa);", 8507 getLLVMStyleWithColumns(40)); 8508 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n" 8509 " (aaaaaaa + aaaaa));", 8510 getLLVMStyleWithColumns(40)); 8511 verifyFormat( 8512 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n" 8513 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n" 8514 " bbbbbbbbbbbbbbbbbbbbbbb);"); 8515 } 8516 8517 TEST_F(FormatTest, UnderstandsEquals) { 8518 verifyFormat( 8519 "aaaaaaaaaaaaaaaaa =\n" 8520 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8521 verifyFormat( 8522 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8523 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 8524 verifyFormat( 8525 "if (a) {\n" 8526 " f();\n" 8527 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8528 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 8529 "}"); 8530 8531 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8532 " 100000000 + 10000000) {\n}"); 8533 } 8534 8535 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 8536 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 8537 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 8538 8539 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 8540 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 8541 8542 verifyFormat( 8543 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 8544 " Parameter2);"); 8545 8546 verifyFormat( 8547 "ShortObject->shortFunction(\n" 8548 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 8549 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 8550 8551 verifyFormat("loooooooooooooongFunction(\n" 8552 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 8553 8554 verifyFormat( 8555 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 8556 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 8557 8558 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 8559 " .WillRepeatedly(Return(SomeValue));"); 8560 verifyFormat("void f() {\n" 8561 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 8562 " .Times(2)\n" 8563 " .WillRepeatedly(Return(SomeValue));\n" 8564 "}"); 8565 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 8566 " ccccccccccccccccccccccc);"); 8567 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8568 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8569 " .aaaaa(aaaaa),\n" 8570 " aaaaaaaaaaaaaaaaaaaaa);"); 8571 verifyFormat("void f() {\n" 8572 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8573 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 8574 "}"); 8575 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8576 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8577 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8578 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8579 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 8580 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8581 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8582 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8583 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 8584 "}"); 8585 8586 // Here, it is not necessary to wrap at "." or "->". 8587 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 8588 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 8589 verifyFormat( 8590 "aaaaaaaaaaa->aaaaaaaaa(\n" 8591 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8592 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 8593 8594 verifyFormat( 8595 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8596 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 8597 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 8598 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 8599 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 8600 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 8601 8602 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8603 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8604 " .a();"); 8605 8606 FormatStyle NoBinPacking = getLLVMStyle(); 8607 NoBinPacking.BinPackParameters = false; 8608 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 8609 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 8610 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 8611 " aaaaaaaaaaaaaaaaaaa,\n" 8612 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8613 NoBinPacking); 8614 8615 // If there is a subsequent call, change to hanging indentation. 8616 verifyFormat( 8617 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8618 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 8619 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8620 verifyFormat( 8621 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8622 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 8623 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8624 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8625 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8626 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8627 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8628 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 8629 } 8630 8631 TEST_F(FormatTest, WrapsTemplateDeclarations) { 8632 verifyFormat("template <typename T>\n" 8633 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 8634 verifyFormat("template <typename T>\n" 8635 "// T should be one of {A, B}.\n" 8636 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 8637 verifyFormat( 8638 "template <typename T>\n" 8639 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 8640 verifyFormat("template <typename T>\n" 8641 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 8642 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 8643 verifyFormat( 8644 "template <typename T>\n" 8645 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 8646 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 8647 verifyFormat( 8648 "template <typename T>\n" 8649 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 8650 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 8651 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8652 verifyFormat("template <typename T>\n" 8653 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8654 " int aaaaaaaaaaaaaaaaaaaaaa);"); 8655 verifyFormat( 8656 "template <typename T1, typename T2 = char, typename T3 = char,\n" 8657 " typename T4 = char>\n" 8658 "void f();"); 8659 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 8660 " template <typename> class cccccccccccccccccccccc,\n" 8661 " typename ddddddddddddd>\n" 8662 "class C {};"); 8663 verifyFormat( 8664 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 8665 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8666 8667 verifyFormat("void f() {\n" 8668 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 8669 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 8670 "}"); 8671 8672 verifyFormat("template <typename T> class C {};"); 8673 verifyFormat("template <typename T> void f();"); 8674 verifyFormat("template <typename T> void f() {}"); 8675 verifyFormat( 8676 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 8677 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8678 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 8679 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 8680 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8681 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 8682 " bbbbbbbbbbbbbbbbbbbbbbbb);", 8683 getLLVMStyleWithColumns(72)); 8684 EXPECT_EQ("static_cast<A< //\n" 8685 " B> *>(\n" 8686 "\n" 8687 ");", 8688 format("static_cast<A<//\n" 8689 " B>*>(\n" 8690 "\n" 8691 " );")); 8692 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8693 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 8694 8695 FormatStyle AlwaysBreak = getLLVMStyle(); 8696 AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 8697 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 8698 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 8699 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 8700 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8701 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 8702 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 8703 verifyFormat("template <template <typename> class Fooooooo,\n" 8704 " template <typename> class Baaaaaaar>\n" 8705 "struct C {};", 8706 AlwaysBreak); 8707 verifyFormat("template <typename T> // T can be A, B or C.\n" 8708 "struct C {};", 8709 AlwaysBreak); 8710 verifyFormat("template <enum E> class A {\n" 8711 "public:\n" 8712 " E *f();\n" 8713 "};"); 8714 8715 FormatStyle NeverBreak = getLLVMStyle(); 8716 NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No; 8717 verifyFormat("template <typename T> class C {};", NeverBreak); 8718 verifyFormat("template <typename T> void f();", NeverBreak); 8719 verifyFormat("template <typename T> void f() {}", NeverBreak); 8720 verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa " 8721 "bbbbbbbbbbbbbbbbbbbb) {}", 8722 NeverBreak); 8723 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8724 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 8725 " ccccccccccccccccccccccccccccccccccccccccccccccc);", 8726 NeverBreak); 8727 verifyFormat("template <template <typename> class Fooooooo,\n" 8728 " template <typename> class Baaaaaaar>\n" 8729 "struct C {};", 8730 NeverBreak); 8731 verifyFormat("template <typename T> // T can be A, B or C.\n" 8732 "struct C {};", 8733 NeverBreak); 8734 verifyFormat("template <enum E> class A {\n" 8735 "public:\n" 8736 " E *f();\n" 8737 "};", 8738 NeverBreak); 8739 NeverBreak.PenaltyBreakTemplateDeclaration = 100; 8740 verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa " 8741 "bbbbbbbbbbbbbbbbbbbb) {}", 8742 NeverBreak); 8743 } 8744 8745 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) { 8746 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 8747 Style.ColumnLimit = 60; 8748 EXPECT_EQ("// Baseline - no comments.\n" 8749 "template <\n" 8750 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 8751 "void f() {}", 8752 format("// Baseline - no comments.\n" 8753 "template <\n" 8754 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 8755 "void f() {}", 8756 Style)); 8757 8758 EXPECT_EQ("template <\n" 8759 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 8760 "void f() {}", 8761 format("template <\n" 8762 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 8763 "void f() {}", 8764 Style)); 8765 8766 EXPECT_EQ( 8767 "template <\n" 8768 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 8769 "void f() {}", 8770 format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 8771 "void f() {}", 8772 Style)); 8773 8774 EXPECT_EQ( 8775 "template <\n" 8776 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 8777 " // multiline\n" 8778 "void f() {}", 8779 format("template <\n" 8780 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 8781 " // multiline\n" 8782 "void f() {}", 8783 Style)); 8784 8785 EXPECT_EQ( 8786 "template <typename aaaaaaaaaa<\n" 8787 " bbbbbbbbbbbb>::value> // trailing loooong\n" 8788 "void f() {}", 8789 format( 8790 "template <\n" 8791 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n" 8792 "void f() {}", 8793 Style)); 8794 } 8795 8796 TEST_F(FormatTest, WrapsTemplateParameters) { 8797 FormatStyle Style = getLLVMStyle(); 8798 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 8799 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 8800 verifyFormat( 8801 "template <typename... a> struct q {};\n" 8802 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 8803 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 8804 " y;", 8805 Style); 8806 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 8807 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 8808 verifyFormat( 8809 "template <typename... a> struct r {};\n" 8810 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 8811 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 8812 " y;", 8813 Style); 8814 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 8815 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 8816 verifyFormat("template <typename... a> struct s {};\n" 8817 "extern s<\n" 8818 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 8819 "aaaaaaaaaaaaaaaaaaaaaa,\n" 8820 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 8821 "aaaaaaaaaaaaaaaaaaaaaa>\n" 8822 " y;", 8823 Style); 8824 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 8825 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 8826 verifyFormat("template <typename... a> struct t {};\n" 8827 "extern t<\n" 8828 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 8829 "aaaaaaaaaaaaaaaaaaaaaa,\n" 8830 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 8831 "aaaaaaaaaaaaaaaaaaaaaa>\n" 8832 " y;", 8833 Style); 8834 } 8835 8836 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 8837 verifyFormat( 8838 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8839 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8840 verifyFormat( 8841 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8842 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8843 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 8844 8845 // FIXME: Should we have the extra indent after the second break? 8846 verifyFormat( 8847 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8848 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8849 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8850 8851 verifyFormat( 8852 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 8853 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 8854 8855 // Breaking at nested name specifiers is generally not desirable. 8856 verifyFormat( 8857 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8858 " aaaaaaaaaaaaaaaaaaaaaaa);"); 8859 8860 verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n" 8861 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8862 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8863 " aaaaaaaaaaaaaaaaaaaaa);", 8864 getLLVMStyleWithColumns(74)); 8865 8866 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8867 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8868 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8869 } 8870 8871 TEST_F(FormatTest, UnderstandsTemplateParameters) { 8872 verifyFormat("A<int> a;"); 8873 verifyFormat("A<A<A<int>>> a;"); 8874 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 8875 verifyFormat("bool x = a < 1 || 2 > a;"); 8876 verifyFormat("bool x = 5 < f<int>();"); 8877 verifyFormat("bool x = f<int>() > 5;"); 8878 verifyFormat("bool x = 5 < a<int>::x;"); 8879 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 8880 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 8881 8882 verifyGoogleFormat("A<A<int>> a;"); 8883 verifyGoogleFormat("A<A<A<int>>> a;"); 8884 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 8885 verifyGoogleFormat("A<A<int> > a;"); 8886 verifyGoogleFormat("A<A<A<int> > > a;"); 8887 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 8888 verifyGoogleFormat("A<::A<int>> a;"); 8889 verifyGoogleFormat("A<::A> a;"); 8890 verifyGoogleFormat("A< ::A> a;"); 8891 verifyGoogleFormat("A< ::A<int> > a;"); 8892 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 8893 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 8894 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 8895 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 8896 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 8897 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 8898 8899 verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 8900 8901 // template closer followed by a token that starts with > or = 8902 verifyFormat("bool b = a<1> > 1;"); 8903 verifyFormat("bool b = a<1> >= 1;"); 8904 verifyFormat("int i = a<1> >> 1;"); 8905 FormatStyle Style = getLLVMStyle(); 8906 Style.SpaceBeforeAssignmentOperators = false; 8907 verifyFormat("bool b= a<1> == 1;", Style); 8908 verifyFormat("a<int> = 1;", Style); 8909 verifyFormat("a<int> >>= 1;", Style); 8910 8911 verifyFormat("test < a | b >> c;"); 8912 verifyFormat("test<test<a | b>> c;"); 8913 verifyFormat("test >> a >> b;"); 8914 verifyFormat("test << a >> b;"); 8915 8916 verifyFormat("f<int>();"); 8917 verifyFormat("template <typename T> void f() {}"); 8918 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 8919 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 8920 "sizeof(char)>::type>;"); 8921 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 8922 verifyFormat("f(a.operator()<A>());"); 8923 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8924 " .template operator()<A>());", 8925 getLLVMStyleWithColumns(35)); 8926 8927 // Not template parameters. 8928 verifyFormat("return a < b && c > d;"); 8929 verifyFormat("void f() {\n" 8930 " while (a < b && c > d) {\n" 8931 " }\n" 8932 "}"); 8933 verifyFormat("template <typename... Types>\n" 8934 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 8935 8936 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8937 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 8938 getLLVMStyleWithColumns(60)); 8939 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 8940 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 8941 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 8942 verifyFormat("some_templated_type<decltype([](int i) { return i; })>"); 8943 } 8944 8945 TEST_F(FormatTest, UnderstandsShiftOperators) { 8946 verifyFormat("if (i < x >> 1)"); 8947 verifyFormat("while (i < x >> 1)"); 8948 verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)"); 8949 verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)"); 8950 verifyFormat( 8951 "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)"); 8952 verifyFormat("Foo.call<Bar<Function>>()"); 8953 verifyFormat("if (Foo.call<Bar<Function>>() == 0)"); 8954 verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; " 8955 "++i, v = v >> 1)"); 8956 verifyFormat("if (w<u<v<x>>, 1>::t)"); 8957 } 8958 8959 TEST_F(FormatTest, BitshiftOperatorWidth) { 8960 EXPECT_EQ("int a = 1 << 2; /* foo\n" 8961 " bar */", 8962 format("int a=1<<2; /* foo\n" 8963 " bar */")); 8964 8965 EXPECT_EQ("int b = 256 >> 1; /* foo\n" 8966 " bar */", 8967 format("int b =256>>1 ; /* foo\n" 8968 " bar */")); 8969 } 8970 8971 TEST_F(FormatTest, UnderstandsBinaryOperators) { 8972 verifyFormat("COMPARE(a, ==, b);"); 8973 verifyFormat("auto s = sizeof...(Ts) - 1;"); 8974 } 8975 8976 TEST_F(FormatTest, UnderstandsPointersToMembers) { 8977 verifyFormat("int A::*x;"); 8978 verifyFormat("int (S::*func)(void *);"); 8979 verifyFormat("void f() { int (S::*func)(void *); }"); 8980 verifyFormat("typedef bool *(Class::*Member)() const;"); 8981 verifyFormat("void f() {\n" 8982 " (a->*f)();\n" 8983 " a->*x;\n" 8984 " (a.*f)();\n" 8985 " ((*a).*f)();\n" 8986 " a.*x;\n" 8987 "}"); 8988 verifyFormat("void f() {\n" 8989 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 8990 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 8991 "}"); 8992 verifyFormat( 8993 "(aaaaaaaaaa->*bbbbbbb)(\n" 8994 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 8995 FormatStyle Style = getLLVMStyle(); 8996 Style.PointerAlignment = FormatStyle::PAS_Left; 8997 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 8998 } 8999 9000 TEST_F(FormatTest, UnderstandsUnaryOperators) { 9001 verifyFormat("int a = -2;"); 9002 verifyFormat("f(-1, -2, -3);"); 9003 verifyFormat("a[-1] = 5;"); 9004 verifyFormat("int a = 5 + -2;"); 9005 verifyFormat("if (i == -1) {\n}"); 9006 verifyFormat("if (i != -1) {\n}"); 9007 verifyFormat("if (i > -1) {\n}"); 9008 verifyFormat("if (i < -1) {\n}"); 9009 verifyFormat("++(a->f());"); 9010 verifyFormat("--(a->f());"); 9011 verifyFormat("(a->f())++;"); 9012 verifyFormat("a[42]++;"); 9013 verifyFormat("if (!(a->f())) {\n}"); 9014 verifyFormat("if (!+i) {\n}"); 9015 verifyFormat("~&a;"); 9016 9017 verifyFormat("a-- > b;"); 9018 verifyFormat("b ? -a : c;"); 9019 verifyFormat("n * sizeof char16;"); 9020 verifyFormat("n * alignof char16;", getGoogleStyle()); 9021 verifyFormat("sizeof(char);"); 9022 verifyFormat("alignof(char);", getGoogleStyle()); 9023 9024 verifyFormat("return -1;"); 9025 verifyFormat("throw -1;"); 9026 verifyFormat("switch (a) {\n" 9027 "case -1:\n" 9028 " break;\n" 9029 "}"); 9030 verifyFormat("#define X -1"); 9031 verifyFormat("#define X -kConstant"); 9032 9033 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 9034 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 9035 9036 verifyFormat("int a = /* confusing comment */ -1;"); 9037 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 9038 verifyFormat("int a = i /* confusing comment */++;"); 9039 9040 verifyFormat("co_yield -1;"); 9041 verifyFormat("co_return -1;"); 9042 9043 // Check that * is not treated as a binary operator when we set 9044 // PointerAlignment as PAS_Left after a keyword and not a declaration. 9045 FormatStyle PASLeftStyle = getLLVMStyle(); 9046 PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left; 9047 verifyFormat("co_return *a;", PASLeftStyle); 9048 verifyFormat("co_await *a;", PASLeftStyle); 9049 verifyFormat("co_yield *a", PASLeftStyle); 9050 verifyFormat("return *a;", PASLeftStyle); 9051 } 9052 9053 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 9054 verifyFormat("if (!aaaaaaaaaa( // break\n" 9055 " aaaaa)) {\n" 9056 "}"); 9057 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 9058 " aaaaa));"); 9059 verifyFormat("*aaa = aaaaaaa( // break\n" 9060 " bbbbbb);"); 9061 } 9062 9063 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 9064 verifyFormat("bool operator<();"); 9065 verifyFormat("bool operator>();"); 9066 verifyFormat("bool operator=();"); 9067 verifyFormat("bool operator==();"); 9068 verifyFormat("bool operator!=();"); 9069 verifyFormat("int operator+();"); 9070 verifyFormat("int operator++();"); 9071 verifyFormat("int operator++(int) volatile noexcept;"); 9072 verifyFormat("bool operator,();"); 9073 verifyFormat("bool operator();"); 9074 verifyFormat("bool operator()();"); 9075 verifyFormat("bool operator[]();"); 9076 verifyFormat("operator bool();"); 9077 verifyFormat("operator int();"); 9078 verifyFormat("operator void *();"); 9079 verifyFormat("operator SomeType<int>();"); 9080 verifyFormat("operator SomeType<int, int>();"); 9081 verifyFormat("operator SomeType<SomeType<int>>();"); 9082 verifyFormat("void *operator new(std::size_t size);"); 9083 verifyFormat("void *operator new[](std::size_t size);"); 9084 verifyFormat("void operator delete(void *ptr);"); 9085 verifyFormat("void operator delete[](void *ptr);"); 9086 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 9087 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 9088 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 9089 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 9090 9091 verifyFormat( 9092 "ostream &operator<<(ostream &OutputStream,\n" 9093 " SomeReallyLongType WithSomeReallyLongValue);"); 9094 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 9095 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 9096 " return left.group < right.group;\n" 9097 "}"); 9098 verifyFormat("SomeType &operator=(const SomeType &S);"); 9099 verifyFormat("f.template operator()<int>();"); 9100 9101 verifyGoogleFormat("operator void*();"); 9102 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 9103 verifyGoogleFormat("operator ::A();"); 9104 9105 verifyFormat("using A::operator+;"); 9106 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 9107 "int i;"); 9108 9109 // Calling an operator as a member function. 9110 verifyFormat("void f() { a.operator*(); }"); 9111 verifyFormat("void f() { a.operator*(b & b); }"); 9112 verifyFormat("void f() { a->operator&(a * b); }"); 9113 verifyFormat("void f() { NS::a.operator+(*b * *b); }"); 9114 // TODO: Calling an operator as a non-member function is hard to distinguish. 9115 // https://llvm.org/PR50629 9116 // verifyFormat("void f() { operator*(a & a); }"); 9117 // verifyFormat("void f() { operator&(a, b * b); }"); 9118 } 9119 9120 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 9121 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 9122 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 9123 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 9124 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 9125 verifyFormat("Deleted &operator=(const Deleted &) &;"); 9126 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 9127 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 9128 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 9129 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 9130 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 9131 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 9132 verifyFormat("void Fn(T const &) const &;"); 9133 verifyFormat("void Fn(T const volatile &&) const volatile &&;"); 9134 verifyFormat("template <typename T>\n" 9135 "void F(T) && = delete;", 9136 getGoogleStyle()); 9137 9138 FormatStyle AlignLeft = getLLVMStyle(); 9139 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 9140 verifyFormat("void A::b() && {}", AlignLeft); 9141 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 9142 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 9143 AlignLeft); 9144 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 9145 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 9146 verifyFormat("auto Function(T t) & -> void {}", AlignLeft); 9147 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft); 9148 verifyFormat("auto Function(T) & -> void {}", AlignLeft); 9149 verifyFormat("auto Function(T) & -> void;", AlignLeft); 9150 verifyFormat("void Fn(T const&) const&;", AlignLeft); 9151 verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft); 9152 9153 FormatStyle Spaces = getLLVMStyle(); 9154 Spaces.SpacesInCStyleCastParentheses = true; 9155 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 9156 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 9157 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 9158 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 9159 9160 Spaces.SpacesInCStyleCastParentheses = false; 9161 Spaces.SpacesInParentheses = true; 9162 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 9163 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", 9164 Spaces); 9165 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 9166 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 9167 9168 FormatStyle BreakTemplate = getLLVMStyle(); 9169 BreakTemplate.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 9170 9171 verifyFormat("struct f {\n" 9172 " template <class T>\n" 9173 " int &foo(const std::string &str) &noexcept {}\n" 9174 "};", 9175 BreakTemplate); 9176 9177 verifyFormat("struct f {\n" 9178 " template <class T>\n" 9179 " int &foo(const std::string &str) &&noexcept {}\n" 9180 "};", 9181 BreakTemplate); 9182 9183 verifyFormat("struct f {\n" 9184 " template <class T>\n" 9185 " int &foo(const std::string &str) const &noexcept {}\n" 9186 "};", 9187 BreakTemplate); 9188 9189 verifyFormat("struct f {\n" 9190 " template <class T>\n" 9191 " int &foo(const std::string &str) const &noexcept {}\n" 9192 "};", 9193 BreakTemplate); 9194 9195 verifyFormat("struct f {\n" 9196 " template <class T>\n" 9197 " auto foo(const std::string &str) &&noexcept -> int & {}\n" 9198 "};", 9199 BreakTemplate); 9200 9201 FormatStyle AlignLeftBreakTemplate = getLLVMStyle(); 9202 AlignLeftBreakTemplate.AlwaysBreakTemplateDeclarations = 9203 FormatStyle::BTDS_Yes; 9204 AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left; 9205 9206 verifyFormat("struct f {\n" 9207 " template <class T>\n" 9208 " int& foo(const std::string& str) & noexcept {}\n" 9209 "};", 9210 AlignLeftBreakTemplate); 9211 9212 verifyFormat("struct f {\n" 9213 " template <class T>\n" 9214 " int& foo(const std::string& str) && noexcept {}\n" 9215 "};", 9216 AlignLeftBreakTemplate); 9217 9218 verifyFormat("struct f {\n" 9219 " template <class T>\n" 9220 " int& foo(const std::string& str) const& noexcept {}\n" 9221 "};", 9222 AlignLeftBreakTemplate); 9223 9224 verifyFormat("struct f {\n" 9225 " template <class T>\n" 9226 " int& foo(const std::string& str) const&& noexcept {}\n" 9227 "};", 9228 AlignLeftBreakTemplate); 9229 9230 verifyFormat("struct f {\n" 9231 " template <class T>\n" 9232 " auto foo(const std::string& str) && noexcept -> int& {}\n" 9233 "};", 9234 AlignLeftBreakTemplate); 9235 9236 // The `&` in `Type&` should not be confused with a trailing `&` of 9237 // DEPRECATED(reason) member function. 9238 verifyFormat("struct f {\n" 9239 " template <class T>\n" 9240 " DEPRECATED(reason)\n" 9241 " Type &foo(arguments) {}\n" 9242 "};", 9243 BreakTemplate); 9244 9245 verifyFormat("struct f {\n" 9246 " template <class T>\n" 9247 " DEPRECATED(reason)\n" 9248 " Type& foo(arguments) {}\n" 9249 "};", 9250 AlignLeftBreakTemplate); 9251 9252 verifyFormat("void (*foopt)(int) = &func;"); 9253 } 9254 9255 TEST_F(FormatTest, UnderstandsNewAndDelete) { 9256 verifyFormat("void f() {\n" 9257 " A *a = new A;\n" 9258 " A *a = new (placement) A;\n" 9259 " delete a;\n" 9260 " delete (A *)a;\n" 9261 "}"); 9262 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 9263 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 9264 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 9265 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 9266 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 9267 verifyFormat("delete[] h->p;"); 9268 } 9269 9270 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 9271 verifyFormat("int *f(int *a) {}"); 9272 verifyFormat("int main(int argc, char **argv) {}"); 9273 verifyFormat("Test::Test(int b) : a(b * b) {}"); 9274 verifyIndependentOfContext("f(a, *a);"); 9275 verifyFormat("void g() { f(*a); }"); 9276 verifyIndependentOfContext("int a = b * 10;"); 9277 verifyIndependentOfContext("int a = 10 * b;"); 9278 verifyIndependentOfContext("int a = b * c;"); 9279 verifyIndependentOfContext("int a += b * c;"); 9280 verifyIndependentOfContext("int a -= b * c;"); 9281 verifyIndependentOfContext("int a *= b * c;"); 9282 verifyIndependentOfContext("int a /= b * c;"); 9283 verifyIndependentOfContext("int a = *b;"); 9284 verifyIndependentOfContext("int a = *b * c;"); 9285 verifyIndependentOfContext("int a = b * *c;"); 9286 verifyIndependentOfContext("int a = b * (10);"); 9287 verifyIndependentOfContext("S << b * (10);"); 9288 verifyIndependentOfContext("return 10 * b;"); 9289 verifyIndependentOfContext("return *b * *c;"); 9290 verifyIndependentOfContext("return a & ~b;"); 9291 verifyIndependentOfContext("f(b ? *c : *d);"); 9292 verifyIndependentOfContext("int a = b ? *c : *d;"); 9293 verifyIndependentOfContext("*b = a;"); 9294 verifyIndependentOfContext("a * ~b;"); 9295 verifyIndependentOfContext("a * !b;"); 9296 verifyIndependentOfContext("a * +b;"); 9297 verifyIndependentOfContext("a * -b;"); 9298 verifyIndependentOfContext("a * ++b;"); 9299 verifyIndependentOfContext("a * --b;"); 9300 verifyIndependentOfContext("a[4] * b;"); 9301 verifyIndependentOfContext("a[a * a] = 1;"); 9302 verifyIndependentOfContext("f() * b;"); 9303 verifyIndependentOfContext("a * [self dostuff];"); 9304 verifyIndependentOfContext("int x = a * (a + b);"); 9305 verifyIndependentOfContext("(a *)(a + b);"); 9306 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 9307 verifyIndependentOfContext("int *pa = (int *)&a;"); 9308 verifyIndependentOfContext("return sizeof(int **);"); 9309 verifyIndependentOfContext("return sizeof(int ******);"); 9310 verifyIndependentOfContext("return (int **&)a;"); 9311 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 9312 verifyFormat("void f(Type (*parameter)[10]) {}"); 9313 verifyFormat("void f(Type (¶meter)[10]) {}"); 9314 verifyGoogleFormat("return sizeof(int**);"); 9315 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 9316 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 9317 verifyFormat("auto a = [](int **&, int ***) {};"); 9318 verifyFormat("auto PointerBinding = [](const char *S) {};"); 9319 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 9320 verifyFormat("[](const decltype(*a) &value) {}"); 9321 verifyFormat("[](const typeof(*a) &value) {}"); 9322 verifyFormat("[](const _Atomic(a *) &value) {}"); 9323 verifyFormat("[](const __underlying_type(a) &value) {}"); 9324 verifyFormat("decltype(a * b) F();"); 9325 verifyFormat("typeof(a * b) F();"); 9326 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 9327 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 9328 verifyIndependentOfContext("typedef void (*f)(int *a);"); 9329 verifyIndependentOfContext("int i{a * b};"); 9330 verifyIndependentOfContext("aaa && aaa->f();"); 9331 verifyIndependentOfContext("int x = ~*p;"); 9332 verifyFormat("Constructor() : a(a), area(width * height) {}"); 9333 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 9334 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 9335 verifyFormat("void f() { f(a, c * d); }"); 9336 verifyFormat("void f() { f(new a(), c * d); }"); 9337 verifyFormat("void f(const MyOverride &override);"); 9338 verifyFormat("void f(const MyFinal &final);"); 9339 verifyIndependentOfContext("bool a = f() && override.f();"); 9340 verifyIndependentOfContext("bool a = f() && final.f();"); 9341 9342 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 9343 9344 verifyIndependentOfContext("A<int *> a;"); 9345 verifyIndependentOfContext("A<int **> a;"); 9346 verifyIndependentOfContext("A<int *, int *> a;"); 9347 verifyIndependentOfContext("A<int *[]> a;"); 9348 verifyIndependentOfContext( 9349 "const char *const p = reinterpret_cast<const char *const>(q);"); 9350 verifyIndependentOfContext("A<int **, int **> a;"); 9351 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 9352 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 9353 verifyFormat("for (; a && b;) {\n}"); 9354 verifyFormat("bool foo = true && [] { return false; }();"); 9355 9356 verifyFormat( 9357 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9358 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9359 9360 verifyGoogleFormat("int const* a = &b;"); 9361 verifyGoogleFormat("**outparam = 1;"); 9362 verifyGoogleFormat("*outparam = a * b;"); 9363 verifyGoogleFormat("int main(int argc, char** argv) {}"); 9364 verifyGoogleFormat("A<int*> a;"); 9365 verifyGoogleFormat("A<int**> a;"); 9366 verifyGoogleFormat("A<int*, int*> a;"); 9367 verifyGoogleFormat("A<int**, int**> a;"); 9368 verifyGoogleFormat("f(b ? *c : *d);"); 9369 verifyGoogleFormat("int a = b ? *c : *d;"); 9370 verifyGoogleFormat("Type* t = **x;"); 9371 verifyGoogleFormat("Type* t = *++*x;"); 9372 verifyGoogleFormat("*++*x;"); 9373 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 9374 verifyGoogleFormat("Type* t = x++ * y;"); 9375 verifyGoogleFormat( 9376 "const char* const p = reinterpret_cast<const char* const>(q);"); 9377 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 9378 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 9379 verifyGoogleFormat("template <typename T>\n" 9380 "void f(int i = 0, SomeType** temps = NULL);"); 9381 9382 FormatStyle Left = getLLVMStyle(); 9383 Left.PointerAlignment = FormatStyle::PAS_Left; 9384 verifyFormat("x = *a(x) = *a(y);", Left); 9385 verifyFormat("for (;; *a = b) {\n}", Left); 9386 verifyFormat("return *this += 1;", Left); 9387 verifyFormat("throw *x;", Left); 9388 verifyFormat("delete *x;", Left); 9389 verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left); 9390 verifyFormat("[](const decltype(*a)* ptr) {}", Left); 9391 verifyFormat("[](const typeof(*a)* ptr) {}", Left); 9392 verifyFormat("[](const _Atomic(a*)* ptr) {}", Left); 9393 verifyFormat("[](const __underlying_type(a)* ptr) {}", Left); 9394 verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left); 9395 verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left); 9396 verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left); 9397 verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left); 9398 9399 verifyIndependentOfContext("a = *(x + y);"); 9400 verifyIndependentOfContext("a = &(x + y);"); 9401 verifyIndependentOfContext("*(x + y).call();"); 9402 verifyIndependentOfContext("&(x + y)->call();"); 9403 verifyFormat("void f() { &(*I).first; }"); 9404 9405 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 9406 verifyFormat( 9407 "int *MyValues = {\n" 9408 " *A, // Operator detection might be confused by the '{'\n" 9409 " *BB // Operator detection might be confused by previous comment\n" 9410 "};"); 9411 9412 verifyIndependentOfContext("if (int *a = &b)"); 9413 verifyIndependentOfContext("if (int &a = *b)"); 9414 verifyIndependentOfContext("if (a & b[i])"); 9415 verifyIndependentOfContext("if constexpr (a & b[i])"); 9416 verifyIndependentOfContext("if CONSTEXPR (a & b[i])"); 9417 verifyIndependentOfContext("if (a * (b * c))"); 9418 verifyIndependentOfContext("if constexpr (a * (b * c))"); 9419 verifyIndependentOfContext("if CONSTEXPR (a * (b * c))"); 9420 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 9421 verifyIndependentOfContext("if (*b[i])"); 9422 verifyIndependentOfContext("if (int *a = (&b))"); 9423 verifyIndependentOfContext("while (int *a = &b)"); 9424 verifyIndependentOfContext("while (a * (b * c))"); 9425 verifyIndependentOfContext("size = sizeof *a;"); 9426 verifyIndependentOfContext("if (a && (b = c))"); 9427 verifyFormat("void f() {\n" 9428 " for (const int &v : Values) {\n" 9429 " }\n" 9430 "}"); 9431 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 9432 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 9433 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 9434 9435 verifyFormat("#define A (!a * b)"); 9436 verifyFormat("#define MACRO \\\n" 9437 " int *i = a * b; \\\n" 9438 " void f(a *b);", 9439 getLLVMStyleWithColumns(19)); 9440 9441 verifyIndependentOfContext("A = new SomeType *[Length];"); 9442 verifyIndependentOfContext("A = new SomeType *[Length]();"); 9443 verifyIndependentOfContext("T **t = new T *;"); 9444 verifyIndependentOfContext("T **t = new T *();"); 9445 verifyGoogleFormat("A = new SomeType*[Length]();"); 9446 verifyGoogleFormat("A = new SomeType*[Length];"); 9447 verifyGoogleFormat("T** t = new T*;"); 9448 verifyGoogleFormat("T** t = new T*();"); 9449 9450 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 9451 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 9452 verifyFormat("template <bool a, bool b> " 9453 "typename t::if<x && y>::type f() {}"); 9454 verifyFormat("template <int *y> f() {}"); 9455 verifyFormat("vector<int *> v;"); 9456 verifyFormat("vector<int *const> v;"); 9457 verifyFormat("vector<int *const **const *> v;"); 9458 verifyFormat("vector<int *volatile> v;"); 9459 verifyFormat("vector<a *_Nonnull> v;"); 9460 verifyFormat("vector<a *_Nullable> v;"); 9461 verifyFormat("vector<a *_Null_unspecified> v;"); 9462 verifyFormat("vector<a *__ptr32> v;"); 9463 verifyFormat("vector<a *__ptr64> v;"); 9464 verifyFormat("vector<a *__capability> v;"); 9465 FormatStyle TypeMacros = getLLVMStyle(); 9466 TypeMacros.TypenameMacros = {"LIST"}; 9467 verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros); 9468 verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros); 9469 verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros); 9470 verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros); 9471 verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros); // multiplication 9472 9473 FormatStyle CustomQualifier = getLLVMStyle(); 9474 // Add identifiers that should not be parsed as a qualifier by default. 9475 CustomQualifier.AttributeMacros.push_back("__my_qualifier"); 9476 CustomQualifier.AttributeMacros.push_back("_My_qualifier"); 9477 CustomQualifier.AttributeMacros.push_back("my_other_qualifier"); 9478 verifyFormat("vector<a * __my_qualifier> parse_as_multiply;"); 9479 verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier); 9480 verifyFormat("vector<a * _My_qualifier> parse_as_multiply;"); 9481 verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier); 9482 verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;"); 9483 verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier); 9484 verifyFormat("vector<a * _NotAQualifier> v;"); 9485 verifyFormat("vector<a * __not_a_qualifier> v;"); 9486 verifyFormat("vector<a * b> v;"); 9487 verifyFormat("foo<b && false>();"); 9488 verifyFormat("foo<b & 1>();"); 9489 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 9490 verifyFormat("typeof(*::std::declval<const T &>()) void F();"); 9491 verifyFormat("_Atomic(*::std::declval<const T &>()) void F();"); 9492 verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();"); 9493 verifyFormat( 9494 "template <class T, class = typename std::enable_if<\n" 9495 " std::is_integral<T>::value &&\n" 9496 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 9497 "void F();", 9498 getLLVMStyleWithColumns(70)); 9499 verifyFormat("template <class T,\n" 9500 " class = typename std::enable_if<\n" 9501 " std::is_integral<T>::value &&\n" 9502 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n" 9503 " class U>\n" 9504 "void F();", 9505 getLLVMStyleWithColumns(70)); 9506 verifyFormat( 9507 "template <class T,\n" 9508 " class = typename ::std::enable_if<\n" 9509 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 9510 "void F();", 9511 getGoogleStyleWithColumns(68)); 9512 9513 verifyIndependentOfContext("MACRO(int *i);"); 9514 verifyIndependentOfContext("MACRO(auto *a);"); 9515 verifyIndependentOfContext("MACRO(const A *a);"); 9516 verifyIndependentOfContext("MACRO(_Atomic(A) *a);"); 9517 verifyIndependentOfContext("MACRO(decltype(A) *a);"); 9518 verifyIndependentOfContext("MACRO(typeof(A) *a);"); 9519 verifyIndependentOfContext("MACRO(__underlying_type(A) *a);"); 9520 verifyIndependentOfContext("MACRO(A *const a);"); 9521 verifyIndependentOfContext("MACRO(A *restrict a);"); 9522 verifyIndependentOfContext("MACRO(A *__restrict__ a);"); 9523 verifyIndependentOfContext("MACRO(A *__restrict a);"); 9524 verifyIndependentOfContext("MACRO(A *volatile a);"); 9525 verifyIndependentOfContext("MACRO(A *__volatile a);"); 9526 verifyIndependentOfContext("MACRO(A *__volatile__ a);"); 9527 verifyIndependentOfContext("MACRO(A *_Nonnull a);"); 9528 verifyIndependentOfContext("MACRO(A *_Nullable a);"); 9529 verifyIndependentOfContext("MACRO(A *_Null_unspecified a);"); 9530 verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);"); 9531 verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);"); 9532 verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);"); 9533 verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);"); 9534 verifyIndependentOfContext("MACRO(A *__ptr32 a);"); 9535 verifyIndependentOfContext("MACRO(A *__ptr64 a);"); 9536 verifyIndependentOfContext("MACRO(A *__capability);"); 9537 verifyIndependentOfContext("MACRO(A &__capability);"); 9538 verifyFormat("MACRO(A *__my_qualifier);"); // type declaration 9539 verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication 9540 // If we add __my_qualifier to AttributeMacros it should always be parsed as 9541 // a type declaration: 9542 verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier); 9543 verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier); 9544 // Also check that TypenameMacros prevents parsing it as multiplication: 9545 verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication 9546 verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros); // type 9547 9548 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 9549 verifyFormat("void f() { f(float{1}, a * a); }"); 9550 verifyFormat("void f() { f(float(1), a * a); }"); 9551 9552 verifyFormat("f((void (*)(int))g);"); 9553 verifyFormat("f((void (&)(int))g);"); 9554 verifyFormat("f((void (^)(int))g);"); 9555 9556 // FIXME: Is there a way to make this work? 9557 // verifyIndependentOfContext("MACRO(A *a);"); 9558 verifyFormat("MACRO(A &B);"); 9559 verifyFormat("MACRO(A *B);"); 9560 verifyFormat("void f() { MACRO(A * B); }"); 9561 verifyFormat("void f() { MACRO(A & B); }"); 9562 9563 // This lambda was mis-formatted after D88956 (treating it as a binop): 9564 verifyFormat("auto x = [](const decltype(x) &ptr) {};"); 9565 verifyFormat("auto x = [](const decltype(x) *ptr) {};"); 9566 verifyFormat("#define lambda [](const decltype(x) &ptr) {}"); 9567 verifyFormat("#define lambda [](const decltype(x) *ptr) {}"); 9568 9569 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 9570 verifyFormat("return options != nullptr && operator==(*options);"); 9571 9572 EXPECT_EQ("#define OP(x) \\\n" 9573 " ostream &operator<<(ostream &s, const A &a) { \\\n" 9574 " return s << a.DebugString(); \\\n" 9575 " }", 9576 format("#define OP(x) \\\n" 9577 " ostream &operator<<(ostream &s, const A &a) { \\\n" 9578 " return s << a.DebugString(); \\\n" 9579 " }", 9580 getLLVMStyleWithColumns(50))); 9581 9582 // FIXME: We cannot handle this case yet; we might be able to figure out that 9583 // foo<x> d > v; doesn't make sense. 9584 verifyFormat("foo<a<b && c> d> v;"); 9585 9586 FormatStyle PointerMiddle = getLLVMStyle(); 9587 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 9588 verifyFormat("delete *x;", PointerMiddle); 9589 verifyFormat("int * x;", PointerMiddle); 9590 verifyFormat("int *[] x;", PointerMiddle); 9591 verifyFormat("template <int * y> f() {}", PointerMiddle); 9592 verifyFormat("int * f(int * a) {}", PointerMiddle); 9593 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 9594 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 9595 verifyFormat("A<int *> a;", PointerMiddle); 9596 verifyFormat("A<int **> a;", PointerMiddle); 9597 verifyFormat("A<int *, int *> a;", PointerMiddle); 9598 verifyFormat("A<int *[]> a;", PointerMiddle); 9599 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 9600 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 9601 verifyFormat("T ** t = new T *;", PointerMiddle); 9602 9603 // Member function reference qualifiers aren't binary operators. 9604 verifyFormat("string // break\n" 9605 "operator()() & {}"); 9606 verifyFormat("string // break\n" 9607 "operator()() && {}"); 9608 verifyGoogleFormat("template <typename T>\n" 9609 "auto x() & -> int {}"); 9610 9611 // Should be binary operators when used as an argument expression (overloaded 9612 // operator invoked as a member function). 9613 verifyFormat("void f() { a.operator()(a * a); }"); 9614 verifyFormat("void f() { a->operator()(a & a); }"); 9615 verifyFormat("void f() { a.operator()(*a & *a); }"); 9616 verifyFormat("void f() { a->operator()(*a * *a); }"); 9617 } 9618 9619 TEST_F(FormatTest, UnderstandsAttributes) { 9620 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 9621 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 9622 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 9623 FormatStyle AfterType = getLLVMStyle(); 9624 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 9625 verifyFormat("__attribute__((nodebug)) void\n" 9626 "foo() {}\n", 9627 AfterType); 9628 verifyFormat("__unused void\n" 9629 "foo() {}", 9630 AfterType); 9631 9632 FormatStyle CustomAttrs = getLLVMStyle(); 9633 CustomAttrs.AttributeMacros.push_back("__unused"); 9634 CustomAttrs.AttributeMacros.push_back("__attr1"); 9635 CustomAttrs.AttributeMacros.push_back("__attr2"); 9636 CustomAttrs.AttributeMacros.push_back("no_underscore_attr"); 9637 verifyFormat("vector<SomeType *__attribute((foo))> v;"); 9638 verifyFormat("vector<SomeType *__attribute__((foo))> v;"); 9639 verifyFormat("vector<SomeType * __not_attribute__((foo))> v;"); 9640 // Check that it is parsed as a multiplication without AttributeMacros and 9641 // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros. 9642 verifyFormat("vector<SomeType * __attr1> v;"); 9643 verifyFormat("vector<SomeType __attr1 *> v;"); 9644 verifyFormat("vector<SomeType __attr1 *const> v;"); 9645 verifyFormat("vector<SomeType __attr1 * __attr2> v;"); 9646 verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs); 9647 verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs); 9648 verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs); 9649 verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs); 9650 verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs); 9651 verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs); 9652 verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs); 9653 9654 // Check that these are not parsed as function declarations: 9655 CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 9656 CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman; 9657 verifyFormat("SomeType s(InitValue);", CustomAttrs); 9658 verifyFormat("SomeType s{InitValue};", CustomAttrs); 9659 verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs); 9660 verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs); 9661 verifyFormat("SomeType s __unused(InitValue);", CustomAttrs); 9662 verifyFormat("SomeType s __unused{InitValue};", CustomAttrs); 9663 verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs); 9664 verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs); 9665 } 9666 9667 TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) { 9668 // Check that qualifiers on pointers don't break parsing of casts. 9669 verifyFormat("x = (foo *const)*v;"); 9670 verifyFormat("x = (foo *volatile)*v;"); 9671 verifyFormat("x = (foo *restrict)*v;"); 9672 verifyFormat("x = (foo *__attribute__((foo)))*v;"); 9673 verifyFormat("x = (foo *_Nonnull)*v;"); 9674 verifyFormat("x = (foo *_Nullable)*v;"); 9675 verifyFormat("x = (foo *_Null_unspecified)*v;"); 9676 verifyFormat("x = (foo *_Nonnull)*v;"); 9677 verifyFormat("x = (foo *[[clang::attr]])*v;"); 9678 verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;"); 9679 verifyFormat("x = (foo *__ptr32)*v;"); 9680 verifyFormat("x = (foo *__ptr64)*v;"); 9681 verifyFormat("x = (foo *__capability)*v;"); 9682 9683 // Check that we handle multiple trailing qualifiers and skip them all to 9684 // determine that the expression is a cast to a pointer type. 9685 FormatStyle LongPointerRight = getLLVMStyleWithColumns(999); 9686 FormatStyle LongPointerLeft = getLLVMStyleWithColumns(999); 9687 LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left; 9688 StringRef AllQualifiers = 9689 "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified " 9690 "_Nonnull [[clang::attr]] __ptr32 __ptr64 __capability"; 9691 verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight); 9692 verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft); 9693 9694 // Also check that address-of is not parsed as a binary bitwise-and: 9695 verifyFormat("x = (foo *const)&v;"); 9696 verifyFormat(("x = (foo *" + AllQualifiers + ")&v;").str(), LongPointerRight); 9697 verifyFormat(("x = (foo* " + AllQualifiers + ")&v;").str(), LongPointerLeft); 9698 9699 // Check custom qualifiers: 9700 FormatStyle CustomQualifier = getLLVMStyleWithColumns(999); 9701 CustomQualifier.AttributeMacros.push_back("__my_qualifier"); 9702 verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier. 9703 verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier); 9704 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)*v;").str(), 9705 CustomQualifier); 9706 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)&v;").str(), 9707 CustomQualifier); 9708 9709 // Check that unknown identifiers result in binary operator parsing: 9710 verifyFormat("x = (foo * __unknown_qualifier) * v;"); 9711 verifyFormat("x = (foo * __unknown_qualifier) & v;"); 9712 } 9713 9714 TEST_F(FormatTest, UnderstandsSquareAttributes) { 9715 verifyFormat("SomeType s [[unused]] (InitValue);"); 9716 verifyFormat("SomeType s [[gnu::unused]] (InitValue);"); 9717 verifyFormat("SomeType s [[using gnu: unused]] (InitValue);"); 9718 verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}"); 9719 verifyFormat("void f() [[deprecated(\"so sorry\")]];"); 9720 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9721 " [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 9722 verifyFormat("[[nodiscard]] bool f() { return false; }"); 9723 verifyFormat("class [[nodiscard]] f {\npublic:\n f() {}\n}"); 9724 verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n f() {}\n}"); 9725 verifyFormat("class [[gnu::unused]] f {\npublic:\n f() {}\n}"); 9726 9727 // Make sure we do not mistake attributes for array subscripts. 9728 verifyFormat("int a() {}\n" 9729 "[[unused]] int b() {}\n"); 9730 verifyFormat("NSArray *arr;\n" 9731 "arr[[Foo() bar]];"); 9732 9733 // On the other hand, we still need to correctly find array subscripts. 9734 verifyFormat("int a = std::vector<int>{1, 2, 3}[0];"); 9735 9736 // Make sure that we do not mistake Objective-C method inside array literals 9737 // as attributes, even if those method names are also keywords. 9738 verifyFormat("@[ [foo bar] ];"); 9739 verifyFormat("@[ [NSArray class] ];"); 9740 verifyFormat("@[ [foo enum] ];"); 9741 9742 verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }"); 9743 9744 // Make sure we do not parse attributes as lambda introducers. 9745 FormatStyle MultiLineFunctions = getLLVMStyle(); 9746 MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 9747 verifyFormat("[[unused]] int b() {\n" 9748 " return 42;\n" 9749 "}\n", 9750 MultiLineFunctions); 9751 } 9752 9753 TEST_F(FormatTest, AttributeClass) { 9754 FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp); 9755 verifyFormat("class S {\n" 9756 " S(S&&) = default;\n" 9757 "};", 9758 Style); 9759 verifyFormat("class [[nodiscard]] S {\n" 9760 " S(S&&) = default;\n" 9761 "};", 9762 Style); 9763 verifyFormat("class __attribute((maybeunused)) S {\n" 9764 " S(S&&) = default;\n" 9765 "};", 9766 Style); 9767 verifyFormat("struct S {\n" 9768 " S(S&&) = default;\n" 9769 "};", 9770 Style); 9771 verifyFormat("struct [[nodiscard]] S {\n" 9772 " S(S&&) = default;\n" 9773 "};", 9774 Style); 9775 } 9776 9777 TEST_F(FormatTest, AttributesAfterMacro) { 9778 FormatStyle Style = getLLVMStyle(); 9779 verifyFormat("MACRO;\n" 9780 "__attribute__((maybe_unused)) int foo() {\n" 9781 " //...\n" 9782 "}"); 9783 9784 verifyFormat("MACRO;\n" 9785 "[[nodiscard]] int foo() {\n" 9786 " //...\n" 9787 "}"); 9788 9789 EXPECT_EQ("MACRO\n\n" 9790 "__attribute__((maybe_unused)) int foo() {\n" 9791 " //...\n" 9792 "}", 9793 format("MACRO\n\n" 9794 "__attribute__((maybe_unused)) int foo() {\n" 9795 " //...\n" 9796 "}")); 9797 9798 EXPECT_EQ("MACRO\n\n" 9799 "[[nodiscard]] int foo() {\n" 9800 " //...\n" 9801 "}", 9802 format("MACRO\n\n" 9803 "[[nodiscard]] int foo() {\n" 9804 " //...\n" 9805 "}")); 9806 } 9807 9808 TEST_F(FormatTest, AttributePenaltyBreaking) { 9809 FormatStyle Style = getLLVMStyle(); 9810 verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n" 9811 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", 9812 Style); 9813 verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n" 9814 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", 9815 Style); 9816 verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const " 9817 "shared_ptr<ALongTypeName> &C d) {\n}", 9818 Style); 9819 } 9820 9821 TEST_F(FormatTest, UnderstandsEllipsis) { 9822 FormatStyle Style = getLLVMStyle(); 9823 verifyFormat("int printf(const char *fmt, ...);"); 9824 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 9825 verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}"); 9826 9827 verifyFormat("template <int *...PP> a;", Style); 9828 9829 Style.PointerAlignment = FormatStyle::PAS_Left; 9830 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style); 9831 9832 verifyFormat("template <int*... PP> a;", Style); 9833 9834 Style.PointerAlignment = FormatStyle::PAS_Middle; 9835 verifyFormat("template <int *... PP> a;", Style); 9836 } 9837 9838 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 9839 EXPECT_EQ("int *a;\n" 9840 "int *a;\n" 9841 "int *a;", 9842 format("int *a;\n" 9843 "int* a;\n" 9844 "int *a;", 9845 getGoogleStyle())); 9846 EXPECT_EQ("int* a;\n" 9847 "int* a;\n" 9848 "int* a;", 9849 format("int* a;\n" 9850 "int* a;\n" 9851 "int *a;", 9852 getGoogleStyle())); 9853 EXPECT_EQ("int *a;\n" 9854 "int *a;\n" 9855 "int *a;", 9856 format("int *a;\n" 9857 "int * a;\n" 9858 "int * a;", 9859 getGoogleStyle())); 9860 EXPECT_EQ("auto x = [] {\n" 9861 " int *a;\n" 9862 " int *a;\n" 9863 " int *a;\n" 9864 "};", 9865 format("auto x=[]{int *a;\n" 9866 "int * a;\n" 9867 "int * a;};", 9868 getGoogleStyle())); 9869 } 9870 9871 TEST_F(FormatTest, UnderstandsRvalueReferences) { 9872 verifyFormat("int f(int &&a) {}"); 9873 verifyFormat("int f(int a, char &&b) {}"); 9874 verifyFormat("void f() { int &&a = b; }"); 9875 verifyGoogleFormat("int f(int a, char&& b) {}"); 9876 verifyGoogleFormat("void f() { int&& a = b; }"); 9877 9878 verifyIndependentOfContext("A<int &&> a;"); 9879 verifyIndependentOfContext("A<int &&, int &&> a;"); 9880 verifyGoogleFormat("A<int&&> a;"); 9881 verifyGoogleFormat("A<int&&, int&&> a;"); 9882 9883 // Not rvalue references: 9884 verifyFormat("template <bool B, bool C> class A {\n" 9885 " static_assert(B && C, \"Something is wrong\");\n" 9886 "};"); 9887 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 9888 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 9889 verifyFormat("#define A(a, b) (a && b)"); 9890 } 9891 9892 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 9893 verifyFormat("void f() {\n" 9894 " x[aaaaaaaaa -\n" 9895 " b] = 23;\n" 9896 "}", 9897 getLLVMStyleWithColumns(15)); 9898 } 9899 9900 TEST_F(FormatTest, FormatsCasts) { 9901 verifyFormat("Type *A = static_cast<Type *>(P);"); 9902 verifyFormat("Type *A = (Type *)P;"); 9903 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 9904 verifyFormat("int a = (int)(2.0f);"); 9905 verifyFormat("int a = (int)2.0f;"); 9906 verifyFormat("x[(int32)y];"); 9907 verifyFormat("x = (int32)y;"); 9908 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 9909 verifyFormat("int a = (int)*b;"); 9910 verifyFormat("int a = (int)2.0f;"); 9911 verifyFormat("int a = (int)~0;"); 9912 verifyFormat("int a = (int)++a;"); 9913 verifyFormat("int a = (int)sizeof(int);"); 9914 verifyFormat("int a = (int)+2;"); 9915 verifyFormat("my_int a = (my_int)2.0f;"); 9916 verifyFormat("my_int a = (my_int)sizeof(int);"); 9917 verifyFormat("return (my_int)aaa;"); 9918 verifyFormat("#define x ((int)-1)"); 9919 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 9920 verifyFormat("#define p(q) ((int *)&q)"); 9921 verifyFormat("fn(a)(b) + 1;"); 9922 9923 verifyFormat("void f() { my_int a = (my_int)*b; }"); 9924 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 9925 verifyFormat("my_int a = (my_int)~0;"); 9926 verifyFormat("my_int a = (my_int)++a;"); 9927 verifyFormat("my_int a = (my_int)-2;"); 9928 verifyFormat("my_int a = (my_int)1;"); 9929 verifyFormat("my_int a = (my_int *)1;"); 9930 verifyFormat("my_int a = (const my_int)-1;"); 9931 verifyFormat("my_int a = (const my_int *)-1;"); 9932 verifyFormat("my_int a = (my_int)(my_int)-1;"); 9933 verifyFormat("my_int a = (ns::my_int)-2;"); 9934 verifyFormat("case (my_int)ONE:"); 9935 verifyFormat("auto x = (X)this;"); 9936 // Casts in Obj-C style calls used to not be recognized as such. 9937 verifyFormat("int a = [(type*)[((type*)val) arg] arg];", getGoogleStyle()); 9938 9939 // FIXME: single value wrapped with paren will be treated as cast. 9940 verifyFormat("void f(int i = (kValue)*kMask) {}"); 9941 9942 verifyFormat("{ (void)F; }"); 9943 9944 // Don't break after a cast's 9945 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 9946 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 9947 " bbbbbbbbbbbbbbbbbbbbbb);"); 9948 9949 // These are not casts. 9950 verifyFormat("void f(int *) {}"); 9951 verifyFormat("f(foo)->b;"); 9952 verifyFormat("f(foo).b;"); 9953 verifyFormat("f(foo)(b);"); 9954 verifyFormat("f(foo)[b];"); 9955 verifyFormat("[](foo) { return 4; }(bar);"); 9956 verifyFormat("(*funptr)(foo)[4];"); 9957 verifyFormat("funptrs[4](foo)[4];"); 9958 verifyFormat("void f(int *);"); 9959 verifyFormat("void f(int *) = 0;"); 9960 verifyFormat("void f(SmallVector<int>) {}"); 9961 verifyFormat("void f(SmallVector<int>);"); 9962 verifyFormat("void f(SmallVector<int>) = 0;"); 9963 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 9964 verifyFormat("int a = sizeof(int) * b;"); 9965 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 9966 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 9967 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 9968 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 9969 9970 // These are not casts, but at some point were confused with casts. 9971 verifyFormat("virtual void foo(int *) override;"); 9972 verifyFormat("virtual void foo(char &) const;"); 9973 verifyFormat("virtual void foo(int *a, char *) const;"); 9974 verifyFormat("int a = sizeof(int *) + b;"); 9975 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 9976 verifyFormat("bool b = f(g<int>) && c;"); 9977 verifyFormat("typedef void (*f)(int i) func;"); 9978 verifyFormat("void operator++(int) noexcept;"); 9979 verifyFormat("void operator++(int &) noexcept;"); 9980 verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t " 9981 "&) noexcept;"); 9982 verifyFormat( 9983 "void operator delete(std::size_t, const std::nothrow_t &) noexcept;"); 9984 verifyFormat("void operator delete(const std::nothrow_t &) noexcept;"); 9985 verifyFormat("void operator delete(std::nothrow_t &) noexcept;"); 9986 verifyFormat("void operator delete(nothrow_t &) noexcept;"); 9987 verifyFormat("void operator delete(foo &) noexcept;"); 9988 verifyFormat("void operator delete(foo) noexcept;"); 9989 verifyFormat("void operator delete(int) noexcept;"); 9990 verifyFormat("void operator delete(int &) noexcept;"); 9991 verifyFormat("void operator delete(int &) volatile noexcept;"); 9992 verifyFormat("void operator delete(int &) const"); 9993 verifyFormat("void operator delete(int &) = default"); 9994 verifyFormat("void operator delete(int &) = delete"); 9995 verifyFormat("void operator delete(int &) [[noreturn]]"); 9996 verifyFormat("void operator delete(int &) throw();"); 9997 verifyFormat("void operator delete(int &) throw(int);"); 9998 verifyFormat("auto operator delete(int &) -> int;"); 9999 verifyFormat("auto operator delete(int &) override"); 10000 verifyFormat("auto operator delete(int &) final"); 10001 10002 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 10003 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 10004 // FIXME: The indentation here is not ideal. 10005 verifyFormat( 10006 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10007 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 10008 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 10009 } 10010 10011 TEST_F(FormatTest, FormatsFunctionTypes) { 10012 verifyFormat("A<bool()> a;"); 10013 verifyFormat("A<SomeType()> a;"); 10014 verifyFormat("A<void (*)(int, std::string)> a;"); 10015 verifyFormat("A<void *(int)>;"); 10016 verifyFormat("void *(*a)(int *, SomeType *);"); 10017 verifyFormat("int (*func)(void *);"); 10018 verifyFormat("void f() { int (*func)(void *); }"); 10019 verifyFormat("template <class CallbackClass>\n" 10020 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 10021 10022 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 10023 verifyGoogleFormat("void* (*a)(int);"); 10024 verifyGoogleFormat( 10025 "template <class CallbackClass>\n" 10026 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 10027 10028 // Other constructs can look somewhat like function types: 10029 verifyFormat("A<sizeof(*x)> a;"); 10030 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 10031 verifyFormat("some_var = function(*some_pointer_var)[0];"); 10032 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 10033 verifyFormat("int x = f(&h)();"); 10034 verifyFormat("returnsFunction(¶m1, ¶m2)(param);"); 10035 verifyFormat("std::function<\n" 10036 " LooooooooooongTemplatedType<\n" 10037 " SomeType>*(\n" 10038 " LooooooooooooooooongType type)>\n" 10039 " function;", 10040 getGoogleStyleWithColumns(40)); 10041 } 10042 10043 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 10044 verifyFormat("A (*foo_)[6];"); 10045 verifyFormat("vector<int> (*foo_)[6];"); 10046 } 10047 10048 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 10049 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10050 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 10051 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 10052 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 10053 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10054 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 10055 10056 // Different ways of ()-initializiation. 10057 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10058 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 10059 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10060 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 10061 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10062 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 10063 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10064 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 10065 10066 // Lambdas should not confuse the variable declaration heuristic. 10067 verifyFormat("LooooooooooooooooongType\n" 10068 " variable(nullptr, [](A *a) {});", 10069 getLLVMStyleWithColumns(40)); 10070 } 10071 10072 TEST_F(FormatTest, BreaksLongDeclarations) { 10073 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 10074 " AnotherNameForTheLongType;"); 10075 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 10076 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 10077 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10078 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 10079 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 10080 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 10081 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10082 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10083 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 10084 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10085 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 10086 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10087 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 10088 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10089 verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n" 10090 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10091 verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n" 10092 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10093 verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n" 10094 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10095 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10096 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);"); 10097 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10098 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}"); 10099 FormatStyle Indented = getLLVMStyle(); 10100 Indented.IndentWrappedFunctionNames = true; 10101 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10102 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 10103 Indented); 10104 verifyFormat( 10105 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10106 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 10107 Indented); 10108 verifyFormat( 10109 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 10110 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 10111 Indented); 10112 verifyFormat( 10113 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 10114 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 10115 Indented); 10116 10117 // FIXME: Without the comment, this breaks after "(". 10118 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 10119 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 10120 getGoogleStyle()); 10121 10122 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 10123 " int LoooooooooooooooooooongParam2) {}"); 10124 verifyFormat( 10125 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 10126 " SourceLocation L, IdentifierIn *II,\n" 10127 " Type *T) {}"); 10128 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 10129 "ReallyReaaallyLongFunctionName(\n" 10130 " const std::string &SomeParameter,\n" 10131 " const SomeType<string, SomeOtherTemplateParameter>\n" 10132 " &ReallyReallyLongParameterName,\n" 10133 " const SomeType<string, SomeOtherTemplateParameter>\n" 10134 " &AnotherLongParameterName) {}"); 10135 verifyFormat("template <typename A>\n" 10136 "SomeLoooooooooooooooooooooongType<\n" 10137 " typename some_namespace::SomeOtherType<A>::Type>\n" 10138 "Function() {}"); 10139 10140 verifyGoogleFormat( 10141 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 10142 " aaaaaaaaaaaaaaaaaaaaaaa;"); 10143 verifyGoogleFormat( 10144 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 10145 " SourceLocation L) {}"); 10146 verifyGoogleFormat( 10147 "some_namespace::LongReturnType\n" 10148 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 10149 " int first_long_parameter, int second_parameter) {}"); 10150 10151 verifyGoogleFormat("template <typename T>\n" 10152 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 10153 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 10154 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 10155 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 10156 10157 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 10158 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10159 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 10160 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10161 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 10162 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 10163 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10164 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 10165 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 10166 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 10167 10168 verifyFormat("template <typename T> // Templates on own line.\n" 10169 "static int // Some comment.\n" 10170 "MyFunction(int a);", 10171 getLLVMStyle()); 10172 } 10173 10174 TEST_F(FormatTest, FormatsAccessModifiers) { 10175 FormatStyle Style = getLLVMStyle(); 10176 EXPECT_EQ(Style.EmptyLineBeforeAccessModifier, 10177 FormatStyle::ELBAMS_LogicalBlock); 10178 verifyFormat("struct foo {\n" 10179 "private:\n" 10180 " void f() {}\n" 10181 "\n" 10182 "private:\n" 10183 " int i;\n" 10184 "\n" 10185 "protected:\n" 10186 " int j;\n" 10187 "};\n", 10188 Style); 10189 verifyFormat("struct foo {\n" 10190 "private:\n" 10191 " void f() {}\n" 10192 "\n" 10193 "private:\n" 10194 " int i;\n" 10195 "\n" 10196 "protected:\n" 10197 " int j;\n" 10198 "};\n", 10199 "struct foo {\n" 10200 "private:\n" 10201 " void f() {}\n" 10202 "private:\n" 10203 " int i;\n" 10204 "protected:\n" 10205 " int j;\n" 10206 "};\n", 10207 Style); 10208 verifyFormat("struct foo { /* comment */\n" 10209 "private:\n" 10210 " int i;\n" 10211 " // comment\n" 10212 "private:\n" 10213 " int j;\n" 10214 "};\n", 10215 Style); 10216 verifyFormat("struct foo {\n" 10217 "#ifdef FOO\n" 10218 "#endif\n" 10219 "private:\n" 10220 " int i;\n" 10221 "#ifdef FOO\n" 10222 "private:\n" 10223 "#endif\n" 10224 " int j;\n" 10225 "};\n", 10226 Style); 10227 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10228 verifyFormat("struct foo {\n" 10229 "private:\n" 10230 " void f() {}\n" 10231 "private:\n" 10232 " int i;\n" 10233 "protected:\n" 10234 " int j;\n" 10235 "};\n", 10236 Style); 10237 verifyFormat("struct foo {\n" 10238 "private:\n" 10239 " void f() {}\n" 10240 "private:\n" 10241 " int i;\n" 10242 "protected:\n" 10243 " int j;\n" 10244 "};\n", 10245 "struct foo {\n" 10246 "\n" 10247 "private:\n" 10248 " void f() {}\n" 10249 "\n" 10250 "private:\n" 10251 " int i;\n" 10252 "\n" 10253 "protected:\n" 10254 " int j;\n" 10255 "};\n", 10256 Style); 10257 verifyFormat("struct foo { /* comment */\n" 10258 "private:\n" 10259 " int i;\n" 10260 " // comment\n" 10261 "private:\n" 10262 " int j;\n" 10263 "};\n", 10264 "struct foo { /* comment */\n" 10265 "\n" 10266 "private:\n" 10267 " int i;\n" 10268 " // comment\n" 10269 "\n" 10270 "private:\n" 10271 " int j;\n" 10272 "};\n", 10273 Style); 10274 verifyFormat("struct foo {\n" 10275 "#ifdef FOO\n" 10276 "#endif\n" 10277 "private:\n" 10278 " int i;\n" 10279 "#ifdef FOO\n" 10280 "private:\n" 10281 "#endif\n" 10282 " int j;\n" 10283 "};\n", 10284 "struct foo {\n" 10285 "#ifdef FOO\n" 10286 "#endif\n" 10287 "\n" 10288 "private:\n" 10289 " int i;\n" 10290 "#ifdef FOO\n" 10291 "\n" 10292 "private:\n" 10293 "#endif\n" 10294 " int j;\n" 10295 "};\n", 10296 Style); 10297 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10298 verifyFormat("struct foo {\n" 10299 "private:\n" 10300 " void f() {}\n" 10301 "\n" 10302 "private:\n" 10303 " int i;\n" 10304 "\n" 10305 "protected:\n" 10306 " int j;\n" 10307 "};\n", 10308 Style); 10309 verifyFormat("struct foo {\n" 10310 "private:\n" 10311 " void f() {}\n" 10312 "\n" 10313 "private:\n" 10314 " int i;\n" 10315 "\n" 10316 "protected:\n" 10317 " int j;\n" 10318 "};\n", 10319 "struct foo {\n" 10320 "private:\n" 10321 " void f() {}\n" 10322 "private:\n" 10323 " int i;\n" 10324 "protected:\n" 10325 " int j;\n" 10326 "};\n", 10327 Style); 10328 verifyFormat("struct foo { /* comment */\n" 10329 "private:\n" 10330 " int i;\n" 10331 " // comment\n" 10332 "\n" 10333 "private:\n" 10334 " int j;\n" 10335 "};\n", 10336 "struct foo { /* comment */\n" 10337 "private:\n" 10338 " int i;\n" 10339 " // comment\n" 10340 "\n" 10341 "private:\n" 10342 " int j;\n" 10343 "};\n", 10344 Style); 10345 verifyFormat("struct foo {\n" 10346 "#ifdef FOO\n" 10347 "#endif\n" 10348 "\n" 10349 "private:\n" 10350 " int i;\n" 10351 "#ifdef FOO\n" 10352 "\n" 10353 "private:\n" 10354 "#endif\n" 10355 " int j;\n" 10356 "};\n", 10357 "struct foo {\n" 10358 "#ifdef FOO\n" 10359 "#endif\n" 10360 "private:\n" 10361 " int i;\n" 10362 "#ifdef FOO\n" 10363 "private:\n" 10364 "#endif\n" 10365 " int j;\n" 10366 "};\n", 10367 Style); 10368 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 10369 EXPECT_EQ("struct foo {\n" 10370 "\n" 10371 "private:\n" 10372 " void f() {}\n" 10373 "\n" 10374 "private:\n" 10375 " int i;\n" 10376 "\n" 10377 "protected:\n" 10378 " int j;\n" 10379 "};\n", 10380 format("struct foo {\n" 10381 "\n" 10382 "private:\n" 10383 " void f() {}\n" 10384 "\n" 10385 "private:\n" 10386 " int i;\n" 10387 "\n" 10388 "protected:\n" 10389 " int j;\n" 10390 "};\n", 10391 Style)); 10392 verifyFormat("struct foo {\n" 10393 "private:\n" 10394 " void f() {}\n" 10395 "private:\n" 10396 " int i;\n" 10397 "protected:\n" 10398 " int j;\n" 10399 "};\n", 10400 Style); 10401 EXPECT_EQ("struct foo { /* comment */\n" 10402 "\n" 10403 "private:\n" 10404 " int i;\n" 10405 " // comment\n" 10406 "\n" 10407 "private:\n" 10408 " int j;\n" 10409 "};\n", 10410 format("struct foo { /* comment */\n" 10411 "\n" 10412 "private:\n" 10413 " int i;\n" 10414 " // comment\n" 10415 "\n" 10416 "private:\n" 10417 " int j;\n" 10418 "};\n", 10419 Style)); 10420 verifyFormat("struct foo { /* comment */\n" 10421 "private:\n" 10422 " int i;\n" 10423 " // comment\n" 10424 "private:\n" 10425 " int j;\n" 10426 "};\n", 10427 Style); 10428 EXPECT_EQ("struct foo {\n" 10429 "#ifdef FOO\n" 10430 "#endif\n" 10431 "\n" 10432 "private:\n" 10433 " int i;\n" 10434 "#ifdef FOO\n" 10435 "\n" 10436 "private:\n" 10437 "#endif\n" 10438 " int j;\n" 10439 "};\n", 10440 format("struct foo {\n" 10441 "#ifdef FOO\n" 10442 "#endif\n" 10443 "\n" 10444 "private:\n" 10445 " int i;\n" 10446 "#ifdef FOO\n" 10447 "\n" 10448 "private:\n" 10449 "#endif\n" 10450 " int j;\n" 10451 "};\n", 10452 Style)); 10453 verifyFormat("struct foo {\n" 10454 "#ifdef FOO\n" 10455 "#endif\n" 10456 "private:\n" 10457 " int i;\n" 10458 "#ifdef FOO\n" 10459 "private:\n" 10460 "#endif\n" 10461 " int j;\n" 10462 "};\n", 10463 Style); 10464 10465 FormatStyle NoEmptyLines = getLLVMStyle(); 10466 NoEmptyLines.MaxEmptyLinesToKeep = 0; 10467 verifyFormat("struct foo {\n" 10468 "private:\n" 10469 " void f() {}\n" 10470 "\n" 10471 "private:\n" 10472 " int i;\n" 10473 "\n" 10474 "public:\n" 10475 "protected:\n" 10476 " int j;\n" 10477 "};\n", 10478 NoEmptyLines); 10479 10480 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10481 verifyFormat("struct foo {\n" 10482 "private:\n" 10483 " void f() {}\n" 10484 "private:\n" 10485 " int i;\n" 10486 "public:\n" 10487 "protected:\n" 10488 " int j;\n" 10489 "};\n", 10490 NoEmptyLines); 10491 10492 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10493 verifyFormat("struct foo {\n" 10494 "private:\n" 10495 " void f() {}\n" 10496 "\n" 10497 "private:\n" 10498 " int i;\n" 10499 "\n" 10500 "public:\n" 10501 "\n" 10502 "protected:\n" 10503 " int j;\n" 10504 "};\n", 10505 NoEmptyLines); 10506 } 10507 10508 TEST_F(FormatTest, FormatsAfterAccessModifiers) { 10509 10510 FormatStyle Style = getLLVMStyle(); 10511 EXPECT_EQ(Style.EmptyLineAfterAccessModifier, FormatStyle::ELAAMS_Never); 10512 verifyFormat("struct foo {\n" 10513 "private:\n" 10514 " void f() {}\n" 10515 "\n" 10516 "private:\n" 10517 " int i;\n" 10518 "\n" 10519 "protected:\n" 10520 " int j;\n" 10521 "};\n", 10522 Style); 10523 10524 // Check if lines are removed. 10525 verifyFormat("struct foo {\n" 10526 "private:\n" 10527 " void f() {}\n" 10528 "\n" 10529 "private:\n" 10530 " int i;\n" 10531 "\n" 10532 "protected:\n" 10533 " int j;\n" 10534 "};\n", 10535 "struct foo {\n" 10536 "private:\n" 10537 "\n" 10538 " void f() {}\n" 10539 "\n" 10540 "private:\n" 10541 "\n" 10542 " int i;\n" 10543 "\n" 10544 "protected:\n" 10545 "\n" 10546 " int j;\n" 10547 "};\n", 10548 Style); 10549 10550 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10551 verifyFormat("struct foo {\n" 10552 "private:\n" 10553 "\n" 10554 " void f() {}\n" 10555 "\n" 10556 "private:\n" 10557 "\n" 10558 " int i;\n" 10559 "\n" 10560 "protected:\n" 10561 "\n" 10562 " int j;\n" 10563 "};\n", 10564 Style); 10565 10566 // Check if lines are added. 10567 verifyFormat("struct foo {\n" 10568 "private:\n" 10569 "\n" 10570 " void f() {}\n" 10571 "\n" 10572 "private:\n" 10573 "\n" 10574 " int i;\n" 10575 "\n" 10576 "protected:\n" 10577 "\n" 10578 " int j;\n" 10579 "};\n", 10580 "struct foo {\n" 10581 "private:\n" 10582 " void f() {}\n" 10583 "\n" 10584 "private:\n" 10585 " int i;\n" 10586 "\n" 10587 "protected:\n" 10588 " int j;\n" 10589 "};\n", 10590 Style); 10591 10592 // Leave tests rely on the code layout, test::messUp can not be used. 10593 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 10594 Style.MaxEmptyLinesToKeep = 0u; 10595 verifyFormat("struct foo {\n" 10596 "private:\n" 10597 " void f() {}\n" 10598 "\n" 10599 "private:\n" 10600 " int i;\n" 10601 "\n" 10602 "protected:\n" 10603 " int j;\n" 10604 "};\n", 10605 Style); 10606 10607 // Check if MaxEmptyLinesToKeep is respected. 10608 EXPECT_EQ("struct foo {\n" 10609 "private:\n" 10610 " void f() {}\n" 10611 "\n" 10612 "private:\n" 10613 " int i;\n" 10614 "\n" 10615 "protected:\n" 10616 " int j;\n" 10617 "};\n", 10618 format("struct foo {\n" 10619 "private:\n" 10620 "\n\n\n" 10621 " void f() {}\n" 10622 "\n" 10623 "private:\n" 10624 "\n\n\n" 10625 " int i;\n" 10626 "\n" 10627 "protected:\n" 10628 "\n\n\n" 10629 " int j;\n" 10630 "};\n", 10631 Style)); 10632 10633 Style.MaxEmptyLinesToKeep = 1u; 10634 EXPECT_EQ("struct foo {\n" 10635 "private:\n" 10636 "\n" 10637 " void f() {}\n" 10638 "\n" 10639 "private:\n" 10640 "\n" 10641 " int i;\n" 10642 "\n" 10643 "protected:\n" 10644 "\n" 10645 " int j;\n" 10646 "};\n", 10647 format("struct foo {\n" 10648 "private:\n" 10649 "\n" 10650 " void f() {}\n" 10651 "\n" 10652 "private:\n" 10653 "\n" 10654 " int i;\n" 10655 "\n" 10656 "protected:\n" 10657 "\n" 10658 " int j;\n" 10659 "};\n", 10660 Style)); 10661 // Check if no lines are kept. 10662 EXPECT_EQ("struct foo {\n" 10663 "private:\n" 10664 " void f() {}\n" 10665 "\n" 10666 "private:\n" 10667 " int i;\n" 10668 "\n" 10669 "protected:\n" 10670 " int j;\n" 10671 "};\n", 10672 format("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 Style)); 10683 // Check if MaxEmptyLinesToKeep is respected. 10684 EXPECT_EQ("struct foo {\n" 10685 "private:\n" 10686 "\n" 10687 " void f() {}\n" 10688 "\n" 10689 "private:\n" 10690 "\n" 10691 " int i;\n" 10692 "\n" 10693 "protected:\n" 10694 "\n" 10695 " int j;\n" 10696 "};\n", 10697 format("struct foo {\n" 10698 "private:\n" 10699 "\n\n\n" 10700 " void f() {}\n" 10701 "\n" 10702 "private:\n" 10703 "\n\n\n" 10704 " int i;\n" 10705 "\n" 10706 "protected:\n" 10707 "\n\n\n" 10708 " int j;\n" 10709 "};\n", 10710 Style)); 10711 10712 Style.MaxEmptyLinesToKeep = 10u; 10713 EXPECT_EQ("struct foo {\n" 10714 "private:\n" 10715 "\n\n\n" 10716 " void f() {}\n" 10717 "\n" 10718 "private:\n" 10719 "\n\n\n" 10720 " int i;\n" 10721 "\n" 10722 "protected:\n" 10723 "\n\n\n" 10724 " int j;\n" 10725 "};\n", 10726 format("struct foo {\n" 10727 "private:\n" 10728 "\n\n\n" 10729 " void f() {}\n" 10730 "\n" 10731 "private:\n" 10732 "\n\n\n" 10733 " int i;\n" 10734 "\n" 10735 "protected:\n" 10736 "\n\n\n" 10737 " int j;\n" 10738 "};\n", 10739 Style)); 10740 10741 // Test with comments. 10742 Style = getLLVMStyle(); 10743 verifyFormat("struct foo {\n" 10744 "private:\n" 10745 " // comment\n" 10746 " void f() {}\n" 10747 "\n" 10748 "private: /* comment */\n" 10749 " int i;\n" 10750 "};\n", 10751 Style); 10752 verifyFormat("struct foo {\n" 10753 "private:\n" 10754 " // comment\n" 10755 " void f() {}\n" 10756 "\n" 10757 "private: /* comment */\n" 10758 " int i;\n" 10759 "};\n", 10760 "struct foo {\n" 10761 "private:\n" 10762 "\n" 10763 " // comment\n" 10764 " void f() {}\n" 10765 "\n" 10766 "private: /* comment */\n" 10767 "\n" 10768 " int i;\n" 10769 "};\n", 10770 Style); 10771 10772 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10773 verifyFormat("struct foo {\n" 10774 "private:\n" 10775 "\n" 10776 " // comment\n" 10777 " void f() {}\n" 10778 "\n" 10779 "private: /* comment */\n" 10780 "\n" 10781 " int i;\n" 10782 "};\n", 10783 "struct foo {\n" 10784 "private:\n" 10785 " // comment\n" 10786 " void f() {}\n" 10787 "\n" 10788 "private: /* comment */\n" 10789 " int i;\n" 10790 "};\n", 10791 Style); 10792 verifyFormat("struct foo {\n" 10793 "private:\n" 10794 "\n" 10795 " // comment\n" 10796 " void f() {}\n" 10797 "\n" 10798 "private: /* comment */\n" 10799 "\n" 10800 " int i;\n" 10801 "};\n", 10802 Style); 10803 10804 // Test with preprocessor defines. 10805 Style = getLLVMStyle(); 10806 verifyFormat("struct foo {\n" 10807 "private:\n" 10808 "#ifdef FOO\n" 10809 "#endif\n" 10810 " void f() {}\n" 10811 "};\n", 10812 Style); 10813 verifyFormat("struct foo {\n" 10814 "private:\n" 10815 "#ifdef FOO\n" 10816 "#endif\n" 10817 " void f() {}\n" 10818 "};\n", 10819 "struct foo {\n" 10820 "private:\n" 10821 "\n" 10822 "#ifdef FOO\n" 10823 "#endif\n" 10824 " void f() {}\n" 10825 "};\n", 10826 Style); 10827 10828 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10829 verifyFormat("struct foo {\n" 10830 "private:\n" 10831 "\n" 10832 "#ifdef FOO\n" 10833 "#endif\n" 10834 " void f() {}\n" 10835 "};\n", 10836 "struct foo {\n" 10837 "private:\n" 10838 "#ifdef FOO\n" 10839 "#endif\n" 10840 " void f() {}\n" 10841 "};\n", 10842 Style); 10843 verifyFormat("struct foo {\n" 10844 "private:\n" 10845 "\n" 10846 "#ifdef FOO\n" 10847 "#endif\n" 10848 " void f() {}\n" 10849 "};\n", 10850 Style); 10851 } 10852 10853 TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) { 10854 // Combined tests of EmptyLineAfterAccessModifier and 10855 // EmptyLineBeforeAccessModifier. 10856 FormatStyle Style = getLLVMStyle(); 10857 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10858 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10859 verifyFormat("struct foo {\n" 10860 "private:\n" 10861 "\n" 10862 "protected:\n" 10863 "};\n", 10864 Style); 10865 10866 Style.MaxEmptyLinesToKeep = 10u; 10867 // Both remove all new lines. 10868 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10869 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 10870 verifyFormat("struct foo {\n" 10871 "private:\n" 10872 "protected:\n" 10873 "};\n", 10874 "struct foo {\n" 10875 "private:\n" 10876 "\n\n\n" 10877 "protected:\n" 10878 "};\n", 10879 Style); 10880 10881 // Leave tests rely on the code layout, test::messUp can not be used. 10882 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 10883 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 10884 Style.MaxEmptyLinesToKeep = 10u; 10885 EXPECT_EQ("struct foo {\n" 10886 "private:\n" 10887 "\n\n\n" 10888 "protected:\n" 10889 "};\n", 10890 format("struct foo {\n" 10891 "private:\n" 10892 "\n\n\n" 10893 "protected:\n" 10894 "};\n", 10895 Style)); 10896 Style.MaxEmptyLinesToKeep = 3u; 10897 EXPECT_EQ("struct foo {\n" 10898 "private:\n" 10899 "\n\n\n" 10900 "protected:\n" 10901 "};\n", 10902 format("struct foo {\n" 10903 "private:\n" 10904 "\n\n\n" 10905 "protected:\n" 10906 "};\n", 10907 Style)); 10908 Style.MaxEmptyLinesToKeep = 1u; 10909 EXPECT_EQ("struct foo {\n" 10910 "private:\n" 10911 "\n\n\n" 10912 "protected:\n" 10913 "};\n", 10914 format("struct foo {\n" 10915 "private:\n" 10916 "\n\n\n" 10917 "protected:\n" 10918 "};\n", 10919 Style)); // Based on new lines in original document and not 10920 // on the setting. 10921 10922 Style.MaxEmptyLinesToKeep = 10u; 10923 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10924 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 10925 // Newlines are kept if they are greater than zero, 10926 // test::messUp removes all new lines which changes the logic 10927 EXPECT_EQ("struct foo {\n" 10928 "private:\n" 10929 "\n\n\n" 10930 "protected:\n" 10931 "};\n", 10932 format("struct foo {\n" 10933 "private:\n" 10934 "\n\n\n" 10935 "protected:\n" 10936 "};\n", 10937 Style)); 10938 10939 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 10940 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10941 // test::messUp removes all new lines which changes the logic 10942 EXPECT_EQ("struct foo {\n" 10943 "private:\n" 10944 "\n\n\n" 10945 "protected:\n" 10946 "};\n", 10947 format("struct foo {\n" 10948 "private:\n" 10949 "\n\n\n" 10950 "protected:\n" 10951 "};\n", 10952 Style)); 10953 10954 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 10955 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 10956 EXPECT_EQ("struct foo {\n" 10957 "private:\n" 10958 "\n\n\n" 10959 "protected:\n" 10960 "};\n", 10961 format("struct foo {\n" 10962 "private:\n" 10963 "\n\n\n" 10964 "protected:\n" 10965 "};\n", 10966 Style)); // test::messUp removes all new lines which changes 10967 // the logic. 10968 10969 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10970 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 10971 verifyFormat("struct foo {\n" 10972 "private:\n" 10973 "protected:\n" 10974 "};\n", 10975 "struct foo {\n" 10976 "private:\n" 10977 "\n\n\n" 10978 "protected:\n" 10979 "};\n", 10980 Style); 10981 10982 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10983 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 10984 EXPECT_EQ("struct foo {\n" 10985 "private:\n" 10986 "\n\n\n" 10987 "protected:\n" 10988 "};\n", 10989 format("struct foo {\n" 10990 "private:\n" 10991 "\n\n\n" 10992 "protected:\n" 10993 "};\n", 10994 Style)); // test::messUp removes all new lines which changes 10995 // the logic. 10996 10997 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10998 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10999 verifyFormat("struct foo {\n" 11000 "private:\n" 11001 "protected:\n" 11002 "};\n", 11003 "struct foo {\n" 11004 "private:\n" 11005 "\n\n\n" 11006 "protected:\n" 11007 "};\n", 11008 Style); 11009 11010 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 11011 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11012 verifyFormat("struct foo {\n" 11013 "private:\n" 11014 "protected:\n" 11015 "};\n", 11016 "struct foo {\n" 11017 "private:\n" 11018 "\n\n\n" 11019 "protected:\n" 11020 "};\n", 11021 Style); 11022 11023 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 11024 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 11025 verifyFormat("struct foo {\n" 11026 "private:\n" 11027 "protected:\n" 11028 "};\n", 11029 "struct foo {\n" 11030 "private:\n" 11031 "\n\n\n" 11032 "protected:\n" 11033 "};\n", 11034 Style); 11035 11036 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 11037 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 11038 verifyFormat("struct foo {\n" 11039 "private:\n" 11040 "protected:\n" 11041 "};\n", 11042 "struct foo {\n" 11043 "private:\n" 11044 "\n\n\n" 11045 "protected:\n" 11046 "};\n", 11047 Style); 11048 } 11049 11050 TEST_F(FormatTest, FormatsArrays) { 11051 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 11052 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 11053 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 11054 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 11055 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 11056 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 11057 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11058 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 11059 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11060 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 11061 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11062 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 11063 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 11064 verifyFormat( 11065 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 11066 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 11067 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 11068 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" 11069 " .aaaaaaaaaaaaaaaaaaaaaa();"); 11070 11071 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 11072 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 11073 verifyFormat( 11074 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 11075 " .aaaaaaa[0]\n" 11076 " .aaaaaaaaaaaaaaaaaaaaaa();"); 11077 verifyFormat("a[::b::c];"); 11078 11079 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 11080 11081 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 11082 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 11083 } 11084 11085 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 11086 verifyFormat("(a)->b();"); 11087 verifyFormat("--a;"); 11088 } 11089 11090 TEST_F(FormatTest, HandlesIncludeDirectives) { 11091 verifyFormat("#include <string>\n" 11092 "#include <a/b/c.h>\n" 11093 "#include \"a/b/string\"\n" 11094 "#include \"string.h\"\n" 11095 "#include \"string.h\"\n" 11096 "#include <a-a>\n" 11097 "#include < path with space >\n" 11098 "#include_next <test.h>" 11099 "#include \"abc.h\" // this is included for ABC\n" 11100 "#include \"some long include\" // with a comment\n" 11101 "#include \"some very long include path\"\n" 11102 "#include <some/very/long/include/path>\n", 11103 getLLVMStyleWithColumns(35)); 11104 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 11105 EXPECT_EQ("#include <a>", format("#include<a>")); 11106 11107 verifyFormat("#import <string>"); 11108 verifyFormat("#import <a/b/c.h>"); 11109 verifyFormat("#import \"a/b/string\""); 11110 verifyFormat("#import \"string.h\""); 11111 verifyFormat("#import \"string.h\""); 11112 verifyFormat("#if __has_include(<strstream>)\n" 11113 "#include <strstream>\n" 11114 "#endif"); 11115 11116 verifyFormat("#define MY_IMPORT <a/b>"); 11117 11118 verifyFormat("#if __has_include(<a/b>)"); 11119 verifyFormat("#if __has_include_next(<a/b>)"); 11120 verifyFormat("#define F __has_include(<a/b>)"); 11121 verifyFormat("#define F __has_include_next(<a/b>)"); 11122 11123 // Protocol buffer definition or missing "#". 11124 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 11125 getLLVMStyleWithColumns(30)); 11126 11127 FormatStyle Style = getLLVMStyle(); 11128 Style.AlwaysBreakBeforeMultilineStrings = true; 11129 Style.ColumnLimit = 0; 11130 verifyFormat("#import \"abc.h\"", Style); 11131 11132 // But 'import' might also be a regular C++ namespace. 11133 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11134 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 11135 } 11136 11137 //===----------------------------------------------------------------------===// 11138 // Error recovery tests. 11139 //===----------------------------------------------------------------------===// 11140 11141 TEST_F(FormatTest, IncompleteParameterLists) { 11142 FormatStyle NoBinPacking = getLLVMStyle(); 11143 NoBinPacking.BinPackParameters = false; 11144 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 11145 " double *min_x,\n" 11146 " double *max_x,\n" 11147 " double *min_y,\n" 11148 " double *max_y,\n" 11149 " double *min_z,\n" 11150 " double *max_z, ) {}", 11151 NoBinPacking); 11152 } 11153 11154 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 11155 verifyFormat("void f() { return; }\n42"); 11156 verifyFormat("void f() {\n" 11157 " if (0)\n" 11158 " return;\n" 11159 "}\n" 11160 "42"); 11161 verifyFormat("void f() { return }\n42"); 11162 verifyFormat("void f() {\n" 11163 " if (0)\n" 11164 " return\n" 11165 "}\n" 11166 "42"); 11167 } 11168 11169 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 11170 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 11171 EXPECT_EQ("void f() {\n" 11172 " if (a)\n" 11173 " return\n" 11174 "}", 11175 format("void f ( ) { if ( a ) return }")); 11176 EXPECT_EQ("namespace N {\n" 11177 "void f()\n" 11178 "}", 11179 format("namespace N { void f() }")); 11180 EXPECT_EQ("namespace N {\n" 11181 "void f() {}\n" 11182 "void g()\n" 11183 "} // namespace N", 11184 format("namespace N { void f( ) { } void g( ) }")); 11185 } 11186 11187 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 11188 verifyFormat("int aaaaaaaa =\n" 11189 " // Overlylongcomment\n" 11190 " b;", 11191 getLLVMStyleWithColumns(20)); 11192 verifyFormat("function(\n" 11193 " ShortArgument,\n" 11194 " LoooooooooooongArgument);\n", 11195 getLLVMStyleWithColumns(20)); 11196 } 11197 11198 TEST_F(FormatTest, IncorrectAccessSpecifier) { 11199 verifyFormat("public:"); 11200 verifyFormat("class A {\n" 11201 "public\n" 11202 " void f() {}\n" 11203 "};"); 11204 verifyFormat("public\n" 11205 "int qwerty;"); 11206 verifyFormat("public\n" 11207 "B {}"); 11208 verifyFormat("public\n" 11209 "{}"); 11210 verifyFormat("public\n" 11211 "B { int x; }"); 11212 } 11213 11214 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 11215 verifyFormat("{"); 11216 verifyFormat("#})"); 11217 verifyNoCrash("(/**/[:!] ?[)."); 11218 } 11219 11220 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) { 11221 // Found by oss-fuzz: 11222 // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212 11223 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 11224 Style.ColumnLimit = 60; 11225 verifyNoCrash( 11226 "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20" 11227 "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20" 11228 "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a", 11229 Style); 11230 } 11231 11232 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 11233 verifyFormat("do {\n}"); 11234 verifyFormat("do {\n}\n" 11235 "f();"); 11236 verifyFormat("do {\n}\n" 11237 "wheeee(fun);"); 11238 verifyFormat("do {\n" 11239 " f();\n" 11240 "}"); 11241 } 11242 11243 TEST_F(FormatTest, IncorrectCodeMissingParens) { 11244 verifyFormat("if {\n foo;\n foo();\n}"); 11245 verifyFormat("switch {\n foo;\n foo();\n}"); 11246 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 11247 verifyFormat("while {\n foo;\n foo();\n}"); 11248 verifyFormat("do {\n foo;\n foo();\n} while;"); 11249 } 11250 11251 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 11252 verifyIncompleteFormat("namespace {\n" 11253 "class Foo { Foo (\n" 11254 "};\n" 11255 "} // namespace"); 11256 } 11257 11258 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 11259 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 11260 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 11261 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 11262 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 11263 11264 EXPECT_EQ("{\n" 11265 " {\n" 11266 " breakme(\n" 11267 " qwe);\n" 11268 " }\n", 11269 format("{\n" 11270 " {\n" 11271 " breakme(qwe);\n" 11272 "}\n", 11273 getLLVMStyleWithColumns(10))); 11274 } 11275 11276 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 11277 verifyFormat("int x = {\n" 11278 " avariable,\n" 11279 " b(alongervariable)};", 11280 getLLVMStyleWithColumns(25)); 11281 } 11282 11283 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 11284 verifyFormat("return (a)(b){1, 2, 3};"); 11285 } 11286 11287 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 11288 verifyFormat("vector<int> x{1, 2, 3, 4};"); 11289 verifyFormat("vector<int> x{\n" 11290 " 1,\n" 11291 " 2,\n" 11292 " 3,\n" 11293 " 4,\n" 11294 "};"); 11295 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 11296 verifyFormat("f({1, 2});"); 11297 verifyFormat("auto v = Foo{-1};"); 11298 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 11299 verifyFormat("Class::Class : member{1, 2, 3} {}"); 11300 verifyFormat("new vector<int>{1, 2, 3};"); 11301 verifyFormat("new int[3]{1, 2, 3};"); 11302 verifyFormat("new int{1};"); 11303 verifyFormat("return {arg1, arg2};"); 11304 verifyFormat("return {arg1, SomeType{parameter}};"); 11305 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 11306 verifyFormat("new T{arg1, arg2};"); 11307 verifyFormat("f(MyMap[{composite, key}]);"); 11308 verifyFormat("class Class {\n" 11309 " T member = {arg1, arg2};\n" 11310 "};"); 11311 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 11312 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 11313 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 11314 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 11315 verifyFormat("int a = std::is_integral<int>{} + 0;"); 11316 11317 verifyFormat("int foo(int i) { return fo1{}(i); }"); 11318 verifyFormat("int foo(int i) { return fo1{}(i); }"); 11319 verifyFormat("auto i = decltype(x){};"); 11320 verifyFormat("auto i = typeof(x){};"); 11321 verifyFormat("auto i = _Atomic(x){};"); 11322 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 11323 verifyFormat("Node n{1, Node{1000}, //\n" 11324 " 2};"); 11325 verifyFormat("Aaaa aaaaaaa{\n" 11326 " {\n" 11327 " aaaa,\n" 11328 " },\n" 11329 "};"); 11330 verifyFormat("class C : public D {\n" 11331 " SomeClass SC{2};\n" 11332 "};"); 11333 verifyFormat("class C : public A {\n" 11334 " class D : public B {\n" 11335 " void f() { int i{2}; }\n" 11336 " };\n" 11337 "};"); 11338 verifyFormat("#define A {a, a},"); 11339 11340 // Avoid breaking between equal sign and opening brace 11341 FormatStyle AvoidBreakingFirstArgument = getLLVMStyle(); 11342 AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200; 11343 verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n" 11344 " {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n" 11345 " {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n" 11346 " {\"ccccccccccccccccccccc\", 2}};", 11347 AvoidBreakingFirstArgument); 11348 11349 // Binpacking only if there is no trailing comma 11350 verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n" 11351 " cccccccccc, dddddddddd};", 11352 getLLVMStyleWithColumns(50)); 11353 verifyFormat("const Aaaaaa aaaaa = {\n" 11354 " aaaaaaaaaaa,\n" 11355 " bbbbbbbbbbb,\n" 11356 " ccccccccccc,\n" 11357 " ddddddddddd,\n" 11358 "};", 11359 getLLVMStyleWithColumns(50)); 11360 11361 // Cases where distinguising braced lists and blocks is hard. 11362 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);"); 11363 verifyFormat("void f() {\n" 11364 " return; // comment\n" 11365 "}\n" 11366 "SomeType t;"); 11367 verifyFormat("void f() {\n" 11368 " if (a) {\n" 11369 " f();\n" 11370 " }\n" 11371 "}\n" 11372 "SomeType t;"); 11373 11374 // In combination with BinPackArguments = false. 11375 FormatStyle NoBinPacking = getLLVMStyle(); 11376 NoBinPacking.BinPackArguments = false; 11377 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 11378 " bbbbb,\n" 11379 " ccccc,\n" 11380 " ddddd,\n" 11381 " eeeee,\n" 11382 " ffffff,\n" 11383 " ggggg,\n" 11384 " hhhhhh,\n" 11385 " iiiiii,\n" 11386 " jjjjjj,\n" 11387 " kkkkkk};", 11388 NoBinPacking); 11389 verifyFormat("const Aaaaaa aaaaa = {\n" 11390 " aaaaa,\n" 11391 " bbbbb,\n" 11392 " ccccc,\n" 11393 " ddddd,\n" 11394 " eeeee,\n" 11395 " ffffff,\n" 11396 " ggggg,\n" 11397 " hhhhhh,\n" 11398 " iiiiii,\n" 11399 " jjjjjj,\n" 11400 " kkkkkk,\n" 11401 "};", 11402 NoBinPacking); 11403 verifyFormat( 11404 "const Aaaaaa aaaaa = {\n" 11405 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 11406 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 11407 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 11408 "};", 11409 NoBinPacking); 11410 11411 NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 11412 EXPECT_EQ("static uint8 CddDp83848Reg[] = {\n" 11413 " CDDDP83848_BMCR_REGISTER,\n" 11414 " CDDDP83848_BMSR_REGISTER,\n" 11415 " CDDDP83848_RBR_REGISTER};", 11416 format("static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n" 11417 " CDDDP83848_BMSR_REGISTER,\n" 11418 " CDDDP83848_RBR_REGISTER};", 11419 NoBinPacking)); 11420 11421 // FIXME: The alignment of these trailing comments might be bad. Then again, 11422 // this might be utterly useless in real code. 11423 verifyFormat("Constructor::Constructor()\n" 11424 " : some_value{ //\n" 11425 " aaaaaaa, //\n" 11426 " bbbbbbb} {}"); 11427 11428 // In braced lists, the first comment is always assumed to belong to the 11429 // first element. Thus, it can be moved to the next or previous line as 11430 // appropriate. 11431 EXPECT_EQ("function({// First element:\n" 11432 " 1,\n" 11433 " // Second element:\n" 11434 " 2});", 11435 format("function({\n" 11436 " // First element:\n" 11437 " 1,\n" 11438 " // Second element:\n" 11439 " 2});")); 11440 EXPECT_EQ("std::vector<int> MyNumbers{\n" 11441 " // First element:\n" 11442 " 1,\n" 11443 " // Second element:\n" 11444 " 2};", 11445 format("std::vector<int> MyNumbers{// First element:\n" 11446 " 1,\n" 11447 " // Second element:\n" 11448 " 2};", 11449 getLLVMStyleWithColumns(30))); 11450 // A trailing comma should still lead to an enforced line break and no 11451 // binpacking. 11452 EXPECT_EQ("vector<int> SomeVector = {\n" 11453 " // aaa\n" 11454 " 1,\n" 11455 " 2,\n" 11456 "};", 11457 format("vector<int> SomeVector = { // aaa\n" 11458 " 1, 2, };")); 11459 11460 // C++11 brace initializer list l-braces should not be treated any differently 11461 // when breaking before lambda bodies is enabled 11462 FormatStyle BreakBeforeLambdaBody = getLLVMStyle(); 11463 BreakBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 11464 BreakBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 11465 BreakBeforeLambdaBody.AlwaysBreakBeforeMultilineStrings = true; 11466 verifyFormat( 11467 "std::runtime_error{\n" 11468 " \"Long string which will force a break onto the next line...\"};", 11469 BreakBeforeLambdaBody); 11470 11471 FormatStyle ExtraSpaces = getLLVMStyle(); 11472 ExtraSpaces.Cpp11BracedListStyle = false; 11473 ExtraSpaces.ColumnLimit = 75; 11474 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 11475 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 11476 verifyFormat("f({ 1, 2 });", ExtraSpaces); 11477 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 11478 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 11479 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 11480 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 11481 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 11482 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 11483 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 11484 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 11485 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 11486 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 11487 verifyFormat("class Class {\n" 11488 " T member = { arg1, arg2 };\n" 11489 "};", 11490 ExtraSpaces); 11491 verifyFormat( 11492 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11493 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 11494 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 11495 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 11496 ExtraSpaces); 11497 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 11498 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 11499 ExtraSpaces); 11500 verifyFormat( 11501 "someFunction(OtherParam,\n" 11502 " BracedList{ // comment 1 (Forcing interesting break)\n" 11503 " param1, param2,\n" 11504 " // comment 2\n" 11505 " param3, param4 });", 11506 ExtraSpaces); 11507 verifyFormat( 11508 "std::this_thread::sleep_for(\n" 11509 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 11510 ExtraSpaces); 11511 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n" 11512 " aaaaaaa,\n" 11513 " aaaaaaaaaa,\n" 11514 " aaaaa,\n" 11515 " aaaaaaaaaaaaaaa,\n" 11516 " aaa,\n" 11517 " aaaaaaaaaa,\n" 11518 " a,\n" 11519 " aaaaaaaaaaaaaaaaaaaaa,\n" 11520 " aaaaaaaaaaaa,\n" 11521 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 11522 " aaaaaaa,\n" 11523 " a};"); 11524 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 11525 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces); 11526 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces); 11527 11528 // Avoid breaking between initializer/equal sign and opening brace 11529 ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200; 11530 verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n" 11531 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 11532 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 11533 " { \"ccccccccccccccccccccc\", 2 }\n" 11534 "};", 11535 ExtraSpaces); 11536 verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n" 11537 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 11538 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 11539 " { \"ccccccccccccccccccccc\", 2 }\n" 11540 "};", 11541 ExtraSpaces); 11542 11543 FormatStyle SpaceBeforeBrace = getLLVMStyle(); 11544 SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true; 11545 verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace); 11546 verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace); 11547 11548 FormatStyle SpaceBetweenBraces = getLLVMStyle(); 11549 SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always; 11550 SpaceBetweenBraces.SpacesInParentheses = true; 11551 SpaceBetweenBraces.SpacesInSquareBrackets = true; 11552 verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces); 11553 verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces); 11554 verifyFormat("vector< int > x{ // comment 1\n" 11555 " 1, 2, 3, 4 };", 11556 SpaceBetweenBraces); 11557 SpaceBetweenBraces.ColumnLimit = 20; 11558 EXPECT_EQ("vector< int > x{\n" 11559 " 1, 2, 3, 4 };", 11560 format("vector<int>x{1,2,3,4};", SpaceBetweenBraces)); 11561 SpaceBetweenBraces.ColumnLimit = 24; 11562 EXPECT_EQ("vector< int > x{ 1, 2,\n" 11563 " 3, 4 };", 11564 format("vector<int>x{1,2,3,4};", SpaceBetweenBraces)); 11565 EXPECT_EQ("vector< int > x{\n" 11566 " 1,\n" 11567 " 2,\n" 11568 " 3,\n" 11569 " 4,\n" 11570 "};", 11571 format("vector<int>x{1,2,3,4,};", SpaceBetweenBraces)); 11572 verifyFormat("vector< int > x{};", SpaceBetweenBraces); 11573 SpaceBetweenBraces.SpaceInEmptyParentheses = true; 11574 verifyFormat("vector< int > x{ };", SpaceBetweenBraces); 11575 } 11576 11577 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 11578 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11579 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11580 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11581 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11582 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11583 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 11584 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 11585 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11586 " 1, 22, 333, 4444, 55555, //\n" 11587 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11588 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 11589 verifyFormat( 11590 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11591 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11592 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 11593 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 11594 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 11595 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 11596 " 7777777};"); 11597 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 11598 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 11599 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 11600 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 11601 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 11602 " // Separating comment.\n" 11603 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 11604 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 11605 " // Leading comment\n" 11606 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 11607 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 11608 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 11609 " 1, 1, 1, 1};", 11610 getLLVMStyleWithColumns(39)); 11611 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 11612 " 1, 1, 1, 1};", 11613 getLLVMStyleWithColumns(38)); 11614 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 11615 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 11616 getLLVMStyleWithColumns(43)); 11617 verifyFormat( 11618 "static unsigned SomeValues[10][3] = {\n" 11619 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 11620 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 11621 verifyFormat("static auto fields = new vector<string>{\n" 11622 " \"aaaaaaaaaaaaa\",\n" 11623 " \"aaaaaaaaaaaaa\",\n" 11624 " \"aaaaaaaaaaaa\",\n" 11625 " \"aaaaaaaaaaaaaa\",\n" 11626 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 11627 " \"aaaaaaaaaaaa\",\n" 11628 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 11629 "};"); 11630 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 11631 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 11632 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 11633 " 3, cccccccccccccccccccccc};", 11634 getLLVMStyleWithColumns(60)); 11635 11636 // Trailing commas. 11637 verifyFormat("vector<int> x = {\n" 11638 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 11639 "};", 11640 getLLVMStyleWithColumns(39)); 11641 verifyFormat("vector<int> x = {\n" 11642 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 11643 "};", 11644 getLLVMStyleWithColumns(39)); 11645 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 11646 " 1, 1, 1, 1,\n" 11647 " /**/ /**/};", 11648 getLLVMStyleWithColumns(39)); 11649 11650 // Trailing comment in the first line. 11651 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 11652 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 11653 " 111111111, 222222222, 3333333333, 444444444, //\n" 11654 " 11111111, 22222222, 333333333, 44444444};"); 11655 // Trailing comment in the last line. 11656 verifyFormat("int aaaaa[] = {\n" 11657 " 1, 2, 3, // comment\n" 11658 " 4, 5, 6 // comment\n" 11659 "};"); 11660 11661 // With nested lists, we should either format one item per line or all nested 11662 // lists one on line. 11663 // FIXME: For some nested lists, we can do better. 11664 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 11665 " {aaaaaaaaaaaaaaaaaaa},\n" 11666 " {aaaaaaaaaaaaaaaaaaaaa},\n" 11667 " {aaaaaaaaaaaaaaaaa}};", 11668 getLLVMStyleWithColumns(60)); 11669 verifyFormat( 11670 "SomeStruct my_struct_array = {\n" 11671 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 11672 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 11673 " {aaa, aaa},\n" 11674 " {aaa, aaa},\n" 11675 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 11676 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 11677 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 11678 11679 // No column layout should be used here. 11680 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 11681 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 11682 11683 verifyNoCrash("a<,"); 11684 11685 // No braced initializer here. 11686 verifyFormat("void f() {\n" 11687 " struct Dummy {};\n" 11688 " f(v);\n" 11689 "}"); 11690 11691 // Long lists should be formatted in columns even if they are nested. 11692 verifyFormat( 11693 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11694 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11695 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11696 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11697 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11698 " 1, 22, 333, 4444, 55555, 666666, 7777777});"); 11699 11700 // Allow "single-column" layout even if that violates the column limit. There 11701 // isn't going to be a better way. 11702 verifyFormat("std::vector<int> a = {\n" 11703 " aaaaaaaa,\n" 11704 " aaaaaaaa,\n" 11705 " aaaaaaaa,\n" 11706 " aaaaaaaa,\n" 11707 " aaaaaaaaaa,\n" 11708 " aaaaaaaa,\n" 11709 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};", 11710 getLLVMStyleWithColumns(30)); 11711 verifyFormat("vector<int> aaaa = {\n" 11712 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11713 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11714 " aaaaaa.aaaaaaa,\n" 11715 " aaaaaa.aaaaaaa,\n" 11716 " aaaaaa.aaaaaaa,\n" 11717 " aaaaaa.aaaaaaa,\n" 11718 "};"); 11719 11720 // Don't create hanging lists. 11721 verifyFormat("someFunction(Param, {List1, List2,\n" 11722 " List3});", 11723 getLLVMStyleWithColumns(35)); 11724 verifyFormat("someFunction(Param, Param,\n" 11725 " {List1, List2,\n" 11726 " List3});", 11727 getLLVMStyleWithColumns(35)); 11728 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n" 11729 " aaaaaaaaaaaaaaaaaaaaaaa);"); 11730 } 11731 11732 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 11733 FormatStyle DoNotMerge = getLLVMStyle(); 11734 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 11735 11736 verifyFormat("void f() { return 42; }"); 11737 verifyFormat("void f() {\n" 11738 " return 42;\n" 11739 "}", 11740 DoNotMerge); 11741 verifyFormat("void f() {\n" 11742 " // Comment\n" 11743 "}"); 11744 verifyFormat("{\n" 11745 "#error {\n" 11746 " int a;\n" 11747 "}"); 11748 verifyFormat("{\n" 11749 " int a;\n" 11750 "#error {\n" 11751 "}"); 11752 verifyFormat("void f() {} // comment"); 11753 verifyFormat("void f() { int a; } // comment"); 11754 verifyFormat("void f() {\n" 11755 "} // comment", 11756 DoNotMerge); 11757 verifyFormat("void f() {\n" 11758 " int a;\n" 11759 "} // comment", 11760 DoNotMerge); 11761 verifyFormat("void f() {\n" 11762 "} // comment", 11763 getLLVMStyleWithColumns(15)); 11764 11765 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 11766 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 11767 11768 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 11769 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 11770 verifyFormat("class C {\n" 11771 " C()\n" 11772 " : iiiiiiii(nullptr),\n" 11773 " kkkkkkk(nullptr),\n" 11774 " mmmmmmm(nullptr),\n" 11775 " nnnnnnn(nullptr) {}\n" 11776 "};", 11777 getGoogleStyle()); 11778 11779 FormatStyle NoColumnLimit = getLLVMStyle(); 11780 NoColumnLimit.ColumnLimit = 0; 11781 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 11782 EXPECT_EQ("class C {\n" 11783 " A() : b(0) {}\n" 11784 "};", 11785 format("class C{A():b(0){}};", NoColumnLimit)); 11786 EXPECT_EQ("A()\n" 11787 " : b(0) {\n" 11788 "}", 11789 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 11790 11791 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 11792 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 11793 FormatStyle::SFS_None; 11794 EXPECT_EQ("A()\n" 11795 " : b(0) {\n" 11796 "}", 11797 format("A():b(0){}", DoNotMergeNoColumnLimit)); 11798 EXPECT_EQ("A()\n" 11799 " : b(0) {\n" 11800 "}", 11801 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 11802 11803 verifyFormat("#define A \\\n" 11804 " void f() { \\\n" 11805 " int i; \\\n" 11806 " }", 11807 getLLVMStyleWithColumns(20)); 11808 verifyFormat("#define A \\\n" 11809 " void f() { int i; }", 11810 getLLVMStyleWithColumns(21)); 11811 verifyFormat("#define A \\\n" 11812 " void f() { \\\n" 11813 " int i; \\\n" 11814 " } \\\n" 11815 " int j;", 11816 getLLVMStyleWithColumns(22)); 11817 verifyFormat("#define A \\\n" 11818 " void f() { int i; } \\\n" 11819 " int j;", 11820 getLLVMStyleWithColumns(23)); 11821 } 11822 11823 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) { 11824 FormatStyle MergeEmptyOnly = getLLVMStyle(); 11825 MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 11826 verifyFormat("class C {\n" 11827 " int f() {}\n" 11828 "};", 11829 MergeEmptyOnly); 11830 verifyFormat("class C {\n" 11831 " int f() {\n" 11832 " return 42;\n" 11833 " }\n" 11834 "};", 11835 MergeEmptyOnly); 11836 verifyFormat("int f() {}", MergeEmptyOnly); 11837 verifyFormat("int f() {\n" 11838 " return 42;\n" 11839 "}", 11840 MergeEmptyOnly); 11841 11842 // Also verify behavior when BraceWrapping.AfterFunction = true 11843 MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 11844 MergeEmptyOnly.BraceWrapping.AfterFunction = true; 11845 verifyFormat("int f() {}", MergeEmptyOnly); 11846 verifyFormat("class C {\n" 11847 " int f() {}\n" 11848 "};", 11849 MergeEmptyOnly); 11850 } 11851 11852 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 11853 FormatStyle MergeInlineOnly = getLLVMStyle(); 11854 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 11855 verifyFormat("class C {\n" 11856 " int f() { return 42; }\n" 11857 "};", 11858 MergeInlineOnly); 11859 verifyFormat("int f() {\n" 11860 " return 42;\n" 11861 "}", 11862 MergeInlineOnly); 11863 11864 // SFS_Inline implies SFS_Empty 11865 verifyFormat("class C {\n" 11866 " int f() {}\n" 11867 "};", 11868 MergeInlineOnly); 11869 verifyFormat("int f() {}", MergeInlineOnly); 11870 11871 // Also verify behavior when BraceWrapping.AfterFunction = true 11872 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 11873 MergeInlineOnly.BraceWrapping.AfterFunction = true; 11874 verifyFormat("class C {\n" 11875 " int f() { return 42; }\n" 11876 "};", 11877 MergeInlineOnly); 11878 verifyFormat("int f()\n" 11879 "{\n" 11880 " return 42;\n" 11881 "}", 11882 MergeInlineOnly); 11883 11884 // SFS_Inline implies SFS_Empty 11885 verifyFormat("int f() {}", MergeInlineOnly); 11886 verifyFormat("class C {\n" 11887 " int f() {}\n" 11888 "};", 11889 MergeInlineOnly); 11890 } 11891 11892 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) { 11893 FormatStyle MergeInlineOnly = getLLVMStyle(); 11894 MergeInlineOnly.AllowShortFunctionsOnASingleLine = 11895 FormatStyle::SFS_InlineOnly; 11896 verifyFormat("class C {\n" 11897 " int f() { return 42; }\n" 11898 "};", 11899 MergeInlineOnly); 11900 verifyFormat("int f() {\n" 11901 " return 42;\n" 11902 "}", 11903 MergeInlineOnly); 11904 11905 // SFS_InlineOnly does not imply SFS_Empty 11906 verifyFormat("class C {\n" 11907 " int f() {}\n" 11908 "};", 11909 MergeInlineOnly); 11910 verifyFormat("int f() {\n" 11911 "}", 11912 MergeInlineOnly); 11913 11914 // Also verify behavior when BraceWrapping.AfterFunction = true 11915 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 11916 MergeInlineOnly.BraceWrapping.AfterFunction = true; 11917 verifyFormat("class C {\n" 11918 " int f() { return 42; }\n" 11919 "};", 11920 MergeInlineOnly); 11921 verifyFormat("int f()\n" 11922 "{\n" 11923 " return 42;\n" 11924 "}", 11925 MergeInlineOnly); 11926 11927 // SFS_InlineOnly does not imply SFS_Empty 11928 verifyFormat("int f()\n" 11929 "{\n" 11930 "}", 11931 MergeInlineOnly); 11932 verifyFormat("class C {\n" 11933 " int f() {}\n" 11934 "};", 11935 MergeInlineOnly); 11936 } 11937 11938 TEST_F(FormatTest, SplitEmptyFunction) { 11939 FormatStyle Style = getLLVMStyle(); 11940 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 11941 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 11942 Style.BraceWrapping.AfterFunction = true; 11943 Style.BraceWrapping.SplitEmptyFunction = false; 11944 Style.ColumnLimit = 40; 11945 11946 verifyFormat("int f()\n" 11947 "{}", 11948 Style); 11949 verifyFormat("int f()\n" 11950 "{\n" 11951 " return 42;\n" 11952 "}", 11953 Style); 11954 verifyFormat("int f()\n" 11955 "{\n" 11956 " // some comment\n" 11957 "}", 11958 Style); 11959 11960 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 11961 verifyFormat("int f() {}", Style); 11962 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 11963 "{}", 11964 Style); 11965 verifyFormat("int f()\n" 11966 "{\n" 11967 " return 0;\n" 11968 "}", 11969 Style); 11970 11971 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 11972 verifyFormat("class Foo {\n" 11973 " int f() {}\n" 11974 "};\n", 11975 Style); 11976 verifyFormat("class Foo {\n" 11977 " int f() { return 0; }\n" 11978 "};\n", 11979 Style); 11980 verifyFormat("class Foo {\n" 11981 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 11982 " {}\n" 11983 "};\n", 11984 Style); 11985 verifyFormat("class Foo {\n" 11986 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 11987 " {\n" 11988 " return 0;\n" 11989 " }\n" 11990 "};\n", 11991 Style); 11992 11993 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 11994 verifyFormat("int f() {}", Style); 11995 verifyFormat("int f() { return 0; }", Style); 11996 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 11997 "{}", 11998 Style); 11999 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12000 "{\n" 12001 " return 0;\n" 12002 "}", 12003 Style); 12004 } 12005 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) { 12006 FormatStyle Style = getLLVMStyle(); 12007 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 12008 verifyFormat("#ifdef A\n" 12009 "int f() {}\n" 12010 "#else\n" 12011 "int g() {}\n" 12012 "#endif", 12013 Style); 12014 } 12015 12016 TEST_F(FormatTest, SplitEmptyClass) { 12017 FormatStyle Style = getLLVMStyle(); 12018 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12019 Style.BraceWrapping.AfterClass = true; 12020 Style.BraceWrapping.SplitEmptyRecord = false; 12021 12022 verifyFormat("class Foo\n" 12023 "{};", 12024 Style); 12025 verifyFormat("/* something */ class Foo\n" 12026 "{};", 12027 Style); 12028 verifyFormat("template <typename X> class Foo\n" 12029 "{};", 12030 Style); 12031 verifyFormat("class Foo\n" 12032 "{\n" 12033 " Foo();\n" 12034 "};", 12035 Style); 12036 verifyFormat("typedef class Foo\n" 12037 "{\n" 12038 "} Foo_t;", 12039 Style); 12040 12041 Style.BraceWrapping.SplitEmptyRecord = true; 12042 Style.BraceWrapping.AfterStruct = true; 12043 verifyFormat("class rep\n" 12044 "{\n" 12045 "};", 12046 Style); 12047 verifyFormat("struct rep\n" 12048 "{\n" 12049 "};", 12050 Style); 12051 verifyFormat("template <typename T> class rep\n" 12052 "{\n" 12053 "};", 12054 Style); 12055 verifyFormat("template <typename T> struct rep\n" 12056 "{\n" 12057 "};", 12058 Style); 12059 verifyFormat("class rep\n" 12060 "{\n" 12061 " int x;\n" 12062 "};", 12063 Style); 12064 verifyFormat("struct rep\n" 12065 "{\n" 12066 " int x;\n" 12067 "};", 12068 Style); 12069 verifyFormat("template <typename T> class rep\n" 12070 "{\n" 12071 " int x;\n" 12072 "};", 12073 Style); 12074 verifyFormat("template <typename T> struct rep\n" 12075 "{\n" 12076 " int x;\n" 12077 "};", 12078 Style); 12079 verifyFormat("template <typename T> class rep // Foo\n" 12080 "{\n" 12081 " int x;\n" 12082 "};", 12083 Style); 12084 verifyFormat("template <typename T> struct rep // Bar\n" 12085 "{\n" 12086 " int x;\n" 12087 "};", 12088 Style); 12089 12090 verifyFormat("template <typename T> class rep<T>\n" 12091 "{\n" 12092 " int x;\n" 12093 "};", 12094 Style); 12095 12096 verifyFormat("template <typename T> class rep<std::complex<T>>\n" 12097 "{\n" 12098 " int x;\n" 12099 "};", 12100 Style); 12101 verifyFormat("template <typename T> class rep<std::complex<T>>\n" 12102 "{\n" 12103 "};", 12104 Style); 12105 12106 verifyFormat("#include \"stdint.h\"\n" 12107 "namespace rep {}", 12108 Style); 12109 verifyFormat("#include <stdint.h>\n" 12110 "namespace rep {}", 12111 Style); 12112 verifyFormat("#include <stdint.h>\n" 12113 "namespace rep {}", 12114 "#include <stdint.h>\n" 12115 "namespace rep {\n" 12116 "\n" 12117 "\n" 12118 "}", 12119 Style); 12120 } 12121 12122 TEST_F(FormatTest, SplitEmptyStruct) { 12123 FormatStyle Style = getLLVMStyle(); 12124 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12125 Style.BraceWrapping.AfterStruct = true; 12126 Style.BraceWrapping.SplitEmptyRecord = false; 12127 12128 verifyFormat("struct Foo\n" 12129 "{};", 12130 Style); 12131 verifyFormat("/* something */ struct Foo\n" 12132 "{};", 12133 Style); 12134 verifyFormat("template <typename X> struct Foo\n" 12135 "{};", 12136 Style); 12137 verifyFormat("struct Foo\n" 12138 "{\n" 12139 " Foo();\n" 12140 "};", 12141 Style); 12142 verifyFormat("typedef struct Foo\n" 12143 "{\n" 12144 "} Foo_t;", 12145 Style); 12146 // typedef struct Bar {} Bar_t; 12147 } 12148 12149 TEST_F(FormatTest, SplitEmptyUnion) { 12150 FormatStyle Style = getLLVMStyle(); 12151 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12152 Style.BraceWrapping.AfterUnion = true; 12153 Style.BraceWrapping.SplitEmptyRecord = false; 12154 12155 verifyFormat("union Foo\n" 12156 "{};", 12157 Style); 12158 verifyFormat("/* something */ union Foo\n" 12159 "{};", 12160 Style); 12161 verifyFormat("union Foo\n" 12162 "{\n" 12163 " A,\n" 12164 "};", 12165 Style); 12166 verifyFormat("typedef union Foo\n" 12167 "{\n" 12168 "} Foo_t;", 12169 Style); 12170 } 12171 12172 TEST_F(FormatTest, SplitEmptyNamespace) { 12173 FormatStyle Style = getLLVMStyle(); 12174 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12175 Style.BraceWrapping.AfterNamespace = true; 12176 Style.BraceWrapping.SplitEmptyNamespace = false; 12177 12178 verifyFormat("namespace Foo\n" 12179 "{};", 12180 Style); 12181 verifyFormat("/* something */ namespace Foo\n" 12182 "{};", 12183 Style); 12184 verifyFormat("inline namespace Foo\n" 12185 "{};", 12186 Style); 12187 verifyFormat("/* something */ inline namespace Foo\n" 12188 "{};", 12189 Style); 12190 verifyFormat("export namespace Foo\n" 12191 "{};", 12192 Style); 12193 verifyFormat("namespace Foo\n" 12194 "{\n" 12195 "void Bar();\n" 12196 "};", 12197 Style); 12198 } 12199 12200 TEST_F(FormatTest, NeverMergeShortRecords) { 12201 FormatStyle Style = getLLVMStyle(); 12202 12203 verifyFormat("class Foo {\n" 12204 " Foo();\n" 12205 "};", 12206 Style); 12207 verifyFormat("typedef class Foo {\n" 12208 " Foo();\n" 12209 "} Foo_t;", 12210 Style); 12211 verifyFormat("struct Foo {\n" 12212 " Foo();\n" 12213 "};", 12214 Style); 12215 verifyFormat("typedef struct Foo {\n" 12216 " Foo();\n" 12217 "} Foo_t;", 12218 Style); 12219 verifyFormat("union Foo {\n" 12220 " A,\n" 12221 "};", 12222 Style); 12223 verifyFormat("typedef union Foo {\n" 12224 " A,\n" 12225 "} Foo_t;", 12226 Style); 12227 verifyFormat("namespace Foo {\n" 12228 "void Bar();\n" 12229 "};", 12230 Style); 12231 12232 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12233 Style.BraceWrapping.AfterClass = true; 12234 Style.BraceWrapping.AfterStruct = true; 12235 Style.BraceWrapping.AfterUnion = true; 12236 Style.BraceWrapping.AfterNamespace = true; 12237 verifyFormat("class Foo\n" 12238 "{\n" 12239 " Foo();\n" 12240 "};", 12241 Style); 12242 verifyFormat("typedef class Foo\n" 12243 "{\n" 12244 " Foo();\n" 12245 "} Foo_t;", 12246 Style); 12247 verifyFormat("struct Foo\n" 12248 "{\n" 12249 " Foo();\n" 12250 "};", 12251 Style); 12252 verifyFormat("typedef struct Foo\n" 12253 "{\n" 12254 " Foo();\n" 12255 "} Foo_t;", 12256 Style); 12257 verifyFormat("union Foo\n" 12258 "{\n" 12259 " A,\n" 12260 "};", 12261 Style); 12262 verifyFormat("typedef union Foo\n" 12263 "{\n" 12264 " A,\n" 12265 "} Foo_t;", 12266 Style); 12267 verifyFormat("namespace Foo\n" 12268 "{\n" 12269 "void Bar();\n" 12270 "};", 12271 Style); 12272 } 12273 12274 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 12275 // Elaborate type variable declarations. 12276 verifyFormat("struct foo a = {bar};\nint n;"); 12277 verifyFormat("class foo a = {bar};\nint n;"); 12278 verifyFormat("union foo a = {bar};\nint n;"); 12279 12280 // Elaborate types inside function definitions. 12281 verifyFormat("struct foo f() {}\nint n;"); 12282 verifyFormat("class foo f() {}\nint n;"); 12283 verifyFormat("union foo f() {}\nint n;"); 12284 12285 // Templates. 12286 verifyFormat("template <class X> void f() {}\nint n;"); 12287 verifyFormat("template <struct X> void f() {}\nint n;"); 12288 verifyFormat("template <union X> void f() {}\nint n;"); 12289 12290 // Actual definitions... 12291 verifyFormat("struct {\n} n;"); 12292 verifyFormat( 12293 "template <template <class T, class Y>, class Z> class X {\n} n;"); 12294 verifyFormat("union Z {\n int n;\n} x;"); 12295 verifyFormat("class MACRO Z {\n} n;"); 12296 verifyFormat("class MACRO(X) Z {\n} n;"); 12297 verifyFormat("class __attribute__(X) Z {\n} n;"); 12298 verifyFormat("class __declspec(X) Z {\n} n;"); 12299 verifyFormat("class A##B##C {\n} n;"); 12300 verifyFormat("class alignas(16) Z {\n} n;"); 12301 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 12302 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 12303 12304 // Redefinition from nested context: 12305 verifyFormat("class A::B::C {\n} n;"); 12306 12307 // Template definitions. 12308 verifyFormat( 12309 "template <typename F>\n" 12310 "Matcher(const Matcher<F> &Other,\n" 12311 " typename enable_if_c<is_base_of<F, T>::value &&\n" 12312 " !is_same<F, T>::value>::type * = 0)\n" 12313 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 12314 12315 // FIXME: This is still incorrectly handled at the formatter side. 12316 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 12317 verifyFormat("int i = SomeFunction(a<b, a> b);"); 12318 12319 // FIXME: 12320 // This now gets parsed incorrectly as class definition. 12321 // verifyFormat("class A<int> f() {\n}\nint n;"); 12322 12323 // Elaborate types where incorrectly parsing the structural element would 12324 // break the indent. 12325 verifyFormat("if (true)\n" 12326 " class X x;\n" 12327 "else\n" 12328 " f();\n"); 12329 12330 // This is simply incomplete. Formatting is not important, but must not crash. 12331 verifyFormat("class A:"); 12332 } 12333 12334 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 12335 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 12336 format("#error Leave all white!!!!! space* alone!\n")); 12337 EXPECT_EQ( 12338 "#warning Leave all white!!!!! space* alone!\n", 12339 format("#warning Leave all white!!!!! space* alone!\n")); 12340 EXPECT_EQ("#error 1", format(" # error 1")); 12341 EXPECT_EQ("#warning 1", format(" # warning 1")); 12342 } 12343 12344 TEST_F(FormatTest, FormatHashIfExpressions) { 12345 verifyFormat("#if AAAA && BBBB"); 12346 verifyFormat("#if (AAAA && BBBB)"); 12347 verifyFormat("#elif (AAAA && BBBB)"); 12348 // FIXME: Come up with a better indentation for #elif. 12349 verifyFormat( 12350 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 12351 " defined(BBBBBBBB)\n" 12352 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 12353 " defined(BBBBBBBB)\n" 12354 "#endif", 12355 getLLVMStyleWithColumns(65)); 12356 } 12357 12358 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 12359 FormatStyle AllowsMergedIf = getGoogleStyle(); 12360 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 12361 FormatStyle::SIS_WithoutElse; 12362 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 12363 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 12364 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 12365 EXPECT_EQ("if (true) return 42;", 12366 format("if (true)\nreturn 42;", AllowsMergedIf)); 12367 FormatStyle ShortMergedIf = AllowsMergedIf; 12368 ShortMergedIf.ColumnLimit = 25; 12369 verifyFormat("#define A \\\n" 12370 " if (true) return 42;", 12371 ShortMergedIf); 12372 verifyFormat("#define A \\\n" 12373 " f(); \\\n" 12374 " if (true)\n" 12375 "#define B", 12376 ShortMergedIf); 12377 verifyFormat("#define A \\\n" 12378 " f(); \\\n" 12379 " if (true)\n" 12380 "g();", 12381 ShortMergedIf); 12382 verifyFormat("{\n" 12383 "#ifdef A\n" 12384 " // Comment\n" 12385 " if (true) continue;\n" 12386 "#endif\n" 12387 " // Comment\n" 12388 " if (true) continue;\n" 12389 "}", 12390 ShortMergedIf); 12391 ShortMergedIf.ColumnLimit = 33; 12392 verifyFormat("#define A \\\n" 12393 " if constexpr (true) return 42;", 12394 ShortMergedIf); 12395 verifyFormat("#define A \\\n" 12396 " if CONSTEXPR (true) return 42;", 12397 ShortMergedIf); 12398 ShortMergedIf.ColumnLimit = 29; 12399 verifyFormat("#define A \\\n" 12400 " if (aaaaaaaaaa) return 1; \\\n" 12401 " return 2;", 12402 ShortMergedIf); 12403 ShortMergedIf.ColumnLimit = 28; 12404 verifyFormat("#define A \\\n" 12405 " if (aaaaaaaaaa) \\\n" 12406 " return 1; \\\n" 12407 " return 2;", 12408 ShortMergedIf); 12409 verifyFormat("#define A \\\n" 12410 " if constexpr (aaaaaaa) \\\n" 12411 " return 1; \\\n" 12412 " return 2;", 12413 ShortMergedIf); 12414 verifyFormat("#define A \\\n" 12415 " if CONSTEXPR (aaaaaaa) \\\n" 12416 " return 1; \\\n" 12417 " return 2;", 12418 ShortMergedIf); 12419 } 12420 12421 TEST_F(FormatTest, FormatStarDependingOnContext) { 12422 verifyFormat("void f(int *a);"); 12423 verifyFormat("void f() { f(fint * b); }"); 12424 verifyFormat("class A {\n void f(int *a);\n};"); 12425 verifyFormat("class A {\n int *a;\n};"); 12426 verifyFormat("namespace a {\n" 12427 "namespace b {\n" 12428 "class A {\n" 12429 " void f() {}\n" 12430 " int *a;\n" 12431 "};\n" 12432 "} // namespace b\n" 12433 "} // namespace a"); 12434 } 12435 12436 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 12437 verifyFormat("while"); 12438 verifyFormat("operator"); 12439 } 12440 12441 TEST_F(FormatTest, SkipsDeeplyNestedLines) { 12442 // This code would be painfully slow to format if we didn't skip it. 12443 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 12444 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12445 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12446 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12447 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12448 "A(1, 1)\n" 12449 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x 12450 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12451 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12452 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12453 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12454 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12455 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12456 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12457 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12458 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n"); 12459 // Deeply nested part is untouched, rest is formatted. 12460 EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n", 12461 format(std::string("int i;\n") + Code + "int j;\n", 12462 getLLVMStyle(), SC_ExpectIncomplete)); 12463 } 12464 12465 //===----------------------------------------------------------------------===// 12466 // Objective-C tests. 12467 //===----------------------------------------------------------------------===// 12468 12469 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 12470 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 12471 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 12472 format("-(NSUInteger)indexOfObject:(id)anObject;")); 12473 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 12474 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 12475 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 12476 format("-(NSInteger)Method3:(id)anObject;")); 12477 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 12478 format("-(NSInteger)Method4:(id)anObject;")); 12479 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 12480 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 12481 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 12482 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 12483 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 12484 "forAllCells:(BOOL)flag;", 12485 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 12486 "forAllCells:(BOOL)flag;")); 12487 12488 // Very long objectiveC method declaration. 12489 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 12490 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 12491 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 12492 " inRange:(NSRange)range\n" 12493 " outRange:(NSRange)out_range\n" 12494 " outRange1:(NSRange)out_range1\n" 12495 " outRange2:(NSRange)out_range2\n" 12496 " outRange3:(NSRange)out_range3\n" 12497 " outRange4:(NSRange)out_range4\n" 12498 " outRange5:(NSRange)out_range5\n" 12499 " outRange6:(NSRange)out_range6\n" 12500 " outRange7:(NSRange)out_range7\n" 12501 " outRange8:(NSRange)out_range8\n" 12502 " outRange9:(NSRange)out_range9;"); 12503 12504 // When the function name has to be wrapped. 12505 FormatStyle Style = getLLVMStyle(); 12506 // ObjC ignores IndentWrappedFunctionNames when wrapping methods 12507 // and always indents instead. 12508 Style.IndentWrappedFunctionNames = false; 12509 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 12510 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 12511 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 12512 "}", 12513 Style); 12514 Style.IndentWrappedFunctionNames = true; 12515 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 12516 " veryLooooooooooongName:(NSString)cccccccccccccc\n" 12517 " anotherName:(NSString)dddddddddddddd {\n" 12518 "}", 12519 Style); 12520 12521 verifyFormat("- (int)sum:(vector<int>)numbers;"); 12522 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 12523 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 12524 // protocol lists (but not for template classes): 12525 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 12526 12527 verifyFormat("- (int (*)())foo:(int (*)())f;"); 12528 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 12529 12530 // If there's no return type (very rare in practice!), LLVM and Google style 12531 // agree. 12532 verifyFormat("- foo;"); 12533 verifyFormat("- foo:(int)f;"); 12534 verifyGoogleFormat("- foo:(int)foo;"); 12535 } 12536 12537 TEST_F(FormatTest, BreaksStringLiterals) { 12538 EXPECT_EQ("\"some text \"\n" 12539 "\"other\";", 12540 format("\"some text other\";", getLLVMStyleWithColumns(12))); 12541 EXPECT_EQ("\"some text \"\n" 12542 "\"other\";", 12543 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 12544 EXPECT_EQ( 12545 "#define A \\\n" 12546 " \"some \" \\\n" 12547 " \"text \" \\\n" 12548 " \"other\";", 12549 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 12550 EXPECT_EQ( 12551 "#define A \\\n" 12552 " \"so \" \\\n" 12553 " \"text \" \\\n" 12554 " \"other\";", 12555 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 12556 12557 EXPECT_EQ("\"some text\"", 12558 format("\"some text\"", getLLVMStyleWithColumns(1))); 12559 EXPECT_EQ("\"some text\"", 12560 format("\"some text\"", getLLVMStyleWithColumns(11))); 12561 EXPECT_EQ("\"some \"\n" 12562 "\"text\"", 12563 format("\"some text\"", getLLVMStyleWithColumns(10))); 12564 EXPECT_EQ("\"some \"\n" 12565 "\"text\"", 12566 format("\"some text\"", getLLVMStyleWithColumns(7))); 12567 EXPECT_EQ("\"some\"\n" 12568 "\" tex\"\n" 12569 "\"t\"", 12570 format("\"some text\"", getLLVMStyleWithColumns(6))); 12571 EXPECT_EQ("\"some\"\n" 12572 "\" tex\"\n" 12573 "\" and\"", 12574 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 12575 EXPECT_EQ("\"some\"\n" 12576 "\"/tex\"\n" 12577 "\"/and\"", 12578 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 12579 12580 EXPECT_EQ("variable =\n" 12581 " \"long string \"\n" 12582 " \"literal\";", 12583 format("variable = \"long string literal\";", 12584 getLLVMStyleWithColumns(20))); 12585 12586 EXPECT_EQ("variable = f(\n" 12587 " \"long string \"\n" 12588 " \"literal\",\n" 12589 " short,\n" 12590 " loooooooooooooooooooong);", 12591 format("variable = f(\"long string literal\", short, " 12592 "loooooooooooooooooooong);", 12593 getLLVMStyleWithColumns(20))); 12594 12595 EXPECT_EQ( 12596 "f(g(\"long string \"\n" 12597 " \"literal\"),\n" 12598 " b);", 12599 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 12600 EXPECT_EQ("f(g(\"long string \"\n" 12601 " \"literal\",\n" 12602 " a),\n" 12603 " b);", 12604 format("f(g(\"long string literal\", a), b);", 12605 getLLVMStyleWithColumns(20))); 12606 EXPECT_EQ( 12607 "f(\"one two\".split(\n" 12608 " variable));", 12609 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 12610 EXPECT_EQ("f(\"one two three four five six \"\n" 12611 " \"seven\".split(\n" 12612 " really_looooong_variable));", 12613 format("f(\"one two three four five six seven\"." 12614 "split(really_looooong_variable));", 12615 getLLVMStyleWithColumns(33))); 12616 12617 EXPECT_EQ("f(\"some \"\n" 12618 " \"text\",\n" 12619 " other);", 12620 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 12621 12622 // Only break as a last resort. 12623 verifyFormat( 12624 "aaaaaaaaaaaaaaaaaaaa(\n" 12625 " aaaaaaaaaaaaaaaaaaaa,\n" 12626 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 12627 12628 EXPECT_EQ("\"splitmea\"\n" 12629 "\"trandomp\"\n" 12630 "\"oint\"", 12631 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 12632 12633 EXPECT_EQ("\"split/\"\n" 12634 "\"pathat/\"\n" 12635 "\"slashes\"", 12636 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 12637 12638 EXPECT_EQ("\"split/\"\n" 12639 "\"pathat/\"\n" 12640 "\"slashes\"", 12641 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 12642 EXPECT_EQ("\"split at \"\n" 12643 "\"spaces/at/\"\n" 12644 "\"slashes.at.any$\"\n" 12645 "\"non-alphanumeric%\"\n" 12646 "\"1111111111characte\"\n" 12647 "\"rs\"", 12648 format("\"split at " 12649 "spaces/at/" 12650 "slashes.at." 12651 "any$non-" 12652 "alphanumeric%" 12653 "1111111111characte" 12654 "rs\"", 12655 getLLVMStyleWithColumns(20))); 12656 12657 // Verify that splitting the strings understands 12658 // Style::AlwaysBreakBeforeMultilineStrings. 12659 EXPECT_EQ("aaaaaaaaaaaa(\n" 12660 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 12661 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 12662 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 12663 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 12664 "aaaaaaaaaaaaaaaaaaaaaa\");", 12665 getGoogleStyle())); 12666 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 12667 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 12668 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 12669 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 12670 "aaaaaaaaaaaaaaaaaaaaaa\";", 12671 getGoogleStyle())); 12672 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 12673 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 12674 format("llvm::outs() << " 12675 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 12676 "aaaaaaaaaaaaaaaaaaa\";")); 12677 EXPECT_EQ("ffff(\n" 12678 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 12679 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 12680 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 12681 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 12682 getGoogleStyle())); 12683 12684 FormatStyle Style = getLLVMStyleWithColumns(12); 12685 Style.BreakStringLiterals = false; 12686 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 12687 12688 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 12689 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 12690 EXPECT_EQ("#define A \\\n" 12691 " \"some \" \\\n" 12692 " \"text \" \\\n" 12693 " \"other\";", 12694 format("#define A \"some text other\";", AlignLeft)); 12695 } 12696 12697 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) { 12698 EXPECT_EQ("C a = \"some more \"\n" 12699 " \"text\";", 12700 format("C a = \"some more text\";", getLLVMStyleWithColumns(18))); 12701 } 12702 12703 TEST_F(FormatTest, FullyRemoveEmptyLines) { 12704 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 12705 NoEmptyLines.MaxEmptyLinesToKeep = 0; 12706 EXPECT_EQ("int i = a(b());", 12707 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 12708 } 12709 12710 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 12711 EXPECT_EQ( 12712 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 12713 "(\n" 12714 " \"x\t\");", 12715 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 12716 "aaaaaaa(" 12717 "\"x\t\");")); 12718 } 12719 12720 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 12721 EXPECT_EQ( 12722 "u8\"utf8 string \"\n" 12723 "u8\"literal\";", 12724 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 12725 EXPECT_EQ( 12726 "u\"utf16 string \"\n" 12727 "u\"literal\";", 12728 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 12729 EXPECT_EQ( 12730 "U\"utf32 string \"\n" 12731 "U\"literal\";", 12732 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 12733 EXPECT_EQ("L\"wide string \"\n" 12734 "L\"literal\";", 12735 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 12736 EXPECT_EQ("@\"NSString \"\n" 12737 "@\"literal\";", 12738 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 12739 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26)); 12740 12741 // This input makes clang-format try to split the incomplete unicode escape 12742 // sequence, which used to lead to a crasher. 12743 verifyNoCrash( 12744 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 12745 getLLVMStyleWithColumns(60)); 12746 } 12747 12748 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 12749 FormatStyle Style = getGoogleStyleWithColumns(15); 12750 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 12751 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 12752 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 12753 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 12754 EXPECT_EQ("u8R\"x(raw literal)x\";", 12755 format("u8R\"x(raw literal)x\";", Style)); 12756 } 12757 12758 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 12759 FormatStyle Style = getLLVMStyleWithColumns(20); 12760 EXPECT_EQ( 12761 "_T(\"aaaaaaaaaaaaaa\")\n" 12762 "_T(\"aaaaaaaaaaaaaa\")\n" 12763 "_T(\"aaaaaaaaaaaa\")", 12764 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 12765 EXPECT_EQ("f(x,\n" 12766 " _T(\"aaaaaaaaaaaa\")\n" 12767 " _T(\"aaa\"),\n" 12768 " z);", 12769 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 12770 12771 // FIXME: Handle embedded spaces in one iteration. 12772 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 12773 // "_T(\"aaaaaaaaaaaaa\")\n" 12774 // "_T(\"aaaaaaaaaaaaa\")\n" 12775 // "_T(\"a\")", 12776 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 12777 // getLLVMStyleWithColumns(20))); 12778 EXPECT_EQ( 12779 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 12780 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 12781 EXPECT_EQ("f(\n" 12782 "#if !TEST\n" 12783 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 12784 "#endif\n" 12785 ");", 12786 format("f(\n" 12787 "#if !TEST\n" 12788 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 12789 "#endif\n" 12790 ");")); 12791 EXPECT_EQ("f(\n" 12792 "\n" 12793 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 12794 format("f(\n" 12795 "\n" 12796 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 12797 } 12798 12799 TEST_F(FormatTest, BreaksStringLiteralOperands) { 12800 // In a function call with two operands, the second can be broken with no line 12801 // break before it. 12802 EXPECT_EQ( 12803 "func(a, \"long long \"\n" 12804 " \"long long\");", 12805 format("func(a, \"long long long long\");", getLLVMStyleWithColumns(24))); 12806 // In a function call with three operands, the second must be broken with a 12807 // line break before it. 12808 EXPECT_EQ("func(a,\n" 12809 " \"long long long \"\n" 12810 " \"long\",\n" 12811 " c);", 12812 format("func(a, \"long long long long\", c);", 12813 getLLVMStyleWithColumns(24))); 12814 // In a function call with three operands, the third must be broken with a 12815 // line break before it. 12816 EXPECT_EQ("func(a, b,\n" 12817 " \"long long long \"\n" 12818 " \"long\");", 12819 format("func(a, b, \"long long long long\");", 12820 getLLVMStyleWithColumns(24))); 12821 // In a function call with three operands, both the second and the third must 12822 // be broken with a line break before them. 12823 EXPECT_EQ("func(a,\n" 12824 " \"long long long \"\n" 12825 " \"long\",\n" 12826 " \"long long long \"\n" 12827 " \"long\");", 12828 format("func(a, \"long long long long\", \"long long long long\");", 12829 getLLVMStyleWithColumns(24))); 12830 // In a chain of << with two operands, the second can be broken with no line 12831 // break before it. 12832 EXPECT_EQ("a << \"line line \"\n" 12833 " \"line\";", 12834 format("a << \"line line line\";", getLLVMStyleWithColumns(20))); 12835 // In a chain of << with three operands, the second can be broken with no line 12836 // break before it. 12837 EXPECT_EQ( 12838 "abcde << \"line \"\n" 12839 " \"line line\"\n" 12840 " << c;", 12841 format("abcde << \"line line line\" << c;", getLLVMStyleWithColumns(20))); 12842 // In a chain of << with three operands, the third must be broken with a line 12843 // break before it. 12844 EXPECT_EQ( 12845 "a << b\n" 12846 " << \"line line \"\n" 12847 " \"line\";", 12848 format("a << b << \"line line line\";", getLLVMStyleWithColumns(20))); 12849 // In a chain of << with three operands, the second can be broken with no line 12850 // break before it and the third must be broken with a line break before it. 12851 EXPECT_EQ("abcd << \"line line \"\n" 12852 " \"line\"\n" 12853 " << \"line line \"\n" 12854 " \"line\";", 12855 format("abcd << \"line line line\" << \"line line line\";", 12856 getLLVMStyleWithColumns(20))); 12857 // In a chain of binary operators with two operands, the second can be broken 12858 // with no line break before it. 12859 EXPECT_EQ( 12860 "abcd + \"line line \"\n" 12861 " \"line line\";", 12862 format("abcd + \"line line line line\";", getLLVMStyleWithColumns(20))); 12863 // In a chain of binary operators with three operands, the second must be 12864 // broken with a line break before it. 12865 EXPECT_EQ("abcd +\n" 12866 " \"line line \"\n" 12867 " \"line line\" +\n" 12868 " e;", 12869 format("abcd + \"line line line line\" + e;", 12870 getLLVMStyleWithColumns(20))); 12871 // In a function call with two operands, with AlignAfterOpenBracket enabled, 12872 // the first must be broken with a line break before it. 12873 FormatStyle Style = getLLVMStyleWithColumns(25); 12874 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 12875 EXPECT_EQ("someFunction(\n" 12876 " \"long long long \"\n" 12877 " \"long\",\n" 12878 " a);", 12879 format("someFunction(\"long long long long\", a);", Style)); 12880 } 12881 12882 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 12883 EXPECT_EQ( 12884 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 12885 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 12886 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 12887 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 12888 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 12889 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 12890 } 12891 12892 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 12893 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 12894 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 12895 EXPECT_EQ("fffffffffff(g(R\"x(\n" 12896 "multiline raw string literal xxxxxxxxxxxxxx\n" 12897 ")x\",\n" 12898 " a),\n" 12899 " b);", 12900 format("fffffffffff(g(R\"x(\n" 12901 "multiline raw string literal xxxxxxxxxxxxxx\n" 12902 ")x\", a), b);", 12903 getGoogleStyleWithColumns(20))); 12904 EXPECT_EQ("fffffffffff(\n" 12905 " g(R\"x(qqq\n" 12906 "multiline raw string literal xxxxxxxxxxxxxx\n" 12907 ")x\",\n" 12908 " a),\n" 12909 " b);", 12910 format("fffffffffff(g(R\"x(qqq\n" 12911 "multiline raw string literal xxxxxxxxxxxxxx\n" 12912 ")x\", a), b);", 12913 getGoogleStyleWithColumns(20))); 12914 12915 EXPECT_EQ("fffffffffff(R\"x(\n" 12916 "multiline raw string literal xxxxxxxxxxxxxx\n" 12917 ")x\");", 12918 format("fffffffffff(R\"x(\n" 12919 "multiline raw string literal xxxxxxxxxxxxxx\n" 12920 ")x\");", 12921 getGoogleStyleWithColumns(20))); 12922 EXPECT_EQ("fffffffffff(R\"x(\n" 12923 "multiline raw string literal xxxxxxxxxxxxxx\n" 12924 ")x\" + bbbbbb);", 12925 format("fffffffffff(R\"x(\n" 12926 "multiline raw string literal xxxxxxxxxxxxxx\n" 12927 ")x\" + bbbbbb);", 12928 getGoogleStyleWithColumns(20))); 12929 EXPECT_EQ("fffffffffff(\n" 12930 " R\"x(\n" 12931 "multiline raw string literal xxxxxxxxxxxxxx\n" 12932 ")x\" +\n" 12933 " bbbbbb);", 12934 format("fffffffffff(\n" 12935 " R\"x(\n" 12936 "multiline raw string literal xxxxxxxxxxxxxx\n" 12937 ")x\" + bbbbbb);", 12938 getGoogleStyleWithColumns(20))); 12939 EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);", 12940 format("fffffffffff(\n" 12941 " R\"(single line raw string)\" + bbbbbb);")); 12942 } 12943 12944 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 12945 verifyFormat("string a = \"unterminated;"); 12946 EXPECT_EQ("function(\"unterminated,\n" 12947 " OtherParameter);", 12948 format("function( \"unterminated,\n" 12949 " OtherParameter);")); 12950 } 12951 12952 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 12953 FormatStyle Style = getLLVMStyle(); 12954 Style.Standard = FormatStyle::LS_Cpp03; 12955 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 12956 format("#define x(_a) printf(\"foo\"_a);", Style)); 12957 } 12958 12959 TEST_F(FormatTest, CppLexVersion) { 12960 FormatStyle Style = getLLVMStyle(); 12961 // Formatting of x * y differs if x is a type. 12962 verifyFormat("void foo() { MACRO(a * b); }", Style); 12963 verifyFormat("void foo() { MACRO(int *b); }", Style); 12964 12965 // LLVM style uses latest lexer. 12966 verifyFormat("void foo() { MACRO(char8_t *b); }", Style); 12967 Style.Standard = FormatStyle::LS_Cpp17; 12968 // But in c++17, char8_t isn't a keyword. 12969 verifyFormat("void foo() { MACRO(char8_t * b); }", Style); 12970 } 12971 12972 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 12973 12974 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 12975 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 12976 " \"ddeeefff\");", 12977 format("someFunction(\"aaabbbcccdddeeefff\");", 12978 getLLVMStyleWithColumns(25))); 12979 EXPECT_EQ("someFunction1234567890(\n" 12980 " \"aaabbbcccdddeeefff\");", 12981 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 12982 getLLVMStyleWithColumns(26))); 12983 EXPECT_EQ("someFunction1234567890(\n" 12984 " \"aaabbbcccdddeeeff\"\n" 12985 " \"f\");", 12986 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 12987 getLLVMStyleWithColumns(25))); 12988 EXPECT_EQ("someFunction1234567890(\n" 12989 " \"aaabbbcccdddeeeff\"\n" 12990 " \"f\");", 12991 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 12992 getLLVMStyleWithColumns(24))); 12993 EXPECT_EQ("someFunction(\n" 12994 " \"aaabbbcc ddde \"\n" 12995 " \"efff\");", 12996 format("someFunction(\"aaabbbcc ddde efff\");", 12997 getLLVMStyleWithColumns(25))); 12998 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 12999 " \"ddeeefff\");", 13000 format("someFunction(\"aaabbbccc ddeeefff\");", 13001 getLLVMStyleWithColumns(25))); 13002 EXPECT_EQ("someFunction1234567890(\n" 13003 " \"aaabb \"\n" 13004 " \"cccdddeeefff\");", 13005 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 13006 getLLVMStyleWithColumns(25))); 13007 EXPECT_EQ("#define A \\\n" 13008 " string s = \\\n" 13009 " \"123456789\" \\\n" 13010 " \"0\"; \\\n" 13011 " int i;", 13012 format("#define A string s = \"1234567890\"; int i;", 13013 getLLVMStyleWithColumns(20))); 13014 EXPECT_EQ("someFunction(\n" 13015 " \"aaabbbcc \"\n" 13016 " \"dddeeefff\");", 13017 format("someFunction(\"aaabbbcc dddeeefff\");", 13018 getLLVMStyleWithColumns(25))); 13019 } 13020 13021 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 13022 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 13023 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 13024 EXPECT_EQ("\"test\"\n" 13025 "\"\\n\"", 13026 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 13027 EXPECT_EQ("\"tes\\\\\"\n" 13028 "\"n\"", 13029 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 13030 EXPECT_EQ("\"\\\\\\\\\"\n" 13031 "\"\\n\"", 13032 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 13033 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 13034 EXPECT_EQ("\"\\uff01\"\n" 13035 "\"test\"", 13036 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 13037 EXPECT_EQ("\"\\Uff01ff02\"", 13038 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 13039 EXPECT_EQ("\"\\x000000000001\"\n" 13040 "\"next\"", 13041 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 13042 EXPECT_EQ("\"\\x000000000001next\"", 13043 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 13044 EXPECT_EQ("\"\\x000000000001\"", 13045 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 13046 EXPECT_EQ("\"test\"\n" 13047 "\"\\000000\"\n" 13048 "\"000001\"", 13049 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 13050 EXPECT_EQ("\"test\\000\"\n" 13051 "\"00000000\"\n" 13052 "\"1\"", 13053 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 13054 } 13055 13056 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 13057 verifyFormat("void f() {\n" 13058 " return g() {}\n" 13059 " void h() {}"); 13060 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 13061 "g();\n" 13062 "}"); 13063 } 13064 13065 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 13066 verifyFormat( 13067 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 13068 } 13069 13070 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 13071 verifyFormat("class X {\n" 13072 " void f() {\n" 13073 " }\n" 13074 "};", 13075 getLLVMStyleWithColumns(12)); 13076 } 13077 13078 TEST_F(FormatTest, ConfigurableIndentWidth) { 13079 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 13080 EightIndent.IndentWidth = 8; 13081 EightIndent.ContinuationIndentWidth = 8; 13082 verifyFormat("void f() {\n" 13083 " someFunction();\n" 13084 " if (true) {\n" 13085 " f();\n" 13086 " }\n" 13087 "}", 13088 EightIndent); 13089 verifyFormat("class X {\n" 13090 " void f() {\n" 13091 " }\n" 13092 "};", 13093 EightIndent); 13094 verifyFormat("int x[] = {\n" 13095 " call(),\n" 13096 " call()};", 13097 EightIndent); 13098 } 13099 13100 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 13101 verifyFormat("double\n" 13102 "f();", 13103 getLLVMStyleWithColumns(8)); 13104 } 13105 13106 TEST_F(FormatTest, ConfigurableUseOfTab) { 13107 FormatStyle Tab = getLLVMStyleWithColumns(42); 13108 Tab.IndentWidth = 8; 13109 Tab.UseTab = FormatStyle::UT_Always; 13110 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left; 13111 13112 EXPECT_EQ("if (aaaaaaaa && // q\n" 13113 " bb)\t\t// w\n" 13114 "\t;", 13115 format("if (aaaaaaaa &&// q\n" 13116 "bb)// w\n" 13117 ";", 13118 Tab)); 13119 EXPECT_EQ("if (aaa && bbb) // w\n" 13120 "\t;", 13121 format("if(aaa&&bbb)// w\n" 13122 ";", 13123 Tab)); 13124 13125 verifyFormat("class X {\n" 13126 "\tvoid f() {\n" 13127 "\t\tsomeFunction(parameter1,\n" 13128 "\t\t\t parameter2);\n" 13129 "\t}\n" 13130 "};", 13131 Tab); 13132 verifyFormat("#define A \\\n" 13133 "\tvoid f() { \\\n" 13134 "\t\tsomeFunction( \\\n" 13135 "\t\t parameter1, \\\n" 13136 "\t\t parameter2); \\\n" 13137 "\t}", 13138 Tab); 13139 verifyFormat("int a;\t // x\n" 13140 "int bbbbbbbb; // x\n", 13141 Tab); 13142 13143 Tab.TabWidth = 4; 13144 Tab.IndentWidth = 8; 13145 verifyFormat("class TabWidth4Indent8 {\n" 13146 "\t\tvoid f() {\n" 13147 "\t\t\t\tsomeFunction(parameter1,\n" 13148 "\t\t\t\t\t\t\t parameter2);\n" 13149 "\t\t}\n" 13150 "};", 13151 Tab); 13152 13153 Tab.TabWidth = 4; 13154 Tab.IndentWidth = 4; 13155 verifyFormat("class TabWidth4Indent4 {\n" 13156 "\tvoid f() {\n" 13157 "\t\tsomeFunction(parameter1,\n" 13158 "\t\t\t\t\t parameter2);\n" 13159 "\t}\n" 13160 "};", 13161 Tab); 13162 13163 Tab.TabWidth = 8; 13164 Tab.IndentWidth = 4; 13165 verifyFormat("class TabWidth8Indent4 {\n" 13166 " void f() {\n" 13167 "\tsomeFunction(parameter1,\n" 13168 "\t\t parameter2);\n" 13169 " }\n" 13170 "};", 13171 Tab); 13172 13173 Tab.TabWidth = 8; 13174 Tab.IndentWidth = 8; 13175 EXPECT_EQ("/*\n" 13176 "\t a\t\tcomment\n" 13177 "\t in multiple lines\n" 13178 " */", 13179 format(" /*\t \t \n" 13180 " \t \t a\t\tcomment\t \t\n" 13181 " \t \t in multiple lines\t\n" 13182 " \t */", 13183 Tab)); 13184 13185 Tab.UseTab = FormatStyle::UT_ForIndentation; 13186 verifyFormat("{\n" 13187 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13188 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13189 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13190 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13191 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13192 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13193 "};", 13194 Tab); 13195 verifyFormat("enum AA {\n" 13196 "\ta1, // Force multiple lines\n" 13197 "\ta2,\n" 13198 "\ta3\n" 13199 "};", 13200 Tab); 13201 EXPECT_EQ("if (aaaaaaaa && // q\n" 13202 " bb) // w\n" 13203 "\t;", 13204 format("if (aaaaaaaa &&// q\n" 13205 "bb)// w\n" 13206 ";", 13207 Tab)); 13208 verifyFormat("class X {\n" 13209 "\tvoid f() {\n" 13210 "\t\tsomeFunction(parameter1,\n" 13211 "\t\t parameter2);\n" 13212 "\t}\n" 13213 "};", 13214 Tab); 13215 verifyFormat("{\n" 13216 "\tQ(\n" 13217 "\t {\n" 13218 "\t\t int a;\n" 13219 "\t\t someFunction(aaaaaaaa,\n" 13220 "\t\t bbbbbbb);\n" 13221 "\t },\n" 13222 "\t p);\n" 13223 "}", 13224 Tab); 13225 EXPECT_EQ("{\n" 13226 "\t/* aaaa\n" 13227 "\t bbbb */\n" 13228 "}", 13229 format("{\n" 13230 "/* aaaa\n" 13231 " bbbb */\n" 13232 "}", 13233 Tab)); 13234 EXPECT_EQ("{\n" 13235 "\t/*\n" 13236 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13237 "\t bbbbbbbbbbbbb\n" 13238 "\t*/\n" 13239 "}", 13240 format("{\n" 13241 "/*\n" 13242 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13243 "*/\n" 13244 "}", 13245 Tab)); 13246 EXPECT_EQ("{\n" 13247 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13248 "\t// bbbbbbbbbbbbb\n" 13249 "}", 13250 format("{\n" 13251 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13252 "}", 13253 Tab)); 13254 EXPECT_EQ("{\n" 13255 "\t/*\n" 13256 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13257 "\t bbbbbbbbbbbbb\n" 13258 "\t*/\n" 13259 "}", 13260 format("{\n" 13261 "\t/*\n" 13262 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13263 "\t*/\n" 13264 "}", 13265 Tab)); 13266 EXPECT_EQ("{\n" 13267 "\t/*\n" 13268 "\n" 13269 "\t*/\n" 13270 "}", 13271 format("{\n" 13272 "\t/*\n" 13273 "\n" 13274 "\t*/\n" 13275 "}", 13276 Tab)); 13277 EXPECT_EQ("{\n" 13278 "\t/*\n" 13279 " asdf\n" 13280 "\t*/\n" 13281 "}", 13282 format("{\n" 13283 "\t/*\n" 13284 " asdf\n" 13285 "\t*/\n" 13286 "}", 13287 Tab)); 13288 13289 Tab.UseTab = FormatStyle::UT_Never; 13290 EXPECT_EQ("/*\n" 13291 " a\t\tcomment\n" 13292 " in multiple lines\n" 13293 " */", 13294 format(" /*\t \t \n" 13295 " \t \t a\t\tcomment\t \t\n" 13296 " \t \t in multiple lines\t\n" 13297 " \t */", 13298 Tab)); 13299 EXPECT_EQ("/* some\n" 13300 " comment */", 13301 format(" \t \t /* some\n" 13302 " \t \t comment */", 13303 Tab)); 13304 EXPECT_EQ("int a; /* some\n" 13305 " comment */", 13306 format(" \t \t int a; /* some\n" 13307 " \t \t comment */", 13308 Tab)); 13309 13310 EXPECT_EQ("int a; /* some\n" 13311 "comment */", 13312 format(" \t \t int\ta; /* some\n" 13313 " \t \t comment */", 13314 Tab)); 13315 EXPECT_EQ("f(\"\t\t\"); /* some\n" 13316 " comment */", 13317 format(" \t \t f(\"\t\t\"); /* some\n" 13318 " \t \t comment */", 13319 Tab)); 13320 EXPECT_EQ("{\n" 13321 " /*\n" 13322 " * Comment\n" 13323 " */\n" 13324 " int i;\n" 13325 "}", 13326 format("{\n" 13327 "\t/*\n" 13328 "\t * Comment\n" 13329 "\t */\n" 13330 "\t int i;\n" 13331 "}", 13332 Tab)); 13333 13334 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 13335 Tab.TabWidth = 8; 13336 Tab.IndentWidth = 8; 13337 EXPECT_EQ("if (aaaaaaaa && // q\n" 13338 " bb) // w\n" 13339 "\t;", 13340 format("if (aaaaaaaa &&// q\n" 13341 "bb)// w\n" 13342 ";", 13343 Tab)); 13344 EXPECT_EQ("if (aaa && bbb) // w\n" 13345 "\t;", 13346 format("if(aaa&&bbb)// w\n" 13347 ";", 13348 Tab)); 13349 verifyFormat("class X {\n" 13350 "\tvoid f() {\n" 13351 "\t\tsomeFunction(parameter1,\n" 13352 "\t\t\t parameter2);\n" 13353 "\t}\n" 13354 "};", 13355 Tab); 13356 verifyFormat("#define A \\\n" 13357 "\tvoid f() { \\\n" 13358 "\t\tsomeFunction( \\\n" 13359 "\t\t parameter1, \\\n" 13360 "\t\t parameter2); \\\n" 13361 "\t}", 13362 Tab); 13363 Tab.TabWidth = 4; 13364 Tab.IndentWidth = 8; 13365 verifyFormat("class TabWidth4Indent8 {\n" 13366 "\t\tvoid f() {\n" 13367 "\t\t\t\tsomeFunction(parameter1,\n" 13368 "\t\t\t\t\t\t\t parameter2);\n" 13369 "\t\t}\n" 13370 "};", 13371 Tab); 13372 Tab.TabWidth = 4; 13373 Tab.IndentWidth = 4; 13374 verifyFormat("class TabWidth4Indent4 {\n" 13375 "\tvoid f() {\n" 13376 "\t\tsomeFunction(parameter1,\n" 13377 "\t\t\t\t\t parameter2);\n" 13378 "\t}\n" 13379 "};", 13380 Tab); 13381 Tab.TabWidth = 8; 13382 Tab.IndentWidth = 4; 13383 verifyFormat("class TabWidth8Indent4 {\n" 13384 " void f() {\n" 13385 "\tsomeFunction(parameter1,\n" 13386 "\t\t parameter2);\n" 13387 " }\n" 13388 "};", 13389 Tab); 13390 Tab.TabWidth = 8; 13391 Tab.IndentWidth = 8; 13392 EXPECT_EQ("/*\n" 13393 "\t a\t\tcomment\n" 13394 "\t in multiple lines\n" 13395 " */", 13396 format(" /*\t \t \n" 13397 " \t \t a\t\tcomment\t \t\n" 13398 " \t \t in multiple lines\t\n" 13399 " \t */", 13400 Tab)); 13401 verifyFormat("{\n" 13402 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13403 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13404 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13405 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13406 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13407 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13408 "};", 13409 Tab); 13410 verifyFormat("enum AA {\n" 13411 "\ta1, // Force multiple lines\n" 13412 "\ta2,\n" 13413 "\ta3\n" 13414 "};", 13415 Tab); 13416 EXPECT_EQ("if (aaaaaaaa && // q\n" 13417 " bb) // w\n" 13418 "\t;", 13419 format("if (aaaaaaaa &&// q\n" 13420 "bb)// w\n" 13421 ";", 13422 Tab)); 13423 verifyFormat("class X {\n" 13424 "\tvoid f() {\n" 13425 "\t\tsomeFunction(parameter1,\n" 13426 "\t\t\t parameter2);\n" 13427 "\t}\n" 13428 "};", 13429 Tab); 13430 verifyFormat("{\n" 13431 "\tQ(\n" 13432 "\t {\n" 13433 "\t\t int a;\n" 13434 "\t\t someFunction(aaaaaaaa,\n" 13435 "\t\t\t\t bbbbbbb);\n" 13436 "\t },\n" 13437 "\t p);\n" 13438 "}", 13439 Tab); 13440 EXPECT_EQ("{\n" 13441 "\t/* aaaa\n" 13442 "\t bbbb */\n" 13443 "}", 13444 format("{\n" 13445 "/* aaaa\n" 13446 " bbbb */\n" 13447 "}", 13448 Tab)); 13449 EXPECT_EQ("{\n" 13450 "\t/*\n" 13451 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13452 "\t bbbbbbbbbbbbb\n" 13453 "\t*/\n" 13454 "}", 13455 format("{\n" 13456 "/*\n" 13457 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13458 "*/\n" 13459 "}", 13460 Tab)); 13461 EXPECT_EQ("{\n" 13462 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13463 "\t// bbbbbbbbbbbbb\n" 13464 "}", 13465 format("{\n" 13466 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13467 "}", 13468 Tab)); 13469 EXPECT_EQ("{\n" 13470 "\t/*\n" 13471 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13472 "\t bbbbbbbbbbbbb\n" 13473 "\t*/\n" 13474 "}", 13475 format("{\n" 13476 "\t/*\n" 13477 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13478 "\t*/\n" 13479 "}", 13480 Tab)); 13481 EXPECT_EQ("{\n" 13482 "\t/*\n" 13483 "\n" 13484 "\t*/\n" 13485 "}", 13486 format("{\n" 13487 "\t/*\n" 13488 "\n" 13489 "\t*/\n" 13490 "}", 13491 Tab)); 13492 EXPECT_EQ("{\n" 13493 "\t/*\n" 13494 " asdf\n" 13495 "\t*/\n" 13496 "}", 13497 format("{\n" 13498 "\t/*\n" 13499 " asdf\n" 13500 "\t*/\n" 13501 "}", 13502 Tab)); 13503 EXPECT_EQ("/* some\n" 13504 " comment */", 13505 format(" \t \t /* some\n" 13506 " \t \t comment */", 13507 Tab)); 13508 EXPECT_EQ("int a; /* some\n" 13509 " comment */", 13510 format(" \t \t int a; /* some\n" 13511 " \t \t comment */", 13512 Tab)); 13513 EXPECT_EQ("int a; /* some\n" 13514 "comment */", 13515 format(" \t \t int\ta; /* some\n" 13516 " \t \t comment */", 13517 Tab)); 13518 EXPECT_EQ("f(\"\t\t\"); /* some\n" 13519 " comment */", 13520 format(" \t \t f(\"\t\t\"); /* some\n" 13521 " \t \t comment */", 13522 Tab)); 13523 EXPECT_EQ("{\n" 13524 "\t/*\n" 13525 "\t * Comment\n" 13526 "\t */\n" 13527 "\tint i;\n" 13528 "}", 13529 format("{\n" 13530 "\t/*\n" 13531 "\t * Comment\n" 13532 "\t */\n" 13533 "\t int i;\n" 13534 "}", 13535 Tab)); 13536 Tab.TabWidth = 2; 13537 Tab.IndentWidth = 2; 13538 EXPECT_EQ("{\n" 13539 "\t/* aaaa\n" 13540 "\t\t bbbb */\n" 13541 "}", 13542 format("{\n" 13543 "/* aaaa\n" 13544 "\t bbbb */\n" 13545 "}", 13546 Tab)); 13547 EXPECT_EQ("{\n" 13548 "\t/*\n" 13549 "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13550 "\t\tbbbbbbbbbbbbb\n" 13551 "\t*/\n" 13552 "}", 13553 format("{\n" 13554 "/*\n" 13555 "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13556 "*/\n" 13557 "}", 13558 Tab)); 13559 Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 13560 Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 13561 Tab.TabWidth = 4; 13562 Tab.IndentWidth = 4; 13563 verifyFormat("class Assign {\n" 13564 "\tvoid f() {\n" 13565 "\t\tint x = 123;\n" 13566 "\t\tint random = 4;\n" 13567 "\t\tstd::string alphabet =\n" 13568 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 13569 "\t}\n" 13570 "};", 13571 Tab); 13572 13573 Tab.UseTab = FormatStyle::UT_AlignWithSpaces; 13574 Tab.TabWidth = 8; 13575 Tab.IndentWidth = 8; 13576 EXPECT_EQ("if (aaaaaaaa && // q\n" 13577 " bb) // w\n" 13578 "\t;", 13579 format("if (aaaaaaaa &&// q\n" 13580 "bb)// w\n" 13581 ";", 13582 Tab)); 13583 EXPECT_EQ("if (aaa && bbb) // w\n" 13584 "\t;", 13585 format("if(aaa&&bbb)// w\n" 13586 ";", 13587 Tab)); 13588 verifyFormat("class X {\n" 13589 "\tvoid f() {\n" 13590 "\t\tsomeFunction(parameter1,\n" 13591 "\t\t parameter2);\n" 13592 "\t}\n" 13593 "};", 13594 Tab); 13595 verifyFormat("#define A \\\n" 13596 "\tvoid f() { \\\n" 13597 "\t\tsomeFunction( \\\n" 13598 "\t\t parameter1, \\\n" 13599 "\t\t parameter2); \\\n" 13600 "\t}", 13601 Tab); 13602 Tab.TabWidth = 4; 13603 Tab.IndentWidth = 8; 13604 verifyFormat("class TabWidth4Indent8 {\n" 13605 "\t\tvoid f() {\n" 13606 "\t\t\t\tsomeFunction(parameter1,\n" 13607 "\t\t\t\t parameter2);\n" 13608 "\t\t}\n" 13609 "};", 13610 Tab); 13611 Tab.TabWidth = 4; 13612 Tab.IndentWidth = 4; 13613 verifyFormat("class TabWidth4Indent4 {\n" 13614 "\tvoid f() {\n" 13615 "\t\tsomeFunction(parameter1,\n" 13616 "\t\t parameter2);\n" 13617 "\t}\n" 13618 "};", 13619 Tab); 13620 Tab.TabWidth = 8; 13621 Tab.IndentWidth = 4; 13622 verifyFormat("class TabWidth8Indent4 {\n" 13623 " void f() {\n" 13624 "\tsomeFunction(parameter1,\n" 13625 "\t parameter2);\n" 13626 " }\n" 13627 "};", 13628 Tab); 13629 Tab.TabWidth = 8; 13630 Tab.IndentWidth = 8; 13631 EXPECT_EQ("/*\n" 13632 " a\t\tcomment\n" 13633 " in multiple lines\n" 13634 " */", 13635 format(" /*\t \t \n" 13636 " \t \t a\t\tcomment\t \t\n" 13637 " \t \t in multiple lines\t\n" 13638 " \t */", 13639 Tab)); 13640 verifyFormat("{\n" 13641 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13642 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13643 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13644 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13645 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13646 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13647 "};", 13648 Tab); 13649 verifyFormat("enum AA {\n" 13650 "\ta1, // Force multiple lines\n" 13651 "\ta2,\n" 13652 "\ta3\n" 13653 "};", 13654 Tab); 13655 EXPECT_EQ("if (aaaaaaaa && // q\n" 13656 " bb) // w\n" 13657 "\t;", 13658 format("if (aaaaaaaa &&// q\n" 13659 "bb)// w\n" 13660 ";", 13661 Tab)); 13662 verifyFormat("class X {\n" 13663 "\tvoid f() {\n" 13664 "\t\tsomeFunction(parameter1,\n" 13665 "\t\t parameter2);\n" 13666 "\t}\n" 13667 "};", 13668 Tab); 13669 verifyFormat("{\n" 13670 "\tQ(\n" 13671 "\t {\n" 13672 "\t\t int a;\n" 13673 "\t\t someFunction(aaaaaaaa,\n" 13674 "\t\t bbbbbbb);\n" 13675 "\t },\n" 13676 "\t p);\n" 13677 "}", 13678 Tab); 13679 EXPECT_EQ("{\n" 13680 "\t/* aaaa\n" 13681 "\t bbbb */\n" 13682 "}", 13683 format("{\n" 13684 "/* aaaa\n" 13685 " bbbb */\n" 13686 "}", 13687 Tab)); 13688 EXPECT_EQ("{\n" 13689 "\t/*\n" 13690 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13691 "\t bbbbbbbbbbbbb\n" 13692 "\t*/\n" 13693 "}", 13694 format("{\n" 13695 "/*\n" 13696 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13697 "*/\n" 13698 "}", 13699 Tab)); 13700 EXPECT_EQ("{\n" 13701 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13702 "\t// bbbbbbbbbbbbb\n" 13703 "}", 13704 format("{\n" 13705 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13706 "}", 13707 Tab)); 13708 EXPECT_EQ("{\n" 13709 "\t/*\n" 13710 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13711 "\t bbbbbbbbbbbbb\n" 13712 "\t*/\n" 13713 "}", 13714 format("{\n" 13715 "\t/*\n" 13716 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13717 "\t*/\n" 13718 "}", 13719 Tab)); 13720 EXPECT_EQ("{\n" 13721 "\t/*\n" 13722 "\n" 13723 "\t*/\n" 13724 "}", 13725 format("{\n" 13726 "\t/*\n" 13727 "\n" 13728 "\t*/\n" 13729 "}", 13730 Tab)); 13731 EXPECT_EQ("{\n" 13732 "\t/*\n" 13733 " asdf\n" 13734 "\t*/\n" 13735 "}", 13736 format("{\n" 13737 "\t/*\n" 13738 " asdf\n" 13739 "\t*/\n" 13740 "}", 13741 Tab)); 13742 EXPECT_EQ("/* some\n" 13743 " comment */", 13744 format(" \t \t /* some\n" 13745 " \t \t comment */", 13746 Tab)); 13747 EXPECT_EQ("int a; /* some\n" 13748 " comment */", 13749 format(" \t \t int a; /* some\n" 13750 " \t \t comment */", 13751 Tab)); 13752 EXPECT_EQ("int a; /* some\n" 13753 "comment */", 13754 format(" \t \t int\ta; /* some\n" 13755 " \t \t comment */", 13756 Tab)); 13757 EXPECT_EQ("f(\"\t\t\"); /* some\n" 13758 " comment */", 13759 format(" \t \t f(\"\t\t\"); /* some\n" 13760 " \t \t comment */", 13761 Tab)); 13762 EXPECT_EQ("{\n" 13763 "\t/*\n" 13764 "\t * Comment\n" 13765 "\t */\n" 13766 "\tint i;\n" 13767 "}", 13768 format("{\n" 13769 "\t/*\n" 13770 "\t * Comment\n" 13771 "\t */\n" 13772 "\t int i;\n" 13773 "}", 13774 Tab)); 13775 Tab.TabWidth = 2; 13776 Tab.IndentWidth = 2; 13777 EXPECT_EQ("{\n" 13778 "\t/* aaaa\n" 13779 "\t bbbb */\n" 13780 "}", 13781 format("{\n" 13782 "/* aaaa\n" 13783 " bbbb */\n" 13784 "}", 13785 Tab)); 13786 EXPECT_EQ("{\n" 13787 "\t/*\n" 13788 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13789 "\t bbbbbbbbbbbbb\n" 13790 "\t*/\n" 13791 "}", 13792 format("{\n" 13793 "/*\n" 13794 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13795 "*/\n" 13796 "}", 13797 Tab)); 13798 Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 13799 Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 13800 Tab.TabWidth = 4; 13801 Tab.IndentWidth = 4; 13802 verifyFormat("class Assign {\n" 13803 "\tvoid f() {\n" 13804 "\t\tint x = 123;\n" 13805 "\t\tint random = 4;\n" 13806 "\t\tstd::string alphabet =\n" 13807 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 13808 "\t}\n" 13809 "};", 13810 Tab); 13811 Tab.AlignOperands = FormatStyle::OAS_Align; 13812 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n" 13813 " cccccccccccccccccccc;", 13814 Tab); 13815 // no alignment 13816 verifyFormat("int aaaaaaaaaa =\n" 13817 "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 13818 Tab); 13819 verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n" 13820 " : bbbbbbbbbbbbbb ? 222222222222222\n" 13821 " : 333333333333333;", 13822 Tab); 13823 Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 13824 Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator; 13825 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n" 13826 " + cccccccccccccccccccc;", 13827 Tab); 13828 } 13829 13830 TEST_F(FormatTest, ZeroTabWidth) { 13831 FormatStyle Tab = getLLVMStyleWithColumns(42); 13832 Tab.IndentWidth = 8; 13833 Tab.UseTab = FormatStyle::UT_Never; 13834 Tab.TabWidth = 0; 13835 EXPECT_EQ("void a(){\n" 13836 " // line starts with '\t'\n" 13837 "};", 13838 format("void a(){\n" 13839 "\t// line starts with '\t'\n" 13840 "};", 13841 Tab)); 13842 13843 EXPECT_EQ("void a(){\n" 13844 " // line starts with '\t'\n" 13845 "};", 13846 format("void a(){\n" 13847 "\t\t// line starts with '\t'\n" 13848 "};", 13849 Tab)); 13850 13851 Tab.UseTab = FormatStyle::UT_ForIndentation; 13852 EXPECT_EQ("void a(){\n" 13853 " // line starts with '\t'\n" 13854 "};", 13855 format("void a(){\n" 13856 "\t// line starts with '\t'\n" 13857 "};", 13858 Tab)); 13859 13860 EXPECT_EQ("void a(){\n" 13861 " // line starts with '\t'\n" 13862 "};", 13863 format("void a(){\n" 13864 "\t\t// line starts with '\t'\n" 13865 "};", 13866 Tab)); 13867 13868 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 13869 EXPECT_EQ("void a(){\n" 13870 " // line starts with '\t'\n" 13871 "};", 13872 format("void a(){\n" 13873 "\t// line starts with '\t'\n" 13874 "};", 13875 Tab)); 13876 13877 EXPECT_EQ("void a(){\n" 13878 " // line starts with '\t'\n" 13879 "};", 13880 format("void a(){\n" 13881 "\t\t// line starts with '\t'\n" 13882 "};", 13883 Tab)); 13884 13885 Tab.UseTab = FormatStyle::UT_AlignWithSpaces; 13886 EXPECT_EQ("void a(){\n" 13887 " // line starts with '\t'\n" 13888 "};", 13889 format("void a(){\n" 13890 "\t// line starts with '\t'\n" 13891 "};", 13892 Tab)); 13893 13894 EXPECT_EQ("void a(){\n" 13895 " // line starts with '\t'\n" 13896 "};", 13897 format("void a(){\n" 13898 "\t\t// line starts with '\t'\n" 13899 "};", 13900 Tab)); 13901 13902 Tab.UseTab = FormatStyle::UT_Always; 13903 EXPECT_EQ("void a(){\n" 13904 "// line starts with '\t'\n" 13905 "};", 13906 format("void a(){\n" 13907 "\t// line starts with '\t'\n" 13908 "};", 13909 Tab)); 13910 13911 EXPECT_EQ("void a(){\n" 13912 "// line starts with '\t'\n" 13913 "};", 13914 format("void a(){\n" 13915 "\t\t// line starts with '\t'\n" 13916 "};", 13917 Tab)); 13918 } 13919 13920 TEST_F(FormatTest, CalculatesOriginalColumn) { 13921 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13922 "q\"; /* some\n" 13923 " comment */", 13924 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13925 "q\"; /* some\n" 13926 " comment */", 13927 getLLVMStyle())); 13928 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 13929 "/* some\n" 13930 " comment */", 13931 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 13932 " /* some\n" 13933 " comment */", 13934 getLLVMStyle())); 13935 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13936 "qqq\n" 13937 "/* some\n" 13938 " comment */", 13939 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13940 "qqq\n" 13941 " /* some\n" 13942 " comment */", 13943 getLLVMStyle())); 13944 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13945 "wwww; /* some\n" 13946 " comment */", 13947 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13948 "wwww; /* some\n" 13949 " comment */", 13950 getLLVMStyle())); 13951 } 13952 13953 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 13954 FormatStyle NoSpace = getLLVMStyle(); 13955 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 13956 13957 verifyFormat("while(true)\n" 13958 " continue;", 13959 NoSpace); 13960 verifyFormat("for(;;)\n" 13961 " continue;", 13962 NoSpace); 13963 verifyFormat("if(true)\n" 13964 " f();\n" 13965 "else if(true)\n" 13966 " f();", 13967 NoSpace); 13968 verifyFormat("do {\n" 13969 " do_something();\n" 13970 "} while(something());", 13971 NoSpace); 13972 verifyFormat("switch(x) {\n" 13973 "default:\n" 13974 " break;\n" 13975 "}", 13976 NoSpace); 13977 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 13978 verifyFormat("size_t x = sizeof(x);", NoSpace); 13979 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 13980 verifyFormat("auto f(int x) -> typeof(x);", NoSpace); 13981 verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace); 13982 verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace); 13983 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 13984 verifyFormat("alignas(128) char a[128];", NoSpace); 13985 verifyFormat("size_t x = alignof(MyType);", NoSpace); 13986 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 13987 verifyFormat("int f() throw(Deprecated);", NoSpace); 13988 verifyFormat("typedef void (*cb)(int);", NoSpace); 13989 verifyFormat("T A::operator()();", NoSpace); 13990 verifyFormat("X A::operator++(T);", NoSpace); 13991 verifyFormat("auto lambda = []() { return 0; };", NoSpace); 13992 13993 FormatStyle Space = getLLVMStyle(); 13994 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 13995 13996 verifyFormat("int f ();", Space); 13997 verifyFormat("void f (int a, T b) {\n" 13998 " while (true)\n" 13999 " continue;\n" 14000 "}", 14001 Space); 14002 verifyFormat("if (true)\n" 14003 " f ();\n" 14004 "else if (true)\n" 14005 " f ();", 14006 Space); 14007 verifyFormat("do {\n" 14008 " do_something ();\n" 14009 "} while (something ());", 14010 Space); 14011 verifyFormat("switch (x) {\n" 14012 "default:\n" 14013 " break;\n" 14014 "}", 14015 Space); 14016 verifyFormat("A::A () : a (1) {}", Space); 14017 verifyFormat("void f () __attribute__ ((asdf));", Space); 14018 verifyFormat("*(&a + 1);\n" 14019 "&((&a)[1]);\n" 14020 "a[(b + c) * d];\n" 14021 "(((a + 1) * 2) + 3) * 4;", 14022 Space); 14023 verifyFormat("#define A(x) x", Space); 14024 verifyFormat("#define A (x) x", Space); 14025 verifyFormat("#if defined(x)\n" 14026 "#endif", 14027 Space); 14028 verifyFormat("auto i = std::make_unique<int> (5);", Space); 14029 verifyFormat("size_t x = sizeof (x);", Space); 14030 verifyFormat("auto f (int x) -> decltype (x);", Space); 14031 verifyFormat("auto f (int x) -> typeof (x);", Space); 14032 verifyFormat("auto f (int x) -> _Atomic (x);", Space); 14033 verifyFormat("auto f (int x) -> __underlying_type (x);", Space); 14034 verifyFormat("int f (T x) noexcept (x.create ());", Space); 14035 verifyFormat("alignas (128) char a[128];", Space); 14036 verifyFormat("size_t x = alignof (MyType);", Space); 14037 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 14038 verifyFormat("int f () throw (Deprecated);", Space); 14039 verifyFormat("typedef void (*cb) (int);", Space); 14040 verifyFormat("T A::operator() ();", Space); 14041 verifyFormat("X A::operator++ (T);", Space); 14042 verifyFormat("auto lambda = [] () { return 0; };", Space); 14043 verifyFormat("int x = int (y);", Space); 14044 14045 FormatStyle SomeSpace = getLLVMStyle(); 14046 SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses; 14047 14048 verifyFormat("[]() -> float {}", SomeSpace); 14049 verifyFormat("[] (auto foo) {}", SomeSpace); 14050 verifyFormat("[foo]() -> int {}", SomeSpace); 14051 verifyFormat("int f();", SomeSpace); 14052 verifyFormat("void f (int a, T b) {\n" 14053 " while (true)\n" 14054 " continue;\n" 14055 "}", 14056 SomeSpace); 14057 verifyFormat("if (true)\n" 14058 " f();\n" 14059 "else if (true)\n" 14060 " f();", 14061 SomeSpace); 14062 verifyFormat("do {\n" 14063 " do_something();\n" 14064 "} while (something());", 14065 SomeSpace); 14066 verifyFormat("switch (x) {\n" 14067 "default:\n" 14068 " break;\n" 14069 "}", 14070 SomeSpace); 14071 verifyFormat("A::A() : a (1) {}", SomeSpace); 14072 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace); 14073 verifyFormat("*(&a + 1);\n" 14074 "&((&a)[1]);\n" 14075 "a[(b + c) * d];\n" 14076 "(((a + 1) * 2) + 3) * 4;", 14077 SomeSpace); 14078 verifyFormat("#define A(x) x", SomeSpace); 14079 verifyFormat("#define A (x) x", SomeSpace); 14080 verifyFormat("#if defined(x)\n" 14081 "#endif", 14082 SomeSpace); 14083 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace); 14084 verifyFormat("size_t x = sizeof (x);", SomeSpace); 14085 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace); 14086 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace); 14087 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace); 14088 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace); 14089 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace); 14090 verifyFormat("alignas (128) char a[128];", SomeSpace); 14091 verifyFormat("size_t x = alignof (MyType);", SomeSpace); 14092 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", 14093 SomeSpace); 14094 verifyFormat("int f() throw (Deprecated);", SomeSpace); 14095 verifyFormat("typedef void (*cb) (int);", SomeSpace); 14096 verifyFormat("T A::operator()();", SomeSpace); 14097 verifyFormat("X A::operator++ (T);", SomeSpace); 14098 verifyFormat("int x = int (y);", SomeSpace); 14099 verifyFormat("auto lambda = []() { return 0; };", SomeSpace); 14100 } 14101 14102 TEST_F(FormatTest, SpaceAfterLogicalNot) { 14103 FormatStyle Spaces = getLLVMStyle(); 14104 Spaces.SpaceAfterLogicalNot = true; 14105 14106 verifyFormat("bool x = ! y", Spaces); 14107 verifyFormat("if (! isFailure())", Spaces); 14108 verifyFormat("if (! (a && b))", Spaces); 14109 verifyFormat("\"Error!\"", Spaces); 14110 verifyFormat("! ! x", Spaces); 14111 } 14112 14113 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 14114 FormatStyle Spaces = getLLVMStyle(); 14115 14116 Spaces.SpacesInParentheses = true; 14117 verifyFormat("do_something( ::globalVar );", Spaces); 14118 verifyFormat("call( x, y, z );", Spaces); 14119 verifyFormat("call();", Spaces); 14120 verifyFormat("std::function<void( int, int )> callback;", Spaces); 14121 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 14122 Spaces); 14123 verifyFormat("while ( (bool)1 )\n" 14124 " continue;", 14125 Spaces); 14126 verifyFormat("for ( ;; )\n" 14127 " continue;", 14128 Spaces); 14129 verifyFormat("if ( true )\n" 14130 " f();\n" 14131 "else if ( true )\n" 14132 " f();", 14133 Spaces); 14134 verifyFormat("do {\n" 14135 " do_something( (int)i );\n" 14136 "} while ( something() );", 14137 Spaces); 14138 verifyFormat("switch ( x ) {\n" 14139 "default:\n" 14140 " break;\n" 14141 "}", 14142 Spaces); 14143 14144 Spaces.SpacesInParentheses = false; 14145 Spaces.SpacesInCStyleCastParentheses = true; 14146 verifyFormat("Type *A = ( Type * )P;", Spaces); 14147 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 14148 verifyFormat("x = ( int32 )y;", Spaces); 14149 verifyFormat("int a = ( int )(2.0f);", Spaces); 14150 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 14151 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 14152 verifyFormat("#define x (( int )-1)", Spaces); 14153 14154 // Run the first set of tests again with: 14155 Spaces.SpacesInParentheses = false; 14156 Spaces.SpaceInEmptyParentheses = true; 14157 Spaces.SpacesInCStyleCastParentheses = true; 14158 verifyFormat("call(x, y, z);", Spaces); 14159 verifyFormat("call( );", Spaces); 14160 verifyFormat("std::function<void(int, int)> callback;", Spaces); 14161 verifyFormat("while (( bool )1)\n" 14162 " continue;", 14163 Spaces); 14164 verifyFormat("for (;;)\n" 14165 " continue;", 14166 Spaces); 14167 verifyFormat("if (true)\n" 14168 " f( );\n" 14169 "else if (true)\n" 14170 " f( );", 14171 Spaces); 14172 verifyFormat("do {\n" 14173 " do_something(( int )i);\n" 14174 "} while (something( ));", 14175 Spaces); 14176 verifyFormat("switch (x) {\n" 14177 "default:\n" 14178 " break;\n" 14179 "}", 14180 Spaces); 14181 14182 // Run the first set of tests again with: 14183 Spaces.SpaceAfterCStyleCast = true; 14184 verifyFormat("call(x, y, z);", Spaces); 14185 verifyFormat("call( );", Spaces); 14186 verifyFormat("std::function<void(int, int)> callback;", Spaces); 14187 verifyFormat("while (( bool ) 1)\n" 14188 " continue;", 14189 Spaces); 14190 verifyFormat("for (;;)\n" 14191 " continue;", 14192 Spaces); 14193 verifyFormat("if (true)\n" 14194 " f( );\n" 14195 "else if (true)\n" 14196 " f( );", 14197 Spaces); 14198 verifyFormat("do {\n" 14199 " do_something(( int ) i);\n" 14200 "} while (something( ));", 14201 Spaces); 14202 verifyFormat("switch (x) {\n" 14203 "default:\n" 14204 " break;\n" 14205 "}", 14206 Spaces); 14207 14208 // Run subset of tests again with: 14209 Spaces.SpacesInCStyleCastParentheses = false; 14210 Spaces.SpaceAfterCStyleCast = true; 14211 verifyFormat("while ((bool) 1)\n" 14212 " continue;", 14213 Spaces); 14214 verifyFormat("do {\n" 14215 " do_something((int) i);\n" 14216 "} while (something( ));", 14217 Spaces); 14218 14219 verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces); 14220 verifyFormat("size_t idx = (size_t) a;", Spaces); 14221 verifyFormat("size_t idx = (size_t) (a - 1);", Spaces); 14222 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); 14223 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); 14224 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); 14225 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); 14226 Spaces.ColumnLimit = 80; 14227 Spaces.IndentWidth = 4; 14228 Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 14229 verifyFormat("void foo( ) {\n" 14230 " size_t foo = (*(function))(\n" 14231 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " 14232 "BarrrrrrrrrrrrLong,\n" 14233 " FoooooooooLooooong);\n" 14234 "}", 14235 Spaces); 14236 Spaces.SpaceAfterCStyleCast = false; 14237 verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces); 14238 verifyFormat("size_t idx = (size_t)a;", Spaces); 14239 verifyFormat("size_t idx = (size_t)(a - 1);", Spaces); 14240 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); 14241 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); 14242 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); 14243 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); 14244 14245 verifyFormat("void foo( ) {\n" 14246 " size_t foo = (*(function))(\n" 14247 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " 14248 "BarrrrrrrrrrrrLong,\n" 14249 " FoooooooooLooooong);\n" 14250 "}", 14251 Spaces); 14252 } 14253 14254 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 14255 verifyFormat("int a[5];"); 14256 verifyFormat("a[3] += 42;"); 14257 14258 FormatStyle Spaces = getLLVMStyle(); 14259 Spaces.SpacesInSquareBrackets = true; 14260 // Not lambdas. 14261 verifyFormat("int a[ 5 ];", Spaces); 14262 verifyFormat("a[ 3 ] += 42;", Spaces); 14263 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 14264 verifyFormat("double &operator[](int i) { return 0; }\n" 14265 "int i;", 14266 Spaces); 14267 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 14268 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 14269 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 14270 // Lambdas. 14271 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 14272 verifyFormat("return [ i, args... ] {};", Spaces); 14273 verifyFormat("int foo = [ &bar ]() {};", Spaces); 14274 verifyFormat("int foo = [ = ]() {};", Spaces); 14275 verifyFormat("int foo = [ & ]() {};", Spaces); 14276 verifyFormat("int foo = [ =, &bar ]() {};", Spaces); 14277 verifyFormat("int foo = [ &bar, = ]() {};", Spaces); 14278 } 14279 14280 TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) { 14281 FormatStyle NoSpaceStyle = getLLVMStyle(); 14282 verifyFormat("int a[5];", NoSpaceStyle); 14283 verifyFormat("a[3] += 42;", NoSpaceStyle); 14284 14285 verifyFormat("int a[1];", NoSpaceStyle); 14286 verifyFormat("int 1 [a];", NoSpaceStyle); 14287 verifyFormat("int a[1][2];", NoSpaceStyle); 14288 verifyFormat("a[7] = 5;", NoSpaceStyle); 14289 verifyFormat("int a = (f())[23];", NoSpaceStyle); 14290 verifyFormat("f([] {})", NoSpaceStyle); 14291 14292 FormatStyle Space = getLLVMStyle(); 14293 Space.SpaceBeforeSquareBrackets = true; 14294 verifyFormat("int c = []() -> int { return 2; }();\n", Space); 14295 verifyFormat("return [i, args...] {};", Space); 14296 14297 verifyFormat("int a [5];", Space); 14298 verifyFormat("a [3] += 42;", Space); 14299 verifyFormat("constexpr char hello []{\"hello\"};", Space); 14300 verifyFormat("double &operator[](int i) { return 0; }\n" 14301 "int i;", 14302 Space); 14303 verifyFormat("std::unique_ptr<int []> foo() {}", Space); 14304 verifyFormat("int i = a [a][a]->f();", Space); 14305 verifyFormat("int i = (*b) [a]->f();", Space); 14306 14307 verifyFormat("int a [1];", Space); 14308 verifyFormat("int 1 [a];", Space); 14309 verifyFormat("int a [1][2];", Space); 14310 verifyFormat("a [7] = 5;", Space); 14311 verifyFormat("int a = (f()) [23];", Space); 14312 verifyFormat("f([] {})", Space); 14313 } 14314 14315 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 14316 verifyFormat("int a = 5;"); 14317 verifyFormat("a += 42;"); 14318 verifyFormat("a or_eq 8;"); 14319 14320 FormatStyle Spaces = getLLVMStyle(); 14321 Spaces.SpaceBeforeAssignmentOperators = false; 14322 verifyFormat("int a= 5;", Spaces); 14323 verifyFormat("a+= 42;", Spaces); 14324 verifyFormat("a or_eq 8;", Spaces); 14325 } 14326 14327 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) { 14328 verifyFormat("class Foo : public Bar {};"); 14329 verifyFormat("Foo::Foo() : foo(1) {}"); 14330 verifyFormat("for (auto a : b) {\n}"); 14331 verifyFormat("int x = a ? b : c;"); 14332 verifyFormat("{\n" 14333 "label0:\n" 14334 " int x = 0;\n" 14335 "}"); 14336 verifyFormat("switch (x) {\n" 14337 "case 1:\n" 14338 "default:\n" 14339 "}"); 14340 verifyFormat("switch (allBraces) {\n" 14341 "case 1: {\n" 14342 " break;\n" 14343 "}\n" 14344 "case 2: {\n" 14345 " [[fallthrough]];\n" 14346 "}\n" 14347 "default: {\n" 14348 " break;\n" 14349 "}\n" 14350 "}"); 14351 14352 FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30); 14353 CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false; 14354 verifyFormat("class Foo : public Bar {};", CtorInitializerStyle); 14355 verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle); 14356 verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle); 14357 verifyFormat("int x = a ? b : c;", CtorInitializerStyle); 14358 verifyFormat("{\n" 14359 "label1:\n" 14360 " int x = 0;\n" 14361 "}", 14362 CtorInitializerStyle); 14363 verifyFormat("switch (x) {\n" 14364 "case 1:\n" 14365 "default:\n" 14366 "}", 14367 CtorInitializerStyle); 14368 verifyFormat("switch (allBraces) {\n" 14369 "case 1: {\n" 14370 " break;\n" 14371 "}\n" 14372 "case 2: {\n" 14373 " [[fallthrough]];\n" 14374 "}\n" 14375 "default: {\n" 14376 " break;\n" 14377 "}\n" 14378 "}", 14379 CtorInitializerStyle); 14380 CtorInitializerStyle.BreakConstructorInitializers = 14381 FormatStyle::BCIS_AfterColon; 14382 verifyFormat("Fooooooooooo::Fooooooooooo():\n" 14383 " aaaaaaaaaaaaaaaa(1),\n" 14384 " bbbbbbbbbbbbbbbb(2) {}", 14385 CtorInitializerStyle); 14386 CtorInitializerStyle.BreakConstructorInitializers = 14387 FormatStyle::BCIS_BeforeComma; 14388 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 14389 " : aaaaaaaaaaaaaaaa(1)\n" 14390 " , bbbbbbbbbbbbbbbb(2) {}", 14391 CtorInitializerStyle); 14392 CtorInitializerStyle.BreakConstructorInitializers = 14393 FormatStyle::BCIS_BeforeColon; 14394 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 14395 " : aaaaaaaaaaaaaaaa(1),\n" 14396 " bbbbbbbbbbbbbbbb(2) {}", 14397 CtorInitializerStyle); 14398 CtorInitializerStyle.ConstructorInitializerIndentWidth = 0; 14399 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 14400 ": aaaaaaaaaaaaaaaa(1),\n" 14401 " bbbbbbbbbbbbbbbb(2) {}", 14402 CtorInitializerStyle); 14403 14404 FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30); 14405 InheritanceStyle.SpaceBeforeInheritanceColon = false; 14406 verifyFormat("class Foo: public Bar {};", InheritanceStyle); 14407 verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle); 14408 verifyFormat("for (auto a : b) {\n}", InheritanceStyle); 14409 verifyFormat("int x = a ? b : c;", InheritanceStyle); 14410 verifyFormat("{\n" 14411 "label2:\n" 14412 " int x = 0;\n" 14413 "}", 14414 InheritanceStyle); 14415 verifyFormat("switch (x) {\n" 14416 "case 1:\n" 14417 "default:\n" 14418 "}", 14419 InheritanceStyle); 14420 verifyFormat("switch (allBraces) {\n" 14421 "case 1: {\n" 14422 " break;\n" 14423 "}\n" 14424 "case 2: {\n" 14425 " [[fallthrough]];\n" 14426 "}\n" 14427 "default: {\n" 14428 " break;\n" 14429 "}\n" 14430 "}", 14431 InheritanceStyle); 14432 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma; 14433 verifyFormat("class Foooooooooooooooooooooo\n" 14434 " : public aaaaaaaaaaaaaaaaaa,\n" 14435 " public bbbbbbbbbbbbbbbbbb {\n" 14436 "}", 14437 InheritanceStyle); 14438 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon; 14439 verifyFormat("class Foooooooooooooooooooooo:\n" 14440 " public aaaaaaaaaaaaaaaaaa,\n" 14441 " public bbbbbbbbbbbbbbbbbb {\n" 14442 "}", 14443 InheritanceStyle); 14444 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 14445 verifyFormat("class Foooooooooooooooooooooo\n" 14446 " : public aaaaaaaaaaaaaaaaaa\n" 14447 " , public bbbbbbbbbbbbbbbbbb {\n" 14448 "}", 14449 InheritanceStyle); 14450 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 14451 verifyFormat("class Foooooooooooooooooooooo\n" 14452 " : public aaaaaaaaaaaaaaaaaa,\n" 14453 " public bbbbbbbbbbbbbbbbbb {\n" 14454 "}", 14455 InheritanceStyle); 14456 InheritanceStyle.ConstructorInitializerIndentWidth = 0; 14457 verifyFormat("class Foooooooooooooooooooooo\n" 14458 ": public aaaaaaaaaaaaaaaaaa,\n" 14459 " public bbbbbbbbbbbbbbbbbb {}", 14460 InheritanceStyle); 14461 14462 FormatStyle ForLoopStyle = getLLVMStyle(); 14463 ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false; 14464 verifyFormat("class Foo : public Bar {};", ForLoopStyle); 14465 verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle); 14466 verifyFormat("for (auto a: b) {\n}", ForLoopStyle); 14467 verifyFormat("int x = a ? b : c;", ForLoopStyle); 14468 verifyFormat("{\n" 14469 "label2:\n" 14470 " int x = 0;\n" 14471 "}", 14472 ForLoopStyle); 14473 verifyFormat("switch (x) {\n" 14474 "case 1:\n" 14475 "default:\n" 14476 "}", 14477 ForLoopStyle); 14478 verifyFormat("switch (allBraces) {\n" 14479 "case 1: {\n" 14480 " break;\n" 14481 "}\n" 14482 "case 2: {\n" 14483 " [[fallthrough]];\n" 14484 "}\n" 14485 "default: {\n" 14486 " break;\n" 14487 "}\n" 14488 "}", 14489 ForLoopStyle); 14490 14491 FormatStyle CaseStyle = getLLVMStyle(); 14492 CaseStyle.SpaceBeforeCaseColon = true; 14493 verifyFormat("class Foo : public Bar {};", CaseStyle); 14494 verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle); 14495 verifyFormat("for (auto a : b) {\n}", CaseStyle); 14496 verifyFormat("int x = a ? b : c;", CaseStyle); 14497 verifyFormat("switch (x) {\n" 14498 "case 1 :\n" 14499 "default :\n" 14500 "}", 14501 CaseStyle); 14502 verifyFormat("switch (allBraces) {\n" 14503 "case 1 : {\n" 14504 " break;\n" 14505 "}\n" 14506 "case 2 : {\n" 14507 " [[fallthrough]];\n" 14508 "}\n" 14509 "default : {\n" 14510 " break;\n" 14511 "}\n" 14512 "}", 14513 CaseStyle); 14514 14515 FormatStyle NoSpaceStyle = getLLVMStyle(); 14516 EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false); 14517 NoSpaceStyle.SpaceBeforeCtorInitializerColon = false; 14518 NoSpaceStyle.SpaceBeforeInheritanceColon = false; 14519 NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 14520 verifyFormat("class Foo: public Bar {};", NoSpaceStyle); 14521 verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle); 14522 verifyFormat("for (auto a: b) {\n}", NoSpaceStyle); 14523 verifyFormat("int x = a ? b : c;", NoSpaceStyle); 14524 verifyFormat("{\n" 14525 "label3:\n" 14526 " int x = 0;\n" 14527 "}", 14528 NoSpaceStyle); 14529 verifyFormat("switch (x) {\n" 14530 "case 1:\n" 14531 "default:\n" 14532 "}", 14533 NoSpaceStyle); 14534 verifyFormat("switch (allBraces) {\n" 14535 "case 1: {\n" 14536 " break;\n" 14537 "}\n" 14538 "case 2: {\n" 14539 " [[fallthrough]];\n" 14540 "}\n" 14541 "default: {\n" 14542 " break;\n" 14543 "}\n" 14544 "}", 14545 NoSpaceStyle); 14546 14547 FormatStyle InvertedSpaceStyle = getLLVMStyle(); 14548 InvertedSpaceStyle.SpaceBeforeCaseColon = true; 14549 InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false; 14550 InvertedSpaceStyle.SpaceBeforeInheritanceColon = false; 14551 InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 14552 verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle); 14553 verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle); 14554 verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle); 14555 verifyFormat("int x = a ? b : c;", InvertedSpaceStyle); 14556 verifyFormat("{\n" 14557 "label3:\n" 14558 " int x = 0;\n" 14559 "}", 14560 InvertedSpaceStyle); 14561 verifyFormat("switch (x) {\n" 14562 "case 1 :\n" 14563 "case 2 : {\n" 14564 " break;\n" 14565 "}\n" 14566 "default :\n" 14567 " break;\n" 14568 "}", 14569 InvertedSpaceStyle); 14570 verifyFormat("switch (allBraces) {\n" 14571 "case 1 : {\n" 14572 " break;\n" 14573 "}\n" 14574 "case 2 : {\n" 14575 " [[fallthrough]];\n" 14576 "}\n" 14577 "default : {\n" 14578 " break;\n" 14579 "}\n" 14580 "}", 14581 InvertedSpaceStyle); 14582 } 14583 14584 TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) { 14585 FormatStyle Style = getLLVMStyle(); 14586 14587 Style.PointerAlignment = FormatStyle::PAS_Left; 14588 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; 14589 verifyFormat("void* const* x = NULL;", Style); 14590 14591 #define verifyQualifierSpaces(Code, Pointers, Qualifiers) \ 14592 do { \ 14593 Style.PointerAlignment = FormatStyle::Pointers; \ 14594 Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers; \ 14595 verifyFormat(Code, Style); \ 14596 } while (false) 14597 14598 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default); 14599 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default); 14600 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default); 14601 14602 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before); 14603 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before); 14604 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before); 14605 14606 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After); 14607 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After); 14608 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After); 14609 14610 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both); 14611 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both); 14612 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both); 14613 14614 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default); 14615 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, 14616 SAPQ_Default); 14617 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 14618 SAPQ_Default); 14619 14620 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before); 14621 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, 14622 SAPQ_Before); 14623 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 14624 SAPQ_Before); 14625 14626 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After); 14627 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After); 14628 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 14629 SAPQ_After); 14630 14631 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both); 14632 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both); 14633 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both); 14634 14635 #undef verifyQualifierSpaces 14636 14637 FormatStyle Spaces = getLLVMStyle(); 14638 Spaces.AttributeMacros.push_back("qualified"); 14639 Spaces.PointerAlignment = FormatStyle::PAS_Right; 14640 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; 14641 verifyFormat("SomeType *volatile *a = NULL;", Spaces); 14642 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces); 14643 verifyFormat("std::vector<SomeType *const *> x;", Spaces); 14644 verifyFormat("std::vector<SomeType *qualified *> x;", Spaces); 14645 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 14646 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before; 14647 verifyFormat("SomeType * volatile *a = NULL;", Spaces); 14648 verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces); 14649 verifyFormat("std::vector<SomeType * const *> x;", Spaces); 14650 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces); 14651 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 14652 14653 // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left. 14654 Spaces.PointerAlignment = FormatStyle::PAS_Left; 14655 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before; 14656 verifyFormat("SomeType* volatile* a = NULL;", Spaces); 14657 verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces); 14658 verifyFormat("std::vector<SomeType* const*> x;", Spaces); 14659 verifyFormat("std::vector<SomeType* qualified*> x;", Spaces); 14660 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 14661 // However, setting it to SAPQ_After should add spaces after __attribute, etc. 14662 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After; 14663 verifyFormat("SomeType* volatile * a = NULL;", Spaces); 14664 verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces); 14665 verifyFormat("std::vector<SomeType* const *> x;", Spaces); 14666 verifyFormat("std::vector<SomeType* qualified *> x;", Spaces); 14667 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 14668 14669 // PAS_Middle should not have any noticeable changes even for SAPQ_Both 14670 Spaces.PointerAlignment = FormatStyle::PAS_Middle; 14671 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After; 14672 verifyFormat("SomeType * volatile * a = NULL;", Spaces); 14673 verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces); 14674 verifyFormat("std::vector<SomeType * const *> x;", Spaces); 14675 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces); 14676 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 14677 } 14678 14679 TEST_F(FormatTest, AlignConsecutiveMacros) { 14680 FormatStyle Style = getLLVMStyle(); 14681 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 14682 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 14683 Style.AlignConsecutiveMacros = FormatStyle::ACS_None; 14684 14685 verifyFormat("#define a 3\n" 14686 "#define bbbb 4\n" 14687 "#define ccc (5)", 14688 Style); 14689 14690 verifyFormat("#define f(x) (x * x)\n" 14691 "#define fff(x, y, z) (x * y + z)\n" 14692 "#define ffff(x, y) (x - y)", 14693 Style); 14694 14695 verifyFormat("#define foo(x, y) (x + y)\n" 14696 "#define bar (5, 6)(2 + 2)", 14697 Style); 14698 14699 verifyFormat("#define a 3\n" 14700 "#define bbbb 4\n" 14701 "#define ccc (5)\n" 14702 "#define f(x) (x * x)\n" 14703 "#define fff(x, y, z) (x * y + z)\n" 14704 "#define ffff(x, y) (x - y)", 14705 Style); 14706 14707 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 14708 verifyFormat("#define a 3\n" 14709 "#define bbbb 4\n" 14710 "#define ccc (5)", 14711 Style); 14712 14713 verifyFormat("#define f(x) (x * x)\n" 14714 "#define fff(x, y, z) (x * y + z)\n" 14715 "#define ffff(x, y) (x - y)", 14716 Style); 14717 14718 verifyFormat("#define foo(x, y) (x + y)\n" 14719 "#define bar (5, 6)(2 + 2)", 14720 Style); 14721 14722 verifyFormat("#define a 3\n" 14723 "#define bbbb 4\n" 14724 "#define ccc (5)\n" 14725 "#define f(x) (x * x)\n" 14726 "#define fff(x, y, z) (x * y + z)\n" 14727 "#define ffff(x, y) (x - y)", 14728 Style); 14729 14730 verifyFormat("#define a 5\n" 14731 "#define foo(x, y) (x + y)\n" 14732 "#define CCC (6)\n" 14733 "auto lambda = []() {\n" 14734 " auto ii = 0;\n" 14735 " float j = 0;\n" 14736 " return 0;\n" 14737 "};\n" 14738 "int i = 0;\n" 14739 "float i2 = 0;\n" 14740 "auto v = type{\n" 14741 " i = 1, //\n" 14742 " (i = 2), //\n" 14743 " i = 3 //\n" 14744 "};", 14745 Style); 14746 14747 Style.AlignConsecutiveMacros = FormatStyle::ACS_None; 14748 Style.ColumnLimit = 20; 14749 14750 verifyFormat("#define a \\\n" 14751 " \"aabbbbbbbbbbbb\"\n" 14752 "#define D \\\n" 14753 " \"aabbbbbbbbbbbb\" \\\n" 14754 " \"ccddeeeeeeeee\"\n" 14755 "#define B \\\n" 14756 " \"QQQQQQQQQQQQQ\" \\\n" 14757 " \"FFFFFFFFFFFFF\" \\\n" 14758 " \"LLLLLLLL\"\n", 14759 Style); 14760 14761 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 14762 verifyFormat("#define a \\\n" 14763 " \"aabbbbbbbbbbbb\"\n" 14764 "#define D \\\n" 14765 " \"aabbbbbbbbbbbb\" \\\n" 14766 " \"ccddeeeeeeeee\"\n" 14767 "#define B \\\n" 14768 " \"QQQQQQQQQQQQQ\" \\\n" 14769 " \"FFFFFFFFFFFFF\" \\\n" 14770 " \"LLLLLLLL\"\n", 14771 Style); 14772 14773 // Test across comments 14774 Style.MaxEmptyLinesToKeep = 10; 14775 Style.ReflowComments = false; 14776 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossComments; 14777 EXPECT_EQ("#define a 3\n" 14778 "// line comment\n" 14779 "#define bbbb 4\n" 14780 "#define ccc (5)", 14781 format("#define a 3\n" 14782 "// line comment\n" 14783 "#define bbbb 4\n" 14784 "#define ccc (5)", 14785 Style)); 14786 14787 EXPECT_EQ("#define a 3\n" 14788 "/* block comment */\n" 14789 "#define bbbb 4\n" 14790 "#define ccc (5)", 14791 format("#define a 3\n" 14792 "/* block comment */\n" 14793 "#define bbbb 4\n" 14794 "#define ccc (5)", 14795 Style)); 14796 14797 EXPECT_EQ("#define a 3\n" 14798 "/* multi-line *\n" 14799 " * block comment */\n" 14800 "#define bbbb 4\n" 14801 "#define ccc (5)", 14802 format("#define a 3\n" 14803 "/* multi-line *\n" 14804 " * block comment */\n" 14805 "#define bbbb 4\n" 14806 "#define ccc (5)", 14807 Style)); 14808 14809 EXPECT_EQ("#define a 3\n" 14810 "// multi-line line comment\n" 14811 "//\n" 14812 "#define bbbb 4\n" 14813 "#define ccc (5)", 14814 format("#define a 3\n" 14815 "// multi-line line comment\n" 14816 "//\n" 14817 "#define bbbb 4\n" 14818 "#define ccc (5)", 14819 Style)); 14820 14821 EXPECT_EQ("#define a 3\n" 14822 "// empty lines still break.\n" 14823 "\n" 14824 "#define bbbb 4\n" 14825 "#define ccc (5)", 14826 format("#define a 3\n" 14827 "// empty lines still break.\n" 14828 "\n" 14829 "#define bbbb 4\n" 14830 "#define ccc (5)", 14831 Style)); 14832 14833 // Test across empty lines 14834 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLines; 14835 EXPECT_EQ("#define a 3\n" 14836 "\n" 14837 "#define bbbb 4\n" 14838 "#define ccc (5)", 14839 format("#define a 3\n" 14840 "\n" 14841 "#define bbbb 4\n" 14842 "#define ccc (5)", 14843 Style)); 14844 14845 EXPECT_EQ("#define a 3\n" 14846 "\n" 14847 "\n" 14848 "\n" 14849 "#define bbbb 4\n" 14850 "#define ccc (5)", 14851 format("#define a 3\n" 14852 "\n" 14853 "\n" 14854 "\n" 14855 "#define bbbb 4\n" 14856 "#define ccc (5)", 14857 Style)); 14858 14859 EXPECT_EQ("#define a 3\n" 14860 "// comments should break alignment\n" 14861 "//\n" 14862 "#define bbbb 4\n" 14863 "#define ccc (5)", 14864 format("#define a 3\n" 14865 "// comments should break alignment\n" 14866 "//\n" 14867 "#define bbbb 4\n" 14868 "#define ccc (5)", 14869 Style)); 14870 14871 // Test across empty lines and comments 14872 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLinesAndComments; 14873 verifyFormat("#define a 3\n" 14874 "\n" 14875 "// line comment\n" 14876 "#define bbbb 4\n" 14877 "#define ccc (5)", 14878 Style); 14879 14880 EXPECT_EQ("#define a 3\n" 14881 "\n" 14882 "\n" 14883 "/* multi-line *\n" 14884 " * block comment */\n" 14885 "\n" 14886 "\n" 14887 "#define bbbb 4\n" 14888 "#define ccc (5)", 14889 format("#define a 3\n" 14890 "\n" 14891 "\n" 14892 "/* multi-line *\n" 14893 " * block comment */\n" 14894 "\n" 14895 "\n" 14896 "#define bbbb 4\n" 14897 "#define ccc (5)", 14898 Style)); 14899 14900 EXPECT_EQ("#define a 3\n" 14901 "\n" 14902 "\n" 14903 "/* multi-line *\n" 14904 " * block comment */\n" 14905 "\n" 14906 "\n" 14907 "#define bbbb 4\n" 14908 "#define ccc (5)", 14909 format("#define a 3\n" 14910 "\n" 14911 "\n" 14912 "/* multi-line *\n" 14913 " * block comment */\n" 14914 "\n" 14915 "\n" 14916 "#define bbbb 4\n" 14917 "#define ccc (5)", 14918 Style)); 14919 } 14920 14921 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) { 14922 FormatStyle Alignment = getLLVMStyle(); 14923 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 14924 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossEmptyLines; 14925 14926 Alignment.MaxEmptyLinesToKeep = 10; 14927 /* Test alignment across empty lines */ 14928 EXPECT_EQ("int a = 5;\n" 14929 "\n" 14930 "int oneTwoThree = 123;", 14931 format("int a = 5;\n" 14932 "\n" 14933 "int oneTwoThree= 123;", 14934 Alignment)); 14935 EXPECT_EQ("int a = 5;\n" 14936 "int one = 1;\n" 14937 "\n" 14938 "int oneTwoThree = 123;", 14939 format("int a = 5;\n" 14940 "int one = 1;\n" 14941 "\n" 14942 "int oneTwoThree = 123;", 14943 Alignment)); 14944 EXPECT_EQ("int a = 5;\n" 14945 "int one = 1;\n" 14946 "\n" 14947 "int oneTwoThree = 123;\n" 14948 "int oneTwo = 12;", 14949 format("int a = 5;\n" 14950 "int one = 1;\n" 14951 "\n" 14952 "int oneTwoThree = 123;\n" 14953 "int oneTwo = 12;", 14954 Alignment)); 14955 14956 /* Test across comments */ 14957 EXPECT_EQ("int a = 5;\n" 14958 "/* block comment */\n" 14959 "int oneTwoThree = 123;", 14960 format("int a = 5;\n" 14961 "/* block comment */\n" 14962 "int oneTwoThree=123;", 14963 Alignment)); 14964 14965 EXPECT_EQ("int a = 5;\n" 14966 "// line comment\n" 14967 "int oneTwoThree = 123;", 14968 format("int a = 5;\n" 14969 "// line comment\n" 14970 "int oneTwoThree=123;", 14971 Alignment)); 14972 14973 /* Test across comments and newlines */ 14974 EXPECT_EQ("int a = 5;\n" 14975 "\n" 14976 "/* block comment */\n" 14977 "int oneTwoThree = 123;", 14978 format("int a = 5;\n" 14979 "\n" 14980 "/* block comment */\n" 14981 "int oneTwoThree=123;", 14982 Alignment)); 14983 14984 EXPECT_EQ("int a = 5;\n" 14985 "\n" 14986 "// line comment\n" 14987 "int oneTwoThree = 123;", 14988 format("int a = 5;\n" 14989 "\n" 14990 "// line comment\n" 14991 "int oneTwoThree=123;", 14992 Alignment)); 14993 } 14994 14995 TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) { 14996 FormatStyle Alignment = getLLVMStyle(); 14997 Alignment.AlignConsecutiveDeclarations = 14998 FormatStyle::ACS_AcrossEmptyLinesAndComments; 14999 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 15000 15001 Alignment.MaxEmptyLinesToKeep = 10; 15002 /* Test alignment across empty lines */ 15003 EXPECT_EQ("int a = 5;\n" 15004 "\n" 15005 "float const oneTwoThree = 123;", 15006 format("int a = 5;\n" 15007 "\n" 15008 "float const oneTwoThree = 123;", 15009 Alignment)); 15010 EXPECT_EQ("int a = 5;\n" 15011 "float const one = 1;\n" 15012 "\n" 15013 "int oneTwoThree = 123;", 15014 format("int a = 5;\n" 15015 "float const one = 1;\n" 15016 "\n" 15017 "int oneTwoThree = 123;", 15018 Alignment)); 15019 15020 /* Test across comments */ 15021 EXPECT_EQ("float const a = 5;\n" 15022 "/* block comment */\n" 15023 "int oneTwoThree = 123;", 15024 format("float const a = 5;\n" 15025 "/* block comment */\n" 15026 "int oneTwoThree=123;", 15027 Alignment)); 15028 15029 EXPECT_EQ("float const a = 5;\n" 15030 "// line comment\n" 15031 "int oneTwoThree = 123;", 15032 format("float const a = 5;\n" 15033 "// line comment\n" 15034 "int oneTwoThree=123;", 15035 Alignment)); 15036 15037 /* Test across comments and newlines */ 15038 EXPECT_EQ("float const a = 5;\n" 15039 "\n" 15040 "/* block comment */\n" 15041 "int oneTwoThree = 123;", 15042 format("float const a = 5;\n" 15043 "\n" 15044 "/* block comment */\n" 15045 "int oneTwoThree=123;", 15046 Alignment)); 15047 15048 EXPECT_EQ("float const a = 5;\n" 15049 "\n" 15050 "// line comment\n" 15051 "int oneTwoThree = 123;", 15052 format("float const a = 5;\n" 15053 "\n" 15054 "// line comment\n" 15055 "int oneTwoThree=123;", 15056 Alignment)); 15057 } 15058 15059 TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) { 15060 FormatStyle Alignment = getLLVMStyle(); 15061 Alignment.AlignConsecutiveBitFields = 15062 FormatStyle::ACS_AcrossEmptyLinesAndComments; 15063 15064 Alignment.MaxEmptyLinesToKeep = 10; 15065 /* Test alignment across empty lines */ 15066 EXPECT_EQ("int a : 5;\n" 15067 "\n" 15068 "int longbitfield : 6;", 15069 format("int a : 5;\n" 15070 "\n" 15071 "int longbitfield : 6;", 15072 Alignment)); 15073 EXPECT_EQ("int a : 5;\n" 15074 "int one : 1;\n" 15075 "\n" 15076 "int longbitfield : 6;", 15077 format("int a : 5;\n" 15078 "int one : 1;\n" 15079 "\n" 15080 "int longbitfield : 6;", 15081 Alignment)); 15082 15083 /* Test across comments */ 15084 EXPECT_EQ("int a : 5;\n" 15085 "/* block comment */\n" 15086 "int longbitfield : 6;", 15087 format("int a : 5;\n" 15088 "/* block comment */\n" 15089 "int longbitfield : 6;", 15090 Alignment)); 15091 EXPECT_EQ("int a : 5;\n" 15092 "int one : 1;\n" 15093 "// line comment\n" 15094 "int longbitfield : 6;", 15095 format("int a : 5;\n" 15096 "int one : 1;\n" 15097 "// line comment\n" 15098 "int longbitfield : 6;", 15099 Alignment)); 15100 15101 /* Test across comments and newlines */ 15102 EXPECT_EQ("int a : 5;\n" 15103 "/* block comment */\n" 15104 "\n" 15105 "int longbitfield : 6;", 15106 format("int a : 5;\n" 15107 "/* block comment */\n" 15108 "\n" 15109 "int longbitfield : 6;", 15110 Alignment)); 15111 EXPECT_EQ("int a : 5;\n" 15112 "int one : 1;\n" 15113 "\n" 15114 "// line comment\n" 15115 "\n" 15116 "int longbitfield : 6;", 15117 format("int a : 5;\n" 15118 "int one : 1;\n" 15119 "\n" 15120 "// line comment \n" 15121 "\n" 15122 "int longbitfield : 6;", 15123 Alignment)); 15124 } 15125 15126 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) { 15127 FormatStyle Alignment = getLLVMStyle(); 15128 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15129 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossComments; 15130 15131 Alignment.MaxEmptyLinesToKeep = 10; 15132 /* Test alignment across empty lines */ 15133 EXPECT_EQ("int a = 5;\n" 15134 "\n" 15135 "int oneTwoThree = 123;", 15136 format("int a = 5;\n" 15137 "\n" 15138 "int oneTwoThree= 123;", 15139 Alignment)); 15140 EXPECT_EQ("int a = 5;\n" 15141 "int one = 1;\n" 15142 "\n" 15143 "int oneTwoThree = 123;", 15144 format("int a = 5;\n" 15145 "int one = 1;\n" 15146 "\n" 15147 "int oneTwoThree = 123;", 15148 Alignment)); 15149 15150 /* Test across comments */ 15151 EXPECT_EQ("int a = 5;\n" 15152 "/* block comment */\n" 15153 "int oneTwoThree = 123;", 15154 format("int a = 5;\n" 15155 "/* block comment */\n" 15156 "int oneTwoThree=123;", 15157 Alignment)); 15158 15159 EXPECT_EQ("int a = 5;\n" 15160 "// line comment\n" 15161 "int oneTwoThree = 123;", 15162 format("int a = 5;\n" 15163 "// line comment\n" 15164 "int oneTwoThree=123;", 15165 Alignment)); 15166 15167 EXPECT_EQ("int a = 5;\n" 15168 "/*\n" 15169 " * multi-line block comment\n" 15170 " */\n" 15171 "int oneTwoThree = 123;", 15172 format("int a = 5;\n" 15173 "/*\n" 15174 " * multi-line block comment\n" 15175 " */\n" 15176 "int oneTwoThree=123;", 15177 Alignment)); 15178 15179 EXPECT_EQ("int a = 5;\n" 15180 "//\n" 15181 "// multi-line line comment\n" 15182 "//\n" 15183 "int oneTwoThree = 123;", 15184 format("int a = 5;\n" 15185 "//\n" 15186 "// multi-line line comment\n" 15187 "//\n" 15188 "int oneTwoThree=123;", 15189 Alignment)); 15190 15191 /* Test across comments and newlines */ 15192 EXPECT_EQ("int a = 5;\n" 15193 "\n" 15194 "/* block comment */\n" 15195 "int oneTwoThree = 123;", 15196 format("int a = 5;\n" 15197 "\n" 15198 "/* block comment */\n" 15199 "int oneTwoThree=123;", 15200 Alignment)); 15201 15202 EXPECT_EQ("int a = 5;\n" 15203 "\n" 15204 "// line comment\n" 15205 "int oneTwoThree = 123;", 15206 format("int a = 5;\n" 15207 "\n" 15208 "// line comment\n" 15209 "int oneTwoThree=123;", 15210 Alignment)); 15211 } 15212 15213 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) { 15214 FormatStyle Alignment = getLLVMStyle(); 15215 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15216 Alignment.AlignConsecutiveAssignments = 15217 FormatStyle::ACS_AcrossEmptyLinesAndComments; 15218 verifyFormat("int a = 5;\n" 15219 "int oneTwoThree = 123;", 15220 Alignment); 15221 verifyFormat("int a = method();\n" 15222 "int oneTwoThree = 133;", 15223 Alignment); 15224 verifyFormat("a &= 5;\n" 15225 "bcd *= 5;\n" 15226 "ghtyf += 5;\n" 15227 "dvfvdb -= 5;\n" 15228 "a /= 5;\n" 15229 "vdsvsv %= 5;\n" 15230 "sfdbddfbdfbb ^= 5;\n" 15231 "dvsdsv |= 5;\n" 15232 "int dsvvdvsdvvv = 123;", 15233 Alignment); 15234 verifyFormat("int i = 1, j = 10;\n" 15235 "something = 2000;", 15236 Alignment); 15237 verifyFormat("something = 2000;\n" 15238 "int i = 1, j = 10;\n", 15239 Alignment); 15240 verifyFormat("something = 2000;\n" 15241 "another = 911;\n" 15242 "int i = 1, j = 10;\n" 15243 "oneMore = 1;\n" 15244 "i = 2;", 15245 Alignment); 15246 verifyFormat("int a = 5;\n" 15247 "int one = 1;\n" 15248 "method();\n" 15249 "int oneTwoThree = 123;\n" 15250 "int oneTwo = 12;", 15251 Alignment); 15252 verifyFormat("int oneTwoThree = 123;\n" 15253 "int oneTwo = 12;\n" 15254 "method();\n", 15255 Alignment); 15256 verifyFormat("int oneTwoThree = 123; // comment\n" 15257 "int oneTwo = 12; // comment", 15258 Alignment); 15259 15260 // Bug 25167 15261 /* Uncomment when fixed 15262 verifyFormat("#if A\n" 15263 "#else\n" 15264 "int aaaaaaaa = 12;\n" 15265 "#endif\n" 15266 "#if B\n" 15267 "#else\n" 15268 "int a = 12;\n" 15269 "#endif\n", 15270 Alignment); 15271 verifyFormat("enum foo {\n" 15272 "#if A\n" 15273 "#else\n" 15274 " aaaaaaaa = 12;\n" 15275 "#endif\n" 15276 "#if B\n" 15277 "#else\n" 15278 " a = 12;\n" 15279 "#endif\n" 15280 "};\n", 15281 Alignment); 15282 */ 15283 15284 Alignment.MaxEmptyLinesToKeep = 10; 15285 /* Test alignment across empty lines */ 15286 EXPECT_EQ("int a = 5;\n" 15287 "\n" 15288 "int oneTwoThree = 123;", 15289 format("int a = 5;\n" 15290 "\n" 15291 "int oneTwoThree= 123;", 15292 Alignment)); 15293 EXPECT_EQ("int a = 5;\n" 15294 "int one = 1;\n" 15295 "\n" 15296 "int oneTwoThree = 123;", 15297 format("int a = 5;\n" 15298 "int one = 1;\n" 15299 "\n" 15300 "int oneTwoThree = 123;", 15301 Alignment)); 15302 EXPECT_EQ("int a = 5;\n" 15303 "int one = 1;\n" 15304 "\n" 15305 "int oneTwoThree = 123;\n" 15306 "int oneTwo = 12;", 15307 format("int a = 5;\n" 15308 "int one = 1;\n" 15309 "\n" 15310 "int oneTwoThree = 123;\n" 15311 "int oneTwo = 12;", 15312 Alignment)); 15313 15314 /* Test across comments */ 15315 EXPECT_EQ("int a = 5;\n" 15316 "/* block comment */\n" 15317 "int oneTwoThree = 123;", 15318 format("int a = 5;\n" 15319 "/* block comment */\n" 15320 "int oneTwoThree=123;", 15321 Alignment)); 15322 15323 EXPECT_EQ("int a = 5;\n" 15324 "// line comment\n" 15325 "int oneTwoThree = 123;", 15326 format("int a = 5;\n" 15327 "// line comment\n" 15328 "int oneTwoThree=123;", 15329 Alignment)); 15330 15331 /* Test across comments and newlines */ 15332 EXPECT_EQ("int a = 5;\n" 15333 "\n" 15334 "/* block comment */\n" 15335 "int oneTwoThree = 123;", 15336 format("int a = 5;\n" 15337 "\n" 15338 "/* block comment */\n" 15339 "int oneTwoThree=123;", 15340 Alignment)); 15341 15342 EXPECT_EQ("int a = 5;\n" 15343 "\n" 15344 "// line comment\n" 15345 "int oneTwoThree = 123;", 15346 format("int a = 5;\n" 15347 "\n" 15348 "// line comment\n" 15349 "int oneTwoThree=123;", 15350 Alignment)); 15351 15352 EXPECT_EQ("int a = 5;\n" 15353 "//\n" 15354 "// multi-line line comment\n" 15355 "//\n" 15356 "int oneTwoThree = 123;", 15357 format("int a = 5;\n" 15358 "//\n" 15359 "// multi-line line comment\n" 15360 "//\n" 15361 "int oneTwoThree=123;", 15362 Alignment)); 15363 15364 EXPECT_EQ("int a = 5;\n" 15365 "/*\n" 15366 " * multi-line block comment\n" 15367 " */\n" 15368 "int oneTwoThree = 123;", 15369 format("int a = 5;\n" 15370 "/*\n" 15371 " * multi-line block comment\n" 15372 " */\n" 15373 "int oneTwoThree=123;", 15374 Alignment)); 15375 15376 EXPECT_EQ("int a = 5;\n" 15377 "\n" 15378 "/* block comment */\n" 15379 "\n" 15380 "\n" 15381 "\n" 15382 "int oneTwoThree = 123;", 15383 format("int a = 5;\n" 15384 "\n" 15385 "/* block comment */\n" 15386 "\n" 15387 "\n" 15388 "\n" 15389 "int oneTwoThree=123;", 15390 Alignment)); 15391 15392 EXPECT_EQ("int a = 5;\n" 15393 "\n" 15394 "// line comment\n" 15395 "\n" 15396 "\n" 15397 "\n" 15398 "int oneTwoThree = 123;", 15399 format("int a = 5;\n" 15400 "\n" 15401 "// line comment\n" 15402 "\n" 15403 "\n" 15404 "\n" 15405 "int oneTwoThree=123;", 15406 Alignment)); 15407 15408 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 15409 verifyFormat("#define A \\\n" 15410 " int aaaa = 12; \\\n" 15411 " int b = 23; \\\n" 15412 " int ccc = 234; \\\n" 15413 " int dddddddddd = 2345;", 15414 Alignment); 15415 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 15416 verifyFormat("#define A \\\n" 15417 " int aaaa = 12; \\\n" 15418 " int b = 23; \\\n" 15419 " int ccc = 234; \\\n" 15420 " int dddddddddd = 2345;", 15421 Alignment); 15422 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 15423 verifyFormat("#define A " 15424 " \\\n" 15425 " int aaaa = 12; " 15426 " \\\n" 15427 " int b = 23; " 15428 " \\\n" 15429 " int ccc = 234; " 15430 " \\\n" 15431 " int dddddddddd = 2345;", 15432 Alignment); 15433 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 15434 "k = 4, int l = 5,\n" 15435 " int m = 6) {\n" 15436 " int j = 10;\n" 15437 " otherThing = 1;\n" 15438 "}", 15439 Alignment); 15440 verifyFormat("void SomeFunction(int parameter = 0) {\n" 15441 " int i = 1;\n" 15442 " int j = 2;\n" 15443 " int big = 10000;\n" 15444 "}", 15445 Alignment); 15446 verifyFormat("class C {\n" 15447 "public:\n" 15448 " int i = 1;\n" 15449 " virtual void f() = 0;\n" 15450 "};", 15451 Alignment); 15452 verifyFormat("int i = 1;\n" 15453 "if (SomeType t = getSomething()) {\n" 15454 "}\n" 15455 "int j = 2;\n" 15456 "int big = 10000;", 15457 Alignment); 15458 verifyFormat("int j = 7;\n" 15459 "for (int k = 0; k < N; ++k) {\n" 15460 "}\n" 15461 "int j = 2;\n" 15462 "int big = 10000;\n" 15463 "}", 15464 Alignment); 15465 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 15466 verifyFormat("int i = 1;\n" 15467 "LooooooooooongType loooooooooooooooooooooongVariable\n" 15468 " = someLooooooooooooooooongFunction();\n" 15469 "int j = 2;", 15470 Alignment); 15471 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 15472 verifyFormat("int i = 1;\n" 15473 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 15474 " someLooooooooooooooooongFunction();\n" 15475 "int j = 2;", 15476 Alignment); 15477 15478 verifyFormat("auto lambda = []() {\n" 15479 " auto i = 0;\n" 15480 " return 0;\n" 15481 "};\n" 15482 "int i = 0;\n" 15483 "auto v = type{\n" 15484 " i = 1, //\n" 15485 " (i = 2), //\n" 15486 " i = 3 //\n" 15487 "};", 15488 Alignment); 15489 15490 verifyFormat( 15491 "int i = 1;\n" 15492 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 15493 " loooooooooooooooooooooongParameterB);\n" 15494 "int j = 2;", 15495 Alignment); 15496 15497 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 15498 " typename B = very_long_type_name_1,\n" 15499 " typename T_2 = very_long_type_name_2>\n" 15500 "auto foo() {}\n", 15501 Alignment); 15502 verifyFormat("int a, b = 1;\n" 15503 "int c = 2;\n" 15504 "int dd = 3;\n", 15505 Alignment); 15506 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 15507 "float b[1][] = {{3.f}};\n", 15508 Alignment); 15509 verifyFormat("for (int i = 0; i < 1; i++)\n" 15510 " int x = 1;\n", 15511 Alignment); 15512 verifyFormat("for (i = 0; i < 1; i++)\n" 15513 " x = 1;\n" 15514 "y = 1;\n", 15515 Alignment); 15516 15517 Alignment.ReflowComments = true; 15518 Alignment.ColumnLimit = 50; 15519 EXPECT_EQ("int x = 0;\n" 15520 "int yy = 1; /// specificlennospace\n" 15521 "int zzz = 2;\n", 15522 format("int x = 0;\n" 15523 "int yy = 1; ///specificlennospace\n" 15524 "int zzz = 2;\n", 15525 Alignment)); 15526 } 15527 15528 TEST_F(FormatTest, AlignConsecutiveAssignments) { 15529 FormatStyle Alignment = getLLVMStyle(); 15530 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15531 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 15532 verifyFormat("int a = 5;\n" 15533 "int oneTwoThree = 123;", 15534 Alignment); 15535 verifyFormat("int a = 5;\n" 15536 "int oneTwoThree = 123;", 15537 Alignment); 15538 15539 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 15540 verifyFormat("int a = 5;\n" 15541 "int oneTwoThree = 123;", 15542 Alignment); 15543 verifyFormat("int a = method();\n" 15544 "int oneTwoThree = 133;", 15545 Alignment); 15546 verifyFormat("a &= 5;\n" 15547 "bcd *= 5;\n" 15548 "ghtyf += 5;\n" 15549 "dvfvdb -= 5;\n" 15550 "a /= 5;\n" 15551 "vdsvsv %= 5;\n" 15552 "sfdbddfbdfbb ^= 5;\n" 15553 "dvsdsv |= 5;\n" 15554 "int dsvvdvsdvvv = 123;", 15555 Alignment); 15556 verifyFormat("int i = 1, j = 10;\n" 15557 "something = 2000;", 15558 Alignment); 15559 verifyFormat("something = 2000;\n" 15560 "int i = 1, j = 10;\n", 15561 Alignment); 15562 verifyFormat("something = 2000;\n" 15563 "another = 911;\n" 15564 "int i = 1, j = 10;\n" 15565 "oneMore = 1;\n" 15566 "i = 2;", 15567 Alignment); 15568 verifyFormat("int a = 5;\n" 15569 "int one = 1;\n" 15570 "method();\n" 15571 "int oneTwoThree = 123;\n" 15572 "int oneTwo = 12;", 15573 Alignment); 15574 verifyFormat("int oneTwoThree = 123;\n" 15575 "int oneTwo = 12;\n" 15576 "method();\n", 15577 Alignment); 15578 verifyFormat("int oneTwoThree = 123; // comment\n" 15579 "int oneTwo = 12; // comment", 15580 Alignment); 15581 15582 // Bug 25167 15583 /* Uncomment when fixed 15584 verifyFormat("#if A\n" 15585 "#else\n" 15586 "int aaaaaaaa = 12;\n" 15587 "#endif\n" 15588 "#if B\n" 15589 "#else\n" 15590 "int a = 12;\n" 15591 "#endif\n", 15592 Alignment); 15593 verifyFormat("enum foo {\n" 15594 "#if A\n" 15595 "#else\n" 15596 " aaaaaaaa = 12;\n" 15597 "#endif\n" 15598 "#if B\n" 15599 "#else\n" 15600 " a = 12;\n" 15601 "#endif\n" 15602 "};\n", 15603 Alignment); 15604 */ 15605 15606 EXPECT_EQ("int a = 5;\n" 15607 "\n" 15608 "int oneTwoThree = 123;", 15609 format("int a = 5;\n" 15610 "\n" 15611 "int oneTwoThree= 123;", 15612 Alignment)); 15613 EXPECT_EQ("int a = 5;\n" 15614 "int one = 1;\n" 15615 "\n" 15616 "int oneTwoThree = 123;", 15617 format("int a = 5;\n" 15618 "int one = 1;\n" 15619 "\n" 15620 "int oneTwoThree = 123;", 15621 Alignment)); 15622 EXPECT_EQ("int a = 5;\n" 15623 "int one = 1;\n" 15624 "\n" 15625 "int oneTwoThree = 123;\n" 15626 "int oneTwo = 12;", 15627 format("int a = 5;\n" 15628 "int one = 1;\n" 15629 "\n" 15630 "int oneTwoThree = 123;\n" 15631 "int oneTwo = 12;", 15632 Alignment)); 15633 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 15634 verifyFormat("#define A \\\n" 15635 " int aaaa = 12; \\\n" 15636 " int b = 23; \\\n" 15637 " int ccc = 234; \\\n" 15638 " int dddddddddd = 2345;", 15639 Alignment); 15640 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 15641 verifyFormat("#define A \\\n" 15642 " int aaaa = 12; \\\n" 15643 " int b = 23; \\\n" 15644 " int ccc = 234; \\\n" 15645 " int dddddddddd = 2345;", 15646 Alignment); 15647 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 15648 verifyFormat("#define A " 15649 " \\\n" 15650 " int aaaa = 12; " 15651 " \\\n" 15652 " int b = 23; " 15653 " \\\n" 15654 " int ccc = 234; " 15655 " \\\n" 15656 " int dddddddddd = 2345;", 15657 Alignment); 15658 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 15659 "k = 4, int l = 5,\n" 15660 " int m = 6) {\n" 15661 " int j = 10;\n" 15662 " otherThing = 1;\n" 15663 "}", 15664 Alignment); 15665 verifyFormat("void SomeFunction(int parameter = 0) {\n" 15666 " int i = 1;\n" 15667 " int j = 2;\n" 15668 " int big = 10000;\n" 15669 "}", 15670 Alignment); 15671 verifyFormat("class C {\n" 15672 "public:\n" 15673 " int i = 1;\n" 15674 " virtual void f() = 0;\n" 15675 "};", 15676 Alignment); 15677 verifyFormat("int i = 1;\n" 15678 "if (SomeType t = getSomething()) {\n" 15679 "}\n" 15680 "int j = 2;\n" 15681 "int big = 10000;", 15682 Alignment); 15683 verifyFormat("int j = 7;\n" 15684 "for (int k = 0; k < N; ++k) {\n" 15685 "}\n" 15686 "int j = 2;\n" 15687 "int big = 10000;\n" 15688 "}", 15689 Alignment); 15690 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 15691 verifyFormat("int i = 1;\n" 15692 "LooooooooooongType loooooooooooooooooooooongVariable\n" 15693 " = someLooooooooooooooooongFunction();\n" 15694 "int j = 2;", 15695 Alignment); 15696 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 15697 verifyFormat("int i = 1;\n" 15698 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 15699 " someLooooooooooooooooongFunction();\n" 15700 "int j = 2;", 15701 Alignment); 15702 15703 verifyFormat("auto lambda = []() {\n" 15704 " auto i = 0;\n" 15705 " return 0;\n" 15706 "};\n" 15707 "int i = 0;\n" 15708 "auto v = type{\n" 15709 " i = 1, //\n" 15710 " (i = 2), //\n" 15711 " i = 3 //\n" 15712 "};", 15713 Alignment); 15714 15715 verifyFormat( 15716 "int i = 1;\n" 15717 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 15718 " loooooooooooooooooooooongParameterB);\n" 15719 "int j = 2;", 15720 Alignment); 15721 15722 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 15723 " typename B = very_long_type_name_1,\n" 15724 " typename T_2 = very_long_type_name_2>\n" 15725 "auto foo() {}\n", 15726 Alignment); 15727 verifyFormat("int a, b = 1;\n" 15728 "int c = 2;\n" 15729 "int dd = 3;\n", 15730 Alignment); 15731 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 15732 "float b[1][] = {{3.f}};\n", 15733 Alignment); 15734 verifyFormat("for (int i = 0; i < 1; i++)\n" 15735 " int x = 1;\n", 15736 Alignment); 15737 verifyFormat("for (i = 0; i < 1; i++)\n" 15738 " x = 1;\n" 15739 "y = 1;\n", 15740 Alignment); 15741 15742 Alignment.ReflowComments = true; 15743 Alignment.ColumnLimit = 50; 15744 EXPECT_EQ("int x = 0;\n" 15745 "int yy = 1; /// specificlennospace\n" 15746 "int zzz = 2;\n", 15747 format("int x = 0;\n" 15748 "int yy = 1; ///specificlennospace\n" 15749 "int zzz = 2;\n", 15750 Alignment)); 15751 } 15752 15753 TEST_F(FormatTest, AlignConsecutiveBitFields) { 15754 FormatStyle Alignment = getLLVMStyle(); 15755 Alignment.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive; 15756 verifyFormat("int const a : 5;\n" 15757 "int oneTwoThree : 23;", 15758 Alignment); 15759 15760 // Initializers are allowed starting with c++2a 15761 verifyFormat("int const a : 5 = 1;\n" 15762 "int oneTwoThree : 23 = 0;", 15763 Alignment); 15764 15765 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 15766 verifyFormat("int const a : 5;\n" 15767 "int oneTwoThree : 23;", 15768 Alignment); 15769 15770 verifyFormat("int const a : 5; // comment\n" 15771 "int oneTwoThree : 23; // comment", 15772 Alignment); 15773 15774 verifyFormat("int const a : 5 = 1;\n" 15775 "int oneTwoThree : 23 = 0;", 15776 Alignment); 15777 15778 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 15779 verifyFormat("int const a : 5 = 1;\n" 15780 "int oneTwoThree : 23 = 0;", 15781 Alignment); 15782 verifyFormat("int const a : 5 = {1};\n" 15783 "int oneTwoThree : 23 = 0;", 15784 Alignment); 15785 15786 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None; 15787 verifyFormat("int const a :5;\n" 15788 "int oneTwoThree:23;", 15789 Alignment); 15790 15791 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before; 15792 verifyFormat("int const a :5;\n" 15793 "int oneTwoThree :23;", 15794 Alignment); 15795 15796 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After; 15797 verifyFormat("int const a : 5;\n" 15798 "int oneTwoThree: 23;", 15799 Alignment); 15800 15801 // Known limitations: ':' is only recognized as a bitfield colon when 15802 // followed by a number. 15803 /* 15804 verifyFormat("int oneTwoThree : SOME_CONSTANT;\n" 15805 "int a : 5;", 15806 Alignment); 15807 */ 15808 } 15809 15810 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 15811 FormatStyle Alignment = getLLVMStyle(); 15812 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15813 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_None; 15814 Alignment.PointerAlignment = FormatStyle::PAS_Right; 15815 verifyFormat("float const a = 5;\n" 15816 "int oneTwoThree = 123;", 15817 Alignment); 15818 verifyFormat("int a = 5;\n" 15819 "float const oneTwoThree = 123;", 15820 Alignment); 15821 15822 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 15823 verifyFormat("float const a = 5;\n" 15824 "int oneTwoThree = 123;", 15825 Alignment); 15826 verifyFormat("int a = method();\n" 15827 "float const oneTwoThree = 133;", 15828 Alignment); 15829 verifyFormat("int i = 1, j = 10;\n" 15830 "something = 2000;", 15831 Alignment); 15832 verifyFormat("something = 2000;\n" 15833 "int i = 1, j = 10;\n", 15834 Alignment); 15835 verifyFormat("float something = 2000;\n" 15836 "double another = 911;\n" 15837 "int i = 1, j = 10;\n" 15838 "const int *oneMore = 1;\n" 15839 "unsigned i = 2;", 15840 Alignment); 15841 verifyFormat("float a = 5;\n" 15842 "int one = 1;\n" 15843 "method();\n" 15844 "const double oneTwoThree = 123;\n" 15845 "const unsigned int oneTwo = 12;", 15846 Alignment); 15847 verifyFormat("int oneTwoThree{0}; // comment\n" 15848 "unsigned oneTwo; // comment", 15849 Alignment); 15850 verifyFormat("unsigned int *a;\n" 15851 "int *b;\n" 15852 "unsigned int Const *c;\n" 15853 "unsigned int const *d;\n" 15854 "unsigned int Const &e;\n" 15855 "unsigned int const &f;", 15856 Alignment); 15857 verifyFormat("Const unsigned int *c;\n" 15858 "const unsigned int *d;\n" 15859 "Const unsigned int &e;\n" 15860 "const unsigned int &f;\n" 15861 "const unsigned g;\n" 15862 "Const unsigned h;", 15863 Alignment); 15864 EXPECT_EQ("float const a = 5;\n" 15865 "\n" 15866 "int oneTwoThree = 123;", 15867 format("float const a = 5;\n" 15868 "\n" 15869 "int oneTwoThree= 123;", 15870 Alignment)); 15871 EXPECT_EQ("float a = 5;\n" 15872 "int one = 1;\n" 15873 "\n" 15874 "unsigned oneTwoThree = 123;", 15875 format("float a = 5;\n" 15876 "int one = 1;\n" 15877 "\n" 15878 "unsigned oneTwoThree = 123;", 15879 Alignment)); 15880 EXPECT_EQ("float a = 5;\n" 15881 "int one = 1;\n" 15882 "\n" 15883 "unsigned oneTwoThree = 123;\n" 15884 "int oneTwo = 12;", 15885 format("float a = 5;\n" 15886 "int one = 1;\n" 15887 "\n" 15888 "unsigned oneTwoThree = 123;\n" 15889 "int oneTwo = 12;", 15890 Alignment)); 15891 // Function prototype alignment 15892 verifyFormat("int a();\n" 15893 "double b();", 15894 Alignment); 15895 verifyFormat("int a(int x);\n" 15896 "double b();", 15897 Alignment); 15898 unsigned OldColumnLimit = Alignment.ColumnLimit; 15899 // We need to set ColumnLimit to zero, in order to stress nested alignments, 15900 // otherwise the function parameters will be re-flowed onto a single line. 15901 Alignment.ColumnLimit = 0; 15902 EXPECT_EQ("int a(int x,\n" 15903 " float y);\n" 15904 "double b(int x,\n" 15905 " double y);", 15906 format("int a(int x,\n" 15907 " float y);\n" 15908 "double b(int x,\n" 15909 " double y);", 15910 Alignment)); 15911 // This ensures that function parameters of function declarations are 15912 // correctly indented when their owning functions are indented. 15913 // The failure case here is for 'double y' to not be indented enough. 15914 EXPECT_EQ("double a(int x);\n" 15915 "int b(int y,\n" 15916 " double z);", 15917 format("double a(int x);\n" 15918 "int b(int y,\n" 15919 " double z);", 15920 Alignment)); 15921 // Set ColumnLimit low so that we induce wrapping immediately after 15922 // the function name and opening paren. 15923 Alignment.ColumnLimit = 13; 15924 verifyFormat("int function(\n" 15925 " int x,\n" 15926 " bool y);", 15927 Alignment); 15928 Alignment.ColumnLimit = OldColumnLimit; 15929 // Ensure function pointers don't screw up recursive alignment 15930 verifyFormat("int a(int x, void (*fp)(int y));\n" 15931 "double b();", 15932 Alignment); 15933 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 15934 // Ensure recursive alignment is broken by function braces, so that the 15935 // "a = 1" does not align with subsequent assignments inside the function 15936 // body. 15937 verifyFormat("int func(int a = 1) {\n" 15938 " int b = 2;\n" 15939 " int cc = 3;\n" 15940 "}", 15941 Alignment); 15942 verifyFormat("float something = 2000;\n" 15943 "double another = 911;\n" 15944 "int i = 1, j = 10;\n" 15945 "const int *oneMore = 1;\n" 15946 "unsigned i = 2;", 15947 Alignment); 15948 verifyFormat("int oneTwoThree = {0}; // comment\n" 15949 "unsigned oneTwo = 0; // comment", 15950 Alignment); 15951 // Make sure that scope is correctly tracked, in the absence of braces 15952 verifyFormat("for (int i = 0; i < n; i++)\n" 15953 " j = i;\n" 15954 "double x = 1;\n", 15955 Alignment); 15956 verifyFormat("if (int i = 0)\n" 15957 " j = i;\n" 15958 "double x = 1;\n", 15959 Alignment); 15960 // Ensure operator[] and operator() are comprehended 15961 verifyFormat("struct test {\n" 15962 " long long int foo();\n" 15963 " int operator[](int a);\n" 15964 " double bar();\n" 15965 "};\n", 15966 Alignment); 15967 verifyFormat("struct test {\n" 15968 " long long int foo();\n" 15969 " int operator()(int a);\n" 15970 " double bar();\n" 15971 "};\n", 15972 Alignment); 15973 15974 // PAS_Right 15975 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 15976 " int const i = 1;\n" 15977 " int *j = 2;\n" 15978 " int big = 10000;\n" 15979 "\n" 15980 " unsigned oneTwoThree = 123;\n" 15981 " int oneTwo = 12;\n" 15982 " method();\n" 15983 " float k = 2;\n" 15984 " int ll = 10000;\n" 15985 "}", 15986 format("void SomeFunction(int parameter= 0) {\n" 15987 " int const i= 1;\n" 15988 " int *j=2;\n" 15989 " int big = 10000;\n" 15990 "\n" 15991 "unsigned oneTwoThree =123;\n" 15992 "int oneTwo = 12;\n" 15993 " method();\n" 15994 "float k= 2;\n" 15995 "int ll=10000;\n" 15996 "}", 15997 Alignment)); 15998 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 15999 " int const i = 1;\n" 16000 " int **j = 2, ***k;\n" 16001 " int &k = i;\n" 16002 " int &&l = i + j;\n" 16003 " int big = 10000;\n" 16004 "\n" 16005 " unsigned oneTwoThree = 123;\n" 16006 " int oneTwo = 12;\n" 16007 " method();\n" 16008 " float k = 2;\n" 16009 " int ll = 10000;\n" 16010 "}", 16011 format("void SomeFunction(int parameter= 0) {\n" 16012 " int const i= 1;\n" 16013 " int **j=2,***k;\n" 16014 "int &k=i;\n" 16015 "int &&l=i+j;\n" 16016 " int big = 10000;\n" 16017 "\n" 16018 "unsigned oneTwoThree =123;\n" 16019 "int oneTwo = 12;\n" 16020 " method();\n" 16021 "float k= 2;\n" 16022 "int ll=10000;\n" 16023 "}", 16024 Alignment)); 16025 // variables are aligned at their name, pointers are at the right most 16026 // position 16027 verifyFormat("int *a;\n" 16028 "int **b;\n" 16029 "int ***c;\n" 16030 "int foobar;\n", 16031 Alignment); 16032 16033 // PAS_Left 16034 FormatStyle AlignmentLeft = Alignment; 16035 AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left; 16036 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16037 " int const i = 1;\n" 16038 " int* j = 2;\n" 16039 " int big = 10000;\n" 16040 "\n" 16041 " unsigned oneTwoThree = 123;\n" 16042 " int oneTwo = 12;\n" 16043 " method();\n" 16044 " float k = 2;\n" 16045 " int ll = 10000;\n" 16046 "}", 16047 format("void SomeFunction(int parameter= 0) {\n" 16048 " int const i= 1;\n" 16049 " int *j=2;\n" 16050 " int big = 10000;\n" 16051 "\n" 16052 "unsigned oneTwoThree =123;\n" 16053 "int oneTwo = 12;\n" 16054 " method();\n" 16055 "float k= 2;\n" 16056 "int ll=10000;\n" 16057 "}", 16058 AlignmentLeft)); 16059 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16060 " int const i = 1;\n" 16061 " int** j = 2;\n" 16062 " int& k = i;\n" 16063 " int&& l = i + j;\n" 16064 " int big = 10000;\n" 16065 "\n" 16066 " unsigned oneTwoThree = 123;\n" 16067 " int oneTwo = 12;\n" 16068 " method();\n" 16069 " float k = 2;\n" 16070 " int ll = 10000;\n" 16071 "}", 16072 format("void SomeFunction(int parameter= 0) {\n" 16073 " int const i= 1;\n" 16074 " int **j=2;\n" 16075 "int &k=i;\n" 16076 "int &&l=i+j;\n" 16077 " int big = 10000;\n" 16078 "\n" 16079 "unsigned oneTwoThree =123;\n" 16080 "int oneTwo = 12;\n" 16081 " method();\n" 16082 "float k= 2;\n" 16083 "int ll=10000;\n" 16084 "}", 16085 AlignmentLeft)); 16086 // variables are aligned at their name, pointers are at the left most position 16087 verifyFormat("int* a;\n" 16088 "int** b;\n" 16089 "int*** c;\n" 16090 "int foobar;\n", 16091 AlignmentLeft); 16092 16093 // PAS_Middle 16094 FormatStyle AlignmentMiddle = Alignment; 16095 AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle; 16096 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16097 " int const i = 1;\n" 16098 " int * j = 2;\n" 16099 " int big = 10000;\n" 16100 "\n" 16101 " unsigned oneTwoThree = 123;\n" 16102 " int oneTwo = 12;\n" 16103 " method();\n" 16104 " float k = 2;\n" 16105 " int ll = 10000;\n" 16106 "}", 16107 format("void SomeFunction(int parameter= 0) {\n" 16108 " int const i= 1;\n" 16109 " int *j=2;\n" 16110 " int big = 10000;\n" 16111 "\n" 16112 "unsigned oneTwoThree =123;\n" 16113 "int oneTwo = 12;\n" 16114 " method();\n" 16115 "float k= 2;\n" 16116 "int ll=10000;\n" 16117 "}", 16118 AlignmentMiddle)); 16119 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16120 " int const i = 1;\n" 16121 " int ** j = 2, ***k;\n" 16122 " int & k = i;\n" 16123 " int && l = i + j;\n" 16124 " int big = 10000;\n" 16125 "\n" 16126 " unsigned oneTwoThree = 123;\n" 16127 " int oneTwo = 12;\n" 16128 " method();\n" 16129 " float k = 2;\n" 16130 " int ll = 10000;\n" 16131 "}", 16132 format("void SomeFunction(int parameter= 0) {\n" 16133 " int const i= 1;\n" 16134 " int **j=2,***k;\n" 16135 "int &k=i;\n" 16136 "int &&l=i+j;\n" 16137 " int big = 10000;\n" 16138 "\n" 16139 "unsigned oneTwoThree =123;\n" 16140 "int oneTwo = 12;\n" 16141 " method();\n" 16142 "float k= 2;\n" 16143 "int ll=10000;\n" 16144 "}", 16145 AlignmentMiddle)); 16146 // variables are aligned at their name, pointers are in the middle 16147 verifyFormat("int * a;\n" 16148 "int * b;\n" 16149 "int *** c;\n" 16150 "int foobar;\n", 16151 AlignmentMiddle); 16152 16153 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16154 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 16155 verifyFormat("#define A \\\n" 16156 " int aaaa = 12; \\\n" 16157 " float b = 23; \\\n" 16158 " const int ccc = 234; \\\n" 16159 " unsigned dddddddddd = 2345;", 16160 Alignment); 16161 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 16162 verifyFormat("#define A \\\n" 16163 " int aaaa = 12; \\\n" 16164 " float b = 23; \\\n" 16165 " const int ccc = 234; \\\n" 16166 " unsigned dddddddddd = 2345;", 16167 Alignment); 16168 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 16169 Alignment.ColumnLimit = 30; 16170 verifyFormat("#define A \\\n" 16171 " int aaaa = 12; \\\n" 16172 " float b = 23; \\\n" 16173 " const int ccc = 234; \\\n" 16174 " int dddddddddd = 2345;", 16175 Alignment); 16176 Alignment.ColumnLimit = 80; 16177 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 16178 "k = 4, int l = 5,\n" 16179 " int m = 6) {\n" 16180 " const int j = 10;\n" 16181 " otherThing = 1;\n" 16182 "}", 16183 Alignment); 16184 verifyFormat("void SomeFunction(int parameter = 0) {\n" 16185 " int const i = 1;\n" 16186 " int *j = 2;\n" 16187 " int big = 10000;\n" 16188 "}", 16189 Alignment); 16190 verifyFormat("class C {\n" 16191 "public:\n" 16192 " int i = 1;\n" 16193 " virtual void f() = 0;\n" 16194 "};", 16195 Alignment); 16196 verifyFormat("float i = 1;\n" 16197 "if (SomeType t = getSomething()) {\n" 16198 "}\n" 16199 "const unsigned j = 2;\n" 16200 "int big = 10000;", 16201 Alignment); 16202 verifyFormat("float j = 7;\n" 16203 "for (int k = 0; k < N; ++k) {\n" 16204 "}\n" 16205 "unsigned j = 2;\n" 16206 "int big = 10000;\n" 16207 "}", 16208 Alignment); 16209 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 16210 verifyFormat("float i = 1;\n" 16211 "LooooooooooongType loooooooooooooooooooooongVariable\n" 16212 " = someLooooooooooooooooongFunction();\n" 16213 "int j = 2;", 16214 Alignment); 16215 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 16216 verifyFormat("int i = 1;\n" 16217 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 16218 " someLooooooooooooooooongFunction();\n" 16219 "int j = 2;", 16220 Alignment); 16221 16222 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16223 verifyFormat("auto lambda = []() {\n" 16224 " auto ii = 0;\n" 16225 " float j = 0;\n" 16226 " return 0;\n" 16227 "};\n" 16228 "int i = 0;\n" 16229 "float i2 = 0;\n" 16230 "auto v = type{\n" 16231 " i = 1, //\n" 16232 " (i = 2), //\n" 16233 " i = 3 //\n" 16234 "};", 16235 Alignment); 16236 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16237 16238 verifyFormat( 16239 "int i = 1;\n" 16240 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 16241 " loooooooooooooooooooooongParameterB);\n" 16242 "int j = 2;", 16243 Alignment); 16244 16245 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 16246 // We expect declarations and assignments to align, as long as it doesn't 16247 // exceed the column limit, starting a new alignment sequence whenever it 16248 // happens. 16249 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16250 Alignment.ColumnLimit = 30; 16251 verifyFormat("float ii = 1;\n" 16252 "unsigned j = 2;\n" 16253 "int someVerylongVariable = 1;\n" 16254 "AnotherLongType ll = 123456;\n" 16255 "VeryVeryLongType k = 2;\n" 16256 "int myvar = 1;", 16257 Alignment); 16258 Alignment.ColumnLimit = 80; 16259 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16260 16261 verifyFormat( 16262 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 16263 " typename LongType, typename B>\n" 16264 "auto foo() {}\n", 16265 Alignment); 16266 verifyFormat("float a, b = 1;\n" 16267 "int c = 2;\n" 16268 "int dd = 3;\n", 16269 Alignment); 16270 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 16271 "float b[1][] = {{3.f}};\n", 16272 Alignment); 16273 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16274 verifyFormat("float a, b = 1;\n" 16275 "int c = 2;\n" 16276 "int dd = 3;\n", 16277 Alignment); 16278 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 16279 "float b[1][] = {{3.f}};\n", 16280 Alignment); 16281 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16282 16283 Alignment.ColumnLimit = 30; 16284 Alignment.BinPackParameters = false; 16285 verifyFormat("void foo(float a,\n" 16286 " float b,\n" 16287 " int c,\n" 16288 " uint32_t *d) {\n" 16289 " int *e = 0;\n" 16290 " float f = 0;\n" 16291 " double g = 0;\n" 16292 "}\n" 16293 "void bar(ino_t a,\n" 16294 " int b,\n" 16295 " uint32_t *c,\n" 16296 " bool d) {}\n", 16297 Alignment); 16298 Alignment.BinPackParameters = true; 16299 Alignment.ColumnLimit = 80; 16300 16301 // Bug 33507 16302 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 16303 verifyFormat( 16304 "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n" 16305 " static const Version verVs2017;\n" 16306 " return true;\n" 16307 "});\n", 16308 Alignment); 16309 Alignment.PointerAlignment = FormatStyle::PAS_Right; 16310 16311 // See llvm.org/PR35641 16312 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16313 verifyFormat("int func() { //\n" 16314 " int b;\n" 16315 " unsigned c;\n" 16316 "}", 16317 Alignment); 16318 16319 // See PR37175 16320 FormatStyle Style = getMozillaStyle(); 16321 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16322 EXPECT_EQ("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n" 16323 "foo(int a);", 16324 format("DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style)); 16325 16326 Alignment.PointerAlignment = FormatStyle::PAS_Left; 16327 verifyFormat("unsigned int* a;\n" 16328 "int* b;\n" 16329 "unsigned int Const* c;\n" 16330 "unsigned int const* d;\n" 16331 "unsigned int Const& e;\n" 16332 "unsigned int const& f;", 16333 Alignment); 16334 verifyFormat("Const unsigned int* c;\n" 16335 "const unsigned int* d;\n" 16336 "Const unsigned int& e;\n" 16337 "const unsigned int& f;\n" 16338 "const unsigned g;\n" 16339 "Const unsigned h;", 16340 Alignment); 16341 16342 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 16343 verifyFormat("unsigned int * a;\n" 16344 "int * b;\n" 16345 "unsigned int Const * c;\n" 16346 "unsigned int const * d;\n" 16347 "unsigned int Const & e;\n" 16348 "unsigned int const & f;", 16349 Alignment); 16350 verifyFormat("Const unsigned int * c;\n" 16351 "const unsigned int * d;\n" 16352 "Const unsigned int & e;\n" 16353 "const unsigned int & f;\n" 16354 "const unsigned g;\n" 16355 "Const unsigned h;", 16356 Alignment); 16357 } 16358 16359 TEST_F(FormatTest, AlignWithLineBreaks) { 16360 auto Style = getLLVMStyleWithColumns(120); 16361 16362 EXPECT_EQ(Style.AlignConsecutiveAssignments, FormatStyle::ACS_None); 16363 EXPECT_EQ(Style.AlignConsecutiveDeclarations, FormatStyle::ACS_None); 16364 verifyFormat("void foo() {\n" 16365 " int myVar = 5;\n" 16366 " double x = 3.14;\n" 16367 " auto str = \"Hello \"\n" 16368 " \"World\";\n" 16369 " auto s = \"Hello \"\n" 16370 " \"Again\";\n" 16371 "}", 16372 Style); 16373 16374 // clang-format off 16375 verifyFormat("void foo() {\n" 16376 " const int capacityBefore = Entries.capacity();\n" 16377 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16378 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16379 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16380 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16381 "}", 16382 Style); 16383 // clang-format on 16384 16385 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16386 verifyFormat("void foo() {\n" 16387 " int myVar = 5;\n" 16388 " double x = 3.14;\n" 16389 " auto str = \"Hello \"\n" 16390 " \"World\";\n" 16391 " auto s = \"Hello \"\n" 16392 " \"Again\";\n" 16393 "}", 16394 Style); 16395 16396 // clang-format off 16397 verifyFormat("void foo() {\n" 16398 " const int capacityBefore = Entries.capacity();\n" 16399 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16400 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16401 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16402 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16403 "}", 16404 Style); 16405 // clang-format on 16406 16407 Style.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16408 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16409 verifyFormat("void foo() {\n" 16410 " int myVar = 5;\n" 16411 " double x = 3.14;\n" 16412 " auto str = \"Hello \"\n" 16413 " \"World\";\n" 16414 " auto s = \"Hello \"\n" 16415 " \"Again\";\n" 16416 "}", 16417 Style); 16418 16419 // clang-format off 16420 verifyFormat("void foo() {\n" 16421 " const int capacityBefore = Entries.capacity();\n" 16422 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16423 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16424 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16425 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16426 "}", 16427 Style); 16428 // clang-format on 16429 16430 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16431 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16432 16433 verifyFormat("void foo() {\n" 16434 " int myVar = 5;\n" 16435 " double x = 3.14;\n" 16436 " auto str = \"Hello \"\n" 16437 " \"World\";\n" 16438 " auto s = \"Hello \"\n" 16439 " \"Again\";\n" 16440 "}", 16441 Style); 16442 16443 // clang-format off 16444 verifyFormat("void foo() {\n" 16445 " const int capacityBefore = Entries.capacity();\n" 16446 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16447 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16448 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16449 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16450 "}", 16451 Style); 16452 // clang-format on 16453 16454 Style = getLLVMStyleWithColumns(120); 16455 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16456 Style.ContinuationIndentWidth = 4; 16457 Style.IndentWidth = 4; 16458 16459 // clang-format off 16460 verifyFormat("void SomeFunc() {\n" 16461 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 16462 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16463 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 16464 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16465 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 16466 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16467 "}", 16468 Style); 16469 // clang-format on 16470 16471 Style.BinPackArguments = false; 16472 16473 // clang-format off 16474 verifyFormat("void SomeFunc() {\n" 16475 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(\n" 16476 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16477 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(\n" 16478 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16479 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(\n" 16480 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16481 "}", 16482 Style); 16483 // clang-format on 16484 } 16485 16486 TEST_F(FormatTest, AlignWithInitializerPeriods) { 16487 auto Style = getLLVMStyleWithColumns(60); 16488 16489 verifyFormat("void foo1(void) {\n" 16490 " BYTE p[1] = 1;\n" 16491 " A B = {.one_foooooooooooooooo = 2,\n" 16492 " .two_fooooooooooooo = 3,\n" 16493 " .three_fooooooooooooo = 4};\n" 16494 " BYTE payload = 2;\n" 16495 "}", 16496 Style); 16497 16498 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16499 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_None; 16500 verifyFormat("void foo2(void) {\n" 16501 " BYTE p[1] = 1;\n" 16502 " A B = {.one_foooooooooooooooo = 2,\n" 16503 " .two_fooooooooooooo = 3,\n" 16504 " .three_fooooooooooooo = 4};\n" 16505 " BYTE payload = 2;\n" 16506 "}", 16507 Style); 16508 16509 Style.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16510 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16511 verifyFormat("void foo3(void) {\n" 16512 " BYTE p[1] = 1;\n" 16513 " A B = {.one_foooooooooooooooo = 2,\n" 16514 " .two_fooooooooooooo = 3,\n" 16515 " .three_fooooooooooooo = 4};\n" 16516 " BYTE payload = 2;\n" 16517 "}", 16518 Style); 16519 16520 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16521 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16522 verifyFormat("void foo4(void) {\n" 16523 " BYTE p[1] = 1;\n" 16524 " A B = {.one_foooooooooooooooo = 2,\n" 16525 " .two_fooooooooooooo = 3,\n" 16526 " .three_fooooooooooooo = 4};\n" 16527 " BYTE payload = 2;\n" 16528 "}", 16529 Style); 16530 } 16531 16532 TEST_F(FormatTest, LinuxBraceBreaking) { 16533 FormatStyle LinuxBraceStyle = getLLVMStyle(); 16534 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 16535 verifyFormat("namespace a\n" 16536 "{\n" 16537 "class A\n" 16538 "{\n" 16539 " void f()\n" 16540 " {\n" 16541 " if (true) {\n" 16542 " a();\n" 16543 " b();\n" 16544 " } else {\n" 16545 " a();\n" 16546 " }\n" 16547 " }\n" 16548 " void g() { return; }\n" 16549 "};\n" 16550 "struct B {\n" 16551 " int x;\n" 16552 "};\n" 16553 "} // namespace a\n", 16554 LinuxBraceStyle); 16555 verifyFormat("enum X {\n" 16556 " Y = 0,\n" 16557 "}\n", 16558 LinuxBraceStyle); 16559 verifyFormat("struct S {\n" 16560 " int Type;\n" 16561 " union {\n" 16562 " int x;\n" 16563 " double y;\n" 16564 " } Value;\n" 16565 " class C\n" 16566 " {\n" 16567 " MyFavoriteType Value;\n" 16568 " } Class;\n" 16569 "}\n", 16570 LinuxBraceStyle); 16571 } 16572 16573 TEST_F(FormatTest, MozillaBraceBreaking) { 16574 FormatStyle MozillaBraceStyle = getLLVMStyle(); 16575 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 16576 MozillaBraceStyle.FixNamespaceComments = false; 16577 verifyFormat("namespace a {\n" 16578 "class A\n" 16579 "{\n" 16580 " void f()\n" 16581 " {\n" 16582 " if (true) {\n" 16583 " a();\n" 16584 " b();\n" 16585 " }\n" 16586 " }\n" 16587 " void g() { return; }\n" 16588 "};\n" 16589 "enum E\n" 16590 "{\n" 16591 " A,\n" 16592 " // foo\n" 16593 " B,\n" 16594 " C\n" 16595 "};\n" 16596 "struct B\n" 16597 "{\n" 16598 " int x;\n" 16599 "};\n" 16600 "}\n", 16601 MozillaBraceStyle); 16602 verifyFormat("struct S\n" 16603 "{\n" 16604 " int Type;\n" 16605 " union\n" 16606 " {\n" 16607 " int x;\n" 16608 " double y;\n" 16609 " } Value;\n" 16610 " class C\n" 16611 " {\n" 16612 " MyFavoriteType Value;\n" 16613 " } Class;\n" 16614 "}\n", 16615 MozillaBraceStyle); 16616 } 16617 16618 TEST_F(FormatTest, StroustrupBraceBreaking) { 16619 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 16620 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 16621 verifyFormat("namespace a {\n" 16622 "class A {\n" 16623 " void f()\n" 16624 " {\n" 16625 " if (true) {\n" 16626 " a();\n" 16627 " b();\n" 16628 " }\n" 16629 " }\n" 16630 " void g() { return; }\n" 16631 "};\n" 16632 "struct B {\n" 16633 " int x;\n" 16634 "};\n" 16635 "} // namespace a\n", 16636 StroustrupBraceStyle); 16637 16638 verifyFormat("void foo()\n" 16639 "{\n" 16640 " if (a) {\n" 16641 " a();\n" 16642 " }\n" 16643 " else {\n" 16644 " b();\n" 16645 " }\n" 16646 "}\n", 16647 StroustrupBraceStyle); 16648 16649 verifyFormat("#ifdef _DEBUG\n" 16650 "int foo(int i = 0)\n" 16651 "#else\n" 16652 "int foo(int i = 5)\n" 16653 "#endif\n" 16654 "{\n" 16655 " return i;\n" 16656 "}", 16657 StroustrupBraceStyle); 16658 16659 verifyFormat("void foo() {}\n" 16660 "void bar()\n" 16661 "#ifdef _DEBUG\n" 16662 "{\n" 16663 " foo();\n" 16664 "}\n" 16665 "#else\n" 16666 "{\n" 16667 "}\n" 16668 "#endif", 16669 StroustrupBraceStyle); 16670 16671 verifyFormat("void foobar() { int i = 5; }\n" 16672 "#ifdef _DEBUG\n" 16673 "void bar() {}\n" 16674 "#else\n" 16675 "void bar() { foobar(); }\n" 16676 "#endif", 16677 StroustrupBraceStyle); 16678 } 16679 16680 TEST_F(FormatTest, AllmanBraceBreaking) { 16681 FormatStyle AllmanBraceStyle = getLLVMStyle(); 16682 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 16683 16684 EXPECT_EQ("namespace a\n" 16685 "{\n" 16686 "void f();\n" 16687 "void g();\n" 16688 "} // namespace a\n", 16689 format("namespace a\n" 16690 "{\n" 16691 "void f();\n" 16692 "void g();\n" 16693 "}\n", 16694 AllmanBraceStyle)); 16695 16696 verifyFormat("namespace a\n" 16697 "{\n" 16698 "class A\n" 16699 "{\n" 16700 " void f()\n" 16701 " {\n" 16702 " if (true)\n" 16703 " {\n" 16704 " a();\n" 16705 " b();\n" 16706 " }\n" 16707 " }\n" 16708 " void g() { return; }\n" 16709 "};\n" 16710 "struct B\n" 16711 "{\n" 16712 " int x;\n" 16713 "};\n" 16714 "union C\n" 16715 "{\n" 16716 "};\n" 16717 "} // namespace a", 16718 AllmanBraceStyle); 16719 16720 verifyFormat("void f()\n" 16721 "{\n" 16722 " if (true)\n" 16723 " {\n" 16724 " a();\n" 16725 " }\n" 16726 " else if (false)\n" 16727 " {\n" 16728 " b();\n" 16729 " }\n" 16730 " else\n" 16731 " {\n" 16732 " c();\n" 16733 " }\n" 16734 "}\n", 16735 AllmanBraceStyle); 16736 16737 verifyFormat("void f()\n" 16738 "{\n" 16739 " for (int i = 0; i < 10; ++i)\n" 16740 " {\n" 16741 " a();\n" 16742 " }\n" 16743 " while (false)\n" 16744 " {\n" 16745 " b();\n" 16746 " }\n" 16747 " do\n" 16748 " {\n" 16749 " c();\n" 16750 " } while (false)\n" 16751 "}\n", 16752 AllmanBraceStyle); 16753 16754 verifyFormat("void f(int a)\n" 16755 "{\n" 16756 " switch (a)\n" 16757 " {\n" 16758 " case 0:\n" 16759 " break;\n" 16760 " case 1:\n" 16761 " {\n" 16762 " break;\n" 16763 " }\n" 16764 " case 2:\n" 16765 " {\n" 16766 " }\n" 16767 " break;\n" 16768 " default:\n" 16769 " break;\n" 16770 " }\n" 16771 "}\n", 16772 AllmanBraceStyle); 16773 16774 verifyFormat("enum X\n" 16775 "{\n" 16776 " Y = 0,\n" 16777 "}\n", 16778 AllmanBraceStyle); 16779 verifyFormat("enum X\n" 16780 "{\n" 16781 " Y = 0\n" 16782 "}\n", 16783 AllmanBraceStyle); 16784 16785 verifyFormat("@interface BSApplicationController ()\n" 16786 "{\n" 16787 "@private\n" 16788 " id _extraIvar;\n" 16789 "}\n" 16790 "@end\n", 16791 AllmanBraceStyle); 16792 16793 verifyFormat("#ifdef _DEBUG\n" 16794 "int foo(int i = 0)\n" 16795 "#else\n" 16796 "int foo(int i = 5)\n" 16797 "#endif\n" 16798 "{\n" 16799 " return i;\n" 16800 "}", 16801 AllmanBraceStyle); 16802 16803 verifyFormat("void foo() {}\n" 16804 "void bar()\n" 16805 "#ifdef _DEBUG\n" 16806 "{\n" 16807 " foo();\n" 16808 "}\n" 16809 "#else\n" 16810 "{\n" 16811 "}\n" 16812 "#endif", 16813 AllmanBraceStyle); 16814 16815 verifyFormat("void foobar() { int i = 5; }\n" 16816 "#ifdef _DEBUG\n" 16817 "void bar() {}\n" 16818 "#else\n" 16819 "void bar() { foobar(); }\n" 16820 "#endif", 16821 AllmanBraceStyle); 16822 16823 EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine, 16824 FormatStyle::SLS_All); 16825 16826 verifyFormat("[](int i) { return i + 2; };\n" 16827 "[](int i, int j)\n" 16828 "{\n" 16829 " auto x = i + j;\n" 16830 " auto y = i * j;\n" 16831 " return x ^ y;\n" 16832 "};\n" 16833 "void foo()\n" 16834 "{\n" 16835 " auto shortLambda = [](int i) { return i + 2; };\n" 16836 " auto longLambda = [](int i, int j)\n" 16837 " {\n" 16838 " auto x = i + j;\n" 16839 " auto y = i * j;\n" 16840 " return x ^ y;\n" 16841 " };\n" 16842 "}", 16843 AllmanBraceStyle); 16844 16845 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 16846 16847 verifyFormat("[](int i)\n" 16848 "{\n" 16849 " return i + 2;\n" 16850 "};\n" 16851 "[](int i, int j)\n" 16852 "{\n" 16853 " auto x = i + j;\n" 16854 " auto y = i * j;\n" 16855 " return x ^ y;\n" 16856 "};\n" 16857 "void foo()\n" 16858 "{\n" 16859 " auto shortLambda = [](int i)\n" 16860 " {\n" 16861 " return i + 2;\n" 16862 " };\n" 16863 " auto longLambda = [](int i, int j)\n" 16864 " {\n" 16865 " auto x = i + j;\n" 16866 " auto y = i * j;\n" 16867 " return x ^ y;\n" 16868 " };\n" 16869 "}", 16870 AllmanBraceStyle); 16871 16872 // Reset 16873 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All; 16874 16875 // This shouldn't affect ObjC blocks.. 16876 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 16877 " // ...\n" 16878 " int i;\n" 16879 "}];", 16880 AllmanBraceStyle); 16881 verifyFormat("void (^block)(void) = ^{\n" 16882 " // ...\n" 16883 " int i;\n" 16884 "};", 16885 AllmanBraceStyle); 16886 // .. or dict literals. 16887 verifyFormat("void f()\n" 16888 "{\n" 16889 " // ...\n" 16890 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 16891 "}", 16892 AllmanBraceStyle); 16893 verifyFormat("void f()\n" 16894 "{\n" 16895 " // ...\n" 16896 " [object someMethod:@{a : @\"b\"}];\n" 16897 "}", 16898 AllmanBraceStyle); 16899 verifyFormat("int f()\n" 16900 "{ // comment\n" 16901 " return 42;\n" 16902 "}", 16903 AllmanBraceStyle); 16904 16905 AllmanBraceStyle.ColumnLimit = 19; 16906 verifyFormat("void f() { int i; }", AllmanBraceStyle); 16907 AllmanBraceStyle.ColumnLimit = 18; 16908 verifyFormat("void f()\n" 16909 "{\n" 16910 " int i;\n" 16911 "}", 16912 AllmanBraceStyle); 16913 AllmanBraceStyle.ColumnLimit = 80; 16914 16915 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 16916 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = 16917 FormatStyle::SIS_WithoutElse; 16918 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 16919 verifyFormat("void f(bool b)\n" 16920 "{\n" 16921 " if (b)\n" 16922 " {\n" 16923 " return;\n" 16924 " }\n" 16925 "}\n", 16926 BreakBeforeBraceShortIfs); 16927 verifyFormat("void f(bool b)\n" 16928 "{\n" 16929 " if constexpr (b)\n" 16930 " {\n" 16931 " return;\n" 16932 " }\n" 16933 "}\n", 16934 BreakBeforeBraceShortIfs); 16935 verifyFormat("void f(bool b)\n" 16936 "{\n" 16937 " if CONSTEXPR (b)\n" 16938 " {\n" 16939 " return;\n" 16940 " }\n" 16941 "}\n", 16942 BreakBeforeBraceShortIfs); 16943 verifyFormat("void f(bool b)\n" 16944 "{\n" 16945 " if (b) return;\n" 16946 "}\n", 16947 BreakBeforeBraceShortIfs); 16948 verifyFormat("void f(bool b)\n" 16949 "{\n" 16950 " if constexpr (b) return;\n" 16951 "}\n", 16952 BreakBeforeBraceShortIfs); 16953 verifyFormat("void f(bool b)\n" 16954 "{\n" 16955 " if CONSTEXPR (b) return;\n" 16956 "}\n", 16957 BreakBeforeBraceShortIfs); 16958 verifyFormat("void f(bool b)\n" 16959 "{\n" 16960 " while (b)\n" 16961 " {\n" 16962 " return;\n" 16963 " }\n" 16964 "}\n", 16965 BreakBeforeBraceShortIfs); 16966 } 16967 16968 TEST_F(FormatTest, WhitesmithsBraceBreaking) { 16969 FormatStyle WhitesmithsBraceStyle = getLLVMStyle(); 16970 WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 16971 16972 // Make a few changes to the style for testing purposes 16973 WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine = 16974 FormatStyle::SFS_Empty; 16975 WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 16976 WhitesmithsBraceStyle.ColumnLimit = 0; 16977 16978 // FIXME: this test case can't decide whether there should be a blank line 16979 // after the ~D() line or not. It adds one if one doesn't exist in the test 16980 // and it removes the line if one exists. 16981 /* 16982 verifyFormat("class A;\n" 16983 "namespace B\n" 16984 " {\n" 16985 "class C;\n" 16986 "// Comment\n" 16987 "class D\n" 16988 " {\n" 16989 "public:\n" 16990 " D();\n" 16991 " ~D() {}\n" 16992 "private:\n" 16993 " enum E\n" 16994 " {\n" 16995 " F\n" 16996 " }\n" 16997 " };\n" 16998 " } // namespace B\n", 16999 WhitesmithsBraceStyle); 17000 */ 17001 17002 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None; 17003 verifyFormat("namespace a\n" 17004 " {\n" 17005 "class A\n" 17006 " {\n" 17007 " void f()\n" 17008 " {\n" 17009 " if (true)\n" 17010 " {\n" 17011 " a();\n" 17012 " b();\n" 17013 " }\n" 17014 " }\n" 17015 " void g()\n" 17016 " {\n" 17017 " return;\n" 17018 " }\n" 17019 " };\n" 17020 "struct B\n" 17021 " {\n" 17022 " int x;\n" 17023 " };\n" 17024 " } // namespace a", 17025 WhitesmithsBraceStyle); 17026 17027 verifyFormat("namespace a\n" 17028 " {\n" 17029 "namespace b\n" 17030 " {\n" 17031 "class A\n" 17032 " {\n" 17033 " void f()\n" 17034 " {\n" 17035 " if (true)\n" 17036 " {\n" 17037 " a();\n" 17038 " b();\n" 17039 " }\n" 17040 " }\n" 17041 " void g()\n" 17042 " {\n" 17043 " return;\n" 17044 " }\n" 17045 " };\n" 17046 "struct B\n" 17047 " {\n" 17048 " int x;\n" 17049 " };\n" 17050 " } // namespace b\n" 17051 " } // namespace a", 17052 WhitesmithsBraceStyle); 17053 17054 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner; 17055 verifyFormat("namespace a\n" 17056 " {\n" 17057 "namespace b\n" 17058 " {\n" 17059 " class A\n" 17060 " {\n" 17061 " void f()\n" 17062 " {\n" 17063 " if (true)\n" 17064 " {\n" 17065 " a();\n" 17066 " b();\n" 17067 " }\n" 17068 " }\n" 17069 " void g()\n" 17070 " {\n" 17071 " return;\n" 17072 " }\n" 17073 " };\n" 17074 " struct B\n" 17075 " {\n" 17076 " int x;\n" 17077 " };\n" 17078 " } // namespace b\n" 17079 " } // namespace a", 17080 WhitesmithsBraceStyle); 17081 17082 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All; 17083 verifyFormat("namespace a\n" 17084 " {\n" 17085 " namespace b\n" 17086 " {\n" 17087 " class A\n" 17088 " {\n" 17089 " void f()\n" 17090 " {\n" 17091 " if (true)\n" 17092 " {\n" 17093 " a();\n" 17094 " b();\n" 17095 " }\n" 17096 " }\n" 17097 " void g()\n" 17098 " {\n" 17099 " return;\n" 17100 " }\n" 17101 " };\n" 17102 " struct B\n" 17103 " {\n" 17104 " int x;\n" 17105 " };\n" 17106 " } // namespace b\n" 17107 " } // namespace a", 17108 WhitesmithsBraceStyle); 17109 17110 verifyFormat("void f()\n" 17111 " {\n" 17112 " if (true)\n" 17113 " {\n" 17114 " a();\n" 17115 " }\n" 17116 " else if (false)\n" 17117 " {\n" 17118 " b();\n" 17119 " }\n" 17120 " else\n" 17121 " {\n" 17122 " c();\n" 17123 " }\n" 17124 " }\n", 17125 WhitesmithsBraceStyle); 17126 17127 verifyFormat("void f()\n" 17128 " {\n" 17129 " for (int i = 0; i < 10; ++i)\n" 17130 " {\n" 17131 " a();\n" 17132 " }\n" 17133 " while (false)\n" 17134 " {\n" 17135 " b();\n" 17136 " }\n" 17137 " do\n" 17138 " {\n" 17139 " c();\n" 17140 " } while (false)\n" 17141 " }\n", 17142 WhitesmithsBraceStyle); 17143 17144 WhitesmithsBraceStyle.IndentCaseLabels = true; 17145 verifyFormat("void switchTest1(int a)\n" 17146 " {\n" 17147 " switch (a)\n" 17148 " {\n" 17149 " case 2:\n" 17150 " {\n" 17151 " }\n" 17152 " break;\n" 17153 " }\n" 17154 " }\n", 17155 WhitesmithsBraceStyle); 17156 17157 verifyFormat("void switchTest2(int a)\n" 17158 " {\n" 17159 " switch (a)\n" 17160 " {\n" 17161 " case 0:\n" 17162 " break;\n" 17163 " case 1:\n" 17164 " {\n" 17165 " break;\n" 17166 " }\n" 17167 " case 2:\n" 17168 " {\n" 17169 " }\n" 17170 " break;\n" 17171 " default:\n" 17172 " break;\n" 17173 " }\n" 17174 " }\n", 17175 WhitesmithsBraceStyle); 17176 17177 verifyFormat("void switchTest3(int a)\n" 17178 " {\n" 17179 " switch (a)\n" 17180 " {\n" 17181 " case 0:\n" 17182 " {\n" 17183 " foo(x);\n" 17184 " }\n" 17185 " break;\n" 17186 " default:\n" 17187 " {\n" 17188 " foo(1);\n" 17189 " }\n" 17190 " break;\n" 17191 " }\n" 17192 " }\n", 17193 WhitesmithsBraceStyle); 17194 17195 WhitesmithsBraceStyle.IndentCaseLabels = false; 17196 17197 verifyFormat("void switchTest4(int a)\n" 17198 " {\n" 17199 " switch (a)\n" 17200 " {\n" 17201 " case 2:\n" 17202 " {\n" 17203 " }\n" 17204 " break;\n" 17205 " }\n" 17206 " }\n", 17207 WhitesmithsBraceStyle); 17208 17209 verifyFormat("void switchTest5(int a)\n" 17210 " {\n" 17211 " switch (a)\n" 17212 " {\n" 17213 " case 0:\n" 17214 " break;\n" 17215 " case 1:\n" 17216 " {\n" 17217 " foo();\n" 17218 " break;\n" 17219 " }\n" 17220 " case 2:\n" 17221 " {\n" 17222 " }\n" 17223 " break;\n" 17224 " default:\n" 17225 " break;\n" 17226 " }\n" 17227 " }\n", 17228 WhitesmithsBraceStyle); 17229 17230 verifyFormat("void switchTest6(int a)\n" 17231 " {\n" 17232 " switch (a)\n" 17233 " {\n" 17234 " case 0:\n" 17235 " {\n" 17236 " foo(x);\n" 17237 " }\n" 17238 " break;\n" 17239 " default:\n" 17240 " {\n" 17241 " foo(1);\n" 17242 " }\n" 17243 " break;\n" 17244 " }\n" 17245 " }\n", 17246 WhitesmithsBraceStyle); 17247 17248 verifyFormat("enum X\n" 17249 " {\n" 17250 " Y = 0, // testing\n" 17251 " }\n", 17252 WhitesmithsBraceStyle); 17253 17254 verifyFormat("enum X\n" 17255 " {\n" 17256 " Y = 0\n" 17257 " }\n", 17258 WhitesmithsBraceStyle); 17259 verifyFormat("enum X\n" 17260 " {\n" 17261 " Y = 0,\n" 17262 " Z = 1\n" 17263 " };\n", 17264 WhitesmithsBraceStyle); 17265 17266 verifyFormat("@interface BSApplicationController ()\n" 17267 " {\n" 17268 "@private\n" 17269 " id _extraIvar;\n" 17270 " }\n" 17271 "@end\n", 17272 WhitesmithsBraceStyle); 17273 17274 verifyFormat("#ifdef _DEBUG\n" 17275 "int foo(int i = 0)\n" 17276 "#else\n" 17277 "int foo(int i = 5)\n" 17278 "#endif\n" 17279 " {\n" 17280 " return i;\n" 17281 " }", 17282 WhitesmithsBraceStyle); 17283 17284 verifyFormat("void foo() {}\n" 17285 "void bar()\n" 17286 "#ifdef _DEBUG\n" 17287 " {\n" 17288 " foo();\n" 17289 " }\n" 17290 "#else\n" 17291 " {\n" 17292 " }\n" 17293 "#endif", 17294 WhitesmithsBraceStyle); 17295 17296 verifyFormat("void foobar()\n" 17297 " {\n" 17298 " int i = 5;\n" 17299 " }\n" 17300 "#ifdef _DEBUG\n" 17301 "void bar()\n" 17302 " {\n" 17303 " }\n" 17304 "#else\n" 17305 "void bar()\n" 17306 " {\n" 17307 " foobar();\n" 17308 " }\n" 17309 "#endif", 17310 WhitesmithsBraceStyle); 17311 17312 // This shouldn't affect ObjC blocks.. 17313 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 17314 " // ...\n" 17315 " int i;\n" 17316 "}];", 17317 WhitesmithsBraceStyle); 17318 verifyFormat("void (^block)(void) = ^{\n" 17319 " // ...\n" 17320 " int i;\n" 17321 "};", 17322 WhitesmithsBraceStyle); 17323 // .. or dict literals. 17324 verifyFormat("void f()\n" 17325 " {\n" 17326 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 17327 " }", 17328 WhitesmithsBraceStyle); 17329 17330 verifyFormat("int f()\n" 17331 " { // comment\n" 17332 " return 42;\n" 17333 " }", 17334 WhitesmithsBraceStyle); 17335 17336 FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle; 17337 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = 17338 FormatStyle::SIS_OnlyFirstIf; 17339 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 17340 verifyFormat("void f(bool b)\n" 17341 " {\n" 17342 " if (b)\n" 17343 " {\n" 17344 " return;\n" 17345 " }\n" 17346 " }\n", 17347 BreakBeforeBraceShortIfs); 17348 verifyFormat("void f(bool b)\n" 17349 " {\n" 17350 " if (b) return;\n" 17351 " }\n", 17352 BreakBeforeBraceShortIfs); 17353 verifyFormat("void f(bool b)\n" 17354 " {\n" 17355 " while (b)\n" 17356 " {\n" 17357 " return;\n" 17358 " }\n" 17359 " }\n", 17360 BreakBeforeBraceShortIfs); 17361 } 17362 17363 TEST_F(FormatTest, GNUBraceBreaking) { 17364 FormatStyle GNUBraceStyle = getLLVMStyle(); 17365 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 17366 verifyFormat("namespace a\n" 17367 "{\n" 17368 "class A\n" 17369 "{\n" 17370 " void f()\n" 17371 " {\n" 17372 " int a;\n" 17373 " {\n" 17374 " int b;\n" 17375 " }\n" 17376 " if (true)\n" 17377 " {\n" 17378 " a();\n" 17379 " b();\n" 17380 " }\n" 17381 " }\n" 17382 " void g() { return; }\n" 17383 "}\n" 17384 "} // namespace a", 17385 GNUBraceStyle); 17386 17387 verifyFormat("void f()\n" 17388 "{\n" 17389 " if (true)\n" 17390 " {\n" 17391 " a();\n" 17392 " }\n" 17393 " else if (false)\n" 17394 " {\n" 17395 " b();\n" 17396 " }\n" 17397 " else\n" 17398 " {\n" 17399 " c();\n" 17400 " }\n" 17401 "}\n", 17402 GNUBraceStyle); 17403 17404 verifyFormat("void f()\n" 17405 "{\n" 17406 " for (int i = 0; i < 10; ++i)\n" 17407 " {\n" 17408 " a();\n" 17409 " }\n" 17410 " while (false)\n" 17411 " {\n" 17412 " b();\n" 17413 " }\n" 17414 " do\n" 17415 " {\n" 17416 " c();\n" 17417 " }\n" 17418 " while (false);\n" 17419 "}\n", 17420 GNUBraceStyle); 17421 17422 verifyFormat("void f(int a)\n" 17423 "{\n" 17424 " switch (a)\n" 17425 " {\n" 17426 " case 0:\n" 17427 " break;\n" 17428 " case 1:\n" 17429 " {\n" 17430 " break;\n" 17431 " }\n" 17432 " case 2:\n" 17433 " {\n" 17434 " }\n" 17435 " break;\n" 17436 " default:\n" 17437 " break;\n" 17438 " }\n" 17439 "}\n", 17440 GNUBraceStyle); 17441 17442 verifyFormat("enum X\n" 17443 "{\n" 17444 " Y = 0,\n" 17445 "}\n", 17446 GNUBraceStyle); 17447 17448 verifyFormat("@interface BSApplicationController ()\n" 17449 "{\n" 17450 "@private\n" 17451 " id _extraIvar;\n" 17452 "}\n" 17453 "@end\n", 17454 GNUBraceStyle); 17455 17456 verifyFormat("#ifdef _DEBUG\n" 17457 "int foo(int i = 0)\n" 17458 "#else\n" 17459 "int foo(int i = 5)\n" 17460 "#endif\n" 17461 "{\n" 17462 " return i;\n" 17463 "}", 17464 GNUBraceStyle); 17465 17466 verifyFormat("void foo() {}\n" 17467 "void bar()\n" 17468 "#ifdef _DEBUG\n" 17469 "{\n" 17470 " foo();\n" 17471 "}\n" 17472 "#else\n" 17473 "{\n" 17474 "}\n" 17475 "#endif", 17476 GNUBraceStyle); 17477 17478 verifyFormat("void foobar() { int i = 5; }\n" 17479 "#ifdef _DEBUG\n" 17480 "void bar() {}\n" 17481 "#else\n" 17482 "void bar() { foobar(); }\n" 17483 "#endif", 17484 GNUBraceStyle); 17485 } 17486 17487 TEST_F(FormatTest, WebKitBraceBreaking) { 17488 FormatStyle WebKitBraceStyle = getLLVMStyle(); 17489 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 17490 WebKitBraceStyle.FixNamespaceComments = false; 17491 verifyFormat("namespace a {\n" 17492 "class A {\n" 17493 " void f()\n" 17494 " {\n" 17495 " if (true) {\n" 17496 " a();\n" 17497 " b();\n" 17498 " }\n" 17499 " }\n" 17500 " void g() { return; }\n" 17501 "};\n" 17502 "enum E {\n" 17503 " A,\n" 17504 " // foo\n" 17505 " B,\n" 17506 " C\n" 17507 "};\n" 17508 "struct B {\n" 17509 " int x;\n" 17510 "};\n" 17511 "}\n", 17512 WebKitBraceStyle); 17513 verifyFormat("struct S {\n" 17514 " int Type;\n" 17515 " union {\n" 17516 " int x;\n" 17517 " double y;\n" 17518 " } Value;\n" 17519 " class C {\n" 17520 " MyFavoriteType Value;\n" 17521 " } Class;\n" 17522 "};\n", 17523 WebKitBraceStyle); 17524 } 17525 17526 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 17527 verifyFormat("void f() {\n" 17528 " try {\n" 17529 " } catch (const Exception &e) {\n" 17530 " }\n" 17531 "}\n", 17532 getLLVMStyle()); 17533 } 17534 17535 TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) { 17536 auto Style = getLLVMStyle(); 17537 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 17538 Style.AlignConsecutiveAssignments = 17539 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 17540 Style.AlignConsecutiveDeclarations = 17541 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 17542 verifyFormat("struct test demo[] = {\n" 17543 " {56, 23, \"hello\"},\n" 17544 " {-1, 93463, \"world\"},\n" 17545 " { 7, 5, \"!!\"}\n" 17546 "};\n", 17547 Style); 17548 17549 verifyFormat("struct test demo[] = {\n" 17550 " {56, 23, \"hello\"}, // first line\n" 17551 " {-1, 93463, \"world\"}, // second line\n" 17552 " { 7, 5, \"!!\"} // third line\n" 17553 "};\n", 17554 Style); 17555 17556 verifyFormat("struct test demo[4] = {\n" 17557 " { 56, 23, 21, \"oh\"}, // first line\n" 17558 " { -1, 93463, 22, \"my\"}, // second line\n" 17559 " { 7, 5, 1, \"goodness\"} // third line\n" 17560 " {234, 5, 1, \"gracious\"} // fourth line\n" 17561 "};\n", 17562 Style); 17563 17564 verifyFormat("struct test demo[3] = {\n" 17565 " {56, 23, \"hello\"},\n" 17566 " {-1, 93463, \"world\"},\n" 17567 " { 7, 5, \"!!\"}\n" 17568 "};\n", 17569 Style); 17570 17571 verifyFormat("struct test demo[3] = {\n" 17572 " {int{56}, 23, \"hello\"},\n" 17573 " {int{-1}, 93463, \"world\"},\n" 17574 " { int{7}, 5, \"!!\"}\n" 17575 "};\n", 17576 Style); 17577 17578 verifyFormat("struct test demo[] = {\n" 17579 " {56, 23, \"hello\"},\n" 17580 " {-1, 93463, \"world\"},\n" 17581 " { 7, 5, \"!!\"},\n" 17582 "};\n", 17583 Style); 17584 17585 verifyFormat("test demo[] = {\n" 17586 " {56, 23, \"hello\"},\n" 17587 " {-1, 93463, \"world\"},\n" 17588 " { 7, 5, \"!!\"},\n" 17589 "};\n", 17590 Style); 17591 17592 verifyFormat("demo = std::array<struct test, 3>{\n" 17593 " test{56, 23, \"hello\"},\n" 17594 " test{-1, 93463, \"world\"},\n" 17595 " test{ 7, 5, \"!!\"},\n" 17596 "};\n", 17597 Style); 17598 17599 verifyFormat("test demo[] = {\n" 17600 " {56, 23, \"hello\"},\n" 17601 "#if X\n" 17602 " {-1, 93463, \"world\"},\n" 17603 "#endif\n" 17604 " { 7, 5, \"!!\"}\n" 17605 "};\n", 17606 Style); 17607 17608 verifyFormat( 17609 "test demo[] = {\n" 17610 " { 7, 23,\n" 17611 " \"hello world i am a very long line that really, in any\"\n" 17612 " \"just world, ought to be split over multiple lines\"},\n" 17613 " {-1, 93463, \"world\"},\n" 17614 " {56, 5, \"!!\"}\n" 17615 "};\n", 17616 Style); 17617 17618 verifyFormat("return GradForUnaryCwise(g, {\n" 17619 " {{\"sign\"}, \"Sign\", " 17620 " {\"x\", \"dy\"}},\n" 17621 " { {\"dx\"}, \"Mul\", {\"dy\"" 17622 ", \"sign\"}},\n" 17623 "});\n", 17624 Style); 17625 17626 Style.ColumnLimit = 0; 17627 EXPECT_EQ( 17628 "test demo[] = {\n" 17629 " {56, 23, \"hello world i am a very long line that really, " 17630 "in any just world, ought to be split over multiple lines\"},\n" 17631 " {-1, 93463, " 17632 " \"world\"},\n" 17633 " { 7, 5, " 17634 " \"!!\"},\n" 17635 "};", 17636 format("test demo[] = {{56, 23, \"hello world i am a very long line " 17637 "that really, in any just world, ought to be split over multiple " 17638 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 17639 Style)); 17640 17641 Style.ColumnLimit = 80; 17642 verifyFormat("test demo[] = {\n" 17643 " {56, 23, /* a comment */ \"hello\"},\n" 17644 " {-1, 93463, \"world\"},\n" 17645 " { 7, 5, \"!!\"}\n" 17646 "};\n", 17647 Style); 17648 17649 verifyFormat("test demo[] = {\n" 17650 " {56, 23, \"hello\"},\n" 17651 " {-1, 93463, \"world\" /* comment here */},\n" 17652 " { 7, 5, \"!!\"}\n" 17653 "};\n", 17654 Style); 17655 17656 verifyFormat("test demo[] = {\n" 17657 " {56, /* a comment */ 23, \"hello\"},\n" 17658 " {-1, 93463, \"world\"},\n" 17659 " { 7, 5, \"!!\"}\n" 17660 "};\n", 17661 Style); 17662 17663 Style.ColumnLimit = 20; 17664 EXPECT_EQ( 17665 "demo = std::array<\n" 17666 " struct test, 3>{\n" 17667 " test{\n" 17668 " 56, 23,\n" 17669 " \"hello \"\n" 17670 " \"world i \"\n" 17671 " \"am a very \"\n" 17672 " \"long line \"\n" 17673 " \"that \"\n" 17674 " \"really, \"\n" 17675 " \"in any \"\n" 17676 " \"just \"\n" 17677 " \"world, \"\n" 17678 " \"ought to \"\n" 17679 " \"be split \"\n" 17680 " \"over \"\n" 17681 " \"multiple \"\n" 17682 " \"lines\"},\n" 17683 " test{-1, 93463,\n" 17684 " \"world\"},\n" 17685 " test{ 7, 5,\n" 17686 " \"!!\" },\n" 17687 "};", 17688 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world " 17689 "i am a very long line that really, in any just world, ought " 17690 "to be split over multiple lines\"},test{-1, 93463, \"world\"}," 17691 "test{7, 5, \"!!\"},};", 17692 Style)); 17693 // This caused a core dump by enabling Alignment in the LLVMStyle globally 17694 Style = getLLVMStyleWithColumns(50); 17695 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 17696 verifyFormat("static A x = {\n" 17697 " {{init1, init2, init3, init4},\n" 17698 " {init1, init2, init3, init4}}\n" 17699 "};", 17700 Style); 17701 Style.ColumnLimit = 100; 17702 EXPECT_EQ( 17703 "test demo[] = {\n" 17704 " {56, 23,\n" 17705 " \"hello world i am a very long line that really, in any just world" 17706 ", ought to be split over \"\n" 17707 " \"multiple lines\" },\n" 17708 " {-1, 93463, \"world\"},\n" 17709 " { 7, 5, \"!!\"},\n" 17710 "};", 17711 format("test demo[] = {{56, 23, \"hello world i am a very long line " 17712 "that really, in any just world, ought to be split over multiple " 17713 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 17714 Style)); 17715 17716 Style = getLLVMStyleWithColumns(50); 17717 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 17718 Style.AlignConsecutiveAssignments = 17719 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 17720 Style.AlignConsecutiveDeclarations = 17721 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 17722 verifyFormat("struct test demo[] = {\n" 17723 " {56, 23, \"hello\"},\n" 17724 " {-1, 93463, \"world\"},\n" 17725 " { 7, 5, \"!!\"}\n" 17726 "};\n" 17727 "static A x = {\n" 17728 " {{init1, init2, init3, init4},\n" 17729 " {init1, init2, init3, init4}}\n" 17730 "};", 17731 Style); 17732 Style.ColumnLimit = 100; 17733 Style.AlignConsecutiveAssignments = 17734 FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments; 17735 Style.AlignConsecutiveDeclarations = 17736 FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments; 17737 verifyFormat("struct test demo[] = {\n" 17738 " {56, 23, \"hello\"},\n" 17739 " {-1, 93463, \"world\"},\n" 17740 " { 7, 5, \"!!\"}\n" 17741 "};\n" 17742 "struct test demo[4] = {\n" 17743 " { 56, 23, 21, \"oh\"}, // first line\n" 17744 " { -1, 93463, 22, \"my\"}, // second line\n" 17745 " { 7, 5, 1, \"goodness\"} // third line\n" 17746 " {234, 5, 1, \"gracious\"} // fourth line\n" 17747 "};\n", 17748 Style); 17749 EXPECT_EQ( 17750 "test demo[] = {\n" 17751 " {56,\n" 17752 " \"hello world i am a very long line that really, in any just world" 17753 ", ought to be split over \"\n" 17754 " \"multiple lines\", 23},\n" 17755 " {-1, \"world\", 93463},\n" 17756 " { 7, \"!!\", 5},\n" 17757 "};", 17758 format("test demo[] = {{56, \"hello world i am a very long line " 17759 "that really, in any just world, ought to be split over multiple " 17760 "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};", 17761 Style)); 17762 } 17763 17764 TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) { 17765 auto Style = getLLVMStyle(); 17766 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; 17767 verifyFormat("struct test demo[] = {\n" 17768 " {56, 23, \"hello\"},\n" 17769 " {-1, 93463, \"world\"},\n" 17770 " {7, 5, \"!!\" }\n" 17771 "};\n", 17772 Style); 17773 17774 verifyFormat("struct test demo[] = {\n" 17775 " {56, 23, \"hello\"}, // first line\n" 17776 " {-1, 93463, \"world\"}, // second line\n" 17777 " {7, 5, \"!!\" } // third line\n" 17778 "};\n", 17779 Style); 17780 verifyFormat("struct test demo[4] = {\n" 17781 " {56, 23, 21, \"oh\" }, // first line\n" 17782 " {-1, 93463, 22, \"my\" }, // second line\n" 17783 " {7, 5, 1, \"goodness\"} // third line\n" 17784 " {234, 5, 1, \"gracious\"} // fourth line\n" 17785 "};\n", 17786 Style); 17787 verifyFormat("struct test demo[3] = {\n" 17788 " {56, 23, \"hello\"},\n" 17789 " {-1, 93463, \"world\"},\n" 17790 " {7, 5, \"!!\" }\n" 17791 "};\n", 17792 Style); 17793 17794 verifyFormat("struct test demo[3] = {\n" 17795 " {int{56}, 23, \"hello\"},\n" 17796 " {int{-1}, 93463, \"world\"},\n" 17797 " {int{7}, 5, \"!!\" }\n" 17798 "};\n", 17799 Style); 17800 verifyFormat("struct test demo[] = {\n" 17801 " {56, 23, \"hello\"},\n" 17802 " {-1, 93463, \"world\"},\n" 17803 " {7, 5, \"!!\" },\n" 17804 "};\n", 17805 Style); 17806 verifyFormat("test demo[] = {\n" 17807 " {56, 23, \"hello\"},\n" 17808 " {-1, 93463, \"world\"},\n" 17809 " {7, 5, \"!!\" },\n" 17810 "};\n", 17811 Style); 17812 verifyFormat("demo = std::array<struct test, 3>{\n" 17813 " test{56, 23, \"hello\"},\n" 17814 " test{-1, 93463, \"world\"},\n" 17815 " test{7, 5, \"!!\" },\n" 17816 "};\n", 17817 Style); 17818 verifyFormat("test demo[] = {\n" 17819 " {56, 23, \"hello\"},\n" 17820 "#if X\n" 17821 " {-1, 93463, \"world\"},\n" 17822 "#endif\n" 17823 " {7, 5, \"!!\" }\n" 17824 "};\n", 17825 Style); 17826 verifyFormat( 17827 "test demo[] = {\n" 17828 " {7, 23,\n" 17829 " \"hello world i am a very long line that really, in any\"\n" 17830 " \"just world, ought to be split over multiple lines\"},\n" 17831 " {-1, 93463, \"world\" },\n" 17832 " {56, 5, \"!!\" }\n" 17833 "};\n", 17834 Style); 17835 17836 verifyFormat("return GradForUnaryCwise(g, {\n" 17837 " {{\"sign\"}, \"Sign\", {\"x\", " 17838 "\"dy\"} },\n" 17839 " {{\"dx\"}, \"Mul\", " 17840 "{\"dy\", \"sign\"}},\n" 17841 "});\n", 17842 Style); 17843 17844 Style.ColumnLimit = 0; 17845 EXPECT_EQ( 17846 "test demo[] = {\n" 17847 " {56, 23, \"hello world i am a very long line that really, in any " 17848 "just world, ought to be split over multiple lines\"},\n" 17849 " {-1, 93463, \"world\" " 17850 " },\n" 17851 " {7, 5, \"!!\" " 17852 " },\n" 17853 "};", 17854 format("test demo[] = {{56, 23, \"hello world i am a very long line " 17855 "that really, in any just world, ought to be split over multiple " 17856 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 17857 Style)); 17858 17859 Style.ColumnLimit = 80; 17860 verifyFormat("test demo[] = {\n" 17861 " {56, 23, /* a comment */ \"hello\"},\n" 17862 " {-1, 93463, \"world\" },\n" 17863 " {7, 5, \"!!\" }\n" 17864 "};\n", 17865 Style); 17866 17867 verifyFormat("test demo[] = {\n" 17868 " {56, 23, \"hello\" },\n" 17869 " {-1, 93463, \"world\" /* comment here */},\n" 17870 " {7, 5, \"!!\" }\n" 17871 "};\n", 17872 Style); 17873 17874 verifyFormat("test demo[] = {\n" 17875 " {56, /* a comment */ 23, \"hello\"},\n" 17876 " {-1, 93463, \"world\"},\n" 17877 " {7, 5, \"!!\" }\n" 17878 "};\n", 17879 Style); 17880 17881 Style.ColumnLimit = 20; 17882 EXPECT_EQ( 17883 "demo = std::array<\n" 17884 " struct test, 3>{\n" 17885 " test{\n" 17886 " 56, 23,\n" 17887 " \"hello \"\n" 17888 " \"world i \"\n" 17889 " \"am a very \"\n" 17890 " \"long line \"\n" 17891 " \"that \"\n" 17892 " \"really, \"\n" 17893 " \"in any \"\n" 17894 " \"just \"\n" 17895 " \"world, \"\n" 17896 " \"ought to \"\n" 17897 " \"be split \"\n" 17898 " \"over \"\n" 17899 " \"multiple \"\n" 17900 " \"lines\"},\n" 17901 " test{-1, 93463,\n" 17902 " \"world\"},\n" 17903 " test{7, 5,\n" 17904 " \"!!\" },\n" 17905 "};", 17906 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world " 17907 "i am a very long line that really, in any just world, ought " 17908 "to be split over multiple lines\"},test{-1, 93463, \"world\"}," 17909 "test{7, 5, \"!!\"},};", 17910 Style)); 17911 17912 // This caused a core dump by enabling Alignment in the LLVMStyle globally 17913 Style = getLLVMStyleWithColumns(50); 17914 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; 17915 verifyFormat("static A x = {\n" 17916 " {{init1, init2, init3, init4},\n" 17917 " {init1, init2, init3, init4}}\n" 17918 "};", 17919 Style); 17920 Style.ColumnLimit = 100; 17921 EXPECT_EQ( 17922 "test demo[] = {\n" 17923 " {56, 23,\n" 17924 " \"hello world i am a very long line that really, in any just world" 17925 ", ought to be split over \"\n" 17926 " \"multiple lines\" },\n" 17927 " {-1, 93463, \"world\"},\n" 17928 " {7, 5, \"!!\" },\n" 17929 "};", 17930 format("test demo[] = {{56, 23, \"hello world i am a very long line " 17931 "that really, in any just world, ought to be split over multiple " 17932 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 17933 Style)); 17934 } 17935 17936 TEST_F(FormatTest, UnderstandsPragmas) { 17937 verifyFormat("#pragma omp reduction(| : var)"); 17938 verifyFormat("#pragma omp reduction(+ : var)"); 17939 17940 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 17941 "(including parentheses).", 17942 format("#pragma mark Any non-hyphenated or hyphenated string " 17943 "(including parentheses).")); 17944 } 17945 17946 TEST_F(FormatTest, UnderstandPragmaOption) { 17947 verifyFormat("#pragma option -C -A"); 17948 17949 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 17950 } 17951 17952 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) { 17953 FormatStyle Style = getLLVMStyle(); 17954 Style.ColumnLimit = 20; 17955 17956 // See PR41213 17957 EXPECT_EQ("/*\n" 17958 " *\t9012345\n" 17959 " * /8901\n" 17960 " */", 17961 format("/*\n" 17962 " *\t9012345 /8901\n" 17963 " */", 17964 Style)); 17965 EXPECT_EQ("/*\n" 17966 " *345678\n" 17967 " *\t/8901\n" 17968 " */", 17969 format("/*\n" 17970 " *345678\t/8901\n" 17971 " */", 17972 Style)); 17973 17974 verifyFormat("int a; // the\n" 17975 " // comment", 17976 Style); 17977 EXPECT_EQ("int a; /* first line\n" 17978 " * second\n" 17979 " * line third\n" 17980 " * line\n" 17981 " */", 17982 format("int a; /* first line\n" 17983 " * second\n" 17984 " * line third\n" 17985 " * line\n" 17986 " */", 17987 Style)); 17988 EXPECT_EQ("int a; // first line\n" 17989 " // second\n" 17990 " // line third\n" 17991 " // line", 17992 format("int a; // first line\n" 17993 " // second line\n" 17994 " // third line", 17995 Style)); 17996 17997 Style.PenaltyExcessCharacter = 90; 17998 verifyFormat("int a; // the comment", Style); 17999 EXPECT_EQ("int a; // the comment\n" 18000 " // aaa", 18001 format("int a; // the comment aaa", Style)); 18002 EXPECT_EQ("int a; /* first line\n" 18003 " * second line\n" 18004 " * third line\n" 18005 " */", 18006 format("int a; /* first line\n" 18007 " * second line\n" 18008 " * third line\n" 18009 " */", 18010 Style)); 18011 EXPECT_EQ("int a; // first line\n" 18012 " // second line\n" 18013 " // third line", 18014 format("int a; // first line\n" 18015 " // second line\n" 18016 " // third line", 18017 Style)); 18018 // FIXME: Investigate why this is not getting the same layout as the test 18019 // above. 18020 EXPECT_EQ("int a; /* first line\n" 18021 " * second line\n" 18022 " * third line\n" 18023 " */", 18024 format("int a; /* first line second line third line" 18025 "\n*/", 18026 Style)); 18027 18028 EXPECT_EQ("// foo bar baz bazfoo\n" 18029 "// foo bar foo bar\n", 18030 format("// foo bar baz bazfoo\n" 18031 "// foo bar foo bar\n", 18032 Style)); 18033 EXPECT_EQ("// foo bar baz bazfoo\n" 18034 "// foo bar foo bar\n", 18035 format("// foo bar baz bazfoo\n" 18036 "// foo bar foo bar\n", 18037 Style)); 18038 18039 // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the 18040 // next one. 18041 EXPECT_EQ("// foo bar baz bazfoo\n" 18042 "// bar foo bar\n", 18043 format("// foo bar baz bazfoo bar\n" 18044 "// foo bar\n", 18045 Style)); 18046 18047 EXPECT_EQ("// foo bar baz bazfoo\n" 18048 "// foo bar baz bazfoo\n" 18049 "// bar foo bar\n", 18050 format("// foo bar baz bazfoo\n" 18051 "// foo bar baz bazfoo bar\n" 18052 "// foo bar\n", 18053 Style)); 18054 18055 EXPECT_EQ("// foo bar baz bazfoo\n" 18056 "// foo bar baz bazfoo\n" 18057 "// bar foo bar\n", 18058 format("// foo bar baz bazfoo\n" 18059 "// foo bar baz bazfoo bar\n" 18060 "// foo bar\n", 18061 Style)); 18062 18063 // Make sure we do not keep protruding characters if strict mode reflow is 18064 // cheaper than keeping protruding characters. 18065 Style.ColumnLimit = 21; 18066 EXPECT_EQ( 18067 "// foo foo foo foo\n" 18068 "// foo foo foo foo\n" 18069 "// foo foo foo foo\n", 18070 format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", Style)); 18071 18072 EXPECT_EQ("int a = /* long block\n" 18073 " comment */\n" 18074 " 42;", 18075 format("int a = /* long block comment */ 42;", Style)); 18076 } 18077 18078 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 18079 for (size_t i = 1; i < Styles.size(); ++i) \ 18080 EXPECT_EQ(Styles[0], Styles[i]) \ 18081 << "Style #" << i << " of " << Styles.size() << " differs from Style #0" 18082 18083 TEST_F(FormatTest, GetsPredefinedStyleByName) { 18084 SmallVector<FormatStyle, 3> Styles; 18085 Styles.resize(3); 18086 18087 Styles[0] = getLLVMStyle(); 18088 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 18089 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 18090 EXPECT_ALL_STYLES_EQUAL(Styles); 18091 18092 Styles[0] = getGoogleStyle(); 18093 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 18094 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 18095 EXPECT_ALL_STYLES_EQUAL(Styles); 18096 18097 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 18098 EXPECT_TRUE( 18099 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 18100 EXPECT_TRUE( 18101 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 18102 EXPECT_ALL_STYLES_EQUAL(Styles); 18103 18104 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 18105 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 18106 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 18107 EXPECT_ALL_STYLES_EQUAL(Styles); 18108 18109 Styles[0] = getMozillaStyle(); 18110 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 18111 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 18112 EXPECT_ALL_STYLES_EQUAL(Styles); 18113 18114 Styles[0] = getWebKitStyle(); 18115 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 18116 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 18117 EXPECT_ALL_STYLES_EQUAL(Styles); 18118 18119 Styles[0] = getGNUStyle(); 18120 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 18121 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 18122 EXPECT_ALL_STYLES_EQUAL(Styles); 18123 18124 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 18125 } 18126 18127 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 18128 SmallVector<FormatStyle, 8> Styles; 18129 Styles.resize(2); 18130 18131 Styles[0] = getGoogleStyle(); 18132 Styles[1] = getLLVMStyle(); 18133 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 18134 EXPECT_ALL_STYLES_EQUAL(Styles); 18135 18136 Styles.resize(5); 18137 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 18138 Styles[1] = getLLVMStyle(); 18139 Styles[1].Language = FormatStyle::LK_JavaScript; 18140 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 18141 18142 Styles[2] = getLLVMStyle(); 18143 Styles[2].Language = FormatStyle::LK_JavaScript; 18144 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 18145 "BasedOnStyle: Google", 18146 &Styles[2]) 18147 .value()); 18148 18149 Styles[3] = getLLVMStyle(); 18150 Styles[3].Language = FormatStyle::LK_JavaScript; 18151 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 18152 "Language: JavaScript", 18153 &Styles[3]) 18154 .value()); 18155 18156 Styles[4] = getLLVMStyle(); 18157 Styles[4].Language = FormatStyle::LK_JavaScript; 18158 EXPECT_EQ(0, parseConfiguration("---\n" 18159 "BasedOnStyle: LLVM\n" 18160 "IndentWidth: 123\n" 18161 "---\n" 18162 "BasedOnStyle: Google\n" 18163 "Language: JavaScript", 18164 &Styles[4]) 18165 .value()); 18166 EXPECT_ALL_STYLES_EQUAL(Styles); 18167 } 18168 18169 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 18170 Style.FIELD = false; \ 18171 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 18172 EXPECT_TRUE(Style.FIELD); \ 18173 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 18174 EXPECT_FALSE(Style.FIELD); 18175 18176 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 18177 18178 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 18179 Style.STRUCT.FIELD = false; \ 18180 EXPECT_EQ(0, \ 18181 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 18182 .value()); \ 18183 EXPECT_TRUE(Style.STRUCT.FIELD); \ 18184 EXPECT_EQ(0, \ 18185 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 18186 .value()); \ 18187 EXPECT_FALSE(Style.STRUCT.FIELD); 18188 18189 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 18190 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 18191 18192 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 18193 EXPECT_NE(VALUE, Style.FIELD) << "Initial value already the same!"; \ 18194 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 18195 EXPECT_EQ(VALUE, Style.FIELD) << "Unexpected value after parsing!" 18196 18197 TEST_F(FormatTest, ParsesConfigurationBools) { 18198 FormatStyle Style = {}; 18199 Style.Language = FormatStyle::LK_Cpp; 18200 CHECK_PARSE_BOOL(AlignTrailingComments); 18201 CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine); 18202 CHECK_PARSE_BOOL(AllowAllConstructorInitializersOnNextLine); 18203 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 18204 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 18205 CHECK_PARSE_BOOL(AllowShortEnumsOnASingleLine); 18206 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 18207 CHECK_PARSE_BOOL(BinPackArguments); 18208 CHECK_PARSE_BOOL(BinPackParameters); 18209 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 18210 CHECK_PARSE_BOOL(BreakBeforeConceptDeclarations); 18211 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 18212 CHECK_PARSE_BOOL(BreakStringLiterals); 18213 CHECK_PARSE_BOOL(CompactNamespaces); 18214 CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); 18215 CHECK_PARSE_BOOL(DeriveLineEnding); 18216 CHECK_PARSE_BOOL(DerivePointerAlignment); 18217 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 18218 CHECK_PARSE_BOOL(DisableFormat); 18219 CHECK_PARSE_BOOL(IndentAccessModifiers); 18220 CHECK_PARSE_BOOL(IndentCaseLabels); 18221 CHECK_PARSE_BOOL(IndentCaseBlocks); 18222 CHECK_PARSE_BOOL(IndentGotoLabels); 18223 CHECK_PARSE_BOOL(IndentRequires); 18224 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 18225 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 18226 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 18227 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 18228 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 18229 CHECK_PARSE_BOOL(ReflowComments); 18230 CHECK_PARSE_BOOL(SortUsingDeclarations); 18231 CHECK_PARSE_BOOL(SpacesInParentheses); 18232 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 18233 CHECK_PARSE_BOOL(SpacesInConditionalStatement); 18234 CHECK_PARSE_BOOL(SpaceInEmptyBlock); 18235 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 18236 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 18237 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 18238 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 18239 CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); 18240 CHECK_PARSE_BOOL(SpaceAfterLogicalNot); 18241 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 18242 CHECK_PARSE_BOOL(SpaceBeforeCaseColon); 18243 CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList); 18244 CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon); 18245 CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon); 18246 CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon); 18247 CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets); 18248 CHECK_PARSE_BOOL(UseCRLF); 18249 18250 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel); 18251 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 18252 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 18253 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 18254 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 18255 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 18256 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 18257 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 18258 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock); 18259 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 18260 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 18261 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeLambdaBody); 18262 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeWhile); 18263 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 18264 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction); 18265 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord); 18266 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace); 18267 } 18268 18269 #undef CHECK_PARSE_BOOL 18270 18271 TEST_F(FormatTest, ParsesConfiguration) { 18272 FormatStyle Style = {}; 18273 Style.Language = FormatStyle::LK_Cpp; 18274 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 18275 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 18276 ConstructorInitializerIndentWidth, 1234u); 18277 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 18278 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 18279 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 18280 CHECK_PARSE("PenaltyBreakAssignment: 1234", PenaltyBreakAssignment, 1234u); 18281 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 18282 PenaltyBreakBeforeFirstCallParameter, 1234u); 18283 CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234", 18284 PenaltyBreakTemplateDeclaration, 1234u); 18285 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 18286 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 18287 PenaltyReturnTypeOnItsOwnLine, 1234u); 18288 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 18289 SpacesBeforeTrailingComments, 1234u); 18290 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 18291 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 18292 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$"); 18293 18294 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 18295 CHECK_PARSE("AlignConsecutiveAssignments: None", AlignConsecutiveAssignments, 18296 FormatStyle::ACS_None); 18297 CHECK_PARSE("AlignConsecutiveAssignments: Consecutive", 18298 AlignConsecutiveAssignments, FormatStyle::ACS_Consecutive); 18299 CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLines", 18300 AlignConsecutiveAssignments, FormatStyle::ACS_AcrossEmptyLines); 18301 CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLinesAndComments", 18302 AlignConsecutiveAssignments, 18303 FormatStyle::ACS_AcrossEmptyLinesAndComments); 18304 // For backwards compability, false / true should still parse 18305 CHECK_PARSE("AlignConsecutiveAssignments: false", AlignConsecutiveAssignments, 18306 FormatStyle::ACS_None); 18307 CHECK_PARSE("AlignConsecutiveAssignments: true", AlignConsecutiveAssignments, 18308 FormatStyle::ACS_Consecutive); 18309 18310 Style.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive; 18311 CHECK_PARSE("AlignConsecutiveBitFields: None", AlignConsecutiveBitFields, 18312 FormatStyle::ACS_None); 18313 CHECK_PARSE("AlignConsecutiveBitFields: Consecutive", 18314 AlignConsecutiveBitFields, FormatStyle::ACS_Consecutive); 18315 CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLines", 18316 AlignConsecutiveBitFields, FormatStyle::ACS_AcrossEmptyLines); 18317 CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLinesAndComments", 18318 AlignConsecutiveBitFields, 18319 FormatStyle::ACS_AcrossEmptyLinesAndComments); 18320 // For backwards compability, false / true should still parse 18321 CHECK_PARSE("AlignConsecutiveBitFields: false", AlignConsecutiveBitFields, 18322 FormatStyle::ACS_None); 18323 CHECK_PARSE("AlignConsecutiveBitFields: true", AlignConsecutiveBitFields, 18324 FormatStyle::ACS_Consecutive); 18325 18326 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 18327 CHECK_PARSE("AlignConsecutiveMacros: None", AlignConsecutiveMacros, 18328 FormatStyle::ACS_None); 18329 CHECK_PARSE("AlignConsecutiveMacros: Consecutive", AlignConsecutiveMacros, 18330 FormatStyle::ACS_Consecutive); 18331 CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLines", 18332 AlignConsecutiveMacros, FormatStyle::ACS_AcrossEmptyLines); 18333 CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLinesAndComments", 18334 AlignConsecutiveMacros, 18335 FormatStyle::ACS_AcrossEmptyLinesAndComments); 18336 // For backwards compability, false / true should still parse 18337 CHECK_PARSE("AlignConsecutiveMacros: false", AlignConsecutiveMacros, 18338 FormatStyle::ACS_None); 18339 CHECK_PARSE("AlignConsecutiveMacros: true", AlignConsecutiveMacros, 18340 FormatStyle::ACS_Consecutive); 18341 18342 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 18343 CHECK_PARSE("AlignConsecutiveDeclarations: None", 18344 AlignConsecutiveDeclarations, FormatStyle::ACS_None); 18345 CHECK_PARSE("AlignConsecutiveDeclarations: Consecutive", 18346 AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive); 18347 CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLines", 18348 AlignConsecutiveDeclarations, FormatStyle::ACS_AcrossEmptyLines); 18349 CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLinesAndComments", 18350 AlignConsecutiveDeclarations, 18351 FormatStyle::ACS_AcrossEmptyLinesAndComments); 18352 // For backwards compability, false / true should still parse 18353 CHECK_PARSE("AlignConsecutiveDeclarations: false", 18354 AlignConsecutiveDeclarations, FormatStyle::ACS_None); 18355 CHECK_PARSE("AlignConsecutiveDeclarations: true", 18356 AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive); 18357 18358 Style.PointerAlignment = FormatStyle::PAS_Middle; 18359 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 18360 FormatStyle::PAS_Left); 18361 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 18362 FormatStyle::PAS_Right); 18363 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 18364 FormatStyle::PAS_Middle); 18365 Style.ReferenceAlignment = FormatStyle::RAS_Middle; 18366 CHECK_PARSE("ReferenceAlignment: Pointer", ReferenceAlignment, 18367 FormatStyle::RAS_Pointer); 18368 CHECK_PARSE("ReferenceAlignment: Left", ReferenceAlignment, 18369 FormatStyle::RAS_Left); 18370 CHECK_PARSE("ReferenceAlignment: Right", ReferenceAlignment, 18371 FormatStyle::RAS_Right); 18372 CHECK_PARSE("ReferenceAlignment: Middle", ReferenceAlignment, 18373 FormatStyle::RAS_Middle); 18374 // For backward compatibility: 18375 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 18376 FormatStyle::PAS_Left); 18377 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 18378 FormatStyle::PAS_Right); 18379 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 18380 FormatStyle::PAS_Middle); 18381 18382 Style.Standard = FormatStyle::LS_Auto; 18383 CHECK_PARSE("Standard: c++03", Standard, FormatStyle::LS_Cpp03); 18384 CHECK_PARSE("Standard: c++11", Standard, FormatStyle::LS_Cpp11); 18385 CHECK_PARSE("Standard: c++14", Standard, FormatStyle::LS_Cpp14); 18386 CHECK_PARSE("Standard: c++17", Standard, FormatStyle::LS_Cpp17); 18387 CHECK_PARSE("Standard: c++20", Standard, FormatStyle::LS_Cpp20); 18388 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 18389 CHECK_PARSE("Standard: Latest", Standard, FormatStyle::LS_Latest); 18390 // Legacy aliases: 18391 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 18392 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Latest); 18393 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 18394 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 18395 18396 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 18397 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 18398 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 18399 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 18400 FormatStyle::BOS_None); 18401 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 18402 FormatStyle::BOS_All); 18403 // For backward compatibility: 18404 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 18405 FormatStyle::BOS_None); 18406 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 18407 FormatStyle::BOS_All); 18408 18409 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 18410 CHECK_PARSE("BreakConstructorInitializers: BeforeComma", 18411 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 18412 CHECK_PARSE("BreakConstructorInitializers: AfterColon", 18413 BreakConstructorInitializers, FormatStyle::BCIS_AfterColon); 18414 CHECK_PARSE("BreakConstructorInitializers: BeforeColon", 18415 BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon); 18416 // For backward compatibility: 18417 CHECK_PARSE("BreakConstructorInitializersBeforeComma: true", 18418 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 18419 18420 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 18421 CHECK_PARSE("BreakInheritanceList: AfterComma", BreakInheritanceList, 18422 FormatStyle::BILS_AfterComma); 18423 CHECK_PARSE("BreakInheritanceList: BeforeComma", BreakInheritanceList, 18424 FormatStyle::BILS_BeforeComma); 18425 CHECK_PARSE("BreakInheritanceList: AfterColon", BreakInheritanceList, 18426 FormatStyle::BILS_AfterColon); 18427 CHECK_PARSE("BreakInheritanceList: BeforeColon", BreakInheritanceList, 18428 FormatStyle::BILS_BeforeColon); 18429 // For backward compatibility: 18430 CHECK_PARSE("BreakBeforeInheritanceComma: true", BreakInheritanceList, 18431 FormatStyle::BILS_BeforeComma); 18432 18433 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 18434 CHECK_PARSE("EmptyLineBeforeAccessModifier: Never", 18435 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Never); 18436 CHECK_PARSE("EmptyLineBeforeAccessModifier: Leave", 18437 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Leave); 18438 CHECK_PARSE("EmptyLineBeforeAccessModifier: LogicalBlock", 18439 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_LogicalBlock); 18440 CHECK_PARSE("EmptyLineBeforeAccessModifier: Always", 18441 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Always); 18442 18443 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 18444 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 18445 FormatStyle::BAS_Align); 18446 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 18447 FormatStyle::BAS_DontAlign); 18448 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 18449 FormatStyle::BAS_AlwaysBreak); 18450 // For backward compatibility: 18451 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 18452 FormatStyle::BAS_DontAlign); 18453 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 18454 FormatStyle::BAS_Align); 18455 18456 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left; 18457 CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines, 18458 FormatStyle::ENAS_DontAlign); 18459 CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines, 18460 FormatStyle::ENAS_Left); 18461 CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines, 18462 FormatStyle::ENAS_Right); 18463 // For backward compatibility: 18464 CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines, 18465 FormatStyle::ENAS_Left); 18466 CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines, 18467 FormatStyle::ENAS_Right); 18468 18469 Style.AlignOperands = FormatStyle::OAS_Align; 18470 CHECK_PARSE("AlignOperands: DontAlign", AlignOperands, 18471 FormatStyle::OAS_DontAlign); 18472 CHECK_PARSE("AlignOperands: Align", AlignOperands, FormatStyle::OAS_Align); 18473 CHECK_PARSE("AlignOperands: AlignAfterOperator", AlignOperands, 18474 FormatStyle::OAS_AlignAfterOperator); 18475 // For backward compatibility: 18476 CHECK_PARSE("AlignOperands: false", AlignOperands, 18477 FormatStyle::OAS_DontAlign); 18478 CHECK_PARSE("AlignOperands: true", AlignOperands, FormatStyle::OAS_Align); 18479 18480 Style.UseTab = FormatStyle::UT_ForIndentation; 18481 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 18482 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 18483 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 18484 CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab, 18485 FormatStyle::UT_ForContinuationAndIndentation); 18486 CHECK_PARSE("UseTab: AlignWithSpaces", UseTab, 18487 FormatStyle::UT_AlignWithSpaces); 18488 // For backward compatibility: 18489 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 18490 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 18491 18492 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 18493 CHECK_PARSE("AllowShortBlocksOnASingleLine: Never", 18494 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 18495 CHECK_PARSE("AllowShortBlocksOnASingleLine: Empty", 18496 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Empty); 18497 CHECK_PARSE("AllowShortBlocksOnASingleLine: Always", 18498 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always); 18499 // For backward compatibility: 18500 CHECK_PARSE("AllowShortBlocksOnASingleLine: false", 18501 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 18502 CHECK_PARSE("AllowShortBlocksOnASingleLine: true", 18503 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always); 18504 18505 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 18506 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 18507 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 18508 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 18509 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 18510 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 18511 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 18512 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 18513 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 18514 // For backward compatibility: 18515 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 18516 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 18517 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 18518 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 18519 18520 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Both; 18521 CHECK_PARSE("SpaceAroundPointerQualifiers: Default", 18522 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Default); 18523 CHECK_PARSE("SpaceAroundPointerQualifiers: Before", 18524 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Before); 18525 CHECK_PARSE("SpaceAroundPointerQualifiers: After", 18526 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_After); 18527 CHECK_PARSE("SpaceAroundPointerQualifiers: Both", 18528 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Both); 18529 18530 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 18531 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 18532 FormatStyle::SBPO_Never); 18533 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 18534 FormatStyle::SBPO_Always); 18535 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 18536 FormatStyle::SBPO_ControlStatements); 18537 CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptControlMacros", 18538 SpaceBeforeParens, 18539 FormatStyle::SBPO_ControlStatementsExceptControlMacros); 18540 CHECK_PARSE("SpaceBeforeParens: NonEmptyParentheses", SpaceBeforeParens, 18541 FormatStyle::SBPO_NonEmptyParentheses); 18542 // For backward compatibility: 18543 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 18544 FormatStyle::SBPO_Never); 18545 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 18546 FormatStyle::SBPO_ControlStatements); 18547 CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptForEachMacros", 18548 SpaceBeforeParens, 18549 FormatStyle::SBPO_ControlStatementsExceptControlMacros); 18550 18551 Style.ColumnLimit = 123; 18552 FormatStyle BaseStyle = getLLVMStyle(); 18553 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 18554 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 18555 18556 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 18557 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 18558 FormatStyle::BS_Attach); 18559 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 18560 FormatStyle::BS_Linux); 18561 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 18562 FormatStyle::BS_Mozilla); 18563 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 18564 FormatStyle::BS_Stroustrup); 18565 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 18566 FormatStyle::BS_Allman); 18567 CHECK_PARSE("BreakBeforeBraces: Whitesmiths", BreakBeforeBraces, 18568 FormatStyle::BS_Whitesmiths); 18569 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 18570 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 18571 FormatStyle::BS_WebKit); 18572 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 18573 FormatStyle::BS_Custom); 18574 18575 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never; 18576 CHECK_PARSE("BraceWrapping:\n" 18577 " AfterControlStatement: MultiLine", 18578 BraceWrapping.AfterControlStatement, 18579 FormatStyle::BWACS_MultiLine); 18580 CHECK_PARSE("BraceWrapping:\n" 18581 " AfterControlStatement: Always", 18582 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always); 18583 CHECK_PARSE("BraceWrapping:\n" 18584 " AfterControlStatement: Never", 18585 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never); 18586 // For backward compatibility: 18587 CHECK_PARSE("BraceWrapping:\n" 18588 " AfterControlStatement: true", 18589 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always); 18590 CHECK_PARSE("BraceWrapping:\n" 18591 " AfterControlStatement: false", 18592 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never); 18593 18594 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 18595 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 18596 FormatStyle::RTBS_None); 18597 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 18598 FormatStyle::RTBS_All); 18599 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 18600 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 18601 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 18602 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 18603 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 18604 AlwaysBreakAfterReturnType, 18605 FormatStyle::RTBS_TopLevelDefinitions); 18606 18607 Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 18608 CHECK_PARSE("AlwaysBreakTemplateDeclarations: No", 18609 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_No); 18610 CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine", 18611 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine); 18612 CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes", 18613 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes); 18614 CHECK_PARSE("AlwaysBreakTemplateDeclarations: false", 18615 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine); 18616 CHECK_PARSE("AlwaysBreakTemplateDeclarations: true", 18617 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes); 18618 18619 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 18620 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 18621 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 18622 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 18623 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 18624 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 18625 AlwaysBreakAfterDefinitionReturnType, 18626 FormatStyle::DRTBS_TopLevel); 18627 18628 Style.NamespaceIndentation = FormatStyle::NI_All; 18629 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 18630 FormatStyle::NI_None); 18631 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 18632 FormatStyle::NI_Inner); 18633 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 18634 FormatStyle::NI_All); 18635 18636 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_OnlyFirstIf; 18637 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Never", 18638 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never); 18639 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: WithoutElse", 18640 AllowShortIfStatementsOnASingleLine, 18641 FormatStyle::SIS_WithoutElse); 18642 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: OnlyFirstIf", 18643 AllowShortIfStatementsOnASingleLine, 18644 FormatStyle::SIS_OnlyFirstIf); 18645 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: AllIfsAndElse", 18646 AllowShortIfStatementsOnASingleLine, 18647 FormatStyle::SIS_AllIfsAndElse); 18648 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Always", 18649 AllowShortIfStatementsOnASingleLine, 18650 FormatStyle::SIS_OnlyFirstIf); 18651 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: false", 18652 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never); 18653 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: true", 18654 AllowShortIfStatementsOnASingleLine, 18655 FormatStyle::SIS_WithoutElse); 18656 18657 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; 18658 CHECK_PARSE("IndentExternBlock: AfterExternBlock", IndentExternBlock, 18659 FormatStyle::IEBS_AfterExternBlock); 18660 CHECK_PARSE("IndentExternBlock: Indent", IndentExternBlock, 18661 FormatStyle::IEBS_Indent); 18662 CHECK_PARSE("IndentExternBlock: NoIndent", IndentExternBlock, 18663 FormatStyle::IEBS_NoIndent); 18664 CHECK_PARSE("IndentExternBlock: true", IndentExternBlock, 18665 FormatStyle::IEBS_Indent); 18666 CHECK_PARSE("IndentExternBlock: false", IndentExternBlock, 18667 FormatStyle::IEBS_NoIndent); 18668 18669 Style.BitFieldColonSpacing = FormatStyle::BFCS_None; 18670 CHECK_PARSE("BitFieldColonSpacing: Both", BitFieldColonSpacing, 18671 FormatStyle::BFCS_Both); 18672 CHECK_PARSE("BitFieldColonSpacing: None", BitFieldColonSpacing, 18673 FormatStyle::BFCS_None); 18674 CHECK_PARSE("BitFieldColonSpacing: Before", BitFieldColonSpacing, 18675 FormatStyle::BFCS_Before); 18676 CHECK_PARSE("BitFieldColonSpacing: After", BitFieldColonSpacing, 18677 FormatStyle::BFCS_After); 18678 18679 Style.SortJavaStaticImport = FormatStyle::SJSIO_Before; 18680 CHECK_PARSE("SortJavaStaticImport: After", SortJavaStaticImport, 18681 FormatStyle::SJSIO_After); 18682 CHECK_PARSE("SortJavaStaticImport: Before", SortJavaStaticImport, 18683 FormatStyle::SJSIO_Before); 18684 18685 // FIXME: This is required because parsing a configuration simply overwrites 18686 // the first N elements of the list instead of resetting it. 18687 Style.ForEachMacros.clear(); 18688 std::vector<std::string> BoostForeach; 18689 BoostForeach.push_back("BOOST_FOREACH"); 18690 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 18691 std::vector<std::string> BoostAndQForeach; 18692 BoostAndQForeach.push_back("BOOST_FOREACH"); 18693 BoostAndQForeach.push_back("Q_FOREACH"); 18694 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 18695 BoostAndQForeach); 18696 18697 Style.IfMacros.clear(); 18698 std::vector<std::string> CustomIfs; 18699 CustomIfs.push_back("MYIF"); 18700 CHECK_PARSE("IfMacros: [MYIF]", IfMacros, CustomIfs); 18701 18702 Style.AttributeMacros.clear(); 18703 CHECK_PARSE("BasedOnStyle: LLVM", AttributeMacros, 18704 std::vector<std::string>{"__capability"}); 18705 CHECK_PARSE("AttributeMacros: [attr1, attr2]", AttributeMacros, 18706 std::vector<std::string>({"attr1", "attr2"})); 18707 18708 Style.StatementAttributeLikeMacros.clear(); 18709 CHECK_PARSE("StatementAttributeLikeMacros: [emit,Q_EMIT]", 18710 StatementAttributeLikeMacros, 18711 std::vector<std::string>({"emit", "Q_EMIT"})); 18712 18713 Style.StatementMacros.clear(); 18714 CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros, 18715 std::vector<std::string>{"QUNUSED"}); 18716 CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros, 18717 std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"})); 18718 18719 Style.NamespaceMacros.clear(); 18720 CHECK_PARSE("NamespaceMacros: [TESTSUITE]", NamespaceMacros, 18721 std::vector<std::string>{"TESTSUITE"}); 18722 CHECK_PARSE("NamespaceMacros: [TESTSUITE, SUITE]", NamespaceMacros, 18723 std::vector<std::string>({"TESTSUITE", "SUITE"})); 18724 18725 Style.WhitespaceSensitiveMacros.clear(); 18726 CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE]", 18727 WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"}); 18728 CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE, ASSERT]", 18729 WhitespaceSensitiveMacros, 18730 std::vector<std::string>({"STRINGIZE", "ASSERT"})); 18731 Style.WhitespaceSensitiveMacros.clear(); 18732 CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE']", 18733 WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"}); 18734 CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE', 'ASSERT']", 18735 WhitespaceSensitiveMacros, 18736 std::vector<std::string>({"STRINGIZE", "ASSERT"})); 18737 18738 Style.IncludeStyle.IncludeCategories.clear(); 18739 std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = { 18740 {"abc/.*", 2, 0, false}, {".*", 1, 0, true}}; 18741 CHECK_PARSE("IncludeCategories:\n" 18742 " - Regex: abc/.*\n" 18743 " Priority: 2\n" 18744 " - Regex: .*\n" 18745 " Priority: 1\n" 18746 " CaseSensitive: true\n", 18747 IncludeStyle.IncludeCategories, ExpectedCategories); 18748 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex, 18749 "abc$"); 18750 CHECK_PARSE("IncludeIsMainSourceRegex: 'abc$'", 18751 IncludeStyle.IncludeIsMainSourceRegex, "abc$"); 18752 18753 Style.SortIncludes = FormatStyle::SI_Never; 18754 CHECK_PARSE("SortIncludes: true", SortIncludes, 18755 FormatStyle::SI_CaseSensitive); 18756 CHECK_PARSE("SortIncludes: false", SortIncludes, FormatStyle::SI_Never); 18757 CHECK_PARSE("SortIncludes: CaseInsensitive", SortIncludes, 18758 FormatStyle::SI_CaseInsensitive); 18759 CHECK_PARSE("SortIncludes: CaseSensitive", SortIncludes, 18760 FormatStyle::SI_CaseSensitive); 18761 CHECK_PARSE("SortIncludes: Never", SortIncludes, FormatStyle::SI_Never); 18762 18763 Style.RawStringFormats.clear(); 18764 std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = { 18765 { 18766 FormatStyle::LK_TextProto, 18767 {"pb", "proto"}, 18768 {"PARSE_TEXT_PROTO"}, 18769 /*CanonicalDelimiter=*/"", 18770 "llvm", 18771 }, 18772 { 18773 FormatStyle::LK_Cpp, 18774 {"cc", "cpp"}, 18775 {"C_CODEBLOCK", "CPPEVAL"}, 18776 /*CanonicalDelimiter=*/"cc", 18777 /*BasedOnStyle=*/"", 18778 }, 18779 }; 18780 18781 CHECK_PARSE("RawStringFormats:\n" 18782 " - Language: TextProto\n" 18783 " Delimiters:\n" 18784 " - 'pb'\n" 18785 " - 'proto'\n" 18786 " EnclosingFunctions:\n" 18787 " - 'PARSE_TEXT_PROTO'\n" 18788 " BasedOnStyle: llvm\n" 18789 " - Language: Cpp\n" 18790 " Delimiters:\n" 18791 " - 'cc'\n" 18792 " - 'cpp'\n" 18793 " EnclosingFunctions:\n" 18794 " - 'C_CODEBLOCK'\n" 18795 " - 'CPPEVAL'\n" 18796 " CanonicalDelimiter: 'cc'", 18797 RawStringFormats, ExpectedRawStringFormats); 18798 18799 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 18800 " Minimum: 0\n" 18801 " Maximum: 0", 18802 SpacesInLineCommentPrefix.Minimum, 0u); 18803 EXPECT_EQ(Style.SpacesInLineCommentPrefix.Maximum, 0u); 18804 Style.SpacesInLineCommentPrefix.Minimum = 1; 18805 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 18806 " Minimum: 2", 18807 SpacesInLineCommentPrefix.Minimum, 0u); 18808 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 18809 " Maximum: -1", 18810 SpacesInLineCommentPrefix.Maximum, -1u); 18811 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 18812 " Minimum: 2", 18813 SpacesInLineCommentPrefix.Minimum, 2u); 18814 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 18815 " Maximum: 1", 18816 SpacesInLineCommentPrefix.Maximum, 1u); 18817 EXPECT_EQ(Style.SpacesInLineCommentPrefix.Minimum, 1u); 18818 18819 Style.SpacesInAngles = FormatStyle::SIAS_Always; 18820 CHECK_PARSE("SpacesInAngles: Never", SpacesInAngles, FormatStyle::SIAS_Never); 18821 CHECK_PARSE("SpacesInAngles: Always", SpacesInAngles, 18822 FormatStyle::SIAS_Always); 18823 CHECK_PARSE("SpacesInAngles: Leave", SpacesInAngles, FormatStyle::SIAS_Leave); 18824 // For backward compatibility: 18825 CHECK_PARSE("SpacesInAngles: false", SpacesInAngles, FormatStyle::SIAS_Never); 18826 CHECK_PARSE("SpacesInAngles: true", SpacesInAngles, FormatStyle::SIAS_Always); 18827 } 18828 18829 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 18830 FormatStyle Style = {}; 18831 Style.Language = FormatStyle::LK_Cpp; 18832 CHECK_PARSE("Language: Cpp\n" 18833 "IndentWidth: 12", 18834 IndentWidth, 12u); 18835 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 18836 "IndentWidth: 34", 18837 &Style), 18838 ParseError::Unsuitable); 18839 FormatStyle BinPackedTCS = {}; 18840 BinPackedTCS.Language = FormatStyle::LK_JavaScript; 18841 EXPECT_EQ(parseConfiguration("BinPackArguments: true\n" 18842 "InsertTrailingCommas: Wrapped", 18843 &BinPackedTCS), 18844 ParseError::BinPackTrailingCommaConflict); 18845 EXPECT_EQ(12u, Style.IndentWidth); 18846 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 18847 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 18848 18849 Style.Language = FormatStyle::LK_JavaScript; 18850 CHECK_PARSE("Language: JavaScript\n" 18851 "IndentWidth: 12", 18852 IndentWidth, 12u); 18853 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 18854 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 18855 "IndentWidth: 34", 18856 &Style), 18857 ParseError::Unsuitable); 18858 EXPECT_EQ(23u, Style.IndentWidth); 18859 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 18860 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 18861 18862 CHECK_PARSE("BasedOnStyle: LLVM\n" 18863 "IndentWidth: 67", 18864 IndentWidth, 67u); 18865 18866 CHECK_PARSE("---\n" 18867 "Language: JavaScript\n" 18868 "IndentWidth: 12\n" 18869 "---\n" 18870 "Language: Cpp\n" 18871 "IndentWidth: 34\n" 18872 "...\n", 18873 IndentWidth, 12u); 18874 18875 Style.Language = FormatStyle::LK_Cpp; 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, 34u); 18884 CHECK_PARSE("---\n" 18885 "IndentWidth: 78\n" 18886 "---\n" 18887 "Language: JavaScript\n" 18888 "IndentWidth: 56\n" 18889 "...\n", 18890 IndentWidth, 78u); 18891 18892 Style.ColumnLimit = 123; 18893 Style.IndentWidth = 234; 18894 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 18895 Style.TabWidth = 345; 18896 EXPECT_FALSE(parseConfiguration("---\n" 18897 "IndentWidth: 456\n" 18898 "BreakBeforeBraces: Allman\n" 18899 "---\n" 18900 "Language: JavaScript\n" 18901 "IndentWidth: 111\n" 18902 "TabWidth: 111\n" 18903 "---\n" 18904 "Language: Cpp\n" 18905 "BreakBeforeBraces: Stroustrup\n" 18906 "TabWidth: 789\n" 18907 "...\n", 18908 &Style)); 18909 EXPECT_EQ(123u, Style.ColumnLimit); 18910 EXPECT_EQ(456u, Style.IndentWidth); 18911 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 18912 EXPECT_EQ(789u, Style.TabWidth); 18913 18914 EXPECT_EQ(parseConfiguration("---\n" 18915 "Language: JavaScript\n" 18916 "IndentWidth: 56\n" 18917 "---\n" 18918 "IndentWidth: 78\n" 18919 "...\n", 18920 &Style), 18921 ParseError::Error); 18922 EXPECT_EQ(parseConfiguration("---\n" 18923 "Language: JavaScript\n" 18924 "IndentWidth: 56\n" 18925 "---\n" 18926 "Language: JavaScript\n" 18927 "IndentWidth: 78\n" 18928 "...\n", 18929 &Style), 18930 ParseError::Error); 18931 18932 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 18933 } 18934 18935 #undef CHECK_PARSE 18936 18937 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 18938 FormatStyle Style = {}; 18939 Style.Language = FormatStyle::LK_JavaScript; 18940 Style.BreakBeforeTernaryOperators = true; 18941 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 18942 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 18943 18944 Style.BreakBeforeTernaryOperators = true; 18945 EXPECT_EQ(0, parseConfiguration("---\n" 18946 "BasedOnStyle: Google\n" 18947 "---\n" 18948 "Language: JavaScript\n" 18949 "IndentWidth: 76\n" 18950 "...\n", 18951 &Style) 18952 .value()); 18953 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 18954 EXPECT_EQ(76u, Style.IndentWidth); 18955 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 18956 } 18957 18958 TEST_F(FormatTest, ConfigurationRoundTripTest) { 18959 FormatStyle Style = getLLVMStyle(); 18960 std::string YAML = configurationAsText(Style); 18961 FormatStyle ParsedStyle = {}; 18962 ParsedStyle.Language = FormatStyle::LK_Cpp; 18963 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 18964 EXPECT_EQ(Style, ParsedStyle); 18965 } 18966 18967 TEST_F(FormatTest, WorksFor8bitEncodings) { 18968 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 18969 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 18970 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 18971 "\"\xef\xee\xf0\xf3...\"", 18972 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 18973 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 18974 "\xef\xee\xf0\xf3...\"", 18975 getLLVMStyleWithColumns(12))); 18976 } 18977 18978 TEST_F(FormatTest, HandlesUTF8BOM) { 18979 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 18980 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 18981 format("\xef\xbb\xbf#include <iostream>")); 18982 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 18983 format("\xef\xbb\xbf\n#include <iostream>")); 18984 } 18985 18986 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 18987 #if !defined(_MSC_VER) 18988 18989 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 18990 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 18991 getLLVMStyleWithColumns(35)); 18992 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 18993 getLLVMStyleWithColumns(31)); 18994 verifyFormat("// Однажды в студёную зимнюю пору...", 18995 getLLVMStyleWithColumns(36)); 18996 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 18997 verifyFormat("/* Однажды в студёную зимнюю пору... */", 18998 getLLVMStyleWithColumns(39)); 18999 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 19000 getLLVMStyleWithColumns(35)); 19001 } 19002 19003 TEST_F(FormatTest, SplitsUTF8Strings) { 19004 // Non-printable characters' width is currently considered to be the length in 19005 // bytes in UTF8. The characters can be displayed in very different manner 19006 // (zero-width, single width with a substitution glyph, expanded to their code 19007 // (e.g. "<8d>"), so there's no single correct way to handle them. 19008 EXPECT_EQ("\"aaaaÄ\"\n" 19009 "\"\xc2\x8d\";", 19010 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 19011 EXPECT_EQ("\"aaaaaaaÄ\"\n" 19012 "\"\xc2\x8d\";", 19013 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 19014 EXPECT_EQ("\"Однажды, в \"\n" 19015 "\"студёную \"\n" 19016 "\"зимнюю \"\n" 19017 "\"пору,\"", 19018 format("\"Однажды, в студёную зимнюю пору,\"", 19019 getLLVMStyleWithColumns(13))); 19020 EXPECT_EQ( 19021 "\"一 二 三 \"\n" 19022 "\"四 五六 \"\n" 19023 "\"七 八 九 \"\n" 19024 "\"十\"", 19025 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 19026 EXPECT_EQ("\"一\t\"\n" 19027 "\"二 \t\"\n" 19028 "\"三 四 \"\n" 19029 "\"五\t\"\n" 19030 "\"六 \t\"\n" 19031 "\"七 \"\n" 19032 "\"八九十\tqq\"", 19033 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 19034 getLLVMStyleWithColumns(11))); 19035 19036 // UTF8 character in an escape sequence. 19037 EXPECT_EQ("\"aaaaaa\"\n" 19038 "\"\\\xC2\x8D\"", 19039 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 19040 } 19041 19042 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 19043 EXPECT_EQ("const char *sssss =\n" 19044 " \"一二三四五六七八\\\n" 19045 " 九 十\";", 19046 format("const char *sssss = \"一二三四五六七八\\\n" 19047 " 九 十\";", 19048 getLLVMStyleWithColumns(30))); 19049 } 19050 19051 TEST_F(FormatTest, SplitsUTF8LineComments) { 19052 EXPECT_EQ("// aaaaÄ\xc2\x8d", 19053 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 19054 EXPECT_EQ("// Я из лесу\n" 19055 "// вышел; был\n" 19056 "// сильный\n" 19057 "// мороз.", 19058 format("// Я из лесу вышел; был сильный мороз.", 19059 getLLVMStyleWithColumns(13))); 19060 EXPECT_EQ("// 一二三\n" 19061 "// 四五六七\n" 19062 "// 八 九\n" 19063 "// 十", 19064 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 19065 } 19066 19067 TEST_F(FormatTest, SplitsUTF8BlockComments) { 19068 EXPECT_EQ("/* Гляжу,\n" 19069 " * поднимается\n" 19070 " * медленно в\n" 19071 " * гору\n" 19072 " * Лошадка,\n" 19073 " * везущая\n" 19074 " * хворосту\n" 19075 " * воз. */", 19076 format("/* Гляжу, поднимается медленно в гору\n" 19077 " * Лошадка, везущая хворосту воз. */", 19078 getLLVMStyleWithColumns(13))); 19079 EXPECT_EQ( 19080 "/* 一二三\n" 19081 " * 四五六七\n" 19082 " * 八 九\n" 19083 " * 十 */", 19084 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 19085 EXPECT_EQ("/* \n" 19086 " * \n" 19087 " * - */", 19088 format("/* - */", getLLVMStyleWithColumns(12))); 19089 } 19090 19091 #endif // _MSC_VER 19092 19093 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 19094 FormatStyle Style = getLLVMStyle(); 19095 19096 Style.ConstructorInitializerIndentWidth = 4; 19097 verifyFormat( 19098 "SomeClass::Constructor()\n" 19099 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 19100 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 19101 Style); 19102 19103 Style.ConstructorInitializerIndentWidth = 2; 19104 verifyFormat( 19105 "SomeClass::Constructor()\n" 19106 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 19107 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 19108 Style); 19109 19110 Style.ConstructorInitializerIndentWidth = 0; 19111 verifyFormat( 19112 "SomeClass::Constructor()\n" 19113 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 19114 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 19115 Style); 19116 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 19117 verifyFormat( 19118 "SomeLongTemplateVariableName<\n" 19119 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 19120 Style); 19121 verifyFormat("bool smaller = 1 < " 19122 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 19123 " " 19124 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 19125 Style); 19126 19127 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 19128 verifyFormat("SomeClass::Constructor() :\n" 19129 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n" 19130 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}", 19131 Style); 19132 } 19133 19134 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 19135 FormatStyle Style = getLLVMStyle(); 19136 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 19137 Style.ConstructorInitializerIndentWidth = 4; 19138 verifyFormat("SomeClass::Constructor()\n" 19139 " : a(a)\n" 19140 " , b(b)\n" 19141 " , c(c) {}", 19142 Style); 19143 verifyFormat("SomeClass::Constructor()\n" 19144 " : a(a) {}", 19145 Style); 19146 19147 Style.ColumnLimit = 0; 19148 verifyFormat("SomeClass::Constructor()\n" 19149 " : a(a) {}", 19150 Style); 19151 verifyFormat("SomeClass::Constructor() noexcept\n" 19152 " : a(a) {}", 19153 Style); 19154 verifyFormat("SomeClass::Constructor()\n" 19155 " : a(a)\n" 19156 " , b(b)\n" 19157 " , c(c) {}", 19158 Style); 19159 verifyFormat("SomeClass::Constructor()\n" 19160 " : a(a) {\n" 19161 " foo();\n" 19162 " bar();\n" 19163 "}", 19164 Style); 19165 19166 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 19167 verifyFormat("SomeClass::Constructor()\n" 19168 " : a(a)\n" 19169 " , b(b)\n" 19170 " , c(c) {\n}", 19171 Style); 19172 verifyFormat("SomeClass::Constructor()\n" 19173 " : a(a) {\n}", 19174 Style); 19175 19176 Style.ColumnLimit = 80; 19177 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 19178 Style.ConstructorInitializerIndentWidth = 2; 19179 verifyFormat("SomeClass::Constructor()\n" 19180 " : a(a)\n" 19181 " , b(b)\n" 19182 " , c(c) {}", 19183 Style); 19184 19185 Style.ConstructorInitializerIndentWidth = 0; 19186 verifyFormat("SomeClass::Constructor()\n" 19187 ": a(a)\n" 19188 ", b(b)\n" 19189 ", c(c) {}", 19190 Style); 19191 19192 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 19193 Style.ConstructorInitializerIndentWidth = 4; 19194 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 19195 verifyFormat( 19196 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 19197 Style); 19198 verifyFormat( 19199 "SomeClass::Constructor()\n" 19200 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 19201 Style); 19202 Style.ConstructorInitializerIndentWidth = 4; 19203 Style.ColumnLimit = 60; 19204 verifyFormat("SomeClass::Constructor()\n" 19205 " : aaaaaaaa(aaaaaaaa)\n" 19206 " , aaaaaaaa(aaaaaaaa)\n" 19207 " , aaaaaaaa(aaaaaaaa) {}", 19208 Style); 19209 } 19210 19211 TEST_F(FormatTest, Destructors) { 19212 verifyFormat("void F(int &i) { i.~int(); }"); 19213 verifyFormat("void F(int &i) { i->~int(); }"); 19214 } 19215 19216 TEST_F(FormatTest, FormatsWithWebKitStyle) { 19217 FormatStyle Style = getWebKitStyle(); 19218 19219 // Don't indent in outer namespaces. 19220 verifyFormat("namespace outer {\n" 19221 "int i;\n" 19222 "namespace inner {\n" 19223 " int i;\n" 19224 "} // namespace inner\n" 19225 "} // namespace outer\n" 19226 "namespace other_outer {\n" 19227 "int i;\n" 19228 "}", 19229 Style); 19230 19231 // Don't indent case labels. 19232 verifyFormat("switch (variable) {\n" 19233 "case 1:\n" 19234 "case 2:\n" 19235 " doSomething();\n" 19236 " break;\n" 19237 "default:\n" 19238 " ++variable;\n" 19239 "}", 19240 Style); 19241 19242 // Wrap before binary operators. 19243 EXPECT_EQ("void f()\n" 19244 "{\n" 19245 " if (aaaaaaaaaaaaaaaa\n" 19246 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 19247 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 19248 " return;\n" 19249 "}", 19250 format("void f() {\n" 19251 "if (aaaaaaaaaaaaaaaa\n" 19252 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 19253 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 19254 "return;\n" 19255 "}", 19256 Style)); 19257 19258 // Allow functions on a single line. 19259 verifyFormat("void f() { return; }", Style); 19260 19261 // Allow empty blocks on a single line and insert a space in empty blocks. 19262 EXPECT_EQ("void f() { }", format("void f() {}", Style)); 19263 EXPECT_EQ("while (true) { }", format("while (true) {}", Style)); 19264 // However, don't merge non-empty short loops. 19265 EXPECT_EQ("while (true) {\n" 19266 " continue;\n" 19267 "}", 19268 format("while (true) { continue; }", Style)); 19269 19270 // Constructor initializers are formatted one per line with the "," on the 19271 // new line. 19272 verifyFormat("Constructor()\n" 19273 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 19274 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 19275 " aaaaaaaaaaaaaa)\n" 19276 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 19277 "{\n" 19278 "}", 19279 Style); 19280 verifyFormat("SomeClass::Constructor()\n" 19281 " : a(a)\n" 19282 "{\n" 19283 "}", 19284 Style); 19285 EXPECT_EQ("SomeClass::Constructor()\n" 19286 " : a(a)\n" 19287 "{\n" 19288 "}", 19289 format("SomeClass::Constructor():a(a){}", Style)); 19290 verifyFormat("SomeClass::Constructor()\n" 19291 " : a(a)\n" 19292 " , b(b)\n" 19293 " , c(c)\n" 19294 "{\n" 19295 "}", 19296 Style); 19297 verifyFormat("SomeClass::Constructor()\n" 19298 " : a(a)\n" 19299 "{\n" 19300 " foo();\n" 19301 " bar();\n" 19302 "}", 19303 Style); 19304 19305 // Access specifiers should be aligned left. 19306 verifyFormat("class C {\n" 19307 "public:\n" 19308 " int i;\n" 19309 "};", 19310 Style); 19311 19312 // Do not align comments. 19313 verifyFormat("int a; // Do not\n" 19314 "double b; // align comments.", 19315 Style); 19316 19317 // Do not align operands. 19318 EXPECT_EQ("ASSERT(aaaa\n" 19319 " || bbbb);", 19320 format("ASSERT ( aaaa\n||bbbb);", Style)); 19321 19322 // Accept input's line breaks. 19323 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 19324 " || bbbbbbbbbbbbbbb) {\n" 19325 " i++;\n" 19326 "}", 19327 format("if (aaaaaaaaaaaaaaa\n" 19328 "|| bbbbbbbbbbbbbbb) { i++; }", 19329 Style)); 19330 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 19331 " i++;\n" 19332 "}", 19333 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 19334 19335 // Don't automatically break all macro definitions (llvm.org/PR17842). 19336 verifyFormat("#define aNumber 10", Style); 19337 // However, generally keep the line breaks that the user authored. 19338 EXPECT_EQ("#define aNumber \\\n" 19339 " 10", 19340 format("#define aNumber \\\n" 19341 " 10", 19342 Style)); 19343 19344 // Keep empty and one-element array literals on a single line. 19345 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 19346 " copyItems:YES];", 19347 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 19348 "copyItems:YES];", 19349 Style)); 19350 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 19351 " copyItems:YES];", 19352 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 19353 " copyItems:YES];", 19354 Style)); 19355 // FIXME: This does not seem right, there should be more indentation before 19356 // the array literal's entries. Nested blocks have the same problem. 19357 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 19358 " @\"a\",\n" 19359 " @\"a\"\n" 19360 "]\n" 19361 " copyItems:YES];", 19362 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 19363 " @\"a\",\n" 19364 " @\"a\"\n" 19365 " ]\n" 19366 " copyItems:YES];", 19367 Style)); 19368 EXPECT_EQ( 19369 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 19370 " copyItems:YES];", 19371 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 19372 " copyItems:YES];", 19373 Style)); 19374 19375 verifyFormat("[self.a b:c c:d];", Style); 19376 EXPECT_EQ("[self.a b:c\n" 19377 " c:d];", 19378 format("[self.a b:c\n" 19379 "c:d];", 19380 Style)); 19381 } 19382 19383 TEST_F(FormatTest, FormatsLambdas) { 19384 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 19385 verifyFormat( 19386 "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();\n"); 19387 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 19388 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 19389 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 19390 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 19391 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 19392 verifyFormat("auto c = [a = [b = 42] {}] {};\n"); 19393 verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n"); 19394 verifyFormat("int x = f(*+[] {});"); 19395 verifyFormat("void f() {\n" 19396 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 19397 "}\n"); 19398 verifyFormat("void f() {\n" 19399 " other(x.begin(), //\n" 19400 " x.end(), //\n" 19401 " [&](int, int) { return 1; });\n" 19402 "}\n"); 19403 verifyFormat("void f() {\n" 19404 " other.other.other.other.other(\n" 19405 " x.begin(), x.end(),\n" 19406 " [something, rather](int, int, int, int, int, int, int) { " 19407 "return 1; });\n" 19408 "}\n"); 19409 verifyFormat( 19410 "void f() {\n" 19411 " other.other.other.other.other(\n" 19412 " x.begin(), x.end(),\n" 19413 " [something, rather](int, int, int, int, int, int, int) {\n" 19414 " //\n" 19415 " });\n" 19416 "}\n"); 19417 verifyFormat("SomeFunction([]() { // A cool function...\n" 19418 " return 43;\n" 19419 "});"); 19420 EXPECT_EQ("SomeFunction([]() {\n" 19421 "#define A a\n" 19422 " return 43;\n" 19423 "});", 19424 format("SomeFunction([](){\n" 19425 "#define A a\n" 19426 "return 43;\n" 19427 "});")); 19428 verifyFormat("void f() {\n" 19429 " SomeFunction([](decltype(x), A *a) {});\n" 19430 " SomeFunction([](typeof(x), A *a) {});\n" 19431 " SomeFunction([](_Atomic(x), A *a) {});\n" 19432 " SomeFunction([](__underlying_type(x), A *a) {});\n" 19433 "}"); 19434 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 19435 " [](const aaaaaaaaaa &a) { return a; });"); 19436 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 19437 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 19438 "});"); 19439 verifyFormat("Constructor()\n" 19440 " : Field([] { // comment\n" 19441 " int i;\n" 19442 " }) {}"); 19443 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 19444 " return some_parameter.size();\n" 19445 "};"); 19446 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 19447 " [](const string &s) { return s; };"); 19448 verifyFormat("int i = aaaaaa ? 1 //\n" 19449 " : [] {\n" 19450 " return 2; //\n" 19451 " }();"); 19452 verifyFormat("llvm::errs() << \"number of twos is \"\n" 19453 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 19454 " return x == 2; // force break\n" 19455 " });"); 19456 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 19457 " [=](int iiiiiiiiiiii) {\n" 19458 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n" 19459 " aaaaaaaaaaaaaaaaaaaaaaa;\n" 19460 " });", 19461 getLLVMStyleWithColumns(60)); 19462 19463 verifyFormat("SomeFunction({[&] {\n" 19464 " // comment\n" 19465 " },\n" 19466 " [&] {\n" 19467 " // comment\n" 19468 " }});"); 19469 verifyFormat("SomeFunction({[&] {\n" 19470 " // comment\n" 19471 "}});"); 19472 verifyFormat( 19473 "virtual aaaaaaaaaaaaaaaa(\n" 19474 " std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n" 19475 " aaaaa aaaaaaaaa);"); 19476 19477 // Lambdas with return types. 19478 verifyFormat("int c = []() -> int { return 2; }();\n"); 19479 verifyFormat("int c = []() -> int * { return 2; }();\n"); 19480 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 19481 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 19482 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 19483 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 19484 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 19485 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 19486 verifyFormat("[a, a]() -> a<1> {};"); 19487 verifyFormat("[]() -> foo<5 + 2> { return {}; };"); 19488 verifyFormat("[]() -> foo<5 - 2> { return {}; };"); 19489 verifyFormat("[]() -> foo<5 / 2> { return {}; };"); 19490 verifyFormat("[]() -> foo<5 * 2> { return {}; };"); 19491 verifyFormat("[]() -> foo<5 % 2> { return {}; };"); 19492 verifyFormat("[]() -> foo<5 << 2> { return {}; };"); 19493 verifyFormat("[]() -> foo<!5> { return {}; };"); 19494 verifyFormat("[]() -> foo<~5> { return {}; };"); 19495 verifyFormat("[]() -> foo<5 | 2> { return {}; };"); 19496 verifyFormat("[]() -> foo<5 || 2> { return {}; };"); 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 < 2> { return {}; };"); 19504 verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };"); 19505 verifyFormat("namespace bar {\n" 19506 "// broken:\n" 19507 "auto foo{[]() -> foo<5 + 2> { return {}; }};\n" 19508 "} // namespace bar"); 19509 verifyFormat("namespace bar {\n" 19510 "// broken:\n" 19511 "auto foo{[]() -> foo<5 - 2> { return {}; }};\n" 19512 "} // namespace bar"); 19513 verifyFormat("namespace bar {\n" 19514 "// broken:\n" 19515 "auto foo{[]() -> foo<5 / 2> { return {}; }};\n" 19516 "} // namespace bar"); 19517 verifyFormat("namespace bar {\n" 19518 "// broken:\n" 19519 "auto foo{[]() -> foo<5 * 2> { return {}; }};\n" 19520 "} // namespace bar"); 19521 verifyFormat("namespace bar {\n" 19522 "// broken:\n" 19523 "auto foo{[]() -> foo<5 % 2> { return {}; }};\n" 19524 "} // namespace bar"); 19525 verifyFormat("namespace bar {\n" 19526 "// broken:\n" 19527 "auto foo{[]() -> foo<5 << 2> { return {}; }};\n" 19528 "} // namespace bar"); 19529 verifyFormat("namespace bar {\n" 19530 "// broken:\n" 19531 "auto foo{[]() -> foo<!5> { return {}; }};\n" 19532 "} // namespace bar"); 19533 verifyFormat("namespace bar {\n" 19534 "// broken:\n" 19535 "auto foo{[]() -> foo<~5> { return {}; }};\n" 19536 "} // namespace bar"); 19537 verifyFormat("namespace bar {\n" 19538 "// broken:\n" 19539 "auto foo{[]() -> foo<5 | 2> { return {}; }};\n" 19540 "} // namespace bar"); 19541 verifyFormat("namespace bar {\n" 19542 "// broken:\n" 19543 "auto foo{[]() -> foo<5 || 2> { return {}; }};\n" 19544 "} // namespace bar"); 19545 verifyFormat("namespace bar {\n" 19546 "// broken:\n" 19547 "auto foo{[]() -> foo<5 & 2> { return {}; }};\n" 19548 "} // namespace bar"); 19549 verifyFormat("namespace bar {\n" 19550 "// broken:\n" 19551 "auto foo{[]() -> foo<5 && 2> { return {}; }};\n" 19552 "} // namespace bar"); 19553 verifyFormat("namespace bar {\n" 19554 "// broken:\n" 19555 "auto foo{[]() -> foo<5 == 2> { return {}; }};\n" 19556 "} // namespace bar"); 19557 verifyFormat("namespace bar {\n" 19558 "// broken:\n" 19559 "auto foo{[]() -> foo<5 != 2> { return {}; }};\n" 19560 "} // namespace bar"); 19561 verifyFormat("namespace bar {\n" 19562 "// broken:\n" 19563 "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n" 19564 "} // namespace bar"); 19565 verifyFormat("namespace bar {\n" 19566 "// broken:\n" 19567 "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n" 19568 "} // namespace bar"); 19569 verifyFormat("namespace bar {\n" 19570 "// broken:\n" 19571 "auto foo{[]() -> foo<5 < 2> { return {}; }};\n" 19572 "} // namespace bar"); 19573 verifyFormat("namespace bar {\n" 19574 "// broken:\n" 19575 "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n" 19576 "} // namespace bar"); 19577 verifyFormat("[]() -> a<1> {};"); 19578 verifyFormat("[]() -> a<1> { ; };"); 19579 verifyFormat("[]() -> a<1> { ; }();"); 19580 verifyFormat("[a, a]() -> a<true> {};"); 19581 verifyFormat("[]() -> a<true> {};"); 19582 verifyFormat("[]() -> a<true> { ; };"); 19583 verifyFormat("[]() -> a<true> { ; }();"); 19584 verifyFormat("[a, a]() -> a<false> {};"); 19585 verifyFormat("[]() -> a<false> {};"); 19586 verifyFormat("[]() -> a<false> { ; };"); 19587 verifyFormat("[]() -> a<false> { ; }();"); 19588 verifyFormat("auto foo{[]() -> foo<false> { ; }};"); 19589 verifyFormat("namespace bar {\n" 19590 "auto foo{[]() -> foo<false> { ; }};\n" 19591 "} // namespace bar"); 19592 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 19593 " int j) -> int {\n" 19594 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 19595 "};"); 19596 verifyFormat( 19597 "aaaaaaaaaaaaaaaaaaaaaa(\n" 19598 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 19599 " return aaaaaaaaaaaaaaaaa;\n" 19600 " });", 19601 getLLVMStyleWithColumns(70)); 19602 verifyFormat("[]() //\n" 19603 " -> int {\n" 19604 " return 1; //\n" 19605 "};"); 19606 verifyFormat("[]() -> Void<T...> {};"); 19607 verifyFormat("[a, b]() -> Tuple<T...> { return {}; };"); 19608 19609 // Lambdas with explicit template argument lists. 19610 verifyFormat( 19611 "auto L = []<template <typename> class T, class U>(T<U> &&a) {};\n"); 19612 19613 // Multiple lambdas in the same parentheses change indentation rules. These 19614 // lambdas are forced to start on new lines. 19615 verifyFormat("SomeFunction(\n" 19616 " []() {\n" 19617 " //\n" 19618 " },\n" 19619 " []() {\n" 19620 " //\n" 19621 " });"); 19622 19623 // A lambda passed as arg0 is always pushed to the next line. 19624 verifyFormat("SomeFunction(\n" 19625 " [this] {\n" 19626 " //\n" 19627 " },\n" 19628 " 1);\n"); 19629 19630 // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like 19631 // the arg0 case above. 19632 auto Style = getGoogleStyle(); 19633 Style.BinPackArguments = false; 19634 verifyFormat("SomeFunction(\n" 19635 " a,\n" 19636 " [this] {\n" 19637 " //\n" 19638 " },\n" 19639 " b);\n", 19640 Style); 19641 verifyFormat("SomeFunction(\n" 19642 " a,\n" 19643 " [this] {\n" 19644 " //\n" 19645 " },\n" 19646 " b);\n"); 19647 19648 // A lambda with a very long line forces arg0 to be pushed out irrespective of 19649 // the BinPackArguments value (as long as the code is wide enough). 19650 verifyFormat( 19651 "something->SomeFunction(\n" 19652 " a,\n" 19653 " [this] {\n" 19654 " " 19655 "D0000000000000000000000000000000000000000000000000000000000001();\n" 19656 " },\n" 19657 " b);\n"); 19658 19659 // A multi-line lambda is pulled up as long as the introducer fits on the 19660 // previous line and there are no further args. 19661 verifyFormat("function(1, [this, that] {\n" 19662 " //\n" 19663 "});\n"); 19664 verifyFormat("function([this, that] {\n" 19665 " //\n" 19666 "});\n"); 19667 // FIXME: this format is not ideal and we should consider forcing the first 19668 // arg onto its own line. 19669 verifyFormat("function(a, b, c, //\n" 19670 " d, [this, that] {\n" 19671 " //\n" 19672 " });\n"); 19673 19674 // Multiple lambdas are treated correctly even when there is a short arg0. 19675 verifyFormat("SomeFunction(\n" 19676 " 1,\n" 19677 " [this] {\n" 19678 " //\n" 19679 " },\n" 19680 " [this] {\n" 19681 " //\n" 19682 " },\n" 19683 " 1);\n"); 19684 19685 // More complex introducers. 19686 verifyFormat("return [i, args...] {};"); 19687 19688 // Not lambdas. 19689 verifyFormat("constexpr char hello[]{\"hello\"};"); 19690 verifyFormat("double &operator[](int i) { return 0; }\n" 19691 "int i;"); 19692 verifyFormat("std::unique_ptr<int[]> foo() {}"); 19693 verifyFormat("int i = a[a][a]->f();"); 19694 verifyFormat("int i = (*b)[a]->f();"); 19695 19696 // Other corner cases. 19697 verifyFormat("void f() {\n" 19698 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 19699 " );\n" 19700 "}"); 19701 19702 // Lambdas created through weird macros. 19703 verifyFormat("void f() {\n" 19704 " MACRO((const AA &a) { return 1; });\n" 19705 " MACRO((AA &a) { return 1; });\n" 19706 "}"); 19707 19708 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 19709 " doo_dah();\n" 19710 " doo_dah();\n" 19711 " })) {\n" 19712 "}"); 19713 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n" 19714 " doo_dah();\n" 19715 " doo_dah();\n" 19716 " })) {\n" 19717 "}"); 19718 verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n" 19719 " doo_dah();\n" 19720 " doo_dah();\n" 19721 " })) {\n" 19722 "}"); 19723 verifyFormat("auto lambda = []() {\n" 19724 " int a = 2\n" 19725 "#if A\n" 19726 " + 2\n" 19727 "#endif\n" 19728 " ;\n" 19729 "};"); 19730 19731 // Lambdas with complex multiline introducers. 19732 verifyFormat( 19733 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 19734 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n" 19735 " -> ::std::unordered_set<\n" 19736 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 19737 " //\n" 19738 " });"); 19739 19740 FormatStyle DoNotMerge = getLLVMStyle(); 19741 DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 19742 verifyFormat("auto c = []() {\n" 19743 " return b;\n" 19744 "};", 19745 "auto c = []() { return b; };", DoNotMerge); 19746 verifyFormat("auto c = []() {\n" 19747 "};", 19748 " auto c = []() {};", DoNotMerge); 19749 19750 FormatStyle MergeEmptyOnly = getLLVMStyle(); 19751 MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty; 19752 verifyFormat("auto c = []() {\n" 19753 " return b;\n" 19754 "};", 19755 "auto c = []() {\n" 19756 " return b;\n" 19757 " };", 19758 MergeEmptyOnly); 19759 verifyFormat("auto c = []() {};", 19760 "auto c = []() {\n" 19761 "};", 19762 MergeEmptyOnly); 19763 19764 FormatStyle MergeInline = getLLVMStyle(); 19765 MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline; 19766 verifyFormat("auto c = []() {\n" 19767 " return b;\n" 19768 "};", 19769 "auto c = []() { return b; };", MergeInline); 19770 verifyFormat("function([]() { return b; })", "function([]() { return b; })", 19771 MergeInline); 19772 verifyFormat("function([]() { return b; }, a)", 19773 "function([]() { return b; }, a)", MergeInline); 19774 verifyFormat("function(a, []() { return b; })", 19775 "function(a, []() { return b; })", MergeInline); 19776 19777 // Check option "BraceWrapping.BeforeLambdaBody" and different state of 19778 // AllowShortLambdasOnASingleLine 19779 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle(); 19780 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 19781 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 19782 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 19783 FormatStyle::ShortLambdaStyle::SLS_None; 19784 verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n" 19785 " []()\n" 19786 " {\n" 19787 " return 17;\n" 19788 " });", 19789 LLVMWithBeforeLambdaBody); 19790 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n" 19791 " []()\n" 19792 " {\n" 19793 " });", 19794 LLVMWithBeforeLambdaBody); 19795 verifyFormat("auto fct_SLS_None = []()\n" 19796 "{\n" 19797 " return 17;\n" 19798 "};", 19799 LLVMWithBeforeLambdaBody); 19800 verifyFormat("TwoNestedLambdas_SLS_None(\n" 19801 " []()\n" 19802 " {\n" 19803 " return Call(\n" 19804 " []()\n" 19805 " {\n" 19806 " return 17;\n" 19807 " });\n" 19808 " });", 19809 LLVMWithBeforeLambdaBody); 19810 verifyFormat("void Fct() {\n" 19811 " return {[]()\n" 19812 " {\n" 19813 " return 17;\n" 19814 " }};\n" 19815 "}", 19816 LLVMWithBeforeLambdaBody); 19817 19818 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 19819 FormatStyle::ShortLambdaStyle::SLS_Empty; 19820 verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n" 19821 " []()\n" 19822 " {\n" 19823 " return 17;\n" 19824 " });", 19825 LLVMWithBeforeLambdaBody); 19826 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});", 19827 LLVMWithBeforeLambdaBody); 19828 verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL" 19829 "ongFunctionName_SLS_Empty(\n" 19830 " []() {});", 19831 LLVMWithBeforeLambdaBody); 19832 verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n" 19833 " []()\n" 19834 " {\n" 19835 " return 17;\n" 19836 " });", 19837 LLVMWithBeforeLambdaBody); 19838 verifyFormat("auto fct_SLS_Empty = []()\n" 19839 "{\n" 19840 " return 17;\n" 19841 "};", 19842 LLVMWithBeforeLambdaBody); 19843 verifyFormat("TwoNestedLambdas_SLS_Empty(\n" 19844 " []()\n" 19845 " {\n" 19846 " return Call([]() {});\n" 19847 " });", 19848 LLVMWithBeforeLambdaBody); 19849 verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n" 19850 " []()\n" 19851 " {\n" 19852 " return Call([]() {});\n" 19853 " });", 19854 LLVMWithBeforeLambdaBody); 19855 verifyFormat( 19856 "FctWithLongLineInLambda_SLS_Empty(\n" 19857 " []()\n" 19858 " {\n" 19859 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 19860 " AndShouldNotBeConsiderAsInline,\n" 19861 " LambdaBodyMustBeBreak);\n" 19862 " });", 19863 LLVMWithBeforeLambdaBody); 19864 19865 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 19866 FormatStyle::ShortLambdaStyle::SLS_Inline; 19867 verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });", 19868 LLVMWithBeforeLambdaBody); 19869 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});", 19870 LLVMWithBeforeLambdaBody); 19871 verifyFormat("auto fct_SLS_Inline = []()\n" 19872 "{\n" 19873 " return 17;\n" 19874 "};", 19875 LLVMWithBeforeLambdaBody); 19876 verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return " 19877 "17; }); });", 19878 LLVMWithBeforeLambdaBody); 19879 verifyFormat( 19880 "FctWithLongLineInLambda_SLS_Inline(\n" 19881 " []()\n" 19882 " {\n" 19883 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 19884 " AndShouldNotBeConsiderAsInline,\n" 19885 " LambdaBodyMustBeBreak);\n" 19886 " });", 19887 LLVMWithBeforeLambdaBody); 19888 verifyFormat("FctWithMultipleParams_SLS_Inline(" 19889 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n" 19890 " []() { return 17; });", 19891 LLVMWithBeforeLambdaBody); 19892 verifyFormat( 19893 "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });", 19894 LLVMWithBeforeLambdaBody); 19895 19896 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 19897 FormatStyle::ShortLambdaStyle::SLS_All; 19898 verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });", 19899 LLVMWithBeforeLambdaBody); 19900 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});", 19901 LLVMWithBeforeLambdaBody); 19902 verifyFormat("auto fct_SLS_All = []() { return 17; };", 19903 LLVMWithBeforeLambdaBody); 19904 verifyFormat("FctWithOneParam_SLS_All(\n" 19905 " []()\n" 19906 " {\n" 19907 " // A cool function...\n" 19908 " return 43;\n" 19909 " });", 19910 LLVMWithBeforeLambdaBody); 19911 verifyFormat("FctWithMultipleParams_SLS_All(" 19912 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n" 19913 " []() { return 17; });", 19914 LLVMWithBeforeLambdaBody); 19915 verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });", 19916 LLVMWithBeforeLambdaBody); 19917 verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });", 19918 LLVMWithBeforeLambdaBody); 19919 verifyFormat( 19920 "FctWithLongLineInLambda_SLS_All(\n" 19921 " []()\n" 19922 " {\n" 19923 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 19924 " AndShouldNotBeConsiderAsInline,\n" 19925 " LambdaBodyMustBeBreak);\n" 19926 " });", 19927 LLVMWithBeforeLambdaBody); 19928 verifyFormat( 19929 "auto fct_SLS_All = []()\n" 19930 "{\n" 19931 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 19932 " AndShouldNotBeConsiderAsInline,\n" 19933 " LambdaBodyMustBeBreak);\n" 19934 "};", 19935 LLVMWithBeforeLambdaBody); 19936 LLVMWithBeforeLambdaBody.BinPackParameters = false; 19937 verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);", 19938 LLVMWithBeforeLambdaBody); 19939 verifyFormat( 19940 "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n" 19941 " FirstParam,\n" 19942 " SecondParam,\n" 19943 " ThirdParam,\n" 19944 " FourthParam);", 19945 LLVMWithBeforeLambdaBody); 19946 verifyFormat("FctWithLongLineInLambda_SLS_All(\n" 19947 " []() { return " 19948 "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n" 19949 " FirstParam,\n" 19950 " SecondParam,\n" 19951 " ThirdParam,\n" 19952 " FourthParam);", 19953 LLVMWithBeforeLambdaBody); 19954 verifyFormat( 19955 "FctWithLongLineInLambda_SLS_All(FirstParam,\n" 19956 " SecondParam,\n" 19957 " ThirdParam,\n" 19958 " FourthParam,\n" 19959 " []() { return SomeValueNotSoLong; });", 19960 LLVMWithBeforeLambdaBody); 19961 verifyFormat("FctWithLongLineInLambda_SLS_All(\n" 19962 " []()\n" 19963 " {\n" 19964 " return " 19965 "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB" 19966 "eConsiderAsInline;\n" 19967 " });", 19968 LLVMWithBeforeLambdaBody); 19969 verifyFormat( 19970 "FctWithLongLineInLambda_SLS_All(\n" 19971 " []()\n" 19972 " {\n" 19973 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 19974 " AndShouldNotBeConsiderAsInline,\n" 19975 " LambdaBodyMustBeBreak);\n" 19976 " });", 19977 LLVMWithBeforeLambdaBody); 19978 verifyFormat("FctWithTwoParams_SLS_All(\n" 19979 " []()\n" 19980 " {\n" 19981 " // A cool function...\n" 19982 " return 43;\n" 19983 " },\n" 19984 " 87);", 19985 LLVMWithBeforeLambdaBody); 19986 verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);", 19987 LLVMWithBeforeLambdaBody); 19988 verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });", 19989 LLVMWithBeforeLambdaBody); 19990 verifyFormat( 19991 "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });", 19992 LLVMWithBeforeLambdaBody); 19993 verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; " 19994 "}); }, x);", 19995 LLVMWithBeforeLambdaBody); 19996 verifyFormat("TwoNestedLambdas_SLS_All(\n" 19997 " []()\n" 19998 " {\n" 19999 " // A cool function...\n" 20000 " return Call([]() { return 17; });\n" 20001 " });", 20002 LLVMWithBeforeLambdaBody); 20003 verifyFormat("TwoNestedLambdas_SLS_All(\n" 20004 " []()\n" 20005 " {\n" 20006 " return Call(\n" 20007 " []()\n" 20008 " {\n" 20009 " // A cool function...\n" 20010 " return 17;\n" 20011 " });\n" 20012 " });", 20013 LLVMWithBeforeLambdaBody); 20014 20015 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20016 FormatStyle::ShortLambdaStyle::SLS_None; 20017 20018 verifyFormat("auto select = [this]() -> const Library::Object *\n" 20019 "{\n" 20020 " return MyAssignment::SelectFromList(this);\n" 20021 "};\n", 20022 LLVMWithBeforeLambdaBody); 20023 20024 verifyFormat("auto select = [this]() -> const Library::Object &\n" 20025 "{\n" 20026 " return MyAssignment::SelectFromList(this);\n" 20027 "};\n", 20028 LLVMWithBeforeLambdaBody); 20029 20030 verifyFormat("auto select = [this]() -> std::unique_ptr<Object>\n" 20031 "{\n" 20032 " return MyAssignment::SelectFromList(this);\n" 20033 "};\n", 20034 LLVMWithBeforeLambdaBody); 20035 20036 verifyFormat("namespace test {\n" 20037 "class Test {\n" 20038 "public:\n" 20039 " Test() = default;\n" 20040 "};\n" 20041 "} // namespace test", 20042 LLVMWithBeforeLambdaBody); 20043 20044 // Lambdas with different indentation styles. 20045 Style = getLLVMStyleWithColumns(100); 20046 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 20047 " return promise.then(\n" 20048 " [this, &someVariable, someObject = " 20049 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 20050 " return someObject.startAsyncAction().then(\n" 20051 " [this, &someVariable](AsyncActionResult result) " 20052 "mutable { result.processMore(); });\n" 20053 " });\n" 20054 "}\n", 20055 format("SomeResult doSomething(SomeObject promise) {\n" 20056 " return promise.then([this, &someVariable, someObject = " 20057 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 20058 " return someObject.startAsyncAction().then([this, " 20059 "&someVariable](AsyncActionResult result) mutable {\n" 20060 " result.processMore();\n" 20061 " });\n" 20062 " });\n" 20063 "}\n", 20064 Style)); 20065 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope; 20066 verifyFormat("test() {\n" 20067 " ([]() -> {\n" 20068 " int b = 32;\n" 20069 " return 3;\n" 20070 " }).foo();\n" 20071 "}", 20072 Style); 20073 verifyFormat("test() {\n" 20074 " []() -> {\n" 20075 " int b = 32;\n" 20076 " return 3;\n" 20077 " }\n" 20078 "}", 20079 Style); 20080 verifyFormat("std::sort(v.begin(), v.end(),\n" 20081 " [](const auto &someLongArgumentName, const auto " 20082 "&someOtherLongArgumentName) {\n" 20083 " return someLongArgumentName.someMemberVariable < " 20084 "someOtherLongArgumentName.someMemberVariable;\n" 20085 "});", 20086 Style); 20087 verifyFormat("test() {\n" 20088 " (\n" 20089 " []() -> {\n" 20090 " int b = 32;\n" 20091 " return 3;\n" 20092 " },\n" 20093 " foo, bar)\n" 20094 " .foo();\n" 20095 "}", 20096 Style); 20097 verifyFormat("test() {\n" 20098 " ([]() -> {\n" 20099 " int b = 32;\n" 20100 " return 3;\n" 20101 " })\n" 20102 " .foo()\n" 20103 " .bar();\n" 20104 "}", 20105 Style); 20106 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 20107 " return promise.then(\n" 20108 " [this, &someVariable, someObject = " 20109 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 20110 " return someObject.startAsyncAction().then(\n" 20111 " [this, &someVariable](AsyncActionResult result) mutable { " 20112 "result.processMore(); });\n" 20113 " });\n" 20114 "}\n", 20115 format("SomeResult doSomething(SomeObject promise) {\n" 20116 " return promise.then([this, &someVariable, someObject = " 20117 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 20118 " return someObject.startAsyncAction().then([this, " 20119 "&someVariable](AsyncActionResult result) mutable {\n" 20120 " result.processMore();\n" 20121 " });\n" 20122 " });\n" 20123 "}\n", 20124 Style)); 20125 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 20126 " return promise.then([this, &someVariable] {\n" 20127 " return someObject.startAsyncAction().then(\n" 20128 " [this, &someVariable](AsyncActionResult result) mutable { " 20129 "result.processMore(); });\n" 20130 " });\n" 20131 "}\n", 20132 format("SomeResult doSomething(SomeObject promise) {\n" 20133 " return promise.then([this, &someVariable] {\n" 20134 " return someObject.startAsyncAction().then([this, " 20135 "&someVariable](AsyncActionResult result) mutable {\n" 20136 " result.processMore();\n" 20137 " });\n" 20138 " });\n" 20139 "}\n", 20140 Style)); 20141 Style = getGoogleStyle(); 20142 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope; 20143 EXPECT_EQ("#define A \\\n" 20144 " [] { \\\n" 20145 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 20146 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 20147 " }", 20148 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 20149 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 20150 Style)); 20151 // TODO: The current formatting has a minor issue that's not worth fixing 20152 // right now whereby the closing brace is indented relative to the signature 20153 // instead of being aligned. This only happens with macros. 20154 } 20155 20156 TEST_F(FormatTest, LambdaWithLineComments) { 20157 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle(); 20158 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 20159 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 20160 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20161 FormatStyle::ShortLambdaStyle::SLS_All; 20162 20163 verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody); 20164 verifyFormat("auto k = []() // comment\n" 20165 "{ return; }", 20166 LLVMWithBeforeLambdaBody); 20167 verifyFormat("auto k = []() /* comment */ { return; }", 20168 LLVMWithBeforeLambdaBody); 20169 verifyFormat("auto k = []() /* comment */ /* comment */ { return; }", 20170 LLVMWithBeforeLambdaBody); 20171 verifyFormat("auto k = []() // X\n" 20172 "{ return; }", 20173 LLVMWithBeforeLambdaBody); 20174 verifyFormat( 20175 "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n" 20176 "{ return; }", 20177 LLVMWithBeforeLambdaBody); 20178 } 20179 20180 TEST_F(FormatTest, EmptyLinesInLambdas) { 20181 verifyFormat("auto lambda = []() {\n" 20182 " x(); //\n" 20183 "};", 20184 "auto lambda = []() {\n" 20185 "\n" 20186 " x(); //\n" 20187 "\n" 20188 "};"); 20189 } 20190 20191 TEST_F(FormatTest, FormatsBlocks) { 20192 FormatStyle ShortBlocks = getLLVMStyle(); 20193 ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 20194 verifyFormat("int (^Block)(int, int);", ShortBlocks); 20195 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 20196 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 20197 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 20198 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 20199 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 20200 20201 verifyFormat("foo(^{ bar(); });", ShortBlocks); 20202 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 20203 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 20204 20205 verifyFormat("[operation setCompletionBlock:^{\n" 20206 " [self onOperationDone];\n" 20207 "}];"); 20208 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 20209 " [self onOperationDone];\n" 20210 "}]};"); 20211 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 20212 " f();\n" 20213 "}];"); 20214 verifyFormat("int a = [operation block:^int(int *i) {\n" 20215 " return 1;\n" 20216 "}];"); 20217 verifyFormat("[myObject doSomethingWith:arg1\n" 20218 " aaa:^int(int *a) {\n" 20219 " return 1;\n" 20220 " }\n" 20221 " bbb:f(a * bbbbbbbb)];"); 20222 20223 verifyFormat("[operation setCompletionBlock:^{\n" 20224 " [self.delegate newDataAvailable];\n" 20225 "}];", 20226 getLLVMStyleWithColumns(60)); 20227 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 20228 " NSString *path = [self sessionFilePath];\n" 20229 " if (path) {\n" 20230 " // ...\n" 20231 " }\n" 20232 "});"); 20233 verifyFormat("[[SessionService sharedService]\n" 20234 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 20235 " if (window) {\n" 20236 " [self windowDidLoad:window];\n" 20237 " } else {\n" 20238 " [self errorLoadingWindow];\n" 20239 " }\n" 20240 " }];"); 20241 verifyFormat("void (^largeBlock)(void) = ^{\n" 20242 " // ...\n" 20243 "};\n", 20244 getLLVMStyleWithColumns(40)); 20245 verifyFormat("[[SessionService sharedService]\n" 20246 " loadWindowWithCompletionBlock: //\n" 20247 " ^(SessionWindow *window) {\n" 20248 " if (window) {\n" 20249 " [self windowDidLoad:window];\n" 20250 " } else {\n" 20251 " [self errorLoadingWindow];\n" 20252 " }\n" 20253 " }];", 20254 getLLVMStyleWithColumns(60)); 20255 verifyFormat("[myObject doSomethingWith:arg1\n" 20256 " firstBlock:^(Foo *a) {\n" 20257 " // ...\n" 20258 " int i;\n" 20259 " }\n" 20260 " secondBlock:^(Bar *b) {\n" 20261 " // ...\n" 20262 " int i;\n" 20263 " }\n" 20264 " thirdBlock:^Foo(Bar *b) {\n" 20265 " // ...\n" 20266 " int i;\n" 20267 " }];"); 20268 verifyFormat("[myObject doSomethingWith:arg1\n" 20269 " firstBlock:-1\n" 20270 " secondBlock:^(Bar *b) {\n" 20271 " // ...\n" 20272 " int i;\n" 20273 " }];"); 20274 20275 verifyFormat("f(^{\n" 20276 " @autoreleasepool {\n" 20277 " if (a) {\n" 20278 " g();\n" 20279 " }\n" 20280 " }\n" 20281 "});"); 20282 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 20283 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" 20284 "};"); 20285 20286 FormatStyle FourIndent = getLLVMStyle(); 20287 FourIndent.ObjCBlockIndentWidth = 4; 20288 verifyFormat("[operation setCompletionBlock:^{\n" 20289 " [self onOperationDone];\n" 20290 "}];", 20291 FourIndent); 20292 } 20293 20294 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 20295 FormatStyle ZeroColumn = getLLVMStyle(); 20296 ZeroColumn.ColumnLimit = 0; 20297 20298 verifyFormat("[[SessionService sharedService] " 20299 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 20300 " if (window) {\n" 20301 " [self windowDidLoad:window];\n" 20302 " } else {\n" 20303 " [self errorLoadingWindow];\n" 20304 " }\n" 20305 "}];", 20306 ZeroColumn); 20307 EXPECT_EQ("[[SessionService sharedService]\n" 20308 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 20309 " if (window) {\n" 20310 " [self windowDidLoad:window];\n" 20311 " } else {\n" 20312 " [self errorLoadingWindow];\n" 20313 " }\n" 20314 " }];", 20315 format("[[SessionService sharedService]\n" 20316 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 20317 " if (window) {\n" 20318 " [self windowDidLoad:window];\n" 20319 " } else {\n" 20320 " [self errorLoadingWindow];\n" 20321 " }\n" 20322 "}];", 20323 ZeroColumn)); 20324 verifyFormat("[myObject doSomethingWith:arg1\n" 20325 " firstBlock:^(Foo *a) {\n" 20326 " // ...\n" 20327 " int i;\n" 20328 " }\n" 20329 " secondBlock:^(Bar *b) {\n" 20330 " // ...\n" 20331 " int i;\n" 20332 " }\n" 20333 " thirdBlock:^Foo(Bar *b) {\n" 20334 " // ...\n" 20335 " int i;\n" 20336 " }];", 20337 ZeroColumn); 20338 verifyFormat("f(^{\n" 20339 " @autoreleasepool {\n" 20340 " if (a) {\n" 20341 " g();\n" 20342 " }\n" 20343 " }\n" 20344 "});", 20345 ZeroColumn); 20346 verifyFormat("void (^largeBlock)(void) = ^{\n" 20347 " // ...\n" 20348 "};", 20349 ZeroColumn); 20350 20351 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 20352 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 20353 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 20354 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 20355 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 20356 " int i;\n" 20357 "};", 20358 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 20359 } 20360 20361 TEST_F(FormatTest, SupportsCRLF) { 20362 EXPECT_EQ("int a;\r\n" 20363 "int b;\r\n" 20364 "int c;\r\n", 20365 format("int a;\r\n" 20366 " int b;\r\n" 20367 " int c;\r\n", 20368 getLLVMStyle())); 20369 EXPECT_EQ("int a;\r\n" 20370 "int b;\r\n" 20371 "int c;\r\n", 20372 format("int a;\r\n" 20373 " int b;\n" 20374 " int c;\r\n", 20375 getLLVMStyle())); 20376 EXPECT_EQ("int a;\n" 20377 "int b;\n" 20378 "int c;\n", 20379 format("int a;\r\n" 20380 " int b;\n" 20381 " int c;\n", 20382 getLLVMStyle())); 20383 EXPECT_EQ("\"aaaaaaa \"\r\n" 20384 "\"bbbbbbb\";\r\n", 20385 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 20386 EXPECT_EQ("#define A \\\r\n" 20387 " b; \\\r\n" 20388 " c; \\\r\n" 20389 " d;\r\n", 20390 format("#define A \\\r\n" 20391 " b; \\\r\n" 20392 " c; d; \r\n", 20393 getGoogleStyle())); 20394 20395 EXPECT_EQ("/*\r\n" 20396 "multi line block comments\r\n" 20397 "should not introduce\r\n" 20398 "an extra carriage return\r\n" 20399 "*/\r\n", 20400 format("/*\r\n" 20401 "multi line block comments\r\n" 20402 "should not introduce\r\n" 20403 "an extra carriage return\r\n" 20404 "*/\r\n")); 20405 EXPECT_EQ("/*\r\n" 20406 "\r\n" 20407 "*/", 20408 format("/*\r\n" 20409 " \r\r\r\n" 20410 "*/")); 20411 20412 FormatStyle style = getLLVMStyle(); 20413 20414 style.DeriveLineEnding = true; 20415 style.UseCRLF = false; 20416 EXPECT_EQ("union FooBarBazQux {\n" 20417 " int foo;\n" 20418 " int bar;\n" 20419 " int baz;\n" 20420 "};", 20421 format("union FooBarBazQux {\r\n" 20422 " int foo;\n" 20423 " int bar;\r\n" 20424 " int baz;\n" 20425 "};", 20426 style)); 20427 style.UseCRLF = true; 20428 EXPECT_EQ("union FooBarBazQux {\r\n" 20429 " int foo;\r\n" 20430 " int bar;\r\n" 20431 " int baz;\r\n" 20432 "};", 20433 format("union FooBarBazQux {\r\n" 20434 " int foo;\n" 20435 " int bar;\r\n" 20436 " int baz;\n" 20437 "};", 20438 style)); 20439 20440 style.DeriveLineEnding = false; 20441 style.UseCRLF = false; 20442 EXPECT_EQ("union FooBarBazQux {\n" 20443 " int foo;\n" 20444 " int bar;\n" 20445 " int baz;\n" 20446 " int qux;\n" 20447 "};", 20448 format("union FooBarBazQux {\r\n" 20449 " int foo;\n" 20450 " int bar;\r\n" 20451 " int baz;\n" 20452 " int qux;\r\n" 20453 "};", 20454 style)); 20455 style.UseCRLF = true; 20456 EXPECT_EQ("union FooBarBazQux {\r\n" 20457 " int foo;\r\n" 20458 " int bar;\r\n" 20459 " int baz;\r\n" 20460 " int qux;\r\n" 20461 "};", 20462 format("union FooBarBazQux {\r\n" 20463 " int foo;\n" 20464 " int bar;\r\n" 20465 " int baz;\n" 20466 " int qux;\n" 20467 "};", 20468 style)); 20469 20470 style.DeriveLineEnding = true; 20471 style.UseCRLF = false; 20472 EXPECT_EQ("union FooBarBazQux {\r\n" 20473 " int foo;\r\n" 20474 " int bar;\r\n" 20475 " int baz;\r\n" 20476 " int qux;\r\n" 20477 "};", 20478 format("union FooBarBazQux {\r\n" 20479 " int foo;\n" 20480 " int bar;\r\n" 20481 " int baz;\n" 20482 " int qux;\r\n" 20483 "};", 20484 style)); 20485 style.UseCRLF = true; 20486 EXPECT_EQ("union FooBarBazQux {\n" 20487 " int foo;\n" 20488 " int bar;\n" 20489 " int baz;\n" 20490 " int qux;\n" 20491 "};", 20492 format("union FooBarBazQux {\r\n" 20493 " int foo;\n" 20494 " int bar;\r\n" 20495 " int baz;\n" 20496 " int qux;\n" 20497 "};", 20498 style)); 20499 } 20500 20501 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 20502 verifyFormat("MY_CLASS(C) {\n" 20503 " int i;\n" 20504 " int j;\n" 20505 "};"); 20506 } 20507 20508 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 20509 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 20510 TwoIndent.ContinuationIndentWidth = 2; 20511 20512 EXPECT_EQ("int i =\n" 20513 " longFunction(\n" 20514 " arg);", 20515 format("int i = longFunction(arg);", TwoIndent)); 20516 20517 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 20518 SixIndent.ContinuationIndentWidth = 6; 20519 20520 EXPECT_EQ("int i =\n" 20521 " longFunction(\n" 20522 " arg);", 20523 format("int i = longFunction(arg);", SixIndent)); 20524 } 20525 20526 TEST_F(FormatTest, WrappedClosingParenthesisIndent) { 20527 FormatStyle Style = getLLVMStyle(); 20528 verifyFormat("int Foo::getter(\n" 20529 " //\n" 20530 ") const {\n" 20531 " return foo;\n" 20532 "}", 20533 Style); 20534 verifyFormat("void Foo::setter(\n" 20535 " //\n" 20536 ") {\n" 20537 " foo = 1;\n" 20538 "}", 20539 Style); 20540 } 20541 20542 TEST_F(FormatTest, SpacesInAngles) { 20543 FormatStyle Spaces = getLLVMStyle(); 20544 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 20545 20546 verifyFormat("vector< ::std::string > x1;", Spaces); 20547 verifyFormat("Foo< int, Bar > x2;", Spaces); 20548 verifyFormat("Foo< ::int, ::Bar > x3;", Spaces); 20549 20550 verifyFormat("static_cast< int >(arg);", Spaces); 20551 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 20552 verifyFormat("f< int, float >();", Spaces); 20553 verifyFormat("template <> g() {}", Spaces); 20554 verifyFormat("template < std::vector< int > > f() {}", Spaces); 20555 verifyFormat("std::function< void(int, int) > fct;", Spaces); 20556 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 20557 Spaces); 20558 20559 Spaces.Standard = FormatStyle::LS_Cpp03; 20560 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 20561 verifyFormat("A< A< int > >();", Spaces); 20562 20563 Spaces.SpacesInAngles = FormatStyle::SIAS_Never; 20564 verifyFormat("A<A<int> >();", Spaces); 20565 20566 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave; 20567 verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;", 20568 Spaces); 20569 verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;", 20570 Spaces); 20571 20572 verifyFormat("A<A<int> >();", Spaces); 20573 verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces); 20574 verifyFormat("A< A< int > >();", Spaces); 20575 20576 Spaces.Standard = FormatStyle::LS_Cpp11; 20577 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 20578 verifyFormat("A< A< int > >();", Spaces); 20579 20580 Spaces.SpacesInAngles = FormatStyle::SIAS_Never; 20581 verifyFormat("vector<::std::string> x4;", Spaces); 20582 verifyFormat("vector<int> x5;", Spaces); 20583 verifyFormat("Foo<int, Bar> x6;", Spaces); 20584 verifyFormat("Foo<::int, ::Bar> x7;", Spaces); 20585 20586 verifyFormat("A<A<int>>();", Spaces); 20587 20588 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave; 20589 verifyFormat("vector<::std::string> x4;", Spaces); 20590 verifyFormat("vector< ::std::string > x4;", Spaces); 20591 verifyFormat("vector<int> x5;", Spaces); 20592 verifyFormat("vector< int > x5;", Spaces); 20593 verifyFormat("Foo<int, Bar> x6;", Spaces); 20594 verifyFormat("Foo< int, Bar > x6;", Spaces); 20595 verifyFormat("Foo<::int, ::Bar> x7;", Spaces); 20596 verifyFormat("Foo< ::int, ::Bar > x7;", Spaces); 20597 20598 verifyFormat("A<A<int>>();", Spaces); 20599 verifyFormat("A< A< int > >();", Spaces); 20600 verifyFormat("A<A<int > >();", Spaces); 20601 verifyFormat("A< A< int>>();", Spaces); 20602 } 20603 20604 TEST_F(FormatTest, SpaceAfterTemplateKeyword) { 20605 FormatStyle Style = getLLVMStyle(); 20606 Style.SpaceAfterTemplateKeyword = false; 20607 verifyFormat("template<int> void foo();", Style); 20608 } 20609 20610 TEST_F(FormatTest, TripleAngleBrackets) { 20611 verifyFormat("f<<<1, 1>>>();"); 20612 verifyFormat("f<<<1, 1, 1, s>>>();"); 20613 verifyFormat("f<<<a, b, c, d>>>();"); 20614 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 20615 verifyFormat("f<param><<<1, 1>>>();"); 20616 verifyFormat("f<1><<<1, 1>>>();"); 20617 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 20618 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 20619 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 20620 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n" 20621 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();"); 20622 } 20623 20624 TEST_F(FormatTest, MergeLessLessAtEnd) { 20625 verifyFormat("<<"); 20626 EXPECT_EQ("< < <", format("\\\n<<<")); 20627 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 20628 "aaallvm::outs() <<"); 20629 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 20630 "aaaallvm::outs()\n <<"); 20631 } 20632 20633 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 20634 std::string code = "#if A\n" 20635 "#if B\n" 20636 "a.\n" 20637 "#endif\n" 20638 " a = 1;\n" 20639 "#else\n" 20640 "#endif\n" 20641 "#if C\n" 20642 "#else\n" 20643 "#endif\n"; 20644 EXPECT_EQ(code, format(code)); 20645 } 20646 20647 TEST_F(FormatTest, HandleConflictMarkers) { 20648 // Git/SVN conflict markers. 20649 EXPECT_EQ("int a;\n" 20650 "void f() {\n" 20651 " callme(some(parameter1,\n" 20652 "<<<<<<< text by the vcs\n" 20653 " parameter2),\n" 20654 "||||||| text by the vcs\n" 20655 " parameter2),\n" 20656 " parameter3,\n" 20657 "======= text by the vcs\n" 20658 " parameter2, parameter3),\n" 20659 ">>>>>>> text by the vcs\n" 20660 " otherparameter);\n", 20661 format("int a;\n" 20662 "void f() {\n" 20663 " callme(some(parameter1,\n" 20664 "<<<<<<< text by the vcs\n" 20665 " parameter2),\n" 20666 "||||||| text by the vcs\n" 20667 " parameter2),\n" 20668 " parameter3,\n" 20669 "======= text by the vcs\n" 20670 " parameter2,\n" 20671 " parameter3),\n" 20672 ">>>>>>> text by the vcs\n" 20673 " otherparameter);\n")); 20674 20675 // Perforce markers. 20676 EXPECT_EQ("void f() {\n" 20677 " function(\n" 20678 ">>>> text by the vcs\n" 20679 " parameter,\n" 20680 "==== text by the vcs\n" 20681 " parameter,\n" 20682 "==== text by the vcs\n" 20683 " parameter,\n" 20684 "<<<< text by the vcs\n" 20685 " parameter);\n", 20686 format("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 20697 EXPECT_EQ("<<<<<<<\n" 20698 "|||||||\n" 20699 "=======\n" 20700 ">>>>>>>", 20701 format("<<<<<<<\n" 20702 "|||||||\n" 20703 "=======\n" 20704 ">>>>>>>")); 20705 20706 EXPECT_EQ("<<<<<<<\n" 20707 "|||||||\n" 20708 "int i;\n" 20709 "=======\n" 20710 ">>>>>>>", 20711 format("<<<<<<<\n" 20712 "|||||||\n" 20713 "int i;\n" 20714 "=======\n" 20715 ">>>>>>>")); 20716 20717 // FIXME: Handle parsing of macros around conflict markers correctly: 20718 EXPECT_EQ("#define Macro \\\n" 20719 "<<<<<<<\n" 20720 "Something \\\n" 20721 "|||||||\n" 20722 "Else \\\n" 20723 "=======\n" 20724 "Other \\\n" 20725 ">>>>>>>\n" 20726 " End int i;\n", 20727 format("#define Macro \\\n" 20728 "<<<<<<<\n" 20729 " Something \\\n" 20730 "|||||||\n" 20731 " Else \\\n" 20732 "=======\n" 20733 " Other \\\n" 20734 ">>>>>>>\n" 20735 " End\n" 20736 "int i;\n")); 20737 } 20738 20739 TEST_F(FormatTest, DisableRegions) { 20740 EXPECT_EQ("int i;\n" 20741 "// clang-format off\n" 20742 " int j;\n" 20743 "// clang-format on\n" 20744 "int k;", 20745 format(" int i;\n" 20746 " // clang-format off\n" 20747 " int j;\n" 20748 " // clang-format on\n" 20749 " int k;")); 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 20761 // Don't reflow comments within disabled regions. 20762 EXPECT_EQ("// clang-format off\n" 20763 "// long long long long long long line\n" 20764 "/* clang-format on */\n" 20765 "/* long long long\n" 20766 " * long long long\n" 20767 " * line */\n" 20768 "int i;\n" 20769 "/* clang-format off */\n" 20770 "/* long long long long long long line */\n", 20771 format("// clang-format off\n" 20772 "// long long long long long long line\n" 20773 "/* clang-format on */\n" 20774 "/* long long long long long long line */\n" 20775 "int i;\n" 20776 "/* clang-format off */\n" 20777 "/* long long long long long long line */\n", 20778 getLLVMStyleWithColumns(20))); 20779 } 20780 20781 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 20782 format("? ) ="); 20783 verifyNoCrash("#define a\\\n /**/}"); 20784 } 20785 20786 TEST_F(FormatTest, FormatsTableGenCode) { 20787 FormatStyle Style = getLLVMStyle(); 20788 Style.Language = FormatStyle::LK_TableGen; 20789 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 20790 } 20791 20792 TEST_F(FormatTest, ArrayOfTemplates) { 20793 EXPECT_EQ("auto a = new unique_ptr<int>[10];", 20794 format("auto a = new unique_ptr<int > [ 10];")); 20795 20796 FormatStyle Spaces = getLLVMStyle(); 20797 Spaces.SpacesInSquareBrackets = true; 20798 EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];", 20799 format("auto a = new unique_ptr<int > [10];", Spaces)); 20800 } 20801 20802 TEST_F(FormatTest, ArrayAsTemplateType) { 20803 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;", 20804 format("auto a = unique_ptr < Foo < Bar>[ 10]> ;")); 20805 20806 FormatStyle Spaces = getLLVMStyle(); 20807 Spaces.SpacesInSquareBrackets = true; 20808 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;", 20809 format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces)); 20810 } 20811 20812 TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); } 20813 20814 TEST(FormatStyle, GetStyleWithEmptyFileName) { 20815 llvm::vfs::InMemoryFileSystem FS; 20816 auto Style1 = getStyle("file", "", "Google", "", &FS); 20817 ASSERT_TRUE((bool)Style1); 20818 ASSERT_EQ(*Style1, getGoogleStyle()); 20819 } 20820 20821 TEST(FormatStyle, GetStyleOfFile) { 20822 llvm::vfs::InMemoryFileSystem FS; 20823 // Test 1: format file in the same directory. 20824 ASSERT_TRUE( 20825 FS.addFile("/a/.clang-format", 0, 20826 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 20827 ASSERT_TRUE( 20828 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 20829 auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS); 20830 ASSERT_TRUE((bool)Style1); 20831 ASSERT_EQ(*Style1, getLLVMStyle()); 20832 20833 // Test 2.1: fallback to default. 20834 ASSERT_TRUE( 20835 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 20836 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS); 20837 ASSERT_TRUE((bool)Style2); 20838 ASSERT_EQ(*Style2, getMozillaStyle()); 20839 20840 // Test 2.2: no format on 'none' fallback style. 20841 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 20842 ASSERT_TRUE((bool)Style2); 20843 ASSERT_EQ(*Style2, getNoStyle()); 20844 20845 // Test 2.3: format if config is found with no based style while fallback is 20846 // 'none'. 20847 ASSERT_TRUE(FS.addFile("/b/.clang-format", 0, 20848 llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2"))); 20849 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 20850 ASSERT_TRUE((bool)Style2); 20851 ASSERT_EQ(*Style2, getLLVMStyle()); 20852 20853 // Test 2.4: format if yaml with no based style, while fallback is 'none'. 20854 Style2 = getStyle("{}", "a.h", "none", "", &FS); 20855 ASSERT_TRUE((bool)Style2); 20856 ASSERT_EQ(*Style2, getLLVMStyle()); 20857 20858 // Test 3: format file in parent directory. 20859 ASSERT_TRUE( 20860 FS.addFile("/c/.clang-format", 0, 20861 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 20862 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, 20863 llvm::MemoryBuffer::getMemBuffer("int i;"))); 20864 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS); 20865 ASSERT_TRUE((bool)Style3); 20866 ASSERT_EQ(*Style3, getGoogleStyle()); 20867 20868 // Test 4: error on invalid fallback style 20869 auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS); 20870 ASSERT_FALSE((bool)Style4); 20871 llvm::consumeError(Style4.takeError()); 20872 20873 // Test 5: error on invalid yaml on command line 20874 auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS); 20875 ASSERT_FALSE((bool)Style5); 20876 llvm::consumeError(Style5.takeError()); 20877 20878 // Test 6: error on invalid style 20879 auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS); 20880 ASSERT_FALSE((bool)Style6); 20881 llvm::consumeError(Style6.takeError()); 20882 20883 // Test 7: found config file, error on parsing it 20884 ASSERT_TRUE( 20885 FS.addFile("/d/.clang-format", 0, 20886 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n" 20887 "InvalidKey: InvalidValue"))); 20888 ASSERT_TRUE( 20889 FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 20890 auto Style7a = getStyle("file", "/d/.clang-format", "LLVM", "", &FS); 20891 ASSERT_FALSE((bool)Style7a); 20892 llvm::consumeError(Style7a.takeError()); 20893 20894 auto Style7b = getStyle("file", "/d/.clang-format", "LLVM", "", &FS, true); 20895 ASSERT_TRUE((bool)Style7b); 20896 20897 // Test 8: inferred per-language defaults apply. 20898 auto StyleTd = getStyle("file", "x.td", "llvm", "", &FS); 20899 ASSERT_TRUE((bool)StyleTd); 20900 ASSERT_EQ(*StyleTd, getLLVMStyle(FormatStyle::LK_TableGen)); 20901 20902 // Test 9.1: overwriting a file style, when parent no file exists with no 20903 // fallback style 20904 ASSERT_TRUE(FS.addFile( 20905 "/e/sub/.clang-format", 0, 20906 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: InheritParentConfig\n" 20907 "ColumnLimit: 20"))); 20908 ASSERT_TRUE(FS.addFile("/e/sub/code.cpp", 0, 20909 llvm::MemoryBuffer::getMemBuffer("int i;"))); 20910 auto Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS); 20911 ASSERT_TRUE(static_cast<bool>(Style9)); 20912 ASSERT_EQ(*Style9, [] { 20913 auto Style = getNoStyle(); 20914 Style.ColumnLimit = 20; 20915 return Style; 20916 }()); 20917 20918 // Test 9.2: with LLVM fallback style 20919 Style9 = getStyle("file", "/e/sub/code.cpp", "LLVM", "", &FS); 20920 ASSERT_TRUE(static_cast<bool>(Style9)); 20921 ASSERT_EQ(*Style9, [] { 20922 auto Style = getLLVMStyle(); 20923 Style.ColumnLimit = 20; 20924 return Style; 20925 }()); 20926 20927 // Test 9.3: with a parent file 20928 ASSERT_TRUE( 20929 FS.addFile("/e/.clang-format", 0, 20930 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google\n" 20931 "UseTab: Always"))); 20932 Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS); 20933 ASSERT_TRUE(static_cast<bool>(Style9)); 20934 ASSERT_EQ(*Style9, [] { 20935 auto Style = getGoogleStyle(); 20936 Style.ColumnLimit = 20; 20937 Style.UseTab = FormatStyle::UT_Always; 20938 return Style; 20939 }()); 20940 20941 // Test 9.4: propagate more than one level 20942 ASSERT_TRUE(FS.addFile("/e/sub/sub/code.cpp", 0, 20943 llvm::MemoryBuffer::getMemBuffer("int i;"))); 20944 ASSERT_TRUE(FS.addFile("/e/sub/sub/.clang-format", 0, 20945 llvm::MemoryBuffer::getMemBuffer( 20946 "BasedOnStyle: InheritParentConfig\n" 20947 "WhitespaceSensitiveMacros: ['FOO', 'BAR']"))); 20948 std::vector<std::string> NonDefaultWhiteSpaceMacros{"FOO", "BAR"}; 20949 20950 const auto SubSubStyle = [&NonDefaultWhiteSpaceMacros] { 20951 auto Style = getGoogleStyle(); 20952 Style.ColumnLimit = 20; 20953 Style.UseTab = FormatStyle::UT_Always; 20954 Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros; 20955 return Style; 20956 }(); 20957 20958 ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros); 20959 Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS); 20960 ASSERT_TRUE(static_cast<bool>(Style9)); 20961 ASSERT_EQ(*Style9, SubSubStyle); 20962 20963 // Test 9.5: use InheritParentConfig as style name 20964 Style9 = 20965 getStyle("inheritparentconfig", "/e/sub/sub/code.cpp", "none", "", &FS); 20966 ASSERT_TRUE(static_cast<bool>(Style9)); 20967 ASSERT_EQ(*Style9, SubSubStyle); 20968 20969 // Test 9.6: use command line style with inheritance 20970 Style9 = getStyle("{BasedOnStyle: InheritParentConfig}", "/e/sub/code.cpp", 20971 "none", "", &FS); 20972 ASSERT_TRUE(static_cast<bool>(Style9)); 20973 ASSERT_EQ(*Style9, SubSubStyle); 20974 20975 // Test 9.7: use command line style with inheritance and own config 20976 Style9 = getStyle("{BasedOnStyle: InheritParentConfig, " 20977 "WhitespaceSensitiveMacros: ['FOO', 'BAR']}", 20978 "/e/sub/code.cpp", "none", "", &FS); 20979 ASSERT_TRUE(static_cast<bool>(Style9)); 20980 ASSERT_EQ(*Style9, SubSubStyle); 20981 20982 // Test 9.8: use inheritance from a file without BasedOnStyle 20983 ASSERT_TRUE(FS.addFile("/e/withoutbase/.clang-format", 0, 20984 llvm::MemoryBuffer::getMemBuffer("ColumnLimit: 123"))); 20985 ASSERT_TRUE( 20986 FS.addFile("/e/withoutbase/sub/.clang-format", 0, 20987 llvm::MemoryBuffer::getMemBuffer( 20988 "BasedOnStyle: InheritParentConfig\nIndentWidth: 7"))); 20989 // Make sure we do not use the fallback style 20990 Style9 = getStyle("file", "/e/withoutbase/code.cpp", "google", "", &FS); 20991 ASSERT_TRUE(static_cast<bool>(Style9)); 20992 ASSERT_EQ(*Style9, [] { 20993 auto Style = getLLVMStyle(); 20994 Style.ColumnLimit = 123; 20995 return Style; 20996 }()); 20997 20998 Style9 = getStyle("file", "/e/withoutbase/sub/code.cpp", "google", "", &FS); 20999 ASSERT_TRUE(static_cast<bool>(Style9)); 21000 ASSERT_EQ(*Style9, [] { 21001 auto Style = getLLVMStyle(); 21002 Style.ColumnLimit = 123; 21003 Style.IndentWidth = 7; 21004 return Style; 21005 }()); 21006 } 21007 21008 TEST_F(ReplacementTest, FormatCodeAfterReplacements) { 21009 // Column limit is 20. 21010 std::string Code = "Type *a =\n" 21011 " new Type();\n" 21012 "g(iiiii, 0, jjjjj,\n" 21013 " 0, kkkkk, 0, mm);\n" 21014 "int bad = format ;"; 21015 std::string Expected = "auto a = new Type();\n" 21016 "g(iiiii, nullptr,\n" 21017 " jjjjj, nullptr,\n" 21018 " kkkkk, nullptr,\n" 21019 " mm);\n" 21020 "int bad = format ;"; 21021 FileID ID = Context.createInMemoryFile("format.cpp", Code); 21022 tooling::Replacements Replaces = toReplacements( 21023 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6, 21024 "auto "), 21025 tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1, 21026 "nullptr"), 21027 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1, 21028 "nullptr"), 21029 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1, 21030 "nullptr")}); 21031 21032 format::FormatStyle Style = format::getLLVMStyle(); 21033 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. 21034 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 21035 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 21036 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 21037 auto Result = applyAllReplacements(Code, *FormattedReplaces); 21038 EXPECT_TRUE(static_cast<bool>(Result)); 21039 EXPECT_EQ(Expected, *Result); 21040 } 21041 21042 TEST_F(ReplacementTest, SortIncludesAfterReplacement) { 21043 std::string Code = "#include \"a.h\"\n" 21044 "#include \"c.h\"\n" 21045 "\n" 21046 "int main() {\n" 21047 " return 0;\n" 21048 "}"; 21049 std::string Expected = "#include \"a.h\"\n" 21050 "#include \"b.h\"\n" 21051 "#include \"c.h\"\n" 21052 "\n" 21053 "int main() {\n" 21054 " return 0;\n" 21055 "}"; 21056 FileID ID = Context.createInMemoryFile("fix.cpp", Code); 21057 tooling::Replacements Replaces = toReplacements( 21058 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0, 21059 "#include \"b.h\"\n")}); 21060 21061 format::FormatStyle Style = format::getLLVMStyle(); 21062 Style.SortIncludes = FormatStyle::SI_CaseSensitive; 21063 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 21064 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 21065 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 21066 auto Result = applyAllReplacements(Code, *FormattedReplaces); 21067 EXPECT_TRUE(static_cast<bool>(Result)); 21068 EXPECT_EQ(Expected, *Result); 21069 } 21070 21071 TEST_F(FormatTest, FormatSortsUsingDeclarations) { 21072 EXPECT_EQ("using std::cin;\n" 21073 "using std::cout;", 21074 format("using std::cout;\n" 21075 "using std::cin;", 21076 getGoogleStyle())); 21077 } 21078 21079 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) { 21080 format::FormatStyle Style = format::getLLVMStyle(); 21081 Style.Standard = FormatStyle::LS_Cpp03; 21082 // cpp03 recognize this string as identifier u8 and literal character 'a' 21083 EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style)); 21084 } 21085 21086 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) { 21087 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers 21088 // all modes, including C++11, C++14 and C++17 21089 EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';")); 21090 } 21091 21092 TEST_F(FormatTest, DoNotFormatLikelyXml) { 21093 EXPECT_EQ("<!-- ;> -->", format("<!-- ;> -->", getGoogleStyle())); 21094 EXPECT_EQ(" <!-- >; -->", format(" <!-- >; -->", getGoogleStyle())); 21095 } 21096 21097 TEST_F(FormatTest, StructuredBindings) { 21098 // Structured bindings is a C++17 feature. 21099 // all modes, including C++11, C++14 and C++17 21100 verifyFormat("auto [a, b] = f();"); 21101 EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();")); 21102 EXPECT_EQ("const auto [a, b] = f();", format("const auto[a, b] = f();")); 21103 EXPECT_EQ("auto const [a, b] = f();", format("auto const[a, b] = f();")); 21104 EXPECT_EQ("auto const volatile [a, b] = f();", 21105 format("auto const volatile[a, b] = f();")); 21106 EXPECT_EQ("auto [a, b, c] = f();", format("auto [ a , b,c ] = f();")); 21107 EXPECT_EQ("auto &[a, b, c] = f();", 21108 format("auto &[ a , b,c ] = f();")); 21109 EXPECT_EQ("auto &&[a, b, c] = f();", 21110 format("auto &&[ a , b,c ] = f();")); 21111 EXPECT_EQ("auto const &[a, b] = f();", format("auto const&[a, b] = f();")); 21112 EXPECT_EQ("auto const volatile &&[a, b] = f();", 21113 format("auto const volatile &&[a, b] = f();")); 21114 EXPECT_EQ("auto const &&[a, b] = f();", 21115 format("auto const && [a, b] = f();")); 21116 EXPECT_EQ("const auto &[a, b] = f();", 21117 format("const auto & [a, b] = f();")); 21118 EXPECT_EQ("const auto volatile &&[a, b] = f();", 21119 format("const auto volatile &&[a, b] = f();")); 21120 EXPECT_EQ("volatile const auto &&[a, b] = f();", 21121 format("volatile const auto &&[a, b] = f();")); 21122 EXPECT_EQ("const auto &&[a, b] = f();", 21123 format("const auto && [a, b] = f();")); 21124 21125 // Make sure we don't mistake structured bindings for lambdas. 21126 FormatStyle PointerMiddle = getLLVMStyle(); 21127 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 21128 verifyFormat("auto [a1, b]{A * i};", getGoogleStyle()); 21129 verifyFormat("auto [a2, b]{A * i};", getLLVMStyle()); 21130 verifyFormat("auto [a3, b]{A * i};", PointerMiddle); 21131 verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle()); 21132 verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle()); 21133 verifyFormat("auto const [a3, b]{A * i};", PointerMiddle); 21134 verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle()); 21135 verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle()); 21136 verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle); 21137 verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle()); 21138 verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle()); 21139 verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle); 21140 21141 EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}", 21142 format("for (const auto && [a, b] : some_range) {\n}")); 21143 EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}", 21144 format("for (const auto & [a, b] : some_range) {\n}")); 21145 EXPECT_EQ("for (const auto [a, b] : some_range) {\n}", 21146 format("for (const auto[a, b] : some_range) {\n}")); 21147 EXPECT_EQ("auto [x, y](expr);", format("auto[x,y] (expr);")); 21148 EXPECT_EQ("auto &[x, y](expr);", format("auto & [x,y] (expr);")); 21149 EXPECT_EQ("auto &&[x, y](expr);", format("auto && [x,y] (expr);")); 21150 EXPECT_EQ("auto const &[x, y](expr);", 21151 format("auto const & [x,y] (expr);")); 21152 EXPECT_EQ("auto const &&[x, y](expr);", 21153 format("auto const && [x,y] (expr);")); 21154 EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y] {expr};")); 21155 EXPECT_EQ("auto const &[x, y]{expr};", 21156 format("auto const & [x,y] {expr};")); 21157 EXPECT_EQ("auto const &&[x, y]{expr};", 21158 format("auto const && [x,y] {expr};")); 21159 21160 format::FormatStyle Spaces = format::getLLVMStyle(); 21161 Spaces.SpacesInSquareBrackets = true; 21162 verifyFormat("auto [ a, b ] = f();", Spaces); 21163 verifyFormat("auto &&[ a, b ] = f();", Spaces); 21164 verifyFormat("auto &[ a, b ] = f();", Spaces); 21165 verifyFormat("auto const &&[ a, b ] = f();", Spaces); 21166 verifyFormat("auto const &[ a, b ] = f();", Spaces); 21167 } 21168 21169 TEST_F(FormatTest, FileAndCode) { 21170 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", "")); 21171 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", "")); 21172 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", "")); 21173 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "")); 21174 EXPECT_EQ(FormatStyle::LK_ObjC, 21175 guessLanguage("foo.h", "@interface Foo\n@end\n")); 21176 EXPECT_EQ( 21177 FormatStyle::LK_ObjC, 21178 guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }")); 21179 EXPECT_EQ(FormatStyle::LK_ObjC, 21180 guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))")); 21181 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;")); 21182 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", "")); 21183 EXPECT_EQ(FormatStyle::LK_ObjC, 21184 guessLanguage("foo", "@interface Foo\n@end\n")); 21185 EXPECT_EQ(FormatStyle::LK_ObjC, 21186 guessLanguage("foo.h", "int DoStuff(CGRect rect);\n")); 21187 EXPECT_EQ( 21188 FormatStyle::LK_ObjC, 21189 guessLanguage("foo.h", 21190 "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n")); 21191 EXPECT_EQ( 21192 FormatStyle::LK_Cpp, 21193 guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;")); 21194 } 21195 21196 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) { 21197 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];")); 21198 EXPECT_EQ(FormatStyle::LK_ObjC, 21199 guessLanguage("foo.h", "array[[calculator getIndex]];")); 21200 EXPECT_EQ(FormatStyle::LK_Cpp, 21201 guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];")); 21202 EXPECT_EQ( 21203 FormatStyle::LK_Cpp, 21204 guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];")); 21205 EXPECT_EQ(FormatStyle::LK_ObjC, 21206 guessLanguage("foo.h", "[[noreturn foo] bar];")); 21207 EXPECT_EQ(FormatStyle::LK_Cpp, 21208 guessLanguage("foo.h", "[[clang::fallthrough]];")); 21209 EXPECT_EQ(FormatStyle::LK_ObjC, 21210 guessLanguage("foo.h", "[[clang:fallthrough] foo];")); 21211 EXPECT_EQ(FormatStyle::LK_Cpp, 21212 guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];")); 21213 EXPECT_EQ(FormatStyle::LK_Cpp, 21214 guessLanguage("foo.h", "[[using clang: fallthrough]];")); 21215 EXPECT_EQ(FormatStyle::LK_ObjC, 21216 guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];")); 21217 EXPECT_EQ(FormatStyle::LK_Cpp, 21218 guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];")); 21219 EXPECT_EQ( 21220 FormatStyle::LK_Cpp, 21221 guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)")); 21222 EXPECT_EQ( 21223 FormatStyle::LK_Cpp, 21224 guessLanguage("foo.h", 21225 "[[clang::callable_when(\"unconsumed\", \"unknown\")]]")); 21226 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]")); 21227 } 21228 21229 TEST_F(FormatTest, GuessLanguageWithCaret) { 21230 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);")); 21231 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);")); 21232 EXPECT_EQ(FormatStyle::LK_ObjC, 21233 guessLanguage("foo.h", "int(^)(char, float);")); 21234 EXPECT_EQ(FormatStyle::LK_ObjC, 21235 guessLanguage("foo.h", "int(^foo)(char, float);")); 21236 EXPECT_EQ(FormatStyle::LK_ObjC, 21237 guessLanguage("foo.h", "int(^foo[10])(char, float);")); 21238 EXPECT_EQ(FormatStyle::LK_ObjC, 21239 guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);")); 21240 EXPECT_EQ( 21241 FormatStyle::LK_ObjC, 21242 guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);")); 21243 } 21244 21245 TEST_F(FormatTest, GuessLanguageWithPragmas) { 21246 EXPECT_EQ(FormatStyle::LK_Cpp, 21247 guessLanguage("foo.h", "__pragma(warning(disable:))")); 21248 EXPECT_EQ(FormatStyle::LK_Cpp, 21249 guessLanguage("foo.h", "#pragma(warning(disable:))")); 21250 EXPECT_EQ(FormatStyle::LK_Cpp, 21251 guessLanguage("foo.h", "_Pragma(warning(disable:))")); 21252 } 21253 21254 TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) { 21255 // ASM symbolic names are identifiers that must be surrounded by [] without 21256 // space in between: 21257 // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands 21258 21259 // Example from https://bugs.llvm.org/show_bug.cgi?id=45108. 21260 verifyFormat(R"(// 21261 asm volatile("mrs %x[result], FPCR" : [result] "=r"(result)); 21262 )"); 21263 21264 // A list of several ASM symbolic names. 21265 verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)"); 21266 21267 // ASM symbolic names in inline ASM with inputs and outputs. 21268 verifyFormat(R"(// 21269 asm("cmoveq %1, %2, %[result]" 21270 : [result] "=r"(result) 21271 : "r"(test), "r"(new), "[result]"(old)); 21272 )"); 21273 21274 // ASM symbolic names in inline ASM with no outputs. 21275 verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)"); 21276 } 21277 21278 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) { 21279 EXPECT_EQ(FormatStyle::LK_Cpp, 21280 guessLanguage("foo.h", "void f() {\n" 21281 " asm (\"mov %[e], %[d]\"\n" 21282 " : [d] \"=rm\" (d)\n" 21283 " [e] \"rm\" (*e));\n" 21284 "}")); 21285 EXPECT_EQ(FormatStyle::LK_Cpp, 21286 guessLanguage("foo.h", "void f() {\n" 21287 " _asm (\"mov %[e], %[d]\"\n" 21288 " : [d] \"=rm\" (d)\n" 21289 " [e] \"rm\" (*e));\n" 21290 "}")); 21291 EXPECT_EQ(FormatStyle::LK_Cpp, 21292 guessLanguage("foo.h", "void f() {\n" 21293 " __asm (\"mov %[e], %[d]\"\n" 21294 " : [d] \"=rm\" (d)\n" 21295 " [e] \"rm\" (*e));\n" 21296 "}")); 21297 EXPECT_EQ(FormatStyle::LK_Cpp, 21298 guessLanguage("foo.h", "void f() {\n" 21299 " __asm__ (\"mov %[e], %[d]\"\n" 21300 " : [d] \"=rm\" (d)\n" 21301 " [e] \"rm\" (*e));\n" 21302 "}")); 21303 EXPECT_EQ(FormatStyle::LK_Cpp, 21304 guessLanguage("foo.h", "void f() {\n" 21305 " asm (\"mov %[e], %[d]\"\n" 21306 " : [d] \"=rm\" (d),\n" 21307 " [e] \"rm\" (*e));\n" 21308 "}")); 21309 EXPECT_EQ(FormatStyle::LK_Cpp, 21310 guessLanguage("foo.h", "void f() {\n" 21311 " asm volatile (\"mov %[e], %[d]\"\n" 21312 " : [d] \"=rm\" (d)\n" 21313 " [e] \"rm\" (*e));\n" 21314 "}")); 21315 } 21316 21317 TEST_F(FormatTest, GuessLanguageWithChildLines) { 21318 EXPECT_EQ(FormatStyle::LK_Cpp, 21319 guessLanguage("foo.h", "#define FOO ({ std::string s; })")); 21320 EXPECT_EQ(FormatStyle::LK_ObjC, 21321 guessLanguage("foo.h", "#define FOO ({ NSString *s; })")); 21322 EXPECT_EQ( 21323 FormatStyle::LK_Cpp, 21324 guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })")); 21325 EXPECT_EQ( 21326 FormatStyle::LK_ObjC, 21327 guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })")); 21328 } 21329 21330 TEST_F(FormatTest, TypenameMacros) { 21331 std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"}; 21332 21333 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353 21334 FormatStyle Google = getGoogleStyleWithColumns(0); 21335 Google.TypenameMacros = TypenameMacros; 21336 verifyFormat("struct foo {\n" 21337 " int bar;\n" 21338 " TAILQ_ENTRY(a) bleh;\n" 21339 "};", 21340 Google); 21341 21342 FormatStyle Macros = getLLVMStyle(); 21343 Macros.TypenameMacros = TypenameMacros; 21344 21345 verifyFormat("STACK_OF(int) a;", Macros); 21346 verifyFormat("STACK_OF(int) *a;", Macros); 21347 verifyFormat("STACK_OF(int const *) *a;", Macros); 21348 verifyFormat("STACK_OF(int *const) *a;", Macros); 21349 verifyFormat("STACK_OF(int, string) a;", Macros); 21350 verifyFormat("STACK_OF(LIST(int)) a;", Macros); 21351 verifyFormat("STACK_OF(LIST(int)) a, b;", Macros); 21352 verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros); 21353 verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros); 21354 verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros); 21355 verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros); 21356 21357 Macros.PointerAlignment = FormatStyle::PAS_Left; 21358 verifyFormat("STACK_OF(int)* a;", Macros); 21359 verifyFormat("STACK_OF(int*)* a;", Macros); 21360 verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros); 21361 verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros); 21362 verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros); 21363 } 21364 21365 TEST_F(FormatTest, AtomicQualifier) { 21366 // Check that we treate _Atomic as a type and not a function call 21367 FormatStyle Google = getGoogleStyleWithColumns(0); 21368 verifyFormat("struct foo {\n" 21369 " int a1;\n" 21370 " _Atomic(a) a2;\n" 21371 " _Atomic(_Atomic(int) *const) a3;\n" 21372 "};", 21373 Google); 21374 verifyFormat("_Atomic(uint64_t) a;"); 21375 verifyFormat("_Atomic(uint64_t) *a;"); 21376 verifyFormat("_Atomic(uint64_t const *) *a;"); 21377 verifyFormat("_Atomic(uint64_t *const) *a;"); 21378 verifyFormat("_Atomic(const uint64_t *) *a;"); 21379 verifyFormat("_Atomic(uint64_t) a;"); 21380 verifyFormat("_Atomic(_Atomic(uint64_t)) a;"); 21381 verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;"); 21382 verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}"); 21383 verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);"); 21384 21385 verifyFormat("_Atomic(uint64_t) *s(InitValue);"); 21386 verifyFormat("_Atomic(uint64_t) *s{InitValue};"); 21387 FormatStyle Style = getLLVMStyle(); 21388 Style.PointerAlignment = FormatStyle::PAS_Left; 21389 verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style); 21390 verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style); 21391 verifyFormat("_Atomic(int)* a;", Style); 21392 verifyFormat("_Atomic(int*)* a;", Style); 21393 verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style); 21394 21395 Style.SpacesInCStyleCastParentheses = true; 21396 Style.SpacesInParentheses = false; 21397 verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style); 21398 Style.SpacesInCStyleCastParentheses = false; 21399 Style.SpacesInParentheses = true; 21400 verifyFormat("x = (_Atomic( uint64_t ))*a;", Style); 21401 verifyFormat("x = (_Atomic( uint64_t ))&a;", Style); 21402 } 21403 21404 TEST_F(FormatTest, AmbersandInLamda) { 21405 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899 21406 FormatStyle AlignStyle = getLLVMStyle(); 21407 AlignStyle.PointerAlignment = FormatStyle::PAS_Left; 21408 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle); 21409 AlignStyle.PointerAlignment = FormatStyle::PAS_Right; 21410 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle); 21411 } 21412 21413 TEST_F(FormatTest, SpacesInConditionalStatement) { 21414 FormatStyle Spaces = getLLVMStyle(); 21415 Spaces.IfMacros.clear(); 21416 Spaces.IfMacros.push_back("MYIF"); 21417 Spaces.SpacesInConditionalStatement = true; 21418 verifyFormat("for ( int i = 0; i; i++ )\n continue;", Spaces); 21419 verifyFormat("if ( !a )\n return;", Spaces); 21420 verifyFormat("if ( a )\n return;", Spaces); 21421 verifyFormat("if constexpr ( a )\n return;", Spaces); 21422 verifyFormat("MYIF ( a )\n return;", Spaces); 21423 verifyFormat("MYIF ( a )\n return;\nelse MYIF ( b )\n return;", Spaces); 21424 verifyFormat("MYIF ( a )\n return;\nelse\n return;", Spaces); 21425 verifyFormat("switch ( a )\ncase 1:\n return;", Spaces); 21426 verifyFormat("while ( a )\n return;", Spaces); 21427 verifyFormat("while ( (a && b) )\n return;", Spaces); 21428 verifyFormat("do {\n} while ( 1 != 0 );", Spaces); 21429 verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces); 21430 // Check that space on the left of "::" is inserted as expected at beginning 21431 // of condition. 21432 verifyFormat("while ( ::func() )\n return;", Spaces); 21433 21434 // Check impact of ControlStatementsExceptControlMacros is honored. 21435 Spaces.SpaceBeforeParens = 21436 FormatStyle::SBPO_ControlStatementsExceptControlMacros; 21437 verifyFormat("MYIF( a )\n return;", Spaces); 21438 verifyFormat("MYIF( a )\n return;\nelse MYIF( b )\n return;", Spaces); 21439 verifyFormat("MYIF( a )\n return;\nelse\n return;", Spaces); 21440 } 21441 21442 TEST_F(FormatTest, AlternativeOperators) { 21443 // Test case for ensuring alternate operators are not 21444 // combined with their right most neighbour. 21445 verifyFormat("int a and b;"); 21446 verifyFormat("int a and_eq b;"); 21447 verifyFormat("int a bitand b;"); 21448 verifyFormat("int a bitor b;"); 21449 verifyFormat("int a compl b;"); 21450 verifyFormat("int a not b;"); 21451 verifyFormat("int a not_eq b;"); 21452 verifyFormat("int a or b;"); 21453 verifyFormat("int a xor b;"); 21454 verifyFormat("int a xor_eq b;"); 21455 verifyFormat("return this not_eq bitand other;"); 21456 verifyFormat("bool operator not_eq(const X bitand other)"); 21457 21458 verifyFormat("int a and 5;"); 21459 verifyFormat("int a and_eq 5;"); 21460 verifyFormat("int a bitand 5;"); 21461 verifyFormat("int a bitor 5;"); 21462 verifyFormat("int a compl 5;"); 21463 verifyFormat("int a not 5;"); 21464 verifyFormat("int a not_eq 5;"); 21465 verifyFormat("int a or 5;"); 21466 verifyFormat("int a xor 5;"); 21467 verifyFormat("int a xor_eq 5;"); 21468 21469 verifyFormat("int a compl(5);"); 21470 verifyFormat("int a not(5);"); 21471 21472 /* FIXME handle alternate tokens 21473 * https://en.cppreference.com/w/cpp/language/operator_alternative 21474 // alternative tokens 21475 verifyFormat("compl foo();"); // ~foo(); 21476 verifyFormat("foo() <%%>;"); // foo(); 21477 verifyFormat("void foo() <%%>;"); // void foo(){} 21478 verifyFormat("int a <:1:>;"); // int a[1];[ 21479 verifyFormat("%:define ABC abc"); // #define ABC abc 21480 verifyFormat("%:%:"); // ## 21481 */ 21482 } 21483 21484 TEST_F(FormatTest, STLWhileNotDefineChed) { 21485 verifyFormat("#if defined(while)\n" 21486 "#define while EMIT WARNING C4005\n" 21487 "#endif // while"); 21488 } 21489 21490 TEST_F(FormatTest, OperatorSpacing) { 21491 FormatStyle Style = getLLVMStyle(); 21492 Style.PointerAlignment = FormatStyle::PAS_Right; 21493 verifyFormat("Foo::operator*();", Style); 21494 verifyFormat("Foo::operator void *();", Style); 21495 verifyFormat("Foo::operator void **();", Style); 21496 verifyFormat("Foo::operator void *&();", Style); 21497 verifyFormat("Foo::operator void *&&();", Style); 21498 verifyFormat("Foo::operator void const *();", Style); 21499 verifyFormat("Foo::operator void const **();", Style); 21500 verifyFormat("Foo::operator void const *&();", Style); 21501 verifyFormat("Foo::operator void const *&&();", Style); 21502 verifyFormat("Foo::operator()(void *);", Style); 21503 verifyFormat("Foo::operator*(void *);", Style); 21504 verifyFormat("Foo::operator*();", Style); 21505 verifyFormat("Foo::operator**();", Style); 21506 verifyFormat("Foo::operator&();", Style); 21507 verifyFormat("Foo::operator<int> *();", Style); 21508 verifyFormat("Foo::operator<Foo> *();", Style); 21509 verifyFormat("Foo::operator<int> **();", Style); 21510 verifyFormat("Foo::operator<Foo> **();", Style); 21511 verifyFormat("Foo::operator<int> &();", Style); 21512 verifyFormat("Foo::operator<Foo> &();", Style); 21513 verifyFormat("Foo::operator<int> &&();", Style); 21514 verifyFormat("Foo::operator<Foo> &&();", Style); 21515 verifyFormat("Foo::operator<int> *&();", Style); 21516 verifyFormat("Foo::operator<Foo> *&();", Style); 21517 verifyFormat("Foo::operator<int> *&&();", Style); 21518 verifyFormat("Foo::operator<Foo> *&&();", Style); 21519 verifyFormat("operator*(int (*)(), class Foo);", Style); 21520 21521 verifyFormat("Foo::operator&();", Style); 21522 verifyFormat("Foo::operator void &();", Style); 21523 verifyFormat("Foo::operator void const &();", Style); 21524 verifyFormat("Foo::operator()(void &);", Style); 21525 verifyFormat("Foo::operator&(void &);", Style); 21526 verifyFormat("Foo::operator&();", Style); 21527 verifyFormat("operator&(int (&)(), class Foo);", Style); 21528 21529 verifyFormat("Foo::operator&&();", Style); 21530 verifyFormat("Foo::operator**();", Style); 21531 verifyFormat("Foo::operator void &&();", Style); 21532 verifyFormat("Foo::operator void const &&();", Style); 21533 verifyFormat("Foo::operator()(void &&);", Style); 21534 verifyFormat("Foo::operator&&(void &&);", Style); 21535 verifyFormat("Foo::operator&&();", Style); 21536 verifyFormat("operator&&(int(&&)(), class Foo);", Style); 21537 verifyFormat("operator const nsTArrayRight<E> &()", Style); 21538 verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()", 21539 Style); 21540 verifyFormat("operator void **()", Style); 21541 verifyFormat("operator const FooRight<Object> &()", Style); 21542 verifyFormat("operator const FooRight<Object> *()", Style); 21543 verifyFormat("operator const FooRight<Object> **()", Style); 21544 verifyFormat("operator const FooRight<Object> *&()", Style); 21545 verifyFormat("operator const FooRight<Object> *&&()", Style); 21546 21547 Style.PointerAlignment = FormatStyle::PAS_Left; 21548 verifyFormat("Foo::operator*();", Style); 21549 verifyFormat("Foo::operator**();", Style); 21550 verifyFormat("Foo::operator void*();", Style); 21551 verifyFormat("Foo::operator void**();", Style); 21552 verifyFormat("Foo::operator void*&();", Style); 21553 verifyFormat("Foo::operator void*&&();", Style); 21554 verifyFormat("Foo::operator void const*();", Style); 21555 verifyFormat("Foo::operator void const**();", Style); 21556 verifyFormat("Foo::operator void const*&();", Style); 21557 verifyFormat("Foo::operator void const*&&();", Style); 21558 verifyFormat("Foo::operator/*comment*/ void*();", Style); 21559 verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style); 21560 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style); 21561 verifyFormat("Foo::operator()(void*);", Style); 21562 verifyFormat("Foo::operator*(void*);", Style); 21563 verifyFormat("Foo::operator*();", Style); 21564 verifyFormat("Foo::operator<int>*();", Style); 21565 verifyFormat("Foo::operator<Foo>*();", Style); 21566 verifyFormat("Foo::operator<int>**();", Style); 21567 verifyFormat("Foo::operator<Foo>**();", Style); 21568 verifyFormat("Foo::operator<Foo>*&();", Style); 21569 verifyFormat("Foo::operator<int>&();", Style); 21570 verifyFormat("Foo::operator<Foo>&();", Style); 21571 verifyFormat("Foo::operator<int>&&();", Style); 21572 verifyFormat("Foo::operator<Foo>&&();", Style); 21573 verifyFormat("Foo::operator<int>*&();", Style); 21574 verifyFormat("Foo::operator<Foo>*&();", Style); 21575 verifyFormat("operator*(int (*)(), class Foo);", Style); 21576 21577 verifyFormat("Foo::operator&();", Style); 21578 verifyFormat("Foo::operator void&();", Style); 21579 verifyFormat("Foo::operator void const&();", Style); 21580 verifyFormat("Foo::operator/*comment*/ void&();", Style); 21581 verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style); 21582 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style); 21583 verifyFormat("Foo::operator()(void&);", Style); 21584 verifyFormat("Foo::operator&(void&);", Style); 21585 verifyFormat("Foo::operator&();", Style); 21586 verifyFormat("operator&(int (&)(), class Foo);", Style); 21587 21588 verifyFormat("Foo::operator&&();", Style); 21589 verifyFormat("Foo::operator void&&();", Style); 21590 verifyFormat("Foo::operator void const&&();", Style); 21591 verifyFormat("Foo::operator/*comment*/ void&&();", Style); 21592 verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style); 21593 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style); 21594 verifyFormat("Foo::operator()(void&&);", Style); 21595 verifyFormat("Foo::operator&&(void&&);", Style); 21596 verifyFormat("Foo::operator&&();", Style); 21597 verifyFormat("operator&&(int(&&)(), class Foo);", Style); 21598 verifyFormat("operator const nsTArrayLeft<E>&()", Style); 21599 verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()", 21600 Style); 21601 verifyFormat("operator void**()", Style); 21602 verifyFormat("operator const FooLeft<Object>&()", Style); 21603 verifyFormat("operator const FooLeft<Object>*()", Style); 21604 verifyFormat("operator const FooLeft<Object>**()", Style); 21605 verifyFormat("operator const FooLeft<Object>*&()", Style); 21606 verifyFormat("operator const FooLeft<Object>*&&()", Style); 21607 21608 // PR45107 21609 verifyFormat("operator Vector<String>&();", Style); 21610 verifyFormat("operator const Vector<String>&();", Style); 21611 verifyFormat("operator foo::Bar*();", Style); 21612 verifyFormat("operator const Foo<X>::Bar<Y>*();", Style); 21613 verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();", 21614 Style); 21615 21616 Style.PointerAlignment = FormatStyle::PAS_Middle; 21617 verifyFormat("Foo::operator*();", Style); 21618 verifyFormat("Foo::operator void *();", Style); 21619 verifyFormat("Foo::operator()(void *);", Style); 21620 verifyFormat("Foo::operator*(void *);", Style); 21621 verifyFormat("Foo::operator*();", Style); 21622 verifyFormat("operator*(int (*)(), class Foo);", Style); 21623 21624 verifyFormat("Foo::operator&();", Style); 21625 verifyFormat("Foo::operator void &();", Style); 21626 verifyFormat("Foo::operator void const &();", Style); 21627 verifyFormat("Foo::operator()(void &);", Style); 21628 verifyFormat("Foo::operator&(void &);", Style); 21629 verifyFormat("Foo::operator&();", Style); 21630 verifyFormat("operator&(int (&)(), class Foo);", Style); 21631 21632 verifyFormat("Foo::operator&&();", Style); 21633 verifyFormat("Foo::operator void &&();", Style); 21634 verifyFormat("Foo::operator void const &&();", Style); 21635 verifyFormat("Foo::operator()(void &&);", Style); 21636 verifyFormat("Foo::operator&&(void &&);", Style); 21637 verifyFormat("Foo::operator&&();", Style); 21638 verifyFormat("operator&&(int(&&)(), class Foo);", Style); 21639 } 21640 21641 TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) { 21642 FormatStyle Style = getLLVMStyle(); 21643 // PR46157 21644 verifyFormat("foo(operator+, -42);", Style); 21645 verifyFormat("foo(operator++, -42);", Style); 21646 verifyFormat("foo(operator--, -42);", Style); 21647 verifyFormat("foo(-42, operator--);", Style); 21648 verifyFormat("foo(-42, operator, );", Style); 21649 verifyFormat("foo(operator, , -42);", Style); 21650 } 21651 21652 TEST_F(FormatTest, WhitespaceSensitiveMacros) { 21653 FormatStyle Style = getLLVMStyle(); 21654 Style.WhitespaceSensitiveMacros.push_back("FOO"); 21655 21656 // Don't use the helpers here, since 'mess up' will change the whitespace 21657 // and these are all whitespace sensitive by definition 21658 EXPECT_EQ("FOO(String-ized&Messy+But(: :Still)=Intentional);", 21659 format("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style)); 21660 EXPECT_EQ( 21661 "FOO(String-ized&Messy+But\\(: :Still)=Intentional);", 21662 format("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style)); 21663 EXPECT_EQ("FOO(String-ized&Messy+But,: :Still=Intentional);", 21664 format("FOO(String-ized&Messy+But,: :Still=Intentional);", Style)); 21665 EXPECT_EQ("FOO(String-ized&Messy+But,: :\n" 21666 " Still=Intentional);", 21667 format("FOO(String-ized&Messy+But,: :\n" 21668 " Still=Intentional);", 21669 Style)); 21670 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 21671 EXPECT_EQ("FOO(String-ized=&Messy+But,: :\n" 21672 " Still=Intentional);", 21673 format("FOO(String-ized=&Messy+But,: :\n" 21674 " Still=Intentional);", 21675 Style)); 21676 21677 Style.ColumnLimit = 21; 21678 EXPECT_EQ("FOO(String-ized&Messy+But: :Still=Intentional);", 21679 format("FOO(String-ized&Messy+But: :Still=Intentional);", Style)); 21680 } 21681 21682 TEST_F(FormatTest, VeryLongNamespaceCommentSplit) { 21683 // These tests are not in NamespaceFixer because that doesn't 21684 // test its interaction with line wrapping 21685 FormatStyle Style = getLLVMStyle(); 21686 Style.ColumnLimit = 80; 21687 verifyFormat("namespace {\n" 21688 "int i;\n" 21689 "int j;\n" 21690 "} // namespace", 21691 Style); 21692 21693 verifyFormat("namespace AAA {\n" 21694 "int i;\n" 21695 "int j;\n" 21696 "} // namespace AAA", 21697 Style); 21698 21699 EXPECT_EQ("namespace Averyveryveryverylongnamespace {\n" 21700 "int i;\n" 21701 "int j;\n" 21702 "} // namespace Averyveryveryverylongnamespace", 21703 format("namespace Averyveryveryverylongnamespace {\n" 21704 "int i;\n" 21705 "int j;\n" 21706 "}", 21707 Style)); 21708 21709 EXPECT_EQ( 21710 "namespace " 21711 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" 21712 " went::mad::now {\n" 21713 "int i;\n" 21714 "int j;\n" 21715 "} // namespace\n" 21716 " // " 21717 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" 21718 "went::mad::now", 21719 format("namespace " 21720 "would::it::save::you::a::lot::of::time::if_::i::" 21721 "just::gave::up::and_::went::mad::now {\n" 21722 "int i;\n" 21723 "int j;\n" 21724 "}", 21725 Style)); 21726 21727 // This used to duplicate the comment again and again on subsequent runs 21728 EXPECT_EQ( 21729 "namespace " 21730 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" 21731 " went::mad::now {\n" 21732 "int i;\n" 21733 "int j;\n" 21734 "} // namespace\n" 21735 " // " 21736 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" 21737 "went::mad::now", 21738 format("namespace " 21739 "would::it::save::you::a::lot::of::time::if_::i::" 21740 "just::gave::up::and_::went::mad::now {\n" 21741 "int i;\n" 21742 "int j;\n" 21743 "} // namespace\n" 21744 " // " 21745 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::" 21746 "and_::went::mad::now", 21747 Style)); 21748 } 21749 21750 TEST_F(FormatTest, LikelyUnlikely) { 21751 FormatStyle Style = getLLVMStyle(); 21752 21753 verifyFormat("if (argc > 5) [[unlikely]] {\n" 21754 " return 29;\n" 21755 "}", 21756 Style); 21757 21758 verifyFormat("if (argc > 5) [[likely]] {\n" 21759 " return 29;\n" 21760 "}", 21761 Style); 21762 21763 verifyFormat("if (argc > 5) [[unlikely]] {\n" 21764 " return 29;\n" 21765 "} else [[likely]] {\n" 21766 " return 42;\n" 21767 "}\n", 21768 Style); 21769 21770 verifyFormat("if (argc > 5) [[unlikely]] {\n" 21771 " return 29;\n" 21772 "} else if (argc > 10) [[likely]] {\n" 21773 " return 99;\n" 21774 "} else {\n" 21775 " return 42;\n" 21776 "}\n", 21777 Style); 21778 21779 verifyFormat("if (argc > 5) [[gnu::unused]] {\n" 21780 " return 29;\n" 21781 "}", 21782 Style); 21783 } 21784 21785 TEST_F(FormatTest, PenaltyIndentedWhitespace) { 21786 verifyFormat("Constructor()\n" 21787 " : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 21788 " aaaa(aaaaaaaaaaaaaaaaaa, " 21789 "aaaaaaaaaaaaaaaaaat))"); 21790 verifyFormat("Constructor()\n" 21791 " : aaaaaaaaaaaaa(aaaaaa), " 21792 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)"); 21793 21794 FormatStyle StyleWithWhitespacePenalty = getLLVMStyle(); 21795 StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5; 21796 verifyFormat("Constructor()\n" 21797 " : aaaaaa(aaaaaa),\n" 21798 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 21799 " aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))", 21800 StyleWithWhitespacePenalty); 21801 verifyFormat("Constructor()\n" 21802 " : aaaaaaaaaaaaa(aaaaaa), " 21803 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)", 21804 StyleWithWhitespacePenalty); 21805 } 21806 21807 TEST_F(FormatTest, LLVMDefaultStyle) { 21808 FormatStyle Style = getLLVMStyle(); 21809 verifyFormat("extern \"C\" {\n" 21810 "int foo();\n" 21811 "}", 21812 Style); 21813 } 21814 TEST_F(FormatTest, GNUDefaultStyle) { 21815 FormatStyle Style = getGNUStyle(); 21816 verifyFormat("extern \"C\"\n" 21817 "{\n" 21818 " int foo ();\n" 21819 "}", 21820 Style); 21821 } 21822 TEST_F(FormatTest, MozillaDefaultStyle) { 21823 FormatStyle Style = getMozillaStyle(); 21824 verifyFormat("extern \"C\"\n" 21825 "{\n" 21826 " int foo();\n" 21827 "}", 21828 Style); 21829 } 21830 TEST_F(FormatTest, GoogleDefaultStyle) { 21831 FormatStyle Style = getGoogleStyle(); 21832 verifyFormat("extern \"C\" {\n" 21833 "int foo();\n" 21834 "}", 21835 Style); 21836 } 21837 TEST_F(FormatTest, ChromiumDefaultStyle) { 21838 FormatStyle Style = getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp); 21839 verifyFormat("extern \"C\" {\n" 21840 "int foo();\n" 21841 "}", 21842 Style); 21843 } 21844 TEST_F(FormatTest, MicrosoftDefaultStyle) { 21845 FormatStyle Style = getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp); 21846 verifyFormat("extern \"C\"\n" 21847 "{\n" 21848 " int foo();\n" 21849 "}", 21850 Style); 21851 } 21852 TEST_F(FormatTest, WebKitDefaultStyle) { 21853 FormatStyle Style = getWebKitStyle(); 21854 verifyFormat("extern \"C\" {\n" 21855 "int foo();\n" 21856 "}", 21857 Style); 21858 } 21859 21860 TEST_F(FormatTest, ConceptsAndRequires) { 21861 FormatStyle Style = getLLVMStyle(); 21862 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 21863 21864 verifyFormat("template <typename T>\n" 21865 "concept Hashable = requires(T a) {\n" 21866 " { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n" 21867 "};", 21868 Style); 21869 verifyFormat("template <typename T>\n" 21870 "concept EqualityComparable = requires(T a, T b) {\n" 21871 " { a == b } -> bool;\n" 21872 "};", 21873 Style); 21874 verifyFormat("template <typename T>\n" 21875 "concept EqualityComparable = requires(T a, T b) {\n" 21876 " { a == b } -> bool;\n" 21877 " { a != b } -> bool;\n" 21878 "};", 21879 Style); 21880 verifyFormat("template <typename T>\n" 21881 "concept EqualityComparable = requires(T a, T b) {\n" 21882 " { a == b } -> bool;\n" 21883 " { a != b } -> bool;\n" 21884 "};", 21885 Style); 21886 21887 verifyFormat("template <typename It>\n" 21888 "requires Iterator<It>\n" 21889 "void sort(It begin, It end) {\n" 21890 " //....\n" 21891 "}", 21892 Style); 21893 21894 verifyFormat("template <typename T>\n" 21895 "concept Large = sizeof(T) > 10;", 21896 Style); 21897 21898 verifyFormat("template <typename T, typename U>\n" 21899 "concept FooableWith = requires(T t, U u) {\n" 21900 " typename T::foo_type;\n" 21901 " { t.foo(u) } -> typename T::foo_type;\n" 21902 " t++;\n" 21903 "};\n" 21904 "void doFoo(FooableWith<int> auto t) {\n" 21905 " t.foo(3);\n" 21906 "}", 21907 Style); 21908 verifyFormat("template <typename T>\n" 21909 "concept Context = sizeof(T) == 1;", 21910 Style); 21911 verifyFormat("template <typename T>\n" 21912 "concept Context = is_specialization_of_v<context, T>;", 21913 Style); 21914 verifyFormat("template <typename T>\n" 21915 "concept Node = std::is_object_v<T>;", 21916 Style); 21917 verifyFormat("template <typename T>\n" 21918 "concept Tree = true;", 21919 Style); 21920 21921 verifyFormat("template <typename T> int g(T i) requires Concept1<I> {\n" 21922 " //...\n" 21923 "}", 21924 Style); 21925 21926 verifyFormat( 21927 "template <typename T> int g(T i) requires Concept1<I> && Concept2<I> {\n" 21928 " //...\n" 21929 "}", 21930 Style); 21931 21932 verifyFormat( 21933 "template <typename T> int g(T i) requires Concept1<I> || Concept2<I> {\n" 21934 " //...\n" 21935 "}", 21936 Style); 21937 21938 verifyFormat("template <typename T>\n" 21939 "veryveryvery_long_return_type g(T i) requires Concept1<I> || " 21940 "Concept2<I> {\n" 21941 " //...\n" 21942 "}", 21943 Style); 21944 21945 verifyFormat("template <typename T>\n" 21946 "veryveryvery_long_return_type g(T i) requires Concept1<I> && " 21947 "Concept2<I> {\n" 21948 " //...\n" 21949 "}", 21950 Style); 21951 21952 verifyFormat( 21953 "template <typename T>\n" 21954 "veryveryvery_long_return_type g(T i) requires Concept1 && Concept2 {\n" 21955 " //...\n" 21956 "}", 21957 Style); 21958 21959 verifyFormat( 21960 "template <typename T>\n" 21961 "veryveryvery_long_return_type g(T i) requires Concept1 || Concept2 {\n" 21962 " //...\n" 21963 "}", 21964 Style); 21965 21966 verifyFormat("template <typename It>\n" 21967 "requires Foo<It>() && Bar<It> {\n" 21968 " //....\n" 21969 "}", 21970 Style); 21971 21972 verifyFormat("template <typename It>\n" 21973 "requires Foo<Bar<It>>() && Bar<Foo<It, It>> {\n" 21974 " //....\n" 21975 "}", 21976 Style); 21977 21978 verifyFormat("template <typename It>\n" 21979 "requires Foo<Bar<It, It>>() && Bar<Foo<It, It>> {\n" 21980 " //....\n" 21981 "}", 21982 Style); 21983 21984 verifyFormat( 21985 "template <typename It>\n" 21986 "requires Foo<Bar<It>, Baz<It>>() && Bar<Foo<It>, Baz<It, It>> {\n" 21987 " //....\n" 21988 "}", 21989 Style); 21990 21991 Style.IndentRequires = true; 21992 verifyFormat("template <typename It>\n" 21993 " requires Iterator<It>\n" 21994 "void sort(It begin, It end) {\n" 21995 " //....\n" 21996 "}", 21997 Style); 21998 verifyFormat("template <std::size index_>\n" 21999 " requires(index_ < sizeof...(Children_))\n" 22000 "Tree auto &child() {\n" 22001 " // ...\n" 22002 "}", 22003 Style); 22004 22005 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 22006 verifyFormat("template <typename T>\n" 22007 "concept Hashable = requires (T a) {\n" 22008 " { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n" 22009 "};", 22010 Style); 22011 22012 verifyFormat("template <class T = void>\n" 22013 " requires EqualityComparable<T> || Same<T, void>\n" 22014 "struct equal_to;", 22015 Style); 22016 22017 verifyFormat("template <class T>\n" 22018 " requires requires {\n" 22019 " T{};\n" 22020 " T (int);\n" 22021 " }\n", 22022 Style); 22023 22024 Style.ColumnLimit = 78; 22025 verifyFormat("template <typename T>\n" 22026 "concept Context = Traits<typename T::traits_type> and\n" 22027 " Interface<typename T::interface_type> and\n" 22028 " Request<typename T::request_type> and\n" 22029 " Response<typename T::response_type> and\n" 22030 " ContextExtension<typename T::extension_type> and\n" 22031 " ::std::is_copy_constructable<T> and " 22032 "::std::is_move_constructable<T> and\n" 22033 " requires (T c) {\n" 22034 " { c.response; } -> Response;\n" 22035 "} and requires (T c) {\n" 22036 " { c.request; } -> Request;\n" 22037 "}\n", 22038 Style); 22039 22040 verifyFormat("template <typename T>\n" 22041 "concept Context = Traits<typename T::traits_type> or\n" 22042 " Interface<typename T::interface_type> or\n" 22043 " Request<typename T::request_type> or\n" 22044 " Response<typename T::response_type> or\n" 22045 " ContextExtension<typename T::extension_type> or\n" 22046 " ::std::is_copy_constructable<T> or " 22047 "::std::is_move_constructable<T> or\n" 22048 " requires (T c) {\n" 22049 " { c.response; } -> Response;\n" 22050 "} or requires (T c) {\n" 22051 " { c.request; } -> Request;\n" 22052 "}\n", 22053 Style); 22054 22055 verifyFormat("template <typename T>\n" 22056 "concept Context = Traits<typename T::traits_type> &&\n" 22057 " Interface<typename T::interface_type> &&\n" 22058 " Request<typename T::request_type> &&\n" 22059 " Response<typename T::response_type> &&\n" 22060 " ContextExtension<typename T::extension_type> &&\n" 22061 " ::std::is_copy_constructable<T> && " 22062 "::std::is_move_constructable<T> &&\n" 22063 " requires (T c) {\n" 22064 " { c.response; } -> Response;\n" 22065 "} && requires (T c) {\n" 22066 " { c.request; } -> Request;\n" 22067 "}\n", 22068 Style); 22069 22070 verifyFormat("template <typename T>\nconcept someConcept = Constraint1<T> && " 22071 "Constraint2<T>;"); 22072 22073 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 22074 Style.BraceWrapping.AfterFunction = true; 22075 Style.BraceWrapping.AfterClass = true; 22076 Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 22077 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 22078 verifyFormat("void Foo () requires (std::copyable<T>)\n" 22079 "{\n" 22080 " return\n" 22081 "}\n", 22082 Style); 22083 22084 verifyFormat("void Foo () requires std::copyable<T>\n" 22085 "{\n" 22086 " return\n" 22087 "}\n", 22088 Style); 22089 22090 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 22091 " requires (std::invocable<F, std::invoke_result_t<Args>...>)\n" 22092 "struct constant;", 22093 Style); 22094 22095 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 22096 " requires std::invocable<F, std::invoke_result_t<Args>...>\n" 22097 "struct constant;", 22098 Style); 22099 22100 verifyFormat("template <class T>\n" 22101 "class plane_with_very_very_very_long_name\n" 22102 "{\n" 22103 " constexpr plane_with_very_very_very_long_name () requires " 22104 "std::copyable<T>\n" 22105 " : plane_with_very_very_very_long_name (1)\n" 22106 " {\n" 22107 " }\n" 22108 "}\n", 22109 Style); 22110 22111 verifyFormat("template <class T>\n" 22112 "class plane_with_long_name\n" 22113 "{\n" 22114 " constexpr plane_with_long_name () requires std::copyable<T>\n" 22115 " : plane_with_long_name (1)\n" 22116 " {\n" 22117 " }\n" 22118 "}\n", 22119 Style); 22120 22121 Style.BreakBeforeConceptDeclarations = false; 22122 verifyFormat("template <typename T> concept Tree = true;", Style); 22123 22124 Style.IndentRequires = false; 22125 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 22126 "requires (std::invocable<F, std::invoke_result_t<Args>...>) " 22127 "struct constant;", 22128 Style); 22129 } 22130 22131 TEST_F(FormatTest, StatementAttributeLikeMacros) { 22132 FormatStyle Style = getLLVMStyle(); 22133 StringRef Source = "void Foo::slot() {\n" 22134 " unsigned char MyChar = 'x';\n" 22135 " emit signal(MyChar);\n" 22136 " Q_EMIT signal(MyChar);\n" 22137 "}"; 22138 22139 EXPECT_EQ(Source, format(Source, Style)); 22140 22141 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 22142 EXPECT_EQ("void Foo::slot() {\n" 22143 " unsigned char MyChar = 'x';\n" 22144 " emit signal(MyChar);\n" 22145 " Q_EMIT signal(MyChar);\n" 22146 "}", 22147 format(Source, Style)); 22148 22149 Style.StatementAttributeLikeMacros.push_back("emit"); 22150 EXPECT_EQ(Source, format(Source, Style)); 22151 22152 Style.StatementAttributeLikeMacros = {}; 22153 EXPECT_EQ("void Foo::slot() {\n" 22154 " unsigned char MyChar = 'x';\n" 22155 " emit signal(MyChar);\n" 22156 " Q_EMIT signal(MyChar);\n" 22157 "}", 22158 format(Source, Style)); 22159 } 22160 22161 TEST_F(FormatTest, IndentAccessModifiers) { 22162 FormatStyle Style = getLLVMStyle(); 22163 Style.IndentAccessModifiers = true; 22164 // Members are *two* levels below the record; 22165 // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation. 22166 verifyFormat("class C {\n" 22167 " int i;\n" 22168 "};\n", 22169 Style); 22170 verifyFormat("union C {\n" 22171 " int i;\n" 22172 " unsigned u;\n" 22173 "};\n", 22174 Style); 22175 // Access modifiers should be indented one level below the record. 22176 verifyFormat("class C {\n" 22177 " public:\n" 22178 " int i;\n" 22179 "};\n", 22180 Style); 22181 verifyFormat("struct S {\n" 22182 " private:\n" 22183 " class C {\n" 22184 " int j;\n" 22185 "\n" 22186 " public:\n" 22187 " C();\n" 22188 " };\n" 22189 "\n" 22190 " public:\n" 22191 " int i;\n" 22192 "};\n", 22193 Style); 22194 // Enumerations are not records and should be unaffected. 22195 Style.AllowShortEnumsOnASingleLine = false; 22196 verifyFormat("enum class E {\n" 22197 " A,\n" 22198 " B\n" 22199 "};\n", 22200 Style); 22201 // Test with a different indentation width; 22202 // also proves that the result is Style.AccessModifierOffset agnostic. 22203 Style.IndentWidth = 3; 22204 verifyFormat("class C {\n" 22205 " public:\n" 22206 " int i;\n" 22207 "};\n", 22208 Style); 22209 } 22210 22211 TEST_F(FormatTest, LimitlessStringsAndComments) { 22212 auto Style = getLLVMStyleWithColumns(0); 22213 constexpr StringRef Code = 22214 "/**\n" 22215 " * This is a multiline comment with quite some long lines, at least for " 22216 "the LLVM Style.\n" 22217 " * We will redo this with strings and line comments. Just to check if " 22218 "everything is working.\n" 22219 " */\n" 22220 "bool foo() {\n" 22221 " /* Single line multi line comment. */\n" 22222 " const std::string String = \"This is a multiline string with quite " 22223 "some long lines, at least for the LLVM Style.\"\n" 22224 " \"We already did it with multi line " 22225 "comments, and we will do it with line comments. Just to check if " 22226 "everything is working.\";\n" 22227 " // This is a line comment (block) with quite some long lines, at " 22228 "least for the LLVM Style.\n" 22229 " // We already did this with multi line comments and strings. Just to " 22230 "check if everything is working.\n" 22231 " const std::string SmallString = \"Hello World\";\n" 22232 " // Small line comment\n" 22233 " return String.size() > SmallString.size();\n" 22234 "}"; 22235 EXPECT_EQ(Code, format(Code, Style)); 22236 } 22237 } // namespace 22238 } // namespace format 22239 } // namespace clang 22240