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 // The return breaking style doesn't affect object definitions with 8251 // attribute-like macros. 8252 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n" 8253 " ABSL_GUARDED_BY(mutex) = {};", 8254 getGoogleStyleWithColumns(40)); 8255 8256 Style = getGNUStyle(); 8257 8258 // Test for comments at the end of function declarations. 8259 verifyFormat("void\n" 8260 "foo (int a, /*abc*/ int b) // def\n" 8261 "{\n" 8262 "}\n", 8263 Style); 8264 8265 verifyFormat("void\n" 8266 "foo (int a, /* abc */ int b) /* def */\n" 8267 "{\n" 8268 "}\n", 8269 Style); 8270 8271 // Definitions that should not break after return type 8272 verifyFormat("void foo (int a, int b); // def\n", Style); 8273 verifyFormat("void foo (int a, int b); /* def */\n", Style); 8274 verifyFormat("void foo (int a, int b);\n", Style); 8275 } 8276 8277 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 8278 FormatStyle NoBreak = getLLVMStyle(); 8279 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 8280 FormatStyle Break = getLLVMStyle(); 8281 Break.AlwaysBreakBeforeMultilineStrings = true; 8282 verifyFormat("aaaa = \"bbbb\"\n" 8283 " \"cccc\";", 8284 NoBreak); 8285 verifyFormat("aaaa =\n" 8286 " \"bbbb\"\n" 8287 " \"cccc\";", 8288 Break); 8289 verifyFormat("aaaa(\"bbbb\"\n" 8290 " \"cccc\");", 8291 NoBreak); 8292 verifyFormat("aaaa(\n" 8293 " \"bbbb\"\n" 8294 " \"cccc\");", 8295 Break); 8296 verifyFormat("aaaa(qqq, \"bbbb\"\n" 8297 " \"cccc\");", 8298 NoBreak); 8299 verifyFormat("aaaa(qqq,\n" 8300 " \"bbbb\"\n" 8301 " \"cccc\");", 8302 Break); 8303 verifyFormat("aaaa(qqq,\n" 8304 " L\"bbbb\"\n" 8305 " L\"cccc\");", 8306 Break); 8307 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 8308 " \"bbbb\"));", 8309 Break); 8310 verifyFormat("string s = someFunction(\n" 8311 " \"abc\"\n" 8312 " \"abc\");", 8313 Break); 8314 8315 // As we break before unary operators, breaking right after them is bad. 8316 verifyFormat("string foo = abc ? \"x\"\n" 8317 " \"blah blah blah blah blah blah\"\n" 8318 " : \"y\";", 8319 Break); 8320 8321 // Don't break if there is no column gain. 8322 verifyFormat("f(\"aaaa\"\n" 8323 " \"bbbb\");", 8324 Break); 8325 8326 // Treat literals with escaped newlines like multi-line string literals. 8327 EXPECT_EQ("x = \"a\\\n" 8328 "b\\\n" 8329 "c\";", 8330 format("x = \"a\\\n" 8331 "b\\\n" 8332 "c\";", 8333 NoBreak)); 8334 EXPECT_EQ("xxxx =\n" 8335 " \"a\\\n" 8336 "b\\\n" 8337 "c\";", 8338 format("xxxx = \"a\\\n" 8339 "b\\\n" 8340 "c\";", 8341 Break)); 8342 8343 EXPECT_EQ("NSString *const kString =\n" 8344 " @\"aaaa\"\n" 8345 " @\"bbbb\";", 8346 format("NSString *const kString = @\"aaaa\"\n" 8347 "@\"bbbb\";", 8348 Break)); 8349 8350 Break.ColumnLimit = 0; 8351 verifyFormat("const char *hello = \"hello llvm\";", Break); 8352 } 8353 8354 TEST_F(FormatTest, AlignsPipes) { 8355 verifyFormat( 8356 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8357 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8358 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8359 verifyFormat( 8360 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 8361 " << aaaaaaaaaaaaaaaaaaaa;"); 8362 verifyFormat( 8363 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8364 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8365 verifyFormat( 8366 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 8367 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8368 verifyFormat( 8369 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 8370 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 8371 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 8372 verifyFormat( 8373 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8374 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8375 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8376 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8377 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8378 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8379 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 8380 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 8381 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 8382 verifyFormat( 8383 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8384 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8385 verifyFormat( 8386 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n" 8387 " aaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8388 8389 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 8390 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 8391 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8392 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8393 " aaaaaaaaaaaaaaaaaaaaa)\n" 8394 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8395 verifyFormat("LOG_IF(aaa == //\n" 8396 " bbb)\n" 8397 " << a << b;"); 8398 8399 // But sometimes, breaking before the first "<<" is desirable. 8400 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 8401 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 8402 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 8403 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8404 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8405 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 8406 " << BEF << IsTemplate << Description << E->getType();"); 8407 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 8408 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8409 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8410 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 8411 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8412 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8413 " << aaa;"); 8414 8415 verifyFormat( 8416 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8417 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8418 8419 // Incomplete string literal. 8420 EXPECT_EQ("llvm::errs() << \"\n" 8421 " << a;", 8422 format("llvm::errs() << \"\n<<a;")); 8423 8424 verifyFormat("void f() {\n" 8425 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 8426 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 8427 "}"); 8428 8429 // Handle 'endl'. 8430 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 8431 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 8432 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 8433 8434 // Handle '\n'. 8435 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 8436 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 8437 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 8438 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 8439 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 8440 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 8441 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 8442 } 8443 8444 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) { 8445 verifyFormat("return out << \"somepacket = {\\n\"\n" 8446 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 8447 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 8448 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 8449 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 8450 " << \"}\";"); 8451 8452 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 8453 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 8454 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 8455 verifyFormat( 8456 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 8457 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 8458 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 8459 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 8460 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 8461 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 8462 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 8463 verifyFormat( 8464 "void f() {\n" 8465 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 8466 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 8467 "}"); 8468 8469 // Breaking before the first "<<" is generally not desirable. 8470 verifyFormat( 8471 "llvm::errs()\n" 8472 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8473 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8474 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8475 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 8476 getLLVMStyleWithColumns(70)); 8477 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 8478 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8479 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 8480 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8481 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 8482 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 8483 getLLVMStyleWithColumns(70)); 8484 8485 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 8486 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 8487 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;"); 8488 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 8489 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 8490 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);"); 8491 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n" 8492 " (aaaa + aaaa);", 8493 getLLVMStyleWithColumns(40)); 8494 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n" 8495 " (aaaaaaa + aaaaa));", 8496 getLLVMStyleWithColumns(40)); 8497 verifyFormat( 8498 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n" 8499 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n" 8500 " bbbbbbbbbbbbbbbbbbbbbbb);"); 8501 } 8502 8503 TEST_F(FormatTest, UnderstandsEquals) { 8504 verifyFormat( 8505 "aaaaaaaaaaaaaaaaa =\n" 8506 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8507 verifyFormat( 8508 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8509 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 8510 verifyFormat( 8511 "if (a) {\n" 8512 " f();\n" 8513 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8514 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 8515 "}"); 8516 8517 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8518 " 100000000 + 10000000) {\n}"); 8519 } 8520 8521 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 8522 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 8523 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 8524 8525 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 8526 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 8527 8528 verifyFormat( 8529 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 8530 " Parameter2);"); 8531 8532 verifyFormat( 8533 "ShortObject->shortFunction(\n" 8534 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 8535 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 8536 8537 verifyFormat("loooooooooooooongFunction(\n" 8538 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 8539 8540 verifyFormat( 8541 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 8542 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 8543 8544 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 8545 " .WillRepeatedly(Return(SomeValue));"); 8546 verifyFormat("void f() {\n" 8547 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 8548 " .Times(2)\n" 8549 " .WillRepeatedly(Return(SomeValue));\n" 8550 "}"); 8551 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 8552 " ccccccccccccccccccccccc);"); 8553 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8554 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8555 " .aaaaa(aaaaa),\n" 8556 " aaaaaaaaaaaaaaaaaaaaa);"); 8557 verifyFormat("void f() {\n" 8558 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8559 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 8560 "}"); 8561 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8562 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8563 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8564 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8565 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 8566 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8567 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8568 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8569 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 8570 "}"); 8571 8572 // Here, it is not necessary to wrap at "." or "->". 8573 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 8574 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 8575 verifyFormat( 8576 "aaaaaaaaaaa->aaaaaaaaa(\n" 8577 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8578 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 8579 8580 verifyFormat( 8581 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8582 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 8583 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 8584 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 8585 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 8586 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 8587 8588 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8589 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8590 " .a();"); 8591 8592 FormatStyle NoBinPacking = getLLVMStyle(); 8593 NoBinPacking.BinPackParameters = false; 8594 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 8595 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 8596 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 8597 " aaaaaaaaaaaaaaaaaaa,\n" 8598 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8599 NoBinPacking); 8600 8601 // If there is a subsequent call, change to hanging indentation. 8602 verifyFormat( 8603 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8604 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 8605 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8606 verifyFormat( 8607 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8608 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 8609 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8610 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8611 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8612 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8613 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8614 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 8615 } 8616 8617 TEST_F(FormatTest, WrapsTemplateDeclarations) { 8618 verifyFormat("template <typename T>\n" 8619 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 8620 verifyFormat("template <typename T>\n" 8621 "// T should be one of {A, B}.\n" 8622 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 8623 verifyFormat( 8624 "template <typename T>\n" 8625 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 8626 verifyFormat("template <typename T>\n" 8627 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 8628 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 8629 verifyFormat( 8630 "template <typename T>\n" 8631 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 8632 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 8633 verifyFormat( 8634 "template <typename T>\n" 8635 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 8636 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 8637 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8638 verifyFormat("template <typename T>\n" 8639 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8640 " int aaaaaaaaaaaaaaaaaaaaaa);"); 8641 verifyFormat( 8642 "template <typename T1, typename T2 = char, typename T3 = char,\n" 8643 " typename T4 = char>\n" 8644 "void f();"); 8645 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 8646 " template <typename> class cccccccccccccccccccccc,\n" 8647 " typename ddddddddddddd>\n" 8648 "class C {};"); 8649 verifyFormat( 8650 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 8651 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8652 8653 verifyFormat("void f() {\n" 8654 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 8655 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 8656 "}"); 8657 8658 verifyFormat("template <typename T> class C {};"); 8659 verifyFormat("template <typename T> void f();"); 8660 verifyFormat("template <typename T> void f() {}"); 8661 verifyFormat( 8662 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 8663 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8664 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 8665 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 8666 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8667 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 8668 " bbbbbbbbbbbbbbbbbbbbbbbb);", 8669 getLLVMStyleWithColumns(72)); 8670 EXPECT_EQ("static_cast<A< //\n" 8671 " B> *>(\n" 8672 "\n" 8673 ");", 8674 format("static_cast<A<//\n" 8675 " B>*>(\n" 8676 "\n" 8677 " );")); 8678 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8679 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 8680 8681 FormatStyle AlwaysBreak = getLLVMStyle(); 8682 AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 8683 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 8684 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 8685 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 8686 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8687 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 8688 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 8689 verifyFormat("template <template <typename> class Fooooooo,\n" 8690 " template <typename> class Baaaaaaar>\n" 8691 "struct C {};", 8692 AlwaysBreak); 8693 verifyFormat("template <typename T> // T can be A, B or C.\n" 8694 "struct C {};", 8695 AlwaysBreak); 8696 verifyFormat("template <enum E> class A {\n" 8697 "public:\n" 8698 " E *f();\n" 8699 "};"); 8700 8701 FormatStyle NeverBreak = getLLVMStyle(); 8702 NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No; 8703 verifyFormat("template <typename T> class C {};", NeverBreak); 8704 verifyFormat("template <typename T> void f();", NeverBreak); 8705 verifyFormat("template <typename T> void f() {}", NeverBreak); 8706 verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa " 8707 "bbbbbbbbbbbbbbbbbbbb) {}", 8708 NeverBreak); 8709 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8710 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 8711 " ccccccccccccccccccccccccccccccccccccccccccccccc);", 8712 NeverBreak); 8713 verifyFormat("template <template <typename> class Fooooooo,\n" 8714 " template <typename> class Baaaaaaar>\n" 8715 "struct C {};", 8716 NeverBreak); 8717 verifyFormat("template <typename T> // T can be A, B or C.\n" 8718 "struct C {};", 8719 NeverBreak); 8720 verifyFormat("template <enum E> class A {\n" 8721 "public:\n" 8722 " E *f();\n" 8723 "};", 8724 NeverBreak); 8725 NeverBreak.PenaltyBreakTemplateDeclaration = 100; 8726 verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa " 8727 "bbbbbbbbbbbbbbbbbbbb) {}", 8728 NeverBreak); 8729 } 8730 8731 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) { 8732 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 8733 Style.ColumnLimit = 60; 8734 EXPECT_EQ("// Baseline - no comments.\n" 8735 "template <\n" 8736 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 8737 "void f() {}", 8738 format("// Baseline - no comments.\n" 8739 "template <\n" 8740 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 8741 "void f() {}", 8742 Style)); 8743 8744 EXPECT_EQ("template <\n" 8745 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 8746 "void f() {}", 8747 format("template <\n" 8748 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 8749 "void f() {}", 8750 Style)); 8751 8752 EXPECT_EQ( 8753 "template <\n" 8754 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 8755 "void f() {}", 8756 format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 8757 "void f() {}", 8758 Style)); 8759 8760 EXPECT_EQ( 8761 "template <\n" 8762 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 8763 " // multiline\n" 8764 "void f() {}", 8765 format("template <\n" 8766 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 8767 " // multiline\n" 8768 "void f() {}", 8769 Style)); 8770 8771 EXPECT_EQ( 8772 "template <typename aaaaaaaaaa<\n" 8773 " bbbbbbbbbbbb>::value> // trailing loooong\n" 8774 "void f() {}", 8775 format( 8776 "template <\n" 8777 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n" 8778 "void f() {}", 8779 Style)); 8780 } 8781 8782 TEST_F(FormatTest, WrapsTemplateParameters) { 8783 FormatStyle Style = getLLVMStyle(); 8784 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 8785 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 8786 verifyFormat( 8787 "template <typename... a> struct q {};\n" 8788 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 8789 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 8790 " y;", 8791 Style); 8792 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 8793 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 8794 verifyFormat( 8795 "template <typename... a> struct r {};\n" 8796 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 8797 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 8798 " y;", 8799 Style); 8800 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 8801 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 8802 verifyFormat("template <typename... a> struct s {};\n" 8803 "extern s<\n" 8804 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 8805 "aaaaaaaaaaaaaaaaaaaaaa,\n" 8806 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 8807 "aaaaaaaaaaaaaaaaaaaaaa>\n" 8808 " y;", 8809 Style); 8810 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 8811 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 8812 verifyFormat("template <typename... a> struct t {};\n" 8813 "extern t<\n" 8814 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 8815 "aaaaaaaaaaaaaaaaaaaaaa,\n" 8816 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 8817 "aaaaaaaaaaaaaaaaaaaaaa>\n" 8818 " y;", 8819 Style); 8820 } 8821 8822 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 8823 verifyFormat( 8824 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8825 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8826 verifyFormat( 8827 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8828 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8829 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 8830 8831 // FIXME: Should we have the extra indent after the second break? 8832 verifyFormat( 8833 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8834 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8835 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8836 8837 verifyFormat( 8838 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 8839 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 8840 8841 // Breaking at nested name specifiers is generally not desirable. 8842 verifyFormat( 8843 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8844 " aaaaaaaaaaaaaaaaaaaaaaa);"); 8845 8846 verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n" 8847 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8848 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8849 " aaaaaaaaaaaaaaaaaaaaa);", 8850 getLLVMStyleWithColumns(74)); 8851 8852 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8853 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8854 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8855 } 8856 8857 TEST_F(FormatTest, UnderstandsTemplateParameters) { 8858 verifyFormat("A<int> a;"); 8859 verifyFormat("A<A<A<int>>> a;"); 8860 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 8861 verifyFormat("bool x = a < 1 || 2 > a;"); 8862 verifyFormat("bool x = 5 < f<int>();"); 8863 verifyFormat("bool x = f<int>() > 5;"); 8864 verifyFormat("bool x = 5 < a<int>::x;"); 8865 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 8866 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 8867 8868 verifyGoogleFormat("A<A<int>> a;"); 8869 verifyGoogleFormat("A<A<A<int>>> a;"); 8870 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 8871 verifyGoogleFormat("A<A<int> > a;"); 8872 verifyGoogleFormat("A<A<A<int> > > a;"); 8873 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 8874 verifyGoogleFormat("A<::A<int>> a;"); 8875 verifyGoogleFormat("A<::A> a;"); 8876 verifyGoogleFormat("A< ::A> a;"); 8877 verifyGoogleFormat("A< ::A<int> > a;"); 8878 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 8879 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 8880 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 8881 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 8882 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 8883 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 8884 8885 verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 8886 8887 // template closer followed by a token that starts with > or = 8888 verifyFormat("bool b = a<1> > 1;"); 8889 verifyFormat("bool b = a<1> >= 1;"); 8890 verifyFormat("int i = a<1> >> 1;"); 8891 FormatStyle Style = getLLVMStyle(); 8892 Style.SpaceBeforeAssignmentOperators = false; 8893 verifyFormat("bool b= a<1> == 1;", Style); 8894 verifyFormat("a<int> = 1;", Style); 8895 verifyFormat("a<int> >>= 1;", Style); 8896 8897 verifyFormat("test < a | b >> c;"); 8898 verifyFormat("test<test<a | b>> c;"); 8899 verifyFormat("test >> a >> b;"); 8900 verifyFormat("test << a >> b;"); 8901 8902 verifyFormat("f<int>();"); 8903 verifyFormat("template <typename T> void f() {}"); 8904 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 8905 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 8906 "sizeof(char)>::type>;"); 8907 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 8908 verifyFormat("f(a.operator()<A>());"); 8909 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8910 " .template operator()<A>());", 8911 getLLVMStyleWithColumns(35)); 8912 8913 // Not template parameters. 8914 verifyFormat("return a < b && c > d;"); 8915 verifyFormat("void f() {\n" 8916 " while (a < b && c > d) {\n" 8917 " }\n" 8918 "}"); 8919 verifyFormat("template <typename... Types>\n" 8920 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 8921 8922 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8923 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 8924 getLLVMStyleWithColumns(60)); 8925 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 8926 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 8927 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 8928 verifyFormat("some_templated_type<decltype([](int i) { return i; })>"); 8929 } 8930 8931 TEST_F(FormatTest, UnderstandsShiftOperators) { 8932 verifyFormat("if (i < x >> 1)"); 8933 verifyFormat("while (i < x >> 1)"); 8934 verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)"); 8935 verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)"); 8936 verifyFormat( 8937 "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)"); 8938 verifyFormat("Foo.call<Bar<Function>>()"); 8939 verifyFormat("if (Foo.call<Bar<Function>>() == 0)"); 8940 verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; " 8941 "++i, v = v >> 1)"); 8942 verifyFormat("if (w<u<v<x>>, 1>::t)"); 8943 } 8944 8945 TEST_F(FormatTest, BitshiftOperatorWidth) { 8946 EXPECT_EQ("int a = 1 << 2; /* foo\n" 8947 " bar */", 8948 format("int a=1<<2; /* foo\n" 8949 " bar */")); 8950 8951 EXPECT_EQ("int b = 256 >> 1; /* foo\n" 8952 " bar */", 8953 format("int b =256>>1 ; /* foo\n" 8954 " bar */")); 8955 } 8956 8957 TEST_F(FormatTest, UnderstandsBinaryOperators) { 8958 verifyFormat("COMPARE(a, ==, b);"); 8959 verifyFormat("auto s = sizeof...(Ts) - 1;"); 8960 } 8961 8962 TEST_F(FormatTest, UnderstandsPointersToMembers) { 8963 verifyFormat("int A::*x;"); 8964 verifyFormat("int (S::*func)(void *);"); 8965 verifyFormat("void f() { int (S::*func)(void *); }"); 8966 verifyFormat("typedef bool *(Class::*Member)() const;"); 8967 verifyFormat("void f() {\n" 8968 " (a->*f)();\n" 8969 " a->*x;\n" 8970 " (a.*f)();\n" 8971 " ((*a).*f)();\n" 8972 " a.*x;\n" 8973 "}"); 8974 verifyFormat("void f() {\n" 8975 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 8976 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 8977 "}"); 8978 verifyFormat( 8979 "(aaaaaaaaaa->*bbbbbbb)(\n" 8980 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 8981 FormatStyle Style = getLLVMStyle(); 8982 Style.PointerAlignment = FormatStyle::PAS_Left; 8983 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 8984 } 8985 8986 TEST_F(FormatTest, UnderstandsUnaryOperators) { 8987 verifyFormat("int a = -2;"); 8988 verifyFormat("f(-1, -2, -3);"); 8989 verifyFormat("a[-1] = 5;"); 8990 verifyFormat("int a = 5 + -2;"); 8991 verifyFormat("if (i == -1) {\n}"); 8992 verifyFormat("if (i != -1) {\n}"); 8993 verifyFormat("if (i > -1) {\n}"); 8994 verifyFormat("if (i < -1) {\n}"); 8995 verifyFormat("++(a->f());"); 8996 verifyFormat("--(a->f());"); 8997 verifyFormat("(a->f())++;"); 8998 verifyFormat("a[42]++;"); 8999 verifyFormat("if (!(a->f())) {\n}"); 9000 verifyFormat("if (!+i) {\n}"); 9001 verifyFormat("~&a;"); 9002 9003 verifyFormat("a-- > b;"); 9004 verifyFormat("b ? -a : c;"); 9005 verifyFormat("n * sizeof char16;"); 9006 verifyFormat("n * alignof char16;", getGoogleStyle()); 9007 verifyFormat("sizeof(char);"); 9008 verifyFormat("alignof(char);", getGoogleStyle()); 9009 9010 verifyFormat("return -1;"); 9011 verifyFormat("throw -1;"); 9012 verifyFormat("switch (a) {\n" 9013 "case -1:\n" 9014 " break;\n" 9015 "}"); 9016 verifyFormat("#define X -1"); 9017 verifyFormat("#define X -kConstant"); 9018 9019 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 9020 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 9021 9022 verifyFormat("int a = /* confusing comment */ -1;"); 9023 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 9024 verifyFormat("int a = i /* confusing comment */++;"); 9025 9026 verifyFormat("co_yield -1;"); 9027 verifyFormat("co_return -1;"); 9028 9029 // Check that * is not treated as a binary operator when we set 9030 // PointerAlignment as PAS_Left after a keyword and not a declaration. 9031 FormatStyle PASLeftStyle = getLLVMStyle(); 9032 PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left; 9033 verifyFormat("co_return *a;", PASLeftStyle); 9034 verifyFormat("co_await *a;", PASLeftStyle); 9035 verifyFormat("co_yield *a", PASLeftStyle); 9036 verifyFormat("return *a;", PASLeftStyle); 9037 } 9038 9039 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 9040 verifyFormat("if (!aaaaaaaaaa( // break\n" 9041 " aaaaa)) {\n" 9042 "}"); 9043 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 9044 " aaaaa));"); 9045 verifyFormat("*aaa = aaaaaaa( // break\n" 9046 " bbbbbb);"); 9047 } 9048 9049 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 9050 verifyFormat("bool operator<();"); 9051 verifyFormat("bool operator>();"); 9052 verifyFormat("bool operator=();"); 9053 verifyFormat("bool operator==();"); 9054 verifyFormat("bool operator!=();"); 9055 verifyFormat("int operator+();"); 9056 verifyFormat("int operator++();"); 9057 verifyFormat("int operator++(int) volatile noexcept;"); 9058 verifyFormat("bool operator,();"); 9059 verifyFormat("bool operator();"); 9060 verifyFormat("bool operator()();"); 9061 verifyFormat("bool operator[]();"); 9062 verifyFormat("operator bool();"); 9063 verifyFormat("operator int();"); 9064 verifyFormat("operator void *();"); 9065 verifyFormat("operator SomeType<int>();"); 9066 verifyFormat("operator SomeType<int, int>();"); 9067 verifyFormat("operator SomeType<SomeType<int>>();"); 9068 verifyFormat("void *operator new(std::size_t size);"); 9069 verifyFormat("void *operator new[](std::size_t size);"); 9070 verifyFormat("void operator delete(void *ptr);"); 9071 verifyFormat("void operator delete[](void *ptr);"); 9072 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 9073 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 9074 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 9075 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 9076 9077 verifyFormat( 9078 "ostream &operator<<(ostream &OutputStream,\n" 9079 " SomeReallyLongType WithSomeReallyLongValue);"); 9080 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 9081 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 9082 " return left.group < right.group;\n" 9083 "}"); 9084 verifyFormat("SomeType &operator=(const SomeType &S);"); 9085 verifyFormat("f.template operator()<int>();"); 9086 9087 verifyGoogleFormat("operator void*();"); 9088 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 9089 verifyGoogleFormat("operator ::A();"); 9090 9091 verifyFormat("using A::operator+;"); 9092 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 9093 "int i;"); 9094 9095 // Calling an operator as a member function. 9096 verifyFormat("void f() { a.operator*(); }"); 9097 verifyFormat("void f() { a.operator*(b & b); }"); 9098 verifyFormat("void f() { a->operator&(a * b); }"); 9099 verifyFormat("void f() { NS::a.operator+(*b * *b); }"); 9100 // TODO: Calling an operator as a non-member function is hard to distinguish. 9101 // https://llvm.org/PR50629 9102 // verifyFormat("void f() { operator*(a & a); }"); 9103 // verifyFormat("void f() { operator&(a, b * b); }"); 9104 } 9105 9106 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 9107 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 9108 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 9109 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 9110 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 9111 verifyFormat("Deleted &operator=(const Deleted &) &;"); 9112 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 9113 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 9114 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 9115 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 9116 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 9117 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 9118 verifyFormat("void Fn(T const &) const &;"); 9119 verifyFormat("void Fn(T const volatile &&) const volatile &&;"); 9120 verifyFormat("template <typename T>\n" 9121 "void F(T) && = delete;", 9122 getGoogleStyle()); 9123 9124 FormatStyle AlignLeft = getLLVMStyle(); 9125 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 9126 verifyFormat("void A::b() && {}", AlignLeft); 9127 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 9128 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 9129 AlignLeft); 9130 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 9131 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 9132 verifyFormat("auto Function(T t) & -> void {}", AlignLeft); 9133 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft); 9134 verifyFormat("auto Function(T) & -> void {}", AlignLeft); 9135 verifyFormat("auto Function(T) & -> void;", AlignLeft); 9136 verifyFormat("void Fn(T const&) const&;", AlignLeft); 9137 verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft); 9138 9139 FormatStyle Spaces = getLLVMStyle(); 9140 Spaces.SpacesInCStyleCastParentheses = true; 9141 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 9142 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 9143 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 9144 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 9145 9146 Spaces.SpacesInCStyleCastParentheses = false; 9147 Spaces.SpacesInParentheses = true; 9148 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 9149 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", 9150 Spaces); 9151 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 9152 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 9153 9154 FormatStyle BreakTemplate = getLLVMStyle(); 9155 BreakTemplate.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 9156 9157 verifyFormat("struct f {\n" 9158 " template <class T>\n" 9159 " int &foo(const std::string &str) &noexcept {}\n" 9160 "};", 9161 BreakTemplate); 9162 9163 verifyFormat("struct f {\n" 9164 " template <class T>\n" 9165 " int &foo(const std::string &str) &&noexcept {}\n" 9166 "};", 9167 BreakTemplate); 9168 9169 verifyFormat("struct f {\n" 9170 " template <class T>\n" 9171 " int &foo(const std::string &str) const &noexcept {}\n" 9172 "};", 9173 BreakTemplate); 9174 9175 verifyFormat("struct f {\n" 9176 " template <class T>\n" 9177 " int &foo(const std::string &str) const &noexcept {}\n" 9178 "};", 9179 BreakTemplate); 9180 9181 verifyFormat("struct f {\n" 9182 " template <class T>\n" 9183 " auto foo(const std::string &str) &&noexcept -> int & {}\n" 9184 "};", 9185 BreakTemplate); 9186 9187 FormatStyle AlignLeftBreakTemplate = getLLVMStyle(); 9188 AlignLeftBreakTemplate.AlwaysBreakTemplateDeclarations = 9189 FormatStyle::BTDS_Yes; 9190 AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left; 9191 9192 verifyFormat("struct f {\n" 9193 " template <class T>\n" 9194 " int& foo(const std::string& str) & noexcept {}\n" 9195 "};", 9196 AlignLeftBreakTemplate); 9197 9198 verifyFormat("struct f {\n" 9199 " template <class T>\n" 9200 " int& foo(const std::string& str) && noexcept {}\n" 9201 "};", 9202 AlignLeftBreakTemplate); 9203 9204 verifyFormat("struct f {\n" 9205 " template <class T>\n" 9206 " int& foo(const std::string& str) const& noexcept {}\n" 9207 "};", 9208 AlignLeftBreakTemplate); 9209 9210 verifyFormat("struct f {\n" 9211 " template <class T>\n" 9212 " int& foo(const std::string& str) const&& noexcept {}\n" 9213 "};", 9214 AlignLeftBreakTemplate); 9215 9216 verifyFormat("struct f {\n" 9217 " template <class T>\n" 9218 " auto foo(const std::string& str) && noexcept -> int& {}\n" 9219 "};", 9220 AlignLeftBreakTemplate); 9221 9222 // The `&` in `Type&` should not be confused with a trailing `&` of 9223 // DEPRECATED(reason) member function. 9224 verifyFormat("struct f {\n" 9225 " template <class T>\n" 9226 " DEPRECATED(reason)\n" 9227 " Type &foo(arguments) {}\n" 9228 "};", 9229 BreakTemplate); 9230 9231 verifyFormat("struct f {\n" 9232 " template <class T>\n" 9233 " DEPRECATED(reason)\n" 9234 " Type& foo(arguments) {}\n" 9235 "};", 9236 AlignLeftBreakTemplate); 9237 9238 verifyFormat("void (*foopt)(int) = &func;"); 9239 } 9240 9241 TEST_F(FormatTest, UnderstandsNewAndDelete) { 9242 verifyFormat("void f() {\n" 9243 " A *a = new A;\n" 9244 " A *a = new (placement) A;\n" 9245 " delete a;\n" 9246 " delete (A *)a;\n" 9247 "}"); 9248 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 9249 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 9250 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 9251 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 9252 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 9253 verifyFormat("delete[] h->p;"); 9254 } 9255 9256 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 9257 verifyFormat("int *f(int *a) {}"); 9258 verifyFormat("int main(int argc, char **argv) {}"); 9259 verifyFormat("Test::Test(int b) : a(b * b) {}"); 9260 verifyIndependentOfContext("f(a, *a);"); 9261 verifyFormat("void g() { f(*a); }"); 9262 verifyIndependentOfContext("int a = b * 10;"); 9263 verifyIndependentOfContext("int a = 10 * b;"); 9264 verifyIndependentOfContext("int a = b * c;"); 9265 verifyIndependentOfContext("int a += b * c;"); 9266 verifyIndependentOfContext("int a -= b * c;"); 9267 verifyIndependentOfContext("int a *= b * c;"); 9268 verifyIndependentOfContext("int a /= b * c;"); 9269 verifyIndependentOfContext("int a = *b;"); 9270 verifyIndependentOfContext("int a = *b * c;"); 9271 verifyIndependentOfContext("int a = b * *c;"); 9272 verifyIndependentOfContext("int a = b * (10);"); 9273 verifyIndependentOfContext("S << b * (10);"); 9274 verifyIndependentOfContext("return 10 * b;"); 9275 verifyIndependentOfContext("return *b * *c;"); 9276 verifyIndependentOfContext("return a & ~b;"); 9277 verifyIndependentOfContext("f(b ? *c : *d);"); 9278 verifyIndependentOfContext("int a = b ? *c : *d;"); 9279 verifyIndependentOfContext("*b = a;"); 9280 verifyIndependentOfContext("a * ~b;"); 9281 verifyIndependentOfContext("a * !b;"); 9282 verifyIndependentOfContext("a * +b;"); 9283 verifyIndependentOfContext("a * -b;"); 9284 verifyIndependentOfContext("a * ++b;"); 9285 verifyIndependentOfContext("a * --b;"); 9286 verifyIndependentOfContext("a[4] * b;"); 9287 verifyIndependentOfContext("a[a * a] = 1;"); 9288 verifyIndependentOfContext("f() * b;"); 9289 verifyIndependentOfContext("a * [self dostuff];"); 9290 verifyIndependentOfContext("int x = a * (a + b);"); 9291 verifyIndependentOfContext("(a *)(a + b);"); 9292 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 9293 verifyIndependentOfContext("int *pa = (int *)&a;"); 9294 verifyIndependentOfContext("return sizeof(int **);"); 9295 verifyIndependentOfContext("return sizeof(int ******);"); 9296 verifyIndependentOfContext("return (int **&)a;"); 9297 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 9298 verifyFormat("void f(Type (*parameter)[10]) {}"); 9299 verifyFormat("void f(Type (¶meter)[10]) {}"); 9300 verifyGoogleFormat("return sizeof(int**);"); 9301 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 9302 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 9303 verifyFormat("auto a = [](int **&, int ***) {};"); 9304 verifyFormat("auto PointerBinding = [](const char *S) {};"); 9305 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 9306 verifyFormat("[](const decltype(*a) &value) {}"); 9307 verifyFormat("[](const typeof(*a) &value) {}"); 9308 verifyFormat("[](const _Atomic(a *) &value) {}"); 9309 verifyFormat("[](const __underlying_type(a) &value) {}"); 9310 verifyFormat("decltype(a * b) F();"); 9311 verifyFormat("typeof(a * b) F();"); 9312 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 9313 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 9314 verifyIndependentOfContext("typedef void (*f)(int *a);"); 9315 verifyIndependentOfContext("int i{a * b};"); 9316 verifyIndependentOfContext("aaa && aaa->f();"); 9317 verifyIndependentOfContext("int x = ~*p;"); 9318 verifyFormat("Constructor() : a(a), area(width * height) {}"); 9319 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 9320 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 9321 verifyFormat("void f() { f(a, c * d); }"); 9322 verifyFormat("void f() { f(new a(), c * d); }"); 9323 verifyFormat("void f(const MyOverride &override);"); 9324 verifyFormat("void f(const MyFinal &final);"); 9325 verifyIndependentOfContext("bool a = f() && override.f();"); 9326 verifyIndependentOfContext("bool a = f() && final.f();"); 9327 9328 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 9329 9330 verifyIndependentOfContext("A<int *> a;"); 9331 verifyIndependentOfContext("A<int **> a;"); 9332 verifyIndependentOfContext("A<int *, int *> a;"); 9333 verifyIndependentOfContext("A<int *[]> a;"); 9334 verifyIndependentOfContext( 9335 "const char *const p = reinterpret_cast<const char *const>(q);"); 9336 verifyIndependentOfContext("A<int **, int **> a;"); 9337 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 9338 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 9339 verifyFormat("for (; a && b;) {\n}"); 9340 verifyFormat("bool foo = true && [] { return false; }();"); 9341 9342 verifyFormat( 9343 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9344 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9345 9346 verifyGoogleFormat("int const* a = &b;"); 9347 verifyGoogleFormat("**outparam = 1;"); 9348 verifyGoogleFormat("*outparam = a * b;"); 9349 verifyGoogleFormat("int main(int argc, char** argv) {}"); 9350 verifyGoogleFormat("A<int*> a;"); 9351 verifyGoogleFormat("A<int**> a;"); 9352 verifyGoogleFormat("A<int*, int*> a;"); 9353 verifyGoogleFormat("A<int**, int**> a;"); 9354 verifyGoogleFormat("f(b ? *c : *d);"); 9355 verifyGoogleFormat("int a = b ? *c : *d;"); 9356 verifyGoogleFormat("Type* t = **x;"); 9357 verifyGoogleFormat("Type* t = *++*x;"); 9358 verifyGoogleFormat("*++*x;"); 9359 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 9360 verifyGoogleFormat("Type* t = x++ * y;"); 9361 verifyGoogleFormat( 9362 "const char* const p = reinterpret_cast<const char* const>(q);"); 9363 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 9364 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 9365 verifyGoogleFormat("template <typename T>\n" 9366 "void f(int i = 0, SomeType** temps = NULL);"); 9367 9368 FormatStyle Left = getLLVMStyle(); 9369 Left.PointerAlignment = FormatStyle::PAS_Left; 9370 verifyFormat("x = *a(x) = *a(y);", Left); 9371 verifyFormat("for (;; *a = b) {\n}", Left); 9372 verifyFormat("return *this += 1;", Left); 9373 verifyFormat("throw *x;", Left); 9374 verifyFormat("delete *x;", Left); 9375 verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left); 9376 verifyFormat("[](const decltype(*a)* ptr) {}", Left); 9377 verifyFormat("[](const typeof(*a)* ptr) {}", Left); 9378 verifyFormat("[](const _Atomic(a*)* ptr) {}", Left); 9379 verifyFormat("[](const __underlying_type(a)* ptr) {}", Left); 9380 verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left); 9381 verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left); 9382 verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left); 9383 verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left); 9384 9385 verifyIndependentOfContext("a = *(x + y);"); 9386 verifyIndependentOfContext("a = &(x + y);"); 9387 verifyIndependentOfContext("*(x + y).call();"); 9388 verifyIndependentOfContext("&(x + y)->call();"); 9389 verifyFormat("void f() { &(*I).first; }"); 9390 9391 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 9392 verifyFormat( 9393 "int *MyValues = {\n" 9394 " *A, // Operator detection might be confused by the '{'\n" 9395 " *BB // Operator detection might be confused by previous comment\n" 9396 "};"); 9397 9398 verifyIndependentOfContext("if (int *a = &b)"); 9399 verifyIndependentOfContext("if (int &a = *b)"); 9400 verifyIndependentOfContext("if (a & b[i])"); 9401 verifyIndependentOfContext("if constexpr (a & b[i])"); 9402 verifyIndependentOfContext("if CONSTEXPR (a & b[i])"); 9403 verifyIndependentOfContext("if (a * (b * c))"); 9404 verifyIndependentOfContext("if constexpr (a * (b * c))"); 9405 verifyIndependentOfContext("if CONSTEXPR (a * (b * c))"); 9406 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 9407 verifyIndependentOfContext("if (*b[i])"); 9408 verifyIndependentOfContext("if (int *a = (&b))"); 9409 verifyIndependentOfContext("while (int *a = &b)"); 9410 verifyIndependentOfContext("while (a * (b * c))"); 9411 verifyIndependentOfContext("size = sizeof *a;"); 9412 verifyIndependentOfContext("if (a && (b = c))"); 9413 verifyFormat("void f() {\n" 9414 " for (const int &v : Values) {\n" 9415 " }\n" 9416 "}"); 9417 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 9418 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 9419 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 9420 9421 verifyFormat("#define A (!a * b)"); 9422 verifyFormat("#define MACRO \\\n" 9423 " int *i = a * b; \\\n" 9424 " void f(a *b);", 9425 getLLVMStyleWithColumns(19)); 9426 9427 verifyIndependentOfContext("A = new SomeType *[Length];"); 9428 verifyIndependentOfContext("A = new SomeType *[Length]();"); 9429 verifyIndependentOfContext("T **t = new T *;"); 9430 verifyIndependentOfContext("T **t = new T *();"); 9431 verifyGoogleFormat("A = new SomeType*[Length]();"); 9432 verifyGoogleFormat("A = new SomeType*[Length];"); 9433 verifyGoogleFormat("T** t = new T*;"); 9434 verifyGoogleFormat("T** t = new T*();"); 9435 9436 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 9437 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 9438 verifyFormat("template <bool a, bool b> " 9439 "typename t::if<x && y>::type f() {}"); 9440 verifyFormat("template <int *y> f() {}"); 9441 verifyFormat("vector<int *> v;"); 9442 verifyFormat("vector<int *const> v;"); 9443 verifyFormat("vector<int *const **const *> v;"); 9444 verifyFormat("vector<int *volatile> v;"); 9445 verifyFormat("vector<a *_Nonnull> v;"); 9446 verifyFormat("vector<a *_Nullable> v;"); 9447 verifyFormat("vector<a *_Null_unspecified> v;"); 9448 verifyFormat("vector<a *__ptr32> v;"); 9449 verifyFormat("vector<a *__ptr64> v;"); 9450 verifyFormat("vector<a *__capability> v;"); 9451 FormatStyle TypeMacros = getLLVMStyle(); 9452 TypeMacros.TypenameMacros = {"LIST"}; 9453 verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros); 9454 verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros); 9455 verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros); 9456 verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros); 9457 verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros); // multiplication 9458 9459 FormatStyle CustomQualifier = getLLVMStyle(); 9460 // Add identifiers that should not be parsed as a qualifier by default. 9461 CustomQualifier.AttributeMacros.push_back("__my_qualifier"); 9462 CustomQualifier.AttributeMacros.push_back("_My_qualifier"); 9463 CustomQualifier.AttributeMacros.push_back("my_other_qualifier"); 9464 verifyFormat("vector<a * __my_qualifier> parse_as_multiply;"); 9465 verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier); 9466 verifyFormat("vector<a * _My_qualifier> parse_as_multiply;"); 9467 verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier); 9468 verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;"); 9469 verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier); 9470 verifyFormat("vector<a * _NotAQualifier> v;"); 9471 verifyFormat("vector<a * __not_a_qualifier> v;"); 9472 verifyFormat("vector<a * b> v;"); 9473 verifyFormat("foo<b && false>();"); 9474 verifyFormat("foo<b & 1>();"); 9475 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 9476 verifyFormat("typeof(*::std::declval<const T &>()) void F();"); 9477 verifyFormat("_Atomic(*::std::declval<const T &>()) void F();"); 9478 verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();"); 9479 verifyFormat( 9480 "template <class T, class = typename std::enable_if<\n" 9481 " std::is_integral<T>::value &&\n" 9482 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 9483 "void F();", 9484 getLLVMStyleWithColumns(70)); 9485 verifyFormat("template <class T,\n" 9486 " class = typename std::enable_if<\n" 9487 " std::is_integral<T>::value &&\n" 9488 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n" 9489 " class U>\n" 9490 "void F();", 9491 getLLVMStyleWithColumns(70)); 9492 verifyFormat( 9493 "template <class T,\n" 9494 " class = typename ::std::enable_if<\n" 9495 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 9496 "void F();", 9497 getGoogleStyleWithColumns(68)); 9498 9499 verifyIndependentOfContext("MACRO(int *i);"); 9500 verifyIndependentOfContext("MACRO(auto *a);"); 9501 verifyIndependentOfContext("MACRO(const A *a);"); 9502 verifyIndependentOfContext("MACRO(_Atomic(A) *a);"); 9503 verifyIndependentOfContext("MACRO(decltype(A) *a);"); 9504 verifyIndependentOfContext("MACRO(typeof(A) *a);"); 9505 verifyIndependentOfContext("MACRO(__underlying_type(A) *a);"); 9506 verifyIndependentOfContext("MACRO(A *const a);"); 9507 verifyIndependentOfContext("MACRO(A *restrict a);"); 9508 verifyIndependentOfContext("MACRO(A *__restrict__ a);"); 9509 verifyIndependentOfContext("MACRO(A *__restrict a);"); 9510 verifyIndependentOfContext("MACRO(A *volatile a);"); 9511 verifyIndependentOfContext("MACRO(A *__volatile a);"); 9512 verifyIndependentOfContext("MACRO(A *__volatile__ a);"); 9513 verifyIndependentOfContext("MACRO(A *_Nonnull a);"); 9514 verifyIndependentOfContext("MACRO(A *_Nullable a);"); 9515 verifyIndependentOfContext("MACRO(A *_Null_unspecified a);"); 9516 verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);"); 9517 verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);"); 9518 verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);"); 9519 verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);"); 9520 verifyIndependentOfContext("MACRO(A *__ptr32 a);"); 9521 verifyIndependentOfContext("MACRO(A *__ptr64 a);"); 9522 verifyIndependentOfContext("MACRO(A *__capability);"); 9523 verifyIndependentOfContext("MACRO(A &__capability);"); 9524 verifyFormat("MACRO(A *__my_qualifier);"); // type declaration 9525 verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication 9526 // If we add __my_qualifier to AttributeMacros it should always be parsed as 9527 // a type declaration: 9528 verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier); 9529 verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier); 9530 // Also check that TypenameMacros prevents parsing it as multiplication: 9531 verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication 9532 verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros); // type 9533 9534 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 9535 verifyFormat("void f() { f(float{1}, a * a); }"); 9536 verifyFormat("void f() { f(float(1), a * a); }"); 9537 9538 verifyFormat("f((void (*)(int))g);"); 9539 verifyFormat("f((void (&)(int))g);"); 9540 verifyFormat("f((void (^)(int))g);"); 9541 9542 // FIXME: Is there a way to make this work? 9543 // verifyIndependentOfContext("MACRO(A *a);"); 9544 verifyFormat("MACRO(A &B);"); 9545 verifyFormat("MACRO(A *B);"); 9546 verifyFormat("void f() { MACRO(A * B); }"); 9547 verifyFormat("void f() { MACRO(A & B); }"); 9548 9549 // This lambda was mis-formatted after D88956 (treating it as a binop): 9550 verifyFormat("auto x = [](const decltype(x) &ptr) {};"); 9551 verifyFormat("auto x = [](const decltype(x) *ptr) {};"); 9552 verifyFormat("#define lambda [](const decltype(x) &ptr) {}"); 9553 verifyFormat("#define lambda [](const decltype(x) *ptr) {}"); 9554 9555 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 9556 verifyFormat("return options != nullptr && operator==(*options);"); 9557 9558 EXPECT_EQ("#define OP(x) \\\n" 9559 " ostream &operator<<(ostream &s, const A &a) { \\\n" 9560 " return s << a.DebugString(); \\\n" 9561 " }", 9562 format("#define OP(x) \\\n" 9563 " ostream &operator<<(ostream &s, const A &a) { \\\n" 9564 " return s << a.DebugString(); \\\n" 9565 " }", 9566 getLLVMStyleWithColumns(50))); 9567 9568 // FIXME: We cannot handle this case yet; we might be able to figure out that 9569 // foo<x> d > v; doesn't make sense. 9570 verifyFormat("foo<a<b && c> d> v;"); 9571 9572 FormatStyle PointerMiddle = getLLVMStyle(); 9573 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 9574 verifyFormat("delete *x;", PointerMiddle); 9575 verifyFormat("int * x;", PointerMiddle); 9576 verifyFormat("int *[] x;", PointerMiddle); 9577 verifyFormat("template <int * y> f() {}", PointerMiddle); 9578 verifyFormat("int * f(int * a) {}", PointerMiddle); 9579 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 9580 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 9581 verifyFormat("A<int *> a;", PointerMiddle); 9582 verifyFormat("A<int **> a;", PointerMiddle); 9583 verifyFormat("A<int *, int *> a;", PointerMiddle); 9584 verifyFormat("A<int *[]> a;", PointerMiddle); 9585 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 9586 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 9587 verifyFormat("T ** t = new T *;", PointerMiddle); 9588 9589 // Member function reference qualifiers aren't binary operators. 9590 verifyFormat("string // break\n" 9591 "operator()() & {}"); 9592 verifyFormat("string // break\n" 9593 "operator()() && {}"); 9594 verifyGoogleFormat("template <typename T>\n" 9595 "auto x() & -> int {}"); 9596 9597 // Should be binary operators when used as an argument expression (overloaded 9598 // operator invoked as a member function). 9599 verifyFormat("void f() { a.operator()(a * a); }"); 9600 verifyFormat("void f() { a->operator()(a & a); }"); 9601 verifyFormat("void f() { a.operator()(*a & *a); }"); 9602 verifyFormat("void f() { a->operator()(*a * *a); }"); 9603 } 9604 9605 TEST_F(FormatTest, UnderstandsAttributes) { 9606 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 9607 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 9608 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 9609 FormatStyle AfterType = getLLVMStyle(); 9610 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 9611 verifyFormat("__attribute__((nodebug)) void\n" 9612 "foo() {}\n", 9613 AfterType); 9614 verifyFormat("__unused void\n" 9615 "foo() {}", 9616 AfterType); 9617 9618 FormatStyle CustomAttrs = getLLVMStyle(); 9619 CustomAttrs.AttributeMacros.push_back("__unused"); 9620 CustomAttrs.AttributeMacros.push_back("__attr1"); 9621 CustomAttrs.AttributeMacros.push_back("__attr2"); 9622 CustomAttrs.AttributeMacros.push_back("no_underscore_attr"); 9623 verifyFormat("vector<SomeType *__attribute((foo))> v;"); 9624 verifyFormat("vector<SomeType *__attribute__((foo))> v;"); 9625 verifyFormat("vector<SomeType * __not_attribute__((foo))> v;"); 9626 // Check that it is parsed as a multiplication without AttributeMacros and 9627 // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros. 9628 verifyFormat("vector<SomeType * __attr1> v;"); 9629 verifyFormat("vector<SomeType __attr1 *> v;"); 9630 verifyFormat("vector<SomeType __attr1 *const> v;"); 9631 verifyFormat("vector<SomeType __attr1 * __attr2> v;"); 9632 verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs); 9633 verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs); 9634 verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs); 9635 verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs); 9636 verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs); 9637 verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs); 9638 verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs); 9639 9640 // Check that these are not parsed as function declarations: 9641 CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 9642 CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman; 9643 verifyFormat("SomeType s(InitValue);", CustomAttrs); 9644 verifyFormat("SomeType s{InitValue};", CustomAttrs); 9645 verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs); 9646 verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs); 9647 verifyFormat("SomeType s __unused(InitValue);", CustomAttrs); 9648 verifyFormat("SomeType s __unused{InitValue};", CustomAttrs); 9649 verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs); 9650 verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs); 9651 } 9652 9653 TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) { 9654 // Check that qualifiers on pointers don't break parsing of casts. 9655 verifyFormat("x = (foo *const)*v;"); 9656 verifyFormat("x = (foo *volatile)*v;"); 9657 verifyFormat("x = (foo *restrict)*v;"); 9658 verifyFormat("x = (foo *__attribute__((foo)))*v;"); 9659 verifyFormat("x = (foo *_Nonnull)*v;"); 9660 verifyFormat("x = (foo *_Nullable)*v;"); 9661 verifyFormat("x = (foo *_Null_unspecified)*v;"); 9662 verifyFormat("x = (foo *_Nonnull)*v;"); 9663 verifyFormat("x = (foo *[[clang::attr]])*v;"); 9664 verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;"); 9665 verifyFormat("x = (foo *__ptr32)*v;"); 9666 verifyFormat("x = (foo *__ptr64)*v;"); 9667 verifyFormat("x = (foo *__capability)*v;"); 9668 9669 // Check that we handle multiple trailing qualifiers and skip them all to 9670 // determine that the expression is a cast to a pointer type. 9671 FormatStyle LongPointerRight = getLLVMStyleWithColumns(999); 9672 FormatStyle LongPointerLeft = getLLVMStyleWithColumns(999); 9673 LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left; 9674 StringRef AllQualifiers = 9675 "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified " 9676 "_Nonnull [[clang::attr]] __ptr32 __ptr64 __capability"; 9677 verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight); 9678 verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft); 9679 9680 // Also check that address-of is not parsed as a binary bitwise-and: 9681 verifyFormat("x = (foo *const)&v;"); 9682 verifyFormat(("x = (foo *" + AllQualifiers + ")&v;").str(), LongPointerRight); 9683 verifyFormat(("x = (foo* " + AllQualifiers + ")&v;").str(), LongPointerLeft); 9684 9685 // Check custom qualifiers: 9686 FormatStyle CustomQualifier = getLLVMStyleWithColumns(999); 9687 CustomQualifier.AttributeMacros.push_back("__my_qualifier"); 9688 verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier. 9689 verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier); 9690 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)*v;").str(), 9691 CustomQualifier); 9692 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)&v;").str(), 9693 CustomQualifier); 9694 9695 // Check that unknown identifiers result in binary operator parsing: 9696 verifyFormat("x = (foo * __unknown_qualifier) * v;"); 9697 verifyFormat("x = (foo * __unknown_qualifier) & v;"); 9698 } 9699 9700 TEST_F(FormatTest, UnderstandsSquareAttributes) { 9701 verifyFormat("SomeType s [[unused]] (InitValue);"); 9702 verifyFormat("SomeType s [[gnu::unused]] (InitValue);"); 9703 verifyFormat("SomeType s [[using gnu: unused]] (InitValue);"); 9704 verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}"); 9705 verifyFormat("void f() [[deprecated(\"so sorry\")]];"); 9706 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9707 " [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 9708 verifyFormat("[[nodiscard]] bool f() { return false; }"); 9709 verifyFormat("class [[nodiscard]] f {\npublic:\n f() {}\n}"); 9710 verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n f() {}\n}"); 9711 verifyFormat("class [[gnu::unused]] f {\npublic:\n f() {}\n}"); 9712 9713 // Make sure we do not mistake attributes for array subscripts. 9714 verifyFormat("int a() {}\n" 9715 "[[unused]] int b() {}\n"); 9716 verifyFormat("NSArray *arr;\n" 9717 "arr[[Foo() bar]];"); 9718 9719 // On the other hand, we still need to correctly find array subscripts. 9720 verifyFormat("int a = std::vector<int>{1, 2, 3}[0];"); 9721 9722 // Make sure that we do not mistake Objective-C method inside array literals 9723 // as attributes, even if those method names are also keywords. 9724 verifyFormat("@[ [foo bar] ];"); 9725 verifyFormat("@[ [NSArray class] ];"); 9726 verifyFormat("@[ [foo enum] ];"); 9727 9728 verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }"); 9729 9730 // Make sure we do not parse attributes as lambda introducers. 9731 FormatStyle MultiLineFunctions = getLLVMStyle(); 9732 MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 9733 verifyFormat("[[unused]] int b() {\n" 9734 " return 42;\n" 9735 "}\n", 9736 MultiLineFunctions); 9737 } 9738 9739 TEST_F(FormatTest, AttributeClass) { 9740 FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp); 9741 verifyFormat("class S {\n" 9742 " S(S&&) = default;\n" 9743 "};", 9744 Style); 9745 verifyFormat("class [[nodiscard]] S {\n" 9746 " S(S&&) = default;\n" 9747 "};", 9748 Style); 9749 verifyFormat("class __attribute((maybeunused)) S {\n" 9750 " S(S&&) = default;\n" 9751 "};", 9752 Style); 9753 verifyFormat("struct S {\n" 9754 " S(S&&) = default;\n" 9755 "};", 9756 Style); 9757 verifyFormat("struct [[nodiscard]] S {\n" 9758 " S(S&&) = default;\n" 9759 "};", 9760 Style); 9761 } 9762 9763 TEST_F(FormatTest, AttributesAfterMacro) { 9764 FormatStyle Style = getLLVMStyle(); 9765 verifyFormat("MACRO;\n" 9766 "__attribute__((maybe_unused)) int foo() {\n" 9767 " //...\n" 9768 "}"); 9769 9770 verifyFormat("MACRO;\n" 9771 "[[nodiscard]] int foo() {\n" 9772 " //...\n" 9773 "}"); 9774 9775 EXPECT_EQ("MACRO\n\n" 9776 "__attribute__((maybe_unused)) int foo() {\n" 9777 " //...\n" 9778 "}", 9779 format("MACRO\n\n" 9780 "__attribute__((maybe_unused)) int foo() {\n" 9781 " //...\n" 9782 "}")); 9783 9784 EXPECT_EQ("MACRO\n\n" 9785 "[[nodiscard]] int foo() {\n" 9786 " //...\n" 9787 "}", 9788 format("MACRO\n\n" 9789 "[[nodiscard]] int foo() {\n" 9790 " //...\n" 9791 "}")); 9792 } 9793 9794 TEST_F(FormatTest, AttributePenaltyBreaking) { 9795 FormatStyle Style = getLLVMStyle(); 9796 verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n" 9797 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", 9798 Style); 9799 verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n" 9800 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", 9801 Style); 9802 verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const " 9803 "shared_ptr<ALongTypeName> &C d) {\n}", 9804 Style); 9805 } 9806 9807 TEST_F(FormatTest, UnderstandsEllipsis) { 9808 FormatStyle Style = getLLVMStyle(); 9809 verifyFormat("int printf(const char *fmt, ...);"); 9810 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 9811 verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}"); 9812 9813 verifyFormat("template <int *...PP> a;", Style); 9814 9815 Style.PointerAlignment = FormatStyle::PAS_Left; 9816 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style); 9817 9818 verifyFormat("template <int*... PP> a;", Style); 9819 9820 Style.PointerAlignment = FormatStyle::PAS_Middle; 9821 verifyFormat("template <int *... PP> a;", Style); 9822 } 9823 9824 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 9825 EXPECT_EQ("int *a;\n" 9826 "int *a;\n" 9827 "int *a;", 9828 format("int *a;\n" 9829 "int* a;\n" 9830 "int *a;", 9831 getGoogleStyle())); 9832 EXPECT_EQ("int* a;\n" 9833 "int* a;\n" 9834 "int* a;", 9835 format("int* a;\n" 9836 "int* a;\n" 9837 "int *a;", 9838 getGoogleStyle())); 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("auto x = [] {\n" 9847 " int *a;\n" 9848 " int *a;\n" 9849 " int *a;\n" 9850 "};", 9851 format("auto x=[]{int *a;\n" 9852 "int * a;\n" 9853 "int * a;};", 9854 getGoogleStyle())); 9855 } 9856 9857 TEST_F(FormatTest, UnderstandsRvalueReferences) { 9858 verifyFormat("int f(int &&a) {}"); 9859 verifyFormat("int f(int a, char &&b) {}"); 9860 verifyFormat("void f() { int &&a = b; }"); 9861 verifyGoogleFormat("int f(int a, char&& b) {}"); 9862 verifyGoogleFormat("void f() { int&& a = b; }"); 9863 9864 verifyIndependentOfContext("A<int &&> a;"); 9865 verifyIndependentOfContext("A<int &&, int &&> a;"); 9866 verifyGoogleFormat("A<int&&> a;"); 9867 verifyGoogleFormat("A<int&&, int&&> a;"); 9868 9869 // Not rvalue references: 9870 verifyFormat("template <bool B, bool C> class A {\n" 9871 " static_assert(B && C, \"Something is wrong\");\n" 9872 "};"); 9873 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 9874 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 9875 verifyFormat("#define A(a, b) (a && b)"); 9876 } 9877 9878 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 9879 verifyFormat("void f() {\n" 9880 " x[aaaaaaaaa -\n" 9881 " b] = 23;\n" 9882 "}", 9883 getLLVMStyleWithColumns(15)); 9884 } 9885 9886 TEST_F(FormatTest, FormatsCasts) { 9887 verifyFormat("Type *A = static_cast<Type *>(P);"); 9888 verifyFormat("Type *A = (Type *)P;"); 9889 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 9890 verifyFormat("int a = (int)(2.0f);"); 9891 verifyFormat("int a = (int)2.0f;"); 9892 verifyFormat("x[(int32)y];"); 9893 verifyFormat("x = (int32)y;"); 9894 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 9895 verifyFormat("int a = (int)*b;"); 9896 verifyFormat("int a = (int)2.0f;"); 9897 verifyFormat("int a = (int)~0;"); 9898 verifyFormat("int a = (int)++a;"); 9899 verifyFormat("int a = (int)sizeof(int);"); 9900 verifyFormat("int a = (int)+2;"); 9901 verifyFormat("my_int a = (my_int)2.0f;"); 9902 verifyFormat("my_int a = (my_int)sizeof(int);"); 9903 verifyFormat("return (my_int)aaa;"); 9904 verifyFormat("#define x ((int)-1)"); 9905 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 9906 verifyFormat("#define p(q) ((int *)&q)"); 9907 verifyFormat("fn(a)(b) + 1;"); 9908 9909 verifyFormat("void f() { my_int a = (my_int)*b; }"); 9910 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 9911 verifyFormat("my_int a = (my_int)~0;"); 9912 verifyFormat("my_int a = (my_int)++a;"); 9913 verifyFormat("my_int a = (my_int)-2;"); 9914 verifyFormat("my_int a = (my_int)1;"); 9915 verifyFormat("my_int a = (my_int *)1;"); 9916 verifyFormat("my_int a = (const my_int)-1;"); 9917 verifyFormat("my_int a = (const my_int *)-1;"); 9918 verifyFormat("my_int a = (my_int)(my_int)-1;"); 9919 verifyFormat("my_int a = (ns::my_int)-2;"); 9920 verifyFormat("case (my_int)ONE:"); 9921 verifyFormat("auto x = (X)this;"); 9922 // Casts in Obj-C style calls used to not be recognized as such. 9923 verifyFormat("int a = [(type*)[((type*)val) arg] arg];", getGoogleStyle()); 9924 9925 // FIXME: single value wrapped with paren will be treated as cast. 9926 verifyFormat("void f(int i = (kValue)*kMask) {}"); 9927 9928 verifyFormat("{ (void)F; }"); 9929 9930 // Don't break after a cast's 9931 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 9932 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 9933 " bbbbbbbbbbbbbbbbbbbbbb);"); 9934 9935 // These are not casts. 9936 verifyFormat("void f(int *) {}"); 9937 verifyFormat("f(foo)->b;"); 9938 verifyFormat("f(foo).b;"); 9939 verifyFormat("f(foo)(b);"); 9940 verifyFormat("f(foo)[b];"); 9941 verifyFormat("[](foo) { return 4; }(bar);"); 9942 verifyFormat("(*funptr)(foo)[4];"); 9943 verifyFormat("funptrs[4](foo)[4];"); 9944 verifyFormat("void f(int *);"); 9945 verifyFormat("void f(int *) = 0;"); 9946 verifyFormat("void f(SmallVector<int>) {}"); 9947 verifyFormat("void f(SmallVector<int>);"); 9948 verifyFormat("void f(SmallVector<int>) = 0;"); 9949 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 9950 verifyFormat("int a = sizeof(int) * b;"); 9951 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 9952 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 9953 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 9954 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 9955 9956 // These are not casts, but at some point were confused with casts. 9957 verifyFormat("virtual void foo(int *) override;"); 9958 verifyFormat("virtual void foo(char &) const;"); 9959 verifyFormat("virtual void foo(int *a, char *) const;"); 9960 verifyFormat("int a = sizeof(int *) + b;"); 9961 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 9962 verifyFormat("bool b = f(g<int>) && c;"); 9963 verifyFormat("typedef void (*f)(int i) func;"); 9964 verifyFormat("void operator++(int) noexcept;"); 9965 verifyFormat("void operator++(int &) noexcept;"); 9966 verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t " 9967 "&) noexcept;"); 9968 verifyFormat( 9969 "void operator delete(std::size_t, const std::nothrow_t &) noexcept;"); 9970 verifyFormat("void operator delete(const std::nothrow_t &) noexcept;"); 9971 verifyFormat("void operator delete(std::nothrow_t &) noexcept;"); 9972 verifyFormat("void operator delete(nothrow_t &) noexcept;"); 9973 verifyFormat("void operator delete(foo &) noexcept;"); 9974 verifyFormat("void operator delete(foo) noexcept;"); 9975 verifyFormat("void operator delete(int) noexcept;"); 9976 verifyFormat("void operator delete(int &) noexcept;"); 9977 verifyFormat("void operator delete(int &) volatile noexcept;"); 9978 verifyFormat("void operator delete(int &) const"); 9979 verifyFormat("void operator delete(int &) = default"); 9980 verifyFormat("void operator delete(int &) = delete"); 9981 verifyFormat("void operator delete(int &) [[noreturn]]"); 9982 verifyFormat("void operator delete(int &) throw();"); 9983 verifyFormat("void operator delete(int &) throw(int);"); 9984 verifyFormat("auto operator delete(int &) -> int;"); 9985 verifyFormat("auto operator delete(int &) override"); 9986 verifyFormat("auto operator delete(int &) final"); 9987 9988 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 9989 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 9990 // FIXME: The indentation here is not ideal. 9991 verifyFormat( 9992 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9993 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 9994 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 9995 } 9996 9997 TEST_F(FormatTest, FormatsFunctionTypes) { 9998 verifyFormat("A<bool()> a;"); 9999 verifyFormat("A<SomeType()> a;"); 10000 verifyFormat("A<void (*)(int, std::string)> a;"); 10001 verifyFormat("A<void *(int)>;"); 10002 verifyFormat("void *(*a)(int *, SomeType *);"); 10003 verifyFormat("int (*func)(void *);"); 10004 verifyFormat("void f() { int (*func)(void *); }"); 10005 verifyFormat("template <class CallbackClass>\n" 10006 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 10007 10008 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 10009 verifyGoogleFormat("void* (*a)(int);"); 10010 verifyGoogleFormat( 10011 "template <class CallbackClass>\n" 10012 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 10013 10014 // Other constructs can look somewhat like function types: 10015 verifyFormat("A<sizeof(*x)> a;"); 10016 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 10017 verifyFormat("some_var = function(*some_pointer_var)[0];"); 10018 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 10019 verifyFormat("int x = f(&h)();"); 10020 verifyFormat("returnsFunction(¶m1, ¶m2)(param);"); 10021 verifyFormat("std::function<\n" 10022 " LooooooooooongTemplatedType<\n" 10023 " SomeType>*(\n" 10024 " LooooooooooooooooongType type)>\n" 10025 " function;", 10026 getGoogleStyleWithColumns(40)); 10027 } 10028 10029 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 10030 verifyFormat("A (*foo_)[6];"); 10031 verifyFormat("vector<int> (*foo_)[6];"); 10032 } 10033 10034 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 10035 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10036 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 10037 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 10038 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 10039 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10040 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 10041 10042 // Different ways of ()-initializiation. 10043 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10044 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 10045 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10046 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 10047 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10048 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 10049 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10050 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 10051 10052 // Lambdas should not confuse the variable declaration heuristic. 10053 verifyFormat("LooooooooooooooooongType\n" 10054 " variable(nullptr, [](A *a) {});", 10055 getLLVMStyleWithColumns(40)); 10056 } 10057 10058 TEST_F(FormatTest, BreaksLongDeclarations) { 10059 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 10060 " AnotherNameForTheLongType;"); 10061 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 10062 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 10063 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10064 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 10065 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 10066 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 10067 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10068 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10069 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 10070 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10071 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 10072 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10073 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 10074 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10075 verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n" 10076 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10077 verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n" 10078 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10079 verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n" 10080 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10081 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10082 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);"); 10083 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10084 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}"); 10085 FormatStyle Indented = getLLVMStyle(); 10086 Indented.IndentWrappedFunctionNames = true; 10087 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10088 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 10089 Indented); 10090 verifyFormat( 10091 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10092 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 10093 Indented); 10094 verifyFormat( 10095 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 10096 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 10097 Indented); 10098 verifyFormat( 10099 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 10100 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 10101 Indented); 10102 10103 // FIXME: Without the comment, this breaks after "(". 10104 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 10105 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 10106 getGoogleStyle()); 10107 10108 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 10109 " int LoooooooooooooooooooongParam2) {}"); 10110 verifyFormat( 10111 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 10112 " SourceLocation L, IdentifierIn *II,\n" 10113 " Type *T) {}"); 10114 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 10115 "ReallyReaaallyLongFunctionName(\n" 10116 " const std::string &SomeParameter,\n" 10117 " const SomeType<string, SomeOtherTemplateParameter>\n" 10118 " &ReallyReallyLongParameterName,\n" 10119 " const SomeType<string, SomeOtherTemplateParameter>\n" 10120 " &AnotherLongParameterName) {}"); 10121 verifyFormat("template <typename A>\n" 10122 "SomeLoooooooooooooooooooooongType<\n" 10123 " typename some_namespace::SomeOtherType<A>::Type>\n" 10124 "Function() {}"); 10125 10126 verifyGoogleFormat( 10127 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 10128 " aaaaaaaaaaaaaaaaaaaaaaa;"); 10129 verifyGoogleFormat( 10130 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 10131 " SourceLocation L) {}"); 10132 verifyGoogleFormat( 10133 "some_namespace::LongReturnType\n" 10134 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 10135 " int first_long_parameter, int second_parameter) {}"); 10136 10137 verifyGoogleFormat("template <typename T>\n" 10138 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 10139 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 10140 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 10141 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 10142 10143 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 10144 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10145 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 10146 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10147 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 10148 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 10149 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10150 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 10151 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 10152 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 10153 10154 verifyFormat("template <typename T> // Templates on own line.\n" 10155 "static int // Some comment.\n" 10156 "MyFunction(int a);", 10157 getLLVMStyle()); 10158 } 10159 10160 TEST_F(FormatTest, FormatsAccessModifiers) { 10161 FormatStyle Style = getLLVMStyle(); 10162 EXPECT_EQ(Style.EmptyLineBeforeAccessModifier, 10163 FormatStyle::ELBAMS_LogicalBlock); 10164 verifyFormat("struct foo {\n" 10165 "private:\n" 10166 " void f() {}\n" 10167 "\n" 10168 "private:\n" 10169 " int i;\n" 10170 "\n" 10171 "protected:\n" 10172 " int j;\n" 10173 "};\n", 10174 Style); 10175 verifyFormat("struct foo {\n" 10176 "private:\n" 10177 " void f() {}\n" 10178 "\n" 10179 "private:\n" 10180 " int i;\n" 10181 "\n" 10182 "protected:\n" 10183 " int j;\n" 10184 "};\n", 10185 "struct foo {\n" 10186 "private:\n" 10187 " void f() {}\n" 10188 "private:\n" 10189 " int i;\n" 10190 "protected:\n" 10191 " int j;\n" 10192 "};\n", 10193 Style); 10194 verifyFormat("struct foo { /* comment */\n" 10195 "private:\n" 10196 " int i;\n" 10197 " // comment\n" 10198 "private:\n" 10199 " int j;\n" 10200 "};\n", 10201 Style); 10202 verifyFormat("struct foo {\n" 10203 "#ifdef FOO\n" 10204 "#endif\n" 10205 "private:\n" 10206 " int i;\n" 10207 "#ifdef FOO\n" 10208 "private:\n" 10209 "#endif\n" 10210 " int j;\n" 10211 "};\n", 10212 Style); 10213 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10214 verifyFormat("struct foo {\n" 10215 "private:\n" 10216 " void f() {}\n" 10217 "private:\n" 10218 " int i;\n" 10219 "protected:\n" 10220 " int j;\n" 10221 "};\n", 10222 Style); 10223 verifyFormat("struct foo {\n" 10224 "private:\n" 10225 " void f() {}\n" 10226 "private:\n" 10227 " int i;\n" 10228 "protected:\n" 10229 " int j;\n" 10230 "};\n", 10231 "struct foo {\n" 10232 "\n" 10233 "private:\n" 10234 " void f() {}\n" 10235 "\n" 10236 "private:\n" 10237 " int i;\n" 10238 "\n" 10239 "protected:\n" 10240 " int j;\n" 10241 "};\n", 10242 Style); 10243 verifyFormat("struct foo { /* comment */\n" 10244 "private:\n" 10245 " int i;\n" 10246 " // comment\n" 10247 "private:\n" 10248 " int j;\n" 10249 "};\n", 10250 "struct foo { /* comment */\n" 10251 "\n" 10252 "private:\n" 10253 " int i;\n" 10254 " // comment\n" 10255 "\n" 10256 "private:\n" 10257 " int j;\n" 10258 "};\n", 10259 Style); 10260 verifyFormat("struct foo {\n" 10261 "#ifdef FOO\n" 10262 "#endif\n" 10263 "private:\n" 10264 " int i;\n" 10265 "#ifdef FOO\n" 10266 "private:\n" 10267 "#endif\n" 10268 " int j;\n" 10269 "};\n", 10270 "struct foo {\n" 10271 "#ifdef FOO\n" 10272 "#endif\n" 10273 "\n" 10274 "private:\n" 10275 " int i;\n" 10276 "#ifdef FOO\n" 10277 "\n" 10278 "private:\n" 10279 "#endif\n" 10280 " int j;\n" 10281 "};\n", 10282 Style); 10283 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10284 verifyFormat("struct foo {\n" 10285 "private:\n" 10286 " void f() {}\n" 10287 "\n" 10288 "private:\n" 10289 " int i;\n" 10290 "\n" 10291 "protected:\n" 10292 " int j;\n" 10293 "};\n", 10294 Style); 10295 verifyFormat("struct foo {\n" 10296 "private:\n" 10297 " void f() {}\n" 10298 "\n" 10299 "private:\n" 10300 " int i;\n" 10301 "\n" 10302 "protected:\n" 10303 " int j;\n" 10304 "};\n", 10305 "struct foo {\n" 10306 "private:\n" 10307 " void f() {}\n" 10308 "private:\n" 10309 " int i;\n" 10310 "protected:\n" 10311 " int j;\n" 10312 "};\n", 10313 Style); 10314 verifyFormat("struct foo { /* comment */\n" 10315 "private:\n" 10316 " int i;\n" 10317 " // comment\n" 10318 "\n" 10319 "private:\n" 10320 " int j;\n" 10321 "};\n", 10322 "struct foo { /* comment */\n" 10323 "private:\n" 10324 " int i;\n" 10325 " // comment\n" 10326 "\n" 10327 "private:\n" 10328 " int j;\n" 10329 "};\n", 10330 Style); 10331 verifyFormat("struct foo {\n" 10332 "#ifdef FOO\n" 10333 "#endif\n" 10334 "\n" 10335 "private:\n" 10336 " int i;\n" 10337 "#ifdef FOO\n" 10338 "\n" 10339 "private:\n" 10340 "#endif\n" 10341 " int j;\n" 10342 "};\n", 10343 "struct foo {\n" 10344 "#ifdef FOO\n" 10345 "#endif\n" 10346 "private:\n" 10347 " int i;\n" 10348 "#ifdef FOO\n" 10349 "private:\n" 10350 "#endif\n" 10351 " int j;\n" 10352 "};\n", 10353 Style); 10354 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 10355 EXPECT_EQ("struct foo {\n" 10356 "\n" 10357 "private:\n" 10358 " void f() {}\n" 10359 "\n" 10360 "private:\n" 10361 " int i;\n" 10362 "\n" 10363 "protected:\n" 10364 " int j;\n" 10365 "};\n", 10366 format("struct foo {\n" 10367 "\n" 10368 "private:\n" 10369 " void f() {}\n" 10370 "\n" 10371 "private:\n" 10372 " int i;\n" 10373 "\n" 10374 "protected:\n" 10375 " int j;\n" 10376 "};\n", 10377 Style)); 10378 verifyFormat("struct foo {\n" 10379 "private:\n" 10380 " void f() {}\n" 10381 "private:\n" 10382 " int i;\n" 10383 "protected:\n" 10384 " int j;\n" 10385 "};\n", 10386 Style); 10387 EXPECT_EQ("struct foo { /* comment */\n" 10388 "\n" 10389 "private:\n" 10390 " int i;\n" 10391 " // comment\n" 10392 "\n" 10393 "private:\n" 10394 " int j;\n" 10395 "};\n", 10396 format("struct foo { /* comment */\n" 10397 "\n" 10398 "private:\n" 10399 " int i;\n" 10400 " // comment\n" 10401 "\n" 10402 "private:\n" 10403 " int j;\n" 10404 "};\n", 10405 Style)); 10406 verifyFormat("struct foo { /* comment */\n" 10407 "private:\n" 10408 " int i;\n" 10409 " // comment\n" 10410 "private:\n" 10411 " int j;\n" 10412 "};\n", 10413 Style); 10414 EXPECT_EQ("struct foo {\n" 10415 "#ifdef FOO\n" 10416 "#endif\n" 10417 "\n" 10418 "private:\n" 10419 " int i;\n" 10420 "#ifdef FOO\n" 10421 "\n" 10422 "private:\n" 10423 "#endif\n" 10424 " int j;\n" 10425 "};\n", 10426 format("struct foo {\n" 10427 "#ifdef FOO\n" 10428 "#endif\n" 10429 "\n" 10430 "private:\n" 10431 " int i;\n" 10432 "#ifdef FOO\n" 10433 "\n" 10434 "private:\n" 10435 "#endif\n" 10436 " int j;\n" 10437 "};\n", 10438 Style)); 10439 verifyFormat("struct foo {\n" 10440 "#ifdef FOO\n" 10441 "#endif\n" 10442 "private:\n" 10443 " int i;\n" 10444 "#ifdef FOO\n" 10445 "private:\n" 10446 "#endif\n" 10447 " int j;\n" 10448 "};\n", 10449 Style); 10450 10451 FormatStyle NoEmptyLines = getLLVMStyle(); 10452 NoEmptyLines.MaxEmptyLinesToKeep = 0; 10453 verifyFormat("struct foo {\n" 10454 "private:\n" 10455 " void f() {}\n" 10456 "\n" 10457 "private:\n" 10458 " int i;\n" 10459 "\n" 10460 "public:\n" 10461 "protected:\n" 10462 " int j;\n" 10463 "};\n", 10464 NoEmptyLines); 10465 10466 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10467 verifyFormat("struct foo {\n" 10468 "private:\n" 10469 " void f() {}\n" 10470 "private:\n" 10471 " int i;\n" 10472 "public:\n" 10473 "protected:\n" 10474 " int j;\n" 10475 "};\n", 10476 NoEmptyLines); 10477 10478 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10479 verifyFormat("struct foo {\n" 10480 "private:\n" 10481 " void f() {}\n" 10482 "\n" 10483 "private:\n" 10484 " int i;\n" 10485 "\n" 10486 "public:\n" 10487 "\n" 10488 "protected:\n" 10489 " int j;\n" 10490 "};\n", 10491 NoEmptyLines); 10492 } 10493 10494 TEST_F(FormatTest, FormatsAfterAccessModifiers) { 10495 10496 FormatStyle Style = getLLVMStyle(); 10497 EXPECT_EQ(Style.EmptyLineAfterAccessModifier, FormatStyle::ELAAMS_Never); 10498 verifyFormat("struct foo {\n" 10499 "private:\n" 10500 " void f() {}\n" 10501 "\n" 10502 "private:\n" 10503 " int i;\n" 10504 "\n" 10505 "protected:\n" 10506 " int j;\n" 10507 "};\n", 10508 Style); 10509 10510 // Check if lines are removed. 10511 verifyFormat("struct foo {\n" 10512 "private:\n" 10513 " void f() {}\n" 10514 "\n" 10515 "private:\n" 10516 " int i;\n" 10517 "\n" 10518 "protected:\n" 10519 " int j;\n" 10520 "};\n", 10521 "struct foo {\n" 10522 "private:\n" 10523 "\n" 10524 " void f() {}\n" 10525 "\n" 10526 "private:\n" 10527 "\n" 10528 " int i;\n" 10529 "\n" 10530 "protected:\n" 10531 "\n" 10532 " int j;\n" 10533 "};\n", 10534 Style); 10535 10536 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10537 verifyFormat("struct foo {\n" 10538 "private:\n" 10539 "\n" 10540 " void f() {}\n" 10541 "\n" 10542 "private:\n" 10543 "\n" 10544 " int i;\n" 10545 "\n" 10546 "protected:\n" 10547 "\n" 10548 " int j;\n" 10549 "};\n", 10550 Style); 10551 10552 // Check if lines are added. 10553 verifyFormat("struct foo {\n" 10554 "private:\n" 10555 "\n" 10556 " void f() {}\n" 10557 "\n" 10558 "private:\n" 10559 "\n" 10560 " int i;\n" 10561 "\n" 10562 "protected:\n" 10563 "\n" 10564 " int j;\n" 10565 "};\n", 10566 "struct foo {\n" 10567 "private:\n" 10568 " void f() {}\n" 10569 "\n" 10570 "private:\n" 10571 " int i;\n" 10572 "\n" 10573 "protected:\n" 10574 " int j;\n" 10575 "};\n", 10576 Style); 10577 10578 // Leave tests rely on the code layout, test::messUp can not be used. 10579 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 10580 Style.MaxEmptyLinesToKeep = 0u; 10581 verifyFormat("struct foo {\n" 10582 "private:\n" 10583 " void f() {}\n" 10584 "\n" 10585 "private:\n" 10586 " int i;\n" 10587 "\n" 10588 "protected:\n" 10589 " int j;\n" 10590 "};\n", 10591 Style); 10592 10593 // Check if MaxEmptyLinesToKeep is respected. 10594 EXPECT_EQ("struct foo {\n" 10595 "private:\n" 10596 " void f() {}\n" 10597 "\n" 10598 "private:\n" 10599 " int i;\n" 10600 "\n" 10601 "protected:\n" 10602 " int j;\n" 10603 "};\n", 10604 format("struct foo {\n" 10605 "private:\n" 10606 "\n\n\n" 10607 " void f() {}\n" 10608 "\n" 10609 "private:\n" 10610 "\n\n\n" 10611 " int i;\n" 10612 "\n" 10613 "protected:\n" 10614 "\n\n\n" 10615 " int j;\n" 10616 "};\n", 10617 Style)); 10618 10619 Style.MaxEmptyLinesToKeep = 1u; 10620 EXPECT_EQ("struct foo {\n" 10621 "private:\n" 10622 "\n" 10623 " void f() {}\n" 10624 "\n" 10625 "private:\n" 10626 "\n" 10627 " int i;\n" 10628 "\n" 10629 "protected:\n" 10630 "\n" 10631 " int j;\n" 10632 "};\n", 10633 format("struct foo {\n" 10634 "private:\n" 10635 "\n" 10636 " void f() {}\n" 10637 "\n" 10638 "private:\n" 10639 "\n" 10640 " int i;\n" 10641 "\n" 10642 "protected:\n" 10643 "\n" 10644 " int j;\n" 10645 "};\n", 10646 Style)); 10647 // Check if no lines are kept. 10648 EXPECT_EQ("struct foo {\n" 10649 "private:\n" 10650 " void f() {}\n" 10651 "\n" 10652 "private:\n" 10653 " int i;\n" 10654 "\n" 10655 "protected:\n" 10656 " int j;\n" 10657 "};\n", 10658 format("struct foo {\n" 10659 "private:\n" 10660 " void f() {}\n" 10661 "\n" 10662 "private:\n" 10663 " int i;\n" 10664 "\n" 10665 "protected:\n" 10666 " int j;\n" 10667 "};\n", 10668 Style)); 10669 // Check if MaxEmptyLinesToKeep is respected. 10670 EXPECT_EQ("struct foo {\n" 10671 "private:\n" 10672 "\n" 10673 " void f() {}\n" 10674 "\n" 10675 "private:\n" 10676 "\n" 10677 " int i;\n" 10678 "\n" 10679 "protected:\n" 10680 "\n" 10681 " int j;\n" 10682 "};\n", 10683 format("struct foo {\n" 10684 "private:\n" 10685 "\n\n\n" 10686 " void f() {}\n" 10687 "\n" 10688 "private:\n" 10689 "\n\n\n" 10690 " int i;\n" 10691 "\n" 10692 "protected:\n" 10693 "\n\n\n" 10694 " int j;\n" 10695 "};\n", 10696 Style)); 10697 10698 Style.MaxEmptyLinesToKeep = 10u; 10699 EXPECT_EQ("struct foo {\n" 10700 "private:\n" 10701 "\n\n\n" 10702 " void f() {}\n" 10703 "\n" 10704 "private:\n" 10705 "\n\n\n" 10706 " int i;\n" 10707 "\n" 10708 "protected:\n" 10709 "\n\n\n" 10710 " int j;\n" 10711 "};\n", 10712 format("struct foo {\n" 10713 "private:\n" 10714 "\n\n\n" 10715 " void f() {}\n" 10716 "\n" 10717 "private:\n" 10718 "\n\n\n" 10719 " int i;\n" 10720 "\n" 10721 "protected:\n" 10722 "\n\n\n" 10723 " int j;\n" 10724 "};\n", 10725 Style)); 10726 10727 // Test with comments. 10728 Style = getLLVMStyle(); 10729 verifyFormat("struct foo {\n" 10730 "private:\n" 10731 " // comment\n" 10732 " void f() {}\n" 10733 "\n" 10734 "private: /* comment */\n" 10735 " int i;\n" 10736 "};\n", 10737 Style); 10738 verifyFormat("struct foo {\n" 10739 "private:\n" 10740 " // comment\n" 10741 " void f() {}\n" 10742 "\n" 10743 "private: /* comment */\n" 10744 " int i;\n" 10745 "};\n", 10746 "struct foo {\n" 10747 "private:\n" 10748 "\n" 10749 " // comment\n" 10750 " void f() {}\n" 10751 "\n" 10752 "private: /* comment */\n" 10753 "\n" 10754 " int i;\n" 10755 "};\n", 10756 Style); 10757 10758 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10759 verifyFormat("struct foo {\n" 10760 "private:\n" 10761 "\n" 10762 " // comment\n" 10763 " void f() {}\n" 10764 "\n" 10765 "private: /* comment */\n" 10766 "\n" 10767 " int i;\n" 10768 "};\n", 10769 "struct foo {\n" 10770 "private:\n" 10771 " // comment\n" 10772 " void f() {}\n" 10773 "\n" 10774 "private: /* comment */\n" 10775 " int i;\n" 10776 "};\n", 10777 Style); 10778 verifyFormat("struct foo {\n" 10779 "private:\n" 10780 "\n" 10781 " // comment\n" 10782 " void f() {}\n" 10783 "\n" 10784 "private: /* comment */\n" 10785 "\n" 10786 " int i;\n" 10787 "};\n", 10788 Style); 10789 10790 // Test with preprocessor defines. 10791 Style = getLLVMStyle(); 10792 verifyFormat("struct foo {\n" 10793 "private:\n" 10794 "#ifdef FOO\n" 10795 "#endif\n" 10796 " void f() {}\n" 10797 "};\n", 10798 Style); 10799 verifyFormat("struct foo {\n" 10800 "private:\n" 10801 "#ifdef FOO\n" 10802 "#endif\n" 10803 " void f() {}\n" 10804 "};\n", 10805 "struct foo {\n" 10806 "private:\n" 10807 "\n" 10808 "#ifdef FOO\n" 10809 "#endif\n" 10810 " void f() {}\n" 10811 "};\n", 10812 Style); 10813 10814 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10815 verifyFormat("struct foo {\n" 10816 "private:\n" 10817 "\n" 10818 "#ifdef FOO\n" 10819 "#endif\n" 10820 " void f() {}\n" 10821 "};\n", 10822 "struct foo {\n" 10823 "private:\n" 10824 "#ifdef FOO\n" 10825 "#endif\n" 10826 " void f() {}\n" 10827 "};\n", 10828 Style); 10829 verifyFormat("struct foo {\n" 10830 "private:\n" 10831 "\n" 10832 "#ifdef FOO\n" 10833 "#endif\n" 10834 " void f() {}\n" 10835 "};\n", 10836 Style); 10837 } 10838 10839 TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) { 10840 // Combined tests of EmptyLineAfterAccessModifier and 10841 // EmptyLineBeforeAccessModifier. 10842 FormatStyle Style = getLLVMStyle(); 10843 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10844 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10845 verifyFormat("struct foo {\n" 10846 "private:\n" 10847 "\n" 10848 "protected:\n" 10849 "};\n", 10850 Style); 10851 10852 Style.MaxEmptyLinesToKeep = 10u; 10853 // Both remove all new lines. 10854 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10855 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 10856 verifyFormat("struct foo {\n" 10857 "private:\n" 10858 "protected:\n" 10859 "};\n", 10860 "struct foo {\n" 10861 "private:\n" 10862 "\n\n\n" 10863 "protected:\n" 10864 "};\n", 10865 Style); 10866 10867 // Leave tests rely on the code layout, test::messUp can not be used. 10868 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 10869 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 10870 Style.MaxEmptyLinesToKeep = 10u; 10871 EXPECT_EQ("struct foo {\n" 10872 "private:\n" 10873 "\n\n\n" 10874 "protected:\n" 10875 "};\n", 10876 format("struct foo {\n" 10877 "private:\n" 10878 "\n\n\n" 10879 "protected:\n" 10880 "};\n", 10881 Style)); 10882 Style.MaxEmptyLinesToKeep = 3u; 10883 EXPECT_EQ("struct foo {\n" 10884 "private:\n" 10885 "\n\n\n" 10886 "protected:\n" 10887 "};\n", 10888 format("struct foo {\n" 10889 "private:\n" 10890 "\n\n\n" 10891 "protected:\n" 10892 "};\n", 10893 Style)); 10894 Style.MaxEmptyLinesToKeep = 1u; 10895 EXPECT_EQ("struct foo {\n" 10896 "private:\n" 10897 "\n\n\n" 10898 "protected:\n" 10899 "};\n", 10900 format("struct foo {\n" 10901 "private:\n" 10902 "\n\n\n" 10903 "protected:\n" 10904 "};\n", 10905 Style)); // Based on new lines in original document and not 10906 // on the setting. 10907 10908 Style.MaxEmptyLinesToKeep = 10u; 10909 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10910 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 10911 // Newlines are kept if they are greater than zero, 10912 // test::messUp removes all new lines which changes the logic 10913 EXPECT_EQ("struct foo {\n" 10914 "private:\n" 10915 "\n\n\n" 10916 "protected:\n" 10917 "};\n", 10918 format("struct foo {\n" 10919 "private:\n" 10920 "\n\n\n" 10921 "protected:\n" 10922 "};\n", 10923 Style)); 10924 10925 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 10926 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10927 // test::messUp removes all new lines which changes the logic 10928 EXPECT_EQ("struct foo {\n" 10929 "private:\n" 10930 "\n\n\n" 10931 "protected:\n" 10932 "};\n", 10933 format("struct foo {\n" 10934 "private:\n" 10935 "\n\n\n" 10936 "protected:\n" 10937 "};\n", 10938 Style)); 10939 10940 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 10941 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 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)); // test::messUp removes all new lines which changes 10953 // the logic. 10954 10955 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10956 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 10957 verifyFormat("struct foo {\n" 10958 "private:\n" 10959 "protected:\n" 10960 "};\n", 10961 "struct foo {\n" 10962 "private:\n" 10963 "\n\n\n" 10964 "protected:\n" 10965 "};\n", 10966 Style); 10967 10968 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10969 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 10970 EXPECT_EQ("struct foo {\n" 10971 "private:\n" 10972 "\n\n\n" 10973 "protected:\n" 10974 "};\n", 10975 format("struct foo {\n" 10976 "private:\n" 10977 "\n\n\n" 10978 "protected:\n" 10979 "};\n", 10980 Style)); // test::messUp removes all new lines which changes 10981 // the logic. 10982 10983 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10984 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10985 verifyFormat("struct foo {\n" 10986 "private:\n" 10987 "protected:\n" 10988 "};\n", 10989 "struct foo {\n" 10990 "private:\n" 10991 "\n\n\n" 10992 "protected:\n" 10993 "};\n", 10994 Style); 10995 10996 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 10997 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10998 verifyFormat("struct foo {\n" 10999 "private:\n" 11000 "protected:\n" 11001 "};\n", 11002 "struct foo {\n" 11003 "private:\n" 11004 "\n\n\n" 11005 "protected:\n" 11006 "};\n", 11007 Style); 11008 11009 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 11010 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 11011 verifyFormat("struct foo {\n" 11012 "private:\n" 11013 "protected:\n" 11014 "};\n", 11015 "struct foo {\n" 11016 "private:\n" 11017 "\n\n\n" 11018 "protected:\n" 11019 "};\n", 11020 Style); 11021 11022 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 11023 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 11024 verifyFormat("struct foo {\n" 11025 "private:\n" 11026 "protected:\n" 11027 "};\n", 11028 "struct foo {\n" 11029 "private:\n" 11030 "\n\n\n" 11031 "protected:\n" 11032 "};\n", 11033 Style); 11034 } 11035 11036 TEST_F(FormatTest, FormatsArrays) { 11037 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 11038 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 11039 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 11040 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 11041 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 11042 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 11043 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11044 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 11045 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11046 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 11047 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11048 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 11049 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 11050 verifyFormat( 11051 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 11052 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 11053 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 11054 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" 11055 " .aaaaaaaaaaaaaaaaaaaaaa();"); 11056 11057 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 11058 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 11059 verifyFormat( 11060 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 11061 " .aaaaaaa[0]\n" 11062 " .aaaaaaaaaaaaaaaaaaaaaa();"); 11063 verifyFormat("a[::b::c];"); 11064 11065 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 11066 11067 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 11068 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 11069 } 11070 11071 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 11072 verifyFormat("(a)->b();"); 11073 verifyFormat("--a;"); 11074 } 11075 11076 TEST_F(FormatTest, HandlesIncludeDirectives) { 11077 verifyFormat("#include <string>\n" 11078 "#include <a/b/c.h>\n" 11079 "#include \"a/b/string\"\n" 11080 "#include \"string.h\"\n" 11081 "#include \"string.h\"\n" 11082 "#include <a-a>\n" 11083 "#include < path with space >\n" 11084 "#include_next <test.h>" 11085 "#include \"abc.h\" // this is included for ABC\n" 11086 "#include \"some long include\" // with a comment\n" 11087 "#include \"some very long include path\"\n" 11088 "#include <some/very/long/include/path>\n", 11089 getLLVMStyleWithColumns(35)); 11090 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 11091 EXPECT_EQ("#include <a>", format("#include<a>")); 11092 11093 verifyFormat("#import <string>"); 11094 verifyFormat("#import <a/b/c.h>"); 11095 verifyFormat("#import \"a/b/string\""); 11096 verifyFormat("#import \"string.h\""); 11097 verifyFormat("#import \"string.h\""); 11098 verifyFormat("#if __has_include(<strstream>)\n" 11099 "#include <strstream>\n" 11100 "#endif"); 11101 11102 verifyFormat("#define MY_IMPORT <a/b>"); 11103 11104 verifyFormat("#if __has_include(<a/b>)"); 11105 verifyFormat("#if __has_include_next(<a/b>)"); 11106 verifyFormat("#define F __has_include(<a/b>)"); 11107 verifyFormat("#define F __has_include_next(<a/b>)"); 11108 11109 // Protocol buffer definition or missing "#". 11110 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 11111 getLLVMStyleWithColumns(30)); 11112 11113 FormatStyle Style = getLLVMStyle(); 11114 Style.AlwaysBreakBeforeMultilineStrings = true; 11115 Style.ColumnLimit = 0; 11116 verifyFormat("#import \"abc.h\"", Style); 11117 11118 // But 'import' might also be a regular C++ namespace. 11119 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11120 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 11121 } 11122 11123 //===----------------------------------------------------------------------===// 11124 // Error recovery tests. 11125 //===----------------------------------------------------------------------===// 11126 11127 TEST_F(FormatTest, IncompleteParameterLists) { 11128 FormatStyle NoBinPacking = getLLVMStyle(); 11129 NoBinPacking.BinPackParameters = false; 11130 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 11131 " double *min_x,\n" 11132 " double *max_x,\n" 11133 " double *min_y,\n" 11134 " double *max_y,\n" 11135 " double *min_z,\n" 11136 " double *max_z, ) {}", 11137 NoBinPacking); 11138 } 11139 11140 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 11141 verifyFormat("void f() { return; }\n42"); 11142 verifyFormat("void f() {\n" 11143 " if (0)\n" 11144 " return;\n" 11145 "}\n" 11146 "42"); 11147 verifyFormat("void f() { return }\n42"); 11148 verifyFormat("void f() {\n" 11149 " if (0)\n" 11150 " return\n" 11151 "}\n" 11152 "42"); 11153 } 11154 11155 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 11156 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 11157 EXPECT_EQ("void f() {\n" 11158 " if (a)\n" 11159 " return\n" 11160 "}", 11161 format("void f ( ) { if ( a ) return }")); 11162 EXPECT_EQ("namespace N {\n" 11163 "void f()\n" 11164 "}", 11165 format("namespace N { void f() }")); 11166 EXPECT_EQ("namespace N {\n" 11167 "void f() {}\n" 11168 "void g()\n" 11169 "} // namespace N", 11170 format("namespace N { void f( ) { } void g( ) }")); 11171 } 11172 11173 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 11174 verifyFormat("int aaaaaaaa =\n" 11175 " // Overlylongcomment\n" 11176 " b;", 11177 getLLVMStyleWithColumns(20)); 11178 verifyFormat("function(\n" 11179 " ShortArgument,\n" 11180 " LoooooooooooongArgument);\n", 11181 getLLVMStyleWithColumns(20)); 11182 } 11183 11184 TEST_F(FormatTest, IncorrectAccessSpecifier) { 11185 verifyFormat("public:"); 11186 verifyFormat("class A {\n" 11187 "public\n" 11188 " void f() {}\n" 11189 "};"); 11190 verifyFormat("public\n" 11191 "int qwerty;"); 11192 verifyFormat("public\n" 11193 "B {}"); 11194 verifyFormat("public\n" 11195 "{}"); 11196 verifyFormat("public\n" 11197 "B { int x; }"); 11198 } 11199 11200 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 11201 verifyFormat("{"); 11202 verifyFormat("#})"); 11203 verifyNoCrash("(/**/[:!] ?[)."); 11204 } 11205 11206 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) { 11207 // Found by oss-fuzz: 11208 // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212 11209 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 11210 Style.ColumnLimit = 60; 11211 verifyNoCrash( 11212 "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20" 11213 "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20" 11214 "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a", 11215 Style); 11216 } 11217 11218 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 11219 verifyFormat("do {\n}"); 11220 verifyFormat("do {\n}\n" 11221 "f();"); 11222 verifyFormat("do {\n}\n" 11223 "wheeee(fun);"); 11224 verifyFormat("do {\n" 11225 " f();\n" 11226 "}"); 11227 } 11228 11229 TEST_F(FormatTest, IncorrectCodeMissingParens) { 11230 verifyFormat("if {\n foo;\n foo();\n}"); 11231 verifyFormat("switch {\n foo;\n foo();\n}"); 11232 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 11233 verifyFormat("while {\n foo;\n foo();\n}"); 11234 verifyFormat("do {\n foo;\n foo();\n} while;"); 11235 } 11236 11237 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 11238 verifyIncompleteFormat("namespace {\n" 11239 "class Foo { Foo (\n" 11240 "};\n" 11241 "} // namespace"); 11242 } 11243 11244 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 11245 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 11246 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 11247 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 11248 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 11249 11250 EXPECT_EQ("{\n" 11251 " {\n" 11252 " breakme(\n" 11253 " qwe);\n" 11254 " }\n", 11255 format("{\n" 11256 " {\n" 11257 " breakme(qwe);\n" 11258 "}\n", 11259 getLLVMStyleWithColumns(10))); 11260 } 11261 11262 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 11263 verifyFormat("int x = {\n" 11264 " avariable,\n" 11265 " b(alongervariable)};", 11266 getLLVMStyleWithColumns(25)); 11267 } 11268 11269 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 11270 verifyFormat("return (a)(b){1, 2, 3};"); 11271 } 11272 11273 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 11274 verifyFormat("vector<int> x{1, 2, 3, 4};"); 11275 verifyFormat("vector<int> x{\n" 11276 " 1,\n" 11277 " 2,\n" 11278 " 3,\n" 11279 " 4,\n" 11280 "};"); 11281 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 11282 verifyFormat("f({1, 2});"); 11283 verifyFormat("auto v = Foo{-1};"); 11284 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 11285 verifyFormat("Class::Class : member{1, 2, 3} {}"); 11286 verifyFormat("new vector<int>{1, 2, 3};"); 11287 verifyFormat("new int[3]{1, 2, 3};"); 11288 verifyFormat("new int{1};"); 11289 verifyFormat("return {arg1, arg2};"); 11290 verifyFormat("return {arg1, SomeType{parameter}};"); 11291 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 11292 verifyFormat("new T{arg1, arg2};"); 11293 verifyFormat("f(MyMap[{composite, key}]);"); 11294 verifyFormat("class Class {\n" 11295 " T member = {arg1, arg2};\n" 11296 "};"); 11297 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 11298 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 11299 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 11300 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 11301 verifyFormat("int a = std::is_integral<int>{} + 0;"); 11302 11303 verifyFormat("int foo(int i) { return fo1{}(i); }"); 11304 verifyFormat("int foo(int i) { return fo1{}(i); }"); 11305 verifyFormat("auto i = decltype(x){};"); 11306 verifyFormat("auto i = typeof(x){};"); 11307 verifyFormat("auto i = _Atomic(x){};"); 11308 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 11309 verifyFormat("Node n{1, Node{1000}, //\n" 11310 " 2};"); 11311 verifyFormat("Aaaa aaaaaaa{\n" 11312 " {\n" 11313 " aaaa,\n" 11314 " },\n" 11315 "};"); 11316 verifyFormat("class C : public D {\n" 11317 " SomeClass SC{2};\n" 11318 "};"); 11319 verifyFormat("class C : public A {\n" 11320 " class D : public B {\n" 11321 " void f() { int i{2}; }\n" 11322 " };\n" 11323 "};"); 11324 verifyFormat("#define A {a, a},"); 11325 11326 // Avoid breaking between equal sign and opening brace 11327 FormatStyle AvoidBreakingFirstArgument = getLLVMStyle(); 11328 AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200; 11329 verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n" 11330 " {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n" 11331 " {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n" 11332 " {\"ccccccccccccccccccccc\", 2}};", 11333 AvoidBreakingFirstArgument); 11334 11335 // Binpacking only if there is no trailing comma 11336 verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n" 11337 " cccccccccc, dddddddddd};", 11338 getLLVMStyleWithColumns(50)); 11339 verifyFormat("const Aaaaaa aaaaa = {\n" 11340 " aaaaaaaaaaa,\n" 11341 " bbbbbbbbbbb,\n" 11342 " ccccccccccc,\n" 11343 " ddddddddddd,\n" 11344 "};", 11345 getLLVMStyleWithColumns(50)); 11346 11347 // Cases where distinguising braced lists and blocks is hard. 11348 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);"); 11349 verifyFormat("void f() {\n" 11350 " return; // comment\n" 11351 "}\n" 11352 "SomeType t;"); 11353 verifyFormat("void f() {\n" 11354 " if (a) {\n" 11355 " f();\n" 11356 " }\n" 11357 "}\n" 11358 "SomeType t;"); 11359 11360 // In combination with BinPackArguments = false. 11361 FormatStyle NoBinPacking = getLLVMStyle(); 11362 NoBinPacking.BinPackArguments = false; 11363 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 11364 " bbbbb,\n" 11365 " ccccc,\n" 11366 " ddddd,\n" 11367 " eeeee,\n" 11368 " ffffff,\n" 11369 " ggggg,\n" 11370 " hhhhhh,\n" 11371 " iiiiii,\n" 11372 " jjjjjj,\n" 11373 " kkkkkk};", 11374 NoBinPacking); 11375 verifyFormat("const Aaaaaa aaaaa = {\n" 11376 " aaaaa,\n" 11377 " bbbbb,\n" 11378 " ccccc,\n" 11379 " ddddd,\n" 11380 " eeeee,\n" 11381 " ffffff,\n" 11382 " ggggg,\n" 11383 " hhhhhh,\n" 11384 " iiiiii,\n" 11385 " jjjjjj,\n" 11386 " kkkkkk,\n" 11387 "};", 11388 NoBinPacking); 11389 verifyFormat( 11390 "const Aaaaaa aaaaa = {\n" 11391 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 11392 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 11393 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 11394 "};", 11395 NoBinPacking); 11396 11397 NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 11398 EXPECT_EQ("static uint8 CddDp83848Reg[] = {\n" 11399 " CDDDP83848_BMCR_REGISTER,\n" 11400 " CDDDP83848_BMSR_REGISTER,\n" 11401 " CDDDP83848_RBR_REGISTER};", 11402 format("static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n" 11403 " CDDDP83848_BMSR_REGISTER,\n" 11404 " CDDDP83848_RBR_REGISTER};", 11405 NoBinPacking)); 11406 11407 // FIXME: The alignment of these trailing comments might be bad. Then again, 11408 // this might be utterly useless in real code. 11409 verifyFormat("Constructor::Constructor()\n" 11410 " : some_value{ //\n" 11411 " aaaaaaa, //\n" 11412 " bbbbbbb} {}"); 11413 11414 // In braced lists, the first comment is always assumed to belong to the 11415 // first element. Thus, it can be moved to the next or previous line as 11416 // appropriate. 11417 EXPECT_EQ("function({// First element:\n" 11418 " 1,\n" 11419 " // Second element:\n" 11420 " 2});", 11421 format("function({\n" 11422 " // First element:\n" 11423 " 1,\n" 11424 " // Second element:\n" 11425 " 2});")); 11426 EXPECT_EQ("std::vector<int> MyNumbers{\n" 11427 " // First element:\n" 11428 " 1,\n" 11429 " // Second element:\n" 11430 " 2};", 11431 format("std::vector<int> MyNumbers{// First element:\n" 11432 " 1,\n" 11433 " // Second element:\n" 11434 " 2};", 11435 getLLVMStyleWithColumns(30))); 11436 // A trailing comma should still lead to an enforced line break and no 11437 // binpacking. 11438 EXPECT_EQ("vector<int> SomeVector = {\n" 11439 " // aaa\n" 11440 " 1,\n" 11441 " 2,\n" 11442 "};", 11443 format("vector<int> SomeVector = { // aaa\n" 11444 " 1, 2, };")); 11445 11446 // C++11 brace initializer list l-braces should not be treated any differently 11447 // when breaking before lambda bodies is enabled 11448 FormatStyle BreakBeforeLambdaBody = getLLVMStyle(); 11449 BreakBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 11450 BreakBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 11451 BreakBeforeLambdaBody.AlwaysBreakBeforeMultilineStrings = true; 11452 verifyFormat( 11453 "std::runtime_error{\n" 11454 " \"Long string which will force a break onto the next line...\"};", 11455 BreakBeforeLambdaBody); 11456 11457 FormatStyle ExtraSpaces = getLLVMStyle(); 11458 ExtraSpaces.Cpp11BracedListStyle = false; 11459 ExtraSpaces.ColumnLimit = 75; 11460 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 11461 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 11462 verifyFormat("f({ 1, 2 });", ExtraSpaces); 11463 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 11464 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 11465 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 11466 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 11467 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 11468 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 11469 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 11470 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 11471 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 11472 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 11473 verifyFormat("class Class {\n" 11474 " T member = { arg1, arg2 };\n" 11475 "};", 11476 ExtraSpaces); 11477 verifyFormat( 11478 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11479 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 11480 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 11481 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 11482 ExtraSpaces); 11483 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 11484 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 11485 ExtraSpaces); 11486 verifyFormat( 11487 "someFunction(OtherParam,\n" 11488 " BracedList{ // comment 1 (Forcing interesting break)\n" 11489 " param1, param2,\n" 11490 " // comment 2\n" 11491 " param3, param4 });", 11492 ExtraSpaces); 11493 verifyFormat( 11494 "std::this_thread::sleep_for(\n" 11495 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 11496 ExtraSpaces); 11497 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n" 11498 " aaaaaaa,\n" 11499 " aaaaaaaaaa,\n" 11500 " aaaaa,\n" 11501 " aaaaaaaaaaaaaaa,\n" 11502 " aaa,\n" 11503 " aaaaaaaaaa,\n" 11504 " a,\n" 11505 " aaaaaaaaaaaaaaaaaaaaa,\n" 11506 " aaaaaaaaaaaa,\n" 11507 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 11508 " aaaaaaa,\n" 11509 " a};"); 11510 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 11511 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces); 11512 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces); 11513 11514 // Avoid breaking between initializer/equal sign and opening brace 11515 ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200; 11516 verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n" 11517 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 11518 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 11519 " { \"ccccccccccccccccccccc\", 2 }\n" 11520 "};", 11521 ExtraSpaces); 11522 verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n" 11523 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 11524 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 11525 " { \"ccccccccccccccccccccc\", 2 }\n" 11526 "};", 11527 ExtraSpaces); 11528 11529 FormatStyle SpaceBeforeBrace = getLLVMStyle(); 11530 SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true; 11531 verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace); 11532 verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace); 11533 11534 FormatStyle SpaceBetweenBraces = getLLVMStyle(); 11535 SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always; 11536 SpaceBetweenBraces.SpacesInParentheses = true; 11537 SpaceBetweenBraces.SpacesInSquareBrackets = true; 11538 verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces); 11539 verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces); 11540 verifyFormat("vector< int > x{ // comment 1\n" 11541 " 1, 2, 3, 4 };", 11542 SpaceBetweenBraces); 11543 SpaceBetweenBraces.ColumnLimit = 20; 11544 EXPECT_EQ("vector< int > x{\n" 11545 " 1, 2, 3, 4 };", 11546 format("vector<int>x{1,2,3,4};", SpaceBetweenBraces)); 11547 SpaceBetweenBraces.ColumnLimit = 24; 11548 EXPECT_EQ("vector< int > x{ 1, 2,\n" 11549 " 3, 4 };", 11550 format("vector<int>x{1,2,3,4};", SpaceBetweenBraces)); 11551 EXPECT_EQ("vector< int > x{\n" 11552 " 1,\n" 11553 " 2,\n" 11554 " 3,\n" 11555 " 4,\n" 11556 "};", 11557 format("vector<int>x{1,2,3,4,};", SpaceBetweenBraces)); 11558 verifyFormat("vector< int > x{};", SpaceBetweenBraces); 11559 SpaceBetweenBraces.SpaceInEmptyParentheses = true; 11560 verifyFormat("vector< int > x{ };", SpaceBetweenBraces); 11561 } 11562 11563 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 11564 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11565 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11566 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11567 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11568 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11569 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 11570 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 11571 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11572 " 1, 22, 333, 4444, 55555, //\n" 11573 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11574 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 11575 verifyFormat( 11576 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11577 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11578 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 11579 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 11580 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 11581 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 11582 " 7777777};"); 11583 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 11584 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 11585 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 11586 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 11587 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 11588 " // Separating comment.\n" 11589 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 11590 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 11591 " // Leading comment\n" 11592 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 11593 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 11594 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 11595 " 1, 1, 1, 1};", 11596 getLLVMStyleWithColumns(39)); 11597 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 11598 " 1, 1, 1, 1};", 11599 getLLVMStyleWithColumns(38)); 11600 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 11601 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 11602 getLLVMStyleWithColumns(43)); 11603 verifyFormat( 11604 "static unsigned SomeValues[10][3] = {\n" 11605 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 11606 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 11607 verifyFormat("static auto fields = new vector<string>{\n" 11608 " \"aaaaaaaaaaaaa\",\n" 11609 " \"aaaaaaaaaaaaa\",\n" 11610 " \"aaaaaaaaaaaa\",\n" 11611 " \"aaaaaaaaaaaaaa\",\n" 11612 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 11613 " \"aaaaaaaaaaaa\",\n" 11614 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 11615 "};"); 11616 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 11617 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 11618 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 11619 " 3, cccccccccccccccccccccc};", 11620 getLLVMStyleWithColumns(60)); 11621 11622 // Trailing commas. 11623 verifyFormat("vector<int> x = {\n" 11624 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 11625 "};", 11626 getLLVMStyleWithColumns(39)); 11627 verifyFormat("vector<int> x = {\n" 11628 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 11629 "};", 11630 getLLVMStyleWithColumns(39)); 11631 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 11632 " 1, 1, 1, 1,\n" 11633 " /**/ /**/};", 11634 getLLVMStyleWithColumns(39)); 11635 11636 // Trailing comment in the first line. 11637 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 11638 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 11639 " 111111111, 222222222, 3333333333, 444444444, //\n" 11640 " 11111111, 22222222, 333333333, 44444444};"); 11641 // Trailing comment in the last line. 11642 verifyFormat("int aaaaa[] = {\n" 11643 " 1, 2, 3, // comment\n" 11644 " 4, 5, 6 // comment\n" 11645 "};"); 11646 11647 // With nested lists, we should either format one item per line or all nested 11648 // lists one on line. 11649 // FIXME: For some nested lists, we can do better. 11650 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 11651 " {aaaaaaaaaaaaaaaaaaa},\n" 11652 " {aaaaaaaaaaaaaaaaaaaaa},\n" 11653 " {aaaaaaaaaaaaaaaaa}};", 11654 getLLVMStyleWithColumns(60)); 11655 verifyFormat( 11656 "SomeStruct my_struct_array = {\n" 11657 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 11658 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 11659 " {aaa, aaa},\n" 11660 " {aaa, aaa},\n" 11661 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 11662 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 11663 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 11664 11665 // No column layout should be used here. 11666 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 11667 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 11668 11669 verifyNoCrash("a<,"); 11670 11671 // No braced initializer here. 11672 verifyFormat("void f() {\n" 11673 " struct Dummy {};\n" 11674 " f(v);\n" 11675 "}"); 11676 11677 // Long lists should be formatted in columns even if they are nested. 11678 verifyFormat( 11679 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11680 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11681 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11682 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11683 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11684 " 1, 22, 333, 4444, 55555, 666666, 7777777});"); 11685 11686 // Allow "single-column" layout even if that violates the column limit. There 11687 // isn't going to be a better way. 11688 verifyFormat("std::vector<int> a = {\n" 11689 " aaaaaaaa,\n" 11690 " aaaaaaaa,\n" 11691 " aaaaaaaa,\n" 11692 " aaaaaaaa,\n" 11693 " aaaaaaaaaa,\n" 11694 " aaaaaaaa,\n" 11695 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};", 11696 getLLVMStyleWithColumns(30)); 11697 verifyFormat("vector<int> aaaa = {\n" 11698 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11699 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11700 " aaaaaa.aaaaaaa,\n" 11701 " aaaaaa.aaaaaaa,\n" 11702 " aaaaaa.aaaaaaa,\n" 11703 " aaaaaa.aaaaaaa,\n" 11704 "};"); 11705 11706 // Don't create hanging lists. 11707 verifyFormat("someFunction(Param, {List1, List2,\n" 11708 " List3});", 11709 getLLVMStyleWithColumns(35)); 11710 verifyFormat("someFunction(Param, Param,\n" 11711 " {List1, List2,\n" 11712 " List3});", 11713 getLLVMStyleWithColumns(35)); 11714 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n" 11715 " aaaaaaaaaaaaaaaaaaaaaaa);"); 11716 } 11717 11718 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 11719 FormatStyle DoNotMerge = getLLVMStyle(); 11720 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 11721 11722 verifyFormat("void f() { return 42; }"); 11723 verifyFormat("void f() {\n" 11724 " return 42;\n" 11725 "}", 11726 DoNotMerge); 11727 verifyFormat("void f() {\n" 11728 " // Comment\n" 11729 "}"); 11730 verifyFormat("{\n" 11731 "#error {\n" 11732 " int a;\n" 11733 "}"); 11734 verifyFormat("{\n" 11735 " int a;\n" 11736 "#error {\n" 11737 "}"); 11738 verifyFormat("void f() {} // comment"); 11739 verifyFormat("void f() { int a; } // comment"); 11740 verifyFormat("void f() {\n" 11741 "} // comment", 11742 DoNotMerge); 11743 verifyFormat("void f() {\n" 11744 " int a;\n" 11745 "} // comment", 11746 DoNotMerge); 11747 verifyFormat("void f() {\n" 11748 "} // comment", 11749 getLLVMStyleWithColumns(15)); 11750 11751 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 11752 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 11753 11754 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 11755 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 11756 verifyFormat("class C {\n" 11757 " C()\n" 11758 " : iiiiiiii(nullptr),\n" 11759 " kkkkkkk(nullptr),\n" 11760 " mmmmmmm(nullptr),\n" 11761 " nnnnnnn(nullptr) {}\n" 11762 "};", 11763 getGoogleStyle()); 11764 11765 FormatStyle NoColumnLimit = getLLVMStyle(); 11766 NoColumnLimit.ColumnLimit = 0; 11767 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 11768 EXPECT_EQ("class C {\n" 11769 " A() : b(0) {}\n" 11770 "};", 11771 format("class C{A():b(0){}};", NoColumnLimit)); 11772 EXPECT_EQ("A()\n" 11773 " : b(0) {\n" 11774 "}", 11775 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 11776 11777 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 11778 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 11779 FormatStyle::SFS_None; 11780 EXPECT_EQ("A()\n" 11781 " : b(0) {\n" 11782 "}", 11783 format("A():b(0){}", DoNotMergeNoColumnLimit)); 11784 EXPECT_EQ("A()\n" 11785 " : b(0) {\n" 11786 "}", 11787 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 11788 11789 verifyFormat("#define A \\\n" 11790 " void f() { \\\n" 11791 " int i; \\\n" 11792 " }", 11793 getLLVMStyleWithColumns(20)); 11794 verifyFormat("#define A \\\n" 11795 " void f() { int i; }", 11796 getLLVMStyleWithColumns(21)); 11797 verifyFormat("#define A \\\n" 11798 " void f() { \\\n" 11799 " int i; \\\n" 11800 " } \\\n" 11801 " int j;", 11802 getLLVMStyleWithColumns(22)); 11803 verifyFormat("#define A \\\n" 11804 " void f() { int i; } \\\n" 11805 " int j;", 11806 getLLVMStyleWithColumns(23)); 11807 } 11808 11809 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) { 11810 FormatStyle MergeEmptyOnly = getLLVMStyle(); 11811 MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 11812 verifyFormat("class C {\n" 11813 " int f() {}\n" 11814 "};", 11815 MergeEmptyOnly); 11816 verifyFormat("class C {\n" 11817 " int f() {\n" 11818 " return 42;\n" 11819 " }\n" 11820 "};", 11821 MergeEmptyOnly); 11822 verifyFormat("int f() {}", MergeEmptyOnly); 11823 verifyFormat("int f() {\n" 11824 " return 42;\n" 11825 "}", 11826 MergeEmptyOnly); 11827 11828 // Also verify behavior when BraceWrapping.AfterFunction = true 11829 MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 11830 MergeEmptyOnly.BraceWrapping.AfterFunction = true; 11831 verifyFormat("int f() {}", MergeEmptyOnly); 11832 verifyFormat("class C {\n" 11833 " int f() {}\n" 11834 "};", 11835 MergeEmptyOnly); 11836 } 11837 11838 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 11839 FormatStyle MergeInlineOnly = getLLVMStyle(); 11840 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 11841 verifyFormat("class C {\n" 11842 " int f() { return 42; }\n" 11843 "};", 11844 MergeInlineOnly); 11845 verifyFormat("int f() {\n" 11846 " return 42;\n" 11847 "}", 11848 MergeInlineOnly); 11849 11850 // SFS_Inline implies SFS_Empty 11851 verifyFormat("class C {\n" 11852 " int f() {}\n" 11853 "};", 11854 MergeInlineOnly); 11855 verifyFormat("int f() {}", MergeInlineOnly); 11856 11857 // Also verify behavior when BraceWrapping.AfterFunction = true 11858 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 11859 MergeInlineOnly.BraceWrapping.AfterFunction = true; 11860 verifyFormat("class C {\n" 11861 " int f() { return 42; }\n" 11862 "};", 11863 MergeInlineOnly); 11864 verifyFormat("int f()\n" 11865 "{\n" 11866 " return 42;\n" 11867 "}", 11868 MergeInlineOnly); 11869 11870 // SFS_Inline implies SFS_Empty 11871 verifyFormat("int f() {}", MergeInlineOnly); 11872 verifyFormat("class C {\n" 11873 " int f() {}\n" 11874 "};", 11875 MergeInlineOnly); 11876 } 11877 11878 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) { 11879 FormatStyle MergeInlineOnly = getLLVMStyle(); 11880 MergeInlineOnly.AllowShortFunctionsOnASingleLine = 11881 FormatStyle::SFS_InlineOnly; 11882 verifyFormat("class C {\n" 11883 " int f() { return 42; }\n" 11884 "};", 11885 MergeInlineOnly); 11886 verifyFormat("int f() {\n" 11887 " return 42;\n" 11888 "}", 11889 MergeInlineOnly); 11890 11891 // SFS_InlineOnly does not imply SFS_Empty 11892 verifyFormat("class C {\n" 11893 " int f() {}\n" 11894 "};", 11895 MergeInlineOnly); 11896 verifyFormat("int f() {\n" 11897 "}", 11898 MergeInlineOnly); 11899 11900 // Also verify behavior when BraceWrapping.AfterFunction = true 11901 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 11902 MergeInlineOnly.BraceWrapping.AfterFunction = true; 11903 verifyFormat("class C {\n" 11904 " int f() { return 42; }\n" 11905 "};", 11906 MergeInlineOnly); 11907 verifyFormat("int f()\n" 11908 "{\n" 11909 " return 42;\n" 11910 "}", 11911 MergeInlineOnly); 11912 11913 // SFS_InlineOnly does not imply SFS_Empty 11914 verifyFormat("int f()\n" 11915 "{\n" 11916 "}", 11917 MergeInlineOnly); 11918 verifyFormat("class C {\n" 11919 " int f() {}\n" 11920 "};", 11921 MergeInlineOnly); 11922 } 11923 11924 TEST_F(FormatTest, SplitEmptyFunction) { 11925 FormatStyle Style = getLLVMStyle(); 11926 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 11927 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 11928 Style.BraceWrapping.AfterFunction = true; 11929 Style.BraceWrapping.SplitEmptyFunction = false; 11930 Style.ColumnLimit = 40; 11931 11932 verifyFormat("int f()\n" 11933 "{}", 11934 Style); 11935 verifyFormat("int f()\n" 11936 "{\n" 11937 " return 42;\n" 11938 "}", 11939 Style); 11940 verifyFormat("int f()\n" 11941 "{\n" 11942 " // some comment\n" 11943 "}", 11944 Style); 11945 11946 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 11947 verifyFormat("int f() {}", Style); 11948 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 11949 "{}", 11950 Style); 11951 verifyFormat("int f()\n" 11952 "{\n" 11953 " return 0;\n" 11954 "}", 11955 Style); 11956 11957 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 11958 verifyFormat("class Foo {\n" 11959 " int f() {}\n" 11960 "};\n", 11961 Style); 11962 verifyFormat("class Foo {\n" 11963 " int f() { return 0; }\n" 11964 "};\n", 11965 Style); 11966 verifyFormat("class Foo {\n" 11967 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 11968 " {}\n" 11969 "};\n", 11970 Style); 11971 verifyFormat("class Foo {\n" 11972 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 11973 " {\n" 11974 " return 0;\n" 11975 " }\n" 11976 "};\n", 11977 Style); 11978 11979 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 11980 verifyFormat("int f() {}", Style); 11981 verifyFormat("int f() { return 0; }", Style); 11982 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 11983 "{}", 11984 Style); 11985 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 11986 "{\n" 11987 " return 0;\n" 11988 "}", 11989 Style); 11990 } 11991 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) { 11992 FormatStyle Style = getLLVMStyle(); 11993 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 11994 verifyFormat("#ifdef A\n" 11995 "int f() {}\n" 11996 "#else\n" 11997 "int g() {}\n" 11998 "#endif", 11999 Style); 12000 } 12001 12002 TEST_F(FormatTest, SplitEmptyClass) { 12003 FormatStyle Style = getLLVMStyle(); 12004 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12005 Style.BraceWrapping.AfterClass = true; 12006 Style.BraceWrapping.SplitEmptyRecord = false; 12007 12008 verifyFormat("class Foo\n" 12009 "{};", 12010 Style); 12011 verifyFormat("/* something */ class Foo\n" 12012 "{};", 12013 Style); 12014 verifyFormat("template <typename X> class Foo\n" 12015 "{};", 12016 Style); 12017 verifyFormat("class Foo\n" 12018 "{\n" 12019 " Foo();\n" 12020 "};", 12021 Style); 12022 verifyFormat("typedef class Foo\n" 12023 "{\n" 12024 "} Foo_t;", 12025 Style); 12026 12027 Style.BraceWrapping.SplitEmptyRecord = true; 12028 Style.BraceWrapping.AfterStruct = true; 12029 verifyFormat("class rep\n" 12030 "{\n" 12031 "};", 12032 Style); 12033 verifyFormat("struct rep\n" 12034 "{\n" 12035 "};", 12036 Style); 12037 verifyFormat("template <typename T> class rep\n" 12038 "{\n" 12039 "};", 12040 Style); 12041 verifyFormat("template <typename T> struct rep\n" 12042 "{\n" 12043 "};", 12044 Style); 12045 verifyFormat("class rep\n" 12046 "{\n" 12047 " int x;\n" 12048 "};", 12049 Style); 12050 verifyFormat("struct rep\n" 12051 "{\n" 12052 " int x;\n" 12053 "};", 12054 Style); 12055 verifyFormat("template <typename T> class rep\n" 12056 "{\n" 12057 " int x;\n" 12058 "};", 12059 Style); 12060 verifyFormat("template <typename T> struct rep\n" 12061 "{\n" 12062 " int x;\n" 12063 "};", 12064 Style); 12065 verifyFormat("template <typename T> class rep // Foo\n" 12066 "{\n" 12067 " int x;\n" 12068 "};", 12069 Style); 12070 verifyFormat("template <typename T> struct rep // Bar\n" 12071 "{\n" 12072 " int x;\n" 12073 "};", 12074 Style); 12075 12076 verifyFormat("template <typename T> class rep<T>\n" 12077 "{\n" 12078 " int x;\n" 12079 "};", 12080 Style); 12081 12082 verifyFormat("template <typename T> class rep<std::complex<T>>\n" 12083 "{\n" 12084 " int x;\n" 12085 "};", 12086 Style); 12087 verifyFormat("template <typename T> class rep<std::complex<T>>\n" 12088 "{\n" 12089 "};", 12090 Style); 12091 12092 verifyFormat("#include \"stdint.h\"\n" 12093 "namespace rep {}", 12094 Style); 12095 verifyFormat("#include <stdint.h>\n" 12096 "namespace rep {}", 12097 Style); 12098 verifyFormat("#include <stdint.h>\n" 12099 "namespace rep {}", 12100 "#include <stdint.h>\n" 12101 "namespace rep {\n" 12102 "\n" 12103 "\n" 12104 "}", 12105 Style); 12106 } 12107 12108 TEST_F(FormatTest, SplitEmptyStruct) { 12109 FormatStyle Style = getLLVMStyle(); 12110 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12111 Style.BraceWrapping.AfterStruct = true; 12112 Style.BraceWrapping.SplitEmptyRecord = false; 12113 12114 verifyFormat("struct Foo\n" 12115 "{};", 12116 Style); 12117 verifyFormat("/* something */ struct Foo\n" 12118 "{};", 12119 Style); 12120 verifyFormat("template <typename X> struct Foo\n" 12121 "{};", 12122 Style); 12123 verifyFormat("struct Foo\n" 12124 "{\n" 12125 " Foo();\n" 12126 "};", 12127 Style); 12128 verifyFormat("typedef struct Foo\n" 12129 "{\n" 12130 "} Foo_t;", 12131 Style); 12132 // typedef struct Bar {} Bar_t; 12133 } 12134 12135 TEST_F(FormatTest, SplitEmptyUnion) { 12136 FormatStyle Style = getLLVMStyle(); 12137 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12138 Style.BraceWrapping.AfterUnion = true; 12139 Style.BraceWrapping.SplitEmptyRecord = false; 12140 12141 verifyFormat("union Foo\n" 12142 "{};", 12143 Style); 12144 verifyFormat("/* something */ union Foo\n" 12145 "{};", 12146 Style); 12147 verifyFormat("union Foo\n" 12148 "{\n" 12149 " A,\n" 12150 "};", 12151 Style); 12152 verifyFormat("typedef union Foo\n" 12153 "{\n" 12154 "} Foo_t;", 12155 Style); 12156 } 12157 12158 TEST_F(FormatTest, SplitEmptyNamespace) { 12159 FormatStyle Style = getLLVMStyle(); 12160 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12161 Style.BraceWrapping.AfterNamespace = true; 12162 Style.BraceWrapping.SplitEmptyNamespace = false; 12163 12164 verifyFormat("namespace Foo\n" 12165 "{};", 12166 Style); 12167 verifyFormat("/* something */ namespace Foo\n" 12168 "{};", 12169 Style); 12170 verifyFormat("inline namespace Foo\n" 12171 "{};", 12172 Style); 12173 verifyFormat("/* something */ inline namespace Foo\n" 12174 "{};", 12175 Style); 12176 verifyFormat("export namespace Foo\n" 12177 "{};", 12178 Style); 12179 verifyFormat("namespace Foo\n" 12180 "{\n" 12181 "void Bar();\n" 12182 "};", 12183 Style); 12184 } 12185 12186 TEST_F(FormatTest, NeverMergeShortRecords) { 12187 FormatStyle Style = getLLVMStyle(); 12188 12189 verifyFormat("class Foo {\n" 12190 " Foo();\n" 12191 "};", 12192 Style); 12193 verifyFormat("typedef class Foo {\n" 12194 " Foo();\n" 12195 "} Foo_t;", 12196 Style); 12197 verifyFormat("struct Foo {\n" 12198 " Foo();\n" 12199 "};", 12200 Style); 12201 verifyFormat("typedef struct Foo {\n" 12202 " Foo();\n" 12203 "} Foo_t;", 12204 Style); 12205 verifyFormat("union Foo {\n" 12206 " A,\n" 12207 "};", 12208 Style); 12209 verifyFormat("typedef union Foo {\n" 12210 " A,\n" 12211 "} Foo_t;", 12212 Style); 12213 verifyFormat("namespace Foo {\n" 12214 "void Bar();\n" 12215 "};", 12216 Style); 12217 12218 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12219 Style.BraceWrapping.AfterClass = true; 12220 Style.BraceWrapping.AfterStruct = true; 12221 Style.BraceWrapping.AfterUnion = true; 12222 Style.BraceWrapping.AfterNamespace = true; 12223 verifyFormat("class Foo\n" 12224 "{\n" 12225 " Foo();\n" 12226 "};", 12227 Style); 12228 verifyFormat("typedef class Foo\n" 12229 "{\n" 12230 " Foo();\n" 12231 "} Foo_t;", 12232 Style); 12233 verifyFormat("struct Foo\n" 12234 "{\n" 12235 " Foo();\n" 12236 "};", 12237 Style); 12238 verifyFormat("typedef struct Foo\n" 12239 "{\n" 12240 " Foo();\n" 12241 "} Foo_t;", 12242 Style); 12243 verifyFormat("union Foo\n" 12244 "{\n" 12245 " A,\n" 12246 "};", 12247 Style); 12248 verifyFormat("typedef union Foo\n" 12249 "{\n" 12250 " A,\n" 12251 "} Foo_t;", 12252 Style); 12253 verifyFormat("namespace Foo\n" 12254 "{\n" 12255 "void Bar();\n" 12256 "};", 12257 Style); 12258 } 12259 12260 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 12261 // Elaborate type variable declarations. 12262 verifyFormat("struct foo a = {bar};\nint n;"); 12263 verifyFormat("class foo a = {bar};\nint n;"); 12264 verifyFormat("union foo a = {bar};\nint n;"); 12265 12266 // Elaborate types inside function definitions. 12267 verifyFormat("struct foo f() {}\nint n;"); 12268 verifyFormat("class foo f() {}\nint n;"); 12269 verifyFormat("union foo f() {}\nint n;"); 12270 12271 // Templates. 12272 verifyFormat("template <class X> void f() {}\nint n;"); 12273 verifyFormat("template <struct X> void f() {}\nint n;"); 12274 verifyFormat("template <union X> void f() {}\nint n;"); 12275 12276 // Actual definitions... 12277 verifyFormat("struct {\n} n;"); 12278 verifyFormat( 12279 "template <template <class T, class Y>, class Z> class X {\n} n;"); 12280 verifyFormat("union Z {\n int n;\n} x;"); 12281 verifyFormat("class MACRO Z {\n} n;"); 12282 verifyFormat("class MACRO(X) Z {\n} n;"); 12283 verifyFormat("class __attribute__(X) Z {\n} n;"); 12284 verifyFormat("class __declspec(X) Z {\n} n;"); 12285 verifyFormat("class A##B##C {\n} n;"); 12286 verifyFormat("class alignas(16) Z {\n} n;"); 12287 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 12288 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 12289 12290 // Redefinition from nested context: 12291 verifyFormat("class A::B::C {\n} n;"); 12292 12293 // Template definitions. 12294 verifyFormat( 12295 "template <typename F>\n" 12296 "Matcher(const Matcher<F> &Other,\n" 12297 " typename enable_if_c<is_base_of<F, T>::value &&\n" 12298 " !is_same<F, T>::value>::type * = 0)\n" 12299 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 12300 12301 // FIXME: This is still incorrectly handled at the formatter side. 12302 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 12303 verifyFormat("int i = SomeFunction(a<b, a> b);"); 12304 12305 // FIXME: 12306 // This now gets parsed incorrectly as class definition. 12307 // verifyFormat("class A<int> f() {\n}\nint n;"); 12308 12309 // Elaborate types where incorrectly parsing the structural element would 12310 // break the indent. 12311 verifyFormat("if (true)\n" 12312 " class X x;\n" 12313 "else\n" 12314 " f();\n"); 12315 12316 // This is simply incomplete. Formatting is not important, but must not crash. 12317 verifyFormat("class A:"); 12318 } 12319 12320 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 12321 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 12322 format("#error Leave all white!!!!! space* alone!\n")); 12323 EXPECT_EQ( 12324 "#warning Leave all white!!!!! space* alone!\n", 12325 format("#warning Leave all white!!!!! space* alone!\n")); 12326 EXPECT_EQ("#error 1", format(" # error 1")); 12327 EXPECT_EQ("#warning 1", format(" # warning 1")); 12328 } 12329 12330 TEST_F(FormatTest, FormatHashIfExpressions) { 12331 verifyFormat("#if AAAA && BBBB"); 12332 verifyFormat("#if (AAAA && BBBB)"); 12333 verifyFormat("#elif (AAAA && BBBB)"); 12334 // FIXME: Come up with a better indentation for #elif. 12335 verifyFormat( 12336 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 12337 " defined(BBBBBBBB)\n" 12338 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 12339 " defined(BBBBBBBB)\n" 12340 "#endif", 12341 getLLVMStyleWithColumns(65)); 12342 } 12343 12344 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 12345 FormatStyle AllowsMergedIf = getGoogleStyle(); 12346 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 12347 FormatStyle::SIS_WithoutElse; 12348 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 12349 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 12350 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 12351 EXPECT_EQ("if (true) return 42;", 12352 format("if (true)\nreturn 42;", AllowsMergedIf)); 12353 FormatStyle ShortMergedIf = AllowsMergedIf; 12354 ShortMergedIf.ColumnLimit = 25; 12355 verifyFormat("#define A \\\n" 12356 " if (true) return 42;", 12357 ShortMergedIf); 12358 verifyFormat("#define A \\\n" 12359 " f(); \\\n" 12360 " if (true)\n" 12361 "#define B", 12362 ShortMergedIf); 12363 verifyFormat("#define A \\\n" 12364 " f(); \\\n" 12365 " if (true)\n" 12366 "g();", 12367 ShortMergedIf); 12368 verifyFormat("{\n" 12369 "#ifdef A\n" 12370 " // Comment\n" 12371 " if (true) continue;\n" 12372 "#endif\n" 12373 " // Comment\n" 12374 " if (true) continue;\n" 12375 "}", 12376 ShortMergedIf); 12377 ShortMergedIf.ColumnLimit = 33; 12378 verifyFormat("#define A \\\n" 12379 " if constexpr (true) return 42;", 12380 ShortMergedIf); 12381 verifyFormat("#define A \\\n" 12382 " if CONSTEXPR (true) return 42;", 12383 ShortMergedIf); 12384 ShortMergedIf.ColumnLimit = 29; 12385 verifyFormat("#define A \\\n" 12386 " if (aaaaaaaaaa) return 1; \\\n" 12387 " return 2;", 12388 ShortMergedIf); 12389 ShortMergedIf.ColumnLimit = 28; 12390 verifyFormat("#define A \\\n" 12391 " if (aaaaaaaaaa) \\\n" 12392 " return 1; \\\n" 12393 " return 2;", 12394 ShortMergedIf); 12395 verifyFormat("#define A \\\n" 12396 " if constexpr (aaaaaaa) \\\n" 12397 " return 1; \\\n" 12398 " return 2;", 12399 ShortMergedIf); 12400 verifyFormat("#define A \\\n" 12401 " if CONSTEXPR (aaaaaaa) \\\n" 12402 " return 1; \\\n" 12403 " return 2;", 12404 ShortMergedIf); 12405 } 12406 12407 TEST_F(FormatTest, FormatStarDependingOnContext) { 12408 verifyFormat("void f(int *a);"); 12409 verifyFormat("void f() { f(fint * b); }"); 12410 verifyFormat("class A {\n void f(int *a);\n};"); 12411 verifyFormat("class A {\n int *a;\n};"); 12412 verifyFormat("namespace a {\n" 12413 "namespace b {\n" 12414 "class A {\n" 12415 " void f() {}\n" 12416 " int *a;\n" 12417 "};\n" 12418 "} // namespace b\n" 12419 "} // namespace a"); 12420 } 12421 12422 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 12423 verifyFormat("while"); 12424 verifyFormat("operator"); 12425 } 12426 12427 TEST_F(FormatTest, SkipsDeeplyNestedLines) { 12428 // This code would be painfully slow to format if we didn't skip it. 12429 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 12430 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12431 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12432 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12433 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12434 "A(1, 1)\n" 12435 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x 12436 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12437 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12438 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12439 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12440 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12441 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12442 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12443 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12444 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n"); 12445 // Deeply nested part is untouched, rest is formatted. 12446 EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n", 12447 format(std::string("int i;\n") + Code + "int j;\n", 12448 getLLVMStyle(), SC_ExpectIncomplete)); 12449 } 12450 12451 //===----------------------------------------------------------------------===// 12452 // Objective-C tests. 12453 //===----------------------------------------------------------------------===// 12454 12455 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 12456 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 12457 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 12458 format("-(NSUInteger)indexOfObject:(id)anObject;")); 12459 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 12460 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 12461 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 12462 format("-(NSInteger)Method3:(id)anObject;")); 12463 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 12464 format("-(NSInteger)Method4:(id)anObject;")); 12465 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 12466 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 12467 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 12468 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 12469 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 12470 "forAllCells:(BOOL)flag;", 12471 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 12472 "forAllCells:(BOOL)flag;")); 12473 12474 // Very long objectiveC method declaration. 12475 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 12476 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 12477 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 12478 " inRange:(NSRange)range\n" 12479 " outRange:(NSRange)out_range\n" 12480 " outRange1:(NSRange)out_range1\n" 12481 " outRange2:(NSRange)out_range2\n" 12482 " outRange3:(NSRange)out_range3\n" 12483 " outRange4:(NSRange)out_range4\n" 12484 " outRange5:(NSRange)out_range5\n" 12485 " outRange6:(NSRange)out_range6\n" 12486 " outRange7:(NSRange)out_range7\n" 12487 " outRange8:(NSRange)out_range8\n" 12488 " outRange9:(NSRange)out_range9;"); 12489 12490 // When the function name has to be wrapped. 12491 FormatStyle Style = getLLVMStyle(); 12492 // ObjC ignores IndentWrappedFunctionNames when wrapping methods 12493 // and always indents instead. 12494 Style.IndentWrappedFunctionNames = false; 12495 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 12496 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 12497 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 12498 "}", 12499 Style); 12500 Style.IndentWrappedFunctionNames = true; 12501 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 12502 " veryLooooooooooongName:(NSString)cccccccccccccc\n" 12503 " anotherName:(NSString)dddddddddddddd {\n" 12504 "}", 12505 Style); 12506 12507 verifyFormat("- (int)sum:(vector<int>)numbers;"); 12508 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 12509 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 12510 // protocol lists (but not for template classes): 12511 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 12512 12513 verifyFormat("- (int (*)())foo:(int (*)())f;"); 12514 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 12515 12516 // If there's no return type (very rare in practice!), LLVM and Google style 12517 // agree. 12518 verifyFormat("- foo;"); 12519 verifyFormat("- foo:(int)f;"); 12520 verifyGoogleFormat("- foo:(int)foo;"); 12521 } 12522 12523 TEST_F(FormatTest, BreaksStringLiterals) { 12524 EXPECT_EQ("\"some text \"\n" 12525 "\"other\";", 12526 format("\"some text other\";", getLLVMStyleWithColumns(12))); 12527 EXPECT_EQ("\"some text \"\n" 12528 "\"other\";", 12529 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 12530 EXPECT_EQ( 12531 "#define A \\\n" 12532 " \"some \" \\\n" 12533 " \"text \" \\\n" 12534 " \"other\";", 12535 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 12536 EXPECT_EQ( 12537 "#define A \\\n" 12538 " \"so \" \\\n" 12539 " \"text \" \\\n" 12540 " \"other\";", 12541 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 12542 12543 EXPECT_EQ("\"some text\"", 12544 format("\"some text\"", getLLVMStyleWithColumns(1))); 12545 EXPECT_EQ("\"some text\"", 12546 format("\"some text\"", getLLVMStyleWithColumns(11))); 12547 EXPECT_EQ("\"some \"\n" 12548 "\"text\"", 12549 format("\"some text\"", getLLVMStyleWithColumns(10))); 12550 EXPECT_EQ("\"some \"\n" 12551 "\"text\"", 12552 format("\"some text\"", getLLVMStyleWithColumns(7))); 12553 EXPECT_EQ("\"some\"\n" 12554 "\" tex\"\n" 12555 "\"t\"", 12556 format("\"some text\"", getLLVMStyleWithColumns(6))); 12557 EXPECT_EQ("\"some\"\n" 12558 "\" tex\"\n" 12559 "\" and\"", 12560 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 12561 EXPECT_EQ("\"some\"\n" 12562 "\"/tex\"\n" 12563 "\"/and\"", 12564 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 12565 12566 EXPECT_EQ("variable =\n" 12567 " \"long string \"\n" 12568 " \"literal\";", 12569 format("variable = \"long string literal\";", 12570 getLLVMStyleWithColumns(20))); 12571 12572 EXPECT_EQ("variable = f(\n" 12573 " \"long string \"\n" 12574 " \"literal\",\n" 12575 " short,\n" 12576 " loooooooooooooooooooong);", 12577 format("variable = f(\"long string literal\", short, " 12578 "loooooooooooooooooooong);", 12579 getLLVMStyleWithColumns(20))); 12580 12581 EXPECT_EQ( 12582 "f(g(\"long string \"\n" 12583 " \"literal\"),\n" 12584 " b);", 12585 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 12586 EXPECT_EQ("f(g(\"long string \"\n" 12587 " \"literal\",\n" 12588 " a),\n" 12589 " b);", 12590 format("f(g(\"long string literal\", a), b);", 12591 getLLVMStyleWithColumns(20))); 12592 EXPECT_EQ( 12593 "f(\"one two\".split(\n" 12594 " variable));", 12595 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 12596 EXPECT_EQ("f(\"one two three four five six \"\n" 12597 " \"seven\".split(\n" 12598 " really_looooong_variable));", 12599 format("f(\"one two three four five six seven\"." 12600 "split(really_looooong_variable));", 12601 getLLVMStyleWithColumns(33))); 12602 12603 EXPECT_EQ("f(\"some \"\n" 12604 " \"text\",\n" 12605 " other);", 12606 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 12607 12608 // Only break as a last resort. 12609 verifyFormat( 12610 "aaaaaaaaaaaaaaaaaaaa(\n" 12611 " aaaaaaaaaaaaaaaaaaaa,\n" 12612 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 12613 12614 EXPECT_EQ("\"splitmea\"\n" 12615 "\"trandomp\"\n" 12616 "\"oint\"", 12617 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 12618 12619 EXPECT_EQ("\"split/\"\n" 12620 "\"pathat/\"\n" 12621 "\"slashes\"", 12622 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 12623 12624 EXPECT_EQ("\"split/\"\n" 12625 "\"pathat/\"\n" 12626 "\"slashes\"", 12627 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 12628 EXPECT_EQ("\"split at \"\n" 12629 "\"spaces/at/\"\n" 12630 "\"slashes.at.any$\"\n" 12631 "\"non-alphanumeric%\"\n" 12632 "\"1111111111characte\"\n" 12633 "\"rs\"", 12634 format("\"split at " 12635 "spaces/at/" 12636 "slashes.at." 12637 "any$non-" 12638 "alphanumeric%" 12639 "1111111111characte" 12640 "rs\"", 12641 getLLVMStyleWithColumns(20))); 12642 12643 // Verify that splitting the strings understands 12644 // Style::AlwaysBreakBeforeMultilineStrings. 12645 EXPECT_EQ("aaaaaaaaaaaa(\n" 12646 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 12647 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 12648 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 12649 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 12650 "aaaaaaaaaaaaaaaaaaaaaa\");", 12651 getGoogleStyle())); 12652 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 12653 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 12654 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 12655 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 12656 "aaaaaaaaaaaaaaaaaaaaaa\";", 12657 getGoogleStyle())); 12658 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 12659 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 12660 format("llvm::outs() << " 12661 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 12662 "aaaaaaaaaaaaaaaaaaa\";")); 12663 EXPECT_EQ("ffff(\n" 12664 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 12665 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 12666 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 12667 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 12668 getGoogleStyle())); 12669 12670 FormatStyle Style = getLLVMStyleWithColumns(12); 12671 Style.BreakStringLiterals = false; 12672 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 12673 12674 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 12675 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 12676 EXPECT_EQ("#define A \\\n" 12677 " \"some \" \\\n" 12678 " \"text \" \\\n" 12679 " \"other\";", 12680 format("#define A \"some text other\";", AlignLeft)); 12681 } 12682 12683 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) { 12684 EXPECT_EQ("C a = \"some more \"\n" 12685 " \"text\";", 12686 format("C a = \"some more text\";", getLLVMStyleWithColumns(18))); 12687 } 12688 12689 TEST_F(FormatTest, FullyRemoveEmptyLines) { 12690 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 12691 NoEmptyLines.MaxEmptyLinesToKeep = 0; 12692 EXPECT_EQ("int i = a(b());", 12693 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 12694 } 12695 12696 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 12697 EXPECT_EQ( 12698 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 12699 "(\n" 12700 " \"x\t\");", 12701 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 12702 "aaaaaaa(" 12703 "\"x\t\");")); 12704 } 12705 12706 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 12707 EXPECT_EQ( 12708 "u8\"utf8 string \"\n" 12709 "u8\"literal\";", 12710 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 12711 EXPECT_EQ( 12712 "u\"utf16 string \"\n" 12713 "u\"literal\";", 12714 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 12715 EXPECT_EQ( 12716 "U\"utf32 string \"\n" 12717 "U\"literal\";", 12718 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 12719 EXPECT_EQ("L\"wide string \"\n" 12720 "L\"literal\";", 12721 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 12722 EXPECT_EQ("@\"NSString \"\n" 12723 "@\"literal\";", 12724 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 12725 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26)); 12726 12727 // This input makes clang-format try to split the incomplete unicode escape 12728 // sequence, which used to lead to a crasher. 12729 verifyNoCrash( 12730 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 12731 getLLVMStyleWithColumns(60)); 12732 } 12733 12734 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 12735 FormatStyle Style = getGoogleStyleWithColumns(15); 12736 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 12737 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 12738 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 12739 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 12740 EXPECT_EQ("u8R\"x(raw literal)x\";", 12741 format("u8R\"x(raw literal)x\";", Style)); 12742 } 12743 12744 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 12745 FormatStyle Style = getLLVMStyleWithColumns(20); 12746 EXPECT_EQ( 12747 "_T(\"aaaaaaaaaaaaaa\")\n" 12748 "_T(\"aaaaaaaaaaaaaa\")\n" 12749 "_T(\"aaaaaaaaaaaa\")", 12750 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 12751 EXPECT_EQ("f(x,\n" 12752 " _T(\"aaaaaaaaaaaa\")\n" 12753 " _T(\"aaa\"),\n" 12754 " z);", 12755 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 12756 12757 // FIXME: Handle embedded spaces in one iteration. 12758 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 12759 // "_T(\"aaaaaaaaaaaaa\")\n" 12760 // "_T(\"aaaaaaaaaaaaa\")\n" 12761 // "_T(\"a\")", 12762 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 12763 // getLLVMStyleWithColumns(20))); 12764 EXPECT_EQ( 12765 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 12766 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 12767 EXPECT_EQ("f(\n" 12768 "#if !TEST\n" 12769 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 12770 "#endif\n" 12771 ");", 12772 format("f(\n" 12773 "#if !TEST\n" 12774 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 12775 "#endif\n" 12776 ");")); 12777 EXPECT_EQ("f(\n" 12778 "\n" 12779 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 12780 format("f(\n" 12781 "\n" 12782 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 12783 } 12784 12785 TEST_F(FormatTest, BreaksStringLiteralOperands) { 12786 // In a function call with two operands, the second can be broken with no line 12787 // break before it. 12788 EXPECT_EQ( 12789 "func(a, \"long long \"\n" 12790 " \"long long\");", 12791 format("func(a, \"long long long long\");", getLLVMStyleWithColumns(24))); 12792 // In a function call with three operands, the second must be broken with a 12793 // line break before it. 12794 EXPECT_EQ("func(a,\n" 12795 " \"long long long \"\n" 12796 " \"long\",\n" 12797 " c);", 12798 format("func(a, \"long long long long\", c);", 12799 getLLVMStyleWithColumns(24))); 12800 // In a function call with three operands, the third must be broken with a 12801 // line break before it. 12802 EXPECT_EQ("func(a, b,\n" 12803 " \"long long long \"\n" 12804 " \"long\");", 12805 format("func(a, b, \"long long long long\");", 12806 getLLVMStyleWithColumns(24))); 12807 // In a function call with three operands, both the second and the third must 12808 // be broken with a line break before them. 12809 EXPECT_EQ("func(a,\n" 12810 " \"long long long \"\n" 12811 " \"long\",\n" 12812 " \"long long long \"\n" 12813 " \"long\");", 12814 format("func(a, \"long long long long\", \"long long long long\");", 12815 getLLVMStyleWithColumns(24))); 12816 // In a chain of << with two operands, the second can be broken with no line 12817 // break before it. 12818 EXPECT_EQ("a << \"line line \"\n" 12819 " \"line\";", 12820 format("a << \"line line line\";", getLLVMStyleWithColumns(20))); 12821 // In a chain of << with three operands, the second can be broken with no line 12822 // break before it. 12823 EXPECT_EQ( 12824 "abcde << \"line \"\n" 12825 " \"line line\"\n" 12826 " << c;", 12827 format("abcde << \"line line line\" << c;", getLLVMStyleWithColumns(20))); 12828 // In a chain of << with three operands, the third must be broken with a line 12829 // break before it. 12830 EXPECT_EQ( 12831 "a << b\n" 12832 " << \"line line \"\n" 12833 " \"line\";", 12834 format("a << b << \"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 and the third must be broken with a line break before it. 12837 EXPECT_EQ("abcd << \"line line \"\n" 12838 " \"line\"\n" 12839 " << \"line line \"\n" 12840 " \"line\";", 12841 format("abcd << \"line line line\" << \"line line line\";", 12842 getLLVMStyleWithColumns(20))); 12843 // In a chain of binary operators with two operands, the second can be broken 12844 // with no line break before it. 12845 EXPECT_EQ( 12846 "abcd + \"line line \"\n" 12847 " \"line line\";", 12848 format("abcd + \"line line line line\";", getLLVMStyleWithColumns(20))); 12849 // In a chain of binary operators with three operands, the second must be 12850 // broken with a line break before it. 12851 EXPECT_EQ("abcd +\n" 12852 " \"line line \"\n" 12853 " \"line line\" +\n" 12854 " e;", 12855 format("abcd + \"line line line line\" + e;", 12856 getLLVMStyleWithColumns(20))); 12857 // In a function call with two operands, with AlignAfterOpenBracket enabled, 12858 // the first must be broken with a line break before it. 12859 FormatStyle Style = getLLVMStyleWithColumns(25); 12860 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 12861 EXPECT_EQ("someFunction(\n" 12862 " \"long long long \"\n" 12863 " \"long\",\n" 12864 " a);", 12865 format("someFunction(\"long long long long\", a);", Style)); 12866 } 12867 12868 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 12869 EXPECT_EQ( 12870 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 12871 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 12872 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 12873 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 12874 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 12875 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 12876 } 12877 12878 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 12879 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 12880 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 12881 EXPECT_EQ("fffffffffff(g(R\"x(\n" 12882 "multiline raw string literal xxxxxxxxxxxxxx\n" 12883 ")x\",\n" 12884 " a),\n" 12885 " b);", 12886 format("fffffffffff(g(R\"x(\n" 12887 "multiline raw string literal xxxxxxxxxxxxxx\n" 12888 ")x\", a), b);", 12889 getGoogleStyleWithColumns(20))); 12890 EXPECT_EQ("fffffffffff(\n" 12891 " g(R\"x(qqq\n" 12892 "multiline raw string literal xxxxxxxxxxxxxx\n" 12893 ")x\",\n" 12894 " a),\n" 12895 " b);", 12896 format("fffffffffff(g(R\"x(qqq\n" 12897 "multiline raw string literal xxxxxxxxxxxxxx\n" 12898 ")x\", a), b);", 12899 getGoogleStyleWithColumns(20))); 12900 12901 EXPECT_EQ("fffffffffff(R\"x(\n" 12902 "multiline raw string literal xxxxxxxxxxxxxx\n" 12903 ")x\");", 12904 format("fffffffffff(R\"x(\n" 12905 "multiline raw string literal xxxxxxxxxxxxxx\n" 12906 ")x\");", 12907 getGoogleStyleWithColumns(20))); 12908 EXPECT_EQ("fffffffffff(R\"x(\n" 12909 "multiline raw string literal xxxxxxxxxxxxxx\n" 12910 ")x\" + bbbbbb);", 12911 format("fffffffffff(R\"x(\n" 12912 "multiline raw string literal xxxxxxxxxxxxxx\n" 12913 ")x\" + bbbbbb);", 12914 getGoogleStyleWithColumns(20))); 12915 EXPECT_EQ("fffffffffff(\n" 12916 " R\"x(\n" 12917 "multiline raw string literal xxxxxxxxxxxxxx\n" 12918 ")x\" +\n" 12919 " bbbbbb);", 12920 format("fffffffffff(\n" 12921 " R\"x(\n" 12922 "multiline raw string literal xxxxxxxxxxxxxx\n" 12923 ")x\" + bbbbbb);", 12924 getGoogleStyleWithColumns(20))); 12925 EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);", 12926 format("fffffffffff(\n" 12927 " R\"(single line raw string)\" + bbbbbb);")); 12928 } 12929 12930 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 12931 verifyFormat("string a = \"unterminated;"); 12932 EXPECT_EQ("function(\"unterminated,\n" 12933 " OtherParameter);", 12934 format("function( \"unterminated,\n" 12935 " OtherParameter);")); 12936 } 12937 12938 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 12939 FormatStyle Style = getLLVMStyle(); 12940 Style.Standard = FormatStyle::LS_Cpp03; 12941 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 12942 format("#define x(_a) printf(\"foo\"_a);", Style)); 12943 } 12944 12945 TEST_F(FormatTest, CppLexVersion) { 12946 FormatStyle Style = getLLVMStyle(); 12947 // Formatting of x * y differs if x is a type. 12948 verifyFormat("void foo() { MACRO(a * b); }", Style); 12949 verifyFormat("void foo() { MACRO(int *b); }", Style); 12950 12951 // LLVM style uses latest lexer. 12952 verifyFormat("void foo() { MACRO(char8_t *b); }", Style); 12953 Style.Standard = FormatStyle::LS_Cpp17; 12954 // But in c++17, char8_t isn't a keyword. 12955 verifyFormat("void foo() { MACRO(char8_t * b); }", Style); 12956 } 12957 12958 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 12959 12960 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 12961 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 12962 " \"ddeeefff\");", 12963 format("someFunction(\"aaabbbcccdddeeefff\");", 12964 getLLVMStyleWithColumns(25))); 12965 EXPECT_EQ("someFunction1234567890(\n" 12966 " \"aaabbbcccdddeeefff\");", 12967 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 12968 getLLVMStyleWithColumns(26))); 12969 EXPECT_EQ("someFunction1234567890(\n" 12970 " \"aaabbbcccdddeeeff\"\n" 12971 " \"f\");", 12972 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 12973 getLLVMStyleWithColumns(25))); 12974 EXPECT_EQ("someFunction1234567890(\n" 12975 " \"aaabbbcccdddeeeff\"\n" 12976 " \"f\");", 12977 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 12978 getLLVMStyleWithColumns(24))); 12979 EXPECT_EQ("someFunction(\n" 12980 " \"aaabbbcc ddde \"\n" 12981 " \"efff\");", 12982 format("someFunction(\"aaabbbcc ddde efff\");", 12983 getLLVMStyleWithColumns(25))); 12984 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 12985 " \"ddeeefff\");", 12986 format("someFunction(\"aaabbbccc ddeeefff\");", 12987 getLLVMStyleWithColumns(25))); 12988 EXPECT_EQ("someFunction1234567890(\n" 12989 " \"aaabb \"\n" 12990 " \"cccdddeeefff\");", 12991 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 12992 getLLVMStyleWithColumns(25))); 12993 EXPECT_EQ("#define A \\\n" 12994 " string s = \\\n" 12995 " \"123456789\" \\\n" 12996 " \"0\"; \\\n" 12997 " int i;", 12998 format("#define A string s = \"1234567890\"; int i;", 12999 getLLVMStyleWithColumns(20))); 13000 EXPECT_EQ("someFunction(\n" 13001 " \"aaabbbcc \"\n" 13002 " \"dddeeefff\");", 13003 format("someFunction(\"aaabbbcc dddeeefff\");", 13004 getLLVMStyleWithColumns(25))); 13005 } 13006 13007 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 13008 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 13009 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 13010 EXPECT_EQ("\"test\"\n" 13011 "\"\\n\"", 13012 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 13013 EXPECT_EQ("\"tes\\\\\"\n" 13014 "\"n\"", 13015 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 13016 EXPECT_EQ("\"\\\\\\\\\"\n" 13017 "\"\\n\"", 13018 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 13019 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 13020 EXPECT_EQ("\"\\uff01\"\n" 13021 "\"test\"", 13022 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 13023 EXPECT_EQ("\"\\Uff01ff02\"", 13024 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 13025 EXPECT_EQ("\"\\x000000000001\"\n" 13026 "\"next\"", 13027 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 13028 EXPECT_EQ("\"\\x000000000001next\"", 13029 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 13030 EXPECT_EQ("\"\\x000000000001\"", 13031 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 13032 EXPECT_EQ("\"test\"\n" 13033 "\"\\000000\"\n" 13034 "\"000001\"", 13035 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 13036 EXPECT_EQ("\"test\\000\"\n" 13037 "\"00000000\"\n" 13038 "\"1\"", 13039 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 13040 } 13041 13042 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 13043 verifyFormat("void f() {\n" 13044 " return g() {}\n" 13045 " void h() {}"); 13046 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 13047 "g();\n" 13048 "}"); 13049 } 13050 13051 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 13052 verifyFormat( 13053 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 13054 } 13055 13056 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 13057 verifyFormat("class X {\n" 13058 " void f() {\n" 13059 " }\n" 13060 "};", 13061 getLLVMStyleWithColumns(12)); 13062 } 13063 13064 TEST_F(FormatTest, ConfigurableIndentWidth) { 13065 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 13066 EightIndent.IndentWidth = 8; 13067 EightIndent.ContinuationIndentWidth = 8; 13068 verifyFormat("void f() {\n" 13069 " someFunction();\n" 13070 " if (true) {\n" 13071 " f();\n" 13072 " }\n" 13073 "}", 13074 EightIndent); 13075 verifyFormat("class X {\n" 13076 " void f() {\n" 13077 " }\n" 13078 "};", 13079 EightIndent); 13080 verifyFormat("int x[] = {\n" 13081 " call(),\n" 13082 " call()};", 13083 EightIndent); 13084 } 13085 13086 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 13087 verifyFormat("double\n" 13088 "f();", 13089 getLLVMStyleWithColumns(8)); 13090 } 13091 13092 TEST_F(FormatTest, ConfigurableUseOfTab) { 13093 FormatStyle Tab = getLLVMStyleWithColumns(42); 13094 Tab.IndentWidth = 8; 13095 Tab.UseTab = FormatStyle::UT_Always; 13096 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left; 13097 13098 EXPECT_EQ("if (aaaaaaaa && // q\n" 13099 " bb)\t\t// w\n" 13100 "\t;", 13101 format("if (aaaaaaaa &&// q\n" 13102 "bb)// w\n" 13103 ";", 13104 Tab)); 13105 EXPECT_EQ("if (aaa && bbb) // w\n" 13106 "\t;", 13107 format("if(aaa&&bbb)// w\n" 13108 ";", 13109 Tab)); 13110 13111 verifyFormat("class X {\n" 13112 "\tvoid f() {\n" 13113 "\t\tsomeFunction(parameter1,\n" 13114 "\t\t\t parameter2);\n" 13115 "\t}\n" 13116 "};", 13117 Tab); 13118 verifyFormat("#define A \\\n" 13119 "\tvoid f() { \\\n" 13120 "\t\tsomeFunction( \\\n" 13121 "\t\t parameter1, \\\n" 13122 "\t\t parameter2); \\\n" 13123 "\t}", 13124 Tab); 13125 verifyFormat("int a;\t // x\n" 13126 "int bbbbbbbb; // x\n", 13127 Tab); 13128 13129 Tab.TabWidth = 4; 13130 Tab.IndentWidth = 8; 13131 verifyFormat("class TabWidth4Indent8 {\n" 13132 "\t\tvoid f() {\n" 13133 "\t\t\t\tsomeFunction(parameter1,\n" 13134 "\t\t\t\t\t\t\t parameter2);\n" 13135 "\t\t}\n" 13136 "};", 13137 Tab); 13138 13139 Tab.TabWidth = 4; 13140 Tab.IndentWidth = 4; 13141 verifyFormat("class TabWidth4Indent4 {\n" 13142 "\tvoid f() {\n" 13143 "\t\tsomeFunction(parameter1,\n" 13144 "\t\t\t\t\t parameter2);\n" 13145 "\t}\n" 13146 "};", 13147 Tab); 13148 13149 Tab.TabWidth = 8; 13150 Tab.IndentWidth = 4; 13151 verifyFormat("class TabWidth8Indent4 {\n" 13152 " void f() {\n" 13153 "\tsomeFunction(parameter1,\n" 13154 "\t\t parameter2);\n" 13155 " }\n" 13156 "};", 13157 Tab); 13158 13159 Tab.TabWidth = 8; 13160 Tab.IndentWidth = 8; 13161 EXPECT_EQ("/*\n" 13162 "\t a\t\tcomment\n" 13163 "\t in multiple lines\n" 13164 " */", 13165 format(" /*\t \t \n" 13166 " \t \t a\t\tcomment\t \t\n" 13167 " \t \t in multiple lines\t\n" 13168 " \t */", 13169 Tab)); 13170 13171 Tab.UseTab = FormatStyle::UT_ForIndentation; 13172 verifyFormat("{\n" 13173 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13174 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13175 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13176 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13177 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13178 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13179 "};", 13180 Tab); 13181 verifyFormat("enum AA {\n" 13182 "\ta1, // Force multiple lines\n" 13183 "\ta2,\n" 13184 "\ta3\n" 13185 "};", 13186 Tab); 13187 EXPECT_EQ("if (aaaaaaaa && // q\n" 13188 " bb) // w\n" 13189 "\t;", 13190 format("if (aaaaaaaa &&// q\n" 13191 "bb)// w\n" 13192 ";", 13193 Tab)); 13194 verifyFormat("class X {\n" 13195 "\tvoid f() {\n" 13196 "\t\tsomeFunction(parameter1,\n" 13197 "\t\t parameter2);\n" 13198 "\t}\n" 13199 "};", 13200 Tab); 13201 verifyFormat("{\n" 13202 "\tQ(\n" 13203 "\t {\n" 13204 "\t\t int a;\n" 13205 "\t\t someFunction(aaaaaaaa,\n" 13206 "\t\t bbbbbbb);\n" 13207 "\t },\n" 13208 "\t p);\n" 13209 "}", 13210 Tab); 13211 EXPECT_EQ("{\n" 13212 "\t/* aaaa\n" 13213 "\t bbbb */\n" 13214 "}", 13215 format("{\n" 13216 "/* aaaa\n" 13217 " bbbb */\n" 13218 "}", 13219 Tab)); 13220 EXPECT_EQ("{\n" 13221 "\t/*\n" 13222 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13223 "\t bbbbbbbbbbbbb\n" 13224 "\t*/\n" 13225 "}", 13226 format("{\n" 13227 "/*\n" 13228 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13229 "*/\n" 13230 "}", 13231 Tab)); 13232 EXPECT_EQ("{\n" 13233 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13234 "\t// bbbbbbbbbbbbb\n" 13235 "}", 13236 format("{\n" 13237 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13238 "}", 13239 Tab)); 13240 EXPECT_EQ("{\n" 13241 "\t/*\n" 13242 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13243 "\t bbbbbbbbbbbbb\n" 13244 "\t*/\n" 13245 "}", 13246 format("{\n" 13247 "\t/*\n" 13248 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13249 "\t*/\n" 13250 "}", 13251 Tab)); 13252 EXPECT_EQ("{\n" 13253 "\t/*\n" 13254 "\n" 13255 "\t*/\n" 13256 "}", 13257 format("{\n" 13258 "\t/*\n" 13259 "\n" 13260 "\t*/\n" 13261 "}", 13262 Tab)); 13263 EXPECT_EQ("{\n" 13264 "\t/*\n" 13265 " asdf\n" 13266 "\t*/\n" 13267 "}", 13268 format("{\n" 13269 "\t/*\n" 13270 " asdf\n" 13271 "\t*/\n" 13272 "}", 13273 Tab)); 13274 13275 Tab.UseTab = FormatStyle::UT_Never; 13276 EXPECT_EQ("/*\n" 13277 " a\t\tcomment\n" 13278 " in multiple lines\n" 13279 " */", 13280 format(" /*\t \t \n" 13281 " \t \t a\t\tcomment\t \t\n" 13282 " \t \t in multiple lines\t\n" 13283 " \t */", 13284 Tab)); 13285 EXPECT_EQ("/* some\n" 13286 " comment */", 13287 format(" \t \t /* some\n" 13288 " \t \t comment */", 13289 Tab)); 13290 EXPECT_EQ("int a; /* some\n" 13291 " comment */", 13292 format(" \t \t int a; /* some\n" 13293 " \t \t comment */", 13294 Tab)); 13295 13296 EXPECT_EQ("int a; /* some\n" 13297 "comment */", 13298 format(" \t \t int\ta; /* some\n" 13299 " \t \t comment */", 13300 Tab)); 13301 EXPECT_EQ("f(\"\t\t\"); /* some\n" 13302 " comment */", 13303 format(" \t \t f(\"\t\t\"); /* some\n" 13304 " \t \t comment */", 13305 Tab)); 13306 EXPECT_EQ("{\n" 13307 " /*\n" 13308 " * Comment\n" 13309 " */\n" 13310 " int i;\n" 13311 "}", 13312 format("{\n" 13313 "\t/*\n" 13314 "\t * Comment\n" 13315 "\t */\n" 13316 "\t int i;\n" 13317 "}", 13318 Tab)); 13319 13320 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 13321 Tab.TabWidth = 8; 13322 Tab.IndentWidth = 8; 13323 EXPECT_EQ("if (aaaaaaaa && // q\n" 13324 " bb) // w\n" 13325 "\t;", 13326 format("if (aaaaaaaa &&// q\n" 13327 "bb)// w\n" 13328 ";", 13329 Tab)); 13330 EXPECT_EQ("if (aaa && bbb) // w\n" 13331 "\t;", 13332 format("if(aaa&&bbb)// w\n" 13333 ";", 13334 Tab)); 13335 verifyFormat("class X {\n" 13336 "\tvoid f() {\n" 13337 "\t\tsomeFunction(parameter1,\n" 13338 "\t\t\t parameter2);\n" 13339 "\t}\n" 13340 "};", 13341 Tab); 13342 verifyFormat("#define A \\\n" 13343 "\tvoid f() { \\\n" 13344 "\t\tsomeFunction( \\\n" 13345 "\t\t parameter1, \\\n" 13346 "\t\t parameter2); \\\n" 13347 "\t}", 13348 Tab); 13349 Tab.TabWidth = 4; 13350 Tab.IndentWidth = 8; 13351 verifyFormat("class TabWidth4Indent8 {\n" 13352 "\t\tvoid f() {\n" 13353 "\t\t\t\tsomeFunction(parameter1,\n" 13354 "\t\t\t\t\t\t\t parameter2);\n" 13355 "\t\t}\n" 13356 "};", 13357 Tab); 13358 Tab.TabWidth = 4; 13359 Tab.IndentWidth = 4; 13360 verifyFormat("class TabWidth4Indent4 {\n" 13361 "\tvoid f() {\n" 13362 "\t\tsomeFunction(parameter1,\n" 13363 "\t\t\t\t\t parameter2);\n" 13364 "\t}\n" 13365 "};", 13366 Tab); 13367 Tab.TabWidth = 8; 13368 Tab.IndentWidth = 4; 13369 verifyFormat("class TabWidth8Indent4 {\n" 13370 " void f() {\n" 13371 "\tsomeFunction(parameter1,\n" 13372 "\t\t parameter2);\n" 13373 " }\n" 13374 "};", 13375 Tab); 13376 Tab.TabWidth = 8; 13377 Tab.IndentWidth = 8; 13378 EXPECT_EQ("/*\n" 13379 "\t a\t\tcomment\n" 13380 "\t in multiple lines\n" 13381 " */", 13382 format(" /*\t \t \n" 13383 " \t \t a\t\tcomment\t \t\n" 13384 " \t \t in multiple lines\t\n" 13385 " \t */", 13386 Tab)); 13387 verifyFormat("{\n" 13388 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13389 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13390 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13391 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13392 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13393 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13394 "};", 13395 Tab); 13396 verifyFormat("enum AA {\n" 13397 "\ta1, // Force multiple lines\n" 13398 "\ta2,\n" 13399 "\ta3\n" 13400 "};", 13401 Tab); 13402 EXPECT_EQ("if (aaaaaaaa && // q\n" 13403 " bb) // w\n" 13404 "\t;", 13405 format("if (aaaaaaaa &&// q\n" 13406 "bb)// w\n" 13407 ";", 13408 Tab)); 13409 verifyFormat("class X {\n" 13410 "\tvoid f() {\n" 13411 "\t\tsomeFunction(parameter1,\n" 13412 "\t\t\t parameter2);\n" 13413 "\t}\n" 13414 "};", 13415 Tab); 13416 verifyFormat("{\n" 13417 "\tQ(\n" 13418 "\t {\n" 13419 "\t\t int a;\n" 13420 "\t\t someFunction(aaaaaaaa,\n" 13421 "\t\t\t\t bbbbbbb);\n" 13422 "\t },\n" 13423 "\t p);\n" 13424 "}", 13425 Tab); 13426 EXPECT_EQ("{\n" 13427 "\t/* aaaa\n" 13428 "\t bbbb */\n" 13429 "}", 13430 format("{\n" 13431 "/* aaaa\n" 13432 " bbbb */\n" 13433 "}", 13434 Tab)); 13435 EXPECT_EQ("{\n" 13436 "\t/*\n" 13437 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13438 "\t bbbbbbbbbbbbb\n" 13439 "\t*/\n" 13440 "}", 13441 format("{\n" 13442 "/*\n" 13443 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13444 "*/\n" 13445 "}", 13446 Tab)); 13447 EXPECT_EQ("{\n" 13448 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13449 "\t// bbbbbbbbbbbbb\n" 13450 "}", 13451 format("{\n" 13452 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13453 "}", 13454 Tab)); 13455 EXPECT_EQ("{\n" 13456 "\t/*\n" 13457 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13458 "\t bbbbbbbbbbbbb\n" 13459 "\t*/\n" 13460 "}", 13461 format("{\n" 13462 "\t/*\n" 13463 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13464 "\t*/\n" 13465 "}", 13466 Tab)); 13467 EXPECT_EQ("{\n" 13468 "\t/*\n" 13469 "\n" 13470 "\t*/\n" 13471 "}", 13472 format("{\n" 13473 "\t/*\n" 13474 "\n" 13475 "\t*/\n" 13476 "}", 13477 Tab)); 13478 EXPECT_EQ("{\n" 13479 "\t/*\n" 13480 " asdf\n" 13481 "\t*/\n" 13482 "}", 13483 format("{\n" 13484 "\t/*\n" 13485 " asdf\n" 13486 "\t*/\n" 13487 "}", 13488 Tab)); 13489 EXPECT_EQ("/* some\n" 13490 " comment */", 13491 format(" \t \t /* some\n" 13492 " \t \t comment */", 13493 Tab)); 13494 EXPECT_EQ("int a; /* some\n" 13495 " comment */", 13496 format(" \t \t int a; /* some\n" 13497 " \t \t comment */", 13498 Tab)); 13499 EXPECT_EQ("int a; /* some\n" 13500 "comment */", 13501 format(" \t \t int\ta; /* some\n" 13502 " \t \t comment */", 13503 Tab)); 13504 EXPECT_EQ("f(\"\t\t\"); /* some\n" 13505 " comment */", 13506 format(" \t \t f(\"\t\t\"); /* some\n" 13507 " \t \t comment */", 13508 Tab)); 13509 EXPECT_EQ("{\n" 13510 "\t/*\n" 13511 "\t * Comment\n" 13512 "\t */\n" 13513 "\tint i;\n" 13514 "}", 13515 format("{\n" 13516 "\t/*\n" 13517 "\t * Comment\n" 13518 "\t */\n" 13519 "\t int i;\n" 13520 "}", 13521 Tab)); 13522 Tab.TabWidth = 2; 13523 Tab.IndentWidth = 2; 13524 EXPECT_EQ("{\n" 13525 "\t/* aaaa\n" 13526 "\t\t bbbb */\n" 13527 "}", 13528 format("{\n" 13529 "/* aaaa\n" 13530 "\t bbbb */\n" 13531 "}", 13532 Tab)); 13533 EXPECT_EQ("{\n" 13534 "\t/*\n" 13535 "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13536 "\t\tbbbbbbbbbbbbb\n" 13537 "\t*/\n" 13538 "}", 13539 format("{\n" 13540 "/*\n" 13541 "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13542 "*/\n" 13543 "}", 13544 Tab)); 13545 Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 13546 Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 13547 Tab.TabWidth = 4; 13548 Tab.IndentWidth = 4; 13549 verifyFormat("class Assign {\n" 13550 "\tvoid f() {\n" 13551 "\t\tint x = 123;\n" 13552 "\t\tint random = 4;\n" 13553 "\t\tstd::string alphabet =\n" 13554 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 13555 "\t}\n" 13556 "};", 13557 Tab); 13558 13559 Tab.UseTab = FormatStyle::UT_AlignWithSpaces; 13560 Tab.TabWidth = 8; 13561 Tab.IndentWidth = 8; 13562 EXPECT_EQ("if (aaaaaaaa && // q\n" 13563 " bb) // w\n" 13564 "\t;", 13565 format("if (aaaaaaaa &&// q\n" 13566 "bb)// w\n" 13567 ";", 13568 Tab)); 13569 EXPECT_EQ("if (aaa && bbb) // w\n" 13570 "\t;", 13571 format("if(aaa&&bbb)// w\n" 13572 ";", 13573 Tab)); 13574 verifyFormat("class X {\n" 13575 "\tvoid f() {\n" 13576 "\t\tsomeFunction(parameter1,\n" 13577 "\t\t parameter2);\n" 13578 "\t}\n" 13579 "};", 13580 Tab); 13581 verifyFormat("#define A \\\n" 13582 "\tvoid f() { \\\n" 13583 "\t\tsomeFunction( \\\n" 13584 "\t\t parameter1, \\\n" 13585 "\t\t parameter2); \\\n" 13586 "\t}", 13587 Tab); 13588 Tab.TabWidth = 4; 13589 Tab.IndentWidth = 8; 13590 verifyFormat("class TabWidth4Indent8 {\n" 13591 "\t\tvoid f() {\n" 13592 "\t\t\t\tsomeFunction(parameter1,\n" 13593 "\t\t\t\t parameter2);\n" 13594 "\t\t}\n" 13595 "};", 13596 Tab); 13597 Tab.TabWidth = 4; 13598 Tab.IndentWidth = 4; 13599 verifyFormat("class TabWidth4Indent4 {\n" 13600 "\tvoid f() {\n" 13601 "\t\tsomeFunction(parameter1,\n" 13602 "\t\t parameter2);\n" 13603 "\t}\n" 13604 "};", 13605 Tab); 13606 Tab.TabWidth = 8; 13607 Tab.IndentWidth = 4; 13608 verifyFormat("class TabWidth8Indent4 {\n" 13609 " void f() {\n" 13610 "\tsomeFunction(parameter1,\n" 13611 "\t parameter2);\n" 13612 " }\n" 13613 "};", 13614 Tab); 13615 Tab.TabWidth = 8; 13616 Tab.IndentWidth = 8; 13617 EXPECT_EQ("/*\n" 13618 " a\t\tcomment\n" 13619 " in multiple lines\n" 13620 " */", 13621 format(" /*\t \t \n" 13622 " \t \t a\t\tcomment\t \t\n" 13623 " \t \t in multiple lines\t\n" 13624 " \t */", 13625 Tab)); 13626 verifyFormat("{\n" 13627 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13628 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13629 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13630 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13631 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13632 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13633 "};", 13634 Tab); 13635 verifyFormat("enum AA {\n" 13636 "\ta1, // Force multiple lines\n" 13637 "\ta2,\n" 13638 "\ta3\n" 13639 "};", 13640 Tab); 13641 EXPECT_EQ("if (aaaaaaaa && // q\n" 13642 " bb) // w\n" 13643 "\t;", 13644 format("if (aaaaaaaa &&// q\n" 13645 "bb)// w\n" 13646 ";", 13647 Tab)); 13648 verifyFormat("class X {\n" 13649 "\tvoid f() {\n" 13650 "\t\tsomeFunction(parameter1,\n" 13651 "\t\t parameter2);\n" 13652 "\t}\n" 13653 "};", 13654 Tab); 13655 verifyFormat("{\n" 13656 "\tQ(\n" 13657 "\t {\n" 13658 "\t\t int a;\n" 13659 "\t\t someFunction(aaaaaaaa,\n" 13660 "\t\t bbbbbbb);\n" 13661 "\t },\n" 13662 "\t p);\n" 13663 "}", 13664 Tab); 13665 EXPECT_EQ("{\n" 13666 "\t/* aaaa\n" 13667 "\t bbbb */\n" 13668 "}", 13669 format("{\n" 13670 "/* aaaa\n" 13671 " bbbb */\n" 13672 "}", 13673 Tab)); 13674 EXPECT_EQ("{\n" 13675 "\t/*\n" 13676 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13677 "\t bbbbbbbbbbbbb\n" 13678 "\t*/\n" 13679 "}", 13680 format("{\n" 13681 "/*\n" 13682 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13683 "*/\n" 13684 "}", 13685 Tab)); 13686 EXPECT_EQ("{\n" 13687 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13688 "\t// bbbbbbbbbbbbb\n" 13689 "}", 13690 format("{\n" 13691 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13692 "}", 13693 Tab)); 13694 EXPECT_EQ("{\n" 13695 "\t/*\n" 13696 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13697 "\t bbbbbbbbbbbbb\n" 13698 "\t*/\n" 13699 "}", 13700 format("{\n" 13701 "\t/*\n" 13702 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13703 "\t*/\n" 13704 "}", 13705 Tab)); 13706 EXPECT_EQ("{\n" 13707 "\t/*\n" 13708 "\n" 13709 "\t*/\n" 13710 "}", 13711 format("{\n" 13712 "\t/*\n" 13713 "\n" 13714 "\t*/\n" 13715 "}", 13716 Tab)); 13717 EXPECT_EQ("{\n" 13718 "\t/*\n" 13719 " asdf\n" 13720 "\t*/\n" 13721 "}", 13722 format("{\n" 13723 "\t/*\n" 13724 " asdf\n" 13725 "\t*/\n" 13726 "}", 13727 Tab)); 13728 EXPECT_EQ("/* some\n" 13729 " comment */", 13730 format(" \t \t /* some\n" 13731 " \t \t comment */", 13732 Tab)); 13733 EXPECT_EQ("int a; /* some\n" 13734 " comment */", 13735 format(" \t \t int a; /* some\n" 13736 " \t \t comment */", 13737 Tab)); 13738 EXPECT_EQ("int a; /* some\n" 13739 "comment */", 13740 format(" \t \t int\ta; /* some\n" 13741 " \t \t comment */", 13742 Tab)); 13743 EXPECT_EQ("f(\"\t\t\"); /* some\n" 13744 " comment */", 13745 format(" \t \t f(\"\t\t\"); /* some\n" 13746 " \t \t comment */", 13747 Tab)); 13748 EXPECT_EQ("{\n" 13749 "\t/*\n" 13750 "\t * Comment\n" 13751 "\t */\n" 13752 "\tint i;\n" 13753 "}", 13754 format("{\n" 13755 "\t/*\n" 13756 "\t * Comment\n" 13757 "\t */\n" 13758 "\t int i;\n" 13759 "}", 13760 Tab)); 13761 Tab.TabWidth = 2; 13762 Tab.IndentWidth = 2; 13763 EXPECT_EQ("{\n" 13764 "\t/* aaaa\n" 13765 "\t bbbb */\n" 13766 "}", 13767 format("{\n" 13768 "/* aaaa\n" 13769 " bbbb */\n" 13770 "}", 13771 Tab)); 13772 EXPECT_EQ("{\n" 13773 "\t/*\n" 13774 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13775 "\t bbbbbbbbbbbbb\n" 13776 "\t*/\n" 13777 "}", 13778 format("{\n" 13779 "/*\n" 13780 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13781 "*/\n" 13782 "}", 13783 Tab)); 13784 Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 13785 Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 13786 Tab.TabWidth = 4; 13787 Tab.IndentWidth = 4; 13788 verifyFormat("class Assign {\n" 13789 "\tvoid f() {\n" 13790 "\t\tint x = 123;\n" 13791 "\t\tint random = 4;\n" 13792 "\t\tstd::string alphabet =\n" 13793 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 13794 "\t}\n" 13795 "};", 13796 Tab); 13797 Tab.AlignOperands = FormatStyle::OAS_Align; 13798 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n" 13799 " cccccccccccccccccccc;", 13800 Tab); 13801 // no alignment 13802 verifyFormat("int aaaaaaaaaa =\n" 13803 "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 13804 Tab); 13805 verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n" 13806 " : bbbbbbbbbbbbbb ? 222222222222222\n" 13807 " : 333333333333333;", 13808 Tab); 13809 Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 13810 Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator; 13811 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n" 13812 " + cccccccccccccccccccc;", 13813 Tab); 13814 } 13815 13816 TEST_F(FormatTest, ZeroTabWidth) { 13817 FormatStyle Tab = getLLVMStyleWithColumns(42); 13818 Tab.IndentWidth = 8; 13819 Tab.UseTab = FormatStyle::UT_Never; 13820 Tab.TabWidth = 0; 13821 EXPECT_EQ("void a(){\n" 13822 " // line starts with '\t'\n" 13823 "};", 13824 format("void a(){\n" 13825 "\t// line starts with '\t'\n" 13826 "};", 13827 Tab)); 13828 13829 EXPECT_EQ("void a(){\n" 13830 " // line starts with '\t'\n" 13831 "};", 13832 format("void a(){\n" 13833 "\t\t// line starts with '\t'\n" 13834 "};", 13835 Tab)); 13836 13837 Tab.UseTab = FormatStyle::UT_ForIndentation; 13838 EXPECT_EQ("void a(){\n" 13839 " // line starts with '\t'\n" 13840 "};", 13841 format("void a(){\n" 13842 "\t// line starts with '\t'\n" 13843 "};", 13844 Tab)); 13845 13846 EXPECT_EQ("void a(){\n" 13847 " // line starts with '\t'\n" 13848 "};", 13849 format("void a(){\n" 13850 "\t\t// line starts with '\t'\n" 13851 "};", 13852 Tab)); 13853 13854 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 13855 EXPECT_EQ("void a(){\n" 13856 " // line starts with '\t'\n" 13857 "};", 13858 format("void a(){\n" 13859 "\t// line starts with '\t'\n" 13860 "};", 13861 Tab)); 13862 13863 EXPECT_EQ("void a(){\n" 13864 " // line starts with '\t'\n" 13865 "};", 13866 format("void a(){\n" 13867 "\t\t// line starts with '\t'\n" 13868 "};", 13869 Tab)); 13870 13871 Tab.UseTab = FormatStyle::UT_AlignWithSpaces; 13872 EXPECT_EQ("void a(){\n" 13873 " // line starts with '\t'\n" 13874 "};", 13875 format("void a(){\n" 13876 "\t// line starts with '\t'\n" 13877 "};", 13878 Tab)); 13879 13880 EXPECT_EQ("void a(){\n" 13881 " // line starts with '\t'\n" 13882 "};", 13883 format("void a(){\n" 13884 "\t\t// line starts with '\t'\n" 13885 "};", 13886 Tab)); 13887 13888 Tab.UseTab = FormatStyle::UT_Always; 13889 EXPECT_EQ("void a(){\n" 13890 "// line starts with '\t'\n" 13891 "};", 13892 format("void a(){\n" 13893 "\t// line starts with '\t'\n" 13894 "};", 13895 Tab)); 13896 13897 EXPECT_EQ("void a(){\n" 13898 "// line starts with '\t'\n" 13899 "};", 13900 format("void a(){\n" 13901 "\t\t// line starts with '\t'\n" 13902 "};", 13903 Tab)); 13904 } 13905 13906 TEST_F(FormatTest, CalculatesOriginalColumn) { 13907 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13908 "q\"; /* some\n" 13909 " comment */", 13910 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13911 "q\"; /* some\n" 13912 " comment */", 13913 getLLVMStyle())); 13914 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 13915 "/* some\n" 13916 " comment */", 13917 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 13918 " /* some\n" 13919 " comment */", 13920 getLLVMStyle())); 13921 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13922 "qqq\n" 13923 "/* some\n" 13924 " comment */", 13925 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13926 "qqq\n" 13927 " /* some\n" 13928 " comment */", 13929 getLLVMStyle())); 13930 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13931 "wwww; /* some\n" 13932 " comment */", 13933 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13934 "wwww; /* some\n" 13935 " comment */", 13936 getLLVMStyle())); 13937 } 13938 13939 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 13940 FormatStyle NoSpace = getLLVMStyle(); 13941 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 13942 13943 verifyFormat("while(true)\n" 13944 " continue;", 13945 NoSpace); 13946 verifyFormat("for(;;)\n" 13947 " continue;", 13948 NoSpace); 13949 verifyFormat("if(true)\n" 13950 " f();\n" 13951 "else if(true)\n" 13952 " f();", 13953 NoSpace); 13954 verifyFormat("do {\n" 13955 " do_something();\n" 13956 "} while(something());", 13957 NoSpace); 13958 verifyFormat("switch(x) {\n" 13959 "default:\n" 13960 " break;\n" 13961 "}", 13962 NoSpace); 13963 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 13964 verifyFormat("size_t x = sizeof(x);", NoSpace); 13965 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 13966 verifyFormat("auto f(int x) -> typeof(x);", NoSpace); 13967 verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace); 13968 verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace); 13969 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 13970 verifyFormat("alignas(128) char a[128];", NoSpace); 13971 verifyFormat("size_t x = alignof(MyType);", NoSpace); 13972 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 13973 verifyFormat("int f() throw(Deprecated);", NoSpace); 13974 verifyFormat("typedef void (*cb)(int);", NoSpace); 13975 verifyFormat("T A::operator()();", NoSpace); 13976 verifyFormat("X A::operator++(T);", NoSpace); 13977 verifyFormat("auto lambda = []() { return 0; };", NoSpace); 13978 13979 FormatStyle Space = getLLVMStyle(); 13980 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 13981 13982 verifyFormat("int f ();", Space); 13983 verifyFormat("void f (int a, T b) {\n" 13984 " while (true)\n" 13985 " continue;\n" 13986 "}", 13987 Space); 13988 verifyFormat("if (true)\n" 13989 " f ();\n" 13990 "else if (true)\n" 13991 " f ();", 13992 Space); 13993 verifyFormat("do {\n" 13994 " do_something ();\n" 13995 "} while (something ());", 13996 Space); 13997 verifyFormat("switch (x) {\n" 13998 "default:\n" 13999 " break;\n" 14000 "}", 14001 Space); 14002 verifyFormat("A::A () : a (1) {}", Space); 14003 verifyFormat("void f () __attribute__ ((asdf));", Space); 14004 verifyFormat("*(&a + 1);\n" 14005 "&((&a)[1]);\n" 14006 "a[(b + c) * d];\n" 14007 "(((a + 1) * 2) + 3) * 4;", 14008 Space); 14009 verifyFormat("#define A(x) x", Space); 14010 verifyFormat("#define A (x) x", Space); 14011 verifyFormat("#if defined(x)\n" 14012 "#endif", 14013 Space); 14014 verifyFormat("auto i = std::make_unique<int> (5);", Space); 14015 verifyFormat("size_t x = sizeof (x);", Space); 14016 verifyFormat("auto f (int x) -> decltype (x);", Space); 14017 verifyFormat("auto f (int x) -> typeof (x);", Space); 14018 verifyFormat("auto f (int x) -> _Atomic (x);", Space); 14019 verifyFormat("auto f (int x) -> __underlying_type (x);", Space); 14020 verifyFormat("int f (T x) noexcept (x.create ());", Space); 14021 verifyFormat("alignas (128) char a[128];", Space); 14022 verifyFormat("size_t x = alignof (MyType);", Space); 14023 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 14024 verifyFormat("int f () throw (Deprecated);", Space); 14025 verifyFormat("typedef void (*cb) (int);", Space); 14026 verifyFormat("T A::operator() ();", Space); 14027 verifyFormat("X A::operator++ (T);", Space); 14028 verifyFormat("auto lambda = [] () { return 0; };", Space); 14029 verifyFormat("int x = int (y);", Space); 14030 14031 FormatStyle SomeSpace = getLLVMStyle(); 14032 SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses; 14033 14034 verifyFormat("[]() -> float {}", SomeSpace); 14035 verifyFormat("[] (auto foo) {}", SomeSpace); 14036 verifyFormat("[foo]() -> int {}", SomeSpace); 14037 verifyFormat("int f();", SomeSpace); 14038 verifyFormat("void f (int a, T b) {\n" 14039 " while (true)\n" 14040 " continue;\n" 14041 "}", 14042 SomeSpace); 14043 verifyFormat("if (true)\n" 14044 " f();\n" 14045 "else if (true)\n" 14046 " f();", 14047 SomeSpace); 14048 verifyFormat("do {\n" 14049 " do_something();\n" 14050 "} while (something());", 14051 SomeSpace); 14052 verifyFormat("switch (x) {\n" 14053 "default:\n" 14054 " break;\n" 14055 "}", 14056 SomeSpace); 14057 verifyFormat("A::A() : a (1) {}", SomeSpace); 14058 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace); 14059 verifyFormat("*(&a + 1);\n" 14060 "&((&a)[1]);\n" 14061 "a[(b + c) * d];\n" 14062 "(((a + 1) * 2) + 3) * 4;", 14063 SomeSpace); 14064 verifyFormat("#define A(x) x", SomeSpace); 14065 verifyFormat("#define A (x) x", SomeSpace); 14066 verifyFormat("#if defined(x)\n" 14067 "#endif", 14068 SomeSpace); 14069 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace); 14070 verifyFormat("size_t x = sizeof (x);", SomeSpace); 14071 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace); 14072 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace); 14073 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace); 14074 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace); 14075 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace); 14076 verifyFormat("alignas (128) char a[128];", SomeSpace); 14077 verifyFormat("size_t x = alignof (MyType);", SomeSpace); 14078 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", 14079 SomeSpace); 14080 verifyFormat("int f() throw (Deprecated);", SomeSpace); 14081 verifyFormat("typedef void (*cb) (int);", SomeSpace); 14082 verifyFormat("T A::operator()();", SomeSpace); 14083 verifyFormat("X A::operator++ (T);", SomeSpace); 14084 verifyFormat("int x = int (y);", SomeSpace); 14085 verifyFormat("auto lambda = []() { return 0; };", SomeSpace); 14086 } 14087 14088 TEST_F(FormatTest, SpaceAfterLogicalNot) { 14089 FormatStyle Spaces = getLLVMStyle(); 14090 Spaces.SpaceAfterLogicalNot = true; 14091 14092 verifyFormat("bool x = ! y", Spaces); 14093 verifyFormat("if (! isFailure())", Spaces); 14094 verifyFormat("if (! (a && b))", Spaces); 14095 verifyFormat("\"Error!\"", Spaces); 14096 verifyFormat("! ! x", Spaces); 14097 } 14098 14099 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 14100 FormatStyle Spaces = getLLVMStyle(); 14101 14102 Spaces.SpacesInParentheses = true; 14103 verifyFormat("do_something( ::globalVar );", Spaces); 14104 verifyFormat("call( x, y, z );", Spaces); 14105 verifyFormat("call();", Spaces); 14106 verifyFormat("std::function<void( int, int )> callback;", Spaces); 14107 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 14108 Spaces); 14109 verifyFormat("while ( (bool)1 )\n" 14110 " continue;", 14111 Spaces); 14112 verifyFormat("for ( ;; )\n" 14113 " continue;", 14114 Spaces); 14115 verifyFormat("if ( true )\n" 14116 " f();\n" 14117 "else if ( true )\n" 14118 " f();", 14119 Spaces); 14120 verifyFormat("do {\n" 14121 " do_something( (int)i );\n" 14122 "} while ( something() );", 14123 Spaces); 14124 verifyFormat("switch ( x ) {\n" 14125 "default:\n" 14126 " break;\n" 14127 "}", 14128 Spaces); 14129 14130 Spaces.SpacesInParentheses = false; 14131 Spaces.SpacesInCStyleCastParentheses = true; 14132 verifyFormat("Type *A = ( Type * )P;", Spaces); 14133 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 14134 verifyFormat("x = ( int32 )y;", Spaces); 14135 verifyFormat("int a = ( int )(2.0f);", Spaces); 14136 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 14137 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 14138 verifyFormat("#define x (( int )-1)", Spaces); 14139 14140 // Run the first set of tests again with: 14141 Spaces.SpacesInParentheses = false; 14142 Spaces.SpaceInEmptyParentheses = true; 14143 Spaces.SpacesInCStyleCastParentheses = true; 14144 verifyFormat("call(x, y, z);", Spaces); 14145 verifyFormat("call( );", Spaces); 14146 verifyFormat("std::function<void(int, int)> callback;", Spaces); 14147 verifyFormat("while (( bool )1)\n" 14148 " continue;", 14149 Spaces); 14150 verifyFormat("for (;;)\n" 14151 " continue;", 14152 Spaces); 14153 verifyFormat("if (true)\n" 14154 " f( );\n" 14155 "else if (true)\n" 14156 " f( );", 14157 Spaces); 14158 verifyFormat("do {\n" 14159 " do_something(( int )i);\n" 14160 "} while (something( ));", 14161 Spaces); 14162 verifyFormat("switch (x) {\n" 14163 "default:\n" 14164 " break;\n" 14165 "}", 14166 Spaces); 14167 14168 // Run the first set of tests again with: 14169 Spaces.SpaceAfterCStyleCast = true; 14170 verifyFormat("call(x, y, z);", Spaces); 14171 verifyFormat("call( );", Spaces); 14172 verifyFormat("std::function<void(int, int)> callback;", Spaces); 14173 verifyFormat("while (( bool ) 1)\n" 14174 " continue;", 14175 Spaces); 14176 verifyFormat("for (;;)\n" 14177 " continue;", 14178 Spaces); 14179 verifyFormat("if (true)\n" 14180 " f( );\n" 14181 "else if (true)\n" 14182 " f( );", 14183 Spaces); 14184 verifyFormat("do {\n" 14185 " do_something(( int ) i);\n" 14186 "} while (something( ));", 14187 Spaces); 14188 verifyFormat("switch (x) {\n" 14189 "default:\n" 14190 " break;\n" 14191 "}", 14192 Spaces); 14193 14194 // Run subset of tests again with: 14195 Spaces.SpacesInCStyleCastParentheses = false; 14196 Spaces.SpaceAfterCStyleCast = true; 14197 verifyFormat("while ((bool) 1)\n" 14198 " continue;", 14199 Spaces); 14200 verifyFormat("do {\n" 14201 " do_something((int) i);\n" 14202 "} while (something( ));", 14203 Spaces); 14204 14205 verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces); 14206 verifyFormat("size_t idx = (size_t) a;", Spaces); 14207 verifyFormat("size_t idx = (size_t) (a - 1);", Spaces); 14208 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); 14209 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); 14210 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); 14211 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); 14212 Spaces.ColumnLimit = 80; 14213 Spaces.IndentWidth = 4; 14214 Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 14215 verifyFormat("void foo( ) {\n" 14216 " size_t foo = (*(function))(\n" 14217 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " 14218 "BarrrrrrrrrrrrLong,\n" 14219 " FoooooooooLooooong);\n" 14220 "}", 14221 Spaces); 14222 Spaces.SpaceAfterCStyleCast = false; 14223 verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces); 14224 verifyFormat("size_t idx = (size_t)a;", Spaces); 14225 verifyFormat("size_t idx = (size_t)(a - 1);", Spaces); 14226 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); 14227 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); 14228 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); 14229 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); 14230 14231 verifyFormat("void foo( ) {\n" 14232 " size_t foo = (*(function))(\n" 14233 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " 14234 "BarrrrrrrrrrrrLong,\n" 14235 " FoooooooooLooooong);\n" 14236 "}", 14237 Spaces); 14238 } 14239 14240 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 14241 verifyFormat("int a[5];"); 14242 verifyFormat("a[3] += 42;"); 14243 14244 FormatStyle Spaces = getLLVMStyle(); 14245 Spaces.SpacesInSquareBrackets = true; 14246 // Not lambdas. 14247 verifyFormat("int a[ 5 ];", Spaces); 14248 verifyFormat("a[ 3 ] += 42;", Spaces); 14249 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 14250 verifyFormat("double &operator[](int i) { return 0; }\n" 14251 "int i;", 14252 Spaces); 14253 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 14254 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 14255 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 14256 // Lambdas. 14257 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 14258 verifyFormat("return [ i, args... ] {};", Spaces); 14259 verifyFormat("int foo = [ &bar ]() {};", Spaces); 14260 verifyFormat("int foo = [ = ]() {};", Spaces); 14261 verifyFormat("int foo = [ & ]() {};", Spaces); 14262 verifyFormat("int foo = [ =, &bar ]() {};", Spaces); 14263 verifyFormat("int foo = [ &bar, = ]() {};", Spaces); 14264 } 14265 14266 TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) { 14267 FormatStyle NoSpaceStyle = getLLVMStyle(); 14268 verifyFormat("int a[5];", NoSpaceStyle); 14269 verifyFormat("a[3] += 42;", NoSpaceStyle); 14270 14271 verifyFormat("int a[1];", NoSpaceStyle); 14272 verifyFormat("int 1 [a];", NoSpaceStyle); 14273 verifyFormat("int a[1][2];", NoSpaceStyle); 14274 verifyFormat("a[7] = 5;", NoSpaceStyle); 14275 verifyFormat("int a = (f())[23];", NoSpaceStyle); 14276 verifyFormat("f([] {})", NoSpaceStyle); 14277 14278 FormatStyle Space = getLLVMStyle(); 14279 Space.SpaceBeforeSquareBrackets = true; 14280 verifyFormat("int c = []() -> int { return 2; }();\n", Space); 14281 verifyFormat("return [i, args...] {};", Space); 14282 14283 verifyFormat("int a [5];", Space); 14284 verifyFormat("a [3] += 42;", Space); 14285 verifyFormat("constexpr char hello []{\"hello\"};", Space); 14286 verifyFormat("double &operator[](int i) { return 0; }\n" 14287 "int i;", 14288 Space); 14289 verifyFormat("std::unique_ptr<int []> foo() {}", Space); 14290 verifyFormat("int i = a [a][a]->f();", Space); 14291 verifyFormat("int i = (*b) [a]->f();", Space); 14292 14293 verifyFormat("int a [1];", Space); 14294 verifyFormat("int 1 [a];", Space); 14295 verifyFormat("int a [1][2];", Space); 14296 verifyFormat("a [7] = 5;", Space); 14297 verifyFormat("int a = (f()) [23];", Space); 14298 verifyFormat("f([] {})", Space); 14299 } 14300 14301 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 14302 verifyFormat("int a = 5;"); 14303 verifyFormat("a += 42;"); 14304 verifyFormat("a or_eq 8;"); 14305 14306 FormatStyle Spaces = getLLVMStyle(); 14307 Spaces.SpaceBeforeAssignmentOperators = false; 14308 verifyFormat("int a= 5;", Spaces); 14309 verifyFormat("a+= 42;", Spaces); 14310 verifyFormat("a or_eq 8;", Spaces); 14311 } 14312 14313 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) { 14314 verifyFormat("class Foo : public Bar {};"); 14315 verifyFormat("Foo::Foo() : foo(1) {}"); 14316 verifyFormat("for (auto a : b) {\n}"); 14317 verifyFormat("int x = a ? b : c;"); 14318 verifyFormat("{\n" 14319 "label0:\n" 14320 " int x = 0;\n" 14321 "}"); 14322 verifyFormat("switch (x) {\n" 14323 "case 1:\n" 14324 "default:\n" 14325 "}"); 14326 verifyFormat("switch (allBraces) {\n" 14327 "case 1: {\n" 14328 " break;\n" 14329 "}\n" 14330 "case 2: {\n" 14331 " [[fallthrough]];\n" 14332 "}\n" 14333 "default: {\n" 14334 " break;\n" 14335 "}\n" 14336 "}"); 14337 14338 FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30); 14339 CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false; 14340 verifyFormat("class Foo : public Bar {};", CtorInitializerStyle); 14341 verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle); 14342 verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle); 14343 verifyFormat("int x = a ? b : c;", CtorInitializerStyle); 14344 verifyFormat("{\n" 14345 "label1:\n" 14346 " int x = 0;\n" 14347 "}", 14348 CtorInitializerStyle); 14349 verifyFormat("switch (x) {\n" 14350 "case 1:\n" 14351 "default:\n" 14352 "}", 14353 CtorInitializerStyle); 14354 verifyFormat("switch (allBraces) {\n" 14355 "case 1: {\n" 14356 " break;\n" 14357 "}\n" 14358 "case 2: {\n" 14359 " [[fallthrough]];\n" 14360 "}\n" 14361 "default: {\n" 14362 " break;\n" 14363 "}\n" 14364 "}", 14365 CtorInitializerStyle); 14366 CtorInitializerStyle.BreakConstructorInitializers = 14367 FormatStyle::BCIS_AfterColon; 14368 verifyFormat("Fooooooooooo::Fooooooooooo():\n" 14369 " aaaaaaaaaaaaaaaa(1),\n" 14370 " bbbbbbbbbbbbbbbb(2) {}", 14371 CtorInitializerStyle); 14372 CtorInitializerStyle.BreakConstructorInitializers = 14373 FormatStyle::BCIS_BeforeComma; 14374 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 14375 " : aaaaaaaaaaaaaaaa(1)\n" 14376 " , bbbbbbbbbbbbbbbb(2) {}", 14377 CtorInitializerStyle); 14378 CtorInitializerStyle.BreakConstructorInitializers = 14379 FormatStyle::BCIS_BeforeColon; 14380 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 14381 " : aaaaaaaaaaaaaaaa(1),\n" 14382 " bbbbbbbbbbbbbbbb(2) {}", 14383 CtorInitializerStyle); 14384 CtorInitializerStyle.ConstructorInitializerIndentWidth = 0; 14385 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 14386 ": aaaaaaaaaaaaaaaa(1),\n" 14387 " bbbbbbbbbbbbbbbb(2) {}", 14388 CtorInitializerStyle); 14389 14390 FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30); 14391 InheritanceStyle.SpaceBeforeInheritanceColon = false; 14392 verifyFormat("class Foo: public Bar {};", InheritanceStyle); 14393 verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle); 14394 verifyFormat("for (auto a : b) {\n}", InheritanceStyle); 14395 verifyFormat("int x = a ? b : c;", InheritanceStyle); 14396 verifyFormat("{\n" 14397 "label2:\n" 14398 " int x = 0;\n" 14399 "}", 14400 InheritanceStyle); 14401 verifyFormat("switch (x) {\n" 14402 "case 1:\n" 14403 "default:\n" 14404 "}", 14405 InheritanceStyle); 14406 verifyFormat("switch (allBraces) {\n" 14407 "case 1: {\n" 14408 " break;\n" 14409 "}\n" 14410 "case 2: {\n" 14411 " [[fallthrough]];\n" 14412 "}\n" 14413 "default: {\n" 14414 " break;\n" 14415 "}\n" 14416 "}", 14417 InheritanceStyle); 14418 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma; 14419 verifyFormat("class Foooooooooooooooooooooo\n" 14420 " : public aaaaaaaaaaaaaaaaaa,\n" 14421 " public bbbbbbbbbbbbbbbbbb {\n" 14422 "}", 14423 InheritanceStyle); 14424 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon; 14425 verifyFormat("class Foooooooooooooooooooooo:\n" 14426 " public aaaaaaaaaaaaaaaaaa,\n" 14427 " public bbbbbbbbbbbbbbbbbb {\n" 14428 "}", 14429 InheritanceStyle); 14430 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 14431 verifyFormat("class Foooooooooooooooooooooo\n" 14432 " : public aaaaaaaaaaaaaaaaaa\n" 14433 " , public bbbbbbbbbbbbbbbbbb {\n" 14434 "}", 14435 InheritanceStyle); 14436 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 14437 verifyFormat("class Foooooooooooooooooooooo\n" 14438 " : public aaaaaaaaaaaaaaaaaa,\n" 14439 " public bbbbbbbbbbbbbbbbbb {\n" 14440 "}", 14441 InheritanceStyle); 14442 InheritanceStyle.ConstructorInitializerIndentWidth = 0; 14443 verifyFormat("class Foooooooooooooooooooooo\n" 14444 ": public aaaaaaaaaaaaaaaaaa,\n" 14445 " public bbbbbbbbbbbbbbbbbb {}", 14446 InheritanceStyle); 14447 14448 FormatStyle ForLoopStyle = getLLVMStyle(); 14449 ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false; 14450 verifyFormat("class Foo : public Bar {};", ForLoopStyle); 14451 verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle); 14452 verifyFormat("for (auto a: b) {\n}", ForLoopStyle); 14453 verifyFormat("int x = a ? b : c;", ForLoopStyle); 14454 verifyFormat("{\n" 14455 "label2:\n" 14456 " int x = 0;\n" 14457 "}", 14458 ForLoopStyle); 14459 verifyFormat("switch (x) {\n" 14460 "case 1:\n" 14461 "default:\n" 14462 "}", 14463 ForLoopStyle); 14464 verifyFormat("switch (allBraces) {\n" 14465 "case 1: {\n" 14466 " break;\n" 14467 "}\n" 14468 "case 2: {\n" 14469 " [[fallthrough]];\n" 14470 "}\n" 14471 "default: {\n" 14472 " break;\n" 14473 "}\n" 14474 "}", 14475 ForLoopStyle); 14476 14477 FormatStyle CaseStyle = getLLVMStyle(); 14478 CaseStyle.SpaceBeforeCaseColon = true; 14479 verifyFormat("class Foo : public Bar {};", CaseStyle); 14480 verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle); 14481 verifyFormat("for (auto a : b) {\n}", CaseStyle); 14482 verifyFormat("int x = a ? b : c;", CaseStyle); 14483 verifyFormat("switch (x) {\n" 14484 "case 1 :\n" 14485 "default :\n" 14486 "}", 14487 CaseStyle); 14488 verifyFormat("switch (allBraces) {\n" 14489 "case 1 : {\n" 14490 " break;\n" 14491 "}\n" 14492 "case 2 : {\n" 14493 " [[fallthrough]];\n" 14494 "}\n" 14495 "default : {\n" 14496 " break;\n" 14497 "}\n" 14498 "}", 14499 CaseStyle); 14500 14501 FormatStyle NoSpaceStyle = getLLVMStyle(); 14502 EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false); 14503 NoSpaceStyle.SpaceBeforeCtorInitializerColon = false; 14504 NoSpaceStyle.SpaceBeforeInheritanceColon = false; 14505 NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 14506 verifyFormat("class Foo: public Bar {};", NoSpaceStyle); 14507 verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle); 14508 verifyFormat("for (auto a: b) {\n}", NoSpaceStyle); 14509 verifyFormat("int x = a ? b : c;", NoSpaceStyle); 14510 verifyFormat("{\n" 14511 "label3:\n" 14512 " int x = 0;\n" 14513 "}", 14514 NoSpaceStyle); 14515 verifyFormat("switch (x) {\n" 14516 "case 1:\n" 14517 "default:\n" 14518 "}", 14519 NoSpaceStyle); 14520 verifyFormat("switch (allBraces) {\n" 14521 "case 1: {\n" 14522 " break;\n" 14523 "}\n" 14524 "case 2: {\n" 14525 " [[fallthrough]];\n" 14526 "}\n" 14527 "default: {\n" 14528 " break;\n" 14529 "}\n" 14530 "}", 14531 NoSpaceStyle); 14532 14533 FormatStyle InvertedSpaceStyle = getLLVMStyle(); 14534 InvertedSpaceStyle.SpaceBeforeCaseColon = true; 14535 InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false; 14536 InvertedSpaceStyle.SpaceBeforeInheritanceColon = false; 14537 InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 14538 verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle); 14539 verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle); 14540 verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle); 14541 verifyFormat("int x = a ? b : c;", InvertedSpaceStyle); 14542 verifyFormat("{\n" 14543 "label3:\n" 14544 " int x = 0;\n" 14545 "}", 14546 InvertedSpaceStyle); 14547 verifyFormat("switch (x) {\n" 14548 "case 1 :\n" 14549 "case 2 : {\n" 14550 " break;\n" 14551 "}\n" 14552 "default :\n" 14553 " break;\n" 14554 "}", 14555 InvertedSpaceStyle); 14556 verifyFormat("switch (allBraces) {\n" 14557 "case 1 : {\n" 14558 " break;\n" 14559 "}\n" 14560 "case 2 : {\n" 14561 " [[fallthrough]];\n" 14562 "}\n" 14563 "default : {\n" 14564 " break;\n" 14565 "}\n" 14566 "}", 14567 InvertedSpaceStyle); 14568 } 14569 14570 TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) { 14571 FormatStyle Style = getLLVMStyle(); 14572 14573 Style.PointerAlignment = FormatStyle::PAS_Left; 14574 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; 14575 verifyFormat("void* const* x = NULL;", Style); 14576 14577 #define verifyQualifierSpaces(Code, Pointers, Qualifiers) \ 14578 do { \ 14579 Style.PointerAlignment = FormatStyle::Pointers; \ 14580 Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers; \ 14581 verifyFormat(Code, Style); \ 14582 } while (false) 14583 14584 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default); 14585 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default); 14586 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default); 14587 14588 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before); 14589 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before); 14590 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before); 14591 14592 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After); 14593 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After); 14594 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After); 14595 14596 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both); 14597 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both); 14598 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both); 14599 14600 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default); 14601 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, 14602 SAPQ_Default); 14603 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 14604 SAPQ_Default); 14605 14606 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before); 14607 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, 14608 SAPQ_Before); 14609 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 14610 SAPQ_Before); 14611 14612 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After); 14613 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After); 14614 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 14615 SAPQ_After); 14616 14617 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both); 14618 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both); 14619 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both); 14620 14621 #undef verifyQualifierSpaces 14622 14623 FormatStyle Spaces = getLLVMStyle(); 14624 Spaces.AttributeMacros.push_back("qualified"); 14625 Spaces.PointerAlignment = FormatStyle::PAS_Right; 14626 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; 14627 verifyFormat("SomeType *volatile *a = NULL;", Spaces); 14628 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces); 14629 verifyFormat("std::vector<SomeType *const *> x;", Spaces); 14630 verifyFormat("std::vector<SomeType *qualified *> x;", Spaces); 14631 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 14632 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before; 14633 verifyFormat("SomeType * volatile *a = NULL;", Spaces); 14634 verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces); 14635 verifyFormat("std::vector<SomeType * const *> x;", Spaces); 14636 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces); 14637 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 14638 14639 // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left. 14640 Spaces.PointerAlignment = FormatStyle::PAS_Left; 14641 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before; 14642 verifyFormat("SomeType* volatile* a = NULL;", Spaces); 14643 verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces); 14644 verifyFormat("std::vector<SomeType* const*> x;", Spaces); 14645 verifyFormat("std::vector<SomeType* qualified*> x;", Spaces); 14646 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 14647 // However, setting it to SAPQ_After should add spaces after __attribute, etc. 14648 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After; 14649 verifyFormat("SomeType* volatile * a = NULL;", Spaces); 14650 verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces); 14651 verifyFormat("std::vector<SomeType* const *> x;", Spaces); 14652 verifyFormat("std::vector<SomeType* qualified *> x;", Spaces); 14653 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 14654 14655 // PAS_Middle should not have any noticeable changes even for SAPQ_Both 14656 Spaces.PointerAlignment = FormatStyle::PAS_Middle; 14657 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After; 14658 verifyFormat("SomeType * volatile * a = NULL;", Spaces); 14659 verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces); 14660 verifyFormat("std::vector<SomeType * const *> x;", Spaces); 14661 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces); 14662 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 14663 } 14664 14665 TEST_F(FormatTest, AlignConsecutiveMacros) { 14666 FormatStyle Style = getLLVMStyle(); 14667 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 14668 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 14669 Style.AlignConsecutiveMacros = FormatStyle::ACS_None; 14670 14671 verifyFormat("#define a 3\n" 14672 "#define bbbb 4\n" 14673 "#define ccc (5)", 14674 Style); 14675 14676 verifyFormat("#define f(x) (x * x)\n" 14677 "#define fff(x, y, z) (x * y + z)\n" 14678 "#define ffff(x, y) (x - y)", 14679 Style); 14680 14681 verifyFormat("#define foo(x, y) (x + y)\n" 14682 "#define bar (5, 6)(2 + 2)", 14683 Style); 14684 14685 verifyFormat("#define a 3\n" 14686 "#define bbbb 4\n" 14687 "#define ccc (5)\n" 14688 "#define f(x) (x * x)\n" 14689 "#define fff(x, y, z) (x * y + z)\n" 14690 "#define ffff(x, y) (x - y)", 14691 Style); 14692 14693 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 14694 verifyFormat("#define a 3\n" 14695 "#define bbbb 4\n" 14696 "#define ccc (5)", 14697 Style); 14698 14699 verifyFormat("#define f(x) (x * x)\n" 14700 "#define fff(x, y, z) (x * y + z)\n" 14701 "#define ffff(x, y) (x - y)", 14702 Style); 14703 14704 verifyFormat("#define foo(x, y) (x + y)\n" 14705 "#define bar (5, 6)(2 + 2)", 14706 Style); 14707 14708 verifyFormat("#define a 3\n" 14709 "#define bbbb 4\n" 14710 "#define ccc (5)\n" 14711 "#define f(x) (x * x)\n" 14712 "#define fff(x, y, z) (x * y + z)\n" 14713 "#define ffff(x, y) (x - y)", 14714 Style); 14715 14716 verifyFormat("#define a 5\n" 14717 "#define foo(x, y) (x + y)\n" 14718 "#define CCC (6)\n" 14719 "auto lambda = []() {\n" 14720 " auto ii = 0;\n" 14721 " float j = 0;\n" 14722 " return 0;\n" 14723 "};\n" 14724 "int i = 0;\n" 14725 "float i2 = 0;\n" 14726 "auto v = type{\n" 14727 " i = 1, //\n" 14728 " (i = 2), //\n" 14729 " i = 3 //\n" 14730 "};", 14731 Style); 14732 14733 Style.AlignConsecutiveMacros = FormatStyle::ACS_None; 14734 Style.ColumnLimit = 20; 14735 14736 verifyFormat("#define a \\\n" 14737 " \"aabbbbbbbbbbbb\"\n" 14738 "#define D \\\n" 14739 " \"aabbbbbbbbbbbb\" \\\n" 14740 " \"ccddeeeeeeeee\"\n" 14741 "#define B \\\n" 14742 " \"QQQQQQQQQQQQQ\" \\\n" 14743 " \"FFFFFFFFFFFFF\" \\\n" 14744 " \"LLLLLLLL\"\n", 14745 Style); 14746 14747 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 14748 verifyFormat("#define a \\\n" 14749 " \"aabbbbbbbbbbbb\"\n" 14750 "#define D \\\n" 14751 " \"aabbbbbbbbbbbb\" \\\n" 14752 " \"ccddeeeeeeeee\"\n" 14753 "#define B \\\n" 14754 " \"QQQQQQQQQQQQQ\" \\\n" 14755 " \"FFFFFFFFFFFFF\" \\\n" 14756 " \"LLLLLLLL\"\n", 14757 Style); 14758 14759 // Test across comments 14760 Style.MaxEmptyLinesToKeep = 10; 14761 Style.ReflowComments = false; 14762 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossComments; 14763 EXPECT_EQ("#define a 3\n" 14764 "// line comment\n" 14765 "#define bbbb 4\n" 14766 "#define ccc (5)", 14767 format("#define a 3\n" 14768 "// line comment\n" 14769 "#define bbbb 4\n" 14770 "#define ccc (5)", 14771 Style)); 14772 14773 EXPECT_EQ("#define a 3\n" 14774 "/* block comment */\n" 14775 "#define bbbb 4\n" 14776 "#define ccc (5)", 14777 format("#define a 3\n" 14778 "/* block comment */\n" 14779 "#define bbbb 4\n" 14780 "#define ccc (5)", 14781 Style)); 14782 14783 EXPECT_EQ("#define a 3\n" 14784 "/* multi-line *\n" 14785 " * block comment */\n" 14786 "#define bbbb 4\n" 14787 "#define ccc (5)", 14788 format("#define a 3\n" 14789 "/* multi-line *\n" 14790 " * block comment */\n" 14791 "#define bbbb 4\n" 14792 "#define ccc (5)", 14793 Style)); 14794 14795 EXPECT_EQ("#define a 3\n" 14796 "// multi-line line comment\n" 14797 "//\n" 14798 "#define bbbb 4\n" 14799 "#define ccc (5)", 14800 format("#define a 3\n" 14801 "// multi-line line comment\n" 14802 "//\n" 14803 "#define bbbb 4\n" 14804 "#define ccc (5)", 14805 Style)); 14806 14807 EXPECT_EQ("#define a 3\n" 14808 "// empty lines still break.\n" 14809 "\n" 14810 "#define bbbb 4\n" 14811 "#define ccc (5)", 14812 format("#define a 3\n" 14813 "// empty lines still break.\n" 14814 "\n" 14815 "#define bbbb 4\n" 14816 "#define ccc (5)", 14817 Style)); 14818 14819 // Test across empty lines 14820 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLines; 14821 EXPECT_EQ("#define a 3\n" 14822 "\n" 14823 "#define bbbb 4\n" 14824 "#define ccc (5)", 14825 format("#define a 3\n" 14826 "\n" 14827 "#define bbbb 4\n" 14828 "#define ccc (5)", 14829 Style)); 14830 14831 EXPECT_EQ("#define a 3\n" 14832 "\n" 14833 "\n" 14834 "\n" 14835 "#define bbbb 4\n" 14836 "#define ccc (5)", 14837 format("#define a 3\n" 14838 "\n" 14839 "\n" 14840 "\n" 14841 "#define bbbb 4\n" 14842 "#define ccc (5)", 14843 Style)); 14844 14845 EXPECT_EQ("#define a 3\n" 14846 "// comments should break alignment\n" 14847 "//\n" 14848 "#define bbbb 4\n" 14849 "#define ccc (5)", 14850 format("#define a 3\n" 14851 "// comments should break alignment\n" 14852 "//\n" 14853 "#define bbbb 4\n" 14854 "#define ccc (5)", 14855 Style)); 14856 14857 // Test across empty lines and comments 14858 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLinesAndComments; 14859 verifyFormat("#define a 3\n" 14860 "\n" 14861 "// line comment\n" 14862 "#define bbbb 4\n" 14863 "#define ccc (5)", 14864 Style); 14865 14866 EXPECT_EQ("#define a 3\n" 14867 "\n" 14868 "\n" 14869 "/* multi-line *\n" 14870 " * block comment */\n" 14871 "\n" 14872 "\n" 14873 "#define bbbb 4\n" 14874 "#define ccc (5)", 14875 format("#define a 3\n" 14876 "\n" 14877 "\n" 14878 "/* multi-line *\n" 14879 " * block comment */\n" 14880 "\n" 14881 "\n" 14882 "#define bbbb 4\n" 14883 "#define ccc (5)", 14884 Style)); 14885 14886 EXPECT_EQ("#define a 3\n" 14887 "\n" 14888 "\n" 14889 "/* multi-line *\n" 14890 " * block comment */\n" 14891 "\n" 14892 "\n" 14893 "#define bbbb 4\n" 14894 "#define ccc (5)", 14895 format("#define a 3\n" 14896 "\n" 14897 "\n" 14898 "/* multi-line *\n" 14899 " * block comment */\n" 14900 "\n" 14901 "\n" 14902 "#define bbbb 4\n" 14903 "#define ccc (5)", 14904 Style)); 14905 } 14906 14907 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) { 14908 FormatStyle Alignment = getLLVMStyle(); 14909 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 14910 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossEmptyLines; 14911 14912 Alignment.MaxEmptyLinesToKeep = 10; 14913 /* Test alignment across empty lines */ 14914 EXPECT_EQ("int a = 5;\n" 14915 "\n" 14916 "int oneTwoThree = 123;", 14917 format("int a = 5;\n" 14918 "\n" 14919 "int oneTwoThree= 123;", 14920 Alignment)); 14921 EXPECT_EQ("int a = 5;\n" 14922 "int one = 1;\n" 14923 "\n" 14924 "int oneTwoThree = 123;", 14925 format("int a = 5;\n" 14926 "int one = 1;\n" 14927 "\n" 14928 "int oneTwoThree = 123;", 14929 Alignment)); 14930 EXPECT_EQ("int a = 5;\n" 14931 "int one = 1;\n" 14932 "\n" 14933 "int oneTwoThree = 123;\n" 14934 "int oneTwo = 12;", 14935 format("int a = 5;\n" 14936 "int one = 1;\n" 14937 "\n" 14938 "int oneTwoThree = 123;\n" 14939 "int oneTwo = 12;", 14940 Alignment)); 14941 14942 /* Test across comments */ 14943 EXPECT_EQ("int a = 5;\n" 14944 "/* block comment */\n" 14945 "int oneTwoThree = 123;", 14946 format("int a = 5;\n" 14947 "/* block comment */\n" 14948 "int oneTwoThree=123;", 14949 Alignment)); 14950 14951 EXPECT_EQ("int a = 5;\n" 14952 "// line comment\n" 14953 "int oneTwoThree = 123;", 14954 format("int a = 5;\n" 14955 "// line comment\n" 14956 "int oneTwoThree=123;", 14957 Alignment)); 14958 14959 /* Test across comments and newlines */ 14960 EXPECT_EQ("int a = 5;\n" 14961 "\n" 14962 "/* block comment */\n" 14963 "int oneTwoThree = 123;", 14964 format("int a = 5;\n" 14965 "\n" 14966 "/* block comment */\n" 14967 "int oneTwoThree=123;", 14968 Alignment)); 14969 14970 EXPECT_EQ("int a = 5;\n" 14971 "\n" 14972 "// line comment\n" 14973 "int oneTwoThree = 123;", 14974 format("int a = 5;\n" 14975 "\n" 14976 "// line comment\n" 14977 "int oneTwoThree=123;", 14978 Alignment)); 14979 } 14980 14981 TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) { 14982 FormatStyle Alignment = getLLVMStyle(); 14983 Alignment.AlignConsecutiveDeclarations = 14984 FormatStyle::ACS_AcrossEmptyLinesAndComments; 14985 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 14986 14987 Alignment.MaxEmptyLinesToKeep = 10; 14988 /* Test alignment across empty lines */ 14989 EXPECT_EQ("int a = 5;\n" 14990 "\n" 14991 "float const oneTwoThree = 123;", 14992 format("int a = 5;\n" 14993 "\n" 14994 "float const oneTwoThree = 123;", 14995 Alignment)); 14996 EXPECT_EQ("int a = 5;\n" 14997 "float const one = 1;\n" 14998 "\n" 14999 "int oneTwoThree = 123;", 15000 format("int a = 5;\n" 15001 "float const one = 1;\n" 15002 "\n" 15003 "int oneTwoThree = 123;", 15004 Alignment)); 15005 15006 /* Test across comments */ 15007 EXPECT_EQ("float const a = 5;\n" 15008 "/* block comment */\n" 15009 "int oneTwoThree = 123;", 15010 format("float const a = 5;\n" 15011 "/* block comment */\n" 15012 "int oneTwoThree=123;", 15013 Alignment)); 15014 15015 EXPECT_EQ("float const a = 5;\n" 15016 "// line comment\n" 15017 "int oneTwoThree = 123;", 15018 format("float const a = 5;\n" 15019 "// line comment\n" 15020 "int oneTwoThree=123;", 15021 Alignment)); 15022 15023 /* Test across comments and newlines */ 15024 EXPECT_EQ("float const a = 5;\n" 15025 "\n" 15026 "/* block comment */\n" 15027 "int oneTwoThree = 123;", 15028 format("float const a = 5;\n" 15029 "\n" 15030 "/* block comment */\n" 15031 "int oneTwoThree=123;", 15032 Alignment)); 15033 15034 EXPECT_EQ("float const a = 5;\n" 15035 "\n" 15036 "// line comment\n" 15037 "int oneTwoThree = 123;", 15038 format("float const a = 5;\n" 15039 "\n" 15040 "// line comment\n" 15041 "int oneTwoThree=123;", 15042 Alignment)); 15043 } 15044 15045 TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) { 15046 FormatStyle Alignment = getLLVMStyle(); 15047 Alignment.AlignConsecutiveBitFields = 15048 FormatStyle::ACS_AcrossEmptyLinesAndComments; 15049 15050 Alignment.MaxEmptyLinesToKeep = 10; 15051 /* Test alignment across empty lines */ 15052 EXPECT_EQ("int a : 5;\n" 15053 "\n" 15054 "int longbitfield : 6;", 15055 format("int a : 5;\n" 15056 "\n" 15057 "int longbitfield : 6;", 15058 Alignment)); 15059 EXPECT_EQ("int a : 5;\n" 15060 "int one : 1;\n" 15061 "\n" 15062 "int longbitfield : 6;", 15063 format("int a : 5;\n" 15064 "int one : 1;\n" 15065 "\n" 15066 "int longbitfield : 6;", 15067 Alignment)); 15068 15069 /* Test across comments */ 15070 EXPECT_EQ("int a : 5;\n" 15071 "/* block comment */\n" 15072 "int longbitfield : 6;", 15073 format("int a : 5;\n" 15074 "/* block comment */\n" 15075 "int longbitfield : 6;", 15076 Alignment)); 15077 EXPECT_EQ("int a : 5;\n" 15078 "int one : 1;\n" 15079 "// line comment\n" 15080 "int longbitfield : 6;", 15081 format("int a : 5;\n" 15082 "int one : 1;\n" 15083 "// line comment\n" 15084 "int longbitfield : 6;", 15085 Alignment)); 15086 15087 /* Test across comments and newlines */ 15088 EXPECT_EQ("int a : 5;\n" 15089 "/* block comment */\n" 15090 "\n" 15091 "int longbitfield : 6;", 15092 format("int a : 5;\n" 15093 "/* block comment */\n" 15094 "\n" 15095 "int longbitfield : 6;", 15096 Alignment)); 15097 EXPECT_EQ("int a : 5;\n" 15098 "int one : 1;\n" 15099 "\n" 15100 "// line comment\n" 15101 "\n" 15102 "int longbitfield : 6;", 15103 format("int a : 5;\n" 15104 "int one : 1;\n" 15105 "\n" 15106 "// line comment \n" 15107 "\n" 15108 "int longbitfield : 6;", 15109 Alignment)); 15110 } 15111 15112 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) { 15113 FormatStyle Alignment = getLLVMStyle(); 15114 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15115 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossComments; 15116 15117 Alignment.MaxEmptyLinesToKeep = 10; 15118 /* Test alignment across empty lines */ 15119 EXPECT_EQ("int a = 5;\n" 15120 "\n" 15121 "int oneTwoThree = 123;", 15122 format("int a = 5;\n" 15123 "\n" 15124 "int oneTwoThree= 123;", 15125 Alignment)); 15126 EXPECT_EQ("int a = 5;\n" 15127 "int one = 1;\n" 15128 "\n" 15129 "int oneTwoThree = 123;", 15130 format("int a = 5;\n" 15131 "int one = 1;\n" 15132 "\n" 15133 "int oneTwoThree = 123;", 15134 Alignment)); 15135 15136 /* Test across comments */ 15137 EXPECT_EQ("int a = 5;\n" 15138 "/* block comment */\n" 15139 "int oneTwoThree = 123;", 15140 format("int a = 5;\n" 15141 "/* block comment */\n" 15142 "int oneTwoThree=123;", 15143 Alignment)); 15144 15145 EXPECT_EQ("int a = 5;\n" 15146 "// line comment\n" 15147 "int oneTwoThree = 123;", 15148 format("int a = 5;\n" 15149 "// line comment\n" 15150 "int oneTwoThree=123;", 15151 Alignment)); 15152 15153 EXPECT_EQ("int a = 5;\n" 15154 "/*\n" 15155 " * multi-line block comment\n" 15156 " */\n" 15157 "int oneTwoThree = 123;", 15158 format("int a = 5;\n" 15159 "/*\n" 15160 " * multi-line block comment\n" 15161 " */\n" 15162 "int oneTwoThree=123;", 15163 Alignment)); 15164 15165 EXPECT_EQ("int a = 5;\n" 15166 "//\n" 15167 "// multi-line line comment\n" 15168 "//\n" 15169 "int oneTwoThree = 123;", 15170 format("int a = 5;\n" 15171 "//\n" 15172 "// multi-line line comment\n" 15173 "//\n" 15174 "int oneTwoThree=123;", 15175 Alignment)); 15176 15177 /* Test across comments and newlines */ 15178 EXPECT_EQ("int a = 5;\n" 15179 "\n" 15180 "/* block comment */\n" 15181 "int oneTwoThree = 123;", 15182 format("int a = 5;\n" 15183 "\n" 15184 "/* block comment */\n" 15185 "int oneTwoThree=123;", 15186 Alignment)); 15187 15188 EXPECT_EQ("int a = 5;\n" 15189 "\n" 15190 "// line comment\n" 15191 "int oneTwoThree = 123;", 15192 format("int a = 5;\n" 15193 "\n" 15194 "// line comment\n" 15195 "int oneTwoThree=123;", 15196 Alignment)); 15197 } 15198 15199 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) { 15200 FormatStyle Alignment = getLLVMStyle(); 15201 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15202 Alignment.AlignConsecutiveAssignments = 15203 FormatStyle::ACS_AcrossEmptyLinesAndComments; 15204 verifyFormat("int a = 5;\n" 15205 "int oneTwoThree = 123;", 15206 Alignment); 15207 verifyFormat("int a = method();\n" 15208 "int oneTwoThree = 133;", 15209 Alignment); 15210 verifyFormat("a &= 5;\n" 15211 "bcd *= 5;\n" 15212 "ghtyf += 5;\n" 15213 "dvfvdb -= 5;\n" 15214 "a /= 5;\n" 15215 "vdsvsv %= 5;\n" 15216 "sfdbddfbdfbb ^= 5;\n" 15217 "dvsdsv |= 5;\n" 15218 "int dsvvdvsdvvv = 123;", 15219 Alignment); 15220 verifyFormat("int i = 1, j = 10;\n" 15221 "something = 2000;", 15222 Alignment); 15223 verifyFormat("something = 2000;\n" 15224 "int i = 1, j = 10;\n", 15225 Alignment); 15226 verifyFormat("something = 2000;\n" 15227 "another = 911;\n" 15228 "int i = 1, j = 10;\n" 15229 "oneMore = 1;\n" 15230 "i = 2;", 15231 Alignment); 15232 verifyFormat("int a = 5;\n" 15233 "int one = 1;\n" 15234 "method();\n" 15235 "int oneTwoThree = 123;\n" 15236 "int oneTwo = 12;", 15237 Alignment); 15238 verifyFormat("int oneTwoThree = 123;\n" 15239 "int oneTwo = 12;\n" 15240 "method();\n", 15241 Alignment); 15242 verifyFormat("int oneTwoThree = 123; // comment\n" 15243 "int oneTwo = 12; // comment", 15244 Alignment); 15245 15246 // Bug 25167 15247 /* Uncomment when fixed 15248 verifyFormat("#if A\n" 15249 "#else\n" 15250 "int aaaaaaaa = 12;\n" 15251 "#endif\n" 15252 "#if B\n" 15253 "#else\n" 15254 "int a = 12;\n" 15255 "#endif\n", 15256 Alignment); 15257 verifyFormat("enum foo {\n" 15258 "#if A\n" 15259 "#else\n" 15260 " aaaaaaaa = 12;\n" 15261 "#endif\n" 15262 "#if B\n" 15263 "#else\n" 15264 " a = 12;\n" 15265 "#endif\n" 15266 "};\n", 15267 Alignment); 15268 */ 15269 15270 Alignment.MaxEmptyLinesToKeep = 10; 15271 /* Test alignment across empty lines */ 15272 EXPECT_EQ("int a = 5;\n" 15273 "\n" 15274 "int oneTwoThree = 123;", 15275 format("int a = 5;\n" 15276 "\n" 15277 "int oneTwoThree= 123;", 15278 Alignment)); 15279 EXPECT_EQ("int a = 5;\n" 15280 "int one = 1;\n" 15281 "\n" 15282 "int oneTwoThree = 123;", 15283 format("int a = 5;\n" 15284 "int one = 1;\n" 15285 "\n" 15286 "int oneTwoThree = 123;", 15287 Alignment)); 15288 EXPECT_EQ("int a = 5;\n" 15289 "int one = 1;\n" 15290 "\n" 15291 "int oneTwoThree = 123;\n" 15292 "int oneTwo = 12;", 15293 format("int a = 5;\n" 15294 "int one = 1;\n" 15295 "\n" 15296 "int oneTwoThree = 123;\n" 15297 "int oneTwo = 12;", 15298 Alignment)); 15299 15300 /* Test across comments */ 15301 EXPECT_EQ("int a = 5;\n" 15302 "/* block comment */\n" 15303 "int oneTwoThree = 123;", 15304 format("int a = 5;\n" 15305 "/* block comment */\n" 15306 "int oneTwoThree=123;", 15307 Alignment)); 15308 15309 EXPECT_EQ("int a = 5;\n" 15310 "// line comment\n" 15311 "int oneTwoThree = 123;", 15312 format("int a = 5;\n" 15313 "// line comment\n" 15314 "int oneTwoThree=123;", 15315 Alignment)); 15316 15317 /* Test across comments and newlines */ 15318 EXPECT_EQ("int a = 5;\n" 15319 "\n" 15320 "/* block comment */\n" 15321 "int oneTwoThree = 123;", 15322 format("int a = 5;\n" 15323 "\n" 15324 "/* block comment */\n" 15325 "int oneTwoThree=123;", 15326 Alignment)); 15327 15328 EXPECT_EQ("int a = 5;\n" 15329 "\n" 15330 "// line comment\n" 15331 "int oneTwoThree = 123;", 15332 format("int a = 5;\n" 15333 "\n" 15334 "// line comment\n" 15335 "int oneTwoThree=123;", 15336 Alignment)); 15337 15338 EXPECT_EQ("int a = 5;\n" 15339 "//\n" 15340 "// multi-line line comment\n" 15341 "//\n" 15342 "int oneTwoThree = 123;", 15343 format("int a = 5;\n" 15344 "//\n" 15345 "// multi-line line comment\n" 15346 "//\n" 15347 "int oneTwoThree=123;", 15348 Alignment)); 15349 15350 EXPECT_EQ("int a = 5;\n" 15351 "/*\n" 15352 " * multi-line block comment\n" 15353 " */\n" 15354 "int oneTwoThree = 123;", 15355 format("int a = 5;\n" 15356 "/*\n" 15357 " * multi-line block comment\n" 15358 " */\n" 15359 "int oneTwoThree=123;", 15360 Alignment)); 15361 15362 EXPECT_EQ("int a = 5;\n" 15363 "\n" 15364 "/* block comment */\n" 15365 "\n" 15366 "\n" 15367 "\n" 15368 "int oneTwoThree = 123;", 15369 format("int a = 5;\n" 15370 "\n" 15371 "/* block comment */\n" 15372 "\n" 15373 "\n" 15374 "\n" 15375 "int oneTwoThree=123;", 15376 Alignment)); 15377 15378 EXPECT_EQ("int a = 5;\n" 15379 "\n" 15380 "// line comment\n" 15381 "\n" 15382 "\n" 15383 "\n" 15384 "int oneTwoThree = 123;", 15385 format("int a = 5;\n" 15386 "\n" 15387 "// line comment\n" 15388 "\n" 15389 "\n" 15390 "\n" 15391 "int oneTwoThree=123;", 15392 Alignment)); 15393 15394 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 15395 verifyFormat("#define A \\\n" 15396 " int aaaa = 12; \\\n" 15397 " int b = 23; \\\n" 15398 " int ccc = 234; \\\n" 15399 " int dddddddddd = 2345;", 15400 Alignment); 15401 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 15402 verifyFormat("#define A \\\n" 15403 " int aaaa = 12; \\\n" 15404 " int b = 23; \\\n" 15405 " int ccc = 234; \\\n" 15406 " int dddddddddd = 2345;", 15407 Alignment); 15408 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 15409 verifyFormat("#define A " 15410 " \\\n" 15411 " int aaaa = 12; " 15412 " \\\n" 15413 " int b = 23; " 15414 " \\\n" 15415 " int ccc = 234; " 15416 " \\\n" 15417 " int dddddddddd = 2345;", 15418 Alignment); 15419 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 15420 "k = 4, int l = 5,\n" 15421 " int m = 6) {\n" 15422 " int j = 10;\n" 15423 " otherThing = 1;\n" 15424 "}", 15425 Alignment); 15426 verifyFormat("void SomeFunction(int parameter = 0) {\n" 15427 " int i = 1;\n" 15428 " int j = 2;\n" 15429 " int big = 10000;\n" 15430 "}", 15431 Alignment); 15432 verifyFormat("class C {\n" 15433 "public:\n" 15434 " int i = 1;\n" 15435 " virtual void f() = 0;\n" 15436 "};", 15437 Alignment); 15438 verifyFormat("int i = 1;\n" 15439 "if (SomeType t = getSomething()) {\n" 15440 "}\n" 15441 "int j = 2;\n" 15442 "int big = 10000;", 15443 Alignment); 15444 verifyFormat("int j = 7;\n" 15445 "for (int k = 0; k < N; ++k) {\n" 15446 "}\n" 15447 "int j = 2;\n" 15448 "int big = 10000;\n" 15449 "}", 15450 Alignment); 15451 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 15452 verifyFormat("int i = 1;\n" 15453 "LooooooooooongType loooooooooooooooooooooongVariable\n" 15454 " = someLooooooooooooooooongFunction();\n" 15455 "int j = 2;", 15456 Alignment); 15457 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 15458 verifyFormat("int i = 1;\n" 15459 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 15460 " someLooooooooooooooooongFunction();\n" 15461 "int j = 2;", 15462 Alignment); 15463 15464 verifyFormat("auto lambda = []() {\n" 15465 " auto i = 0;\n" 15466 " return 0;\n" 15467 "};\n" 15468 "int i = 0;\n" 15469 "auto v = type{\n" 15470 " i = 1, //\n" 15471 " (i = 2), //\n" 15472 " i = 3 //\n" 15473 "};", 15474 Alignment); 15475 15476 verifyFormat( 15477 "int i = 1;\n" 15478 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 15479 " loooooooooooooooooooooongParameterB);\n" 15480 "int j = 2;", 15481 Alignment); 15482 15483 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 15484 " typename B = very_long_type_name_1,\n" 15485 " typename T_2 = very_long_type_name_2>\n" 15486 "auto foo() {}\n", 15487 Alignment); 15488 verifyFormat("int a, b = 1;\n" 15489 "int c = 2;\n" 15490 "int dd = 3;\n", 15491 Alignment); 15492 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 15493 "float b[1][] = {{3.f}};\n", 15494 Alignment); 15495 verifyFormat("for (int i = 0; i < 1; i++)\n" 15496 " int x = 1;\n", 15497 Alignment); 15498 verifyFormat("for (i = 0; i < 1; i++)\n" 15499 " x = 1;\n" 15500 "y = 1;\n", 15501 Alignment); 15502 15503 Alignment.ReflowComments = true; 15504 Alignment.ColumnLimit = 50; 15505 EXPECT_EQ("int x = 0;\n" 15506 "int yy = 1; /// specificlennospace\n" 15507 "int zzz = 2;\n", 15508 format("int x = 0;\n" 15509 "int yy = 1; ///specificlennospace\n" 15510 "int zzz = 2;\n", 15511 Alignment)); 15512 } 15513 15514 TEST_F(FormatTest, AlignConsecutiveAssignments) { 15515 FormatStyle Alignment = getLLVMStyle(); 15516 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15517 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 15518 verifyFormat("int a = 5;\n" 15519 "int oneTwoThree = 123;", 15520 Alignment); 15521 verifyFormat("int a = 5;\n" 15522 "int oneTwoThree = 123;", 15523 Alignment); 15524 15525 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 15526 verifyFormat("int a = 5;\n" 15527 "int oneTwoThree = 123;", 15528 Alignment); 15529 verifyFormat("int a = method();\n" 15530 "int oneTwoThree = 133;", 15531 Alignment); 15532 verifyFormat("a &= 5;\n" 15533 "bcd *= 5;\n" 15534 "ghtyf += 5;\n" 15535 "dvfvdb -= 5;\n" 15536 "a /= 5;\n" 15537 "vdsvsv %= 5;\n" 15538 "sfdbddfbdfbb ^= 5;\n" 15539 "dvsdsv |= 5;\n" 15540 "int dsvvdvsdvvv = 123;", 15541 Alignment); 15542 verifyFormat("int i = 1, j = 10;\n" 15543 "something = 2000;", 15544 Alignment); 15545 verifyFormat("something = 2000;\n" 15546 "int i = 1, j = 10;\n", 15547 Alignment); 15548 verifyFormat("something = 2000;\n" 15549 "another = 911;\n" 15550 "int i = 1, j = 10;\n" 15551 "oneMore = 1;\n" 15552 "i = 2;", 15553 Alignment); 15554 verifyFormat("int a = 5;\n" 15555 "int one = 1;\n" 15556 "method();\n" 15557 "int oneTwoThree = 123;\n" 15558 "int oneTwo = 12;", 15559 Alignment); 15560 verifyFormat("int oneTwoThree = 123;\n" 15561 "int oneTwo = 12;\n" 15562 "method();\n", 15563 Alignment); 15564 verifyFormat("int oneTwoThree = 123; // comment\n" 15565 "int oneTwo = 12; // comment", 15566 Alignment); 15567 15568 // Bug 25167 15569 /* Uncomment when fixed 15570 verifyFormat("#if A\n" 15571 "#else\n" 15572 "int aaaaaaaa = 12;\n" 15573 "#endif\n" 15574 "#if B\n" 15575 "#else\n" 15576 "int a = 12;\n" 15577 "#endif\n", 15578 Alignment); 15579 verifyFormat("enum foo {\n" 15580 "#if A\n" 15581 "#else\n" 15582 " aaaaaaaa = 12;\n" 15583 "#endif\n" 15584 "#if B\n" 15585 "#else\n" 15586 " a = 12;\n" 15587 "#endif\n" 15588 "};\n", 15589 Alignment); 15590 */ 15591 15592 EXPECT_EQ("int a = 5;\n" 15593 "\n" 15594 "int oneTwoThree = 123;", 15595 format("int a = 5;\n" 15596 "\n" 15597 "int oneTwoThree= 123;", 15598 Alignment)); 15599 EXPECT_EQ("int a = 5;\n" 15600 "int one = 1;\n" 15601 "\n" 15602 "int oneTwoThree = 123;", 15603 format("int a = 5;\n" 15604 "int one = 1;\n" 15605 "\n" 15606 "int oneTwoThree = 123;", 15607 Alignment)); 15608 EXPECT_EQ("int a = 5;\n" 15609 "int one = 1;\n" 15610 "\n" 15611 "int oneTwoThree = 123;\n" 15612 "int oneTwo = 12;", 15613 format("int a = 5;\n" 15614 "int one = 1;\n" 15615 "\n" 15616 "int oneTwoThree = 123;\n" 15617 "int oneTwo = 12;", 15618 Alignment)); 15619 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 15620 verifyFormat("#define A \\\n" 15621 " int aaaa = 12; \\\n" 15622 " int b = 23; \\\n" 15623 " int ccc = 234; \\\n" 15624 " int dddddddddd = 2345;", 15625 Alignment); 15626 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 15627 verifyFormat("#define A \\\n" 15628 " int aaaa = 12; \\\n" 15629 " int b = 23; \\\n" 15630 " int ccc = 234; \\\n" 15631 " int dddddddddd = 2345;", 15632 Alignment); 15633 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 15634 verifyFormat("#define A " 15635 " \\\n" 15636 " int aaaa = 12; " 15637 " \\\n" 15638 " int b = 23; " 15639 " \\\n" 15640 " int ccc = 234; " 15641 " \\\n" 15642 " int dddddddddd = 2345;", 15643 Alignment); 15644 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 15645 "k = 4, int l = 5,\n" 15646 " int m = 6) {\n" 15647 " int j = 10;\n" 15648 " otherThing = 1;\n" 15649 "}", 15650 Alignment); 15651 verifyFormat("void SomeFunction(int parameter = 0) {\n" 15652 " int i = 1;\n" 15653 " int j = 2;\n" 15654 " int big = 10000;\n" 15655 "}", 15656 Alignment); 15657 verifyFormat("class C {\n" 15658 "public:\n" 15659 " int i = 1;\n" 15660 " virtual void f() = 0;\n" 15661 "};", 15662 Alignment); 15663 verifyFormat("int i = 1;\n" 15664 "if (SomeType t = getSomething()) {\n" 15665 "}\n" 15666 "int j = 2;\n" 15667 "int big = 10000;", 15668 Alignment); 15669 verifyFormat("int j = 7;\n" 15670 "for (int k = 0; k < N; ++k) {\n" 15671 "}\n" 15672 "int j = 2;\n" 15673 "int big = 10000;\n" 15674 "}", 15675 Alignment); 15676 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 15677 verifyFormat("int i = 1;\n" 15678 "LooooooooooongType loooooooooooooooooooooongVariable\n" 15679 " = someLooooooooooooooooongFunction();\n" 15680 "int j = 2;", 15681 Alignment); 15682 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 15683 verifyFormat("int i = 1;\n" 15684 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 15685 " someLooooooooooooooooongFunction();\n" 15686 "int j = 2;", 15687 Alignment); 15688 15689 verifyFormat("auto lambda = []() {\n" 15690 " auto i = 0;\n" 15691 " return 0;\n" 15692 "};\n" 15693 "int i = 0;\n" 15694 "auto v = type{\n" 15695 " i = 1, //\n" 15696 " (i = 2), //\n" 15697 " i = 3 //\n" 15698 "};", 15699 Alignment); 15700 15701 verifyFormat( 15702 "int i = 1;\n" 15703 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 15704 " loooooooooooooooooooooongParameterB);\n" 15705 "int j = 2;", 15706 Alignment); 15707 15708 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 15709 " typename B = very_long_type_name_1,\n" 15710 " typename T_2 = very_long_type_name_2>\n" 15711 "auto foo() {}\n", 15712 Alignment); 15713 verifyFormat("int a, b = 1;\n" 15714 "int c = 2;\n" 15715 "int dd = 3;\n", 15716 Alignment); 15717 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 15718 "float b[1][] = {{3.f}};\n", 15719 Alignment); 15720 verifyFormat("for (int i = 0; i < 1; i++)\n" 15721 " int x = 1;\n", 15722 Alignment); 15723 verifyFormat("for (i = 0; i < 1; i++)\n" 15724 " x = 1;\n" 15725 "y = 1;\n", 15726 Alignment); 15727 15728 Alignment.ReflowComments = true; 15729 Alignment.ColumnLimit = 50; 15730 EXPECT_EQ("int x = 0;\n" 15731 "int yy = 1; /// specificlennospace\n" 15732 "int zzz = 2;\n", 15733 format("int x = 0;\n" 15734 "int yy = 1; ///specificlennospace\n" 15735 "int zzz = 2;\n", 15736 Alignment)); 15737 } 15738 15739 TEST_F(FormatTest, AlignConsecutiveBitFields) { 15740 FormatStyle Alignment = getLLVMStyle(); 15741 Alignment.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive; 15742 verifyFormat("int const a : 5;\n" 15743 "int oneTwoThree : 23;", 15744 Alignment); 15745 15746 // Initializers are allowed starting with c++2a 15747 verifyFormat("int const a : 5 = 1;\n" 15748 "int oneTwoThree : 23 = 0;", 15749 Alignment); 15750 15751 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 15752 verifyFormat("int const a : 5;\n" 15753 "int oneTwoThree : 23;", 15754 Alignment); 15755 15756 verifyFormat("int const a : 5; // comment\n" 15757 "int oneTwoThree : 23; // comment", 15758 Alignment); 15759 15760 verifyFormat("int const a : 5 = 1;\n" 15761 "int oneTwoThree : 23 = 0;", 15762 Alignment); 15763 15764 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 15765 verifyFormat("int const a : 5 = 1;\n" 15766 "int oneTwoThree : 23 = 0;", 15767 Alignment); 15768 verifyFormat("int const a : 5 = {1};\n" 15769 "int oneTwoThree : 23 = 0;", 15770 Alignment); 15771 15772 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None; 15773 verifyFormat("int const a :5;\n" 15774 "int oneTwoThree:23;", 15775 Alignment); 15776 15777 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before; 15778 verifyFormat("int const a :5;\n" 15779 "int oneTwoThree :23;", 15780 Alignment); 15781 15782 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After; 15783 verifyFormat("int const a : 5;\n" 15784 "int oneTwoThree: 23;", 15785 Alignment); 15786 15787 // Known limitations: ':' is only recognized as a bitfield colon when 15788 // followed by a number. 15789 /* 15790 verifyFormat("int oneTwoThree : SOME_CONSTANT;\n" 15791 "int a : 5;", 15792 Alignment); 15793 */ 15794 } 15795 15796 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 15797 FormatStyle Alignment = getLLVMStyle(); 15798 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15799 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_None; 15800 Alignment.PointerAlignment = FormatStyle::PAS_Right; 15801 verifyFormat("float const a = 5;\n" 15802 "int oneTwoThree = 123;", 15803 Alignment); 15804 verifyFormat("int a = 5;\n" 15805 "float const oneTwoThree = 123;", 15806 Alignment); 15807 15808 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 15809 verifyFormat("float const a = 5;\n" 15810 "int oneTwoThree = 123;", 15811 Alignment); 15812 verifyFormat("int a = method();\n" 15813 "float const oneTwoThree = 133;", 15814 Alignment); 15815 verifyFormat("int i = 1, j = 10;\n" 15816 "something = 2000;", 15817 Alignment); 15818 verifyFormat("something = 2000;\n" 15819 "int i = 1, j = 10;\n", 15820 Alignment); 15821 verifyFormat("float something = 2000;\n" 15822 "double another = 911;\n" 15823 "int i = 1, j = 10;\n" 15824 "const int *oneMore = 1;\n" 15825 "unsigned i = 2;", 15826 Alignment); 15827 verifyFormat("float a = 5;\n" 15828 "int one = 1;\n" 15829 "method();\n" 15830 "const double oneTwoThree = 123;\n" 15831 "const unsigned int oneTwo = 12;", 15832 Alignment); 15833 verifyFormat("int oneTwoThree{0}; // comment\n" 15834 "unsigned oneTwo; // comment", 15835 Alignment); 15836 verifyFormat("unsigned int *a;\n" 15837 "int *b;\n" 15838 "unsigned int Const *c;\n" 15839 "unsigned int const *d;\n" 15840 "unsigned int Const &e;\n" 15841 "unsigned int const &f;", 15842 Alignment); 15843 verifyFormat("Const unsigned int *c;\n" 15844 "const unsigned int *d;\n" 15845 "Const unsigned int &e;\n" 15846 "const unsigned int &f;\n" 15847 "const unsigned g;\n" 15848 "Const unsigned h;", 15849 Alignment); 15850 EXPECT_EQ("float const a = 5;\n" 15851 "\n" 15852 "int oneTwoThree = 123;", 15853 format("float const a = 5;\n" 15854 "\n" 15855 "int oneTwoThree= 123;", 15856 Alignment)); 15857 EXPECT_EQ("float a = 5;\n" 15858 "int one = 1;\n" 15859 "\n" 15860 "unsigned oneTwoThree = 123;", 15861 format("float a = 5;\n" 15862 "int one = 1;\n" 15863 "\n" 15864 "unsigned oneTwoThree = 123;", 15865 Alignment)); 15866 EXPECT_EQ("float a = 5;\n" 15867 "int one = 1;\n" 15868 "\n" 15869 "unsigned oneTwoThree = 123;\n" 15870 "int oneTwo = 12;", 15871 format("float a = 5;\n" 15872 "int one = 1;\n" 15873 "\n" 15874 "unsigned oneTwoThree = 123;\n" 15875 "int oneTwo = 12;", 15876 Alignment)); 15877 // Function prototype alignment 15878 verifyFormat("int a();\n" 15879 "double b();", 15880 Alignment); 15881 verifyFormat("int a(int x);\n" 15882 "double b();", 15883 Alignment); 15884 unsigned OldColumnLimit = Alignment.ColumnLimit; 15885 // We need to set ColumnLimit to zero, in order to stress nested alignments, 15886 // otherwise the function parameters will be re-flowed onto a single line. 15887 Alignment.ColumnLimit = 0; 15888 EXPECT_EQ("int a(int x,\n" 15889 " float y);\n" 15890 "double b(int x,\n" 15891 " double y);", 15892 format("int a(int x,\n" 15893 " float y);\n" 15894 "double b(int x,\n" 15895 " double y);", 15896 Alignment)); 15897 // This ensures that function parameters of function declarations are 15898 // correctly indented when their owning functions are indented. 15899 // The failure case here is for 'double y' to not be indented enough. 15900 EXPECT_EQ("double a(int x);\n" 15901 "int b(int y,\n" 15902 " double z);", 15903 format("double a(int x);\n" 15904 "int b(int y,\n" 15905 " double z);", 15906 Alignment)); 15907 // Set ColumnLimit low so that we induce wrapping immediately after 15908 // the function name and opening paren. 15909 Alignment.ColumnLimit = 13; 15910 verifyFormat("int function(\n" 15911 " int x,\n" 15912 " bool y);", 15913 Alignment); 15914 Alignment.ColumnLimit = OldColumnLimit; 15915 // Ensure function pointers don't screw up recursive alignment 15916 verifyFormat("int a(int x, void (*fp)(int y));\n" 15917 "double b();", 15918 Alignment); 15919 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 15920 // Ensure recursive alignment is broken by function braces, so that the 15921 // "a = 1" does not align with subsequent assignments inside the function 15922 // body. 15923 verifyFormat("int func(int a = 1) {\n" 15924 " int b = 2;\n" 15925 " int cc = 3;\n" 15926 "}", 15927 Alignment); 15928 verifyFormat("float something = 2000;\n" 15929 "double another = 911;\n" 15930 "int i = 1, j = 10;\n" 15931 "const int *oneMore = 1;\n" 15932 "unsigned i = 2;", 15933 Alignment); 15934 verifyFormat("int oneTwoThree = {0}; // comment\n" 15935 "unsigned oneTwo = 0; // comment", 15936 Alignment); 15937 // Make sure that scope is correctly tracked, in the absence of braces 15938 verifyFormat("for (int i = 0; i < n; i++)\n" 15939 " j = i;\n" 15940 "double x = 1;\n", 15941 Alignment); 15942 verifyFormat("if (int i = 0)\n" 15943 " j = i;\n" 15944 "double x = 1;\n", 15945 Alignment); 15946 // Ensure operator[] and operator() are comprehended 15947 verifyFormat("struct test {\n" 15948 " long long int foo();\n" 15949 " int operator[](int a);\n" 15950 " double bar();\n" 15951 "};\n", 15952 Alignment); 15953 verifyFormat("struct test {\n" 15954 " long long int foo();\n" 15955 " int operator()(int a);\n" 15956 " double bar();\n" 15957 "};\n", 15958 Alignment); 15959 15960 // PAS_Right 15961 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 15962 " int const i = 1;\n" 15963 " int *j = 2;\n" 15964 " int big = 10000;\n" 15965 "\n" 15966 " unsigned oneTwoThree = 123;\n" 15967 " int oneTwo = 12;\n" 15968 " method();\n" 15969 " float k = 2;\n" 15970 " int ll = 10000;\n" 15971 "}", 15972 format("void SomeFunction(int parameter= 0) {\n" 15973 " int const i= 1;\n" 15974 " int *j=2;\n" 15975 " int big = 10000;\n" 15976 "\n" 15977 "unsigned oneTwoThree =123;\n" 15978 "int oneTwo = 12;\n" 15979 " method();\n" 15980 "float k= 2;\n" 15981 "int ll=10000;\n" 15982 "}", 15983 Alignment)); 15984 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 15985 " int const i = 1;\n" 15986 " int **j = 2, ***k;\n" 15987 " int &k = i;\n" 15988 " int &&l = i + j;\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 format("void SomeFunction(int parameter= 0) {\n" 15998 " int const i= 1;\n" 15999 " int **j=2,***k;\n" 16000 "int &k=i;\n" 16001 "int &&l=i+j;\n" 16002 " int big = 10000;\n" 16003 "\n" 16004 "unsigned oneTwoThree =123;\n" 16005 "int oneTwo = 12;\n" 16006 " method();\n" 16007 "float k= 2;\n" 16008 "int ll=10000;\n" 16009 "}", 16010 Alignment)); 16011 // variables are aligned at their name, pointers are at the right most 16012 // position 16013 verifyFormat("int *a;\n" 16014 "int **b;\n" 16015 "int ***c;\n" 16016 "int foobar;\n", 16017 Alignment); 16018 16019 // PAS_Left 16020 FormatStyle AlignmentLeft = Alignment; 16021 AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left; 16022 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16023 " int const i = 1;\n" 16024 " int* j = 2;\n" 16025 " int big = 10000;\n" 16026 "\n" 16027 " unsigned oneTwoThree = 123;\n" 16028 " int oneTwo = 12;\n" 16029 " method();\n" 16030 " float k = 2;\n" 16031 " int ll = 10000;\n" 16032 "}", 16033 format("void SomeFunction(int parameter= 0) {\n" 16034 " int const i= 1;\n" 16035 " int *j=2;\n" 16036 " int big = 10000;\n" 16037 "\n" 16038 "unsigned oneTwoThree =123;\n" 16039 "int oneTwo = 12;\n" 16040 " method();\n" 16041 "float k= 2;\n" 16042 "int ll=10000;\n" 16043 "}", 16044 AlignmentLeft)); 16045 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16046 " int const i = 1;\n" 16047 " int** j = 2;\n" 16048 " int& k = i;\n" 16049 " int&& l = i + j;\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 format("void SomeFunction(int parameter= 0) {\n" 16059 " int const i= 1;\n" 16060 " int **j=2;\n" 16061 "int &k=i;\n" 16062 "int &&l=i+j;\n" 16063 " int big = 10000;\n" 16064 "\n" 16065 "unsigned oneTwoThree =123;\n" 16066 "int oneTwo = 12;\n" 16067 " method();\n" 16068 "float k= 2;\n" 16069 "int ll=10000;\n" 16070 "}", 16071 AlignmentLeft)); 16072 // variables are aligned at their name, pointers are at the left most position 16073 verifyFormat("int* a;\n" 16074 "int** b;\n" 16075 "int*** c;\n" 16076 "int foobar;\n", 16077 AlignmentLeft); 16078 16079 // PAS_Middle 16080 FormatStyle AlignmentMiddle = Alignment; 16081 AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle; 16082 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16083 " int const i = 1;\n" 16084 " int * j = 2;\n" 16085 " int big = 10000;\n" 16086 "\n" 16087 " unsigned oneTwoThree = 123;\n" 16088 " int oneTwo = 12;\n" 16089 " method();\n" 16090 " float k = 2;\n" 16091 " int ll = 10000;\n" 16092 "}", 16093 format("void SomeFunction(int parameter= 0) {\n" 16094 " int const i= 1;\n" 16095 " int *j=2;\n" 16096 " int big = 10000;\n" 16097 "\n" 16098 "unsigned oneTwoThree =123;\n" 16099 "int oneTwo = 12;\n" 16100 " method();\n" 16101 "float k= 2;\n" 16102 "int ll=10000;\n" 16103 "}", 16104 AlignmentMiddle)); 16105 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16106 " int const i = 1;\n" 16107 " int ** j = 2, ***k;\n" 16108 " int & k = i;\n" 16109 " int && l = i + j;\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 format("void SomeFunction(int parameter= 0) {\n" 16119 " int const i= 1;\n" 16120 " int **j=2,***k;\n" 16121 "int &k=i;\n" 16122 "int &&l=i+j;\n" 16123 " int big = 10000;\n" 16124 "\n" 16125 "unsigned oneTwoThree =123;\n" 16126 "int oneTwo = 12;\n" 16127 " method();\n" 16128 "float k= 2;\n" 16129 "int ll=10000;\n" 16130 "}", 16131 AlignmentMiddle)); 16132 // variables are aligned at their name, pointers are in the middle 16133 verifyFormat("int * a;\n" 16134 "int * b;\n" 16135 "int *** c;\n" 16136 "int foobar;\n", 16137 AlignmentMiddle); 16138 16139 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16140 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 16141 verifyFormat("#define A \\\n" 16142 " int aaaa = 12; \\\n" 16143 " float b = 23; \\\n" 16144 " const int ccc = 234; \\\n" 16145 " unsigned dddddddddd = 2345;", 16146 Alignment); 16147 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 16148 verifyFormat("#define A \\\n" 16149 " int aaaa = 12; \\\n" 16150 " float b = 23; \\\n" 16151 " const int ccc = 234; \\\n" 16152 " unsigned dddddddddd = 2345;", 16153 Alignment); 16154 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 16155 Alignment.ColumnLimit = 30; 16156 verifyFormat("#define A \\\n" 16157 " int aaaa = 12; \\\n" 16158 " float b = 23; \\\n" 16159 " const int ccc = 234; \\\n" 16160 " int dddddddddd = 2345;", 16161 Alignment); 16162 Alignment.ColumnLimit = 80; 16163 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 16164 "k = 4, int l = 5,\n" 16165 " int m = 6) {\n" 16166 " const int j = 10;\n" 16167 " otherThing = 1;\n" 16168 "}", 16169 Alignment); 16170 verifyFormat("void SomeFunction(int parameter = 0) {\n" 16171 " int const i = 1;\n" 16172 " int *j = 2;\n" 16173 " int big = 10000;\n" 16174 "}", 16175 Alignment); 16176 verifyFormat("class C {\n" 16177 "public:\n" 16178 " int i = 1;\n" 16179 " virtual void f() = 0;\n" 16180 "};", 16181 Alignment); 16182 verifyFormat("float i = 1;\n" 16183 "if (SomeType t = getSomething()) {\n" 16184 "}\n" 16185 "const unsigned j = 2;\n" 16186 "int big = 10000;", 16187 Alignment); 16188 verifyFormat("float j = 7;\n" 16189 "for (int k = 0; k < N; ++k) {\n" 16190 "}\n" 16191 "unsigned j = 2;\n" 16192 "int big = 10000;\n" 16193 "}", 16194 Alignment); 16195 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 16196 verifyFormat("float i = 1;\n" 16197 "LooooooooooongType loooooooooooooooooooooongVariable\n" 16198 " = someLooooooooooooooooongFunction();\n" 16199 "int j = 2;", 16200 Alignment); 16201 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 16202 verifyFormat("int i = 1;\n" 16203 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 16204 " someLooooooooooooooooongFunction();\n" 16205 "int j = 2;", 16206 Alignment); 16207 16208 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16209 verifyFormat("auto lambda = []() {\n" 16210 " auto ii = 0;\n" 16211 " float j = 0;\n" 16212 " return 0;\n" 16213 "};\n" 16214 "int i = 0;\n" 16215 "float i2 = 0;\n" 16216 "auto v = type{\n" 16217 " i = 1, //\n" 16218 " (i = 2), //\n" 16219 " i = 3 //\n" 16220 "};", 16221 Alignment); 16222 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16223 16224 verifyFormat( 16225 "int i = 1;\n" 16226 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 16227 " loooooooooooooooooooooongParameterB);\n" 16228 "int j = 2;", 16229 Alignment); 16230 16231 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 16232 // We expect declarations and assignments to align, as long as it doesn't 16233 // exceed the column limit, starting a new alignment sequence whenever it 16234 // happens. 16235 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16236 Alignment.ColumnLimit = 30; 16237 verifyFormat("float ii = 1;\n" 16238 "unsigned j = 2;\n" 16239 "int someVerylongVariable = 1;\n" 16240 "AnotherLongType ll = 123456;\n" 16241 "VeryVeryLongType k = 2;\n" 16242 "int myvar = 1;", 16243 Alignment); 16244 Alignment.ColumnLimit = 80; 16245 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16246 16247 verifyFormat( 16248 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 16249 " typename LongType, typename B>\n" 16250 "auto foo() {}\n", 16251 Alignment); 16252 verifyFormat("float a, b = 1;\n" 16253 "int c = 2;\n" 16254 "int dd = 3;\n", 16255 Alignment); 16256 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 16257 "float b[1][] = {{3.f}};\n", 16258 Alignment); 16259 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16260 verifyFormat("float a, b = 1;\n" 16261 "int c = 2;\n" 16262 "int dd = 3;\n", 16263 Alignment); 16264 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 16265 "float b[1][] = {{3.f}};\n", 16266 Alignment); 16267 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16268 16269 Alignment.ColumnLimit = 30; 16270 Alignment.BinPackParameters = false; 16271 verifyFormat("void foo(float a,\n" 16272 " float b,\n" 16273 " int c,\n" 16274 " uint32_t *d) {\n" 16275 " int *e = 0;\n" 16276 " float f = 0;\n" 16277 " double g = 0;\n" 16278 "}\n" 16279 "void bar(ino_t a,\n" 16280 " int b,\n" 16281 " uint32_t *c,\n" 16282 " bool d) {}\n", 16283 Alignment); 16284 Alignment.BinPackParameters = true; 16285 Alignment.ColumnLimit = 80; 16286 16287 // Bug 33507 16288 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 16289 verifyFormat( 16290 "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n" 16291 " static const Version verVs2017;\n" 16292 " return true;\n" 16293 "});\n", 16294 Alignment); 16295 Alignment.PointerAlignment = FormatStyle::PAS_Right; 16296 16297 // See llvm.org/PR35641 16298 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16299 verifyFormat("int func() { //\n" 16300 " int b;\n" 16301 " unsigned c;\n" 16302 "}", 16303 Alignment); 16304 16305 // See PR37175 16306 FormatStyle Style = getMozillaStyle(); 16307 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16308 EXPECT_EQ("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n" 16309 "foo(int a);", 16310 format("DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style)); 16311 16312 Alignment.PointerAlignment = FormatStyle::PAS_Left; 16313 verifyFormat("unsigned int* a;\n" 16314 "int* b;\n" 16315 "unsigned int Const* c;\n" 16316 "unsigned int const* d;\n" 16317 "unsigned int Const& e;\n" 16318 "unsigned int const& f;", 16319 Alignment); 16320 verifyFormat("Const unsigned int* c;\n" 16321 "const unsigned int* d;\n" 16322 "Const unsigned int& e;\n" 16323 "const unsigned int& f;\n" 16324 "const unsigned g;\n" 16325 "Const unsigned h;", 16326 Alignment); 16327 16328 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 16329 verifyFormat("unsigned int * a;\n" 16330 "int * b;\n" 16331 "unsigned int Const * c;\n" 16332 "unsigned int const * d;\n" 16333 "unsigned int Const & e;\n" 16334 "unsigned int const & f;", 16335 Alignment); 16336 verifyFormat("Const unsigned int * c;\n" 16337 "const unsigned int * d;\n" 16338 "Const unsigned int & e;\n" 16339 "const unsigned int & f;\n" 16340 "const unsigned g;\n" 16341 "Const unsigned h;", 16342 Alignment); 16343 } 16344 16345 TEST_F(FormatTest, AlignWithLineBreaks) { 16346 auto Style = getLLVMStyleWithColumns(120); 16347 16348 EXPECT_EQ(Style.AlignConsecutiveAssignments, FormatStyle::ACS_None); 16349 EXPECT_EQ(Style.AlignConsecutiveDeclarations, FormatStyle::ACS_None); 16350 verifyFormat("void foo() {\n" 16351 " int myVar = 5;\n" 16352 " double x = 3.14;\n" 16353 " auto str = \"Hello \"\n" 16354 " \"World\";\n" 16355 " auto s = \"Hello \"\n" 16356 " \"Again\";\n" 16357 "}", 16358 Style); 16359 16360 // clang-format off 16361 verifyFormat("void foo() {\n" 16362 " const int capacityBefore = Entries.capacity();\n" 16363 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16364 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16365 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16366 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16367 "}", 16368 Style); 16369 // clang-format on 16370 16371 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16372 verifyFormat("void foo() {\n" 16373 " int myVar = 5;\n" 16374 " double x = 3.14;\n" 16375 " auto str = \"Hello \"\n" 16376 " \"World\";\n" 16377 " auto s = \"Hello \"\n" 16378 " \"Again\";\n" 16379 "}", 16380 Style); 16381 16382 // clang-format off 16383 verifyFormat("void foo() {\n" 16384 " const int capacityBefore = Entries.capacity();\n" 16385 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16386 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16387 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16388 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16389 "}", 16390 Style); 16391 // clang-format on 16392 16393 Style.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16394 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16395 verifyFormat("void foo() {\n" 16396 " int myVar = 5;\n" 16397 " double x = 3.14;\n" 16398 " auto str = \"Hello \"\n" 16399 " \"World\";\n" 16400 " auto s = \"Hello \"\n" 16401 " \"Again\";\n" 16402 "}", 16403 Style); 16404 16405 // clang-format off 16406 verifyFormat("void foo() {\n" 16407 " const int capacityBefore = Entries.capacity();\n" 16408 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16409 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16410 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16411 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16412 "}", 16413 Style); 16414 // clang-format on 16415 16416 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16417 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16418 16419 verifyFormat("void foo() {\n" 16420 " int myVar = 5;\n" 16421 " double x = 3.14;\n" 16422 " auto str = \"Hello \"\n" 16423 " \"World\";\n" 16424 " auto s = \"Hello \"\n" 16425 " \"Again\";\n" 16426 "}", 16427 Style); 16428 16429 // clang-format off 16430 verifyFormat("void foo() {\n" 16431 " const int capacityBefore = Entries.capacity();\n" 16432 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16433 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16434 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16435 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16436 "}", 16437 Style); 16438 // clang-format on 16439 16440 Style = getLLVMStyleWithColumns(120); 16441 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16442 Style.ContinuationIndentWidth = 4; 16443 Style.IndentWidth = 4; 16444 16445 // clang-format off 16446 verifyFormat("void SomeFunc() {\n" 16447 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 16448 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16449 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 16450 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16451 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 16452 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16453 "}", 16454 Style); 16455 // clang-format on 16456 16457 Style.BinPackArguments = false; 16458 16459 // clang-format off 16460 verifyFormat("void SomeFunc() {\n" 16461 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(\n" 16462 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16463 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(\n" 16464 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16465 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(\n" 16466 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16467 "}", 16468 Style); 16469 // clang-format on 16470 } 16471 16472 TEST_F(FormatTest, AlignWithInitializerPeriods) { 16473 auto Style = getLLVMStyleWithColumns(60); 16474 16475 verifyFormat("void foo1(void) {\n" 16476 " BYTE p[1] = 1;\n" 16477 " A B = {.one_foooooooooooooooo = 2,\n" 16478 " .two_fooooooooooooo = 3,\n" 16479 " .three_fooooooooooooo = 4};\n" 16480 " BYTE payload = 2;\n" 16481 "}", 16482 Style); 16483 16484 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16485 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_None; 16486 verifyFormat("void foo2(void) {\n" 16487 " BYTE p[1] = 1;\n" 16488 " A B = {.one_foooooooooooooooo = 2,\n" 16489 " .two_fooooooooooooo = 3,\n" 16490 " .three_fooooooooooooo = 4};\n" 16491 " BYTE payload = 2;\n" 16492 "}", 16493 Style); 16494 16495 Style.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16496 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16497 verifyFormat("void foo3(void) {\n" 16498 " BYTE p[1] = 1;\n" 16499 " A B = {.one_foooooooooooooooo = 2,\n" 16500 " .two_fooooooooooooo = 3,\n" 16501 " .three_fooooooooooooo = 4};\n" 16502 " BYTE payload = 2;\n" 16503 "}", 16504 Style); 16505 16506 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16507 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16508 verifyFormat("void foo4(void) {\n" 16509 " BYTE p[1] = 1;\n" 16510 " A B = {.one_foooooooooooooooo = 2,\n" 16511 " .two_fooooooooooooo = 3,\n" 16512 " .three_fooooooooooooo = 4};\n" 16513 " BYTE payload = 2;\n" 16514 "}", 16515 Style); 16516 } 16517 16518 TEST_F(FormatTest, LinuxBraceBreaking) { 16519 FormatStyle LinuxBraceStyle = getLLVMStyle(); 16520 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 16521 verifyFormat("namespace a\n" 16522 "{\n" 16523 "class A\n" 16524 "{\n" 16525 " void f()\n" 16526 " {\n" 16527 " if (true) {\n" 16528 " a();\n" 16529 " b();\n" 16530 " } else {\n" 16531 " a();\n" 16532 " }\n" 16533 " }\n" 16534 " void g() { return; }\n" 16535 "};\n" 16536 "struct B {\n" 16537 " int x;\n" 16538 "};\n" 16539 "} // namespace a\n", 16540 LinuxBraceStyle); 16541 verifyFormat("enum X {\n" 16542 " Y = 0,\n" 16543 "}\n", 16544 LinuxBraceStyle); 16545 verifyFormat("struct S {\n" 16546 " int Type;\n" 16547 " union {\n" 16548 " int x;\n" 16549 " double y;\n" 16550 " } Value;\n" 16551 " class C\n" 16552 " {\n" 16553 " MyFavoriteType Value;\n" 16554 " } Class;\n" 16555 "}\n", 16556 LinuxBraceStyle); 16557 } 16558 16559 TEST_F(FormatTest, MozillaBraceBreaking) { 16560 FormatStyle MozillaBraceStyle = getLLVMStyle(); 16561 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 16562 MozillaBraceStyle.FixNamespaceComments = false; 16563 verifyFormat("namespace a {\n" 16564 "class A\n" 16565 "{\n" 16566 " void f()\n" 16567 " {\n" 16568 " if (true) {\n" 16569 " a();\n" 16570 " b();\n" 16571 " }\n" 16572 " }\n" 16573 " void g() { return; }\n" 16574 "};\n" 16575 "enum E\n" 16576 "{\n" 16577 " A,\n" 16578 " // foo\n" 16579 " B,\n" 16580 " C\n" 16581 "};\n" 16582 "struct B\n" 16583 "{\n" 16584 " int x;\n" 16585 "};\n" 16586 "}\n", 16587 MozillaBraceStyle); 16588 verifyFormat("struct S\n" 16589 "{\n" 16590 " int Type;\n" 16591 " union\n" 16592 " {\n" 16593 " int x;\n" 16594 " double y;\n" 16595 " } Value;\n" 16596 " class C\n" 16597 " {\n" 16598 " MyFavoriteType Value;\n" 16599 " } Class;\n" 16600 "}\n", 16601 MozillaBraceStyle); 16602 } 16603 16604 TEST_F(FormatTest, StroustrupBraceBreaking) { 16605 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 16606 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 16607 verifyFormat("namespace a {\n" 16608 "class A {\n" 16609 " void f()\n" 16610 " {\n" 16611 " if (true) {\n" 16612 " a();\n" 16613 " b();\n" 16614 " }\n" 16615 " }\n" 16616 " void g() { return; }\n" 16617 "};\n" 16618 "struct B {\n" 16619 " int x;\n" 16620 "};\n" 16621 "} // namespace a\n", 16622 StroustrupBraceStyle); 16623 16624 verifyFormat("void foo()\n" 16625 "{\n" 16626 " if (a) {\n" 16627 " a();\n" 16628 " }\n" 16629 " else {\n" 16630 " b();\n" 16631 " }\n" 16632 "}\n", 16633 StroustrupBraceStyle); 16634 16635 verifyFormat("#ifdef _DEBUG\n" 16636 "int foo(int i = 0)\n" 16637 "#else\n" 16638 "int foo(int i = 5)\n" 16639 "#endif\n" 16640 "{\n" 16641 " return i;\n" 16642 "}", 16643 StroustrupBraceStyle); 16644 16645 verifyFormat("void foo() {}\n" 16646 "void bar()\n" 16647 "#ifdef _DEBUG\n" 16648 "{\n" 16649 " foo();\n" 16650 "}\n" 16651 "#else\n" 16652 "{\n" 16653 "}\n" 16654 "#endif", 16655 StroustrupBraceStyle); 16656 16657 verifyFormat("void foobar() { int i = 5; }\n" 16658 "#ifdef _DEBUG\n" 16659 "void bar() {}\n" 16660 "#else\n" 16661 "void bar() { foobar(); }\n" 16662 "#endif", 16663 StroustrupBraceStyle); 16664 } 16665 16666 TEST_F(FormatTest, AllmanBraceBreaking) { 16667 FormatStyle AllmanBraceStyle = getLLVMStyle(); 16668 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 16669 16670 EXPECT_EQ("namespace a\n" 16671 "{\n" 16672 "void f();\n" 16673 "void g();\n" 16674 "} // namespace a\n", 16675 format("namespace a\n" 16676 "{\n" 16677 "void f();\n" 16678 "void g();\n" 16679 "}\n", 16680 AllmanBraceStyle)); 16681 16682 verifyFormat("namespace a\n" 16683 "{\n" 16684 "class A\n" 16685 "{\n" 16686 " void f()\n" 16687 " {\n" 16688 " if (true)\n" 16689 " {\n" 16690 " a();\n" 16691 " b();\n" 16692 " }\n" 16693 " }\n" 16694 " void g() { return; }\n" 16695 "};\n" 16696 "struct B\n" 16697 "{\n" 16698 " int x;\n" 16699 "};\n" 16700 "union C\n" 16701 "{\n" 16702 "};\n" 16703 "} // namespace a", 16704 AllmanBraceStyle); 16705 16706 verifyFormat("void f()\n" 16707 "{\n" 16708 " if (true)\n" 16709 " {\n" 16710 " a();\n" 16711 " }\n" 16712 " else if (false)\n" 16713 " {\n" 16714 " b();\n" 16715 " }\n" 16716 " else\n" 16717 " {\n" 16718 " c();\n" 16719 " }\n" 16720 "}\n", 16721 AllmanBraceStyle); 16722 16723 verifyFormat("void f()\n" 16724 "{\n" 16725 " for (int i = 0; i < 10; ++i)\n" 16726 " {\n" 16727 " a();\n" 16728 " }\n" 16729 " while (false)\n" 16730 " {\n" 16731 " b();\n" 16732 " }\n" 16733 " do\n" 16734 " {\n" 16735 " c();\n" 16736 " } while (false)\n" 16737 "}\n", 16738 AllmanBraceStyle); 16739 16740 verifyFormat("void f(int a)\n" 16741 "{\n" 16742 " switch (a)\n" 16743 " {\n" 16744 " case 0:\n" 16745 " break;\n" 16746 " case 1:\n" 16747 " {\n" 16748 " break;\n" 16749 " }\n" 16750 " case 2:\n" 16751 " {\n" 16752 " }\n" 16753 " break;\n" 16754 " default:\n" 16755 " break;\n" 16756 " }\n" 16757 "}\n", 16758 AllmanBraceStyle); 16759 16760 verifyFormat("enum X\n" 16761 "{\n" 16762 " Y = 0,\n" 16763 "}\n", 16764 AllmanBraceStyle); 16765 verifyFormat("enum X\n" 16766 "{\n" 16767 " Y = 0\n" 16768 "}\n", 16769 AllmanBraceStyle); 16770 16771 verifyFormat("@interface BSApplicationController ()\n" 16772 "{\n" 16773 "@private\n" 16774 " id _extraIvar;\n" 16775 "}\n" 16776 "@end\n", 16777 AllmanBraceStyle); 16778 16779 verifyFormat("#ifdef _DEBUG\n" 16780 "int foo(int i = 0)\n" 16781 "#else\n" 16782 "int foo(int i = 5)\n" 16783 "#endif\n" 16784 "{\n" 16785 " return i;\n" 16786 "}", 16787 AllmanBraceStyle); 16788 16789 verifyFormat("void foo() {}\n" 16790 "void bar()\n" 16791 "#ifdef _DEBUG\n" 16792 "{\n" 16793 " foo();\n" 16794 "}\n" 16795 "#else\n" 16796 "{\n" 16797 "}\n" 16798 "#endif", 16799 AllmanBraceStyle); 16800 16801 verifyFormat("void foobar() { int i = 5; }\n" 16802 "#ifdef _DEBUG\n" 16803 "void bar() {}\n" 16804 "#else\n" 16805 "void bar() { foobar(); }\n" 16806 "#endif", 16807 AllmanBraceStyle); 16808 16809 EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine, 16810 FormatStyle::SLS_All); 16811 16812 verifyFormat("[](int i) { return i + 2; };\n" 16813 "[](int i, int j)\n" 16814 "{\n" 16815 " auto x = i + j;\n" 16816 " auto y = i * j;\n" 16817 " return x ^ y;\n" 16818 "};\n" 16819 "void foo()\n" 16820 "{\n" 16821 " auto shortLambda = [](int i) { return i + 2; };\n" 16822 " auto longLambda = [](int i, int j)\n" 16823 " {\n" 16824 " auto x = i + j;\n" 16825 " auto y = i * j;\n" 16826 " return x ^ y;\n" 16827 " };\n" 16828 "}", 16829 AllmanBraceStyle); 16830 16831 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 16832 16833 verifyFormat("[](int i)\n" 16834 "{\n" 16835 " return i + 2;\n" 16836 "};\n" 16837 "[](int i, int j)\n" 16838 "{\n" 16839 " auto x = i + j;\n" 16840 " auto y = i * j;\n" 16841 " return x ^ y;\n" 16842 "};\n" 16843 "void foo()\n" 16844 "{\n" 16845 " auto shortLambda = [](int i)\n" 16846 " {\n" 16847 " return i + 2;\n" 16848 " };\n" 16849 " auto longLambda = [](int i, int j)\n" 16850 " {\n" 16851 " auto x = i + j;\n" 16852 " auto y = i * j;\n" 16853 " return x ^ y;\n" 16854 " };\n" 16855 "}", 16856 AllmanBraceStyle); 16857 16858 // Reset 16859 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All; 16860 16861 // This shouldn't affect ObjC blocks.. 16862 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 16863 " // ...\n" 16864 " int i;\n" 16865 "}];", 16866 AllmanBraceStyle); 16867 verifyFormat("void (^block)(void) = ^{\n" 16868 " // ...\n" 16869 " int i;\n" 16870 "};", 16871 AllmanBraceStyle); 16872 // .. or dict literals. 16873 verifyFormat("void f()\n" 16874 "{\n" 16875 " // ...\n" 16876 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 16877 "}", 16878 AllmanBraceStyle); 16879 verifyFormat("void f()\n" 16880 "{\n" 16881 " // ...\n" 16882 " [object someMethod:@{a : @\"b\"}];\n" 16883 "}", 16884 AllmanBraceStyle); 16885 verifyFormat("int f()\n" 16886 "{ // comment\n" 16887 " return 42;\n" 16888 "}", 16889 AllmanBraceStyle); 16890 16891 AllmanBraceStyle.ColumnLimit = 19; 16892 verifyFormat("void f() { int i; }", AllmanBraceStyle); 16893 AllmanBraceStyle.ColumnLimit = 18; 16894 verifyFormat("void f()\n" 16895 "{\n" 16896 " int i;\n" 16897 "}", 16898 AllmanBraceStyle); 16899 AllmanBraceStyle.ColumnLimit = 80; 16900 16901 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 16902 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = 16903 FormatStyle::SIS_WithoutElse; 16904 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 16905 verifyFormat("void f(bool b)\n" 16906 "{\n" 16907 " if (b)\n" 16908 " {\n" 16909 " return;\n" 16910 " }\n" 16911 "}\n", 16912 BreakBeforeBraceShortIfs); 16913 verifyFormat("void f(bool b)\n" 16914 "{\n" 16915 " if constexpr (b)\n" 16916 " {\n" 16917 " return;\n" 16918 " }\n" 16919 "}\n", 16920 BreakBeforeBraceShortIfs); 16921 verifyFormat("void f(bool b)\n" 16922 "{\n" 16923 " if CONSTEXPR (b)\n" 16924 " {\n" 16925 " return;\n" 16926 " }\n" 16927 "}\n", 16928 BreakBeforeBraceShortIfs); 16929 verifyFormat("void f(bool b)\n" 16930 "{\n" 16931 " if (b) return;\n" 16932 "}\n", 16933 BreakBeforeBraceShortIfs); 16934 verifyFormat("void f(bool b)\n" 16935 "{\n" 16936 " if constexpr (b) return;\n" 16937 "}\n", 16938 BreakBeforeBraceShortIfs); 16939 verifyFormat("void f(bool b)\n" 16940 "{\n" 16941 " if CONSTEXPR (b) return;\n" 16942 "}\n", 16943 BreakBeforeBraceShortIfs); 16944 verifyFormat("void f(bool b)\n" 16945 "{\n" 16946 " while (b)\n" 16947 " {\n" 16948 " return;\n" 16949 " }\n" 16950 "}\n", 16951 BreakBeforeBraceShortIfs); 16952 } 16953 16954 TEST_F(FormatTest, WhitesmithsBraceBreaking) { 16955 FormatStyle WhitesmithsBraceStyle = getLLVMStyle(); 16956 WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 16957 16958 // Make a few changes to the style for testing purposes 16959 WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine = 16960 FormatStyle::SFS_Empty; 16961 WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 16962 WhitesmithsBraceStyle.ColumnLimit = 0; 16963 16964 // FIXME: this test case can't decide whether there should be a blank line 16965 // after the ~D() line or not. It adds one if one doesn't exist in the test 16966 // and it removes the line if one exists. 16967 /* 16968 verifyFormat("class A;\n" 16969 "namespace B\n" 16970 " {\n" 16971 "class C;\n" 16972 "// Comment\n" 16973 "class D\n" 16974 " {\n" 16975 "public:\n" 16976 " D();\n" 16977 " ~D() {}\n" 16978 "private:\n" 16979 " enum E\n" 16980 " {\n" 16981 " F\n" 16982 " }\n" 16983 " };\n" 16984 " } // namespace B\n", 16985 WhitesmithsBraceStyle); 16986 */ 16987 16988 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None; 16989 verifyFormat("namespace a\n" 16990 " {\n" 16991 "class A\n" 16992 " {\n" 16993 " void f()\n" 16994 " {\n" 16995 " if (true)\n" 16996 " {\n" 16997 " a();\n" 16998 " b();\n" 16999 " }\n" 17000 " }\n" 17001 " void g()\n" 17002 " {\n" 17003 " return;\n" 17004 " }\n" 17005 " };\n" 17006 "struct B\n" 17007 " {\n" 17008 " int x;\n" 17009 " };\n" 17010 " } // namespace a", 17011 WhitesmithsBraceStyle); 17012 17013 verifyFormat("namespace a\n" 17014 " {\n" 17015 "namespace b\n" 17016 " {\n" 17017 "class A\n" 17018 " {\n" 17019 " void f()\n" 17020 " {\n" 17021 " if (true)\n" 17022 " {\n" 17023 " a();\n" 17024 " b();\n" 17025 " }\n" 17026 " }\n" 17027 " void g()\n" 17028 " {\n" 17029 " return;\n" 17030 " }\n" 17031 " };\n" 17032 "struct B\n" 17033 " {\n" 17034 " int x;\n" 17035 " };\n" 17036 " } // namespace b\n" 17037 " } // namespace a", 17038 WhitesmithsBraceStyle); 17039 17040 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner; 17041 verifyFormat("namespace a\n" 17042 " {\n" 17043 "namespace b\n" 17044 " {\n" 17045 " class A\n" 17046 " {\n" 17047 " void f()\n" 17048 " {\n" 17049 " if (true)\n" 17050 " {\n" 17051 " a();\n" 17052 " b();\n" 17053 " }\n" 17054 " }\n" 17055 " void g()\n" 17056 " {\n" 17057 " return;\n" 17058 " }\n" 17059 " };\n" 17060 " struct B\n" 17061 " {\n" 17062 " int x;\n" 17063 " };\n" 17064 " } // namespace b\n" 17065 " } // namespace a", 17066 WhitesmithsBraceStyle); 17067 17068 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All; 17069 verifyFormat("namespace a\n" 17070 " {\n" 17071 " namespace b\n" 17072 " {\n" 17073 " class A\n" 17074 " {\n" 17075 " void f()\n" 17076 " {\n" 17077 " if (true)\n" 17078 " {\n" 17079 " a();\n" 17080 " b();\n" 17081 " }\n" 17082 " }\n" 17083 " void g()\n" 17084 " {\n" 17085 " return;\n" 17086 " }\n" 17087 " };\n" 17088 " struct B\n" 17089 " {\n" 17090 " int x;\n" 17091 " };\n" 17092 " } // namespace b\n" 17093 " } // namespace a", 17094 WhitesmithsBraceStyle); 17095 17096 verifyFormat("void f()\n" 17097 " {\n" 17098 " if (true)\n" 17099 " {\n" 17100 " a();\n" 17101 " }\n" 17102 " else if (false)\n" 17103 " {\n" 17104 " b();\n" 17105 " }\n" 17106 " else\n" 17107 " {\n" 17108 " c();\n" 17109 " }\n" 17110 " }\n", 17111 WhitesmithsBraceStyle); 17112 17113 verifyFormat("void f()\n" 17114 " {\n" 17115 " for (int i = 0; i < 10; ++i)\n" 17116 " {\n" 17117 " a();\n" 17118 " }\n" 17119 " while (false)\n" 17120 " {\n" 17121 " b();\n" 17122 " }\n" 17123 " do\n" 17124 " {\n" 17125 " c();\n" 17126 " } while (false)\n" 17127 " }\n", 17128 WhitesmithsBraceStyle); 17129 17130 WhitesmithsBraceStyle.IndentCaseLabels = true; 17131 verifyFormat("void switchTest1(int a)\n" 17132 " {\n" 17133 " switch (a)\n" 17134 " {\n" 17135 " case 2:\n" 17136 " {\n" 17137 " }\n" 17138 " break;\n" 17139 " }\n" 17140 " }\n", 17141 WhitesmithsBraceStyle); 17142 17143 verifyFormat("void switchTest2(int a)\n" 17144 " {\n" 17145 " switch (a)\n" 17146 " {\n" 17147 " case 0:\n" 17148 " break;\n" 17149 " case 1:\n" 17150 " {\n" 17151 " break;\n" 17152 " }\n" 17153 " case 2:\n" 17154 " {\n" 17155 " }\n" 17156 " break;\n" 17157 " default:\n" 17158 " break;\n" 17159 " }\n" 17160 " }\n", 17161 WhitesmithsBraceStyle); 17162 17163 verifyFormat("void switchTest3(int a)\n" 17164 " {\n" 17165 " switch (a)\n" 17166 " {\n" 17167 " case 0:\n" 17168 " {\n" 17169 " foo(x);\n" 17170 " }\n" 17171 " break;\n" 17172 " default:\n" 17173 " {\n" 17174 " foo(1);\n" 17175 " }\n" 17176 " break;\n" 17177 " }\n" 17178 " }\n", 17179 WhitesmithsBraceStyle); 17180 17181 WhitesmithsBraceStyle.IndentCaseLabels = false; 17182 17183 verifyFormat("void switchTest4(int a)\n" 17184 " {\n" 17185 " switch (a)\n" 17186 " {\n" 17187 " case 2:\n" 17188 " {\n" 17189 " }\n" 17190 " break;\n" 17191 " }\n" 17192 " }\n", 17193 WhitesmithsBraceStyle); 17194 17195 verifyFormat("void switchTest5(int a)\n" 17196 " {\n" 17197 " switch (a)\n" 17198 " {\n" 17199 " case 0:\n" 17200 " break;\n" 17201 " case 1:\n" 17202 " {\n" 17203 " foo();\n" 17204 " break;\n" 17205 " }\n" 17206 " case 2:\n" 17207 " {\n" 17208 " }\n" 17209 " break;\n" 17210 " default:\n" 17211 " break;\n" 17212 " }\n" 17213 " }\n", 17214 WhitesmithsBraceStyle); 17215 17216 verifyFormat("void switchTest6(int a)\n" 17217 " {\n" 17218 " switch (a)\n" 17219 " {\n" 17220 " case 0:\n" 17221 " {\n" 17222 " foo(x);\n" 17223 " }\n" 17224 " break;\n" 17225 " default:\n" 17226 " {\n" 17227 " foo(1);\n" 17228 " }\n" 17229 " break;\n" 17230 " }\n" 17231 " }\n", 17232 WhitesmithsBraceStyle); 17233 17234 verifyFormat("enum X\n" 17235 " {\n" 17236 " Y = 0, // testing\n" 17237 " }\n", 17238 WhitesmithsBraceStyle); 17239 17240 verifyFormat("enum X\n" 17241 " {\n" 17242 " Y = 0\n" 17243 " }\n", 17244 WhitesmithsBraceStyle); 17245 verifyFormat("enum X\n" 17246 " {\n" 17247 " Y = 0,\n" 17248 " Z = 1\n" 17249 " };\n", 17250 WhitesmithsBraceStyle); 17251 17252 verifyFormat("@interface BSApplicationController ()\n" 17253 " {\n" 17254 "@private\n" 17255 " id _extraIvar;\n" 17256 " }\n" 17257 "@end\n", 17258 WhitesmithsBraceStyle); 17259 17260 verifyFormat("#ifdef _DEBUG\n" 17261 "int foo(int i = 0)\n" 17262 "#else\n" 17263 "int foo(int i = 5)\n" 17264 "#endif\n" 17265 " {\n" 17266 " return i;\n" 17267 " }", 17268 WhitesmithsBraceStyle); 17269 17270 verifyFormat("void foo() {}\n" 17271 "void bar()\n" 17272 "#ifdef _DEBUG\n" 17273 " {\n" 17274 " foo();\n" 17275 " }\n" 17276 "#else\n" 17277 " {\n" 17278 " }\n" 17279 "#endif", 17280 WhitesmithsBraceStyle); 17281 17282 verifyFormat("void foobar()\n" 17283 " {\n" 17284 " int i = 5;\n" 17285 " }\n" 17286 "#ifdef _DEBUG\n" 17287 "void bar()\n" 17288 " {\n" 17289 " }\n" 17290 "#else\n" 17291 "void bar()\n" 17292 " {\n" 17293 " foobar();\n" 17294 " }\n" 17295 "#endif", 17296 WhitesmithsBraceStyle); 17297 17298 // This shouldn't affect ObjC blocks.. 17299 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 17300 " // ...\n" 17301 " int i;\n" 17302 "}];", 17303 WhitesmithsBraceStyle); 17304 verifyFormat("void (^block)(void) = ^{\n" 17305 " // ...\n" 17306 " int i;\n" 17307 "};", 17308 WhitesmithsBraceStyle); 17309 // .. or dict literals. 17310 verifyFormat("void f()\n" 17311 " {\n" 17312 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 17313 " }", 17314 WhitesmithsBraceStyle); 17315 17316 verifyFormat("int f()\n" 17317 " { // comment\n" 17318 " return 42;\n" 17319 " }", 17320 WhitesmithsBraceStyle); 17321 17322 FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle; 17323 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = 17324 FormatStyle::SIS_OnlyFirstIf; 17325 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 17326 verifyFormat("void f(bool b)\n" 17327 " {\n" 17328 " if (b)\n" 17329 " {\n" 17330 " return;\n" 17331 " }\n" 17332 " }\n", 17333 BreakBeforeBraceShortIfs); 17334 verifyFormat("void f(bool b)\n" 17335 " {\n" 17336 " if (b) return;\n" 17337 " }\n", 17338 BreakBeforeBraceShortIfs); 17339 verifyFormat("void f(bool b)\n" 17340 " {\n" 17341 " while (b)\n" 17342 " {\n" 17343 " return;\n" 17344 " }\n" 17345 " }\n", 17346 BreakBeforeBraceShortIfs); 17347 } 17348 17349 TEST_F(FormatTest, GNUBraceBreaking) { 17350 FormatStyle GNUBraceStyle = getLLVMStyle(); 17351 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 17352 verifyFormat("namespace a\n" 17353 "{\n" 17354 "class A\n" 17355 "{\n" 17356 " void f()\n" 17357 " {\n" 17358 " int a;\n" 17359 " {\n" 17360 " int b;\n" 17361 " }\n" 17362 " if (true)\n" 17363 " {\n" 17364 " a();\n" 17365 " b();\n" 17366 " }\n" 17367 " }\n" 17368 " void g() { return; }\n" 17369 "}\n" 17370 "} // namespace a", 17371 GNUBraceStyle); 17372 17373 verifyFormat("void f()\n" 17374 "{\n" 17375 " if (true)\n" 17376 " {\n" 17377 " a();\n" 17378 " }\n" 17379 " else if (false)\n" 17380 " {\n" 17381 " b();\n" 17382 " }\n" 17383 " else\n" 17384 " {\n" 17385 " c();\n" 17386 " }\n" 17387 "}\n", 17388 GNUBraceStyle); 17389 17390 verifyFormat("void f()\n" 17391 "{\n" 17392 " for (int i = 0; i < 10; ++i)\n" 17393 " {\n" 17394 " a();\n" 17395 " }\n" 17396 " while (false)\n" 17397 " {\n" 17398 " b();\n" 17399 " }\n" 17400 " do\n" 17401 " {\n" 17402 " c();\n" 17403 " }\n" 17404 " while (false);\n" 17405 "}\n", 17406 GNUBraceStyle); 17407 17408 verifyFormat("void f(int a)\n" 17409 "{\n" 17410 " switch (a)\n" 17411 " {\n" 17412 " case 0:\n" 17413 " break;\n" 17414 " case 1:\n" 17415 " {\n" 17416 " break;\n" 17417 " }\n" 17418 " case 2:\n" 17419 " {\n" 17420 " }\n" 17421 " break;\n" 17422 " default:\n" 17423 " break;\n" 17424 " }\n" 17425 "}\n", 17426 GNUBraceStyle); 17427 17428 verifyFormat("enum X\n" 17429 "{\n" 17430 " Y = 0,\n" 17431 "}\n", 17432 GNUBraceStyle); 17433 17434 verifyFormat("@interface BSApplicationController ()\n" 17435 "{\n" 17436 "@private\n" 17437 " id _extraIvar;\n" 17438 "}\n" 17439 "@end\n", 17440 GNUBraceStyle); 17441 17442 verifyFormat("#ifdef _DEBUG\n" 17443 "int foo(int i = 0)\n" 17444 "#else\n" 17445 "int foo(int i = 5)\n" 17446 "#endif\n" 17447 "{\n" 17448 " return i;\n" 17449 "}", 17450 GNUBraceStyle); 17451 17452 verifyFormat("void foo() {}\n" 17453 "void bar()\n" 17454 "#ifdef _DEBUG\n" 17455 "{\n" 17456 " foo();\n" 17457 "}\n" 17458 "#else\n" 17459 "{\n" 17460 "}\n" 17461 "#endif", 17462 GNUBraceStyle); 17463 17464 verifyFormat("void foobar() { int i = 5; }\n" 17465 "#ifdef _DEBUG\n" 17466 "void bar() {}\n" 17467 "#else\n" 17468 "void bar() { foobar(); }\n" 17469 "#endif", 17470 GNUBraceStyle); 17471 } 17472 17473 TEST_F(FormatTest, WebKitBraceBreaking) { 17474 FormatStyle WebKitBraceStyle = getLLVMStyle(); 17475 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 17476 WebKitBraceStyle.FixNamespaceComments = false; 17477 verifyFormat("namespace a {\n" 17478 "class A {\n" 17479 " void f()\n" 17480 " {\n" 17481 " if (true) {\n" 17482 " a();\n" 17483 " b();\n" 17484 " }\n" 17485 " }\n" 17486 " void g() { return; }\n" 17487 "};\n" 17488 "enum E {\n" 17489 " A,\n" 17490 " // foo\n" 17491 " B,\n" 17492 " C\n" 17493 "};\n" 17494 "struct B {\n" 17495 " int x;\n" 17496 "};\n" 17497 "}\n", 17498 WebKitBraceStyle); 17499 verifyFormat("struct S {\n" 17500 " int Type;\n" 17501 " union {\n" 17502 " int x;\n" 17503 " double y;\n" 17504 " } Value;\n" 17505 " class C {\n" 17506 " MyFavoriteType Value;\n" 17507 " } Class;\n" 17508 "};\n", 17509 WebKitBraceStyle); 17510 } 17511 17512 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 17513 verifyFormat("void f() {\n" 17514 " try {\n" 17515 " } catch (const Exception &e) {\n" 17516 " }\n" 17517 "}\n", 17518 getLLVMStyle()); 17519 } 17520 17521 TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) { 17522 auto Style = getLLVMStyle(); 17523 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 17524 Style.AlignConsecutiveAssignments = 17525 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 17526 Style.AlignConsecutiveDeclarations = 17527 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 17528 verifyFormat("struct test demo[] = {\n" 17529 " {56, 23, \"hello\"},\n" 17530 " {-1, 93463, \"world\"},\n" 17531 " { 7, 5, \"!!\"}\n" 17532 "};\n", 17533 Style); 17534 17535 verifyFormat("struct test demo[] = {\n" 17536 " {56, 23, \"hello\"}, // first line\n" 17537 " {-1, 93463, \"world\"}, // second line\n" 17538 " { 7, 5, \"!!\"} // third line\n" 17539 "};\n", 17540 Style); 17541 17542 verifyFormat("struct test demo[4] = {\n" 17543 " { 56, 23, 21, \"oh\"}, // first line\n" 17544 " { -1, 93463, 22, \"my\"}, // second line\n" 17545 " { 7, 5, 1, \"goodness\"} // third line\n" 17546 " {234, 5, 1, \"gracious\"} // fourth line\n" 17547 "};\n", 17548 Style); 17549 17550 verifyFormat("struct test demo[3] = {\n" 17551 " {56, 23, \"hello\"},\n" 17552 " {-1, 93463, \"world\"},\n" 17553 " { 7, 5, \"!!\"}\n" 17554 "};\n", 17555 Style); 17556 17557 verifyFormat("struct test demo[3] = {\n" 17558 " {int{56}, 23, \"hello\"},\n" 17559 " {int{-1}, 93463, \"world\"},\n" 17560 " { int{7}, 5, \"!!\"}\n" 17561 "};\n", 17562 Style); 17563 17564 verifyFormat("struct test demo[] = {\n" 17565 " {56, 23, \"hello\"},\n" 17566 " {-1, 93463, \"world\"},\n" 17567 " { 7, 5, \"!!\"},\n" 17568 "};\n", 17569 Style); 17570 17571 verifyFormat("test demo[] = {\n" 17572 " {56, 23, \"hello\"},\n" 17573 " {-1, 93463, \"world\"},\n" 17574 " { 7, 5, \"!!\"},\n" 17575 "};\n", 17576 Style); 17577 17578 verifyFormat("demo = std::array<struct test, 3>{\n" 17579 " test{56, 23, \"hello\"},\n" 17580 " test{-1, 93463, \"world\"},\n" 17581 " test{ 7, 5, \"!!\"},\n" 17582 "};\n", 17583 Style); 17584 17585 verifyFormat("test demo[] = {\n" 17586 " {56, 23, \"hello\"},\n" 17587 "#if X\n" 17588 " {-1, 93463, \"world\"},\n" 17589 "#endif\n" 17590 " { 7, 5, \"!!\"}\n" 17591 "};\n", 17592 Style); 17593 17594 verifyFormat( 17595 "test demo[] = {\n" 17596 " { 7, 23,\n" 17597 " \"hello world i am a very long line that really, in any\"\n" 17598 " \"just world, ought to be split over multiple lines\"},\n" 17599 " {-1, 93463, \"world\"},\n" 17600 " {56, 5, \"!!\"}\n" 17601 "};\n", 17602 Style); 17603 17604 verifyFormat("return GradForUnaryCwise(g, {\n" 17605 " {{\"sign\"}, \"Sign\", " 17606 " {\"x\", \"dy\"}},\n" 17607 " { {\"dx\"}, \"Mul\", {\"dy\"" 17608 ", \"sign\"}},\n" 17609 "});\n", 17610 Style); 17611 17612 Style.ColumnLimit = 0; 17613 EXPECT_EQ( 17614 "test demo[] = {\n" 17615 " {56, 23, \"hello world i am a very long line that really, " 17616 "in any just world, ought to be split over multiple lines\"},\n" 17617 " {-1, 93463, " 17618 " \"world\"},\n" 17619 " { 7, 5, " 17620 " \"!!\"},\n" 17621 "};", 17622 format("test demo[] = {{56, 23, \"hello world i am a very long line " 17623 "that really, in any just world, ought to be split over multiple " 17624 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 17625 Style)); 17626 17627 Style.ColumnLimit = 80; 17628 verifyFormat("test demo[] = {\n" 17629 " {56, 23, /* a comment */ \"hello\"},\n" 17630 " {-1, 93463, \"world\"},\n" 17631 " { 7, 5, \"!!\"}\n" 17632 "};\n", 17633 Style); 17634 17635 verifyFormat("test demo[] = {\n" 17636 " {56, 23, \"hello\"},\n" 17637 " {-1, 93463, \"world\" /* comment here */},\n" 17638 " { 7, 5, \"!!\"}\n" 17639 "};\n", 17640 Style); 17641 17642 verifyFormat("test demo[] = {\n" 17643 " {56, /* a comment */ 23, \"hello\"},\n" 17644 " {-1, 93463, \"world\"},\n" 17645 " { 7, 5, \"!!\"}\n" 17646 "};\n", 17647 Style); 17648 17649 Style.ColumnLimit = 20; 17650 EXPECT_EQ( 17651 "demo = std::array<\n" 17652 " struct test, 3>{\n" 17653 " test{\n" 17654 " 56, 23,\n" 17655 " \"hello \"\n" 17656 " \"world i \"\n" 17657 " \"am a very \"\n" 17658 " \"long line \"\n" 17659 " \"that \"\n" 17660 " \"really, \"\n" 17661 " \"in any \"\n" 17662 " \"just \"\n" 17663 " \"world, \"\n" 17664 " \"ought to \"\n" 17665 " \"be split \"\n" 17666 " \"over \"\n" 17667 " \"multiple \"\n" 17668 " \"lines\"},\n" 17669 " test{-1, 93463,\n" 17670 " \"world\"},\n" 17671 " test{ 7, 5,\n" 17672 " \"!!\" },\n" 17673 "};", 17674 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world " 17675 "i am a very long line that really, in any just world, ought " 17676 "to be split over multiple lines\"},test{-1, 93463, \"world\"}," 17677 "test{7, 5, \"!!\"},};", 17678 Style)); 17679 // This caused a core dump by enabling Alignment in the LLVMStyle globally 17680 Style = getLLVMStyleWithColumns(50); 17681 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 17682 verifyFormat("static A x = {\n" 17683 " {{init1, init2, init3, init4},\n" 17684 " {init1, init2, init3, init4}}\n" 17685 "};", 17686 Style); 17687 Style.ColumnLimit = 100; 17688 EXPECT_EQ( 17689 "test demo[] = {\n" 17690 " {56, 23,\n" 17691 " \"hello world i am a very long line that really, in any just world" 17692 ", ought to be split over \"\n" 17693 " \"multiple lines\" },\n" 17694 " {-1, 93463, \"world\"},\n" 17695 " { 7, 5, \"!!\"},\n" 17696 "};", 17697 format("test demo[] = {{56, 23, \"hello world i am a very long line " 17698 "that really, in any just world, ought to be split over multiple " 17699 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 17700 Style)); 17701 17702 Style = getLLVMStyleWithColumns(50); 17703 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 17704 Style.AlignConsecutiveAssignments = 17705 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 17706 Style.AlignConsecutiveDeclarations = 17707 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 17708 verifyFormat("struct test demo[] = {\n" 17709 " {56, 23, \"hello\"},\n" 17710 " {-1, 93463, \"world\"},\n" 17711 " { 7, 5, \"!!\"}\n" 17712 "};\n" 17713 "static A x = {\n" 17714 " {{init1, init2, init3, init4},\n" 17715 " {init1, init2, init3, init4}}\n" 17716 "};", 17717 Style); 17718 Style.ColumnLimit = 100; 17719 Style.AlignConsecutiveAssignments = 17720 FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments; 17721 Style.AlignConsecutiveDeclarations = 17722 FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments; 17723 verifyFormat("struct test demo[] = {\n" 17724 " {56, 23, \"hello\"},\n" 17725 " {-1, 93463, \"world\"},\n" 17726 " { 7, 5, \"!!\"}\n" 17727 "};\n" 17728 "struct test demo[4] = {\n" 17729 " { 56, 23, 21, \"oh\"}, // first line\n" 17730 " { -1, 93463, 22, \"my\"}, // second line\n" 17731 " { 7, 5, 1, \"goodness\"} // third line\n" 17732 " {234, 5, 1, \"gracious\"} // fourth line\n" 17733 "};\n", 17734 Style); 17735 EXPECT_EQ( 17736 "test demo[] = {\n" 17737 " {56,\n" 17738 " \"hello world i am a very long line that really, in any just world" 17739 ", ought to be split over \"\n" 17740 " \"multiple lines\", 23},\n" 17741 " {-1, \"world\", 93463},\n" 17742 " { 7, \"!!\", 5},\n" 17743 "};", 17744 format("test demo[] = {{56, \"hello world i am a very long line " 17745 "that really, in any just world, ought to be split over multiple " 17746 "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};", 17747 Style)); 17748 } 17749 17750 TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) { 17751 auto Style = getLLVMStyle(); 17752 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; 17753 verifyFormat("struct test demo[] = {\n" 17754 " {56, 23, \"hello\"},\n" 17755 " {-1, 93463, \"world\"},\n" 17756 " {7, 5, \"!!\" }\n" 17757 "};\n", 17758 Style); 17759 17760 verifyFormat("struct test demo[] = {\n" 17761 " {56, 23, \"hello\"}, // first line\n" 17762 " {-1, 93463, \"world\"}, // second line\n" 17763 " {7, 5, \"!!\" } // third line\n" 17764 "};\n", 17765 Style); 17766 verifyFormat("struct test demo[4] = {\n" 17767 " {56, 23, 21, \"oh\" }, // first line\n" 17768 " {-1, 93463, 22, \"my\" }, // second line\n" 17769 " {7, 5, 1, \"goodness\"} // third line\n" 17770 " {234, 5, 1, \"gracious\"} // fourth line\n" 17771 "};\n", 17772 Style); 17773 verifyFormat("struct test demo[3] = {\n" 17774 " {56, 23, \"hello\"},\n" 17775 " {-1, 93463, \"world\"},\n" 17776 " {7, 5, \"!!\" }\n" 17777 "};\n", 17778 Style); 17779 17780 verifyFormat("struct test demo[3] = {\n" 17781 " {int{56}, 23, \"hello\"},\n" 17782 " {int{-1}, 93463, \"world\"},\n" 17783 " {int{7}, 5, \"!!\" }\n" 17784 "};\n", 17785 Style); 17786 verifyFormat("struct test demo[] = {\n" 17787 " {56, 23, \"hello\"},\n" 17788 " {-1, 93463, \"world\"},\n" 17789 " {7, 5, \"!!\" },\n" 17790 "};\n", 17791 Style); 17792 verifyFormat("test demo[] = {\n" 17793 " {56, 23, \"hello\"},\n" 17794 " {-1, 93463, \"world\"},\n" 17795 " {7, 5, \"!!\" },\n" 17796 "};\n", 17797 Style); 17798 verifyFormat("demo = std::array<struct test, 3>{\n" 17799 " test{56, 23, \"hello\"},\n" 17800 " test{-1, 93463, \"world\"},\n" 17801 " test{7, 5, \"!!\" },\n" 17802 "};\n", 17803 Style); 17804 verifyFormat("test demo[] = {\n" 17805 " {56, 23, \"hello\"},\n" 17806 "#if X\n" 17807 " {-1, 93463, \"world\"},\n" 17808 "#endif\n" 17809 " {7, 5, \"!!\" }\n" 17810 "};\n", 17811 Style); 17812 verifyFormat( 17813 "test demo[] = {\n" 17814 " {7, 23,\n" 17815 " \"hello world i am a very long line that really, in any\"\n" 17816 " \"just world, ought to be split over multiple lines\"},\n" 17817 " {-1, 93463, \"world\" },\n" 17818 " {56, 5, \"!!\" }\n" 17819 "};\n", 17820 Style); 17821 17822 verifyFormat("return GradForUnaryCwise(g, {\n" 17823 " {{\"sign\"}, \"Sign\", {\"x\", " 17824 "\"dy\"} },\n" 17825 " {{\"dx\"}, \"Mul\", " 17826 "{\"dy\", \"sign\"}},\n" 17827 "});\n", 17828 Style); 17829 17830 Style.ColumnLimit = 0; 17831 EXPECT_EQ( 17832 "test demo[] = {\n" 17833 " {56, 23, \"hello world i am a very long line that really, in any " 17834 "just world, ought to be split over multiple lines\"},\n" 17835 " {-1, 93463, \"world\" " 17836 " },\n" 17837 " {7, 5, \"!!\" " 17838 " },\n" 17839 "};", 17840 format("test demo[] = {{56, 23, \"hello world i am a very long line " 17841 "that really, in any just world, ought to be split over multiple " 17842 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 17843 Style)); 17844 17845 Style.ColumnLimit = 80; 17846 verifyFormat("test demo[] = {\n" 17847 " {56, 23, /* a comment */ \"hello\"},\n" 17848 " {-1, 93463, \"world\" },\n" 17849 " {7, 5, \"!!\" }\n" 17850 "};\n", 17851 Style); 17852 17853 verifyFormat("test demo[] = {\n" 17854 " {56, 23, \"hello\" },\n" 17855 " {-1, 93463, \"world\" /* comment here */},\n" 17856 " {7, 5, \"!!\" }\n" 17857 "};\n", 17858 Style); 17859 17860 verifyFormat("test demo[] = {\n" 17861 " {56, /* a comment */ 23, \"hello\"},\n" 17862 " {-1, 93463, \"world\"},\n" 17863 " {7, 5, \"!!\" }\n" 17864 "};\n", 17865 Style); 17866 17867 Style.ColumnLimit = 20; 17868 EXPECT_EQ( 17869 "demo = std::array<\n" 17870 " struct test, 3>{\n" 17871 " test{\n" 17872 " 56, 23,\n" 17873 " \"hello \"\n" 17874 " \"world i \"\n" 17875 " \"am a very \"\n" 17876 " \"long line \"\n" 17877 " \"that \"\n" 17878 " \"really, \"\n" 17879 " \"in any \"\n" 17880 " \"just \"\n" 17881 " \"world, \"\n" 17882 " \"ought to \"\n" 17883 " \"be split \"\n" 17884 " \"over \"\n" 17885 " \"multiple \"\n" 17886 " \"lines\"},\n" 17887 " test{-1, 93463,\n" 17888 " \"world\"},\n" 17889 " test{7, 5,\n" 17890 " \"!!\" },\n" 17891 "};", 17892 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world " 17893 "i am a very long line that really, in any just world, ought " 17894 "to be split over multiple lines\"},test{-1, 93463, \"world\"}," 17895 "test{7, 5, \"!!\"},};", 17896 Style)); 17897 17898 // This caused a core dump by enabling Alignment in the LLVMStyle globally 17899 Style = getLLVMStyleWithColumns(50); 17900 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; 17901 verifyFormat("static A x = {\n" 17902 " {{init1, init2, init3, init4},\n" 17903 " {init1, init2, init3, init4}}\n" 17904 "};", 17905 Style); 17906 Style.ColumnLimit = 100; 17907 EXPECT_EQ( 17908 "test demo[] = {\n" 17909 " {56, 23,\n" 17910 " \"hello world i am a very long line that really, in any just world" 17911 ", ought to be split over \"\n" 17912 " \"multiple lines\" },\n" 17913 " {-1, 93463, \"world\"},\n" 17914 " {7, 5, \"!!\" },\n" 17915 "};", 17916 format("test demo[] = {{56, 23, \"hello world i am a very long line " 17917 "that really, in any just world, ought to be split over multiple " 17918 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 17919 Style)); 17920 } 17921 17922 TEST_F(FormatTest, UnderstandsPragmas) { 17923 verifyFormat("#pragma omp reduction(| : var)"); 17924 verifyFormat("#pragma omp reduction(+ : var)"); 17925 17926 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 17927 "(including parentheses).", 17928 format("#pragma mark Any non-hyphenated or hyphenated string " 17929 "(including parentheses).")); 17930 } 17931 17932 TEST_F(FormatTest, UnderstandPragmaOption) { 17933 verifyFormat("#pragma option -C -A"); 17934 17935 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 17936 } 17937 17938 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) { 17939 FormatStyle Style = getLLVMStyle(); 17940 Style.ColumnLimit = 20; 17941 17942 // See PR41213 17943 EXPECT_EQ("/*\n" 17944 " *\t9012345\n" 17945 " * /8901\n" 17946 " */", 17947 format("/*\n" 17948 " *\t9012345 /8901\n" 17949 " */", 17950 Style)); 17951 EXPECT_EQ("/*\n" 17952 " *345678\n" 17953 " *\t/8901\n" 17954 " */", 17955 format("/*\n" 17956 " *345678\t/8901\n" 17957 " */", 17958 Style)); 17959 17960 verifyFormat("int a; // the\n" 17961 " // comment", 17962 Style); 17963 EXPECT_EQ("int a; /* first line\n" 17964 " * second\n" 17965 " * line third\n" 17966 " * line\n" 17967 " */", 17968 format("int a; /* first line\n" 17969 " * second\n" 17970 " * line third\n" 17971 " * line\n" 17972 " */", 17973 Style)); 17974 EXPECT_EQ("int a; // first line\n" 17975 " // second\n" 17976 " // line third\n" 17977 " // line", 17978 format("int a; // first line\n" 17979 " // second line\n" 17980 " // third line", 17981 Style)); 17982 17983 Style.PenaltyExcessCharacter = 90; 17984 verifyFormat("int a; // the comment", Style); 17985 EXPECT_EQ("int a; // the comment\n" 17986 " // aaa", 17987 format("int a; // the comment aaa", Style)); 17988 EXPECT_EQ("int a; /* first line\n" 17989 " * second line\n" 17990 " * third line\n" 17991 " */", 17992 format("int a; /* first line\n" 17993 " * second line\n" 17994 " * third line\n" 17995 " */", 17996 Style)); 17997 EXPECT_EQ("int a; // first line\n" 17998 " // second line\n" 17999 " // third line", 18000 format("int a; // first line\n" 18001 " // second line\n" 18002 " // third line", 18003 Style)); 18004 // FIXME: Investigate why this is not getting the same layout as the test 18005 // above. 18006 EXPECT_EQ("int a; /* first line\n" 18007 " * second line\n" 18008 " * third line\n" 18009 " */", 18010 format("int a; /* first line second line third line" 18011 "\n*/", 18012 Style)); 18013 18014 EXPECT_EQ("// foo bar baz bazfoo\n" 18015 "// foo bar foo bar\n", 18016 format("// foo bar baz bazfoo\n" 18017 "// foo bar foo bar\n", 18018 Style)); 18019 EXPECT_EQ("// foo bar baz bazfoo\n" 18020 "// foo bar foo bar\n", 18021 format("// foo bar baz bazfoo\n" 18022 "// foo bar foo bar\n", 18023 Style)); 18024 18025 // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the 18026 // next one. 18027 EXPECT_EQ("// foo bar baz bazfoo\n" 18028 "// bar foo bar\n", 18029 format("// foo bar baz bazfoo bar\n" 18030 "// foo bar\n", 18031 Style)); 18032 18033 EXPECT_EQ("// foo bar baz bazfoo\n" 18034 "// foo bar baz bazfoo\n" 18035 "// bar foo bar\n", 18036 format("// foo bar baz bazfoo\n" 18037 "// foo bar baz bazfoo bar\n" 18038 "// foo bar\n", 18039 Style)); 18040 18041 EXPECT_EQ("// foo bar baz bazfoo\n" 18042 "// foo bar baz bazfoo\n" 18043 "// bar foo bar\n", 18044 format("// foo bar baz bazfoo\n" 18045 "// foo bar baz bazfoo bar\n" 18046 "// foo bar\n", 18047 Style)); 18048 18049 // Make sure we do not keep protruding characters if strict mode reflow is 18050 // cheaper than keeping protruding characters. 18051 Style.ColumnLimit = 21; 18052 EXPECT_EQ( 18053 "// foo foo foo foo\n" 18054 "// foo foo foo foo\n" 18055 "// foo foo foo foo\n", 18056 format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", Style)); 18057 18058 EXPECT_EQ("int a = /* long block\n" 18059 " comment */\n" 18060 " 42;", 18061 format("int a = /* long block comment */ 42;", Style)); 18062 } 18063 18064 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 18065 for (size_t i = 1; i < Styles.size(); ++i) \ 18066 EXPECT_EQ(Styles[0], Styles[i]) \ 18067 << "Style #" << i << " of " << Styles.size() << " differs from Style #0" 18068 18069 TEST_F(FormatTest, GetsPredefinedStyleByName) { 18070 SmallVector<FormatStyle, 3> Styles; 18071 Styles.resize(3); 18072 18073 Styles[0] = getLLVMStyle(); 18074 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 18075 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 18076 EXPECT_ALL_STYLES_EQUAL(Styles); 18077 18078 Styles[0] = getGoogleStyle(); 18079 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 18080 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 18081 EXPECT_ALL_STYLES_EQUAL(Styles); 18082 18083 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 18084 EXPECT_TRUE( 18085 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 18086 EXPECT_TRUE( 18087 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 18088 EXPECT_ALL_STYLES_EQUAL(Styles); 18089 18090 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 18091 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 18092 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 18093 EXPECT_ALL_STYLES_EQUAL(Styles); 18094 18095 Styles[0] = getMozillaStyle(); 18096 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 18097 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 18098 EXPECT_ALL_STYLES_EQUAL(Styles); 18099 18100 Styles[0] = getWebKitStyle(); 18101 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 18102 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 18103 EXPECT_ALL_STYLES_EQUAL(Styles); 18104 18105 Styles[0] = getGNUStyle(); 18106 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 18107 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 18108 EXPECT_ALL_STYLES_EQUAL(Styles); 18109 18110 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 18111 } 18112 18113 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 18114 SmallVector<FormatStyle, 8> Styles; 18115 Styles.resize(2); 18116 18117 Styles[0] = getGoogleStyle(); 18118 Styles[1] = getLLVMStyle(); 18119 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 18120 EXPECT_ALL_STYLES_EQUAL(Styles); 18121 18122 Styles.resize(5); 18123 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 18124 Styles[1] = getLLVMStyle(); 18125 Styles[1].Language = FormatStyle::LK_JavaScript; 18126 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 18127 18128 Styles[2] = getLLVMStyle(); 18129 Styles[2].Language = FormatStyle::LK_JavaScript; 18130 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 18131 "BasedOnStyle: Google", 18132 &Styles[2]) 18133 .value()); 18134 18135 Styles[3] = getLLVMStyle(); 18136 Styles[3].Language = FormatStyle::LK_JavaScript; 18137 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 18138 "Language: JavaScript", 18139 &Styles[3]) 18140 .value()); 18141 18142 Styles[4] = getLLVMStyle(); 18143 Styles[4].Language = FormatStyle::LK_JavaScript; 18144 EXPECT_EQ(0, parseConfiguration("---\n" 18145 "BasedOnStyle: LLVM\n" 18146 "IndentWidth: 123\n" 18147 "---\n" 18148 "BasedOnStyle: Google\n" 18149 "Language: JavaScript", 18150 &Styles[4]) 18151 .value()); 18152 EXPECT_ALL_STYLES_EQUAL(Styles); 18153 } 18154 18155 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 18156 Style.FIELD = false; \ 18157 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 18158 EXPECT_TRUE(Style.FIELD); \ 18159 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 18160 EXPECT_FALSE(Style.FIELD); 18161 18162 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 18163 18164 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 18165 Style.STRUCT.FIELD = false; \ 18166 EXPECT_EQ(0, \ 18167 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 18168 .value()); \ 18169 EXPECT_TRUE(Style.STRUCT.FIELD); \ 18170 EXPECT_EQ(0, \ 18171 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 18172 .value()); \ 18173 EXPECT_FALSE(Style.STRUCT.FIELD); 18174 18175 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 18176 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 18177 18178 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 18179 EXPECT_NE(VALUE, Style.FIELD) << "Initial value already the same!"; \ 18180 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 18181 EXPECT_EQ(VALUE, Style.FIELD) << "Unexpected value after parsing!" 18182 18183 TEST_F(FormatTest, ParsesConfigurationBools) { 18184 FormatStyle Style = {}; 18185 Style.Language = FormatStyle::LK_Cpp; 18186 CHECK_PARSE_BOOL(AlignTrailingComments); 18187 CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine); 18188 CHECK_PARSE_BOOL(AllowAllConstructorInitializersOnNextLine); 18189 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 18190 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 18191 CHECK_PARSE_BOOL(AllowShortEnumsOnASingleLine); 18192 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 18193 CHECK_PARSE_BOOL(BinPackArguments); 18194 CHECK_PARSE_BOOL(BinPackParameters); 18195 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 18196 CHECK_PARSE_BOOL(BreakBeforeConceptDeclarations); 18197 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 18198 CHECK_PARSE_BOOL(BreakStringLiterals); 18199 CHECK_PARSE_BOOL(CompactNamespaces); 18200 CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); 18201 CHECK_PARSE_BOOL(DeriveLineEnding); 18202 CHECK_PARSE_BOOL(DerivePointerAlignment); 18203 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 18204 CHECK_PARSE_BOOL(DisableFormat); 18205 CHECK_PARSE_BOOL(IndentAccessModifiers); 18206 CHECK_PARSE_BOOL(IndentCaseLabels); 18207 CHECK_PARSE_BOOL(IndentCaseBlocks); 18208 CHECK_PARSE_BOOL(IndentGotoLabels); 18209 CHECK_PARSE_BOOL(IndentRequires); 18210 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 18211 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 18212 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 18213 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 18214 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 18215 CHECK_PARSE_BOOL(ReflowComments); 18216 CHECK_PARSE_BOOL(SortUsingDeclarations); 18217 CHECK_PARSE_BOOL(SpacesInParentheses); 18218 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 18219 CHECK_PARSE_BOOL(SpacesInConditionalStatement); 18220 CHECK_PARSE_BOOL(SpaceInEmptyBlock); 18221 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 18222 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 18223 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 18224 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 18225 CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); 18226 CHECK_PARSE_BOOL(SpaceAfterLogicalNot); 18227 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 18228 CHECK_PARSE_BOOL(SpaceBeforeCaseColon); 18229 CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList); 18230 CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon); 18231 CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon); 18232 CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon); 18233 CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets); 18234 CHECK_PARSE_BOOL(UseCRLF); 18235 18236 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel); 18237 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 18238 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 18239 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 18240 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 18241 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 18242 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 18243 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 18244 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock); 18245 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 18246 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 18247 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeLambdaBody); 18248 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeWhile); 18249 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 18250 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction); 18251 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord); 18252 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace); 18253 } 18254 18255 #undef CHECK_PARSE_BOOL 18256 18257 TEST_F(FormatTest, ParsesConfiguration) { 18258 FormatStyle Style = {}; 18259 Style.Language = FormatStyle::LK_Cpp; 18260 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 18261 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 18262 ConstructorInitializerIndentWidth, 1234u); 18263 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 18264 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 18265 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 18266 CHECK_PARSE("PenaltyBreakAssignment: 1234", PenaltyBreakAssignment, 1234u); 18267 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 18268 PenaltyBreakBeforeFirstCallParameter, 1234u); 18269 CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234", 18270 PenaltyBreakTemplateDeclaration, 1234u); 18271 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 18272 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 18273 PenaltyReturnTypeOnItsOwnLine, 1234u); 18274 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 18275 SpacesBeforeTrailingComments, 1234u); 18276 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 18277 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 18278 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$"); 18279 18280 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 18281 CHECK_PARSE("AlignConsecutiveAssignments: None", AlignConsecutiveAssignments, 18282 FormatStyle::ACS_None); 18283 CHECK_PARSE("AlignConsecutiveAssignments: Consecutive", 18284 AlignConsecutiveAssignments, FormatStyle::ACS_Consecutive); 18285 CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLines", 18286 AlignConsecutiveAssignments, FormatStyle::ACS_AcrossEmptyLines); 18287 CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLinesAndComments", 18288 AlignConsecutiveAssignments, 18289 FormatStyle::ACS_AcrossEmptyLinesAndComments); 18290 // For backwards compability, false / true should still parse 18291 CHECK_PARSE("AlignConsecutiveAssignments: false", AlignConsecutiveAssignments, 18292 FormatStyle::ACS_None); 18293 CHECK_PARSE("AlignConsecutiveAssignments: true", AlignConsecutiveAssignments, 18294 FormatStyle::ACS_Consecutive); 18295 18296 Style.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive; 18297 CHECK_PARSE("AlignConsecutiveBitFields: None", AlignConsecutiveBitFields, 18298 FormatStyle::ACS_None); 18299 CHECK_PARSE("AlignConsecutiveBitFields: Consecutive", 18300 AlignConsecutiveBitFields, FormatStyle::ACS_Consecutive); 18301 CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLines", 18302 AlignConsecutiveBitFields, FormatStyle::ACS_AcrossEmptyLines); 18303 CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLinesAndComments", 18304 AlignConsecutiveBitFields, 18305 FormatStyle::ACS_AcrossEmptyLinesAndComments); 18306 // For backwards compability, false / true should still parse 18307 CHECK_PARSE("AlignConsecutiveBitFields: false", AlignConsecutiveBitFields, 18308 FormatStyle::ACS_None); 18309 CHECK_PARSE("AlignConsecutiveBitFields: true", AlignConsecutiveBitFields, 18310 FormatStyle::ACS_Consecutive); 18311 18312 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 18313 CHECK_PARSE("AlignConsecutiveMacros: None", AlignConsecutiveMacros, 18314 FormatStyle::ACS_None); 18315 CHECK_PARSE("AlignConsecutiveMacros: Consecutive", AlignConsecutiveMacros, 18316 FormatStyle::ACS_Consecutive); 18317 CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLines", 18318 AlignConsecutiveMacros, FormatStyle::ACS_AcrossEmptyLines); 18319 CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLinesAndComments", 18320 AlignConsecutiveMacros, 18321 FormatStyle::ACS_AcrossEmptyLinesAndComments); 18322 // For backwards compability, false / true should still parse 18323 CHECK_PARSE("AlignConsecutiveMacros: false", AlignConsecutiveMacros, 18324 FormatStyle::ACS_None); 18325 CHECK_PARSE("AlignConsecutiveMacros: true", AlignConsecutiveMacros, 18326 FormatStyle::ACS_Consecutive); 18327 18328 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 18329 CHECK_PARSE("AlignConsecutiveDeclarations: None", 18330 AlignConsecutiveDeclarations, FormatStyle::ACS_None); 18331 CHECK_PARSE("AlignConsecutiveDeclarations: Consecutive", 18332 AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive); 18333 CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLines", 18334 AlignConsecutiveDeclarations, FormatStyle::ACS_AcrossEmptyLines); 18335 CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLinesAndComments", 18336 AlignConsecutiveDeclarations, 18337 FormatStyle::ACS_AcrossEmptyLinesAndComments); 18338 // For backwards compability, false / true should still parse 18339 CHECK_PARSE("AlignConsecutiveDeclarations: false", 18340 AlignConsecutiveDeclarations, FormatStyle::ACS_None); 18341 CHECK_PARSE("AlignConsecutiveDeclarations: true", 18342 AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive); 18343 18344 Style.PointerAlignment = FormatStyle::PAS_Middle; 18345 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 18346 FormatStyle::PAS_Left); 18347 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 18348 FormatStyle::PAS_Right); 18349 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 18350 FormatStyle::PAS_Middle); 18351 Style.ReferenceAlignment = FormatStyle::RAS_Middle; 18352 CHECK_PARSE("ReferenceAlignment: Pointer", ReferenceAlignment, 18353 FormatStyle::RAS_Pointer); 18354 CHECK_PARSE("ReferenceAlignment: Left", ReferenceAlignment, 18355 FormatStyle::RAS_Left); 18356 CHECK_PARSE("ReferenceAlignment: Right", ReferenceAlignment, 18357 FormatStyle::RAS_Right); 18358 CHECK_PARSE("ReferenceAlignment: Middle", ReferenceAlignment, 18359 FormatStyle::RAS_Middle); 18360 // For backward compatibility: 18361 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 18362 FormatStyle::PAS_Left); 18363 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 18364 FormatStyle::PAS_Right); 18365 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 18366 FormatStyle::PAS_Middle); 18367 18368 Style.Standard = FormatStyle::LS_Auto; 18369 CHECK_PARSE("Standard: c++03", Standard, FormatStyle::LS_Cpp03); 18370 CHECK_PARSE("Standard: c++11", Standard, FormatStyle::LS_Cpp11); 18371 CHECK_PARSE("Standard: c++14", Standard, FormatStyle::LS_Cpp14); 18372 CHECK_PARSE("Standard: c++17", Standard, FormatStyle::LS_Cpp17); 18373 CHECK_PARSE("Standard: c++20", Standard, FormatStyle::LS_Cpp20); 18374 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 18375 CHECK_PARSE("Standard: Latest", Standard, FormatStyle::LS_Latest); 18376 // Legacy aliases: 18377 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 18378 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Latest); 18379 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 18380 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 18381 18382 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 18383 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 18384 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 18385 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 18386 FormatStyle::BOS_None); 18387 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 18388 FormatStyle::BOS_All); 18389 // For backward compatibility: 18390 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 18391 FormatStyle::BOS_None); 18392 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 18393 FormatStyle::BOS_All); 18394 18395 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 18396 CHECK_PARSE("BreakConstructorInitializers: BeforeComma", 18397 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 18398 CHECK_PARSE("BreakConstructorInitializers: AfterColon", 18399 BreakConstructorInitializers, FormatStyle::BCIS_AfterColon); 18400 CHECK_PARSE("BreakConstructorInitializers: BeforeColon", 18401 BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon); 18402 // For backward compatibility: 18403 CHECK_PARSE("BreakConstructorInitializersBeforeComma: true", 18404 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 18405 18406 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 18407 CHECK_PARSE("BreakInheritanceList: AfterComma", BreakInheritanceList, 18408 FormatStyle::BILS_AfterComma); 18409 CHECK_PARSE("BreakInheritanceList: BeforeComma", BreakInheritanceList, 18410 FormatStyle::BILS_BeforeComma); 18411 CHECK_PARSE("BreakInheritanceList: AfterColon", BreakInheritanceList, 18412 FormatStyle::BILS_AfterColon); 18413 CHECK_PARSE("BreakInheritanceList: BeforeColon", BreakInheritanceList, 18414 FormatStyle::BILS_BeforeColon); 18415 // For backward compatibility: 18416 CHECK_PARSE("BreakBeforeInheritanceComma: true", BreakInheritanceList, 18417 FormatStyle::BILS_BeforeComma); 18418 18419 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 18420 CHECK_PARSE("EmptyLineBeforeAccessModifier: Never", 18421 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Never); 18422 CHECK_PARSE("EmptyLineBeforeAccessModifier: Leave", 18423 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Leave); 18424 CHECK_PARSE("EmptyLineBeforeAccessModifier: LogicalBlock", 18425 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_LogicalBlock); 18426 CHECK_PARSE("EmptyLineBeforeAccessModifier: Always", 18427 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Always); 18428 18429 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 18430 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 18431 FormatStyle::BAS_Align); 18432 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 18433 FormatStyle::BAS_DontAlign); 18434 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 18435 FormatStyle::BAS_AlwaysBreak); 18436 // For backward compatibility: 18437 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 18438 FormatStyle::BAS_DontAlign); 18439 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 18440 FormatStyle::BAS_Align); 18441 18442 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left; 18443 CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines, 18444 FormatStyle::ENAS_DontAlign); 18445 CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines, 18446 FormatStyle::ENAS_Left); 18447 CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines, 18448 FormatStyle::ENAS_Right); 18449 // For backward compatibility: 18450 CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines, 18451 FormatStyle::ENAS_Left); 18452 CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines, 18453 FormatStyle::ENAS_Right); 18454 18455 Style.AlignOperands = FormatStyle::OAS_Align; 18456 CHECK_PARSE("AlignOperands: DontAlign", AlignOperands, 18457 FormatStyle::OAS_DontAlign); 18458 CHECK_PARSE("AlignOperands: Align", AlignOperands, FormatStyle::OAS_Align); 18459 CHECK_PARSE("AlignOperands: AlignAfterOperator", AlignOperands, 18460 FormatStyle::OAS_AlignAfterOperator); 18461 // For backward compatibility: 18462 CHECK_PARSE("AlignOperands: false", AlignOperands, 18463 FormatStyle::OAS_DontAlign); 18464 CHECK_PARSE("AlignOperands: true", AlignOperands, FormatStyle::OAS_Align); 18465 18466 Style.UseTab = FormatStyle::UT_ForIndentation; 18467 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 18468 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 18469 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 18470 CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab, 18471 FormatStyle::UT_ForContinuationAndIndentation); 18472 CHECK_PARSE("UseTab: AlignWithSpaces", UseTab, 18473 FormatStyle::UT_AlignWithSpaces); 18474 // For backward compatibility: 18475 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 18476 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 18477 18478 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 18479 CHECK_PARSE("AllowShortBlocksOnASingleLine: Never", 18480 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 18481 CHECK_PARSE("AllowShortBlocksOnASingleLine: Empty", 18482 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Empty); 18483 CHECK_PARSE("AllowShortBlocksOnASingleLine: Always", 18484 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always); 18485 // For backward compatibility: 18486 CHECK_PARSE("AllowShortBlocksOnASingleLine: false", 18487 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 18488 CHECK_PARSE("AllowShortBlocksOnASingleLine: true", 18489 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always); 18490 18491 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 18492 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 18493 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 18494 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 18495 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 18496 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 18497 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 18498 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 18499 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 18500 // For backward compatibility: 18501 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 18502 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 18503 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 18504 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 18505 18506 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Both; 18507 CHECK_PARSE("SpaceAroundPointerQualifiers: Default", 18508 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Default); 18509 CHECK_PARSE("SpaceAroundPointerQualifiers: Before", 18510 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Before); 18511 CHECK_PARSE("SpaceAroundPointerQualifiers: After", 18512 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_After); 18513 CHECK_PARSE("SpaceAroundPointerQualifiers: Both", 18514 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Both); 18515 18516 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 18517 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 18518 FormatStyle::SBPO_Never); 18519 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 18520 FormatStyle::SBPO_Always); 18521 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 18522 FormatStyle::SBPO_ControlStatements); 18523 CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptControlMacros", 18524 SpaceBeforeParens, 18525 FormatStyle::SBPO_ControlStatementsExceptControlMacros); 18526 CHECK_PARSE("SpaceBeforeParens: NonEmptyParentheses", SpaceBeforeParens, 18527 FormatStyle::SBPO_NonEmptyParentheses); 18528 // For backward compatibility: 18529 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 18530 FormatStyle::SBPO_Never); 18531 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 18532 FormatStyle::SBPO_ControlStatements); 18533 CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptForEachMacros", 18534 SpaceBeforeParens, 18535 FormatStyle::SBPO_ControlStatementsExceptControlMacros); 18536 18537 Style.ColumnLimit = 123; 18538 FormatStyle BaseStyle = getLLVMStyle(); 18539 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 18540 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 18541 18542 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 18543 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 18544 FormatStyle::BS_Attach); 18545 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 18546 FormatStyle::BS_Linux); 18547 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 18548 FormatStyle::BS_Mozilla); 18549 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 18550 FormatStyle::BS_Stroustrup); 18551 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 18552 FormatStyle::BS_Allman); 18553 CHECK_PARSE("BreakBeforeBraces: Whitesmiths", BreakBeforeBraces, 18554 FormatStyle::BS_Whitesmiths); 18555 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 18556 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 18557 FormatStyle::BS_WebKit); 18558 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 18559 FormatStyle::BS_Custom); 18560 18561 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never; 18562 CHECK_PARSE("BraceWrapping:\n" 18563 " AfterControlStatement: MultiLine", 18564 BraceWrapping.AfterControlStatement, 18565 FormatStyle::BWACS_MultiLine); 18566 CHECK_PARSE("BraceWrapping:\n" 18567 " AfterControlStatement: Always", 18568 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always); 18569 CHECK_PARSE("BraceWrapping:\n" 18570 " AfterControlStatement: Never", 18571 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never); 18572 // For backward compatibility: 18573 CHECK_PARSE("BraceWrapping:\n" 18574 " AfterControlStatement: true", 18575 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always); 18576 CHECK_PARSE("BraceWrapping:\n" 18577 " AfterControlStatement: false", 18578 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never); 18579 18580 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 18581 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 18582 FormatStyle::RTBS_None); 18583 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 18584 FormatStyle::RTBS_All); 18585 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 18586 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 18587 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 18588 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 18589 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 18590 AlwaysBreakAfterReturnType, 18591 FormatStyle::RTBS_TopLevelDefinitions); 18592 18593 Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 18594 CHECK_PARSE("AlwaysBreakTemplateDeclarations: No", 18595 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_No); 18596 CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine", 18597 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine); 18598 CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes", 18599 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes); 18600 CHECK_PARSE("AlwaysBreakTemplateDeclarations: false", 18601 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine); 18602 CHECK_PARSE("AlwaysBreakTemplateDeclarations: true", 18603 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes); 18604 18605 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 18606 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 18607 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 18608 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 18609 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 18610 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 18611 AlwaysBreakAfterDefinitionReturnType, 18612 FormatStyle::DRTBS_TopLevel); 18613 18614 Style.NamespaceIndentation = FormatStyle::NI_All; 18615 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 18616 FormatStyle::NI_None); 18617 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 18618 FormatStyle::NI_Inner); 18619 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 18620 FormatStyle::NI_All); 18621 18622 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_OnlyFirstIf; 18623 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Never", 18624 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never); 18625 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: WithoutElse", 18626 AllowShortIfStatementsOnASingleLine, 18627 FormatStyle::SIS_WithoutElse); 18628 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: OnlyFirstIf", 18629 AllowShortIfStatementsOnASingleLine, 18630 FormatStyle::SIS_OnlyFirstIf); 18631 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: AllIfsAndElse", 18632 AllowShortIfStatementsOnASingleLine, 18633 FormatStyle::SIS_AllIfsAndElse); 18634 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Always", 18635 AllowShortIfStatementsOnASingleLine, 18636 FormatStyle::SIS_OnlyFirstIf); 18637 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: false", 18638 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never); 18639 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: true", 18640 AllowShortIfStatementsOnASingleLine, 18641 FormatStyle::SIS_WithoutElse); 18642 18643 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; 18644 CHECK_PARSE("IndentExternBlock: AfterExternBlock", IndentExternBlock, 18645 FormatStyle::IEBS_AfterExternBlock); 18646 CHECK_PARSE("IndentExternBlock: Indent", IndentExternBlock, 18647 FormatStyle::IEBS_Indent); 18648 CHECK_PARSE("IndentExternBlock: NoIndent", IndentExternBlock, 18649 FormatStyle::IEBS_NoIndent); 18650 CHECK_PARSE("IndentExternBlock: true", IndentExternBlock, 18651 FormatStyle::IEBS_Indent); 18652 CHECK_PARSE("IndentExternBlock: false", IndentExternBlock, 18653 FormatStyle::IEBS_NoIndent); 18654 18655 Style.BitFieldColonSpacing = FormatStyle::BFCS_None; 18656 CHECK_PARSE("BitFieldColonSpacing: Both", BitFieldColonSpacing, 18657 FormatStyle::BFCS_Both); 18658 CHECK_PARSE("BitFieldColonSpacing: None", BitFieldColonSpacing, 18659 FormatStyle::BFCS_None); 18660 CHECK_PARSE("BitFieldColonSpacing: Before", BitFieldColonSpacing, 18661 FormatStyle::BFCS_Before); 18662 CHECK_PARSE("BitFieldColonSpacing: After", BitFieldColonSpacing, 18663 FormatStyle::BFCS_After); 18664 18665 Style.SortJavaStaticImport = FormatStyle::SJSIO_Before; 18666 CHECK_PARSE("SortJavaStaticImport: After", SortJavaStaticImport, 18667 FormatStyle::SJSIO_After); 18668 CHECK_PARSE("SortJavaStaticImport: Before", SortJavaStaticImport, 18669 FormatStyle::SJSIO_Before); 18670 18671 // FIXME: This is required because parsing a configuration simply overwrites 18672 // the first N elements of the list instead of resetting it. 18673 Style.ForEachMacros.clear(); 18674 std::vector<std::string> BoostForeach; 18675 BoostForeach.push_back("BOOST_FOREACH"); 18676 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 18677 std::vector<std::string> BoostAndQForeach; 18678 BoostAndQForeach.push_back("BOOST_FOREACH"); 18679 BoostAndQForeach.push_back("Q_FOREACH"); 18680 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 18681 BoostAndQForeach); 18682 18683 Style.IfMacros.clear(); 18684 std::vector<std::string> CustomIfs; 18685 CustomIfs.push_back("MYIF"); 18686 CHECK_PARSE("IfMacros: [MYIF]", IfMacros, CustomIfs); 18687 18688 Style.AttributeMacros.clear(); 18689 CHECK_PARSE("BasedOnStyle: LLVM", AttributeMacros, 18690 std::vector<std::string>{"__capability"}); 18691 CHECK_PARSE("AttributeMacros: [attr1, attr2]", AttributeMacros, 18692 std::vector<std::string>({"attr1", "attr2"})); 18693 18694 Style.StatementAttributeLikeMacros.clear(); 18695 CHECK_PARSE("StatementAttributeLikeMacros: [emit,Q_EMIT]", 18696 StatementAttributeLikeMacros, 18697 std::vector<std::string>({"emit", "Q_EMIT"})); 18698 18699 Style.StatementMacros.clear(); 18700 CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros, 18701 std::vector<std::string>{"QUNUSED"}); 18702 CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros, 18703 std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"})); 18704 18705 Style.NamespaceMacros.clear(); 18706 CHECK_PARSE("NamespaceMacros: [TESTSUITE]", NamespaceMacros, 18707 std::vector<std::string>{"TESTSUITE"}); 18708 CHECK_PARSE("NamespaceMacros: [TESTSUITE, SUITE]", NamespaceMacros, 18709 std::vector<std::string>({"TESTSUITE", "SUITE"})); 18710 18711 Style.WhitespaceSensitiveMacros.clear(); 18712 CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE]", 18713 WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"}); 18714 CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE, ASSERT]", 18715 WhitespaceSensitiveMacros, 18716 std::vector<std::string>({"STRINGIZE", "ASSERT"})); 18717 Style.WhitespaceSensitiveMacros.clear(); 18718 CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE']", 18719 WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"}); 18720 CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE', 'ASSERT']", 18721 WhitespaceSensitiveMacros, 18722 std::vector<std::string>({"STRINGIZE", "ASSERT"})); 18723 18724 Style.IncludeStyle.IncludeCategories.clear(); 18725 std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = { 18726 {"abc/.*", 2, 0, false}, {".*", 1, 0, true}}; 18727 CHECK_PARSE("IncludeCategories:\n" 18728 " - Regex: abc/.*\n" 18729 " Priority: 2\n" 18730 " - Regex: .*\n" 18731 " Priority: 1\n" 18732 " CaseSensitive: true\n", 18733 IncludeStyle.IncludeCategories, ExpectedCategories); 18734 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex, 18735 "abc$"); 18736 CHECK_PARSE("IncludeIsMainSourceRegex: 'abc$'", 18737 IncludeStyle.IncludeIsMainSourceRegex, "abc$"); 18738 18739 Style.SortIncludes = FormatStyle::SI_Never; 18740 CHECK_PARSE("SortIncludes: true", SortIncludes, 18741 FormatStyle::SI_CaseSensitive); 18742 CHECK_PARSE("SortIncludes: false", SortIncludes, FormatStyle::SI_Never); 18743 CHECK_PARSE("SortIncludes: CaseInsensitive", SortIncludes, 18744 FormatStyle::SI_CaseInsensitive); 18745 CHECK_PARSE("SortIncludes: CaseSensitive", SortIncludes, 18746 FormatStyle::SI_CaseSensitive); 18747 CHECK_PARSE("SortIncludes: Never", SortIncludes, FormatStyle::SI_Never); 18748 18749 Style.RawStringFormats.clear(); 18750 std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = { 18751 { 18752 FormatStyle::LK_TextProto, 18753 {"pb", "proto"}, 18754 {"PARSE_TEXT_PROTO"}, 18755 /*CanonicalDelimiter=*/"", 18756 "llvm", 18757 }, 18758 { 18759 FormatStyle::LK_Cpp, 18760 {"cc", "cpp"}, 18761 {"C_CODEBLOCK", "CPPEVAL"}, 18762 /*CanonicalDelimiter=*/"cc", 18763 /*BasedOnStyle=*/"", 18764 }, 18765 }; 18766 18767 CHECK_PARSE("RawStringFormats:\n" 18768 " - Language: TextProto\n" 18769 " Delimiters:\n" 18770 " - 'pb'\n" 18771 " - 'proto'\n" 18772 " EnclosingFunctions:\n" 18773 " - 'PARSE_TEXT_PROTO'\n" 18774 " BasedOnStyle: llvm\n" 18775 " - Language: Cpp\n" 18776 " Delimiters:\n" 18777 " - 'cc'\n" 18778 " - 'cpp'\n" 18779 " EnclosingFunctions:\n" 18780 " - 'C_CODEBLOCK'\n" 18781 " - 'CPPEVAL'\n" 18782 " CanonicalDelimiter: 'cc'", 18783 RawStringFormats, ExpectedRawStringFormats); 18784 18785 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 18786 " Minimum: 0\n" 18787 " Maximum: 0", 18788 SpacesInLineCommentPrefix.Minimum, 0u); 18789 EXPECT_EQ(Style.SpacesInLineCommentPrefix.Maximum, 0u); 18790 Style.SpacesInLineCommentPrefix.Minimum = 1; 18791 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 18792 " Minimum: 2", 18793 SpacesInLineCommentPrefix.Minimum, 0u); 18794 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 18795 " Maximum: -1", 18796 SpacesInLineCommentPrefix.Maximum, -1u); 18797 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 18798 " Minimum: 2", 18799 SpacesInLineCommentPrefix.Minimum, 2u); 18800 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 18801 " Maximum: 1", 18802 SpacesInLineCommentPrefix.Maximum, 1u); 18803 EXPECT_EQ(Style.SpacesInLineCommentPrefix.Minimum, 1u); 18804 18805 Style.SpacesInAngles = FormatStyle::SIAS_Always; 18806 CHECK_PARSE("SpacesInAngles: Never", SpacesInAngles, FormatStyle::SIAS_Never); 18807 CHECK_PARSE("SpacesInAngles: Always", SpacesInAngles, 18808 FormatStyle::SIAS_Always); 18809 CHECK_PARSE("SpacesInAngles: Leave", SpacesInAngles, FormatStyle::SIAS_Leave); 18810 // For backward compatibility: 18811 CHECK_PARSE("SpacesInAngles: false", SpacesInAngles, FormatStyle::SIAS_Never); 18812 CHECK_PARSE("SpacesInAngles: true", SpacesInAngles, FormatStyle::SIAS_Always); 18813 } 18814 18815 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 18816 FormatStyle Style = {}; 18817 Style.Language = FormatStyle::LK_Cpp; 18818 CHECK_PARSE("Language: Cpp\n" 18819 "IndentWidth: 12", 18820 IndentWidth, 12u); 18821 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 18822 "IndentWidth: 34", 18823 &Style), 18824 ParseError::Unsuitable); 18825 FormatStyle BinPackedTCS = {}; 18826 BinPackedTCS.Language = FormatStyle::LK_JavaScript; 18827 EXPECT_EQ(parseConfiguration("BinPackArguments: true\n" 18828 "InsertTrailingCommas: Wrapped", 18829 &BinPackedTCS), 18830 ParseError::BinPackTrailingCommaConflict); 18831 EXPECT_EQ(12u, Style.IndentWidth); 18832 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 18833 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 18834 18835 Style.Language = FormatStyle::LK_JavaScript; 18836 CHECK_PARSE("Language: JavaScript\n" 18837 "IndentWidth: 12", 18838 IndentWidth, 12u); 18839 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 18840 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 18841 "IndentWidth: 34", 18842 &Style), 18843 ParseError::Unsuitable); 18844 EXPECT_EQ(23u, Style.IndentWidth); 18845 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 18846 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 18847 18848 CHECK_PARSE("BasedOnStyle: LLVM\n" 18849 "IndentWidth: 67", 18850 IndentWidth, 67u); 18851 18852 CHECK_PARSE("---\n" 18853 "Language: JavaScript\n" 18854 "IndentWidth: 12\n" 18855 "---\n" 18856 "Language: Cpp\n" 18857 "IndentWidth: 34\n" 18858 "...\n", 18859 IndentWidth, 12u); 18860 18861 Style.Language = FormatStyle::LK_Cpp; 18862 CHECK_PARSE("---\n" 18863 "Language: JavaScript\n" 18864 "IndentWidth: 12\n" 18865 "---\n" 18866 "Language: Cpp\n" 18867 "IndentWidth: 34\n" 18868 "...\n", 18869 IndentWidth, 34u); 18870 CHECK_PARSE("---\n" 18871 "IndentWidth: 78\n" 18872 "---\n" 18873 "Language: JavaScript\n" 18874 "IndentWidth: 56\n" 18875 "...\n", 18876 IndentWidth, 78u); 18877 18878 Style.ColumnLimit = 123; 18879 Style.IndentWidth = 234; 18880 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 18881 Style.TabWidth = 345; 18882 EXPECT_FALSE(parseConfiguration("---\n" 18883 "IndentWidth: 456\n" 18884 "BreakBeforeBraces: Allman\n" 18885 "---\n" 18886 "Language: JavaScript\n" 18887 "IndentWidth: 111\n" 18888 "TabWidth: 111\n" 18889 "---\n" 18890 "Language: Cpp\n" 18891 "BreakBeforeBraces: Stroustrup\n" 18892 "TabWidth: 789\n" 18893 "...\n", 18894 &Style)); 18895 EXPECT_EQ(123u, Style.ColumnLimit); 18896 EXPECT_EQ(456u, Style.IndentWidth); 18897 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 18898 EXPECT_EQ(789u, Style.TabWidth); 18899 18900 EXPECT_EQ(parseConfiguration("---\n" 18901 "Language: JavaScript\n" 18902 "IndentWidth: 56\n" 18903 "---\n" 18904 "IndentWidth: 78\n" 18905 "...\n", 18906 &Style), 18907 ParseError::Error); 18908 EXPECT_EQ(parseConfiguration("---\n" 18909 "Language: JavaScript\n" 18910 "IndentWidth: 56\n" 18911 "---\n" 18912 "Language: JavaScript\n" 18913 "IndentWidth: 78\n" 18914 "...\n", 18915 &Style), 18916 ParseError::Error); 18917 18918 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 18919 } 18920 18921 #undef CHECK_PARSE 18922 18923 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 18924 FormatStyle Style = {}; 18925 Style.Language = FormatStyle::LK_JavaScript; 18926 Style.BreakBeforeTernaryOperators = true; 18927 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 18928 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 18929 18930 Style.BreakBeforeTernaryOperators = true; 18931 EXPECT_EQ(0, parseConfiguration("---\n" 18932 "BasedOnStyle: Google\n" 18933 "---\n" 18934 "Language: JavaScript\n" 18935 "IndentWidth: 76\n" 18936 "...\n", 18937 &Style) 18938 .value()); 18939 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 18940 EXPECT_EQ(76u, Style.IndentWidth); 18941 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 18942 } 18943 18944 TEST_F(FormatTest, ConfigurationRoundTripTest) { 18945 FormatStyle Style = getLLVMStyle(); 18946 std::string YAML = configurationAsText(Style); 18947 FormatStyle ParsedStyle = {}; 18948 ParsedStyle.Language = FormatStyle::LK_Cpp; 18949 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 18950 EXPECT_EQ(Style, ParsedStyle); 18951 } 18952 18953 TEST_F(FormatTest, WorksFor8bitEncodings) { 18954 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 18955 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 18956 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 18957 "\"\xef\xee\xf0\xf3...\"", 18958 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 18959 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 18960 "\xef\xee\xf0\xf3...\"", 18961 getLLVMStyleWithColumns(12))); 18962 } 18963 18964 TEST_F(FormatTest, HandlesUTF8BOM) { 18965 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 18966 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 18967 format("\xef\xbb\xbf#include <iostream>")); 18968 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 18969 format("\xef\xbb\xbf\n#include <iostream>")); 18970 } 18971 18972 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 18973 #if !defined(_MSC_VER) 18974 18975 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 18976 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 18977 getLLVMStyleWithColumns(35)); 18978 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 18979 getLLVMStyleWithColumns(31)); 18980 verifyFormat("// Однажды в студёную зимнюю пору...", 18981 getLLVMStyleWithColumns(36)); 18982 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 18983 verifyFormat("/* Однажды в студёную зимнюю пору... */", 18984 getLLVMStyleWithColumns(39)); 18985 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 18986 getLLVMStyleWithColumns(35)); 18987 } 18988 18989 TEST_F(FormatTest, SplitsUTF8Strings) { 18990 // Non-printable characters' width is currently considered to be the length in 18991 // bytes in UTF8. The characters can be displayed in very different manner 18992 // (zero-width, single width with a substitution glyph, expanded to their code 18993 // (e.g. "<8d>"), so there's no single correct way to handle them. 18994 EXPECT_EQ("\"aaaaÄ\"\n" 18995 "\"\xc2\x8d\";", 18996 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 18997 EXPECT_EQ("\"aaaaaaaÄ\"\n" 18998 "\"\xc2\x8d\";", 18999 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 19000 EXPECT_EQ("\"Однажды, в \"\n" 19001 "\"студёную \"\n" 19002 "\"зимнюю \"\n" 19003 "\"пору,\"", 19004 format("\"Однажды, в студёную зимнюю пору,\"", 19005 getLLVMStyleWithColumns(13))); 19006 EXPECT_EQ( 19007 "\"一 二 三 \"\n" 19008 "\"四 五六 \"\n" 19009 "\"七 八 九 \"\n" 19010 "\"十\"", 19011 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 19012 EXPECT_EQ("\"一\t\"\n" 19013 "\"二 \t\"\n" 19014 "\"三 四 \"\n" 19015 "\"五\t\"\n" 19016 "\"六 \t\"\n" 19017 "\"七 \"\n" 19018 "\"八九十\tqq\"", 19019 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 19020 getLLVMStyleWithColumns(11))); 19021 19022 // UTF8 character in an escape sequence. 19023 EXPECT_EQ("\"aaaaaa\"\n" 19024 "\"\\\xC2\x8D\"", 19025 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 19026 } 19027 19028 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 19029 EXPECT_EQ("const char *sssss =\n" 19030 " \"一二三四五六七八\\\n" 19031 " 九 十\";", 19032 format("const char *sssss = \"一二三四五六七八\\\n" 19033 " 九 十\";", 19034 getLLVMStyleWithColumns(30))); 19035 } 19036 19037 TEST_F(FormatTest, SplitsUTF8LineComments) { 19038 EXPECT_EQ("// aaaaÄ\xc2\x8d", 19039 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 19040 EXPECT_EQ("// Я из лесу\n" 19041 "// вышел; был\n" 19042 "// сильный\n" 19043 "// мороз.", 19044 format("// Я из лесу вышел; был сильный мороз.", 19045 getLLVMStyleWithColumns(13))); 19046 EXPECT_EQ("// 一二三\n" 19047 "// 四五六七\n" 19048 "// 八 九\n" 19049 "// 十", 19050 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 19051 } 19052 19053 TEST_F(FormatTest, SplitsUTF8BlockComments) { 19054 EXPECT_EQ("/* Гляжу,\n" 19055 " * поднимается\n" 19056 " * медленно в\n" 19057 " * гору\n" 19058 " * Лошадка,\n" 19059 " * везущая\n" 19060 " * хворосту\n" 19061 " * воз. */", 19062 format("/* Гляжу, поднимается медленно в гору\n" 19063 " * Лошадка, везущая хворосту воз. */", 19064 getLLVMStyleWithColumns(13))); 19065 EXPECT_EQ( 19066 "/* 一二三\n" 19067 " * 四五六七\n" 19068 " * 八 九\n" 19069 " * 十 */", 19070 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 19071 EXPECT_EQ("/* \n" 19072 " * \n" 19073 " * - */", 19074 format("/* - */", getLLVMStyleWithColumns(12))); 19075 } 19076 19077 #endif // _MSC_VER 19078 19079 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 19080 FormatStyle Style = getLLVMStyle(); 19081 19082 Style.ConstructorInitializerIndentWidth = 4; 19083 verifyFormat( 19084 "SomeClass::Constructor()\n" 19085 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 19086 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 19087 Style); 19088 19089 Style.ConstructorInitializerIndentWidth = 2; 19090 verifyFormat( 19091 "SomeClass::Constructor()\n" 19092 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 19093 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 19094 Style); 19095 19096 Style.ConstructorInitializerIndentWidth = 0; 19097 verifyFormat( 19098 "SomeClass::Constructor()\n" 19099 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 19100 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 19101 Style); 19102 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 19103 verifyFormat( 19104 "SomeLongTemplateVariableName<\n" 19105 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 19106 Style); 19107 verifyFormat("bool smaller = 1 < " 19108 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 19109 " " 19110 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 19111 Style); 19112 19113 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 19114 verifyFormat("SomeClass::Constructor() :\n" 19115 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n" 19116 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}", 19117 Style); 19118 } 19119 19120 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 19121 FormatStyle Style = getLLVMStyle(); 19122 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 19123 Style.ConstructorInitializerIndentWidth = 4; 19124 verifyFormat("SomeClass::Constructor()\n" 19125 " : a(a)\n" 19126 " , b(b)\n" 19127 " , c(c) {}", 19128 Style); 19129 verifyFormat("SomeClass::Constructor()\n" 19130 " : a(a) {}", 19131 Style); 19132 19133 Style.ColumnLimit = 0; 19134 verifyFormat("SomeClass::Constructor()\n" 19135 " : a(a) {}", 19136 Style); 19137 verifyFormat("SomeClass::Constructor() noexcept\n" 19138 " : a(a) {}", 19139 Style); 19140 verifyFormat("SomeClass::Constructor()\n" 19141 " : a(a)\n" 19142 " , b(b)\n" 19143 " , c(c) {}", 19144 Style); 19145 verifyFormat("SomeClass::Constructor()\n" 19146 " : a(a) {\n" 19147 " foo();\n" 19148 " bar();\n" 19149 "}", 19150 Style); 19151 19152 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 19153 verifyFormat("SomeClass::Constructor()\n" 19154 " : a(a)\n" 19155 " , b(b)\n" 19156 " , c(c) {\n}", 19157 Style); 19158 verifyFormat("SomeClass::Constructor()\n" 19159 " : a(a) {\n}", 19160 Style); 19161 19162 Style.ColumnLimit = 80; 19163 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 19164 Style.ConstructorInitializerIndentWidth = 2; 19165 verifyFormat("SomeClass::Constructor()\n" 19166 " : a(a)\n" 19167 " , b(b)\n" 19168 " , c(c) {}", 19169 Style); 19170 19171 Style.ConstructorInitializerIndentWidth = 0; 19172 verifyFormat("SomeClass::Constructor()\n" 19173 ": a(a)\n" 19174 ", b(b)\n" 19175 ", c(c) {}", 19176 Style); 19177 19178 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 19179 Style.ConstructorInitializerIndentWidth = 4; 19180 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 19181 verifyFormat( 19182 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 19183 Style); 19184 verifyFormat( 19185 "SomeClass::Constructor()\n" 19186 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 19187 Style); 19188 Style.ConstructorInitializerIndentWidth = 4; 19189 Style.ColumnLimit = 60; 19190 verifyFormat("SomeClass::Constructor()\n" 19191 " : aaaaaaaa(aaaaaaaa)\n" 19192 " , aaaaaaaa(aaaaaaaa)\n" 19193 " , aaaaaaaa(aaaaaaaa) {}", 19194 Style); 19195 } 19196 19197 TEST_F(FormatTest, Destructors) { 19198 verifyFormat("void F(int &i) { i.~int(); }"); 19199 verifyFormat("void F(int &i) { i->~int(); }"); 19200 } 19201 19202 TEST_F(FormatTest, FormatsWithWebKitStyle) { 19203 FormatStyle Style = getWebKitStyle(); 19204 19205 // Don't indent in outer namespaces. 19206 verifyFormat("namespace outer {\n" 19207 "int i;\n" 19208 "namespace inner {\n" 19209 " int i;\n" 19210 "} // namespace inner\n" 19211 "} // namespace outer\n" 19212 "namespace other_outer {\n" 19213 "int i;\n" 19214 "}", 19215 Style); 19216 19217 // Don't indent case labels. 19218 verifyFormat("switch (variable) {\n" 19219 "case 1:\n" 19220 "case 2:\n" 19221 " doSomething();\n" 19222 " break;\n" 19223 "default:\n" 19224 " ++variable;\n" 19225 "}", 19226 Style); 19227 19228 // Wrap before binary operators. 19229 EXPECT_EQ("void f()\n" 19230 "{\n" 19231 " if (aaaaaaaaaaaaaaaa\n" 19232 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 19233 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 19234 " return;\n" 19235 "}", 19236 format("void f() {\n" 19237 "if (aaaaaaaaaaaaaaaa\n" 19238 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 19239 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 19240 "return;\n" 19241 "}", 19242 Style)); 19243 19244 // Allow functions on a single line. 19245 verifyFormat("void f() { return; }", Style); 19246 19247 // Allow empty blocks on a single line and insert a space in empty blocks. 19248 EXPECT_EQ("void f() { }", format("void f() {}", Style)); 19249 EXPECT_EQ("while (true) { }", format("while (true) {}", Style)); 19250 // However, don't merge non-empty short loops. 19251 EXPECT_EQ("while (true) {\n" 19252 " continue;\n" 19253 "}", 19254 format("while (true) { continue; }", Style)); 19255 19256 // Constructor initializers are formatted one per line with the "," on the 19257 // new line. 19258 verifyFormat("Constructor()\n" 19259 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 19260 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 19261 " aaaaaaaaaaaaaa)\n" 19262 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 19263 "{\n" 19264 "}", 19265 Style); 19266 verifyFormat("SomeClass::Constructor()\n" 19267 " : a(a)\n" 19268 "{\n" 19269 "}", 19270 Style); 19271 EXPECT_EQ("SomeClass::Constructor()\n" 19272 " : a(a)\n" 19273 "{\n" 19274 "}", 19275 format("SomeClass::Constructor():a(a){}", Style)); 19276 verifyFormat("SomeClass::Constructor()\n" 19277 " : a(a)\n" 19278 " , b(b)\n" 19279 " , c(c)\n" 19280 "{\n" 19281 "}", 19282 Style); 19283 verifyFormat("SomeClass::Constructor()\n" 19284 " : a(a)\n" 19285 "{\n" 19286 " foo();\n" 19287 " bar();\n" 19288 "}", 19289 Style); 19290 19291 // Access specifiers should be aligned left. 19292 verifyFormat("class C {\n" 19293 "public:\n" 19294 " int i;\n" 19295 "};", 19296 Style); 19297 19298 // Do not align comments. 19299 verifyFormat("int a; // Do not\n" 19300 "double b; // align comments.", 19301 Style); 19302 19303 // Do not align operands. 19304 EXPECT_EQ("ASSERT(aaaa\n" 19305 " || bbbb);", 19306 format("ASSERT ( aaaa\n||bbbb);", Style)); 19307 19308 // Accept input's line breaks. 19309 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 19310 " || bbbbbbbbbbbbbbb) {\n" 19311 " i++;\n" 19312 "}", 19313 format("if (aaaaaaaaaaaaaaa\n" 19314 "|| bbbbbbbbbbbbbbb) { i++; }", 19315 Style)); 19316 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 19317 " i++;\n" 19318 "}", 19319 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 19320 19321 // Don't automatically break all macro definitions (llvm.org/PR17842). 19322 verifyFormat("#define aNumber 10", Style); 19323 // However, generally keep the line breaks that the user authored. 19324 EXPECT_EQ("#define aNumber \\\n" 19325 " 10", 19326 format("#define aNumber \\\n" 19327 " 10", 19328 Style)); 19329 19330 // Keep empty and one-element array literals on a single line. 19331 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 19332 " copyItems:YES];", 19333 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 19334 "copyItems:YES];", 19335 Style)); 19336 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 19337 " copyItems:YES];", 19338 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 19339 " copyItems:YES];", 19340 Style)); 19341 // FIXME: This does not seem right, there should be more indentation before 19342 // the array literal's entries. Nested blocks have the same problem. 19343 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 19344 " @\"a\",\n" 19345 " @\"a\"\n" 19346 "]\n" 19347 " copyItems:YES];", 19348 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 19349 " @\"a\",\n" 19350 " @\"a\"\n" 19351 " ]\n" 19352 " copyItems:YES];", 19353 Style)); 19354 EXPECT_EQ( 19355 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 19356 " copyItems:YES];", 19357 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 19358 " copyItems:YES];", 19359 Style)); 19360 19361 verifyFormat("[self.a b:c c:d];", Style); 19362 EXPECT_EQ("[self.a b:c\n" 19363 " c:d];", 19364 format("[self.a b:c\n" 19365 "c:d];", 19366 Style)); 19367 } 19368 19369 TEST_F(FormatTest, FormatsLambdas) { 19370 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 19371 verifyFormat( 19372 "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();\n"); 19373 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 19374 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 19375 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 19376 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 19377 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 19378 verifyFormat("auto c = [a = [b = 42] {}] {};\n"); 19379 verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n"); 19380 verifyFormat("int x = f(*+[] {});"); 19381 verifyFormat("void f() {\n" 19382 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 19383 "}\n"); 19384 verifyFormat("void f() {\n" 19385 " other(x.begin(), //\n" 19386 " x.end(), //\n" 19387 " [&](int, int) { return 1; });\n" 19388 "}\n"); 19389 verifyFormat("void f() {\n" 19390 " other.other.other.other.other(\n" 19391 " x.begin(), x.end(),\n" 19392 " [something, rather](int, int, int, int, int, int, int) { " 19393 "return 1; });\n" 19394 "}\n"); 19395 verifyFormat( 19396 "void f() {\n" 19397 " other.other.other.other.other(\n" 19398 " x.begin(), x.end(),\n" 19399 " [something, rather](int, int, int, int, int, int, int) {\n" 19400 " //\n" 19401 " });\n" 19402 "}\n"); 19403 verifyFormat("SomeFunction([]() { // A cool function...\n" 19404 " return 43;\n" 19405 "});"); 19406 EXPECT_EQ("SomeFunction([]() {\n" 19407 "#define A a\n" 19408 " return 43;\n" 19409 "});", 19410 format("SomeFunction([](){\n" 19411 "#define A a\n" 19412 "return 43;\n" 19413 "});")); 19414 verifyFormat("void f() {\n" 19415 " SomeFunction([](decltype(x), A *a) {});\n" 19416 " SomeFunction([](typeof(x), A *a) {});\n" 19417 " SomeFunction([](_Atomic(x), A *a) {});\n" 19418 " SomeFunction([](__underlying_type(x), A *a) {});\n" 19419 "}"); 19420 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 19421 " [](const aaaaaaaaaa &a) { return a; });"); 19422 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 19423 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 19424 "});"); 19425 verifyFormat("Constructor()\n" 19426 " : Field([] { // comment\n" 19427 " int i;\n" 19428 " }) {}"); 19429 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 19430 " return some_parameter.size();\n" 19431 "};"); 19432 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 19433 " [](const string &s) { return s; };"); 19434 verifyFormat("int i = aaaaaa ? 1 //\n" 19435 " : [] {\n" 19436 " return 2; //\n" 19437 " }();"); 19438 verifyFormat("llvm::errs() << \"number of twos is \"\n" 19439 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 19440 " return x == 2; // force break\n" 19441 " });"); 19442 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 19443 " [=](int iiiiiiiiiiii) {\n" 19444 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n" 19445 " aaaaaaaaaaaaaaaaaaaaaaa;\n" 19446 " });", 19447 getLLVMStyleWithColumns(60)); 19448 19449 verifyFormat("SomeFunction({[&] {\n" 19450 " // comment\n" 19451 " },\n" 19452 " [&] {\n" 19453 " // comment\n" 19454 " }});"); 19455 verifyFormat("SomeFunction({[&] {\n" 19456 " // comment\n" 19457 "}});"); 19458 verifyFormat( 19459 "virtual aaaaaaaaaaaaaaaa(\n" 19460 " std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n" 19461 " aaaaa aaaaaaaaa);"); 19462 19463 // Lambdas with return types. 19464 verifyFormat("int c = []() -> int { return 2; }();\n"); 19465 verifyFormat("int c = []() -> int * { return 2; }();\n"); 19466 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 19467 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 19468 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 19469 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 19470 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 19471 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 19472 verifyFormat("[a, a]() -> a<1> {};"); 19473 verifyFormat("[]() -> foo<5 + 2> { return {}; };"); 19474 verifyFormat("[]() -> foo<5 - 2> { return {}; };"); 19475 verifyFormat("[]() -> foo<5 / 2> { return {}; };"); 19476 verifyFormat("[]() -> foo<5 * 2> { return {}; };"); 19477 verifyFormat("[]() -> foo<5 % 2> { return {}; };"); 19478 verifyFormat("[]() -> foo<5 << 2> { return {}; };"); 19479 verifyFormat("[]() -> foo<!5> { return {}; };"); 19480 verifyFormat("[]() -> foo<~5> { return {}; };"); 19481 verifyFormat("[]() -> foo<5 | 2> { return {}; };"); 19482 verifyFormat("[]() -> foo<5 || 2> { return {}; };"); 19483 verifyFormat("[]() -> foo<5 & 2> { return {}; };"); 19484 verifyFormat("[]() -> foo<5 && 2> { return {}; };"); 19485 verifyFormat("[]() -> foo<5 == 2> { return {}; };"); 19486 verifyFormat("[]() -> foo<5 != 2> { return {}; };"); 19487 verifyFormat("[]() -> foo<5 >= 2> { return {}; };"); 19488 verifyFormat("[]() -> foo<5 <= 2> { return {}; };"); 19489 verifyFormat("[]() -> foo<5 < 2> { return {}; };"); 19490 verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };"); 19491 verifyFormat("namespace bar {\n" 19492 "// broken:\n" 19493 "auto foo{[]() -> foo<5 + 2> { return {}; }};\n" 19494 "} // namespace bar"); 19495 verifyFormat("namespace bar {\n" 19496 "// broken:\n" 19497 "auto foo{[]() -> foo<5 - 2> { return {}; }};\n" 19498 "} // namespace bar"); 19499 verifyFormat("namespace bar {\n" 19500 "// broken:\n" 19501 "auto foo{[]() -> foo<5 / 2> { return {}; }};\n" 19502 "} // namespace bar"); 19503 verifyFormat("namespace bar {\n" 19504 "// broken:\n" 19505 "auto foo{[]() -> foo<5 * 2> { return {}; }};\n" 19506 "} // namespace bar"); 19507 verifyFormat("namespace bar {\n" 19508 "// broken:\n" 19509 "auto foo{[]() -> foo<5 % 2> { return {}; }};\n" 19510 "} // namespace bar"); 19511 verifyFormat("namespace bar {\n" 19512 "// broken:\n" 19513 "auto foo{[]() -> foo<5 << 2> { return {}; }};\n" 19514 "} // namespace bar"); 19515 verifyFormat("namespace bar {\n" 19516 "// broken:\n" 19517 "auto foo{[]() -> foo<!5> { return {}; }};\n" 19518 "} // namespace bar"); 19519 verifyFormat("namespace bar {\n" 19520 "// broken:\n" 19521 "auto foo{[]() -> foo<~5> { return {}; }};\n" 19522 "} // namespace bar"); 19523 verifyFormat("namespace bar {\n" 19524 "// broken:\n" 19525 "auto foo{[]() -> foo<5 | 2> { return {}; }};\n" 19526 "} // namespace bar"); 19527 verifyFormat("namespace bar {\n" 19528 "// broken:\n" 19529 "auto foo{[]() -> foo<5 || 2> { return {}; }};\n" 19530 "} // namespace bar"); 19531 verifyFormat("namespace bar {\n" 19532 "// broken:\n" 19533 "auto foo{[]() -> foo<5 & 2> { return {}; }};\n" 19534 "} // namespace bar"); 19535 verifyFormat("namespace bar {\n" 19536 "// broken:\n" 19537 "auto foo{[]() -> foo<5 && 2> { return {}; }};\n" 19538 "} // namespace bar"); 19539 verifyFormat("namespace bar {\n" 19540 "// broken:\n" 19541 "auto foo{[]() -> foo<5 == 2> { return {}; }};\n" 19542 "} // namespace bar"); 19543 verifyFormat("namespace bar {\n" 19544 "// broken:\n" 19545 "auto foo{[]() -> foo<5 != 2> { return {}; }};\n" 19546 "} // namespace bar"); 19547 verifyFormat("namespace bar {\n" 19548 "// broken:\n" 19549 "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n" 19550 "} // namespace bar"); 19551 verifyFormat("namespace bar {\n" 19552 "// broken:\n" 19553 "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n" 19554 "} // namespace bar"); 19555 verifyFormat("namespace bar {\n" 19556 "// broken:\n" 19557 "auto foo{[]() -> foo<5 < 2> { return {}; }};\n" 19558 "} // namespace bar"); 19559 verifyFormat("namespace bar {\n" 19560 "// broken:\n" 19561 "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n" 19562 "} // namespace bar"); 19563 verifyFormat("[]() -> a<1> {};"); 19564 verifyFormat("[]() -> a<1> { ; };"); 19565 verifyFormat("[]() -> a<1> { ; }();"); 19566 verifyFormat("[a, a]() -> a<true> {};"); 19567 verifyFormat("[]() -> a<true> {};"); 19568 verifyFormat("[]() -> a<true> { ; };"); 19569 verifyFormat("[]() -> a<true> { ; }();"); 19570 verifyFormat("[a, a]() -> a<false> {};"); 19571 verifyFormat("[]() -> a<false> {};"); 19572 verifyFormat("[]() -> a<false> { ; };"); 19573 verifyFormat("[]() -> a<false> { ; }();"); 19574 verifyFormat("auto foo{[]() -> foo<false> { ; }};"); 19575 verifyFormat("namespace bar {\n" 19576 "auto foo{[]() -> foo<false> { ; }};\n" 19577 "} // namespace bar"); 19578 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 19579 " int j) -> int {\n" 19580 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 19581 "};"); 19582 verifyFormat( 19583 "aaaaaaaaaaaaaaaaaaaaaa(\n" 19584 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 19585 " return aaaaaaaaaaaaaaaaa;\n" 19586 " });", 19587 getLLVMStyleWithColumns(70)); 19588 verifyFormat("[]() //\n" 19589 " -> int {\n" 19590 " return 1; //\n" 19591 "};"); 19592 verifyFormat("[]() -> Void<T...> {};"); 19593 verifyFormat("[a, b]() -> Tuple<T...> { return {}; };"); 19594 19595 // Lambdas with explicit template argument lists. 19596 verifyFormat( 19597 "auto L = []<template <typename> class T, class U>(T<U> &&a) {};\n"); 19598 19599 // Multiple lambdas in the same parentheses change indentation rules. These 19600 // lambdas are forced to start on new lines. 19601 verifyFormat("SomeFunction(\n" 19602 " []() {\n" 19603 " //\n" 19604 " },\n" 19605 " []() {\n" 19606 " //\n" 19607 " });"); 19608 19609 // A lambda passed as arg0 is always pushed to the next line. 19610 verifyFormat("SomeFunction(\n" 19611 " [this] {\n" 19612 " //\n" 19613 " },\n" 19614 " 1);\n"); 19615 19616 // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like 19617 // the arg0 case above. 19618 auto Style = getGoogleStyle(); 19619 Style.BinPackArguments = false; 19620 verifyFormat("SomeFunction(\n" 19621 " a,\n" 19622 " [this] {\n" 19623 " //\n" 19624 " },\n" 19625 " b);\n", 19626 Style); 19627 verifyFormat("SomeFunction(\n" 19628 " a,\n" 19629 " [this] {\n" 19630 " //\n" 19631 " },\n" 19632 " b);\n"); 19633 19634 // A lambda with a very long line forces arg0 to be pushed out irrespective of 19635 // the BinPackArguments value (as long as the code is wide enough). 19636 verifyFormat( 19637 "something->SomeFunction(\n" 19638 " a,\n" 19639 " [this] {\n" 19640 " " 19641 "D0000000000000000000000000000000000000000000000000000000000001();\n" 19642 " },\n" 19643 " b);\n"); 19644 19645 // A multi-line lambda is pulled up as long as the introducer fits on the 19646 // previous line and there are no further args. 19647 verifyFormat("function(1, [this, that] {\n" 19648 " //\n" 19649 "});\n"); 19650 verifyFormat("function([this, that] {\n" 19651 " //\n" 19652 "});\n"); 19653 // FIXME: this format is not ideal and we should consider forcing the first 19654 // arg onto its own line. 19655 verifyFormat("function(a, b, c, //\n" 19656 " d, [this, that] {\n" 19657 " //\n" 19658 " });\n"); 19659 19660 // Multiple lambdas are treated correctly even when there is a short arg0. 19661 verifyFormat("SomeFunction(\n" 19662 " 1,\n" 19663 " [this] {\n" 19664 " //\n" 19665 " },\n" 19666 " [this] {\n" 19667 " //\n" 19668 " },\n" 19669 " 1);\n"); 19670 19671 // More complex introducers. 19672 verifyFormat("return [i, args...] {};"); 19673 19674 // Not lambdas. 19675 verifyFormat("constexpr char hello[]{\"hello\"};"); 19676 verifyFormat("double &operator[](int i) { return 0; }\n" 19677 "int i;"); 19678 verifyFormat("std::unique_ptr<int[]> foo() {}"); 19679 verifyFormat("int i = a[a][a]->f();"); 19680 verifyFormat("int i = (*b)[a]->f();"); 19681 19682 // Other corner cases. 19683 verifyFormat("void f() {\n" 19684 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 19685 " );\n" 19686 "}"); 19687 19688 // Lambdas created through weird macros. 19689 verifyFormat("void f() {\n" 19690 " MACRO((const AA &a) { return 1; });\n" 19691 " MACRO((AA &a) { return 1; });\n" 19692 "}"); 19693 19694 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 19695 " doo_dah();\n" 19696 " doo_dah();\n" 19697 " })) {\n" 19698 "}"); 19699 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n" 19700 " doo_dah();\n" 19701 " doo_dah();\n" 19702 " })) {\n" 19703 "}"); 19704 verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n" 19705 " doo_dah();\n" 19706 " doo_dah();\n" 19707 " })) {\n" 19708 "}"); 19709 verifyFormat("auto lambda = []() {\n" 19710 " int a = 2\n" 19711 "#if A\n" 19712 " + 2\n" 19713 "#endif\n" 19714 " ;\n" 19715 "};"); 19716 19717 // Lambdas with complex multiline introducers. 19718 verifyFormat( 19719 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 19720 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n" 19721 " -> ::std::unordered_set<\n" 19722 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 19723 " //\n" 19724 " });"); 19725 19726 FormatStyle DoNotMerge = getLLVMStyle(); 19727 DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 19728 verifyFormat("auto c = []() {\n" 19729 " return b;\n" 19730 "};", 19731 "auto c = []() { return b; };", DoNotMerge); 19732 verifyFormat("auto c = []() {\n" 19733 "};", 19734 " auto c = []() {};", DoNotMerge); 19735 19736 FormatStyle MergeEmptyOnly = getLLVMStyle(); 19737 MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty; 19738 verifyFormat("auto c = []() {\n" 19739 " return b;\n" 19740 "};", 19741 "auto c = []() {\n" 19742 " return b;\n" 19743 " };", 19744 MergeEmptyOnly); 19745 verifyFormat("auto c = []() {};", 19746 "auto c = []() {\n" 19747 "};", 19748 MergeEmptyOnly); 19749 19750 FormatStyle MergeInline = getLLVMStyle(); 19751 MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline; 19752 verifyFormat("auto c = []() {\n" 19753 " return b;\n" 19754 "};", 19755 "auto c = []() { return b; };", MergeInline); 19756 verifyFormat("function([]() { return b; })", "function([]() { return b; })", 19757 MergeInline); 19758 verifyFormat("function([]() { return b; }, a)", 19759 "function([]() { return b; }, a)", MergeInline); 19760 verifyFormat("function(a, []() { return b; })", 19761 "function(a, []() { return b; })", MergeInline); 19762 19763 // Check option "BraceWrapping.BeforeLambdaBody" and different state of 19764 // AllowShortLambdasOnASingleLine 19765 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle(); 19766 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 19767 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 19768 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 19769 FormatStyle::ShortLambdaStyle::SLS_None; 19770 verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n" 19771 " []()\n" 19772 " {\n" 19773 " return 17;\n" 19774 " });", 19775 LLVMWithBeforeLambdaBody); 19776 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n" 19777 " []()\n" 19778 " {\n" 19779 " });", 19780 LLVMWithBeforeLambdaBody); 19781 verifyFormat("auto fct_SLS_None = []()\n" 19782 "{\n" 19783 " return 17;\n" 19784 "};", 19785 LLVMWithBeforeLambdaBody); 19786 verifyFormat("TwoNestedLambdas_SLS_None(\n" 19787 " []()\n" 19788 " {\n" 19789 " return Call(\n" 19790 " []()\n" 19791 " {\n" 19792 " return 17;\n" 19793 " });\n" 19794 " });", 19795 LLVMWithBeforeLambdaBody); 19796 verifyFormat("void Fct() {\n" 19797 " return {[]()\n" 19798 " {\n" 19799 " return 17;\n" 19800 " }};\n" 19801 "}", 19802 LLVMWithBeforeLambdaBody); 19803 19804 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 19805 FormatStyle::ShortLambdaStyle::SLS_Empty; 19806 verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n" 19807 " []()\n" 19808 " {\n" 19809 " return 17;\n" 19810 " });", 19811 LLVMWithBeforeLambdaBody); 19812 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});", 19813 LLVMWithBeforeLambdaBody); 19814 verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL" 19815 "ongFunctionName_SLS_Empty(\n" 19816 " []() {});", 19817 LLVMWithBeforeLambdaBody); 19818 verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n" 19819 " []()\n" 19820 " {\n" 19821 " return 17;\n" 19822 " });", 19823 LLVMWithBeforeLambdaBody); 19824 verifyFormat("auto fct_SLS_Empty = []()\n" 19825 "{\n" 19826 " return 17;\n" 19827 "};", 19828 LLVMWithBeforeLambdaBody); 19829 verifyFormat("TwoNestedLambdas_SLS_Empty(\n" 19830 " []()\n" 19831 " {\n" 19832 " return Call([]() {});\n" 19833 " });", 19834 LLVMWithBeforeLambdaBody); 19835 verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n" 19836 " []()\n" 19837 " {\n" 19838 " return Call([]() {});\n" 19839 " });", 19840 LLVMWithBeforeLambdaBody); 19841 verifyFormat( 19842 "FctWithLongLineInLambda_SLS_Empty(\n" 19843 " []()\n" 19844 " {\n" 19845 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 19846 " AndShouldNotBeConsiderAsInline,\n" 19847 " LambdaBodyMustBeBreak);\n" 19848 " });", 19849 LLVMWithBeforeLambdaBody); 19850 19851 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 19852 FormatStyle::ShortLambdaStyle::SLS_Inline; 19853 verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });", 19854 LLVMWithBeforeLambdaBody); 19855 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});", 19856 LLVMWithBeforeLambdaBody); 19857 verifyFormat("auto fct_SLS_Inline = []()\n" 19858 "{\n" 19859 " return 17;\n" 19860 "};", 19861 LLVMWithBeforeLambdaBody); 19862 verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return " 19863 "17; }); });", 19864 LLVMWithBeforeLambdaBody); 19865 verifyFormat( 19866 "FctWithLongLineInLambda_SLS_Inline(\n" 19867 " []()\n" 19868 " {\n" 19869 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 19870 " AndShouldNotBeConsiderAsInline,\n" 19871 " LambdaBodyMustBeBreak);\n" 19872 " });", 19873 LLVMWithBeforeLambdaBody); 19874 verifyFormat("FctWithMultipleParams_SLS_Inline(" 19875 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n" 19876 " []() { return 17; });", 19877 LLVMWithBeforeLambdaBody); 19878 verifyFormat( 19879 "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });", 19880 LLVMWithBeforeLambdaBody); 19881 19882 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 19883 FormatStyle::ShortLambdaStyle::SLS_All; 19884 verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });", 19885 LLVMWithBeforeLambdaBody); 19886 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});", 19887 LLVMWithBeforeLambdaBody); 19888 verifyFormat("auto fct_SLS_All = []() { return 17; };", 19889 LLVMWithBeforeLambdaBody); 19890 verifyFormat("FctWithOneParam_SLS_All(\n" 19891 " []()\n" 19892 " {\n" 19893 " // A cool function...\n" 19894 " return 43;\n" 19895 " });", 19896 LLVMWithBeforeLambdaBody); 19897 verifyFormat("FctWithMultipleParams_SLS_All(" 19898 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n" 19899 " []() { return 17; });", 19900 LLVMWithBeforeLambdaBody); 19901 verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });", 19902 LLVMWithBeforeLambdaBody); 19903 verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });", 19904 LLVMWithBeforeLambdaBody); 19905 verifyFormat( 19906 "FctWithLongLineInLambda_SLS_All(\n" 19907 " []()\n" 19908 " {\n" 19909 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 19910 " AndShouldNotBeConsiderAsInline,\n" 19911 " LambdaBodyMustBeBreak);\n" 19912 " });", 19913 LLVMWithBeforeLambdaBody); 19914 verifyFormat( 19915 "auto fct_SLS_All = []()\n" 19916 "{\n" 19917 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 19918 " AndShouldNotBeConsiderAsInline,\n" 19919 " LambdaBodyMustBeBreak);\n" 19920 "};", 19921 LLVMWithBeforeLambdaBody); 19922 LLVMWithBeforeLambdaBody.BinPackParameters = false; 19923 verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);", 19924 LLVMWithBeforeLambdaBody); 19925 verifyFormat( 19926 "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n" 19927 " FirstParam,\n" 19928 " SecondParam,\n" 19929 " ThirdParam,\n" 19930 " FourthParam);", 19931 LLVMWithBeforeLambdaBody); 19932 verifyFormat("FctWithLongLineInLambda_SLS_All(\n" 19933 " []() { return " 19934 "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n" 19935 " FirstParam,\n" 19936 " SecondParam,\n" 19937 " ThirdParam,\n" 19938 " FourthParam);", 19939 LLVMWithBeforeLambdaBody); 19940 verifyFormat( 19941 "FctWithLongLineInLambda_SLS_All(FirstParam,\n" 19942 " SecondParam,\n" 19943 " ThirdParam,\n" 19944 " FourthParam,\n" 19945 " []() { return SomeValueNotSoLong; });", 19946 LLVMWithBeforeLambdaBody); 19947 verifyFormat("FctWithLongLineInLambda_SLS_All(\n" 19948 " []()\n" 19949 " {\n" 19950 " return " 19951 "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB" 19952 "eConsiderAsInline;\n" 19953 " });", 19954 LLVMWithBeforeLambdaBody); 19955 verifyFormat( 19956 "FctWithLongLineInLambda_SLS_All(\n" 19957 " []()\n" 19958 " {\n" 19959 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 19960 " AndShouldNotBeConsiderAsInline,\n" 19961 " LambdaBodyMustBeBreak);\n" 19962 " });", 19963 LLVMWithBeforeLambdaBody); 19964 verifyFormat("FctWithTwoParams_SLS_All(\n" 19965 " []()\n" 19966 " {\n" 19967 " // A cool function...\n" 19968 " return 43;\n" 19969 " },\n" 19970 " 87);", 19971 LLVMWithBeforeLambdaBody); 19972 verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);", 19973 LLVMWithBeforeLambdaBody); 19974 verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });", 19975 LLVMWithBeforeLambdaBody); 19976 verifyFormat( 19977 "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });", 19978 LLVMWithBeforeLambdaBody); 19979 verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; " 19980 "}); }, x);", 19981 LLVMWithBeforeLambdaBody); 19982 verifyFormat("TwoNestedLambdas_SLS_All(\n" 19983 " []()\n" 19984 " {\n" 19985 " // A cool function...\n" 19986 " return Call([]() { return 17; });\n" 19987 " });", 19988 LLVMWithBeforeLambdaBody); 19989 verifyFormat("TwoNestedLambdas_SLS_All(\n" 19990 " []()\n" 19991 " {\n" 19992 " return Call(\n" 19993 " []()\n" 19994 " {\n" 19995 " // A cool function...\n" 19996 " return 17;\n" 19997 " });\n" 19998 " });", 19999 LLVMWithBeforeLambdaBody); 20000 20001 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20002 FormatStyle::ShortLambdaStyle::SLS_None; 20003 20004 verifyFormat("auto select = [this]() -> const Library::Object *\n" 20005 "{\n" 20006 " return MyAssignment::SelectFromList(this);\n" 20007 "};\n", 20008 LLVMWithBeforeLambdaBody); 20009 20010 verifyFormat("auto select = [this]() -> const Library::Object &\n" 20011 "{\n" 20012 " return MyAssignment::SelectFromList(this);\n" 20013 "};\n", 20014 LLVMWithBeforeLambdaBody); 20015 20016 verifyFormat("auto select = [this]() -> std::unique_ptr<Object>\n" 20017 "{\n" 20018 " return MyAssignment::SelectFromList(this);\n" 20019 "};\n", 20020 LLVMWithBeforeLambdaBody); 20021 20022 verifyFormat("namespace test {\n" 20023 "class Test {\n" 20024 "public:\n" 20025 " Test() = default;\n" 20026 "};\n" 20027 "} // namespace test", 20028 LLVMWithBeforeLambdaBody); 20029 20030 // Lambdas with different indentation styles. 20031 Style = getLLVMStyleWithColumns(100); 20032 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 20033 " return promise.then(\n" 20034 " [this, &someVariable, someObject = " 20035 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 20036 " return someObject.startAsyncAction().then(\n" 20037 " [this, &someVariable](AsyncActionResult result) " 20038 "mutable { result.processMore(); });\n" 20039 " });\n" 20040 "}\n", 20041 format("SomeResult doSomething(SomeObject promise) {\n" 20042 " return promise.then([this, &someVariable, someObject = " 20043 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 20044 " return someObject.startAsyncAction().then([this, " 20045 "&someVariable](AsyncActionResult result) mutable {\n" 20046 " result.processMore();\n" 20047 " });\n" 20048 " });\n" 20049 "}\n", 20050 Style)); 20051 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope; 20052 verifyFormat("test() {\n" 20053 " ([]() -> {\n" 20054 " int b = 32;\n" 20055 " return 3;\n" 20056 " }).foo();\n" 20057 "}", 20058 Style); 20059 verifyFormat("test() {\n" 20060 " []() -> {\n" 20061 " int b = 32;\n" 20062 " return 3;\n" 20063 " }\n" 20064 "}", 20065 Style); 20066 verifyFormat("std::sort(v.begin(), v.end(),\n" 20067 " [](const auto &someLongArgumentName, const auto " 20068 "&someOtherLongArgumentName) {\n" 20069 " return someLongArgumentName.someMemberVariable < " 20070 "someOtherLongArgumentName.someMemberVariable;\n" 20071 "});", 20072 Style); 20073 verifyFormat("test() {\n" 20074 " (\n" 20075 " []() -> {\n" 20076 " int b = 32;\n" 20077 " return 3;\n" 20078 " },\n" 20079 " foo, bar)\n" 20080 " .foo();\n" 20081 "}", 20082 Style); 20083 verifyFormat("test() {\n" 20084 " ([]() -> {\n" 20085 " int b = 32;\n" 20086 " return 3;\n" 20087 " })\n" 20088 " .foo()\n" 20089 " .bar();\n" 20090 "}", 20091 Style); 20092 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 20093 " return promise.then(\n" 20094 " [this, &someVariable, someObject = " 20095 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 20096 " return someObject.startAsyncAction().then(\n" 20097 " [this, &someVariable](AsyncActionResult result) mutable { " 20098 "result.processMore(); });\n" 20099 " });\n" 20100 "}\n", 20101 format("SomeResult doSomething(SomeObject promise) {\n" 20102 " return promise.then([this, &someVariable, someObject = " 20103 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 20104 " return someObject.startAsyncAction().then([this, " 20105 "&someVariable](AsyncActionResult result) mutable {\n" 20106 " result.processMore();\n" 20107 " });\n" 20108 " });\n" 20109 "}\n", 20110 Style)); 20111 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 20112 " return promise.then([this, &someVariable] {\n" 20113 " return someObject.startAsyncAction().then(\n" 20114 " [this, &someVariable](AsyncActionResult result) mutable { " 20115 "result.processMore(); });\n" 20116 " });\n" 20117 "}\n", 20118 format("SomeResult doSomething(SomeObject promise) {\n" 20119 " return promise.then([this, &someVariable] {\n" 20120 " return someObject.startAsyncAction().then([this, " 20121 "&someVariable](AsyncActionResult result) mutable {\n" 20122 " result.processMore();\n" 20123 " });\n" 20124 " });\n" 20125 "}\n", 20126 Style)); 20127 Style = getGoogleStyle(); 20128 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope; 20129 EXPECT_EQ("#define A \\\n" 20130 " [] { \\\n" 20131 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 20132 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 20133 " }", 20134 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 20135 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 20136 Style)); 20137 // TODO: The current formatting has a minor issue that's not worth fixing 20138 // right now whereby the closing brace is indented relative to the signature 20139 // instead of being aligned. This only happens with macros. 20140 } 20141 20142 TEST_F(FormatTest, LambdaWithLineComments) { 20143 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle(); 20144 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 20145 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 20146 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20147 FormatStyle::ShortLambdaStyle::SLS_All; 20148 20149 verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody); 20150 verifyFormat("auto k = []() // comment\n" 20151 "{ return; }", 20152 LLVMWithBeforeLambdaBody); 20153 verifyFormat("auto k = []() /* comment */ { return; }", 20154 LLVMWithBeforeLambdaBody); 20155 verifyFormat("auto k = []() /* comment */ /* comment */ { return; }", 20156 LLVMWithBeforeLambdaBody); 20157 verifyFormat("auto k = []() // X\n" 20158 "{ return; }", 20159 LLVMWithBeforeLambdaBody); 20160 verifyFormat( 20161 "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n" 20162 "{ return; }", 20163 LLVMWithBeforeLambdaBody); 20164 } 20165 20166 TEST_F(FormatTest, EmptyLinesInLambdas) { 20167 verifyFormat("auto lambda = []() {\n" 20168 " x(); //\n" 20169 "};", 20170 "auto lambda = []() {\n" 20171 "\n" 20172 " x(); //\n" 20173 "\n" 20174 "};"); 20175 } 20176 20177 TEST_F(FormatTest, FormatsBlocks) { 20178 FormatStyle ShortBlocks = getLLVMStyle(); 20179 ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 20180 verifyFormat("int (^Block)(int, int);", ShortBlocks); 20181 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 20182 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 20183 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 20184 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 20185 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 20186 20187 verifyFormat("foo(^{ bar(); });", ShortBlocks); 20188 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 20189 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 20190 20191 verifyFormat("[operation setCompletionBlock:^{\n" 20192 " [self onOperationDone];\n" 20193 "}];"); 20194 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 20195 " [self onOperationDone];\n" 20196 "}]};"); 20197 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 20198 " f();\n" 20199 "}];"); 20200 verifyFormat("int a = [operation block:^int(int *i) {\n" 20201 " return 1;\n" 20202 "}];"); 20203 verifyFormat("[myObject doSomethingWith:arg1\n" 20204 " aaa:^int(int *a) {\n" 20205 " return 1;\n" 20206 " }\n" 20207 " bbb:f(a * bbbbbbbb)];"); 20208 20209 verifyFormat("[operation setCompletionBlock:^{\n" 20210 " [self.delegate newDataAvailable];\n" 20211 "}];", 20212 getLLVMStyleWithColumns(60)); 20213 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 20214 " NSString *path = [self sessionFilePath];\n" 20215 " if (path) {\n" 20216 " // ...\n" 20217 " }\n" 20218 "});"); 20219 verifyFormat("[[SessionService sharedService]\n" 20220 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 20221 " if (window) {\n" 20222 " [self windowDidLoad:window];\n" 20223 " } else {\n" 20224 " [self errorLoadingWindow];\n" 20225 " }\n" 20226 " }];"); 20227 verifyFormat("void (^largeBlock)(void) = ^{\n" 20228 " // ...\n" 20229 "};\n", 20230 getLLVMStyleWithColumns(40)); 20231 verifyFormat("[[SessionService sharedService]\n" 20232 " loadWindowWithCompletionBlock: //\n" 20233 " ^(SessionWindow *window) {\n" 20234 " if (window) {\n" 20235 " [self windowDidLoad:window];\n" 20236 " } else {\n" 20237 " [self errorLoadingWindow];\n" 20238 " }\n" 20239 " }];", 20240 getLLVMStyleWithColumns(60)); 20241 verifyFormat("[myObject doSomethingWith:arg1\n" 20242 " firstBlock:^(Foo *a) {\n" 20243 " // ...\n" 20244 " int i;\n" 20245 " }\n" 20246 " secondBlock:^(Bar *b) {\n" 20247 " // ...\n" 20248 " int i;\n" 20249 " }\n" 20250 " thirdBlock:^Foo(Bar *b) {\n" 20251 " // ...\n" 20252 " int i;\n" 20253 " }];"); 20254 verifyFormat("[myObject doSomethingWith:arg1\n" 20255 " firstBlock:-1\n" 20256 " secondBlock:^(Bar *b) {\n" 20257 " // ...\n" 20258 " int i;\n" 20259 " }];"); 20260 20261 verifyFormat("f(^{\n" 20262 " @autoreleasepool {\n" 20263 " if (a) {\n" 20264 " g();\n" 20265 " }\n" 20266 " }\n" 20267 "});"); 20268 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 20269 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" 20270 "};"); 20271 20272 FormatStyle FourIndent = getLLVMStyle(); 20273 FourIndent.ObjCBlockIndentWidth = 4; 20274 verifyFormat("[operation setCompletionBlock:^{\n" 20275 " [self onOperationDone];\n" 20276 "}];", 20277 FourIndent); 20278 } 20279 20280 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 20281 FormatStyle ZeroColumn = getLLVMStyle(); 20282 ZeroColumn.ColumnLimit = 0; 20283 20284 verifyFormat("[[SessionService sharedService] " 20285 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 20286 " if (window) {\n" 20287 " [self windowDidLoad:window];\n" 20288 " } else {\n" 20289 " [self errorLoadingWindow];\n" 20290 " }\n" 20291 "}];", 20292 ZeroColumn); 20293 EXPECT_EQ("[[SessionService sharedService]\n" 20294 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 20295 " if (window) {\n" 20296 " [self windowDidLoad:window];\n" 20297 " } else {\n" 20298 " [self errorLoadingWindow];\n" 20299 " }\n" 20300 " }];", 20301 format("[[SessionService sharedService]\n" 20302 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 20303 " if (window) {\n" 20304 " [self windowDidLoad:window];\n" 20305 " } else {\n" 20306 " [self errorLoadingWindow];\n" 20307 " }\n" 20308 "}];", 20309 ZeroColumn)); 20310 verifyFormat("[myObject doSomethingWith:arg1\n" 20311 " firstBlock:^(Foo *a) {\n" 20312 " // ...\n" 20313 " int i;\n" 20314 " }\n" 20315 " secondBlock:^(Bar *b) {\n" 20316 " // ...\n" 20317 " int i;\n" 20318 " }\n" 20319 " thirdBlock:^Foo(Bar *b) {\n" 20320 " // ...\n" 20321 " int i;\n" 20322 " }];", 20323 ZeroColumn); 20324 verifyFormat("f(^{\n" 20325 " @autoreleasepool {\n" 20326 " if (a) {\n" 20327 " g();\n" 20328 " }\n" 20329 " }\n" 20330 "});", 20331 ZeroColumn); 20332 verifyFormat("void (^largeBlock)(void) = ^{\n" 20333 " // ...\n" 20334 "};", 20335 ZeroColumn); 20336 20337 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 20338 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 20339 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 20340 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 20341 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 20342 " int i;\n" 20343 "};", 20344 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 20345 } 20346 20347 TEST_F(FormatTest, SupportsCRLF) { 20348 EXPECT_EQ("int a;\r\n" 20349 "int b;\r\n" 20350 "int c;\r\n", 20351 format("int a;\r\n" 20352 " int b;\r\n" 20353 " int c;\r\n", 20354 getLLVMStyle())); 20355 EXPECT_EQ("int a;\r\n" 20356 "int b;\r\n" 20357 "int c;\r\n", 20358 format("int a;\r\n" 20359 " int b;\n" 20360 " int c;\r\n", 20361 getLLVMStyle())); 20362 EXPECT_EQ("int a;\n" 20363 "int b;\n" 20364 "int c;\n", 20365 format("int a;\r\n" 20366 " int b;\n" 20367 " int c;\n", 20368 getLLVMStyle())); 20369 EXPECT_EQ("\"aaaaaaa \"\r\n" 20370 "\"bbbbbbb\";\r\n", 20371 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 20372 EXPECT_EQ("#define A \\\r\n" 20373 " b; \\\r\n" 20374 " c; \\\r\n" 20375 " d;\r\n", 20376 format("#define A \\\r\n" 20377 " b; \\\r\n" 20378 " c; d; \r\n", 20379 getGoogleStyle())); 20380 20381 EXPECT_EQ("/*\r\n" 20382 "multi line block comments\r\n" 20383 "should not introduce\r\n" 20384 "an extra carriage return\r\n" 20385 "*/\r\n", 20386 format("/*\r\n" 20387 "multi line block comments\r\n" 20388 "should not introduce\r\n" 20389 "an extra carriage return\r\n" 20390 "*/\r\n")); 20391 EXPECT_EQ("/*\r\n" 20392 "\r\n" 20393 "*/", 20394 format("/*\r\n" 20395 " \r\r\r\n" 20396 "*/")); 20397 20398 FormatStyle style = getLLVMStyle(); 20399 20400 style.DeriveLineEnding = true; 20401 style.UseCRLF = false; 20402 EXPECT_EQ("union FooBarBazQux {\n" 20403 " int foo;\n" 20404 " int bar;\n" 20405 " int baz;\n" 20406 "};", 20407 format("union FooBarBazQux {\r\n" 20408 " int foo;\n" 20409 " int bar;\r\n" 20410 " int baz;\n" 20411 "};", 20412 style)); 20413 style.UseCRLF = true; 20414 EXPECT_EQ("union FooBarBazQux {\r\n" 20415 " int foo;\r\n" 20416 " int bar;\r\n" 20417 " int baz;\r\n" 20418 "};", 20419 format("union FooBarBazQux {\r\n" 20420 " int foo;\n" 20421 " int bar;\r\n" 20422 " int baz;\n" 20423 "};", 20424 style)); 20425 20426 style.DeriveLineEnding = false; 20427 style.UseCRLF = false; 20428 EXPECT_EQ("union FooBarBazQux {\n" 20429 " int foo;\n" 20430 " int bar;\n" 20431 " int baz;\n" 20432 " int qux;\n" 20433 "};", 20434 format("union FooBarBazQux {\r\n" 20435 " int foo;\n" 20436 " int bar;\r\n" 20437 " int baz;\n" 20438 " int qux;\r\n" 20439 "};", 20440 style)); 20441 style.UseCRLF = true; 20442 EXPECT_EQ("union FooBarBazQux {\r\n" 20443 " int foo;\r\n" 20444 " int bar;\r\n" 20445 " int baz;\r\n" 20446 " int qux;\r\n" 20447 "};", 20448 format("union FooBarBazQux {\r\n" 20449 " int foo;\n" 20450 " int bar;\r\n" 20451 " int baz;\n" 20452 " int qux;\n" 20453 "};", 20454 style)); 20455 20456 style.DeriveLineEnding = true; 20457 style.UseCRLF = false; 20458 EXPECT_EQ("union FooBarBazQux {\r\n" 20459 " int foo;\r\n" 20460 " int bar;\r\n" 20461 " int baz;\r\n" 20462 " int qux;\r\n" 20463 "};", 20464 format("union FooBarBazQux {\r\n" 20465 " int foo;\n" 20466 " int bar;\r\n" 20467 " int baz;\n" 20468 " int qux;\r\n" 20469 "};", 20470 style)); 20471 style.UseCRLF = true; 20472 EXPECT_EQ("union FooBarBazQux {\n" 20473 " int foo;\n" 20474 " int bar;\n" 20475 " int baz;\n" 20476 " int qux;\n" 20477 "};", 20478 format("union FooBarBazQux {\r\n" 20479 " int foo;\n" 20480 " int bar;\r\n" 20481 " int baz;\n" 20482 " int qux;\n" 20483 "};", 20484 style)); 20485 } 20486 20487 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 20488 verifyFormat("MY_CLASS(C) {\n" 20489 " int i;\n" 20490 " int j;\n" 20491 "};"); 20492 } 20493 20494 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 20495 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 20496 TwoIndent.ContinuationIndentWidth = 2; 20497 20498 EXPECT_EQ("int i =\n" 20499 " longFunction(\n" 20500 " arg);", 20501 format("int i = longFunction(arg);", TwoIndent)); 20502 20503 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 20504 SixIndent.ContinuationIndentWidth = 6; 20505 20506 EXPECT_EQ("int i =\n" 20507 " longFunction(\n" 20508 " arg);", 20509 format("int i = longFunction(arg);", SixIndent)); 20510 } 20511 20512 TEST_F(FormatTest, WrappedClosingParenthesisIndent) { 20513 FormatStyle Style = getLLVMStyle(); 20514 verifyFormat("int Foo::getter(\n" 20515 " //\n" 20516 ") const {\n" 20517 " return foo;\n" 20518 "}", 20519 Style); 20520 verifyFormat("void Foo::setter(\n" 20521 " //\n" 20522 ") {\n" 20523 " foo = 1;\n" 20524 "}", 20525 Style); 20526 } 20527 20528 TEST_F(FormatTest, SpacesInAngles) { 20529 FormatStyle Spaces = getLLVMStyle(); 20530 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 20531 20532 verifyFormat("vector< ::std::string > x1;", Spaces); 20533 verifyFormat("Foo< int, Bar > x2;", Spaces); 20534 verifyFormat("Foo< ::int, ::Bar > x3;", Spaces); 20535 20536 verifyFormat("static_cast< int >(arg);", Spaces); 20537 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 20538 verifyFormat("f< int, float >();", Spaces); 20539 verifyFormat("template <> g() {}", Spaces); 20540 verifyFormat("template < std::vector< int > > f() {}", Spaces); 20541 verifyFormat("std::function< void(int, int) > fct;", Spaces); 20542 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 20543 Spaces); 20544 20545 Spaces.Standard = FormatStyle::LS_Cpp03; 20546 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 20547 verifyFormat("A< A< int > >();", Spaces); 20548 20549 Spaces.SpacesInAngles = FormatStyle::SIAS_Never; 20550 verifyFormat("A<A<int> >();", Spaces); 20551 20552 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave; 20553 verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;", 20554 Spaces); 20555 verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;", 20556 Spaces); 20557 20558 verifyFormat("A<A<int> >();", Spaces); 20559 verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces); 20560 verifyFormat("A< A< int > >();", Spaces); 20561 20562 Spaces.Standard = FormatStyle::LS_Cpp11; 20563 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 20564 verifyFormat("A< A< int > >();", Spaces); 20565 20566 Spaces.SpacesInAngles = FormatStyle::SIAS_Never; 20567 verifyFormat("vector<::std::string> x4;", Spaces); 20568 verifyFormat("vector<int> x5;", Spaces); 20569 verifyFormat("Foo<int, Bar> x6;", Spaces); 20570 verifyFormat("Foo<::int, ::Bar> x7;", Spaces); 20571 20572 verifyFormat("A<A<int>>();", Spaces); 20573 20574 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave; 20575 verifyFormat("vector<::std::string> x4;", Spaces); 20576 verifyFormat("vector< ::std::string > x4;", Spaces); 20577 verifyFormat("vector<int> x5;", Spaces); 20578 verifyFormat("vector< int > x5;", Spaces); 20579 verifyFormat("Foo<int, Bar> x6;", Spaces); 20580 verifyFormat("Foo< int, Bar > x6;", Spaces); 20581 verifyFormat("Foo<::int, ::Bar> x7;", Spaces); 20582 verifyFormat("Foo< ::int, ::Bar > x7;", Spaces); 20583 20584 verifyFormat("A<A<int>>();", Spaces); 20585 verifyFormat("A< A< int > >();", Spaces); 20586 verifyFormat("A<A<int > >();", Spaces); 20587 verifyFormat("A< A< int>>();", Spaces); 20588 } 20589 20590 TEST_F(FormatTest, SpaceAfterTemplateKeyword) { 20591 FormatStyle Style = getLLVMStyle(); 20592 Style.SpaceAfterTemplateKeyword = false; 20593 verifyFormat("template<int> void foo();", Style); 20594 } 20595 20596 TEST_F(FormatTest, TripleAngleBrackets) { 20597 verifyFormat("f<<<1, 1>>>();"); 20598 verifyFormat("f<<<1, 1, 1, s>>>();"); 20599 verifyFormat("f<<<a, b, c, d>>>();"); 20600 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 20601 verifyFormat("f<param><<<1, 1>>>();"); 20602 verifyFormat("f<1><<<1, 1>>>();"); 20603 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 20604 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 20605 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 20606 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n" 20607 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();"); 20608 } 20609 20610 TEST_F(FormatTest, MergeLessLessAtEnd) { 20611 verifyFormat("<<"); 20612 EXPECT_EQ("< < <", format("\\\n<<<")); 20613 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 20614 "aaallvm::outs() <<"); 20615 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 20616 "aaaallvm::outs()\n <<"); 20617 } 20618 20619 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 20620 std::string code = "#if A\n" 20621 "#if B\n" 20622 "a.\n" 20623 "#endif\n" 20624 " a = 1;\n" 20625 "#else\n" 20626 "#endif\n" 20627 "#if C\n" 20628 "#else\n" 20629 "#endif\n"; 20630 EXPECT_EQ(code, format(code)); 20631 } 20632 20633 TEST_F(FormatTest, HandleConflictMarkers) { 20634 // Git/SVN conflict markers. 20635 EXPECT_EQ("int a;\n" 20636 "void f() {\n" 20637 " callme(some(parameter1,\n" 20638 "<<<<<<< text by the vcs\n" 20639 " parameter2),\n" 20640 "||||||| text by the vcs\n" 20641 " parameter2),\n" 20642 " parameter3,\n" 20643 "======= text by the vcs\n" 20644 " parameter2, parameter3),\n" 20645 ">>>>>>> text by the vcs\n" 20646 " otherparameter);\n", 20647 format("int a;\n" 20648 "void f() {\n" 20649 " callme(some(parameter1,\n" 20650 "<<<<<<< text by the vcs\n" 20651 " parameter2),\n" 20652 "||||||| text by the vcs\n" 20653 " parameter2),\n" 20654 " parameter3,\n" 20655 "======= text by the vcs\n" 20656 " parameter2,\n" 20657 " parameter3),\n" 20658 ">>>>>>> text by the vcs\n" 20659 " otherparameter);\n")); 20660 20661 // Perforce markers. 20662 EXPECT_EQ("void f() {\n" 20663 " function(\n" 20664 ">>>> text by the vcs\n" 20665 " parameter,\n" 20666 "==== text by the vcs\n" 20667 " parameter,\n" 20668 "==== text by the vcs\n" 20669 " parameter,\n" 20670 "<<<< text by the vcs\n" 20671 " parameter);\n", 20672 format("void f() {\n" 20673 " function(\n" 20674 ">>>> text by the vcs\n" 20675 " parameter,\n" 20676 "==== text by the vcs\n" 20677 " parameter,\n" 20678 "==== text by the vcs\n" 20679 " parameter,\n" 20680 "<<<< text by the vcs\n" 20681 " parameter);\n")); 20682 20683 EXPECT_EQ("<<<<<<<\n" 20684 "|||||||\n" 20685 "=======\n" 20686 ">>>>>>>", 20687 format("<<<<<<<\n" 20688 "|||||||\n" 20689 "=======\n" 20690 ">>>>>>>")); 20691 20692 EXPECT_EQ("<<<<<<<\n" 20693 "|||||||\n" 20694 "int i;\n" 20695 "=======\n" 20696 ">>>>>>>", 20697 format("<<<<<<<\n" 20698 "|||||||\n" 20699 "int i;\n" 20700 "=======\n" 20701 ">>>>>>>")); 20702 20703 // FIXME: Handle parsing of macros around conflict markers correctly: 20704 EXPECT_EQ("#define Macro \\\n" 20705 "<<<<<<<\n" 20706 "Something \\\n" 20707 "|||||||\n" 20708 "Else \\\n" 20709 "=======\n" 20710 "Other \\\n" 20711 ">>>>>>>\n" 20712 " End int i;\n", 20713 format("#define Macro \\\n" 20714 "<<<<<<<\n" 20715 " Something \\\n" 20716 "|||||||\n" 20717 " Else \\\n" 20718 "=======\n" 20719 " Other \\\n" 20720 ">>>>>>>\n" 20721 " End\n" 20722 "int i;\n")); 20723 } 20724 20725 TEST_F(FormatTest, DisableRegions) { 20726 EXPECT_EQ("int i;\n" 20727 "// clang-format off\n" 20728 " int j;\n" 20729 "// clang-format on\n" 20730 "int k;", 20731 format(" int i;\n" 20732 " // clang-format off\n" 20733 " int j;\n" 20734 " // clang-format on\n" 20735 " int k;")); 20736 EXPECT_EQ("int i;\n" 20737 "/* clang-format off */\n" 20738 " int j;\n" 20739 "/* clang-format on */\n" 20740 "int k;", 20741 format(" int i;\n" 20742 " /* clang-format off */\n" 20743 " int j;\n" 20744 " /* clang-format on */\n" 20745 " int k;")); 20746 20747 // Don't reflow comments within disabled regions. 20748 EXPECT_EQ("// clang-format off\n" 20749 "// long long long long long long line\n" 20750 "/* clang-format on */\n" 20751 "/* long long long\n" 20752 " * long long long\n" 20753 " * line */\n" 20754 "int i;\n" 20755 "/* clang-format off */\n" 20756 "/* long long long long long long line */\n", 20757 format("// clang-format off\n" 20758 "// long long long long long long line\n" 20759 "/* clang-format on */\n" 20760 "/* long long long long long long line */\n" 20761 "int i;\n" 20762 "/* clang-format off */\n" 20763 "/* long long long long long long line */\n", 20764 getLLVMStyleWithColumns(20))); 20765 } 20766 20767 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 20768 format("? ) ="); 20769 verifyNoCrash("#define a\\\n /**/}"); 20770 } 20771 20772 TEST_F(FormatTest, FormatsTableGenCode) { 20773 FormatStyle Style = getLLVMStyle(); 20774 Style.Language = FormatStyle::LK_TableGen; 20775 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 20776 } 20777 20778 TEST_F(FormatTest, ArrayOfTemplates) { 20779 EXPECT_EQ("auto a = new unique_ptr<int>[10];", 20780 format("auto a = new unique_ptr<int > [ 10];")); 20781 20782 FormatStyle Spaces = getLLVMStyle(); 20783 Spaces.SpacesInSquareBrackets = true; 20784 EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];", 20785 format("auto a = new unique_ptr<int > [10];", Spaces)); 20786 } 20787 20788 TEST_F(FormatTest, ArrayAsTemplateType) { 20789 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;", 20790 format("auto a = unique_ptr < Foo < Bar>[ 10]> ;")); 20791 20792 FormatStyle Spaces = getLLVMStyle(); 20793 Spaces.SpacesInSquareBrackets = true; 20794 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;", 20795 format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces)); 20796 } 20797 20798 TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); } 20799 20800 TEST(FormatStyle, GetStyleWithEmptyFileName) { 20801 llvm::vfs::InMemoryFileSystem FS; 20802 auto Style1 = getStyle("file", "", "Google", "", &FS); 20803 ASSERT_TRUE((bool)Style1); 20804 ASSERT_EQ(*Style1, getGoogleStyle()); 20805 } 20806 20807 TEST(FormatStyle, GetStyleOfFile) { 20808 llvm::vfs::InMemoryFileSystem FS; 20809 // Test 1: format file in the same directory. 20810 ASSERT_TRUE( 20811 FS.addFile("/a/.clang-format", 0, 20812 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 20813 ASSERT_TRUE( 20814 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 20815 auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS); 20816 ASSERT_TRUE((bool)Style1); 20817 ASSERT_EQ(*Style1, getLLVMStyle()); 20818 20819 // Test 2.1: fallback to default. 20820 ASSERT_TRUE( 20821 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 20822 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS); 20823 ASSERT_TRUE((bool)Style2); 20824 ASSERT_EQ(*Style2, getMozillaStyle()); 20825 20826 // Test 2.2: no format on 'none' fallback style. 20827 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 20828 ASSERT_TRUE((bool)Style2); 20829 ASSERT_EQ(*Style2, getNoStyle()); 20830 20831 // Test 2.3: format if config is found with no based style while fallback is 20832 // 'none'. 20833 ASSERT_TRUE(FS.addFile("/b/.clang-format", 0, 20834 llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2"))); 20835 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 20836 ASSERT_TRUE((bool)Style2); 20837 ASSERT_EQ(*Style2, getLLVMStyle()); 20838 20839 // Test 2.4: format if yaml with no based style, while fallback is 'none'. 20840 Style2 = getStyle("{}", "a.h", "none", "", &FS); 20841 ASSERT_TRUE((bool)Style2); 20842 ASSERT_EQ(*Style2, getLLVMStyle()); 20843 20844 // Test 3: format file in parent directory. 20845 ASSERT_TRUE( 20846 FS.addFile("/c/.clang-format", 0, 20847 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 20848 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, 20849 llvm::MemoryBuffer::getMemBuffer("int i;"))); 20850 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS); 20851 ASSERT_TRUE((bool)Style3); 20852 ASSERT_EQ(*Style3, getGoogleStyle()); 20853 20854 // Test 4: error on invalid fallback style 20855 auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS); 20856 ASSERT_FALSE((bool)Style4); 20857 llvm::consumeError(Style4.takeError()); 20858 20859 // Test 5: error on invalid yaml on command line 20860 auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS); 20861 ASSERT_FALSE((bool)Style5); 20862 llvm::consumeError(Style5.takeError()); 20863 20864 // Test 6: error on invalid style 20865 auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS); 20866 ASSERT_FALSE((bool)Style6); 20867 llvm::consumeError(Style6.takeError()); 20868 20869 // Test 7: found config file, error on parsing it 20870 ASSERT_TRUE( 20871 FS.addFile("/d/.clang-format", 0, 20872 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n" 20873 "InvalidKey: InvalidValue"))); 20874 ASSERT_TRUE( 20875 FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 20876 auto Style7a = getStyle("file", "/d/.clang-format", "LLVM", "", &FS); 20877 ASSERT_FALSE((bool)Style7a); 20878 llvm::consumeError(Style7a.takeError()); 20879 20880 auto Style7b = getStyle("file", "/d/.clang-format", "LLVM", "", &FS, true); 20881 ASSERT_TRUE((bool)Style7b); 20882 20883 // Test 8: inferred per-language defaults apply. 20884 auto StyleTd = getStyle("file", "x.td", "llvm", "", &FS); 20885 ASSERT_TRUE((bool)StyleTd); 20886 ASSERT_EQ(*StyleTd, getLLVMStyle(FormatStyle::LK_TableGen)); 20887 20888 // Test 9.1: overwriting a file style, when parent no file exists with no 20889 // fallback style 20890 ASSERT_TRUE(FS.addFile( 20891 "/e/sub/.clang-format", 0, 20892 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: InheritParentConfig\n" 20893 "ColumnLimit: 20"))); 20894 ASSERT_TRUE(FS.addFile("/e/sub/code.cpp", 0, 20895 llvm::MemoryBuffer::getMemBuffer("int i;"))); 20896 auto Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS); 20897 ASSERT_TRUE(static_cast<bool>(Style9)); 20898 ASSERT_EQ(*Style9, [] { 20899 auto Style = getNoStyle(); 20900 Style.ColumnLimit = 20; 20901 return Style; 20902 }()); 20903 20904 // Test 9.2: with LLVM fallback style 20905 Style9 = getStyle("file", "/e/sub/code.cpp", "LLVM", "", &FS); 20906 ASSERT_TRUE(static_cast<bool>(Style9)); 20907 ASSERT_EQ(*Style9, [] { 20908 auto Style = getLLVMStyle(); 20909 Style.ColumnLimit = 20; 20910 return Style; 20911 }()); 20912 20913 // Test 9.3: with a parent file 20914 ASSERT_TRUE( 20915 FS.addFile("/e/.clang-format", 0, 20916 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google\n" 20917 "UseTab: Always"))); 20918 Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS); 20919 ASSERT_TRUE(static_cast<bool>(Style9)); 20920 ASSERT_EQ(*Style9, [] { 20921 auto Style = getGoogleStyle(); 20922 Style.ColumnLimit = 20; 20923 Style.UseTab = FormatStyle::UT_Always; 20924 return Style; 20925 }()); 20926 20927 // Test 9.4: propagate more than one level 20928 ASSERT_TRUE(FS.addFile("/e/sub/sub/code.cpp", 0, 20929 llvm::MemoryBuffer::getMemBuffer("int i;"))); 20930 ASSERT_TRUE(FS.addFile("/e/sub/sub/.clang-format", 0, 20931 llvm::MemoryBuffer::getMemBuffer( 20932 "BasedOnStyle: InheritParentConfig\n" 20933 "WhitespaceSensitiveMacros: ['FOO', 'BAR']"))); 20934 std::vector<std::string> NonDefaultWhiteSpaceMacros{"FOO", "BAR"}; 20935 20936 const auto SubSubStyle = [&NonDefaultWhiteSpaceMacros] { 20937 auto Style = getGoogleStyle(); 20938 Style.ColumnLimit = 20; 20939 Style.UseTab = FormatStyle::UT_Always; 20940 Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros; 20941 return Style; 20942 }(); 20943 20944 ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros); 20945 Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS); 20946 ASSERT_TRUE(static_cast<bool>(Style9)); 20947 ASSERT_EQ(*Style9, SubSubStyle); 20948 20949 // Test 9.5: use InheritParentConfig as style name 20950 Style9 = 20951 getStyle("inheritparentconfig", "/e/sub/sub/code.cpp", "none", "", &FS); 20952 ASSERT_TRUE(static_cast<bool>(Style9)); 20953 ASSERT_EQ(*Style9, SubSubStyle); 20954 20955 // Test 9.6: use command line style with inheritance 20956 Style9 = getStyle("{BasedOnStyle: InheritParentConfig}", "/e/sub/code.cpp", 20957 "none", "", &FS); 20958 ASSERT_TRUE(static_cast<bool>(Style9)); 20959 ASSERT_EQ(*Style9, SubSubStyle); 20960 20961 // Test 9.7: use command line style with inheritance and own config 20962 Style9 = getStyle("{BasedOnStyle: InheritParentConfig, " 20963 "WhitespaceSensitiveMacros: ['FOO', 'BAR']}", 20964 "/e/sub/code.cpp", "none", "", &FS); 20965 ASSERT_TRUE(static_cast<bool>(Style9)); 20966 ASSERT_EQ(*Style9, SubSubStyle); 20967 20968 // Test 9.8: use inheritance from a file without BasedOnStyle 20969 ASSERT_TRUE(FS.addFile("/e/withoutbase/.clang-format", 0, 20970 llvm::MemoryBuffer::getMemBuffer("ColumnLimit: 123"))); 20971 ASSERT_TRUE( 20972 FS.addFile("/e/withoutbase/sub/.clang-format", 0, 20973 llvm::MemoryBuffer::getMemBuffer( 20974 "BasedOnStyle: InheritParentConfig\nIndentWidth: 7"))); 20975 // Make sure we do not use the fallback style 20976 Style9 = getStyle("file", "/e/withoutbase/code.cpp", "google", "", &FS); 20977 ASSERT_TRUE(static_cast<bool>(Style9)); 20978 ASSERT_EQ(*Style9, [] { 20979 auto Style = getLLVMStyle(); 20980 Style.ColumnLimit = 123; 20981 return Style; 20982 }()); 20983 20984 Style9 = getStyle("file", "/e/withoutbase/sub/code.cpp", "google", "", &FS); 20985 ASSERT_TRUE(static_cast<bool>(Style9)); 20986 ASSERT_EQ(*Style9, [] { 20987 auto Style = getLLVMStyle(); 20988 Style.ColumnLimit = 123; 20989 Style.IndentWidth = 7; 20990 return Style; 20991 }()); 20992 } 20993 20994 TEST_F(ReplacementTest, FormatCodeAfterReplacements) { 20995 // Column limit is 20. 20996 std::string Code = "Type *a =\n" 20997 " new Type();\n" 20998 "g(iiiii, 0, jjjjj,\n" 20999 " 0, kkkkk, 0, mm);\n" 21000 "int bad = format ;"; 21001 std::string Expected = "auto a = new Type();\n" 21002 "g(iiiii, nullptr,\n" 21003 " jjjjj, nullptr,\n" 21004 " kkkkk, nullptr,\n" 21005 " mm);\n" 21006 "int bad = format ;"; 21007 FileID ID = Context.createInMemoryFile("format.cpp", Code); 21008 tooling::Replacements Replaces = toReplacements( 21009 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6, 21010 "auto "), 21011 tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1, 21012 "nullptr"), 21013 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1, 21014 "nullptr"), 21015 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1, 21016 "nullptr")}); 21017 21018 format::FormatStyle Style = format::getLLVMStyle(); 21019 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. 21020 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 21021 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 21022 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 21023 auto Result = applyAllReplacements(Code, *FormattedReplaces); 21024 EXPECT_TRUE(static_cast<bool>(Result)); 21025 EXPECT_EQ(Expected, *Result); 21026 } 21027 21028 TEST_F(ReplacementTest, SortIncludesAfterReplacement) { 21029 std::string Code = "#include \"a.h\"\n" 21030 "#include \"c.h\"\n" 21031 "\n" 21032 "int main() {\n" 21033 " return 0;\n" 21034 "}"; 21035 std::string Expected = "#include \"a.h\"\n" 21036 "#include \"b.h\"\n" 21037 "#include \"c.h\"\n" 21038 "\n" 21039 "int main() {\n" 21040 " return 0;\n" 21041 "}"; 21042 FileID ID = Context.createInMemoryFile("fix.cpp", Code); 21043 tooling::Replacements Replaces = toReplacements( 21044 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0, 21045 "#include \"b.h\"\n")}); 21046 21047 format::FormatStyle Style = format::getLLVMStyle(); 21048 Style.SortIncludes = FormatStyle::SI_CaseSensitive; 21049 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 21050 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 21051 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 21052 auto Result = applyAllReplacements(Code, *FormattedReplaces); 21053 EXPECT_TRUE(static_cast<bool>(Result)); 21054 EXPECT_EQ(Expected, *Result); 21055 } 21056 21057 TEST_F(FormatTest, FormatSortsUsingDeclarations) { 21058 EXPECT_EQ("using std::cin;\n" 21059 "using std::cout;", 21060 format("using std::cout;\n" 21061 "using std::cin;", 21062 getGoogleStyle())); 21063 } 21064 21065 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) { 21066 format::FormatStyle Style = format::getLLVMStyle(); 21067 Style.Standard = FormatStyle::LS_Cpp03; 21068 // cpp03 recognize this string as identifier u8 and literal character 'a' 21069 EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style)); 21070 } 21071 21072 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) { 21073 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers 21074 // all modes, including C++11, C++14 and C++17 21075 EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';")); 21076 } 21077 21078 TEST_F(FormatTest, DoNotFormatLikelyXml) { 21079 EXPECT_EQ("<!-- ;> -->", format("<!-- ;> -->", getGoogleStyle())); 21080 EXPECT_EQ(" <!-- >; -->", format(" <!-- >; -->", getGoogleStyle())); 21081 } 21082 21083 TEST_F(FormatTest, StructuredBindings) { 21084 // Structured bindings is a C++17 feature. 21085 // all modes, including C++11, C++14 and C++17 21086 verifyFormat("auto [a, b] = f();"); 21087 EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();")); 21088 EXPECT_EQ("const auto [a, b] = f();", format("const auto[a, b] = f();")); 21089 EXPECT_EQ("auto const [a, b] = f();", format("auto const[a, b] = f();")); 21090 EXPECT_EQ("auto const volatile [a, b] = f();", 21091 format("auto const volatile[a, b] = f();")); 21092 EXPECT_EQ("auto [a, b, c] = f();", format("auto [ a , b,c ] = f();")); 21093 EXPECT_EQ("auto &[a, b, c] = f();", 21094 format("auto &[ a , b,c ] = f();")); 21095 EXPECT_EQ("auto &&[a, b, c] = f();", 21096 format("auto &&[ a , b,c ] = f();")); 21097 EXPECT_EQ("auto const &[a, b] = f();", format("auto const&[a, b] = f();")); 21098 EXPECT_EQ("auto const volatile &&[a, b] = f();", 21099 format("auto const volatile &&[a, b] = f();")); 21100 EXPECT_EQ("auto const &&[a, b] = f();", 21101 format("auto const && [a, b] = f();")); 21102 EXPECT_EQ("const auto &[a, b] = f();", 21103 format("const auto & [a, b] = f();")); 21104 EXPECT_EQ("const auto volatile &&[a, b] = f();", 21105 format("const auto volatile &&[a, b] = f();")); 21106 EXPECT_EQ("volatile const auto &&[a, b] = f();", 21107 format("volatile const auto &&[a, b] = f();")); 21108 EXPECT_EQ("const auto &&[a, b] = f();", 21109 format("const auto && [a, b] = f();")); 21110 21111 // Make sure we don't mistake structured bindings for lambdas. 21112 FormatStyle PointerMiddle = getLLVMStyle(); 21113 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 21114 verifyFormat("auto [a1, b]{A * i};", getGoogleStyle()); 21115 verifyFormat("auto [a2, b]{A * i};", getLLVMStyle()); 21116 verifyFormat("auto [a3, b]{A * i};", PointerMiddle); 21117 verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle()); 21118 verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle()); 21119 verifyFormat("auto const [a3, b]{A * i};", PointerMiddle); 21120 verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle()); 21121 verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle()); 21122 verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle); 21123 verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle()); 21124 verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle()); 21125 verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle); 21126 21127 EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}", 21128 format("for (const auto && [a, b] : some_range) {\n}")); 21129 EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}", 21130 format("for (const auto & [a, b] : some_range) {\n}")); 21131 EXPECT_EQ("for (const auto [a, b] : some_range) {\n}", 21132 format("for (const auto[a, b] : some_range) {\n}")); 21133 EXPECT_EQ("auto [x, y](expr);", format("auto[x,y] (expr);")); 21134 EXPECT_EQ("auto &[x, y](expr);", format("auto & [x,y] (expr);")); 21135 EXPECT_EQ("auto &&[x, y](expr);", format("auto && [x,y] (expr);")); 21136 EXPECT_EQ("auto const &[x, y](expr);", 21137 format("auto const & [x,y] (expr);")); 21138 EXPECT_EQ("auto const &&[x, y](expr);", 21139 format("auto const && [x,y] (expr);")); 21140 EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y] {expr};")); 21141 EXPECT_EQ("auto const &[x, y]{expr};", 21142 format("auto const & [x,y] {expr};")); 21143 EXPECT_EQ("auto const &&[x, y]{expr};", 21144 format("auto const && [x,y] {expr};")); 21145 21146 format::FormatStyle Spaces = format::getLLVMStyle(); 21147 Spaces.SpacesInSquareBrackets = true; 21148 verifyFormat("auto [ a, b ] = f();", Spaces); 21149 verifyFormat("auto &&[ a, b ] = f();", Spaces); 21150 verifyFormat("auto &[ a, b ] = f();", Spaces); 21151 verifyFormat("auto const &&[ a, b ] = f();", Spaces); 21152 verifyFormat("auto const &[ a, b ] = f();", Spaces); 21153 } 21154 21155 TEST_F(FormatTest, FileAndCode) { 21156 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", "")); 21157 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", "")); 21158 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", "")); 21159 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "")); 21160 EXPECT_EQ(FormatStyle::LK_ObjC, 21161 guessLanguage("foo.h", "@interface Foo\n@end\n")); 21162 EXPECT_EQ( 21163 FormatStyle::LK_ObjC, 21164 guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }")); 21165 EXPECT_EQ(FormatStyle::LK_ObjC, 21166 guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))")); 21167 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;")); 21168 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", "")); 21169 EXPECT_EQ(FormatStyle::LK_ObjC, 21170 guessLanguage("foo", "@interface Foo\n@end\n")); 21171 EXPECT_EQ(FormatStyle::LK_ObjC, 21172 guessLanguage("foo.h", "int DoStuff(CGRect rect);\n")); 21173 EXPECT_EQ( 21174 FormatStyle::LK_ObjC, 21175 guessLanguage("foo.h", 21176 "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n")); 21177 EXPECT_EQ( 21178 FormatStyle::LK_Cpp, 21179 guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;")); 21180 } 21181 21182 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) { 21183 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];")); 21184 EXPECT_EQ(FormatStyle::LK_ObjC, 21185 guessLanguage("foo.h", "array[[calculator getIndex]];")); 21186 EXPECT_EQ(FormatStyle::LK_Cpp, 21187 guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];")); 21188 EXPECT_EQ( 21189 FormatStyle::LK_Cpp, 21190 guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];")); 21191 EXPECT_EQ(FormatStyle::LK_ObjC, 21192 guessLanguage("foo.h", "[[noreturn foo] bar];")); 21193 EXPECT_EQ(FormatStyle::LK_Cpp, 21194 guessLanguage("foo.h", "[[clang::fallthrough]];")); 21195 EXPECT_EQ(FormatStyle::LK_ObjC, 21196 guessLanguage("foo.h", "[[clang:fallthrough] foo];")); 21197 EXPECT_EQ(FormatStyle::LK_Cpp, 21198 guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];")); 21199 EXPECT_EQ(FormatStyle::LK_Cpp, 21200 guessLanguage("foo.h", "[[using clang: fallthrough]];")); 21201 EXPECT_EQ(FormatStyle::LK_ObjC, 21202 guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];")); 21203 EXPECT_EQ(FormatStyle::LK_Cpp, 21204 guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];")); 21205 EXPECT_EQ( 21206 FormatStyle::LK_Cpp, 21207 guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)")); 21208 EXPECT_EQ( 21209 FormatStyle::LK_Cpp, 21210 guessLanguage("foo.h", 21211 "[[clang::callable_when(\"unconsumed\", \"unknown\")]]")); 21212 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]")); 21213 } 21214 21215 TEST_F(FormatTest, GuessLanguageWithCaret) { 21216 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);")); 21217 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);")); 21218 EXPECT_EQ(FormatStyle::LK_ObjC, 21219 guessLanguage("foo.h", "int(^)(char, float);")); 21220 EXPECT_EQ(FormatStyle::LK_ObjC, 21221 guessLanguage("foo.h", "int(^foo)(char, float);")); 21222 EXPECT_EQ(FormatStyle::LK_ObjC, 21223 guessLanguage("foo.h", "int(^foo[10])(char, float);")); 21224 EXPECT_EQ(FormatStyle::LK_ObjC, 21225 guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);")); 21226 EXPECT_EQ( 21227 FormatStyle::LK_ObjC, 21228 guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);")); 21229 } 21230 21231 TEST_F(FormatTest, GuessLanguageWithPragmas) { 21232 EXPECT_EQ(FormatStyle::LK_Cpp, 21233 guessLanguage("foo.h", "__pragma(warning(disable:))")); 21234 EXPECT_EQ(FormatStyle::LK_Cpp, 21235 guessLanguage("foo.h", "#pragma(warning(disable:))")); 21236 EXPECT_EQ(FormatStyle::LK_Cpp, 21237 guessLanguage("foo.h", "_Pragma(warning(disable:))")); 21238 } 21239 21240 TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) { 21241 // ASM symbolic names are identifiers that must be surrounded by [] without 21242 // space in between: 21243 // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands 21244 21245 // Example from https://bugs.llvm.org/show_bug.cgi?id=45108. 21246 verifyFormat(R"(// 21247 asm volatile("mrs %x[result], FPCR" : [result] "=r"(result)); 21248 )"); 21249 21250 // A list of several ASM symbolic names. 21251 verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)"); 21252 21253 // ASM symbolic names in inline ASM with inputs and outputs. 21254 verifyFormat(R"(// 21255 asm("cmoveq %1, %2, %[result]" 21256 : [result] "=r"(result) 21257 : "r"(test), "r"(new), "[result]"(old)); 21258 )"); 21259 21260 // ASM symbolic names in inline ASM with no outputs. 21261 verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)"); 21262 } 21263 21264 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) { 21265 EXPECT_EQ(FormatStyle::LK_Cpp, 21266 guessLanguage("foo.h", "void f() {\n" 21267 " asm (\"mov %[e], %[d]\"\n" 21268 " : [d] \"=rm\" (d)\n" 21269 " [e] \"rm\" (*e));\n" 21270 "}")); 21271 EXPECT_EQ(FormatStyle::LK_Cpp, 21272 guessLanguage("foo.h", "void f() {\n" 21273 " _asm (\"mov %[e], %[d]\"\n" 21274 " : [d] \"=rm\" (d)\n" 21275 " [e] \"rm\" (*e));\n" 21276 "}")); 21277 EXPECT_EQ(FormatStyle::LK_Cpp, 21278 guessLanguage("foo.h", "void f() {\n" 21279 " __asm (\"mov %[e], %[d]\"\n" 21280 " : [d] \"=rm\" (d)\n" 21281 " [e] \"rm\" (*e));\n" 21282 "}")); 21283 EXPECT_EQ(FormatStyle::LK_Cpp, 21284 guessLanguage("foo.h", "void f() {\n" 21285 " __asm__ (\"mov %[e], %[d]\"\n" 21286 " : [d] \"=rm\" (d)\n" 21287 " [e] \"rm\" (*e));\n" 21288 "}")); 21289 EXPECT_EQ(FormatStyle::LK_Cpp, 21290 guessLanguage("foo.h", "void f() {\n" 21291 " asm (\"mov %[e], %[d]\"\n" 21292 " : [d] \"=rm\" (d),\n" 21293 " [e] \"rm\" (*e));\n" 21294 "}")); 21295 EXPECT_EQ(FormatStyle::LK_Cpp, 21296 guessLanguage("foo.h", "void f() {\n" 21297 " asm volatile (\"mov %[e], %[d]\"\n" 21298 " : [d] \"=rm\" (d)\n" 21299 " [e] \"rm\" (*e));\n" 21300 "}")); 21301 } 21302 21303 TEST_F(FormatTest, GuessLanguageWithChildLines) { 21304 EXPECT_EQ(FormatStyle::LK_Cpp, 21305 guessLanguage("foo.h", "#define FOO ({ std::string s; })")); 21306 EXPECT_EQ(FormatStyle::LK_ObjC, 21307 guessLanguage("foo.h", "#define FOO ({ NSString *s; })")); 21308 EXPECT_EQ( 21309 FormatStyle::LK_Cpp, 21310 guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })")); 21311 EXPECT_EQ( 21312 FormatStyle::LK_ObjC, 21313 guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })")); 21314 } 21315 21316 TEST_F(FormatTest, TypenameMacros) { 21317 std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"}; 21318 21319 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353 21320 FormatStyle Google = getGoogleStyleWithColumns(0); 21321 Google.TypenameMacros = TypenameMacros; 21322 verifyFormat("struct foo {\n" 21323 " int bar;\n" 21324 " TAILQ_ENTRY(a) bleh;\n" 21325 "};", 21326 Google); 21327 21328 FormatStyle Macros = getLLVMStyle(); 21329 Macros.TypenameMacros = TypenameMacros; 21330 21331 verifyFormat("STACK_OF(int) a;", Macros); 21332 verifyFormat("STACK_OF(int) *a;", Macros); 21333 verifyFormat("STACK_OF(int const *) *a;", Macros); 21334 verifyFormat("STACK_OF(int *const) *a;", Macros); 21335 verifyFormat("STACK_OF(int, string) a;", Macros); 21336 verifyFormat("STACK_OF(LIST(int)) a;", Macros); 21337 verifyFormat("STACK_OF(LIST(int)) a, b;", Macros); 21338 verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros); 21339 verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros); 21340 verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros); 21341 verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros); 21342 21343 Macros.PointerAlignment = FormatStyle::PAS_Left; 21344 verifyFormat("STACK_OF(int)* a;", Macros); 21345 verifyFormat("STACK_OF(int*)* a;", Macros); 21346 verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros); 21347 verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros); 21348 verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros); 21349 } 21350 21351 TEST_F(FormatTest, AtomicQualifier) { 21352 // Check that we treate _Atomic as a type and not a function call 21353 FormatStyle Google = getGoogleStyleWithColumns(0); 21354 verifyFormat("struct foo {\n" 21355 " int a1;\n" 21356 " _Atomic(a) a2;\n" 21357 " _Atomic(_Atomic(int) *const) a3;\n" 21358 "};", 21359 Google); 21360 verifyFormat("_Atomic(uint64_t) a;"); 21361 verifyFormat("_Atomic(uint64_t) *a;"); 21362 verifyFormat("_Atomic(uint64_t const *) *a;"); 21363 verifyFormat("_Atomic(uint64_t *const) *a;"); 21364 verifyFormat("_Atomic(const uint64_t *) *a;"); 21365 verifyFormat("_Atomic(uint64_t) a;"); 21366 verifyFormat("_Atomic(_Atomic(uint64_t)) a;"); 21367 verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;"); 21368 verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}"); 21369 verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);"); 21370 21371 verifyFormat("_Atomic(uint64_t) *s(InitValue);"); 21372 verifyFormat("_Atomic(uint64_t) *s{InitValue};"); 21373 FormatStyle Style = getLLVMStyle(); 21374 Style.PointerAlignment = FormatStyle::PAS_Left; 21375 verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style); 21376 verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style); 21377 verifyFormat("_Atomic(int)* a;", Style); 21378 verifyFormat("_Atomic(int*)* a;", Style); 21379 verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style); 21380 21381 Style.SpacesInCStyleCastParentheses = true; 21382 Style.SpacesInParentheses = false; 21383 verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style); 21384 Style.SpacesInCStyleCastParentheses = false; 21385 Style.SpacesInParentheses = true; 21386 verifyFormat("x = (_Atomic( uint64_t ))*a;", Style); 21387 verifyFormat("x = (_Atomic( uint64_t ))&a;", Style); 21388 } 21389 21390 TEST_F(FormatTest, AmbersandInLamda) { 21391 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899 21392 FormatStyle AlignStyle = getLLVMStyle(); 21393 AlignStyle.PointerAlignment = FormatStyle::PAS_Left; 21394 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle); 21395 AlignStyle.PointerAlignment = FormatStyle::PAS_Right; 21396 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle); 21397 } 21398 21399 TEST_F(FormatTest, SpacesInConditionalStatement) { 21400 FormatStyle Spaces = getLLVMStyle(); 21401 Spaces.IfMacros.clear(); 21402 Spaces.IfMacros.push_back("MYIF"); 21403 Spaces.SpacesInConditionalStatement = true; 21404 verifyFormat("for ( int i = 0; i; i++ )\n continue;", Spaces); 21405 verifyFormat("if ( !a )\n return;", Spaces); 21406 verifyFormat("if ( a )\n return;", Spaces); 21407 verifyFormat("if constexpr ( a )\n return;", Spaces); 21408 verifyFormat("MYIF ( a )\n return;", Spaces); 21409 verifyFormat("MYIF ( a )\n return;\nelse MYIF ( b )\n return;", Spaces); 21410 verifyFormat("MYIF ( a )\n return;\nelse\n return;", Spaces); 21411 verifyFormat("switch ( a )\ncase 1:\n return;", Spaces); 21412 verifyFormat("while ( a )\n return;", Spaces); 21413 verifyFormat("while ( (a && b) )\n return;", Spaces); 21414 verifyFormat("do {\n} while ( 1 != 0 );", Spaces); 21415 verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces); 21416 // Check that space on the left of "::" is inserted as expected at beginning 21417 // of condition. 21418 verifyFormat("while ( ::func() )\n return;", Spaces); 21419 21420 // Check impact of ControlStatementsExceptControlMacros is honored. 21421 Spaces.SpaceBeforeParens = 21422 FormatStyle::SBPO_ControlStatementsExceptControlMacros; 21423 verifyFormat("MYIF( a )\n return;", Spaces); 21424 verifyFormat("MYIF( a )\n return;\nelse MYIF( b )\n return;", Spaces); 21425 verifyFormat("MYIF( a )\n return;\nelse\n return;", Spaces); 21426 } 21427 21428 TEST_F(FormatTest, AlternativeOperators) { 21429 // Test case for ensuring alternate operators are not 21430 // combined with their right most neighbour. 21431 verifyFormat("int a and b;"); 21432 verifyFormat("int a and_eq b;"); 21433 verifyFormat("int a bitand b;"); 21434 verifyFormat("int a bitor b;"); 21435 verifyFormat("int a compl b;"); 21436 verifyFormat("int a not b;"); 21437 verifyFormat("int a not_eq b;"); 21438 verifyFormat("int a or b;"); 21439 verifyFormat("int a xor b;"); 21440 verifyFormat("int a xor_eq b;"); 21441 verifyFormat("return this not_eq bitand other;"); 21442 verifyFormat("bool operator not_eq(const X bitand other)"); 21443 21444 verifyFormat("int a and 5;"); 21445 verifyFormat("int a and_eq 5;"); 21446 verifyFormat("int a bitand 5;"); 21447 verifyFormat("int a bitor 5;"); 21448 verifyFormat("int a compl 5;"); 21449 verifyFormat("int a not 5;"); 21450 verifyFormat("int a not_eq 5;"); 21451 verifyFormat("int a or 5;"); 21452 verifyFormat("int a xor 5;"); 21453 verifyFormat("int a xor_eq 5;"); 21454 21455 verifyFormat("int a compl(5);"); 21456 verifyFormat("int a not(5);"); 21457 21458 /* FIXME handle alternate tokens 21459 * https://en.cppreference.com/w/cpp/language/operator_alternative 21460 // alternative tokens 21461 verifyFormat("compl foo();"); // ~foo(); 21462 verifyFormat("foo() <%%>;"); // foo(); 21463 verifyFormat("void foo() <%%>;"); // void foo(){} 21464 verifyFormat("int a <:1:>;"); // int a[1];[ 21465 verifyFormat("%:define ABC abc"); // #define ABC abc 21466 verifyFormat("%:%:"); // ## 21467 */ 21468 } 21469 21470 TEST_F(FormatTest, STLWhileNotDefineChed) { 21471 verifyFormat("#if defined(while)\n" 21472 "#define while EMIT WARNING C4005\n" 21473 "#endif // while"); 21474 } 21475 21476 TEST_F(FormatTest, OperatorSpacing) { 21477 FormatStyle Style = getLLVMStyle(); 21478 Style.PointerAlignment = FormatStyle::PAS_Right; 21479 verifyFormat("Foo::operator*();", Style); 21480 verifyFormat("Foo::operator void *();", Style); 21481 verifyFormat("Foo::operator void **();", Style); 21482 verifyFormat("Foo::operator void *&();", Style); 21483 verifyFormat("Foo::operator void *&&();", Style); 21484 verifyFormat("Foo::operator void const *();", Style); 21485 verifyFormat("Foo::operator void const **();", Style); 21486 verifyFormat("Foo::operator void const *&();", Style); 21487 verifyFormat("Foo::operator void const *&&();", Style); 21488 verifyFormat("Foo::operator()(void *);", Style); 21489 verifyFormat("Foo::operator*(void *);", Style); 21490 verifyFormat("Foo::operator*();", Style); 21491 verifyFormat("Foo::operator**();", Style); 21492 verifyFormat("Foo::operator&();", Style); 21493 verifyFormat("Foo::operator<int> *();", Style); 21494 verifyFormat("Foo::operator<Foo> *();", Style); 21495 verifyFormat("Foo::operator<int> **();", Style); 21496 verifyFormat("Foo::operator<Foo> **();", Style); 21497 verifyFormat("Foo::operator<int> &();", Style); 21498 verifyFormat("Foo::operator<Foo> &();", Style); 21499 verifyFormat("Foo::operator<int> &&();", Style); 21500 verifyFormat("Foo::operator<Foo> &&();", Style); 21501 verifyFormat("Foo::operator<int> *&();", Style); 21502 verifyFormat("Foo::operator<Foo> *&();", Style); 21503 verifyFormat("Foo::operator<int> *&&();", Style); 21504 verifyFormat("Foo::operator<Foo> *&&();", Style); 21505 verifyFormat("operator*(int (*)(), class Foo);", Style); 21506 21507 verifyFormat("Foo::operator&();", Style); 21508 verifyFormat("Foo::operator void &();", Style); 21509 verifyFormat("Foo::operator void const &();", Style); 21510 verifyFormat("Foo::operator()(void &);", Style); 21511 verifyFormat("Foo::operator&(void &);", Style); 21512 verifyFormat("Foo::operator&();", Style); 21513 verifyFormat("operator&(int (&)(), class Foo);", Style); 21514 21515 verifyFormat("Foo::operator&&();", Style); 21516 verifyFormat("Foo::operator**();", Style); 21517 verifyFormat("Foo::operator void &&();", Style); 21518 verifyFormat("Foo::operator void const &&();", Style); 21519 verifyFormat("Foo::operator()(void &&);", Style); 21520 verifyFormat("Foo::operator&&(void &&);", Style); 21521 verifyFormat("Foo::operator&&();", Style); 21522 verifyFormat("operator&&(int(&&)(), class Foo);", Style); 21523 verifyFormat("operator const nsTArrayRight<E> &()", Style); 21524 verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()", 21525 Style); 21526 verifyFormat("operator void **()", Style); 21527 verifyFormat("operator const FooRight<Object> &()", Style); 21528 verifyFormat("operator const FooRight<Object> *()", Style); 21529 verifyFormat("operator const FooRight<Object> **()", Style); 21530 verifyFormat("operator const FooRight<Object> *&()", Style); 21531 verifyFormat("operator const FooRight<Object> *&&()", Style); 21532 21533 Style.PointerAlignment = FormatStyle::PAS_Left; 21534 verifyFormat("Foo::operator*();", Style); 21535 verifyFormat("Foo::operator**();", Style); 21536 verifyFormat("Foo::operator void*();", Style); 21537 verifyFormat("Foo::operator void**();", Style); 21538 verifyFormat("Foo::operator void*&();", Style); 21539 verifyFormat("Foo::operator void*&&();", Style); 21540 verifyFormat("Foo::operator void const*();", Style); 21541 verifyFormat("Foo::operator void const**();", Style); 21542 verifyFormat("Foo::operator void const*&();", Style); 21543 verifyFormat("Foo::operator void const*&&();", Style); 21544 verifyFormat("Foo::operator/*comment*/ void*();", Style); 21545 verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style); 21546 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style); 21547 verifyFormat("Foo::operator()(void*);", Style); 21548 verifyFormat("Foo::operator*(void*);", Style); 21549 verifyFormat("Foo::operator*();", Style); 21550 verifyFormat("Foo::operator<int>*();", Style); 21551 verifyFormat("Foo::operator<Foo>*();", Style); 21552 verifyFormat("Foo::operator<int>**();", Style); 21553 verifyFormat("Foo::operator<Foo>**();", Style); 21554 verifyFormat("Foo::operator<Foo>*&();", Style); 21555 verifyFormat("Foo::operator<int>&();", Style); 21556 verifyFormat("Foo::operator<Foo>&();", Style); 21557 verifyFormat("Foo::operator<int>&&();", Style); 21558 verifyFormat("Foo::operator<Foo>&&();", Style); 21559 verifyFormat("Foo::operator<int>*&();", Style); 21560 verifyFormat("Foo::operator<Foo>*&();", Style); 21561 verifyFormat("operator*(int (*)(), class Foo);", Style); 21562 21563 verifyFormat("Foo::operator&();", Style); 21564 verifyFormat("Foo::operator void&();", Style); 21565 verifyFormat("Foo::operator void const&();", Style); 21566 verifyFormat("Foo::operator/*comment*/ void&();", Style); 21567 verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style); 21568 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style); 21569 verifyFormat("Foo::operator()(void&);", Style); 21570 verifyFormat("Foo::operator&(void&);", Style); 21571 verifyFormat("Foo::operator&();", Style); 21572 verifyFormat("operator&(int (&)(), class Foo);", Style); 21573 21574 verifyFormat("Foo::operator&&();", Style); 21575 verifyFormat("Foo::operator void&&();", Style); 21576 verifyFormat("Foo::operator void const&&();", Style); 21577 verifyFormat("Foo::operator/*comment*/ void&&();", Style); 21578 verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style); 21579 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style); 21580 verifyFormat("Foo::operator()(void&&);", Style); 21581 verifyFormat("Foo::operator&&(void&&);", Style); 21582 verifyFormat("Foo::operator&&();", Style); 21583 verifyFormat("operator&&(int(&&)(), class Foo);", Style); 21584 verifyFormat("operator const nsTArrayLeft<E>&()", Style); 21585 verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()", 21586 Style); 21587 verifyFormat("operator void**()", Style); 21588 verifyFormat("operator const FooLeft<Object>&()", Style); 21589 verifyFormat("operator const FooLeft<Object>*()", Style); 21590 verifyFormat("operator const FooLeft<Object>**()", Style); 21591 verifyFormat("operator const FooLeft<Object>*&()", Style); 21592 verifyFormat("operator const FooLeft<Object>*&&()", Style); 21593 21594 // PR45107 21595 verifyFormat("operator Vector<String>&();", Style); 21596 verifyFormat("operator const Vector<String>&();", Style); 21597 verifyFormat("operator foo::Bar*();", Style); 21598 verifyFormat("operator const Foo<X>::Bar<Y>*();", Style); 21599 verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();", 21600 Style); 21601 21602 Style.PointerAlignment = FormatStyle::PAS_Middle; 21603 verifyFormat("Foo::operator*();", Style); 21604 verifyFormat("Foo::operator void *();", Style); 21605 verifyFormat("Foo::operator()(void *);", Style); 21606 verifyFormat("Foo::operator*(void *);", Style); 21607 verifyFormat("Foo::operator*();", Style); 21608 verifyFormat("operator*(int (*)(), class Foo);", Style); 21609 21610 verifyFormat("Foo::operator&();", Style); 21611 verifyFormat("Foo::operator void &();", Style); 21612 verifyFormat("Foo::operator void const &();", Style); 21613 verifyFormat("Foo::operator()(void &);", Style); 21614 verifyFormat("Foo::operator&(void &);", Style); 21615 verifyFormat("Foo::operator&();", Style); 21616 verifyFormat("operator&(int (&)(), class Foo);", Style); 21617 21618 verifyFormat("Foo::operator&&();", Style); 21619 verifyFormat("Foo::operator void &&();", Style); 21620 verifyFormat("Foo::operator void const &&();", Style); 21621 verifyFormat("Foo::operator()(void &&);", Style); 21622 verifyFormat("Foo::operator&&(void &&);", Style); 21623 verifyFormat("Foo::operator&&();", Style); 21624 verifyFormat("operator&&(int(&&)(), class Foo);", Style); 21625 } 21626 21627 TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) { 21628 FormatStyle Style = getLLVMStyle(); 21629 // PR46157 21630 verifyFormat("foo(operator+, -42);", Style); 21631 verifyFormat("foo(operator++, -42);", Style); 21632 verifyFormat("foo(operator--, -42);", Style); 21633 verifyFormat("foo(-42, operator--);", Style); 21634 verifyFormat("foo(-42, operator, );", Style); 21635 verifyFormat("foo(operator, , -42);", Style); 21636 } 21637 21638 TEST_F(FormatTest, WhitespaceSensitiveMacros) { 21639 FormatStyle Style = getLLVMStyle(); 21640 Style.WhitespaceSensitiveMacros.push_back("FOO"); 21641 21642 // Don't use the helpers here, since 'mess up' will change the whitespace 21643 // and these are all whitespace sensitive by definition 21644 EXPECT_EQ("FOO(String-ized&Messy+But(: :Still)=Intentional);", 21645 format("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style)); 21646 EXPECT_EQ( 21647 "FOO(String-ized&Messy+But\\(: :Still)=Intentional);", 21648 format("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style)); 21649 EXPECT_EQ("FOO(String-ized&Messy+But,: :Still=Intentional);", 21650 format("FOO(String-ized&Messy+But,: :Still=Intentional);", Style)); 21651 EXPECT_EQ("FOO(String-ized&Messy+But,: :\n" 21652 " Still=Intentional);", 21653 format("FOO(String-ized&Messy+But,: :\n" 21654 " Still=Intentional);", 21655 Style)); 21656 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 21657 EXPECT_EQ("FOO(String-ized=&Messy+But,: :\n" 21658 " Still=Intentional);", 21659 format("FOO(String-ized=&Messy+But,: :\n" 21660 " Still=Intentional);", 21661 Style)); 21662 21663 Style.ColumnLimit = 21; 21664 EXPECT_EQ("FOO(String-ized&Messy+But: :Still=Intentional);", 21665 format("FOO(String-ized&Messy+But: :Still=Intentional);", Style)); 21666 } 21667 21668 TEST_F(FormatTest, VeryLongNamespaceCommentSplit) { 21669 // These tests are not in NamespaceFixer because that doesn't 21670 // test its interaction with line wrapping 21671 FormatStyle Style = getLLVMStyle(); 21672 Style.ColumnLimit = 80; 21673 verifyFormat("namespace {\n" 21674 "int i;\n" 21675 "int j;\n" 21676 "} // namespace", 21677 Style); 21678 21679 verifyFormat("namespace AAA {\n" 21680 "int i;\n" 21681 "int j;\n" 21682 "} // namespace AAA", 21683 Style); 21684 21685 EXPECT_EQ("namespace Averyveryveryverylongnamespace {\n" 21686 "int i;\n" 21687 "int j;\n" 21688 "} // namespace Averyveryveryverylongnamespace", 21689 format("namespace Averyveryveryverylongnamespace {\n" 21690 "int i;\n" 21691 "int j;\n" 21692 "}", 21693 Style)); 21694 21695 EXPECT_EQ( 21696 "namespace " 21697 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" 21698 " went::mad::now {\n" 21699 "int i;\n" 21700 "int j;\n" 21701 "} // namespace\n" 21702 " // " 21703 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" 21704 "went::mad::now", 21705 format("namespace " 21706 "would::it::save::you::a::lot::of::time::if_::i::" 21707 "just::gave::up::and_::went::mad::now {\n" 21708 "int i;\n" 21709 "int j;\n" 21710 "}", 21711 Style)); 21712 21713 // This used to duplicate the comment again and again on subsequent runs 21714 EXPECT_EQ( 21715 "namespace " 21716 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" 21717 " went::mad::now {\n" 21718 "int i;\n" 21719 "int j;\n" 21720 "} // namespace\n" 21721 " // " 21722 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" 21723 "went::mad::now", 21724 format("namespace " 21725 "would::it::save::you::a::lot::of::time::if_::i::" 21726 "just::gave::up::and_::went::mad::now {\n" 21727 "int i;\n" 21728 "int j;\n" 21729 "} // namespace\n" 21730 " // " 21731 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::" 21732 "and_::went::mad::now", 21733 Style)); 21734 } 21735 21736 TEST_F(FormatTest, LikelyUnlikely) { 21737 FormatStyle Style = getLLVMStyle(); 21738 21739 verifyFormat("if (argc > 5) [[unlikely]] {\n" 21740 " return 29;\n" 21741 "}", 21742 Style); 21743 21744 verifyFormat("if (argc > 5) [[likely]] {\n" 21745 " return 29;\n" 21746 "}", 21747 Style); 21748 21749 verifyFormat("if (argc > 5) [[unlikely]] {\n" 21750 " return 29;\n" 21751 "} else [[likely]] {\n" 21752 " return 42;\n" 21753 "}\n", 21754 Style); 21755 21756 verifyFormat("if (argc > 5) [[unlikely]] {\n" 21757 " return 29;\n" 21758 "} else if (argc > 10) [[likely]] {\n" 21759 " return 99;\n" 21760 "} else {\n" 21761 " return 42;\n" 21762 "}\n", 21763 Style); 21764 21765 verifyFormat("if (argc > 5) [[gnu::unused]] {\n" 21766 " return 29;\n" 21767 "}", 21768 Style); 21769 } 21770 21771 TEST_F(FormatTest, PenaltyIndentedWhitespace) { 21772 verifyFormat("Constructor()\n" 21773 " : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 21774 " aaaa(aaaaaaaaaaaaaaaaaa, " 21775 "aaaaaaaaaaaaaaaaaat))"); 21776 verifyFormat("Constructor()\n" 21777 " : aaaaaaaaaaaaa(aaaaaa), " 21778 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)"); 21779 21780 FormatStyle StyleWithWhitespacePenalty = getLLVMStyle(); 21781 StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5; 21782 verifyFormat("Constructor()\n" 21783 " : aaaaaa(aaaaaa),\n" 21784 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 21785 " aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))", 21786 StyleWithWhitespacePenalty); 21787 verifyFormat("Constructor()\n" 21788 " : aaaaaaaaaaaaa(aaaaaa), " 21789 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)", 21790 StyleWithWhitespacePenalty); 21791 } 21792 21793 TEST_F(FormatTest, LLVMDefaultStyle) { 21794 FormatStyle Style = getLLVMStyle(); 21795 verifyFormat("extern \"C\" {\n" 21796 "int foo();\n" 21797 "}", 21798 Style); 21799 } 21800 TEST_F(FormatTest, GNUDefaultStyle) { 21801 FormatStyle Style = getGNUStyle(); 21802 verifyFormat("extern \"C\"\n" 21803 "{\n" 21804 " int foo ();\n" 21805 "}", 21806 Style); 21807 } 21808 TEST_F(FormatTest, MozillaDefaultStyle) { 21809 FormatStyle Style = getMozillaStyle(); 21810 verifyFormat("extern \"C\"\n" 21811 "{\n" 21812 " int foo();\n" 21813 "}", 21814 Style); 21815 } 21816 TEST_F(FormatTest, GoogleDefaultStyle) { 21817 FormatStyle Style = getGoogleStyle(); 21818 verifyFormat("extern \"C\" {\n" 21819 "int foo();\n" 21820 "}", 21821 Style); 21822 } 21823 TEST_F(FormatTest, ChromiumDefaultStyle) { 21824 FormatStyle Style = getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp); 21825 verifyFormat("extern \"C\" {\n" 21826 "int foo();\n" 21827 "}", 21828 Style); 21829 } 21830 TEST_F(FormatTest, MicrosoftDefaultStyle) { 21831 FormatStyle Style = getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp); 21832 verifyFormat("extern \"C\"\n" 21833 "{\n" 21834 " int foo();\n" 21835 "}", 21836 Style); 21837 } 21838 TEST_F(FormatTest, WebKitDefaultStyle) { 21839 FormatStyle Style = getWebKitStyle(); 21840 verifyFormat("extern \"C\" {\n" 21841 "int foo();\n" 21842 "}", 21843 Style); 21844 } 21845 21846 TEST_F(FormatTest, ConceptsAndRequires) { 21847 FormatStyle Style = getLLVMStyle(); 21848 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 21849 21850 verifyFormat("template <typename T>\n" 21851 "concept Hashable = requires(T a) {\n" 21852 " { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n" 21853 "};", 21854 Style); 21855 verifyFormat("template <typename T>\n" 21856 "concept EqualityComparable = requires(T a, T b) {\n" 21857 " { a == b } -> bool;\n" 21858 "};", 21859 Style); 21860 verifyFormat("template <typename T>\n" 21861 "concept EqualityComparable = requires(T a, T b) {\n" 21862 " { a == b } -> bool;\n" 21863 " { a != b } -> bool;\n" 21864 "};", 21865 Style); 21866 verifyFormat("template <typename T>\n" 21867 "concept EqualityComparable = requires(T a, T b) {\n" 21868 " { a == b } -> bool;\n" 21869 " { a != b } -> bool;\n" 21870 "};", 21871 Style); 21872 21873 verifyFormat("template <typename It>\n" 21874 "requires Iterator<It>\n" 21875 "void sort(It begin, It end) {\n" 21876 " //....\n" 21877 "}", 21878 Style); 21879 21880 verifyFormat("template <typename T>\n" 21881 "concept Large = sizeof(T) > 10;", 21882 Style); 21883 21884 verifyFormat("template <typename T, typename U>\n" 21885 "concept FooableWith = requires(T t, U u) {\n" 21886 " typename T::foo_type;\n" 21887 " { t.foo(u) } -> typename T::foo_type;\n" 21888 " t++;\n" 21889 "};\n" 21890 "void doFoo(FooableWith<int> auto t) {\n" 21891 " t.foo(3);\n" 21892 "}", 21893 Style); 21894 verifyFormat("template <typename T>\n" 21895 "concept Context = sizeof(T) == 1;", 21896 Style); 21897 verifyFormat("template <typename T>\n" 21898 "concept Context = is_specialization_of_v<context, T>;", 21899 Style); 21900 verifyFormat("template <typename T>\n" 21901 "concept Node = std::is_object_v<T>;", 21902 Style); 21903 verifyFormat("template <typename T>\n" 21904 "concept Tree = true;", 21905 Style); 21906 21907 verifyFormat("template <typename T> int g(T i) requires Concept1<I> {\n" 21908 " //...\n" 21909 "}", 21910 Style); 21911 21912 verifyFormat( 21913 "template <typename T> int g(T i) requires Concept1<I> && Concept2<I> {\n" 21914 " //...\n" 21915 "}", 21916 Style); 21917 21918 verifyFormat( 21919 "template <typename T> int g(T i) requires Concept1<I> || Concept2<I> {\n" 21920 " //...\n" 21921 "}", 21922 Style); 21923 21924 verifyFormat("template <typename T>\n" 21925 "veryveryvery_long_return_type g(T i) requires Concept1<I> || " 21926 "Concept2<I> {\n" 21927 " //...\n" 21928 "}", 21929 Style); 21930 21931 verifyFormat("template <typename T>\n" 21932 "veryveryvery_long_return_type g(T i) requires Concept1<I> && " 21933 "Concept2<I> {\n" 21934 " //...\n" 21935 "}", 21936 Style); 21937 21938 verifyFormat( 21939 "template <typename T>\n" 21940 "veryveryvery_long_return_type g(T i) requires Concept1 && Concept2 {\n" 21941 " //...\n" 21942 "}", 21943 Style); 21944 21945 verifyFormat( 21946 "template <typename T>\n" 21947 "veryveryvery_long_return_type g(T i) requires Concept1 || Concept2 {\n" 21948 " //...\n" 21949 "}", 21950 Style); 21951 21952 verifyFormat("template <typename It>\n" 21953 "requires Foo<It>() && Bar<It> {\n" 21954 " //....\n" 21955 "}", 21956 Style); 21957 21958 verifyFormat("template <typename It>\n" 21959 "requires Foo<Bar<It>>() && Bar<Foo<It, It>> {\n" 21960 " //....\n" 21961 "}", 21962 Style); 21963 21964 verifyFormat("template <typename It>\n" 21965 "requires Foo<Bar<It, It>>() && Bar<Foo<It, It>> {\n" 21966 " //....\n" 21967 "}", 21968 Style); 21969 21970 verifyFormat( 21971 "template <typename It>\n" 21972 "requires Foo<Bar<It>, Baz<It>>() && Bar<Foo<It>, Baz<It, It>> {\n" 21973 " //....\n" 21974 "}", 21975 Style); 21976 21977 Style.IndentRequires = true; 21978 verifyFormat("template <typename It>\n" 21979 " requires Iterator<It>\n" 21980 "void sort(It begin, It end) {\n" 21981 " //....\n" 21982 "}", 21983 Style); 21984 verifyFormat("template <std::size index_>\n" 21985 " requires(index_ < sizeof...(Children_))\n" 21986 "Tree auto &child() {\n" 21987 " // ...\n" 21988 "}", 21989 Style); 21990 21991 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 21992 verifyFormat("template <typename T>\n" 21993 "concept Hashable = requires (T a) {\n" 21994 " { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n" 21995 "};", 21996 Style); 21997 21998 verifyFormat("template <class T = void>\n" 21999 " requires EqualityComparable<T> || Same<T, void>\n" 22000 "struct equal_to;", 22001 Style); 22002 22003 verifyFormat("template <class T>\n" 22004 " requires requires {\n" 22005 " T{};\n" 22006 " T (int);\n" 22007 " }\n", 22008 Style); 22009 22010 Style.ColumnLimit = 78; 22011 verifyFormat("template <typename T>\n" 22012 "concept Context = Traits<typename T::traits_type> and\n" 22013 " Interface<typename T::interface_type> and\n" 22014 " Request<typename T::request_type> and\n" 22015 " Response<typename T::response_type> and\n" 22016 " ContextExtension<typename T::extension_type> and\n" 22017 " ::std::is_copy_constructable<T> and " 22018 "::std::is_move_constructable<T> and\n" 22019 " requires (T c) {\n" 22020 " { c.response; } -> Response;\n" 22021 "} and requires (T c) {\n" 22022 " { c.request; } -> Request;\n" 22023 "}\n", 22024 Style); 22025 22026 verifyFormat("template <typename T>\n" 22027 "concept Context = Traits<typename T::traits_type> or\n" 22028 " Interface<typename T::interface_type> or\n" 22029 " Request<typename T::request_type> or\n" 22030 " Response<typename T::response_type> or\n" 22031 " ContextExtension<typename T::extension_type> or\n" 22032 " ::std::is_copy_constructable<T> or " 22033 "::std::is_move_constructable<T> or\n" 22034 " requires (T c) {\n" 22035 " { c.response; } -> Response;\n" 22036 "} or requires (T c) {\n" 22037 " { c.request; } -> Request;\n" 22038 "}\n", 22039 Style); 22040 22041 verifyFormat("template <typename T>\n" 22042 "concept Context = Traits<typename T::traits_type> &&\n" 22043 " Interface<typename T::interface_type> &&\n" 22044 " Request<typename T::request_type> &&\n" 22045 " Response<typename T::response_type> &&\n" 22046 " ContextExtension<typename T::extension_type> &&\n" 22047 " ::std::is_copy_constructable<T> && " 22048 "::std::is_move_constructable<T> &&\n" 22049 " requires (T c) {\n" 22050 " { c.response; } -> Response;\n" 22051 "} && requires (T c) {\n" 22052 " { c.request; } -> Request;\n" 22053 "}\n", 22054 Style); 22055 22056 verifyFormat("template <typename T>\nconcept someConcept = Constraint1<T> && " 22057 "Constraint2<T>;"); 22058 22059 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 22060 Style.BraceWrapping.AfterFunction = true; 22061 Style.BraceWrapping.AfterClass = true; 22062 Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 22063 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 22064 verifyFormat("void Foo () requires (std::copyable<T>)\n" 22065 "{\n" 22066 " return\n" 22067 "}\n", 22068 Style); 22069 22070 verifyFormat("void Foo () requires std::copyable<T>\n" 22071 "{\n" 22072 " return\n" 22073 "}\n", 22074 Style); 22075 22076 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 22077 " requires (std::invocable<F, std::invoke_result_t<Args>...>)\n" 22078 "struct constant;", 22079 Style); 22080 22081 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 22082 " requires std::invocable<F, std::invoke_result_t<Args>...>\n" 22083 "struct constant;", 22084 Style); 22085 22086 verifyFormat("template <class T>\n" 22087 "class plane_with_very_very_very_long_name\n" 22088 "{\n" 22089 " constexpr plane_with_very_very_very_long_name () requires " 22090 "std::copyable<T>\n" 22091 " : plane_with_very_very_very_long_name (1)\n" 22092 " {\n" 22093 " }\n" 22094 "}\n", 22095 Style); 22096 22097 verifyFormat("template <class T>\n" 22098 "class plane_with_long_name\n" 22099 "{\n" 22100 " constexpr plane_with_long_name () requires std::copyable<T>\n" 22101 " : plane_with_long_name (1)\n" 22102 " {\n" 22103 " }\n" 22104 "}\n", 22105 Style); 22106 22107 Style.BreakBeforeConceptDeclarations = false; 22108 verifyFormat("template <typename T> concept Tree = true;", Style); 22109 22110 Style.IndentRequires = false; 22111 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 22112 "requires (std::invocable<F, std::invoke_result_t<Args>...>) " 22113 "struct constant;", 22114 Style); 22115 } 22116 22117 TEST_F(FormatTest, StatementAttributeLikeMacros) { 22118 FormatStyle Style = getLLVMStyle(); 22119 StringRef Source = "void Foo::slot() {\n" 22120 " unsigned char MyChar = 'x';\n" 22121 " emit signal(MyChar);\n" 22122 " Q_EMIT signal(MyChar);\n" 22123 "}"; 22124 22125 EXPECT_EQ(Source, format(Source, Style)); 22126 22127 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 22128 EXPECT_EQ("void Foo::slot() {\n" 22129 " unsigned char MyChar = 'x';\n" 22130 " emit signal(MyChar);\n" 22131 " Q_EMIT signal(MyChar);\n" 22132 "}", 22133 format(Source, Style)); 22134 22135 Style.StatementAttributeLikeMacros.push_back("emit"); 22136 EXPECT_EQ(Source, format(Source, Style)); 22137 22138 Style.StatementAttributeLikeMacros = {}; 22139 EXPECT_EQ("void Foo::slot() {\n" 22140 " unsigned char MyChar = 'x';\n" 22141 " emit signal(MyChar);\n" 22142 " Q_EMIT signal(MyChar);\n" 22143 "}", 22144 format(Source, Style)); 22145 } 22146 22147 TEST_F(FormatTest, IndentAccessModifiers) { 22148 FormatStyle Style = getLLVMStyle(); 22149 Style.IndentAccessModifiers = true; 22150 // Members are *two* levels below the record; 22151 // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation. 22152 verifyFormat("class C {\n" 22153 " int i;\n" 22154 "};\n", 22155 Style); 22156 verifyFormat("union C {\n" 22157 " int i;\n" 22158 " unsigned u;\n" 22159 "};\n", 22160 Style); 22161 // Access modifiers should be indented one level below the record. 22162 verifyFormat("class C {\n" 22163 " public:\n" 22164 " int i;\n" 22165 "};\n", 22166 Style); 22167 verifyFormat("struct S {\n" 22168 " private:\n" 22169 " class C {\n" 22170 " int j;\n" 22171 "\n" 22172 " public:\n" 22173 " C();\n" 22174 " };\n" 22175 "\n" 22176 " public:\n" 22177 " int i;\n" 22178 "};\n", 22179 Style); 22180 // Enumerations are not records and should be unaffected. 22181 Style.AllowShortEnumsOnASingleLine = false; 22182 verifyFormat("enum class E {\n" 22183 " A,\n" 22184 " B\n" 22185 "};\n", 22186 Style); 22187 // Test with a different indentation width; 22188 // also proves that the result is Style.AccessModifierOffset agnostic. 22189 Style.IndentWidth = 3; 22190 verifyFormat("class C {\n" 22191 " public:\n" 22192 " int i;\n" 22193 "};\n", 22194 Style); 22195 } 22196 22197 TEST_F(FormatTest, LimitlessStringsAndComments) { 22198 auto Style = getLLVMStyleWithColumns(0); 22199 constexpr StringRef Code = 22200 "/**\n" 22201 " * This is a multiline comment with quite some long lines, at least for " 22202 "the LLVM Style.\n" 22203 " * We will redo this with strings and line comments. Just to check if " 22204 "everything is working.\n" 22205 " */\n" 22206 "bool foo() {\n" 22207 " /* Single line multi line comment. */\n" 22208 " const std::string String = \"This is a multiline string with quite " 22209 "some long lines, at least for the LLVM Style.\"\n" 22210 " \"We already did it with multi line " 22211 "comments, and we will do it with line comments. Just to check if " 22212 "everything is working.\";\n" 22213 " // This is a line comment (block) with quite some long lines, at " 22214 "least for the LLVM Style.\n" 22215 " // We already did this with multi line comments and strings. Just to " 22216 "check if everything is working.\n" 22217 " const std::string SmallString = \"Hello World\";\n" 22218 " // Small line comment\n" 22219 " return String.size() > SmallString.size();\n" 22220 "}"; 22221 EXPECT_EQ(Code, format(Code, Style)); 22222 } 22223 } // namespace 22224 } // namespace format 22225 } // namespace clang 22226