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 // ...but do keep inlining and removing empty lines for non-block extern "C" 266 // functions. 267 verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle()); 268 EXPECT_EQ("extern \"C\" int f() {\n" 269 " int i = 42;\n" 270 " return i;\n" 271 "}", 272 format("extern \"C\" int f() {\n" 273 "\n" 274 " int i = 42;\n" 275 " return i;\n" 276 "}", 277 getGoogleStyle())); 278 279 // Remove empty lines at the beginning and end of blocks. 280 EXPECT_EQ("void f() {\n" 281 "\n" 282 " if (a) {\n" 283 "\n" 284 " f();\n" 285 " }\n" 286 "}", 287 format("void f() {\n" 288 "\n" 289 " if (a) {\n" 290 "\n" 291 " f();\n" 292 "\n" 293 " }\n" 294 "\n" 295 "}", 296 getLLVMStyle())); 297 EXPECT_EQ("void f() {\n" 298 " if (a) {\n" 299 " f();\n" 300 " }\n" 301 "}", 302 format("void f() {\n" 303 "\n" 304 " if (a) {\n" 305 "\n" 306 " f();\n" 307 "\n" 308 " }\n" 309 "\n" 310 "}", 311 getGoogleStyle())); 312 313 // Don't remove empty lines in more complex control statements. 314 EXPECT_EQ("void f() {\n" 315 " if (a) {\n" 316 " f();\n" 317 "\n" 318 " } else if (b) {\n" 319 " f();\n" 320 " }\n" 321 "}", 322 format("void f() {\n" 323 " if (a) {\n" 324 " f();\n" 325 "\n" 326 " } else if (b) {\n" 327 " f();\n" 328 "\n" 329 " }\n" 330 "\n" 331 "}")); 332 333 // Don't remove empty lines before namespace endings. 334 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 335 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 336 EXPECT_EQ("namespace {\n" 337 "int i;\n" 338 "\n" 339 "}", 340 format("namespace {\n" 341 "int i;\n" 342 "\n" 343 "}", 344 LLVMWithNoNamespaceFix)); 345 EXPECT_EQ("namespace {\n" 346 "int i;\n" 347 "}", 348 format("namespace {\n" 349 "int i;\n" 350 "}", 351 LLVMWithNoNamespaceFix)); 352 EXPECT_EQ("namespace {\n" 353 "int i;\n" 354 "\n" 355 "};", 356 format("namespace {\n" 357 "int i;\n" 358 "\n" 359 "};", 360 LLVMWithNoNamespaceFix)); 361 EXPECT_EQ("namespace {\n" 362 "int i;\n" 363 "};", 364 format("namespace {\n" 365 "int i;\n" 366 "};", 367 LLVMWithNoNamespaceFix)); 368 EXPECT_EQ("namespace {\n" 369 "int i;\n" 370 "\n" 371 "}", 372 format("namespace {\n" 373 "int i;\n" 374 "\n" 375 "}")); 376 EXPECT_EQ("namespace {\n" 377 "int i;\n" 378 "\n" 379 "} // namespace", 380 format("namespace {\n" 381 "int i;\n" 382 "\n" 383 "} // namespace")); 384 385 FormatStyle Style = getLLVMStyle(); 386 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 387 Style.MaxEmptyLinesToKeep = 2; 388 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 389 Style.BraceWrapping.AfterClass = true; 390 Style.BraceWrapping.AfterFunction = true; 391 Style.KeepEmptyLinesAtTheStartOfBlocks = false; 392 393 EXPECT_EQ("class Foo\n" 394 "{\n" 395 " Foo() {}\n" 396 "\n" 397 " void funk() {}\n" 398 "};", 399 format("class Foo\n" 400 "{\n" 401 " Foo()\n" 402 " {\n" 403 " }\n" 404 "\n" 405 " void funk() {}\n" 406 "};", 407 Style)); 408 } 409 410 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) { 411 verifyFormat("x = (a) and (b);"); 412 verifyFormat("x = (a) or (b);"); 413 verifyFormat("x = (a) bitand (b);"); 414 verifyFormat("x = (a) bitor (b);"); 415 verifyFormat("x = (a) not_eq (b);"); 416 verifyFormat("x = (a) and_eq (b);"); 417 verifyFormat("x = (a) or_eq (b);"); 418 verifyFormat("x = (a) xor (b);"); 419 } 420 421 TEST_F(FormatTest, RecognizesUnaryOperatorKeywords) { 422 verifyFormat("x = compl(a);"); 423 verifyFormat("x = not(a);"); 424 verifyFormat("x = bitand(a);"); 425 // Unary operator must not be merged with the next identifier 426 verifyFormat("x = compl a;"); 427 verifyFormat("x = not a;"); 428 verifyFormat("x = bitand a;"); 429 } 430 431 //===----------------------------------------------------------------------===// 432 // Tests for control statements. 433 //===----------------------------------------------------------------------===// 434 435 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) { 436 verifyFormat("if (true)\n f();\ng();"); 437 verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); 438 verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); 439 verifyFormat("if constexpr (true)\n" 440 " f();\ng();"); 441 verifyFormat("if CONSTEXPR (true)\n" 442 " f();\ng();"); 443 verifyFormat("if constexpr (a)\n" 444 " if constexpr (b)\n" 445 " if constexpr (c)\n" 446 " g();\n" 447 "h();"); 448 verifyFormat("if CONSTEXPR (a)\n" 449 " if CONSTEXPR (b)\n" 450 " if CONSTEXPR (c)\n" 451 " g();\n" 452 "h();"); 453 verifyFormat("if constexpr (a)\n" 454 " if constexpr (b) {\n" 455 " f();\n" 456 " }\n" 457 "g();"); 458 verifyFormat("if CONSTEXPR (a)\n" 459 " if CONSTEXPR (b) {\n" 460 " f();\n" 461 " }\n" 462 "g();"); 463 464 verifyFormat("if (a)\n" 465 " g();"); 466 verifyFormat("if (a) {\n" 467 " g()\n" 468 "};"); 469 verifyFormat("if (a)\n" 470 " g();\n" 471 "else\n" 472 " g();"); 473 verifyFormat("if (a) {\n" 474 " g();\n" 475 "} else\n" 476 " g();"); 477 verifyFormat("if (a)\n" 478 " g();\n" 479 "else {\n" 480 " g();\n" 481 "}"); 482 verifyFormat("if (a) {\n" 483 " g();\n" 484 "} else {\n" 485 " g();\n" 486 "}"); 487 verifyFormat("if (a)\n" 488 " g();\n" 489 "else if (b)\n" 490 " g();\n" 491 "else\n" 492 " g();"); 493 verifyFormat("if (a) {\n" 494 " g();\n" 495 "} else if (b)\n" 496 " g();\n" 497 "else\n" 498 " g();"); 499 verifyFormat("if (a)\n" 500 " g();\n" 501 "else if (b) {\n" 502 " g();\n" 503 "} else\n" 504 " g();"); 505 verifyFormat("if (a)\n" 506 " g();\n" 507 "else if (b)\n" 508 " g();\n" 509 "else {\n" 510 " g();\n" 511 "}"); 512 verifyFormat("if (a)\n" 513 " g();\n" 514 "else if (b) {\n" 515 " g();\n" 516 "} else {\n" 517 " g();\n" 518 "}"); 519 verifyFormat("if (a) {\n" 520 " g();\n" 521 "} else if (b) {\n" 522 " g();\n" 523 "} else {\n" 524 " g();\n" 525 "}"); 526 527 FormatStyle AllowsMergedIf = getLLVMStyle(); 528 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left; 529 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 530 FormatStyle::SIS_WithoutElse; 531 verifyFormat("if (a)\n" 532 " // comment\n" 533 " f();", 534 AllowsMergedIf); 535 verifyFormat("{\n" 536 " if (a)\n" 537 " label:\n" 538 " f();\n" 539 "}", 540 AllowsMergedIf); 541 verifyFormat("#define A \\\n" 542 " if (a) \\\n" 543 " label: \\\n" 544 " f()", 545 AllowsMergedIf); 546 verifyFormat("if (a)\n" 547 " ;", 548 AllowsMergedIf); 549 verifyFormat("if (a)\n" 550 " if (b) return;", 551 AllowsMergedIf); 552 553 verifyFormat("if (a) // Can't merge this\n" 554 " f();\n", 555 AllowsMergedIf); 556 verifyFormat("if (a) /* still don't merge */\n" 557 " f();", 558 AllowsMergedIf); 559 verifyFormat("if (a) { // Never merge this\n" 560 " f();\n" 561 "}", 562 AllowsMergedIf); 563 verifyFormat("if (a) { /* Never merge this */\n" 564 " f();\n" 565 "}", 566 AllowsMergedIf); 567 568 AllowsMergedIf.ColumnLimit = 14; 569 verifyFormat("if (a) return;", AllowsMergedIf); 570 verifyFormat("if (aaaaaaaaa)\n" 571 " return;", 572 AllowsMergedIf); 573 574 AllowsMergedIf.ColumnLimit = 13; 575 verifyFormat("if (a)\n return;", AllowsMergedIf); 576 577 FormatStyle AllowsMergedIfElse = getLLVMStyle(); 578 AllowsMergedIfElse.AllowShortIfStatementsOnASingleLine = 579 FormatStyle::SIS_AllIfsAndElse; 580 verifyFormat("if (a)\n" 581 " // comment\n" 582 " f();\n" 583 "else\n" 584 " // comment\n" 585 " f();", 586 AllowsMergedIfElse); 587 verifyFormat("{\n" 588 " if (a)\n" 589 " label:\n" 590 " f();\n" 591 " else\n" 592 " label:\n" 593 " f();\n" 594 "}", 595 AllowsMergedIfElse); 596 verifyFormat("if (a)\n" 597 " ;\n" 598 "else\n" 599 " ;", 600 AllowsMergedIfElse); 601 verifyFormat("if (a) {\n" 602 "} else {\n" 603 "}", 604 AllowsMergedIfElse); 605 verifyFormat("if (a) return;\n" 606 "else if (b) return;\n" 607 "else return;", 608 AllowsMergedIfElse); 609 verifyFormat("if (a) {\n" 610 "} else return;", 611 AllowsMergedIfElse); 612 verifyFormat("if (a) {\n" 613 "} else if (b) return;\n" 614 "else return;", 615 AllowsMergedIfElse); 616 verifyFormat("if (a) return;\n" 617 "else if (b) {\n" 618 "} else return;", 619 AllowsMergedIfElse); 620 verifyFormat("if (a)\n" 621 " if (b) return;\n" 622 " else return;", 623 AllowsMergedIfElse); 624 verifyFormat("if constexpr (a)\n" 625 " if constexpr (b) return;\n" 626 " else if constexpr (c) return;\n" 627 " else return;", 628 AllowsMergedIfElse); 629 } 630 631 TEST_F(FormatTest, FormatIfWithoutCompoundStatementButElseWith) { 632 FormatStyle AllowsMergedIf = getLLVMStyle(); 633 AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left; 634 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 635 FormatStyle::SIS_WithoutElse; 636 verifyFormat("if (a)\n" 637 " f();\n" 638 "else {\n" 639 " g();\n" 640 "}", 641 AllowsMergedIf); 642 verifyFormat("if (a)\n" 643 " f();\n" 644 "else\n" 645 " g();\n", 646 AllowsMergedIf); 647 648 verifyFormat("if (a) g();", AllowsMergedIf); 649 verifyFormat("if (a) {\n" 650 " g()\n" 651 "};", 652 AllowsMergedIf); 653 verifyFormat("if (a)\n" 654 " g();\n" 655 "else\n" 656 " g();", 657 AllowsMergedIf); 658 verifyFormat("if (a) {\n" 659 " g();\n" 660 "} else\n" 661 " g();", 662 AllowsMergedIf); 663 verifyFormat("if (a)\n" 664 " g();\n" 665 "else {\n" 666 " g();\n" 667 "}", 668 AllowsMergedIf); 669 verifyFormat("if (a) {\n" 670 " g();\n" 671 "} else {\n" 672 " g();\n" 673 "}", 674 AllowsMergedIf); 675 verifyFormat("if (a)\n" 676 " g();\n" 677 "else if (b)\n" 678 " g();\n" 679 "else\n" 680 " g();", 681 AllowsMergedIf); 682 verifyFormat("if (a) {\n" 683 " g();\n" 684 "} else if (b)\n" 685 " g();\n" 686 "else\n" 687 " g();", 688 AllowsMergedIf); 689 verifyFormat("if (a)\n" 690 " g();\n" 691 "else if (b) {\n" 692 " g();\n" 693 "} else\n" 694 " g();", 695 AllowsMergedIf); 696 verifyFormat("if (a)\n" 697 " g();\n" 698 "else if (b)\n" 699 " g();\n" 700 "else {\n" 701 " g();\n" 702 "}", 703 AllowsMergedIf); 704 verifyFormat("if (a)\n" 705 " g();\n" 706 "else if (b) {\n" 707 " g();\n" 708 "} else {\n" 709 " g();\n" 710 "}", 711 AllowsMergedIf); 712 verifyFormat("if (a) {\n" 713 " g();\n" 714 "} else if (b) {\n" 715 " g();\n" 716 "} else {\n" 717 " g();\n" 718 "}", 719 AllowsMergedIf); 720 721 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 722 FormatStyle::SIS_OnlyFirstIf; 723 724 verifyFormat("if (a) f();\n" 725 "else {\n" 726 " g();\n" 727 "}", 728 AllowsMergedIf); 729 verifyFormat("if (a) f();\n" 730 "else {\n" 731 " if (a) f();\n" 732 " else {\n" 733 " g();\n" 734 " }\n" 735 " g();\n" 736 "}", 737 AllowsMergedIf); 738 739 verifyFormat("if (a) g();", AllowsMergedIf); 740 verifyFormat("if (a) {\n" 741 " g()\n" 742 "};", 743 AllowsMergedIf); 744 verifyFormat("if (a) g();\n" 745 "else\n" 746 " g();", 747 AllowsMergedIf); 748 verifyFormat("if (a) {\n" 749 " g();\n" 750 "} else\n" 751 " g();", 752 AllowsMergedIf); 753 verifyFormat("if (a) g();\n" 754 "else {\n" 755 " g();\n" 756 "}", 757 AllowsMergedIf); 758 verifyFormat("if (a) {\n" 759 " g();\n" 760 "} else {\n" 761 " g();\n" 762 "}", 763 AllowsMergedIf); 764 verifyFormat("if (a) g();\n" 765 "else if (b)\n" 766 " g();\n" 767 "else\n" 768 " g();", 769 AllowsMergedIf); 770 verifyFormat("if (a) {\n" 771 " g();\n" 772 "} else if (b)\n" 773 " g();\n" 774 "else\n" 775 " g();", 776 AllowsMergedIf); 777 verifyFormat("if (a) g();\n" 778 "else if (b) {\n" 779 " g();\n" 780 "} else\n" 781 " g();", 782 AllowsMergedIf); 783 verifyFormat("if (a) g();\n" 784 "else if (b)\n" 785 " g();\n" 786 "else {\n" 787 " g();\n" 788 "}", 789 AllowsMergedIf); 790 verifyFormat("if (a) g();\n" 791 "else if (b) {\n" 792 " g();\n" 793 "} else {\n" 794 " g();\n" 795 "}", 796 AllowsMergedIf); 797 verifyFormat("if (a) {\n" 798 " g();\n" 799 "} else if (b) {\n" 800 " g();\n" 801 "} else {\n" 802 " g();\n" 803 "}", 804 AllowsMergedIf); 805 806 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 807 FormatStyle::SIS_AllIfsAndElse; 808 809 verifyFormat("if (a) f();\n" 810 "else {\n" 811 " g();\n" 812 "}", 813 AllowsMergedIf); 814 verifyFormat("if (a) f();\n" 815 "else {\n" 816 " if (a) f();\n" 817 " else {\n" 818 " g();\n" 819 " }\n" 820 " g();\n" 821 "}", 822 AllowsMergedIf); 823 824 verifyFormat("if (a) g();", AllowsMergedIf); 825 verifyFormat("if (a) {\n" 826 " g()\n" 827 "};", 828 AllowsMergedIf); 829 verifyFormat("if (a) g();\n" 830 "else g();", 831 AllowsMergedIf); 832 verifyFormat("if (a) {\n" 833 " g();\n" 834 "} else g();", 835 AllowsMergedIf); 836 verifyFormat("if (a) g();\n" 837 "else {\n" 838 " g();\n" 839 "}", 840 AllowsMergedIf); 841 verifyFormat("if (a) {\n" 842 " g();\n" 843 "} else {\n" 844 " g();\n" 845 "}", 846 AllowsMergedIf); 847 verifyFormat("if (a) g();\n" 848 "else if (b) g();\n" 849 "else g();", 850 AllowsMergedIf); 851 verifyFormat("if (a) {\n" 852 " g();\n" 853 "} else if (b) g();\n" 854 "else g();", 855 AllowsMergedIf); 856 verifyFormat("if (a) g();\n" 857 "else if (b) {\n" 858 " g();\n" 859 "} else g();", 860 AllowsMergedIf); 861 verifyFormat("if (a) g();\n" 862 "else if (b) g();\n" 863 "else {\n" 864 " g();\n" 865 "}", 866 AllowsMergedIf); 867 verifyFormat("if (a) g();\n" 868 "else if (b) {\n" 869 " g();\n" 870 "} else {\n" 871 " g();\n" 872 "}", 873 AllowsMergedIf); 874 verifyFormat("if (a) {\n" 875 " g();\n" 876 "} else if (b) {\n" 877 " g();\n" 878 "} else {\n" 879 " g();\n" 880 "}", 881 AllowsMergedIf); 882 } 883 884 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) { 885 FormatStyle AllowsMergedLoops = getLLVMStyle(); 886 AllowsMergedLoops.AllowShortLoopsOnASingleLine = true; 887 verifyFormat("while (true) continue;", AllowsMergedLoops); 888 verifyFormat("for (;;) continue;", AllowsMergedLoops); 889 verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops); 890 verifyFormat("while (true)\n" 891 " ;", 892 AllowsMergedLoops); 893 verifyFormat("for (;;)\n" 894 " ;", 895 AllowsMergedLoops); 896 verifyFormat("for (;;)\n" 897 " for (;;) continue;", 898 AllowsMergedLoops); 899 verifyFormat("for (;;) // Can't merge this\n" 900 " continue;", 901 AllowsMergedLoops); 902 verifyFormat("for (;;) /* still don't merge */\n" 903 " continue;", 904 AllowsMergedLoops); 905 verifyFormat("do a++;\n" 906 "while (true);", 907 AllowsMergedLoops); 908 verifyFormat("do /* Don't merge */\n" 909 " a++;\n" 910 "while (true);", 911 AllowsMergedLoops); 912 verifyFormat("do // Don't merge\n" 913 " a++;\n" 914 "while (true);", 915 AllowsMergedLoops); 916 verifyFormat("do\n" 917 " // Don't merge\n" 918 " a++;\n" 919 "while (true);", 920 AllowsMergedLoops); 921 // Without braces labels are interpreted differently. 922 verifyFormat("{\n" 923 " do\n" 924 " label:\n" 925 " a++;\n" 926 " while (true);\n" 927 "}", 928 AllowsMergedLoops); 929 } 930 931 TEST_F(FormatTest, FormatShortBracedStatements) { 932 FormatStyle AllowSimpleBracedStatements = getLLVMStyle(); 933 AllowSimpleBracedStatements.ColumnLimit = 40; 934 AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = 935 FormatStyle::SBS_Always; 936 937 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 938 FormatStyle::SIS_WithoutElse; 939 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 940 941 AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom; 942 AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true; 943 AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false; 944 945 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 946 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 947 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements); 948 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 949 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 950 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 951 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 952 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 953 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 954 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 955 verifyFormat("if (true) { fffffffffffffffffffffff(); }", 956 AllowSimpleBracedStatements); 957 verifyFormat("if (true) {\n" 958 " ffffffffffffffffffffffff();\n" 959 "}", 960 AllowSimpleBracedStatements); 961 verifyFormat("if (true) {\n" 962 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 963 "}", 964 AllowSimpleBracedStatements); 965 verifyFormat("if (true) { //\n" 966 " f();\n" 967 "}", 968 AllowSimpleBracedStatements); 969 verifyFormat("if (true) {\n" 970 " f();\n" 971 " f();\n" 972 "}", 973 AllowSimpleBracedStatements); 974 verifyFormat("if (true) {\n" 975 " f();\n" 976 "} else {\n" 977 " f();\n" 978 "}", 979 AllowSimpleBracedStatements); 980 981 verifyFormat("struct A2 {\n" 982 " int X;\n" 983 "};", 984 AllowSimpleBracedStatements); 985 verifyFormat("typedef struct A2 {\n" 986 " int X;\n" 987 "} A2_t;", 988 AllowSimpleBracedStatements); 989 verifyFormat("template <int> struct A2 {\n" 990 " struct B {};\n" 991 "};", 992 AllowSimpleBracedStatements); 993 994 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 995 FormatStyle::SIS_Never; 996 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 997 verifyFormat("if (true) {\n" 998 " f();\n" 999 "}", 1000 AllowSimpleBracedStatements); 1001 verifyFormat("if (true) {\n" 1002 " f();\n" 1003 "} else {\n" 1004 " f();\n" 1005 "}", 1006 AllowSimpleBracedStatements); 1007 1008 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 1009 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1010 verifyFormat("while (true) {\n" 1011 " f();\n" 1012 "}", 1013 AllowSimpleBracedStatements); 1014 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1015 verifyFormat("for (;;) {\n" 1016 " f();\n" 1017 "}", 1018 AllowSimpleBracedStatements); 1019 1020 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1021 FormatStyle::SIS_WithoutElse; 1022 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true; 1023 AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement = 1024 FormatStyle::BWACS_Always; 1025 1026 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1027 verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements); 1028 verifyFormat("if CONSTEXPR (true) {}", AllowSimpleBracedStatements); 1029 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1030 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1031 verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements); 1032 verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements); 1033 verifyFormat("if CONSTEXPR (true) { f(); }", AllowSimpleBracedStatements); 1034 verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements); 1035 verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements); 1036 verifyFormat("if (true) { fffffffffffffffffffffff(); }", 1037 AllowSimpleBracedStatements); 1038 verifyFormat("if (true)\n" 1039 "{\n" 1040 " ffffffffffffffffffffffff();\n" 1041 "}", 1042 AllowSimpleBracedStatements); 1043 verifyFormat("if (true)\n" 1044 "{\n" 1045 " ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n" 1046 "}", 1047 AllowSimpleBracedStatements); 1048 verifyFormat("if (true)\n" 1049 "{ //\n" 1050 " f();\n" 1051 "}", 1052 AllowSimpleBracedStatements); 1053 verifyFormat("if (true)\n" 1054 "{\n" 1055 " f();\n" 1056 " f();\n" 1057 "}", 1058 AllowSimpleBracedStatements); 1059 verifyFormat("if (true)\n" 1060 "{\n" 1061 " f();\n" 1062 "} else\n" 1063 "{\n" 1064 " f();\n" 1065 "}", 1066 AllowSimpleBracedStatements); 1067 1068 AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = 1069 FormatStyle::SIS_Never; 1070 verifyFormat("if (true) {}", AllowSimpleBracedStatements); 1071 verifyFormat("if (true)\n" 1072 "{\n" 1073 " f();\n" 1074 "}", 1075 AllowSimpleBracedStatements); 1076 verifyFormat("if (true)\n" 1077 "{\n" 1078 " f();\n" 1079 "} else\n" 1080 "{\n" 1081 " f();\n" 1082 "}", 1083 AllowSimpleBracedStatements); 1084 1085 AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false; 1086 verifyFormat("while (true) {}", AllowSimpleBracedStatements); 1087 verifyFormat("while (true)\n" 1088 "{\n" 1089 " f();\n" 1090 "}", 1091 AllowSimpleBracedStatements); 1092 verifyFormat("for (;;) {}", AllowSimpleBracedStatements); 1093 verifyFormat("for (;;)\n" 1094 "{\n" 1095 " f();\n" 1096 "}", 1097 AllowSimpleBracedStatements); 1098 } 1099 1100 TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) { 1101 FormatStyle Style = getLLVMStyleWithColumns(60); 1102 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 1103 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; 1104 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 1105 EXPECT_EQ("#define A \\\n" 1106 " if (HANDLEwernufrnuLwrmviferuvnierv) \\\n" 1107 " { \\\n" 1108 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" 1109 " }\n" 1110 "X;", 1111 format("#define A \\\n" 1112 " if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n" 1113 " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" 1114 " }\n" 1115 "X;", 1116 Style)); 1117 } 1118 1119 TEST_F(FormatTest, ParseIfElse) { 1120 verifyFormat("if (true)\n" 1121 " if (true)\n" 1122 " if (true)\n" 1123 " f();\n" 1124 " else\n" 1125 " g();\n" 1126 " else\n" 1127 " h();\n" 1128 "else\n" 1129 " i();"); 1130 verifyFormat("if (true)\n" 1131 " if (true)\n" 1132 " if (true) {\n" 1133 " if (true)\n" 1134 " f();\n" 1135 " } else {\n" 1136 " g();\n" 1137 " }\n" 1138 " else\n" 1139 " h();\n" 1140 "else {\n" 1141 " i();\n" 1142 "}"); 1143 verifyFormat("if (true)\n" 1144 " if constexpr (true)\n" 1145 " if (true) {\n" 1146 " if constexpr (true)\n" 1147 " f();\n" 1148 " } else {\n" 1149 " g();\n" 1150 " }\n" 1151 " else\n" 1152 " h();\n" 1153 "else {\n" 1154 " i();\n" 1155 "}"); 1156 verifyFormat("if (true)\n" 1157 " if CONSTEXPR (true)\n" 1158 " if (true) {\n" 1159 " if CONSTEXPR (true)\n" 1160 " f();\n" 1161 " } else {\n" 1162 " g();\n" 1163 " }\n" 1164 " else\n" 1165 " h();\n" 1166 "else {\n" 1167 " i();\n" 1168 "}"); 1169 verifyFormat("void f() {\n" 1170 " if (a) {\n" 1171 " } else {\n" 1172 " }\n" 1173 "}"); 1174 } 1175 1176 TEST_F(FormatTest, ElseIf) { 1177 verifyFormat("if (a) {\n} else if (b) {\n}"); 1178 verifyFormat("if (a)\n" 1179 " f();\n" 1180 "else if (b)\n" 1181 " g();\n" 1182 "else\n" 1183 " h();"); 1184 verifyFormat("if (a)\n" 1185 " f();\n" 1186 "else // comment\n" 1187 " if (b) {\n" 1188 " g();\n" 1189 " h();\n" 1190 " }"); 1191 verifyFormat("if constexpr (a)\n" 1192 " f();\n" 1193 "else if constexpr (b)\n" 1194 " g();\n" 1195 "else\n" 1196 " h();"); 1197 verifyFormat("if CONSTEXPR (a)\n" 1198 " f();\n" 1199 "else if CONSTEXPR (b)\n" 1200 " g();\n" 1201 "else\n" 1202 " h();"); 1203 verifyFormat("if (a) {\n" 1204 " f();\n" 1205 "}\n" 1206 "// or else ..\n" 1207 "else {\n" 1208 " g()\n" 1209 "}"); 1210 1211 verifyFormat("if (a) {\n" 1212 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1213 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 1214 "}"); 1215 verifyFormat("if (a) {\n" 1216 "} else if constexpr (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1217 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 1218 "}"); 1219 verifyFormat("if (a) {\n" 1220 "} else if CONSTEXPR (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1221 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 1222 "}"); 1223 verifyFormat("if (a) {\n" 1224 "} else if (\n" 1225 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 1226 "}", 1227 getLLVMStyleWithColumns(62)); 1228 verifyFormat("if (a) {\n" 1229 "} else if constexpr (\n" 1230 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 1231 "}", 1232 getLLVMStyleWithColumns(62)); 1233 verifyFormat("if (a) {\n" 1234 "} else if CONSTEXPR (\n" 1235 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 1236 "}", 1237 getLLVMStyleWithColumns(62)); 1238 } 1239 1240 TEST_F(FormatTest, FormatsForLoop) { 1241 verifyFormat( 1242 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 1243 " ++VeryVeryLongLoopVariable)\n" 1244 " ;"); 1245 verifyFormat("for (;;)\n" 1246 " f();"); 1247 verifyFormat("for (;;) {\n}"); 1248 verifyFormat("for (;;) {\n" 1249 " f();\n" 1250 "}"); 1251 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}"); 1252 1253 verifyFormat( 1254 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 1255 " E = UnwrappedLines.end();\n" 1256 " I != E; ++I) {\n}"); 1257 1258 verifyFormat( 1259 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" 1260 " ++IIIII) {\n}"); 1261 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" 1262 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" 1263 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); 1264 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n" 1265 " I = FD->getDeclsInPrototypeScope().begin(),\n" 1266 " E = FD->getDeclsInPrototypeScope().end();\n" 1267 " I != E; ++I) {\n}"); 1268 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n" 1269 " I = Container.begin(),\n" 1270 " E = Container.end();\n" 1271 " I != E; ++I) {\n}", 1272 getLLVMStyleWithColumns(76)); 1273 1274 verifyFormat( 1275 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 1276 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" 1277 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1278 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 1279 " ++aaaaaaaaaaa) {\n}"); 1280 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 1281 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n" 1282 " ++i) {\n}"); 1283 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" 1284 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 1285 "}"); 1286 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n" 1287 " aaaaaaaaaa);\n" 1288 " iter; ++iter) {\n" 1289 "}"); 1290 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1291 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 1292 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n" 1293 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {"); 1294 1295 // These should not be formatted as Objective-C for-in loops. 1296 verifyFormat("for (Foo *x = 0; x != in; x++) {\n}"); 1297 verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}"); 1298 verifyFormat("Foo *x;\nfor (x in y) {\n}"); 1299 verifyFormat( 1300 "for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}"); 1301 1302 FormatStyle NoBinPacking = getLLVMStyle(); 1303 NoBinPacking.BinPackParameters = false; 1304 verifyFormat("for (int aaaaaaaaaaa = 1;\n" 1305 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" 1306 " aaaaaaaaaaaaaaaa,\n" 1307 " aaaaaaaaaaaaaaaa,\n" 1308 " aaaaaaaaaaaaaaaa);\n" 1309 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 1310 "}", 1311 NoBinPacking); 1312 verifyFormat( 1313 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 1314 " E = UnwrappedLines.end();\n" 1315 " I != E;\n" 1316 " ++I) {\n}", 1317 NoBinPacking); 1318 1319 FormatStyle AlignLeft = getLLVMStyle(); 1320 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 1321 verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft); 1322 } 1323 1324 TEST_F(FormatTest, RangeBasedForLoops) { 1325 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 1326 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 1327 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" 1328 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); 1329 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" 1330 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 1331 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n" 1332 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}"); 1333 } 1334 1335 TEST_F(FormatTest, ForEachLoops) { 1336 verifyFormat("void f() {\n" 1337 " foreach (Item *item, itemlist) {}\n" 1338 " Q_FOREACH (Item *item, itemlist) {}\n" 1339 " BOOST_FOREACH (Item *item, itemlist) {}\n" 1340 " UNKNOWN_FORACH(Item * item, itemlist) {}\n" 1341 "}"); 1342 1343 FormatStyle Style = getLLVMStyle(); 1344 Style.SpaceBeforeParens = 1345 FormatStyle::SBPO_ControlStatementsExceptForEachMacros; 1346 verifyFormat("void f() {\n" 1347 " foreach(Item *item, itemlist) {}\n" 1348 " Q_FOREACH(Item *item, itemlist) {}\n" 1349 " BOOST_FOREACH(Item *item, itemlist) {}\n" 1350 " UNKNOWN_FORACH(Item * item, itemlist) {}\n" 1351 "}", 1352 Style); 1353 1354 // As function-like macros. 1355 verifyFormat("#define foreach(x, y)\n" 1356 "#define Q_FOREACH(x, y)\n" 1357 "#define BOOST_FOREACH(x, y)\n" 1358 "#define UNKNOWN_FOREACH(x, y)\n"); 1359 1360 // Not as function-like macros. 1361 verifyFormat("#define foreach (x, y)\n" 1362 "#define Q_FOREACH (x, y)\n" 1363 "#define BOOST_FOREACH (x, y)\n" 1364 "#define UNKNOWN_FOREACH (x, y)\n"); 1365 1366 // handle microsoft non standard extension 1367 verifyFormat("for each (char c in x->MyStringProperty)"); 1368 } 1369 1370 TEST_F(FormatTest, FormatsWhileLoop) { 1371 verifyFormat("while (true) {\n}"); 1372 verifyFormat("while (true)\n" 1373 " f();"); 1374 verifyFormat("while () {\n}"); 1375 verifyFormat("while () {\n" 1376 " f();\n" 1377 "}"); 1378 } 1379 1380 TEST_F(FormatTest, FormatsDoWhile) { 1381 verifyFormat("do {\n" 1382 " do_something();\n" 1383 "} while (something());"); 1384 verifyFormat("do\n" 1385 " do_something();\n" 1386 "while (something());"); 1387 } 1388 1389 TEST_F(FormatTest, FormatsSwitchStatement) { 1390 verifyFormat("switch (x) {\n" 1391 "case 1:\n" 1392 " f();\n" 1393 " break;\n" 1394 "case kFoo:\n" 1395 "case ns::kBar:\n" 1396 "case kBaz:\n" 1397 " break;\n" 1398 "default:\n" 1399 " g();\n" 1400 " break;\n" 1401 "}"); 1402 verifyFormat("switch (x) {\n" 1403 "case 1: {\n" 1404 " f();\n" 1405 " break;\n" 1406 "}\n" 1407 "case 2: {\n" 1408 " break;\n" 1409 "}\n" 1410 "}"); 1411 verifyFormat("switch (x) {\n" 1412 "case 1: {\n" 1413 " f();\n" 1414 " {\n" 1415 " g();\n" 1416 " h();\n" 1417 " }\n" 1418 " break;\n" 1419 "}\n" 1420 "}"); 1421 verifyFormat("switch (x) {\n" 1422 "case 1: {\n" 1423 " f();\n" 1424 " if (foo) {\n" 1425 " g();\n" 1426 " h();\n" 1427 " }\n" 1428 " break;\n" 1429 "}\n" 1430 "}"); 1431 verifyFormat("switch (x) {\n" 1432 "case 1: {\n" 1433 " f();\n" 1434 " g();\n" 1435 "} break;\n" 1436 "}"); 1437 verifyFormat("switch (test)\n" 1438 " ;"); 1439 verifyFormat("switch (x) {\n" 1440 "default: {\n" 1441 " // Do nothing.\n" 1442 "}\n" 1443 "}"); 1444 verifyFormat("switch (x) {\n" 1445 "// comment\n" 1446 "// if 1, do f()\n" 1447 "case 1:\n" 1448 " f();\n" 1449 "}"); 1450 verifyFormat("switch (x) {\n" 1451 "case 1:\n" 1452 " // Do amazing stuff\n" 1453 " {\n" 1454 " f();\n" 1455 " g();\n" 1456 " }\n" 1457 " break;\n" 1458 "}"); 1459 verifyFormat("#define A \\\n" 1460 " switch (x) { \\\n" 1461 " case a: \\\n" 1462 " foo = b; \\\n" 1463 " }", 1464 getLLVMStyleWithColumns(20)); 1465 verifyFormat("#define OPERATION_CASE(name) \\\n" 1466 " case OP_name: \\\n" 1467 " return operations::Operation##name\n", 1468 getLLVMStyleWithColumns(40)); 1469 verifyFormat("switch (x) {\n" 1470 "case 1:;\n" 1471 "default:;\n" 1472 " int i;\n" 1473 "}"); 1474 1475 verifyGoogleFormat("switch (x) {\n" 1476 " case 1:\n" 1477 " f();\n" 1478 " break;\n" 1479 " case kFoo:\n" 1480 " case ns::kBar:\n" 1481 " case kBaz:\n" 1482 " break;\n" 1483 " default:\n" 1484 " g();\n" 1485 " break;\n" 1486 "}"); 1487 verifyGoogleFormat("switch (x) {\n" 1488 " case 1: {\n" 1489 " f();\n" 1490 " break;\n" 1491 " }\n" 1492 "}"); 1493 verifyGoogleFormat("switch (test)\n" 1494 " ;"); 1495 1496 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n" 1497 " case OP_name: \\\n" 1498 " return operations::Operation##name\n"); 1499 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n" 1500 " // Get the correction operation class.\n" 1501 " switch (OpCode) {\n" 1502 " CASE(Add);\n" 1503 " CASE(Subtract);\n" 1504 " default:\n" 1505 " return operations::Unknown;\n" 1506 " }\n" 1507 "#undef OPERATION_CASE\n" 1508 "}"); 1509 verifyFormat("DEBUG({\n" 1510 " switch (x) {\n" 1511 " case A:\n" 1512 " f();\n" 1513 " break;\n" 1514 " // fallthrough\n" 1515 " case B:\n" 1516 " g();\n" 1517 " break;\n" 1518 " }\n" 1519 "});"); 1520 EXPECT_EQ("DEBUG({\n" 1521 " switch (x) {\n" 1522 " case A:\n" 1523 " f();\n" 1524 " break;\n" 1525 " // On B:\n" 1526 " case B:\n" 1527 " g();\n" 1528 " break;\n" 1529 " }\n" 1530 "});", 1531 format("DEBUG({\n" 1532 " switch (x) {\n" 1533 " case A:\n" 1534 " f();\n" 1535 " break;\n" 1536 " // On B:\n" 1537 " case B:\n" 1538 " g();\n" 1539 " break;\n" 1540 " }\n" 1541 "});", 1542 getLLVMStyle())); 1543 EXPECT_EQ("switch (n) {\n" 1544 "case 0: {\n" 1545 " return false;\n" 1546 "}\n" 1547 "default: {\n" 1548 " return true;\n" 1549 "}\n" 1550 "}", 1551 format("switch (n)\n" 1552 "{\n" 1553 "case 0: {\n" 1554 " return false;\n" 1555 "}\n" 1556 "default: {\n" 1557 " return true;\n" 1558 "}\n" 1559 "}", 1560 getLLVMStyle())); 1561 verifyFormat("switch (a) {\n" 1562 "case (b):\n" 1563 " return;\n" 1564 "}"); 1565 1566 verifyFormat("switch (a) {\n" 1567 "case some_namespace::\n" 1568 " some_constant:\n" 1569 " return;\n" 1570 "}", 1571 getLLVMStyleWithColumns(34)); 1572 1573 FormatStyle Style = getLLVMStyle(); 1574 Style.IndentCaseLabels = true; 1575 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 1576 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1577 Style.BraceWrapping.AfterCaseLabel = true; 1578 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 1579 EXPECT_EQ("switch (n)\n" 1580 "{\n" 1581 " case 0:\n" 1582 " {\n" 1583 " return false;\n" 1584 " }\n" 1585 " default:\n" 1586 " {\n" 1587 " return true;\n" 1588 " }\n" 1589 "}", 1590 format("switch (n) {\n" 1591 " case 0: {\n" 1592 " return false;\n" 1593 " }\n" 1594 " default: {\n" 1595 " return true;\n" 1596 " }\n" 1597 "}", 1598 Style)); 1599 Style.BraceWrapping.AfterCaseLabel = false; 1600 EXPECT_EQ("switch (n)\n" 1601 "{\n" 1602 " case 0: {\n" 1603 " return false;\n" 1604 " }\n" 1605 " default: {\n" 1606 " return true;\n" 1607 " }\n" 1608 "}", 1609 format("switch (n) {\n" 1610 " case 0:\n" 1611 " {\n" 1612 " return false;\n" 1613 " }\n" 1614 " default:\n" 1615 " {\n" 1616 " return true;\n" 1617 " }\n" 1618 "}", 1619 Style)); 1620 Style.IndentCaseLabels = false; 1621 Style.IndentCaseBlocks = true; 1622 EXPECT_EQ("switch (n)\n" 1623 "{\n" 1624 "case 0:\n" 1625 " {\n" 1626 " return false;\n" 1627 " }\n" 1628 "case 1:\n" 1629 " break;\n" 1630 "default:\n" 1631 " {\n" 1632 " return true;\n" 1633 " }\n" 1634 "}", 1635 format("switch (n) {\n" 1636 "case 0: {\n" 1637 " return false;\n" 1638 "}\n" 1639 "case 1:\n" 1640 " break;\n" 1641 "default: {\n" 1642 " return true;\n" 1643 "}\n" 1644 "}", 1645 Style)); 1646 Style.IndentCaseLabels = true; 1647 Style.IndentCaseBlocks = true; 1648 EXPECT_EQ("switch (n)\n" 1649 "{\n" 1650 " case 0:\n" 1651 " {\n" 1652 " return false;\n" 1653 " }\n" 1654 " case 1:\n" 1655 " break;\n" 1656 " default:\n" 1657 " {\n" 1658 " return true;\n" 1659 " }\n" 1660 "}", 1661 format("switch (n) {\n" 1662 "case 0: {\n" 1663 " return false;\n" 1664 "}\n" 1665 "case 1:\n" 1666 " break;\n" 1667 "default: {\n" 1668 " return true;\n" 1669 "}\n" 1670 "}", 1671 Style)); 1672 } 1673 1674 TEST_F(FormatTest, CaseRanges) { 1675 verifyFormat("switch (x) {\n" 1676 "case 'A' ... 'Z':\n" 1677 "case 1 ... 5:\n" 1678 "case a ... b:\n" 1679 " break;\n" 1680 "}"); 1681 } 1682 1683 TEST_F(FormatTest, ShortEnums) { 1684 FormatStyle Style = getLLVMStyle(); 1685 Style.AllowShortEnumsOnASingleLine = true; 1686 verifyFormat("enum { A, B, C } ShortEnum1, ShortEnum2;", Style); 1687 Style.AllowShortEnumsOnASingleLine = false; 1688 verifyFormat("enum\n" 1689 "{\n" 1690 " A,\n" 1691 " B,\n" 1692 " C\n" 1693 "} ShortEnum1, ShortEnum2;", 1694 Style); 1695 } 1696 1697 TEST_F(FormatTest, ShortCaseLabels) { 1698 FormatStyle Style = getLLVMStyle(); 1699 Style.AllowShortCaseLabelsOnASingleLine = true; 1700 verifyFormat("switch (a) {\n" 1701 "case 1: x = 1; break;\n" 1702 "case 2: return;\n" 1703 "case 3:\n" 1704 "case 4:\n" 1705 "case 5: return;\n" 1706 "case 6: // comment\n" 1707 " return;\n" 1708 "case 7:\n" 1709 " // comment\n" 1710 " return;\n" 1711 "case 8:\n" 1712 " x = 8; // comment\n" 1713 " break;\n" 1714 "default: y = 1; break;\n" 1715 "}", 1716 Style); 1717 verifyFormat("switch (a) {\n" 1718 "case 0: return; // comment\n" 1719 "case 1: break; // comment\n" 1720 "case 2: return;\n" 1721 "// comment\n" 1722 "case 3: return;\n" 1723 "// comment 1\n" 1724 "// comment 2\n" 1725 "// comment 3\n" 1726 "case 4: break; /* comment */\n" 1727 "case 5:\n" 1728 " // comment\n" 1729 " break;\n" 1730 "case 6: /* comment */ x = 1; break;\n" 1731 "case 7: x = /* comment */ 1; break;\n" 1732 "case 8:\n" 1733 " x = 1; /* comment */\n" 1734 " break;\n" 1735 "case 9:\n" 1736 " break; // comment line 1\n" 1737 " // comment line 2\n" 1738 "}", 1739 Style); 1740 EXPECT_EQ("switch (a) {\n" 1741 "case 1:\n" 1742 " x = 8;\n" 1743 " // fall through\n" 1744 "case 2: x = 8;\n" 1745 "// comment\n" 1746 "case 3:\n" 1747 " return; /* comment line 1\n" 1748 " * comment line 2 */\n" 1749 "case 4: i = 8;\n" 1750 "// something else\n" 1751 "#if FOO\n" 1752 "case 5: break;\n" 1753 "#endif\n" 1754 "}", 1755 format("switch (a) {\n" 1756 "case 1: x = 8;\n" 1757 " // fall through\n" 1758 "case 2:\n" 1759 " x = 8;\n" 1760 "// comment\n" 1761 "case 3:\n" 1762 " return; /* comment line 1\n" 1763 " * comment line 2 */\n" 1764 "case 4:\n" 1765 " i = 8;\n" 1766 "// something else\n" 1767 "#if FOO\n" 1768 "case 5: break;\n" 1769 "#endif\n" 1770 "}", 1771 Style)); 1772 EXPECT_EQ("switch (a) {\n" 1773 "case 0:\n" 1774 " return; // long long long long long long long long long long " 1775 "long long comment\n" 1776 " // line\n" 1777 "}", 1778 format("switch (a) {\n" 1779 "case 0: return; // long long long long long long long long " 1780 "long long long long comment line\n" 1781 "}", 1782 Style)); 1783 EXPECT_EQ("switch (a) {\n" 1784 "case 0:\n" 1785 " return; /* long long long long long long long long long long " 1786 "long long comment\n" 1787 " line */\n" 1788 "}", 1789 format("switch (a) {\n" 1790 "case 0: return; /* long long long long long long long long " 1791 "long long long long comment line */\n" 1792 "}", 1793 Style)); 1794 verifyFormat("switch (a) {\n" 1795 "#if FOO\n" 1796 "case 0: return 0;\n" 1797 "#endif\n" 1798 "}", 1799 Style); 1800 verifyFormat("switch (a) {\n" 1801 "case 1: {\n" 1802 "}\n" 1803 "case 2: {\n" 1804 " return;\n" 1805 "}\n" 1806 "case 3: {\n" 1807 " x = 1;\n" 1808 " return;\n" 1809 "}\n" 1810 "case 4:\n" 1811 " if (x)\n" 1812 " return;\n" 1813 "}", 1814 Style); 1815 Style.ColumnLimit = 21; 1816 verifyFormat("switch (a) {\n" 1817 "case 1: x = 1; break;\n" 1818 "case 2: return;\n" 1819 "case 3:\n" 1820 "case 4:\n" 1821 "case 5: return;\n" 1822 "default:\n" 1823 " y = 1;\n" 1824 " break;\n" 1825 "}", 1826 Style); 1827 Style.ColumnLimit = 80; 1828 Style.AllowShortCaseLabelsOnASingleLine = false; 1829 Style.IndentCaseLabels = true; 1830 EXPECT_EQ("switch (n) {\n" 1831 " default /*comments*/:\n" 1832 " return true;\n" 1833 " case 0:\n" 1834 " return false;\n" 1835 "}", 1836 format("switch (n) {\n" 1837 "default/*comments*/:\n" 1838 " return true;\n" 1839 "case 0:\n" 1840 " return false;\n" 1841 "}", 1842 Style)); 1843 Style.AllowShortCaseLabelsOnASingleLine = true; 1844 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1845 Style.BraceWrapping.AfterCaseLabel = true; 1846 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 1847 EXPECT_EQ("switch (n)\n" 1848 "{\n" 1849 " case 0:\n" 1850 " {\n" 1851 " return false;\n" 1852 " }\n" 1853 " default:\n" 1854 " {\n" 1855 " return true;\n" 1856 " }\n" 1857 "}", 1858 format("switch (n) {\n" 1859 " case 0: {\n" 1860 " return false;\n" 1861 " }\n" 1862 " default:\n" 1863 " {\n" 1864 " return true;\n" 1865 " }\n" 1866 "}", 1867 Style)); 1868 } 1869 1870 TEST_F(FormatTest, FormatsLabels) { 1871 verifyFormat("void f() {\n" 1872 " some_code();\n" 1873 "test_label:\n" 1874 " some_other_code();\n" 1875 " {\n" 1876 " some_more_code();\n" 1877 " another_label:\n" 1878 " some_more_code();\n" 1879 " }\n" 1880 "}"); 1881 verifyFormat("{\n" 1882 " some_code();\n" 1883 "test_label:\n" 1884 " some_other_code();\n" 1885 "}"); 1886 verifyFormat("{\n" 1887 " some_code();\n" 1888 "test_label:;\n" 1889 " int i = 0;\n" 1890 "}"); 1891 FormatStyle Style = getLLVMStyle(); 1892 Style.IndentGotoLabels = false; 1893 verifyFormat("void f() {\n" 1894 " some_code();\n" 1895 "test_label:\n" 1896 " some_other_code();\n" 1897 " {\n" 1898 " some_more_code();\n" 1899 "another_label:\n" 1900 " some_more_code();\n" 1901 " }\n" 1902 "}", 1903 Style); 1904 verifyFormat("{\n" 1905 " some_code();\n" 1906 "test_label:\n" 1907 " some_other_code();\n" 1908 "}", 1909 Style); 1910 verifyFormat("{\n" 1911 " some_code();\n" 1912 "test_label:;\n" 1913 " int i = 0;\n" 1914 "}"); 1915 } 1916 1917 TEST_F(FormatTest, MultiLineControlStatements) { 1918 FormatStyle Style = getLLVMStyle(); 1919 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom; 1920 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine; 1921 Style.ColumnLimit = 20; 1922 // Short lines should keep opening brace on same line. 1923 EXPECT_EQ("if (foo) {\n" 1924 " bar();\n" 1925 "}", 1926 format("if(foo){bar();}", Style)); 1927 EXPECT_EQ("if (foo) {\n" 1928 " bar();\n" 1929 "} else {\n" 1930 " baz();\n" 1931 "}", 1932 format("if(foo){bar();}else{baz();}", Style)); 1933 EXPECT_EQ("if (foo && bar) {\n" 1934 " baz();\n" 1935 "}", 1936 format("if(foo&&bar){baz();}", Style)); 1937 EXPECT_EQ("if (foo) {\n" 1938 " bar();\n" 1939 "} else if (baz) {\n" 1940 " quux();\n" 1941 "}", 1942 format("if(foo){bar();}else if(baz){quux();}", Style)); 1943 EXPECT_EQ( 1944 "if (foo) {\n" 1945 " bar();\n" 1946 "} else if (baz) {\n" 1947 " quux();\n" 1948 "} else {\n" 1949 " foobar();\n" 1950 "}", 1951 format("if(foo){bar();}else if(baz){quux();}else{foobar();}", Style)); 1952 EXPECT_EQ("for (;;) {\n" 1953 " foo();\n" 1954 "}", 1955 format("for(;;){foo();}")); 1956 EXPECT_EQ("while (1) {\n" 1957 " foo();\n" 1958 "}", 1959 format("while(1){foo();}", Style)); 1960 EXPECT_EQ("switch (foo) {\n" 1961 "case bar:\n" 1962 " return;\n" 1963 "}", 1964 format("switch(foo){case bar:return;}", Style)); 1965 EXPECT_EQ("try {\n" 1966 " foo();\n" 1967 "} catch (...) {\n" 1968 " bar();\n" 1969 "}", 1970 format("try{foo();}catch(...){bar();}", Style)); 1971 EXPECT_EQ("do {\n" 1972 " foo();\n" 1973 "} while (bar &&\n" 1974 " baz);", 1975 format("do{foo();}while(bar&&baz);", Style)); 1976 // Long lines should put opening brace on new line. 1977 EXPECT_EQ("if (foo && bar &&\n" 1978 " baz)\n" 1979 "{\n" 1980 " quux();\n" 1981 "}", 1982 format("if(foo&&bar&&baz){quux();}", Style)); 1983 EXPECT_EQ("if (foo && bar &&\n" 1984 " baz)\n" 1985 "{\n" 1986 " quux();\n" 1987 "}", 1988 format("if (foo && bar &&\n" 1989 " baz) {\n" 1990 " quux();\n" 1991 "}", 1992 Style)); 1993 EXPECT_EQ("if (foo) {\n" 1994 " bar();\n" 1995 "} else if (baz ||\n" 1996 " quux)\n" 1997 "{\n" 1998 " foobar();\n" 1999 "}", 2000 format("if(foo){bar();}else if(baz||quux){foobar();}", Style)); 2001 EXPECT_EQ( 2002 "if (foo) {\n" 2003 " bar();\n" 2004 "} else if (baz ||\n" 2005 " quux)\n" 2006 "{\n" 2007 " foobar();\n" 2008 "} else {\n" 2009 " barbaz();\n" 2010 "}", 2011 format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}", 2012 Style)); 2013 EXPECT_EQ("for (int i = 0;\n" 2014 " i < 10; ++i)\n" 2015 "{\n" 2016 " foo();\n" 2017 "}", 2018 format("for(int i=0;i<10;++i){foo();}", Style)); 2019 EXPECT_EQ("foreach (int i,\n" 2020 " list)\n" 2021 "{\n" 2022 " foo();\n" 2023 "}", 2024 format("foreach(int i, list){foo();}", Style)); 2025 Style.ColumnLimit = 2026 40; // to concentrate at brace wrapping, not line wrap due to column limit 2027 EXPECT_EQ("foreach (int i, list) {\n" 2028 " foo();\n" 2029 "}", 2030 format("foreach(int i, list){foo();}", Style)); 2031 Style.ColumnLimit = 2032 20; // to concentrate at brace wrapping, not line wrap due to column limit 2033 EXPECT_EQ("while (foo || bar ||\n" 2034 " baz)\n" 2035 "{\n" 2036 " quux();\n" 2037 "}", 2038 format("while(foo||bar||baz){quux();}", Style)); 2039 EXPECT_EQ("switch (\n" 2040 " foo = barbaz)\n" 2041 "{\n" 2042 "case quux:\n" 2043 " return;\n" 2044 "}", 2045 format("switch(foo=barbaz){case quux:return;}", Style)); 2046 EXPECT_EQ("try {\n" 2047 " foo();\n" 2048 "} catch (\n" 2049 " Exception &bar)\n" 2050 "{\n" 2051 " baz();\n" 2052 "}", 2053 format("try{foo();}catch(Exception&bar){baz();}", Style)); 2054 Style.ColumnLimit = 2055 40; // to concentrate at brace wrapping, not line wrap due to column limit 2056 EXPECT_EQ("try {\n" 2057 " foo();\n" 2058 "} catch (Exception &bar) {\n" 2059 " baz();\n" 2060 "}", 2061 format("try{foo();}catch(Exception&bar){baz();}", Style)); 2062 Style.ColumnLimit = 2063 20; // to concentrate at brace wrapping, not line wrap due to column limit 2064 2065 Style.BraceWrapping.BeforeElse = true; 2066 EXPECT_EQ( 2067 "if (foo) {\n" 2068 " bar();\n" 2069 "}\n" 2070 "else if (baz ||\n" 2071 " quux)\n" 2072 "{\n" 2073 " foobar();\n" 2074 "}\n" 2075 "else {\n" 2076 " barbaz();\n" 2077 "}", 2078 format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}", 2079 Style)); 2080 2081 Style.BraceWrapping.BeforeCatch = true; 2082 EXPECT_EQ("try {\n" 2083 " foo();\n" 2084 "}\n" 2085 "catch (...) {\n" 2086 " baz();\n" 2087 "}", 2088 format("try{foo();}catch(...){baz();}", Style)); 2089 } 2090 2091 TEST_F(FormatTest, BeforeWhile) { 2092 FormatStyle Style = getLLVMStyle(); 2093 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom; 2094 2095 verifyFormat("do {\n" 2096 " foo();\n" 2097 "} while (1);", 2098 Style); 2099 Style.BraceWrapping.BeforeWhile = true; 2100 verifyFormat("do {\n" 2101 " foo();\n" 2102 "}\n" 2103 "while (1);", 2104 Style); 2105 } 2106 2107 //===----------------------------------------------------------------------===// 2108 // Tests for classes, namespaces, etc. 2109 //===----------------------------------------------------------------------===// 2110 2111 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 2112 verifyFormat("class A {};"); 2113 } 2114 2115 TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 2116 verifyFormat("class A {\n" 2117 "public:\n" 2118 "public: // comment\n" 2119 "protected:\n" 2120 "private:\n" 2121 " void f() {}\n" 2122 "};"); 2123 verifyFormat("export class A {\n" 2124 "public:\n" 2125 "public: // comment\n" 2126 "protected:\n" 2127 "private:\n" 2128 " void f() {}\n" 2129 "};"); 2130 verifyGoogleFormat("class A {\n" 2131 " public:\n" 2132 " protected:\n" 2133 " private:\n" 2134 " void f() {}\n" 2135 "};"); 2136 verifyGoogleFormat("export class A {\n" 2137 " public:\n" 2138 " protected:\n" 2139 " private:\n" 2140 " void f() {}\n" 2141 "};"); 2142 verifyFormat("class A {\n" 2143 "public slots:\n" 2144 " void f1() {}\n" 2145 "public Q_SLOTS:\n" 2146 " void f2() {}\n" 2147 "protected slots:\n" 2148 " void f3() {}\n" 2149 "protected Q_SLOTS:\n" 2150 " void f4() {}\n" 2151 "private slots:\n" 2152 " void f5() {}\n" 2153 "private Q_SLOTS:\n" 2154 " void f6() {}\n" 2155 "signals:\n" 2156 " void g1();\n" 2157 "Q_SIGNALS:\n" 2158 " void g2();\n" 2159 "};"); 2160 2161 // Don't interpret 'signals' the wrong way. 2162 verifyFormat("signals.set();"); 2163 verifyFormat("for (Signals signals : f()) {\n}"); 2164 verifyFormat("{\n" 2165 " signals.set(); // This needs indentation.\n" 2166 "}"); 2167 verifyFormat("void f() {\n" 2168 "label:\n" 2169 " signals.baz();\n" 2170 "}"); 2171 } 2172 2173 TEST_F(FormatTest, SeparatesLogicalBlocks) { 2174 EXPECT_EQ("class A {\n" 2175 "public:\n" 2176 " void f();\n" 2177 "\n" 2178 "private:\n" 2179 " void g() {}\n" 2180 " // test\n" 2181 "protected:\n" 2182 " int h;\n" 2183 "};", 2184 format("class A {\n" 2185 "public:\n" 2186 "void f();\n" 2187 "private:\n" 2188 "void g() {}\n" 2189 "// test\n" 2190 "protected:\n" 2191 "int h;\n" 2192 "};")); 2193 EXPECT_EQ("class A {\n" 2194 "protected:\n" 2195 "public:\n" 2196 " void f();\n" 2197 "};", 2198 format("class A {\n" 2199 "protected:\n" 2200 "\n" 2201 "public:\n" 2202 "\n" 2203 " void f();\n" 2204 "};")); 2205 2206 // Even ensure proper spacing inside macros. 2207 EXPECT_EQ("#define B \\\n" 2208 " class A { \\\n" 2209 " protected: \\\n" 2210 " public: \\\n" 2211 " void f(); \\\n" 2212 " };", 2213 format("#define B \\\n" 2214 " class A { \\\n" 2215 " protected: \\\n" 2216 " \\\n" 2217 " public: \\\n" 2218 " \\\n" 2219 " void f(); \\\n" 2220 " };", 2221 getGoogleStyle())); 2222 // But don't remove empty lines after macros ending in access specifiers. 2223 EXPECT_EQ("#define A private:\n" 2224 "\n" 2225 "int i;", 2226 format("#define A private:\n" 2227 "\n" 2228 "int i;")); 2229 } 2230 2231 TEST_F(FormatTest, FormatsClasses) { 2232 verifyFormat("class A : public B {};"); 2233 verifyFormat("class A : public ::B {};"); 2234 2235 verifyFormat( 2236 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 2237 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 2238 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 2239 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 2240 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 2241 verifyFormat( 2242 "class A : public B, public C, public D, public E, public F {};"); 2243 verifyFormat("class AAAAAAAAAAAA : public B,\n" 2244 " public C,\n" 2245 " public D,\n" 2246 " public E,\n" 2247 " public F,\n" 2248 " public G {};"); 2249 2250 verifyFormat("class\n" 2251 " ReallyReallyLongClassName {\n" 2252 " int i;\n" 2253 "};", 2254 getLLVMStyleWithColumns(32)); 2255 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 2256 " aaaaaaaaaaaaaaaa> {};"); 2257 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" 2258 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n" 2259 " aaaaaaaaaaaaaaaaaaaaaa> {};"); 2260 verifyFormat("template <class R, class C>\n" 2261 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n" 2262 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};"); 2263 verifyFormat("class ::A::B {};"); 2264 } 2265 2266 TEST_F(FormatTest, BreakInheritanceStyle) { 2267 FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle(); 2268 StyleWithInheritanceBreakBeforeComma.BreakInheritanceList = 2269 FormatStyle::BILS_BeforeComma; 2270 verifyFormat("class MyClass : public X {};", 2271 StyleWithInheritanceBreakBeforeComma); 2272 verifyFormat("class MyClass\n" 2273 " : public X\n" 2274 " , public Y {};", 2275 StyleWithInheritanceBreakBeforeComma); 2276 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n" 2277 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n" 2278 " , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", 2279 StyleWithInheritanceBreakBeforeComma); 2280 verifyFormat("struct aaaaaaaaaaaaa\n" 2281 " : public aaaaaaaaaaaaaaaaaaa< // break\n" 2282 " aaaaaaaaaaaaaaaa> {};", 2283 StyleWithInheritanceBreakBeforeComma); 2284 2285 FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle(); 2286 StyleWithInheritanceBreakAfterColon.BreakInheritanceList = 2287 FormatStyle::BILS_AfterColon; 2288 verifyFormat("class MyClass : public X {};", 2289 StyleWithInheritanceBreakAfterColon); 2290 verifyFormat("class MyClass : public X, public Y {};", 2291 StyleWithInheritanceBreakAfterColon); 2292 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n" 2293 " public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 2294 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", 2295 StyleWithInheritanceBreakAfterColon); 2296 verifyFormat("struct aaaaaaaaaaaaa :\n" 2297 " public aaaaaaaaaaaaaaaaaaa< // break\n" 2298 " aaaaaaaaaaaaaaaa> {};", 2299 StyleWithInheritanceBreakAfterColon); 2300 2301 FormatStyle StyleWithInheritanceBreakAfterComma = getLLVMStyle(); 2302 StyleWithInheritanceBreakAfterComma.BreakInheritanceList = 2303 FormatStyle::BILS_AfterComma; 2304 verifyFormat("class MyClass : public X {};", 2305 StyleWithInheritanceBreakAfterComma); 2306 verifyFormat("class MyClass : public X,\n" 2307 " public Y {};", 2308 StyleWithInheritanceBreakAfterComma); 2309 verifyFormat( 2310 "class AAAAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 2311 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC " 2312 "{};", 2313 StyleWithInheritanceBreakAfterComma); 2314 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 2315 " aaaaaaaaaaaaaaaa> {};", 2316 StyleWithInheritanceBreakAfterComma); 2317 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 2318 " : public OnceBreak,\n" 2319 " public AlwaysBreak,\n" 2320 " EvenBasesFitInOneLine {};", 2321 StyleWithInheritanceBreakAfterComma); 2322 } 2323 2324 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { 2325 verifyFormat("class A {\n} a, b;"); 2326 verifyFormat("struct A {\n} a, b;"); 2327 verifyFormat("union A {\n} a;"); 2328 } 2329 2330 TEST_F(FormatTest, FormatsEnum) { 2331 verifyFormat("enum {\n" 2332 " Zero,\n" 2333 " One = 1,\n" 2334 " Two = One + 1,\n" 2335 " Three = (One + Two),\n" 2336 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2337 " Five = (One, Two, Three, Four, 5)\n" 2338 "};"); 2339 verifyGoogleFormat("enum {\n" 2340 " Zero,\n" 2341 " One = 1,\n" 2342 " Two = One + 1,\n" 2343 " Three = (One + Two),\n" 2344 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2345 " Five = (One, Two, Three, Four, 5)\n" 2346 "};"); 2347 verifyFormat("enum Enum {};"); 2348 verifyFormat("enum {};"); 2349 verifyFormat("enum X E {} d;"); 2350 verifyFormat("enum __attribute__((...)) E {} d;"); 2351 verifyFormat("enum __declspec__((...)) E {} d;"); 2352 verifyFormat("enum {\n" 2353 " Bar = Foo<int, int>::value\n" 2354 "};", 2355 getLLVMStyleWithColumns(30)); 2356 2357 verifyFormat("enum ShortEnum { A, B, C };"); 2358 verifyGoogleFormat("enum ShortEnum { A, B, C };"); 2359 2360 EXPECT_EQ("enum KeepEmptyLines {\n" 2361 " ONE,\n" 2362 "\n" 2363 " TWO,\n" 2364 "\n" 2365 " THREE\n" 2366 "}", 2367 format("enum KeepEmptyLines {\n" 2368 " ONE,\n" 2369 "\n" 2370 " TWO,\n" 2371 "\n" 2372 "\n" 2373 " THREE\n" 2374 "}")); 2375 verifyFormat("enum E { // comment\n" 2376 " ONE,\n" 2377 " TWO\n" 2378 "};\n" 2379 "int i;"); 2380 2381 FormatStyle EightIndent = getLLVMStyle(); 2382 EightIndent.IndentWidth = 8; 2383 verifyFormat("enum {\n" 2384 " VOID,\n" 2385 " CHAR,\n" 2386 " SHORT,\n" 2387 " INT,\n" 2388 " LONG,\n" 2389 " SIGNED,\n" 2390 " UNSIGNED,\n" 2391 " BOOL,\n" 2392 " FLOAT,\n" 2393 " DOUBLE,\n" 2394 " COMPLEX\n" 2395 "};", 2396 EightIndent); 2397 2398 // Not enums. 2399 verifyFormat("enum X f() {\n" 2400 " a();\n" 2401 " return 42;\n" 2402 "}"); 2403 verifyFormat("enum X Type::f() {\n" 2404 " a();\n" 2405 " return 42;\n" 2406 "}"); 2407 verifyFormat("enum ::X f() {\n" 2408 " a();\n" 2409 " return 42;\n" 2410 "}"); 2411 verifyFormat("enum ns::X f() {\n" 2412 " a();\n" 2413 " return 42;\n" 2414 "}"); 2415 } 2416 2417 TEST_F(FormatTest, FormatsEnumsWithErrors) { 2418 verifyFormat("enum Type {\n" 2419 " One = 0; // These semicolons should be commas.\n" 2420 " Two = 1;\n" 2421 "};"); 2422 verifyFormat("namespace n {\n" 2423 "enum Type {\n" 2424 " One,\n" 2425 " Two, // missing };\n" 2426 " int i;\n" 2427 "}\n" 2428 "void g() {}"); 2429 } 2430 2431 TEST_F(FormatTest, FormatsEnumStruct) { 2432 verifyFormat("enum struct {\n" 2433 " Zero,\n" 2434 " One = 1,\n" 2435 " Two = One + 1,\n" 2436 " Three = (One + Two),\n" 2437 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2438 " Five = (One, Two, Three, Four, 5)\n" 2439 "};"); 2440 verifyFormat("enum struct Enum {};"); 2441 verifyFormat("enum struct {};"); 2442 verifyFormat("enum struct X E {} d;"); 2443 verifyFormat("enum struct __attribute__((...)) E {} d;"); 2444 verifyFormat("enum struct __declspec__((...)) E {} d;"); 2445 verifyFormat("enum struct X f() {\n a();\n return 42;\n}"); 2446 } 2447 2448 TEST_F(FormatTest, FormatsEnumClass) { 2449 verifyFormat("enum class {\n" 2450 " Zero,\n" 2451 " One = 1,\n" 2452 " Two = One + 1,\n" 2453 " Three = (One + Two),\n" 2454 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2455 " Five = (One, Two, Three, Four, 5)\n" 2456 "};"); 2457 verifyFormat("enum class Enum {};"); 2458 verifyFormat("enum class {};"); 2459 verifyFormat("enum class X E {} d;"); 2460 verifyFormat("enum class __attribute__((...)) E {} d;"); 2461 verifyFormat("enum class __declspec__((...)) E {} d;"); 2462 verifyFormat("enum class X f() {\n a();\n return 42;\n}"); 2463 } 2464 2465 TEST_F(FormatTest, FormatsEnumTypes) { 2466 verifyFormat("enum X : int {\n" 2467 " A, // Force multiple lines.\n" 2468 " B\n" 2469 "};"); 2470 verifyFormat("enum X : int { A, B };"); 2471 verifyFormat("enum X : std::uint32_t { A, B };"); 2472 } 2473 2474 TEST_F(FormatTest, FormatsTypedefEnum) { 2475 FormatStyle Style = getLLVMStyle(); 2476 Style.ColumnLimit = 40; 2477 verifyFormat("typedef enum {} EmptyEnum;"); 2478 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 2479 verifyFormat("typedef enum {\n" 2480 " ZERO = 0,\n" 2481 " ONE = 1,\n" 2482 " TWO = 2,\n" 2483 " THREE = 3\n" 2484 "} LongEnum;", 2485 Style); 2486 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2487 Style.BraceWrapping.AfterEnum = true; 2488 verifyFormat("typedef enum {} EmptyEnum;"); 2489 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 2490 verifyFormat("typedef enum\n" 2491 "{\n" 2492 " ZERO = 0,\n" 2493 " ONE = 1,\n" 2494 " TWO = 2,\n" 2495 " THREE = 3\n" 2496 "} LongEnum;", 2497 Style); 2498 } 2499 2500 TEST_F(FormatTest, FormatsNSEnums) { 2501 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }"); 2502 verifyGoogleFormat( 2503 "typedef NS_CLOSED_ENUM(NSInteger, SomeName) { AAA, BBB }"); 2504 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n" 2505 " // Information about someDecentlyLongValue.\n" 2506 " someDecentlyLongValue,\n" 2507 " // Information about anotherDecentlyLongValue.\n" 2508 " anotherDecentlyLongValue,\n" 2509 " // Information about aThirdDecentlyLongValue.\n" 2510 " aThirdDecentlyLongValue\n" 2511 "};"); 2512 verifyGoogleFormat("typedef NS_CLOSED_ENUM(NSInteger, MyType) {\n" 2513 " // Information about someDecentlyLongValue.\n" 2514 " someDecentlyLongValue,\n" 2515 " // Information about anotherDecentlyLongValue.\n" 2516 " anotherDecentlyLongValue,\n" 2517 " // Information about aThirdDecentlyLongValue.\n" 2518 " aThirdDecentlyLongValue\n" 2519 "};"); 2520 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n" 2521 " a = 1,\n" 2522 " b = 2,\n" 2523 " c = 3,\n" 2524 "};"); 2525 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n" 2526 " a = 1,\n" 2527 " b = 2,\n" 2528 " c = 3,\n" 2529 "};"); 2530 verifyGoogleFormat("typedef CF_CLOSED_ENUM(NSInteger, MyType) {\n" 2531 " a = 1,\n" 2532 " b = 2,\n" 2533 " c = 3,\n" 2534 "};"); 2535 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n" 2536 " a = 1,\n" 2537 " b = 2,\n" 2538 " c = 3,\n" 2539 "};"); 2540 } 2541 2542 TEST_F(FormatTest, FormatsBitfields) { 2543 verifyFormat("struct Bitfields {\n" 2544 " unsigned sClass : 8;\n" 2545 " unsigned ValueKind : 2;\n" 2546 "};"); 2547 verifyFormat("struct A {\n" 2548 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" 2549 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" 2550 "};"); 2551 verifyFormat("struct MyStruct {\n" 2552 " uchar data;\n" 2553 " uchar : 8;\n" 2554 " uchar : 8;\n" 2555 " uchar other;\n" 2556 "};"); 2557 FormatStyle Style = getLLVMStyle(); 2558 Style.BitFieldColonSpacing = FormatStyle::BFCS_None; 2559 verifyFormat("struct Bitfields {\n" 2560 " unsigned sClass:8;\n" 2561 " unsigned ValueKind:2;\n" 2562 " uchar other;\n" 2563 "};", 2564 Style); 2565 verifyFormat("struct A {\n" 2566 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1,\n" 2567 " bbbbbbbbbbbbbbbbbbbbbbbbb:2;\n" 2568 "};", 2569 Style); 2570 Style.BitFieldColonSpacing = FormatStyle::BFCS_Before; 2571 verifyFormat("struct Bitfields {\n" 2572 " unsigned sClass :8;\n" 2573 " unsigned ValueKind :2;\n" 2574 " uchar other;\n" 2575 "};", 2576 Style); 2577 Style.BitFieldColonSpacing = FormatStyle::BFCS_After; 2578 verifyFormat("struct Bitfields {\n" 2579 " unsigned sClass: 8;\n" 2580 " unsigned ValueKind: 2;\n" 2581 " uchar other;\n" 2582 "};", 2583 Style); 2584 } 2585 2586 TEST_F(FormatTest, FormatsNamespaces) { 2587 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 2588 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 2589 2590 verifyFormat("namespace some_namespace {\n" 2591 "class A {};\n" 2592 "void f() { f(); }\n" 2593 "}", 2594 LLVMWithNoNamespaceFix); 2595 verifyFormat("namespace N::inline D {\n" 2596 "class A {};\n" 2597 "void f() { f(); }\n" 2598 "}", 2599 LLVMWithNoNamespaceFix); 2600 verifyFormat("namespace N::inline D::E {\n" 2601 "class A {};\n" 2602 "void f() { f(); }\n" 2603 "}", 2604 LLVMWithNoNamespaceFix); 2605 verifyFormat("namespace [[deprecated(\"foo[bar\")]] some_namespace {\n" 2606 "class A {};\n" 2607 "void f() { f(); }\n" 2608 "}", 2609 LLVMWithNoNamespaceFix); 2610 verifyFormat("/* something */ namespace some_namespace {\n" 2611 "class A {};\n" 2612 "void f() { f(); }\n" 2613 "}", 2614 LLVMWithNoNamespaceFix); 2615 verifyFormat("namespace {\n" 2616 "class A {};\n" 2617 "void f() { f(); }\n" 2618 "}", 2619 LLVMWithNoNamespaceFix); 2620 verifyFormat("/* something */ namespace {\n" 2621 "class A {};\n" 2622 "void f() { f(); }\n" 2623 "}", 2624 LLVMWithNoNamespaceFix); 2625 verifyFormat("inline namespace X {\n" 2626 "class A {};\n" 2627 "void f() { f(); }\n" 2628 "}", 2629 LLVMWithNoNamespaceFix); 2630 verifyFormat("/* something */ inline namespace X {\n" 2631 "class A {};\n" 2632 "void f() { f(); }\n" 2633 "}", 2634 LLVMWithNoNamespaceFix); 2635 verifyFormat("export namespace X {\n" 2636 "class A {};\n" 2637 "void f() { f(); }\n" 2638 "}", 2639 LLVMWithNoNamespaceFix); 2640 verifyFormat("using namespace some_namespace;\n" 2641 "class A {};\n" 2642 "void f() { f(); }", 2643 LLVMWithNoNamespaceFix); 2644 2645 // This code is more common than we thought; if we 2646 // layout this correctly the semicolon will go into 2647 // its own line, which is undesirable. 2648 verifyFormat("namespace {};", LLVMWithNoNamespaceFix); 2649 verifyFormat("namespace {\n" 2650 "class A {};\n" 2651 "};", 2652 LLVMWithNoNamespaceFix); 2653 2654 verifyFormat("namespace {\n" 2655 "int SomeVariable = 0; // comment\n" 2656 "} // namespace", 2657 LLVMWithNoNamespaceFix); 2658 EXPECT_EQ("#ifndef HEADER_GUARD\n" 2659 "#define HEADER_GUARD\n" 2660 "namespace my_namespace {\n" 2661 "int i;\n" 2662 "} // my_namespace\n" 2663 "#endif // HEADER_GUARD", 2664 format("#ifndef HEADER_GUARD\n" 2665 " #define HEADER_GUARD\n" 2666 " namespace my_namespace {\n" 2667 "int i;\n" 2668 "} // my_namespace\n" 2669 "#endif // HEADER_GUARD", 2670 LLVMWithNoNamespaceFix)); 2671 2672 EXPECT_EQ("namespace A::B {\n" 2673 "class C {};\n" 2674 "}", 2675 format("namespace A::B {\n" 2676 "class C {};\n" 2677 "}", 2678 LLVMWithNoNamespaceFix)); 2679 2680 FormatStyle Style = getLLVMStyle(); 2681 Style.NamespaceIndentation = FormatStyle::NI_All; 2682 EXPECT_EQ("namespace out {\n" 2683 " int i;\n" 2684 " namespace in {\n" 2685 " int i;\n" 2686 " } // namespace in\n" 2687 "} // namespace out", 2688 format("namespace out {\n" 2689 "int i;\n" 2690 "namespace in {\n" 2691 "int i;\n" 2692 "} // namespace in\n" 2693 "} // namespace out", 2694 Style)); 2695 2696 Style.NamespaceIndentation = FormatStyle::NI_Inner; 2697 EXPECT_EQ("namespace out {\n" 2698 "int i;\n" 2699 "namespace in {\n" 2700 " int i;\n" 2701 "} // namespace in\n" 2702 "} // namespace out", 2703 format("namespace out {\n" 2704 "int i;\n" 2705 "namespace in {\n" 2706 "int i;\n" 2707 "} // namespace in\n" 2708 "} // namespace out", 2709 Style)); 2710 } 2711 2712 TEST_F(FormatTest, NamespaceMacros) { 2713 FormatStyle Style = getLLVMStyle(); 2714 Style.NamespaceMacros.push_back("TESTSUITE"); 2715 2716 verifyFormat("TESTSUITE(A) {\n" 2717 "int foo();\n" 2718 "} // TESTSUITE(A)", 2719 Style); 2720 2721 verifyFormat("TESTSUITE(A, B) {\n" 2722 "int foo();\n" 2723 "} // TESTSUITE(A)", 2724 Style); 2725 2726 // Properly indent according to NamespaceIndentation style 2727 Style.NamespaceIndentation = FormatStyle::NI_All; 2728 verifyFormat("TESTSUITE(A) {\n" 2729 " int foo();\n" 2730 "} // TESTSUITE(A)", 2731 Style); 2732 verifyFormat("TESTSUITE(A) {\n" 2733 " namespace B {\n" 2734 " int foo();\n" 2735 " } // namespace B\n" 2736 "} // TESTSUITE(A)", 2737 Style); 2738 verifyFormat("namespace A {\n" 2739 " TESTSUITE(B) {\n" 2740 " int foo();\n" 2741 " } // TESTSUITE(B)\n" 2742 "} // namespace A", 2743 Style); 2744 2745 Style.NamespaceIndentation = FormatStyle::NI_Inner; 2746 verifyFormat("TESTSUITE(A) {\n" 2747 "TESTSUITE(B) {\n" 2748 " int foo();\n" 2749 "} // TESTSUITE(B)\n" 2750 "} // TESTSUITE(A)", 2751 Style); 2752 verifyFormat("TESTSUITE(A) {\n" 2753 "namespace B {\n" 2754 " int foo();\n" 2755 "} // namespace B\n" 2756 "} // TESTSUITE(A)", 2757 Style); 2758 verifyFormat("namespace A {\n" 2759 "TESTSUITE(B) {\n" 2760 " int foo();\n" 2761 "} // TESTSUITE(B)\n" 2762 "} // namespace A", 2763 Style); 2764 2765 // Properly merge namespace-macros blocks in CompactNamespaces mode 2766 Style.NamespaceIndentation = FormatStyle::NI_None; 2767 Style.CompactNamespaces = true; 2768 verifyFormat("TESTSUITE(A) { TESTSUITE(B) {\n" 2769 "}} // TESTSUITE(A::B)", 2770 Style); 2771 2772 EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n" 2773 "}} // TESTSUITE(out::in)", 2774 format("TESTSUITE(out) {\n" 2775 "TESTSUITE(in) {\n" 2776 "} // TESTSUITE(in)\n" 2777 "} // TESTSUITE(out)", 2778 Style)); 2779 2780 EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n" 2781 "}} // TESTSUITE(out::in)", 2782 format("TESTSUITE(out) {\n" 2783 "TESTSUITE(in) {\n" 2784 "} // TESTSUITE(in)\n" 2785 "} // TESTSUITE(out)", 2786 Style)); 2787 2788 // Do not merge different namespaces/macros 2789 EXPECT_EQ("namespace out {\n" 2790 "TESTSUITE(in) {\n" 2791 "} // TESTSUITE(in)\n" 2792 "} // namespace out", 2793 format("namespace out {\n" 2794 "TESTSUITE(in) {\n" 2795 "} // TESTSUITE(in)\n" 2796 "} // namespace out", 2797 Style)); 2798 EXPECT_EQ("TESTSUITE(out) {\n" 2799 "namespace in {\n" 2800 "} // namespace in\n" 2801 "} // TESTSUITE(out)", 2802 format("TESTSUITE(out) {\n" 2803 "namespace in {\n" 2804 "} // namespace in\n" 2805 "} // TESTSUITE(out)", 2806 Style)); 2807 Style.NamespaceMacros.push_back("FOOBAR"); 2808 EXPECT_EQ("TESTSUITE(out) {\n" 2809 "FOOBAR(in) {\n" 2810 "} // FOOBAR(in)\n" 2811 "} // TESTSUITE(out)", 2812 format("TESTSUITE(out) {\n" 2813 "FOOBAR(in) {\n" 2814 "} // FOOBAR(in)\n" 2815 "} // TESTSUITE(out)", 2816 Style)); 2817 } 2818 2819 TEST_F(FormatTest, FormatsCompactNamespaces) { 2820 FormatStyle Style = getLLVMStyle(); 2821 Style.CompactNamespaces = true; 2822 Style.NamespaceMacros.push_back("TESTSUITE"); 2823 2824 verifyFormat("namespace A { namespace B {\n" 2825 "}} // namespace A::B", 2826 Style); 2827 2828 EXPECT_EQ("namespace out { namespace in {\n" 2829 "}} // namespace out::in", 2830 format("namespace out {\n" 2831 "namespace in {\n" 2832 "} // namespace in\n" 2833 "} // namespace out", 2834 Style)); 2835 2836 // Only namespaces which have both consecutive opening and end get compacted 2837 EXPECT_EQ("namespace out {\n" 2838 "namespace in1 {\n" 2839 "} // namespace in1\n" 2840 "namespace in2 {\n" 2841 "} // namespace in2\n" 2842 "} // namespace out", 2843 format("namespace out {\n" 2844 "namespace in1 {\n" 2845 "} // namespace in1\n" 2846 "namespace in2 {\n" 2847 "} // namespace in2\n" 2848 "} // namespace out", 2849 Style)); 2850 2851 EXPECT_EQ("namespace out {\n" 2852 "int i;\n" 2853 "namespace in {\n" 2854 "int j;\n" 2855 "} // namespace in\n" 2856 "int k;\n" 2857 "} // namespace out", 2858 format("namespace out { int i;\n" 2859 "namespace in { int j; } // namespace in\n" 2860 "int k; } // namespace out", 2861 Style)); 2862 2863 EXPECT_EQ("namespace A { namespace B { namespace C {\n" 2864 "}}} // namespace A::B::C\n", 2865 format("namespace A { namespace B {\n" 2866 "namespace C {\n" 2867 "}} // namespace B::C\n" 2868 "} // namespace A\n", 2869 Style)); 2870 2871 Style.ColumnLimit = 40; 2872 EXPECT_EQ("namespace aaaaaaaaaa {\n" 2873 "namespace bbbbbbbbbb {\n" 2874 "}} // namespace aaaaaaaaaa::bbbbbbbbbb", 2875 format("namespace aaaaaaaaaa {\n" 2876 "namespace bbbbbbbbbb {\n" 2877 "} // namespace bbbbbbbbbb\n" 2878 "} // namespace aaaaaaaaaa", 2879 Style)); 2880 2881 EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n" 2882 "namespace cccccc {\n" 2883 "}}} // namespace aaaaaa::bbbbbb::cccccc", 2884 format("namespace aaaaaa {\n" 2885 "namespace bbbbbb {\n" 2886 "namespace cccccc {\n" 2887 "} // namespace cccccc\n" 2888 "} // namespace bbbbbb\n" 2889 "} // namespace aaaaaa", 2890 Style)); 2891 Style.ColumnLimit = 80; 2892 2893 // Extra semicolon after 'inner' closing brace prevents merging 2894 EXPECT_EQ("namespace out { namespace in {\n" 2895 "}; } // namespace out::in", 2896 format("namespace out {\n" 2897 "namespace in {\n" 2898 "}; // namespace in\n" 2899 "} // namespace out", 2900 Style)); 2901 2902 // Extra semicolon after 'outer' closing brace is conserved 2903 EXPECT_EQ("namespace out { namespace in {\n" 2904 "}}; // namespace out::in", 2905 format("namespace out {\n" 2906 "namespace in {\n" 2907 "} // namespace in\n" 2908 "}; // namespace out", 2909 Style)); 2910 2911 Style.NamespaceIndentation = FormatStyle::NI_All; 2912 EXPECT_EQ("namespace out { namespace in {\n" 2913 " int i;\n" 2914 "}} // namespace out::in", 2915 format("namespace out {\n" 2916 "namespace in {\n" 2917 "int i;\n" 2918 "} // namespace in\n" 2919 "} // namespace out", 2920 Style)); 2921 EXPECT_EQ("namespace out { namespace mid {\n" 2922 " namespace in {\n" 2923 " int j;\n" 2924 " } // namespace in\n" 2925 " int k;\n" 2926 "}} // namespace out::mid", 2927 format("namespace out { namespace mid {\n" 2928 "namespace in { int j; } // namespace in\n" 2929 "int k; }} // namespace out::mid", 2930 Style)); 2931 2932 Style.NamespaceIndentation = FormatStyle::NI_Inner; 2933 EXPECT_EQ("namespace out { namespace in {\n" 2934 " int i;\n" 2935 "}} // namespace out::in", 2936 format("namespace out {\n" 2937 "namespace in {\n" 2938 "int i;\n" 2939 "} // namespace in\n" 2940 "} // namespace out", 2941 Style)); 2942 EXPECT_EQ("namespace out { namespace mid { namespace in {\n" 2943 " int i;\n" 2944 "}}} // namespace out::mid::in", 2945 format("namespace out {\n" 2946 "namespace mid {\n" 2947 "namespace in {\n" 2948 "int i;\n" 2949 "} // namespace in\n" 2950 "} // namespace mid\n" 2951 "} // namespace out", 2952 Style)); 2953 } 2954 2955 TEST_F(FormatTest, FormatsExternC) { 2956 verifyFormat("extern \"C\" {\nint a;"); 2957 verifyFormat("extern \"C\" {}"); 2958 verifyFormat("extern \"C\" {\n" 2959 "int foo();\n" 2960 "}"); 2961 verifyFormat("extern \"C\" int foo() {}"); 2962 verifyFormat("extern \"C\" int foo();"); 2963 verifyFormat("extern \"C\" int foo() {\n" 2964 " int i = 42;\n" 2965 " return i;\n" 2966 "}"); 2967 2968 FormatStyle Style = getLLVMStyle(); 2969 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2970 Style.BraceWrapping.AfterFunction = true; 2971 verifyFormat("extern \"C\" int foo() {}", Style); 2972 verifyFormat("extern \"C\" int foo();", Style); 2973 verifyFormat("extern \"C\" int foo()\n" 2974 "{\n" 2975 " int i = 42;\n" 2976 " return i;\n" 2977 "}", 2978 Style); 2979 2980 Style.BraceWrapping.AfterExternBlock = true; 2981 Style.BraceWrapping.SplitEmptyRecord = false; 2982 verifyFormat("extern \"C\"\n" 2983 "{}", 2984 Style); 2985 verifyFormat("extern \"C\"\n" 2986 "{\n" 2987 " int foo();\n" 2988 "}", 2989 Style); 2990 } 2991 2992 TEST_F(FormatTest, IndentExternBlockStyle) { 2993 FormatStyle Style = getLLVMStyle(); 2994 Style.IndentWidth = 2; 2995 2996 Style.IndentExternBlock = FormatStyle::IEBS_Indent; 2997 verifyFormat("extern \"C\" { /*9*/\n}", Style); 2998 verifyFormat("extern \"C\" {\n" 2999 " int foo10();\n" 3000 "}", 3001 Style); 3002 3003 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; 3004 verifyFormat("extern \"C\" { /*11*/\n}", Style); 3005 verifyFormat("extern \"C\" {\n" 3006 "int foo12();\n" 3007 "}", 3008 Style); 3009 3010 Style.IndentExternBlock = FormatStyle::IEBS_AfterExternBlock; 3011 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3012 Style.BraceWrapping.AfterExternBlock = true; 3013 verifyFormat("extern \"C\"\n{ /*13*/\n}", Style); 3014 verifyFormat("extern \"C\"\n{\n" 3015 " int foo14();\n" 3016 "}", 3017 Style); 3018 3019 Style.IndentExternBlock = FormatStyle::IEBS_AfterExternBlock; 3020 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3021 Style.BraceWrapping.AfterExternBlock = false; 3022 verifyFormat("extern \"C\" { /*15*/\n}", Style); 3023 verifyFormat("extern \"C\" {\n" 3024 "int foo16();\n" 3025 "}", 3026 Style); 3027 } 3028 3029 TEST_F(FormatTest, FormatsInlineASM) { 3030 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); 3031 verifyFormat("asm(\"nop\" ::: \"memory\");"); 3032 verifyFormat( 3033 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 3034 " \"cpuid\\n\\t\"\n" 3035 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 3036 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" 3037 " : \"a\"(value));"); 3038 EXPECT_EQ( 3039 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 3040 " __asm {\n" 3041 " mov edx,[that] // vtable in edx\n" 3042 " mov eax,methodIndex\n" 3043 " call [edx][eax*4] // stdcall\n" 3044 " }\n" 3045 "}", 3046 format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 3047 " __asm {\n" 3048 " mov edx,[that] // vtable in edx\n" 3049 " mov eax,methodIndex\n" 3050 " call [edx][eax*4] // stdcall\n" 3051 " }\n" 3052 "}")); 3053 EXPECT_EQ("_asm {\n" 3054 " xor eax, eax;\n" 3055 " cpuid;\n" 3056 "}", 3057 format("_asm {\n" 3058 " xor eax, eax;\n" 3059 " cpuid;\n" 3060 "}")); 3061 verifyFormat("void function() {\n" 3062 " // comment\n" 3063 " asm(\"\");\n" 3064 "}"); 3065 EXPECT_EQ("__asm {\n" 3066 "}\n" 3067 "int i;", 3068 format("__asm {\n" 3069 "}\n" 3070 "int i;")); 3071 } 3072 3073 TEST_F(FormatTest, FormatTryCatch) { 3074 verifyFormat("try {\n" 3075 " throw a * b;\n" 3076 "} catch (int a) {\n" 3077 " // Do nothing.\n" 3078 "} catch (...) {\n" 3079 " exit(42);\n" 3080 "}"); 3081 3082 // Function-level try statements. 3083 verifyFormat("int f() try { return 4; } catch (...) {\n" 3084 " return 5;\n" 3085 "}"); 3086 verifyFormat("class A {\n" 3087 " int a;\n" 3088 " A() try : a(0) {\n" 3089 " } catch (...) {\n" 3090 " throw;\n" 3091 " }\n" 3092 "};\n"); 3093 verifyFormat("class A {\n" 3094 " int a;\n" 3095 " A() try : a(0), b{1} {\n" 3096 " } catch (...) {\n" 3097 " throw;\n" 3098 " }\n" 3099 "};\n"); 3100 verifyFormat("class A {\n" 3101 " int a;\n" 3102 " A() try : a(0), b{1}, c{2} {\n" 3103 " } catch (...) {\n" 3104 " throw;\n" 3105 " }\n" 3106 "};\n"); 3107 verifyFormat("class A {\n" 3108 " int a;\n" 3109 " A() try : a(0), b{1}, c{2} {\n" 3110 " { // New scope.\n" 3111 " }\n" 3112 " } catch (...) {\n" 3113 " throw;\n" 3114 " }\n" 3115 "};\n"); 3116 3117 // Incomplete try-catch blocks. 3118 verifyIncompleteFormat("try {} catch ("); 3119 } 3120 3121 TEST_F(FormatTest, FormatTryAsAVariable) { 3122 verifyFormat("int try;"); 3123 verifyFormat("int try, size;"); 3124 verifyFormat("try = foo();"); 3125 verifyFormat("if (try < size) {\n return true;\n}"); 3126 3127 verifyFormat("int catch;"); 3128 verifyFormat("int catch, size;"); 3129 verifyFormat("catch = foo();"); 3130 verifyFormat("if (catch < size) {\n return true;\n}"); 3131 3132 FormatStyle Style = getLLVMStyle(); 3133 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3134 Style.BraceWrapping.AfterFunction = true; 3135 Style.BraceWrapping.BeforeCatch = true; 3136 verifyFormat("try {\n" 3137 " int bar = 1;\n" 3138 "}\n" 3139 "catch (...) {\n" 3140 " int bar = 1;\n" 3141 "}", 3142 Style); 3143 verifyFormat("#if NO_EX\n" 3144 "try\n" 3145 "#endif\n" 3146 "{\n" 3147 "}\n" 3148 "#if NO_EX\n" 3149 "catch (...) {\n" 3150 "}", 3151 Style); 3152 verifyFormat("try /* abc */ {\n" 3153 " int bar = 1;\n" 3154 "}\n" 3155 "catch (...) {\n" 3156 " int bar = 1;\n" 3157 "}", 3158 Style); 3159 verifyFormat("try\n" 3160 "// abc\n" 3161 "{\n" 3162 " int bar = 1;\n" 3163 "}\n" 3164 "catch (...) {\n" 3165 " int bar = 1;\n" 3166 "}", 3167 Style); 3168 } 3169 3170 TEST_F(FormatTest, FormatSEHTryCatch) { 3171 verifyFormat("__try {\n" 3172 " int a = b * c;\n" 3173 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n" 3174 " // Do nothing.\n" 3175 "}"); 3176 3177 verifyFormat("__try {\n" 3178 " int a = b * c;\n" 3179 "} __finally {\n" 3180 " // Do nothing.\n" 3181 "}"); 3182 3183 verifyFormat("DEBUG({\n" 3184 " __try {\n" 3185 " } __finally {\n" 3186 " }\n" 3187 "});\n"); 3188 } 3189 3190 TEST_F(FormatTest, IncompleteTryCatchBlocks) { 3191 verifyFormat("try {\n" 3192 " f();\n" 3193 "} catch {\n" 3194 " g();\n" 3195 "}"); 3196 verifyFormat("try {\n" 3197 " f();\n" 3198 "} catch (A a) MACRO(x) {\n" 3199 " g();\n" 3200 "} catch (B b) MACRO(x) {\n" 3201 " g();\n" 3202 "}"); 3203 } 3204 3205 TEST_F(FormatTest, FormatTryCatchBraceStyles) { 3206 FormatStyle Style = getLLVMStyle(); 3207 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, 3208 FormatStyle::BS_WebKit}) { 3209 Style.BreakBeforeBraces = BraceStyle; 3210 verifyFormat("try {\n" 3211 " // something\n" 3212 "} catch (...) {\n" 3213 " // something\n" 3214 "}", 3215 Style); 3216 } 3217 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 3218 verifyFormat("try {\n" 3219 " // something\n" 3220 "}\n" 3221 "catch (...) {\n" 3222 " // something\n" 3223 "}", 3224 Style); 3225 verifyFormat("__try {\n" 3226 " // something\n" 3227 "}\n" 3228 "__finally {\n" 3229 " // something\n" 3230 "}", 3231 Style); 3232 verifyFormat("@try {\n" 3233 " // something\n" 3234 "}\n" 3235 "@finally {\n" 3236 " // something\n" 3237 "}", 3238 Style); 3239 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 3240 verifyFormat("try\n" 3241 "{\n" 3242 " // something\n" 3243 "}\n" 3244 "catch (...)\n" 3245 "{\n" 3246 " // something\n" 3247 "}", 3248 Style); 3249 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 3250 verifyFormat("try\n" 3251 " {\n" 3252 " // something white\n" 3253 " }\n" 3254 "catch (...)\n" 3255 " {\n" 3256 " // something white\n" 3257 " }", 3258 Style); 3259 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 3260 verifyFormat("try\n" 3261 " {\n" 3262 " // something\n" 3263 " }\n" 3264 "catch (...)\n" 3265 " {\n" 3266 " // something\n" 3267 " }", 3268 Style); 3269 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3270 Style.BraceWrapping.BeforeCatch = true; 3271 verifyFormat("try {\n" 3272 " // something\n" 3273 "}\n" 3274 "catch (...) {\n" 3275 " // something\n" 3276 "}", 3277 Style); 3278 } 3279 3280 TEST_F(FormatTest, StaticInitializers) { 3281 verifyFormat("static SomeClass SC = {1, 'a'};"); 3282 3283 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" 3284 " 100000000, " 3285 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); 3286 3287 // Here, everything other than the "}" would fit on a line. 3288 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" 3289 " 10000000000000000000000000};"); 3290 EXPECT_EQ("S s = {a,\n" 3291 "\n" 3292 " b};", 3293 format("S s = {\n" 3294 " a,\n" 3295 "\n" 3296 " b\n" 3297 "};")); 3298 3299 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first 3300 // line. However, the formatting looks a bit off and this probably doesn't 3301 // happen often in practice. 3302 verifyFormat("static int Variable[1] = {\n" 3303 " {1000000000000000000000000000000000000}};", 3304 getLLVMStyleWithColumns(40)); 3305 } 3306 3307 TEST_F(FormatTest, DesignatedInitializers) { 3308 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 3309 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" 3310 " .bbbbbbbbbb = 2,\n" 3311 " .cccccccccc = 3,\n" 3312 " .dddddddddd = 4,\n" 3313 " .eeeeeeeeee = 5};"); 3314 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 3315 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" 3316 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" 3317 " .ccccccccccccccccccccccccccc = 3,\n" 3318 " .ddddddddddddddddddddddddddd = 4,\n" 3319 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); 3320 3321 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); 3322 3323 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 3324 verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n" 3325 " [2] = bbbbbbbbbb,\n" 3326 " [3] = cccccccccc,\n" 3327 " [4] = dddddddddd,\n" 3328 " [5] = eeeeeeeeee};"); 3329 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 3330 " [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3331 " [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 3332 " [3] = cccccccccccccccccccccccccccccccccccccc,\n" 3333 " [4] = dddddddddddddddddddddddddddddddddddddd,\n" 3334 " [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};"); 3335 } 3336 3337 TEST_F(FormatTest, NestedStaticInitializers) { 3338 verifyFormat("static A x = {{{}}};\n"); 3339 verifyFormat("static A x = {{{init1, init2, init3, init4},\n" 3340 " {init1, init2, init3, init4}}};", 3341 getLLVMStyleWithColumns(50)); 3342 3343 verifyFormat("somes Status::global_reps[3] = {\n" 3344 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 3345 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 3346 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", 3347 getLLVMStyleWithColumns(60)); 3348 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n" 3349 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 3350 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 3351 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};"); 3352 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" 3353 " {rect.fRight - rect.fLeft, rect.fBottom - " 3354 "rect.fTop}};"); 3355 3356 verifyFormat( 3357 "SomeArrayOfSomeType a = {\n" 3358 " {{1, 2, 3},\n" 3359 " {1, 2, 3},\n" 3360 " {111111111111111111111111111111, 222222222222222222222222222222,\n" 3361 " 333333333333333333333333333333},\n" 3362 " {1, 2, 3},\n" 3363 " {1, 2, 3}}};"); 3364 verifyFormat( 3365 "SomeArrayOfSomeType a = {\n" 3366 " {{1, 2, 3}},\n" 3367 " {{1, 2, 3}},\n" 3368 " {{111111111111111111111111111111, 222222222222222222222222222222,\n" 3369 " 333333333333333333333333333333}},\n" 3370 " {{1, 2, 3}},\n" 3371 " {{1, 2, 3}}};"); 3372 3373 verifyFormat("struct {\n" 3374 " unsigned bit;\n" 3375 " const char *const name;\n" 3376 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" 3377 " {kOsWin, \"Windows\"},\n" 3378 " {kOsLinux, \"Linux\"},\n" 3379 " {kOsCrOS, \"Chrome OS\"}};"); 3380 verifyFormat("struct {\n" 3381 " unsigned bit;\n" 3382 " const char *const name;\n" 3383 "} kBitsToOs[] = {\n" 3384 " {kOsMac, \"Mac\"},\n" 3385 " {kOsWin, \"Windows\"},\n" 3386 " {kOsLinux, \"Linux\"},\n" 3387 " {kOsCrOS, \"Chrome OS\"},\n" 3388 "};"); 3389 } 3390 3391 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 3392 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 3393 " \\\n" 3394 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 3395 } 3396 3397 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 3398 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 3399 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 3400 3401 // Do break defaulted and deleted functions. 3402 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 3403 " default;", 3404 getLLVMStyleWithColumns(40)); 3405 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 3406 " delete;", 3407 getLLVMStyleWithColumns(40)); 3408 } 3409 3410 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { 3411 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3", 3412 getLLVMStyleWithColumns(40)); 3413 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 3414 getLLVMStyleWithColumns(40)); 3415 EXPECT_EQ("#define Q \\\n" 3416 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" 3417 " \"aaaaaaaa.cpp\"", 3418 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 3419 getLLVMStyleWithColumns(40))); 3420 } 3421 3422 TEST_F(FormatTest, UnderstandsLinePPDirective) { 3423 EXPECT_EQ("# 123 \"A string literal\"", 3424 format(" # 123 \"A string literal\"")); 3425 } 3426 3427 TEST_F(FormatTest, LayoutUnknownPPDirective) { 3428 EXPECT_EQ("#;", format("#;")); 3429 verifyFormat("#\n;\n;\n;"); 3430 } 3431 3432 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 3433 EXPECT_EQ("#line 42 \"test\"\n", 3434 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 3435 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", 3436 getLLVMStyleWithColumns(12))); 3437 } 3438 3439 TEST_F(FormatTest, EndOfFileEndsPPDirective) { 3440 EXPECT_EQ("#line 42 \"test\"", 3441 format("# \\\n line \\\n 42 \\\n \"test\"")); 3442 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); 3443 } 3444 3445 TEST_F(FormatTest, DoesntRemoveUnknownTokens) { 3446 verifyFormat("#define A \\x20"); 3447 verifyFormat("#define A \\ x20"); 3448 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20")); 3449 verifyFormat("#define A ''"); 3450 verifyFormat("#define A ''qqq"); 3451 verifyFormat("#define A `qqq"); 3452 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); 3453 EXPECT_EQ("const char *c = STRINGIFY(\n" 3454 "\\na : b);", 3455 format("const char * c = STRINGIFY(\n" 3456 "\\na : b);")); 3457 3458 verifyFormat("a\r\\"); 3459 verifyFormat("a\v\\"); 3460 verifyFormat("a\f\\"); 3461 } 3462 3463 TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) { 3464 FormatStyle style = getChromiumStyle(FormatStyle::LK_Cpp); 3465 style.IndentWidth = 4; 3466 style.PPIndentWidth = 1; 3467 3468 style.IndentPPDirectives = FormatStyle::PPDIS_None; 3469 verifyFormat("#ifdef __linux__\n" 3470 "void foo() {\n" 3471 " int x = 0;\n" 3472 "}\n" 3473 "#define FOO\n" 3474 "#endif\n" 3475 "void bar() {\n" 3476 " int y = 0;\n" 3477 "}\n", 3478 style); 3479 3480 style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 3481 verifyFormat("#ifdef __linux__\n" 3482 "void foo() {\n" 3483 " int x = 0;\n" 3484 "}\n" 3485 "# define FOO foo\n" 3486 "#endif\n" 3487 "void bar() {\n" 3488 " int y = 0;\n" 3489 "}\n", 3490 style); 3491 3492 style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash; 3493 verifyFormat("#ifdef __linux__\n" 3494 "void foo() {\n" 3495 " int x = 0;\n" 3496 "}\n" 3497 " #define FOO foo\n" 3498 "#endif\n" 3499 "void bar() {\n" 3500 " int y = 0;\n" 3501 "}\n", 3502 style); 3503 } 3504 3505 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 3506 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 3507 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 3508 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 3509 // FIXME: We never break before the macro name. 3510 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12)); 3511 3512 verifyFormat("#define A A\n#define A A"); 3513 verifyFormat("#define A(X) A\n#define A A"); 3514 3515 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 3516 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 3517 } 3518 3519 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 3520 EXPECT_EQ("// somecomment\n" 3521 "#include \"a.h\"\n" 3522 "#define A( \\\n" 3523 " A, B)\n" 3524 "#include \"b.h\"\n" 3525 "// somecomment\n", 3526 format(" // somecomment\n" 3527 " #include \"a.h\"\n" 3528 "#define A(A,\\\n" 3529 " B)\n" 3530 " #include \"b.h\"\n" 3531 " // somecomment\n", 3532 getLLVMStyleWithColumns(13))); 3533 } 3534 3535 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } 3536 3537 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 3538 EXPECT_EQ("#define A \\\n" 3539 " c; \\\n" 3540 " e;\n" 3541 "f;", 3542 format("#define A c; e;\n" 3543 "f;", 3544 getLLVMStyleWithColumns(14))); 3545 } 3546 3547 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } 3548 3549 TEST_F(FormatTest, MacroDefinitionInsideStatement) { 3550 EXPECT_EQ("int x,\n" 3551 "#define A\n" 3552 " y;", 3553 format("int x,\n#define A\ny;")); 3554 } 3555 3556 TEST_F(FormatTest, HashInMacroDefinition) { 3557 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle())); 3558 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 3559 verifyFormat("#define A \\\n" 3560 " { \\\n" 3561 " f(#c); \\\n" 3562 " }", 3563 getLLVMStyleWithColumns(11)); 3564 3565 verifyFormat("#define A(X) \\\n" 3566 " void function##X()", 3567 getLLVMStyleWithColumns(22)); 3568 3569 verifyFormat("#define A(a, b, c) \\\n" 3570 " void a##b##c()", 3571 getLLVMStyleWithColumns(22)); 3572 3573 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 3574 } 3575 3576 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 3577 EXPECT_EQ("#define A (x)", format("#define A (x)")); 3578 EXPECT_EQ("#define A(x)", format("#define A(x)")); 3579 3580 FormatStyle Style = getLLVMStyle(); 3581 Style.SpaceBeforeParens = FormatStyle::SBPO_Never; 3582 verifyFormat("#define true ((foo)1)", Style); 3583 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 3584 verifyFormat("#define false((foo)0)", Style); 3585 } 3586 3587 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 3588 EXPECT_EQ("#define A b;", format("#define A \\\n" 3589 " \\\n" 3590 " b;", 3591 getLLVMStyleWithColumns(25))); 3592 EXPECT_EQ("#define A \\\n" 3593 " \\\n" 3594 " a; \\\n" 3595 " b;", 3596 format("#define A \\\n" 3597 " \\\n" 3598 " a; \\\n" 3599 " b;", 3600 getLLVMStyleWithColumns(11))); 3601 EXPECT_EQ("#define A \\\n" 3602 " a; \\\n" 3603 " \\\n" 3604 " b;", 3605 format("#define A \\\n" 3606 " a; \\\n" 3607 " \\\n" 3608 " b;", 3609 getLLVMStyleWithColumns(11))); 3610 } 3611 3612 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 3613 verifyIncompleteFormat("#define A :"); 3614 verifyFormat("#define SOMECASES \\\n" 3615 " case 1: \\\n" 3616 " case 2\n", 3617 getLLVMStyleWithColumns(20)); 3618 verifyFormat("#define MACRO(a) \\\n" 3619 " if (a) \\\n" 3620 " f(); \\\n" 3621 " else \\\n" 3622 " g()", 3623 getLLVMStyleWithColumns(18)); 3624 verifyFormat("#define A template <typename T>"); 3625 verifyIncompleteFormat("#define STR(x) #x\n" 3626 "f(STR(this_is_a_string_literal{));"); 3627 verifyFormat("#pragma omp threadprivate( \\\n" 3628 " y)), // expected-warning", 3629 getLLVMStyleWithColumns(28)); 3630 verifyFormat("#d, = };"); 3631 verifyFormat("#if \"a"); 3632 verifyIncompleteFormat("({\n" 3633 "#define b \\\n" 3634 " } \\\n" 3635 " a\n" 3636 "a", 3637 getLLVMStyleWithColumns(15)); 3638 verifyFormat("#define A \\\n" 3639 " { \\\n" 3640 " {\n" 3641 "#define B \\\n" 3642 " } \\\n" 3643 " }", 3644 getLLVMStyleWithColumns(15)); 3645 verifyNoCrash("#if a\na(\n#else\n#endif\n{a"); 3646 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}"); 3647 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};"); 3648 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}"); 3649 } 3650 3651 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { 3652 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. 3653 EXPECT_EQ("class A : public QObject {\n" 3654 " Q_OBJECT\n" 3655 "\n" 3656 " A() {}\n" 3657 "};", 3658 format("class A : public QObject {\n" 3659 " Q_OBJECT\n" 3660 "\n" 3661 " A() {\n}\n" 3662 "} ;")); 3663 EXPECT_EQ("MACRO\n" 3664 "/*static*/ int i;", 3665 format("MACRO\n" 3666 " /*static*/ int i;")); 3667 EXPECT_EQ("SOME_MACRO\n" 3668 "namespace {\n" 3669 "void f();\n" 3670 "} // namespace", 3671 format("SOME_MACRO\n" 3672 " namespace {\n" 3673 "void f( );\n" 3674 "} // namespace")); 3675 // Only if the identifier contains at least 5 characters. 3676 EXPECT_EQ("HTTP f();", format("HTTP\nf();")); 3677 EXPECT_EQ("MACRO\nf();", format("MACRO\nf();")); 3678 // Only if everything is upper case. 3679 EXPECT_EQ("class A : public QObject {\n" 3680 " Q_Object A() {}\n" 3681 "};", 3682 format("class A : public QObject {\n" 3683 " Q_Object\n" 3684 " A() {\n}\n" 3685 "} ;")); 3686 3687 // Only if the next line can actually start an unwrapped line. 3688 EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;", 3689 format("SOME_WEIRD_LOG_MACRO\n" 3690 "<< SomeThing;")); 3691 3692 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " 3693 "(n, buffers))\n", 3694 getChromiumStyle(FormatStyle::LK_Cpp)); 3695 3696 // See PR41483 3697 EXPECT_EQ("/**/ FOO(a)\n" 3698 "FOO(b)", 3699 format("/**/ FOO(a)\n" 3700 "FOO(b)")); 3701 } 3702 3703 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { 3704 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 3705 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 3706 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 3707 "class X {};\n" 3708 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 3709 "int *createScopDetectionPass() { return 0; }", 3710 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 3711 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 3712 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 3713 " class X {};\n" 3714 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 3715 " int *createScopDetectionPass() { return 0; }")); 3716 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as 3717 // braces, so that inner block is indented one level more. 3718 EXPECT_EQ("int q() {\n" 3719 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 3720 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 3721 " IPC_END_MESSAGE_MAP()\n" 3722 "}", 3723 format("int q() {\n" 3724 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 3725 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 3726 " IPC_END_MESSAGE_MAP()\n" 3727 "}")); 3728 3729 // Same inside macros. 3730 EXPECT_EQ("#define LIST(L) \\\n" 3731 " L(A) \\\n" 3732 " L(B) \\\n" 3733 " L(C)", 3734 format("#define LIST(L) \\\n" 3735 " L(A) \\\n" 3736 " L(B) \\\n" 3737 " L(C)", 3738 getGoogleStyle())); 3739 3740 // These must not be recognized as macros. 3741 EXPECT_EQ("int q() {\n" 3742 " f(x);\n" 3743 " f(x) {}\n" 3744 " f(x)->g();\n" 3745 " f(x)->*g();\n" 3746 " f(x).g();\n" 3747 " f(x) = x;\n" 3748 " f(x) += x;\n" 3749 " f(x) -= x;\n" 3750 " f(x) *= x;\n" 3751 " f(x) /= x;\n" 3752 " f(x) %= x;\n" 3753 " f(x) &= x;\n" 3754 " f(x) |= x;\n" 3755 " f(x) ^= x;\n" 3756 " f(x) >>= x;\n" 3757 " f(x) <<= x;\n" 3758 " f(x)[y].z();\n" 3759 " LOG(INFO) << x;\n" 3760 " ifstream(x) >> x;\n" 3761 "}\n", 3762 format("int q() {\n" 3763 " f(x)\n;\n" 3764 " f(x)\n {}\n" 3765 " f(x)\n->g();\n" 3766 " f(x)\n->*g();\n" 3767 " f(x)\n.g();\n" 3768 " f(x)\n = x;\n" 3769 " f(x)\n += x;\n" 3770 " f(x)\n -= x;\n" 3771 " f(x)\n *= x;\n" 3772 " f(x)\n /= x;\n" 3773 " f(x)\n %= x;\n" 3774 " f(x)\n &= x;\n" 3775 " f(x)\n |= x;\n" 3776 " f(x)\n ^= x;\n" 3777 " f(x)\n >>= x;\n" 3778 " f(x)\n <<= x;\n" 3779 " f(x)\n[y].z();\n" 3780 " LOG(INFO)\n << x;\n" 3781 " ifstream(x)\n >> x;\n" 3782 "}\n")); 3783 EXPECT_EQ("int q() {\n" 3784 " F(x)\n" 3785 " if (1) {\n" 3786 " }\n" 3787 " F(x)\n" 3788 " while (1) {\n" 3789 " }\n" 3790 " F(x)\n" 3791 " G(x);\n" 3792 " F(x)\n" 3793 " try {\n" 3794 " Q();\n" 3795 " } catch (...) {\n" 3796 " }\n" 3797 "}\n", 3798 format("int q() {\n" 3799 "F(x)\n" 3800 "if (1) {}\n" 3801 "F(x)\n" 3802 "while (1) {}\n" 3803 "F(x)\n" 3804 "G(x);\n" 3805 "F(x)\n" 3806 "try { Q(); } catch (...) {}\n" 3807 "}\n")); 3808 EXPECT_EQ("class A {\n" 3809 " A() : t(0) {}\n" 3810 " A(int i) noexcept() : {}\n" 3811 " A(X x)\n" // FIXME: function-level try blocks are broken. 3812 " try : t(0) {\n" 3813 " } catch (...) {\n" 3814 " }\n" 3815 "};", 3816 format("class A {\n" 3817 " A()\n : t(0) {}\n" 3818 " A(int i)\n noexcept() : {}\n" 3819 " A(X x)\n" 3820 " try : t(0) {} catch (...) {}\n" 3821 "};")); 3822 FormatStyle Style = getLLVMStyle(); 3823 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3824 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 3825 Style.BraceWrapping.AfterFunction = true; 3826 EXPECT_EQ("void f()\n" 3827 "try\n" 3828 "{\n" 3829 "}", 3830 format("void f() try {\n" 3831 "}", 3832 Style)); 3833 EXPECT_EQ("class SomeClass {\n" 3834 "public:\n" 3835 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 3836 "};", 3837 format("class SomeClass {\n" 3838 "public:\n" 3839 " SomeClass()\n" 3840 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 3841 "};")); 3842 EXPECT_EQ("class SomeClass {\n" 3843 "public:\n" 3844 " SomeClass()\n" 3845 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 3846 "};", 3847 format("class SomeClass {\n" 3848 "public:\n" 3849 " SomeClass()\n" 3850 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 3851 "};", 3852 getLLVMStyleWithColumns(40))); 3853 3854 verifyFormat("MACRO(>)"); 3855 3856 // Some macros contain an implicit semicolon. 3857 Style = getLLVMStyle(); 3858 Style.StatementMacros.push_back("FOO"); 3859 verifyFormat("FOO(a) int b = 0;"); 3860 verifyFormat("FOO(a)\n" 3861 "int b = 0;", 3862 Style); 3863 verifyFormat("FOO(a);\n" 3864 "int b = 0;", 3865 Style); 3866 verifyFormat("FOO(argc, argv, \"4.0.2\")\n" 3867 "int b = 0;", 3868 Style); 3869 verifyFormat("FOO()\n" 3870 "int b = 0;", 3871 Style); 3872 verifyFormat("FOO\n" 3873 "int b = 0;", 3874 Style); 3875 verifyFormat("void f() {\n" 3876 " FOO(a)\n" 3877 " return a;\n" 3878 "}", 3879 Style); 3880 verifyFormat("FOO(a)\n" 3881 "FOO(b)", 3882 Style); 3883 verifyFormat("int a = 0;\n" 3884 "FOO(b)\n" 3885 "int c = 0;", 3886 Style); 3887 verifyFormat("int a = 0;\n" 3888 "int x = FOO(a)\n" 3889 "int b = 0;", 3890 Style); 3891 verifyFormat("void foo(int a) { FOO(a) }\n" 3892 "uint32_t bar() {}", 3893 Style); 3894 } 3895 3896 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { 3897 verifyFormat("#define A \\\n" 3898 " f({ \\\n" 3899 " g(); \\\n" 3900 " });", 3901 getLLVMStyleWithColumns(11)); 3902 } 3903 3904 TEST_F(FormatTest, IndentPreprocessorDirectives) { 3905 FormatStyle Style = getLLVMStyle(); 3906 Style.IndentPPDirectives = FormatStyle::PPDIS_None; 3907 Style.ColumnLimit = 40; 3908 verifyFormat("#ifdef _WIN32\n" 3909 "#define A 0\n" 3910 "#ifdef VAR2\n" 3911 "#define B 1\n" 3912 "#include <someheader.h>\n" 3913 "#define MACRO \\\n" 3914 " some_very_long_func_aaaaaaaaaa();\n" 3915 "#endif\n" 3916 "#else\n" 3917 "#define A 1\n" 3918 "#endif", 3919 Style); 3920 Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 3921 verifyFormat("#ifdef _WIN32\n" 3922 "# define A 0\n" 3923 "# ifdef VAR2\n" 3924 "# define B 1\n" 3925 "# include <someheader.h>\n" 3926 "# define MACRO \\\n" 3927 " some_very_long_func_aaaaaaaaaa();\n" 3928 "# endif\n" 3929 "#else\n" 3930 "# define A 1\n" 3931 "#endif", 3932 Style); 3933 verifyFormat("#if A\n" 3934 "# define MACRO \\\n" 3935 " void a(int x) { \\\n" 3936 " b(); \\\n" 3937 " c(); \\\n" 3938 " d(); \\\n" 3939 " e(); \\\n" 3940 " f(); \\\n" 3941 " }\n" 3942 "#endif", 3943 Style); 3944 // Comments before include guard. 3945 verifyFormat("// file comment\n" 3946 "// file comment\n" 3947 "#ifndef HEADER_H\n" 3948 "#define HEADER_H\n" 3949 "code();\n" 3950 "#endif", 3951 Style); 3952 // Test with include guards. 3953 verifyFormat("#ifndef HEADER_H\n" 3954 "#define HEADER_H\n" 3955 "code();\n" 3956 "#endif", 3957 Style); 3958 // Include guards must have a #define with the same variable immediately 3959 // after #ifndef. 3960 verifyFormat("#ifndef NOT_GUARD\n" 3961 "# define FOO\n" 3962 "code();\n" 3963 "#endif", 3964 Style); 3965 3966 // Include guards must cover the entire file. 3967 verifyFormat("code();\n" 3968 "code();\n" 3969 "#ifndef NOT_GUARD\n" 3970 "# define NOT_GUARD\n" 3971 "code();\n" 3972 "#endif", 3973 Style); 3974 verifyFormat("#ifndef NOT_GUARD\n" 3975 "# define NOT_GUARD\n" 3976 "code();\n" 3977 "#endif\n" 3978 "code();", 3979 Style); 3980 // Test with trailing blank lines. 3981 verifyFormat("#ifndef HEADER_H\n" 3982 "#define HEADER_H\n" 3983 "code();\n" 3984 "#endif\n", 3985 Style); 3986 // Include guards don't have #else. 3987 verifyFormat("#ifndef NOT_GUARD\n" 3988 "# define NOT_GUARD\n" 3989 "code();\n" 3990 "#else\n" 3991 "#endif", 3992 Style); 3993 verifyFormat("#ifndef NOT_GUARD\n" 3994 "# define NOT_GUARD\n" 3995 "code();\n" 3996 "#elif FOO\n" 3997 "#endif", 3998 Style); 3999 // Non-identifier #define after potential include guard. 4000 verifyFormat("#ifndef FOO\n" 4001 "# define 1\n" 4002 "#endif\n", 4003 Style); 4004 // #if closes past last non-preprocessor line. 4005 verifyFormat("#ifndef FOO\n" 4006 "#define FOO\n" 4007 "#if 1\n" 4008 "int i;\n" 4009 "# define A 0\n" 4010 "#endif\n" 4011 "#endif\n", 4012 Style); 4013 // Don't crash if there is an #elif directive without a condition. 4014 verifyFormat("#if 1\n" 4015 "int x;\n" 4016 "#elif\n" 4017 "int y;\n" 4018 "#else\n" 4019 "int z;\n" 4020 "#endif", 4021 Style); 4022 // FIXME: This doesn't handle the case where there's code between the 4023 // #ifndef and #define but all other conditions hold. This is because when 4024 // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the 4025 // previous code line yet, so we can't detect it. 4026 EXPECT_EQ("#ifndef NOT_GUARD\n" 4027 "code();\n" 4028 "#define NOT_GUARD\n" 4029 "code();\n" 4030 "#endif", 4031 format("#ifndef NOT_GUARD\n" 4032 "code();\n" 4033 "# define NOT_GUARD\n" 4034 "code();\n" 4035 "#endif", 4036 Style)); 4037 // FIXME: This doesn't handle cases where legitimate preprocessor lines may 4038 // be outside an include guard. Examples are #pragma once and 4039 // #pragma GCC diagnostic, or anything else that does not change the meaning 4040 // of the file if it's included multiple times. 4041 EXPECT_EQ("#ifdef WIN32\n" 4042 "# pragma once\n" 4043 "#endif\n" 4044 "#ifndef HEADER_H\n" 4045 "# define HEADER_H\n" 4046 "code();\n" 4047 "#endif", 4048 format("#ifdef WIN32\n" 4049 "# pragma once\n" 4050 "#endif\n" 4051 "#ifndef HEADER_H\n" 4052 "#define HEADER_H\n" 4053 "code();\n" 4054 "#endif", 4055 Style)); 4056 // FIXME: This does not detect when there is a single non-preprocessor line 4057 // in front of an include-guard-like structure where other conditions hold 4058 // because ScopedLineState hides the line. 4059 EXPECT_EQ("code();\n" 4060 "#ifndef HEADER_H\n" 4061 "#define HEADER_H\n" 4062 "code();\n" 4063 "#endif", 4064 format("code();\n" 4065 "#ifndef HEADER_H\n" 4066 "# define HEADER_H\n" 4067 "code();\n" 4068 "#endif", 4069 Style)); 4070 // Keep comments aligned with #, otherwise indent comments normally. These 4071 // tests cannot use verifyFormat because messUp manipulates leading 4072 // whitespace. 4073 { 4074 const char *Expected = "" 4075 "void f() {\n" 4076 "#if 1\n" 4077 "// Preprocessor aligned.\n" 4078 "# define A 0\n" 4079 " // Code. Separated by blank line.\n" 4080 "\n" 4081 "# define B 0\n" 4082 " // Code. Not aligned with #\n" 4083 "# define C 0\n" 4084 "#endif"; 4085 const char *ToFormat = "" 4086 "void f() {\n" 4087 "#if 1\n" 4088 "// Preprocessor aligned.\n" 4089 "# define A 0\n" 4090 "// Code. Separated by blank line.\n" 4091 "\n" 4092 "# define B 0\n" 4093 " // Code. Not aligned with #\n" 4094 "# define C 0\n" 4095 "#endif"; 4096 EXPECT_EQ(Expected, format(ToFormat, Style)); 4097 EXPECT_EQ(Expected, format(Expected, Style)); 4098 } 4099 // Keep block quotes aligned. 4100 { 4101 const char *Expected = "" 4102 "void f() {\n" 4103 "#if 1\n" 4104 "/* Preprocessor aligned. */\n" 4105 "# define A 0\n" 4106 " /* Code. Separated by blank line. */\n" 4107 "\n" 4108 "# define B 0\n" 4109 " /* Code. Not aligned with # */\n" 4110 "# define C 0\n" 4111 "#endif"; 4112 const char *ToFormat = "" 4113 "void f() {\n" 4114 "#if 1\n" 4115 "/* Preprocessor aligned. */\n" 4116 "# define A 0\n" 4117 "/* Code. Separated by blank line. */\n" 4118 "\n" 4119 "# define B 0\n" 4120 " /* Code. Not aligned with # */\n" 4121 "# define C 0\n" 4122 "#endif"; 4123 EXPECT_EQ(Expected, format(ToFormat, Style)); 4124 EXPECT_EQ(Expected, format(Expected, Style)); 4125 } 4126 // Keep comments aligned with un-indented directives. 4127 { 4128 const char *Expected = "" 4129 "void f() {\n" 4130 "// Preprocessor aligned.\n" 4131 "#define A 0\n" 4132 " // Code. Separated by blank line.\n" 4133 "\n" 4134 "#define B 0\n" 4135 " // Code. Not aligned with #\n" 4136 "#define C 0\n"; 4137 const char *ToFormat = "" 4138 "void f() {\n" 4139 "// Preprocessor aligned.\n" 4140 "#define A 0\n" 4141 "// Code. Separated by blank line.\n" 4142 "\n" 4143 "#define B 0\n" 4144 " // Code. Not aligned with #\n" 4145 "#define C 0\n"; 4146 EXPECT_EQ(Expected, format(ToFormat, Style)); 4147 EXPECT_EQ(Expected, format(Expected, Style)); 4148 } 4149 // Test AfterHash with tabs. 4150 { 4151 FormatStyle Tabbed = Style; 4152 Tabbed.UseTab = FormatStyle::UT_Always; 4153 Tabbed.IndentWidth = 8; 4154 Tabbed.TabWidth = 8; 4155 verifyFormat("#ifdef _WIN32\n" 4156 "#\tdefine A 0\n" 4157 "#\tifdef VAR2\n" 4158 "#\t\tdefine B 1\n" 4159 "#\t\tinclude <someheader.h>\n" 4160 "#\t\tdefine MACRO \\\n" 4161 "\t\t\tsome_very_long_func_aaaaaaaaaa();\n" 4162 "#\tendif\n" 4163 "#else\n" 4164 "#\tdefine A 1\n" 4165 "#endif", 4166 Tabbed); 4167 } 4168 4169 // Regression test: Multiline-macro inside include guards. 4170 verifyFormat("#ifndef HEADER_H\n" 4171 "#define HEADER_H\n" 4172 "#define A() \\\n" 4173 " int i; \\\n" 4174 " int j;\n" 4175 "#endif // HEADER_H", 4176 getLLVMStyleWithColumns(20)); 4177 4178 Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash; 4179 // Basic before hash indent tests 4180 verifyFormat("#ifdef _WIN32\n" 4181 " #define A 0\n" 4182 " #ifdef VAR2\n" 4183 " #define B 1\n" 4184 " #include <someheader.h>\n" 4185 " #define MACRO \\\n" 4186 " some_very_long_func_aaaaaaaaaa();\n" 4187 " #endif\n" 4188 "#else\n" 4189 " #define A 1\n" 4190 "#endif", 4191 Style); 4192 verifyFormat("#if A\n" 4193 " #define MACRO \\\n" 4194 " void a(int x) { \\\n" 4195 " b(); \\\n" 4196 " c(); \\\n" 4197 " d(); \\\n" 4198 " e(); \\\n" 4199 " f(); \\\n" 4200 " }\n" 4201 "#endif", 4202 Style); 4203 // Keep comments aligned with indented directives. These 4204 // tests cannot use verifyFormat because messUp manipulates leading 4205 // whitespace. 4206 { 4207 const char *Expected = "void f() {\n" 4208 "// Aligned to preprocessor.\n" 4209 "#if 1\n" 4210 " // Aligned to code.\n" 4211 " int a;\n" 4212 " #if 1\n" 4213 " // Aligned to preprocessor.\n" 4214 " #define A 0\n" 4215 " // Aligned to code.\n" 4216 " int b;\n" 4217 " #endif\n" 4218 "#endif\n" 4219 "}"; 4220 const char *ToFormat = "void f() {\n" 4221 "// Aligned to preprocessor.\n" 4222 "#if 1\n" 4223 "// Aligned to code.\n" 4224 "int a;\n" 4225 "#if 1\n" 4226 "// Aligned to preprocessor.\n" 4227 "#define A 0\n" 4228 "// Aligned to code.\n" 4229 "int b;\n" 4230 "#endif\n" 4231 "#endif\n" 4232 "}"; 4233 EXPECT_EQ(Expected, format(ToFormat, Style)); 4234 EXPECT_EQ(Expected, format(Expected, Style)); 4235 } 4236 { 4237 const char *Expected = "void f() {\n" 4238 "/* Aligned to preprocessor. */\n" 4239 "#if 1\n" 4240 " /* Aligned to code. */\n" 4241 " int a;\n" 4242 " #if 1\n" 4243 " /* Aligned to preprocessor. */\n" 4244 " #define A 0\n" 4245 " /* Aligned to code. */\n" 4246 " int b;\n" 4247 " #endif\n" 4248 "#endif\n" 4249 "}"; 4250 const char *ToFormat = "void f() {\n" 4251 "/* Aligned to preprocessor. */\n" 4252 "#if 1\n" 4253 "/* Aligned to code. */\n" 4254 "int a;\n" 4255 "#if 1\n" 4256 "/* Aligned to preprocessor. */\n" 4257 "#define A 0\n" 4258 "/* Aligned to code. */\n" 4259 "int b;\n" 4260 "#endif\n" 4261 "#endif\n" 4262 "}"; 4263 EXPECT_EQ(Expected, format(ToFormat, Style)); 4264 EXPECT_EQ(Expected, format(Expected, Style)); 4265 } 4266 4267 // Test single comment before preprocessor 4268 verifyFormat("// Comment\n" 4269 "\n" 4270 "#if 1\n" 4271 "#endif", 4272 Style); 4273 } 4274 4275 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 4276 verifyFormat("{\n { a #c; }\n}"); 4277 } 4278 4279 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 4280 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 4281 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 4282 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 4283 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 4284 } 4285 4286 TEST_F(FormatTest, EscapedNewlines) { 4287 FormatStyle Narrow = getLLVMStyleWithColumns(11); 4288 EXPECT_EQ("#define A \\\n int i; \\\n int j;", 4289 format("#define A \\\nint i;\\\n int j;", Narrow)); 4290 EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;")); 4291 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 4292 EXPECT_EQ("/* \\ \\ \\\n */", format("\\\n/* \\ \\ \\\n */")); 4293 EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>")); 4294 4295 FormatStyle AlignLeft = getLLVMStyle(); 4296 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 4297 EXPECT_EQ("#define MACRO(x) \\\n" 4298 "private: \\\n" 4299 " int x(int a);\n", 4300 format("#define MACRO(x) \\\n" 4301 "private: \\\n" 4302 " int x(int a);\n", 4303 AlignLeft)); 4304 4305 // CRLF line endings 4306 EXPECT_EQ("#define A \\\r\n int i; \\\r\n int j;", 4307 format("#define A \\\r\nint i;\\\r\n int j;", Narrow)); 4308 EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;")); 4309 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 4310 EXPECT_EQ("/* \\ \\ \\\r\n */", format("\\\r\n/* \\ \\ \\\r\n */")); 4311 EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>")); 4312 EXPECT_EQ("#define MACRO(x) \\\r\n" 4313 "private: \\\r\n" 4314 " int x(int a);\r\n", 4315 format("#define MACRO(x) \\\r\n" 4316 "private: \\\r\n" 4317 " int x(int a);\r\n", 4318 AlignLeft)); 4319 4320 FormatStyle DontAlign = getLLVMStyle(); 4321 DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 4322 DontAlign.MaxEmptyLinesToKeep = 3; 4323 // FIXME: can't use verifyFormat here because the newline before 4324 // "public:" is not inserted the first time it's reformatted 4325 EXPECT_EQ("#define A \\\n" 4326 " class Foo { \\\n" 4327 " void bar(); \\\n" 4328 "\\\n" 4329 "\\\n" 4330 "\\\n" 4331 " public: \\\n" 4332 " void baz(); \\\n" 4333 " };", 4334 format("#define A \\\n" 4335 " class Foo { \\\n" 4336 " void bar(); \\\n" 4337 "\\\n" 4338 "\\\n" 4339 "\\\n" 4340 " public: \\\n" 4341 " void baz(); \\\n" 4342 " };", 4343 DontAlign)); 4344 } 4345 4346 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 4347 verifyFormat("#define A \\\n" 4348 " int v( \\\n" 4349 " a); \\\n" 4350 " int i;", 4351 getLLVMStyleWithColumns(11)); 4352 } 4353 4354 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 4355 EXPECT_EQ( 4356 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 4357 " \\\n" 4358 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 4359 "\n" 4360 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 4361 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 4362 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 4363 "\\\n" 4364 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 4365 " \n" 4366 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 4367 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 4368 } 4369 4370 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 4371 EXPECT_EQ("int\n" 4372 "#define A\n" 4373 " a;", 4374 format("int\n#define A\na;")); 4375 verifyFormat("functionCallTo(\n" 4376 " someOtherFunction(\n" 4377 " withSomeParameters, whichInSequence,\n" 4378 " areLongerThanALine(andAnotherCall,\n" 4379 "#define A B\n" 4380 " withMoreParamters,\n" 4381 " whichStronglyInfluenceTheLayout),\n" 4382 " andMoreParameters),\n" 4383 " trailing);", 4384 getLLVMStyleWithColumns(69)); 4385 verifyFormat("Foo::Foo()\n" 4386 "#ifdef BAR\n" 4387 " : baz(0)\n" 4388 "#endif\n" 4389 "{\n" 4390 "}"); 4391 verifyFormat("void f() {\n" 4392 " if (true)\n" 4393 "#ifdef A\n" 4394 " f(42);\n" 4395 " x();\n" 4396 "#else\n" 4397 " g();\n" 4398 " x();\n" 4399 "#endif\n" 4400 "}"); 4401 verifyFormat("void f(param1, param2,\n" 4402 " param3,\n" 4403 "#ifdef A\n" 4404 " param4(param5,\n" 4405 "#ifdef A1\n" 4406 " param6,\n" 4407 "#ifdef A2\n" 4408 " param7),\n" 4409 "#else\n" 4410 " param8),\n" 4411 " param9,\n" 4412 "#endif\n" 4413 " param10,\n" 4414 "#endif\n" 4415 " param11)\n" 4416 "#else\n" 4417 " param12)\n" 4418 "#endif\n" 4419 "{\n" 4420 " x();\n" 4421 "}", 4422 getLLVMStyleWithColumns(28)); 4423 verifyFormat("#if 1\n" 4424 "int i;"); 4425 verifyFormat("#if 1\n" 4426 "#endif\n" 4427 "#if 1\n" 4428 "#else\n" 4429 "#endif\n"); 4430 verifyFormat("DEBUG({\n" 4431 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4432 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 4433 "});\n" 4434 "#if a\n" 4435 "#else\n" 4436 "#endif"); 4437 4438 verifyIncompleteFormat("void f(\n" 4439 "#if A\n" 4440 ");\n" 4441 "#else\n" 4442 "#endif"); 4443 } 4444 4445 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { 4446 verifyFormat("#endif\n" 4447 "#if B"); 4448 } 4449 4450 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { 4451 FormatStyle SingleLine = getLLVMStyle(); 4452 SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; 4453 verifyFormat("#if 0\n" 4454 "#elif 1\n" 4455 "#endif\n" 4456 "void foo() {\n" 4457 " if (test) foo2();\n" 4458 "}", 4459 SingleLine); 4460 } 4461 4462 TEST_F(FormatTest, LayoutBlockInsideParens) { 4463 verifyFormat("functionCall({ int i; });"); 4464 verifyFormat("functionCall({\n" 4465 " int i;\n" 4466 " int j;\n" 4467 "});"); 4468 verifyFormat("functionCall(\n" 4469 " {\n" 4470 " int i;\n" 4471 " int j;\n" 4472 " },\n" 4473 " aaaa, bbbb, cccc);"); 4474 verifyFormat("functionA(functionB({\n" 4475 " int i;\n" 4476 " int j;\n" 4477 " }),\n" 4478 " aaaa, bbbb, cccc);"); 4479 verifyFormat("functionCall(\n" 4480 " {\n" 4481 " int i;\n" 4482 " int j;\n" 4483 " },\n" 4484 " aaaa, bbbb, // comment\n" 4485 " cccc);"); 4486 verifyFormat("functionA(functionB({\n" 4487 " int i;\n" 4488 " int j;\n" 4489 " }),\n" 4490 " aaaa, bbbb, // comment\n" 4491 " cccc);"); 4492 verifyFormat("functionCall(aaaa, bbbb, { int i; });"); 4493 verifyFormat("functionCall(aaaa, bbbb, {\n" 4494 " int i;\n" 4495 " int j;\n" 4496 "});"); 4497 verifyFormat( 4498 "Aaa(\n" // FIXME: There shouldn't be a linebreak here. 4499 " {\n" 4500 " int i; // break\n" 4501 " },\n" 4502 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 4503 " ccccccccccccccccc));"); 4504 verifyFormat("DEBUG({\n" 4505 " if (a)\n" 4506 " f();\n" 4507 "});"); 4508 } 4509 4510 TEST_F(FormatTest, LayoutBlockInsideStatement) { 4511 EXPECT_EQ("SOME_MACRO { int i; }\n" 4512 "int i;", 4513 format(" SOME_MACRO {int i;} int i;")); 4514 } 4515 4516 TEST_F(FormatTest, LayoutNestedBlocks) { 4517 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 4518 " struct s {\n" 4519 " int i;\n" 4520 " };\n" 4521 " s kBitsToOs[] = {{10}};\n" 4522 " for (int i = 0; i < 10; ++i)\n" 4523 " return;\n" 4524 "}"); 4525 verifyFormat("call(parameter, {\n" 4526 " something();\n" 4527 " // Comment using all columns.\n" 4528 " somethingelse();\n" 4529 "});", 4530 getLLVMStyleWithColumns(40)); 4531 verifyFormat("DEBUG( //\n" 4532 " { f(); }, a);"); 4533 verifyFormat("DEBUG( //\n" 4534 " {\n" 4535 " f(); //\n" 4536 " },\n" 4537 " a);"); 4538 4539 EXPECT_EQ("call(parameter, {\n" 4540 " something();\n" 4541 " // Comment too\n" 4542 " // looooooooooong.\n" 4543 " somethingElse();\n" 4544 "});", 4545 format("call(parameter, {\n" 4546 " something();\n" 4547 " // Comment too looooooooooong.\n" 4548 " somethingElse();\n" 4549 "});", 4550 getLLVMStyleWithColumns(29))); 4551 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });")); 4552 EXPECT_EQ("DEBUG({ // comment\n" 4553 " int i;\n" 4554 "});", 4555 format("DEBUG({ // comment\n" 4556 "int i;\n" 4557 "});")); 4558 EXPECT_EQ("DEBUG({\n" 4559 " int i;\n" 4560 "\n" 4561 " // comment\n" 4562 " int j;\n" 4563 "});", 4564 format("DEBUG({\n" 4565 " int i;\n" 4566 "\n" 4567 " // comment\n" 4568 " int j;\n" 4569 "});")); 4570 4571 verifyFormat("DEBUG({\n" 4572 " if (a)\n" 4573 " return;\n" 4574 "});"); 4575 verifyGoogleFormat("DEBUG({\n" 4576 " if (a) return;\n" 4577 "});"); 4578 FormatStyle Style = getGoogleStyle(); 4579 Style.ColumnLimit = 45; 4580 verifyFormat("Debug(\n" 4581 " aaaaa,\n" 4582 " {\n" 4583 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" 4584 " },\n" 4585 " a);", 4586 Style); 4587 4588 verifyFormat("SomeFunction({MACRO({ return output; }), b});"); 4589 4590 verifyNoCrash("^{v^{a}}"); 4591 } 4592 4593 TEST_F(FormatTest, FormatNestedBlocksInMacros) { 4594 EXPECT_EQ("#define MACRO() \\\n" 4595 " Debug(aaa, /* force line break */ \\\n" 4596 " { \\\n" 4597 " int i; \\\n" 4598 " int j; \\\n" 4599 " })", 4600 format("#define MACRO() Debug(aaa, /* force line break */ \\\n" 4601 " { int i; int j; })", 4602 getGoogleStyle())); 4603 4604 EXPECT_EQ("#define A \\\n" 4605 " [] { \\\n" 4606 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 4607 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 4608 " }", 4609 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 4610 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 4611 getGoogleStyle())); 4612 } 4613 4614 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 4615 EXPECT_EQ("{}", format("{}")); 4616 verifyFormat("enum E {};"); 4617 verifyFormat("enum E {}"); 4618 FormatStyle Style = getLLVMStyle(); 4619 Style.SpaceInEmptyBlock = true; 4620 EXPECT_EQ("void f() { }", format("void f() {}", Style)); 4621 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 4622 EXPECT_EQ("while (true) { }", format("while (true) {}", Style)); 4623 } 4624 4625 TEST_F(FormatTest, FormatBeginBlockEndMacros) { 4626 FormatStyle Style = getLLVMStyle(); 4627 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$"; 4628 Style.MacroBlockEnd = "^[A-Z_]+_END$"; 4629 verifyFormat("FOO_BEGIN\n" 4630 " FOO_ENTRY\n" 4631 "FOO_END", 4632 Style); 4633 verifyFormat("FOO_BEGIN\n" 4634 " NESTED_FOO_BEGIN\n" 4635 " NESTED_FOO_ENTRY\n" 4636 " NESTED_FOO_END\n" 4637 "FOO_END", 4638 Style); 4639 verifyFormat("FOO_BEGIN(Foo, Bar)\n" 4640 " int x;\n" 4641 " x = 1;\n" 4642 "FOO_END(Baz)", 4643 Style); 4644 } 4645 4646 //===----------------------------------------------------------------------===// 4647 // Line break tests. 4648 //===----------------------------------------------------------------------===// 4649 4650 TEST_F(FormatTest, PreventConfusingIndents) { 4651 verifyFormat( 4652 "void f() {\n" 4653 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 4654 " parameter, parameter, parameter)),\n" 4655 " SecondLongCall(parameter));\n" 4656 "}"); 4657 verifyFormat( 4658 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4659 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 4660 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4661 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 4662 verifyFormat( 4663 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4664 " [aaaaaaaaaaaaaaaaaaaaaaaa\n" 4665 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 4666 " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); 4667 verifyFormat( 4668 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 4669 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 4670 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 4671 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 4672 verifyFormat("int a = bbbb && ccc &&\n" 4673 " fffff(\n" 4674 "#define A Just forcing a new line\n" 4675 " ddd);"); 4676 } 4677 4678 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 4679 verifyFormat( 4680 "bool aaaaaaa =\n" 4681 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 4682 " bbbbbbbb();"); 4683 verifyFormat( 4684 "bool aaaaaaa =\n" 4685 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" 4686 " bbbbbbbb();"); 4687 4688 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 4689 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 4690 " ccccccccc == ddddddddddd;"); 4691 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 4692 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" 4693 " ccccccccc == ddddddddddd;"); 4694 verifyFormat( 4695 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 4696 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" 4697 " ccccccccc == ddddddddddd;"); 4698 4699 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 4700 " aaaaaa) &&\n" 4701 " bbbbbb && cccccc;"); 4702 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 4703 " aaaaaa) >>\n" 4704 " bbbbbb;"); 4705 verifyFormat("aa = Whitespaces.addUntouchableComment(\n" 4706 " SourceMgr.getSpellingColumnNumber(\n" 4707 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 4708 " 1);"); 4709 4710 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4711 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 4712 " cccccc) {\n}"); 4713 verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4714 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 4715 " cccccc) {\n}"); 4716 verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4717 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 4718 " cccccc) {\n}"); 4719 verifyFormat("b = a &&\n" 4720 " // Comment\n" 4721 " b.c && d;"); 4722 4723 // If the LHS of a comparison is not a binary expression itself, the 4724 // additional linebreak confuses many people. 4725 verifyFormat( 4726 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4727 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 4728 "}"); 4729 verifyFormat( 4730 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4731 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 4732 "}"); 4733 verifyFormat( 4734 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 4735 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 4736 "}"); 4737 verifyFormat( 4738 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4739 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n" 4740 "}"); 4741 // Even explicit parentheses stress the precedence enough to make the 4742 // additional break unnecessary. 4743 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4744 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 4745 "}"); 4746 // This cases is borderline, but with the indentation it is still readable. 4747 verifyFormat( 4748 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4749 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4750 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 4751 "}", 4752 getLLVMStyleWithColumns(75)); 4753 4754 // If the LHS is a binary expression, we should still use the additional break 4755 // as otherwise the formatting hides the operator precedence. 4756 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4757 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 4758 " 5) {\n" 4759 "}"); 4760 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4761 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n" 4762 " 5) {\n" 4763 "}"); 4764 4765 FormatStyle OnePerLine = getLLVMStyle(); 4766 OnePerLine.BinPackParameters = false; 4767 verifyFormat( 4768 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4769 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4770 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 4771 OnePerLine); 4772 4773 verifyFormat("int i = someFunction(aaaaaaa, 0)\n" 4774 " .aaa(aaaaaaaaaaaaa) *\n" 4775 " aaaaaaa +\n" 4776 " aaaaaaa;", 4777 getLLVMStyleWithColumns(40)); 4778 } 4779 4780 TEST_F(FormatTest, ExpressionIndentation) { 4781 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4782 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4783 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 4784 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 4785 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 4786 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 4787 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 4788 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 4789 " ccccccccccccccccccccccccccccccccccccccccc;"); 4790 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 4791 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4792 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 4793 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 4794 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4795 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 4796 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 4797 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 4798 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 4799 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 4800 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4801 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 4802 verifyFormat("if () {\n" 4803 "} else if (aaaaa && bbbbb > // break\n" 4804 " ccccc) {\n" 4805 "}"); 4806 verifyFormat("if () {\n" 4807 "} else if constexpr (aaaaa && bbbbb > // break\n" 4808 " ccccc) {\n" 4809 "}"); 4810 verifyFormat("if () {\n" 4811 "} else if CONSTEXPR (aaaaa && bbbbb > // break\n" 4812 " ccccc) {\n" 4813 "}"); 4814 verifyFormat("if () {\n" 4815 "} else if (aaaaa &&\n" 4816 " bbbbb > // break\n" 4817 " ccccc &&\n" 4818 " ddddd) {\n" 4819 "}"); 4820 4821 // Presence of a trailing comment used to change indentation of b. 4822 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 4823 " b;\n" 4824 "return aaaaaaaaaaaaaaaaaaa +\n" 4825 " b; //", 4826 getLLVMStyleWithColumns(30)); 4827 } 4828 4829 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 4830 // Not sure what the best system is here. Like this, the LHS can be found 4831 // immediately above an operator (everything with the same or a higher 4832 // indent). The RHS is aligned right of the operator and so compasses 4833 // everything until something with the same indent as the operator is found. 4834 // FIXME: Is this a good system? 4835 FormatStyle Style = getLLVMStyle(); 4836 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 4837 verifyFormat( 4838 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4839 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4840 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4841 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4842 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 4843 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 4844 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4845 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4846 " > ccccccccccccccccccccccccccccccccccccccccc;", 4847 Style); 4848 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4849 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4850 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4851 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 4852 Style); 4853 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4854 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4855 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4856 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 4857 Style); 4858 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4859 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4860 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4861 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 4862 Style); 4863 verifyFormat("if () {\n" 4864 "} else if (aaaaa\n" 4865 " && bbbbb // break\n" 4866 " > ccccc) {\n" 4867 "}", 4868 Style); 4869 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4870 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 4871 Style); 4872 verifyFormat("return (a)\n" 4873 " // comment\n" 4874 " + b;", 4875 Style); 4876 verifyFormat( 4877 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4878 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 4879 " + cc;", 4880 Style); 4881 4882 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4883 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4884 Style); 4885 4886 // Forced by comments. 4887 verifyFormat( 4888 "unsigned ContentSize =\n" 4889 " sizeof(int16_t) // DWARF ARange version number\n" 4890 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 4891 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 4892 " + sizeof(int8_t); // Segment Size (in bytes)"); 4893 4894 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 4895 " == boost::fusion::at_c<1>(iiii).second;", 4896 Style); 4897 4898 Style.ColumnLimit = 60; 4899 verifyFormat("zzzzzzzzzz\n" 4900 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 4901 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 4902 Style); 4903 4904 Style.ColumnLimit = 80; 4905 Style.IndentWidth = 4; 4906 Style.TabWidth = 4; 4907 Style.UseTab = FormatStyle::UT_Always; 4908 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4909 Style.AlignOperands = FormatStyle::OAS_DontAlign; 4910 EXPECT_EQ("return someVeryVeryLongConditionThatBarelyFitsOnALine\n" 4911 "\t&& (someOtherLongishConditionPart1\n" 4912 "\t\t|| someOtherEvenLongerNestedConditionPart2);", 4913 format("return someVeryVeryLongConditionThatBarelyFitsOnALine && " 4914 "(someOtherLongishConditionPart1 || " 4915 "someOtherEvenLongerNestedConditionPart2);", 4916 Style)); 4917 } 4918 4919 TEST_F(FormatTest, ExpressionIndentationStrictAlign) { 4920 FormatStyle Style = getLLVMStyle(); 4921 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 4922 Style.AlignOperands = FormatStyle::OAS_AlignAfterOperator; 4923 4924 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4925 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4926 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4927 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4928 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 4929 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 4930 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4931 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4932 " > ccccccccccccccccccccccccccccccccccccccccc;", 4933 Style); 4934 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4935 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4936 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4937 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 4938 Style); 4939 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4940 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4941 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4942 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 4943 Style); 4944 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4945 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4946 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4947 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 4948 Style); 4949 verifyFormat("if () {\n" 4950 "} else if (aaaaa\n" 4951 " && bbbbb // break\n" 4952 " > ccccc) {\n" 4953 "}", 4954 Style); 4955 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4956 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 4957 Style); 4958 verifyFormat("return (a)\n" 4959 " // comment\n" 4960 " + b;", 4961 Style); 4962 verifyFormat( 4963 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4964 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 4965 " + cc;", 4966 Style); 4967 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 4968 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 4969 " : 3333333333333333;", 4970 Style); 4971 verifyFormat( 4972 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 4973 " : ccccccccccccccc ? dddddddddddddddddd\n" 4974 " : eeeeeeeeeeeeeeeeee)\n" 4975 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 4976 " : 3333333333333333;", 4977 Style); 4978 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4979 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4980 Style); 4981 4982 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 4983 " == boost::fusion::at_c<1>(iiii).second;", 4984 Style); 4985 4986 Style.ColumnLimit = 60; 4987 verifyFormat("zzzzzzzzzzzzz\n" 4988 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 4989 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 4990 Style); 4991 4992 // Forced by comments. 4993 Style.ColumnLimit = 80; 4994 verifyFormat( 4995 "unsigned ContentSize\n" 4996 " = sizeof(int16_t) // DWARF ARange version number\n" 4997 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 4998 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 4999 " + sizeof(int8_t); // Segment Size (in bytes)", 5000 Style); 5001 5002 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 5003 verifyFormat( 5004 "unsigned ContentSize =\n" 5005 " sizeof(int16_t) // DWARF ARange version number\n" 5006 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 5007 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 5008 " + sizeof(int8_t); // Segment Size (in bytes)", 5009 Style); 5010 5011 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 5012 verifyFormat( 5013 "unsigned ContentSize =\n" 5014 " sizeof(int16_t) // DWARF ARange version number\n" 5015 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 5016 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 5017 " + sizeof(int8_t); // Segment Size (in bytes)", 5018 Style); 5019 } 5020 5021 TEST_F(FormatTest, EnforcedOperatorWraps) { 5022 // Here we'd like to wrap after the || operators, but a comment is forcing an 5023 // earlier wrap. 5024 verifyFormat("bool x = aaaaa //\n" 5025 " || bbbbb\n" 5026 " //\n" 5027 " || cccc;"); 5028 } 5029 5030 TEST_F(FormatTest, NoOperandAlignment) { 5031 FormatStyle Style = getLLVMStyle(); 5032 Style.AlignOperands = FormatStyle::OAS_DontAlign; 5033 verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n" 5034 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5035 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5036 Style); 5037 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 5038 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5039 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5040 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5041 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5042 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5043 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5044 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5045 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5046 " > ccccccccccccccccccccccccccccccccccccccccc;", 5047 Style); 5048 5049 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5050 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5051 " + cc;", 5052 Style); 5053 verifyFormat("int a = aa\n" 5054 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5055 " * cccccccccccccccccccccccccccccccccccc;\n", 5056 Style); 5057 5058 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5059 verifyFormat("return (a > b\n" 5060 " // comment1\n" 5061 " // comment2\n" 5062 " || c);", 5063 Style); 5064 } 5065 5066 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { 5067 FormatStyle Style = getLLVMStyle(); 5068 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 5069 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5070 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5071 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 5072 Style); 5073 } 5074 5075 TEST_F(FormatTest, AllowBinPackingInsideArguments) { 5076 FormatStyle Style = getLLVMStyle(); 5077 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 5078 Style.BinPackArguments = false; 5079 Style.ColumnLimit = 40; 5080 verifyFormat("void test() {\n" 5081 " someFunction(\n" 5082 " this + argument + is + quite\n" 5083 " + long + so + it + gets + wrapped\n" 5084 " + but + remains + bin - packed);\n" 5085 "}", 5086 Style); 5087 verifyFormat("void test() {\n" 5088 " someFunction(arg1,\n" 5089 " this + argument + is\n" 5090 " + quite + long + so\n" 5091 " + it + gets + wrapped\n" 5092 " + but + remains + bin\n" 5093 " - packed,\n" 5094 " arg3);\n" 5095 "}", 5096 Style); 5097 verifyFormat("void test() {\n" 5098 " someFunction(\n" 5099 " arg1,\n" 5100 " this + argument + has\n" 5101 " + anotherFunc(nested,\n" 5102 " calls + whose\n" 5103 " + arguments\n" 5104 " + are + also\n" 5105 " + wrapped,\n" 5106 " in + addition)\n" 5107 " + to + being + bin - packed,\n" 5108 " arg3);\n" 5109 "}", 5110 Style); 5111 5112 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 5113 verifyFormat("void test() {\n" 5114 " someFunction(\n" 5115 " arg1,\n" 5116 " this + argument + has +\n" 5117 " anotherFunc(nested,\n" 5118 " calls + whose +\n" 5119 " arguments +\n" 5120 " are + also +\n" 5121 " wrapped,\n" 5122 " in + addition) +\n" 5123 " to + being + bin - packed,\n" 5124 " arg3);\n" 5125 "}", 5126 Style); 5127 } 5128 5129 TEST_F(FormatTest, ConstructorInitializers) { 5130 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 5131 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 5132 getLLVMStyleWithColumns(45)); 5133 verifyFormat("Constructor()\n" 5134 " : Inttializer(FitsOnTheLine) {}", 5135 getLLVMStyleWithColumns(44)); 5136 verifyFormat("Constructor()\n" 5137 " : Inttializer(FitsOnTheLine) {}", 5138 getLLVMStyleWithColumns(43)); 5139 5140 verifyFormat("template <typename T>\n" 5141 "Constructor() : Initializer(FitsOnTheLine) {}", 5142 getLLVMStyleWithColumns(45)); 5143 5144 verifyFormat( 5145 "SomeClass::Constructor()\n" 5146 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 5147 5148 verifyFormat( 5149 "SomeClass::Constructor()\n" 5150 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 5151 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 5152 verifyFormat( 5153 "SomeClass::Constructor()\n" 5154 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5155 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 5156 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5157 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5158 " : aaaaaaaaaa(aaaaaa) {}"); 5159 5160 verifyFormat("Constructor()\n" 5161 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5162 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5163 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5164 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 5165 5166 verifyFormat("Constructor()\n" 5167 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5168 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 5169 5170 verifyFormat("Constructor(int Parameter = 0)\n" 5171 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 5172 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 5173 verifyFormat("Constructor()\n" 5174 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 5175 "}", 5176 getLLVMStyleWithColumns(60)); 5177 verifyFormat("Constructor()\n" 5178 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5179 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 5180 5181 // Here a line could be saved by splitting the second initializer onto two 5182 // lines, but that is not desirable. 5183 verifyFormat("Constructor()\n" 5184 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 5185 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 5186 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 5187 5188 FormatStyle OnePerLine = getLLVMStyle(); 5189 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 5190 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 5191 verifyFormat("SomeClass::Constructor()\n" 5192 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 5193 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 5194 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 5195 OnePerLine); 5196 verifyFormat("SomeClass::Constructor()\n" 5197 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 5198 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 5199 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 5200 OnePerLine); 5201 verifyFormat("MyClass::MyClass(int var)\n" 5202 " : some_var_(var), // 4 space indent\n" 5203 " some_other_var_(var + 1) { // lined up\n" 5204 "}", 5205 OnePerLine); 5206 verifyFormat("Constructor()\n" 5207 " : aaaaa(aaaaaa),\n" 5208 " aaaaa(aaaaaa),\n" 5209 " aaaaa(aaaaaa),\n" 5210 " aaaaa(aaaaaa),\n" 5211 " aaaaa(aaaaaa) {}", 5212 OnePerLine); 5213 verifyFormat("Constructor()\n" 5214 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 5215 " aaaaaaaaaaaaaaaaaaaaaa) {}", 5216 OnePerLine); 5217 OnePerLine.BinPackParameters = false; 5218 verifyFormat( 5219 "Constructor()\n" 5220 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n" 5221 " aaaaaaaaaaa().aaa(),\n" 5222 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 5223 OnePerLine); 5224 OnePerLine.ColumnLimit = 60; 5225 verifyFormat("Constructor()\n" 5226 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 5227 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 5228 OnePerLine); 5229 5230 EXPECT_EQ("Constructor()\n" 5231 " : // Comment forcing unwanted break.\n" 5232 " aaaa(aaaa) {}", 5233 format("Constructor() :\n" 5234 " // Comment forcing unwanted break.\n" 5235 " aaaa(aaaa) {}")); 5236 } 5237 5238 TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) { 5239 FormatStyle Style = getLLVMStyle(); 5240 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 5241 Style.ColumnLimit = 60; 5242 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 5243 Style.AllowAllConstructorInitializersOnNextLine = true; 5244 Style.BinPackParameters = false; 5245 5246 for (int i = 0; i < 4; ++i) { 5247 // Test all combinations of parameters that should not have an effect. 5248 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1; 5249 Style.AllowAllArgumentsOnNextLine = i & 2; 5250 5251 Style.AllowAllConstructorInitializersOnNextLine = true; 5252 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 5253 verifyFormat("Constructor()\n" 5254 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 5255 Style); 5256 verifyFormat("Constructor() : a(a), b(b) {}", Style); 5257 5258 Style.AllowAllConstructorInitializersOnNextLine = false; 5259 verifyFormat("Constructor()\n" 5260 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 5261 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 5262 Style); 5263 verifyFormat("Constructor() : a(a), b(b) {}", Style); 5264 5265 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 5266 Style.AllowAllConstructorInitializersOnNextLine = true; 5267 verifyFormat("Constructor()\n" 5268 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 5269 Style); 5270 5271 Style.AllowAllConstructorInitializersOnNextLine = false; 5272 verifyFormat("Constructor()\n" 5273 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 5274 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 5275 Style); 5276 5277 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 5278 Style.AllowAllConstructorInitializersOnNextLine = true; 5279 verifyFormat("Constructor() :\n" 5280 " aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 5281 Style); 5282 5283 Style.AllowAllConstructorInitializersOnNextLine = false; 5284 verifyFormat("Constructor() :\n" 5285 " aaaaaaaaaaaaaaaaaa(a),\n" 5286 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 5287 Style); 5288 } 5289 5290 // Test interactions between AllowAllParametersOfDeclarationOnNextLine and 5291 // AllowAllConstructorInitializersOnNextLine in all 5292 // BreakConstructorInitializers modes 5293 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 5294 Style.AllowAllParametersOfDeclarationOnNextLine = true; 5295 Style.AllowAllConstructorInitializersOnNextLine = false; 5296 verifyFormat("SomeClassWithALongName::Constructor(\n" 5297 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n" 5298 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 5299 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 5300 Style); 5301 5302 Style.AllowAllConstructorInitializersOnNextLine = true; 5303 verifyFormat("SomeClassWithALongName::Constructor(\n" 5304 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 5305 " int bbbbbbbbbbbbb,\n" 5306 " int cccccccccccccccc)\n" 5307 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 5308 Style); 5309 5310 Style.AllowAllParametersOfDeclarationOnNextLine = false; 5311 Style.AllowAllConstructorInitializersOnNextLine = false; 5312 verifyFormat("SomeClassWithALongName::Constructor(\n" 5313 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 5314 " int bbbbbbbbbbbbb)\n" 5315 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 5316 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 5317 Style); 5318 5319 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 5320 5321 Style.AllowAllParametersOfDeclarationOnNextLine = true; 5322 verifyFormat("SomeClassWithALongName::Constructor(\n" 5323 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n" 5324 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 5325 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 5326 Style); 5327 5328 Style.AllowAllConstructorInitializersOnNextLine = true; 5329 verifyFormat("SomeClassWithALongName::Constructor(\n" 5330 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 5331 " int bbbbbbbbbbbbb,\n" 5332 " int cccccccccccccccc)\n" 5333 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 5334 Style); 5335 5336 Style.AllowAllParametersOfDeclarationOnNextLine = false; 5337 Style.AllowAllConstructorInitializersOnNextLine = false; 5338 verifyFormat("SomeClassWithALongName::Constructor(\n" 5339 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 5340 " int bbbbbbbbbbbbb)\n" 5341 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 5342 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 5343 Style); 5344 5345 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 5346 Style.AllowAllParametersOfDeclarationOnNextLine = true; 5347 verifyFormat("SomeClassWithALongName::Constructor(\n" 5348 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n" 5349 " aaaaaaaaaaaaaaaaaaaa(a),\n" 5350 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 5351 Style); 5352 5353 Style.AllowAllConstructorInitializersOnNextLine = true; 5354 verifyFormat("SomeClassWithALongName::Constructor(\n" 5355 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 5356 " int bbbbbbbbbbbbb,\n" 5357 " int cccccccccccccccc) :\n" 5358 " aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 5359 Style); 5360 5361 Style.AllowAllParametersOfDeclarationOnNextLine = false; 5362 Style.AllowAllConstructorInitializersOnNextLine = false; 5363 verifyFormat("SomeClassWithALongName::Constructor(\n" 5364 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 5365 " int bbbbbbbbbbbbb) :\n" 5366 " aaaaaaaaaaaaaaaaaaaa(a),\n" 5367 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 5368 Style); 5369 } 5370 5371 TEST_F(FormatTest, AllowAllArgumentsOnNextLine) { 5372 FormatStyle Style = getLLVMStyle(); 5373 Style.ColumnLimit = 60; 5374 Style.BinPackArguments = false; 5375 for (int i = 0; i < 4; ++i) { 5376 // Test all combinations of parameters that should not have an effect. 5377 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1; 5378 Style.AllowAllConstructorInitializersOnNextLine = i & 2; 5379 5380 Style.AllowAllArgumentsOnNextLine = true; 5381 verifyFormat("void foo() {\n" 5382 " FunctionCallWithReallyLongName(\n" 5383 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n" 5384 "}", 5385 Style); 5386 Style.AllowAllArgumentsOnNextLine = false; 5387 verifyFormat("void foo() {\n" 5388 " FunctionCallWithReallyLongName(\n" 5389 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5390 " bbbbbbbbbbbb);\n" 5391 "}", 5392 Style); 5393 5394 Style.AllowAllArgumentsOnNextLine = true; 5395 verifyFormat("void foo() {\n" 5396 " auto VariableWithReallyLongName = {\n" 5397 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n" 5398 "}", 5399 Style); 5400 Style.AllowAllArgumentsOnNextLine = false; 5401 verifyFormat("void foo() {\n" 5402 " auto VariableWithReallyLongName = {\n" 5403 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5404 " bbbbbbbbbbbb};\n" 5405 "}", 5406 Style); 5407 } 5408 5409 // This parameter should not affect declarations. 5410 Style.BinPackParameters = false; 5411 Style.AllowAllArgumentsOnNextLine = false; 5412 Style.AllowAllParametersOfDeclarationOnNextLine = true; 5413 verifyFormat("void FunctionCallWithReallyLongName(\n" 5414 " int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);", 5415 Style); 5416 Style.AllowAllParametersOfDeclarationOnNextLine = false; 5417 verifyFormat("void FunctionCallWithReallyLongName(\n" 5418 " int aaaaaaaaaaaaaaaaaaaaaaa,\n" 5419 " int bbbbbbbbbbbb);", 5420 Style); 5421 } 5422 5423 TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) { 5424 // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign 5425 // and BAS_Align. 5426 auto Style = getLLVMStyle(); 5427 Style.ColumnLimit = 35; 5428 StringRef Input = "functionCall(paramA, paramB, paramC);\n" 5429 "void functionDecl(int A, int B, int C);"; 5430 Style.AllowAllArgumentsOnNextLine = false; 5431 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5432 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 5433 " paramC);\n" 5434 "void functionDecl(int A, int B,\n" 5435 " int C);"), 5436 format(Input, Style)); 5437 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 5438 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 5439 " paramC);\n" 5440 "void functionDecl(int A, int B,\n" 5441 " int C);"), 5442 format(Input, Style)); 5443 // However, BAS_AlwaysBreak should take precedence over 5444 // AllowAllArgumentsOnNextLine. 5445 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 5446 EXPECT_EQ(StringRef("functionCall(\n" 5447 " paramA, paramB, paramC);\n" 5448 "void functionDecl(\n" 5449 " int A, int B, int C);"), 5450 format(Input, Style)); 5451 5452 // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the 5453 // first argument. 5454 Style.AllowAllArgumentsOnNextLine = true; 5455 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 5456 EXPECT_EQ(StringRef("functionCall(\n" 5457 " paramA, paramB, paramC);\n" 5458 "void functionDecl(\n" 5459 " int A, int B, int C);"), 5460 format(Input, Style)); 5461 // It wouldn't fit on one line with aligned parameters so this setting 5462 // doesn't change anything for BAS_Align. 5463 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 5464 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 5465 " paramC);\n" 5466 "void functionDecl(int A, int B,\n" 5467 " int C);"), 5468 format(Input, Style)); 5469 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5470 EXPECT_EQ(StringRef("functionCall(\n" 5471 " paramA, paramB, paramC);\n" 5472 "void functionDecl(\n" 5473 " int A, int B, int C);"), 5474 format(Input, Style)); 5475 } 5476 5477 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) { 5478 FormatStyle Style = getLLVMStyle(); 5479 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 5480 5481 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 5482 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}", 5483 getStyleWithColumns(Style, 45)); 5484 verifyFormat("Constructor() :\n" 5485 " Initializer(FitsOnTheLine) {}", 5486 getStyleWithColumns(Style, 44)); 5487 verifyFormat("Constructor() :\n" 5488 " Initializer(FitsOnTheLine) {}", 5489 getStyleWithColumns(Style, 43)); 5490 5491 verifyFormat("template <typename T>\n" 5492 "Constructor() : Initializer(FitsOnTheLine) {}", 5493 getStyleWithColumns(Style, 50)); 5494 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 5495 verifyFormat( 5496 "SomeClass::Constructor() :\n" 5497 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 5498 Style); 5499 5500 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = false; 5501 verifyFormat( 5502 "SomeClass::Constructor() :\n" 5503 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 5504 Style); 5505 5506 verifyFormat( 5507 "SomeClass::Constructor() :\n" 5508 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 5509 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 5510 Style); 5511 verifyFormat( 5512 "SomeClass::Constructor() :\n" 5513 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5514 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 5515 Style); 5516 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5517 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 5518 " aaaaaaaaaa(aaaaaa) {}", 5519 Style); 5520 5521 verifyFormat("Constructor() :\n" 5522 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5523 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5524 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5525 " aaaaaaaaaaaaaaaaaaaaaaa() {}", 5526 Style); 5527 5528 verifyFormat("Constructor() :\n" 5529 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5530 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 5531 Style); 5532 5533 verifyFormat("Constructor(int Parameter = 0) :\n" 5534 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 5535 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}", 5536 Style); 5537 verifyFormat("Constructor() :\n" 5538 " aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 5539 "}", 5540 getStyleWithColumns(Style, 60)); 5541 verifyFormat("Constructor() :\n" 5542 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5543 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}", 5544 Style); 5545 5546 // Here a line could be saved by splitting the second initializer onto two 5547 // lines, but that is not desirable. 5548 verifyFormat("Constructor() :\n" 5549 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 5550 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 5551 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 5552 Style); 5553 5554 FormatStyle OnePerLine = Style; 5555 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 5556 OnePerLine.AllowAllConstructorInitializersOnNextLine = false; 5557 verifyFormat("SomeClass::Constructor() :\n" 5558 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 5559 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 5560 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 5561 OnePerLine); 5562 verifyFormat("SomeClass::Constructor() :\n" 5563 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 5564 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 5565 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 5566 OnePerLine); 5567 verifyFormat("MyClass::MyClass(int var) :\n" 5568 " some_var_(var), // 4 space indent\n" 5569 " some_other_var_(var + 1) { // lined up\n" 5570 "}", 5571 OnePerLine); 5572 verifyFormat("Constructor() :\n" 5573 " aaaaa(aaaaaa),\n" 5574 " aaaaa(aaaaaa),\n" 5575 " aaaaa(aaaaaa),\n" 5576 " aaaaa(aaaaaa),\n" 5577 " aaaaa(aaaaaa) {}", 5578 OnePerLine); 5579 verifyFormat("Constructor() :\n" 5580 " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 5581 " aaaaaaaaaaaaaaaaaaaaaa) {}", 5582 OnePerLine); 5583 OnePerLine.BinPackParameters = false; 5584 verifyFormat("Constructor() :\n" 5585 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 5586 " aaaaaaaaaaa().aaa(),\n" 5587 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 5588 OnePerLine); 5589 OnePerLine.ColumnLimit = 60; 5590 verifyFormat("Constructor() :\n" 5591 " aaaaaaaaaaaaaaaaaaaa(a),\n" 5592 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 5593 OnePerLine); 5594 5595 EXPECT_EQ("Constructor() :\n" 5596 " // Comment forcing unwanted break.\n" 5597 " aaaa(aaaa) {}", 5598 format("Constructor() :\n" 5599 " // Comment forcing unwanted break.\n" 5600 " aaaa(aaaa) {}", 5601 Style)); 5602 5603 Style.ColumnLimit = 0; 5604 verifyFormat("SomeClass::Constructor() :\n" 5605 " a(a) {}", 5606 Style); 5607 verifyFormat("SomeClass::Constructor() noexcept :\n" 5608 " a(a) {}", 5609 Style); 5610 verifyFormat("SomeClass::Constructor() :\n" 5611 " a(a), b(b), c(c) {}", 5612 Style); 5613 verifyFormat("SomeClass::Constructor() :\n" 5614 " a(a) {\n" 5615 " foo();\n" 5616 " bar();\n" 5617 "}", 5618 Style); 5619 5620 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 5621 verifyFormat("SomeClass::Constructor() :\n" 5622 " a(a), b(b), c(c) {\n" 5623 "}", 5624 Style); 5625 verifyFormat("SomeClass::Constructor() :\n" 5626 " a(a) {\n" 5627 "}", 5628 Style); 5629 5630 Style.ColumnLimit = 80; 5631 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 5632 Style.ConstructorInitializerIndentWidth = 2; 5633 verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style); 5634 verifyFormat("SomeClass::Constructor() :\n" 5635 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5636 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}", 5637 Style); 5638 5639 // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as 5640 // well 5641 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 5642 verifyFormat( 5643 "class SomeClass\n" 5644 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5645 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 5646 Style); 5647 Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 5648 verifyFormat( 5649 "class SomeClass\n" 5650 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5651 " , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 5652 Style); 5653 Style.BreakInheritanceList = FormatStyle::BILS_AfterColon; 5654 verifyFormat( 5655 "class SomeClass :\n" 5656 " public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5657 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 5658 Style); 5659 Style.BreakInheritanceList = FormatStyle::BILS_AfterComma; 5660 verifyFormat( 5661 "class SomeClass\n" 5662 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5663 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 5664 Style); 5665 } 5666 5667 #ifndef EXPENSIVE_CHECKS 5668 // Expensive checks enables libstdc++ checking which includes validating the 5669 // state of ranges used in std::priority_queue - this blows out the 5670 // runtime/scalability of the function and makes this test unacceptably slow. 5671 TEST_F(FormatTest, MemoizationTests) { 5672 // This breaks if the memoization lookup does not take \c Indent and 5673 // \c LastSpace into account. 5674 verifyFormat( 5675 "extern CFRunLoopTimerRef\n" 5676 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 5677 " CFTimeInterval interval, CFOptionFlags flags,\n" 5678 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 5679 " CFRunLoopTimerContext *context) {}"); 5680 5681 // Deep nesting somewhat works around our memoization. 5682 verifyFormat( 5683 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 5684 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 5685 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 5686 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 5687 " aaaaa())))))))))))))))))))))))))))))))))))))));", 5688 getLLVMStyleWithColumns(65)); 5689 verifyFormat( 5690 "aaaaa(\n" 5691 " aaaaa,\n" 5692 " aaaaa(\n" 5693 " aaaaa,\n" 5694 " aaaaa(\n" 5695 " aaaaa,\n" 5696 " aaaaa(\n" 5697 " aaaaa,\n" 5698 " aaaaa(\n" 5699 " aaaaa,\n" 5700 " aaaaa(\n" 5701 " aaaaa,\n" 5702 " aaaaa(\n" 5703 " aaaaa,\n" 5704 " aaaaa(\n" 5705 " aaaaa,\n" 5706 " aaaaa(\n" 5707 " aaaaa,\n" 5708 " aaaaa(\n" 5709 " aaaaa,\n" 5710 " aaaaa(\n" 5711 " aaaaa,\n" 5712 " aaaaa(\n" 5713 " aaaaa,\n" 5714 " aaaaa))))))))))));", 5715 getLLVMStyleWithColumns(65)); 5716 verifyFormat( 5717 "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" 5718 " a),\n" 5719 " a),\n" 5720 " a),\n" 5721 " a),\n" 5722 " a),\n" 5723 " a),\n" 5724 " a),\n" 5725 " a),\n" 5726 " a),\n" 5727 " a),\n" 5728 " a),\n" 5729 " a),\n" 5730 " a),\n" 5731 " a),\n" 5732 " a),\n" 5733 " a),\n" 5734 " a)", 5735 getLLVMStyleWithColumns(65)); 5736 5737 // This test takes VERY long when memoization is broken. 5738 FormatStyle OnePerLine = getLLVMStyle(); 5739 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 5740 OnePerLine.BinPackParameters = false; 5741 std::string input = "Constructor()\n" 5742 " : aaaa(a,\n"; 5743 for (unsigned i = 0, e = 80; i != e; ++i) { 5744 input += " a,\n"; 5745 } 5746 input += " a) {}"; 5747 verifyFormat(input, OnePerLine); 5748 } 5749 #endif 5750 5751 TEST_F(FormatTest, BreaksAsHighAsPossible) { 5752 verifyFormat( 5753 "void f() {\n" 5754 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 5755 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 5756 " f();\n" 5757 "}"); 5758 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 5759 " Intervals[i - 1].getRange().getLast()) {\n}"); 5760 } 5761 5762 TEST_F(FormatTest, BreaksFunctionDeclarations) { 5763 // Principially, we break function declarations in a certain order: 5764 // 1) break amongst arguments. 5765 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 5766 " Cccccccccccccc cccccccccccccc);"); 5767 verifyFormat("template <class TemplateIt>\n" 5768 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 5769 " TemplateIt *stop) {}"); 5770 5771 // 2) break after return type. 5772 verifyFormat( 5773 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5774 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", 5775 getGoogleStyle()); 5776 5777 // 3) break after (. 5778 verifyFormat( 5779 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 5780 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", 5781 getGoogleStyle()); 5782 5783 // 4) break before after nested name specifiers. 5784 verifyFormat( 5785 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5786 "SomeClasssssssssssssssssssssssssssssssssssssss::\n" 5787 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", 5788 getGoogleStyle()); 5789 5790 // However, there are exceptions, if a sufficient amount of lines can be 5791 // saved. 5792 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 5793 // more adjusting. 5794 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 5795 " Cccccccccccccc cccccccccc,\n" 5796 " Cccccccccccccc cccccccccc,\n" 5797 " Cccccccccccccc cccccccccc,\n" 5798 " Cccccccccccccc cccccccccc);"); 5799 verifyFormat( 5800 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5801 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 5802 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 5803 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", 5804 getGoogleStyle()); 5805 verifyFormat( 5806 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 5807 " Cccccccccccccc cccccccccc,\n" 5808 " Cccccccccccccc cccccccccc,\n" 5809 " Cccccccccccccc cccccccccc,\n" 5810 " Cccccccccccccc cccccccccc,\n" 5811 " Cccccccccccccc cccccccccc,\n" 5812 " Cccccccccccccc cccccccccc);"); 5813 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 5814 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 5815 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 5816 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 5817 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 5818 5819 // Break after multi-line parameters. 5820 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5821 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5822 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5823 " bbbb bbbb);"); 5824 verifyFormat("void SomeLoooooooooooongFunction(\n" 5825 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 5826 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5827 " int bbbbbbbbbbbbb);"); 5828 5829 // Treat overloaded operators like other functions. 5830 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 5831 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 5832 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 5833 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 5834 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 5835 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 5836 verifyGoogleFormat( 5837 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 5838 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 5839 verifyGoogleFormat( 5840 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 5841 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 5842 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5843 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 5844 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 5845 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 5846 verifyGoogleFormat( 5847 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" 5848 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5849 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); 5850 verifyGoogleFormat("template <typename T>\n" 5851 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5852 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n" 5853 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);"); 5854 5855 FormatStyle Style = getLLVMStyle(); 5856 Style.PointerAlignment = FormatStyle::PAS_Left; 5857 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5858 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", 5859 Style); 5860 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 5861 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 5862 Style); 5863 } 5864 5865 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) { 5866 // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516: 5867 // Prefer keeping `::` followed by `operator` together. 5868 EXPECT_EQ("const aaaa::bbbbbbb &\n" 5869 "ccccccccc::operator++() {\n" 5870 " stuff();\n" 5871 "}", 5872 format("const aaaa::bbbbbbb\n" 5873 "&ccccccccc::operator++() { stuff(); }", 5874 getLLVMStyleWithColumns(40))); 5875 } 5876 5877 TEST_F(FormatTest, TrailingReturnType) { 5878 verifyFormat("auto foo() -> int;\n"); 5879 // correct trailing return type spacing 5880 verifyFormat("auto operator->() -> int;\n"); 5881 verifyFormat("auto operator++(int) -> int;\n"); 5882 5883 verifyFormat("struct S {\n" 5884 " auto bar() const -> int;\n" 5885 "};"); 5886 verifyFormat("template <size_t Order, typename T>\n" 5887 "auto load_img(const std::string &filename)\n" 5888 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 5889 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 5890 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 5891 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 5892 verifyFormat("template <typename T>\n" 5893 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" 5894 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); 5895 5896 // Not trailing return types. 5897 verifyFormat("void f() { auto a = b->c(); }"); 5898 } 5899 5900 TEST_F(FormatTest, DeductionGuides) { 5901 verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;"); 5902 verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;"); 5903 verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;"); 5904 verifyFormat( 5905 "template <class... T>\n" 5906 "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;"); 5907 verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;"); 5908 verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;"); 5909 verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;"); 5910 verifyFormat("template <class T> A() -> A<(3 < 2)>;"); 5911 verifyFormat("template <class T> A() -> A<((3) < (2))>;"); 5912 verifyFormat("template <class T> x() -> x<1>;"); 5913 verifyFormat("template <class T> explicit x(T &) -> x<1>;"); 5914 5915 // Ensure not deduction guides. 5916 verifyFormat("c()->f<int>();"); 5917 verifyFormat("x()->foo<1>;"); 5918 verifyFormat("x = p->foo<3>();"); 5919 verifyFormat("x()->x<1>();"); 5920 verifyFormat("x()->x<1>;"); 5921 } 5922 5923 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 5924 // Avoid breaking before trailing 'const' or other trailing annotations, if 5925 // they are not function-like. 5926 FormatStyle Style = getGoogleStyle(); 5927 Style.ColumnLimit = 47; 5928 verifyFormat("void someLongFunction(\n" 5929 " int someLoooooooooooooongParameter) const {\n}", 5930 getLLVMStyleWithColumns(47)); 5931 verifyFormat("LoooooongReturnType\n" 5932 "someLoooooooongFunction() const {}", 5933 getLLVMStyleWithColumns(47)); 5934 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 5935 " const {}", 5936 Style); 5937 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 5938 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 5939 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 5940 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 5941 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 5942 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 5943 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 5944 " aaaaaaaaaaa aaaaa) const override;"); 5945 verifyGoogleFormat( 5946 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 5947 " const override;"); 5948 5949 // Even if the first parameter has to be wrapped. 5950 verifyFormat("void someLongFunction(\n" 5951 " int someLongParameter) const {}", 5952 getLLVMStyleWithColumns(46)); 5953 verifyFormat("void someLongFunction(\n" 5954 " int someLongParameter) const {}", 5955 Style); 5956 verifyFormat("void someLongFunction(\n" 5957 " int someLongParameter) override {}", 5958 Style); 5959 verifyFormat("void someLongFunction(\n" 5960 " int someLongParameter) OVERRIDE {}", 5961 Style); 5962 verifyFormat("void someLongFunction(\n" 5963 " int someLongParameter) final {}", 5964 Style); 5965 verifyFormat("void someLongFunction(\n" 5966 " int someLongParameter) FINAL {}", 5967 Style); 5968 verifyFormat("void someLongFunction(\n" 5969 " int parameter) const override {}", 5970 Style); 5971 5972 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 5973 verifyFormat("void someLongFunction(\n" 5974 " int someLongParameter) const\n" 5975 "{\n" 5976 "}", 5977 Style); 5978 5979 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 5980 verifyFormat("void someLongFunction(\n" 5981 " int someLongParameter) const\n" 5982 " {\n" 5983 " }", 5984 Style); 5985 5986 // Unless these are unknown annotations. 5987 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 5988 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5989 " LONG_AND_UGLY_ANNOTATION;"); 5990 5991 // Breaking before function-like trailing annotations is fine to keep them 5992 // close to their arguments. 5993 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5994 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 5995 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 5996 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 5997 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 5998 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 5999 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 6000 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 6001 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 6002 6003 verifyFormat( 6004 "void aaaaaaaaaaaaaaaaaa()\n" 6005 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 6006 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 6007 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6008 " __attribute__((unused));"); 6009 verifyGoogleFormat( 6010 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6011 " GUARDED_BY(aaaaaaaaaaaa);"); 6012 verifyGoogleFormat( 6013 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6014 " GUARDED_BY(aaaaaaaaaaaa);"); 6015 verifyGoogleFormat( 6016 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 6017 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6018 verifyGoogleFormat( 6019 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 6020 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 6021 } 6022 6023 TEST_F(FormatTest, FunctionAnnotations) { 6024 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 6025 "int OldFunction(const string ¶meter) {}"); 6026 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 6027 "string OldFunction(const string ¶meter) {}"); 6028 verifyFormat("template <typename T>\n" 6029 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 6030 "string OldFunction(const string ¶meter) {}"); 6031 6032 // Not function annotations. 6033 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6034 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 6035 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" 6036 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); 6037 verifyFormat("MACRO(abc).function() // wrap\n" 6038 " << abc;"); 6039 verifyFormat("MACRO(abc)->function() // wrap\n" 6040 " << abc;"); 6041 verifyFormat("MACRO(abc)::function() // wrap\n" 6042 " << abc;"); 6043 } 6044 6045 TEST_F(FormatTest, BreaksDesireably) { 6046 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 6047 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 6048 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 6049 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6050 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 6051 "}"); 6052 6053 verifyFormat( 6054 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6055 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 6056 6057 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6058 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6059 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 6060 6061 verifyFormat( 6062 "aaaaaaaa(aaaaaaaaaaaaa,\n" 6063 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6064 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 6065 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6066 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 6067 6068 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 6069 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6070 6071 verifyFormat( 6072 "void f() {\n" 6073 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 6074 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 6075 "}"); 6076 verifyFormat( 6077 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6078 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 6079 verifyFormat( 6080 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6081 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 6082 verifyFormat( 6083 "aaaaaa(aaa,\n" 6084 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6085 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6086 " aaaa);"); 6087 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6088 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6089 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6090 6091 // Indent consistently independent of call expression and unary operator. 6092 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 6093 " dddddddddddddddddddddddddddddd));"); 6094 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 6095 " dddddddddddddddddddddddddddddd));"); 6096 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 6097 " dddddddddddddddddddddddddddddd));"); 6098 6099 // This test case breaks on an incorrect memoization, i.e. an optimization not 6100 // taking into account the StopAt value. 6101 verifyFormat( 6102 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 6103 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 6104 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 6105 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6106 6107 verifyFormat("{\n {\n {\n" 6108 " Annotation.SpaceRequiredBefore =\n" 6109 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 6110 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 6111 " }\n }\n}"); 6112 6113 // Break on an outer level if there was a break on an inner level. 6114 EXPECT_EQ("f(g(h(a, // comment\n" 6115 " b, c),\n" 6116 " d, e),\n" 6117 " x, y);", 6118 format("f(g(h(a, // comment\n" 6119 " b, c), d, e), x, y);")); 6120 6121 // Prefer breaking similar line breaks. 6122 verifyFormat( 6123 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 6124 " NSTrackingMouseEnteredAndExited |\n" 6125 " NSTrackingActiveAlways;"); 6126 } 6127 6128 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 6129 FormatStyle NoBinPacking = getGoogleStyle(); 6130 NoBinPacking.BinPackParameters = false; 6131 NoBinPacking.BinPackArguments = true; 6132 verifyFormat("void f() {\n" 6133 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 6134 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 6135 "}", 6136 NoBinPacking); 6137 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 6138 " int aaaaaaaaaaaaaaaaaaaa,\n" 6139 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6140 NoBinPacking); 6141 6142 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 6143 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6144 " vector<int> bbbbbbbbbbbbbbb);", 6145 NoBinPacking); 6146 // FIXME: This behavior difference is probably not wanted. However, currently 6147 // we cannot distinguish BreakBeforeParameter being set because of the wrapped 6148 // template arguments from BreakBeforeParameter being set because of the 6149 // one-per-line formatting. 6150 verifyFormat( 6151 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n" 6152 " aaaaaaaaaa> aaaaaaaaaa);", 6153 NoBinPacking); 6154 verifyFormat( 6155 "void fffffffffff(\n" 6156 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n" 6157 " aaaaaaaaaa);"); 6158 } 6159 6160 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 6161 FormatStyle NoBinPacking = getGoogleStyle(); 6162 NoBinPacking.BinPackParameters = false; 6163 NoBinPacking.BinPackArguments = false; 6164 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 6165 " aaaaaaaaaaaaaaaaaaaa,\n" 6166 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 6167 NoBinPacking); 6168 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 6169 " aaaaaaaaaaaaa,\n" 6170 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 6171 NoBinPacking); 6172 verifyFormat( 6173 "aaaaaaaa(aaaaaaaaaaaaa,\n" 6174 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6175 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 6176 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6177 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 6178 NoBinPacking); 6179 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 6180 " .aaaaaaaaaaaaaaaaaa();", 6181 NoBinPacking); 6182 verifyFormat("void f() {\n" 6183 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6184 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 6185 "}", 6186 NoBinPacking); 6187 6188 verifyFormat( 6189 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6190 " aaaaaaaaaaaa,\n" 6191 " aaaaaaaaaaaa);", 6192 NoBinPacking); 6193 verifyFormat( 6194 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 6195 " ddddddddddddddddddddddddddddd),\n" 6196 " test);", 6197 NoBinPacking); 6198 6199 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 6200 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 6201 " aaaaaaaaaaaaaaaaaaaaaaa>\n" 6202 " aaaaaaaaaaaaaaaaaa;", 6203 NoBinPacking); 6204 verifyFormat("a(\"a\"\n" 6205 " \"a\",\n" 6206 " a);"); 6207 6208 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 6209 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 6210 " aaaaaaaaa,\n" 6211 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 6212 NoBinPacking); 6213 verifyFormat( 6214 "void f() {\n" 6215 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 6216 " .aaaaaaa();\n" 6217 "}", 6218 NoBinPacking); 6219 verifyFormat( 6220 "template <class SomeType, class SomeOtherType>\n" 6221 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 6222 NoBinPacking); 6223 } 6224 6225 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 6226 FormatStyle Style = getLLVMStyleWithColumns(15); 6227 Style.ExperimentalAutoDetectBinPacking = true; 6228 EXPECT_EQ("aaa(aaaa,\n" 6229 " aaaa,\n" 6230 " aaaa);\n" 6231 "aaa(aaaa,\n" 6232 " aaaa,\n" 6233 " aaaa);", 6234 format("aaa(aaaa,\n" // one-per-line 6235 " aaaa,\n" 6236 " aaaa );\n" 6237 "aaa(aaaa, aaaa, aaaa);", // inconclusive 6238 Style)); 6239 EXPECT_EQ("aaa(aaaa, aaaa,\n" 6240 " aaaa);\n" 6241 "aaa(aaaa, aaaa,\n" 6242 " aaaa);", 6243 format("aaa(aaaa, aaaa,\n" // bin-packed 6244 " aaaa );\n" 6245 "aaa(aaaa, aaaa, aaaa);", // inconclusive 6246 Style)); 6247 } 6248 6249 TEST_F(FormatTest, FormatsBuilderPattern) { 6250 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 6251 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 6252 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 6253 " .StartsWith(\".init\", ORDER_INIT)\n" 6254 " .StartsWith(\".fini\", ORDER_FINI)\n" 6255 " .StartsWith(\".hash\", ORDER_HASH)\n" 6256 " .Default(ORDER_TEXT);\n"); 6257 6258 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 6259 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 6260 verifyFormat("aaaaaaa->aaaaaaa\n" 6261 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6262 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6263 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 6264 verifyFormat( 6265 "aaaaaaa->aaaaaaa\n" 6266 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6267 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 6268 verifyFormat( 6269 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 6270 " aaaaaaaaaaaaaa);"); 6271 verifyFormat( 6272 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 6273 " aaaaaa->aaaaaaaaaaaa()\n" 6274 " ->aaaaaaaaaaaaaaaa(\n" 6275 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6276 " ->aaaaaaaaaaaaaaaaa();"); 6277 verifyGoogleFormat( 6278 "void f() {\n" 6279 " someo->Add((new util::filetools::Handler(dir))\n" 6280 " ->OnEvent1(NewPermanentCallback(\n" 6281 " this, &HandlerHolderClass::EventHandlerCBA))\n" 6282 " ->OnEvent2(NewPermanentCallback(\n" 6283 " this, &HandlerHolderClass::EventHandlerCBB))\n" 6284 " ->OnEvent3(NewPermanentCallback(\n" 6285 " this, &HandlerHolderClass::EventHandlerCBC))\n" 6286 " ->OnEvent5(NewPermanentCallback(\n" 6287 " this, &HandlerHolderClass::EventHandlerCBD))\n" 6288 " ->OnEvent6(NewPermanentCallback(\n" 6289 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 6290 "}"); 6291 6292 verifyFormat( 6293 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 6294 verifyFormat("aaaaaaaaaaaaaaa()\n" 6295 " .aaaaaaaaaaaaaaa()\n" 6296 " .aaaaaaaaaaaaaaa()\n" 6297 " .aaaaaaaaaaaaaaa()\n" 6298 " .aaaaaaaaaaaaaaa();"); 6299 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 6300 " .aaaaaaaaaaaaaaa()\n" 6301 " .aaaaaaaaaaaaaaa()\n" 6302 " .aaaaaaaaaaaaaaa();"); 6303 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 6304 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 6305 " .aaaaaaaaaaaaaaa();"); 6306 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 6307 " ->aaaaaaaaaaaaaae(0)\n" 6308 " ->aaaaaaaaaaaaaaa();"); 6309 6310 // Don't linewrap after very short segments. 6311 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 6312 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 6313 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 6314 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 6315 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 6316 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 6317 verifyFormat("aaa()\n" 6318 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 6319 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 6320 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 6321 6322 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 6323 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 6324 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 6325 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 6326 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 6327 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 6328 6329 // Prefer not to break after empty parentheses. 6330 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 6331 " First->LastNewlineOffset);"); 6332 6333 // Prefer not to create "hanging" indents. 6334 verifyFormat( 6335 "return !soooooooooooooome_map\n" 6336 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6337 " .second;"); 6338 verifyFormat( 6339 "return aaaaaaaaaaaaaaaa\n" 6340 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n" 6341 " .aaaa(aaaaaaaaaaaaaa);"); 6342 // No hanging indent here. 6343 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n" 6344 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6345 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n" 6346 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6347 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 6348 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 6349 getLLVMStyleWithColumns(60)); 6350 verifyFormat("aaaaaaaaaaaaaaaaaa\n" 6351 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 6352 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 6353 getLLVMStyleWithColumns(59)); 6354 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6355 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6356 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6357 6358 // Dont break if only closing statements before member call 6359 verifyFormat("test() {\n" 6360 " ([]() -> {\n" 6361 " int b = 32;\n" 6362 " return 3;\n" 6363 " }).foo();\n" 6364 "}"); 6365 verifyFormat("test() {\n" 6366 " (\n" 6367 " []() -> {\n" 6368 " int b = 32;\n" 6369 " return 3;\n" 6370 " },\n" 6371 " foo, bar)\n" 6372 " .foo();\n" 6373 "}"); 6374 verifyFormat("test() {\n" 6375 " ([]() -> {\n" 6376 " int b = 32;\n" 6377 " return 3;\n" 6378 " })\n" 6379 " .foo()\n" 6380 " .bar();\n" 6381 "}"); 6382 verifyFormat("test() {\n" 6383 " ([]() -> {\n" 6384 " int b = 32;\n" 6385 " return 3;\n" 6386 " })\n" 6387 " .foo(\"aaaaaaaaaaaaaaaaa\"\n" 6388 " \"bbbb\");\n" 6389 "}", 6390 getLLVMStyleWithColumns(30)); 6391 } 6392 6393 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 6394 verifyFormat( 6395 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 6396 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 6397 verifyFormat( 6398 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 6399 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 6400 6401 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 6402 " ccccccccccccccccccccccccc) {\n}"); 6403 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 6404 " ccccccccccccccccccccccccc) {\n}"); 6405 6406 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 6407 " ccccccccccccccccccccccccc) {\n}"); 6408 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 6409 " ccccccccccccccccccccccccc) {\n}"); 6410 6411 verifyFormat( 6412 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 6413 " ccccccccccccccccccccccccc) {\n}"); 6414 verifyFormat( 6415 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 6416 " ccccccccccccccccccccccccc) {\n}"); 6417 6418 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 6419 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 6420 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 6421 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 6422 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 6423 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 6424 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 6425 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 6426 6427 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 6428 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 6429 " aaaaaaaaaaaaaaa != aa) {\n}"); 6430 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 6431 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 6432 " aaaaaaaaaaaaaaa != aa) {\n}"); 6433 } 6434 6435 TEST_F(FormatTest, BreaksAfterAssignments) { 6436 verifyFormat( 6437 "unsigned Cost =\n" 6438 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 6439 " SI->getPointerAddressSpaceee());\n"); 6440 verifyFormat( 6441 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 6442 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 6443 6444 verifyFormat( 6445 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 6446 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 6447 verifyFormat("unsigned OriginalStartColumn =\n" 6448 " SourceMgr.getSpellingColumnNumber(\n" 6449 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 6450 " 1;"); 6451 } 6452 6453 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) { 6454 FormatStyle Style = getLLVMStyle(); 6455 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 6456 " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;", 6457 Style); 6458 6459 Style.PenaltyBreakAssignment = 20; 6460 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 6461 " cccccccccccccccccccccccccc;", 6462 Style); 6463 } 6464 6465 TEST_F(FormatTest, AlignsAfterAssignments) { 6466 verifyFormat( 6467 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6468 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 6469 verifyFormat( 6470 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6471 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 6472 verifyFormat( 6473 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6474 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 6475 verifyFormat( 6476 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6477 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 6478 verifyFormat( 6479 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 6480 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 6481 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 6482 } 6483 6484 TEST_F(FormatTest, AlignsAfterReturn) { 6485 verifyFormat( 6486 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6487 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 6488 verifyFormat( 6489 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6490 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 6491 verifyFormat( 6492 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 6493 " aaaaaaaaaaaaaaaaaaaaaa();"); 6494 verifyFormat( 6495 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 6496 " aaaaaaaaaaaaaaaaaaaaaa());"); 6497 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6498 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6499 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6500 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 6501 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6502 verifyFormat("return\n" 6503 " // true if code is one of a or b.\n" 6504 " code == a || code == b;"); 6505 } 6506 6507 TEST_F(FormatTest, AlignsAfterOpenBracket) { 6508 verifyFormat( 6509 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 6510 " aaaaaaaaa aaaaaaa) {}"); 6511 verifyFormat( 6512 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 6513 " aaaaaaaaaaa aaaaaaaaa);"); 6514 verifyFormat( 6515 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 6516 " aaaaaaaaaaaaaaaaaaaaa));"); 6517 FormatStyle Style = getLLVMStyle(); 6518 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6519 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6520 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 6521 Style); 6522 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 6523 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 6524 Style); 6525 verifyFormat("SomeLongVariableName->someFunction(\n" 6526 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 6527 Style); 6528 verifyFormat( 6529 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 6530 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6531 Style); 6532 verifyFormat( 6533 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 6534 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 6535 Style); 6536 verifyFormat( 6537 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 6538 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 6539 Style); 6540 6541 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n" 6542 " ccccccc(aaaaaaaaaaaaaaaaa, //\n" 6543 " b));", 6544 Style); 6545 6546 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 6547 Style.BinPackArguments = false; 6548 Style.BinPackParameters = false; 6549 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6550 " aaaaaaaaaaa aaaaaaaa,\n" 6551 " aaaaaaaaa aaaaaaa,\n" 6552 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6553 Style); 6554 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 6555 " aaaaaaaaaaa aaaaaaaaa,\n" 6556 " aaaaaaaaaaa aaaaaaaaa,\n" 6557 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 6558 Style); 6559 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" 6560 " aaaaaaaaaaaaaaa,\n" 6561 " aaaaaaaaaaaaaaaaaaaaa,\n" 6562 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 6563 Style); 6564 verifyFormat( 6565 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" 6566 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 6567 Style); 6568 verifyFormat( 6569 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" 6570 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 6571 Style); 6572 verifyFormat( 6573 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 6574 " aaaaaaaaaaaaaaaaaaaaa(\n" 6575 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n" 6576 " aaaaaaaaaaaaaaaa);", 6577 Style); 6578 verifyFormat( 6579 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 6580 " aaaaaaaaaaaaaaaaaaaaa(\n" 6581 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n" 6582 " aaaaaaaaaaaaaaaa);", 6583 Style); 6584 } 6585 6586 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 6587 FormatStyle Style = getLLVMStyleWithColumns(40); 6588 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 6589 " bbbbbbbbbbbbbbbbbbbbbb);", 6590 Style); 6591 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 6592 Style.AlignOperands = FormatStyle::OAS_DontAlign; 6593 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 6594 " bbbbbbbbbbbbbbbbbbbbbb);", 6595 Style); 6596 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6597 Style.AlignOperands = FormatStyle::OAS_Align; 6598 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 6599 " bbbbbbbbbbbbbbbbbbbbbb);", 6600 Style); 6601 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6602 Style.AlignOperands = FormatStyle::OAS_DontAlign; 6603 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 6604 " bbbbbbbbbbbbbbbbbbbbbb);", 6605 Style); 6606 } 6607 6608 TEST_F(FormatTest, BreaksConditionalExpressions) { 6609 verifyFormat( 6610 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6611 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6612 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6613 verifyFormat( 6614 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 6615 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6616 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6617 verifyFormat( 6618 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6619 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6620 verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n" 6621 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6622 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6623 verifyFormat( 6624 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 6625 " : aaaaaaaaaaaaa);"); 6626 verifyFormat( 6627 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6628 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6629 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6630 " aaaaaaaaaaaaa);"); 6631 verifyFormat( 6632 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6633 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6634 " aaaaaaaaaaaaa);"); 6635 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6636 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6637 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6638 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6639 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6640 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6641 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6642 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6643 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6644 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6645 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6646 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6647 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6648 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6649 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6650 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6651 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6652 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6653 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6654 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6655 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 6656 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6657 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6658 " : aaaaaaaaaaaaaaaa;"); 6659 verifyFormat( 6660 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6661 " ? aaaaaaaaaaaaaaa\n" 6662 " : aaaaaaaaaaaaaaa;"); 6663 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 6664 " aaaaaaaaa\n" 6665 " ? b\n" 6666 " : c);"); 6667 verifyFormat("return aaaa == bbbb\n" 6668 " // comment\n" 6669 " ? aaaa\n" 6670 " : bbbb;"); 6671 verifyFormat("unsigned Indent =\n" 6672 " format(TheLine.First,\n" 6673 " IndentForLevel[TheLine.Level] >= 0\n" 6674 " ? IndentForLevel[TheLine.Level]\n" 6675 " : TheLine * 2,\n" 6676 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 6677 getLLVMStyleWithColumns(60)); 6678 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 6679 " ? aaaaaaaaaaaaaaa\n" 6680 " : bbbbbbbbbbbbbbb //\n" 6681 " ? ccccccccccccccc\n" 6682 " : ddddddddddddddd;"); 6683 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 6684 " ? aaaaaaaaaaaaaaa\n" 6685 " : (bbbbbbbbbbbbbbb //\n" 6686 " ? ccccccccccccccc\n" 6687 " : ddddddddddddddd);"); 6688 verifyFormat( 6689 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6690 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6691 " aaaaaaaaaaaaaaaaaaaaa +\n" 6692 " aaaaaaaaaaaaaaaaaaaaa\n" 6693 " : aaaaaaaaaa;"); 6694 verifyFormat( 6695 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6696 " : aaaaaaaaaaaaaaaaaaaaaa\n" 6697 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6698 6699 FormatStyle NoBinPacking = getLLVMStyle(); 6700 NoBinPacking.BinPackArguments = false; 6701 verifyFormat( 6702 "void f() {\n" 6703 " g(aaa,\n" 6704 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 6705 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6706 " ? aaaaaaaaaaaaaaa\n" 6707 " : aaaaaaaaaaaaaaa);\n" 6708 "}", 6709 NoBinPacking); 6710 verifyFormat( 6711 "void f() {\n" 6712 " g(aaa,\n" 6713 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 6714 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6715 " ?: aaaaaaaaaaaaaaa);\n" 6716 "}", 6717 NoBinPacking); 6718 6719 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 6720 " // comment.\n" 6721 " ccccccccccccccccccccccccccccccccccccccc\n" 6722 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6723 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 6724 6725 // Assignments in conditional expressions. Apparently not uncommon :-(. 6726 verifyFormat("return a != b\n" 6727 " // comment\n" 6728 " ? a = b\n" 6729 " : a = b;"); 6730 verifyFormat("return a != b\n" 6731 " // comment\n" 6732 " ? a = a != b\n" 6733 " // comment\n" 6734 " ? a = b\n" 6735 " : a\n" 6736 " : a;\n"); 6737 verifyFormat("return a != b\n" 6738 " // comment\n" 6739 " ? a\n" 6740 " : a = a != b\n" 6741 " // comment\n" 6742 " ? a = b\n" 6743 " : a;"); 6744 6745 // Chained conditionals 6746 FormatStyle Style = getLLVMStyle(); 6747 Style.ColumnLimit = 70; 6748 Style.AlignOperands = FormatStyle::OAS_Align; 6749 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 6750 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 6751 " : 3333333333333333;", 6752 Style); 6753 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 6754 " : bbbbbbbbbb ? 2222222222222222\n" 6755 " : 3333333333333333;", 6756 Style); 6757 verifyFormat("return aaaaaaaaaa ? 1111111111111111\n" 6758 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 6759 " : 3333333333333333;", 6760 Style); 6761 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 6762 " : bbbbbbbbbbbbbb ? 222222\n" 6763 " : 333333;", 6764 Style); 6765 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 6766 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 6767 " : cccccccccccccc ? 3333333333333333\n" 6768 " : 4444444444444444;", 6769 Style); 6770 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc)\n" 6771 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 6772 " : 3333333333333333;", 6773 Style); 6774 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 6775 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 6776 " : (aaa ? bbb : ccc);", 6777 Style); 6778 verifyFormat( 6779 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 6780 " : cccccccccccccccccc)\n" 6781 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 6782 " : 3333333333333333;", 6783 Style); 6784 verifyFormat( 6785 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 6786 " : cccccccccccccccccc)\n" 6787 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 6788 " : 3333333333333333;", 6789 Style); 6790 verifyFormat( 6791 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 6792 " : dddddddddddddddddd)\n" 6793 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 6794 " : 3333333333333333;", 6795 Style); 6796 verifyFormat( 6797 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 6798 " : dddddddddddddddddd)\n" 6799 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 6800 " : 3333333333333333;", 6801 Style); 6802 verifyFormat( 6803 "return aaaaaaaaa ? 1111111111111111\n" 6804 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 6805 " : a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 6806 " : dddddddddddddddddd)\n", 6807 Style); 6808 verifyFormat( 6809 "return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 6810 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 6811 " : (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 6812 " : cccccccccccccccccc);", 6813 Style); 6814 verifyFormat( 6815 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 6816 " : ccccccccccccccc ? dddddddddddddddddd\n" 6817 " : eeeeeeeeeeeeeeeeee)\n" 6818 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 6819 " : 3333333333333333;", 6820 Style); 6821 verifyFormat( 6822 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 6823 " : ccccccccccccccc ? dddddddddddddddddd\n" 6824 " : eeeeeeeeeeeeeeeeee)\n" 6825 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 6826 " : 3333333333333333;", 6827 Style); 6828 verifyFormat( 6829 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 6830 " : cccccccccccc ? dddddddddddddddddd\n" 6831 " : eeeeeeeeeeeeeeeeee)\n" 6832 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 6833 " : 3333333333333333;", 6834 Style); 6835 verifyFormat( 6836 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 6837 " : cccccccccccccccccc\n" 6838 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 6839 " : 3333333333333333;", 6840 Style); 6841 verifyFormat( 6842 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 6843 " : cccccccccccccccc ? dddddddddddddddddd\n" 6844 " : eeeeeeeeeeeeeeeeee\n" 6845 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 6846 " : 3333333333333333;", 6847 Style); 6848 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa\n" 6849 " ? (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 6850 " : cccccccccccccccccc ? dddddddddddddddddd\n" 6851 " : eeeeeeeeeeeeeeeeee)\n" 6852 " : bbbbbbbbbbbbbbbbbbb ? 2222222222222222\n" 6853 " : 3333333333333333;", 6854 Style); 6855 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaa\n" 6856 " ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 6857 " : cccccccccccccccc ? dddddddddddddddddd\n" 6858 " : eeeeeeeeeeeeeeeeee\n" 6859 " : bbbbbbbbbbbbbbbbbbbbbbb ? 2222222222222222\n" 6860 " : 3333333333333333;", 6861 Style); 6862 6863 Style.AlignOperands = FormatStyle::OAS_DontAlign; 6864 Style.BreakBeforeTernaryOperators = false; 6865 // FIXME: Aligning the question marks is weird given DontAlign. 6866 // Consider disabling this alignment in this case. Also check whether this 6867 // will render the adjustment from https://reviews.llvm.org/D82199 6868 // unnecessary. 6869 verifyFormat("int x = aaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa :\n" 6870 " bbbb ? cccccccccccccccccc :\n" 6871 " ddddd;\n", 6872 Style); 6873 6874 EXPECT_EQ( 6875 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n" 6876 " /*\n" 6877 " */\n" 6878 " function() {\n" 6879 " try {\n" 6880 " return JJJJJJJJJJJJJJ(\n" 6881 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n" 6882 " }\n" 6883 " } :\n" 6884 " function() {};", 6885 format( 6886 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n" 6887 " /*\n" 6888 " */\n" 6889 " function() {\n" 6890 " try {\n" 6891 " return JJJJJJJJJJJJJJ(\n" 6892 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n" 6893 " }\n" 6894 " } :\n" 6895 " function() {};", 6896 getGoogleStyle(FormatStyle::LK_JavaScript))); 6897 } 6898 6899 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 6900 FormatStyle Style = getLLVMStyle(); 6901 Style.BreakBeforeTernaryOperators = false; 6902 Style.ColumnLimit = 70; 6903 verifyFormat( 6904 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 6905 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 6906 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 6907 Style); 6908 verifyFormat( 6909 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 6910 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 6911 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 6912 Style); 6913 verifyFormat( 6914 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 6915 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 6916 Style); 6917 verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n" 6918 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 6919 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 6920 Style); 6921 verifyFormat( 6922 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 6923 " aaaaaaaaaaaaa);", 6924 Style); 6925 verifyFormat( 6926 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6927 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 6928 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6929 " aaaaaaaaaaaaa);", 6930 Style); 6931 verifyFormat( 6932 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6933 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6934 " aaaaaaaaaaaaa);", 6935 Style); 6936 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 6937 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6938 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 6939 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6940 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 6941 Style); 6942 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6943 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 6944 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6945 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 6946 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6947 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6948 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 6949 Style); 6950 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6951 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 6952 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6953 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6954 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 6955 Style); 6956 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 6957 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 6958 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 6959 Style); 6960 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 6961 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 6962 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 6963 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 6964 Style); 6965 verifyFormat( 6966 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 6967 " aaaaaaaaaaaaaaa :\n" 6968 " aaaaaaaaaaaaaaa;", 6969 Style); 6970 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 6971 " aaaaaaaaa ?\n" 6972 " b :\n" 6973 " c);", 6974 Style); 6975 verifyFormat("unsigned Indent =\n" 6976 " format(TheLine.First,\n" 6977 " IndentForLevel[TheLine.Level] >= 0 ?\n" 6978 " IndentForLevel[TheLine.Level] :\n" 6979 " TheLine * 2,\n" 6980 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 6981 Style); 6982 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 6983 " aaaaaaaaaaaaaaa :\n" 6984 " bbbbbbbbbbbbbbb ? //\n" 6985 " ccccccccccccccc :\n" 6986 " ddddddddddddddd;", 6987 Style); 6988 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 6989 " aaaaaaaaaaaaaaa :\n" 6990 " (bbbbbbbbbbbbbbb ? //\n" 6991 " ccccccccccccccc :\n" 6992 " ddddddddddddddd);", 6993 Style); 6994 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 6995 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n" 6996 " ccccccccccccccccccccccccccc;", 6997 Style); 6998 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 6999 " aaaaa :\n" 7000 " bbbbbbbbbbbbbbb + cccccccccccccccc;", 7001 Style); 7002 7003 // Chained conditionals 7004 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7005 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7006 " 3333333333333333;", 7007 Style); 7008 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7009 " bbbbbbbbbb ? 2222222222222222 :\n" 7010 " 3333333333333333;", 7011 Style); 7012 verifyFormat("return aaaaaaaaaa ? 1111111111111111 :\n" 7013 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7014 " 3333333333333333;", 7015 Style); 7016 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7017 " bbbbbbbbbbbbbbbb ? 222222 :\n" 7018 " 333333;", 7019 Style); 7020 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7021 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7022 " cccccccccccccccc ? 3333333333333333 :\n" 7023 " 4444444444444444;", 7024 Style); 7025 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc) :\n" 7026 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7027 " 3333333333333333;", 7028 Style); 7029 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7030 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7031 " (aaa ? bbb : ccc);", 7032 Style); 7033 verifyFormat( 7034 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7035 " cccccccccccccccccc) :\n" 7036 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7037 " 3333333333333333;", 7038 Style); 7039 verifyFormat( 7040 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7041 " cccccccccccccccccc) :\n" 7042 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7043 " 3333333333333333;", 7044 Style); 7045 verifyFormat( 7046 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7047 " dddddddddddddddddd) :\n" 7048 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7049 " 3333333333333333;", 7050 Style); 7051 verifyFormat( 7052 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7053 " dddddddddddddddddd) :\n" 7054 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7055 " 3333333333333333;", 7056 Style); 7057 verifyFormat( 7058 "return aaaaaaaaa ? 1111111111111111 :\n" 7059 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7060 " a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7061 " dddddddddddddddddd)\n", 7062 Style); 7063 verifyFormat( 7064 "return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7065 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7066 " (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7067 " cccccccccccccccccc);", 7068 Style); 7069 verifyFormat( 7070 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7071 " ccccccccccccccccc ? dddddddddddddddddd :\n" 7072 " eeeeeeeeeeeeeeeeee) :\n" 7073 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7074 " 3333333333333333;", 7075 Style); 7076 verifyFormat( 7077 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7078 " ccccccccccccc ? dddddddddddddddddd :\n" 7079 " eeeeeeeeeeeeeeeeee) :\n" 7080 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7081 " 3333333333333333;", 7082 Style); 7083 verifyFormat( 7084 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7085 " ccccccccccccccccc ? dddddddddddddddddd :\n" 7086 " eeeeeeeeeeeeeeeeee) :\n" 7087 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7088 " 3333333333333333;", 7089 Style); 7090 verifyFormat( 7091 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7092 " cccccccccccccccccc :\n" 7093 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7094 " 3333333333333333;", 7095 Style); 7096 verifyFormat( 7097 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7098 " cccccccccccccccccc ? dddddddddddddddddd :\n" 7099 " eeeeeeeeeeeeeeeeee :\n" 7100 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7101 " 3333333333333333;", 7102 Style); 7103 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n" 7104 " (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7105 " cccccccccccccccccc ? dddddddddddddddddd :\n" 7106 " eeeeeeeeeeeeeeeeee) :\n" 7107 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7108 " 3333333333333333;", 7109 Style); 7110 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n" 7111 " aaaaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7112 " cccccccccccccccccccc ? dddddddddddddddddd :\n" 7113 " eeeeeeeeeeeeeeeeee :\n" 7114 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7115 " 3333333333333333;", 7116 Style); 7117 } 7118 7119 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 7120 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 7121 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 7122 verifyFormat("bool a = true, b = false;"); 7123 7124 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 7125 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 7126 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 7127 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 7128 verifyFormat( 7129 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 7130 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 7131 " d = e && f;"); 7132 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 7133 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 7134 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 7135 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 7136 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 7137 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 7138 7139 FormatStyle Style = getGoogleStyle(); 7140 Style.PointerAlignment = FormatStyle::PAS_Left; 7141 Style.DerivePointerAlignment = false; 7142 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7143 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 7144 " *b = bbbbbbbbbbbbbbbbbbb;", 7145 Style); 7146 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 7147 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 7148 Style); 7149 verifyFormat("vector<int*> a, b;", Style); 7150 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style); 7151 } 7152 7153 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 7154 verifyFormat("arr[foo ? bar : baz];"); 7155 verifyFormat("f()[foo ? bar : baz];"); 7156 verifyFormat("(a + b)[foo ? bar : baz];"); 7157 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 7158 } 7159 7160 TEST_F(FormatTest, AlignsStringLiterals) { 7161 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 7162 " \"short literal\");"); 7163 verifyFormat( 7164 "looooooooooooooooooooooooongFunction(\n" 7165 " \"short literal\"\n" 7166 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 7167 verifyFormat("someFunction(\"Always break between multi-line\"\n" 7168 " \" string literals\",\n" 7169 " and, other, parameters);"); 7170 EXPECT_EQ("fun + \"1243\" /* comment */\n" 7171 " \"5678\";", 7172 format("fun + \"1243\" /* comment */\n" 7173 " \"5678\";", 7174 getLLVMStyleWithColumns(28))); 7175 EXPECT_EQ( 7176 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7177 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 7178 " \"aaaaaaaaaaaaaaaa\";", 7179 format("aaaaaa =" 7180 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 7181 "aaaaaaaaaaaaaaaaaaaaa\" " 7182 "\"aaaaaaaaaaaaaaaa\";")); 7183 verifyFormat("a = a + \"a\"\n" 7184 " \"a\"\n" 7185 " \"a\";"); 7186 verifyFormat("f(\"a\", \"b\"\n" 7187 " \"c\");"); 7188 7189 verifyFormat( 7190 "#define LL_FORMAT \"ll\"\n" 7191 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 7192 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 7193 7194 verifyFormat("#define A(X) \\\n" 7195 " \"aaaaa\" #X \"bbbbbb\" \\\n" 7196 " \"ccccc\"", 7197 getLLVMStyleWithColumns(23)); 7198 verifyFormat("#define A \"def\"\n" 7199 "f(\"abc\" A \"ghi\"\n" 7200 " \"jkl\");"); 7201 7202 verifyFormat("f(L\"a\"\n" 7203 " L\"b\");"); 7204 verifyFormat("#define A(X) \\\n" 7205 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 7206 " L\"ccccc\"", 7207 getLLVMStyleWithColumns(25)); 7208 7209 verifyFormat("f(@\"a\"\n" 7210 " @\"b\");"); 7211 verifyFormat("NSString s = @\"a\"\n" 7212 " @\"b\"\n" 7213 " @\"c\";"); 7214 verifyFormat("NSString s = @\"a\"\n" 7215 " \"b\"\n" 7216 " \"c\";"); 7217 } 7218 7219 TEST_F(FormatTest, ReturnTypeBreakingStyle) { 7220 FormatStyle Style = getLLVMStyle(); 7221 // No declarations or definitions should be moved to own line. 7222 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; 7223 verifyFormat("class A {\n" 7224 " int f() { return 1; }\n" 7225 " int g();\n" 7226 "};\n" 7227 "int f() { return 1; }\n" 7228 "int g();\n", 7229 Style); 7230 7231 // All declarations and definitions should have the return type moved to its 7232 // own line. 7233 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 7234 Style.TypenameMacros = {"LIST"}; 7235 verifyFormat("SomeType\n" 7236 "funcdecl(LIST(uint64_t));", 7237 Style); 7238 verifyFormat("class E {\n" 7239 " int\n" 7240 " f() {\n" 7241 " return 1;\n" 7242 " }\n" 7243 " int\n" 7244 " g();\n" 7245 "};\n" 7246 "int\n" 7247 "f() {\n" 7248 " return 1;\n" 7249 "}\n" 7250 "int\n" 7251 "g();\n", 7252 Style); 7253 7254 // Top-level definitions, and no kinds of declarations should have the 7255 // return type moved to its own line. 7256 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; 7257 verifyFormat("class B {\n" 7258 " int f() { return 1; }\n" 7259 " int g();\n" 7260 "};\n" 7261 "int\n" 7262 "f() {\n" 7263 " return 1;\n" 7264 "}\n" 7265 "int g();\n", 7266 Style); 7267 7268 // Top-level definitions and declarations should have the return type moved 7269 // to its own line. 7270 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel; 7271 verifyFormat("class C {\n" 7272 " int f() { return 1; }\n" 7273 " int g();\n" 7274 "};\n" 7275 "int\n" 7276 "f() {\n" 7277 " return 1;\n" 7278 "}\n" 7279 "int\n" 7280 "g();\n", 7281 Style); 7282 7283 // All definitions should have the return type moved to its own line, but no 7284 // kinds of declarations. 7285 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 7286 verifyFormat("class D {\n" 7287 " int\n" 7288 " f() {\n" 7289 " return 1;\n" 7290 " }\n" 7291 " int g();\n" 7292 "};\n" 7293 "int\n" 7294 "f() {\n" 7295 " return 1;\n" 7296 "}\n" 7297 "int g();\n", 7298 Style); 7299 verifyFormat("const char *\n" 7300 "f(void) {\n" // Break here. 7301 " return \"\";\n" 7302 "}\n" 7303 "const char *bar(void);\n", // No break here. 7304 Style); 7305 verifyFormat("template <class T>\n" 7306 "T *\n" 7307 "f(T &c) {\n" // Break here. 7308 " return NULL;\n" 7309 "}\n" 7310 "template <class T> T *f(T &c);\n", // No break here. 7311 Style); 7312 verifyFormat("class C {\n" 7313 " int\n" 7314 " operator+() {\n" 7315 " return 1;\n" 7316 " }\n" 7317 " int\n" 7318 " operator()() {\n" 7319 " return 1;\n" 7320 " }\n" 7321 "};\n", 7322 Style); 7323 verifyFormat("void\n" 7324 "A::operator()() {}\n" 7325 "void\n" 7326 "A::operator>>() {}\n" 7327 "void\n" 7328 "A::operator+() {}\n" 7329 "void\n" 7330 "A::operator*() {}\n" 7331 "void\n" 7332 "A::operator->() {}\n" 7333 "void\n" 7334 "A::operator void *() {}\n" 7335 "void\n" 7336 "A::operator void &() {}\n" 7337 "void\n" 7338 "A::operator void &&() {}\n" 7339 "void\n" 7340 "A::operator char *() {}\n" 7341 "void\n" 7342 "A::operator[]() {}\n" 7343 "void\n" 7344 "A::operator!() {}\n" 7345 "void\n" 7346 "A::operator**() {}\n" 7347 "void\n" 7348 "A::operator<Foo> *() {}\n" 7349 "void\n" 7350 "A::operator<Foo> **() {}\n" 7351 "void\n" 7352 "A::operator<Foo> &() {}\n" 7353 "void\n" 7354 "A::operator void **() {}\n", 7355 Style); 7356 verifyFormat("constexpr auto\n" 7357 "operator()() const -> reference {}\n" 7358 "constexpr auto\n" 7359 "operator>>() const -> reference {}\n" 7360 "constexpr auto\n" 7361 "operator+() const -> reference {}\n" 7362 "constexpr auto\n" 7363 "operator*() const -> reference {}\n" 7364 "constexpr auto\n" 7365 "operator->() const -> reference {}\n" 7366 "constexpr auto\n" 7367 "operator++() const -> reference {}\n" 7368 "constexpr auto\n" 7369 "operator void *() const -> reference {}\n" 7370 "constexpr auto\n" 7371 "operator void **() const -> reference {}\n" 7372 "constexpr auto\n" 7373 "operator void *() const -> reference {}\n" 7374 "constexpr auto\n" 7375 "operator void &() const -> reference {}\n" 7376 "constexpr auto\n" 7377 "operator void &&() const -> reference {}\n" 7378 "constexpr auto\n" 7379 "operator char *() const -> reference {}\n" 7380 "constexpr auto\n" 7381 "operator!() const -> reference {}\n" 7382 "constexpr auto\n" 7383 "operator[]() const -> reference {}\n", 7384 Style); 7385 verifyFormat("void *operator new(std::size_t s);", // No break here. 7386 Style); 7387 verifyFormat("void *\n" 7388 "operator new(std::size_t s) {}", 7389 Style); 7390 verifyFormat("void *\n" 7391 "operator delete[](void *ptr) {}", 7392 Style); 7393 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 7394 verifyFormat("const char *\n" 7395 "f(void)\n" // Break here. 7396 "{\n" 7397 " return \"\";\n" 7398 "}\n" 7399 "const char *bar(void);\n", // No break here. 7400 Style); 7401 verifyFormat("template <class T>\n" 7402 "T *\n" // Problem here: no line break 7403 "f(T &c)\n" // Break here. 7404 "{\n" 7405 " return NULL;\n" 7406 "}\n" 7407 "template <class T> T *f(T &c);\n", // No break here. 7408 Style); 7409 verifyFormat("int\n" 7410 "foo(A<bool> a)\n" 7411 "{\n" 7412 " return a;\n" 7413 "}\n", 7414 Style); 7415 verifyFormat("int\n" 7416 "foo(A<8> a)\n" 7417 "{\n" 7418 " return a;\n" 7419 "}\n", 7420 Style); 7421 verifyFormat("int\n" 7422 "foo(A<B<bool>, 8> a)\n" 7423 "{\n" 7424 " return a;\n" 7425 "}\n", 7426 Style); 7427 verifyFormat("int\n" 7428 "foo(A<B<8>, bool> a)\n" 7429 "{\n" 7430 " return a;\n" 7431 "}\n", 7432 Style); 7433 verifyFormat("int\n" 7434 "foo(A<B<bool>, bool> a)\n" 7435 "{\n" 7436 " return a;\n" 7437 "}\n", 7438 Style); 7439 verifyFormat("int\n" 7440 "foo(A<B<8>, 8> a)\n" 7441 "{\n" 7442 " return a;\n" 7443 "}\n", 7444 Style); 7445 7446 Style = getGNUStyle(); 7447 7448 // Test for comments at the end of function declarations. 7449 verifyFormat("void\n" 7450 "foo (int a, /*abc*/ int b) // def\n" 7451 "{\n" 7452 "}\n", 7453 Style); 7454 7455 verifyFormat("void\n" 7456 "foo (int a, /* abc */ int b) /* def */\n" 7457 "{\n" 7458 "}\n", 7459 Style); 7460 7461 // Definitions that should not break after return type 7462 verifyFormat("void foo (int a, int b); // def\n", Style); 7463 verifyFormat("void foo (int a, int b); /* def */\n", Style); 7464 verifyFormat("void foo (int a, int b);\n", Style); 7465 } 7466 7467 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 7468 FormatStyle NoBreak = getLLVMStyle(); 7469 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 7470 FormatStyle Break = getLLVMStyle(); 7471 Break.AlwaysBreakBeforeMultilineStrings = true; 7472 verifyFormat("aaaa = \"bbbb\"\n" 7473 " \"cccc\";", 7474 NoBreak); 7475 verifyFormat("aaaa =\n" 7476 " \"bbbb\"\n" 7477 " \"cccc\";", 7478 Break); 7479 verifyFormat("aaaa(\"bbbb\"\n" 7480 " \"cccc\");", 7481 NoBreak); 7482 verifyFormat("aaaa(\n" 7483 " \"bbbb\"\n" 7484 " \"cccc\");", 7485 Break); 7486 verifyFormat("aaaa(qqq, \"bbbb\"\n" 7487 " \"cccc\");", 7488 NoBreak); 7489 verifyFormat("aaaa(qqq,\n" 7490 " \"bbbb\"\n" 7491 " \"cccc\");", 7492 Break); 7493 verifyFormat("aaaa(qqq,\n" 7494 " L\"bbbb\"\n" 7495 " L\"cccc\");", 7496 Break); 7497 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 7498 " \"bbbb\"));", 7499 Break); 7500 verifyFormat("string s = someFunction(\n" 7501 " \"abc\"\n" 7502 " \"abc\");", 7503 Break); 7504 7505 // As we break before unary operators, breaking right after them is bad. 7506 verifyFormat("string foo = abc ? \"x\"\n" 7507 " \"blah blah blah blah blah blah\"\n" 7508 " : \"y\";", 7509 Break); 7510 7511 // Don't break if there is no column gain. 7512 verifyFormat("f(\"aaaa\"\n" 7513 " \"bbbb\");", 7514 Break); 7515 7516 // Treat literals with escaped newlines like multi-line string literals. 7517 EXPECT_EQ("x = \"a\\\n" 7518 "b\\\n" 7519 "c\";", 7520 format("x = \"a\\\n" 7521 "b\\\n" 7522 "c\";", 7523 NoBreak)); 7524 EXPECT_EQ("xxxx =\n" 7525 " \"a\\\n" 7526 "b\\\n" 7527 "c\";", 7528 format("xxxx = \"a\\\n" 7529 "b\\\n" 7530 "c\";", 7531 Break)); 7532 7533 EXPECT_EQ("NSString *const kString =\n" 7534 " @\"aaaa\"\n" 7535 " @\"bbbb\";", 7536 format("NSString *const kString = @\"aaaa\"\n" 7537 "@\"bbbb\";", 7538 Break)); 7539 7540 Break.ColumnLimit = 0; 7541 verifyFormat("const char *hello = \"hello llvm\";", Break); 7542 } 7543 7544 TEST_F(FormatTest, AlignsPipes) { 7545 verifyFormat( 7546 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7547 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7548 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7549 verifyFormat( 7550 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 7551 " << aaaaaaaaaaaaaaaaaaaa;"); 7552 verifyFormat( 7553 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7554 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7555 verifyFormat( 7556 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7557 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7558 verifyFormat( 7559 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 7560 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 7561 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 7562 verifyFormat( 7563 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7564 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7565 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7566 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7567 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7568 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7569 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 7570 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 7571 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 7572 verifyFormat( 7573 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7574 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7575 verifyFormat( 7576 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n" 7577 " aaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7578 7579 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 7580 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 7581 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7582 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7583 " aaaaaaaaaaaaaaaaaaaaa)\n" 7584 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7585 verifyFormat("LOG_IF(aaa == //\n" 7586 " bbb)\n" 7587 " << a << b;"); 7588 7589 // But sometimes, breaking before the first "<<" is desirable. 7590 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 7591 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 7592 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 7593 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7594 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7595 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 7596 " << BEF << IsTemplate << Description << E->getType();"); 7597 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 7598 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7599 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7600 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 7601 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7602 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7603 " << aaa;"); 7604 7605 verifyFormat( 7606 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7607 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7608 7609 // Incomplete string literal. 7610 EXPECT_EQ("llvm::errs() << \"\n" 7611 " << a;", 7612 format("llvm::errs() << \"\n<<a;")); 7613 7614 verifyFormat("void f() {\n" 7615 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 7616 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 7617 "}"); 7618 7619 // Handle 'endl'. 7620 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 7621 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 7622 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 7623 7624 // Handle '\n'. 7625 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 7626 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 7627 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 7628 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 7629 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 7630 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 7631 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 7632 } 7633 7634 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) { 7635 verifyFormat("return out << \"somepacket = {\\n\"\n" 7636 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 7637 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 7638 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 7639 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 7640 " << \"}\";"); 7641 7642 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 7643 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 7644 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 7645 verifyFormat( 7646 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 7647 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 7648 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 7649 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 7650 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 7651 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 7652 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 7653 verifyFormat( 7654 "void f() {\n" 7655 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 7656 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 7657 "}"); 7658 7659 // Breaking before the first "<<" is generally not desirable. 7660 verifyFormat( 7661 "llvm::errs()\n" 7662 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7663 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7664 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7665 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 7666 getLLVMStyleWithColumns(70)); 7667 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 7668 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7669 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 7670 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7671 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 7672 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 7673 getLLVMStyleWithColumns(70)); 7674 7675 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 7676 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 7677 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;"); 7678 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 7679 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 7680 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);"); 7681 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n" 7682 " (aaaa + aaaa);", 7683 getLLVMStyleWithColumns(40)); 7684 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n" 7685 " (aaaaaaa + aaaaa));", 7686 getLLVMStyleWithColumns(40)); 7687 verifyFormat( 7688 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n" 7689 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n" 7690 " bbbbbbbbbbbbbbbbbbbbbbb);"); 7691 } 7692 7693 TEST_F(FormatTest, UnderstandsEquals) { 7694 verifyFormat( 7695 "aaaaaaaaaaaaaaaaa =\n" 7696 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7697 verifyFormat( 7698 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 7699 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 7700 verifyFormat( 7701 "if (a) {\n" 7702 " f();\n" 7703 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 7704 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 7705 "}"); 7706 7707 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 7708 " 100000000 + 10000000) {\n}"); 7709 } 7710 7711 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 7712 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 7713 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 7714 7715 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 7716 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 7717 7718 verifyFormat( 7719 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 7720 " Parameter2);"); 7721 7722 verifyFormat( 7723 "ShortObject->shortFunction(\n" 7724 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 7725 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 7726 7727 verifyFormat("loooooooooooooongFunction(\n" 7728 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 7729 7730 verifyFormat( 7731 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 7732 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 7733 7734 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 7735 " .WillRepeatedly(Return(SomeValue));"); 7736 verifyFormat("void f() {\n" 7737 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 7738 " .Times(2)\n" 7739 " .WillRepeatedly(Return(SomeValue));\n" 7740 "}"); 7741 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 7742 " ccccccccccccccccccccccc);"); 7743 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7744 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7745 " .aaaaa(aaaaa),\n" 7746 " aaaaaaaaaaaaaaaaaaaaa);"); 7747 verifyFormat("void f() {\n" 7748 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7749 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 7750 "}"); 7751 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7752 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7753 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7754 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7755 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 7756 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7757 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7758 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7759 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 7760 "}"); 7761 7762 // Here, it is not necessary to wrap at "." or "->". 7763 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 7764 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 7765 verifyFormat( 7766 "aaaaaaaaaaa->aaaaaaaaa(\n" 7767 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7768 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 7769 7770 verifyFormat( 7771 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7772 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 7773 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 7774 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 7775 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 7776 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 7777 7778 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7779 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7780 " .a();"); 7781 7782 FormatStyle NoBinPacking = getLLVMStyle(); 7783 NoBinPacking.BinPackParameters = false; 7784 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 7785 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 7786 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 7787 " aaaaaaaaaaaaaaaaaaa,\n" 7788 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7789 NoBinPacking); 7790 7791 // If there is a subsequent call, change to hanging indentation. 7792 verifyFormat( 7793 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7794 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 7795 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7796 verifyFormat( 7797 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7798 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 7799 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7800 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7801 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7802 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7803 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7804 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 7805 } 7806 7807 TEST_F(FormatTest, WrapsTemplateDeclarations) { 7808 verifyFormat("template <typename T>\n" 7809 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 7810 verifyFormat("template <typename T>\n" 7811 "// T should be one of {A, B}.\n" 7812 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 7813 verifyFormat( 7814 "template <typename T>\n" 7815 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 7816 verifyFormat("template <typename T>\n" 7817 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 7818 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 7819 verifyFormat( 7820 "template <typename T>\n" 7821 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 7822 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 7823 verifyFormat( 7824 "template <typename T>\n" 7825 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 7826 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 7827 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7828 verifyFormat("template <typename T>\n" 7829 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7830 " int aaaaaaaaaaaaaaaaaaaaaa);"); 7831 verifyFormat( 7832 "template <typename T1, typename T2 = char, typename T3 = char,\n" 7833 " typename T4 = char>\n" 7834 "void f();"); 7835 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 7836 " template <typename> class cccccccccccccccccccccc,\n" 7837 " typename ddddddddddddd>\n" 7838 "class C {};"); 7839 verifyFormat( 7840 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 7841 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7842 7843 verifyFormat("void f() {\n" 7844 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 7845 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 7846 "}"); 7847 7848 verifyFormat("template <typename T> class C {};"); 7849 verifyFormat("template <typename T> void f();"); 7850 verifyFormat("template <typename T> void f() {}"); 7851 verifyFormat( 7852 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 7853 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7854 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 7855 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 7856 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7857 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 7858 " bbbbbbbbbbbbbbbbbbbbbbbb);", 7859 getLLVMStyleWithColumns(72)); 7860 EXPECT_EQ("static_cast<A< //\n" 7861 " B> *>(\n" 7862 "\n" 7863 ");", 7864 format("static_cast<A<//\n" 7865 " B>*>(\n" 7866 "\n" 7867 " );")); 7868 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7869 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 7870 7871 FormatStyle AlwaysBreak = getLLVMStyle(); 7872 AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 7873 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 7874 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 7875 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 7876 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7877 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 7878 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 7879 verifyFormat("template <template <typename> class Fooooooo,\n" 7880 " template <typename> class Baaaaaaar>\n" 7881 "struct C {};", 7882 AlwaysBreak); 7883 verifyFormat("template <typename T> // T can be A, B or C.\n" 7884 "struct C {};", 7885 AlwaysBreak); 7886 verifyFormat("template <enum E> class A {\n" 7887 "public:\n" 7888 " E *f();\n" 7889 "};"); 7890 7891 FormatStyle NeverBreak = getLLVMStyle(); 7892 NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No; 7893 verifyFormat("template <typename T> class C {};", NeverBreak); 7894 verifyFormat("template <typename T> void f();", NeverBreak); 7895 verifyFormat("template <typename T> void f() {}", NeverBreak); 7896 verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa " 7897 "bbbbbbbbbbbbbbbbbbbb) {}", 7898 NeverBreak); 7899 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7900 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 7901 " ccccccccccccccccccccccccccccccccccccccccccccccc);", 7902 NeverBreak); 7903 verifyFormat("template <template <typename> class Fooooooo,\n" 7904 " template <typename> class Baaaaaaar>\n" 7905 "struct C {};", 7906 NeverBreak); 7907 verifyFormat("template <typename T> // T can be A, B or C.\n" 7908 "struct C {};", 7909 NeverBreak); 7910 verifyFormat("template <enum E> class A {\n" 7911 "public:\n" 7912 " E *f();\n" 7913 "};", 7914 NeverBreak); 7915 NeverBreak.PenaltyBreakTemplateDeclaration = 100; 7916 verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa " 7917 "bbbbbbbbbbbbbbbbbbbb) {}", 7918 NeverBreak); 7919 } 7920 7921 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) { 7922 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 7923 Style.ColumnLimit = 60; 7924 EXPECT_EQ("// Baseline - no comments.\n" 7925 "template <\n" 7926 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 7927 "void f() {}", 7928 format("// Baseline - no comments.\n" 7929 "template <\n" 7930 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 7931 "void f() {}", 7932 Style)); 7933 7934 EXPECT_EQ("template <\n" 7935 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 7936 "void f() {}", 7937 format("template <\n" 7938 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 7939 "void f() {}", 7940 Style)); 7941 7942 EXPECT_EQ( 7943 "template <\n" 7944 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 7945 "void f() {}", 7946 format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 7947 "void f() {}", 7948 Style)); 7949 7950 EXPECT_EQ( 7951 "template <\n" 7952 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 7953 " // multiline\n" 7954 "void f() {}", 7955 format("template <\n" 7956 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 7957 " // multiline\n" 7958 "void f() {}", 7959 Style)); 7960 7961 EXPECT_EQ( 7962 "template <typename aaaaaaaaaa<\n" 7963 " bbbbbbbbbbbb>::value> // trailing loooong\n" 7964 "void f() {}", 7965 format( 7966 "template <\n" 7967 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n" 7968 "void f() {}", 7969 Style)); 7970 } 7971 7972 TEST_F(FormatTest, WrapsTemplateParameters) { 7973 FormatStyle Style = getLLVMStyle(); 7974 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7975 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 7976 verifyFormat( 7977 "template <typename... a> struct q {};\n" 7978 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 7979 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 7980 " y;", 7981 Style); 7982 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7983 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 7984 verifyFormat( 7985 "template <typename... a> struct r {};\n" 7986 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 7987 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 7988 " y;", 7989 Style); 7990 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 7991 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 7992 verifyFormat("template <typename... a> struct s {};\n" 7993 "extern s<\n" 7994 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 7995 "aaaaaaaaaaaaaaaaaaaaaa,\n" 7996 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 7997 "aaaaaaaaaaaaaaaaaaaaaa>\n" 7998 " y;", 7999 Style); 8000 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 8001 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 8002 verifyFormat("template <typename... a> struct t {};\n" 8003 "extern t<\n" 8004 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 8005 "aaaaaaaaaaaaaaaaaaaaaa,\n" 8006 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 8007 "aaaaaaaaaaaaaaaaaaaaaa>\n" 8008 " y;", 8009 Style); 8010 } 8011 8012 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 8013 verifyFormat( 8014 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8015 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8016 verifyFormat( 8017 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8018 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8019 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 8020 8021 // FIXME: Should we have the extra indent after the second break? 8022 verifyFormat( 8023 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8024 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8025 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8026 8027 verifyFormat( 8028 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 8029 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 8030 8031 // Breaking at nested name specifiers is generally not desirable. 8032 verifyFormat( 8033 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8034 " aaaaaaaaaaaaaaaaaaaaaaa);"); 8035 8036 verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n" 8037 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8038 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8039 " aaaaaaaaaaaaaaaaaaaaa);", 8040 getLLVMStyleWithColumns(74)); 8041 8042 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8043 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8044 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8045 } 8046 8047 TEST_F(FormatTest, UnderstandsTemplateParameters) { 8048 verifyFormat("A<int> a;"); 8049 verifyFormat("A<A<A<int>>> a;"); 8050 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 8051 verifyFormat("bool x = a < 1 || 2 > a;"); 8052 verifyFormat("bool x = 5 < f<int>();"); 8053 verifyFormat("bool x = f<int>() > 5;"); 8054 verifyFormat("bool x = 5 < a<int>::x;"); 8055 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 8056 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 8057 8058 verifyGoogleFormat("A<A<int>> a;"); 8059 verifyGoogleFormat("A<A<A<int>>> a;"); 8060 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 8061 verifyGoogleFormat("A<A<int> > a;"); 8062 verifyGoogleFormat("A<A<A<int> > > a;"); 8063 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 8064 verifyGoogleFormat("A<::A<int>> a;"); 8065 verifyGoogleFormat("A<::A> a;"); 8066 verifyGoogleFormat("A< ::A> a;"); 8067 verifyGoogleFormat("A< ::A<int> > a;"); 8068 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 8069 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 8070 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 8071 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 8072 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 8073 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 8074 8075 verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 8076 8077 // template closer followed by a token that starts with > or = 8078 verifyFormat("bool b = a<1> > 1;"); 8079 verifyFormat("bool b = a<1> >= 1;"); 8080 verifyFormat("int i = a<1> >> 1;"); 8081 FormatStyle Style = getLLVMStyle(); 8082 Style.SpaceBeforeAssignmentOperators = false; 8083 verifyFormat("bool b= a<1> == 1;", Style); 8084 verifyFormat("a<int> = 1;", Style); 8085 verifyFormat("a<int> >>= 1;", Style); 8086 8087 verifyFormat("test < a | b >> c;"); 8088 verifyFormat("test<test<a | b>> c;"); 8089 verifyFormat("test >> a >> b;"); 8090 verifyFormat("test << a >> b;"); 8091 8092 verifyFormat("f<int>();"); 8093 verifyFormat("template <typename T> void f() {}"); 8094 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 8095 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 8096 "sizeof(char)>::type>;"); 8097 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 8098 verifyFormat("f(a.operator()<A>());"); 8099 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8100 " .template operator()<A>());", 8101 getLLVMStyleWithColumns(35)); 8102 8103 // Not template parameters. 8104 verifyFormat("return a < b && c > d;"); 8105 verifyFormat("void f() {\n" 8106 " while (a < b && c > d) {\n" 8107 " }\n" 8108 "}"); 8109 verifyFormat("template <typename... Types>\n" 8110 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 8111 8112 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8113 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 8114 getLLVMStyleWithColumns(60)); 8115 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 8116 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 8117 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 8118 verifyFormat("some_templated_type<decltype([](int i) { return i; })>"); 8119 } 8120 8121 TEST_F(FormatTest, UnderstandsShiftOperators) { 8122 verifyFormat("if (i < x >> 1)"); 8123 verifyFormat("while (i < x >> 1)"); 8124 verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)"); 8125 verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)"); 8126 verifyFormat( 8127 "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)"); 8128 verifyFormat("Foo.call<Bar<Function>>()"); 8129 verifyFormat("if (Foo.call<Bar<Function>>() == 0)"); 8130 verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; " 8131 "++i, v = v >> 1)"); 8132 verifyFormat("if (w<u<v<x>>, 1>::t)"); 8133 } 8134 8135 TEST_F(FormatTest, BitshiftOperatorWidth) { 8136 EXPECT_EQ("int a = 1 << 2; /* foo\n" 8137 " bar */", 8138 format("int a=1<<2; /* foo\n" 8139 " bar */")); 8140 8141 EXPECT_EQ("int b = 256 >> 1; /* foo\n" 8142 " bar */", 8143 format("int b =256>>1 ; /* foo\n" 8144 " bar */")); 8145 } 8146 8147 TEST_F(FormatTest, UnderstandsBinaryOperators) { 8148 verifyFormat("COMPARE(a, ==, b);"); 8149 verifyFormat("auto s = sizeof...(Ts) - 1;"); 8150 } 8151 8152 TEST_F(FormatTest, UnderstandsPointersToMembers) { 8153 verifyFormat("int A::*x;"); 8154 verifyFormat("int (S::*func)(void *);"); 8155 verifyFormat("void f() { int (S::*func)(void *); }"); 8156 verifyFormat("typedef bool *(Class::*Member)() const;"); 8157 verifyFormat("void f() {\n" 8158 " (a->*f)();\n" 8159 " a->*x;\n" 8160 " (a.*f)();\n" 8161 " ((*a).*f)();\n" 8162 " a.*x;\n" 8163 "}"); 8164 verifyFormat("void f() {\n" 8165 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 8166 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 8167 "}"); 8168 verifyFormat( 8169 "(aaaaaaaaaa->*bbbbbbb)(\n" 8170 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 8171 FormatStyle Style = getLLVMStyle(); 8172 Style.PointerAlignment = FormatStyle::PAS_Left; 8173 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 8174 } 8175 8176 TEST_F(FormatTest, UnderstandsUnaryOperators) { 8177 verifyFormat("int a = -2;"); 8178 verifyFormat("f(-1, -2, -3);"); 8179 verifyFormat("a[-1] = 5;"); 8180 verifyFormat("int a = 5 + -2;"); 8181 verifyFormat("if (i == -1) {\n}"); 8182 verifyFormat("if (i != -1) {\n}"); 8183 verifyFormat("if (i > -1) {\n}"); 8184 verifyFormat("if (i < -1) {\n}"); 8185 verifyFormat("++(a->f());"); 8186 verifyFormat("--(a->f());"); 8187 verifyFormat("(a->f())++;"); 8188 verifyFormat("a[42]++;"); 8189 verifyFormat("if (!(a->f())) {\n}"); 8190 verifyFormat("if (!+i) {\n}"); 8191 verifyFormat("~&a;"); 8192 8193 verifyFormat("a-- > b;"); 8194 verifyFormat("b ? -a : c;"); 8195 verifyFormat("n * sizeof char16;"); 8196 verifyFormat("n * alignof char16;", getGoogleStyle()); 8197 verifyFormat("sizeof(char);"); 8198 verifyFormat("alignof(char);", getGoogleStyle()); 8199 8200 verifyFormat("return -1;"); 8201 verifyFormat("throw -1;"); 8202 verifyFormat("switch (a) {\n" 8203 "case -1:\n" 8204 " break;\n" 8205 "}"); 8206 verifyFormat("#define X -1"); 8207 verifyFormat("#define X -kConstant"); 8208 8209 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 8210 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 8211 8212 verifyFormat("int a = /* confusing comment */ -1;"); 8213 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 8214 verifyFormat("int a = i /* confusing comment */++;"); 8215 8216 verifyFormat("co_yield -1;"); 8217 verifyFormat("co_return -1;"); 8218 8219 // Check that * is not treated as a binary operator when we set 8220 // PointerAlignment as PAS_Left after a keyword and not a declaration. 8221 FormatStyle PASLeftStyle = getLLVMStyle(); 8222 PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left; 8223 verifyFormat("co_return *a;", PASLeftStyle); 8224 verifyFormat("co_await *a;", PASLeftStyle); 8225 verifyFormat("co_yield *a", PASLeftStyle); 8226 verifyFormat("return *a;", PASLeftStyle); 8227 } 8228 8229 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 8230 verifyFormat("if (!aaaaaaaaaa( // break\n" 8231 " aaaaa)) {\n" 8232 "}"); 8233 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 8234 " aaaaa));"); 8235 verifyFormat("*aaa = aaaaaaa( // break\n" 8236 " bbbbbb);"); 8237 } 8238 8239 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 8240 verifyFormat("bool operator<();"); 8241 verifyFormat("bool operator>();"); 8242 verifyFormat("bool operator=();"); 8243 verifyFormat("bool operator==();"); 8244 verifyFormat("bool operator!=();"); 8245 verifyFormat("int operator+();"); 8246 verifyFormat("int operator++();"); 8247 verifyFormat("int operator++(int) volatile noexcept;"); 8248 verifyFormat("bool operator,();"); 8249 verifyFormat("bool operator();"); 8250 verifyFormat("bool operator()();"); 8251 verifyFormat("bool operator[]();"); 8252 verifyFormat("operator bool();"); 8253 verifyFormat("operator int();"); 8254 verifyFormat("operator void *();"); 8255 verifyFormat("operator SomeType<int>();"); 8256 verifyFormat("operator SomeType<int, int>();"); 8257 verifyFormat("operator SomeType<SomeType<int>>();"); 8258 verifyFormat("void *operator new(std::size_t size);"); 8259 verifyFormat("void *operator new[](std::size_t size);"); 8260 verifyFormat("void operator delete(void *ptr);"); 8261 verifyFormat("void operator delete[](void *ptr);"); 8262 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 8263 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 8264 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 8265 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 8266 8267 verifyFormat( 8268 "ostream &operator<<(ostream &OutputStream,\n" 8269 " SomeReallyLongType WithSomeReallyLongValue);"); 8270 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 8271 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 8272 " return left.group < right.group;\n" 8273 "}"); 8274 verifyFormat("SomeType &operator=(const SomeType &S);"); 8275 verifyFormat("f.template operator()<int>();"); 8276 8277 verifyGoogleFormat("operator void*();"); 8278 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 8279 verifyGoogleFormat("operator ::A();"); 8280 8281 verifyFormat("using A::operator+;"); 8282 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 8283 "int i;"); 8284 8285 // Calling an operator as a member function. 8286 verifyFormat("void f() { a.operator*(); }"); 8287 verifyFormat("void f() { a.operator*(b & b); }"); 8288 verifyFormat("void f() { a->operator&(a * b); }"); 8289 verifyFormat("void f() { NS::a.operator+(*b * *b); }"); 8290 // TODO: Calling an operator as a non-member function is hard to distinguish. 8291 // https://llvm.org/PR50629 8292 // verifyFormat("void f() { operator*(a & a); }"); 8293 // verifyFormat("void f() { operator&(a, b * b); }"); 8294 } 8295 8296 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 8297 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 8298 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 8299 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 8300 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 8301 verifyFormat("Deleted &operator=(const Deleted &) &;"); 8302 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 8303 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 8304 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 8305 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 8306 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 8307 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 8308 verifyFormat("void Fn(T const &) const &;"); 8309 verifyFormat("void Fn(T const volatile &&) const volatile &&;"); 8310 verifyFormat("template <typename T>\n" 8311 "void F(T) && = delete;", 8312 getGoogleStyle()); 8313 8314 FormatStyle AlignLeft = getLLVMStyle(); 8315 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 8316 verifyFormat("void A::b() && {}", AlignLeft); 8317 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 8318 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 8319 AlignLeft); 8320 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 8321 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 8322 verifyFormat("auto Function(T t) & -> void {}", AlignLeft); 8323 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft); 8324 verifyFormat("auto Function(T) & -> void {}", AlignLeft); 8325 verifyFormat("auto Function(T) & -> void;", AlignLeft); 8326 verifyFormat("void Fn(T const&) const&;", AlignLeft); 8327 verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft); 8328 8329 FormatStyle Spaces = getLLVMStyle(); 8330 Spaces.SpacesInCStyleCastParentheses = true; 8331 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 8332 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 8333 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 8334 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 8335 8336 Spaces.SpacesInCStyleCastParentheses = false; 8337 Spaces.SpacesInParentheses = true; 8338 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 8339 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", 8340 Spaces); 8341 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 8342 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 8343 8344 FormatStyle BreakTemplate = getLLVMStyle(); 8345 BreakTemplate.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 8346 8347 verifyFormat("struct f {\n" 8348 " template <class T>\n" 8349 " int &foo(const std::string &str) &noexcept {}\n" 8350 "};", 8351 BreakTemplate); 8352 8353 verifyFormat("struct f {\n" 8354 " template <class T>\n" 8355 " int &foo(const std::string &str) &&noexcept {}\n" 8356 "};", 8357 BreakTemplate); 8358 8359 verifyFormat("struct f {\n" 8360 " template <class T>\n" 8361 " int &foo(const std::string &str) const &noexcept {}\n" 8362 "};", 8363 BreakTemplate); 8364 8365 verifyFormat("struct f {\n" 8366 " template <class T>\n" 8367 " int &foo(const std::string &str) const &noexcept {}\n" 8368 "};", 8369 BreakTemplate); 8370 8371 verifyFormat("struct f {\n" 8372 " template <class T>\n" 8373 " auto foo(const std::string &str) &&noexcept -> int & {}\n" 8374 "};", 8375 BreakTemplate); 8376 8377 FormatStyle AlignLeftBreakTemplate = getLLVMStyle(); 8378 AlignLeftBreakTemplate.AlwaysBreakTemplateDeclarations = 8379 FormatStyle::BTDS_Yes; 8380 AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left; 8381 8382 verifyFormat("struct f {\n" 8383 " template <class T>\n" 8384 " int& foo(const std::string& str) & noexcept {}\n" 8385 "};", 8386 AlignLeftBreakTemplate); 8387 8388 verifyFormat("struct f {\n" 8389 " template <class T>\n" 8390 " int& foo(const std::string& str) && noexcept {}\n" 8391 "};", 8392 AlignLeftBreakTemplate); 8393 8394 verifyFormat("struct f {\n" 8395 " template <class T>\n" 8396 " int& foo(const std::string& str) const& noexcept {}\n" 8397 "};", 8398 AlignLeftBreakTemplate); 8399 8400 verifyFormat("struct f {\n" 8401 " template <class T>\n" 8402 " int& foo(const std::string& str) const&& noexcept {}\n" 8403 "};", 8404 AlignLeftBreakTemplate); 8405 8406 verifyFormat("struct f {\n" 8407 " template <class T>\n" 8408 " auto foo(const std::string& str) && noexcept -> int& {}\n" 8409 "};", 8410 AlignLeftBreakTemplate); 8411 8412 // The `&` in `Type&` should not be confused with a trailing `&` of 8413 // DEPRECATED(reason) member function. 8414 verifyFormat("struct f {\n" 8415 " template <class T>\n" 8416 " DEPRECATED(reason)\n" 8417 " Type &foo(arguments) {}\n" 8418 "};", 8419 BreakTemplate); 8420 8421 verifyFormat("struct f {\n" 8422 " template <class T>\n" 8423 " DEPRECATED(reason)\n" 8424 " Type& foo(arguments) {}\n" 8425 "};", 8426 AlignLeftBreakTemplate); 8427 8428 verifyFormat("void (*foopt)(int) = &func;"); 8429 } 8430 8431 TEST_F(FormatTest, UnderstandsNewAndDelete) { 8432 verifyFormat("void f() {\n" 8433 " A *a = new A;\n" 8434 " A *a = new (placement) A;\n" 8435 " delete a;\n" 8436 " delete (A *)a;\n" 8437 "}"); 8438 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 8439 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 8440 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8441 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 8442 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 8443 verifyFormat("delete[] h->p;"); 8444 } 8445 8446 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 8447 verifyFormat("int *f(int *a) {}"); 8448 verifyFormat("int main(int argc, char **argv) {}"); 8449 verifyFormat("Test::Test(int b) : a(b * b) {}"); 8450 verifyIndependentOfContext("f(a, *a);"); 8451 verifyFormat("void g() { f(*a); }"); 8452 verifyIndependentOfContext("int a = b * 10;"); 8453 verifyIndependentOfContext("int a = 10 * b;"); 8454 verifyIndependentOfContext("int a = b * c;"); 8455 verifyIndependentOfContext("int a += b * c;"); 8456 verifyIndependentOfContext("int a -= b * c;"); 8457 verifyIndependentOfContext("int a *= b * c;"); 8458 verifyIndependentOfContext("int a /= b * c;"); 8459 verifyIndependentOfContext("int a = *b;"); 8460 verifyIndependentOfContext("int a = *b * c;"); 8461 verifyIndependentOfContext("int a = b * *c;"); 8462 verifyIndependentOfContext("int a = b * (10);"); 8463 verifyIndependentOfContext("S << b * (10);"); 8464 verifyIndependentOfContext("return 10 * b;"); 8465 verifyIndependentOfContext("return *b * *c;"); 8466 verifyIndependentOfContext("return a & ~b;"); 8467 verifyIndependentOfContext("f(b ? *c : *d);"); 8468 verifyIndependentOfContext("int a = b ? *c : *d;"); 8469 verifyIndependentOfContext("*b = a;"); 8470 verifyIndependentOfContext("a * ~b;"); 8471 verifyIndependentOfContext("a * !b;"); 8472 verifyIndependentOfContext("a * +b;"); 8473 verifyIndependentOfContext("a * -b;"); 8474 verifyIndependentOfContext("a * ++b;"); 8475 verifyIndependentOfContext("a * --b;"); 8476 verifyIndependentOfContext("a[4] * b;"); 8477 verifyIndependentOfContext("a[a * a] = 1;"); 8478 verifyIndependentOfContext("f() * b;"); 8479 verifyIndependentOfContext("a * [self dostuff];"); 8480 verifyIndependentOfContext("int x = a * (a + b);"); 8481 verifyIndependentOfContext("(a *)(a + b);"); 8482 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 8483 verifyIndependentOfContext("int *pa = (int *)&a;"); 8484 verifyIndependentOfContext("return sizeof(int **);"); 8485 verifyIndependentOfContext("return sizeof(int ******);"); 8486 verifyIndependentOfContext("return (int **&)a;"); 8487 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 8488 verifyFormat("void f(Type (*parameter)[10]) {}"); 8489 verifyFormat("void f(Type (¶meter)[10]) {}"); 8490 verifyGoogleFormat("return sizeof(int**);"); 8491 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 8492 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 8493 verifyFormat("auto a = [](int **&, int ***) {};"); 8494 verifyFormat("auto PointerBinding = [](const char *S) {};"); 8495 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 8496 verifyFormat("[](const decltype(*a) &value) {}"); 8497 verifyFormat("[](const typeof(*a) &value) {}"); 8498 verifyFormat("[](const _Atomic(a *) &value) {}"); 8499 verifyFormat("[](const __underlying_type(a) &value) {}"); 8500 verifyFormat("decltype(a * b) F();"); 8501 verifyFormat("typeof(a * b) F();"); 8502 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 8503 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 8504 verifyIndependentOfContext("typedef void (*f)(int *a);"); 8505 verifyIndependentOfContext("int i{a * b};"); 8506 verifyIndependentOfContext("aaa && aaa->f();"); 8507 verifyIndependentOfContext("int x = ~*p;"); 8508 verifyFormat("Constructor() : a(a), area(width * height) {}"); 8509 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 8510 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 8511 verifyFormat("void f() { f(a, c * d); }"); 8512 verifyFormat("void f() { f(new a(), c * d); }"); 8513 verifyFormat("void f(const MyOverride &override);"); 8514 verifyFormat("void f(const MyFinal &final);"); 8515 verifyIndependentOfContext("bool a = f() && override.f();"); 8516 verifyIndependentOfContext("bool a = f() && final.f();"); 8517 8518 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 8519 8520 verifyIndependentOfContext("A<int *> a;"); 8521 verifyIndependentOfContext("A<int **> a;"); 8522 verifyIndependentOfContext("A<int *, int *> a;"); 8523 verifyIndependentOfContext("A<int *[]> a;"); 8524 verifyIndependentOfContext( 8525 "const char *const p = reinterpret_cast<const char *const>(q);"); 8526 verifyIndependentOfContext("A<int **, int **> a;"); 8527 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 8528 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 8529 verifyFormat("for (; a && b;) {\n}"); 8530 verifyFormat("bool foo = true && [] { return false; }();"); 8531 8532 verifyFormat( 8533 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8534 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8535 8536 verifyGoogleFormat("int const* a = &b;"); 8537 verifyGoogleFormat("**outparam = 1;"); 8538 verifyGoogleFormat("*outparam = a * b;"); 8539 verifyGoogleFormat("int main(int argc, char** argv) {}"); 8540 verifyGoogleFormat("A<int*> a;"); 8541 verifyGoogleFormat("A<int**> a;"); 8542 verifyGoogleFormat("A<int*, int*> a;"); 8543 verifyGoogleFormat("A<int**, int**> a;"); 8544 verifyGoogleFormat("f(b ? *c : *d);"); 8545 verifyGoogleFormat("int a = b ? *c : *d;"); 8546 verifyGoogleFormat("Type* t = **x;"); 8547 verifyGoogleFormat("Type* t = *++*x;"); 8548 verifyGoogleFormat("*++*x;"); 8549 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 8550 verifyGoogleFormat("Type* t = x++ * y;"); 8551 verifyGoogleFormat( 8552 "const char* const p = reinterpret_cast<const char* const>(q);"); 8553 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 8554 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 8555 verifyGoogleFormat("template <typename T>\n" 8556 "void f(int i = 0, SomeType** temps = NULL);"); 8557 8558 FormatStyle Left = getLLVMStyle(); 8559 Left.PointerAlignment = FormatStyle::PAS_Left; 8560 verifyFormat("x = *a(x) = *a(y);", Left); 8561 verifyFormat("for (;; *a = b) {\n}", Left); 8562 verifyFormat("return *this += 1;", Left); 8563 verifyFormat("throw *x;", Left); 8564 verifyFormat("delete *x;", Left); 8565 verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left); 8566 verifyFormat("[](const decltype(*a)* ptr) {}", Left); 8567 verifyFormat("[](const typeof(*a)* ptr) {}", Left); 8568 verifyFormat("[](const _Atomic(a*)* ptr) {}", Left); 8569 verifyFormat("[](const __underlying_type(a)* ptr) {}", Left); 8570 verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left); 8571 verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left); 8572 verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left); 8573 verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left); 8574 8575 verifyIndependentOfContext("a = *(x + y);"); 8576 verifyIndependentOfContext("a = &(x + y);"); 8577 verifyIndependentOfContext("*(x + y).call();"); 8578 verifyIndependentOfContext("&(x + y)->call();"); 8579 verifyFormat("void f() { &(*I).first; }"); 8580 8581 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 8582 verifyFormat( 8583 "int *MyValues = {\n" 8584 " *A, // Operator detection might be confused by the '{'\n" 8585 " *BB // Operator detection might be confused by previous comment\n" 8586 "};"); 8587 8588 verifyIndependentOfContext("if (int *a = &b)"); 8589 verifyIndependentOfContext("if (int &a = *b)"); 8590 verifyIndependentOfContext("if (a & b[i])"); 8591 verifyIndependentOfContext("if constexpr (a & b[i])"); 8592 verifyIndependentOfContext("if CONSTEXPR (a & b[i])"); 8593 verifyIndependentOfContext("if (a * (b * c))"); 8594 verifyIndependentOfContext("if constexpr (a * (b * c))"); 8595 verifyIndependentOfContext("if CONSTEXPR (a * (b * c))"); 8596 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 8597 verifyIndependentOfContext("if (*b[i])"); 8598 verifyIndependentOfContext("if (int *a = (&b))"); 8599 verifyIndependentOfContext("while (int *a = &b)"); 8600 verifyIndependentOfContext("while (a * (b * c))"); 8601 verifyIndependentOfContext("size = sizeof *a;"); 8602 verifyIndependentOfContext("if (a && (b = c))"); 8603 verifyFormat("void f() {\n" 8604 " for (const int &v : Values) {\n" 8605 " }\n" 8606 "}"); 8607 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 8608 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 8609 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 8610 8611 verifyFormat("#define A (!a * b)"); 8612 verifyFormat("#define MACRO \\\n" 8613 " int *i = a * b; \\\n" 8614 " void f(a *b);", 8615 getLLVMStyleWithColumns(19)); 8616 8617 verifyIndependentOfContext("A = new SomeType *[Length];"); 8618 verifyIndependentOfContext("A = new SomeType *[Length]();"); 8619 verifyIndependentOfContext("T **t = new T *;"); 8620 verifyIndependentOfContext("T **t = new T *();"); 8621 verifyGoogleFormat("A = new SomeType*[Length]();"); 8622 verifyGoogleFormat("A = new SomeType*[Length];"); 8623 verifyGoogleFormat("T** t = new T*;"); 8624 verifyGoogleFormat("T** t = new T*();"); 8625 8626 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 8627 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 8628 verifyFormat("template <bool a, bool b> " 8629 "typename t::if<x && y>::type f() {}"); 8630 verifyFormat("template <int *y> f() {}"); 8631 verifyFormat("vector<int *> v;"); 8632 verifyFormat("vector<int *const> v;"); 8633 verifyFormat("vector<int *const **const *> v;"); 8634 verifyFormat("vector<int *volatile> v;"); 8635 verifyFormat("vector<a *_Nonnull> v;"); 8636 verifyFormat("vector<a *_Nullable> v;"); 8637 verifyFormat("vector<a *_Null_unspecified> v;"); 8638 verifyFormat("vector<a *__ptr32> v;"); 8639 verifyFormat("vector<a *__ptr64> v;"); 8640 verifyFormat("vector<a *__capability> v;"); 8641 FormatStyle TypeMacros = getLLVMStyle(); 8642 TypeMacros.TypenameMacros = {"LIST"}; 8643 verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros); 8644 verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros); 8645 verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros); 8646 verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros); 8647 verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros); // multiplication 8648 8649 FormatStyle CustomQualifier = getLLVMStyle(); 8650 // Add indentifers that should not be parsed as a qualifier by default. 8651 CustomQualifier.AttributeMacros.push_back("__my_qualifier"); 8652 CustomQualifier.AttributeMacros.push_back("_My_qualifier"); 8653 CustomQualifier.AttributeMacros.push_back("my_other_qualifier"); 8654 verifyFormat("vector<a * __my_qualifier> parse_as_multiply;"); 8655 verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier); 8656 verifyFormat("vector<a * _My_qualifier> parse_as_multiply;"); 8657 verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier); 8658 verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;"); 8659 verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier); 8660 verifyFormat("vector<a * _NotAQualifier> v;"); 8661 verifyFormat("vector<a * __not_a_qualifier> v;"); 8662 verifyFormat("vector<a * b> v;"); 8663 verifyFormat("foo<b && false>();"); 8664 verifyFormat("foo<b & 1>();"); 8665 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 8666 verifyFormat("typeof(*::std::declval<const T &>()) void F();"); 8667 verifyFormat("_Atomic(*::std::declval<const T &>()) void F();"); 8668 verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();"); 8669 verifyFormat( 8670 "template <class T, class = typename std::enable_if<\n" 8671 " std::is_integral<T>::value &&\n" 8672 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 8673 "void F();", 8674 getLLVMStyleWithColumns(70)); 8675 verifyFormat("template <class T,\n" 8676 " class = typename std::enable_if<\n" 8677 " std::is_integral<T>::value &&\n" 8678 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n" 8679 " class U>\n" 8680 "void F();", 8681 getLLVMStyleWithColumns(70)); 8682 verifyFormat( 8683 "template <class T,\n" 8684 " class = typename ::std::enable_if<\n" 8685 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 8686 "void F();", 8687 getGoogleStyleWithColumns(68)); 8688 8689 verifyIndependentOfContext("MACRO(int *i);"); 8690 verifyIndependentOfContext("MACRO(auto *a);"); 8691 verifyIndependentOfContext("MACRO(const A *a);"); 8692 verifyIndependentOfContext("MACRO(_Atomic(A) *a);"); 8693 verifyIndependentOfContext("MACRO(decltype(A) *a);"); 8694 verifyIndependentOfContext("MACRO(typeof(A) *a);"); 8695 verifyIndependentOfContext("MACRO(__underlying_type(A) *a);"); 8696 verifyIndependentOfContext("MACRO(A *const a);"); 8697 verifyIndependentOfContext("MACRO(A *restrict a);"); 8698 verifyIndependentOfContext("MACRO(A *__restrict__ a);"); 8699 verifyIndependentOfContext("MACRO(A *__restrict a);"); 8700 verifyIndependentOfContext("MACRO(A *volatile a);"); 8701 verifyIndependentOfContext("MACRO(A *__volatile a);"); 8702 verifyIndependentOfContext("MACRO(A *__volatile__ a);"); 8703 verifyIndependentOfContext("MACRO(A *_Nonnull a);"); 8704 verifyIndependentOfContext("MACRO(A *_Nullable a);"); 8705 verifyIndependentOfContext("MACRO(A *_Null_unspecified a);"); 8706 verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);"); 8707 verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);"); 8708 verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);"); 8709 verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);"); 8710 verifyIndependentOfContext("MACRO(A *__ptr32 a);"); 8711 verifyIndependentOfContext("MACRO(A *__ptr64 a);"); 8712 verifyIndependentOfContext("MACRO(A *__capability);"); 8713 verifyIndependentOfContext("MACRO(A &__capability);"); 8714 verifyFormat("MACRO(A *__my_qualifier);"); // type declaration 8715 verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication 8716 // If we add __my_qualifier to AttributeMacros it should always be parsed as 8717 // a type declaration: 8718 verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier); 8719 verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier); 8720 // Also check that TypenameMacros prevents parsing it as multiplication: 8721 verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication 8722 verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros); // type 8723 8724 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 8725 verifyFormat("void f() { f(float{1}, a * a); }"); 8726 verifyFormat("void f() { f(float(1), a * a); }"); 8727 8728 verifyFormat("f((void (*)(int))g);"); 8729 verifyFormat("f((void (&)(int))g);"); 8730 verifyFormat("f((void (^)(int))g);"); 8731 8732 // FIXME: Is there a way to make this work? 8733 // verifyIndependentOfContext("MACRO(A *a);"); 8734 verifyFormat("MACRO(A &B);"); 8735 verifyFormat("MACRO(A *B);"); 8736 verifyFormat("void f() { MACRO(A * B); }"); 8737 verifyFormat("void f() { MACRO(A & B); }"); 8738 8739 // This lambda was mis-formatted after D88956 (treating it as a binop): 8740 verifyFormat("auto x = [](const decltype(x) &ptr) {};"); 8741 verifyFormat("auto x = [](const decltype(x) *ptr) {};"); 8742 verifyFormat("#define lambda [](const decltype(x) &ptr) {}"); 8743 verifyFormat("#define lambda [](const decltype(x) *ptr) {}"); 8744 8745 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 8746 verifyFormat("return options != nullptr && operator==(*options);"); 8747 8748 EXPECT_EQ("#define OP(x) \\\n" 8749 " ostream &operator<<(ostream &s, const A &a) { \\\n" 8750 " return s << a.DebugString(); \\\n" 8751 " }", 8752 format("#define OP(x) \\\n" 8753 " ostream &operator<<(ostream &s, const A &a) { \\\n" 8754 " return s << a.DebugString(); \\\n" 8755 " }", 8756 getLLVMStyleWithColumns(50))); 8757 8758 // FIXME: We cannot handle this case yet; we might be able to figure out that 8759 // foo<x> d > v; doesn't make sense. 8760 verifyFormat("foo<a<b && c> d> v;"); 8761 8762 FormatStyle PointerMiddle = getLLVMStyle(); 8763 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 8764 verifyFormat("delete *x;", PointerMiddle); 8765 verifyFormat("int * x;", PointerMiddle); 8766 verifyFormat("int *[] x;", PointerMiddle); 8767 verifyFormat("template <int * y> f() {}", PointerMiddle); 8768 verifyFormat("int * f(int * a) {}", PointerMiddle); 8769 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 8770 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 8771 verifyFormat("A<int *> a;", PointerMiddle); 8772 verifyFormat("A<int **> a;", PointerMiddle); 8773 verifyFormat("A<int *, int *> a;", PointerMiddle); 8774 verifyFormat("A<int *[]> a;", PointerMiddle); 8775 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 8776 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 8777 verifyFormat("T ** t = new T *;", PointerMiddle); 8778 8779 // Member function reference qualifiers aren't binary operators. 8780 verifyFormat("string // break\n" 8781 "operator()() & {}"); 8782 verifyFormat("string // break\n" 8783 "operator()() && {}"); 8784 verifyGoogleFormat("template <typename T>\n" 8785 "auto x() & -> int {}"); 8786 8787 // Should be binary operators when used as an argument expression (overloaded 8788 // operator invoked as a member function). 8789 verifyFormat("void f() { a.operator()(a * a); }"); 8790 verifyFormat("void f() { a->operator()(a & a); }"); 8791 verifyFormat("void f() { a.operator()(*a & *a); }"); 8792 verifyFormat("void f() { a->operator()(*a * *a); }"); 8793 } 8794 8795 TEST_F(FormatTest, UnderstandsAttributes) { 8796 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 8797 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 8798 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 8799 FormatStyle AfterType = getLLVMStyle(); 8800 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 8801 verifyFormat("__attribute__((nodebug)) void\n" 8802 "foo() {}\n", 8803 AfterType); 8804 verifyFormat("__unused void\n" 8805 "foo() {}", 8806 AfterType); 8807 8808 FormatStyle CustomAttrs = getLLVMStyle(); 8809 CustomAttrs.AttributeMacros.push_back("__unused"); 8810 CustomAttrs.AttributeMacros.push_back("__attr1"); 8811 CustomAttrs.AttributeMacros.push_back("__attr2"); 8812 CustomAttrs.AttributeMacros.push_back("no_underscore_attr"); 8813 verifyFormat("vector<SomeType *__attribute((foo))> v;"); 8814 verifyFormat("vector<SomeType *__attribute__((foo))> v;"); 8815 verifyFormat("vector<SomeType * __not_attribute__((foo))> v;"); 8816 // Check that it is parsed as a multiplication without AttributeMacros and 8817 // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros. 8818 verifyFormat("vector<SomeType * __attr1> v;"); 8819 verifyFormat("vector<SomeType __attr1 *> v;"); 8820 verifyFormat("vector<SomeType __attr1 *const> v;"); 8821 verifyFormat("vector<SomeType __attr1 * __attr2> v;"); 8822 verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs); 8823 verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs); 8824 verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs); 8825 verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs); 8826 verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs); 8827 verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs); 8828 verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs); 8829 8830 // Check that these are not parsed as function declarations: 8831 CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 8832 CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman; 8833 verifyFormat("SomeType s(InitValue);", CustomAttrs); 8834 verifyFormat("SomeType s{InitValue};", CustomAttrs); 8835 verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs); 8836 verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs); 8837 verifyFormat("SomeType s __unused(InitValue);", CustomAttrs); 8838 verifyFormat("SomeType s __unused{InitValue};", CustomAttrs); 8839 verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs); 8840 verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs); 8841 } 8842 8843 TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) { 8844 // Check that qualifiers on pointers don't break parsing of casts. 8845 verifyFormat("x = (foo *const)*v;"); 8846 verifyFormat("x = (foo *volatile)*v;"); 8847 verifyFormat("x = (foo *restrict)*v;"); 8848 verifyFormat("x = (foo *__attribute__((foo)))*v;"); 8849 verifyFormat("x = (foo *_Nonnull)*v;"); 8850 verifyFormat("x = (foo *_Nullable)*v;"); 8851 verifyFormat("x = (foo *_Null_unspecified)*v;"); 8852 verifyFormat("x = (foo *_Nonnull)*v;"); 8853 verifyFormat("x = (foo *[[clang::attr]])*v;"); 8854 verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;"); 8855 verifyFormat("x = (foo *__ptr32)*v;"); 8856 verifyFormat("x = (foo *__ptr64)*v;"); 8857 verifyFormat("x = (foo *__capability)*v;"); 8858 8859 // Check that we handle multiple trailing qualifiers and skip them all to 8860 // determine that the expression is a cast to a pointer type. 8861 FormatStyle LongPointerRight = getLLVMStyleWithColumns(999); 8862 FormatStyle LongPointerLeft = getLLVMStyleWithColumns(999); 8863 LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left; 8864 StringRef AllQualifiers = 8865 "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified " 8866 "_Nonnull [[clang::attr]] __ptr32 __ptr64 __capability"; 8867 verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight); 8868 verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft); 8869 8870 // Also check that address-of is not parsed as a binary bitwise-and: 8871 verifyFormat("x = (foo *const)&v;"); 8872 verifyFormat(("x = (foo *" + AllQualifiers + ")&v;").str(), LongPointerRight); 8873 verifyFormat(("x = (foo* " + AllQualifiers + ")&v;").str(), LongPointerLeft); 8874 8875 // Check custom qualifiers: 8876 FormatStyle CustomQualifier = getLLVMStyleWithColumns(999); 8877 CustomQualifier.AttributeMacros.push_back("__my_qualifier"); 8878 verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier. 8879 verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier); 8880 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)*v;").str(), 8881 CustomQualifier); 8882 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)&v;").str(), 8883 CustomQualifier); 8884 8885 // Check that unknown identifiers result in binary operator parsing: 8886 verifyFormat("x = (foo * __unknown_qualifier) * v;"); 8887 verifyFormat("x = (foo * __unknown_qualifier) & v;"); 8888 } 8889 8890 TEST_F(FormatTest, UnderstandsSquareAttributes) { 8891 verifyFormat("SomeType s [[unused]] (InitValue);"); 8892 verifyFormat("SomeType s [[gnu::unused]] (InitValue);"); 8893 verifyFormat("SomeType s [[using gnu: unused]] (InitValue);"); 8894 verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}"); 8895 verifyFormat("void f() [[deprecated(\"so sorry\")]];"); 8896 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8897 " [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 8898 verifyFormat("[[nodiscard]] bool f() { return false; }"); 8899 verifyFormat("class [[nodiscard]] f {\npublic:\n f() {}\n}"); 8900 verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n f() {}\n}"); 8901 verifyFormat("class [[gnu::unused]] f {\npublic:\n f() {}\n}"); 8902 8903 // Make sure we do not mistake attributes for array subscripts. 8904 verifyFormat("int a() {}\n" 8905 "[[unused]] int b() {}\n"); 8906 verifyFormat("NSArray *arr;\n" 8907 "arr[[Foo() bar]];"); 8908 8909 // On the other hand, we still need to correctly find array subscripts. 8910 verifyFormat("int a = std::vector<int>{1, 2, 3}[0];"); 8911 8912 // Make sure that we do not mistake Objective-C method inside array literals 8913 // as attributes, even if those method names are also keywords. 8914 verifyFormat("@[ [foo bar] ];"); 8915 verifyFormat("@[ [NSArray class] ];"); 8916 verifyFormat("@[ [foo enum] ];"); 8917 8918 verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }"); 8919 8920 // Make sure we do not parse attributes as lambda introducers. 8921 FormatStyle MultiLineFunctions = getLLVMStyle(); 8922 MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 8923 verifyFormat("[[unused]] int b() {\n" 8924 " return 42;\n" 8925 "}\n", 8926 MultiLineFunctions); 8927 } 8928 8929 TEST_F(FormatTest, AttributeClass) { 8930 FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp); 8931 verifyFormat("class S {\n" 8932 " S(S&&) = default;\n" 8933 "};", 8934 Style); 8935 verifyFormat("class [[nodiscard]] S {\n" 8936 " S(S&&) = default;\n" 8937 "};", 8938 Style); 8939 verifyFormat("class __attribute((maybeunused)) S {\n" 8940 " S(S&&) = default;\n" 8941 "};", 8942 Style); 8943 verifyFormat("struct S {\n" 8944 " S(S&&) = default;\n" 8945 "};", 8946 Style); 8947 verifyFormat("struct [[nodiscard]] S {\n" 8948 " S(S&&) = default;\n" 8949 "};", 8950 Style); 8951 } 8952 8953 TEST_F(FormatTest, AttributesAfterMacro) { 8954 FormatStyle Style = getLLVMStyle(); 8955 verifyFormat("MACRO;\n" 8956 "__attribute__((maybe_unused)) int foo() {\n" 8957 " //...\n" 8958 "}"); 8959 8960 verifyFormat("MACRO;\n" 8961 "[[nodiscard]] int foo() {\n" 8962 " //...\n" 8963 "}"); 8964 8965 EXPECT_EQ("MACRO\n\n" 8966 "__attribute__((maybe_unused)) int foo() {\n" 8967 " //...\n" 8968 "}", 8969 format("MACRO\n\n" 8970 "__attribute__((maybe_unused)) int foo() {\n" 8971 " //...\n" 8972 "}")); 8973 8974 EXPECT_EQ("MACRO\n\n" 8975 "[[nodiscard]] int foo() {\n" 8976 " //...\n" 8977 "}", 8978 format("MACRO\n\n" 8979 "[[nodiscard]] int foo() {\n" 8980 " //...\n" 8981 "}")); 8982 } 8983 8984 TEST_F(FormatTest, AttributePenaltyBreaking) { 8985 FormatStyle Style = getLLVMStyle(); 8986 verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n" 8987 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", 8988 Style); 8989 verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n" 8990 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", 8991 Style); 8992 verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const " 8993 "shared_ptr<ALongTypeName> &C d) {\n}", 8994 Style); 8995 } 8996 8997 TEST_F(FormatTest, UnderstandsEllipsis) { 8998 FormatStyle Style = getLLVMStyle(); 8999 verifyFormat("int printf(const char *fmt, ...);"); 9000 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 9001 verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}"); 9002 9003 verifyFormat("template <int *...PP> a;", Style); 9004 9005 Style.PointerAlignment = FormatStyle::PAS_Left; 9006 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style); 9007 9008 verifyFormat("template <int*... PP> a;", Style); 9009 9010 Style.PointerAlignment = FormatStyle::PAS_Middle; 9011 verifyFormat("template <int *... PP> a;", Style); 9012 } 9013 9014 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 9015 EXPECT_EQ("int *a;\n" 9016 "int *a;\n" 9017 "int *a;", 9018 format("int *a;\n" 9019 "int* a;\n" 9020 "int *a;", 9021 getGoogleStyle())); 9022 EXPECT_EQ("int* a;\n" 9023 "int* a;\n" 9024 "int* a;", 9025 format("int* a;\n" 9026 "int* a;\n" 9027 "int *a;", 9028 getGoogleStyle())); 9029 EXPECT_EQ("int *a;\n" 9030 "int *a;\n" 9031 "int *a;", 9032 format("int *a;\n" 9033 "int * a;\n" 9034 "int * a;", 9035 getGoogleStyle())); 9036 EXPECT_EQ("auto x = [] {\n" 9037 " int *a;\n" 9038 " int *a;\n" 9039 " int *a;\n" 9040 "};", 9041 format("auto x=[]{int *a;\n" 9042 "int * a;\n" 9043 "int * a;};", 9044 getGoogleStyle())); 9045 } 9046 9047 TEST_F(FormatTest, UnderstandsRvalueReferences) { 9048 verifyFormat("int f(int &&a) {}"); 9049 verifyFormat("int f(int a, char &&b) {}"); 9050 verifyFormat("void f() { int &&a = b; }"); 9051 verifyGoogleFormat("int f(int a, char&& b) {}"); 9052 verifyGoogleFormat("void f() { int&& a = b; }"); 9053 9054 verifyIndependentOfContext("A<int &&> a;"); 9055 verifyIndependentOfContext("A<int &&, int &&> a;"); 9056 verifyGoogleFormat("A<int&&> a;"); 9057 verifyGoogleFormat("A<int&&, int&&> a;"); 9058 9059 // Not rvalue references: 9060 verifyFormat("template <bool B, bool C> class A {\n" 9061 " static_assert(B && C, \"Something is wrong\");\n" 9062 "};"); 9063 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 9064 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 9065 verifyFormat("#define A(a, b) (a && b)"); 9066 } 9067 9068 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 9069 verifyFormat("void f() {\n" 9070 " x[aaaaaaaaa -\n" 9071 " b] = 23;\n" 9072 "}", 9073 getLLVMStyleWithColumns(15)); 9074 } 9075 9076 TEST_F(FormatTest, FormatsCasts) { 9077 verifyFormat("Type *A = static_cast<Type *>(P);"); 9078 verifyFormat("Type *A = (Type *)P;"); 9079 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 9080 verifyFormat("int a = (int)(2.0f);"); 9081 verifyFormat("int a = (int)2.0f;"); 9082 verifyFormat("x[(int32)y];"); 9083 verifyFormat("x = (int32)y;"); 9084 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 9085 verifyFormat("int a = (int)*b;"); 9086 verifyFormat("int a = (int)2.0f;"); 9087 verifyFormat("int a = (int)~0;"); 9088 verifyFormat("int a = (int)++a;"); 9089 verifyFormat("int a = (int)sizeof(int);"); 9090 verifyFormat("int a = (int)+2;"); 9091 verifyFormat("my_int a = (my_int)2.0f;"); 9092 verifyFormat("my_int a = (my_int)sizeof(int);"); 9093 verifyFormat("return (my_int)aaa;"); 9094 verifyFormat("#define x ((int)-1)"); 9095 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 9096 verifyFormat("#define p(q) ((int *)&q)"); 9097 verifyFormat("fn(a)(b) + 1;"); 9098 9099 verifyFormat("void f() { my_int a = (my_int)*b; }"); 9100 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 9101 verifyFormat("my_int a = (my_int)~0;"); 9102 verifyFormat("my_int a = (my_int)++a;"); 9103 verifyFormat("my_int a = (my_int)-2;"); 9104 verifyFormat("my_int a = (my_int)1;"); 9105 verifyFormat("my_int a = (my_int *)1;"); 9106 verifyFormat("my_int a = (const my_int)-1;"); 9107 verifyFormat("my_int a = (const my_int *)-1;"); 9108 verifyFormat("my_int a = (my_int)(my_int)-1;"); 9109 verifyFormat("my_int a = (ns::my_int)-2;"); 9110 verifyFormat("case (my_int)ONE:"); 9111 verifyFormat("auto x = (X)this;"); 9112 // Casts in Obj-C style calls used to not be recognized as such. 9113 verifyFormat("int a = [(type*)[((type*)val) arg] arg];", getGoogleStyle()); 9114 9115 // FIXME: single value wrapped with paren will be treated as cast. 9116 verifyFormat("void f(int i = (kValue)*kMask) {}"); 9117 9118 verifyFormat("{ (void)F; }"); 9119 9120 // Don't break after a cast's 9121 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 9122 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 9123 " bbbbbbbbbbbbbbbbbbbbbb);"); 9124 9125 // These are not casts. 9126 verifyFormat("void f(int *) {}"); 9127 verifyFormat("f(foo)->b;"); 9128 verifyFormat("f(foo).b;"); 9129 verifyFormat("f(foo)(b);"); 9130 verifyFormat("f(foo)[b];"); 9131 verifyFormat("[](foo) { return 4; }(bar);"); 9132 verifyFormat("(*funptr)(foo)[4];"); 9133 verifyFormat("funptrs[4](foo)[4];"); 9134 verifyFormat("void f(int *);"); 9135 verifyFormat("void f(int *) = 0;"); 9136 verifyFormat("void f(SmallVector<int>) {}"); 9137 verifyFormat("void f(SmallVector<int>);"); 9138 verifyFormat("void f(SmallVector<int>) = 0;"); 9139 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 9140 verifyFormat("int a = sizeof(int) * b;"); 9141 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 9142 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 9143 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 9144 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 9145 9146 // These are not casts, but at some point were confused with casts. 9147 verifyFormat("virtual void foo(int *) override;"); 9148 verifyFormat("virtual void foo(char &) const;"); 9149 verifyFormat("virtual void foo(int *a, char *) const;"); 9150 verifyFormat("int a = sizeof(int *) + b;"); 9151 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 9152 verifyFormat("bool b = f(g<int>) && c;"); 9153 verifyFormat("typedef void (*f)(int i) func;"); 9154 verifyFormat("void operator++(int) noexcept;"); 9155 verifyFormat("void operator++(int &) noexcept;"); 9156 verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t " 9157 "&) noexcept;"); 9158 verifyFormat( 9159 "void operator delete(std::size_t, const std::nothrow_t &) noexcept;"); 9160 verifyFormat("void operator delete(const std::nothrow_t &) noexcept;"); 9161 verifyFormat("void operator delete(std::nothrow_t &) noexcept;"); 9162 verifyFormat("void operator delete(nothrow_t &) noexcept;"); 9163 verifyFormat("void operator delete(foo &) noexcept;"); 9164 verifyFormat("void operator delete(foo) noexcept;"); 9165 verifyFormat("void operator delete(int) noexcept;"); 9166 verifyFormat("void operator delete(int &) noexcept;"); 9167 verifyFormat("void operator delete(int &) volatile noexcept;"); 9168 verifyFormat("void operator delete(int &) const"); 9169 verifyFormat("void operator delete(int &) = default"); 9170 verifyFormat("void operator delete(int &) = delete"); 9171 verifyFormat("void operator delete(int &) [[noreturn]]"); 9172 verifyFormat("void operator delete(int &) throw();"); 9173 verifyFormat("void operator delete(int &) throw(int);"); 9174 verifyFormat("auto operator delete(int &) -> int;"); 9175 verifyFormat("auto operator delete(int &) override"); 9176 verifyFormat("auto operator delete(int &) final"); 9177 9178 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 9179 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 9180 // FIXME: The indentation here is not ideal. 9181 verifyFormat( 9182 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9183 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 9184 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 9185 } 9186 9187 TEST_F(FormatTest, FormatsFunctionTypes) { 9188 verifyFormat("A<bool()> a;"); 9189 verifyFormat("A<SomeType()> a;"); 9190 verifyFormat("A<void (*)(int, std::string)> a;"); 9191 verifyFormat("A<void *(int)>;"); 9192 verifyFormat("void *(*a)(int *, SomeType *);"); 9193 verifyFormat("int (*func)(void *);"); 9194 verifyFormat("void f() { int (*func)(void *); }"); 9195 verifyFormat("template <class CallbackClass>\n" 9196 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 9197 9198 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 9199 verifyGoogleFormat("void* (*a)(int);"); 9200 verifyGoogleFormat( 9201 "template <class CallbackClass>\n" 9202 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 9203 9204 // Other constructs can look somewhat like function types: 9205 verifyFormat("A<sizeof(*x)> a;"); 9206 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 9207 verifyFormat("some_var = function(*some_pointer_var)[0];"); 9208 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 9209 verifyFormat("int x = f(&h)();"); 9210 verifyFormat("returnsFunction(¶m1, ¶m2)(param);"); 9211 verifyFormat("std::function<\n" 9212 " LooooooooooongTemplatedType<\n" 9213 " SomeType>*(\n" 9214 " LooooooooooooooooongType type)>\n" 9215 " function;", 9216 getGoogleStyleWithColumns(40)); 9217 } 9218 9219 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 9220 verifyFormat("A (*foo_)[6];"); 9221 verifyFormat("vector<int> (*foo_)[6];"); 9222 } 9223 9224 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 9225 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 9226 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 9227 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 9228 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 9229 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 9230 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 9231 9232 // Different ways of ()-initializiation. 9233 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 9234 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 9235 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 9236 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 9237 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 9238 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 9239 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 9240 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 9241 9242 // Lambdas should not confuse the variable declaration heuristic. 9243 verifyFormat("LooooooooooooooooongType\n" 9244 " variable(nullptr, [](A *a) {});", 9245 getLLVMStyleWithColumns(40)); 9246 } 9247 9248 TEST_F(FormatTest, BreaksLongDeclarations) { 9249 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 9250 " AnotherNameForTheLongType;"); 9251 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 9252 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 9253 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 9254 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 9255 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 9256 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 9257 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 9258 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 9259 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 9260 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 9261 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 9262 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 9263 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 9264 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 9265 verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n" 9266 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 9267 verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n" 9268 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 9269 verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n" 9270 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 9271 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 9272 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);"); 9273 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 9274 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}"); 9275 FormatStyle Indented = getLLVMStyle(); 9276 Indented.IndentWrappedFunctionNames = true; 9277 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 9278 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 9279 Indented); 9280 verifyFormat( 9281 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 9282 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 9283 Indented); 9284 verifyFormat( 9285 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 9286 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 9287 Indented); 9288 verifyFormat( 9289 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 9290 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 9291 Indented); 9292 9293 // FIXME: Without the comment, this breaks after "(". 9294 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 9295 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 9296 getGoogleStyle()); 9297 9298 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 9299 " int LoooooooooooooooooooongParam2) {}"); 9300 verifyFormat( 9301 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 9302 " SourceLocation L, IdentifierIn *II,\n" 9303 " Type *T) {}"); 9304 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 9305 "ReallyReaaallyLongFunctionName(\n" 9306 " const std::string &SomeParameter,\n" 9307 " const SomeType<string, SomeOtherTemplateParameter>\n" 9308 " &ReallyReallyLongParameterName,\n" 9309 " const SomeType<string, SomeOtherTemplateParameter>\n" 9310 " &AnotherLongParameterName) {}"); 9311 verifyFormat("template <typename A>\n" 9312 "SomeLoooooooooooooooooooooongType<\n" 9313 " typename some_namespace::SomeOtherType<A>::Type>\n" 9314 "Function() {}"); 9315 9316 verifyGoogleFormat( 9317 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 9318 " aaaaaaaaaaaaaaaaaaaaaaa;"); 9319 verifyGoogleFormat( 9320 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 9321 " SourceLocation L) {}"); 9322 verifyGoogleFormat( 9323 "some_namespace::LongReturnType\n" 9324 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 9325 " int first_long_parameter, int second_parameter) {}"); 9326 9327 verifyGoogleFormat("template <typename T>\n" 9328 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 9329 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 9330 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9331 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 9332 9333 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 9334 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9335 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9336 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9337 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 9338 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 9339 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9340 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 9341 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 9342 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9343 9344 verifyFormat("template <typename T> // Templates on own line.\n" 9345 "static int // Some comment.\n" 9346 "MyFunction(int a);", 9347 getLLVMStyle()); 9348 } 9349 9350 TEST_F(FormatTest, FormatsAccessModifiers) { 9351 FormatStyle Style = getLLVMStyle(); 9352 EXPECT_EQ(Style.EmptyLineBeforeAccessModifier, 9353 FormatStyle::ELBAMS_LogicalBlock); 9354 verifyFormat("struct foo {\n" 9355 "private:\n" 9356 " void f() {}\n" 9357 "\n" 9358 "private:\n" 9359 " int i;\n" 9360 "\n" 9361 "protected:\n" 9362 " int j;\n" 9363 "};\n", 9364 Style); 9365 verifyFormat("struct foo {\n" 9366 "private:\n" 9367 " void f() {}\n" 9368 "\n" 9369 "private:\n" 9370 " int i;\n" 9371 "\n" 9372 "protected:\n" 9373 " int j;\n" 9374 "};\n", 9375 "struct foo {\n" 9376 "private:\n" 9377 " void f() {}\n" 9378 "private:\n" 9379 " int i;\n" 9380 "protected:\n" 9381 " int j;\n" 9382 "};\n", 9383 Style); 9384 verifyFormat("struct foo { /* comment */\n" 9385 "private:\n" 9386 " int i;\n" 9387 " // comment\n" 9388 "private:\n" 9389 " int j;\n" 9390 "};\n", 9391 Style); 9392 verifyFormat("struct foo {\n" 9393 "#ifdef FOO\n" 9394 "#endif\n" 9395 "private:\n" 9396 " int i;\n" 9397 "#ifdef FOO\n" 9398 "private:\n" 9399 "#endif\n" 9400 " int j;\n" 9401 "};\n", 9402 Style); 9403 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 9404 verifyFormat("struct foo {\n" 9405 "private:\n" 9406 " void f() {}\n" 9407 "private:\n" 9408 " int i;\n" 9409 "protected:\n" 9410 " int j;\n" 9411 "};\n", 9412 Style); 9413 verifyFormat("struct foo {\n" 9414 "private:\n" 9415 " void f() {}\n" 9416 "private:\n" 9417 " int i;\n" 9418 "protected:\n" 9419 " int j;\n" 9420 "};\n", 9421 "struct foo {\n" 9422 "\n" 9423 "private:\n" 9424 " void f() {}\n" 9425 "\n" 9426 "private:\n" 9427 " int i;\n" 9428 "\n" 9429 "protected:\n" 9430 " int j;\n" 9431 "};\n", 9432 Style); 9433 verifyFormat("struct foo { /* comment */\n" 9434 "private:\n" 9435 " int i;\n" 9436 " // comment\n" 9437 "private:\n" 9438 " int j;\n" 9439 "};\n", 9440 "struct foo { /* comment */\n" 9441 "\n" 9442 "private:\n" 9443 " int i;\n" 9444 " // comment\n" 9445 "\n" 9446 "private:\n" 9447 " int j;\n" 9448 "};\n", 9449 Style); 9450 verifyFormat("struct foo {\n" 9451 "#ifdef FOO\n" 9452 "#endif\n" 9453 "private:\n" 9454 " int i;\n" 9455 "#ifdef FOO\n" 9456 "private:\n" 9457 "#endif\n" 9458 " int j;\n" 9459 "};\n", 9460 "struct foo {\n" 9461 "#ifdef FOO\n" 9462 "#endif\n" 9463 "\n" 9464 "private:\n" 9465 " int i;\n" 9466 "#ifdef FOO\n" 9467 "\n" 9468 "private:\n" 9469 "#endif\n" 9470 " int j;\n" 9471 "};\n", 9472 Style); 9473 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 9474 verifyFormat("struct foo {\n" 9475 "private:\n" 9476 " void f() {}\n" 9477 "\n" 9478 "private:\n" 9479 " int i;\n" 9480 "\n" 9481 "protected:\n" 9482 " int j;\n" 9483 "};\n", 9484 Style); 9485 verifyFormat("struct foo {\n" 9486 "private:\n" 9487 " void f() {}\n" 9488 "\n" 9489 "private:\n" 9490 " int i;\n" 9491 "\n" 9492 "protected:\n" 9493 " int j;\n" 9494 "};\n", 9495 "struct foo {\n" 9496 "private:\n" 9497 " void f() {}\n" 9498 "private:\n" 9499 " int i;\n" 9500 "protected:\n" 9501 " int j;\n" 9502 "};\n", 9503 Style); 9504 verifyFormat("struct foo { /* comment */\n" 9505 "private:\n" 9506 " int i;\n" 9507 " // comment\n" 9508 "\n" 9509 "private:\n" 9510 " int j;\n" 9511 "};\n", 9512 "struct foo { /* comment */\n" 9513 "private:\n" 9514 " int i;\n" 9515 " // comment\n" 9516 "\n" 9517 "private:\n" 9518 " int j;\n" 9519 "};\n", 9520 Style); 9521 verifyFormat("struct foo {\n" 9522 "#ifdef FOO\n" 9523 "#endif\n" 9524 "\n" 9525 "private:\n" 9526 " int i;\n" 9527 "#ifdef FOO\n" 9528 "\n" 9529 "private:\n" 9530 "#endif\n" 9531 " int j;\n" 9532 "};\n", 9533 "struct foo {\n" 9534 "#ifdef FOO\n" 9535 "#endif\n" 9536 "private:\n" 9537 " int i;\n" 9538 "#ifdef FOO\n" 9539 "private:\n" 9540 "#endif\n" 9541 " int j;\n" 9542 "};\n", 9543 Style); 9544 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 9545 EXPECT_EQ("struct foo {\n" 9546 "\n" 9547 "private:\n" 9548 " void f() {}\n" 9549 "\n" 9550 "private:\n" 9551 " int i;\n" 9552 "\n" 9553 "protected:\n" 9554 " int j;\n" 9555 "};\n", 9556 format("struct foo {\n" 9557 "\n" 9558 "private:\n" 9559 " void f() {}\n" 9560 "\n" 9561 "private:\n" 9562 " int i;\n" 9563 "\n" 9564 "protected:\n" 9565 " int j;\n" 9566 "};\n", 9567 Style)); 9568 verifyFormat("struct foo {\n" 9569 "private:\n" 9570 " void f() {}\n" 9571 "private:\n" 9572 " int i;\n" 9573 "protected:\n" 9574 " int j;\n" 9575 "};\n", 9576 Style); 9577 EXPECT_EQ("struct foo { /* comment */\n" 9578 "\n" 9579 "private:\n" 9580 " int i;\n" 9581 " // comment\n" 9582 "\n" 9583 "private:\n" 9584 " int j;\n" 9585 "};\n", 9586 format("struct foo { /* comment */\n" 9587 "\n" 9588 "private:\n" 9589 " int i;\n" 9590 " // comment\n" 9591 "\n" 9592 "private:\n" 9593 " int j;\n" 9594 "};\n", 9595 Style)); 9596 verifyFormat("struct foo { /* comment */\n" 9597 "private:\n" 9598 " int i;\n" 9599 " // comment\n" 9600 "private:\n" 9601 " int j;\n" 9602 "};\n", 9603 Style); 9604 EXPECT_EQ("struct foo {\n" 9605 "#ifdef FOO\n" 9606 "#endif\n" 9607 "\n" 9608 "private:\n" 9609 " int i;\n" 9610 "#ifdef FOO\n" 9611 "\n" 9612 "private:\n" 9613 "#endif\n" 9614 " int j;\n" 9615 "};\n", 9616 format("struct foo {\n" 9617 "#ifdef FOO\n" 9618 "#endif\n" 9619 "\n" 9620 "private:\n" 9621 " int i;\n" 9622 "#ifdef FOO\n" 9623 "\n" 9624 "private:\n" 9625 "#endif\n" 9626 " int j;\n" 9627 "};\n", 9628 Style)); 9629 verifyFormat("struct foo {\n" 9630 "#ifdef FOO\n" 9631 "#endif\n" 9632 "private:\n" 9633 " int i;\n" 9634 "#ifdef FOO\n" 9635 "private:\n" 9636 "#endif\n" 9637 " int j;\n" 9638 "};\n", 9639 Style); 9640 9641 FormatStyle NoEmptyLines = getLLVMStyle(); 9642 NoEmptyLines.MaxEmptyLinesToKeep = 0; 9643 verifyFormat("struct foo {\n" 9644 "private:\n" 9645 " void f() {}\n" 9646 "\n" 9647 "private:\n" 9648 " int i;\n" 9649 "\n" 9650 "public:\n" 9651 "protected:\n" 9652 " int j;\n" 9653 "};\n", 9654 NoEmptyLines); 9655 9656 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 9657 verifyFormat("struct foo {\n" 9658 "private:\n" 9659 " void f() {}\n" 9660 "private:\n" 9661 " int i;\n" 9662 "public:\n" 9663 "protected:\n" 9664 " int j;\n" 9665 "};\n", 9666 NoEmptyLines); 9667 9668 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 9669 verifyFormat("struct foo {\n" 9670 "private:\n" 9671 " void f() {}\n" 9672 "\n" 9673 "private:\n" 9674 " int i;\n" 9675 "\n" 9676 "public:\n" 9677 "\n" 9678 "protected:\n" 9679 " int j;\n" 9680 "};\n", 9681 NoEmptyLines); 9682 } 9683 9684 TEST_F(FormatTest, FormatsAfterAccessModifiers) { 9685 9686 FormatStyle Style = getLLVMStyle(); 9687 EXPECT_EQ(Style.EmptyLineAfterAccessModifier, FormatStyle::ELAAMS_Never); 9688 verifyFormat("struct foo {\n" 9689 "private:\n" 9690 " void f() {}\n" 9691 "\n" 9692 "private:\n" 9693 " int i;\n" 9694 "\n" 9695 "protected:\n" 9696 " int j;\n" 9697 "};\n", 9698 Style); 9699 9700 // Check if lines are removed. 9701 verifyFormat("struct foo {\n" 9702 "private:\n" 9703 " void f() {}\n" 9704 "\n" 9705 "private:\n" 9706 " int i;\n" 9707 "\n" 9708 "protected:\n" 9709 " int j;\n" 9710 "};\n", 9711 "struct foo {\n" 9712 "private:\n" 9713 "\n" 9714 " void f() {}\n" 9715 "\n" 9716 "private:\n" 9717 "\n" 9718 " int i;\n" 9719 "\n" 9720 "protected:\n" 9721 "\n" 9722 " int j;\n" 9723 "};\n", 9724 Style); 9725 9726 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 9727 verifyFormat("struct foo {\n" 9728 "private:\n" 9729 "\n" 9730 " void f() {}\n" 9731 "\n" 9732 "private:\n" 9733 "\n" 9734 " int i;\n" 9735 "\n" 9736 "protected:\n" 9737 "\n" 9738 " int j;\n" 9739 "};\n", 9740 Style); 9741 9742 // Check if lines are added. 9743 verifyFormat("struct foo {\n" 9744 "private:\n" 9745 "\n" 9746 " void f() {}\n" 9747 "\n" 9748 "private:\n" 9749 "\n" 9750 " int i;\n" 9751 "\n" 9752 "protected:\n" 9753 "\n" 9754 " int j;\n" 9755 "};\n", 9756 "struct foo {\n" 9757 "private:\n" 9758 " void f() {}\n" 9759 "\n" 9760 "private:\n" 9761 " int i;\n" 9762 "\n" 9763 "protected:\n" 9764 " int j;\n" 9765 "};\n", 9766 Style); 9767 9768 // Leave tests rely on the code layout, test::messUp can not be used. 9769 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 9770 Style.MaxEmptyLinesToKeep = 0u; 9771 verifyFormat("struct foo {\n" 9772 "private:\n" 9773 " void f() {}\n" 9774 "\n" 9775 "private:\n" 9776 " int i;\n" 9777 "\n" 9778 "protected:\n" 9779 " int j;\n" 9780 "};\n", 9781 Style); 9782 9783 // Check if MaxEmptyLinesToKeep is respected. 9784 EXPECT_EQ("struct foo {\n" 9785 "private:\n" 9786 " void f() {}\n" 9787 "\n" 9788 "private:\n" 9789 " int i;\n" 9790 "\n" 9791 "protected:\n" 9792 " int j;\n" 9793 "};\n", 9794 format("struct foo {\n" 9795 "private:\n" 9796 "\n\n\n" 9797 " void f() {}\n" 9798 "\n" 9799 "private:\n" 9800 "\n\n\n" 9801 " int i;\n" 9802 "\n" 9803 "protected:\n" 9804 "\n\n\n" 9805 " int j;\n" 9806 "};\n", 9807 Style)); 9808 9809 Style.MaxEmptyLinesToKeep = 1u; 9810 EXPECT_EQ("struct foo {\n" 9811 "private:\n" 9812 "\n" 9813 " void f() {}\n" 9814 "\n" 9815 "private:\n" 9816 "\n" 9817 " int i;\n" 9818 "\n" 9819 "protected:\n" 9820 "\n" 9821 " int j;\n" 9822 "};\n", 9823 format("struct foo {\n" 9824 "private:\n" 9825 "\n" 9826 " void f() {}\n" 9827 "\n" 9828 "private:\n" 9829 "\n" 9830 " int i;\n" 9831 "\n" 9832 "protected:\n" 9833 "\n" 9834 " int j;\n" 9835 "};\n", 9836 Style)); 9837 // Check if no lines are kept. 9838 EXPECT_EQ("struct foo {\n" 9839 "private:\n" 9840 " void f() {}\n" 9841 "\n" 9842 "private:\n" 9843 " int i;\n" 9844 "\n" 9845 "protected:\n" 9846 " int j;\n" 9847 "};\n", 9848 format("struct foo {\n" 9849 "private:\n" 9850 " void f() {}\n" 9851 "\n" 9852 "private:\n" 9853 " int i;\n" 9854 "\n" 9855 "protected:\n" 9856 " int j;\n" 9857 "};\n", 9858 Style)); 9859 // Check if MaxEmptyLinesToKeep is respected. 9860 EXPECT_EQ("struct foo {\n" 9861 "private:\n" 9862 "\n" 9863 " void f() {}\n" 9864 "\n" 9865 "private:\n" 9866 "\n" 9867 " int i;\n" 9868 "\n" 9869 "protected:\n" 9870 "\n" 9871 " int j;\n" 9872 "};\n", 9873 format("struct foo {\n" 9874 "private:\n" 9875 "\n\n\n" 9876 " void f() {}\n" 9877 "\n" 9878 "private:\n" 9879 "\n\n\n" 9880 " int i;\n" 9881 "\n" 9882 "protected:\n" 9883 "\n\n\n" 9884 " int j;\n" 9885 "};\n", 9886 Style)); 9887 9888 Style.MaxEmptyLinesToKeep = 10u; 9889 EXPECT_EQ("struct foo {\n" 9890 "private:\n" 9891 "\n\n\n" 9892 " void f() {}\n" 9893 "\n" 9894 "private:\n" 9895 "\n\n\n" 9896 " int i;\n" 9897 "\n" 9898 "protected:\n" 9899 "\n\n\n" 9900 " int j;\n" 9901 "};\n", 9902 format("struct foo {\n" 9903 "private:\n" 9904 "\n\n\n" 9905 " void f() {}\n" 9906 "\n" 9907 "private:\n" 9908 "\n\n\n" 9909 " int i;\n" 9910 "\n" 9911 "protected:\n" 9912 "\n\n\n" 9913 " int j;\n" 9914 "};\n", 9915 Style)); 9916 9917 // Test with comments. 9918 Style = getLLVMStyle(); 9919 verifyFormat("struct foo {\n" 9920 "private:\n" 9921 " // comment\n" 9922 " void f() {}\n" 9923 "\n" 9924 "private: /* comment */\n" 9925 " int i;\n" 9926 "};\n", 9927 Style); 9928 verifyFormat("struct foo {\n" 9929 "private:\n" 9930 " // comment\n" 9931 " void f() {}\n" 9932 "\n" 9933 "private: /* comment */\n" 9934 " int i;\n" 9935 "};\n", 9936 "struct foo {\n" 9937 "private:\n" 9938 "\n" 9939 " // comment\n" 9940 " void f() {}\n" 9941 "\n" 9942 "private: /* comment */\n" 9943 "\n" 9944 " int i;\n" 9945 "};\n", 9946 Style); 9947 9948 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 9949 verifyFormat("struct foo {\n" 9950 "private:\n" 9951 "\n" 9952 " // comment\n" 9953 " void f() {}\n" 9954 "\n" 9955 "private: /* comment */\n" 9956 "\n" 9957 " int i;\n" 9958 "};\n", 9959 "struct foo {\n" 9960 "private:\n" 9961 " // comment\n" 9962 " void f() {}\n" 9963 "\n" 9964 "private: /* comment */\n" 9965 " int i;\n" 9966 "};\n", 9967 Style); 9968 verifyFormat("struct foo {\n" 9969 "private:\n" 9970 "\n" 9971 " // comment\n" 9972 " void f() {}\n" 9973 "\n" 9974 "private: /* comment */\n" 9975 "\n" 9976 " int i;\n" 9977 "};\n", 9978 Style); 9979 9980 // Test with preprocessor defines. 9981 Style = getLLVMStyle(); 9982 verifyFormat("struct foo {\n" 9983 "private:\n" 9984 "#ifdef FOO\n" 9985 "#endif\n" 9986 " void f() {}\n" 9987 "};\n", 9988 Style); 9989 verifyFormat("struct foo {\n" 9990 "private:\n" 9991 "#ifdef FOO\n" 9992 "#endif\n" 9993 " void f() {}\n" 9994 "};\n", 9995 "struct foo {\n" 9996 "private:\n" 9997 "\n" 9998 "#ifdef FOO\n" 9999 "#endif\n" 10000 " void f() {}\n" 10001 "};\n", 10002 Style); 10003 10004 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10005 verifyFormat("struct foo {\n" 10006 "private:\n" 10007 "\n" 10008 "#ifdef FOO\n" 10009 "#endif\n" 10010 " void f() {}\n" 10011 "};\n", 10012 "struct foo {\n" 10013 "private:\n" 10014 "#ifdef FOO\n" 10015 "#endif\n" 10016 " void f() {}\n" 10017 "};\n", 10018 Style); 10019 verifyFormat("struct foo {\n" 10020 "private:\n" 10021 "\n" 10022 "#ifdef FOO\n" 10023 "#endif\n" 10024 " void f() {}\n" 10025 "};\n", 10026 Style); 10027 } 10028 10029 TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) { 10030 // Combined tests of EmptyLineAfterAccessModifier and 10031 // EmptyLineBeforeAccessModifier. 10032 FormatStyle Style = getLLVMStyle(); 10033 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10034 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10035 verifyFormat("struct foo {\n" 10036 "private:\n" 10037 "\n" 10038 "protected:\n" 10039 "};\n", 10040 Style); 10041 10042 Style.MaxEmptyLinesToKeep = 10u; 10043 // Both remove all new lines. 10044 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10045 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 10046 verifyFormat("struct foo {\n" 10047 "private:\n" 10048 "protected:\n" 10049 "};\n", 10050 "struct foo {\n" 10051 "private:\n" 10052 "\n\n\n" 10053 "protected:\n" 10054 "};\n", 10055 Style); 10056 10057 // Leave tests rely on the code layout, test::messUp can not be used. 10058 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 10059 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 10060 Style.MaxEmptyLinesToKeep = 10u; 10061 EXPECT_EQ("struct foo {\n" 10062 "private:\n" 10063 "\n\n\n" 10064 "protected:\n" 10065 "};\n", 10066 format("struct foo {\n" 10067 "private:\n" 10068 "\n\n\n" 10069 "protected:\n" 10070 "};\n", 10071 Style)); 10072 Style.MaxEmptyLinesToKeep = 3u; 10073 EXPECT_EQ("struct foo {\n" 10074 "private:\n" 10075 "\n\n\n" 10076 "protected:\n" 10077 "};\n", 10078 format("struct foo {\n" 10079 "private:\n" 10080 "\n\n\n" 10081 "protected:\n" 10082 "};\n", 10083 Style)); 10084 Style.MaxEmptyLinesToKeep = 1u; 10085 EXPECT_EQ("struct foo {\n" 10086 "private:\n" 10087 "\n\n\n" 10088 "protected:\n" 10089 "};\n", 10090 format("struct foo {\n" 10091 "private:\n" 10092 "\n\n\n" 10093 "protected:\n" 10094 "};\n", 10095 Style)); // Based on new lines in original document and not 10096 // on the setting. 10097 10098 Style.MaxEmptyLinesToKeep = 10u; 10099 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10100 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 10101 // Newlines are kept if they are greater than zero, 10102 // test::messUp removes all new lines which changes the logic 10103 EXPECT_EQ("struct foo {\n" 10104 "private:\n" 10105 "\n\n\n" 10106 "protected:\n" 10107 "};\n", 10108 format("struct foo {\n" 10109 "private:\n" 10110 "\n\n\n" 10111 "protected:\n" 10112 "};\n", 10113 Style)); 10114 10115 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 10116 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10117 // test::messUp removes all new lines which changes the logic 10118 EXPECT_EQ("struct foo {\n" 10119 "private:\n" 10120 "\n\n\n" 10121 "protected:\n" 10122 "};\n", 10123 format("struct foo {\n" 10124 "private:\n" 10125 "\n\n\n" 10126 "protected:\n" 10127 "};\n", 10128 Style)); 10129 10130 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 10131 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 10132 EXPECT_EQ("struct foo {\n" 10133 "private:\n" 10134 "\n\n\n" 10135 "protected:\n" 10136 "};\n", 10137 format("struct foo {\n" 10138 "private:\n" 10139 "\n\n\n" 10140 "protected:\n" 10141 "};\n", 10142 Style)); // test::messUp removes all new lines which changes 10143 // the logic. 10144 10145 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10146 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 10147 verifyFormat("struct foo {\n" 10148 "private:\n" 10149 "protected:\n" 10150 "};\n", 10151 "struct foo {\n" 10152 "private:\n" 10153 "\n\n\n" 10154 "protected:\n" 10155 "};\n", 10156 Style); 10157 10158 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10159 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 10160 EXPECT_EQ("struct foo {\n" 10161 "private:\n" 10162 "\n\n\n" 10163 "protected:\n" 10164 "};\n", 10165 format("struct foo {\n" 10166 "private:\n" 10167 "\n\n\n" 10168 "protected:\n" 10169 "};\n", 10170 Style)); // test::messUp removes all new lines which changes 10171 // the logic. 10172 10173 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10174 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10175 verifyFormat("struct foo {\n" 10176 "private:\n" 10177 "protected:\n" 10178 "};\n", 10179 "struct foo {\n" 10180 "private:\n" 10181 "\n\n\n" 10182 "protected:\n" 10183 "};\n", 10184 Style); 10185 10186 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 10187 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10188 verifyFormat("struct foo {\n" 10189 "private:\n" 10190 "protected:\n" 10191 "};\n", 10192 "struct foo {\n" 10193 "private:\n" 10194 "\n\n\n" 10195 "protected:\n" 10196 "};\n", 10197 Style); 10198 10199 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 10200 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 10201 verifyFormat("struct foo {\n" 10202 "private:\n" 10203 "protected:\n" 10204 "};\n", 10205 "struct foo {\n" 10206 "private:\n" 10207 "\n\n\n" 10208 "protected:\n" 10209 "};\n", 10210 Style); 10211 10212 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 10213 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 10214 verifyFormat("struct foo {\n" 10215 "private:\n" 10216 "protected:\n" 10217 "};\n", 10218 "struct foo {\n" 10219 "private:\n" 10220 "\n\n\n" 10221 "protected:\n" 10222 "};\n", 10223 Style); 10224 } 10225 10226 TEST_F(FormatTest, FormatsArrays) { 10227 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 10228 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 10229 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 10230 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 10231 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 10232 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 10233 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10234 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 10235 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10236 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 10237 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10238 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 10239 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 10240 verifyFormat( 10241 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 10242 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 10243 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 10244 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" 10245 " .aaaaaaaaaaaaaaaaaaaaaa();"); 10246 10247 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 10248 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 10249 verifyFormat( 10250 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 10251 " .aaaaaaa[0]\n" 10252 " .aaaaaaaaaaaaaaaaaaaaaa();"); 10253 verifyFormat("a[::b::c];"); 10254 10255 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 10256 10257 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 10258 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 10259 } 10260 10261 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 10262 verifyFormat("(a)->b();"); 10263 verifyFormat("--a;"); 10264 } 10265 10266 TEST_F(FormatTest, HandlesIncludeDirectives) { 10267 verifyFormat("#include <string>\n" 10268 "#include <a/b/c.h>\n" 10269 "#include \"a/b/string\"\n" 10270 "#include \"string.h\"\n" 10271 "#include \"string.h\"\n" 10272 "#include <a-a>\n" 10273 "#include < path with space >\n" 10274 "#include_next <test.h>" 10275 "#include \"abc.h\" // this is included for ABC\n" 10276 "#include \"some long include\" // with a comment\n" 10277 "#include \"some very long include path\"\n" 10278 "#include <some/very/long/include/path>\n", 10279 getLLVMStyleWithColumns(35)); 10280 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 10281 EXPECT_EQ("#include <a>", format("#include<a>")); 10282 10283 verifyFormat("#import <string>"); 10284 verifyFormat("#import <a/b/c.h>"); 10285 verifyFormat("#import \"a/b/string\""); 10286 verifyFormat("#import \"string.h\""); 10287 verifyFormat("#import \"string.h\""); 10288 verifyFormat("#if __has_include(<strstream>)\n" 10289 "#include <strstream>\n" 10290 "#endif"); 10291 10292 verifyFormat("#define MY_IMPORT <a/b>"); 10293 10294 verifyFormat("#if __has_include(<a/b>)"); 10295 verifyFormat("#if __has_include_next(<a/b>)"); 10296 verifyFormat("#define F __has_include(<a/b>)"); 10297 verifyFormat("#define F __has_include_next(<a/b>)"); 10298 10299 // Protocol buffer definition or missing "#". 10300 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 10301 getLLVMStyleWithColumns(30)); 10302 10303 FormatStyle Style = getLLVMStyle(); 10304 Style.AlwaysBreakBeforeMultilineStrings = true; 10305 Style.ColumnLimit = 0; 10306 verifyFormat("#import \"abc.h\"", Style); 10307 10308 // But 'import' might also be a regular C++ namespace. 10309 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 10310 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 10311 } 10312 10313 //===----------------------------------------------------------------------===// 10314 // Error recovery tests. 10315 //===----------------------------------------------------------------------===// 10316 10317 TEST_F(FormatTest, IncompleteParameterLists) { 10318 FormatStyle NoBinPacking = getLLVMStyle(); 10319 NoBinPacking.BinPackParameters = false; 10320 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 10321 " double *min_x,\n" 10322 " double *max_x,\n" 10323 " double *min_y,\n" 10324 " double *max_y,\n" 10325 " double *min_z,\n" 10326 " double *max_z, ) {}", 10327 NoBinPacking); 10328 } 10329 10330 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 10331 verifyFormat("void f() { return; }\n42"); 10332 verifyFormat("void f() {\n" 10333 " if (0)\n" 10334 " return;\n" 10335 "}\n" 10336 "42"); 10337 verifyFormat("void f() { return }\n42"); 10338 verifyFormat("void f() {\n" 10339 " if (0)\n" 10340 " return\n" 10341 "}\n" 10342 "42"); 10343 } 10344 10345 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 10346 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 10347 EXPECT_EQ("void f() {\n" 10348 " if (a)\n" 10349 " return\n" 10350 "}", 10351 format("void f ( ) { if ( a ) return }")); 10352 EXPECT_EQ("namespace N {\n" 10353 "void f()\n" 10354 "}", 10355 format("namespace N { void f() }")); 10356 EXPECT_EQ("namespace N {\n" 10357 "void f() {}\n" 10358 "void g()\n" 10359 "} // namespace N", 10360 format("namespace N { void f( ) { } void g( ) }")); 10361 } 10362 10363 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 10364 verifyFormat("int aaaaaaaa =\n" 10365 " // Overlylongcomment\n" 10366 " b;", 10367 getLLVMStyleWithColumns(20)); 10368 verifyFormat("function(\n" 10369 " ShortArgument,\n" 10370 " LoooooooooooongArgument);\n", 10371 getLLVMStyleWithColumns(20)); 10372 } 10373 10374 TEST_F(FormatTest, IncorrectAccessSpecifier) { 10375 verifyFormat("public:"); 10376 verifyFormat("class A {\n" 10377 "public\n" 10378 " void f() {}\n" 10379 "};"); 10380 verifyFormat("public\n" 10381 "int qwerty;"); 10382 verifyFormat("public\n" 10383 "B {}"); 10384 verifyFormat("public\n" 10385 "{}"); 10386 verifyFormat("public\n" 10387 "B { int x; }"); 10388 } 10389 10390 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 10391 verifyFormat("{"); 10392 verifyFormat("#})"); 10393 verifyNoCrash("(/**/[:!] ?[)."); 10394 } 10395 10396 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) { 10397 // Found by oss-fuzz: 10398 // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212 10399 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 10400 Style.ColumnLimit = 60; 10401 verifyNoCrash( 10402 "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20" 10403 "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20" 10404 "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a", 10405 Style); 10406 } 10407 10408 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 10409 verifyFormat("do {\n}"); 10410 verifyFormat("do {\n}\n" 10411 "f();"); 10412 verifyFormat("do {\n}\n" 10413 "wheeee(fun);"); 10414 verifyFormat("do {\n" 10415 " f();\n" 10416 "}"); 10417 } 10418 10419 TEST_F(FormatTest, IncorrectCodeMissingParens) { 10420 verifyFormat("if {\n foo;\n foo();\n}"); 10421 verifyFormat("switch {\n foo;\n foo();\n}"); 10422 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 10423 verifyFormat("while {\n foo;\n foo();\n}"); 10424 verifyFormat("do {\n foo;\n foo();\n} while;"); 10425 } 10426 10427 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 10428 verifyIncompleteFormat("namespace {\n" 10429 "class Foo { Foo (\n" 10430 "};\n" 10431 "} // namespace"); 10432 } 10433 10434 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 10435 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 10436 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 10437 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 10438 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 10439 10440 EXPECT_EQ("{\n" 10441 " {\n" 10442 " breakme(\n" 10443 " qwe);\n" 10444 " }\n", 10445 format("{\n" 10446 " {\n" 10447 " breakme(qwe);\n" 10448 "}\n", 10449 getLLVMStyleWithColumns(10))); 10450 } 10451 10452 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 10453 verifyFormat("int x = {\n" 10454 " avariable,\n" 10455 " b(alongervariable)};", 10456 getLLVMStyleWithColumns(25)); 10457 } 10458 10459 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 10460 verifyFormat("return (a)(b){1, 2, 3};"); 10461 } 10462 10463 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 10464 verifyFormat("vector<int> x{1, 2, 3, 4};"); 10465 verifyFormat("vector<int> x{\n" 10466 " 1,\n" 10467 " 2,\n" 10468 " 3,\n" 10469 " 4,\n" 10470 "};"); 10471 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 10472 verifyFormat("f({1, 2});"); 10473 verifyFormat("auto v = Foo{-1};"); 10474 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 10475 verifyFormat("Class::Class : member{1, 2, 3} {}"); 10476 verifyFormat("new vector<int>{1, 2, 3};"); 10477 verifyFormat("new int[3]{1, 2, 3};"); 10478 verifyFormat("new int{1};"); 10479 verifyFormat("return {arg1, arg2};"); 10480 verifyFormat("return {arg1, SomeType{parameter}};"); 10481 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 10482 verifyFormat("new T{arg1, arg2};"); 10483 verifyFormat("f(MyMap[{composite, key}]);"); 10484 verifyFormat("class Class {\n" 10485 " T member = {arg1, arg2};\n" 10486 "};"); 10487 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 10488 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 10489 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 10490 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 10491 verifyFormat("int a = std::is_integral<int>{} + 0;"); 10492 10493 verifyFormat("int foo(int i) { return fo1{}(i); }"); 10494 verifyFormat("int foo(int i) { return fo1{}(i); }"); 10495 verifyFormat("auto i = decltype(x){};"); 10496 verifyFormat("auto i = typeof(x){};"); 10497 verifyFormat("auto i = _Atomic(x){};"); 10498 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 10499 verifyFormat("Node n{1, Node{1000}, //\n" 10500 " 2};"); 10501 verifyFormat("Aaaa aaaaaaa{\n" 10502 " {\n" 10503 " aaaa,\n" 10504 " },\n" 10505 "};"); 10506 verifyFormat("class C : public D {\n" 10507 " SomeClass SC{2};\n" 10508 "};"); 10509 verifyFormat("class C : public A {\n" 10510 " class D : public B {\n" 10511 " void f() { int i{2}; }\n" 10512 " };\n" 10513 "};"); 10514 verifyFormat("#define A {a, a},"); 10515 10516 // Avoid breaking between equal sign and opening brace 10517 FormatStyle AvoidBreakingFirstArgument = getLLVMStyle(); 10518 AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200; 10519 verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n" 10520 " {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n" 10521 " {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n" 10522 " {\"ccccccccccccccccccccc\", 2}};", 10523 AvoidBreakingFirstArgument); 10524 10525 // Binpacking only if there is no trailing comma 10526 verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n" 10527 " cccccccccc, dddddddddd};", 10528 getLLVMStyleWithColumns(50)); 10529 verifyFormat("const Aaaaaa aaaaa = {\n" 10530 " aaaaaaaaaaa,\n" 10531 " bbbbbbbbbbb,\n" 10532 " ccccccccccc,\n" 10533 " ddddddddddd,\n" 10534 "};", 10535 getLLVMStyleWithColumns(50)); 10536 10537 // Cases where distinguising braced lists and blocks is hard. 10538 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);"); 10539 verifyFormat("void f() {\n" 10540 " return; // comment\n" 10541 "}\n" 10542 "SomeType t;"); 10543 verifyFormat("void f() {\n" 10544 " if (a) {\n" 10545 " f();\n" 10546 " }\n" 10547 "}\n" 10548 "SomeType t;"); 10549 10550 // In combination with BinPackArguments = false. 10551 FormatStyle NoBinPacking = getLLVMStyle(); 10552 NoBinPacking.BinPackArguments = false; 10553 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 10554 " bbbbb,\n" 10555 " ccccc,\n" 10556 " ddddd,\n" 10557 " eeeee,\n" 10558 " ffffff,\n" 10559 " ggggg,\n" 10560 " hhhhhh,\n" 10561 " iiiiii,\n" 10562 " jjjjjj,\n" 10563 " kkkkkk};", 10564 NoBinPacking); 10565 verifyFormat("const Aaaaaa aaaaa = {\n" 10566 " aaaaa,\n" 10567 " bbbbb,\n" 10568 " ccccc,\n" 10569 " ddddd,\n" 10570 " eeeee,\n" 10571 " ffffff,\n" 10572 " ggggg,\n" 10573 " hhhhhh,\n" 10574 " iiiiii,\n" 10575 " jjjjjj,\n" 10576 " kkkkkk,\n" 10577 "};", 10578 NoBinPacking); 10579 verifyFormat( 10580 "const Aaaaaa aaaaa = {\n" 10581 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 10582 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 10583 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 10584 "};", 10585 NoBinPacking); 10586 10587 NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 10588 EXPECT_EQ("static uint8 CddDp83848Reg[] = {\n" 10589 " CDDDP83848_BMCR_REGISTER,\n" 10590 " CDDDP83848_BMSR_REGISTER,\n" 10591 " CDDDP83848_RBR_REGISTER};", 10592 format("static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n" 10593 " CDDDP83848_BMSR_REGISTER,\n" 10594 " CDDDP83848_RBR_REGISTER};", 10595 NoBinPacking)); 10596 10597 // FIXME: The alignment of these trailing comments might be bad. Then again, 10598 // this might be utterly useless in real code. 10599 verifyFormat("Constructor::Constructor()\n" 10600 " : some_value{ //\n" 10601 " aaaaaaa, //\n" 10602 " bbbbbbb} {}"); 10603 10604 // In braced lists, the first comment is always assumed to belong to the 10605 // first element. Thus, it can be moved to the next or previous line as 10606 // appropriate. 10607 EXPECT_EQ("function({// First element:\n" 10608 " 1,\n" 10609 " // Second element:\n" 10610 " 2});", 10611 format("function({\n" 10612 " // First element:\n" 10613 " 1,\n" 10614 " // Second element:\n" 10615 " 2});")); 10616 EXPECT_EQ("std::vector<int> MyNumbers{\n" 10617 " // First element:\n" 10618 " 1,\n" 10619 " // Second element:\n" 10620 " 2};", 10621 format("std::vector<int> MyNumbers{// First element:\n" 10622 " 1,\n" 10623 " // Second element:\n" 10624 " 2};", 10625 getLLVMStyleWithColumns(30))); 10626 // A trailing comma should still lead to an enforced line break and no 10627 // binpacking. 10628 EXPECT_EQ("vector<int> SomeVector = {\n" 10629 " // aaa\n" 10630 " 1,\n" 10631 " 2,\n" 10632 "};", 10633 format("vector<int> SomeVector = { // aaa\n" 10634 " 1, 2, };")); 10635 10636 // C++11 brace initializer list l-braces should not be treated any differently 10637 // when breaking before lambda bodies is enabled 10638 FormatStyle BreakBeforeLambdaBody = getLLVMStyle(); 10639 BreakBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 10640 BreakBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 10641 BreakBeforeLambdaBody.AlwaysBreakBeforeMultilineStrings = true; 10642 verifyFormat( 10643 "std::runtime_error{\n" 10644 " \"Long string which will force a break onto the next line...\"};", 10645 BreakBeforeLambdaBody); 10646 10647 FormatStyle ExtraSpaces = getLLVMStyle(); 10648 ExtraSpaces.Cpp11BracedListStyle = false; 10649 ExtraSpaces.ColumnLimit = 75; 10650 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 10651 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 10652 verifyFormat("f({ 1, 2 });", ExtraSpaces); 10653 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 10654 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 10655 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 10656 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 10657 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 10658 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 10659 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 10660 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 10661 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 10662 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 10663 verifyFormat("class Class {\n" 10664 " T member = { arg1, arg2 };\n" 10665 "};", 10666 ExtraSpaces); 10667 verifyFormat( 10668 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 10669 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 10670 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 10671 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 10672 ExtraSpaces); 10673 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 10674 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 10675 ExtraSpaces); 10676 verifyFormat( 10677 "someFunction(OtherParam,\n" 10678 " BracedList{ // comment 1 (Forcing interesting break)\n" 10679 " param1, param2,\n" 10680 " // comment 2\n" 10681 " param3, param4 });", 10682 ExtraSpaces); 10683 verifyFormat( 10684 "std::this_thread::sleep_for(\n" 10685 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 10686 ExtraSpaces); 10687 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n" 10688 " aaaaaaa,\n" 10689 " aaaaaaaaaa,\n" 10690 " aaaaa,\n" 10691 " aaaaaaaaaaaaaaa,\n" 10692 " aaa,\n" 10693 " aaaaaaaaaa,\n" 10694 " a,\n" 10695 " aaaaaaaaaaaaaaaaaaaaa,\n" 10696 " aaaaaaaaaaaa,\n" 10697 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 10698 " aaaaaaa,\n" 10699 " a};"); 10700 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 10701 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces); 10702 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces); 10703 10704 // Avoid breaking between initializer/equal sign and opening brace 10705 ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200; 10706 verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n" 10707 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 10708 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 10709 " { \"ccccccccccccccccccccc\", 2 }\n" 10710 "};", 10711 ExtraSpaces); 10712 verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n" 10713 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 10714 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 10715 " { \"ccccccccccccccccccccc\", 2 }\n" 10716 "};", 10717 ExtraSpaces); 10718 10719 FormatStyle SpaceBeforeBrace = getLLVMStyle(); 10720 SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true; 10721 verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace); 10722 verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace); 10723 10724 FormatStyle SpaceBetweenBraces = getLLVMStyle(); 10725 SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always; 10726 SpaceBetweenBraces.SpacesInParentheses = true; 10727 SpaceBetweenBraces.SpacesInSquareBrackets = true; 10728 verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces); 10729 verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces); 10730 verifyFormat("vector< int > x{ // comment 1\n" 10731 " 1, 2, 3, 4 };", 10732 SpaceBetweenBraces); 10733 SpaceBetweenBraces.ColumnLimit = 20; 10734 EXPECT_EQ("vector< int > x{\n" 10735 " 1, 2, 3, 4 };", 10736 format("vector<int>x{1,2,3,4};", SpaceBetweenBraces)); 10737 SpaceBetweenBraces.ColumnLimit = 24; 10738 EXPECT_EQ("vector< int > x{ 1, 2,\n" 10739 " 3, 4 };", 10740 format("vector<int>x{1,2,3,4};", SpaceBetweenBraces)); 10741 EXPECT_EQ("vector< int > x{\n" 10742 " 1,\n" 10743 " 2,\n" 10744 " 3,\n" 10745 " 4,\n" 10746 "};", 10747 format("vector<int>x{1,2,3,4,};", SpaceBetweenBraces)); 10748 verifyFormat("vector< int > x{};", SpaceBetweenBraces); 10749 SpaceBetweenBraces.SpaceInEmptyParentheses = true; 10750 verifyFormat("vector< int > x{ };", SpaceBetweenBraces); 10751 } 10752 10753 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 10754 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 10755 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 10756 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 10757 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 10758 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 10759 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 10760 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 10761 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 10762 " 1, 22, 333, 4444, 55555, //\n" 10763 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 10764 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 10765 verifyFormat( 10766 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 10767 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 10768 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 10769 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 10770 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 10771 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 10772 " 7777777};"); 10773 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 10774 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 10775 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 10776 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 10777 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 10778 " // Separating comment.\n" 10779 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 10780 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 10781 " // Leading comment\n" 10782 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 10783 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 10784 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 10785 " 1, 1, 1, 1};", 10786 getLLVMStyleWithColumns(39)); 10787 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 10788 " 1, 1, 1, 1};", 10789 getLLVMStyleWithColumns(38)); 10790 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 10791 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 10792 getLLVMStyleWithColumns(43)); 10793 verifyFormat( 10794 "static unsigned SomeValues[10][3] = {\n" 10795 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 10796 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 10797 verifyFormat("static auto fields = new vector<string>{\n" 10798 " \"aaaaaaaaaaaaa\",\n" 10799 " \"aaaaaaaaaaaaa\",\n" 10800 " \"aaaaaaaaaaaa\",\n" 10801 " \"aaaaaaaaaaaaaa\",\n" 10802 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 10803 " \"aaaaaaaaaaaa\",\n" 10804 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 10805 "};"); 10806 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 10807 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 10808 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 10809 " 3, cccccccccccccccccccccc};", 10810 getLLVMStyleWithColumns(60)); 10811 10812 // Trailing commas. 10813 verifyFormat("vector<int> x = {\n" 10814 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 10815 "};", 10816 getLLVMStyleWithColumns(39)); 10817 verifyFormat("vector<int> x = {\n" 10818 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 10819 "};", 10820 getLLVMStyleWithColumns(39)); 10821 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 10822 " 1, 1, 1, 1,\n" 10823 " /**/ /**/};", 10824 getLLVMStyleWithColumns(39)); 10825 10826 // Trailing comment in the first line. 10827 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 10828 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 10829 " 111111111, 222222222, 3333333333, 444444444, //\n" 10830 " 11111111, 22222222, 333333333, 44444444};"); 10831 // Trailing comment in the last line. 10832 verifyFormat("int aaaaa[] = {\n" 10833 " 1, 2, 3, // comment\n" 10834 " 4, 5, 6 // comment\n" 10835 "};"); 10836 10837 // With nested lists, we should either format one item per line or all nested 10838 // lists one on line. 10839 // FIXME: For some nested lists, we can do better. 10840 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 10841 " {aaaaaaaaaaaaaaaaaaa},\n" 10842 " {aaaaaaaaaaaaaaaaaaaaa},\n" 10843 " {aaaaaaaaaaaaaaaaa}};", 10844 getLLVMStyleWithColumns(60)); 10845 verifyFormat( 10846 "SomeStruct my_struct_array = {\n" 10847 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 10848 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 10849 " {aaa, aaa},\n" 10850 " {aaa, aaa},\n" 10851 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 10852 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 10853 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 10854 10855 // No column layout should be used here. 10856 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 10857 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 10858 10859 verifyNoCrash("a<,"); 10860 10861 // No braced initializer here. 10862 verifyFormat("void f() {\n" 10863 " struct Dummy {};\n" 10864 " f(v);\n" 10865 "}"); 10866 10867 // Long lists should be formatted in columns even if they are nested. 10868 verifyFormat( 10869 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 10870 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 10871 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 10872 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 10873 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 10874 " 1, 22, 333, 4444, 55555, 666666, 7777777});"); 10875 10876 // Allow "single-column" layout even if that violates the column limit. There 10877 // isn't going to be a better way. 10878 verifyFormat("std::vector<int> a = {\n" 10879 " aaaaaaaa,\n" 10880 " aaaaaaaa,\n" 10881 " aaaaaaaa,\n" 10882 " aaaaaaaa,\n" 10883 " aaaaaaaaaa,\n" 10884 " aaaaaaaa,\n" 10885 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};", 10886 getLLVMStyleWithColumns(30)); 10887 verifyFormat("vector<int> aaaa = {\n" 10888 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 10889 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 10890 " aaaaaa.aaaaaaa,\n" 10891 " aaaaaa.aaaaaaa,\n" 10892 " aaaaaa.aaaaaaa,\n" 10893 " aaaaaa.aaaaaaa,\n" 10894 "};"); 10895 10896 // Don't create hanging lists. 10897 verifyFormat("someFunction(Param, {List1, List2,\n" 10898 " List3});", 10899 getLLVMStyleWithColumns(35)); 10900 verifyFormat("someFunction(Param, Param,\n" 10901 " {List1, List2,\n" 10902 " List3});", 10903 getLLVMStyleWithColumns(35)); 10904 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n" 10905 " aaaaaaaaaaaaaaaaaaaaaaa);"); 10906 } 10907 10908 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 10909 FormatStyle DoNotMerge = getLLVMStyle(); 10910 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 10911 10912 verifyFormat("void f() { return 42; }"); 10913 verifyFormat("void f() {\n" 10914 " return 42;\n" 10915 "}", 10916 DoNotMerge); 10917 verifyFormat("void f() {\n" 10918 " // Comment\n" 10919 "}"); 10920 verifyFormat("{\n" 10921 "#error {\n" 10922 " int a;\n" 10923 "}"); 10924 verifyFormat("{\n" 10925 " int a;\n" 10926 "#error {\n" 10927 "}"); 10928 verifyFormat("void f() {} // comment"); 10929 verifyFormat("void f() { int a; } // comment"); 10930 verifyFormat("void f() {\n" 10931 "} // comment", 10932 DoNotMerge); 10933 verifyFormat("void f() {\n" 10934 " int a;\n" 10935 "} // comment", 10936 DoNotMerge); 10937 verifyFormat("void f() {\n" 10938 "} // comment", 10939 getLLVMStyleWithColumns(15)); 10940 10941 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 10942 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 10943 10944 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 10945 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 10946 verifyFormat("class C {\n" 10947 " C()\n" 10948 " : iiiiiiii(nullptr),\n" 10949 " kkkkkkk(nullptr),\n" 10950 " mmmmmmm(nullptr),\n" 10951 " nnnnnnn(nullptr) {}\n" 10952 "};", 10953 getGoogleStyle()); 10954 10955 FormatStyle NoColumnLimit = getLLVMStyle(); 10956 NoColumnLimit.ColumnLimit = 0; 10957 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 10958 EXPECT_EQ("class C {\n" 10959 " A() : b(0) {}\n" 10960 "};", 10961 format("class C{A():b(0){}};", NoColumnLimit)); 10962 EXPECT_EQ("A()\n" 10963 " : b(0) {\n" 10964 "}", 10965 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 10966 10967 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 10968 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 10969 FormatStyle::SFS_None; 10970 EXPECT_EQ("A()\n" 10971 " : b(0) {\n" 10972 "}", 10973 format("A():b(0){}", DoNotMergeNoColumnLimit)); 10974 EXPECT_EQ("A()\n" 10975 " : b(0) {\n" 10976 "}", 10977 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 10978 10979 verifyFormat("#define A \\\n" 10980 " void f() { \\\n" 10981 " int i; \\\n" 10982 " }", 10983 getLLVMStyleWithColumns(20)); 10984 verifyFormat("#define A \\\n" 10985 " void f() { int i; }", 10986 getLLVMStyleWithColumns(21)); 10987 verifyFormat("#define A \\\n" 10988 " void f() { \\\n" 10989 " int i; \\\n" 10990 " } \\\n" 10991 " int j;", 10992 getLLVMStyleWithColumns(22)); 10993 verifyFormat("#define A \\\n" 10994 " void f() { int i; } \\\n" 10995 " int j;", 10996 getLLVMStyleWithColumns(23)); 10997 } 10998 10999 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) { 11000 FormatStyle MergeEmptyOnly = getLLVMStyle(); 11001 MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 11002 verifyFormat("class C {\n" 11003 " int f() {}\n" 11004 "};", 11005 MergeEmptyOnly); 11006 verifyFormat("class C {\n" 11007 " int f() {\n" 11008 " return 42;\n" 11009 " }\n" 11010 "};", 11011 MergeEmptyOnly); 11012 verifyFormat("int f() {}", MergeEmptyOnly); 11013 verifyFormat("int f() {\n" 11014 " return 42;\n" 11015 "}", 11016 MergeEmptyOnly); 11017 11018 // Also verify behavior when BraceWrapping.AfterFunction = true 11019 MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 11020 MergeEmptyOnly.BraceWrapping.AfterFunction = true; 11021 verifyFormat("int f() {}", MergeEmptyOnly); 11022 verifyFormat("class C {\n" 11023 " int f() {}\n" 11024 "};", 11025 MergeEmptyOnly); 11026 } 11027 11028 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 11029 FormatStyle MergeInlineOnly = getLLVMStyle(); 11030 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 11031 verifyFormat("class C {\n" 11032 " int f() { return 42; }\n" 11033 "};", 11034 MergeInlineOnly); 11035 verifyFormat("int f() {\n" 11036 " return 42;\n" 11037 "}", 11038 MergeInlineOnly); 11039 11040 // SFS_Inline implies SFS_Empty 11041 verifyFormat("class C {\n" 11042 " int f() {}\n" 11043 "};", 11044 MergeInlineOnly); 11045 verifyFormat("int f() {}", MergeInlineOnly); 11046 11047 // Also verify behavior when BraceWrapping.AfterFunction = true 11048 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 11049 MergeInlineOnly.BraceWrapping.AfterFunction = true; 11050 verifyFormat("class C {\n" 11051 " int f() { return 42; }\n" 11052 "};", 11053 MergeInlineOnly); 11054 verifyFormat("int f()\n" 11055 "{\n" 11056 " return 42;\n" 11057 "}", 11058 MergeInlineOnly); 11059 11060 // SFS_Inline implies SFS_Empty 11061 verifyFormat("int f() {}", MergeInlineOnly); 11062 verifyFormat("class C {\n" 11063 " int f() {}\n" 11064 "};", 11065 MergeInlineOnly); 11066 } 11067 11068 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) { 11069 FormatStyle MergeInlineOnly = getLLVMStyle(); 11070 MergeInlineOnly.AllowShortFunctionsOnASingleLine = 11071 FormatStyle::SFS_InlineOnly; 11072 verifyFormat("class C {\n" 11073 " int f() { return 42; }\n" 11074 "};", 11075 MergeInlineOnly); 11076 verifyFormat("int f() {\n" 11077 " return 42;\n" 11078 "}", 11079 MergeInlineOnly); 11080 11081 // SFS_InlineOnly does not imply SFS_Empty 11082 verifyFormat("class C {\n" 11083 " int f() {}\n" 11084 "};", 11085 MergeInlineOnly); 11086 verifyFormat("int f() {\n" 11087 "}", 11088 MergeInlineOnly); 11089 11090 // Also verify behavior when BraceWrapping.AfterFunction = true 11091 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 11092 MergeInlineOnly.BraceWrapping.AfterFunction = true; 11093 verifyFormat("class C {\n" 11094 " int f() { return 42; }\n" 11095 "};", 11096 MergeInlineOnly); 11097 verifyFormat("int f()\n" 11098 "{\n" 11099 " return 42;\n" 11100 "}", 11101 MergeInlineOnly); 11102 11103 // SFS_InlineOnly does not imply SFS_Empty 11104 verifyFormat("int f()\n" 11105 "{\n" 11106 "}", 11107 MergeInlineOnly); 11108 verifyFormat("class C {\n" 11109 " int f() {}\n" 11110 "};", 11111 MergeInlineOnly); 11112 } 11113 11114 TEST_F(FormatTest, SplitEmptyFunction) { 11115 FormatStyle Style = getLLVMStyle(); 11116 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 11117 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 11118 Style.BraceWrapping.AfterFunction = true; 11119 Style.BraceWrapping.SplitEmptyFunction = false; 11120 Style.ColumnLimit = 40; 11121 11122 verifyFormat("int f()\n" 11123 "{}", 11124 Style); 11125 verifyFormat("int f()\n" 11126 "{\n" 11127 " return 42;\n" 11128 "}", 11129 Style); 11130 verifyFormat("int f()\n" 11131 "{\n" 11132 " // some comment\n" 11133 "}", 11134 Style); 11135 11136 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 11137 verifyFormat("int f() {}", Style); 11138 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 11139 "{}", 11140 Style); 11141 verifyFormat("int f()\n" 11142 "{\n" 11143 " return 0;\n" 11144 "}", 11145 Style); 11146 11147 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 11148 verifyFormat("class Foo {\n" 11149 " int f() {}\n" 11150 "};\n", 11151 Style); 11152 verifyFormat("class Foo {\n" 11153 " int f() { return 0; }\n" 11154 "};\n", 11155 Style); 11156 verifyFormat("class Foo {\n" 11157 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 11158 " {}\n" 11159 "};\n", 11160 Style); 11161 verifyFormat("class Foo {\n" 11162 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 11163 " {\n" 11164 " return 0;\n" 11165 " }\n" 11166 "};\n", 11167 Style); 11168 11169 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 11170 verifyFormat("int f() {}", Style); 11171 verifyFormat("int f() { return 0; }", Style); 11172 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 11173 "{}", 11174 Style); 11175 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 11176 "{\n" 11177 " return 0;\n" 11178 "}", 11179 Style); 11180 } 11181 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) { 11182 FormatStyle Style = getLLVMStyle(); 11183 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 11184 verifyFormat("#ifdef A\n" 11185 "int f() {}\n" 11186 "#else\n" 11187 "int g() {}\n" 11188 "#endif", 11189 Style); 11190 } 11191 11192 TEST_F(FormatTest, SplitEmptyClass) { 11193 FormatStyle Style = getLLVMStyle(); 11194 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 11195 Style.BraceWrapping.AfterClass = true; 11196 Style.BraceWrapping.SplitEmptyRecord = false; 11197 11198 verifyFormat("class Foo\n" 11199 "{};", 11200 Style); 11201 verifyFormat("/* something */ class Foo\n" 11202 "{};", 11203 Style); 11204 verifyFormat("template <typename X> class Foo\n" 11205 "{};", 11206 Style); 11207 verifyFormat("class Foo\n" 11208 "{\n" 11209 " Foo();\n" 11210 "};", 11211 Style); 11212 verifyFormat("typedef class Foo\n" 11213 "{\n" 11214 "} Foo_t;", 11215 Style); 11216 11217 Style.BraceWrapping.SplitEmptyRecord = true; 11218 Style.BraceWrapping.AfterStruct = true; 11219 verifyFormat("class rep\n" 11220 "{\n" 11221 "};", 11222 Style); 11223 verifyFormat("struct rep\n" 11224 "{\n" 11225 "};", 11226 Style); 11227 verifyFormat("template <typename T> class rep\n" 11228 "{\n" 11229 "};", 11230 Style); 11231 verifyFormat("template <typename T> struct rep\n" 11232 "{\n" 11233 "};", 11234 Style); 11235 verifyFormat("class rep\n" 11236 "{\n" 11237 " int x;\n" 11238 "};", 11239 Style); 11240 verifyFormat("struct rep\n" 11241 "{\n" 11242 " int x;\n" 11243 "};", 11244 Style); 11245 verifyFormat("template <typename T> class rep\n" 11246 "{\n" 11247 " int x;\n" 11248 "};", 11249 Style); 11250 verifyFormat("template <typename T> struct rep\n" 11251 "{\n" 11252 " int x;\n" 11253 "};", 11254 Style); 11255 verifyFormat("template <typename T> class rep // Foo\n" 11256 "{\n" 11257 " int x;\n" 11258 "};", 11259 Style); 11260 verifyFormat("template <typename T> struct rep // Bar\n" 11261 "{\n" 11262 " int x;\n" 11263 "};", 11264 Style); 11265 11266 verifyFormat("template <typename T> class rep<T>\n" 11267 "{\n" 11268 " int x;\n" 11269 "};", 11270 Style); 11271 11272 verifyFormat("template <typename T> class rep<std::complex<T>>\n" 11273 "{\n" 11274 " int x;\n" 11275 "};", 11276 Style); 11277 verifyFormat("template <typename T> class rep<std::complex<T>>\n" 11278 "{\n" 11279 "};", 11280 Style); 11281 11282 verifyFormat("#include \"stdint.h\"\n" 11283 "namespace rep {}", 11284 Style); 11285 verifyFormat("#include <stdint.h>\n" 11286 "namespace rep {}", 11287 Style); 11288 verifyFormat("#include <stdint.h>\n" 11289 "namespace rep {}", 11290 "#include <stdint.h>\n" 11291 "namespace rep {\n" 11292 "\n" 11293 "\n" 11294 "}", 11295 Style); 11296 } 11297 11298 TEST_F(FormatTest, SplitEmptyStruct) { 11299 FormatStyle Style = getLLVMStyle(); 11300 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 11301 Style.BraceWrapping.AfterStruct = true; 11302 Style.BraceWrapping.SplitEmptyRecord = false; 11303 11304 verifyFormat("struct Foo\n" 11305 "{};", 11306 Style); 11307 verifyFormat("/* something */ struct Foo\n" 11308 "{};", 11309 Style); 11310 verifyFormat("template <typename X> struct Foo\n" 11311 "{};", 11312 Style); 11313 verifyFormat("struct Foo\n" 11314 "{\n" 11315 " Foo();\n" 11316 "};", 11317 Style); 11318 verifyFormat("typedef struct Foo\n" 11319 "{\n" 11320 "} Foo_t;", 11321 Style); 11322 // typedef struct Bar {} Bar_t; 11323 } 11324 11325 TEST_F(FormatTest, SplitEmptyUnion) { 11326 FormatStyle Style = getLLVMStyle(); 11327 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 11328 Style.BraceWrapping.AfterUnion = true; 11329 Style.BraceWrapping.SplitEmptyRecord = false; 11330 11331 verifyFormat("union Foo\n" 11332 "{};", 11333 Style); 11334 verifyFormat("/* something */ union Foo\n" 11335 "{};", 11336 Style); 11337 verifyFormat("union Foo\n" 11338 "{\n" 11339 " A,\n" 11340 "};", 11341 Style); 11342 verifyFormat("typedef union Foo\n" 11343 "{\n" 11344 "} Foo_t;", 11345 Style); 11346 } 11347 11348 TEST_F(FormatTest, SplitEmptyNamespace) { 11349 FormatStyle Style = getLLVMStyle(); 11350 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 11351 Style.BraceWrapping.AfterNamespace = true; 11352 Style.BraceWrapping.SplitEmptyNamespace = false; 11353 11354 verifyFormat("namespace Foo\n" 11355 "{};", 11356 Style); 11357 verifyFormat("/* something */ namespace Foo\n" 11358 "{};", 11359 Style); 11360 verifyFormat("inline namespace Foo\n" 11361 "{};", 11362 Style); 11363 verifyFormat("/* something */ inline namespace Foo\n" 11364 "{};", 11365 Style); 11366 verifyFormat("export namespace Foo\n" 11367 "{};", 11368 Style); 11369 verifyFormat("namespace Foo\n" 11370 "{\n" 11371 "void Bar();\n" 11372 "};", 11373 Style); 11374 } 11375 11376 TEST_F(FormatTest, NeverMergeShortRecords) { 11377 FormatStyle Style = getLLVMStyle(); 11378 11379 verifyFormat("class Foo {\n" 11380 " Foo();\n" 11381 "};", 11382 Style); 11383 verifyFormat("typedef class Foo {\n" 11384 " Foo();\n" 11385 "} Foo_t;", 11386 Style); 11387 verifyFormat("struct Foo {\n" 11388 " Foo();\n" 11389 "};", 11390 Style); 11391 verifyFormat("typedef struct Foo {\n" 11392 " Foo();\n" 11393 "} Foo_t;", 11394 Style); 11395 verifyFormat("union Foo {\n" 11396 " A,\n" 11397 "};", 11398 Style); 11399 verifyFormat("typedef union Foo {\n" 11400 " A,\n" 11401 "} Foo_t;", 11402 Style); 11403 verifyFormat("namespace Foo {\n" 11404 "void Bar();\n" 11405 "};", 11406 Style); 11407 11408 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 11409 Style.BraceWrapping.AfterClass = true; 11410 Style.BraceWrapping.AfterStruct = true; 11411 Style.BraceWrapping.AfterUnion = true; 11412 Style.BraceWrapping.AfterNamespace = true; 11413 verifyFormat("class Foo\n" 11414 "{\n" 11415 " Foo();\n" 11416 "};", 11417 Style); 11418 verifyFormat("typedef class Foo\n" 11419 "{\n" 11420 " Foo();\n" 11421 "} Foo_t;", 11422 Style); 11423 verifyFormat("struct Foo\n" 11424 "{\n" 11425 " Foo();\n" 11426 "};", 11427 Style); 11428 verifyFormat("typedef struct Foo\n" 11429 "{\n" 11430 " Foo();\n" 11431 "} Foo_t;", 11432 Style); 11433 verifyFormat("union Foo\n" 11434 "{\n" 11435 " A,\n" 11436 "};", 11437 Style); 11438 verifyFormat("typedef union Foo\n" 11439 "{\n" 11440 " A,\n" 11441 "} Foo_t;", 11442 Style); 11443 verifyFormat("namespace Foo\n" 11444 "{\n" 11445 "void Bar();\n" 11446 "};", 11447 Style); 11448 } 11449 11450 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 11451 // Elaborate type variable declarations. 11452 verifyFormat("struct foo a = {bar};\nint n;"); 11453 verifyFormat("class foo a = {bar};\nint n;"); 11454 verifyFormat("union foo a = {bar};\nint n;"); 11455 11456 // Elaborate types inside function definitions. 11457 verifyFormat("struct foo f() {}\nint n;"); 11458 verifyFormat("class foo f() {}\nint n;"); 11459 verifyFormat("union foo f() {}\nint n;"); 11460 11461 // Templates. 11462 verifyFormat("template <class X> void f() {}\nint n;"); 11463 verifyFormat("template <struct X> void f() {}\nint n;"); 11464 verifyFormat("template <union X> void f() {}\nint n;"); 11465 11466 // Actual definitions... 11467 verifyFormat("struct {\n} n;"); 11468 verifyFormat( 11469 "template <template <class T, class Y>, class Z> class X {\n} n;"); 11470 verifyFormat("union Z {\n int n;\n} x;"); 11471 verifyFormat("class MACRO Z {\n} n;"); 11472 verifyFormat("class MACRO(X) Z {\n} n;"); 11473 verifyFormat("class __attribute__(X) Z {\n} n;"); 11474 verifyFormat("class __declspec(X) Z {\n} n;"); 11475 verifyFormat("class A##B##C {\n} n;"); 11476 verifyFormat("class alignas(16) Z {\n} n;"); 11477 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 11478 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 11479 11480 // Redefinition from nested context: 11481 verifyFormat("class A::B::C {\n} n;"); 11482 11483 // Template definitions. 11484 verifyFormat( 11485 "template <typename F>\n" 11486 "Matcher(const Matcher<F> &Other,\n" 11487 " typename enable_if_c<is_base_of<F, T>::value &&\n" 11488 " !is_same<F, T>::value>::type * = 0)\n" 11489 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 11490 11491 // FIXME: This is still incorrectly handled at the formatter side. 11492 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 11493 verifyFormat("int i = SomeFunction(a<b, a> b);"); 11494 11495 // FIXME: 11496 // This now gets parsed incorrectly as class definition. 11497 // verifyFormat("class A<int> f() {\n}\nint n;"); 11498 11499 // Elaborate types where incorrectly parsing the structural element would 11500 // break the indent. 11501 verifyFormat("if (true)\n" 11502 " class X x;\n" 11503 "else\n" 11504 " f();\n"); 11505 11506 // This is simply incomplete. Formatting is not important, but must not crash. 11507 verifyFormat("class A:"); 11508 } 11509 11510 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 11511 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 11512 format("#error Leave all white!!!!! space* alone!\n")); 11513 EXPECT_EQ( 11514 "#warning Leave all white!!!!! space* alone!\n", 11515 format("#warning Leave all white!!!!! space* alone!\n")); 11516 EXPECT_EQ("#error 1", format(" # error 1")); 11517 EXPECT_EQ("#warning 1", format(" # warning 1")); 11518 } 11519 11520 TEST_F(FormatTest, FormatHashIfExpressions) { 11521 verifyFormat("#if AAAA && BBBB"); 11522 verifyFormat("#if (AAAA && BBBB)"); 11523 verifyFormat("#elif (AAAA && BBBB)"); 11524 // FIXME: Come up with a better indentation for #elif. 11525 verifyFormat( 11526 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 11527 " defined(BBBBBBBB)\n" 11528 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 11529 " defined(BBBBBBBB)\n" 11530 "#endif", 11531 getLLVMStyleWithColumns(65)); 11532 } 11533 11534 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 11535 FormatStyle AllowsMergedIf = getGoogleStyle(); 11536 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 11537 FormatStyle::SIS_WithoutElse; 11538 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 11539 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 11540 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 11541 EXPECT_EQ("if (true) return 42;", 11542 format("if (true)\nreturn 42;", AllowsMergedIf)); 11543 FormatStyle ShortMergedIf = AllowsMergedIf; 11544 ShortMergedIf.ColumnLimit = 25; 11545 verifyFormat("#define A \\\n" 11546 " if (true) return 42;", 11547 ShortMergedIf); 11548 verifyFormat("#define A \\\n" 11549 " f(); \\\n" 11550 " if (true)\n" 11551 "#define B", 11552 ShortMergedIf); 11553 verifyFormat("#define A \\\n" 11554 " f(); \\\n" 11555 " if (true)\n" 11556 "g();", 11557 ShortMergedIf); 11558 verifyFormat("{\n" 11559 "#ifdef A\n" 11560 " // Comment\n" 11561 " if (true) continue;\n" 11562 "#endif\n" 11563 " // Comment\n" 11564 " if (true) continue;\n" 11565 "}", 11566 ShortMergedIf); 11567 ShortMergedIf.ColumnLimit = 33; 11568 verifyFormat("#define A \\\n" 11569 " if constexpr (true) return 42;", 11570 ShortMergedIf); 11571 verifyFormat("#define A \\\n" 11572 " if CONSTEXPR (true) return 42;", 11573 ShortMergedIf); 11574 ShortMergedIf.ColumnLimit = 29; 11575 verifyFormat("#define A \\\n" 11576 " if (aaaaaaaaaa) return 1; \\\n" 11577 " return 2;", 11578 ShortMergedIf); 11579 ShortMergedIf.ColumnLimit = 28; 11580 verifyFormat("#define A \\\n" 11581 " if (aaaaaaaaaa) \\\n" 11582 " return 1; \\\n" 11583 " return 2;", 11584 ShortMergedIf); 11585 verifyFormat("#define A \\\n" 11586 " if constexpr (aaaaaaa) \\\n" 11587 " return 1; \\\n" 11588 " return 2;", 11589 ShortMergedIf); 11590 verifyFormat("#define A \\\n" 11591 " if CONSTEXPR (aaaaaaa) \\\n" 11592 " return 1; \\\n" 11593 " return 2;", 11594 ShortMergedIf); 11595 } 11596 11597 TEST_F(FormatTest, FormatStarDependingOnContext) { 11598 verifyFormat("void f(int *a);"); 11599 verifyFormat("void f() { f(fint * b); }"); 11600 verifyFormat("class A {\n void f(int *a);\n};"); 11601 verifyFormat("class A {\n int *a;\n};"); 11602 verifyFormat("namespace a {\n" 11603 "namespace b {\n" 11604 "class A {\n" 11605 " void f() {}\n" 11606 " int *a;\n" 11607 "};\n" 11608 "} // namespace b\n" 11609 "} // namespace a"); 11610 } 11611 11612 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 11613 verifyFormat("while"); 11614 verifyFormat("operator"); 11615 } 11616 11617 TEST_F(FormatTest, SkipsDeeplyNestedLines) { 11618 // This code would be painfully slow to format if we didn't skip it. 11619 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 11620 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 11621 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 11622 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 11623 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 11624 "A(1, 1)\n" 11625 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x 11626 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 11627 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 11628 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 11629 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 11630 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 11631 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 11632 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 11633 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 11634 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n"); 11635 // Deeply nested part is untouched, rest is formatted. 11636 EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n", 11637 format(std::string("int i;\n") + Code + "int j;\n", 11638 getLLVMStyle(), SC_ExpectIncomplete)); 11639 } 11640 11641 //===----------------------------------------------------------------------===// 11642 // Objective-C tests. 11643 //===----------------------------------------------------------------------===// 11644 11645 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 11646 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 11647 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 11648 format("-(NSUInteger)indexOfObject:(id)anObject;")); 11649 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 11650 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 11651 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 11652 format("-(NSInteger)Method3:(id)anObject;")); 11653 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 11654 format("-(NSInteger)Method4:(id)anObject;")); 11655 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 11656 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 11657 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 11658 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 11659 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 11660 "forAllCells:(BOOL)flag;", 11661 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 11662 "forAllCells:(BOOL)flag;")); 11663 11664 // Very long objectiveC method declaration. 11665 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 11666 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 11667 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 11668 " inRange:(NSRange)range\n" 11669 " outRange:(NSRange)out_range\n" 11670 " outRange1:(NSRange)out_range1\n" 11671 " outRange2:(NSRange)out_range2\n" 11672 " outRange3:(NSRange)out_range3\n" 11673 " outRange4:(NSRange)out_range4\n" 11674 " outRange5:(NSRange)out_range5\n" 11675 " outRange6:(NSRange)out_range6\n" 11676 " outRange7:(NSRange)out_range7\n" 11677 " outRange8:(NSRange)out_range8\n" 11678 " outRange9:(NSRange)out_range9;"); 11679 11680 // When the function name has to be wrapped. 11681 FormatStyle Style = getLLVMStyle(); 11682 // ObjC ignores IndentWrappedFunctionNames when wrapping methods 11683 // and always indents instead. 11684 Style.IndentWrappedFunctionNames = false; 11685 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 11686 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 11687 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 11688 "}", 11689 Style); 11690 Style.IndentWrappedFunctionNames = true; 11691 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 11692 " veryLooooooooooongName:(NSString)cccccccccccccc\n" 11693 " anotherName:(NSString)dddddddddddddd {\n" 11694 "}", 11695 Style); 11696 11697 verifyFormat("- (int)sum:(vector<int>)numbers;"); 11698 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 11699 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 11700 // protocol lists (but not for template classes): 11701 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 11702 11703 verifyFormat("- (int (*)())foo:(int (*)())f;"); 11704 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 11705 11706 // If there's no return type (very rare in practice!), LLVM and Google style 11707 // agree. 11708 verifyFormat("- foo;"); 11709 verifyFormat("- foo:(int)f;"); 11710 verifyGoogleFormat("- foo:(int)foo;"); 11711 } 11712 11713 TEST_F(FormatTest, BreaksStringLiterals) { 11714 EXPECT_EQ("\"some text \"\n" 11715 "\"other\";", 11716 format("\"some text other\";", getLLVMStyleWithColumns(12))); 11717 EXPECT_EQ("\"some text \"\n" 11718 "\"other\";", 11719 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 11720 EXPECT_EQ( 11721 "#define A \\\n" 11722 " \"some \" \\\n" 11723 " \"text \" \\\n" 11724 " \"other\";", 11725 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 11726 EXPECT_EQ( 11727 "#define A \\\n" 11728 " \"so \" \\\n" 11729 " \"text \" \\\n" 11730 " \"other\";", 11731 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 11732 11733 EXPECT_EQ("\"some text\"", 11734 format("\"some text\"", getLLVMStyleWithColumns(1))); 11735 EXPECT_EQ("\"some text\"", 11736 format("\"some text\"", getLLVMStyleWithColumns(11))); 11737 EXPECT_EQ("\"some \"\n" 11738 "\"text\"", 11739 format("\"some text\"", getLLVMStyleWithColumns(10))); 11740 EXPECT_EQ("\"some \"\n" 11741 "\"text\"", 11742 format("\"some text\"", getLLVMStyleWithColumns(7))); 11743 EXPECT_EQ("\"some\"\n" 11744 "\" tex\"\n" 11745 "\"t\"", 11746 format("\"some text\"", getLLVMStyleWithColumns(6))); 11747 EXPECT_EQ("\"some\"\n" 11748 "\" tex\"\n" 11749 "\" and\"", 11750 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 11751 EXPECT_EQ("\"some\"\n" 11752 "\"/tex\"\n" 11753 "\"/and\"", 11754 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 11755 11756 EXPECT_EQ("variable =\n" 11757 " \"long string \"\n" 11758 " \"literal\";", 11759 format("variable = \"long string literal\";", 11760 getLLVMStyleWithColumns(20))); 11761 11762 EXPECT_EQ("variable = f(\n" 11763 " \"long string \"\n" 11764 " \"literal\",\n" 11765 " short,\n" 11766 " loooooooooooooooooooong);", 11767 format("variable = f(\"long string literal\", short, " 11768 "loooooooooooooooooooong);", 11769 getLLVMStyleWithColumns(20))); 11770 11771 EXPECT_EQ( 11772 "f(g(\"long string \"\n" 11773 " \"literal\"),\n" 11774 " b);", 11775 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 11776 EXPECT_EQ("f(g(\"long string \"\n" 11777 " \"literal\",\n" 11778 " a),\n" 11779 " b);", 11780 format("f(g(\"long string literal\", a), b);", 11781 getLLVMStyleWithColumns(20))); 11782 EXPECT_EQ( 11783 "f(\"one two\".split(\n" 11784 " variable));", 11785 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 11786 EXPECT_EQ("f(\"one two three four five six \"\n" 11787 " \"seven\".split(\n" 11788 " really_looooong_variable));", 11789 format("f(\"one two three four five six seven\"." 11790 "split(really_looooong_variable));", 11791 getLLVMStyleWithColumns(33))); 11792 11793 EXPECT_EQ("f(\"some \"\n" 11794 " \"text\",\n" 11795 " other);", 11796 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 11797 11798 // Only break as a last resort. 11799 verifyFormat( 11800 "aaaaaaaaaaaaaaaaaaaa(\n" 11801 " aaaaaaaaaaaaaaaaaaaa,\n" 11802 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 11803 11804 EXPECT_EQ("\"splitmea\"\n" 11805 "\"trandomp\"\n" 11806 "\"oint\"", 11807 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 11808 11809 EXPECT_EQ("\"split/\"\n" 11810 "\"pathat/\"\n" 11811 "\"slashes\"", 11812 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 11813 11814 EXPECT_EQ("\"split/\"\n" 11815 "\"pathat/\"\n" 11816 "\"slashes\"", 11817 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 11818 EXPECT_EQ("\"split at \"\n" 11819 "\"spaces/at/\"\n" 11820 "\"slashes.at.any$\"\n" 11821 "\"non-alphanumeric%\"\n" 11822 "\"1111111111characte\"\n" 11823 "\"rs\"", 11824 format("\"split at " 11825 "spaces/at/" 11826 "slashes.at." 11827 "any$non-" 11828 "alphanumeric%" 11829 "1111111111characte" 11830 "rs\"", 11831 getLLVMStyleWithColumns(20))); 11832 11833 // Verify that splitting the strings understands 11834 // Style::AlwaysBreakBeforeMultilineStrings. 11835 EXPECT_EQ("aaaaaaaaaaaa(\n" 11836 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 11837 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 11838 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 11839 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 11840 "aaaaaaaaaaaaaaaaaaaaaa\");", 11841 getGoogleStyle())); 11842 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 11843 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 11844 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 11845 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 11846 "aaaaaaaaaaaaaaaaaaaaaa\";", 11847 getGoogleStyle())); 11848 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 11849 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 11850 format("llvm::outs() << " 11851 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 11852 "aaaaaaaaaaaaaaaaaaa\";")); 11853 EXPECT_EQ("ffff(\n" 11854 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 11855 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 11856 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 11857 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 11858 getGoogleStyle())); 11859 11860 FormatStyle Style = getLLVMStyleWithColumns(12); 11861 Style.BreakStringLiterals = false; 11862 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 11863 11864 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 11865 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 11866 EXPECT_EQ("#define A \\\n" 11867 " \"some \" \\\n" 11868 " \"text \" \\\n" 11869 " \"other\";", 11870 format("#define A \"some text other\";", AlignLeft)); 11871 } 11872 11873 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) { 11874 EXPECT_EQ("C a = \"some more \"\n" 11875 " \"text\";", 11876 format("C a = \"some more text\";", getLLVMStyleWithColumns(18))); 11877 } 11878 11879 TEST_F(FormatTest, FullyRemoveEmptyLines) { 11880 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 11881 NoEmptyLines.MaxEmptyLinesToKeep = 0; 11882 EXPECT_EQ("int i = a(b());", 11883 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 11884 } 11885 11886 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 11887 EXPECT_EQ( 11888 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11889 "(\n" 11890 " \"x\t\");", 11891 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11892 "aaaaaaa(" 11893 "\"x\t\");")); 11894 } 11895 11896 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 11897 EXPECT_EQ( 11898 "u8\"utf8 string \"\n" 11899 "u8\"literal\";", 11900 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 11901 EXPECT_EQ( 11902 "u\"utf16 string \"\n" 11903 "u\"literal\";", 11904 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 11905 EXPECT_EQ( 11906 "U\"utf32 string \"\n" 11907 "U\"literal\";", 11908 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 11909 EXPECT_EQ("L\"wide string \"\n" 11910 "L\"literal\";", 11911 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 11912 EXPECT_EQ("@\"NSString \"\n" 11913 "@\"literal\";", 11914 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 11915 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26)); 11916 11917 // This input makes clang-format try to split the incomplete unicode escape 11918 // sequence, which used to lead to a crasher. 11919 verifyNoCrash( 11920 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 11921 getLLVMStyleWithColumns(60)); 11922 } 11923 11924 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 11925 FormatStyle Style = getGoogleStyleWithColumns(15); 11926 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 11927 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 11928 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 11929 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 11930 EXPECT_EQ("u8R\"x(raw literal)x\";", 11931 format("u8R\"x(raw literal)x\";", Style)); 11932 } 11933 11934 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 11935 FormatStyle Style = getLLVMStyleWithColumns(20); 11936 EXPECT_EQ( 11937 "_T(\"aaaaaaaaaaaaaa\")\n" 11938 "_T(\"aaaaaaaaaaaaaa\")\n" 11939 "_T(\"aaaaaaaaaaaa\")", 11940 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 11941 EXPECT_EQ("f(x,\n" 11942 " _T(\"aaaaaaaaaaaa\")\n" 11943 " _T(\"aaa\"),\n" 11944 " z);", 11945 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 11946 11947 // FIXME: Handle embedded spaces in one iteration. 11948 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 11949 // "_T(\"aaaaaaaaaaaaa\")\n" 11950 // "_T(\"aaaaaaaaaaaaa\")\n" 11951 // "_T(\"a\")", 11952 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 11953 // getLLVMStyleWithColumns(20))); 11954 EXPECT_EQ( 11955 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 11956 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 11957 EXPECT_EQ("f(\n" 11958 "#if !TEST\n" 11959 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 11960 "#endif\n" 11961 ");", 11962 format("f(\n" 11963 "#if !TEST\n" 11964 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 11965 "#endif\n" 11966 ");")); 11967 EXPECT_EQ("f(\n" 11968 "\n" 11969 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 11970 format("f(\n" 11971 "\n" 11972 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 11973 } 11974 11975 TEST_F(FormatTest, BreaksStringLiteralOperands) { 11976 // In a function call with two operands, the second can be broken with no line 11977 // break before it. 11978 EXPECT_EQ( 11979 "func(a, \"long long \"\n" 11980 " \"long long\");", 11981 format("func(a, \"long long long long\");", getLLVMStyleWithColumns(24))); 11982 // In a function call with three operands, the second must be broken with a 11983 // line break before it. 11984 EXPECT_EQ("func(a,\n" 11985 " \"long long long \"\n" 11986 " \"long\",\n" 11987 " c);", 11988 format("func(a, \"long long long long\", c);", 11989 getLLVMStyleWithColumns(24))); 11990 // In a function call with three operands, the third must be broken with a 11991 // line break before it. 11992 EXPECT_EQ("func(a, b,\n" 11993 " \"long long long \"\n" 11994 " \"long\");", 11995 format("func(a, b, \"long long long long\");", 11996 getLLVMStyleWithColumns(24))); 11997 // In a function call with three operands, both the second and the third must 11998 // be broken with a line break before them. 11999 EXPECT_EQ("func(a,\n" 12000 " \"long long long \"\n" 12001 " \"long\",\n" 12002 " \"long long long \"\n" 12003 " \"long\");", 12004 format("func(a, \"long long long long\", \"long long long long\");", 12005 getLLVMStyleWithColumns(24))); 12006 // In a chain of << with two operands, the second can be broken with no line 12007 // break before it. 12008 EXPECT_EQ("a << \"line line \"\n" 12009 " \"line\";", 12010 format("a << \"line line line\";", getLLVMStyleWithColumns(20))); 12011 // In a chain of << with three operands, the second can be broken with no line 12012 // break before it. 12013 EXPECT_EQ( 12014 "abcde << \"line \"\n" 12015 " \"line line\"\n" 12016 " << c;", 12017 format("abcde << \"line line line\" << c;", getLLVMStyleWithColumns(20))); 12018 // In a chain of << with three operands, the third must be broken with a line 12019 // break before it. 12020 EXPECT_EQ( 12021 "a << b\n" 12022 " << \"line line \"\n" 12023 " \"line\";", 12024 format("a << b << \"line line line\";", getLLVMStyleWithColumns(20))); 12025 // In a chain of << with three operands, the second can be broken with no line 12026 // break before it and the third must be broken with a line break before it. 12027 EXPECT_EQ("abcd << \"line line \"\n" 12028 " \"line\"\n" 12029 " << \"line line \"\n" 12030 " \"line\";", 12031 format("abcd << \"line line line\" << \"line line line\";", 12032 getLLVMStyleWithColumns(20))); 12033 // In a chain of binary operators with two operands, the second can be broken 12034 // with no line break before it. 12035 EXPECT_EQ( 12036 "abcd + \"line line \"\n" 12037 " \"line line\";", 12038 format("abcd + \"line line line line\";", getLLVMStyleWithColumns(20))); 12039 // In a chain of binary operators with three operands, the second must be 12040 // broken with a line break before it. 12041 EXPECT_EQ("abcd +\n" 12042 " \"line line \"\n" 12043 " \"line line\" +\n" 12044 " e;", 12045 format("abcd + \"line line line line\" + e;", 12046 getLLVMStyleWithColumns(20))); 12047 // In a function call with two operands, with AlignAfterOpenBracket enabled, 12048 // the first must be broken with a line break before it. 12049 FormatStyle Style = getLLVMStyleWithColumns(25); 12050 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 12051 EXPECT_EQ("someFunction(\n" 12052 " \"long long long \"\n" 12053 " \"long\",\n" 12054 " a);", 12055 format("someFunction(\"long long long long\", a);", Style)); 12056 } 12057 12058 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 12059 EXPECT_EQ( 12060 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 12061 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 12062 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 12063 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 12064 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 12065 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 12066 } 12067 12068 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 12069 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 12070 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 12071 EXPECT_EQ("fffffffffff(g(R\"x(\n" 12072 "multiline raw string literal xxxxxxxxxxxxxx\n" 12073 ")x\",\n" 12074 " a),\n" 12075 " b);", 12076 format("fffffffffff(g(R\"x(\n" 12077 "multiline raw string literal xxxxxxxxxxxxxx\n" 12078 ")x\", a), b);", 12079 getGoogleStyleWithColumns(20))); 12080 EXPECT_EQ("fffffffffff(\n" 12081 " g(R\"x(qqq\n" 12082 "multiline raw string literal xxxxxxxxxxxxxx\n" 12083 ")x\",\n" 12084 " a),\n" 12085 " b);", 12086 format("fffffffffff(g(R\"x(qqq\n" 12087 "multiline raw string literal xxxxxxxxxxxxxx\n" 12088 ")x\", a), b);", 12089 getGoogleStyleWithColumns(20))); 12090 12091 EXPECT_EQ("fffffffffff(R\"x(\n" 12092 "multiline raw string literal xxxxxxxxxxxxxx\n" 12093 ")x\");", 12094 format("fffffffffff(R\"x(\n" 12095 "multiline raw string literal xxxxxxxxxxxxxx\n" 12096 ")x\");", 12097 getGoogleStyleWithColumns(20))); 12098 EXPECT_EQ("fffffffffff(R\"x(\n" 12099 "multiline raw string literal xxxxxxxxxxxxxx\n" 12100 ")x\" + bbbbbb);", 12101 format("fffffffffff(R\"x(\n" 12102 "multiline raw string literal xxxxxxxxxxxxxx\n" 12103 ")x\" + bbbbbb);", 12104 getGoogleStyleWithColumns(20))); 12105 EXPECT_EQ("fffffffffff(\n" 12106 " R\"x(\n" 12107 "multiline raw string literal xxxxxxxxxxxxxx\n" 12108 ")x\" +\n" 12109 " bbbbbb);", 12110 format("fffffffffff(\n" 12111 " R\"x(\n" 12112 "multiline raw string literal xxxxxxxxxxxxxx\n" 12113 ")x\" + bbbbbb);", 12114 getGoogleStyleWithColumns(20))); 12115 EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);", 12116 format("fffffffffff(\n" 12117 " R\"(single line raw string)\" + bbbbbb);")); 12118 } 12119 12120 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 12121 verifyFormat("string a = \"unterminated;"); 12122 EXPECT_EQ("function(\"unterminated,\n" 12123 " OtherParameter);", 12124 format("function( \"unterminated,\n" 12125 " OtherParameter);")); 12126 } 12127 12128 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 12129 FormatStyle Style = getLLVMStyle(); 12130 Style.Standard = FormatStyle::LS_Cpp03; 12131 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 12132 format("#define x(_a) printf(\"foo\"_a);", Style)); 12133 } 12134 12135 TEST_F(FormatTest, CppLexVersion) { 12136 FormatStyle Style = getLLVMStyle(); 12137 // Formatting of x * y differs if x is a type. 12138 verifyFormat("void foo() { MACRO(a * b); }", Style); 12139 verifyFormat("void foo() { MACRO(int *b); }", Style); 12140 12141 // LLVM style uses latest lexer. 12142 verifyFormat("void foo() { MACRO(char8_t *b); }", Style); 12143 Style.Standard = FormatStyle::LS_Cpp17; 12144 // But in c++17, char8_t isn't a keyword. 12145 verifyFormat("void foo() { MACRO(char8_t * b); }", Style); 12146 } 12147 12148 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 12149 12150 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 12151 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 12152 " \"ddeeefff\");", 12153 format("someFunction(\"aaabbbcccdddeeefff\");", 12154 getLLVMStyleWithColumns(25))); 12155 EXPECT_EQ("someFunction1234567890(\n" 12156 " \"aaabbbcccdddeeefff\");", 12157 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 12158 getLLVMStyleWithColumns(26))); 12159 EXPECT_EQ("someFunction1234567890(\n" 12160 " \"aaabbbcccdddeeeff\"\n" 12161 " \"f\");", 12162 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 12163 getLLVMStyleWithColumns(25))); 12164 EXPECT_EQ("someFunction1234567890(\n" 12165 " \"aaabbbcccdddeeeff\"\n" 12166 " \"f\");", 12167 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 12168 getLLVMStyleWithColumns(24))); 12169 EXPECT_EQ("someFunction(\n" 12170 " \"aaabbbcc ddde \"\n" 12171 " \"efff\");", 12172 format("someFunction(\"aaabbbcc ddde efff\");", 12173 getLLVMStyleWithColumns(25))); 12174 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 12175 " \"ddeeefff\");", 12176 format("someFunction(\"aaabbbccc ddeeefff\");", 12177 getLLVMStyleWithColumns(25))); 12178 EXPECT_EQ("someFunction1234567890(\n" 12179 " \"aaabb \"\n" 12180 " \"cccdddeeefff\");", 12181 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 12182 getLLVMStyleWithColumns(25))); 12183 EXPECT_EQ("#define A \\\n" 12184 " string s = \\\n" 12185 " \"123456789\" \\\n" 12186 " \"0\"; \\\n" 12187 " int i;", 12188 format("#define A string s = \"1234567890\"; int i;", 12189 getLLVMStyleWithColumns(20))); 12190 EXPECT_EQ("someFunction(\n" 12191 " \"aaabbbcc \"\n" 12192 " \"dddeeefff\");", 12193 format("someFunction(\"aaabbbcc dddeeefff\");", 12194 getLLVMStyleWithColumns(25))); 12195 } 12196 12197 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 12198 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 12199 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 12200 EXPECT_EQ("\"test\"\n" 12201 "\"\\n\"", 12202 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 12203 EXPECT_EQ("\"tes\\\\\"\n" 12204 "\"n\"", 12205 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 12206 EXPECT_EQ("\"\\\\\\\\\"\n" 12207 "\"\\n\"", 12208 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 12209 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 12210 EXPECT_EQ("\"\\uff01\"\n" 12211 "\"test\"", 12212 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 12213 EXPECT_EQ("\"\\Uff01ff02\"", 12214 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 12215 EXPECT_EQ("\"\\x000000000001\"\n" 12216 "\"next\"", 12217 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 12218 EXPECT_EQ("\"\\x000000000001next\"", 12219 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 12220 EXPECT_EQ("\"\\x000000000001\"", 12221 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 12222 EXPECT_EQ("\"test\"\n" 12223 "\"\\000000\"\n" 12224 "\"000001\"", 12225 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 12226 EXPECT_EQ("\"test\\000\"\n" 12227 "\"00000000\"\n" 12228 "\"1\"", 12229 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 12230 } 12231 12232 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 12233 verifyFormat("void f() {\n" 12234 " return g() {}\n" 12235 " void h() {}"); 12236 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 12237 "g();\n" 12238 "}"); 12239 } 12240 12241 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 12242 verifyFormat( 12243 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 12244 } 12245 12246 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 12247 verifyFormat("class X {\n" 12248 " void f() {\n" 12249 " }\n" 12250 "};", 12251 getLLVMStyleWithColumns(12)); 12252 } 12253 12254 TEST_F(FormatTest, ConfigurableIndentWidth) { 12255 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 12256 EightIndent.IndentWidth = 8; 12257 EightIndent.ContinuationIndentWidth = 8; 12258 verifyFormat("void f() {\n" 12259 " someFunction();\n" 12260 " if (true) {\n" 12261 " f();\n" 12262 " }\n" 12263 "}", 12264 EightIndent); 12265 verifyFormat("class X {\n" 12266 " void f() {\n" 12267 " }\n" 12268 "};", 12269 EightIndent); 12270 verifyFormat("int x[] = {\n" 12271 " call(),\n" 12272 " call()};", 12273 EightIndent); 12274 } 12275 12276 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 12277 verifyFormat("double\n" 12278 "f();", 12279 getLLVMStyleWithColumns(8)); 12280 } 12281 12282 TEST_F(FormatTest, ConfigurableUseOfTab) { 12283 FormatStyle Tab = getLLVMStyleWithColumns(42); 12284 Tab.IndentWidth = 8; 12285 Tab.UseTab = FormatStyle::UT_Always; 12286 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left; 12287 12288 EXPECT_EQ("if (aaaaaaaa && // q\n" 12289 " bb)\t\t// w\n" 12290 "\t;", 12291 format("if (aaaaaaaa &&// q\n" 12292 "bb)// w\n" 12293 ";", 12294 Tab)); 12295 EXPECT_EQ("if (aaa && bbb) // w\n" 12296 "\t;", 12297 format("if(aaa&&bbb)// w\n" 12298 ";", 12299 Tab)); 12300 12301 verifyFormat("class X {\n" 12302 "\tvoid f() {\n" 12303 "\t\tsomeFunction(parameter1,\n" 12304 "\t\t\t parameter2);\n" 12305 "\t}\n" 12306 "};", 12307 Tab); 12308 verifyFormat("#define A \\\n" 12309 "\tvoid f() { \\\n" 12310 "\t\tsomeFunction( \\\n" 12311 "\t\t parameter1, \\\n" 12312 "\t\t parameter2); \\\n" 12313 "\t}", 12314 Tab); 12315 verifyFormat("int a;\t // x\n" 12316 "int bbbbbbbb; // x\n", 12317 Tab); 12318 12319 Tab.TabWidth = 4; 12320 Tab.IndentWidth = 8; 12321 verifyFormat("class TabWidth4Indent8 {\n" 12322 "\t\tvoid f() {\n" 12323 "\t\t\t\tsomeFunction(parameter1,\n" 12324 "\t\t\t\t\t\t\t parameter2);\n" 12325 "\t\t}\n" 12326 "};", 12327 Tab); 12328 12329 Tab.TabWidth = 4; 12330 Tab.IndentWidth = 4; 12331 verifyFormat("class TabWidth4Indent4 {\n" 12332 "\tvoid f() {\n" 12333 "\t\tsomeFunction(parameter1,\n" 12334 "\t\t\t\t\t parameter2);\n" 12335 "\t}\n" 12336 "};", 12337 Tab); 12338 12339 Tab.TabWidth = 8; 12340 Tab.IndentWidth = 4; 12341 verifyFormat("class TabWidth8Indent4 {\n" 12342 " void f() {\n" 12343 "\tsomeFunction(parameter1,\n" 12344 "\t\t parameter2);\n" 12345 " }\n" 12346 "};", 12347 Tab); 12348 12349 Tab.TabWidth = 8; 12350 Tab.IndentWidth = 8; 12351 EXPECT_EQ("/*\n" 12352 "\t a\t\tcomment\n" 12353 "\t in multiple lines\n" 12354 " */", 12355 format(" /*\t \t \n" 12356 " \t \t a\t\tcomment\t \t\n" 12357 " \t \t in multiple lines\t\n" 12358 " \t */", 12359 Tab)); 12360 12361 Tab.UseTab = FormatStyle::UT_ForIndentation; 12362 verifyFormat("{\n" 12363 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 12364 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 12365 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 12366 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 12367 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 12368 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 12369 "};", 12370 Tab); 12371 verifyFormat("enum AA {\n" 12372 "\ta1, // Force multiple lines\n" 12373 "\ta2,\n" 12374 "\ta3\n" 12375 "};", 12376 Tab); 12377 EXPECT_EQ("if (aaaaaaaa && // q\n" 12378 " bb) // w\n" 12379 "\t;", 12380 format("if (aaaaaaaa &&// q\n" 12381 "bb)// w\n" 12382 ";", 12383 Tab)); 12384 verifyFormat("class X {\n" 12385 "\tvoid f() {\n" 12386 "\t\tsomeFunction(parameter1,\n" 12387 "\t\t parameter2);\n" 12388 "\t}\n" 12389 "};", 12390 Tab); 12391 verifyFormat("{\n" 12392 "\tQ(\n" 12393 "\t {\n" 12394 "\t\t int a;\n" 12395 "\t\t someFunction(aaaaaaaa,\n" 12396 "\t\t bbbbbbb);\n" 12397 "\t },\n" 12398 "\t p);\n" 12399 "}", 12400 Tab); 12401 EXPECT_EQ("{\n" 12402 "\t/* aaaa\n" 12403 "\t bbbb */\n" 12404 "}", 12405 format("{\n" 12406 "/* aaaa\n" 12407 " bbbb */\n" 12408 "}", 12409 Tab)); 12410 EXPECT_EQ("{\n" 12411 "\t/*\n" 12412 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 12413 "\t bbbbbbbbbbbbb\n" 12414 "\t*/\n" 12415 "}", 12416 format("{\n" 12417 "/*\n" 12418 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 12419 "*/\n" 12420 "}", 12421 Tab)); 12422 EXPECT_EQ("{\n" 12423 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 12424 "\t// bbbbbbbbbbbbb\n" 12425 "}", 12426 format("{\n" 12427 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 12428 "}", 12429 Tab)); 12430 EXPECT_EQ("{\n" 12431 "\t/*\n" 12432 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 12433 "\t bbbbbbbbbbbbb\n" 12434 "\t*/\n" 12435 "}", 12436 format("{\n" 12437 "\t/*\n" 12438 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 12439 "\t*/\n" 12440 "}", 12441 Tab)); 12442 EXPECT_EQ("{\n" 12443 "\t/*\n" 12444 "\n" 12445 "\t*/\n" 12446 "}", 12447 format("{\n" 12448 "\t/*\n" 12449 "\n" 12450 "\t*/\n" 12451 "}", 12452 Tab)); 12453 EXPECT_EQ("{\n" 12454 "\t/*\n" 12455 " asdf\n" 12456 "\t*/\n" 12457 "}", 12458 format("{\n" 12459 "\t/*\n" 12460 " asdf\n" 12461 "\t*/\n" 12462 "}", 12463 Tab)); 12464 12465 Tab.UseTab = FormatStyle::UT_Never; 12466 EXPECT_EQ("/*\n" 12467 " a\t\tcomment\n" 12468 " in multiple lines\n" 12469 " */", 12470 format(" /*\t \t \n" 12471 " \t \t a\t\tcomment\t \t\n" 12472 " \t \t in multiple lines\t\n" 12473 " \t */", 12474 Tab)); 12475 EXPECT_EQ("/* some\n" 12476 " comment */", 12477 format(" \t \t /* some\n" 12478 " \t \t comment */", 12479 Tab)); 12480 EXPECT_EQ("int a; /* some\n" 12481 " comment */", 12482 format(" \t \t int a; /* some\n" 12483 " \t \t comment */", 12484 Tab)); 12485 12486 EXPECT_EQ("int a; /* some\n" 12487 "comment */", 12488 format(" \t \t int\ta; /* some\n" 12489 " \t \t comment */", 12490 Tab)); 12491 EXPECT_EQ("f(\"\t\t\"); /* some\n" 12492 " comment */", 12493 format(" \t \t f(\"\t\t\"); /* some\n" 12494 " \t \t comment */", 12495 Tab)); 12496 EXPECT_EQ("{\n" 12497 " /*\n" 12498 " * Comment\n" 12499 " */\n" 12500 " int i;\n" 12501 "}", 12502 format("{\n" 12503 "\t/*\n" 12504 "\t * Comment\n" 12505 "\t */\n" 12506 "\t int i;\n" 12507 "}", 12508 Tab)); 12509 12510 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 12511 Tab.TabWidth = 8; 12512 Tab.IndentWidth = 8; 12513 EXPECT_EQ("if (aaaaaaaa && // q\n" 12514 " bb) // w\n" 12515 "\t;", 12516 format("if (aaaaaaaa &&// q\n" 12517 "bb)// w\n" 12518 ";", 12519 Tab)); 12520 EXPECT_EQ("if (aaa && bbb) // w\n" 12521 "\t;", 12522 format("if(aaa&&bbb)// w\n" 12523 ";", 12524 Tab)); 12525 verifyFormat("class X {\n" 12526 "\tvoid f() {\n" 12527 "\t\tsomeFunction(parameter1,\n" 12528 "\t\t\t parameter2);\n" 12529 "\t}\n" 12530 "};", 12531 Tab); 12532 verifyFormat("#define A \\\n" 12533 "\tvoid f() { \\\n" 12534 "\t\tsomeFunction( \\\n" 12535 "\t\t parameter1, \\\n" 12536 "\t\t parameter2); \\\n" 12537 "\t}", 12538 Tab); 12539 Tab.TabWidth = 4; 12540 Tab.IndentWidth = 8; 12541 verifyFormat("class TabWidth4Indent8 {\n" 12542 "\t\tvoid f() {\n" 12543 "\t\t\t\tsomeFunction(parameter1,\n" 12544 "\t\t\t\t\t\t\t parameter2);\n" 12545 "\t\t}\n" 12546 "};", 12547 Tab); 12548 Tab.TabWidth = 4; 12549 Tab.IndentWidth = 4; 12550 verifyFormat("class TabWidth4Indent4 {\n" 12551 "\tvoid f() {\n" 12552 "\t\tsomeFunction(parameter1,\n" 12553 "\t\t\t\t\t parameter2);\n" 12554 "\t}\n" 12555 "};", 12556 Tab); 12557 Tab.TabWidth = 8; 12558 Tab.IndentWidth = 4; 12559 verifyFormat("class TabWidth8Indent4 {\n" 12560 " void f() {\n" 12561 "\tsomeFunction(parameter1,\n" 12562 "\t\t parameter2);\n" 12563 " }\n" 12564 "};", 12565 Tab); 12566 Tab.TabWidth = 8; 12567 Tab.IndentWidth = 8; 12568 EXPECT_EQ("/*\n" 12569 "\t a\t\tcomment\n" 12570 "\t in multiple lines\n" 12571 " */", 12572 format(" /*\t \t \n" 12573 " \t \t a\t\tcomment\t \t\n" 12574 " \t \t in multiple lines\t\n" 12575 " \t */", 12576 Tab)); 12577 verifyFormat("{\n" 12578 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 12579 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 12580 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 12581 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 12582 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 12583 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 12584 "};", 12585 Tab); 12586 verifyFormat("enum AA {\n" 12587 "\ta1, // Force multiple lines\n" 12588 "\ta2,\n" 12589 "\ta3\n" 12590 "};", 12591 Tab); 12592 EXPECT_EQ("if (aaaaaaaa && // q\n" 12593 " bb) // w\n" 12594 "\t;", 12595 format("if (aaaaaaaa &&// q\n" 12596 "bb)// w\n" 12597 ";", 12598 Tab)); 12599 verifyFormat("class X {\n" 12600 "\tvoid f() {\n" 12601 "\t\tsomeFunction(parameter1,\n" 12602 "\t\t\t parameter2);\n" 12603 "\t}\n" 12604 "};", 12605 Tab); 12606 verifyFormat("{\n" 12607 "\tQ(\n" 12608 "\t {\n" 12609 "\t\t int a;\n" 12610 "\t\t someFunction(aaaaaaaa,\n" 12611 "\t\t\t\t bbbbbbb);\n" 12612 "\t },\n" 12613 "\t p);\n" 12614 "}", 12615 Tab); 12616 EXPECT_EQ("{\n" 12617 "\t/* aaaa\n" 12618 "\t bbbb */\n" 12619 "}", 12620 format("{\n" 12621 "/* aaaa\n" 12622 " bbbb */\n" 12623 "}", 12624 Tab)); 12625 EXPECT_EQ("{\n" 12626 "\t/*\n" 12627 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 12628 "\t bbbbbbbbbbbbb\n" 12629 "\t*/\n" 12630 "}", 12631 format("{\n" 12632 "/*\n" 12633 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 12634 "*/\n" 12635 "}", 12636 Tab)); 12637 EXPECT_EQ("{\n" 12638 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 12639 "\t// bbbbbbbbbbbbb\n" 12640 "}", 12641 format("{\n" 12642 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 12643 "}", 12644 Tab)); 12645 EXPECT_EQ("{\n" 12646 "\t/*\n" 12647 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 12648 "\t bbbbbbbbbbbbb\n" 12649 "\t*/\n" 12650 "}", 12651 format("{\n" 12652 "\t/*\n" 12653 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 12654 "\t*/\n" 12655 "}", 12656 Tab)); 12657 EXPECT_EQ("{\n" 12658 "\t/*\n" 12659 "\n" 12660 "\t*/\n" 12661 "}", 12662 format("{\n" 12663 "\t/*\n" 12664 "\n" 12665 "\t*/\n" 12666 "}", 12667 Tab)); 12668 EXPECT_EQ("{\n" 12669 "\t/*\n" 12670 " asdf\n" 12671 "\t*/\n" 12672 "}", 12673 format("{\n" 12674 "\t/*\n" 12675 " asdf\n" 12676 "\t*/\n" 12677 "}", 12678 Tab)); 12679 EXPECT_EQ("/* some\n" 12680 " comment */", 12681 format(" \t \t /* some\n" 12682 " \t \t comment */", 12683 Tab)); 12684 EXPECT_EQ("int a; /* some\n" 12685 " comment */", 12686 format(" \t \t int a; /* some\n" 12687 " \t \t comment */", 12688 Tab)); 12689 EXPECT_EQ("int a; /* some\n" 12690 "comment */", 12691 format(" \t \t int\ta; /* some\n" 12692 " \t \t comment */", 12693 Tab)); 12694 EXPECT_EQ("f(\"\t\t\"); /* some\n" 12695 " comment */", 12696 format(" \t \t f(\"\t\t\"); /* some\n" 12697 " \t \t comment */", 12698 Tab)); 12699 EXPECT_EQ("{\n" 12700 "\t/*\n" 12701 "\t * Comment\n" 12702 "\t */\n" 12703 "\tint i;\n" 12704 "}", 12705 format("{\n" 12706 "\t/*\n" 12707 "\t * Comment\n" 12708 "\t */\n" 12709 "\t int i;\n" 12710 "}", 12711 Tab)); 12712 Tab.TabWidth = 2; 12713 Tab.IndentWidth = 2; 12714 EXPECT_EQ("{\n" 12715 "\t/* aaaa\n" 12716 "\t\t bbbb */\n" 12717 "}", 12718 format("{\n" 12719 "/* aaaa\n" 12720 "\t bbbb */\n" 12721 "}", 12722 Tab)); 12723 EXPECT_EQ("{\n" 12724 "\t/*\n" 12725 "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n" 12726 "\t\tbbbbbbbbbbbbb\n" 12727 "\t*/\n" 12728 "}", 12729 format("{\n" 12730 "/*\n" 12731 "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 12732 "*/\n" 12733 "}", 12734 Tab)); 12735 Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 12736 Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 12737 Tab.TabWidth = 4; 12738 Tab.IndentWidth = 4; 12739 verifyFormat("class Assign {\n" 12740 "\tvoid f() {\n" 12741 "\t\tint x = 123;\n" 12742 "\t\tint random = 4;\n" 12743 "\t\tstd::string alphabet =\n" 12744 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 12745 "\t}\n" 12746 "};", 12747 Tab); 12748 12749 Tab.UseTab = FormatStyle::UT_AlignWithSpaces; 12750 Tab.TabWidth = 8; 12751 Tab.IndentWidth = 8; 12752 EXPECT_EQ("if (aaaaaaaa && // q\n" 12753 " bb) // w\n" 12754 "\t;", 12755 format("if (aaaaaaaa &&// q\n" 12756 "bb)// w\n" 12757 ";", 12758 Tab)); 12759 EXPECT_EQ("if (aaa && bbb) // w\n" 12760 "\t;", 12761 format("if(aaa&&bbb)// w\n" 12762 ";", 12763 Tab)); 12764 verifyFormat("class X {\n" 12765 "\tvoid f() {\n" 12766 "\t\tsomeFunction(parameter1,\n" 12767 "\t\t parameter2);\n" 12768 "\t}\n" 12769 "};", 12770 Tab); 12771 verifyFormat("#define A \\\n" 12772 "\tvoid f() { \\\n" 12773 "\t\tsomeFunction( \\\n" 12774 "\t\t parameter1, \\\n" 12775 "\t\t parameter2); \\\n" 12776 "\t}", 12777 Tab); 12778 Tab.TabWidth = 4; 12779 Tab.IndentWidth = 8; 12780 verifyFormat("class TabWidth4Indent8 {\n" 12781 "\t\tvoid f() {\n" 12782 "\t\t\t\tsomeFunction(parameter1,\n" 12783 "\t\t\t\t parameter2);\n" 12784 "\t\t}\n" 12785 "};", 12786 Tab); 12787 Tab.TabWidth = 4; 12788 Tab.IndentWidth = 4; 12789 verifyFormat("class TabWidth4Indent4 {\n" 12790 "\tvoid f() {\n" 12791 "\t\tsomeFunction(parameter1,\n" 12792 "\t\t parameter2);\n" 12793 "\t}\n" 12794 "};", 12795 Tab); 12796 Tab.TabWidth = 8; 12797 Tab.IndentWidth = 4; 12798 verifyFormat("class TabWidth8Indent4 {\n" 12799 " void f() {\n" 12800 "\tsomeFunction(parameter1,\n" 12801 "\t parameter2);\n" 12802 " }\n" 12803 "};", 12804 Tab); 12805 Tab.TabWidth = 8; 12806 Tab.IndentWidth = 8; 12807 EXPECT_EQ("/*\n" 12808 " a\t\tcomment\n" 12809 " in multiple lines\n" 12810 " */", 12811 format(" /*\t \t \n" 12812 " \t \t a\t\tcomment\t \t\n" 12813 " \t \t in multiple lines\t\n" 12814 " \t */", 12815 Tab)); 12816 verifyFormat("{\n" 12817 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 12818 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 12819 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 12820 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 12821 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 12822 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 12823 "};", 12824 Tab); 12825 verifyFormat("enum AA {\n" 12826 "\ta1, // Force multiple lines\n" 12827 "\ta2,\n" 12828 "\ta3\n" 12829 "};", 12830 Tab); 12831 EXPECT_EQ("if (aaaaaaaa && // q\n" 12832 " bb) // w\n" 12833 "\t;", 12834 format("if (aaaaaaaa &&// q\n" 12835 "bb)// w\n" 12836 ";", 12837 Tab)); 12838 verifyFormat("class X {\n" 12839 "\tvoid f() {\n" 12840 "\t\tsomeFunction(parameter1,\n" 12841 "\t\t parameter2);\n" 12842 "\t}\n" 12843 "};", 12844 Tab); 12845 verifyFormat("{\n" 12846 "\tQ(\n" 12847 "\t {\n" 12848 "\t\t int a;\n" 12849 "\t\t someFunction(aaaaaaaa,\n" 12850 "\t\t bbbbbbb);\n" 12851 "\t },\n" 12852 "\t p);\n" 12853 "}", 12854 Tab); 12855 EXPECT_EQ("{\n" 12856 "\t/* aaaa\n" 12857 "\t bbbb */\n" 12858 "}", 12859 format("{\n" 12860 "/* aaaa\n" 12861 " bbbb */\n" 12862 "}", 12863 Tab)); 12864 EXPECT_EQ("{\n" 12865 "\t/*\n" 12866 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 12867 "\t bbbbbbbbbbbbb\n" 12868 "\t*/\n" 12869 "}", 12870 format("{\n" 12871 "/*\n" 12872 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 12873 "*/\n" 12874 "}", 12875 Tab)); 12876 EXPECT_EQ("{\n" 12877 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 12878 "\t// bbbbbbbbbbbbb\n" 12879 "}", 12880 format("{\n" 12881 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 12882 "}", 12883 Tab)); 12884 EXPECT_EQ("{\n" 12885 "\t/*\n" 12886 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 12887 "\t bbbbbbbbbbbbb\n" 12888 "\t*/\n" 12889 "}", 12890 format("{\n" 12891 "\t/*\n" 12892 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 12893 "\t*/\n" 12894 "}", 12895 Tab)); 12896 EXPECT_EQ("{\n" 12897 "\t/*\n" 12898 "\n" 12899 "\t*/\n" 12900 "}", 12901 format("{\n" 12902 "\t/*\n" 12903 "\n" 12904 "\t*/\n" 12905 "}", 12906 Tab)); 12907 EXPECT_EQ("{\n" 12908 "\t/*\n" 12909 " asdf\n" 12910 "\t*/\n" 12911 "}", 12912 format("{\n" 12913 "\t/*\n" 12914 " asdf\n" 12915 "\t*/\n" 12916 "}", 12917 Tab)); 12918 EXPECT_EQ("/* some\n" 12919 " comment */", 12920 format(" \t \t /* some\n" 12921 " \t \t comment */", 12922 Tab)); 12923 EXPECT_EQ("int a; /* some\n" 12924 " comment */", 12925 format(" \t \t int a; /* some\n" 12926 " \t \t comment */", 12927 Tab)); 12928 EXPECT_EQ("int a; /* some\n" 12929 "comment */", 12930 format(" \t \t int\ta; /* some\n" 12931 " \t \t comment */", 12932 Tab)); 12933 EXPECT_EQ("f(\"\t\t\"); /* some\n" 12934 " comment */", 12935 format(" \t \t f(\"\t\t\"); /* some\n" 12936 " \t \t comment */", 12937 Tab)); 12938 EXPECT_EQ("{\n" 12939 "\t/*\n" 12940 "\t * Comment\n" 12941 "\t */\n" 12942 "\tint i;\n" 12943 "}", 12944 format("{\n" 12945 "\t/*\n" 12946 "\t * Comment\n" 12947 "\t */\n" 12948 "\t int i;\n" 12949 "}", 12950 Tab)); 12951 Tab.TabWidth = 2; 12952 Tab.IndentWidth = 2; 12953 EXPECT_EQ("{\n" 12954 "\t/* aaaa\n" 12955 "\t bbbb */\n" 12956 "}", 12957 format("{\n" 12958 "/* aaaa\n" 12959 " bbbb */\n" 12960 "}", 12961 Tab)); 12962 EXPECT_EQ("{\n" 12963 "\t/*\n" 12964 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 12965 "\t bbbbbbbbbbbbb\n" 12966 "\t*/\n" 12967 "}", 12968 format("{\n" 12969 "/*\n" 12970 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 12971 "*/\n" 12972 "}", 12973 Tab)); 12974 Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 12975 Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 12976 Tab.TabWidth = 4; 12977 Tab.IndentWidth = 4; 12978 verifyFormat("class Assign {\n" 12979 "\tvoid f() {\n" 12980 "\t\tint x = 123;\n" 12981 "\t\tint random = 4;\n" 12982 "\t\tstd::string alphabet =\n" 12983 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 12984 "\t}\n" 12985 "};", 12986 Tab); 12987 Tab.AlignOperands = FormatStyle::OAS_Align; 12988 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n" 12989 " cccccccccccccccccccc;", 12990 Tab); 12991 // no alignment 12992 verifyFormat("int aaaaaaaaaa =\n" 12993 "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 12994 Tab); 12995 verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n" 12996 " : bbbbbbbbbbbbbb ? 222222222222222\n" 12997 " : 333333333333333;", 12998 Tab); 12999 Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 13000 Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator; 13001 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n" 13002 " + cccccccccccccccccccc;", 13003 Tab); 13004 } 13005 13006 TEST_F(FormatTest, ZeroTabWidth) { 13007 FormatStyle Tab = getLLVMStyleWithColumns(42); 13008 Tab.IndentWidth = 8; 13009 Tab.UseTab = FormatStyle::UT_Never; 13010 Tab.TabWidth = 0; 13011 EXPECT_EQ("void a(){\n" 13012 " // line starts with '\t'\n" 13013 "};", 13014 format("void a(){\n" 13015 "\t// line starts with '\t'\n" 13016 "};", 13017 Tab)); 13018 13019 EXPECT_EQ("void a(){\n" 13020 " // line starts with '\t'\n" 13021 "};", 13022 format("void a(){\n" 13023 "\t\t// line starts with '\t'\n" 13024 "};", 13025 Tab)); 13026 13027 Tab.UseTab = FormatStyle::UT_ForIndentation; 13028 EXPECT_EQ("void a(){\n" 13029 " // line starts with '\t'\n" 13030 "};", 13031 format("void a(){\n" 13032 "\t// line starts with '\t'\n" 13033 "};", 13034 Tab)); 13035 13036 EXPECT_EQ("void a(){\n" 13037 " // line starts with '\t'\n" 13038 "};", 13039 format("void a(){\n" 13040 "\t\t// line starts with '\t'\n" 13041 "};", 13042 Tab)); 13043 13044 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 13045 EXPECT_EQ("void a(){\n" 13046 " // line starts with '\t'\n" 13047 "};", 13048 format("void a(){\n" 13049 "\t// line starts with '\t'\n" 13050 "};", 13051 Tab)); 13052 13053 EXPECT_EQ("void a(){\n" 13054 " // line starts with '\t'\n" 13055 "};", 13056 format("void a(){\n" 13057 "\t\t// line starts with '\t'\n" 13058 "};", 13059 Tab)); 13060 13061 Tab.UseTab = FormatStyle::UT_AlignWithSpaces; 13062 EXPECT_EQ("void a(){\n" 13063 " // line starts with '\t'\n" 13064 "};", 13065 format("void a(){\n" 13066 "\t// line starts with '\t'\n" 13067 "};", 13068 Tab)); 13069 13070 EXPECT_EQ("void a(){\n" 13071 " // line starts with '\t'\n" 13072 "};", 13073 format("void a(){\n" 13074 "\t\t// line starts with '\t'\n" 13075 "};", 13076 Tab)); 13077 13078 Tab.UseTab = FormatStyle::UT_Always; 13079 EXPECT_EQ("void a(){\n" 13080 "// line starts with '\t'\n" 13081 "};", 13082 format("void a(){\n" 13083 "\t// line starts with '\t'\n" 13084 "};", 13085 Tab)); 13086 13087 EXPECT_EQ("void a(){\n" 13088 "// line starts with '\t'\n" 13089 "};", 13090 format("void a(){\n" 13091 "\t\t// line starts with '\t'\n" 13092 "};", 13093 Tab)); 13094 } 13095 13096 TEST_F(FormatTest, CalculatesOriginalColumn) { 13097 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13098 "q\"; /* some\n" 13099 " comment */", 13100 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13101 "q\"; /* some\n" 13102 " comment */", 13103 getLLVMStyle())); 13104 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 13105 "/* some\n" 13106 " comment */", 13107 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 13108 " /* some\n" 13109 " comment */", 13110 getLLVMStyle())); 13111 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13112 "qqq\n" 13113 "/* some\n" 13114 " comment */", 13115 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13116 "qqq\n" 13117 " /* some\n" 13118 " comment */", 13119 getLLVMStyle())); 13120 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13121 "wwww; /* some\n" 13122 " comment */", 13123 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13124 "wwww; /* some\n" 13125 " comment */", 13126 getLLVMStyle())); 13127 } 13128 13129 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 13130 FormatStyle NoSpace = getLLVMStyle(); 13131 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 13132 13133 verifyFormat("while(true)\n" 13134 " continue;", 13135 NoSpace); 13136 verifyFormat("for(;;)\n" 13137 " continue;", 13138 NoSpace); 13139 verifyFormat("if(true)\n" 13140 " f();\n" 13141 "else if(true)\n" 13142 " f();", 13143 NoSpace); 13144 verifyFormat("do {\n" 13145 " do_something();\n" 13146 "} while(something());", 13147 NoSpace); 13148 verifyFormat("switch(x) {\n" 13149 "default:\n" 13150 " break;\n" 13151 "}", 13152 NoSpace); 13153 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 13154 verifyFormat("size_t x = sizeof(x);", NoSpace); 13155 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 13156 verifyFormat("auto f(int x) -> typeof(x);", NoSpace); 13157 verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace); 13158 verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace); 13159 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 13160 verifyFormat("alignas(128) char a[128];", NoSpace); 13161 verifyFormat("size_t x = alignof(MyType);", NoSpace); 13162 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 13163 verifyFormat("int f() throw(Deprecated);", NoSpace); 13164 verifyFormat("typedef void (*cb)(int);", NoSpace); 13165 verifyFormat("T A::operator()();", NoSpace); 13166 verifyFormat("X A::operator++(T);", NoSpace); 13167 verifyFormat("auto lambda = []() { return 0; };", NoSpace); 13168 13169 FormatStyle Space = getLLVMStyle(); 13170 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 13171 13172 verifyFormat("int f ();", Space); 13173 verifyFormat("void f (int a, T b) {\n" 13174 " while (true)\n" 13175 " continue;\n" 13176 "}", 13177 Space); 13178 verifyFormat("if (true)\n" 13179 " f ();\n" 13180 "else if (true)\n" 13181 " f ();", 13182 Space); 13183 verifyFormat("do {\n" 13184 " do_something ();\n" 13185 "} while (something ());", 13186 Space); 13187 verifyFormat("switch (x) {\n" 13188 "default:\n" 13189 " break;\n" 13190 "}", 13191 Space); 13192 verifyFormat("A::A () : a (1) {}", Space); 13193 verifyFormat("void f () __attribute__ ((asdf));", Space); 13194 verifyFormat("*(&a + 1);\n" 13195 "&((&a)[1]);\n" 13196 "a[(b + c) * d];\n" 13197 "(((a + 1) * 2) + 3) * 4;", 13198 Space); 13199 verifyFormat("#define A(x) x", Space); 13200 verifyFormat("#define A (x) x", Space); 13201 verifyFormat("#if defined(x)\n" 13202 "#endif", 13203 Space); 13204 verifyFormat("auto i = std::make_unique<int> (5);", Space); 13205 verifyFormat("size_t x = sizeof (x);", Space); 13206 verifyFormat("auto f (int x) -> decltype (x);", Space); 13207 verifyFormat("auto f (int x) -> typeof (x);", Space); 13208 verifyFormat("auto f (int x) -> _Atomic (x);", Space); 13209 verifyFormat("auto f (int x) -> __underlying_type (x);", Space); 13210 verifyFormat("int f (T x) noexcept (x.create ());", Space); 13211 verifyFormat("alignas (128) char a[128];", Space); 13212 verifyFormat("size_t x = alignof (MyType);", Space); 13213 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 13214 verifyFormat("int f () throw (Deprecated);", Space); 13215 verifyFormat("typedef void (*cb) (int);", Space); 13216 verifyFormat("T A::operator() ();", Space); 13217 verifyFormat("X A::operator++ (T);", Space); 13218 verifyFormat("auto lambda = [] () { return 0; };", Space); 13219 verifyFormat("int x = int (y);", Space); 13220 13221 FormatStyle SomeSpace = getLLVMStyle(); 13222 SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses; 13223 13224 verifyFormat("[]() -> float {}", SomeSpace); 13225 verifyFormat("[] (auto foo) {}", SomeSpace); 13226 verifyFormat("[foo]() -> int {}", SomeSpace); 13227 verifyFormat("int f();", SomeSpace); 13228 verifyFormat("void f (int a, T b) {\n" 13229 " while (true)\n" 13230 " continue;\n" 13231 "}", 13232 SomeSpace); 13233 verifyFormat("if (true)\n" 13234 " f();\n" 13235 "else if (true)\n" 13236 " f();", 13237 SomeSpace); 13238 verifyFormat("do {\n" 13239 " do_something();\n" 13240 "} while (something());", 13241 SomeSpace); 13242 verifyFormat("switch (x) {\n" 13243 "default:\n" 13244 " break;\n" 13245 "}", 13246 SomeSpace); 13247 verifyFormat("A::A() : a (1) {}", SomeSpace); 13248 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace); 13249 verifyFormat("*(&a + 1);\n" 13250 "&((&a)[1]);\n" 13251 "a[(b + c) * d];\n" 13252 "(((a + 1) * 2) + 3) * 4;", 13253 SomeSpace); 13254 verifyFormat("#define A(x) x", SomeSpace); 13255 verifyFormat("#define A (x) x", SomeSpace); 13256 verifyFormat("#if defined(x)\n" 13257 "#endif", 13258 SomeSpace); 13259 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace); 13260 verifyFormat("size_t x = sizeof (x);", SomeSpace); 13261 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace); 13262 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace); 13263 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace); 13264 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace); 13265 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace); 13266 verifyFormat("alignas (128) char a[128];", SomeSpace); 13267 verifyFormat("size_t x = alignof (MyType);", SomeSpace); 13268 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", 13269 SomeSpace); 13270 verifyFormat("int f() throw (Deprecated);", SomeSpace); 13271 verifyFormat("typedef void (*cb) (int);", SomeSpace); 13272 verifyFormat("T A::operator()();", SomeSpace); 13273 verifyFormat("X A::operator++ (T);", SomeSpace); 13274 verifyFormat("int x = int (y);", SomeSpace); 13275 verifyFormat("auto lambda = []() { return 0; };", SomeSpace); 13276 } 13277 13278 TEST_F(FormatTest, SpaceAfterLogicalNot) { 13279 FormatStyle Spaces = getLLVMStyle(); 13280 Spaces.SpaceAfterLogicalNot = true; 13281 13282 verifyFormat("bool x = ! y", Spaces); 13283 verifyFormat("if (! isFailure())", Spaces); 13284 verifyFormat("if (! (a && b))", Spaces); 13285 verifyFormat("\"Error!\"", Spaces); 13286 verifyFormat("! ! x", Spaces); 13287 } 13288 13289 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 13290 FormatStyle Spaces = getLLVMStyle(); 13291 13292 Spaces.SpacesInParentheses = true; 13293 verifyFormat("do_something( ::globalVar );", Spaces); 13294 verifyFormat("call( x, y, z );", Spaces); 13295 verifyFormat("call();", Spaces); 13296 verifyFormat("std::function<void( int, int )> callback;", Spaces); 13297 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 13298 Spaces); 13299 verifyFormat("while ( (bool)1 )\n" 13300 " continue;", 13301 Spaces); 13302 verifyFormat("for ( ;; )\n" 13303 " continue;", 13304 Spaces); 13305 verifyFormat("if ( true )\n" 13306 " f();\n" 13307 "else if ( true )\n" 13308 " f();", 13309 Spaces); 13310 verifyFormat("do {\n" 13311 " do_something( (int)i );\n" 13312 "} while ( something() );", 13313 Spaces); 13314 verifyFormat("switch ( x ) {\n" 13315 "default:\n" 13316 " break;\n" 13317 "}", 13318 Spaces); 13319 13320 Spaces.SpacesInParentheses = false; 13321 Spaces.SpacesInCStyleCastParentheses = true; 13322 verifyFormat("Type *A = ( Type * )P;", Spaces); 13323 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 13324 verifyFormat("x = ( int32 )y;", Spaces); 13325 verifyFormat("int a = ( int )(2.0f);", Spaces); 13326 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 13327 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 13328 verifyFormat("#define x (( int )-1)", Spaces); 13329 13330 // Run the first set of tests again with: 13331 Spaces.SpacesInParentheses = false; 13332 Spaces.SpaceInEmptyParentheses = true; 13333 Spaces.SpacesInCStyleCastParentheses = true; 13334 verifyFormat("call(x, y, z);", Spaces); 13335 verifyFormat("call( );", Spaces); 13336 verifyFormat("std::function<void(int, int)> callback;", Spaces); 13337 verifyFormat("while (( bool )1)\n" 13338 " continue;", 13339 Spaces); 13340 verifyFormat("for (;;)\n" 13341 " continue;", 13342 Spaces); 13343 verifyFormat("if (true)\n" 13344 " f( );\n" 13345 "else if (true)\n" 13346 " f( );", 13347 Spaces); 13348 verifyFormat("do {\n" 13349 " do_something(( int )i);\n" 13350 "} while (something( ));", 13351 Spaces); 13352 verifyFormat("switch (x) {\n" 13353 "default:\n" 13354 " break;\n" 13355 "}", 13356 Spaces); 13357 13358 // Run the first set of tests again with: 13359 Spaces.SpaceAfterCStyleCast = true; 13360 verifyFormat("call(x, y, z);", Spaces); 13361 verifyFormat("call( );", Spaces); 13362 verifyFormat("std::function<void(int, int)> callback;", Spaces); 13363 verifyFormat("while (( bool ) 1)\n" 13364 " continue;", 13365 Spaces); 13366 verifyFormat("for (;;)\n" 13367 " continue;", 13368 Spaces); 13369 verifyFormat("if (true)\n" 13370 " f( );\n" 13371 "else if (true)\n" 13372 " f( );", 13373 Spaces); 13374 verifyFormat("do {\n" 13375 " do_something(( int ) i);\n" 13376 "} while (something( ));", 13377 Spaces); 13378 verifyFormat("switch (x) {\n" 13379 "default:\n" 13380 " break;\n" 13381 "}", 13382 Spaces); 13383 13384 // Run subset of tests again with: 13385 Spaces.SpacesInCStyleCastParentheses = false; 13386 Spaces.SpaceAfterCStyleCast = true; 13387 verifyFormat("while ((bool) 1)\n" 13388 " continue;", 13389 Spaces); 13390 verifyFormat("do {\n" 13391 " do_something((int) i);\n" 13392 "} while (something( ));", 13393 Spaces); 13394 13395 verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces); 13396 verifyFormat("size_t idx = (size_t) a;", Spaces); 13397 verifyFormat("size_t idx = (size_t) (a - 1);", Spaces); 13398 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); 13399 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); 13400 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); 13401 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); 13402 Spaces.ColumnLimit = 80; 13403 Spaces.IndentWidth = 4; 13404 Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 13405 verifyFormat("void foo( ) {\n" 13406 " size_t foo = (*(function))(\n" 13407 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " 13408 "BarrrrrrrrrrrrLong,\n" 13409 " FoooooooooLooooong);\n" 13410 "}", 13411 Spaces); 13412 Spaces.SpaceAfterCStyleCast = false; 13413 verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces); 13414 verifyFormat("size_t idx = (size_t)a;", Spaces); 13415 verifyFormat("size_t idx = (size_t)(a - 1);", Spaces); 13416 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); 13417 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); 13418 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); 13419 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); 13420 13421 verifyFormat("void foo( ) {\n" 13422 " size_t foo = (*(function))(\n" 13423 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " 13424 "BarrrrrrrrrrrrLong,\n" 13425 " FoooooooooLooooong);\n" 13426 "}", 13427 Spaces); 13428 } 13429 13430 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 13431 verifyFormat("int a[5];"); 13432 verifyFormat("a[3] += 42;"); 13433 13434 FormatStyle Spaces = getLLVMStyle(); 13435 Spaces.SpacesInSquareBrackets = true; 13436 // Not lambdas. 13437 verifyFormat("int a[ 5 ];", Spaces); 13438 verifyFormat("a[ 3 ] += 42;", Spaces); 13439 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 13440 verifyFormat("double &operator[](int i) { return 0; }\n" 13441 "int i;", 13442 Spaces); 13443 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 13444 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 13445 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 13446 // Lambdas. 13447 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 13448 verifyFormat("return [ i, args... ] {};", Spaces); 13449 verifyFormat("int foo = [ &bar ]() {};", Spaces); 13450 verifyFormat("int foo = [ = ]() {};", Spaces); 13451 verifyFormat("int foo = [ & ]() {};", Spaces); 13452 verifyFormat("int foo = [ =, &bar ]() {};", Spaces); 13453 verifyFormat("int foo = [ &bar, = ]() {};", Spaces); 13454 } 13455 13456 TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) { 13457 FormatStyle NoSpaceStyle = getLLVMStyle(); 13458 verifyFormat("int a[5];", NoSpaceStyle); 13459 verifyFormat("a[3] += 42;", NoSpaceStyle); 13460 13461 verifyFormat("int a[1];", NoSpaceStyle); 13462 verifyFormat("int 1 [a];", NoSpaceStyle); 13463 verifyFormat("int a[1][2];", NoSpaceStyle); 13464 verifyFormat("a[7] = 5;", NoSpaceStyle); 13465 verifyFormat("int a = (f())[23];", NoSpaceStyle); 13466 verifyFormat("f([] {})", NoSpaceStyle); 13467 13468 FormatStyle Space = getLLVMStyle(); 13469 Space.SpaceBeforeSquareBrackets = true; 13470 verifyFormat("int c = []() -> int { return 2; }();\n", Space); 13471 verifyFormat("return [i, args...] {};", Space); 13472 13473 verifyFormat("int a [5];", Space); 13474 verifyFormat("a [3] += 42;", Space); 13475 verifyFormat("constexpr char hello []{\"hello\"};", Space); 13476 verifyFormat("double &operator[](int i) { return 0; }\n" 13477 "int i;", 13478 Space); 13479 verifyFormat("std::unique_ptr<int []> foo() {}", Space); 13480 verifyFormat("int i = a [a][a]->f();", Space); 13481 verifyFormat("int i = (*b) [a]->f();", Space); 13482 13483 verifyFormat("int a [1];", Space); 13484 verifyFormat("int 1 [a];", Space); 13485 verifyFormat("int a [1][2];", Space); 13486 verifyFormat("a [7] = 5;", Space); 13487 verifyFormat("int a = (f()) [23];", Space); 13488 verifyFormat("f([] {})", Space); 13489 } 13490 13491 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 13492 verifyFormat("int a = 5;"); 13493 verifyFormat("a += 42;"); 13494 verifyFormat("a or_eq 8;"); 13495 13496 FormatStyle Spaces = getLLVMStyle(); 13497 Spaces.SpaceBeforeAssignmentOperators = false; 13498 verifyFormat("int a= 5;", Spaces); 13499 verifyFormat("a+= 42;", Spaces); 13500 verifyFormat("a or_eq 8;", Spaces); 13501 } 13502 13503 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) { 13504 verifyFormat("class Foo : public Bar {};"); 13505 verifyFormat("Foo::Foo() : foo(1) {}"); 13506 verifyFormat("for (auto a : b) {\n}"); 13507 verifyFormat("int x = a ? b : c;"); 13508 verifyFormat("{\n" 13509 "label0:\n" 13510 " int x = 0;\n" 13511 "}"); 13512 verifyFormat("switch (x) {\n" 13513 "case 1:\n" 13514 "default:\n" 13515 "}"); 13516 verifyFormat("switch (allBraces) {\n" 13517 "case 1: {\n" 13518 " break;\n" 13519 "}\n" 13520 "case 2: {\n" 13521 " [[fallthrough]];\n" 13522 "}\n" 13523 "default: {\n" 13524 " break;\n" 13525 "}\n" 13526 "}"); 13527 13528 FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30); 13529 CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false; 13530 verifyFormat("class Foo : public Bar {};", CtorInitializerStyle); 13531 verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle); 13532 verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle); 13533 verifyFormat("int x = a ? b : c;", CtorInitializerStyle); 13534 verifyFormat("{\n" 13535 "label1:\n" 13536 " int x = 0;\n" 13537 "}", 13538 CtorInitializerStyle); 13539 verifyFormat("switch (x) {\n" 13540 "case 1:\n" 13541 "default:\n" 13542 "}", 13543 CtorInitializerStyle); 13544 verifyFormat("switch (allBraces) {\n" 13545 "case 1: {\n" 13546 " break;\n" 13547 "}\n" 13548 "case 2: {\n" 13549 " [[fallthrough]];\n" 13550 "}\n" 13551 "default: {\n" 13552 " break;\n" 13553 "}\n" 13554 "}", 13555 CtorInitializerStyle); 13556 CtorInitializerStyle.BreakConstructorInitializers = 13557 FormatStyle::BCIS_AfterColon; 13558 verifyFormat("Fooooooooooo::Fooooooooooo():\n" 13559 " aaaaaaaaaaaaaaaa(1),\n" 13560 " bbbbbbbbbbbbbbbb(2) {}", 13561 CtorInitializerStyle); 13562 CtorInitializerStyle.BreakConstructorInitializers = 13563 FormatStyle::BCIS_BeforeComma; 13564 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 13565 " : aaaaaaaaaaaaaaaa(1)\n" 13566 " , bbbbbbbbbbbbbbbb(2) {}", 13567 CtorInitializerStyle); 13568 CtorInitializerStyle.BreakConstructorInitializers = 13569 FormatStyle::BCIS_BeforeColon; 13570 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 13571 " : aaaaaaaaaaaaaaaa(1),\n" 13572 " bbbbbbbbbbbbbbbb(2) {}", 13573 CtorInitializerStyle); 13574 CtorInitializerStyle.ConstructorInitializerIndentWidth = 0; 13575 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 13576 ": aaaaaaaaaaaaaaaa(1),\n" 13577 " bbbbbbbbbbbbbbbb(2) {}", 13578 CtorInitializerStyle); 13579 13580 FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30); 13581 InheritanceStyle.SpaceBeforeInheritanceColon = false; 13582 verifyFormat("class Foo: public Bar {};", InheritanceStyle); 13583 verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle); 13584 verifyFormat("for (auto a : b) {\n}", InheritanceStyle); 13585 verifyFormat("int x = a ? b : c;", InheritanceStyle); 13586 verifyFormat("{\n" 13587 "label2:\n" 13588 " int x = 0;\n" 13589 "}", 13590 InheritanceStyle); 13591 verifyFormat("switch (x) {\n" 13592 "case 1:\n" 13593 "default:\n" 13594 "}", 13595 InheritanceStyle); 13596 verifyFormat("switch (allBraces) {\n" 13597 "case 1: {\n" 13598 " break;\n" 13599 "}\n" 13600 "case 2: {\n" 13601 " [[fallthrough]];\n" 13602 "}\n" 13603 "default: {\n" 13604 " break;\n" 13605 "}\n" 13606 "}", 13607 InheritanceStyle); 13608 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma; 13609 verifyFormat("class Foooooooooooooooooooooo\n" 13610 " : public aaaaaaaaaaaaaaaaaa,\n" 13611 " public bbbbbbbbbbbbbbbbbb {\n" 13612 "}", 13613 InheritanceStyle); 13614 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon; 13615 verifyFormat("class Foooooooooooooooooooooo:\n" 13616 " public aaaaaaaaaaaaaaaaaa,\n" 13617 " public bbbbbbbbbbbbbbbbbb {\n" 13618 "}", 13619 InheritanceStyle); 13620 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 13621 verifyFormat("class Foooooooooooooooooooooo\n" 13622 " : public aaaaaaaaaaaaaaaaaa\n" 13623 " , public bbbbbbbbbbbbbbbbbb {\n" 13624 "}", 13625 InheritanceStyle); 13626 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 13627 verifyFormat("class Foooooooooooooooooooooo\n" 13628 " : public aaaaaaaaaaaaaaaaaa,\n" 13629 " public bbbbbbbbbbbbbbbbbb {\n" 13630 "}", 13631 InheritanceStyle); 13632 InheritanceStyle.ConstructorInitializerIndentWidth = 0; 13633 verifyFormat("class Foooooooooooooooooooooo\n" 13634 ": public aaaaaaaaaaaaaaaaaa,\n" 13635 " public bbbbbbbbbbbbbbbbbb {}", 13636 InheritanceStyle); 13637 13638 FormatStyle ForLoopStyle = getLLVMStyle(); 13639 ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false; 13640 verifyFormat("class Foo : public Bar {};", ForLoopStyle); 13641 verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle); 13642 verifyFormat("for (auto a: b) {\n}", ForLoopStyle); 13643 verifyFormat("int x = a ? b : c;", ForLoopStyle); 13644 verifyFormat("{\n" 13645 "label2:\n" 13646 " int x = 0;\n" 13647 "}", 13648 ForLoopStyle); 13649 verifyFormat("switch (x) {\n" 13650 "case 1:\n" 13651 "default:\n" 13652 "}", 13653 ForLoopStyle); 13654 verifyFormat("switch (allBraces) {\n" 13655 "case 1: {\n" 13656 " break;\n" 13657 "}\n" 13658 "case 2: {\n" 13659 " [[fallthrough]];\n" 13660 "}\n" 13661 "default: {\n" 13662 " break;\n" 13663 "}\n" 13664 "}", 13665 ForLoopStyle); 13666 13667 FormatStyle CaseStyle = getLLVMStyle(); 13668 CaseStyle.SpaceBeforeCaseColon = true; 13669 verifyFormat("class Foo : public Bar {};", CaseStyle); 13670 verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle); 13671 verifyFormat("for (auto a : b) {\n}", CaseStyle); 13672 verifyFormat("int x = a ? b : c;", CaseStyle); 13673 verifyFormat("switch (x) {\n" 13674 "case 1 :\n" 13675 "default :\n" 13676 "}", 13677 CaseStyle); 13678 verifyFormat("switch (allBraces) {\n" 13679 "case 1 : {\n" 13680 " break;\n" 13681 "}\n" 13682 "case 2 : {\n" 13683 " [[fallthrough]];\n" 13684 "}\n" 13685 "default : {\n" 13686 " break;\n" 13687 "}\n" 13688 "}", 13689 CaseStyle); 13690 13691 FormatStyle NoSpaceStyle = getLLVMStyle(); 13692 EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false); 13693 NoSpaceStyle.SpaceBeforeCtorInitializerColon = false; 13694 NoSpaceStyle.SpaceBeforeInheritanceColon = false; 13695 NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 13696 verifyFormat("class Foo: public Bar {};", NoSpaceStyle); 13697 verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle); 13698 verifyFormat("for (auto a: b) {\n}", NoSpaceStyle); 13699 verifyFormat("int x = a ? b : c;", NoSpaceStyle); 13700 verifyFormat("{\n" 13701 "label3:\n" 13702 " int x = 0;\n" 13703 "}", 13704 NoSpaceStyle); 13705 verifyFormat("switch (x) {\n" 13706 "case 1:\n" 13707 "default:\n" 13708 "}", 13709 NoSpaceStyle); 13710 verifyFormat("switch (allBraces) {\n" 13711 "case 1: {\n" 13712 " break;\n" 13713 "}\n" 13714 "case 2: {\n" 13715 " [[fallthrough]];\n" 13716 "}\n" 13717 "default: {\n" 13718 " break;\n" 13719 "}\n" 13720 "}", 13721 NoSpaceStyle); 13722 13723 FormatStyle InvertedSpaceStyle = getLLVMStyle(); 13724 InvertedSpaceStyle.SpaceBeforeCaseColon = true; 13725 InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false; 13726 InvertedSpaceStyle.SpaceBeforeInheritanceColon = false; 13727 InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 13728 verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle); 13729 verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle); 13730 verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle); 13731 verifyFormat("int x = a ? b : c;", InvertedSpaceStyle); 13732 verifyFormat("{\n" 13733 "label3:\n" 13734 " int x = 0;\n" 13735 "}", 13736 InvertedSpaceStyle); 13737 verifyFormat("switch (x) {\n" 13738 "case 1 :\n" 13739 "case 2 : {\n" 13740 " break;\n" 13741 "}\n" 13742 "default :\n" 13743 " break;\n" 13744 "}", 13745 InvertedSpaceStyle); 13746 verifyFormat("switch (allBraces) {\n" 13747 "case 1 : {\n" 13748 " break;\n" 13749 "}\n" 13750 "case 2 : {\n" 13751 " [[fallthrough]];\n" 13752 "}\n" 13753 "default : {\n" 13754 " break;\n" 13755 "}\n" 13756 "}", 13757 InvertedSpaceStyle); 13758 } 13759 13760 TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) { 13761 FormatStyle Style = getLLVMStyle(); 13762 13763 Style.PointerAlignment = FormatStyle::PAS_Left; 13764 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; 13765 verifyFormat("void* const* x = NULL;", Style); 13766 13767 #define verifyQualifierSpaces(Code, Pointers, Qualifiers) \ 13768 do { \ 13769 Style.PointerAlignment = FormatStyle::Pointers; \ 13770 Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers; \ 13771 verifyFormat(Code, Style); \ 13772 } while (false) 13773 13774 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default); 13775 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default); 13776 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default); 13777 13778 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before); 13779 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before); 13780 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before); 13781 13782 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After); 13783 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After); 13784 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After); 13785 13786 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both); 13787 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both); 13788 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both); 13789 13790 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default); 13791 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, 13792 SAPQ_Default); 13793 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 13794 SAPQ_Default); 13795 13796 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before); 13797 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, 13798 SAPQ_Before); 13799 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 13800 SAPQ_Before); 13801 13802 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After); 13803 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After); 13804 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 13805 SAPQ_After); 13806 13807 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both); 13808 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both); 13809 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both); 13810 13811 #undef verifyQualifierSpaces 13812 13813 FormatStyle Spaces = getLLVMStyle(); 13814 Spaces.AttributeMacros.push_back("qualified"); 13815 Spaces.PointerAlignment = FormatStyle::PAS_Right; 13816 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; 13817 verifyFormat("SomeType *volatile *a = NULL;", Spaces); 13818 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces); 13819 verifyFormat("std::vector<SomeType *const *> x;", Spaces); 13820 verifyFormat("std::vector<SomeType *qualified *> x;", Spaces); 13821 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 13822 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before; 13823 verifyFormat("SomeType * volatile *a = NULL;", Spaces); 13824 verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces); 13825 verifyFormat("std::vector<SomeType * const *> x;", Spaces); 13826 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces); 13827 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 13828 13829 // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left. 13830 Spaces.PointerAlignment = FormatStyle::PAS_Left; 13831 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before; 13832 verifyFormat("SomeType* volatile* a = NULL;", Spaces); 13833 verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces); 13834 verifyFormat("std::vector<SomeType* const*> x;", Spaces); 13835 verifyFormat("std::vector<SomeType* qualified*> x;", Spaces); 13836 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 13837 // However, setting it to SAPQ_After should add spaces after __attribute, etc. 13838 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After; 13839 verifyFormat("SomeType* volatile * a = NULL;", Spaces); 13840 verifyFormat("SomeType* __attribute__((attr)) * a = NULL;", Spaces); 13841 verifyFormat("std::vector<SomeType* const *> x;", Spaces); 13842 verifyFormat("std::vector<SomeType* qualified *> x;", Spaces); 13843 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 13844 13845 // PAS_Middle should not have any noticeable changes even for SAPQ_Both 13846 Spaces.PointerAlignment = FormatStyle::PAS_Middle; 13847 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After; 13848 verifyFormat("SomeType * volatile * a = NULL;", Spaces); 13849 verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces); 13850 verifyFormat("std::vector<SomeType * const *> x;", Spaces); 13851 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces); 13852 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 13853 } 13854 13855 TEST_F(FormatTest, AlignConsecutiveMacros) { 13856 FormatStyle Style = getLLVMStyle(); 13857 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 13858 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 13859 Style.AlignConsecutiveMacros = FormatStyle::ACS_None; 13860 13861 verifyFormat("#define a 3\n" 13862 "#define bbbb 4\n" 13863 "#define ccc (5)", 13864 Style); 13865 13866 verifyFormat("#define f(x) (x * x)\n" 13867 "#define fff(x, y, z) (x * y + z)\n" 13868 "#define ffff(x, y) (x - y)", 13869 Style); 13870 13871 verifyFormat("#define foo(x, y) (x + y)\n" 13872 "#define bar (5, 6)(2 + 2)", 13873 Style); 13874 13875 verifyFormat("#define a 3\n" 13876 "#define bbbb 4\n" 13877 "#define ccc (5)\n" 13878 "#define f(x) (x * x)\n" 13879 "#define fff(x, y, z) (x * y + z)\n" 13880 "#define ffff(x, y) (x - y)", 13881 Style); 13882 13883 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 13884 verifyFormat("#define a 3\n" 13885 "#define bbbb 4\n" 13886 "#define ccc (5)", 13887 Style); 13888 13889 verifyFormat("#define f(x) (x * x)\n" 13890 "#define fff(x, y, z) (x * y + z)\n" 13891 "#define ffff(x, y) (x - y)", 13892 Style); 13893 13894 verifyFormat("#define foo(x, y) (x + y)\n" 13895 "#define bar (5, 6)(2 + 2)", 13896 Style); 13897 13898 verifyFormat("#define a 3\n" 13899 "#define bbbb 4\n" 13900 "#define ccc (5)\n" 13901 "#define f(x) (x * x)\n" 13902 "#define fff(x, y, z) (x * y + z)\n" 13903 "#define ffff(x, y) (x - y)", 13904 Style); 13905 13906 verifyFormat("#define a 5\n" 13907 "#define foo(x, y) (x + y)\n" 13908 "#define CCC (6)\n" 13909 "auto lambda = []() {\n" 13910 " auto ii = 0;\n" 13911 " float j = 0;\n" 13912 " return 0;\n" 13913 "};\n" 13914 "int i = 0;\n" 13915 "float i2 = 0;\n" 13916 "auto v = type{\n" 13917 " i = 1, //\n" 13918 " (i = 2), //\n" 13919 " i = 3 //\n" 13920 "};", 13921 Style); 13922 13923 Style.AlignConsecutiveMacros = FormatStyle::ACS_None; 13924 Style.ColumnLimit = 20; 13925 13926 verifyFormat("#define a \\\n" 13927 " \"aabbbbbbbbbbbb\"\n" 13928 "#define D \\\n" 13929 " \"aabbbbbbbbbbbb\" \\\n" 13930 " \"ccddeeeeeeeee\"\n" 13931 "#define B \\\n" 13932 " \"QQQQQQQQQQQQQ\" \\\n" 13933 " \"FFFFFFFFFFFFF\" \\\n" 13934 " \"LLLLLLLL\"\n", 13935 Style); 13936 13937 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 13938 verifyFormat("#define a \\\n" 13939 " \"aabbbbbbbbbbbb\"\n" 13940 "#define D \\\n" 13941 " \"aabbbbbbbbbbbb\" \\\n" 13942 " \"ccddeeeeeeeee\"\n" 13943 "#define B \\\n" 13944 " \"QQQQQQQQQQQQQ\" \\\n" 13945 " \"FFFFFFFFFFFFF\" \\\n" 13946 " \"LLLLLLLL\"\n", 13947 Style); 13948 13949 // Test across comments 13950 Style.MaxEmptyLinesToKeep = 10; 13951 Style.ReflowComments = false; 13952 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossComments; 13953 EXPECT_EQ("#define a 3\n" 13954 "// line comment\n" 13955 "#define bbbb 4\n" 13956 "#define ccc (5)", 13957 format("#define a 3\n" 13958 "// line comment\n" 13959 "#define bbbb 4\n" 13960 "#define ccc (5)", 13961 Style)); 13962 13963 EXPECT_EQ("#define a 3\n" 13964 "/* block comment */\n" 13965 "#define bbbb 4\n" 13966 "#define ccc (5)", 13967 format("#define a 3\n" 13968 "/* block comment */\n" 13969 "#define bbbb 4\n" 13970 "#define ccc (5)", 13971 Style)); 13972 13973 EXPECT_EQ("#define a 3\n" 13974 "/* multi-line *\n" 13975 " * block comment */\n" 13976 "#define bbbb 4\n" 13977 "#define ccc (5)", 13978 format("#define a 3\n" 13979 "/* multi-line *\n" 13980 " * block comment */\n" 13981 "#define bbbb 4\n" 13982 "#define ccc (5)", 13983 Style)); 13984 13985 EXPECT_EQ("#define a 3\n" 13986 "// multi-line line comment\n" 13987 "//\n" 13988 "#define bbbb 4\n" 13989 "#define ccc (5)", 13990 format("#define a 3\n" 13991 "// multi-line line comment\n" 13992 "//\n" 13993 "#define bbbb 4\n" 13994 "#define ccc (5)", 13995 Style)); 13996 13997 EXPECT_EQ("#define a 3\n" 13998 "// empty lines still break.\n" 13999 "\n" 14000 "#define bbbb 4\n" 14001 "#define ccc (5)", 14002 format("#define a 3\n" 14003 "// empty lines still break.\n" 14004 "\n" 14005 "#define bbbb 4\n" 14006 "#define ccc (5)", 14007 Style)); 14008 14009 // Test across empty lines 14010 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLines; 14011 EXPECT_EQ("#define a 3\n" 14012 "\n" 14013 "#define bbbb 4\n" 14014 "#define ccc (5)", 14015 format("#define a 3\n" 14016 "\n" 14017 "#define bbbb 4\n" 14018 "#define ccc (5)", 14019 Style)); 14020 14021 EXPECT_EQ("#define a 3\n" 14022 "\n" 14023 "\n" 14024 "\n" 14025 "#define bbbb 4\n" 14026 "#define ccc (5)", 14027 format("#define a 3\n" 14028 "\n" 14029 "\n" 14030 "\n" 14031 "#define bbbb 4\n" 14032 "#define ccc (5)", 14033 Style)); 14034 14035 EXPECT_EQ("#define a 3\n" 14036 "// comments should break alignment\n" 14037 "//\n" 14038 "#define bbbb 4\n" 14039 "#define ccc (5)", 14040 format("#define a 3\n" 14041 "// comments should break alignment\n" 14042 "//\n" 14043 "#define bbbb 4\n" 14044 "#define ccc (5)", 14045 Style)); 14046 14047 // Test across empty lines and comments 14048 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLinesAndComments; 14049 verifyFormat("#define a 3\n" 14050 "\n" 14051 "// line comment\n" 14052 "#define bbbb 4\n" 14053 "#define ccc (5)", 14054 Style); 14055 14056 EXPECT_EQ("#define a 3\n" 14057 "\n" 14058 "\n" 14059 "/* multi-line *\n" 14060 " * block comment */\n" 14061 "\n" 14062 "\n" 14063 "#define bbbb 4\n" 14064 "#define ccc (5)", 14065 format("#define a 3\n" 14066 "\n" 14067 "\n" 14068 "/* multi-line *\n" 14069 " * block comment */\n" 14070 "\n" 14071 "\n" 14072 "#define bbbb 4\n" 14073 "#define ccc (5)", 14074 Style)); 14075 14076 EXPECT_EQ("#define a 3\n" 14077 "\n" 14078 "\n" 14079 "/* multi-line *\n" 14080 " * block comment */\n" 14081 "\n" 14082 "\n" 14083 "#define bbbb 4\n" 14084 "#define ccc (5)", 14085 format("#define a 3\n" 14086 "\n" 14087 "\n" 14088 "/* multi-line *\n" 14089 " * block comment */\n" 14090 "\n" 14091 "\n" 14092 "#define bbbb 4\n" 14093 "#define ccc (5)", 14094 Style)); 14095 } 14096 14097 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) { 14098 FormatStyle Alignment = getLLVMStyle(); 14099 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 14100 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossEmptyLines; 14101 14102 Alignment.MaxEmptyLinesToKeep = 10; 14103 /* Test alignment across empty lines */ 14104 EXPECT_EQ("int a = 5;\n" 14105 "\n" 14106 "int oneTwoThree = 123;", 14107 format("int a = 5;\n" 14108 "\n" 14109 "int oneTwoThree= 123;", 14110 Alignment)); 14111 EXPECT_EQ("int a = 5;\n" 14112 "int one = 1;\n" 14113 "\n" 14114 "int oneTwoThree = 123;", 14115 format("int a = 5;\n" 14116 "int one = 1;\n" 14117 "\n" 14118 "int oneTwoThree = 123;", 14119 Alignment)); 14120 EXPECT_EQ("int a = 5;\n" 14121 "int one = 1;\n" 14122 "\n" 14123 "int oneTwoThree = 123;\n" 14124 "int oneTwo = 12;", 14125 format("int a = 5;\n" 14126 "int one = 1;\n" 14127 "\n" 14128 "int oneTwoThree = 123;\n" 14129 "int oneTwo = 12;", 14130 Alignment)); 14131 14132 /* Test across comments */ 14133 EXPECT_EQ("int a = 5;\n" 14134 "/* block comment */\n" 14135 "int oneTwoThree = 123;", 14136 format("int a = 5;\n" 14137 "/* block comment */\n" 14138 "int oneTwoThree=123;", 14139 Alignment)); 14140 14141 EXPECT_EQ("int a = 5;\n" 14142 "// line comment\n" 14143 "int oneTwoThree = 123;", 14144 format("int a = 5;\n" 14145 "// line comment\n" 14146 "int oneTwoThree=123;", 14147 Alignment)); 14148 14149 /* Test across comments and newlines */ 14150 EXPECT_EQ("int a = 5;\n" 14151 "\n" 14152 "/* block comment */\n" 14153 "int oneTwoThree = 123;", 14154 format("int a = 5;\n" 14155 "\n" 14156 "/* block comment */\n" 14157 "int oneTwoThree=123;", 14158 Alignment)); 14159 14160 EXPECT_EQ("int a = 5;\n" 14161 "\n" 14162 "// line comment\n" 14163 "int oneTwoThree = 123;", 14164 format("int a = 5;\n" 14165 "\n" 14166 "// line comment\n" 14167 "int oneTwoThree=123;", 14168 Alignment)); 14169 } 14170 14171 TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) { 14172 FormatStyle Alignment = getLLVMStyle(); 14173 Alignment.AlignConsecutiveDeclarations = 14174 FormatStyle::ACS_AcrossEmptyLinesAndComments; 14175 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 14176 14177 Alignment.MaxEmptyLinesToKeep = 10; 14178 /* Test alignment across empty lines */ 14179 EXPECT_EQ("int a = 5;\n" 14180 "\n" 14181 "float const oneTwoThree = 123;", 14182 format("int a = 5;\n" 14183 "\n" 14184 "float const oneTwoThree = 123;", 14185 Alignment)); 14186 EXPECT_EQ("int a = 5;\n" 14187 "float const one = 1;\n" 14188 "\n" 14189 "int oneTwoThree = 123;", 14190 format("int a = 5;\n" 14191 "float const one = 1;\n" 14192 "\n" 14193 "int oneTwoThree = 123;", 14194 Alignment)); 14195 14196 /* Test across comments */ 14197 EXPECT_EQ("float const a = 5;\n" 14198 "/* block comment */\n" 14199 "int oneTwoThree = 123;", 14200 format("float const a = 5;\n" 14201 "/* block comment */\n" 14202 "int oneTwoThree=123;", 14203 Alignment)); 14204 14205 EXPECT_EQ("float const a = 5;\n" 14206 "// line comment\n" 14207 "int oneTwoThree = 123;", 14208 format("float const a = 5;\n" 14209 "// line comment\n" 14210 "int oneTwoThree=123;", 14211 Alignment)); 14212 14213 /* Test across comments and newlines */ 14214 EXPECT_EQ("float const a = 5;\n" 14215 "\n" 14216 "/* block comment */\n" 14217 "int oneTwoThree = 123;", 14218 format("float const a = 5;\n" 14219 "\n" 14220 "/* block comment */\n" 14221 "int oneTwoThree=123;", 14222 Alignment)); 14223 14224 EXPECT_EQ("float const a = 5;\n" 14225 "\n" 14226 "// line comment\n" 14227 "int oneTwoThree = 123;", 14228 format("float const a = 5;\n" 14229 "\n" 14230 "// line comment\n" 14231 "int oneTwoThree=123;", 14232 Alignment)); 14233 } 14234 14235 TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) { 14236 FormatStyle Alignment = getLLVMStyle(); 14237 Alignment.AlignConsecutiveBitFields = 14238 FormatStyle::ACS_AcrossEmptyLinesAndComments; 14239 14240 Alignment.MaxEmptyLinesToKeep = 10; 14241 /* Test alignment across empty lines */ 14242 EXPECT_EQ("int a : 5;\n" 14243 "\n" 14244 "int longbitfield : 6;", 14245 format("int a : 5;\n" 14246 "\n" 14247 "int longbitfield : 6;", 14248 Alignment)); 14249 EXPECT_EQ("int a : 5;\n" 14250 "int one : 1;\n" 14251 "\n" 14252 "int longbitfield : 6;", 14253 format("int a : 5;\n" 14254 "int one : 1;\n" 14255 "\n" 14256 "int longbitfield : 6;", 14257 Alignment)); 14258 14259 /* Test across comments */ 14260 EXPECT_EQ("int a : 5;\n" 14261 "/* block comment */\n" 14262 "int longbitfield : 6;", 14263 format("int a : 5;\n" 14264 "/* block comment */\n" 14265 "int longbitfield : 6;", 14266 Alignment)); 14267 EXPECT_EQ("int a : 5;\n" 14268 "int one : 1;\n" 14269 "// line comment\n" 14270 "int longbitfield : 6;", 14271 format("int a : 5;\n" 14272 "int one : 1;\n" 14273 "// line comment\n" 14274 "int longbitfield : 6;", 14275 Alignment)); 14276 14277 /* Test across comments and newlines */ 14278 EXPECT_EQ("int a : 5;\n" 14279 "/* block comment */\n" 14280 "\n" 14281 "int longbitfield : 6;", 14282 format("int a : 5;\n" 14283 "/* block comment */\n" 14284 "\n" 14285 "int longbitfield : 6;", 14286 Alignment)); 14287 EXPECT_EQ("int a : 5;\n" 14288 "int one : 1;\n" 14289 "\n" 14290 "// line comment\n" 14291 "\n" 14292 "int longbitfield : 6;", 14293 format("int a : 5;\n" 14294 "int one : 1;\n" 14295 "\n" 14296 "// line comment \n" 14297 "\n" 14298 "int longbitfield : 6;", 14299 Alignment)); 14300 } 14301 14302 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) { 14303 FormatStyle Alignment = getLLVMStyle(); 14304 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 14305 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossComments; 14306 14307 Alignment.MaxEmptyLinesToKeep = 10; 14308 /* Test alignment across empty lines */ 14309 EXPECT_EQ("int a = 5;\n" 14310 "\n" 14311 "int oneTwoThree = 123;", 14312 format("int a = 5;\n" 14313 "\n" 14314 "int oneTwoThree= 123;", 14315 Alignment)); 14316 EXPECT_EQ("int a = 5;\n" 14317 "int one = 1;\n" 14318 "\n" 14319 "int oneTwoThree = 123;", 14320 format("int a = 5;\n" 14321 "int one = 1;\n" 14322 "\n" 14323 "int oneTwoThree = 123;", 14324 Alignment)); 14325 14326 /* Test across comments */ 14327 EXPECT_EQ("int a = 5;\n" 14328 "/* block comment */\n" 14329 "int oneTwoThree = 123;", 14330 format("int a = 5;\n" 14331 "/* block comment */\n" 14332 "int oneTwoThree=123;", 14333 Alignment)); 14334 14335 EXPECT_EQ("int a = 5;\n" 14336 "// line comment\n" 14337 "int oneTwoThree = 123;", 14338 format("int a = 5;\n" 14339 "// line comment\n" 14340 "int oneTwoThree=123;", 14341 Alignment)); 14342 14343 EXPECT_EQ("int a = 5;\n" 14344 "/*\n" 14345 " * multi-line block comment\n" 14346 " */\n" 14347 "int oneTwoThree = 123;", 14348 format("int a = 5;\n" 14349 "/*\n" 14350 " * multi-line block comment\n" 14351 " */\n" 14352 "int oneTwoThree=123;", 14353 Alignment)); 14354 14355 EXPECT_EQ("int a = 5;\n" 14356 "//\n" 14357 "// multi-line line comment\n" 14358 "//\n" 14359 "int oneTwoThree = 123;", 14360 format("int a = 5;\n" 14361 "//\n" 14362 "// multi-line line comment\n" 14363 "//\n" 14364 "int oneTwoThree=123;", 14365 Alignment)); 14366 14367 /* Test across comments and newlines */ 14368 EXPECT_EQ("int a = 5;\n" 14369 "\n" 14370 "/* block comment */\n" 14371 "int oneTwoThree = 123;", 14372 format("int a = 5;\n" 14373 "\n" 14374 "/* block comment */\n" 14375 "int oneTwoThree=123;", 14376 Alignment)); 14377 14378 EXPECT_EQ("int a = 5;\n" 14379 "\n" 14380 "// line comment\n" 14381 "int oneTwoThree = 123;", 14382 format("int a = 5;\n" 14383 "\n" 14384 "// line comment\n" 14385 "int oneTwoThree=123;", 14386 Alignment)); 14387 } 14388 14389 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) { 14390 FormatStyle Alignment = getLLVMStyle(); 14391 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 14392 Alignment.AlignConsecutiveAssignments = 14393 FormatStyle::ACS_AcrossEmptyLinesAndComments; 14394 verifyFormat("int a = 5;\n" 14395 "int oneTwoThree = 123;", 14396 Alignment); 14397 verifyFormat("int a = method();\n" 14398 "int oneTwoThree = 133;", 14399 Alignment); 14400 verifyFormat("a &= 5;\n" 14401 "bcd *= 5;\n" 14402 "ghtyf += 5;\n" 14403 "dvfvdb -= 5;\n" 14404 "a /= 5;\n" 14405 "vdsvsv %= 5;\n" 14406 "sfdbddfbdfbb ^= 5;\n" 14407 "dvsdsv |= 5;\n" 14408 "int dsvvdvsdvvv = 123;", 14409 Alignment); 14410 verifyFormat("int i = 1, j = 10;\n" 14411 "something = 2000;", 14412 Alignment); 14413 verifyFormat("something = 2000;\n" 14414 "int i = 1, j = 10;\n", 14415 Alignment); 14416 verifyFormat("something = 2000;\n" 14417 "another = 911;\n" 14418 "int i = 1, j = 10;\n" 14419 "oneMore = 1;\n" 14420 "i = 2;", 14421 Alignment); 14422 verifyFormat("int a = 5;\n" 14423 "int one = 1;\n" 14424 "method();\n" 14425 "int oneTwoThree = 123;\n" 14426 "int oneTwo = 12;", 14427 Alignment); 14428 verifyFormat("int oneTwoThree = 123;\n" 14429 "int oneTwo = 12;\n" 14430 "method();\n", 14431 Alignment); 14432 verifyFormat("int oneTwoThree = 123; // comment\n" 14433 "int oneTwo = 12; // comment", 14434 Alignment); 14435 14436 // Bug 25167 14437 /* Uncomment when fixed 14438 verifyFormat("#if A\n" 14439 "#else\n" 14440 "int aaaaaaaa = 12;\n" 14441 "#endif\n" 14442 "#if B\n" 14443 "#else\n" 14444 "int a = 12;\n" 14445 "#endif\n", 14446 Alignment); 14447 verifyFormat("enum foo {\n" 14448 "#if A\n" 14449 "#else\n" 14450 " aaaaaaaa = 12;\n" 14451 "#endif\n" 14452 "#if B\n" 14453 "#else\n" 14454 " a = 12;\n" 14455 "#endif\n" 14456 "};\n", 14457 Alignment); 14458 */ 14459 14460 Alignment.MaxEmptyLinesToKeep = 10; 14461 /* Test alignment across empty lines */ 14462 EXPECT_EQ("int a = 5;\n" 14463 "\n" 14464 "int oneTwoThree = 123;", 14465 format("int a = 5;\n" 14466 "\n" 14467 "int oneTwoThree= 123;", 14468 Alignment)); 14469 EXPECT_EQ("int a = 5;\n" 14470 "int one = 1;\n" 14471 "\n" 14472 "int oneTwoThree = 123;", 14473 format("int a = 5;\n" 14474 "int one = 1;\n" 14475 "\n" 14476 "int oneTwoThree = 123;", 14477 Alignment)); 14478 EXPECT_EQ("int a = 5;\n" 14479 "int one = 1;\n" 14480 "\n" 14481 "int oneTwoThree = 123;\n" 14482 "int oneTwo = 12;", 14483 format("int a = 5;\n" 14484 "int one = 1;\n" 14485 "\n" 14486 "int oneTwoThree = 123;\n" 14487 "int oneTwo = 12;", 14488 Alignment)); 14489 14490 /* Test across comments */ 14491 EXPECT_EQ("int a = 5;\n" 14492 "/* block comment */\n" 14493 "int oneTwoThree = 123;", 14494 format("int a = 5;\n" 14495 "/* block comment */\n" 14496 "int oneTwoThree=123;", 14497 Alignment)); 14498 14499 EXPECT_EQ("int a = 5;\n" 14500 "// line comment\n" 14501 "int oneTwoThree = 123;", 14502 format("int a = 5;\n" 14503 "// line comment\n" 14504 "int oneTwoThree=123;", 14505 Alignment)); 14506 14507 /* Test across comments and newlines */ 14508 EXPECT_EQ("int a = 5;\n" 14509 "\n" 14510 "/* block comment */\n" 14511 "int oneTwoThree = 123;", 14512 format("int a = 5;\n" 14513 "\n" 14514 "/* block comment */\n" 14515 "int oneTwoThree=123;", 14516 Alignment)); 14517 14518 EXPECT_EQ("int a = 5;\n" 14519 "\n" 14520 "// line comment\n" 14521 "int oneTwoThree = 123;", 14522 format("int a = 5;\n" 14523 "\n" 14524 "// line comment\n" 14525 "int oneTwoThree=123;", 14526 Alignment)); 14527 14528 EXPECT_EQ("int a = 5;\n" 14529 "//\n" 14530 "// multi-line line comment\n" 14531 "//\n" 14532 "int oneTwoThree = 123;", 14533 format("int a = 5;\n" 14534 "//\n" 14535 "// multi-line line comment\n" 14536 "//\n" 14537 "int oneTwoThree=123;", 14538 Alignment)); 14539 14540 EXPECT_EQ("int a = 5;\n" 14541 "/*\n" 14542 " * multi-line block comment\n" 14543 " */\n" 14544 "int oneTwoThree = 123;", 14545 format("int a = 5;\n" 14546 "/*\n" 14547 " * multi-line block comment\n" 14548 " */\n" 14549 "int oneTwoThree=123;", 14550 Alignment)); 14551 14552 EXPECT_EQ("int a = 5;\n" 14553 "\n" 14554 "/* block comment */\n" 14555 "\n" 14556 "\n" 14557 "\n" 14558 "int oneTwoThree = 123;", 14559 format("int a = 5;\n" 14560 "\n" 14561 "/* block comment */\n" 14562 "\n" 14563 "\n" 14564 "\n" 14565 "int oneTwoThree=123;", 14566 Alignment)); 14567 14568 EXPECT_EQ("int a = 5;\n" 14569 "\n" 14570 "// line comment\n" 14571 "\n" 14572 "\n" 14573 "\n" 14574 "int oneTwoThree = 123;", 14575 format("int a = 5;\n" 14576 "\n" 14577 "// line comment\n" 14578 "\n" 14579 "\n" 14580 "\n" 14581 "int oneTwoThree=123;", 14582 Alignment)); 14583 14584 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 14585 verifyFormat("#define A \\\n" 14586 " int aaaa = 12; \\\n" 14587 " int b = 23; \\\n" 14588 " int ccc = 234; \\\n" 14589 " int dddddddddd = 2345;", 14590 Alignment); 14591 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 14592 verifyFormat("#define A \\\n" 14593 " int aaaa = 12; \\\n" 14594 " int b = 23; \\\n" 14595 " int ccc = 234; \\\n" 14596 " int dddddddddd = 2345;", 14597 Alignment); 14598 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 14599 verifyFormat("#define A " 14600 " \\\n" 14601 " int aaaa = 12; " 14602 " \\\n" 14603 " int b = 23; " 14604 " \\\n" 14605 " int ccc = 234; " 14606 " \\\n" 14607 " int dddddddddd = 2345;", 14608 Alignment); 14609 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 14610 "k = 4, int l = 5,\n" 14611 " int m = 6) {\n" 14612 " int j = 10;\n" 14613 " otherThing = 1;\n" 14614 "}", 14615 Alignment); 14616 verifyFormat("void SomeFunction(int parameter = 0) {\n" 14617 " int i = 1;\n" 14618 " int j = 2;\n" 14619 " int big = 10000;\n" 14620 "}", 14621 Alignment); 14622 verifyFormat("class C {\n" 14623 "public:\n" 14624 " int i = 1;\n" 14625 " virtual void f() = 0;\n" 14626 "};", 14627 Alignment); 14628 verifyFormat("int i = 1;\n" 14629 "if (SomeType t = getSomething()) {\n" 14630 "}\n" 14631 "int j = 2;\n" 14632 "int big = 10000;", 14633 Alignment); 14634 verifyFormat("int j = 7;\n" 14635 "for (int k = 0; k < N; ++k) {\n" 14636 "}\n" 14637 "int j = 2;\n" 14638 "int big = 10000;\n" 14639 "}", 14640 Alignment); 14641 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 14642 verifyFormat("int i = 1;\n" 14643 "LooooooooooongType loooooooooooooooooooooongVariable\n" 14644 " = someLooooooooooooooooongFunction();\n" 14645 "int j = 2;", 14646 Alignment); 14647 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 14648 verifyFormat("int i = 1;\n" 14649 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 14650 " someLooooooooooooooooongFunction();\n" 14651 "int j = 2;", 14652 Alignment); 14653 14654 verifyFormat("auto lambda = []() {\n" 14655 " auto i = 0;\n" 14656 " return 0;\n" 14657 "};\n" 14658 "int i = 0;\n" 14659 "auto v = type{\n" 14660 " i = 1, //\n" 14661 " (i = 2), //\n" 14662 " i = 3 //\n" 14663 "};", 14664 Alignment); 14665 14666 verifyFormat( 14667 "int i = 1;\n" 14668 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 14669 " loooooooooooooooooooooongParameterB);\n" 14670 "int j = 2;", 14671 Alignment); 14672 14673 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 14674 " typename B = very_long_type_name_1,\n" 14675 " typename T_2 = very_long_type_name_2>\n" 14676 "auto foo() {}\n", 14677 Alignment); 14678 verifyFormat("int a, b = 1;\n" 14679 "int c = 2;\n" 14680 "int dd = 3;\n", 14681 Alignment); 14682 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 14683 "float b[1][] = {{3.f}};\n", 14684 Alignment); 14685 verifyFormat("for (int i = 0; i < 1; i++)\n" 14686 " int x = 1;\n", 14687 Alignment); 14688 verifyFormat("for (i = 0; i < 1; i++)\n" 14689 " x = 1;\n" 14690 "y = 1;\n", 14691 Alignment); 14692 14693 Alignment.ReflowComments = true; 14694 Alignment.ColumnLimit = 50; 14695 EXPECT_EQ("int x = 0;\n" 14696 "int yy = 1; /// specificlennospace\n" 14697 "int zzz = 2;\n", 14698 format("int x = 0;\n" 14699 "int yy = 1; ///specificlennospace\n" 14700 "int zzz = 2;\n", 14701 Alignment)); 14702 } 14703 14704 TEST_F(FormatTest, AlignConsecutiveAssignments) { 14705 FormatStyle Alignment = getLLVMStyle(); 14706 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 14707 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 14708 verifyFormat("int a = 5;\n" 14709 "int oneTwoThree = 123;", 14710 Alignment); 14711 verifyFormat("int a = 5;\n" 14712 "int oneTwoThree = 123;", 14713 Alignment); 14714 14715 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 14716 verifyFormat("int a = 5;\n" 14717 "int oneTwoThree = 123;", 14718 Alignment); 14719 verifyFormat("int a = method();\n" 14720 "int oneTwoThree = 133;", 14721 Alignment); 14722 verifyFormat("a &= 5;\n" 14723 "bcd *= 5;\n" 14724 "ghtyf += 5;\n" 14725 "dvfvdb -= 5;\n" 14726 "a /= 5;\n" 14727 "vdsvsv %= 5;\n" 14728 "sfdbddfbdfbb ^= 5;\n" 14729 "dvsdsv |= 5;\n" 14730 "int dsvvdvsdvvv = 123;", 14731 Alignment); 14732 verifyFormat("int i = 1, j = 10;\n" 14733 "something = 2000;", 14734 Alignment); 14735 verifyFormat("something = 2000;\n" 14736 "int i = 1, j = 10;\n", 14737 Alignment); 14738 verifyFormat("something = 2000;\n" 14739 "another = 911;\n" 14740 "int i = 1, j = 10;\n" 14741 "oneMore = 1;\n" 14742 "i = 2;", 14743 Alignment); 14744 verifyFormat("int a = 5;\n" 14745 "int one = 1;\n" 14746 "method();\n" 14747 "int oneTwoThree = 123;\n" 14748 "int oneTwo = 12;", 14749 Alignment); 14750 verifyFormat("int oneTwoThree = 123;\n" 14751 "int oneTwo = 12;\n" 14752 "method();\n", 14753 Alignment); 14754 verifyFormat("int oneTwoThree = 123; // comment\n" 14755 "int oneTwo = 12; // comment", 14756 Alignment); 14757 14758 // Bug 25167 14759 /* Uncomment when fixed 14760 verifyFormat("#if A\n" 14761 "#else\n" 14762 "int aaaaaaaa = 12;\n" 14763 "#endif\n" 14764 "#if B\n" 14765 "#else\n" 14766 "int a = 12;\n" 14767 "#endif\n", 14768 Alignment); 14769 verifyFormat("enum foo {\n" 14770 "#if A\n" 14771 "#else\n" 14772 " aaaaaaaa = 12;\n" 14773 "#endif\n" 14774 "#if B\n" 14775 "#else\n" 14776 " a = 12;\n" 14777 "#endif\n" 14778 "};\n", 14779 Alignment); 14780 */ 14781 14782 EXPECT_EQ("int a = 5;\n" 14783 "\n" 14784 "int oneTwoThree = 123;", 14785 format("int a = 5;\n" 14786 "\n" 14787 "int oneTwoThree= 123;", 14788 Alignment)); 14789 EXPECT_EQ("int a = 5;\n" 14790 "int one = 1;\n" 14791 "\n" 14792 "int oneTwoThree = 123;", 14793 format("int a = 5;\n" 14794 "int one = 1;\n" 14795 "\n" 14796 "int oneTwoThree = 123;", 14797 Alignment)); 14798 EXPECT_EQ("int a = 5;\n" 14799 "int one = 1;\n" 14800 "\n" 14801 "int oneTwoThree = 123;\n" 14802 "int oneTwo = 12;", 14803 format("int a = 5;\n" 14804 "int one = 1;\n" 14805 "\n" 14806 "int oneTwoThree = 123;\n" 14807 "int oneTwo = 12;", 14808 Alignment)); 14809 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 14810 verifyFormat("#define A \\\n" 14811 " int aaaa = 12; \\\n" 14812 " int b = 23; \\\n" 14813 " int ccc = 234; \\\n" 14814 " int dddddddddd = 2345;", 14815 Alignment); 14816 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 14817 verifyFormat("#define A \\\n" 14818 " int aaaa = 12; \\\n" 14819 " int b = 23; \\\n" 14820 " int ccc = 234; \\\n" 14821 " int dddddddddd = 2345;", 14822 Alignment); 14823 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 14824 verifyFormat("#define A " 14825 " \\\n" 14826 " int aaaa = 12; " 14827 " \\\n" 14828 " int b = 23; " 14829 " \\\n" 14830 " int ccc = 234; " 14831 " \\\n" 14832 " int dddddddddd = 2345;", 14833 Alignment); 14834 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 14835 "k = 4, int l = 5,\n" 14836 " int m = 6) {\n" 14837 " int j = 10;\n" 14838 " otherThing = 1;\n" 14839 "}", 14840 Alignment); 14841 verifyFormat("void SomeFunction(int parameter = 0) {\n" 14842 " int i = 1;\n" 14843 " int j = 2;\n" 14844 " int big = 10000;\n" 14845 "}", 14846 Alignment); 14847 verifyFormat("class C {\n" 14848 "public:\n" 14849 " int i = 1;\n" 14850 " virtual void f() = 0;\n" 14851 "};", 14852 Alignment); 14853 verifyFormat("int i = 1;\n" 14854 "if (SomeType t = getSomething()) {\n" 14855 "}\n" 14856 "int j = 2;\n" 14857 "int big = 10000;", 14858 Alignment); 14859 verifyFormat("int j = 7;\n" 14860 "for (int k = 0; k < N; ++k) {\n" 14861 "}\n" 14862 "int j = 2;\n" 14863 "int big = 10000;\n" 14864 "}", 14865 Alignment); 14866 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 14867 verifyFormat("int i = 1;\n" 14868 "LooooooooooongType loooooooooooooooooooooongVariable\n" 14869 " = someLooooooooooooooooongFunction();\n" 14870 "int j = 2;", 14871 Alignment); 14872 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 14873 verifyFormat("int i = 1;\n" 14874 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 14875 " someLooooooooooooooooongFunction();\n" 14876 "int j = 2;", 14877 Alignment); 14878 14879 verifyFormat("auto lambda = []() {\n" 14880 " auto i = 0;\n" 14881 " return 0;\n" 14882 "};\n" 14883 "int i = 0;\n" 14884 "auto v = type{\n" 14885 " i = 1, //\n" 14886 " (i = 2), //\n" 14887 " i = 3 //\n" 14888 "};", 14889 Alignment); 14890 14891 verifyFormat( 14892 "int i = 1;\n" 14893 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 14894 " loooooooooooooooooooooongParameterB);\n" 14895 "int j = 2;", 14896 Alignment); 14897 14898 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 14899 " typename B = very_long_type_name_1,\n" 14900 " typename T_2 = very_long_type_name_2>\n" 14901 "auto foo() {}\n", 14902 Alignment); 14903 verifyFormat("int a, b = 1;\n" 14904 "int c = 2;\n" 14905 "int dd = 3;\n", 14906 Alignment); 14907 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 14908 "float b[1][] = {{3.f}};\n", 14909 Alignment); 14910 verifyFormat("for (int i = 0; i < 1; i++)\n" 14911 " int x = 1;\n", 14912 Alignment); 14913 verifyFormat("for (i = 0; i < 1; i++)\n" 14914 " x = 1;\n" 14915 "y = 1;\n", 14916 Alignment); 14917 14918 Alignment.ReflowComments = true; 14919 Alignment.ColumnLimit = 50; 14920 EXPECT_EQ("int x = 0;\n" 14921 "int yy = 1; /// specificlennospace\n" 14922 "int zzz = 2;\n", 14923 format("int x = 0;\n" 14924 "int yy = 1; ///specificlennospace\n" 14925 "int zzz = 2;\n", 14926 Alignment)); 14927 } 14928 14929 TEST_F(FormatTest, AlignConsecutiveBitFields) { 14930 FormatStyle Alignment = getLLVMStyle(); 14931 Alignment.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive; 14932 verifyFormat("int const a : 5;\n" 14933 "int oneTwoThree : 23;", 14934 Alignment); 14935 14936 // Initializers are allowed starting with c++2a 14937 verifyFormat("int const a : 5 = 1;\n" 14938 "int oneTwoThree : 23 = 0;", 14939 Alignment); 14940 14941 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 14942 verifyFormat("int const a : 5;\n" 14943 "int oneTwoThree : 23;", 14944 Alignment); 14945 14946 verifyFormat("int const a : 5; // comment\n" 14947 "int oneTwoThree : 23; // comment", 14948 Alignment); 14949 14950 verifyFormat("int const a : 5 = 1;\n" 14951 "int oneTwoThree : 23 = 0;", 14952 Alignment); 14953 14954 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 14955 verifyFormat("int const a : 5 = 1;\n" 14956 "int oneTwoThree : 23 = 0;", 14957 Alignment); 14958 verifyFormat("int const a : 5 = {1};\n" 14959 "int oneTwoThree : 23 = 0;", 14960 Alignment); 14961 14962 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None; 14963 verifyFormat("int const a :5;\n" 14964 "int oneTwoThree:23;", 14965 Alignment); 14966 14967 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before; 14968 verifyFormat("int const a :5;\n" 14969 "int oneTwoThree :23;", 14970 Alignment); 14971 14972 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After; 14973 verifyFormat("int const a : 5;\n" 14974 "int oneTwoThree: 23;", 14975 Alignment); 14976 14977 // Known limitations: ':' is only recognized as a bitfield colon when 14978 // followed by a number. 14979 /* 14980 verifyFormat("int oneTwoThree : SOME_CONSTANT;\n" 14981 "int a : 5;", 14982 Alignment); 14983 */ 14984 } 14985 14986 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 14987 FormatStyle Alignment = getLLVMStyle(); 14988 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 14989 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_None; 14990 Alignment.PointerAlignment = FormatStyle::PAS_Right; 14991 verifyFormat("float const a = 5;\n" 14992 "int oneTwoThree = 123;", 14993 Alignment); 14994 verifyFormat("int a = 5;\n" 14995 "float const oneTwoThree = 123;", 14996 Alignment); 14997 14998 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 14999 verifyFormat("float const a = 5;\n" 15000 "int oneTwoThree = 123;", 15001 Alignment); 15002 verifyFormat("int a = method();\n" 15003 "float const oneTwoThree = 133;", 15004 Alignment); 15005 verifyFormat("int i = 1, j = 10;\n" 15006 "something = 2000;", 15007 Alignment); 15008 verifyFormat("something = 2000;\n" 15009 "int i = 1, j = 10;\n", 15010 Alignment); 15011 verifyFormat("float something = 2000;\n" 15012 "double another = 911;\n" 15013 "int i = 1, j = 10;\n" 15014 "const int *oneMore = 1;\n" 15015 "unsigned i = 2;", 15016 Alignment); 15017 verifyFormat("float a = 5;\n" 15018 "int one = 1;\n" 15019 "method();\n" 15020 "const double oneTwoThree = 123;\n" 15021 "const unsigned int oneTwo = 12;", 15022 Alignment); 15023 verifyFormat("int oneTwoThree{0}; // comment\n" 15024 "unsigned oneTwo; // comment", 15025 Alignment); 15026 verifyFormat("unsigned int *a;\n" 15027 "int *b;\n" 15028 "unsigned int Const *c;\n" 15029 "unsigned int const *d;\n" 15030 "unsigned int Const &e;\n" 15031 "unsigned int const &f;", 15032 Alignment); 15033 verifyFormat("Const unsigned int *c;\n" 15034 "const unsigned int *d;\n" 15035 "Const unsigned int &e;\n" 15036 "const unsigned int &f;\n" 15037 "const unsigned g;\n" 15038 "Const unsigned h;", 15039 Alignment); 15040 EXPECT_EQ("float const a = 5;\n" 15041 "\n" 15042 "int oneTwoThree = 123;", 15043 format("float const a = 5;\n" 15044 "\n" 15045 "int oneTwoThree= 123;", 15046 Alignment)); 15047 EXPECT_EQ("float a = 5;\n" 15048 "int one = 1;\n" 15049 "\n" 15050 "unsigned oneTwoThree = 123;", 15051 format("float a = 5;\n" 15052 "int one = 1;\n" 15053 "\n" 15054 "unsigned oneTwoThree = 123;", 15055 Alignment)); 15056 EXPECT_EQ("float a = 5;\n" 15057 "int one = 1;\n" 15058 "\n" 15059 "unsigned oneTwoThree = 123;\n" 15060 "int oneTwo = 12;", 15061 format("float a = 5;\n" 15062 "int one = 1;\n" 15063 "\n" 15064 "unsigned oneTwoThree = 123;\n" 15065 "int oneTwo = 12;", 15066 Alignment)); 15067 // Function prototype alignment 15068 verifyFormat("int a();\n" 15069 "double b();", 15070 Alignment); 15071 verifyFormat("int a(int x);\n" 15072 "double b();", 15073 Alignment); 15074 unsigned OldColumnLimit = Alignment.ColumnLimit; 15075 // We need to set ColumnLimit to zero, in order to stress nested alignments, 15076 // otherwise the function parameters will be re-flowed onto a single line. 15077 Alignment.ColumnLimit = 0; 15078 EXPECT_EQ("int a(int x,\n" 15079 " float y);\n" 15080 "double b(int x,\n" 15081 " double y);", 15082 format("int a(int x,\n" 15083 " float y);\n" 15084 "double b(int x,\n" 15085 " double y);", 15086 Alignment)); 15087 // This ensures that function parameters of function declarations are 15088 // correctly indented when their owning functions are indented. 15089 // The failure case here is for 'double y' to not be indented enough. 15090 EXPECT_EQ("double a(int x);\n" 15091 "int b(int y,\n" 15092 " double z);", 15093 format("double a(int x);\n" 15094 "int b(int y,\n" 15095 " double z);", 15096 Alignment)); 15097 // Set ColumnLimit low so that we induce wrapping immediately after 15098 // the function name and opening paren. 15099 Alignment.ColumnLimit = 13; 15100 verifyFormat("int function(\n" 15101 " int x,\n" 15102 " bool y);", 15103 Alignment); 15104 Alignment.ColumnLimit = OldColumnLimit; 15105 // Ensure function pointers don't screw up recursive alignment 15106 verifyFormat("int a(int x, void (*fp)(int y));\n" 15107 "double b();", 15108 Alignment); 15109 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 15110 // Ensure recursive alignment is broken by function braces, so that the 15111 // "a = 1" does not align with subsequent assignments inside the function 15112 // body. 15113 verifyFormat("int func(int a = 1) {\n" 15114 " int b = 2;\n" 15115 " int cc = 3;\n" 15116 "}", 15117 Alignment); 15118 verifyFormat("float something = 2000;\n" 15119 "double another = 911;\n" 15120 "int i = 1, j = 10;\n" 15121 "const int *oneMore = 1;\n" 15122 "unsigned i = 2;", 15123 Alignment); 15124 verifyFormat("int oneTwoThree = {0}; // comment\n" 15125 "unsigned oneTwo = 0; // comment", 15126 Alignment); 15127 // Make sure that scope is correctly tracked, in the absence of braces 15128 verifyFormat("for (int i = 0; i < n; i++)\n" 15129 " j = i;\n" 15130 "double x = 1;\n", 15131 Alignment); 15132 verifyFormat("if (int i = 0)\n" 15133 " j = i;\n" 15134 "double x = 1;\n", 15135 Alignment); 15136 // Ensure operator[] and operator() are comprehended 15137 verifyFormat("struct test {\n" 15138 " long long int foo();\n" 15139 " int operator[](int a);\n" 15140 " double bar();\n" 15141 "};\n", 15142 Alignment); 15143 verifyFormat("struct test {\n" 15144 " long long int foo();\n" 15145 " int operator()(int a);\n" 15146 " double bar();\n" 15147 "};\n", 15148 Alignment); 15149 15150 // PAS_Right 15151 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 15152 " int const i = 1;\n" 15153 " int *j = 2;\n" 15154 " int big = 10000;\n" 15155 "\n" 15156 " unsigned oneTwoThree = 123;\n" 15157 " int oneTwo = 12;\n" 15158 " method();\n" 15159 " float k = 2;\n" 15160 " int ll = 10000;\n" 15161 "}", 15162 format("void SomeFunction(int parameter= 0) {\n" 15163 " int const i= 1;\n" 15164 " int *j=2;\n" 15165 " int big = 10000;\n" 15166 "\n" 15167 "unsigned oneTwoThree =123;\n" 15168 "int oneTwo = 12;\n" 15169 " method();\n" 15170 "float k= 2;\n" 15171 "int ll=10000;\n" 15172 "}", 15173 Alignment)); 15174 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 15175 " int const i = 1;\n" 15176 " int **j = 2, ***k;\n" 15177 " int &k = i;\n" 15178 " int &&l = i + j;\n" 15179 " int big = 10000;\n" 15180 "\n" 15181 " unsigned oneTwoThree = 123;\n" 15182 " int oneTwo = 12;\n" 15183 " method();\n" 15184 " float k = 2;\n" 15185 " int ll = 10000;\n" 15186 "}", 15187 format("void SomeFunction(int parameter= 0) {\n" 15188 " int const i= 1;\n" 15189 " int **j=2,***k;\n" 15190 "int &k=i;\n" 15191 "int &&l=i+j;\n" 15192 " int big = 10000;\n" 15193 "\n" 15194 "unsigned oneTwoThree =123;\n" 15195 "int oneTwo = 12;\n" 15196 " method();\n" 15197 "float k= 2;\n" 15198 "int ll=10000;\n" 15199 "}", 15200 Alignment)); 15201 // variables are aligned at their name, pointers are at the right most 15202 // position 15203 verifyFormat("int *a;\n" 15204 "int **b;\n" 15205 "int ***c;\n" 15206 "int foobar;\n", 15207 Alignment); 15208 15209 // PAS_Left 15210 FormatStyle AlignmentLeft = Alignment; 15211 AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left; 15212 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 15213 " int const i = 1;\n" 15214 " int* j = 2;\n" 15215 " int big = 10000;\n" 15216 "\n" 15217 " unsigned oneTwoThree = 123;\n" 15218 " int oneTwo = 12;\n" 15219 " method();\n" 15220 " float k = 2;\n" 15221 " int ll = 10000;\n" 15222 "}", 15223 format("void SomeFunction(int parameter= 0) {\n" 15224 " int const i= 1;\n" 15225 " int *j=2;\n" 15226 " int big = 10000;\n" 15227 "\n" 15228 "unsigned oneTwoThree =123;\n" 15229 "int oneTwo = 12;\n" 15230 " method();\n" 15231 "float k= 2;\n" 15232 "int ll=10000;\n" 15233 "}", 15234 AlignmentLeft)); 15235 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 15236 " int const i = 1;\n" 15237 " int** j = 2;\n" 15238 " int& k = i;\n" 15239 " int&& l = i + j;\n" 15240 " int big = 10000;\n" 15241 "\n" 15242 " unsigned oneTwoThree = 123;\n" 15243 " int oneTwo = 12;\n" 15244 " method();\n" 15245 " float k = 2;\n" 15246 " int ll = 10000;\n" 15247 "}", 15248 format("void SomeFunction(int parameter= 0) {\n" 15249 " int const i= 1;\n" 15250 " int **j=2;\n" 15251 "int &k=i;\n" 15252 "int &&l=i+j;\n" 15253 " int big = 10000;\n" 15254 "\n" 15255 "unsigned oneTwoThree =123;\n" 15256 "int oneTwo = 12;\n" 15257 " method();\n" 15258 "float k= 2;\n" 15259 "int ll=10000;\n" 15260 "}", 15261 AlignmentLeft)); 15262 // variables are aligned at their name, pointers are at the left most position 15263 verifyFormat("int* a;\n" 15264 "int** b;\n" 15265 "int*** c;\n" 15266 "int foobar;\n", 15267 AlignmentLeft); 15268 15269 // PAS_Middle 15270 FormatStyle AlignmentMiddle = Alignment; 15271 AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle; 15272 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 15273 " int const i = 1;\n" 15274 " int * j = 2;\n" 15275 " int big = 10000;\n" 15276 "\n" 15277 " unsigned oneTwoThree = 123;\n" 15278 " int oneTwo = 12;\n" 15279 " method();\n" 15280 " float k = 2;\n" 15281 " int ll = 10000;\n" 15282 "}", 15283 format("void SomeFunction(int parameter= 0) {\n" 15284 " int const i= 1;\n" 15285 " int *j=2;\n" 15286 " int big = 10000;\n" 15287 "\n" 15288 "unsigned oneTwoThree =123;\n" 15289 "int oneTwo = 12;\n" 15290 " method();\n" 15291 "float k= 2;\n" 15292 "int ll=10000;\n" 15293 "}", 15294 AlignmentMiddle)); 15295 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 15296 " int const i = 1;\n" 15297 " int ** j = 2, ***k;\n" 15298 " int & k = i;\n" 15299 " int && l = i + j;\n" 15300 " int big = 10000;\n" 15301 "\n" 15302 " unsigned oneTwoThree = 123;\n" 15303 " int oneTwo = 12;\n" 15304 " method();\n" 15305 " float k = 2;\n" 15306 " int ll = 10000;\n" 15307 "}", 15308 format("void SomeFunction(int parameter= 0) {\n" 15309 " int const i= 1;\n" 15310 " int **j=2,***k;\n" 15311 "int &k=i;\n" 15312 "int &&l=i+j;\n" 15313 " int big = 10000;\n" 15314 "\n" 15315 "unsigned oneTwoThree =123;\n" 15316 "int oneTwo = 12;\n" 15317 " method();\n" 15318 "float k= 2;\n" 15319 "int ll=10000;\n" 15320 "}", 15321 AlignmentMiddle)); 15322 // variables are aligned at their name, pointers are in the middle 15323 verifyFormat("int * a;\n" 15324 "int * b;\n" 15325 "int *** c;\n" 15326 "int foobar;\n", 15327 AlignmentMiddle); 15328 15329 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 15330 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 15331 verifyFormat("#define A \\\n" 15332 " int aaaa = 12; \\\n" 15333 " float b = 23; \\\n" 15334 " const int ccc = 234; \\\n" 15335 " unsigned dddddddddd = 2345;", 15336 Alignment); 15337 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 15338 verifyFormat("#define A \\\n" 15339 " int aaaa = 12; \\\n" 15340 " float b = 23; \\\n" 15341 " const int ccc = 234; \\\n" 15342 " unsigned dddddddddd = 2345;", 15343 Alignment); 15344 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right; 15345 Alignment.ColumnLimit = 30; 15346 verifyFormat("#define A \\\n" 15347 " int aaaa = 12; \\\n" 15348 " float b = 23; \\\n" 15349 " const int ccc = 234; \\\n" 15350 " int dddddddddd = 2345;", 15351 Alignment); 15352 Alignment.ColumnLimit = 80; 15353 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 15354 "k = 4, int l = 5,\n" 15355 " int m = 6) {\n" 15356 " const int j = 10;\n" 15357 " otherThing = 1;\n" 15358 "}", 15359 Alignment); 15360 verifyFormat("void SomeFunction(int parameter = 0) {\n" 15361 " int const i = 1;\n" 15362 " int *j = 2;\n" 15363 " int big = 10000;\n" 15364 "}", 15365 Alignment); 15366 verifyFormat("class C {\n" 15367 "public:\n" 15368 " int i = 1;\n" 15369 " virtual void f() = 0;\n" 15370 "};", 15371 Alignment); 15372 verifyFormat("float i = 1;\n" 15373 "if (SomeType t = getSomething()) {\n" 15374 "}\n" 15375 "const unsigned j = 2;\n" 15376 "int big = 10000;", 15377 Alignment); 15378 verifyFormat("float j = 7;\n" 15379 "for (int k = 0; k < N; ++k) {\n" 15380 "}\n" 15381 "unsigned j = 2;\n" 15382 "int big = 10000;\n" 15383 "}", 15384 Alignment); 15385 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 15386 verifyFormat("float i = 1;\n" 15387 "LooooooooooongType loooooooooooooooooooooongVariable\n" 15388 " = someLooooooooooooooooongFunction();\n" 15389 "int j = 2;", 15390 Alignment); 15391 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 15392 verifyFormat("int i = 1;\n" 15393 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 15394 " someLooooooooooooooooongFunction();\n" 15395 "int j = 2;", 15396 Alignment); 15397 15398 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 15399 verifyFormat("auto lambda = []() {\n" 15400 " auto ii = 0;\n" 15401 " float j = 0;\n" 15402 " return 0;\n" 15403 "};\n" 15404 "int i = 0;\n" 15405 "float i2 = 0;\n" 15406 "auto v = type{\n" 15407 " i = 1, //\n" 15408 " (i = 2), //\n" 15409 " i = 3 //\n" 15410 "};", 15411 Alignment); 15412 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 15413 15414 verifyFormat( 15415 "int i = 1;\n" 15416 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 15417 " loooooooooooooooooooooongParameterB);\n" 15418 "int j = 2;", 15419 Alignment); 15420 15421 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 15422 // We expect declarations and assignments to align, as long as it doesn't 15423 // exceed the column limit, starting a new alignment sequence whenever it 15424 // happens. 15425 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 15426 Alignment.ColumnLimit = 30; 15427 verifyFormat("float ii = 1;\n" 15428 "unsigned j = 2;\n" 15429 "int someVerylongVariable = 1;\n" 15430 "AnotherLongType ll = 123456;\n" 15431 "VeryVeryLongType k = 2;\n" 15432 "int myvar = 1;", 15433 Alignment); 15434 Alignment.ColumnLimit = 80; 15435 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 15436 15437 verifyFormat( 15438 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 15439 " typename LongType, typename B>\n" 15440 "auto foo() {}\n", 15441 Alignment); 15442 verifyFormat("float a, b = 1;\n" 15443 "int c = 2;\n" 15444 "int dd = 3;\n", 15445 Alignment); 15446 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 15447 "float b[1][] = {{3.f}};\n", 15448 Alignment); 15449 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 15450 verifyFormat("float a, b = 1;\n" 15451 "int c = 2;\n" 15452 "int dd = 3;\n", 15453 Alignment); 15454 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 15455 "float b[1][] = {{3.f}};\n", 15456 Alignment); 15457 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 15458 15459 Alignment.ColumnLimit = 30; 15460 Alignment.BinPackParameters = false; 15461 verifyFormat("void foo(float a,\n" 15462 " float b,\n" 15463 " int c,\n" 15464 " uint32_t *d) {\n" 15465 " int *e = 0;\n" 15466 " float f = 0;\n" 15467 " double g = 0;\n" 15468 "}\n" 15469 "void bar(ino_t a,\n" 15470 " int b,\n" 15471 " uint32_t *c,\n" 15472 " bool d) {}\n", 15473 Alignment); 15474 Alignment.BinPackParameters = true; 15475 Alignment.ColumnLimit = 80; 15476 15477 // Bug 33507 15478 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 15479 verifyFormat( 15480 "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n" 15481 " static const Version verVs2017;\n" 15482 " return true;\n" 15483 "});\n", 15484 Alignment); 15485 Alignment.PointerAlignment = FormatStyle::PAS_Right; 15486 15487 // See llvm.org/PR35641 15488 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 15489 verifyFormat("int func() { //\n" 15490 " int b;\n" 15491 " unsigned c;\n" 15492 "}", 15493 Alignment); 15494 15495 // See PR37175 15496 FormatStyle Style = getMozillaStyle(); 15497 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 15498 EXPECT_EQ("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n" 15499 "foo(int a);", 15500 format("DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style)); 15501 15502 Alignment.PointerAlignment = FormatStyle::PAS_Left; 15503 verifyFormat("unsigned int* a;\n" 15504 "int* b;\n" 15505 "unsigned int Const* c;\n" 15506 "unsigned int const* d;\n" 15507 "unsigned int Const& e;\n" 15508 "unsigned int const& f;", 15509 Alignment); 15510 verifyFormat("Const unsigned int* c;\n" 15511 "const unsigned int* d;\n" 15512 "Const unsigned int& e;\n" 15513 "const unsigned int& f;\n" 15514 "const unsigned g;\n" 15515 "Const unsigned h;", 15516 Alignment); 15517 15518 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 15519 verifyFormat("unsigned int * a;\n" 15520 "int * b;\n" 15521 "unsigned int Const * c;\n" 15522 "unsigned int const * d;\n" 15523 "unsigned int Const & e;\n" 15524 "unsigned int const & f;", 15525 Alignment); 15526 verifyFormat("Const unsigned int * c;\n" 15527 "const unsigned int * d;\n" 15528 "Const unsigned int & e;\n" 15529 "const unsigned int & f;\n" 15530 "const unsigned g;\n" 15531 "Const unsigned h;", 15532 Alignment); 15533 } 15534 15535 TEST_F(FormatTest, AlignWithLineBreaks) { 15536 auto Style = getLLVMStyleWithColumns(120); 15537 15538 EXPECT_EQ(Style.AlignConsecutiveAssignments, FormatStyle::ACS_None); 15539 EXPECT_EQ(Style.AlignConsecutiveDeclarations, FormatStyle::ACS_None); 15540 verifyFormat("void foo() {\n" 15541 " int myVar = 5;\n" 15542 " double x = 3.14;\n" 15543 " auto str = \"Hello \"\n" 15544 " \"World\";\n" 15545 " auto s = \"Hello \"\n" 15546 " \"Again\";\n" 15547 "}", 15548 Style); 15549 15550 // clang-format off 15551 verifyFormat("void foo() {\n" 15552 " const int capacityBefore = Entries.capacity();\n" 15553 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 15554 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 15555 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 15556 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 15557 "}", 15558 Style); 15559 // clang-format on 15560 15561 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 15562 verifyFormat("void foo() {\n" 15563 " int myVar = 5;\n" 15564 " double x = 3.14;\n" 15565 " auto str = \"Hello \"\n" 15566 " \"World\";\n" 15567 " auto s = \"Hello \"\n" 15568 " \"Again\";\n" 15569 "}", 15570 Style); 15571 15572 // clang-format off 15573 verifyFormat("void foo() {\n" 15574 " const int capacityBefore = Entries.capacity();\n" 15575 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 15576 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 15577 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 15578 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 15579 "}", 15580 Style); 15581 // clang-format on 15582 15583 Style.AlignConsecutiveAssignments = FormatStyle::ACS_None; 15584 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 15585 verifyFormat("void foo() {\n" 15586 " int myVar = 5;\n" 15587 " double x = 3.14;\n" 15588 " auto str = \"Hello \"\n" 15589 " \"World\";\n" 15590 " auto s = \"Hello \"\n" 15591 " \"Again\";\n" 15592 "}", 15593 Style); 15594 15595 // clang-format off 15596 verifyFormat("void foo() {\n" 15597 " const int capacityBefore = Entries.capacity();\n" 15598 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 15599 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 15600 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 15601 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 15602 "}", 15603 Style); 15604 // clang-format on 15605 15606 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 15607 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 15608 15609 verifyFormat("void foo() {\n" 15610 " int myVar = 5;\n" 15611 " double x = 3.14;\n" 15612 " auto str = \"Hello \"\n" 15613 " \"World\";\n" 15614 " auto s = \"Hello \"\n" 15615 " \"Again\";\n" 15616 "}", 15617 Style); 15618 15619 // clang-format off 15620 verifyFormat("void foo() {\n" 15621 " const int capacityBefore = Entries.capacity();\n" 15622 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 15623 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 15624 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 15625 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 15626 "}", 15627 Style); 15628 // clang-format on 15629 } 15630 15631 TEST_F(FormatTest, LinuxBraceBreaking) { 15632 FormatStyle LinuxBraceStyle = getLLVMStyle(); 15633 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 15634 verifyFormat("namespace a\n" 15635 "{\n" 15636 "class A\n" 15637 "{\n" 15638 " void f()\n" 15639 " {\n" 15640 " if (true) {\n" 15641 " a();\n" 15642 " b();\n" 15643 " } else {\n" 15644 " a();\n" 15645 " }\n" 15646 " }\n" 15647 " void g() { return; }\n" 15648 "};\n" 15649 "struct B {\n" 15650 " int x;\n" 15651 "};\n" 15652 "} // namespace a\n", 15653 LinuxBraceStyle); 15654 verifyFormat("enum X {\n" 15655 " Y = 0,\n" 15656 "}\n", 15657 LinuxBraceStyle); 15658 verifyFormat("struct S {\n" 15659 " int Type;\n" 15660 " union {\n" 15661 " int x;\n" 15662 " double y;\n" 15663 " } Value;\n" 15664 " class C\n" 15665 " {\n" 15666 " MyFavoriteType Value;\n" 15667 " } Class;\n" 15668 "}\n", 15669 LinuxBraceStyle); 15670 } 15671 15672 TEST_F(FormatTest, MozillaBraceBreaking) { 15673 FormatStyle MozillaBraceStyle = getLLVMStyle(); 15674 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 15675 MozillaBraceStyle.FixNamespaceComments = false; 15676 verifyFormat("namespace a {\n" 15677 "class A\n" 15678 "{\n" 15679 " void f()\n" 15680 " {\n" 15681 " if (true) {\n" 15682 " a();\n" 15683 " b();\n" 15684 " }\n" 15685 " }\n" 15686 " void g() { return; }\n" 15687 "};\n" 15688 "enum E\n" 15689 "{\n" 15690 " A,\n" 15691 " // foo\n" 15692 " B,\n" 15693 " C\n" 15694 "};\n" 15695 "struct B\n" 15696 "{\n" 15697 " int x;\n" 15698 "};\n" 15699 "}\n", 15700 MozillaBraceStyle); 15701 verifyFormat("struct S\n" 15702 "{\n" 15703 " int Type;\n" 15704 " union\n" 15705 " {\n" 15706 " int x;\n" 15707 " double y;\n" 15708 " } Value;\n" 15709 " class C\n" 15710 " {\n" 15711 " MyFavoriteType Value;\n" 15712 " } Class;\n" 15713 "}\n", 15714 MozillaBraceStyle); 15715 } 15716 15717 TEST_F(FormatTest, StroustrupBraceBreaking) { 15718 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 15719 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 15720 verifyFormat("namespace a {\n" 15721 "class A {\n" 15722 " void f()\n" 15723 " {\n" 15724 " if (true) {\n" 15725 " a();\n" 15726 " b();\n" 15727 " }\n" 15728 " }\n" 15729 " void g() { return; }\n" 15730 "};\n" 15731 "struct B {\n" 15732 " int x;\n" 15733 "};\n" 15734 "} // namespace a\n", 15735 StroustrupBraceStyle); 15736 15737 verifyFormat("void foo()\n" 15738 "{\n" 15739 " if (a) {\n" 15740 " a();\n" 15741 " }\n" 15742 " else {\n" 15743 " b();\n" 15744 " }\n" 15745 "}\n", 15746 StroustrupBraceStyle); 15747 15748 verifyFormat("#ifdef _DEBUG\n" 15749 "int foo(int i = 0)\n" 15750 "#else\n" 15751 "int foo(int i = 5)\n" 15752 "#endif\n" 15753 "{\n" 15754 " return i;\n" 15755 "}", 15756 StroustrupBraceStyle); 15757 15758 verifyFormat("void foo() {}\n" 15759 "void bar()\n" 15760 "#ifdef _DEBUG\n" 15761 "{\n" 15762 " foo();\n" 15763 "}\n" 15764 "#else\n" 15765 "{\n" 15766 "}\n" 15767 "#endif", 15768 StroustrupBraceStyle); 15769 15770 verifyFormat("void foobar() { int i = 5; }\n" 15771 "#ifdef _DEBUG\n" 15772 "void bar() {}\n" 15773 "#else\n" 15774 "void bar() { foobar(); }\n" 15775 "#endif", 15776 StroustrupBraceStyle); 15777 } 15778 15779 TEST_F(FormatTest, AllmanBraceBreaking) { 15780 FormatStyle AllmanBraceStyle = getLLVMStyle(); 15781 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 15782 15783 EXPECT_EQ("namespace a\n" 15784 "{\n" 15785 "void f();\n" 15786 "void g();\n" 15787 "} // namespace a\n", 15788 format("namespace a\n" 15789 "{\n" 15790 "void f();\n" 15791 "void g();\n" 15792 "}\n", 15793 AllmanBraceStyle)); 15794 15795 verifyFormat("namespace a\n" 15796 "{\n" 15797 "class A\n" 15798 "{\n" 15799 " void f()\n" 15800 " {\n" 15801 " if (true)\n" 15802 " {\n" 15803 " a();\n" 15804 " b();\n" 15805 " }\n" 15806 " }\n" 15807 " void g() { return; }\n" 15808 "};\n" 15809 "struct B\n" 15810 "{\n" 15811 " int x;\n" 15812 "};\n" 15813 "union C\n" 15814 "{\n" 15815 "};\n" 15816 "} // namespace a", 15817 AllmanBraceStyle); 15818 15819 verifyFormat("void f()\n" 15820 "{\n" 15821 " if (true)\n" 15822 " {\n" 15823 " a();\n" 15824 " }\n" 15825 " else if (false)\n" 15826 " {\n" 15827 " b();\n" 15828 " }\n" 15829 " else\n" 15830 " {\n" 15831 " c();\n" 15832 " }\n" 15833 "}\n", 15834 AllmanBraceStyle); 15835 15836 verifyFormat("void f()\n" 15837 "{\n" 15838 " for (int i = 0; i < 10; ++i)\n" 15839 " {\n" 15840 " a();\n" 15841 " }\n" 15842 " while (false)\n" 15843 " {\n" 15844 " b();\n" 15845 " }\n" 15846 " do\n" 15847 " {\n" 15848 " c();\n" 15849 " } while (false)\n" 15850 "}\n", 15851 AllmanBraceStyle); 15852 15853 verifyFormat("void f(int a)\n" 15854 "{\n" 15855 " switch (a)\n" 15856 " {\n" 15857 " case 0:\n" 15858 " break;\n" 15859 " case 1:\n" 15860 " {\n" 15861 " break;\n" 15862 " }\n" 15863 " case 2:\n" 15864 " {\n" 15865 " }\n" 15866 " break;\n" 15867 " default:\n" 15868 " break;\n" 15869 " }\n" 15870 "}\n", 15871 AllmanBraceStyle); 15872 15873 verifyFormat("enum X\n" 15874 "{\n" 15875 " Y = 0,\n" 15876 "}\n", 15877 AllmanBraceStyle); 15878 verifyFormat("enum X\n" 15879 "{\n" 15880 " Y = 0\n" 15881 "}\n", 15882 AllmanBraceStyle); 15883 15884 verifyFormat("@interface BSApplicationController ()\n" 15885 "{\n" 15886 "@private\n" 15887 " id _extraIvar;\n" 15888 "}\n" 15889 "@end\n", 15890 AllmanBraceStyle); 15891 15892 verifyFormat("#ifdef _DEBUG\n" 15893 "int foo(int i = 0)\n" 15894 "#else\n" 15895 "int foo(int i = 5)\n" 15896 "#endif\n" 15897 "{\n" 15898 " return i;\n" 15899 "}", 15900 AllmanBraceStyle); 15901 15902 verifyFormat("void foo() {}\n" 15903 "void bar()\n" 15904 "#ifdef _DEBUG\n" 15905 "{\n" 15906 " foo();\n" 15907 "}\n" 15908 "#else\n" 15909 "{\n" 15910 "}\n" 15911 "#endif", 15912 AllmanBraceStyle); 15913 15914 verifyFormat("void foobar() { int i = 5; }\n" 15915 "#ifdef _DEBUG\n" 15916 "void bar() {}\n" 15917 "#else\n" 15918 "void bar() { foobar(); }\n" 15919 "#endif", 15920 AllmanBraceStyle); 15921 15922 EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine, 15923 FormatStyle::SLS_All); 15924 15925 verifyFormat("[](int i) { return i + 2; };\n" 15926 "[](int i, int j)\n" 15927 "{\n" 15928 " auto x = i + j;\n" 15929 " auto y = i * j;\n" 15930 " return x ^ y;\n" 15931 "};\n" 15932 "void foo()\n" 15933 "{\n" 15934 " auto shortLambda = [](int i) { return i + 2; };\n" 15935 " auto longLambda = [](int i, int j)\n" 15936 " {\n" 15937 " auto x = i + j;\n" 15938 " auto y = i * j;\n" 15939 " return x ^ y;\n" 15940 " };\n" 15941 "}", 15942 AllmanBraceStyle); 15943 15944 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 15945 15946 verifyFormat("[](int i)\n" 15947 "{\n" 15948 " return i + 2;\n" 15949 "};\n" 15950 "[](int i, int j)\n" 15951 "{\n" 15952 " auto x = i + j;\n" 15953 " auto y = i * j;\n" 15954 " return x ^ y;\n" 15955 "};\n" 15956 "void foo()\n" 15957 "{\n" 15958 " auto shortLambda = [](int i)\n" 15959 " {\n" 15960 " return i + 2;\n" 15961 " };\n" 15962 " auto longLambda = [](int i, int j)\n" 15963 " {\n" 15964 " auto x = i + j;\n" 15965 " auto y = i * j;\n" 15966 " return x ^ y;\n" 15967 " };\n" 15968 "}", 15969 AllmanBraceStyle); 15970 15971 // Reset 15972 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All; 15973 15974 // This shouldn't affect ObjC blocks.. 15975 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 15976 " // ...\n" 15977 " int i;\n" 15978 "}];", 15979 AllmanBraceStyle); 15980 verifyFormat("void (^block)(void) = ^{\n" 15981 " // ...\n" 15982 " int i;\n" 15983 "};", 15984 AllmanBraceStyle); 15985 // .. or dict literals. 15986 verifyFormat("void f()\n" 15987 "{\n" 15988 " // ...\n" 15989 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 15990 "}", 15991 AllmanBraceStyle); 15992 verifyFormat("void f()\n" 15993 "{\n" 15994 " // ...\n" 15995 " [object someMethod:@{a : @\"b\"}];\n" 15996 "}", 15997 AllmanBraceStyle); 15998 verifyFormat("int f()\n" 15999 "{ // comment\n" 16000 " return 42;\n" 16001 "}", 16002 AllmanBraceStyle); 16003 16004 AllmanBraceStyle.ColumnLimit = 19; 16005 verifyFormat("void f() { int i; }", AllmanBraceStyle); 16006 AllmanBraceStyle.ColumnLimit = 18; 16007 verifyFormat("void f()\n" 16008 "{\n" 16009 " int i;\n" 16010 "}", 16011 AllmanBraceStyle); 16012 AllmanBraceStyle.ColumnLimit = 80; 16013 16014 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 16015 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = 16016 FormatStyle::SIS_WithoutElse; 16017 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 16018 verifyFormat("void f(bool b)\n" 16019 "{\n" 16020 " if (b)\n" 16021 " {\n" 16022 " return;\n" 16023 " }\n" 16024 "}\n", 16025 BreakBeforeBraceShortIfs); 16026 verifyFormat("void f(bool b)\n" 16027 "{\n" 16028 " if constexpr (b)\n" 16029 " {\n" 16030 " return;\n" 16031 " }\n" 16032 "}\n", 16033 BreakBeforeBraceShortIfs); 16034 verifyFormat("void f(bool b)\n" 16035 "{\n" 16036 " if CONSTEXPR (b)\n" 16037 " {\n" 16038 " return;\n" 16039 " }\n" 16040 "}\n", 16041 BreakBeforeBraceShortIfs); 16042 verifyFormat("void f(bool b)\n" 16043 "{\n" 16044 " if (b) return;\n" 16045 "}\n", 16046 BreakBeforeBraceShortIfs); 16047 verifyFormat("void f(bool b)\n" 16048 "{\n" 16049 " if constexpr (b) return;\n" 16050 "}\n", 16051 BreakBeforeBraceShortIfs); 16052 verifyFormat("void f(bool b)\n" 16053 "{\n" 16054 " if CONSTEXPR (b) return;\n" 16055 "}\n", 16056 BreakBeforeBraceShortIfs); 16057 verifyFormat("void f(bool b)\n" 16058 "{\n" 16059 " while (b)\n" 16060 " {\n" 16061 " return;\n" 16062 " }\n" 16063 "}\n", 16064 BreakBeforeBraceShortIfs); 16065 } 16066 16067 TEST_F(FormatTest, WhitesmithsBraceBreaking) { 16068 FormatStyle WhitesmithsBraceStyle = getLLVMStyle(); 16069 WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 16070 16071 // Make a few changes to the style for testing purposes 16072 WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine = 16073 FormatStyle::SFS_Empty; 16074 WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 16075 WhitesmithsBraceStyle.ColumnLimit = 0; 16076 16077 // FIXME: this test case can't decide whether there should be a blank line 16078 // after the ~D() line or not. It adds one if one doesn't exist in the test 16079 // and it removes the line if one exists. 16080 /* 16081 verifyFormat("class A;\n" 16082 "namespace B\n" 16083 " {\n" 16084 "class C;\n" 16085 "// Comment\n" 16086 "class D\n" 16087 " {\n" 16088 "public:\n" 16089 " D();\n" 16090 " ~D() {}\n" 16091 "private:\n" 16092 " enum E\n" 16093 " {\n" 16094 " F\n" 16095 " }\n" 16096 " };\n" 16097 " } // namespace B\n", 16098 WhitesmithsBraceStyle); 16099 */ 16100 16101 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None; 16102 verifyFormat("namespace a\n" 16103 " {\n" 16104 "class A\n" 16105 " {\n" 16106 " void f()\n" 16107 " {\n" 16108 " if (true)\n" 16109 " {\n" 16110 " a();\n" 16111 " b();\n" 16112 " }\n" 16113 " }\n" 16114 " void g()\n" 16115 " {\n" 16116 " return;\n" 16117 " }\n" 16118 " };\n" 16119 "struct B\n" 16120 " {\n" 16121 " int x;\n" 16122 " };\n" 16123 " } // namespace a", 16124 WhitesmithsBraceStyle); 16125 16126 verifyFormat("namespace a\n" 16127 " {\n" 16128 "namespace b\n" 16129 " {\n" 16130 "class A\n" 16131 " {\n" 16132 " void f()\n" 16133 " {\n" 16134 " if (true)\n" 16135 " {\n" 16136 " a();\n" 16137 " b();\n" 16138 " }\n" 16139 " }\n" 16140 " void g()\n" 16141 " {\n" 16142 " return;\n" 16143 " }\n" 16144 " };\n" 16145 "struct B\n" 16146 " {\n" 16147 " int x;\n" 16148 " };\n" 16149 " } // namespace b\n" 16150 " } // namespace a", 16151 WhitesmithsBraceStyle); 16152 16153 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner; 16154 verifyFormat("namespace a\n" 16155 " {\n" 16156 "namespace b\n" 16157 " {\n" 16158 " class A\n" 16159 " {\n" 16160 " void f()\n" 16161 " {\n" 16162 " if (true)\n" 16163 " {\n" 16164 " a();\n" 16165 " b();\n" 16166 " }\n" 16167 " }\n" 16168 " void g()\n" 16169 " {\n" 16170 " return;\n" 16171 " }\n" 16172 " };\n" 16173 " struct B\n" 16174 " {\n" 16175 " int x;\n" 16176 " };\n" 16177 " } // namespace b\n" 16178 " } // namespace a", 16179 WhitesmithsBraceStyle); 16180 16181 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All; 16182 verifyFormat("namespace a\n" 16183 " {\n" 16184 " namespace b\n" 16185 " {\n" 16186 " class A\n" 16187 " {\n" 16188 " void f()\n" 16189 " {\n" 16190 " if (true)\n" 16191 " {\n" 16192 " a();\n" 16193 " b();\n" 16194 " }\n" 16195 " }\n" 16196 " void g()\n" 16197 " {\n" 16198 " return;\n" 16199 " }\n" 16200 " };\n" 16201 " struct B\n" 16202 " {\n" 16203 " int x;\n" 16204 " };\n" 16205 " } // namespace b\n" 16206 " } // namespace a", 16207 WhitesmithsBraceStyle); 16208 16209 verifyFormat("void f()\n" 16210 " {\n" 16211 " if (true)\n" 16212 " {\n" 16213 " a();\n" 16214 " }\n" 16215 " else if (false)\n" 16216 " {\n" 16217 " b();\n" 16218 " }\n" 16219 " else\n" 16220 " {\n" 16221 " c();\n" 16222 " }\n" 16223 " }\n", 16224 WhitesmithsBraceStyle); 16225 16226 verifyFormat("void f()\n" 16227 " {\n" 16228 " for (int i = 0; i < 10; ++i)\n" 16229 " {\n" 16230 " a();\n" 16231 " }\n" 16232 " while (false)\n" 16233 " {\n" 16234 " b();\n" 16235 " }\n" 16236 " do\n" 16237 " {\n" 16238 " c();\n" 16239 " } while (false)\n" 16240 " }\n", 16241 WhitesmithsBraceStyle); 16242 16243 WhitesmithsBraceStyle.IndentCaseLabels = true; 16244 verifyFormat("void switchTest1(int a)\n" 16245 " {\n" 16246 " switch (a)\n" 16247 " {\n" 16248 " case 2:\n" 16249 " {\n" 16250 " }\n" 16251 " break;\n" 16252 " }\n" 16253 " }\n", 16254 WhitesmithsBraceStyle); 16255 16256 verifyFormat("void switchTest2(int a)\n" 16257 " {\n" 16258 " switch (a)\n" 16259 " {\n" 16260 " case 0:\n" 16261 " break;\n" 16262 " case 1:\n" 16263 " {\n" 16264 " break;\n" 16265 " }\n" 16266 " case 2:\n" 16267 " {\n" 16268 " }\n" 16269 " break;\n" 16270 " default:\n" 16271 " break;\n" 16272 " }\n" 16273 " }\n", 16274 WhitesmithsBraceStyle); 16275 16276 verifyFormat("void switchTest3(int a)\n" 16277 " {\n" 16278 " switch (a)\n" 16279 " {\n" 16280 " case 0:\n" 16281 " {\n" 16282 " foo(x);\n" 16283 " }\n" 16284 " break;\n" 16285 " default:\n" 16286 " {\n" 16287 " foo(1);\n" 16288 " }\n" 16289 " break;\n" 16290 " }\n" 16291 " }\n", 16292 WhitesmithsBraceStyle); 16293 16294 WhitesmithsBraceStyle.IndentCaseLabels = false; 16295 16296 verifyFormat("void switchTest4(int a)\n" 16297 " {\n" 16298 " switch (a)\n" 16299 " {\n" 16300 " case 2:\n" 16301 " {\n" 16302 " }\n" 16303 " break;\n" 16304 " }\n" 16305 " }\n", 16306 WhitesmithsBraceStyle); 16307 16308 verifyFormat("void switchTest5(int a)\n" 16309 " {\n" 16310 " switch (a)\n" 16311 " {\n" 16312 " case 0:\n" 16313 " break;\n" 16314 " case 1:\n" 16315 " {\n" 16316 " foo();\n" 16317 " break;\n" 16318 " }\n" 16319 " case 2:\n" 16320 " {\n" 16321 " }\n" 16322 " break;\n" 16323 " default:\n" 16324 " break;\n" 16325 " }\n" 16326 " }\n", 16327 WhitesmithsBraceStyle); 16328 16329 verifyFormat("void switchTest6(int a)\n" 16330 " {\n" 16331 " switch (a)\n" 16332 " {\n" 16333 " case 0:\n" 16334 " {\n" 16335 " foo(x);\n" 16336 " }\n" 16337 " break;\n" 16338 " default:\n" 16339 " {\n" 16340 " foo(1);\n" 16341 " }\n" 16342 " break;\n" 16343 " }\n" 16344 " }\n", 16345 WhitesmithsBraceStyle); 16346 16347 verifyFormat("enum X\n" 16348 " {\n" 16349 " Y = 0, // testing\n" 16350 " }\n", 16351 WhitesmithsBraceStyle); 16352 16353 verifyFormat("enum X\n" 16354 " {\n" 16355 " Y = 0\n" 16356 " }\n", 16357 WhitesmithsBraceStyle); 16358 verifyFormat("enum X\n" 16359 " {\n" 16360 " Y = 0,\n" 16361 " Z = 1\n" 16362 " };\n", 16363 WhitesmithsBraceStyle); 16364 16365 verifyFormat("@interface BSApplicationController ()\n" 16366 " {\n" 16367 "@private\n" 16368 " id _extraIvar;\n" 16369 " }\n" 16370 "@end\n", 16371 WhitesmithsBraceStyle); 16372 16373 verifyFormat("#ifdef _DEBUG\n" 16374 "int foo(int i = 0)\n" 16375 "#else\n" 16376 "int foo(int i = 5)\n" 16377 "#endif\n" 16378 " {\n" 16379 " return i;\n" 16380 " }", 16381 WhitesmithsBraceStyle); 16382 16383 verifyFormat("void foo() {}\n" 16384 "void bar()\n" 16385 "#ifdef _DEBUG\n" 16386 " {\n" 16387 " foo();\n" 16388 " }\n" 16389 "#else\n" 16390 " {\n" 16391 " }\n" 16392 "#endif", 16393 WhitesmithsBraceStyle); 16394 16395 verifyFormat("void foobar()\n" 16396 " {\n" 16397 " int i = 5;\n" 16398 " }\n" 16399 "#ifdef _DEBUG\n" 16400 "void bar()\n" 16401 " {\n" 16402 " }\n" 16403 "#else\n" 16404 "void bar()\n" 16405 " {\n" 16406 " foobar();\n" 16407 " }\n" 16408 "#endif", 16409 WhitesmithsBraceStyle); 16410 16411 // This shouldn't affect ObjC blocks.. 16412 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 16413 " // ...\n" 16414 " int i;\n" 16415 "}];", 16416 WhitesmithsBraceStyle); 16417 verifyFormat("void (^block)(void) = ^{\n" 16418 " // ...\n" 16419 " int i;\n" 16420 "};", 16421 WhitesmithsBraceStyle); 16422 // .. or dict literals. 16423 verifyFormat("void f()\n" 16424 " {\n" 16425 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 16426 " }", 16427 WhitesmithsBraceStyle); 16428 16429 verifyFormat("int f()\n" 16430 " { // comment\n" 16431 " return 42;\n" 16432 " }", 16433 WhitesmithsBraceStyle); 16434 16435 FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle; 16436 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = 16437 FormatStyle::SIS_OnlyFirstIf; 16438 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 16439 verifyFormat("void f(bool b)\n" 16440 " {\n" 16441 " if (b)\n" 16442 " {\n" 16443 " return;\n" 16444 " }\n" 16445 " }\n", 16446 BreakBeforeBraceShortIfs); 16447 verifyFormat("void f(bool b)\n" 16448 " {\n" 16449 " if (b) return;\n" 16450 " }\n", 16451 BreakBeforeBraceShortIfs); 16452 verifyFormat("void f(bool b)\n" 16453 " {\n" 16454 " while (b)\n" 16455 " {\n" 16456 " return;\n" 16457 " }\n" 16458 " }\n", 16459 BreakBeforeBraceShortIfs); 16460 } 16461 16462 TEST_F(FormatTest, GNUBraceBreaking) { 16463 FormatStyle GNUBraceStyle = getLLVMStyle(); 16464 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 16465 verifyFormat("namespace a\n" 16466 "{\n" 16467 "class A\n" 16468 "{\n" 16469 " void f()\n" 16470 " {\n" 16471 " int a;\n" 16472 " {\n" 16473 " int b;\n" 16474 " }\n" 16475 " if (true)\n" 16476 " {\n" 16477 " a();\n" 16478 " b();\n" 16479 " }\n" 16480 " }\n" 16481 " void g() { return; }\n" 16482 "}\n" 16483 "} // namespace a", 16484 GNUBraceStyle); 16485 16486 verifyFormat("void f()\n" 16487 "{\n" 16488 " if (true)\n" 16489 " {\n" 16490 " a();\n" 16491 " }\n" 16492 " else if (false)\n" 16493 " {\n" 16494 " b();\n" 16495 " }\n" 16496 " else\n" 16497 " {\n" 16498 " c();\n" 16499 " }\n" 16500 "}\n", 16501 GNUBraceStyle); 16502 16503 verifyFormat("void f()\n" 16504 "{\n" 16505 " for (int i = 0; i < 10; ++i)\n" 16506 " {\n" 16507 " a();\n" 16508 " }\n" 16509 " while (false)\n" 16510 " {\n" 16511 " b();\n" 16512 " }\n" 16513 " do\n" 16514 " {\n" 16515 " c();\n" 16516 " }\n" 16517 " while (false);\n" 16518 "}\n", 16519 GNUBraceStyle); 16520 16521 verifyFormat("void f(int a)\n" 16522 "{\n" 16523 " switch (a)\n" 16524 " {\n" 16525 " case 0:\n" 16526 " break;\n" 16527 " case 1:\n" 16528 " {\n" 16529 " break;\n" 16530 " }\n" 16531 " case 2:\n" 16532 " {\n" 16533 " }\n" 16534 " break;\n" 16535 " default:\n" 16536 " break;\n" 16537 " }\n" 16538 "}\n", 16539 GNUBraceStyle); 16540 16541 verifyFormat("enum X\n" 16542 "{\n" 16543 " Y = 0,\n" 16544 "}\n", 16545 GNUBraceStyle); 16546 16547 verifyFormat("@interface BSApplicationController ()\n" 16548 "{\n" 16549 "@private\n" 16550 " id _extraIvar;\n" 16551 "}\n" 16552 "@end\n", 16553 GNUBraceStyle); 16554 16555 verifyFormat("#ifdef _DEBUG\n" 16556 "int foo(int i = 0)\n" 16557 "#else\n" 16558 "int foo(int i = 5)\n" 16559 "#endif\n" 16560 "{\n" 16561 " return i;\n" 16562 "}", 16563 GNUBraceStyle); 16564 16565 verifyFormat("void foo() {}\n" 16566 "void bar()\n" 16567 "#ifdef _DEBUG\n" 16568 "{\n" 16569 " foo();\n" 16570 "}\n" 16571 "#else\n" 16572 "{\n" 16573 "}\n" 16574 "#endif", 16575 GNUBraceStyle); 16576 16577 verifyFormat("void foobar() { int i = 5; }\n" 16578 "#ifdef _DEBUG\n" 16579 "void bar() {}\n" 16580 "#else\n" 16581 "void bar() { foobar(); }\n" 16582 "#endif", 16583 GNUBraceStyle); 16584 } 16585 16586 TEST_F(FormatTest, WebKitBraceBreaking) { 16587 FormatStyle WebKitBraceStyle = getLLVMStyle(); 16588 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 16589 WebKitBraceStyle.FixNamespaceComments = false; 16590 verifyFormat("namespace a {\n" 16591 "class A {\n" 16592 " void f()\n" 16593 " {\n" 16594 " if (true) {\n" 16595 " a();\n" 16596 " b();\n" 16597 " }\n" 16598 " }\n" 16599 " void g() { return; }\n" 16600 "};\n" 16601 "enum E {\n" 16602 " A,\n" 16603 " // foo\n" 16604 " B,\n" 16605 " C\n" 16606 "};\n" 16607 "struct B {\n" 16608 " int x;\n" 16609 "};\n" 16610 "}\n", 16611 WebKitBraceStyle); 16612 verifyFormat("struct S {\n" 16613 " int Type;\n" 16614 " union {\n" 16615 " int x;\n" 16616 " double y;\n" 16617 " } Value;\n" 16618 " class C {\n" 16619 " MyFavoriteType Value;\n" 16620 " } Class;\n" 16621 "};\n", 16622 WebKitBraceStyle); 16623 } 16624 16625 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 16626 verifyFormat("void f() {\n" 16627 " try {\n" 16628 " } catch (const Exception &e) {\n" 16629 " }\n" 16630 "}\n", 16631 getLLVMStyle()); 16632 } 16633 16634 TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) { 16635 auto Style = getLLVMStyle(); 16636 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 16637 Style.AlignConsecutiveAssignments = 16638 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 16639 Style.AlignConsecutiveDeclarations = 16640 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 16641 verifyFormat("struct test demo[] = {\n" 16642 " {56, 23, \"hello\"},\n" 16643 " {-1, 93463, \"world\"},\n" 16644 " { 7, 5, \"!!\"}\n" 16645 "};\n", 16646 Style); 16647 16648 verifyFormat("struct test demo[] = {\n" 16649 " {56, 23, \"hello\"}, // first line\n" 16650 " {-1, 93463, \"world\"}, // second line\n" 16651 " { 7, 5, \"!!\"} // third line\n" 16652 "};\n", 16653 Style); 16654 16655 verifyFormat("struct test demo[4] = {\n" 16656 " { 56, 23, 21, \"oh\"}, // first line\n" 16657 " { -1, 93463, 22, \"my\"}, // second line\n" 16658 " { 7, 5, 1, \"goodness\"} // third line\n" 16659 " {234, 5, 1, \"gracious\"} // fourth line\n" 16660 "};\n", 16661 Style); 16662 16663 verifyFormat("struct test demo[3] = {\n" 16664 " {56, 23, \"hello\"},\n" 16665 " {-1, 93463, \"world\"},\n" 16666 " { 7, 5, \"!!\"}\n" 16667 "};\n", 16668 Style); 16669 16670 verifyFormat("struct test demo[3] = {\n" 16671 " {int{56}, 23, \"hello\"},\n" 16672 " {int{-1}, 93463, \"world\"},\n" 16673 " { int{7}, 5, \"!!\"}\n" 16674 "};\n", 16675 Style); 16676 16677 verifyFormat("struct test demo[] = {\n" 16678 " {56, 23, \"hello\"},\n" 16679 " {-1, 93463, \"world\"},\n" 16680 " { 7, 5, \"!!\"},\n" 16681 "};\n", 16682 Style); 16683 16684 verifyFormat("test demo[] = {\n" 16685 " {56, 23, \"hello\"},\n" 16686 " {-1, 93463, \"world\"},\n" 16687 " { 7, 5, \"!!\"},\n" 16688 "};\n", 16689 Style); 16690 16691 verifyFormat("demo = std::array<struct test, 3>{\n" 16692 " test{56, 23, \"hello\"},\n" 16693 " test{-1, 93463, \"world\"},\n" 16694 " test{ 7, 5, \"!!\"},\n" 16695 "};\n", 16696 Style); 16697 16698 verifyFormat("test demo[] = {\n" 16699 " {56, 23, \"hello\"},\n" 16700 "#if X\n" 16701 " {-1, 93463, \"world\"},\n" 16702 "#endif\n" 16703 " { 7, 5, \"!!\"}\n" 16704 "};\n", 16705 Style); 16706 16707 verifyFormat( 16708 "test demo[] = {\n" 16709 " { 7, 23,\n" 16710 " \"hello world i am a very long line that really, in any\"\n" 16711 " \"just world, ought to be split over multiple lines\"},\n" 16712 " {-1, 93463, \"world\"},\n" 16713 " {56, 5, \"!!\"}\n" 16714 "};\n", 16715 Style); 16716 16717 verifyFormat("return GradForUnaryCwise(g, {\n" 16718 " {{\"sign\"}, \"Sign\", " 16719 " {\"x\", \"dy\"}},\n" 16720 " { {\"dx\"}, \"Mul\", {\"dy\"" 16721 ", \"sign\"}},\n" 16722 "});\n", 16723 Style); 16724 16725 Style.ColumnLimit = 0; 16726 EXPECT_EQ( 16727 "test demo[] = {\n" 16728 " {56, 23, \"hello world i am a very long line that really, " 16729 "in any just world, ought to be split over multiple lines\"},\n" 16730 " {-1, 93463, " 16731 " \"world\"},\n" 16732 " { 7, 5, " 16733 " \"!!\"},\n" 16734 "};", 16735 format("test demo[] = {{56, 23, \"hello world i am a very long line " 16736 "that really, in any just world, ought to be split over multiple " 16737 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 16738 Style)); 16739 16740 Style.ColumnLimit = 80; 16741 verifyFormat("test demo[] = {\n" 16742 " {56, 23, /* a comment */ \"hello\"},\n" 16743 " {-1, 93463, \"world\"},\n" 16744 " { 7, 5, \"!!\"}\n" 16745 "};\n", 16746 Style); 16747 16748 verifyFormat("test demo[] = {\n" 16749 " {56, 23, \"hello\"},\n" 16750 " {-1, 93463, \"world\" /* comment here */},\n" 16751 " { 7, 5, \"!!\"}\n" 16752 "};\n", 16753 Style); 16754 16755 verifyFormat("test demo[] = {\n" 16756 " {56, /* a comment */ 23, \"hello\"},\n" 16757 " {-1, 93463, \"world\"},\n" 16758 " { 7, 5, \"!!\"}\n" 16759 "};\n", 16760 Style); 16761 16762 Style.ColumnLimit = 20; 16763 EXPECT_EQ( 16764 "demo = std::array<\n" 16765 " struct test, 3>{\n" 16766 " test{\n" 16767 " 56, 23,\n" 16768 " \"hello \"\n" 16769 " \"world i \"\n" 16770 " \"am a very \"\n" 16771 " \"long line \"\n" 16772 " \"that \"\n" 16773 " \"really, \"\n" 16774 " \"in any \"\n" 16775 " \"just \"\n" 16776 " \"world, \"\n" 16777 " \"ought to \"\n" 16778 " \"be split \"\n" 16779 " \"over \"\n" 16780 " \"multiple \"\n" 16781 " \"lines\"},\n" 16782 " test{-1, 93463,\n" 16783 " \"world\"},\n" 16784 " test{ 7, 5,\n" 16785 " \"!!\" },\n" 16786 "};", 16787 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world " 16788 "i am a very long line that really, in any just world, ought " 16789 "to be split over multiple lines\"},test{-1, 93463, \"world\"}," 16790 "test{7, 5, \"!!\"},};", 16791 Style)); 16792 // This caused a core dump by enabling Alignment in the LLVMStyle globally 16793 Style = getLLVMStyleWithColumns(50); 16794 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 16795 verifyFormat("static A x = {\n" 16796 " {{init1, init2, init3, init4},\n" 16797 " {init1, init2, init3, init4}}\n" 16798 "};", 16799 Style); 16800 Style.ColumnLimit = 100; 16801 EXPECT_EQ( 16802 "test demo[] = {\n" 16803 " {56, 23,\n" 16804 " \"hello world i am a very long line that really, in any just world" 16805 ", ought to be split over \"\n" 16806 " \"multiple lines\" },\n" 16807 " {-1, 93463, \"world\"},\n" 16808 " { 7, 5, \"!!\"},\n" 16809 "};", 16810 format("test demo[] = {{56, 23, \"hello world i am a very long line " 16811 "that really, in any just world, ought to be split over multiple " 16812 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 16813 Style)); 16814 16815 Style = getLLVMStyleWithColumns(50); 16816 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 16817 Style.AlignConsecutiveAssignments = 16818 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 16819 Style.AlignConsecutiveDeclarations = 16820 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 16821 verifyFormat("struct test demo[] = {\n" 16822 " {56, 23, \"hello\"},\n" 16823 " {-1, 93463, \"world\"},\n" 16824 " { 7, 5, \"!!\"}\n" 16825 "};\n" 16826 "static A x = {\n" 16827 " {{init1, init2, init3, init4},\n" 16828 " {init1, init2, init3, init4}}\n" 16829 "};", 16830 Style); 16831 Style.ColumnLimit = 100; 16832 Style.AlignConsecutiveAssignments = 16833 FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments; 16834 Style.AlignConsecutiveDeclarations = 16835 FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments; 16836 verifyFormat("struct test demo[] = {\n" 16837 " {56, 23, \"hello\"},\n" 16838 " {-1, 93463, \"world\"},\n" 16839 " { 7, 5, \"!!\"}\n" 16840 "};\n" 16841 "struct test demo[4] = {\n" 16842 " { 56, 23, 21, \"oh\"}, // first line\n" 16843 " { -1, 93463, 22, \"my\"}, // second line\n" 16844 " { 7, 5, 1, \"goodness\"} // third line\n" 16845 " {234, 5, 1, \"gracious\"} // fourth line\n" 16846 "};\n", 16847 Style); 16848 EXPECT_EQ( 16849 "test demo[] = {\n" 16850 " {56,\n" 16851 " \"hello world i am a very long line that really, in any just world" 16852 ", ought to be split over \"\n" 16853 " \"multiple lines\", 23},\n" 16854 " {-1, \"world\", 93463},\n" 16855 " { 7, \"!!\", 5},\n" 16856 "};", 16857 format("test demo[] = {{56, \"hello world i am a very long line " 16858 "that really, in any just world, ought to be split over multiple " 16859 "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};", 16860 Style)); 16861 } 16862 16863 TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) { 16864 auto Style = getLLVMStyle(); 16865 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; 16866 verifyFormat("struct test demo[] = {\n" 16867 " {56, 23, \"hello\"},\n" 16868 " {-1, 93463, \"world\"},\n" 16869 " {7, 5, \"!!\" }\n" 16870 "};\n", 16871 Style); 16872 16873 verifyFormat("struct test demo[] = {\n" 16874 " {56, 23, \"hello\"}, // first line\n" 16875 " {-1, 93463, \"world\"}, // second line\n" 16876 " {7, 5, \"!!\" } // third line\n" 16877 "};\n", 16878 Style); 16879 verifyFormat("struct test demo[4] = {\n" 16880 " {56, 23, 21, \"oh\" }, // first line\n" 16881 " {-1, 93463, 22, \"my\" }, // second line\n" 16882 " {7, 5, 1, \"goodness\"} // third line\n" 16883 " {234, 5, 1, \"gracious\"} // fourth line\n" 16884 "};\n", 16885 Style); 16886 verifyFormat("struct test demo[3] = {\n" 16887 " {56, 23, \"hello\"},\n" 16888 " {-1, 93463, \"world\"},\n" 16889 " {7, 5, \"!!\" }\n" 16890 "};\n", 16891 Style); 16892 16893 verifyFormat("struct test demo[3] = {\n" 16894 " {int{56}, 23, \"hello\"},\n" 16895 " {int{-1}, 93463, \"world\"},\n" 16896 " {int{7}, 5, \"!!\" }\n" 16897 "};\n", 16898 Style); 16899 verifyFormat("struct test demo[] = {\n" 16900 " {56, 23, \"hello\"},\n" 16901 " {-1, 93463, \"world\"},\n" 16902 " {7, 5, \"!!\" },\n" 16903 "};\n", 16904 Style); 16905 verifyFormat("test demo[] = {\n" 16906 " {56, 23, \"hello\"},\n" 16907 " {-1, 93463, \"world\"},\n" 16908 " {7, 5, \"!!\" },\n" 16909 "};\n", 16910 Style); 16911 verifyFormat("demo = std::array<struct test, 3>{\n" 16912 " test{56, 23, \"hello\"},\n" 16913 " test{-1, 93463, \"world\"},\n" 16914 " test{7, 5, \"!!\" },\n" 16915 "};\n", 16916 Style); 16917 verifyFormat("test demo[] = {\n" 16918 " {56, 23, \"hello\"},\n" 16919 "#if X\n" 16920 " {-1, 93463, \"world\"},\n" 16921 "#endif\n" 16922 " {7, 5, \"!!\" }\n" 16923 "};\n", 16924 Style); 16925 verifyFormat( 16926 "test demo[] = {\n" 16927 " {7, 23,\n" 16928 " \"hello world i am a very long line that really, in any\"\n" 16929 " \"just world, ought to be split over multiple lines\"},\n" 16930 " {-1, 93463, \"world\" },\n" 16931 " {56, 5, \"!!\" }\n" 16932 "};\n", 16933 Style); 16934 16935 verifyFormat("return GradForUnaryCwise(g, {\n" 16936 " {{\"sign\"}, \"Sign\", {\"x\", " 16937 "\"dy\"} },\n" 16938 " {{\"dx\"}, \"Mul\", " 16939 "{\"dy\", \"sign\"}},\n" 16940 "});\n", 16941 Style); 16942 16943 Style.ColumnLimit = 0; 16944 EXPECT_EQ( 16945 "test demo[] = {\n" 16946 " {56, 23, \"hello world i am a very long line that really, in any " 16947 "just world, ought to be split over multiple lines\"},\n" 16948 " {-1, 93463, \"world\" " 16949 " },\n" 16950 " {7, 5, \"!!\" " 16951 " },\n" 16952 "};", 16953 format("test demo[] = {{56, 23, \"hello world i am a very long line " 16954 "that really, in any just world, ought to be split over multiple " 16955 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 16956 Style)); 16957 16958 Style.ColumnLimit = 80; 16959 verifyFormat("test demo[] = {\n" 16960 " {56, 23, /* a comment */ \"hello\"},\n" 16961 " {-1, 93463, \"world\" },\n" 16962 " {7, 5, \"!!\" }\n" 16963 "};\n", 16964 Style); 16965 16966 verifyFormat("test demo[] = {\n" 16967 " {56, 23, \"hello\" },\n" 16968 " {-1, 93463, \"world\" /* comment here */},\n" 16969 " {7, 5, \"!!\" }\n" 16970 "};\n", 16971 Style); 16972 16973 verifyFormat("test demo[] = {\n" 16974 " {56, /* a comment */ 23, \"hello\"},\n" 16975 " {-1, 93463, \"world\"},\n" 16976 " {7, 5, \"!!\" }\n" 16977 "};\n", 16978 Style); 16979 16980 Style.ColumnLimit = 20; 16981 EXPECT_EQ( 16982 "demo = std::array<\n" 16983 " struct test, 3>{\n" 16984 " test{\n" 16985 " 56, 23,\n" 16986 " \"hello \"\n" 16987 " \"world i \"\n" 16988 " \"am a very \"\n" 16989 " \"long line \"\n" 16990 " \"that \"\n" 16991 " \"really, \"\n" 16992 " \"in any \"\n" 16993 " \"just \"\n" 16994 " \"world, \"\n" 16995 " \"ought to \"\n" 16996 " \"be split \"\n" 16997 " \"over \"\n" 16998 " \"multiple \"\n" 16999 " \"lines\"},\n" 17000 " test{-1, 93463,\n" 17001 " \"world\"},\n" 17002 " test{7, 5,\n" 17003 " \"!!\" },\n" 17004 "};", 17005 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world " 17006 "i am a very long line that really, in any just world, ought " 17007 "to be split over multiple lines\"},test{-1, 93463, \"world\"}," 17008 "test{7, 5, \"!!\"},};", 17009 Style)); 17010 17011 // This caused a core dump by enabling Alignment in the LLVMStyle globally 17012 Style = getLLVMStyleWithColumns(50); 17013 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; 17014 verifyFormat("static A x = {\n" 17015 " {{init1, init2, init3, init4},\n" 17016 " {init1, init2, init3, init4}}\n" 17017 "};", 17018 Style); 17019 Style.ColumnLimit = 100; 17020 EXPECT_EQ( 17021 "test demo[] = {\n" 17022 " {56, 23,\n" 17023 " \"hello world i am a very long line that really, in any just world" 17024 ", ought to be split over \"\n" 17025 " \"multiple lines\" },\n" 17026 " {-1, 93463, \"world\"},\n" 17027 " {7, 5, \"!!\" },\n" 17028 "};", 17029 format("test demo[] = {{56, 23, \"hello world i am a very long line " 17030 "that really, in any just world, ought to be split over multiple " 17031 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 17032 Style)); 17033 } 17034 17035 TEST_F(FormatTest, UnderstandsPragmas) { 17036 verifyFormat("#pragma omp reduction(| : var)"); 17037 verifyFormat("#pragma omp reduction(+ : var)"); 17038 17039 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 17040 "(including parentheses).", 17041 format("#pragma mark Any non-hyphenated or hyphenated string " 17042 "(including parentheses).")); 17043 } 17044 17045 TEST_F(FormatTest, UnderstandPragmaOption) { 17046 verifyFormat("#pragma option -C -A"); 17047 17048 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 17049 } 17050 17051 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) { 17052 FormatStyle Style = getLLVMStyle(); 17053 Style.ColumnLimit = 20; 17054 17055 // See PR41213 17056 EXPECT_EQ("/*\n" 17057 " *\t9012345\n" 17058 " * /8901\n" 17059 " */", 17060 format("/*\n" 17061 " *\t9012345 /8901\n" 17062 " */", 17063 Style)); 17064 EXPECT_EQ("/*\n" 17065 " *345678\n" 17066 " *\t/8901\n" 17067 " */", 17068 format("/*\n" 17069 " *345678\t/8901\n" 17070 " */", 17071 Style)); 17072 17073 verifyFormat("int a; // the\n" 17074 " // comment", 17075 Style); 17076 EXPECT_EQ("int a; /* first line\n" 17077 " * second\n" 17078 " * line third\n" 17079 " * line\n" 17080 " */", 17081 format("int a; /* first line\n" 17082 " * second\n" 17083 " * line third\n" 17084 " * line\n" 17085 " */", 17086 Style)); 17087 EXPECT_EQ("int a; // first line\n" 17088 " // second\n" 17089 " // line third\n" 17090 " // line", 17091 format("int a; // first line\n" 17092 " // second line\n" 17093 " // third line", 17094 Style)); 17095 17096 Style.PenaltyExcessCharacter = 90; 17097 verifyFormat("int a; // the comment", Style); 17098 EXPECT_EQ("int a; // the comment\n" 17099 " // aaa", 17100 format("int a; // the comment aaa", Style)); 17101 EXPECT_EQ("int a; /* first line\n" 17102 " * second line\n" 17103 " * third line\n" 17104 " */", 17105 format("int a; /* first line\n" 17106 " * second line\n" 17107 " * third line\n" 17108 " */", 17109 Style)); 17110 EXPECT_EQ("int a; // first line\n" 17111 " // second line\n" 17112 " // third line", 17113 format("int a; // first line\n" 17114 " // second line\n" 17115 " // third line", 17116 Style)); 17117 // FIXME: Investigate why this is not getting the same layout as the test 17118 // above. 17119 EXPECT_EQ("int a; /* first line\n" 17120 " * second line\n" 17121 " * third line\n" 17122 " */", 17123 format("int a; /* first line second line third line" 17124 "\n*/", 17125 Style)); 17126 17127 EXPECT_EQ("// foo bar baz bazfoo\n" 17128 "// foo bar foo bar\n", 17129 format("// foo bar baz bazfoo\n" 17130 "// foo bar foo bar\n", 17131 Style)); 17132 EXPECT_EQ("// foo bar baz bazfoo\n" 17133 "// foo bar foo bar\n", 17134 format("// foo bar baz bazfoo\n" 17135 "// foo bar foo bar\n", 17136 Style)); 17137 17138 // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the 17139 // next one. 17140 EXPECT_EQ("// foo bar baz bazfoo\n" 17141 "// bar foo bar\n", 17142 format("// foo bar baz bazfoo bar\n" 17143 "// foo bar\n", 17144 Style)); 17145 17146 EXPECT_EQ("// foo bar baz bazfoo\n" 17147 "// foo bar baz bazfoo\n" 17148 "// bar foo bar\n", 17149 format("// foo bar baz bazfoo\n" 17150 "// foo bar baz bazfoo bar\n" 17151 "// foo bar\n", 17152 Style)); 17153 17154 EXPECT_EQ("// foo bar baz bazfoo\n" 17155 "// foo bar baz bazfoo\n" 17156 "// bar foo bar\n", 17157 format("// foo bar baz bazfoo\n" 17158 "// foo bar baz bazfoo bar\n" 17159 "// foo bar\n", 17160 Style)); 17161 17162 // Make sure we do not keep protruding characters if strict mode reflow is 17163 // cheaper than keeping protruding characters. 17164 Style.ColumnLimit = 21; 17165 EXPECT_EQ( 17166 "// foo foo foo foo\n" 17167 "// foo foo foo foo\n" 17168 "// foo foo foo foo\n", 17169 format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", Style)); 17170 17171 EXPECT_EQ("int a = /* long block\n" 17172 " comment */\n" 17173 " 42;", 17174 format("int a = /* long block comment */ 42;", Style)); 17175 } 17176 17177 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 17178 for (size_t i = 1; i < Styles.size(); ++i) \ 17179 EXPECT_EQ(Styles[0], Styles[i]) \ 17180 << "Style #" << i << " of " << Styles.size() << " differs from Style #0" 17181 17182 TEST_F(FormatTest, GetsPredefinedStyleByName) { 17183 SmallVector<FormatStyle, 3> Styles; 17184 Styles.resize(3); 17185 17186 Styles[0] = getLLVMStyle(); 17187 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 17188 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 17189 EXPECT_ALL_STYLES_EQUAL(Styles); 17190 17191 Styles[0] = getGoogleStyle(); 17192 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 17193 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 17194 EXPECT_ALL_STYLES_EQUAL(Styles); 17195 17196 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 17197 EXPECT_TRUE( 17198 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 17199 EXPECT_TRUE( 17200 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 17201 EXPECT_ALL_STYLES_EQUAL(Styles); 17202 17203 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 17204 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 17205 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 17206 EXPECT_ALL_STYLES_EQUAL(Styles); 17207 17208 Styles[0] = getMozillaStyle(); 17209 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 17210 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 17211 EXPECT_ALL_STYLES_EQUAL(Styles); 17212 17213 Styles[0] = getWebKitStyle(); 17214 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 17215 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 17216 EXPECT_ALL_STYLES_EQUAL(Styles); 17217 17218 Styles[0] = getGNUStyle(); 17219 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 17220 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 17221 EXPECT_ALL_STYLES_EQUAL(Styles); 17222 17223 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 17224 } 17225 17226 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 17227 SmallVector<FormatStyle, 8> Styles; 17228 Styles.resize(2); 17229 17230 Styles[0] = getGoogleStyle(); 17231 Styles[1] = getLLVMStyle(); 17232 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 17233 EXPECT_ALL_STYLES_EQUAL(Styles); 17234 17235 Styles.resize(5); 17236 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 17237 Styles[1] = getLLVMStyle(); 17238 Styles[1].Language = FormatStyle::LK_JavaScript; 17239 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 17240 17241 Styles[2] = getLLVMStyle(); 17242 Styles[2].Language = FormatStyle::LK_JavaScript; 17243 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 17244 "BasedOnStyle: Google", 17245 &Styles[2]) 17246 .value()); 17247 17248 Styles[3] = getLLVMStyle(); 17249 Styles[3].Language = FormatStyle::LK_JavaScript; 17250 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 17251 "Language: JavaScript", 17252 &Styles[3]) 17253 .value()); 17254 17255 Styles[4] = getLLVMStyle(); 17256 Styles[4].Language = FormatStyle::LK_JavaScript; 17257 EXPECT_EQ(0, parseConfiguration("---\n" 17258 "BasedOnStyle: LLVM\n" 17259 "IndentWidth: 123\n" 17260 "---\n" 17261 "BasedOnStyle: Google\n" 17262 "Language: JavaScript", 17263 &Styles[4]) 17264 .value()); 17265 EXPECT_ALL_STYLES_EQUAL(Styles); 17266 } 17267 17268 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 17269 Style.FIELD = false; \ 17270 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 17271 EXPECT_TRUE(Style.FIELD); \ 17272 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 17273 EXPECT_FALSE(Style.FIELD); 17274 17275 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 17276 17277 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 17278 Style.STRUCT.FIELD = false; \ 17279 EXPECT_EQ(0, \ 17280 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 17281 .value()); \ 17282 EXPECT_TRUE(Style.STRUCT.FIELD); \ 17283 EXPECT_EQ(0, \ 17284 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 17285 .value()); \ 17286 EXPECT_FALSE(Style.STRUCT.FIELD); 17287 17288 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 17289 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 17290 17291 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 17292 EXPECT_NE(VALUE, Style.FIELD) << "Initial value already the same!"; \ 17293 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 17294 EXPECT_EQ(VALUE, Style.FIELD) << "Unexpected value after parsing!" 17295 17296 TEST_F(FormatTest, ParsesConfigurationBools) { 17297 FormatStyle Style = {}; 17298 Style.Language = FormatStyle::LK_Cpp; 17299 CHECK_PARSE_BOOL(AlignTrailingComments); 17300 CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine); 17301 CHECK_PARSE_BOOL(AllowAllConstructorInitializersOnNextLine); 17302 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 17303 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 17304 CHECK_PARSE_BOOL(AllowShortEnumsOnASingleLine); 17305 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 17306 CHECK_PARSE_BOOL(BinPackArguments); 17307 CHECK_PARSE_BOOL(BinPackParameters); 17308 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 17309 CHECK_PARSE_BOOL(BreakBeforeConceptDeclarations); 17310 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 17311 CHECK_PARSE_BOOL(BreakStringLiterals); 17312 CHECK_PARSE_BOOL(CompactNamespaces); 17313 CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); 17314 CHECK_PARSE_BOOL(DeriveLineEnding); 17315 CHECK_PARSE_BOOL(DerivePointerAlignment); 17316 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 17317 CHECK_PARSE_BOOL(DisableFormat); 17318 CHECK_PARSE_BOOL(IndentAccessModifiers); 17319 CHECK_PARSE_BOOL(IndentCaseLabels); 17320 CHECK_PARSE_BOOL(IndentCaseBlocks); 17321 CHECK_PARSE_BOOL(IndentGotoLabels); 17322 CHECK_PARSE_BOOL(IndentRequires); 17323 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 17324 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 17325 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 17326 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 17327 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 17328 CHECK_PARSE_BOOL(ReflowComments); 17329 CHECK_PARSE_BOOL(SortUsingDeclarations); 17330 CHECK_PARSE_BOOL(SpacesInParentheses); 17331 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 17332 CHECK_PARSE_BOOL(SpacesInConditionalStatement); 17333 CHECK_PARSE_BOOL(SpaceInEmptyBlock); 17334 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 17335 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 17336 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 17337 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 17338 CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); 17339 CHECK_PARSE_BOOL(SpaceAfterLogicalNot); 17340 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 17341 CHECK_PARSE_BOOL(SpaceBeforeCaseColon); 17342 CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList); 17343 CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon); 17344 CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon); 17345 CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon); 17346 CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets); 17347 CHECK_PARSE_BOOL(UseCRLF); 17348 17349 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel); 17350 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 17351 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 17352 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 17353 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 17354 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 17355 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 17356 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 17357 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock); 17358 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 17359 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 17360 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeLambdaBody); 17361 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeWhile); 17362 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 17363 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction); 17364 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord); 17365 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace); 17366 } 17367 17368 #undef CHECK_PARSE_BOOL 17369 17370 TEST_F(FormatTest, ParsesConfiguration) { 17371 FormatStyle Style = {}; 17372 Style.Language = FormatStyle::LK_Cpp; 17373 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 17374 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 17375 ConstructorInitializerIndentWidth, 1234u); 17376 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 17377 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 17378 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 17379 CHECK_PARSE("PenaltyBreakAssignment: 1234", PenaltyBreakAssignment, 1234u); 17380 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 17381 PenaltyBreakBeforeFirstCallParameter, 1234u); 17382 CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234", 17383 PenaltyBreakTemplateDeclaration, 1234u); 17384 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 17385 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 17386 PenaltyReturnTypeOnItsOwnLine, 1234u); 17387 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 17388 SpacesBeforeTrailingComments, 1234u); 17389 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 17390 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 17391 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$"); 17392 17393 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 17394 CHECK_PARSE("AlignConsecutiveAssignments: None", AlignConsecutiveAssignments, 17395 FormatStyle::ACS_None); 17396 CHECK_PARSE("AlignConsecutiveAssignments: Consecutive", 17397 AlignConsecutiveAssignments, FormatStyle::ACS_Consecutive); 17398 CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLines", 17399 AlignConsecutiveAssignments, FormatStyle::ACS_AcrossEmptyLines); 17400 CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLinesAndComments", 17401 AlignConsecutiveAssignments, 17402 FormatStyle::ACS_AcrossEmptyLinesAndComments); 17403 // For backwards compability, false / true should still parse 17404 CHECK_PARSE("AlignConsecutiveAssignments: false", AlignConsecutiveAssignments, 17405 FormatStyle::ACS_None); 17406 CHECK_PARSE("AlignConsecutiveAssignments: true", AlignConsecutiveAssignments, 17407 FormatStyle::ACS_Consecutive); 17408 17409 Style.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive; 17410 CHECK_PARSE("AlignConsecutiveBitFields: None", AlignConsecutiveBitFields, 17411 FormatStyle::ACS_None); 17412 CHECK_PARSE("AlignConsecutiveBitFields: Consecutive", 17413 AlignConsecutiveBitFields, FormatStyle::ACS_Consecutive); 17414 CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLines", 17415 AlignConsecutiveBitFields, FormatStyle::ACS_AcrossEmptyLines); 17416 CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLinesAndComments", 17417 AlignConsecutiveBitFields, 17418 FormatStyle::ACS_AcrossEmptyLinesAndComments); 17419 // For backwards compability, false / true should still parse 17420 CHECK_PARSE("AlignConsecutiveBitFields: false", AlignConsecutiveBitFields, 17421 FormatStyle::ACS_None); 17422 CHECK_PARSE("AlignConsecutiveBitFields: true", AlignConsecutiveBitFields, 17423 FormatStyle::ACS_Consecutive); 17424 17425 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 17426 CHECK_PARSE("AlignConsecutiveMacros: None", AlignConsecutiveMacros, 17427 FormatStyle::ACS_None); 17428 CHECK_PARSE("AlignConsecutiveMacros: Consecutive", AlignConsecutiveMacros, 17429 FormatStyle::ACS_Consecutive); 17430 CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLines", 17431 AlignConsecutiveMacros, FormatStyle::ACS_AcrossEmptyLines); 17432 CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLinesAndComments", 17433 AlignConsecutiveMacros, 17434 FormatStyle::ACS_AcrossEmptyLinesAndComments); 17435 // For backwards compability, false / true should still parse 17436 CHECK_PARSE("AlignConsecutiveMacros: false", AlignConsecutiveMacros, 17437 FormatStyle::ACS_None); 17438 CHECK_PARSE("AlignConsecutiveMacros: true", AlignConsecutiveMacros, 17439 FormatStyle::ACS_Consecutive); 17440 17441 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 17442 CHECK_PARSE("AlignConsecutiveDeclarations: None", 17443 AlignConsecutiveDeclarations, FormatStyle::ACS_None); 17444 CHECK_PARSE("AlignConsecutiveDeclarations: Consecutive", 17445 AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive); 17446 CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLines", 17447 AlignConsecutiveDeclarations, FormatStyle::ACS_AcrossEmptyLines); 17448 CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLinesAndComments", 17449 AlignConsecutiveDeclarations, 17450 FormatStyle::ACS_AcrossEmptyLinesAndComments); 17451 // For backwards compability, false / true should still parse 17452 CHECK_PARSE("AlignConsecutiveDeclarations: false", 17453 AlignConsecutiveDeclarations, FormatStyle::ACS_None); 17454 CHECK_PARSE("AlignConsecutiveDeclarations: true", 17455 AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive); 17456 17457 Style.PointerAlignment = FormatStyle::PAS_Middle; 17458 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 17459 FormatStyle::PAS_Left); 17460 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 17461 FormatStyle::PAS_Right); 17462 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 17463 FormatStyle::PAS_Middle); 17464 // For backward compatibility: 17465 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 17466 FormatStyle::PAS_Left); 17467 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 17468 FormatStyle::PAS_Right); 17469 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 17470 FormatStyle::PAS_Middle); 17471 17472 Style.Standard = FormatStyle::LS_Auto; 17473 CHECK_PARSE("Standard: c++03", Standard, FormatStyle::LS_Cpp03); 17474 CHECK_PARSE("Standard: c++11", Standard, FormatStyle::LS_Cpp11); 17475 CHECK_PARSE("Standard: c++14", Standard, FormatStyle::LS_Cpp14); 17476 CHECK_PARSE("Standard: c++17", Standard, FormatStyle::LS_Cpp17); 17477 CHECK_PARSE("Standard: c++20", Standard, FormatStyle::LS_Cpp20); 17478 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 17479 CHECK_PARSE("Standard: Latest", Standard, FormatStyle::LS_Latest); 17480 // Legacy aliases: 17481 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 17482 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Latest); 17483 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 17484 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 17485 17486 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 17487 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 17488 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 17489 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 17490 FormatStyle::BOS_None); 17491 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 17492 FormatStyle::BOS_All); 17493 // For backward compatibility: 17494 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 17495 FormatStyle::BOS_None); 17496 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 17497 FormatStyle::BOS_All); 17498 17499 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 17500 CHECK_PARSE("BreakConstructorInitializers: BeforeComma", 17501 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 17502 CHECK_PARSE("BreakConstructorInitializers: AfterColon", 17503 BreakConstructorInitializers, FormatStyle::BCIS_AfterColon); 17504 CHECK_PARSE("BreakConstructorInitializers: BeforeColon", 17505 BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon); 17506 // For backward compatibility: 17507 CHECK_PARSE("BreakConstructorInitializersBeforeComma: true", 17508 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 17509 17510 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 17511 CHECK_PARSE("BreakInheritanceList: AfterComma", BreakInheritanceList, 17512 FormatStyle::BILS_AfterComma); 17513 CHECK_PARSE("BreakInheritanceList: BeforeComma", BreakInheritanceList, 17514 FormatStyle::BILS_BeforeComma); 17515 CHECK_PARSE("BreakInheritanceList: AfterColon", BreakInheritanceList, 17516 FormatStyle::BILS_AfterColon); 17517 CHECK_PARSE("BreakInheritanceList: BeforeColon", BreakInheritanceList, 17518 FormatStyle::BILS_BeforeColon); 17519 // For backward compatibility: 17520 CHECK_PARSE("BreakBeforeInheritanceComma: true", BreakInheritanceList, 17521 FormatStyle::BILS_BeforeComma); 17522 17523 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 17524 CHECK_PARSE("EmptyLineBeforeAccessModifier: Never", 17525 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Never); 17526 CHECK_PARSE("EmptyLineBeforeAccessModifier: Leave", 17527 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Leave); 17528 CHECK_PARSE("EmptyLineBeforeAccessModifier: LogicalBlock", 17529 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_LogicalBlock); 17530 CHECK_PARSE("EmptyLineBeforeAccessModifier: Always", 17531 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Always); 17532 17533 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 17534 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 17535 FormatStyle::BAS_Align); 17536 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 17537 FormatStyle::BAS_DontAlign); 17538 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 17539 FormatStyle::BAS_AlwaysBreak); 17540 // For backward compatibility: 17541 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 17542 FormatStyle::BAS_DontAlign); 17543 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 17544 FormatStyle::BAS_Align); 17545 17546 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left; 17547 CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines, 17548 FormatStyle::ENAS_DontAlign); 17549 CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines, 17550 FormatStyle::ENAS_Left); 17551 CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines, 17552 FormatStyle::ENAS_Right); 17553 // For backward compatibility: 17554 CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines, 17555 FormatStyle::ENAS_Left); 17556 CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines, 17557 FormatStyle::ENAS_Right); 17558 17559 Style.AlignOperands = FormatStyle::OAS_Align; 17560 CHECK_PARSE("AlignOperands: DontAlign", AlignOperands, 17561 FormatStyle::OAS_DontAlign); 17562 CHECK_PARSE("AlignOperands: Align", AlignOperands, FormatStyle::OAS_Align); 17563 CHECK_PARSE("AlignOperands: AlignAfterOperator", AlignOperands, 17564 FormatStyle::OAS_AlignAfterOperator); 17565 // For backward compatibility: 17566 CHECK_PARSE("AlignOperands: false", AlignOperands, 17567 FormatStyle::OAS_DontAlign); 17568 CHECK_PARSE("AlignOperands: true", AlignOperands, FormatStyle::OAS_Align); 17569 17570 Style.UseTab = FormatStyle::UT_ForIndentation; 17571 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 17572 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 17573 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 17574 CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab, 17575 FormatStyle::UT_ForContinuationAndIndentation); 17576 CHECK_PARSE("UseTab: AlignWithSpaces", UseTab, 17577 FormatStyle::UT_AlignWithSpaces); 17578 // For backward compatibility: 17579 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 17580 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 17581 17582 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 17583 CHECK_PARSE("AllowShortBlocksOnASingleLine: Never", 17584 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 17585 CHECK_PARSE("AllowShortBlocksOnASingleLine: Empty", 17586 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Empty); 17587 CHECK_PARSE("AllowShortBlocksOnASingleLine: Always", 17588 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always); 17589 // For backward compatibility: 17590 CHECK_PARSE("AllowShortBlocksOnASingleLine: false", 17591 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 17592 CHECK_PARSE("AllowShortBlocksOnASingleLine: true", 17593 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always); 17594 17595 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 17596 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 17597 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 17598 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 17599 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 17600 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 17601 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 17602 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 17603 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 17604 // For backward compatibility: 17605 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 17606 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 17607 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 17608 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 17609 17610 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Both; 17611 CHECK_PARSE("SpaceAroundPointerQualifiers: Default", 17612 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Default); 17613 CHECK_PARSE("SpaceAroundPointerQualifiers: Before", 17614 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Before); 17615 CHECK_PARSE("SpaceAroundPointerQualifiers: After", 17616 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_After); 17617 CHECK_PARSE("SpaceAroundPointerQualifiers: Both", 17618 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Both); 17619 17620 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 17621 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 17622 FormatStyle::SBPO_Never); 17623 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 17624 FormatStyle::SBPO_Always); 17625 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 17626 FormatStyle::SBPO_ControlStatements); 17627 CHECK_PARSE("SpaceBeforeParens: NonEmptyParentheses", SpaceBeforeParens, 17628 FormatStyle::SBPO_NonEmptyParentheses); 17629 // For backward compatibility: 17630 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 17631 FormatStyle::SBPO_Never); 17632 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 17633 FormatStyle::SBPO_ControlStatements); 17634 17635 Style.ColumnLimit = 123; 17636 FormatStyle BaseStyle = getLLVMStyle(); 17637 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 17638 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 17639 17640 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 17641 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 17642 FormatStyle::BS_Attach); 17643 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 17644 FormatStyle::BS_Linux); 17645 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 17646 FormatStyle::BS_Mozilla); 17647 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 17648 FormatStyle::BS_Stroustrup); 17649 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 17650 FormatStyle::BS_Allman); 17651 CHECK_PARSE("BreakBeforeBraces: Whitesmiths", BreakBeforeBraces, 17652 FormatStyle::BS_Whitesmiths); 17653 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 17654 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 17655 FormatStyle::BS_WebKit); 17656 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 17657 FormatStyle::BS_Custom); 17658 17659 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never; 17660 CHECK_PARSE("BraceWrapping:\n" 17661 " AfterControlStatement: MultiLine", 17662 BraceWrapping.AfterControlStatement, 17663 FormatStyle::BWACS_MultiLine); 17664 CHECK_PARSE("BraceWrapping:\n" 17665 " AfterControlStatement: Always", 17666 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always); 17667 CHECK_PARSE("BraceWrapping:\n" 17668 " AfterControlStatement: Never", 17669 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never); 17670 // For backward compatibility: 17671 CHECK_PARSE("BraceWrapping:\n" 17672 " AfterControlStatement: true", 17673 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always); 17674 CHECK_PARSE("BraceWrapping:\n" 17675 " AfterControlStatement: false", 17676 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never); 17677 17678 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 17679 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 17680 FormatStyle::RTBS_None); 17681 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 17682 FormatStyle::RTBS_All); 17683 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 17684 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 17685 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 17686 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 17687 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 17688 AlwaysBreakAfterReturnType, 17689 FormatStyle::RTBS_TopLevelDefinitions); 17690 17691 Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 17692 CHECK_PARSE("AlwaysBreakTemplateDeclarations: No", 17693 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_No); 17694 CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine", 17695 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine); 17696 CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes", 17697 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes); 17698 CHECK_PARSE("AlwaysBreakTemplateDeclarations: false", 17699 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine); 17700 CHECK_PARSE("AlwaysBreakTemplateDeclarations: true", 17701 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes); 17702 17703 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 17704 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 17705 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 17706 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 17707 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 17708 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 17709 AlwaysBreakAfterDefinitionReturnType, 17710 FormatStyle::DRTBS_TopLevel); 17711 17712 Style.NamespaceIndentation = FormatStyle::NI_All; 17713 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 17714 FormatStyle::NI_None); 17715 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 17716 FormatStyle::NI_Inner); 17717 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 17718 FormatStyle::NI_All); 17719 17720 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_OnlyFirstIf; 17721 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Never", 17722 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never); 17723 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: WithoutElse", 17724 AllowShortIfStatementsOnASingleLine, 17725 FormatStyle::SIS_WithoutElse); 17726 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: OnlyFirstIf", 17727 AllowShortIfStatementsOnASingleLine, 17728 FormatStyle::SIS_OnlyFirstIf); 17729 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: AllIfsAndElse", 17730 AllowShortIfStatementsOnASingleLine, 17731 FormatStyle::SIS_AllIfsAndElse); 17732 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Always", 17733 AllowShortIfStatementsOnASingleLine, 17734 FormatStyle::SIS_OnlyFirstIf); 17735 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: false", 17736 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never); 17737 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: true", 17738 AllowShortIfStatementsOnASingleLine, 17739 FormatStyle::SIS_WithoutElse); 17740 17741 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; 17742 CHECK_PARSE("IndentExternBlock: AfterExternBlock", IndentExternBlock, 17743 FormatStyle::IEBS_AfterExternBlock); 17744 CHECK_PARSE("IndentExternBlock: Indent", IndentExternBlock, 17745 FormatStyle::IEBS_Indent); 17746 CHECK_PARSE("IndentExternBlock: NoIndent", IndentExternBlock, 17747 FormatStyle::IEBS_NoIndent); 17748 CHECK_PARSE("IndentExternBlock: true", IndentExternBlock, 17749 FormatStyle::IEBS_Indent); 17750 CHECK_PARSE("IndentExternBlock: false", IndentExternBlock, 17751 FormatStyle::IEBS_NoIndent); 17752 17753 Style.BitFieldColonSpacing = FormatStyle::BFCS_None; 17754 CHECK_PARSE("BitFieldColonSpacing: Both", BitFieldColonSpacing, 17755 FormatStyle::BFCS_Both); 17756 CHECK_PARSE("BitFieldColonSpacing: None", BitFieldColonSpacing, 17757 FormatStyle::BFCS_None); 17758 CHECK_PARSE("BitFieldColonSpacing: Before", BitFieldColonSpacing, 17759 FormatStyle::BFCS_Before); 17760 CHECK_PARSE("BitFieldColonSpacing: After", BitFieldColonSpacing, 17761 FormatStyle::BFCS_After); 17762 17763 Style.SortJavaStaticImport = FormatStyle::SJSIO_Before; 17764 CHECK_PARSE("SortJavaStaticImport: After", SortJavaStaticImport, 17765 FormatStyle::SJSIO_After); 17766 CHECK_PARSE("SortJavaStaticImport: Before", SortJavaStaticImport, 17767 FormatStyle::SJSIO_Before); 17768 17769 // FIXME: This is required because parsing a configuration simply overwrites 17770 // the first N elements of the list instead of resetting it. 17771 Style.ForEachMacros.clear(); 17772 std::vector<std::string> BoostForeach; 17773 BoostForeach.push_back("BOOST_FOREACH"); 17774 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 17775 std::vector<std::string> BoostAndQForeach; 17776 BoostAndQForeach.push_back("BOOST_FOREACH"); 17777 BoostAndQForeach.push_back("Q_FOREACH"); 17778 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 17779 BoostAndQForeach); 17780 17781 Style.AttributeMacros.clear(); 17782 CHECK_PARSE("BasedOnStyle: LLVM", AttributeMacros, 17783 std::vector<std::string>{"__capability"}); 17784 CHECK_PARSE("AttributeMacros: [attr1, attr2]", AttributeMacros, 17785 std::vector<std::string>({"attr1", "attr2"})); 17786 17787 Style.StatementAttributeLikeMacros.clear(); 17788 CHECK_PARSE("StatementAttributeLikeMacros: [emit,Q_EMIT]", 17789 StatementAttributeLikeMacros, 17790 std::vector<std::string>({"emit", "Q_EMIT"})); 17791 17792 Style.StatementMacros.clear(); 17793 CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros, 17794 std::vector<std::string>{"QUNUSED"}); 17795 CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros, 17796 std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"})); 17797 17798 Style.NamespaceMacros.clear(); 17799 CHECK_PARSE("NamespaceMacros: [TESTSUITE]", NamespaceMacros, 17800 std::vector<std::string>{"TESTSUITE"}); 17801 CHECK_PARSE("NamespaceMacros: [TESTSUITE, SUITE]", NamespaceMacros, 17802 std::vector<std::string>({"TESTSUITE", "SUITE"})); 17803 17804 Style.WhitespaceSensitiveMacros.clear(); 17805 CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE]", 17806 WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"}); 17807 CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE, ASSERT]", 17808 WhitespaceSensitiveMacros, 17809 std::vector<std::string>({"STRINGIZE", "ASSERT"})); 17810 Style.WhitespaceSensitiveMacros.clear(); 17811 CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE']", 17812 WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"}); 17813 CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE', 'ASSERT']", 17814 WhitespaceSensitiveMacros, 17815 std::vector<std::string>({"STRINGIZE", "ASSERT"})); 17816 17817 Style.IncludeStyle.IncludeCategories.clear(); 17818 std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = { 17819 {"abc/.*", 2, 0, false}, {".*", 1, 0, true}}; 17820 CHECK_PARSE("IncludeCategories:\n" 17821 " - Regex: abc/.*\n" 17822 " Priority: 2\n" 17823 " - Regex: .*\n" 17824 " Priority: 1\n" 17825 " CaseSensitive: true\n", 17826 IncludeStyle.IncludeCategories, ExpectedCategories); 17827 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex, 17828 "abc$"); 17829 CHECK_PARSE("IncludeIsMainSourceRegex: 'abc$'", 17830 IncludeStyle.IncludeIsMainSourceRegex, "abc$"); 17831 17832 Style.SortIncludes = FormatStyle::SI_Never; 17833 CHECK_PARSE("SortIncludes: true", SortIncludes, 17834 FormatStyle::SI_CaseSensitive); 17835 CHECK_PARSE("SortIncludes: false", SortIncludes, FormatStyle::SI_Never); 17836 CHECK_PARSE("SortIncludes: CaseInsensitive", SortIncludes, 17837 FormatStyle::SI_CaseInsensitive); 17838 CHECK_PARSE("SortIncludes: CaseSensitive", SortIncludes, 17839 FormatStyle::SI_CaseSensitive); 17840 CHECK_PARSE("SortIncludes: Never", SortIncludes, FormatStyle::SI_Never); 17841 17842 Style.RawStringFormats.clear(); 17843 std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = { 17844 { 17845 FormatStyle::LK_TextProto, 17846 {"pb", "proto"}, 17847 {"PARSE_TEXT_PROTO"}, 17848 /*CanonicalDelimiter=*/"", 17849 "llvm", 17850 }, 17851 { 17852 FormatStyle::LK_Cpp, 17853 {"cc", "cpp"}, 17854 {"C_CODEBLOCK", "CPPEVAL"}, 17855 /*CanonicalDelimiter=*/"cc", 17856 /*BasedOnStyle=*/"", 17857 }, 17858 }; 17859 17860 CHECK_PARSE("RawStringFormats:\n" 17861 " - Language: TextProto\n" 17862 " Delimiters:\n" 17863 " - 'pb'\n" 17864 " - 'proto'\n" 17865 " EnclosingFunctions:\n" 17866 " - 'PARSE_TEXT_PROTO'\n" 17867 " BasedOnStyle: llvm\n" 17868 " - Language: Cpp\n" 17869 " Delimiters:\n" 17870 " - 'cc'\n" 17871 " - 'cpp'\n" 17872 " EnclosingFunctions:\n" 17873 " - 'C_CODEBLOCK'\n" 17874 " - 'CPPEVAL'\n" 17875 " CanonicalDelimiter: 'cc'", 17876 RawStringFormats, ExpectedRawStringFormats); 17877 17878 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 17879 " Minimum: 0\n" 17880 " Maximum: 0", 17881 SpacesInLineCommentPrefix.Minimum, 0u); 17882 EXPECT_EQ(Style.SpacesInLineCommentPrefix.Maximum, 0u); 17883 Style.SpacesInLineCommentPrefix.Minimum = 1; 17884 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 17885 " Minimum: 2", 17886 SpacesInLineCommentPrefix.Minimum, 0u); 17887 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 17888 " Maximum: -1", 17889 SpacesInLineCommentPrefix.Maximum, -1u); 17890 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 17891 " Minimum: 2", 17892 SpacesInLineCommentPrefix.Minimum, 2u); 17893 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 17894 " Maximum: 1", 17895 SpacesInLineCommentPrefix.Maximum, 1u); 17896 EXPECT_EQ(Style.SpacesInLineCommentPrefix.Minimum, 1u); 17897 17898 Style.SpacesInAngles = FormatStyle::SIAS_Always; 17899 CHECK_PARSE("SpacesInAngles: Never", SpacesInAngles, FormatStyle::SIAS_Never); 17900 CHECK_PARSE("SpacesInAngles: Always", SpacesInAngles, 17901 FormatStyle::SIAS_Always); 17902 CHECK_PARSE("SpacesInAngles: Leave", SpacesInAngles, FormatStyle::SIAS_Leave); 17903 // For backward compatibility: 17904 CHECK_PARSE("SpacesInAngles: false", SpacesInAngles, FormatStyle::SIAS_Never); 17905 CHECK_PARSE("SpacesInAngles: true", SpacesInAngles, FormatStyle::SIAS_Always); 17906 } 17907 17908 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 17909 FormatStyle Style = {}; 17910 Style.Language = FormatStyle::LK_Cpp; 17911 CHECK_PARSE("Language: Cpp\n" 17912 "IndentWidth: 12", 17913 IndentWidth, 12u); 17914 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 17915 "IndentWidth: 34", 17916 &Style), 17917 ParseError::Unsuitable); 17918 FormatStyle BinPackedTCS = {}; 17919 BinPackedTCS.Language = FormatStyle::LK_JavaScript; 17920 EXPECT_EQ(parseConfiguration("BinPackArguments: true\n" 17921 "InsertTrailingCommas: Wrapped", 17922 &BinPackedTCS), 17923 ParseError::BinPackTrailingCommaConflict); 17924 EXPECT_EQ(12u, Style.IndentWidth); 17925 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 17926 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 17927 17928 Style.Language = FormatStyle::LK_JavaScript; 17929 CHECK_PARSE("Language: JavaScript\n" 17930 "IndentWidth: 12", 17931 IndentWidth, 12u); 17932 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 17933 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 17934 "IndentWidth: 34", 17935 &Style), 17936 ParseError::Unsuitable); 17937 EXPECT_EQ(23u, Style.IndentWidth); 17938 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 17939 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 17940 17941 CHECK_PARSE("BasedOnStyle: LLVM\n" 17942 "IndentWidth: 67", 17943 IndentWidth, 67u); 17944 17945 CHECK_PARSE("---\n" 17946 "Language: JavaScript\n" 17947 "IndentWidth: 12\n" 17948 "---\n" 17949 "Language: Cpp\n" 17950 "IndentWidth: 34\n" 17951 "...\n", 17952 IndentWidth, 12u); 17953 17954 Style.Language = FormatStyle::LK_Cpp; 17955 CHECK_PARSE("---\n" 17956 "Language: JavaScript\n" 17957 "IndentWidth: 12\n" 17958 "---\n" 17959 "Language: Cpp\n" 17960 "IndentWidth: 34\n" 17961 "...\n", 17962 IndentWidth, 34u); 17963 CHECK_PARSE("---\n" 17964 "IndentWidth: 78\n" 17965 "---\n" 17966 "Language: JavaScript\n" 17967 "IndentWidth: 56\n" 17968 "...\n", 17969 IndentWidth, 78u); 17970 17971 Style.ColumnLimit = 123; 17972 Style.IndentWidth = 234; 17973 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 17974 Style.TabWidth = 345; 17975 EXPECT_FALSE(parseConfiguration("---\n" 17976 "IndentWidth: 456\n" 17977 "BreakBeforeBraces: Allman\n" 17978 "---\n" 17979 "Language: JavaScript\n" 17980 "IndentWidth: 111\n" 17981 "TabWidth: 111\n" 17982 "---\n" 17983 "Language: Cpp\n" 17984 "BreakBeforeBraces: Stroustrup\n" 17985 "TabWidth: 789\n" 17986 "...\n", 17987 &Style)); 17988 EXPECT_EQ(123u, Style.ColumnLimit); 17989 EXPECT_EQ(456u, Style.IndentWidth); 17990 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 17991 EXPECT_EQ(789u, Style.TabWidth); 17992 17993 EXPECT_EQ(parseConfiguration("---\n" 17994 "Language: JavaScript\n" 17995 "IndentWidth: 56\n" 17996 "---\n" 17997 "IndentWidth: 78\n" 17998 "...\n", 17999 &Style), 18000 ParseError::Error); 18001 EXPECT_EQ(parseConfiguration("---\n" 18002 "Language: JavaScript\n" 18003 "IndentWidth: 56\n" 18004 "---\n" 18005 "Language: JavaScript\n" 18006 "IndentWidth: 78\n" 18007 "...\n", 18008 &Style), 18009 ParseError::Error); 18010 18011 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 18012 } 18013 18014 #undef CHECK_PARSE 18015 18016 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 18017 FormatStyle Style = {}; 18018 Style.Language = FormatStyle::LK_JavaScript; 18019 Style.BreakBeforeTernaryOperators = true; 18020 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 18021 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 18022 18023 Style.BreakBeforeTernaryOperators = true; 18024 EXPECT_EQ(0, parseConfiguration("---\n" 18025 "BasedOnStyle: Google\n" 18026 "---\n" 18027 "Language: JavaScript\n" 18028 "IndentWidth: 76\n" 18029 "...\n", 18030 &Style) 18031 .value()); 18032 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 18033 EXPECT_EQ(76u, Style.IndentWidth); 18034 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 18035 } 18036 18037 TEST_F(FormatTest, ConfigurationRoundTripTest) { 18038 FormatStyle Style = getLLVMStyle(); 18039 std::string YAML = configurationAsText(Style); 18040 FormatStyle ParsedStyle = {}; 18041 ParsedStyle.Language = FormatStyle::LK_Cpp; 18042 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 18043 EXPECT_EQ(Style, ParsedStyle); 18044 } 18045 18046 TEST_F(FormatTest, WorksFor8bitEncodings) { 18047 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 18048 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 18049 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 18050 "\"\xef\xee\xf0\xf3...\"", 18051 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 18052 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 18053 "\xef\xee\xf0\xf3...\"", 18054 getLLVMStyleWithColumns(12))); 18055 } 18056 18057 TEST_F(FormatTest, HandlesUTF8BOM) { 18058 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 18059 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 18060 format("\xef\xbb\xbf#include <iostream>")); 18061 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 18062 format("\xef\xbb\xbf\n#include <iostream>")); 18063 } 18064 18065 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 18066 #if !defined(_MSC_VER) 18067 18068 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 18069 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 18070 getLLVMStyleWithColumns(35)); 18071 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 18072 getLLVMStyleWithColumns(31)); 18073 verifyFormat("// Однажды в студёную зимнюю пору...", 18074 getLLVMStyleWithColumns(36)); 18075 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 18076 verifyFormat("/* Однажды в студёную зимнюю пору... */", 18077 getLLVMStyleWithColumns(39)); 18078 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 18079 getLLVMStyleWithColumns(35)); 18080 } 18081 18082 TEST_F(FormatTest, SplitsUTF8Strings) { 18083 // Non-printable characters' width is currently considered to be the length in 18084 // bytes in UTF8. The characters can be displayed in very different manner 18085 // (zero-width, single width with a substitution glyph, expanded to their code 18086 // (e.g. "<8d>"), so there's no single correct way to handle them. 18087 EXPECT_EQ("\"aaaaÄ\"\n" 18088 "\"\xc2\x8d\";", 18089 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 18090 EXPECT_EQ("\"aaaaaaaÄ\"\n" 18091 "\"\xc2\x8d\";", 18092 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 18093 EXPECT_EQ("\"Однажды, в \"\n" 18094 "\"студёную \"\n" 18095 "\"зимнюю \"\n" 18096 "\"пору,\"", 18097 format("\"Однажды, в студёную зимнюю пору,\"", 18098 getLLVMStyleWithColumns(13))); 18099 EXPECT_EQ( 18100 "\"一 二 三 \"\n" 18101 "\"四 五六 \"\n" 18102 "\"七 八 九 \"\n" 18103 "\"十\"", 18104 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 18105 EXPECT_EQ("\"一\t\"\n" 18106 "\"二 \t\"\n" 18107 "\"三 四 \"\n" 18108 "\"五\t\"\n" 18109 "\"六 \t\"\n" 18110 "\"七 \"\n" 18111 "\"八九十\tqq\"", 18112 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 18113 getLLVMStyleWithColumns(11))); 18114 18115 // UTF8 character in an escape sequence. 18116 EXPECT_EQ("\"aaaaaa\"\n" 18117 "\"\\\xC2\x8D\"", 18118 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 18119 } 18120 18121 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 18122 EXPECT_EQ("const char *sssss =\n" 18123 " \"一二三四五六七八\\\n" 18124 " 九 十\";", 18125 format("const char *sssss = \"一二三四五六七八\\\n" 18126 " 九 十\";", 18127 getLLVMStyleWithColumns(30))); 18128 } 18129 18130 TEST_F(FormatTest, SplitsUTF8LineComments) { 18131 EXPECT_EQ("// aaaaÄ\xc2\x8d", 18132 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 18133 EXPECT_EQ("// Я из лесу\n" 18134 "// вышел; был\n" 18135 "// сильный\n" 18136 "// мороз.", 18137 format("// Я из лесу вышел; был сильный мороз.", 18138 getLLVMStyleWithColumns(13))); 18139 EXPECT_EQ("// 一二三\n" 18140 "// 四五六七\n" 18141 "// 八 九\n" 18142 "// 十", 18143 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 18144 } 18145 18146 TEST_F(FormatTest, SplitsUTF8BlockComments) { 18147 EXPECT_EQ("/* Гляжу,\n" 18148 " * поднимается\n" 18149 " * медленно в\n" 18150 " * гору\n" 18151 " * Лошадка,\n" 18152 " * везущая\n" 18153 " * хворосту\n" 18154 " * воз. */", 18155 format("/* Гляжу, поднимается медленно в гору\n" 18156 " * Лошадка, везущая хворосту воз. */", 18157 getLLVMStyleWithColumns(13))); 18158 EXPECT_EQ( 18159 "/* 一二三\n" 18160 " * 四五六七\n" 18161 " * 八 九\n" 18162 " * 十 */", 18163 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 18164 EXPECT_EQ("/* \n" 18165 " * \n" 18166 " * - */", 18167 format("/* - */", getLLVMStyleWithColumns(12))); 18168 } 18169 18170 #endif // _MSC_VER 18171 18172 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 18173 FormatStyle Style = getLLVMStyle(); 18174 18175 Style.ConstructorInitializerIndentWidth = 4; 18176 verifyFormat( 18177 "SomeClass::Constructor()\n" 18178 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 18179 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 18180 Style); 18181 18182 Style.ConstructorInitializerIndentWidth = 2; 18183 verifyFormat( 18184 "SomeClass::Constructor()\n" 18185 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 18186 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 18187 Style); 18188 18189 Style.ConstructorInitializerIndentWidth = 0; 18190 verifyFormat( 18191 "SomeClass::Constructor()\n" 18192 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 18193 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 18194 Style); 18195 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 18196 verifyFormat( 18197 "SomeLongTemplateVariableName<\n" 18198 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 18199 Style); 18200 verifyFormat("bool smaller = 1 < " 18201 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 18202 " " 18203 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 18204 Style); 18205 18206 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 18207 verifyFormat("SomeClass::Constructor() :\n" 18208 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n" 18209 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}", 18210 Style); 18211 } 18212 18213 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 18214 FormatStyle Style = getLLVMStyle(); 18215 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 18216 Style.ConstructorInitializerIndentWidth = 4; 18217 verifyFormat("SomeClass::Constructor()\n" 18218 " : a(a)\n" 18219 " , b(b)\n" 18220 " , c(c) {}", 18221 Style); 18222 verifyFormat("SomeClass::Constructor()\n" 18223 " : a(a) {}", 18224 Style); 18225 18226 Style.ColumnLimit = 0; 18227 verifyFormat("SomeClass::Constructor()\n" 18228 " : a(a) {}", 18229 Style); 18230 verifyFormat("SomeClass::Constructor() noexcept\n" 18231 " : a(a) {}", 18232 Style); 18233 verifyFormat("SomeClass::Constructor()\n" 18234 " : a(a)\n" 18235 " , b(b)\n" 18236 " , c(c) {}", 18237 Style); 18238 verifyFormat("SomeClass::Constructor()\n" 18239 " : a(a) {\n" 18240 " foo();\n" 18241 " bar();\n" 18242 "}", 18243 Style); 18244 18245 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 18246 verifyFormat("SomeClass::Constructor()\n" 18247 " : a(a)\n" 18248 " , b(b)\n" 18249 " , c(c) {\n}", 18250 Style); 18251 verifyFormat("SomeClass::Constructor()\n" 18252 " : a(a) {\n}", 18253 Style); 18254 18255 Style.ColumnLimit = 80; 18256 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 18257 Style.ConstructorInitializerIndentWidth = 2; 18258 verifyFormat("SomeClass::Constructor()\n" 18259 " : a(a)\n" 18260 " , b(b)\n" 18261 " , c(c) {}", 18262 Style); 18263 18264 Style.ConstructorInitializerIndentWidth = 0; 18265 verifyFormat("SomeClass::Constructor()\n" 18266 ": a(a)\n" 18267 ", b(b)\n" 18268 ", c(c) {}", 18269 Style); 18270 18271 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 18272 Style.ConstructorInitializerIndentWidth = 4; 18273 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 18274 verifyFormat( 18275 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 18276 Style); 18277 verifyFormat( 18278 "SomeClass::Constructor()\n" 18279 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 18280 Style); 18281 Style.ConstructorInitializerIndentWidth = 4; 18282 Style.ColumnLimit = 60; 18283 verifyFormat("SomeClass::Constructor()\n" 18284 " : aaaaaaaa(aaaaaaaa)\n" 18285 " , aaaaaaaa(aaaaaaaa)\n" 18286 " , aaaaaaaa(aaaaaaaa) {}", 18287 Style); 18288 } 18289 18290 TEST_F(FormatTest, Destructors) { 18291 verifyFormat("void F(int &i) { i.~int(); }"); 18292 verifyFormat("void F(int &i) { i->~int(); }"); 18293 } 18294 18295 TEST_F(FormatTest, FormatsWithWebKitStyle) { 18296 FormatStyle Style = getWebKitStyle(); 18297 18298 // Don't indent in outer namespaces. 18299 verifyFormat("namespace outer {\n" 18300 "int i;\n" 18301 "namespace inner {\n" 18302 " int i;\n" 18303 "} // namespace inner\n" 18304 "} // namespace outer\n" 18305 "namespace other_outer {\n" 18306 "int i;\n" 18307 "}", 18308 Style); 18309 18310 // Don't indent case labels. 18311 verifyFormat("switch (variable) {\n" 18312 "case 1:\n" 18313 "case 2:\n" 18314 " doSomething();\n" 18315 " break;\n" 18316 "default:\n" 18317 " ++variable;\n" 18318 "}", 18319 Style); 18320 18321 // Wrap before binary operators. 18322 EXPECT_EQ("void f()\n" 18323 "{\n" 18324 " if (aaaaaaaaaaaaaaaa\n" 18325 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 18326 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 18327 " return;\n" 18328 "}", 18329 format("void f() {\n" 18330 "if (aaaaaaaaaaaaaaaa\n" 18331 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 18332 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 18333 "return;\n" 18334 "}", 18335 Style)); 18336 18337 // Allow functions on a single line. 18338 verifyFormat("void f() { return; }", Style); 18339 18340 // Allow empty blocks on a single line and insert a space in empty blocks. 18341 EXPECT_EQ("void f() { }", format("void f() {}", Style)); 18342 EXPECT_EQ("while (true) { }", format("while (true) {}", Style)); 18343 // However, don't merge non-empty short loops. 18344 EXPECT_EQ("while (true) {\n" 18345 " continue;\n" 18346 "}", 18347 format("while (true) { continue; }", Style)); 18348 18349 // Constructor initializers are formatted one per line with the "," on the 18350 // new line. 18351 verifyFormat("Constructor()\n" 18352 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 18353 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 18354 " aaaaaaaaaaaaaa)\n" 18355 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 18356 "{\n" 18357 "}", 18358 Style); 18359 verifyFormat("SomeClass::Constructor()\n" 18360 " : a(a)\n" 18361 "{\n" 18362 "}", 18363 Style); 18364 EXPECT_EQ("SomeClass::Constructor()\n" 18365 " : a(a)\n" 18366 "{\n" 18367 "}", 18368 format("SomeClass::Constructor():a(a){}", Style)); 18369 verifyFormat("SomeClass::Constructor()\n" 18370 " : a(a)\n" 18371 " , b(b)\n" 18372 " , c(c)\n" 18373 "{\n" 18374 "}", 18375 Style); 18376 verifyFormat("SomeClass::Constructor()\n" 18377 " : a(a)\n" 18378 "{\n" 18379 " foo();\n" 18380 " bar();\n" 18381 "}", 18382 Style); 18383 18384 // Access specifiers should be aligned left. 18385 verifyFormat("class C {\n" 18386 "public:\n" 18387 " int i;\n" 18388 "};", 18389 Style); 18390 18391 // Do not align comments. 18392 verifyFormat("int a; // Do not\n" 18393 "double b; // align comments.", 18394 Style); 18395 18396 // Do not align operands. 18397 EXPECT_EQ("ASSERT(aaaa\n" 18398 " || bbbb);", 18399 format("ASSERT ( aaaa\n||bbbb);", Style)); 18400 18401 // Accept input's line breaks. 18402 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 18403 " || bbbbbbbbbbbbbbb) {\n" 18404 " i++;\n" 18405 "}", 18406 format("if (aaaaaaaaaaaaaaa\n" 18407 "|| bbbbbbbbbbbbbbb) { i++; }", 18408 Style)); 18409 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 18410 " i++;\n" 18411 "}", 18412 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 18413 18414 // Don't automatically break all macro definitions (llvm.org/PR17842). 18415 verifyFormat("#define aNumber 10", Style); 18416 // However, generally keep the line breaks that the user authored. 18417 EXPECT_EQ("#define aNumber \\\n" 18418 " 10", 18419 format("#define aNumber \\\n" 18420 " 10", 18421 Style)); 18422 18423 // Keep empty and one-element array literals on a single line. 18424 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 18425 " copyItems:YES];", 18426 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 18427 "copyItems:YES];", 18428 Style)); 18429 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 18430 " copyItems:YES];", 18431 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 18432 " copyItems:YES];", 18433 Style)); 18434 // FIXME: This does not seem right, there should be more indentation before 18435 // the array literal's entries. Nested blocks have the same problem. 18436 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 18437 " @\"a\",\n" 18438 " @\"a\"\n" 18439 "]\n" 18440 " copyItems:YES];", 18441 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 18442 " @\"a\",\n" 18443 " @\"a\"\n" 18444 " ]\n" 18445 " copyItems:YES];", 18446 Style)); 18447 EXPECT_EQ( 18448 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 18449 " copyItems:YES];", 18450 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 18451 " copyItems:YES];", 18452 Style)); 18453 18454 verifyFormat("[self.a b:c c:d];", Style); 18455 EXPECT_EQ("[self.a b:c\n" 18456 " c:d];", 18457 format("[self.a b:c\n" 18458 "c:d];", 18459 Style)); 18460 } 18461 18462 TEST_F(FormatTest, FormatsLambdas) { 18463 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 18464 verifyFormat( 18465 "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();\n"); 18466 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 18467 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 18468 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 18469 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 18470 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 18471 verifyFormat("auto c = [a = [b = 42] {}] {};\n"); 18472 verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n"); 18473 verifyFormat("int x = f(*+[] {});"); 18474 verifyFormat("void f() {\n" 18475 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 18476 "}\n"); 18477 verifyFormat("void f() {\n" 18478 " other(x.begin(), //\n" 18479 " x.end(), //\n" 18480 " [&](int, int) { return 1; });\n" 18481 "}\n"); 18482 verifyFormat("void f() {\n" 18483 " other.other.other.other.other(\n" 18484 " x.begin(), x.end(),\n" 18485 " [something, rather](int, int, int, int, int, int, int) { " 18486 "return 1; });\n" 18487 "}\n"); 18488 verifyFormat( 18489 "void f() {\n" 18490 " other.other.other.other.other(\n" 18491 " x.begin(), x.end(),\n" 18492 " [something, rather](int, int, int, int, int, int, int) {\n" 18493 " //\n" 18494 " });\n" 18495 "}\n"); 18496 verifyFormat("SomeFunction([]() { // A cool function...\n" 18497 " return 43;\n" 18498 "});"); 18499 EXPECT_EQ("SomeFunction([]() {\n" 18500 "#define A a\n" 18501 " return 43;\n" 18502 "});", 18503 format("SomeFunction([](){\n" 18504 "#define A a\n" 18505 "return 43;\n" 18506 "});")); 18507 verifyFormat("void f() {\n" 18508 " SomeFunction([](decltype(x), A *a) {});\n" 18509 " SomeFunction([](typeof(x), A *a) {});\n" 18510 " SomeFunction([](_Atomic(x), A *a) {});\n" 18511 " SomeFunction([](__underlying_type(x), A *a) {});\n" 18512 "}"); 18513 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 18514 " [](const aaaaaaaaaa &a) { return a; });"); 18515 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 18516 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 18517 "});"); 18518 verifyFormat("Constructor()\n" 18519 " : Field([] { // comment\n" 18520 " int i;\n" 18521 " }) {}"); 18522 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 18523 " return some_parameter.size();\n" 18524 "};"); 18525 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 18526 " [](const string &s) { return s; };"); 18527 verifyFormat("int i = aaaaaa ? 1 //\n" 18528 " : [] {\n" 18529 " return 2; //\n" 18530 " }();"); 18531 verifyFormat("llvm::errs() << \"number of twos is \"\n" 18532 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 18533 " return x == 2; // force break\n" 18534 " });"); 18535 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 18536 " [=](int iiiiiiiiiiii) {\n" 18537 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n" 18538 " aaaaaaaaaaaaaaaaaaaaaaa;\n" 18539 " });", 18540 getLLVMStyleWithColumns(60)); 18541 18542 verifyFormat("SomeFunction({[&] {\n" 18543 " // comment\n" 18544 " },\n" 18545 " [&] {\n" 18546 " // comment\n" 18547 " }});"); 18548 verifyFormat("SomeFunction({[&] {\n" 18549 " // comment\n" 18550 "}});"); 18551 verifyFormat( 18552 "virtual aaaaaaaaaaaaaaaa(\n" 18553 " std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n" 18554 " aaaaa aaaaaaaaa);"); 18555 18556 // Lambdas with return types. 18557 verifyFormat("int c = []() -> int { return 2; }();\n"); 18558 verifyFormat("int c = []() -> int * { return 2; }();\n"); 18559 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 18560 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 18561 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 18562 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 18563 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 18564 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 18565 verifyFormat("[a, a]() -> a<1> {};"); 18566 verifyFormat("[]() -> foo<5 + 2> { return {}; };"); 18567 verifyFormat("[]() -> foo<5 - 2> { return {}; };"); 18568 verifyFormat("[]() -> foo<5 / 2> { return {}; };"); 18569 verifyFormat("[]() -> foo<5 * 2> { return {}; };"); 18570 verifyFormat("[]() -> foo<5 % 2> { return {}; };"); 18571 verifyFormat("[]() -> foo<5 << 2> { return {}; };"); 18572 verifyFormat("[]() -> foo<!5> { return {}; };"); 18573 verifyFormat("[]() -> foo<~5> { return {}; };"); 18574 verifyFormat("[]() -> foo<5 | 2> { return {}; };"); 18575 verifyFormat("[]() -> foo<5 || 2> { return {}; };"); 18576 verifyFormat("[]() -> foo<5 & 2> { return {}; };"); 18577 verifyFormat("[]() -> foo<5 && 2> { return {}; };"); 18578 verifyFormat("[]() -> foo<5 == 2> { return {}; };"); 18579 verifyFormat("[]() -> foo<5 != 2> { return {}; };"); 18580 verifyFormat("[]() -> foo<5 >= 2> { return {}; };"); 18581 verifyFormat("[]() -> foo<5 <= 2> { return {}; };"); 18582 verifyFormat("[]() -> foo<5 < 2> { return {}; };"); 18583 verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };"); 18584 verifyFormat("namespace bar {\n" 18585 "// broken:\n" 18586 "auto foo{[]() -> foo<5 + 2> { return {}; }};\n" 18587 "} // namespace bar"); 18588 verifyFormat("namespace bar {\n" 18589 "// broken:\n" 18590 "auto foo{[]() -> foo<5 - 2> { return {}; }};\n" 18591 "} // namespace bar"); 18592 verifyFormat("namespace bar {\n" 18593 "// broken:\n" 18594 "auto foo{[]() -> foo<5 / 2> { return {}; }};\n" 18595 "} // namespace bar"); 18596 verifyFormat("namespace bar {\n" 18597 "// broken:\n" 18598 "auto foo{[]() -> foo<5 * 2> { return {}; }};\n" 18599 "} // namespace bar"); 18600 verifyFormat("namespace bar {\n" 18601 "// broken:\n" 18602 "auto foo{[]() -> foo<5 % 2> { return {}; }};\n" 18603 "} // namespace bar"); 18604 verifyFormat("namespace bar {\n" 18605 "// broken:\n" 18606 "auto foo{[]() -> foo<5 << 2> { return {}; }};\n" 18607 "} // namespace bar"); 18608 verifyFormat("namespace bar {\n" 18609 "// broken:\n" 18610 "auto foo{[]() -> foo<!5> { return {}; }};\n" 18611 "} // namespace bar"); 18612 verifyFormat("namespace bar {\n" 18613 "// broken:\n" 18614 "auto foo{[]() -> foo<~5> { return {}; }};\n" 18615 "} // namespace bar"); 18616 verifyFormat("namespace bar {\n" 18617 "// broken:\n" 18618 "auto foo{[]() -> foo<5 | 2> { return {}; }};\n" 18619 "} // namespace bar"); 18620 verifyFormat("namespace bar {\n" 18621 "// broken:\n" 18622 "auto foo{[]() -> foo<5 || 2> { return {}; }};\n" 18623 "} // namespace bar"); 18624 verifyFormat("namespace bar {\n" 18625 "// broken:\n" 18626 "auto foo{[]() -> foo<5 & 2> { return {}; }};\n" 18627 "} // namespace bar"); 18628 verifyFormat("namespace bar {\n" 18629 "// broken:\n" 18630 "auto foo{[]() -> foo<5 && 2> { return {}; }};\n" 18631 "} // namespace bar"); 18632 verifyFormat("namespace bar {\n" 18633 "// broken:\n" 18634 "auto foo{[]() -> foo<5 == 2> { return {}; }};\n" 18635 "} // namespace bar"); 18636 verifyFormat("namespace bar {\n" 18637 "// broken:\n" 18638 "auto foo{[]() -> foo<5 != 2> { return {}; }};\n" 18639 "} // namespace bar"); 18640 verifyFormat("namespace bar {\n" 18641 "// broken:\n" 18642 "auto foo{[]() -> foo<5 >= 2> { return {}; }};\n" 18643 "} // namespace bar"); 18644 verifyFormat("namespace bar {\n" 18645 "// broken:\n" 18646 "auto foo{[]() -> foo<5 <= 2> { return {}; }};\n" 18647 "} // namespace bar"); 18648 verifyFormat("namespace bar {\n" 18649 "// broken:\n" 18650 "auto foo{[]() -> foo<5 < 2> { return {}; }};\n" 18651 "} // namespace bar"); 18652 verifyFormat("namespace bar {\n" 18653 "// broken:\n" 18654 "auto foo{[]() -> foo<2 ? 1 : 0> { return {}; }};\n" 18655 "} // namespace bar"); 18656 verifyFormat("[]() -> a<1> {};"); 18657 verifyFormat("[]() -> a<1> { ; };"); 18658 verifyFormat("[]() -> a<1> { ; }();"); 18659 verifyFormat("[a, a]() -> a<true> {};"); 18660 verifyFormat("[]() -> a<true> {};"); 18661 verifyFormat("[]() -> a<true> { ; };"); 18662 verifyFormat("[]() -> a<true> { ; }();"); 18663 verifyFormat("[a, a]() -> a<false> {};"); 18664 verifyFormat("[]() -> a<false> {};"); 18665 verifyFormat("[]() -> a<false> { ; };"); 18666 verifyFormat("[]() -> a<false> { ; }();"); 18667 verifyFormat("auto foo{[]() -> foo<false> { ; }};"); 18668 verifyFormat("namespace bar {\n" 18669 "auto foo{[]() -> foo<false> { ; }};\n" 18670 "} // namespace bar"); 18671 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 18672 " int j) -> int {\n" 18673 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 18674 "};"); 18675 verifyFormat( 18676 "aaaaaaaaaaaaaaaaaaaaaa(\n" 18677 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 18678 " return aaaaaaaaaaaaaaaaa;\n" 18679 " });", 18680 getLLVMStyleWithColumns(70)); 18681 verifyFormat("[]() //\n" 18682 " -> int {\n" 18683 " return 1; //\n" 18684 "};"); 18685 verifyFormat("[]() -> Void<T...> {};"); 18686 verifyFormat("[a, b]() -> Tuple<T...> { return {}; };"); 18687 18688 // Lambdas with explicit template argument lists. 18689 verifyFormat( 18690 "auto L = []<template <typename> class T, class U>(T<U> &&a) {};\n"); 18691 18692 // Multiple lambdas in the same parentheses change indentation rules. These 18693 // lambdas are forced to start on new lines. 18694 verifyFormat("SomeFunction(\n" 18695 " []() {\n" 18696 " //\n" 18697 " },\n" 18698 " []() {\n" 18699 " //\n" 18700 " });"); 18701 18702 // A lambda passed as arg0 is always pushed to the next line. 18703 verifyFormat("SomeFunction(\n" 18704 " [this] {\n" 18705 " //\n" 18706 " },\n" 18707 " 1);\n"); 18708 18709 // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like 18710 // the arg0 case above. 18711 auto Style = getGoogleStyle(); 18712 Style.BinPackArguments = false; 18713 verifyFormat("SomeFunction(\n" 18714 " a,\n" 18715 " [this] {\n" 18716 " //\n" 18717 " },\n" 18718 " b);\n", 18719 Style); 18720 verifyFormat("SomeFunction(\n" 18721 " a,\n" 18722 " [this] {\n" 18723 " //\n" 18724 " },\n" 18725 " b);\n"); 18726 18727 // A lambda with a very long line forces arg0 to be pushed out irrespective of 18728 // the BinPackArguments value (as long as the code is wide enough). 18729 verifyFormat( 18730 "something->SomeFunction(\n" 18731 " a,\n" 18732 " [this] {\n" 18733 " " 18734 "D0000000000000000000000000000000000000000000000000000000000001();\n" 18735 " },\n" 18736 " b);\n"); 18737 18738 // A multi-line lambda is pulled up as long as the introducer fits on the 18739 // previous line and there are no further args. 18740 verifyFormat("function(1, [this, that] {\n" 18741 " //\n" 18742 "});\n"); 18743 verifyFormat("function([this, that] {\n" 18744 " //\n" 18745 "});\n"); 18746 // FIXME: this format is not ideal and we should consider forcing the first 18747 // arg onto its own line. 18748 verifyFormat("function(a, b, c, //\n" 18749 " d, [this, that] {\n" 18750 " //\n" 18751 " });\n"); 18752 18753 // Multiple lambdas are treated correctly even when there is a short arg0. 18754 verifyFormat("SomeFunction(\n" 18755 " 1,\n" 18756 " [this] {\n" 18757 " //\n" 18758 " },\n" 18759 " [this] {\n" 18760 " //\n" 18761 " },\n" 18762 " 1);\n"); 18763 18764 // More complex introducers. 18765 verifyFormat("return [i, args...] {};"); 18766 18767 // Not lambdas. 18768 verifyFormat("constexpr char hello[]{\"hello\"};"); 18769 verifyFormat("double &operator[](int i) { return 0; }\n" 18770 "int i;"); 18771 verifyFormat("std::unique_ptr<int[]> foo() {}"); 18772 verifyFormat("int i = a[a][a]->f();"); 18773 verifyFormat("int i = (*b)[a]->f();"); 18774 18775 // Other corner cases. 18776 verifyFormat("void f() {\n" 18777 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 18778 " );\n" 18779 "}"); 18780 18781 // Lambdas created through weird macros. 18782 verifyFormat("void f() {\n" 18783 " MACRO((const AA &a) { return 1; });\n" 18784 " MACRO((AA &a) { return 1; });\n" 18785 "}"); 18786 18787 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 18788 " doo_dah();\n" 18789 " doo_dah();\n" 18790 " })) {\n" 18791 "}"); 18792 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n" 18793 " doo_dah();\n" 18794 " doo_dah();\n" 18795 " })) {\n" 18796 "}"); 18797 verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n" 18798 " doo_dah();\n" 18799 " doo_dah();\n" 18800 " })) {\n" 18801 "}"); 18802 verifyFormat("auto lambda = []() {\n" 18803 " int a = 2\n" 18804 "#if A\n" 18805 " + 2\n" 18806 "#endif\n" 18807 " ;\n" 18808 "};"); 18809 18810 // Lambdas with complex multiline introducers. 18811 verifyFormat( 18812 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 18813 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n" 18814 " -> ::std::unordered_set<\n" 18815 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 18816 " //\n" 18817 " });"); 18818 18819 FormatStyle DoNotMerge = getLLVMStyle(); 18820 DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 18821 verifyFormat("auto c = []() {\n" 18822 " return b;\n" 18823 "};", 18824 "auto c = []() { return b; };", DoNotMerge); 18825 verifyFormat("auto c = []() {\n" 18826 "};", 18827 " auto c = []() {};", DoNotMerge); 18828 18829 FormatStyle MergeEmptyOnly = getLLVMStyle(); 18830 MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty; 18831 verifyFormat("auto c = []() {\n" 18832 " return b;\n" 18833 "};", 18834 "auto c = []() {\n" 18835 " return b;\n" 18836 " };", 18837 MergeEmptyOnly); 18838 verifyFormat("auto c = []() {};", 18839 "auto c = []() {\n" 18840 "};", 18841 MergeEmptyOnly); 18842 18843 FormatStyle MergeInline = getLLVMStyle(); 18844 MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline; 18845 verifyFormat("auto c = []() {\n" 18846 " return b;\n" 18847 "};", 18848 "auto c = []() { return b; };", MergeInline); 18849 verifyFormat("function([]() { return b; })", "function([]() { return b; })", 18850 MergeInline); 18851 verifyFormat("function([]() { return b; }, a)", 18852 "function([]() { return b; }, a)", MergeInline); 18853 verifyFormat("function(a, []() { return b; })", 18854 "function(a, []() { return b; })", MergeInline); 18855 18856 // Check option "BraceWrapping.BeforeLambdaBody" and different state of 18857 // AllowShortLambdasOnASingleLine 18858 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle(); 18859 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 18860 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 18861 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 18862 FormatStyle::ShortLambdaStyle::SLS_None; 18863 verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n" 18864 " []()\n" 18865 " {\n" 18866 " return 17;\n" 18867 " });", 18868 LLVMWithBeforeLambdaBody); 18869 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n" 18870 " []()\n" 18871 " {\n" 18872 " });", 18873 LLVMWithBeforeLambdaBody); 18874 verifyFormat("auto fct_SLS_None = []()\n" 18875 "{\n" 18876 " return 17;\n" 18877 "};", 18878 LLVMWithBeforeLambdaBody); 18879 verifyFormat("TwoNestedLambdas_SLS_None(\n" 18880 " []()\n" 18881 " {\n" 18882 " return Call(\n" 18883 " []()\n" 18884 " {\n" 18885 " return 17;\n" 18886 " });\n" 18887 " });", 18888 LLVMWithBeforeLambdaBody); 18889 verifyFormat("void Fct()\n" 18890 "{\n" 18891 " return {[]()\n" 18892 " {\n" 18893 " return 17;\n" 18894 " }};\n" 18895 "}", 18896 LLVMWithBeforeLambdaBody); 18897 18898 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 18899 FormatStyle::ShortLambdaStyle::SLS_Empty; 18900 verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n" 18901 " []()\n" 18902 " {\n" 18903 " return 17;\n" 18904 " });", 18905 LLVMWithBeforeLambdaBody); 18906 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});", 18907 LLVMWithBeforeLambdaBody); 18908 verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL" 18909 "ongFunctionName_SLS_Empty(\n" 18910 " []() {});", 18911 LLVMWithBeforeLambdaBody); 18912 verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n" 18913 " []()\n" 18914 " {\n" 18915 " return 17;\n" 18916 " });", 18917 LLVMWithBeforeLambdaBody); 18918 verifyFormat("auto fct_SLS_Empty = []()\n" 18919 "{\n" 18920 " return 17;\n" 18921 "};", 18922 LLVMWithBeforeLambdaBody); 18923 verifyFormat("TwoNestedLambdas_SLS_Empty(\n" 18924 " []()\n" 18925 " {\n" 18926 " return Call([]() {});\n" 18927 " });", 18928 LLVMWithBeforeLambdaBody); 18929 verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n" 18930 " []()\n" 18931 " {\n" 18932 " return Call([]() {});\n" 18933 " });", 18934 LLVMWithBeforeLambdaBody); 18935 verifyFormat( 18936 "FctWithLongLineInLambda_SLS_Empty(\n" 18937 " []()\n" 18938 " {\n" 18939 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 18940 " AndShouldNotBeConsiderAsInline,\n" 18941 " LambdaBodyMustBeBreak);\n" 18942 " });", 18943 LLVMWithBeforeLambdaBody); 18944 18945 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 18946 FormatStyle::ShortLambdaStyle::SLS_Inline; 18947 verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });", 18948 LLVMWithBeforeLambdaBody); 18949 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});", 18950 LLVMWithBeforeLambdaBody); 18951 verifyFormat("auto fct_SLS_Inline = []()\n" 18952 "{\n" 18953 " return 17;\n" 18954 "};", 18955 LLVMWithBeforeLambdaBody); 18956 verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return " 18957 "17; }); });", 18958 LLVMWithBeforeLambdaBody); 18959 verifyFormat( 18960 "FctWithLongLineInLambda_SLS_Inline(\n" 18961 " []()\n" 18962 " {\n" 18963 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 18964 " AndShouldNotBeConsiderAsInline,\n" 18965 " LambdaBodyMustBeBreak);\n" 18966 " });", 18967 LLVMWithBeforeLambdaBody); 18968 verifyFormat("FctWithMultipleParams_SLS_Inline(" 18969 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n" 18970 " []() { return 17; });", 18971 LLVMWithBeforeLambdaBody); 18972 verifyFormat( 18973 "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });", 18974 LLVMWithBeforeLambdaBody); 18975 18976 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 18977 FormatStyle::ShortLambdaStyle::SLS_All; 18978 verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });", 18979 LLVMWithBeforeLambdaBody); 18980 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});", 18981 LLVMWithBeforeLambdaBody); 18982 verifyFormat("auto fct_SLS_All = []() { return 17; };", 18983 LLVMWithBeforeLambdaBody); 18984 verifyFormat("FctWithOneParam_SLS_All(\n" 18985 " []()\n" 18986 " {\n" 18987 " // A cool function...\n" 18988 " return 43;\n" 18989 " });", 18990 LLVMWithBeforeLambdaBody); 18991 verifyFormat("FctWithMultipleParams_SLS_All(" 18992 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n" 18993 " []() { return 17; });", 18994 LLVMWithBeforeLambdaBody); 18995 verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });", 18996 LLVMWithBeforeLambdaBody); 18997 verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });", 18998 LLVMWithBeforeLambdaBody); 18999 verifyFormat( 19000 "FctWithLongLineInLambda_SLS_All(\n" 19001 " []()\n" 19002 " {\n" 19003 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 19004 " AndShouldNotBeConsiderAsInline,\n" 19005 " LambdaBodyMustBeBreak);\n" 19006 " });", 19007 LLVMWithBeforeLambdaBody); 19008 verifyFormat( 19009 "auto fct_SLS_All = []()\n" 19010 "{\n" 19011 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 19012 " AndShouldNotBeConsiderAsInline,\n" 19013 " LambdaBodyMustBeBreak);\n" 19014 "};", 19015 LLVMWithBeforeLambdaBody); 19016 LLVMWithBeforeLambdaBody.BinPackParameters = false; 19017 verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);", 19018 LLVMWithBeforeLambdaBody); 19019 verifyFormat( 19020 "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n" 19021 " FirstParam,\n" 19022 " SecondParam,\n" 19023 " ThirdParam,\n" 19024 " FourthParam);", 19025 LLVMWithBeforeLambdaBody); 19026 verifyFormat("FctWithLongLineInLambda_SLS_All(\n" 19027 " []() { return " 19028 "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n" 19029 " FirstParam,\n" 19030 " SecondParam,\n" 19031 " ThirdParam,\n" 19032 " FourthParam);", 19033 LLVMWithBeforeLambdaBody); 19034 verifyFormat( 19035 "FctWithLongLineInLambda_SLS_All(FirstParam,\n" 19036 " SecondParam,\n" 19037 " ThirdParam,\n" 19038 " FourthParam,\n" 19039 " []() { return SomeValueNotSoLong; });", 19040 LLVMWithBeforeLambdaBody); 19041 verifyFormat("FctWithLongLineInLambda_SLS_All(\n" 19042 " []()\n" 19043 " {\n" 19044 " return " 19045 "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB" 19046 "eConsiderAsInline;\n" 19047 " });", 19048 LLVMWithBeforeLambdaBody); 19049 verifyFormat( 19050 "FctWithLongLineInLambda_SLS_All(\n" 19051 " []()\n" 19052 " {\n" 19053 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 19054 " AndShouldNotBeConsiderAsInline,\n" 19055 " LambdaBodyMustBeBreak);\n" 19056 " });", 19057 LLVMWithBeforeLambdaBody); 19058 verifyFormat("FctWithTwoParams_SLS_All(\n" 19059 " []()\n" 19060 " {\n" 19061 " // A cool function...\n" 19062 " return 43;\n" 19063 " },\n" 19064 " 87);", 19065 LLVMWithBeforeLambdaBody); 19066 verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);", 19067 LLVMWithBeforeLambdaBody); 19068 verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });", 19069 LLVMWithBeforeLambdaBody); 19070 verifyFormat( 19071 "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });", 19072 LLVMWithBeforeLambdaBody); 19073 verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; " 19074 "}); }, x);", 19075 LLVMWithBeforeLambdaBody); 19076 verifyFormat("TwoNestedLambdas_SLS_All(\n" 19077 " []()\n" 19078 " {\n" 19079 " // A cool function...\n" 19080 " return Call([]() { return 17; });\n" 19081 " });", 19082 LLVMWithBeforeLambdaBody); 19083 verifyFormat("TwoNestedLambdas_SLS_All(\n" 19084 " []()\n" 19085 " {\n" 19086 " return Call(\n" 19087 " []()\n" 19088 " {\n" 19089 " // A cool function...\n" 19090 " return 17;\n" 19091 " });\n" 19092 " });", 19093 LLVMWithBeforeLambdaBody); 19094 19095 // Lambdas with different indentation styles. 19096 Style = getLLVMStyleWithColumns(100); 19097 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 19098 " return promise.then(\n" 19099 " [this, &someVariable, someObject = " 19100 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 19101 " return someObject.startAsyncAction().then(\n" 19102 " [this, &someVariable](AsyncActionResult result) " 19103 "mutable { result.processMore(); });\n" 19104 " });\n" 19105 "}\n", 19106 format("SomeResult doSomething(SomeObject promise) {\n" 19107 " return promise.then([this, &someVariable, someObject = " 19108 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 19109 " return someObject.startAsyncAction().then([this, " 19110 "&someVariable](AsyncActionResult result) mutable {\n" 19111 " result.processMore();\n" 19112 " });\n" 19113 " });\n" 19114 "}\n", 19115 Style)); 19116 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope; 19117 verifyFormat("test() {\n" 19118 " ([]() -> {\n" 19119 " int b = 32;\n" 19120 " return 3;\n" 19121 " }).foo();\n" 19122 "}", 19123 Style); 19124 verifyFormat("test() {\n" 19125 " []() -> {\n" 19126 " int b = 32;\n" 19127 " return 3;\n" 19128 " }\n" 19129 "}", 19130 Style); 19131 verifyFormat("std::sort(v.begin(), v.end(),\n" 19132 " [](const auto &someLongArgumentName, const auto " 19133 "&someOtherLongArgumentName) {\n" 19134 " return someLongArgumentName.someMemberVariable < " 19135 "someOtherLongArgumentName.someMemberVariable;\n" 19136 "});", 19137 Style); 19138 verifyFormat("test() {\n" 19139 " (\n" 19140 " []() -> {\n" 19141 " int b = 32;\n" 19142 " return 3;\n" 19143 " },\n" 19144 " foo, bar)\n" 19145 " .foo();\n" 19146 "}", 19147 Style); 19148 verifyFormat("test() {\n" 19149 " ([]() -> {\n" 19150 " int b = 32;\n" 19151 " return 3;\n" 19152 " })\n" 19153 " .foo()\n" 19154 " .bar();\n" 19155 "}", 19156 Style); 19157 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 19158 " return promise.then(\n" 19159 " [this, &someVariable, someObject = " 19160 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 19161 " return someObject.startAsyncAction().then(\n" 19162 " [this, &someVariable](AsyncActionResult result) mutable { " 19163 "result.processMore(); });\n" 19164 " });\n" 19165 "}\n", 19166 format("SomeResult doSomething(SomeObject promise) {\n" 19167 " return promise.then([this, &someVariable, someObject = " 19168 "std::mv(s)](std::vector<int> evaluated) mutable {\n" 19169 " return someObject.startAsyncAction().then([this, " 19170 "&someVariable](AsyncActionResult result) mutable {\n" 19171 " result.processMore();\n" 19172 " });\n" 19173 " });\n" 19174 "}\n", 19175 Style)); 19176 EXPECT_EQ("SomeResult doSomething(SomeObject promise) {\n" 19177 " return promise.then([this, &someVariable] {\n" 19178 " return someObject.startAsyncAction().then(\n" 19179 " [this, &someVariable](AsyncActionResult result) mutable { " 19180 "result.processMore(); });\n" 19181 " });\n" 19182 "}\n", 19183 format("SomeResult doSomething(SomeObject promise) {\n" 19184 " return promise.then([this, &someVariable] {\n" 19185 " return someObject.startAsyncAction().then([this, " 19186 "&someVariable](AsyncActionResult result) mutable {\n" 19187 " result.processMore();\n" 19188 " });\n" 19189 " });\n" 19190 "}\n", 19191 Style)); 19192 Style = getGoogleStyle(); 19193 Style.LambdaBodyIndentation = FormatStyle::LBI_OuterScope; 19194 EXPECT_EQ("#define A \\\n" 19195 " [] { \\\n" 19196 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 19197 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 19198 " }", 19199 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 19200 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 19201 Style)); 19202 // TODO: The current formatting has a minor issue that's not worth fixing 19203 // right now whereby the closing brace is indented relative to the signature 19204 // instead of being aligned. This only happens with macros. 19205 } 19206 19207 TEST_F(FormatTest, LambdaWithLineComments) { 19208 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle(); 19209 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 19210 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 19211 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 19212 FormatStyle::ShortLambdaStyle::SLS_All; 19213 19214 verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody); 19215 verifyFormat("auto k = []() // comment\n" 19216 "{ return; }", 19217 LLVMWithBeforeLambdaBody); 19218 verifyFormat("auto k = []() /* comment */ { return; }", 19219 LLVMWithBeforeLambdaBody); 19220 verifyFormat("auto k = []() /* comment */ /* comment */ { return; }", 19221 LLVMWithBeforeLambdaBody); 19222 verifyFormat("auto k = []() // X\n" 19223 "{ return; }", 19224 LLVMWithBeforeLambdaBody); 19225 verifyFormat( 19226 "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n" 19227 "{ return; }", 19228 LLVMWithBeforeLambdaBody); 19229 } 19230 19231 TEST_F(FormatTest, EmptyLinesInLambdas) { 19232 verifyFormat("auto lambda = []() {\n" 19233 " x(); //\n" 19234 "};", 19235 "auto lambda = []() {\n" 19236 "\n" 19237 " x(); //\n" 19238 "\n" 19239 "};"); 19240 } 19241 19242 TEST_F(FormatTest, FormatsBlocks) { 19243 FormatStyle ShortBlocks = getLLVMStyle(); 19244 ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 19245 verifyFormat("int (^Block)(int, int);", ShortBlocks); 19246 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 19247 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 19248 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 19249 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 19250 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 19251 19252 verifyFormat("foo(^{ bar(); });", ShortBlocks); 19253 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 19254 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 19255 19256 verifyFormat("[operation setCompletionBlock:^{\n" 19257 " [self onOperationDone];\n" 19258 "}];"); 19259 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 19260 " [self onOperationDone];\n" 19261 "}]};"); 19262 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 19263 " f();\n" 19264 "}];"); 19265 verifyFormat("int a = [operation block:^int(int *i) {\n" 19266 " return 1;\n" 19267 "}];"); 19268 verifyFormat("[myObject doSomethingWith:arg1\n" 19269 " aaa:^int(int *a) {\n" 19270 " return 1;\n" 19271 " }\n" 19272 " bbb:f(a * bbbbbbbb)];"); 19273 19274 verifyFormat("[operation setCompletionBlock:^{\n" 19275 " [self.delegate newDataAvailable];\n" 19276 "}];", 19277 getLLVMStyleWithColumns(60)); 19278 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 19279 " NSString *path = [self sessionFilePath];\n" 19280 " if (path) {\n" 19281 " // ...\n" 19282 " }\n" 19283 "});"); 19284 verifyFormat("[[SessionService sharedService]\n" 19285 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 19286 " if (window) {\n" 19287 " [self windowDidLoad:window];\n" 19288 " } else {\n" 19289 " [self errorLoadingWindow];\n" 19290 " }\n" 19291 " }];"); 19292 verifyFormat("void (^largeBlock)(void) = ^{\n" 19293 " // ...\n" 19294 "};\n", 19295 getLLVMStyleWithColumns(40)); 19296 verifyFormat("[[SessionService sharedService]\n" 19297 " loadWindowWithCompletionBlock: //\n" 19298 " ^(SessionWindow *window) {\n" 19299 " if (window) {\n" 19300 " [self windowDidLoad:window];\n" 19301 " } else {\n" 19302 " [self errorLoadingWindow];\n" 19303 " }\n" 19304 " }];", 19305 getLLVMStyleWithColumns(60)); 19306 verifyFormat("[myObject doSomethingWith:arg1\n" 19307 " firstBlock:^(Foo *a) {\n" 19308 " // ...\n" 19309 " int i;\n" 19310 " }\n" 19311 " secondBlock:^(Bar *b) {\n" 19312 " // ...\n" 19313 " int i;\n" 19314 " }\n" 19315 " thirdBlock:^Foo(Bar *b) {\n" 19316 " // ...\n" 19317 " int i;\n" 19318 " }];"); 19319 verifyFormat("[myObject doSomethingWith:arg1\n" 19320 " firstBlock:-1\n" 19321 " secondBlock:^(Bar *b) {\n" 19322 " // ...\n" 19323 " int i;\n" 19324 " }];"); 19325 19326 verifyFormat("f(^{\n" 19327 " @autoreleasepool {\n" 19328 " if (a) {\n" 19329 " g();\n" 19330 " }\n" 19331 " }\n" 19332 "});"); 19333 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 19334 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" 19335 "};"); 19336 19337 FormatStyle FourIndent = getLLVMStyle(); 19338 FourIndent.ObjCBlockIndentWidth = 4; 19339 verifyFormat("[operation setCompletionBlock:^{\n" 19340 " [self onOperationDone];\n" 19341 "}];", 19342 FourIndent); 19343 } 19344 19345 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 19346 FormatStyle ZeroColumn = getLLVMStyle(); 19347 ZeroColumn.ColumnLimit = 0; 19348 19349 verifyFormat("[[SessionService sharedService] " 19350 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 19351 " if (window) {\n" 19352 " [self windowDidLoad:window];\n" 19353 " } else {\n" 19354 " [self errorLoadingWindow];\n" 19355 " }\n" 19356 "}];", 19357 ZeroColumn); 19358 EXPECT_EQ("[[SessionService sharedService]\n" 19359 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 19360 " if (window) {\n" 19361 " [self windowDidLoad:window];\n" 19362 " } else {\n" 19363 " [self errorLoadingWindow];\n" 19364 " }\n" 19365 " }];", 19366 format("[[SessionService sharedService]\n" 19367 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 19368 " if (window) {\n" 19369 " [self windowDidLoad:window];\n" 19370 " } else {\n" 19371 " [self errorLoadingWindow];\n" 19372 " }\n" 19373 "}];", 19374 ZeroColumn)); 19375 verifyFormat("[myObject doSomethingWith:arg1\n" 19376 " firstBlock:^(Foo *a) {\n" 19377 " // ...\n" 19378 " int i;\n" 19379 " }\n" 19380 " secondBlock:^(Bar *b) {\n" 19381 " // ...\n" 19382 " int i;\n" 19383 " }\n" 19384 " thirdBlock:^Foo(Bar *b) {\n" 19385 " // ...\n" 19386 " int i;\n" 19387 " }];", 19388 ZeroColumn); 19389 verifyFormat("f(^{\n" 19390 " @autoreleasepool {\n" 19391 " if (a) {\n" 19392 " g();\n" 19393 " }\n" 19394 " }\n" 19395 "});", 19396 ZeroColumn); 19397 verifyFormat("void (^largeBlock)(void) = ^{\n" 19398 " // ...\n" 19399 "};", 19400 ZeroColumn); 19401 19402 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 19403 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 19404 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 19405 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 19406 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 19407 " int i;\n" 19408 "};", 19409 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 19410 } 19411 19412 TEST_F(FormatTest, SupportsCRLF) { 19413 EXPECT_EQ("int a;\r\n" 19414 "int b;\r\n" 19415 "int c;\r\n", 19416 format("int a;\r\n" 19417 " int b;\r\n" 19418 " int c;\r\n", 19419 getLLVMStyle())); 19420 EXPECT_EQ("int a;\r\n" 19421 "int b;\r\n" 19422 "int c;\r\n", 19423 format("int a;\r\n" 19424 " int b;\n" 19425 " int c;\r\n", 19426 getLLVMStyle())); 19427 EXPECT_EQ("int a;\n" 19428 "int b;\n" 19429 "int c;\n", 19430 format("int a;\r\n" 19431 " int b;\n" 19432 " int c;\n", 19433 getLLVMStyle())); 19434 EXPECT_EQ("\"aaaaaaa \"\r\n" 19435 "\"bbbbbbb\";\r\n", 19436 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 19437 EXPECT_EQ("#define A \\\r\n" 19438 " b; \\\r\n" 19439 " c; \\\r\n" 19440 " d;\r\n", 19441 format("#define A \\\r\n" 19442 " b; \\\r\n" 19443 " c; d; \r\n", 19444 getGoogleStyle())); 19445 19446 EXPECT_EQ("/*\r\n" 19447 "multi line block comments\r\n" 19448 "should not introduce\r\n" 19449 "an extra carriage return\r\n" 19450 "*/\r\n", 19451 format("/*\r\n" 19452 "multi line block comments\r\n" 19453 "should not introduce\r\n" 19454 "an extra carriage return\r\n" 19455 "*/\r\n")); 19456 EXPECT_EQ("/*\r\n" 19457 "\r\n" 19458 "*/", 19459 format("/*\r\n" 19460 " \r\r\r\n" 19461 "*/")); 19462 19463 FormatStyle style = getLLVMStyle(); 19464 19465 style.DeriveLineEnding = true; 19466 style.UseCRLF = false; 19467 EXPECT_EQ("union FooBarBazQux {\n" 19468 " int foo;\n" 19469 " int bar;\n" 19470 " int baz;\n" 19471 "};", 19472 format("union FooBarBazQux {\r\n" 19473 " int foo;\n" 19474 " int bar;\r\n" 19475 " int baz;\n" 19476 "};", 19477 style)); 19478 style.UseCRLF = true; 19479 EXPECT_EQ("union FooBarBazQux {\r\n" 19480 " int foo;\r\n" 19481 " int bar;\r\n" 19482 " int baz;\r\n" 19483 "};", 19484 format("union FooBarBazQux {\r\n" 19485 " int foo;\n" 19486 " int bar;\r\n" 19487 " int baz;\n" 19488 "};", 19489 style)); 19490 19491 style.DeriveLineEnding = false; 19492 style.UseCRLF = false; 19493 EXPECT_EQ("union FooBarBazQux {\n" 19494 " int foo;\n" 19495 " int bar;\n" 19496 " int baz;\n" 19497 " int qux;\n" 19498 "};", 19499 format("union FooBarBazQux {\r\n" 19500 " int foo;\n" 19501 " int bar;\r\n" 19502 " int baz;\n" 19503 " int qux;\r\n" 19504 "};", 19505 style)); 19506 style.UseCRLF = true; 19507 EXPECT_EQ("union FooBarBazQux {\r\n" 19508 " int foo;\r\n" 19509 " int bar;\r\n" 19510 " int baz;\r\n" 19511 " int qux;\r\n" 19512 "};", 19513 format("union FooBarBazQux {\r\n" 19514 " int foo;\n" 19515 " int bar;\r\n" 19516 " int baz;\n" 19517 " int qux;\n" 19518 "};", 19519 style)); 19520 19521 style.DeriveLineEnding = true; 19522 style.UseCRLF = false; 19523 EXPECT_EQ("union FooBarBazQux {\r\n" 19524 " int foo;\r\n" 19525 " int bar;\r\n" 19526 " int baz;\r\n" 19527 " int qux;\r\n" 19528 "};", 19529 format("union FooBarBazQux {\r\n" 19530 " int foo;\n" 19531 " int bar;\r\n" 19532 " int baz;\n" 19533 " int qux;\r\n" 19534 "};", 19535 style)); 19536 style.UseCRLF = true; 19537 EXPECT_EQ("union FooBarBazQux {\n" 19538 " int foo;\n" 19539 " int bar;\n" 19540 " int baz;\n" 19541 " int qux;\n" 19542 "};", 19543 format("union FooBarBazQux {\r\n" 19544 " int foo;\n" 19545 " int bar;\r\n" 19546 " int baz;\n" 19547 " int qux;\n" 19548 "};", 19549 style)); 19550 } 19551 19552 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 19553 verifyFormat("MY_CLASS(C) {\n" 19554 " int i;\n" 19555 " int j;\n" 19556 "};"); 19557 } 19558 19559 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 19560 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 19561 TwoIndent.ContinuationIndentWidth = 2; 19562 19563 EXPECT_EQ("int i =\n" 19564 " longFunction(\n" 19565 " arg);", 19566 format("int i = longFunction(arg);", TwoIndent)); 19567 19568 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 19569 SixIndent.ContinuationIndentWidth = 6; 19570 19571 EXPECT_EQ("int i =\n" 19572 " longFunction(\n" 19573 " arg);", 19574 format("int i = longFunction(arg);", SixIndent)); 19575 } 19576 19577 TEST_F(FormatTest, WrappedClosingParenthesisIndent) { 19578 FormatStyle Style = getLLVMStyle(); 19579 verifyFormat("int Foo::getter(\n" 19580 " //\n" 19581 ") const {\n" 19582 " return foo;\n" 19583 "}", 19584 Style); 19585 verifyFormat("void Foo::setter(\n" 19586 " //\n" 19587 ") {\n" 19588 " foo = 1;\n" 19589 "}", 19590 Style); 19591 } 19592 19593 TEST_F(FormatTest, SpacesInAngles) { 19594 FormatStyle Spaces = getLLVMStyle(); 19595 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 19596 19597 verifyFormat("vector< ::std::string > x1;", Spaces); 19598 verifyFormat("Foo< int, Bar > x2;", Spaces); 19599 verifyFormat("Foo< ::int, ::Bar > x3;", Spaces); 19600 19601 verifyFormat("static_cast< int >(arg);", Spaces); 19602 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 19603 verifyFormat("f< int, float >();", Spaces); 19604 verifyFormat("template <> g() {}", Spaces); 19605 verifyFormat("template < std::vector< int > > f() {}", Spaces); 19606 verifyFormat("std::function< void(int, int) > fct;", Spaces); 19607 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 19608 Spaces); 19609 19610 Spaces.Standard = FormatStyle::LS_Cpp03; 19611 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 19612 verifyFormat("A< A< int > >();", Spaces); 19613 19614 Spaces.SpacesInAngles = FormatStyle::SIAS_Never; 19615 verifyFormat("A<A<int> >();", Spaces); 19616 19617 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave; 19618 verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;", 19619 Spaces); 19620 verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;", 19621 Spaces); 19622 19623 verifyFormat("A<A<int> >();", Spaces); 19624 verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces); 19625 verifyFormat("A< A< int > >();", Spaces); 19626 19627 Spaces.Standard = FormatStyle::LS_Cpp11; 19628 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 19629 verifyFormat("A< A< int > >();", Spaces); 19630 19631 Spaces.SpacesInAngles = FormatStyle::SIAS_Never; 19632 verifyFormat("vector<::std::string> x4;", Spaces); 19633 verifyFormat("vector<int> x5;", Spaces); 19634 verifyFormat("Foo<int, Bar> x6;", Spaces); 19635 verifyFormat("Foo<::int, ::Bar> x7;", Spaces); 19636 19637 verifyFormat("A<A<int>>();", Spaces); 19638 19639 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave; 19640 verifyFormat("vector<::std::string> x4;", Spaces); 19641 verifyFormat("vector< ::std::string > x4;", Spaces); 19642 verifyFormat("vector<int> x5;", Spaces); 19643 verifyFormat("vector< int > x5;", Spaces); 19644 verifyFormat("Foo<int, Bar> x6;", Spaces); 19645 verifyFormat("Foo< int, Bar > x6;", Spaces); 19646 verifyFormat("Foo<::int, ::Bar> x7;", Spaces); 19647 verifyFormat("Foo< ::int, ::Bar > x7;", Spaces); 19648 19649 verifyFormat("A<A<int>>();", Spaces); 19650 verifyFormat("A< A< int > >();", Spaces); 19651 verifyFormat("A<A<int > >();", Spaces); 19652 verifyFormat("A< A< int>>();", Spaces); 19653 } 19654 19655 TEST_F(FormatTest, SpaceAfterTemplateKeyword) { 19656 FormatStyle Style = getLLVMStyle(); 19657 Style.SpaceAfterTemplateKeyword = false; 19658 verifyFormat("template<int> void foo();", Style); 19659 } 19660 19661 TEST_F(FormatTest, TripleAngleBrackets) { 19662 verifyFormat("f<<<1, 1>>>();"); 19663 verifyFormat("f<<<1, 1, 1, s>>>();"); 19664 verifyFormat("f<<<a, b, c, d>>>();"); 19665 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 19666 verifyFormat("f<param><<<1, 1>>>();"); 19667 verifyFormat("f<1><<<1, 1>>>();"); 19668 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 19669 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 19670 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 19671 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n" 19672 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();"); 19673 } 19674 19675 TEST_F(FormatTest, MergeLessLessAtEnd) { 19676 verifyFormat("<<"); 19677 EXPECT_EQ("< < <", format("\\\n<<<")); 19678 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 19679 "aaallvm::outs() <<"); 19680 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 19681 "aaaallvm::outs()\n <<"); 19682 } 19683 19684 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 19685 std::string code = "#if A\n" 19686 "#if B\n" 19687 "a.\n" 19688 "#endif\n" 19689 " a = 1;\n" 19690 "#else\n" 19691 "#endif\n" 19692 "#if C\n" 19693 "#else\n" 19694 "#endif\n"; 19695 EXPECT_EQ(code, format(code)); 19696 } 19697 19698 TEST_F(FormatTest, HandleConflictMarkers) { 19699 // Git/SVN conflict markers. 19700 EXPECT_EQ("int a;\n" 19701 "void f() {\n" 19702 " callme(some(parameter1,\n" 19703 "<<<<<<< text by the vcs\n" 19704 " parameter2),\n" 19705 "||||||| text by the vcs\n" 19706 " parameter2),\n" 19707 " parameter3,\n" 19708 "======= text by the vcs\n" 19709 " parameter2, parameter3),\n" 19710 ">>>>>>> text by the vcs\n" 19711 " otherparameter);\n", 19712 format("int a;\n" 19713 "void f() {\n" 19714 " callme(some(parameter1,\n" 19715 "<<<<<<< text by the vcs\n" 19716 " parameter2),\n" 19717 "||||||| text by the vcs\n" 19718 " parameter2),\n" 19719 " parameter3,\n" 19720 "======= text by the vcs\n" 19721 " parameter2,\n" 19722 " parameter3),\n" 19723 ">>>>>>> text by the vcs\n" 19724 " otherparameter);\n")); 19725 19726 // Perforce markers. 19727 EXPECT_EQ("void f() {\n" 19728 " function(\n" 19729 ">>>> text by the vcs\n" 19730 " parameter,\n" 19731 "==== text by the vcs\n" 19732 " parameter,\n" 19733 "==== text by the vcs\n" 19734 " parameter,\n" 19735 "<<<< text by the vcs\n" 19736 " parameter);\n", 19737 format("void f() {\n" 19738 " function(\n" 19739 ">>>> text by the vcs\n" 19740 " parameter,\n" 19741 "==== text by the vcs\n" 19742 " parameter,\n" 19743 "==== text by the vcs\n" 19744 " parameter,\n" 19745 "<<<< text by the vcs\n" 19746 " parameter);\n")); 19747 19748 EXPECT_EQ("<<<<<<<\n" 19749 "|||||||\n" 19750 "=======\n" 19751 ">>>>>>>", 19752 format("<<<<<<<\n" 19753 "|||||||\n" 19754 "=======\n" 19755 ">>>>>>>")); 19756 19757 EXPECT_EQ("<<<<<<<\n" 19758 "|||||||\n" 19759 "int i;\n" 19760 "=======\n" 19761 ">>>>>>>", 19762 format("<<<<<<<\n" 19763 "|||||||\n" 19764 "int i;\n" 19765 "=======\n" 19766 ">>>>>>>")); 19767 19768 // FIXME: Handle parsing of macros around conflict markers correctly: 19769 EXPECT_EQ("#define Macro \\\n" 19770 "<<<<<<<\n" 19771 "Something \\\n" 19772 "|||||||\n" 19773 "Else \\\n" 19774 "=======\n" 19775 "Other \\\n" 19776 ">>>>>>>\n" 19777 " End int i;\n", 19778 format("#define Macro \\\n" 19779 "<<<<<<<\n" 19780 " Something \\\n" 19781 "|||||||\n" 19782 " Else \\\n" 19783 "=======\n" 19784 " Other \\\n" 19785 ">>>>>>>\n" 19786 " End\n" 19787 "int i;\n")); 19788 } 19789 19790 TEST_F(FormatTest, DisableRegions) { 19791 EXPECT_EQ("int i;\n" 19792 "// clang-format off\n" 19793 " int j;\n" 19794 "// clang-format on\n" 19795 "int k;", 19796 format(" int i;\n" 19797 " // clang-format off\n" 19798 " int j;\n" 19799 " // clang-format on\n" 19800 " int k;")); 19801 EXPECT_EQ("int i;\n" 19802 "/* clang-format off */\n" 19803 " int j;\n" 19804 "/* clang-format on */\n" 19805 "int k;", 19806 format(" int i;\n" 19807 " /* clang-format off */\n" 19808 " int j;\n" 19809 " /* clang-format on */\n" 19810 " int k;")); 19811 19812 // Don't reflow comments within disabled regions. 19813 EXPECT_EQ("// clang-format off\n" 19814 "// long long long long long long line\n" 19815 "/* clang-format on */\n" 19816 "/* long long long\n" 19817 " * long long long\n" 19818 " * line */\n" 19819 "int i;\n" 19820 "/* clang-format off */\n" 19821 "/* long long long long long long line */\n", 19822 format("// clang-format off\n" 19823 "// long long long long long long line\n" 19824 "/* clang-format on */\n" 19825 "/* long long long long long long line */\n" 19826 "int i;\n" 19827 "/* clang-format off */\n" 19828 "/* long long long long long long line */\n", 19829 getLLVMStyleWithColumns(20))); 19830 } 19831 19832 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 19833 format("? ) ="); 19834 verifyNoCrash("#define a\\\n /**/}"); 19835 } 19836 19837 TEST_F(FormatTest, FormatsTableGenCode) { 19838 FormatStyle Style = getLLVMStyle(); 19839 Style.Language = FormatStyle::LK_TableGen; 19840 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 19841 } 19842 19843 TEST_F(FormatTest, ArrayOfTemplates) { 19844 EXPECT_EQ("auto a = new unique_ptr<int>[10];", 19845 format("auto a = new unique_ptr<int > [ 10];")); 19846 19847 FormatStyle Spaces = getLLVMStyle(); 19848 Spaces.SpacesInSquareBrackets = true; 19849 EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];", 19850 format("auto a = new unique_ptr<int > [10];", Spaces)); 19851 } 19852 19853 TEST_F(FormatTest, ArrayAsTemplateType) { 19854 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;", 19855 format("auto a = unique_ptr < Foo < Bar>[ 10]> ;")); 19856 19857 FormatStyle Spaces = getLLVMStyle(); 19858 Spaces.SpacesInSquareBrackets = true; 19859 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;", 19860 format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces)); 19861 } 19862 19863 TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); } 19864 19865 TEST(FormatStyle, GetStyleWithEmptyFileName) { 19866 llvm::vfs::InMemoryFileSystem FS; 19867 auto Style1 = getStyle("file", "", "Google", "", &FS); 19868 ASSERT_TRUE((bool)Style1); 19869 ASSERT_EQ(*Style1, getGoogleStyle()); 19870 } 19871 19872 TEST(FormatStyle, GetStyleOfFile) { 19873 llvm::vfs::InMemoryFileSystem FS; 19874 // Test 1: format file in the same directory. 19875 ASSERT_TRUE( 19876 FS.addFile("/a/.clang-format", 0, 19877 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 19878 ASSERT_TRUE( 19879 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 19880 auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS); 19881 ASSERT_TRUE((bool)Style1); 19882 ASSERT_EQ(*Style1, getLLVMStyle()); 19883 19884 // Test 2.1: fallback to default. 19885 ASSERT_TRUE( 19886 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 19887 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS); 19888 ASSERT_TRUE((bool)Style2); 19889 ASSERT_EQ(*Style2, getMozillaStyle()); 19890 19891 // Test 2.2: no format on 'none' fallback style. 19892 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 19893 ASSERT_TRUE((bool)Style2); 19894 ASSERT_EQ(*Style2, getNoStyle()); 19895 19896 // Test 2.3: format if config is found with no based style while fallback is 19897 // 'none'. 19898 ASSERT_TRUE(FS.addFile("/b/.clang-format", 0, 19899 llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2"))); 19900 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 19901 ASSERT_TRUE((bool)Style2); 19902 ASSERT_EQ(*Style2, getLLVMStyle()); 19903 19904 // Test 2.4: format if yaml with no based style, while fallback is 'none'. 19905 Style2 = getStyle("{}", "a.h", "none", "", &FS); 19906 ASSERT_TRUE((bool)Style2); 19907 ASSERT_EQ(*Style2, getLLVMStyle()); 19908 19909 // Test 3: format file in parent directory. 19910 ASSERT_TRUE( 19911 FS.addFile("/c/.clang-format", 0, 19912 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 19913 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, 19914 llvm::MemoryBuffer::getMemBuffer("int i;"))); 19915 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS); 19916 ASSERT_TRUE((bool)Style3); 19917 ASSERT_EQ(*Style3, getGoogleStyle()); 19918 19919 // Test 4: error on invalid fallback style 19920 auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS); 19921 ASSERT_FALSE((bool)Style4); 19922 llvm::consumeError(Style4.takeError()); 19923 19924 // Test 5: error on invalid yaml on command line 19925 auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS); 19926 ASSERT_FALSE((bool)Style5); 19927 llvm::consumeError(Style5.takeError()); 19928 19929 // Test 6: error on invalid style 19930 auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS); 19931 ASSERT_FALSE((bool)Style6); 19932 llvm::consumeError(Style6.takeError()); 19933 19934 // Test 7: found config file, error on parsing it 19935 ASSERT_TRUE( 19936 FS.addFile("/d/.clang-format", 0, 19937 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n" 19938 "InvalidKey: InvalidValue"))); 19939 ASSERT_TRUE( 19940 FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 19941 auto Style7a = getStyle("file", "/d/.clang-format", "LLVM", "", &FS); 19942 ASSERT_FALSE((bool)Style7a); 19943 llvm::consumeError(Style7a.takeError()); 19944 19945 auto Style7b = getStyle("file", "/d/.clang-format", "LLVM", "", &FS, true); 19946 ASSERT_TRUE((bool)Style7b); 19947 19948 // Test 8: inferred per-language defaults apply. 19949 auto StyleTd = getStyle("file", "x.td", "llvm", "", &FS); 19950 ASSERT_TRUE((bool)StyleTd); 19951 ASSERT_EQ(*StyleTd, getLLVMStyle(FormatStyle::LK_TableGen)); 19952 19953 // Test 9.1: overwriting a file style, when parent no file exists with no 19954 // fallback style 19955 ASSERT_TRUE(FS.addFile( 19956 "/e/sub/.clang-format", 0, 19957 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: InheritParentConfig\n" 19958 "ColumnLimit: 20"))); 19959 ASSERT_TRUE(FS.addFile("/e/sub/code.cpp", 0, 19960 llvm::MemoryBuffer::getMemBuffer("int i;"))); 19961 auto Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS); 19962 ASSERT_TRUE(static_cast<bool>(Style9)); 19963 ASSERT_EQ(*Style9, [] { 19964 auto Style = getNoStyle(); 19965 Style.ColumnLimit = 20; 19966 return Style; 19967 }()); 19968 19969 // Test 9.2: with LLVM fallback style 19970 Style9 = getStyle("file", "/e/sub/code.cpp", "LLVM", "", &FS); 19971 ASSERT_TRUE(static_cast<bool>(Style9)); 19972 ASSERT_EQ(*Style9, [] { 19973 auto Style = getLLVMStyle(); 19974 Style.ColumnLimit = 20; 19975 return Style; 19976 }()); 19977 19978 // Test 9.3: with a parent file 19979 ASSERT_TRUE( 19980 FS.addFile("/e/.clang-format", 0, 19981 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google\n" 19982 "UseTab: Always"))); 19983 Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS); 19984 ASSERT_TRUE(static_cast<bool>(Style9)); 19985 ASSERT_EQ(*Style9, [] { 19986 auto Style = getGoogleStyle(); 19987 Style.ColumnLimit = 20; 19988 Style.UseTab = FormatStyle::UT_Always; 19989 return Style; 19990 }()); 19991 19992 // Test 9.4: propagate more than one level 19993 ASSERT_TRUE(FS.addFile("/e/sub/sub/code.cpp", 0, 19994 llvm::MemoryBuffer::getMemBuffer("int i;"))); 19995 ASSERT_TRUE(FS.addFile("/e/sub/sub/.clang-format", 0, 19996 llvm::MemoryBuffer::getMemBuffer( 19997 "BasedOnStyle: InheritParentConfig\n" 19998 "WhitespaceSensitiveMacros: ['FOO', 'BAR']"))); 19999 std::vector<std::string> NonDefaultWhiteSpaceMacros{"FOO", "BAR"}; 20000 20001 const auto SubSubStyle = [&NonDefaultWhiteSpaceMacros] { 20002 auto Style = getGoogleStyle(); 20003 Style.ColumnLimit = 20; 20004 Style.UseTab = FormatStyle::UT_Always; 20005 Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros; 20006 return Style; 20007 }(); 20008 20009 ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros); 20010 Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS); 20011 ASSERT_TRUE(static_cast<bool>(Style9)); 20012 ASSERT_EQ(*Style9, SubSubStyle); 20013 20014 // Test 9.5: use InheritParentConfig as style name 20015 Style9 = 20016 getStyle("inheritparentconfig", "/e/sub/sub/code.cpp", "none", "", &FS); 20017 ASSERT_TRUE(static_cast<bool>(Style9)); 20018 ASSERT_EQ(*Style9, SubSubStyle); 20019 20020 // Test 9.6: use command line style with inheritance 20021 Style9 = getStyle("{BasedOnStyle: InheritParentConfig}", "/e/sub/code.cpp", 20022 "none", "", &FS); 20023 ASSERT_TRUE(static_cast<bool>(Style9)); 20024 ASSERT_EQ(*Style9, SubSubStyle); 20025 20026 // Test 9.7: use command line style with inheritance and own config 20027 Style9 = getStyle("{BasedOnStyle: InheritParentConfig, " 20028 "WhitespaceSensitiveMacros: ['FOO', 'BAR']}", 20029 "/e/sub/code.cpp", "none", "", &FS); 20030 ASSERT_TRUE(static_cast<bool>(Style9)); 20031 ASSERT_EQ(*Style9, SubSubStyle); 20032 20033 // Test 9.8: use inheritance from a file without BasedOnStyle 20034 ASSERT_TRUE(FS.addFile("/e/withoutbase/.clang-format", 0, 20035 llvm::MemoryBuffer::getMemBuffer("ColumnLimit: 123"))); 20036 ASSERT_TRUE( 20037 FS.addFile("/e/withoutbase/sub/.clang-format", 0, 20038 llvm::MemoryBuffer::getMemBuffer( 20039 "BasedOnStyle: InheritParentConfig\nIndentWidth: 7"))); 20040 // Make sure we do not use the fallback style 20041 Style9 = getStyle("file", "/e/withoutbase/code.cpp", "google", "", &FS); 20042 ASSERT_TRUE(static_cast<bool>(Style9)); 20043 ASSERT_EQ(*Style9, [] { 20044 auto Style = getLLVMStyle(); 20045 Style.ColumnLimit = 123; 20046 return Style; 20047 }()); 20048 20049 Style9 = getStyle("file", "/e/withoutbase/sub/code.cpp", "google", "", &FS); 20050 ASSERT_TRUE(static_cast<bool>(Style9)); 20051 ASSERT_EQ(*Style9, [] { 20052 auto Style = getLLVMStyle(); 20053 Style.ColumnLimit = 123; 20054 Style.IndentWidth = 7; 20055 return Style; 20056 }()); 20057 } 20058 20059 TEST_F(ReplacementTest, FormatCodeAfterReplacements) { 20060 // Column limit is 20. 20061 std::string Code = "Type *a =\n" 20062 " new Type();\n" 20063 "g(iiiii, 0, jjjjj,\n" 20064 " 0, kkkkk, 0, mm);\n" 20065 "int bad = format ;"; 20066 std::string Expected = "auto a = new Type();\n" 20067 "g(iiiii, nullptr,\n" 20068 " jjjjj, nullptr,\n" 20069 " kkkkk, nullptr,\n" 20070 " mm);\n" 20071 "int bad = format ;"; 20072 FileID ID = Context.createInMemoryFile("format.cpp", Code); 20073 tooling::Replacements Replaces = toReplacements( 20074 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6, 20075 "auto "), 20076 tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1, 20077 "nullptr"), 20078 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1, 20079 "nullptr"), 20080 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1, 20081 "nullptr")}); 20082 20083 format::FormatStyle Style = format::getLLVMStyle(); 20084 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. 20085 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 20086 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 20087 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 20088 auto Result = applyAllReplacements(Code, *FormattedReplaces); 20089 EXPECT_TRUE(static_cast<bool>(Result)); 20090 EXPECT_EQ(Expected, *Result); 20091 } 20092 20093 TEST_F(ReplacementTest, SortIncludesAfterReplacement) { 20094 std::string Code = "#include \"a.h\"\n" 20095 "#include \"c.h\"\n" 20096 "\n" 20097 "int main() {\n" 20098 " return 0;\n" 20099 "}"; 20100 std::string Expected = "#include \"a.h\"\n" 20101 "#include \"b.h\"\n" 20102 "#include \"c.h\"\n" 20103 "\n" 20104 "int main() {\n" 20105 " return 0;\n" 20106 "}"; 20107 FileID ID = Context.createInMemoryFile("fix.cpp", Code); 20108 tooling::Replacements Replaces = toReplacements( 20109 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0, 20110 "#include \"b.h\"\n")}); 20111 20112 format::FormatStyle Style = format::getLLVMStyle(); 20113 Style.SortIncludes = FormatStyle::SI_CaseSensitive; 20114 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 20115 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 20116 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 20117 auto Result = applyAllReplacements(Code, *FormattedReplaces); 20118 EXPECT_TRUE(static_cast<bool>(Result)); 20119 EXPECT_EQ(Expected, *Result); 20120 } 20121 20122 TEST_F(FormatTest, FormatSortsUsingDeclarations) { 20123 EXPECT_EQ("using std::cin;\n" 20124 "using std::cout;", 20125 format("using std::cout;\n" 20126 "using std::cin;", 20127 getGoogleStyle())); 20128 } 20129 20130 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) { 20131 format::FormatStyle Style = format::getLLVMStyle(); 20132 Style.Standard = FormatStyle::LS_Cpp03; 20133 // cpp03 recognize this string as identifier u8 and literal character 'a' 20134 EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style)); 20135 } 20136 20137 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) { 20138 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers 20139 // all modes, including C++11, C++14 and C++17 20140 EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';")); 20141 } 20142 20143 TEST_F(FormatTest, DoNotFormatLikelyXml) { 20144 EXPECT_EQ("<!-- ;> -->", format("<!-- ;> -->", getGoogleStyle())); 20145 EXPECT_EQ(" <!-- >; -->", format(" <!-- >; -->", getGoogleStyle())); 20146 } 20147 20148 TEST_F(FormatTest, StructuredBindings) { 20149 // Structured bindings is a C++17 feature. 20150 // all modes, including C++11, C++14 and C++17 20151 verifyFormat("auto [a, b] = f();"); 20152 EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();")); 20153 EXPECT_EQ("const auto [a, b] = f();", format("const auto[a, b] = f();")); 20154 EXPECT_EQ("auto const [a, b] = f();", format("auto const[a, b] = f();")); 20155 EXPECT_EQ("auto const volatile [a, b] = f();", 20156 format("auto const volatile[a, b] = f();")); 20157 EXPECT_EQ("auto [a, b, c] = f();", format("auto [ a , b,c ] = f();")); 20158 EXPECT_EQ("auto &[a, b, c] = f();", 20159 format("auto &[ a , b,c ] = f();")); 20160 EXPECT_EQ("auto &&[a, b, c] = f();", 20161 format("auto &&[ a , b,c ] = f();")); 20162 EXPECT_EQ("auto const &[a, b] = f();", format("auto const&[a, b] = f();")); 20163 EXPECT_EQ("auto const volatile &&[a, b] = f();", 20164 format("auto const volatile &&[a, b] = f();")); 20165 EXPECT_EQ("auto const &&[a, b] = f();", 20166 format("auto const && [a, b] = f();")); 20167 EXPECT_EQ("const auto &[a, b] = f();", 20168 format("const auto & [a, b] = f();")); 20169 EXPECT_EQ("const auto volatile &&[a, b] = f();", 20170 format("const auto volatile &&[a, b] = f();")); 20171 EXPECT_EQ("volatile const auto &&[a, b] = f();", 20172 format("volatile const auto &&[a, b] = f();")); 20173 EXPECT_EQ("const auto &&[a, b] = f();", 20174 format("const auto && [a, b] = f();")); 20175 20176 // Make sure we don't mistake structured bindings for lambdas. 20177 FormatStyle PointerMiddle = getLLVMStyle(); 20178 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 20179 verifyFormat("auto [a1, b]{A * i};", getGoogleStyle()); 20180 verifyFormat("auto [a2, b]{A * i};", getLLVMStyle()); 20181 verifyFormat("auto [a3, b]{A * i};", PointerMiddle); 20182 verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle()); 20183 verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle()); 20184 verifyFormat("auto const [a3, b]{A * i};", PointerMiddle); 20185 verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle()); 20186 verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle()); 20187 verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle); 20188 verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle()); 20189 verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle()); 20190 verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle); 20191 20192 EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}", 20193 format("for (const auto && [a, b] : some_range) {\n}")); 20194 EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}", 20195 format("for (const auto & [a, b] : some_range) {\n}")); 20196 EXPECT_EQ("for (const auto [a, b] : some_range) {\n}", 20197 format("for (const auto[a, b] : some_range) {\n}")); 20198 EXPECT_EQ("auto [x, y](expr);", format("auto[x,y] (expr);")); 20199 EXPECT_EQ("auto &[x, y](expr);", format("auto & [x,y] (expr);")); 20200 EXPECT_EQ("auto &&[x, y](expr);", format("auto && [x,y] (expr);")); 20201 EXPECT_EQ("auto const &[x, y](expr);", 20202 format("auto const & [x,y] (expr);")); 20203 EXPECT_EQ("auto const &&[x, y](expr);", 20204 format("auto const && [x,y] (expr);")); 20205 EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y] {expr};")); 20206 EXPECT_EQ("auto const &[x, y]{expr};", 20207 format("auto const & [x,y] {expr};")); 20208 EXPECT_EQ("auto const &&[x, y]{expr};", 20209 format("auto const && [x,y] {expr};")); 20210 20211 format::FormatStyle Spaces = format::getLLVMStyle(); 20212 Spaces.SpacesInSquareBrackets = true; 20213 verifyFormat("auto [ a, b ] = f();", Spaces); 20214 verifyFormat("auto &&[ a, b ] = f();", Spaces); 20215 verifyFormat("auto &[ a, b ] = f();", Spaces); 20216 verifyFormat("auto const &&[ a, b ] = f();", Spaces); 20217 verifyFormat("auto const &[ a, b ] = f();", Spaces); 20218 } 20219 20220 TEST_F(FormatTest, FileAndCode) { 20221 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", "")); 20222 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", "")); 20223 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", "")); 20224 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "")); 20225 EXPECT_EQ(FormatStyle::LK_ObjC, 20226 guessLanguage("foo.h", "@interface Foo\n@end\n")); 20227 EXPECT_EQ( 20228 FormatStyle::LK_ObjC, 20229 guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }")); 20230 EXPECT_EQ(FormatStyle::LK_ObjC, 20231 guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))")); 20232 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;")); 20233 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", "")); 20234 EXPECT_EQ(FormatStyle::LK_ObjC, 20235 guessLanguage("foo", "@interface Foo\n@end\n")); 20236 EXPECT_EQ(FormatStyle::LK_ObjC, 20237 guessLanguage("foo.h", "int DoStuff(CGRect rect);\n")); 20238 EXPECT_EQ( 20239 FormatStyle::LK_ObjC, 20240 guessLanguage("foo.h", 20241 "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n")); 20242 EXPECT_EQ( 20243 FormatStyle::LK_Cpp, 20244 guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;")); 20245 } 20246 20247 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) { 20248 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];")); 20249 EXPECT_EQ(FormatStyle::LK_ObjC, 20250 guessLanguage("foo.h", "array[[calculator getIndex]];")); 20251 EXPECT_EQ(FormatStyle::LK_Cpp, 20252 guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];")); 20253 EXPECT_EQ( 20254 FormatStyle::LK_Cpp, 20255 guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];")); 20256 EXPECT_EQ(FormatStyle::LK_ObjC, 20257 guessLanguage("foo.h", "[[noreturn foo] bar];")); 20258 EXPECT_EQ(FormatStyle::LK_Cpp, 20259 guessLanguage("foo.h", "[[clang::fallthrough]];")); 20260 EXPECT_EQ(FormatStyle::LK_ObjC, 20261 guessLanguage("foo.h", "[[clang:fallthrough] foo];")); 20262 EXPECT_EQ(FormatStyle::LK_Cpp, 20263 guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];")); 20264 EXPECT_EQ(FormatStyle::LK_Cpp, 20265 guessLanguage("foo.h", "[[using clang: fallthrough]];")); 20266 EXPECT_EQ(FormatStyle::LK_ObjC, 20267 guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];")); 20268 EXPECT_EQ(FormatStyle::LK_Cpp, 20269 guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];")); 20270 EXPECT_EQ( 20271 FormatStyle::LK_Cpp, 20272 guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)")); 20273 EXPECT_EQ( 20274 FormatStyle::LK_Cpp, 20275 guessLanguage("foo.h", 20276 "[[clang::callable_when(\"unconsumed\", \"unknown\")]]")); 20277 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]")); 20278 } 20279 20280 TEST_F(FormatTest, GuessLanguageWithCaret) { 20281 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);")); 20282 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);")); 20283 EXPECT_EQ(FormatStyle::LK_ObjC, 20284 guessLanguage("foo.h", "int(^)(char, float);")); 20285 EXPECT_EQ(FormatStyle::LK_ObjC, 20286 guessLanguage("foo.h", "int(^foo)(char, float);")); 20287 EXPECT_EQ(FormatStyle::LK_ObjC, 20288 guessLanguage("foo.h", "int(^foo[10])(char, float);")); 20289 EXPECT_EQ(FormatStyle::LK_ObjC, 20290 guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);")); 20291 EXPECT_EQ( 20292 FormatStyle::LK_ObjC, 20293 guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);")); 20294 } 20295 20296 TEST_F(FormatTest, GuessLanguageWithPragmas) { 20297 EXPECT_EQ(FormatStyle::LK_Cpp, 20298 guessLanguage("foo.h", "__pragma(warning(disable:))")); 20299 EXPECT_EQ(FormatStyle::LK_Cpp, 20300 guessLanguage("foo.h", "#pragma(warning(disable:))")); 20301 EXPECT_EQ(FormatStyle::LK_Cpp, 20302 guessLanguage("foo.h", "_Pragma(warning(disable:))")); 20303 } 20304 20305 TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) { 20306 // ASM symbolic names are identifiers that must be surrounded by [] without 20307 // space in between: 20308 // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands 20309 20310 // Example from https://bugs.llvm.org/show_bug.cgi?id=45108. 20311 verifyFormat(R"(// 20312 asm volatile("mrs %x[result], FPCR" : [result] "=r"(result)); 20313 )"); 20314 20315 // A list of several ASM symbolic names. 20316 verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)"); 20317 20318 // ASM symbolic names in inline ASM with inputs and outputs. 20319 verifyFormat(R"(// 20320 asm("cmoveq %1, %2, %[result]" 20321 : [result] "=r"(result) 20322 : "r"(test), "r"(new), "[result]"(old)); 20323 )"); 20324 20325 // ASM symbolic names in inline ASM with no outputs. 20326 verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)"); 20327 } 20328 20329 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) { 20330 EXPECT_EQ(FormatStyle::LK_Cpp, 20331 guessLanguage("foo.h", "void f() {\n" 20332 " asm (\"mov %[e], %[d]\"\n" 20333 " : [d] \"=rm\" (d)\n" 20334 " [e] \"rm\" (*e));\n" 20335 "}")); 20336 EXPECT_EQ(FormatStyle::LK_Cpp, 20337 guessLanguage("foo.h", "void f() {\n" 20338 " _asm (\"mov %[e], %[d]\"\n" 20339 " : [d] \"=rm\" (d)\n" 20340 " [e] \"rm\" (*e));\n" 20341 "}")); 20342 EXPECT_EQ(FormatStyle::LK_Cpp, 20343 guessLanguage("foo.h", "void f() {\n" 20344 " __asm (\"mov %[e], %[d]\"\n" 20345 " : [d] \"=rm\" (d)\n" 20346 " [e] \"rm\" (*e));\n" 20347 "}")); 20348 EXPECT_EQ(FormatStyle::LK_Cpp, 20349 guessLanguage("foo.h", "void f() {\n" 20350 " __asm__ (\"mov %[e], %[d]\"\n" 20351 " : [d] \"=rm\" (d)\n" 20352 " [e] \"rm\" (*e));\n" 20353 "}")); 20354 EXPECT_EQ(FormatStyle::LK_Cpp, 20355 guessLanguage("foo.h", "void f() {\n" 20356 " asm (\"mov %[e], %[d]\"\n" 20357 " : [d] \"=rm\" (d),\n" 20358 " [e] \"rm\" (*e));\n" 20359 "}")); 20360 EXPECT_EQ(FormatStyle::LK_Cpp, 20361 guessLanguage("foo.h", "void f() {\n" 20362 " asm volatile (\"mov %[e], %[d]\"\n" 20363 " : [d] \"=rm\" (d)\n" 20364 " [e] \"rm\" (*e));\n" 20365 "}")); 20366 } 20367 20368 TEST_F(FormatTest, GuessLanguageWithChildLines) { 20369 EXPECT_EQ(FormatStyle::LK_Cpp, 20370 guessLanguage("foo.h", "#define FOO ({ std::string s; })")); 20371 EXPECT_EQ(FormatStyle::LK_ObjC, 20372 guessLanguage("foo.h", "#define FOO ({ NSString *s; })")); 20373 EXPECT_EQ( 20374 FormatStyle::LK_Cpp, 20375 guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })")); 20376 EXPECT_EQ( 20377 FormatStyle::LK_ObjC, 20378 guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })")); 20379 } 20380 20381 TEST_F(FormatTest, TypenameMacros) { 20382 std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"}; 20383 20384 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353 20385 FormatStyle Google = getGoogleStyleWithColumns(0); 20386 Google.TypenameMacros = TypenameMacros; 20387 verifyFormat("struct foo {\n" 20388 " int bar;\n" 20389 " TAILQ_ENTRY(a) bleh;\n" 20390 "};", 20391 Google); 20392 20393 FormatStyle Macros = getLLVMStyle(); 20394 Macros.TypenameMacros = TypenameMacros; 20395 20396 verifyFormat("STACK_OF(int) a;", Macros); 20397 verifyFormat("STACK_OF(int) *a;", Macros); 20398 verifyFormat("STACK_OF(int const *) *a;", Macros); 20399 verifyFormat("STACK_OF(int *const) *a;", Macros); 20400 verifyFormat("STACK_OF(int, string) a;", Macros); 20401 verifyFormat("STACK_OF(LIST(int)) a;", Macros); 20402 verifyFormat("STACK_OF(LIST(int)) a, b;", Macros); 20403 verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros); 20404 verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros); 20405 verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros); 20406 verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros); 20407 20408 Macros.PointerAlignment = FormatStyle::PAS_Left; 20409 verifyFormat("STACK_OF(int)* a;", Macros); 20410 verifyFormat("STACK_OF(int*)* a;", Macros); 20411 verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros); 20412 verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros); 20413 verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros); 20414 } 20415 20416 TEST_F(FormatTest, AtomicQualifier) { 20417 // Check that we treate _Atomic as a type and not a function call 20418 FormatStyle Google = getGoogleStyleWithColumns(0); 20419 verifyFormat("struct foo {\n" 20420 " int a1;\n" 20421 " _Atomic(a) a2;\n" 20422 " _Atomic(_Atomic(int) *const) a3;\n" 20423 "};", 20424 Google); 20425 verifyFormat("_Atomic(uint64_t) a;"); 20426 verifyFormat("_Atomic(uint64_t) *a;"); 20427 verifyFormat("_Atomic(uint64_t const *) *a;"); 20428 verifyFormat("_Atomic(uint64_t *const) *a;"); 20429 verifyFormat("_Atomic(const uint64_t *) *a;"); 20430 verifyFormat("_Atomic(uint64_t) a;"); 20431 verifyFormat("_Atomic(_Atomic(uint64_t)) a;"); 20432 verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;"); 20433 verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}"); 20434 verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);"); 20435 20436 verifyFormat("_Atomic(uint64_t) *s(InitValue);"); 20437 verifyFormat("_Atomic(uint64_t) *s{InitValue};"); 20438 FormatStyle Style = getLLVMStyle(); 20439 Style.PointerAlignment = FormatStyle::PAS_Left; 20440 verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style); 20441 verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style); 20442 verifyFormat("_Atomic(int)* a;", Style); 20443 verifyFormat("_Atomic(int*)* a;", Style); 20444 verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style); 20445 20446 Style.SpacesInCStyleCastParentheses = true; 20447 Style.SpacesInParentheses = false; 20448 verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style); 20449 Style.SpacesInCStyleCastParentheses = false; 20450 Style.SpacesInParentheses = true; 20451 verifyFormat("x = (_Atomic( uint64_t ))*a;", Style); 20452 verifyFormat("x = (_Atomic( uint64_t ))&a;", Style); 20453 } 20454 20455 TEST_F(FormatTest, AmbersandInLamda) { 20456 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899 20457 FormatStyle AlignStyle = getLLVMStyle(); 20458 AlignStyle.PointerAlignment = FormatStyle::PAS_Left; 20459 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle); 20460 AlignStyle.PointerAlignment = FormatStyle::PAS_Right; 20461 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle); 20462 } 20463 20464 TEST_F(FormatTest, SpacesInConditionalStatement) { 20465 FormatStyle Spaces = getLLVMStyle(); 20466 Spaces.SpacesInConditionalStatement = true; 20467 verifyFormat("for ( int i = 0; i; i++ )\n continue;", Spaces); 20468 verifyFormat("if ( !a )\n return;", Spaces); 20469 verifyFormat("if ( a )\n return;", Spaces); 20470 verifyFormat("if constexpr ( a )\n return;", Spaces); 20471 verifyFormat("switch ( a )\ncase 1:\n return;", Spaces); 20472 verifyFormat("while ( a )\n return;", Spaces); 20473 verifyFormat("while ( (a && b) )\n return;", Spaces); 20474 verifyFormat("do {\n} while ( 1 != 0 );", Spaces); 20475 verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces); 20476 // Check that space on the left of "::" is inserted as expected at beginning 20477 // of condition. 20478 verifyFormat("while ( ::func() )\n return;", Spaces); 20479 } 20480 20481 TEST_F(FormatTest, AlternativeOperators) { 20482 // Test case for ensuring alternate operators are not 20483 // combined with their right most neighbour. 20484 verifyFormat("int a and b;"); 20485 verifyFormat("int a and_eq b;"); 20486 verifyFormat("int a bitand b;"); 20487 verifyFormat("int a bitor b;"); 20488 verifyFormat("int a compl b;"); 20489 verifyFormat("int a not b;"); 20490 verifyFormat("int a not_eq b;"); 20491 verifyFormat("int a or b;"); 20492 verifyFormat("int a xor b;"); 20493 verifyFormat("int a xor_eq b;"); 20494 verifyFormat("return this not_eq bitand other;"); 20495 verifyFormat("bool operator not_eq(const X bitand other)"); 20496 20497 verifyFormat("int a and 5;"); 20498 verifyFormat("int a and_eq 5;"); 20499 verifyFormat("int a bitand 5;"); 20500 verifyFormat("int a bitor 5;"); 20501 verifyFormat("int a compl 5;"); 20502 verifyFormat("int a not 5;"); 20503 verifyFormat("int a not_eq 5;"); 20504 verifyFormat("int a or 5;"); 20505 verifyFormat("int a xor 5;"); 20506 verifyFormat("int a xor_eq 5;"); 20507 20508 verifyFormat("int a compl(5);"); 20509 verifyFormat("int a not(5);"); 20510 20511 /* FIXME handle alternate tokens 20512 * https://en.cppreference.com/w/cpp/language/operator_alternative 20513 // alternative tokens 20514 verifyFormat("compl foo();"); // ~foo(); 20515 verifyFormat("foo() <%%>;"); // foo(); 20516 verifyFormat("void foo() <%%>;"); // void foo(){} 20517 verifyFormat("int a <:1:>;"); // int a[1];[ 20518 verifyFormat("%:define ABC abc"); // #define ABC abc 20519 verifyFormat("%:%:"); // ## 20520 */ 20521 } 20522 20523 TEST_F(FormatTest, STLWhileNotDefineChed) { 20524 verifyFormat("#if defined(while)\n" 20525 "#define while EMIT WARNING C4005\n" 20526 "#endif // while"); 20527 } 20528 20529 TEST_F(FormatTest, OperatorSpacing) { 20530 FormatStyle Style = getLLVMStyle(); 20531 Style.PointerAlignment = FormatStyle::PAS_Right; 20532 verifyFormat("Foo::operator*();", Style); 20533 verifyFormat("Foo::operator void *();", Style); 20534 verifyFormat("Foo::operator void **();", Style); 20535 verifyFormat("Foo::operator void *&();", Style); 20536 verifyFormat("Foo::operator void *&&();", Style); 20537 verifyFormat("Foo::operator void const *();", Style); 20538 verifyFormat("Foo::operator void const **();", Style); 20539 verifyFormat("Foo::operator void const *&();", Style); 20540 verifyFormat("Foo::operator void const *&&();", Style); 20541 verifyFormat("Foo::operator()(void *);", Style); 20542 verifyFormat("Foo::operator*(void *);", Style); 20543 verifyFormat("Foo::operator*();", Style); 20544 verifyFormat("Foo::operator**();", Style); 20545 verifyFormat("Foo::operator&();", Style); 20546 verifyFormat("Foo::operator<int> *();", Style); 20547 verifyFormat("Foo::operator<Foo> *();", Style); 20548 verifyFormat("Foo::operator<int> **();", Style); 20549 verifyFormat("Foo::operator<Foo> **();", Style); 20550 verifyFormat("Foo::operator<int> &();", Style); 20551 verifyFormat("Foo::operator<Foo> &();", Style); 20552 verifyFormat("Foo::operator<int> &&();", Style); 20553 verifyFormat("Foo::operator<Foo> &&();", Style); 20554 verifyFormat("Foo::operator<int> *&();", Style); 20555 verifyFormat("Foo::operator<Foo> *&();", Style); 20556 verifyFormat("Foo::operator<int> *&&();", Style); 20557 verifyFormat("Foo::operator<Foo> *&&();", Style); 20558 verifyFormat("operator*(int (*)(), class Foo);", Style); 20559 20560 verifyFormat("Foo::operator&();", Style); 20561 verifyFormat("Foo::operator void &();", Style); 20562 verifyFormat("Foo::operator void const &();", Style); 20563 verifyFormat("Foo::operator()(void &);", Style); 20564 verifyFormat("Foo::operator&(void &);", Style); 20565 verifyFormat("Foo::operator&();", Style); 20566 verifyFormat("operator&(int (&)(), class Foo);", Style); 20567 20568 verifyFormat("Foo::operator&&();", Style); 20569 verifyFormat("Foo::operator**();", Style); 20570 verifyFormat("Foo::operator void &&();", Style); 20571 verifyFormat("Foo::operator void const &&();", Style); 20572 verifyFormat("Foo::operator()(void &&);", Style); 20573 verifyFormat("Foo::operator&&(void &&);", Style); 20574 verifyFormat("Foo::operator&&();", Style); 20575 verifyFormat("operator&&(int(&&)(), class Foo);", Style); 20576 verifyFormat("operator const nsTArrayRight<E> &()", Style); 20577 verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()", 20578 Style); 20579 verifyFormat("operator void **()", Style); 20580 verifyFormat("operator const FooRight<Object> &()", Style); 20581 verifyFormat("operator const FooRight<Object> *()", Style); 20582 verifyFormat("operator const FooRight<Object> **()", Style); 20583 verifyFormat("operator const FooRight<Object> *&()", Style); 20584 verifyFormat("operator const FooRight<Object> *&&()", Style); 20585 20586 Style.PointerAlignment = FormatStyle::PAS_Left; 20587 verifyFormat("Foo::operator*();", Style); 20588 verifyFormat("Foo::operator**();", Style); 20589 verifyFormat("Foo::operator void*();", Style); 20590 verifyFormat("Foo::operator void**();", Style); 20591 verifyFormat("Foo::operator void*&();", Style); 20592 verifyFormat("Foo::operator void*&&();", Style); 20593 verifyFormat("Foo::operator void const*();", Style); 20594 verifyFormat("Foo::operator void const**();", Style); 20595 verifyFormat("Foo::operator void const*&();", Style); 20596 verifyFormat("Foo::operator void const*&&();", Style); 20597 verifyFormat("Foo::operator/*comment*/ void*();", Style); 20598 verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style); 20599 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style); 20600 verifyFormat("Foo::operator()(void*);", Style); 20601 verifyFormat("Foo::operator*(void*);", Style); 20602 verifyFormat("Foo::operator*();", Style); 20603 verifyFormat("Foo::operator<int>*();", Style); 20604 verifyFormat("Foo::operator<Foo>*();", Style); 20605 verifyFormat("Foo::operator<int>**();", Style); 20606 verifyFormat("Foo::operator<Foo>**();", Style); 20607 verifyFormat("Foo::operator<Foo>*&();", Style); 20608 verifyFormat("Foo::operator<int>&();", Style); 20609 verifyFormat("Foo::operator<Foo>&();", Style); 20610 verifyFormat("Foo::operator<int>&&();", Style); 20611 verifyFormat("Foo::operator<Foo>&&();", Style); 20612 verifyFormat("Foo::operator<int>*&();", Style); 20613 verifyFormat("Foo::operator<Foo>*&();", Style); 20614 verifyFormat("operator*(int (*)(), class Foo);", Style); 20615 20616 verifyFormat("Foo::operator&();", Style); 20617 verifyFormat("Foo::operator void&();", Style); 20618 verifyFormat("Foo::operator void const&();", Style); 20619 verifyFormat("Foo::operator/*comment*/ void&();", Style); 20620 verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style); 20621 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style); 20622 verifyFormat("Foo::operator()(void&);", Style); 20623 verifyFormat("Foo::operator&(void&);", Style); 20624 verifyFormat("Foo::operator&();", Style); 20625 verifyFormat("operator&(int (&)(), class Foo);", Style); 20626 20627 verifyFormat("Foo::operator&&();", Style); 20628 verifyFormat("Foo::operator void&&();", Style); 20629 verifyFormat("Foo::operator void const&&();", Style); 20630 verifyFormat("Foo::operator/*comment*/ void&&();", Style); 20631 verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style); 20632 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style); 20633 verifyFormat("Foo::operator()(void&&);", Style); 20634 verifyFormat("Foo::operator&&(void&&);", Style); 20635 verifyFormat("Foo::operator&&();", Style); 20636 verifyFormat("operator&&(int(&&)(), class Foo);", Style); 20637 verifyFormat("operator const nsTArrayLeft<E>&()", Style); 20638 verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()", 20639 Style); 20640 verifyFormat("operator void**()", Style); 20641 verifyFormat("operator const FooLeft<Object>&()", Style); 20642 verifyFormat("operator const FooLeft<Object>*()", Style); 20643 verifyFormat("operator const FooLeft<Object>**()", Style); 20644 verifyFormat("operator const FooLeft<Object>*&()", Style); 20645 verifyFormat("operator const FooLeft<Object>*&&()", Style); 20646 20647 // PR45107 20648 verifyFormat("operator Vector<String>&();", Style); 20649 verifyFormat("operator const Vector<String>&();", Style); 20650 verifyFormat("operator foo::Bar*();", Style); 20651 verifyFormat("operator const Foo<X>::Bar<Y>*();", Style); 20652 verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();", 20653 Style); 20654 20655 Style.PointerAlignment = FormatStyle::PAS_Middle; 20656 verifyFormat("Foo::operator*();", Style); 20657 verifyFormat("Foo::operator void *();", Style); 20658 verifyFormat("Foo::operator()(void *);", Style); 20659 verifyFormat("Foo::operator*(void *);", Style); 20660 verifyFormat("Foo::operator*();", Style); 20661 verifyFormat("operator*(int (*)(), class Foo);", Style); 20662 20663 verifyFormat("Foo::operator&();", Style); 20664 verifyFormat("Foo::operator void &();", Style); 20665 verifyFormat("Foo::operator void const &();", Style); 20666 verifyFormat("Foo::operator()(void &);", Style); 20667 verifyFormat("Foo::operator&(void &);", Style); 20668 verifyFormat("Foo::operator&();", Style); 20669 verifyFormat("operator&(int (&)(), class Foo);", Style); 20670 20671 verifyFormat("Foo::operator&&();", Style); 20672 verifyFormat("Foo::operator void &&();", Style); 20673 verifyFormat("Foo::operator void const &&();", Style); 20674 verifyFormat("Foo::operator()(void &&);", Style); 20675 verifyFormat("Foo::operator&&(void &&);", Style); 20676 verifyFormat("Foo::operator&&();", Style); 20677 verifyFormat("operator&&(int(&&)(), class Foo);", Style); 20678 } 20679 20680 TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) { 20681 FormatStyle Style = getLLVMStyle(); 20682 // PR46157 20683 verifyFormat("foo(operator+, -42);", Style); 20684 verifyFormat("foo(operator++, -42);", Style); 20685 verifyFormat("foo(operator--, -42);", Style); 20686 verifyFormat("foo(-42, operator--);", Style); 20687 verifyFormat("foo(-42, operator, );", Style); 20688 verifyFormat("foo(operator, , -42);", Style); 20689 } 20690 20691 TEST_F(FormatTest, WhitespaceSensitiveMacros) { 20692 FormatStyle Style = getLLVMStyle(); 20693 Style.WhitespaceSensitiveMacros.push_back("FOO"); 20694 20695 // Don't use the helpers here, since 'mess up' will change the whitespace 20696 // and these are all whitespace sensitive by definition 20697 EXPECT_EQ("FOO(String-ized&Messy+But(: :Still)=Intentional);", 20698 format("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style)); 20699 EXPECT_EQ( 20700 "FOO(String-ized&Messy+But\\(: :Still)=Intentional);", 20701 format("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style)); 20702 EXPECT_EQ("FOO(String-ized&Messy+But,: :Still=Intentional);", 20703 format("FOO(String-ized&Messy+But,: :Still=Intentional);", Style)); 20704 EXPECT_EQ("FOO(String-ized&Messy+But,: :\n" 20705 " Still=Intentional);", 20706 format("FOO(String-ized&Messy+But,: :\n" 20707 " Still=Intentional);", 20708 Style)); 20709 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 20710 EXPECT_EQ("FOO(String-ized=&Messy+But,: :\n" 20711 " Still=Intentional);", 20712 format("FOO(String-ized=&Messy+But,: :\n" 20713 " Still=Intentional);", 20714 Style)); 20715 20716 Style.ColumnLimit = 21; 20717 EXPECT_EQ("FOO(String-ized&Messy+But: :Still=Intentional);", 20718 format("FOO(String-ized&Messy+But: :Still=Intentional);", Style)); 20719 } 20720 20721 TEST_F(FormatTest, VeryLongNamespaceCommentSplit) { 20722 // These tests are not in NamespaceFixer because that doesn't 20723 // test its interaction with line wrapping 20724 FormatStyle Style = getLLVMStyle(); 20725 Style.ColumnLimit = 80; 20726 verifyFormat("namespace {\n" 20727 "int i;\n" 20728 "int j;\n" 20729 "} // namespace", 20730 Style); 20731 20732 verifyFormat("namespace AAA {\n" 20733 "int i;\n" 20734 "int j;\n" 20735 "} // namespace AAA", 20736 Style); 20737 20738 EXPECT_EQ("namespace Averyveryveryverylongnamespace {\n" 20739 "int i;\n" 20740 "int j;\n" 20741 "} // namespace Averyveryveryverylongnamespace", 20742 format("namespace Averyveryveryverylongnamespace {\n" 20743 "int i;\n" 20744 "int j;\n" 20745 "}", 20746 Style)); 20747 20748 EXPECT_EQ( 20749 "namespace " 20750 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" 20751 " went::mad::now {\n" 20752 "int i;\n" 20753 "int j;\n" 20754 "} // namespace\n" 20755 " // " 20756 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" 20757 "went::mad::now", 20758 format("namespace " 20759 "would::it::save::you::a::lot::of::time::if_::i::" 20760 "just::gave::up::and_::went::mad::now {\n" 20761 "int i;\n" 20762 "int j;\n" 20763 "}", 20764 Style)); 20765 20766 // This used to duplicate the comment again and again on subsequent runs 20767 EXPECT_EQ( 20768 "namespace " 20769 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" 20770 " went::mad::now {\n" 20771 "int i;\n" 20772 "int j;\n" 20773 "} // namespace\n" 20774 " // " 20775 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" 20776 "went::mad::now", 20777 format("namespace " 20778 "would::it::save::you::a::lot::of::time::if_::i::" 20779 "just::gave::up::and_::went::mad::now {\n" 20780 "int i;\n" 20781 "int j;\n" 20782 "} // namespace\n" 20783 " // " 20784 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::" 20785 "and_::went::mad::now", 20786 Style)); 20787 } 20788 20789 TEST_F(FormatTest, LikelyUnlikely) { 20790 FormatStyle Style = getLLVMStyle(); 20791 20792 verifyFormat("if (argc > 5) [[unlikely]] {\n" 20793 " return 29;\n" 20794 "}", 20795 Style); 20796 20797 verifyFormat("if (argc > 5) [[likely]] {\n" 20798 " return 29;\n" 20799 "}", 20800 Style); 20801 20802 verifyFormat("if (argc > 5) [[unlikely]] {\n" 20803 " return 29;\n" 20804 "} else [[likely]] {\n" 20805 " return 42;\n" 20806 "}\n", 20807 Style); 20808 20809 verifyFormat("if (argc > 5) [[unlikely]] {\n" 20810 " return 29;\n" 20811 "} else if (argc > 10) [[likely]] {\n" 20812 " return 99;\n" 20813 "} else {\n" 20814 " return 42;\n" 20815 "}\n", 20816 Style); 20817 20818 verifyFormat("if (argc > 5) [[gnu::unused]] {\n" 20819 " return 29;\n" 20820 "}", 20821 Style); 20822 } 20823 20824 TEST_F(FormatTest, PenaltyIndentedWhitespace) { 20825 verifyFormat("Constructor()\n" 20826 " : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 20827 " aaaa(aaaaaaaaaaaaaaaaaa, " 20828 "aaaaaaaaaaaaaaaaaat))"); 20829 verifyFormat("Constructor()\n" 20830 " : aaaaaaaaaaaaa(aaaaaa), " 20831 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)"); 20832 20833 FormatStyle StyleWithWhitespacePenalty = getLLVMStyle(); 20834 StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5; 20835 verifyFormat("Constructor()\n" 20836 " : aaaaaa(aaaaaa),\n" 20837 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 20838 " aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))", 20839 StyleWithWhitespacePenalty); 20840 verifyFormat("Constructor()\n" 20841 " : aaaaaaaaaaaaa(aaaaaa), " 20842 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)", 20843 StyleWithWhitespacePenalty); 20844 } 20845 20846 TEST_F(FormatTest, LLVMDefaultStyle) { 20847 FormatStyle Style = getLLVMStyle(); 20848 verifyFormat("extern \"C\" {\n" 20849 "int foo();\n" 20850 "}", 20851 Style); 20852 } 20853 TEST_F(FormatTest, GNUDefaultStyle) { 20854 FormatStyle Style = getGNUStyle(); 20855 verifyFormat("extern \"C\"\n" 20856 "{\n" 20857 " int foo ();\n" 20858 "}", 20859 Style); 20860 } 20861 TEST_F(FormatTest, MozillaDefaultStyle) { 20862 FormatStyle Style = getMozillaStyle(); 20863 verifyFormat("extern \"C\"\n" 20864 "{\n" 20865 " int foo();\n" 20866 "}", 20867 Style); 20868 } 20869 TEST_F(FormatTest, GoogleDefaultStyle) { 20870 FormatStyle Style = getGoogleStyle(); 20871 verifyFormat("extern \"C\" {\n" 20872 "int foo();\n" 20873 "}", 20874 Style); 20875 } 20876 TEST_F(FormatTest, ChromiumDefaultStyle) { 20877 FormatStyle Style = getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp); 20878 verifyFormat("extern \"C\" {\n" 20879 "int foo();\n" 20880 "}", 20881 Style); 20882 } 20883 TEST_F(FormatTest, MicrosoftDefaultStyle) { 20884 FormatStyle Style = getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp); 20885 verifyFormat("extern \"C\"\n" 20886 "{\n" 20887 " int foo();\n" 20888 "}", 20889 Style); 20890 } 20891 TEST_F(FormatTest, WebKitDefaultStyle) { 20892 FormatStyle Style = getWebKitStyle(); 20893 verifyFormat("extern \"C\" {\n" 20894 "int foo();\n" 20895 "}", 20896 Style); 20897 } 20898 20899 TEST_F(FormatTest, ConceptsAndRequires) { 20900 FormatStyle Style = getLLVMStyle(); 20901 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 20902 20903 verifyFormat("template <typename T>\n" 20904 "concept Hashable = requires(T a) {\n" 20905 " { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n" 20906 "};", 20907 Style); 20908 verifyFormat("template <typename T>\n" 20909 "concept EqualityComparable = requires(T a, T b) {\n" 20910 " { a == b } -> bool;\n" 20911 "};", 20912 Style); 20913 verifyFormat("template <typename T>\n" 20914 "concept EqualityComparable = requires(T a, T b) {\n" 20915 " { a == b } -> bool;\n" 20916 " { a != b } -> bool;\n" 20917 "};", 20918 Style); 20919 verifyFormat("template <typename T>\n" 20920 "concept EqualityComparable = requires(T a, T b) {\n" 20921 " { a == b } -> bool;\n" 20922 " { a != b } -> bool;\n" 20923 "};", 20924 Style); 20925 20926 verifyFormat("template <typename It>\n" 20927 "requires Iterator<It>\n" 20928 "void sort(It begin, It end) {\n" 20929 " //....\n" 20930 "}", 20931 Style); 20932 20933 verifyFormat("template <typename T>\n" 20934 "concept Large = sizeof(T) > 10;", 20935 Style); 20936 20937 verifyFormat("template <typename T, typename U>\n" 20938 "concept FooableWith = requires(T t, U u) {\n" 20939 " typename T::foo_type;\n" 20940 " { t.foo(u) } -> typename T::foo_type;\n" 20941 " t++;\n" 20942 "};\n" 20943 "void doFoo(FooableWith<int> auto t) {\n" 20944 " t.foo(3);\n" 20945 "}", 20946 Style); 20947 verifyFormat("template <typename T>\n" 20948 "concept Context = sizeof(T) == 1;", 20949 Style); 20950 verifyFormat("template <typename T>\n" 20951 "concept Context = is_specialization_of_v<context, T>;", 20952 Style); 20953 verifyFormat("template <typename T>\n" 20954 "concept Node = std::is_object_v<T>;", 20955 Style); 20956 verifyFormat("template <typename T>\n" 20957 "concept Tree = true;", 20958 Style); 20959 20960 verifyFormat("template <typename T> int g(T i) requires Concept1<I> {\n" 20961 " //...\n" 20962 "}", 20963 Style); 20964 20965 verifyFormat( 20966 "template <typename T> int g(T i) requires Concept1<I> && Concept2<I> {\n" 20967 " //...\n" 20968 "}", 20969 Style); 20970 20971 verifyFormat( 20972 "template <typename T> int g(T i) requires Concept1<I> || Concept2<I> {\n" 20973 " //...\n" 20974 "}", 20975 Style); 20976 20977 verifyFormat("template <typename T>\n" 20978 "veryveryvery_long_return_type g(T i) requires Concept1<I> || " 20979 "Concept2<I> {\n" 20980 " //...\n" 20981 "}", 20982 Style); 20983 20984 verifyFormat("template <typename T>\n" 20985 "veryveryvery_long_return_type g(T i) requires Concept1<I> && " 20986 "Concept2<I> {\n" 20987 " //...\n" 20988 "}", 20989 Style); 20990 20991 verifyFormat( 20992 "template <typename T>\n" 20993 "veryveryvery_long_return_type g(T i) requires Concept1 && Concept2 {\n" 20994 " //...\n" 20995 "}", 20996 Style); 20997 20998 verifyFormat( 20999 "template <typename T>\n" 21000 "veryveryvery_long_return_type g(T i) requires Concept1 || Concept2 {\n" 21001 " //...\n" 21002 "}", 21003 Style); 21004 21005 verifyFormat("template <typename It>\n" 21006 "requires Foo<It>() && Bar<It> {\n" 21007 " //....\n" 21008 "}", 21009 Style); 21010 21011 verifyFormat("template <typename It>\n" 21012 "requires Foo<Bar<It>>() && Bar<Foo<It, It>> {\n" 21013 " //....\n" 21014 "}", 21015 Style); 21016 21017 verifyFormat("template <typename It>\n" 21018 "requires Foo<Bar<It, It>>() && Bar<Foo<It, It>> {\n" 21019 " //....\n" 21020 "}", 21021 Style); 21022 21023 verifyFormat( 21024 "template <typename It>\n" 21025 "requires Foo<Bar<It>, Baz<It>>() && Bar<Foo<It>, Baz<It, It>> {\n" 21026 " //....\n" 21027 "}", 21028 Style); 21029 21030 Style.IndentRequires = true; 21031 verifyFormat("template <typename It>\n" 21032 " requires Iterator<It>\n" 21033 "void sort(It begin, It end) {\n" 21034 " //....\n" 21035 "}", 21036 Style); 21037 verifyFormat("template <std::size index_>\n" 21038 " requires(index_ < sizeof...(Children_))\n" 21039 "Tree auto &child() {\n" 21040 " // ...\n" 21041 "}", 21042 Style); 21043 21044 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 21045 verifyFormat("template <typename T>\n" 21046 "concept Hashable = requires (T a) {\n" 21047 " { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n" 21048 "};", 21049 Style); 21050 21051 verifyFormat("template <class T = void>\n" 21052 " requires EqualityComparable<T> || Same<T, void>\n" 21053 "struct equal_to;", 21054 Style); 21055 21056 verifyFormat("template <class T>\n" 21057 " requires requires {\n" 21058 " T{};\n" 21059 " T (int);\n" 21060 " }\n", 21061 Style); 21062 21063 Style.ColumnLimit = 78; 21064 verifyFormat("template <typename T>\n" 21065 "concept Context = Traits<typename T::traits_type> and\n" 21066 " Interface<typename T::interface_type> and\n" 21067 " Request<typename T::request_type> and\n" 21068 " Response<typename T::response_type> and\n" 21069 " ContextExtension<typename T::extension_type> and\n" 21070 " ::std::is_copy_constructable<T> and " 21071 "::std::is_move_constructable<T> and\n" 21072 " requires (T c) {\n" 21073 " { c.response; } -> Response;\n" 21074 "} and requires (T c) {\n" 21075 " { c.request; } -> Request;\n" 21076 "}\n", 21077 Style); 21078 21079 verifyFormat("template <typename T>\n" 21080 "concept Context = Traits<typename T::traits_type> or\n" 21081 " Interface<typename T::interface_type> or\n" 21082 " Request<typename T::request_type> or\n" 21083 " Response<typename T::response_type> or\n" 21084 " ContextExtension<typename T::extension_type> or\n" 21085 " ::std::is_copy_constructable<T> or " 21086 "::std::is_move_constructable<T> or\n" 21087 " requires (T c) {\n" 21088 " { c.response; } -> Response;\n" 21089 "} or requires (T c) {\n" 21090 " { c.request; } -> Request;\n" 21091 "}\n", 21092 Style); 21093 21094 verifyFormat("template <typename T>\n" 21095 "concept Context = Traits<typename T::traits_type> &&\n" 21096 " Interface<typename T::interface_type> &&\n" 21097 " Request<typename T::request_type> &&\n" 21098 " Response<typename T::response_type> &&\n" 21099 " ContextExtension<typename T::extension_type> &&\n" 21100 " ::std::is_copy_constructable<T> && " 21101 "::std::is_move_constructable<T> &&\n" 21102 " requires (T c) {\n" 21103 " { c.response; } -> Response;\n" 21104 "} && requires (T c) {\n" 21105 " { c.request; } -> Request;\n" 21106 "}\n", 21107 Style); 21108 21109 verifyFormat("template <typename T>\nconcept someConcept = Constraint1<T> && " 21110 "Constraint2<T>;"); 21111 21112 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 21113 Style.BraceWrapping.AfterFunction = true; 21114 Style.BraceWrapping.AfterClass = true; 21115 Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 21116 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 21117 verifyFormat("void Foo () requires (std::copyable<T>)\n" 21118 "{\n" 21119 " return\n" 21120 "}\n", 21121 Style); 21122 21123 verifyFormat("void Foo () requires std::copyable<T>\n" 21124 "{\n" 21125 " return\n" 21126 "}\n", 21127 Style); 21128 21129 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 21130 " requires (std::invocable<F, std::invoke_result_t<Args>...>)\n" 21131 "struct constant;", 21132 Style); 21133 21134 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 21135 " requires std::invocable<F, std::invoke_result_t<Args>...>\n" 21136 "struct constant;", 21137 Style); 21138 21139 verifyFormat("template <class T>\n" 21140 "class plane_with_very_very_very_long_name\n" 21141 "{\n" 21142 " constexpr plane_with_very_very_very_long_name () requires " 21143 "std::copyable<T>\n" 21144 " : plane_with_very_very_very_long_name (1)\n" 21145 " {\n" 21146 " }\n" 21147 "}\n", 21148 Style); 21149 21150 verifyFormat("template <class T>\n" 21151 "class plane_with_long_name\n" 21152 "{\n" 21153 " constexpr plane_with_long_name () requires std::copyable<T>\n" 21154 " : plane_with_long_name (1)\n" 21155 " {\n" 21156 " }\n" 21157 "}\n", 21158 Style); 21159 21160 Style.BreakBeforeConceptDeclarations = false; 21161 verifyFormat("template <typename T> concept Tree = true;", Style); 21162 21163 Style.IndentRequires = false; 21164 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 21165 "requires (std::invocable<F, std::invoke_result_t<Args>...>) " 21166 "struct constant;", 21167 Style); 21168 } 21169 21170 TEST_F(FormatTest, StatementAttributeLikeMacros) { 21171 FormatStyle Style = getLLVMStyle(); 21172 StringRef Source = "void Foo::slot() {\n" 21173 " unsigned char MyChar = 'x';\n" 21174 " emit signal(MyChar);\n" 21175 " Q_EMIT signal(MyChar);\n" 21176 "}"; 21177 21178 EXPECT_EQ(Source, format(Source, Style)); 21179 21180 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 21181 EXPECT_EQ("void Foo::slot() {\n" 21182 " unsigned char MyChar = 'x';\n" 21183 " emit signal(MyChar);\n" 21184 " Q_EMIT signal(MyChar);\n" 21185 "}", 21186 format(Source, Style)); 21187 21188 Style.StatementAttributeLikeMacros.push_back("emit"); 21189 EXPECT_EQ(Source, format(Source, Style)); 21190 21191 Style.StatementAttributeLikeMacros = {}; 21192 EXPECT_EQ("void Foo::slot() {\n" 21193 " unsigned char MyChar = 'x';\n" 21194 " emit signal(MyChar);\n" 21195 " Q_EMIT signal(MyChar);\n" 21196 "}", 21197 format(Source, Style)); 21198 } 21199 21200 TEST_F(FormatTest, IndentAccessModifiers) { 21201 FormatStyle Style = getLLVMStyle(); 21202 Style.IndentAccessModifiers = true; 21203 // Members are *two* levels below the record; 21204 // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation. 21205 verifyFormat("class C {\n" 21206 " int i;\n" 21207 "};\n", 21208 Style); 21209 verifyFormat("union C {\n" 21210 " int i;\n" 21211 " unsigned u;\n" 21212 "};\n", 21213 Style); 21214 // Access modifiers should be indented one level below the record. 21215 verifyFormat("class C {\n" 21216 " public:\n" 21217 " int i;\n" 21218 "};\n", 21219 Style); 21220 verifyFormat("struct S {\n" 21221 " private:\n" 21222 " class C {\n" 21223 " int j;\n" 21224 "\n" 21225 " public:\n" 21226 " C();\n" 21227 " };\n" 21228 "\n" 21229 " public:\n" 21230 " int i;\n" 21231 "};\n", 21232 Style); 21233 // Enumerations are not records and should be unaffected. 21234 Style.AllowShortEnumsOnASingleLine = false; 21235 verifyFormat("enum class E\n" 21236 "{\n" 21237 " A,\n" 21238 " B\n" 21239 "};\n", 21240 Style); 21241 // Test with a different indentation width; 21242 // also proves that the result is Style.AccessModifierOffset agnostic. 21243 Style.IndentWidth = 3; 21244 verifyFormat("class C {\n" 21245 " public:\n" 21246 " int i;\n" 21247 "};\n", 21248 Style); 21249 } 21250 21251 TEST_F(FormatTest, LimitlessStringsAndComments) { 21252 auto Style = getLLVMStyleWithColumns(0); 21253 constexpr StringRef Code = 21254 "/**\n" 21255 " * This is a multiline comment with quite some long lines, at least for " 21256 "the LLVM Style.\n" 21257 " * We will redo this with strings and line comments. Just to check if " 21258 "everything is working.\n" 21259 " */\n" 21260 "bool foo() {\n" 21261 " /* Single line multi line comment. */\n" 21262 " const std::string String = \"This is a multiline string with quite " 21263 "some long lines, at least for the LLVM Style.\"\n" 21264 " \"We already did it with multi line " 21265 "comments, and we will do it with line comments. Just to check if " 21266 "everything is working.\";\n" 21267 " // This is a line comment (block) with quite some long lines, at " 21268 "least for the LLVM Style.\n" 21269 " // We already did this with multi line comments and strings. Just to " 21270 "check if everything is working.\n" 21271 " const std::string SmallString = \"Hello World\";\n" 21272 " // Small line comment\n" 21273 " return String.size() > SmallString.size();\n" 21274 "}"; 21275 EXPECT_EQ(Code, format(Code, Style)); 21276 } 21277 } // namespace 21278 } // namespace format 21279 } // namespace clang 21280