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 = getLLVMStyle(); 266 CustomStyle.BreakBeforeBraces = 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 verifyFormat("for (auto a = 0, b = 0; const auto &c : {1, 2, 3})", Style); 1928 verifyFormat("for (auto a = 0, b = 0; const int &c : {1, 2, 3})", Style); 1929 verifyFormat("for (auto a = 0, b = 0; const Foo &c : {1, 2, 3})", Style); 1930 verifyFormat("for (auto a = 0, b = 0; const Foo *c : {1, 2, 3})", Style); 1931 verifyFormat("for (int a = 0, b = 0; const auto &c : {1, 2, 3})", Style); 1932 verifyFormat("for (int a = 0, b = 0; const int &c : {1, 2, 3})", Style); 1933 verifyFormat("for (int a = 0, b = 0; const Foo &c : {1, 2, 3})", Style); 1934 verifyFormat("for (int a = 0, b++; const auto &c : {1, 2, 3})", Style); 1935 verifyFormat("for (int a = 0, b++; const int &c : {1, 2, 3})", Style); 1936 verifyFormat("for (int a = 0, b++; const Foo &c : {1, 2, 3})", Style); 1937 verifyFormat("for (auto x = 0; auto &c : {1, 2, 3})", Style); 1938 verifyFormat("for (auto x = 0; int &c : {1, 2, 3})", Style); 1939 verifyFormat("for (int x = 0; auto &c : {1, 2, 3})", Style); 1940 verifyFormat("for (int x = 0; int &c : {1, 2, 3})", Style); 1941 verifyFormat("for (f(); auto &c : {1, 2, 3})", Style); 1942 verifyFormat("for (f(); int &c : {1, 2, 3})", Style); 1943 1944 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 1945 verifyFormat("Const unsigned int *c;\n" 1946 "const unsigned int *d;\n" 1947 "Const unsigned int &e;\n" 1948 "const unsigned int &f;\n" 1949 "const unsigned &&g;\n" 1950 "Const unsigned h;", 1951 Style); 1952 1953 Style.PointerAlignment = FormatStyle::PAS_Left; 1954 Style.ReferenceAlignment = FormatStyle::RAS_Pointer; 1955 verifyFormat("int* f1(int* a, int& b, int&& c);", Style); 1956 verifyFormat("int& f2(int&& c, int* a, int& b);", Style); 1957 verifyFormat("int&& f3(int& b, int&& c, int* a);", Style); 1958 verifyFormat("int* f1(int& a) const& = 0;", Style); 1959 verifyFormat("int* a = f1();", Style); 1960 verifyFormat("int& b = f2();", Style); 1961 verifyFormat("int&& c = f3();", Style); 1962 verifyFormat("for (auto a = 0, b = 0; const auto& c : {1, 2, 3})", Style); 1963 verifyFormat("for (auto a = 0, b = 0; const int& c : {1, 2, 3})", Style); 1964 verifyFormat("for (auto a = 0, b = 0; const Foo& c : {1, 2, 3})", Style); 1965 verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style); 1966 verifyFormat("for (int a = 0, b = 0; const auto& c : {1, 2, 3})", Style); 1967 verifyFormat("for (int a = 0, b = 0; const int& c : {1, 2, 3})", Style); 1968 verifyFormat("for (int a = 0, b = 0; const Foo& c : {1, 2, 3})", Style); 1969 verifyFormat("for (int a = 0, b = 0; const Foo* c : {1, 2, 3})", Style); 1970 verifyFormat("for (int a = 0, b++; const auto& c : {1, 2, 3})", Style); 1971 verifyFormat("for (int a = 0, b++; const int& c : {1, 2, 3})", Style); 1972 verifyFormat("for (int a = 0, b++; const Foo& c : {1, 2, 3})", Style); 1973 verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style); 1974 verifyFormat("for (auto x = 0; auto& c : {1, 2, 3})", Style); 1975 verifyFormat("for (auto x = 0; int& c : {1, 2, 3})", Style); 1976 verifyFormat("for (int x = 0; auto& c : {1, 2, 3})", Style); 1977 verifyFormat("for (int x = 0; int& c : {1, 2, 3})", Style); 1978 verifyFormat("for (f(); auto& c : {1, 2, 3})", Style); 1979 verifyFormat("for (f(); int& c : {1, 2, 3})", Style); 1980 1981 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 1982 verifyFormat("Const unsigned int* c;\n" 1983 "const unsigned int* d;\n" 1984 "Const unsigned int& e;\n" 1985 "const unsigned int& f;\n" 1986 "const unsigned&& g;\n" 1987 "Const unsigned h;", 1988 Style); 1989 1990 Style.PointerAlignment = FormatStyle::PAS_Right; 1991 Style.ReferenceAlignment = FormatStyle::RAS_Left; 1992 verifyFormat("int *f1(int *a, int& b, int&& c);", Style); 1993 verifyFormat("int& f2(int&& c, int *a, int& b);", Style); 1994 verifyFormat("int&& f3(int& b, int&& c, int *a);", Style); 1995 verifyFormat("int *a = f1();", Style); 1996 verifyFormat("int& b = f2();", Style); 1997 verifyFormat("int&& c = f3();", Style); 1998 verifyFormat("for (auto a = 0, b = 0; const Foo *c : {1, 2, 3})", Style); 1999 verifyFormat("for (int a = 0, b = 0; const Foo *c : {1, 2, 3})", Style); 2000 verifyFormat("for (int a = 0, b++; const Foo *c : {1, 2, 3})", Style); 2001 2002 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 2003 verifyFormat("Const unsigned int *c;\n" 2004 "const unsigned int *d;\n" 2005 "Const unsigned int& e;\n" 2006 "const unsigned int& f;\n" 2007 "const unsigned g;\n" 2008 "Const unsigned h;", 2009 Style); 2010 2011 Style.PointerAlignment = FormatStyle::PAS_Left; 2012 Style.ReferenceAlignment = FormatStyle::RAS_Middle; 2013 verifyFormat("int* f1(int* a, int & b, int && c);", Style); 2014 verifyFormat("int & f2(int && c, int* a, int & b);", Style); 2015 verifyFormat("int && f3(int & b, int && c, int* a);", Style); 2016 verifyFormat("int* a = f1();", Style); 2017 verifyFormat("int & b = f2();", Style); 2018 verifyFormat("int && c = f3();", Style); 2019 verifyFormat("for (auto a = 0, b = 0; const auto & c : {1, 2, 3})", Style); 2020 verifyFormat("for (auto a = 0, b = 0; const int & c : {1, 2, 3})", Style); 2021 verifyFormat("for (auto a = 0, b = 0; const Foo & c : {1, 2, 3})", Style); 2022 verifyFormat("for (auto a = 0, b = 0; const Foo* c : {1, 2, 3})", Style); 2023 verifyFormat("for (int a = 0, b++; const auto & c : {1, 2, 3})", Style); 2024 verifyFormat("for (int a = 0, b++; const int & c : {1, 2, 3})", Style); 2025 verifyFormat("for (int a = 0, b++; const Foo & c : {1, 2, 3})", Style); 2026 verifyFormat("for (int a = 0, b++; const Foo* c : {1, 2, 3})", Style); 2027 verifyFormat("for (auto x = 0; auto & c : {1, 2, 3})", Style); 2028 verifyFormat("for (auto x = 0; int & c : {1, 2, 3})", Style); 2029 verifyFormat("for (int x = 0; auto & c : {1, 2, 3})", Style); 2030 verifyFormat("for (int x = 0; int & c : {1, 2, 3})", Style); 2031 verifyFormat("for (f(); auto & c : {1, 2, 3})", Style); 2032 verifyFormat("for (f(); int & c : {1, 2, 3})", Style); 2033 2034 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 2035 verifyFormat("Const unsigned int* c;\n" 2036 "const unsigned int* d;\n" 2037 "Const unsigned int & e;\n" 2038 "const unsigned int & f;\n" 2039 "const unsigned && g;\n" 2040 "Const unsigned h;", 2041 Style); 2042 2043 Style.PointerAlignment = FormatStyle::PAS_Middle; 2044 Style.ReferenceAlignment = FormatStyle::RAS_Right; 2045 verifyFormat("int * f1(int * a, int &b, int &&c);", Style); 2046 verifyFormat("int &f2(int &&c, int * a, int &b);", Style); 2047 verifyFormat("int &&f3(int &b, int &&c, int * a);", Style); 2048 verifyFormat("int * a = f1();", Style); 2049 verifyFormat("int &b = f2();", Style); 2050 verifyFormat("int &&c = f3();", Style); 2051 verifyFormat("for (auto a = 0, b = 0; const Foo * c : {1, 2, 3})", Style); 2052 verifyFormat("for (int a = 0, b = 0; const Foo * c : {1, 2, 3})", Style); 2053 verifyFormat("for (int a = 0, b++; const Foo * c : {1, 2, 3})", Style); 2054 2055 // FIXME: we don't handle this yet, so output may be arbitrary until it's 2056 // specifically handled 2057 // verifyFormat("int Add2(BTree * &Root, char * szToAdd)", Style); 2058 } 2059 2060 TEST_F(FormatTest, FormatsForLoop) { 2061 verifyFormat( 2062 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 2063 " ++VeryVeryLongLoopVariable)\n" 2064 " ;"); 2065 verifyFormat("for (;;)\n" 2066 " f();"); 2067 verifyFormat("for (;;) {\n}"); 2068 verifyFormat("for (;;) {\n" 2069 " f();\n" 2070 "}"); 2071 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}"); 2072 2073 verifyFormat( 2074 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 2075 " E = UnwrappedLines.end();\n" 2076 " I != E; ++I) {\n}"); 2077 2078 verifyFormat( 2079 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" 2080 " ++IIIII) {\n}"); 2081 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" 2082 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" 2083 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); 2084 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n" 2085 " I = FD->getDeclsInPrototypeScope().begin(),\n" 2086 " E = FD->getDeclsInPrototypeScope().end();\n" 2087 " I != E; ++I) {\n}"); 2088 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n" 2089 " I = Container.begin(),\n" 2090 " E = Container.end();\n" 2091 " I != E; ++I) {\n}", 2092 getLLVMStyleWithColumns(76)); 2093 2094 verifyFormat( 2095 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 2096 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" 2097 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2098 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 2099 " ++aaaaaaaaaaa) {\n}"); 2100 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 2101 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n" 2102 " ++i) {\n}"); 2103 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" 2104 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 2105 "}"); 2106 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n" 2107 " aaaaaaaaaa);\n" 2108 " iter; ++iter) {\n" 2109 "}"); 2110 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 2111 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 2112 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n" 2113 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {"); 2114 2115 // These should not be formatted as Objective-C for-in loops. 2116 verifyFormat("for (Foo *x = 0; x != in; x++) {\n}"); 2117 verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}"); 2118 verifyFormat("Foo *x;\nfor (x in y) {\n}"); 2119 verifyFormat( 2120 "for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}"); 2121 2122 FormatStyle NoBinPacking = getLLVMStyle(); 2123 NoBinPacking.BinPackParameters = false; 2124 verifyFormat("for (int aaaaaaaaaaa = 1;\n" 2125 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" 2126 " aaaaaaaaaaaaaaaa,\n" 2127 " aaaaaaaaaaaaaaaa,\n" 2128 " aaaaaaaaaaaaaaaa);\n" 2129 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 2130 "}", 2131 NoBinPacking); 2132 verifyFormat( 2133 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 2134 " E = UnwrappedLines.end();\n" 2135 " I != E;\n" 2136 " ++I) {\n}", 2137 NoBinPacking); 2138 2139 FormatStyle AlignLeft = getLLVMStyle(); 2140 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 2141 verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft); 2142 } 2143 2144 TEST_F(FormatTest, RangeBasedForLoops) { 2145 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 2146 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 2147 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" 2148 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); 2149 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" 2150 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 2151 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n" 2152 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}"); 2153 } 2154 2155 TEST_F(FormatTest, ForEachLoops) { 2156 verifyFormat("void f() {\n" 2157 " foreach (Item *item, itemlist) {}\n" 2158 " Q_FOREACH (Item *item, itemlist) {}\n" 2159 " BOOST_FOREACH (Item *item, itemlist) {}\n" 2160 " UNKNOWN_FORACH(Item * item, itemlist) {}\n" 2161 "}"); 2162 2163 FormatStyle Style = getLLVMStyle(); 2164 Style.SpaceBeforeParens = 2165 FormatStyle::SBPO_ControlStatementsExceptControlMacros; 2166 verifyFormat("void f() {\n" 2167 " foreach(Item *item, itemlist) {}\n" 2168 " Q_FOREACH(Item *item, itemlist) {}\n" 2169 " BOOST_FOREACH(Item *item, itemlist) {}\n" 2170 " UNKNOWN_FORACH(Item * item, itemlist) {}\n" 2171 "}", 2172 Style); 2173 2174 // As function-like macros. 2175 verifyFormat("#define foreach(x, y)\n" 2176 "#define Q_FOREACH(x, y)\n" 2177 "#define BOOST_FOREACH(x, y)\n" 2178 "#define UNKNOWN_FOREACH(x, y)\n"); 2179 2180 // Not as function-like macros. 2181 verifyFormat("#define foreach (x, y)\n" 2182 "#define Q_FOREACH (x, y)\n" 2183 "#define BOOST_FOREACH (x, y)\n" 2184 "#define UNKNOWN_FOREACH (x, y)\n"); 2185 2186 // handle microsoft non standard extension 2187 verifyFormat("for each (char c in x->MyStringProperty)"); 2188 } 2189 2190 TEST_F(FormatTest, FormatsWhileLoop) { 2191 verifyFormat("while (true) {\n}"); 2192 verifyFormat("while (true)\n" 2193 " f();"); 2194 verifyFormat("while () {\n}"); 2195 verifyFormat("while () {\n" 2196 " f();\n" 2197 "}"); 2198 } 2199 2200 TEST_F(FormatTest, FormatsDoWhile) { 2201 verifyFormat("do {\n" 2202 " do_something();\n" 2203 "} while (something());"); 2204 verifyFormat("do\n" 2205 " do_something();\n" 2206 "while (something());"); 2207 } 2208 2209 TEST_F(FormatTest, FormatsSwitchStatement) { 2210 verifyFormat("switch (x) {\n" 2211 "case 1:\n" 2212 " f();\n" 2213 " break;\n" 2214 "case kFoo:\n" 2215 "case ns::kBar:\n" 2216 "case kBaz:\n" 2217 " break;\n" 2218 "default:\n" 2219 " g();\n" 2220 " break;\n" 2221 "}"); 2222 verifyFormat("switch (x) {\n" 2223 "case 1: {\n" 2224 " f();\n" 2225 " break;\n" 2226 "}\n" 2227 "case 2: {\n" 2228 " break;\n" 2229 "}\n" 2230 "}"); 2231 verifyFormat("switch (x) {\n" 2232 "case 1: {\n" 2233 " f();\n" 2234 " {\n" 2235 " g();\n" 2236 " h();\n" 2237 " }\n" 2238 " break;\n" 2239 "}\n" 2240 "}"); 2241 verifyFormat("switch (x) {\n" 2242 "case 1: {\n" 2243 " f();\n" 2244 " if (foo) {\n" 2245 " g();\n" 2246 " h();\n" 2247 " }\n" 2248 " break;\n" 2249 "}\n" 2250 "}"); 2251 verifyFormat("switch (x) {\n" 2252 "case 1: {\n" 2253 " f();\n" 2254 " g();\n" 2255 "} break;\n" 2256 "}"); 2257 verifyFormat("switch (test)\n" 2258 " ;"); 2259 verifyFormat("switch (x) {\n" 2260 "default: {\n" 2261 " // Do nothing.\n" 2262 "}\n" 2263 "}"); 2264 verifyFormat("switch (x) {\n" 2265 "// comment\n" 2266 "// if 1, do f()\n" 2267 "case 1:\n" 2268 " f();\n" 2269 "}"); 2270 verifyFormat("switch (x) {\n" 2271 "case 1:\n" 2272 " // Do amazing stuff\n" 2273 " {\n" 2274 " f();\n" 2275 " g();\n" 2276 " }\n" 2277 " break;\n" 2278 "}"); 2279 verifyFormat("#define A \\\n" 2280 " switch (x) { \\\n" 2281 " case a: \\\n" 2282 " foo = b; \\\n" 2283 " }", 2284 getLLVMStyleWithColumns(20)); 2285 verifyFormat("#define OPERATION_CASE(name) \\\n" 2286 " case OP_name: \\\n" 2287 " return operations::Operation##name\n", 2288 getLLVMStyleWithColumns(40)); 2289 verifyFormat("switch (x) {\n" 2290 "case 1:;\n" 2291 "default:;\n" 2292 " int i;\n" 2293 "}"); 2294 2295 verifyGoogleFormat("switch (x) {\n" 2296 " case 1:\n" 2297 " f();\n" 2298 " break;\n" 2299 " case kFoo:\n" 2300 " case ns::kBar:\n" 2301 " case kBaz:\n" 2302 " break;\n" 2303 " default:\n" 2304 " g();\n" 2305 " break;\n" 2306 "}"); 2307 verifyGoogleFormat("switch (x) {\n" 2308 " case 1: {\n" 2309 " f();\n" 2310 " break;\n" 2311 " }\n" 2312 "}"); 2313 verifyGoogleFormat("switch (test)\n" 2314 " ;"); 2315 2316 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n" 2317 " case OP_name: \\\n" 2318 " return operations::Operation##name\n"); 2319 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n" 2320 " // Get the correction operation class.\n" 2321 " switch (OpCode) {\n" 2322 " CASE(Add);\n" 2323 " CASE(Subtract);\n" 2324 " default:\n" 2325 " return operations::Unknown;\n" 2326 " }\n" 2327 "#undef OPERATION_CASE\n" 2328 "}"); 2329 verifyFormat("DEBUG({\n" 2330 " switch (x) {\n" 2331 " case A:\n" 2332 " f();\n" 2333 " break;\n" 2334 " // fallthrough\n" 2335 " case B:\n" 2336 " g();\n" 2337 " break;\n" 2338 " }\n" 2339 "});"); 2340 EXPECT_EQ("DEBUG({\n" 2341 " switch (x) {\n" 2342 " case A:\n" 2343 " f();\n" 2344 " break;\n" 2345 " // On B:\n" 2346 " case B:\n" 2347 " g();\n" 2348 " break;\n" 2349 " }\n" 2350 "});", 2351 format("DEBUG({\n" 2352 " switch (x) {\n" 2353 " case A:\n" 2354 " f();\n" 2355 " break;\n" 2356 " // On B:\n" 2357 " case B:\n" 2358 " g();\n" 2359 " break;\n" 2360 " }\n" 2361 "});", 2362 getLLVMStyle())); 2363 EXPECT_EQ("switch (n) {\n" 2364 "case 0: {\n" 2365 " return false;\n" 2366 "}\n" 2367 "default: {\n" 2368 " return true;\n" 2369 "}\n" 2370 "}", 2371 format("switch (n)\n" 2372 "{\n" 2373 "case 0: {\n" 2374 " return false;\n" 2375 "}\n" 2376 "default: {\n" 2377 " return true;\n" 2378 "}\n" 2379 "}", 2380 getLLVMStyle())); 2381 verifyFormat("switch (a) {\n" 2382 "case (b):\n" 2383 " return;\n" 2384 "}"); 2385 2386 verifyFormat("switch (a) {\n" 2387 "case some_namespace::\n" 2388 " some_constant:\n" 2389 " return;\n" 2390 "}", 2391 getLLVMStyleWithColumns(34)); 2392 2393 FormatStyle Style = getLLVMStyle(); 2394 Style.IndentCaseLabels = true; 2395 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 2396 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2397 Style.BraceWrapping.AfterCaseLabel = true; 2398 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 2399 EXPECT_EQ("switch (n)\n" 2400 "{\n" 2401 " case 0:\n" 2402 " {\n" 2403 " return false;\n" 2404 " }\n" 2405 " default:\n" 2406 " {\n" 2407 " return true;\n" 2408 " }\n" 2409 "}", 2410 format("switch (n) {\n" 2411 " case 0: {\n" 2412 " return false;\n" 2413 " }\n" 2414 " default: {\n" 2415 " return true;\n" 2416 " }\n" 2417 "}", 2418 Style)); 2419 Style.BraceWrapping.AfterCaseLabel = false; 2420 EXPECT_EQ("switch (n)\n" 2421 "{\n" 2422 " case 0: {\n" 2423 " return false;\n" 2424 " }\n" 2425 " default: {\n" 2426 " return true;\n" 2427 " }\n" 2428 "}", 2429 format("switch (n) {\n" 2430 " case 0:\n" 2431 " {\n" 2432 " return false;\n" 2433 " }\n" 2434 " default:\n" 2435 " {\n" 2436 " return true;\n" 2437 " }\n" 2438 "}", 2439 Style)); 2440 Style.IndentCaseLabels = false; 2441 Style.IndentCaseBlocks = true; 2442 EXPECT_EQ("switch (n)\n" 2443 "{\n" 2444 "case 0:\n" 2445 " {\n" 2446 " return false;\n" 2447 " }\n" 2448 "case 1:\n" 2449 " break;\n" 2450 "default:\n" 2451 " {\n" 2452 " return true;\n" 2453 " }\n" 2454 "}", 2455 format("switch (n) {\n" 2456 "case 0: {\n" 2457 " return false;\n" 2458 "}\n" 2459 "case 1:\n" 2460 " break;\n" 2461 "default: {\n" 2462 " return true;\n" 2463 "}\n" 2464 "}", 2465 Style)); 2466 Style.IndentCaseLabels = true; 2467 Style.IndentCaseBlocks = true; 2468 EXPECT_EQ("switch (n)\n" 2469 "{\n" 2470 " case 0:\n" 2471 " {\n" 2472 " return false;\n" 2473 " }\n" 2474 " case 1:\n" 2475 " break;\n" 2476 " default:\n" 2477 " {\n" 2478 " return true;\n" 2479 " }\n" 2480 "}", 2481 format("switch (n) {\n" 2482 "case 0: {\n" 2483 " return false;\n" 2484 "}\n" 2485 "case 1:\n" 2486 " break;\n" 2487 "default: {\n" 2488 " return true;\n" 2489 "}\n" 2490 "}", 2491 Style)); 2492 } 2493 2494 TEST_F(FormatTest, CaseRanges) { 2495 verifyFormat("switch (x) {\n" 2496 "case 'A' ... 'Z':\n" 2497 "case 1 ... 5:\n" 2498 "case a ... b:\n" 2499 " break;\n" 2500 "}"); 2501 } 2502 2503 TEST_F(FormatTest, ShortEnums) { 2504 FormatStyle Style = getLLVMStyle(); 2505 Style.AllowShortEnumsOnASingleLine = true; 2506 verifyFormat("enum { A, B, C } ShortEnum1, ShortEnum2;", Style); 2507 verifyFormat("typedef enum { A, B, C } ShortEnum1, ShortEnum2;", Style); 2508 Style.AllowShortEnumsOnASingleLine = false; 2509 verifyFormat("enum {\n" 2510 " A,\n" 2511 " B,\n" 2512 " C\n" 2513 "} ShortEnum1, ShortEnum2;", 2514 Style); 2515 verifyFormat("typedef enum {\n" 2516 " A,\n" 2517 " B,\n" 2518 " C\n" 2519 "} ShortEnum1, ShortEnum2;", 2520 Style); 2521 verifyFormat("enum {\n" 2522 " A,\n" 2523 "} ShortEnum1, ShortEnum2;", 2524 Style); 2525 verifyFormat("typedef enum {\n" 2526 " A,\n" 2527 "} ShortEnum1, ShortEnum2;", 2528 Style); 2529 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2530 Style.BraceWrapping.AfterEnum = true; 2531 verifyFormat("enum\n" 2532 "{\n" 2533 " A,\n" 2534 " B,\n" 2535 " C\n" 2536 "} ShortEnum1, ShortEnum2;", 2537 Style); 2538 verifyFormat("typedef enum\n" 2539 "{\n" 2540 " A,\n" 2541 " B,\n" 2542 " C\n" 2543 "} ShortEnum1, ShortEnum2;", 2544 Style); 2545 } 2546 2547 TEST_F(FormatTest, ShortCaseLabels) { 2548 FormatStyle Style = getLLVMStyle(); 2549 Style.AllowShortCaseLabelsOnASingleLine = true; 2550 verifyFormat("switch (a) {\n" 2551 "case 1: x = 1; break;\n" 2552 "case 2: return;\n" 2553 "case 3:\n" 2554 "case 4:\n" 2555 "case 5: return;\n" 2556 "case 6: // comment\n" 2557 " return;\n" 2558 "case 7:\n" 2559 " // comment\n" 2560 " return;\n" 2561 "case 8:\n" 2562 " x = 8; // comment\n" 2563 " break;\n" 2564 "default: y = 1; break;\n" 2565 "}", 2566 Style); 2567 verifyFormat("switch (a) {\n" 2568 "case 0: return; // comment\n" 2569 "case 1: break; // comment\n" 2570 "case 2: return;\n" 2571 "// comment\n" 2572 "case 3: return;\n" 2573 "// comment 1\n" 2574 "// comment 2\n" 2575 "// comment 3\n" 2576 "case 4: break; /* comment */\n" 2577 "case 5:\n" 2578 " // comment\n" 2579 " break;\n" 2580 "case 6: /* comment */ x = 1; break;\n" 2581 "case 7: x = /* comment */ 1; break;\n" 2582 "case 8:\n" 2583 " x = 1; /* comment */\n" 2584 " break;\n" 2585 "case 9:\n" 2586 " break; // comment line 1\n" 2587 " // comment line 2\n" 2588 "}", 2589 Style); 2590 EXPECT_EQ("switch (a) {\n" 2591 "case 1:\n" 2592 " x = 8;\n" 2593 " // fall through\n" 2594 "case 2: x = 8;\n" 2595 "// comment\n" 2596 "case 3:\n" 2597 " return; /* comment line 1\n" 2598 " * comment line 2 */\n" 2599 "case 4: i = 8;\n" 2600 "// something else\n" 2601 "#if FOO\n" 2602 "case 5: break;\n" 2603 "#endif\n" 2604 "}", 2605 format("switch (a) {\n" 2606 "case 1: x = 8;\n" 2607 " // fall through\n" 2608 "case 2:\n" 2609 " x = 8;\n" 2610 "// comment\n" 2611 "case 3:\n" 2612 " return; /* comment line 1\n" 2613 " * comment line 2 */\n" 2614 "case 4:\n" 2615 " i = 8;\n" 2616 "// something else\n" 2617 "#if FOO\n" 2618 "case 5: break;\n" 2619 "#endif\n" 2620 "}", 2621 Style)); 2622 EXPECT_EQ("switch (a) {\n" 2623 "case 0:\n" 2624 " return; // long long long long long long long long long long " 2625 "long long comment\n" 2626 " // line\n" 2627 "}", 2628 format("switch (a) {\n" 2629 "case 0: return; // long long long long long long long long " 2630 "long long long long comment line\n" 2631 "}", 2632 Style)); 2633 EXPECT_EQ("switch (a) {\n" 2634 "case 0:\n" 2635 " return; /* long long long long long long long long long long " 2636 "long long comment\n" 2637 " line */\n" 2638 "}", 2639 format("switch (a) {\n" 2640 "case 0: return; /* long long long long long long long long " 2641 "long long long long comment line */\n" 2642 "}", 2643 Style)); 2644 verifyFormat("switch (a) {\n" 2645 "#if FOO\n" 2646 "case 0: return 0;\n" 2647 "#endif\n" 2648 "}", 2649 Style); 2650 verifyFormat("switch (a) {\n" 2651 "case 1: {\n" 2652 "}\n" 2653 "case 2: {\n" 2654 " return;\n" 2655 "}\n" 2656 "case 3: {\n" 2657 " x = 1;\n" 2658 " return;\n" 2659 "}\n" 2660 "case 4:\n" 2661 " if (x)\n" 2662 " return;\n" 2663 "}", 2664 Style); 2665 Style.ColumnLimit = 21; 2666 verifyFormat("switch (a) {\n" 2667 "case 1: x = 1; break;\n" 2668 "case 2: return;\n" 2669 "case 3:\n" 2670 "case 4:\n" 2671 "case 5: return;\n" 2672 "default:\n" 2673 " y = 1;\n" 2674 " break;\n" 2675 "}", 2676 Style); 2677 Style.ColumnLimit = 80; 2678 Style.AllowShortCaseLabelsOnASingleLine = false; 2679 Style.IndentCaseLabels = true; 2680 EXPECT_EQ("switch (n) {\n" 2681 " default /*comments*/:\n" 2682 " return true;\n" 2683 " case 0:\n" 2684 " return false;\n" 2685 "}", 2686 format("switch (n) {\n" 2687 "default/*comments*/:\n" 2688 " return true;\n" 2689 "case 0:\n" 2690 " return false;\n" 2691 "}", 2692 Style)); 2693 Style.AllowShortCaseLabelsOnASingleLine = true; 2694 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2695 Style.BraceWrapping.AfterCaseLabel = true; 2696 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 2697 EXPECT_EQ("switch (n)\n" 2698 "{\n" 2699 " case 0:\n" 2700 " {\n" 2701 " return false;\n" 2702 " }\n" 2703 " default:\n" 2704 " {\n" 2705 " return true;\n" 2706 " }\n" 2707 "}", 2708 format("switch (n) {\n" 2709 " case 0: {\n" 2710 " return false;\n" 2711 " }\n" 2712 " default:\n" 2713 " {\n" 2714 " return true;\n" 2715 " }\n" 2716 "}", 2717 Style)); 2718 } 2719 2720 TEST_F(FormatTest, FormatsLabels) { 2721 verifyFormat("void f() {\n" 2722 " some_code();\n" 2723 "test_label:\n" 2724 " some_other_code();\n" 2725 " {\n" 2726 " some_more_code();\n" 2727 " another_label:\n" 2728 " some_more_code();\n" 2729 " }\n" 2730 "}"); 2731 verifyFormat("{\n" 2732 " some_code();\n" 2733 "test_label:\n" 2734 " some_other_code();\n" 2735 "}"); 2736 verifyFormat("{\n" 2737 " some_code();\n" 2738 "test_label:;\n" 2739 " int i = 0;\n" 2740 "}"); 2741 FormatStyle Style = getLLVMStyle(); 2742 Style.IndentGotoLabels = false; 2743 verifyFormat("void f() {\n" 2744 " some_code();\n" 2745 "test_label:\n" 2746 " some_other_code();\n" 2747 " {\n" 2748 " some_more_code();\n" 2749 "another_label:\n" 2750 " some_more_code();\n" 2751 " }\n" 2752 "}", 2753 Style); 2754 verifyFormat("{\n" 2755 " some_code();\n" 2756 "test_label:\n" 2757 " some_other_code();\n" 2758 "}", 2759 Style); 2760 verifyFormat("{\n" 2761 " some_code();\n" 2762 "test_label:;\n" 2763 " int i = 0;\n" 2764 "}"); 2765 } 2766 2767 TEST_F(FormatTest, MultiLineControlStatements) { 2768 FormatStyle Style = getLLVMStyleWithColumns(20); 2769 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom; 2770 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine; 2771 // Short lines should keep opening brace on same line. 2772 EXPECT_EQ("if (foo) {\n" 2773 " bar();\n" 2774 "}", 2775 format("if(foo){bar();}", Style)); 2776 EXPECT_EQ("if (foo) {\n" 2777 " bar();\n" 2778 "} else {\n" 2779 " baz();\n" 2780 "}", 2781 format("if(foo){bar();}else{baz();}", Style)); 2782 EXPECT_EQ("if (foo && bar) {\n" 2783 " baz();\n" 2784 "}", 2785 format("if(foo&&bar){baz();}", Style)); 2786 EXPECT_EQ("if (foo) {\n" 2787 " bar();\n" 2788 "} else if (baz) {\n" 2789 " quux();\n" 2790 "}", 2791 format("if(foo){bar();}else if(baz){quux();}", Style)); 2792 EXPECT_EQ( 2793 "if (foo) {\n" 2794 " bar();\n" 2795 "} else if (baz) {\n" 2796 " quux();\n" 2797 "} else {\n" 2798 " foobar();\n" 2799 "}", 2800 format("if(foo){bar();}else if(baz){quux();}else{foobar();}", Style)); 2801 EXPECT_EQ("for (;;) {\n" 2802 " foo();\n" 2803 "}", 2804 format("for(;;){foo();}")); 2805 EXPECT_EQ("while (1) {\n" 2806 " foo();\n" 2807 "}", 2808 format("while(1){foo();}", Style)); 2809 EXPECT_EQ("switch (foo) {\n" 2810 "case bar:\n" 2811 " return;\n" 2812 "}", 2813 format("switch(foo){case bar:return;}", Style)); 2814 EXPECT_EQ("try {\n" 2815 " foo();\n" 2816 "} catch (...) {\n" 2817 " bar();\n" 2818 "}", 2819 format("try{foo();}catch(...){bar();}", Style)); 2820 EXPECT_EQ("do {\n" 2821 " foo();\n" 2822 "} while (bar &&\n" 2823 " baz);", 2824 format("do{foo();}while(bar&&baz);", Style)); 2825 // Long lines should put opening brace on new line. 2826 EXPECT_EQ("if (foo && bar &&\n" 2827 " baz)\n" 2828 "{\n" 2829 " quux();\n" 2830 "}", 2831 format("if(foo&&bar&&baz){quux();}", Style)); 2832 EXPECT_EQ("if (foo && bar &&\n" 2833 " baz)\n" 2834 "{\n" 2835 " quux();\n" 2836 "}", 2837 format("if (foo && bar &&\n" 2838 " baz) {\n" 2839 " quux();\n" 2840 "}", 2841 Style)); 2842 EXPECT_EQ("if (foo) {\n" 2843 " bar();\n" 2844 "} else if (baz ||\n" 2845 " quux)\n" 2846 "{\n" 2847 " foobar();\n" 2848 "}", 2849 format("if(foo){bar();}else if(baz||quux){foobar();}", Style)); 2850 EXPECT_EQ( 2851 "if (foo) {\n" 2852 " bar();\n" 2853 "} else if (baz ||\n" 2854 " quux)\n" 2855 "{\n" 2856 " foobar();\n" 2857 "} else {\n" 2858 " barbaz();\n" 2859 "}", 2860 format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}", 2861 Style)); 2862 EXPECT_EQ("for (int i = 0;\n" 2863 " i < 10; ++i)\n" 2864 "{\n" 2865 " foo();\n" 2866 "}", 2867 format("for(int i=0;i<10;++i){foo();}", Style)); 2868 EXPECT_EQ("foreach (int i,\n" 2869 " list)\n" 2870 "{\n" 2871 " foo();\n" 2872 "}", 2873 format("foreach(int i, list){foo();}", Style)); 2874 Style.ColumnLimit = 2875 40; // to concentrate at brace wrapping, not line wrap due to column limit 2876 EXPECT_EQ("foreach (int i, list) {\n" 2877 " foo();\n" 2878 "}", 2879 format("foreach(int i, list){foo();}", Style)); 2880 Style.ColumnLimit = 2881 20; // to concentrate at brace wrapping, not line wrap due to column limit 2882 EXPECT_EQ("while (foo || bar ||\n" 2883 " baz)\n" 2884 "{\n" 2885 " quux();\n" 2886 "}", 2887 format("while(foo||bar||baz){quux();}", Style)); 2888 EXPECT_EQ("switch (\n" 2889 " foo = barbaz)\n" 2890 "{\n" 2891 "case quux:\n" 2892 " return;\n" 2893 "}", 2894 format("switch(foo=barbaz){case quux:return;}", Style)); 2895 EXPECT_EQ("try {\n" 2896 " foo();\n" 2897 "} catch (\n" 2898 " Exception &bar)\n" 2899 "{\n" 2900 " baz();\n" 2901 "}", 2902 format("try{foo();}catch(Exception&bar){baz();}", Style)); 2903 Style.ColumnLimit = 2904 40; // to concentrate at brace wrapping, not line wrap due to column limit 2905 EXPECT_EQ("try {\n" 2906 " foo();\n" 2907 "} catch (Exception &bar) {\n" 2908 " baz();\n" 2909 "}", 2910 format("try{foo();}catch(Exception&bar){baz();}", Style)); 2911 Style.ColumnLimit = 2912 20; // to concentrate at brace wrapping, not line wrap due to column limit 2913 2914 Style.BraceWrapping.BeforeElse = true; 2915 EXPECT_EQ( 2916 "if (foo) {\n" 2917 " bar();\n" 2918 "}\n" 2919 "else if (baz ||\n" 2920 " quux)\n" 2921 "{\n" 2922 " foobar();\n" 2923 "}\n" 2924 "else {\n" 2925 " barbaz();\n" 2926 "}", 2927 format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}", 2928 Style)); 2929 2930 Style.BraceWrapping.BeforeCatch = true; 2931 EXPECT_EQ("try {\n" 2932 " foo();\n" 2933 "}\n" 2934 "catch (...) {\n" 2935 " baz();\n" 2936 "}", 2937 format("try{foo();}catch(...){baz();}", Style)); 2938 2939 Style.BraceWrapping.AfterFunction = true; 2940 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine; 2941 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 2942 Style.ColumnLimit = 80; 2943 verifyFormat("void shortfunction() { bar(); }", Style); 2944 2945 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 2946 verifyFormat("void shortfunction()\n" 2947 "{\n" 2948 " bar();\n" 2949 "}", 2950 Style); 2951 } 2952 2953 TEST_F(FormatTest, BeforeWhile) { 2954 FormatStyle Style = getLLVMStyle(); 2955 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom; 2956 2957 verifyFormat("do {\n" 2958 " foo();\n" 2959 "} while (1);", 2960 Style); 2961 Style.BraceWrapping.BeforeWhile = true; 2962 verifyFormat("do {\n" 2963 " foo();\n" 2964 "}\n" 2965 "while (1);", 2966 Style); 2967 } 2968 2969 //===----------------------------------------------------------------------===// 2970 // Tests for classes, namespaces, etc. 2971 //===----------------------------------------------------------------------===// 2972 2973 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 2974 verifyFormat("class A {};"); 2975 } 2976 2977 TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 2978 verifyFormat("class A {\n" 2979 "public:\n" 2980 "public: // comment\n" 2981 "protected:\n" 2982 "private:\n" 2983 " void f() {}\n" 2984 "};"); 2985 verifyFormat("export class A {\n" 2986 "public:\n" 2987 "public: // comment\n" 2988 "protected:\n" 2989 "private:\n" 2990 " void f() {}\n" 2991 "};"); 2992 verifyGoogleFormat("class A {\n" 2993 " public:\n" 2994 " protected:\n" 2995 " private:\n" 2996 " void f() {}\n" 2997 "};"); 2998 verifyGoogleFormat("export class A {\n" 2999 " public:\n" 3000 " protected:\n" 3001 " private:\n" 3002 " void f() {}\n" 3003 "};"); 3004 verifyFormat("class A {\n" 3005 "public slots:\n" 3006 " void f1() {}\n" 3007 "public Q_SLOTS:\n" 3008 " void f2() {}\n" 3009 "protected slots:\n" 3010 " void f3() {}\n" 3011 "protected Q_SLOTS:\n" 3012 " void f4() {}\n" 3013 "private slots:\n" 3014 " void f5() {}\n" 3015 "private Q_SLOTS:\n" 3016 " void f6() {}\n" 3017 "signals:\n" 3018 " void g1();\n" 3019 "Q_SIGNALS:\n" 3020 " void g2();\n" 3021 "};"); 3022 3023 // Don't interpret 'signals' the wrong way. 3024 verifyFormat("signals.set();"); 3025 verifyFormat("for (Signals signals : f()) {\n}"); 3026 verifyFormat("{\n" 3027 " signals.set(); // This needs indentation.\n" 3028 "}"); 3029 verifyFormat("void f() {\n" 3030 "label:\n" 3031 " signals.baz();\n" 3032 "}"); 3033 } 3034 3035 TEST_F(FormatTest, SeparatesLogicalBlocks) { 3036 EXPECT_EQ("class A {\n" 3037 "public:\n" 3038 " void f();\n" 3039 "\n" 3040 "private:\n" 3041 " void g() {}\n" 3042 " // test\n" 3043 "protected:\n" 3044 " int h;\n" 3045 "};", 3046 format("class A {\n" 3047 "public:\n" 3048 "void f();\n" 3049 "private:\n" 3050 "void g() {}\n" 3051 "// test\n" 3052 "protected:\n" 3053 "int h;\n" 3054 "};")); 3055 EXPECT_EQ("class A {\n" 3056 "protected:\n" 3057 "public:\n" 3058 " void f();\n" 3059 "};", 3060 format("class A {\n" 3061 "protected:\n" 3062 "\n" 3063 "public:\n" 3064 "\n" 3065 " void f();\n" 3066 "};")); 3067 3068 // Even ensure proper spacing inside macros. 3069 EXPECT_EQ("#define B \\\n" 3070 " class A { \\\n" 3071 " protected: \\\n" 3072 " public: \\\n" 3073 " void f(); \\\n" 3074 " };", 3075 format("#define B \\\n" 3076 " class A { \\\n" 3077 " protected: \\\n" 3078 " \\\n" 3079 " public: \\\n" 3080 " \\\n" 3081 " void f(); \\\n" 3082 " };", 3083 getGoogleStyle())); 3084 // But don't remove empty lines after macros ending in access specifiers. 3085 EXPECT_EQ("#define A private:\n" 3086 "\n" 3087 "int i;", 3088 format("#define A private:\n" 3089 "\n" 3090 "int i;")); 3091 } 3092 3093 TEST_F(FormatTest, FormatsClasses) { 3094 verifyFormat("class A : public B {};"); 3095 verifyFormat("class A : public ::B {};"); 3096 3097 verifyFormat( 3098 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3099 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 3100 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 3101 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3102 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 3103 verifyFormat( 3104 "class A : public B, public C, public D, public E, public F {};"); 3105 verifyFormat("class AAAAAAAAAAAA : public B,\n" 3106 " public C,\n" 3107 " public D,\n" 3108 " public E,\n" 3109 " public F,\n" 3110 " public G {};"); 3111 3112 verifyFormat("class\n" 3113 " ReallyReallyLongClassName {\n" 3114 " int i;\n" 3115 "};", 3116 getLLVMStyleWithColumns(32)); 3117 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 3118 " aaaaaaaaaaaaaaaa> {};"); 3119 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" 3120 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n" 3121 " aaaaaaaaaaaaaaaaaaaaaa> {};"); 3122 verifyFormat("template <class R, class C>\n" 3123 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n" 3124 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};"); 3125 verifyFormat("class ::A::B {};"); 3126 } 3127 3128 TEST_F(FormatTest, BreakInheritanceStyle) { 3129 FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle(); 3130 StyleWithInheritanceBreakBeforeComma.BreakInheritanceList = 3131 FormatStyle::BILS_BeforeComma; 3132 verifyFormat("class MyClass : public X {};", 3133 StyleWithInheritanceBreakBeforeComma); 3134 verifyFormat("class MyClass\n" 3135 " : public X\n" 3136 " , public Y {};", 3137 StyleWithInheritanceBreakBeforeComma); 3138 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n" 3139 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n" 3140 " , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", 3141 StyleWithInheritanceBreakBeforeComma); 3142 verifyFormat("struct aaaaaaaaaaaaa\n" 3143 " : public aaaaaaaaaaaaaaaaaaa< // break\n" 3144 " aaaaaaaaaaaaaaaa> {};", 3145 StyleWithInheritanceBreakBeforeComma); 3146 3147 FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle(); 3148 StyleWithInheritanceBreakAfterColon.BreakInheritanceList = 3149 FormatStyle::BILS_AfterColon; 3150 verifyFormat("class MyClass : public X {};", 3151 StyleWithInheritanceBreakAfterColon); 3152 verifyFormat("class MyClass : public X, public Y {};", 3153 StyleWithInheritanceBreakAfterColon); 3154 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n" 3155 " public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3156 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", 3157 StyleWithInheritanceBreakAfterColon); 3158 verifyFormat("struct aaaaaaaaaaaaa :\n" 3159 " public aaaaaaaaaaaaaaaaaaa< // break\n" 3160 " aaaaaaaaaaaaaaaa> {};", 3161 StyleWithInheritanceBreakAfterColon); 3162 3163 FormatStyle StyleWithInheritanceBreakAfterComma = getLLVMStyle(); 3164 StyleWithInheritanceBreakAfterComma.BreakInheritanceList = 3165 FormatStyle::BILS_AfterComma; 3166 verifyFormat("class MyClass : public X {};", 3167 StyleWithInheritanceBreakAfterComma); 3168 verifyFormat("class MyClass : public X,\n" 3169 " public Y {};", 3170 StyleWithInheritanceBreakAfterComma); 3171 verifyFormat( 3172 "class AAAAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 3173 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC " 3174 "{};", 3175 StyleWithInheritanceBreakAfterComma); 3176 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 3177 " aaaaaaaaaaaaaaaa> {};", 3178 StyleWithInheritanceBreakAfterComma); 3179 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 3180 " : public OnceBreak,\n" 3181 " public AlwaysBreak,\n" 3182 " EvenBasesFitInOneLine {};", 3183 StyleWithInheritanceBreakAfterComma); 3184 } 3185 3186 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { 3187 verifyFormat("class A {\n} a, b;"); 3188 verifyFormat("struct A {\n} a, b;"); 3189 verifyFormat("union A {\n} a;"); 3190 } 3191 3192 TEST_F(FormatTest, FormatsEnum) { 3193 verifyFormat("enum {\n" 3194 " Zero,\n" 3195 " One = 1,\n" 3196 " Two = One + 1,\n" 3197 " Three = (One + Two),\n" 3198 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3199 " Five = (One, Two, Three, Four, 5)\n" 3200 "};"); 3201 verifyGoogleFormat("enum {\n" 3202 " Zero,\n" 3203 " One = 1,\n" 3204 " Two = One + 1,\n" 3205 " Three = (One + Two),\n" 3206 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3207 " Five = (One, Two, Three, Four, 5)\n" 3208 "};"); 3209 verifyFormat("enum Enum {};"); 3210 verifyFormat("enum {};"); 3211 verifyFormat("enum X E {} d;"); 3212 verifyFormat("enum __attribute__((...)) E {} d;"); 3213 verifyFormat("enum __declspec__((...)) E {} d;"); 3214 verifyFormat("enum {\n" 3215 " Bar = Foo<int, int>::value\n" 3216 "};", 3217 getLLVMStyleWithColumns(30)); 3218 3219 verifyFormat("enum ShortEnum { A, B, C };"); 3220 verifyGoogleFormat("enum ShortEnum { A, B, C };"); 3221 3222 EXPECT_EQ("enum KeepEmptyLines {\n" 3223 " ONE,\n" 3224 "\n" 3225 " TWO,\n" 3226 "\n" 3227 " THREE\n" 3228 "}", 3229 format("enum KeepEmptyLines {\n" 3230 " ONE,\n" 3231 "\n" 3232 " TWO,\n" 3233 "\n" 3234 "\n" 3235 " THREE\n" 3236 "}")); 3237 verifyFormat("enum E { // comment\n" 3238 " ONE,\n" 3239 " TWO\n" 3240 "};\n" 3241 "int i;"); 3242 3243 FormatStyle EightIndent = getLLVMStyle(); 3244 EightIndent.IndentWidth = 8; 3245 verifyFormat("enum {\n" 3246 " VOID,\n" 3247 " CHAR,\n" 3248 " SHORT,\n" 3249 " INT,\n" 3250 " LONG,\n" 3251 " SIGNED,\n" 3252 " UNSIGNED,\n" 3253 " BOOL,\n" 3254 " FLOAT,\n" 3255 " DOUBLE,\n" 3256 " COMPLEX\n" 3257 "};", 3258 EightIndent); 3259 3260 // Not enums. 3261 verifyFormat("enum X f() {\n" 3262 " a();\n" 3263 " return 42;\n" 3264 "}"); 3265 verifyFormat("enum X Type::f() {\n" 3266 " a();\n" 3267 " return 42;\n" 3268 "}"); 3269 verifyFormat("enum ::X f() {\n" 3270 " a();\n" 3271 " return 42;\n" 3272 "}"); 3273 verifyFormat("enum ns::X f() {\n" 3274 " a();\n" 3275 " return 42;\n" 3276 "}"); 3277 } 3278 3279 TEST_F(FormatTest, FormatsEnumsWithErrors) { 3280 verifyFormat("enum Type {\n" 3281 " One = 0; // These semicolons should be commas.\n" 3282 " Two = 1;\n" 3283 "};"); 3284 verifyFormat("namespace n {\n" 3285 "enum Type {\n" 3286 " One,\n" 3287 " Two, // missing };\n" 3288 " int i;\n" 3289 "}\n" 3290 "void g() {}"); 3291 } 3292 3293 TEST_F(FormatTest, FormatsEnumStruct) { 3294 verifyFormat("enum struct {\n" 3295 " Zero,\n" 3296 " One = 1,\n" 3297 " Two = One + 1,\n" 3298 " Three = (One + Two),\n" 3299 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3300 " Five = (One, Two, Three, Four, 5)\n" 3301 "};"); 3302 verifyFormat("enum struct Enum {};"); 3303 verifyFormat("enum struct {};"); 3304 verifyFormat("enum struct X E {} d;"); 3305 verifyFormat("enum struct __attribute__((...)) E {} d;"); 3306 verifyFormat("enum struct __declspec__((...)) E {} d;"); 3307 verifyFormat("enum struct X f() {\n a();\n return 42;\n}"); 3308 } 3309 3310 TEST_F(FormatTest, FormatsEnumClass) { 3311 verifyFormat("enum class {\n" 3312 " Zero,\n" 3313 " One = 1,\n" 3314 " Two = One + 1,\n" 3315 " Three = (One + Two),\n" 3316 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 3317 " Five = (One, Two, Three, Four, 5)\n" 3318 "};"); 3319 verifyFormat("enum class Enum {};"); 3320 verifyFormat("enum class {};"); 3321 verifyFormat("enum class X E {} d;"); 3322 verifyFormat("enum class __attribute__((...)) E {} d;"); 3323 verifyFormat("enum class __declspec__((...)) E {} d;"); 3324 verifyFormat("enum class X f() {\n a();\n return 42;\n}"); 3325 } 3326 3327 TEST_F(FormatTest, FormatsEnumTypes) { 3328 verifyFormat("enum X : int {\n" 3329 " A, // Force multiple lines.\n" 3330 " B\n" 3331 "};"); 3332 verifyFormat("enum X : int { A, B };"); 3333 verifyFormat("enum X : std::uint32_t { A, B };"); 3334 } 3335 3336 TEST_F(FormatTest, FormatsTypedefEnum) { 3337 FormatStyle Style = getLLVMStyleWithColumns(40); 3338 verifyFormat("typedef enum {} EmptyEnum;"); 3339 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 3340 verifyFormat("typedef enum {\n" 3341 " ZERO = 0,\n" 3342 " ONE = 1,\n" 3343 " TWO = 2,\n" 3344 " THREE = 3\n" 3345 "} LongEnum;", 3346 Style); 3347 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3348 Style.BraceWrapping.AfterEnum = true; 3349 verifyFormat("typedef enum {} EmptyEnum;"); 3350 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 3351 verifyFormat("typedef enum\n" 3352 "{\n" 3353 " ZERO = 0,\n" 3354 " ONE = 1,\n" 3355 " TWO = 2,\n" 3356 " THREE = 3\n" 3357 "} LongEnum;", 3358 Style); 3359 } 3360 3361 TEST_F(FormatTest, FormatsNSEnums) { 3362 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }"); 3363 verifyGoogleFormat( 3364 "typedef NS_CLOSED_ENUM(NSInteger, SomeName) { AAA, BBB }"); 3365 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n" 3366 " // Information about someDecentlyLongValue.\n" 3367 " someDecentlyLongValue,\n" 3368 " // Information about anotherDecentlyLongValue.\n" 3369 " anotherDecentlyLongValue,\n" 3370 " // Information about aThirdDecentlyLongValue.\n" 3371 " aThirdDecentlyLongValue\n" 3372 "};"); 3373 verifyGoogleFormat("typedef NS_CLOSED_ENUM(NSInteger, MyType) {\n" 3374 " // Information about someDecentlyLongValue.\n" 3375 " someDecentlyLongValue,\n" 3376 " // Information about anotherDecentlyLongValue.\n" 3377 " anotherDecentlyLongValue,\n" 3378 " // Information about aThirdDecentlyLongValue.\n" 3379 " aThirdDecentlyLongValue\n" 3380 "};"); 3381 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n" 3382 " a = 1,\n" 3383 " b = 2,\n" 3384 " c = 3,\n" 3385 "};"); 3386 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n" 3387 " a = 1,\n" 3388 " b = 2,\n" 3389 " c = 3,\n" 3390 "};"); 3391 verifyGoogleFormat("typedef CF_CLOSED_ENUM(NSInteger, MyType) {\n" 3392 " a = 1,\n" 3393 " b = 2,\n" 3394 " c = 3,\n" 3395 "};"); 3396 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n" 3397 " a = 1,\n" 3398 " b = 2,\n" 3399 " c = 3,\n" 3400 "};"); 3401 } 3402 3403 TEST_F(FormatTest, FormatsBitfields) { 3404 verifyFormat("struct Bitfields {\n" 3405 " unsigned sClass : 8;\n" 3406 " unsigned ValueKind : 2;\n" 3407 "};"); 3408 verifyFormat("struct A {\n" 3409 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" 3410 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" 3411 "};"); 3412 verifyFormat("struct MyStruct {\n" 3413 " uchar data;\n" 3414 " uchar : 8;\n" 3415 " uchar : 8;\n" 3416 " uchar other;\n" 3417 "};"); 3418 FormatStyle Style = getLLVMStyle(); 3419 Style.BitFieldColonSpacing = FormatStyle::BFCS_None; 3420 verifyFormat("struct Bitfields {\n" 3421 " unsigned sClass:8;\n" 3422 " unsigned ValueKind:2;\n" 3423 " uchar other;\n" 3424 "};", 3425 Style); 3426 verifyFormat("struct A {\n" 3427 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1,\n" 3428 " bbbbbbbbbbbbbbbbbbbbbbbbb:2;\n" 3429 "};", 3430 Style); 3431 Style.BitFieldColonSpacing = FormatStyle::BFCS_Before; 3432 verifyFormat("struct Bitfields {\n" 3433 " unsigned sClass :8;\n" 3434 " unsigned ValueKind :2;\n" 3435 " uchar other;\n" 3436 "};", 3437 Style); 3438 Style.BitFieldColonSpacing = FormatStyle::BFCS_After; 3439 verifyFormat("struct Bitfields {\n" 3440 " unsigned sClass: 8;\n" 3441 " unsigned ValueKind: 2;\n" 3442 " uchar other;\n" 3443 "};", 3444 Style); 3445 } 3446 3447 TEST_F(FormatTest, FormatsNamespaces) { 3448 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 3449 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 3450 3451 verifyFormat("namespace some_namespace {\n" 3452 "class A {};\n" 3453 "void f() { f(); }\n" 3454 "}", 3455 LLVMWithNoNamespaceFix); 3456 verifyFormat("namespace N::inline D {\n" 3457 "class A {};\n" 3458 "void f() { f(); }\n" 3459 "}", 3460 LLVMWithNoNamespaceFix); 3461 verifyFormat("namespace N::inline D::E {\n" 3462 "class A {};\n" 3463 "void f() { f(); }\n" 3464 "}", 3465 LLVMWithNoNamespaceFix); 3466 verifyFormat("namespace [[deprecated(\"foo[bar\")]] some_namespace {\n" 3467 "class A {};\n" 3468 "void f() { f(); }\n" 3469 "}", 3470 LLVMWithNoNamespaceFix); 3471 verifyFormat("/* something */ namespace some_namespace {\n" 3472 "class A {};\n" 3473 "void f() { f(); }\n" 3474 "}", 3475 LLVMWithNoNamespaceFix); 3476 verifyFormat("namespace {\n" 3477 "class A {};\n" 3478 "void f() { f(); }\n" 3479 "}", 3480 LLVMWithNoNamespaceFix); 3481 verifyFormat("/* something */ namespace {\n" 3482 "class A {};\n" 3483 "void f() { f(); }\n" 3484 "}", 3485 LLVMWithNoNamespaceFix); 3486 verifyFormat("inline namespace X {\n" 3487 "class A {};\n" 3488 "void f() { f(); }\n" 3489 "}", 3490 LLVMWithNoNamespaceFix); 3491 verifyFormat("/* something */ inline namespace X {\n" 3492 "class A {};\n" 3493 "void f() { f(); }\n" 3494 "}", 3495 LLVMWithNoNamespaceFix); 3496 verifyFormat("export namespace X {\n" 3497 "class A {};\n" 3498 "void f() { f(); }\n" 3499 "}", 3500 LLVMWithNoNamespaceFix); 3501 verifyFormat("using namespace some_namespace;\n" 3502 "class A {};\n" 3503 "void f() { f(); }", 3504 LLVMWithNoNamespaceFix); 3505 3506 // This code is more common than we thought; if we 3507 // layout this correctly the semicolon will go into 3508 // its own line, which is undesirable. 3509 verifyFormat("namespace {};", LLVMWithNoNamespaceFix); 3510 verifyFormat("namespace {\n" 3511 "class A {};\n" 3512 "};", 3513 LLVMWithNoNamespaceFix); 3514 3515 verifyFormat("namespace {\n" 3516 "int SomeVariable = 0; // comment\n" 3517 "} // namespace", 3518 LLVMWithNoNamespaceFix); 3519 EXPECT_EQ("#ifndef HEADER_GUARD\n" 3520 "#define HEADER_GUARD\n" 3521 "namespace my_namespace {\n" 3522 "int i;\n" 3523 "} // my_namespace\n" 3524 "#endif // HEADER_GUARD", 3525 format("#ifndef HEADER_GUARD\n" 3526 " #define HEADER_GUARD\n" 3527 " namespace my_namespace {\n" 3528 "int i;\n" 3529 "} // my_namespace\n" 3530 "#endif // HEADER_GUARD", 3531 LLVMWithNoNamespaceFix)); 3532 3533 EXPECT_EQ("namespace A::B {\n" 3534 "class C {};\n" 3535 "}", 3536 format("namespace A::B {\n" 3537 "class C {};\n" 3538 "}", 3539 LLVMWithNoNamespaceFix)); 3540 3541 FormatStyle Style = getLLVMStyle(); 3542 Style.NamespaceIndentation = FormatStyle::NI_All; 3543 EXPECT_EQ("namespace out {\n" 3544 " int i;\n" 3545 " namespace in {\n" 3546 " int i;\n" 3547 " } // namespace in\n" 3548 "} // namespace out", 3549 format("namespace out {\n" 3550 "int i;\n" 3551 "namespace in {\n" 3552 "int i;\n" 3553 "} // namespace in\n" 3554 "} // namespace out", 3555 Style)); 3556 3557 Style.NamespaceIndentation = FormatStyle::NI_Inner; 3558 EXPECT_EQ("namespace out {\n" 3559 "int i;\n" 3560 "namespace in {\n" 3561 " int i;\n" 3562 "} // namespace in\n" 3563 "} // namespace out", 3564 format("namespace out {\n" 3565 "int i;\n" 3566 "namespace in {\n" 3567 "int i;\n" 3568 "} // namespace in\n" 3569 "} // namespace out", 3570 Style)); 3571 3572 Style.NamespaceIndentation = FormatStyle::NI_None; 3573 verifyFormat("template <class T>\n" 3574 "concept a_concept = X<>;\n" 3575 "namespace B {\n" 3576 "struct b_struct {};\n" 3577 "} // namespace B\n", 3578 Style); 3579 verifyFormat("template <int I> constexpr void foo requires(I == 42) {}\n" 3580 "namespace ns {\n" 3581 "void foo() {}\n" 3582 "} // namespace ns\n", 3583 Style); 3584 } 3585 3586 TEST_F(FormatTest, NamespaceMacros) { 3587 FormatStyle Style = getLLVMStyle(); 3588 Style.NamespaceMacros.push_back("TESTSUITE"); 3589 3590 verifyFormat("TESTSUITE(A) {\n" 3591 "int foo();\n" 3592 "} // TESTSUITE(A)", 3593 Style); 3594 3595 verifyFormat("TESTSUITE(A, B) {\n" 3596 "int foo();\n" 3597 "} // TESTSUITE(A)", 3598 Style); 3599 3600 // Properly indent according to NamespaceIndentation style 3601 Style.NamespaceIndentation = FormatStyle::NI_All; 3602 verifyFormat("TESTSUITE(A) {\n" 3603 " int foo();\n" 3604 "} // TESTSUITE(A)", 3605 Style); 3606 verifyFormat("TESTSUITE(A) {\n" 3607 " namespace B {\n" 3608 " int foo();\n" 3609 " } // namespace B\n" 3610 "} // TESTSUITE(A)", 3611 Style); 3612 verifyFormat("namespace A {\n" 3613 " TESTSUITE(B) {\n" 3614 " int foo();\n" 3615 " } // TESTSUITE(B)\n" 3616 "} // namespace A", 3617 Style); 3618 3619 Style.NamespaceIndentation = FormatStyle::NI_Inner; 3620 verifyFormat("TESTSUITE(A) {\n" 3621 "TESTSUITE(B) {\n" 3622 " int foo();\n" 3623 "} // TESTSUITE(B)\n" 3624 "} // TESTSUITE(A)", 3625 Style); 3626 verifyFormat("TESTSUITE(A) {\n" 3627 "namespace B {\n" 3628 " int foo();\n" 3629 "} // namespace B\n" 3630 "} // TESTSUITE(A)", 3631 Style); 3632 verifyFormat("namespace A {\n" 3633 "TESTSUITE(B) {\n" 3634 " int foo();\n" 3635 "} // TESTSUITE(B)\n" 3636 "} // namespace A", 3637 Style); 3638 3639 // Properly merge namespace-macros blocks in CompactNamespaces mode 3640 Style.NamespaceIndentation = FormatStyle::NI_None; 3641 Style.CompactNamespaces = true; 3642 verifyFormat("TESTSUITE(A) { TESTSUITE(B) {\n" 3643 "}} // TESTSUITE(A::B)", 3644 Style); 3645 3646 EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n" 3647 "}} // TESTSUITE(out::in)", 3648 format("TESTSUITE(out) {\n" 3649 "TESTSUITE(in) {\n" 3650 "} // TESTSUITE(in)\n" 3651 "} // TESTSUITE(out)", 3652 Style)); 3653 3654 EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n" 3655 "}} // TESTSUITE(out::in)", 3656 format("TESTSUITE(out) {\n" 3657 "TESTSUITE(in) {\n" 3658 "} // TESTSUITE(in)\n" 3659 "} // TESTSUITE(out)", 3660 Style)); 3661 3662 // Do not merge different namespaces/macros 3663 EXPECT_EQ("namespace out {\n" 3664 "TESTSUITE(in) {\n" 3665 "} // TESTSUITE(in)\n" 3666 "} // namespace out", 3667 format("namespace out {\n" 3668 "TESTSUITE(in) {\n" 3669 "} // TESTSUITE(in)\n" 3670 "} // namespace out", 3671 Style)); 3672 EXPECT_EQ("TESTSUITE(out) {\n" 3673 "namespace in {\n" 3674 "} // namespace in\n" 3675 "} // TESTSUITE(out)", 3676 format("TESTSUITE(out) {\n" 3677 "namespace in {\n" 3678 "} // namespace in\n" 3679 "} // TESTSUITE(out)", 3680 Style)); 3681 Style.NamespaceMacros.push_back("FOOBAR"); 3682 EXPECT_EQ("TESTSUITE(out) {\n" 3683 "FOOBAR(in) {\n" 3684 "} // FOOBAR(in)\n" 3685 "} // TESTSUITE(out)", 3686 format("TESTSUITE(out) {\n" 3687 "FOOBAR(in) {\n" 3688 "} // FOOBAR(in)\n" 3689 "} // TESTSUITE(out)", 3690 Style)); 3691 } 3692 3693 TEST_F(FormatTest, FormatsCompactNamespaces) { 3694 FormatStyle Style = getLLVMStyle(); 3695 Style.CompactNamespaces = true; 3696 Style.NamespaceMacros.push_back("TESTSUITE"); 3697 3698 verifyFormat("namespace A { namespace B {\n" 3699 "}} // namespace A::B", 3700 Style); 3701 3702 EXPECT_EQ("namespace out { namespace in {\n" 3703 "}} // namespace out::in", 3704 format("namespace out {\n" 3705 "namespace in {\n" 3706 "} // namespace in\n" 3707 "} // namespace out", 3708 Style)); 3709 3710 // Only namespaces which have both consecutive opening and end get compacted 3711 EXPECT_EQ("namespace out {\n" 3712 "namespace in1 {\n" 3713 "} // namespace in1\n" 3714 "namespace in2 {\n" 3715 "} // namespace in2\n" 3716 "} // namespace out", 3717 format("namespace out {\n" 3718 "namespace in1 {\n" 3719 "} // namespace in1\n" 3720 "namespace in2 {\n" 3721 "} // namespace in2\n" 3722 "} // namespace out", 3723 Style)); 3724 3725 EXPECT_EQ("namespace out {\n" 3726 "int i;\n" 3727 "namespace in {\n" 3728 "int j;\n" 3729 "} // namespace in\n" 3730 "int k;\n" 3731 "} // namespace out", 3732 format("namespace out { int i;\n" 3733 "namespace in { int j; } // namespace in\n" 3734 "int k; } // namespace out", 3735 Style)); 3736 3737 EXPECT_EQ("namespace A { namespace B { namespace C {\n" 3738 "}}} // namespace A::B::C\n", 3739 format("namespace A { namespace B {\n" 3740 "namespace C {\n" 3741 "}} // namespace B::C\n" 3742 "} // namespace A\n", 3743 Style)); 3744 3745 Style.ColumnLimit = 40; 3746 EXPECT_EQ("namespace aaaaaaaaaa {\n" 3747 "namespace bbbbbbbbbb {\n" 3748 "}} // namespace aaaaaaaaaa::bbbbbbbbbb", 3749 format("namespace aaaaaaaaaa {\n" 3750 "namespace bbbbbbbbbb {\n" 3751 "} // namespace bbbbbbbbbb\n" 3752 "} // namespace aaaaaaaaaa", 3753 Style)); 3754 3755 EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n" 3756 "namespace cccccc {\n" 3757 "}}} // namespace aaaaaa::bbbbbb::cccccc", 3758 format("namespace aaaaaa {\n" 3759 "namespace bbbbbb {\n" 3760 "namespace cccccc {\n" 3761 "} // namespace cccccc\n" 3762 "} // namespace bbbbbb\n" 3763 "} // namespace aaaaaa", 3764 Style)); 3765 Style.ColumnLimit = 80; 3766 3767 // Extra semicolon after 'inner' closing brace prevents merging 3768 EXPECT_EQ("namespace out { namespace in {\n" 3769 "}; } // namespace out::in", 3770 format("namespace out {\n" 3771 "namespace in {\n" 3772 "}; // namespace in\n" 3773 "} // namespace out", 3774 Style)); 3775 3776 // Extra semicolon after 'outer' closing brace is conserved 3777 EXPECT_EQ("namespace out { namespace in {\n" 3778 "}}; // namespace out::in", 3779 format("namespace out {\n" 3780 "namespace in {\n" 3781 "} // namespace in\n" 3782 "}; // namespace out", 3783 Style)); 3784 3785 Style.NamespaceIndentation = FormatStyle::NI_All; 3786 EXPECT_EQ("namespace out { namespace in {\n" 3787 " int i;\n" 3788 "}} // namespace out::in", 3789 format("namespace out {\n" 3790 "namespace in {\n" 3791 "int i;\n" 3792 "} // namespace in\n" 3793 "} // namespace out", 3794 Style)); 3795 EXPECT_EQ("namespace out { namespace mid {\n" 3796 " namespace in {\n" 3797 " int j;\n" 3798 " } // namespace in\n" 3799 " int k;\n" 3800 "}} // namespace out::mid", 3801 format("namespace out { namespace mid {\n" 3802 "namespace in { int j; } // namespace in\n" 3803 "int k; }} // namespace out::mid", 3804 Style)); 3805 3806 Style.NamespaceIndentation = FormatStyle::NI_Inner; 3807 EXPECT_EQ("namespace out { namespace in {\n" 3808 " int i;\n" 3809 "}} // namespace out::in", 3810 format("namespace out {\n" 3811 "namespace in {\n" 3812 "int i;\n" 3813 "} // namespace in\n" 3814 "} // namespace out", 3815 Style)); 3816 EXPECT_EQ("namespace out { namespace mid { namespace in {\n" 3817 " int i;\n" 3818 "}}} // namespace out::mid::in", 3819 format("namespace out {\n" 3820 "namespace mid {\n" 3821 "namespace in {\n" 3822 "int i;\n" 3823 "} // namespace in\n" 3824 "} // namespace mid\n" 3825 "} // namespace out", 3826 Style)); 3827 } 3828 3829 TEST_F(FormatTest, FormatsExternC) { 3830 verifyFormat("extern \"C\" {\nint a;"); 3831 verifyFormat("extern \"C\" {}"); 3832 verifyFormat("extern \"C\" {\n" 3833 "int foo();\n" 3834 "}"); 3835 verifyFormat("extern \"C\" int foo() {}"); 3836 verifyFormat("extern \"C\" int foo();"); 3837 verifyFormat("extern \"C\" int foo() {\n" 3838 " int i = 42;\n" 3839 " return i;\n" 3840 "}"); 3841 3842 FormatStyle Style = getLLVMStyle(); 3843 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3844 Style.BraceWrapping.AfterFunction = true; 3845 verifyFormat("extern \"C\" int foo() {}", Style); 3846 verifyFormat("extern \"C\" int foo();", Style); 3847 verifyFormat("extern \"C\" int foo()\n" 3848 "{\n" 3849 " int i = 42;\n" 3850 " return i;\n" 3851 "}", 3852 Style); 3853 3854 Style.BraceWrapping.AfterExternBlock = true; 3855 Style.BraceWrapping.SplitEmptyRecord = false; 3856 verifyFormat("extern \"C\"\n" 3857 "{}", 3858 Style); 3859 verifyFormat("extern \"C\"\n" 3860 "{\n" 3861 " int foo();\n" 3862 "}", 3863 Style); 3864 } 3865 3866 TEST_F(FormatTest, IndentExternBlockStyle) { 3867 FormatStyle Style = getLLVMStyle(); 3868 Style.IndentWidth = 2; 3869 3870 Style.IndentExternBlock = FormatStyle::IEBS_Indent; 3871 verifyFormat("extern \"C\" { /*9*/\n" 3872 "}", 3873 Style); 3874 verifyFormat("extern \"C\" {\n" 3875 " int foo10();\n" 3876 "}", 3877 Style); 3878 3879 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; 3880 verifyFormat("extern \"C\" { /*11*/\n" 3881 "}", 3882 Style); 3883 verifyFormat("extern \"C\" {\n" 3884 "int foo12();\n" 3885 "}", 3886 Style); 3887 3888 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3889 Style.BraceWrapping.AfterExternBlock = true; 3890 Style.IndentExternBlock = FormatStyle::IEBS_Indent; 3891 verifyFormat("extern \"C\"\n" 3892 "{ /*13*/\n" 3893 "}", 3894 Style); 3895 verifyFormat("extern \"C\"\n{\n" 3896 " int foo14();\n" 3897 "}", 3898 Style); 3899 3900 Style.BraceWrapping.AfterExternBlock = false; 3901 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; 3902 verifyFormat("extern \"C\" { /*15*/\n" 3903 "}", 3904 Style); 3905 verifyFormat("extern \"C\" {\n" 3906 "int foo16();\n" 3907 "}", 3908 Style); 3909 3910 Style.BraceWrapping.AfterExternBlock = true; 3911 verifyFormat("extern \"C\"\n" 3912 "{ /*13*/\n" 3913 "}", 3914 Style); 3915 verifyFormat("extern \"C\"\n" 3916 "{\n" 3917 "int foo14();\n" 3918 "}", 3919 Style); 3920 3921 Style.IndentExternBlock = FormatStyle::IEBS_Indent; 3922 verifyFormat("extern \"C\"\n" 3923 "{ /*13*/\n" 3924 "}", 3925 Style); 3926 verifyFormat("extern \"C\"\n" 3927 "{\n" 3928 " int foo14();\n" 3929 "}", 3930 Style); 3931 } 3932 3933 TEST_F(FormatTest, FormatsInlineASM) { 3934 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); 3935 verifyFormat("asm(\"nop\" ::: \"memory\");"); 3936 verifyFormat( 3937 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 3938 " \"cpuid\\n\\t\"\n" 3939 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 3940 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" 3941 " : \"a\"(value));"); 3942 EXPECT_EQ( 3943 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 3944 " __asm {\n" 3945 " mov edx,[that] // vtable in edx\n" 3946 " mov eax,methodIndex\n" 3947 " call [edx][eax*4] // stdcall\n" 3948 " }\n" 3949 "}", 3950 format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 3951 " __asm {\n" 3952 " mov edx,[that] // vtable in edx\n" 3953 " mov eax,methodIndex\n" 3954 " call [edx][eax*4] // stdcall\n" 3955 " }\n" 3956 "}")); 3957 EXPECT_EQ("_asm {\n" 3958 " xor eax, eax;\n" 3959 " cpuid;\n" 3960 "}", 3961 format("_asm {\n" 3962 " xor eax, eax;\n" 3963 " cpuid;\n" 3964 "}")); 3965 verifyFormat("void function() {\n" 3966 " // comment\n" 3967 " asm(\"\");\n" 3968 "}"); 3969 EXPECT_EQ("__asm {\n" 3970 "}\n" 3971 "int i;", 3972 format("__asm {\n" 3973 "}\n" 3974 "int i;")); 3975 } 3976 3977 TEST_F(FormatTest, FormatTryCatch) { 3978 verifyFormat("try {\n" 3979 " throw a * b;\n" 3980 "} catch (int a) {\n" 3981 " // Do nothing.\n" 3982 "} catch (...) {\n" 3983 " exit(42);\n" 3984 "}"); 3985 3986 // Function-level try statements. 3987 verifyFormat("int f() try { return 4; } catch (...) {\n" 3988 " return 5;\n" 3989 "}"); 3990 verifyFormat("class A {\n" 3991 " int a;\n" 3992 " A() try : a(0) {\n" 3993 " } catch (...) {\n" 3994 " throw;\n" 3995 " }\n" 3996 "};\n"); 3997 verifyFormat("class A {\n" 3998 " int a;\n" 3999 " A() try : a(0), b{1} {\n" 4000 " } catch (...) {\n" 4001 " throw;\n" 4002 " }\n" 4003 "};\n"); 4004 verifyFormat("class A {\n" 4005 " int a;\n" 4006 " A() try : a(0), b{1}, c{2} {\n" 4007 " } catch (...) {\n" 4008 " throw;\n" 4009 " }\n" 4010 "};\n"); 4011 verifyFormat("class A {\n" 4012 " int a;\n" 4013 " A() try : a(0), b{1}, c{2} {\n" 4014 " { // New scope.\n" 4015 " }\n" 4016 " } catch (...) {\n" 4017 " throw;\n" 4018 " }\n" 4019 "};\n"); 4020 4021 // Incomplete try-catch blocks. 4022 verifyIncompleteFormat("try {} catch ("); 4023 } 4024 4025 TEST_F(FormatTest, FormatTryAsAVariable) { 4026 verifyFormat("int try;"); 4027 verifyFormat("int try, size;"); 4028 verifyFormat("try = foo();"); 4029 verifyFormat("if (try < size) {\n return true;\n}"); 4030 4031 verifyFormat("int catch;"); 4032 verifyFormat("int catch, size;"); 4033 verifyFormat("catch = foo();"); 4034 verifyFormat("if (catch < size) {\n return true;\n}"); 4035 4036 FormatStyle Style = getLLVMStyle(); 4037 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4038 Style.BraceWrapping.AfterFunction = true; 4039 Style.BraceWrapping.BeforeCatch = true; 4040 verifyFormat("try {\n" 4041 " int bar = 1;\n" 4042 "}\n" 4043 "catch (...) {\n" 4044 " int bar = 1;\n" 4045 "}", 4046 Style); 4047 verifyFormat("#if NO_EX\n" 4048 "try\n" 4049 "#endif\n" 4050 "{\n" 4051 "}\n" 4052 "#if NO_EX\n" 4053 "catch (...) {\n" 4054 "}", 4055 Style); 4056 verifyFormat("try /* abc */ {\n" 4057 " int bar = 1;\n" 4058 "}\n" 4059 "catch (...) {\n" 4060 " int bar = 1;\n" 4061 "}", 4062 Style); 4063 verifyFormat("try\n" 4064 "// abc\n" 4065 "{\n" 4066 " int bar = 1;\n" 4067 "}\n" 4068 "catch (...) {\n" 4069 " int bar = 1;\n" 4070 "}", 4071 Style); 4072 } 4073 4074 TEST_F(FormatTest, FormatSEHTryCatch) { 4075 verifyFormat("__try {\n" 4076 " int a = b * c;\n" 4077 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n" 4078 " // Do nothing.\n" 4079 "}"); 4080 4081 verifyFormat("__try {\n" 4082 " int a = b * c;\n" 4083 "} __finally {\n" 4084 " // Do nothing.\n" 4085 "}"); 4086 4087 verifyFormat("DEBUG({\n" 4088 " __try {\n" 4089 " } __finally {\n" 4090 " }\n" 4091 "});\n"); 4092 } 4093 4094 TEST_F(FormatTest, IncompleteTryCatchBlocks) { 4095 verifyFormat("try {\n" 4096 " f();\n" 4097 "} catch {\n" 4098 " g();\n" 4099 "}"); 4100 verifyFormat("try {\n" 4101 " f();\n" 4102 "} catch (A a) MACRO(x) {\n" 4103 " g();\n" 4104 "} catch (B b) MACRO(x) {\n" 4105 " g();\n" 4106 "}"); 4107 } 4108 4109 TEST_F(FormatTest, FormatTryCatchBraceStyles) { 4110 FormatStyle Style = getLLVMStyle(); 4111 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, 4112 FormatStyle::BS_WebKit}) { 4113 Style.BreakBeforeBraces = BraceStyle; 4114 verifyFormat("try {\n" 4115 " // something\n" 4116 "} catch (...) {\n" 4117 " // something\n" 4118 "}", 4119 Style); 4120 } 4121 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 4122 verifyFormat("try {\n" 4123 " // something\n" 4124 "}\n" 4125 "catch (...) {\n" 4126 " // something\n" 4127 "}", 4128 Style); 4129 verifyFormat("__try {\n" 4130 " // something\n" 4131 "}\n" 4132 "__finally {\n" 4133 " // something\n" 4134 "}", 4135 Style); 4136 verifyFormat("@try {\n" 4137 " // something\n" 4138 "}\n" 4139 "@finally {\n" 4140 " // something\n" 4141 "}", 4142 Style); 4143 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 4144 verifyFormat("try\n" 4145 "{\n" 4146 " // something\n" 4147 "}\n" 4148 "catch (...)\n" 4149 "{\n" 4150 " // something\n" 4151 "}", 4152 Style); 4153 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 4154 verifyFormat("try\n" 4155 " {\n" 4156 " // something white\n" 4157 " }\n" 4158 "catch (...)\n" 4159 " {\n" 4160 " // something white\n" 4161 " }", 4162 Style); 4163 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 4164 verifyFormat("try\n" 4165 " {\n" 4166 " // something\n" 4167 " }\n" 4168 "catch (...)\n" 4169 " {\n" 4170 " // something\n" 4171 " }", 4172 Style); 4173 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4174 Style.BraceWrapping.BeforeCatch = true; 4175 verifyFormat("try {\n" 4176 " // something\n" 4177 "}\n" 4178 "catch (...) {\n" 4179 " // something\n" 4180 "}", 4181 Style); 4182 } 4183 4184 TEST_F(FormatTest, StaticInitializers) { 4185 verifyFormat("static SomeClass SC = {1, 'a'};"); 4186 4187 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" 4188 " 100000000, " 4189 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); 4190 4191 // Here, everything other than the "}" would fit on a line. 4192 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" 4193 " 10000000000000000000000000};"); 4194 EXPECT_EQ("S s = {a,\n" 4195 "\n" 4196 " b};", 4197 format("S s = {\n" 4198 " a,\n" 4199 "\n" 4200 " b\n" 4201 "};")); 4202 4203 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first 4204 // line. However, the formatting looks a bit off and this probably doesn't 4205 // happen often in practice. 4206 verifyFormat("static int Variable[1] = {\n" 4207 " {1000000000000000000000000000000000000}};", 4208 getLLVMStyleWithColumns(40)); 4209 } 4210 4211 TEST_F(FormatTest, DesignatedInitializers) { 4212 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 4213 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" 4214 " .bbbbbbbbbb = 2,\n" 4215 " .cccccccccc = 3,\n" 4216 " .dddddddddd = 4,\n" 4217 " .eeeeeeeeee = 5};"); 4218 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 4219 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" 4220 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" 4221 " .ccccccccccccccccccccccccccc = 3,\n" 4222 " .ddddddddddddddddddddddddddd = 4,\n" 4223 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); 4224 4225 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); 4226 4227 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 4228 verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n" 4229 " [2] = bbbbbbbbbb,\n" 4230 " [3] = cccccccccc,\n" 4231 " [4] = dddddddddd,\n" 4232 " [5] = eeeeeeeeee};"); 4233 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 4234 " [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 4235 " [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 4236 " [3] = cccccccccccccccccccccccccccccccccccccc,\n" 4237 " [4] = dddddddddddddddddddddddddddddddddddddd,\n" 4238 " [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};"); 4239 } 4240 4241 TEST_F(FormatTest, NestedStaticInitializers) { 4242 verifyFormat("static A x = {{{}}};\n"); 4243 verifyFormat("static A x = {{{init1, init2, init3, init4},\n" 4244 " {init1, init2, init3, init4}}};", 4245 getLLVMStyleWithColumns(50)); 4246 4247 verifyFormat("somes Status::global_reps[3] = {\n" 4248 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 4249 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 4250 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", 4251 getLLVMStyleWithColumns(60)); 4252 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n" 4253 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 4254 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 4255 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};"); 4256 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" 4257 " {rect.fRight - rect.fLeft, rect.fBottom - " 4258 "rect.fTop}};"); 4259 4260 verifyFormat( 4261 "SomeArrayOfSomeType a = {\n" 4262 " {{1, 2, 3},\n" 4263 " {1, 2, 3},\n" 4264 " {111111111111111111111111111111, 222222222222222222222222222222,\n" 4265 " 333333333333333333333333333333},\n" 4266 " {1, 2, 3},\n" 4267 " {1, 2, 3}}};"); 4268 verifyFormat( 4269 "SomeArrayOfSomeType a = {\n" 4270 " {{1, 2, 3}},\n" 4271 " {{1, 2, 3}},\n" 4272 " {{111111111111111111111111111111, 222222222222222222222222222222,\n" 4273 " 333333333333333333333333333333}},\n" 4274 " {{1, 2, 3}},\n" 4275 " {{1, 2, 3}}};"); 4276 4277 verifyFormat("struct {\n" 4278 " unsigned bit;\n" 4279 " const char *const name;\n" 4280 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" 4281 " {kOsWin, \"Windows\"},\n" 4282 " {kOsLinux, \"Linux\"},\n" 4283 " {kOsCrOS, \"Chrome OS\"}};"); 4284 verifyFormat("struct {\n" 4285 " unsigned bit;\n" 4286 " const char *const name;\n" 4287 "} kBitsToOs[] = {\n" 4288 " {kOsMac, \"Mac\"},\n" 4289 " {kOsWin, \"Windows\"},\n" 4290 " {kOsLinux, \"Linux\"},\n" 4291 " {kOsCrOS, \"Chrome OS\"},\n" 4292 "};"); 4293 } 4294 4295 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 4296 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 4297 " \\\n" 4298 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 4299 } 4300 4301 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 4302 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 4303 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 4304 4305 // Do break defaulted and deleted functions. 4306 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 4307 " default;", 4308 getLLVMStyleWithColumns(40)); 4309 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 4310 " delete;", 4311 getLLVMStyleWithColumns(40)); 4312 } 4313 4314 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { 4315 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3", 4316 getLLVMStyleWithColumns(40)); 4317 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 4318 getLLVMStyleWithColumns(40)); 4319 EXPECT_EQ("#define Q \\\n" 4320 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" 4321 " \"aaaaaaaa.cpp\"", 4322 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 4323 getLLVMStyleWithColumns(40))); 4324 } 4325 4326 TEST_F(FormatTest, UnderstandsLinePPDirective) { 4327 EXPECT_EQ("# 123 \"A string literal\"", 4328 format(" # 123 \"A string literal\"")); 4329 } 4330 4331 TEST_F(FormatTest, LayoutUnknownPPDirective) { 4332 EXPECT_EQ("#;", format("#;")); 4333 verifyFormat("#\n;\n;\n;"); 4334 } 4335 4336 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 4337 EXPECT_EQ("#line 42 \"test\"\n", 4338 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 4339 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", 4340 getLLVMStyleWithColumns(12))); 4341 } 4342 4343 TEST_F(FormatTest, EndOfFileEndsPPDirective) { 4344 EXPECT_EQ("#line 42 \"test\"", 4345 format("# \\\n line \\\n 42 \\\n \"test\"")); 4346 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); 4347 } 4348 4349 TEST_F(FormatTest, DoesntRemoveUnknownTokens) { 4350 verifyFormat("#define A \\x20"); 4351 verifyFormat("#define A \\ x20"); 4352 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20")); 4353 verifyFormat("#define A ''"); 4354 verifyFormat("#define A ''qqq"); 4355 verifyFormat("#define A `qqq"); 4356 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); 4357 EXPECT_EQ("const char *c = STRINGIFY(\n" 4358 "\\na : b);", 4359 format("const char * c = STRINGIFY(\n" 4360 "\\na : b);")); 4361 4362 verifyFormat("a\r\\"); 4363 verifyFormat("a\v\\"); 4364 verifyFormat("a\f\\"); 4365 } 4366 4367 TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) { 4368 FormatStyle style = getChromiumStyle(FormatStyle::LK_Cpp); 4369 style.IndentWidth = 4; 4370 style.PPIndentWidth = 1; 4371 4372 style.IndentPPDirectives = FormatStyle::PPDIS_None; 4373 verifyFormat("#ifdef __linux__\n" 4374 "void foo() {\n" 4375 " int x = 0;\n" 4376 "}\n" 4377 "#define FOO\n" 4378 "#endif\n" 4379 "void bar() {\n" 4380 " int y = 0;\n" 4381 "}\n", 4382 style); 4383 4384 style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 4385 verifyFormat("#ifdef __linux__\n" 4386 "void foo() {\n" 4387 " int x = 0;\n" 4388 "}\n" 4389 "# define FOO foo\n" 4390 "#endif\n" 4391 "void bar() {\n" 4392 " int y = 0;\n" 4393 "}\n", 4394 style); 4395 4396 style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash; 4397 verifyFormat("#ifdef __linux__\n" 4398 "void foo() {\n" 4399 " int x = 0;\n" 4400 "}\n" 4401 " #define FOO foo\n" 4402 "#endif\n" 4403 "void bar() {\n" 4404 " int y = 0;\n" 4405 "}\n", 4406 style); 4407 } 4408 4409 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 4410 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 4411 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 4412 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 4413 // FIXME: We never break before the macro name. 4414 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12)); 4415 4416 verifyFormat("#define A A\n#define A A"); 4417 verifyFormat("#define A(X) A\n#define A A"); 4418 4419 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 4420 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 4421 } 4422 4423 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 4424 EXPECT_EQ("// somecomment\n" 4425 "#include \"a.h\"\n" 4426 "#define A( \\\n" 4427 " A, B)\n" 4428 "#include \"b.h\"\n" 4429 "// somecomment\n", 4430 format(" // somecomment\n" 4431 " #include \"a.h\"\n" 4432 "#define A(A,\\\n" 4433 " B)\n" 4434 " #include \"b.h\"\n" 4435 " // somecomment\n", 4436 getLLVMStyleWithColumns(13))); 4437 } 4438 4439 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } 4440 4441 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 4442 EXPECT_EQ("#define A \\\n" 4443 " c; \\\n" 4444 " e;\n" 4445 "f;", 4446 format("#define A c; e;\n" 4447 "f;", 4448 getLLVMStyleWithColumns(14))); 4449 } 4450 4451 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } 4452 4453 TEST_F(FormatTest, MacroDefinitionInsideStatement) { 4454 EXPECT_EQ("int x,\n" 4455 "#define A\n" 4456 " y;", 4457 format("int x,\n#define A\ny;")); 4458 } 4459 4460 TEST_F(FormatTest, HashInMacroDefinition) { 4461 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle())); 4462 EXPECT_EQ("#define A(c) u#c", format("#define A(c) u#c", getLLVMStyle())); 4463 EXPECT_EQ("#define A(c) U#c", format("#define A(c) U#c", getLLVMStyle())); 4464 EXPECT_EQ("#define A(c) u8#c", format("#define A(c) u8#c", getLLVMStyle())); 4465 EXPECT_EQ("#define A(c) LR#c", format("#define A(c) LR#c", getLLVMStyle())); 4466 EXPECT_EQ("#define A(c) uR#c", format("#define A(c) uR#c", getLLVMStyle())); 4467 EXPECT_EQ("#define A(c) UR#c", format("#define A(c) UR#c", getLLVMStyle())); 4468 EXPECT_EQ("#define A(c) u8R#c", format("#define A(c) u8R#c", getLLVMStyle())); 4469 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 4470 verifyFormat("#define A \\\n" 4471 " { \\\n" 4472 " f(#c); \\\n" 4473 " }", 4474 getLLVMStyleWithColumns(11)); 4475 4476 verifyFormat("#define A(X) \\\n" 4477 " void function##X()", 4478 getLLVMStyleWithColumns(22)); 4479 4480 verifyFormat("#define A(a, b, c) \\\n" 4481 " void a##b##c()", 4482 getLLVMStyleWithColumns(22)); 4483 4484 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 4485 } 4486 4487 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 4488 EXPECT_EQ("#define A (x)", format("#define A (x)")); 4489 EXPECT_EQ("#define A(x)", format("#define A(x)")); 4490 4491 FormatStyle Style = getLLVMStyle(); 4492 Style.SpaceBeforeParens = FormatStyle::SBPO_Never; 4493 verifyFormat("#define true ((foo)1)", Style); 4494 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 4495 verifyFormat("#define false((foo)0)", Style); 4496 } 4497 4498 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 4499 EXPECT_EQ("#define A b;", format("#define A \\\n" 4500 " \\\n" 4501 " b;", 4502 getLLVMStyleWithColumns(25))); 4503 EXPECT_EQ("#define A \\\n" 4504 " \\\n" 4505 " a; \\\n" 4506 " b;", 4507 format("#define A \\\n" 4508 " \\\n" 4509 " a; \\\n" 4510 " b;", 4511 getLLVMStyleWithColumns(11))); 4512 EXPECT_EQ("#define A \\\n" 4513 " a; \\\n" 4514 " \\\n" 4515 " b;", 4516 format("#define A \\\n" 4517 " a; \\\n" 4518 " \\\n" 4519 " b;", 4520 getLLVMStyleWithColumns(11))); 4521 } 4522 4523 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 4524 verifyIncompleteFormat("#define A :"); 4525 verifyFormat("#define SOMECASES \\\n" 4526 " case 1: \\\n" 4527 " case 2\n", 4528 getLLVMStyleWithColumns(20)); 4529 verifyFormat("#define MACRO(a) \\\n" 4530 " if (a) \\\n" 4531 " f(); \\\n" 4532 " else \\\n" 4533 " g()", 4534 getLLVMStyleWithColumns(18)); 4535 verifyFormat("#define A template <typename T>"); 4536 verifyIncompleteFormat("#define STR(x) #x\n" 4537 "f(STR(this_is_a_string_literal{));"); 4538 verifyFormat("#pragma omp threadprivate( \\\n" 4539 " y)), // expected-warning", 4540 getLLVMStyleWithColumns(28)); 4541 verifyFormat("#d, = };"); 4542 verifyFormat("#if \"a"); 4543 verifyIncompleteFormat("({\n" 4544 "#define b \\\n" 4545 " } \\\n" 4546 " a\n" 4547 "a", 4548 getLLVMStyleWithColumns(15)); 4549 verifyFormat("#define A \\\n" 4550 " { \\\n" 4551 " {\n" 4552 "#define B \\\n" 4553 " } \\\n" 4554 " }", 4555 getLLVMStyleWithColumns(15)); 4556 verifyNoCrash("#if a\na(\n#else\n#endif\n{a"); 4557 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}"); 4558 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};"); 4559 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}"); 4560 } 4561 4562 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { 4563 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. 4564 EXPECT_EQ("class A : public QObject {\n" 4565 " Q_OBJECT\n" 4566 "\n" 4567 " A() {}\n" 4568 "};", 4569 format("class A : public QObject {\n" 4570 " Q_OBJECT\n" 4571 "\n" 4572 " A() {\n}\n" 4573 "} ;")); 4574 EXPECT_EQ("MACRO\n" 4575 "/*static*/ int i;", 4576 format("MACRO\n" 4577 " /*static*/ int i;")); 4578 EXPECT_EQ("SOME_MACRO\n" 4579 "namespace {\n" 4580 "void f();\n" 4581 "} // namespace", 4582 format("SOME_MACRO\n" 4583 " namespace {\n" 4584 "void f( );\n" 4585 "} // namespace")); 4586 // Only if the identifier contains at least 5 characters. 4587 EXPECT_EQ("HTTP f();", format("HTTP\nf();")); 4588 EXPECT_EQ("MACRO\nf();", format("MACRO\nf();")); 4589 // Only if everything is upper case. 4590 EXPECT_EQ("class A : public QObject {\n" 4591 " Q_Object A() {}\n" 4592 "};", 4593 format("class A : public QObject {\n" 4594 " Q_Object\n" 4595 " A() {\n}\n" 4596 "} ;")); 4597 4598 // Only if the next line can actually start an unwrapped line. 4599 EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;", 4600 format("SOME_WEIRD_LOG_MACRO\n" 4601 "<< SomeThing;")); 4602 4603 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " 4604 "(n, buffers))\n", 4605 getChromiumStyle(FormatStyle::LK_Cpp)); 4606 4607 // See PR41483 4608 EXPECT_EQ("/**/ FOO(a)\n" 4609 "FOO(b)", 4610 format("/**/ FOO(a)\n" 4611 "FOO(b)")); 4612 } 4613 4614 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { 4615 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 4616 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 4617 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 4618 "class X {};\n" 4619 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 4620 "int *createScopDetectionPass() { return 0; }", 4621 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 4622 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 4623 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 4624 " class X {};\n" 4625 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 4626 " int *createScopDetectionPass() { return 0; }")); 4627 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as 4628 // braces, so that inner block is indented one level more. 4629 EXPECT_EQ("int q() {\n" 4630 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 4631 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 4632 " IPC_END_MESSAGE_MAP()\n" 4633 "}", 4634 format("int q() {\n" 4635 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 4636 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 4637 " IPC_END_MESSAGE_MAP()\n" 4638 "}")); 4639 4640 // Same inside macros. 4641 EXPECT_EQ("#define LIST(L) \\\n" 4642 " L(A) \\\n" 4643 " L(B) \\\n" 4644 " L(C)", 4645 format("#define LIST(L) \\\n" 4646 " L(A) \\\n" 4647 " L(B) \\\n" 4648 " L(C)", 4649 getGoogleStyle())); 4650 4651 // These must not be recognized as macros. 4652 EXPECT_EQ("int q() {\n" 4653 " f(x);\n" 4654 " f(x) {}\n" 4655 " f(x)->g();\n" 4656 " f(x)->*g();\n" 4657 " f(x).g();\n" 4658 " f(x) = x;\n" 4659 " f(x) += x;\n" 4660 " f(x) -= x;\n" 4661 " f(x) *= x;\n" 4662 " f(x) /= x;\n" 4663 " f(x) %= x;\n" 4664 " f(x) &= x;\n" 4665 " f(x) |= x;\n" 4666 " f(x) ^= x;\n" 4667 " f(x) >>= x;\n" 4668 " f(x) <<= x;\n" 4669 " f(x)[y].z();\n" 4670 " LOG(INFO) << x;\n" 4671 " ifstream(x) >> x;\n" 4672 "}\n", 4673 format("int q() {\n" 4674 " f(x)\n;\n" 4675 " f(x)\n {}\n" 4676 " f(x)\n->g();\n" 4677 " f(x)\n->*g();\n" 4678 " f(x)\n.g();\n" 4679 " f(x)\n = x;\n" 4680 " f(x)\n += x;\n" 4681 " f(x)\n -= x;\n" 4682 " f(x)\n *= x;\n" 4683 " f(x)\n /= x;\n" 4684 " f(x)\n %= x;\n" 4685 " f(x)\n &= x;\n" 4686 " f(x)\n |= x;\n" 4687 " f(x)\n ^= x;\n" 4688 " f(x)\n >>= x;\n" 4689 " f(x)\n <<= x;\n" 4690 " f(x)\n[y].z();\n" 4691 " LOG(INFO)\n << x;\n" 4692 " ifstream(x)\n >> x;\n" 4693 "}\n")); 4694 EXPECT_EQ("int q() {\n" 4695 " F(x)\n" 4696 " if (1) {\n" 4697 " }\n" 4698 " F(x)\n" 4699 " while (1) {\n" 4700 " }\n" 4701 " F(x)\n" 4702 " G(x);\n" 4703 " F(x)\n" 4704 " try {\n" 4705 " Q();\n" 4706 " } catch (...) {\n" 4707 " }\n" 4708 "}\n", 4709 format("int q() {\n" 4710 "F(x)\n" 4711 "if (1) {}\n" 4712 "F(x)\n" 4713 "while (1) {}\n" 4714 "F(x)\n" 4715 "G(x);\n" 4716 "F(x)\n" 4717 "try { Q(); } catch (...) {}\n" 4718 "}\n")); 4719 EXPECT_EQ("class A {\n" 4720 " A() : t(0) {}\n" 4721 " A(int i) noexcept() : {}\n" 4722 " A(X x)\n" // FIXME: function-level try blocks are broken. 4723 " try : t(0) {\n" 4724 " } catch (...) {\n" 4725 " }\n" 4726 "};", 4727 format("class A {\n" 4728 " A()\n : t(0) {}\n" 4729 " A(int i)\n noexcept() : {}\n" 4730 " A(X x)\n" 4731 " try : t(0) {} catch (...) {}\n" 4732 "};")); 4733 FormatStyle Style = getLLVMStyle(); 4734 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 4735 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 4736 Style.BraceWrapping.AfterFunction = true; 4737 EXPECT_EQ("void f()\n" 4738 "try\n" 4739 "{\n" 4740 "}", 4741 format("void f() try {\n" 4742 "}", 4743 Style)); 4744 EXPECT_EQ("class SomeClass {\n" 4745 "public:\n" 4746 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 4747 "};", 4748 format("class SomeClass {\n" 4749 "public:\n" 4750 " SomeClass()\n" 4751 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 4752 "};")); 4753 EXPECT_EQ("class SomeClass {\n" 4754 "public:\n" 4755 " SomeClass()\n" 4756 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 4757 "};", 4758 format("class SomeClass {\n" 4759 "public:\n" 4760 " SomeClass()\n" 4761 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 4762 "};", 4763 getLLVMStyleWithColumns(40))); 4764 4765 verifyFormat("MACRO(>)"); 4766 4767 // Some macros contain an implicit semicolon. 4768 Style = getLLVMStyle(); 4769 Style.StatementMacros.push_back("FOO"); 4770 verifyFormat("FOO(a) int b = 0;"); 4771 verifyFormat("FOO(a)\n" 4772 "int b = 0;", 4773 Style); 4774 verifyFormat("FOO(a);\n" 4775 "int b = 0;", 4776 Style); 4777 verifyFormat("FOO(argc, argv, \"4.0.2\")\n" 4778 "int b = 0;", 4779 Style); 4780 verifyFormat("FOO()\n" 4781 "int b = 0;", 4782 Style); 4783 verifyFormat("FOO\n" 4784 "int b = 0;", 4785 Style); 4786 verifyFormat("void f() {\n" 4787 " FOO(a)\n" 4788 " return a;\n" 4789 "}", 4790 Style); 4791 verifyFormat("FOO(a)\n" 4792 "FOO(b)", 4793 Style); 4794 verifyFormat("int a = 0;\n" 4795 "FOO(b)\n" 4796 "int c = 0;", 4797 Style); 4798 verifyFormat("int a = 0;\n" 4799 "int x = FOO(a)\n" 4800 "int b = 0;", 4801 Style); 4802 verifyFormat("void foo(int a) { FOO(a) }\n" 4803 "uint32_t bar() {}", 4804 Style); 4805 } 4806 4807 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { 4808 verifyFormat("#define A \\\n" 4809 " f({ \\\n" 4810 " g(); \\\n" 4811 " });", 4812 getLLVMStyleWithColumns(11)); 4813 } 4814 4815 TEST_F(FormatTest, IndentPreprocessorDirectives) { 4816 FormatStyle Style = getLLVMStyleWithColumns(40); 4817 Style.IndentPPDirectives = FormatStyle::PPDIS_None; 4818 verifyFormat("#ifdef _WIN32\n" 4819 "#define A 0\n" 4820 "#ifdef VAR2\n" 4821 "#define B 1\n" 4822 "#include <someheader.h>\n" 4823 "#define MACRO \\\n" 4824 " some_very_long_func_aaaaaaaaaa();\n" 4825 "#endif\n" 4826 "#else\n" 4827 "#define A 1\n" 4828 "#endif", 4829 Style); 4830 Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 4831 verifyFormat("#ifdef _WIN32\n" 4832 "# define A 0\n" 4833 "# ifdef VAR2\n" 4834 "# define B 1\n" 4835 "# include <someheader.h>\n" 4836 "# define MACRO \\\n" 4837 " some_very_long_func_aaaaaaaaaa();\n" 4838 "# endif\n" 4839 "#else\n" 4840 "# define A 1\n" 4841 "#endif", 4842 Style); 4843 verifyFormat("#if A\n" 4844 "# define MACRO \\\n" 4845 " void a(int x) { \\\n" 4846 " b(); \\\n" 4847 " c(); \\\n" 4848 " d(); \\\n" 4849 " e(); \\\n" 4850 " f(); \\\n" 4851 " }\n" 4852 "#endif", 4853 Style); 4854 // Comments before include guard. 4855 verifyFormat("// file comment\n" 4856 "// file comment\n" 4857 "#ifndef HEADER_H\n" 4858 "#define HEADER_H\n" 4859 "code();\n" 4860 "#endif", 4861 Style); 4862 // Test with include guards. 4863 verifyFormat("#ifndef HEADER_H\n" 4864 "#define HEADER_H\n" 4865 "code();\n" 4866 "#endif", 4867 Style); 4868 // Include guards must have a #define with the same variable immediately 4869 // after #ifndef. 4870 verifyFormat("#ifndef NOT_GUARD\n" 4871 "# define FOO\n" 4872 "code();\n" 4873 "#endif", 4874 Style); 4875 4876 // Include guards must cover the entire file. 4877 verifyFormat("code();\n" 4878 "code();\n" 4879 "#ifndef NOT_GUARD\n" 4880 "# define NOT_GUARD\n" 4881 "code();\n" 4882 "#endif", 4883 Style); 4884 verifyFormat("#ifndef NOT_GUARD\n" 4885 "# define NOT_GUARD\n" 4886 "code();\n" 4887 "#endif\n" 4888 "code();", 4889 Style); 4890 // Test with trailing blank lines. 4891 verifyFormat("#ifndef HEADER_H\n" 4892 "#define HEADER_H\n" 4893 "code();\n" 4894 "#endif\n", 4895 Style); 4896 // Include guards don't have #else. 4897 verifyFormat("#ifndef NOT_GUARD\n" 4898 "# define NOT_GUARD\n" 4899 "code();\n" 4900 "#else\n" 4901 "#endif", 4902 Style); 4903 verifyFormat("#ifndef NOT_GUARD\n" 4904 "# define NOT_GUARD\n" 4905 "code();\n" 4906 "#elif FOO\n" 4907 "#endif", 4908 Style); 4909 // Non-identifier #define after potential include guard. 4910 verifyFormat("#ifndef FOO\n" 4911 "# define 1\n" 4912 "#endif\n", 4913 Style); 4914 // #if closes past last non-preprocessor line. 4915 verifyFormat("#ifndef FOO\n" 4916 "#define FOO\n" 4917 "#if 1\n" 4918 "int i;\n" 4919 "# define A 0\n" 4920 "#endif\n" 4921 "#endif\n", 4922 Style); 4923 // Don't crash if there is an #elif directive without a condition. 4924 verifyFormat("#if 1\n" 4925 "int x;\n" 4926 "#elif\n" 4927 "int y;\n" 4928 "#else\n" 4929 "int z;\n" 4930 "#endif", 4931 Style); 4932 // FIXME: This doesn't handle the case where there's code between the 4933 // #ifndef and #define but all other conditions hold. This is because when 4934 // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the 4935 // previous code line yet, so we can't detect it. 4936 EXPECT_EQ("#ifndef NOT_GUARD\n" 4937 "code();\n" 4938 "#define NOT_GUARD\n" 4939 "code();\n" 4940 "#endif", 4941 format("#ifndef NOT_GUARD\n" 4942 "code();\n" 4943 "# define NOT_GUARD\n" 4944 "code();\n" 4945 "#endif", 4946 Style)); 4947 // FIXME: This doesn't handle cases where legitimate preprocessor lines may 4948 // be outside an include guard. Examples are #pragma once and 4949 // #pragma GCC diagnostic, or anything else that does not change the meaning 4950 // of the file if it's included multiple times. 4951 EXPECT_EQ("#ifdef WIN32\n" 4952 "# pragma once\n" 4953 "#endif\n" 4954 "#ifndef HEADER_H\n" 4955 "# define HEADER_H\n" 4956 "code();\n" 4957 "#endif", 4958 format("#ifdef WIN32\n" 4959 "# pragma once\n" 4960 "#endif\n" 4961 "#ifndef HEADER_H\n" 4962 "#define HEADER_H\n" 4963 "code();\n" 4964 "#endif", 4965 Style)); 4966 // FIXME: This does not detect when there is a single non-preprocessor line 4967 // in front of an include-guard-like structure where other conditions hold 4968 // because ScopedLineState hides the line. 4969 EXPECT_EQ("code();\n" 4970 "#ifndef HEADER_H\n" 4971 "#define HEADER_H\n" 4972 "code();\n" 4973 "#endif", 4974 format("code();\n" 4975 "#ifndef HEADER_H\n" 4976 "# define HEADER_H\n" 4977 "code();\n" 4978 "#endif", 4979 Style)); 4980 // Keep comments aligned with #, otherwise indent comments normally. These 4981 // tests cannot use verifyFormat because messUp manipulates leading 4982 // whitespace. 4983 { 4984 const char *Expected = "" 4985 "void f() {\n" 4986 "#if 1\n" 4987 "// Preprocessor aligned.\n" 4988 "# define A 0\n" 4989 " // Code. Separated by blank line.\n" 4990 "\n" 4991 "# define B 0\n" 4992 " // Code. Not aligned with #\n" 4993 "# define C 0\n" 4994 "#endif"; 4995 const char *ToFormat = "" 4996 "void f() {\n" 4997 "#if 1\n" 4998 "// Preprocessor aligned.\n" 4999 "# define A 0\n" 5000 "// Code. Separated by blank line.\n" 5001 "\n" 5002 "# define B 0\n" 5003 " // Code. Not aligned with #\n" 5004 "# define C 0\n" 5005 "#endif"; 5006 EXPECT_EQ(Expected, format(ToFormat, Style)); 5007 EXPECT_EQ(Expected, format(Expected, Style)); 5008 } 5009 // Keep block quotes aligned. 5010 { 5011 const char *Expected = "" 5012 "void f() {\n" 5013 "#if 1\n" 5014 "/* Preprocessor aligned. */\n" 5015 "# define A 0\n" 5016 " /* Code. Separated by blank line. */\n" 5017 "\n" 5018 "# define B 0\n" 5019 " /* Code. Not aligned with # */\n" 5020 "# define C 0\n" 5021 "#endif"; 5022 const char *ToFormat = "" 5023 "void f() {\n" 5024 "#if 1\n" 5025 "/* Preprocessor aligned. */\n" 5026 "# define A 0\n" 5027 "/* Code. Separated by blank line. */\n" 5028 "\n" 5029 "# define B 0\n" 5030 " /* Code. Not aligned with # */\n" 5031 "# define C 0\n" 5032 "#endif"; 5033 EXPECT_EQ(Expected, format(ToFormat, Style)); 5034 EXPECT_EQ(Expected, format(Expected, Style)); 5035 } 5036 // Keep comments aligned with un-indented directives. 5037 { 5038 const char *Expected = "" 5039 "void f() {\n" 5040 "// Preprocessor aligned.\n" 5041 "#define A 0\n" 5042 " // Code. Separated by blank line.\n" 5043 "\n" 5044 "#define B 0\n" 5045 " // Code. Not aligned with #\n" 5046 "#define C 0\n"; 5047 const char *ToFormat = "" 5048 "void f() {\n" 5049 "// Preprocessor aligned.\n" 5050 "#define A 0\n" 5051 "// Code. Separated by blank line.\n" 5052 "\n" 5053 "#define B 0\n" 5054 " // Code. Not aligned with #\n" 5055 "#define C 0\n"; 5056 EXPECT_EQ(Expected, format(ToFormat, Style)); 5057 EXPECT_EQ(Expected, format(Expected, Style)); 5058 } 5059 // Test AfterHash with tabs. 5060 { 5061 FormatStyle Tabbed = Style; 5062 Tabbed.UseTab = FormatStyle::UT_Always; 5063 Tabbed.IndentWidth = 8; 5064 Tabbed.TabWidth = 8; 5065 verifyFormat("#ifdef _WIN32\n" 5066 "#\tdefine A 0\n" 5067 "#\tifdef VAR2\n" 5068 "#\t\tdefine B 1\n" 5069 "#\t\tinclude <someheader.h>\n" 5070 "#\t\tdefine MACRO \\\n" 5071 "\t\t\tsome_very_long_func_aaaaaaaaaa();\n" 5072 "#\tendif\n" 5073 "#else\n" 5074 "#\tdefine A 1\n" 5075 "#endif", 5076 Tabbed); 5077 } 5078 5079 // Regression test: Multiline-macro inside include guards. 5080 verifyFormat("#ifndef HEADER_H\n" 5081 "#define HEADER_H\n" 5082 "#define A() \\\n" 5083 " int i; \\\n" 5084 " int j;\n" 5085 "#endif // HEADER_H", 5086 getLLVMStyleWithColumns(20)); 5087 5088 Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash; 5089 // Basic before hash indent tests 5090 verifyFormat("#ifdef _WIN32\n" 5091 " #define A 0\n" 5092 " #ifdef VAR2\n" 5093 " #define B 1\n" 5094 " #include <someheader.h>\n" 5095 " #define MACRO \\\n" 5096 " some_very_long_func_aaaaaaaaaa();\n" 5097 " #endif\n" 5098 "#else\n" 5099 " #define A 1\n" 5100 "#endif", 5101 Style); 5102 verifyFormat("#if A\n" 5103 " #define MACRO \\\n" 5104 " void a(int x) { \\\n" 5105 " b(); \\\n" 5106 " c(); \\\n" 5107 " d(); \\\n" 5108 " e(); \\\n" 5109 " f(); \\\n" 5110 " }\n" 5111 "#endif", 5112 Style); 5113 // Keep comments aligned with indented directives. These 5114 // tests cannot use verifyFormat because messUp manipulates leading 5115 // whitespace. 5116 { 5117 const char *Expected = "void f() {\n" 5118 "// Aligned to preprocessor.\n" 5119 "#if 1\n" 5120 " // Aligned to code.\n" 5121 " int a;\n" 5122 " #if 1\n" 5123 " // Aligned to preprocessor.\n" 5124 " #define A 0\n" 5125 " // Aligned to code.\n" 5126 " int b;\n" 5127 " #endif\n" 5128 "#endif\n" 5129 "}"; 5130 const char *ToFormat = "void f() {\n" 5131 "// Aligned to preprocessor.\n" 5132 "#if 1\n" 5133 "// Aligned to code.\n" 5134 "int a;\n" 5135 "#if 1\n" 5136 "// Aligned to preprocessor.\n" 5137 "#define A 0\n" 5138 "// Aligned to code.\n" 5139 "int b;\n" 5140 "#endif\n" 5141 "#endif\n" 5142 "}"; 5143 EXPECT_EQ(Expected, format(ToFormat, Style)); 5144 EXPECT_EQ(Expected, format(Expected, Style)); 5145 } 5146 { 5147 const char *Expected = "void f() {\n" 5148 "/* Aligned to preprocessor. */\n" 5149 "#if 1\n" 5150 " /* Aligned to code. */\n" 5151 " int a;\n" 5152 " #if 1\n" 5153 " /* Aligned to preprocessor. */\n" 5154 " #define A 0\n" 5155 " /* Aligned to code. */\n" 5156 " int b;\n" 5157 " #endif\n" 5158 "#endif\n" 5159 "}"; 5160 const char *ToFormat = "void f() {\n" 5161 "/* Aligned to preprocessor. */\n" 5162 "#if 1\n" 5163 "/* Aligned to code. */\n" 5164 "int a;\n" 5165 "#if 1\n" 5166 "/* Aligned to preprocessor. */\n" 5167 "#define A 0\n" 5168 "/* Aligned to code. */\n" 5169 "int b;\n" 5170 "#endif\n" 5171 "#endif\n" 5172 "}"; 5173 EXPECT_EQ(Expected, format(ToFormat, Style)); 5174 EXPECT_EQ(Expected, format(Expected, Style)); 5175 } 5176 5177 // Test single comment before preprocessor 5178 verifyFormat("// Comment\n" 5179 "\n" 5180 "#if 1\n" 5181 "#endif", 5182 Style); 5183 } 5184 5185 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 5186 verifyFormat("{\n { a #c; }\n}"); 5187 } 5188 5189 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 5190 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 5191 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 5192 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 5193 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 5194 } 5195 5196 TEST_F(FormatTest, EscapedNewlines) { 5197 FormatStyle Narrow = getLLVMStyleWithColumns(11); 5198 EXPECT_EQ("#define A \\\n int i; \\\n int j;", 5199 format("#define A \\\nint i;\\\n int j;", Narrow)); 5200 EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;")); 5201 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 5202 EXPECT_EQ("/* \\ \\ \\\n */", format("\\\n/* \\ \\ \\\n */")); 5203 EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>")); 5204 5205 FormatStyle AlignLeft = getLLVMStyle(); 5206 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 5207 EXPECT_EQ("#define MACRO(x) \\\n" 5208 "private: \\\n" 5209 " int x(int a);\n", 5210 format("#define MACRO(x) \\\n" 5211 "private: \\\n" 5212 " int x(int a);\n", 5213 AlignLeft)); 5214 5215 // CRLF line endings 5216 EXPECT_EQ("#define A \\\r\n int i; \\\r\n int j;", 5217 format("#define A \\\r\nint i;\\\r\n int j;", Narrow)); 5218 EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;")); 5219 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 5220 EXPECT_EQ("/* \\ \\ \\\r\n */", format("\\\r\n/* \\ \\ \\\r\n */")); 5221 EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>")); 5222 EXPECT_EQ("#define MACRO(x) \\\r\n" 5223 "private: \\\r\n" 5224 " int x(int a);\r\n", 5225 format("#define MACRO(x) \\\r\n" 5226 "private: \\\r\n" 5227 " int x(int a);\r\n", 5228 AlignLeft)); 5229 5230 FormatStyle DontAlign = getLLVMStyle(); 5231 DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 5232 DontAlign.MaxEmptyLinesToKeep = 3; 5233 // FIXME: can't use verifyFormat here because the newline before 5234 // "public:" is not inserted the first time it's reformatted 5235 EXPECT_EQ("#define A \\\n" 5236 " class Foo { \\\n" 5237 " void bar(); \\\n" 5238 "\\\n" 5239 "\\\n" 5240 "\\\n" 5241 " public: \\\n" 5242 " void baz(); \\\n" 5243 " };", 5244 format("#define A \\\n" 5245 " class Foo { \\\n" 5246 " void bar(); \\\n" 5247 "\\\n" 5248 "\\\n" 5249 "\\\n" 5250 " public: \\\n" 5251 " void baz(); \\\n" 5252 " };", 5253 DontAlign)); 5254 } 5255 5256 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 5257 verifyFormat("#define A \\\n" 5258 " int v( \\\n" 5259 " a); \\\n" 5260 " int i;", 5261 getLLVMStyleWithColumns(11)); 5262 } 5263 5264 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 5265 EXPECT_EQ( 5266 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 5267 " \\\n" 5268 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 5269 "\n" 5270 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 5271 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 5272 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 5273 "\\\n" 5274 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 5275 " \n" 5276 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 5277 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 5278 } 5279 5280 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 5281 EXPECT_EQ("int\n" 5282 "#define A\n" 5283 " a;", 5284 format("int\n#define A\na;")); 5285 verifyFormat("functionCallTo(\n" 5286 " someOtherFunction(\n" 5287 " withSomeParameters, whichInSequence,\n" 5288 " areLongerThanALine(andAnotherCall,\n" 5289 "#define A B\n" 5290 " withMoreParamters,\n" 5291 " whichStronglyInfluenceTheLayout),\n" 5292 " andMoreParameters),\n" 5293 " trailing);", 5294 getLLVMStyleWithColumns(69)); 5295 verifyFormat("Foo::Foo()\n" 5296 "#ifdef BAR\n" 5297 " : baz(0)\n" 5298 "#endif\n" 5299 "{\n" 5300 "}"); 5301 verifyFormat("void f() {\n" 5302 " if (true)\n" 5303 "#ifdef A\n" 5304 " f(42);\n" 5305 " x();\n" 5306 "#else\n" 5307 " g();\n" 5308 " x();\n" 5309 "#endif\n" 5310 "}"); 5311 verifyFormat("void f(param1, param2,\n" 5312 " param3,\n" 5313 "#ifdef A\n" 5314 " param4(param5,\n" 5315 "#ifdef A1\n" 5316 " param6,\n" 5317 "#ifdef A2\n" 5318 " param7),\n" 5319 "#else\n" 5320 " param8),\n" 5321 " param9,\n" 5322 "#endif\n" 5323 " param10,\n" 5324 "#endif\n" 5325 " param11)\n" 5326 "#else\n" 5327 " param12)\n" 5328 "#endif\n" 5329 "{\n" 5330 " x();\n" 5331 "}", 5332 getLLVMStyleWithColumns(28)); 5333 verifyFormat("#if 1\n" 5334 "int i;"); 5335 verifyFormat("#if 1\n" 5336 "#endif\n" 5337 "#if 1\n" 5338 "#else\n" 5339 "#endif\n"); 5340 verifyFormat("DEBUG({\n" 5341 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5342 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 5343 "});\n" 5344 "#if a\n" 5345 "#else\n" 5346 "#endif"); 5347 5348 verifyIncompleteFormat("void f(\n" 5349 "#if A\n" 5350 ");\n" 5351 "#else\n" 5352 "#endif"); 5353 } 5354 5355 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { 5356 verifyFormat("#endif\n" 5357 "#if B"); 5358 } 5359 5360 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { 5361 FormatStyle SingleLine = getLLVMStyle(); 5362 SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; 5363 verifyFormat("#if 0\n" 5364 "#elif 1\n" 5365 "#endif\n" 5366 "void foo() {\n" 5367 " if (test) foo2();\n" 5368 "}", 5369 SingleLine); 5370 } 5371 5372 TEST_F(FormatTest, LayoutBlockInsideParens) { 5373 verifyFormat("functionCall({ int i; });"); 5374 verifyFormat("functionCall({\n" 5375 " int i;\n" 5376 " int j;\n" 5377 "});"); 5378 verifyFormat("functionCall(\n" 5379 " {\n" 5380 " int i;\n" 5381 " int j;\n" 5382 " },\n" 5383 " aaaa, bbbb, cccc);"); 5384 verifyFormat("functionA(functionB({\n" 5385 " int i;\n" 5386 " int j;\n" 5387 " }),\n" 5388 " aaaa, bbbb, cccc);"); 5389 verifyFormat("functionCall(\n" 5390 " {\n" 5391 " int i;\n" 5392 " int j;\n" 5393 " },\n" 5394 " aaaa, bbbb, // comment\n" 5395 " cccc);"); 5396 verifyFormat("functionA(functionB({\n" 5397 " int i;\n" 5398 " int j;\n" 5399 " }),\n" 5400 " aaaa, bbbb, // comment\n" 5401 " cccc);"); 5402 verifyFormat("functionCall(aaaa, bbbb, { int i; });"); 5403 verifyFormat("functionCall(aaaa, bbbb, {\n" 5404 " int i;\n" 5405 " int j;\n" 5406 "});"); 5407 verifyFormat( 5408 "Aaa(\n" // FIXME: There shouldn't be a linebreak here. 5409 " {\n" 5410 " int i; // break\n" 5411 " },\n" 5412 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 5413 " ccccccccccccccccc));"); 5414 verifyFormat("DEBUG({\n" 5415 " if (a)\n" 5416 " f();\n" 5417 "});"); 5418 } 5419 5420 TEST_F(FormatTest, LayoutBlockInsideStatement) { 5421 EXPECT_EQ("SOME_MACRO { int i; }\n" 5422 "int i;", 5423 format(" SOME_MACRO {int i;} int i;")); 5424 } 5425 5426 TEST_F(FormatTest, LayoutNestedBlocks) { 5427 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 5428 " struct s {\n" 5429 " int i;\n" 5430 " };\n" 5431 " s kBitsToOs[] = {{10}};\n" 5432 " for (int i = 0; i < 10; ++i)\n" 5433 " return;\n" 5434 "}"); 5435 verifyFormat("call(parameter, {\n" 5436 " something();\n" 5437 " // Comment using all columns.\n" 5438 " somethingelse();\n" 5439 "});", 5440 getLLVMStyleWithColumns(40)); 5441 verifyFormat("DEBUG( //\n" 5442 " { f(); }, a);"); 5443 verifyFormat("DEBUG( //\n" 5444 " {\n" 5445 " f(); //\n" 5446 " },\n" 5447 " a);"); 5448 5449 EXPECT_EQ("call(parameter, {\n" 5450 " something();\n" 5451 " // Comment too\n" 5452 " // looooooooooong.\n" 5453 " somethingElse();\n" 5454 "});", 5455 format("call(parameter, {\n" 5456 " something();\n" 5457 " // Comment too looooooooooong.\n" 5458 " somethingElse();\n" 5459 "});", 5460 getLLVMStyleWithColumns(29))); 5461 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });")); 5462 EXPECT_EQ("DEBUG({ // comment\n" 5463 " int i;\n" 5464 "});", 5465 format("DEBUG({ // comment\n" 5466 "int i;\n" 5467 "});")); 5468 EXPECT_EQ("DEBUG({\n" 5469 " int i;\n" 5470 "\n" 5471 " // comment\n" 5472 " int j;\n" 5473 "});", 5474 format("DEBUG({\n" 5475 " int i;\n" 5476 "\n" 5477 " // comment\n" 5478 " int j;\n" 5479 "});")); 5480 5481 verifyFormat("DEBUG({\n" 5482 " if (a)\n" 5483 " return;\n" 5484 "});"); 5485 verifyGoogleFormat("DEBUG({\n" 5486 " if (a) return;\n" 5487 "});"); 5488 FormatStyle Style = getGoogleStyle(); 5489 Style.ColumnLimit = 45; 5490 verifyFormat("Debug(\n" 5491 " aaaaa,\n" 5492 " {\n" 5493 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" 5494 " },\n" 5495 " a);", 5496 Style); 5497 5498 verifyFormat("SomeFunction({MACRO({ return output; }), b});"); 5499 5500 verifyNoCrash("^{v^{a}}"); 5501 } 5502 5503 TEST_F(FormatTest, FormatNestedBlocksInMacros) { 5504 EXPECT_EQ("#define MACRO() \\\n" 5505 " Debug(aaa, /* force line break */ \\\n" 5506 " { \\\n" 5507 " int i; \\\n" 5508 " int j; \\\n" 5509 " })", 5510 format("#define MACRO() Debug(aaa, /* force line break */ \\\n" 5511 " { int i; int j; })", 5512 getGoogleStyle())); 5513 5514 EXPECT_EQ("#define A \\\n" 5515 " [] { \\\n" 5516 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 5517 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 5518 " }", 5519 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 5520 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 5521 getGoogleStyle())); 5522 } 5523 5524 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 5525 EXPECT_EQ("{}", format("{}")); 5526 verifyFormat("enum E {};"); 5527 verifyFormat("enum E {}"); 5528 FormatStyle Style = getLLVMStyle(); 5529 Style.SpaceInEmptyBlock = true; 5530 EXPECT_EQ("void f() { }", format("void f() {}", Style)); 5531 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 5532 EXPECT_EQ("while (true) { }", format("while (true) {}", Style)); 5533 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 5534 Style.BraceWrapping.BeforeElse = false; 5535 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 5536 verifyFormat("if (a)\n" 5537 "{\n" 5538 "} else if (b)\n" 5539 "{\n" 5540 "} else\n" 5541 "{ }", 5542 Style); 5543 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never; 5544 verifyFormat("if (a) {\n" 5545 "} else if (b) {\n" 5546 "} else {\n" 5547 "}", 5548 Style); 5549 Style.BraceWrapping.BeforeElse = true; 5550 verifyFormat("if (a) { }\n" 5551 "else if (b) { }\n" 5552 "else { }", 5553 Style); 5554 } 5555 5556 TEST_F(FormatTest, FormatBeginBlockEndMacros) { 5557 FormatStyle Style = getLLVMStyle(); 5558 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$"; 5559 Style.MacroBlockEnd = "^[A-Z_]+_END$"; 5560 verifyFormat("FOO_BEGIN\n" 5561 " FOO_ENTRY\n" 5562 "FOO_END", 5563 Style); 5564 verifyFormat("FOO_BEGIN\n" 5565 " NESTED_FOO_BEGIN\n" 5566 " NESTED_FOO_ENTRY\n" 5567 " NESTED_FOO_END\n" 5568 "FOO_END", 5569 Style); 5570 verifyFormat("FOO_BEGIN(Foo, Bar)\n" 5571 " int x;\n" 5572 " x = 1;\n" 5573 "FOO_END(Baz)", 5574 Style); 5575 } 5576 5577 //===----------------------------------------------------------------------===// 5578 // Line break tests. 5579 //===----------------------------------------------------------------------===// 5580 5581 TEST_F(FormatTest, PreventConfusingIndents) { 5582 verifyFormat( 5583 "void f() {\n" 5584 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 5585 " parameter, parameter, parameter)),\n" 5586 " SecondLongCall(parameter));\n" 5587 "}"); 5588 verifyFormat( 5589 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5590 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 5591 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5592 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 5593 verifyFormat( 5594 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5595 " [aaaaaaaaaaaaaaaaaaaaaaaa\n" 5596 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 5597 " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); 5598 verifyFormat( 5599 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 5600 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 5601 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 5602 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 5603 verifyFormat("int a = bbbb && ccc &&\n" 5604 " fffff(\n" 5605 "#define A Just forcing a new line\n" 5606 " ddd);"); 5607 } 5608 5609 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 5610 verifyFormat( 5611 "bool aaaaaaa =\n" 5612 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 5613 " bbbbbbbb();"); 5614 verifyFormat( 5615 "bool aaaaaaa =\n" 5616 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" 5617 " bbbbbbbb();"); 5618 5619 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 5620 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 5621 " ccccccccc == ddddddddddd;"); 5622 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 5623 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" 5624 " ccccccccc == ddddddddddd;"); 5625 verifyFormat( 5626 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 5627 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" 5628 " ccccccccc == ddddddddddd;"); 5629 5630 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 5631 " aaaaaa) &&\n" 5632 " bbbbbb && cccccc;"); 5633 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 5634 " aaaaaa) >>\n" 5635 " bbbbbb;"); 5636 verifyFormat("aa = Whitespaces.addUntouchableComment(\n" 5637 " SourceMgr.getSpellingColumnNumber(\n" 5638 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 5639 " 1);"); 5640 5641 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5642 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 5643 " cccccc) {\n}"); 5644 verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5645 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 5646 " cccccc) {\n}"); 5647 verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5648 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 5649 " cccccc) {\n}"); 5650 verifyFormat("b = a &&\n" 5651 " // Comment\n" 5652 " b.c && d;"); 5653 5654 // If the LHS of a comparison is not a binary expression itself, the 5655 // additional linebreak confuses many people. 5656 verifyFormat( 5657 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5658 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 5659 "}"); 5660 verifyFormat( 5661 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5662 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 5663 "}"); 5664 verifyFormat( 5665 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 5666 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 5667 "}"); 5668 verifyFormat( 5669 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5670 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n" 5671 "}"); 5672 // Even explicit parentheses stress the precedence enough to make the 5673 // additional break unnecessary. 5674 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5675 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 5676 "}"); 5677 // This cases is borderline, but with the indentation it is still readable. 5678 verifyFormat( 5679 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5680 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5681 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 5682 "}", 5683 getLLVMStyleWithColumns(75)); 5684 5685 // If the LHS is a binary expression, we should still use the additional break 5686 // as otherwise the formatting hides the operator precedence. 5687 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5688 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5689 " 5) {\n" 5690 "}"); 5691 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5692 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n" 5693 " 5) {\n" 5694 "}"); 5695 5696 FormatStyle OnePerLine = getLLVMStyle(); 5697 OnePerLine.BinPackParameters = false; 5698 verifyFormat( 5699 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5700 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 5701 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 5702 OnePerLine); 5703 5704 verifyFormat("int i = someFunction(aaaaaaa, 0)\n" 5705 " .aaa(aaaaaaaaaaaaa) *\n" 5706 " aaaaaaa +\n" 5707 " aaaaaaa;", 5708 getLLVMStyleWithColumns(40)); 5709 } 5710 5711 TEST_F(FormatTest, ExpressionIndentation) { 5712 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5713 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5714 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5715 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5716 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 5717 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 5718 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5719 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 5720 " ccccccccccccccccccccccccccccccccccccccccc;"); 5721 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5722 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5723 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5724 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 5725 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5726 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5727 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5728 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 5729 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 5730 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 5731 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5732 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 5733 verifyFormat("if () {\n" 5734 "} else if (aaaaa && bbbbb > // break\n" 5735 " ccccc) {\n" 5736 "}"); 5737 verifyFormat("if () {\n" 5738 "} else if constexpr (aaaaa && bbbbb > // break\n" 5739 " ccccc) {\n" 5740 "}"); 5741 verifyFormat("if () {\n" 5742 "} else if CONSTEXPR (aaaaa && bbbbb > // break\n" 5743 " ccccc) {\n" 5744 "}"); 5745 verifyFormat("if () {\n" 5746 "} else if (aaaaa &&\n" 5747 " bbbbb > // break\n" 5748 " ccccc &&\n" 5749 " ddddd) {\n" 5750 "}"); 5751 5752 // Presence of a trailing comment used to change indentation of b. 5753 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 5754 " b;\n" 5755 "return aaaaaaaaaaaaaaaaaaa +\n" 5756 " b; //", 5757 getLLVMStyleWithColumns(30)); 5758 } 5759 5760 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 5761 // Not sure what the best system is here. Like this, the LHS can be found 5762 // immediately above an operator (everything with the same or a higher 5763 // indent). The RHS is aligned right of the operator and so compasses 5764 // everything until something with the same indent as the operator is found. 5765 // FIXME: Is this a good system? 5766 FormatStyle Style = getLLVMStyle(); 5767 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 5768 verifyFormat( 5769 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5770 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5771 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5772 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5773 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5774 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5775 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5776 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5777 " > ccccccccccccccccccccccccccccccccccccccccc;", 5778 Style); 5779 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5780 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5781 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5782 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5783 Style); 5784 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5785 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5786 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5787 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5788 Style); 5789 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5790 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5791 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5792 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5793 Style); 5794 verifyFormat("if () {\n" 5795 "} else if (aaaaa\n" 5796 " && bbbbb // break\n" 5797 " > ccccc) {\n" 5798 "}", 5799 Style); 5800 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5801 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 5802 Style); 5803 verifyFormat("return (a)\n" 5804 " // comment\n" 5805 " + b;", 5806 Style); 5807 verifyFormat( 5808 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5809 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5810 " + cc;", 5811 Style); 5812 5813 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5814 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5815 Style); 5816 5817 // Forced by comments. 5818 verifyFormat( 5819 "unsigned ContentSize =\n" 5820 " sizeof(int16_t) // DWARF ARange version number\n" 5821 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 5822 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 5823 " + sizeof(int8_t); // Segment Size (in bytes)"); 5824 5825 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 5826 " == boost::fusion::at_c<1>(iiii).second;", 5827 Style); 5828 5829 Style.ColumnLimit = 60; 5830 verifyFormat("zzzzzzzzzz\n" 5831 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5832 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 5833 Style); 5834 5835 Style.ColumnLimit = 80; 5836 Style.IndentWidth = 4; 5837 Style.TabWidth = 4; 5838 Style.UseTab = FormatStyle::UT_Always; 5839 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5840 Style.AlignOperands = FormatStyle::OAS_DontAlign; 5841 EXPECT_EQ("return someVeryVeryLongConditionThatBarelyFitsOnALine\n" 5842 "\t&& (someOtherLongishConditionPart1\n" 5843 "\t\t|| someOtherEvenLongerNestedConditionPart2);", 5844 format("return someVeryVeryLongConditionThatBarelyFitsOnALine && " 5845 "(someOtherLongishConditionPart1 || " 5846 "someOtherEvenLongerNestedConditionPart2);", 5847 Style)); 5848 } 5849 5850 TEST_F(FormatTest, ExpressionIndentationStrictAlign) { 5851 FormatStyle Style = getLLVMStyle(); 5852 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 5853 Style.AlignOperands = FormatStyle::OAS_AlignAfterOperator; 5854 5855 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5856 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5857 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5858 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5859 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5860 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5861 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5862 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5863 " > ccccccccccccccccccccccccccccccccccccccccc;", 5864 Style); 5865 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5866 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5867 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5868 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5869 Style); 5870 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5871 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5872 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5873 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5874 Style); 5875 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5876 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5877 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5878 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 5879 Style); 5880 verifyFormat("if () {\n" 5881 "} else if (aaaaa\n" 5882 " && bbbbb // break\n" 5883 " > ccccc) {\n" 5884 "}", 5885 Style); 5886 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5887 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 5888 Style); 5889 verifyFormat("return (a)\n" 5890 " // comment\n" 5891 " + b;", 5892 Style); 5893 verifyFormat( 5894 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5895 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5896 " + cc;", 5897 Style); 5898 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 5899 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 5900 " : 3333333333333333;", 5901 Style); 5902 verifyFormat( 5903 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 5904 " : ccccccccccccccc ? dddddddddddddddddd\n" 5905 " : eeeeeeeeeeeeeeeeee)\n" 5906 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 5907 " : 3333333333333333;", 5908 Style); 5909 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5910 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 5911 Style); 5912 5913 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 5914 " == boost::fusion::at_c<1>(iiii).second;", 5915 Style); 5916 5917 Style.ColumnLimit = 60; 5918 verifyFormat("zzzzzzzzzzzzz\n" 5919 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5920 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 5921 Style); 5922 5923 // Forced by comments. 5924 Style.ColumnLimit = 80; 5925 verifyFormat( 5926 "unsigned ContentSize\n" 5927 " = sizeof(int16_t) // DWARF ARange version number\n" 5928 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 5929 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 5930 " + sizeof(int8_t); // Segment Size (in bytes)", 5931 Style); 5932 5933 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 5934 verifyFormat( 5935 "unsigned ContentSize =\n" 5936 " sizeof(int16_t) // DWARF ARange version number\n" 5937 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 5938 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 5939 " + sizeof(int8_t); // Segment Size (in bytes)", 5940 Style); 5941 5942 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 5943 verifyFormat( 5944 "unsigned ContentSize =\n" 5945 " sizeof(int16_t) // DWARF ARange version number\n" 5946 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 5947 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 5948 " + sizeof(int8_t); // Segment Size (in bytes)", 5949 Style); 5950 } 5951 5952 TEST_F(FormatTest, EnforcedOperatorWraps) { 5953 // Here we'd like to wrap after the || operators, but a comment is forcing an 5954 // earlier wrap. 5955 verifyFormat("bool x = aaaaa //\n" 5956 " || bbbbb\n" 5957 " //\n" 5958 " || cccc;"); 5959 } 5960 5961 TEST_F(FormatTest, NoOperandAlignment) { 5962 FormatStyle Style = getLLVMStyle(); 5963 Style.AlignOperands = FormatStyle::OAS_DontAlign; 5964 verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n" 5965 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5966 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5967 Style); 5968 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 5969 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5970 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5971 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5972 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5973 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5974 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5975 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5976 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5977 " > ccccccccccccccccccccccccccccccccccccccccc;", 5978 Style); 5979 5980 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5981 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5982 " + cc;", 5983 Style); 5984 verifyFormat("int a = aa\n" 5985 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5986 " * cccccccccccccccccccccccccccccccccccc;\n", 5987 Style); 5988 5989 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5990 verifyFormat("return (a > b\n" 5991 " // comment1\n" 5992 " // comment2\n" 5993 " || c);", 5994 Style); 5995 } 5996 5997 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { 5998 FormatStyle Style = getLLVMStyle(); 5999 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 6000 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 6001 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6002 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 6003 Style); 6004 } 6005 6006 TEST_F(FormatTest, AllowBinPackingInsideArguments) { 6007 FormatStyle Style = getLLVMStyleWithColumns(40); 6008 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 6009 Style.BinPackArguments = false; 6010 verifyFormat("void test() {\n" 6011 " someFunction(\n" 6012 " this + argument + is + quite\n" 6013 " + long + so + it + gets + wrapped\n" 6014 " + but + remains + bin - packed);\n" 6015 "}", 6016 Style); 6017 verifyFormat("void test() {\n" 6018 " someFunction(arg1,\n" 6019 " this + argument + is\n" 6020 " + quite + long + so\n" 6021 " + it + gets + wrapped\n" 6022 " + but + remains + bin\n" 6023 " - packed,\n" 6024 " arg3);\n" 6025 "}", 6026 Style); 6027 verifyFormat("void test() {\n" 6028 " someFunction(\n" 6029 " arg1,\n" 6030 " this + argument + has\n" 6031 " + anotherFunc(nested,\n" 6032 " calls + whose\n" 6033 " + arguments\n" 6034 " + are + also\n" 6035 " + wrapped,\n" 6036 " in + addition)\n" 6037 " + to + being + bin - packed,\n" 6038 " arg3);\n" 6039 "}", 6040 Style); 6041 6042 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 6043 verifyFormat("void test() {\n" 6044 " someFunction(\n" 6045 " arg1,\n" 6046 " this + argument + has +\n" 6047 " anotherFunc(nested,\n" 6048 " calls + whose +\n" 6049 " arguments +\n" 6050 " are + also +\n" 6051 " wrapped,\n" 6052 " in + addition) +\n" 6053 " to + being + bin - packed,\n" 6054 " arg3);\n" 6055 "}", 6056 Style); 6057 } 6058 6059 TEST_F(FormatTest, ConstructorInitializers) { 6060 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 6061 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 6062 getLLVMStyleWithColumns(45)); 6063 verifyFormat("Constructor()\n" 6064 " : Inttializer(FitsOnTheLine) {}", 6065 getLLVMStyleWithColumns(44)); 6066 verifyFormat("Constructor()\n" 6067 " : Inttializer(FitsOnTheLine) {}", 6068 getLLVMStyleWithColumns(43)); 6069 6070 verifyFormat("template <typename T>\n" 6071 "Constructor() : Initializer(FitsOnTheLine) {}", 6072 getLLVMStyleWithColumns(45)); 6073 6074 verifyFormat( 6075 "SomeClass::Constructor()\n" 6076 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 6077 6078 verifyFormat( 6079 "SomeClass::Constructor()\n" 6080 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6081 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 6082 verifyFormat( 6083 "SomeClass::Constructor()\n" 6084 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6085 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 6086 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6087 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6088 " : aaaaaaaaaa(aaaaaa) {}"); 6089 6090 verifyFormat("Constructor()\n" 6091 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6092 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6093 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6094 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 6095 6096 verifyFormat("Constructor()\n" 6097 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6098 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 6099 6100 verifyFormat("Constructor(int Parameter = 0)\n" 6101 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 6102 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 6103 verifyFormat("Constructor()\n" 6104 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 6105 "}", 6106 getLLVMStyleWithColumns(60)); 6107 verifyFormat("Constructor()\n" 6108 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6109 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 6110 6111 // Here a line could be saved by splitting the second initializer onto two 6112 // lines, but that is not desirable. 6113 verifyFormat("Constructor()\n" 6114 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 6115 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 6116 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 6117 6118 FormatStyle OnePerLine = getLLVMStyle(); 6119 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_Never; 6120 verifyFormat("MyClass::MyClass()\n" 6121 " : a(a),\n" 6122 " b(b),\n" 6123 " c(c) {}", 6124 OnePerLine); 6125 verifyFormat("MyClass::MyClass()\n" 6126 " : a(a), // comment\n" 6127 " b(b),\n" 6128 " c(c) {}", 6129 OnePerLine); 6130 verifyFormat("MyClass::MyClass(int a)\n" 6131 " : b(a), // comment\n" 6132 " c(a + 1) { // lined up\n" 6133 "}", 6134 OnePerLine); 6135 verifyFormat("Constructor()\n" 6136 " : a(b, b, b) {}", 6137 OnePerLine); 6138 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6139 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 6140 verifyFormat("SomeClass::Constructor()\n" 6141 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6142 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6143 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6144 OnePerLine); 6145 verifyFormat("SomeClass::Constructor()\n" 6146 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 6147 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6148 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6149 OnePerLine); 6150 verifyFormat("MyClass::MyClass(int var)\n" 6151 " : some_var_(var), // 4 space indent\n" 6152 " some_other_var_(var + 1) { // lined up\n" 6153 "}", 6154 OnePerLine); 6155 verifyFormat("Constructor()\n" 6156 " : aaaaa(aaaaaa),\n" 6157 " aaaaa(aaaaaa),\n" 6158 " aaaaa(aaaaaa),\n" 6159 " aaaaa(aaaaaa),\n" 6160 " aaaaa(aaaaaa) {}", 6161 OnePerLine); 6162 verifyFormat("Constructor()\n" 6163 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 6164 " aaaaaaaaaaaaaaaaaaaaaa) {}", 6165 OnePerLine); 6166 OnePerLine.BinPackParameters = false; 6167 verifyFormat( 6168 "Constructor()\n" 6169 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n" 6170 " aaaaaaaaaaa().aaa(),\n" 6171 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6172 OnePerLine); 6173 OnePerLine.ColumnLimit = 60; 6174 verifyFormat("Constructor()\n" 6175 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6176 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 6177 OnePerLine); 6178 6179 EXPECT_EQ("Constructor()\n" 6180 " : // Comment forcing unwanted break.\n" 6181 " aaaa(aaaa) {}", 6182 format("Constructor() :\n" 6183 " // Comment forcing unwanted break.\n" 6184 " aaaa(aaaa) {}")); 6185 } 6186 6187 TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) { 6188 FormatStyle Style = getLLVMStyleWithColumns(60); 6189 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 6190 Style.BinPackParameters = false; 6191 6192 for (int i = 0; i < 4; ++i) { 6193 // Test all combinations of parameters that should not have an effect. 6194 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1; 6195 Style.AllowAllArgumentsOnNextLine = i & 2; 6196 6197 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6198 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 6199 verifyFormat("Constructor()\n" 6200 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6201 Style); 6202 verifyFormat("Constructor() : a(a), b(b) {}", Style); 6203 6204 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6205 verifyFormat("Constructor()\n" 6206 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 6207 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 6208 Style); 6209 verifyFormat("Constructor() : a(a), b(b) {}", Style); 6210 6211 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 6212 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6213 verifyFormat("Constructor()\n" 6214 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6215 Style); 6216 6217 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6218 verifyFormat("Constructor()\n" 6219 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6220 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6221 Style); 6222 6223 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 6224 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6225 verifyFormat("Constructor() :\n" 6226 " aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6227 Style); 6228 6229 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6230 verifyFormat("Constructor() :\n" 6231 " aaaaaaaaaaaaaaaaaa(a),\n" 6232 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6233 Style); 6234 } 6235 6236 // Test interactions between AllowAllParametersOfDeclarationOnNextLine and 6237 // AllowAllConstructorInitializersOnNextLine in all 6238 // BreakConstructorInitializers modes 6239 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 6240 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6241 verifyFormat("SomeClassWithALongName::Constructor(\n" 6242 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n" 6243 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 6244 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 6245 Style); 6246 6247 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6248 verifyFormat("SomeClassWithALongName::Constructor(\n" 6249 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6250 " int bbbbbbbbbbbbb,\n" 6251 " int cccccccccccccccc)\n" 6252 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6253 Style); 6254 6255 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6256 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6257 verifyFormat("SomeClassWithALongName::Constructor(\n" 6258 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6259 " int bbbbbbbbbbbbb)\n" 6260 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 6261 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 6262 Style); 6263 6264 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 6265 6266 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6267 verifyFormat("SomeClassWithALongName::Constructor(\n" 6268 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n" 6269 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6270 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6271 Style); 6272 6273 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6274 verifyFormat("SomeClassWithALongName::Constructor(\n" 6275 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6276 " int bbbbbbbbbbbbb,\n" 6277 " int cccccccccccccccc)\n" 6278 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6279 Style); 6280 6281 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6282 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6283 verifyFormat("SomeClassWithALongName::Constructor(\n" 6284 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6285 " int bbbbbbbbbbbbb)\n" 6286 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 6287 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6288 Style); 6289 6290 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 6291 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6292 verifyFormat("SomeClassWithALongName::Constructor(\n" 6293 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n" 6294 " aaaaaaaaaaaaaaaaaaaa(a),\n" 6295 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6296 Style); 6297 6298 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6299 verifyFormat("SomeClassWithALongName::Constructor(\n" 6300 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6301 " int bbbbbbbbbbbbb,\n" 6302 " int cccccccccccccccc) :\n" 6303 " aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 6304 Style); 6305 6306 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6307 Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6308 verifyFormat("SomeClassWithALongName::Constructor(\n" 6309 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 6310 " int bbbbbbbbbbbbb) :\n" 6311 " aaaaaaaaaaaaaaaaaaaa(a),\n" 6312 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 6313 Style); 6314 } 6315 6316 TEST_F(FormatTest, AllowAllArgumentsOnNextLine) { 6317 FormatStyle Style = getLLVMStyleWithColumns(60); 6318 Style.BinPackArguments = false; 6319 for (int i = 0; i < 4; ++i) { 6320 // Test all combinations of parameters that should not have an effect. 6321 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1; 6322 Style.PackConstructorInitializers = 6323 i & 2 ? FormatStyle::PCIS_BinPack : FormatStyle::PCIS_Never; 6324 6325 Style.AllowAllArgumentsOnNextLine = true; 6326 verifyFormat("void foo() {\n" 6327 " FunctionCallWithReallyLongName(\n" 6328 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n" 6329 "}", 6330 Style); 6331 Style.AllowAllArgumentsOnNextLine = false; 6332 verifyFormat("void foo() {\n" 6333 " FunctionCallWithReallyLongName(\n" 6334 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6335 " bbbbbbbbbbbb);\n" 6336 "}", 6337 Style); 6338 6339 Style.AllowAllArgumentsOnNextLine = true; 6340 verifyFormat("void foo() {\n" 6341 " auto VariableWithReallyLongName = {\n" 6342 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n" 6343 "}", 6344 Style); 6345 Style.AllowAllArgumentsOnNextLine = false; 6346 verifyFormat("void foo() {\n" 6347 " auto VariableWithReallyLongName = {\n" 6348 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6349 " bbbbbbbbbbbb};\n" 6350 "}", 6351 Style); 6352 } 6353 6354 // This parameter should not affect declarations. 6355 Style.BinPackParameters = false; 6356 Style.AllowAllArgumentsOnNextLine = false; 6357 Style.AllowAllParametersOfDeclarationOnNextLine = true; 6358 verifyFormat("void FunctionCallWithReallyLongName(\n" 6359 " int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);", 6360 Style); 6361 Style.AllowAllParametersOfDeclarationOnNextLine = false; 6362 verifyFormat("void FunctionCallWithReallyLongName(\n" 6363 " int aaaaaaaaaaaaaaaaaaaaaaa,\n" 6364 " int bbbbbbbbbbbb);", 6365 Style); 6366 } 6367 6368 TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) { 6369 // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign 6370 // and BAS_Align. 6371 FormatStyle Style = getLLVMStyleWithColumns(35); 6372 StringRef Input = "functionCall(paramA, paramB, paramC);\n" 6373 "void functionDecl(int A, int B, int C);"; 6374 Style.AllowAllArgumentsOnNextLine = false; 6375 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6376 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 6377 " paramC);\n" 6378 "void functionDecl(int A, int B,\n" 6379 " int C);"), 6380 format(Input, Style)); 6381 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 6382 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 6383 " paramC);\n" 6384 "void functionDecl(int A, int B,\n" 6385 " int C);"), 6386 format(Input, Style)); 6387 // However, BAS_AlwaysBreak should take precedence over 6388 // AllowAllArgumentsOnNextLine. 6389 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 6390 EXPECT_EQ(StringRef("functionCall(\n" 6391 " paramA, paramB, paramC);\n" 6392 "void functionDecl(\n" 6393 " int A, int B, int C);"), 6394 format(Input, Style)); 6395 6396 // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the 6397 // first argument. 6398 Style.AllowAllArgumentsOnNextLine = true; 6399 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 6400 EXPECT_EQ(StringRef("functionCall(\n" 6401 " paramA, paramB, paramC);\n" 6402 "void functionDecl(\n" 6403 " int A, int B, int C);"), 6404 format(Input, Style)); 6405 // It wouldn't fit on one line with aligned parameters so this setting 6406 // doesn't change anything for BAS_Align. 6407 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 6408 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 6409 " paramC);\n" 6410 "void functionDecl(int A, int B,\n" 6411 " int C);"), 6412 format(Input, Style)); 6413 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6414 EXPECT_EQ(StringRef("functionCall(\n" 6415 " paramA, paramB, paramC);\n" 6416 "void functionDecl(\n" 6417 " int A, int B, int C);"), 6418 format(Input, Style)); 6419 } 6420 6421 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) { 6422 FormatStyle Style = getLLVMStyle(); 6423 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 6424 6425 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 6426 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}", 6427 getStyleWithColumns(Style, 45)); 6428 verifyFormat("Constructor() :\n" 6429 " Initializer(FitsOnTheLine) {}", 6430 getStyleWithColumns(Style, 44)); 6431 verifyFormat("Constructor() :\n" 6432 " Initializer(FitsOnTheLine) {}", 6433 getStyleWithColumns(Style, 43)); 6434 6435 verifyFormat("template <typename T>\n" 6436 "Constructor() : Initializer(FitsOnTheLine) {}", 6437 getStyleWithColumns(Style, 50)); 6438 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6439 verifyFormat( 6440 "SomeClass::Constructor() :\n" 6441 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 6442 Style); 6443 6444 Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack; 6445 verifyFormat( 6446 "SomeClass::Constructor() :\n" 6447 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 6448 Style); 6449 6450 verifyFormat( 6451 "SomeClass::Constructor() :\n" 6452 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6453 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6454 Style); 6455 verifyFormat( 6456 "SomeClass::Constructor() :\n" 6457 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6458 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 6459 Style); 6460 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6461 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 6462 " aaaaaaaaaa(aaaaaa) {}", 6463 Style); 6464 6465 verifyFormat("Constructor() :\n" 6466 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6467 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6468 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6469 " aaaaaaaaaaaaaaaaaaaaaaa() {}", 6470 Style); 6471 6472 verifyFormat("Constructor() :\n" 6473 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6474 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6475 Style); 6476 6477 verifyFormat("Constructor(int Parameter = 0) :\n" 6478 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 6479 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}", 6480 Style); 6481 verifyFormat("Constructor() :\n" 6482 " aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 6483 "}", 6484 getStyleWithColumns(Style, 60)); 6485 verifyFormat("Constructor() :\n" 6486 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6487 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}", 6488 Style); 6489 6490 // Here a line could be saved by splitting the second initializer onto two 6491 // lines, but that is not desirable. 6492 verifyFormat("Constructor() :\n" 6493 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 6494 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 6495 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6496 Style); 6497 6498 FormatStyle OnePerLine = Style; 6499 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine; 6500 verifyFormat("SomeClass::Constructor() :\n" 6501 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6502 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6503 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6504 OnePerLine); 6505 verifyFormat("SomeClass::Constructor() :\n" 6506 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 6507 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 6508 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 6509 OnePerLine); 6510 verifyFormat("MyClass::MyClass(int var) :\n" 6511 " some_var_(var), // 4 space indent\n" 6512 " some_other_var_(var + 1) { // lined up\n" 6513 "}", 6514 OnePerLine); 6515 verifyFormat("Constructor() :\n" 6516 " aaaaa(aaaaaa),\n" 6517 " aaaaa(aaaaaa),\n" 6518 " aaaaa(aaaaaa),\n" 6519 " aaaaa(aaaaaa),\n" 6520 " aaaaa(aaaaaa) {}", 6521 OnePerLine); 6522 verifyFormat("Constructor() :\n" 6523 " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 6524 " aaaaaaaaaaaaaaaaaaaaaa) {}", 6525 OnePerLine); 6526 OnePerLine.BinPackParameters = false; 6527 verifyFormat("Constructor() :\n" 6528 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 6529 " aaaaaaaaaaa().aaa(),\n" 6530 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6531 OnePerLine); 6532 OnePerLine.ColumnLimit = 60; 6533 verifyFormat("Constructor() :\n" 6534 " aaaaaaaaaaaaaaaaaaaa(a),\n" 6535 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 6536 OnePerLine); 6537 6538 EXPECT_EQ("Constructor() :\n" 6539 " // Comment forcing unwanted break.\n" 6540 " aaaa(aaaa) {}", 6541 format("Constructor() :\n" 6542 " // Comment forcing unwanted break.\n" 6543 " aaaa(aaaa) {}", 6544 Style)); 6545 6546 Style.ColumnLimit = 0; 6547 verifyFormat("SomeClass::Constructor() :\n" 6548 " a(a) {}", 6549 Style); 6550 verifyFormat("SomeClass::Constructor() noexcept :\n" 6551 " a(a) {}", 6552 Style); 6553 verifyFormat("SomeClass::Constructor() :\n" 6554 " a(a), b(b), c(c) {}", 6555 Style); 6556 verifyFormat("SomeClass::Constructor() :\n" 6557 " a(a) {\n" 6558 " foo();\n" 6559 " bar();\n" 6560 "}", 6561 Style); 6562 6563 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 6564 verifyFormat("SomeClass::Constructor() :\n" 6565 " a(a), b(b), c(c) {\n" 6566 "}", 6567 Style); 6568 verifyFormat("SomeClass::Constructor() :\n" 6569 " a(a) {\n" 6570 "}", 6571 Style); 6572 6573 Style.ColumnLimit = 80; 6574 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 6575 Style.ConstructorInitializerIndentWidth = 2; 6576 verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style); 6577 verifyFormat("SomeClass::Constructor() :\n" 6578 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6579 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}", 6580 Style); 6581 6582 // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as 6583 // well 6584 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 6585 verifyFormat( 6586 "class SomeClass\n" 6587 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6588 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 6589 Style); 6590 Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 6591 verifyFormat( 6592 "class SomeClass\n" 6593 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6594 " , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 6595 Style); 6596 Style.BreakInheritanceList = FormatStyle::BILS_AfterColon; 6597 verifyFormat( 6598 "class SomeClass :\n" 6599 " public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6600 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 6601 Style); 6602 Style.BreakInheritanceList = FormatStyle::BILS_AfterComma; 6603 verifyFormat( 6604 "class SomeClass\n" 6605 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6606 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 6607 Style); 6608 } 6609 6610 #ifndef EXPENSIVE_CHECKS 6611 // Expensive checks enables libstdc++ checking which includes validating the 6612 // state of ranges used in std::priority_queue - this blows out the 6613 // runtime/scalability of the function and makes this test unacceptably slow. 6614 TEST_F(FormatTest, MemoizationTests) { 6615 // This breaks if the memoization lookup does not take \c Indent and 6616 // \c LastSpace into account. 6617 verifyFormat( 6618 "extern CFRunLoopTimerRef\n" 6619 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 6620 " CFTimeInterval interval, CFOptionFlags flags,\n" 6621 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 6622 " CFRunLoopTimerContext *context) {}"); 6623 6624 // Deep nesting somewhat works around our memoization. 6625 verifyFormat( 6626 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 6627 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 6628 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 6629 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 6630 " aaaaa())))))))))))))))))))))))))))))))))))))));", 6631 getLLVMStyleWithColumns(65)); 6632 verifyFormat( 6633 "aaaaa(\n" 6634 " aaaaa,\n" 6635 " aaaaa(\n" 6636 " aaaaa,\n" 6637 " aaaaa(\n" 6638 " aaaaa,\n" 6639 " aaaaa(\n" 6640 " aaaaa,\n" 6641 " aaaaa(\n" 6642 " aaaaa,\n" 6643 " aaaaa(\n" 6644 " aaaaa,\n" 6645 " aaaaa(\n" 6646 " aaaaa,\n" 6647 " aaaaa(\n" 6648 " aaaaa,\n" 6649 " aaaaa(\n" 6650 " aaaaa,\n" 6651 " aaaaa(\n" 6652 " aaaaa,\n" 6653 " aaaaa(\n" 6654 " aaaaa,\n" 6655 " aaaaa(\n" 6656 " aaaaa,\n" 6657 " aaaaa))))))))))));", 6658 getLLVMStyleWithColumns(65)); 6659 verifyFormat( 6660 "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" 6661 " a),\n" 6662 " a),\n" 6663 " a),\n" 6664 " a),\n" 6665 " a),\n" 6666 " a),\n" 6667 " a),\n" 6668 " a),\n" 6669 " a),\n" 6670 " a),\n" 6671 " a),\n" 6672 " a),\n" 6673 " a),\n" 6674 " a),\n" 6675 " a),\n" 6676 " a),\n" 6677 " a)", 6678 getLLVMStyleWithColumns(65)); 6679 6680 // This test takes VERY long when memoization is broken. 6681 FormatStyle OnePerLine = getLLVMStyle(); 6682 OnePerLine.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 6683 OnePerLine.BinPackParameters = false; 6684 std::string input = "Constructor()\n" 6685 " : aaaa(a,\n"; 6686 for (unsigned i = 0, e = 80; i != e; ++i) { 6687 input += " a,\n"; 6688 } 6689 input += " a) {}"; 6690 verifyFormat(input, OnePerLine); 6691 } 6692 #endif 6693 6694 TEST_F(FormatTest, BreaksAsHighAsPossible) { 6695 verifyFormat( 6696 "void f() {\n" 6697 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 6698 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 6699 " f();\n" 6700 "}"); 6701 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 6702 " Intervals[i - 1].getRange().getLast()) {\n}"); 6703 } 6704 6705 TEST_F(FormatTest, BreaksFunctionDeclarations) { 6706 // Principially, we break function declarations in a certain order: 6707 // 1) break amongst arguments. 6708 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 6709 " Cccccccccccccc cccccccccccccc);"); 6710 verifyFormat("template <class TemplateIt>\n" 6711 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 6712 " TemplateIt *stop) {}"); 6713 6714 // 2) break after return type. 6715 verifyFormat( 6716 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6717 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", 6718 getGoogleStyle()); 6719 6720 // 3) break after (. 6721 verifyFormat( 6722 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 6723 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", 6724 getGoogleStyle()); 6725 6726 // 4) break before after nested name specifiers. 6727 verifyFormat( 6728 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6729 "SomeClasssssssssssssssssssssssssssssssssssssss::\n" 6730 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", 6731 getGoogleStyle()); 6732 6733 // However, there are exceptions, if a sufficient amount of lines can be 6734 // saved. 6735 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 6736 // more adjusting. 6737 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 6738 " Cccccccccccccc cccccccccc,\n" 6739 " Cccccccccccccc cccccccccc,\n" 6740 " Cccccccccccccc cccccccccc,\n" 6741 " Cccccccccccccc cccccccccc);"); 6742 verifyFormat( 6743 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6744 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6745 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6746 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", 6747 getGoogleStyle()); 6748 verifyFormat( 6749 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 6750 " Cccccccccccccc cccccccccc,\n" 6751 " Cccccccccccccc cccccccccc,\n" 6752 " Cccccccccccccc cccccccccc,\n" 6753 " Cccccccccccccc cccccccccc,\n" 6754 " Cccccccccccccc cccccccccc,\n" 6755 " Cccccccccccccc cccccccccc);"); 6756 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 6757 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6758 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6759 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 6760 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 6761 6762 // Break after multi-line parameters. 6763 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6764 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6765 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6766 " bbbb bbbb);"); 6767 verifyFormat("void SomeLoooooooooooongFunction(\n" 6768 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 6769 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6770 " int bbbbbbbbbbbbb);"); 6771 6772 // Treat overloaded operators like other functions. 6773 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 6774 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 6775 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 6776 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 6777 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 6778 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 6779 verifyGoogleFormat( 6780 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 6781 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 6782 verifyGoogleFormat( 6783 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 6784 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 6785 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6786 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 6787 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 6788 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 6789 verifyGoogleFormat( 6790 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" 6791 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6792 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); 6793 verifyGoogleFormat("template <typename T>\n" 6794 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6795 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n" 6796 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);"); 6797 6798 FormatStyle Style = getLLVMStyle(); 6799 Style.PointerAlignment = FormatStyle::PAS_Left; 6800 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6801 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", 6802 Style); 6803 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 6804 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6805 Style); 6806 } 6807 6808 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) { 6809 // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516: 6810 // Prefer keeping `::` followed by `operator` together. 6811 EXPECT_EQ("const aaaa::bbbbbbb &\n" 6812 "ccccccccc::operator++() {\n" 6813 " stuff();\n" 6814 "}", 6815 format("const aaaa::bbbbbbb\n" 6816 "&ccccccccc::operator++() { stuff(); }", 6817 getLLVMStyleWithColumns(40))); 6818 } 6819 6820 TEST_F(FormatTest, TrailingReturnType) { 6821 verifyFormat("auto foo() -> int;\n"); 6822 // correct trailing return type spacing 6823 verifyFormat("auto operator->() -> int;\n"); 6824 verifyFormat("auto operator++(int) -> int;\n"); 6825 6826 verifyFormat("struct S {\n" 6827 " auto bar() const -> int;\n" 6828 "};"); 6829 verifyFormat("template <size_t Order, typename T>\n" 6830 "auto load_img(const std::string &filename)\n" 6831 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 6832 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 6833 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 6834 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 6835 verifyFormat("template <typename T>\n" 6836 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" 6837 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); 6838 6839 // Not trailing return types. 6840 verifyFormat("void f() { auto a = b->c(); }"); 6841 verifyFormat("auto a = p->foo();"); 6842 verifyFormat("int a = p->foo();"); 6843 verifyFormat("auto lmbd = [] NOEXCEPT -> int { return 0; };"); 6844 } 6845 6846 TEST_F(FormatTest, DeductionGuides) { 6847 verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;"); 6848 verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;"); 6849 verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;"); 6850 verifyFormat( 6851 "template <class... T>\n" 6852 "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;"); 6853 verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;"); 6854 verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;"); 6855 verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;"); 6856 verifyFormat("template <class T> A() -> A<(3 < 2)>;"); 6857 verifyFormat("template <class T> A() -> A<((3) < (2))>;"); 6858 verifyFormat("template <class T> x() -> x<1>;"); 6859 verifyFormat("template <class T> explicit x(T &) -> x<1>;"); 6860 6861 // Ensure not deduction guides. 6862 verifyFormat("c()->f<int>();"); 6863 verifyFormat("x()->foo<1>;"); 6864 verifyFormat("x = p->foo<3>();"); 6865 verifyFormat("x()->x<1>();"); 6866 verifyFormat("x()->x<1>;"); 6867 } 6868 6869 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 6870 // Avoid breaking before trailing 'const' or other trailing annotations, if 6871 // they are not function-like. 6872 FormatStyle Style = getGoogleStyleWithColumns(47); 6873 verifyFormat("void someLongFunction(\n" 6874 " int someLoooooooooooooongParameter) const {\n}", 6875 getLLVMStyleWithColumns(47)); 6876 verifyFormat("LoooooongReturnType\n" 6877 "someLoooooooongFunction() const {}", 6878 getLLVMStyleWithColumns(47)); 6879 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 6880 " const {}", 6881 Style); 6882 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 6883 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 6884 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 6885 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 6886 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 6887 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 6888 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 6889 " aaaaaaaaaaa aaaaa) const override;"); 6890 verifyGoogleFormat( 6891 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 6892 " const override;"); 6893 6894 // Even if the first parameter has to be wrapped. 6895 verifyFormat("void someLongFunction(\n" 6896 " int someLongParameter) const {}", 6897 getLLVMStyleWithColumns(46)); 6898 verifyFormat("void someLongFunction(\n" 6899 " int someLongParameter) const {}", 6900 Style); 6901 verifyFormat("void someLongFunction(\n" 6902 " int someLongParameter) override {}", 6903 Style); 6904 verifyFormat("void someLongFunction(\n" 6905 " int someLongParameter) OVERRIDE {}", 6906 Style); 6907 verifyFormat("void someLongFunction(\n" 6908 " int someLongParameter) final {}", 6909 Style); 6910 verifyFormat("void someLongFunction(\n" 6911 " int someLongParameter) FINAL {}", 6912 Style); 6913 verifyFormat("void someLongFunction(\n" 6914 " int parameter) const override {}", 6915 Style); 6916 6917 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 6918 verifyFormat("void someLongFunction(\n" 6919 " int someLongParameter) const\n" 6920 "{\n" 6921 "}", 6922 Style); 6923 6924 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 6925 verifyFormat("void someLongFunction(\n" 6926 " int someLongParameter) const\n" 6927 " {\n" 6928 " }", 6929 Style); 6930 6931 // Unless these are unknown annotations. 6932 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 6933 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6934 " LONG_AND_UGLY_ANNOTATION;"); 6935 6936 // Breaking before function-like trailing annotations is fine to keep them 6937 // close to their arguments. 6938 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6939 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 6940 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 6941 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 6942 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 6943 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 6944 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 6945 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 6946 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 6947 6948 verifyFormat( 6949 "void aaaaaaaaaaaaaaaaaa()\n" 6950 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 6951 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 6952 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6953 " __attribute__((unused));"); 6954 verifyGoogleFormat( 6955 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6956 " GUARDED_BY(aaaaaaaaaaaa);"); 6957 verifyGoogleFormat( 6958 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6959 " GUARDED_BY(aaaaaaaaaaaa);"); 6960 verifyGoogleFormat( 6961 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 6962 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6963 verifyGoogleFormat( 6964 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 6965 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 6966 } 6967 6968 TEST_F(FormatTest, FunctionAnnotations) { 6969 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 6970 "int OldFunction(const string ¶meter) {}"); 6971 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 6972 "string OldFunction(const string ¶meter) {}"); 6973 verifyFormat("template <typename T>\n" 6974 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 6975 "string OldFunction(const string ¶meter) {}"); 6976 6977 // Not function annotations. 6978 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6979 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 6980 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" 6981 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); 6982 verifyFormat("MACRO(abc).function() // wrap\n" 6983 " << abc;"); 6984 verifyFormat("MACRO(abc)->function() // wrap\n" 6985 " << abc;"); 6986 verifyFormat("MACRO(abc)::function() // wrap\n" 6987 " << abc;"); 6988 } 6989 6990 TEST_F(FormatTest, BreaksDesireably) { 6991 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 6992 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 6993 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 6994 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6995 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 6996 "}"); 6997 6998 verifyFormat( 6999 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7000 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 7001 7002 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7003 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7004 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 7005 7006 verifyFormat( 7007 "aaaaaaaa(aaaaaaaaaaaaa,\n" 7008 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7009 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 7010 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7011 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 7012 7013 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7014 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7015 7016 verifyFormat( 7017 "void f() {\n" 7018 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 7019 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 7020 "}"); 7021 verifyFormat( 7022 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7023 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 7024 verifyFormat( 7025 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7026 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 7027 verifyFormat( 7028 "aaaaaa(aaa,\n" 7029 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7030 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7031 " aaaa);"); 7032 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7033 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7034 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7035 7036 // Indent consistently independent of call expression and unary operator. 7037 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 7038 " dddddddddddddddddddddddddddddd));"); 7039 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 7040 " dddddddddddddddddddddddddddddd));"); 7041 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 7042 " dddddddddddddddddddddddddddddd));"); 7043 7044 // This test case breaks on an incorrect memoization, i.e. an optimization not 7045 // taking into account the StopAt value. 7046 verifyFormat( 7047 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 7048 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 7049 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 7050 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7051 7052 verifyFormat("{\n {\n {\n" 7053 " Annotation.SpaceRequiredBefore =\n" 7054 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 7055 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 7056 " }\n }\n}"); 7057 7058 // Break on an outer level if there was a break on an inner level. 7059 EXPECT_EQ("f(g(h(a, // comment\n" 7060 " b, c),\n" 7061 " d, e),\n" 7062 " x, y);", 7063 format("f(g(h(a, // comment\n" 7064 " b, c), d, e), x, y);")); 7065 7066 // Prefer breaking similar line breaks. 7067 verifyFormat( 7068 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 7069 " NSTrackingMouseEnteredAndExited |\n" 7070 " NSTrackingActiveAlways;"); 7071 } 7072 7073 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 7074 FormatStyle NoBinPacking = getGoogleStyle(); 7075 NoBinPacking.BinPackParameters = false; 7076 NoBinPacking.BinPackArguments = true; 7077 verifyFormat("void f() {\n" 7078 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 7079 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 7080 "}", 7081 NoBinPacking); 7082 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 7083 " int aaaaaaaaaaaaaaaaaaaa,\n" 7084 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 7085 NoBinPacking); 7086 7087 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 7088 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7089 " vector<int> bbbbbbbbbbbbbbb);", 7090 NoBinPacking); 7091 // FIXME: This behavior difference is probably not wanted. However, currently 7092 // we cannot distinguish BreakBeforeParameter being set because of the wrapped 7093 // template arguments from BreakBeforeParameter being set because of the 7094 // one-per-line formatting. 7095 verifyFormat( 7096 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n" 7097 " aaaaaaaaaa> aaaaaaaaaa);", 7098 NoBinPacking); 7099 verifyFormat( 7100 "void fffffffffff(\n" 7101 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n" 7102 " aaaaaaaaaa);"); 7103 } 7104 7105 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 7106 FormatStyle NoBinPacking = getGoogleStyle(); 7107 NoBinPacking.BinPackParameters = false; 7108 NoBinPacking.BinPackArguments = false; 7109 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 7110 " aaaaaaaaaaaaaaaaaaaa,\n" 7111 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 7112 NoBinPacking); 7113 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 7114 " aaaaaaaaaaaaa,\n" 7115 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 7116 NoBinPacking); 7117 verifyFormat( 7118 "aaaaaaaa(aaaaaaaaaaaaa,\n" 7119 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7120 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 7121 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7122 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 7123 NoBinPacking); 7124 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 7125 " .aaaaaaaaaaaaaaaaaa();", 7126 NoBinPacking); 7127 verifyFormat("void f() {\n" 7128 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7129 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 7130 "}", 7131 NoBinPacking); 7132 7133 verifyFormat( 7134 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7135 " aaaaaaaaaaaa,\n" 7136 " aaaaaaaaaaaa);", 7137 NoBinPacking); 7138 verifyFormat( 7139 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 7140 " ddddddddddddddddddddddddddddd),\n" 7141 " test);", 7142 NoBinPacking); 7143 7144 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 7145 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 7146 " aaaaaaaaaaaaaaaaaaaaaaa>\n" 7147 " aaaaaaaaaaaaaaaaaa;", 7148 NoBinPacking); 7149 verifyFormat("a(\"a\"\n" 7150 " \"a\",\n" 7151 " a);"); 7152 7153 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 7154 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 7155 " aaaaaaaaa,\n" 7156 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7157 NoBinPacking); 7158 verifyFormat( 7159 "void f() {\n" 7160 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 7161 " .aaaaaaa();\n" 7162 "}", 7163 NoBinPacking); 7164 verifyFormat( 7165 "template <class SomeType, class SomeOtherType>\n" 7166 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 7167 NoBinPacking); 7168 } 7169 7170 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 7171 FormatStyle Style = getLLVMStyleWithColumns(15); 7172 Style.ExperimentalAutoDetectBinPacking = true; 7173 EXPECT_EQ("aaa(aaaa,\n" 7174 " aaaa,\n" 7175 " aaaa);\n" 7176 "aaa(aaaa,\n" 7177 " aaaa,\n" 7178 " aaaa);", 7179 format("aaa(aaaa,\n" // one-per-line 7180 " aaaa,\n" 7181 " aaaa );\n" 7182 "aaa(aaaa, aaaa, aaaa);", // inconclusive 7183 Style)); 7184 EXPECT_EQ("aaa(aaaa, aaaa,\n" 7185 " aaaa);\n" 7186 "aaa(aaaa, aaaa,\n" 7187 " aaaa);", 7188 format("aaa(aaaa, aaaa,\n" // bin-packed 7189 " aaaa );\n" 7190 "aaa(aaaa, aaaa, aaaa);", // inconclusive 7191 Style)); 7192 } 7193 7194 TEST_F(FormatTest, FormatsBuilderPattern) { 7195 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 7196 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 7197 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 7198 " .StartsWith(\".init\", ORDER_INIT)\n" 7199 " .StartsWith(\".fini\", ORDER_FINI)\n" 7200 " .StartsWith(\".hash\", ORDER_HASH)\n" 7201 " .Default(ORDER_TEXT);\n"); 7202 7203 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 7204 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 7205 verifyFormat("aaaaaaa->aaaaaaa\n" 7206 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7207 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7208 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 7209 verifyFormat( 7210 "aaaaaaa->aaaaaaa\n" 7211 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7212 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 7213 verifyFormat( 7214 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 7215 " aaaaaaaaaaaaaa);"); 7216 verifyFormat( 7217 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 7218 " aaaaaa->aaaaaaaaaaaa()\n" 7219 " ->aaaaaaaaaaaaaaaa(\n" 7220 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7221 " ->aaaaaaaaaaaaaaaaa();"); 7222 verifyGoogleFormat( 7223 "void f() {\n" 7224 " someo->Add((new util::filetools::Handler(dir))\n" 7225 " ->OnEvent1(NewPermanentCallback(\n" 7226 " this, &HandlerHolderClass::EventHandlerCBA))\n" 7227 " ->OnEvent2(NewPermanentCallback(\n" 7228 " this, &HandlerHolderClass::EventHandlerCBB))\n" 7229 " ->OnEvent3(NewPermanentCallback(\n" 7230 " this, &HandlerHolderClass::EventHandlerCBC))\n" 7231 " ->OnEvent5(NewPermanentCallback(\n" 7232 " this, &HandlerHolderClass::EventHandlerCBD))\n" 7233 " ->OnEvent6(NewPermanentCallback(\n" 7234 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 7235 "}"); 7236 7237 verifyFormat( 7238 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 7239 verifyFormat("aaaaaaaaaaaaaaa()\n" 7240 " .aaaaaaaaaaaaaaa()\n" 7241 " .aaaaaaaaaaaaaaa()\n" 7242 " .aaaaaaaaaaaaaaa()\n" 7243 " .aaaaaaaaaaaaaaa();"); 7244 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 7245 " .aaaaaaaaaaaaaaa()\n" 7246 " .aaaaaaaaaaaaaaa()\n" 7247 " .aaaaaaaaaaaaaaa();"); 7248 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 7249 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 7250 " .aaaaaaaaaaaaaaa();"); 7251 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 7252 " ->aaaaaaaaaaaaaae(0)\n" 7253 " ->aaaaaaaaaaaaaaa();"); 7254 7255 // Don't linewrap after very short segments. 7256 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7257 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7258 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7259 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7260 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7261 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7262 verifyFormat("aaa()\n" 7263 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7264 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7265 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7266 7267 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 7268 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7269 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 7270 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 7271 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 7272 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 7273 7274 // Prefer not to break after empty parentheses. 7275 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 7276 " First->LastNewlineOffset);"); 7277 7278 // Prefer not to create "hanging" indents. 7279 verifyFormat( 7280 "return !soooooooooooooome_map\n" 7281 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7282 " .second;"); 7283 verifyFormat( 7284 "return aaaaaaaaaaaaaaaa\n" 7285 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n" 7286 " .aaaa(aaaaaaaaaaaaaa);"); 7287 // No hanging indent here. 7288 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n" 7289 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7290 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n" 7291 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7292 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 7293 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7294 getLLVMStyleWithColumns(60)); 7295 verifyFormat("aaaaaaaaaaaaaaaaaa\n" 7296 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 7297 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7298 getLLVMStyleWithColumns(59)); 7299 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7300 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7301 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7302 7303 // Dont break if only closing statements before member call 7304 verifyFormat("test() {\n" 7305 " ([]() -> {\n" 7306 " int b = 32;\n" 7307 " return 3;\n" 7308 " }).foo();\n" 7309 "}"); 7310 verifyFormat("test() {\n" 7311 " (\n" 7312 " []() -> {\n" 7313 " int b = 32;\n" 7314 " return 3;\n" 7315 " },\n" 7316 " foo, bar)\n" 7317 " .foo();\n" 7318 "}"); 7319 verifyFormat("test() {\n" 7320 " ([]() -> {\n" 7321 " int b = 32;\n" 7322 " return 3;\n" 7323 " })\n" 7324 " .foo()\n" 7325 " .bar();\n" 7326 "}"); 7327 verifyFormat("test() {\n" 7328 " ([]() -> {\n" 7329 " int b = 32;\n" 7330 " return 3;\n" 7331 " })\n" 7332 " .foo(\"aaaaaaaaaaaaaaaaa\"\n" 7333 " \"bbbb\");\n" 7334 "}", 7335 getLLVMStyleWithColumns(30)); 7336 } 7337 7338 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 7339 verifyFormat( 7340 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 7341 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 7342 verifyFormat( 7343 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 7344 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 7345 7346 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 7347 " ccccccccccccccccccccccccc) {\n}"); 7348 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 7349 " ccccccccccccccccccccccccc) {\n}"); 7350 7351 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 7352 " ccccccccccccccccccccccccc) {\n}"); 7353 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 7354 " ccccccccccccccccccccccccc) {\n}"); 7355 7356 verifyFormat( 7357 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 7358 " ccccccccccccccccccccccccc) {\n}"); 7359 verifyFormat( 7360 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 7361 " ccccccccccccccccccccccccc) {\n}"); 7362 7363 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 7364 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 7365 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 7366 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 7367 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 7368 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 7369 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 7370 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 7371 7372 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 7373 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 7374 " aaaaaaaaaaaaaaa != aa) {\n}"); 7375 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 7376 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 7377 " aaaaaaaaaaaaaaa != aa) {\n}"); 7378 } 7379 7380 TEST_F(FormatTest, BreaksAfterAssignments) { 7381 verifyFormat( 7382 "unsigned Cost =\n" 7383 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 7384 " SI->getPointerAddressSpaceee());\n"); 7385 verifyFormat( 7386 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 7387 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 7388 7389 verifyFormat( 7390 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 7391 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 7392 verifyFormat("unsigned OriginalStartColumn =\n" 7393 " SourceMgr.getSpellingColumnNumber(\n" 7394 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 7395 " 1;"); 7396 } 7397 7398 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) { 7399 FormatStyle Style = getLLVMStyle(); 7400 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 7401 " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;", 7402 Style); 7403 7404 Style.PenaltyBreakAssignment = 20; 7405 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 7406 " cccccccccccccccccccccccccc;", 7407 Style); 7408 } 7409 7410 TEST_F(FormatTest, AlignsAfterAssignments) { 7411 verifyFormat( 7412 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7413 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7414 verifyFormat( 7415 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7416 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7417 verifyFormat( 7418 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7419 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7420 verifyFormat( 7421 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7422 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 7423 verifyFormat( 7424 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 7425 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 7426 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 7427 } 7428 7429 TEST_F(FormatTest, AlignsAfterReturn) { 7430 verifyFormat( 7431 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7432 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 7433 verifyFormat( 7434 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7435 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 7436 verifyFormat( 7437 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 7438 " aaaaaaaaaaaaaaaaaaaaaa();"); 7439 verifyFormat( 7440 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 7441 " aaaaaaaaaaaaaaaaaaaaaa());"); 7442 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7443 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7444 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7445 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 7446 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7447 verifyFormat("return\n" 7448 " // true if code is one of a or b.\n" 7449 " code == a || code == b;"); 7450 } 7451 7452 TEST_F(FormatTest, AlignsAfterOpenBracket) { 7453 verifyFormat( 7454 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 7455 " aaaaaaaaa aaaaaaa) {}"); 7456 verifyFormat( 7457 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 7458 " aaaaaaaaaaa aaaaaaaaa);"); 7459 verifyFormat( 7460 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 7461 " aaaaaaaaaaaaaaaaaaaaa));"); 7462 FormatStyle Style = getLLVMStyle(); 7463 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7464 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7465 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 7466 Style); 7467 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 7468 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 7469 Style); 7470 verifyFormat("SomeLongVariableName->someFunction(\n" 7471 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 7472 Style); 7473 verifyFormat( 7474 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 7475 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 7476 Style); 7477 verifyFormat( 7478 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 7479 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7480 Style); 7481 verifyFormat( 7482 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 7483 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 7484 Style); 7485 7486 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n" 7487 " ccccccc(aaaaaaaaaaaaaaaaa, //\n" 7488 " b));", 7489 Style); 7490 7491 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 7492 Style.BinPackArguments = false; 7493 Style.BinPackParameters = false; 7494 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7495 " aaaaaaaaaaa aaaaaaaa,\n" 7496 " aaaaaaaaa aaaaaaa,\n" 7497 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 7498 Style); 7499 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 7500 " aaaaaaaaaaa aaaaaaaaa,\n" 7501 " aaaaaaaaaaa aaaaaaaaa,\n" 7502 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7503 Style); 7504 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" 7505 " aaaaaaaaaaaaaaa,\n" 7506 " aaaaaaaaaaaaaaaaaaaaa,\n" 7507 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 7508 Style); 7509 verifyFormat( 7510 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" 7511 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 7512 Style); 7513 verifyFormat( 7514 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" 7515 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 7516 Style); 7517 verifyFormat( 7518 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 7519 " aaaaaaaaaaaaaaaaaaaaa(\n" 7520 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n" 7521 " aaaaaaaaaaaaaaaa);", 7522 Style); 7523 verifyFormat( 7524 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 7525 " aaaaaaaaaaaaaaaaaaaaa(\n" 7526 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n" 7527 " aaaaaaaaaaaaaaaa);", 7528 Style); 7529 } 7530 7531 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 7532 FormatStyle Style = getLLVMStyleWithColumns(40); 7533 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7534 " bbbbbbbbbbbbbbbbbbbbbb);", 7535 Style); 7536 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 7537 Style.AlignOperands = FormatStyle::OAS_DontAlign; 7538 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7539 " bbbbbbbbbbbbbbbbbbbbbb);", 7540 Style); 7541 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7542 Style.AlignOperands = FormatStyle::OAS_Align; 7543 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7544 " bbbbbbbbbbbbbbbbbbbbbb);", 7545 Style); 7546 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7547 Style.AlignOperands = FormatStyle::OAS_DontAlign; 7548 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 7549 " bbbbbbbbbbbbbbbbbbbbbb);", 7550 Style); 7551 } 7552 7553 TEST_F(FormatTest, BreaksConditionalExpressions) { 7554 verifyFormat( 7555 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7556 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7557 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7558 verifyFormat( 7559 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 7560 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7561 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7562 verifyFormat( 7563 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7564 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7565 verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n" 7566 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7567 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7568 verifyFormat( 7569 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 7570 " : aaaaaaaaaaaaa);"); 7571 verifyFormat( 7572 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7573 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7574 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7575 " aaaaaaaaaaaaa);"); 7576 verifyFormat( 7577 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7578 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7579 " aaaaaaaaaaaaa);"); 7580 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7581 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7582 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7583 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7584 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7585 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7586 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7587 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7588 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7589 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7590 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7591 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7592 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7593 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7594 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7595 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7596 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7597 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7598 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7599 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7600 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 7601 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7602 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7603 " : aaaaaaaaaaaaaaaa;"); 7604 verifyFormat( 7605 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7606 " ? aaaaaaaaaaaaaaa\n" 7607 " : aaaaaaaaaaaaaaa;"); 7608 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 7609 " aaaaaaaaa\n" 7610 " ? b\n" 7611 " : c);"); 7612 verifyFormat("return aaaa == bbbb\n" 7613 " // comment\n" 7614 " ? aaaa\n" 7615 " : bbbb;"); 7616 verifyFormat("unsigned Indent =\n" 7617 " format(TheLine.First,\n" 7618 " IndentForLevel[TheLine.Level] >= 0\n" 7619 " ? IndentForLevel[TheLine.Level]\n" 7620 " : TheLine * 2,\n" 7621 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 7622 getLLVMStyleWithColumns(60)); 7623 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 7624 " ? aaaaaaaaaaaaaaa\n" 7625 " : bbbbbbbbbbbbbbb //\n" 7626 " ? ccccccccccccccc\n" 7627 " : ddddddddddddddd;"); 7628 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 7629 " ? aaaaaaaaaaaaaaa\n" 7630 " : (bbbbbbbbbbbbbbb //\n" 7631 " ? ccccccccccccccc\n" 7632 " : ddddddddddddddd);"); 7633 verifyFormat( 7634 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7635 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 7636 " aaaaaaaaaaaaaaaaaaaaa +\n" 7637 " aaaaaaaaaaaaaaaaaaaaa\n" 7638 " : aaaaaaaaaa;"); 7639 verifyFormat( 7640 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7641 " : aaaaaaaaaaaaaaaaaaaaaa\n" 7642 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7643 7644 FormatStyle NoBinPacking = getLLVMStyle(); 7645 NoBinPacking.BinPackArguments = false; 7646 verifyFormat( 7647 "void f() {\n" 7648 " g(aaa,\n" 7649 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 7650 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7651 " ? aaaaaaaaaaaaaaa\n" 7652 " : aaaaaaaaaaaaaaa);\n" 7653 "}", 7654 NoBinPacking); 7655 verifyFormat( 7656 "void f() {\n" 7657 " g(aaa,\n" 7658 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 7659 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7660 " ?: aaaaaaaaaaaaaaa);\n" 7661 "}", 7662 NoBinPacking); 7663 7664 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 7665 " // comment.\n" 7666 " ccccccccccccccccccccccccccccccccccccccc\n" 7667 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7668 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 7669 7670 // Assignments in conditional expressions. Apparently not uncommon :-(. 7671 verifyFormat("return a != b\n" 7672 " // comment\n" 7673 " ? a = b\n" 7674 " : a = b;"); 7675 verifyFormat("return a != b\n" 7676 " // comment\n" 7677 " ? a = a != b\n" 7678 " // comment\n" 7679 " ? a = b\n" 7680 " : a\n" 7681 " : a;\n"); 7682 verifyFormat("return a != b\n" 7683 " // comment\n" 7684 " ? a\n" 7685 " : a = a != b\n" 7686 " // comment\n" 7687 " ? a = b\n" 7688 " : a;"); 7689 7690 // Chained conditionals 7691 FormatStyle Style = getLLVMStyleWithColumns(70); 7692 Style.AlignOperands = FormatStyle::OAS_Align; 7693 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7694 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7695 " : 3333333333333333;", 7696 Style); 7697 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7698 " : bbbbbbbbbb ? 2222222222222222\n" 7699 " : 3333333333333333;", 7700 Style); 7701 verifyFormat("return aaaaaaaaaa ? 1111111111111111\n" 7702 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 7703 " : 3333333333333333;", 7704 Style); 7705 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7706 " : bbbbbbbbbbbbbb ? 222222\n" 7707 " : 333333;", 7708 Style); 7709 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7710 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7711 " : cccccccccccccc ? 3333333333333333\n" 7712 " : 4444444444444444;", 7713 Style); 7714 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc)\n" 7715 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7716 " : 3333333333333333;", 7717 Style); 7718 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7719 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7720 " : (aaa ? bbb : ccc);", 7721 Style); 7722 verifyFormat( 7723 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7724 " : cccccccccccccccccc)\n" 7725 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7726 " : 3333333333333333;", 7727 Style); 7728 verifyFormat( 7729 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7730 " : cccccccccccccccccc)\n" 7731 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7732 " : 3333333333333333;", 7733 Style); 7734 verifyFormat( 7735 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7736 " : dddddddddddddddddd)\n" 7737 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7738 " : 3333333333333333;", 7739 Style); 7740 verifyFormat( 7741 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7742 " : dddddddddddddddddd)\n" 7743 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7744 " : 3333333333333333;", 7745 Style); 7746 verifyFormat( 7747 "return aaaaaaaaa ? 1111111111111111\n" 7748 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7749 " : a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7750 " : dddddddddddddddddd)\n", 7751 Style); 7752 verifyFormat( 7753 "return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 7754 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7755 " : (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7756 " : cccccccccccccccccc);", 7757 Style); 7758 verifyFormat( 7759 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7760 " : ccccccccccccccc ? dddddddddddddddddd\n" 7761 " : eeeeeeeeeeeeeeeeee)\n" 7762 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7763 " : 3333333333333333;", 7764 Style); 7765 verifyFormat( 7766 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7767 " : ccccccccccccccc ? dddddddddddddddddd\n" 7768 " : eeeeeeeeeeeeeeeeee)\n" 7769 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7770 " : 3333333333333333;", 7771 Style); 7772 verifyFormat( 7773 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7774 " : cccccccccccc ? dddddddddddddddddd\n" 7775 " : eeeeeeeeeeeeeeeeee)\n" 7776 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7777 " : 3333333333333333;", 7778 Style); 7779 verifyFormat( 7780 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7781 " : cccccccccccccccccc\n" 7782 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7783 " : 3333333333333333;", 7784 Style); 7785 verifyFormat( 7786 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7787 " : cccccccccccccccc ? dddddddddddddddddd\n" 7788 " : eeeeeeeeeeeeeeeeee\n" 7789 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 7790 " : 3333333333333333;", 7791 Style); 7792 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa\n" 7793 " ? (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7794 " : cccccccccccccccccc ? dddddddddddddddddd\n" 7795 " : eeeeeeeeeeeeeeeeee)\n" 7796 " : bbbbbbbbbbbbbbbbbbb ? 2222222222222222\n" 7797 " : 3333333333333333;", 7798 Style); 7799 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaa\n" 7800 " ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 7801 " : cccccccccccccccc ? dddddddddddddddddd\n" 7802 " : eeeeeeeeeeeeeeeeee\n" 7803 " : bbbbbbbbbbbbbbbbbbbbbbb ? 2222222222222222\n" 7804 " : 3333333333333333;", 7805 Style); 7806 7807 Style.AlignOperands = FormatStyle::OAS_DontAlign; 7808 Style.BreakBeforeTernaryOperators = false; 7809 // FIXME: Aligning the question marks is weird given DontAlign. 7810 // Consider disabling this alignment in this case. Also check whether this 7811 // will render the adjustment from https://reviews.llvm.org/D82199 7812 // unnecessary. 7813 verifyFormat("int x = aaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa :\n" 7814 " bbbb ? cccccccccccccccccc :\n" 7815 " ddddd;\n", 7816 Style); 7817 7818 EXPECT_EQ( 7819 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n" 7820 " /*\n" 7821 " */\n" 7822 " function() {\n" 7823 " try {\n" 7824 " return JJJJJJJJJJJJJJ(\n" 7825 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n" 7826 " }\n" 7827 " } :\n" 7828 " function() {};", 7829 format( 7830 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n" 7831 " /*\n" 7832 " */\n" 7833 " function() {\n" 7834 " try {\n" 7835 " return JJJJJJJJJJJJJJ(\n" 7836 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n" 7837 " }\n" 7838 " } :\n" 7839 " function() {};", 7840 getGoogleStyle(FormatStyle::LK_JavaScript))); 7841 } 7842 7843 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 7844 FormatStyle Style = getLLVMStyleWithColumns(70); 7845 Style.BreakBeforeTernaryOperators = false; 7846 verifyFormat( 7847 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7848 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7849 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7850 Style); 7851 verifyFormat( 7852 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 7853 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7854 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7855 Style); 7856 verifyFormat( 7857 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7858 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7859 Style); 7860 verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n" 7861 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7862 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7863 Style); 7864 verifyFormat( 7865 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 7866 " aaaaaaaaaaaaa);", 7867 Style); 7868 verifyFormat( 7869 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7870 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7871 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7872 " aaaaaaaaaaaaa);", 7873 Style); 7874 verifyFormat( 7875 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7876 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7877 " aaaaaaaaaaaaa);", 7878 Style); 7879 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7880 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7881 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 7882 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7883 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7884 Style); 7885 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7886 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7887 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7888 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 7889 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7890 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7891 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7892 Style); 7893 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7894 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 7895 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7896 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 7897 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7898 Style); 7899 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7900 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7901 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 7902 Style); 7903 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 7904 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7905 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 7906 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 7907 Style); 7908 verifyFormat( 7909 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7910 " aaaaaaaaaaaaaaa :\n" 7911 " aaaaaaaaaaaaaaa;", 7912 Style); 7913 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 7914 " aaaaaaaaa ?\n" 7915 " b :\n" 7916 " c);", 7917 Style); 7918 verifyFormat("unsigned Indent =\n" 7919 " format(TheLine.First,\n" 7920 " IndentForLevel[TheLine.Level] >= 0 ?\n" 7921 " IndentForLevel[TheLine.Level] :\n" 7922 " TheLine * 2,\n" 7923 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 7924 Style); 7925 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 7926 " aaaaaaaaaaaaaaa :\n" 7927 " bbbbbbbbbbbbbbb ? //\n" 7928 " ccccccccccccccc :\n" 7929 " ddddddddddddddd;", 7930 Style); 7931 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 7932 " aaaaaaaaaaaaaaa :\n" 7933 " (bbbbbbbbbbbbbbb ? //\n" 7934 " ccccccccccccccc :\n" 7935 " ddddddddddddddd);", 7936 Style); 7937 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7938 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n" 7939 " ccccccccccccccccccccccccccc;", 7940 Style); 7941 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 7942 " aaaaa :\n" 7943 " bbbbbbbbbbbbbbb + cccccccccccccccc;", 7944 Style); 7945 7946 // Chained conditionals 7947 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7948 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7949 " 3333333333333333;", 7950 Style); 7951 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7952 " bbbbbbbbbb ? 2222222222222222 :\n" 7953 " 3333333333333333;", 7954 Style); 7955 verifyFormat("return aaaaaaaaaa ? 1111111111111111 :\n" 7956 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7957 " 3333333333333333;", 7958 Style); 7959 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7960 " bbbbbbbbbbbbbbbb ? 222222 :\n" 7961 " 333333;", 7962 Style); 7963 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7964 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7965 " cccccccccccccccc ? 3333333333333333 :\n" 7966 " 4444444444444444;", 7967 Style); 7968 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc) :\n" 7969 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7970 " 3333333333333333;", 7971 Style); 7972 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7973 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7974 " (aaa ? bbb : ccc);", 7975 Style); 7976 verifyFormat( 7977 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7978 " cccccccccccccccccc) :\n" 7979 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7980 " 3333333333333333;", 7981 Style); 7982 verifyFormat( 7983 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7984 " cccccccccccccccccc) :\n" 7985 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7986 " 3333333333333333;", 7987 Style); 7988 verifyFormat( 7989 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7990 " dddddddddddddddddd) :\n" 7991 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7992 " 3333333333333333;", 7993 Style); 7994 verifyFormat( 7995 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7996 " dddddddddddddddddd) :\n" 7997 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7998 " 3333333333333333;", 7999 Style); 8000 verifyFormat( 8001 "return aaaaaaaaa ? 1111111111111111 :\n" 8002 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8003 " a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8004 " dddddddddddddddddd)\n", 8005 Style); 8006 verifyFormat( 8007 "return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 8008 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8009 " (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8010 " cccccccccccccccccc);", 8011 Style); 8012 verifyFormat( 8013 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8014 " ccccccccccccccccc ? dddddddddddddddddd :\n" 8015 " eeeeeeeeeeeeeeeeee) :\n" 8016 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8017 " 3333333333333333;", 8018 Style); 8019 verifyFormat( 8020 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8021 " ccccccccccccc ? dddddddddddddddddd :\n" 8022 " eeeeeeeeeeeeeeeeee) :\n" 8023 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8024 " 3333333333333333;", 8025 Style); 8026 verifyFormat( 8027 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8028 " ccccccccccccccccc ? dddddddddddddddddd :\n" 8029 " eeeeeeeeeeeeeeeeee) :\n" 8030 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8031 " 3333333333333333;", 8032 Style); 8033 verifyFormat( 8034 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8035 " cccccccccccccccccc :\n" 8036 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8037 " 3333333333333333;", 8038 Style); 8039 verifyFormat( 8040 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8041 " cccccccccccccccccc ? dddddddddddddddddd :\n" 8042 " eeeeeeeeeeeeeeeeee :\n" 8043 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8044 " 3333333333333333;", 8045 Style); 8046 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n" 8047 " (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8048 " cccccccccccccccccc ? dddddddddddddddddd :\n" 8049 " eeeeeeeeeeeeeeeeee) :\n" 8050 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8051 " 3333333333333333;", 8052 Style); 8053 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n" 8054 " aaaaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 8055 " cccccccccccccccccccc ? dddddddddddddddddd :\n" 8056 " eeeeeeeeeeeeeeeeee :\n" 8057 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 8058 " 3333333333333333;", 8059 Style); 8060 } 8061 8062 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 8063 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 8064 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 8065 verifyFormat("bool a = true, b = false;"); 8066 8067 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8068 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 8069 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 8070 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 8071 verifyFormat( 8072 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 8073 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 8074 " d = e && f;"); 8075 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 8076 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 8077 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 8078 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 8079 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 8080 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 8081 8082 FormatStyle Style = getGoogleStyle(); 8083 Style.PointerAlignment = FormatStyle::PAS_Left; 8084 Style.DerivePointerAlignment = false; 8085 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8086 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 8087 " *b = bbbbbbbbbbbbbbbbbbb;", 8088 Style); 8089 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 8090 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 8091 Style); 8092 verifyFormat("vector<int*> a, b;", Style); 8093 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style); 8094 } 8095 8096 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 8097 verifyFormat("arr[foo ? bar : baz];"); 8098 verifyFormat("f()[foo ? bar : baz];"); 8099 verifyFormat("(a + b)[foo ? bar : baz];"); 8100 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 8101 } 8102 8103 TEST_F(FormatTest, AlignsStringLiterals) { 8104 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 8105 " \"short literal\");"); 8106 verifyFormat( 8107 "looooooooooooooooooooooooongFunction(\n" 8108 " \"short literal\"\n" 8109 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 8110 verifyFormat("someFunction(\"Always break between multi-line\"\n" 8111 " \" string literals\",\n" 8112 " and, other, parameters);"); 8113 EXPECT_EQ("fun + \"1243\" /* comment */\n" 8114 " \"5678\";", 8115 format("fun + \"1243\" /* comment */\n" 8116 " \"5678\";", 8117 getLLVMStyleWithColumns(28))); 8118 EXPECT_EQ( 8119 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 8120 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 8121 " \"aaaaaaaaaaaaaaaa\";", 8122 format("aaaaaa =" 8123 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 8124 "aaaaaaaaaaaaaaaaaaaaa\" " 8125 "\"aaaaaaaaaaaaaaaa\";")); 8126 verifyFormat("a = a + \"a\"\n" 8127 " \"a\"\n" 8128 " \"a\";"); 8129 verifyFormat("f(\"a\", \"b\"\n" 8130 " \"c\");"); 8131 8132 verifyFormat( 8133 "#define LL_FORMAT \"ll\"\n" 8134 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 8135 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 8136 8137 verifyFormat("#define A(X) \\\n" 8138 " \"aaaaa\" #X \"bbbbbb\" \\\n" 8139 " \"ccccc\"", 8140 getLLVMStyleWithColumns(23)); 8141 verifyFormat("#define A \"def\"\n" 8142 "f(\"abc\" A \"ghi\"\n" 8143 " \"jkl\");"); 8144 8145 verifyFormat("f(L\"a\"\n" 8146 " L\"b\");"); 8147 verifyFormat("#define A(X) \\\n" 8148 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 8149 " L\"ccccc\"", 8150 getLLVMStyleWithColumns(25)); 8151 8152 verifyFormat("f(@\"a\"\n" 8153 " @\"b\");"); 8154 verifyFormat("NSString s = @\"a\"\n" 8155 " @\"b\"\n" 8156 " @\"c\";"); 8157 verifyFormat("NSString s = @\"a\"\n" 8158 " \"b\"\n" 8159 " \"c\";"); 8160 } 8161 8162 TEST_F(FormatTest, ReturnTypeBreakingStyle) { 8163 FormatStyle Style = getLLVMStyle(); 8164 // No declarations or definitions should be moved to own line. 8165 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; 8166 verifyFormat("class A {\n" 8167 " int f() { return 1; }\n" 8168 " int g();\n" 8169 "};\n" 8170 "int f() { return 1; }\n" 8171 "int g();\n", 8172 Style); 8173 8174 // All declarations and definitions should have the return type moved to its 8175 // own line. 8176 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 8177 Style.TypenameMacros = {"LIST"}; 8178 verifyFormat("SomeType\n" 8179 "funcdecl(LIST(uint64_t));", 8180 Style); 8181 verifyFormat("class E {\n" 8182 " int\n" 8183 " f() {\n" 8184 " return 1;\n" 8185 " }\n" 8186 " int\n" 8187 " g();\n" 8188 "};\n" 8189 "int\n" 8190 "f() {\n" 8191 " return 1;\n" 8192 "}\n" 8193 "int\n" 8194 "g();\n", 8195 Style); 8196 8197 // Top-level definitions, and no kinds of declarations should have the 8198 // return type moved to its own line. 8199 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; 8200 verifyFormat("class B {\n" 8201 " int f() { return 1; }\n" 8202 " int g();\n" 8203 "};\n" 8204 "int\n" 8205 "f() {\n" 8206 " return 1;\n" 8207 "}\n" 8208 "int g();\n", 8209 Style); 8210 8211 // Top-level definitions and declarations should have the return type moved 8212 // to its own line. 8213 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel; 8214 verifyFormat("class C {\n" 8215 " int f() { return 1; }\n" 8216 " int g();\n" 8217 "};\n" 8218 "int\n" 8219 "f() {\n" 8220 " return 1;\n" 8221 "}\n" 8222 "int\n" 8223 "g();\n", 8224 Style); 8225 8226 // All definitions should have the return type moved to its own line, but no 8227 // kinds of declarations. 8228 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 8229 verifyFormat("class D {\n" 8230 " int\n" 8231 " f() {\n" 8232 " return 1;\n" 8233 " }\n" 8234 " int g();\n" 8235 "};\n" 8236 "int\n" 8237 "f() {\n" 8238 " return 1;\n" 8239 "}\n" 8240 "int g();\n", 8241 Style); 8242 verifyFormat("const char *\n" 8243 "f(void) {\n" // Break here. 8244 " return \"\";\n" 8245 "}\n" 8246 "const char *bar(void);\n", // No break here. 8247 Style); 8248 verifyFormat("template <class T>\n" 8249 "T *\n" 8250 "f(T &c) {\n" // Break here. 8251 " return NULL;\n" 8252 "}\n" 8253 "template <class T> T *f(T &c);\n", // No break here. 8254 Style); 8255 verifyFormat("class C {\n" 8256 " int\n" 8257 " operator+() {\n" 8258 " return 1;\n" 8259 " }\n" 8260 " int\n" 8261 " operator()() {\n" 8262 " return 1;\n" 8263 " }\n" 8264 "};\n", 8265 Style); 8266 verifyFormat("void\n" 8267 "A::operator()() {}\n" 8268 "void\n" 8269 "A::operator>>() {}\n" 8270 "void\n" 8271 "A::operator+() {}\n" 8272 "void\n" 8273 "A::operator*() {}\n" 8274 "void\n" 8275 "A::operator->() {}\n" 8276 "void\n" 8277 "A::operator void *() {}\n" 8278 "void\n" 8279 "A::operator void &() {}\n" 8280 "void\n" 8281 "A::operator void &&() {}\n" 8282 "void\n" 8283 "A::operator char *() {}\n" 8284 "void\n" 8285 "A::operator[]() {}\n" 8286 "void\n" 8287 "A::operator!() {}\n" 8288 "void\n" 8289 "A::operator**() {}\n" 8290 "void\n" 8291 "A::operator<Foo> *() {}\n" 8292 "void\n" 8293 "A::operator<Foo> **() {}\n" 8294 "void\n" 8295 "A::operator<Foo> &() {}\n" 8296 "void\n" 8297 "A::operator void **() {}\n", 8298 Style); 8299 verifyFormat("constexpr auto\n" 8300 "operator()() const -> reference {}\n" 8301 "constexpr auto\n" 8302 "operator>>() const -> reference {}\n" 8303 "constexpr auto\n" 8304 "operator+() const -> reference {}\n" 8305 "constexpr auto\n" 8306 "operator*() const -> reference {}\n" 8307 "constexpr auto\n" 8308 "operator->() const -> reference {}\n" 8309 "constexpr auto\n" 8310 "operator++() const -> reference {}\n" 8311 "constexpr auto\n" 8312 "operator void *() const -> reference {}\n" 8313 "constexpr auto\n" 8314 "operator void **() const -> reference {}\n" 8315 "constexpr auto\n" 8316 "operator void *() const -> reference {}\n" 8317 "constexpr auto\n" 8318 "operator void &() const -> reference {}\n" 8319 "constexpr auto\n" 8320 "operator void &&() const -> reference {}\n" 8321 "constexpr auto\n" 8322 "operator char *() const -> reference {}\n" 8323 "constexpr auto\n" 8324 "operator!() const -> reference {}\n" 8325 "constexpr auto\n" 8326 "operator[]() const -> reference {}\n", 8327 Style); 8328 verifyFormat("void *operator new(std::size_t s);", // No break here. 8329 Style); 8330 verifyFormat("void *\n" 8331 "operator new(std::size_t s) {}", 8332 Style); 8333 verifyFormat("void *\n" 8334 "operator delete[](void *ptr) {}", 8335 Style); 8336 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 8337 verifyFormat("const char *\n" 8338 "f(void)\n" // Break here. 8339 "{\n" 8340 " return \"\";\n" 8341 "}\n" 8342 "const char *bar(void);\n", // No break here. 8343 Style); 8344 verifyFormat("template <class T>\n" 8345 "T *\n" // Problem here: no line break 8346 "f(T &c)\n" // Break here. 8347 "{\n" 8348 " return NULL;\n" 8349 "}\n" 8350 "template <class T> T *f(T &c);\n", // No break here. 8351 Style); 8352 verifyFormat("int\n" 8353 "foo(A<bool> a)\n" 8354 "{\n" 8355 " return a;\n" 8356 "}\n", 8357 Style); 8358 verifyFormat("int\n" 8359 "foo(A<8> a)\n" 8360 "{\n" 8361 " return a;\n" 8362 "}\n", 8363 Style); 8364 verifyFormat("int\n" 8365 "foo(A<B<bool>, 8> a)\n" 8366 "{\n" 8367 " return a;\n" 8368 "}\n", 8369 Style); 8370 verifyFormat("int\n" 8371 "foo(A<B<8>, bool> a)\n" 8372 "{\n" 8373 " return a;\n" 8374 "}\n", 8375 Style); 8376 verifyFormat("int\n" 8377 "foo(A<B<bool>, bool> a)\n" 8378 "{\n" 8379 " return a;\n" 8380 "}\n", 8381 Style); 8382 verifyFormat("int\n" 8383 "foo(A<B<8>, 8> a)\n" 8384 "{\n" 8385 " return a;\n" 8386 "}\n", 8387 Style); 8388 8389 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 8390 Style.BraceWrapping.AfterFunction = true; 8391 verifyFormat("int f(i);\n" // No break here. 8392 "int\n" // Break here. 8393 "f(i)\n" 8394 "{\n" 8395 " return i + 1;\n" 8396 "}\n" 8397 "int\n" // Break here. 8398 "f(i)\n" 8399 "{\n" 8400 " return i + 1;\n" 8401 "};", 8402 Style); 8403 verifyFormat("int f(a, b, c);\n" // No break here. 8404 "int\n" // Break here. 8405 "f(a, b, c)\n" // Break here. 8406 "short a, b;\n" 8407 "float c;\n" 8408 "{\n" 8409 " return a + b < c;\n" 8410 "}\n" 8411 "int\n" // Break here. 8412 "f(a, b, c)\n" // Break here. 8413 "short a, b;\n" 8414 "float c;\n" 8415 "{\n" 8416 " return a + b < c;\n" 8417 "};", 8418 Style); 8419 verifyFormat("byte *\n" // Break here. 8420 "f(a)\n" // Break here. 8421 "byte a[];\n" 8422 "{\n" 8423 " return a;\n" 8424 "}", 8425 Style); 8426 verifyFormat("bool f(int a, int) override;\n" 8427 "Bar g(int a, Bar) final;\n" 8428 "Bar h(a, Bar) final;", 8429 Style); 8430 verifyFormat("int\n" 8431 "f(a)", 8432 Style); 8433 verifyFormat("bool\n" 8434 "f(size_t = 0, bool b = false)\n" 8435 "{\n" 8436 " return !b;\n" 8437 "}", 8438 Style); 8439 8440 // The return breaking style doesn't affect: 8441 // * function and object definitions with attribute-like macros 8442 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n" 8443 " ABSL_GUARDED_BY(mutex) = {};", 8444 getGoogleStyleWithColumns(40)); 8445 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n" 8446 " ABSL_GUARDED_BY(mutex); // comment", 8447 getGoogleStyleWithColumns(40)); 8448 verifyFormat("Tttttttttttttttttttttttt ppppppppppppppp\n" 8449 " ABSL_GUARDED_BY(mutex1)\n" 8450 " ABSL_GUARDED_BY(mutex2);", 8451 getGoogleStyleWithColumns(40)); 8452 verifyFormat("Tttttt f(int a, int b)\n" 8453 " ABSL_GUARDED_BY(mutex1)\n" 8454 " ABSL_GUARDED_BY(mutex2);", 8455 getGoogleStyleWithColumns(40)); 8456 // * typedefs 8457 verifyFormat("typedef ATTR(X) char x;", getGoogleStyle()); 8458 8459 Style = getGNUStyle(); 8460 8461 // Test for comments at the end of function declarations. 8462 verifyFormat("void\n" 8463 "foo (int a, /*abc*/ int b) // def\n" 8464 "{\n" 8465 "}\n", 8466 Style); 8467 8468 verifyFormat("void\n" 8469 "foo (int a, /* abc */ int b) /* def */\n" 8470 "{\n" 8471 "}\n", 8472 Style); 8473 8474 // Definitions that should not break after return type 8475 verifyFormat("void foo (int a, int b); // def\n", Style); 8476 verifyFormat("void foo (int a, int b); /* def */\n", Style); 8477 verifyFormat("void foo (int a, int b);\n", Style); 8478 } 8479 8480 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 8481 FormatStyle NoBreak = getLLVMStyle(); 8482 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 8483 FormatStyle Break = getLLVMStyle(); 8484 Break.AlwaysBreakBeforeMultilineStrings = true; 8485 verifyFormat("aaaa = \"bbbb\"\n" 8486 " \"cccc\";", 8487 NoBreak); 8488 verifyFormat("aaaa =\n" 8489 " \"bbbb\"\n" 8490 " \"cccc\";", 8491 Break); 8492 verifyFormat("aaaa(\"bbbb\"\n" 8493 " \"cccc\");", 8494 NoBreak); 8495 verifyFormat("aaaa(\n" 8496 " \"bbbb\"\n" 8497 " \"cccc\");", 8498 Break); 8499 verifyFormat("aaaa(qqq, \"bbbb\"\n" 8500 " \"cccc\");", 8501 NoBreak); 8502 verifyFormat("aaaa(qqq,\n" 8503 " \"bbbb\"\n" 8504 " \"cccc\");", 8505 Break); 8506 verifyFormat("aaaa(qqq,\n" 8507 " L\"bbbb\"\n" 8508 " L\"cccc\");", 8509 Break); 8510 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 8511 " \"bbbb\"));", 8512 Break); 8513 verifyFormat("string s = someFunction(\n" 8514 " \"abc\"\n" 8515 " \"abc\");", 8516 Break); 8517 8518 // As we break before unary operators, breaking right after them is bad. 8519 verifyFormat("string foo = abc ? \"x\"\n" 8520 " \"blah blah blah blah blah blah\"\n" 8521 " : \"y\";", 8522 Break); 8523 8524 // Don't break if there is no column gain. 8525 verifyFormat("f(\"aaaa\"\n" 8526 " \"bbbb\");", 8527 Break); 8528 8529 // Treat literals with escaped newlines like multi-line string literals. 8530 EXPECT_EQ("x = \"a\\\n" 8531 "b\\\n" 8532 "c\";", 8533 format("x = \"a\\\n" 8534 "b\\\n" 8535 "c\";", 8536 NoBreak)); 8537 EXPECT_EQ("xxxx =\n" 8538 " \"a\\\n" 8539 "b\\\n" 8540 "c\";", 8541 format("xxxx = \"a\\\n" 8542 "b\\\n" 8543 "c\";", 8544 Break)); 8545 8546 EXPECT_EQ("NSString *const kString =\n" 8547 " @\"aaaa\"\n" 8548 " @\"bbbb\";", 8549 format("NSString *const kString = @\"aaaa\"\n" 8550 "@\"bbbb\";", 8551 Break)); 8552 8553 Break.ColumnLimit = 0; 8554 verifyFormat("const char *hello = \"hello llvm\";", Break); 8555 } 8556 8557 TEST_F(FormatTest, AlignsPipes) { 8558 verifyFormat( 8559 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8560 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8561 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8562 verifyFormat( 8563 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 8564 " << aaaaaaaaaaaaaaaaaaaa;"); 8565 verifyFormat( 8566 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8567 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8568 verifyFormat( 8569 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 8570 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8571 verifyFormat( 8572 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 8573 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 8574 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 8575 verifyFormat( 8576 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8577 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8578 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8579 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8580 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8581 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8582 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 8583 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 8584 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 8585 verifyFormat( 8586 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8587 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8588 verifyFormat( 8589 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n" 8590 " aaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8591 8592 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 8593 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 8594 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8595 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8596 " aaaaaaaaaaaaaaaaaaaaa)\n" 8597 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8598 verifyFormat("LOG_IF(aaa == //\n" 8599 " bbb)\n" 8600 " << a << b;"); 8601 8602 // But sometimes, breaking before the first "<<" is desirable. 8603 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 8604 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 8605 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 8606 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8607 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8608 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 8609 " << BEF << IsTemplate << Description << E->getType();"); 8610 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 8611 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8612 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8613 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 8614 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8615 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8616 " << aaa;"); 8617 8618 verifyFormat( 8619 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8620 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8621 8622 // Incomplete string literal. 8623 EXPECT_EQ("llvm::errs() << \"\n" 8624 " << a;", 8625 format("llvm::errs() << \"\n<<a;")); 8626 8627 verifyFormat("void f() {\n" 8628 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 8629 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 8630 "}"); 8631 8632 // Handle 'endl'. 8633 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 8634 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 8635 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 8636 8637 // Handle '\n'. 8638 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 8639 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 8640 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 8641 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 8642 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 8643 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 8644 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 8645 } 8646 8647 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) { 8648 verifyFormat("return out << \"somepacket = {\\n\"\n" 8649 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 8650 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 8651 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 8652 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 8653 " << \"}\";"); 8654 8655 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 8656 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 8657 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 8658 verifyFormat( 8659 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 8660 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 8661 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 8662 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 8663 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 8664 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 8665 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 8666 verifyFormat( 8667 "void f() {\n" 8668 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 8669 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 8670 "}"); 8671 8672 // Breaking before the first "<<" is generally not desirable. 8673 verifyFormat( 8674 "llvm::errs()\n" 8675 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8676 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8677 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8678 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 8679 getLLVMStyleWithColumns(70)); 8680 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 8681 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8682 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 8683 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8684 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 8685 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 8686 getLLVMStyleWithColumns(70)); 8687 8688 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 8689 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 8690 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;"); 8691 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 8692 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 8693 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);"); 8694 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n" 8695 " (aaaa + aaaa);", 8696 getLLVMStyleWithColumns(40)); 8697 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n" 8698 " (aaaaaaa + aaaaa));", 8699 getLLVMStyleWithColumns(40)); 8700 verifyFormat( 8701 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n" 8702 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n" 8703 " bbbbbbbbbbbbbbbbbbbbbbb);"); 8704 } 8705 8706 TEST_F(FormatTest, UnderstandsEquals) { 8707 verifyFormat( 8708 "aaaaaaaaaaaaaaaaa =\n" 8709 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 8710 verifyFormat( 8711 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8712 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 8713 verifyFormat( 8714 "if (a) {\n" 8715 " f();\n" 8716 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8717 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 8718 "}"); 8719 8720 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8721 " 100000000 + 10000000) {\n}"); 8722 } 8723 8724 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 8725 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 8726 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 8727 8728 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 8729 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 8730 8731 verifyFormat( 8732 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 8733 " Parameter2);"); 8734 8735 verifyFormat( 8736 "ShortObject->shortFunction(\n" 8737 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 8738 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 8739 8740 verifyFormat("loooooooooooooongFunction(\n" 8741 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 8742 8743 verifyFormat( 8744 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 8745 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 8746 8747 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 8748 " .WillRepeatedly(Return(SomeValue));"); 8749 verifyFormat("void f() {\n" 8750 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 8751 " .Times(2)\n" 8752 " .WillRepeatedly(Return(SomeValue));\n" 8753 "}"); 8754 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 8755 " ccccccccccccccccccccccc);"); 8756 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8757 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8758 " .aaaaa(aaaaa),\n" 8759 " aaaaaaaaaaaaaaaaaaaaa);"); 8760 verifyFormat("void f() {\n" 8761 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8762 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 8763 "}"); 8764 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8765 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8766 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8767 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8768 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 8769 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8770 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8771 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8772 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 8773 "}"); 8774 8775 // Here, it is not necessary to wrap at "." or "->". 8776 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 8777 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 8778 verifyFormat( 8779 "aaaaaaaaaaa->aaaaaaaaa(\n" 8780 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8781 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 8782 8783 verifyFormat( 8784 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8785 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 8786 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 8787 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 8788 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 8789 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 8790 8791 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8792 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8793 " .a();"); 8794 8795 FormatStyle NoBinPacking = getLLVMStyle(); 8796 NoBinPacking.BinPackParameters = false; 8797 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 8798 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 8799 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 8800 " aaaaaaaaaaaaaaaaaaa,\n" 8801 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 8802 NoBinPacking); 8803 8804 // If there is a subsequent call, change to hanging indentation. 8805 verifyFormat( 8806 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8807 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 8808 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8809 verifyFormat( 8810 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8811 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 8812 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8813 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8814 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8815 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8816 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 8817 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 8818 } 8819 8820 TEST_F(FormatTest, WrapsTemplateDeclarations) { 8821 verifyFormat("template <typename T>\n" 8822 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 8823 verifyFormat("template <typename T>\n" 8824 "// T should be one of {A, B}.\n" 8825 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 8826 verifyFormat( 8827 "template <typename T>\n" 8828 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 8829 verifyFormat("template <typename T>\n" 8830 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 8831 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 8832 verifyFormat( 8833 "template <typename T>\n" 8834 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 8835 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 8836 verifyFormat( 8837 "template <typename T>\n" 8838 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 8839 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 8840 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8841 verifyFormat("template <typename T>\n" 8842 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8843 " int aaaaaaaaaaaaaaaaaaaaaa);"); 8844 verifyFormat( 8845 "template <typename T1, typename T2 = char, typename T3 = char,\n" 8846 " typename T4 = char>\n" 8847 "void f();"); 8848 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 8849 " template <typename> class cccccccccccccccccccccc,\n" 8850 " typename ddddddddddddd>\n" 8851 "class C {};"); 8852 verifyFormat( 8853 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 8854 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8855 8856 verifyFormat("void f() {\n" 8857 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 8858 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 8859 "}"); 8860 8861 verifyFormat("template <typename T> class C {};"); 8862 verifyFormat("template <typename T> void f();"); 8863 verifyFormat("template <typename T> void f() {}"); 8864 verifyFormat( 8865 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 8866 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8867 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 8868 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 8869 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8870 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 8871 " bbbbbbbbbbbbbbbbbbbbbbbb);", 8872 getLLVMStyleWithColumns(72)); 8873 EXPECT_EQ("static_cast<A< //\n" 8874 " B> *>(\n" 8875 "\n" 8876 ");", 8877 format("static_cast<A<//\n" 8878 " B>*>(\n" 8879 "\n" 8880 " );")); 8881 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8882 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 8883 8884 FormatStyle AlwaysBreak = getLLVMStyle(); 8885 AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 8886 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 8887 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 8888 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 8889 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8890 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 8891 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 8892 verifyFormat("template <template <typename> class Fooooooo,\n" 8893 " template <typename> class Baaaaaaar>\n" 8894 "struct C {};", 8895 AlwaysBreak); 8896 verifyFormat("template <typename T> // T can be A, B or C.\n" 8897 "struct C {};", 8898 AlwaysBreak); 8899 verifyFormat("template <enum E> class A {\n" 8900 "public:\n" 8901 " E *f();\n" 8902 "};"); 8903 8904 FormatStyle NeverBreak = getLLVMStyle(); 8905 NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No; 8906 verifyFormat("template <typename T> class C {};", NeverBreak); 8907 verifyFormat("template <typename T> void f();", NeverBreak); 8908 verifyFormat("template <typename T> void f() {}", NeverBreak); 8909 verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa " 8910 "bbbbbbbbbbbbbbbbbbbb) {}", 8911 NeverBreak); 8912 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8913 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 8914 " ccccccccccccccccccccccccccccccccccccccccccccccc);", 8915 NeverBreak); 8916 verifyFormat("template <template <typename> class Fooooooo,\n" 8917 " template <typename> class Baaaaaaar>\n" 8918 "struct C {};", 8919 NeverBreak); 8920 verifyFormat("template <typename T> // T can be A, B or C.\n" 8921 "struct C {};", 8922 NeverBreak); 8923 verifyFormat("template <enum E> class A {\n" 8924 "public:\n" 8925 " E *f();\n" 8926 "};", 8927 NeverBreak); 8928 NeverBreak.PenaltyBreakTemplateDeclaration = 100; 8929 verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa " 8930 "bbbbbbbbbbbbbbbbbbbb) {}", 8931 NeverBreak); 8932 } 8933 8934 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) { 8935 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 8936 Style.ColumnLimit = 60; 8937 EXPECT_EQ("// Baseline - no comments.\n" 8938 "template <\n" 8939 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 8940 "void f() {}", 8941 format("// Baseline - no comments.\n" 8942 "template <\n" 8943 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 8944 "void f() {}", 8945 Style)); 8946 8947 EXPECT_EQ("template <\n" 8948 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 8949 "void f() {}", 8950 format("template <\n" 8951 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 8952 "void f() {}", 8953 Style)); 8954 8955 EXPECT_EQ( 8956 "template <\n" 8957 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 8958 "void f() {}", 8959 format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 8960 "void f() {}", 8961 Style)); 8962 8963 EXPECT_EQ( 8964 "template <\n" 8965 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 8966 " // multiline\n" 8967 "void f() {}", 8968 format("template <\n" 8969 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 8970 " // multiline\n" 8971 "void f() {}", 8972 Style)); 8973 8974 EXPECT_EQ( 8975 "template <typename aaaaaaaaaa<\n" 8976 " bbbbbbbbbbbb>::value> // trailing loooong\n" 8977 "void f() {}", 8978 format( 8979 "template <\n" 8980 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n" 8981 "void f() {}", 8982 Style)); 8983 } 8984 8985 TEST_F(FormatTest, WrapsTemplateParameters) { 8986 FormatStyle Style = getLLVMStyle(); 8987 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 8988 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 8989 verifyFormat( 8990 "template <typename... a> struct q {};\n" 8991 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 8992 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 8993 " y;", 8994 Style); 8995 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 8996 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 8997 verifyFormat( 8998 "template <typename... a> struct r {};\n" 8999 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 9000 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 9001 " y;", 9002 Style); 9003 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 9004 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 9005 verifyFormat("template <typename... a> struct s {};\n" 9006 "extern s<\n" 9007 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 9008 "aaaaaaaaaaaaaaaaaaaaaa,\n" 9009 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 9010 "aaaaaaaaaaaaaaaaaaaaaa>\n" 9011 " y;", 9012 Style); 9013 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 9014 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 9015 verifyFormat("template <typename... a> struct t {};\n" 9016 "extern t<\n" 9017 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 9018 "aaaaaaaaaaaaaaaaaaaaaa,\n" 9019 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 9020 "aaaaaaaaaaaaaaaaaaaaaa>\n" 9021 " y;", 9022 Style); 9023 } 9024 9025 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 9026 verifyFormat( 9027 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 9028 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 9029 verifyFormat( 9030 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 9031 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9032 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 9033 9034 // FIXME: Should we have the extra indent after the second break? 9035 verifyFormat( 9036 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 9037 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 9038 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 9039 9040 verifyFormat( 9041 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 9042 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 9043 9044 // Breaking at nested name specifiers is generally not desirable. 9045 verifyFormat( 9046 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9047 " aaaaaaaaaaaaaaaaaaaaaaa);"); 9048 9049 verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n" 9050 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 9051 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9052 " aaaaaaaaaaaaaaaaaaaaa);", 9053 getLLVMStyleWithColumns(74)); 9054 9055 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 9056 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9057 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 9058 } 9059 9060 TEST_F(FormatTest, UnderstandsTemplateParameters) { 9061 verifyFormat("A<int> a;"); 9062 verifyFormat("A<A<A<int>>> a;"); 9063 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 9064 verifyFormat("bool x = a < 1 || 2 > a;"); 9065 verifyFormat("bool x = 5 < f<int>();"); 9066 verifyFormat("bool x = f<int>() > 5;"); 9067 verifyFormat("bool x = 5 < a<int>::x;"); 9068 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 9069 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 9070 9071 verifyGoogleFormat("A<A<int>> a;"); 9072 verifyGoogleFormat("A<A<A<int>>> a;"); 9073 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 9074 verifyGoogleFormat("A<A<int> > a;"); 9075 verifyGoogleFormat("A<A<A<int> > > a;"); 9076 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 9077 verifyGoogleFormat("A<::A<int>> a;"); 9078 verifyGoogleFormat("A<::A> a;"); 9079 verifyGoogleFormat("A< ::A> a;"); 9080 verifyGoogleFormat("A< ::A<int> > a;"); 9081 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 9082 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 9083 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 9084 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 9085 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 9086 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 9087 9088 verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 9089 9090 // template closer followed by a token that starts with > or = 9091 verifyFormat("bool b = a<1> > 1;"); 9092 verifyFormat("bool b = a<1> >= 1;"); 9093 verifyFormat("int i = a<1> >> 1;"); 9094 FormatStyle Style = getLLVMStyle(); 9095 Style.SpaceBeforeAssignmentOperators = false; 9096 verifyFormat("bool b= a<1> == 1;", Style); 9097 verifyFormat("a<int> = 1;", Style); 9098 verifyFormat("a<int> >>= 1;", Style); 9099 9100 verifyFormat("test < a | b >> c;"); 9101 verifyFormat("test<test<a | b>> c;"); 9102 verifyFormat("test >> a >> b;"); 9103 verifyFormat("test << a >> b;"); 9104 9105 verifyFormat("f<int>();"); 9106 verifyFormat("template <typename T> void f() {}"); 9107 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 9108 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 9109 "sizeof(char)>::type>;"); 9110 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 9111 verifyFormat("f(a.operator()<A>());"); 9112 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9113 " .template operator()<A>());", 9114 getLLVMStyleWithColumns(35)); 9115 9116 // Not template parameters. 9117 verifyFormat("return a < b && c > d;"); 9118 verifyFormat("void f() {\n" 9119 " while (a < b && c > d) {\n" 9120 " }\n" 9121 "}"); 9122 verifyFormat("template <typename... Types>\n" 9123 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 9124 9125 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9126 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 9127 getLLVMStyleWithColumns(60)); 9128 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 9129 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 9130 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 9131 verifyFormat("some_templated_type<decltype([](int i) { return i; })>"); 9132 } 9133 9134 TEST_F(FormatTest, UnderstandsShiftOperators) { 9135 verifyFormat("if (i < x >> 1)"); 9136 verifyFormat("while (i < x >> 1)"); 9137 verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)"); 9138 verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)"); 9139 verifyFormat( 9140 "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)"); 9141 verifyFormat("Foo.call<Bar<Function>>()"); 9142 verifyFormat("if (Foo.call<Bar<Function>>() == 0)"); 9143 verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; " 9144 "++i, v = v >> 1)"); 9145 verifyFormat("if (w<u<v<x>>, 1>::t)"); 9146 } 9147 9148 TEST_F(FormatTest, BitshiftOperatorWidth) { 9149 EXPECT_EQ("int a = 1 << 2; /* foo\n" 9150 " bar */", 9151 format("int a=1<<2; /* foo\n" 9152 " bar */")); 9153 9154 EXPECT_EQ("int b = 256 >> 1; /* foo\n" 9155 " bar */", 9156 format("int b =256>>1 ; /* foo\n" 9157 " bar */")); 9158 } 9159 9160 TEST_F(FormatTest, UnderstandsBinaryOperators) { 9161 verifyFormat("COMPARE(a, ==, b);"); 9162 verifyFormat("auto s = sizeof...(Ts) - 1;"); 9163 } 9164 9165 TEST_F(FormatTest, UnderstandsPointersToMembers) { 9166 verifyFormat("int A::*x;"); 9167 verifyFormat("int (S::*func)(void *);"); 9168 verifyFormat("void f() { int (S::*func)(void *); }"); 9169 verifyFormat("typedef bool *(Class::*Member)() const;"); 9170 verifyFormat("void f() {\n" 9171 " (a->*f)();\n" 9172 " a->*x;\n" 9173 " (a.*f)();\n" 9174 " ((*a).*f)();\n" 9175 " a.*x;\n" 9176 "}"); 9177 verifyFormat("void f() {\n" 9178 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 9179 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 9180 "}"); 9181 verifyFormat( 9182 "(aaaaaaaaaa->*bbbbbbb)(\n" 9183 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 9184 FormatStyle Style = getLLVMStyle(); 9185 Style.PointerAlignment = FormatStyle::PAS_Left; 9186 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 9187 } 9188 9189 TEST_F(FormatTest, UnderstandsUnaryOperators) { 9190 verifyFormat("int a = -2;"); 9191 verifyFormat("f(-1, -2, -3);"); 9192 verifyFormat("a[-1] = 5;"); 9193 verifyFormat("int a = 5 + -2;"); 9194 verifyFormat("if (i == -1) {\n}"); 9195 verifyFormat("if (i != -1) {\n}"); 9196 verifyFormat("if (i > -1) {\n}"); 9197 verifyFormat("if (i < -1) {\n}"); 9198 verifyFormat("++(a->f());"); 9199 verifyFormat("--(a->f());"); 9200 verifyFormat("(a->f())++;"); 9201 verifyFormat("a[42]++;"); 9202 verifyFormat("if (!(a->f())) {\n}"); 9203 verifyFormat("if (!+i) {\n}"); 9204 verifyFormat("~&a;"); 9205 9206 verifyFormat("a-- > b;"); 9207 verifyFormat("b ? -a : c;"); 9208 verifyFormat("n * sizeof char16;"); 9209 verifyFormat("n * alignof char16;", getGoogleStyle()); 9210 verifyFormat("sizeof(char);"); 9211 verifyFormat("alignof(char);", getGoogleStyle()); 9212 9213 verifyFormat("return -1;"); 9214 verifyFormat("throw -1;"); 9215 verifyFormat("switch (a) {\n" 9216 "case -1:\n" 9217 " break;\n" 9218 "}"); 9219 verifyFormat("#define X -1"); 9220 verifyFormat("#define X -kConstant"); 9221 9222 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 9223 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 9224 9225 verifyFormat("int a = /* confusing comment */ -1;"); 9226 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 9227 verifyFormat("int a = i /* confusing comment */++;"); 9228 9229 verifyFormat("co_yield -1;"); 9230 verifyFormat("co_return -1;"); 9231 9232 // Check that * is not treated as a binary operator when we set 9233 // PointerAlignment as PAS_Left after a keyword and not a declaration. 9234 FormatStyle PASLeftStyle = getLLVMStyle(); 9235 PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left; 9236 verifyFormat("co_return *a;", PASLeftStyle); 9237 verifyFormat("co_await *a;", PASLeftStyle); 9238 verifyFormat("co_yield *a", PASLeftStyle); 9239 verifyFormat("return *a;", PASLeftStyle); 9240 } 9241 9242 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 9243 verifyFormat("if (!aaaaaaaaaa( // break\n" 9244 " aaaaa)) {\n" 9245 "}"); 9246 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 9247 " aaaaa));"); 9248 verifyFormat("*aaa = aaaaaaa( // break\n" 9249 " bbbbbb);"); 9250 } 9251 9252 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 9253 verifyFormat("bool operator<();"); 9254 verifyFormat("bool operator>();"); 9255 verifyFormat("bool operator=();"); 9256 verifyFormat("bool operator==();"); 9257 verifyFormat("bool operator!=();"); 9258 verifyFormat("int operator+();"); 9259 verifyFormat("int operator++();"); 9260 verifyFormat("int operator++(int) volatile noexcept;"); 9261 verifyFormat("bool operator,();"); 9262 verifyFormat("bool operator();"); 9263 verifyFormat("bool operator()();"); 9264 verifyFormat("bool operator[]();"); 9265 verifyFormat("operator bool();"); 9266 verifyFormat("operator int();"); 9267 verifyFormat("operator void *();"); 9268 verifyFormat("operator SomeType<int>();"); 9269 verifyFormat("operator SomeType<int, int>();"); 9270 verifyFormat("operator SomeType<SomeType<int>>();"); 9271 verifyFormat("void *operator new(std::size_t size);"); 9272 verifyFormat("void *operator new[](std::size_t size);"); 9273 verifyFormat("void operator delete(void *ptr);"); 9274 verifyFormat("void operator delete[](void *ptr);"); 9275 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 9276 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 9277 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 9278 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 9279 9280 verifyFormat( 9281 "ostream &operator<<(ostream &OutputStream,\n" 9282 " SomeReallyLongType WithSomeReallyLongValue);"); 9283 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 9284 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 9285 " return left.group < right.group;\n" 9286 "}"); 9287 verifyFormat("SomeType &operator=(const SomeType &S);"); 9288 verifyFormat("f.template operator()<int>();"); 9289 9290 verifyGoogleFormat("operator void*();"); 9291 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 9292 verifyGoogleFormat("operator ::A();"); 9293 9294 verifyFormat("using A::operator+;"); 9295 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 9296 "int i;"); 9297 9298 // Calling an operator as a member function. 9299 verifyFormat("void f() { a.operator*(); }"); 9300 verifyFormat("void f() { a.operator*(b & b); }"); 9301 verifyFormat("void f() { a->operator&(a * b); }"); 9302 verifyFormat("void f() { NS::a.operator+(*b * *b); }"); 9303 // TODO: Calling an operator as a non-member function is hard to distinguish. 9304 // https://llvm.org/PR50629 9305 // verifyFormat("void f() { operator*(a & a); }"); 9306 // verifyFormat("void f() { operator&(a, b * b); }"); 9307 9308 verifyFormat("::operator delete(foo);"); 9309 verifyFormat("::operator new(n * sizeof(foo));"); 9310 verifyFormat("foo() { ::operator delete(foo); }"); 9311 verifyFormat("foo() { ::operator new(n * sizeof(foo)); }"); 9312 } 9313 9314 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 9315 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 9316 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 9317 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 9318 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 9319 verifyFormat("Deleted &operator=(const Deleted &) &;"); 9320 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 9321 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 9322 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 9323 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 9324 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 9325 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 9326 verifyFormat("void Fn(T const &) const &;"); 9327 verifyFormat("void Fn(T const volatile &&) const volatile &&;"); 9328 verifyFormat("template <typename T>\n" 9329 "void F(T) && = delete;", 9330 getGoogleStyle()); 9331 9332 FormatStyle AlignLeft = getLLVMStyle(); 9333 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 9334 verifyFormat("void A::b() && {}", AlignLeft); 9335 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 9336 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 9337 AlignLeft); 9338 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 9339 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 9340 verifyFormat("auto Function(T t) & -> void {}", AlignLeft); 9341 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft); 9342 verifyFormat("auto Function(T) & -> void {}", AlignLeft); 9343 verifyFormat("auto Function(T) & -> void;", AlignLeft); 9344 verifyFormat("void Fn(T const&) const&;", AlignLeft); 9345 verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft); 9346 9347 FormatStyle Spaces = getLLVMStyle(); 9348 Spaces.SpacesInCStyleCastParentheses = true; 9349 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 9350 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 9351 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 9352 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 9353 9354 Spaces.SpacesInCStyleCastParentheses = false; 9355 Spaces.SpacesInParentheses = true; 9356 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 9357 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", 9358 Spaces); 9359 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 9360 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 9361 9362 FormatStyle BreakTemplate = getLLVMStyle(); 9363 BreakTemplate.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 9364 9365 verifyFormat("struct f {\n" 9366 " template <class T>\n" 9367 " int &foo(const std::string &str) &noexcept {}\n" 9368 "};", 9369 BreakTemplate); 9370 9371 verifyFormat("struct f {\n" 9372 " template <class T>\n" 9373 " int &foo(const std::string &str) &&noexcept {}\n" 9374 "};", 9375 BreakTemplate); 9376 9377 verifyFormat("struct f {\n" 9378 " template <class T>\n" 9379 " int &foo(const std::string &str) const &noexcept {}\n" 9380 "};", 9381 BreakTemplate); 9382 9383 verifyFormat("struct f {\n" 9384 " template <class T>\n" 9385 " int &foo(const std::string &str) const &noexcept {}\n" 9386 "};", 9387 BreakTemplate); 9388 9389 verifyFormat("struct f {\n" 9390 " template <class T>\n" 9391 " auto foo(const std::string &str) &&noexcept -> int & {}\n" 9392 "};", 9393 BreakTemplate); 9394 9395 FormatStyle AlignLeftBreakTemplate = getLLVMStyle(); 9396 AlignLeftBreakTemplate.AlwaysBreakTemplateDeclarations = 9397 FormatStyle::BTDS_Yes; 9398 AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left; 9399 9400 verifyFormat("struct f {\n" 9401 " template <class T>\n" 9402 " int& foo(const std::string& str) & noexcept {}\n" 9403 "};", 9404 AlignLeftBreakTemplate); 9405 9406 verifyFormat("struct f {\n" 9407 " template <class T>\n" 9408 " int& foo(const std::string& str) && noexcept {}\n" 9409 "};", 9410 AlignLeftBreakTemplate); 9411 9412 verifyFormat("struct f {\n" 9413 " template <class T>\n" 9414 " int& foo(const std::string& str) const& noexcept {}\n" 9415 "};", 9416 AlignLeftBreakTemplate); 9417 9418 verifyFormat("struct f {\n" 9419 " template <class T>\n" 9420 " int& foo(const std::string& str) const&& noexcept {}\n" 9421 "};", 9422 AlignLeftBreakTemplate); 9423 9424 verifyFormat("struct f {\n" 9425 " template <class T>\n" 9426 " auto foo(const std::string& str) && noexcept -> int& {}\n" 9427 "};", 9428 AlignLeftBreakTemplate); 9429 9430 // The `&` in `Type&` should not be confused with a trailing `&` of 9431 // DEPRECATED(reason) member function. 9432 verifyFormat("struct f {\n" 9433 " template <class T>\n" 9434 " DEPRECATED(reason)\n" 9435 " Type &foo(arguments) {}\n" 9436 "};", 9437 BreakTemplate); 9438 9439 verifyFormat("struct f {\n" 9440 " template <class T>\n" 9441 " DEPRECATED(reason)\n" 9442 " Type& foo(arguments) {}\n" 9443 "};", 9444 AlignLeftBreakTemplate); 9445 9446 verifyFormat("void (*foopt)(int) = &func;"); 9447 } 9448 9449 TEST_F(FormatTest, UnderstandsNewAndDelete) { 9450 verifyFormat("void f() {\n" 9451 " A *a = new A;\n" 9452 " A *a = new (placement) A;\n" 9453 " delete a;\n" 9454 " delete (A *)a;\n" 9455 "}"); 9456 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 9457 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 9458 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 9459 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 9460 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 9461 verifyFormat("delete[] h->p;"); 9462 } 9463 9464 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 9465 verifyFormat("int *f(int *a) {}"); 9466 verifyFormat("int main(int argc, char **argv) {}"); 9467 verifyFormat("Test::Test(int b) : a(b * b) {}"); 9468 verifyIndependentOfContext("f(a, *a);"); 9469 verifyFormat("void g() { f(*a); }"); 9470 verifyIndependentOfContext("int a = b * 10;"); 9471 verifyIndependentOfContext("int a = 10 * b;"); 9472 verifyIndependentOfContext("int a = b * c;"); 9473 verifyIndependentOfContext("int a += b * c;"); 9474 verifyIndependentOfContext("int a -= b * c;"); 9475 verifyIndependentOfContext("int a *= b * c;"); 9476 verifyIndependentOfContext("int a /= b * c;"); 9477 verifyIndependentOfContext("int a = *b;"); 9478 verifyIndependentOfContext("int a = *b * c;"); 9479 verifyIndependentOfContext("int a = b * *c;"); 9480 verifyIndependentOfContext("int a = b * (10);"); 9481 verifyIndependentOfContext("S << b * (10);"); 9482 verifyIndependentOfContext("return 10 * b;"); 9483 verifyIndependentOfContext("return *b * *c;"); 9484 verifyIndependentOfContext("return a & ~b;"); 9485 verifyIndependentOfContext("f(b ? *c : *d);"); 9486 verifyIndependentOfContext("int a = b ? *c : *d;"); 9487 verifyIndependentOfContext("*b = a;"); 9488 verifyIndependentOfContext("a * ~b;"); 9489 verifyIndependentOfContext("a * !b;"); 9490 verifyIndependentOfContext("a * +b;"); 9491 verifyIndependentOfContext("a * -b;"); 9492 verifyIndependentOfContext("a * ++b;"); 9493 verifyIndependentOfContext("a * --b;"); 9494 verifyIndependentOfContext("a[4] * b;"); 9495 verifyIndependentOfContext("a[a * a] = 1;"); 9496 verifyIndependentOfContext("f() * b;"); 9497 verifyIndependentOfContext("a * [self dostuff];"); 9498 verifyIndependentOfContext("int x = a * (a + b);"); 9499 verifyIndependentOfContext("(a *)(a + b);"); 9500 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 9501 verifyIndependentOfContext("int *pa = (int *)&a;"); 9502 verifyIndependentOfContext("return sizeof(int **);"); 9503 verifyIndependentOfContext("return sizeof(int ******);"); 9504 verifyIndependentOfContext("return (int **&)a;"); 9505 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 9506 verifyFormat("void f(Type (*parameter)[10]) {}"); 9507 verifyFormat("void f(Type (¶meter)[10]) {}"); 9508 verifyGoogleFormat("return sizeof(int**);"); 9509 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 9510 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 9511 verifyFormat("auto a = [](int **&, int ***) {};"); 9512 verifyFormat("auto PointerBinding = [](const char *S) {};"); 9513 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 9514 verifyFormat("[](const decltype(*a) &value) {}"); 9515 verifyFormat("[](const typeof(*a) &value) {}"); 9516 verifyFormat("[](const _Atomic(a *) &value) {}"); 9517 verifyFormat("[](const __underlying_type(a) &value) {}"); 9518 verifyFormat("decltype(a * b) F();"); 9519 verifyFormat("typeof(a * b) F();"); 9520 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 9521 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 9522 verifyIndependentOfContext("typedef void (*f)(int *a);"); 9523 verifyIndependentOfContext("int i{a * b};"); 9524 verifyIndependentOfContext("aaa && aaa->f();"); 9525 verifyIndependentOfContext("int x = ~*p;"); 9526 verifyFormat("Constructor() : a(a), area(width * height) {}"); 9527 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 9528 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 9529 verifyFormat("void f() { f(a, c * d); }"); 9530 verifyFormat("void f() { f(new a(), c * d); }"); 9531 verifyFormat("void f(const MyOverride &override);"); 9532 verifyFormat("void f(const MyFinal &final);"); 9533 verifyIndependentOfContext("bool a = f() && override.f();"); 9534 verifyIndependentOfContext("bool a = f() && final.f();"); 9535 9536 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 9537 9538 verifyIndependentOfContext("A<int *> a;"); 9539 verifyIndependentOfContext("A<int **> a;"); 9540 verifyIndependentOfContext("A<int *, int *> a;"); 9541 verifyIndependentOfContext("A<int *[]> a;"); 9542 verifyIndependentOfContext( 9543 "const char *const p = reinterpret_cast<const char *const>(q);"); 9544 verifyIndependentOfContext("A<int **, int **> a;"); 9545 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 9546 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 9547 verifyFormat("for (; a && b;) {\n}"); 9548 verifyFormat("bool foo = true && [] { return false; }();"); 9549 9550 verifyFormat( 9551 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9552 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9553 9554 verifyGoogleFormat("int const* a = &b;"); 9555 verifyGoogleFormat("**outparam = 1;"); 9556 verifyGoogleFormat("*outparam = a * b;"); 9557 verifyGoogleFormat("int main(int argc, char** argv) {}"); 9558 verifyGoogleFormat("A<int*> a;"); 9559 verifyGoogleFormat("A<int**> a;"); 9560 verifyGoogleFormat("A<int*, int*> a;"); 9561 verifyGoogleFormat("A<int**, int**> a;"); 9562 verifyGoogleFormat("f(b ? *c : *d);"); 9563 verifyGoogleFormat("int a = b ? *c : *d;"); 9564 verifyGoogleFormat("Type* t = **x;"); 9565 verifyGoogleFormat("Type* t = *++*x;"); 9566 verifyGoogleFormat("*++*x;"); 9567 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 9568 verifyGoogleFormat("Type* t = x++ * y;"); 9569 verifyGoogleFormat( 9570 "const char* const p = reinterpret_cast<const char* const>(q);"); 9571 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 9572 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 9573 verifyGoogleFormat("template <typename T>\n" 9574 "void f(int i = 0, SomeType** temps = NULL);"); 9575 9576 FormatStyle Left = getLLVMStyle(); 9577 Left.PointerAlignment = FormatStyle::PAS_Left; 9578 verifyFormat("x = *a(x) = *a(y);", Left); 9579 verifyFormat("for (;; *a = b) {\n}", Left); 9580 verifyFormat("return *this += 1;", Left); 9581 verifyFormat("throw *x;", Left); 9582 verifyFormat("delete *x;", Left); 9583 verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left); 9584 verifyFormat("[](const decltype(*a)* ptr) {}", Left); 9585 verifyFormat("[](const typeof(*a)* ptr) {}", Left); 9586 verifyFormat("[](const _Atomic(a*)* ptr) {}", Left); 9587 verifyFormat("[](const __underlying_type(a)* ptr) {}", Left); 9588 verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left); 9589 verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left); 9590 verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left); 9591 verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left); 9592 9593 verifyIndependentOfContext("a = *(x + y);"); 9594 verifyIndependentOfContext("a = &(x + y);"); 9595 verifyIndependentOfContext("*(x + y).call();"); 9596 verifyIndependentOfContext("&(x + y)->call();"); 9597 verifyFormat("void f() { &(*I).first; }"); 9598 9599 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 9600 verifyFormat("f(* /* confusing comment */ foo);"); 9601 verifyFormat("void (* /*deleter*/)(const Slice &key, void *value)"); 9602 verifyFormat("void foo(int * // this is the first paramters\n" 9603 " ,\n" 9604 " int second);"); 9605 verifyFormat("double term = a * // first\n" 9606 " b;"); 9607 verifyFormat( 9608 "int *MyValues = {\n" 9609 " *A, // Operator detection might be confused by the '{'\n" 9610 " *BB // Operator detection might be confused by previous comment\n" 9611 "};"); 9612 9613 verifyIndependentOfContext("if (int *a = &b)"); 9614 verifyIndependentOfContext("if (int &a = *b)"); 9615 verifyIndependentOfContext("if (a & b[i])"); 9616 verifyIndependentOfContext("if constexpr (a & b[i])"); 9617 verifyIndependentOfContext("if CONSTEXPR (a & b[i])"); 9618 verifyIndependentOfContext("if (a * (b * c))"); 9619 verifyIndependentOfContext("if constexpr (a * (b * c))"); 9620 verifyIndependentOfContext("if CONSTEXPR (a * (b * c))"); 9621 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 9622 verifyIndependentOfContext("if (*b[i])"); 9623 verifyIndependentOfContext("if (int *a = (&b))"); 9624 verifyIndependentOfContext("while (int *a = &b)"); 9625 verifyIndependentOfContext("while (a * (b * c))"); 9626 verifyIndependentOfContext("size = sizeof *a;"); 9627 verifyIndependentOfContext("if (a && (b = c))"); 9628 verifyFormat("void f() {\n" 9629 " for (const int &v : Values) {\n" 9630 " }\n" 9631 "}"); 9632 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 9633 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 9634 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 9635 9636 verifyFormat("#define A (!a * b)"); 9637 verifyFormat("#define MACRO \\\n" 9638 " int *i = a * b; \\\n" 9639 " void f(a *b);", 9640 getLLVMStyleWithColumns(19)); 9641 9642 verifyIndependentOfContext("A = new SomeType *[Length];"); 9643 verifyIndependentOfContext("A = new SomeType *[Length]();"); 9644 verifyIndependentOfContext("T **t = new T *;"); 9645 verifyIndependentOfContext("T **t = new T *();"); 9646 verifyGoogleFormat("A = new SomeType*[Length]();"); 9647 verifyGoogleFormat("A = new SomeType*[Length];"); 9648 verifyGoogleFormat("T** t = new T*;"); 9649 verifyGoogleFormat("T** t = new T*();"); 9650 9651 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 9652 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 9653 verifyFormat("template <bool a, bool b> " 9654 "typename t::if<x && y>::type f() {}"); 9655 verifyFormat("template <int *y> f() {}"); 9656 verifyFormat("vector<int *> v;"); 9657 verifyFormat("vector<int *const> v;"); 9658 verifyFormat("vector<int *const **const *> v;"); 9659 verifyFormat("vector<int *volatile> v;"); 9660 verifyFormat("vector<a *_Nonnull> v;"); 9661 verifyFormat("vector<a *_Nullable> v;"); 9662 verifyFormat("vector<a *_Null_unspecified> v;"); 9663 verifyFormat("vector<a *__ptr32> v;"); 9664 verifyFormat("vector<a *__ptr64> v;"); 9665 verifyFormat("vector<a *__capability> v;"); 9666 FormatStyle TypeMacros = getLLVMStyle(); 9667 TypeMacros.TypenameMacros = {"LIST"}; 9668 verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros); 9669 verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros); 9670 verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros); 9671 verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros); 9672 verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros); // multiplication 9673 9674 FormatStyle CustomQualifier = getLLVMStyle(); 9675 // Add identifiers that should not be parsed as a qualifier by default. 9676 CustomQualifier.AttributeMacros.push_back("__my_qualifier"); 9677 CustomQualifier.AttributeMacros.push_back("_My_qualifier"); 9678 CustomQualifier.AttributeMacros.push_back("my_other_qualifier"); 9679 verifyFormat("vector<a * __my_qualifier> parse_as_multiply;"); 9680 verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier); 9681 verifyFormat("vector<a * _My_qualifier> parse_as_multiply;"); 9682 verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier); 9683 verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;"); 9684 verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier); 9685 verifyFormat("vector<a * _NotAQualifier> v;"); 9686 verifyFormat("vector<a * __not_a_qualifier> v;"); 9687 verifyFormat("vector<a * b> v;"); 9688 verifyFormat("foo<b && false>();"); 9689 verifyFormat("foo<b & 1>();"); 9690 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 9691 verifyFormat("typeof(*::std::declval<const T &>()) void F();"); 9692 verifyFormat("_Atomic(*::std::declval<const T &>()) void F();"); 9693 verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();"); 9694 verifyFormat( 9695 "template <class T, class = typename std::enable_if<\n" 9696 " std::is_integral<T>::value &&\n" 9697 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 9698 "void F();", 9699 getLLVMStyleWithColumns(70)); 9700 verifyFormat("template <class T,\n" 9701 " class = typename std::enable_if<\n" 9702 " std::is_integral<T>::value &&\n" 9703 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n" 9704 " class U>\n" 9705 "void F();", 9706 getLLVMStyleWithColumns(70)); 9707 verifyFormat( 9708 "template <class T,\n" 9709 " class = typename ::std::enable_if<\n" 9710 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 9711 "void F();", 9712 getGoogleStyleWithColumns(68)); 9713 9714 verifyIndependentOfContext("MACRO(int *i);"); 9715 verifyIndependentOfContext("MACRO(auto *a);"); 9716 verifyIndependentOfContext("MACRO(const A *a);"); 9717 verifyIndependentOfContext("MACRO(_Atomic(A) *a);"); 9718 verifyIndependentOfContext("MACRO(decltype(A) *a);"); 9719 verifyIndependentOfContext("MACRO(typeof(A) *a);"); 9720 verifyIndependentOfContext("MACRO(__underlying_type(A) *a);"); 9721 verifyIndependentOfContext("MACRO(A *const a);"); 9722 verifyIndependentOfContext("MACRO(A *restrict a);"); 9723 verifyIndependentOfContext("MACRO(A *__restrict__ a);"); 9724 verifyIndependentOfContext("MACRO(A *__restrict a);"); 9725 verifyIndependentOfContext("MACRO(A *volatile a);"); 9726 verifyIndependentOfContext("MACRO(A *__volatile a);"); 9727 verifyIndependentOfContext("MACRO(A *__volatile__ a);"); 9728 verifyIndependentOfContext("MACRO(A *_Nonnull a);"); 9729 verifyIndependentOfContext("MACRO(A *_Nullable a);"); 9730 verifyIndependentOfContext("MACRO(A *_Null_unspecified a);"); 9731 verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);"); 9732 verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);"); 9733 verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);"); 9734 verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);"); 9735 verifyIndependentOfContext("MACRO(A *__ptr32 a);"); 9736 verifyIndependentOfContext("MACRO(A *__ptr64 a);"); 9737 verifyIndependentOfContext("MACRO(A *__capability);"); 9738 verifyIndependentOfContext("MACRO(A &__capability);"); 9739 verifyFormat("MACRO(A *__my_qualifier);"); // type declaration 9740 verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication 9741 // If we add __my_qualifier to AttributeMacros it should always be parsed as 9742 // a type declaration: 9743 verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier); 9744 verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier); 9745 // Also check that TypenameMacros prevents parsing it as multiplication: 9746 verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication 9747 verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros); // type 9748 9749 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 9750 verifyFormat("void f() { f(float{1}, a * a); }"); 9751 verifyFormat("void f() { f(float(1), a * a); }"); 9752 9753 verifyFormat("f((void (*)(int))g);"); 9754 verifyFormat("f((void (&)(int))g);"); 9755 verifyFormat("f((void (^)(int))g);"); 9756 9757 // FIXME: Is there a way to make this work? 9758 // verifyIndependentOfContext("MACRO(A *a);"); 9759 verifyFormat("MACRO(A &B);"); 9760 verifyFormat("MACRO(A *B);"); 9761 verifyFormat("void f() { MACRO(A * B); }"); 9762 verifyFormat("void f() { MACRO(A & B); }"); 9763 9764 // This lambda was mis-formatted after D88956 (treating it as a binop): 9765 verifyFormat("auto x = [](const decltype(x) &ptr) {};"); 9766 verifyFormat("auto x = [](const decltype(x) *ptr) {};"); 9767 verifyFormat("#define lambda [](const decltype(x) &ptr) {}"); 9768 verifyFormat("#define lambda [](const decltype(x) *ptr) {}"); 9769 9770 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 9771 verifyFormat("return options != nullptr && operator==(*options);"); 9772 9773 EXPECT_EQ("#define OP(x) \\\n" 9774 " ostream &operator<<(ostream &s, const A &a) { \\\n" 9775 " return s << a.DebugString(); \\\n" 9776 " }", 9777 format("#define OP(x) \\\n" 9778 " ostream &operator<<(ostream &s, const A &a) { \\\n" 9779 " return s << a.DebugString(); \\\n" 9780 " }", 9781 getLLVMStyleWithColumns(50))); 9782 9783 // FIXME: We cannot handle this case yet; we might be able to figure out that 9784 // foo<x> d > v; doesn't make sense. 9785 verifyFormat("foo<a<b && c> d> v;"); 9786 9787 FormatStyle PointerMiddle = getLLVMStyle(); 9788 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 9789 verifyFormat("delete *x;", PointerMiddle); 9790 verifyFormat("int * x;", PointerMiddle); 9791 verifyFormat("int *[] x;", PointerMiddle); 9792 verifyFormat("template <int * y> f() {}", PointerMiddle); 9793 verifyFormat("int * f(int * a) {}", PointerMiddle); 9794 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 9795 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 9796 verifyFormat("A<int *> a;", PointerMiddle); 9797 verifyFormat("A<int **> a;", PointerMiddle); 9798 verifyFormat("A<int *, int *> a;", PointerMiddle); 9799 verifyFormat("A<int *[]> a;", PointerMiddle); 9800 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 9801 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 9802 verifyFormat("T ** t = new T *;", PointerMiddle); 9803 9804 // Member function reference qualifiers aren't binary operators. 9805 verifyFormat("string // break\n" 9806 "operator()() & {}"); 9807 verifyFormat("string // break\n" 9808 "operator()() && {}"); 9809 verifyGoogleFormat("template <typename T>\n" 9810 "auto x() & -> int {}"); 9811 9812 // Should be binary operators when used as an argument expression (overloaded 9813 // operator invoked as a member function). 9814 verifyFormat("void f() { a.operator()(a * a); }"); 9815 verifyFormat("void f() { a->operator()(a & a); }"); 9816 verifyFormat("void f() { a.operator()(*a & *a); }"); 9817 verifyFormat("void f() { a->operator()(*a * *a); }"); 9818 9819 verifyFormat("int operator()(T (&&)[N]) { return 1; }"); 9820 verifyFormat("int operator()(T (&)[N]) { return 0; }"); 9821 } 9822 9823 TEST_F(FormatTest, UnderstandsAttributes) { 9824 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 9825 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 9826 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 9827 FormatStyle AfterType = getLLVMStyle(); 9828 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 9829 verifyFormat("__attribute__((nodebug)) void\n" 9830 "foo() {}\n", 9831 AfterType); 9832 verifyFormat("__unused void\n" 9833 "foo() {}", 9834 AfterType); 9835 9836 FormatStyle CustomAttrs = getLLVMStyle(); 9837 CustomAttrs.AttributeMacros.push_back("__unused"); 9838 CustomAttrs.AttributeMacros.push_back("__attr1"); 9839 CustomAttrs.AttributeMacros.push_back("__attr2"); 9840 CustomAttrs.AttributeMacros.push_back("no_underscore_attr"); 9841 verifyFormat("vector<SomeType *__attribute((foo))> v;"); 9842 verifyFormat("vector<SomeType *__attribute__((foo))> v;"); 9843 verifyFormat("vector<SomeType * __not_attribute__((foo))> v;"); 9844 // Check that it is parsed as a multiplication without AttributeMacros and 9845 // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros. 9846 verifyFormat("vector<SomeType * __attr1> v;"); 9847 verifyFormat("vector<SomeType __attr1 *> v;"); 9848 verifyFormat("vector<SomeType __attr1 *const> v;"); 9849 verifyFormat("vector<SomeType __attr1 * __attr2> v;"); 9850 verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs); 9851 verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs); 9852 verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs); 9853 verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs); 9854 verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs); 9855 verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs); 9856 verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs); 9857 9858 // Check that these are not parsed as function declarations: 9859 CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 9860 CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman; 9861 verifyFormat("SomeType s(InitValue);", CustomAttrs); 9862 verifyFormat("SomeType s{InitValue};", CustomAttrs); 9863 verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs); 9864 verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs); 9865 verifyFormat("SomeType s __unused(InitValue);", CustomAttrs); 9866 verifyFormat("SomeType s __unused{InitValue};", CustomAttrs); 9867 verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs); 9868 verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs); 9869 } 9870 9871 TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) { 9872 // Check that qualifiers on pointers don't break parsing of casts. 9873 verifyFormat("x = (foo *const)*v;"); 9874 verifyFormat("x = (foo *volatile)*v;"); 9875 verifyFormat("x = (foo *restrict)*v;"); 9876 verifyFormat("x = (foo *__attribute__((foo)))*v;"); 9877 verifyFormat("x = (foo *_Nonnull)*v;"); 9878 verifyFormat("x = (foo *_Nullable)*v;"); 9879 verifyFormat("x = (foo *_Null_unspecified)*v;"); 9880 verifyFormat("x = (foo *_Nonnull)*v;"); 9881 verifyFormat("x = (foo *[[clang::attr]])*v;"); 9882 verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;"); 9883 verifyFormat("x = (foo *__ptr32)*v;"); 9884 verifyFormat("x = (foo *__ptr64)*v;"); 9885 verifyFormat("x = (foo *__capability)*v;"); 9886 9887 // Check that we handle multiple trailing qualifiers and skip them all to 9888 // determine that the expression is a cast to a pointer type. 9889 FormatStyle LongPointerRight = getLLVMStyleWithColumns(999); 9890 FormatStyle LongPointerLeft = getLLVMStyleWithColumns(999); 9891 LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left; 9892 StringRef AllQualifiers = 9893 "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified " 9894 "_Nonnull [[clang::attr]] __ptr32 __ptr64 __capability"; 9895 verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight); 9896 verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft); 9897 9898 // Also check that address-of is not parsed as a binary bitwise-and: 9899 verifyFormat("x = (foo *const)&v;"); 9900 verifyFormat(("x = (foo *" + AllQualifiers + ")&v;").str(), LongPointerRight); 9901 verifyFormat(("x = (foo* " + AllQualifiers + ")&v;").str(), LongPointerLeft); 9902 9903 // Check custom qualifiers: 9904 FormatStyle CustomQualifier = getLLVMStyleWithColumns(999); 9905 CustomQualifier.AttributeMacros.push_back("__my_qualifier"); 9906 verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier. 9907 verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier); 9908 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)*v;").str(), 9909 CustomQualifier); 9910 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)&v;").str(), 9911 CustomQualifier); 9912 9913 // Check that unknown identifiers result in binary operator parsing: 9914 verifyFormat("x = (foo * __unknown_qualifier) * v;"); 9915 verifyFormat("x = (foo * __unknown_qualifier) & v;"); 9916 } 9917 9918 TEST_F(FormatTest, UnderstandsSquareAttributes) { 9919 verifyFormat("SomeType s [[unused]] (InitValue);"); 9920 verifyFormat("SomeType s [[gnu::unused]] (InitValue);"); 9921 verifyFormat("SomeType s [[using gnu: unused]] (InitValue);"); 9922 verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}"); 9923 verifyFormat("void f() [[deprecated(\"so sorry\")]];"); 9924 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9925 " [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 9926 verifyFormat("[[nodiscard]] bool f() { return false; }"); 9927 verifyFormat("class [[nodiscard]] f {\npublic:\n f() {}\n}"); 9928 verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n f() {}\n}"); 9929 verifyFormat("class [[gnu::unused]] f {\npublic:\n f() {}\n}"); 9930 9931 // Make sure we do not mistake attributes for array subscripts. 9932 verifyFormat("int a() {}\n" 9933 "[[unused]] int b() {}\n"); 9934 verifyFormat("NSArray *arr;\n" 9935 "arr[[Foo() bar]];"); 9936 9937 // On the other hand, we still need to correctly find array subscripts. 9938 verifyFormat("int a = std::vector<int>{1, 2, 3}[0];"); 9939 9940 // Make sure that we do not mistake Objective-C method inside array literals 9941 // as attributes, even if those method names are also keywords. 9942 verifyFormat("@[ [foo bar] ];"); 9943 verifyFormat("@[ [NSArray class] ];"); 9944 verifyFormat("@[ [foo enum] ];"); 9945 9946 verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }"); 9947 9948 // Make sure we do not parse attributes as lambda introducers. 9949 FormatStyle MultiLineFunctions = getLLVMStyle(); 9950 MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 9951 verifyFormat("[[unused]] int b() {\n" 9952 " return 42;\n" 9953 "}\n", 9954 MultiLineFunctions); 9955 } 9956 9957 TEST_F(FormatTest, AttributeClass) { 9958 FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp); 9959 verifyFormat("class S {\n" 9960 " S(S&&) = default;\n" 9961 "};", 9962 Style); 9963 verifyFormat("class [[nodiscard]] S {\n" 9964 " S(S&&) = default;\n" 9965 "};", 9966 Style); 9967 verifyFormat("class __attribute((maybeunused)) S {\n" 9968 " S(S&&) = default;\n" 9969 "};", 9970 Style); 9971 verifyFormat("struct S {\n" 9972 " S(S&&) = default;\n" 9973 "};", 9974 Style); 9975 verifyFormat("struct [[nodiscard]] S {\n" 9976 " S(S&&) = default;\n" 9977 "};", 9978 Style); 9979 } 9980 9981 TEST_F(FormatTest, AttributesAfterMacro) { 9982 FormatStyle Style = getLLVMStyle(); 9983 verifyFormat("MACRO;\n" 9984 "__attribute__((maybe_unused)) int foo() {\n" 9985 " //...\n" 9986 "}"); 9987 9988 verifyFormat("MACRO;\n" 9989 "[[nodiscard]] int foo() {\n" 9990 " //...\n" 9991 "}"); 9992 9993 EXPECT_EQ("MACRO\n\n" 9994 "__attribute__((maybe_unused)) int foo() {\n" 9995 " //...\n" 9996 "}", 9997 format("MACRO\n\n" 9998 "__attribute__((maybe_unused)) int foo() {\n" 9999 " //...\n" 10000 "}")); 10001 10002 EXPECT_EQ("MACRO\n\n" 10003 "[[nodiscard]] int foo() {\n" 10004 " //...\n" 10005 "}", 10006 format("MACRO\n\n" 10007 "[[nodiscard]] int foo() {\n" 10008 " //...\n" 10009 "}")); 10010 } 10011 10012 TEST_F(FormatTest, AttributePenaltyBreaking) { 10013 FormatStyle Style = getLLVMStyle(); 10014 verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n" 10015 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", 10016 Style); 10017 verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n" 10018 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", 10019 Style); 10020 verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const " 10021 "shared_ptr<ALongTypeName> &C d) {\n}", 10022 Style); 10023 } 10024 10025 TEST_F(FormatTest, UnderstandsEllipsis) { 10026 FormatStyle Style = getLLVMStyle(); 10027 verifyFormat("int printf(const char *fmt, ...);"); 10028 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 10029 verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}"); 10030 10031 verifyFormat("template <int *...PP> a;", Style); 10032 10033 Style.PointerAlignment = FormatStyle::PAS_Left; 10034 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style); 10035 10036 verifyFormat("template <int*... PP> a;", Style); 10037 10038 Style.PointerAlignment = FormatStyle::PAS_Middle; 10039 verifyFormat("template <int *... PP> a;", Style); 10040 } 10041 10042 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 10043 EXPECT_EQ("int *a;\n" 10044 "int *a;\n" 10045 "int *a;", 10046 format("int *a;\n" 10047 "int* a;\n" 10048 "int *a;", 10049 getGoogleStyle())); 10050 EXPECT_EQ("int* a;\n" 10051 "int* a;\n" 10052 "int* a;", 10053 format("int* a;\n" 10054 "int* a;\n" 10055 "int *a;", 10056 getGoogleStyle())); 10057 EXPECT_EQ("int *a;\n" 10058 "int *a;\n" 10059 "int *a;", 10060 format("int *a;\n" 10061 "int * a;\n" 10062 "int * a;", 10063 getGoogleStyle())); 10064 EXPECT_EQ("auto x = [] {\n" 10065 " int *a;\n" 10066 " int *a;\n" 10067 " int *a;\n" 10068 "};", 10069 format("auto x=[]{int *a;\n" 10070 "int * a;\n" 10071 "int * a;};", 10072 getGoogleStyle())); 10073 } 10074 10075 TEST_F(FormatTest, UnderstandsRvalueReferences) { 10076 verifyFormat("int f(int &&a) {}"); 10077 verifyFormat("int f(int a, char &&b) {}"); 10078 verifyFormat("void f() { int &&a = b; }"); 10079 verifyGoogleFormat("int f(int a, char&& b) {}"); 10080 verifyGoogleFormat("void f() { int&& a = b; }"); 10081 10082 verifyIndependentOfContext("A<int &&> a;"); 10083 verifyIndependentOfContext("A<int &&, int &&> a;"); 10084 verifyGoogleFormat("A<int&&> a;"); 10085 verifyGoogleFormat("A<int&&, int&&> a;"); 10086 10087 // Not rvalue references: 10088 verifyFormat("template <bool B, bool C> class A {\n" 10089 " static_assert(B && C, \"Something is wrong\");\n" 10090 "};"); 10091 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 10092 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 10093 verifyFormat("#define A(a, b) (a && b)"); 10094 } 10095 10096 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 10097 verifyFormat("void f() {\n" 10098 " x[aaaaaaaaa -\n" 10099 " b] = 23;\n" 10100 "}", 10101 getLLVMStyleWithColumns(15)); 10102 } 10103 10104 TEST_F(FormatTest, FormatsCasts) { 10105 verifyFormat("Type *A = static_cast<Type *>(P);"); 10106 verifyFormat("Type *A = (Type *)P;"); 10107 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 10108 verifyFormat("int a = (int)(2.0f);"); 10109 verifyFormat("int a = (int)2.0f;"); 10110 verifyFormat("x[(int32)y];"); 10111 verifyFormat("x = (int32)y;"); 10112 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 10113 verifyFormat("int a = (int)*b;"); 10114 verifyFormat("int a = (int)2.0f;"); 10115 verifyFormat("int a = (int)~0;"); 10116 verifyFormat("int a = (int)++a;"); 10117 verifyFormat("int a = (int)sizeof(int);"); 10118 verifyFormat("int a = (int)+2;"); 10119 verifyFormat("my_int a = (my_int)2.0f;"); 10120 verifyFormat("my_int a = (my_int)sizeof(int);"); 10121 verifyFormat("return (my_int)aaa;"); 10122 verifyFormat("#define x ((int)-1)"); 10123 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 10124 verifyFormat("#define p(q) ((int *)&q)"); 10125 verifyFormat("fn(a)(b) + 1;"); 10126 10127 verifyFormat("void f() { my_int a = (my_int)*b; }"); 10128 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 10129 verifyFormat("my_int a = (my_int)~0;"); 10130 verifyFormat("my_int a = (my_int)++a;"); 10131 verifyFormat("my_int a = (my_int)-2;"); 10132 verifyFormat("my_int a = (my_int)1;"); 10133 verifyFormat("my_int a = (my_int *)1;"); 10134 verifyFormat("my_int a = (const my_int)-1;"); 10135 verifyFormat("my_int a = (const my_int *)-1;"); 10136 verifyFormat("my_int a = (my_int)(my_int)-1;"); 10137 verifyFormat("my_int a = (ns::my_int)-2;"); 10138 verifyFormat("case (my_int)ONE:"); 10139 verifyFormat("auto x = (X)this;"); 10140 // Casts in Obj-C style calls used to not be recognized as such. 10141 verifyFormat("int a = [(type*)[((type*)val) arg] arg];", getGoogleStyle()); 10142 10143 // FIXME: single value wrapped with paren will be treated as cast. 10144 verifyFormat("void f(int i = (kValue)*kMask) {}"); 10145 10146 verifyFormat("{ (void)F; }"); 10147 10148 // Don't break after a cast's 10149 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 10150 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 10151 " bbbbbbbbbbbbbbbbbbbbbb);"); 10152 10153 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(x)"); 10154 verifyFormat("#define CONF_BOOL(x) (bool *)(x)"); 10155 verifyFormat("#define CONF_BOOL(x) (bool)(x)"); 10156 verifyFormat("bool *y = (bool *)(void *)(x);"); 10157 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)(x)"); 10158 verifyFormat("bool *y = (bool *)(void *)(int)(x);"); 10159 verifyFormat("#define CONF_BOOL(x) (bool *)(void *)(int)foo(x)"); 10160 verifyFormat("bool *y = (bool *)(void *)(int)foo(x);"); 10161 10162 // These are not casts. 10163 verifyFormat("void f(int *) {}"); 10164 verifyFormat("f(foo)->b;"); 10165 verifyFormat("f(foo).b;"); 10166 verifyFormat("f(foo)(b);"); 10167 verifyFormat("f(foo)[b];"); 10168 verifyFormat("[](foo) { return 4; }(bar);"); 10169 verifyFormat("(*funptr)(foo)[4];"); 10170 verifyFormat("funptrs[4](foo)[4];"); 10171 verifyFormat("void f(int *);"); 10172 verifyFormat("void f(int *) = 0;"); 10173 verifyFormat("void f(SmallVector<int>) {}"); 10174 verifyFormat("void f(SmallVector<int>);"); 10175 verifyFormat("void f(SmallVector<int>) = 0;"); 10176 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 10177 verifyFormat("int a = sizeof(int) * b;"); 10178 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 10179 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 10180 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 10181 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 10182 10183 // These are not casts, but at some point were confused with casts. 10184 verifyFormat("virtual void foo(int *) override;"); 10185 verifyFormat("virtual void foo(char &) const;"); 10186 verifyFormat("virtual void foo(int *a, char *) const;"); 10187 verifyFormat("int a = sizeof(int *) + b;"); 10188 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 10189 verifyFormat("bool b = f(g<int>) && c;"); 10190 verifyFormat("typedef void (*f)(int i) func;"); 10191 verifyFormat("void operator++(int) noexcept;"); 10192 verifyFormat("void operator++(int &) noexcept;"); 10193 verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t " 10194 "&) noexcept;"); 10195 verifyFormat( 10196 "void operator delete(std::size_t, const std::nothrow_t &) noexcept;"); 10197 verifyFormat("void operator delete(const std::nothrow_t &) noexcept;"); 10198 verifyFormat("void operator delete(std::nothrow_t &) noexcept;"); 10199 verifyFormat("void operator delete(nothrow_t &) noexcept;"); 10200 verifyFormat("void operator delete(foo &) noexcept;"); 10201 verifyFormat("void operator delete(foo) noexcept;"); 10202 verifyFormat("void operator delete(int) noexcept;"); 10203 verifyFormat("void operator delete(int &) noexcept;"); 10204 verifyFormat("void operator delete(int &) volatile noexcept;"); 10205 verifyFormat("void operator delete(int &) const"); 10206 verifyFormat("void operator delete(int &) = default"); 10207 verifyFormat("void operator delete(int &) = delete"); 10208 verifyFormat("void operator delete(int &) [[noreturn]]"); 10209 verifyFormat("void operator delete(int &) throw();"); 10210 verifyFormat("void operator delete(int &) throw(int);"); 10211 verifyFormat("auto operator delete(int &) -> int;"); 10212 verifyFormat("auto operator delete(int &) override"); 10213 verifyFormat("auto operator delete(int &) final"); 10214 10215 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 10216 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 10217 // FIXME: The indentation here is not ideal. 10218 verifyFormat( 10219 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10220 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 10221 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 10222 } 10223 10224 TEST_F(FormatTest, FormatsFunctionTypes) { 10225 verifyFormat("A<bool()> a;"); 10226 verifyFormat("A<SomeType()> a;"); 10227 verifyFormat("A<void (*)(int, std::string)> a;"); 10228 verifyFormat("A<void *(int)>;"); 10229 verifyFormat("void *(*a)(int *, SomeType *);"); 10230 verifyFormat("int (*func)(void *);"); 10231 verifyFormat("void f() { int (*func)(void *); }"); 10232 verifyFormat("template <class CallbackClass>\n" 10233 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 10234 10235 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 10236 verifyGoogleFormat("void* (*a)(int);"); 10237 verifyGoogleFormat( 10238 "template <class CallbackClass>\n" 10239 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 10240 10241 // Other constructs can look somewhat like function types: 10242 verifyFormat("A<sizeof(*x)> a;"); 10243 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 10244 verifyFormat("some_var = function(*some_pointer_var)[0];"); 10245 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 10246 verifyFormat("int x = f(&h)();"); 10247 verifyFormat("returnsFunction(¶m1, ¶m2)(param);"); 10248 verifyFormat("std::function<\n" 10249 " LooooooooooongTemplatedType<\n" 10250 " SomeType>*(\n" 10251 " LooooooooooooooooongType type)>\n" 10252 " function;", 10253 getGoogleStyleWithColumns(40)); 10254 } 10255 10256 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 10257 verifyFormat("A (*foo_)[6];"); 10258 verifyFormat("vector<int> (*foo_)[6];"); 10259 } 10260 10261 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 10262 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10263 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 10264 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 10265 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 10266 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10267 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 10268 10269 // Different ways of ()-initializiation. 10270 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10271 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 10272 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10273 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 10274 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10275 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 10276 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 10277 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 10278 10279 // Lambdas should not confuse the variable declaration heuristic. 10280 verifyFormat("LooooooooooooooooongType\n" 10281 " variable(nullptr, [](A *a) {});", 10282 getLLVMStyleWithColumns(40)); 10283 } 10284 10285 TEST_F(FormatTest, BreaksLongDeclarations) { 10286 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 10287 " AnotherNameForTheLongType;"); 10288 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 10289 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 10290 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10291 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 10292 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 10293 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 10294 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10295 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10296 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 10297 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10298 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 10299 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10300 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 10301 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10302 verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n" 10303 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10304 verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n" 10305 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10306 verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n" 10307 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 10308 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10309 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);"); 10310 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10311 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}"); 10312 FormatStyle Indented = getLLVMStyle(); 10313 Indented.IndentWrappedFunctionNames = true; 10314 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10315 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 10316 Indented); 10317 verifyFormat( 10318 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 10319 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 10320 Indented); 10321 verifyFormat( 10322 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 10323 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 10324 Indented); 10325 verifyFormat( 10326 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 10327 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 10328 Indented); 10329 10330 // FIXME: Without the comment, this breaks after "(". 10331 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 10332 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 10333 getGoogleStyle()); 10334 10335 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 10336 " int LoooooooooooooooooooongParam2) {}"); 10337 verifyFormat( 10338 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 10339 " SourceLocation L, IdentifierIn *II,\n" 10340 " Type *T) {}"); 10341 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 10342 "ReallyReaaallyLongFunctionName(\n" 10343 " const std::string &SomeParameter,\n" 10344 " const SomeType<string, SomeOtherTemplateParameter>\n" 10345 " &ReallyReallyLongParameterName,\n" 10346 " const SomeType<string, SomeOtherTemplateParameter>\n" 10347 " &AnotherLongParameterName) {}"); 10348 verifyFormat("template <typename A>\n" 10349 "SomeLoooooooooooooooooooooongType<\n" 10350 " typename some_namespace::SomeOtherType<A>::Type>\n" 10351 "Function() {}"); 10352 10353 verifyGoogleFormat( 10354 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 10355 " aaaaaaaaaaaaaaaaaaaaaaa;"); 10356 verifyGoogleFormat( 10357 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 10358 " SourceLocation L) {}"); 10359 verifyGoogleFormat( 10360 "some_namespace::LongReturnType\n" 10361 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 10362 " int first_long_parameter, int second_parameter) {}"); 10363 10364 verifyGoogleFormat("template <typename T>\n" 10365 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 10366 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 10367 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 10368 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 10369 10370 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 10371 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10372 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 10373 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10374 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 10375 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 10376 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 10377 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 10378 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 10379 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 10380 10381 verifyFormat("template <typename T> // Templates on own line.\n" 10382 "static int // Some comment.\n" 10383 "MyFunction(int a);", 10384 getLLVMStyle()); 10385 } 10386 10387 TEST_F(FormatTest, FormatsAccessModifiers) { 10388 FormatStyle Style = getLLVMStyle(); 10389 EXPECT_EQ(Style.EmptyLineBeforeAccessModifier, 10390 FormatStyle::ELBAMS_LogicalBlock); 10391 verifyFormat("struct foo {\n" 10392 "private:\n" 10393 " void f() {}\n" 10394 "\n" 10395 "private:\n" 10396 " int i;\n" 10397 "\n" 10398 "protected:\n" 10399 " int j;\n" 10400 "};\n", 10401 Style); 10402 verifyFormat("struct foo {\n" 10403 "private:\n" 10404 " void f() {}\n" 10405 "\n" 10406 "private:\n" 10407 " int i;\n" 10408 "\n" 10409 "protected:\n" 10410 " int j;\n" 10411 "};\n", 10412 "struct foo {\n" 10413 "private:\n" 10414 " void f() {}\n" 10415 "private:\n" 10416 " int i;\n" 10417 "protected:\n" 10418 " int j;\n" 10419 "};\n", 10420 Style); 10421 verifyFormat("struct foo { /* comment */\n" 10422 "private:\n" 10423 " int i;\n" 10424 " // comment\n" 10425 "private:\n" 10426 " int j;\n" 10427 "};\n", 10428 Style); 10429 verifyFormat("struct foo {\n" 10430 "#ifdef FOO\n" 10431 "#endif\n" 10432 "private:\n" 10433 " int i;\n" 10434 "#ifdef FOO\n" 10435 "private:\n" 10436 "#endif\n" 10437 " int j;\n" 10438 "};\n", 10439 Style); 10440 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10441 verifyFormat("struct foo {\n" 10442 "private:\n" 10443 " void f() {}\n" 10444 "private:\n" 10445 " int i;\n" 10446 "protected:\n" 10447 " int j;\n" 10448 "};\n", 10449 Style); 10450 verifyFormat("struct foo {\n" 10451 "private:\n" 10452 " void f() {}\n" 10453 "private:\n" 10454 " int i;\n" 10455 "protected:\n" 10456 " int j;\n" 10457 "};\n", 10458 "struct foo {\n" 10459 "\n" 10460 "private:\n" 10461 " void f() {}\n" 10462 "\n" 10463 "private:\n" 10464 " int i;\n" 10465 "\n" 10466 "protected:\n" 10467 " int j;\n" 10468 "};\n", 10469 Style); 10470 verifyFormat("struct foo { /* comment */\n" 10471 "private:\n" 10472 " int i;\n" 10473 " // comment\n" 10474 "private:\n" 10475 " int j;\n" 10476 "};\n", 10477 "struct foo { /* comment */\n" 10478 "\n" 10479 "private:\n" 10480 " int i;\n" 10481 " // comment\n" 10482 "\n" 10483 "private:\n" 10484 " int j;\n" 10485 "};\n", 10486 Style); 10487 verifyFormat("struct foo {\n" 10488 "#ifdef FOO\n" 10489 "#endif\n" 10490 "private:\n" 10491 " int i;\n" 10492 "#ifdef FOO\n" 10493 "private:\n" 10494 "#endif\n" 10495 " int j;\n" 10496 "};\n", 10497 "struct foo {\n" 10498 "#ifdef FOO\n" 10499 "#endif\n" 10500 "\n" 10501 "private:\n" 10502 " int i;\n" 10503 "#ifdef FOO\n" 10504 "\n" 10505 "private:\n" 10506 "#endif\n" 10507 " int j;\n" 10508 "};\n", 10509 Style); 10510 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 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 Style); 10522 verifyFormat("struct foo {\n" 10523 "private:\n" 10524 " void f() {}\n" 10525 "\n" 10526 "private:\n" 10527 " int i;\n" 10528 "\n" 10529 "protected:\n" 10530 " int j;\n" 10531 "};\n", 10532 "struct foo {\n" 10533 "private:\n" 10534 " void f() {}\n" 10535 "private:\n" 10536 " int i;\n" 10537 "protected:\n" 10538 " int j;\n" 10539 "};\n", 10540 Style); 10541 verifyFormat("struct foo { /* comment */\n" 10542 "private:\n" 10543 " int i;\n" 10544 " // comment\n" 10545 "\n" 10546 "private:\n" 10547 " int j;\n" 10548 "};\n", 10549 "struct foo { /* comment */\n" 10550 "private:\n" 10551 " int i;\n" 10552 " // comment\n" 10553 "\n" 10554 "private:\n" 10555 " int j;\n" 10556 "};\n", 10557 Style); 10558 verifyFormat("struct foo {\n" 10559 "#ifdef FOO\n" 10560 "#endif\n" 10561 "\n" 10562 "private:\n" 10563 " int i;\n" 10564 "#ifdef FOO\n" 10565 "\n" 10566 "private:\n" 10567 "#endif\n" 10568 " int j;\n" 10569 "};\n", 10570 "struct foo {\n" 10571 "#ifdef FOO\n" 10572 "#endif\n" 10573 "private:\n" 10574 " int i;\n" 10575 "#ifdef FOO\n" 10576 "private:\n" 10577 "#endif\n" 10578 " int j;\n" 10579 "};\n", 10580 Style); 10581 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 10582 EXPECT_EQ("struct foo {\n" 10583 "\n" 10584 "private:\n" 10585 " void f() {}\n" 10586 "\n" 10587 "private:\n" 10588 " int i;\n" 10589 "\n" 10590 "protected:\n" 10591 " int j;\n" 10592 "};\n", 10593 format("struct foo {\n" 10594 "\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 Style)); 10605 verifyFormat("struct foo {\n" 10606 "private:\n" 10607 " void f() {}\n" 10608 "private:\n" 10609 " int i;\n" 10610 "protected:\n" 10611 " int j;\n" 10612 "};\n", 10613 Style); 10614 EXPECT_EQ("struct foo { /* comment */\n" 10615 "\n" 10616 "private:\n" 10617 " int i;\n" 10618 " // comment\n" 10619 "\n" 10620 "private:\n" 10621 " int j;\n" 10622 "};\n", 10623 format("struct foo { /* comment */\n" 10624 "\n" 10625 "private:\n" 10626 " int i;\n" 10627 " // comment\n" 10628 "\n" 10629 "private:\n" 10630 " int j;\n" 10631 "};\n", 10632 Style)); 10633 verifyFormat("struct foo { /* comment */\n" 10634 "private:\n" 10635 " int i;\n" 10636 " // comment\n" 10637 "private:\n" 10638 " int j;\n" 10639 "};\n", 10640 Style); 10641 EXPECT_EQ("struct foo {\n" 10642 "#ifdef FOO\n" 10643 "#endif\n" 10644 "\n" 10645 "private:\n" 10646 " int i;\n" 10647 "#ifdef FOO\n" 10648 "\n" 10649 "private:\n" 10650 "#endif\n" 10651 " int j;\n" 10652 "};\n", 10653 format("struct foo {\n" 10654 "#ifdef FOO\n" 10655 "#endif\n" 10656 "\n" 10657 "private:\n" 10658 " int i;\n" 10659 "#ifdef FOO\n" 10660 "\n" 10661 "private:\n" 10662 "#endif\n" 10663 " int j;\n" 10664 "};\n", 10665 Style)); 10666 verifyFormat("struct foo {\n" 10667 "#ifdef FOO\n" 10668 "#endif\n" 10669 "private:\n" 10670 " int i;\n" 10671 "#ifdef FOO\n" 10672 "private:\n" 10673 "#endif\n" 10674 " int j;\n" 10675 "};\n", 10676 Style); 10677 10678 FormatStyle NoEmptyLines = getLLVMStyle(); 10679 NoEmptyLines.MaxEmptyLinesToKeep = 0; 10680 verifyFormat("struct foo {\n" 10681 "private:\n" 10682 " void f() {}\n" 10683 "\n" 10684 "private:\n" 10685 " int i;\n" 10686 "\n" 10687 "public:\n" 10688 "protected:\n" 10689 " int j;\n" 10690 "};\n", 10691 NoEmptyLines); 10692 10693 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10694 verifyFormat("struct foo {\n" 10695 "private:\n" 10696 " void f() {}\n" 10697 "private:\n" 10698 " int i;\n" 10699 "public:\n" 10700 "protected:\n" 10701 " int j;\n" 10702 "};\n", 10703 NoEmptyLines); 10704 10705 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10706 verifyFormat("struct foo {\n" 10707 "private:\n" 10708 " void f() {}\n" 10709 "\n" 10710 "private:\n" 10711 " int i;\n" 10712 "\n" 10713 "public:\n" 10714 "\n" 10715 "protected:\n" 10716 " int j;\n" 10717 "};\n", 10718 NoEmptyLines); 10719 } 10720 10721 TEST_F(FormatTest, FormatsAfterAccessModifiers) { 10722 10723 FormatStyle Style = getLLVMStyle(); 10724 EXPECT_EQ(Style.EmptyLineAfterAccessModifier, FormatStyle::ELAAMS_Never); 10725 verifyFormat("struct foo {\n" 10726 "private:\n" 10727 " void f() {}\n" 10728 "\n" 10729 "private:\n" 10730 " int i;\n" 10731 "\n" 10732 "protected:\n" 10733 " int j;\n" 10734 "};\n", 10735 Style); 10736 10737 // Check if lines are removed. 10738 verifyFormat("struct foo {\n" 10739 "private:\n" 10740 " void f() {}\n" 10741 "\n" 10742 "private:\n" 10743 " int i;\n" 10744 "\n" 10745 "protected:\n" 10746 " int j;\n" 10747 "};\n", 10748 "struct foo {\n" 10749 "private:\n" 10750 "\n" 10751 " void f() {}\n" 10752 "\n" 10753 "private:\n" 10754 "\n" 10755 " int i;\n" 10756 "\n" 10757 "protected:\n" 10758 "\n" 10759 " int j;\n" 10760 "};\n", 10761 Style); 10762 10763 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10764 verifyFormat("struct foo {\n" 10765 "private:\n" 10766 "\n" 10767 " void f() {}\n" 10768 "\n" 10769 "private:\n" 10770 "\n" 10771 " int i;\n" 10772 "\n" 10773 "protected:\n" 10774 "\n" 10775 " int j;\n" 10776 "};\n", 10777 Style); 10778 10779 // Check if lines are added. 10780 verifyFormat("struct foo {\n" 10781 "private:\n" 10782 "\n" 10783 " void f() {}\n" 10784 "\n" 10785 "private:\n" 10786 "\n" 10787 " int i;\n" 10788 "\n" 10789 "protected:\n" 10790 "\n" 10791 " int j;\n" 10792 "};\n", 10793 "struct foo {\n" 10794 "private:\n" 10795 " void f() {}\n" 10796 "\n" 10797 "private:\n" 10798 " int i;\n" 10799 "\n" 10800 "protected:\n" 10801 " int j;\n" 10802 "};\n", 10803 Style); 10804 10805 // Leave tests rely on the code layout, test::messUp can not be used. 10806 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 10807 Style.MaxEmptyLinesToKeep = 0u; 10808 verifyFormat("struct foo {\n" 10809 "private:\n" 10810 " void f() {}\n" 10811 "\n" 10812 "private:\n" 10813 " int i;\n" 10814 "\n" 10815 "protected:\n" 10816 " int j;\n" 10817 "};\n", 10818 Style); 10819 10820 // Check if MaxEmptyLinesToKeep is respected. 10821 EXPECT_EQ("struct foo {\n" 10822 "private:\n" 10823 " void f() {}\n" 10824 "\n" 10825 "private:\n" 10826 " int i;\n" 10827 "\n" 10828 "protected:\n" 10829 " int j;\n" 10830 "};\n", 10831 format("struct foo {\n" 10832 "private:\n" 10833 "\n\n\n" 10834 " void f() {}\n" 10835 "\n" 10836 "private:\n" 10837 "\n\n\n" 10838 " int i;\n" 10839 "\n" 10840 "protected:\n" 10841 "\n\n\n" 10842 " int j;\n" 10843 "};\n", 10844 Style)); 10845 10846 Style.MaxEmptyLinesToKeep = 1u; 10847 EXPECT_EQ("struct foo {\n" 10848 "private:\n" 10849 "\n" 10850 " void f() {}\n" 10851 "\n" 10852 "private:\n" 10853 "\n" 10854 " int i;\n" 10855 "\n" 10856 "protected:\n" 10857 "\n" 10858 " int j;\n" 10859 "};\n", 10860 format("struct foo {\n" 10861 "private:\n" 10862 "\n" 10863 " void f() {}\n" 10864 "\n" 10865 "private:\n" 10866 "\n" 10867 " int i;\n" 10868 "\n" 10869 "protected:\n" 10870 "\n" 10871 " int j;\n" 10872 "};\n", 10873 Style)); 10874 // Check if no lines are kept. 10875 EXPECT_EQ("struct foo {\n" 10876 "private:\n" 10877 " void f() {}\n" 10878 "\n" 10879 "private:\n" 10880 " int i;\n" 10881 "\n" 10882 "protected:\n" 10883 " int j;\n" 10884 "};\n", 10885 format("struct foo {\n" 10886 "private:\n" 10887 " void f() {}\n" 10888 "\n" 10889 "private:\n" 10890 " int i;\n" 10891 "\n" 10892 "protected:\n" 10893 " int j;\n" 10894 "};\n", 10895 Style)); 10896 // Check if MaxEmptyLinesToKeep is respected. 10897 EXPECT_EQ("struct foo {\n" 10898 "private:\n" 10899 "\n" 10900 " void f() {}\n" 10901 "\n" 10902 "private:\n" 10903 "\n" 10904 " int i;\n" 10905 "\n" 10906 "protected:\n" 10907 "\n" 10908 " int j;\n" 10909 "};\n", 10910 format("struct foo {\n" 10911 "private:\n" 10912 "\n\n\n" 10913 " void f() {}\n" 10914 "\n" 10915 "private:\n" 10916 "\n\n\n" 10917 " int i;\n" 10918 "\n" 10919 "protected:\n" 10920 "\n\n\n" 10921 " int j;\n" 10922 "};\n", 10923 Style)); 10924 10925 Style.MaxEmptyLinesToKeep = 10u; 10926 EXPECT_EQ("struct foo {\n" 10927 "private:\n" 10928 "\n\n\n" 10929 " void f() {}\n" 10930 "\n" 10931 "private:\n" 10932 "\n\n\n" 10933 " int i;\n" 10934 "\n" 10935 "protected:\n" 10936 "\n\n\n" 10937 " int j;\n" 10938 "};\n", 10939 format("struct foo {\n" 10940 "private:\n" 10941 "\n\n\n" 10942 " void f() {}\n" 10943 "\n" 10944 "private:\n" 10945 "\n\n\n" 10946 " int i;\n" 10947 "\n" 10948 "protected:\n" 10949 "\n\n\n" 10950 " int j;\n" 10951 "};\n", 10952 Style)); 10953 10954 // Test with comments. 10955 Style = getLLVMStyle(); 10956 verifyFormat("struct foo {\n" 10957 "private:\n" 10958 " // comment\n" 10959 " void f() {}\n" 10960 "\n" 10961 "private: /* comment */\n" 10962 " int i;\n" 10963 "};\n", 10964 Style); 10965 verifyFormat("struct foo {\n" 10966 "private:\n" 10967 " // comment\n" 10968 " void f() {}\n" 10969 "\n" 10970 "private: /* comment */\n" 10971 " int i;\n" 10972 "};\n", 10973 "struct foo {\n" 10974 "private:\n" 10975 "\n" 10976 " // comment\n" 10977 " void f() {}\n" 10978 "\n" 10979 "private: /* comment */\n" 10980 "\n" 10981 " int i;\n" 10982 "};\n", 10983 Style); 10984 10985 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10986 verifyFormat("struct foo {\n" 10987 "private:\n" 10988 "\n" 10989 " // comment\n" 10990 " void f() {}\n" 10991 "\n" 10992 "private: /* comment */\n" 10993 "\n" 10994 " int i;\n" 10995 "};\n", 10996 "struct foo {\n" 10997 "private:\n" 10998 " // comment\n" 10999 " void f() {}\n" 11000 "\n" 11001 "private: /* comment */\n" 11002 " int i;\n" 11003 "};\n", 11004 Style); 11005 verifyFormat("struct foo {\n" 11006 "private:\n" 11007 "\n" 11008 " // comment\n" 11009 " void f() {}\n" 11010 "\n" 11011 "private: /* comment */\n" 11012 "\n" 11013 " int i;\n" 11014 "};\n", 11015 Style); 11016 11017 // Test with preprocessor defines. 11018 Style = getLLVMStyle(); 11019 verifyFormat("struct foo {\n" 11020 "private:\n" 11021 "#ifdef FOO\n" 11022 "#endif\n" 11023 " void f() {}\n" 11024 "};\n", 11025 Style); 11026 verifyFormat("struct foo {\n" 11027 "private:\n" 11028 "#ifdef FOO\n" 11029 "#endif\n" 11030 " void f() {}\n" 11031 "};\n", 11032 "struct foo {\n" 11033 "private:\n" 11034 "\n" 11035 "#ifdef FOO\n" 11036 "#endif\n" 11037 " void f() {}\n" 11038 "};\n", 11039 Style); 11040 11041 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11042 verifyFormat("struct foo {\n" 11043 "private:\n" 11044 "\n" 11045 "#ifdef FOO\n" 11046 "#endif\n" 11047 " void f() {}\n" 11048 "};\n", 11049 "struct foo {\n" 11050 "private:\n" 11051 "#ifdef FOO\n" 11052 "#endif\n" 11053 " void f() {}\n" 11054 "};\n", 11055 Style); 11056 verifyFormat("struct foo {\n" 11057 "private:\n" 11058 "\n" 11059 "#ifdef FOO\n" 11060 "#endif\n" 11061 " void f() {}\n" 11062 "};\n", 11063 Style); 11064 } 11065 11066 TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) { 11067 // Combined tests of EmptyLineAfterAccessModifier and 11068 // EmptyLineBeforeAccessModifier. 11069 FormatStyle Style = getLLVMStyle(); 11070 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 11071 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11072 verifyFormat("struct foo {\n" 11073 "private:\n" 11074 "\n" 11075 "protected:\n" 11076 "};\n", 11077 Style); 11078 11079 Style.MaxEmptyLinesToKeep = 10u; 11080 // Both remove all new lines. 11081 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 11082 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 11083 verifyFormat("struct foo {\n" 11084 "private:\n" 11085 "protected:\n" 11086 "};\n", 11087 "struct foo {\n" 11088 "private:\n" 11089 "\n\n\n" 11090 "protected:\n" 11091 "};\n", 11092 Style); 11093 11094 // Leave tests rely on the code layout, test::messUp can not be used. 11095 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 11096 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 11097 Style.MaxEmptyLinesToKeep = 10u; 11098 EXPECT_EQ("struct foo {\n" 11099 "private:\n" 11100 "\n\n\n" 11101 "protected:\n" 11102 "};\n", 11103 format("struct foo {\n" 11104 "private:\n" 11105 "\n\n\n" 11106 "protected:\n" 11107 "};\n", 11108 Style)); 11109 Style.MaxEmptyLinesToKeep = 3u; 11110 EXPECT_EQ("struct foo {\n" 11111 "private:\n" 11112 "\n\n\n" 11113 "protected:\n" 11114 "};\n", 11115 format("struct foo {\n" 11116 "private:\n" 11117 "\n\n\n" 11118 "protected:\n" 11119 "};\n", 11120 Style)); 11121 Style.MaxEmptyLinesToKeep = 1u; 11122 EXPECT_EQ("struct foo {\n" 11123 "private:\n" 11124 "\n\n\n" 11125 "protected:\n" 11126 "};\n", 11127 format("struct foo {\n" 11128 "private:\n" 11129 "\n\n\n" 11130 "protected:\n" 11131 "};\n", 11132 Style)); // Based on new lines in original document and not 11133 // on the setting. 11134 11135 Style.MaxEmptyLinesToKeep = 10u; 11136 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 11137 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 11138 // Newlines are kept if they are greater than zero, 11139 // test::messUp removes all new lines which changes the logic 11140 EXPECT_EQ("struct foo {\n" 11141 "private:\n" 11142 "\n\n\n" 11143 "protected:\n" 11144 "};\n", 11145 format("struct foo {\n" 11146 "private:\n" 11147 "\n\n\n" 11148 "protected:\n" 11149 "};\n", 11150 Style)); 11151 11152 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 11153 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11154 // test::messUp removes all new lines which changes the logic 11155 EXPECT_EQ("struct foo {\n" 11156 "private:\n" 11157 "\n\n\n" 11158 "protected:\n" 11159 "};\n", 11160 format("struct foo {\n" 11161 "private:\n" 11162 "\n\n\n" 11163 "protected:\n" 11164 "};\n", 11165 Style)); 11166 11167 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 11168 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 11169 EXPECT_EQ("struct foo {\n" 11170 "private:\n" 11171 "\n\n\n" 11172 "protected:\n" 11173 "};\n", 11174 format("struct foo {\n" 11175 "private:\n" 11176 "\n\n\n" 11177 "protected:\n" 11178 "};\n", 11179 Style)); // test::messUp removes all new lines which changes 11180 // the logic. 11181 11182 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 11183 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 11184 verifyFormat("struct foo {\n" 11185 "private:\n" 11186 "protected:\n" 11187 "};\n", 11188 "struct foo {\n" 11189 "private:\n" 11190 "\n\n\n" 11191 "protected:\n" 11192 "};\n", 11193 Style); 11194 11195 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 11196 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 11197 EXPECT_EQ("struct foo {\n" 11198 "private:\n" 11199 "\n\n\n" 11200 "protected:\n" 11201 "};\n", 11202 format("struct foo {\n" 11203 "private:\n" 11204 "\n\n\n" 11205 "protected:\n" 11206 "};\n", 11207 Style)); // test::messUp removes all new lines which changes 11208 // the logic. 11209 11210 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 11211 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11212 verifyFormat("struct foo {\n" 11213 "private:\n" 11214 "protected:\n" 11215 "};\n", 11216 "struct foo {\n" 11217 "private:\n" 11218 "\n\n\n" 11219 "protected:\n" 11220 "};\n", 11221 Style); 11222 11223 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 11224 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 11225 verifyFormat("struct foo {\n" 11226 "private:\n" 11227 "protected:\n" 11228 "};\n", 11229 "struct foo {\n" 11230 "private:\n" 11231 "\n\n\n" 11232 "protected:\n" 11233 "};\n", 11234 Style); 11235 11236 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 11237 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 11238 verifyFormat("struct foo {\n" 11239 "private:\n" 11240 "protected:\n" 11241 "};\n", 11242 "struct foo {\n" 11243 "private:\n" 11244 "\n\n\n" 11245 "protected:\n" 11246 "};\n", 11247 Style); 11248 11249 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 11250 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 11251 verifyFormat("struct foo {\n" 11252 "private:\n" 11253 "protected:\n" 11254 "};\n", 11255 "struct foo {\n" 11256 "private:\n" 11257 "\n\n\n" 11258 "protected:\n" 11259 "};\n", 11260 Style); 11261 } 11262 11263 TEST_F(FormatTest, FormatsArrays) { 11264 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 11265 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 11266 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 11267 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 11268 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 11269 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 11270 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11271 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 11272 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11273 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 11274 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 11275 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 11276 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 11277 verifyFormat( 11278 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 11279 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 11280 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 11281 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" 11282 " .aaaaaaaaaaaaaaaaaaaaaa();"); 11283 11284 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 11285 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 11286 verifyFormat( 11287 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 11288 " .aaaaaaa[0]\n" 11289 " .aaaaaaaaaaaaaaaaaaaaaa();"); 11290 verifyFormat("a[::b::c];"); 11291 11292 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 11293 11294 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 11295 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 11296 } 11297 11298 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 11299 verifyFormat("(a)->b();"); 11300 verifyFormat("--a;"); 11301 } 11302 11303 TEST_F(FormatTest, HandlesIncludeDirectives) { 11304 verifyFormat("#include <string>\n" 11305 "#include <a/b/c.h>\n" 11306 "#include \"a/b/string\"\n" 11307 "#include \"string.h\"\n" 11308 "#include \"string.h\"\n" 11309 "#include <a-a>\n" 11310 "#include < path with space >\n" 11311 "#include_next <test.h>" 11312 "#include \"abc.h\" // this is included for ABC\n" 11313 "#include \"some long include\" // with a comment\n" 11314 "#include \"some very long include path\"\n" 11315 "#include <some/very/long/include/path>\n", 11316 getLLVMStyleWithColumns(35)); 11317 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 11318 EXPECT_EQ("#include <a>", format("#include<a>")); 11319 11320 verifyFormat("#import <string>"); 11321 verifyFormat("#import <a/b/c.h>"); 11322 verifyFormat("#import \"a/b/string\""); 11323 verifyFormat("#import \"string.h\""); 11324 verifyFormat("#import \"string.h\""); 11325 verifyFormat("#if __has_include(<strstream>)\n" 11326 "#include <strstream>\n" 11327 "#endif"); 11328 11329 verifyFormat("#define MY_IMPORT <a/b>"); 11330 11331 verifyFormat("#if __has_include(<a/b>)"); 11332 verifyFormat("#if __has_include_next(<a/b>)"); 11333 verifyFormat("#define F __has_include(<a/b>)"); 11334 verifyFormat("#define F __has_include_next(<a/b>)"); 11335 11336 // Protocol buffer definition or missing "#". 11337 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 11338 getLLVMStyleWithColumns(30)); 11339 11340 FormatStyle Style = getLLVMStyle(); 11341 Style.AlwaysBreakBeforeMultilineStrings = true; 11342 Style.ColumnLimit = 0; 11343 verifyFormat("#import \"abc.h\"", Style); 11344 11345 // But 'import' might also be a regular C++ namespace. 11346 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11347 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 11348 } 11349 11350 //===----------------------------------------------------------------------===// 11351 // Error recovery tests. 11352 //===----------------------------------------------------------------------===// 11353 11354 TEST_F(FormatTest, IncompleteParameterLists) { 11355 FormatStyle NoBinPacking = getLLVMStyle(); 11356 NoBinPacking.BinPackParameters = false; 11357 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 11358 " double *min_x,\n" 11359 " double *max_x,\n" 11360 " double *min_y,\n" 11361 " double *max_y,\n" 11362 " double *min_z,\n" 11363 " double *max_z, ) {}", 11364 NoBinPacking); 11365 } 11366 11367 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 11368 verifyFormat("void f() { return; }\n42"); 11369 verifyFormat("void f() {\n" 11370 " if (0)\n" 11371 " return;\n" 11372 "}\n" 11373 "42"); 11374 verifyFormat("void f() { return }\n42"); 11375 verifyFormat("void f() {\n" 11376 " if (0)\n" 11377 " return\n" 11378 "}\n" 11379 "42"); 11380 } 11381 11382 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 11383 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 11384 EXPECT_EQ("void f() {\n" 11385 " if (a)\n" 11386 " return\n" 11387 "}", 11388 format("void f ( ) { if ( a ) return }")); 11389 EXPECT_EQ("namespace N {\n" 11390 "void f()\n" 11391 "}", 11392 format("namespace N { void f() }")); 11393 EXPECT_EQ("namespace N {\n" 11394 "void f() {}\n" 11395 "void g()\n" 11396 "} // namespace N", 11397 format("namespace N { void f( ) { } void g( ) }")); 11398 } 11399 11400 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 11401 verifyFormat("int aaaaaaaa =\n" 11402 " // Overlylongcomment\n" 11403 " b;", 11404 getLLVMStyleWithColumns(20)); 11405 verifyFormat("function(\n" 11406 " ShortArgument,\n" 11407 " LoooooooooooongArgument);\n", 11408 getLLVMStyleWithColumns(20)); 11409 } 11410 11411 TEST_F(FormatTest, IncorrectAccessSpecifier) { 11412 verifyFormat("public:"); 11413 verifyFormat("class A {\n" 11414 "public\n" 11415 " void f() {}\n" 11416 "};"); 11417 verifyFormat("public\n" 11418 "int qwerty;"); 11419 verifyFormat("public\n" 11420 "B {}"); 11421 verifyFormat("public\n" 11422 "{}"); 11423 verifyFormat("public\n" 11424 "B { int x; }"); 11425 } 11426 11427 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 11428 verifyFormat("{"); 11429 verifyFormat("#})"); 11430 verifyNoCrash("(/**/[:!] ?[)."); 11431 } 11432 11433 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) { 11434 // Found by oss-fuzz: 11435 // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212 11436 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 11437 Style.ColumnLimit = 60; 11438 verifyNoCrash( 11439 "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20" 11440 "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20" 11441 "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a", 11442 Style); 11443 } 11444 11445 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 11446 verifyFormat("do {\n}"); 11447 verifyFormat("do {\n}\n" 11448 "f();"); 11449 verifyFormat("do {\n}\n" 11450 "wheeee(fun);"); 11451 verifyFormat("do {\n" 11452 " f();\n" 11453 "}"); 11454 } 11455 11456 TEST_F(FormatTest, IncorrectCodeMissingParens) { 11457 verifyFormat("if {\n foo;\n foo();\n}"); 11458 verifyFormat("switch {\n foo;\n foo();\n}"); 11459 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 11460 verifyFormat("while {\n foo;\n foo();\n}"); 11461 verifyFormat("do {\n foo;\n foo();\n} while;"); 11462 } 11463 11464 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 11465 verifyIncompleteFormat("namespace {\n" 11466 "class Foo { Foo (\n" 11467 "};\n" 11468 "} // namespace"); 11469 } 11470 11471 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 11472 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 11473 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 11474 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 11475 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 11476 11477 EXPECT_EQ("{\n" 11478 " {\n" 11479 " breakme(\n" 11480 " qwe);\n" 11481 " }\n", 11482 format("{\n" 11483 " {\n" 11484 " breakme(qwe);\n" 11485 "}\n", 11486 getLLVMStyleWithColumns(10))); 11487 } 11488 11489 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 11490 verifyFormat("int x = {\n" 11491 " avariable,\n" 11492 " b(alongervariable)};", 11493 getLLVMStyleWithColumns(25)); 11494 } 11495 11496 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 11497 verifyFormat("return (a)(b){1, 2, 3};"); 11498 } 11499 11500 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 11501 verifyFormat("vector<int> x{1, 2, 3, 4};"); 11502 verifyFormat("vector<int> x{\n" 11503 " 1,\n" 11504 " 2,\n" 11505 " 3,\n" 11506 " 4,\n" 11507 "};"); 11508 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 11509 verifyFormat("f({1, 2});"); 11510 verifyFormat("auto v = Foo{-1};"); 11511 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 11512 verifyFormat("Class::Class : member{1, 2, 3} {}"); 11513 verifyFormat("new vector<int>{1, 2, 3};"); 11514 verifyFormat("new int[3]{1, 2, 3};"); 11515 verifyFormat("new int{1};"); 11516 verifyFormat("return {arg1, arg2};"); 11517 verifyFormat("return {arg1, SomeType{parameter}};"); 11518 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 11519 verifyFormat("new T{arg1, arg2};"); 11520 verifyFormat("f(MyMap[{composite, key}]);"); 11521 verifyFormat("class Class {\n" 11522 " T member = {arg1, arg2};\n" 11523 "};"); 11524 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 11525 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 11526 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 11527 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 11528 verifyFormat("int a = std::is_integral<int>{} + 0;"); 11529 11530 verifyFormat("int foo(int i) { return fo1{}(i); }"); 11531 verifyFormat("int foo(int i) { return fo1{}(i); }"); 11532 verifyFormat("auto i = decltype(x){};"); 11533 verifyFormat("auto i = typeof(x){};"); 11534 verifyFormat("auto i = _Atomic(x){};"); 11535 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 11536 verifyFormat("Node n{1, Node{1000}, //\n" 11537 " 2};"); 11538 verifyFormat("Aaaa aaaaaaa{\n" 11539 " {\n" 11540 " aaaa,\n" 11541 " },\n" 11542 "};"); 11543 verifyFormat("class C : public D {\n" 11544 " SomeClass SC{2};\n" 11545 "};"); 11546 verifyFormat("class C : public A {\n" 11547 " class D : public B {\n" 11548 " void f() { int i{2}; }\n" 11549 " };\n" 11550 "};"); 11551 verifyFormat("#define A {a, a},"); 11552 // Don't confuse braced list initializers with compound statements. 11553 verifyFormat( 11554 "class A {\n" 11555 " A() : a{} {}\n" 11556 " A(int b) : b(b) {}\n" 11557 " A(int a, int b) : a(a), bs{{bs...}} { f(); }\n" 11558 " int a, b;\n" 11559 " explicit Expr(const Scalar<Result> &x) : u{Constant<Result>{x}} {}\n" 11560 " explicit Expr(Scalar<Result> &&x) : u{Constant<Result>{std::move(x)}} " 11561 "{}\n" 11562 "};"); 11563 11564 // Avoid breaking between equal sign and opening brace 11565 FormatStyle AvoidBreakingFirstArgument = getLLVMStyle(); 11566 AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200; 11567 verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n" 11568 " {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n" 11569 " {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n" 11570 " {\"ccccccccccccccccccccc\", 2}};", 11571 AvoidBreakingFirstArgument); 11572 11573 // Binpacking only if there is no trailing comma 11574 verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n" 11575 " cccccccccc, dddddddddd};", 11576 getLLVMStyleWithColumns(50)); 11577 verifyFormat("const Aaaaaa aaaaa = {\n" 11578 " aaaaaaaaaaa,\n" 11579 " bbbbbbbbbbb,\n" 11580 " ccccccccccc,\n" 11581 " ddddddddddd,\n" 11582 "};", 11583 getLLVMStyleWithColumns(50)); 11584 11585 // Cases where distinguising braced lists and blocks is hard. 11586 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);"); 11587 verifyFormat("void f() {\n" 11588 " return; // comment\n" 11589 "}\n" 11590 "SomeType t;"); 11591 verifyFormat("void f() {\n" 11592 " if (a) {\n" 11593 " f();\n" 11594 " }\n" 11595 "}\n" 11596 "SomeType t;"); 11597 11598 // In combination with BinPackArguments = false. 11599 FormatStyle NoBinPacking = getLLVMStyle(); 11600 NoBinPacking.BinPackArguments = false; 11601 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 11602 " bbbbb,\n" 11603 " ccccc,\n" 11604 " ddddd,\n" 11605 " eeeee,\n" 11606 " ffffff,\n" 11607 " ggggg,\n" 11608 " hhhhhh,\n" 11609 " iiiiii,\n" 11610 " jjjjjj,\n" 11611 " kkkkkk};", 11612 NoBinPacking); 11613 verifyFormat("const Aaaaaa aaaaa = {\n" 11614 " aaaaa,\n" 11615 " bbbbb,\n" 11616 " ccccc,\n" 11617 " ddddd,\n" 11618 " eeeee,\n" 11619 " ffffff,\n" 11620 " ggggg,\n" 11621 " hhhhhh,\n" 11622 " iiiiii,\n" 11623 " jjjjjj,\n" 11624 " kkkkkk,\n" 11625 "};", 11626 NoBinPacking); 11627 verifyFormat( 11628 "const Aaaaaa aaaaa = {\n" 11629 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 11630 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 11631 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 11632 "};", 11633 NoBinPacking); 11634 11635 NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 11636 EXPECT_EQ("static uint8 CddDp83848Reg[] = {\n" 11637 " CDDDP83848_BMCR_REGISTER,\n" 11638 " CDDDP83848_BMSR_REGISTER,\n" 11639 " CDDDP83848_RBR_REGISTER};", 11640 format("static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n" 11641 " CDDDP83848_BMSR_REGISTER,\n" 11642 " CDDDP83848_RBR_REGISTER};", 11643 NoBinPacking)); 11644 11645 // FIXME: The alignment of these trailing comments might be bad. Then again, 11646 // this might be utterly useless in real code. 11647 verifyFormat("Constructor::Constructor()\n" 11648 " : some_value{ //\n" 11649 " aaaaaaa, //\n" 11650 " bbbbbbb} {}"); 11651 11652 // In braced lists, the first comment is always assumed to belong to the 11653 // first element. Thus, it can be moved to the next or previous line as 11654 // appropriate. 11655 EXPECT_EQ("function({// First element:\n" 11656 " 1,\n" 11657 " // Second element:\n" 11658 " 2});", 11659 format("function({\n" 11660 " // First element:\n" 11661 " 1,\n" 11662 " // Second element:\n" 11663 " 2});")); 11664 EXPECT_EQ("std::vector<int> MyNumbers{\n" 11665 " // First element:\n" 11666 " 1,\n" 11667 " // Second element:\n" 11668 " 2};", 11669 format("std::vector<int> MyNumbers{// First element:\n" 11670 " 1,\n" 11671 " // Second element:\n" 11672 " 2};", 11673 getLLVMStyleWithColumns(30))); 11674 // A trailing comma should still lead to an enforced line break and no 11675 // binpacking. 11676 EXPECT_EQ("vector<int> SomeVector = {\n" 11677 " // aaa\n" 11678 " 1,\n" 11679 " 2,\n" 11680 "};", 11681 format("vector<int> SomeVector = { // aaa\n" 11682 " 1, 2, };")); 11683 11684 // C++11 brace initializer list l-braces should not be treated any differently 11685 // when breaking before lambda bodies is enabled 11686 FormatStyle BreakBeforeLambdaBody = getLLVMStyle(); 11687 BreakBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 11688 BreakBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 11689 BreakBeforeLambdaBody.AlwaysBreakBeforeMultilineStrings = true; 11690 verifyFormat( 11691 "std::runtime_error{\n" 11692 " \"Long string which will force a break onto the next line...\"};", 11693 BreakBeforeLambdaBody); 11694 11695 FormatStyle ExtraSpaces = getLLVMStyle(); 11696 ExtraSpaces.Cpp11BracedListStyle = false; 11697 ExtraSpaces.ColumnLimit = 75; 11698 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 11699 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 11700 verifyFormat("f({ 1, 2 });", ExtraSpaces); 11701 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 11702 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 11703 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 11704 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 11705 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 11706 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 11707 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 11708 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 11709 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 11710 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 11711 verifyFormat("class Class {\n" 11712 " T member = { arg1, arg2 };\n" 11713 "};", 11714 ExtraSpaces); 11715 verifyFormat( 11716 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11717 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 11718 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 11719 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 11720 ExtraSpaces); 11721 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 11722 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 11723 ExtraSpaces); 11724 verifyFormat( 11725 "someFunction(OtherParam,\n" 11726 " BracedList{ // comment 1 (Forcing interesting break)\n" 11727 " param1, param2,\n" 11728 " // comment 2\n" 11729 " param3, param4 });", 11730 ExtraSpaces); 11731 verifyFormat( 11732 "std::this_thread::sleep_for(\n" 11733 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 11734 ExtraSpaces); 11735 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n" 11736 " aaaaaaa,\n" 11737 " aaaaaaaaaa,\n" 11738 " aaaaa,\n" 11739 " aaaaaaaaaaaaaaa,\n" 11740 " aaa,\n" 11741 " aaaaaaaaaa,\n" 11742 " a,\n" 11743 " aaaaaaaaaaaaaaaaaaaaa,\n" 11744 " aaaaaaaaaaaa,\n" 11745 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 11746 " aaaaaaa,\n" 11747 " a};"); 11748 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 11749 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces); 11750 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces); 11751 11752 // Avoid breaking between initializer/equal sign and opening brace 11753 ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200; 11754 verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n" 11755 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 11756 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 11757 " { \"ccccccccccccccccccccc\", 2 }\n" 11758 "};", 11759 ExtraSpaces); 11760 verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n" 11761 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 11762 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 11763 " { \"ccccccccccccccccccccc\", 2 }\n" 11764 "};", 11765 ExtraSpaces); 11766 11767 FormatStyle SpaceBeforeBrace = getLLVMStyle(); 11768 SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true; 11769 verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace); 11770 verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace); 11771 11772 FormatStyle SpaceBetweenBraces = getLLVMStyle(); 11773 SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always; 11774 SpaceBetweenBraces.SpacesInParentheses = true; 11775 SpaceBetweenBraces.SpacesInSquareBrackets = true; 11776 verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces); 11777 verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces); 11778 verifyFormat("vector< int > x{ // comment 1\n" 11779 " 1, 2, 3, 4 };", 11780 SpaceBetweenBraces); 11781 SpaceBetweenBraces.ColumnLimit = 20; 11782 EXPECT_EQ("vector< int > x{\n" 11783 " 1, 2, 3, 4 };", 11784 format("vector<int>x{1,2,3,4};", SpaceBetweenBraces)); 11785 SpaceBetweenBraces.ColumnLimit = 24; 11786 EXPECT_EQ("vector< int > x{ 1, 2,\n" 11787 " 3, 4 };", 11788 format("vector<int>x{1,2,3,4};", SpaceBetweenBraces)); 11789 EXPECT_EQ("vector< int > x{\n" 11790 " 1,\n" 11791 " 2,\n" 11792 " 3,\n" 11793 " 4,\n" 11794 "};", 11795 format("vector<int>x{1,2,3,4,};", SpaceBetweenBraces)); 11796 verifyFormat("vector< int > x{};", SpaceBetweenBraces); 11797 SpaceBetweenBraces.SpaceInEmptyParentheses = true; 11798 verifyFormat("vector< int > x{ };", SpaceBetweenBraces); 11799 } 11800 11801 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 11802 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11803 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11804 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11805 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11806 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11807 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 11808 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 11809 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11810 " 1, 22, 333, 4444, 55555, //\n" 11811 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11812 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 11813 verifyFormat( 11814 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11815 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11816 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 11817 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 11818 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 11819 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 11820 " 7777777};"); 11821 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 11822 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 11823 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 11824 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 11825 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 11826 " // Separating comment.\n" 11827 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 11828 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 11829 " // Leading comment\n" 11830 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 11831 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 11832 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 11833 " 1, 1, 1, 1};", 11834 getLLVMStyleWithColumns(39)); 11835 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 11836 " 1, 1, 1, 1};", 11837 getLLVMStyleWithColumns(38)); 11838 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 11839 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 11840 getLLVMStyleWithColumns(43)); 11841 verifyFormat( 11842 "static unsigned SomeValues[10][3] = {\n" 11843 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 11844 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 11845 verifyFormat("static auto fields = new vector<string>{\n" 11846 " \"aaaaaaaaaaaaa\",\n" 11847 " \"aaaaaaaaaaaaa\",\n" 11848 " \"aaaaaaaaaaaa\",\n" 11849 " \"aaaaaaaaaaaaaa\",\n" 11850 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 11851 " \"aaaaaaaaaaaa\",\n" 11852 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 11853 "};"); 11854 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 11855 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 11856 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 11857 " 3, cccccccccccccccccccccc};", 11858 getLLVMStyleWithColumns(60)); 11859 11860 // Trailing commas. 11861 verifyFormat("vector<int> x = {\n" 11862 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 11863 "};", 11864 getLLVMStyleWithColumns(39)); 11865 verifyFormat("vector<int> x = {\n" 11866 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 11867 "};", 11868 getLLVMStyleWithColumns(39)); 11869 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 11870 " 1, 1, 1, 1,\n" 11871 " /**/ /**/};", 11872 getLLVMStyleWithColumns(39)); 11873 11874 // Trailing comment in the first line. 11875 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 11876 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 11877 " 111111111, 222222222, 3333333333, 444444444, //\n" 11878 " 11111111, 22222222, 333333333, 44444444};"); 11879 // Trailing comment in the last line. 11880 verifyFormat("int aaaaa[] = {\n" 11881 " 1, 2, 3, // comment\n" 11882 " 4, 5, 6 // comment\n" 11883 "};"); 11884 11885 // With nested lists, we should either format one item per line or all nested 11886 // lists one on line. 11887 // FIXME: For some nested lists, we can do better. 11888 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 11889 " {aaaaaaaaaaaaaaaaaaa},\n" 11890 " {aaaaaaaaaaaaaaaaaaaaa},\n" 11891 " {aaaaaaaaaaaaaaaaa}};", 11892 getLLVMStyleWithColumns(60)); 11893 verifyFormat( 11894 "SomeStruct my_struct_array = {\n" 11895 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 11896 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 11897 " {aaa, aaa},\n" 11898 " {aaa, aaa},\n" 11899 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 11900 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 11901 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 11902 11903 // No column layout should be used here. 11904 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 11905 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 11906 11907 verifyNoCrash("a<,"); 11908 11909 // No braced initializer here. 11910 verifyFormat("void f() {\n" 11911 " struct Dummy {};\n" 11912 " f(v);\n" 11913 "}"); 11914 11915 // Long lists should be formatted in columns even if they are nested. 11916 verifyFormat( 11917 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11918 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11919 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11920 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11921 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 11922 " 1, 22, 333, 4444, 55555, 666666, 7777777});"); 11923 11924 // Allow "single-column" layout even if that violates the column limit. There 11925 // isn't going to be a better way. 11926 verifyFormat("std::vector<int> a = {\n" 11927 " aaaaaaaa,\n" 11928 " aaaaaaaa,\n" 11929 " aaaaaaaa,\n" 11930 " aaaaaaaa,\n" 11931 " aaaaaaaaaa,\n" 11932 " aaaaaaaa,\n" 11933 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};", 11934 getLLVMStyleWithColumns(30)); 11935 verifyFormat("vector<int> aaaa = {\n" 11936 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11937 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 11938 " aaaaaa.aaaaaaa,\n" 11939 " aaaaaa.aaaaaaa,\n" 11940 " aaaaaa.aaaaaaa,\n" 11941 " aaaaaa.aaaaaaa,\n" 11942 "};"); 11943 11944 // Don't create hanging lists. 11945 verifyFormat("someFunction(Param, {List1, List2,\n" 11946 " List3});", 11947 getLLVMStyleWithColumns(35)); 11948 verifyFormat("someFunction(Param, Param,\n" 11949 " {List1, List2,\n" 11950 " List3});", 11951 getLLVMStyleWithColumns(35)); 11952 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n" 11953 " aaaaaaaaaaaaaaaaaaaaaaa);"); 11954 } 11955 11956 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 11957 FormatStyle DoNotMerge = getLLVMStyle(); 11958 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 11959 11960 verifyFormat("void f() { return 42; }"); 11961 verifyFormat("void f() {\n" 11962 " return 42;\n" 11963 "}", 11964 DoNotMerge); 11965 verifyFormat("void f() {\n" 11966 " // Comment\n" 11967 "}"); 11968 verifyFormat("{\n" 11969 "#error {\n" 11970 " int a;\n" 11971 "}"); 11972 verifyFormat("{\n" 11973 " int a;\n" 11974 "#error {\n" 11975 "}"); 11976 verifyFormat("void f() {} // comment"); 11977 verifyFormat("void f() { int a; } // comment"); 11978 verifyFormat("void f() {\n" 11979 "} // comment", 11980 DoNotMerge); 11981 verifyFormat("void f() {\n" 11982 " int a;\n" 11983 "} // comment", 11984 DoNotMerge); 11985 verifyFormat("void f() {\n" 11986 "} // comment", 11987 getLLVMStyleWithColumns(15)); 11988 11989 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 11990 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 11991 11992 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 11993 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 11994 verifyFormat("class C {\n" 11995 " C()\n" 11996 " : iiiiiiii(nullptr),\n" 11997 " kkkkkkk(nullptr),\n" 11998 " mmmmmmm(nullptr),\n" 11999 " nnnnnnn(nullptr) {}\n" 12000 "};", 12001 getGoogleStyle()); 12002 12003 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 12004 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 12005 EXPECT_EQ("class C {\n" 12006 " A() : b(0) {}\n" 12007 "};", 12008 format("class C{A():b(0){}};", NoColumnLimit)); 12009 EXPECT_EQ("A()\n" 12010 " : b(0) {\n" 12011 "}", 12012 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 12013 12014 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 12015 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 12016 FormatStyle::SFS_None; 12017 EXPECT_EQ("A()\n" 12018 " : b(0) {\n" 12019 "}", 12020 format("A():b(0){}", DoNotMergeNoColumnLimit)); 12021 EXPECT_EQ("A()\n" 12022 " : b(0) {\n" 12023 "}", 12024 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 12025 12026 verifyFormat("#define A \\\n" 12027 " void f() { \\\n" 12028 " int i; \\\n" 12029 " }", 12030 getLLVMStyleWithColumns(20)); 12031 verifyFormat("#define A \\\n" 12032 " void f() { int i; }", 12033 getLLVMStyleWithColumns(21)); 12034 verifyFormat("#define A \\\n" 12035 " void f() { \\\n" 12036 " int i; \\\n" 12037 " } \\\n" 12038 " int j;", 12039 getLLVMStyleWithColumns(22)); 12040 verifyFormat("#define A \\\n" 12041 " void f() { int i; } \\\n" 12042 " int j;", 12043 getLLVMStyleWithColumns(23)); 12044 } 12045 12046 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) { 12047 FormatStyle MergeEmptyOnly = getLLVMStyle(); 12048 MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 12049 verifyFormat("class C {\n" 12050 " int f() {}\n" 12051 "};", 12052 MergeEmptyOnly); 12053 verifyFormat("class C {\n" 12054 " int f() {\n" 12055 " return 42;\n" 12056 " }\n" 12057 "};", 12058 MergeEmptyOnly); 12059 verifyFormat("int f() {}", MergeEmptyOnly); 12060 verifyFormat("int f() {\n" 12061 " return 42;\n" 12062 "}", 12063 MergeEmptyOnly); 12064 12065 // Also verify behavior when BraceWrapping.AfterFunction = true 12066 MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 12067 MergeEmptyOnly.BraceWrapping.AfterFunction = true; 12068 verifyFormat("int f() {}", MergeEmptyOnly); 12069 verifyFormat("class C {\n" 12070 " int f() {}\n" 12071 "};", 12072 MergeEmptyOnly); 12073 } 12074 12075 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 12076 FormatStyle MergeInlineOnly = getLLVMStyle(); 12077 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 12078 verifyFormat("class C {\n" 12079 " int f() { return 42; }\n" 12080 "};", 12081 MergeInlineOnly); 12082 verifyFormat("int f() {\n" 12083 " return 42;\n" 12084 "}", 12085 MergeInlineOnly); 12086 12087 // SFS_Inline implies SFS_Empty 12088 verifyFormat("class C {\n" 12089 " int f() {}\n" 12090 "};", 12091 MergeInlineOnly); 12092 verifyFormat("int f() {}", MergeInlineOnly); 12093 12094 // Also verify behavior when BraceWrapping.AfterFunction = true 12095 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 12096 MergeInlineOnly.BraceWrapping.AfterFunction = true; 12097 verifyFormat("class C {\n" 12098 " int f() { return 42; }\n" 12099 "};", 12100 MergeInlineOnly); 12101 verifyFormat("int f()\n" 12102 "{\n" 12103 " return 42;\n" 12104 "}", 12105 MergeInlineOnly); 12106 12107 // SFS_Inline implies SFS_Empty 12108 verifyFormat("int f() {}", MergeInlineOnly); 12109 verifyFormat("class C {\n" 12110 " int f() {}\n" 12111 "};", 12112 MergeInlineOnly); 12113 } 12114 12115 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) { 12116 FormatStyle MergeInlineOnly = getLLVMStyle(); 12117 MergeInlineOnly.AllowShortFunctionsOnASingleLine = 12118 FormatStyle::SFS_InlineOnly; 12119 verifyFormat("class C {\n" 12120 " int f() { return 42; }\n" 12121 "};", 12122 MergeInlineOnly); 12123 verifyFormat("int f() {\n" 12124 " return 42;\n" 12125 "}", 12126 MergeInlineOnly); 12127 12128 // SFS_InlineOnly does not imply SFS_Empty 12129 verifyFormat("class C {\n" 12130 " int f() {}\n" 12131 "};", 12132 MergeInlineOnly); 12133 verifyFormat("int f() {\n" 12134 "}", 12135 MergeInlineOnly); 12136 12137 // Also verify behavior when BraceWrapping.AfterFunction = true 12138 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 12139 MergeInlineOnly.BraceWrapping.AfterFunction = true; 12140 verifyFormat("class C {\n" 12141 " int f() { return 42; }\n" 12142 "};", 12143 MergeInlineOnly); 12144 verifyFormat("int f()\n" 12145 "{\n" 12146 " return 42;\n" 12147 "}", 12148 MergeInlineOnly); 12149 12150 // SFS_InlineOnly does not imply SFS_Empty 12151 verifyFormat("int f()\n" 12152 "{\n" 12153 "}", 12154 MergeInlineOnly); 12155 verifyFormat("class C {\n" 12156 " int f() {}\n" 12157 "};", 12158 MergeInlineOnly); 12159 } 12160 12161 TEST_F(FormatTest, SplitEmptyFunction) { 12162 FormatStyle Style = getLLVMStyleWithColumns(40); 12163 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 12164 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12165 Style.BraceWrapping.AfterFunction = true; 12166 Style.BraceWrapping.SplitEmptyFunction = false; 12167 12168 verifyFormat("int f()\n" 12169 "{}", 12170 Style); 12171 verifyFormat("int f()\n" 12172 "{\n" 12173 " return 42;\n" 12174 "}", 12175 Style); 12176 verifyFormat("int f()\n" 12177 "{\n" 12178 " // some comment\n" 12179 "}", 12180 Style); 12181 12182 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 12183 verifyFormat("int f() {}", Style); 12184 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12185 "{}", 12186 Style); 12187 verifyFormat("int f()\n" 12188 "{\n" 12189 " return 0;\n" 12190 "}", 12191 Style); 12192 12193 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 12194 verifyFormat("class Foo {\n" 12195 " int f() {}\n" 12196 "};\n", 12197 Style); 12198 verifyFormat("class Foo {\n" 12199 " int f() { return 0; }\n" 12200 "};\n", 12201 Style); 12202 verifyFormat("class Foo {\n" 12203 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12204 " {}\n" 12205 "};\n", 12206 Style); 12207 verifyFormat("class Foo {\n" 12208 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12209 " {\n" 12210 " return 0;\n" 12211 " }\n" 12212 "};\n", 12213 Style); 12214 12215 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 12216 verifyFormat("int f() {}", Style); 12217 verifyFormat("int f() { return 0; }", Style); 12218 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12219 "{}", 12220 Style); 12221 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 12222 "{\n" 12223 " return 0;\n" 12224 "}", 12225 Style); 12226 } 12227 12228 TEST_F(FormatTest, SplitEmptyFunctionButNotRecord) { 12229 FormatStyle Style = getLLVMStyleWithColumns(40); 12230 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 12231 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12232 Style.BraceWrapping.AfterFunction = true; 12233 Style.BraceWrapping.SplitEmptyFunction = true; 12234 Style.BraceWrapping.SplitEmptyRecord = false; 12235 12236 verifyFormat("class C {};", Style); 12237 verifyFormat("struct C {};", Style); 12238 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 12239 " int bbbbbbbbbbbbbbbbbbbbbbbb)\n" 12240 "{\n" 12241 "}", 12242 Style); 12243 verifyFormat("class C {\n" 12244 " C()\n" 12245 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa(),\n" 12246 " bbbbbbbbbbbbbbbbbbb()\n" 12247 " {\n" 12248 " }\n" 12249 " void\n" 12250 " m(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 12251 " int bbbbbbbbbbbbbbbbbbbbbbbb)\n" 12252 " {\n" 12253 " }\n" 12254 "};", 12255 Style); 12256 } 12257 12258 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) { 12259 FormatStyle Style = getLLVMStyle(); 12260 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 12261 verifyFormat("#ifdef A\n" 12262 "int f() {}\n" 12263 "#else\n" 12264 "int g() {}\n" 12265 "#endif", 12266 Style); 12267 } 12268 12269 TEST_F(FormatTest, SplitEmptyClass) { 12270 FormatStyle Style = getLLVMStyle(); 12271 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12272 Style.BraceWrapping.AfterClass = true; 12273 Style.BraceWrapping.SplitEmptyRecord = false; 12274 12275 verifyFormat("class Foo\n" 12276 "{};", 12277 Style); 12278 verifyFormat("/* something */ class Foo\n" 12279 "{};", 12280 Style); 12281 verifyFormat("template <typename X> class Foo\n" 12282 "{};", 12283 Style); 12284 verifyFormat("class Foo\n" 12285 "{\n" 12286 " Foo();\n" 12287 "};", 12288 Style); 12289 verifyFormat("typedef class Foo\n" 12290 "{\n" 12291 "} Foo_t;", 12292 Style); 12293 12294 Style.BraceWrapping.SplitEmptyRecord = true; 12295 Style.BraceWrapping.AfterStruct = true; 12296 verifyFormat("class rep\n" 12297 "{\n" 12298 "};", 12299 Style); 12300 verifyFormat("struct rep\n" 12301 "{\n" 12302 "};", 12303 Style); 12304 verifyFormat("template <typename T> class rep\n" 12305 "{\n" 12306 "};", 12307 Style); 12308 verifyFormat("template <typename T> struct rep\n" 12309 "{\n" 12310 "};", 12311 Style); 12312 verifyFormat("class rep\n" 12313 "{\n" 12314 " int x;\n" 12315 "};", 12316 Style); 12317 verifyFormat("struct rep\n" 12318 "{\n" 12319 " int x;\n" 12320 "};", 12321 Style); 12322 verifyFormat("template <typename T> class rep\n" 12323 "{\n" 12324 " int x;\n" 12325 "};", 12326 Style); 12327 verifyFormat("template <typename T> struct rep\n" 12328 "{\n" 12329 " int x;\n" 12330 "};", 12331 Style); 12332 verifyFormat("template <typename T> class rep // Foo\n" 12333 "{\n" 12334 " int x;\n" 12335 "};", 12336 Style); 12337 verifyFormat("template <typename T> struct rep // Bar\n" 12338 "{\n" 12339 " int x;\n" 12340 "};", 12341 Style); 12342 12343 verifyFormat("template <typename T> class rep<T>\n" 12344 "{\n" 12345 " int x;\n" 12346 "};", 12347 Style); 12348 12349 verifyFormat("template <typename T> class rep<std::complex<T>>\n" 12350 "{\n" 12351 " int x;\n" 12352 "};", 12353 Style); 12354 verifyFormat("template <typename T> class rep<std::complex<T>>\n" 12355 "{\n" 12356 "};", 12357 Style); 12358 12359 verifyFormat("#include \"stdint.h\"\n" 12360 "namespace rep {}", 12361 Style); 12362 verifyFormat("#include <stdint.h>\n" 12363 "namespace rep {}", 12364 Style); 12365 verifyFormat("#include <stdint.h>\n" 12366 "namespace rep {}", 12367 "#include <stdint.h>\n" 12368 "namespace rep {\n" 12369 "\n" 12370 "\n" 12371 "}", 12372 Style); 12373 } 12374 12375 TEST_F(FormatTest, SplitEmptyStruct) { 12376 FormatStyle Style = getLLVMStyle(); 12377 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12378 Style.BraceWrapping.AfterStruct = true; 12379 Style.BraceWrapping.SplitEmptyRecord = false; 12380 12381 verifyFormat("struct Foo\n" 12382 "{};", 12383 Style); 12384 verifyFormat("/* something */ struct Foo\n" 12385 "{};", 12386 Style); 12387 verifyFormat("template <typename X> struct Foo\n" 12388 "{};", 12389 Style); 12390 verifyFormat("struct Foo\n" 12391 "{\n" 12392 " Foo();\n" 12393 "};", 12394 Style); 12395 verifyFormat("typedef struct Foo\n" 12396 "{\n" 12397 "} Foo_t;", 12398 Style); 12399 // typedef struct Bar {} Bar_t; 12400 } 12401 12402 TEST_F(FormatTest, SplitEmptyUnion) { 12403 FormatStyle Style = getLLVMStyle(); 12404 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12405 Style.BraceWrapping.AfterUnion = true; 12406 Style.BraceWrapping.SplitEmptyRecord = false; 12407 12408 verifyFormat("union Foo\n" 12409 "{};", 12410 Style); 12411 verifyFormat("/* something */ union Foo\n" 12412 "{};", 12413 Style); 12414 verifyFormat("union Foo\n" 12415 "{\n" 12416 " A,\n" 12417 "};", 12418 Style); 12419 verifyFormat("typedef union Foo\n" 12420 "{\n" 12421 "} Foo_t;", 12422 Style); 12423 } 12424 12425 TEST_F(FormatTest, SplitEmptyNamespace) { 12426 FormatStyle Style = getLLVMStyle(); 12427 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12428 Style.BraceWrapping.AfterNamespace = true; 12429 Style.BraceWrapping.SplitEmptyNamespace = false; 12430 12431 verifyFormat("namespace Foo\n" 12432 "{};", 12433 Style); 12434 verifyFormat("/* something */ namespace Foo\n" 12435 "{};", 12436 Style); 12437 verifyFormat("inline namespace Foo\n" 12438 "{};", 12439 Style); 12440 verifyFormat("/* something */ inline namespace Foo\n" 12441 "{};", 12442 Style); 12443 verifyFormat("export namespace Foo\n" 12444 "{};", 12445 Style); 12446 verifyFormat("namespace Foo\n" 12447 "{\n" 12448 "void Bar();\n" 12449 "};", 12450 Style); 12451 } 12452 12453 TEST_F(FormatTest, NeverMergeShortRecords) { 12454 FormatStyle Style = getLLVMStyle(); 12455 12456 verifyFormat("class Foo {\n" 12457 " Foo();\n" 12458 "};", 12459 Style); 12460 verifyFormat("typedef class Foo {\n" 12461 " Foo();\n" 12462 "} Foo_t;", 12463 Style); 12464 verifyFormat("struct Foo {\n" 12465 " Foo();\n" 12466 "};", 12467 Style); 12468 verifyFormat("typedef struct Foo {\n" 12469 " Foo();\n" 12470 "} Foo_t;", 12471 Style); 12472 verifyFormat("union Foo {\n" 12473 " A,\n" 12474 "};", 12475 Style); 12476 verifyFormat("typedef union Foo {\n" 12477 " A,\n" 12478 "} Foo_t;", 12479 Style); 12480 verifyFormat("namespace Foo {\n" 12481 "void Bar();\n" 12482 "};", 12483 Style); 12484 12485 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 12486 Style.BraceWrapping.AfterClass = true; 12487 Style.BraceWrapping.AfterStruct = true; 12488 Style.BraceWrapping.AfterUnion = true; 12489 Style.BraceWrapping.AfterNamespace = true; 12490 verifyFormat("class Foo\n" 12491 "{\n" 12492 " Foo();\n" 12493 "};", 12494 Style); 12495 verifyFormat("typedef class Foo\n" 12496 "{\n" 12497 " Foo();\n" 12498 "} Foo_t;", 12499 Style); 12500 verifyFormat("struct Foo\n" 12501 "{\n" 12502 " Foo();\n" 12503 "};", 12504 Style); 12505 verifyFormat("typedef struct Foo\n" 12506 "{\n" 12507 " Foo();\n" 12508 "} Foo_t;", 12509 Style); 12510 verifyFormat("union Foo\n" 12511 "{\n" 12512 " A,\n" 12513 "};", 12514 Style); 12515 verifyFormat("typedef union Foo\n" 12516 "{\n" 12517 " A,\n" 12518 "} Foo_t;", 12519 Style); 12520 verifyFormat("namespace Foo\n" 12521 "{\n" 12522 "void Bar();\n" 12523 "};", 12524 Style); 12525 } 12526 12527 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 12528 // Elaborate type variable declarations. 12529 verifyFormat("struct foo a = {bar};\nint n;"); 12530 verifyFormat("class foo a = {bar};\nint n;"); 12531 verifyFormat("union foo a = {bar};\nint n;"); 12532 12533 // Elaborate types inside function definitions. 12534 verifyFormat("struct foo f() {}\nint n;"); 12535 verifyFormat("class foo f() {}\nint n;"); 12536 verifyFormat("union foo f() {}\nint n;"); 12537 12538 // Templates. 12539 verifyFormat("template <class X> void f() {}\nint n;"); 12540 verifyFormat("template <struct X> void f() {}\nint n;"); 12541 verifyFormat("template <union X> void f() {}\nint n;"); 12542 12543 // Actual definitions... 12544 verifyFormat("struct {\n} n;"); 12545 verifyFormat( 12546 "template <template <class T, class Y>, class Z> class X {\n} n;"); 12547 verifyFormat("union Z {\n int n;\n} x;"); 12548 verifyFormat("class MACRO Z {\n} n;"); 12549 verifyFormat("class MACRO(X) Z {\n} n;"); 12550 verifyFormat("class __attribute__(X) Z {\n} n;"); 12551 verifyFormat("class __declspec(X) Z {\n} n;"); 12552 verifyFormat("class A##B##C {\n} n;"); 12553 verifyFormat("class alignas(16) Z {\n} n;"); 12554 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 12555 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 12556 12557 // Redefinition from nested context: 12558 verifyFormat("class A::B::C {\n} n;"); 12559 12560 // Template definitions. 12561 verifyFormat( 12562 "template <typename F>\n" 12563 "Matcher(const Matcher<F> &Other,\n" 12564 " typename enable_if_c<is_base_of<F, T>::value &&\n" 12565 " !is_same<F, T>::value>::type * = 0)\n" 12566 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 12567 12568 // FIXME: This is still incorrectly handled at the formatter side. 12569 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 12570 verifyFormat("int i = SomeFunction(a<b, a> b);"); 12571 12572 // FIXME: 12573 // This now gets parsed incorrectly as class definition. 12574 // verifyFormat("class A<int> f() {\n}\nint n;"); 12575 12576 // Elaborate types where incorrectly parsing the structural element would 12577 // break the indent. 12578 verifyFormat("if (true)\n" 12579 " class X x;\n" 12580 "else\n" 12581 " f();\n"); 12582 12583 // This is simply incomplete. Formatting is not important, but must not crash. 12584 verifyFormat("class A:"); 12585 } 12586 12587 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 12588 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 12589 format("#error Leave all white!!!!! space* alone!\n")); 12590 EXPECT_EQ( 12591 "#warning Leave all white!!!!! space* alone!\n", 12592 format("#warning Leave all white!!!!! space* alone!\n")); 12593 EXPECT_EQ("#error 1", format(" # error 1")); 12594 EXPECT_EQ("#warning 1", format(" # warning 1")); 12595 } 12596 12597 TEST_F(FormatTest, FormatHashIfExpressions) { 12598 verifyFormat("#if AAAA && BBBB"); 12599 verifyFormat("#if (AAAA && BBBB)"); 12600 verifyFormat("#elif (AAAA && BBBB)"); 12601 // FIXME: Come up with a better indentation for #elif. 12602 verifyFormat( 12603 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 12604 " defined(BBBBBBBB)\n" 12605 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 12606 " defined(BBBBBBBB)\n" 12607 "#endif", 12608 getLLVMStyleWithColumns(65)); 12609 } 12610 12611 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 12612 FormatStyle AllowsMergedIf = getGoogleStyle(); 12613 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 12614 FormatStyle::SIS_WithoutElse; 12615 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 12616 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 12617 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 12618 EXPECT_EQ("if (true) return 42;", 12619 format("if (true)\nreturn 42;", AllowsMergedIf)); 12620 FormatStyle ShortMergedIf = AllowsMergedIf; 12621 ShortMergedIf.ColumnLimit = 25; 12622 verifyFormat("#define A \\\n" 12623 " if (true) return 42;", 12624 ShortMergedIf); 12625 verifyFormat("#define A \\\n" 12626 " f(); \\\n" 12627 " if (true)\n" 12628 "#define B", 12629 ShortMergedIf); 12630 verifyFormat("#define A \\\n" 12631 " f(); \\\n" 12632 " if (true)\n" 12633 "g();", 12634 ShortMergedIf); 12635 verifyFormat("{\n" 12636 "#ifdef A\n" 12637 " // Comment\n" 12638 " if (true) continue;\n" 12639 "#endif\n" 12640 " // Comment\n" 12641 " if (true) continue;\n" 12642 "}", 12643 ShortMergedIf); 12644 ShortMergedIf.ColumnLimit = 33; 12645 verifyFormat("#define A \\\n" 12646 " if constexpr (true) return 42;", 12647 ShortMergedIf); 12648 verifyFormat("#define A \\\n" 12649 " if CONSTEXPR (true) return 42;", 12650 ShortMergedIf); 12651 ShortMergedIf.ColumnLimit = 29; 12652 verifyFormat("#define A \\\n" 12653 " if (aaaaaaaaaa) return 1; \\\n" 12654 " return 2;", 12655 ShortMergedIf); 12656 ShortMergedIf.ColumnLimit = 28; 12657 verifyFormat("#define A \\\n" 12658 " if (aaaaaaaaaa) \\\n" 12659 " return 1; \\\n" 12660 " return 2;", 12661 ShortMergedIf); 12662 verifyFormat("#define A \\\n" 12663 " if constexpr (aaaaaaa) \\\n" 12664 " return 1; \\\n" 12665 " return 2;", 12666 ShortMergedIf); 12667 verifyFormat("#define A \\\n" 12668 " if CONSTEXPR (aaaaaaa) \\\n" 12669 " return 1; \\\n" 12670 " return 2;", 12671 ShortMergedIf); 12672 } 12673 12674 TEST_F(FormatTest, FormatStarDependingOnContext) { 12675 verifyFormat("void f(int *a);"); 12676 verifyFormat("void f() { f(fint * b); }"); 12677 verifyFormat("class A {\n void f(int *a);\n};"); 12678 verifyFormat("class A {\n int *a;\n};"); 12679 verifyFormat("namespace a {\n" 12680 "namespace b {\n" 12681 "class A {\n" 12682 " void f() {}\n" 12683 " int *a;\n" 12684 "};\n" 12685 "} // namespace b\n" 12686 "} // namespace a"); 12687 } 12688 12689 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 12690 verifyFormat("while"); 12691 verifyFormat("operator"); 12692 } 12693 12694 TEST_F(FormatTest, SkipsDeeplyNestedLines) { 12695 // This code would be painfully slow to format if we didn't skip it. 12696 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 12697 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12698 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12699 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12700 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 12701 "A(1, 1)\n" 12702 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x 12703 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12704 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12705 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12706 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12707 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12708 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12709 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12710 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 12711 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n"); 12712 // Deeply nested part is untouched, rest is formatted. 12713 EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n", 12714 format(std::string("int i;\n") + Code + "int j;\n", 12715 getLLVMStyle(), SC_ExpectIncomplete)); 12716 } 12717 12718 //===----------------------------------------------------------------------===// 12719 // Objective-C tests. 12720 //===----------------------------------------------------------------------===// 12721 12722 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 12723 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 12724 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 12725 format("-(NSUInteger)indexOfObject:(id)anObject;")); 12726 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 12727 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 12728 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 12729 format("-(NSInteger)Method3:(id)anObject;")); 12730 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 12731 format("-(NSInteger)Method4:(id)anObject;")); 12732 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 12733 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 12734 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 12735 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 12736 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 12737 "forAllCells:(BOOL)flag;", 12738 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 12739 "forAllCells:(BOOL)flag;")); 12740 12741 // Very long objectiveC method declaration. 12742 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 12743 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 12744 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 12745 " inRange:(NSRange)range\n" 12746 " outRange:(NSRange)out_range\n" 12747 " outRange1:(NSRange)out_range1\n" 12748 " outRange2:(NSRange)out_range2\n" 12749 " outRange3:(NSRange)out_range3\n" 12750 " outRange4:(NSRange)out_range4\n" 12751 " outRange5:(NSRange)out_range5\n" 12752 " outRange6:(NSRange)out_range6\n" 12753 " outRange7:(NSRange)out_range7\n" 12754 " outRange8:(NSRange)out_range8\n" 12755 " outRange9:(NSRange)out_range9;"); 12756 12757 // When the function name has to be wrapped. 12758 FormatStyle Style = getLLVMStyle(); 12759 // ObjC ignores IndentWrappedFunctionNames when wrapping methods 12760 // and always indents instead. 12761 Style.IndentWrappedFunctionNames = false; 12762 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 12763 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 12764 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 12765 "}", 12766 Style); 12767 Style.IndentWrappedFunctionNames = true; 12768 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 12769 " veryLooooooooooongName:(NSString)cccccccccccccc\n" 12770 " anotherName:(NSString)dddddddddddddd {\n" 12771 "}", 12772 Style); 12773 12774 verifyFormat("- (int)sum:(vector<int>)numbers;"); 12775 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 12776 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 12777 // protocol lists (but not for template classes): 12778 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 12779 12780 verifyFormat("- (int (*)())foo:(int (*)())f;"); 12781 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 12782 12783 // If there's no return type (very rare in practice!), LLVM and Google style 12784 // agree. 12785 verifyFormat("- foo;"); 12786 verifyFormat("- foo:(int)f;"); 12787 verifyGoogleFormat("- foo:(int)foo;"); 12788 } 12789 12790 TEST_F(FormatTest, BreaksStringLiterals) { 12791 EXPECT_EQ("\"some text \"\n" 12792 "\"other\";", 12793 format("\"some text other\";", getLLVMStyleWithColumns(12))); 12794 EXPECT_EQ("\"some text \"\n" 12795 "\"other\";", 12796 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 12797 EXPECT_EQ( 12798 "#define A \\\n" 12799 " \"some \" \\\n" 12800 " \"text \" \\\n" 12801 " \"other\";", 12802 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 12803 EXPECT_EQ( 12804 "#define A \\\n" 12805 " \"so \" \\\n" 12806 " \"text \" \\\n" 12807 " \"other\";", 12808 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 12809 12810 EXPECT_EQ("\"some text\"", 12811 format("\"some text\"", getLLVMStyleWithColumns(1))); 12812 EXPECT_EQ("\"some text\"", 12813 format("\"some text\"", getLLVMStyleWithColumns(11))); 12814 EXPECT_EQ("\"some \"\n" 12815 "\"text\"", 12816 format("\"some text\"", getLLVMStyleWithColumns(10))); 12817 EXPECT_EQ("\"some \"\n" 12818 "\"text\"", 12819 format("\"some text\"", getLLVMStyleWithColumns(7))); 12820 EXPECT_EQ("\"some\"\n" 12821 "\" tex\"\n" 12822 "\"t\"", 12823 format("\"some text\"", getLLVMStyleWithColumns(6))); 12824 EXPECT_EQ("\"some\"\n" 12825 "\" tex\"\n" 12826 "\" and\"", 12827 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 12828 EXPECT_EQ("\"some\"\n" 12829 "\"/tex\"\n" 12830 "\"/and\"", 12831 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 12832 12833 EXPECT_EQ("variable =\n" 12834 " \"long string \"\n" 12835 " \"literal\";", 12836 format("variable = \"long string literal\";", 12837 getLLVMStyleWithColumns(20))); 12838 12839 EXPECT_EQ("variable = f(\n" 12840 " \"long string \"\n" 12841 " \"literal\",\n" 12842 " short,\n" 12843 " loooooooooooooooooooong);", 12844 format("variable = f(\"long string literal\", short, " 12845 "loooooooooooooooooooong);", 12846 getLLVMStyleWithColumns(20))); 12847 12848 EXPECT_EQ( 12849 "f(g(\"long string \"\n" 12850 " \"literal\"),\n" 12851 " b);", 12852 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 12853 EXPECT_EQ("f(g(\"long string \"\n" 12854 " \"literal\",\n" 12855 " a),\n" 12856 " b);", 12857 format("f(g(\"long string literal\", a), b);", 12858 getLLVMStyleWithColumns(20))); 12859 EXPECT_EQ( 12860 "f(\"one two\".split(\n" 12861 " variable));", 12862 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 12863 EXPECT_EQ("f(\"one two three four five six \"\n" 12864 " \"seven\".split(\n" 12865 " really_looooong_variable));", 12866 format("f(\"one two three four five six seven\"." 12867 "split(really_looooong_variable));", 12868 getLLVMStyleWithColumns(33))); 12869 12870 EXPECT_EQ("f(\"some \"\n" 12871 " \"text\",\n" 12872 " other);", 12873 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 12874 12875 // Only break as a last resort. 12876 verifyFormat( 12877 "aaaaaaaaaaaaaaaaaaaa(\n" 12878 " aaaaaaaaaaaaaaaaaaaa,\n" 12879 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 12880 12881 EXPECT_EQ("\"splitmea\"\n" 12882 "\"trandomp\"\n" 12883 "\"oint\"", 12884 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 12885 12886 EXPECT_EQ("\"split/\"\n" 12887 "\"pathat/\"\n" 12888 "\"slashes\"", 12889 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 12890 12891 EXPECT_EQ("\"split/\"\n" 12892 "\"pathat/\"\n" 12893 "\"slashes\"", 12894 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 12895 EXPECT_EQ("\"split at \"\n" 12896 "\"spaces/at/\"\n" 12897 "\"slashes.at.any$\"\n" 12898 "\"non-alphanumeric%\"\n" 12899 "\"1111111111characte\"\n" 12900 "\"rs\"", 12901 format("\"split at " 12902 "spaces/at/" 12903 "slashes.at." 12904 "any$non-" 12905 "alphanumeric%" 12906 "1111111111characte" 12907 "rs\"", 12908 getLLVMStyleWithColumns(20))); 12909 12910 // Verify that splitting the strings understands 12911 // Style::AlwaysBreakBeforeMultilineStrings. 12912 EXPECT_EQ("aaaaaaaaaaaa(\n" 12913 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 12914 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 12915 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 12916 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 12917 "aaaaaaaaaaaaaaaaaaaaaa\");", 12918 getGoogleStyle())); 12919 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 12920 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 12921 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 12922 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 12923 "aaaaaaaaaaaaaaaaaaaaaa\";", 12924 getGoogleStyle())); 12925 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 12926 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 12927 format("llvm::outs() << " 12928 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 12929 "aaaaaaaaaaaaaaaaaaa\";")); 12930 EXPECT_EQ("ffff(\n" 12931 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 12932 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 12933 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 12934 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 12935 getGoogleStyle())); 12936 12937 FormatStyle Style = getLLVMStyleWithColumns(12); 12938 Style.BreakStringLiterals = false; 12939 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 12940 12941 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 12942 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 12943 EXPECT_EQ("#define A \\\n" 12944 " \"some \" \\\n" 12945 " \"text \" \\\n" 12946 " \"other\";", 12947 format("#define A \"some text other\";", AlignLeft)); 12948 } 12949 12950 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) { 12951 EXPECT_EQ("C a = \"some more \"\n" 12952 " \"text\";", 12953 format("C a = \"some more text\";", getLLVMStyleWithColumns(18))); 12954 } 12955 12956 TEST_F(FormatTest, FullyRemoveEmptyLines) { 12957 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 12958 NoEmptyLines.MaxEmptyLinesToKeep = 0; 12959 EXPECT_EQ("int i = a(b());", 12960 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 12961 } 12962 12963 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 12964 EXPECT_EQ( 12965 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 12966 "(\n" 12967 " \"x\t\");", 12968 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 12969 "aaaaaaa(" 12970 "\"x\t\");")); 12971 } 12972 12973 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 12974 EXPECT_EQ( 12975 "u8\"utf8 string \"\n" 12976 "u8\"literal\";", 12977 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 12978 EXPECT_EQ( 12979 "u\"utf16 string \"\n" 12980 "u\"literal\";", 12981 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 12982 EXPECT_EQ( 12983 "U\"utf32 string \"\n" 12984 "U\"literal\";", 12985 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 12986 EXPECT_EQ("L\"wide string \"\n" 12987 "L\"literal\";", 12988 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 12989 EXPECT_EQ("@\"NSString \"\n" 12990 "@\"literal\";", 12991 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 12992 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26)); 12993 12994 // This input makes clang-format try to split the incomplete unicode escape 12995 // sequence, which used to lead to a crasher. 12996 verifyNoCrash( 12997 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 12998 getLLVMStyleWithColumns(60)); 12999 } 13000 13001 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 13002 FormatStyle Style = getGoogleStyleWithColumns(15); 13003 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 13004 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 13005 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 13006 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 13007 EXPECT_EQ("u8R\"x(raw literal)x\";", 13008 format("u8R\"x(raw literal)x\";", Style)); 13009 } 13010 13011 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 13012 FormatStyle Style = getLLVMStyleWithColumns(20); 13013 EXPECT_EQ( 13014 "_T(\"aaaaaaaaaaaaaa\")\n" 13015 "_T(\"aaaaaaaaaaaaaa\")\n" 13016 "_T(\"aaaaaaaaaaaa\")", 13017 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 13018 EXPECT_EQ("f(x,\n" 13019 " _T(\"aaaaaaaaaaaa\")\n" 13020 " _T(\"aaa\"),\n" 13021 " z);", 13022 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 13023 13024 // FIXME: Handle embedded spaces in one iteration. 13025 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 13026 // "_T(\"aaaaaaaaaaaaa\")\n" 13027 // "_T(\"aaaaaaaaaaaaa\")\n" 13028 // "_T(\"a\")", 13029 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 13030 // getLLVMStyleWithColumns(20))); 13031 EXPECT_EQ( 13032 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 13033 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 13034 EXPECT_EQ("f(\n" 13035 "#if !TEST\n" 13036 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 13037 "#endif\n" 13038 ");", 13039 format("f(\n" 13040 "#if !TEST\n" 13041 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 13042 "#endif\n" 13043 ");")); 13044 EXPECT_EQ("f(\n" 13045 "\n" 13046 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 13047 format("f(\n" 13048 "\n" 13049 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 13050 // Regression test for accessing tokens past the end of a vector in the 13051 // TokenLexer. 13052 verifyNoCrash(R"(_T( 13053 " 13054 ) 13055 )"); 13056 } 13057 13058 TEST_F(FormatTest, BreaksStringLiteralOperands) { 13059 // In a function call with two operands, the second can be broken with no line 13060 // break before it. 13061 EXPECT_EQ( 13062 "func(a, \"long long \"\n" 13063 " \"long long\");", 13064 format("func(a, \"long long long long\");", getLLVMStyleWithColumns(24))); 13065 // In a function call with three operands, the second must be broken with a 13066 // line break before it. 13067 EXPECT_EQ("func(a,\n" 13068 " \"long long long \"\n" 13069 " \"long\",\n" 13070 " c);", 13071 format("func(a, \"long long long long\", c);", 13072 getLLVMStyleWithColumns(24))); 13073 // In a function call with three operands, the third must be broken with a 13074 // line break before it. 13075 EXPECT_EQ("func(a, b,\n" 13076 " \"long long long \"\n" 13077 " \"long\");", 13078 format("func(a, b, \"long long long long\");", 13079 getLLVMStyleWithColumns(24))); 13080 // In a function call with three operands, both the second and the third must 13081 // be broken with a line break before them. 13082 EXPECT_EQ("func(a,\n" 13083 " \"long long long \"\n" 13084 " \"long\",\n" 13085 " \"long long long \"\n" 13086 " \"long\");", 13087 format("func(a, \"long long long long\", \"long long long long\");", 13088 getLLVMStyleWithColumns(24))); 13089 // In a chain of << with two operands, the second can be broken with no line 13090 // break before it. 13091 EXPECT_EQ("a << \"line line \"\n" 13092 " \"line\";", 13093 format("a << \"line line line\";", getLLVMStyleWithColumns(20))); 13094 // In a chain of << with three operands, the second can be broken with no line 13095 // break before it. 13096 EXPECT_EQ( 13097 "abcde << \"line \"\n" 13098 " \"line line\"\n" 13099 " << c;", 13100 format("abcde << \"line line line\" << c;", getLLVMStyleWithColumns(20))); 13101 // In a chain of << with three operands, the third must be broken with a line 13102 // break before it. 13103 EXPECT_EQ( 13104 "a << b\n" 13105 " << \"line line \"\n" 13106 " \"line\";", 13107 format("a << b << \"line line line\";", getLLVMStyleWithColumns(20))); 13108 // In a chain of << with three operands, the second can be broken with no line 13109 // break before it and the third must be broken with a line break before it. 13110 EXPECT_EQ("abcd << \"line line \"\n" 13111 " \"line\"\n" 13112 " << \"line line \"\n" 13113 " \"line\";", 13114 format("abcd << \"line line line\" << \"line line line\";", 13115 getLLVMStyleWithColumns(20))); 13116 // In a chain of binary operators with two operands, the second can be broken 13117 // with no line break before it. 13118 EXPECT_EQ( 13119 "abcd + \"line line \"\n" 13120 " \"line line\";", 13121 format("abcd + \"line line line line\";", getLLVMStyleWithColumns(20))); 13122 // In a chain of binary operators with three operands, the second must be 13123 // broken with a line break before it. 13124 EXPECT_EQ("abcd +\n" 13125 " \"line line \"\n" 13126 " \"line line\" +\n" 13127 " e;", 13128 format("abcd + \"line line line line\" + e;", 13129 getLLVMStyleWithColumns(20))); 13130 // In a function call with two operands, with AlignAfterOpenBracket enabled, 13131 // the first must be broken with a line break before it. 13132 FormatStyle Style = getLLVMStyleWithColumns(25); 13133 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 13134 EXPECT_EQ("someFunction(\n" 13135 " \"long long long \"\n" 13136 " \"long\",\n" 13137 " a);", 13138 format("someFunction(\"long long long long\", a);", Style)); 13139 } 13140 13141 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 13142 EXPECT_EQ( 13143 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 13144 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 13145 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 13146 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 13147 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 13148 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 13149 } 13150 13151 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 13152 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 13153 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 13154 EXPECT_EQ("fffffffffff(g(R\"x(\n" 13155 "multiline raw string literal xxxxxxxxxxxxxx\n" 13156 ")x\",\n" 13157 " a),\n" 13158 " b);", 13159 format("fffffffffff(g(R\"x(\n" 13160 "multiline raw string literal xxxxxxxxxxxxxx\n" 13161 ")x\", a), b);", 13162 getGoogleStyleWithColumns(20))); 13163 EXPECT_EQ("fffffffffff(\n" 13164 " g(R\"x(qqq\n" 13165 "multiline raw string literal xxxxxxxxxxxxxx\n" 13166 ")x\",\n" 13167 " a),\n" 13168 " b);", 13169 format("fffffffffff(g(R\"x(qqq\n" 13170 "multiline raw string literal xxxxxxxxxxxxxx\n" 13171 ")x\", a), b);", 13172 getGoogleStyleWithColumns(20))); 13173 13174 EXPECT_EQ("fffffffffff(R\"x(\n" 13175 "multiline raw string literal xxxxxxxxxxxxxx\n" 13176 ")x\");", 13177 format("fffffffffff(R\"x(\n" 13178 "multiline raw string literal xxxxxxxxxxxxxx\n" 13179 ")x\");", 13180 getGoogleStyleWithColumns(20))); 13181 EXPECT_EQ("fffffffffff(R\"x(\n" 13182 "multiline raw string literal xxxxxxxxxxxxxx\n" 13183 ")x\" + bbbbbb);", 13184 format("fffffffffff(R\"x(\n" 13185 "multiline raw string literal xxxxxxxxxxxxxx\n" 13186 ")x\" + bbbbbb);", 13187 getGoogleStyleWithColumns(20))); 13188 EXPECT_EQ("fffffffffff(\n" 13189 " R\"x(\n" 13190 "multiline raw string literal xxxxxxxxxxxxxx\n" 13191 ")x\" +\n" 13192 " bbbbbb);", 13193 format("fffffffffff(\n" 13194 " R\"x(\n" 13195 "multiline raw string literal xxxxxxxxxxxxxx\n" 13196 ")x\" + bbbbbb);", 13197 getGoogleStyleWithColumns(20))); 13198 EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);", 13199 format("fffffffffff(\n" 13200 " R\"(single line raw string)\" + bbbbbb);")); 13201 } 13202 13203 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 13204 verifyFormat("string a = \"unterminated;"); 13205 EXPECT_EQ("function(\"unterminated,\n" 13206 " OtherParameter);", 13207 format("function( \"unterminated,\n" 13208 " OtherParameter);")); 13209 } 13210 13211 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 13212 FormatStyle Style = getLLVMStyle(); 13213 Style.Standard = FormatStyle::LS_Cpp03; 13214 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 13215 format("#define x(_a) printf(\"foo\"_a);", Style)); 13216 } 13217 13218 TEST_F(FormatTest, CppLexVersion) { 13219 FormatStyle Style = getLLVMStyle(); 13220 // Formatting of x * y differs if x is a type. 13221 verifyFormat("void foo() { MACRO(a * b); }", Style); 13222 verifyFormat("void foo() { MACRO(int *b); }", Style); 13223 13224 // LLVM style uses latest lexer. 13225 verifyFormat("void foo() { MACRO(char8_t *b); }", Style); 13226 Style.Standard = FormatStyle::LS_Cpp17; 13227 // But in c++17, char8_t isn't a keyword. 13228 verifyFormat("void foo() { MACRO(char8_t * b); }", Style); 13229 } 13230 13231 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 13232 13233 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 13234 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 13235 " \"ddeeefff\");", 13236 format("someFunction(\"aaabbbcccdddeeefff\");", 13237 getLLVMStyleWithColumns(25))); 13238 EXPECT_EQ("someFunction1234567890(\n" 13239 " \"aaabbbcccdddeeefff\");", 13240 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 13241 getLLVMStyleWithColumns(26))); 13242 EXPECT_EQ("someFunction1234567890(\n" 13243 " \"aaabbbcccdddeeeff\"\n" 13244 " \"f\");", 13245 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 13246 getLLVMStyleWithColumns(25))); 13247 EXPECT_EQ("someFunction1234567890(\n" 13248 " \"aaabbbcccdddeeeff\"\n" 13249 " \"f\");", 13250 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 13251 getLLVMStyleWithColumns(24))); 13252 EXPECT_EQ("someFunction(\n" 13253 " \"aaabbbcc ddde \"\n" 13254 " \"efff\");", 13255 format("someFunction(\"aaabbbcc ddde efff\");", 13256 getLLVMStyleWithColumns(25))); 13257 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 13258 " \"ddeeefff\");", 13259 format("someFunction(\"aaabbbccc ddeeefff\");", 13260 getLLVMStyleWithColumns(25))); 13261 EXPECT_EQ("someFunction1234567890(\n" 13262 " \"aaabb \"\n" 13263 " \"cccdddeeefff\");", 13264 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 13265 getLLVMStyleWithColumns(25))); 13266 EXPECT_EQ("#define A \\\n" 13267 " string s = \\\n" 13268 " \"123456789\" \\\n" 13269 " \"0\"; \\\n" 13270 " int i;", 13271 format("#define A string s = \"1234567890\"; int i;", 13272 getLLVMStyleWithColumns(20))); 13273 EXPECT_EQ("someFunction(\n" 13274 " \"aaabbbcc \"\n" 13275 " \"dddeeefff\");", 13276 format("someFunction(\"aaabbbcc dddeeefff\");", 13277 getLLVMStyleWithColumns(25))); 13278 } 13279 13280 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 13281 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 13282 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 13283 EXPECT_EQ("\"test\"\n" 13284 "\"\\n\"", 13285 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 13286 EXPECT_EQ("\"tes\\\\\"\n" 13287 "\"n\"", 13288 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 13289 EXPECT_EQ("\"\\\\\\\\\"\n" 13290 "\"\\n\"", 13291 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 13292 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 13293 EXPECT_EQ("\"\\uff01\"\n" 13294 "\"test\"", 13295 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 13296 EXPECT_EQ("\"\\Uff01ff02\"", 13297 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 13298 EXPECT_EQ("\"\\x000000000001\"\n" 13299 "\"next\"", 13300 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 13301 EXPECT_EQ("\"\\x000000000001next\"", 13302 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 13303 EXPECT_EQ("\"\\x000000000001\"", 13304 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 13305 EXPECT_EQ("\"test\"\n" 13306 "\"\\000000\"\n" 13307 "\"000001\"", 13308 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 13309 EXPECT_EQ("\"test\\000\"\n" 13310 "\"00000000\"\n" 13311 "\"1\"", 13312 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 13313 } 13314 13315 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 13316 verifyFormat("void f() {\n" 13317 " return g() {}\n" 13318 " void h() {}"); 13319 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 13320 "g();\n" 13321 "}"); 13322 } 13323 13324 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 13325 verifyFormat( 13326 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 13327 } 13328 13329 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 13330 verifyFormat("class X {\n" 13331 " void f() {\n" 13332 " }\n" 13333 "};", 13334 getLLVMStyleWithColumns(12)); 13335 } 13336 13337 TEST_F(FormatTest, ConfigurableIndentWidth) { 13338 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 13339 EightIndent.IndentWidth = 8; 13340 EightIndent.ContinuationIndentWidth = 8; 13341 verifyFormat("void f() {\n" 13342 " someFunction();\n" 13343 " if (true) {\n" 13344 " f();\n" 13345 " }\n" 13346 "}", 13347 EightIndent); 13348 verifyFormat("class X {\n" 13349 " void f() {\n" 13350 " }\n" 13351 "};", 13352 EightIndent); 13353 verifyFormat("int x[] = {\n" 13354 " call(),\n" 13355 " call()};", 13356 EightIndent); 13357 } 13358 13359 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 13360 verifyFormat("double\n" 13361 "f();", 13362 getLLVMStyleWithColumns(8)); 13363 } 13364 13365 TEST_F(FormatTest, ConfigurableUseOfTab) { 13366 FormatStyle Tab = getLLVMStyleWithColumns(42); 13367 Tab.IndentWidth = 8; 13368 Tab.UseTab = FormatStyle::UT_Always; 13369 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left; 13370 13371 EXPECT_EQ("if (aaaaaaaa && // q\n" 13372 " bb)\t\t// w\n" 13373 "\t;", 13374 format("if (aaaaaaaa &&// q\n" 13375 "bb)// w\n" 13376 ";", 13377 Tab)); 13378 EXPECT_EQ("if (aaa && bbb) // w\n" 13379 "\t;", 13380 format("if(aaa&&bbb)// w\n" 13381 ";", 13382 Tab)); 13383 13384 verifyFormat("class X {\n" 13385 "\tvoid f() {\n" 13386 "\t\tsomeFunction(parameter1,\n" 13387 "\t\t\t parameter2);\n" 13388 "\t}\n" 13389 "};", 13390 Tab); 13391 verifyFormat("#define A \\\n" 13392 "\tvoid f() { \\\n" 13393 "\t\tsomeFunction( \\\n" 13394 "\t\t parameter1, \\\n" 13395 "\t\t parameter2); \\\n" 13396 "\t}", 13397 Tab); 13398 verifyFormat("int a;\t // x\n" 13399 "int bbbbbbbb; // x\n", 13400 Tab); 13401 13402 Tab.TabWidth = 4; 13403 Tab.IndentWidth = 8; 13404 verifyFormat("class TabWidth4Indent8 {\n" 13405 "\t\tvoid f() {\n" 13406 "\t\t\t\tsomeFunction(parameter1,\n" 13407 "\t\t\t\t\t\t\t parameter2);\n" 13408 "\t\t}\n" 13409 "};", 13410 Tab); 13411 13412 Tab.TabWidth = 4; 13413 Tab.IndentWidth = 4; 13414 verifyFormat("class TabWidth4Indent4 {\n" 13415 "\tvoid f() {\n" 13416 "\t\tsomeFunction(parameter1,\n" 13417 "\t\t\t\t\t parameter2);\n" 13418 "\t}\n" 13419 "};", 13420 Tab); 13421 13422 Tab.TabWidth = 8; 13423 Tab.IndentWidth = 4; 13424 verifyFormat("class TabWidth8Indent4 {\n" 13425 " void f() {\n" 13426 "\tsomeFunction(parameter1,\n" 13427 "\t\t parameter2);\n" 13428 " }\n" 13429 "};", 13430 Tab); 13431 13432 Tab.TabWidth = 8; 13433 Tab.IndentWidth = 8; 13434 EXPECT_EQ("/*\n" 13435 "\t a\t\tcomment\n" 13436 "\t in multiple lines\n" 13437 " */", 13438 format(" /*\t \t \n" 13439 " \t \t a\t\tcomment\t \t\n" 13440 " \t \t in multiple lines\t\n" 13441 " \t */", 13442 Tab)); 13443 13444 Tab.UseTab = FormatStyle::UT_ForIndentation; 13445 verifyFormat("{\n" 13446 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13447 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13448 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13449 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13450 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13451 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13452 "};", 13453 Tab); 13454 verifyFormat("enum AA {\n" 13455 "\ta1, // Force multiple lines\n" 13456 "\ta2,\n" 13457 "\ta3\n" 13458 "};", 13459 Tab); 13460 EXPECT_EQ("if (aaaaaaaa && // q\n" 13461 " bb) // w\n" 13462 "\t;", 13463 format("if (aaaaaaaa &&// q\n" 13464 "bb)// w\n" 13465 ";", 13466 Tab)); 13467 verifyFormat("class X {\n" 13468 "\tvoid f() {\n" 13469 "\t\tsomeFunction(parameter1,\n" 13470 "\t\t parameter2);\n" 13471 "\t}\n" 13472 "};", 13473 Tab); 13474 verifyFormat("{\n" 13475 "\tQ(\n" 13476 "\t {\n" 13477 "\t\t int a;\n" 13478 "\t\t someFunction(aaaaaaaa,\n" 13479 "\t\t bbbbbbb);\n" 13480 "\t },\n" 13481 "\t p);\n" 13482 "}", 13483 Tab); 13484 EXPECT_EQ("{\n" 13485 "\t/* aaaa\n" 13486 "\t bbbb */\n" 13487 "}", 13488 format("{\n" 13489 "/* aaaa\n" 13490 " bbbb */\n" 13491 "}", 13492 Tab)); 13493 EXPECT_EQ("{\n" 13494 "\t/*\n" 13495 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13496 "\t bbbbbbbbbbbbb\n" 13497 "\t*/\n" 13498 "}", 13499 format("{\n" 13500 "/*\n" 13501 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13502 "*/\n" 13503 "}", 13504 Tab)); 13505 EXPECT_EQ("{\n" 13506 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13507 "\t// bbbbbbbbbbbbb\n" 13508 "}", 13509 format("{\n" 13510 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13511 "}", 13512 Tab)); 13513 EXPECT_EQ("{\n" 13514 "\t/*\n" 13515 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13516 "\t bbbbbbbbbbbbb\n" 13517 "\t*/\n" 13518 "}", 13519 format("{\n" 13520 "\t/*\n" 13521 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13522 "\t*/\n" 13523 "}", 13524 Tab)); 13525 EXPECT_EQ("{\n" 13526 "\t/*\n" 13527 "\n" 13528 "\t*/\n" 13529 "}", 13530 format("{\n" 13531 "\t/*\n" 13532 "\n" 13533 "\t*/\n" 13534 "}", 13535 Tab)); 13536 EXPECT_EQ("{\n" 13537 "\t/*\n" 13538 " asdf\n" 13539 "\t*/\n" 13540 "}", 13541 format("{\n" 13542 "\t/*\n" 13543 " asdf\n" 13544 "\t*/\n" 13545 "}", 13546 Tab)); 13547 13548 verifyFormat("void f() {\n" 13549 "\treturn true ? aaaaaaaaaaaaaaaaaa\n" 13550 "\t : bbbbbbbbbbbbbbbbbb\n" 13551 "}", 13552 Tab); 13553 FormatStyle TabNoBreak = Tab; 13554 TabNoBreak.BreakBeforeTernaryOperators = false; 13555 verifyFormat("void f() {\n" 13556 "\treturn true ? aaaaaaaaaaaaaaaaaa :\n" 13557 "\t bbbbbbbbbbbbbbbbbb\n" 13558 "}", 13559 TabNoBreak); 13560 verifyFormat("void f() {\n" 13561 "\treturn true ?\n" 13562 "\t aaaaaaaaaaaaaaaaaaaa :\n" 13563 "\t bbbbbbbbbbbbbbbbbbbb\n" 13564 "}", 13565 TabNoBreak); 13566 13567 Tab.UseTab = FormatStyle::UT_Never; 13568 EXPECT_EQ("/*\n" 13569 " a\t\tcomment\n" 13570 " in multiple lines\n" 13571 " */", 13572 format(" /*\t \t \n" 13573 " \t \t a\t\tcomment\t \t\n" 13574 " \t \t in multiple lines\t\n" 13575 " \t */", 13576 Tab)); 13577 EXPECT_EQ("/* some\n" 13578 " comment */", 13579 format(" \t \t /* some\n" 13580 " \t \t comment */", 13581 Tab)); 13582 EXPECT_EQ("int a; /* some\n" 13583 " comment */", 13584 format(" \t \t int a; /* some\n" 13585 " \t \t comment */", 13586 Tab)); 13587 13588 EXPECT_EQ("int a; /* some\n" 13589 "comment */", 13590 format(" \t \t int\ta; /* some\n" 13591 " \t \t comment */", 13592 Tab)); 13593 EXPECT_EQ("f(\"\t\t\"); /* some\n" 13594 " comment */", 13595 format(" \t \t f(\"\t\t\"); /* some\n" 13596 " \t \t comment */", 13597 Tab)); 13598 EXPECT_EQ("{\n" 13599 " /*\n" 13600 " * Comment\n" 13601 " */\n" 13602 " int i;\n" 13603 "}", 13604 format("{\n" 13605 "\t/*\n" 13606 "\t * Comment\n" 13607 "\t */\n" 13608 "\t int i;\n" 13609 "}", 13610 Tab)); 13611 13612 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 13613 Tab.TabWidth = 8; 13614 Tab.IndentWidth = 8; 13615 EXPECT_EQ("if (aaaaaaaa && // q\n" 13616 " bb) // w\n" 13617 "\t;", 13618 format("if (aaaaaaaa &&// q\n" 13619 "bb)// w\n" 13620 ";", 13621 Tab)); 13622 EXPECT_EQ("if (aaa && bbb) // w\n" 13623 "\t;", 13624 format("if(aaa&&bbb)// w\n" 13625 ";", 13626 Tab)); 13627 verifyFormat("class X {\n" 13628 "\tvoid f() {\n" 13629 "\t\tsomeFunction(parameter1,\n" 13630 "\t\t\t parameter2);\n" 13631 "\t}\n" 13632 "};", 13633 Tab); 13634 verifyFormat("#define A \\\n" 13635 "\tvoid f() { \\\n" 13636 "\t\tsomeFunction( \\\n" 13637 "\t\t parameter1, \\\n" 13638 "\t\t parameter2); \\\n" 13639 "\t}", 13640 Tab); 13641 Tab.TabWidth = 4; 13642 Tab.IndentWidth = 8; 13643 verifyFormat("class TabWidth4Indent8 {\n" 13644 "\t\tvoid f() {\n" 13645 "\t\t\t\tsomeFunction(parameter1,\n" 13646 "\t\t\t\t\t\t\t parameter2);\n" 13647 "\t\t}\n" 13648 "};", 13649 Tab); 13650 Tab.TabWidth = 4; 13651 Tab.IndentWidth = 4; 13652 verifyFormat("class TabWidth4Indent4 {\n" 13653 "\tvoid f() {\n" 13654 "\t\tsomeFunction(parameter1,\n" 13655 "\t\t\t\t\t parameter2);\n" 13656 "\t}\n" 13657 "};", 13658 Tab); 13659 Tab.TabWidth = 8; 13660 Tab.IndentWidth = 4; 13661 verifyFormat("class TabWidth8Indent4 {\n" 13662 " void f() {\n" 13663 "\tsomeFunction(parameter1,\n" 13664 "\t\t parameter2);\n" 13665 " }\n" 13666 "};", 13667 Tab); 13668 Tab.TabWidth = 8; 13669 Tab.IndentWidth = 8; 13670 EXPECT_EQ("/*\n" 13671 "\t a\t\tcomment\n" 13672 "\t in multiple lines\n" 13673 " */", 13674 format(" /*\t \t \n" 13675 " \t \t a\t\tcomment\t \t\n" 13676 " \t \t in multiple lines\t\n" 13677 " \t */", 13678 Tab)); 13679 verifyFormat("{\n" 13680 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13681 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13682 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13683 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13684 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13685 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13686 "};", 13687 Tab); 13688 verifyFormat("enum AA {\n" 13689 "\ta1, // Force multiple lines\n" 13690 "\ta2,\n" 13691 "\ta3\n" 13692 "};", 13693 Tab); 13694 EXPECT_EQ("if (aaaaaaaa && // q\n" 13695 " bb) // w\n" 13696 "\t;", 13697 format("if (aaaaaaaa &&// q\n" 13698 "bb)// w\n" 13699 ";", 13700 Tab)); 13701 verifyFormat("class X {\n" 13702 "\tvoid f() {\n" 13703 "\t\tsomeFunction(parameter1,\n" 13704 "\t\t\t parameter2);\n" 13705 "\t}\n" 13706 "};", 13707 Tab); 13708 verifyFormat("{\n" 13709 "\tQ(\n" 13710 "\t {\n" 13711 "\t\t int a;\n" 13712 "\t\t someFunction(aaaaaaaa,\n" 13713 "\t\t\t\t bbbbbbb);\n" 13714 "\t },\n" 13715 "\t p);\n" 13716 "}", 13717 Tab); 13718 EXPECT_EQ("{\n" 13719 "\t/* aaaa\n" 13720 "\t bbbb */\n" 13721 "}", 13722 format("{\n" 13723 "/* aaaa\n" 13724 " bbbb */\n" 13725 "}", 13726 Tab)); 13727 EXPECT_EQ("{\n" 13728 "\t/*\n" 13729 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13730 "\t bbbbbbbbbbbbb\n" 13731 "\t*/\n" 13732 "}", 13733 format("{\n" 13734 "/*\n" 13735 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13736 "*/\n" 13737 "}", 13738 Tab)); 13739 EXPECT_EQ("{\n" 13740 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13741 "\t// bbbbbbbbbbbbb\n" 13742 "}", 13743 format("{\n" 13744 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13745 "}", 13746 Tab)); 13747 EXPECT_EQ("{\n" 13748 "\t/*\n" 13749 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13750 "\t bbbbbbbbbbbbb\n" 13751 "\t*/\n" 13752 "}", 13753 format("{\n" 13754 "\t/*\n" 13755 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13756 "\t*/\n" 13757 "}", 13758 Tab)); 13759 EXPECT_EQ("{\n" 13760 "\t/*\n" 13761 "\n" 13762 "\t*/\n" 13763 "}", 13764 format("{\n" 13765 "\t/*\n" 13766 "\n" 13767 "\t*/\n" 13768 "}", 13769 Tab)); 13770 EXPECT_EQ("{\n" 13771 "\t/*\n" 13772 " asdf\n" 13773 "\t*/\n" 13774 "}", 13775 format("{\n" 13776 "\t/*\n" 13777 " asdf\n" 13778 "\t*/\n" 13779 "}", 13780 Tab)); 13781 EXPECT_EQ("/* some\n" 13782 " comment */", 13783 format(" \t \t /* some\n" 13784 " \t \t comment */", 13785 Tab)); 13786 EXPECT_EQ("int a; /* some\n" 13787 " comment */", 13788 format(" \t \t int a; /* some\n" 13789 " \t \t comment */", 13790 Tab)); 13791 EXPECT_EQ("int a; /* some\n" 13792 "comment */", 13793 format(" \t \t int\ta; /* some\n" 13794 " \t \t comment */", 13795 Tab)); 13796 EXPECT_EQ("f(\"\t\t\"); /* some\n" 13797 " comment */", 13798 format(" \t \t f(\"\t\t\"); /* some\n" 13799 " \t \t comment */", 13800 Tab)); 13801 EXPECT_EQ("{\n" 13802 "\t/*\n" 13803 "\t * Comment\n" 13804 "\t */\n" 13805 "\tint i;\n" 13806 "}", 13807 format("{\n" 13808 "\t/*\n" 13809 "\t * Comment\n" 13810 "\t */\n" 13811 "\t int i;\n" 13812 "}", 13813 Tab)); 13814 Tab.TabWidth = 2; 13815 Tab.IndentWidth = 2; 13816 EXPECT_EQ("{\n" 13817 "\t/* aaaa\n" 13818 "\t\t bbbb */\n" 13819 "}", 13820 format("{\n" 13821 "/* aaaa\n" 13822 "\t bbbb */\n" 13823 "}", 13824 Tab)); 13825 EXPECT_EQ("{\n" 13826 "\t/*\n" 13827 "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13828 "\t\tbbbbbbbbbbbbb\n" 13829 "\t*/\n" 13830 "}", 13831 format("{\n" 13832 "/*\n" 13833 "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13834 "*/\n" 13835 "}", 13836 Tab)); 13837 Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 13838 Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 13839 Tab.TabWidth = 4; 13840 Tab.IndentWidth = 4; 13841 verifyFormat("class Assign {\n" 13842 "\tvoid f() {\n" 13843 "\t\tint x = 123;\n" 13844 "\t\tint random = 4;\n" 13845 "\t\tstd::string alphabet =\n" 13846 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 13847 "\t}\n" 13848 "};", 13849 Tab); 13850 13851 Tab.UseTab = FormatStyle::UT_AlignWithSpaces; 13852 Tab.TabWidth = 8; 13853 Tab.IndentWidth = 8; 13854 EXPECT_EQ("if (aaaaaaaa && // q\n" 13855 " bb) // w\n" 13856 "\t;", 13857 format("if (aaaaaaaa &&// q\n" 13858 "bb)// w\n" 13859 ";", 13860 Tab)); 13861 EXPECT_EQ("if (aaa && bbb) // w\n" 13862 "\t;", 13863 format("if(aaa&&bbb)// w\n" 13864 ";", 13865 Tab)); 13866 verifyFormat("class X {\n" 13867 "\tvoid f() {\n" 13868 "\t\tsomeFunction(parameter1,\n" 13869 "\t\t parameter2);\n" 13870 "\t}\n" 13871 "};", 13872 Tab); 13873 verifyFormat("#define A \\\n" 13874 "\tvoid f() { \\\n" 13875 "\t\tsomeFunction( \\\n" 13876 "\t\t parameter1, \\\n" 13877 "\t\t parameter2); \\\n" 13878 "\t}", 13879 Tab); 13880 Tab.TabWidth = 4; 13881 Tab.IndentWidth = 8; 13882 verifyFormat("class TabWidth4Indent8 {\n" 13883 "\t\tvoid f() {\n" 13884 "\t\t\t\tsomeFunction(parameter1,\n" 13885 "\t\t\t\t parameter2);\n" 13886 "\t\t}\n" 13887 "};", 13888 Tab); 13889 Tab.TabWidth = 4; 13890 Tab.IndentWidth = 4; 13891 verifyFormat("class TabWidth4Indent4 {\n" 13892 "\tvoid f() {\n" 13893 "\t\tsomeFunction(parameter1,\n" 13894 "\t\t parameter2);\n" 13895 "\t}\n" 13896 "};", 13897 Tab); 13898 Tab.TabWidth = 8; 13899 Tab.IndentWidth = 4; 13900 verifyFormat("class TabWidth8Indent4 {\n" 13901 " void f() {\n" 13902 "\tsomeFunction(parameter1,\n" 13903 "\t parameter2);\n" 13904 " }\n" 13905 "};", 13906 Tab); 13907 Tab.TabWidth = 8; 13908 Tab.IndentWidth = 8; 13909 EXPECT_EQ("/*\n" 13910 " a\t\tcomment\n" 13911 " in multiple lines\n" 13912 " */", 13913 format(" /*\t \t \n" 13914 " \t \t a\t\tcomment\t \t\n" 13915 " \t \t in multiple lines\t\n" 13916 " \t */", 13917 Tab)); 13918 verifyFormat("{\n" 13919 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13920 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13921 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13922 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13923 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13924 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 13925 "};", 13926 Tab); 13927 verifyFormat("enum AA {\n" 13928 "\ta1, // Force multiple lines\n" 13929 "\ta2,\n" 13930 "\ta3\n" 13931 "};", 13932 Tab); 13933 EXPECT_EQ("if (aaaaaaaa && // q\n" 13934 " bb) // w\n" 13935 "\t;", 13936 format("if (aaaaaaaa &&// q\n" 13937 "bb)// w\n" 13938 ";", 13939 Tab)); 13940 verifyFormat("class X {\n" 13941 "\tvoid f() {\n" 13942 "\t\tsomeFunction(parameter1,\n" 13943 "\t\t parameter2);\n" 13944 "\t}\n" 13945 "};", 13946 Tab); 13947 verifyFormat("{\n" 13948 "\tQ(\n" 13949 "\t {\n" 13950 "\t\t int a;\n" 13951 "\t\t someFunction(aaaaaaaa,\n" 13952 "\t\t bbbbbbb);\n" 13953 "\t },\n" 13954 "\t p);\n" 13955 "}", 13956 Tab); 13957 EXPECT_EQ("{\n" 13958 "\t/* aaaa\n" 13959 "\t bbbb */\n" 13960 "}", 13961 format("{\n" 13962 "/* aaaa\n" 13963 " bbbb */\n" 13964 "}", 13965 Tab)); 13966 EXPECT_EQ("{\n" 13967 "\t/*\n" 13968 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13969 "\t bbbbbbbbbbbbb\n" 13970 "\t*/\n" 13971 "}", 13972 format("{\n" 13973 "/*\n" 13974 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13975 "*/\n" 13976 "}", 13977 Tab)); 13978 EXPECT_EQ("{\n" 13979 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13980 "\t// bbbbbbbbbbbbb\n" 13981 "}", 13982 format("{\n" 13983 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13984 "}", 13985 Tab)); 13986 EXPECT_EQ("{\n" 13987 "\t/*\n" 13988 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 13989 "\t bbbbbbbbbbbbb\n" 13990 "\t*/\n" 13991 "}", 13992 format("{\n" 13993 "\t/*\n" 13994 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 13995 "\t*/\n" 13996 "}", 13997 Tab)); 13998 EXPECT_EQ("{\n" 13999 "\t/*\n" 14000 "\n" 14001 "\t*/\n" 14002 "}", 14003 format("{\n" 14004 "\t/*\n" 14005 "\n" 14006 "\t*/\n" 14007 "}", 14008 Tab)); 14009 EXPECT_EQ("{\n" 14010 "\t/*\n" 14011 " asdf\n" 14012 "\t*/\n" 14013 "}", 14014 format("{\n" 14015 "\t/*\n" 14016 " asdf\n" 14017 "\t*/\n" 14018 "}", 14019 Tab)); 14020 EXPECT_EQ("/* some\n" 14021 " comment */", 14022 format(" \t \t /* some\n" 14023 " \t \t comment */", 14024 Tab)); 14025 EXPECT_EQ("int a; /* some\n" 14026 " comment */", 14027 format(" \t \t int a; /* some\n" 14028 " \t \t comment */", 14029 Tab)); 14030 EXPECT_EQ("int a; /* some\n" 14031 "comment */", 14032 format(" \t \t int\ta; /* some\n" 14033 " \t \t comment */", 14034 Tab)); 14035 EXPECT_EQ("f(\"\t\t\"); /* some\n" 14036 " comment */", 14037 format(" \t \t f(\"\t\t\"); /* some\n" 14038 " \t \t comment */", 14039 Tab)); 14040 EXPECT_EQ("{\n" 14041 "\t/*\n" 14042 "\t * Comment\n" 14043 "\t */\n" 14044 "\tint i;\n" 14045 "}", 14046 format("{\n" 14047 "\t/*\n" 14048 "\t * Comment\n" 14049 "\t */\n" 14050 "\t int i;\n" 14051 "}", 14052 Tab)); 14053 Tab.TabWidth = 2; 14054 Tab.IndentWidth = 2; 14055 EXPECT_EQ("{\n" 14056 "\t/* aaaa\n" 14057 "\t bbbb */\n" 14058 "}", 14059 format("{\n" 14060 "/* aaaa\n" 14061 " bbbb */\n" 14062 "}", 14063 Tab)); 14064 EXPECT_EQ("{\n" 14065 "\t/*\n" 14066 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 14067 "\t bbbbbbbbbbbbb\n" 14068 "\t*/\n" 14069 "}", 14070 format("{\n" 14071 "/*\n" 14072 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 14073 "*/\n" 14074 "}", 14075 Tab)); 14076 Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 14077 Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 14078 Tab.TabWidth = 4; 14079 Tab.IndentWidth = 4; 14080 verifyFormat("class Assign {\n" 14081 "\tvoid f() {\n" 14082 "\t\tint x = 123;\n" 14083 "\t\tint random = 4;\n" 14084 "\t\tstd::string alphabet =\n" 14085 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 14086 "\t}\n" 14087 "};", 14088 Tab); 14089 Tab.AlignOperands = FormatStyle::OAS_Align; 14090 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n" 14091 " cccccccccccccccccccc;", 14092 Tab); 14093 // no alignment 14094 verifyFormat("int aaaaaaaaaa =\n" 14095 "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 14096 Tab); 14097 verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n" 14098 " : bbbbbbbbbbbbbb ? 222222222222222\n" 14099 " : 333333333333333;", 14100 Tab); 14101 Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 14102 Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator; 14103 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n" 14104 " + cccccccccccccccccccc;", 14105 Tab); 14106 } 14107 14108 TEST_F(FormatTest, ZeroTabWidth) { 14109 FormatStyle Tab = getLLVMStyleWithColumns(42); 14110 Tab.IndentWidth = 8; 14111 Tab.UseTab = FormatStyle::UT_Never; 14112 Tab.TabWidth = 0; 14113 EXPECT_EQ("void a(){\n" 14114 " // line starts with '\t'\n" 14115 "};", 14116 format("void a(){\n" 14117 "\t// line starts with '\t'\n" 14118 "};", 14119 Tab)); 14120 14121 EXPECT_EQ("void a(){\n" 14122 " // line starts with '\t'\n" 14123 "};", 14124 format("void a(){\n" 14125 "\t\t// line starts with '\t'\n" 14126 "};", 14127 Tab)); 14128 14129 Tab.UseTab = FormatStyle::UT_ForIndentation; 14130 EXPECT_EQ("void a(){\n" 14131 " // line starts with '\t'\n" 14132 "};", 14133 format("void a(){\n" 14134 "\t// line starts with '\t'\n" 14135 "};", 14136 Tab)); 14137 14138 EXPECT_EQ("void a(){\n" 14139 " // line starts with '\t'\n" 14140 "};", 14141 format("void a(){\n" 14142 "\t\t// line starts with '\t'\n" 14143 "};", 14144 Tab)); 14145 14146 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 14147 EXPECT_EQ("void a(){\n" 14148 " // line starts with '\t'\n" 14149 "};", 14150 format("void a(){\n" 14151 "\t// line starts with '\t'\n" 14152 "};", 14153 Tab)); 14154 14155 EXPECT_EQ("void a(){\n" 14156 " // line starts with '\t'\n" 14157 "};", 14158 format("void a(){\n" 14159 "\t\t// line starts with '\t'\n" 14160 "};", 14161 Tab)); 14162 14163 Tab.UseTab = FormatStyle::UT_AlignWithSpaces; 14164 EXPECT_EQ("void a(){\n" 14165 " // line starts with '\t'\n" 14166 "};", 14167 format("void a(){\n" 14168 "\t// line starts with '\t'\n" 14169 "};", 14170 Tab)); 14171 14172 EXPECT_EQ("void a(){\n" 14173 " // line starts with '\t'\n" 14174 "};", 14175 format("void a(){\n" 14176 "\t\t// line starts with '\t'\n" 14177 "};", 14178 Tab)); 14179 14180 Tab.UseTab = FormatStyle::UT_Always; 14181 EXPECT_EQ("void a(){\n" 14182 "// line starts with '\t'\n" 14183 "};", 14184 format("void a(){\n" 14185 "\t// line starts with '\t'\n" 14186 "};", 14187 Tab)); 14188 14189 EXPECT_EQ("void a(){\n" 14190 "// line starts with '\t'\n" 14191 "};", 14192 format("void a(){\n" 14193 "\t\t// line starts with '\t'\n" 14194 "};", 14195 Tab)); 14196 } 14197 14198 TEST_F(FormatTest, CalculatesOriginalColumn) { 14199 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14200 "q\"; /* some\n" 14201 " comment */", 14202 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14203 "q\"; /* some\n" 14204 " comment */", 14205 getLLVMStyle())); 14206 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 14207 "/* some\n" 14208 " comment */", 14209 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 14210 " /* some\n" 14211 " comment */", 14212 getLLVMStyle())); 14213 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14214 "qqq\n" 14215 "/* some\n" 14216 " comment */", 14217 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14218 "qqq\n" 14219 " /* some\n" 14220 " comment */", 14221 getLLVMStyle())); 14222 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14223 "wwww; /* some\n" 14224 " comment */", 14225 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 14226 "wwww; /* some\n" 14227 " comment */", 14228 getLLVMStyle())); 14229 } 14230 14231 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 14232 FormatStyle NoSpace = getLLVMStyle(); 14233 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 14234 14235 verifyFormat("while(true)\n" 14236 " continue;", 14237 NoSpace); 14238 verifyFormat("for(;;)\n" 14239 " continue;", 14240 NoSpace); 14241 verifyFormat("if(true)\n" 14242 " f();\n" 14243 "else if(true)\n" 14244 " f();", 14245 NoSpace); 14246 verifyFormat("do {\n" 14247 " do_something();\n" 14248 "} while(something());", 14249 NoSpace); 14250 verifyFormat("switch(x) {\n" 14251 "default:\n" 14252 " break;\n" 14253 "}", 14254 NoSpace); 14255 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 14256 verifyFormat("size_t x = sizeof(x);", NoSpace); 14257 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 14258 verifyFormat("auto f(int x) -> typeof(x);", NoSpace); 14259 verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace); 14260 verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace); 14261 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 14262 verifyFormat("alignas(128) char a[128];", NoSpace); 14263 verifyFormat("size_t x = alignof(MyType);", NoSpace); 14264 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 14265 verifyFormat("int f() throw(Deprecated);", NoSpace); 14266 verifyFormat("typedef void (*cb)(int);", NoSpace); 14267 verifyFormat("T A::operator()();", NoSpace); 14268 verifyFormat("X A::operator++(T);", NoSpace); 14269 verifyFormat("auto lambda = []() { return 0; };", NoSpace); 14270 14271 FormatStyle Space = getLLVMStyle(); 14272 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 14273 14274 verifyFormat("int f ();", Space); 14275 verifyFormat("void f (int a, T b) {\n" 14276 " while (true)\n" 14277 " continue;\n" 14278 "}", 14279 Space); 14280 verifyFormat("if (true)\n" 14281 " f ();\n" 14282 "else if (true)\n" 14283 " f ();", 14284 Space); 14285 verifyFormat("do {\n" 14286 " do_something ();\n" 14287 "} while (something ());", 14288 Space); 14289 verifyFormat("switch (x) {\n" 14290 "default:\n" 14291 " break;\n" 14292 "}", 14293 Space); 14294 verifyFormat("A::A () : a (1) {}", Space); 14295 verifyFormat("void f () __attribute__ ((asdf));", Space); 14296 verifyFormat("*(&a + 1);\n" 14297 "&((&a)[1]);\n" 14298 "a[(b + c) * d];\n" 14299 "(((a + 1) * 2) + 3) * 4;", 14300 Space); 14301 verifyFormat("#define A(x) x", Space); 14302 verifyFormat("#define A (x) x", Space); 14303 verifyFormat("#if defined(x)\n" 14304 "#endif", 14305 Space); 14306 verifyFormat("auto i = std::make_unique<int> (5);", Space); 14307 verifyFormat("size_t x = sizeof (x);", Space); 14308 verifyFormat("auto f (int x) -> decltype (x);", Space); 14309 verifyFormat("auto f (int x) -> typeof (x);", Space); 14310 verifyFormat("auto f (int x) -> _Atomic (x);", Space); 14311 verifyFormat("auto f (int x) -> __underlying_type (x);", Space); 14312 verifyFormat("int f (T x) noexcept (x.create ());", Space); 14313 verifyFormat("alignas (128) char a[128];", Space); 14314 verifyFormat("size_t x = alignof (MyType);", Space); 14315 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 14316 verifyFormat("int f () throw (Deprecated);", Space); 14317 verifyFormat("typedef void (*cb) (int);", Space); 14318 // FIXME these tests regressed behaviour. 14319 // verifyFormat("T A::operator() ();", Space); 14320 // verifyFormat("X A::operator++ (T);", Space); 14321 verifyFormat("auto lambda = [] () { return 0; };", Space); 14322 verifyFormat("int x = int (y);", Space); 14323 14324 FormatStyle SomeSpace = getLLVMStyle(); 14325 SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses; 14326 14327 verifyFormat("[]() -> float {}", SomeSpace); 14328 verifyFormat("[] (auto foo) {}", SomeSpace); 14329 verifyFormat("[foo]() -> int {}", SomeSpace); 14330 verifyFormat("int f();", SomeSpace); 14331 verifyFormat("void f (int a, T b) {\n" 14332 " while (true)\n" 14333 " continue;\n" 14334 "}", 14335 SomeSpace); 14336 verifyFormat("if (true)\n" 14337 " f();\n" 14338 "else if (true)\n" 14339 " f();", 14340 SomeSpace); 14341 verifyFormat("do {\n" 14342 " do_something();\n" 14343 "} while (something());", 14344 SomeSpace); 14345 verifyFormat("switch (x) {\n" 14346 "default:\n" 14347 " break;\n" 14348 "}", 14349 SomeSpace); 14350 verifyFormat("A::A() : a (1) {}", SomeSpace); 14351 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace); 14352 verifyFormat("*(&a + 1);\n" 14353 "&((&a)[1]);\n" 14354 "a[(b + c) * d];\n" 14355 "(((a + 1) * 2) + 3) * 4;", 14356 SomeSpace); 14357 verifyFormat("#define A(x) x", SomeSpace); 14358 verifyFormat("#define A (x) x", SomeSpace); 14359 verifyFormat("#if defined(x)\n" 14360 "#endif", 14361 SomeSpace); 14362 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace); 14363 verifyFormat("size_t x = sizeof (x);", SomeSpace); 14364 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace); 14365 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace); 14366 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace); 14367 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace); 14368 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace); 14369 verifyFormat("alignas (128) char a[128];", SomeSpace); 14370 verifyFormat("size_t x = alignof (MyType);", SomeSpace); 14371 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", 14372 SomeSpace); 14373 verifyFormat("int f() throw (Deprecated);", SomeSpace); 14374 verifyFormat("typedef void (*cb) (int);", SomeSpace); 14375 verifyFormat("T A::operator()();", SomeSpace); 14376 // FIXME these tests regressed behaviour. 14377 // verifyFormat("X A::operator++ (T);", SomeSpace); 14378 verifyFormat("int x = int (y);", SomeSpace); 14379 verifyFormat("auto lambda = []() { return 0; };", SomeSpace); 14380 14381 FormatStyle SpaceControlStatements = getLLVMStyle(); 14382 SpaceControlStatements.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14383 SpaceControlStatements.SpaceBeforeParensOptions.AfterControlStatements = true; 14384 14385 verifyFormat("while (true)\n" 14386 " continue;", 14387 SpaceControlStatements); 14388 verifyFormat("if (true)\n" 14389 " f();\n" 14390 "else if (true)\n" 14391 " f();", 14392 SpaceControlStatements); 14393 verifyFormat("for (;;) {\n" 14394 " do_something();\n" 14395 "}", 14396 SpaceControlStatements); 14397 verifyFormat("do {\n" 14398 " do_something();\n" 14399 "} while (something());", 14400 SpaceControlStatements); 14401 verifyFormat("switch (x) {\n" 14402 "default:\n" 14403 " break;\n" 14404 "}", 14405 SpaceControlStatements); 14406 14407 FormatStyle SpaceFuncDecl = getLLVMStyle(); 14408 SpaceFuncDecl.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14409 SpaceFuncDecl.SpaceBeforeParensOptions.AfterFunctionDeclarationName = true; 14410 14411 verifyFormat("int f ();", SpaceFuncDecl); 14412 verifyFormat("void f(int a, T b) {}", SpaceFuncDecl); 14413 verifyFormat("A::A() : a(1) {}", SpaceFuncDecl); 14414 verifyFormat("void f () __attribute__((asdf));", SpaceFuncDecl); 14415 verifyFormat("#define A(x) x", SpaceFuncDecl); 14416 verifyFormat("#define A (x) x", SpaceFuncDecl); 14417 verifyFormat("#if defined(x)\n" 14418 "#endif", 14419 SpaceFuncDecl); 14420 verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDecl); 14421 verifyFormat("size_t x = sizeof(x);", SpaceFuncDecl); 14422 verifyFormat("auto f (int x) -> decltype(x);", SpaceFuncDecl); 14423 verifyFormat("auto f (int x) -> typeof(x);", SpaceFuncDecl); 14424 verifyFormat("auto f (int x) -> _Atomic(x);", SpaceFuncDecl); 14425 verifyFormat("auto f (int x) -> __underlying_type(x);", SpaceFuncDecl); 14426 verifyFormat("int f (T x) noexcept(x.create());", SpaceFuncDecl); 14427 verifyFormat("alignas(128) char a[128];", SpaceFuncDecl); 14428 verifyFormat("size_t x = alignof(MyType);", SpaceFuncDecl); 14429 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", 14430 SpaceFuncDecl); 14431 verifyFormat("int f () throw(Deprecated);", SpaceFuncDecl); 14432 verifyFormat("typedef void (*cb)(int);", SpaceFuncDecl); 14433 // FIXME these tests regressed behaviour. 14434 // verifyFormat("T A::operator() ();", SpaceFuncDecl); 14435 // verifyFormat("X A::operator++ (T);", SpaceFuncDecl); 14436 verifyFormat("T A::operator()() {}", SpaceFuncDecl); 14437 verifyFormat("auto lambda = []() { return 0; };", SpaceFuncDecl); 14438 verifyFormat("int x = int(y);", SpaceFuncDecl); 14439 verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}", 14440 SpaceFuncDecl); 14441 14442 FormatStyle SpaceFuncDef = getLLVMStyle(); 14443 SpaceFuncDef.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14444 SpaceFuncDef.SpaceBeforeParensOptions.AfterFunctionDefinitionName = true; 14445 14446 verifyFormat("int f();", SpaceFuncDef); 14447 verifyFormat("void f (int a, T b) {}", SpaceFuncDef); 14448 verifyFormat("A::A() : a(1) {}", SpaceFuncDef); 14449 verifyFormat("void f() __attribute__((asdf));", SpaceFuncDef); 14450 verifyFormat("#define A(x) x", SpaceFuncDef); 14451 verifyFormat("#define A (x) x", SpaceFuncDef); 14452 verifyFormat("#if defined(x)\n" 14453 "#endif", 14454 SpaceFuncDef); 14455 verifyFormat("auto i = std::make_unique<int>(5);", SpaceFuncDef); 14456 verifyFormat("size_t x = sizeof(x);", SpaceFuncDef); 14457 verifyFormat("auto f(int x) -> decltype(x);", SpaceFuncDef); 14458 verifyFormat("auto f(int x) -> typeof(x);", SpaceFuncDef); 14459 verifyFormat("auto f(int x) -> _Atomic(x);", SpaceFuncDef); 14460 verifyFormat("auto f(int x) -> __underlying_type(x);", SpaceFuncDef); 14461 verifyFormat("int f(T x) noexcept(x.create());", SpaceFuncDef); 14462 verifyFormat("alignas(128) char a[128];", SpaceFuncDef); 14463 verifyFormat("size_t x = alignof(MyType);", SpaceFuncDef); 14464 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", 14465 SpaceFuncDef); 14466 verifyFormat("int f() throw(Deprecated);", SpaceFuncDef); 14467 verifyFormat("typedef void (*cb)(int);", SpaceFuncDef); 14468 verifyFormat("T A::operator()();", SpaceFuncDef); 14469 verifyFormat("X A::operator++(T);", SpaceFuncDef); 14470 // verifyFormat("T A::operator() () {}", SpaceFuncDef); 14471 verifyFormat("auto lambda = [] () { return 0; };", SpaceFuncDef); 14472 verifyFormat("int x = int(y);", SpaceFuncDef); 14473 verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}", 14474 SpaceFuncDef); 14475 14476 FormatStyle SpaceIfMacros = getLLVMStyle(); 14477 SpaceIfMacros.IfMacros.clear(); 14478 SpaceIfMacros.IfMacros.push_back("MYIF"); 14479 SpaceIfMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14480 SpaceIfMacros.SpaceBeforeParensOptions.AfterIfMacros = true; 14481 verifyFormat("MYIF (a)\n return;", SpaceIfMacros); 14482 verifyFormat("MYIF (a)\n return;\nelse MYIF (b)\n return;", SpaceIfMacros); 14483 verifyFormat("MYIF (a)\n return;\nelse\n return;", SpaceIfMacros); 14484 14485 FormatStyle SpaceForeachMacros = getLLVMStyle(); 14486 SpaceForeachMacros.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14487 SpaceForeachMacros.SpaceBeforeParensOptions.AfterForeachMacros = true; 14488 verifyFormat("foreach (Item *item, itemlist) {}", SpaceForeachMacros); 14489 verifyFormat("Q_FOREACH (Item *item, itemlist) {}", SpaceForeachMacros); 14490 verifyFormat("BOOST_FOREACH (Item *item, itemlist) {}", SpaceForeachMacros); 14491 verifyFormat("UNKNOWN_FOREACH(Item *item, itemlist) {}", SpaceForeachMacros); 14492 14493 FormatStyle SomeSpace2 = getLLVMStyle(); 14494 SomeSpace2.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14495 SomeSpace2.SpaceBeforeParensOptions.BeforeNonEmptyParentheses = true; 14496 verifyFormat("[]() -> float {}", SomeSpace2); 14497 verifyFormat("[] (auto foo) {}", SomeSpace2); 14498 verifyFormat("[foo]() -> int {}", SomeSpace2); 14499 verifyFormat("int f();", SomeSpace2); 14500 verifyFormat("void f (int a, T b) {\n" 14501 " while (true)\n" 14502 " continue;\n" 14503 "}", 14504 SomeSpace2); 14505 verifyFormat("if (true)\n" 14506 " f();\n" 14507 "else if (true)\n" 14508 " f();", 14509 SomeSpace2); 14510 verifyFormat("do {\n" 14511 " do_something();\n" 14512 "} while (something());", 14513 SomeSpace2); 14514 verifyFormat("switch (x) {\n" 14515 "default:\n" 14516 " break;\n" 14517 "}", 14518 SomeSpace2); 14519 verifyFormat("A::A() : a (1) {}", SomeSpace2); 14520 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace2); 14521 verifyFormat("*(&a + 1);\n" 14522 "&((&a)[1]);\n" 14523 "a[(b + c) * d];\n" 14524 "(((a + 1) * 2) + 3) * 4;", 14525 SomeSpace2); 14526 verifyFormat("#define A(x) x", SomeSpace2); 14527 verifyFormat("#define A (x) x", SomeSpace2); 14528 verifyFormat("#if defined(x)\n" 14529 "#endif", 14530 SomeSpace2); 14531 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace2); 14532 verifyFormat("size_t x = sizeof (x);", SomeSpace2); 14533 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace2); 14534 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace2); 14535 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace2); 14536 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace2); 14537 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace2); 14538 verifyFormat("alignas (128) char a[128];", SomeSpace2); 14539 verifyFormat("size_t x = alignof (MyType);", SomeSpace2); 14540 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", 14541 SomeSpace2); 14542 verifyFormat("int f() throw (Deprecated);", SomeSpace2); 14543 verifyFormat("typedef void (*cb) (int);", SomeSpace2); 14544 verifyFormat("T A::operator()();", SomeSpace2); 14545 // verifyFormat("X A::operator++ (T);", SomeSpace2); 14546 verifyFormat("int x = int (y);", SomeSpace2); 14547 verifyFormat("auto lambda = []() { return 0; };", SomeSpace2); 14548 14549 FormatStyle SpaceAfterOverloadedOperator = getLLVMStyle(); 14550 SpaceAfterOverloadedOperator.SpaceBeforeParens = FormatStyle::SBPO_Custom; 14551 SpaceAfterOverloadedOperator.SpaceBeforeParensOptions 14552 .AfterOverloadedOperator = true; 14553 14554 verifyFormat("auto operator++ () -> int;", SpaceAfterOverloadedOperator); 14555 verifyFormat("X A::operator++ ();", SpaceAfterOverloadedOperator); 14556 verifyFormat("some_object.operator++ ();", SpaceAfterOverloadedOperator); 14557 verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator); 14558 14559 SpaceAfterOverloadedOperator.SpaceBeforeParensOptions 14560 .AfterOverloadedOperator = false; 14561 14562 verifyFormat("auto operator++() -> int;", SpaceAfterOverloadedOperator); 14563 verifyFormat("X A::operator++();", SpaceAfterOverloadedOperator); 14564 verifyFormat("some_object.operator++();", SpaceAfterOverloadedOperator); 14565 verifyFormat("auto func() -> int;", SpaceAfterOverloadedOperator); 14566 } 14567 14568 TEST_F(FormatTest, SpaceAfterLogicalNot) { 14569 FormatStyle Spaces = getLLVMStyle(); 14570 Spaces.SpaceAfterLogicalNot = true; 14571 14572 verifyFormat("bool x = ! y", Spaces); 14573 verifyFormat("if (! isFailure())", Spaces); 14574 verifyFormat("if (! (a && b))", Spaces); 14575 verifyFormat("\"Error!\"", Spaces); 14576 verifyFormat("! ! x", Spaces); 14577 } 14578 14579 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 14580 FormatStyle Spaces = getLLVMStyle(); 14581 14582 Spaces.SpacesInParentheses = true; 14583 verifyFormat("do_something( ::globalVar );", Spaces); 14584 verifyFormat("call( x, y, z );", Spaces); 14585 verifyFormat("call();", Spaces); 14586 verifyFormat("std::function<void( int, int )> callback;", Spaces); 14587 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 14588 Spaces); 14589 verifyFormat("while ( (bool)1 )\n" 14590 " continue;", 14591 Spaces); 14592 verifyFormat("for ( ;; )\n" 14593 " continue;", 14594 Spaces); 14595 verifyFormat("if ( true )\n" 14596 " f();\n" 14597 "else if ( true )\n" 14598 " f();", 14599 Spaces); 14600 verifyFormat("do {\n" 14601 " do_something( (int)i );\n" 14602 "} while ( something() );", 14603 Spaces); 14604 verifyFormat("switch ( x ) {\n" 14605 "default:\n" 14606 " break;\n" 14607 "}", 14608 Spaces); 14609 14610 Spaces.SpacesInParentheses = false; 14611 Spaces.SpacesInCStyleCastParentheses = true; 14612 verifyFormat("Type *A = ( Type * )P;", Spaces); 14613 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 14614 verifyFormat("x = ( int32 )y;", Spaces); 14615 verifyFormat("int a = ( int )(2.0f);", Spaces); 14616 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 14617 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 14618 verifyFormat("#define x (( int )-1)", Spaces); 14619 14620 // Run the first set of tests again with: 14621 Spaces.SpacesInParentheses = false; 14622 Spaces.SpaceInEmptyParentheses = true; 14623 Spaces.SpacesInCStyleCastParentheses = true; 14624 verifyFormat("call(x, y, z);", Spaces); 14625 verifyFormat("call( );", Spaces); 14626 verifyFormat("std::function<void(int, int)> callback;", Spaces); 14627 verifyFormat("while (( bool )1)\n" 14628 " continue;", 14629 Spaces); 14630 verifyFormat("for (;;)\n" 14631 " continue;", 14632 Spaces); 14633 verifyFormat("if (true)\n" 14634 " f( );\n" 14635 "else if (true)\n" 14636 " f( );", 14637 Spaces); 14638 verifyFormat("do {\n" 14639 " do_something(( int )i);\n" 14640 "} while (something( ));", 14641 Spaces); 14642 verifyFormat("switch (x) {\n" 14643 "default:\n" 14644 " break;\n" 14645 "}", 14646 Spaces); 14647 14648 // Run the first set of tests again with: 14649 Spaces.SpaceAfterCStyleCast = true; 14650 verifyFormat("call(x, y, z);", Spaces); 14651 verifyFormat("call( );", Spaces); 14652 verifyFormat("std::function<void(int, int)> callback;", Spaces); 14653 verifyFormat("while (( bool ) 1)\n" 14654 " continue;", 14655 Spaces); 14656 verifyFormat("for (;;)\n" 14657 " continue;", 14658 Spaces); 14659 verifyFormat("if (true)\n" 14660 " f( );\n" 14661 "else if (true)\n" 14662 " f( );", 14663 Spaces); 14664 verifyFormat("do {\n" 14665 " do_something(( int ) i);\n" 14666 "} while (something( ));", 14667 Spaces); 14668 verifyFormat("switch (x) {\n" 14669 "default:\n" 14670 " break;\n" 14671 "}", 14672 Spaces); 14673 verifyFormat("#define CONF_BOOL(x) ( bool * ) ( void * ) (x)", Spaces); 14674 verifyFormat("#define CONF_BOOL(x) ( bool * ) (x)", Spaces); 14675 verifyFormat("#define CONF_BOOL(x) ( bool ) (x)", Spaces); 14676 verifyFormat("bool *y = ( bool * ) ( void * ) (x);", Spaces); 14677 verifyFormat("bool *y = ( bool * ) (x);", Spaces); 14678 14679 // Run subset of tests again with: 14680 Spaces.SpacesInCStyleCastParentheses = false; 14681 Spaces.SpaceAfterCStyleCast = true; 14682 verifyFormat("while ((bool) 1)\n" 14683 " continue;", 14684 Spaces); 14685 verifyFormat("do {\n" 14686 " do_something((int) i);\n" 14687 "} while (something( ));", 14688 Spaces); 14689 14690 verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces); 14691 verifyFormat("size_t idx = (size_t) a;", Spaces); 14692 verifyFormat("size_t idx = (size_t) (a - 1);", Spaces); 14693 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); 14694 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); 14695 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); 14696 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); 14697 verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (x)", Spaces); 14698 verifyFormat("#define CONF_BOOL(x) (bool *) (void *) (int) (x)", Spaces); 14699 verifyFormat("bool *y = (bool *) (void *) (x);", Spaces); 14700 verifyFormat("bool *y = (bool *) (void *) (int) (x);", Spaces); 14701 verifyFormat("bool *y = (bool *) (void *) (int) foo(x);", Spaces); 14702 Spaces.ColumnLimit = 80; 14703 Spaces.IndentWidth = 4; 14704 Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 14705 verifyFormat("void foo( ) {\n" 14706 " size_t foo = (*(function))(\n" 14707 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " 14708 "BarrrrrrrrrrrrLong,\n" 14709 " FoooooooooLooooong);\n" 14710 "}", 14711 Spaces); 14712 Spaces.SpaceAfterCStyleCast = false; 14713 verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces); 14714 verifyFormat("size_t idx = (size_t)a;", Spaces); 14715 verifyFormat("size_t idx = (size_t)(a - 1);", Spaces); 14716 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); 14717 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); 14718 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); 14719 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); 14720 14721 verifyFormat("void foo( ) {\n" 14722 " size_t foo = (*(function))(\n" 14723 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " 14724 "BarrrrrrrrrrrrLong,\n" 14725 " FoooooooooLooooong);\n" 14726 "}", 14727 Spaces); 14728 } 14729 14730 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 14731 verifyFormat("int a[5];"); 14732 verifyFormat("a[3] += 42;"); 14733 14734 FormatStyle Spaces = getLLVMStyle(); 14735 Spaces.SpacesInSquareBrackets = true; 14736 // Not lambdas. 14737 verifyFormat("int a[ 5 ];", Spaces); 14738 verifyFormat("a[ 3 ] += 42;", Spaces); 14739 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 14740 verifyFormat("double &operator[](int i) { return 0; }\n" 14741 "int i;", 14742 Spaces); 14743 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 14744 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 14745 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 14746 // Lambdas. 14747 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 14748 verifyFormat("return [ i, args... ] {};", Spaces); 14749 verifyFormat("int foo = [ &bar ]() {};", Spaces); 14750 verifyFormat("int foo = [ = ]() {};", Spaces); 14751 verifyFormat("int foo = [ & ]() {};", Spaces); 14752 verifyFormat("int foo = [ =, &bar ]() {};", Spaces); 14753 verifyFormat("int foo = [ &bar, = ]() {};", Spaces); 14754 } 14755 14756 TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) { 14757 FormatStyle NoSpaceStyle = getLLVMStyle(); 14758 verifyFormat("int a[5];", NoSpaceStyle); 14759 verifyFormat("a[3] += 42;", NoSpaceStyle); 14760 14761 verifyFormat("int a[1];", NoSpaceStyle); 14762 verifyFormat("int 1 [a];", NoSpaceStyle); 14763 verifyFormat("int a[1][2];", NoSpaceStyle); 14764 verifyFormat("a[7] = 5;", NoSpaceStyle); 14765 verifyFormat("int a = (f())[23];", NoSpaceStyle); 14766 verifyFormat("f([] {})", NoSpaceStyle); 14767 14768 FormatStyle Space = getLLVMStyle(); 14769 Space.SpaceBeforeSquareBrackets = true; 14770 verifyFormat("int c = []() -> int { return 2; }();\n", Space); 14771 verifyFormat("return [i, args...] {};", Space); 14772 14773 verifyFormat("int a [5];", Space); 14774 verifyFormat("a [3] += 42;", Space); 14775 verifyFormat("constexpr char hello []{\"hello\"};", Space); 14776 verifyFormat("double &operator[](int i) { return 0; }\n" 14777 "int i;", 14778 Space); 14779 verifyFormat("std::unique_ptr<int []> foo() {}", Space); 14780 verifyFormat("int i = a [a][a]->f();", Space); 14781 verifyFormat("int i = (*b) [a]->f();", Space); 14782 14783 verifyFormat("int a [1];", Space); 14784 verifyFormat("int 1 [a];", Space); 14785 verifyFormat("int a [1][2];", Space); 14786 verifyFormat("a [7] = 5;", Space); 14787 verifyFormat("int a = (f()) [23];", Space); 14788 verifyFormat("f([] {})", Space); 14789 } 14790 14791 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 14792 verifyFormat("int a = 5;"); 14793 verifyFormat("a += 42;"); 14794 verifyFormat("a or_eq 8;"); 14795 14796 FormatStyle Spaces = getLLVMStyle(); 14797 Spaces.SpaceBeforeAssignmentOperators = false; 14798 verifyFormat("int a= 5;", Spaces); 14799 verifyFormat("a+= 42;", Spaces); 14800 verifyFormat("a or_eq 8;", Spaces); 14801 } 14802 14803 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) { 14804 verifyFormat("class Foo : public Bar {};"); 14805 verifyFormat("Foo::Foo() : foo(1) {}"); 14806 verifyFormat("for (auto a : b) {\n}"); 14807 verifyFormat("int x = a ? b : c;"); 14808 verifyFormat("{\n" 14809 "label0:\n" 14810 " int x = 0;\n" 14811 "}"); 14812 verifyFormat("switch (x) {\n" 14813 "case 1:\n" 14814 "default:\n" 14815 "}"); 14816 verifyFormat("switch (allBraces) {\n" 14817 "case 1: {\n" 14818 " break;\n" 14819 "}\n" 14820 "case 2: {\n" 14821 " [[fallthrough]];\n" 14822 "}\n" 14823 "default: {\n" 14824 " break;\n" 14825 "}\n" 14826 "}"); 14827 14828 FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30); 14829 CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false; 14830 verifyFormat("class Foo : public Bar {};", CtorInitializerStyle); 14831 verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle); 14832 verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle); 14833 verifyFormat("int x = a ? b : c;", CtorInitializerStyle); 14834 verifyFormat("{\n" 14835 "label1:\n" 14836 " int x = 0;\n" 14837 "}", 14838 CtorInitializerStyle); 14839 verifyFormat("switch (x) {\n" 14840 "case 1:\n" 14841 "default:\n" 14842 "}", 14843 CtorInitializerStyle); 14844 verifyFormat("switch (allBraces) {\n" 14845 "case 1: {\n" 14846 " break;\n" 14847 "}\n" 14848 "case 2: {\n" 14849 " [[fallthrough]];\n" 14850 "}\n" 14851 "default: {\n" 14852 " break;\n" 14853 "}\n" 14854 "}", 14855 CtorInitializerStyle); 14856 CtorInitializerStyle.BreakConstructorInitializers = 14857 FormatStyle::BCIS_AfterColon; 14858 verifyFormat("Fooooooooooo::Fooooooooooo():\n" 14859 " aaaaaaaaaaaaaaaa(1),\n" 14860 " bbbbbbbbbbbbbbbb(2) {}", 14861 CtorInitializerStyle); 14862 CtorInitializerStyle.BreakConstructorInitializers = 14863 FormatStyle::BCIS_BeforeComma; 14864 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 14865 " : aaaaaaaaaaaaaaaa(1)\n" 14866 " , bbbbbbbbbbbbbbbb(2) {}", 14867 CtorInitializerStyle); 14868 CtorInitializerStyle.BreakConstructorInitializers = 14869 FormatStyle::BCIS_BeforeColon; 14870 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 14871 " : aaaaaaaaaaaaaaaa(1),\n" 14872 " bbbbbbbbbbbbbbbb(2) {}", 14873 CtorInitializerStyle); 14874 CtorInitializerStyle.ConstructorInitializerIndentWidth = 0; 14875 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 14876 ": aaaaaaaaaaaaaaaa(1),\n" 14877 " bbbbbbbbbbbbbbbb(2) {}", 14878 CtorInitializerStyle); 14879 14880 FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30); 14881 InheritanceStyle.SpaceBeforeInheritanceColon = false; 14882 verifyFormat("class Foo: public Bar {};", InheritanceStyle); 14883 verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle); 14884 verifyFormat("for (auto a : b) {\n}", InheritanceStyle); 14885 verifyFormat("int x = a ? b : c;", InheritanceStyle); 14886 verifyFormat("{\n" 14887 "label2:\n" 14888 " int x = 0;\n" 14889 "}", 14890 InheritanceStyle); 14891 verifyFormat("switch (x) {\n" 14892 "case 1:\n" 14893 "default:\n" 14894 "}", 14895 InheritanceStyle); 14896 verifyFormat("switch (allBraces) {\n" 14897 "case 1: {\n" 14898 " break;\n" 14899 "}\n" 14900 "case 2: {\n" 14901 " [[fallthrough]];\n" 14902 "}\n" 14903 "default: {\n" 14904 " break;\n" 14905 "}\n" 14906 "}", 14907 InheritanceStyle); 14908 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma; 14909 verifyFormat("class Foooooooooooooooooooooo\n" 14910 " : public aaaaaaaaaaaaaaaaaa,\n" 14911 " public bbbbbbbbbbbbbbbbbb {\n" 14912 "}", 14913 InheritanceStyle); 14914 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon; 14915 verifyFormat("class Foooooooooooooooooooooo:\n" 14916 " public aaaaaaaaaaaaaaaaaa,\n" 14917 " public bbbbbbbbbbbbbbbbbb {\n" 14918 "}", 14919 InheritanceStyle); 14920 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 14921 verifyFormat("class Foooooooooooooooooooooo\n" 14922 " : public aaaaaaaaaaaaaaaaaa\n" 14923 " , public bbbbbbbbbbbbbbbbbb {\n" 14924 "}", 14925 InheritanceStyle); 14926 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 14927 verifyFormat("class Foooooooooooooooooooooo\n" 14928 " : public aaaaaaaaaaaaaaaaaa,\n" 14929 " public bbbbbbbbbbbbbbbbbb {\n" 14930 "}", 14931 InheritanceStyle); 14932 InheritanceStyle.ConstructorInitializerIndentWidth = 0; 14933 verifyFormat("class Foooooooooooooooooooooo\n" 14934 ": public aaaaaaaaaaaaaaaaaa,\n" 14935 " public bbbbbbbbbbbbbbbbbb {}", 14936 InheritanceStyle); 14937 14938 FormatStyle ForLoopStyle = getLLVMStyle(); 14939 ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false; 14940 verifyFormat("class Foo : public Bar {};", ForLoopStyle); 14941 verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle); 14942 verifyFormat("for (auto a: b) {\n}", ForLoopStyle); 14943 verifyFormat("int x = a ? b : c;", ForLoopStyle); 14944 verifyFormat("{\n" 14945 "label2:\n" 14946 " int x = 0;\n" 14947 "}", 14948 ForLoopStyle); 14949 verifyFormat("switch (x) {\n" 14950 "case 1:\n" 14951 "default:\n" 14952 "}", 14953 ForLoopStyle); 14954 verifyFormat("switch (allBraces) {\n" 14955 "case 1: {\n" 14956 " break;\n" 14957 "}\n" 14958 "case 2: {\n" 14959 " [[fallthrough]];\n" 14960 "}\n" 14961 "default: {\n" 14962 " break;\n" 14963 "}\n" 14964 "}", 14965 ForLoopStyle); 14966 14967 FormatStyle CaseStyle = getLLVMStyle(); 14968 CaseStyle.SpaceBeforeCaseColon = true; 14969 verifyFormat("class Foo : public Bar {};", CaseStyle); 14970 verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle); 14971 verifyFormat("for (auto a : b) {\n}", CaseStyle); 14972 verifyFormat("int x = a ? b : c;", CaseStyle); 14973 verifyFormat("switch (x) {\n" 14974 "case 1 :\n" 14975 "default :\n" 14976 "}", 14977 CaseStyle); 14978 verifyFormat("switch (allBraces) {\n" 14979 "case 1 : {\n" 14980 " break;\n" 14981 "}\n" 14982 "case 2 : {\n" 14983 " [[fallthrough]];\n" 14984 "}\n" 14985 "default : {\n" 14986 " break;\n" 14987 "}\n" 14988 "}", 14989 CaseStyle); 14990 14991 FormatStyle NoSpaceStyle = getLLVMStyle(); 14992 EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false); 14993 NoSpaceStyle.SpaceBeforeCtorInitializerColon = false; 14994 NoSpaceStyle.SpaceBeforeInheritanceColon = false; 14995 NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 14996 verifyFormat("class Foo: public Bar {};", NoSpaceStyle); 14997 verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle); 14998 verifyFormat("for (auto a: b) {\n}", NoSpaceStyle); 14999 verifyFormat("int x = a ? b : c;", NoSpaceStyle); 15000 verifyFormat("{\n" 15001 "label3:\n" 15002 " int x = 0;\n" 15003 "}", 15004 NoSpaceStyle); 15005 verifyFormat("switch (x) {\n" 15006 "case 1:\n" 15007 "default:\n" 15008 "}", 15009 NoSpaceStyle); 15010 verifyFormat("switch (allBraces) {\n" 15011 "case 1: {\n" 15012 " break;\n" 15013 "}\n" 15014 "case 2: {\n" 15015 " [[fallthrough]];\n" 15016 "}\n" 15017 "default: {\n" 15018 " break;\n" 15019 "}\n" 15020 "}", 15021 NoSpaceStyle); 15022 15023 FormatStyle InvertedSpaceStyle = getLLVMStyle(); 15024 InvertedSpaceStyle.SpaceBeforeCaseColon = true; 15025 InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false; 15026 InvertedSpaceStyle.SpaceBeforeInheritanceColon = false; 15027 InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 15028 verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle); 15029 verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle); 15030 verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle); 15031 verifyFormat("int x = a ? b : c;", InvertedSpaceStyle); 15032 verifyFormat("{\n" 15033 "label3:\n" 15034 " int x = 0;\n" 15035 "}", 15036 InvertedSpaceStyle); 15037 verifyFormat("switch (x) {\n" 15038 "case 1 :\n" 15039 "case 2 : {\n" 15040 " break;\n" 15041 "}\n" 15042 "default :\n" 15043 " break;\n" 15044 "}", 15045 InvertedSpaceStyle); 15046 verifyFormat("switch (allBraces) {\n" 15047 "case 1 : {\n" 15048 " break;\n" 15049 "}\n" 15050 "case 2 : {\n" 15051 " [[fallthrough]];\n" 15052 "}\n" 15053 "default : {\n" 15054 " break;\n" 15055 "}\n" 15056 "}", 15057 InvertedSpaceStyle); 15058 } 15059 15060 TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) { 15061 FormatStyle Style = getLLVMStyle(); 15062 15063 Style.PointerAlignment = FormatStyle::PAS_Left; 15064 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; 15065 verifyFormat("void* const* x = NULL;", Style); 15066 15067 #define verifyQualifierSpaces(Code, Pointers, Qualifiers) \ 15068 do { \ 15069 Style.PointerAlignment = FormatStyle::Pointers; \ 15070 Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers; \ 15071 verifyFormat(Code, Style); \ 15072 } while (false) 15073 15074 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default); 15075 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default); 15076 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default); 15077 15078 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before); 15079 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before); 15080 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before); 15081 15082 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After); 15083 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After); 15084 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After); 15085 15086 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both); 15087 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both); 15088 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both); 15089 15090 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default); 15091 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, 15092 SAPQ_Default); 15093 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 15094 SAPQ_Default); 15095 15096 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before); 15097 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, 15098 SAPQ_Before); 15099 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 15100 SAPQ_Before); 15101 15102 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After); 15103 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After); 15104 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 15105 SAPQ_After); 15106 15107 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both); 15108 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both); 15109 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both); 15110 15111 #undef verifyQualifierSpaces 15112 15113 FormatStyle Spaces = getLLVMStyle(); 15114 Spaces.AttributeMacros.push_back("qualified"); 15115 Spaces.PointerAlignment = FormatStyle::PAS_Right; 15116 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; 15117 verifyFormat("SomeType *volatile *a = NULL;", Spaces); 15118 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces); 15119 verifyFormat("std::vector<SomeType *const *> x;", Spaces); 15120 verifyFormat("std::vector<SomeType *qualified *> x;", Spaces); 15121 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 15122 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before; 15123 verifyFormat("SomeType * volatile *a = NULL;", Spaces); 15124 verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces); 15125 verifyFormat("std::vector<SomeType * const *> x;", Spaces); 15126 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces); 15127 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 15128 15129 // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left. 15130 Spaces.PointerAlignment = FormatStyle::PAS_Left; 15131 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before; 15132 verifyFormat("SomeType* volatile* a = NULL;", Spaces); 15133 verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces); 15134 verifyFormat("std::vector<SomeType* const*> x;", Spaces); 15135 verifyFormat("std::vector<SomeType* qualified*> x;", Spaces); 15136 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 15137 // However, setting it to SAPQ_After should add spaces after __attribute, etc. 15138 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After; 15139 verifyFormat("SomeType* volatile * a = NULL;", Spaces); 15140 verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces); 15141 verifyFormat("std::vector<SomeType* const *> x;", Spaces); 15142 verifyFormat("std::vector<SomeType* qualified *> x;", Spaces); 15143 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 15144 15145 // PAS_Middle should not have any noticeable changes even for SAPQ_Both 15146 Spaces.PointerAlignment = FormatStyle::PAS_Middle; 15147 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After; 15148 verifyFormat("SomeType * volatile * a = NULL;", Spaces); 15149 verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces); 15150 verifyFormat("std::vector<SomeType * const *> x;", Spaces); 15151 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces); 15152 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 15153 } 15154 15155 TEST_F(FormatTest, AlignConsecutiveMacros) { 15156 FormatStyle Style = getLLVMStyle(); 15157 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 15158 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 15159 Style.AlignConsecutiveMacros = FormatStyle::ACS_None; 15160 15161 verifyFormat("#define a 3\n" 15162 "#define bbbb 4\n" 15163 "#define ccc (5)", 15164 Style); 15165 15166 verifyFormat("#define f(x) (x * x)\n" 15167 "#define fff(x, y, z) (x * y + z)\n" 15168 "#define ffff(x, y) (x - y)", 15169 Style); 15170 15171 verifyFormat("#define foo(x, y) (x + y)\n" 15172 "#define bar (5, 6)(2 + 2)", 15173 Style); 15174 15175 verifyFormat("#define a 3\n" 15176 "#define bbbb 4\n" 15177 "#define ccc (5)\n" 15178 "#define f(x) (x * x)\n" 15179 "#define fff(x, y, z) (x * y + z)\n" 15180 "#define ffff(x, y) (x - y)", 15181 Style); 15182 15183 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15184 verifyFormat("#define a 3\n" 15185 "#define bbbb 4\n" 15186 "#define ccc (5)", 15187 Style); 15188 15189 verifyFormat("#define f(x) (x * x)\n" 15190 "#define fff(x, y, z) (x * y + z)\n" 15191 "#define ffff(x, y) (x - y)", 15192 Style); 15193 15194 verifyFormat("#define foo(x, y) (x + y)\n" 15195 "#define bar (5, 6)(2 + 2)", 15196 Style); 15197 15198 verifyFormat("#define a 3\n" 15199 "#define bbbb 4\n" 15200 "#define ccc (5)\n" 15201 "#define f(x) (x * x)\n" 15202 "#define fff(x, y, z) (x * y + z)\n" 15203 "#define ffff(x, y) (x - y)", 15204 Style); 15205 15206 verifyFormat("#define a 5\n" 15207 "#define foo(x, y) (x + y)\n" 15208 "#define CCC (6)\n" 15209 "auto lambda = []() {\n" 15210 " auto ii = 0;\n" 15211 " float j = 0;\n" 15212 " return 0;\n" 15213 "};\n" 15214 "int i = 0;\n" 15215 "float i2 = 0;\n" 15216 "auto v = type{\n" 15217 " i = 1, //\n" 15218 " (i = 2), //\n" 15219 " i = 3 //\n" 15220 "};", 15221 Style); 15222 15223 Style.AlignConsecutiveMacros = FormatStyle::ACS_None; 15224 Style.ColumnLimit = 20; 15225 15226 verifyFormat("#define a \\\n" 15227 " \"aabbbbbbbbbbbb\"\n" 15228 "#define D \\\n" 15229 " \"aabbbbbbbbbbbb\" \\\n" 15230 " \"ccddeeeeeeeee\"\n" 15231 "#define B \\\n" 15232 " \"QQQQQQQQQQQQQ\" \\\n" 15233 " \"FFFFFFFFFFFFF\" \\\n" 15234 " \"LLLLLLLL\"\n", 15235 Style); 15236 15237 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15238 verifyFormat("#define a \\\n" 15239 " \"aabbbbbbbbbbbb\"\n" 15240 "#define D \\\n" 15241 " \"aabbbbbbbbbbbb\" \\\n" 15242 " \"ccddeeeeeeeee\"\n" 15243 "#define B \\\n" 15244 " \"QQQQQQQQQQQQQ\" \\\n" 15245 " \"FFFFFFFFFFFFF\" \\\n" 15246 " \"LLLLLLLL\"\n", 15247 Style); 15248 15249 // Test across comments 15250 Style.MaxEmptyLinesToKeep = 10; 15251 Style.ReflowComments = false; 15252 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossComments; 15253 EXPECT_EQ("#define a 3\n" 15254 "// line comment\n" 15255 "#define bbbb 4\n" 15256 "#define ccc (5)", 15257 format("#define a 3\n" 15258 "// line comment\n" 15259 "#define bbbb 4\n" 15260 "#define ccc (5)", 15261 Style)); 15262 15263 EXPECT_EQ("#define a 3\n" 15264 "/* block comment */\n" 15265 "#define bbbb 4\n" 15266 "#define ccc (5)", 15267 format("#define a 3\n" 15268 "/* block comment */\n" 15269 "#define bbbb 4\n" 15270 "#define ccc (5)", 15271 Style)); 15272 15273 EXPECT_EQ("#define a 3\n" 15274 "/* multi-line *\n" 15275 " * block comment */\n" 15276 "#define bbbb 4\n" 15277 "#define ccc (5)", 15278 format("#define a 3\n" 15279 "/* multi-line *\n" 15280 " * block comment */\n" 15281 "#define bbbb 4\n" 15282 "#define ccc (5)", 15283 Style)); 15284 15285 EXPECT_EQ("#define a 3\n" 15286 "// multi-line line comment\n" 15287 "//\n" 15288 "#define bbbb 4\n" 15289 "#define ccc (5)", 15290 format("#define a 3\n" 15291 "// multi-line line comment\n" 15292 "//\n" 15293 "#define bbbb 4\n" 15294 "#define ccc (5)", 15295 Style)); 15296 15297 EXPECT_EQ("#define a 3\n" 15298 "// empty lines still break.\n" 15299 "\n" 15300 "#define bbbb 4\n" 15301 "#define ccc (5)", 15302 format("#define a 3\n" 15303 "// empty lines still break.\n" 15304 "\n" 15305 "#define bbbb 4\n" 15306 "#define ccc (5)", 15307 Style)); 15308 15309 // Test across empty lines 15310 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLines; 15311 EXPECT_EQ("#define a 3\n" 15312 "\n" 15313 "#define bbbb 4\n" 15314 "#define ccc (5)", 15315 format("#define a 3\n" 15316 "\n" 15317 "#define bbbb 4\n" 15318 "#define ccc (5)", 15319 Style)); 15320 15321 EXPECT_EQ("#define a 3\n" 15322 "\n" 15323 "\n" 15324 "\n" 15325 "#define bbbb 4\n" 15326 "#define ccc (5)", 15327 format("#define a 3\n" 15328 "\n" 15329 "\n" 15330 "\n" 15331 "#define bbbb 4\n" 15332 "#define ccc (5)", 15333 Style)); 15334 15335 EXPECT_EQ("#define a 3\n" 15336 "// comments should break alignment\n" 15337 "//\n" 15338 "#define bbbb 4\n" 15339 "#define ccc (5)", 15340 format("#define a 3\n" 15341 "// comments should break alignment\n" 15342 "//\n" 15343 "#define bbbb 4\n" 15344 "#define ccc (5)", 15345 Style)); 15346 15347 // Test across empty lines and comments 15348 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLinesAndComments; 15349 verifyFormat("#define a 3\n" 15350 "\n" 15351 "// line comment\n" 15352 "#define bbbb 4\n" 15353 "#define ccc (5)", 15354 Style); 15355 15356 EXPECT_EQ("#define a 3\n" 15357 "\n" 15358 "\n" 15359 "/* multi-line *\n" 15360 " * block comment */\n" 15361 "\n" 15362 "\n" 15363 "#define bbbb 4\n" 15364 "#define ccc (5)", 15365 format("#define a 3\n" 15366 "\n" 15367 "\n" 15368 "/* multi-line *\n" 15369 " * block comment */\n" 15370 "\n" 15371 "\n" 15372 "#define bbbb 4\n" 15373 "#define ccc (5)", 15374 Style)); 15375 15376 EXPECT_EQ("#define a 3\n" 15377 "\n" 15378 "\n" 15379 "/* multi-line *\n" 15380 " * block comment */\n" 15381 "\n" 15382 "\n" 15383 "#define bbbb 4\n" 15384 "#define ccc (5)", 15385 format("#define a 3\n" 15386 "\n" 15387 "\n" 15388 "/* multi-line *\n" 15389 " * block comment */\n" 15390 "\n" 15391 "\n" 15392 "#define bbbb 4\n" 15393 "#define ccc (5)", 15394 Style)); 15395 } 15396 15397 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) { 15398 FormatStyle Alignment = getLLVMStyle(); 15399 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15400 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossEmptyLines; 15401 15402 Alignment.MaxEmptyLinesToKeep = 10; 15403 /* Test alignment across empty lines */ 15404 EXPECT_EQ("int a = 5;\n" 15405 "\n" 15406 "int oneTwoThree = 123;", 15407 format("int a = 5;\n" 15408 "\n" 15409 "int oneTwoThree= 123;", 15410 Alignment)); 15411 EXPECT_EQ("int a = 5;\n" 15412 "int one = 1;\n" 15413 "\n" 15414 "int oneTwoThree = 123;", 15415 format("int a = 5;\n" 15416 "int one = 1;\n" 15417 "\n" 15418 "int oneTwoThree = 123;", 15419 Alignment)); 15420 EXPECT_EQ("int a = 5;\n" 15421 "int one = 1;\n" 15422 "\n" 15423 "int oneTwoThree = 123;\n" 15424 "int oneTwo = 12;", 15425 format("int a = 5;\n" 15426 "int one = 1;\n" 15427 "\n" 15428 "int oneTwoThree = 123;\n" 15429 "int oneTwo = 12;", 15430 Alignment)); 15431 15432 /* Test across comments */ 15433 EXPECT_EQ("int a = 5;\n" 15434 "/* block comment */\n" 15435 "int oneTwoThree = 123;", 15436 format("int a = 5;\n" 15437 "/* block comment */\n" 15438 "int oneTwoThree=123;", 15439 Alignment)); 15440 15441 EXPECT_EQ("int a = 5;\n" 15442 "// line comment\n" 15443 "int oneTwoThree = 123;", 15444 format("int a = 5;\n" 15445 "// line comment\n" 15446 "int oneTwoThree=123;", 15447 Alignment)); 15448 15449 /* Test across comments and newlines */ 15450 EXPECT_EQ("int a = 5;\n" 15451 "\n" 15452 "/* block comment */\n" 15453 "int oneTwoThree = 123;", 15454 format("int a = 5;\n" 15455 "\n" 15456 "/* block comment */\n" 15457 "int oneTwoThree=123;", 15458 Alignment)); 15459 15460 EXPECT_EQ("int a = 5;\n" 15461 "\n" 15462 "// line comment\n" 15463 "int oneTwoThree = 123;", 15464 format("int a = 5;\n" 15465 "\n" 15466 "// line comment\n" 15467 "int oneTwoThree=123;", 15468 Alignment)); 15469 } 15470 15471 TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) { 15472 FormatStyle Alignment = getLLVMStyle(); 15473 Alignment.AlignConsecutiveDeclarations = 15474 FormatStyle::ACS_AcrossEmptyLinesAndComments; 15475 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 15476 15477 Alignment.MaxEmptyLinesToKeep = 10; 15478 /* Test alignment across empty lines */ 15479 EXPECT_EQ("int a = 5;\n" 15480 "\n" 15481 "float const oneTwoThree = 123;", 15482 format("int a = 5;\n" 15483 "\n" 15484 "float const oneTwoThree = 123;", 15485 Alignment)); 15486 EXPECT_EQ("int a = 5;\n" 15487 "float const one = 1;\n" 15488 "\n" 15489 "int oneTwoThree = 123;", 15490 format("int a = 5;\n" 15491 "float const one = 1;\n" 15492 "\n" 15493 "int oneTwoThree = 123;", 15494 Alignment)); 15495 15496 /* Test across comments */ 15497 EXPECT_EQ("float const a = 5;\n" 15498 "/* block comment */\n" 15499 "int oneTwoThree = 123;", 15500 format("float const a = 5;\n" 15501 "/* block comment */\n" 15502 "int oneTwoThree=123;", 15503 Alignment)); 15504 15505 EXPECT_EQ("float const a = 5;\n" 15506 "// line comment\n" 15507 "int oneTwoThree = 123;", 15508 format("float const a = 5;\n" 15509 "// line comment\n" 15510 "int oneTwoThree=123;", 15511 Alignment)); 15512 15513 /* Test across comments and newlines */ 15514 EXPECT_EQ("float const a = 5;\n" 15515 "\n" 15516 "/* block comment */\n" 15517 "int oneTwoThree = 123;", 15518 format("float const a = 5;\n" 15519 "\n" 15520 "/* block comment */\n" 15521 "int oneTwoThree=123;", 15522 Alignment)); 15523 15524 EXPECT_EQ("float const a = 5;\n" 15525 "\n" 15526 "// line comment\n" 15527 "int oneTwoThree = 123;", 15528 format("float const a = 5;\n" 15529 "\n" 15530 "// line comment\n" 15531 "int oneTwoThree=123;", 15532 Alignment)); 15533 } 15534 15535 TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) { 15536 FormatStyle Alignment = getLLVMStyle(); 15537 Alignment.AlignConsecutiveBitFields = 15538 FormatStyle::ACS_AcrossEmptyLinesAndComments; 15539 15540 Alignment.MaxEmptyLinesToKeep = 10; 15541 /* Test alignment across empty lines */ 15542 EXPECT_EQ("int a : 5;\n" 15543 "\n" 15544 "int longbitfield : 6;", 15545 format("int a : 5;\n" 15546 "\n" 15547 "int longbitfield : 6;", 15548 Alignment)); 15549 EXPECT_EQ("int a : 5;\n" 15550 "int one : 1;\n" 15551 "\n" 15552 "int longbitfield : 6;", 15553 format("int a : 5;\n" 15554 "int one : 1;\n" 15555 "\n" 15556 "int longbitfield : 6;", 15557 Alignment)); 15558 15559 /* Test across comments */ 15560 EXPECT_EQ("int a : 5;\n" 15561 "/* block comment */\n" 15562 "int longbitfield : 6;", 15563 format("int a : 5;\n" 15564 "/* block comment */\n" 15565 "int longbitfield : 6;", 15566 Alignment)); 15567 EXPECT_EQ("int a : 5;\n" 15568 "int one : 1;\n" 15569 "// line comment\n" 15570 "int longbitfield : 6;", 15571 format("int a : 5;\n" 15572 "int one : 1;\n" 15573 "// line comment\n" 15574 "int longbitfield : 6;", 15575 Alignment)); 15576 15577 /* Test across comments and newlines */ 15578 EXPECT_EQ("int a : 5;\n" 15579 "/* block comment */\n" 15580 "\n" 15581 "int longbitfield : 6;", 15582 format("int a : 5;\n" 15583 "/* block comment */\n" 15584 "\n" 15585 "int longbitfield : 6;", 15586 Alignment)); 15587 EXPECT_EQ("int a : 5;\n" 15588 "int one : 1;\n" 15589 "\n" 15590 "// line comment\n" 15591 "\n" 15592 "int longbitfield : 6;", 15593 format("int a : 5;\n" 15594 "int one : 1;\n" 15595 "\n" 15596 "// line comment \n" 15597 "\n" 15598 "int longbitfield : 6;", 15599 Alignment)); 15600 } 15601 15602 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) { 15603 FormatStyle Alignment = getLLVMStyle(); 15604 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15605 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossComments; 15606 15607 Alignment.MaxEmptyLinesToKeep = 10; 15608 /* Test alignment across empty lines */ 15609 EXPECT_EQ("int a = 5;\n" 15610 "\n" 15611 "int oneTwoThree = 123;", 15612 format("int a = 5;\n" 15613 "\n" 15614 "int oneTwoThree= 123;", 15615 Alignment)); 15616 EXPECT_EQ("int a = 5;\n" 15617 "int one = 1;\n" 15618 "\n" 15619 "int oneTwoThree = 123;", 15620 format("int a = 5;\n" 15621 "int one = 1;\n" 15622 "\n" 15623 "int oneTwoThree = 123;", 15624 Alignment)); 15625 15626 /* Test across comments */ 15627 EXPECT_EQ("int a = 5;\n" 15628 "/* block comment */\n" 15629 "int oneTwoThree = 123;", 15630 format("int a = 5;\n" 15631 "/* block comment */\n" 15632 "int oneTwoThree=123;", 15633 Alignment)); 15634 15635 EXPECT_EQ("int a = 5;\n" 15636 "// line comment\n" 15637 "int oneTwoThree = 123;", 15638 format("int a = 5;\n" 15639 "// line comment\n" 15640 "int oneTwoThree=123;", 15641 Alignment)); 15642 15643 EXPECT_EQ("int a = 5;\n" 15644 "/*\n" 15645 " * multi-line block comment\n" 15646 " */\n" 15647 "int oneTwoThree = 123;", 15648 format("int a = 5;\n" 15649 "/*\n" 15650 " * multi-line block comment\n" 15651 " */\n" 15652 "int oneTwoThree=123;", 15653 Alignment)); 15654 15655 EXPECT_EQ("int a = 5;\n" 15656 "//\n" 15657 "// multi-line line comment\n" 15658 "//\n" 15659 "int oneTwoThree = 123;", 15660 format("int a = 5;\n" 15661 "//\n" 15662 "// multi-line line comment\n" 15663 "//\n" 15664 "int oneTwoThree=123;", 15665 Alignment)); 15666 15667 /* Test across comments and newlines */ 15668 EXPECT_EQ("int a = 5;\n" 15669 "\n" 15670 "/* block comment */\n" 15671 "int oneTwoThree = 123;", 15672 format("int a = 5;\n" 15673 "\n" 15674 "/* block comment */\n" 15675 "int oneTwoThree=123;", 15676 Alignment)); 15677 15678 EXPECT_EQ("int a = 5;\n" 15679 "\n" 15680 "// line comment\n" 15681 "int oneTwoThree = 123;", 15682 format("int a = 5;\n" 15683 "\n" 15684 "// line comment\n" 15685 "int oneTwoThree=123;", 15686 Alignment)); 15687 } 15688 15689 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) { 15690 FormatStyle Alignment = getLLVMStyle(); 15691 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 15692 Alignment.AlignConsecutiveAssignments = 15693 FormatStyle::ACS_AcrossEmptyLinesAndComments; 15694 verifyFormat("int a = 5;\n" 15695 "int oneTwoThree = 123;", 15696 Alignment); 15697 verifyFormat("int a = method();\n" 15698 "int oneTwoThree = 133;", 15699 Alignment); 15700 verifyFormat("a &= 5;\n" 15701 "bcd *= 5;\n" 15702 "ghtyf += 5;\n" 15703 "dvfvdb -= 5;\n" 15704 "a /= 5;\n" 15705 "vdsvsv %= 5;\n" 15706 "sfdbddfbdfbb ^= 5;\n" 15707 "dvsdsv |= 5;\n" 15708 "int dsvvdvsdvvv = 123;", 15709 Alignment); 15710 verifyFormat("int i = 1, j = 10;\n" 15711 "something = 2000;", 15712 Alignment); 15713 verifyFormat("something = 2000;\n" 15714 "int i = 1, j = 10;\n", 15715 Alignment); 15716 verifyFormat("something = 2000;\n" 15717 "another = 911;\n" 15718 "int i = 1, j = 10;\n" 15719 "oneMore = 1;\n" 15720 "i = 2;", 15721 Alignment); 15722 verifyFormat("int a = 5;\n" 15723 "int one = 1;\n" 15724 "method();\n" 15725 "int oneTwoThree = 123;\n" 15726 "int oneTwo = 12;", 15727 Alignment); 15728 verifyFormat("int oneTwoThree = 123;\n" 15729 "int oneTwo = 12;\n" 15730 "method();\n", 15731 Alignment); 15732 verifyFormat("int oneTwoThree = 123; // comment\n" 15733 "int oneTwo = 12; // comment", 15734 Alignment); 15735 15736 // Bug 25167 15737 /* Uncomment when fixed 15738 verifyFormat("#if A\n" 15739 "#else\n" 15740 "int aaaaaaaa = 12;\n" 15741 "#endif\n" 15742 "#if B\n" 15743 "#else\n" 15744 "int a = 12;\n" 15745 "#endif\n", 15746 Alignment); 15747 verifyFormat("enum foo {\n" 15748 "#if A\n" 15749 "#else\n" 15750 " aaaaaaaa = 12;\n" 15751 "#endif\n" 15752 "#if B\n" 15753 "#else\n" 15754 " a = 12;\n" 15755 "#endif\n" 15756 "};\n", 15757 Alignment); 15758 */ 15759 15760 Alignment.MaxEmptyLinesToKeep = 10; 15761 /* Test alignment across empty lines */ 15762 EXPECT_EQ("int a = 5;\n" 15763 "\n" 15764 "int oneTwoThree = 123;", 15765 format("int a = 5;\n" 15766 "\n" 15767 "int oneTwoThree= 123;", 15768 Alignment)); 15769 EXPECT_EQ("int a = 5;\n" 15770 "int one = 1;\n" 15771 "\n" 15772 "int oneTwoThree = 123;", 15773 format("int a = 5;\n" 15774 "int one = 1;\n" 15775 "\n" 15776 "int oneTwoThree = 123;", 15777 Alignment)); 15778 EXPECT_EQ("int a = 5;\n" 15779 "int one = 1;\n" 15780 "\n" 15781 "int oneTwoThree = 123;\n" 15782 "int oneTwo = 12;", 15783 format("int a = 5;\n" 15784 "int one = 1;\n" 15785 "\n" 15786 "int oneTwoThree = 123;\n" 15787 "int oneTwo = 12;", 15788 Alignment)); 15789 15790 /* Test across comments */ 15791 EXPECT_EQ("int a = 5;\n" 15792 "/* block comment */\n" 15793 "int oneTwoThree = 123;", 15794 format("int a = 5;\n" 15795 "/* block comment */\n" 15796 "int oneTwoThree=123;", 15797 Alignment)); 15798 15799 EXPECT_EQ("int a = 5;\n" 15800 "// line comment\n" 15801 "int oneTwoThree = 123;", 15802 format("int a = 5;\n" 15803 "// line comment\n" 15804 "int oneTwoThree=123;", 15805 Alignment)); 15806 15807 /* Test across comments and newlines */ 15808 EXPECT_EQ("int a = 5;\n" 15809 "\n" 15810 "/* block comment */\n" 15811 "int oneTwoThree = 123;", 15812 format("int a = 5;\n" 15813 "\n" 15814 "/* block comment */\n" 15815 "int oneTwoThree=123;", 15816 Alignment)); 15817 15818 EXPECT_EQ("int a = 5;\n" 15819 "\n" 15820 "// line comment\n" 15821 "int oneTwoThree = 123;", 15822 format("int a = 5;\n" 15823 "\n" 15824 "// line comment\n" 15825 "int oneTwoThree=123;", 15826 Alignment)); 15827 15828 EXPECT_EQ("int a = 5;\n" 15829 "//\n" 15830 "// multi-line line comment\n" 15831 "//\n" 15832 "int oneTwoThree = 123;", 15833 format("int a = 5;\n" 15834 "//\n" 15835 "// multi-line line comment\n" 15836 "//\n" 15837 "int oneTwoThree=123;", 15838 Alignment)); 15839 15840 EXPECT_EQ("int a = 5;\n" 15841 "/*\n" 15842 " * multi-line block comment\n" 15843 " */\n" 15844 "int oneTwoThree = 123;", 15845 format("int a = 5;\n" 15846 "/*\n" 15847 " * multi-line block comment\n" 15848 " */\n" 15849 "int oneTwoThree=123;", 15850 Alignment)); 15851 15852 EXPECT_EQ("int a = 5;\n" 15853 "\n" 15854 "/* block comment */\n" 15855 "\n" 15856 "\n" 15857 "\n" 15858 "int oneTwoThree = 123;", 15859 format("int a = 5;\n" 15860 "\n" 15861 "/* block comment */\n" 15862 "\n" 15863 "\n" 15864 "\n" 15865 "int oneTwoThree=123;", 15866 Alignment)); 15867 15868 EXPECT_EQ("int a = 5;\n" 15869 "\n" 15870 "// line comment\n" 15871 "\n" 15872 "\n" 15873 "\n" 15874 "int oneTwoThree = 123;", 15875 format("int a = 5;\n" 15876 "\n" 15877 "// line comment\n" 15878 "\n" 15879 "\n" 15880 "\n" 15881 "int oneTwoThree=123;", 15882 Alignment)); 15883 15884 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 15885 verifyFormat("#define A \\\n" 15886 " int aaaa = 12; \\\n" 15887 " int b = 23; \\\n" 15888 " int ccc = 234; \\\n" 15889 " int dddddddddd = 2345;", 15890 Alignment); 15891 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 15892 verifyFormat("#define A \\\n" 15893 " int aaaa = 12; \\\n" 15894 " int b = 23; \\\n" 15895 " int ccc = 234; \\\n" 15896 " int dddddddddd = 2345;", 15897 Alignment); 15898 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 15899 verifyFormat("#define A " 15900 " \\\n" 15901 " int aaaa = 12; " 15902 " \\\n" 15903 " int b = 23; " 15904 " \\\n" 15905 " int ccc = 234; " 15906 " \\\n" 15907 " int dddddddddd = 2345;", 15908 Alignment); 15909 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 15910 "k = 4, int l = 5,\n" 15911 " int m = 6) {\n" 15912 " int j = 10;\n" 15913 " otherThing = 1;\n" 15914 "}", 15915 Alignment); 15916 verifyFormat("void SomeFunction(int parameter = 0) {\n" 15917 " int i = 1;\n" 15918 " int j = 2;\n" 15919 " int big = 10000;\n" 15920 "}", 15921 Alignment); 15922 verifyFormat("class C {\n" 15923 "public:\n" 15924 " int i = 1;\n" 15925 " virtual void f() = 0;\n" 15926 "};", 15927 Alignment); 15928 verifyFormat("int i = 1;\n" 15929 "if (SomeType t = getSomething()) {\n" 15930 "}\n" 15931 "int j = 2;\n" 15932 "int big = 10000;", 15933 Alignment); 15934 verifyFormat("int j = 7;\n" 15935 "for (int k = 0; k < N; ++k) {\n" 15936 "}\n" 15937 "int j = 2;\n" 15938 "int big = 10000;\n" 15939 "}", 15940 Alignment); 15941 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 15942 verifyFormat("int i = 1;\n" 15943 "LooooooooooongType loooooooooooooooooooooongVariable\n" 15944 " = someLooooooooooooooooongFunction();\n" 15945 "int j = 2;", 15946 Alignment); 15947 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 15948 verifyFormat("int i = 1;\n" 15949 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 15950 " someLooooooooooooooooongFunction();\n" 15951 "int j = 2;", 15952 Alignment); 15953 15954 verifyFormat("auto lambda = []() {\n" 15955 " auto i = 0;\n" 15956 " return 0;\n" 15957 "};\n" 15958 "int i = 0;\n" 15959 "auto v = type{\n" 15960 " i = 1, //\n" 15961 " (i = 2), //\n" 15962 " i = 3 //\n" 15963 "};", 15964 Alignment); 15965 15966 verifyFormat( 15967 "int i = 1;\n" 15968 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 15969 " loooooooooooooooooooooongParameterB);\n" 15970 "int j = 2;", 15971 Alignment); 15972 15973 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 15974 " typename B = very_long_type_name_1,\n" 15975 " typename T_2 = very_long_type_name_2>\n" 15976 "auto foo() {}\n", 15977 Alignment); 15978 verifyFormat("int a, b = 1;\n" 15979 "int c = 2;\n" 15980 "int dd = 3;\n", 15981 Alignment); 15982 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 15983 "float b[1][] = {{3.f}};\n", 15984 Alignment); 15985 verifyFormat("for (int i = 0; i < 1; i++)\n" 15986 " int x = 1;\n", 15987 Alignment); 15988 verifyFormat("for (i = 0; i < 1; i++)\n" 15989 " x = 1;\n" 15990 "y = 1;\n", 15991 Alignment); 15992 15993 Alignment.ReflowComments = true; 15994 Alignment.ColumnLimit = 50; 15995 EXPECT_EQ("int x = 0;\n" 15996 "int yy = 1; /// specificlennospace\n" 15997 "int zzz = 2;\n", 15998 format("int x = 0;\n" 15999 "int yy = 1; ///specificlennospace\n" 16000 "int zzz = 2;\n", 16001 Alignment)); 16002 } 16003 16004 TEST_F(FormatTest, AlignConsecutiveAssignments) { 16005 FormatStyle Alignment = getLLVMStyle(); 16006 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 16007 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16008 verifyFormat("int a = 5;\n" 16009 "int oneTwoThree = 123;", 16010 Alignment); 16011 verifyFormat("int a = 5;\n" 16012 "int oneTwoThree = 123;", 16013 Alignment); 16014 16015 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16016 verifyFormat("int a = 5;\n" 16017 "int oneTwoThree = 123;", 16018 Alignment); 16019 verifyFormat("int a = method();\n" 16020 "int oneTwoThree = 133;", 16021 Alignment); 16022 verifyFormat("a &= 5;\n" 16023 "bcd *= 5;\n" 16024 "ghtyf += 5;\n" 16025 "dvfvdb -= 5;\n" 16026 "a /= 5;\n" 16027 "vdsvsv %= 5;\n" 16028 "sfdbddfbdfbb ^= 5;\n" 16029 "dvsdsv |= 5;\n" 16030 "int dsvvdvsdvvv = 123;", 16031 Alignment); 16032 verifyFormat("int i = 1, j = 10;\n" 16033 "something = 2000;", 16034 Alignment); 16035 verifyFormat("something = 2000;\n" 16036 "int i = 1, j = 10;\n", 16037 Alignment); 16038 verifyFormat("something = 2000;\n" 16039 "another = 911;\n" 16040 "int i = 1, j = 10;\n" 16041 "oneMore = 1;\n" 16042 "i = 2;", 16043 Alignment); 16044 verifyFormat("int a = 5;\n" 16045 "int one = 1;\n" 16046 "method();\n" 16047 "int oneTwoThree = 123;\n" 16048 "int oneTwo = 12;", 16049 Alignment); 16050 verifyFormat("int oneTwoThree = 123;\n" 16051 "int oneTwo = 12;\n" 16052 "method();\n", 16053 Alignment); 16054 verifyFormat("int oneTwoThree = 123; // comment\n" 16055 "int oneTwo = 12; // comment", 16056 Alignment); 16057 16058 // Bug 25167 16059 /* Uncomment when fixed 16060 verifyFormat("#if A\n" 16061 "#else\n" 16062 "int aaaaaaaa = 12;\n" 16063 "#endif\n" 16064 "#if B\n" 16065 "#else\n" 16066 "int a = 12;\n" 16067 "#endif\n", 16068 Alignment); 16069 verifyFormat("enum foo {\n" 16070 "#if A\n" 16071 "#else\n" 16072 " aaaaaaaa = 12;\n" 16073 "#endif\n" 16074 "#if B\n" 16075 "#else\n" 16076 " a = 12;\n" 16077 "#endif\n" 16078 "};\n", 16079 Alignment); 16080 */ 16081 16082 EXPECT_EQ("int a = 5;\n" 16083 "\n" 16084 "int oneTwoThree = 123;", 16085 format("int a = 5;\n" 16086 "\n" 16087 "int oneTwoThree= 123;", 16088 Alignment)); 16089 EXPECT_EQ("int a = 5;\n" 16090 "int one = 1;\n" 16091 "\n" 16092 "int oneTwoThree = 123;", 16093 format("int a = 5;\n" 16094 "int one = 1;\n" 16095 "\n" 16096 "int oneTwoThree = 123;", 16097 Alignment)); 16098 EXPECT_EQ("int a = 5;\n" 16099 "int one = 1;\n" 16100 "\n" 16101 "int oneTwoThree = 123;\n" 16102 "int oneTwo = 12;", 16103 format("int a = 5;\n" 16104 "int one = 1;\n" 16105 "\n" 16106 "int oneTwoThree = 123;\n" 16107 "int oneTwo = 12;", 16108 Alignment)); 16109 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 16110 verifyFormat("#define A \\\n" 16111 " int aaaa = 12; \\\n" 16112 " int b = 23; \\\n" 16113 " int ccc = 234; \\\n" 16114 " int dddddddddd = 2345;", 16115 Alignment); 16116 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 16117 verifyFormat("#define A \\\n" 16118 " int aaaa = 12; \\\n" 16119 " int b = 23; \\\n" 16120 " int ccc = 234; \\\n" 16121 " int dddddddddd = 2345;", 16122 Alignment); 16123 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 16124 verifyFormat("#define A " 16125 " \\\n" 16126 " int aaaa = 12; " 16127 " \\\n" 16128 " int b = 23; " 16129 " \\\n" 16130 " int ccc = 234; " 16131 " \\\n" 16132 " int dddddddddd = 2345;", 16133 Alignment); 16134 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 16135 "k = 4, int l = 5,\n" 16136 " int m = 6) {\n" 16137 " int j = 10;\n" 16138 " otherThing = 1;\n" 16139 "}", 16140 Alignment); 16141 verifyFormat("void SomeFunction(int parameter = 0) {\n" 16142 " int i = 1;\n" 16143 " int j = 2;\n" 16144 " int big = 10000;\n" 16145 "}", 16146 Alignment); 16147 verifyFormat("class C {\n" 16148 "public:\n" 16149 " int i = 1;\n" 16150 " virtual void f() = 0;\n" 16151 "};", 16152 Alignment); 16153 verifyFormat("int i = 1;\n" 16154 "if (SomeType t = getSomething()) {\n" 16155 "}\n" 16156 "int j = 2;\n" 16157 "int big = 10000;", 16158 Alignment); 16159 verifyFormat("int j = 7;\n" 16160 "for (int k = 0; k < N; ++k) {\n" 16161 "}\n" 16162 "int j = 2;\n" 16163 "int big = 10000;\n" 16164 "}", 16165 Alignment); 16166 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 16167 verifyFormat("int i = 1;\n" 16168 "LooooooooooongType loooooooooooooooooooooongVariable\n" 16169 " = someLooooooooooooooooongFunction();\n" 16170 "int j = 2;", 16171 Alignment); 16172 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 16173 verifyFormat("int i = 1;\n" 16174 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 16175 " someLooooooooooooooooongFunction();\n" 16176 "int j = 2;", 16177 Alignment); 16178 16179 verifyFormat("auto lambda = []() {\n" 16180 " auto i = 0;\n" 16181 " return 0;\n" 16182 "};\n" 16183 "int i = 0;\n" 16184 "auto v = type{\n" 16185 " i = 1, //\n" 16186 " (i = 2), //\n" 16187 " i = 3 //\n" 16188 "};", 16189 Alignment); 16190 16191 verifyFormat( 16192 "int i = 1;\n" 16193 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 16194 " loooooooooooooooooooooongParameterB);\n" 16195 "int j = 2;", 16196 Alignment); 16197 16198 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 16199 " typename B = very_long_type_name_1,\n" 16200 " typename T_2 = very_long_type_name_2>\n" 16201 "auto foo() {}\n", 16202 Alignment); 16203 verifyFormat("int a, b = 1;\n" 16204 "int c = 2;\n" 16205 "int dd = 3;\n", 16206 Alignment); 16207 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 16208 "float b[1][] = {{3.f}};\n", 16209 Alignment); 16210 verifyFormat("for (int i = 0; i < 1; i++)\n" 16211 " int x = 1;\n", 16212 Alignment); 16213 verifyFormat("for (i = 0; i < 1; i++)\n" 16214 " x = 1;\n" 16215 "y = 1;\n", 16216 Alignment); 16217 16218 Alignment.ReflowComments = true; 16219 Alignment.ColumnLimit = 50; 16220 EXPECT_EQ("int x = 0;\n" 16221 "int yy = 1; /// specificlennospace\n" 16222 "int zzz = 2;\n", 16223 format("int x = 0;\n" 16224 "int yy = 1; ///specificlennospace\n" 16225 "int zzz = 2;\n", 16226 Alignment)); 16227 } 16228 16229 TEST_F(FormatTest, AlignConsecutiveBitFields) { 16230 FormatStyle Alignment = getLLVMStyle(); 16231 Alignment.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive; 16232 verifyFormat("int const a : 5;\n" 16233 "int oneTwoThree : 23;", 16234 Alignment); 16235 16236 // Initializers are allowed starting with c++2a 16237 verifyFormat("int const a : 5 = 1;\n" 16238 "int oneTwoThree : 23 = 0;", 16239 Alignment); 16240 16241 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16242 verifyFormat("int const a : 5;\n" 16243 "int oneTwoThree : 23;", 16244 Alignment); 16245 16246 verifyFormat("int const a : 5; // comment\n" 16247 "int oneTwoThree : 23; // comment", 16248 Alignment); 16249 16250 verifyFormat("int const a : 5 = 1;\n" 16251 "int oneTwoThree : 23 = 0;", 16252 Alignment); 16253 16254 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16255 verifyFormat("int const a : 5 = 1;\n" 16256 "int oneTwoThree : 23 = 0;", 16257 Alignment); 16258 verifyFormat("int const a : 5 = {1};\n" 16259 "int oneTwoThree : 23 = 0;", 16260 Alignment); 16261 16262 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None; 16263 verifyFormat("int const a :5;\n" 16264 "int oneTwoThree:23;", 16265 Alignment); 16266 16267 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before; 16268 verifyFormat("int const a :5;\n" 16269 "int oneTwoThree :23;", 16270 Alignment); 16271 16272 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After; 16273 verifyFormat("int const a : 5;\n" 16274 "int oneTwoThree: 23;", 16275 Alignment); 16276 16277 // Known limitations: ':' is only recognized as a bitfield colon when 16278 // followed by a number. 16279 /* 16280 verifyFormat("int oneTwoThree : SOME_CONSTANT;\n" 16281 "int a : 5;", 16282 Alignment); 16283 */ 16284 } 16285 16286 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 16287 FormatStyle Alignment = getLLVMStyle(); 16288 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 16289 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_None; 16290 Alignment.PointerAlignment = FormatStyle::PAS_Right; 16291 verifyFormat("float const a = 5;\n" 16292 "int oneTwoThree = 123;", 16293 Alignment); 16294 verifyFormat("int a = 5;\n" 16295 "float const oneTwoThree = 123;", 16296 Alignment); 16297 16298 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16299 verifyFormat("float const a = 5;\n" 16300 "int oneTwoThree = 123;", 16301 Alignment); 16302 verifyFormat("int a = method();\n" 16303 "float const oneTwoThree = 133;", 16304 Alignment); 16305 verifyFormat("int i = 1, j = 10;\n" 16306 "something = 2000;", 16307 Alignment); 16308 verifyFormat("something = 2000;\n" 16309 "int i = 1, j = 10;\n", 16310 Alignment); 16311 verifyFormat("float something = 2000;\n" 16312 "double another = 911;\n" 16313 "int i = 1, j = 10;\n" 16314 "const int *oneMore = 1;\n" 16315 "unsigned i = 2;", 16316 Alignment); 16317 verifyFormat("float a = 5;\n" 16318 "int one = 1;\n" 16319 "method();\n" 16320 "const double oneTwoThree = 123;\n" 16321 "const unsigned int oneTwo = 12;", 16322 Alignment); 16323 verifyFormat("int oneTwoThree{0}; // comment\n" 16324 "unsigned oneTwo; // comment", 16325 Alignment); 16326 verifyFormat("unsigned int *a;\n" 16327 "int *b;\n" 16328 "unsigned int Const *c;\n" 16329 "unsigned int const *d;\n" 16330 "unsigned int Const &e;\n" 16331 "unsigned int const &f;", 16332 Alignment); 16333 verifyFormat("Const unsigned int *c;\n" 16334 "const unsigned int *d;\n" 16335 "Const unsigned int &e;\n" 16336 "const unsigned int &f;\n" 16337 "const unsigned g;\n" 16338 "Const unsigned h;", 16339 Alignment); 16340 EXPECT_EQ("float const a = 5;\n" 16341 "\n" 16342 "int oneTwoThree = 123;", 16343 format("float const a = 5;\n" 16344 "\n" 16345 "int oneTwoThree= 123;", 16346 Alignment)); 16347 EXPECT_EQ("float a = 5;\n" 16348 "int one = 1;\n" 16349 "\n" 16350 "unsigned oneTwoThree = 123;", 16351 format("float a = 5;\n" 16352 "int one = 1;\n" 16353 "\n" 16354 "unsigned oneTwoThree = 123;", 16355 Alignment)); 16356 EXPECT_EQ("float a = 5;\n" 16357 "int one = 1;\n" 16358 "\n" 16359 "unsigned oneTwoThree = 123;\n" 16360 "int oneTwo = 12;", 16361 format("float a = 5;\n" 16362 "int one = 1;\n" 16363 "\n" 16364 "unsigned oneTwoThree = 123;\n" 16365 "int oneTwo = 12;", 16366 Alignment)); 16367 // Function prototype alignment 16368 verifyFormat("int a();\n" 16369 "double b();", 16370 Alignment); 16371 verifyFormat("int a(int x);\n" 16372 "double b();", 16373 Alignment); 16374 unsigned OldColumnLimit = Alignment.ColumnLimit; 16375 // We need to set ColumnLimit to zero, in order to stress nested alignments, 16376 // otherwise the function parameters will be re-flowed onto a single line. 16377 Alignment.ColumnLimit = 0; 16378 EXPECT_EQ("int a(int x,\n" 16379 " float y);\n" 16380 "double b(int x,\n" 16381 " double y);", 16382 format("int a(int x,\n" 16383 " float y);\n" 16384 "double b(int x,\n" 16385 " double y);", 16386 Alignment)); 16387 // This ensures that function parameters of function declarations are 16388 // correctly indented when their owning functions are indented. 16389 // The failure case here is for 'double y' to not be indented enough. 16390 EXPECT_EQ("double a(int x);\n" 16391 "int b(int y,\n" 16392 " double z);", 16393 format("double a(int x);\n" 16394 "int b(int y,\n" 16395 " double z);", 16396 Alignment)); 16397 // Set ColumnLimit low so that we induce wrapping immediately after 16398 // the function name and opening paren. 16399 Alignment.ColumnLimit = 13; 16400 verifyFormat("int function(\n" 16401 " int x,\n" 16402 " bool y);", 16403 Alignment); 16404 Alignment.ColumnLimit = OldColumnLimit; 16405 // Ensure function pointers don't screw up recursive alignment 16406 verifyFormat("int a(int x, void (*fp)(int y));\n" 16407 "double b();", 16408 Alignment); 16409 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16410 // Ensure recursive alignment is broken by function braces, so that the 16411 // "a = 1" does not align with subsequent assignments inside the function 16412 // body. 16413 verifyFormat("int func(int a = 1) {\n" 16414 " int b = 2;\n" 16415 " int cc = 3;\n" 16416 "}", 16417 Alignment); 16418 verifyFormat("float something = 2000;\n" 16419 "double another = 911;\n" 16420 "int i = 1, j = 10;\n" 16421 "const int *oneMore = 1;\n" 16422 "unsigned i = 2;", 16423 Alignment); 16424 verifyFormat("int oneTwoThree = {0}; // comment\n" 16425 "unsigned oneTwo = 0; // comment", 16426 Alignment); 16427 // Make sure that scope is correctly tracked, in the absence of braces 16428 verifyFormat("for (int i = 0; i < n; i++)\n" 16429 " j = i;\n" 16430 "double x = 1;\n", 16431 Alignment); 16432 verifyFormat("if (int i = 0)\n" 16433 " j = i;\n" 16434 "double x = 1;\n", 16435 Alignment); 16436 // Ensure operator[] and operator() are comprehended 16437 verifyFormat("struct test {\n" 16438 " long long int foo();\n" 16439 " int operator[](int a);\n" 16440 " double bar();\n" 16441 "};\n", 16442 Alignment); 16443 verifyFormat("struct test {\n" 16444 " long long int foo();\n" 16445 " int operator()(int a);\n" 16446 " double bar();\n" 16447 "};\n", 16448 Alignment); 16449 // http://llvm.org/PR52914 16450 verifyFormat("char *a[] = {\"a\", // comment\n" 16451 " \"bb\"};\n" 16452 "int bbbbbbb = 0;", 16453 Alignment); 16454 16455 // PAS_Right 16456 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16457 " int const i = 1;\n" 16458 " int *j = 2;\n" 16459 " int big = 10000;\n" 16460 "\n" 16461 " unsigned oneTwoThree = 123;\n" 16462 " int oneTwo = 12;\n" 16463 " method();\n" 16464 " float k = 2;\n" 16465 " int ll = 10000;\n" 16466 "}", 16467 format("void SomeFunction(int parameter= 0) {\n" 16468 " int const i= 1;\n" 16469 " int *j=2;\n" 16470 " int big = 10000;\n" 16471 "\n" 16472 "unsigned oneTwoThree =123;\n" 16473 "int oneTwo = 12;\n" 16474 " method();\n" 16475 "float k= 2;\n" 16476 "int ll=10000;\n" 16477 "}", 16478 Alignment)); 16479 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16480 " int const i = 1;\n" 16481 " int **j = 2, ***k;\n" 16482 " int &k = i;\n" 16483 " int &&l = i + j;\n" 16484 " int big = 10000;\n" 16485 "\n" 16486 " unsigned oneTwoThree = 123;\n" 16487 " int oneTwo = 12;\n" 16488 " method();\n" 16489 " float k = 2;\n" 16490 " int ll = 10000;\n" 16491 "}", 16492 format("void SomeFunction(int parameter= 0) {\n" 16493 " int const i= 1;\n" 16494 " int **j=2,***k;\n" 16495 "int &k=i;\n" 16496 "int &&l=i+j;\n" 16497 " int big = 10000;\n" 16498 "\n" 16499 "unsigned oneTwoThree =123;\n" 16500 "int oneTwo = 12;\n" 16501 " method();\n" 16502 "float k= 2;\n" 16503 "int ll=10000;\n" 16504 "}", 16505 Alignment)); 16506 // variables are aligned at their name, pointers are at the right most 16507 // position 16508 verifyFormat("int *a;\n" 16509 "int **b;\n" 16510 "int ***c;\n" 16511 "int foobar;\n", 16512 Alignment); 16513 16514 // PAS_Left 16515 FormatStyle AlignmentLeft = Alignment; 16516 AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left; 16517 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16518 " int const i = 1;\n" 16519 " int* j = 2;\n" 16520 " int big = 10000;\n" 16521 "\n" 16522 " unsigned oneTwoThree = 123;\n" 16523 " int oneTwo = 12;\n" 16524 " method();\n" 16525 " float k = 2;\n" 16526 " int ll = 10000;\n" 16527 "}", 16528 format("void SomeFunction(int parameter= 0) {\n" 16529 " int const i= 1;\n" 16530 " int *j=2;\n" 16531 " int big = 10000;\n" 16532 "\n" 16533 "unsigned oneTwoThree =123;\n" 16534 "int oneTwo = 12;\n" 16535 " method();\n" 16536 "float k= 2;\n" 16537 "int ll=10000;\n" 16538 "}", 16539 AlignmentLeft)); 16540 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16541 " int const i = 1;\n" 16542 " int** j = 2;\n" 16543 " int& k = i;\n" 16544 " int&& l = i + j;\n" 16545 " int big = 10000;\n" 16546 "\n" 16547 " unsigned oneTwoThree = 123;\n" 16548 " int oneTwo = 12;\n" 16549 " method();\n" 16550 " float k = 2;\n" 16551 " int ll = 10000;\n" 16552 "}", 16553 format("void SomeFunction(int parameter= 0) {\n" 16554 " int const i= 1;\n" 16555 " int **j=2;\n" 16556 "int &k=i;\n" 16557 "int &&l=i+j;\n" 16558 " int big = 10000;\n" 16559 "\n" 16560 "unsigned oneTwoThree =123;\n" 16561 "int oneTwo = 12;\n" 16562 " method();\n" 16563 "float k= 2;\n" 16564 "int ll=10000;\n" 16565 "}", 16566 AlignmentLeft)); 16567 // variables are aligned at their name, pointers are at the left most position 16568 verifyFormat("int* a;\n" 16569 "int** b;\n" 16570 "int*** c;\n" 16571 "int foobar;\n", 16572 AlignmentLeft); 16573 16574 // PAS_Middle 16575 FormatStyle AlignmentMiddle = Alignment; 16576 AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle; 16577 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16578 " int const i = 1;\n" 16579 " int * j = 2;\n" 16580 " int big = 10000;\n" 16581 "\n" 16582 " unsigned oneTwoThree = 123;\n" 16583 " int oneTwo = 12;\n" 16584 " method();\n" 16585 " float k = 2;\n" 16586 " int ll = 10000;\n" 16587 "}", 16588 format("void SomeFunction(int parameter= 0) {\n" 16589 " int const i= 1;\n" 16590 " int *j=2;\n" 16591 " int big = 10000;\n" 16592 "\n" 16593 "unsigned oneTwoThree =123;\n" 16594 "int oneTwo = 12;\n" 16595 " method();\n" 16596 "float k= 2;\n" 16597 "int ll=10000;\n" 16598 "}", 16599 AlignmentMiddle)); 16600 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 16601 " int const i = 1;\n" 16602 " int ** j = 2, ***k;\n" 16603 " int & k = i;\n" 16604 " int && l = i + j;\n" 16605 " int big = 10000;\n" 16606 "\n" 16607 " unsigned oneTwoThree = 123;\n" 16608 " int oneTwo = 12;\n" 16609 " method();\n" 16610 " float k = 2;\n" 16611 " int ll = 10000;\n" 16612 "}", 16613 format("void SomeFunction(int parameter= 0) {\n" 16614 " int const i= 1;\n" 16615 " int **j=2,***k;\n" 16616 "int &k=i;\n" 16617 "int &&l=i+j;\n" 16618 " int big = 10000;\n" 16619 "\n" 16620 "unsigned oneTwoThree =123;\n" 16621 "int oneTwo = 12;\n" 16622 " method();\n" 16623 "float k= 2;\n" 16624 "int ll=10000;\n" 16625 "}", 16626 AlignmentMiddle)); 16627 // variables are aligned at their name, pointers are in the middle 16628 verifyFormat("int * a;\n" 16629 "int * b;\n" 16630 "int *** c;\n" 16631 "int foobar;\n", 16632 AlignmentMiddle); 16633 16634 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16635 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 16636 verifyFormat("#define A \\\n" 16637 " int aaaa = 12; \\\n" 16638 " float b = 23; \\\n" 16639 " const int ccc = 234; \\\n" 16640 " unsigned dddddddddd = 2345;", 16641 Alignment); 16642 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 16643 verifyFormat("#define A \\\n" 16644 " int aaaa = 12; \\\n" 16645 " float b = 23; \\\n" 16646 " const int ccc = 234; \\\n" 16647 " unsigned dddddddddd = 2345;", 16648 Alignment); 16649 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 16650 Alignment.ColumnLimit = 30; 16651 verifyFormat("#define A \\\n" 16652 " int aaaa = 12; \\\n" 16653 " float b = 23; \\\n" 16654 " const int ccc = 234; \\\n" 16655 " int dddddddddd = 2345;", 16656 Alignment); 16657 Alignment.ColumnLimit = 80; 16658 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 16659 "k = 4, int l = 5,\n" 16660 " int m = 6) {\n" 16661 " const int j = 10;\n" 16662 " otherThing = 1;\n" 16663 "}", 16664 Alignment); 16665 verifyFormat("void SomeFunction(int parameter = 0) {\n" 16666 " int const i = 1;\n" 16667 " int *j = 2;\n" 16668 " int big = 10000;\n" 16669 "}", 16670 Alignment); 16671 verifyFormat("class C {\n" 16672 "public:\n" 16673 " int i = 1;\n" 16674 " virtual void f() = 0;\n" 16675 "};", 16676 Alignment); 16677 verifyFormat("float i = 1;\n" 16678 "if (SomeType t = getSomething()) {\n" 16679 "}\n" 16680 "const unsigned j = 2;\n" 16681 "int big = 10000;", 16682 Alignment); 16683 verifyFormat("float j = 7;\n" 16684 "for (int k = 0; k < N; ++k) {\n" 16685 "}\n" 16686 "unsigned j = 2;\n" 16687 "int big = 10000;\n" 16688 "}", 16689 Alignment); 16690 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 16691 verifyFormat("float i = 1;\n" 16692 "LooooooooooongType loooooooooooooooooooooongVariable\n" 16693 " = someLooooooooooooooooongFunction();\n" 16694 "int j = 2;", 16695 Alignment); 16696 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 16697 verifyFormat("int i = 1;\n" 16698 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 16699 " someLooooooooooooooooongFunction();\n" 16700 "int j = 2;", 16701 Alignment); 16702 16703 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16704 verifyFormat("auto lambda = []() {\n" 16705 " auto ii = 0;\n" 16706 " float j = 0;\n" 16707 " return 0;\n" 16708 "};\n" 16709 "int i = 0;\n" 16710 "float i2 = 0;\n" 16711 "auto v = type{\n" 16712 " i = 1, //\n" 16713 " (i = 2), //\n" 16714 " i = 3 //\n" 16715 "};", 16716 Alignment); 16717 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16718 16719 verifyFormat( 16720 "int i = 1;\n" 16721 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 16722 " loooooooooooooooooooooongParameterB);\n" 16723 "int j = 2;", 16724 Alignment); 16725 16726 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 16727 // We expect declarations and assignments to align, as long as it doesn't 16728 // exceed the column limit, starting a new alignment sequence whenever it 16729 // happens. 16730 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16731 Alignment.ColumnLimit = 30; 16732 verifyFormat("float ii = 1;\n" 16733 "unsigned j = 2;\n" 16734 "int someVerylongVariable = 1;\n" 16735 "AnotherLongType ll = 123456;\n" 16736 "VeryVeryLongType k = 2;\n" 16737 "int myvar = 1;", 16738 Alignment); 16739 Alignment.ColumnLimit = 80; 16740 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16741 16742 verifyFormat( 16743 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 16744 " typename LongType, typename B>\n" 16745 "auto foo() {}\n", 16746 Alignment); 16747 verifyFormat("float a, b = 1;\n" 16748 "int c = 2;\n" 16749 "int dd = 3;\n", 16750 Alignment); 16751 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 16752 "float b[1][] = {{3.f}};\n", 16753 Alignment); 16754 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16755 verifyFormat("float a, b = 1;\n" 16756 "int c = 2;\n" 16757 "int dd = 3;\n", 16758 Alignment); 16759 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 16760 "float b[1][] = {{3.f}};\n", 16761 Alignment); 16762 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16763 16764 Alignment.ColumnLimit = 30; 16765 Alignment.BinPackParameters = false; 16766 verifyFormat("void foo(float a,\n" 16767 " float b,\n" 16768 " int c,\n" 16769 " uint32_t *d) {\n" 16770 " int *e = 0;\n" 16771 " float f = 0;\n" 16772 " double g = 0;\n" 16773 "}\n" 16774 "void bar(ino_t a,\n" 16775 " int b,\n" 16776 " uint32_t *c,\n" 16777 " bool d) {}\n", 16778 Alignment); 16779 Alignment.BinPackParameters = true; 16780 Alignment.ColumnLimit = 80; 16781 16782 // Bug 33507 16783 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 16784 verifyFormat( 16785 "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n" 16786 " static const Version verVs2017;\n" 16787 " return true;\n" 16788 "});\n", 16789 Alignment); 16790 Alignment.PointerAlignment = FormatStyle::PAS_Right; 16791 16792 // See llvm.org/PR35641 16793 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16794 verifyFormat("int func() { //\n" 16795 " int b;\n" 16796 " unsigned c;\n" 16797 "}", 16798 Alignment); 16799 16800 // See PR37175 16801 FormatStyle Style = getMozillaStyle(); 16802 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16803 EXPECT_EQ("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n" 16804 "foo(int a);", 16805 format("DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style)); 16806 16807 Alignment.PointerAlignment = FormatStyle::PAS_Left; 16808 verifyFormat("unsigned int* a;\n" 16809 "int* b;\n" 16810 "unsigned int Const* c;\n" 16811 "unsigned int const* d;\n" 16812 "unsigned int Const& e;\n" 16813 "unsigned int const& f;", 16814 Alignment); 16815 verifyFormat("Const unsigned int* c;\n" 16816 "const unsigned int* d;\n" 16817 "Const unsigned int& e;\n" 16818 "const unsigned int& f;\n" 16819 "const unsigned g;\n" 16820 "Const unsigned h;", 16821 Alignment); 16822 16823 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 16824 verifyFormat("unsigned int * a;\n" 16825 "int * b;\n" 16826 "unsigned int Const * c;\n" 16827 "unsigned int const * d;\n" 16828 "unsigned int Const & e;\n" 16829 "unsigned int const & f;", 16830 Alignment); 16831 verifyFormat("Const unsigned int * c;\n" 16832 "const unsigned int * d;\n" 16833 "Const unsigned int & e;\n" 16834 "const unsigned int & f;\n" 16835 "const unsigned g;\n" 16836 "Const unsigned h;", 16837 Alignment); 16838 } 16839 16840 TEST_F(FormatTest, AlignWithLineBreaks) { 16841 auto Style = getLLVMStyleWithColumns(120); 16842 16843 EXPECT_EQ(Style.AlignConsecutiveAssignments, FormatStyle::ACS_None); 16844 EXPECT_EQ(Style.AlignConsecutiveDeclarations, FormatStyle::ACS_None); 16845 verifyFormat("void foo() {\n" 16846 " int myVar = 5;\n" 16847 " double x = 3.14;\n" 16848 " auto str = \"Hello \"\n" 16849 " \"World\";\n" 16850 " auto s = \"Hello \"\n" 16851 " \"Again\";\n" 16852 "}", 16853 Style); 16854 16855 // clang-format off 16856 verifyFormat("void foo() {\n" 16857 " const int capacityBefore = Entries.capacity();\n" 16858 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16859 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16860 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16861 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16862 "}", 16863 Style); 16864 // clang-format on 16865 16866 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16867 verifyFormat("void foo() {\n" 16868 " int myVar = 5;\n" 16869 " double x = 3.14;\n" 16870 " auto str = \"Hello \"\n" 16871 " \"World\";\n" 16872 " auto s = \"Hello \"\n" 16873 " \"Again\";\n" 16874 "}", 16875 Style); 16876 16877 // clang-format off 16878 verifyFormat("void foo() {\n" 16879 " const int capacityBefore = Entries.capacity();\n" 16880 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16881 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16882 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16883 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16884 "}", 16885 Style); 16886 // clang-format on 16887 16888 Style.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16889 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16890 verifyFormat("void foo() {\n" 16891 " int myVar = 5;\n" 16892 " double x = 3.14;\n" 16893 " auto str = \"Hello \"\n" 16894 " \"World\";\n" 16895 " auto s = \"Hello \"\n" 16896 " \"Again\";\n" 16897 "}", 16898 Style); 16899 16900 // clang-format off 16901 verifyFormat("void foo() {\n" 16902 " const int capacityBefore = Entries.capacity();\n" 16903 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16904 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16905 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16906 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16907 "}", 16908 Style); 16909 // clang-format on 16910 16911 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16912 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16913 16914 verifyFormat("void foo() {\n" 16915 " int myVar = 5;\n" 16916 " double x = 3.14;\n" 16917 " auto str = \"Hello \"\n" 16918 " \"World\";\n" 16919 " auto s = \"Hello \"\n" 16920 " \"Again\";\n" 16921 "}", 16922 Style); 16923 16924 // clang-format off 16925 verifyFormat("void foo() {\n" 16926 " const int capacityBefore = Entries.capacity();\n" 16927 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16928 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16929 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 16930 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 16931 "}", 16932 Style); 16933 // clang-format on 16934 16935 Style = getLLVMStyleWithColumns(120); 16936 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16937 Style.ContinuationIndentWidth = 4; 16938 Style.IndentWidth = 4; 16939 16940 // clang-format off 16941 verifyFormat("void SomeFunc() {\n" 16942 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 16943 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16944 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 16945 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16946 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec),\n" 16947 " seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16948 "}", 16949 Style); 16950 // clang-format on 16951 16952 Style.BinPackArguments = false; 16953 16954 // clang-format off 16955 verifyFormat("void SomeFunc() {\n" 16956 " newWatcher.maxAgeUsec = ToLegacyTimestamp(GetMaxAge(\n" 16957 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16958 " newWatcher.maxAge = ToLegacyTimestamp(GetMaxAge(\n" 16959 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16960 " newWatcher.max = ToLegacyTimestamp(GetMaxAge(\n" 16961 " FromLegacyTimestamp<milliseconds>(monitorFrequencyUsec), seconds(std::uint64_t(maxSampleAge)), maxKeepSamples));\n" 16962 "}", 16963 Style); 16964 // clang-format on 16965 } 16966 16967 TEST_F(FormatTest, AlignWithInitializerPeriods) { 16968 auto Style = getLLVMStyleWithColumns(60); 16969 16970 verifyFormat("void foo1(void) {\n" 16971 " BYTE p[1] = 1;\n" 16972 " A B = {.one_foooooooooooooooo = 2,\n" 16973 " .two_fooooooooooooo = 3,\n" 16974 " .three_fooooooooooooo = 4};\n" 16975 " BYTE payload = 2;\n" 16976 "}", 16977 Style); 16978 16979 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 16980 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_None; 16981 verifyFormat("void foo2(void) {\n" 16982 " BYTE p[1] = 1;\n" 16983 " A B = {.one_foooooooooooooooo = 2,\n" 16984 " .two_fooooooooooooo = 3,\n" 16985 " .three_fooooooooooooo = 4};\n" 16986 " BYTE payload = 2;\n" 16987 "}", 16988 Style); 16989 16990 Style.AlignConsecutiveAssignments = FormatStyle::ACS_None; 16991 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 16992 verifyFormat("void foo3(void) {\n" 16993 " BYTE p[1] = 1;\n" 16994 " A B = {.one_foooooooooooooooo = 2,\n" 16995 " .two_fooooooooooooo = 3,\n" 16996 " .three_fooooooooooooo = 4};\n" 16997 " BYTE payload = 2;\n" 16998 "}", 16999 Style); 17000 17001 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 17002 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 17003 verifyFormat("void foo4(void) {\n" 17004 " BYTE p[1] = 1;\n" 17005 " A B = {.one_foooooooooooooooo = 2,\n" 17006 " .two_fooooooooooooo = 3,\n" 17007 " .three_fooooooooooooo = 4};\n" 17008 " BYTE payload = 2;\n" 17009 "}", 17010 Style); 17011 } 17012 17013 TEST_F(FormatTest, LinuxBraceBreaking) { 17014 FormatStyle LinuxBraceStyle = getLLVMStyle(); 17015 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 17016 verifyFormat("namespace a\n" 17017 "{\n" 17018 "class A\n" 17019 "{\n" 17020 " void f()\n" 17021 " {\n" 17022 " if (true) {\n" 17023 " a();\n" 17024 " b();\n" 17025 " } else {\n" 17026 " a();\n" 17027 " }\n" 17028 " }\n" 17029 " void g() { return; }\n" 17030 "};\n" 17031 "struct B {\n" 17032 " int x;\n" 17033 "};\n" 17034 "} // namespace a\n", 17035 LinuxBraceStyle); 17036 verifyFormat("enum X {\n" 17037 " Y = 0,\n" 17038 "}\n", 17039 LinuxBraceStyle); 17040 verifyFormat("struct S {\n" 17041 " int Type;\n" 17042 " union {\n" 17043 " int x;\n" 17044 " double y;\n" 17045 " } Value;\n" 17046 " class C\n" 17047 " {\n" 17048 " MyFavoriteType Value;\n" 17049 " } Class;\n" 17050 "}\n", 17051 LinuxBraceStyle); 17052 } 17053 17054 TEST_F(FormatTest, MozillaBraceBreaking) { 17055 FormatStyle MozillaBraceStyle = getLLVMStyle(); 17056 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 17057 MozillaBraceStyle.FixNamespaceComments = false; 17058 verifyFormat("namespace a {\n" 17059 "class A\n" 17060 "{\n" 17061 " void f()\n" 17062 " {\n" 17063 " if (true) {\n" 17064 " a();\n" 17065 " b();\n" 17066 " }\n" 17067 " }\n" 17068 " void g() { return; }\n" 17069 "};\n" 17070 "enum E\n" 17071 "{\n" 17072 " A,\n" 17073 " // foo\n" 17074 " B,\n" 17075 " C\n" 17076 "};\n" 17077 "struct B\n" 17078 "{\n" 17079 " int x;\n" 17080 "};\n" 17081 "}\n", 17082 MozillaBraceStyle); 17083 verifyFormat("struct S\n" 17084 "{\n" 17085 " int Type;\n" 17086 " union\n" 17087 " {\n" 17088 " int x;\n" 17089 " double y;\n" 17090 " } Value;\n" 17091 " class C\n" 17092 " {\n" 17093 " MyFavoriteType Value;\n" 17094 " } Class;\n" 17095 "}\n", 17096 MozillaBraceStyle); 17097 } 17098 17099 TEST_F(FormatTest, StroustrupBraceBreaking) { 17100 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 17101 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 17102 verifyFormat("namespace a {\n" 17103 "class A {\n" 17104 " void f()\n" 17105 " {\n" 17106 " if (true) {\n" 17107 " a();\n" 17108 " b();\n" 17109 " }\n" 17110 " }\n" 17111 " void g() { return; }\n" 17112 "};\n" 17113 "struct B {\n" 17114 " int x;\n" 17115 "};\n" 17116 "} // namespace a\n", 17117 StroustrupBraceStyle); 17118 17119 verifyFormat("void foo()\n" 17120 "{\n" 17121 " if (a) {\n" 17122 " a();\n" 17123 " }\n" 17124 " else {\n" 17125 " b();\n" 17126 " }\n" 17127 "}\n", 17128 StroustrupBraceStyle); 17129 17130 verifyFormat("#ifdef _DEBUG\n" 17131 "int foo(int i = 0)\n" 17132 "#else\n" 17133 "int foo(int i = 5)\n" 17134 "#endif\n" 17135 "{\n" 17136 " return i;\n" 17137 "}", 17138 StroustrupBraceStyle); 17139 17140 verifyFormat("void foo() {}\n" 17141 "void bar()\n" 17142 "#ifdef _DEBUG\n" 17143 "{\n" 17144 " foo();\n" 17145 "}\n" 17146 "#else\n" 17147 "{\n" 17148 "}\n" 17149 "#endif", 17150 StroustrupBraceStyle); 17151 17152 verifyFormat("void foobar() { int i = 5; }\n" 17153 "#ifdef _DEBUG\n" 17154 "void bar() {}\n" 17155 "#else\n" 17156 "void bar() { foobar(); }\n" 17157 "#endif", 17158 StroustrupBraceStyle); 17159 } 17160 17161 TEST_F(FormatTest, AllmanBraceBreaking) { 17162 FormatStyle AllmanBraceStyle = getLLVMStyle(); 17163 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 17164 17165 EXPECT_EQ("namespace a\n" 17166 "{\n" 17167 "void f();\n" 17168 "void g();\n" 17169 "} // namespace a\n", 17170 format("namespace a\n" 17171 "{\n" 17172 "void f();\n" 17173 "void g();\n" 17174 "}\n", 17175 AllmanBraceStyle)); 17176 17177 verifyFormat("namespace a\n" 17178 "{\n" 17179 "class A\n" 17180 "{\n" 17181 " void f()\n" 17182 " {\n" 17183 " if (true)\n" 17184 " {\n" 17185 " a();\n" 17186 " b();\n" 17187 " }\n" 17188 " }\n" 17189 " void g() { return; }\n" 17190 "};\n" 17191 "struct B\n" 17192 "{\n" 17193 " int x;\n" 17194 "};\n" 17195 "union C\n" 17196 "{\n" 17197 "};\n" 17198 "} // namespace a", 17199 AllmanBraceStyle); 17200 17201 verifyFormat("void f()\n" 17202 "{\n" 17203 " if (true)\n" 17204 " {\n" 17205 " a();\n" 17206 " }\n" 17207 " else if (false)\n" 17208 " {\n" 17209 " b();\n" 17210 " }\n" 17211 " else\n" 17212 " {\n" 17213 " c();\n" 17214 " }\n" 17215 "}\n", 17216 AllmanBraceStyle); 17217 17218 verifyFormat("void f()\n" 17219 "{\n" 17220 " for (int i = 0; i < 10; ++i)\n" 17221 " {\n" 17222 " a();\n" 17223 " }\n" 17224 " while (false)\n" 17225 " {\n" 17226 " b();\n" 17227 " }\n" 17228 " do\n" 17229 " {\n" 17230 " c();\n" 17231 " } while (false)\n" 17232 "}\n", 17233 AllmanBraceStyle); 17234 17235 verifyFormat("void f(int a)\n" 17236 "{\n" 17237 " switch (a)\n" 17238 " {\n" 17239 " case 0:\n" 17240 " break;\n" 17241 " case 1:\n" 17242 " {\n" 17243 " break;\n" 17244 " }\n" 17245 " case 2:\n" 17246 " {\n" 17247 " }\n" 17248 " break;\n" 17249 " default:\n" 17250 " break;\n" 17251 " }\n" 17252 "}\n", 17253 AllmanBraceStyle); 17254 17255 verifyFormat("enum X\n" 17256 "{\n" 17257 " Y = 0,\n" 17258 "}\n", 17259 AllmanBraceStyle); 17260 verifyFormat("enum X\n" 17261 "{\n" 17262 " Y = 0\n" 17263 "}\n", 17264 AllmanBraceStyle); 17265 17266 verifyFormat("@interface BSApplicationController ()\n" 17267 "{\n" 17268 "@private\n" 17269 " id _extraIvar;\n" 17270 "}\n" 17271 "@end\n", 17272 AllmanBraceStyle); 17273 17274 verifyFormat("#ifdef _DEBUG\n" 17275 "int foo(int i = 0)\n" 17276 "#else\n" 17277 "int foo(int i = 5)\n" 17278 "#endif\n" 17279 "{\n" 17280 " return i;\n" 17281 "}", 17282 AllmanBraceStyle); 17283 17284 verifyFormat("void foo() {}\n" 17285 "void bar()\n" 17286 "#ifdef _DEBUG\n" 17287 "{\n" 17288 " foo();\n" 17289 "}\n" 17290 "#else\n" 17291 "{\n" 17292 "}\n" 17293 "#endif", 17294 AllmanBraceStyle); 17295 17296 verifyFormat("void foobar() { int i = 5; }\n" 17297 "#ifdef _DEBUG\n" 17298 "void bar() {}\n" 17299 "#else\n" 17300 "void bar() { foobar(); }\n" 17301 "#endif", 17302 AllmanBraceStyle); 17303 17304 EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine, 17305 FormatStyle::SLS_All); 17306 17307 verifyFormat("[](int i) { return i + 2; };\n" 17308 "[](int i, int j)\n" 17309 "{\n" 17310 " auto x = i + j;\n" 17311 " auto y = i * j;\n" 17312 " return x ^ y;\n" 17313 "};\n" 17314 "void foo()\n" 17315 "{\n" 17316 " auto shortLambda = [](int i) { return i + 2; };\n" 17317 " auto longLambda = [](int i, int j)\n" 17318 " {\n" 17319 " auto x = i + j;\n" 17320 " auto y = i * j;\n" 17321 " return x ^ y;\n" 17322 " };\n" 17323 "}", 17324 AllmanBraceStyle); 17325 17326 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 17327 17328 verifyFormat("[](int i)\n" 17329 "{\n" 17330 " return i + 2;\n" 17331 "};\n" 17332 "[](int i, int j)\n" 17333 "{\n" 17334 " auto x = i + j;\n" 17335 " auto y = i * j;\n" 17336 " return x ^ y;\n" 17337 "};\n" 17338 "void foo()\n" 17339 "{\n" 17340 " auto shortLambda = [](int i)\n" 17341 " {\n" 17342 " return i + 2;\n" 17343 " };\n" 17344 " auto longLambda = [](int i, int j)\n" 17345 " {\n" 17346 " auto x = i + j;\n" 17347 " auto y = i * j;\n" 17348 " return x ^ y;\n" 17349 " };\n" 17350 "}", 17351 AllmanBraceStyle); 17352 17353 // Reset 17354 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All; 17355 17356 // This shouldn't affect ObjC blocks.. 17357 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 17358 " // ...\n" 17359 " int i;\n" 17360 "}];", 17361 AllmanBraceStyle); 17362 verifyFormat("void (^block)(void) = ^{\n" 17363 " // ...\n" 17364 " int i;\n" 17365 "};", 17366 AllmanBraceStyle); 17367 // .. or dict literals. 17368 verifyFormat("void f()\n" 17369 "{\n" 17370 " // ...\n" 17371 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 17372 "}", 17373 AllmanBraceStyle); 17374 verifyFormat("void f()\n" 17375 "{\n" 17376 " // ...\n" 17377 " [object someMethod:@{a : @\"b\"}];\n" 17378 "}", 17379 AllmanBraceStyle); 17380 verifyFormat("int f()\n" 17381 "{ // comment\n" 17382 " return 42;\n" 17383 "}", 17384 AllmanBraceStyle); 17385 17386 AllmanBraceStyle.ColumnLimit = 19; 17387 verifyFormat("void f() { int i; }", AllmanBraceStyle); 17388 AllmanBraceStyle.ColumnLimit = 18; 17389 verifyFormat("void f()\n" 17390 "{\n" 17391 " int i;\n" 17392 "}", 17393 AllmanBraceStyle); 17394 AllmanBraceStyle.ColumnLimit = 80; 17395 17396 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 17397 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = 17398 FormatStyle::SIS_WithoutElse; 17399 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 17400 verifyFormat("void f(bool b)\n" 17401 "{\n" 17402 " if (b)\n" 17403 " {\n" 17404 " return;\n" 17405 " }\n" 17406 "}\n", 17407 BreakBeforeBraceShortIfs); 17408 verifyFormat("void f(bool b)\n" 17409 "{\n" 17410 " if constexpr (b)\n" 17411 " {\n" 17412 " return;\n" 17413 " }\n" 17414 "}\n", 17415 BreakBeforeBraceShortIfs); 17416 verifyFormat("void f(bool b)\n" 17417 "{\n" 17418 " if CONSTEXPR (b)\n" 17419 " {\n" 17420 " return;\n" 17421 " }\n" 17422 "}\n", 17423 BreakBeforeBraceShortIfs); 17424 verifyFormat("void f(bool b)\n" 17425 "{\n" 17426 " if (b) return;\n" 17427 "}\n", 17428 BreakBeforeBraceShortIfs); 17429 verifyFormat("void f(bool b)\n" 17430 "{\n" 17431 " if constexpr (b) return;\n" 17432 "}\n", 17433 BreakBeforeBraceShortIfs); 17434 verifyFormat("void f(bool b)\n" 17435 "{\n" 17436 " if CONSTEXPR (b) return;\n" 17437 "}\n", 17438 BreakBeforeBraceShortIfs); 17439 verifyFormat("void f(bool b)\n" 17440 "{\n" 17441 " while (b)\n" 17442 " {\n" 17443 " return;\n" 17444 " }\n" 17445 "}\n", 17446 BreakBeforeBraceShortIfs); 17447 } 17448 17449 TEST_F(FormatTest, WhitesmithsBraceBreaking) { 17450 FormatStyle WhitesmithsBraceStyle = getLLVMStyleWithColumns(0); 17451 WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 17452 17453 // Make a few changes to the style for testing purposes 17454 WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine = 17455 FormatStyle::SFS_Empty; 17456 WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 17457 17458 // FIXME: this test case can't decide whether there should be a blank line 17459 // after the ~D() line or not. It adds one if one doesn't exist in the test 17460 // and it removes the line if one exists. 17461 /* 17462 verifyFormat("class A;\n" 17463 "namespace B\n" 17464 " {\n" 17465 "class C;\n" 17466 "// Comment\n" 17467 "class D\n" 17468 " {\n" 17469 "public:\n" 17470 " D();\n" 17471 " ~D() {}\n" 17472 "private:\n" 17473 " enum E\n" 17474 " {\n" 17475 " F\n" 17476 " }\n" 17477 " };\n" 17478 " } // namespace B\n", 17479 WhitesmithsBraceStyle); 17480 */ 17481 17482 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None; 17483 verifyFormat("namespace a\n" 17484 " {\n" 17485 "class A\n" 17486 " {\n" 17487 " void f()\n" 17488 " {\n" 17489 " if (true)\n" 17490 " {\n" 17491 " a();\n" 17492 " b();\n" 17493 " }\n" 17494 " }\n" 17495 " void g()\n" 17496 " {\n" 17497 " return;\n" 17498 " }\n" 17499 " };\n" 17500 "struct B\n" 17501 " {\n" 17502 " int x;\n" 17503 " };\n" 17504 " } // namespace a", 17505 WhitesmithsBraceStyle); 17506 17507 verifyFormat("namespace a\n" 17508 " {\n" 17509 "namespace b\n" 17510 " {\n" 17511 "class A\n" 17512 " {\n" 17513 " void f()\n" 17514 " {\n" 17515 " if (true)\n" 17516 " {\n" 17517 " a();\n" 17518 " b();\n" 17519 " }\n" 17520 " }\n" 17521 " void g()\n" 17522 " {\n" 17523 " return;\n" 17524 " }\n" 17525 " };\n" 17526 "struct B\n" 17527 " {\n" 17528 " int x;\n" 17529 " };\n" 17530 " } // namespace b\n" 17531 " } // namespace a", 17532 WhitesmithsBraceStyle); 17533 17534 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner; 17535 verifyFormat("namespace a\n" 17536 " {\n" 17537 "namespace b\n" 17538 " {\n" 17539 " class A\n" 17540 " {\n" 17541 " void f()\n" 17542 " {\n" 17543 " if (true)\n" 17544 " {\n" 17545 " a();\n" 17546 " b();\n" 17547 " }\n" 17548 " }\n" 17549 " void g()\n" 17550 " {\n" 17551 " return;\n" 17552 " }\n" 17553 " };\n" 17554 " struct B\n" 17555 " {\n" 17556 " int x;\n" 17557 " };\n" 17558 " } // namespace b\n" 17559 " } // namespace a", 17560 WhitesmithsBraceStyle); 17561 17562 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All; 17563 verifyFormat("namespace a\n" 17564 " {\n" 17565 " namespace b\n" 17566 " {\n" 17567 " class A\n" 17568 " {\n" 17569 " void f()\n" 17570 " {\n" 17571 " if (true)\n" 17572 " {\n" 17573 " a();\n" 17574 " b();\n" 17575 " }\n" 17576 " }\n" 17577 " void g()\n" 17578 " {\n" 17579 " return;\n" 17580 " }\n" 17581 " };\n" 17582 " struct B\n" 17583 " {\n" 17584 " int x;\n" 17585 " };\n" 17586 " } // namespace b\n" 17587 " } // namespace a", 17588 WhitesmithsBraceStyle); 17589 17590 verifyFormat("void f()\n" 17591 " {\n" 17592 " if (true)\n" 17593 " {\n" 17594 " a();\n" 17595 " }\n" 17596 " else if (false)\n" 17597 " {\n" 17598 " b();\n" 17599 " }\n" 17600 " else\n" 17601 " {\n" 17602 " c();\n" 17603 " }\n" 17604 " }\n", 17605 WhitesmithsBraceStyle); 17606 17607 verifyFormat("void f()\n" 17608 " {\n" 17609 " for (int i = 0; i < 10; ++i)\n" 17610 " {\n" 17611 " a();\n" 17612 " }\n" 17613 " while (false)\n" 17614 " {\n" 17615 " b();\n" 17616 " }\n" 17617 " do\n" 17618 " {\n" 17619 " c();\n" 17620 " } while (false)\n" 17621 " }\n", 17622 WhitesmithsBraceStyle); 17623 17624 WhitesmithsBraceStyle.IndentCaseLabels = true; 17625 verifyFormat("void switchTest1(int a)\n" 17626 " {\n" 17627 " switch (a)\n" 17628 " {\n" 17629 " case 2:\n" 17630 " {\n" 17631 " }\n" 17632 " break;\n" 17633 " }\n" 17634 " }\n", 17635 WhitesmithsBraceStyle); 17636 17637 verifyFormat("void switchTest2(int a)\n" 17638 " {\n" 17639 " switch (a)\n" 17640 " {\n" 17641 " case 0:\n" 17642 " break;\n" 17643 " case 1:\n" 17644 " {\n" 17645 " break;\n" 17646 " }\n" 17647 " case 2:\n" 17648 " {\n" 17649 " }\n" 17650 " break;\n" 17651 " default:\n" 17652 " break;\n" 17653 " }\n" 17654 " }\n", 17655 WhitesmithsBraceStyle); 17656 17657 verifyFormat("void switchTest3(int a)\n" 17658 " {\n" 17659 " switch (a)\n" 17660 " {\n" 17661 " case 0:\n" 17662 " {\n" 17663 " foo(x);\n" 17664 " }\n" 17665 " break;\n" 17666 " default:\n" 17667 " {\n" 17668 " foo(1);\n" 17669 " }\n" 17670 " break;\n" 17671 " }\n" 17672 " }\n", 17673 WhitesmithsBraceStyle); 17674 17675 WhitesmithsBraceStyle.IndentCaseLabels = false; 17676 17677 verifyFormat("void switchTest4(int a)\n" 17678 " {\n" 17679 " switch (a)\n" 17680 " {\n" 17681 " case 2:\n" 17682 " {\n" 17683 " }\n" 17684 " break;\n" 17685 " }\n" 17686 " }\n", 17687 WhitesmithsBraceStyle); 17688 17689 verifyFormat("void switchTest5(int a)\n" 17690 " {\n" 17691 " switch (a)\n" 17692 " {\n" 17693 " case 0:\n" 17694 " break;\n" 17695 " case 1:\n" 17696 " {\n" 17697 " foo();\n" 17698 " break;\n" 17699 " }\n" 17700 " case 2:\n" 17701 " {\n" 17702 " }\n" 17703 " break;\n" 17704 " default:\n" 17705 " break;\n" 17706 " }\n" 17707 " }\n", 17708 WhitesmithsBraceStyle); 17709 17710 verifyFormat("void switchTest6(int a)\n" 17711 " {\n" 17712 " switch (a)\n" 17713 " {\n" 17714 " case 0:\n" 17715 " {\n" 17716 " foo(x);\n" 17717 " }\n" 17718 " break;\n" 17719 " default:\n" 17720 " {\n" 17721 " foo(1);\n" 17722 " }\n" 17723 " break;\n" 17724 " }\n" 17725 " }\n", 17726 WhitesmithsBraceStyle); 17727 17728 verifyFormat("enum X\n" 17729 " {\n" 17730 " Y = 0, // testing\n" 17731 " }\n", 17732 WhitesmithsBraceStyle); 17733 17734 verifyFormat("enum X\n" 17735 " {\n" 17736 " Y = 0\n" 17737 " }\n", 17738 WhitesmithsBraceStyle); 17739 verifyFormat("enum X\n" 17740 " {\n" 17741 " Y = 0,\n" 17742 " Z = 1\n" 17743 " };\n", 17744 WhitesmithsBraceStyle); 17745 17746 verifyFormat("@interface BSApplicationController ()\n" 17747 " {\n" 17748 "@private\n" 17749 " id _extraIvar;\n" 17750 " }\n" 17751 "@end\n", 17752 WhitesmithsBraceStyle); 17753 17754 verifyFormat("#ifdef _DEBUG\n" 17755 "int foo(int i = 0)\n" 17756 "#else\n" 17757 "int foo(int i = 5)\n" 17758 "#endif\n" 17759 " {\n" 17760 " return i;\n" 17761 " }", 17762 WhitesmithsBraceStyle); 17763 17764 verifyFormat("void foo() {}\n" 17765 "void bar()\n" 17766 "#ifdef _DEBUG\n" 17767 " {\n" 17768 " foo();\n" 17769 " }\n" 17770 "#else\n" 17771 " {\n" 17772 " }\n" 17773 "#endif", 17774 WhitesmithsBraceStyle); 17775 17776 verifyFormat("void foobar()\n" 17777 " {\n" 17778 " int i = 5;\n" 17779 " }\n" 17780 "#ifdef _DEBUG\n" 17781 "void bar()\n" 17782 " {\n" 17783 " }\n" 17784 "#else\n" 17785 "void bar()\n" 17786 " {\n" 17787 " foobar();\n" 17788 " }\n" 17789 "#endif", 17790 WhitesmithsBraceStyle); 17791 17792 // This shouldn't affect ObjC blocks.. 17793 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 17794 " // ...\n" 17795 " int i;\n" 17796 "}];", 17797 WhitesmithsBraceStyle); 17798 verifyFormat("void (^block)(void) = ^{\n" 17799 " // ...\n" 17800 " int i;\n" 17801 "};", 17802 WhitesmithsBraceStyle); 17803 // .. or dict literals. 17804 verifyFormat("void f()\n" 17805 " {\n" 17806 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 17807 " }", 17808 WhitesmithsBraceStyle); 17809 17810 verifyFormat("int f()\n" 17811 " { // comment\n" 17812 " return 42;\n" 17813 " }", 17814 WhitesmithsBraceStyle); 17815 17816 FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle; 17817 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = 17818 FormatStyle::SIS_OnlyFirstIf; 17819 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 17820 verifyFormat("void f(bool b)\n" 17821 " {\n" 17822 " if (b)\n" 17823 " {\n" 17824 " return;\n" 17825 " }\n" 17826 " }\n", 17827 BreakBeforeBraceShortIfs); 17828 verifyFormat("void f(bool b)\n" 17829 " {\n" 17830 " if (b) return;\n" 17831 " }\n", 17832 BreakBeforeBraceShortIfs); 17833 verifyFormat("void f(bool b)\n" 17834 " {\n" 17835 " while (b)\n" 17836 " {\n" 17837 " return;\n" 17838 " }\n" 17839 " }\n", 17840 BreakBeforeBraceShortIfs); 17841 } 17842 17843 TEST_F(FormatTest, GNUBraceBreaking) { 17844 FormatStyle GNUBraceStyle = getLLVMStyle(); 17845 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 17846 verifyFormat("namespace a\n" 17847 "{\n" 17848 "class A\n" 17849 "{\n" 17850 " void f()\n" 17851 " {\n" 17852 " int a;\n" 17853 " {\n" 17854 " int b;\n" 17855 " }\n" 17856 " if (true)\n" 17857 " {\n" 17858 " a();\n" 17859 " b();\n" 17860 " }\n" 17861 " }\n" 17862 " void g() { return; }\n" 17863 "}\n" 17864 "} // namespace a", 17865 GNUBraceStyle); 17866 17867 verifyFormat("void f()\n" 17868 "{\n" 17869 " if (true)\n" 17870 " {\n" 17871 " a();\n" 17872 " }\n" 17873 " else if (false)\n" 17874 " {\n" 17875 " b();\n" 17876 " }\n" 17877 " else\n" 17878 " {\n" 17879 " c();\n" 17880 " }\n" 17881 "}\n", 17882 GNUBraceStyle); 17883 17884 verifyFormat("void f()\n" 17885 "{\n" 17886 " for (int i = 0; i < 10; ++i)\n" 17887 " {\n" 17888 " a();\n" 17889 " }\n" 17890 " while (false)\n" 17891 " {\n" 17892 " b();\n" 17893 " }\n" 17894 " do\n" 17895 " {\n" 17896 " c();\n" 17897 " }\n" 17898 " while (false);\n" 17899 "}\n", 17900 GNUBraceStyle); 17901 17902 verifyFormat("void f(int a)\n" 17903 "{\n" 17904 " switch (a)\n" 17905 " {\n" 17906 " case 0:\n" 17907 " break;\n" 17908 " case 1:\n" 17909 " {\n" 17910 " break;\n" 17911 " }\n" 17912 " case 2:\n" 17913 " {\n" 17914 " }\n" 17915 " break;\n" 17916 " default:\n" 17917 " break;\n" 17918 " }\n" 17919 "}\n", 17920 GNUBraceStyle); 17921 17922 verifyFormat("enum X\n" 17923 "{\n" 17924 " Y = 0,\n" 17925 "}\n", 17926 GNUBraceStyle); 17927 17928 verifyFormat("@interface BSApplicationController ()\n" 17929 "{\n" 17930 "@private\n" 17931 " id _extraIvar;\n" 17932 "}\n" 17933 "@end\n", 17934 GNUBraceStyle); 17935 17936 verifyFormat("#ifdef _DEBUG\n" 17937 "int foo(int i = 0)\n" 17938 "#else\n" 17939 "int foo(int i = 5)\n" 17940 "#endif\n" 17941 "{\n" 17942 " return i;\n" 17943 "}", 17944 GNUBraceStyle); 17945 17946 verifyFormat("void foo() {}\n" 17947 "void bar()\n" 17948 "#ifdef _DEBUG\n" 17949 "{\n" 17950 " foo();\n" 17951 "}\n" 17952 "#else\n" 17953 "{\n" 17954 "}\n" 17955 "#endif", 17956 GNUBraceStyle); 17957 17958 verifyFormat("void foobar() { int i = 5; }\n" 17959 "#ifdef _DEBUG\n" 17960 "void bar() {}\n" 17961 "#else\n" 17962 "void bar() { foobar(); }\n" 17963 "#endif", 17964 GNUBraceStyle); 17965 } 17966 17967 TEST_F(FormatTest, WebKitBraceBreaking) { 17968 FormatStyle WebKitBraceStyle = getLLVMStyle(); 17969 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 17970 WebKitBraceStyle.FixNamespaceComments = false; 17971 verifyFormat("namespace a {\n" 17972 "class A {\n" 17973 " void f()\n" 17974 " {\n" 17975 " if (true) {\n" 17976 " a();\n" 17977 " b();\n" 17978 " }\n" 17979 " }\n" 17980 " void g() { return; }\n" 17981 "};\n" 17982 "enum E {\n" 17983 " A,\n" 17984 " // foo\n" 17985 " B,\n" 17986 " C\n" 17987 "};\n" 17988 "struct B {\n" 17989 " int x;\n" 17990 "};\n" 17991 "}\n", 17992 WebKitBraceStyle); 17993 verifyFormat("struct S {\n" 17994 " int Type;\n" 17995 " union {\n" 17996 " int x;\n" 17997 " double y;\n" 17998 " } Value;\n" 17999 " class C {\n" 18000 " MyFavoriteType Value;\n" 18001 " } Class;\n" 18002 "};\n", 18003 WebKitBraceStyle); 18004 } 18005 18006 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 18007 verifyFormat("void f() {\n" 18008 " try {\n" 18009 " } catch (const Exception &e) {\n" 18010 " }\n" 18011 "}\n", 18012 getLLVMStyle()); 18013 } 18014 18015 TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) { 18016 auto Style = getLLVMStyle(); 18017 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 18018 Style.AlignConsecutiveAssignments = 18019 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 18020 Style.AlignConsecutiveDeclarations = 18021 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 18022 verifyFormat("struct test demo[] = {\n" 18023 " {56, 23, \"hello\"},\n" 18024 " {-1, 93463, \"world\"},\n" 18025 " { 7, 5, \"!!\"}\n" 18026 "};\n", 18027 Style); 18028 18029 verifyFormat("struct test demo[] = {\n" 18030 " {56, 23, \"hello\"}, // first line\n" 18031 " {-1, 93463, \"world\"}, // second line\n" 18032 " { 7, 5, \"!!\"} // third line\n" 18033 "};\n", 18034 Style); 18035 18036 verifyFormat("struct test demo[4] = {\n" 18037 " { 56, 23, 21, \"oh\"}, // first line\n" 18038 " { -1, 93463, 22, \"my\"}, // second line\n" 18039 " { 7, 5, 1, \"goodness\"} // third line\n" 18040 " {234, 5, 1, \"gracious\"} // fourth line\n" 18041 "};\n", 18042 Style); 18043 18044 verifyFormat("struct test demo[3] = {\n" 18045 " {56, 23, \"hello\"},\n" 18046 " {-1, 93463, \"world\"},\n" 18047 " { 7, 5, \"!!\"}\n" 18048 "};\n", 18049 Style); 18050 18051 verifyFormat("struct test demo[3] = {\n" 18052 " {int{56}, 23, \"hello\"},\n" 18053 " {int{-1}, 93463, \"world\"},\n" 18054 " { int{7}, 5, \"!!\"}\n" 18055 "};\n", 18056 Style); 18057 18058 verifyFormat("struct test demo[] = {\n" 18059 " {56, 23, \"hello\"},\n" 18060 " {-1, 93463, \"world\"},\n" 18061 " { 7, 5, \"!!\"},\n" 18062 "};\n", 18063 Style); 18064 18065 verifyFormat("test demo[] = {\n" 18066 " {56, 23, \"hello\"},\n" 18067 " {-1, 93463, \"world\"},\n" 18068 " { 7, 5, \"!!\"},\n" 18069 "};\n", 18070 Style); 18071 18072 verifyFormat("demo = std::array<struct test, 3>{\n" 18073 " test{56, 23, \"hello\"},\n" 18074 " test{-1, 93463, \"world\"},\n" 18075 " test{ 7, 5, \"!!\"},\n" 18076 "};\n", 18077 Style); 18078 18079 verifyFormat("test demo[] = {\n" 18080 " {56, 23, \"hello\"},\n" 18081 "#if X\n" 18082 " {-1, 93463, \"world\"},\n" 18083 "#endif\n" 18084 " { 7, 5, \"!!\"}\n" 18085 "};\n", 18086 Style); 18087 18088 verifyFormat( 18089 "test demo[] = {\n" 18090 " { 7, 23,\n" 18091 " \"hello world i am a very long line that really, in any\"\n" 18092 " \"just world, ought to be split over multiple lines\"},\n" 18093 " {-1, 93463, \"world\"},\n" 18094 " {56, 5, \"!!\"}\n" 18095 "};\n", 18096 Style); 18097 18098 verifyFormat("return GradForUnaryCwise(g, {\n" 18099 " {{\"sign\"}, \"Sign\", " 18100 " {\"x\", \"dy\"}},\n" 18101 " { {\"dx\"}, \"Mul\", {\"dy\"" 18102 ", \"sign\"}},\n" 18103 "});\n", 18104 Style); 18105 18106 Style.ColumnLimit = 0; 18107 EXPECT_EQ( 18108 "test demo[] = {\n" 18109 " {56, 23, \"hello world i am a very long line that really, " 18110 "in any just world, ought to be split over multiple lines\"},\n" 18111 " {-1, 93463, " 18112 " \"world\"},\n" 18113 " { 7, 5, " 18114 " \"!!\"},\n" 18115 "};", 18116 format("test demo[] = {{56, 23, \"hello world i am a very long line " 18117 "that really, in any just world, ought to be split over multiple " 18118 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 18119 Style)); 18120 18121 Style.ColumnLimit = 80; 18122 verifyFormat("test demo[] = {\n" 18123 " {56, 23, /* a comment */ \"hello\"},\n" 18124 " {-1, 93463, \"world\"},\n" 18125 " { 7, 5, \"!!\"}\n" 18126 "};\n", 18127 Style); 18128 18129 verifyFormat("test demo[] = {\n" 18130 " {56, 23, \"hello\"},\n" 18131 " {-1, 93463, \"world\" /* comment here */},\n" 18132 " { 7, 5, \"!!\"}\n" 18133 "};\n", 18134 Style); 18135 18136 verifyFormat("test demo[] = {\n" 18137 " {56, /* a comment */ 23, \"hello\"},\n" 18138 " {-1, 93463, \"world\"},\n" 18139 " { 7, 5, \"!!\"}\n" 18140 "};\n", 18141 Style); 18142 18143 Style.ColumnLimit = 20; 18144 EXPECT_EQ( 18145 "demo = std::array<\n" 18146 " struct test, 3>{\n" 18147 " test{\n" 18148 " 56, 23,\n" 18149 " \"hello \"\n" 18150 " \"world i \"\n" 18151 " \"am a very \"\n" 18152 " \"long line \"\n" 18153 " \"that \"\n" 18154 " \"really, \"\n" 18155 " \"in any \"\n" 18156 " \"just \"\n" 18157 " \"world, \"\n" 18158 " \"ought to \"\n" 18159 " \"be split \"\n" 18160 " \"over \"\n" 18161 " \"multiple \"\n" 18162 " \"lines\"},\n" 18163 " test{-1, 93463,\n" 18164 " \"world\"},\n" 18165 " test{ 7, 5,\n" 18166 " \"!!\" },\n" 18167 "};", 18168 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world " 18169 "i am a very long line that really, in any just world, ought " 18170 "to be split over multiple lines\"},test{-1, 93463, \"world\"}," 18171 "test{7, 5, \"!!\"},};", 18172 Style)); 18173 // This caused a core dump by enabling Alignment in the LLVMStyle globally 18174 Style = getLLVMStyleWithColumns(50); 18175 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 18176 verifyFormat("static A x = {\n" 18177 " {{init1, init2, init3, init4},\n" 18178 " {init1, init2, init3, init4}}\n" 18179 "};", 18180 Style); 18181 Style.ColumnLimit = 100; 18182 EXPECT_EQ( 18183 "test demo[] = {\n" 18184 " {56, 23,\n" 18185 " \"hello world i am a very long line that really, in any just world" 18186 ", ought to be split over \"\n" 18187 " \"multiple lines\" },\n" 18188 " {-1, 93463, \"world\"},\n" 18189 " { 7, 5, \"!!\"},\n" 18190 "};", 18191 format("test demo[] = {{56, 23, \"hello world i am a very long line " 18192 "that really, in any just world, ought to be split over multiple " 18193 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 18194 Style)); 18195 18196 Style = getLLVMStyleWithColumns(50); 18197 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 18198 Style.AlignConsecutiveAssignments = 18199 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 18200 Style.AlignConsecutiveDeclarations = 18201 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 18202 verifyFormat("struct test demo[] = {\n" 18203 " {56, 23, \"hello\"},\n" 18204 " {-1, 93463, \"world\"},\n" 18205 " { 7, 5, \"!!\"}\n" 18206 "};\n" 18207 "static A x = {\n" 18208 " {{init1, init2, init3, init4},\n" 18209 " {init1, init2, init3, init4}}\n" 18210 "};", 18211 Style); 18212 Style.ColumnLimit = 100; 18213 Style.AlignConsecutiveAssignments = 18214 FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments; 18215 Style.AlignConsecutiveDeclarations = 18216 FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments; 18217 verifyFormat("struct test demo[] = {\n" 18218 " {56, 23, \"hello\"},\n" 18219 " {-1, 93463, \"world\"},\n" 18220 " { 7, 5, \"!!\"}\n" 18221 "};\n" 18222 "struct test demo[4] = {\n" 18223 " { 56, 23, 21, \"oh\"}, // first line\n" 18224 " { -1, 93463, 22, \"my\"}, // second line\n" 18225 " { 7, 5, 1, \"goodness\"} // third line\n" 18226 " {234, 5, 1, \"gracious\"} // fourth line\n" 18227 "};\n", 18228 Style); 18229 EXPECT_EQ( 18230 "test demo[] = {\n" 18231 " {56,\n" 18232 " \"hello world i am a very long line that really, in any just world" 18233 ", ought to be split over \"\n" 18234 " \"multiple lines\", 23},\n" 18235 " {-1, \"world\", 93463},\n" 18236 " { 7, \"!!\", 5},\n" 18237 "};", 18238 format("test demo[] = {{56, \"hello world i am a very long line " 18239 "that really, in any just world, ought to be split over multiple " 18240 "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};", 18241 Style)); 18242 } 18243 18244 TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) { 18245 auto Style = getLLVMStyle(); 18246 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; 18247 /* FIXME: This case gets misformatted. 18248 verifyFormat("auto foo = Items{\n" 18249 " Section{0, bar(), },\n" 18250 " Section{1, boo() }\n" 18251 "};\n", 18252 Style); 18253 */ 18254 verifyFormat("auto foo = Items{\n" 18255 " Section{\n" 18256 " 0, bar(),\n" 18257 " }\n" 18258 "};\n", 18259 Style); 18260 verifyFormat("struct test demo[] = {\n" 18261 " {56, 23, \"hello\"},\n" 18262 " {-1, 93463, \"world\"},\n" 18263 " {7, 5, \"!!\" }\n" 18264 "};\n", 18265 Style); 18266 verifyFormat("struct test demo[] = {\n" 18267 " {56, 23, \"hello\"}, // first line\n" 18268 " {-1, 93463, \"world\"}, // second line\n" 18269 " {7, 5, \"!!\" } // third line\n" 18270 "};\n", 18271 Style); 18272 verifyFormat("struct test demo[4] = {\n" 18273 " {56, 23, 21, \"oh\" }, // first line\n" 18274 " {-1, 93463, 22, \"my\" }, // second line\n" 18275 " {7, 5, 1, \"goodness\"} // third line\n" 18276 " {234, 5, 1, \"gracious\"} // fourth line\n" 18277 "};\n", 18278 Style); 18279 verifyFormat("struct test demo[3] = {\n" 18280 " {56, 23, \"hello\"},\n" 18281 " {-1, 93463, \"world\"},\n" 18282 " {7, 5, \"!!\" }\n" 18283 "};\n", 18284 Style); 18285 18286 verifyFormat("struct test demo[3] = {\n" 18287 " {int{56}, 23, \"hello\"},\n" 18288 " {int{-1}, 93463, \"world\"},\n" 18289 " {int{7}, 5, \"!!\" }\n" 18290 "};\n", 18291 Style); 18292 verifyFormat("struct test demo[] = {\n" 18293 " {56, 23, \"hello\"},\n" 18294 " {-1, 93463, \"world\"},\n" 18295 " {7, 5, \"!!\" },\n" 18296 "};\n", 18297 Style); 18298 verifyFormat("test demo[] = {\n" 18299 " {56, 23, \"hello\"},\n" 18300 " {-1, 93463, \"world\"},\n" 18301 " {7, 5, \"!!\" },\n" 18302 "};\n", 18303 Style); 18304 verifyFormat("demo = std::array<struct test, 3>{\n" 18305 " test{56, 23, \"hello\"},\n" 18306 " test{-1, 93463, \"world\"},\n" 18307 " test{7, 5, \"!!\" },\n" 18308 "};\n", 18309 Style); 18310 verifyFormat("test demo[] = {\n" 18311 " {56, 23, \"hello\"},\n" 18312 "#if X\n" 18313 " {-1, 93463, \"world\"},\n" 18314 "#endif\n" 18315 " {7, 5, \"!!\" }\n" 18316 "};\n", 18317 Style); 18318 verifyFormat( 18319 "test demo[] = {\n" 18320 " {7, 23,\n" 18321 " \"hello world i am a very long line that really, in any\"\n" 18322 " \"just world, ought to be split over multiple lines\"},\n" 18323 " {-1, 93463, \"world\" },\n" 18324 " {56, 5, \"!!\" }\n" 18325 "};\n", 18326 Style); 18327 18328 verifyFormat("return GradForUnaryCwise(g, {\n" 18329 " {{\"sign\"}, \"Sign\", {\"x\", " 18330 "\"dy\"} },\n" 18331 " {{\"dx\"}, \"Mul\", " 18332 "{\"dy\", \"sign\"}},\n" 18333 "});\n", 18334 Style); 18335 18336 Style.ColumnLimit = 0; 18337 EXPECT_EQ( 18338 "test demo[] = {\n" 18339 " {56, 23, \"hello world i am a very long line that really, in any " 18340 "just world, ought to be split over multiple lines\"},\n" 18341 " {-1, 93463, \"world\" " 18342 " },\n" 18343 " {7, 5, \"!!\" " 18344 " },\n" 18345 "};", 18346 format("test demo[] = {{56, 23, \"hello world i am a very long line " 18347 "that really, in any just world, ought to be split over multiple " 18348 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 18349 Style)); 18350 18351 Style.ColumnLimit = 80; 18352 verifyFormat("test demo[] = {\n" 18353 " {56, 23, /* a comment */ \"hello\"},\n" 18354 " {-1, 93463, \"world\" },\n" 18355 " {7, 5, \"!!\" }\n" 18356 "};\n", 18357 Style); 18358 18359 verifyFormat("test demo[] = {\n" 18360 " {56, 23, \"hello\" },\n" 18361 " {-1, 93463, \"world\" /* comment here */},\n" 18362 " {7, 5, \"!!\" }\n" 18363 "};\n", 18364 Style); 18365 18366 verifyFormat("test demo[] = {\n" 18367 " {56, /* a comment */ 23, \"hello\"},\n" 18368 " {-1, 93463, \"world\"},\n" 18369 " {7, 5, \"!!\" }\n" 18370 "};\n", 18371 Style); 18372 18373 Style.ColumnLimit = 20; 18374 EXPECT_EQ( 18375 "demo = std::array<\n" 18376 " struct test, 3>{\n" 18377 " test{\n" 18378 " 56, 23,\n" 18379 " \"hello \"\n" 18380 " \"world i \"\n" 18381 " \"am a very \"\n" 18382 " \"long line \"\n" 18383 " \"that \"\n" 18384 " \"really, \"\n" 18385 " \"in any \"\n" 18386 " \"just \"\n" 18387 " \"world, \"\n" 18388 " \"ought to \"\n" 18389 " \"be split \"\n" 18390 " \"over \"\n" 18391 " \"multiple \"\n" 18392 " \"lines\"},\n" 18393 " test{-1, 93463,\n" 18394 " \"world\"},\n" 18395 " test{7, 5,\n" 18396 " \"!!\" },\n" 18397 "};", 18398 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world " 18399 "i am a very long line that really, in any just world, ought " 18400 "to be split over multiple lines\"},test{-1, 93463, \"world\"}," 18401 "test{7, 5, \"!!\"},};", 18402 Style)); 18403 18404 // This caused a core dump by enabling Alignment in the LLVMStyle globally 18405 Style = getLLVMStyleWithColumns(50); 18406 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; 18407 verifyFormat("static A x = {\n" 18408 " {{init1, init2, init3, init4},\n" 18409 " {init1, init2, init3, init4}}\n" 18410 "};", 18411 Style); 18412 Style.ColumnLimit = 100; 18413 EXPECT_EQ( 18414 "test demo[] = {\n" 18415 " {56, 23,\n" 18416 " \"hello world i am a very long line that really, in any just world" 18417 ", ought to be split over \"\n" 18418 " \"multiple lines\" },\n" 18419 " {-1, 93463, \"world\"},\n" 18420 " {7, 5, \"!!\" },\n" 18421 "};", 18422 format("test demo[] = {{56, 23, \"hello world i am a very long line " 18423 "that really, in any just world, ought to be split over multiple " 18424 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 18425 Style)); 18426 } 18427 18428 TEST_F(FormatTest, UnderstandsPragmas) { 18429 verifyFormat("#pragma omp reduction(| : var)"); 18430 verifyFormat("#pragma omp reduction(+ : var)"); 18431 18432 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 18433 "(including parentheses).", 18434 format("#pragma mark Any non-hyphenated or hyphenated string " 18435 "(including parentheses).")); 18436 } 18437 18438 TEST_F(FormatTest, UnderstandPragmaOption) { 18439 verifyFormat("#pragma option -C -A"); 18440 18441 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 18442 } 18443 18444 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) { 18445 FormatStyle Style = getLLVMStyleWithColumns(20); 18446 18447 // See PR41213 18448 EXPECT_EQ("/*\n" 18449 " *\t9012345\n" 18450 " * /8901\n" 18451 " */", 18452 format("/*\n" 18453 " *\t9012345 /8901\n" 18454 " */", 18455 Style)); 18456 EXPECT_EQ("/*\n" 18457 " *345678\n" 18458 " *\t/8901\n" 18459 " */", 18460 format("/*\n" 18461 " *345678\t/8901\n" 18462 " */", 18463 Style)); 18464 18465 verifyFormat("int a; // the\n" 18466 " // comment", 18467 Style); 18468 EXPECT_EQ("int a; /* first line\n" 18469 " * second\n" 18470 " * line third\n" 18471 " * line\n" 18472 " */", 18473 format("int a; /* first line\n" 18474 " * second\n" 18475 " * line third\n" 18476 " * line\n" 18477 " */", 18478 Style)); 18479 EXPECT_EQ("int a; // first line\n" 18480 " // second\n" 18481 " // line third\n" 18482 " // line", 18483 format("int a; // first line\n" 18484 " // second line\n" 18485 " // third line", 18486 Style)); 18487 18488 Style.PenaltyExcessCharacter = 90; 18489 verifyFormat("int a; // the comment", Style); 18490 EXPECT_EQ("int a; // the comment\n" 18491 " // aaa", 18492 format("int a; // the comment aaa", Style)); 18493 EXPECT_EQ("int a; /* first line\n" 18494 " * second line\n" 18495 " * third line\n" 18496 " */", 18497 format("int a; /* first line\n" 18498 " * second line\n" 18499 " * third line\n" 18500 " */", 18501 Style)); 18502 EXPECT_EQ("int a; // first line\n" 18503 " // second line\n" 18504 " // third line", 18505 format("int a; // first line\n" 18506 " // second line\n" 18507 " // third line", 18508 Style)); 18509 // FIXME: Investigate why this is not getting the same layout as the test 18510 // above. 18511 EXPECT_EQ("int a; /* first line\n" 18512 " * second line\n" 18513 " * third line\n" 18514 " */", 18515 format("int a; /* first line second line third line" 18516 "\n*/", 18517 Style)); 18518 18519 EXPECT_EQ("// foo bar baz bazfoo\n" 18520 "// foo bar foo bar\n", 18521 format("// foo bar baz bazfoo\n" 18522 "// foo bar foo bar\n", 18523 Style)); 18524 EXPECT_EQ("// foo bar baz bazfoo\n" 18525 "// foo bar foo bar\n", 18526 format("// foo bar baz bazfoo\n" 18527 "// foo bar foo bar\n", 18528 Style)); 18529 18530 // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the 18531 // next one. 18532 EXPECT_EQ("// foo bar baz bazfoo\n" 18533 "// bar foo bar\n", 18534 format("// foo bar baz bazfoo bar\n" 18535 "// foo bar\n", 18536 Style)); 18537 18538 EXPECT_EQ("// foo bar baz bazfoo\n" 18539 "// foo bar baz bazfoo\n" 18540 "// bar foo bar\n", 18541 format("// foo bar baz bazfoo\n" 18542 "// foo bar baz bazfoo bar\n" 18543 "// foo bar\n", 18544 Style)); 18545 18546 EXPECT_EQ("// foo bar baz bazfoo\n" 18547 "// foo bar baz bazfoo\n" 18548 "// bar foo bar\n", 18549 format("// foo bar baz bazfoo\n" 18550 "// foo bar baz bazfoo bar\n" 18551 "// foo bar\n", 18552 Style)); 18553 18554 // Make sure we do not keep protruding characters if strict mode reflow is 18555 // cheaper than keeping protruding characters. 18556 Style.ColumnLimit = 21; 18557 EXPECT_EQ( 18558 "// foo foo foo foo\n" 18559 "// foo foo foo foo\n" 18560 "// foo foo foo foo\n", 18561 format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", Style)); 18562 18563 EXPECT_EQ("int a = /* long block\n" 18564 " comment */\n" 18565 " 42;", 18566 format("int a = /* long block comment */ 42;", Style)); 18567 } 18568 18569 TEST_F(FormatTest, BreakPenaltyAfterLParen) { 18570 FormatStyle Style = getLLVMStyle(); 18571 Style.ColumnLimit = 8; 18572 Style.PenaltyExcessCharacter = 15; 18573 verifyFormat("int foo(\n" 18574 " int aaaaaaaaaaaaaaaaaaaaaaaa);", 18575 Style); 18576 Style.PenaltyBreakOpenParenthesis = 200; 18577 EXPECT_EQ("int foo(int aaaaaaaaaaaaaaaaaaaaaaaa);", 18578 format("int foo(\n" 18579 " int aaaaaaaaaaaaaaaaaaaaaaaa);", 18580 Style)); 18581 } 18582 18583 TEST_F(FormatTest, BreakPenaltyAfterCastLParen) { 18584 FormatStyle Style = getLLVMStyle(); 18585 Style.ColumnLimit = 5; 18586 Style.PenaltyExcessCharacter = 150; 18587 verifyFormat("foo((\n" 18588 " int)aaaaaaaaaaaaaaaaaaaaaaaa);", 18589 18590 Style); 18591 Style.PenaltyBreakOpenParenthesis = 100000; 18592 EXPECT_EQ("foo((int)\n" 18593 " aaaaaaaaaaaaaaaaaaaaaaaa);", 18594 format("foo((\n" 18595 "int)aaaaaaaaaaaaaaaaaaaaaaaa);", 18596 Style)); 18597 } 18598 18599 TEST_F(FormatTest, BreakPenaltyAfterForLoopLParen) { 18600 FormatStyle Style = getLLVMStyle(); 18601 Style.ColumnLimit = 4; 18602 Style.PenaltyExcessCharacter = 100; 18603 verifyFormat("for (\n" 18604 " int iiiiiiiiiiiiiiiii =\n" 18605 " 0;\n" 18606 " iiiiiiiiiiiiiiiii <\n" 18607 " 2;\n" 18608 " iiiiiiiiiiiiiiiii++) {\n" 18609 "}", 18610 18611 Style); 18612 Style.PenaltyBreakOpenParenthesis = 1250; 18613 EXPECT_EQ("for (int iiiiiiiiiiiiiiiii =\n" 18614 " 0;\n" 18615 " iiiiiiiiiiiiiiiii <\n" 18616 " 2;\n" 18617 " iiiiiiiiiiiiiiiii++) {\n" 18618 "}", 18619 format("for (\n" 18620 " int iiiiiiiiiiiiiiiii =\n" 18621 " 0;\n" 18622 " iiiiiiiiiiiiiiiii <\n" 18623 " 2;\n" 18624 " iiiiiiiiiiiiiiiii++) {\n" 18625 "}", 18626 Style)); 18627 } 18628 18629 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 18630 for (size_t i = 1; i < Styles.size(); ++i) \ 18631 EXPECT_EQ(Styles[0], Styles[i]) \ 18632 << "Style #" << i << " of " << Styles.size() << " differs from Style #0" 18633 18634 TEST_F(FormatTest, GetsPredefinedStyleByName) { 18635 SmallVector<FormatStyle, 3> Styles; 18636 Styles.resize(3); 18637 18638 Styles[0] = getLLVMStyle(); 18639 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 18640 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 18641 EXPECT_ALL_STYLES_EQUAL(Styles); 18642 18643 Styles[0] = getGoogleStyle(); 18644 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 18645 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 18646 EXPECT_ALL_STYLES_EQUAL(Styles); 18647 18648 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 18649 EXPECT_TRUE( 18650 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 18651 EXPECT_TRUE( 18652 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 18653 EXPECT_ALL_STYLES_EQUAL(Styles); 18654 18655 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 18656 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 18657 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 18658 EXPECT_ALL_STYLES_EQUAL(Styles); 18659 18660 Styles[0] = getMozillaStyle(); 18661 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 18662 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 18663 EXPECT_ALL_STYLES_EQUAL(Styles); 18664 18665 Styles[0] = getWebKitStyle(); 18666 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 18667 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 18668 EXPECT_ALL_STYLES_EQUAL(Styles); 18669 18670 Styles[0] = getGNUStyle(); 18671 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 18672 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 18673 EXPECT_ALL_STYLES_EQUAL(Styles); 18674 18675 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 18676 } 18677 18678 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 18679 SmallVector<FormatStyle, 8> Styles; 18680 Styles.resize(2); 18681 18682 Styles[0] = getGoogleStyle(); 18683 Styles[1] = getLLVMStyle(); 18684 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 18685 EXPECT_ALL_STYLES_EQUAL(Styles); 18686 18687 Styles.resize(5); 18688 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 18689 Styles[1] = getLLVMStyle(); 18690 Styles[1].Language = FormatStyle::LK_JavaScript; 18691 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 18692 18693 Styles[2] = getLLVMStyle(); 18694 Styles[2].Language = FormatStyle::LK_JavaScript; 18695 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 18696 "BasedOnStyle: Google", 18697 &Styles[2]) 18698 .value()); 18699 18700 Styles[3] = getLLVMStyle(); 18701 Styles[3].Language = FormatStyle::LK_JavaScript; 18702 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 18703 "Language: JavaScript", 18704 &Styles[3]) 18705 .value()); 18706 18707 Styles[4] = getLLVMStyle(); 18708 Styles[4].Language = FormatStyle::LK_JavaScript; 18709 EXPECT_EQ(0, parseConfiguration("---\n" 18710 "BasedOnStyle: LLVM\n" 18711 "IndentWidth: 123\n" 18712 "---\n" 18713 "BasedOnStyle: Google\n" 18714 "Language: JavaScript", 18715 &Styles[4]) 18716 .value()); 18717 EXPECT_ALL_STYLES_EQUAL(Styles); 18718 } 18719 18720 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 18721 Style.FIELD = false; \ 18722 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 18723 EXPECT_TRUE(Style.FIELD); \ 18724 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 18725 EXPECT_FALSE(Style.FIELD); 18726 18727 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 18728 18729 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 18730 Style.STRUCT.FIELD = false; \ 18731 EXPECT_EQ(0, \ 18732 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 18733 .value()); \ 18734 EXPECT_TRUE(Style.STRUCT.FIELD); \ 18735 EXPECT_EQ(0, \ 18736 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 18737 .value()); \ 18738 EXPECT_FALSE(Style.STRUCT.FIELD); 18739 18740 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 18741 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 18742 18743 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 18744 EXPECT_NE(VALUE, Style.FIELD) << "Initial value already the same!"; \ 18745 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 18746 EXPECT_EQ(VALUE, Style.FIELD) << "Unexpected value after parsing!" 18747 18748 TEST_F(FormatTest, ParsesConfigurationBools) { 18749 FormatStyle Style = {}; 18750 Style.Language = FormatStyle::LK_Cpp; 18751 CHECK_PARSE_BOOL(AlignTrailingComments); 18752 CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine); 18753 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 18754 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 18755 CHECK_PARSE_BOOL(AllowShortEnumsOnASingleLine); 18756 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 18757 CHECK_PARSE_BOOL(BinPackArguments); 18758 CHECK_PARSE_BOOL(BinPackParameters); 18759 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 18760 CHECK_PARSE_BOOL(BreakBeforeConceptDeclarations); 18761 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 18762 CHECK_PARSE_BOOL(BreakStringLiterals); 18763 CHECK_PARSE_BOOL(CompactNamespaces); 18764 CHECK_PARSE_BOOL(DeriveLineEnding); 18765 CHECK_PARSE_BOOL(DerivePointerAlignment); 18766 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 18767 CHECK_PARSE_BOOL(DisableFormat); 18768 CHECK_PARSE_BOOL(IndentAccessModifiers); 18769 CHECK_PARSE_BOOL(IndentCaseLabels); 18770 CHECK_PARSE_BOOL(IndentCaseBlocks); 18771 CHECK_PARSE_BOOL(IndentGotoLabels); 18772 CHECK_PARSE_BOOL(IndentRequires); 18773 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 18774 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 18775 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 18776 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 18777 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 18778 CHECK_PARSE_BOOL(ReflowComments); 18779 CHECK_PARSE_BOOL(SortUsingDeclarations); 18780 CHECK_PARSE_BOOL(SpacesInParentheses); 18781 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 18782 CHECK_PARSE_BOOL(SpacesInConditionalStatement); 18783 CHECK_PARSE_BOOL(SpaceInEmptyBlock); 18784 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 18785 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 18786 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 18787 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 18788 CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); 18789 CHECK_PARSE_BOOL(SpaceAfterLogicalNot); 18790 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 18791 CHECK_PARSE_BOOL(SpaceBeforeCaseColon); 18792 CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList); 18793 CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon); 18794 CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon); 18795 CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon); 18796 CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets); 18797 CHECK_PARSE_BOOL(UseCRLF); 18798 18799 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel); 18800 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 18801 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 18802 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 18803 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 18804 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 18805 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 18806 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 18807 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock); 18808 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 18809 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 18810 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeLambdaBody); 18811 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeWhile); 18812 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 18813 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction); 18814 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord); 18815 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace); 18816 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterControlStatements); 18817 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterForeachMacros); 18818 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, 18819 AfterFunctionDeclarationName); 18820 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, 18821 AfterFunctionDefinitionName); 18822 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterIfMacros); 18823 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterOverloadedOperator); 18824 CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, BeforeNonEmptyParentheses); 18825 } 18826 18827 #undef CHECK_PARSE_BOOL 18828 18829 TEST_F(FormatTest, ParsesConfiguration) { 18830 FormatStyle Style = {}; 18831 Style.Language = FormatStyle::LK_Cpp; 18832 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 18833 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 18834 ConstructorInitializerIndentWidth, 1234u); 18835 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 18836 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 18837 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 18838 CHECK_PARSE("PenaltyBreakAssignment: 1234", PenaltyBreakAssignment, 1234u); 18839 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 18840 PenaltyBreakBeforeFirstCallParameter, 1234u); 18841 CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234", 18842 PenaltyBreakTemplateDeclaration, 1234u); 18843 CHECK_PARSE("PenaltyBreakOpenParenthesis: 1234", PenaltyBreakOpenParenthesis, 18844 1234u); 18845 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 18846 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 18847 PenaltyReturnTypeOnItsOwnLine, 1234u); 18848 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 18849 SpacesBeforeTrailingComments, 1234u); 18850 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 18851 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 18852 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$"); 18853 18854 Style.QualifierAlignment = FormatStyle::QAS_Right; 18855 CHECK_PARSE("QualifierAlignment: Leave", QualifierAlignment, 18856 FormatStyle::QAS_Leave); 18857 CHECK_PARSE("QualifierAlignment: Right", QualifierAlignment, 18858 FormatStyle::QAS_Right); 18859 CHECK_PARSE("QualifierAlignment: Left", QualifierAlignment, 18860 FormatStyle::QAS_Left); 18861 CHECK_PARSE("QualifierAlignment: Custom", QualifierAlignment, 18862 FormatStyle::QAS_Custom); 18863 18864 Style.QualifierOrder.clear(); 18865 CHECK_PARSE("QualifierOrder: [ const, volatile, type ]", QualifierOrder, 18866 std::vector<std::string>({"const", "volatile", "type"})); 18867 Style.QualifierOrder.clear(); 18868 CHECK_PARSE("QualifierOrder: [const, type]", QualifierOrder, 18869 std::vector<std::string>({"const", "type"})); 18870 Style.QualifierOrder.clear(); 18871 CHECK_PARSE("QualifierOrder: [volatile, type]", QualifierOrder, 18872 std::vector<std::string>({"volatile", "type"})); 18873 18874 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 18875 CHECK_PARSE("AlignConsecutiveAssignments: None", AlignConsecutiveAssignments, 18876 FormatStyle::ACS_None); 18877 CHECK_PARSE("AlignConsecutiveAssignments: Consecutive", 18878 AlignConsecutiveAssignments, FormatStyle::ACS_Consecutive); 18879 CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLines", 18880 AlignConsecutiveAssignments, FormatStyle::ACS_AcrossEmptyLines); 18881 CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLinesAndComments", 18882 AlignConsecutiveAssignments, 18883 FormatStyle::ACS_AcrossEmptyLinesAndComments); 18884 // For backwards compability, false / true should still parse 18885 CHECK_PARSE("AlignConsecutiveAssignments: false", AlignConsecutiveAssignments, 18886 FormatStyle::ACS_None); 18887 CHECK_PARSE("AlignConsecutiveAssignments: true", AlignConsecutiveAssignments, 18888 FormatStyle::ACS_Consecutive); 18889 18890 Style.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive; 18891 CHECK_PARSE("AlignConsecutiveBitFields: None", AlignConsecutiveBitFields, 18892 FormatStyle::ACS_None); 18893 CHECK_PARSE("AlignConsecutiveBitFields: Consecutive", 18894 AlignConsecutiveBitFields, FormatStyle::ACS_Consecutive); 18895 CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLines", 18896 AlignConsecutiveBitFields, FormatStyle::ACS_AcrossEmptyLines); 18897 CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLinesAndComments", 18898 AlignConsecutiveBitFields, 18899 FormatStyle::ACS_AcrossEmptyLinesAndComments); 18900 // For backwards compability, false / true should still parse 18901 CHECK_PARSE("AlignConsecutiveBitFields: false", AlignConsecutiveBitFields, 18902 FormatStyle::ACS_None); 18903 CHECK_PARSE("AlignConsecutiveBitFields: true", AlignConsecutiveBitFields, 18904 FormatStyle::ACS_Consecutive); 18905 18906 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 18907 CHECK_PARSE("AlignConsecutiveMacros: None", AlignConsecutiveMacros, 18908 FormatStyle::ACS_None); 18909 CHECK_PARSE("AlignConsecutiveMacros: Consecutive", AlignConsecutiveMacros, 18910 FormatStyle::ACS_Consecutive); 18911 CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLines", 18912 AlignConsecutiveMacros, FormatStyle::ACS_AcrossEmptyLines); 18913 CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLinesAndComments", 18914 AlignConsecutiveMacros, 18915 FormatStyle::ACS_AcrossEmptyLinesAndComments); 18916 // For backwards compability, false / true should still parse 18917 CHECK_PARSE("AlignConsecutiveMacros: false", AlignConsecutiveMacros, 18918 FormatStyle::ACS_None); 18919 CHECK_PARSE("AlignConsecutiveMacros: true", AlignConsecutiveMacros, 18920 FormatStyle::ACS_Consecutive); 18921 18922 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 18923 CHECK_PARSE("AlignConsecutiveDeclarations: None", 18924 AlignConsecutiveDeclarations, FormatStyle::ACS_None); 18925 CHECK_PARSE("AlignConsecutiveDeclarations: Consecutive", 18926 AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive); 18927 CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLines", 18928 AlignConsecutiveDeclarations, FormatStyle::ACS_AcrossEmptyLines); 18929 CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLinesAndComments", 18930 AlignConsecutiveDeclarations, 18931 FormatStyle::ACS_AcrossEmptyLinesAndComments); 18932 // For backwards compability, false / true should still parse 18933 CHECK_PARSE("AlignConsecutiveDeclarations: false", 18934 AlignConsecutiveDeclarations, FormatStyle::ACS_None); 18935 CHECK_PARSE("AlignConsecutiveDeclarations: true", 18936 AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive); 18937 18938 Style.PointerAlignment = FormatStyle::PAS_Middle; 18939 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 18940 FormatStyle::PAS_Left); 18941 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 18942 FormatStyle::PAS_Right); 18943 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 18944 FormatStyle::PAS_Middle); 18945 Style.ReferenceAlignment = FormatStyle::RAS_Middle; 18946 CHECK_PARSE("ReferenceAlignment: Pointer", ReferenceAlignment, 18947 FormatStyle::RAS_Pointer); 18948 CHECK_PARSE("ReferenceAlignment: Left", ReferenceAlignment, 18949 FormatStyle::RAS_Left); 18950 CHECK_PARSE("ReferenceAlignment: Right", ReferenceAlignment, 18951 FormatStyle::RAS_Right); 18952 CHECK_PARSE("ReferenceAlignment: Middle", ReferenceAlignment, 18953 FormatStyle::RAS_Middle); 18954 // For backward compatibility: 18955 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 18956 FormatStyle::PAS_Left); 18957 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 18958 FormatStyle::PAS_Right); 18959 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 18960 FormatStyle::PAS_Middle); 18961 18962 Style.Standard = FormatStyle::LS_Auto; 18963 CHECK_PARSE("Standard: c++03", Standard, FormatStyle::LS_Cpp03); 18964 CHECK_PARSE("Standard: c++11", Standard, FormatStyle::LS_Cpp11); 18965 CHECK_PARSE("Standard: c++14", Standard, FormatStyle::LS_Cpp14); 18966 CHECK_PARSE("Standard: c++17", Standard, FormatStyle::LS_Cpp17); 18967 CHECK_PARSE("Standard: c++20", Standard, FormatStyle::LS_Cpp20); 18968 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 18969 CHECK_PARSE("Standard: Latest", Standard, FormatStyle::LS_Latest); 18970 // Legacy aliases: 18971 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 18972 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Latest); 18973 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 18974 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 18975 18976 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 18977 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 18978 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 18979 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 18980 FormatStyle::BOS_None); 18981 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 18982 FormatStyle::BOS_All); 18983 // For backward compatibility: 18984 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 18985 FormatStyle::BOS_None); 18986 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 18987 FormatStyle::BOS_All); 18988 18989 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 18990 CHECK_PARSE("BreakConstructorInitializers: BeforeComma", 18991 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 18992 CHECK_PARSE("BreakConstructorInitializers: AfterColon", 18993 BreakConstructorInitializers, FormatStyle::BCIS_AfterColon); 18994 CHECK_PARSE("BreakConstructorInitializers: BeforeColon", 18995 BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon); 18996 // For backward compatibility: 18997 CHECK_PARSE("BreakConstructorInitializersBeforeComma: true", 18998 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 18999 19000 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 19001 CHECK_PARSE("BreakInheritanceList: AfterComma", BreakInheritanceList, 19002 FormatStyle::BILS_AfterComma); 19003 CHECK_PARSE("BreakInheritanceList: BeforeComma", BreakInheritanceList, 19004 FormatStyle::BILS_BeforeComma); 19005 CHECK_PARSE("BreakInheritanceList: AfterColon", BreakInheritanceList, 19006 FormatStyle::BILS_AfterColon); 19007 CHECK_PARSE("BreakInheritanceList: BeforeColon", BreakInheritanceList, 19008 FormatStyle::BILS_BeforeColon); 19009 // For backward compatibility: 19010 CHECK_PARSE("BreakBeforeInheritanceComma: true", BreakInheritanceList, 19011 FormatStyle::BILS_BeforeComma); 19012 19013 Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack; 19014 CHECK_PARSE("PackConstructorInitializers: Never", PackConstructorInitializers, 19015 FormatStyle::PCIS_Never); 19016 CHECK_PARSE("PackConstructorInitializers: BinPack", 19017 PackConstructorInitializers, FormatStyle::PCIS_BinPack); 19018 CHECK_PARSE("PackConstructorInitializers: CurrentLine", 19019 PackConstructorInitializers, FormatStyle::PCIS_CurrentLine); 19020 CHECK_PARSE("PackConstructorInitializers: NextLine", 19021 PackConstructorInitializers, FormatStyle::PCIS_NextLine); 19022 // For backward compatibility: 19023 CHECK_PARSE("BasedOnStyle: Google\n" 19024 "ConstructorInitializerAllOnOneLineOrOnePerLine: true\n" 19025 "AllowAllConstructorInitializersOnNextLine: false", 19026 PackConstructorInitializers, FormatStyle::PCIS_CurrentLine); 19027 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 19028 CHECK_PARSE("BasedOnStyle: Google\n" 19029 "ConstructorInitializerAllOnOneLineOrOnePerLine: false", 19030 PackConstructorInitializers, FormatStyle::PCIS_BinPack); 19031 CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n" 19032 "AllowAllConstructorInitializersOnNextLine: true", 19033 PackConstructorInitializers, FormatStyle::PCIS_NextLine); 19034 Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack; 19035 CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n" 19036 "AllowAllConstructorInitializersOnNextLine: false", 19037 PackConstructorInitializers, FormatStyle::PCIS_CurrentLine); 19038 19039 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 19040 CHECK_PARSE("EmptyLineBeforeAccessModifier: Never", 19041 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Never); 19042 CHECK_PARSE("EmptyLineBeforeAccessModifier: Leave", 19043 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Leave); 19044 CHECK_PARSE("EmptyLineBeforeAccessModifier: LogicalBlock", 19045 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_LogicalBlock); 19046 CHECK_PARSE("EmptyLineBeforeAccessModifier: Always", 19047 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Always); 19048 19049 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 19050 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 19051 FormatStyle::BAS_Align); 19052 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 19053 FormatStyle::BAS_DontAlign); 19054 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 19055 FormatStyle::BAS_AlwaysBreak); 19056 // For backward compatibility: 19057 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 19058 FormatStyle::BAS_DontAlign); 19059 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 19060 FormatStyle::BAS_Align); 19061 19062 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left; 19063 CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines, 19064 FormatStyle::ENAS_DontAlign); 19065 CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines, 19066 FormatStyle::ENAS_Left); 19067 CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines, 19068 FormatStyle::ENAS_Right); 19069 // For backward compatibility: 19070 CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines, 19071 FormatStyle::ENAS_Left); 19072 CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines, 19073 FormatStyle::ENAS_Right); 19074 19075 Style.AlignOperands = FormatStyle::OAS_Align; 19076 CHECK_PARSE("AlignOperands: DontAlign", AlignOperands, 19077 FormatStyle::OAS_DontAlign); 19078 CHECK_PARSE("AlignOperands: Align", AlignOperands, FormatStyle::OAS_Align); 19079 CHECK_PARSE("AlignOperands: AlignAfterOperator", AlignOperands, 19080 FormatStyle::OAS_AlignAfterOperator); 19081 // For backward compatibility: 19082 CHECK_PARSE("AlignOperands: false", AlignOperands, 19083 FormatStyle::OAS_DontAlign); 19084 CHECK_PARSE("AlignOperands: true", AlignOperands, FormatStyle::OAS_Align); 19085 19086 Style.UseTab = FormatStyle::UT_ForIndentation; 19087 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 19088 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 19089 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 19090 CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab, 19091 FormatStyle::UT_ForContinuationAndIndentation); 19092 CHECK_PARSE("UseTab: AlignWithSpaces", UseTab, 19093 FormatStyle::UT_AlignWithSpaces); 19094 // For backward compatibility: 19095 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 19096 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 19097 19098 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 19099 CHECK_PARSE("AllowShortBlocksOnASingleLine: Never", 19100 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 19101 CHECK_PARSE("AllowShortBlocksOnASingleLine: Empty", 19102 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Empty); 19103 CHECK_PARSE("AllowShortBlocksOnASingleLine: Always", 19104 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always); 19105 // For backward compatibility: 19106 CHECK_PARSE("AllowShortBlocksOnASingleLine: false", 19107 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 19108 CHECK_PARSE("AllowShortBlocksOnASingleLine: true", 19109 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always); 19110 19111 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 19112 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 19113 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 19114 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 19115 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 19116 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 19117 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 19118 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 19119 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 19120 // For backward compatibility: 19121 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 19122 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 19123 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 19124 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 19125 19126 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Both; 19127 CHECK_PARSE("SpaceAroundPointerQualifiers: Default", 19128 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Default); 19129 CHECK_PARSE("SpaceAroundPointerQualifiers: Before", 19130 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Before); 19131 CHECK_PARSE("SpaceAroundPointerQualifiers: After", 19132 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_After); 19133 CHECK_PARSE("SpaceAroundPointerQualifiers: Both", 19134 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Both); 19135 19136 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 19137 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 19138 FormatStyle::SBPO_Never); 19139 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 19140 FormatStyle::SBPO_Always); 19141 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 19142 FormatStyle::SBPO_ControlStatements); 19143 CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptControlMacros", 19144 SpaceBeforeParens, 19145 FormatStyle::SBPO_ControlStatementsExceptControlMacros); 19146 CHECK_PARSE("SpaceBeforeParens: NonEmptyParentheses", SpaceBeforeParens, 19147 FormatStyle::SBPO_NonEmptyParentheses); 19148 CHECK_PARSE("SpaceBeforeParens: Custom", SpaceBeforeParens, 19149 FormatStyle::SBPO_Custom); 19150 // For backward compatibility: 19151 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 19152 FormatStyle::SBPO_Never); 19153 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 19154 FormatStyle::SBPO_ControlStatements); 19155 CHECK_PARSE("SpaceBeforeParens: ControlStatementsExceptForEachMacros", 19156 SpaceBeforeParens, 19157 FormatStyle::SBPO_ControlStatementsExceptControlMacros); 19158 19159 Style.ColumnLimit = 123; 19160 FormatStyle BaseStyle = getLLVMStyle(); 19161 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 19162 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 19163 19164 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 19165 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 19166 FormatStyle::BS_Attach); 19167 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 19168 FormatStyle::BS_Linux); 19169 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 19170 FormatStyle::BS_Mozilla); 19171 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 19172 FormatStyle::BS_Stroustrup); 19173 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 19174 FormatStyle::BS_Allman); 19175 CHECK_PARSE("BreakBeforeBraces: Whitesmiths", BreakBeforeBraces, 19176 FormatStyle::BS_Whitesmiths); 19177 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 19178 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 19179 FormatStyle::BS_WebKit); 19180 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 19181 FormatStyle::BS_Custom); 19182 19183 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never; 19184 CHECK_PARSE("BraceWrapping:\n" 19185 " AfterControlStatement: MultiLine", 19186 BraceWrapping.AfterControlStatement, 19187 FormatStyle::BWACS_MultiLine); 19188 CHECK_PARSE("BraceWrapping:\n" 19189 " AfterControlStatement: Always", 19190 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always); 19191 CHECK_PARSE("BraceWrapping:\n" 19192 " AfterControlStatement: Never", 19193 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never); 19194 // For backward compatibility: 19195 CHECK_PARSE("BraceWrapping:\n" 19196 " AfterControlStatement: true", 19197 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always); 19198 CHECK_PARSE("BraceWrapping:\n" 19199 " AfterControlStatement: false", 19200 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never); 19201 19202 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 19203 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 19204 FormatStyle::RTBS_None); 19205 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 19206 FormatStyle::RTBS_All); 19207 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 19208 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 19209 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 19210 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 19211 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 19212 AlwaysBreakAfterReturnType, 19213 FormatStyle::RTBS_TopLevelDefinitions); 19214 19215 Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 19216 CHECK_PARSE("AlwaysBreakTemplateDeclarations: No", 19217 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_No); 19218 CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine", 19219 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine); 19220 CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes", 19221 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes); 19222 CHECK_PARSE("AlwaysBreakTemplateDeclarations: false", 19223 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine); 19224 CHECK_PARSE("AlwaysBreakTemplateDeclarations: true", 19225 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes); 19226 19227 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 19228 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 19229 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 19230 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 19231 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 19232 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 19233 AlwaysBreakAfterDefinitionReturnType, 19234 FormatStyle::DRTBS_TopLevel); 19235 19236 Style.NamespaceIndentation = FormatStyle::NI_All; 19237 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 19238 FormatStyle::NI_None); 19239 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 19240 FormatStyle::NI_Inner); 19241 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 19242 FormatStyle::NI_All); 19243 19244 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_OnlyFirstIf; 19245 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Never", 19246 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never); 19247 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: WithoutElse", 19248 AllowShortIfStatementsOnASingleLine, 19249 FormatStyle::SIS_WithoutElse); 19250 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: OnlyFirstIf", 19251 AllowShortIfStatementsOnASingleLine, 19252 FormatStyle::SIS_OnlyFirstIf); 19253 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: AllIfsAndElse", 19254 AllowShortIfStatementsOnASingleLine, 19255 FormatStyle::SIS_AllIfsAndElse); 19256 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Always", 19257 AllowShortIfStatementsOnASingleLine, 19258 FormatStyle::SIS_OnlyFirstIf); 19259 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: false", 19260 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never); 19261 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: true", 19262 AllowShortIfStatementsOnASingleLine, 19263 FormatStyle::SIS_WithoutElse); 19264 19265 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; 19266 CHECK_PARSE("IndentExternBlock: AfterExternBlock", IndentExternBlock, 19267 FormatStyle::IEBS_AfterExternBlock); 19268 CHECK_PARSE("IndentExternBlock: Indent", IndentExternBlock, 19269 FormatStyle::IEBS_Indent); 19270 CHECK_PARSE("IndentExternBlock: NoIndent", IndentExternBlock, 19271 FormatStyle::IEBS_NoIndent); 19272 CHECK_PARSE("IndentExternBlock: true", IndentExternBlock, 19273 FormatStyle::IEBS_Indent); 19274 CHECK_PARSE("IndentExternBlock: false", IndentExternBlock, 19275 FormatStyle::IEBS_NoIndent); 19276 19277 Style.BitFieldColonSpacing = FormatStyle::BFCS_None; 19278 CHECK_PARSE("BitFieldColonSpacing: Both", BitFieldColonSpacing, 19279 FormatStyle::BFCS_Both); 19280 CHECK_PARSE("BitFieldColonSpacing: None", BitFieldColonSpacing, 19281 FormatStyle::BFCS_None); 19282 CHECK_PARSE("BitFieldColonSpacing: Before", BitFieldColonSpacing, 19283 FormatStyle::BFCS_Before); 19284 CHECK_PARSE("BitFieldColonSpacing: After", BitFieldColonSpacing, 19285 FormatStyle::BFCS_After); 19286 19287 Style.SortJavaStaticImport = FormatStyle::SJSIO_Before; 19288 CHECK_PARSE("SortJavaStaticImport: After", SortJavaStaticImport, 19289 FormatStyle::SJSIO_After); 19290 CHECK_PARSE("SortJavaStaticImport: Before", SortJavaStaticImport, 19291 FormatStyle::SJSIO_Before); 19292 19293 // FIXME: This is required because parsing a configuration simply overwrites 19294 // the first N elements of the list instead of resetting it. 19295 Style.ForEachMacros.clear(); 19296 std::vector<std::string> BoostForeach; 19297 BoostForeach.push_back("BOOST_FOREACH"); 19298 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 19299 std::vector<std::string> BoostAndQForeach; 19300 BoostAndQForeach.push_back("BOOST_FOREACH"); 19301 BoostAndQForeach.push_back("Q_FOREACH"); 19302 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 19303 BoostAndQForeach); 19304 19305 Style.IfMacros.clear(); 19306 std::vector<std::string> CustomIfs; 19307 CustomIfs.push_back("MYIF"); 19308 CHECK_PARSE("IfMacros: [MYIF]", IfMacros, CustomIfs); 19309 19310 Style.AttributeMacros.clear(); 19311 CHECK_PARSE("BasedOnStyle: LLVM", AttributeMacros, 19312 std::vector<std::string>{"__capability"}); 19313 CHECK_PARSE("AttributeMacros: [attr1, attr2]", AttributeMacros, 19314 std::vector<std::string>({"attr1", "attr2"})); 19315 19316 Style.StatementAttributeLikeMacros.clear(); 19317 CHECK_PARSE("StatementAttributeLikeMacros: [emit,Q_EMIT]", 19318 StatementAttributeLikeMacros, 19319 std::vector<std::string>({"emit", "Q_EMIT"})); 19320 19321 Style.StatementMacros.clear(); 19322 CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros, 19323 std::vector<std::string>{"QUNUSED"}); 19324 CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros, 19325 std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"})); 19326 19327 Style.NamespaceMacros.clear(); 19328 CHECK_PARSE("NamespaceMacros: [TESTSUITE]", NamespaceMacros, 19329 std::vector<std::string>{"TESTSUITE"}); 19330 CHECK_PARSE("NamespaceMacros: [TESTSUITE, SUITE]", NamespaceMacros, 19331 std::vector<std::string>({"TESTSUITE", "SUITE"})); 19332 19333 Style.WhitespaceSensitiveMacros.clear(); 19334 CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE]", 19335 WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"}); 19336 CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE, ASSERT]", 19337 WhitespaceSensitiveMacros, 19338 std::vector<std::string>({"STRINGIZE", "ASSERT"})); 19339 Style.WhitespaceSensitiveMacros.clear(); 19340 CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE']", 19341 WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"}); 19342 CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE', 'ASSERT']", 19343 WhitespaceSensitiveMacros, 19344 std::vector<std::string>({"STRINGIZE", "ASSERT"})); 19345 19346 Style.IncludeStyle.IncludeCategories.clear(); 19347 std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = { 19348 {"abc/.*", 2, 0, false}, {".*", 1, 0, true}}; 19349 CHECK_PARSE("IncludeCategories:\n" 19350 " - Regex: abc/.*\n" 19351 " Priority: 2\n" 19352 " - Regex: .*\n" 19353 " Priority: 1\n" 19354 " CaseSensitive: true\n", 19355 IncludeStyle.IncludeCategories, ExpectedCategories); 19356 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex, 19357 "abc$"); 19358 CHECK_PARSE("IncludeIsMainSourceRegex: 'abc$'", 19359 IncludeStyle.IncludeIsMainSourceRegex, "abc$"); 19360 19361 Style.SortIncludes = FormatStyle::SI_Never; 19362 CHECK_PARSE("SortIncludes: true", SortIncludes, 19363 FormatStyle::SI_CaseSensitive); 19364 CHECK_PARSE("SortIncludes: false", SortIncludes, FormatStyle::SI_Never); 19365 CHECK_PARSE("SortIncludes: CaseInsensitive", SortIncludes, 19366 FormatStyle::SI_CaseInsensitive); 19367 CHECK_PARSE("SortIncludes: CaseSensitive", SortIncludes, 19368 FormatStyle::SI_CaseSensitive); 19369 CHECK_PARSE("SortIncludes: Never", SortIncludes, FormatStyle::SI_Never); 19370 19371 Style.RawStringFormats.clear(); 19372 std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = { 19373 { 19374 FormatStyle::LK_TextProto, 19375 {"pb", "proto"}, 19376 {"PARSE_TEXT_PROTO"}, 19377 /*CanonicalDelimiter=*/"", 19378 "llvm", 19379 }, 19380 { 19381 FormatStyle::LK_Cpp, 19382 {"cc", "cpp"}, 19383 {"C_CODEBLOCK", "CPPEVAL"}, 19384 /*CanonicalDelimiter=*/"cc", 19385 /*BasedOnStyle=*/"", 19386 }, 19387 }; 19388 19389 CHECK_PARSE("RawStringFormats:\n" 19390 " - Language: TextProto\n" 19391 " Delimiters:\n" 19392 " - 'pb'\n" 19393 " - 'proto'\n" 19394 " EnclosingFunctions:\n" 19395 " - 'PARSE_TEXT_PROTO'\n" 19396 " BasedOnStyle: llvm\n" 19397 " - Language: Cpp\n" 19398 " Delimiters:\n" 19399 " - 'cc'\n" 19400 " - 'cpp'\n" 19401 " EnclosingFunctions:\n" 19402 " - 'C_CODEBLOCK'\n" 19403 " - 'CPPEVAL'\n" 19404 " CanonicalDelimiter: 'cc'", 19405 RawStringFormats, ExpectedRawStringFormats); 19406 19407 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 19408 " Minimum: 0\n" 19409 " Maximum: 0", 19410 SpacesInLineCommentPrefix.Minimum, 0u); 19411 EXPECT_EQ(Style.SpacesInLineCommentPrefix.Maximum, 0u); 19412 Style.SpacesInLineCommentPrefix.Minimum = 1; 19413 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 19414 " Minimum: 2", 19415 SpacesInLineCommentPrefix.Minimum, 0u); 19416 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 19417 " Maximum: -1", 19418 SpacesInLineCommentPrefix.Maximum, -1u); 19419 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 19420 " Minimum: 2", 19421 SpacesInLineCommentPrefix.Minimum, 2u); 19422 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 19423 " Maximum: 1", 19424 SpacesInLineCommentPrefix.Maximum, 1u); 19425 EXPECT_EQ(Style.SpacesInLineCommentPrefix.Minimum, 1u); 19426 19427 Style.SpacesInAngles = FormatStyle::SIAS_Always; 19428 CHECK_PARSE("SpacesInAngles: Never", SpacesInAngles, FormatStyle::SIAS_Never); 19429 CHECK_PARSE("SpacesInAngles: Always", SpacesInAngles, 19430 FormatStyle::SIAS_Always); 19431 CHECK_PARSE("SpacesInAngles: Leave", SpacesInAngles, FormatStyle::SIAS_Leave); 19432 // For backward compatibility: 19433 CHECK_PARSE("SpacesInAngles: false", SpacesInAngles, FormatStyle::SIAS_Never); 19434 CHECK_PARSE("SpacesInAngles: true", SpacesInAngles, FormatStyle::SIAS_Always); 19435 } 19436 19437 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 19438 FormatStyle Style = {}; 19439 Style.Language = FormatStyle::LK_Cpp; 19440 CHECK_PARSE("Language: Cpp\n" 19441 "IndentWidth: 12", 19442 IndentWidth, 12u); 19443 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 19444 "IndentWidth: 34", 19445 &Style), 19446 ParseError::Unsuitable); 19447 FormatStyle BinPackedTCS = {}; 19448 BinPackedTCS.Language = FormatStyle::LK_JavaScript; 19449 EXPECT_EQ(parseConfiguration("BinPackArguments: true\n" 19450 "InsertTrailingCommas: Wrapped", 19451 &BinPackedTCS), 19452 ParseError::BinPackTrailingCommaConflict); 19453 EXPECT_EQ(12u, Style.IndentWidth); 19454 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 19455 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 19456 19457 Style.Language = FormatStyle::LK_JavaScript; 19458 CHECK_PARSE("Language: JavaScript\n" 19459 "IndentWidth: 12", 19460 IndentWidth, 12u); 19461 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 19462 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 19463 "IndentWidth: 34", 19464 &Style), 19465 ParseError::Unsuitable); 19466 EXPECT_EQ(23u, Style.IndentWidth); 19467 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 19468 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 19469 19470 CHECK_PARSE("BasedOnStyle: LLVM\n" 19471 "IndentWidth: 67", 19472 IndentWidth, 67u); 19473 19474 CHECK_PARSE("---\n" 19475 "Language: JavaScript\n" 19476 "IndentWidth: 12\n" 19477 "---\n" 19478 "Language: Cpp\n" 19479 "IndentWidth: 34\n" 19480 "...\n", 19481 IndentWidth, 12u); 19482 19483 Style.Language = FormatStyle::LK_Cpp; 19484 CHECK_PARSE("---\n" 19485 "Language: JavaScript\n" 19486 "IndentWidth: 12\n" 19487 "---\n" 19488 "Language: Cpp\n" 19489 "IndentWidth: 34\n" 19490 "...\n", 19491 IndentWidth, 34u); 19492 CHECK_PARSE("---\n" 19493 "IndentWidth: 78\n" 19494 "---\n" 19495 "Language: JavaScript\n" 19496 "IndentWidth: 56\n" 19497 "...\n", 19498 IndentWidth, 78u); 19499 19500 Style.ColumnLimit = 123; 19501 Style.IndentWidth = 234; 19502 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 19503 Style.TabWidth = 345; 19504 EXPECT_FALSE(parseConfiguration("---\n" 19505 "IndentWidth: 456\n" 19506 "BreakBeforeBraces: Allman\n" 19507 "---\n" 19508 "Language: JavaScript\n" 19509 "IndentWidth: 111\n" 19510 "TabWidth: 111\n" 19511 "---\n" 19512 "Language: Cpp\n" 19513 "BreakBeforeBraces: Stroustrup\n" 19514 "TabWidth: 789\n" 19515 "...\n", 19516 &Style)); 19517 EXPECT_EQ(123u, Style.ColumnLimit); 19518 EXPECT_EQ(456u, Style.IndentWidth); 19519 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 19520 EXPECT_EQ(789u, Style.TabWidth); 19521 19522 EXPECT_EQ(parseConfiguration("---\n" 19523 "Language: JavaScript\n" 19524 "IndentWidth: 56\n" 19525 "---\n" 19526 "IndentWidth: 78\n" 19527 "...\n", 19528 &Style), 19529 ParseError::Error); 19530 EXPECT_EQ(parseConfiguration("---\n" 19531 "Language: JavaScript\n" 19532 "IndentWidth: 56\n" 19533 "---\n" 19534 "Language: JavaScript\n" 19535 "IndentWidth: 78\n" 19536 "...\n", 19537 &Style), 19538 ParseError::Error); 19539 19540 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 19541 } 19542 19543 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 19544 FormatStyle Style = {}; 19545 Style.Language = FormatStyle::LK_JavaScript; 19546 Style.BreakBeforeTernaryOperators = true; 19547 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 19548 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 19549 19550 Style.BreakBeforeTernaryOperators = true; 19551 EXPECT_EQ(0, parseConfiguration("---\n" 19552 "BasedOnStyle: Google\n" 19553 "---\n" 19554 "Language: JavaScript\n" 19555 "IndentWidth: 76\n" 19556 "...\n", 19557 &Style) 19558 .value()); 19559 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 19560 EXPECT_EQ(76u, Style.IndentWidth); 19561 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 19562 } 19563 19564 TEST_F(FormatTest, ConfigurationRoundTripTest) { 19565 FormatStyle Style = getLLVMStyle(); 19566 std::string YAML = configurationAsText(Style); 19567 FormatStyle ParsedStyle = {}; 19568 ParsedStyle.Language = FormatStyle::LK_Cpp; 19569 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 19570 EXPECT_EQ(Style, ParsedStyle); 19571 } 19572 19573 TEST_F(FormatTest, WorksFor8bitEncodings) { 19574 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 19575 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 19576 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 19577 "\"\xef\xee\xf0\xf3...\"", 19578 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 19579 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 19580 "\xef\xee\xf0\xf3...\"", 19581 getLLVMStyleWithColumns(12))); 19582 } 19583 19584 TEST_F(FormatTest, HandlesUTF8BOM) { 19585 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 19586 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 19587 format("\xef\xbb\xbf#include <iostream>")); 19588 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 19589 format("\xef\xbb\xbf\n#include <iostream>")); 19590 } 19591 19592 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 19593 #if !defined(_MSC_VER) 19594 19595 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 19596 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 19597 getLLVMStyleWithColumns(35)); 19598 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 19599 getLLVMStyleWithColumns(31)); 19600 verifyFormat("// Однажды в студёную зимнюю пору...", 19601 getLLVMStyleWithColumns(36)); 19602 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 19603 verifyFormat("/* Однажды в студёную зимнюю пору... */", 19604 getLLVMStyleWithColumns(39)); 19605 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 19606 getLLVMStyleWithColumns(35)); 19607 } 19608 19609 TEST_F(FormatTest, SplitsUTF8Strings) { 19610 // Non-printable characters' width is currently considered to be the length in 19611 // bytes in UTF8. The characters can be displayed in very different manner 19612 // (zero-width, single width with a substitution glyph, expanded to their code 19613 // (e.g. "<8d>"), so there's no single correct way to handle them. 19614 EXPECT_EQ("\"aaaaÄ\"\n" 19615 "\"\xc2\x8d\";", 19616 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 19617 EXPECT_EQ("\"aaaaaaaÄ\"\n" 19618 "\"\xc2\x8d\";", 19619 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 19620 EXPECT_EQ("\"Однажды, в \"\n" 19621 "\"студёную \"\n" 19622 "\"зимнюю \"\n" 19623 "\"пору,\"", 19624 format("\"Однажды, в студёную зимнюю пору,\"", 19625 getLLVMStyleWithColumns(13))); 19626 EXPECT_EQ( 19627 "\"一 二 三 \"\n" 19628 "\"四 五六 \"\n" 19629 "\"七 八 九 \"\n" 19630 "\"十\"", 19631 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 19632 EXPECT_EQ("\"一\t\"\n" 19633 "\"二 \t\"\n" 19634 "\"三 四 \"\n" 19635 "\"五\t\"\n" 19636 "\"六 \t\"\n" 19637 "\"七 \"\n" 19638 "\"八九十\tqq\"", 19639 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 19640 getLLVMStyleWithColumns(11))); 19641 19642 // UTF8 character in an escape sequence. 19643 EXPECT_EQ("\"aaaaaa\"\n" 19644 "\"\\\xC2\x8D\"", 19645 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 19646 } 19647 19648 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 19649 EXPECT_EQ("const char *sssss =\n" 19650 " \"一二三四五六七八\\\n" 19651 " 九 十\";", 19652 format("const char *sssss = \"一二三四五六七八\\\n" 19653 " 九 十\";", 19654 getLLVMStyleWithColumns(30))); 19655 } 19656 19657 TEST_F(FormatTest, SplitsUTF8LineComments) { 19658 EXPECT_EQ("// aaaaÄ\xc2\x8d", 19659 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 19660 EXPECT_EQ("// Я из лесу\n" 19661 "// вышел; был\n" 19662 "// сильный\n" 19663 "// мороз.", 19664 format("// Я из лесу вышел; был сильный мороз.", 19665 getLLVMStyleWithColumns(13))); 19666 EXPECT_EQ("// 一二三\n" 19667 "// 四五六七\n" 19668 "// 八 九\n" 19669 "// 十", 19670 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 19671 } 19672 19673 TEST_F(FormatTest, SplitsUTF8BlockComments) { 19674 EXPECT_EQ("/* Гляжу,\n" 19675 " * поднимается\n" 19676 " * медленно в\n" 19677 " * гору\n" 19678 " * Лошадка,\n" 19679 " * везущая\n" 19680 " * хворосту\n" 19681 " * воз. */", 19682 format("/* Гляжу, поднимается медленно в гору\n" 19683 " * Лошадка, везущая хворосту воз. */", 19684 getLLVMStyleWithColumns(13))); 19685 EXPECT_EQ( 19686 "/* 一二三\n" 19687 " * 四五六七\n" 19688 " * 八 九\n" 19689 " * 十 */", 19690 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 19691 EXPECT_EQ("/* \n" 19692 " * \n" 19693 " * - */", 19694 format("/* - */", getLLVMStyleWithColumns(12))); 19695 } 19696 19697 #endif // _MSC_VER 19698 19699 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 19700 FormatStyle Style = getLLVMStyle(); 19701 19702 Style.ConstructorInitializerIndentWidth = 4; 19703 verifyFormat( 19704 "SomeClass::Constructor()\n" 19705 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 19706 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 19707 Style); 19708 19709 Style.ConstructorInitializerIndentWidth = 2; 19710 verifyFormat( 19711 "SomeClass::Constructor()\n" 19712 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 19713 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 19714 Style); 19715 19716 Style.ConstructorInitializerIndentWidth = 0; 19717 verifyFormat( 19718 "SomeClass::Constructor()\n" 19719 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 19720 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 19721 Style); 19722 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 19723 verifyFormat( 19724 "SomeLongTemplateVariableName<\n" 19725 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 19726 Style); 19727 verifyFormat("bool smaller = 1 < " 19728 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 19729 " " 19730 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 19731 Style); 19732 19733 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 19734 verifyFormat("SomeClass::Constructor() :\n" 19735 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n" 19736 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}", 19737 Style); 19738 } 19739 19740 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 19741 FormatStyle Style = getLLVMStyle(); 19742 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 19743 Style.ConstructorInitializerIndentWidth = 4; 19744 verifyFormat("SomeClass::Constructor()\n" 19745 " : a(a)\n" 19746 " , b(b)\n" 19747 " , c(c) {}", 19748 Style); 19749 verifyFormat("SomeClass::Constructor()\n" 19750 " : a(a) {}", 19751 Style); 19752 19753 Style.ColumnLimit = 0; 19754 verifyFormat("SomeClass::Constructor()\n" 19755 " : a(a) {}", 19756 Style); 19757 verifyFormat("SomeClass::Constructor() noexcept\n" 19758 " : a(a) {}", 19759 Style); 19760 verifyFormat("SomeClass::Constructor()\n" 19761 " : a(a)\n" 19762 " , b(b)\n" 19763 " , c(c) {}", 19764 Style); 19765 verifyFormat("SomeClass::Constructor()\n" 19766 " : a(a) {\n" 19767 " foo();\n" 19768 " bar();\n" 19769 "}", 19770 Style); 19771 19772 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 19773 verifyFormat("SomeClass::Constructor()\n" 19774 " : a(a)\n" 19775 " , b(b)\n" 19776 " , c(c) {\n}", 19777 Style); 19778 verifyFormat("SomeClass::Constructor()\n" 19779 " : a(a) {\n}", 19780 Style); 19781 19782 Style.ColumnLimit = 80; 19783 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 19784 Style.ConstructorInitializerIndentWidth = 2; 19785 verifyFormat("SomeClass::Constructor()\n" 19786 " : a(a)\n" 19787 " , b(b)\n" 19788 " , c(c) {}", 19789 Style); 19790 19791 Style.ConstructorInitializerIndentWidth = 0; 19792 verifyFormat("SomeClass::Constructor()\n" 19793 ": a(a)\n" 19794 ", b(b)\n" 19795 ", c(c) {}", 19796 Style); 19797 19798 Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine; 19799 Style.ConstructorInitializerIndentWidth = 4; 19800 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 19801 verifyFormat( 19802 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 19803 Style); 19804 verifyFormat( 19805 "SomeClass::Constructor()\n" 19806 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 19807 Style); 19808 Style.ConstructorInitializerIndentWidth = 4; 19809 Style.ColumnLimit = 60; 19810 verifyFormat("SomeClass::Constructor()\n" 19811 " : aaaaaaaa(aaaaaaaa)\n" 19812 " , aaaaaaaa(aaaaaaaa)\n" 19813 " , aaaaaaaa(aaaaaaaa) {}", 19814 Style); 19815 } 19816 19817 TEST_F(FormatTest, ConstructorInitializersWithPreprocessorDirective) { 19818 FormatStyle Style = getLLVMStyle(); 19819 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 19820 Style.ConstructorInitializerIndentWidth = 4; 19821 verifyFormat("SomeClass::Constructor()\n" 19822 " : a{a}\n" 19823 " , b{b} {}", 19824 Style); 19825 verifyFormat("SomeClass::Constructor()\n" 19826 " : a{a}\n" 19827 "#if CONDITION\n" 19828 " , b{b}\n" 19829 "#endif\n" 19830 "{\n}", 19831 Style); 19832 Style.ConstructorInitializerIndentWidth = 2; 19833 verifyFormat("SomeClass::Constructor()\n" 19834 "#if CONDITION\n" 19835 " : a{a}\n" 19836 "#endif\n" 19837 " , b{b}\n" 19838 " , c{c} {\n}", 19839 Style); 19840 Style.ConstructorInitializerIndentWidth = 0; 19841 verifyFormat("SomeClass::Constructor()\n" 19842 ": a{a}\n" 19843 "#ifdef CONDITION\n" 19844 ", b{b}\n" 19845 "#else\n" 19846 ", c{c}\n" 19847 "#endif\n" 19848 ", d{d} {\n}", 19849 Style); 19850 Style.ConstructorInitializerIndentWidth = 4; 19851 verifyFormat("SomeClass::Constructor()\n" 19852 " : a{a}\n" 19853 "#if WINDOWS\n" 19854 "#if DEBUG\n" 19855 " , b{0}\n" 19856 "#else\n" 19857 " , b{1}\n" 19858 "#endif\n" 19859 "#else\n" 19860 "#if DEBUG\n" 19861 " , b{2}\n" 19862 "#else\n" 19863 " , b{3}\n" 19864 "#endif\n" 19865 "#endif\n" 19866 "{\n}", 19867 Style); 19868 verifyFormat("SomeClass::Constructor()\n" 19869 " : a{a}\n" 19870 "#if WINDOWS\n" 19871 " , b{0}\n" 19872 "#if DEBUG\n" 19873 " , c{0}\n" 19874 "#else\n" 19875 " , c{1}\n" 19876 "#endif\n" 19877 "#else\n" 19878 "#if DEBUG\n" 19879 " , c{2}\n" 19880 "#else\n" 19881 " , c{3}\n" 19882 "#endif\n" 19883 " , b{1}\n" 19884 "#endif\n" 19885 "{\n}", 19886 Style); 19887 } 19888 19889 TEST_F(FormatTest, Destructors) { 19890 verifyFormat("void F(int &i) { i.~int(); }"); 19891 verifyFormat("void F(int &i) { i->~int(); }"); 19892 } 19893 19894 TEST_F(FormatTest, FormatsWithWebKitStyle) { 19895 FormatStyle Style = getWebKitStyle(); 19896 19897 // Don't indent in outer namespaces. 19898 verifyFormat("namespace outer {\n" 19899 "int i;\n" 19900 "namespace inner {\n" 19901 " int i;\n" 19902 "} // namespace inner\n" 19903 "} // namespace outer\n" 19904 "namespace other_outer {\n" 19905 "int i;\n" 19906 "}", 19907 Style); 19908 19909 // Don't indent case labels. 19910 verifyFormat("switch (variable) {\n" 19911 "case 1:\n" 19912 "case 2:\n" 19913 " doSomething();\n" 19914 " break;\n" 19915 "default:\n" 19916 " ++variable;\n" 19917 "}", 19918 Style); 19919 19920 // Wrap before binary operators. 19921 EXPECT_EQ("void f()\n" 19922 "{\n" 19923 " if (aaaaaaaaaaaaaaaa\n" 19924 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 19925 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 19926 " return;\n" 19927 "}", 19928 format("void f() {\n" 19929 "if (aaaaaaaaaaaaaaaa\n" 19930 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 19931 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 19932 "return;\n" 19933 "}", 19934 Style)); 19935 19936 // Allow functions on a single line. 19937 verifyFormat("void f() { return; }", Style); 19938 19939 // Allow empty blocks on a single line and insert a space in empty blocks. 19940 EXPECT_EQ("void f() { }", format("void f() {}", Style)); 19941 EXPECT_EQ("while (true) { }", format("while (true) {}", Style)); 19942 // However, don't merge non-empty short loops. 19943 EXPECT_EQ("while (true) {\n" 19944 " continue;\n" 19945 "}", 19946 format("while (true) { continue; }", Style)); 19947 19948 // Constructor initializers are formatted one per line with the "," on the 19949 // new line. 19950 verifyFormat("Constructor()\n" 19951 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 19952 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 19953 " aaaaaaaaaaaaaa)\n" 19954 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 19955 "{\n" 19956 "}", 19957 Style); 19958 verifyFormat("SomeClass::Constructor()\n" 19959 " : a(a)\n" 19960 "{\n" 19961 "}", 19962 Style); 19963 EXPECT_EQ("SomeClass::Constructor()\n" 19964 " : a(a)\n" 19965 "{\n" 19966 "}", 19967 format("SomeClass::Constructor():a(a){}", Style)); 19968 verifyFormat("SomeClass::Constructor()\n" 19969 " : a(a)\n" 19970 " , b(b)\n" 19971 " , c(c)\n" 19972 "{\n" 19973 "}", 19974 Style); 19975 verifyFormat("SomeClass::Constructor()\n" 19976 " : a(a)\n" 19977 "{\n" 19978 " foo();\n" 19979 " bar();\n" 19980 "}", 19981 Style); 19982 19983 // Access specifiers should be aligned left. 19984 verifyFormat("class C {\n" 19985 "public:\n" 19986 " int i;\n" 19987 "};", 19988 Style); 19989 19990 // Do not align comments. 19991 verifyFormat("int a; // Do not\n" 19992 "double b; // align comments.", 19993 Style); 19994 19995 // Do not align operands. 19996 EXPECT_EQ("ASSERT(aaaa\n" 19997 " || bbbb);", 19998 format("ASSERT ( aaaa\n||bbbb);", Style)); 19999 20000 // Accept input's line breaks. 20001 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 20002 " || bbbbbbbbbbbbbbb) {\n" 20003 " i++;\n" 20004 "}", 20005 format("if (aaaaaaaaaaaaaaa\n" 20006 "|| bbbbbbbbbbbbbbb) { i++; }", 20007 Style)); 20008 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 20009 " i++;\n" 20010 "}", 20011 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 20012 20013 // Don't automatically break all macro definitions (llvm.org/PR17842). 20014 verifyFormat("#define aNumber 10", Style); 20015 // However, generally keep the line breaks that the user authored. 20016 EXPECT_EQ("#define aNumber \\\n" 20017 " 10", 20018 format("#define aNumber \\\n" 20019 " 10", 20020 Style)); 20021 20022 // Keep empty and one-element array literals on a single line. 20023 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 20024 " copyItems:YES];", 20025 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 20026 "copyItems:YES];", 20027 Style)); 20028 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 20029 " copyItems:YES];", 20030 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 20031 " copyItems:YES];", 20032 Style)); 20033 // FIXME: This does not seem right, there should be more indentation before 20034 // the array literal's entries. Nested blocks have the same problem. 20035 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 20036 " @\"a\",\n" 20037 " @\"a\"\n" 20038 "]\n" 20039 " copyItems:YES];", 20040 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 20041 " @\"a\",\n" 20042 " @\"a\"\n" 20043 " ]\n" 20044 " copyItems:YES];", 20045 Style)); 20046 EXPECT_EQ( 20047 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 20048 " copyItems:YES];", 20049 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 20050 " copyItems:YES];", 20051 Style)); 20052 20053 verifyFormat("[self.a b:c c:d];", Style); 20054 EXPECT_EQ("[self.a b:c\n" 20055 " c:d];", 20056 format("[self.a b:c\n" 20057 "c:d];", 20058 Style)); 20059 } 20060 20061 TEST_F(FormatTest, FormatsLambdas) { 20062 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 20063 verifyFormat( 20064 "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();\n"); 20065 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 20066 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 20067 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 20068 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 20069 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 20070 verifyFormat("auto c = [a = [b = 42] {}] {};\n"); 20071 verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n"); 20072 verifyFormat("int x = f(*+[] {});"); 20073 verifyFormat("void f() {\n" 20074 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 20075 "}\n"); 20076 verifyFormat("void f() {\n" 20077 " other(x.begin(), //\n" 20078 " x.end(), //\n" 20079 " [&](int, int) { return 1; });\n" 20080 "}\n"); 20081 verifyFormat("void f() {\n" 20082 " other.other.other.other.other(\n" 20083 " x.begin(), x.end(),\n" 20084 " [something, rather](int, int, int, int, int, int, int) { " 20085 "return 1; });\n" 20086 "}\n"); 20087 verifyFormat( 20088 "void f() {\n" 20089 " other.other.other.other.other(\n" 20090 " x.begin(), x.end(),\n" 20091 " [something, rather](int, int, int, int, int, int, int) {\n" 20092 " //\n" 20093 " });\n" 20094 "}\n"); 20095 verifyFormat("SomeFunction([]() { // A cool function...\n" 20096 " return 43;\n" 20097 "});"); 20098 EXPECT_EQ("SomeFunction([]() {\n" 20099 "#define A a\n" 20100 " return 43;\n" 20101 "});", 20102 format("SomeFunction([](){\n" 20103 "#define A a\n" 20104 "return 43;\n" 20105 "});")); 20106 verifyFormat("void f() {\n" 20107 " SomeFunction([](decltype(x), A *a) {});\n" 20108 " SomeFunction([](typeof(x), A *a) {});\n" 20109 " SomeFunction([](_Atomic(x), A *a) {});\n" 20110 " SomeFunction([](__underlying_type(x), A *a) {});\n" 20111 "}"); 20112 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 20113 " [](const aaaaaaaaaa &a) { return a; });"); 20114 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 20115 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 20116 "});"); 20117 verifyFormat("Constructor()\n" 20118 " : Field([] { // comment\n" 20119 " int i;\n" 20120 " }) {}"); 20121 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 20122 " return some_parameter.size();\n" 20123 "};"); 20124 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 20125 " [](const string &s) { return s; };"); 20126 verifyFormat("int i = aaaaaa ? 1 //\n" 20127 " : [] {\n" 20128 " return 2; //\n" 20129 " }();"); 20130 verifyFormat("llvm::errs() << \"number of twos is \"\n" 20131 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 20132 " return x == 2; // force break\n" 20133 " });"); 20134 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 20135 " [=](int iiiiiiiiiiii) {\n" 20136 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n" 20137 " aaaaaaaaaaaaaaaaaaaaaaa;\n" 20138 " });", 20139 getLLVMStyleWithColumns(60)); 20140 20141 verifyFormat("SomeFunction({[&] {\n" 20142 " // comment\n" 20143 " },\n" 20144 " [&] {\n" 20145 " // comment\n" 20146 " }});"); 20147 verifyFormat("SomeFunction({[&] {\n" 20148 " // comment\n" 20149 "}});"); 20150 verifyFormat( 20151 "virtual aaaaaaaaaaaaaaaa(\n" 20152 " std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n" 20153 " aaaaa aaaaaaaaa);"); 20154 20155 // Lambdas with return types. 20156 verifyFormat("int c = []() -> int { return 2; }();\n"); 20157 verifyFormat("int c = []() -> int * { return 2; }();\n"); 20158 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 20159 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 20160 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 20161 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 20162 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 20163 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 20164 verifyFormat("[a, a]() -> a<1> {};"); 20165 verifyFormat("[]() -> foo<5 + 2> { return {}; };"); 20166 verifyFormat("[]() -> foo<5 - 2> { return {}; };"); 20167 verifyFormat("[]() -> foo<5 / 2> { return {}; };"); 20168 verifyFormat("[]() -> foo<5 * 2> { return {}; };"); 20169 verifyFormat("[]() -> foo<5 % 2> { return {}; };"); 20170 verifyFormat("[]() -> foo<5 << 2> { return {}; };"); 20171 verifyFormat("[]() -> foo<!5> { return {}; };"); 20172 verifyFormat("[]() -> foo<~5> { return {}; };"); 20173 verifyFormat("[]() -> foo<5 | 2> { return {}; };"); 20174 verifyFormat("[]() -> foo<5 || 2> { return {}; };"); 20175 verifyFormat("[]() -> foo<5 & 2> { return {}; };"); 20176 verifyFormat("[]() -> foo<5 && 2> { return {}; };"); 20177 verifyFormat("[]() -> foo<5 == 2> { return {}; };"); 20178 verifyFormat("[]() -> foo<5 != 2> { return {}; };"); 20179 verifyFormat("[]() -> foo<5 >= 2> { return {}; };"); 20180 verifyFormat("[]() -> foo<5 <= 2> { return {}; };"); 20181 verifyFormat("[]() -> foo<5 < 2> { return {}; };"); 20182 verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };"); 20183 verifyFormat("namespace bar {\n" 20184 "// broken:\n" 20185 "auto foo{[]() -> foo<5 + 2> { return {}; }};\n" 20186 "} // namespace bar"); 20187 verifyFormat("namespace bar {\n" 20188 "// broken:\n" 20189 "auto foo{[]() -> foo<5 - 2> { return {}; }};\n" 20190 "} // namespace bar"); 20191 verifyFormat("namespace bar {\n" 20192 "// broken:\n" 20193 "auto foo{[]() -> foo<5 / 2> { return {}; }};\n" 20194 "} // namespace bar"); 20195 verifyFormat("namespace bar {\n" 20196 "// broken:\n" 20197 "auto foo{[]() -> foo<5 * 2> { return {}; }};\n" 20198 "} // namespace bar"); 20199 verifyFormat("namespace bar {\n" 20200 "// broken:\n" 20201 "auto foo{[]() -> foo<5 % 2> { return {}; }};\n" 20202 "} // namespace bar"); 20203 verifyFormat("namespace bar {\n" 20204 "// broken:\n" 20205 "auto foo{[]() -> foo<5 << 2> { return {}; }};\n" 20206 "} // namespace bar"); 20207 verifyFormat("namespace bar {\n" 20208 "// broken:\n" 20209 "auto foo{[]() -> foo<!5> { return {}; }};\n" 20210 "} // namespace bar"); 20211 verifyFormat("namespace bar {\n" 20212 "// broken:\n" 20213 "auto foo{[]() -> foo<~5> { return {}; }};\n" 20214 "} // namespace bar"); 20215 verifyFormat("namespace bar {\n" 20216 "// broken:\n" 20217 "auto foo{[]() -> foo<5 | 2> { return {}; }};\n" 20218 "} // namespace bar"); 20219 verifyFormat("namespace bar {\n" 20220 "// broken:\n" 20221 "auto foo{[]() -> foo<5 || 2> { return {}; }};\n" 20222 "} // namespace bar"); 20223 verifyFormat("namespace bar {\n" 20224 "// broken:\n" 20225 "auto foo{[]() -> foo<5 & 2> { return {}; }};\n" 20226 "} // namespace bar"); 20227 verifyFormat("namespace bar {\n" 20228 "// broken:\n" 20229 "auto foo{[]() -> foo<5 && 2> { return {}; }};\n" 20230 "} // namespace bar"); 20231 verifyFormat("namespace bar {\n" 20232 "// broken:\n" 20233 "auto foo{[]() -> foo<5 == 2> { return {}; }};\n" 20234 "} // namespace bar"); 20235 verifyFormat("namespace bar {\n" 20236 "// broken:\n" 20237 "auto foo{[]() -> foo<5 != 2> { return {}; }};\n" 20238 "} // namespace bar"); 20239 verifyFormat("namespace bar {\n" 20240 "// broken:\n" 20241 "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n" 20242 "} // namespace bar"); 20243 verifyFormat("namespace bar {\n" 20244 "// broken:\n" 20245 "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n" 20246 "} // namespace bar"); 20247 verifyFormat("namespace bar {\n" 20248 "// broken:\n" 20249 "auto foo{[]() -> foo<5 < 2> { return {}; }};\n" 20250 "} // namespace bar"); 20251 verifyFormat("namespace bar {\n" 20252 "// broken:\n" 20253 "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n" 20254 "} // namespace bar"); 20255 verifyFormat("[]() -> a<1> {};"); 20256 verifyFormat("[]() -> a<1> { ; };"); 20257 verifyFormat("[]() -> a<1> { ; }();"); 20258 verifyFormat("[a, a]() -> a<true> {};"); 20259 verifyFormat("[]() -> a<true> {};"); 20260 verifyFormat("[]() -> a<true> { ; };"); 20261 verifyFormat("[]() -> a<true> { ; }();"); 20262 verifyFormat("[a, a]() -> a<false> {};"); 20263 verifyFormat("[]() -> a<false> {};"); 20264 verifyFormat("[]() -> a<false> { ; };"); 20265 verifyFormat("[]() -> a<false> { ; }();"); 20266 verifyFormat("auto foo{[]() -> foo<false> { ; }};"); 20267 verifyFormat("namespace bar {\n" 20268 "auto foo{[]() -> foo<false> { ; }};\n" 20269 "} // namespace bar"); 20270 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 20271 " int j) -> int {\n" 20272 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 20273 "};"); 20274 verifyFormat( 20275 "aaaaaaaaaaaaaaaaaaaaaa(\n" 20276 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 20277 " return aaaaaaaaaaaaaaaaa;\n" 20278 " });", 20279 getLLVMStyleWithColumns(70)); 20280 verifyFormat("[]() //\n" 20281 " -> int {\n" 20282 " return 1; //\n" 20283 "};"); 20284 verifyFormat("[]() -> Void<T...> {};"); 20285 verifyFormat("[a, b]() -> Tuple<T...> { return {}; };"); 20286 verifyFormat("SomeFunction({[]() -> int[] { return {}; }});"); 20287 verifyFormat("SomeFunction({[]() -> int *[] { return {}; }});"); 20288 verifyFormat("SomeFunction({[]() -> int (*)[] { return {}; }});"); 20289 verifyFormat("SomeFunction({[]() -> ns::type<int (*)[]> { return {}; }});"); 20290 verifyFormat("return int{[x = x]() { return x; }()};"); 20291 20292 // Lambdas with explicit template argument lists. 20293 verifyFormat( 20294 "auto L = []<template <typename> class T, class U>(T<U> &&a) {};\n"); 20295 20296 // Multiple lambdas in the same parentheses change indentation rules. These 20297 // lambdas are forced to start on new lines. 20298 verifyFormat("SomeFunction(\n" 20299 " []() {\n" 20300 " //\n" 20301 " },\n" 20302 " []() {\n" 20303 " //\n" 20304 " });"); 20305 20306 // A lambda passed as arg0 is always pushed to the next line. 20307 verifyFormat("SomeFunction(\n" 20308 " [this] {\n" 20309 " //\n" 20310 " },\n" 20311 " 1);\n"); 20312 20313 // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like 20314 // the arg0 case above. 20315 auto Style = getGoogleStyle(); 20316 Style.BinPackArguments = false; 20317 verifyFormat("SomeFunction(\n" 20318 " a,\n" 20319 " [this] {\n" 20320 " //\n" 20321 " },\n" 20322 " b);\n", 20323 Style); 20324 verifyFormat("SomeFunction(\n" 20325 " a,\n" 20326 " [this] {\n" 20327 " //\n" 20328 " },\n" 20329 " b);\n"); 20330 20331 // A lambda with a very long line forces arg0 to be pushed out irrespective of 20332 // the BinPackArguments value (as long as the code is wide enough). 20333 verifyFormat( 20334 "something->SomeFunction(\n" 20335 " a,\n" 20336 " [this] {\n" 20337 " " 20338 "D0000000000000000000000000000000000000000000000000000000000001();\n" 20339 " },\n" 20340 " b);\n"); 20341 20342 // A multi-line lambda is pulled up as long as the introducer fits on the 20343 // previous line and there are no further args. 20344 verifyFormat("function(1, [this, that] {\n" 20345 " //\n" 20346 "});\n"); 20347 verifyFormat("function([this, that] {\n" 20348 " //\n" 20349 "});\n"); 20350 // FIXME: this format is not ideal and we should consider forcing the first 20351 // arg onto its own line. 20352 verifyFormat("function(a, b, c, //\n" 20353 " d, [this, that] {\n" 20354 " //\n" 20355 " });\n"); 20356 20357 // Multiple lambdas are treated correctly even when there is a short arg0. 20358 verifyFormat("SomeFunction(\n" 20359 " 1,\n" 20360 " [this] {\n" 20361 " //\n" 20362 " },\n" 20363 " [this] {\n" 20364 " //\n" 20365 " },\n" 20366 " 1);\n"); 20367 20368 // More complex introducers. 20369 verifyFormat("return [i, args...] {};"); 20370 20371 // Not lambdas. 20372 verifyFormat("constexpr char hello[]{\"hello\"};"); 20373 verifyFormat("double &operator[](int i) { return 0; }\n" 20374 "int i;"); 20375 verifyFormat("std::unique_ptr<int[]> foo() {}"); 20376 verifyFormat("int i = a[a][a]->f();"); 20377 verifyFormat("int i = (*b)[a]->f();"); 20378 20379 // Other corner cases. 20380 verifyFormat("void f() {\n" 20381 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 20382 " );\n" 20383 "}"); 20384 20385 // Lambdas created through weird macros. 20386 verifyFormat("void f() {\n" 20387 " MACRO((const AA &a) { return 1; });\n" 20388 " MACRO((AA &a) { return 1; });\n" 20389 "}"); 20390 20391 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 20392 " doo_dah();\n" 20393 " doo_dah();\n" 20394 " })) {\n" 20395 "}"); 20396 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n" 20397 " doo_dah();\n" 20398 " doo_dah();\n" 20399 " })) {\n" 20400 "}"); 20401 verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n" 20402 " doo_dah();\n" 20403 " doo_dah();\n" 20404 " })) {\n" 20405 "}"); 20406 verifyFormat("auto lambda = []() {\n" 20407 " int a = 2\n" 20408 "#if A\n" 20409 " + 2\n" 20410 "#endif\n" 20411 " ;\n" 20412 "};"); 20413 20414 // Lambdas with complex multiline introducers. 20415 verifyFormat( 20416 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 20417 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n" 20418 " -> ::std::unordered_set<\n" 20419 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 20420 " //\n" 20421 " });"); 20422 20423 FormatStyle DoNotMerge = getLLVMStyle(); 20424 DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 20425 verifyFormat("auto c = []() {\n" 20426 " return b;\n" 20427 "};", 20428 "auto c = []() { return b; };", DoNotMerge); 20429 verifyFormat("auto c = []() {\n" 20430 "};", 20431 " auto c = []() {};", DoNotMerge); 20432 20433 FormatStyle MergeEmptyOnly = getLLVMStyle(); 20434 MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty; 20435 verifyFormat("auto c = []() {\n" 20436 " return b;\n" 20437 "};", 20438 "auto c = []() {\n" 20439 " return b;\n" 20440 " };", 20441 MergeEmptyOnly); 20442 verifyFormat("auto c = []() {};", 20443 "auto c = []() {\n" 20444 "};", 20445 MergeEmptyOnly); 20446 20447 FormatStyle MergeInline = getLLVMStyle(); 20448 MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline; 20449 verifyFormat("auto c = []() {\n" 20450 " return b;\n" 20451 "};", 20452 "auto c = []() { return b; };", MergeInline); 20453 verifyFormat("function([]() { return b; })", "function([]() { return b; })", 20454 MergeInline); 20455 verifyFormat("function([]() { return b; }, a)", 20456 "function([]() { return b; }, a)", MergeInline); 20457 verifyFormat("function(a, []() { return b; })", 20458 "function(a, []() { return b; })", MergeInline); 20459 20460 // Check option "BraceWrapping.BeforeLambdaBody" and different state of 20461 // AllowShortLambdasOnASingleLine 20462 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle(); 20463 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 20464 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 20465 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20466 FormatStyle::ShortLambdaStyle::SLS_None; 20467 verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n" 20468 " []()\n" 20469 " {\n" 20470 " return 17;\n" 20471 " });", 20472 LLVMWithBeforeLambdaBody); 20473 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n" 20474 " []()\n" 20475 " {\n" 20476 " });", 20477 LLVMWithBeforeLambdaBody); 20478 verifyFormat("auto fct_SLS_None = []()\n" 20479 "{\n" 20480 " return 17;\n" 20481 "};", 20482 LLVMWithBeforeLambdaBody); 20483 verifyFormat("TwoNestedLambdas_SLS_None(\n" 20484 " []()\n" 20485 " {\n" 20486 " return Call(\n" 20487 " []()\n" 20488 " {\n" 20489 " return 17;\n" 20490 " });\n" 20491 " });", 20492 LLVMWithBeforeLambdaBody); 20493 verifyFormat("void Fct() {\n" 20494 " return {[]()\n" 20495 " {\n" 20496 " return 17;\n" 20497 " }};\n" 20498 "}", 20499 LLVMWithBeforeLambdaBody); 20500 20501 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20502 FormatStyle::ShortLambdaStyle::SLS_Empty; 20503 verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n" 20504 " []()\n" 20505 " {\n" 20506 " return 17;\n" 20507 " });", 20508 LLVMWithBeforeLambdaBody); 20509 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});", 20510 LLVMWithBeforeLambdaBody); 20511 verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL" 20512 "ongFunctionName_SLS_Empty(\n" 20513 " []() {});", 20514 LLVMWithBeforeLambdaBody); 20515 verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n" 20516 " []()\n" 20517 " {\n" 20518 " return 17;\n" 20519 " });", 20520 LLVMWithBeforeLambdaBody); 20521 verifyFormat("auto fct_SLS_Empty = []()\n" 20522 "{\n" 20523 " return 17;\n" 20524 "};", 20525 LLVMWithBeforeLambdaBody); 20526 verifyFormat("TwoNestedLambdas_SLS_Empty(\n" 20527 " []()\n" 20528 " {\n" 20529 " return Call([]() {});\n" 20530 " });", 20531 LLVMWithBeforeLambdaBody); 20532 verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n" 20533 " []()\n" 20534 " {\n" 20535 " return Call([]() {});\n" 20536 " });", 20537 LLVMWithBeforeLambdaBody); 20538 verifyFormat( 20539 "FctWithLongLineInLambda_SLS_Empty(\n" 20540 " []()\n" 20541 " {\n" 20542 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 20543 " AndShouldNotBeConsiderAsInline,\n" 20544 " LambdaBodyMustBeBreak);\n" 20545 " });", 20546 LLVMWithBeforeLambdaBody); 20547 20548 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20549 FormatStyle::ShortLambdaStyle::SLS_Inline; 20550 verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });", 20551 LLVMWithBeforeLambdaBody); 20552 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});", 20553 LLVMWithBeforeLambdaBody); 20554 verifyFormat("auto fct_SLS_Inline = []()\n" 20555 "{\n" 20556 " return 17;\n" 20557 "};", 20558 LLVMWithBeforeLambdaBody); 20559 verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return " 20560 "17; }); });", 20561 LLVMWithBeforeLambdaBody); 20562 verifyFormat( 20563 "FctWithLongLineInLambda_SLS_Inline(\n" 20564 " []()\n" 20565 " {\n" 20566 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 20567 " AndShouldNotBeConsiderAsInline,\n" 20568 " LambdaBodyMustBeBreak);\n" 20569 " });", 20570 LLVMWithBeforeLambdaBody); 20571 verifyFormat("FctWithMultipleParams_SLS_Inline(" 20572 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n" 20573 " []() { return 17; });", 20574 LLVMWithBeforeLambdaBody); 20575 verifyFormat( 20576 "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });", 20577 LLVMWithBeforeLambdaBody); 20578 20579 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20580 FormatStyle::ShortLambdaStyle::SLS_All; 20581 verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });", 20582 LLVMWithBeforeLambdaBody); 20583 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});", 20584 LLVMWithBeforeLambdaBody); 20585 verifyFormat("auto fct_SLS_All = []() { return 17; };", 20586 LLVMWithBeforeLambdaBody); 20587 verifyFormat("FctWithOneParam_SLS_All(\n" 20588 " []()\n" 20589 " {\n" 20590 " // A cool function...\n" 20591 " return 43;\n" 20592 " });", 20593 LLVMWithBeforeLambdaBody); 20594 verifyFormat("FctWithMultipleParams_SLS_All(" 20595 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n" 20596 " []() { return 17; });", 20597 LLVMWithBeforeLambdaBody); 20598 verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });", 20599 LLVMWithBeforeLambdaBody); 20600 verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });", 20601 LLVMWithBeforeLambdaBody); 20602 verifyFormat( 20603 "FctWithLongLineInLambda_SLS_All(\n" 20604 " []()\n" 20605 " {\n" 20606 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 20607 " AndShouldNotBeConsiderAsInline,\n" 20608 " LambdaBodyMustBeBreak);\n" 20609 " });", 20610 LLVMWithBeforeLambdaBody); 20611 verifyFormat( 20612 "auto fct_SLS_All = []()\n" 20613 "{\n" 20614 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 20615 " AndShouldNotBeConsiderAsInline,\n" 20616 " LambdaBodyMustBeBreak);\n" 20617 "};", 20618 LLVMWithBeforeLambdaBody); 20619 LLVMWithBeforeLambdaBody.BinPackParameters = false; 20620 verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);", 20621 LLVMWithBeforeLambdaBody); 20622 verifyFormat( 20623 "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n" 20624 " FirstParam,\n" 20625 " SecondParam,\n" 20626 " ThirdParam,\n" 20627 " FourthParam);", 20628 LLVMWithBeforeLambdaBody); 20629 verifyFormat("FctWithLongLineInLambda_SLS_All(\n" 20630 " []() { return " 20631 "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n" 20632 " FirstParam,\n" 20633 " SecondParam,\n" 20634 " ThirdParam,\n" 20635 " FourthParam);", 20636 LLVMWithBeforeLambdaBody); 20637 verifyFormat( 20638 "FctWithLongLineInLambda_SLS_All(FirstParam,\n" 20639 " SecondParam,\n" 20640 " ThirdParam,\n" 20641 " FourthParam,\n" 20642 " []() { return SomeValueNotSoLong; });", 20643 LLVMWithBeforeLambdaBody); 20644 verifyFormat("FctWithLongLineInLambda_SLS_All(\n" 20645 " []()\n" 20646 " {\n" 20647 " return " 20648 "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB" 20649 "eConsiderAsInline;\n" 20650 " });", 20651 LLVMWithBeforeLambdaBody); 20652 verifyFormat( 20653 "FctWithLongLineInLambda_SLS_All(\n" 20654 " []()\n" 20655 " {\n" 20656 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 20657 " AndShouldNotBeConsiderAsInline,\n" 20658 " LambdaBodyMustBeBreak);\n" 20659 " });", 20660 LLVMWithBeforeLambdaBody); 20661 verifyFormat("FctWithTwoParams_SLS_All(\n" 20662 " []()\n" 20663 " {\n" 20664 " // A cool function...\n" 20665 " return 43;\n" 20666 " },\n" 20667 " 87);", 20668 LLVMWithBeforeLambdaBody); 20669 verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);", 20670 LLVMWithBeforeLambdaBody); 20671 verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });", 20672 LLVMWithBeforeLambdaBody); 20673 verifyFormat( 20674 "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });", 20675 LLVMWithBeforeLambdaBody); 20676 verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; " 20677 "}); }, x);", 20678 LLVMWithBeforeLambdaBody); 20679 verifyFormat("TwoNestedLambdas_SLS_All(\n" 20680 " []()\n" 20681 " {\n" 20682 " // A cool function...\n" 20683 " return Call([]() { return 17; });\n" 20684 " });", 20685 LLVMWithBeforeLambdaBody); 20686 verifyFormat("TwoNestedLambdas_SLS_All(\n" 20687 " []()\n" 20688 " {\n" 20689 " return Call(\n" 20690 " []()\n" 20691 " {\n" 20692 " // A cool function...\n" 20693 " return 17;\n" 20694 " });\n" 20695 " });", 20696 LLVMWithBeforeLambdaBody); 20697 20698 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20699 FormatStyle::ShortLambdaStyle::SLS_None; 20700 20701 verifyFormat("auto select = [this]() -> const Library::Object *\n" 20702 "{\n" 20703 " return MyAssignment::SelectFromList(this);\n" 20704 "};\n", 20705 LLVMWithBeforeLambdaBody); 20706 20707 verifyFormat("auto select = [this]() -> const Library::Object &\n" 20708 "{\n" 20709 " return MyAssignment::SelectFromList(this);\n" 20710 "};\n", 20711 LLVMWithBeforeLambdaBody); 20712 20713 verifyFormat("auto select = [this]() -> std::unique_ptr<Object>\n" 20714 "{\n" 20715 " return MyAssignment::SelectFromList(this);\n" 20716 "};\n", 20717 LLVMWithBeforeLambdaBody); 20718 20719 verifyFormat("namespace test {\n" 20720 "class Test {\n" 20721 "public:\n" 20722 " Test() = default;\n" 20723 "};\n" 20724 "} // namespace test", 20725 LLVMWithBeforeLambdaBody); 20726 20727 // Lambdas with different indentation styles. 20728 Style = getLLVMStyleWithColumns(100); 20729 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 20730 " return promise.then(\n" 20731 " [this, &someVariable, someObject = " 20732 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 20733 " return someObject.startAsyncAction().then(\n" 20734 " [this, &someVariable](AsyncActionResult result) " 20735 "mutable { result.processMore(); });\n" 20736 " });\n" 20737 "}\n", 20738 format("SomeResult doSomething(SomeObject promise) {\n" 20739 " return promise.then([this, &someVariable, someObject = " 20740 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 20741 " return someObject.startAsyncAction().then([this, " 20742 "&someVariable](AsyncActionResult result) mutable {\n" 20743 " result.processMore();\n" 20744 " });\n" 20745 " });\n" 20746 "}\n", 20747 Style)); 20748 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope; 20749 verifyFormat("test() {\n" 20750 " ([]() -> {\n" 20751 " int b = 32;\n" 20752 " return 3;\n" 20753 " }).foo();\n" 20754 "}", 20755 Style); 20756 verifyFormat("test() {\n" 20757 " []() -> {\n" 20758 " int b = 32;\n" 20759 " return 3;\n" 20760 " }\n" 20761 "}", 20762 Style); 20763 verifyFormat("std::sort(v.begin(), v.end(),\n" 20764 " [](const auto &someLongArgumentName, const auto " 20765 "&someOtherLongArgumentName) {\n" 20766 " return someLongArgumentName.someMemberVariable < " 20767 "someOtherLongArgumentName.someMemberVariable;\n" 20768 "});", 20769 Style); 20770 verifyFormat("test() {\n" 20771 " (\n" 20772 " []() -> {\n" 20773 " int b = 32;\n" 20774 " return 3;\n" 20775 " },\n" 20776 " foo, bar)\n" 20777 " .foo();\n" 20778 "}", 20779 Style); 20780 verifyFormat("test() {\n" 20781 " ([]() -> {\n" 20782 " int b = 32;\n" 20783 " return 3;\n" 20784 " })\n" 20785 " .foo()\n" 20786 " .bar();\n" 20787 "}", 20788 Style); 20789 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 20790 " return promise.then(\n" 20791 " [this, &someVariable, someObject = " 20792 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 20793 " return someObject.startAsyncAction().then(\n" 20794 " [this, &someVariable](AsyncActionResult result) mutable { " 20795 "result.processMore(); });\n" 20796 " });\n" 20797 "}\n", 20798 format("SomeResult doSomething(SomeObject promise) {\n" 20799 " return promise.then([this, &someVariable, someObject = " 20800 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 20801 " return someObject.startAsyncAction().then([this, " 20802 "&someVariable](AsyncActionResult result) mutable {\n" 20803 " result.processMore();\n" 20804 " });\n" 20805 " });\n" 20806 "}\n", 20807 Style)); 20808 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 20809 " return promise.then([this, &someVariable] {\n" 20810 " return someObject.startAsyncAction().then(\n" 20811 " [this, &someVariable](AsyncActionResult result) mutable { " 20812 "result.processMore(); });\n" 20813 " });\n" 20814 "}\n", 20815 format("SomeResult doSomething(SomeObject promise) {\n" 20816 " return promise.then([this, &someVariable] {\n" 20817 " return someObject.startAsyncAction().then([this, " 20818 "&someVariable](AsyncActionResult result) mutable {\n" 20819 " result.processMore();\n" 20820 " });\n" 20821 " });\n" 20822 "}\n", 20823 Style)); 20824 Style = getGoogleStyle(); 20825 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope; 20826 EXPECT_EQ("#define A \\\n" 20827 " [] { \\\n" 20828 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 20829 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 20830 " }", 20831 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 20832 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 20833 Style)); 20834 // TODO: The current formatting has a minor issue that's not worth fixing 20835 // right now whereby the closing brace is indented relative to the signature 20836 // instead of being aligned. This only happens with macros. 20837 } 20838 20839 TEST_F(FormatTest, LambdaWithLineComments) { 20840 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle(); 20841 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 20842 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 20843 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 20844 FormatStyle::ShortLambdaStyle::SLS_All; 20845 20846 verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody); 20847 verifyFormat("auto k = []() // comment\n" 20848 "{ return; }", 20849 LLVMWithBeforeLambdaBody); 20850 verifyFormat("auto k = []() /* comment */ { return; }", 20851 LLVMWithBeforeLambdaBody); 20852 verifyFormat("auto k = []() /* comment */ /* comment */ { return; }", 20853 LLVMWithBeforeLambdaBody); 20854 verifyFormat("auto k = []() // X\n" 20855 "{ return; }", 20856 LLVMWithBeforeLambdaBody); 20857 verifyFormat( 20858 "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n" 20859 "{ return; }", 20860 LLVMWithBeforeLambdaBody); 20861 } 20862 20863 TEST_F(FormatTest, EmptyLinesInLambdas) { 20864 verifyFormat("auto lambda = []() {\n" 20865 " x(); //\n" 20866 "};", 20867 "auto lambda = []() {\n" 20868 "\n" 20869 " x(); //\n" 20870 "\n" 20871 "};"); 20872 } 20873 20874 TEST_F(FormatTest, FormatsBlocks) { 20875 FormatStyle ShortBlocks = getLLVMStyle(); 20876 ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 20877 verifyFormat("int (^Block)(int, int);", ShortBlocks); 20878 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 20879 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 20880 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 20881 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 20882 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 20883 20884 verifyFormat("foo(^{ bar(); });", ShortBlocks); 20885 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 20886 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 20887 20888 verifyFormat("[operation setCompletionBlock:^{\n" 20889 " [self onOperationDone];\n" 20890 "}];"); 20891 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 20892 " [self onOperationDone];\n" 20893 "}]};"); 20894 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 20895 " f();\n" 20896 "}];"); 20897 verifyFormat("int a = [operation block:^int(int *i) {\n" 20898 " return 1;\n" 20899 "}];"); 20900 verifyFormat("[myObject doSomethingWith:arg1\n" 20901 " aaa:^int(int *a) {\n" 20902 " return 1;\n" 20903 " }\n" 20904 " bbb:f(a * bbbbbbbb)];"); 20905 20906 verifyFormat("[operation setCompletionBlock:^{\n" 20907 " [self.delegate newDataAvailable];\n" 20908 "}];", 20909 getLLVMStyleWithColumns(60)); 20910 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 20911 " NSString *path = [self sessionFilePath];\n" 20912 " if (path) {\n" 20913 " // ...\n" 20914 " }\n" 20915 "});"); 20916 verifyFormat("[[SessionService sharedService]\n" 20917 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 20918 " if (window) {\n" 20919 " [self windowDidLoad:window];\n" 20920 " } else {\n" 20921 " [self errorLoadingWindow];\n" 20922 " }\n" 20923 " }];"); 20924 verifyFormat("void (^largeBlock)(void) = ^{\n" 20925 " // ...\n" 20926 "};\n", 20927 getLLVMStyleWithColumns(40)); 20928 verifyFormat("[[SessionService sharedService]\n" 20929 " loadWindowWithCompletionBlock: //\n" 20930 " ^(SessionWindow *window) {\n" 20931 " if (window) {\n" 20932 " [self windowDidLoad:window];\n" 20933 " } else {\n" 20934 " [self errorLoadingWindow];\n" 20935 " }\n" 20936 " }];", 20937 getLLVMStyleWithColumns(60)); 20938 verifyFormat("[myObject doSomethingWith:arg1\n" 20939 " firstBlock:^(Foo *a) {\n" 20940 " // ...\n" 20941 " int i;\n" 20942 " }\n" 20943 " secondBlock:^(Bar *b) {\n" 20944 " // ...\n" 20945 " int i;\n" 20946 " }\n" 20947 " thirdBlock:^Foo(Bar *b) {\n" 20948 " // ...\n" 20949 " int i;\n" 20950 " }];"); 20951 verifyFormat("[myObject doSomethingWith:arg1\n" 20952 " firstBlock:-1\n" 20953 " secondBlock:^(Bar *b) {\n" 20954 " // ...\n" 20955 " int i;\n" 20956 " }];"); 20957 20958 verifyFormat("f(^{\n" 20959 " @autoreleasepool {\n" 20960 " if (a) {\n" 20961 " g();\n" 20962 " }\n" 20963 " }\n" 20964 "});"); 20965 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 20966 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" 20967 "};"); 20968 20969 FormatStyle FourIndent = getLLVMStyle(); 20970 FourIndent.ObjCBlockIndentWidth = 4; 20971 verifyFormat("[operation setCompletionBlock:^{\n" 20972 " [self onOperationDone];\n" 20973 "}];", 20974 FourIndent); 20975 } 20976 20977 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 20978 FormatStyle ZeroColumn = getLLVMStyleWithColumns(0); 20979 20980 verifyFormat("[[SessionService sharedService] " 20981 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 20982 " if (window) {\n" 20983 " [self windowDidLoad:window];\n" 20984 " } else {\n" 20985 " [self errorLoadingWindow];\n" 20986 " }\n" 20987 "}];", 20988 ZeroColumn); 20989 EXPECT_EQ("[[SessionService sharedService]\n" 20990 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 20991 " if (window) {\n" 20992 " [self windowDidLoad:window];\n" 20993 " } else {\n" 20994 " [self errorLoadingWindow];\n" 20995 " }\n" 20996 " }];", 20997 format("[[SessionService sharedService]\n" 20998 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 20999 " if (window) {\n" 21000 " [self windowDidLoad:window];\n" 21001 " } else {\n" 21002 " [self errorLoadingWindow];\n" 21003 " }\n" 21004 "}];", 21005 ZeroColumn)); 21006 verifyFormat("[myObject doSomethingWith:arg1\n" 21007 " firstBlock:^(Foo *a) {\n" 21008 " // ...\n" 21009 " int i;\n" 21010 " }\n" 21011 " secondBlock:^(Bar *b) {\n" 21012 " // ...\n" 21013 " int i;\n" 21014 " }\n" 21015 " thirdBlock:^Foo(Bar *b) {\n" 21016 " // ...\n" 21017 " int i;\n" 21018 " }];", 21019 ZeroColumn); 21020 verifyFormat("f(^{\n" 21021 " @autoreleasepool {\n" 21022 " if (a) {\n" 21023 " g();\n" 21024 " }\n" 21025 " }\n" 21026 "});", 21027 ZeroColumn); 21028 verifyFormat("void (^largeBlock)(void) = ^{\n" 21029 " // ...\n" 21030 "};", 21031 ZeroColumn); 21032 21033 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 21034 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 21035 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 21036 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 21037 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 21038 " int i;\n" 21039 "};", 21040 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 21041 } 21042 21043 TEST_F(FormatTest, SupportsCRLF) { 21044 EXPECT_EQ("int a;\r\n" 21045 "int b;\r\n" 21046 "int c;\r\n", 21047 format("int a;\r\n" 21048 " int b;\r\n" 21049 " int c;\r\n", 21050 getLLVMStyle())); 21051 EXPECT_EQ("int a;\r\n" 21052 "int b;\r\n" 21053 "int c;\r\n", 21054 format("int a;\r\n" 21055 " int b;\n" 21056 " int c;\r\n", 21057 getLLVMStyle())); 21058 EXPECT_EQ("int a;\n" 21059 "int b;\n" 21060 "int c;\n", 21061 format("int a;\r\n" 21062 " int b;\n" 21063 " int c;\n", 21064 getLLVMStyle())); 21065 EXPECT_EQ("\"aaaaaaa \"\r\n" 21066 "\"bbbbbbb\";\r\n", 21067 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 21068 EXPECT_EQ("#define A \\\r\n" 21069 " b; \\\r\n" 21070 " c; \\\r\n" 21071 " d;\r\n", 21072 format("#define A \\\r\n" 21073 " b; \\\r\n" 21074 " c; d; \r\n", 21075 getGoogleStyle())); 21076 21077 EXPECT_EQ("/*\r\n" 21078 "multi line block comments\r\n" 21079 "should not introduce\r\n" 21080 "an extra carriage return\r\n" 21081 "*/\r\n", 21082 format("/*\r\n" 21083 "multi line block comments\r\n" 21084 "should not introduce\r\n" 21085 "an extra carriage return\r\n" 21086 "*/\r\n")); 21087 EXPECT_EQ("/*\r\n" 21088 "\r\n" 21089 "*/", 21090 format("/*\r\n" 21091 " \r\r\r\n" 21092 "*/")); 21093 21094 FormatStyle style = getLLVMStyle(); 21095 21096 style.DeriveLineEnding = true; 21097 style.UseCRLF = false; 21098 EXPECT_EQ("union FooBarBazQux {\n" 21099 " int foo;\n" 21100 " int bar;\n" 21101 " int baz;\n" 21102 "};", 21103 format("union FooBarBazQux {\r\n" 21104 " int foo;\n" 21105 " int bar;\r\n" 21106 " int baz;\n" 21107 "};", 21108 style)); 21109 style.UseCRLF = true; 21110 EXPECT_EQ("union FooBarBazQux {\r\n" 21111 " int foo;\r\n" 21112 " int bar;\r\n" 21113 " int baz;\r\n" 21114 "};", 21115 format("union FooBarBazQux {\r\n" 21116 " int foo;\n" 21117 " int bar;\r\n" 21118 " int baz;\n" 21119 "};", 21120 style)); 21121 21122 style.DeriveLineEnding = false; 21123 style.UseCRLF = false; 21124 EXPECT_EQ("union FooBarBazQux {\n" 21125 " int foo;\n" 21126 " int bar;\n" 21127 " int baz;\n" 21128 " int qux;\n" 21129 "};", 21130 format("union FooBarBazQux {\r\n" 21131 " int foo;\n" 21132 " int bar;\r\n" 21133 " int baz;\n" 21134 " int qux;\r\n" 21135 "};", 21136 style)); 21137 style.UseCRLF = true; 21138 EXPECT_EQ("union FooBarBazQux {\r\n" 21139 " int foo;\r\n" 21140 " int bar;\r\n" 21141 " int baz;\r\n" 21142 " int qux;\r\n" 21143 "};", 21144 format("union FooBarBazQux {\r\n" 21145 " int foo;\n" 21146 " int bar;\r\n" 21147 " int baz;\n" 21148 " int qux;\n" 21149 "};", 21150 style)); 21151 21152 style.DeriveLineEnding = true; 21153 style.UseCRLF = false; 21154 EXPECT_EQ("union FooBarBazQux {\r\n" 21155 " int foo;\r\n" 21156 " int bar;\r\n" 21157 " int baz;\r\n" 21158 " int qux;\r\n" 21159 "};", 21160 format("union FooBarBazQux {\r\n" 21161 " int foo;\n" 21162 " int bar;\r\n" 21163 " int baz;\n" 21164 " int qux;\r\n" 21165 "};", 21166 style)); 21167 style.UseCRLF = true; 21168 EXPECT_EQ("union FooBarBazQux {\n" 21169 " int foo;\n" 21170 " int bar;\n" 21171 " int baz;\n" 21172 " int qux;\n" 21173 "};", 21174 format("union FooBarBazQux {\r\n" 21175 " int foo;\n" 21176 " int bar;\r\n" 21177 " int baz;\n" 21178 " int qux;\n" 21179 "};", 21180 style)); 21181 } 21182 21183 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 21184 verifyFormat("MY_CLASS(C) {\n" 21185 " int i;\n" 21186 " int j;\n" 21187 "};"); 21188 } 21189 21190 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 21191 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 21192 TwoIndent.ContinuationIndentWidth = 2; 21193 21194 EXPECT_EQ("int i =\n" 21195 " longFunction(\n" 21196 " arg);", 21197 format("int i = longFunction(arg);", TwoIndent)); 21198 21199 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 21200 SixIndent.ContinuationIndentWidth = 6; 21201 21202 EXPECT_EQ("int i =\n" 21203 " longFunction(\n" 21204 " arg);", 21205 format("int i = longFunction(arg);", SixIndent)); 21206 } 21207 21208 TEST_F(FormatTest, WrappedClosingParenthesisIndent) { 21209 FormatStyle Style = getLLVMStyle(); 21210 verifyFormat("int Foo::getter(\n" 21211 " //\n" 21212 ") const {\n" 21213 " return foo;\n" 21214 "}", 21215 Style); 21216 verifyFormat("void Foo::setter(\n" 21217 " //\n" 21218 ") {\n" 21219 " foo = 1;\n" 21220 "}", 21221 Style); 21222 } 21223 21224 TEST_F(FormatTest, SpacesInAngles) { 21225 FormatStyle Spaces = getLLVMStyle(); 21226 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 21227 21228 verifyFormat("vector< ::std::string > x1;", Spaces); 21229 verifyFormat("Foo< int, Bar > x2;", Spaces); 21230 verifyFormat("Foo< ::int, ::Bar > x3;", Spaces); 21231 21232 verifyFormat("static_cast< int >(arg);", Spaces); 21233 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 21234 verifyFormat("f< int, float >();", Spaces); 21235 verifyFormat("template <> g() {}", Spaces); 21236 verifyFormat("template < std::vector< int > > f() {}", Spaces); 21237 verifyFormat("std::function< void(int, int) > fct;", Spaces); 21238 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 21239 Spaces); 21240 21241 Spaces.Standard = FormatStyle::LS_Cpp03; 21242 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 21243 verifyFormat("A< A< int > >();", Spaces); 21244 21245 Spaces.SpacesInAngles = FormatStyle::SIAS_Never; 21246 verifyFormat("A<A<int> >();", Spaces); 21247 21248 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave; 21249 verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;", 21250 Spaces); 21251 verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;", 21252 Spaces); 21253 21254 verifyFormat("A<A<int> >();", Spaces); 21255 verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces); 21256 verifyFormat("A< A< int > >();", Spaces); 21257 21258 Spaces.Standard = FormatStyle::LS_Cpp11; 21259 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 21260 verifyFormat("A< A< int > >();", Spaces); 21261 21262 Spaces.SpacesInAngles = FormatStyle::SIAS_Never; 21263 verifyFormat("vector<::std::string> x4;", Spaces); 21264 verifyFormat("vector<int> x5;", Spaces); 21265 verifyFormat("Foo<int, Bar> x6;", Spaces); 21266 verifyFormat("Foo<::int, ::Bar> x7;", Spaces); 21267 21268 verifyFormat("A<A<int>>();", Spaces); 21269 21270 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave; 21271 verifyFormat("vector<::std::string> x4;", Spaces); 21272 verifyFormat("vector< ::std::string > x4;", Spaces); 21273 verifyFormat("vector<int> x5;", Spaces); 21274 verifyFormat("vector< int > x5;", Spaces); 21275 verifyFormat("Foo<int, Bar> x6;", Spaces); 21276 verifyFormat("Foo< int, Bar > x6;", Spaces); 21277 verifyFormat("Foo<::int, ::Bar> x7;", Spaces); 21278 verifyFormat("Foo< ::int, ::Bar > x7;", Spaces); 21279 21280 verifyFormat("A<A<int>>();", Spaces); 21281 verifyFormat("A< A< int > >();", Spaces); 21282 verifyFormat("A<A<int > >();", Spaces); 21283 verifyFormat("A< A< int>>();", Spaces); 21284 21285 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 21286 verifyFormat("// clang-format off\n" 21287 "foo<<<1, 1>>>();\n" 21288 "// clang-format on\n", 21289 Spaces); 21290 verifyFormat("// clang-format off\n" 21291 "foo< < <1, 1> > >();\n" 21292 "// clang-format on\n", 21293 Spaces); 21294 } 21295 21296 TEST_F(FormatTest, SpaceAfterTemplateKeyword) { 21297 FormatStyle Style = getLLVMStyle(); 21298 Style.SpaceAfterTemplateKeyword = false; 21299 verifyFormat("template<int> void foo();", Style); 21300 } 21301 21302 TEST_F(FormatTest, TripleAngleBrackets) { 21303 verifyFormat("f<<<1, 1>>>();"); 21304 verifyFormat("f<<<1, 1, 1, s>>>();"); 21305 verifyFormat("f<<<a, b, c, d>>>();"); 21306 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 21307 verifyFormat("f<param><<<1, 1>>>();"); 21308 verifyFormat("f<1><<<1, 1>>>();"); 21309 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 21310 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 21311 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 21312 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n" 21313 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();"); 21314 } 21315 21316 TEST_F(FormatTest, MergeLessLessAtEnd) { 21317 verifyFormat("<<"); 21318 EXPECT_EQ("< < <", format("\\\n<<<")); 21319 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 21320 "aaallvm::outs() <<"); 21321 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 21322 "aaaallvm::outs()\n <<"); 21323 } 21324 21325 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 21326 std::string code = "#if A\n" 21327 "#if B\n" 21328 "a.\n" 21329 "#endif\n" 21330 " a = 1;\n" 21331 "#else\n" 21332 "#endif\n" 21333 "#if C\n" 21334 "#else\n" 21335 "#endif\n"; 21336 EXPECT_EQ(code, format(code)); 21337 } 21338 21339 TEST_F(FormatTest, HandleConflictMarkers) { 21340 // Git/SVN conflict markers. 21341 EXPECT_EQ("int a;\n" 21342 "void f() {\n" 21343 " callme(some(parameter1,\n" 21344 "<<<<<<< text by the vcs\n" 21345 " parameter2),\n" 21346 "||||||| text by the vcs\n" 21347 " parameter2),\n" 21348 " parameter3,\n" 21349 "======= text by the vcs\n" 21350 " parameter2, parameter3),\n" 21351 ">>>>>>> text by the vcs\n" 21352 " otherparameter);\n", 21353 format("int a;\n" 21354 "void f() {\n" 21355 " callme(some(parameter1,\n" 21356 "<<<<<<< text by the vcs\n" 21357 " parameter2),\n" 21358 "||||||| text by the vcs\n" 21359 " parameter2),\n" 21360 " parameter3,\n" 21361 "======= text by the vcs\n" 21362 " parameter2,\n" 21363 " parameter3),\n" 21364 ">>>>>>> text by the vcs\n" 21365 " otherparameter);\n")); 21366 21367 // Perforce markers. 21368 EXPECT_EQ("void f() {\n" 21369 " function(\n" 21370 ">>>> text by the vcs\n" 21371 " parameter,\n" 21372 "==== text by the vcs\n" 21373 " parameter,\n" 21374 "==== text by the vcs\n" 21375 " parameter,\n" 21376 "<<<< text by the vcs\n" 21377 " parameter);\n", 21378 format("void f() {\n" 21379 " function(\n" 21380 ">>>> text by the vcs\n" 21381 " parameter,\n" 21382 "==== text by the vcs\n" 21383 " parameter,\n" 21384 "==== text by the vcs\n" 21385 " parameter,\n" 21386 "<<<< text by the vcs\n" 21387 " parameter);\n")); 21388 21389 EXPECT_EQ("<<<<<<<\n" 21390 "|||||||\n" 21391 "=======\n" 21392 ">>>>>>>", 21393 format("<<<<<<<\n" 21394 "|||||||\n" 21395 "=======\n" 21396 ">>>>>>>")); 21397 21398 EXPECT_EQ("<<<<<<<\n" 21399 "|||||||\n" 21400 "int i;\n" 21401 "=======\n" 21402 ">>>>>>>", 21403 format("<<<<<<<\n" 21404 "|||||||\n" 21405 "int i;\n" 21406 "=======\n" 21407 ">>>>>>>")); 21408 21409 // FIXME: Handle parsing of macros around conflict markers correctly: 21410 EXPECT_EQ("#define Macro \\\n" 21411 "<<<<<<<\n" 21412 "Something \\\n" 21413 "|||||||\n" 21414 "Else \\\n" 21415 "=======\n" 21416 "Other \\\n" 21417 ">>>>>>>\n" 21418 " End int i;\n", 21419 format("#define Macro \\\n" 21420 "<<<<<<<\n" 21421 " Something \\\n" 21422 "|||||||\n" 21423 " Else \\\n" 21424 "=======\n" 21425 " Other \\\n" 21426 ">>>>>>>\n" 21427 " End\n" 21428 "int i;\n")); 21429 21430 verifyFormat(R"(==== 21431 #ifdef A 21432 a 21433 #else 21434 b 21435 #endif 21436 )"); 21437 } 21438 21439 TEST_F(FormatTest, DisableRegions) { 21440 EXPECT_EQ("int i;\n" 21441 "// clang-format off\n" 21442 " int j;\n" 21443 "// clang-format on\n" 21444 "int k;", 21445 format(" int i;\n" 21446 " // clang-format off\n" 21447 " int j;\n" 21448 " // clang-format on\n" 21449 " int k;")); 21450 EXPECT_EQ("int i;\n" 21451 "/* clang-format off */\n" 21452 " int j;\n" 21453 "/* clang-format on */\n" 21454 "int k;", 21455 format(" int i;\n" 21456 " /* clang-format off */\n" 21457 " int j;\n" 21458 " /* clang-format on */\n" 21459 " int k;")); 21460 21461 // Don't reflow comments within disabled regions. 21462 EXPECT_EQ("// clang-format off\n" 21463 "// long long long long long long line\n" 21464 "/* clang-format on */\n" 21465 "/* long long long\n" 21466 " * long long long\n" 21467 " * line */\n" 21468 "int i;\n" 21469 "/* clang-format off */\n" 21470 "/* long long long long long long line */\n", 21471 format("// clang-format off\n" 21472 "// long long long long long long line\n" 21473 "/* clang-format on */\n" 21474 "/* long long long long long long line */\n" 21475 "int i;\n" 21476 "/* clang-format off */\n" 21477 "/* long long long long long long line */\n", 21478 getLLVMStyleWithColumns(20))); 21479 } 21480 21481 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 21482 format("? ) ="); 21483 verifyNoCrash("#define a\\\n /**/}"); 21484 } 21485 21486 TEST_F(FormatTest, FormatsTableGenCode) { 21487 FormatStyle Style = getLLVMStyle(); 21488 Style.Language = FormatStyle::LK_TableGen; 21489 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 21490 } 21491 21492 TEST_F(FormatTest, ArrayOfTemplates) { 21493 EXPECT_EQ("auto a = new unique_ptr<int>[10];", 21494 format("auto a = new unique_ptr<int > [ 10];")); 21495 21496 FormatStyle Spaces = getLLVMStyle(); 21497 Spaces.SpacesInSquareBrackets = true; 21498 EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];", 21499 format("auto a = new unique_ptr<int > [10];", Spaces)); 21500 } 21501 21502 TEST_F(FormatTest, ArrayAsTemplateType) { 21503 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;", 21504 format("auto a = unique_ptr < Foo < Bar>[ 10]> ;")); 21505 21506 FormatStyle Spaces = getLLVMStyle(); 21507 Spaces.SpacesInSquareBrackets = true; 21508 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;", 21509 format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces)); 21510 } 21511 21512 TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); } 21513 21514 TEST(FormatStyle, GetStyleWithEmptyFileName) { 21515 llvm::vfs::InMemoryFileSystem FS; 21516 auto Style1 = getStyle("file", "", "Google", "", &FS); 21517 ASSERT_TRUE((bool)Style1); 21518 ASSERT_EQ(*Style1, getGoogleStyle()); 21519 } 21520 21521 TEST(FormatStyle, GetStyleOfFile) { 21522 llvm::vfs::InMemoryFileSystem FS; 21523 // Test 1: format file in the same directory. 21524 ASSERT_TRUE( 21525 FS.addFile("/a/.clang-format", 0, 21526 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 21527 ASSERT_TRUE( 21528 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 21529 auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS); 21530 ASSERT_TRUE((bool)Style1); 21531 ASSERT_EQ(*Style1, getLLVMStyle()); 21532 21533 // Test 2.1: fallback to default. 21534 ASSERT_TRUE( 21535 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 21536 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS); 21537 ASSERT_TRUE((bool)Style2); 21538 ASSERT_EQ(*Style2, getMozillaStyle()); 21539 21540 // Test 2.2: no format on 'none' fallback style. 21541 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 21542 ASSERT_TRUE((bool)Style2); 21543 ASSERT_EQ(*Style2, getNoStyle()); 21544 21545 // Test 2.3: format if config is found with no based style while fallback is 21546 // 'none'. 21547 ASSERT_TRUE(FS.addFile("/b/.clang-format", 0, 21548 llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2"))); 21549 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 21550 ASSERT_TRUE((bool)Style2); 21551 ASSERT_EQ(*Style2, getLLVMStyle()); 21552 21553 // Test 2.4: format if yaml with no based style, while fallback is 'none'. 21554 Style2 = getStyle("{}", "a.h", "none", "", &FS); 21555 ASSERT_TRUE((bool)Style2); 21556 ASSERT_EQ(*Style2, getLLVMStyle()); 21557 21558 // Test 3: format file in parent directory. 21559 ASSERT_TRUE( 21560 FS.addFile("/c/.clang-format", 0, 21561 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 21562 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, 21563 llvm::MemoryBuffer::getMemBuffer("int i;"))); 21564 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS); 21565 ASSERT_TRUE((bool)Style3); 21566 ASSERT_EQ(*Style3, getGoogleStyle()); 21567 21568 // Test 4: error on invalid fallback style 21569 auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS); 21570 ASSERT_FALSE((bool)Style4); 21571 llvm::consumeError(Style4.takeError()); 21572 21573 // Test 5: error on invalid yaml on command line 21574 auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS); 21575 ASSERT_FALSE((bool)Style5); 21576 llvm::consumeError(Style5.takeError()); 21577 21578 // Test 6: error on invalid style 21579 auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS); 21580 ASSERT_FALSE((bool)Style6); 21581 llvm::consumeError(Style6.takeError()); 21582 21583 // Test 7: found config file, error on parsing it 21584 ASSERT_TRUE( 21585 FS.addFile("/d/.clang-format", 0, 21586 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n" 21587 "InvalidKey: InvalidValue"))); 21588 ASSERT_TRUE( 21589 FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 21590 auto Style7a = getStyle("file", "/d/.clang-format", "LLVM", "", &FS); 21591 ASSERT_FALSE((bool)Style7a); 21592 llvm::consumeError(Style7a.takeError()); 21593 21594 auto Style7b = getStyle("file", "/d/.clang-format", "LLVM", "", &FS, true); 21595 ASSERT_TRUE((bool)Style7b); 21596 21597 // Test 8: inferred per-language defaults apply. 21598 auto StyleTd = getStyle("file", "x.td", "llvm", "", &FS); 21599 ASSERT_TRUE((bool)StyleTd); 21600 ASSERT_EQ(*StyleTd, getLLVMStyle(FormatStyle::LK_TableGen)); 21601 21602 // Test 9.1.1: overwriting a file style, when no parent file exists with no 21603 // fallback style. 21604 ASSERT_TRUE(FS.addFile( 21605 "/e/sub/.clang-format", 0, 21606 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: InheritParentConfig\n" 21607 "ColumnLimit: 20"))); 21608 ASSERT_TRUE(FS.addFile("/e/sub/code.cpp", 0, 21609 llvm::MemoryBuffer::getMemBuffer("int i;"))); 21610 auto Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS); 21611 ASSERT_TRUE(static_cast<bool>(Style9)); 21612 ASSERT_EQ(*Style9, [] { 21613 auto Style = getNoStyle(); 21614 Style.ColumnLimit = 20; 21615 return Style; 21616 }()); 21617 21618 // Test 9.1.2: propagate more than one level with no parent file. 21619 ASSERT_TRUE(FS.addFile("/e/sub/sub/code.cpp", 0, 21620 llvm::MemoryBuffer::getMemBuffer("int i;"))); 21621 ASSERT_TRUE(FS.addFile("/e/sub/sub/.clang-format", 0, 21622 llvm::MemoryBuffer::getMemBuffer( 21623 "BasedOnStyle: InheritParentConfig\n" 21624 "WhitespaceSensitiveMacros: ['FOO', 'BAR']"))); 21625 std::vector<std::string> NonDefaultWhiteSpaceMacros{"FOO", "BAR"}; 21626 21627 ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros); 21628 Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS); 21629 ASSERT_TRUE(static_cast<bool>(Style9)); 21630 ASSERT_EQ(*Style9, [&NonDefaultWhiteSpaceMacros] { 21631 auto Style = getNoStyle(); 21632 Style.ColumnLimit = 20; 21633 Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros; 21634 return Style; 21635 }()); 21636 21637 // Test 9.2: with LLVM fallback style 21638 Style9 = getStyle("file", "/e/sub/code.cpp", "LLVM", "", &FS); 21639 ASSERT_TRUE(static_cast<bool>(Style9)); 21640 ASSERT_EQ(*Style9, [] { 21641 auto Style = getLLVMStyle(); 21642 Style.ColumnLimit = 20; 21643 return Style; 21644 }()); 21645 21646 // Test 9.3: with a parent file 21647 ASSERT_TRUE( 21648 FS.addFile("/e/.clang-format", 0, 21649 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google\n" 21650 "UseTab: Always"))); 21651 Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS); 21652 ASSERT_TRUE(static_cast<bool>(Style9)); 21653 ASSERT_EQ(*Style9, [] { 21654 auto Style = getGoogleStyle(); 21655 Style.ColumnLimit = 20; 21656 Style.UseTab = FormatStyle::UT_Always; 21657 return Style; 21658 }()); 21659 21660 // Test 9.4: propagate more than one level with a parent file. 21661 const auto SubSubStyle = [&NonDefaultWhiteSpaceMacros] { 21662 auto Style = getGoogleStyle(); 21663 Style.ColumnLimit = 20; 21664 Style.UseTab = FormatStyle::UT_Always; 21665 Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros; 21666 return Style; 21667 }(); 21668 21669 ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros); 21670 Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS); 21671 ASSERT_TRUE(static_cast<bool>(Style9)); 21672 ASSERT_EQ(*Style9, SubSubStyle); 21673 21674 // Test 9.5: use InheritParentConfig as style name 21675 Style9 = 21676 getStyle("inheritparentconfig", "/e/sub/sub/code.cpp", "none", "", &FS); 21677 ASSERT_TRUE(static_cast<bool>(Style9)); 21678 ASSERT_EQ(*Style9, SubSubStyle); 21679 21680 // Test 9.6: use command line style with inheritance 21681 Style9 = getStyle("{BasedOnStyle: InheritParentConfig}", "/e/sub/code.cpp", 21682 "none", "", &FS); 21683 ASSERT_TRUE(static_cast<bool>(Style9)); 21684 ASSERT_EQ(*Style9, SubSubStyle); 21685 21686 // Test 9.7: use command line style with inheritance and own config 21687 Style9 = getStyle("{BasedOnStyle: InheritParentConfig, " 21688 "WhitespaceSensitiveMacros: ['FOO', 'BAR']}", 21689 "/e/sub/code.cpp", "none", "", &FS); 21690 ASSERT_TRUE(static_cast<bool>(Style9)); 21691 ASSERT_EQ(*Style9, SubSubStyle); 21692 21693 // Test 9.8: use inheritance from a file without BasedOnStyle 21694 ASSERT_TRUE(FS.addFile("/e/withoutbase/.clang-format", 0, 21695 llvm::MemoryBuffer::getMemBuffer("ColumnLimit: 123"))); 21696 ASSERT_TRUE( 21697 FS.addFile("/e/withoutbase/sub/.clang-format", 0, 21698 llvm::MemoryBuffer::getMemBuffer( 21699 "BasedOnStyle: InheritParentConfig\nIndentWidth: 7"))); 21700 // Make sure we do not use the fallback style 21701 Style9 = getStyle("file", "/e/withoutbase/code.cpp", "google", "", &FS); 21702 ASSERT_TRUE(static_cast<bool>(Style9)); 21703 ASSERT_EQ(*Style9, [] { 21704 auto Style = getLLVMStyle(); 21705 Style.ColumnLimit = 123; 21706 return Style; 21707 }()); 21708 21709 Style9 = getStyle("file", "/e/withoutbase/sub/code.cpp", "google", "", &FS); 21710 ASSERT_TRUE(static_cast<bool>(Style9)); 21711 ASSERT_EQ(*Style9, [] { 21712 auto Style = getLLVMStyle(); 21713 Style.ColumnLimit = 123; 21714 Style.IndentWidth = 7; 21715 return Style; 21716 }()); 21717 21718 // Test 9.9: use inheritance from a specific config file. 21719 Style9 = getStyle("file:/e/sub/sub/.clang-format", "/e/sub/sub/code.cpp", 21720 "none", "", &FS); 21721 ASSERT_TRUE(static_cast<bool>(Style9)); 21722 ASSERT_EQ(*Style9, SubSubStyle); 21723 } 21724 21725 TEST(FormatStyle, GetStyleOfSpecificFile) { 21726 llvm::vfs::InMemoryFileSystem FS; 21727 // Specify absolute path to a format file in a parent directory. 21728 ASSERT_TRUE( 21729 FS.addFile("/e/.clang-format", 0, 21730 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 21731 ASSERT_TRUE( 21732 FS.addFile("/e/explicit.clang-format", 0, 21733 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 21734 ASSERT_TRUE(FS.addFile("/e/sub/sub/sub/test.cpp", 0, 21735 llvm::MemoryBuffer::getMemBuffer("int i;"))); 21736 auto Style = getStyle("file:/e/explicit.clang-format", 21737 "/e/sub/sub/sub/test.cpp", "LLVM", "", &FS); 21738 ASSERT_TRUE(static_cast<bool>(Style)); 21739 ASSERT_EQ(*Style, getGoogleStyle()); 21740 21741 // Specify relative path to a format file. 21742 ASSERT_TRUE( 21743 FS.addFile("../../e/explicit.clang-format", 0, 21744 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 21745 Style = getStyle("file:../../e/explicit.clang-format", 21746 "/e/sub/sub/sub/test.cpp", "LLVM", "", &FS); 21747 ASSERT_TRUE(static_cast<bool>(Style)); 21748 ASSERT_EQ(*Style, getGoogleStyle()); 21749 21750 // Specify path to a format file that does not exist. 21751 Style = getStyle("file:/e/missing.clang-format", "/e/sub/sub/sub/test.cpp", 21752 "LLVM", "", &FS); 21753 ASSERT_FALSE(static_cast<bool>(Style)); 21754 llvm::consumeError(Style.takeError()); 21755 21756 // Specify path to a file on the filesystem. 21757 SmallString<128> FormatFilePath; 21758 std::error_code ECF = llvm::sys::fs::createTemporaryFile( 21759 "FormatFileTest", "tpl", FormatFilePath); 21760 EXPECT_FALSE((bool)ECF); 21761 llvm::raw_fd_ostream FormatFileTest(FormatFilePath, ECF); 21762 EXPECT_FALSE((bool)ECF); 21763 FormatFileTest << "BasedOnStyle: Google\n"; 21764 FormatFileTest.close(); 21765 21766 SmallString<128> TestFilePath; 21767 std::error_code ECT = 21768 llvm::sys::fs::createTemporaryFile("CodeFileTest", "cc", TestFilePath); 21769 EXPECT_FALSE((bool)ECT); 21770 llvm::raw_fd_ostream CodeFileTest(TestFilePath, ECT); 21771 CodeFileTest << "int i;\n"; 21772 CodeFileTest.close(); 21773 21774 std::string format_file_arg = std::string("file:") + FormatFilePath.c_str(); 21775 Style = getStyle(format_file_arg, TestFilePath, "LLVM", "", nullptr); 21776 21777 llvm::sys::fs::remove(FormatFilePath.c_str()); 21778 llvm::sys::fs::remove(TestFilePath.c_str()); 21779 ASSERT_TRUE(static_cast<bool>(Style)); 21780 ASSERT_EQ(*Style, getGoogleStyle()); 21781 } 21782 21783 TEST_F(ReplacementTest, FormatCodeAfterReplacements) { 21784 // Column limit is 20. 21785 std::string Code = "Type *a =\n" 21786 " new Type();\n" 21787 "g(iiiii, 0, jjjjj,\n" 21788 " 0, kkkkk, 0, mm);\n" 21789 "int bad = format ;"; 21790 std::string Expected = "auto a = new Type();\n" 21791 "g(iiiii, nullptr,\n" 21792 " jjjjj, nullptr,\n" 21793 " kkkkk, nullptr,\n" 21794 " mm);\n" 21795 "int bad = format ;"; 21796 FileID ID = Context.createInMemoryFile("format.cpp", Code); 21797 tooling::Replacements Replaces = toReplacements( 21798 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6, 21799 "auto "), 21800 tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1, 21801 "nullptr"), 21802 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1, 21803 "nullptr"), 21804 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1, 21805 "nullptr")}); 21806 21807 FormatStyle Style = getLLVMStyle(); 21808 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. 21809 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 21810 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 21811 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 21812 auto Result = applyAllReplacements(Code, *FormattedReplaces); 21813 EXPECT_TRUE(static_cast<bool>(Result)); 21814 EXPECT_EQ(Expected, *Result); 21815 } 21816 21817 TEST_F(ReplacementTest, SortIncludesAfterReplacement) { 21818 std::string Code = "#include \"a.h\"\n" 21819 "#include \"c.h\"\n" 21820 "\n" 21821 "int main() {\n" 21822 " return 0;\n" 21823 "}"; 21824 std::string Expected = "#include \"a.h\"\n" 21825 "#include \"b.h\"\n" 21826 "#include \"c.h\"\n" 21827 "\n" 21828 "int main() {\n" 21829 " return 0;\n" 21830 "}"; 21831 FileID ID = Context.createInMemoryFile("fix.cpp", Code); 21832 tooling::Replacements Replaces = toReplacements( 21833 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0, 21834 "#include \"b.h\"\n")}); 21835 21836 FormatStyle Style = getLLVMStyle(); 21837 Style.SortIncludes = FormatStyle::SI_CaseSensitive; 21838 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 21839 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 21840 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 21841 auto Result = applyAllReplacements(Code, *FormattedReplaces); 21842 EXPECT_TRUE(static_cast<bool>(Result)); 21843 EXPECT_EQ(Expected, *Result); 21844 } 21845 21846 TEST_F(FormatTest, FormatSortsUsingDeclarations) { 21847 EXPECT_EQ("using std::cin;\n" 21848 "using std::cout;", 21849 format("using std::cout;\n" 21850 "using std::cin;", 21851 getGoogleStyle())); 21852 } 21853 21854 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) { 21855 FormatStyle Style = getLLVMStyle(); 21856 Style.Standard = FormatStyle::LS_Cpp03; 21857 // cpp03 recognize this string as identifier u8 and literal character 'a' 21858 EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style)); 21859 } 21860 21861 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) { 21862 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers 21863 // all modes, including C++11, C++14 and C++17 21864 EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';")); 21865 } 21866 21867 TEST_F(FormatTest, DoNotFormatLikelyXml) { 21868 EXPECT_EQ("<!-- ;> -->", format("<!-- ;> -->", getGoogleStyle())); 21869 EXPECT_EQ(" <!-- >; -->", format(" <!-- >; -->", getGoogleStyle())); 21870 } 21871 21872 TEST_F(FormatTest, StructuredBindings) { 21873 // Structured bindings is a C++17 feature. 21874 // all modes, including C++11, C++14 and C++17 21875 verifyFormat("auto [a, b] = f();"); 21876 EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();")); 21877 EXPECT_EQ("const auto [a, b] = f();", format("const auto[a, b] = f();")); 21878 EXPECT_EQ("auto const [a, b] = f();", format("auto const[a, b] = f();")); 21879 EXPECT_EQ("auto const volatile [a, b] = f();", 21880 format("auto const volatile[a, b] = f();")); 21881 EXPECT_EQ("auto [a, b, c] = f();", format("auto [ a , b,c ] = f();")); 21882 EXPECT_EQ("auto &[a, b, c] = f();", 21883 format("auto &[ a , b,c ] = f();")); 21884 EXPECT_EQ("auto &&[a, b, c] = f();", 21885 format("auto &&[ a , b,c ] = f();")); 21886 EXPECT_EQ("auto const &[a, b] = f();", format("auto const&[a, b] = f();")); 21887 EXPECT_EQ("auto const volatile &&[a, b] = f();", 21888 format("auto const volatile &&[a, b] = f();")); 21889 EXPECT_EQ("auto const &&[a, b] = f();", 21890 format("auto const && [a, b] = f();")); 21891 EXPECT_EQ("const auto &[a, b] = f();", 21892 format("const auto & [a, b] = f();")); 21893 EXPECT_EQ("const auto volatile &&[a, b] = f();", 21894 format("const auto volatile &&[a, b] = f();")); 21895 EXPECT_EQ("volatile const auto &&[a, b] = f();", 21896 format("volatile const auto &&[a, b] = f();")); 21897 EXPECT_EQ("const auto &&[a, b] = f();", 21898 format("const auto && [a, b] = f();")); 21899 21900 // Make sure we don't mistake structured bindings for lambdas. 21901 FormatStyle PointerMiddle = getLLVMStyle(); 21902 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 21903 verifyFormat("auto [a1, b]{A * i};", getGoogleStyle()); 21904 verifyFormat("auto [a2, b]{A * i};", getLLVMStyle()); 21905 verifyFormat("auto [a3, b]{A * i};", PointerMiddle); 21906 verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle()); 21907 verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle()); 21908 verifyFormat("auto const [a3, b]{A * i};", PointerMiddle); 21909 verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle()); 21910 verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle()); 21911 verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle); 21912 verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle()); 21913 verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle()); 21914 verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle); 21915 21916 EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}", 21917 format("for (const auto && [a, b] : some_range) {\n}")); 21918 EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}", 21919 format("for (const auto & [a, b] : some_range) {\n}")); 21920 EXPECT_EQ("for (const auto [a, b] : some_range) {\n}", 21921 format("for (const auto[a, b] : some_range) {\n}")); 21922 EXPECT_EQ("auto [x, y](expr);", format("auto[x,y] (expr);")); 21923 EXPECT_EQ("auto &[x, y](expr);", format("auto & [x,y] (expr);")); 21924 EXPECT_EQ("auto &&[x, y](expr);", format("auto && [x,y] (expr);")); 21925 EXPECT_EQ("auto const &[x, y](expr);", 21926 format("auto const & [x,y] (expr);")); 21927 EXPECT_EQ("auto const &&[x, y](expr);", 21928 format("auto const && [x,y] (expr);")); 21929 EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y] {expr};")); 21930 EXPECT_EQ("auto const &[x, y]{expr};", 21931 format("auto const & [x,y] {expr};")); 21932 EXPECT_EQ("auto const &&[x, y]{expr};", 21933 format("auto const && [x,y] {expr};")); 21934 21935 FormatStyle Spaces = getLLVMStyle(); 21936 Spaces.SpacesInSquareBrackets = true; 21937 verifyFormat("auto [ a, b ] = f();", Spaces); 21938 verifyFormat("auto &&[ a, b ] = f();", Spaces); 21939 verifyFormat("auto &[ a, b ] = f();", Spaces); 21940 verifyFormat("auto const &&[ a, b ] = f();", Spaces); 21941 verifyFormat("auto const &[ a, b ] = f();", Spaces); 21942 } 21943 21944 TEST_F(FormatTest, FileAndCode) { 21945 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", "")); 21946 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", "")); 21947 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", "")); 21948 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "")); 21949 EXPECT_EQ(FormatStyle::LK_ObjC, 21950 guessLanguage("foo.h", "@interface Foo\n@end\n")); 21951 EXPECT_EQ( 21952 FormatStyle::LK_ObjC, 21953 guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }")); 21954 EXPECT_EQ(FormatStyle::LK_ObjC, 21955 guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))")); 21956 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;")); 21957 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", "")); 21958 EXPECT_EQ(FormatStyle::LK_ObjC, 21959 guessLanguage("foo", "@interface Foo\n@end\n")); 21960 EXPECT_EQ(FormatStyle::LK_ObjC, 21961 guessLanguage("foo.h", "int DoStuff(CGRect rect);\n")); 21962 EXPECT_EQ( 21963 FormatStyle::LK_ObjC, 21964 guessLanguage("foo.h", 21965 "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n")); 21966 EXPECT_EQ( 21967 FormatStyle::LK_Cpp, 21968 guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;")); 21969 } 21970 21971 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) { 21972 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];")); 21973 EXPECT_EQ(FormatStyle::LK_ObjC, 21974 guessLanguage("foo.h", "array[[calculator getIndex]];")); 21975 EXPECT_EQ(FormatStyle::LK_Cpp, 21976 guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];")); 21977 EXPECT_EQ( 21978 FormatStyle::LK_Cpp, 21979 guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];")); 21980 EXPECT_EQ(FormatStyle::LK_ObjC, 21981 guessLanguage("foo.h", "[[noreturn foo] bar];")); 21982 EXPECT_EQ(FormatStyle::LK_Cpp, 21983 guessLanguage("foo.h", "[[clang::fallthrough]];")); 21984 EXPECT_EQ(FormatStyle::LK_ObjC, 21985 guessLanguage("foo.h", "[[clang:fallthrough] foo];")); 21986 EXPECT_EQ(FormatStyle::LK_Cpp, 21987 guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];")); 21988 EXPECT_EQ(FormatStyle::LK_Cpp, 21989 guessLanguage("foo.h", "[[using clang: fallthrough]];")); 21990 EXPECT_EQ(FormatStyle::LK_ObjC, 21991 guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];")); 21992 EXPECT_EQ(FormatStyle::LK_Cpp, 21993 guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];")); 21994 EXPECT_EQ( 21995 FormatStyle::LK_Cpp, 21996 guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)")); 21997 EXPECT_EQ( 21998 FormatStyle::LK_Cpp, 21999 guessLanguage("foo.h", 22000 "[[clang::callable_when(\"unconsumed\", \"unknown\")]]")); 22001 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]")); 22002 } 22003 22004 TEST_F(FormatTest, GuessLanguageWithCaret) { 22005 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);")); 22006 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);")); 22007 EXPECT_EQ(FormatStyle::LK_ObjC, 22008 guessLanguage("foo.h", "int(^)(char, float);")); 22009 EXPECT_EQ(FormatStyle::LK_ObjC, 22010 guessLanguage("foo.h", "int(^foo)(char, float);")); 22011 EXPECT_EQ(FormatStyle::LK_ObjC, 22012 guessLanguage("foo.h", "int(^foo[10])(char, float);")); 22013 EXPECT_EQ(FormatStyle::LK_ObjC, 22014 guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);")); 22015 EXPECT_EQ( 22016 FormatStyle::LK_ObjC, 22017 guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);")); 22018 } 22019 22020 TEST_F(FormatTest, GuessLanguageWithPragmas) { 22021 EXPECT_EQ(FormatStyle::LK_Cpp, 22022 guessLanguage("foo.h", "__pragma(warning(disable:))")); 22023 EXPECT_EQ(FormatStyle::LK_Cpp, 22024 guessLanguage("foo.h", "#pragma(warning(disable:))")); 22025 EXPECT_EQ(FormatStyle::LK_Cpp, 22026 guessLanguage("foo.h", "_Pragma(warning(disable:))")); 22027 } 22028 22029 TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) { 22030 // ASM symbolic names are identifiers that must be surrounded by [] without 22031 // space in between: 22032 // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands 22033 22034 // Example from https://bugs.llvm.org/show_bug.cgi?id=45108. 22035 verifyFormat(R"(// 22036 asm volatile("mrs %x[result], FPCR" : [result] "=r"(result)); 22037 )"); 22038 22039 // A list of several ASM symbolic names. 22040 verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)"); 22041 22042 // ASM symbolic names in inline ASM with inputs and outputs. 22043 verifyFormat(R"(// 22044 asm("cmoveq %1, %2, %[result]" 22045 : [result] "=r"(result) 22046 : "r"(test), "r"(new), "[result]"(old)); 22047 )"); 22048 22049 // ASM symbolic names in inline ASM with no outputs. 22050 verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)"); 22051 } 22052 22053 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) { 22054 EXPECT_EQ(FormatStyle::LK_Cpp, 22055 guessLanguage("foo.h", "void f() {\n" 22056 " asm (\"mov %[e], %[d]\"\n" 22057 " : [d] \"=rm\" (d)\n" 22058 " [e] \"rm\" (*e));\n" 22059 "}")); 22060 EXPECT_EQ(FormatStyle::LK_Cpp, 22061 guessLanguage("foo.h", "void f() {\n" 22062 " _asm (\"mov %[e], %[d]\"\n" 22063 " : [d] \"=rm\" (d)\n" 22064 " [e] \"rm\" (*e));\n" 22065 "}")); 22066 EXPECT_EQ(FormatStyle::LK_Cpp, 22067 guessLanguage("foo.h", "void f() {\n" 22068 " __asm (\"mov %[e], %[d]\"\n" 22069 " : [d] \"=rm\" (d)\n" 22070 " [e] \"rm\" (*e));\n" 22071 "}")); 22072 EXPECT_EQ(FormatStyle::LK_Cpp, 22073 guessLanguage("foo.h", "void f() {\n" 22074 " __asm__ (\"mov %[e], %[d]\"\n" 22075 " : [d] \"=rm\" (d)\n" 22076 " [e] \"rm\" (*e));\n" 22077 "}")); 22078 EXPECT_EQ(FormatStyle::LK_Cpp, 22079 guessLanguage("foo.h", "void f() {\n" 22080 " asm (\"mov %[e], %[d]\"\n" 22081 " : [d] \"=rm\" (d),\n" 22082 " [e] \"rm\" (*e));\n" 22083 "}")); 22084 EXPECT_EQ(FormatStyle::LK_Cpp, 22085 guessLanguage("foo.h", "void f() {\n" 22086 " asm volatile (\"mov %[e], %[d]\"\n" 22087 " : [d] \"=rm\" (d)\n" 22088 " [e] \"rm\" (*e));\n" 22089 "}")); 22090 } 22091 22092 TEST_F(FormatTest, GuessLanguageWithChildLines) { 22093 EXPECT_EQ(FormatStyle::LK_Cpp, 22094 guessLanguage("foo.h", "#define FOO ({ std::string s; })")); 22095 EXPECT_EQ(FormatStyle::LK_ObjC, 22096 guessLanguage("foo.h", "#define FOO ({ NSString *s; })")); 22097 EXPECT_EQ( 22098 FormatStyle::LK_Cpp, 22099 guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })")); 22100 EXPECT_EQ( 22101 FormatStyle::LK_ObjC, 22102 guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })")); 22103 } 22104 22105 TEST_F(FormatTest, TypenameMacros) { 22106 std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"}; 22107 22108 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353 22109 FormatStyle Google = getGoogleStyleWithColumns(0); 22110 Google.TypenameMacros = TypenameMacros; 22111 verifyFormat("struct foo {\n" 22112 " int bar;\n" 22113 " TAILQ_ENTRY(a) bleh;\n" 22114 "};", 22115 Google); 22116 22117 FormatStyle Macros = getLLVMStyle(); 22118 Macros.TypenameMacros = TypenameMacros; 22119 22120 verifyFormat("STACK_OF(int) a;", Macros); 22121 verifyFormat("STACK_OF(int) *a;", Macros); 22122 verifyFormat("STACK_OF(int const *) *a;", Macros); 22123 verifyFormat("STACK_OF(int *const) *a;", Macros); 22124 verifyFormat("STACK_OF(int, string) a;", Macros); 22125 verifyFormat("STACK_OF(LIST(int)) a;", Macros); 22126 verifyFormat("STACK_OF(LIST(int)) a, b;", Macros); 22127 verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros); 22128 verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros); 22129 verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros); 22130 verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros); 22131 22132 Macros.PointerAlignment = FormatStyle::PAS_Left; 22133 verifyFormat("STACK_OF(int)* a;", Macros); 22134 verifyFormat("STACK_OF(int*)* a;", Macros); 22135 verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros); 22136 verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros); 22137 verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros); 22138 } 22139 22140 TEST_F(FormatTest, AtomicQualifier) { 22141 // Check that we treate _Atomic as a type and not a function call 22142 FormatStyle Google = getGoogleStyleWithColumns(0); 22143 verifyFormat("struct foo {\n" 22144 " int a1;\n" 22145 " _Atomic(a) a2;\n" 22146 " _Atomic(_Atomic(int) *const) a3;\n" 22147 "};", 22148 Google); 22149 verifyFormat("_Atomic(uint64_t) a;"); 22150 verifyFormat("_Atomic(uint64_t) *a;"); 22151 verifyFormat("_Atomic(uint64_t const *) *a;"); 22152 verifyFormat("_Atomic(uint64_t *const) *a;"); 22153 verifyFormat("_Atomic(const uint64_t *) *a;"); 22154 verifyFormat("_Atomic(uint64_t) a;"); 22155 verifyFormat("_Atomic(_Atomic(uint64_t)) a;"); 22156 verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;"); 22157 verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}"); 22158 verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);"); 22159 22160 verifyFormat("_Atomic(uint64_t) *s(InitValue);"); 22161 verifyFormat("_Atomic(uint64_t) *s{InitValue};"); 22162 FormatStyle Style = getLLVMStyle(); 22163 Style.PointerAlignment = FormatStyle::PAS_Left; 22164 verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style); 22165 verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style); 22166 verifyFormat("_Atomic(int)* a;", Style); 22167 verifyFormat("_Atomic(int*)* a;", Style); 22168 verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style); 22169 22170 Style.SpacesInCStyleCastParentheses = true; 22171 Style.SpacesInParentheses = false; 22172 verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style); 22173 Style.SpacesInCStyleCastParentheses = false; 22174 Style.SpacesInParentheses = true; 22175 verifyFormat("x = (_Atomic( uint64_t ))*a;", Style); 22176 verifyFormat("x = (_Atomic( uint64_t ))&a;", Style); 22177 } 22178 22179 TEST_F(FormatTest, AmbersandInLamda) { 22180 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899 22181 FormatStyle AlignStyle = getLLVMStyle(); 22182 AlignStyle.PointerAlignment = FormatStyle::PAS_Left; 22183 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle); 22184 AlignStyle.PointerAlignment = FormatStyle::PAS_Right; 22185 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle); 22186 } 22187 22188 TEST_F(FormatTest, SpacesInConditionalStatement) { 22189 FormatStyle Spaces = getLLVMStyle(); 22190 Spaces.IfMacros.clear(); 22191 Spaces.IfMacros.push_back("MYIF"); 22192 Spaces.SpacesInConditionalStatement = true; 22193 verifyFormat("for ( int i = 0; i; i++ )\n continue;", Spaces); 22194 verifyFormat("if ( !a )\n return;", Spaces); 22195 verifyFormat("if ( a )\n return;", Spaces); 22196 verifyFormat("if constexpr ( a )\n return;", Spaces); 22197 verifyFormat("MYIF ( a )\n return;", Spaces); 22198 verifyFormat("MYIF ( a )\n return;\nelse MYIF ( b )\n return;", Spaces); 22199 verifyFormat("MYIF ( a )\n return;\nelse\n return;", Spaces); 22200 verifyFormat("switch ( a )\ncase 1:\n return;", Spaces); 22201 verifyFormat("while ( a )\n return;", Spaces); 22202 verifyFormat("while ( (a && b) )\n return;", Spaces); 22203 verifyFormat("do {\n} while ( 1 != 0 );", Spaces); 22204 verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces); 22205 // Check that space on the left of "::" is inserted as expected at beginning 22206 // of condition. 22207 verifyFormat("while ( ::func() )\n return;", Spaces); 22208 22209 // Check impact of ControlStatementsExceptControlMacros is honored. 22210 Spaces.SpaceBeforeParens = 22211 FormatStyle::SBPO_ControlStatementsExceptControlMacros; 22212 verifyFormat("MYIF( a )\n return;", Spaces); 22213 verifyFormat("MYIF( a )\n return;\nelse MYIF( b )\n return;", Spaces); 22214 verifyFormat("MYIF( a )\n return;\nelse\n return;", Spaces); 22215 } 22216 22217 TEST_F(FormatTest, AlternativeOperators) { 22218 // Test case for ensuring alternate operators are not 22219 // combined with their right most neighbour. 22220 verifyFormat("int a and b;"); 22221 verifyFormat("int a and_eq b;"); 22222 verifyFormat("int a bitand b;"); 22223 verifyFormat("int a bitor b;"); 22224 verifyFormat("int a compl b;"); 22225 verifyFormat("int a not b;"); 22226 verifyFormat("int a not_eq b;"); 22227 verifyFormat("int a or b;"); 22228 verifyFormat("int a xor b;"); 22229 verifyFormat("int a xor_eq b;"); 22230 verifyFormat("return this not_eq bitand other;"); 22231 verifyFormat("bool operator not_eq(const X bitand other)"); 22232 22233 verifyFormat("int a and 5;"); 22234 verifyFormat("int a and_eq 5;"); 22235 verifyFormat("int a bitand 5;"); 22236 verifyFormat("int a bitor 5;"); 22237 verifyFormat("int a compl 5;"); 22238 verifyFormat("int a not 5;"); 22239 verifyFormat("int a not_eq 5;"); 22240 verifyFormat("int a or 5;"); 22241 verifyFormat("int a xor 5;"); 22242 verifyFormat("int a xor_eq 5;"); 22243 22244 verifyFormat("int a compl(5);"); 22245 verifyFormat("int a not(5);"); 22246 22247 /* FIXME handle alternate tokens 22248 * https://en.cppreference.com/w/cpp/language/operator_alternative 22249 // alternative tokens 22250 verifyFormat("compl foo();"); // ~foo(); 22251 verifyFormat("foo() <%%>;"); // foo(); 22252 verifyFormat("void foo() <%%>;"); // void foo(){} 22253 verifyFormat("int a <:1:>;"); // int a[1];[ 22254 verifyFormat("%:define ABC abc"); // #define ABC abc 22255 verifyFormat("%:%:"); // ## 22256 */ 22257 } 22258 22259 TEST_F(FormatTest, STLWhileNotDefineChed) { 22260 verifyFormat("#if defined(while)\n" 22261 "#define while EMIT WARNING C4005\n" 22262 "#endif // while"); 22263 } 22264 22265 TEST_F(FormatTest, OperatorSpacing) { 22266 FormatStyle Style = getLLVMStyle(); 22267 Style.PointerAlignment = FormatStyle::PAS_Right; 22268 verifyFormat("Foo::operator*();", Style); 22269 verifyFormat("Foo::operator void *();", Style); 22270 verifyFormat("Foo::operator void **();", Style); 22271 verifyFormat("Foo::operator void *&();", Style); 22272 verifyFormat("Foo::operator void *&&();", Style); 22273 verifyFormat("Foo::operator void const *();", Style); 22274 verifyFormat("Foo::operator void const **();", Style); 22275 verifyFormat("Foo::operator void const *&();", Style); 22276 verifyFormat("Foo::operator void const *&&();", Style); 22277 verifyFormat("Foo::operator()(void *);", Style); 22278 verifyFormat("Foo::operator*(void *);", Style); 22279 verifyFormat("Foo::operator*();", Style); 22280 verifyFormat("Foo::operator**();", Style); 22281 verifyFormat("Foo::operator&();", Style); 22282 verifyFormat("Foo::operator<int> *();", Style); 22283 verifyFormat("Foo::operator<Foo> *();", Style); 22284 verifyFormat("Foo::operator<int> **();", Style); 22285 verifyFormat("Foo::operator<Foo> **();", Style); 22286 verifyFormat("Foo::operator<int> &();", Style); 22287 verifyFormat("Foo::operator<Foo> &();", Style); 22288 verifyFormat("Foo::operator<int> &&();", Style); 22289 verifyFormat("Foo::operator<Foo> &&();", Style); 22290 verifyFormat("Foo::operator<int> *&();", Style); 22291 verifyFormat("Foo::operator<Foo> *&();", Style); 22292 verifyFormat("Foo::operator<int> *&&();", Style); 22293 verifyFormat("Foo::operator<Foo> *&&();", Style); 22294 verifyFormat("operator*(int (*)(), class Foo);", Style); 22295 22296 verifyFormat("Foo::operator&();", Style); 22297 verifyFormat("Foo::operator void &();", Style); 22298 verifyFormat("Foo::operator void const &();", Style); 22299 verifyFormat("Foo::operator()(void &);", Style); 22300 verifyFormat("Foo::operator&(void &);", Style); 22301 verifyFormat("Foo::operator&();", Style); 22302 verifyFormat("operator&(int (&)(), class Foo);", Style); 22303 verifyFormat("operator&&(int (&)(), class Foo);", Style); 22304 22305 verifyFormat("Foo::operator&&();", Style); 22306 verifyFormat("Foo::operator**();", Style); 22307 verifyFormat("Foo::operator void &&();", Style); 22308 verifyFormat("Foo::operator void const &&();", Style); 22309 verifyFormat("Foo::operator()(void &&);", Style); 22310 verifyFormat("Foo::operator&&(void &&);", Style); 22311 verifyFormat("Foo::operator&&();", Style); 22312 verifyFormat("operator&&(int (&&)(), class Foo);", Style); 22313 verifyFormat("operator const nsTArrayRight<E> &()", Style); 22314 verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()", 22315 Style); 22316 verifyFormat("operator void **()", Style); 22317 verifyFormat("operator const FooRight<Object> &()", Style); 22318 verifyFormat("operator const FooRight<Object> *()", Style); 22319 verifyFormat("operator const FooRight<Object> **()", Style); 22320 verifyFormat("operator const FooRight<Object> *&()", Style); 22321 verifyFormat("operator const FooRight<Object> *&&()", Style); 22322 22323 Style.PointerAlignment = FormatStyle::PAS_Left; 22324 verifyFormat("Foo::operator*();", Style); 22325 verifyFormat("Foo::operator**();", Style); 22326 verifyFormat("Foo::operator void*();", Style); 22327 verifyFormat("Foo::operator void**();", Style); 22328 verifyFormat("Foo::operator void*&();", Style); 22329 verifyFormat("Foo::operator void*&&();", Style); 22330 verifyFormat("Foo::operator void const*();", Style); 22331 verifyFormat("Foo::operator void const**();", Style); 22332 verifyFormat("Foo::operator void const*&();", Style); 22333 verifyFormat("Foo::operator void const*&&();", Style); 22334 verifyFormat("Foo::operator/*comment*/ void*();", Style); 22335 verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style); 22336 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style); 22337 verifyFormat("Foo::operator()(void*);", Style); 22338 verifyFormat("Foo::operator*(void*);", Style); 22339 verifyFormat("Foo::operator*();", Style); 22340 verifyFormat("Foo::operator<int>*();", Style); 22341 verifyFormat("Foo::operator<Foo>*();", Style); 22342 verifyFormat("Foo::operator<int>**();", Style); 22343 verifyFormat("Foo::operator<Foo>**();", Style); 22344 verifyFormat("Foo::operator<Foo>*&();", Style); 22345 verifyFormat("Foo::operator<int>&();", Style); 22346 verifyFormat("Foo::operator<Foo>&();", Style); 22347 verifyFormat("Foo::operator<int>&&();", Style); 22348 verifyFormat("Foo::operator<Foo>&&();", Style); 22349 verifyFormat("Foo::operator<int>*&();", Style); 22350 verifyFormat("Foo::operator<Foo>*&();", Style); 22351 verifyFormat("operator*(int (*)(), class Foo);", Style); 22352 22353 verifyFormat("Foo::operator&();", Style); 22354 verifyFormat("Foo::operator void&();", Style); 22355 verifyFormat("Foo::operator void const&();", Style); 22356 verifyFormat("Foo::operator/*comment*/ void&();", Style); 22357 verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style); 22358 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style); 22359 verifyFormat("Foo::operator()(void&);", Style); 22360 verifyFormat("Foo::operator&(void&);", Style); 22361 verifyFormat("Foo::operator&();", Style); 22362 verifyFormat("operator&(int (&)(), class Foo);", Style); 22363 verifyFormat("operator&(int (&&)(), class Foo);", Style); 22364 verifyFormat("operator&&(int (&&)(), class Foo);", Style); 22365 22366 verifyFormat("Foo::operator&&();", Style); 22367 verifyFormat("Foo::operator void&&();", Style); 22368 verifyFormat("Foo::operator void const&&();", Style); 22369 verifyFormat("Foo::operator/*comment*/ void&&();", Style); 22370 verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style); 22371 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style); 22372 verifyFormat("Foo::operator()(void&&);", Style); 22373 verifyFormat("Foo::operator&&(void&&);", Style); 22374 verifyFormat("Foo::operator&&();", Style); 22375 verifyFormat("operator&&(int (&&)(), class Foo);", Style); 22376 verifyFormat("operator const nsTArrayLeft<E>&()", Style); 22377 verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()", 22378 Style); 22379 verifyFormat("operator void**()", Style); 22380 verifyFormat("operator const FooLeft<Object>&()", Style); 22381 verifyFormat("operator const FooLeft<Object>*()", Style); 22382 verifyFormat("operator const FooLeft<Object>**()", Style); 22383 verifyFormat("operator const FooLeft<Object>*&()", Style); 22384 verifyFormat("operator const FooLeft<Object>*&&()", Style); 22385 22386 // PR45107 22387 verifyFormat("operator Vector<String>&();", Style); 22388 verifyFormat("operator const Vector<String>&();", Style); 22389 verifyFormat("operator foo::Bar*();", Style); 22390 verifyFormat("operator const Foo<X>::Bar<Y>*();", Style); 22391 verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();", 22392 Style); 22393 22394 Style.PointerAlignment = FormatStyle::PAS_Middle; 22395 verifyFormat("Foo::operator*();", Style); 22396 verifyFormat("Foo::operator void *();", Style); 22397 verifyFormat("Foo::operator()(void *);", Style); 22398 verifyFormat("Foo::operator*(void *);", Style); 22399 verifyFormat("Foo::operator*();", Style); 22400 verifyFormat("operator*(int (*)(), class Foo);", Style); 22401 22402 verifyFormat("Foo::operator&();", Style); 22403 verifyFormat("Foo::operator void &();", Style); 22404 verifyFormat("Foo::operator void const &();", Style); 22405 verifyFormat("Foo::operator()(void &);", Style); 22406 verifyFormat("Foo::operator&(void &);", Style); 22407 verifyFormat("Foo::operator&();", Style); 22408 verifyFormat("operator&(int (&)(), class Foo);", Style); 22409 22410 verifyFormat("Foo::operator&&();", Style); 22411 verifyFormat("Foo::operator void &&();", Style); 22412 verifyFormat("Foo::operator void const &&();", Style); 22413 verifyFormat("Foo::operator()(void &&);", Style); 22414 verifyFormat("Foo::operator&&(void &&);", Style); 22415 verifyFormat("Foo::operator&&();", Style); 22416 verifyFormat("operator&&(int (&&)(), class Foo);", Style); 22417 } 22418 22419 TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) { 22420 FormatStyle Style = getLLVMStyle(); 22421 // PR46157 22422 verifyFormat("foo(operator+, -42);", Style); 22423 verifyFormat("foo(operator++, -42);", Style); 22424 verifyFormat("foo(operator--, -42);", Style); 22425 verifyFormat("foo(-42, operator--);", Style); 22426 verifyFormat("foo(-42, operator, );", Style); 22427 verifyFormat("foo(operator, , -42);", Style); 22428 } 22429 22430 TEST_F(FormatTest, WhitespaceSensitiveMacros) { 22431 FormatStyle Style = getLLVMStyle(); 22432 Style.WhitespaceSensitiveMacros.push_back("FOO"); 22433 22434 // Don't use the helpers here, since 'mess up' will change the whitespace 22435 // and these are all whitespace sensitive by definition 22436 EXPECT_EQ("FOO(String-ized&Messy+But(: :Still)=Intentional);", 22437 format("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style)); 22438 EXPECT_EQ( 22439 "FOO(String-ized&Messy+But\\(: :Still)=Intentional);", 22440 format("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style)); 22441 EXPECT_EQ("FOO(String-ized&Messy+But,: :Still=Intentional);", 22442 format("FOO(String-ized&Messy+But,: :Still=Intentional);", Style)); 22443 EXPECT_EQ("FOO(String-ized&Messy+But,: :\n" 22444 " Still=Intentional);", 22445 format("FOO(String-ized&Messy+But,: :\n" 22446 " Still=Intentional);", 22447 Style)); 22448 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 22449 EXPECT_EQ("FOO(String-ized=&Messy+But,: :\n" 22450 " Still=Intentional);", 22451 format("FOO(String-ized=&Messy+But,: :\n" 22452 " Still=Intentional);", 22453 Style)); 22454 22455 Style.ColumnLimit = 21; 22456 EXPECT_EQ("FOO(String-ized&Messy+But: :Still=Intentional);", 22457 format("FOO(String-ized&Messy+But: :Still=Intentional);", Style)); 22458 } 22459 22460 TEST_F(FormatTest, VeryLongNamespaceCommentSplit) { 22461 // These tests are not in NamespaceFixer because that doesn't 22462 // test its interaction with line wrapping 22463 FormatStyle Style = getLLVMStyleWithColumns(80); 22464 verifyFormat("namespace {\n" 22465 "int i;\n" 22466 "int j;\n" 22467 "} // namespace", 22468 Style); 22469 22470 verifyFormat("namespace AAA {\n" 22471 "int i;\n" 22472 "int j;\n" 22473 "} // namespace AAA", 22474 Style); 22475 22476 EXPECT_EQ("namespace Averyveryveryverylongnamespace {\n" 22477 "int i;\n" 22478 "int j;\n" 22479 "} // namespace Averyveryveryverylongnamespace", 22480 format("namespace Averyveryveryverylongnamespace {\n" 22481 "int i;\n" 22482 "int j;\n" 22483 "}", 22484 Style)); 22485 22486 EXPECT_EQ( 22487 "namespace " 22488 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" 22489 " went::mad::now {\n" 22490 "int i;\n" 22491 "int j;\n" 22492 "} // namespace\n" 22493 " // " 22494 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" 22495 "went::mad::now", 22496 format("namespace " 22497 "would::it::save::you::a::lot::of::time::if_::i::" 22498 "just::gave::up::and_::went::mad::now {\n" 22499 "int i;\n" 22500 "int j;\n" 22501 "}", 22502 Style)); 22503 22504 // This used to duplicate the comment again and again on subsequent runs 22505 EXPECT_EQ( 22506 "namespace " 22507 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" 22508 " went::mad::now {\n" 22509 "int i;\n" 22510 "int j;\n" 22511 "} // namespace\n" 22512 " // " 22513 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" 22514 "went::mad::now", 22515 format("namespace " 22516 "would::it::save::you::a::lot::of::time::if_::i::" 22517 "just::gave::up::and_::went::mad::now {\n" 22518 "int i;\n" 22519 "int j;\n" 22520 "} // namespace\n" 22521 " // " 22522 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::" 22523 "and_::went::mad::now", 22524 Style)); 22525 } 22526 22527 TEST_F(FormatTest, LikelyUnlikely) { 22528 FormatStyle Style = getLLVMStyle(); 22529 22530 verifyFormat("if (argc > 5) [[unlikely]] {\n" 22531 " return 29;\n" 22532 "}", 22533 Style); 22534 22535 verifyFormat("if (argc > 5) [[likely]] {\n" 22536 " return 29;\n" 22537 "}", 22538 Style); 22539 22540 verifyFormat("if (argc > 5) [[unlikely]] {\n" 22541 " return 29;\n" 22542 "} else [[likely]] {\n" 22543 " return 42;\n" 22544 "}\n", 22545 Style); 22546 22547 verifyFormat("if (argc > 5) [[unlikely]] {\n" 22548 " return 29;\n" 22549 "} else if (argc > 10) [[likely]] {\n" 22550 " return 99;\n" 22551 "} else {\n" 22552 " return 42;\n" 22553 "}\n", 22554 Style); 22555 22556 verifyFormat("if (argc > 5) [[gnu::unused]] {\n" 22557 " return 29;\n" 22558 "}", 22559 Style); 22560 22561 verifyFormat("if (argc > 5) [[unlikely]]\n" 22562 " return 29;\n", 22563 Style); 22564 verifyFormat("if (argc > 5) [[likely]]\n" 22565 " return 29;\n", 22566 Style); 22567 22568 Style.AttributeMacros.push_back("UNLIKELY"); 22569 Style.AttributeMacros.push_back("LIKELY"); 22570 verifyFormat("if (argc > 5) UNLIKELY\n" 22571 " return 29;\n", 22572 Style); 22573 22574 verifyFormat("if (argc > 5) UNLIKELY {\n" 22575 " return 29;\n" 22576 "}", 22577 Style); 22578 verifyFormat("if (argc > 5) UNLIKELY {\n" 22579 " return 29;\n" 22580 "} else [[likely]] {\n" 22581 " return 42;\n" 22582 "}\n", 22583 Style); 22584 verifyFormat("if (argc > 5) UNLIKELY {\n" 22585 " return 29;\n" 22586 "} else LIKELY {\n" 22587 " return 42;\n" 22588 "}\n", 22589 Style); 22590 verifyFormat("if (argc > 5) [[unlikely]] {\n" 22591 " return 29;\n" 22592 "} else LIKELY {\n" 22593 " return 42;\n" 22594 "}\n", 22595 Style); 22596 } 22597 22598 TEST_F(FormatTest, PenaltyIndentedWhitespace) { 22599 verifyFormat("Constructor()\n" 22600 " : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 22601 " aaaa(aaaaaaaaaaaaaaaaaa, " 22602 "aaaaaaaaaaaaaaaaaat))"); 22603 verifyFormat("Constructor()\n" 22604 " : aaaaaaaaaaaaa(aaaaaa), " 22605 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)"); 22606 22607 FormatStyle StyleWithWhitespacePenalty = getLLVMStyle(); 22608 StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5; 22609 verifyFormat("Constructor()\n" 22610 " : aaaaaa(aaaaaa),\n" 22611 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 22612 " aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))", 22613 StyleWithWhitespacePenalty); 22614 verifyFormat("Constructor()\n" 22615 " : aaaaaaaaaaaaa(aaaaaa), " 22616 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)", 22617 StyleWithWhitespacePenalty); 22618 } 22619 22620 TEST_F(FormatTest, LLVMDefaultStyle) { 22621 FormatStyle Style = getLLVMStyle(); 22622 verifyFormat("extern \"C\" {\n" 22623 "int foo();\n" 22624 "}", 22625 Style); 22626 } 22627 TEST_F(FormatTest, GNUDefaultStyle) { 22628 FormatStyle Style = getGNUStyle(); 22629 verifyFormat("extern \"C\"\n" 22630 "{\n" 22631 " int foo ();\n" 22632 "}", 22633 Style); 22634 } 22635 TEST_F(FormatTest, MozillaDefaultStyle) { 22636 FormatStyle Style = getMozillaStyle(); 22637 verifyFormat("extern \"C\"\n" 22638 "{\n" 22639 " int foo();\n" 22640 "}", 22641 Style); 22642 } 22643 TEST_F(FormatTest, GoogleDefaultStyle) { 22644 FormatStyle Style = getGoogleStyle(); 22645 verifyFormat("extern \"C\" {\n" 22646 "int foo();\n" 22647 "}", 22648 Style); 22649 } 22650 TEST_F(FormatTest, ChromiumDefaultStyle) { 22651 FormatStyle Style = getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp); 22652 verifyFormat("extern \"C\" {\n" 22653 "int foo();\n" 22654 "}", 22655 Style); 22656 } 22657 TEST_F(FormatTest, MicrosoftDefaultStyle) { 22658 FormatStyle Style = getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp); 22659 verifyFormat("extern \"C\"\n" 22660 "{\n" 22661 " int foo();\n" 22662 "}", 22663 Style); 22664 } 22665 TEST_F(FormatTest, WebKitDefaultStyle) { 22666 FormatStyle Style = getWebKitStyle(); 22667 verifyFormat("extern \"C\" {\n" 22668 "int foo();\n" 22669 "}", 22670 Style); 22671 } 22672 22673 TEST_F(FormatTest, ConceptsAndRequires) { 22674 FormatStyle Style = getLLVMStyle(); 22675 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 22676 22677 verifyFormat("template <typename T>\n" 22678 "concept Hashable = requires(T a) {\n" 22679 " { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n" 22680 "};", 22681 Style); 22682 verifyFormat("template <typename T>\n" 22683 "concept EqualityComparable = requires(T a, T b) {\n" 22684 " { a == b } -> bool;\n" 22685 "};", 22686 Style); 22687 verifyFormat("template <typename T>\n" 22688 "concept EqualityComparable = requires(T a, T b) {\n" 22689 " { a == b } -> bool;\n" 22690 " { a != b } -> bool;\n" 22691 "};", 22692 Style); 22693 verifyFormat("template <typename T>\n" 22694 "concept EqualityComparable = requires(T a, T b) {\n" 22695 " { a == b } -> bool;\n" 22696 " { a != b } -> bool;\n" 22697 "};", 22698 Style); 22699 22700 verifyFormat("template <typename It>\n" 22701 "requires Iterator<It>\n" 22702 "void sort(It begin, It end) {\n" 22703 " //....\n" 22704 "}", 22705 Style); 22706 22707 verifyFormat("template <typename T>\n" 22708 "concept Large = sizeof(T) > 10;", 22709 Style); 22710 22711 verifyFormat("template <typename T, typename U>\n" 22712 "concept FooableWith = requires(T t, U u) {\n" 22713 " typename T::foo_type;\n" 22714 " { t.foo(u) } -> typename T::foo_type;\n" 22715 " t++;\n" 22716 "};\n" 22717 "void doFoo(FooableWith<int> auto t) {\n" 22718 " t.foo(3);\n" 22719 "}", 22720 Style); 22721 verifyFormat("template <typename T>\n" 22722 "concept Context = sizeof(T) == 1;", 22723 Style); 22724 verifyFormat("template <typename T>\n" 22725 "concept Context = is_specialization_of_v<context, T>;", 22726 Style); 22727 verifyFormat("template <typename T>\n" 22728 "concept Node = std::is_object_v<T>;", 22729 Style); 22730 verifyFormat("template <typename T>\n" 22731 "concept Tree = true;", 22732 Style); 22733 22734 verifyFormat("template <typename T> int g(T i) requires Concept1<I> {\n" 22735 " //...\n" 22736 "}", 22737 Style); 22738 22739 verifyFormat( 22740 "template <typename T> int g(T i) requires Concept1<I> && Concept2<I> {\n" 22741 " //...\n" 22742 "}", 22743 Style); 22744 22745 verifyFormat( 22746 "template <typename T> int g(T i) requires Concept1<I> || Concept2<I> {\n" 22747 " //...\n" 22748 "}", 22749 Style); 22750 22751 verifyFormat("template <typename T>\n" 22752 "veryveryvery_long_return_type g(T i) requires Concept1<I> || " 22753 "Concept2<I> {\n" 22754 " //...\n" 22755 "}", 22756 Style); 22757 22758 verifyFormat("template <typename T>\n" 22759 "veryveryvery_long_return_type g(T i) requires Concept1<I> && " 22760 "Concept2<I> {\n" 22761 " //...\n" 22762 "}", 22763 Style); 22764 22765 verifyFormat( 22766 "template <typename T>\n" 22767 "veryveryvery_long_return_type g(T i) requires Concept1 && Concept2 {\n" 22768 " //...\n" 22769 "}", 22770 Style); 22771 22772 verifyFormat( 22773 "template <typename T>\n" 22774 "veryveryvery_long_return_type g(T i) requires Concept1 || Concept2 {\n" 22775 " //...\n" 22776 "}", 22777 Style); 22778 22779 verifyFormat("template <typename It>\n" 22780 "requires Foo<It>() && Bar<It> {\n" 22781 " //....\n" 22782 "}", 22783 Style); 22784 22785 verifyFormat("template <typename It>\n" 22786 "requires Foo<Bar<It>>() && Bar<Foo<It, It>> {\n" 22787 " //....\n" 22788 "}", 22789 Style); 22790 22791 verifyFormat("template <typename It>\n" 22792 "requires Foo<Bar<It, It>>() && Bar<Foo<It, It>> {\n" 22793 " //....\n" 22794 "}", 22795 Style); 22796 22797 verifyFormat( 22798 "template <typename It>\n" 22799 "requires Foo<Bar<It>, Baz<It>>() && Bar<Foo<It>, Baz<It, It>> {\n" 22800 " //....\n" 22801 "}", 22802 Style); 22803 22804 Style.IndentRequires = true; 22805 verifyFormat("template <typename It>\n" 22806 " requires Iterator<It>\n" 22807 "void sort(It begin, It end) {\n" 22808 " //....\n" 22809 "}", 22810 Style); 22811 verifyFormat("template <std::size index_>\n" 22812 " requires(index_ < sizeof...(Children_))\n" 22813 "Tree auto &child() {\n" 22814 " // ...\n" 22815 "}", 22816 Style); 22817 22818 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 22819 verifyFormat("template <typename T>\n" 22820 "concept Hashable = requires (T a) {\n" 22821 " { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n" 22822 "};", 22823 Style); 22824 22825 verifyFormat("template <class T = void>\n" 22826 " requires EqualityComparable<T> || Same<T, void>\n" 22827 "struct equal_to;", 22828 Style); 22829 22830 verifyFormat("template <class T>\n" 22831 " requires requires {\n" 22832 " T{};\n" 22833 " T (int);\n" 22834 " }\n", 22835 Style); 22836 22837 Style.ColumnLimit = 78; 22838 verifyFormat("template <typename T>\n" 22839 "concept Context = Traits<typename T::traits_type> and\n" 22840 " Interface<typename T::interface_type> and\n" 22841 " Request<typename T::request_type> and\n" 22842 " Response<typename T::response_type> and\n" 22843 " ContextExtension<typename T::extension_type> and\n" 22844 " ::std::is_copy_constructable<T> and " 22845 "::std::is_move_constructable<T> and\n" 22846 " requires (T c) {\n" 22847 " { c.response; } -> Response;\n" 22848 "} and requires (T c) {\n" 22849 " { c.request; } -> Request;\n" 22850 "}\n", 22851 Style); 22852 22853 verifyFormat("template <typename T>\n" 22854 "concept Context = Traits<typename T::traits_type> or\n" 22855 " Interface<typename T::interface_type> or\n" 22856 " Request<typename T::request_type> or\n" 22857 " Response<typename T::response_type> or\n" 22858 " ContextExtension<typename T::extension_type> or\n" 22859 " ::std::is_copy_constructable<T> or " 22860 "::std::is_move_constructable<T> or\n" 22861 " requires (T c) {\n" 22862 " { c.response; } -> Response;\n" 22863 "} or requires (T c) {\n" 22864 " { c.request; } -> Request;\n" 22865 "}\n", 22866 Style); 22867 22868 verifyFormat("template <typename T>\n" 22869 "concept Context = Traits<typename T::traits_type> &&\n" 22870 " Interface<typename T::interface_type> &&\n" 22871 " Request<typename T::request_type> &&\n" 22872 " Response<typename T::response_type> &&\n" 22873 " ContextExtension<typename T::extension_type> &&\n" 22874 " ::std::is_copy_constructable<T> && " 22875 "::std::is_move_constructable<T> &&\n" 22876 " requires (T c) {\n" 22877 " { c.response; } -> Response;\n" 22878 "} && requires (T c) {\n" 22879 " { c.request; } -> Request;\n" 22880 "}\n", 22881 Style); 22882 22883 verifyFormat("template <typename T>\nconcept someConcept = Constraint1<T> && " 22884 "Constraint2<T>;"); 22885 22886 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 22887 Style.BraceWrapping.AfterFunction = true; 22888 Style.BraceWrapping.AfterClass = true; 22889 Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 22890 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 22891 verifyFormat("void Foo () requires (std::copyable<T>)\n" 22892 "{\n" 22893 " return\n" 22894 "}\n", 22895 Style); 22896 22897 verifyFormat("void Foo () requires std::copyable<T>\n" 22898 "{\n" 22899 " return\n" 22900 "}\n", 22901 Style); 22902 22903 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 22904 " requires (std::invocable<F, std::invoke_result_t<Args>...>)\n" 22905 "struct constant;", 22906 Style); 22907 22908 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 22909 " requires std::invocable<F, std::invoke_result_t<Args>...>\n" 22910 "struct constant;", 22911 Style); 22912 22913 verifyFormat("template <class T>\n" 22914 "class plane_with_very_very_very_long_name\n" 22915 "{\n" 22916 " constexpr plane_with_very_very_very_long_name () requires " 22917 "std::copyable<T>\n" 22918 " : plane_with_very_very_very_long_name (1)\n" 22919 " {\n" 22920 " }\n" 22921 "}\n", 22922 Style); 22923 22924 verifyFormat("template <class T>\n" 22925 "class plane_with_long_name\n" 22926 "{\n" 22927 " constexpr plane_with_long_name () requires std::copyable<T>\n" 22928 " : plane_with_long_name (1)\n" 22929 " {\n" 22930 " }\n" 22931 "}\n", 22932 Style); 22933 22934 Style.BreakBeforeConceptDeclarations = false; 22935 verifyFormat("template <typename T> concept Tree = true;", Style); 22936 22937 Style.IndentRequires = false; 22938 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 22939 "requires (std::invocable<F, std::invoke_result_t<Args>...>) " 22940 "struct constant;", 22941 Style); 22942 } 22943 22944 TEST_F(FormatTest, StatementAttributeLikeMacros) { 22945 FormatStyle Style = getLLVMStyle(); 22946 StringRef Source = "void Foo::slot() {\n" 22947 " unsigned char MyChar = 'x';\n" 22948 " emit signal(MyChar);\n" 22949 " Q_EMIT signal(MyChar);\n" 22950 "}"; 22951 22952 EXPECT_EQ(Source, format(Source, Style)); 22953 22954 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 22955 EXPECT_EQ("void Foo::slot() {\n" 22956 " unsigned char MyChar = 'x';\n" 22957 " emit signal(MyChar);\n" 22958 " Q_EMIT signal(MyChar);\n" 22959 "}", 22960 format(Source, Style)); 22961 22962 Style.StatementAttributeLikeMacros.push_back("emit"); 22963 EXPECT_EQ(Source, format(Source, Style)); 22964 22965 Style.StatementAttributeLikeMacros = {}; 22966 EXPECT_EQ("void Foo::slot() {\n" 22967 " unsigned char MyChar = 'x';\n" 22968 " emit signal(MyChar);\n" 22969 " Q_EMIT signal(MyChar);\n" 22970 "}", 22971 format(Source, Style)); 22972 } 22973 22974 TEST_F(FormatTest, IndentAccessModifiers) { 22975 FormatStyle Style = getLLVMStyle(); 22976 Style.IndentAccessModifiers = true; 22977 // Members are *two* levels below the record; 22978 // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation. 22979 verifyFormat("class C {\n" 22980 " int i;\n" 22981 "};\n", 22982 Style); 22983 verifyFormat("union C {\n" 22984 " int i;\n" 22985 " unsigned u;\n" 22986 "};\n", 22987 Style); 22988 // Access modifiers should be indented one level below the record. 22989 verifyFormat("class C {\n" 22990 " public:\n" 22991 " int i;\n" 22992 "};\n", 22993 Style); 22994 verifyFormat("struct S {\n" 22995 " private:\n" 22996 " class C {\n" 22997 " int j;\n" 22998 "\n" 22999 " public:\n" 23000 " C();\n" 23001 " };\n" 23002 "\n" 23003 " public:\n" 23004 " int i;\n" 23005 "};\n", 23006 Style); 23007 // Enumerations are not records and should be unaffected. 23008 Style.AllowShortEnumsOnASingleLine = false; 23009 verifyFormat("enum class E {\n" 23010 " A,\n" 23011 " B\n" 23012 "};\n", 23013 Style); 23014 // Test with a different indentation width; 23015 // also proves that the result is Style.AccessModifierOffset agnostic. 23016 Style.IndentWidth = 3; 23017 verifyFormat("class C {\n" 23018 " public:\n" 23019 " int i;\n" 23020 "};\n", 23021 Style); 23022 } 23023 23024 TEST_F(FormatTest, LimitlessStringsAndComments) { 23025 auto Style = getLLVMStyleWithColumns(0); 23026 constexpr StringRef Code = 23027 "/**\n" 23028 " * This is a multiline comment with quite some long lines, at least for " 23029 "the LLVM Style.\n" 23030 " * We will redo this with strings and line comments. Just to check if " 23031 "everything is working.\n" 23032 " */\n" 23033 "bool foo() {\n" 23034 " /* Single line multi line comment. */\n" 23035 " const std::string String = \"This is a multiline string with quite " 23036 "some long lines, at least for the LLVM Style.\"\n" 23037 " \"We already did it with multi line " 23038 "comments, and we will do it with line comments. Just to check if " 23039 "everything is working.\";\n" 23040 " // This is a line comment (block) with quite some long lines, at " 23041 "least for the LLVM Style.\n" 23042 " // We already did this with multi line comments and strings. Just to " 23043 "check if everything is working.\n" 23044 " const std::string SmallString = \"Hello World\";\n" 23045 " // Small line comment\n" 23046 " return String.size() > SmallString.size();\n" 23047 "}"; 23048 EXPECT_EQ(Code, format(Code, Style)); 23049 } 23050 23051 TEST_F(FormatTest, FormatDecayCopy) { 23052 // error cases from unit tests 23053 verifyFormat("foo(auto())"); 23054 verifyFormat("foo(auto{})"); 23055 verifyFormat("foo(auto({}))"); 23056 verifyFormat("foo(auto{{}})"); 23057 23058 verifyFormat("foo(auto(1))"); 23059 verifyFormat("foo(auto{1})"); 23060 verifyFormat("foo(new auto(1))"); 23061 verifyFormat("foo(new auto{1})"); 23062 verifyFormat("decltype(auto(1)) x;"); 23063 verifyFormat("decltype(auto{1}) x;"); 23064 verifyFormat("auto(x);"); 23065 verifyFormat("auto{x};"); 23066 verifyFormat("new auto{x};"); 23067 verifyFormat("auto{x} = y;"); 23068 verifyFormat("auto(x) = y;"); // actually a declaration, but this is clearly 23069 // the user's own fault 23070 verifyFormat("integral auto(x) = y;"); // actually a declaration, but this is 23071 // clearly the user's own fault 23072 verifyFormat("auto(*p)() = f;"); // actually a declaration; TODO FIXME 23073 } 23074 23075 TEST_F(FormatTest, Cpp20ModulesSupport) { 23076 FormatStyle Style = getLLVMStyle(); 23077 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 23078 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 23079 23080 verifyFormat("export import foo;", Style); 23081 verifyFormat("export import foo:bar;", Style); 23082 verifyFormat("export import foo.bar;", Style); 23083 verifyFormat("export import foo.bar:baz;", Style); 23084 verifyFormat("export import :bar;", Style); 23085 verifyFormat("export module foo:bar;", Style); 23086 verifyFormat("export module foo;", Style); 23087 verifyFormat("export module foo.bar;", Style); 23088 verifyFormat("export module foo.bar:baz;", Style); 23089 verifyFormat("export import <string_view>;", Style); 23090 23091 verifyFormat("export type_name var;", Style); 23092 verifyFormat("template <class T> export using A = B<T>;", Style); 23093 verifyFormat("export using A = B;", Style); 23094 verifyFormat("export int func() {\n" 23095 " foo();\n" 23096 "}", 23097 Style); 23098 verifyFormat("export struct {\n" 23099 " int foo;\n" 23100 "};", 23101 Style); 23102 verifyFormat("export {\n" 23103 " int foo;\n" 23104 "};", 23105 Style); 23106 verifyFormat("export export char const *hello() { return \"hello\"; }"); 23107 23108 verifyFormat("import bar;", Style); 23109 verifyFormat("import foo.bar;", Style); 23110 verifyFormat("import foo:bar;", Style); 23111 verifyFormat("import :bar;", Style); 23112 verifyFormat("import <ctime>;", Style); 23113 verifyFormat("import \"header\";", Style); 23114 23115 verifyFormat("module foo;", Style); 23116 verifyFormat("module foo:bar;", Style); 23117 verifyFormat("module foo.bar;", Style); 23118 verifyFormat("module;", Style); 23119 23120 verifyFormat("export namespace hi {\n" 23121 "const char *sayhi();\n" 23122 "}", 23123 Style); 23124 23125 verifyFormat("module :private;", Style); 23126 verifyFormat("import <foo/bar.h>;", Style); 23127 verifyFormat("import foo...bar;", Style); 23128 verifyFormat("import ..........;", Style); 23129 verifyFormat("module foo:private;", Style); 23130 verifyFormat("import a", Style); 23131 verifyFormat("module a", Style); 23132 verifyFormat("export import a", Style); 23133 verifyFormat("export module a", Style); 23134 23135 verifyFormat("import", Style); 23136 verifyFormat("module", Style); 23137 verifyFormat("export", Style); 23138 } 23139 23140 TEST_F(FormatTest, CoroutineForCoawait) { 23141 FormatStyle Style = getLLVMStyle(); 23142 verifyFormat("for co_await (auto x : range())\n ;"); 23143 verifyFormat("for (auto i : arr) {\n" 23144 "}", 23145 Style); 23146 verifyFormat("for co_await (auto i : arr) {\n" 23147 "}", 23148 Style); 23149 verifyFormat("for co_await (auto i : foo(T{})) {\n" 23150 "}", 23151 Style); 23152 } 23153 23154 TEST_F(FormatTest, CoroutineCoAwait) { 23155 verifyFormat("int x = co_await foo();"); 23156 verifyFormat("int x = (co_await foo());"); 23157 verifyFormat("co_await (42);"); 23158 verifyFormat("void operator co_await(int);"); 23159 verifyFormat("void operator co_await(a);"); 23160 verifyFormat("co_await a;"); 23161 verifyFormat("co_await missing_await_resume{};"); 23162 verifyFormat("co_await a; // comment"); 23163 verifyFormat("void test0() { co_await a; }"); 23164 verifyFormat("co_await co_await co_await foo();"); 23165 verifyFormat("co_await foo().bar();"); 23166 verifyFormat("co_await [this]() -> Task { co_return x; }"); 23167 verifyFormat("co_await [this](int a, int b) -> Task { co_return co_await " 23168 "foo(); }(x, y);"); 23169 23170 FormatStyle Style = getLLVMStyleWithColumns(40); 23171 verifyFormat("co_await [this](int a, int b) -> Task {\n" 23172 " co_return co_await foo();\n" 23173 "}(x, y);", 23174 Style); 23175 verifyFormat("co_await;"); 23176 } 23177 23178 TEST_F(FormatTest, CoroutineCoYield) { 23179 verifyFormat("int x = co_yield foo();"); 23180 verifyFormat("int x = (co_yield foo());"); 23181 verifyFormat("co_yield (42);"); 23182 verifyFormat("co_yield {42};"); 23183 verifyFormat("co_yield 42;"); 23184 verifyFormat("co_yield n++;"); 23185 verifyFormat("co_yield ++n;"); 23186 verifyFormat("co_yield;"); 23187 } 23188 23189 TEST_F(FormatTest, CoroutineCoReturn) { 23190 verifyFormat("co_return (42);"); 23191 verifyFormat("co_return;"); 23192 verifyFormat("co_return {};"); 23193 verifyFormat("co_return x;"); 23194 verifyFormat("co_return co_await foo();"); 23195 verifyFormat("co_return co_yield foo();"); 23196 } 23197 23198 TEST_F(FormatTest, EmptyShortBlock) { 23199 auto Style = getLLVMStyle(); 23200 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 23201 23202 verifyFormat("try {\n" 23203 " doA();\n" 23204 "} catch (Exception &e) {\n" 23205 " e.printStackTrace();\n" 23206 "}\n", 23207 Style); 23208 23209 verifyFormat("try {\n" 23210 " doA();\n" 23211 "} catch (Exception &e) {}\n", 23212 Style); 23213 } 23214 23215 } // namespace 23216 } // namespace format 23217 } // namespace clang 23218