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 constexpr (a)\n" 1185 " f();\n" 1186 "else if constexpr (b)\n" 1187 " g();\n" 1188 "else\n" 1189 " h();"); 1190 verifyFormat("if CONSTEXPR (a)\n" 1191 " f();\n" 1192 "else if CONSTEXPR (b)\n" 1193 " g();\n" 1194 "else\n" 1195 " h();"); 1196 verifyFormat("if (a) {\n" 1197 " f();\n" 1198 "}\n" 1199 "// or else ..\n" 1200 "else {\n" 1201 " g()\n" 1202 "}"); 1203 1204 verifyFormat("if (a) {\n" 1205 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1206 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 1207 "}"); 1208 verifyFormat("if (a) {\n" 1209 "} else if constexpr (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1210 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 1211 "}"); 1212 verifyFormat("if (a) {\n" 1213 "} else if CONSTEXPR (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1214 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 1215 "}"); 1216 verifyFormat("if (a) {\n" 1217 "} else if (\n" 1218 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 1219 "}", 1220 getLLVMStyleWithColumns(62)); 1221 verifyFormat("if (a) {\n" 1222 "} else if constexpr (\n" 1223 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 1224 "}", 1225 getLLVMStyleWithColumns(62)); 1226 verifyFormat("if (a) {\n" 1227 "} else if CONSTEXPR (\n" 1228 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 1229 "}", 1230 getLLVMStyleWithColumns(62)); 1231 } 1232 1233 TEST_F(FormatTest, FormatsForLoop) { 1234 verifyFormat( 1235 "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" 1236 " ++VeryVeryLongLoopVariable)\n" 1237 " ;"); 1238 verifyFormat("for (;;)\n" 1239 " f();"); 1240 verifyFormat("for (;;) {\n}"); 1241 verifyFormat("for (;;) {\n" 1242 " f();\n" 1243 "}"); 1244 verifyFormat("for (int i = 0; (i < 10); ++i) {\n}"); 1245 1246 verifyFormat( 1247 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 1248 " E = UnwrappedLines.end();\n" 1249 " I != E; ++I) {\n}"); 1250 1251 verifyFormat( 1252 "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" 1253 " ++IIIII) {\n}"); 1254 verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" 1255 " aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" 1256 " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); 1257 verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n" 1258 " I = FD->getDeclsInPrototypeScope().begin(),\n" 1259 " E = FD->getDeclsInPrototypeScope().end();\n" 1260 " I != E; ++I) {\n}"); 1261 verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n" 1262 " I = Container.begin(),\n" 1263 " E = Container.end();\n" 1264 " I != E; ++I) {\n}", 1265 getLLVMStyleWithColumns(76)); 1266 1267 verifyFormat( 1268 "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 1269 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" 1270 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1271 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 1272 " ++aaaaaaaaaaa) {\n}"); 1273 verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 1274 " bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n" 1275 " ++i) {\n}"); 1276 verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" 1277 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 1278 "}"); 1279 verifyFormat("for (some_namespace::SomeIterator iter( // force break\n" 1280 " aaaaaaaaaa);\n" 1281 " iter; ++iter) {\n" 1282 "}"); 1283 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 1284 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 1285 " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n" 1286 " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {"); 1287 1288 // These should not be formatted as Objective-C for-in loops. 1289 verifyFormat("for (Foo *x = 0; x != in; x++) {\n}"); 1290 verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}"); 1291 verifyFormat("Foo *x;\nfor (x in y) {\n}"); 1292 verifyFormat( 1293 "for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}"); 1294 1295 FormatStyle NoBinPacking = getLLVMStyle(); 1296 NoBinPacking.BinPackParameters = false; 1297 verifyFormat("for (int aaaaaaaaaaa = 1;\n" 1298 " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" 1299 " aaaaaaaaaaaaaaaa,\n" 1300 " aaaaaaaaaaaaaaaa,\n" 1301 " aaaaaaaaaaaaaaaa);\n" 1302 " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" 1303 "}", 1304 NoBinPacking); 1305 verifyFormat( 1306 "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" 1307 " E = UnwrappedLines.end();\n" 1308 " I != E;\n" 1309 " ++I) {\n}", 1310 NoBinPacking); 1311 1312 FormatStyle AlignLeft = getLLVMStyle(); 1313 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 1314 verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft); 1315 } 1316 1317 TEST_F(FormatTest, RangeBasedForLoops) { 1318 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 1319 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 1320 verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" 1321 " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); 1322 verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" 1323 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 1324 verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n" 1325 " aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}"); 1326 } 1327 1328 TEST_F(FormatTest, ForEachLoops) { 1329 verifyFormat("void f() {\n" 1330 " foreach (Item *item, itemlist) {}\n" 1331 " Q_FOREACH (Item *item, itemlist) {}\n" 1332 " BOOST_FOREACH (Item *item, itemlist) {}\n" 1333 " UNKNOWN_FORACH(Item * item, itemlist) {}\n" 1334 "}"); 1335 1336 FormatStyle Style = getLLVMStyle(); 1337 Style.SpaceBeforeParens = 1338 FormatStyle::SBPO_ControlStatementsExceptForEachMacros; 1339 verifyFormat("void f() {\n" 1340 " foreach(Item *item, itemlist) {}\n" 1341 " Q_FOREACH(Item *item, itemlist) {}\n" 1342 " BOOST_FOREACH(Item *item, itemlist) {}\n" 1343 " UNKNOWN_FORACH(Item * item, itemlist) {}\n" 1344 "}", 1345 Style); 1346 1347 // As function-like macros. 1348 verifyFormat("#define foreach(x, y)\n" 1349 "#define Q_FOREACH(x, y)\n" 1350 "#define BOOST_FOREACH(x, y)\n" 1351 "#define UNKNOWN_FOREACH(x, y)\n"); 1352 1353 // Not as function-like macros. 1354 verifyFormat("#define foreach (x, y)\n" 1355 "#define Q_FOREACH (x, y)\n" 1356 "#define BOOST_FOREACH (x, y)\n" 1357 "#define UNKNOWN_FOREACH (x, y)\n"); 1358 1359 // handle microsoft non standard extension 1360 verifyFormat("for each (char c in x->MyStringProperty)"); 1361 } 1362 1363 TEST_F(FormatTest, FormatsWhileLoop) { 1364 verifyFormat("while (true) {\n}"); 1365 verifyFormat("while (true)\n" 1366 " f();"); 1367 verifyFormat("while () {\n}"); 1368 verifyFormat("while () {\n" 1369 " f();\n" 1370 "}"); 1371 } 1372 1373 TEST_F(FormatTest, FormatsDoWhile) { 1374 verifyFormat("do {\n" 1375 " do_something();\n" 1376 "} while (something());"); 1377 verifyFormat("do\n" 1378 " do_something();\n" 1379 "while (something());"); 1380 } 1381 1382 TEST_F(FormatTest, FormatsSwitchStatement) { 1383 verifyFormat("switch (x) {\n" 1384 "case 1:\n" 1385 " f();\n" 1386 " break;\n" 1387 "case kFoo:\n" 1388 "case ns::kBar:\n" 1389 "case kBaz:\n" 1390 " break;\n" 1391 "default:\n" 1392 " g();\n" 1393 " break;\n" 1394 "}"); 1395 verifyFormat("switch (x) {\n" 1396 "case 1: {\n" 1397 " f();\n" 1398 " break;\n" 1399 "}\n" 1400 "case 2: {\n" 1401 " break;\n" 1402 "}\n" 1403 "}"); 1404 verifyFormat("switch (x) {\n" 1405 "case 1: {\n" 1406 " f();\n" 1407 " {\n" 1408 " g();\n" 1409 " h();\n" 1410 " }\n" 1411 " break;\n" 1412 "}\n" 1413 "}"); 1414 verifyFormat("switch (x) {\n" 1415 "case 1: {\n" 1416 " f();\n" 1417 " if (foo) {\n" 1418 " g();\n" 1419 " h();\n" 1420 " }\n" 1421 " break;\n" 1422 "}\n" 1423 "}"); 1424 verifyFormat("switch (x) {\n" 1425 "case 1: {\n" 1426 " f();\n" 1427 " g();\n" 1428 "} break;\n" 1429 "}"); 1430 verifyFormat("switch (test)\n" 1431 " ;"); 1432 verifyFormat("switch (x) {\n" 1433 "default: {\n" 1434 " // Do nothing.\n" 1435 "}\n" 1436 "}"); 1437 verifyFormat("switch (x) {\n" 1438 "// comment\n" 1439 "// if 1, do f()\n" 1440 "case 1:\n" 1441 " f();\n" 1442 "}"); 1443 verifyFormat("switch (x) {\n" 1444 "case 1:\n" 1445 " // Do amazing stuff\n" 1446 " {\n" 1447 " f();\n" 1448 " g();\n" 1449 " }\n" 1450 " break;\n" 1451 "}"); 1452 verifyFormat("#define A \\\n" 1453 " switch (x) { \\\n" 1454 " case a: \\\n" 1455 " foo = b; \\\n" 1456 " }", 1457 getLLVMStyleWithColumns(20)); 1458 verifyFormat("#define OPERATION_CASE(name) \\\n" 1459 " case OP_name: \\\n" 1460 " return operations::Operation##name\n", 1461 getLLVMStyleWithColumns(40)); 1462 verifyFormat("switch (x) {\n" 1463 "case 1:;\n" 1464 "default:;\n" 1465 " int i;\n" 1466 "}"); 1467 1468 verifyGoogleFormat("switch (x) {\n" 1469 " case 1:\n" 1470 " f();\n" 1471 " break;\n" 1472 " case kFoo:\n" 1473 " case ns::kBar:\n" 1474 " case kBaz:\n" 1475 " break;\n" 1476 " default:\n" 1477 " g();\n" 1478 " break;\n" 1479 "}"); 1480 verifyGoogleFormat("switch (x) {\n" 1481 " case 1: {\n" 1482 " f();\n" 1483 " break;\n" 1484 " }\n" 1485 "}"); 1486 verifyGoogleFormat("switch (test)\n" 1487 " ;"); 1488 1489 verifyGoogleFormat("#define OPERATION_CASE(name) \\\n" 1490 " case OP_name: \\\n" 1491 " return operations::Operation##name\n"); 1492 verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n" 1493 " // Get the correction operation class.\n" 1494 " switch (OpCode) {\n" 1495 " CASE(Add);\n" 1496 " CASE(Subtract);\n" 1497 " default:\n" 1498 " return operations::Unknown;\n" 1499 " }\n" 1500 "#undef OPERATION_CASE\n" 1501 "}"); 1502 verifyFormat("DEBUG({\n" 1503 " switch (x) {\n" 1504 " case A:\n" 1505 " f();\n" 1506 " break;\n" 1507 " // fallthrough\n" 1508 " case B:\n" 1509 " g();\n" 1510 " break;\n" 1511 " }\n" 1512 "});"); 1513 EXPECT_EQ("DEBUG({\n" 1514 " switch (x) {\n" 1515 " case A:\n" 1516 " f();\n" 1517 " break;\n" 1518 " // On B:\n" 1519 " case B:\n" 1520 " g();\n" 1521 " break;\n" 1522 " }\n" 1523 "});", 1524 format("DEBUG({\n" 1525 " switch (x) {\n" 1526 " case A:\n" 1527 " f();\n" 1528 " break;\n" 1529 " // On B:\n" 1530 " case B:\n" 1531 " g();\n" 1532 " break;\n" 1533 " }\n" 1534 "});", 1535 getLLVMStyle())); 1536 EXPECT_EQ("switch (n) {\n" 1537 "case 0: {\n" 1538 " return false;\n" 1539 "}\n" 1540 "default: {\n" 1541 " return true;\n" 1542 "}\n" 1543 "}", 1544 format("switch (n)\n" 1545 "{\n" 1546 "case 0: {\n" 1547 " return false;\n" 1548 "}\n" 1549 "default: {\n" 1550 " return true;\n" 1551 "}\n" 1552 "}", 1553 getLLVMStyle())); 1554 verifyFormat("switch (a) {\n" 1555 "case (b):\n" 1556 " return;\n" 1557 "}"); 1558 1559 verifyFormat("switch (a) {\n" 1560 "case some_namespace::\n" 1561 " some_constant:\n" 1562 " return;\n" 1563 "}", 1564 getLLVMStyleWithColumns(34)); 1565 1566 FormatStyle Style = getLLVMStyle(); 1567 Style.IndentCaseLabels = true; 1568 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 1569 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1570 Style.BraceWrapping.AfterCaseLabel = true; 1571 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 1572 EXPECT_EQ("switch (n)\n" 1573 "{\n" 1574 " case 0:\n" 1575 " {\n" 1576 " return false;\n" 1577 " }\n" 1578 " default:\n" 1579 " {\n" 1580 " return true;\n" 1581 " }\n" 1582 "}", 1583 format("switch (n) {\n" 1584 " case 0: {\n" 1585 " return false;\n" 1586 " }\n" 1587 " default: {\n" 1588 " return true;\n" 1589 " }\n" 1590 "}", 1591 Style)); 1592 Style.BraceWrapping.AfterCaseLabel = false; 1593 EXPECT_EQ("switch (n)\n" 1594 "{\n" 1595 " case 0: {\n" 1596 " return false;\n" 1597 " }\n" 1598 " default: {\n" 1599 " return true;\n" 1600 " }\n" 1601 "}", 1602 format("switch (n) {\n" 1603 " case 0:\n" 1604 " {\n" 1605 " return false;\n" 1606 " }\n" 1607 " default:\n" 1608 " {\n" 1609 " return true;\n" 1610 " }\n" 1611 "}", 1612 Style)); 1613 Style.IndentCaseLabels = false; 1614 Style.IndentCaseBlocks = true; 1615 EXPECT_EQ("switch (n)\n" 1616 "{\n" 1617 "case 0:\n" 1618 " {\n" 1619 " return false;\n" 1620 " }\n" 1621 "case 1:\n" 1622 " break;\n" 1623 "default:\n" 1624 " {\n" 1625 " return true;\n" 1626 " }\n" 1627 "}", 1628 format("switch (n) {\n" 1629 "case 0: {\n" 1630 " return false;\n" 1631 "}\n" 1632 "case 1:\n" 1633 " break;\n" 1634 "default: {\n" 1635 " return true;\n" 1636 "}\n" 1637 "}", 1638 Style)); 1639 Style.IndentCaseLabels = true; 1640 Style.IndentCaseBlocks = true; 1641 EXPECT_EQ("switch (n)\n" 1642 "{\n" 1643 " case 0:\n" 1644 " {\n" 1645 " return false;\n" 1646 " }\n" 1647 " case 1:\n" 1648 " break;\n" 1649 " default:\n" 1650 " {\n" 1651 " return true;\n" 1652 " }\n" 1653 "}", 1654 format("switch (n) {\n" 1655 "case 0: {\n" 1656 " return false;\n" 1657 "}\n" 1658 "case 1:\n" 1659 " break;\n" 1660 "default: {\n" 1661 " return true;\n" 1662 "}\n" 1663 "}", 1664 Style)); 1665 } 1666 1667 TEST_F(FormatTest, CaseRanges) { 1668 verifyFormat("switch (x) {\n" 1669 "case 'A' ... 'Z':\n" 1670 "case 1 ... 5:\n" 1671 "case a ... b:\n" 1672 " break;\n" 1673 "}"); 1674 } 1675 1676 TEST_F(FormatTest, ShortEnums) { 1677 FormatStyle Style = getLLVMStyle(); 1678 Style.AllowShortEnumsOnASingleLine = true; 1679 verifyFormat("enum { A, B, C } ShortEnum1, ShortEnum2;", Style); 1680 Style.AllowShortEnumsOnASingleLine = false; 1681 verifyFormat("enum\n" 1682 "{\n" 1683 " A,\n" 1684 " B,\n" 1685 " C\n" 1686 "} ShortEnum1, ShortEnum2;", 1687 Style); 1688 } 1689 1690 TEST_F(FormatTest, ShortCaseLabels) { 1691 FormatStyle Style = getLLVMStyle(); 1692 Style.AllowShortCaseLabelsOnASingleLine = true; 1693 verifyFormat("switch (a) {\n" 1694 "case 1: x = 1; break;\n" 1695 "case 2: return;\n" 1696 "case 3:\n" 1697 "case 4:\n" 1698 "case 5: return;\n" 1699 "case 6: // comment\n" 1700 " return;\n" 1701 "case 7:\n" 1702 " // comment\n" 1703 " return;\n" 1704 "case 8:\n" 1705 " x = 8; // comment\n" 1706 " break;\n" 1707 "default: y = 1; break;\n" 1708 "}", 1709 Style); 1710 verifyFormat("switch (a) {\n" 1711 "case 0: return; // comment\n" 1712 "case 1: break; // comment\n" 1713 "case 2: return;\n" 1714 "// comment\n" 1715 "case 3: return;\n" 1716 "// comment 1\n" 1717 "// comment 2\n" 1718 "// comment 3\n" 1719 "case 4: break; /* comment */\n" 1720 "case 5:\n" 1721 " // comment\n" 1722 " break;\n" 1723 "case 6: /* comment */ x = 1; break;\n" 1724 "case 7: x = /* comment */ 1; break;\n" 1725 "case 8:\n" 1726 " x = 1; /* comment */\n" 1727 " break;\n" 1728 "case 9:\n" 1729 " break; // comment line 1\n" 1730 " // comment line 2\n" 1731 "}", 1732 Style); 1733 EXPECT_EQ("switch (a) {\n" 1734 "case 1:\n" 1735 " x = 8;\n" 1736 " // fall through\n" 1737 "case 2: x = 8;\n" 1738 "// comment\n" 1739 "case 3:\n" 1740 " return; /* comment line 1\n" 1741 " * comment line 2 */\n" 1742 "case 4: i = 8;\n" 1743 "// something else\n" 1744 "#if FOO\n" 1745 "case 5: break;\n" 1746 "#endif\n" 1747 "}", 1748 format("switch (a) {\n" 1749 "case 1: x = 8;\n" 1750 " // fall through\n" 1751 "case 2:\n" 1752 " x = 8;\n" 1753 "// comment\n" 1754 "case 3:\n" 1755 " return; /* comment line 1\n" 1756 " * comment line 2 */\n" 1757 "case 4:\n" 1758 " i = 8;\n" 1759 "// something else\n" 1760 "#if FOO\n" 1761 "case 5: break;\n" 1762 "#endif\n" 1763 "}", 1764 Style)); 1765 EXPECT_EQ("switch (a) {\n" 1766 "case 0:\n" 1767 " return; // long long long long long long long long long long " 1768 "long long comment\n" 1769 " // line\n" 1770 "}", 1771 format("switch (a) {\n" 1772 "case 0: return; // long long long long long long long long " 1773 "long long long long comment line\n" 1774 "}", 1775 Style)); 1776 EXPECT_EQ("switch (a) {\n" 1777 "case 0:\n" 1778 " return; /* long long long long long long long long long long " 1779 "long long comment\n" 1780 " line */\n" 1781 "}", 1782 format("switch (a) {\n" 1783 "case 0: return; /* long long long long long long long long " 1784 "long long long long comment line */\n" 1785 "}", 1786 Style)); 1787 verifyFormat("switch (a) {\n" 1788 "#if FOO\n" 1789 "case 0: return 0;\n" 1790 "#endif\n" 1791 "}", 1792 Style); 1793 verifyFormat("switch (a) {\n" 1794 "case 1: {\n" 1795 "}\n" 1796 "case 2: {\n" 1797 " return;\n" 1798 "}\n" 1799 "case 3: {\n" 1800 " x = 1;\n" 1801 " return;\n" 1802 "}\n" 1803 "case 4:\n" 1804 " if (x)\n" 1805 " return;\n" 1806 "}", 1807 Style); 1808 Style.ColumnLimit = 21; 1809 verifyFormat("switch (a) {\n" 1810 "case 1: x = 1; break;\n" 1811 "case 2: return;\n" 1812 "case 3:\n" 1813 "case 4:\n" 1814 "case 5: return;\n" 1815 "default:\n" 1816 " y = 1;\n" 1817 " break;\n" 1818 "}", 1819 Style); 1820 Style.ColumnLimit = 80; 1821 Style.AllowShortCaseLabelsOnASingleLine = false; 1822 Style.IndentCaseLabels = true; 1823 EXPECT_EQ("switch (n) {\n" 1824 " default /*comments*/:\n" 1825 " return true;\n" 1826 " case 0:\n" 1827 " return false;\n" 1828 "}", 1829 format("switch (n) {\n" 1830 "default/*comments*/:\n" 1831 " return true;\n" 1832 "case 0:\n" 1833 " return false;\n" 1834 "}", 1835 Style)); 1836 Style.AllowShortCaseLabelsOnASingleLine = true; 1837 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 1838 Style.BraceWrapping.AfterCaseLabel = true; 1839 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 1840 EXPECT_EQ("switch (n)\n" 1841 "{\n" 1842 " case 0:\n" 1843 " {\n" 1844 " return false;\n" 1845 " }\n" 1846 " default:\n" 1847 " {\n" 1848 " return true;\n" 1849 " }\n" 1850 "}", 1851 format("switch (n) {\n" 1852 " case 0: {\n" 1853 " return false;\n" 1854 " }\n" 1855 " default:\n" 1856 " {\n" 1857 " return true;\n" 1858 " }\n" 1859 "}", 1860 Style)); 1861 } 1862 1863 TEST_F(FormatTest, FormatsLabels) { 1864 verifyFormat("void f() {\n" 1865 " some_code();\n" 1866 "test_label:\n" 1867 " some_other_code();\n" 1868 " {\n" 1869 " some_more_code();\n" 1870 " another_label:\n" 1871 " some_more_code();\n" 1872 " }\n" 1873 "}"); 1874 verifyFormat("{\n" 1875 " some_code();\n" 1876 "test_label:\n" 1877 " some_other_code();\n" 1878 "}"); 1879 verifyFormat("{\n" 1880 " some_code();\n" 1881 "test_label:;\n" 1882 " int i = 0;\n" 1883 "}"); 1884 FormatStyle Style = getLLVMStyle(); 1885 Style.IndentGotoLabels = false; 1886 verifyFormat("void f() {\n" 1887 " some_code();\n" 1888 "test_label:\n" 1889 " some_other_code();\n" 1890 " {\n" 1891 " some_more_code();\n" 1892 "another_label:\n" 1893 " some_more_code();\n" 1894 " }\n" 1895 "}", 1896 Style); 1897 verifyFormat("{\n" 1898 " some_code();\n" 1899 "test_label:\n" 1900 " some_other_code();\n" 1901 "}", 1902 Style); 1903 verifyFormat("{\n" 1904 " some_code();\n" 1905 "test_label:;\n" 1906 " int i = 0;\n" 1907 "}"); 1908 } 1909 1910 TEST_F(FormatTest, MultiLineControlStatements) { 1911 FormatStyle Style = getLLVMStyle(); 1912 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom; 1913 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine; 1914 Style.ColumnLimit = 20; 1915 // Short lines should keep opening brace on same line. 1916 EXPECT_EQ("if (foo) {\n" 1917 " bar();\n" 1918 "}", 1919 format("if(foo){bar();}", Style)); 1920 EXPECT_EQ("if (foo) {\n" 1921 " bar();\n" 1922 "} else {\n" 1923 " baz();\n" 1924 "}", 1925 format("if(foo){bar();}else{baz();}", Style)); 1926 EXPECT_EQ("if (foo && bar) {\n" 1927 " baz();\n" 1928 "}", 1929 format("if(foo&&bar){baz();}", Style)); 1930 EXPECT_EQ("if (foo) {\n" 1931 " bar();\n" 1932 "} else if (baz) {\n" 1933 " quux();\n" 1934 "}", 1935 format("if(foo){bar();}else if(baz){quux();}", Style)); 1936 EXPECT_EQ( 1937 "if (foo) {\n" 1938 " bar();\n" 1939 "} else if (baz) {\n" 1940 " quux();\n" 1941 "} else {\n" 1942 " foobar();\n" 1943 "}", 1944 format("if(foo){bar();}else if(baz){quux();}else{foobar();}", Style)); 1945 EXPECT_EQ("for (;;) {\n" 1946 " foo();\n" 1947 "}", 1948 format("for(;;){foo();}")); 1949 EXPECT_EQ("while (1) {\n" 1950 " foo();\n" 1951 "}", 1952 format("while(1){foo();}", Style)); 1953 EXPECT_EQ("switch (foo) {\n" 1954 "case bar:\n" 1955 " return;\n" 1956 "}", 1957 format("switch(foo){case bar:return;}", Style)); 1958 EXPECT_EQ("try {\n" 1959 " foo();\n" 1960 "} catch (...) {\n" 1961 " bar();\n" 1962 "}", 1963 format("try{foo();}catch(...){bar();}", Style)); 1964 EXPECT_EQ("do {\n" 1965 " foo();\n" 1966 "} while (bar &&\n" 1967 " baz);", 1968 format("do{foo();}while(bar&&baz);", Style)); 1969 // Long lines should put opening brace on new line. 1970 EXPECT_EQ("if (foo && bar &&\n" 1971 " baz)\n" 1972 "{\n" 1973 " quux();\n" 1974 "}", 1975 format("if(foo&&bar&&baz){quux();}", Style)); 1976 EXPECT_EQ("if (foo && bar &&\n" 1977 " baz)\n" 1978 "{\n" 1979 " quux();\n" 1980 "}", 1981 format("if (foo && bar &&\n" 1982 " baz) {\n" 1983 " quux();\n" 1984 "}", 1985 Style)); 1986 EXPECT_EQ("if (foo) {\n" 1987 " bar();\n" 1988 "} else if (baz ||\n" 1989 " quux)\n" 1990 "{\n" 1991 " foobar();\n" 1992 "}", 1993 format("if(foo){bar();}else if(baz||quux){foobar();}", Style)); 1994 EXPECT_EQ( 1995 "if (foo) {\n" 1996 " bar();\n" 1997 "} else if (baz ||\n" 1998 " quux)\n" 1999 "{\n" 2000 " foobar();\n" 2001 "} else {\n" 2002 " barbaz();\n" 2003 "}", 2004 format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}", 2005 Style)); 2006 EXPECT_EQ("for (int i = 0;\n" 2007 " i < 10; ++i)\n" 2008 "{\n" 2009 " foo();\n" 2010 "}", 2011 format("for(int i=0;i<10;++i){foo();}", Style)); 2012 EXPECT_EQ("foreach (int i,\n" 2013 " list)\n" 2014 "{\n" 2015 " foo();\n" 2016 "}", 2017 format("foreach(int i, list){foo();}", Style)); 2018 Style.ColumnLimit = 2019 40; // to concentrate at brace wrapping, not line wrap due to column limit 2020 EXPECT_EQ("foreach (int i, list) {\n" 2021 " foo();\n" 2022 "}", 2023 format("foreach(int i, list){foo();}", Style)); 2024 Style.ColumnLimit = 2025 20; // to concentrate at brace wrapping, not line wrap due to column limit 2026 EXPECT_EQ("while (foo || bar ||\n" 2027 " baz)\n" 2028 "{\n" 2029 " quux();\n" 2030 "}", 2031 format("while(foo||bar||baz){quux();}", Style)); 2032 EXPECT_EQ("switch (\n" 2033 " foo = barbaz)\n" 2034 "{\n" 2035 "case quux:\n" 2036 " return;\n" 2037 "}", 2038 format("switch(foo=barbaz){case quux:return;}", Style)); 2039 EXPECT_EQ("try {\n" 2040 " foo();\n" 2041 "} catch (\n" 2042 " Exception &bar)\n" 2043 "{\n" 2044 " baz();\n" 2045 "}", 2046 format("try{foo();}catch(Exception&bar){baz();}", Style)); 2047 Style.ColumnLimit = 2048 40; // to concentrate at brace wrapping, not line wrap due to column limit 2049 EXPECT_EQ("try {\n" 2050 " foo();\n" 2051 "} catch (Exception &bar) {\n" 2052 " baz();\n" 2053 "}", 2054 format("try{foo();}catch(Exception&bar){baz();}", Style)); 2055 Style.ColumnLimit = 2056 20; // to concentrate at brace wrapping, not line wrap due to column limit 2057 2058 Style.BraceWrapping.BeforeElse = true; 2059 EXPECT_EQ( 2060 "if (foo) {\n" 2061 " bar();\n" 2062 "}\n" 2063 "else if (baz ||\n" 2064 " quux)\n" 2065 "{\n" 2066 " foobar();\n" 2067 "}\n" 2068 "else {\n" 2069 " barbaz();\n" 2070 "}", 2071 format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}", 2072 Style)); 2073 2074 Style.BraceWrapping.BeforeCatch = true; 2075 EXPECT_EQ("try {\n" 2076 " foo();\n" 2077 "}\n" 2078 "catch (...) {\n" 2079 " baz();\n" 2080 "}", 2081 format("try{foo();}catch(...){baz();}", Style)); 2082 } 2083 2084 TEST_F(FormatTest, BeforeWhile) { 2085 FormatStyle Style = getLLVMStyle(); 2086 Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom; 2087 2088 verifyFormat("do {\n" 2089 " foo();\n" 2090 "} while (1);", 2091 Style); 2092 Style.BraceWrapping.BeforeWhile = true; 2093 verifyFormat("do {\n" 2094 " foo();\n" 2095 "}\n" 2096 "while (1);", 2097 Style); 2098 } 2099 2100 //===----------------------------------------------------------------------===// 2101 // Tests for classes, namespaces, etc. 2102 //===----------------------------------------------------------------------===// 2103 2104 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { 2105 verifyFormat("class A {};"); 2106 } 2107 2108 TEST_F(FormatTest, UnderstandsAccessSpecifiers) { 2109 verifyFormat("class A {\n" 2110 "public:\n" 2111 "public: // comment\n" 2112 "protected:\n" 2113 "private:\n" 2114 " void f() {}\n" 2115 "};"); 2116 verifyFormat("export class A {\n" 2117 "public:\n" 2118 "public: // comment\n" 2119 "protected:\n" 2120 "private:\n" 2121 " void f() {}\n" 2122 "};"); 2123 verifyGoogleFormat("class A {\n" 2124 " public:\n" 2125 " protected:\n" 2126 " private:\n" 2127 " void f() {}\n" 2128 "};"); 2129 verifyGoogleFormat("export class A {\n" 2130 " public:\n" 2131 " protected:\n" 2132 " private:\n" 2133 " void f() {}\n" 2134 "};"); 2135 verifyFormat("class A {\n" 2136 "public slots:\n" 2137 " void f1() {}\n" 2138 "public Q_SLOTS:\n" 2139 " void f2() {}\n" 2140 "protected slots:\n" 2141 " void f3() {}\n" 2142 "protected Q_SLOTS:\n" 2143 " void f4() {}\n" 2144 "private slots:\n" 2145 " void f5() {}\n" 2146 "private Q_SLOTS:\n" 2147 " void f6() {}\n" 2148 "signals:\n" 2149 " void g1();\n" 2150 "Q_SIGNALS:\n" 2151 " void g2();\n" 2152 "};"); 2153 2154 // Don't interpret 'signals' the wrong way. 2155 verifyFormat("signals.set();"); 2156 verifyFormat("for (Signals signals : f()) {\n}"); 2157 verifyFormat("{\n" 2158 " signals.set(); // This needs indentation.\n" 2159 "}"); 2160 verifyFormat("void f() {\n" 2161 "label:\n" 2162 " signals.baz();\n" 2163 "}"); 2164 } 2165 2166 TEST_F(FormatTest, SeparatesLogicalBlocks) { 2167 EXPECT_EQ("class A {\n" 2168 "public:\n" 2169 " void f();\n" 2170 "\n" 2171 "private:\n" 2172 " void g() {}\n" 2173 " // test\n" 2174 "protected:\n" 2175 " int h;\n" 2176 "};", 2177 format("class A {\n" 2178 "public:\n" 2179 "void f();\n" 2180 "private:\n" 2181 "void g() {}\n" 2182 "// test\n" 2183 "protected:\n" 2184 "int h;\n" 2185 "};")); 2186 EXPECT_EQ("class A {\n" 2187 "protected:\n" 2188 "public:\n" 2189 " void f();\n" 2190 "};", 2191 format("class A {\n" 2192 "protected:\n" 2193 "\n" 2194 "public:\n" 2195 "\n" 2196 " void f();\n" 2197 "};")); 2198 2199 // Even ensure proper spacing inside macros. 2200 EXPECT_EQ("#define B \\\n" 2201 " class A { \\\n" 2202 " protected: \\\n" 2203 " public: \\\n" 2204 " void f(); \\\n" 2205 " };", 2206 format("#define B \\\n" 2207 " class A { \\\n" 2208 " protected: \\\n" 2209 " \\\n" 2210 " public: \\\n" 2211 " \\\n" 2212 " void f(); \\\n" 2213 " };", 2214 getGoogleStyle())); 2215 // But don't remove empty lines after macros ending in access specifiers. 2216 EXPECT_EQ("#define A private:\n" 2217 "\n" 2218 "int i;", 2219 format("#define A private:\n" 2220 "\n" 2221 "int i;")); 2222 } 2223 2224 TEST_F(FormatTest, FormatsClasses) { 2225 verifyFormat("class A : public B {};"); 2226 verifyFormat("class A : public ::B {};"); 2227 2228 verifyFormat( 2229 "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 2230 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 2231 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 2232 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 2233 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};"); 2234 verifyFormat( 2235 "class A : public B, public C, public D, public E, public F {};"); 2236 verifyFormat("class AAAAAAAAAAAA : public B,\n" 2237 " public C,\n" 2238 " public D,\n" 2239 " public E,\n" 2240 " public F,\n" 2241 " public G {};"); 2242 2243 verifyFormat("class\n" 2244 " ReallyReallyLongClassName {\n" 2245 " int i;\n" 2246 "};", 2247 getLLVMStyleWithColumns(32)); 2248 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 2249 " aaaaaaaaaaaaaaaa> {};"); 2250 verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n" 2251 " : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n" 2252 " aaaaaaaaaaaaaaaaaaaaaa> {};"); 2253 verifyFormat("template <class R, class C>\n" 2254 "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n" 2255 " : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};"); 2256 verifyFormat("class ::A::B {};"); 2257 } 2258 2259 TEST_F(FormatTest, BreakInheritanceStyle) { 2260 FormatStyle StyleWithInheritanceBreakBeforeComma = getLLVMStyle(); 2261 StyleWithInheritanceBreakBeforeComma.BreakInheritanceList = 2262 FormatStyle::BILS_BeforeComma; 2263 verifyFormat("class MyClass : public X {};", 2264 StyleWithInheritanceBreakBeforeComma); 2265 verifyFormat("class MyClass\n" 2266 " : public X\n" 2267 " , public Y {};", 2268 StyleWithInheritanceBreakBeforeComma); 2269 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA\n" 2270 " : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n" 2271 " , public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", 2272 StyleWithInheritanceBreakBeforeComma); 2273 verifyFormat("struct aaaaaaaaaaaaa\n" 2274 " : public aaaaaaaaaaaaaaaaaaa< // break\n" 2275 " aaaaaaaaaaaaaaaa> {};", 2276 StyleWithInheritanceBreakBeforeComma); 2277 2278 FormatStyle StyleWithInheritanceBreakAfterColon = getLLVMStyle(); 2279 StyleWithInheritanceBreakAfterColon.BreakInheritanceList = 2280 FormatStyle::BILS_AfterColon; 2281 verifyFormat("class MyClass : public X {};", 2282 StyleWithInheritanceBreakAfterColon); 2283 verifyFormat("class MyClass : public X, public Y {};", 2284 StyleWithInheritanceBreakAfterColon); 2285 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAA :\n" 2286 " public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 2287 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};", 2288 StyleWithInheritanceBreakAfterColon); 2289 verifyFormat("struct aaaaaaaaaaaaa :\n" 2290 " public aaaaaaaaaaaaaaaaaaa< // break\n" 2291 " aaaaaaaaaaaaaaaa> {};", 2292 StyleWithInheritanceBreakAfterColon); 2293 2294 FormatStyle StyleWithInheritanceBreakAfterComma = getLLVMStyle(); 2295 StyleWithInheritanceBreakAfterComma.BreakInheritanceList = 2296 FormatStyle::BILS_AfterComma; 2297 verifyFormat("class MyClass : public X {};", 2298 StyleWithInheritanceBreakAfterComma); 2299 verifyFormat("class MyClass : public X,\n" 2300 " public Y {};", 2301 StyleWithInheritanceBreakAfterComma); 2302 verifyFormat( 2303 "class AAAAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" 2304 " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC " 2305 "{};", 2306 StyleWithInheritanceBreakAfterComma); 2307 verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n" 2308 " aaaaaaaaaaaaaaaa> {};", 2309 StyleWithInheritanceBreakAfterComma); 2310 verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" 2311 " : public OnceBreak,\n" 2312 " public AlwaysBreak,\n" 2313 " EvenBasesFitInOneLine {};", 2314 StyleWithInheritanceBreakAfterComma); 2315 } 2316 2317 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { 2318 verifyFormat("class A {\n} a, b;"); 2319 verifyFormat("struct A {\n} a, b;"); 2320 verifyFormat("union A {\n} a;"); 2321 } 2322 2323 TEST_F(FormatTest, FormatsEnum) { 2324 verifyFormat("enum {\n" 2325 " Zero,\n" 2326 " One = 1,\n" 2327 " Two = One + 1,\n" 2328 " Three = (One + Two),\n" 2329 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2330 " Five = (One, Two, Three, Four, 5)\n" 2331 "};"); 2332 verifyGoogleFormat("enum {\n" 2333 " Zero,\n" 2334 " One = 1,\n" 2335 " Two = One + 1,\n" 2336 " Three = (One + Two),\n" 2337 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2338 " Five = (One, Two, Three, Four, 5)\n" 2339 "};"); 2340 verifyFormat("enum Enum {};"); 2341 verifyFormat("enum {};"); 2342 verifyFormat("enum X E {} d;"); 2343 verifyFormat("enum __attribute__((...)) E {} d;"); 2344 verifyFormat("enum __declspec__((...)) E {} d;"); 2345 verifyFormat("enum {\n" 2346 " Bar = Foo<int, int>::value\n" 2347 "};", 2348 getLLVMStyleWithColumns(30)); 2349 2350 verifyFormat("enum ShortEnum { A, B, C };"); 2351 verifyGoogleFormat("enum ShortEnum { A, B, C };"); 2352 2353 EXPECT_EQ("enum KeepEmptyLines {\n" 2354 " ONE,\n" 2355 "\n" 2356 " TWO,\n" 2357 "\n" 2358 " THREE\n" 2359 "}", 2360 format("enum KeepEmptyLines {\n" 2361 " ONE,\n" 2362 "\n" 2363 " TWO,\n" 2364 "\n" 2365 "\n" 2366 " THREE\n" 2367 "}")); 2368 verifyFormat("enum E { // comment\n" 2369 " ONE,\n" 2370 " TWO\n" 2371 "};\n" 2372 "int i;"); 2373 2374 FormatStyle EightIndent = getLLVMStyle(); 2375 EightIndent.IndentWidth = 8; 2376 verifyFormat("enum {\n" 2377 " VOID,\n" 2378 " CHAR,\n" 2379 " SHORT,\n" 2380 " INT,\n" 2381 " LONG,\n" 2382 " SIGNED,\n" 2383 " UNSIGNED,\n" 2384 " BOOL,\n" 2385 " FLOAT,\n" 2386 " DOUBLE,\n" 2387 " COMPLEX\n" 2388 "};", 2389 EightIndent); 2390 2391 // Not enums. 2392 verifyFormat("enum X f() {\n" 2393 " a();\n" 2394 " return 42;\n" 2395 "}"); 2396 verifyFormat("enum X Type::f() {\n" 2397 " a();\n" 2398 " return 42;\n" 2399 "}"); 2400 verifyFormat("enum ::X f() {\n" 2401 " a();\n" 2402 " return 42;\n" 2403 "}"); 2404 verifyFormat("enum ns::X f() {\n" 2405 " a();\n" 2406 " return 42;\n" 2407 "}"); 2408 } 2409 2410 TEST_F(FormatTest, FormatsEnumsWithErrors) { 2411 verifyFormat("enum Type {\n" 2412 " One = 0; // These semicolons should be commas.\n" 2413 " Two = 1;\n" 2414 "};"); 2415 verifyFormat("namespace n {\n" 2416 "enum Type {\n" 2417 " One,\n" 2418 " Two, // missing };\n" 2419 " int i;\n" 2420 "}\n" 2421 "void g() {}"); 2422 } 2423 2424 TEST_F(FormatTest, FormatsEnumStruct) { 2425 verifyFormat("enum struct {\n" 2426 " Zero,\n" 2427 " One = 1,\n" 2428 " Two = One + 1,\n" 2429 " Three = (One + Two),\n" 2430 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2431 " Five = (One, Two, Three, Four, 5)\n" 2432 "};"); 2433 verifyFormat("enum struct Enum {};"); 2434 verifyFormat("enum struct {};"); 2435 verifyFormat("enum struct X E {} d;"); 2436 verifyFormat("enum struct __attribute__((...)) E {} d;"); 2437 verifyFormat("enum struct __declspec__((...)) E {} d;"); 2438 verifyFormat("enum struct X f() {\n a();\n return 42;\n}"); 2439 } 2440 2441 TEST_F(FormatTest, FormatsEnumClass) { 2442 verifyFormat("enum class {\n" 2443 " Zero,\n" 2444 " One = 1,\n" 2445 " Two = One + 1,\n" 2446 " Three = (One + Two),\n" 2447 " Four = (Zero && (One ^ Two)) | (One << Two),\n" 2448 " Five = (One, Two, Three, Four, 5)\n" 2449 "};"); 2450 verifyFormat("enum class Enum {};"); 2451 verifyFormat("enum class {};"); 2452 verifyFormat("enum class X E {} d;"); 2453 verifyFormat("enum class __attribute__((...)) E {} d;"); 2454 verifyFormat("enum class __declspec__((...)) E {} d;"); 2455 verifyFormat("enum class X f() {\n a();\n return 42;\n}"); 2456 } 2457 2458 TEST_F(FormatTest, FormatsEnumTypes) { 2459 verifyFormat("enum X : int {\n" 2460 " A, // Force multiple lines.\n" 2461 " B\n" 2462 "};"); 2463 verifyFormat("enum X : int { A, B };"); 2464 verifyFormat("enum X : std::uint32_t { A, B };"); 2465 } 2466 2467 TEST_F(FormatTest, FormatsTypedefEnum) { 2468 FormatStyle Style = getLLVMStyle(); 2469 Style.ColumnLimit = 40; 2470 verifyFormat("typedef enum {} EmptyEnum;"); 2471 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 2472 verifyFormat("typedef enum {\n" 2473 " ZERO = 0,\n" 2474 " ONE = 1,\n" 2475 " TWO = 2,\n" 2476 " THREE = 3\n" 2477 "} LongEnum;", 2478 Style); 2479 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2480 Style.BraceWrapping.AfterEnum = true; 2481 verifyFormat("typedef enum {} EmptyEnum;"); 2482 verifyFormat("typedef enum { A, B, C } ShortEnum;"); 2483 verifyFormat("typedef enum\n" 2484 "{\n" 2485 " ZERO = 0,\n" 2486 " ONE = 1,\n" 2487 " TWO = 2,\n" 2488 " THREE = 3\n" 2489 "} LongEnum;", 2490 Style); 2491 } 2492 2493 TEST_F(FormatTest, FormatsNSEnums) { 2494 verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }"); 2495 verifyGoogleFormat( 2496 "typedef NS_CLOSED_ENUM(NSInteger, SomeName) { AAA, BBB }"); 2497 verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n" 2498 " // Information about someDecentlyLongValue.\n" 2499 " someDecentlyLongValue,\n" 2500 " // Information about anotherDecentlyLongValue.\n" 2501 " anotherDecentlyLongValue,\n" 2502 " // Information about aThirdDecentlyLongValue.\n" 2503 " aThirdDecentlyLongValue\n" 2504 "};"); 2505 verifyGoogleFormat("typedef NS_CLOSED_ENUM(NSInteger, MyType) {\n" 2506 " // Information about someDecentlyLongValue.\n" 2507 " someDecentlyLongValue,\n" 2508 " // Information about anotherDecentlyLongValue.\n" 2509 " anotherDecentlyLongValue,\n" 2510 " // Information about aThirdDecentlyLongValue.\n" 2511 " aThirdDecentlyLongValue\n" 2512 "};"); 2513 verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n" 2514 " a = 1,\n" 2515 " b = 2,\n" 2516 " c = 3,\n" 2517 "};"); 2518 verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n" 2519 " a = 1,\n" 2520 " b = 2,\n" 2521 " c = 3,\n" 2522 "};"); 2523 verifyGoogleFormat("typedef CF_CLOSED_ENUM(NSInteger, MyType) {\n" 2524 " a = 1,\n" 2525 " b = 2,\n" 2526 " c = 3,\n" 2527 "};"); 2528 verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n" 2529 " a = 1,\n" 2530 " b = 2,\n" 2531 " c = 3,\n" 2532 "};"); 2533 } 2534 2535 TEST_F(FormatTest, FormatsBitfields) { 2536 verifyFormat("struct Bitfields {\n" 2537 " unsigned sClass : 8;\n" 2538 " unsigned ValueKind : 2;\n" 2539 "};"); 2540 verifyFormat("struct A {\n" 2541 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" 2542 " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" 2543 "};"); 2544 verifyFormat("struct MyStruct {\n" 2545 " uchar data;\n" 2546 " uchar : 8;\n" 2547 " uchar : 8;\n" 2548 " uchar other;\n" 2549 "};"); 2550 FormatStyle Style = getLLVMStyle(); 2551 Style.BitFieldColonSpacing = FormatStyle::BFCS_None; 2552 verifyFormat("struct Bitfields {\n" 2553 " unsigned sClass:8;\n" 2554 " unsigned ValueKind:2;\n" 2555 " uchar other;\n" 2556 "};", 2557 Style); 2558 verifyFormat("struct A {\n" 2559 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1,\n" 2560 " bbbbbbbbbbbbbbbbbbbbbbbbb:2;\n" 2561 "};", 2562 Style); 2563 Style.BitFieldColonSpacing = FormatStyle::BFCS_Before; 2564 verifyFormat("struct Bitfields {\n" 2565 " unsigned sClass :8;\n" 2566 " unsigned ValueKind :2;\n" 2567 " uchar other;\n" 2568 "};", 2569 Style); 2570 Style.BitFieldColonSpacing = FormatStyle::BFCS_After; 2571 verifyFormat("struct Bitfields {\n" 2572 " unsigned sClass: 8;\n" 2573 " unsigned ValueKind: 2;\n" 2574 " uchar other;\n" 2575 "};", 2576 Style); 2577 } 2578 2579 TEST_F(FormatTest, FormatsNamespaces) { 2580 FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); 2581 LLVMWithNoNamespaceFix.FixNamespaceComments = false; 2582 2583 verifyFormat("namespace some_namespace {\n" 2584 "class A {};\n" 2585 "void f() { f(); }\n" 2586 "}", 2587 LLVMWithNoNamespaceFix); 2588 verifyFormat("namespace N::inline D {\n" 2589 "class A {};\n" 2590 "void f() { f(); }\n" 2591 "}", 2592 LLVMWithNoNamespaceFix); 2593 verifyFormat("namespace N::inline D::E {\n" 2594 "class A {};\n" 2595 "void f() { f(); }\n" 2596 "}", 2597 LLVMWithNoNamespaceFix); 2598 verifyFormat("namespace [[deprecated(\"foo[bar\")]] some_namespace {\n" 2599 "class A {};\n" 2600 "void f() { f(); }\n" 2601 "}", 2602 LLVMWithNoNamespaceFix); 2603 verifyFormat("/* something */ namespace some_namespace {\n" 2604 "class A {};\n" 2605 "void f() { f(); }\n" 2606 "}", 2607 LLVMWithNoNamespaceFix); 2608 verifyFormat("namespace {\n" 2609 "class A {};\n" 2610 "void f() { f(); }\n" 2611 "}", 2612 LLVMWithNoNamespaceFix); 2613 verifyFormat("/* something */ namespace {\n" 2614 "class A {};\n" 2615 "void f() { f(); }\n" 2616 "}", 2617 LLVMWithNoNamespaceFix); 2618 verifyFormat("inline namespace X {\n" 2619 "class A {};\n" 2620 "void f() { f(); }\n" 2621 "}", 2622 LLVMWithNoNamespaceFix); 2623 verifyFormat("/* something */ inline namespace X {\n" 2624 "class A {};\n" 2625 "void f() { f(); }\n" 2626 "}", 2627 LLVMWithNoNamespaceFix); 2628 verifyFormat("export namespace X {\n" 2629 "class A {};\n" 2630 "void f() { f(); }\n" 2631 "}", 2632 LLVMWithNoNamespaceFix); 2633 verifyFormat("using namespace some_namespace;\n" 2634 "class A {};\n" 2635 "void f() { f(); }", 2636 LLVMWithNoNamespaceFix); 2637 2638 // This code is more common than we thought; if we 2639 // layout this correctly the semicolon will go into 2640 // its own line, which is undesirable. 2641 verifyFormat("namespace {};", LLVMWithNoNamespaceFix); 2642 verifyFormat("namespace {\n" 2643 "class A {};\n" 2644 "};", 2645 LLVMWithNoNamespaceFix); 2646 2647 verifyFormat("namespace {\n" 2648 "int SomeVariable = 0; // comment\n" 2649 "} // namespace", 2650 LLVMWithNoNamespaceFix); 2651 EXPECT_EQ("#ifndef HEADER_GUARD\n" 2652 "#define HEADER_GUARD\n" 2653 "namespace my_namespace {\n" 2654 "int i;\n" 2655 "} // my_namespace\n" 2656 "#endif // HEADER_GUARD", 2657 format("#ifndef HEADER_GUARD\n" 2658 " #define HEADER_GUARD\n" 2659 " namespace my_namespace {\n" 2660 "int i;\n" 2661 "} // my_namespace\n" 2662 "#endif // HEADER_GUARD", 2663 LLVMWithNoNamespaceFix)); 2664 2665 EXPECT_EQ("namespace A::B {\n" 2666 "class C {};\n" 2667 "}", 2668 format("namespace A::B {\n" 2669 "class C {};\n" 2670 "}", 2671 LLVMWithNoNamespaceFix)); 2672 2673 FormatStyle Style = getLLVMStyle(); 2674 Style.NamespaceIndentation = FormatStyle::NI_All; 2675 EXPECT_EQ("namespace out {\n" 2676 " int i;\n" 2677 " namespace in {\n" 2678 " int i;\n" 2679 " } // namespace in\n" 2680 "} // namespace out", 2681 format("namespace out {\n" 2682 "int i;\n" 2683 "namespace in {\n" 2684 "int i;\n" 2685 "} // namespace in\n" 2686 "} // namespace out", 2687 Style)); 2688 2689 Style.NamespaceIndentation = FormatStyle::NI_Inner; 2690 EXPECT_EQ("namespace out {\n" 2691 "int i;\n" 2692 "namespace in {\n" 2693 " int i;\n" 2694 "} // namespace in\n" 2695 "} // namespace out", 2696 format("namespace out {\n" 2697 "int i;\n" 2698 "namespace in {\n" 2699 "int i;\n" 2700 "} // namespace in\n" 2701 "} // namespace out", 2702 Style)); 2703 } 2704 2705 TEST_F(FormatTest, NamespaceMacros) { 2706 FormatStyle Style = getLLVMStyle(); 2707 Style.NamespaceMacros.push_back("TESTSUITE"); 2708 2709 verifyFormat("TESTSUITE(A) {\n" 2710 "int foo();\n" 2711 "} // TESTSUITE(A)", 2712 Style); 2713 2714 verifyFormat("TESTSUITE(A, B) {\n" 2715 "int foo();\n" 2716 "} // TESTSUITE(A)", 2717 Style); 2718 2719 // Properly indent according to NamespaceIndentation style 2720 Style.NamespaceIndentation = FormatStyle::NI_All; 2721 verifyFormat("TESTSUITE(A) {\n" 2722 " int foo();\n" 2723 "} // TESTSUITE(A)", 2724 Style); 2725 verifyFormat("TESTSUITE(A) {\n" 2726 " namespace B {\n" 2727 " int foo();\n" 2728 " } // namespace B\n" 2729 "} // TESTSUITE(A)", 2730 Style); 2731 verifyFormat("namespace A {\n" 2732 " TESTSUITE(B) {\n" 2733 " int foo();\n" 2734 " } // TESTSUITE(B)\n" 2735 "} // namespace A", 2736 Style); 2737 2738 Style.NamespaceIndentation = FormatStyle::NI_Inner; 2739 verifyFormat("TESTSUITE(A) {\n" 2740 "TESTSUITE(B) {\n" 2741 " int foo();\n" 2742 "} // TESTSUITE(B)\n" 2743 "} // TESTSUITE(A)", 2744 Style); 2745 verifyFormat("TESTSUITE(A) {\n" 2746 "namespace B {\n" 2747 " int foo();\n" 2748 "} // namespace B\n" 2749 "} // TESTSUITE(A)", 2750 Style); 2751 verifyFormat("namespace A {\n" 2752 "TESTSUITE(B) {\n" 2753 " int foo();\n" 2754 "} // TESTSUITE(B)\n" 2755 "} // namespace A", 2756 Style); 2757 2758 // Properly merge namespace-macros blocks in CompactNamespaces mode 2759 Style.NamespaceIndentation = FormatStyle::NI_None; 2760 Style.CompactNamespaces = true; 2761 verifyFormat("TESTSUITE(A) { TESTSUITE(B) {\n" 2762 "}} // TESTSUITE(A::B)", 2763 Style); 2764 2765 EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n" 2766 "}} // TESTSUITE(out::in)", 2767 format("TESTSUITE(out) {\n" 2768 "TESTSUITE(in) {\n" 2769 "} // TESTSUITE(in)\n" 2770 "} // TESTSUITE(out)", 2771 Style)); 2772 2773 EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n" 2774 "}} // TESTSUITE(out::in)", 2775 format("TESTSUITE(out) {\n" 2776 "TESTSUITE(in) {\n" 2777 "} // TESTSUITE(in)\n" 2778 "} // TESTSUITE(out)", 2779 Style)); 2780 2781 // Do not merge different namespaces/macros 2782 EXPECT_EQ("namespace out {\n" 2783 "TESTSUITE(in) {\n" 2784 "} // TESTSUITE(in)\n" 2785 "} // namespace out", 2786 format("namespace out {\n" 2787 "TESTSUITE(in) {\n" 2788 "} // TESTSUITE(in)\n" 2789 "} // namespace out", 2790 Style)); 2791 EXPECT_EQ("TESTSUITE(out) {\n" 2792 "namespace in {\n" 2793 "} // namespace in\n" 2794 "} // TESTSUITE(out)", 2795 format("TESTSUITE(out) {\n" 2796 "namespace in {\n" 2797 "} // namespace in\n" 2798 "} // TESTSUITE(out)", 2799 Style)); 2800 Style.NamespaceMacros.push_back("FOOBAR"); 2801 EXPECT_EQ("TESTSUITE(out) {\n" 2802 "FOOBAR(in) {\n" 2803 "} // FOOBAR(in)\n" 2804 "} // TESTSUITE(out)", 2805 format("TESTSUITE(out) {\n" 2806 "FOOBAR(in) {\n" 2807 "} // FOOBAR(in)\n" 2808 "} // TESTSUITE(out)", 2809 Style)); 2810 } 2811 2812 TEST_F(FormatTest, FormatsCompactNamespaces) { 2813 FormatStyle Style = getLLVMStyle(); 2814 Style.CompactNamespaces = true; 2815 Style.NamespaceMacros.push_back("TESTSUITE"); 2816 2817 verifyFormat("namespace A { namespace B {\n" 2818 "}} // namespace A::B", 2819 Style); 2820 2821 EXPECT_EQ("namespace out { namespace in {\n" 2822 "}} // namespace out::in", 2823 format("namespace out {\n" 2824 "namespace in {\n" 2825 "} // namespace in\n" 2826 "} // namespace out", 2827 Style)); 2828 2829 // Only namespaces which have both consecutive opening and end get compacted 2830 EXPECT_EQ("namespace out {\n" 2831 "namespace in1 {\n" 2832 "} // namespace in1\n" 2833 "namespace in2 {\n" 2834 "} // namespace in2\n" 2835 "} // namespace out", 2836 format("namespace out {\n" 2837 "namespace in1 {\n" 2838 "} // namespace in1\n" 2839 "namespace in2 {\n" 2840 "} // namespace in2\n" 2841 "} // namespace out", 2842 Style)); 2843 2844 EXPECT_EQ("namespace out {\n" 2845 "int i;\n" 2846 "namespace in {\n" 2847 "int j;\n" 2848 "} // namespace in\n" 2849 "int k;\n" 2850 "} // namespace out", 2851 format("namespace out { int i;\n" 2852 "namespace in { int j; } // namespace in\n" 2853 "int k; } // namespace out", 2854 Style)); 2855 2856 EXPECT_EQ("namespace A { namespace B { namespace C {\n" 2857 "}}} // namespace A::B::C\n", 2858 format("namespace A { namespace B {\n" 2859 "namespace C {\n" 2860 "}} // namespace B::C\n" 2861 "} // namespace A\n", 2862 Style)); 2863 2864 Style.ColumnLimit = 40; 2865 EXPECT_EQ("namespace aaaaaaaaaa {\n" 2866 "namespace bbbbbbbbbb {\n" 2867 "}} // namespace aaaaaaaaaa::bbbbbbbbbb", 2868 format("namespace aaaaaaaaaa {\n" 2869 "namespace bbbbbbbbbb {\n" 2870 "} // namespace bbbbbbbbbb\n" 2871 "} // namespace aaaaaaaaaa", 2872 Style)); 2873 2874 EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n" 2875 "namespace cccccc {\n" 2876 "}}} // namespace aaaaaa::bbbbbb::cccccc", 2877 format("namespace aaaaaa {\n" 2878 "namespace bbbbbb {\n" 2879 "namespace cccccc {\n" 2880 "} // namespace cccccc\n" 2881 "} // namespace bbbbbb\n" 2882 "} // namespace aaaaaa", 2883 Style)); 2884 Style.ColumnLimit = 80; 2885 2886 // Extra semicolon after 'inner' closing brace prevents merging 2887 EXPECT_EQ("namespace out { namespace in {\n" 2888 "}; } // namespace out::in", 2889 format("namespace out {\n" 2890 "namespace in {\n" 2891 "}; // namespace in\n" 2892 "} // namespace out", 2893 Style)); 2894 2895 // Extra semicolon after 'outer' closing brace is conserved 2896 EXPECT_EQ("namespace out { namespace in {\n" 2897 "}}; // namespace out::in", 2898 format("namespace out {\n" 2899 "namespace in {\n" 2900 "} // namespace in\n" 2901 "}; // namespace out", 2902 Style)); 2903 2904 Style.NamespaceIndentation = FormatStyle::NI_All; 2905 EXPECT_EQ("namespace out { namespace in {\n" 2906 " int i;\n" 2907 "}} // namespace out::in", 2908 format("namespace out {\n" 2909 "namespace in {\n" 2910 "int i;\n" 2911 "} // namespace in\n" 2912 "} // namespace out", 2913 Style)); 2914 EXPECT_EQ("namespace out { namespace mid {\n" 2915 " namespace in {\n" 2916 " int j;\n" 2917 " } // namespace in\n" 2918 " int k;\n" 2919 "}} // namespace out::mid", 2920 format("namespace out { namespace mid {\n" 2921 "namespace in { int j; } // namespace in\n" 2922 "int k; }} // namespace out::mid", 2923 Style)); 2924 2925 Style.NamespaceIndentation = FormatStyle::NI_Inner; 2926 EXPECT_EQ("namespace out { namespace in {\n" 2927 " int i;\n" 2928 "}} // namespace out::in", 2929 format("namespace out {\n" 2930 "namespace in {\n" 2931 "int i;\n" 2932 "} // namespace in\n" 2933 "} // namespace out", 2934 Style)); 2935 EXPECT_EQ("namespace out { namespace mid { namespace in {\n" 2936 " int i;\n" 2937 "}}} // namespace out::mid::in", 2938 format("namespace out {\n" 2939 "namespace mid {\n" 2940 "namespace in {\n" 2941 "int i;\n" 2942 "} // namespace in\n" 2943 "} // namespace mid\n" 2944 "} // namespace out", 2945 Style)); 2946 } 2947 2948 TEST_F(FormatTest, FormatsExternC) { 2949 verifyFormat("extern \"C\" {\nint a;"); 2950 verifyFormat("extern \"C\" {}"); 2951 verifyFormat("extern \"C\" {\n" 2952 "int foo();\n" 2953 "}"); 2954 verifyFormat("extern \"C\" int foo() {}"); 2955 verifyFormat("extern \"C\" int foo();"); 2956 verifyFormat("extern \"C\" int foo() {\n" 2957 " int i = 42;\n" 2958 " return i;\n" 2959 "}"); 2960 2961 FormatStyle Style = getLLVMStyle(); 2962 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 2963 Style.BraceWrapping.AfterFunction = true; 2964 verifyFormat("extern \"C\" int foo() {}", Style); 2965 verifyFormat("extern \"C\" int foo();", Style); 2966 verifyFormat("extern \"C\" int foo()\n" 2967 "{\n" 2968 " int i = 42;\n" 2969 " return i;\n" 2970 "}", 2971 Style); 2972 2973 Style.BraceWrapping.AfterExternBlock = true; 2974 Style.BraceWrapping.SplitEmptyRecord = false; 2975 verifyFormat("extern \"C\"\n" 2976 "{}", 2977 Style); 2978 verifyFormat("extern \"C\"\n" 2979 "{\n" 2980 " int foo();\n" 2981 "}", 2982 Style); 2983 } 2984 2985 TEST_F(FormatTest, IndentExternBlockStyle) { 2986 FormatStyle Style = getLLVMStyle(); 2987 Style.IndentWidth = 2; 2988 2989 Style.IndentExternBlock = FormatStyle::IEBS_Indent; 2990 verifyFormat("extern \"C\" { /*9*/\n}", Style); 2991 verifyFormat("extern \"C\" {\n" 2992 " int foo10();\n" 2993 "}", 2994 Style); 2995 2996 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; 2997 verifyFormat("extern \"C\" { /*11*/\n}", Style); 2998 verifyFormat("extern \"C\" {\n" 2999 "int foo12();\n" 3000 "}", 3001 Style); 3002 3003 Style.IndentExternBlock = FormatStyle::IEBS_AfterExternBlock; 3004 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3005 Style.BraceWrapping.AfterExternBlock = true; 3006 verifyFormat("extern \"C\"\n{ /*13*/\n}", Style); 3007 verifyFormat("extern \"C\"\n{\n" 3008 " int foo14();\n" 3009 "}", 3010 Style); 3011 3012 Style.IndentExternBlock = FormatStyle::IEBS_AfterExternBlock; 3013 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3014 Style.BraceWrapping.AfterExternBlock = false; 3015 verifyFormat("extern \"C\" { /*15*/\n}", Style); 3016 verifyFormat("extern \"C\" {\n" 3017 "int foo16();\n" 3018 "}", 3019 Style); 3020 } 3021 3022 TEST_F(FormatTest, FormatsInlineASM) { 3023 verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); 3024 verifyFormat("asm(\"nop\" ::: \"memory\");"); 3025 verifyFormat( 3026 "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" 3027 " \"cpuid\\n\\t\"\n" 3028 " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" 3029 " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" 3030 " : \"a\"(value));"); 3031 EXPECT_EQ( 3032 "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" 3033 " __asm {\n" 3034 " mov edx,[that] // vtable in edx\n" 3035 " mov eax,methodIndex\n" 3036 " call [edx][eax*4] // stdcall\n" 3037 " }\n" 3038 "}", 3039 format("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 EXPECT_EQ("_asm {\n" 3047 " xor eax, eax;\n" 3048 " cpuid;\n" 3049 "}", 3050 format("_asm {\n" 3051 " xor eax, eax;\n" 3052 " cpuid;\n" 3053 "}")); 3054 verifyFormat("void function() {\n" 3055 " // comment\n" 3056 " asm(\"\");\n" 3057 "}"); 3058 EXPECT_EQ("__asm {\n" 3059 "}\n" 3060 "int i;", 3061 format("__asm {\n" 3062 "}\n" 3063 "int i;")); 3064 } 3065 3066 TEST_F(FormatTest, FormatTryCatch) { 3067 verifyFormat("try {\n" 3068 " throw a * b;\n" 3069 "} catch (int a) {\n" 3070 " // Do nothing.\n" 3071 "} catch (...) {\n" 3072 " exit(42);\n" 3073 "}"); 3074 3075 // Function-level try statements. 3076 verifyFormat("int f() try { return 4; } catch (...) {\n" 3077 " return 5;\n" 3078 "}"); 3079 verifyFormat("class A {\n" 3080 " int a;\n" 3081 " A() try : a(0) {\n" 3082 " } catch (...) {\n" 3083 " throw;\n" 3084 " }\n" 3085 "};\n"); 3086 verifyFormat("class A {\n" 3087 " int a;\n" 3088 " A() try : a(0), b{1} {\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}, c{2} {\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 " { // New scope.\n" 3104 " }\n" 3105 " } catch (...) {\n" 3106 " throw;\n" 3107 " }\n" 3108 "};\n"); 3109 3110 // Incomplete try-catch blocks. 3111 verifyIncompleteFormat("try {} catch ("); 3112 } 3113 3114 TEST_F(FormatTest, FormatTryAsAVariable) { 3115 verifyFormat("int try;"); 3116 verifyFormat("int try, size;"); 3117 verifyFormat("try = foo();"); 3118 verifyFormat("if (try < size) {\n return true;\n}"); 3119 3120 verifyFormat("int catch;"); 3121 verifyFormat("int catch, size;"); 3122 verifyFormat("catch = foo();"); 3123 verifyFormat("if (catch < size) {\n return true;\n}"); 3124 3125 FormatStyle Style = getLLVMStyle(); 3126 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3127 Style.BraceWrapping.AfterFunction = true; 3128 Style.BraceWrapping.BeforeCatch = true; 3129 verifyFormat("try {\n" 3130 " int bar = 1;\n" 3131 "}\n" 3132 "catch (...) {\n" 3133 " int bar = 1;\n" 3134 "}", 3135 Style); 3136 verifyFormat("#if NO_EX\n" 3137 "try\n" 3138 "#endif\n" 3139 "{\n" 3140 "}\n" 3141 "#if NO_EX\n" 3142 "catch (...) {\n" 3143 "}", 3144 Style); 3145 verifyFormat("try /* abc */ {\n" 3146 " int bar = 1;\n" 3147 "}\n" 3148 "catch (...) {\n" 3149 " int bar = 1;\n" 3150 "}", 3151 Style); 3152 verifyFormat("try\n" 3153 "// abc\n" 3154 "{\n" 3155 " int bar = 1;\n" 3156 "}\n" 3157 "catch (...) {\n" 3158 " int bar = 1;\n" 3159 "}", 3160 Style); 3161 } 3162 3163 TEST_F(FormatTest, FormatSEHTryCatch) { 3164 verifyFormat("__try {\n" 3165 " int a = b * c;\n" 3166 "} __except (EXCEPTION_EXECUTE_HANDLER) {\n" 3167 " // Do nothing.\n" 3168 "}"); 3169 3170 verifyFormat("__try {\n" 3171 " int a = b * c;\n" 3172 "} __finally {\n" 3173 " // Do nothing.\n" 3174 "}"); 3175 3176 verifyFormat("DEBUG({\n" 3177 " __try {\n" 3178 " } __finally {\n" 3179 " }\n" 3180 "});\n"); 3181 } 3182 3183 TEST_F(FormatTest, IncompleteTryCatchBlocks) { 3184 verifyFormat("try {\n" 3185 " f();\n" 3186 "} catch {\n" 3187 " g();\n" 3188 "}"); 3189 verifyFormat("try {\n" 3190 " f();\n" 3191 "} catch (A a) MACRO(x) {\n" 3192 " g();\n" 3193 "} catch (B b) MACRO(x) {\n" 3194 " g();\n" 3195 "}"); 3196 } 3197 3198 TEST_F(FormatTest, FormatTryCatchBraceStyles) { 3199 FormatStyle Style = getLLVMStyle(); 3200 for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, 3201 FormatStyle::BS_WebKit}) { 3202 Style.BreakBeforeBraces = BraceStyle; 3203 verifyFormat("try {\n" 3204 " // something\n" 3205 "} catch (...) {\n" 3206 " // something\n" 3207 "}", 3208 Style); 3209 } 3210 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 3211 verifyFormat("try {\n" 3212 " // something\n" 3213 "}\n" 3214 "catch (...) {\n" 3215 " // something\n" 3216 "}", 3217 Style); 3218 verifyFormat("__try {\n" 3219 " // something\n" 3220 "}\n" 3221 "__finally {\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 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 3233 verifyFormat("try\n" 3234 "{\n" 3235 " // something\n" 3236 "}\n" 3237 "catch (...)\n" 3238 "{\n" 3239 " // something\n" 3240 "}", 3241 Style); 3242 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 3243 verifyFormat("try\n" 3244 " {\n" 3245 " // something white\n" 3246 " }\n" 3247 "catch (...)\n" 3248 " {\n" 3249 " // something white\n" 3250 " }", 3251 Style); 3252 Style.BreakBeforeBraces = FormatStyle::BS_GNU; 3253 verifyFormat("try\n" 3254 " {\n" 3255 " // something\n" 3256 " }\n" 3257 "catch (...)\n" 3258 " {\n" 3259 " // something\n" 3260 " }", 3261 Style); 3262 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3263 Style.BraceWrapping.BeforeCatch = true; 3264 verifyFormat("try {\n" 3265 " // something\n" 3266 "}\n" 3267 "catch (...) {\n" 3268 " // something\n" 3269 "}", 3270 Style); 3271 } 3272 3273 TEST_F(FormatTest, StaticInitializers) { 3274 verifyFormat("static SomeClass SC = {1, 'a'};"); 3275 3276 verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n" 3277 " 100000000, " 3278 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};"); 3279 3280 // Here, everything other than the "}" would fit on a line. 3281 verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" 3282 " 10000000000000000000000000};"); 3283 EXPECT_EQ("S s = {a,\n" 3284 "\n" 3285 " b};", 3286 format("S s = {\n" 3287 " a,\n" 3288 "\n" 3289 " b\n" 3290 "};")); 3291 3292 // FIXME: This would fit into the column limit if we'd fit "{ {" on the first 3293 // line. However, the formatting looks a bit off and this probably doesn't 3294 // happen often in practice. 3295 verifyFormat("static int Variable[1] = {\n" 3296 " {1000000000000000000000000000000000000}};", 3297 getLLVMStyleWithColumns(40)); 3298 } 3299 3300 TEST_F(FormatTest, DesignatedInitializers) { 3301 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 3302 verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n" 3303 " .bbbbbbbbbb = 2,\n" 3304 " .cccccccccc = 3,\n" 3305 " .dddddddddd = 4,\n" 3306 " .eeeeeeeeee = 5};"); 3307 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 3308 " .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n" 3309 " .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n" 3310 " .ccccccccccccccccccccccccccc = 3,\n" 3311 " .ddddddddddddddddddddddddddd = 4,\n" 3312 " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); 3313 3314 verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); 3315 3316 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 3317 verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n" 3318 " [2] = bbbbbbbbbb,\n" 3319 " [3] = cccccccccc,\n" 3320 " [4] = dddddddddd,\n" 3321 " [5] = eeeeeeeeee};"); 3322 verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" 3323 " [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 3324 " [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 3325 " [3] = cccccccccccccccccccccccccccccccccccccc,\n" 3326 " [4] = dddddddddddddddddddddddddddddddddddddd,\n" 3327 " [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};"); 3328 } 3329 3330 TEST_F(FormatTest, NestedStaticInitializers) { 3331 verifyFormat("static A x = {{{}}};\n"); 3332 verifyFormat("static A x = {{{init1, init2, init3, init4},\n" 3333 " {init1, init2, init3, init4}}};", 3334 getLLVMStyleWithColumns(50)); 3335 3336 verifyFormat("somes Status::global_reps[3] = {\n" 3337 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 3338 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 3339 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};", 3340 getLLVMStyleWithColumns(60)); 3341 verifyGoogleFormat("SomeType Status::global_reps[3] = {\n" 3342 " {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n" 3343 " {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n" 3344 " {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};"); 3345 verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n" 3346 " {rect.fRight - rect.fLeft, rect.fBottom - " 3347 "rect.fTop}};"); 3348 3349 verifyFormat( 3350 "SomeArrayOfSomeType a = {\n" 3351 " {{1, 2, 3},\n" 3352 " {1, 2, 3},\n" 3353 " {111111111111111111111111111111, 222222222222222222222222222222,\n" 3354 " 333333333333333333333333333333},\n" 3355 " {1, 2, 3},\n" 3356 " {1, 2, 3}}};"); 3357 verifyFormat( 3358 "SomeArrayOfSomeType a = {\n" 3359 " {{1, 2, 3}},\n" 3360 " {{1, 2, 3}},\n" 3361 " {{111111111111111111111111111111, 222222222222222222222222222222,\n" 3362 " 333333333333333333333333333333}},\n" 3363 " {{1, 2, 3}},\n" 3364 " {{1, 2, 3}}};"); 3365 3366 verifyFormat("struct {\n" 3367 " unsigned bit;\n" 3368 " const char *const name;\n" 3369 "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n" 3370 " {kOsWin, \"Windows\"},\n" 3371 " {kOsLinux, \"Linux\"},\n" 3372 " {kOsCrOS, \"Chrome OS\"}};"); 3373 verifyFormat("struct {\n" 3374 " unsigned bit;\n" 3375 " const char *const name;\n" 3376 "} kBitsToOs[] = {\n" 3377 " {kOsMac, \"Mac\"},\n" 3378 " {kOsWin, \"Windows\"},\n" 3379 " {kOsLinux, \"Linux\"},\n" 3380 " {kOsCrOS, \"Chrome OS\"},\n" 3381 "};"); 3382 } 3383 3384 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { 3385 verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 3386 " \\\n" 3387 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); 3388 } 3389 3390 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { 3391 verifyFormat("virtual void write(ELFWriter *writerrr,\n" 3392 " OwningPtr<FileOutputBuffer> &buffer) = 0;"); 3393 3394 // Do break defaulted and deleted functions. 3395 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 3396 " default;", 3397 getLLVMStyleWithColumns(40)); 3398 verifyFormat("virtual void ~Deeeeeeeestructor() =\n" 3399 " delete;", 3400 getLLVMStyleWithColumns(40)); 3401 } 3402 3403 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { 3404 verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3", 3405 getLLVMStyleWithColumns(40)); 3406 verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 3407 getLLVMStyleWithColumns(40)); 3408 EXPECT_EQ("#define Q \\\n" 3409 " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" 3410 " \"aaaaaaaa.cpp\"", 3411 format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", 3412 getLLVMStyleWithColumns(40))); 3413 } 3414 3415 TEST_F(FormatTest, UnderstandsLinePPDirective) { 3416 EXPECT_EQ("# 123 \"A string literal\"", 3417 format(" # 123 \"A string literal\"")); 3418 } 3419 3420 TEST_F(FormatTest, LayoutUnknownPPDirective) { 3421 EXPECT_EQ("#;", format("#;")); 3422 verifyFormat("#\n;\n;\n;"); 3423 } 3424 3425 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { 3426 EXPECT_EQ("#line 42 \"test\"\n", 3427 format("# \\\n line \\\n 42 \\\n \"test\"\n")); 3428 EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", 3429 getLLVMStyleWithColumns(12))); 3430 } 3431 3432 TEST_F(FormatTest, EndOfFileEndsPPDirective) { 3433 EXPECT_EQ("#line 42 \"test\"", 3434 format("# \\\n line \\\n 42 \\\n \"test\"")); 3435 EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); 3436 } 3437 3438 TEST_F(FormatTest, DoesntRemoveUnknownTokens) { 3439 verifyFormat("#define A \\x20"); 3440 verifyFormat("#define A \\ x20"); 3441 EXPECT_EQ("#define A \\ x20", format("#define A \\ x20")); 3442 verifyFormat("#define A ''"); 3443 verifyFormat("#define A ''qqq"); 3444 verifyFormat("#define A `qqq"); 3445 verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); 3446 EXPECT_EQ("const char *c = STRINGIFY(\n" 3447 "\\na : b);", 3448 format("const char * c = STRINGIFY(\n" 3449 "\\na : b);")); 3450 3451 verifyFormat("a\r\\"); 3452 verifyFormat("a\v\\"); 3453 verifyFormat("a\f\\"); 3454 } 3455 3456 TEST_F(FormatTest, IndentsPPDirectiveWithPPIndentWidth) { 3457 FormatStyle style = getChromiumStyle(FormatStyle::LK_Cpp); 3458 style.IndentWidth = 4; 3459 style.PPIndentWidth = 1; 3460 3461 style.IndentPPDirectives = FormatStyle::PPDIS_None; 3462 verifyFormat("#ifdef __linux__\n" 3463 "void foo() {\n" 3464 " int x = 0;\n" 3465 "}\n" 3466 "#define FOO\n" 3467 "#endif\n" 3468 "void bar() {\n" 3469 " int y = 0;\n" 3470 "}\n", 3471 style); 3472 3473 style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 3474 verifyFormat("#ifdef __linux__\n" 3475 "void foo() {\n" 3476 " int x = 0;\n" 3477 "}\n" 3478 "# define FOO foo\n" 3479 "#endif\n" 3480 "void bar() {\n" 3481 " int y = 0;\n" 3482 "}\n", 3483 style); 3484 3485 style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash; 3486 verifyFormat("#ifdef __linux__\n" 3487 "void foo() {\n" 3488 " int x = 0;\n" 3489 "}\n" 3490 " #define FOO foo\n" 3491 "#endif\n" 3492 "void bar() {\n" 3493 " int y = 0;\n" 3494 "}\n", 3495 style); 3496 } 3497 3498 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { 3499 verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); 3500 verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); 3501 verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); 3502 // FIXME: We never break before the macro name. 3503 verifyFormat("#define AA( \\\n B)", getLLVMStyleWithColumns(12)); 3504 3505 verifyFormat("#define A A\n#define A A"); 3506 verifyFormat("#define A(X) A\n#define A A"); 3507 3508 verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); 3509 verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); 3510 } 3511 3512 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { 3513 EXPECT_EQ("// somecomment\n" 3514 "#include \"a.h\"\n" 3515 "#define A( \\\n" 3516 " A, B)\n" 3517 "#include \"b.h\"\n" 3518 "// somecomment\n", 3519 format(" // somecomment\n" 3520 " #include \"a.h\"\n" 3521 "#define A(A,\\\n" 3522 " B)\n" 3523 " #include \"b.h\"\n" 3524 " // somecomment\n", 3525 getLLVMStyleWithColumns(13))); 3526 } 3527 3528 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } 3529 3530 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { 3531 EXPECT_EQ("#define A \\\n" 3532 " c; \\\n" 3533 " e;\n" 3534 "f;", 3535 format("#define A c; e;\n" 3536 "f;", 3537 getLLVMStyleWithColumns(14))); 3538 } 3539 3540 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } 3541 3542 TEST_F(FormatTest, MacroDefinitionInsideStatement) { 3543 EXPECT_EQ("int x,\n" 3544 "#define A\n" 3545 " y;", 3546 format("int x,\n#define A\ny;")); 3547 } 3548 3549 TEST_F(FormatTest, HashInMacroDefinition) { 3550 EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle())); 3551 verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); 3552 verifyFormat("#define A \\\n" 3553 " { \\\n" 3554 " f(#c); \\\n" 3555 " }", 3556 getLLVMStyleWithColumns(11)); 3557 3558 verifyFormat("#define A(X) \\\n" 3559 " void function##X()", 3560 getLLVMStyleWithColumns(22)); 3561 3562 verifyFormat("#define A(a, b, c) \\\n" 3563 " void a##b##c()", 3564 getLLVMStyleWithColumns(22)); 3565 3566 verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); 3567 } 3568 3569 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { 3570 EXPECT_EQ("#define A (x)", format("#define A (x)")); 3571 EXPECT_EQ("#define A(x)", format("#define A(x)")); 3572 3573 FormatStyle Style = getLLVMStyle(); 3574 Style.SpaceBeforeParens = FormatStyle::SBPO_Never; 3575 verifyFormat("#define true ((foo)1)", Style); 3576 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 3577 verifyFormat("#define false((foo)0)", Style); 3578 } 3579 3580 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { 3581 EXPECT_EQ("#define A b;", format("#define A \\\n" 3582 " \\\n" 3583 " b;", 3584 getLLVMStyleWithColumns(25))); 3585 EXPECT_EQ("#define A \\\n" 3586 " \\\n" 3587 " a; \\\n" 3588 " b;", 3589 format("#define A \\\n" 3590 " \\\n" 3591 " a; \\\n" 3592 " b;", 3593 getLLVMStyleWithColumns(11))); 3594 EXPECT_EQ("#define A \\\n" 3595 " a; \\\n" 3596 " \\\n" 3597 " b;", 3598 format("#define A \\\n" 3599 " a; \\\n" 3600 " \\\n" 3601 " b;", 3602 getLLVMStyleWithColumns(11))); 3603 } 3604 3605 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { 3606 verifyIncompleteFormat("#define A :"); 3607 verifyFormat("#define SOMECASES \\\n" 3608 " case 1: \\\n" 3609 " case 2\n", 3610 getLLVMStyleWithColumns(20)); 3611 verifyFormat("#define MACRO(a) \\\n" 3612 " if (a) \\\n" 3613 " f(); \\\n" 3614 " else \\\n" 3615 " g()", 3616 getLLVMStyleWithColumns(18)); 3617 verifyFormat("#define A template <typename T>"); 3618 verifyIncompleteFormat("#define STR(x) #x\n" 3619 "f(STR(this_is_a_string_literal{));"); 3620 verifyFormat("#pragma omp threadprivate( \\\n" 3621 " y)), // expected-warning", 3622 getLLVMStyleWithColumns(28)); 3623 verifyFormat("#d, = };"); 3624 verifyFormat("#if \"a"); 3625 verifyIncompleteFormat("({\n" 3626 "#define b \\\n" 3627 " } \\\n" 3628 " a\n" 3629 "a", 3630 getLLVMStyleWithColumns(15)); 3631 verifyFormat("#define A \\\n" 3632 " { \\\n" 3633 " {\n" 3634 "#define B \\\n" 3635 " } \\\n" 3636 " }", 3637 getLLVMStyleWithColumns(15)); 3638 verifyNoCrash("#if a\na(\n#else\n#endif\n{a"); 3639 verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}"); 3640 verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};"); 3641 verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() { \n)}"); 3642 } 3643 3644 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { 3645 verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. 3646 EXPECT_EQ("class A : public QObject {\n" 3647 " Q_OBJECT\n" 3648 "\n" 3649 " A() {}\n" 3650 "};", 3651 format("class A : public QObject {\n" 3652 " Q_OBJECT\n" 3653 "\n" 3654 " A() {\n}\n" 3655 "} ;")); 3656 EXPECT_EQ("MACRO\n" 3657 "/*static*/ int i;", 3658 format("MACRO\n" 3659 " /*static*/ int i;")); 3660 EXPECT_EQ("SOME_MACRO\n" 3661 "namespace {\n" 3662 "void f();\n" 3663 "} // namespace", 3664 format("SOME_MACRO\n" 3665 " namespace {\n" 3666 "void f( );\n" 3667 "} // namespace")); 3668 // Only if the identifier contains at least 5 characters. 3669 EXPECT_EQ("HTTP f();", format("HTTP\nf();")); 3670 EXPECT_EQ("MACRO\nf();", format("MACRO\nf();")); 3671 // Only if everything is upper case. 3672 EXPECT_EQ("class A : public QObject {\n" 3673 " Q_Object A() {}\n" 3674 "};", 3675 format("class A : public QObject {\n" 3676 " Q_Object\n" 3677 " A() {\n}\n" 3678 "} ;")); 3679 3680 // Only if the next line can actually start an unwrapped line. 3681 EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;", 3682 format("SOME_WEIRD_LOG_MACRO\n" 3683 "<< SomeThing;")); 3684 3685 verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " 3686 "(n, buffers))\n", 3687 getChromiumStyle(FormatStyle::LK_Cpp)); 3688 3689 // See PR41483 3690 EXPECT_EQ("/**/ FOO(a)\n" 3691 "FOO(b)", 3692 format("/**/ FOO(a)\n" 3693 "FOO(b)")); 3694 } 3695 3696 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { 3697 EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 3698 "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 3699 "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 3700 "class X {};\n" 3701 "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 3702 "int *createScopDetectionPass() { return 0; }", 3703 format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" 3704 " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" 3705 " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" 3706 " class X {};\n" 3707 " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" 3708 " int *createScopDetectionPass() { return 0; }")); 3709 // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as 3710 // braces, so that inner block is indented one level more. 3711 EXPECT_EQ("int q() {\n" 3712 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 3713 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 3714 " IPC_END_MESSAGE_MAP()\n" 3715 "}", 3716 format("int q() {\n" 3717 " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" 3718 " IPC_MESSAGE_HANDLER(xxx, qqq)\n" 3719 " IPC_END_MESSAGE_MAP()\n" 3720 "}")); 3721 3722 // Same inside macros. 3723 EXPECT_EQ("#define LIST(L) \\\n" 3724 " L(A) \\\n" 3725 " L(B) \\\n" 3726 " L(C)", 3727 format("#define LIST(L) \\\n" 3728 " L(A) \\\n" 3729 " L(B) \\\n" 3730 " L(C)", 3731 getGoogleStyle())); 3732 3733 // These must not be recognized as macros. 3734 EXPECT_EQ("int q() {\n" 3735 " f(x);\n" 3736 " f(x) {}\n" 3737 " f(x)->g();\n" 3738 " f(x)->*g();\n" 3739 " f(x).g();\n" 3740 " f(x) = x;\n" 3741 " f(x) += x;\n" 3742 " f(x) -= x;\n" 3743 " f(x) *= x;\n" 3744 " f(x) /= x;\n" 3745 " f(x) %= x;\n" 3746 " f(x) &= x;\n" 3747 " f(x) |= x;\n" 3748 " f(x) ^= x;\n" 3749 " f(x) >>= x;\n" 3750 " f(x) <<= x;\n" 3751 " f(x)[y].z();\n" 3752 " LOG(INFO) << x;\n" 3753 " ifstream(x) >> x;\n" 3754 "}\n", 3755 format("int q() {\n" 3756 " f(x)\n;\n" 3757 " f(x)\n {}\n" 3758 " f(x)\n->g();\n" 3759 " f(x)\n->*g();\n" 3760 " f(x)\n.g();\n" 3761 " f(x)\n = x;\n" 3762 " f(x)\n += x;\n" 3763 " f(x)\n -= x;\n" 3764 " f(x)\n *= x;\n" 3765 " f(x)\n /= x;\n" 3766 " f(x)\n %= x;\n" 3767 " f(x)\n &= x;\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[y].z();\n" 3773 " LOG(INFO)\n << x;\n" 3774 " ifstream(x)\n >> x;\n" 3775 "}\n")); 3776 EXPECT_EQ("int q() {\n" 3777 " F(x)\n" 3778 " if (1) {\n" 3779 " }\n" 3780 " F(x)\n" 3781 " while (1) {\n" 3782 " }\n" 3783 " F(x)\n" 3784 " G(x);\n" 3785 " F(x)\n" 3786 " try {\n" 3787 " Q();\n" 3788 " } catch (...) {\n" 3789 " }\n" 3790 "}\n", 3791 format("int q() {\n" 3792 "F(x)\n" 3793 "if (1) {}\n" 3794 "F(x)\n" 3795 "while (1) {}\n" 3796 "F(x)\n" 3797 "G(x);\n" 3798 "F(x)\n" 3799 "try { Q(); } catch (...) {}\n" 3800 "}\n")); 3801 EXPECT_EQ("class A {\n" 3802 " A() : t(0) {}\n" 3803 " A(int i) noexcept() : {}\n" 3804 " A(X x)\n" // FIXME: function-level try blocks are broken. 3805 " try : t(0) {\n" 3806 " } catch (...) {\n" 3807 " }\n" 3808 "};", 3809 format("class A {\n" 3810 " A()\n : t(0) {}\n" 3811 " A(int i)\n noexcept() : {}\n" 3812 " A(X x)\n" 3813 " try : t(0) {} catch (...) {}\n" 3814 "};")); 3815 FormatStyle Style = getLLVMStyle(); 3816 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 3817 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; 3818 Style.BraceWrapping.AfterFunction = true; 3819 EXPECT_EQ("void f()\n" 3820 "try\n" 3821 "{\n" 3822 "}", 3823 format("void f() try {\n" 3824 "}", 3825 Style)); 3826 EXPECT_EQ("class SomeClass {\n" 3827 "public:\n" 3828 " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 3829 "};", 3830 format("class SomeClass {\n" 3831 "public:\n" 3832 " SomeClass()\n" 3833 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 3834 "};")); 3835 EXPECT_EQ("class SomeClass {\n" 3836 "public:\n" 3837 " SomeClass()\n" 3838 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 3839 "};", 3840 format("class SomeClass {\n" 3841 "public:\n" 3842 " SomeClass()\n" 3843 " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" 3844 "};", 3845 getLLVMStyleWithColumns(40))); 3846 3847 verifyFormat("MACRO(>)"); 3848 3849 // Some macros contain an implicit semicolon. 3850 Style = getLLVMStyle(); 3851 Style.StatementMacros.push_back("FOO"); 3852 verifyFormat("FOO(a) int b = 0;"); 3853 verifyFormat("FOO(a)\n" 3854 "int b = 0;", 3855 Style); 3856 verifyFormat("FOO(a);\n" 3857 "int b = 0;", 3858 Style); 3859 verifyFormat("FOO(argc, argv, \"4.0.2\")\n" 3860 "int b = 0;", 3861 Style); 3862 verifyFormat("FOO()\n" 3863 "int b = 0;", 3864 Style); 3865 verifyFormat("FOO\n" 3866 "int b = 0;", 3867 Style); 3868 verifyFormat("void f() {\n" 3869 " FOO(a)\n" 3870 " return a;\n" 3871 "}", 3872 Style); 3873 verifyFormat("FOO(a)\n" 3874 "FOO(b)", 3875 Style); 3876 verifyFormat("int a = 0;\n" 3877 "FOO(b)\n" 3878 "int c = 0;", 3879 Style); 3880 verifyFormat("int a = 0;\n" 3881 "int x = FOO(a)\n" 3882 "int b = 0;", 3883 Style); 3884 verifyFormat("void foo(int a) { FOO(a) }\n" 3885 "uint32_t bar() {}", 3886 Style); 3887 } 3888 3889 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) { 3890 verifyFormat("#define A \\\n" 3891 " f({ \\\n" 3892 " g(); \\\n" 3893 " });", 3894 getLLVMStyleWithColumns(11)); 3895 } 3896 3897 TEST_F(FormatTest, IndentPreprocessorDirectives) { 3898 FormatStyle Style = getLLVMStyle(); 3899 Style.IndentPPDirectives = FormatStyle::PPDIS_None; 3900 Style.ColumnLimit = 40; 3901 verifyFormat("#ifdef _WIN32\n" 3902 "#define A 0\n" 3903 "#ifdef VAR2\n" 3904 "#define B 1\n" 3905 "#include <someheader.h>\n" 3906 "#define MACRO \\\n" 3907 " some_very_long_func_aaaaaaaaaa();\n" 3908 "#endif\n" 3909 "#else\n" 3910 "#define A 1\n" 3911 "#endif", 3912 Style); 3913 Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash; 3914 verifyFormat("#ifdef _WIN32\n" 3915 "# define A 0\n" 3916 "# ifdef VAR2\n" 3917 "# define B 1\n" 3918 "# include <someheader.h>\n" 3919 "# define MACRO \\\n" 3920 " some_very_long_func_aaaaaaaaaa();\n" 3921 "# endif\n" 3922 "#else\n" 3923 "# define A 1\n" 3924 "#endif", 3925 Style); 3926 verifyFormat("#if A\n" 3927 "# define MACRO \\\n" 3928 " void a(int x) { \\\n" 3929 " b(); \\\n" 3930 " c(); \\\n" 3931 " d(); \\\n" 3932 " e(); \\\n" 3933 " f(); \\\n" 3934 " }\n" 3935 "#endif", 3936 Style); 3937 // Comments before include guard. 3938 verifyFormat("// file comment\n" 3939 "// file comment\n" 3940 "#ifndef HEADER_H\n" 3941 "#define HEADER_H\n" 3942 "code();\n" 3943 "#endif", 3944 Style); 3945 // Test with include guards. 3946 verifyFormat("#ifndef HEADER_H\n" 3947 "#define HEADER_H\n" 3948 "code();\n" 3949 "#endif", 3950 Style); 3951 // Include guards must have a #define with the same variable immediately 3952 // after #ifndef. 3953 verifyFormat("#ifndef NOT_GUARD\n" 3954 "# define FOO\n" 3955 "code();\n" 3956 "#endif", 3957 Style); 3958 3959 // Include guards must cover the entire file. 3960 verifyFormat("code();\n" 3961 "code();\n" 3962 "#ifndef NOT_GUARD\n" 3963 "# define NOT_GUARD\n" 3964 "code();\n" 3965 "#endif", 3966 Style); 3967 verifyFormat("#ifndef NOT_GUARD\n" 3968 "# define NOT_GUARD\n" 3969 "code();\n" 3970 "#endif\n" 3971 "code();", 3972 Style); 3973 // Test with trailing blank lines. 3974 verifyFormat("#ifndef HEADER_H\n" 3975 "#define HEADER_H\n" 3976 "code();\n" 3977 "#endif\n", 3978 Style); 3979 // Include guards don't have #else. 3980 verifyFormat("#ifndef NOT_GUARD\n" 3981 "# define NOT_GUARD\n" 3982 "code();\n" 3983 "#else\n" 3984 "#endif", 3985 Style); 3986 verifyFormat("#ifndef NOT_GUARD\n" 3987 "# define NOT_GUARD\n" 3988 "code();\n" 3989 "#elif FOO\n" 3990 "#endif", 3991 Style); 3992 // Non-identifier #define after potential include guard. 3993 verifyFormat("#ifndef FOO\n" 3994 "# define 1\n" 3995 "#endif\n", 3996 Style); 3997 // #if closes past last non-preprocessor line. 3998 verifyFormat("#ifndef FOO\n" 3999 "#define FOO\n" 4000 "#if 1\n" 4001 "int i;\n" 4002 "# define A 0\n" 4003 "#endif\n" 4004 "#endif\n", 4005 Style); 4006 // Don't crash if there is an #elif directive without a condition. 4007 verifyFormat("#if 1\n" 4008 "int x;\n" 4009 "#elif\n" 4010 "int y;\n" 4011 "#else\n" 4012 "int z;\n" 4013 "#endif", 4014 Style); 4015 // FIXME: This doesn't handle the case where there's code between the 4016 // #ifndef and #define but all other conditions hold. This is because when 4017 // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the 4018 // previous code line yet, so we can't detect it. 4019 EXPECT_EQ("#ifndef NOT_GUARD\n" 4020 "code();\n" 4021 "#define NOT_GUARD\n" 4022 "code();\n" 4023 "#endif", 4024 format("#ifndef NOT_GUARD\n" 4025 "code();\n" 4026 "# define NOT_GUARD\n" 4027 "code();\n" 4028 "#endif", 4029 Style)); 4030 // FIXME: This doesn't handle cases where legitimate preprocessor lines may 4031 // be outside an include guard. Examples are #pragma once and 4032 // #pragma GCC diagnostic, or anything else that does not change the meaning 4033 // of the file if it's included multiple times. 4034 EXPECT_EQ("#ifdef WIN32\n" 4035 "# pragma once\n" 4036 "#endif\n" 4037 "#ifndef HEADER_H\n" 4038 "# define HEADER_H\n" 4039 "code();\n" 4040 "#endif", 4041 format("#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 Style)); 4049 // FIXME: This does not detect when there is a single non-preprocessor line 4050 // in front of an include-guard-like structure where other conditions hold 4051 // because ScopedLineState hides the line. 4052 EXPECT_EQ("code();\n" 4053 "#ifndef HEADER_H\n" 4054 "#define HEADER_H\n" 4055 "code();\n" 4056 "#endif", 4057 format("code();\n" 4058 "#ifndef HEADER_H\n" 4059 "# define HEADER_H\n" 4060 "code();\n" 4061 "#endif", 4062 Style)); 4063 // Keep comments aligned with #, otherwise indent comments normally. These 4064 // tests cannot use verifyFormat because messUp manipulates leading 4065 // whitespace. 4066 { 4067 const char *Expected = "" 4068 "void f() {\n" 4069 "#if 1\n" 4070 "// Preprocessor aligned.\n" 4071 "# define A 0\n" 4072 " // Code. Separated by blank line.\n" 4073 "\n" 4074 "# define B 0\n" 4075 " // Code. Not aligned with #\n" 4076 "# define C 0\n" 4077 "#endif"; 4078 const char *ToFormat = "" 4079 "void f() {\n" 4080 "#if 1\n" 4081 "// Preprocessor aligned.\n" 4082 "# define A 0\n" 4083 "// Code. Separated by blank line.\n" 4084 "\n" 4085 "# define B 0\n" 4086 " // Code. Not aligned with #\n" 4087 "# define C 0\n" 4088 "#endif"; 4089 EXPECT_EQ(Expected, format(ToFormat, Style)); 4090 EXPECT_EQ(Expected, format(Expected, Style)); 4091 } 4092 // Keep block quotes aligned. 4093 { 4094 const char *Expected = "" 4095 "void f() {\n" 4096 "#if 1\n" 4097 "/* Preprocessor aligned. */\n" 4098 "# define A 0\n" 4099 " /* Code. Separated by blank line. */\n" 4100 "\n" 4101 "# define B 0\n" 4102 " /* Code. Not aligned with # */\n" 4103 "# define C 0\n" 4104 "#endif"; 4105 const char *ToFormat = "" 4106 "void f() {\n" 4107 "#if 1\n" 4108 "/* Preprocessor aligned. */\n" 4109 "# define A 0\n" 4110 "/* Code. Separated by blank line. */\n" 4111 "\n" 4112 "# define B 0\n" 4113 " /* Code. Not aligned with # */\n" 4114 "# define C 0\n" 4115 "#endif"; 4116 EXPECT_EQ(Expected, format(ToFormat, Style)); 4117 EXPECT_EQ(Expected, format(Expected, Style)); 4118 } 4119 // Keep comments aligned with un-indented directives. 4120 { 4121 const char *Expected = "" 4122 "void f() {\n" 4123 "// Preprocessor aligned.\n" 4124 "#define A 0\n" 4125 " // Code. Separated by blank line.\n" 4126 "\n" 4127 "#define B 0\n" 4128 " // Code. Not aligned with #\n" 4129 "#define C 0\n"; 4130 const char *ToFormat = "" 4131 "void f() {\n" 4132 "// Preprocessor aligned.\n" 4133 "#define A 0\n" 4134 "// Code. Separated by blank line.\n" 4135 "\n" 4136 "#define B 0\n" 4137 " // Code. Not aligned with #\n" 4138 "#define C 0\n"; 4139 EXPECT_EQ(Expected, format(ToFormat, Style)); 4140 EXPECT_EQ(Expected, format(Expected, Style)); 4141 } 4142 // Test AfterHash with tabs. 4143 { 4144 FormatStyle Tabbed = Style; 4145 Tabbed.UseTab = FormatStyle::UT_Always; 4146 Tabbed.IndentWidth = 8; 4147 Tabbed.TabWidth = 8; 4148 verifyFormat("#ifdef _WIN32\n" 4149 "#\tdefine A 0\n" 4150 "#\tifdef VAR2\n" 4151 "#\t\tdefine B 1\n" 4152 "#\t\tinclude <someheader.h>\n" 4153 "#\t\tdefine MACRO \\\n" 4154 "\t\t\tsome_very_long_func_aaaaaaaaaa();\n" 4155 "#\tendif\n" 4156 "#else\n" 4157 "#\tdefine A 1\n" 4158 "#endif", 4159 Tabbed); 4160 } 4161 4162 // Regression test: Multiline-macro inside include guards. 4163 verifyFormat("#ifndef HEADER_H\n" 4164 "#define HEADER_H\n" 4165 "#define A() \\\n" 4166 " int i; \\\n" 4167 " int j;\n" 4168 "#endif // HEADER_H", 4169 getLLVMStyleWithColumns(20)); 4170 4171 Style.IndentPPDirectives = FormatStyle::PPDIS_BeforeHash; 4172 // Basic before hash indent tests 4173 verifyFormat("#ifdef _WIN32\n" 4174 " #define A 0\n" 4175 " #ifdef VAR2\n" 4176 " #define B 1\n" 4177 " #include <someheader.h>\n" 4178 " #define MACRO \\\n" 4179 " some_very_long_func_aaaaaaaaaa();\n" 4180 " #endif\n" 4181 "#else\n" 4182 " #define A 1\n" 4183 "#endif", 4184 Style); 4185 verifyFormat("#if A\n" 4186 " #define MACRO \\\n" 4187 " void a(int x) { \\\n" 4188 " b(); \\\n" 4189 " c(); \\\n" 4190 " d(); \\\n" 4191 " e(); \\\n" 4192 " f(); \\\n" 4193 " }\n" 4194 "#endif", 4195 Style); 4196 // Keep comments aligned with indented directives. These 4197 // tests cannot use verifyFormat because messUp manipulates leading 4198 // whitespace. 4199 { 4200 const char *Expected = "void f() {\n" 4201 "// Aligned to preprocessor.\n" 4202 "#if 1\n" 4203 " // Aligned to code.\n" 4204 " int a;\n" 4205 " #if 1\n" 4206 " // Aligned to preprocessor.\n" 4207 " #define A 0\n" 4208 " // Aligned to code.\n" 4209 " int b;\n" 4210 " #endif\n" 4211 "#endif\n" 4212 "}"; 4213 const char *ToFormat = "void f() {\n" 4214 "// Aligned to preprocessor.\n" 4215 "#if 1\n" 4216 "// Aligned to code.\n" 4217 "int a;\n" 4218 "#if 1\n" 4219 "// Aligned to preprocessor.\n" 4220 "#define A 0\n" 4221 "// Aligned to code.\n" 4222 "int b;\n" 4223 "#endif\n" 4224 "#endif\n" 4225 "}"; 4226 EXPECT_EQ(Expected, format(ToFormat, Style)); 4227 EXPECT_EQ(Expected, format(Expected, Style)); 4228 } 4229 { 4230 const char *Expected = "void f() {\n" 4231 "/* Aligned to preprocessor. */\n" 4232 "#if 1\n" 4233 " /* Aligned to code. */\n" 4234 " int a;\n" 4235 " #if 1\n" 4236 " /* Aligned to preprocessor. */\n" 4237 " #define A 0\n" 4238 " /* Aligned to code. */\n" 4239 " int b;\n" 4240 " #endif\n" 4241 "#endif\n" 4242 "}"; 4243 const char *ToFormat = "void f() {\n" 4244 "/* Aligned to preprocessor. */\n" 4245 "#if 1\n" 4246 "/* Aligned to code. */\n" 4247 "int a;\n" 4248 "#if 1\n" 4249 "/* Aligned to preprocessor. */\n" 4250 "#define A 0\n" 4251 "/* Aligned to code. */\n" 4252 "int b;\n" 4253 "#endif\n" 4254 "#endif\n" 4255 "}"; 4256 EXPECT_EQ(Expected, format(ToFormat, Style)); 4257 EXPECT_EQ(Expected, format(Expected, Style)); 4258 } 4259 4260 // Test single comment before preprocessor 4261 verifyFormat("// Comment\n" 4262 "\n" 4263 "#if 1\n" 4264 "#endif", 4265 Style); 4266 } 4267 4268 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { 4269 verifyFormat("{\n { a #c; }\n}"); 4270 } 4271 4272 TEST_F(FormatTest, FormatUnbalancedStructuralElements) { 4273 EXPECT_EQ("#define A \\\n { \\\n {\nint i;", 4274 format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); 4275 EXPECT_EQ("#define A \\\n } \\\n }\nint i;", 4276 format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); 4277 } 4278 4279 TEST_F(FormatTest, EscapedNewlines) { 4280 FormatStyle Narrow = getLLVMStyleWithColumns(11); 4281 EXPECT_EQ("#define A \\\n int i; \\\n int j;", 4282 format("#define A \\\nint i;\\\n int j;", Narrow)); 4283 EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;")); 4284 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 4285 EXPECT_EQ("/* \\ \\ \\\n */", format("\\\n/* \\ \\ \\\n */")); 4286 EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>")); 4287 4288 FormatStyle AlignLeft = getLLVMStyle(); 4289 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 4290 EXPECT_EQ("#define MACRO(x) \\\n" 4291 "private: \\\n" 4292 " int x(int a);\n", 4293 format("#define MACRO(x) \\\n" 4294 "private: \\\n" 4295 " int x(int a);\n", 4296 AlignLeft)); 4297 4298 // CRLF line endings 4299 EXPECT_EQ("#define A \\\r\n int i; \\\r\n int j;", 4300 format("#define A \\\r\nint i;\\\r\n int j;", Narrow)); 4301 EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;")); 4302 EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();")); 4303 EXPECT_EQ("/* \\ \\ \\\r\n */", format("\\\r\n/* \\ \\ \\\r\n */")); 4304 EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>")); 4305 EXPECT_EQ("#define MACRO(x) \\\r\n" 4306 "private: \\\r\n" 4307 " int x(int a);\r\n", 4308 format("#define MACRO(x) \\\r\n" 4309 "private: \\\r\n" 4310 " int x(int a);\r\n", 4311 AlignLeft)); 4312 4313 FormatStyle DontAlign = getLLVMStyle(); 4314 DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 4315 DontAlign.MaxEmptyLinesToKeep = 3; 4316 // FIXME: can't use verifyFormat here because the newline before 4317 // "public:" is not inserted the first time it's reformatted 4318 EXPECT_EQ("#define A \\\n" 4319 " class Foo { \\\n" 4320 " void bar(); \\\n" 4321 "\\\n" 4322 "\\\n" 4323 "\\\n" 4324 " public: \\\n" 4325 " void baz(); \\\n" 4326 " };", 4327 format("#define A \\\n" 4328 " class Foo { \\\n" 4329 " void bar(); \\\n" 4330 "\\\n" 4331 "\\\n" 4332 "\\\n" 4333 " public: \\\n" 4334 " void baz(); \\\n" 4335 " };", 4336 DontAlign)); 4337 } 4338 4339 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { 4340 verifyFormat("#define A \\\n" 4341 " int v( \\\n" 4342 " a); \\\n" 4343 " int i;", 4344 getLLVMStyleWithColumns(11)); 4345 } 4346 4347 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { 4348 EXPECT_EQ( 4349 "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" 4350 " \\\n" 4351 " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 4352 "\n" 4353 "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 4354 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", 4355 format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" 4356 "\\\n" 4357 "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" 4358 " \n" 4359 " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" 4360 " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); 4361 } 4362 4363 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { 4364 EXPECT_EQ("int\n" 4365 "#define A\n" 4366 " a;", 4367 format("int\n#define A\na;")); 4368 verifyFormat("functionCallTo(\n" 4369 " someOtherFunction(\n" 4370 " withSomeParameters, whichInSequence,\n" 4371 " areLongerThanALine(andAnotherCall,\n" 4372 "#define A B\n" 4373 " withMoreParamters,\n" 4374 " whichStronglyInfluenceTheLayout),\n" 4375 " andMoreParameters),\n" 4376 " trailing);", 4377 getLLVMStyleWithColumns(69)); 4378 verifyFormat("Foo::Foo()\n" 4379 "#ifdef BAR\n" 4380 " : baz(0)\n" 4381 "#endif\n" 4382 "{\n" 4383 "}"); 4384 verifyFormat("void f() {\n" 4385 " if (true)\n" 4386 "#ifdef A\n" 4387 " f(42);\n" 4388 " x();\n" 4389 "#else\n" 4390 " g();\n" 4391 " x();\n" 4392 "#endif\n" 4393 "}"); 4394 verifyFormat("void f(param1, param2,\n" 4395 " param3,\n" 4396 "#ifdef A\n" 4397 " param4(param5,\n" 4398 "#ifdef A1\n" 4399 " param6,\n" 4400 "#ifdef A2\n" 4401 " param7),\n" 4402 "#else\n" 4403 " param8),\n" 4404 " param9,\n" 4405 "#endif\n" 4406 " param10,\n" 4407 "#endif\n" 4408 " param11)\n" 4409 "#else\n" 4410 " param12)\n" 4411 "#endif\n" 4412 "{\n" 4413 " x();\n" 4414 "}", 4415 getLLVMStyleWithColumns(28)); 4416 verifyFormat("#if 1\n" 4417 "int i;"); 4418 verifyFormat("#if 1\n" 4419 "#endif\n" 4420 "#if 1\n" 4421 "#else\n" 4422 "#endif\n"); 4423 verifyFormat("DEBUG({\n" 4424 " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4425 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" 4426 "});\n" 4427 "#if a\n" 4428 "#else\n" 4429 "#endif"); 4430 4431 verifyIncompleteFormat("void f(\n" 4432 "#if A\n" 4433 ");\n" 4434 "#else\n" 4435 "#endif"); 4436 } 4437 4438 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) { 4439 verifyFormat("#endif\n" 4440 "#if B"); 4441 } 4442 4443 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { 4444 FormatStyle SingleLine = getLLVMStyle(); 4445 SingleLine.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; 4446 verifyFormat("#if 0\n" 4447 "#elif 1\n" 4448 "#endif\n" 4449 "void foo() {\n" 4450 " if (test) foo2();\n" 4451 "}", 4452 SingleLine); 4453 } 4454 4455 TEST_F(FormatTest, LayoutBlockInsideParens) { 4456 verifyFormat("functionCall({ int i; });"); 4457 verifyFormat("functionCall({\n" 4458 " int i;\n" 4459 " int j;\n" 4460 "});"); 4461 verifyFormat("functionCall(\n" 4462 " {\n" 4463 " int i;\n" 4464 " int j;\n" 4465 " },\n" 4466 " aaaa, bbbb, cccc);"); 4467 verifyFormat("functionA(functionB({\n" 4468 " int i;\n" 4469 " int j;\n" 4470 " }),\n" 4471 " aaaa, bbbb, cccc);"); 4472 verifyFormat("functionCall(\n" 4473 " {\n" 4474 " int i;\n" 4475 " int j;\n" 4476 " },\n" 4477 " aaaa, bbbb, // comment\n" 4478 " cccc);"); 4479 verifyFormat("functionA(functionB({\n" 4480 " int i;\n" 4481 " int j;\n" 4482 " }),\n" 4483 " aaaa, bbbb, // comment\n" 4484 " cccc);"); 4485 verifyFormat("functionCall(aaaa, bbbb, { int i; });"); 4486 verifyFormat("functionCall(aaaa, bbbb, {\n" 4487 " int i;\n" 4488 " int j;\n" 4489 "});"); 4490 verifyFormat( 4491 "Aaa(\n" // FIXME: There shouldn't be a linebreak here. 4492 " {\n" 4493 " int i; // break\n" 4494 " },\n" 4495 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 4496 " ccccccccccccccccc));"); 4497 verifyFormat("DEBUG({\n" 4498 " if (a)\n" 4499 " f();\n" 4500 "});"); 4501 } 4502 4503 TEST_F(FormatTest, LayoutBlockInsideStatement) { 4504 EXPECT_EQ("SOME_MACRO { int i; }\n" 4505 "int i;", 4506 format(" SOME_MACRO {int i;} int i;")); 4507 } 4508 4509 TEST_F(FormatTest, LayoutNestedBlocks) { 4510 verifyFormat("void AddOsStrings(unsigned bitmask) {\n" 4511 " struct s {\n" 4512 " int i;\n" 4513 " };\n" 4514 " s kBitsToOs[] = {{10}};\n" 4515 " for (int i = 0; i < 10; ++i)\n" 4516 " return;\n" 4517 "}"); 4518 verifyFormat("call(parameter, {\n" 4519 " something();\n" 4520 " // Comment using all columns.\n" 4521 " somethingelse();\n" 4522 "});", 4523 getLLVMStyleWithColumns(40)); 4524 verifyFormat("DEBUG( //\n" 4525 " { f(); }, a);"); 4526 verifyFormat("DEBUG( //\n" 4527 " {\n" 4528 " f(); //\n" 4529 " },\n" 4530 " a);"); 4531 4532 EXPECT_EQ("call(parameter, {\n" 4533 " something();\n" 4534 " // Comment too\n" 4535 " // looooooooooong.\n" 4536 " somethingElse();\n" 4537 "});", 4538 format("call(parameter, {\n" 4539 " something();\n" 4540 " // Comment too looooooooooong.\n" 4541 " somethingElse();\n" 4542 "});", 4543 getLLVMStyleWithColumns(29))); 4544 EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });")); 4545 EXPECT_EQ("DEBUG({ // comment\n" 4546 " int i;\n" 4547 "});", 4548 format("DEBUG({ // comment\n" 4549 "int i;\n" 4550 "});")); 4551 EXPECT_EQ("DEBUG({\n" 4552 " int i;\n" 4553 "\n" 4554 " // comment\n" 4555 " int j;\n" 4556 "});", 4557 format("DEBUG({\n" 4558 " int i;\n" 4559 "\n" 4560 " // comment\n" 4561 " int j;\n" 4562 "});")); 4563 4564 verifyFormat("DEBUG({\n" 4565 " if (a)\n" 4566 " return;\n" 4567 "});"); 4568 verifyGoogleFormat("DEBUG({\n" 4569 " if (a) return;\n" 4570 "});"); 4571 FormatStyle Style = getGoogleStyle(); 4572 Style.ColumnLimit = 45; 4573 verifyFormat("Debug(\n" 4574 " aaaaa,\n" 4575 " {\n" 4576 " if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n" 4577 " },\n" 4578 " a);", 4579 Style); 4580 4581 verifyFormat("SomeFunction({MACRO({ return output; }), b});"); 4582 4583 verifyNoCrash("^{v^{a}}"); 4584 } 4585 4586 TEST_F(FormatTest, FormatNestedBlocksInMacros) { 4587 EXPECT_EQ("#define MACRO() \\\n" 4588 " Debug(aaa, /* force line break */ \\\n" 4589 " { \\\n" 4590 " int i; \\\n" 4591 " int j; \\\n" 4592 " })", 4593 format("#define MACRO() Debug(aaa, /* force line break */ \\\n" 4594 " { int i; int j; })", 4595 getGoogleStyle())); 4596 4597 EXPECT_EQ("#define A \\\n" 4598 " [] { \\\n" 4599 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 4600 " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" 4601 " }", 4602 format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" 4603 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", 4604 getGoogleStyle())); 4605 } 4606 4607 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { 4608 EXPECT_EQ("{}", format("{}")); 4609 verifyFormat("enum E {};"); 4610 verifyFormat("enum E {}"); 4611 FormatStyle Style = getLLVMStyle(); 4612 Style.SpaceInEmptyBlock = true; 4613 EXPECT_EQ("void f() { }", format("void f() {}", Style)); 4614 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 4615 EXPECT_EQ("while (true) { }", format("while (true) {}", Style)); 4616 } 4617 4618 TEST_F(FormatTest, FormatBeginBlockEndMacros) { 4619 FormatStyle Style = getLLVMStyle(); 4620 Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$"; 4621 Style.MacroBlockEnd = "^[A-Z_]+_END$"; 4622 verifyFormat("FOO_BEGIN\n" 4623 " FOO_ENTRY\n" 4624 "FOO_END", 4625 Style); 4626 verifyFormat("FOO_BEGIN\n" 4627 " NESTED_FOO_BEGIN\n" 4628 " NESTED_FOO_ENTRY\n" 4629 " NESTED_FOO_END\n" 4630 "FOO_END", 4631 Style); 4632 verifyFormat("FOO_BEGIN(Foo, Bar)\n" 4633 " int x;\n" 4634 " x = 1;\n" 4635 "FOO_END(Baz)", 4636 Style); 4637 } 4638 4639 //===----------------------------------------------------------------------===// 4640 // Line break tests. 4641 //===----------------------------------------------------------------------===// 4642 4643 TEST_F(FormatTest, PreventConfusingIndents) { 4644 verifyFormat( 4645 "void f() {\n" 4646 " SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" 4647 " parameter, parameter, parameter)),\n" 4648 " SecondLongCall(parameter));\n" 4649 "}"); 4650 verifyFormat( 4651 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4652 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 4653 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 4654 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 4655 verifyFormat( 4656 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4657 " [aaaaaaaaaaaaaaaaaaaaaaaa\n" 4658 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 4659 " [aaaaaaaaaaaaaaaaaaaaaaaa]];"); 4660 verifyFormat( 4661 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 4662 " aaaaaaaaaaaaaaaaaaaaaaaa<\n" 4663 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" 4664 " aaaaaaaaaaaaaaaaaaaaaaaa>;"); 4665 verifyFormat("int a = bbbb && ccc &&\n" 4666 " fffff(\n" 4667 "#define A Just forcing a new line\n" 4668 " ddd);"); 4669 } 4670 4671 TEST_F(FormatTest, LineBreakingInBinaryExpressions) { 4672 verifyFormat( 4673 "bool aaaaaaa =\n" 4674 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n" 4675 " bbbbbbbb();"); 4676 verifyFormat( 4677 "bool aaaaaaa =\n" 4678 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n" 4679 " bbbbbbbb();"); 4680 4681 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 4682 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n" 4683 " ccccccccc == ddddddddddd;"); 4684 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n" 4685 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n" 4686 " ccccccccc == ddddddddddd;"); 4687 verifyFormat( 4688 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 4689 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n" 4690 " ccccccccc == ddddddddddd;"); 4691 4692 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 4693 " aaaaaa) &&\n" 4694 " bbbbbb && cccccc;"); 4695 verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" 4696 " aaaaaa) >>\n" 4697 " bbbbbb;"); 4698 verifyFormat("aa = Whitespaces.addUntouchableComment(\n" 4699 " SourceMgr.getSpellingColumnNumber(\n" 4700 " TheLine.Last->FormatTok.Tok.getLocation()) -\n" 4701 " 1);"); 4702 4703 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4704 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n" 4705 " cccccc) {\n}"); 4706 verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4707 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 4708 " cccccc) {\n}"); 4709 verifyFormat("if CONSTEXPR ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4710 " bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n" 4711 " cccccc) {\n}"); 4712 verifyFormat("b = a &&\n" 4713 " // Comment\n" 4714 " b.c && d;"); 4715 4716 // If the LHS of a comparison is not a binary expression itself, the 4717 // additional linebreak confuses many people. 4718 verifyFormat( 4719 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4720 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n" 4721 "}"); 4722 verifyFormat( 4723 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4724 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 4725 "}"); 4726 verifyFormat( 4727 "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n" 4728 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 4729 "}"); 4730 verifyFormat( 4731 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4732 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) <=> 5) {\n" 4733 "}"); 4734 // Even explicit parentheses stress the precedence enough to make the 4735 // additional break unnecessary. 4736 verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4737 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n" 4738 "}"); 4739 // This cases is borderline, but with the indentation it is still readable. 4740 verifyFormat( 4741 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 4742 " aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4743 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 4744 "}", 4745 getLLVMStyleWithColumns(75)); 4746 4747 // If the LHS is a binary expression, we should still use the additional break 4748 // as otherwise the formatting hides the operator precedence. 4749 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4750 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 4751 " 5) {\n" 4752 "}"); 4753 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4754 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <=>\n" 4755 " 5) {\n" 4756 "}"); 4757 4758 FormatStyle OnePerLine = getLLVMStyle(); 4759 OnePerLine.BinPackParameters = false; 4760 verifyFormat( 4761 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4762 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 4763 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}", 4764 OnePerLine); 4765 4766 verifyFormat("int i = someFunction(aaaaaaa, 0)\n" 4767 " .aaa(aaaaaaaaaaaaa) *\n" 4768 " aaaaaaa +\n" 4769 " aaaaaaa;", 4770 getLLVMStyleWithColumns(40)); 4771 } 4772 4773 TEST_F(FormatTest, ExpressionIndentation) { 4774 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4775 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4776 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 4777 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 4778 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 4779 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" 4780 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 4781 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" 4782 " ccccccccccccccccccccccccccccccccccccccccc;"); 4783 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 4784 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4785 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 4786 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 4787 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4788 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 4789 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 4790 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 4791 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" 4792 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" 4793 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 4794 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); 4795 verifyFormat("if () {\n" 4796 "} else if (aaaaa && bbbbb > // break\n" 4797 " ccccc) {\n" 4798 "}"); 4799 verifyFormat("if () {\n" 4800 "} else if constexpr (aaaaa && bbbbb > // break\n" 4801 " ccccc) {\n" 4802 "}"); 4803 verifyFormat("if () {\n" 4804 "} else if CONSTEXPR (aaaaa && bbbbb > // break\n" 4805 " ccccc) {\n" 4806 "}"); 4807 verifyFormat("if () {\n" 4808 "} else if (aaaaa &&\n" 4809 " bbbbb > // break\n" 4810 " ccccc &&\n" 4811 " ddddd) {\n" 4812 "}"); 4813 4814 // Presence of a trailing comment used to change indentation of b. 4815 verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n" 4816 " b;\n" 4817 "return aaaaaaaaaaaaaaaaaaa +\n" 4818 " b; //", 4819 getLLVMStyleWithColumns(30)); 4820 } 4821 4822 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { 4823 // Not sure what the best system is here. Like this, the LHS can be found 4824 // immediately above an operator (everything with the same or a higher 4825 // indent). The RHS is aligned right of the operator and so compasses 4826 // everything until something with the same indent as the operator is found. 4827 // FIXME: Is this a good system? 4828 FormatStyle Style = getLLVMStyle(); 4829 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 4830 verifyFormat( 4831 "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4832 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4833 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4834 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4835 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 4836 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 4837 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4838 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4839 " > ccccccccccccccccccccccccccccccccccccccccc;", 4840 Style); 4841 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4842 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4843 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4844 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 4845 Style); 4846 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4847 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4848 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4849 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 4850 Style); 4851 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4852 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4853 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4854 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 4855 Style); 4856 verifyFormat("if () {\n" 4857 "} else if (aaaaa\n" 4858 " && bbbbb // break\n" 4859 " > ccccc) {\n" 4860 "}", 4861 Style); 4862 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4863 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 4864 Style); 4865 verifyFormat("return (a)\n" 4866 " // comment\n" 4867 " + b;", 4868 Style); 4869 verifyFormat( 4870 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4871 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 4872 " + cc;", 4873 Style); 4874 4875 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4876 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4877 Style); 4878 4879 // Forced by comments. 4880 verifyFormat( 4881 "unsigned ContentSize =\n" 4882 " sizeof(int16_t) // DWARF ARange version number\n" 4883 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 4884 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 4885 " + sizeof(int8_t); // Segment Size (in bytes)"); 4886 4887 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 4888 " == boost::fusion::at_c<1>(iiii).second;", 4889 Style); 4890 4891 Style.ColumnLimit = 60; 4892 verifyFormat("zzzzzzzzzz\n" 4893 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 4894 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 4895 Style); 4896 4897 Style.ColumnLimit = 80; 4898 Style.IndentWidth = 4; 4899 Style.TabWidth = 4; 4900 Style.UseTab = FormatStyle::UT_Always; 4901 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 4902 Style.AlignOperands = FormatStyle::OAS_DontAlign; 4903 EXPECT_EQ("return someVeryVeryLongConditionThatBarelyFitsOnALine\n" 4904 "\t&& (someOtherLongishConditionPart1\n" 4905 "\t\t|| someOtherEvenLongerNestedConditionPart2);", 4906 format("return someVeryVeryLongConditionThatBarelyFitsOnALine && " 4907 "(someOtherLongishConditionPart1 || " 4908 "someOtherEvenLongerNestedConditionPart2);", 4909 Style)); 4910 } 4911 4912 TEST_F(FormatTest, ExpressionIndentationStrictAlign) { 4913 FormatStyle Style = getLLVMStyle(); 4914 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 4915 Style.AlignOperands = FormatStyle::OAS_AlignAfterOperator; 4916 4917 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4918 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4919 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4920 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4921 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 4922 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 4923 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4924 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4925 " > ccccccccccccccccccccccccccccccccccccccccc;", 4926 Style); 4927 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4928 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4929 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4930 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 4931 Style); 4932 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4933 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4934 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4935 " == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 4936 Style); 4937 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4938 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4939 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4940 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}", 4941 Style); 4942 verifyFormat("if () {\n" 4943 "} else if (aaaaa\n" 4944 " && bbbbb // break\n" 4945 " > ccccc) {\n" 4946 "}", 4947 Style); 4948 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4949 " && bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 4950 Style); 4951 verifyFormat("return (a)\n" 4952 " // comment\n" 4953 " + b;", 4954 Style); 4955 verifyFormat( 4956 "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4957 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 4958 " + cc;", 4959 Style); 4960 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 4961 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 4962 " : 3333333333333333;", 4963 Style); 4964 verifyFormat( 4965 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 4966 " : ccccccccccccccc ? dddddddddddddddddd\n" 4967 " : eeeeeeeeeeeeeeeeee)\n" 4968 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 4969 " : 3333333333333333;", 4970 Style); 4971 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 4972 " = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 4973 Style); 4974 4975 verifyFormat("return boost::fusion::at_c<0>(iiii).second\n" 4976 " == boost::fusion::at_c<1>(iiii).second;", 4977 Style); 4978 4979 Style.ColumnLimit = 60; 4980 verifyFormat("zzzzzzzzzzzzz\n" 4981 " = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 4982 " >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);", 4983 Style); 4984 4985 // Forced by comments. 4986 Style.ColumnLimit = 80; 4987 verifyFormat( 4988 "unsigned ContentSize\n" 4989 " = sizeof(int16_t) // DWARF ARange version number\n" 4990 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 4991 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 4992 " + sizeof(int8_t); // Segment Size (in bytes)", 4993 Style); 4994 4995 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 4996 verifyFormat( 4997 "unsigned ContentSize =\n" 4998 " sizeof(int16_t) // DWARF ARange version number\n" 4999 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 5000 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 5001 " + sizeof(int8_t); // Segment Size (in bytes)", 5002 Style); 5003 5004 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 5005 verifyFormat( 5006 "unsigned ContentSize =\n" 5007 " sizeof(int16_t) // DWARF ARange version number\n" 5008 " + sizeof(int32_t) // Offset of CU in the .debug_info section\n" 5009 " + sizeof(int8_t) // Pointer Size (in bytes)\n" 5010 " + sizeof(int8_t); // Segment Size (in bytes)", 5011 Style); 5012 } 5013 5014 TEST_F(FormatTest, EnforcedOperatorWraps) { 5015 // Here we'd like to wrap after the || operators, but a comment is forcing an 5016 // earlier wrap. 5017 verifyFormat("bool x = aaaaa //\n" 5018 " || bbbbb\n" 5019 " //\n" 5020 " || cccc;"); 5021 } 5022 5023 TEST_F(FormatTest, NoOperandAlignment) { 5024 FormatStyle Style = getLLVMStyle(); 5025 Style.AlignOperands = FormatStyle::OAS_DontAlign; 5026 verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n" 5027 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 5028 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 5029 Style); 5030 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 5031 verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5032 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5033 " + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5034 " == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5035 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5036 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5037 " && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5038 " * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5039 " > ccccccccccccccccccccccccccccccccccccccccc;", 5040 Style); 5041 5042 verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5043 " * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5044 " + cc;", 5045 Style); 5046 verifyFormat("int a = aa\n" 5047 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" 5048 " * cccccccccccccccccccccccccccccccccccc;\n", 5049 Style); 5050 5051 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5052 verifyFormat("return (a > b\n" 5053 " // comment1\n" 5054 " // comment2\n" 5055 " || c);", 5056 Style); 5057 } 5058 5059 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) { 5060 FormatStyle Style = getLLVMStyle(); 5061 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 5062 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 5063 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5064 " + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 5065 Style); 5066 } 5067 5068 TEST_F(FormatTest, AllowBinPackingInsideArguments) { 5069 FormatStyle Style = getLLVMStyle(); 5070 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment; 5071 Style.BinPackArguments = false; 5072 Style.ColumnLimit = 40; 5073 verifyFormat("void test() {\n" 5074 " someFunction(\n" 5075 " this + argument + is + quite\n" 5076 " + long + so + it + gets + wrapped\n" 5077 " + but + remains + bin - packed);\n" 5078 "}", 5079 Style); 5080 verifyFormat("void test() {\n" 5081 " someFunction(arg1,\n" 5082 " this + argument + is\n" 5083 " + quite + long + so\n" 5084 " + it + gets + wrapped\n" 5085 " + but + remains + bin\n" 5086 " - packed,\n" 5087 " arg3);\n" 5088 "}", 5089 Style); 5090 verifyFormat("void test() {\n" 5091 " someFunction(\n" 5092 " arg1,\n" 5093 " this + argument + has\n" 5094 " + anotherFunc(nested,\n" 5095 " calls + whose\n" 5096 " + arguments\n" 5097 " + are + also\n" 5098 " + wrapped,\n" 5099 " in + addition)\n" 5100 " + to + being + bin - packed,\n" 5101 " arg3);\n" 5102 "}", 5103 Style); 5104 5105 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 5106 verifyFormat("void test() {\n" 5107 " someFunction(\n" 5108 " arg1,\n" 5109 " this + argument + has +\n" 5110 " anotherFunc(nested,\n" 5111 " calls + whose +\n" 5112 " arguments +\n" 5113 " are + also +\n" 5114 " wrapped,\n" 5115 " in + addition) +\n" 5116 " to + being + bin - packed,\n" 5117 " arg3);\n" 5118 "}", 5119 Style); 5120 } 5121 5122 TEST_F(FormatTest, ConstructorInitializers) { 5123 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 5124 verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", 5125 getLLVMStyleWithColumns(45)); 5126 verifyFormat("Constructor()\n" 5127 " : Inttializer(FitsOnTheLine) {}", 5128 getLLVMStyleWithColumns(44)); 5129 verifyFormat("Constructor()\n" 5130 " : Inttializer(FitsOnTheLine) {}", 5131 getLLVMStyleWithColumns(43)); 5132 5133 verifyFormat("template <typename T>\n" 5134 "Constructor() : Initializer(FitsOnTheLine) {}", 5135 getLLVMStyleWithColumns(45)); 5136 5137 verifyFormat( 5138 "SomeClass::Constructor()\n" 5139 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 5140 5141 verifyFormat( 5142 "SomeClass::Constructor()\n" 5143 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 5144 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); 5145 verifyFormat( 5146 "SomeClass::Constructor()\n" 5147 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5148 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); 5149 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5150 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5151 " : aaaaaaaaaa(aaaaaa) {}"); 5152 5153 verifyFormat("Constructor()\n" 5154 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5155 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5156 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5157 " aaaaaaaaaaaaaaaaaaaaaaa() {}"); 5158 5159 verifyFormat("Constructor()\n" 5160 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5161 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 5162 5163 verifyFormat("Constructor(int Parameter = 0)\n" 5164 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 5165 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); 5166 verifyFormat("Constructor()\n" 5167 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 5168 "}", 5169 getLLVMStyleWithColumns(60)); 5170 verifyFormat("Constructor()\n" 5171 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5172 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}"); 5173 5174 // Here a line could be saved by splitting the second initializer onto two 5175 // lines, but that is not desirable. 5176 verifyFormat("Constructor()\n" 5177 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 5178 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 5179 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 5180 5181 FormatStyle OnePerLine = getLLVMStyle(); 5182 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 5183 OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false; 5184 verifyFormat("SomeClass::Constructor()\n" 5185 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 5186 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 5187 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 5188 OnePerLine); 5189 verifyFormat("SomeClass::Constructor()\n" 5190 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 5191 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 5192 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 5193 OnePerLine); 5194 verifyFormat("MyClass::MyClass(int var)\n" 5195 " : some_var_(var), // 4 space indent\n" 5196 " some_other_var_(var + 1) { // lined up\n" 5197 "}", 5198 OnePerLine); 5199 verifyFormat("Constructor()\n" 5200 " : aaaaa(aaaaaa),\n" 5201 " aaaaa(aaaaaa),\n" 5202 " aaaaa(aaaaaa),\n" 5203 " aaaaa(aaaaaa),\n" 5204 " aaaaa(aaaaaa) {}", 5205 OnePerLine); 5206 verifyFormat("Constructor()\n" 5207 " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 5208 " aaaaaaaaaaaaaaaaaaaaaa) {}", 5209 OnePerLine); 5210 OnePerLine.BinPackParameters = false; 5211 verifyFormat( 5212 "Constructor()\n" 5213 " : aaaaaaaaaaaaaaaaaaaaaaaa(\n" 5214 " aaaaaaaaaaa().aaa(),\n" 5215 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 5216 OnePerLine); 5217 OnePerLine.ColumnLimit = 60; 5218 verifyFormat("Constructor()\n" 5219 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 5220 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 5221 OnePerLine); 5222 5223 EXPECT_EQ("Constructor()\n" 5224 " : // Comment forcing unwanted break.\n" 5225 " aaaa(aaaa) {}", 5226 format("Constructor() :\n" 5227 " // Comment forcing unwanted break.\n" 5228 " aaaa(aaaa) {}")); 5229 } 5230 5231 TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) { 5232 FormatStyle Style = getLLVMStyle(); 5233 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 5234 Style.ColumnLimit = 60; 5235 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 5236 Style.AllowAllConstructorInitializersOnNextLine = true; 5237 Style.BinPackParameters = false; 5238 5239 for (int i = 0; i < 4; ++i) { 5240 // Test all combinations of parameters that should not have an effect. 5241 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1; 5242 Style.AllowAllArgumentsOnNextLine = i & 2; 5243 5244 Style.AllowAllConstructorInitializersOnNextLine = true; 5245 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 5246 verifyFormat("Constructor()\n" 5247 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 5248 Style); 5249 verifyFormat("Constructor() : a(a), b(b) {}", Style); 5250 5251 Style.AllowAllConstructorInitializersOnNextLine = false; 5252 verifyFormat("Constructor()\n" 5253 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 5254 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 5255 Style); 5256 verifyFormat("Constructor() : a(a), b(b) {}", Style); 5257 5258 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 5259 Style.AllowAllConstructorInitializersOnNextLine = true; 5260 verifyFormat("Constructor()\n" 5261 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 5262 Style); 5263 5264 Style.AllowAllConstructorInitializersOnNextLine = false; 5265 verifyFormat("Constructor()\n" 5266 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 5267 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 5268 Style); 5269 5270 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 5271 Style.AllowAllConstructorInitializersOnNextLine = true; 5272 verifyFormat("Constructor() :\n" 5273 " aaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 5274 Style); 5275 5276 Style.AllowAllConstructorInitializersOnNextLine = false; 5277 verifyFormat("Constructor() :\n" 5278 " aaaaaaaaaaaaaaaaaa(a),\n" 5279 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 5280 Style); 5281 } 5282 5283 // Test interactions between AllowAllParametersOfDeclarationOnNextLine and 5284 // AllowAllConstructorInitializersOnNextLine in all 5285 // BreakConstructorInitializers modes 5286 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 5287 Style.AllowAllParametersOfDeclarationOnNextLine = true; 5288 Style.AllowAllConstructorInitializersOnNextLine = false; 5289 verifyFormat("SomeClassWithALongName::Constructor(\n" 5290 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n" 5291 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 5292 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 5293 Style); 5294 5295 Style.AllowAllConstructorInitializersOnNextLine = true; 5296 verifyFormat("SomeClassWithALongName::Constructor(\n" 5297 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 5298 " int bbbbbbbbbbbbb,\n" 5299 " int cccccccccccccccc)\n" 5300 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 5301 Style); 5302 5303 Style.AllowAllParametersOfDeclarationOnNextLine = false; 5304 Style.AllowAllConstructorInitializersOnNextLine = false; 5305 verifyFormat("SomeClassWithALongName::Constructor(\n" 5306 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 5307 " int bbbbbbbbbbbbb)\n" 5308 " : aaaaaaaaaaaaaaaaaaaa(a)\n" 5309 " , bbbbbbbbbbbbbbbbbbbbb(b) {}", 5310 Style); 5311 5312 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 5313 5314 Style.AllowAllParametersOfDeclarationOnNextLine = true; 5315 verifyFormat("SomeClassWithALongName::Constructor(\n" 5316 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb)\n" 5317 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 5318 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 5319 Style); 5320 5321 Style.AllowAllConstructorInitializersOnNextLine = true; 5322 verifyFormat("SomeClassWithALongName::Constructor(\n" 5323 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 5324 " int bbbbbbbbbbbbb,\n" 5325 " int cccccccccccccccc)\n" 5326 " : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 5327 Style); 5328 5329 Style.AllowAllParametersOfDeclarationOnNextLine = false; 5330 Style.AllowAllConstructorInitializersOnNextLine = false; 5331 verifyFormat("SomeClassWithALongName::Constructor(\n" 5332 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 5333 " int bbbbbbbbbbbbb)\n" 5334 " : aaaaaaaaaaaaaaaaaaaa(a),\n" 5335 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 5336 Style); 5337 5338 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 5339 Style.AllowAllParametersOfDeclarationOnNextLine = true; 5340 verifyFormat("SomeClassWithALongName::Constructor(\n" 5341 " int aaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbb) :\n" 5342 " aaaaaaaaaaaaaaaaaaaa(a),\n" 5343 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 5344 Style); 5345 5346 Style.AllowAllConstructorInitializersOnNextLine = true; 5347 verifyFormat("SomeClassWithALongName::Constructor(\n" 5348 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 5349 " int bbbbbbbbbbbbb,\n" 5350 " int cccccccccccccccc) :\n" 5351 " aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbb(b) {}", 5352 Style); 5353 5354 Style.AllowAllParametersOfDeclarationOnNextLine = false; 5355 Style.AllowAllConstructorInitializersOnNextLine = false; 5356 verifyFormat("SomeClassWithALongName::Constructor(\n" 5357 " int aaaaaaaaaaaaaaaaaaaaaaaa,\n" 5358 " int bbbbbbbbbbbbb) :\n" 5359 " aaaaaaaaaaaaaaaaaaaa(a),\n" 5360 " bbbbbbbbbbbbbbbbbbbbb(b) {}", 5361 Style); 5362 } 5363 5364 TEST_F(FormatTest, AllowAllArgumentsOnNextLine) { 5365 FormatStyle Style = getLLVMStyle(); 5366 Style.ColumnLimit = 60; 5367 Style.BinPackArguments = false; 5368 for (int i = 0; i < 4; ++i) { 5369 // Test all combinations of parameters that should not have an effect. 5370 Style.AllowAllParametersOfDeclarationOnNextLine = i & 1; 5371 Style.AllowAllConstructorInitializersOnNextLine = i & 2; 5372 5373 Style.AllowAllArgumentsOnNextLine = true; 5374 verifyFormat("void foo() {\n" 5375 " FunctionCallWithReallyLongName(\n" 5376 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb);\n" 5377 "}", 5378 Style); 5379 Style.AllowAllArgumentsOnNextLine = false; 5380 verifyFormat("void foo() {\n" 5381 " FunctionCallWithReallyLongName(\n" 5382 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5383 " bbbbbbbbbbbb);\n" 5384 "}", 5385 Style); 5386 5387 Style.AllowAllArgumentsOnNextLine = true; 5388 verifyFormat("void foo() {\n" 5389 " auto VariableWithReallyLongName = {\n" 5390 " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbb};\n" 5391 "}", 5392 Style); 5393 Style.AllowAllArgumentsOnNextLine = false; 5394 verifyFormat("void foo() {\n" 5395 " auto VariableWithReallyLongName = {\n" 5396 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5397 " bbbbbbbbbbbb};\n" 5398 "}", 5399 Style); 5400 } 5401 5402 // This parameter should not affect declarations. 5403 Style.BinPackParameters = false; 5404 Style.AllowAllArgumentsOnNextLine = false; 5405 Style.AllowAllParametersOfDeclarationOnNextLine = true; 5406 verifyFormat("void FunctionCallWithReallyLongName(\n" 5407 " int aaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbb);", 5408 Style); 5409 Style.AllowAllParametersOfDeclarationOnNextLine = false; 5410 verifyFormat("void FunctionCallWithReallyLongName(\n" 5411 " int aaaaaaaaaaaaaaaaaaaaaaa,\n" 5412 " int bbbbbbbbbbbb);", 5413 Style); 5414 } 5415 5416 TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) { 5417 // Check that AllowAllArgumentsOnNextLine is respected for both BAS_DontAlign 5418 // and BAS_Align. 5419 auto Style = getLLVMStyle(); 5420 Style.ColumnLimit = 35; 5421 StringRef Input = "functionCall(paramA, paramB, paramC);\n" 5422 "void functionDecl(int A, int B, int C);"; 5423 Style.AllowAllArgumentsOnNextLine = false; 5424 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5425 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 5426 " paramC);\n" 5427 "void functionDecl(int A, int B,\n" 5428 " int C);"), 5429 format(Input, Style)); 5430 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 5431 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 5432 " paramC);\n" 5433 "void functionDecl(int A, int B,\n" 5434 " int C);"), 5435 format(Input, Style)); 5436 // However, BAS_AlwaysBreak should take precedence over 5437 // AllowAllArgumentsOnNextLine. 5438 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 5439 EXPECT_EQ(StringRef("functionCall(\n" 5440 " paramA, paramB, paramC);\n" 5441 "void functionDecl(\n" 5442 " int A, int B, int C);"), 5443 format(Input, Style)); 5444 5445 // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the 5446 // first argument. 5447 Style.AllowAllArgumentsOnNextLine = true; 5448 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 5449 EXPECT_EQ(StringRef("functionCall(\n" 5450 " paramA, paramB, paramC);\n" 5451 "void functionDecl(\n" 5452 " int A, int B, int C);"), 5453 format(Input, Style)); 5454 // It wouldn't fit on one line with aligned parameters so this setting 5455 // doesn't change anything for BAS_Align. 5456 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 5457 EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" 5458 " paramC);\n" 5459 "void functionDecl(int A, int B,\n" 5460 " int C);"), 5461 format(Input, Style)); 5462 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 5463 EXPECT_EQ(StringRef("functionCall(\n" 5464 " paramA, paramB, paramC);\n" 5465 "void functionDecl(\n" 5466 " int A, int B, int C);"), 5467 format(Input, Style)); 5468 } 5469 5470 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) { 5471 FormatStyle Style = getLLVMStyle(); 5472 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 5473 5474 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); 5475 verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}", 5476 getStyleWithColumns(Style, 45)); 5477 verifyFormat("Constructor() :\n" 5478 " Initializer(FitsOnTheLine) {}", 5479 getStyleWithColumns(Style, 44)); 5480 verifyFormat("Constructor() :\n" 5481 " Initializer(FitsOnTheLine) {}", 5482 getStyleWithColumns(Style, 43)); 5483 5484 verifyFormat("template <typename T>\n" 5485 "Constructor() : Initializer(FitsOnTheLine) {}", 5486 getStyleWithColumns(Style, 50)); 5487 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 5488 verifyFormat( 5489 "SomeClass::Constructor() :\n" 5490 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 5491 Style); 5492 5493 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = false; 5494 verifyFormat( 5495 "SomeClass::Constructor() :\n" 5496 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 5497 Style); 5498 5499 verifyFormat( 5500 "SomeClass::Constructor() :\n" 5501 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 5502 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 5503 Style); 5504 verifyFormat( 5505 "SomeClass::Constructor() :\n" 5506 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5507 " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", 5508 Style); 5509 verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5510 " aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 5511 " aaaaaaaaaa(aaaaaa) {}", 5512 Style); 5513 5514 verifyFormat("Constructor() :\n" 5515 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5516 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5517 " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 5518 " aaaaaaaaaaaaaaaaaaaaaaa() {}", 5519 Style); 5520 5521 verifyFormat("Constructor() :\n" 5522 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5523 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 5524 Style); 5525 5526 verifyFormat("Constructor(int Parameter = 0) :\n" 5527 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" 5528 " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}", 5529 Style); 5530 verifyFormat("Constructor() :\n" 5531 " aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n" 5532 "}", 5533 getStyleWithColumns(Style, 60)); 5534 verifyFormat("Constructor() :\n" 5535 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5536 " aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}", 5537 Style); 5538 5539 // Here a line could be saved by splitting the second initializer onto two 5540 // lines, but that is not desirable. 5541 verifyFormat("Constructor() :\n" 5542 " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" 5543 " aaaaaaaaaaa(aaaaaaaaaaa),\n" 5544 " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 5545 Style); 5546 5547 FormatStyle OnePerLine = Style; 5548 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 5549 OnePerLine.AllowAllConstructorInitializersOnNextLine = false; 5550 verifyFormat("SomeClass::Constructor() :\n" 5551 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 5552 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 5553 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 5554 OnePerLine); 5555 verifyFormat("SomeClass::Constructor() :\n" 5556 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" 5557 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 5558 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 5559 OnePerLine); 5560 verifyFormat("MyClass::MyClass(int var) :\n" 5561 " some_var_(var), // 4 space indent\n" 5562 " some_other_var_(var + 1) { // lined up\n" 5563 "}", 5564 OnePerLine); 5565 verifyFormat("Constructor() :\n" 5566 " aaaaa(aaaaaa),\n" 5567 " aaaaa(aaaaaa),\n" 5568 " aaaaa(aaaaaa),\n" 5569 " aaaaa(aaaaaa),\n" 5570 " aaaaa(aaaaaa) {}", 5571 OnePerLine); 5572 verifyFormat("Constructor() :\n" 5573 " aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" 5574 " aaaaaaaaaaaaaaaaaaaaaa) {}", 5575 OnePerLine); 5576 OnePerLine.BinPackParameters = false; 5577 verifyFormat("Constructor() :\n" 5578 " aaaaaaaaaaaaaaaaaaaaaaaa(\n" 5579 " aaaaaaaaaaa().aaa(),\n" 5580 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 5581 OnePerLine); 5582 OnePerLine.ColumnLimit = 60; 5583 verifyFormat("Constructor() :\n" 5584 " aaaaaaaaaaaaaaaaaaaa(a),\n" 5585 " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", 5586 OnePerLine); 5587 5588 EXPECT_EQ("Constructor() :\n" 5589 " // Comment forcing unwanted break.\n" 5590 " aaaa(aaaa) {}", 5591 format("Constructor() :\n" 5592 " // Comment forcing unwanted break.\n" 5593 " aaaa(aaaa) {}", 5594 Style)); 5595 5596 Style.ColumnLimit = 0; 5597 verifyFormat("SomeClass::Constructor() :\n" 5598 " a(a) {}", 5599 Style); 5600 verifyFormat("SomeClass::Constructor() noexcept :\n" 5601 " a(a) {}", 5602 Style); 5603 verifyFormat("SomeClass::Constructor() :\n" 5604 " a(a), b(b), c(c) {}", 5605 Style); 5606 verifyFormat("SomeClass::Constructor() :\n" 5607 " a(a) {\n" 5608 " foo();\n" 5609 " bar();\n" 5610 "}", 5611 Style); 5612 5613 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 5614 verifyFormat("SomeClass::Constructor() :\n" 5615 " a(a), b(b), c(c) {\n" 5616 "}", 5617 Style); 5618 verifyFormat("SomeClass::Constructor() :\n" 5619 " a(a) {\n" 5620 "}", 5621 Style); 5622 5623 Style.ColumnLimit = 80; 5624 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 5625 Style.ConstructorInitializerIndentWidth = 2; 5626 verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}", Style); 5627 verifyFormat("SomeClass::Constructor() :\n" 5628 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5629 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}", 5630 Style); 5631 5632 // `ConstructorInitializerIndentWidth` actually applies to InheritanceList as 5633 // well 5634 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 5635 verifyFormat( 5636 "class SomeClass\n" 5637 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5638 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 5639 Style); 5640 Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 5641 verifyFormat( 5642 "class SomeClass\n" 5643 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5644 " , public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 5645 Style); 5646 Style.BreakInheritanceList = FormatStyle::BILS_AfterColon; 5647 verifyFormat( 5648 "class SomeClass :\n" 5649 " public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5650 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 5651 Style); 5652 Style.BreakInheritanceList = FormatStyle::BILS_AfterComma; 5653 verifyFormat( 5654 "class SomeClass\n" 5655 " : public aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5656 " public bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {};", 5657 Style); 5658 } 5659 5660 #ifndef EXPENSIVE_CHECKS 5661 // Expensive checks enables libstdc++ checking which includes validating the 5662 // state of ranges used in std::priority_queue - this blows out the 5663 // runtime/scalability of the function and makes this test unacceptably slow. 5664 TEST_F(FormatTest, MemoizationTests) { 5665 // This breaks if the memoization lookup does not take \c Indent and 5666 // \c LastSpace into account. 5667 verifyFormat( 5668 "extern CFRunLoopTimerRef\n" 5669 "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" 5670 " CFTimeInterval interval, CFOptionFlags flags,\n" 5671 " CFIndex order, CFRunLoopTimerCallBack callout,\n" 5672 " CFRunLoopTimerContext *context) {}"); 5673 5674 // Deep nesting somewhat works around our memoization. 5675 verifyFormat( 5676 "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 5677 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 5678 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 5679 " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" 5680 " aaaaa())))))))))))))))))))))))))))))))))))))));", 5681 getLLVMStyleWithColumns(65)); 5682 verifyFormat( 5683 "aaaaa(\n" 5684 " aaaaa,\n" 5685 " aaaaa(\n" 5686 " aaaaa,\n" 5687 " aaaaa(\n" 5688 " aaaaa,\n" 5689 " aaaaa(\n" 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))))))))))));", 5708 getLLVMStyleWithColumns(65)); 5709 verifyFormat( 5710 "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" 5711 " a),\n" 5712 " a),\n" 5713 " a),\n" 5714 " a),\n" 5715 " a),\n" 5716 " a),\n" 5717 " 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)", 5728 getLLVMStyleWithColumns(65)); 5729 5730 // This test takes VERY long when memoization is broken. 5731 FormatStyle OnePerLine = getLLVMStyle(); 5732 OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 5733 OnePerLine.BinPackParameters = false; 5734 std::string input = "Constructor()\n" 5735 " : aaaa(a,\n"; 5736 for (unsigned i = 0, e = 80; i != e; ++i) { 5737 input += " a,\n"; 5738 } 5739 input += " a) {}"; 5740 verifyFormat(input, OnePerLine); 5741 } 5742 #endif 5743 5744 TEST_F(FormatTest, BreaksAsHighAsPossible) { 5745 verifyFormat( 5746 "void f() {\n" 5747 " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" 5748 " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" 5749 " f();\n" 5750 "}"); 5751 verifyFormat("if (Intervals[i].getRange().getFirst() <\n" 5752 " Intervals[i - 1].getRange().getLast()) {\n}"); 5753 } 5754 5755 TEST_F(FormatTest, BreaksFunctionDeclarations) { 5756 // Principially, we break function declarations in a certain order: 5757 // 1) break amongst arguments. 5758 verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" 5759 " Cccccccccccccc cccccccccccccc);"); 5760 verifyFormat("template <class TemplateIt>\n" 5761 "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n" 5762 " TemplateIt *stop) {}"); 5763 5764 // 2) break after return type. 5765 verifyFormat( 5766 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5767 "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", 5768 getGoogleStyle()); 5769 5770 // 3) break after (. 5771 verifyFormat( 5772 "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" 5773 " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", 5774 getGoogleStyle()); 5775 5776 // 4) break before after nested name specifiers. 5777 verifyFormat( 5778 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5779 "SomeClasssssssssssssssssssssssssssssssssssssss::\n" 5780 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", 5781 getGoogleStyle()); 5782 5783 // However, there are exceptions, if a sufficient amount of lines can be 5784 // saved. 5785 // FIXME: The precise cut-offs wrt. the number of saved lines might need some 5786 // more adjusting. 5787 verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 5788 " Cccccccccccccc cccccccccc,\n" 5789 " Cccccccccccccc cccccccccc,\n" 5790 " Cccccccccccccc cccccccccc,\n" 5791 " Cccccccccccccc cccccccccc);"); 5792 verifyFormat( 5793 "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5794 "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 5795 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 5796 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", 5797 getGoogleStyle()); 5798 verifyFormat( 5799 "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" 5800 " Cccccccccccccc cccccccccc,\n" 5801 " Cccccccccccccc cccccccccc,\n" 5802 " Cccccccccccccc cccccccccc,\n" 5803 " Cccccccccccccc cccccccccc,\n" 5804 " Cccccccccccccc cccccccccc,\n" 5805 " Cccccccccccccc cccccccccc);"); 5806 verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 5807 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 5808 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 5809 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" 5810 " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); 5811 5812 // Break after multi-line parameters. 5813 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5814 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5815 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5816 " bbbb bbbb);"); 5817 verifyFormat("void SomeLoooooooooooongFunction(\n" 5818 " std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 5819 " aaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 5820 " int bbbbbbbbbbbbb);"); 5821 5822 // Treat overloaded operators like other functions. 5823 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 5824 "operator>(const SomeLoooooooooooooooooooooooooogType &other);"); 5825 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 5826 "operator>>(const SomeLooooooooooooooooooooooooogType &other);"); 5827 verifyFormat("SomeLoooooooooooooooooooooooooogType\n" 5828 "operator<<(const SomeLooooooooooooooooooooooooogType &other);"); 5829 verifyGoogleFormat( 5830 "SomeLoooooooooooooooooooooooooooooogType operator>>(\n" 5831 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 5832 verifyGoogleFormat( 5833 "SomeLoooooooooooooooooooooooooooooogType operator<<(\n" 5834 " const SomeLooooooooogType &a, const SomeLooooooooogType &b);"); 5835 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5836 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 5837 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n" 5838 "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);"); 5839 verifyGoogleFormat( 5840 "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n" 5841 "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5842 " bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}"); 5843 verifyGoogleFormat("template <typename T>\n" 5844 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 5845 "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n" 5846 " aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);"); 5847 5848 FormatStyle Style = getLLVMStyle(); 5849 Style.PointerAlignment = FormatStyle::PAS_Left; 5850 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 5851 " aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}", 5852 Style); 5853 verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" 5854 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 5855 Style); 5856 } 5857 5858 TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) { 5859 // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516: 5860 // Prefer keeping `::` followed by `operator` together. 5861 EXPECT_EQ("const aaaa::bbbbbbb &\n" 5862 "ccccccccc::operator++() {\n" 5863 " stuff();\n" 5864 "}", 5865 format("const aaaa::bbbbbbb\n" 5866 "&ccccccccc::operator++() { stuff(); }", 5867 getLLVMStyleWithColumns(40))); 5868 } 5869 5870 TEST_F(FormatTest, TrailingReturnType) { 5871 verifyFormat("auto foo() -> int;\n"); 5872 // correct trailing return type spacing 5873 verifyFormat("auto operator->() -> int;\n"); 5874 verifyFormat("auto operator++(int) -> int;\n"); 5875 5876 verifyFormat("struct S {\n" 5877 " auto bar() const -> int;\n" 5878 "};"); 5879 verifyFormat("template <size_t Order, typename T>\n" 5880 "auto load_img(const std::string &filename)\n" 5881 " -> alias::tensor<Order, T, mem::tag::cpu> {}"); 5882 verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n" 5883 " -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}"); 5884 verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}"); 5885 verifyFormat("template <typename T>\n" 5886 "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n" 5887 " -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());"); 5888 5889 // Not trailing return types. 5890 verifyFormat("void f() { auto a = b->c(); }"); 5891 } 5892 5893 TEST_F(FormatTest, DeductionGuides) { 5894 verifyFormat("template <class T> A(const T &, const T &) -> A<T &>;"); 5895 verifyFormat("template <class T> explicit A(T &, T &&) -> A<T>;"); 5896 verifyFormat("template <class... Ts> S(Ts...) -> S<Ts...>;"); 5897 verifyFormat( 5898 "template <class... T>\n" 5899 "array(T &&...t) -> array<std::common_type_t<T...>, sizeof...(T)>;"); 5900 verifyFormat("template <class T> A() -> A<decltype(p->foo<3>())>;"); 5901 verifyFormat("template <class T> A() -> A<decltype(foo<traits<1>>)>;"); 5902 verifyFormat("template <class T> A() -> A<sizeof(p->foo<1>)>;"); 5903 verifyFormat("template <class T> A() -> A<(3 < 2)>;"); 5904 verifyFormat("template <class T> A() -> A<((3) < (2))>;"); 5905 verifyFormat("template <class T> x() -> x<1>;"); 5906 verifyFormat("template <class T> explicit x(T &) -> x<1>;"); 5907 5908 // Ensure not deduction guides. 5909 verifyFormat("c()->f<int>();"); 5910 verifyFormat("x()->foo<1>;"); 5911 verifyFormat("x = p->foo<3>();"); 5912 verifyFormat("x()->x<1>();"); 5913 verifyFormat("x()->x<1>;"); 5914 } 5915 5916 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { 5917 // Avoid breaking before trailing 'const' or other trailing annotations, if 5918 // they are not function-like. 5919 FormatStyle Style = getGoogleStyle(); 5920 Style.ColumnLimit = 47; 5921 verifyFormat("void someLongFunction(\n" 5922 " int someLoooooooooooooongParameter) const {\n}", 5923 getLLVMStyleWithColumns(47)); 5924 verifyFormat("LoooooongReturnType\n" 5925 "someLoooooooongFunction() const {}", 5926 getLLVMStyleWithColumns(47)); 5927 verifyFormat("LoooooongReturnType someLoooooooongFunction()\n" 5928 " const {}", 5929 Style); 5930 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 5931 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;"); 5932 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 5933 " aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;"); 5934 verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n" 5935 " aaaaa aaaaaaaaaaaaaaaaaaaa) override final;"); 5936 verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n" 5937 " aaaaaaaaaaa aaaaa) const override;"); 5938 verifyGoogleFormat( 5939 "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 5940 " const override;"); 5941 5942 // Even if the first parameter has to be wrapped. 5943 verifyFormat("void someLongFunction(\n" 5944 " int someLongParameter) const {}", 5945 getLLVMStyleWithColumns(46)); 5946 verifyFormat("void someLongFunction(\n" 5947 " int someLongParameter) const {}", 5948 Style); 5949 verifyFormat("void someLongFunction(\n" 5950 " int someLongParameter) override {}", 5951 Style); 5952 verifyFormat("void someLongFunction(\n" 5953 " int someLongParameter) OVERRIDE {}", 5954 Style); 5955 verifyFormat("void someLongFunction(\n" 5956 " int someLongParameter) final {}", 5957 Style); 5958 verifyFormat("void someLongFunction(\n" 5959 " int someLongParameter) FINAL {}", 5960 Style); 5961 verifyFormat("void someLongFunction(\n" 5962 " int parameter) const override {}", 5963 Style); 5964 5965 Style.BreakBeforeBraces = FormatStyle::BS_Allman; 5966 verifyFormat("void someLongFunction(\n" 5967 " int someLongParameter) const\n" 5968 "{\n" 5969 "}", 5970 Style); 5971 5972 Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 5973 verifyFormat("void someLongFunction(\n" 5974 " int someLongParameter) const\n" 5975 " {\n" 5976 " }", 5977 Style); 5978 5979 // Unless these are unknown annotations. 5980 verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" 5981 " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5982 " LONG_AND_UGLY_ANNOTATION;"); 5983 5984 // Breaking before function-like trailing annotations is fine to keep them 5985 // close to their arguments. 5986 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 5987 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 5988 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 5989 " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); 5990 verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" 5991 " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); 5992 verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n" 5993 " AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);"); 5994 verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});"); 5995 5996 verifyFormat( 5997 "void aaaaaaaaaaaaaaaaaa()\n" 5998 " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" 5999 " aaaaaaaaaaaaaaaaaaaaaaaaa));"); 6000 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6001 " __attribute__((unused));"); 6002 verifyGoogleFormat( 6003 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6004 " GUARDED_BY(aaaaaaaaaaaa);"); 6005 verifyGoogleFormat( 6006 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6007 " GUARDED_BY(aaaaaaaaaaaa);"); 6008 verifyGoogleFormat( 6009 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 6010 " aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6011 verifyGoogleFormat( 6012 "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n" 6013 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 6014 } 6015 6016 TEST_F(FormatTest, FunctionAnnotations) { 6017 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 6018 "int OldFunction(const string ¶meter) {}"); 6019 verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 6020 "string OldFunction(const string ¶meter) {}"); 6021 verifyFormat("template <typename T>\n" 6022 "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n" 6023 "string OldFunction(const string ¶meter) {}"); 6024 6025 // Not function annotations. 6026 verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6027 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"); 6028 verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n" 6029 " ThisIsATestWithAReallyReallyReallyReallyLongName) {}"); 6030 verifyFormat("MACRO(abc).function() // wrap\n" 6031 " << abc;"); 6032 verifyFormat("MACRO(abc)->function() // wrap\n" 6033 " << abc;"); 6034 verifyFormat("MACRO(abc)::function() // wrap\n" 6035 " << abc;"); 6036 } 6037 6038 TEST_F(FormatTest, BreaksDesireably) { 6039 verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 6040 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" 6041 " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); 6042 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6043 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" 6044 "}"); 6045 6046 verifyFormat( 6047 "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6048 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); 6049 6050 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6051 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6052 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 6053 6054 verifyFormat( 6055 "aaaaaaaa(aaaaaaaaaaaaa,\n" 6056 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6057 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 6058 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6059 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); 6060 6061 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 6062 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6063 6064 verifyFormat( 6065 "void f() {\n" 6066 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" 6067 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 6068 "}"); 6069 verifyFormat( 6070 "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6071 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 6072 verifyFormat( 6073 "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6074 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 6075 verifyFormat( 6076 "aaaaaa(aaa,\n" 6077 " new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6078 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6079 " aaaa);"); 6080 verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6081 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6082 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6083 6084 // Indent consistently independent of call expression and unary operator. 6085 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 6086 " dddddddddddddddddddddddddddddd));"); 6087 verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 6088 " dddddddddddddddddddddddddddddd));"); 6089 verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n" 6090 " dddddddddddddddddddddddddddddd));"); 6091 6092 // This test case breaks on an incorrect memoization, i.e. an optimization not 6093 // taking into account the StopAt value. 6094 verifyFormat( 6095 "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 6096 " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 6097 " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" 6098 " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6099 6100 verifyFormat("{\n {\n {\n" 6101 " Annotation.SpaceRequiredBefore =\n" 6102 " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" 6103 " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" 6104 " }\n }\n}"); 6105 6106 // Break on an outer level if there was a break on an inner level. 6107 EXPECT_EQ("f(g(h(a, // comment\n" 6108 " b, c),\n" 6109 " d, e),\n" 6110 " x, y);", 6111 format("f(g(h(a, // comment\n" 6112 " b, c), d, e), x, y);")); 6113 6114 // Prefer breaking similar line breaks. 6115 verifyFormat( 6116 "const int kTrackingOptions = NSTrackingMouseMoved |\n" 6117 " NSTrackingMouseEnteredAndExited |\n" 6118 " NSTrackingActiveAlways;"); 6119 } 6120 6121 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) { 6122 FormatStyle NoBinPacking = getGoogleStyle(); 6123 NoBinPacking.BinPackParameters = false; 6124 NoBinPacking.BinPackArguments = true; 6125 verifyFormat("void f() {\n" 6126 " f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n" 6127 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 6128 "}", 6129 NoBinPacking); 6130 verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n" 6131 " int aaaaaaaaaaaaaaaaaaaa,\n" 6132 " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6133 NoBinPacking); 6134 6135 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 6136 verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6137 " vector<int> bbbbbbbbbbbbbbb);", 6138 NoBinPacking); 6139 // FIXME: This behavior difference is probably not wanted. However, currently 6140 // we cannot distinguish BreakBeforeParameter being set because of the wrapped 6141 // template arguments from BreakBeforeParameter being set because of the 6142 // one-per-line formatting. 6143 verifyFormat( 6144 "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n" 6145 " aaaaaaaaaa> aaaaaaaaaa);", 6146 NoBinPacking); 6147 verifyFormat( 6148 "void fffffffffff(\n" 6149 " aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n" 6150 " aaaaaaaaaa);"); 6151 } 6152 6153 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { 6154 FormatStyle NoBinPacking = getGoogleStyle(); 6155 NoBinPacking.BinPackParameters = false; 6156 NoBinPacking.BinPackArguments = false; 6157 verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" 6158 " aaaaaaaaaaaaaaaaaaaa,\n" 6159 " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", 6160 NoBinPacking); 6161 verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" 6162 " aaaaaaaaaaaaa,\n" 6163 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", 6164 NoBinPacking); 6165 verifyFormat( 6166 "aaaaaaaa(aaaaaaaaaaaaa,\n" 6167 " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6168 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" 6169 " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6170 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", 6171 NoBinPacking); 6172 verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 6173 " .aaaaaaaaaaaaaaaaaa();", 6174 NoBinPacking); 6175 verifyFormat("void f() {\n" 6176 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6177 " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n" 6178 "}", 6179 NoBinPacking); 6180 6181 verifyFormat( 6182 "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6183 " aaaaaaaaaaaa,\n" 6184 " aaaaaaaaaaaa);", 6185 NoBinPacking); 6186 verifyFormat( 6187 "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" 6188 " ddddddddddddddddddddddddddddd),\n" 6189 " test);", 6190 NoBinPacking); 6191 6192 verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" 6193 " aaaaaaaaaaaaaaaaaaaaaaa,\n" 6194 " aaaaaaaaaaaaaaaaaaaaaaa>\n" 6195 " aaaaaaaaaaaaaaaaaa;", 6196 NoBinPacking); 6197 verifyFormat("a(\"a\"\n" 6198 " \"a\",\n" 6199 " a);"); 6200 6201 NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; 6202 verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" 6203 " aaaaaaaaa,\n" 6204 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 6205 NoBinPacking); 6206 verifyFormat( 6207 "void f() {\n" 6208 " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" 6209 " .aaaaaaa();\n" 6210 "}", 6211 NoBinPacking); 6212 verifyFormat( 6213 "template <class SomeType, class SomeOtherType>\n" 6214 "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}", 6215 NoBinPacking); 6216 } 6217 6218 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { 6219 FormatStyle Style = getLLVMStyleWithColumns(15); 6220 Style.ExperimentalAutoDetectBinPacking = true; 6221 EXPECT_EQ("aaa(aaaa,\n" 6222 " aaaa,\n" 6223 " aaaa);\n" 6224 "aaa(aaaa,\n" 6225 " aaaa,\n" 6226 " aaaa);", 6227 format("aaa(aaaa,\n" // one-per-line 6228 " aaaa,\n" 6229 " aaaa );\n" 6230 "aaa(aaaa, aaaa, aaaa);", // inconclusive 6231 Style)); 6232 EXPECT_EQ("aaa(aaaa, aaaa,\n" 6233 " aaaa);\n" 6234 "aaa(aaaa, aaaa,\n" 6235 " aaaa);", 6236 format("aaa(aaaa, aaaa,\n" // bin-packed 6237 " aaaa );\n" 6238 "aaa(aaaa, aaaa, aaaa);", // inconclusive 6239 Style)); 6240 } 6241 6242 TEST_F(FormatTest, FormatsBuilderPattern) { 6243 verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n" 6244 " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" 6245 " .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n" 6246 " .StartsWith(\".init\", ORDER_INIT)\n" 6247 " .StartsWith(\".fini\", ORDER_FINI)\n" 6248 " .StartsWith(\".hash\", ORDER_HASH)\n" 6249 " .Default(ORDER_TEXT);\n"); 6250 6251 verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" 6252 " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); 6253 verifyFormat("aaaaaaa->aaaaaaa\n" 6254 " ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6255 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6256 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 6257 verifyFormat( 6258 "aaaaaaa->aaaaaaa\n" 6259 " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6260 " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); 6261 verifyFormat( 6262 "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" 6263 " aaaaaaaaaaaaaa);"); 6264 verifyFormat( 6265 "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n" 6266 " aaaaaa->aaaaaaaaaaaa()\n" 6267 " ->aaaaaaaaaaaaaaaa(\n" 6268 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6269 " ->aaaaaaaaaaaaaaaaa();"); 6270 verifyGoogleFormat( 6271 "void f() {\n" 6272 " someo->Add((new util::filetools::Handler(dir))\n" 6273 " ->OnEvent1(NewPermanentCallback(\n" 6274 " this, &HandlerHolderClass::EventHandlerCBA))\n" 6275 " ->OnEvent2(NewPermanentCallback(\n" 6276 " this, &HandlerHolderClass::EventHandlerCBB))\n" 6277 " ->OnEvent3(NewPermanentCallback(\n" 6278 " this, &HandlerHolderClass::EventHandlerCBC))\n" 6279 " ->OnEvent5(NewPermanentCallback(\n" 6280 " this, &HandlerHolderClass::EventHandlerCBD))\n" 6281 " ->OnEvent6(NewPermanentCallback(\n" 6282 " this, &HandlerHolderClass::EventHandlerCBE)));\n" 6283 "}"); 6284 6285 verifyFormat( 6286 "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();"); 6287 verifyFormat("aaaaaaaaaaaaaaa()\n" 6288 " .aaaaaaaaaaaaaaa()\n" 6289 " .aaaaaaaaaaaaaaa()\n" 6290 " .aaaaaaaaaaaaaaa()\n" 6291 " .aaaaaaaaaaaaaaa();"); 6292 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 6293 " .aaaaaaaaaaaaaaa()\n" 6294 " .aaaaaaaaaaaaaaa()\n" 6295 " .aaaaaaaaaaaaaaa();"); 6296 verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 6297 " .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n" 6298 " .aaaaaaaaaaaaaaa();"); 6299 verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n" 6300 " ->aaaaaaaaaaaaaae(0)\n" 6301 " ->aaaaaaaaaaaaaaa();"); 6302 6303 // Don't linewrap after very short segments. 6304 verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 6305 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 6306 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 6307 verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 6308 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 6309 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 6310 verifyFormat("aaa()\n" 6311 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 6312 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 6313 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 6314 6315 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 6316 " .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 6317 " .has<bbbbbbbbbbbbbbbbbbbbb>();"); 6318 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n" 6319 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 6320 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();"); 6321 6322 // Prefer not to break after empty parentheses. 6323 verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" 6324 " First->LastNewlineOffset);"); 6325 6326 // Prefer not to create "hanging" indents. 6327 verifyFormat( 6328 "return !soooooooooooooome_map\n" 6329 " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6330 " .second;"); 6331 verifyFormat( 6332 "return aaaaaaaaaaaaaaaa\n" 6333 " .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n" 6334 " .aaaa(aaaaaaaaaaaaaa);"); 6335 // No hanging indent here. 6336 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n" 6337 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6338 verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n" 6339 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6340 verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 6341 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 6342 getLLVMStyleWithColumns(60)); 6343 verifyFormat("aaaaaaaaaaaaaaaaaa\n" 6344 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n" 6345 " .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 6346 getLLVMStyleWithColumns(59)); 6347 verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6348 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6349 " .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6350 6351 // Dont break if only closing statements before member call 6352 verifyFormat("test() {\n" 6353 " ([]() -> {\n" 6354 " int b = 32;\n" 6355 " return 3;\n" 6356 " }).foo();\n" 6357 "}"); 6358 verifyFormat("test() {\n" 6359 " (\n" 6360 " []() -> {\n" 6361 " int b = 32;\n" 6362 " return 3;\n" 6363 " },\n" 6364 " foo, bar)\n" 6365 " .foo();\n" 6366 "}"); 6367 verifyFormat("test() {\n" 6368 " ([]() -> {\n" 6369 " int b = 32;\n" 6370 " return 3;\n" 6371 " })\n" 6372 " .foo()\n" 6373 " .bar();\n" 6374 "}"); 6375 verifyFormat("test() {\n" 6376 " ([]() -> {\n" 6377 " int b = 32;\n" 6378 " return 3;\n" 6379 " })\n" 6380 " .foo(\"aaaaaaaaaaaaaaaaa\"\n" 6381 " \"bbbb\");\n" 6382 "}", 6383 getLLVMStyleWithColumns(30)); 6384 } 6385 6386 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { 6387 verifyFormat( 6388 "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" 6389 " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); 6390 verifyFormat( 6391 "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n" 6392 " bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}"); 6393 6394 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 6395 " ccccccccccccccccccccccccc) {\n}"); 6396 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n" 6397 " ccccccccccccccccccccccccc) {\n}"); 6398 6399 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" 6400 " ccccccccccccccccccccccccc) {\n}"); 6401 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n" 6402 " ccccccccccccccccccccccccc) {\n}"); 6403 6404 verifyFormat( 6405 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" 6406 " ccccccccccccccccccccccccc) {\n}"); 6407 verifyFormat( 6408 "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n" 6409 " ccccccccccccccccccccccccc) {\n}"); 6410 6411 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" 6412 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" 6413 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" 6414 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 6415 verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n" 6416 " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n" 6417 " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n" 6418 " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); 6419 6420 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" 6421 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" 6422 " aaaaaaaaaaaaaaa != aa) {\n}"); 6423 verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n" 6424 " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n" 6425 " aaaaaaaaaaaaaaa != aa) {\n}"); 6426 } 6427 6428 TEST_F(FormatTest, BreaksAfterAssignments) { 6429 verifyFormat( 6430 "unsigned Cost =\n" 6431 " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" 6432 " SI->getPointerAddressSpaceee());\n"); 6433 verifyFormat( 6434 "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" 6435 " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); 6436 6437 verifyFormat( 6438 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n" 6439 " aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); 6440 verifyFormat("unsigned OriginalStartColumn =\n" 6441 " SourceMgr.getSpellingColumnNumber(\n" 6442 " Current.FormatTok.getStartOfNonWhitespace()) -\n" 6443 " 1;"); 6444 } 6445 6446 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) { 6447 FormatStyle Style = getLLVMStyle(); 6448 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 6449 " bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;", 6450 Style); 6451 6452 Style.PenaltyBreakAssignment = 20; 6453 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n" 6454 " cccccccccccccccccccccccccc;", 6455 Style); 6456 } 6457 6458 TEST_F(FormatTest, AlignsAfterAssignments) { 6459 verifyFormat( 6460 "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6461 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 6462 verifyFormat( 6463 "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6464 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 6465 verifyFormat( 6466 "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6467 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 6468 verifyFormat( 6469 "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6470 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 6471 verifyFormat( 6472 "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" 6473 " aaaaaaaaaaaaaaaaaaaaaaaa +\n" 6474 " aaaaaaaaaaaaaaaaaaaaaaaa;"); 6475 } 6476 6477 TEST_F(FormatTest, AlignsAfterReturn) { 6478 verifyFormat( 6479 "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6480 " aaaaaaaaaaaaaaaaaaaaaaaaa;"); 6481 verifyFormat( 6482 "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6483 " aaaaaaaaaaaaaaaaaaaaaaaaa);"); 6484 verifyFormat( 6485 "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 6486 " aaaaaaaaaaaaaaaaaaaaaa();"); 6487 verifyFormat( 6488 "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" 6489 " aaaaaaaaaaaaaaaaaaaaaa());"); 6490 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6491 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6492 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6493 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" 6494 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6495 verifyFormat("return\n" 6496 " // true if code is one of a or b.\n" 6497 " code == a || code == b;"); 6498 } 6499 6500 TEST_F(FormatTest, AlignsAfterOpenBracket) { 6501 verifyFormat( 6502 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 6503 " aaaaaaaaa aaaaaaa) {}"); 6504 verifyFormat( 6505 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 6506 " aaaaaaaaaaa aaaaaaaaa);"); 6507 verifyFormat( 6508 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 6509 " aaaaaaaaaaaaaaaaaaaaa));"); 6510 FormatStyle Style = getLLVMStyle(); 6511 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6512 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6513 " aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}", 6514 Style); 6515 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 6516 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);", 6517 Style); 6518 verifyFormat("SomeLongVariableName->someFunction(\n" 6519 " foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));", 6520 Style); 6521 verifyFormat( 6522 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n" 6523 " aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6524 Style); 6525 verifyFormat( 6526 "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n" 6527 " aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 6528 Style); 6529 verifyFormat( 6530 "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n" 6531 " aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 6532 Style); 6533 6534 verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n" 6535 " ccccccc(aaaaaaaaaaaaaaaaa, //\n" 6536 " b));", 6537 Style); 6538 6539 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 6540 Style.BinPackArguments = false; 6541 Style.BinPackParameters = false; 6542 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6543 " aaaaaaaaaaa aaaaaaaa,\n" 6544 " aaaaaaaaa aaaaaaa,\n" 6545 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", 6546 Style); 6547 verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" 6548 " aaaaaaaaaaa aaaaaaaaa,\n" 6549 " aaaaaaaaaaa aaaaaaaaa,\n" 6550 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 6551 Style); 6552 verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" 6553 " aaaaaaaaaaaaaaa,\n" 6554 " aaaaaaaaaaaaaaaaaaaaa,\n" 6555 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));", 6556 Style); 6557 verifyFormat( 6558 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" 6559 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 6560 Style); 6561 verifyFormat( 6562 "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" 6563 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));", 6564 Style); 6565 verifyFormat( 6566 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 6567 " aaaaaaaaaaaaaaaaaaaaa(\n" 6568 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n" 6569 " aaaaaaaaaaaaaaaa);", 6570 Style); 6571 verifyFormat( 6572 "aaaaaaaaaaaaaaaaaaaaaaaa(\n" 6573 " aaaaaaaaaaaaaaaaaaaaa(\n" 6574 " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n" 6575 " aaaaaaaaaaaaaaaa);", 6576 Style); 6577 } 6578 6579 TEST_F(FormatTest, ParenthesesAndOperandAlignment) { 6580 FormatStyle Style = getLLVMStyleWithColumns(40); 6581 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 6582 " bbbbbbbbbbbbbbbbbbbbbb);", 6583 Style); 6584 Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; 6585 Style.AlignOperands = FormatStyle::OAS_DontAlign; 6586 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 6587 " bbbbbbbbbbbbbbbbbbbbbb);", 6588 Style); 6589 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6590 Style.AlignOperands = FormatStyle::OAS_Align; 6591 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 6592 " bbbbbbbbbbbbbbbbbbbbbb);", 6593 Style); 6594 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 6595 Style.AlignOperands = FormatStyle::OAS_DontAlign; 6596 verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" 6597 " bbbbbbbbbbbbbbbbbbbbbb);", 6598 Style); 6599 } 6600 6601 TEST_F(FormatTest, BreaksConditionalExpressions) { 6602 verifyFormat( 6603 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6604 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6605 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6606 verifyFormat( 6607 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 6608 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6609 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6610 verifyFormat( 6611 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6612 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6613 verifyFormat("aaaa(aaaaaaaaa, aaaaaaaaa,\n" 6614 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6615 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6616 verifyFormat( 6617 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" 6618 " : aaaaaaaaaaaaa);"); 6619 verifyFormat( 6620 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6621 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6622 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6623 " aaaaaaaaaaaaa);"); 6624 verifyFormat( 6625 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6626 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6627 " aaaaaaaaaaaaa);"); 6628 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6629 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6630 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6631 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6632 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6633 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6634 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6635 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6636 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 6637 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6638 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6639 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6640 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6641 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6642 " ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6643 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6644 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 6645 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6646 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6647 " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6648 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 6649 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6650 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6651 " : aaaaaaaaaaaaaaaa;"); 6652 verifyFormat( 6653 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6654 " ? aaaaaaaaaaaaaaa\n" 6655 " : aaaaaaaaaaaaaaa;"); 6656 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 6657 " aaaaaaaaa\n" 6658 " ? b\n" 6659 " : c);"); 6660 verifyFormat("return aaaa == bbbb\n" 6661 " // comment\n" 6662 " ? aaaa\n" 6663 " : bbbb;"); 6664 verifyFormat("unsigned Indent =\n" 6665 " format(TheLine.First,\n" 6666 " IndentForLevel[TheLine.Level] >= 0\n" 6667 " ? IndentForLevel[TheLine.Level]\n" 6668 " : TheLine * 2,\n" 6669 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 6670 getLLVMStyleWithColumns(60)); 6671 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 6672 " ? aaaaaaaaaaaaaaa\n" 6673 " : bbbbbbbbbbbbbbb //\n" 6674 " ? ccccccccccccccc\n" 6675 " : ddddddddddddddd;"); 6676 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n" 6677 " ? aaaaaaaaaaaaaaa\n" 6678 " : (bbbbbbbbbbbbbbb //\n" 6679 " ? ccccccccccccccc\n" 6680 " : ddddddddddddddd);"); 6681 verifyFormat( 6682 "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6683 " ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n" 6684 " aaaaaaaaaaaaaaaaaaaaa +\n" 6685 " aaaaaaaaaaaaaaaaaaaaa\n" 6686 " : aaaaaaaaaa;"); 6687 verifyFormat( 6688 "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6689 " : aaaaaaaaaaaaaaaaaaaaaa\n" 6690 " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 6691 6692 FormatStyle NoBinPacking = getLLVMStyle(); 6693 NoBinPacking.BinPackArguments = false; 6694 verifyFormat( 6695 "void f() {\n" 6696 " g(aaa,\n" 6697 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 6698 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6699 " ? aaaaaaaaaaaaaaa\n" 6700 " : aaaaaaaaaaaaaaa);\n" 6701 "}", 6702 NoBinPacking); 6703 verifyFormat( 6704 "void f() {\n" 6705 " g(aaa,\n" 6706 " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" 6707 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6708 " ?: aaaaaaaaaaaaaaa);\n" 6709 "}", 6710 NoBinPacking); 6711 6712 verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n" 6713 " // comment.\n" 6714 " ccccccccccccccccccccccccccccccccccccccc\n" 6715 " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 6716 " : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);"); 6717 6718 // Assignments in conditional expressions. Apparently not uncommon :-(. 6719 verifyFormat("return a != b\n" 6720 " // comment\n" 6721 " ? a = b\n" 6722 " : a = b;"); 6723 verifyFormat("return a != b\n" 6724 " // comment\n" 6725 " ? a = a != b\n" 6726 " // comment\n" 6727 " ? a = b\n" 6728 " : a\n" 6729 " : a;\n"); 6730 verifyFormat("return a != b\n" 6731 " // comment\n" 6732 " ? a\n" 6733 " : a = a != b\n" 6734 " // comment\n" 6735 " ? a = b\n" 6736 " : a;"); 6737 6738 // Chained conditionals 6739 FormatStyle Style = getLLVMStyle(); 6740 Style.ColumnLimit = 70; 6741 Style.AlignOperands = FormatStyle::OAS_Align; 6742 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 6743 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 6744 " : 3333333333333333;", 6745 Style); 6746 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 6747 " : bbbbbbbbbb ? 2222222222222222\n" 6748 " : 3333333333333333;", 6749 Style); 6750 verifyFormat("return aaaaaaaaaa ? 1111111111111111\n" 6751 " : bbbbbbbbbbbbbbbb ? 2222222222222222\n" 6752 " : 3333333333333333;", 6753 Style); 6754 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 6755 " : bbbbbbbbbbbbbb ? 222222\n" 6756 " : 333333;", 6757 Style); 6758 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 6759 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 6760 " : cccccccccccccc ? 3333333333333333\n" 6761 " : 4444444444444444;", 6762 Style); 6763 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc)\n" 6764 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 6765 " : 3333333333333333;", 6766 Style); 6767 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 6768 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 6769 " : (aaa ? bbb : ccc);", 6770 Style); 6771 verifyFormat( 6772 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 6773 " : cccccccccccccccccc)\n" 6774 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 6775 " : 3333333333333333;", 6776 Style); 6777 verifyFormat( 6778 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 6779 " : cccccccccccccccccc)\n" 6780 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 6781 " : 3333333333333333;", 6782 Style); 6783 verifyFormat( 6784 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 6785 " : dddddddddddddddddd)\n" 6786 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 6787 " : 3333333333333333;", 6788 Style); 6789 verifyFormat( 6790 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 6791 " : dddddddddddddddddd)\n" 6792 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 6793 " : 3333333333333333;", 6794 Style); 6795 verifyFormat( 6796 "return aaaaaaaaa ? 1111111111111111\n" 6797 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 6798 " : a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 6799 " : dddddddddddddddddd)\n", 6800 Style); 6801 verifyFormat( 6802 "return aaaaaaaaaaaaaaaa ? 1111111111111111\n" 6803 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 6804 " : (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 6805 " : cccccccccccccccccc);", 6806 Style); 6807 verifyFormat( 6808 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 6809 " : ccccccccccccccc ? dddddddddddddddddd\n" 6810 " : eeeeeeeeeeeeeeeeee)\n" 6811 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 6812 " : 3333333333333333;", 6813 Style); 6814 verifyFormat( 6815 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 6816 " : ccccccccccccccc ? dddddddddddddddddd\n" 6817 " : eeeeeeeeeeeeeeeeee)\n" 6818 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 6819 " : 3333333333333333;", 6820 Style); 6821 verifyFormat( 6822 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 6823 " : cccccccccccc ? dddddddddddddddddd\n" 6824 " : eeeeeeeeeeeeeeeeee)\n" 6825 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 6826 " : 3333333333333333;", 6827 Style); 6828 verifyFormat( 6829 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 6830 " : cccccccccccccccccc\n" 6831 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 6832 " : 3333333333333333;", 6833 Style); 6834 verifyFormat( 6835 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 6836 " : cccccccccccccccc ? dddddddddddddddddd\n" 6837 " : eeeeeeeeeeeeeeeeee\n" 6838 " : bbbbbbbbbbbbbb ? 2222222222222222\n" 6839 " : 3333333333333333;", 6840 Style); 6841 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa\n" 6842 " ? (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 6843 " : cccccccccccccccccc ? dddddddddddddddddd\n" 6844 " : eeeeeeeeeeeeeeeeee)\n" 6845 " : bbbbbbbbbbbbbbbbbbb ? 2222222222222222\n" 6846 " : 3333333333333333;", 6847 Style); 6848 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaa\n" 6849 " ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb\n" 6850 " : cccccccccccccccc ? dddddddddddddddddd\n" 6851 " : eeeeeeeeeeeeeeeeee\n" 6852 " : bbbbbbbbbbbbbbbbbbbbbbb ? 2222222222222222\n" 6853 " : 3333333333333333;", 6854 Style); 6855 6856 Style.AlignOperands = FormatStyle::OAS_DontAlign; 6857 Style.BreakBeforeTernaryOperators = false; 6858 // FIXME: Aligning the question marks is weird given DontAlign. 6859 // Consider disabling this alignment in this case. Also check whether this 6860 // will render the adjustment from https://reviews.llvm.org/D82199 6861 // unnecessary. 6862 verifyFormat("int x = aaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa :\n" 6863 " bbbb ? cccccccccccccccccc :\n" 6864 " ddddd;\n", 6865 Style); 6866 6867 EXPECT_EQ( 6868 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n" 6869 " /*\n" 6870 " */\n" 6871 " function() {\n" 6872 " try {\n" 6873 " return JJJJJJJJJJJJJJ(\n" 6874 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n" 6875 " }\n" 6876 " } :\n" 6877 " function() {};", 6878 format( 6879 "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n" 6880 " /*\n" 6881 " */\n" 6882 " function() {\n" 6883 " try {\n" 6884 " return JJJJJJJJJJJJJJ(\n" 6885 " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n" 6886 " }\n" 6887 " } :\n" 6888 " function() {};", 6889 getGoogleStyle(FormatStyle::LK_JavaScript))); 6890 } 6891 6892 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { 6893 FormatStyle Style = getLLVMStyle(); 6894 Style.BreakBeforeTernaryOperators = false; 6895 Style.ColumnLimit = 70; 6896 verifyFormat( 6897 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 6898 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 6899 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 6900 Style); 6901 verifyFormat( 6902 "aaaa(aaaaaaaaaa, aaaaaaaa,\n" 6903 " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 6904 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 6905 Style); 6906 verifyFormat( 6907 "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 6908 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 6909 Style); 6910 verifyFormat("aaaa(aaaaaaaa, aaaaaaaaaa,\n" 6911 " aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 6912 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 6913 Style); 6914 verifyFormat( 6915 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" 6916 " aaaaaaaaaaaaa);", 6917 Style); 6918 verifyFormat( 6919 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6920 " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 6921 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6922 " aaaaaaaaaaaaa);", 6923 Style); 6924 verifyFormat( 6925 "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6926 " aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6927 " aaaaaaaaaaaaa);", 6928 Style); 6929 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 6930 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6931 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 6932 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6933 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 6934 Style); 6935 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6936 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 6937 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6938 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n" 6939 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6940 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6941 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 6942 Style); 6943 verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 6944 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n" 6945 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 6946 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" 6947 " aaaaaaaaaaaaaaaaaaaaaaaaaaa);", 6948 Style); 6949 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 6950 " aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 6951 " aaaaaaaaaaaaaaaaaaaaaaaaaaa;", 6952 Style); 6953 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" 6954 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 6955 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" 6956 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 6957 Style); 6958 verifyFormat( 6959 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 6960 " aaaaaaaaaaaaaaa :\n" 6961 " aaaaaaaaaaaaaaa;", 6962 Style); 6963 verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" 6964 " aaaaaaaaa ?\n" 6965 " b :\n" 6966 " c);", 6967 Style); 6968 verifyFormat("unsigned Indent =\n" 6969 " format(TheLine.First,\n" 6970 " IndentForLevel[TheLine.Level] >= 0 ?\n" 6971 " IndentForLevel[TheLine.Level] :\n" 6972 " TheLine * 2,\n" 6973 " TheLine.InPPDirective, PreviousEndOfLineColumn);", 6974 Style); 6975 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 6976 " aaaaaaaaaaaaaaa :\n" 6977 " bbbbbbbbbbbbbbb ? //\n" 6978 " ccccccccccccccc :\n" 6979 " ddddddddddddddd;", 6980 Style); 6981 verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n" 6982 " aaaaaaaaaaaaaaa :\n" 6983 " (bbbbbbbbbbbbbbb ? //\n" 6984 " ccccccccccccccc :\n" 6985 " ddddddddddddddd);", 6986 Style); 6987 verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 6988 " /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n" 6989 " ccccccccccccccccccccccccccc;", 6990 Style); 6991 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" 6992 " aaaaa :\n" 6993 " bbbbbbbbbbbbbbb + cccccccccccccccc;", 6994 Style); 6995 6996 // Chained conditionals 6997 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 6998 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 6999 " 3333333333333333;", 7000 Style); 7001 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7002 " bbbbbbbbbb ? 2222222222222222 :\n" 7003 " 3333333333333333;", 7004 Style); 7005 verifyFormat("return aaaaaaaaaa ? 1111111111111111 :\n" 7006 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7007 " 3333333333333333;", 7008 Style); 7009 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7010 " bbbbbbbbbbbbbbbb ? 222222 :\n" 7011 " 333333;", 7012 Style); 7013 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7014 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7015 " cccccccccccccccc ? 3333333333333333 :\n" 7016 " 4444444444444444;", 7017 Style); 7018 verifyFormat("return aaaaaaaaaaaaaaaa ? (aaa ? bbb : ccc) :\n" 7019 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7020 " 3333333333333333;", 7021 Style); 7022 verifyFormat("return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7023 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7024 " (aaa ? bbb : ccc);", 7025 Style); 7026 verifyFormat( 7027 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7028 " cccccccccccccccccc) :\n" 7029 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7030 " 3333333333333333;", 7031 Style); 7032 verifyFormat( 7033 "return aaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7034 " cccccccccccccccccc) :\n" 7035 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7036 " 3333333333333333;", 7037 Style); 7038 verifyFormat( 7039 "return aaaaaaaaa ? a = (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7040 " dddddddddddddddddd) :\n" 7041 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7042 " 3333333333333333;", 7043 Style); 7044 verifyFormat( 7045 "return aaaaaaaaa ? a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7046 " dddddddddddddddddd) :\n" 7047 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7048 " 3333333333333333;", 7049 Style); 7050 verifyFormat( 7051 "return aaaaaaaaa ? 1111111111111111 :\n" 7052 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7053 " a + (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7054 " dddddddddddddddddd)\n", 7055 Style); 7056 verifyFormat( 7057 "return aaaaaaaaaaaaaaaa ? 1111111111111111 :\n" 7058 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7059 " (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7060 " cccccccccccccccccc);", 7061 Style); 7062 verifyFormat( 7063 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7064 " ccccccccccccccccc ? dddddddddddddddddd :\n" 7065 " eeeeeeeeeeeeeeeeee) :\n" 7066 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7067 " 3333333333333333;", 7068 Style); 7069 verifyFormat( 7070 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7071 " ccccccccccccc ? dddddddddddddddddd :\n" 7072 " eeeeeeeeeeeeeeeeee) :\n" 7073 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7074 " 3333333333333333;", 7075 Style); 7076 verifyFormat( 7077 "return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7078 " ccccccccccccccccc ? dddddddddddddddddd :\n" 7079 " eeeeeeeeeeeeeeeeee) :\n" 7080 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7081 " 3333333333333333;", 7082 Style); 7083 verifyFormat( 7084 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7085 " cccccccccccccccccc :\n" 7086 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7087 " 3333333333333333;", 7088 Style); 7089 verifyFormat( 7090 "return aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7091 " cccccccccccccccccc ? dddddddddddddddddd :\n" 7092 " eeeeeeeeeeeeeeeeee :\n" 7093 " bbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7094 " 3333333333333333;", 7095 Style); 7096 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n" 7097 " (aaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7098 " cccccccccccccccccc ? dddddddddddddddddd :\n" 7099 " eeeeeeeeeeeeeeeeee) :\n" 7100 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7101 " 3333333333333333;", 7102 Style); 7103 verifyFormat("return aaaaaaaaaaaaaaaaaaaaa ?\n" 7104 " aaaaaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :\n" 7105 " cccccccccccccccccccc ? dddddddddddddddddd :\n" 7106 " eeeeeeeeeeeeeeeeee :\n" 7107 " bbbbbbbbbbbbbbbbbbbbb ? 2222222222222222 :\n" 7108 " 3333333333333333;", 7109 Style); 7110 } 7111 7112 TEST_F(FormatTest, DeclarationsOfMultipleVariables) { 7113 verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" 7114 " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); 7115 verifyFormat("bool a = true, b = false;"); 7116 7117 verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" 7118 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" 7119 " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" 7120 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); 7121 verifyFormat( 7122 "bool aaaaaaaaaaaaaaaaaaaaa =\n" 7123 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" 7124 " d = e && f;"); 7125 verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" 7126 " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); 7127 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 7128 " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); 7129 verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" 7130 " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); 7131 7132 FormatStyle Style = getGoogleStyle(); 7133 Style.PointerAlignment = FormatStyle::PAS_Left; 7134 Style.DerivePointerAlignment = false; 7135 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7136 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" 7137 " *b = bbbbbbbbbbbbbbbbbbb;", 7138 Style); 7139 verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" 7140 " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", 7141 Style); 7142 verifyFormat("vector<int*> a, b;", Style); 7143 verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style); 7144 } 7145 7146 TEST_F(FormatTest, ConditionalExpressionsInBrackets) { 7147 verifyFormat("arr[foo ? bar : baz];"); 7148 verifyFormat("f()[foo ? bar : baz];"); 7149 verifyFormat("(a + b)[foo ? bar : baz];"); 7150 verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); 7151 } 7152 7153 TEST_F(FormatTest, AlignsStringLiterals) { 7154 verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" 7155 " \"short literal\");"); 7156 verifyFormat( 7157 "looooooooooooooooooooooooongFunction(\n" 7158 " \"short literal\"\n" 7159 " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); 7160 verifyFormat("someFunction(\"Always break between multi-line\"\n" 7161 " \" string literals\",\n" 7162 " and, other, parameters);"); 7163 EXPECT_EQ("fun + \"1243\" /* comment */\n" 7164 " \"5678\";", 7165 format("fun + \"1243\" /* comment */\n" 7166 " \"5678\";", 7167 getLLVMStyleWithColumns(28))); 7168 EXPECT_EQ( 7169 "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 7170 " \"aaaaaaaaaaaaaaaaaaaaa\"\n" 7171 " \"aaaaaaaaaaaaaaaa\";", 7172 format("aaaaaa =" 7173 "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " 7174 "aaaaaaaaaaaaaaaaaaaaa\" " 7175 "\"aaaaaaaaaaaaaaaa\";")); 7176 verifyFormat("a = a + \"a\"\n" 7177 " \"a\"\n" 7178 " \"a\";"); 7179 verifyFormat("f(\"a\", \"b\"\n" 7180 " \"c\");"); 7181 7182 verifyFormat( 7183 "#define LL_FORMAT \"ll\"\n" 7184 "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" 7185 " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); 7186 7187 verifyFormat("#define A(X) \\\n" 7188 " \"aaaaa\" #X \"bbbbbb\" \\\n" 7189 " \"ccccc\"", 7190 getLLVMStyleWithColumns(23)); 7191 verifyFormat("#define A \"def\"\n" 7192 "f(\"abc\" A \"ghi\"\n" 7193 " \"jkl\");"); 7194 7195 verifyFormat("f(L\"a\"\n" 7196 " L\"b\");"); 7197 verifyFormat("#define A(X) \\\n" 7198 " L\"aaaaa\" #X L\"bbbbbb\" \\\n" 7199 " L\"ccccc\"", 7200 getLLVMStyleWithColumns(25)); 7201 7202 verifyFormat("f(@\"a\"\n" 7203 " @\"b\");"); 7204 verifyFormat("NSString s = @\"a\"\n" 7205 " @\"b\"\n" 7206 " @\"c\";"); 7207 verifyFormat("NSString s = @\"a\"\n" 7208 " \"b\"\n" 7209 " \"c\";"); 7210 } 7211 7212 TEST_F(FormatTest, ReturnTypeBreakingStyle) { 7213 FormatStyle Style = getLLVMStyle(); 7214 // No declarations or definitions should be moved to own line. 7215 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; 7216 verifyFormat("class A {\n" 7217 " int f() { return 1; }\n" 7218 " int g();\n" 7219 "};\n" 7220 "int f() { return 1; }\n" 7221 "int g();\n", 7222 Style); 7223 7224 // All declarations and definitions should have the return type moved to its 7225 // own line. 7226 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 7227 Style.TypenameMacros = {"LIST"}; 7228 verifyFormat("SomeType\n" 7229 "funcdecl(LIST(uint64_t));", 7230 Style); 7231 verifyFormat("class E {\n" 7232 " int\n" 7233 " f() {\n" 7234 " return 1;\n" 7235 " }\n" 7236 " int\n" 7237 " g();\n" 7238 "};\n" 7239 "int\n" 7240 "f() {\n" 7241 " return 1;\n" 7242 "}\n" 7243 "int\n" 7244 "g();\n", 7245 Style); 7246 7247 // Top-level definitions, and no kinds of declarations should have the 7248 // return type moved to its own line. 7249 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; 7250 verifyFormat("class B {\n" 7251 " int f() { return 1; }\n" 7252 " int g();\n" 7253 "};\n" 7254 "int\n" 7255 "f() {\n" 7256 " return 1;\n" 7257 "}\n" 7258 "int g();\n", 7259 Style); 7260 7261 // Top-level definitions and declarations should have the return type moved 7262 // to its own line. 7263 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel; 7264 verifyFormat("class C {\n" 7265 " int f() { return 1; }\n" 7266 " int g();\n" 7267 "};\n" 7268 "int\n" 7269 "f() {\n" 7270 " return 1;\n" 7271 "}\n" 7272 "int\n" 7273 "g();\n", 7274 Style); 7275 7276 // All definitions should have the return type moved to its own line, but no 7277 // kinds of declarations. 7278 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; 7279 verifyFormat("class D {\n" 7280 " int\n" 7281 " f() {\n" 7282 " return 1;\n" 7283 " }\n" 7284 " int g();\n" 7285 "};\n" 7286 "int\n" 7287 "f() {\n" 7288 " return 1;\n" 7289 "}\n" 7290 "int g();\n", 7291 Style); 7292 verifyFormat("const char *\n" 7293 "f(void) {\n" // Break here. 7294 " return \"\";\n" 7295 "}\n" 7296 "const char *bar(void);\n", // No break here. 7297 Style); 7298 verifyFormat("template <class T>\n" 7299 "T *\n" 7300 "f(T &c) {\n" // Break here. 7301 " return NULL;\n" 7302 "}\n" 7303 "template <class T> T *f(T &c);\n", // No break here. 7304 Style); 7305 verifyFormat("class C {\n" 7306 " int\n" 7307 " operator+() {\n" 7308 " return 1;\n" 7309 " }\n" 7310 " int\n" 7311 " operator()() {\n" 7312 " return 1;\n" 7313 " }\n" 7314 "};\n", 7315 Style); 7316 verifyFormat("void\n" 7317 "A::operator()() {}\n" 7318 "void\n" 7319 "A::operator>>() {}\n" 7320 "void\n" 7321 "A::operator+() {}\n" 7322 "void\n" 7323 "A::operator*() {}\n" 7324 "void\n" 7325 "A::operator->() {}\n" 7326 "void\n" 7327 "A::operator void *() {}\n" 7328 "void\n" 7329 "A::operator void &() {}\n" 7330 "void\n" 7331 "A::operator void &&() {}\n" 7332 "void\n" 7333 "A::operator char *() {}\n" 7334 "void\n" 7335 "A::operator[]() {}\n" 7336 "void\n" 7337 "A::operator!() {}\n" 7338 "void\n" 7339 "A::operator**() {}\n" 7340 "void\n" 7341 "A::operator<Foo> *() {}\n" 7342 "void\n" 7343 "A::operator<Foo> **() {}\n" 7344 "void\n" 7345 "A::operator<Foo> &() {}\n" 7346 "void\n" 7347 "A::operator void **() {}\n", 7348 Style); 7349 verifyFormat("constexpr auto\n" 7350 "operator()() const -> reference {}\n" 7351 "constexpr auto\n" 7352 "operator>>() const -> reference {}\n" 7353 "constexpr auto\n" 7354 "operator+() const -> reference {}\n" 7355 "constexpr auto\n" 7356 "operator*() const -> reference {}\n" 7357 "constexpr auto\n" 7358 "operator->() const -> reference {}\n" 7359 "constexpr auto\n" 7360 "operator++() const -> reference {}\n" 7361 "constexpr auto\n" 7362 "operator void *() const -> reference {}\n" 7363 "constexpr auto\n" 7364 "operator void **() const -> reference {}\n" 7365 "constexpr auto\n" 7366 "operator void *() const -> reference {}\n" 7367 "constexpr auto\n" 7368 "operator void &() const -> reference {}\n" 7369 "constexpr auto\n" 7370 "operator void &&() const -> reference {}\n" 7371 "constexpr auto\n" 7372 "operator char *() const -> reference {}\n" 7373 "constexpr auto\n" 7374 "operator!() const -> reference {}\n" 7375 "constexpr auto\n" 7376 "operator[]() const -> reference {}\n", 7377 Style); 7378 verifyFormat("void *operator new(std::size_t s);", // No break here. 7379 Style); 7380 verifyFormat("void *\n" 7381 "operator new(std::size_t s) {}", 7382 Style); 7383 verifyFormat("void *\n" 7384 "operator delete[](void *ptr) {}", 7385 Style); 7386 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 7387 verifyFormat("const char *\n" 7388 "f(void)\n" // Break here. 7389 "{\n" 7390 " return \"\";\n" 7391 "}\n" 7392 "const char *bar(void);\n", // No break here. 7393 Style); 7394 verifyFormat("template <class T>\n" 7395 "T *\n" // Problem here: no line break 7396 "f(T &c)\n" // Break here. 7397 "{\n" 7398 " return NULL;\n" 7399 "}\n" 7400 "template <class T> T *f(T &c);\n", // No break here. 7401 Style); 7402 verifyFormat("int\n" 7403 "foo(A<bool> a)\n" 7404 "{\n" 7405 " return a;\n" 7406 "}\n", 7407 Style); 7408 verifyFormat("int\n" 7409 "foo(A<8> a)\n" 7410 "{\n" 7411 " return a;\n" 7412 "}\n", 7413 Style); 7414 verifyFormat("int\n" 7415 "foo(A<B<bool>, 8> a)\n" 7416 "{\n" 7417 " return a;\n" 7418 "}\n", 7419 Style); 7420 verifyFormat("int\n" 7421 "foo(A<B<8>, bool> a)\n" 7422 "{\n" 7423 " return a;\n" 7424 "}\n", 7425 Style); 7426 verifyFormat("int\n" 7427 "foo(A<B<bool>, bool> a)\n" 7428 "{\n" 7429 " return a;\n" 7430 "}\n", 7431 Style); 7432 verifyFormat("int\n" 7433 "foo(A<B<8>, 8> a)\n" 7434 "{\n" 7435 " return a;\n" 7436 "}\n", 7437 Style); 7438 7439 Style = getGNUStyle(); 7440 7441 // Test for comments at the end of function declarations. 7442 verifyFormat("void\n" 7443 "foo (int a, /*abc*/ int b) // def\n" 7444 "{\n" 7445 "}\n", 7446 Style); 7447 7448 verifyFormat("void\n" 7449 "foo (int a, /* abc */ int b) /* def */\n" 7450 "{\n" 7451 "}\n", 7452 Style); 7453 7454 // Definitions that should not break after return type 7455 verifyFormat("void foo (int a, int b); // def\n", Style); 7456 verifyFormat("void foo (int a, int b); /* def */\n", Style); 7457 verifyFormat("void foo (int a, int b);\n", Style); 7458 } 7459 7460 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { 7461 FormatStyle NoBreak = getLLVMStyle(); 7462 NoBreak.AlwaysBreakBeforeMultilineStrings = false; 7463 FormatStyle Break = getLLVMStyle(); 7464 Break.AlwaysBreakBeforeMultilineStrings = true; 7465 verifyFormat("aaaa = \"bbbb\"\n" 7466 " \"cccc\";", 7467 NoBreak); 7468 verifyFormat("aaaa =\n" 7469 " \"bbbb\"\n" 7470 " \"cccc\";", 7471 Break); 7472 verifyFormat("aaaa(\"bbbb\"\n" 7473 " \"cccc\");", 7474 NoBreak); 7475 verifyFormat("aaaa(\n" 7476 " \"bbbb\"\n" 7477 " \"cccc\");", 7478 Break); 7479 verifyFormat("aaaa(qqq, \"bbbb\"\n" 7480 " \"cccc\");", 7481 NoBreak); 7482 verifyFormat("aaaa(qqq,\n" 7483 " \"bbbb\"\n" 7484 " \"cccc\");", 7485 Break); 7486 verifyFormat("aaaa(qqq,\n" 7487 " L\"bbbb\"\n" 7488 " L\"cccc\");", 7489 Break); 7490 verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n" 7491 " \"bbbb\"));", 7492 Break); 7493 verifyFormat("string s = someFunction(\n" 7494 " \"abc\"\n" 7495 " \"abc\");", 7496 Break); 7497 7498 // As we break before unary operators, breaking right after them is bad. 7499 verifyFormat("string foo = abc ? \"x\"\n" 7500 " \"blah blah blah blah blah blah\"\n" 7501 " : \"y\";", 7502 Break); 7503 7504 // Don't break if there is no column gain. 7505 verifyFormat("f(\"aaaa\"\n" 7506 " \"bbbb\");", 7507 Break); 7508 7509 // Treat literals with escaped newlines like multi-line string literals. 7510 EXPECT_EQ("x = \"a\\\n" 7511 "b\\\n" 7512 "c\";", 7513 format("x = \"a\\\n" 7514 "b\\\n" 7515 "c\";", 7516 NoBreak)); 7517 EXPECT_EQ("xxxx =\n" 7518 " \"a\\\n" 7519 "b\\\n" 7520 "c\";", 7521 format("xxxx = \"a\\\n" 7522 "b\\\n" 7523 "c\";", 7524 Break)); 7525 7526 EXPECT_EQ("NSString *const kString =\n" 7527 " @\"aaaa\"\n" 7528 " @\"bbbb\";", 7529 format("NSString *const kString = @\"aaaa\"\n" 7530 "@\"bbbb\";", 7531 Break)); 7532 7533 Break.ColumnLimit = 0; 7534 verifyFormat("const char *hello = \"hello llvm\";", Break); 7535 } 7536 7537 TEST_F(FormatTest, AlignsPipes) { 7538 verifyFormat( 7539 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7540 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7541 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7542 verifyFormat( 7543 "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" 7544 " << aaaaaaaaaaaaaaaaaaaa;"); 7545 verifyFormat( 7546 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7547 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7548 verifyFormat( 7549 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" 7550 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7551 verifyFormat( 7552 "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 7553 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" 7554 " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); 7555 verifyFormat( 7556 "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7557 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7558 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7559 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7560 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7561 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7562 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 7563 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n" 7564 " << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);"); 7565 verifyFormat( 7566 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7567 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7568 verifyFormat( 7569 "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n" 7570 " aaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7571 7572 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" 7573 " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); 7574 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7575 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7576 " aaaaaaaaaaaaaaaaaaaaa)\n" 7577 " << aaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7578 verifyFormat("LOG_IF(aaa == //\n" 7579 " bbb)\n" 7580 " << a << b;"); 7581 7582 // But sometimes, breaking before the first "<<" is desirable. 7583 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 7584 " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); 7585 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n" 7586 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7587 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7588 verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n" 7589 " << BEF << IsTemplate << Description << E->getType();"); 7590 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 7591 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7592 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7593 verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" 7594 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7595 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7596 " << aaa;"); 7597 7598 verifyFormat( 7599 "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7600 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7601 7602 // Incomplete string literal. 7603 EXPECT_EQ("llvm::errs() << \"\n" 7604 " << a;", 7605 format("llvm::errs() << \"\n<<a;")); 7606 7607 verifyFormat("void f() {\n" 7608 " CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n" 7609 " << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n" 7610 "}"); 7611 7612 // Handle 'endl'. 7613 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n" 7614 " << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 7615 verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;"); 7616 7617 // Handle '\n'. 7618 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n" 7619 " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 7620 verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n" 7621 " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';"); 7622 verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n" 7623 " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";"); 7624 verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); 7625 } 7626 7627 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) { 7628 verifyFormat("return out << \"somepacket = {\\n\"\n" 7629 " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" 7630 " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" 7631 " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" 7632 " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" 7633 " << \"}\";"); 7634 7635 verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 7636 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n" 7637 " << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;"); 7638 verifyFormat( 7639 "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" 7640 " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" 7641 " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" 7642 " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" 7643 " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); 7644 verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" 7645 " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 7646 verifyFormat( 7647 "void f() {\n" 7648 " llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n" 7649 " << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" 7650 "}"); 7651 7652 // Breaking before the first "<<" is generally not desirable. 7653 verifyFormat( 7654 "llvm::errs()\n" 7655 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7656 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7657 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7658 " << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 7659 getLLVMStyleWithColumns(70)); 7660 verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n" 7661 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7662 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 7663 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7664 " << \"aaaaaaaaaaaaaaaaaaa: \"\n" 7665 " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;", 7666 getLLVMStyleWithColumns(70)); 7667 7668 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 7669 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n" 7670 " \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;"); 7671 verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 7672 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n" 7673 " \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);"); 7674 verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n" 7675 " (aaaa + aaaa);", 7676 getLLVMStyleWithColumns(40)); 7677 verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n" 7678 " (aaaaaaa + aaaaa));", 7679 getLLVMStyleWithColumns(40)); 7680 verifyFormat( 7681 "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n" 7682 " SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n" 7683 " bbbbbbbbbbbbbbbbbbbbbbb);"); 7684 } 7685 7686 TEST_F(FormatTest, UnderstandsEquals) { 7687 verifyFormat( 7688 "aaaaaaaaaaaaaaaaa =\n" 7689 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 7690 verifyFormat( 7691 "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 7692 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 7693 verifyFormat( 7694 "if (a) {\n" 7695 " f();\n" 7696 "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 7697 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" 7698 "}"); 7699 7700 verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 7701 " 100000000 + 10000000) {\n}"); 7702 } 7703 7704 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { 7705 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 7706 " .looooooooooooooooooooooooooooooooooooooongFunction();"); 7707 7708 verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" 7709 " ->looooooooooooooooooooooooooooooooooooooongFunction();"); 7710 7711 verifyFormat( 7712 "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" 7713 " Parameter2);"); 7714 7715 verifyFormat( 7716 "ShortObject->shortFunction(\n" 7717 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" 7718 " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); 7719 7720 verifyFormat("loooooooooooooongFunction(\n" 7721 " LoooooooooooooongObject->looooooooooooooooongFunction());"); 7722 7723 verifyFormat( 7724 "function(LoooooooooooooooooooooooooooooooooooongObject\n" 7725 " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); 7726 7727 verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 7728 " .WillRepeatedly(Return(SomeValue));"); 7729 verifyFormat("void f() {\n" 7730 " EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" 7731 " .Times(2)\n" 7732 " .WillRepeatedly(Return(SomeValue));\n" 7733 "}"); 7734 verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n" 7735 " ccccccccccccccccccccccc);"); 7736 verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7737 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7738 " .aaaaa(aaaaa),\n" 7739 " aaaaaaaaaaaaaaaaaaaaa);"); 7740 verifyFormat("void f() {\n" 7741 " aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7742 " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n" 7743 "}"); 7744 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7745 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7746 " .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7747 " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7748 " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 7749 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7750 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7751 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 7752 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" 7753 "}"); 7754 7755 // Here, it is not necessary to wrap at "." or "->". 7756 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" 7757 " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); 7758 verifyFormat( 7759 "aaaaaaaaaaa->aaaaaaaaa(\n" 7760 " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7761 " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); 7762 7763 verifyFormat( 7764 "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7765 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); 7766 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" 7767 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 7768 verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" 7769 " aaaaaaaaa()->aaaaaa()->aaaaa());"); 7770 7771 verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7772 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7773 " .a();"); 7774 7775 FormatStyle NoBinPacking = getLLVMStyle(); 7776 NoBinPacking.BinPackParameters = false; 7777 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 7778 " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 7779 " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" 7780 " aaaaaaaaaaaaaaaaaaa,\n" 7781 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 7782 NoBinPacking); 7783 7784 // If there is a subsequent call, change to hanging indentation. 7785 verifyFormat( 7786 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7787 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" 7788 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7789 verifyFormat( 7790 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7791 " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); 7792 verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7793 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7794 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 7795 verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7796 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 7797 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 7798 } 7799 7800 TEST_F(FormatTest, WrapsTemplateDeclarations) { 7801 verifyFormat("template <typename T>\n" 7802 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 7803 verifyFormat("template <typename T>\n" 7804 "// T should be one of {A, B}.\n" 7805 "virtual void loooooooooooongFunction(int Param1, int Param2);"); 7806 verifyFormat( 7807 "template <typename T>\n" 7808 "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); 7809 verifyFormat("template <typename T>\n" 7810 "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" 7811 " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); 7812 verifyFormat( 7813 "template <typename T>\n" 7814 "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" 7815 " int Paaaaaaaaaaaaaaaaaaaaram2);"); 7816 verifyFormat( 7817 "template <typename T>\n" 7818 "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" 7819 " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" 7820 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7821 verifyFormat("template <typename T>\n" 7822 "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7823 " int aaaaaaaaaaaaaaaaaaaaaa);"); 7824 verifyFormat( 7825 "template <typename T1, typename T2 = char, typename T3 = char,\n" 7826 " typename T4 = char>\n" 7827 "void f();"); 7828 verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n" 7829 " template <typename> class cccccccccccccccccccccc,\n" 7830 " typename ddddddddddddd>\n" 7831 "class C {};"); 7832 verifyFormat( 7833 "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" 7834 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 7835 7836 verifyFormat("void f() {\n" 7837 " a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" 7838 " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n" 7839 "}"); 7840 7841 verifyFormat("template <typename T> class C {};"); 7842 verifyFormat("template <typename T> void f();"); 7843 verifyFormat("template <typename T> void f() {}"); 7844 verifyFormat( 7845 "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 7846 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7847 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n" 7848 " new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n" 7849 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7850 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" 7851 " bbbbbbbbbbbbbbbbbbbbbbbb);", 7852 getLLVMStyleWithColumns(72)); 7853 EXPECT_EQ("static_cast<A< //\n" 7854 " B> *>(\n" 7855 "\n" 7856 ");", 7857 format("static_cast<A<//\n" 7858 " B>*>(\n" 7859 "\n" 7860 " );")); 7861 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 7862 " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); 7863 7864 FormatStyle AlwaysBreak = getLLVMStyle(); 7865 AlwaysBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 7866 verifyFormat("template <typename T>\nclass C {};", AlwaysBreak); 7867 verifyFormat("template <typename T>\nvoid f();", AlwaysBreak); 7868 verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak); 7869 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7870 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 7871 " ccccccccccccccccccccccccccccccccccccccccccccccc);"); 7872 verifyFormat("template <template <typename> class Fooooooo,\n" 7873 " template <typename> class Baaaaaaar>\n" 7874 "struct C {};", 7875 AlwaysBreak); 7876 verifyFormat("template <typename T> // T can be A, B or C.\n" 7877 "struct C {};", 7878 AlwaysBreak); 7879 verifyFormat("template <enum E> class A {\n" 7880 "public:\n" 7881 " E *f();\n" 7882 "};"); 7883 7884 FormatStyle NeverBreak = getLLVMStyle(); 7885 NeverBreak.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_No; 7886 verifyFormat("template <typename T> class C {};", NeverBreak); 7887 verifyFormat("template <typename T> void f();", NeverBreak); 7888 verifyFormat("template <typename T> void f() {}", NeverBreak); 7889 verifyFormat("template <typename T>\nvoid foo(aaaaaaaaaaaaaaaaaaaaaaaaaa " 7890 "bbbbbbbbbbbbbbbbbbbb) {}", 7891 NeverBreak); 7892 verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 7893 " bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n" 7894 " ccccccccccccccccccccccccccccccccccccccccccccccc);", 7895 NeverBreak); 7896 verifyFormat("template <template <typename> class Fooooooo,\n" 7897 " template <typename> class Baaaaaaar>\n" 7898 "struct C {};", 7899 NeverBreak); 7900 verifyFormat("template <typename T> // T can be A, B or C.\n" 7901 "struct C {};", 7902 NeverBreak); 7903 verifyFormat("template <enum E> class A {\n" 7904 "public:\n" 7905 " E *f();\n" 7906 "};", 7907 NeverBreak); 7908 NeverBreak.PenaltyBreakTemplateDeclaration = 100; 7909 verifyFormat("template <typename T> void\nfoo(aaaaaaaaaaaaaaaaaaaaaaaaaa " 7910 "bbbbbbbbbbbbbbbbbbbb) {}", 7911 NeverBreak); 7912 } 7913 7914 TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) { 7915 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 7916 Style.ColumnLimit = 60; 7917 EXPECT_EQ("// Baseline - no comments.\n" 7918 "template <\n" 7919 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 7920 "void f() {}", 7921 format("// Baseline - no comments.\n" 7922 "template <\n" 7923 " typename aaaaaaaaaaaaaaaaaaaaaa<bbbbbbbbbbbb>::value>\n" 7924 "void f() {}", 7925 Style)); 7926 7927 EXPECT_EQ("template <\n" 7928 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 7929 "void f() {}", 7930 format("template <\n" 7931 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 7932 "void f() {}", 7933 Style)); 7934 7935 EXPECT_EQ( 7936 "template <\n" 7937 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 7938 "void f() {}", 7939 format("template <typename aaaaaaaaaa<bbbbbbbbbbbb>::value> /* line */\n" 7940 "void f() {}", 7941 Style)); 7942 7943 EXPECT_EQ( 7944 "template <\n" 7945 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 7946 " // multiline\n" 7947 "void f() {}", 7948 format("template <\n" 7949 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing\n" 7950 " // multiline\n" 7951 "void f() {}", 7952 Style)); 7953 7954 EXPECT_EQ( 7955 "template <typename aaaaaaaaaa<\n" 7956 " bbbbbbbbbbbb>::value> // trailing loooong\n" 7957 "void f() {}", 7958 format( 7959 "template <\n" 7960 " typename aaaaaaaaaa<bbbbbbbbbbbb>::value> // trailing loooong\n" 7961 "void f() {}", 7962 Style)); 7963 } 7964 7965 TEST_F(FormatTest, WrapsTemplateParameters) { 7966 FormatStyle Style = getLLVMStyle(); 7967 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7968 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 7969 verifyFormat( 7970 "template <typename... a> struct q {};\n" 7971 "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 7972 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 7973 " y;", 7974 Style); 7975 Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; 7976 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 7977 verifyFormat( 7978 "template <typename... a> struct r {};\n" 7979 "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n" 7980 " aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n" 7981 " y;", 7982 Style); 7983 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 7984 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 7985 verifyFormat("template <typename... a> struct s {};\n" 7986 "extern s<\n" 7987 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 7988 "aaaaaaaaaaaaaaaaaaaaaa,\n" 7989 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 7990 "aaaaaaaaaaaaaaaaaaaaaa>\n" 7991 " y;", 7992 Style); 7993 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 7994 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 7995 verifyFormat("template <typename... a> struct t {};\n" 7996 "extern t<\n" 7997 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 7998 "aaaaaaaaaaaaaaaaaaaaaa,\n" 7999 " aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, " 8000 "aaaaaaaaaaaaaaaaaaaaaa>\n" 8001 " y;", 8002 Style); 8003 } 8004 8005 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { 8006 verifyFormat( 8007 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8008 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8009 verifyFormat( 8010 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8011 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8012 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); 8013 8014 // FIXME: Should we have the extra indent after the second break? 8015 verifyFormat( 8016 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8017 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8018 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8019 8020 verifyFormat( 8021 "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" 8022 " cccccccccccccccccccccccccccccccccccccccccccccc());"); 8023 8024 // Breaking at nested name specifiers is generally not desirable. 8025 verifyFormat( 8026 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8027 " aaaaaaaaaaaaaaaaaaaaaaa);"); 8028 8029 verifyFormat("aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n" 8030 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8031 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 8032 " aaaaaaaaaaaaaaaaaaaaa);", 8033 getLLVMStyleWithColumns(74)); 8034 8035 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" 8036 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8037 " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); 8038 } 8039 8040 TEST_F(FormatTest, UnderstandsTemplateParameters) { 8041 verifyFormat("A<int> a;"); 8042 verifyFormat("A<A<A<int>>> a;"); 8043 verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); 8044 verifyFormat("bool x = a < 1 || 2 > a;"); 8045 verifyFormat("bool x = 5 < f<int>();"); 8046 verifyFormat("bool x = f<int>() > 5;"); 8047 verifyFormat("bool x = 5 < a<int>::x;"); 8048 verifyFormat("bool x = a < 4 ? a > 2 : false;"); 8049 verifyFormat("bool x = f() ? a < 2 : a > 2;"); 8050 8051 verifyGoogleFormat("A<A<int>> a;"); 8052 verifyGoogleFormat("A<A<A<int>>> a;"); 8053 verifyGoogleFormat("A<A<A<A<int>>>> a;"); 8054 verifyGoogleFormat("A<A<int> > a;"); 8055 verifyGoogleFormat("A<A<A<int> > > a;"); 8056 verifyGoogleFormat("A<A<A<A<int> > > > a;"); 8057 verifyGoogleFormat("A<::A<int>> a;"); 8058 verifyGoogleFormat("A<::A> a;"); 8059 verifyGoogleFormat("A< ::A> a;"); 8060 verifyGoogleFormat("A< ::A<int> > a;"); 8061 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); 8062 EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); 8063 EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle())); 8064 EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle())); 8065 EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };", 8066 format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle())); 8067 8068 verifyFormat("A<A<int>> a;", getChromiumStyle(FormatStyle::LK_Cpp)); 8069 8070 // template closer followed by a token that starts with > or = 8071 verifyFormat("bool b = a<1> > 1;"); 8072 verifyFormat("bool b = a<1> >= 1;"); 8073 verifyFormat("int i = a<1> >> 1;"); 8074 FormatStyle Style = getLLVMStyle(); 8075 Style.SpaceBeforeAssignmentOperators = false; 8076 verifyFormat("bool b= a<1> == 1;", Style); 8077 verifyFormat("a<int> = 1;", Style); 8078 verifyFormat("a<int> >>= 1;", Style); 8079 8080 verifyFormat("test < a | b >> c;"); 8081 verifyFormat("test<test<a | b>> c;"); 8082 verifyFormat("test >> a >> b;"); 8083 verifyFormat("test << a >> b;"); 8084 8085 verifyFormat("f<int>();"); 8086 verifyFormat("template <typename T> void f() {}"); 8087 verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;"); 8088 verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : " 8089 "sizeof(char)>::type>;"); 8090 verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};"); 8091 verifyFormat("f(a.operator()<A>());"); 8092 verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8093 " .template operator()<A>());", 8094 getLLVMStyleWithColumns(35)); 8095 8096 // Not template parameters. 8097 verifyFormat("return a < b && c > d;"); 8098 verifyFormat("void f() {\n" 8099 " while (a < b && c > d) {\n" 8100 " }\n" 8101 "}"); 8102 verifyFormat("template <typename... Types>\n" 8103 "typename enable_if<0 < sizeof...(Types)>::type Foo() {}"); 8104 8105 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8106 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);", 8107 getLLVMStyleWithColumns(60)); 8108 verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");"); 8109 verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}"); 8110 verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); 8111 verifyFormat("some_templated_type<decltype([](int i) { return i; })>"); 8112 } 8113 8114 TEST_F(FormatTest, UnderstandsShiftOperators) { 8115 verifyFormat("if (i < x >> 1)"); 8116 verifyFormat("while (i < x >> 1)"); 8117 verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)"); 8118 verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)"); 8119 verifyFormat( 8120 "for (std::vector<int>::iterator i = 0; i < x >> 1; ++i, v = v >> 1)"); 8121 verifyFormat("Foo.call<Bar<Function>>()"); 8122 verifyFormat("if (Foo.call<Bar<Function>>() == 0)"); 8123 verifyFormat("for (std::vector<std::pair<int>>::iterator i = 0; i < x >> 1; " 8124 "++i, v = v >> 1)"); 8125 verifyFormat("if (w<u<v<x>>, 1>::t)"); 8126 } 8127 8128 TEST_F(FormatTest, BitshiftOperatorWidth) { 8129 EXPECT_EQ("int a = 1 << 2; /* foo\n" 8130 " bar */", 8131 format("int a=1<<2; /* foo\n" 8132 " bar */")); 8133 8134 EXPECT_EQ("int b = 256 >> 1; /* foo\n" 8135 " bar */", 8136 format("int b =256>>1 ; /* foo\n" 8137 " bar */")); 8138 } 8139 8140 TEST_F(FormatTest, UnderstandsBinaryOperators) { 8141 verifyFormat("COMPARE(a, ==, b);"); 8142 verifyFormat("auto s = sizeof...(Ts) - 1;"); 8143 } 8144 8145 TEST_F(FormatTest, UnderstandsPointersToMembers) { 8146 verifyFormat("int A::*x;"); 8147 verifyFormat("int (S::*func)(void *);"); 8148 verifyFormat("void f() { int (S::*func)(void *); }"); 8149 verifyFormat("typedef bool *(Class::*Member)() const;"); 8150 verifyFormat("void f() {\n" 8151 " (a->*f)();\n" 8152 " a->*x;\n" 8153 " (a.*f)();\n" 8154 " ((*a).*f)();\n" 8155 " a.*x;\n" 8156 "}"); 8157 verifyFormat("void f() {\n" 8158 " (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 8159 " aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n" 8160 "}"); 8161 verifyFormat( 8162 "(aaaaaaaaaa->*bbbbbbb)(\n" 8163 " aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); 8164 FormatStyle Style = getLLVMStyle(); 8165 Style.PointerAlignment = FormatStyle::PAS_Left; 8166 verifyFormat("typedef bool* (Class::*Member)() const;", Style); 8167 } 8168 8169 TEST_F(FormatTest, UnderstandsUnaryOperators) { 8170 verifyFormat("int a = -2;"); 8171 verifyFormat("f(-1, -2, -3);"); 8172 verifyFormat("a[-1] = 5;"); 8173 verifyFormat("int a = 5 + -2;"); 8174 verifyFormat("if (i == -1) {\n}"); 8175 verifyFormat("if (i != -1) {\n}"); 8176 verifyFormat("if (i > -1) {\n}"); 8177 verifyFormat("if (i < -1) {\n}"); 8178 verifyFormat("++(a->f());"); 8179 verifyFormat("--(a->f());"); 8180 verifyFormat("(a->f())++;"); 8181 verifyFormat("a[42]++;"); 8182 verifyFormat("if (!(a->f())) {\n}"); 8183 verifyFormat("if (!+i) {\n}"); 8184 verifyFormat("~&a;"); 8185 8186 verifyFormat("a-- > b;"); 8187 verifyFormat("b ? -a : c;"); 8188 verifyFormat("n * sizeof char16;"); 8189 verifyFormat("n * alignof char16;", getGoogleStyle()); 8190 verifyFormat("sizeof(char);"); 8191 verifyFormat("alignof(char);", getGoogleStyle()); 8192 8193 verifyFormat("return -1;"); 8194 verifyFormat("throw -1;"); 8195 verifyFormat("switch (a) {\n" 8196 "case -1:\n" 8197 " break;\n" 8198 "}"); 8199 verifyFormat("#define X -1"); 8200 verifyFormat("#define X -kConstant"); 8201 8202 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};"); 8203 verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};"); 8204 8205 verifyFormat("int a = /* confusing comment */ -1;"); 8206 // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. 8207 verifyFormat("int a = i /* confusing comment */++;"); 8208 8209 verifyFormat("co_yield -1;"); 8210 verifyFormat("co_return -1;"); 8211 8212 // Check that * is not treated as a binary operator when we set 8213 // PointerAlignment as PAS_Left after a keyword and not a declaration. 8214 FormatStyle PASLeftStyle = getLLVMStyle(); 8215 PASLeftStyle.PointerAlignment = FormatStyle::PAS_Left; 8216 verifyFormat("co_return *a;", PASLeftStyle); 8217 verifyFormat("co_await *a;", PASLeftStyle); 8218 verifyFormat("co_yield *a", PASLeftStyle); 8219 verifyFormat("return *a;", PASLeftStyle); 8220 } 8221 8222 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) { 8223 verifyFormat("if (!aaaaaaaaaa( // break\n" 8224 " aaaaa)) {\n" 8225 "}"); 8226 verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n" 8227 " aaaaa));"); 8228 verifyFormat("*aaa = aaaaaaa( // break\n" 8229 " bbbbbb);"); 8230 } 8231 8232 TEST_F(FormatTest, UnderstandsOverloadedOperators) { 8233 verifyFormat("bool operator<();"); 8234 verifyFormat("bool operator>();"); 8235 verifyFormat("bool operator=();"); 8236 verifyFormat("bool operator==();"); 8237 verifyFormat("bool operator!=();"); 8238 verifyFormat("int operator+();"); 8239 verifyFormat("int operator++();"); 8240 verifyFormat("int operator++(int) volatile noexcept;"); 8241 verifyFormat("bool operator,();"); 8242 verifyFormat("bool operator();"); 8243 verifyFormat("bool operator()();"); 8244 verifyFormat("bool operator[]();"); 8245 verifyFormat("operator bool();"); 8246 verifyFormat("operator int();"); 8247 verifyFormat("operator void *();"); 8248 verifyFormat("operator SomeType<int>();"); 8249 verifyFormat("operator SomeType<int, int>();"); 8250 verifyFormat("operator SomeType<SomeType<int>>();"); 8251 verifyFormat("void *operator new(std::size_t size);"); 8252 verifyFormat("void *operator new[](std::size_t size);"); 8253 verifyFormat("void operator delete(void *ptr);"); 8254 verifyFormat("void operator delete[](void *ptr);"); 8255 verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n" 8256 "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);"); 8257 verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n" 8258 " aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;"); 8259 8260 verifyFormat( 8261 "ostream &operator<<(ostream &OutputStream,\n" 8262 " SomeReallyLongType WithSomeReallyLongValue);"); 8263 verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" 8264 " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" 8265 " return left.group < right.group;\n" 8266 "}"); 8267 verifyFormat("SomeType &operator=(const SomeType &S);"); 8268 verifyFormat("f.template operator()<int>();"); 8269 8270 verifyGoogleFormat("operator void*();"); 8271 verifyGoogleFormat("operator SomeType<SomeType<int>>();"); 8272 verifyGoogleFormat("operator ::A();"); 8273 8274 verifyFormat("using A::operator+;"); 8275 verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n" 8276 "int i;"); 8277 8278 // Calling an operator as a member function. 8279 verifyFormat("void f() { a.operator*(); }"); 8280 verifyFormat("void f() { a.operator*(b & b); }"); 8281 verifyFormat("void f() { a->operator&(a * b); }"); 8282 verifyFormat("void f() { NS::a.operator+(*b * *b); }"); 8283 // TODO: Calling an operator as a non-member function is hard to distinguish. 8284 // https://llvm.org/PR50629 8285 // verifyFormat("void f() { operator*(a & a); }"); 8286 // verifyFormat("void f() { operator&(a, b * b); }"); 8287 } 8288 8289 TEST_F(FormatTest, UnderstandsFunctionRefQualification) { 8290 verifyFormat("Deleted &operator=(const Deleted &) & = default;"); 8291 verifyFormat("Deleted &operator=(const Deleted &) && = delete;"); 8292 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;"); 8293 verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;"); 8294 verifyFormat("Deleted &operator=(const Deleted &) &;"); 8295 verifyFormat("Deleted &operator=(const Deleted &) &&;"); 8296 verifyFormat("SomeType MemberFunction(const Deleted &) &;"); 8297 verifyFormat("SomeType MemberFunction(const Deleted &) &&;"); 8298 verifyFormat("SomeType MemberFunction(const Deleted &) && {}"); 8299 verifyFormat("SomeType MemberFunction(const Deleted &) && final {}"); 8300 verifyFormat("SomeType MemberFunction(const Deleted &) && override {}"); 8301 verifyFormat("void Fn(T const &) const &;"); 8302 verifyFormat("void Fn(T const volatile &&) const volatile &&;"); 8303 verifyFormat("template <typename T>\n" 8304 "void F(T) && = delete;", 8305 getGoogleStyle()); 8306 8307 FormatStyle AlignLeft = getLLVMStyle(); 8308 AlignLeft.PointerAlignment = FormatStyle::PAS_Left; 8309 verifyFormat("void A::b() && {}", AlignLeft); 8310 verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft); 8311 verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;", 8312 AlignLeft); 8313 verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft); 8314 verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft); 8315 verifyFormat("auto Function(T t) & -> void {}", AlignLeft); 8316 verifyFormat("auto Function(T... t) & -> void {}", AlignLeft); 8317 verifyFormat("auto Function(T) & -> void {}", AlignLeft); 8318 verifyFormat("auto Function(T) & -> void;", AlignLeft); 8319 verifyFormat("void Fn(T const&) const&;", AlignLeft); 8320 verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft); 8321 8322 FormatStyle Spaces = getLLVMStyle(); 8323 Spaces.SpacesInCStyleCastParentheses = true; 8324 verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces); 8325 verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces); 8326 verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces); 8327 verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces); 8328 8329 Spaces.SpacesInCStyleCastParentheses = false; 8330 Spaces.SpacesInParentheses = true; 8331 verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces); 8332 verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", 8333 Spaces); 8334 verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces); 8335 verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces); 8336 8337 FormatStyle BreakTemplate = getLLVMStyle(); 8338 BreakTemplate.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 8339 8340 verifyFormat("struct f {\n" 8341 " template <class T>\n" 8342 " int &foo(const std::string &str) &noexcept {}\n" 8343 "};", 8344 BreakTemplate); 8345 8346 verifyFormat("struct f {\n" 8347 " template <class T>\n" 8348 " int &foo(const std::string &str) &&noexcept {}\n" 8349 "};", 8350 BreakTemplate); 8351 8352 verifyFormat("struct f {\n" 8353 " template <class T>\n" 8354 " int &foo(const std::string &str) const &noexcept {}\n" 8355 "};", 8356 BreakTemplate); 8357 8358 verifyFormat("struct f {\n" 8359 " template <class T>\n" 8360 " int &foo(const std::string &str) const &noexcept {}\n" 8361 "};", 8362 BreakTemplate); 8363 8364 verifyFormat("struct f {\n" 8365 " template <class T>\n" 8366 " auto foo(const std::string &str) &&noexcept -> int & {}\n" 8367 "};", 8368 BreakTemplate); 8369 8370 FormatStyle AlignLeftBreakTemplate = getLLVMStyle(); 8371 AlignLeftBreakTemplate.AlwaysBreakTemplateDeclarations = 8372 FormatStyle::BTDS_Yes; 8373 AlignLeftBreakTemplate.PointerAlignment = FormatStyle::PAS_Left; 8374 8375 verifyFormat("struct f {\n" 8376 " template <class T>\n" 8377 " int& foo(const std::string& str) & noexcept {}\n" 8378 "};", 8379 AlignLeftBreakTemplate); 8380 8381 verifyFormat("struct f {\n" 8382 " template <class T>\n" 8383 " int& foo(const std::string& str) && noexcept {}\n" 8384 "};", 8385 AlignLeftBreakTemplate); 8386 8387 verifyFormat("struct f {\n" 8388 " template <class T>\n" 8389 " int& foo(const std::string& str) const& noexcept {}\n" 8390 "};", 8391 AlignLeftBreakTemplate); 8392 8393 verifyFormat("struct f {\n" 8394 " template <class T>\n" 8395 " int& foo(const std::string& str) const&& noexcept {}\n" 8396 "};", 8397 AlignLeftBreakTemplate); 8398 8399 verifyFormat("struct f {\n" 8400 " template <class T>\n" 8401 " auto foo(const std::string& str) && noexcept -> int& {}\n" 8402 "};", 8403 AlignLeftBreakTemplate); 8404 8405 // The `&` in `Type&` should not be confused with a trailing `&` of 8406 // DEPRECATED(reason) member function. 8407 verifyFormat("struct f {\n" 8408 " template <class T>\n" 8409 " DEPRECATED(reason)\n" 8410 " Type &foo(arguments) {}\n" 8411 "};", 8412 BreakTemplate); 8413 8414 verifyFormat("struct f {\n" 8415 " template <class T>\n" 8416 " DEPRECATED(reason)\n" 8417 " Type& foo(arguments) {}\n" 8418 "};", 8419 AlignLeftBreakTemplate); 8420 8421 verifyFormat("void (*foopt)(int) = &func;"); 8422 } 8423 8424 TEST_F(FormatTest, UnderstandsNewAndDelete) { 8425 verifyFormat("void f() {\n" 8426 " A *a = new A;\n" 8427 " A *a = new (placement) A;\n" 8428 " delete a;\n" 8429 " delete (A *)a;\n" 8430 "}"); 8431 verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 8432 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 8433 verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 8434 " new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n" 8435 " typename aaaaaaaaaaaaaaaaaaaaaaaa();"); 8436 verifyFormat("delete[] h->p;"); 8437 } 8438 8439 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 8440 verifyFormat("int *f(int *a) {}"); 8441 verifyFormat("int main(int argc, char **argv) {}"); 8442 verifyFormat("Test::Test(int b) : a(b * b) {}"); 8443 verifyIndependentOfContext("f(a, *a);"); 8444 verifyFormat("void g() { f(*a); }"); 8445 verifyIndependentOfContext("int a = b * 10;"); 8446 verifyIndependentOfContext("int a = 10 * b;"); 8447 verifyIndependentOfContext("int a = b * c;"); 8448 verifyIndependentOfContext("int a += b * c;"); 8449 verifyIndependentOfContext("int a -= b * c;"); 8450 verifyIndependentOfContext("int a *= b * c;"); 8451 verifyIndependentOfContext("int a /= b * c;"); 8452 verifyIndependentOfContext("int a = *b;"); 8453 verifyIndependentOfContext("int a = *b * c;"); 8454 verifyIndependentOfContext("int a = b * *c;"); 8455 verifyIndependentOfContext("int a = b * (10);"); 8456 verifyIndependentOfContext("S << b * (10);"); 8457 verifyIndependentOfContext("return 10 * b;"); 8458 verifyIndependentOfContext("return *b * *c;"); 8459 verifyIndependentOfContext("return a & ~b;"); 8460 verifyIndependentOfContext("f(b ? *c : *d);"); 8461 verifyIndependentOfContext("int a = b ? *c : *d;"); 8462 verifyIndependentOfContext("*b = a;"); 8463 verifyIndependentOfContext("a * ~b;"); 8464 verifyIndependentOfContext("a * !b;"); 8465 verifyIndependentOfContext("a * +b;"); 8466 verifyIndependentOfContext("a * -b;"); 8467 verifyIndependentOfContext("a * ++b;"); 8468 verifyIndependentOfContext("a * --b;"); 8469 verifyIndependentOfContext("a[4] * b;"); 8470 verifyIndependentOfContext("a[a * a] = 1;"); 8471 verifyIndependentOfContext("f() * b;"); 8472 verifyIndependentOfContext("a * [self dostuff];"); 8473 verifyIndependentOfContext("int x = a * (a + b);"); 8474 verifyIndependentOfContext("(a *)(a + b);"); 8475 verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;"); 8476 verifyIndependentOfContext("int *pa = (int *)&a;"); 8477 verifyIndependentOfContext("return sizeof(int **);"); 8478 verifyIndependentOfContext("return sizeof(int ******);"); 8479 verifyIndependentOfContext("return (int **&)a;"); 8480 verifyIndependentOfContext("f((*PointerToArray)[10]);"); 8481 verifyFormat("void f(Type (*parameter)[10]) {}"); 8482 verifyFormat("void f(Type (¶meter)[10]) {}"); 8483 verifyGoogleFormat("return sizeof(int**);"); 8484 verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); 8485 verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); 8486 verifyFormat("auto a = [](int **&, int ***) {};"); 8487 verifyFormat("auto PointerBinding = [](const char *S) {};"); 8488 verifyFormat("typedef typeof(int(int, int)) *MyFunc;"); 8489 verifyFormat("[](const decltype(*a) &value) {}"); 8490 verifyFormat("[](const typeof(*a) &value) {}"); 8491 verifyFormat("[](const _Atomic(a *) &value) {}"); 8492 verifyFormat("[](const __underlying_type(a) &value) {}"); 8493 verifyFormat("decltype(a * b) F();"); 8494 verifyFormat("typeof(a * b) F();"); 8495 verifyFormat("#define MACRO() [](A *a) { return 1; }"); 8496 verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); 8497 verifyIndependentOfContext("typedef void (*f)(int *a);"); 8498 verifyIndependentOfContext("int i{a * b};"); 8499 verifyIndependentOfContext("aaa && aaa->f();"); 8500 verifyIndependentOfContext("int x = ~*p;"); 8501 verifyFormat("Constructor() : a(a), area(width * height) {}"); 8502 verifyFormat("Constructor() : a(a), area(a, width * height) {}"); 8503 verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}"); 8504 verifyFormat("void f() { f(a, c * d); }"); 8505 verifyFormat("void f() { f(new a(), c * d); }"); 8506 verifyFormat("void f(const MyOverride &override);"); 8507 verifyFormat("void f(const MyFinal &final);"); 8508 verifyIndependentOfContext("bool a = f() && override.f();"); 8509 verifyIndependentOfContext("bool a = f() && final.f();"); 8510 8511 verifyIndependentOfContext("InvalidRegions[*R] = 0;"); 8512 8513 verifyIndependentOfContext("A<int *> a;"); 8514 verifyIndependentOfContext("A<int **> a;"); 8515 verifyIndependentOfContext("A<int *, int *> a;"); 8516 verifyIndependentOfContext("A<int *[]> a;"); 8517 verifyIndependentOfContext( 8518 "const char *const p = reinterpret_cast<const char *const>(q);"); 8519 verifyIndependentOfContext("A<int **, int **> a;"); 8520 verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); 8521 verifyFormat("for (char **a = b; *a; ++a) {\n}"); 8522 verifyFormat("for (; a && b;) {\n}"); 8523 verifyFormat("bool foo = true && [] { return false; }();"); 8524 8525 verifyFormat( 8526 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 8527 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 8528 8529 verifyGoogleFormat("int const* a = &b;"); 8530 verifyGoogleFormat("**outparam = 1;"); 8531 verifyGoogleFormat("*outparam = a * b;"); 8532 verifyGoogleFormat("int main(int argc, char** argv) {}"); 8533 verifyGoogleFormat("A<int*> a;"); 8534 verifyGoogleFormat("A<int**> a;"); 8535 verifyGoogleFormat("A<int*, int*> a;"); 8536 verifyGoogleFormat("A<int**, int**> a;"); 8537 verifyGoogleFormat("f(b ? *c : *d);"); 8538 verifyGoogleFormat("int a = b ? *c : *d;"); 8539 verifyGoogleFormat("Type* t = **x;"); 8540 verifyGoogleFormat("Type* t = *++*x;"); 8541 verifyGoogleFormat("*++*x;"); 8542 verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); 8543 verifyGoogleFormat("Type* t = x++ * y;"); 8544 verifyGoogleFormat( 8545 "const char* const p = reinterpret_cast<const char* const>(q);"); 8546 verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);"); 8547 verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);"); 8548 verifyGoogleFormat("template <typename T>\n" 8549 "void f(int i = 0, SomeType** temps = NULL);"); 8550 8551 FormatStyle Left = getLLVMStyle(); 8552 Left.PointerAlignment = FormatStyle::PAS_Left; 8553 verifyFormat("x = *a(x) = *a(y);", Left); 8554 verifyFormat("for (;; *a = b) {\n}", Left); 8555 verifyFormat("return *this += 1;", Left); 8556 verifyFormat("throw *x;", Left); 8557 verifyFormat("delete *x;", Left); 8558 verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left); 8559 verifyFormat("[](const decltype(*a)* ptr) {}", Left); 8560 verifyFormat("[](const typeof(*a)* ptr) {}", Left); 8561 verifyFormat("[](const _Atomic(a*)* ptr) {}", Left); 8562 verifyFormat("[](const __underlying_type(a)* ptr) {}", Left); 8563 verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left); 8564 verifyFormat("auto x(A&&, B&&, C&&) -> D;", Left); 8565 verifyFormat("auto x = [](A&&, B&&, C&&) -> D {};", Left); 8566 verifyFormat("template <class T> X(T&&, T&&, T&&) -> X<T>;", Left); 8567 8568 verifyIndependentOfContext("a = *(x + y);"); 8569 verifyIndependentOfContext("a = &(x + y);"); 8570 verifyIndependentOfContext("*(x + y).call();"); 8571 verifyIndependentOfContext("&(x + y)->call();"); 8572 verifyFormat("void f() { &(*I).first; }"); 8573 8574 verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); 8575 verifyFormat( 8576 "int *MyValues = {\n" 8577 " *A, // Operator detection might be confused by the '{'\n" 8578 " *BB // Operator detection might be confused by previous comment\n" 8579 "};"); 8580 8581 verifyIndependentOfContext("if (int *a = &b)"); 8582 verifyIndependentOfContext("if (int &a = *b)"); 8583 verifyIndependentOfContext("if (a & b[i])"); 8584 verifyIndependentOfContext("if constexpr (a & b[i])"); 8585 verifyIndependentOfContext("if CONSTEXPR (a & b[i])"); 8586 verifyIndependentOfContext("if (a * (b * c))"); 8587 verifyIndependentOfContext("if constexpr (a * (b * c))"); 8588 verifyIndependentOfContext("if CONSTEXPR (a * (b * c))"); 8589 verifyIndependentOfContext("if (a::b::c::d & b[i])"); 8590 verifyIndependentOfContext("if (*b[i])"); 8591 verifyIndependentOfContext("if (int *a = (&b))"); 8592 verifyIndependentOfContext("while (int *a = &b)"); 8593 verifyIndependentOfContext("while (a * (b * c))"); 8594 verifyIndependentOfContext("size = sizeof *a;"); 8595 verifyIndependentOfContext("if (a && (b = c))"); 8596 verifyFormat("void f() {\n" 8597 " for (const int &v : Values) {\n" 8598 " }\n" 8599 "}"); 8600 verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); 8601 verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); 8602 verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}"); 8603 8604 verifyFormat("#define A (!a * b)"); 8605 verifyFormat("#define MACRO \\\n" 8606 " int *i = a * b; \\\n" 8607 " void f(a *b);", 8608 getLLVMStyleWithColumns(19)); 8609 8610 verifyIndependentOfContext("A = new SomeType *[Length];"); 8611 verifyIndependentOfContext("A = new SomeType *[Length]();"); 8612 verifyIndependentOfContext("T **t = new T *;"); 8613 verifyIndependentOfContext("T **t = new T *();"); 8614 verifyGoogleFormat("A = new SomeType*[Length]();"); 8615 verifyGoogleFormat("A = new SomeType*[Length];"); 8616 verifyGoogleFormat("T** t = new T*;"); 8617 verifyGoogleFormat("T** t = new T*();"); 8618 8619 verifyFormat("STATIC_ASSERT((a & b) == 0);"); 8620 verifyFormat("STATIC_ASSERT(0 == (a & b));"); 8621 verifyFormat("template <bool a, bool b> " 8622 "typename t::if<x && y>::type f() {}"); 8623 verifyFormat("template <int *y> f() {}"); 8624 verifyFormat("vector<int *> v;"); 8625 verifyFormat("vector<int *const> v;"); 8626 verifyFormat("vector<int *const **const *> v;"); 8627 verifyFormat("vector<int *volatile> v;"); 8628 verifyFormat("vector<a *_Nonnull> v;"); 8629 verifyFormat("vector<a *_Nullable> v;"); 8630 verifyFormat("vector<a *_Null_unspecified> v;"); 8631 verifyFormat("vector<a *__ptr32> v;"); 8632 verifyFormat("vector<a *__ptr64> v;"); 8633 verifyFormat("vector<a *__capability> v;"); 8634 FormatStyle TypeMacros = getLLVMStyle(); 8635 TypeMacros.TypenameMacros = {"LIST"}; 8636 verifyFormat("vector<LIST(uint64_t)> v;", TypeMacros); 8637 verifyFormat("vector<LIST(uint64_t) *> v;", TypeMacros); 8638 verifyFormat("vector<LIST(uint64_t) **> v;", TypeMacros); 8639 verifyFormat("vector<LIST(uint64_t) *attr> v;", TypeMacros); 8640 verifyFormat("vector<A(uint64_t) * attr> v;", TypeMacros); // multiplication 8641 8642 FormatStyle CustomQualifier = getLLVMStyle(); 8643 // Add indentifers that should not be parsed as a qualifier by default. 8644 CustomQualifier.AttributeMacros.push_back("__my_qualifier"); 8645 CustomQualifier.AttributeMacros.push_back("_My_qualifier"); 8646 CustomQualifier.AttributeMacros.push_back("my_other_qualifier"); 8647 verifyFormat("vector<a * __my_qualifier> parse_as_multiply;"); 8648 verifyFormat("vector<a *__my_qualifier> v;", CustomQualifier); 8649 verifyFormat("vector<a * _My_qualifier> parse_as_multiply;"); 8650 verifyFormat("vector<a *_My_qualifier> v;", CustomQualifier); 8651 verifyFormat("vector<a * my_other_qualifier> parse_as_multiply;"); 8652 verifyFormat("vector<a *my_other_qualifier> v;", CustomQualifier); 8653 verifyFormat("vector<a * _NotAQualifier> v;"); 8654 verifyFormat("vector<a * __not_a_qualifier> v;"); 8655 verifyFormat("vector<a * b> v;"); 8656 verifyFormat("foo<b && false>();"); 8657 verifyFormat("foo<b & 1>();"); 8658 verifyFormat("decltype(*::std::declval<const T &>()) void F();"); 8659 verifyFormat("typeof(*::std::declval<const T &>()) void F();"); 8660 verifyFormat("_Atomic(*::std::declval<const T &>()) void F();"); 8661 verifyFormat("__underlying_type(*::std::declval<const T &>()) void F();"); 8662 verifyFormat( 8663 "template <class T, class = typename std::enable_if<\n" 8664 " std::is_integral<T>::value &&\n" 8665 " (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n" 8666 "void F();", 8667 getLLVMStyleWithColumns(70)); 8668 verifyFormat("template <class T,\n" 8669 " class = typename std::enable_if<\n" 8670 " std::is_integral<T>::value &&\n" 8671 " (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n" 8672 " class U>\n" 8673 "void F();", 8674 getLLVMStyleWithColumns(70)); 8675 verifyFormat( 8676 "template <class T,\n" 8677 " class = typename ::std::enable_if<\n" 8678 " ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n" 8679 "void F();", 8680 getGoogleStyleWithColumns(68)); 8681 8682 verifyIndependentOfContext("MACRO(int *i);"); 8683 verifyIndependentOfContext("MACRO(auto *a);"); 8684 verifyIndependentOfContext("MACRO(const A *a);"); 8685 verifyIndependentOfContext("MACRO(_Atomic(A) *a);"); 8686 verifyIndependentOfContext("MACRO(decltype(A) *a);"); 8687 verifyIndependentOfContext("MACRO(typeof(A) *a);"); 8688 verifyIndependentOfContext("MACRO(__underlying_type(A) *a);"); 8689 verifyIndependentOfContext("MACRO(A *const a);"); 8690 verifyIndependentOfContext("MACRO(A *restrict a);"); 8691 verifyIndependentOfContext("MACRO(A *__restrict__ a);"); 8692 verifyIndependentOfContext("MACRO(A *__restrict a);"); 8693 verifyIndependentOfContext("MACRO(A *volatile a);"); 8694 verifyIndependentOfContext("MACRO(A *__volatile a);"); 8695 verifyIndependentOfContext("MACRO(A *__volatile__ a);"); 8696 verifyIndependentOfContext("MACRO(A *_Nonnull a);"); 8697 verifyIndependentOfContext("MACRO(A *_Nullable a);"); 8698 verifyIndependentOfContext("MACRO(A *_Null_unspecified a);"); 8699 verifyIndependentOfContext("MACRO(A *__attribute__((foo)) a);"); 8700 verifyIndependentOfContext("MACRO(A *__attribute((foo)) a);"); 8701 verifyIndependentOfContext("MACRO(A *[[clang::attr]] a);"); 8702 verifyIndependentOfContext("MACRO(A *[[clang::attr(\"foo\")]] a);"); 8703 verifyIndependentOfContext("MACRO(A *__ptr32 a);"); 8704 verifyIndependentOfContext("MACRO(A *__ptr64 a);"); 8705 verifyIndependentOfContext("MACRO(A *__capability);"); 8706 verifyIndependentOfContext("MACRO(A &__capability);"); 8707 verifyFormat("MACRO(A *__my_qualifier);"); // type declaration 8708 verifyFormat("void f() { MACRO(A * __my_qualifier); }"); // multiplication 8709 // If we add __my_qualifier to AttributeMacros it should always be parsed as 8710 // a type declaration: 8711 verifyFormat("MACRO(A *__my_qualifier);", CustomQualifier); 8712 verifyFormat("void f() { MACRO(A *__my_qualifier); }", CustomQualifier); 8713 // Also check that TypenameMacros prevents parsing it as multiplication: 8714 verifyIndependentOfContext("MACRO(LIST(uint64_t) * a);"); // multiplication 8715 verifyIndependentOfContext("MACRO(LIST(uint64_t) *a);", TypeMacros); // type 8716 8717 verifyIndependentOfContext("MACRO('0' <= c && c <= '9');"); 8718 verifyFormat("void f() { f(float{1}, a * a); }"); 8719 verifyFormat("void f() { f(float(1), a * a); }"); 8720 8721 verifyFormat("f((void (*)(int))g);"); 8722 verifyFormat("f((void (&)(int))g);"); 8723 verifyFormat("f((void (^)(int))g);"); 8724 8725 // FIXME: Is there a way to make this work? 8726 // verifyIndependentOfContext("MACRO(A *a);"); 8727 verifyFormat("MACRO(A &B);"); 8728 verifyFormat("MACRO(A *B);"); 8729 verifyFormat("void f() { MACRO(A * B); }"); 8730 verifyFormat("void f() { MACRO(A & B); }"); 8731 8732 // This lambda was mis-formatted after D88956 (treating it as a binop): 8733 verifyFormat("auto x = [](const decltype(x) &ptr) {};"); 8734 verifyFormat("auto x = [](const decltype(x) *ptr) {};"); 8735 verifyFormat("#define lambda [](const decltype(x) &ptr) {}"); 8736 verifyFormat("#define lambda [](const decltype(x) *ptr) {}"); 8737 8738 verifyFormat("DatumHandle const *operator->() const { return input_; }"); 8739 verifyFormat("return options != nullptr && operator==(*options);"); 8740 8741 EXPECT_EQ("#define OP(x) \\\n" 8742 " ostream &operator<<(ostream &s, const A &a) { \\\n" 8743 " return s << a.DebugString(); \\\n" 8744 " }", 8745 format("#define OP(x) \\\n" 8746 " ostream &operator<<(ostream &s, const A &a) { \\\n" 8747 " return s << a.DebugString(); \\\n" 8748 " }", 8749 getLLVMStyleWithColumns(50))); 8750 8751 // FIXME: We cannot handle this case yet; we might be able to figure out that 8752 // foo<x> d > v; doesn't make sense. 8753 verifyFormat("foo<a<b && c> d> v;"); 8754 8755 FormatStyle PointerMiddle = getLLVMStyle(); 8756 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 8757 verifyFormat("delete *x;", PointerMiddle); 8758 verifyFormat("int * x;", PointerMiddle); 8759 verifyFormat("int *[] x;", PointerMiddle); 8760 verifyFormat("template <int * y> f() {}", PointerMiddle); 8761 verifyFormat("int * f(int * a) {}", PointerMiddle); 8762 verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle); 8763 verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle); 8764 verifyFormat("A<int *> a;", PointerMiddle); 8765 verifyFormat("A<int **> a;", PointerMiddle); 8766 verifyFormat("A<int *, int *> a;", PointerMiddle); 8767 verifyFormat("A<int *[]> a;", PointerMiddle); 8768 verifyFormat("A = new SomeType *[Length]();", PointerMiddle); 8769 verifyFormat("A = new SomeType *[Length];", PointerMiddle); 8770 verifyFormat("T ** t = new T *;", PointerMiddle); 8771 8772 // Member function reference qualifiers aren't binary operators. 8773 verifyFormat("string // break\n" 8774 "operator()() & {}"); 8775 verifyFormat("string // break\n" 8776 "operator()() && {}"); 8777 verifyGoogleFormat("template <typename T>\n" 8778 "auto x() & -> int {}"); 8779 8780 // Should be binary operators when used as an argument expression (overloaded 8781 // operator invoked as a member function). 8782 verifyFormat("void f() { a.operator()(a * a); }"); 8783 verifyFormat("void f() { a->operator()(a & a); }"); 8784 verifyFormat("void f() { a.operator()(*a & *a); }"); 8785 verifyFormat("void f() { a->operator()(*a * *a); }"); 8786 } 8787 8788 TEST_F(FormatTest, UnderstandsAttributes) { 8789 verifyFormat("SomeType s __attribute__((unused)) (InitValue);"); 8790 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n" 8791 "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 8792 FormatStyle AfterType = getLLVMStyle(); 8793 AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 8794 verifyFormat("__attribute__((nodebug)) void\n" 8795 "foo() {}\n", 8796 AfterType); 8797 verifyFormat("__unused void\n" 8798 "foo() {}", 8799 AfterType); 8800 8801 FormatStyle CustomAttrs = getLLVMStyle(); 8802 CustomAttrs.AttributeMacros.push_back("__unused"); 8803 CustomAttrs.AttributeMacros.push_back("__attr1"); 8804 CustomAttrs.AttributeMacros.push_back("__attr2"); 8805 CustomAttrs.AttributeMacros.push_back("no_underscore_attr"); 8806 verifyFormat("vector<SomeType *__attribute((foo))> v;"); 8807 verifyFormat("vector<SomeType *__attribute__((foo))> v;"); 8808 verifyFormat("vector<SomeType * __not_attribute__((foo))> v;"); 8809 // Check that it is parsed as a multiplication without AttributeMacros and 8810 // as a pointer qualifier when we add __attr1/__attr2 to AttributeMacros. 8811 verifyFormat("vector<SomeType * __attr1> v;"); 8812 verifyFormat("vector<SomeType __attr1 *> v;"); 8813 verifyFormat("vector<SomeType __attr1 *const> v;"); 8814 verifyFormat("vector<SomeType __attr1 * __attr2> v;"); 8815 verifyFormat("vector<SomeType *__attr1> v;", CustomAttrs); 8816 verifyFormat("vector<SomeType *__attr2> v;", CustomAttrs); 8817 verifyFormat("vector<SomeType *no_underscore_attr> v;", CustomAttrs); 8818 verifyFormat("vector<SomeType __attr1 *> v;", CustomAttrs); 8819 verifyFormat("vector<SomeType __attr1 *const> v;", CustomAttrs); 8820 verifyFormat("vector<SomeType __attr1 *__attr2> v;", CustomAttrs); 8821 verifyFormat("vector<SomeType __attr1 *no_underscore_attr> v;", CustomAttrs); 8822 8823 // Check that these are not parsed as function declarations: 8824 CustomAttrs.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 8825 CustomAttrs.BreakBeforeBraces = FormatStyle::BS_Allman; 8826 verifyFormat("SomeType s(InitValue);", CustomAttrs); 8827 verifyFormat("SomeType s{InitValue};", CustomAttrs); 8828 verifyFormat("SomeType *__unused s(InitValue);", CustomAttrs); 8829 verifyFormat("SomeType *__unused s{InitValue};", CustomAttrs); 8830 verifyFormat("SomeType s __unused(InitValue);", CustomAttrs); 8831 verifyFormat("SomeType s __unused{InitValue};", CustomAttrs); 8832 verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs); 8833 verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs); 8834 } 8835 8836 TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) { 8837 // Check that qualifiers on pointers don't break parsing of casts. 8838 verifyFormat("x = (foo *const)*v;"); 8839 verifyFormat("x = (foo *volatile)*v;"); 8840 verifyFormat("x = (foo *restrict)*v;"); 8841 verifyFormat("x = (foo *__attribute__((foo)))*v;"); 8842 verifyFormat("x = (foo *_Nonnull)*v;"); 8843 verifyFormat("x = (foo *_Nullable)*v;"); 8844 verifyFormat("x = (foo *_Null_unspecified)*v;"); 8845 verifyFormat("x = (foo *_Nonnull)*v;"); 8846 verifyFormat("x = (foo *[[clang::attr]])*v;"); 8847 verifyFormat("x = (foo *[[clang::attr(\"foo\")]])*v;"); 8848 verifyFormat("x = (foo *__ptr32)*v;"); 8849 verifyFormat("x = (foo *__ptr64)*v;"); 8850 verifyFormat("x = (foo *__capability)*v;"); 8851 8852 // Check that we handle multiple trailing qualifiers and skip them all to 8853 // determine that the expression is a cast to a pointer type. 8854 FormatStyle LongPointerRight = getLLVMStyleWithColumns(999); 8855 FormatStyle LongPointerLeft = getLLVMStyleWithColumns(999); 8856 LongPointerLeft.PointerAlignment = FormatStyle::PAS_Left; 8857 StringRef AllQualifiers = 8858 "const volatile restrict __attribute__((foo)) _Nonnull _Null_unspecified " 8859 "_Nonnull [[clang::attr]] __ptr32 __ptr64 __capability"; 8860 verifyFormat(("x = (foo *" + AllQualifiers + ")*v;").str(), LongPointerRight); 8861 verifyFormat(("x = (foo* " + AllQualifiers + ")*v;").str(), LongPointerLeft); 8862 8863 // Also check that address-of is not parsed as a binary bitwise-and: 8864 verifyFormat("x = (foo *const)&v;"); 8865 verifyFormat(("x = (foo *" + AllQualifiers + ")&v;").str(), LongPointerRight); 8866 verifyFormat(("x = (foo* " + AllQualifiers + ")&v;").str(), LongPointerLeft); 8867 8868 // Check custom qualifiers: 8869 FormatStyle CustomQualifier = getLLVMStyleWithColumns(999); 8870 CustomQualifier.AttributeMacros.push_back("__my_qualifier"); 8871 verifyFormat("x = (foo * __my_qualifier) * v;"); // not parsed as qualifier. 8872 verifyFormat("x = (foo *__my_qualifier)*v;", CustomQualifier); 8873 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)*v;").str(), 8874 CustomQualifier); 8875 verifyFormat(("x = (foo *" + AllQualifiers + " __my_qualifier)&v;").str(), 8876 CustomQualifier); 8877 8878 // Check that unknown identifiers result in binary operator parsing: 8879 verifyFormat("x = (foo * __unknown_qualifier) * v;"); 8880 verifyFormat("x = (foo * __unknown_qualifier) & v;"); 8881 } 8882 8883 TEST_F(FormatTest, UnderstandsSquareAttributes) { 8884 verifyFormat("SomeType s [[unused]] (InitValue);"); 8885 verifyFormat("SomeType s [[gnu::unused]] (InitValue);"); 8886 verifyFormat("SomeType s [[using gnu: unused]] (InitValue);"); 8887 verifyFormat("[[gsl::suppress(\"clang-tidy-check-name\")]] void f() {}"); 8888 verifyFormat("void f() [[deprecated(\"so sorry\")]];"); 8889 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 8890 " [[unused]] aaaaaaaaaaaaaaaaaaaaaaa(int i);"); 8891 verifyFormat("[[nodiscard]] bool f() { return false; }"); 8892 verifyFormat("class [[nodiscard]] f {\npublic:\n f() {}\n}"); 8893 verifyFormat("class [[deprecated(\"so sorry\")]] f {\npublic:\n f() {}\n}"); 8894 verifyFormat("class [[gnu::unused]] f {\npublic:\n f() {}\n}"); 8895 8896 // Make sure we do not mistake attributes for array subscripts. 8897 verifyFormat("int a() {}\n" 8898 "[[unused]] int b() {}\n"); 8899 verifyFormat("NSArray *arr;\n" 8900 "arr[[Foo() bar]];"); 8901 8902 // On the other hand, we still need to correctly find array subscripts. 8903 verifyFormat("int a = std::vector<int>{1, 2, 3}[0];"); 8904 8905 // Make sure that we do not mistake Objective-C method inside array literals 8906 // as attributes, even if those method names are also keywords. 8907 verifyFormat("@[ [foo bar] ];"); 8908 verifyFormat("@[ [NSArray class] ];"); 8909 verifyFormat("@[ [foo enum] ];"); 8910 8911 verifyFormat("template <typename T> [[nodiscard]] int a() { return 1; }"); 8912 8913 // Make sure we do not parse attributes as lambda introducers. 8914 FormatStyle MultiLineFunctions = getLLVMStyle(); 8915 MultiLineFunctions.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 8916 verifyFormat("[[unused]] int b() {\n" 8917 " return 42;\n" 8918 "}\n", 8919 MultiLineFunctions); 8920 } 8921 8922 TEST_F(FormatTest, AttributeClass) { 8923 FormatStyle Style = getChromiumStyle(FormatStyle::LK_Cpp); 8924 verifyFormat("class S {\n" 8925 " S(S&&) = default;\n" 8926 "};", 8927 Style); 8928 verifyFormat("class [[nodiscard]] S {\n" 8929 " S(S&&) = default;\n" 8930 "};", 8931 Style); 8932 verifyFormat("class __attribute((maybeunused)) S {\n" 8933 " S(S&&) = default;\n" 8934 "};", 8935 Style); 8936 verifyFormat("struct S {\n" 8937 " S(S&&) = default;\n" 8938 "};", 8939 Style); 8940 verifyFormat("struct [[nodiscard]] S {\n" 8941 " S(S&&) = default;\n" 8942 "};", 8943 Style); 8944 } 8945 8946 TEST_F(FormatTest, AttributesAfterMacro) { 8947 FormatStyle Style = getLLVMStyle(); 8948 verifyFormat("MACRO;\n" 8949 "__attribute__((maybe_unused)) int foo() {\n" 8950 " //...\n" 8951 "}"); 8952 8953 verifyFormat("MACRO;\n" 8954 "[[nodiscard]] int foo() {\n" 8955 " //...\n" 8956 "}"); 8957 8958 EXPECT_EQ("MACRO\n\n" 8959 "__attribute__((maybe_unused)) int foo() {\n" 8960 " //...\n" 8961 "}", 8962 format("MACRO\n\n" 8963 "__attribute__((maybe_unused)) int foo() {\n" 8964 " //...\n" 8965 "}")); 8966 8967 EXPECT_EQ("MACRO\n\n" 8968 "[[nodiscard]] int foo() {\n" 8969 " //...\n" 8970 "}", 8971 format("MACRO\n\n" 8972 "[[nodiscard]] int foo() {\n" 8973 " //...\n" 8974 "}")); 8975 } 8976 8977 TEST_F(FormatTest, AttributePenaltyBreaking) { 8978 FormatStyle Style = getLLVMStyle(); 8979 verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n" 8980 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", 8981 Style); 8982 verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n" 8983 " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", 8984 Style); 8985 verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const " 8986 "shared_ptr<ALongTypeName> &C d) {\n}", 8987 Style); 8988 } 8989 8990 TEST_F(FormatTest, UnderstandsEllipsis) { 8991 FormatStyle Style = getLLVMStyle(); 8992 verifyFormat("int printf(const char *fmt, ...);"); 8993 verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); 8994 verifyFormat("template <class... Ts> void Foo(Ts *...ts) {}"); 8995 8996 verifyFormat("template <int *...PP> a;", Style); 8997 8998 Style.PointerAlignment = FormatStyle::PAS_Left; 8999 verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", Style); 9000 9001 verifyFormat("template <int*... PP> a;", Style); 9002 9003 Style.PointerAlignment = FormatStyle::PAS_Middle; 9004 verifyFormat("template <int *... PP> a;", Style); 9005 } 9006 9007 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { 9008 EXPECT_EQ("int *a;\n" 9009 "int *a;\n" 9010 "int *a;", 9011 format("int *a;\n" 9012 "int* a;\n" 9013 "int *a;", 9014 getGoogleStyle())); 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("auto x = [] {\n" 9030 " int *a;\n" 9031 " int *a;\n" 9032 " int *a;\n" 9033 "};", 9034 format("auto x=[]{int *a;\n" 9035 "int * a;\n" 9036 "int * a;};", 9037 getGoogleStyle())); 9038 } 9039 9040 TEST_F(FormatTest, UnderstandsRvalueReferences) { 9041 verifyFormat("int f(int &&a) {}"); 9042 verifyFormat("int f(int a, char &&b) {}"); 9043 verifyFormat("void f() { int &&a = b; }"); 9044 verifyGoogleFormat("int f(int a, char&& b) {}"); 9045 verifyGoogleFormat("void f() { int&& a = b; }"); 9046 9047 verifyIndependentOfContext("A<int &&> a;"); 9048 verifyIndependentOfContext("A<int &&, int &&> a;"); 9049 verifyGoogleFormat("A<int&&> a;"); 9050 verifyGoogleFormat("A<int&&, int&&> a;"); 9051 9052 // Not rvalue references: 9053 verifyFormat("template <bool B, bool C> class A {\n" 9054 " static_assert(B && C, \"Something is wrong\");\n" 9055 "};"); 9056 verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))"); 9057 verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))"); 9058 verifyFormat("#define A(a, b) (a && b)"); 9059 } 9060 9061 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { 9062 verifyFormat("void f() {\n" 9063 " x[aaaaaaaaa -\n" 9064 " b] = 23;\n" 9065 "}", 9066 getLLVMStyleWithColumns(15)); 9067 } 9068 9069 TEST_F(FormatTest, FormatsCasts) { 9070 verifyFormat("Type *A = static_cast<Type *>(P);"); 9071 verifyFormat("Type *A = (Type *)P;"); 9072 verifyFormat("Type *A = (vector<Type *, int *>)P;"); 9073 verifyFormat("int a = (int)(2.0f);"); 9074 verifyFormat("int a = (int)2.0f;"); 9075 verifyFormat("x[(int32)y];"); 9076 verifyFormat("x = (int32)y;"); 9077 verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)"); 9078 verifyFormat("int a = (int)*b;"); 9079 verifyFormat("int a = (int)2.0f;"); 9080 verifyFormat("int a = (int)~0;"); 9081 verifyFormat("int a = (int)++a;"); 9082 verifyFormat("int a = (int)sizeof(int);"); 9083 verifyFormat("int a = (int)+2;"); 9084 verifyFormat("my_int a = (my_int)2.0f;"); 9085 verifyFormat("my_int a = (my_int)sizeof(int);"); 9086 verifyFormat("return (my_int)aaa;"); 9087 verifyFormat("#define x ((int)-1)"); 9088 verifyFormat("#define LENGTH(x, y) (x) - (y) + 1"); 9089 verifyFormat("#define p(q) ((int *)&q)"); 9090 verifyFormat("fn(a)(b) + 1;"); 9091 9092 verifyFormat("void f() { my_int a = (my_int)*b; }"); 9093 verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }"); 9094 verifyFormat("my_int a = (my_int)~0;"); 9095 verifyFormat("my_int a = (my_int)++a;"); 9096 verifyFormat("my_int a = (my_int)-2;"); 9097 verifyFormat("my_int a = (my_int)1;"); 9098 verifyFormat("my_int a = (my_int *)1;"); 9099 verifyFormat("my_int a = (const my_int)-1;"); 9100 verifyFormat("my_int a = (const my_int *)-1;"); 9101 verifyFormat("my_int a = (my_int)(my_int)-1;"); 9102 verifyFormat("my_int a = (ns::my_int)-2;"); 9103 verifyFormat("case (my_int)ONE:"); 9104 verifyFormat("auto x = (X)this;"); 9105 // Casts in Obj-C style calls used to not be recognized as such. 9106 verifyFormat("int a = [(type*)[((type*)val) arg] arg];", getGoogleStyle()); 9107 9108 // FIXME: single value wrapped with paren will be treated as cast. 9109 verifyFormat("void f(int i = (kValue)*kMask) {}"); 9110 9111 verifyFormat("{ (void)F; }"); 9112 9113 // Don't break after a cast's 9114 verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" 9115 " (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n" 9116 " bbbbbbbbbbbbbbbbbbbbbb);"); 9117 9118 // These are not casts. 9119 verifyFormat("void f(int *) {}"); 9120 verifyFormat("f(foo)->b;"); 9121 verifyFormat("f(foo).b;"); 9122 verifyFormat("f(foo)(b);"); 9123 verifyFormat("f(foo)[b];"); 9124 verifyFormat("[](foo) { return 4; }(bar);"); 9125 verifyFormat("(*funptr)(foo)[4];"); 9126 verifyFormat("funptrs[4](foo)[4];"); 9127 verifyFormat("void f(int *);"); 9128 verifyFormat("void f(int *) = 0;"); 9129 verifyFormat("void f(SmallVector<int>) {}"); 9130 verifyFormat("void f(SmallVector<int>);"); 9131 verifyFormat("void f(SmallVector<int>) = 0;"); 9132 verifyFormat("void f(int i = (kA * kB) & kMask) {}"); 9133 verifyFormat("int a = sizeof(int) * b;"); 9134 verifyFormat("int a = alignof(int) * b;", getGoogleStyle()); 9135 verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;"); 9136 verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");"); 9137 verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;"); 9138 9139 // These are not casts, but at some point were confused with casts. 9140 verifyFormat("virtual void foo(int *) override;"); 9141 verifyFormat("virtual void foo(char &) const;"); 9142 verifyFormat("virtual void foo(int *a, char *) const;"); 9143 verifyFormat("int a = sizeof(int *) + b;"); 9144 verifyFormat("int a = alignof(int *) + b;", getGoogleStyle()); 9145 verifyFormat("bool b = f(g<int>) && c;"); 9146 verifyFormat("typedef void (*f)(int i) func;"); 9147 verifyFormat("void operator++(int) noexcept;"); 9148 verifyFormat("void operator++(int &) noexcept;"); 9149 verifyFormat("void operator delete(void *, std::size_t, const std::nothrow_t " 9150 "&) noexcept;"); 9151 verifyFormat( 9152 "void operator delete(std::size_t, const std::nothrow_t &) noexcept;"); 9153 verifyFormat("void operator delete(const std::nothrow_t &) noexcept;"); 9154 verifyFormat("void operator delete(std::nothrow_t &) noexcept;"); 9155 verifyFormat("void operator delete(nothrow_t &) noexcept;"); 9156 verifyFormat("void operator delete(foo &) noexcept;"); 9157 verifyFormat("void operator delete(foo) noexcept;"); 9158 verifyFormat("void operator delete(int) noexcept;"); 9159 verifyFormat("void operator delete(int &) noexcept;"); 9160 verifyFormat("void operator delete(int &) volatile noexcept;"); 9161 verifyFormat("void operator delete(int &) const"); 9162 verifyFormat("void operator delete(int &) = default"); 9163 verifyFormat("void operator delete(int &) = delete"); 9164 verifyFormat("void operator delete(int &) [[noreturn]]"); 9165 verifyFormat("void operator delete(int &) throw();"); 9166 verifyFormat("void operator delete(int &) throw(int);"); 9167 verifyFormat("auto operator delete(int &) -> int;"); 9168 verifyFormat("auto operator delete(int &) override"); 9169 verifyFormat("auto operator delete(int &) final"); 9170 9171 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n" 9172 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); 9173 // FIXME: The indentation here is not ideal. 9174 verifyFormat( 9175 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9176 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n" 9177 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];"); 9178 } 9179 9180 TEST_F(FormatTest, FormatsFunctionTypes) { 9181 verifyFormat("A<bool()> a;"); 9182 verifyFormat("A<SomeType()> a;"); 9183 verifyFormat("A<void (*)(int, std::string)> a;"); 9184 verifyFormat("A<void *(int)>;"); 9185 verifyFormat("void *(*a)(int *, SomeType *);"); 9186 verifyFormat("int (*func)(void *);"); 9187 verifyFormat("void f() { int (*func)(void *); }"); 9188 verifyFormat("template <class CallbackClass>\n" 9189 "using MyCallback = void (CallbackClass::*)(SomeObject *Data);"); 9190 9191 verifyGoogleFormat("A<void*(int*, SomeType*)>;"); 9192 verifyGoogleFormat("void* (*a)(int);"); 9193 verifyGoogleFormat( 9194 "template <class CallbackClass>\n" 9195 "using MyCallback = void (CallbackClass::*)(SomeObject* Data);"); 9196 9197 // Other constructs can look somewhat like function types: 9198 verifyFormat("A<sizeof(*x)> a;"); 9199 verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); 9200 verifyFormat("some_var = function(*some_pointer_var)[0];"); 9201 verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }"); 9202 verifyFormat("int x = f(&h)();"); 9203 verifyFormat("returnsFunction(¶m1, ¶m2)(param);"); 9204 verifyFormat("std::function<\n" 9205 " LooooooooooongTemplatedType<\n" 9206 " SomeType>*(\n" 9207 " LooooooooooooooooongType type)>\n" 9208 " function;", 9209 getGoogleStyleWithColumns(40)); 9210 } 9211 9212 TEST_F(FormatTest, FormatsPointersToArrayTypes) { 9213 verifyFormat("A (*foo_)[6];"); 9214 verifyFormat("vector<int> (*foo_)[6];"); 9215 } 9216 9217 TEST_F(FormatTest, BreaksLongVariableDeclarations) { 9218 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 9219 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 9220 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" 9221 " LoooooooooooooooooooooooooooooooooooooooongVariable;"); 9222 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 9223 " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); 9224 9225 // Different ways of ()-initializiation. 9226 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 9227 " LoooooooooooooooooooooooooooooooooooooooongVariable(1);"); 9228 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 9229 " LoooooooooooooooooooooooooooooooooooooooongVariable(a);"); 9230 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 9231 " LoooooooooooooooooooooooooooooooooooooooongVariable({});"); 9232 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" 9233 " LoooooooooooooooooooooooooooooooooooooongVariable([A a]);"); 9234 9235 // Lambdas should not confuse the variable declaration heuristic. 9236 verifyFormat("LooooooooooooooooongType\n" 9237 " variable(nullptr, [](A *a) {});", 9238 getLLVMStyleWithColumns(40)); 9239 } 9240 9241 TEST_F(FormatTest, BreaksLongDeclarations) { 9242 verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" 9243 " AnotherNameForTheLongType;"); 9244 verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n" 9245 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); 9246 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 9247 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 9248 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" 9249 "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); 9250 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 9251 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 9252 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" 9253 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 9254 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 9255 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 9256 verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 9257 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 9258 verifyFormat("typeof(LoooooooooooooooooooooooooooooooooooooooooongName)\n" 9259 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 9260 verifyFormat("_Atomic(LooooooooooooooooooooooooooooooooooooooooongName)\n" 9261 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 9262 verifyFormat("__underlying_type(LooooooooooooooooooooooooooooooongName)\n" 9263 "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); 9264 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 9265 "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);"); 9266 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 9267 "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}"); 9268 FormatStyle Indented = getLLVMStyle(); 9269 Indented.IndentWrappedFunctionNames = true; 9270 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 9271 " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", 9272 Indented); 9273 verifyFormat( 9274 "LoooooooooooooooooooooooooooooooooooooooongReturnType\n" 9275 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 9276 Indented); 9277 verifyFormat( 9278 "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n" 9279 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 9280 Indented); 9281 verifyFormat( 9282 "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n" 9283 " LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}", 9284 Indented); 9285 9286 // FIXME: Without the comment, this breaks after "(". 9287 verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" 9288 " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", 9289 getGoogleStyle()); 9290 9291 verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" 9292 " int LoooooooooooooooooooongParam2) {}"); 9293 verifyFormat( 9294 "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" 9295 " SourceLocation L, IdentifierIn *II,\n" 9296 " Type *T) {}"); 9297 verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" 9298 "ReallyReaaallyLongFunctionName(\n" 9299 " const std::string &SomeParameter,\n" 9300 " const SomeType<string, SomeOtherTemplateParameter>\n" 9301 " &ReallyReallyLongParameterName,\n" 9302 " const SomeType<string, SomeOtherTemplateParameter>\n" 9303 " &AnotherLongParameterName) {}"); 9304 verifyFormat("template <typename A>\n" 9305 "SomeLoooooooooooooooooooooongType<\n" 9306 " typename some_namespace::SomeOtherType<A>::Type>\n" 9307 "Function() {}"); 9308 9309 verifyGoogleFormat( 9310 "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" 9311 " aaaaaaaaaaaaaaaaaaaaaaa;"); 9312 verifyGoogleFormat( 9313 "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" 9314 " SourceLocation L) {}"); 9315 verifyGoogleFormat( 9316 "some_namespace::LongReturnType\n" 9317 "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" 9318 " int first_long_parameter, int second_parameter) {}"); 9319 9320 verifyGoogleFormat("template <typename T>\n" 9321 "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" 9322 "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); 9323 verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 9324 " int aaaaaaaaaaaaaaaaaaaaaaa);"); 9325 9326 verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" 9327 " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 9328 " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9329 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9330 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n" 9331 " aaaaaaaaaaaaaaaaaaaaaaaa);"); 9332 verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 9333 " vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" 9334 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n" 9335 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 9336 9337 verifyFormat("template <typename T> // Templates on own line.\n" 9338 "static int // Some comment.\n" 9339 "MyFunction(int a);", 9340 getLLVMStyle()); 9341 } 9342 9343 TEST_F(FormatTest, FormatsAccessModifiers) { 9344 FormatStyle Style = getLLVMStyle(); 9345 EXPECT_EQ(Style.EmptyLineBeforeAccessModifier, 9346 FormatStyle::ELBAMS_LogicalBlock); 9347 verifyFormat("struct foo {\n" 9348 "private:\n" 9349 " void f() {}\n" 9350 "\n" 9351 "private:\n" 9352 " int i;\n" 9353 "\n" 9354 "protected:\n" 9355 " int j;\n" 9356 "};\n", 9357 Style); 9358 verifyFormat("struct foo {\n" 9359 "private:\n" 9360 " void f() {}\n" 9361 "\n" 9362 "private:\n" 9363 " int i;\n" 9364 "\n" 9365 "protected:\n" 9366 " int j;\n" 9367 "};\n", 9368 "struct foo {\n" 9369 "private:\n" 9370 " void f() {}\n" 9371 "private:\n" 9372 " int i;\n" 9373 "protected:\n" 9374 " int j;\n" 9375 "};\n", 9376 Style); 9377 verifyFormat("struct foo { /* comment */\n" 9378 "private:\n" 9379 " int i;\n" 9380 " // comment\n" 9381 "private:\n" 9382 " int j;\n" 9383 "};\n", 9384 Style); 9385 verifyFormat("struct foo {\n" 9386 "#ifdef FOO\n" 9387 "#endif\n" 9388 "private:\n" 9389 " int i;\n" 9390 "#ifdef FOO\n" 9391 "private:\n" 9392 "#endif\n" 9393 " int j;\n" 9394 "};\n", 9395 Style); 9396 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 9397 verifyFormat("struct foo {\n" 9398 "private:\n" 9399 " void f() {}\n" 9400 "private:\n" 9401 " int i;\n" 9402 "protected:\n" 9403 " int j;\n" 9404 "};\n", 9405 Style); 9406 verifyFormat("struct foo {\n" 9407 "private:\n" 9408 " void f() {}\n" 9409 "private:\n" 9410 " int i;\n" 9411 "protected:\n" 9412 " int j;\n" 9413 "};\n", 9414 "struct foo {\n" 9415 "\n" 9416 "private:\n" 9417 " void f() {}\n" 9418 "\n" 9419 "private:\n" 9420 " int i;\n" 9421 "\n" 9422 "protected:\n" 9423 " int j;\n" 9424 "};\n", 9425 Style); 9426 verifyFormat("struct foo { /* comment */\n" 9427 "private:\n" 9428 " int i;\n" 9429 " // comment\n" 9430 "private:\n" 9431 " int j;\n" 9432 "};\n", 9433 "struct foo { /* comment */\n" 9434 "\n" 9435 "private:\n" 9436 " int i;\n" 9437 " // comment\n" 9438 "\n" 9439 "private:\n" 9440 " int j;\n" 9441 "};\n", 9442 Style); 9443 verifyFormat("struct foo {\n" 9444 "#ifdef FOO\n" 9445 "#endif\n" 9446 "private:\n" 9447 " int i;\n" 9448 "#ifdef FOO\n" 9449 "private:\n" 9450 "#endif\n" 9451 " int j;\n" 9452 "};\n", 9453 "struct foo {\n" 9454 "#ifdef FOO\n" 9455 "#endif\n" 9456 "\n" 9457 "private:\n" 9458 " int i;\n" 9459 "#ifdef FOO\n" 9460 "\n" 9461 "private:\n" 9462 "#endif\n" 9463 " int j;\n" 9464 "};\n", 9465 Style); 9466 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 9467 verifyFormat("struct foo {\n" 9468 "private:\n" 9469 " void f() {}\n" 9470 "\n" 9471 "private:\n" 9472 " int i;\n" 9473 "\n" 9474 "protected:\n" 9475 " int j;\n" 9476 "};\n", 9477 Style); 9478 verifyFormat("struct foo {\n" 9479 "private:\n" 9480 " void f() {}\n" 9481 "\n" 9482 "private:\n" 9483 " int i;\n" 9484 "\n" 9485 "protected:\n" 9486 " int j;\n" 9487 "};\n", 9488 "struct foo {\n" 9489 "private:\n" 9490 " void f() {}\n" 9491 "private:\n" 9492 " int i;\n" 9493 "protected:\n" 9494 " int j;\n" 9495 "};\n", 9496 Style); 9497 verifyFormat("struct foo { /* comment */\n" 9498 "private:\n" 9499 " int i;\n" 9500 " // comment\n" 9501 "\n" 9502 "private:\n" 9503 " int j;\n" 9504 "};\n", 9505 "struct foo { /* comment */\n" 9506 "private:\n" 9507 " int i;\n" 9508 " // comment\n" 9509 "\n" 9510 "private:\n" 9511 " int j;\n" 9512 "};\n", 9513 Style); 9514 verifyFormat("struct foo {\n" 9515 "#ifdef FOO\n" 9516 "#endif\n" 9517 "\n" 9518 "private:\n" 9519 " int i;\n" 9520 "#ifdef FOO\n" 9521 "\n" 9522 "private:\n" 9523 "#endif\n" 9524 " int j;\n" 9525 "};\n", 9526 "struct foo {\n" 9527 "#ifdef FOO\n" 9528 "#endif\n" 9529 "private:\n" 9530 " int i;\n" 9531 "#ifdef FOO\n" 9532 "private:\n" 9533 "#endif\n" 9534 " int j;\n" 9535 "};\n", 9536 Style); 9537 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 9538 EXPECT_EQ("struct foo {\n" 9539 "\n" 9540 "private:\n" 9541 " void f() {}\n" 9542 "\n" 9543 "private:\n" 9544 " int i;\n" 9545 "\n" 9546 "protected:\n" 9547 " int j;\n" 9548 "};\n", 9549 format("struct foo {\n" 9550 "\n" 9551 "private:\n" 9552 " void f() {}\n" 9553 "\n" 9554 "private:\n" 9555 " int i;\n" 9556 "\n" 9557 "protected:\n" 9558 " int j;\n" 9559 "};\n", 9560 Style)); 9561 verifyFormat("struct foo {\n" 9562 "private:\n" 9563 " void f() {}\n" 9564 "private:\n" 9565 " int i;\n" 9566 "protected:\n" 9567 " int j;\n" 9568 "};\n", 9569 Style); 9570 EXPECT_EQ("struct foo { /* comment */\n" 9571 "\n" 9572 "private:\n" 9573 " int i;\n" 9574 " // comment\n" 9575 "\n" 9576 "private:\n" 9577 " int j;\n" 9578 "};\n", 9579 format("struct foo { /* comment */\n" 9580 "\n" 9581 "private:\n" 9582 " int i;\n" 9583 " // comment\n" 9584 "\n" 9585 "private:\n" 9586 " int j;\n" 9587 "};\n", 9588 Style)); 9589 verifyFormat("struct foo { /* comment */\n" 9590 "private:\n" 9591 " int i;\n" 9592 " // comment\n" 9593 "private:\n" 9594 " int j;\n" 9595 "};\n", 9596 Style); 9597 EXPECT_EQ("struct foo {\n" 9598 "#ifdef FOO\n" 9599 "#endif\n" 9600 "\n" 9601 "private:\n" 9602 " int i;\n" 9603 "#ifdef FOO\n" 9604 "\n" 9605 "private:\n" 9606 "#endif\n" 9607 " int j;\n" 9608 "};\n", 9609 format("struct foo {\n" 9610 "#ifdef FOO\n" 9611 "#endif\n" 9612 "\n" 9613 "private:\n" 9614 " int i;\n" 9615 "#ifdef FOO\n" 9616 "\n" 9617 "private:\n" 9618 "#endif\n" 9619 " int j;\n" 9620 "};\n", 9621 Style)); 9622 verifyFormat("struct foo {\n" 9623 "#ifdef FOO\n" 9624 "#endif\n" 9625 "private:\n" 9626 " int i;\n" 9627 "#ifdef FOO\n" 9628 "private:\n" 9629 "#endif\n" 9630 " int j;\n" 9631 "};\n", 9632 Style); 9633 9634 FormatStyle NoEmptyLines = getLLVMStyle(); 9635 NoEmptyLines.MaxEmptyLinesToKeep = 0; 9636 verifyFormat("struct foo {\n" 9637 "private:\n" 9638 " void f() {}\n" 9639 "\n" 9640 "private:\n" 9641 " int i;\n" 9642 "\n" 9643 "public:\n" 9644 "protected:\n" 9645 " int j;\n" 9646 "};\n", 9647 NoEmptyLines); 9648 9649 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 9650 verifyFormat("struct foo {\n" 9651 "private:\n" 9652 " void f() {}\n" 9653 "private:\n" 9654 " int i;\n" 9655 "public:\n" 9656 "protected:\n" 9657 " int j;\n" 9658 "};\n", 9659 NoEmptyLines); 9660 9661 NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 9662 verifyFormat("struct foo {\n" 9663 "private:\n" 9664 " void f() {}\n" 9665 "\n" 9666 "private:\n" 9667 " int i;\n" 9668 "\n" 9669 "public:\n" 9670 "\n" 9671 "protected:\n" 9672 " int j;\n" 9673 "};\n", 9674 NoEmptyLines); 9675 } 9676 9677 TEST_F(FormatTest, FormatsAfterAccessModifiers) { 9678 9679 FormatStyle Style = getLLVMStyle(); 9680 EXPECT_EQ(Style.EmptyLineAfterAccessModifier, FormatStyle::ELAAMS_Never); 9681 verifyFormat("struct foo {\n" 9682 "private:\n" 9683 " void f() {}\n" 9684 "\n" 9685 "private:\n" 9686 " int i;\n" 9687 "\n" 9688 "protected:\n" 9689 " int j;\n" 9690 "};\n", 9691 Style); 9692 9693 // Check if lines are removed. 9694 verifyFormat("struct foo {\n" 9695 "private:\n" 9696 " void f() {}\n" 9697 "\n" 9698 "private:\n" 9699 " int i;\n" 9700 "\n" 9701 "protected:\n" 9702 " int j;\n" 9703 "};\n", 9704 "struct foo {\n" 9705 "private:\n" 9706 "\n" 9707 " void f() {}\n" 9708 "\n" 9709 "private:\n" 9710 "\n" 9711 " int i;\n" 9712 "\n" 9713 "protected:\n" 9714 "\n" 9715 " int j;\n" 9716 "};\n", 9717 Style); 9718 9719 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 9720 verifyFormat("struct foo {\n" 9721 "private:\n" 9722 "\n" 9723 " void f() {}\n" 9724 "\n" 9725 "private:\n" 9726 "\n" 9727 " int i;\n" 9728 "\n" 9729 "protected:\n" 9730 "\n" 9731 " int j;\n" 9732 "};\n", 9733 Style); 9734 9735 // Check if lines are added. 9736 verifyFormat("struct foo {\n" 9737 "private:\n" 9738 "\n" 9739 " void f() {}\n" 9740 "\n" 9741 "private:\n" 9742 "\n" 9743 " int i;\n" 9744 "\n" 9745 "protected:\n" 9746 "\n" 9747 " int j;\n" 9748 "};\n", 9749 "struct foo {\n" 9750 "private:\n" 9751 " void f() {}\n" 9752 "\n" 9753 "private:\n" 9754 " int i;\n" 9755 "\n" 9756 "protected:\n" 9757 " int j;\n" 9758 "};\n", 9759 Style); 9760 9761 // Leave tests rely on the code layout, test::messUp can not be used. 9762 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 9763 Style.MaxEmptyLinesToKeep = 0u; 9764 verifyFormat("struct foo {\n" 9765 "private:\n" 9766 " void f() {}\n" 9767 "\n" 9768 "private:\n" 9769 " int i;\n" 9770 "\n" 9771 "protected:\n" 9772 " int j;\n" 9773 "};\n", 9774 Style); 9775 9776 // Check if MaxEmptyLinesToKeep is respected. 9777 EXPECT_EQ("struct foo {\n" 9778 "private:\n" 9779 " void f() {}\n" 9780 "\n" 9781 "private:\n" 9782 " int i;\n" 9783 "\n" 9784 "protected:\n" 9785 " int j;\n" 9786 "};\n", 9787 format("struct foo {\n" 9788 "private:\n" 9789 "\n\n\n" 9790 " void f() {}\n" 9791 "\n" 9792 "private:\n" 9793 "\n\n\n" 9794 " int i;\n" 9795 "\n" 9796 "protected:\n" 9797 "\n\n\n" 9798 " int j;\n" 9799 "};\n", 9800 Style)); 9801 9802 Style.MaxEmptyLinesToKeep = 1u; 9803 EXPECT_EQ("struct foo {\n" 9804 "private:\n" 9805 "\n" 9806 " void f() {}\n" 9807 "\n" 9808 "private:\n" 9809 "\n" 9810 " int i;\n" 9811 "\n" 9812 "protected:\n" 9813 "\n" 9814 " int j;\n" 9815 "};\n", 9816 format("struct foo {\n" 9817 "private:\n" 9818 "\n" 9819 " void f() {}\n" 9820 "\n" 9821 "private:\n" 9822 "\n" 9823 " int i;\n" 9824 "\n" 9825 "protected:\n" 9826 "\n" 9827 " int j;\n" 9828 "};\n", 9829 Style)); 9830 // Check if no lines are kept. 9831 EXPECT_EQ("struct foo {\n" 9832 "private:\n" 9833 " void f() {}\n" 9834 "\n" 9835 "private:\n" 9836 " int i;\n" 9837 "\n" 9838 "protected:\n" 9839 " int j;\n" 9840 "};\n", 9841 format("struct foo {\n" 9842 "private:\n" 9843 " void f() {}\n" 9844 "\n" 9845 "private:\n" 9846 " int i;\n" 9847 "\n" 9848 "protected:\n" 9849 " int j;\n" 9850 "};\n", 9851 Style)); 9852 // Check if MaxEmptyLinesToKeep is respected. 9853 EXPECT_EQ("struct foo {\n" 9854 "private:\n" 9855 "\n" 9856 " void f() {}\n" 9857 "\n" 9858 "private:\n" 9859 "\n" 9860 " int i;\n" 9861 "\n" 9862 "protected:\n" 9863 "\n" 9864 " int j;\n" 9865 "};\n", 9866 format("struct foo {\n" 9867 "private:\n" 9868 "\n\n\n" 9869 " void f() {}\n" 9870 "\n" 9871 "private:\n" 9872 "\n\n\n" 9873 " int i;\n" 9874 "\n" 9875 "protected:\n" 9876 "\n\n\n" 9877 " int j;\n" 9878 "};\n", 9879 Style)); 9880 9881 Style.MaxEmptyLinesToKeep = 10u; 9882 EXPECT_EQ("struct foo {\n" 9883 "private:\n" 9884 "\n\n\n" 9885 " void f() {}\n" 9886 "\n" 9887 "private:\n" 9888 "\n\n\n" 9889 " int i;\n" 9890 "\n" 9891 "protected:\n" 9892 "\n\n\n" 9893 " int j;\n" 9894 "};\n", 9895 format("struct foo {\n" 9896 "private:\n" 9897 "\n\n\n" 9898 " void f() {}\n" 9899 "\n" 9900 "private:\n" 9901 "\n\n\n" 9902 " int i;\n" 9903 "\n" 9904 "protected:\n" 9905 "\n\n\n" 9906 " int j;\n" 9907 "};\n", 9908 Style)); 9909 9910 // Test with comments. 9911 Style = getLLVMStyle(); 9912 verifyFormat("struct foo {\n" 9913 "private:\n" 9914 " // comment\n" 9915 " void f() {}\n" 9916 "\n" 9917 "private: /* comment */\n" 9918 " int i;\n" 9919 "};\n", 9920 Style); 9921 verifyFormat("struct foo {\n" 9922 "private:\n" 9923 " // comment\n" 9924 " void f() {}\n" 9925 "\n" 9926 "private: /* comment */\n" 9927 " int i;\n" 9928 "};\n", 9929 "struct foo {\n" 9930 "private:\n" 9931 "\n" 9932 " // comment\n" 9933 " void f() {}\n" 9934 "\n" 9935 "private: /* comment */\n" 9936 "\n" 9937 " int i;\n" 9938 "};\n", 9939 Style); 9940 9941 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 9942 verifyFormat("struct foo {\n" 9943 "private:\n" 9944 "\n" 9945 " // comment\n" 9946 " void f() {}\n" 9947 "\n" 9948 "private: /* comment */\n" 9949 "\n" 9950 " int i;\n" 9951 "};\n", 9952 "struct foo {\n" 9953 "private:\n" 9954 " // comment\n" 9955 " void f() {}\n" 9956 "\n" 9957 "private: /* comment */\n" 9958 " int i;\n" 9959 "};\n", 9960 Style); 9961 verifyFormat("struct foo {\n" 9962 "private:\n" 9963 "\n" 9964 " // comment\n" 9965 " void f() {}\n" 9966 "\n" 9967 "private: /* comment */\n" 9968 "\n" 9969 " int i;\n" 9970 "};\n", 9971 Style); 9972 9973 // Test with preprocessor defines. 9974 Style = getLLVMStyle(); 9975 verifyFormat("struct foo {\n" 9976 "private:\n" 9977 "#ifdef FOO\n" 9978 "#endif\n" 9979 " void f() {}\n" 9980 "};\n", 9981 Style); 9982 verifyFormat("struct foo {\n" 9983 "private:\n" 9984 "#ifdef FOO\n" 9985 "#endif\n" 9986 " void f() {}\n" 9987 "};\n", 9988 "struct foo {\n" 9989 "private:\n" 9990 "\n" 9991 "#ifdef FOO\n" 9992 "#endif\n" 9993 " void f() {}\n" 9994 "};\n", 9995 Style); 9996 9997 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 9998 verifyFormat("struct foo {\n" 9999 "private:\n" 10000 "\n" 10001 "#ifdef FOO\n" 10002 "#endif\n" 10003 " void f() {}\n" 10004 "};\n", 10005 "struct foo {\n" 10006 "private:\n" 10007 "#ifdef FOO\n" 10008 "#endif\n" 10009 " void f() {}\n" 10010 "};\n", 10011 Style); 10012 verifyFormat("struct foo {\n" 10013 "private:\n" 10014 "\n" 10015 "#ifdef FOO\n" 10016 "#endif\n" 10017 " void f() {}\n" 10018 "};\n", 10019 Style); 10020 } 10021 10022 TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) { 10023 // Combined tests of EmptyLineAfterAccessModifier and 10024 // EmptyLineBeforeAccessModifier. 10025 FormatStyle Style = getLLVMStyle(); 10026 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10027 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10028 verifyFormat("struct foo {\n" 10029 "private:\n" 10030 "\n" 10031 "protected:\n" 10032 "};\n", 10033 Style); 10034 10035 Style.MaxEmptyLinesToKeep = 10u; 10036 // Both remove all new lines. 10037 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10038 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 10039 verifyFormat("struct foo {\n" 10040 "private:\n" 10041 "protected:\n" 10042 "};\n", 10043 "struct foo {\n" 10044 "private:\n" 10045 "\n\n\n" 10046 "protected:\n" 10047 "};\n", 10048 Style); 10049 10050 // Leave tests rely on the code layout, test::messUp can not be used. 10051 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 10052 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 10053 Style.MaxEmptyLinesToKeep = 10u; 10054 EXPECT_EQ("struct foo {\n" 10055 "private:\n" 10056 "\n\n\n" 10057 "protected:\n" 10058 "};\n", 10059 format("struct foo {\n" 10060 "private:\n" 10061 "\n\n\n" 10062 "protected:\n" 10063 "};\n", 10064 Style)); 10065 Style.MaxEmptyLinesToKeep = 3u; 10066 EXPECT_EQ("struct foo {\n" 10067 "private:\n" 10068 "\n\n\n" 10069 "protected:\n" 10070 "};\n", 10071 format("struct foo {\n" 10072 "private:\n" 10073 "\n\n\n" 10074 "protected:\n" 10075 "};\n", 10076 Style)); 10077 Style.MaxEmptyLinesToKeep = 1u; 10078 EXPECT_EQ("struct foo {\n" 10079 "private:\n" 10080 "\n\n\n" 10081 "protected:\n" 10082 "};\n", 10083 format("struct foo {\n" 10084 "private:\n" 10085 "\n\n\n" 10086 "protected:\n" 10087 "};\n", 10088 Style)); // Based on new lines in original document and not 10089 // on the setting. 10090 10091 Style.MaxEmptyLinesToKeep = 10u; 10092 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10093 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 10094 // Newlines are kept if they are greater than zero, 10095 // test::messUp removes all new lines which changes the logic 10096 EXPECT_EQ("struct foo {\n" 10097 "private:\n" 10098 "\n\n\n" 10099 "protected:\n" 10100 "};\n", 10101 format("struct foo {\n" 10102 "private:\n" 10103 "\n\n\n" 10104 "protected:\n" 10105 "};\n", 10106 Style)); 10107 10108 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 10109 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10110 // test::messUp removes all new lines which changes the logic 10111 EXPECT_EQ("struct foo {\n" 10112 "private:\n" 10113 "\n\n\n" 10114 "protected:\n" 10115 "};\n", 10116 format("struct foo {\n" 10117 "private:\n" 10118 "\n\n\n" 10119 "protected:\n" 10120 "};\n", 10121 Style)); 10122 10123 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; 10124 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 10125 EXPECT_EQ("struct foo {\n" 10126 "private:\n" 10127 "\n\n\n" 10128 "protected:\n" 10129 "};\n", 10130 format("struct foo {\n" 10131 "private:\n" 10132 "\n\n\n" 10133 "protected:\n" 10134 "};\n", 10135 Style)); // test::messUp removes all new lines which changes 10136 // the logic. 10137 10138 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10139 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 10140 verifyFormat("struct foo {\n" 10141 "private:\n" 10142 "protected:\n" 10143 "};\n", 10144 "struct foo {\n" 10145 "private:\n" 10146 "\n\n\n" 10147 "protected:\n" 10148 "};\n", 10149 Style); 10150 10151 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; 10152 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 10153 EXPECT_EQ("struct foo {\n" 10154 "private:\n" 10155 "\n\n\n" 10156 "protected:\n" 10157 "};\n", 10158 format("struct foo {\n" 10159 "private:\n" 10160 "\n\n\n" 10161 "protected:\n" 10162 "};\n", 10163 Style)); // test::messUp removes all new lines which changes 10164 // the logic. 10165 10166 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; 10167 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10168 verifyFormat("struct foo {\n" 10169 "private:\n" 10170 "protected:\n" 10171 "};\n", 10172 "struct foo {\n" 10173 "private:\n" 10174 "\n\n\n" 10175 "protected:\n" 10176 "};\n", 10177 Style); 10178 10179 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 10180 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; 10181 verifyFormat("struct foo {\n" 10182 "private:\n" 10183 "protected:\n" 10184 "};\n", 10185 "struct foo {\n" 10186 "private:\n" 10187 "\n\n\n" 10188 "protected:\n" 10189 "};\n", 10190 Style); 10191 10192 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 10193 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; 10194 verifyFormat("struct foo {\n" 10195 "private:\n" 10196 "protected:\n" 10197 "};\n", 10198 "struct foo {\n" 10199 "private:\n" 10200 "\n\n\n" 10201 "protected:\n" 10202 "};\n", 10203 Style); 10204 10205 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 10206 Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; 10207 verifyFormat("struct foo {\n" 10208 "private:\n" 10209 "protected:\n" 10210 "};\n", 10211 "struct foo {\n" 10212 "private:\n" 10213 "\n\n\n" 10214 "protected:\n" 10215 "};\n", 10216 Style); 10217 } 10218 10219 TEST_F(FormatTest, FormatsArrays) { 10220 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 10221 " [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;"); 10222 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n" 10223 " [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;"); 10224 verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n" 10225 " aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}"); 10226 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10227 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 10228 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10229 " [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;"); 10230 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 10231 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n" 10232 " [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;"); 10233 verifyFormat( 10234 "llvm::outs() << \"aaaaaaaaaaaa: \"\n" 10235 " << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n" 10236 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 10237 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n" 10238 " .aaaaaaaaaaaaaaaaaaaaaa();"); 10239 10240 verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n" 10241 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];"); 10242 verifyFormat( 10243 "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n" 10244 " .aaaaaaa[0]\n" 10245 " .aaaaaaaaaaaaaaaaaaaaaa();"); 10246 verifyFormat("a[::b::c];"); 10247 10248 verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10)); 10249 10250 FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); 10251 verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit); 10252 } 10253 10254 TEST_F(FormatTest, LineStartsWithSpecialCharacter) { 10255 verifyFormat("(a)->b();"); 10256 verifyFormat("--a;"); 10257 } 10258 10259 TEST_F(FormatTest, HandlesIncludeDirectives) { 10260 verifyFormat("#include <string>\n" 10261 "#include <a/b/c.h>\n" 10262 "#include \"a/b/string\"\n" 10263 "#include \"string.h\"\n" 10264 "#include \"string.h\"\n" 10265 "#include <a-a>\n" 10266 "#include < path with space >\n" 10267 "#include_next <test.h>" 10268 "#include \"abc.h\" // this is included for ABC\n" 10269 "#include \"some long include\" // with a comment\n" 10270 "#include \"some very long include path\"\n" 10271 "#include <some/very/long/include/path>\n", 10272 getLLVMStyleWithColumns(35)); 10273 EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); 10274 EXPECT_EQ("#include <a>", format("#include<a>")); 10275 10276 verifyFormat("#import <string>"); 10277 verifyFormat("#import <a/b/c.h>"); 10278 verifyFormat("#import \"a/b/string\""); 10279 verifyFormat("#import \"string.h\""); 10280 verifyFormat("#import \"string.h\""); 10281 verifyFormat("#if __has_include(<strstream>)\n" 10282 "#include <strstream>\n" 10283 "#endif"); 10284 10285 verifyFormat("#define MY_IMPORT <a/b>"); 10286 10287 verifyFormat("#if __has_include(<a/b>)"); 10288 verifyFormat("#if __has_include_next(<a/b>)"); 10289 verifyFormat("#define F __has_include(<a/b>)"); 10290 verifyFormat("#define F __has_include_next(<a/b>)"); 10291 10292 // Protocol buffer definition or missing "#". 10293 verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";", 10294 getLLVMStyleWithColumns(30)); 10295 10296 FormatStyle Style = getLLVMStyle(); 10297 Style.AlwaysBreakBeforeMultilineStrings = true; 10298 Style.ColumnLimit = 0; 10299 verifyFormat("#import \"abc.h\"", Style); 10300 10301 // But 'import' might also be a regular C++ namespace. 10302 verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 10303 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); 10304 } 10305 10306 //===----------------------------------------------------------------------===// 10307 // Error recovery tests. 10308 //===----------------------------------------------------------------------===// 10309 10310 TEST_F(FormatTest, IncompleteParameterLists) { 10311 FormatStyle NoBinPacking = getLLVMStyle(); 10312 NoBinPacking.BinPackParameters = false; 10313 verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" 10314 " double *min_x,\n" 10315 " double *max_x,\n" 10316 " double *min_y,\n" 10317 " double *max_y,\n" 10318 " double *min_z,\n" 10319 " double *max_z, ) {}", 10320 NoBinPacking); 10321 } 10322 10323 TEST_F(FormatTest, IncorrectCodeTrailingStuff) { 10324 verifyFormat("void f() { return; }\n42"); 10325 verifyFormat("void f() {\n" 10326 " if (0)\n" 10327 " return;\n" 10328 "}\n" 10329 "42"); 10330 verifyFormat("void f() { return }\n42"); 10331 verifyFormat("void f() {\n" 10332 " if (0)\n" 10333 " return\n" 10334 "}\n" 10335 "42"); 10336 } 10337 10338 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { 10339 EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); 10340 EXPECT_EQ("void f() {\n" 10341 " if (a)\n" 10342 " return\n" 10343 "}", 10344 format("void f ( ) { if ( a ) return }")); 10345 EXPECT_EQ("namespace N {\n" 10346 "void f()\n" 10347 "}", 10348 format("namespace N { void f() }")); 10349 EXPECT_EQ("namespace N {\n" 10350 "void f() {}\n" 10351 "void g()\n" 10352 "} // namespace N", 10353 format("namespace N { void f( ) { } void g( ) }")); 10354 } 10355 10356 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { 10357 verifyFormat("int aaaaaaaa =\n" 10358 " // Overlylongcomment\n" 10359 " b;", 10360 getLLVMStyleWithColumns(20)); 10361 verifyFormat("function(\n" 10362 " ShortArgument,\n" 10363 " LoooooooooooongArgument);\n", 10364 getLLVMStyleWithColumns(20)); 10365 } 10366 10367 TEST_F(FormatTest, IncorrectAccessSpecifier) { 10368 verifyFormat("public:"); 10369 verifyFormat("class A {\n" 10370 "public\n" 10371 " void f() {}\n" 10372 "};"); 10373 verifyFormat("public\n" 10374 "int qwerty;"); 10375 verifyFormat("public\n" 10376 "B {}"); 10377 verifyFormat("public\n" 10378 "{}"); 10379 verifyFormat("public\n" 10380 "B { int x; }"); 10381 } 10382 10383 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { 10384 verifyFormat("{"); 10385 verifyFormat("#})"); 10386 verifyNoCrash("(/**/[:!] ?[)."); 10387 } 10388 10389 TEST_F(FormatTest, IncorrectUnbalancedBracesInMacrosWithUnicode) { 10390 // Found by oss-fuzz: 10391 // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=8212 10392 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); 10393 Style.ColumnLimit = 60; 10394 verifyNoCrash( 10395 "\x23\x47\xff\x20\x28\xff\x3c\xff\x3f\xff\x20\x2f\x7b\x7a\xff\x20" 10396 "\xff\xff\xff\xca\xb5\xff\xff\xff\xff\x3a\x7b\x7d\xff\x20\xff\x20" 10397 "\xff\x74\xff\x20\x7d\x7d\xff\x7b\x3a\xff\x20\x71\xff\x20\xff\x0a", 10398 Style); 10399 } 10400 10401 TEST_F(FormatTest, IncorrectCodeDoNoWhile) { 10402 verifyFormat("do {\n}"); 10403 verifyFormat("do {\n}\n" 10404 "f();"); 10405 verifyFormat("do {\n}\n" 10406 "wheeee(fun);"); 10407 verifyFormat("do {\n" 10408 " f();\n" 10409 "}"); 10410 } 10411 10412 TEST_F(FormatTest, IncorrectCodeMissingParens) { 10413 verifyFormat("if {\n foo;\n foo();\n}"); 10414 verifyFormat("switch {\n foo;\n foo();\n}"); 10415 verifyIncompleteFormat("for {\n foo;\n foo();\n}"); 10416 verifyFormat("while {\n foo;\n foo();\n}"); 10417 verifyFormat("do {\n foo;\n foo();\n} while;"); 10418 } 10419 10420 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { 10421 verifyIncompleteFormat("namespace {\n" 10422 "class Foo { Foo (\n" 10423 "};\n" 10424 "} // namespace"); 10425 } 10426 10427 TEST_F(FormatTest, IncorrectCodeErrorDetection) { 10428 EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); 10429 EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); 10430 EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); 10431 EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); 10432 10433 EXPECT_EQ("{\n" 10434 " {\n" 10435 " breakme(\n" 10436 " qwe);\n" 10437 " }\n", 10438 format("{\n" 10439 " {\n" 10440 " breakme(qwe);\n" 10441 "}\n", 10442 getLLVMStyleWithColumns(10))); 10443 } 10444 10445 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { 10446 verifyFormat("int x = {\n" 10447 " avariable,\n" 10448 " b(alongervariable)};", 10449 getLLVMStyleWithColumns(25)); 10450 } 10451 10452 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { 10453 verifyFormat("return (a)(b){1, 2, 3};"); 10454 } 10455 10456 TEST_F(FormatTest, LayoutCxx11BraceInitializers) { 10457 verifyFormat("vector<int> x{1, 2, 3, 4};"); 10458 verifyFormat("vector<int> x{\n" 10459 " 1,\n" 10460 " 2,\n" 10461 " 3,\n" 10462 " 4,\n" 10463 "};"); 10464 verifyFormat("vector<T> x{{}, {}, {}, {}};"); 10465 verifyFormat("f({1, 2});"); 10466 verifyFormat("auto v = Foo{-1};"); 10467 verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});"); 10468 verifyFormat("Class::Class : member{1, 2, 3} {}"); 10469 verifyFormat("new vector<int>{1, 2, 3};"); 10470 verifyFormat("new int[3]{1, 2, 3};"); 10471 verifyFormat("new int{1};"); 10472 verifyFormat("return {arg1, arg2};"); 10473 verifyFormat("return {arg1, SomeType{parameter}};"); 10474 verifyFormat("int count = set<int>{f(), g(), h()}.size();"); 10475 verifyFormat("new T{arg1, arg2};"); 10476 verifyFormat("f(MyMap[{composite, key}]);"); 10477 verifyFormat("class Class {\n" 10478 " T member = {arg1, arg2};\n" 10479 "};"); 10480 verifyFormat("vector<int> foo = {::SomeGlobalFunction()};"); 10481 verifyFormat("const struct A a = {.a = 1, .b = 2};"); 10482 verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); 10483 verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");"); 10484 verifyFormat("int a = std::is_integral<int>{} + 0;"); 10485 10486 verifyFormat("int foo(int i) { return fo1{}(i); }"); 10487 verifyFormat("int foo(int i) { return fo1{}(i); }"); 10488 verifyFormat("auto i = decltype(x){};"); 10489 verifyFormat("auto i = typeof(x){};"); 10490 verifyFormat("auto i = _Atomic(x){};"); 10491 verifyFormat("std::vector<int> v = {1, 0 /* comment */};"); 10492 verifyFormat("Node n{1, Node{1000}, //\n" 10493 " 2};"); 10494 verifyFormat("Aaaa aaaaaaa{\n" 10495 " {\n" 10496 " aaaa,\n" 10497 " },\n" 10498 "};"); 10499 verifyFormat("class C : public D {\n" 10500 " SomeClass SC{2};\n" 10501 "};"); 10502 verifyFormat("class C : public A {\n" 10503 " class D : public B {\n" 10504 " void f() { int i{2}; }\n" 10505 " };\n" 10506 "};"); 10507 verifyFormat("#define A {a, a},"); 10508 10509 // Avoid breaking between equal sign and opening brace 10510 FormatStyle AvoidBreakingFirstArgument = getLLVMStyle(); 10511 AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200; 10512 verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n" 10513 " {{\"aaaaaaaaaaaaaaaaaaaaa\", 0},\n" 10514 " {\"bbbbbbbbbbbbbbbbbbbbb\", 1},\n" 10515 " {\"ccccccccccccccccccccc\", 2}};", 10516 AvoidBreakingFirstArgument); 10517 10518 // Binpacking only if there is no trailing comma 10519 verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n" 10520 " cccccccccc, dddddddddd};", 10521 getLLVMStyleWithColumns(50)); 10522 verifyFormat("const Aaaaaa aaaaa = {\n" 10523 " aaaaaaaaaaa,\n" 10524 " bbbbbbbbbbb,\n" 10525 " ccccccccccc,\n" 10526 " ddddddddddd,\n" 10527 "};", 10528 getLLVMStyleWithColumns(50)); 10529 10530 // Cases where distinguising braced lists and blocks is hard. 10531 verifyFormat("vector<int> v{12} GUARDED_BY(mutex);"); 10532 verifyFormat("void f() {\n" 10533 " return; // comment\n" 10534 "}\n" 10535 "SomeType t;"); 10536 verifyFormat("void f() {\n" 10537 " if (a) {\n" 10538 " f();\n" 10539 " }\n" 10540 "}\n" 10541 "SomeType t;"); 10542 10543 // In combination with BinPackArguments = false. 10544 FormatStyle NoBinPacking = getLLVMStyle(); 10545 NoBinPacking.BinPackArguments = false; 10546 verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n" 10547 " bbbbb,\n" 10548 " ccccc,\n" 10549 " ddddd,\n" 10550 " eeeee,\n" 10551 " ffffff,\n" 10552 " ggggg,\n" 10553 " hhhhhh,\n" 10554 " iiiiii,\n" 10555 " jjjjjj,\n" 10556 " kkkkkk};", 10557 NoBinPacking); 10558 verifyFormat("const Aaaaaa aaaaa = {\n" 10559 " aaaaa,\n" 10560 " bbbbb,\n" 10561 " ccccc,\n" 10562 " ddddd,\n" 10563 " eeeee,\n" 10564 " ffffff,\n" 10565 " ggggg,\n" 10566 " hhhhhh,\n" 10567 " iiiiii,\n" 10568 " jjjjjj,\n" 10569 " kkkkkk,\n" 10570 "};", 10571 NoBinPacking); 10572 verifyFormat( 10573 "const Aaaaaa aaaaa = {\n" 10574 " aaaaa, bbbbb, ccccc, ddddd, eeeee, ffffff, ggggg, hhhhhh,\n" 10575 " iiiiii, jjjjjj, kkkkkk, aaaaa, bbbbb, ccccc, ddddd, eeeee,\n" 10576 " ffffff, ggggg, hhhhhh, iiiiii, jjjjjj, kkkkkk,\n" 10577 "};", 10578 NoBinPacking); 10579 10580 NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 10581 EXPECT_EQ("static uint8 CddDp83848Reg[] = {\n" 10582 " CDDDP83848_BMCR_REGISTER,\n" 10583 " CDDDP83848_BMSR_REGISTER,\n" 10584 " CDDDP83848_RBR_REGISTER};", 10585 format("static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n" 10586 " CDDDP83848_BMSR_REGISTER,\n" 10587 " CDDDP83848_RBR_REGISTER};", 10588 NoBinPacking)); 10589 10590 // FIXME: The alignment of these trailing comments might be bad. Then again, 10591 // this might be utterly useless in real code. 10592 verifyFormat("Constructor::Constructor()\n" 10593 " : some_value{ //\n" 10594 " aaaaaaa, //\n" 10595 " bbbbbbb} {}"); 10596 10597 // In braced lists, the first comment is always assumed to belong to the 10598 // first element. Thus, it can be moved to the next or previous line as 10599 // appropriate. 10600 EXPECT_EQ("function({// First element:\n" 10601 " 1,\n" 10602 " // Second element:\n" 10603 " 2});", 10604 format("function({\n" 10605 " // First element:\n" 10606 " 1,\n" 10607 " // Second element:\n" 10608 " 2});")); 10609 EXPECT_EQ("std::vector<int> MyNumbers{\n" 10610 " // First element:\n" 10611 " 1,\n" 10612 " // Second element:\n" 10613 " 2};", 10614 format("std::vector<int> MyNumbers{// First element:\n" 10615 " 1,\n" 10616 " // Second element:\n" 10617 " 2};", 10618 getLLVMStyleWithColumns(30))); 10619 // A trailing comma should still lead to an enforced line break and no 10620 // binpacking. 10621 EXPECT_EQ("vector<int> SomeVector = {\n" 10622 " // aaa\n" 10623 " 1,\n" 10624 " 2,\n" 10625 "};", 10626 format("vector<int> SomeVector = { // aaa\n" 10627 " 1, 2, };")); 10628 10629 // C++11 brace initializer list l-braces should not be treated any differently 10630 // when breaking before lambda bodies is enabled 10631 FormatStyle BreakBeforeLambdaBody = getLLVMStyle(); 10632 BreakBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 10633 BreakBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 10634 BreakBeforeLambdaBody.AlwaysBreakBeforeMultilineStrings = true; 10635 verifyFormat( 10636 "std::runtime_error{\n" 10637 " \"Long string which will force a break onto the next line...\"};", 10638 BreakBeforeLambdaBody); 10639 10640 FormatStyle ExtraSpaces = getLLVMStyle(); 10641 ExtraSpaces.Cpp11BracedListStyle = false; 10642 ExtraSpaces.ColumnLimit = 75; 10643 verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces); 10644 verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces); 10645 verifyFormat("f({ 1, 2 });", ExtraSpaces); 10646 verifyFormat("auto v = Foo{ 1 };", ExtraSpaces); 10647 verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces); 10648 verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces); 10649 verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces); 10650 verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces); 10651 verifyFormat("return { arg1, arg2 };", ExtraSpaces); 10652 verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces); 10653 verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces); 10654 verifyFormat("new T{ arg1, arg2 };", ExtraSpaces); 10655 verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces); 10656 verifyFormat("class Class {\n" 10657 " T member = { arg1, arg2 };\n" 10658 "};", 10659 ExtraSpaces); 10660 verifyFormat( 10661 "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 10662 " aaaaaaaaaaaaaaaaaaaa, aaaaa }\n" 10663 " : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" 10664 " bbbbbbbbbbbbbbbbbbbb, bbbbb };", 10665 ExtraSpaces); 10666 verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces); 10667 verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });", 10668 ExtraSpaces); 10669 verifyFormat( 10670 "someFunction(OtherParam,\n" 10671 " BracedList{ // comment 1 (Forcing interesting break)\n" 10672 " param1, param2,\n" 10673 " // comment 2\n" 10674 " param3, param4 });", 10675 ExtraSpaces); 10676 verifyFormat( 10677 "std::this_thread::sleep_for(\n" 10678 " std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);", 10679 ExtraSpaces); 10680 verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n" 10681 " aaaaaaa,\n" 10682 " aaaaaaaaaa,\n" 10683 " aaaaa,\n" 10684 " aaaaaaaaaaaaaaa,\n" 10685 " aaa,\n" 10686 " aaaaaaaaaa,\n" 10687 " a,\n" 10688 " aaaaaaaaaaaaaaaaaaaaa,\n" 10689 " aaaaaaaaaaaa,\n" 10690 " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n" 10691 " aaaaaaa,\n" 10692 " a};"); 10693 verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces); 10694 verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces); 10695 verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces); 10696 10697 // Avoid breaking between initializer/equal sign and opening brace 10698 ExtraSpaces.PenaltyBreakBeforeFirstCallParameter = 200; 10699 verifyFormat("const std::unordered_map<std::string, int> MyHashTable = {\n" 10700 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 10701 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 10702 " { \"ccccccccccccccccccccc\", 2 }\n" 10703 "};", 10704 ExtraSpaces); 10705 verifyFormat("const std::unordered_map<std::string, int> MyHashTable{\n" 10706 " { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },\n" 10707 " { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },\n" 10708 " { \"ccccccccccccccccccccc\", 2 }\n" 10709 "};", 10710 ExtraSpaces); 10711 10712 FormatStyle SpaceBeforeBrace = getLLVMStyle(); 10713 SpaceBeforeBrace.SpaceBeforeCpp11BracedList = true; 10714 verifyFormat("vector<int> x {1, 2, 3, 4};", SpaceBeforeBrace); 10715 verifyFormat("f({}, {{}, {}}, MyMap[{k, v}]);", SpaceBeforeBrace); 10716 10717 FormatStyle SpaceBetweenBraces = getLLVMStyle(); 10718 SpaceBetweenBraces.SpacesInAngles = FormatStyle::SIAS_Always; 10719 SpaceBetweenBraces.SpacesInParentheses = true; 10720 SpaceBetweenBraces.SpacesInSquareBrackets = true; 10721 verifyFormat("vector< int > x{ 1, 2, 3, 4 };", SpaceBetweenBraces); 10722 verifyFormat("f( {}, { {}, {} }, MyMap[ { k, v } ] );", SpaceBetweenBraces); 10723 verifyFormat("vector< int > x{ // comment 1\n" 10724 " 1, 2, 3, 4 };", 10725 SpaceBetweenBraces); 10726 SpaceBetweenBraces.ColumnLimit = 20; 10727 EXPECT_EQ("vector< int > x{\n" 10728 " 1, 2, 3, 4 };", 10729 format("vector<int>x{1,2,3,4};", SpaceBetweenBraces)); 10730 SpaceBetweenBraces.ColumnLimit = 24; 10731 EXPECT_EQ("vector< int > x{ 1, 2,\n" 10732 " 3, 4 };", 10733 format("vector<int>x{1,2,3,4};", SpaceBetweenBraces)); 10734 EXPECT_EQ("vector< int > x{\n" 10735 " 1,\n" 10736 " 2,\n" 10737 " 3,\n" 10738 " 4,\n" 10739 "};", 10740 format("vector<int>x{1,2,3,4,};", SpaceBetweenBraces)); 10741 verifyFormat("vector< int > x{};", SpaceBetweenBraces); 10742 SpaceBetweenBraces.SpaceInEmptyParentheses = true; 10743 verifyFormat("vector< int > x{ };", SpaceBetweenBraces); 10744 } 10745 10746 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { 10747 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 10748 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 10749 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 10750 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 10751 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 10752 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 10753 verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n" 10754 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 10755 " 1, 22, 333, 4444, 55555, //\n" 10756 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 10757 " 1, 22, 333, 4444, 55555, 666666, 7777777};"); 10758 verifyFormat( 10759 "vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" 10760 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 10761 " 1, 22, 333, 4444, 55555, 666666, // comment\n" 10762 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 10763 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 10764 " 7777777, 1, 22, 333, 4444, 55555, 666666,\n" 10765 " 7777777};"); 10766 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 10767 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 10768 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 10769 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 10770 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 10771 " // Separating comment.\n" 10772 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 10773 verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n" 10774 " // Leading comment\n" 10775 " X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n" 10776 " X86::R8, X86::R9, X86::R10, X86::R11, 0};"); 10777 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 10778 " 1, 1, 1, 1};", 10779 getLLVMStyleWithColumns(39)); 10780 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 10781 " 1, 1, 1, 1};", 10782 getLLVMStyleWithColumns(38)); 10783 verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n" 10784 " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};", 10785 getLLVMStyleWithColumns(43)); 10786 verifyFormat( 10787 "static unsigned SomeValues[10][3] = {\n" 10788 " {1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},\n" 10789 " {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};"); 10790 verifyFormat("static auto fields = new vector<string>{\n" 10791 " \"aaaaaaaaaaaaa\",\n" 10792 " \"aaaaaaaaaaaaa\",\n" 10793 " \"aaaaaaaaaaaa\",\n" 10794 " \"aaaaaaaaaaaaaa\",\n" 10795 " \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 10796 " \"aaaaaaaaaaaa\",\n" 10797 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n" 10798 "};"); 10799 verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};"); 10800 verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n" 10801 " 2, bbbbbbbbbbbbbbbbbbbbbb,\n" 10802 " 3, cccccccccccccccccccccc};", 10803 getLLVMStyleWithColumns(60)); 10804 10805 // Trailing commas. 10806 verifyFormat("vector<int> x = {\n" 10807 " 1, 1, 1, 1, 1, 1, 1, 1,\n" 10808 "};", 10809 getLLVMStyleWithColumns(39)); 10810 verifyFormat("vector<int> x = {\n" 10811 " 1, 1, 1, 1, 1, 1, 1, 1, //\n" 10812 "};", 10813 getLLVMStyleWithColumns(39)); 10814 verifyFormat("vector<int> x = {1, 1, 1, 1,\n" 10815 " 1, 1, 1, 1,\n" 10816 " /**/ /**/};", 10817 getLLVMStyleWithColumns(39)); 10818 10819 // Trailing comment in the first line. 10820 verifyFormat("vector<int> iiiiiiiiiiiiiii = { //\n" 10821 " 1111111111, 2222222222, 33333333333, 4444444444, //\n" 10822 " 111111111, 222222222, 3333333333, 444444444, //\n" 10823 " 11111111, 22222222, 333333333, 44444444};"); 10824 // Trailing comment in the last line. 10825 verifyFormat("int aaaaa[] = {\n" 10826 " 1, 2, 3, // comment\n" 10827 " 4, 5, 6 // comment\n" 10828 "};"); 10829 10830 // With nested lists, we should either format one item per line or all nested 10831 // lists one on line. 10832 // FIXME: For some nested lists, we can do better. 10833 verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n" 10834 " {aaaaaaaaaaaaaaaaaaa},\n" 10835 " {aaaaaaaaaaaaaaaaaaaaa},\n" 10836 " {aaaaaaaaaaaaaaaaa}};", 10837 getLLVMStyleWithColumns(60)); 10838 verifyFormat( 10839 "SomeStruct my_struct_array = {\n" 10840 " {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n" 10841 " aaaaaaaaaaaaa, aaaaaaa, aaa},\n" 10842 " {aaa, aaa},\n" 10843 " {aaa, aaa},\n" 10844 " {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n" 10845 " {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n" 10846 " aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};"); 10847 10848 // No column layout should be used here. 10849 verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n" 10850 " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};"); 10851 10852 verifyNoCrash("a<,"); 10853 10854 // No braced initializer here. 10855 verifyFormat("void f() {\n" 10856 " struct Dummy {};\n" 10857 " f(v);\n" 10858 "}"); 10859 10860 // Long lists should be formatted in columns even if they are nested. 10861 verifyFormat( 10862 "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n" 10863 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 10864 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 10865 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 10866 " 1, 22, 333, 4444, 55555, 666666, 7777777,\n" 10867 " 1, 22, 333, 4444, 55555, 666666, 7777777});"); 10868 10869 // Allow "single-column" layout even if that violates the column limit. There 10870 // isn't going to be a better way. 10871 verifyFormat("std::vector<int> a = {\n" 10872 " aaaaaaaa,\n" 10873 " aaaaaaaa,\n" 10874 " aaaaaaaa,\n" 10875 " aaaaaaaa,\n" 10876 " aaaaaaaaaa,\n" 10877 " aaaaaaaa,\n" 10878 " aaaaaaaaaaaaaaaaaaaaaaaaaaa};", 10879 getLLVMStyleWithColumns(30)); 10880 verifyFormat("vector<int> aaaa = {\n" 10881 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 10882 " aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" 10883 " aaaaaa.aaaaaaa,\n" 10884 " aaaaaa.aaaaaaa,\n" 10885 " aaaaaa.aaaaaaa,\n" 10886 " aaaaaa.aaaaaaa,\n" 10887 "};"); 10888 10889 // Don't create hanging lists. 10890 verifyFormat("someFunction(Param, {List1, List2,\n" 10891 " List3});", 10892 getLLVMStyleWithColumns(35)); 10893 verifyFormat("someFunction(Param, Param,\n" 10894 " {List1, List2,\n" 10895 " List3});", 10896 getLLVMStyleWithColumns(35)); 10897 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n" 10898 " aaaaaaaaaaaaaaaaaaaaaaa);"); 10899 } 10900 10901 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { 10902 FormatStyle DoNotMerge = getLLVMStyle(); 10903 DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 10904 10905 verifyFormat("void f() { return 42; }"); 10906 verifyFormat("void f() {\n" 10907 " return 42;\n" 10908 "}", 10909 DoNotMerge); 10910 verifyFormat("void f() {\n" 10911 " // Comment\n" 10912 "}"); 10913 verifyFormat("{\n" 10914 "#error {\n" 10915 " int a;\n" 10916 "}"); 10917 verifyFormat("{\n" 10918 " int a;\n" 10919 "#error {\n" 10920 "}"); 10921 verifyFormat("void f() {} // comment"); 10922 verifyFormat("void f() { int a; } // comment"); 10923 verifyFormat("void f() {\n" 10924 "} // comment", 10925 DoNotMerge); 10926 verifyFormat("void f() {\n" 10927 " int a;\n" 10928 "} // comment", 10929 DoNotMerge); 10930 verifyFormat("void f() {\n" 10931 "} // comment", 10932 getLLVMStyleWithColumns(15)); 10933 10934 verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); 10935 verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); 10936 10937 verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); 10938 verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); 10939 verifyFormat("class C {\n" 10940 " C()\n" 10941 " : iiiiiiii(nullptr),\n" 10942 " kkkkkkk(nullptr),\n" 10943 " mmmmmmm(nullptr),\n" 10944 " nnnnnnn(nullptr) {}\n" 10945 "};", 10946 getGoogleStyle()); 10947 10948 FormatStyle NoColumnLimit = getLLVMStyle(); 10949 NoColumnLimit.ColumnLimit = 0; 10950 EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); 10951 EXPECT_EQ("class C {\n" 10952 " A() : b(0) {}\n" 10953 "};", 10954 format("class C{A():b(0){}};", NoColumnLimit)); 10955 EXPECT_EQ("A()\n" 10956 " : b(0) {\n" 10957 "}", 10958 format("A()\n:b(0)\n{\n}", NoColumnLimit)); 10959 10960 FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; 10961 DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = 10962 FormatStyle::SFS_None; 10963 EXPECT_EQ("A()\n" 10964 " : b(0) {\n" 10965 "}", 10966 format("A():b(0){}", DoNotMergeNoColumnLimit)); 10967 EXPECT_EQ("A()\n" 10968 " : b(0) {\n" 10969 "}", 10970 format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); 10971 10972 verifyFormat("#define A \\\n" 10973 " void f() { \\\n" 10974 " int i; \\\n" 10975 " }", 10976 getLLVMStyleWithColumns(20)); 10977 verifyFormat("#define A \\\n" 10978 " void f() { int i; }", 10979 getLLVMStyleWithColumns(21)); 10980 verifyFormat("#define A \\\n" 10981 " void f() { \\\n" 10982 " int i; \\\n" 10983 " } \\\n" 10984 " int j;", 10985 getLLVMStyleWithColumns(22)); 10986 verifyFormat("#define A \\\n" 10987 " void f() { int i; } \\\n" 10988 " int j;", 10989 getLLVMStyleWithColumns(23)); 10990 } 10991 10992 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) { 10993 FormatStyle MergeEmptyOnly = getLLVMStyle(); 10994 MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 10995 verifyFormat("class C {\n" 10996 " int f() {}\n" 10997 "};", 10998 MergeEmptyOnly); 10999 verifyFormat("class C {\n" 11000 " int f() {\n" 11001 " return 42;\n" 11002 " }\n" 11003 "};", 11004 MergeEmptyOnly); 11005 verifyFormat("int f() {}", MergeEmptyOnly); 11006 verifyFormat("int f() {\n" 11007 " return 42;\n" 11008 "}", 11009 MergeEmptyOnly); 11010 11011 // Also verify behavior when BraceWrapping.AfterFunction = true 11012 MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 11013 MergeEmptyOnly.BraceWrapping.AfterFunction = true; 11014 verifyFormat("int f() {}", MergeEmptyOnly); 11015 verifyFormat("class C {\n" 11016 " int f() {}\n" 11017 "};", 11018 MergeEmptyOnly); 11019 } 11020 11021 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) { 11022 FormatStyle MergeInlineOnly = getLLVMStyle(); 11023 MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 11024 verifyFormat("class C {\n" 11025 " int f() { return 42; }\n" 11026 "};", 11027 MergeInlineOnly); 11028 verifyFormat("int f() {\n" 11029 " return 42;\n" 11030 "}", 11031 MergeInlineOnly); 11032 11033 // SFS_Inline implies SFS_Empty 11034 verifyFormat("class C {\n" 11035 " int f() {}\n" 11036 "};", 11037 MergeInlineOnly); 11038 verifyFormat("int f() {}", MergeInlineOnly); 11039 11040 // Also verify behavior when BraceWrapping.AfterFunction = true 11041 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 11042 MergeInlineOnly.BraceWrapping.AfterFunction = true; 11043 verifyFormat("class C {\n" 11044 " int f() { return 42; }\n" 11045 "};", 11046 MergeInlineOnly); 11047 verifyFormat("int f()\n" 11048 "{\n" 11049 " return 42;\n" 11050 "}", 11051 MergeInlineOnly); 11052 11053 // SFS_Inline implies SFS_Empty 11054 verifyFormat("int f() {}", MergeInlineOnly); 11055 verifyFormat("class C {\n" 11056 " int f() {}\n" 11057 "};", 11058 MergeInlineOnly); 11059 } 11060 11061 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) { 11062 FormatStyle MergeInlineOnly = getLLVMStyle(); 11063 MergeInlineOnly.AllowShortFunctionsOnASingleLine = 11064 FormatStyle::SFS_InlineOnly; 11065 verifyFormat("class C {\n" 11066 " int f() { return 42; }\n" 11067 "};", 11068 MergeInlineOnly); 11069 verifyFormat("int f() {\n" 11070 " return 42;\n" 11071 "}", 11072 MergeInlineOnly); 11073 11074 // SFS_InlineOnly does not imply SFS_Empty 11075 verifyFormat("class C {\n" 11076 " int f() {}\n" 11077 "};", 11078 MergeInlineOnly); 11079 verifyFormat("int f() {\n" 11080 "}", 11081 MergeInlineOnly); 11082 11083 // Also verify behavior when BraceWrapping.AfterFunction = true 11084 MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom; 11085 MergeInlineOnly.BraceWrapping.AfterFunction = true; 11086 verifyFormat("class C {\n" 11087 " int f() { return 42; }\n" 11088 "};", 11089 MergeInlineOnly); 11090 verifyFormat("int f()\n" 11091 "{\n" 11092 " return 42;\n" 11093 "}", 11094 MergeInlineOnly); 11095 11096 // SFS_InlineOnly does not imply SFS_Empty 11097 verifyFormat("int f()\n" 11098 "{\n" 11099 "}", 11100 MergeInlineOnly); 11101 verifyFormat("class C {\n" 11102 " int f() {}\n" 11103 "};", 11104 MergeInlineOnly); 11105 } 11106 11107 TEST_F(FormatTest, SplitEmptyFunction) { 11108 FormatStyle Style = getLLVMStyle(); 11109 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 11110 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 11111 Style.BraceWrapping.AfterFunction = true; 11112 Style.BraceWrapping.SplitEmptyFunction = false; 11113 Style.ColumnLimit = 40; 11114 11115 verifyFormat("int f()\n" 11116 "{}", 11117 Style); 11118 verifyFormat("int f()\n" 11119 "{\n" 11120 " return 42;\n" 11121 "}", 11122 Style); 11123 verifyFormat("int f()\n" 11124 "{\n" 11125 " // some comment\n" 11126 "}", 11127 Style); 11128 11129 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty; 11130 verifyFormat("int f() {}", Style); 11131 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 11132 "{}", 11133 Style); 11134 verifyFormat("int f()\n" 11135 "{\n" 11136 " return 0;\n" 11137 "}", 11138 Style); 11139 11140 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 11141 verifyFormat("class Foo {\n" 11142 " int f() {}\n" 11143 "};\n", 11144 Style); 11145 verifyFormat("class Foo {\n" 11146 " int f() { return 0; }\n" 11147 "};\n", 11148 Style); 11149 verifyFormat("class Foo {\n" 11150 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 11151 " {}\n" 11152 "};\n", 11153 Style); 11154 verifyFormat("class Foo {\n" 11155 " int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 11156 " {\n" 11157 " return 0;\n" 11158 " }\n" 11159 "};\n", 11160 Style); 11161 11162 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 11163 verifyFormat("int f() {}", Style); 11164 verifyFormat("int f() { return 0; }", Style); 11165 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 11166 "{}", 11167 Style); 11168 verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n" 11169 "{\n" 11170 " return 0;\n" 11171 "}", 11172 Style); 11173 } 11174 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) { 11175 FormatStyle Style = getLLVMStyle(); 11176 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 11177 verifyFormat("#ifdef A\n" 11178 "int f() {}\n" 11179 "#else\n" 11180 "int g() {}\n" 11181 "#endif", 11182 Style); 11183 } 11184 11185 TEST_F(FormatTest, SplitEmptyClass) { 11186 FormatStyle Style = getLLVMStyle(); 11187 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 11188 Style.BraceWrapping.AfterClass = true; 11189 Style.BraceWrapping.SplitEmptyRecord = false; 11190 11191 verifyFormat("class Foo\n" 11192 "{};", 11193 Style); 11194 verifyFormat("/* something */ class Foo\n" 11195 "{};", 11196 Style); 11197 verifyFormat("template <typename X> class Foo\n" 11198 "{};", 11199 Style); 11200 verifyFormat("class Foo\n" 11201 "{\n" 11202 " Foo();\n" 11203 "};", 11204 Style); 11205 verifyFormat("typedef class Foo\n" 11206 "{\n" 11207 "} Foo_t;", 11208 Style); 11209 11210 Style.BraceWrapping.SplitEmptyRecord = true; 11211 Style.BraceWrapping.AfterStruct = true; 11212 verifyFormat("class rep\n" 11213 "{\n" 11214 "};", 11215 Style); 11216 verifyFormat("struct rep\n" 11217 "{\n" 11218 "};", 11219 Style); 11220 verifyFormat("template <typename T> class rep\n" 11221 "{\n" 11222 "};", 11223 Style); 11224 verifyFormat("template <typename T> struct rep\n" 11225 "{\n" 11226 "};", 11227 Style); 11228 verifyFormat("class rep\n" 11229 "{\n" 11230 " int x;\n" 11231 "};", 11232 Style); 11233 verifyFormat("struct rep\n" 11234 "{\n" 11235 " int x;\n" 11236 "};", 11237 Style); 11238 verifyFormat("template <typename T> class rep\n" 11239 "{\n" 11240 " int x;\n" 11241 "};", 11242 Style); 11243 verifyFormat("template <typename T> struct rep\n" 11244 "{\n" 11245 " int x;\n" 11246 "};", 11247 Style); 11248 verifyFormat("template <typename T> class rep // Foo\n" 11249 "{\n" 11250 " int x;\n" 11251 "};", 11252 Style); 11253 verifyFormat("template <typename T> struct rep // Bar\n" 11254 "{\n" 11255 " int x;\n" 11256 "};", 11257 Style); 11258 11259 verifyFormat("template <typename T> class rep<T>\n" 11260 "{\n" 11261 " int x;\n" 11262 "};", 11263 Style); 11264 11265 verifyFormat("template <typename T> class rep<std::complex<T>>\n" 11266 "{\n" 11267 " int x;\n" 11268 "};", 11269 Style); 11270 verifyFormat("template <typename T> class rep<std::complex<T>>\n" 11271 "{\n" 11272 "};", 11273 Style); 11274 11275 verifyFormat("#include \"stdint.h\"\n" 11276 "namespace rep {}", 11277 Style); 11278 verifyFormat("#include <stdint.h>\n" 11279 "namespace rep {}", 11280 Style); 11281 verifyFormat("#include <stdint.h>\n" 11282 "namespace rep {}", 11283 "#include <stdint.h>\n" 11284 "namespace rep {\n" 11285 "\n" 11286 "\n" 11287 "}", 11288 Style); 11289 } 11290 11291 TEST_F(FormatTest, SplitEmptyStruct) { 11292 FormatStyle Style = getLLVMStyle(); 11293 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 11294 Style.BraceWrapping.AfterStruct = true; 11295 Style.BraceWrapping.SplitEmptyRecord = false; 11296 11297 verifyFormat("struct Foo\n" 11298 "{};", 11299 Style); 11300 verifyFormat("/* something */ struct Foo\n" 11301 "{};", 11302 Style); 11303 verifyFormat("template <typename X> struct Foo\n" 11304 "{};", 11305 Style); 11306 verifyFormat("struct Foo\n" 11307 "{\n" 11308 " Foo();\n" 11309 "};", 11310 Style); 11311 verifyFormat("typedef struct Foo\n" 11312 "{\n" 11313 "} Foo_t;", 11314 Style); 11315 // typedef struct Bar {} Bar_t; 11316 } 11317 11318 TEST_F(FormatTest, SplitEmptyUnion) { 11319 FormatStyle Style = getLLVMStyle(); 11320 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 11321 Style.BraceWrapping.AfterUnion = true; 11322 Style.BraceWrapping.SplitEmptyRecord = false; 11323 11324 verifyFormat("union Foo\n" 11325 "{};", 11326 Style); 11327 verifyFormat("/* something */ union Foo\n" 11328 "{};", 11329 Style); 11330 verifyFormat("union Foo\n" 11331 "{\n" 11332 " A,\n" 11333 "};", 11334 Style); 11335 verifyFormat("typedef union Foo\n" 11336 "{\n" 11337 "} Foo_t;", 11338 Style); 11339 } 11340 11341 TEST_F(FormatTest, SplitEmptyNamespace) { 11342 FormatStyle Style = getLLVMStyle(); 11343 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 11344 Style.BraceWrapping.AfterNamespace = true; 11345 Style.BraceWrapping.SplitEmptyNamespace = false; 11346 11347 verifyFormat("namespace Foo\n" 11348 "{};", 11349 Style); 11350 verifyFormat("/* something */ namespace Foo\n" 11351 "{};", 11352 Style); 11353 verifyFormat("inline namespace Foo\n" 11354 "{};", 11355 Style); 11356 verifyFormat("/* something */ inline namespace Foo\n" 11357 "{};", 11358 Style); 11359 verifyFormat("export namespace Foo\n" 11360 "{};", 11361 Style); 11362 verifyFormat("namespace Foo\n" 11363 "{\n" 11364 "void Bar();\n" 11365 "};", 11366 Style); 11367 } 11368 11369 TEST_F(FormatTest, NeverMergeShortRecords) { 11370 FormatStyle Style = getLLVMStyle(); 11371 11372 verifyFormat("class Foo {\n" 11373 " Foo();\n" 11374 "};", 11375 Style); 11376 verifyFormat("typedef class Foo {\n" 11377 " Foo();\n" 11378 "} Foo_t;", 11379 Style); 11380 verifyFormat("struct Foo {\n" 11381 " Foo();\n" 11382 "};", 11383 Style); 11384 verifyFormat("typedef struct Foo {\n" 11385 " Foo();\n" 11386 "} Foo_t;", 11387 Style); 11388 verifyFormat("union Foo {\n" 11389 " A,\n" 11390 "};", 11391 Style); 11392 verifyFormat("typedef union Foo {\n" 11393 " A,\n" 11394 "} Foo_t;", 11395 Style); 11396 verifyFormat("namespace Foo {\n" 11397 "void Bar();\n" 11398 "};", 11399 Style); 11400 11401 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 11402 Style.BraceWrapping.AfterClass = true; 11403 Style.BraceWrapping.AfterStruct = true; 11404 Style.BraceWrapping.AfterUnion = true; 11405 Style.BraceWrapping.AfterNamespace = true; 11406 verifyFormat("class Foo\n" 11407 "{\n" 11408 " Foo();\n" 11409 "};", 11410 Style); 11411 verifyFormat("typedef class Foo\n" 11412 "{\n" 11413 " Foo();\n" 11414 "} Foo_t;", 11415 Style); 11416 verifyFormat("struct Foo\n" 11417 "{\n" 11418 " Foo();\n" 11419 "};", 11420 Style); 11421 verifyFormat("typedef struct Foo\n" 11422 "{\n" 11423 " Foo();\n" 11424 "} Foo_t;", 11425 Style); 11426 verifyFormat("union Foo\n" 11427 "{\n" 11428 " A,\n" 11429 "};", 11430 Style); 11431 verifyFormat("typedef union Foo\n" 11432 "{\n" 11433 " A,\n" 11434 "} Foo_t;", 11435 Style); 11436 verifyFormat("namespace Foo\n" 11437 "{\n" 11438 "void Bar();\n" 11439 "};", 11440 Style); 11441 } 11442 11443 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { 11444 // Elaborate type variable declarations. 11445 verifyFormat("struct foo a = {bar};\nint n;"); 11446 verifyFormat("class foo a = {bar};\nint n;"); 11447 verifyFormat("union foo a = {bar};\nint n;"); 11448 11449 // Elaborate types inside function definitions. 11450 verifyFormat("struct foo f() {}\nint n;"); 11451 verifyFormat("class foo f() {}\nint n;"); 11452 verifyFormat("union foo f() {}\nint n;"); 11453 11454 // Templates. 11455 verifyFormat("template <class X> void f() {}\nint n;"); 11456 verifyFormat("template <struct X> void f() {}\nint n;"); 11457 verifyFormat("template <union X> void f() {}\nint n;"); 11458 11459 // Actual definitions... 11460 verifyFormat("struct {\n} n;"); 11461 verifyFormat( 11462 "template <template <class T, class Y>, class Z> class X {\n} n;"); 11463 verifyFormat("union Z {\n int n;\n} x;"); 11464 verifyFormat("class MACRO Z {\n} n;"); 11465 verifyFormat("class MACRO(X) Z {\n} n;"); 11466 verifyFormat("class __attribute__(X) Z {\n} n;"); 11467 verifyFormat("class __declspec(X) Z {\n} n;"); 11468 verifyFormat("class A##B##C {\n} n;"); 11469 verifyFormat("class alignas(16) Z {\n} n;"); 11470 verifyFormat("class MACRO(X) alignas(16) Z {\n} n;"); 11471 verifyFormat("class MACROA MACRO(X) Z {\n} n;"); 11472 11473 // Redefinition from nested context: 11474 verifyFormat("class A::B::C {\n} n;"); 11475 11476 // Template definitions. 11477 verifyFormat( 11478 "template <typename F>\n" 11479 "Matcher(const Matcher<F> &Other,\n" 11480 " typename enable_if_c<is_base_of<F, T>::value &&\n" 11481 " !is_same<F, T>::value>::type * = 0)\n" 11482 " : Implementation(new ImplicitCastMatcher<F>(Other)) {}"); 11483 11484 // FIXME: This is still incorrectly handled at the formatter side. 11485 verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};"); 11486 verifyFormat("int i = SomeFunction(a<b, a> b);"); 11487 11488 // FIXME: 11489 // This now gets parsed incorrectly as class definition. 11490 // verifyFormat("class A<int> f() {\n}\nint n;"); 11491 11492 // Elaborate types where incorrectly parsing the structural element would 11493 // break the indent. 11494 verifyFormat("if (true)\n" 11495 " class X x;\n" 11496 "else\n" 11497 " f();\n"); 11498 11499 // This is simply incomplete. Formatting is not important, but must not crash. 11500 verifyFormat("class A:"); 11501 } 11502 11503 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { 11504 EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", 11505 format("#error Leave all white!!!!! space* alone!\n")); 11506 EXPECT_EQ( 11507 "#warning Leave all white!!!!! space* alone!\n", 11508 format("#warning Leave all white!!!!! space* alone!\n")); 11509 EXPECT_EQ("#error 1", format(" # error 1")); 11510 EXPECT_EQ("#warning 1", format(" # warning 1")); 11511 } 11512 11513 TEST_F(FormatTest, FormatHashIfExpressions) { 11514 verifyFormat("#if AAAA && BBBB"); 11515 verifyFormat("#if (AAAA && BBBB)"); 11516 verifyFormat("#elif (AAAA && BBBB)"); 11517 // FIXME: Come up with a better indentation for #elif. 11518 verifyFormat( 11519 "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" 11520 " defined(BBBBBBBB)\n" 11521 "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" 11522 " defined(BBBBBBBB)\n" 11523 "#endif", 11524 getLLVMStyleWithColumns(65)); 11525 } 11526 11527 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { 11528 FormatStyle AllowsMergedIf = getGoogleStyle(); 11529 AllowsMergedIf.AllowShortIfStatementsOnASingleLine = 11530 FormatStyle::SIS_WithoutElse; 11531 verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); 11532 verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); 11533 verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); 11534 EXPECT_EQ("if (true) return 42;", 11535 format("if (true)\nreturn 42;", AllowsMergedIf)); 11536 FormatStyle ShortMergedIf = AllowsMergedIf; 11537 ShortMergedIf.ColumnLimit = 25; 11538 verifyFormat("#define A \\\n" 11539 " if (true) return 42;", 11540 ShortMergedIf); 11541 verifyFormat("#define A \\\n" 11542 " f(); \\\n" 11543 " if (true)\n" 11544 "#define B", 11545 ShortMergedIf); 11546 verifyFormat("#define A \\\n" 11547 " f(); \\\n" 11548 " if (true)\n" 11549 "g();", 11550 ShortMergedIf); 11551 verifyFormat("{\n" 11552 "#ifdef A\n" 11553 " // Comment\n" 11554 " if (true) continue;\n" 11555 "#endif\n" 11556 " // Comment\n" 11557 " if (true) continue;\n" 11558 "}", 11559 ShortMergedIf); 11560 ShortMergedIf.ColumnLimit = 33; 11561 verifyFormat("#define A \\\n" 11562 " if constexpr (true) return 42;", 11563 ShortMergedIf); 11564 verifyFormat("#define A \\\n" 11565 " if CONSTEXPR (true) return 42;", 11566 ShortMergedIf); 11567 ShortMergedIf.ColumnLimit = 29; 11568 verifyFormat("#define A \\\n" 11569 " if (aaaaaaaaaa) return 1; \\\n" 11570 " return 2;", 11571 ShortMergedIf); 11572 ShortMergedIf.ColumnLimit = 28; 11573 verifyFormat("#define A \\\n" 11574 " if (aaaaaaaaaa) \\\n" 11575 " return 1; \\\n" 11576 " return 2;", 11577 ShortMergedIf); 11578 verifyFormat("#define A \\\n" 11579 " if constexpr (aaaaaaa) \\\n" 11580 " return 1; \\\n" 11581 " return 2;", 11582 ShortMergedIf); 11583 verifyFormat("#define A \\\n" 11584 " if CONSTEXPR (aaaaaaa) \\\n" 11585 " return 1; \\\n" 11586 " return 2;", 11587 ShortMergedIf); 11588 } 11589 11590 TEST_F(FormatTest, FormatStarDependingOnContext) { 11591 verifyFormat("void f(int *a);"); 11592 verifyFormat("void f() { f(fint * b); }"); 11593 verifyFormat("class A {\n void f(int *a);\n};"); 11594 verifyFormat("class A {\n int *a;\n};"); 11595 verifyFormat("namespace a {\n" 11596 "namespace b {\n" 11597 "class A {\n" 11598 " void f() {}\n" 11599 " int *a;\n" 11600 "};\n" 11601 "} // namespace b\n" 11602 "} // namespace a"); 11603 } 11604 11605 TEST_F(FormatTest, SpecialTokensAtEndOfLine) { 11606 verifyFormat("while"); 11607 verifyFormat("operator"); 11608 } 11609 11610 TEST_F(FormatTest, SkipsDeeplyNestedLines) { 11611 // This code would be painfully slow to format if we didn't skip it. 11612 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 11613 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 11614 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 11615 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 11616 "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" 11617 "A(1, 1)\n" 11618 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x 11619 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 11620 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 11621 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 11622 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 11623 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 11624 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 11625 ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" 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 // Deeply nested part is untouched, rest is formatted. 11629 EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n", 11630 format(std::string("int i;\n") + Code + "int j;\n", 11631 getLLVMStyle(), SC_ExpectIncomplete)); 11632 } 11633 11634 //===----------------------------------------------------------------------===// 11635 // Objective-C tests. 11636 //===----------------------------------------------------------------------===// 11637 11638 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { 11639 verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); 11640 EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", 11641 format("-(NSUInteger)indexOfObject:(id)anObject;")); 11642 EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); 11643 EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); 11644 EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", 11645 format("-(NSInteger)Method3:(id)anObject;")); 11646 EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", 11647 format("-(NSInteger)Method4:(id)anObject;")); 11648 EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", 11649 format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); 11650 EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", 11651 format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); 11652 EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " 11653 "forAllCells:(BOOL)flag;", 11654 format("- (void)sendAction:(SEL)aSelector to:(id)anObject " 11655 "forAllCells:(BOOL)flag;")); 11656 11657 // Very long objectiveC method declaration. 11658 verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" 11659 " (SoooooooooooooooooooooomeType *)bbbbbbbbbb;"); 11660 verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" 11661 " inRange:(NSRange)range\n" 11662 " outRange:(NSRange)out_range\n" 11663 " outRange1:(NSRange)out_range1\n" 11664 " outRange2:(NSRange)out_range2\n" 11665 " outRange3:(NSRange)out_range3\n" 11666 " outRange4:(NSRange)out_range4\n" 11667 " outRange5:(NSRange)out_range5\n" 11668 " outRange6:(NSRange)out_range6\n" 11669 " outRange7:(NSRange)out_range7\n" 11670 " outRange8:(NSRange)out_range8\n" 11671 " outRange9:(NSRange)out_range9;"); 11672 11673 // When the function name has to be wrapped. 11674 FormatStyle Style = getLLVMStyle(); 11675 // ObjC ignores IndentWrappedFunctionNames when wrapping methods 11676 // and always indents instead. 11677 Style.IndentWrappedFunctionNames = false; 11678 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 11679 " veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n" 11680 " anotherName:(NSString)bbbbbbbbbbbbbb {\n" 11681 "}", 11682 Style); 11683 Style.IndentWrappedFunctionNames = true; 11684 verifyFormat("- (SomeLooooooooooooooooooooongType *)\n" 11685 " veryLooooooooooongName:(NSString)cccccccccccccc\n" 11686 " anotherName:(NSString)dddddddddddddd {\n" 11687 "}", 11688 Style); 11689 11690 verifyFormat("- (int)sum:(vector<int>)numbers;"); 11691 verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); 11692 // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC 11693 // protocol lists (but not for template classes): 11694 // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); 11695 11696 verifyFormat("- (int (*)())foo:(int (*)())f;"); 11697 verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;"); 11698 11699 // If there's no return type (very rare in practice!), LLVM and Google style 11700 // agree. 11701 verifyFormat("- foo;"); 11702 verifyFormat("- foo:(int)f;"); 11703 verifyGoogleFormat("- foo:(int)foo;"); 11704 } 11705 11706 TEST_F(FormatTest, BreaksStringLiterals) { 11707 EXPECT_EQ("\"some text \"\n" 11708 "\"other\";", 11709 format("\"some text other\";", getLLVMStyleWithColumns(12))); 11710 EXPECT_EQ("\"some text \"\n" 11711 "\"other\";", 11712 format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); 11713 EXPECT_EQ( 11714 "#define A \\\n" 11715 " \"some \" \\\n" 11716 " \"text \" \\\n" 11717 " \"other\";", 11718 format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); 11719 EXPECT_EQ( 11720 "#define A \\\n" 11721 " \"so \" \\\n" 11722 " \"text \" \\\n" 11723 " \"other\";", 11724 format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); 11725 11726 EXPECT_EQ("\"some text\"", 11727 format("\"some text\"", getLLVMStyleWithColumns(1))); 11728 EXPECT_EQ("\"some text\"", 11729 format("\"some text\"", getLLVMStyleWithColumns(11))); 11730 EXPECT_EQ("\"some \"\n" 11731 "\"text\"", 11732 format("\"some text\"", getLLVMStyleWithColumns(10))); 11733 EXPECT_EQ("\"some \"\n" 11734 "\"text\"", 11735 format("\"some text\"", getLLVMStyleWithColumns(7))); 11736 EXPECT_EQ("\"some\"\n" 11737 "\" tex\"\n" 11738 "\"t\"", 11739 format("\"some text\"", getLLVMStyleWithColumns(6))); 11740 EXPECT_EQ("\"some\"\n" 11741 "\" tex\"\n" 11742 "\" and\"", 11743 format("\"some tex and\"", getLLVMStyleWithColumns(6))); 11744 EXPECT_EQ("\"some\"\n" 11745 "\"/tex\"\n" 11746 "\"/and\"", 11747 format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); 11748 11749 EXPECT_EQ("variable =\n" 11750 " \"long string \"\n" 11751 " \"literal\";", 11752 format("variable = \"long string literal\";", 11753 getLLVMStyleWithColumns(20))); 11754 11755 EXPECT_EQ("variable = f(\n" 11756 " \"long string \"\n" 11757 " \"literal\",\n" 11758 " short,\n" 11759 " loooooooooooooooooooong);", 11760 format("variable = f(\"long string literal\", short, " 11761 "loooooooooooooooooooong);", 11762 getLLVMStyleWithColumns(20))); 11763 11764 EXPECT_EQ( 11765 "f(g(\"long string \"\n" 11766 " \"literal\"),\n" 11767 " b);", 11768 format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); 11769 EXPECT_EQ("f(g(\"long string \"\n" 11770 " \"literal\",\n" 11771 " a),\n" 11772 " b);", 11773 format("f(g(\"long string literal\", a), b);", 11774 getLLVMStyleWithColumns(20))); 11775 EXPECT_EQ( 11776 "f(\"one two\".split(\n" 11777 " variable));", 11778 format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); 11779 EXPECT_EQ("f(\"one two three four five six \"\n" 11780 " \"seven\".split(\n" 11781 " really_looooong_variable));", 11782 format("f(\"one two three four five six seven\"." 11783 "split(really_looooong_variable));", 11784 getLLVMStyleWithColumns(33))); 11785 11786 EXPECT_EQ("f(\"some \"\n" 11787 " \"text\",\n" 11788 " other);", 11789 format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); 11790 11791 // Only break as a last resort. 11792 verifyFormat( 11793 "aaaaaaaaaaaaaaaaaaaa(\n" 11794 " aaaaaaaaaaaaaaaaaaaa,\n" 11795 " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); 11796 11797 EXPECT_EQ("\"splitmea\"\n" 11798 "\"trandomp\"\n" 11799 "\"oint\"", 11800 format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); 11801 11802 EXPECT_EQ("\"split/\"\n" 11803 "\"pathat/\"\n" 11804 "\"slashes\"", 11805 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 11806 11807 EXPECT_EQ("\"split/\"\n" 11808 "\"pathat/\"\n" 11809 "\"slashes\"", 11810 format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); 11811 EXPECT_EQ("\"split at \"\n" 11812 "\"spaces/at/\"\n" 11813 "\"slashes.at.any$\"\n" 11814 "\"non-alphanumeric%\"\n" 11815 "\"1111111111characte\"\n" 11816 "\"rs\"", 11817 format("\"split at " 11818 "spaces/at/" 11819 "slashes.at." 11820 "any$non-" 11821 "alphanumeric%" 11822 "1111111111characte" 11823 "rs\"", 11824 getLLVMStyleWithColumns(20))); 11825 11826 // Verify that splitting the strings understands 11827 // Style::AlwaysBreakBeforeMultilineStrings. 11828 EXPECT_EQ("aaaaaaaaaaaa(\n" 11829 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" 11830 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", 11831 format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " 11832 "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 11833 "aaaaaaaaaaaaaaaaaaaaaa\");", 11834 getGoogleStyle())); 11835 EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 11836 " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", 11837 format("return \"aaaaaaaaaaaaaaaaaaaaaa " 11838 "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " 11839 "aaaaaaaaaaaaaaaaaaaaaa\";", 11840 getGoogleStyle())); 11841 EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 11842 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 11843 format("llvm::outs() << " 11844 "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" 11845 "aaaaaaaaaaaaaaaaaaa\";")); 11846 EXPECT_EQ("ffff(\n" 11847 " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" 11848 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 11849 format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " 11850 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", 11851 getGoogleStyle())); 11852 11853 FormatStyle Style = getLLVMStyleWithColumns(12); 11854 Style.BreakStringLiterals = false; 11855 EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); 11856 11857 FormatStyle AlignLeft = getLLVMStyleWithColumns(12); 11858 AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; 11859 EXPECT_EQ("#define A \\\n" 11860 " \"some \" \\\n" 11861 " \"text \" \\\n" 11862 " \"other\";", 11863 format("#define A \"some text other\";", AlignLeft)); 11864 } 11865 11866 TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) { 11867 EXPECT_EQ("C a = \"some more \"\n" 11868 " \"text\";", 11869 format("C a = \"some more text\";", getLLVMStyleWithColumns(18))); 11870 } 11871 11872 TEST_F(FormatTest, FullyRemoveEmptyLines) { 11873 FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); 11874 NoEmptyLines.MaxEmptyLinesToKeep = 0; 11875 EXPECT_EQ("int i = a(b());", 11876 format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); 11877 } 11878 11879 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { 11880 EXPECT_EQ( 11881 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11882 "(\n" 11883 " \"x\t\");", 11884 format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 11885 "aaaaaaa(" 11886 "\"x\t\");")); 11887 } 11888 11889 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { 11890 EXPECT_EQ( 11891 "u8\"utf8 string \"\n" 11892 "u8\"literal\";", 11893 format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); 11894 EXPECT_EQ( 11895 "u\"utf16 string \"\n" 11896 "u\"literal\";", 11897 format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); 11898 EXPECT_EQ( 11899 "U\"utf32 string \"\n" 11900 "U\"literal\";", 11901 format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); 11902 EXPECT_EQ("L\"wide string \"\n" 11903 "L\"literal\";", 11904 format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); 11905 EXPECT_EQ("@\"NSString \"\n" 11906 "@\"literal\";", 11907 format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); 11908 verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26)); 11909 11910 // This input makes clang-format try to split the incomplete unicode escape 11911 // sequence, which used to lead to a crasher. 11912 verifyNoCrash( 11913 "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 11914 getLLVMStyleWithColumns(60)); 11915 } 11916 11917 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { 11918 FormatStyle Style = getGoogleStyleWithColumns(15); 11919 EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); 11920 EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); 11921 EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); 11922 EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); 11923 EXPECT_EQ("u8R\"x(raw literal)x\";", 11924 format("u8R\"x(raw literal)x\";", Style)); 11925 } 11926 11927 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { 11928 FormatStyle Style = getLLVMStyleWithColumns(20); 11929 EXPECT_EQ( 11930 "_T(\"aaaaaaaaaaaaaa\")\n" 11931 "_T(\"aaaaaaaaaaaaaa\")\n" 11932 "_T(\"aaaaaaaaaaaa\")", 11933 format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); 11934 EXPECT_EQ("f(x,\n" 11935 " _T(\"aaaaaaaaaaaa\")\n" 11936 " _T(\"aaa\"),\n" 11937 " z);", 11938 format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); 11939 11940 // FIXME: Handle embedded spaces in one iteration. 11941 // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" 11942 // "_T(\"aaaaaaaaaaaaa\")\n" 11943 // "_T(\"aaaaaaaaaaaaa\")\n" 11944 // "_T(\"a\")", 11945 // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 11946 // getLLVMStyleWithColumns(20))); 11947 EXPECT_EQ( 11948 "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", 11949 format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); 11950 EXPECT_EQ("f(\n" 11951 "#if !TEST\n" 11952 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 11953 "#endif\n" 11954 ");", 11955 format("f(\n" 11956 "#if !TEST\n" 11957 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" 11958 "#endif\n" 11959 ");")); 11960 EXPECT_EQ("f(\n" 11961 "\n" 11962 " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", 11963 format("f(\n" 11964 "\n" 11965 "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); 11966 } 11967 11968 TEST_F(FormatTest, BreaksStringLiteralOperands) { 11969 // In a function call with two operands, the second can be broken with no line 11970 // break before it. 11971 EXPECT_EQ( 11972 "func(a, \"long long \"\n" 11973 " \"long long\");", 11974 format("func(a, \"long long long long\");", getLLVMStyleWithColumns(24))); 11975 // In a function call with three operands, the second must be broken with a 11976 // line break before it. 11977 EXPECT_EQ("func(a,\n" 11978 " \"long long long \"\n" 11979 " \"long\",\n" 11980 " c);", 11981 format("func(a, \"long long long long\", c);", 11982 getLLVMStyleWithColumns(24))); 11983 // In a function call with three operands, the third must be broken with a 11984 // line break before it. 11985 EXPECT_EQ("func(a, b,\n" 11986 " \"long long long \"\n" 11987 " \"long\");", 11988 format("func(a, b, \"long long long long\");", 11989 getLLVMStyleWithColumns(24))); 11990 // In a function call with three operands, both the second and the third must 11991 // be broken with a line break before them. 11992 EXPECT_EQ("func(a,\n" 11993 " \"long long long \"\n" 11994 " \"long\",\n" 11995 " \"long long long \"\n" 11996 " \"long\");", 11997 format("func(a, \"long long long long\", \"long long long long\");", 11998 getLLVMStyleWithColumns(24))); 11999 // In a chain of << with two operands, the second can be broken with no line 12000 // break before it. 12001 EXPECT_EQ("a << \"line line \"\n" 12002 " \"line\";", 12003 format("a << \"line line line\";", getLLVMStyleWithColumns(20))); 12004 // In a chain of << with three operands, the second can be broken with no line 12005 // break before it. 12006 EXPECT_EQ( 12007 "abcde << \"line \"\n" 12008 " \"line line\"\n" 12009 " << c;", 12010 format("abcde << \"line line line\" << c;", getLLVMStyleWithColumns(20))); 12011 // In a chain of << with three operands, the third must be broken with a line 12012 // break before it. 12013 EXPECT_EQ( 12014 "a << b\n" 12015 " << \"line line \"\n" 12016 " \"line\";", 12017 format("a << b << \"line line line\";", getLLVMStyleWithColumns(20))); 12018 // In a chain of << with three operands, the second can be broken with no line 12019 // break before it and the third must be broken with a line break before it. 12020 EXPECT_EQ("abcd << \"line line \"\n" 12021 " \"line\"\n" 12022 " << \"line line \"\n" 12023 " \"line\";", 12024 format("abcd << \"line line line\" << \"line line line\";", 12025 getLLVMStyleWithColumns(20))); 12026 // In a chain of binary operators with two operands, the second can be broken 12027 // with no line break before it. 12028 EXPECT_EQ( 12029 "abcd + \"line line \"\n" 12030 " \"line line\";", 12031 format("abcd + \"line line line line\";", getLLVMStyleWithColumns(20))); 12032 // In a chain of binary operators with three operands, the second must be 12033 // broken with a line break before it. 12034 EXPECT_EQ("abcd +\n" 12035 " \"line line \"\n" 12036 " \"line line\" +\n" 12037 " e;", 12038 format("abcd + \"line line line line\" + e;", 12039 getLLVMStyleWithColumns(20))); 12040 // In a function call with two operands, with AlignAfterOpenBracket enabled, 12041 // the first must be broken with a line break before it. 12042 FormatStyle Style = getLLVMStyleWithColumns(25); 12043 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 12044 EXPECT_EQ("someFunction(\n" 12045 " \"long long long \"\n" 12046 " \"long\",\n" 12047 " a);", 12048 format("someFunction(\"long long long long\", a);", Style)); 12049 } 12050 12051 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { 12052 EXPECT_EQ( 12053 "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 12054 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 12055 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", 12056 format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 12057 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" 12058 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); 12059 } 12060 12061 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { 12062 EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", 12063 format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); 12064 EXPECT_EQ("fffffffffff(g(R\"x(\n" 12065 "multiline raw string literal xxxxxxxxxxxxxx\n" 12066 ")x\",\n" 12067 " a),\n" 12068 " b);", 12069 format("fffffffffff(g(R\"x(\n" 12070 "multiline raw string literal xxxxxxxxxxxxxx\n" 12071 ")x\", a), b);", 12072 getGoogleStyleWithColumns(20))); 12073 EXPECT_EQ("fffffffffff(\n" 12074 " g(R\"x(qqq\n" 12075 "multiline raw string literal xxxxxxxxxxxxxx\n" 12076 ")x\",\n" 12077 " a),\n" 12078 " b);", 12079 format("fffffffffff(g(R\"x(qqq\n" 12080 "multiline raw string literal xxxxxxxxxxxxxx\n" 12081 ")x\", a), b);", 12082 getGoogleStyleWithColumns(20))); 12083 12084 EXPECT_EQ("fffffffffff(R\"x(\n" 12085 "multiline raw string literal xxxxxxxxxxxxxx\n" 12086 ")x\");", 12087 format("fffffffffff(R\"x(\n" 12088 "multiline raw string literal xxxxxxxxxxxxxx\n" 12089 ")x\");", 12090 getGoogleStyleWithColumns(20))); 12091 EXPECT_EQ("fffffffffff(R\"x(\n" 12092 "multiline raw string literal xxxxxxxxxxxxxx\n" 12093 ")x\" + bbbbbb);", 12094 format("fffffffffff(R\"x(\n" 12095 "multiline raw string literal xxxxxxxxxxxxxx\n" 12096 ")x\" + bbbbbb);", 12097 getGoogleStyleWithColumns(20))); 12098 EXPECT_EQ("fffffffffff(\n" 12099 " R\"x(\n" 12100 "multiline raw string literal xxxxxxxxxxxxxx\n" 12101 ")x\" +\n" 12102 " bbbbbb);", 12103 format("fffffffffff(\n" 12104 " R\"x(\n" 12105 "multiline raw string literal xxxxxxxxxxxxxx\n" 12106 ")x\" + bbbbbb);", 12107 getGoogleStyleWithColumns(20))); 12108 EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);", 12109 format("fffffffffff(\n" 12110 " R\"(single line raw string)\" + bbbbbb);")); 12111 } 12112 12113 TEST_F(FormatTest, SkipsUnknownStringLiterals) { 12114 verifyFormat("string a = \"unterminated;"); 12115 EXPECT_EQ("function(\"unterminated,\n" 12116 " OtherParameter);", 12117 format("function( \"unterminated,\n" 12118 " OtherParameter);")); 12119 } 12120 12121 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { 12122 FormatStyle Style = getLLVMStyle(); 12123 Style.Standard = FormatStyle::LS_Cpp03; 12124 EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", 12125 format("#define x(_a) printf(\"foo\"_a);", Style)); 12126 } 12127 12128 TEST_F(FormatTest, CppLexVersion) { 12129 FormatStyle Style = getLLVMStyle(); 12130 // Formatting of x * y differs if x is a type. 12131 verifyFormat("void foo() { MACRO(a * b); }", Style); 12132 verifyFormat("void foo() { MACRO(int *b); }", Style); 12133 12134 // LLVM style uses latest lexer. 12135 verifyFormat("void foo() { MACRO(char8_t *b); }", Style); 12136 Style.Standard = FormatStyle::LS_Cpp17; 12137 // But in c++17, char8_t isn't a keyword. 12138 verifyFormat("void foo() { MACRO(char8_t * b); }", Style); 12139 } 12140 12141 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } 12142 12143 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { 12144 EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" 12145 " \"ddeeefff\");", 12146 format("someFunction(\"aaabbbcccdddeeefff\");", 12147 getLLVMStyleWithColumns(25))); 12148 EXPECT_EQ("someFunction1234567890(\n" 12149 " \"aaabbbcccdddeeefff\");", 12150 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 12151 getLLVMStyleWithColumns(26))); 12152 EXPECT_EQ("someFunction1234567890(\n" 12153 " \"aaabbbcccdddeeeff\"\n" 12154 " \"f\");", 12155 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 12156 getLLVMStyleWithColumns(25))); 12157 EXPECT_EQ("someFunction1234567890(\n" 12158 " \"aaabbbcccdddeeeff\"\n" 12159 " \"f\");", 12160 format("someFunction1234567890(\"aaabbbcccdddeeefff\");", 12161 getLLVMStyleWithColumns(24))); 12162 EXPECT_EQ("someFunction(\n" 12163 " \"aaabbbcc ddde \"\n" 12164 " \"efff\");", 12165 format("someFunction(\"aaabbbcc ddde efff\");", 12166 getLLVMStyleWithColumns(25))); 12167 EXPECT_EQ("someFunction(\"aaabbbccc \"\n" 12168 " \"ddeeefff\");", 12169 format("someFunction(\"aaabbbccc ddeeefff\");", 12170 getLLVMStyleWithColumns(25))); 12171 EXPECT_EQ("someFunction1234567890(\n" 12172 " \"aaabb \"\n" 12173 " \"cccdddeeefff\");", 12174 format("someFunction1234567890(\"aaabb cccdddeeefff\");", 12175 getLLVMStyleWithColumns(25))); 12176 EXPECT_EQ("#define A \\\n" 12177 " string s = \\\n" 12178 " \"123456789\" \\\n" 12179 " \"0\"; \\\n" 12180 " int i;", 12181 format("#define A string s = \"1234567890\"; int i;", 12182 getLLVMStyleWithColumns(20))); 12183 EXPECT_EQ("someFunction(\n" 12184 " \"aaabbbcc \"\n" 12185 " \"dddeeefff\");", 12186 format("someFunction(\"aaabbbcc dddeeefff\");", 12187 getLLVMStyleWithColumns(25))); 12188 } 12189 12190 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { 12191 EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); 12192 EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); 12193 EXPECT_EQ("\"test\"\n" 12194 "\"\\n\"", 12195 format("\"test\\n\"", getLLVMStyleWithColumns(7))); 12196 EXPECT_EQ("\"tes\\\\\"\n" 12197 "\"n\"", 12198 format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); 12199 EXPECT_EQ("\"\\\\\\\\\"\n" 12200 "\"\\n\"", 12201 format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); 12202 EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); 12203 EXPECT_EQ("\"\\uff01\"\n" 12204 "\"test\"", 12205 format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); 12206 EXPECT_EQ("\"\\Uff01ff02\"", 12207 format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); 12208 EXPECT_EQ("\"\\x000000000001\"\n" 12209 "\"next\"", 12210 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); 12211 EXPECT_EQ("\"\\x000000000001next\"", 12212 format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); 12213 EXPECT_EQ("\"\\x000000000001\"", 12214 format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); 12215 EXPECT_EQ("\"test\"\n" 12216 "\"\\000000\"\n" 12217 "\"000001\"", 12218 format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); 12219 EXPECT_EQ("\"test\\000\"\n" 12220 "\"00000000\"\n" 12221 "\"1\"", 12222 format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); 12223 } 12224 12225 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { 12226 verifyFormat("void f() {\n" 12227 " return g() {}\n" 12228 " void h() {}"); 12229 verifyFormat("int a[] = {void forgot_closing_brace(){f();\n" 12230 "g();\n" 12231 "}"); 12232 } 12233 12234 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) { 12235 verifyFormat( 12236 "void f() { return C{param1, param2}.SomeCall(param1, param2); }"); 12237 } 12238 12239 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { 12240 verifyFormat("class X {\n" 12241 " void f() {\n" 12242 " }\n" 12243 "};", 12244 getLLVMStyleWithColumns(12)); 12245 } 12246 12247 TEST_F(FormatTest, ConfigurableIndentWidth) { 12248 FormatStyle EightIndent = getLLVMStyleWithColumns(18); 12249 EightIndent.IndentWidth = 8; 12250 EightIndent.ContinuationIndentWidth = 8; 12251 verifyFormat("void f() {\n" 12252 " someFunction();\n" 12253 " if (true) {\n" 12254 " f();\n" 12255 " }\n" 12256 "}", 12257 EightIndent); 12258 verifyFormat("class X {\n" 12259 " void f() {\n" 12260 " }\n" 12261 "};", 12262 EightIndent); 12263 verifyFormat("int x[] = {\n" 12264 " call(),\n" 12265 " call()};", 12266 EightIndent); 12267 } 12268 12269 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) { 12270 verifyFormat("double\n" 12271 "f();", 12272 getLLVMStyleWithColumns(8)); 12273 } 12274 12275 TEST_F(FormatTest, ConfigurableUseOfTab) { 12276 FormatStyle Tab = getLLVMStyleWithColumns(42); 12277 Tab.IndentWidth = 8; 12278 Tab.UseTab = FormatStyle::UT_Always; 12279 Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left; 12280 12281 EXPECT_EQ("if (aaaaaaaa && // q\n" 12282 " bb)\t\t// w\n" 12283 "\t;", 12284 format("if (aaaaaaaa &&// q\n" 12285 "bb)// w\n" 12286 ";", 12287 Tab)); 12288 EXPECT_EQ("if (aaa && bbb) // w\n" 12289 "\t;", 12290 format("if(aaa&&bbb)// w\n" 12291 ";", 12292 Tab)); 12293 12294 verifyFormat("class X {\n" 12295 "\tvoid f() {\n" 12296 "\t\tsomeFunction(parameter1,\n" 12297 "\t\t\t parameter2);\n" 12298 "\t}\n" 12299 "};", 12300 Tab); 12301 verifyFormat("#define A \\\n" 12302 "\tvoid f() { \\\n" 12303 "\t\tsomeFunction( \\\n" 12304 "\t\t parameter1, \\\n" 12305 "\t\t parameter2); \\\n" 12306 "\t}", 12307 Tab); 12308 verifyFormat("int a;\t // x\n" 12309 "int bbbbbbbb; // x\n", 12310 Tab); 12311 12312 Tab.TabWidth = 4; 12313 Tab.IndentWidth = 8; 12314 verifyFormat("class TabWidth4Indent8 {\n" 12315 "\t\tvoid f() {\n" 12316 "\t\t\t\tsomeFunction(parameter1,\n" 12317 "\t\t\t\t\t\t\t parameter2);\n" 12318 "\t\t}\n" 12319 "};", 12320 Tab); 12321 12322 Tab.TabWidth = 4; 12323 Tab.IndentWidth = 4; 12324 verifyFormat("class TabWidth4Indent4 {\n" 12325 "\tvoid f() {\n" 12326 "\t\tsomeFunction(parameter1,\n" 12327 "\t\t\t\t\t parameter2);\n" 12328 "\t}\n" 12329 "};", 12330 Tab); 12331 12332 Tab.TabWidth = 8; 12333 Tab.IndentWidth = 4; 12334 verifyFormat("class TabWidth8Indent4 {\n" 12335 " void f() {\n" 12336 "\tsomeFunction(parameter1,\n" 12337 "\t\t parameter2);\n" 12338 " }\n" 12339 "};", 12340 Tab); 12341 12342 Tab.TabWidth = 8; 12343 Tab.IndentWidth = 8; 12344 EXPECT_EQ("/*\n" 12345 "\t a\t\tcomment\n" 12346 "\t in multiple lines\n" 12347 " */", 12348 format(" /*\t \t \n" 12349 " \t \t a\t\tcomment\t \t\n" 12350 " \t \t in multiple lines\t\n" 12351 " \t */", 12352 Tab)); 12353 12354 Tab.UseTab = FormatStyle::UT_ForIndentation; 12355 verifyFormat("{\n" 12356 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 12357 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 12358 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 12359 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 12360 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 12361 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 12362 "};", 12363 Tab); 12364 verifyFormat("enum AA {\n" 12365 "\ta1, // Force multiple lines\n" 12366 "\ta2,\n" 12367 "\ta3\n" 12368 "};", 12369 Tab); 12370 EXPECT_EQ("if (aaaaaaaa && // q\n" 12371 " bb) // w\n" 12372 "\t;", 12373 format("if (aaaaaaaa &&// q\n" 12374 "bb)// w\n" 12375 ";", 12376 Tab)); 12377 verifyFormat("class X {\n" 12378 "\tvoid f() {\n" 12379 "\t\tsomeFunction(parameter1,\n" 12380 "\t\t parameter2);\n" 12381 "\t}\n" 12382 "};", 12383 Tab); 12384 verifyFormat("{\n" 12385 "\tQ(\n" 12386 "\t {\n" 12387 "\t\t int a;\n" 12388 "\t\t someFunction(aaaaaaaa,\n" 12389 "\t\t bbbbbbb);\n" 12390 "\t },\n" 12391 "\t p);\n" 12392 "}", 12393 Tab); 12394 EXPECT_EQ("{\n" 12395 "\t/* aaaa\n" 12396 "\t bbbb */\n" 12397 "}", 12398 format("{\n" 12399 "/* aaaa\n" 12400 " bbbb */\n" 12401 "}", 12402 Tab)); 12403 EXPECT_EQ("{\n" 12404 "\t/*\n" 12405 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 12406 "\t bbbbbbbbbbbbb\n" 12407 "\t*/\n" 12408 "}", 12409 format("{\n" 12410 "/*\n" 12411 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 12412 "*/\n" 12413 "}", 12414 Tab)); 12415 EXPECT_EQ("{\n" 12416 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 12417 "\t// bbbbbbbbbbbbb\n" 12418 "}", 12419 format("{\n" 12420 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 12421 "}", 12422 Tab)); 12423 EXPECT_EQ("{\n" 12424 "\t/*\n" 12425 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 12426 "\t bbbbbbbbbbbbb\n" 12427 "\t*/\n" 12428 "}", 12429 format("{\n" 12430 "\t/*\n" 12431 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 12432 "\t*/\n" 12433 "}", 12434 Tab)); 12435 EXPECT_EQ("{\n" 12436 "\t/*\n" 12437 "\n" 12438 "\t*/\n" 12439 "}", 12440 format("{\n" 12441 "\t/*\n" 12442 "\n" 12443 "\t*/\n" 12444 "}", 12445 Tab)); 12446 EXPECT_EQ("{\n" 12447 "\t/*\n" 12448 " asdf\n" 12449 "\t*/\n" 12450 "}", 12451 format("{\n" 12452 "\t/*\n" 12453 " asdf\n" 12454 "\t*/\n" 12455 "}", 12456 Tab)); 12457 12458 Tab.UseTab = FormatStyle::UT_Never; 12459 EXPECT_EQ("/*\n" 12460 " a\t\tcomment\n" 12461 " in multiple lines\n" 12462 " */", 12463 format(" /*\t \t \n" 12464 " \t \t a\t\tcomment\t \t\n" 12465 " \t \t in multiple lines\t\n" 12466 " \t */", 12467 Tab)); 12468 EXPECT_EQ("/* some\n" 12469 " comment */", 12470 format(" \t \t /* some\n" 12471 " \t \t comment */", 12472 Tab)); 12473 EXPECT_EQ("int a; /* some\n" 12474 " comment */", 12475 format(" \t \t int a; /* some\n" 12476 " \t \t comment */", 12477 Tab)); 12478 12479 EXPECT_EQ("int a; /* some\n" 12480 "comment */", 12481 format(" \t \t int\ta; /* some\n" 12482 " \t \t comment */", 12483 Tab)); 12484 EXPECT_EQ("f(\"\t\t\"); /* some\n" 12485 " comment */", 12486 format(" \t \t f(\"\t\t\"); /* some\n" 12487 " \t \t comment */", 12488 Tab)); 12489 EXPECT_EQ("{\n" 12490 " /*\n" 12491 " * Comment\n" 12492 " */\n" 12493 " int i;\n" 12494 "}", 12495 format("{\n" 12496 "\t/*\n" 12497 "\t * Comment\n" 12498 "\t */\n" 12499 "\t int i;\n" 12500 "}", 12501 Tab)); 12502 12503 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 12504 Tab.TabWidth = 8; 12505 Tab.IndentWidth = 8; 12506 EXPECT_EQ("if (aaaaaaaa && // q\n" 12507 " bb) // w\n" 12508 "\t;", 12509 format("if (aaaaaaaa &&// q\n" 12510 "bb)// w\n" 12511 ";", 12512 Tab)); 12513 EXPECT_EQ("if (aaa && bbb) // w\n" 12514 "\t;", 12515 format("if(aaa&&bbb)// w\n" 12516 ";", 12517 Tab)); 12518 verifyFormat("class X {\n" 12519 "\tvoid f() {\n" 12520 "\t\tsomeFunction(parameter1,\n" 12521 "\t\t\t parameter2);\n" 12522 "\t}\n" 12523 "};", 12524 Tab); 12525 verifyFormat("#define A \\\n" 12526 "\tvoid f() { \\\n" 12527 "\t\tsomeFunction( \\\n" 12528 "\t\t parameter1, \\\n" 12529 "\t\t parameter2); \\\n" 12530 "\t}", 12531 Tab); 12532 Tab.TabWidth = 4; 12533 Tab.IndentWidth = 8; 12534 verifyFormat("class TabWidth4Indent8 {\n" 12535 "\t\tvoid f() {\n" 12536 "\t\t\t\tsomeFunction(parameter1,\n" 12537 "\t\t\t\t\t\t\t parameter2);\n" 12538 "\t\t}\n" 12539 "};", 12540 Tab); 12541 Tab.TabWidth = 4; 12542 Tab.IndentWidth = 4; 12543 verifyFormat("class TabWidth4Indent4 {\n" 12544 "\tvoid f() {\n" 12545 "\t\tsomeFunction(parameter1,\n" 12546 "\t\t\t\t\t parameter2);\n" 12547 "\t}\n" 12548 "};", 12549 Tab); 12550 Tab.TabWidth = 8; 12551 Tab.IndentWidth = 4; 12552 verifyFormat("class TabWidth8Indent4 {\n" 12553 " void f() {\n" 12554 "\tsomeFunction(parameter1,\n" 12555 "\t\t parameter2);\n" 12556 " }\n" 12557 "};", 12558 Tab); 12559 Tab.TabWidth = 8; 12560 Tab.IndentWidth = 8; 12561 EXPECT_EQ("/*\n" 12562 "\t a\t\tcomment\n" 12563 "\t in multiple lines\n" 12564 " */", 12565 format(" /*\t \t \n" 12566 " \t \t a\t\tcomment\t \t\n" 12567 " \t \t in multiple lines\t\n" 12568 " \t */", 12569 Tab)); 12570 verifyFormat("{\n" 12571 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 12572 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 12573 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 12574 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 12575 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 12576 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 12577 "};", 12578 Tab); 12579 verifyFormat("enum AA {\n" 12580 "\ta1, // Force multiple lines\n" 12581 "\ta2,\n" 12582 "\ta3\n" 12583 "};", 12584 Tab); 12585 EXPECT_EQ("if (aaaaaaaa && // q\n" 12586 " bb) // w\n" 12587 "\t;", 12588 format("if (aaaaaaaa &&// q\n" 12589 "bb)// w\n" 12590 ";", 12591 Tab)); 12592 verifyFormat("class X {\n" 12593 "\tvoid f() {\n" 12594 "\t\tsomeFunction(parameter1,\n" 12595 "\t\t\t parameter2);\n" 12596 "\t}\n" 12597 "};", 12598 Tab); 12599 verifyFormat("{\n" 12600 "\tQ(\n" 12601 "\t {\n" 12602 "\t\t int a;\n" 12603 "\t\t someFunction(aaaaaaaa,\n" 12604 "\t\t\t\t bbbbbbb);\n" 12605 "\t },\n" 12606 "\t p);\n" 12607 "}", 12608 Tab); 12609 EXPECT_EQ("{\n" 12610 "\t/* aaaa\n" 12611 "\t bbbb */\n" 12612 "}", 12613 format("{\n" 12614 "/* aaaa\n" 12615 " bbbb */\n" 12616 "}", 12617 Tab)); 12618 EXPECT_EQ("{\n" 12619 "\t/*\n" 12620 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 12621 "\t bbbbbbbbbbbbb\n" 12622 "\t*/\n" 12623 "}", 12624 format("{\n" 12625 "/*\n" 12626 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 12627 "*/\n" 12628 "}", 12629 Tab)); 12630 EXPECT_EQ("{\n" 12631 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 12632 "\t// bbbbbbbbbbbbb\n" 12633 "}", 12634 format("{\n" 12635 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 12636 "}", 12637 Tab)); 12638 EXPECT_EQ("{\n" 12639 "\t/*\n" 12640 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 12641 "\t bbbbbbbbbbbbb\n" 12642 "\t*/\n" 12643 "}", 12644 format("{\n" 12645 "\t/*\n" 12646 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 12647 "\t*/\n" 12648 "}", 12649 Tab)); 12650 EXPECT_EQ("{\n" 12651 "\t/*\n" 12652 "\n" 12653 "\t*/\n" 12654 "}", 12655 format("{\n" 12656 "\t/*\n" 12657 "\n" 12658 "\t*/\n" 12659 "}", 12660 Tab)); 12661 EXPECT_EQ("{\n" 12662 "\t/*\n" 12663 " asdf\n" 12664 "\t*/\n" 12665 "}", 12666 format("{\n" 12667 "\t/*\n" 12668 " asdf\n" 12669 "\t*/\n" 12670 "}", 12671 Tab)); 12672 EXPECT_EQ("/* some\n" 12673 " comment */", 12674 format(" \t \t /* some\n" 12675 " \t \t comment */", 12676 Tab)); 12677 EXPECT_EQ("int a; /* some\n" 12678 " comment */", 12679 format(" \t \t int a; /* some\n" 12680 " \t \t comment */", 12681 Tab)); 12682 EXPECT_EQ("int a; /* some\n" 12683 "comment */", 12684 format(" \t \t int\ta; /* some\n" 12685 " \t \t comment */", 12686 Tab)); 12687 EXPECT_EQ("f(\"\t\t\"); /* some\n" 12688 " comment */", 12689 format(" \t \t f(\"\t\t\"); /* some\n" 12690 " \t \t comment */", 12691 Tab)); 12692 EXPECT_EQ("{\n" 12693 "\t/*\n" 12694 "\t * Comment\n" 12695 "\t */\n" 12696 "\tint i;\n" 12697 "}", 12698 format("{\n" 12699 "\t/*\n" 12700 "\t * Comment\n" 12701 "\t */\n" 12702 "\t int i;\n" 12703 "}", 12704 Tab)); 12705 Tab.TabWidth = 2; 12706 Tab.IndentWidth = 2; 12707 EXPECT_EQ("{\n" 12708 "\t/* aaaa\n" 12709 "\t\t bbbb */\n" 12710 "}", 12711 format("{\n" 12712 "/* aaaa\n" 12713 "\t bbbb */\n" 12714 "}", 12715 Tab)); 12716 EXPECT_EQ("{\n" 12717 "\t/*\n" 12718 "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n" 12719 "\t\tbbbbbbbbbbbbb\n" 12720 "\t*/\n" 12721 "}", 12722 format("{\n" 12723 "/*\n" 12724 "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 12725 "*/\n" 12726 "}", 12727 Tab)); 12728 Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 12729 Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 12730 Tab.TabWidth = 4; 12731 Tab.IndentWidth = 4; 12732 verifyFormat("class Assign {\n" 12733 "\tvoid f() {\n" 12734 "\t\tint x = 123;\n" 12735 "\t\tint random = 4;\n" 12736 "\t\tstd::string alphabet =\n" 12737 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 12738 "\t}\n" 12739 "};", 12740 Tab); 12741 12742 Tab.UseTab = FormatStyle::UT_AlignWithSpaces; 12743 Tab.TabWidth = 8; 12744 Tab.IndentWidth = 8; 12745 EXPECT_EQ("if (aaaaaaaa && // q\n" 12746 " bb) // w\n" 12747 "\t;", 12748 format("if (aaaaaaaa &&// q\n" 12749 "bb)// w\n" 12750 ";", 12751 Tab)); 12752 EXPECT_EQ("if (aaa && bbb) // w\n" 12753 "\t;", 12754 format("if(aaa&&bbb)// w\n" 12755 ";", 12756 Tab)); 12757 verifyFormat("class X {\n" 12758 "\tvoid f() {\n" 12759 "\t\tsomeFunction(parameter1,\n" 12760 "\t\t parameter2);\n" 12761 "\t}\n" 12762 "};", 12763 Tab); 12764 verifyFormat("#define A \\\n" 12765 "\tvoid f() { \\\n" 12766 "\t\tsomeFunction( \\\n" 12767 "\t\t parameter1, \\\n" 12768 "\t\t parameter2); \\\n" 12769 "\t}", 12770 Tab); 12771 Tab.TabWidth = 4; 12772 Tab.IndentWidth = 8; 12773 verifyFormat("class TabWidth4Indent8 {\n" 12774 "\t\tvoid f() {\n" 12775 "\t\t\t\tsomeFunction(parameter1,\n" 12776 "\t\t\t\t parameter2);\n" 12777 "\t\t}\n" 12778 "};", 12779 Tab); 12780 Tab.TabWidth = 4; 12781 Tab.IndentWidth = 4; 12782 verifyFormat("class TabWidth4Indent4 {\n" 12783 "\tvoid f() {\n" 12784 "\t\tsomeFunction(parameter1,\n" 12785 "\t\t parameter2);\n" 12786 "\t}\n" 12787 "};", 12788 Tab); 12789 Tab.TabWidth = 8; 12790 Tab.IndentWidth = 4; 12791 verifyFormat("class TabWidth8Indent4 {\n" 12792 " void f() {\n" 12793 "\tsomeFunction(parameter1,\n" 12794 "\t parameter2);\n" 12795 " }\n" 12796 "};", 12797 Tab); 12798 Tab.TabWidth = 8; 12799 Tab.IndentWidth = 8; 12800 EXPECT_EQ("/*\n" 12801 " a\t\tcomment\n" 12802 " in multiple lines\n" 12803 " */", 12804 format(" /*\t \t \n" 12805 " \t \t a\t\tcomment\t \t\n" 12806 " \t \t in multiple lines\t\n" 12807 " \t */", 12808 Tab)); 12809 verifyFormat("{\n" 12810 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 12811 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 12812 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 12813 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 12814 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 12815 "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" 12816 "};", 12817 Tab); 12818 verifyFormat("enum AA {\n" 12819 "\ta1, // Force multiple lines\n" 12820 "\ta2,\n" 12821 "\ta3\n" 12822 "};", 12823 Tab); 12824 EXPECT_EQ("if (aaaaaaaa && // q\n" 12825 " bb) // w\n" 12826 "\t;", 12827 format("if (aaaaaaaa &&// q\n" 12828 "bb)// w\n" 12829 ";", 12830 Tab)); 12831 verifyFormat("class X {\n" 12832 "\tvoid f() {\n" 12833 "\t\tsomeFunction(parameter1,\n" 12834 "\t\t parameter2);\n" 12835 "\t}\n" 12836 "};", 12837 Tab); 12838 verifyFormat("{\n" 12839 "\tQ(\n" 12840 "\t {\n" 12841 "\t\t int a;\n" 12842 "\t\t someFunction(aaaaaaaa,\n" 12843 "\t\t bbbbbbb);\n" 12844 "\t },\n" 12845 "\t p);\n" 12846 "}", 12847 Tab); 12848 EXPECT_EQ("{\n" 12849 "\t/* aaaa\n" 12850 "\t bbbb */\n" 12851 "}", 12852 format("{\n" 12853 "/* aaaa\n" 12854 " bbbb */\n" 12855 "}", 12856 Tab)); 12857 EXPECT_EQ("{\n" 12858 "\t/*\n" 12859 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 12860 "\t bbbbbbbbbbbbb\n" 12861 "\t*/\n" 12862 "}", 12863 format("{\n" 12864 "/*\n" 12865 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 12866 "*/\n" 12867 "}", 12868 Tab)); 12869 EXPECT_EQ("{\n" 12870 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 12871 "\t// bbbbbbbbbbbbb\n" 12872 "}", 12873 format("{\n" 12874 "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 12875 "}", 12876 Tab)); 12877 EXPECT_EQ("{\n" 12878 "\t/*\n" 12879 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 12880 "\t bbbbbbbbbbbbb\n" 12881 "\t*/\n" 12882 "}", 12883 format("{\n" 12884 "\t/*\n" 12885 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 12886 "\t*/\n" 12887 "}", 12888 Tab)); 12889 EXPECT_EQ("{\n" 12890 "\t/*\n" 12891 "\n" 12892 "\t*/\n" 12893 "}", 12894 format("{\n" 12895 "\t/*\n" 12896 "\n" 12897 "\t*/\n" 12898 "}", 12899 Tab)); 12900 EXPECT_EQ("{\n" 12901 "\t/*\n" 12902 " asdf\n" 12903 "\t*/\n" 12904 "}", 12905 format("{\n" 12906 "\t/*\n" 12907 " asdf\n" 12908 "\t*/\n" 12909 "}", 12910 Tab)); 12911 EXPECT_EQ("/* some\n" 12912 " comment */", 12913 format(" \t \t /* some\n" 12914 " \t \t comment */", 12915 Tab)); 12916 EXPECT_EQ("int a; /* some\n" 12917 " comment */", 12918 format(" \t \t int a; /* some\n" 12919 " \t \t comment */", 12920 Tab)); 12921 EXPECT_EQ("int a; /* some\n" 12922 "comment */", 12923 format(" \t \t int\ta; /* some\n" 12924 " \t \t comment */", 12925 Tab)); 12926 EXPECT_EQ("f(\"\t\t\"); /* some\n" 12927 " comment */", 12928 format(" \t \t f(\"\t\t\"); /* some\n" 12929 " \t \t comment */", 12930 Tab)); 12931 EXPECT_EQ("{\n" 12932 "\t/*\n" 12933 "\t * Comment\n" 12934 "\t */\n" 12935 "\tint i;\n" 12936 "}", 12937 format("{\n" 12938 "\t/*\n" 12939 "\t * Comment\n" 12940 "\t */\n" 12941 "\t int i;\n" 12942 "}", 12943 Tab)); 12944 Tab.TabWidth = 2; 12945 Tab.IndentWidth = 2; 12946 EXPECT_EQ("{\n" 12947 "\t/* aaaa\n" 12948 "\t bbbb */\n" 12949 "}", 12950 format("{\n" 12951 "/* aaaa\n" 12952 " bbbb */\n" 12953 "}", 12954 Tab)); 12955 EXPECT_EQ("{\n" 12956 "\t/*\n" 12957 "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" 12958 "\t bbbbbbbbbbbbb\n" 12959 "\t*/\n" 12960 "}", 12961 format("{\n" 12962 "/*\n" 12963 " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" 12964 "*/\n" 12965 "}", 12966 Tab)); 12967 Tab.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 12968 Tab.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 12969 Tab.TabWidth = 4; 12970 Tab.IndentWidth = 4; 12971 verifyFormat("class Assign {\n" 12972 "\tvoid f() {\n" 12973 "\t\tint x = 123;\n" 12974 "\t\tint random = 4;\n" 12975 "\t\tstd::string alphabet =\n" 12976 "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n" 12977 "\t}\n" 12978 "};", 12979 Tab); 12980 Tab.AlignOperands = FormatStyle::OAS_Align; 12981 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb +\n" 12982 " cccccccccccccccccccc;", 12983 Tab); 12984 // no alignment 12985 verifyFormat("int aaaaaaaaaa =\n" 12986 "\tbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;", 12987 Tab); 12988 verifyFormat("return aaaaaaaaaaaaaaaa ? 111111111111111\n" 12989 " : bbbbbbbbbbbbbb ? 222222222222222\n" 12990 " : 333333333333333;", 12991 Tab); 12992 Tab.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 12993 Tab.AlignOperands = FormatStyle::OAS_AlignAfterOperator; 12994 verifyFormat("int aaaaaaaaaa = bbbbbbbbbbbbbbbbbbbb\n" 12995 " + cccccccccccccccccccc;", 12996 Tab); 12997 } 12998 12999 TEST_F(FormatTest, ZeroTabWidth) { 13000 FormatStyle Tab = getLLVMStyleWithColumns(42); 13001 Tab.IndentWidth = 8; 13002 Tab.UseTab = FormatStyle::UT_Never; 13003 Tab.TabWidth = 0; 13004 EXPECT_EQ("void a(){\n" 13005 " // line starts with '\t'\n" 13006 "};", 13007 format("void a(){\n" 13008 "\t// line starts with '\t'\n" 13009 "};", 13010 Tab)); 13011 13012 EXPECT_EQ("void a(){\n" 13013 " // line starts with '\t'\n" 13014 "};", 13015 format("void a(){\n" 13016 "\t\t// line starts with '\t'\n" 13017 "};", 13018 Tab)); 13019 13020 Tab.UseTab = FormatStyle::UT_ForIndentation; 13021 EXPECT_EQ("void a(){\n" 13022 " // line starts with '\t'\n" 13023 "};", 13024 format("void a(){\n" 13025 "\t// line starts with '\t'\n" 13026 "};", 13027 Tab)); 13028 13029 EXPECT_EQ("void a(){\n" 13030 " // line starts with '\t'\n" 13031 "};", 13032 format("void a(){\n" 13033 "\t\t// line starts with '\t'\n" 13034 "};", 13035 Tab)); 13036 13037 Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; 13038 EXPECT_EQ("void a(){\n" 13039 " // line starts with '\t'\n" 13040 "};", 13041 format("void a(){\n" 13042 "\t// line starts with '\t'\n" 13043 "};", 13044 Tab)); 13045 13046 EXPECT_EQ("void a(){\n" 13047 " // line starts with '\t'\n" 13048 "};", 13049 format("void a(){\n" 13050 "\t\t// line starts with '\t'\n" 13051 "};", 13052 Tab)); 13053 13054 Tab.UseTab = FormatStyle::UT_AlignWithSpaces; 13055 EXPECT_EQ("void a(){\n" 13056 " // line starts with '\t'\n" 13057 "};", 13058 format("void a(){\n" 13059 "\t// line starts with '\t'\n" 13060 "};", 13061 Tab)); 13062 13063 EXPECT_EQ("void a(){\n" 13064 " // line starts with '\t'\n" 13065 "};", 13066 format("void a(){\n" 13067 "\t\t// line starts with '\t'\n" 13068 "};", 13069 Tab)); 13070 13071 Tab.UseTab = FormatStyle::UT_Always; 13072 EXPECT_EQ("void a(){\n" 13073 "// line starts with '\t'\n" 13074 "};", 13075 format("void a(){\n" 13076 "\t// line starts with '\t'\n" 13077 "};", 13078 Tab)); 13079 13080 EXPECT_EQ("void a(){\n" 13081 "// line starts with '\t'\n" 13082 "};", 13083 format("void a(){\n" 13084 "\t\t// line starts with '\t'\n" 13085 "};", 13086 Tab)); 13087 } 13088 13089 TEST_F(FormatTest, CalculatesOriginalColumn) { 13090 EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13091 "q\"; /* some\n" 13092 " comment */", 13093 format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13094 "q\"; /* some\n" 13095 " comment */", 13096 getLLVMStyle())); 13097 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 13098 "/* some\n" 13099 " comment */", 13100 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" 13101 " /* some\n" 13102 " comment */", 13103 getLLVMStyle())); 13104 EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13105 "qqq\n" 13106 "/* some\n" 13107 " comment */", 13108 format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13109 "qqq\n" 13110 " /* some\n" 13111 " comment */", 13112 getLLVMStyle())); 13113 EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13114 "wwww; /* some\n" 13115 " comment */", 13116 format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" 13117 "wwww; /* some\n" 13118 " comment */", 13119 getLLVMStyle())); 13120 } 13121 13122 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { 13123 FormatStyle NoSpace = getLLVMStyle(); 13124 NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never; 13125 13126 verifyFormat("while(true)\n" 13127 " continue;", 13128 NoSpace); 13129 verifyFormat("for(;;)\n" 13130 " continue;", 13131 NoSpace); 13132 verifyFormat("if(true)\n" 13133 " f();\n" 13134 "else if(true)\n" 13135 " f();", 13136 NoSpace); 13137 verifyFormat("do {\n" 13138 " do_something();\n" 13139 "} while(something());", 13140 NoSpace); 13141 verifyFormat("switch(x) {\n" 13142 "default:\n" 13143 " break;\n" 13144 "}", 13145 NoSpace); 13146 verifyFormat("auto i = std::make_unique<int>(5);", NoSpace); 13147 verifyFormat("size_t x = sizeof(x);", NoSpace); 13148 verifyFormat("auto f(int x) -> decltype(x);", NoSpace); 13149 verifyFormat("auto f(int x) -> typeof(x);", NoSpace); 13150 verifyFormat("auto f(int x) -> _Atomic(x);", NoSpace); 13151 verifyFormat("auto f(int x) -> __underlying_type(x);", NoSpace); 13152 verifyFormat("int f(T x) noexcept(x.create());", NoSpace); 13153 verifyFormat("alignas(128) char a[128];", NoSpace); 13154 verifyFormat("size_t x = alignof(MyType);", NoSpace); 13155 verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace); 13156 verifyFormat("int f() throw(Deprecated);", NoSpace); 13157 verifyFormat("typedef void (*cb)(int);", NoSpace); 13158 verifyFormat("T A::operator()();", NoSpace); 13159 verifyFormat("X A::operator++(T);", NoSpace); 13160 verifyFormat("auto lambda = []() { return 0; };", NoSpace); 13161 13162 FormatStyle Space = getLLVMStyle(); 13163 Space.SpaceBeforeParens = FormatStyle::SBPO_Always; 13164 13165 verifyFormat("int f ();", Space); 13166 verifyFormat("void f (int a, T b) {\n" 13167 " while (true)\n" 13168 " continue;\n" 13169 "}", 13170 Space); 13171 verifyFormat("if (true)\n" 13172 " f ();\n" 13173 "else if (true)\n" 13174 " f ();", 13175 Space); 13176 verifyFormat("do {\n" 13177 " do_something ();\n" 13178 "} while (something ());", 13179 Space); 13180 verifyFormat("switch (x) {\n" 13181 "default:\n" 13182 " break;\n" 13183 "}", 13184 Space); 13185 verifyFormat("A::A () : a (1) {}", Space); 13186 verifyFormat("void f () __attribute__ ((asdf));", Space); 13187 verifyFormat("*(&a + 1);\n" 13188 "&((&a)[1]);\n" 13189 "a[(b + c) * d];\n" 13190 "(((a + 1) * 2) + 3) * 4;", 13191 Space); 13192 verifyFormat("#define A(x) x", Space); 13193 verifyFormat("#define A (x) x", Space); 13194 verifyFormat("#if defined(x)\n" 13195 "#endif", 13196 Space); 13197 verifyFormat("auto i = std::make_unique<int> (5);", Space); 13198 verifyFormat("size_t x = sizeof (x);", Space); 13199 verifyFormat("auto f (int x) -> decltype (x);", Space); 13200 verifyFormat("auto f (int x) -> typeof (x);", Space); 13201 verifyFormat("auto f (int x) -> _Atomic (x);", Space); 13202 verifyFormat("auto f (int x) -> __underlying_type (x);", Space); 13203 verifyFormat("int f (T x) noexcept (x.create ());", Space); 13204 verifyFormat("alignas (128) char a[128];", Space); 13205 verifyFormat("size_t x = alignof (MyType);", Space); 13206 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space); 13207 verifyFormat("int f () throw (Deprecated);", Space); 13208 verifyFormat("typedef void (*cb) (int);", Space); 13209 verifyFormat("T A::operator() ();", Space); 13210 verifyFormat("X A::operator++ (T);", Space); 13211 verifyFormat("auto lambda = [] () { return 0; };", Space); 13212 verifyFormat("int x = int (y);", Space); 13213 13214 FormatStyle SomeSpace = getLLVMStyle(); 13215 SomeSpace.SpaceBeforeParens = FormatStyle::SBPO_NonEmptyParentheses; 13216 13217 verifyFormat("[]() -> float {}", SomeSpace); 13218 verifyFormat("[] (auto foo) {}", SomeSpace); 13219 verifyFormat("[foo]() -> int {}", SomeSpace); 13220 verifyFormat("int f();", SomeSpace); 13221 verifyFormat("void f (int a, T b) {\n" 13222 " while (true)\n" 13223 " continue;\n" 13224 "}", 13225 SomeSpace); 13226 verifyFormat("if (true)\n" 13227 " f();\n" 13228 "else if (true)\n" 13229 " f();", 13230 SomeSpace); 13231 verifyFormat("do {\n" 13232 " do_something();\n" 13233 "} while (something());", 13234 SomeSpace); 13235 verifyFormat("switch (x) {\n" 13236 "default:\n" 13237 " break;\n" 13238 "}", 13239 SomeSpace); 13240 verifyFormat("A::A() : a (1) {}", SomeSpace); 13241 verifyFormat("void f() __attribute__ ((asdf));", SomeSpace); 13242 verifyFormat("*(&a + 1);\n" 13243 "&((&a)[1]);\n" 13244 "a[(b + c) * d];\n" 13245 "(((a + 1) * 2) + 3) * 4;", 13246 SomeSpace); 13247 verifyFormat("#define A(x) x", SomeSpace); 13248 verifyFormat("#define A (x) x", SomeSpace); 13249 verifyFormat("#if defined(x)\n" 13250 "#endif", 13251 SomeSpace); 13252 verifyFormat("auto i = std::make_unique<int> (5);", SomeSpace); 13253 verifyFormat("size_t x = sizeof (x);", SomeSpace); 13254 verifyFormat("auto f (int x) -> decltype (x);", SomeSpace); 13255 verifyFormat("auto f (int x) -> typeof (x);", SomeSpace); 13256 verifyFormat("auto f (int x) -> _Atomic (x);", SomeSpace); 13257 verifyFormat("auto f (int x) -> __underlying_type (x);", SomeSpace); 13258 verifyFormat("int f (T x) noexcept (x.create());", SomeSpace); 13259 verifyFormat("alignas (128) char a[128];", SomeSpace); 13260 verifyFormat("size_t x = alignof (MyType);", SomeSpace); 13261 verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", 13262 SomeSpace); 13263 verifyFormat("int f() throw (Deprecated);", SomeSpace); 13264 verifyFormat("typedef void (*cb) (int);", SomeSpace); 13265 verifyFormat("T A::operator()();", SomeSpace); 13266 verifyFormat("X A::operator++ (T);", SomeSpace); 13267 verifyFormat("int x = int (y);", SomeSpace); 13268 verifyFormat("auto lambda = []() { return 0; };", SomeSpace); 13269 } 13270 13271 TEST_F(FormatTest, SpaceAfterLogicalNot) { 13272 FormatStyle Spaces = getLLVMStyle(); 13273 Spaces.SpaceAfterLogicalNot = true; 13274 13275 verifyFormat("bool x = ! y", Spaces); 13276 verifyFormat("if (! isFailure())", Spaces); 13277 verifyFormat("if (! (a && b))", Spaces); 13278 verifyFormat("\"Error!\"", Spaces); 13279 verifyFormat("! ! x", Spaces); 13280 } 13281 13282 TEST_F(FormatTest, ConfigurableSpacesInParentheses) { 13283 FormatStyle Spaces = getLLVMStyle(); 13284 13285 Spaces.SpacesInParentheses = true; 13286 verifyFormat("do_something( ::globalVar );", Spaces); 13287 verifyFormat("call( x, y, z );", Spaces); 13288 verifyFormat("call();", Spaces); 13289 verifyFormat("std::function<void( int, int )> callback;", Spaces); 13290 verifyFormat("void inFunction() { std::function<void( int, int )> fct; }", 13291 Spaces); 13292 verifyFormat("while ( (bool)1 )\n" 13293 " continue;", 13294 Spaces); 13295 verifyFormat("for ( ;; )\n" 13296 " continue;", 13297 Spaces); 13298 verifyFormat("if ( true )\n" 13299 " f();\n" 13300 "else if ( true )\n" 13301 " f();", 13302 Spaces); 13303 verifyFormat("do {\n" 13304 " do_something( (int)i );\n" 13305 "} while ( something() );", 13306 Spaces); 13307 verifyFormat("switch ( x ) {\n" 13308 "default:\n" 13309 " break;\n" 13310 "}", 13311 Spaces); 13312 13313 Spaces.SpacesInParentheses = false; 13314 Spaces.SpacesInCStyleCastParentheses = true; 13315 verifyFormat("Type *A = ( Type * )P;", Spaces); 13316 verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces); 13317 verifyFormat("x = ( int32 )y;", Spaces); 13318 verifyFormat("int a = ( int )(2.0f);", Spaces); 13319 verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces); 13320 verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces); 13321 verifyFormat("#define x (( int )-1)", Spaces); 13322 13323 // Run the first set of tests again with: 13324 Spaces.SpacesInParentheses = false; 13325 Spaces.SpaceInEmptyParentheses = true; 13326 Spaces.SpacesInCStyleCastParentheses = true; 13327 verifyFormat("call(x, y, z);", Spaces); 13328 verifyFormat("call( );", Spaces); 13329 verifyFormat("std::function<void(int, int)> callback;", Spaces); 13330 verifyFormat("while (( bool )1)\n" 13331 " continue;", 13332 Spaces); 13333 verifyFormat("for (;;)\n" 13334 " continue;", 13335 Spaces); 13336 verifyFormat("if (true)\n" 13337 " f( );\n" 13338 "else if (true)\n" 13339 " f( );", 13340 Spaces); 13341 verifyFormat("do {\n" 13342 " do_something(( int )i);\n" 13343 "} while (something( ));", 13344 Spaces); 13345 verifyFormat("switch (x) {\n" 13346 "default:\n" 13347 " break;\n" 13348 "}", 13349 Spaces); 13350 13351 // Run the first set of tests again with: 13352 Spaces.SpaceAfterCStyleCast = true; 13353 verifyFormat("call(x, y, z);", Spaces); 13354 verifyFormat("call( );", Spaces); 13355 verifyFormat("std::function<void(int, int)> callback;", Spaces); 13356 verifyFormat("while (( bool ) 1)\n" 13357 " continue;", 13358 Spaces); 13359 verifyFormat("for (;;)\n" 13360 " continue;", 13361 Spaces); 13362 verifyFormat("if (true)\n" 13363 " f( );\n" 13364 "else if (true)\n" 13365 " f( );", 13366 Spaces); 13367 verifyFormat("do {\n" 13368 " do_something(( int ) i);\n" 13369 "} while (something( ));", 13370 Spaces); 13371 verifyFormat("switch (x) {\n" 13372 "default:\n" 13373 " break;\n" 13374 "}", 13375 Spaces); 13376 13377 // Run subset of tests again with: 13378 Spaces.SpacesInCStyleCastParentheses = false; 13379 Spaces.SpaceAfterCStyleCast = true; 13380 verifyFormat("while ((bool) 1)\n" 13381 " continue;", 13382 Spaces); 13383 verifyFormat("do {\n" 13384 " do_something((int) i);\n" 13385 "} while (something( ));", 13386 Spaces); 13387 13388 verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces); 13389 verifyFormat("size_t idx = (size_t) a;", Spaces); 13390 verifyFormat("size_t idx = (size_t) (a - 1);", Spaces); 13391 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); 13392 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); 13393 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); 13394 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); 13395 Spaces.ColumnLimit = 80; 13396 Spaces.IndentWidth = 4; 13397 Spaces.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 13398 verifyFormat("void foo( ) {\n" 13399 " size_t foo = (*(function))(\n" 13400 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " 13401 "BarrrrrrrrrrrrLong,\n" 13402 " FoooooooooLooooong);\n" 13403 "}", 13404 Spaces); 13405 Spaces.SpaceAfterCStyleCast = false; 13406 verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces); 13407 verifyFormat("size_t idx = (size_t)a;", Spaces); 13408 verifyFormat("size_t idx = (size_t)(a - 1);", Spaces); 13409 verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); 13410 verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); 13411 verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); 13412 verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); 13413 13414 verifyFormat("void foo( ) {\n" 13415 " size_t foo = (*(function))(\n" 13416 " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " 13417 "BarrrrrrrrrrrrLong,\n" 13418 " FoooooooooLooooong);\n" 13419 "}", 13420 Spaces); 13421 } 13422 13423 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { 13424 verifyFormat("int a[5];"); 13425 verifyFormat("a[3] += 42;"); 13426 13427 FormatStyle Spaces = getLLVMStyle(); 13428 Spaces.SpacesInSquareBrackets = true; 13429 // Not lambdas. 13430 verifyFormat("int a[ 5 ];", Spaces); 13431 verifyFormat("a[ 3 ] += 42;", Spaces); 13432 verifyFormat("constexpr char hello[]{\"hello\"};", Spaces); 13433 verifyFormat("double &operator[](int i) { return 0; }\n" 13434 "int i;", 13435 Spaces); 13436 verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces); 13437 verifyFormat("int i = a[ a ][ a ]->f();", Spaces); 13438 verifyFormat("int i = (*b)[ a ]->f();", Spaces); 13439 // Lambdas. 13440 verifyFormat("int c = []() -> int { return 2; }();\n", Spaces); 13441 verifyFormat("return [ i, args... ] {};", Spaces); 13442 verifyFormat("int foo = [ &bar ]() {};", Spaces); 13443 verifyFormat("int foo = [ = ]() {};", Spaces); 13444 verifyFormat("int foo = [ & ]() {};", Spaces); 13445 verifyFormat("int foo = [ =, &bar ]() {};", Spaces); 13446 verifyFormat("int foo = [ &bar, = ]() {};", Spaces); 13447 } 13448 13449 TEST_F(FormatTest, ConfigurableSpaceBeforeBrackets) { 13450 FormatStyle NoSpaceStyle = getLLVMStyle(); 13451 verifyFormat("int a[5];", NoSpaceStyle); 13452 verifyFormat("a[3] += 42;", NoSpaceStyle); 13453 13454 verifyFormat("int a[1];", NoSpaceStyle); 13455 verifyFormat("int 1 [a];", NoSpaceStyle); 13456 verifyFormat("int a[1][2];", NoSpaceStyle); 13457 verifyFormat("a[7] = 5;", NoSpaceStyle); 13458 verifyFormat("int a = (f())[23];", NoSpaceStyle); 13459 verifyFormat("f([] {})", NoSpaceStyle); 13460 13461 FormatStyle Space = getLLVMStyle(); 13462 Space.SpaceBeforeSquareBrackets = true; 13463 verifyFormat("int c = []() -> int { return 2; }();\n", Space); 13464 verifyFormat("return [i, args...] {};", Space); 13465 13466 verifyFormat("int a [5];", Space); 13467 verifyFormat("a [3] += 42;", Space); 13468 verifyFormat("constexpr char hello []{\"hello\"};", Space); 13469 verifyFormat("double &operator[](int i) { return 0; }\n" 13470 "int i;", 13471 Space); 13472 verifyFormat("std::unique_ptr<int []> foo() {}", Space); 13473 verifyFormat("int i = a [a][a]->f();", Space); 13474 verifyFormat("int i = (*b) [a]->f();", Space); 13475 13476 verifyFormat("int a [1];", Space); 13477 verifyFormat("int 1 [a];", Space); 13478 verifyFormat("int a [1][2];", Space); 13479 verifyFormat("a [7] = 5;", Space); 13480 verifyFormat("int a = (f()) [23];", Space); 13481 verifyFormat("f([] {})", Space); 13482 } 13483 13484 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) { 13485 verifyFormat("int a = 5;"); 13486 verifyFormat("a += 42;"); 13487 verifyFormat("a or_eq 8;"); 13488 13489 FormatStyle Spaces = getLLVMStyle(); 13490 Spaces.SpaceBeforeAssignmentOperators = false; 13491 verifyFormat("int a= 5;", Spaces); 13492 verifyFormat("a+= 42;", Spaces); 13493 verifyFormat("a or_eq 8;", Spaces); 13494 } 13495 13496 TEST_F(FormatTest, ConfigurableSpaceBeforeColon) { 13497 verifyFormat("class Foo : public Bar {};"); 13498 verifyFormat("Foo::Foo() : foo(1) {}"); 13499 verifyFormat("for (auto a : b) {\n}"); 13500 verifyFormat("int x = a ? b : c;"); 13501 verifyFormat("{\n" 13502 "label0:\n" 13503 " int x = 0;\n" 13504 "}"); 13505 verifyFormat("switch (x) {\n" 13506 "case 1:\n" 13507 "default:\n" 13508 "}"); 13509 verifyFormat("switch (allBraces) {\n" 13510 "case 1: {\n" 13511 " break;\n" 13512 "}\n" 13513 "case 2: {\n" 13514 " [[fallthrough]];\n" 13515 "}\n" 13516 "default: {\n" 13517 " break;\n" 13518 "}\n" 13519 "}"); 13520 13521 FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30); 13522 CtorInitializerStyle.SpaceBeforeCtorInitializerColon = false; 13523 verifyFormat("class Foo : public Bar {};", CtorInitializerStyle); 13524 verifyFormat("Foo::Foo(): foo(1) {}", CtorInitializerStyle); 13525 verifyFormat("for (auto a : b) {\n}", CtorInitializerStyle); 13526 verifyFormat("int x = a ? b : c;", CtorInitializerStyle); 13527 verifyFormat("{\n" 13528 "label1:\n" 13529 " int x = 0;\n" 13530 "}", 13531 CtorInitializerStyle); 13532 verifyFormat("switch (x) {\n" 13533 "case 1:\n" 13534 "default:\n" 13535 "}", 13536 CtorInitializerStyle); 13537 verifyFormat("switch (allBraces) {\n" 13538 "case 1: {\n" 13539 " break;\n" 13540 "}\n" 13541 "case 2: {\n" 13542 " [[fallthrough]];\n" 13543 "}\n" 13544 "default: {\n" 13545 " break;\n" 13546 "}\n" 13547 "}", 13548 CtorInitializerStyle); 13549 CtorInitializerStyle.BreakConstructorInitializers = 13550 FormatStyle::BCIS_AfterColon; 13551 verifyFormat("Fooooooooooo::Fooooooooooo():\n" 13552 " aaaaaaaaaaaaaaaa(1),\n" 13553 " bbbbbbbbbbbbbbbb(2) {}", 13554 CtorInitializerStyle); 13555 CtorInitializerStyle.BreakConstructorInitializers = 13556 FormatStyle::BCIS_BeforeComma; 13557 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 13558 " : aaaaaaaaaaaaaaaa(1)\n" 13559 " , bbbbbbbbbbbbbbbb(2) {}", 13560 CtorInitializerStyle); 13561 CtorInitializerStyle.BreakConstructorInitializers = 13562 FormatStyle::BCIS_BeforeColon; 13563 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 13564 " : aaaaaaaaaaaaaaaa(1),\n" 13565 " bbbbbbbbbbbbbbbb(2) {}", 13566 CtorInitializerStyle); 13567 CtorInitializerStyle.ConstructorInitializerIndentWidth = 0; 13568 verifyFormat("Fooooooooooo::Fooooooooooo()\n" 13569 ": aaaaaaaaaaaaaaaa(1),\n" 13570 " bbbbbbbbbbbbbbbb(2) {}", 13571 CtorInitializerStyle); 13572 13573 FormatStyle InheritanceStyle = getLLVMStyleWithColumns(30); 13574 InheritanceStyle.SpaceBeforeInheritanceColon = false; 13575 verifyFormat("class Foo: public Bar {};", InheritanceStyle); 13576 verifyFormat("Foo::Foo() : foo(1) {}", InheritanceStyle); 13577 verifyFormat("for (auto a : b) {\n}", InheritanceStyle); 13578 verifyFormat("int x = a ? b : c;", InheritanceStyle); 13579 verifyFormat("{\n" 13580 "label2:\n" 13581 " int x = 0;\n" 13582 "}", 13583 InheritanceStyle); 13584 verifyFormat("switch (x) {\n" 13585 "case 1:\n" 13586 "default:\n" 13587 "}", 13588 InheritanceStyle); 13589 verifyFormat("switch (allBraces) {\n" 13590 "case 1: {\n" 13591 " break;\n" 13592 "}\n" 13593 "case 2: {\n" 13594 " [[fallthrough]];\n" 13595 "}\n" 13596 "default: {\n" 13597 " break;\n" 13598 "}\n" 13599 "}", 13600 InheritanceStyle); 13601 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterComma; 13602 verifyFormat("class Foooooooooooooooooooooo\n" 13603 " : public aaaaaaaaaaaaaaaaaa,\n" 13604 " public bbbbbbbbbbbbbbbbbb {\n" 13605 "}", 13606 InheritanceStyle); 13607 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon; 13608 verifyFormat("class Foooooooooooooooooooooo:\n" 13609 " public aaaaaaaaaaaaaaaaaa,\n" 13610 " public bbbbbbbbbbbbbbbbbb {\n" 13611 "}", 13612 InheritanceStyle); 13613 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma; 13614 verifyFormat("class Foooooooooooooooooooooo\n" 13615 " : public aaaaaaaaaaaaaaaaaa\n" 13616 " , public bbbbbbbbbbbbbbbbbb {\n" 13617 "}", 13618 InheritanceStyle); 13619 InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 13620 verifyFormat("class Foooooooooooooooooooooo\n" 13621 " : public aaaaaaaaaaaaaaaaaa,\n" 13622 " public bbbbbbbbbbbbbbbbbb {\n" 13623 "}", 13624 InheritanceStyle); 13625 InheritanceStyle.ConstructorInitializerIndentWidth = 0; 13626 verifyFormat("class Foooooooooooooooooooooo\n" 13627 ": public aaaaaaaaaaaaaaaaaa,\n" 13628 " public bbbbbbbbbbbbbbbbbb {}", 13629 InheritanceStyle); 13630 13631 FormatStyle ForLoopStyle = getLLVMStyle(); 13632 ForLoopStyle.SpaceBeforeRangeBasedForLoopColon = false; 13633 verifyFormat("class Foo : public Bar {};", ForLoopStyle); 13634 verifyFormat("Foo::Foo() : foo(1) {}", ForLoopStyle); 13635 verifyFormat("for (auto a: b) {\n}", ForLoopStyle); 13636 verifyFormat("int x = a ? b : c;", ForLoopStyle); 13637 verifyFormat("{\n" 13638 "label2:\n" 13639 " int x = 0;\n" 13640 "}", 13641 ForLoopStyle); 13642 verifyFormat("switch (x) {\n" 13643 "case 1:\n" 13644 "default:\n" 13645 "}", 13646 ForLoopStyle); 13647 verifyFormat("switch (allBraces) {\n" 13648 "case 1: {\n" 13649 " break;\n" 13650 "}\n" 13651 "case 2: {\n" 13652 " [[fallthrough]];\n" 13653 "}\n" 13654 "default: {\n" 13655 " break;\n" 13656 "}\n" 13657 "}", 13658 ForLoopStyle); 13659 13660 FormatStyle CaseStyle = getLLVMStyle(); 13661 CaseStyle.SpaceBeforeCaseColon = true; 13662 verifyFormat("class Foo : public Bar {};", CaseStyle); 13663 verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle); 13664 verifyFormat("for (auto a : b) {\n}", CaseStyle); 13665 verifyFormat("int x = a ? b : c;", CaseStyle); 13666 verifyFormat("switch (x) {\n" 13667 "case 1 :\n" 13668 "default :\n" 13669 "}", 13670 CaseStyle); 13671 verifyFormat("switch (allBraces) {\n" 13672 "case 1 : {\n" 13673 " break;\n" 13674 "}\n" 13675 "case 2 : {\n" 13676 " [[fallthrough]];\n" 13677 "}\n" 13678 "default : {\n" 13679 " break;\n" 13680 "}\n" 13681 "}", 13682 CaseStyle); 13683 13684 FormatStyle NoSpaceStyle = getLLVMStyle(); 13685 EXPECT_EQ(NoSpaceStyle.SpaceBeforeCaseColon, false); 13686 NoSpaceStyle.SpaceBeforeCtorInitializerColon = false; 13687 NoSpaceStyle.SpaceBeforeInheritanceColon = false; 13688 NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 13689 verifyFormat("class Foo: public Bar {};", NoSpaceStyle); 13690 verifyFormat("Foo::Foo(): foo(1) {}", NoSpaceStyle); 13691 verifyFormat("for (auto a: b) {\n}", NoSpaceStyle); 13692 verifyFormat("int x = a ? b : c;", NoSpaceStyle); 13693 verifyFormat("{\n" 13694 "label3:\n" 13695 " int x = 0;\n" 13696 "}", 13697 NoSpaceStyle); 13698 verifyFormat("switch (x) {\n" 13699 "case 1:\n" 13700 "default:\n" 13701 "}", 13702 NoSpaceStyle); 13703 verifyFormat("switch (allBraces) {\n" 13704 "case 1: {\n" 13705 " break;\n" 13706 "}\n" 13707 "case 2: {\n" 13708 " [[fallthrough]];\n" 13709 "}\n" 13710 "default: {\n" 13711 " break;\n" 13712 "}\n" 13713 "}", 13714 NoSpaceStyle); 13715 13716 FormatStyle InvertedSpaceStyle = getLLVMStyle(); 13717 InvertedSpaceStyle.SpaceBeforeCaseColon = true; 13718 InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false; 13719 InvertedSpaceStyle.SpaceBeforeInheritanceColon = false; 13720 InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; 13721 verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle); 13722 verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle); 13723 verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle); 13724 verifyFormat("int x = a ? b : c;", InvertedSpaceStyle); 13725 verifyFormat("{\n" 13726 "label3:\n" 13727 " int x = 0;\n" 13728 "}", 13729 InvertedSpaceStyle); 13730 verifyFormat("switch (x) {\n" 13731 "case 1 :\n" 13732 "case 2 : {\n" 13733 " break;\n" 13734 "}\n" 13735 "default :\n" 13736 " break;\n" 13737 "}", 13738 InvertedSpaceStyle); 13739 verifyFormat("switch (allBraces) {\n" 13740 "case 1 : {\n" 13741 " break;\n" 13742 "}\n" 13743 "case 2 : {\n" 13744 " [[fallthrough]];\n" 13745 "}\n" 13746 "default : {\n" 13747 " break;\n" 13748 "}\n" 13749 "}", 13750 InvertedSpaceStyle); 13751 } 13752 13753 TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) { 13754 FormatStyle Style = getLLVMStyle(); 13755 13756 Style.PointerAlignment = FormatStyle::PAS_Left; 13757 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; 13758 verifyFormat("void* const* x = NULL;", Style); 13759 13760 #define verifyQualifierSpaces(Code, Pointers, Qualifiers) \ 13761 do { \ 13762 Style.PointerAlignment = FormatStyle::Pointers; \ 13763 Style.SpaceAroundPointerQualifiers = FormatStyle::Qualifiers; \ 13764 verifyFormat(Code, Style); \ 13765 } while (false) 13766 13767 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Default); 13768 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_Default); 13769 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Default); 13770 13771 verifyQualifierSpaces("void* const* x = NULL;", PAS_Left, SAPQ_Before); 13772 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Before); 13773 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Before); 13774 13775 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_After); 13776 verifyQualifierSpaces("void *const *x = NULL;", PAS_Right, SAPQ_After); 13777 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_After); 13778 13779 verifyQualifierSpaces("void* const * x = NULL;", PAS_Left, SAPQ_Both); 13780 verifyQualifierSpaces("void * const *x = NULL;", PAS_Right, SAPQ_Both); 13781 verifyQualifierSpaces("void * const * x = NULL;", PAS_Middle, SAPQ_Both); 13782 13783 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Default); 13784 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, 13785 SAPQ_Default); 13786 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 13787 SAPQ_Default); 13788 13789 verifyQualifierSpaces("Foo::operator void const*();", PAS_Left, SAPQ_Before); 13790 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, 13791 SAPQ_Before); 13792 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 13793 SAPQ_Before); 13794 13795 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_After); 13796 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_After); 13797 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, 13798 SAPQ_After); 13799 13800 verifyQualifierSpaces("Foo::operator void const *();", PAS_Left, SAPQ_Both); 13801 verifyQualifierSpaces("Foo::operator void const *();", PAS_Right, SAPQ_Both); 13802 verifyQualifierSpaces("Foo::operator void const *();", PAS_Middle, SAPQ_Both); 13803 13804 #undef verifyQualifierSpaces 13805 13806 FormatStyle Spaces = getLLVMStyle(); 13807 Spaces.AttributeMacros.push_back("qualified"); 13808 Spaces.PointerAlignment = FormatStyle::PAS_Right; 13809 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; 13810 verifyFormat("SomeType *volatile *a = NULL;", Spaces); 13811 verifyFormat("SomeType *__attribute__((attr)) *a = NULL;", Spaces); 13812 verifyFormat("std::vector<SomeType *const *> x;", Spaces); 13813 verifyFormat("std::vector<SomeType *qualified *> x;", Spaces); 13814 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 13815 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before; 13816 verifyFormat("SomeType * volatile *a = NULL;", Spaces); 13817 verifyFormat("SomeType * __attribute__((attr)) *a = NULL;", Spaces); 13818 verifyFormat("std::vector<SomeType * const *> x;", Spaces); 13819 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces); 13820 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 13821 13822 // Check that SAPQ_Before doesn't result in extra spaces for PAS_Left. 13823 Spaces.PointerAlignment = FormatStyle::PAS_Left; 13824 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Before; 13825 verifyFormat("SomeType* volatile* a = NULL;", Spaces); 13826 verifyFormat("SomeType* __attribute__((attr))* a = NULL;", Spaces); 13827 verifyFormat("std::vector<SomeType* const*> x;", Spaces); 13828 verifyFormat("std::vector<SomeType* qualified*> x;", Spaces); 13829 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 13830 // However, setting it to SAPQ_After should add spaces after __attribute, etc. 13831 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After; 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 13838 // PAS_Middle should not have any noticeable changes even for SAPQ_Both 13839 Spaces.PointerAlignment = FormatStyle::PAS_Middle; 13840 Spaces.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_After; 13841 verifyFormat("SomeType * volatile * a = NULL;", Spaces); 13842 verifyFormat("SomeType * __attribute__((attr)) * a = NULL;", Spaces); 13843 verifyFormat("std::vector<SomeType * const *> x;", Spaces); 13844 verifyFormat("std::vector<SomeType * qualified *> x;", Spaces); 13845 verifyFormat("std::vector<SomeVar * NotAQualifier> x;", Spaces); 13846 } 13847 13848 TEST_F(FormatTest, AlignConsecutiveMacros) { 13849 FormatStyle Style = getLLVMStyle(); 13850 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 13851 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 13852 Style.AlignConsecutiveMacros = FormatStyle::ACS_None; 13853 13854 verifyFormat("#define a 3\n" 13855 "#define bbbb 4\n" 13856 "#define ccc (5)", 13857 Style); 13858 13859 verifyFormat("#define f(x) (x * x)\n" 13860 "#define fff(x, y, z) (x * y + z)\n" 13861 "#define ffff(x, y) (x - y)", 13862 Style); 13863 13864 verifyFormat("#define foo(x, y) (x + y)\n" 13865 "#define bar (5, 6)(2 + 2)", 13866 Style); 13867 13868 verifyFormat("#define a 3\n" 13869 "#define bbbb 4\n" 13870 "#define ccc (5)\n" 13871 "#define f(x) (x * x)\n" 13872 "#define fff(x, y, z) (x * y + z)\n" 13873 "#define ffff(x, y) (x - y)", 13874 Style); 13875 13876 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 13877 verifyFormat("#define a 3\n" 13878 "#define bbbb 4\n" 13879 "#define ccc (5)", 13880 Style); 13881 13882 verifyFormat("#define f(x) (x * x)\n" 13883 "#define fff(x, y, z) (x * y + z)\n" 13884 "#define ffff(x, y) (x - y)", 13885 Style); 13886 13887 verifyFormat("#define foo(x, y) (x + y)\n" 13888 "#define bar (5, 6)(2 + 2)", 13889 Style); 13890 13891 verifyFormat("#define a 3\n" 13892 "#define bbbb 4\n" 13893 "#define ccc (5)\n" 13894 "#define f(x) (x * x)\n" 13895 "#define fff(x, y, z) (x * y + z)\n" 13896 "#define ffff(x, y) (x - y)", 13897 Style); 13898 13899 verifyFormat("#define a 5\n" 13900 "#define foo(x, y) (x + y)\n" 13901 "#define CCC (6)\n" 13902 "auto lambda = []() {\n" 13903 " auto ii = 0;\n" 13904 " float j = 0;\n" 13905 " return 0;\n" 13906 "};\n" 13907 "int i = 0;\n" 13908 "float i2 = 0;\n" 13909 "auto v = type{\n" 13910 " i = 1, //\n" 13911 " (i = 2), //\n" 13912 " i = 3 //\n" 13913 "};", 13914 Style); 13915 13916 Style.AlignConsecutiveMacros = FormatStyle::ACS_None; 13917 Style.ColumnLimit = 20; 13918 13919 verifyFormat("#define a \\\n" 13920 " \"aabbbbbbbbbbbb\"\n" 13921 "#define D \\\n" 13922 " \"aabbbbbbbbbbbb\" \\\n" 13923 " \"ccddeeeeeeeee\"\n" 13924 "#define B \\\n" 13925 " \"QQQQQQQQQQQQQ\" \\\n" 13926 " \"FFFFFFFFFFFFF\" \\\n" 13927 " \"LLLLLLLL\"\n", 13928 Style); 13929 13930 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 13931 verifyFormat("#define a \\\n" 13932 " \"aabbbbbbbbbbbb\"\n" 13933 "#define D \\\n" 13934 " \"aabbbbbbbbbbbb\" \\\n" 13935 " \"ccddeeeeeeeee\"\n" 13936 "#define B \\\n" 13937 " \"QQQQQQQQQQQQQ\" \\\n" 13938 " \"FFFFFFFFFFFFF\" \\\n" 13939 " \"LLLLLLLL\"\n", 13940 Style); 13941 13942 // Test across comments 13943 Style.MaxEmptyLinesToKeep = 10; 13944 Style.ReflowComments = false; 13945 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossComments; 13946 EXPECT_EQ("#define a 3\n" 13947 "// line comment\n" 13948 "#define bbbb 4\n" 13949 "#define ccc (5)", 13950 format("#define a 3\n" 13951 "// line comment\n" 13952 "#define bbbb 4\n" 13953 "#define ccc (5)", 13954 Style)); 13955 13956 EXPECT_EQ("#define a 3\n" 13957 "/* block comment */\n" 13958 "#define bbbb 4\n" 13959 "#define ccc (5)", 13960 format("#define a 3\n" 13961 "/* block comment */\n" 13962 "#define bbbb 4\n" 13963 "#define ccc (5)", 13964 Style)); 13965 13966 EXPECT_EQ("#define a 3\n" 13967 "/* multi-line *\n" 13968 " * block comment */\n" 13969 "#define bbbb 4\n" 13970 "#define ccc (5)", 13971 format("#define a 3\n" 13972 "/* multi-line *\n" 13973 " * block comment */\n" 13974 "#define bbbb 4\n" 13975 "#define ccc (5)", 13976 Style)); 13977 13978 EXPECT_EQ("#define a 3\n" 13979 "// multi-line line comment\n" 13980 "//\n" 13981 "#define bbbb 4\n" 13982 "#define ccc (5)", 13983 format("#define a 3\n" 13984 "// multi-line line comment\n" 13985 "//\n" 13986 "#define bbbb 4\n" 13987 "#define ccc (5)", 13988 Style)); 13989 13990 EXPECT_EQ("#define a 3\n" 13991 "// empty lines still break.\n" 13992 "\n" 13993 "#define bbbb 4\n" 13994 "#define ccc (5)", 13995 format("#define a 3\n" 13996 "// empty lines still break.\n" 13997 "\n" 13998 "#define bbbb 4\n" 13999 "#define ccc (5)", 14000 Style)); 14001 14002 // Test across empty lines 14003 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLines; 14004 EXPECT_EQ("#define a 3\n" 14005 "\n" 14006 "#define bbbb 4\n" 14007 "#define ccc (5)", 14008 format("#define a 3\n" 14009 "\n" 14010 "#define bbbb 4\n" 14011 "#define ccc (5)", 14012 Style)); 14013 14014 EXPECT_EQ("#define a 3\n" 14015 "\n" 14016 "\n" 14017 "\n" 14018 "#define bbbb 4\n" 14019 "#define ccc (5)", 14020 format("#define a 3\n" 14021 "\n" 14022 "\n" 14023 "\n" 14024 "#define bbbb 4\n" 14025 "#define ccc (5)", 14026 Style)); 14027 14028 EXPECT_EQ("#define a 3\n" 14029 "// comments should break alignment\n" 14030 "//\n" 14031 "#define bbbb 4\n" 14032 "#define ccc (5)", 14033 format("#define a 3\n" 14034 "// comments should break alignment\n" 14035 "//\n" 14036 "#define bbbb 4\n" 14037 "#define ccc (5)", 14038 Style)); 14039 14040 // Test across empty lines and comments 14041 Style.AlignConsecutiveMacros = FormatStyle::ACS_AcrossEmptyLinesAndComments; 14042 verifyFormat("#define a 3\n" 14043 "\n" 14044 "// line comment\n" 14045 "#define bbbb 4\n" 14046 "#define ccc (5)", 14047 Style); 14048 14049 EXPECT_EQ("#define a 3\n" 14050 "\n" 14051 "\n" 14052 "/* multi-line *\n" 14053 " * block comment */\n" 14054 "\n" 14055 "\n" 14056 "#define bbbb 4\n" 14057 "#define ccc (5)", 14058 format("#define a 3\n" 14059 "\n" 14060 "\n" 14061 "/* multi-line *\n" 14062 " * block comment */\n" 14063 "\n" 14064 "\n" 14065 "#define bbbb 4\n" 14066 "#define ccc (5)", 14067 Style)); 14068 14069 EXPECT_EQ("#define a 3\n" 14070 "\n" 14071 "\n" 14072 "/* multi-line *\n" 14073 " * block comment */\n" 14074 "\n" 14075 "\n" 14076 "#define bbbb 4\n" 14077 "#define ccc (5)", 14078 format("#define a 3\n" 14079 "\n" 14080 "\n" 14081 "/* multi-line *\n" 14082 " * block comment */\n" 14083 "\n" 14084 "\n" 14085 "#define bbbb 4\n" 14086 "#define ccc (5)", 14087 Style)); 14088 } 14089 14090 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) { 14091 FormatStyle Alignment = getLLVMStyle(); 14092 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 14093 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossEmptyLines; 14094 14095 Alignment.MaxEmptyLinesToKeep = 10; 14096 /* Test alignment across empty lines */ 14097 EXPECT_EQ("int a = 5;\n" 14098 "\n" 14099 "int oneTwoThree = 123;", 14100 format("int a = 5;\n" 14101 "\n" 14102 "int oneTwoThree= 123;", 14103 Alignment)); 14104 EXPECT_EQ("int a = 5;\n" 14105 "int one = 1;\n" 14106 "\n" 14107 "int oneTwoThree = 123;", 14108 format("int a = 5;\n" 14109 "int one = 1;\n" 14110 "\n" 14111 "int oneTwoThree = 123;", 14112 Alignment)); 14113 EXPECT_EQ("int a = 5;\n" 14114 "int one = 1;\n" 14115 "\n" 14116 "int oneTwoThree = 123;\n" 14117 "int oneTwo = 12;", 14118 format("int a = 5;\n" 14119 "int one = 1;\n" 14120 "\n" 14121 "int oneTwoThree = 123;\n" 14122 "int oneTwo = 12;", 14123 Alignment)); 14124 14125 /* Test across comments */ 14126 EXPECT_EQ("int a = 5;\n" 14127 "/* block comment */\n" 14128 "int oneTwoThree = 123;", 14129 format("int a = 5;\n" 14130 "/* block comment */\n" 14131 "int oneTwoThree=123;", 14132 Alignment)); 14133 14134 EXPECT_EQ("int a = 5;\n" 14135 "// line comment\n" 14136 "int oneTwoThree = 123;", 14137 format("int a = 5;\n" 14138 "// line comment\n" 14139 "int oneTwoThree=123;", 14140 Alignment)); 14141 14142 /* Test across comments and newlines */ 14143 EXPECT_EQ("int a = 5;\n" 14144 "\n" 14145 "/* block comment */\n" 14146 "int oneTwoThree = 123;", 14147 format("int a = 5;\n" 14148 "\n" 14149 "/* block comment */\n" 14150 "int oneTwoThree=123;", 14151 Alignment)); 14152 14153 EXPECT_EQ("int a = 5;\n" 14154 "\n" 14155 "// line comment\n" 14156 "int oneTwoThree = 123;", 14157 format("int a = 5;\n" 14158 "\n" 14159 "// line comment\n" 14160 "int oneTwoThree=123;", 14161 Alignment)); 14162 } 14163 14164 TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) { 14165 FormatStyle Alignment = getLLVMStyle(); 14166 Alignment.AlignConsecutiveDeclarations = 14167 FormatStyle::ACS_AcrossEmptyLinesAndComments; 14168 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 14169 14170 Alignment.MaxEmptyLinesToKeep = 10; 14171 /* Test alignment across empty lines */ 14172 EXPECT_EQ("int a = 5;\n" 14173 "\n" 14174 "float const oneTwoThree = 123;", 14175 format("int a = 5;\n" 14176 "\n" 14177 "float const oneTwoThree = 123;", 14178 Alignment)); 14179 EXPECT_EQ("int a = 5;\n" 14180 "float const one = 1;\n" 14181 "\n" 14182 "int oneTwoThree = 123;", 14183 format("int a = 5;\n" 14184 "float const one = 1;\n" 14185 "\n" 14186 "int oneTwoThree = 123;", 14187 Alignment)); 14188 14189 /* Test across comments */ 14190 EXPECT_EQ("float const a = 5;\n" 14191 "/* block comment */\n" 14192 "int oneTwoThree = 123;", 14193 format("float const a = 5;\n" 14194 "/* block comment */\n" 14195 "int oneTwoThree=123;", 14196 Alignment)); 14197 14198 EXPECT_EQ("float const a = 5;\n" 14199 "// line comment\n" 14200 "int oneTwoThree = 123;", 14201 format("float const a = 5;\n" 14202 "// line comment\n" 14203 "int oneTwoThree=123;", 14204 Alignment)); 14205 14206 /* Test across comments and newlines */ 14207 EXPECT_EQ("float const a = 5;\n" 14208 "\n" 14209 "/* block comment */\n" 14210 "int oneTwoThree = 123;", 14211 format("float const a = 5;\n" 14212 "\n" 14213 "/* block comment */\n" 14214 "int oneTwoThree=123;", 14215 Alignment)); 14216 14217 EXPECT_EQ("float const a = 5;\n" 14218 "\n" 14219 "// line comment\n" 14220 "int oneTwoThree = 123;", 14221 format("float const a = 5;\n" 14222 "\n" 14223 "// line comment\n" 14224 "int oneTwoThree=123;", 14225 Alignment)); 14226 } 14227 14228 TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) { 14229 FormatStyle Alignment = getLLVMStyle(); 14230 Alignment.AlignConsecutiveBitFields = 14231 FormatStyle::ACS_AcrossEmptyLinesAndComments; 14232 14233 Alignment.MaxEmptyLinesToKeep = 10; 14234 /* Test alignment across empty lines */ 14235 EXPECT_EQ("int a : 5;\n" 14236 "\n" 14237 "int longbitfield : 6;", 14238 format("int a : 5;\n" 14239 "\n" 14240 "int longbitfield : 6;", 14241 Alignment)); 14242 EXPECT_EQ("int a : 5;\n" 14243 "int one : 1;\n" 14244 "\n" 14245 "int longbitfield : 6;", 14246 format("int a : 5;\n" 14247 "int one : 1;\n" 14248 "\n" 14249 "int longbitfield : 6;", 14250 Alignment)); 14251 14252 /* Test across comments */ 14253 EXPECT_EQ("int a : 5;\n" 14254 "/* block comment */\n" 14255 "int longbitfield : 6;", 14256 format("int a : 5;\n" 14257 "/* block comment */\n" 14258 "int longbitfield : 6;", 14259 Alignment)); 14260 EXPECT_EQ("int a : 5;\n" 14261 "int one : 1;\n" 14262 "// line comment\n" 14263 "int longbitfield : 6;", 14264 format("int a : 5;\n" 14265 "int one : 1;\n" 14266 "// line comment\n" 14267 "int longbitfield : 6;", 14268 Alignment)); 14269 14270 /* Test across comments and newlines */ 14271 EXPECT_EQ("int a : 5;\n" 14272 "/* block comment */\n" 14273 "\n" 14274 "int longbitfield : 6;", 14275 format("int a : 5;\n" 14276 "/* block comment */\n" 14277 "\n" 14278 "int longbitfield : 6;", 14279 Alignment)); 14280 EXPECT_EQ("int a : 5;\n" 14281 "int one : 1;\n" 14282 "\n" 14283 "// line comment\n" 14284 "\n" 14285 "int longbitfield : 6;", 14286 format("int a : 5;\n" 14287 "int one : 1;\n" 14288 "\n" 14289 "// line comment \n" 14290 "\n" 14291 "int longbitfield : 6;", 14292 Alignment)); 14293 } 14294 14295 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) { 14296 FormatStyle Alignment = getLLVMStyle(); 14297 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 14298 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_AcrossComments; 14299 14300 Alignment.MaxEmptyLinesToKeep = 10; 14301 /* Test alignment across empty lines */ 14302 EXPECT_EQ("int a = 5;\n" 14303 "\n" 14304 "int oneTwoThree = 123;", 14305 format("int a = 5;\n" 14306 "\n" 14307 "int oneTwoThree= 123;", 14308 Alignment)); 14309 EXPECT_EQ("int a = 5;\n" 14310 "int one = 1;\n" 14311 "\n" 14312 "int oneTwoThree = 123;", 14313 format("int a = 5;\n" 14314 "int one = 1;\n" 14315 "\n" 14316 "int oneTwoThree = 123;", 14317 Alignment)); 14318 14319 /* Test across comments */ 14320 EXPECT_EQ("int a = 5;\n" 14321 "/* block comment */\n" 14322 "int oneTwoThree = 123;", 14323 format("int a = 5;\n" 14324 "/* block comment */\n" 14325 "int oneTwoThree=123;", 14326 Alignment)); 14327 14328 EXPECT_EQ("int a = 5;\n" 14329 "// line comment\n" 14330 "int oneTwoThree = 123;", 14331 format("int a = 5;\n" 14332 "// line comment\n" 14333 "int oneTwoThree=123;", 14334 Alignment)); 14335 14336 EXPECT_EQ("int a = 5;\n" 14337 "/*\n" 14338 " * multi-line block comment\n" 14339 " */\n" 14340 "int oneTwoThree = 123;", 14341 format("int a = 5;\n" 14342 "/*\n" 14343 " * multi-line block comment\n" 14344 " */\n" 14345 "int oneTwoThree=123;", 14346 Alignment)); 14347 14348 EXPECT_EQ("int a = 5;\n" 14349 "//\n" 14350 "// multi-line line comment\n" 14351 "//\n" 14352 "int oneTwoThree = 123;", 14353 format("int a = 5;\n" 14354 "//\n" 14355 "// multi-line line comment\n" 14356 "//\n" 14357 "int oneTwoThree=123;", 14358 Alignment)); 14359 14360 /* Test across comments and newlines */ 14361 EXPECT_EQ("int a = 5;\n" 14362 "\n" 14363 "/* block comment */\n" 14364 "int oneTwoThree = 123;", 14365 format("int a = 5;\n" 14366 "\n" 14367 "/* block comment */\n" 14368 "int oneTwoThree=123;", 14369 Alignment)); 14370 14371 EXPECT_EQ("int a = 5;\n" 14372 "\n" 14373 "// line comment\n" 14374 "int oneTwoThree = 123;", 14375 format("int a = 5;\n" 14376 "\n" 14377 "// line comment\n" 14378 "int oneTwoThree=123;", 14379 Alignment)); 14380 } 14381 14382 TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) { 14383 FormatStyle Alignment = getLLVMStyle(); 14384 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 14385 Alignment.AlignConsecutiveAssignments = 14386 FormatStyle::ACS_AcrossEmptyLinesAndComments; 14387 verifyFormat("int a = 5;\n" 14388 "int oneTwoThree = 123;", 14389 Alignment); 14390 verifyFormat("int a = method();\n" 14391 "int oneTwoThree = 133;", 14392 Alignment); 14393 verifyFormat("a &= 5;\n" 14394 "bcd *= 5;\n" 14395 "ghtyf += 5;\n" 14396 "dvfvdb -= 5;\n" 14397 "a /= 5;\n" 14398 "vdsvsv %= 5;\n" 14399 "sfdbddfbdfbb ^= 5;\n" 14400 "dvsdsv |= 5;\n" 14401 "int dsvvdvsdvvv = 123;", 14402 Alignment); 14403 verifyFormat("int i = 1, j = 10;\n" 14404 "something = 2000;", 14405 Alignment); 14406 verifyFormat("something = 2000;\n" 14407 "int i = 1, j = 10;\n", 14408 Alignment); 14409 verifyFormat("something = 2000;\n" 14410 "another = 911;\n" 14411 "int i = 1, j = 10;\n" 14412 "oneMore = 1;\n" 14413 "i = 2;", 14414 Alignment); 14415 verifyFormat("int a = 5;\n" 14416 "int one = 1;\n" 14417 "method();\n" 14418 "int oneTwoThree = 123;\n" 14419 "int oneTwo = 12;", 14420 Alignment); 14421 verifyFormat("int oneTwoThree = 123;\n" 14422 "int oneTwo = 12;\n" 14423 "method();\n", 14424 Alignment); 14425 verifyFormat("int oneTwoThree = 123; // comment\n" 14426 "int oneTwo = 12; // comment", 14427 Alignment); 14428 14429 // Bug 25167 14430 /* Uncomment when fixed 14431 verifyFormat("#if A\n" 14432 "#else\n" 14433 "int aaaaaaaa = 12;\n" 14434 "#endif\n" 14435 "#if B\n" 14436 "#else\n" 14437 "int a = 12;\n" 14438 "#endif\n", 14439 Alignment); 14440 verifyFormat("enum foo {\n" 14441 "#if A\n" 14442 "#else\n" 14443 " aaaaaaaa = 12;\n" 14444 "#endif\n" 14445 "#if B\n" 14446 "#else\n" 14447 " a = 12;\n" 14448 "#endif\n" 14449 "};\n", 14450 Alignment); 14451 */ 14452 14453 Alignment.MaxEmptyLinesToKeep = 10; 14454 /* Test alignment across empty lines */ 14455 EXPECT_EQ("int a = 5;\n" 14456 "\n" 14457 "int oneTwoThree = 123;", 14458 format("int a = 5;\n" 14459 "\n" 14460 "int oneTwoThree= 123;", 14461 Alignment)); 14462 EXPECT_EQ("int a = 5;\n" 14463 "int one = 1;\n" 14464 "\n" 14465 "int oneTwoThree = 123;", 14466 format("int a = 5;\n" 14467 "int one = 1;\n" 14468 "\n" 14469 "int oneTwoThree = 123;", 14470 Alignment)); 14471 EXPECT_EQ("int a = 5;\n" 14472 "int one = 1;\n" 14473 "\n" 14474 "int oneTwoThree = 123;\n" 14475 "int oneTwo = 12;", 14476 format("int a = 5;\n" 14477 "int one = 1;\n" 14478 "\n" 14479 "int oneTwoThree = 123;\n" 14480 "int oneTwo = 12;", 14481 Alignment)); 14482 14483 /* Test across comments */ 14484 EXPECT_EQ("int a = 5;\n" 14485 "/* block comment */\n" 14486 "int oneTwoThree = 123;", 14487 format("int a = 5;\n" 14488 "/* block comment */\n" 14489 "int oneTwoThree=123;", 14490 Alignment)); 14491 14492 EXPECT_EQ("int a = 5;\n" 14493 "// line comment\n" 14494 "int oneTwoThree = 123;", 14495 format("int a = 5;\n" 14496 "// line comment\n" 14497 "int oneTwoThree=123;", 14498 Alignment)); 14499 14500 /* Test across comments and newlines */ 14501 EXPECT_EQ("int a = 5;\n" 14502 "\n" 14503 "/* block comment */\n" 14504 "int oneTwoThree = 123;", 14505 format("int a = 5;\n" 14506 "\n" 14507 "/* block comment */\n" 14508 "int oneTwoThree=123;", 14509 Alignment)); 14510 14511 EXPECT_EQ("int a = 5;\n" 14512 "\n" 14513 "// line comment\n" 14514 "int oneTwoThree = 123;", 14515 format("int a = 5;\n" 14516 "\n" 14517 "// line comment\n" 14518 "int oneTwoThree=123;", 14519 Alignment)); 14520 14521 EXPECT_EQ("int a = 5;\n" 14522 "//\n" 14523 "// multi-line line comment\n" 14524 "//\n" 14525 "int oneTwoThree = 123;", 14526 format("int a = 5;\n" 14527 "//\n" 14528 "// multi-line line comment\n" 14529 "//\n" 14530 "int oneTwoThree=123;", 14531 Alignment)); 14532 14533 EXPECT_EQ("int a = 5;\n" 14534 "/*\n" 14535 " * multi-line block comment\n" 14536 " */\n" 14537 "int oneTwoThree = 123;", 14538 format("int a = 5;\n" 14539 "/*\n" 14540 " * multi-line block comment\n" 14541 " */\n" 14542 "int oneTwoThree=123;", 14543 Alignment)); 14544 14545 EXPECT_EQ("int a = 5;\n" 14546 "\n" 14547 "/* block comment */\n" 14548 "\n" 14549 "\n" 14550 "\n" 14551 "int oneTwoThree = 123;", 14552 format("int a = 5;\n" 14553 "\n" 14554 "/* block comment */\n" 14555 "\n" 14556 "\n" 14557 "\n" 14558 "int oneTwoThree=123;", 14559 Alignment)); 14560 14561 EXPECT_EQ("int a = 5;\n" 14562 "\n" 14563 "// line comment\n" 14564 "\n" 14565 "\n" 14566 "\n" 14567 "int oneTwoThree = 123;", 14568 format("int a = 5;\n" 14569 "\n" 14570 "// line comment\n" 14571 "\n" 14572 "\n" 14573 "\n" 14574 "int oneTwoThree=123;", 14575 Alignment)); 14576 14577 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 14578 verifyFormat("#define A \\\n" 14579 " int aaaa = 12; \\\n" 14580 " int b = 23; \\\n" 14581 " int ccc = 234; \\\n" 14582 " int dddddddddd = 2345;", 14583 Alignment); 14584 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 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_Right; 14592 verifyFormat("#define A " 14593 " \\\n" 14594 " int aaaa = 12; " 14595 " \\\n" 14596 " int b = 23; " 14597 " \\\n" 14598 " int ccc = 234; " 14599 " \\\n" 14600 " int dddddddddd = 2345;", 14601 Alignment); 14602 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 14603 "k = 4, int l = 5,\n" 14604 " int m = 6) {\n" 14605 " int j = 10;\n" 14606 " otherThing = 1;\n" 14607 "}", 14608 Alignment); 14609 verifyFormat("void SomeFunction(int parameter = 0) {\n" 14610 " int i = 1;\n" 14611 " int j = 2;\n" 14612 " int big = 10000;\n" 14613 "}", 14614 Alignment); 14615 verifyFormat("class C {\n" 14616 "public:\n" 14617 " int i = 1;\n" 14618 " virtual void f() = 0;\n" 14619 "};", 14620 Alignment); 14621 verifyFormat("int i = 1;\n" 14622 "if (SomeType t = getSomething()) {\n" 14623 "}\n" 14624 "int j = 2;\n" 14625 "int big = 10000;", 14626 Alignment); 14627 verifyFormat("int j = 7;\n" 14628 "for (int k = 0; k < N; ++k) {\n" 14629 "}\n" 14630 "int j = 2;\n" 14631 "int big = 10000;\n" 14632 "}", 14633 Alignment); 14634 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 14635 verifyFormat("int i = 1;\n" 14636 "LooooooooooongType loooooooooooooooooooooongVariable\n" 14637 " = someLooooooooooooooooongFunction();\n" 14638 "int j = 2;", 14639 Alignment); 14640 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 14641 verifyFormat("int i = 1;\n" 14642 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 14643 " someLooooooooooooooooongFunction();\n" 14644 "int j = 2;", 14645 Alignment); 14646 14647 verifyFormat("auto lambda = []() {\n" 14648 " auto i = 0;\n" 14649 " return 0;\n" 14650 "};\n" 14651 "int i = 0;\n" 14652 "auto v = type{\n" 14653 " i = 1, //\n" 14654 " (i = 2), //\n" 14655 " i = 3 //\n" 14656 "};", 14657 Alignment); 14658 14659 verifyFormat( 14660 "int i = 1;\n" 14661 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 14662 " loooooooooooooooooooooongParameterB);\n" 14663 "int j = 2;", 14664 Alignment); 14665 14666 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 14667 " typename B = very_long_type_name_1,\n" 14668 " typename T_2 = very_long_type_name_2>\n" 14669 "auto foo() {}\n", 14670 Alignment); 14671 verifyFormat("int a, b = 1;\n" 14672 "int c = 2;\n" 14673 "int dd = 3;\n", 14674 Alignment); 14675 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 14676 "float b[1][] = {{3.f}};\n", 14677 Alignment); 14678 verifyFormat("for (int i = 0; i < 1; i++)\n" 14679 " int x = 1;\n", 14680 Alignment); 14681 verifyFormat("for (i = 0; i < 1; i++)\n" 14682 " x = 1;\n" 14683 "y = 1;\n", 14684 Alignment); 14685 14686 Alignment.ReflowComments = true; 14687 Alignment.ColumnLimit = 50; 14688 EXPECT_EQ("int x = 0;\n" 14689 "int yy = 1; /// specificlennospace\n" 14690 "int zzz = 2;\n", 14691 format("int x = 0;\n" 14692 "int yy = 1; ///specificlennospace\n" 14693 "int zzz = 2;\n", 14694 Alignment)); 14695 } 14696 14697 TEST_F(FormatTest, AlignConsecutiveAssignments) { 14698 FormatStyle Alignment = getLLVMStyle(); 14699 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 14700 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 14701 verifyFormat("int a = 5;\n" 14702 "int oneTwoThree = 123;", 14703 Alignment); 14704 verifyFormat("int a = 5;\n" 14705 "int oneTwoThree = 123;", 14706 Alignment); 14707 14708 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 14709 verifyFormat("int a = 5;\n" 14710 "int oneTwoThree = 123;", 14711 Alignment); 14712 verifyFormat("int a = method();\n" 14713 "int oneTwoThree = 133;", 14714 Alignment); 14715 verifyFormat("a &= 5;\n" 14716 "bcd *= 5;\n" 14717 "ghtyf += 5;\n" 14718 "dvfvdb -= 5;\n" 14719 "a /= 5;\n" 14720 "vdsvsv %= 5;\n" 14721 "sfdbddfbdfbb ^= 5;\n" 14722 "dvsdsv |= 5;\n" 14723 "int dsvvdvsdvvv = 123;", 14724 Alignment); 14725 verifyFormat("int i = 1, j = 10;\n" 14726 "something = 2000;", 14727 Alignment); 14728 verifyFormat("something = 2000;\n" 14729 "int i = 1, j = 10;\n", 14730 Alignment); 14731 verifyFormat("something = 2000;\n" 14732 "another = 911;\n" 14733 "int i = 1, j = 10;\n" 14734 "oneMore = 1;\n" 14735 "i = 2;", 14736 Alignment); 14737 verifyFormat("int a = 5;\n" 14738 "int one = 1;\n" 14739 "method();\n" 14740 "int oneTwoThree = 123;\n" 14741 "int oneTwo = 12;", 14742 Alignment); 14743 verifyFormat("int oneTwoThree = 123;\n" 14744 "int oneTwo = 12;\n" 14745 "method();\n", 14746 Alignment); 14747 verifyFormat("int oneTwoThree = 123; // comment\n" 14748 "int oneTwo = 12; // comment", 14749 Alignment); 14750 14751 // Bug 25167 14752 /* Uncomment when fixed 14753 verifyFormat("#if A\n" 14754 "#else\n" 14755 "int aaaaaaaa = 12;\n" 14756 "#endif\n" 14757 "#if B\n" 14758 "#else\n" 14759 "int a = 12;\n" 14760 "#endif\n", 14761 Alignment); 14762 verifyFormat("enum foo {\n" 14763 "#if A\n" 14764 "#else\n" 14765 " aaaaaaaa = 12;\n" 14766 "#endif\n" 14767 "#if B\n" 14768 "#else\n" 14769 " a = 12;\n" 14770 "#endif\n" 14771 "};\n", 14772 Alignment); 14773 */ 14774 14775 EXPECT_EQ("int a = 5;\n" 14776 "\n" 14777 "int oneTwoThree = 123;", 14778 format("int a = 5;\n" 14779 "\n" 14780 "int oneTwoThree= 123;", 14781 Alignment)); 14782 EXPECT_EQ("int a = 5;\n" 14783 "int one = 1;\n" 14784 "\n" 14785 "int oneTwoThree = 123;", 14786 format("int a = 5;\n" 14787 "int one = 1;\n" 14788 "\n" 14789 "int oneTwoThree = 123;", 14790 Alignment)); 14791 EXPECT_EQ("int a = 5;\n" 14792 "int one = 1;\n" 14793 "\n" 14794 "int oneTwoThree = 123;\n" 14795 "int oneTwo = 12;", 14796 format("int a = 5;\n" 14797 "int one = 1;\n" 14798 "\n" 14799 "int oneTwoThree = 123;\n" 14800 "int oneTwo = 12;", 14801 Alignment)); 14802 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 14803 verifyFormat("#define A \\\n" 14804 " int aaaa = 12; \\\n" 14805 " int b = 23; \\\n" 14806 " int ccc = 234; \\\n" 14807 " int dddddddddd = 2345;", 14808 Alignment); 14809 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 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_Right; 14817 verifyFormat("#define A " 14818 " \\\n" 14819 " int aaaa = 12; " 14820 " \\\n" 14821 " int b = 23; " 14822 " \\\n" 14823 " int ccc = 234; " 14824 " \\\n" 14825 " int dddddddddd = 2345;", 14826 Alignment); 14827 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 14828 "k = 4, int l = 5,\n" 14829 " int m = 6) {\n" 14830 " int j = 10;\n" 14831 " otherThing = 1;\n" 14832 "}", 14833 Alignment); 14834 verifyFormat("void SomeFunction(int parameter = 0) {\n" 14835 " int i = 1;\n" 14836 " int j = 2;\n" 14837 " int big = 10000;\n" 14838 "}", 14839 Alignment); 14840 verifyFormat("class C {\n" 14841 "public:\n" 14842 " int i = 1;\n" 14843 " virtual void f() = 0;\n" 14844 "};", 14845 Alignment); 14846 verifyFormat("int i = 1;\n" 14847 "if (SomeType t = getSomething()) {\n" 14848 "}\n" 14849 "int j = 2;\n" 14850 "int big = 10000;", 14851 Alignment); 14852 verifyFormat("int j = 7;\n" 14853 "for (int k = 0; k < N; ++k) {\n" 14854 "}\n" 14855 "int j = 2;\n" 14856 "int big = 10000;\n" 14857 "}", 14858 Alignment); 14859 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 14860 verifyFormat("int i = 1;\n" 14861 "LooooooooooongType loooooooooooooooooooooongVariable\n" 14862 " = someLooooooooooooooooongFunction();\n" 14863 "int j = 2;", 14864 Alignment); 14865 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 14866 verifyFormat("int i = 1;\n" 14867 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 14868 " someLooooooooooooooooongFunction();\n" 14869 "int j = 2;", 14870 Alignment); 14871 14872 verifyFormat("auto lambda = []() {\n" 14873 " auto i = 0;\n" 14874 " return 0;\n" 14875 "};\n" 14876 "int i = 0;\n" 14877 "auto v = type{\n" 14878 " i = 1, //\n" 14879 " (i = 2), //\n" 14880 " i = 3 //\n" 14881 "};", 14882 Alignment); 14883 14884 verifyFormat( 14885 "int i = 1;\n" 14886 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 14887 " loooooooooooooooooooooongParameterB);\n" 14888 "int j = 2;", 14889 Alignment); 14890 14891 verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n" 14892 " typename B = very_long_type_name_1,\n" 14893 " typename T_2 = very_long_type_name_2>\n" 14894 "auto foo() {}\n", 14895 Alignment); 14896 verifyFormat("int a, b = 1;\n" 14897 "int c = 2;\n" 14898 "int dd = 3;\n", 14899 Alignment); 14900 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 14901 "float b[1][] = {{3.f}};\n", 14902 Alignment); 14903 verifyFormat("for (int i = 0; i < 1; i++)\n" 14904 " int x = 1;\n", 14905 Alignment); 14906 verifyFormat("for (i = 0; i < 1; i++)\n" 14907 " x = 1;\n" 14908 "y = 1;\n", 14909 Alignment); 14910 14911 Alignment.ReflowComments = true; 14912 Alignment.ColumnLimit = 50; 14913 EXPECT_EQ("int x = 0;\n" 14914 "int yy = 1; /// specificlennospace\n" 14915 "int zzz = 2;\n", 14916 format("int x = 0;\n" 14917 "int yy = 1; ///specificlennospace\n" 14918 "int zzz = 2;\n", 14919 Alignment)); 14920 } 14921 14922 TEST_F(FormatTest, AlignConsecutiveBitFields) { 14923 FormatStyle Alignment = getLLVMStyle(); 14924 Alignment.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive; 14925 verifyFormat("int const a : 5;\n" 14926 "int oneTwoThree : 23;", 14927 Alignment); 14928 14929 // Initializers are allowed starting with c++2a 14930 verifyFormat("int const a : 5 = 1;\n" 14931 "int oneTwoThree : 23 = 0;", 14932 Alignment); 14933 14934 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 14935 verifyFormat("int const a : 5;\n" 14936 "int oneTwoThree : 23;", 14937 Alignment); 14938 14939 verifyFormat("int const a : 5; // comment\n" 14940 "int oneTwoThree : 23; // comment", 14941 Alignment); 14942 14943 verifyFormat("int const a : 5 = 1;\n" 14944 "int oneTwoThree : 23 = 0;", 14945 Alignment); 14946 14947 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 14948 verifyFormat("int const a : 5 = 1;\n" 14949 "int oneTwoThree : 23 = 0;", 14950 Alignment); 14951 verifyFormat("int const a : 5 = {1};\n" 14952 "int oneTwoThree : 23 = 0;", 14953 Alignment); 14954 14955 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_None; 14956 verifyFormat("int const a :5;\n" 14957 "int oneTwoThree:23;", 14958 Alignment); 14959 14960 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_Before; 14961 verifyFormat("int const a :5;\n" 14962 "int oneTwoThree :23;", 14963 Alignment); 14964 14965 Alignment.BitFieldColonSpacing = FormatStyle::BFCS_After; 14966 verifyFormat("int const a : 5;\n" 14967 "int oneTwoThree: 23;", 14968 Alignment); 14969 14970 // Known limitations: ':' is only recognized as a bitfield colon when 14971 // followed by a number. 14972 /* 14973 verifyFormat("int oneTwoThree : SOME_CONSTANT;\n" 14974 "int a : 5;", 14975 Alignment); 14976 */ 14977 } 14978 14979 TEST_F(FormatTest, AlignConsecutiveDeclarations) { 14980 FormatStyle Alignment = getLLVMStyle(); 14981 Alignment.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 14982 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_None; 14983 Alignment.PointerAlignment = FormatStyle::PAS_Right; 14984 verifyFormat("float const a = 5;\n" 14985 "int oneTwoThree = 123;", 14986 Alignment); 14987 verifyFormat("int a = 5;\n" 14988 "float const oneTwoThree = 123;", 14989 Alignment); 14990 14991 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 14992 verifyFormat("float const a = 5;\n" 14993 "int oneTwoThree = 123;", 14994 Alignment); 14995 verifyFormat("int a = method();\n" 14996 "float const oneTwoThree = 133;", 14997 Alignment); 14998 verifyFormat("int i = 1, j = 10;\n" 14999 "something = 2000;", 15000 Alignment); 15001 verifyFormat("something = 2000;\n" 15002 "int i = 1, j = 10;\n", 15003 Alignment); 15004 verifyFormat("float something = 2000;\n" 15005 "double another = 911;\n" 15006 "int i = 1, j = 10;\n" 15007 "const int *oneMore = 1;\n" 15008 "unsigned i = 2;", 15009 Alignment); 15010 verifyFormat("float a = 5;\n" 15011 "int one = 1;\n" 15012 "method();\n" 15013 "const double oneTwoThree = 123;\n" 15014 "const unsigned int oneTwo = 12;", 15015 Alignment); 15016 verifyFormat("int oneTwoThree{0}; // comment\n" 15017 "unsigned oneTwo; // comment", 15018 Alignment); 15019 verifyFormat("unsigned int *a;\n" 15020 "int *b;\n" 15021 "unsigned int Const *c;\n" 15022 "unsigned int const *d;\n" 15023 "unsigned int Const &e;\n" 15024 "unsigned int const &f;", 15025 Alignment); 15026 verifyFormat("Const unsigned int *c;\n" 15027 "const unsigned int *d;\n" 15028 "Const unsigned int &e;\n" 15029 "const unsigned int &f;\n" 15030 "const unsigned g;\n" 15031 "Const unsigned h;", 15032 Alignment); 15033 EXPECT_EQ("float const a = 5;\n" 15034 "\n" 15035 "int oneTwoThree = 123;", 15036 format("float const a = 5;\n" 15037 "\n" 15038 "int oneTwoThree= 123;", 15039 Alignment)); 15040 EXPECT_EQ("float a = 5;\n" 15041 "int one = 1;\n" 15042 "\n" 15043 "unsigned oneTwoThree = 123;", 15044 format("float a = 5;\n" 15045 "int one = 1;\n" 15046 "\n" 15047 "unsigned oneTwoThree = 123;", 15048 Alignment)); 15049 EXPECT_EQ("float a = 5;\n" 15050 "int one = 1;\n" 15051 "\n" 15052 "unsigned oneTwoThree = 123;\n" 15053 "int oneTwo = 12;", 15054 format("float a = 5;\n" 15055 "int one = 1;\n" 15056 "\n" 15057 "unsigned oneTwoThree = 123;\n" 15058 "int oneTwo = 12;", 15059 Alignment)); 15060 // Function prototype alignment 15061 verifyFormat("int a();\n" 15062 "double b();", 15063 Alignment); 15064 verifyFormat("int a(int x);\n" 15065 "double b();", 15066 Alignment); 15067 unsigned OldColumnLimit = Alignment.ColumnLimit; 15068 // We need to set ColumnLimit to zero, in order to stress nested alignments, 15069 // otherwise the function parameters will be re-flowed onto a single line. 15070 Alignment.ColumnLimit = 0; 15071 EXPECT_EQ("int a(int x,\n" 15072 " float y);\n" 15073 "double b(int x,\n" 15074 " double y);", 15075 format("int a(int x,\n" 15076 " float y);\n" 15077 "double b(int x,\n" 15078 " double y);", 15079 Alignment)); 15080 // This ensures that function parameters of function declarations are 15081 // correctly indented when their owning functions are indented. 15082 // The failure case here is for 'double y' to not be indented enough. 15083 EXPECT_EQ("double a(int x);\n" 15084 "int b(int y,\n" 15085 " double z);", 15086 format("double a(int x);\n" 15087 "int b(int y,\n" 15088 " double z);", 15089 Alignment)); 15090 // Set ColumnLimit low so that we induce wrapping immediately after 15091 // the function name and opening paren. 15092 Alignment.ColumnLimit = 13; 15093 verifyFormat("int function(\n" 15094 " int x,\n" 15095 " bool y);", 15096 Alignment); 15097 Alignment.ColumnLimit = OldColumnLimit; 15098 // Ensure function pointers don't screw up recursive alignment 15099 verifyFormat("int a(int x, void (*fp)(int y));\n" 15100 "double b();", 15101 Alignment); 15102 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 15103 // Ensure recursive alignment is broken by function braces, so that the 15104 // "a = 1" does not align with subsequent assignments inside the function 15105 // body. 15106 verifyFormat("int func(int a = 1) {\n" 15107 " int b = 2;\n" 15108 " int cc = 3;\n" 15109 "}", 15110 Alignment); 15111 verifyFormat("float something = 2000;\n" 15112 "double another = 911;\n" 15113 "int i = 1, j = 10;\n" 15114 "const int *oneMore = 1;\n" 15115 "unsigned i = 2;", 15116 Alignment); 15117 verifyFormat("int oneTwoThree = {0}; // comment\n" 15118 "unsigned oneTwo = 0; // comment", 15119 Alignment); 15120 // Make sure that scope is correctly tracked, in the absence of braces 15121 verifyFormat("for (int i = 0; i < n; i++)\n" 15122 " j = i;\n" 15123 "double x = 1;\n", 15124 Alignment); 15125 verifyFormat("if (int i = 0)\n" 15126 " j = i;\n" 15127 "double x = 1;\n", 15128 Alignment); 15129 // Ensure operator[] and operator() are comprehended 15130 verifyFormat("struct test {\n" 15131 " long long int foo();\n" 15132 " int operator[](int a);\n" 15133 " double bar();\n" 15134 "};\n", 15135 Alignment); 15136 verifyFormat("struct test {\n" 15137 " long long int foo();\n" 15138 " int operator()(int a);\n" 15139 " double bar();\n" 15140 "};\n", 15141 Alignment); 15142 15143 // PAS_Right 15144 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 15145 " int const i = 1;\n" 15146 " int *j = 2;\n" 15147 " int big = 10000;\n" 15148 "\n" 15149 " unsigned oneTwoThree = 123;\n" 15150 " int oneTwo = 12;\n" 15151 " method();\n" 15152 " float k = 2;\n" 15153 " int ll = 10000;\n" 15154 "}", 15155 format("void SomeFunction(int parameter= 0) {\n" 15156 " int const i= 1;\n" 15157 " int *j=2;\n" 15158 " int big = 10000;\n" 15159 "\n" 15160 "unsigned oneTwoThree =123;\n" 15161 "int oneTwo = 12;\n" 15162 " method();\n" 15163 "float k= 2;\n" 15164 "int ll=10000;\n" 15165 "}", 15166 Alignment)); 15167 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 15168 " int const i = 1;\n" 15169 " int **j = 2, ***k;\n" 15170 " int &k = i;\n" 15171 " int &&l = i + j;\n" 15172 " int big = 10000;\n" 15173 "\n" 15174 " unsigned oneTwoThree = 123;\n" 15175 " int oneTwo = 12;\n" 15176 " method();\n" 15177 " float k = 2;\n" 15178 " int ll = 10000;\n" 15179 "}", 15180 format("void SomeFunction(int parameter= 0) {\n" 15181 " int const i= 1;\n" 15182 " int **j=2,***k;\n" 15183 "int &k=i;\n" 15184 "int &&l=i+j;\n" 15185 " int big = 10000;\n" 15186 "\n" 15187 "unsigned oneTwoThree =123;\n" 15188 "int oneTwo = 12;\n" 15189 " method();\n" 15190 "float k= 2;\n" 15191 "int ll=10000;\n" 15192 "}", 15193 Alignment)); 15194 // variables are aligned at their name, pointers are at the right most 15195 // position 15196 verifyFormat("int *a;\n" 15197 "int **b;\n" 15198 "int ***c;\n" 15199 "int foobar;\n", 15200 Alignment); 15201 15202 // PAS_Left 15203 FormatStyle AlignmentLeft = Alignment; 15204 AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left; 15205 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 15206 " int const i = 1;\n" 15207 " int* j = 2;\n" 15208 " int big = 10000;\n" 15209 "\n" 15210 " unsigned oneTwoThree = 123;\n" 15211 " int oneTwo = 12;\n" 15212 " method();\n" 15213 " float k = 2;\n" 15214 " int ll = 10000;\n" 15215 "}", 15216 format("void SomeFunction(int parameter= 0) {\n" 15217 " int const i= 1;\n" 15218 " int *j=2;\n" 15219 " int big = 10000;\n" 15220 "\n" 15221 "unsigned oneTwoThree =123;\n" 15222 "int oneTwo = 12;\n" 15223 " method();\n" 15224 "float k= 2;\n" 15225 "int ll=10000;\n" 15226 "}", 15227 AlignmentLeft)); 15228 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 15229 " int const i = 1;\n" 15230 " int** j = 2;\n" 15231 " int& k = i;\n" 15232 " int&& l = i + j;\n" 15233 " int big = 10000;\n" 15234 "\n" 15235 " unsigned oneTwoThree = 123;\n" 15236 " int oneTwo = 12;\n" 15237 " method();\n" 15238 " float k = 2;\n" 15239 " int ll = 10000;\n" 15240 "}", 15241 format("void SomeFunction(int parameter= 0) {\n" 15242 " int const i= 1;\n" 15243 " int **j=2;\n" 15244 "int &k=i;\n" 15245 "int &&l=i+j;\n" 15246 " int big = 10000;\n" 15247 "\n" 15248 "unsigned oneTwoThree =123;\n" 15249 "int oneTwo = 12;\n" 15250 " method();\n" 15251 "float k= 2;\n" 15252 "int ll=10000;\n" 15253 "}", 15254 AlignmentLeft)); 15255 // variables are aligned at their name, pointers are at the left most position 15256 verifyFormat("int* a;\n" 15257 "int** b;\n" 15258 "int*** c;\n" 15259 "int foobar;\n", 15260 AlignmentLeft); 15261 15262 // PAS_Middle 15263 FormatStyle AlignmentMiddle = Alignment; 15264 AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle; 15265 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 15266 " int const i = 1;\n" 15267 " int * j = 2;\n" 15268 " int big = 10000;\n" 15269 "\n" 15270 " unsigned oneTwoThree = 123;\n" 15271 " int oneTwo = 12;\n" 15272 " method();\n" 15273 " float k = 2;\n" 15274 " int ll = 10000;\n" 15275 "}", 15276 format("void SomeFunction(int parameter= 0) {\n" 15277 " int const i= 1;\n" 15278 " int *j=2;\n" 15279 " int big = 10000;\n" 15280 "\n" 15281 "unsigned oneTwoThree =123;\n" 15282 "int oneTwo = 12;\n" 15283 " method();\n" 15284 "float k= 2;\n" 15285 "int ll=10000;\n" 15286 "}", 15287 AlignmentMiddle)); 15288 EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" 15289 " int const i = 1;\n" 15290 " int ** j = 2, ***k;\n" 15291 " int & k = i;\n" 15292 " int && l = i + j;\n" 15293 " int big = 10000;\n" 15294 "\n" 15295 " unsigned oneTwoThree = 123;\n" 15296 " int oneTwo = 12;\n" 15297 " method();\n" 15298 " float k = 2;\n" 15299 " int ll = 10000;\n" 15300 "}", 15301 format("void SomeFunction(int parameter= 0) {\n" 15302 " int const i= 1;\n" 15303 " int **j=2,***k;\n" 15304 "int &k=i;\n" 15305 "int &&l=i+j;\n" 15306 " int big = 10000;\n" 15307 "\n" 15308 "unsigned oneTwoThree =123;\n" 15309 "int oneTwo = 12;\n" 15310 " method();\n" 15311 "float k= 2;\n" 15312 "int ll=10000;\n" 15313 "}", 15314 AlignmentMiddle)); 15315 // variables are aligned at their name, pointers are in the middle 15316 verifyFormat("int * a;\n" 15317 "int * b;\n" 15318 "int *** c;\n" 15319 "int foobar;\n", 15320 AlignmentMiddle); 15321 15322 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 15323 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; 15324 verifyFormat("#define A \\\n" 15325 " int aaaa = 12; \\\n" 15326 " float b = 23; \\\n" 15327 " const int ccc = 234; \\\n" 15328 " unsigned dddddddddd = 2345;", 15329 Alignment); 15330 Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left; 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_Right; 15338 Alignment.ColumnLimit = 30; 15339 verifyFormat("#define A \\\n" 15340 " int aaaa = 12; \\\n" 15341 " float b = 23; \\\n" 15342 " const int ccc = 234; \\\n" 15343 " int dddddddddd = 2345;", 15344 Alignment); 15345 Alignment.ColumnLimit = 80; 15346 verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int " 15347 "k = 4, int l = 5,\n" 15348 " int m = 6) {\n" 15349 " const int j = 10;\n" 15350 " otherThing = 1;\n" 15351 "}", 15352 Alignment); 15353 verifyFormat("void SomeFunction(int parameter = 0) {\n" 15354 " int const i = 1;\n" 15355 " int *j = 2;\n" 15356 " int big = 10000;\n" 15357 "}", 15358 Alignment); 15359 verifyFormat("class C {\n" 15360 "public:\n" 15361 " int i = 1;\n" 15362 " virtual void f() = 0;\n" 15363 "};", 15364 Alignment); 15365 verifyFormat("float i = 1;\n" 15366 "if (SomeType t = getSomething()) {\n" 15367 "}\n" 15368 "const unsigned j = 2;\n" 15369 "int big = 10000;", 15370 Alignment); 15371 verifyFormat("float j = 7;\n" 15372 "for (int k = 0; k < N; ++k) {\n" 15373 "}\n" 15374 "unsigned j = 2;\n" 15375 "int big = 10000;\n" 15376 "}", 15377 Alignment); 15378 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 15379 verifyFormat("float i = 1;\n" 15380 "LooooooooooongType loooooooooooooooooooooongVariable\n" 15381 " = someLooooooooooooooooongFunction();\n" 15382 "int j = 2;", 15383 Alignment); 15384 Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; 15385 verifyFormat("int i = 1;\n" 15386 "LooooooooooongType loooooooooooooooooooooongVariable =\n" 15387 " someLooooooooooooooooongFunction();\n" 15388 "int j = 2;", 15389 Alignment); 15390 15391 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 15392 verifyFormat("auto lambda = []() {\n" 15393 " auto ii = 0;\n" 15394 " float j = 0;\n" 15395 " return 0;\n" 15396 "};\n" 15397 "int i = 0;\n" 15398 "float i2 = 0;\n" 15399 "auto v = type{\n" 15400 " i = 1, //\n" 15401 " (i = 2), //\n" 15402 " i = 3 //\n" 15403 "};", 15404 Alignment); 15405 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 15406 15407 verifyFormat( 15408 "int i = 1;\n" 15409 "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n" 15410 " loooooooooooooooooooooongParameterB);\n" 15411 "int j = 2;", 15412 Alignment); 15413 15414 // Test interactions with ColumnLimit and AlignConsecutiveAssignments: 15415 // We expect declarations and assignments to align, as long as it doesn't 15416 // exceed the column limit, starting a new alignment sequence whenever it 15417 // happens. 15418 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 15419 Alignment.ColumnLimit = 30; 15420 verifyFormat("float ii = 1;\n" 15421 "unsigned j = 2;\n" 15422 "int someVerylongVariable = 1;\n" 15423 "AnotherLongType ll = 123456;\n" 15424 "VeryVeryLongType k = 2;\n" 15425 "int myvar = 1;", 15426 Alignment); 15427 Alignment.ColumnLimit = 80; 15428 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 15429 15430 verifyFormat( 15431 "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n" 15432 " typename LongType, typename B>\n" 15433 "auto foo() {}\n", 15434 Alignment); 15435 verifyFormat("float a, b = 1;\n" 15436 "int c = 2;\n" 15437 "int dd = 3;\n", 15438 Alignment); 15439 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 15440 "float b[1][] = {{3.f}};\n", 15441 Alignment); 15442 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 15443 verifyFormat("float a, b = 1;\n" 15444 "int c = 2;\n" 15445 "int dd = 3;\n", 15446 Alignment); 15447 verifyFormat("int aa = ((1 > 2) ? 3 : 4);\n" 15448 "float b[1][] = {{3.f}};\n", 15449 Alignment); 15450 Alignment.AlignConsecutiveAssignments = FormatStyle::ACS_None; 15451 15452 Alignment.ColumnLimit = 30; 15453 Alignment.BinPackParameters = false; 15454 verifyFormat("void foo(float a,\n" 15455 " float b,\n" 15456 " int c,\n" 15457 " uint32_t *d) {\n" 15458 " int *e = 0;\n" 15459 " float f = 0;\n" 15460 " double g = 0;\n" 15461 "}\n" 15462 "void bar(ino_t a,\n" 15463 " int b,\n" 15464 " uint32_t *c,\n" 15465 " bool d) {}\n", 15466 Alignment); 15467 Alignment.BinPackParameters = true; 15468 Alignment.ColumnLimit = 80; 15469 15470 // Bug 33507 15471 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 15472 verifyFormat( 15473 "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n" 15474 " static const Version verVs2017;\n" 15475 " return true;\n" 15476 "});\n", 15477 Alignment); 15478 Alignment.PointerAlignment = FormatStyle::PAS_Right; 15479 15480 // See llvm.org/PR35641 15481 Alignment.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 15482 verifyFormat("int func() { //\n" 15483 " int b;\n" 15484 " unsigned c;\n" 15485 "}", 15486 Alignment); 15487 15488 // See PR37175 15489 FormatStyle Style = getMozillaStyle(); 15490 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 15491 EXPECT_EQ("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n" 15492 "foo(int a);", 15493 format("DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style)); 15494 15495 Alignment.PointerAlignment = FormatStyle::PAS_Left; 15496 verifyFormat("unsigned int* a;\n" 15497 "int* b;\n" 15498 "unsigned int Const* c;\n" 15499 "unsigned int const* d;\n" 15500 "unsigned int Const& e;\n" 15501 "unsigned int const& f;", 15502 Alignment); 15503 verifyFormat("Const unsigned int* c;\n" 15504 "const unsigned int* d;\n" 15505 "Const unsigned int& e;\n" 15506 "const unsigned int& f;\n" 15507 "const unsigned g;\n" 15508 "Const unsigned h;", 15509 Alignment); 15510 15511 Alignment.PointerAlignment = FormatStyle::PAS_Middle; 15512 verifyFormat("unsigned int * a;\n" 15513 "int * b;\n" 15514 "unsigned int Const * c;\n" 15515 "unsigned int const * d;\n" 15516 "unsigned int Const & e;\n" 15517 "unsigned int const & f;", 15518 Alignment); 15519 verifyFormat("Const unsigned int * c;\n" 15520 "const unsigned int * d;\n" 15521 "Const unsigned int & e;\n" 15522 "const unsigned int & f;\n" 15523 "const unsigned g;\n" 15524 "Const unsigned h;", 15525 Alignment); 15526 } 15527 15528 TEST_F(FormatTest, AlignWithLineBreaks) { 15529 auto Style = getLLVMStyleWithColumns(120); 15530 15531 EXPECT_EQ(Style.AlignConsecutiveAssignments, FormatStyle::ACS_None); 15532 EXPECT_EQ(Style.AlignConsecutiveDeclarations, FormatStyle::ACS_None); 15533 verifyFormat("void foo() {\n" 15534 " int myVar = 5;\n" 15535 " double x = 3.14;\n" 15536 " auto str = \"Hello \"\n" 15537 " \"World\";\n" 15538 " auto s = \"Hello \"\n" 15539 " \"Again\";\n" 15540 "}", 15541 Style); 15542 15543 // clang-format off 15544 verifyFormat("void foo() {\n" 15545 " const int capacityBefore = Entries.capacity();\n" 15546 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 15547 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 15548 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 15549 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 15550 "}", 15551 Style); 15552 // clang-format on 15553 15554 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 15555 verifyFormat("void foo() {\n" 15556 " int myVar = 5;\n" 15557 " double x = 3.14;\n" 15558 " auto str = \"Hello \"\n" 15559 " \"World\";\n" 15560 " auto s = \"Hello \"\n" 15561 " \"Again\";\n" 15562 "}", 15563 Style); 15564 15565 // clang-format off 15566 verifyFormat("void foo() {\n" 15567 " const int capacityBefore = Entries.capacity();\n" 15568 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 15569 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 15570 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 15571 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 15572 "}", 15573 Style); 15574 // clang-format on 15575 15576 Style.AlignConsecutiveAssignments = FormatStyle::ACS_None; 15577 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 15578 verifyFormat("void foo() {\n" 15579 " int myVar = 5;\n" 15580 " double x = 3.14;\n" 15581 " auto str = \"Hello \"\n" 15582 " \"World\";\n" 15583 " auto s = \"Hello \"\n" 15584 " \"Again\";\n" 15585 "}", 15586 Style); 15587 15588 // clang-format off 15589 verifyFormat("void foo() {\n" 15590 " const int capacityBefore = Entries.capacity();\n" 15591 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 15592 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 15593 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 15594 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 15595 "}", 15596 Style); 15597 // clang-format on 15598 15599 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 15600 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 15601 15602 verifyFormat("void foo() {\n" 15603 " int myVar = 5;\n" 15604 " double x = 3.14;\n" 15605 " auto str = \"Hello \"\n" 15606 " \"World\";\n" 15607 " auto s = \"Hello \"\n" 15608 " \"Again\";\n" 15609 "}", 15610 Style); 15611 15612 // clang-format off 15613 verifyFormat("void foo() {\n" 15614 " const int capacityBefore = Entries.capacity();\n" 15615 " const auto newEntry = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 15616 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 15617 " const X newEntry2 = Entries.emplaceHint(std::piecewise_construct, std::forward_as_tuple(uniqueId),\n" 15618 " std::forward_as_tuple(id, uniqueId, name, threadCreation));\n" 15619 "}", 15620 Style); 15621 // clang-format on 15622 } 15623 15624 TEST_F(FormatTest, LinuxBraceBreaking) { 15625 FormatStyle LinuxBraceStyle = getLLVMStyle(); 15626 LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; 15627 verifyFormat("namespace a\n" 15628 "{\n" 15629 "class A\n" 15630 "{\n" 15631 " void f()\n" 15632 " {\n" 15633 " if (true) {\n" 15634 " a();\n" 15635 " b();\n" 15636 " } else {\n" 15637 " a();\n" 15638 " }\n" 15639 " }\n" 15640 " void g() { return; }\n" 15641 "};\n" 15642 "struct B {\n" 15643 " int x;\n" 15644 "};\n" 15645 "} // namespace a\n", 15646 LinuxBraceStyle); 15647 verifyFormat("enum X {\n" 15648 " Y = 0,\n" 15649 "}\n", 15650 LinuxBraceStyle); 15651 verifyFormat("struct S {\n" 15652 " int Type;\n" 15653 " union {\n" 15654 " int x;\n" 15655 " double y;\n" 15656 " } Value;\n" 15657 " class C\n" 15658 " {\n" 15659 " MyFavoriteType Value;\n" 15660 " } Class;\n" 15661 "}\n", 15662 LinuxBraceStyle); 15663 } 15664 15665 TEST_F(FormatTest, MozillaBraceBreaking) { 15666 FormatStyle MozillaBraceStyle = getLLVMStyle(); 15667 MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; 15668 MozillaBraceStyle.FixNamespaceComments = false; 15669 verifyFormat("namespace a {\n" 15670 "class A\n" 15671 "{\n" 15672 " void f()\n" 15673 " {\n" 15674 " if (true) {\n" 15675 " a();\n" 15676 " b();\n" 15677 " }\n" 15678 " }\n" 15679 " void g() { return; }\n" 15680 "};\n" 15681 "enum E\n" 15682 "{\n" 15683 " A,\n" 15684 " // foo\n" 15685 " B,\n" 15686 " C\n" 15687 "};\n" 15688 "struct B\n" 15689 "{\n" 15690 " int x;\n" 15691 "};\n" 15692 "}\n", 15693 MozillaBraceStyle); 15694 verifyFormat("struct S\n" 15695 "{\n" 15696 " int Type;\n" 15697 " union\n" 15698 " {\n" 15699 " int x;\n" 15700 " double y;\n" 15701 " } Value;\n" 15702 " class C\n" 15703 " {\n" 15704 " MyFavoriteType Value;\n" 15705 " } Class;\n" 15706 "}\n", 15707 MozillaBraceStyle); 15708 } 15709 15710 TEST_F(FormatTest, StroustrupBraceBreaking) { 15711 FormatStyle StroustrupBraceStyle = getLLVMStyle(); 15712 StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 15713 verifyFormat("namespace a {\n" 15714 "class A {\n" 15715 " void f()\n" 15716 " {\n" 15717 " if (true) {\n" 15718 " a();\n" 15719 " b();\n" 15720 " }\n" 15721 " }\n" 15722 " void g() { return; }\n" 15723 "};\n" 15724 "struct B {\n" 15725 " int x;\n" 15726 "};\n" 15727 "} // namespace a\n", 15728 StroustrupBraceStyle); 15729 15730 verifyFormat("void foo()\n" 15731 "{\n" 15732 " if (a) {\n" 15733 " a();\n" 15734 " }\n" 15735 " else {\n" 15736 " b();\n" 15737 " }\n" 15738 "}\n", 15739 StroustrupBraceStyle); 15740 15741 verifyFormat("#ifdef _DEBUG\n" 15742 "int foo(int i = 0)\n" 15743 "#else\n" 15744 "int foo(int i = 5)\n" 15745 "#endif\n" 15746 "{\n" 15747 " return i;\n" 15748 "}", 15749 StroustrupBraceStyle); 15750 15751 verifyFormat("void foo() {}\n" 15752 "void bar()\n" 15753 "#ifdef _DEBUG\n" 15754 "{\n" 15755 " foo();\n" 15756 "}\n" 15757 "#else\n" 15758 "{\n" 15759 "}\n" 15760 "#endif", 15761 StroustrupBraceStyle); 15762 15763 verifyFormat("void foobar() { int i = 5; }\n" 15764 "#ifdef _DEBUG\n" 15765 "void bar() {}\n" 15766 "#else\n" 15767 "void bar() { foobar(); }\n" 15768 "#endif", 15769 StroustrupBraceStyle); 15770 } 15771 15772 TEST_F(FormatTest, AllmanBraceBreaking) { 15773 FormatStyle AllmanBraceStyle = getLLVMStyle(); 15774 AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; 15775 15776 EXPECT_EQ("namespace a\n" 15777 "{\n" 15778 "void f();\n" 15779 "void g();\n" 15780 "} // namespace a\n", 15781 format("namespace a\n" 15782 "{\n" 15783 "void f();\n" 15784 "void g();\n" 15785 "}\n", 15786 AllmanBraceStyle)); 15787 15788 verifyFormat("namespace a\n" 15789 "{\n" 15790 "class A\n" 15791 "{\n" 15792 " void f()\n" 15793 " {\n" 15794 " if (true)\n" 15795 " {\n" 15796 " a();\n" 15797 " b();\n" 15798 " }\n" 15799 " }\n" 15800 " void g() { return; }\n" 15801 "};\n" 15802 "struct B\n" 15803 "{\n" 15804 " int x;\n" 15805 "};\n" 15806 "union C\n" 15807 "{\n" 15808 "};\n" 15809 "} // namespace a", 15810 AllmanBraceStyle); 15811 15812 verifyFormat("void f()\n" 15813 "{\n" 15814 " if (true)\n" 15815 " {\n" 15816 " a();\n" 15817 " }\n" 15818 " else if (false)\n" 15819 " {\n" 15820 " b();\n" 15821 " }\n" 15822 " else\n" 15823 " {\n" 15824 " c();\n" 15825 " }\n" 15826 "}\n", 15827 AllmanBraceStyle); 15828 15829 verifyFormat("void f()\n" 15830 "{\n" 15831 " for (int i = 0; i < 10; ++i)\n" 15832 " {\n" 15833 " a();\n" 15834 " }\n" 15835 " while (false)\n" 15836 " {\n" 15837 " b();\n" 15838 " }\n" 15839 " do\n" 15840 " {\n" 15841 " c();\n" 15842 " } while (false)\n" 15843 "}\n", 15844 AllmanBraceStyle); 15845 15846 verifyFormat("void f(int a)\n" 15847 "{\n" 15848 " switch (a)\n" 15849 " {\n" 15850 " case 0:\n" 15851 " break;\n" 15852 " case 1:\n" 15853 " {\n" 15854 " break;\n" 15855 " }\n" 15856 " case 2:\n" 15857 " {\n" 15858 " }\n" 15859 " break;\n" 15860 " default:\n" 15861 " break;\n" 15862 " }\n" 15863 "}\n", 15864 AllmanBraceStyle); 15865 15866 verifyFormat("enum X\n" 15867 "{\n" 15868 " Y = 0,\n" 15869 "}\n", 15870 AllmanBraceStyle); 15871 verifyFormat("enum X\n" 15872 "{\n" 15873 " Y = 0\n" 15874 "}\n", 15875 AllmanBraceStyle); 15876 15877 verifyFormat("@interface BSApplicationController ()\n" 15878 "{\n" 15879 "@private\n" 15880 " id _extraIvar;\n" 15881 "}\n" 15882 "@end\n", 15883 AllmanBraceStyle); 15884 15885 verifyFormat("#ifdef _DEBUG\n" 15886 "int foo(int i = 0)\n" 15887 "#else\n" 15888 "int foo(int i = 5)\n" 15889 "#endif\n" 15890 "{\n" 15891 " return i;\n" 15892 "}", 15893 AllmanBraceStyle); 15894 15895 verifyFormat("void foo() {}\n" 15896 "void bar()\n" 15897 "#ifdef _DEBUG\n" 15898 "{\n" 15899 " foo();\n" 15900 "}\n" 15901 "#else\n" 15902 "{\n" 15903 "}\n" 15904 "#endif", 15905 AllmanBraceStyle); 15906 15907 verifyFormat("void foobar() { int i = 5; }\n" 15908 "#ifdef _DEBUG\n" 15909 "void bar() {}\n" 15910 "#else\n" 15911 "void bar() { foobar(); }\n" 15912 "#endif", 15913 AllmanBraceStyle); 15914 15915 EXPECT_EQ(AllmanBraceStyle.AllowShortLambdasOnASingleLine, 15916 FormatStyle::SLS_All); 15917 15918 verifyFormat("[](int i) { return i + 2; };\n" 15919 "[](int i, int j)\n" 15920 "{\n" 15921 " auto x = i + j;\n" 15922 " auto y = i * j;\n" 15923 " return x ^ y;\n" 15924 "};\n" 15925 "void foo()\n" 15926 "{\n" 15927 " auto shortLambda = [](int i) { return i + 2; };\n" 15928 " auto longLambda = [](int i, int j)\n" 15929 " {\n" 15930 " auto x = i + j;\n" 15931 " auto y = i * j;\n" 15932 " return x ^ y;\n" 15933 " };\n" 15934 "}", 15935 AllmanBraceStyle); 15936 15937 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 15938 15939 verifyFormat("[](int i)\n" 15940 "{\n" 15941 " return i + 2;\n" 15942 "};\n" 15943 "[](int i, int j)\n" 15944 "{\n" 15945 " auto x = i + j;\n" 15946 " auto y = i * j;\n" 15947 " return x ^ y;\n" 15948 "};\n" 15949 "void foo()\n" 15950 "{\n" 15951 " auto shortLambda = [](int i)\n" 15952 " {\n" 15953 " return i + 2;\n" 15954 " };\n" 15955 " auto longLambda = [](int i, int j)\n" 15956 " {\n" 15957 " auto x = i + j;\n" 15958 " auto y = i * j;\n" 15959 " return x ^ y;\n" 15960 " };\n" 15961 "}", 15962 AllmanBraceStyle); 15963 15964 // Reset 15965 AllmanBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All; 15966 15967 // This shouldn't affect ObjC blocks.. 15968 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 15969 " // ...\n" 15970 " int i;\n" 15971 "}];", 15972 AllmanBraceStyle); 15973 verifyFormat("void (^block)(void) = ^{\n" 15974 " // ...\n" 15975 " int i;\n" 15976 "};", 15977 AllmanBraceStyle); 15978 // .. or dict literals. 15979 verifyFormat("void f()\n" 15980 "{\n" 15981 " // ...\n" 15982 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 15983 "}", 15984 AllmanBraceStyle); 15985 verifyFormat("void f()\n" 15986 "{\n" 15987 " // ...\n" 15988 " [object someMethod:@{a : @\"b\"}];\n" 15989 "}", 15990 AllmanBraceStyle); 15991 verifyFormat("int f()\n" 15992 "{ // comment\n" 15993 " return 42;\n" 15994 "}", 15995 AllmanBraceStyle); 15996 15997 AllmanBraceStyle.ColumnLimit = 19; 15998 verifyFormat("void f() { int i; }", AllmanBraceStyle); 15999 AllmanBraceStyle.ColumnLimit = 18; 16000 verifyFormat("void f()\n" 16001 "{\n" 16002 " int i;\n" 16003 "}", 16004 AllmanBraceStyle); 16005 AllmanBraceStyle.ColumnLimit = 80; 16006 16007 FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle; 16008 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = 16009 FormatStyle::SIS_WithoutElse; 16010 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 16011 verifyFormat("void f(bool b)\n" 16012 "{\n" 16013 " if (b)\n" 16014 " {\n" 16015 " return;\n" 16016 " }\n" 16017 "}\n", 16018 BreakBeforeBraceShortIfs); 16019 verifyFormat("void f(bool b)\n" 16020 "{\n" 16021 " if constexpr (b)\n" 16022 " {\n" 16023 " return;\n" 16024 " }\n" 16025 "}\n", 16026 BreakBeforeBraceShortIfs); 16027 verifyFormat("void f(bool b)\n" 16028 "{\n" 16029 " if CONSTEXPR (b)\n" 16030 " {\n" 16031 " return;\n" 16032 " }\n" 16033 "}\n", 16034 BreakBeforeBraceShortIfs); 16035 verifyFormat("void f(bool b)\n" 16036 "{\n" 16037 " if (b) return;\n" 16038 "}\n", 16039 BreakBeforeBraceShortIfs); 16040 verifyFormat("void f(bool b)\n" 16041 "{\n" 16042 " if constexpr (b) return;\n" 16043 "}\n", 16044 BreakBeforeBraceShortIfs); 16045 verifyFormat("void f(bool b)\n" 16046 "{\n" 16047 " if CONSTEXPR (b) return;\n" 16048 "}\n", 16049 BreakBeforeBraceShortIfs); 16050 verifyFormat("void f(bool b)\n" 16051 "{\n" 16052 " while (b)\n" 16053 " {\n" 16054 " return;\n" 16055 " }\n" 16056 "}\n", 16057 BreakBeforeBraceShortIfs); 16058 } 16059 16060 TEST_F(FormatTest, WhitesmithsBraceBreaking) { 16061 FormatStyle WhitesmithsBraceStyle = getLLVMStyle(); 16062 WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths; 16063 16064 // Make a few changes to the style for testing purposes 16065 WhitesmithsBraceStyle.AllowShortFunctionsOnASingleLine = 16066 FormatStyle::SFS_Empty; 16067 WhitesmithsBraceStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 16068 WhitesmithsBraceStyle.ColumnLimit = 0; 16069 16070 // FIXME: this test case can't decide whether there should be a blank line 16071 // after the ~D() line or not. It adds one if one doesn't exist in the test 16072 // and it removes the line if one exists. 16073 /* 16074 verifyFormat("class A;\n" 16075 "namespace B\n" 16076 " {\n" 16077 "class C;\n" 16078 "// Comment\n" 16079 "class D\n" 16080 " {\n" 16081 "public:\n" 16082 " D();\n" 16083 " ~D() {}\n" 16084 "private:\n" 16085 " enum E\n" 16086 " {\n" 16087 " F\n" 16088 " }\n" 16089 " };\n" 16090 " } // namespace B\n", 16091 WhitesmithsBraceStyle); 16092 */ 16093 16094 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_None; 16095 verifyFormat("namespace a\n" 16096 " {\n" 16097 "class A\n" 16098 " {\n" 16099 " void f()\n" 16100 " {\n" 16101 " if (true)\n" 16102 " {\n" 16103 " a();\n" 16104 " b();\n" 16105 " }\n" 16106 " }\n" 16107 " void g()\n" 16108 " {\n" 16109 " return;\n" 16110 " }\n" 16111 " };\n" 16112 "struct B\n" 16113 " {\n" 16114 " int x;\n" 16115 " };\n" 16116 " } // namespace a", 16117 WhitesmithsBraceStyle); 16118 16119 verifyFormat("namespace a\n" 16120 " {\n" 16121 "namespace b\n" 16122 " {\n" 16123 "class A\n" 16124 " {\n" 16125 " void f()\n" 16126 " {\n" 16127 " if (true)\n" 16128 " {\n" 16129 " a();\n" 16130 " b();\n" 16131 " }\n" 16132 " }\n" 16133 " void g()\n" 16134 " {\n" 16135 " return;\n" 16136 " }\n" 16137 " };\n" 16138 "struct B\n" 16139 " {\n" 16140 " int x;\n" 16141 " };\n" 16142 " } // namespace b\n" 16143 " } // namespace a", 16144 WhitesmithsBraceStyle); 16145 16146 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_Inner; 16147 verifyFormat("namespace a\n" 16148 " {\n" 16149 "namespace b\n" 16150 " {\n" 16151 " class A\n" 16152 " {\n" 16153 " void f()\n" 16154 " {\n" 16155 " if (true)\n" 16156 " {\n" 16157 " a();\n" 16158 " b();\n" 16159 " }\n" 16160 " }\n" 16161 " void g()\n" 16162 " {\n" 16163 " return;\n" 16164 " }\n" 16165 " };\n" 16166 " struct B\n" 16167 " {\n" 16168 " int x;\n" 16169 " };\n" 16170 " } // namespace b\n" 16171 " } // namespace a", 16172 WhitesmithsBraceStyle); 16173 16174 WhitesmithsBraceStyle.NamespaceIndentation = FormatStyle::NI_All; 16175 verifyFormat("namespace a\n" 16176 " {\n" 16177 " namespace b\n" 16178 " {\n" 16179 " class A\n" 16180 " {\n" 16181 " void f()\n" 16182 " {\n" 16183 " if (true)\n" 16184 " {\n" 16185 " a();\n" 16186 " b();\n" 16187 " }\n" 16188 " }\n" 16189 " void g()\n" 16190 " {\n" 16191 " return;\n" 16192 " }\n" 16193 " };\n" 16194 " struct B\n" 16195 " {\n" 16196 " int x;\n" 16197 " };\n" 16198 " } // namespace b\n" 16199 " } // namespace a", 16200 WhitesmithsBraceStyle); 16201 16202 verifyFormat("void f()\n" 16203 " {\n" 16204 " if (true)\n" 16205 " {\n" 16206 " a();\n" 16207 " }\n" 16208 " else if (false)\n" 16209 " {\n" 16210 " b();\n" 16211 " }\n" 16212 " else\n" 16213 " {\n" 16214 " c();\n" 16215 " }\n" 16216 " }\n", 16217 WhitesmithsBraceStyle); 16218 16219 verifyFormat("void f()\n" 16220 " {\n" 16221 " for (int i = 0; i < 10; ++i)\n" 16222 " {\n" 16223 " a();\n" 16224 " }\n" 16225 " while (false)\n" 16226 " {\n" 16227 " b();\n" 16228 " }\n" 16229 " do\n" 16230 " {\n" 16231 " c();\n" 16232 " } while (false)\n" 16233 " }\n", 16234 WhitesmithsBraceStyle); 16235 16236 WhitesmithsBraceStyle.IndentCaseLabels = true; 16237 verifyFormat("void switchTest1(int a)\n" 16238 " {\n" 16239 " switch (a)\n" 16240 " {\n" 16241 " case 2:\n" 16242 " {\n" 16243 " }\n" 16244 " break;\n" 16245 " }\n" 16246 " }\n", 16247 WhitesmithsBraceStyle); 16248 16249 verifyFormat("void switchTest2(int a)\n" 16250 " {\n" 16251 " switch (a)\n" 16252 " {\n" 16253 " case 0:\n" 16254 " break;\n" 16255 " case 1:\n" 16256 " {\n" 16257 " break;\n" 16258 " }\n" 16259 " case 2:\n" 16260 " {\n" 16261 " }\n" 16262 " break;\n" 16263 " default:\n" 16264 " break;\n" 16265 " }\n" 16266 " }\n", 16267 WhitesmithsBraceStyle); 16268 16269 verifyFormat("void switchTest3(int a)\n" 16270 " {\n" 16271 " switch (a)\n" 16272 " {\n" 16273 " case 0:\n" 16274 " {\n" 16275 " foo(x);\n" 16276 " }\n" 16277 " break;\n" 16278 " default:\n" 16279 " {\n" 16280 " foo(1);\n" 16281 " }\n" 16282 " break;\n" 16283 " }\n" 16284 " }\n", 16285 WhitesmithsBraceStyle); 16286 16287 WhitesmithsBraceStyle.IndentCaseLabels = false; 16288 16289 verifyFormat("void switchTest4(int a)\n" 16290 " {\n" 16291 " switch (a)\n" 16292 " {\n" 16293 " case 2:\n" 16294 " {\n" 16295 " }\n" 16296 " break;\n" 16297 " }\n" 16298 " }\n", 16299 WhitesmithsBraceStyle); 16300 16301 verifyFormat("void switchTest5(int a)\n" 16302 " {\n" 16303 " switch (a)\n" 16304 " {\n" 16305 " case 0:\n" 16306 " break;\n" 16307 " case 1:\n" 16308 " {\n" 16309 " foo();\n" 16310 " break;\n" 16311 " }\n" 16312 " case 2:\n" 16313 " {\n" 16314 " }\n" 16315 " break;\n" 16316 " default:\n" 16317 " break;\n" 16318 " }\n" 16319 " }\n", 16320 WhitesmithsBraceStyle); 16321 16322 verifyFormat("void switchTest6(int a)\n" 16323 " {\n" 16324 " switch (a)\n" 16325 " {\n" 16326 " case 0:\n" 16327 " {\n" 16328 " foo(x);\n" 16329 " }\n" 16330 " break;\n" 16331 " default:\n" 16332 " {\n" 16333 " foo(1);\n" 16334 " }\n" 16335 " break;\n" 16336 " }\n" 16337 " }\n", 16338 WhitesmithsBraceStyle); 16339 16340 verifyFormat("enum X\n" 16341 " {\n" 16342 " Y = 0, // testing\n" 16343 " }\n", 16344 WhitesmithsBraceStyle); 16345 16346 verifyFormat("enum X\n" 16347 " {\n" 16348 " Y = 0\n" 16349 " }\n", 16350 WhitesmithsBraceStyle); 16351 verifyFormat("enum X\n" 16352 " {\n" 16353 " Y = 0,\n" 16354 " Z = 1\n" 16355 " };\n", 16356 WhitesmithsBraceStyle); 16357 16358 verifyFormat("@interface BSApplicationController ()\n" 16359 " {\n" 16360 "@private\n" 16361 " id _extraIvar;\n" 16362 " }\n" 16363 "@end\n", 16364 WhitesmithsBraceStyle); 16365 16366 verifyFormat("#ifdef _DEBUG\n" 16367 "int foo(int i = 0)\n" 16368 "#else\n" 16369 "int foo(int i = 5)\n" 16370 "#endif\n" 16371 " {\n" 16372 " return i;\n" 16373 " }", 16374 WhitesmithsBraceStyle); 16375 16376 verifyFormat("void foo() {}\n" 16377 "void bar()\n" 16378 "#ifdef _DEBUG\n" 16379 " {\n" 16380 " foo();\n" 16381 " }\n" 16382 "#else\n" 16383 " {\n" 16384 " }\n" 16385 "#endif", 16386 WhitesmithsBraceStyle); 16387 16388 verifyFormat("void foobar()\n" 16389 " {\n" 16390 " int i = 5;\n" 16391 " }\n" 16392 "#ifdef _DEBUG\n" 16393 "void bar()\n" 16394 " {\n" 16395 " }\n" 16396 "#else\n" 16397 "void bar()\n" 16398 " {\n" 16399 " foobar();\n" 16400 " }\n" 16401 "#endif", 16402 WhitesmithsBraceStyle); 16403 16404 // This shouldn't affect ObjC blocks.. 16405 verifyFormat("[self doSomeThingWithACompletionHandler:^{\n" 16406 " // ...\n" 16407 " int i;\n" 16408 "}];", 16409 WhitesmithsBraceStyle); 16410 verifyFormat("void (^block)(void) = ^{\n" 16411 " // ...\n" 16412 " int i;\n" 16413 "};", 16414 WhitesmithsBraceStyle); 16415 // .. or dict literals. 16416 verifyFormat("void f()\n" 16417 " {\n" 16418 " [object someMethod:@{@\"a\" : @\"b\"}];\n" 16419 " }", 16420 WhitesmithsBraceStyle); 16421 16422 verifyFormat("int f()\n" 16423 " { // comment\n" 16424 " return 42;\n" 16425 " }", 16426 WhitesmithsBraceStyle); 16427 16428 FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle; 16429 BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = 16430 FormatStyle::SIS_OnlyFirstIf; 16431 BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true; 16432 verifyFormat("void f(bool b)\n" 16433 " {\n" 16434 " if (b)\n" 16435 " {\n" 16436 " return;\n" 16437 " }\n" 16438 " }\n", 16439 BreakBeforeBraceShortIfs); 16440 verifyFormat("void f(bool b)\n" 16441 " {\n" 16442 " if (b) return;\n" 16443 " }\n", 16444 BreakBeforeBraceShortIfs); 16445 verifyFormat("void f(bool b)\n" 16446 " {\n" 16447 " while (b)\n" 16448 " {\n" 16449 " return;\n" 16450 " }\n" 16451 " }\n", 16452 BreakBeforeBraceShortIfs); 16453 } 16454 16455 TEST_F(FormatTest, GNUBraceBreaking) { 16456 FormatStyle GNUBraceStyle = getLLVMStyle(); 16457 GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU; 16458 verifyFormat("namespace a\n" 16459 "{\n" 16460 "class A\n" 16461 "{\n" 16462 " void f()\n" 16463 " {\n" 16464 " int a;\n" 16465 " {\n" 16466 " int b;\n" 16467 " }\n" 16468 " if (true)\n" 16469 " {\n" 16470 " a();\n" 16471 " b();\n" 16472 " }\n" 16473 " }\n" 16474 " void g() { return; }\n" 16475 "}\n" 16476 "} // namespace a", 16477 GNUBraceStyle); 16478 16479 verifyFormat("void f()\n" 16480 "{\n" 16481 " if (true)\n" 16482 " {\n" 16483 " a();\n" 16484 " }\n" 16485 " else if (false)\n" 16486 " {\n" 16487 " b();\n" 16488 " }\n" 16489 " else\n" 16490 " {\n" 16491 " c();\n" 16492 " }\n" 16493 "}\n", 16494 GNUBraceStyle); 16495 16496 verifyFormat("void f()\n" 16497 "{\n" 16498 " for (int i = 0; i < 10; ++i)\n" 16499 " {\n" 16500 " a();\n" 16501 " }\n" 16502 " while (false)\n" 16503 " {\n" 16504 " b();\n" 16505 " }\n" 16506 " do\n" 16507 " {\n" 16508 " c();\n" 16509 " }\n" 16510 " while (false);\n" 16511 "}\n", 16512 GNUBraceStyle); 16513 16514 verifyFormat("void f(int a)\n" 16515 "{\n" 16516 " switch (a)\n" 16517 " {\n" 16518 " case 0:\n" 16519 " break;\n" 16520 " case 1:\n" 16521 " {\n" 16522 " break;\n" 16523 " }\n" 16524 " case 2:\n" 16525 " {\n" 16526 " }\n" 16527 " break;\n" 16528 " default:\n" 16529 " break;\n" 16530 " }\n" 16531 "}\n", 16532 GNUBraceStyle); 16533 16534 verifyFormat("enum X\n" 16535 "{\n" 16536 " Y = 0,\n" 16537 "}\n", 16538 GNUBraceStyle); 16539 16540 verifyFormat("@interface BSApplicationController ()\n" 16541 "{\n" 16542 "@private\n" 16543 " id _extraIvar;\n" 16544 "}\n" 16545 "@end\n", 16546 GNUBraceStyle); 16547 16548 verifyFormat("#ifdef _DEBUG\n" 16549 "int foo(int i = 0)\n" 16550 "#else\n" 16551 "int foo(int i = 5)\n" 16552 "#endif\n" 16553 "{\n" 16554 " return i;\n" 16555 "}", 16556 GNUBraceStyle); 16557 16558 verifyFormat("void foo() {}\n" 16559 "void bar()\n" 16560 "#ifdef _DEBUG\n" 16561 "{\n" 16562 " foo();\n" 16563 "}\n" 16564 "#else\n" 16565 "{\n" 16566 "}\n" 16567 "#endif", 16568 GNUBraceStyle); 16569 16570 verifyFormat("void foobar() { int i = 5; }\n" 16571 "#ifdef _DEBUG\n" 16572 "void bar() {}\n" 16573 "#else\n" 16574 "void bar() { foobar(); }\n" 16575 "#endif", 16576 GNUBraceStyle); 16577 } 16578 16579 TEST_F(FormatTest, WebKitBraceBreaking) { 16580 FormatStyle WebKitBraceStyle = getLLVMStyle(); 16581 WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit; 16582 WebKitBraceStyle.FixNamespaceComments = false; 16583 verifyFormat("namespace a {\n" 16584 "class A {\n" 16585 " void f()\n" 16586 " {\n" 16587 " if (true) {\n" 16588 " a();\n" 16589 " b();\n" 16590 " }\n" 16591 " }\n" 16592 " void g() { return; }\n" 16593 "};\n" 16594 "enum E {\n" 16595 " A,\n" 16596 " // foo\n" 16597 " B,\n" 16598 " C\n" 16599 "};\n" 16600 "struct B {\n" 16601 " int x;\n" 16602 "};\n" 16603 "}\n", 16604 WebKitBraceStyle); 16605 verifyFormat("struct S {\n" 16606 " int Type;\n" 16607 " union {\n" 16608 " int x;\n" 16609 " double y;\n" 16610 " } Value;\n" 16611 " class C {\n" 16612 " MyFavoriteType Value;\n" 16613 " } Class;\n" 16614 "};\n", 16615 WebKitBraceStyle); 16616 } 16617 16618 TEST_F(FormatTest, CatchExceptionReferenceBinding) { 16619 verifyFormat("void f() {\n" 16620 " try {\n" 16621 " } catch (const Exception &e) {\n" 16622 " }\n" 16623 "}\n", 16624 getLLVMStyle()); 16625 } 16626 16627 TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) { 16628 auto Style = getLLVMStyle(); 16629 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 16630 Style.AlignConsecutiveAssignments = 16631 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 16632 Style.AlignConsecutiveDeclarations = 16633 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 16634 verifyFormat("struct test demo[] = {\n" 16635 " {56, 23, \"hello\"},\n" 16636 " {-1, 93463, \"world\"},\n" 16637 " { 7, 5, \"!!\"}\n" 16638 "};\n", 16639 Style); 16640 16641 verifyFormat("struct test demo[] = {\n" 16642 " {56, 23, \"hello\"}, // first line\n" 16643 " {-1, 93463, \"world\"}, // second line\n" 16644 " { 7, 5, \"!!\"} // third line\n" 16645 "};\n", 16646 Style); 16647 16648 verifyFormat("struct test demo[4] = {\n" 16649 " { 56, 23, 21, \"oh\"}, // first line\n" 16650 " { -1, 93463, 22, \"my\"}, // second line\n" 16651 " { 7, 5, 1, \"goodness\"} // third line\n" 16652 " {234, 5, 1, \"gracious\"} // fourth line\n" 16653 "};\n", 16654 Style); 16655 16656 verifyFormat("struct test demo[3] = {\n" 16657 " {56, 23, \"hello\"},\n" 16658 " {-1, 93463, \"world\"},\n" 16659 " { 7, 5, \"!!\"}\n" 16660 "};\n", 16661 Style); 16662 16663 verifyFormat("struct test demo[3] = {\n" 16664 " {int{56}, 23, \"hello\"},\n" 16665 " {int{-1}, 93463, \"world\"},\n" 16666 " { int{7}, 5, \"!!\"}\n" 16667 "};\n", 16668 Style); 16669 16670 verifyFormat("struct test demo[] = {\n" 16671 " {56, 23, \"hello\"},\n" 16672 " {-1, 93463, \"world\"},\n" 16673 " { 7, 5, \"!!\"},\n" 16674 "};\n", 16675 Style); 16676 16677 verifyFormat("test demo[] = {\n" 16678 " {56, 23, \"hello\"},\n" 16679 " {-1, 93463, \"world\"},\n" 16680 " { 7, 5, \"!!\"},\n" 16681 "};\n", 16682 Style); 16683 16684 verifyFormat("demo = std::array<struct test, 3>{\n" 16685 " test{56, 23, \"hello\"},\n" 16686 " test{-1, 93463, \"world\"},\n" 16687 " test{ 7, 5, \"!!\"},\n" 16688 "};\n", 16689 Style); 16690 16691 verifyFormat("test demo[] = {\n" 16692 " {56, 23, \"hello\"},\n" 16693 "#if X\n" 16694 " {-1, 93463, \"world\"},\n" 16695 "#endif\n" 16696 " { 7, 5, \"!!\"}\n" 16697 "};\n", 16698 Style); 16699 16700 verifyFormat( 16701 "test demo[] = {\n" 16702 " { 7, 23,\n" 16703 " \"hello world i am a very long line that really, in any\"\n" 16704 " \"just world, ought to be split over multiple lines\"},\n" 16705 " {-1, 93463, \"world\"},\n" 16706 " {56, 5, \"!!\"}\n" 16707 "};\n", 16708 Style); 16709 16710 verifyFormat("return GradForUnaryCwise(g, {\n" 16711 " {{\"sign\"}, \"Sign\", " 16712 " {\"x\", \"dy\"}},\n" 16713 " { {\"dx\"}, \"Mul\", {\"dy\"" 16714 ", \"sign\"}},\n" 16715 "});\n", 16716 Style); 16717 16718 Style.ColumnLimit = 0; 16719 EXPECT_EQ( 16720 "test demo[] = {\n" 16721 " {56, 23, \"hello world i am a very long line that really, " 16722 "in any just world, ought to be split over multiple lines\"},\n" 16723 " {-1, 93463, " 16724 " \"world\"},\n" 16725 " { 7, 5, " 16726 " \"!!\"},\n" 16727 "};", 16728 format("test demo[] = {{56, 23, \"hello world i am a very long line " 16729 "that really, in any just world, ought to be split over multiple " 16730 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 16731 Style)); 16732 16733 Style.ColumnLimit = 80; 16734 verifyFormat("test demo[] = {\n" 16735 " {56, 23, /* a comment */ \"hello\"},\n" 16736 " {-1, 93463, \"world\"},\n" 16737 " { 7, 5, \"!!\"}\n" 16738 "};\n", 16739 Style); 16740 16741 verifyFormat("test demo[] = {\n" 16742 " {56, 23, \"hello\"},\n" 16743 " {-1, 93463, \"world\" /* comment here */},\n" 16744 " { 7, 5, \"!!\"}\n" 16745 "};\n", 16746 Style); 16747 16748 verifyFormat("test demo[] = {\n" 16749 " {56, /* a comment */ 23, \"hello\"},\n" 16750 " {-1, 93463, \"world\"},\n" 16751 " { 7, 5, \"!!\"}\n" 16752 "};\n", 16753 Style); 16754 16755 Style.ColumnLimit = 20; 16756 EXPECT_EQ( 16757 "demo = std::array<\n" 16758 " struct test, 3>{\n" 16759 " test{\n" 16760 " 56, 23,\n" 16761 " \"hello \"\n" 16762 " \"world i \"\n" 16763 " \"am a very \"\n" 16764 " \"long line \"\n" 16765 " \"that \"\n" 16766 " \"really, \"\n" 16767 " \"in any \"\n" 16768 " \"just \"\n" 16769 " \"world, \"\n" 16770 " \"ought to \"\n" 16771 " \"be split \"\n" 16772 " \"over \"\n" 16773 " \"multiple \"\n" 16774 " \"lines\"},\n" 16775 " test{-1, 93463,\n" 16776 " \"world\"},\n" 16777 " test{ 7, 5,\n" 16778 " \"!!\" },\n" 16779 "};", 16780 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world " 16781 "i am a very long line that really, in any just world, ought " 16782 "to be split over multiple lines\"},test{-1, 93463, \"world\"}," 16783 "test{7, 5, \"!!\"},};", 16784 Style)); 16785 // This caused a core dump by enabling Alignment in the LLVMStyle globally 16786 Style = getLLVMStyleWithColumns(50); 16787 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 16788 verifyFormat("static A x = {\n" 16789 " {{init1, init2, init3, init4},\n" 16790 " {init1, init2, init3, init4}}\n" 16791 "};", 16792 Style); 16793 Style.ColumnLimit = 100; 16794 EXPECT_EQ( 16795 "test demo[] = {\n" 16796 " {56, 23,\n" 16797 " \"hello world i am a very long line that really, in any just world" 16798 ", ought to be split over \"\n" 16799 " \"multiple lines\" },\n" 16800 " {-1, 93463, \"world\"},\n" 16801 " { 7, 5, \"!!\"},\n" 16802 "};", 16803 format("test demo[] = {{56, 23, \"hello world i am a very long line " 16804 "that really, in any just world, ought to be split over multiple " 16805 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 16806 Style)); 16807 16808 Style = getLLVMStyleWithColumns(50); 16809 Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; 16810 Style.AlignConsecutiveAssignments = 16811 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 16812 Style.AlignConsecutiveDeclarations = 16813 FormatStyle::AlignConsecutiveStyle::ACS_Consecutive; 16814 verifyFormat("struct test demo[] = {\n" 16815 " {56, 23, \"hello\"},\n" 16816 " {-1, 93463, \"world\"},\n" 16817 " { 7, 5, \"!!\"}\n" 16818 "};\n" 16819 "static A x = {\n" 16820 " {{init1, init2, init3, init4},\n" 16821 " {init1, init2, init3, init4}}\n" 16822 "};", 16823 Style); 16824 Style.ColumnLimit = 100; 16825 Style.AlignConsecutiveAssignments = 16826 FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments; 16827 Style.AlignConsecutiveDeclarations = 16828 FormatStyle::AlignConsecutiveStyle::ACS_AcrossComments; 16829 verifyFormat("struct test demo[] = {\n" 16830 " {56, 23, \"hello\"},\n" 16831 " {-1, 93463, \"world\"},\n" 16832 " { 7, 5, \"!!\"}\n" 16833 "};\n" 16834 "struct test demo[4] = {\n" 16835 " { 56, 23, 21, \"oh\"}, // first line\n" 16836 " { -1, 93463, 22, \"my\"}, // second line\n" 16837 " { 7, 5, 1, \"goodness\"} // third line\n" 16838 " {234, 5, 1, \"gracious\"} // fourth line\n" 16839 "};\n", 16840 Style); 16841 EXPECT_EQ( 16842 "test demo[] = {\n" 16843 " {56,\n" 16844 " \"hello world i am a very long line that really, in any just world" 16845 ", ought to be split over \"\n" 16846 " \"multiple lines\", 23},\n" 16847 " {-1, \"world\", 93463},\n" 16848 " { 7, \"!!\", 5},\n" 16849 "};", 16850 format("test demo[] = {{56, \"hello world i am a very long line " 16851 "that really, in any just world, ought to be split over multiple " 16852 "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};", 16853 Style)); 16854 } 16855 16856 TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) { 16857 auto Style = getLLVMStyle(); 16858 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; 16859 verifyFormat("struct test demo[] = {\n" 16860 " {56, 23, \"hello\"},\n" 16861 " {-1, 93463, \"world\"},\n" 16862 " {7, 5, \"!!\" }\n" 16863 "};\n", 16864 Style); 16865 16866 verifyFormat("struct test demo[] = {\n" 16867 " {56, 23, \"hello\"}, // first line\n" 16868 " {-1, 93463, \"world\"}, // second line\n" 16869 " {7, 5, \"!!\" } // third line\n" 16870 "};\n", 16871 Style); 16872 verifyFormat("struct test demo[4] = {\n" 16873 " {56, 23, 21, \"oh\" }, // first line\n" 16874 " {-1, 93463, 22, \"my\" }, // second line\n" 16875 " {7, 5, 1, \"goodness\"} // third line\n" 16876 " {234, 5, 1, \"gracious\"} // fourth line\n" 16877 "};\n", 16878 Style); 16879 verifyFormat("struct test demo[3] = {\n" 16880 " {56, 23, \"hello\"},\n" 16881 " {-1, 93463, \"world\"},\n" 16882 " {7, 5, \"!!\" }\n" 16883 "};\n", 16884 Style); 16885 16886 verifyFormat("struct test demo[3] = {\n" 16887 " {int{56}, 23, \"hello\"},\n" 16888 " {int{-1}, 93463, \"world\"},\n" 16889 " {int{7}, 5, \"!!\" }\n" 16890 "};\n", 16891 Style); 16892 verifyFormat("struct test demo[] = {\n" 16893 " {56, 23, \"hello\"},\n" 16894 " {-1, 93463, \"world\"},\n" 16895 " {7, 5, \"!!\" },\n" 16896 "};\n", 16897 Style); 16898 verifyFormat("test demo[] = {\n" 16899 " {56, 23, \"hello\"},\n" 16900 " {-1, 93463, \"world\"},\n" 16901 " {7, 5, \"!!\" },\n" 16902 "};\n", 16903 Style); 16904 verifyFormat("demo = std::array<struct test, 3>{\n" 16905 " test{56, 23, \"hello\"},\n" 16906 " test{-1, 93463, \"world\"},\n" 16907 " test{7, 5, \"!!\" },\n" 16908 "};\n", 16909 Style); 16910 verifyFormat("test demo[] = {\n" 16911 " {56, 23, \"hello\"},\n" 16912 "#if X\n" 16913 " {-1, 93463, \"world\"},\n" 16914 "#endif\n" 16915 " {7, 5, \"!!\" }\n" 16916 "};\n", 16917 Style); 16918 verifyFormat( 16919 "test demo[] = {\n" 16920 " {7, 23,\n" 16921 " \"hello world i am a very long line that really, in any\"\n" 16922 " \"just world, ought to be split over multiple lines\"},\n" 16923 " {-1, 93463, \"world\" },\n" 16924 " {56, 5, \"!!\" }\n" 16925 "};\n", 16926 Style); 16927 16928 verifyFormat("return GradForUnaryCwise(g, {\n" 16929 " {{\"sign\"}, \"Sign\", {\"x\", " 16930 "\"dy\"} },\n" 16931 " {{\"dx\"}, \"Mul\", " 16932 "{\"dy\", \"sign\"}},\n" 16933 "});\n", 16934 Style); 16935 16936 Style.ColumnLimit = 0; 16937 EXPECT_EQ( 16938 "test demo[] = {\n" 16939 " {56, 23, \"hello world i am a very long line that really, in any " 16940 "just world, ought to be split over multiple lines\"},\n" 16941 " {-1, 93463, \"world\" " 16942 " },\n" 16943 " {7, 5, \"!!\" " 16944 " },\n" 16945 "};", 16946 format("test demo[] = {{56, 23, \"hello world i am a very long line " 16947 "that really, in any just world, ought to be split over multiple " 16948 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 16949 Style)); 16950 16951 Style.ColumnLimit = 80; 16952 verifyFormat("test demo[] = {\n" 16953 " {56, 23, /* a comment */ \"hello\"},\n" 16954 " {-1, 93463, \"world\" },\n" 16955 " {7, 5, \"!!\" }\n" 16956 "};\n", 16957 Style); 16958 16959 verifyFormat("test demo[] = {\n" 16960 " {56, 23, \"hello\" },\n" 16961 " {-1, 93463, \"world\" /* comment here */},\n" 16962 " {7, 5, \"!!\" }\n" 16963 "};\n", 16964 Style); 16965 16966 verifyFormat("test demo[] = {\n" 16967 " {56, /* a comment */ 23, \"hello\"},\n" 16968 " {-1, 93463, \"world\"},\n" 16969 " {7, 5, \"!!\" }\n" 16970 "};\n", 16971 Style); 16972 16973 Style.ColumnLimit = 20; 16974 EXPECT_EQ( 16975 "demo = std::array<\n" 16976 " struct test, 3>{\n" 16977 " test{\n" 16978 " 56, 23,\n" 16979 " \"hello \"\n" 16980 " \"world i \"\n" 16981 " \"am a very \"\n" 16982 " \"long line \"\n" 16983 " \"that \"\n" 16984 " \"really, \"\n" 16985 " \"in any \"\n" 16986 " \"just \"\n" 16987 " \"world, \"\n" 16988 " \"ought to \"\n" 16989 " \"be split \"\n" 16990 " \"over \"\n" 16991 " \"multiple \"\n" 16992 " \"lines\"},\n" 16993 " test{-1, 93463,\n" 16994 " \"world\"},\n" 16995 " test{7, 5,\n" 16996 " \"!!\" },\n" 16997 "};", 16998 format("demo = std::array<struct test, 3>{test{56, 23, \"hello world " 16999 "i am a very long line that really, in any just world, ought " 17000 "to be split over multiple lines\"},test{-1, 93463, \"world\"}," 17001 "test{7, 5, \"!!\"},};", 17002 Style)); 17003 17004 // This caused a core dump by enabling Alignment in the LLVMStyle globally 17005 Style = getLLVMStyleWithColumns(50); 17006 Style.AlignArrayOfStructures = FormatStyle::AIAS_Left; 17007 verifyFormat("static A x = {\n" 17008 " {{init1, init2, init3, init4},\n" 17009 " {init1, init2, init3, init4}}\n" 17010 "};", 17011 Style); 17012 Style.ColumnLimit = 100; 17013 EXPECT_EQ( 17014 "test demo[] = {\n" 17015 " {56, 23,\n" 17016 " \"hello world i am a very long line that really, in any just world" 17017 ", ought to be split over \"\n" 17018 " \"multiple lines\" },\n" 17019 " {-1, 93463, \"world\"},\n" 17020 " {7, 5, \"!!\" },\n" 17021 "};", 17022 format("test demo[] = {{56, 23, \"hello world i am a very long line " 17023 "that really, in any just world, ought to be split over multiple " 17024 "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", 17025 Style)); 17026 } 17027 17028 TEST_F(FormatTest, UnderstandsPragmas) { 17029 verifyFormat("#pragma omp reduction(| : var)"); 17030 verifyFormat("#pragma omp reduction(+ : var)"); 17031 17032 EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " 17033 "(including parentheses).", 17034 format("#pragma mark Any non-hyphenated or hyphenated string " 17035 "(including parentheses).")); 17036 } 17037 17038 TEST_F(FormatTest, UnderstandPragmaOption) { 17039 verifyFormat("#pragma option -C -A"); 17040 17041 EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); 17042 } 17043 17044 TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) { 17045 FormatStyle Style = getLLVMStyle(); 17046 Style.ColumnLimit = 20; 17047 17048 // See PR41213 17049 EXPECT_EQ("/*\n" 17050 " *\t9012345\n" 17051 " * /8901\n" 17052 " */", 17053 format("/*\n" 17054 " *\t9012345 /8901\n" 17055 " */", 17056 Style)); 17057 EXPECT_EQ("/*\n" 17058 " *345678\n" 17059 " *\t/8901\n" 17060 " */", 17061 format("/*\n" 17062 " *345678\t/8901\n" 17063 " */", 17064 Style)); 17065 17066 verifyFormat("int a; // the\n" 17067 " // comment", 17068 Style); 17069 EXPECT_EQ("int a; /* first line\n" 17070 " * second\n" 17071 " * line third\n" 17072 " * line\n" 17073 " */", 17074 format("int a; /* first line\n" 17075 " * second\n" 17076 " * line third\n" 17077 " * line\n" 17078 " */", 17079 Style)); 17080 EXPECT_EQ("int a; // first line\n" 17081 " // second\n" 17082 " // line third\n" 17083 " // line", 17084 format("int a; // first line\n" 17085 " // second line\n" 17086 " // third line", 17087 Style)); 17088 17089 Style.PenaltyExcessCharacter = 90; 17090 verifyFormat("int a; // the comment", Style); 17091 EXPECT_EQ("int a; // the comment\n" 17092 " // aaa", 17093 format("int a; // the comment aaa", Style)); 17094 EXPECT_EQ("int a; /* first line\n" 17095 " * second line\n" 17096 " * third line\n" 17097 " */", 17098 format("int a; /* first line\n" 17099 " * second line\n" 17100 " * third line\n" 17101 " */", 17102 Style)); 17103 EXPECT_EQ("int a; // first line\n" 17104 " // second line\n" 17105 " // third line", 17106 format("int a; // first line\n" 17107 " // second line\n" 17108 " // third line", 17109 Style)); 17110 // FIXME: Investigate why this is not getting the same layout as the test 17111 // above. 17112 EXPECT_EQ("int a; /* first line\n" 17113 " * second line\n" 17114 " * third line\n" 17115 " */", 17116 format("int a; /* first line second line third line" 17117 "\n*/", 17118 Style)); 17119 17120 EXPECT_EQ("// foo bar baz bazfoo\n" 17121 "// foo bar foo bar\n", 17122 format("// foo bar baz bazfoo\n" 17123 "// foo bar foo bar\n", 17124 Style)); 17125 EXPECT_EQ("// foo bar baz bazfoo\n" 17126 "// foo bar foo bar\n", 17127 format("// foo bar baz bazfoo\n" 17128 "// foo bar foo bar\n", 17129 Style)); 17130 17131 // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the 17132 // next one. 17133 EXPECT_EQ("// foo bar baz bazfoo\n" 17134 "// bar foo bar\n", 17135 format("// foo bar baz bazfoo bar\n" 17136 "// foo bar\n", 17137 Style)); 17138 17139 EXPECT_EQ("// foo bar baz bazfoo\n" 17140 "// foo bar baz bazfoo\n" 17141 "// bar foo bar\n", 17142 format("// foo bar baz bazfoo\n" 17143 "// foo bar baz bazfoo bar\n" 17144 "// foo bar\n", 17145 Style)); 17146 17147 EXPECT_EQ("// foo bar baz bazfoo\n" 17148 "// foo bar baz bazfoo\n" 17149 "// bar foo bar\n", 17150 format("// foo bar baz bazfoo\n" 17151 "// foo bar baz bazfoo bar\n" 17152 "// foo bar\n", 17153 Style)); 17154 17155 // Make sure we do not keep protruding characters if strict mode reflow is 17156 // cheaper than keeping protruding characters. 17157 Style.ColumnLimit = 21; 17158 EXPECT_EQ( 17159 "// foo foo foo foo\n" 17160 "// foo foo foo foo\n" 17161 "// foo foo foo foo\n", 17162 format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", Style)); 17163 17164 EXPECT_EQ("int a = /* long block\n" 17165 " comment */\n" 17166 " 42;", 17167 format("int a = /* long block comment */ 42;", Style)); 17168 } 17169 17170 #define EXPECT_ALL_STYLES_EQUAL(Styles) \ 17171 for (size_t i = 1; i < Styles.size(); ++i) \ 17172 EXPECT_EQ(Styles[0], Styles[i]) \ 17173 << "Style #" << i << " of " << Styles.size() << " differs from Style #0" 17174 17175 TEST_F(FormatTest, GetsPredefinedStyleByName) { 17176 SmallVector<FormatStyle, 3> Styles; 17177 Styles.resize(3); 17178 17179 Styles[0] = getLLVMStyle(); 17180 EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1])); 17181 EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2])); 17182 EXPECT_ALL_STYLES_EQUAL(Styles); 17183 17184 Styles[0] = getGoogleStyle(); 17185 EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1])); 17186 EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2])); 17187 EXPECT_ALL_STYLES_EQUAL(Styles); 17188 17189 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 17190 EXPECT_TRUE( 17191 getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1])); 17192 EXPECT_TRUE( 17193 getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2])); 17194 EXPECT_ALL_STYLES_EQUAL(Styles); 17195 17196 Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp); 17197 EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1])); 17198 EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2])); 17199 EXPECT_ALL_STYLES_EQUAL(Styles); 17200 17201 Styles[0] = getMozillaStyle(); 17202 EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1])); 17203 EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2])); 17204 EXPECT_ALL_STYLES_EQUAL(Styles); 17205 17206 Styles[0] = getWebKitStyle(); 17207 EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1])); 17208 EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2])); 17209 EXPECT_ALL_STYLES_EQUAL(Styles); 17210 17211 Styles[0] = getGNUStyle(); 17212 EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1])); 17213 EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2])); 17214 EXPECT_ALL_STYLES_EQUAL(Styles); 17215 17216 EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0])); 17217 } 17218 17219 TEST_F(FormatTest, GetsCorrectBasedOnStyle) { 17220 SmallVector<FormatStyle, 8> Styles; 17221 Styles.resize(2); 17222 17223 Styles[0] = getGoogleStyle(); 17224 Styles[1] = getLLVMStyle(); 17225 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 17226 EXPECT_ALL_STYLES_EQUAL(Styles); 17227 17228 Styles.resize(5); 17229 Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript); 17230 Styles[1] = getLLVMStyle(); 17231 Styles[1].Language = FormatStyle::LK_JavaScript; 17232 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value()); 17233 17234 Styles[2] = getLLVMStyle(); 17235 Styles[2].Language = FormatStyle::LK_JavaScript; 17236 EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n" 17237 "BasedOnStyle: Google", 17238 &Styles[2]) 17239 .value()); 17240 17241 Styles[3] = getLLVMStyle(); 17242 Styles[3].Language = FormatStyle::LK_JavaScript; 17243 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n" 17244 "Language: JavaScript", 17245 &Styles[3]) 17246 .value()); 17247 17248 Styles[4] = getLLVMStyle(); 17249 Styles[4].Language = FormatStyle::LK_JavaScript; 17250 EXPECT_EQ(0, parseConfiguration("---\n" 17251 "BasedOnStyle: LLVM\n" 17252 "IndentWidth: 123\n" 17253 "---\n" 17254 "BasedOnStyle: Google\n" 17255 "Language: JavaScript", 17256 &Styles[4]) 17257 .value()); 17258 EXPECT_ALL_STYLES_EQUAL(Styles); 17259 } 17260 17261 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME) \ 17262 Style.FIELD = false; \ 17263 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value()); \ 17264 EXPECT_TRUE(Style.FIELD); \ 17265 EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value()); \ 17266 EXPECT_FALSE(Style.FIELD); 17267 17268 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD) 17269 17270 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME) \ 17271 Style.STRUCT.FIELD = false; \ 17272 EXPECT_EQ(0, \ 17273 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": true", &Style) \ 17274 .value()); \ 17275 EXPECT_TRUE(Style.STRUCT.FIELD); \ 17276 EXPECT_EQ(0, \ 17277 parseConfiguration(#STRUCT ":\n " CONFIG_NAME ": false", &Style) \ 17278 .value()); \ 17279 EXPECT_FALSE(Style.STRUCT.FIELD); 17280 17281 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD) \ 17282 CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD) 17283 17284 #define CHECK_PARSE(TEXT, FIELD, VALUE) \ 17285 EXPECT_NE(VALUE, Style.FIELD) << "Initial value already the same!"; \ 17286 EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value()); \ 17287 EXPECT_EQ(VALUE, Style.FIELD) << "Unexpected value after parsing!" 17288 17289 TEST_F(FormatTest, ParsesConfigurationBools) { 17290 FormatStyle Style = {}; 17291 Style.Language = FormatStyle::LK_Cpp; 17292 CHECK_PARSE_BOOL(AlignTrailingComments); 17293 CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine); 17294 CHECK_PARSE_BOOL(AllowAllConstructorInitializersOnNextLine); 17295 CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); 17296 CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); 17297 CHECK_PARSE_BOOL(AllowShortEnumsOnASingleLine); 17298 CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); 17299 CHECK_PARSE_BOOL(BinPackArguments); 17300 CHECK_PARSE_BOOL(BinPackParameters); 17301 CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); 17302 CHECK_PARSE_BOOL(BreakBeforeConceptDeclarations); 17303 CHECK_PARSE_BOOL(BreakBeforeTernaryOperators); 17304 CHECK_PARSE_BOOL(BreakStringLiterals); 17305 CHECK_PARSE_BOOL(CompactNamespaces); 17306 CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); 17307 CHECK_PARSE_BOOL(DeriveLineEnding); 17308 CHECK_PARSE_BOOL(DerivePointerAlignment); 17309 CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding"); 17310 CHECK_PARSE_BOOL(DisableFormat); 17311 CHECK_PARSE_BOOL(IndentAccessModifiers); 17312 CHECK_PARSE_BOOL(IndentCaseLabels); 17313 CHECK_PARSE_BOOL(IndentCaseBlocks); 17314 CHECK_PARSE_BOOL(IndentGotoLabels); 17315 CHECK_PARSE_BOOL(IndentRequires); 17316 CHECK_PARSE_BOOL(IndentWrappedFunctionNames); 17317 CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); 17318 CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); 17319 CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); 17320 CHECK_PARSE_BOOL(Cpp11BracedListStyle); 17321 CHECK_PARSE_BOOL(ReflowComments); 17322 CHECK_PARSE_BOOL(SortUsingDeclarations); 17323 CHECK_PARSE_BOOL(SpacesInParentheses); 17324 CHECK_PARSE_BOOL(SpacesInSquareBrackets); 17325 CHECK_PARSE_BOOL(SpacesInConditionalStatement); 17326 CHECK_PARSE_BOOL(SpaceInEmptyBlock); 17327 CHECK_PARSE_BOOL(SpaceInEmptyParentheses); 17328 CHECK_PARSE_BOOL(SpacesInContainerLiterals); 17329 CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses); 17330 CHECK_PARSE_BOOL(SpaceAfterCStyleCast); 17331 CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); 17332 CHECK_PARSE_BOOL(SpaceAfterLogicalNot); 17333 CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); 17334 CHECK_PARSE_BOOL(SpaceBeforeCaseColon); 17335 CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList); 17336 CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon); 17337 CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon); 17338 CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon); 17339 CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets); 17340 CHECK_PARSE_BOOL(UseCRLF); 17341 17342 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel); 17343 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass); 17344 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum); 17345 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction); 17346 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace); 17347 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration); 17348 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct); 17349 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion); 17350 CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock); 17351 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch); 17352 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse); 17353 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeLambdaBody); 17354 CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeWhile); 17355 CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces); 17356 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction); 17357 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord); 17358 CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace); 17359 } 17360 17361 #undef CHECK_PARSE_BOOL 17362 17363 TEST_F(FormatTest, ParsesConfiguration) { 17364 FormatStyle Style = {}; 17365 Style.Language = FormatStyle::LK_Cpp; 17366 CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); 17367 CHECK_PARSE("ConstructorInitializerIndentWidth: 1234", 17368 ConstructorInitializerIndentWidth, 1234u); 17369 CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u); 17370 CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); 17371 CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u); 17372 CHECK_PARSE("PenaltyBreakAssignment: 1234", PenaltyBreakAssignment, 1234u); 17373 CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234", 17374 PenaltyBreakBeforeFirstCallParameter, 1234u); 17375 CHECK_PARSE("PenaltyBreakTemplateDeclaration: 1234", 17376 PenaltyBreakTemplateDeclaration, 1234u); 17377 CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u); 17378 CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234", 17379 PenaltyReturnTypeOnItsOwnLine, 1234u); 17380 CHECK_PARSE("SpacesBeforeTrailingComments: 1234", 17381 SpacesBeforeTrailingComments, 1234u); 17382 CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); 17383 CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u); 17384 CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$"); 17385 17386 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 17387 CHECK_PARSE("AlignConsecutiveAssignments: None", AlignConsecutiveAssignments, 17388 FormatStyle::ACS_None); 17389 CHECK_PARSE("AlignConsecutiveAssignments: Consecutive", 17390 AlignConsecutiveAssignments, FormatStyle::ACS_Consecutive); 17391 CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLines", 17392 AlignConsecutiveAssignments, FormatStyle::ACS_AcrossEmptyLines); 17393 CHECK_PARSE("AlignConsecutiveAssignments: AcrossEmptyLinesAndComments", 17394 AlignConsecutiveAssignments, 17395 FormatStyle::ACS_AcrossEmptyLinesAndComments); 17396 // For backwards compability, false / true should still parse 17397 CHECK_PARSE("AlignConsecutiveAssignments: false", AlignConsecutiveAssignments, 17398 FormatStyle::ACS_None); 17399 CHECK_PARSE("AlignConsecutiveAssignments: true", AlignConsecutiveAssignments, 17400 FormatStyle::ACS_Consecutive); 17401 17402 Style.AlignConsecutiveBitFields = FormatStyle::ACS_Consecutive; 17403 CHECK_PARSE("AlignConsecutiveBitFields: None", AlignConsecutiveBitFields, 17404 FormatStyle::ACS_None); 17405 CHECK_PARSE("AlignConsecutiveBitFields: Consecutive", 17406 AlignConsecutiveBitFields, FormatStyle::ACS_Consecutive); 17407 CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLines", 17408 AlignConsecutiveBitFields, FormatStyle::ACS_AcrossEmptyLines); 17409 CHECK_PARSE("AlignConsecutiveBitFields: AcrossEmptyLinesAndComments", 17410 AlignConsecutiveBitFields, 17411 FormatStyle::ACS_AcrossEmptyLinesAndComments); 17412 // For backwards compability, false / true should still parse 17413 CHECK_PARSE("AlignConsecutiveBitFields: false", AlignConsecutiveBitFields, 17414 FormatStyle::ACS_None); 17415 CHECK_PARSE("AlignConsecutiveBitFields: true", AlignConsecutiveBitFields, 17416 FormatStyle::ACS_Consecutive); 17417 17418 Style.AlignConsecutiveMacros = FormatStyle::ACS_Consecutive; 17419 CHECK_PARSE("AlignConsecutiveMacros: None", AlignConsecutiveMacros, 17420 FormatStyle::ACS_None); 17421 CHECK_PARSE("AlignConsecutiveMacros: Consecutive", AlignConsecutiveMacros, 17422 FormatStyle::ACS_Consecutive); 17423 CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLines", 17424 AlignConsecutiveMacros, FormatStyle::ACS_AcrossEmptyLines); 17425 CHECK_PARSE("AlignConsecutiveMacros: AcrossEmptyLinesAndComments", 17426 AlignConsecutiveMacros, 17427 FormatStyle::ACS_AcrossEmptyLinesAndComments); 17428 // For backwards compability, false / true should still parse 17429 CHECK_PARSE("AlignConsecutiveMacros: false", AlignConsecutiveMacros, 17430 FormatStyle::ACS_None); 17431 CHECK_PARSE("AlignConsecutiveMacros: true", AlignConsecutiveMacros, 17432 FormatStyle::ACS_Consecutive); 17433 17434 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 17435 CHECK_PARSE("AlignConsecutiveDeclarations: None", 17436 AlignConsecutiveDeclarations, FormatStyle::ACS_None); 17437 CHECK_PARSE("AlignConsecutiveDeclarations: Consecutive", 17438 AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive); 17439 CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLines", 17440 AlignConsecutiveDeclarations, FormatStyle::ACS_AcrossEmptyLines); 17441 CHECK_PARSE("AlignConsecutiveDeclarations: AcrossEmptyLinesAndComments", 17442 AlignConsecutiveDeclarations, 17443 FormatStyle::ACS_AcrossEmptyLinesAndComments); 17444 // For backwards compability, false / true should still parse 17445 CHECK_PARSE("AlignConsecutiveDeclarations: false", 17446 AlignConsecutiveDeclarations, FormatStyle::ACS_None); 17447 CHECK_PARSE("AlignConsecutiveDeclarations: true", 17448 AlignConsecutiveDeclarations, FormatStyle::ACS_Consecutive); 17449 17450 Style.PointerAlignment = FormatStyle::PAS_Middle; 17451 CHECK_PARSE("PointerAlignment: Left", PointerAlignment, 17452 FormatStyle::PAS_Left); 17453 CHECK_PARSE("PointerAlignment: Right", PointerAlignment, 17454 FormatStyle::PAS_Right); 17455 CHECK_PARSE("PointerAlignment: Middle", PointerAlignment, 17456 FormatStyle::PAS_Middle); 17457 // For backward compatibility: 17458 CHECK_PARSE("PointerBindsToType: Left", PointerAlignment, 17459 FormatStyle::PAS_Left); 17460 CHECK_PARSE("PointerBindsToType: Right", PointerAlignment, 17461 FormatStyle::PAS_Right); 17462 CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment, 17463 FormatStyle::PAS_Middle); 17464 17465 Style.Standard = FormatStyle::LS_Auto; 17466 CHECK_PARSE("Standard: c++03", Standard, FormatStyle::LS_Cpp03); 17467 CHECK_PARSE("Standard: c++11", Standard, FormatStyle::LS_Cpp11); 17468 CHECK_PARSE("Standard: c++14", Standard, FormatStyle::LS_Cpp14); 17469 CHECK_PARSE("Standard: c++17", Standard, FormatStyle::LS_Cpp17); 17470 CHECK_PARSE("Standard: c++20", Standard, FormatStyle::LS_Cpp20); 17471 CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto); 17472 CHECK_PARSE("Standard: Latest", Standard, FormatStyle::LS_Latest); 17473 // Legacy aliases: 17474 CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03); 17475 CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Latest); 17476 CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03); 17477 CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11); 17478 17479 Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; 17480 CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment", 17481 BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment); 17482 CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators, 17483 FormatStyle::BOS_None); 17484 CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators, 17485 FormatStyle::BOS_All); 17486 // For backward compatibility: 17487 CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators, 17488 FormatStyle::BOS_None); 17489 CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators, 17490 FormatStyle::BOS_All); 17491 17492 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 17493 CHECK_PARSE("BreakConstructorInitializers: BeforeComma", 17494 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 17495 CHECK_PARSE("BreakConstructorInitializers: AfterColon", 17496 BreakConstructorInitializers, FormatStyle::BCIS_AfterColon); 17497 CHECK_PARSE("BreakConstructorInitializers: BeforeColon", 17498 BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon); 17499 // For backward compatibility: 17500 CHECK_PARSE("BreakConstructorInitializersBeforeComma: true", 17501 BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma); 17502 17503 Style.BreakInheritanceList = FormatStyle::BILS_BeforeColon; 17504 CHECK_PARSE("BreakInheritanceList: AfterComma", BreakInheritanceList, 17505 FormatStyle::BILS_AfterComma); 17506 CHECK_PARSE("BreakInheritanceList: BeforeComma", BreakInheritanceList, 17507 FormatStyle::BILS_BeforeComma); 17508 CHECK_PARSE("BreakInheritanceList: AfterColon", BreakInheritanceList, 17509 FormatStyle::BILS_AfterColon); 17510 CHECK_PARSE("BreakInheritanceList: BeforeColon", BreakInheritanceList, 17511 FormatStyle::BILS_BeforeColon); 17512 // For backward compatibility: 17513 CHECK_PARSE("BreakBeforeInheritanceComma: true", BreakInheritanceList, 17514 FormatStyle::BILS_BeforeComma); 17515 17516 Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock; 17517 CHECK_PARSE("EmptyLineBeforeAccessModifier: Never", 17518 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Never); 17519 CHECK_PARSE("EmptyLineBeforeAccessModifier: Leave", 17520 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Leave); 17521 CHECK_PARSE("EmptyLineBeforeAccessModifier: LogicalBlock", 17522 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_LogicalBlock); 17523 CHECK_PARSE("EmptyLineBeforeAccessModifier: Always", 17524 EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Always); 17525 17526 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 17527 CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket, 17528 FormatStyle::BAS_Align); 17529 CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket, 17530 FormatStyle::BAS_DontAlign); 17531 CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket, 17532 FormatStyle::BAS_AlwaysBreak); 17533 // For backward compatibility: 17534 CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket, 17535 FormatStyle::BAS_DontAlign); 17536 CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket, 17537 FormatStyle::BAS_Align); 17538 17539 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left; 17540 CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines, 17541 FormatStyle::ENAS_DontAlign); 17542 CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines, 17543 FormatStyle::ENAS_Left); 17544 CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines, 17545 FormatStyle::ENAS_Right); 17546 // For backward compatibility: 17547 CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines, 17548 FormatStyle::ENAS_Left); 17549 CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines, 17550 FormatStyle::ENAS_Right); 17551 17552 Style.AlignOperands = FormatStyle::OAS_Align; 17553 CHECK_PARSE("AlignOperands: DontAlign", AlignOperands, 17554 FormatStyle::OAS_DontAlign); 17555 CHECK_PARSE("AlignOperands: Align", AlignOperands, FormatStyle::OAS_Align); 17556 CHECK_PARSE("AlignOperands: AlignAfterOperator", AlignOperands, 17557 FormatStyle::OAS_AlignAfterOperator); 17558 // For backward compatibility: 17559 CHECK_PARSE("AlignOperands: false", AlignOperands, 17560 FormatStyle::OAS_DontAlign); 17561 CHECK_PARSE("AlignOperands: true", AlignOperands, FormatStyle::OAS_Align); 17562 17563 Style.UseTab = FormatStyle::UT_ForIndentation; 17564 CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never); 17565 CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation); 17566 CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always); 17567 CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab, 17568 FormatStyle::UT_ForContinuationAndIndentation); 17569 CHECK_PARSE("UseTab: AlignWithSpaces", UseTab, 17570 FormatStyle::UT_AlignWithSpaces); 17571 // For backward compatibility: 17572 CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never); 17573 CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always); 17574 17575 Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; 17576 CHECK_PARSE("AllowShortBlocksOnASingleLine: Never", 17577 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 17578 CHECK_PARSE("AllowShortBlocksOnASingleLine: Empty", 17579 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Empty); 17580 CHECK_PARSE("AllowShortBlocksOnASingleLine: Always", 17581 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always); 17582 // For backward compatibility: 17583 CHECK_PARSE("AllowShortBlocksOnASingleLine: false", 17584 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Never); 17585 CHECK_PARSE("AllowShortBlocksOnASingleLine: true", 17586 AllowShortBlocksOnASingleLine, FormatStyle::SBS_Always); 17587 17588 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline; 17589 CHECK_PARSE("AllowShortFunctionsOnASingleLine: None", 17590 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 17591 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline", 17592 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline); 17593 CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty", 17594 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty); 17595 CHECK_PARSE("AllowShortFunctionsOnASingleLine: All", 17596 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 17597 // For backward compatibility: 17598 CHECK_PARSE("AllowShortFunctionsOnASingleLine: false", 17599 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None); 17600 CHECK_PARSE("AllowShortFunctionsOnASingleLine: true", 17601 AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All); 17602 17603 Style.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Both; 17604 CHECK_PARSE("SpaceAroundPointerQualifiers: Default", 17605 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Default); 17606 CHECK_PARSE("SpaceAroundPointerQualifiers: Before", 17607 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Before); 17608 CHECK_PARSE("SpaceAroundPointerQualifiers: After", 17609 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_After); 17610 CHECK_PARSE("SpaceAroundPointerQualifiers: Both", 17611 SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Both); 17612 17613 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 17614 CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens, 17615 FormatStyle::SBPO_Never); 17616 CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens, 17617 FormatStyle::SBPO_Always); 17618 CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens, 17619 FormatStyle::SBPO_ControlStatements); 17620 CHECK_PARSE("SpaceBeforeParens: NonEmptyParentheses", SpaceBeforeParens, 17621 FormatStyle::SBPO_NonEmptyParentheses); 17622 // For backward compatibility: 17623 CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens, 17624 FormatStyle::SBPO_Never); 17625 CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens, 17626 FormatStyle::SBPO_ControlStatements); 17627 17628 Style.ColumnLimit = 123; 17629 FormatStyle BaseStyle = getLLVMStyle(); 17630 CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit); 17631 CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u); 17632 17633 Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup; 17634 CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces, 17635 FormatStyle::BS_Attach); 17636 CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces, 17637 FormatStyle::BS_Linux); 17638 CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces, 17639 FormatStyle::BS_Mozilla); 17640 CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces, 17641 FormatStyle::BS_Stroustrup); 17642 CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces, 17643 FormatStyle::BS_Allman); 17644 CHECK_PARSE("BreakBeforeBraces: Whitesmiths", BreakBeforeBraces, 17645 FormatStyle::BS_Whitesmiths); 17646 CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU); 17647 CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces, 17648 FormatStyle::BS_WebKit); 17649 CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces, 17650 FormatStyle::BS_Custom); 17651 17652 Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never; 17653 CHECK_PARSE("BraceWrapping:\n" 17654 " AfterControlStatement: MultiLine", 17655 BraceWrapping.AfterControlStatement, 17656 FormatStyle::BWACS_MultiLine); 17657 CHECK_PARSE("BraceWrapping:\n" 17658 " AfterControlStatement: Always", 17659 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always); 17660 CHECK_PARSE("BraceWrapping:\n" 17661 " AfterControlStatement: Never", 17662 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never); 17663 // For backward compatibility: 17664 CHECK_PARSE("BraceWrapping:\n" 17665 " AfterControlStatement: true", 17666 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Always); 17667 CHECK_PARSE("BraceWrapping:\n" 17668 " AfterControlStatement: false", 17669 BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never); 17670 17671 Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; 17672 CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, 17673 FormatStyle::RTBS_None); 17674 CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType, 17675 FormatStyle::RTBS_All); 17676 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel", 17677 AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel); 17678 CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions", 17679 AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions); 17680 CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions", 17681 AlwaysBreakAfterReturnType, 17682 FormatStyle::RTBS_TopLevelDefinitions); 17683 17684 Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 17685 CHECK_PARSE("AlwaysBreakTemplateDeclarations: No", 17686 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_No); 17687 CHECK_PARSE("AlwaysBreakTemplateDeclarations: MultiLine", 17688 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine); 17689 CHECK_PARSE("AlwaysBreakTemplateDeclarations: Yes", 17690 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes); 17691 CHECK_PARSE("AlwaysBreakTemplateDeclarations: false", 17692 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_MultiLine); 17693 CHECK_PARSE("AlwaysBreakTemplateDeclarations: true", 17694 AlwaysBreakTemplateDeclarations, FormatStyle::BTDS_Yes); 17695 17696 Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; 17697 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None", 17698 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None); 17699 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All", 17700 AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All); 17701 CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel", 17702 AlwaysBreakAfterDefinitionReturnType, 17703 FormatStyle::DRTBS_TopLevel); 17704 17705 Style.NamespaceIndentation = FormatStyle::NI_All; 17706 CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation, 17707 FormatStyle::NI_None); 17708 CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation, 17709 FormatStyle::NI_Inner); 17710 CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation, 17711 FormatStyle::NI_All); 17712 17713 Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_OnlyFirstIf; 17714 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Never", 17715 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never); 17716 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: WithoutElse", 17717 AllowShortIfStatementsOnASingleLine, 17718 FormatStyle::SIS_WithoutElse); 17719 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: OnlyFirstIf", 17720 AllowShortIfStatementsOnASingleLine, 17721 FormatStyle::SIS_OnlyFirstIf); 17722 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: AllIfsAndElse", 17723 AllowShortIfStatementsOnASingleLine, 17724 FormatStyle::SIS_AllIfsAndElse); 17725 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: Always", 17726 AllowShortIfStatementsOnASingleLine, 17727 FormatStyle::SIS_OnlyFirstIf); 17728 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: false", 17729 AllowShortIfStatementsOnASingleLine, FormatStyle::SIS_Never); 17730 CHECK_PARSE("AllowShortIfStatementsOnASingleLine: true", 17731 AllowShortIfStatementsOnASingleLine, 17732 FormatStyle::SIS_WithoutElse); 17733 17734 Style.IndentExternBlock = FormatStyle::IEBS_NoIndent; 17735 CHECK_PARSE("IndentExternBlock: AfterExternBlock", IndentExternBlock, 17736 FormatStyle::IEBS_AfterExternBlock); 17737 CHECK_PARSE("IndentExternBlock: Indent", IndentExternBlock, 17738 FormatStyle::IEBS_Indent); 17739 CHECK_PARSE("IndentExternBlock: NoIndent", IndentExternBlock, 17740 FormatStyle::IEBS_NoIndent); 17741 CHECK_PARSE("IndentExternBlock: true", IndentExternBlock, 17742 FormatStyle::IEBS_Indent); 17743 CHECK_PARSE("IndentExternBlock: false", IndentExternBlock, 17744 FormatStyle::IEBS_NoIndent); 17745 17746 Style.BitFieldColonSpacing = FormatStyle::BFCS_None; 17747 CHECK_PARSE("BitFieldColonSpacing: Both", BitFieldColonSpacing, 17748 FormatStyle::BFCS_Both); 17749 CHECK_PARSE("BitFieldColonSpacing: None", BitFieldColonSpacing, 17750 FormatStyle::BFCS_None); 17751 CHECK_PARSE("BitFieldColonSpacing: Before", BitFieldColonSpacing, 17752 FormatStyle::BFCS_Before); 17753 CHECK_PARSE("BitFieldColonSpacing: After", BitFieldColonSpacing, 17754 FormatStyle::BFCS_After); 17755 17756 Style.SortJavaStaticImport = FormatStyle::SJSIO_Before; 17757 CHECK_PARSE("SortJavaStaticImport: After", SortJavaStaticImport, 17758 FormatStyle::SJSIO_After); 17759 CHECK_PARSE("SortJavaStaticImport: Before", SortJavaStaticImport, 17760 FormatStyle::SJSIO_Before); 17761 17762 // FIXME: This is required because parsing a configuration simply overwrites 17763 // the first N elements of the list instead of resetting it. 17764 Style.ForEachMacros.clear(); 17765 std::vector<std::string> BoostForeach; 17766 BoostForeach.push_back("BOOST_FOREACH"); 17767 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach); 17768 std::vector<std::string> BoostAndQForeach; 17769 BoostAndQForeach.push_back("BOOST_FOREACH"); 17770 BoostAndQForeach.push_back("Q_FOREACH"); 17771 CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros, 17772 BoostAndQForeach); 17773 17774 Style.AttributeMacros.clear(); 17775 CHECK_PARSE("BasedOnStyle: LLVM", AttributeMacros, 17776 std::vector<std::string>{"__capability"}); 17777 CHECK_PARSE("AttributeMacros: [attr1, attr2]", AttributeMacros, 17778 std::vector<std::string>({"attr1", "attr2"})); 17779 17780 Style.StatementAttributeLikeMacros.clear(); 17781 CHECK_PARSE("StatementAttributeLikeMacros: [emit,Q_EMIT]", 17782 StatementAttributeLikeMacros, 17783 std::vector<std::string>({"emit", "Q_EMIT"})); 17784 17785 Style.StatementMacros.clear(); 17786 CHECK_PARSE("StatementMacros: [QUNUSED]", StatementMacros, 17787 std::vector<std::string>{"QUNUSED"}); 17788 CHECK_PARSE("StatementMacros: [QUNUSED, QT_REQUIRE_VERSION]", StatementMacros, 17789 std::vector<std::string>({"QUNUSED", "QT_REQUIRE_VERSION"})); 17790 17791 Style.NamespaceMacros.clear(); 17792 CHECK_PARSE("NamespaceMacros: [TESTSUITE]", NamespaceMacros, 17793 std::vector<std::string>{"TESTSUITE"}); 17794 CHECK_PARSE("NamespaceMacros: [TESTSUITE, SUITE]", NamespaceMacros, 17795 std::vector<std::string>({"TESTSUITE", "SUITE"})); 17796 17797 Style.WhitespaceSensitiveMacros.clear(); 17798 CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE]", 17799 WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"}); 17800 CHECK_PARSE("WhitespaceSensitiveMacros: [STRINGIZE, ASSERT]", 17801 WhitespaceSensitiveMacros, 17802 std::vector<std::string>({"STRINGIZE", "ASSERT"})); 17803 Style.WhitespaceSensitiveMacros.clear(); 17804 CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE']", 17805 WhitespaceSensitiveMacros, std::vector<std::string>{"STRINGIZE"}); 17806 CHECK_PARSE("WhitespaceSensitiveMacros: ['STRINGIZE', 'ASSERT']", 17807 WhitespaceSensitiveMacros, 17808 std::vector<std::string>({"STRINGIZE", "ASSERT"})); 17809 17810 Style.IncludeStyle.IncludeCategories.clear(); 17811 std::vector<tooling::IncludeStyle::IncludeCategory> ExpectedCategories = { 17812 {"abc/.*", 2, 0, false}, {".*", 1, 0, true}}; 17813 CHECK_PARSE("IncludeCategories:\n" 17814 " - Regex: abc/.*\n" 17815 " Priority: 2\n" 17816 " - Regex: .*\n" 17817 " Priority: 1\n" 17818 " CaseSensitive: true\n", 17819 IncludeStyle.IncludeCategories, ExpectedCategories); 17820 CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeStyle.IncludeIsMainRegex, 17821 "abc$"); 17822 CHECK_PARSE("IncludeIsMainSourceRegex: 'abc$'", 17823 IncludeStyle.IncludeIsMainSourceRegex, "abc$"); 17824 17825 Style.SortIncludes = FormatStyle::SI_Never; 17826 CHECK_PARSE("SortIncludes: true", SortIncludes, 17827 FormatStyle::SI_CaseSensitive); 17828 CHECK_PARSE("SortIncludes: false", SortIncludes, FormatStyle::SI_Never); 17829 CHECK_PARSE("SortIncludes: CaseInsensitive", SortIncludes, 17830 FormatStyle::SI_CaseInsensitive); 17831 CHECK_PARSE("SortIncludes: CaseSensitive", SortIncludes, 17832 FormatStyle::SI_CaseSensitive); 17833 CHECK_PARSE("SortIncludes: Never", SortIncludes, FormatStyle::SI_Never); 17834 17835 Style.RawStringFormats.clear(); 17836 std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = { 17837 { 17838 FormatStyle::LK_TextProto, 17839 {"pb", "proto"}, 17840 {"PARSE_TEXT_PROTO"}, 17841 /*CanonicalDelimiter=*/"", 17842 "llvm", 17843 }, 17844 { 17845 FormatStyle::LK_Cpp, 17846 {"cc", "cpp"}, 17847 {"C_CODEBLOCK", "CPPEVAL"}, 17848 /*CanonicalDelimiter=*/"cc", 17849 /*BasedOnStyle=*/"", 17850 }, 17851 }; 17852 17853 CHECK_PARSE("RawStringFormats:\n" 17854 " - Language: TextProto\n" 17855 " Delimiters:\n" 17856 " - 'pb'\n" 17857 " - 'proto'\n" 17858 " EnclosingFunctions:\n" 17859 " - 'PARSE_TEXT_PROTO'\n" 17860 " BasedOnStyle: llvm\n" 17861 " - Language: Cpp\n" 17862 " Delimiters:\n" 17863 " - 'cc'\n" 17864 " - 'cpp'\n" 17865 " EnclosingFunctions:\n" 17866 " - 'C_CODEBLOCK'\n" 17867 " - 'CPPEVAL'\n" 17868 " CanonicalDelimiter: 'cc'", 17869 RawStringFormats, ExpectedRawStringFormats); 17870 17871 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 17872 " Minimum: 0\n" 17873 " Maximum: 0", 17874 SpacesInLineCommentPrefix.Minimum, 0u); 17875 EXPECT_EQ(Style.SpacesInLineCommentPrefix.Maximum, 0u); 17876 Style.SpacesInLineCommentPrefix.Minimum = 1; 17877 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 17878 " Minimum: 2", 17879 SpacesInLineCommentPrefix.Minimum, 0u); 17880 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 17881 " Maximum: -1", 17882 SpacesInLineCommentPrefix.Maximum, -1u); 17883 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 17884 " Minimum: 2", 17885 SpacesInLineCommentPrefix.Minimum, 2u); 17886 CHECK_PARSE("SpacesInLineCommentPrefix:\n" 17887 " Maximum: 1", 17888 SpacesInLineCommentPrefix.Maximum, 1u); 17889 EXPECT_EQ(Style.SpacesInLineCommentPrefix.Minimum, 1u); 17890 17891 Style.SpacesInAngles = FormatStyle::SIAS_Always; 17892 CHECK_PARSE("SpacesInAngles: Never", SpacesInAngles, FormatStyle::SIAS_Never); 17893 CHECK_PARSE("SpacesInAngles: Always", SpacesInAngles, 17894 FormatStyle::SIAS_Always); 17895 CHECK_PARSE("SpacesInAngles: Leave", SpacesInAngles, FormatStyle::SIAS_Leave); 17896 // For backward compatibility: 17897 CHECK_PARSE("SpacesInAngles: false", SpacesInAngles, FormatStyle::SIAS_Never); 17898 CHECK_PARSE("SpacesInAngles: true", SpacesInAngles, FormatStyle::SIAS_Always); 17899 } 17900 17901 TEST_F(FormatTest, ParsesConfigurationWithLanguages) { 17902 FormatStyle Style = {}; 17903 Style.Language = FormatStyle::LK_Cpp; 17904 CHECK_PARSE("Language: Cpp\n" 17905 "IndentWidth: 12", 17906 IndentWidth, 12u); 17907 EXPECT_EQ(parseConfiguration("Language: JavaScript\n" 17908 "IndentWidth: 34", 17909 &Style), 17910 ParseError::Unsuitable); 17911 FormatStyle BinPackedTCS = {}; 17912 BinPackedTCS.Language = FormatStyle::LK_JavaScript; 17913 EXPECT_EQ(parseConfiguration("BinPackArguments: true\n" 17914 "InsertTrailingCommas: Wrapped", 17915 &BinPackedTCS), 17916 ParseError::BinPackTrailingCommaConflict); 17917 EXPECT_EQ(12u, Style.IndentWidth); 17918 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 17919 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 17920 17921 Style.Language = FormatStyle::LK_JavaScript; 17922 CHECK_PARSE("Language: JavaScript\n" 17923 "IndentWidth: 12", 17924 IndentWidth, 12u); 17925 CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u); 17926 EXPECT_EQ(parseConfiguration("Language: Cpp\n" 17927 "IndentWidth: 34", 17928 &Style), 17929 ParseError::Unsuitable); 17930 EXPECT_EQ(23u, Style.IndentWidth); 17931 CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u); 17932 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 17933 17934 CHECK_PARSE("BasedOnStyle: LLVM\n" 17935 "IndentWidth: 67", 17936 IndentWidth, 67u); 17937 17938 CHECK_PARSE("---\n" 17939 "Language: JavaScript\n" 17940 "IndentWidth: 12\n" 17941 "---\n" 17942 "Language: Cpp\n" 17943 "IndentWidth: 34\n" 17944 "...\n", 17945 IndentWidth, 12u); 17946 17947 Style.Language = FormatStyle::LK_Cpp; 17948 CHECK_PARSE("---\n" 17949 "Language: JavaScript\n" 17950 "IndentWidth: 12\n" 17951 "---\n" 17952 "Language: Cpp\n" 17953 "IndentWidth: 34\n" 17954 "...\n", 17955 IndentWidth, 34u); 17956 CHECK_PARSE("---\n" 17957 "IndentWidth: 78\n" 17958 "---\n" 17959 "Language: JavaScript\n" 17960 "IndentWidth: 56\n" 17961 "...\n", 17962 IndentWidth, 78u); 17963 17964 Style.ColumnLimit = 123; 17965 Style.IndentWidth = 234; 17966 Style.BreakBeforeBraces = FormatStyle::BS_Linux; 17967 Style.TabWidth = 345; 17968 EXPECT_FALSE(parseConfiguration("---\n" 17969 "IndentWidth: 456\n" 17970 "BreakBeforeBraces: Allman\n" 17971 "---\n" 17972 "Language: JavaScript\n" 17973 "IndentWidth: 111\n" 17974 "TabWidth: 111\n" 17975 "---\n" 17976 "Language: Cpp\n" 17977 "BreakBeforeBraces: Stroustrup\n" 17978 "TabWidth: 789\n" 17979 "...\n", 17980 &Style)); 17981 EXPECT_EQ(123u, Style.ColumnLimit); 17982 EXPECT_EQ(456u, Style.IndentWidth); 17983 EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces); 17984 EXPECT_EQ(789u, Style.TabWidth); 17985 17986 EXPECT_EQ(parseConfiguration("---\n" 17987 "Language: JavaScript\n" 17988 "IndentWidth: 56\n" 17989 "---\n" 17990 "IndentWidth: 78\n" 17991 "...\n", 17992 &Style), 17993 ParseError::Error); 17994 EXPECT_EQ(parseConfiguration("---\n" 17995 "Language: JavaScript\n" 17996 "IndentWidth: 56\n" 17997 "---\n" 17998 "Language: JavaScript\n" 17999 "IndentWidth: 78\n" 18000 "...\n", 18001 &Style), 18002 ParseError::Error); 18003 18004 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); 18005 } 18006 18007 #undef CHECK_PARSE 18008 18009 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) { 18010 FormatStyle Style = {}; 18011 Style.Language = FormatStyle::LK_JavaScript; 18012 Style.BreakBeforeTernaryOperators = true; 18013 EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value()); 18014 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 18015 18016 Style.BreakBeforeTernaryOperators = true; 18017 EXPECT_EQ(0, parseConfiguration("---\n" 18018 "BasedOnStyle: Google\n" 18019 "---\n" 18020 "Language: JavaScript\n" 18021 "IndentWidth: 76\n" 18022 "...\n", 18023 &Style) 18024 .value()); 18025 EXPECT_FALSE(Style.BreakBeforeTernaryOperators); 18026 EXPECT_EQ(76u, Style.IndentWidth); 18027 EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language); 18028 } 18029 18030 TEST_F(FormatTest, ConfigurationRoundTripTest) { 18031 FormatStyle Style = getLLVMStyle(); 18032 std::string YAML = configurationAsText(Style); 18033 FormatStyle ParsedStyle = {}; 18034 ParsedStyle.Language = FormatStyle::LK_Cpp; 18035 EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value()); 18036 EXPECT_EQ(Style, ParsedStyle); 18037 } 18038 18039 TEST_F(FormatTest, WorksFor8bitEncodings) { 18040 EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" 18041 "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" 18042 "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" 18043 "\"\xef\xee\xf0\xf3...\"", 18044 format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " 18045 "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " 18046 "\xef\xee\xf0\xf3...\"", 18047 getLLVMStyleWithColumns(12))); 18048 } 18049 18050 TEST_F(FormatTest, HandlesUTF8BOM) { 18051 EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); 18052 EXPECT_EQ("\xef\xbb\xbf#include <iostream>", 18053 format("\xef\xbb\xbf#include <iostream>")); 18054 EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>", 18055 format("\xef\xbb\xbf\n#include <iostream>")); 18056 } 18057 18058 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. 18059 #if !defined(_MSC_VER) 18060 18061 TEST_F(FormatTest, CountsUTF8CharactersProperly) { 18062 verifyFormat("\"Однажды в студёную зимнюю пору...\"", 18063 getLLVMStyleWithColumns(35)); 18064 verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", 18065 getLLVMStyleWithColumns(31)); 18066 verifyFormat("// Однажды в студёную зимнюю пору...", 18067 getLLVMStyleWithColumns(36)); 18068 verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); 18069 verifyFormat("/* Однажды в студёную зимнюю пору... */", 18070 getLLVMStyleWithColumns(39)); 18071 verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", 18072 getLLVMStyleWithColumns(35)); 18073 } 18074 18075 TEST_F(FormatTest, SplitsUTF8Strings) { 18076 // Non-printable characters' width is currently considered to be the length in 18077 // bytes in UTF8. The characters can be displayed in very different manner 18078 // (zero-width, single width with a substitution glyph, expanded to their code 18079 // (e.g. "<8d>"), so there's no single correct way to handle them. 18080 EXPECT_EQ("\"aaaaÄ\"\n" 18081 "\"\xc2\x8d\";", 18082 format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 18083 EXPECT_EQ("\"aaaaaaaÄ\"\n" 18084 "\"\xc2\x8d\";", 18085 format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); 18086 EXPECT_EQ("\"Однажды, в \"\n" 18087 "\"студёную \"\n" 18088 "\"зимнюю \"\n" 18089 "\"пору,\"", 18090 format("\"Однажды, в студёную зимнюю пору,\"", 18091 getLLVMStyleWithColumns(13))); 18092 EXPECT_EQ( 18093 "\"一 二 三 \"\n" 18094 "\"四 五六 \"\n" 18095 "\"七 八 九 \"\n" 18096 "\"十\"", 18097 format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); 18098 EXPECT_EQ("\"一\t\"\n" 18099 "\"二 \t\"\n" 18100 "\"三 四 \"\n" 18101 "\"五\t\"\n" 18102 "\"六 \t\"\n" 18103 "\"七 \"\n" 18104 "\"八九十\tqq\"", 18105 format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", 18106 getLLVMStyleWithColumns(11))); 18107 18108 // UTF8 character in an escape sequence. 18109 EXPECT_EQ("\"aaaaaa\"\n" 18110 "\"\\\xC2\x8D\"", 18111 format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); 18112 } 18113 18114 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { 18115 EXPECT_EQ("const char *sssss =\n" 18116 " \"一二三四五六七八\\\n" 18117 " 九 十\";", 18118 format("const char *sssss = \"一二三四五六七八\\\n" 18119 " 九 十\";", 18120 getLLVMStyleWithColumns(30))); 18121 } 18122 18123 TEST_F(FormatTest, SplitsUTF8LineComments) { 18124 EXPECT_EQ("// aaaaÄ\xc2\x8d", 18125 format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); 18126 EXPECT_EQ("// Я из лесу\n" 18127 "// вышел; был\n" 18128 "// сильный\n" 18129 "// мороз.", 18130 format("// Я из лесу вышел; был сильный мороз.", 18131 getLLVMStyleWithColumns(13))); 18132 EXPECT_EQ("// 一二三\n" 18133 "// 四五六七\n" 18134 "// 八 九\n" 18135 "// 十", 18136 format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); 18137 } 18138 18139 TEST_F(FormatTest, SplitsUTF8BlockComments) { 18140 EXPECT_EQ("/* Гляжу,\n" 18141 " * поднимается\n" 18142 " * медленно в\n" 18143 " * гору\n" 18144 " * Лошадка,\n" 18145 " * везущая\n" 18146 " * хворосту\n" 18147 " * воз. */", 18148 format("/* Гляжу, поднимается медленно в гору\n" 18149 " * Лошадка, везущая хворосту воз. */", 18150 getLLVMStyleWithColumns(13))); 18151 EXPECT_EQ( 18152 "/* 一二三\n" 18153 " * 四五六七\n" 18154 " * 八 九\n" 18155 " * 十 */", 18156 format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); 18157 EXPECT_EQ("/* \n" 18158 " * \n" 18159 " * - */", 18160 format("/* - */", getLLVMStyleWithColumns(12))); 18161 } 18162 18163 #endif // _MSC_VER 18164 18165 TEST_F(FormatTest, ConstructorInitializerIndentWidth) { 18166 FormatStyle Style = getLLVMStyle(); 18167 18168 Style.ConstructorInitializerIndentWidth = 4; 18169 verifyFormat( 18170 "SomeClass::Constructor()\n" 18171 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 18172 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 18173 Style); 18174 18175 Style.ConstructorInitializerIndentWidth = 2; 18176 verifyFormat( 18177 "SomeClass::Constructor()\n" 18178 " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 18179 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 18180 Style); 18181 18182 Style.ConstructorInitializerIndentWidth = 0; 18183 verifyFormat( 18184 "SomeClass::Constructor()\n" 18185 ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" 18186 " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", 18187 Style); 18188 Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; 18189 verifyFormat( 18190 "SomeLongTemplateVariableName<\n" 18191 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>", 18192 Style); 18193 verifyFormat("bool smaller = 1 < " 18194 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" 18195 " " 18196 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", 18197 Style); 18198 18199 Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; 18200 verifyFormat("SomeClass::Constructor() :\n" 18201 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa),\n" 18202 "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa) {}", 18203 Style); 18204 } 18205 18206 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) { 18207 FormatStyle Style = getLLVMStyle(); 18208 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; 18209 Style.ConstructorInitializerIndentWidth = 4; 18210 verifyFormat("SomeClass::Constructor()\n" 18211 " : a(a)\n" 18212 " , b(b)\n" 18213 " , c(c) {}", 18214 Style); 18215 verifyFormat("SomeClass::Constructor()\n" 18216 " : a(a) {}", 18217 Style); 18218 18219 Style.ColumnLimit = 0; 18220 verifyFormat("SomeClass::Constructor()\n" 18221 " : a(a) {}", 18222 Style); 18223 verifyFormat("SomeClass::Constructor() noexcept\n" 18224 " : a(a) {}", 18225 Style); 18226 verifyFormat("SomeClass::Constructor()\n" 18227 " : a(a)\n" 18228 " , b(b)\n" 18229 " , c(c) {}", 18230 Style); 18231 verifyFormat("SomeClass::Constructor()\n" 18232 " : a(a) {\n" 18233 " foo();\n" 18234 " bar();\n" 18235 "}", 18236 Style); 18237 18238 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 18239 verifyFormat("SomeClass::Constructor()\n" 18240 " : a(a)\n" 18241 " , b(b)\n" 18242 " , c(c) {\n}", 18243 Style); 18244 verifyFormat("SomeClass::Constructor()\n" 18245 " : a(a) {\n}", 18246 Style); 18247 18248 Style.ColumnLimit = 80; 18249 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; 18250 Style.ConstructorInitializerIndentWidth = 2; 18251 verifyFormat("SomeClass::Constructor()\n" 18252 " : a(a)\n" 18253 " , b(b)\n" 18254 " , c(c) {}", 18255 Style); 18256 18257 Style.ConstructorInitializerIndentWidth = 0; 18258 verifyFormat("SomeClass::Constructor()\n" 18259 ": a(a)\n" 18260 ", b(b)\n" 18261 ", c(c) {}", 18262 Style); 18263 18264 Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; 18265 Style.ConstructorInitializerIndentWidth = 4; 18266 verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style); 18267 verifyFormat( 18268 "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n", 18269 Style); 18270 verifyFormat( 18271 "SomeClass::Constructor()\n" 18272 " : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}", 18273 Style); 18274 Style.ConstructorInitializerIndentWidth = 4; 18275 Style.ColumnLimit = 60; 18276 verifyFormat("SomeClass::Constructor()\n" 18277 " : aaaaaaaa(aaaaaaaa)\n" 18278 " , aaaaaaaa(aaaaaaaa)\n" 18279 " , aaaaaaaa(aaaaaaaa) {}", 18280 Style); 18281 } 18282 18283 TEST_F(FormatTest, Destructors) { 18284 verifyFormat("void F(int &i) { i.~int(); }"); 18285 verifyFormat("void F(int &i) { i->~int(); }"); 18286 } 18287 18288 TEST_F(FormatTest, FormatsWithWebKitStyle) { 18289 FormatStyle Style = getWebKitStyle(); 18290 18291 // Don't indent in outer namespaces. 18292 verifyFormat("namespace outer {\n" 18293 "int i;\n" 18294 "namespace inner {\n" 18295 " int i;\n" 18296 "} // namespace inner\n" 18297 "} // namespace outer\n" 18298 "namespace other_outer {\n" 18299 "int i;\n" 18300 "}", 18301 Style); 18302 18303 // Don't indent case labels. 18304 verifyFormat("switch (variable) {\n" 18305 "case 1:\n" 18306 "case 2:\n" 18307 " doSomething();\n" 18308 " break;\n" 18309 "default:\n" 18310 " ++variable;\n" 18311 "}", 18312 Style); 18313 18314 // Wrap before binary operators. 18315 EXPECT_EQ("void f()\n" 18316 "{\n" 18317 " if (aaaaaaaaaaaaaaaa\n" 18318 " && bbbbbbbbbbbbbbbbbbbbbbbb\n" 18319 " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 18320 " return;\n" 18321 "}", 18322 format("void f() {\n" 18323 "if (aaaaaaaaaaaaaaaa\n" 18324 "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" 18325 "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" 18326 "return;\n" 18327 "}", 18328 Style)); 18329 18330 // Allow functions on a single line. 18331 verifyFormat("void f() { return; }", Style); 18332 18333 // Allow empty blocks on a single line and insert a space in empty blocks. 18334 EXPECT_EQ("void f() { }", format("void f() {}", Style)); 18335 EXPECT_EQ("while (true) { }", format("while (true) {}", Style)); 18336 // However, don't merge non-empty short loops. 18337 EXPECT_EQ("while (true) {\n" 18338 " continue;\n" 18339 "}", 18340 format("while (true) { continue; }", Style)); 18341 18342 // Constructor initializers are formatted one per line with the "," on the 18343 // new line. 18344 verifyFormat("Constructor()\n" 18345 " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 18346 " , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n" 18347 " aaaaaaaaaaaaaa)\n" 18348 " , aaaaaaaaaaaaaaaaaaaaaaa()\n" 18349 "{\n" 18350 "}", 18351 Style); 18352 verifyFormat("SomeClass::Constructor()\n" 18353 " : a(a)\n" 18354 "{\n" 18355 "}", 18356 Style); 18357 EXPECT_EQ("SomeClass::Constructor()\n" 18358 " : a(a)\n" 18359 "{\n" 18360 "}", 18361 format("SomeClass::Constructor():a(a){}", Style)); 18362 verifyFormat("SomeClass::Constructor()\n" 18363 " : a(a)\n" 18364 " , b(b)\n" 18365 " , c(c)\n" 18366 "{\n" 18367 "}", 18368 Style); 18369 verifyFormat("SomeClass::Constructor()\n" 18370 " : a(a)\n" 18371 "{\n" 18372 " foo();\n" 18373 " bar();\n" 18374 "}", 18375 Style); 18376 18377 // Access specifiers should be aligned left. 18378 verifyFormat("class C {\n" 18379 "public:\n" 18380 " int i;\n" 18381 "};", 18382 Style); 18383 18384 // Do not align comments. 18385 verifyFormat("int a; // Do not\n" 18386 "double b; // align comments.", 18387 Style); 18388 18389 // Do not align operands. 18390 EXPECT_EQ("ASSERT(aaaa\n" 18391 " || bbbb);", 18392 format("ASSERT ( aaaa\n||bbbb);", Style)); 18393 18394 // Accept input's line breaks. 18395 EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" 18396 " || bbbbbbbbbbbbbbb) {\n" 18397 " i++;\n" 18398 "}", 18399 format("if (aaaaaaaaaaaaaaa\n" 18400 "|| bbbbbbbbbbbbbbb) { i++; }", 18401 Style)); 18402 EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" 18403 " i++;\n" 18404 "}", 18405 format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); 18406 18407 // Don't automatically break all macro definitions (llvm.org/PR17842). 18408 verifyFormat("#define aNumber 10", Style); 18409 // However, generally keep the line breaks that the user authored. 18410 EXPECT_EQ("#define aNumber \\\n" 18411 " 10", 18412 format("#define aNumber \\\n" 18413 " 10", 18414 Style)); 18415 18416 // Keep empty and one-element array literals on a single line. 18417 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" 18418 " copyItems:YES];", 18419 format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" 18420 "copyItems:YES];", 18421 Style)); 18422 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" 18423 " copyItems:YES];", 18424 format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" 18425 " copyItems:YES];", 18426 Style)); 18427 // FIXME: This does not seem right, there should be more indentation before 18428 // the array literal's entries. Nested blocks have the same problem. 18429 EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 18430 " @\"a\",\n" 18431 " @\"a\"\n" 18432 "]\n" 18433 " copyItems:YES];", 18434 format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" 18435 " @\"a\",\n" 18436 " @\"a\"\n" 18437 " ]\n" 18438 " copyItems:YES];", 18439 Style)); 18440 EXPECT_EQ( 18441 "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 18442 " copyItems:YES];", 18443 format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" 18444 " copyItems:YES];", 18445 Style)); 18446 18447 verifyFormat("[self.a b:c c:d];", Style); 18448 EXPECT_EQ("[self.a b:c\n" 18449 " c:d];", 18450 format("[self.a b:c\n" 18451 "c:d];", 18452 Style)); 18453 } 18454 18455 TEST_F(FormatTest, FormatsLambdas) { 18456 verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n"); 18457 verifyFormat( 18458 "int c = [b]() mutable noexcept { return [&b] { return b++; }(); }();\n"); 18459 verifyFormat("int c = [&] { [=] { return b++; }(); }();\n"); 18460 verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n"); 18461 verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n"); 18462 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n"); 18463 verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n"); 18464 verifyFormat("auto c = [a = [b = 42] {}] {};\n"); 18465 verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n"); 18466 verifyFormat("int x = f(*+[] {});"); 18467 verifyFormat("void f() {\n" 18468 " other(x.begin(), x.end(), [&](int, int) { return 1; });\n" 18469 "}\n"); 18470 verifyFormat("void f() {\n" 18471 " other(x.begin(), //\n" 18472 " x.end(), //\n" 18473 " [&](int, int) { return 1; });\n" 18474 "}\n"); 18475 verifyFormat("void f() {\n" 18476 " other.other.other.other.other(\n" 18477 " x.begin(), x.end(),\n" 18478 " [something, rather](int, int, int, int, int, int, int) { " 18479 "return 1; });\n" 18480 "}\n"); 18481 verifyFormat( 18482 "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) {\n" 18486 " //\n" 18487 " });\n" 18488 "}\n"); 18489 verifyFormat("SomeFunction([]() { // A cool function...\n" 18490 " return 43;\n" 18491 "});"); 18492 EXPECT_EQ("SomeFunction([]() {\n" 18493 "#define A a\n" 18494 " return 43;\n" 18495 "});", 18496 format("SomeFunction([](){\n" 18497 "#define A a\n" 18498 "return 43;\n" 18499 "});")); 18500 verifyFormat("void f() {\n" 18501 " SomeFunction([](decltype(x), A *a) {});\n" 18502 " SomeFunction([](typeof(x), A *a) {});\n" 18503 " SomeFunction([](_Atomic(x), A *a) {});\n" 18504 " SomeFunction([](__underlying_type(x), A *a) {});\n" 18505 "}"); 18506 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 18507 " [](const aaaaaaaaaa &a) { return a; });"); 18508 verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n" 18509 " SomeOtherFunctioooooooooooooooooooooooooon();\n" 18510 "});"); 18511 verifyFormat("Constructor()\n" 18512 " : Field([] { // comment\n" 18513 " int i;\n" 18514 " }) {}"); 18515 verifyFormat("auto my_lambda = [](const string &some_parameter) {\n" 18516 " return some_parameter.size();\n" 18517 "};"); 18518 verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n" 18519 " [](const string &s) { return s; };"); 18520 verifyFormat("int i = aaaaaa ? 1 //\n" 18521 " : [] {\n" 18522 " return 2; //\n" 18523 " }();"); 18524 verifyFormat("llvm::errs() << \"number of twos is \"\n" 18525 " << std::count_if(v.begin(), v.end(), [](int x) {\n" 18526 " return x == 2; // force break\n" 18527 " });"); 18528 verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 18529 " [=](int iiiiiiiiiiii) {\n" 18530 " return aaaaaaaaaaaaaaaaaaaaaaa !=\n" 18531 " aaaaaaaaaaaaaaaaaaaaaaa;\n" 18532 " });", 18533 getLLVMStyleWithColumns(60)); 18534 verifyFormat("SomeFunction({[&] {\n" 18535 " // comment\n" 18536 " },\n" 18537 " [&] {\n" 18538 " // comment\n" 18539 " }});"); 18540 verifyFormat("SomeFunction({[&] {\n" 18541 " // comment\n" 18542 "}});"); 18543 verifyFormat( 18544 "virtual aaaaaaaaaaaaaaaa(\n" 18545 " std::function<bool()> bbbbbbbbbbbb = [&]() { return true; },\n" 18546 " aaaaa aaaaaaaaa);"); 18547 18548 // Lambdas with return types. 18549 verifyFormat("int c = []() -> int { return 2; }();\n"); 18550 verifyFormat("int c = []() -> int * { return 2; }();\n"); 18551 verifyFormat("int c = []() -> vector<int> { return {2}; }();\n"); 18552 verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());"); 18553 verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};"); 18554 verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};"); 18555 verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};"); 18556 verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};"); 18557 verifyFormat("[a, a]() -> a<1> {};"); 18558 verifyFormat("[]() -> foo<5 + 2> { return {}; };"); 18559 verifyFormat("[]() -> foo<5 - 2> { return {}; };"); 18560 verifyFormat("[]() -> foo<5 / 2> { return {}; };"); 18561 verifyFormat("[]() -> foo<5 * 2> { return {}; };"); 18562 verifyFormat("[]() -> foo<5 % 2> { return {}; };"); 18563 verifyFormat("[]() -> foo<5 << 2> { return {}; };"); 18564 verifyFormat("[]() -> foo<!5> { return {}; };"); 18565 verifyFormat("[]() -> foo<~5> { return {}; };"); 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 >= 2> { return {}; };"); 18573 verifyFormat("[]() -> foo<5 <= 2> { return {}; };"); 18574 verifyFormat("[]() -> foo<5 < 2> { return {}; };"); 18575 verifyFormat("[]() -> foo<2 ? 1 : 0> { return {}; };"); 18576 verifyFormat("namespace bar {\n" 18577 "// broken:\n" 18578 "auto foo{[]() -> foo<5 + 2> { return {}; }};\n" 18579 "} // namespace bar"); 18580 verifyFormat("namespace bar {\n" 18581 "// broken:\n" 18582 "auto foo{[]() -> foo<5 - 2> { return {}; }};\n" 18583 "} // namespace bar"); 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> { return {}; }};\n" 18603 "} // namespace bar"); 18604 verifyFormat("namespace bar {\n" 18605 "// broken:\n" 18606 "auto foo{[]() -> foo<~5> { return {}; }};\n" 18607 "} // namespace bar"); 18608 verifyFormat("namespace bar {\n" 18609 "// broken:\n" 18610 "auto foo{[]() -> foo<5 | 2> { return {}; }};\n" 18611 "} // namespace bar"); 18612 verifyFormat("namespace bar {\n" 18613 "// broken:\n" 18614 "auto foo{[]() -> foo<5 || 2> { 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<2 ? 1 : 0> { return {}; }};\n" 18647 "} // namespace bar"); 18648 verifyFormat("[]() -> a<1> {};"); 18649 verifyFormat("[]() -> a<1> { ; };"); 18650 verifyFormat("[]() -> a<1> { ; }();"); 18651 verifyFormat("[a, a]() -> a<true> {};"); 18652 verifyFormat("[]() -> a<true> {};"); 18653 verifyFormat("[]() -> a<true> { ; };"); 18654 verifyFormat("[]() -> a<true> { ; }();"); 18655 verifyFormat("[a, a]() -> a<false> {};"); 18656 verifyFormat("[]() -> a<false> {};"); 18657 verifyFormat("[]() -> a<false> { ; };"); 18658 verifyFormat("[]() -> a<false> { ; }();"); 18659 verifyFormat("auto foo{[]() -> foo<false> { ; }};"); 18660 verifyFormat("namespace bar {\n" 18661 "auto foo{[]() -> foo<false> { ; }};\n" 18662 "} // namespace bar"); 18663 verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n" 18664 " int j) -> int {\n" 18665 " return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n" 18666 "};"); 18667 verifyFormat( 18668 "aaaaaaaaaaaaaaaaaaaaaa(\n" 18669 " [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n" 18670 " return aaaaaaaaaaaaaaaaa;\n" 18671 " });", 18672 getLLVMStyleWithColumns(70)); 18673 verifyFormat("[]() //\n" 18674 " -> int {\n" 18675 " return 1; //\n" 18676 "};"); 18677 verifyFormat("[]() -> Void<T...> {};"); 18678 verifyFormat("[a, b]() -> Tuple<T...> { return {}; };"); 18679 18680 // Lambdas with explicit template argument lists. 18681 verifyFormat( 18682 "auto L = []<template <typename> class T, class U>(T<U> &&a) {};\n"); 18683 18684 // Multiple lambdas in the same parentheses change indentation rules. These 18685 // lambdas are forced to start on new lines. 18686 verifyFormat("SomeFunction(\n" 18687 " []() {\n" 18688 " //\n" 18689 " },\n" 18690 " []() {\n" 18691 " //\n" 18692 " });"); 18693 18694 // A lambda passed as arg0 is always pushed to the next line. 18695 verifyFormat("SomeFunction(\n" 18696 " [this] {\n" 18697 " //\n" 18698 " },\n" 18699 " 1);\n"); 18700 18701 // A multi-line lambda passed as arg1 forces arg0 to be pushed out, just like 18702 // the arg0 case above. 18703 auto Style = getGoogleStyle(); 18704 Style.BinPackArguments = false; 18705 verifyFormat("SomeFunction(\n" 18706 " a,\n" 18707 " [this] {\n" 18708 " //\n" 18709 " },\n" 18710 " b);\n", 18711 Style); 18712 verifyFormat("SomeFunction(\n" 18713 " a,\n" 18714 " [this] {\n" 18715 " //\n" 18716 " },\n" 18717 " b);\n"); 18718 18719 // A lambda with a very long line forces arg0 to be pushed out irrespective of 18720 // the BinPackArguments value (as long as the code is wide enough). 18721 verifyFormat( 18722 "something->SomeFunction(\n" 18723 " a,\n" 18724 " [this] {\n" 18725 " " 18726 "D0000000000000000000000000000000000000000000000000000000000001();\n" 18727 " },\n" 18728 " b);\n"); 18729 18730 // A multi-line lambda is pulled up as long as the introducer fits on the 18731 // previous line and there are no further args. 18732 verifyFormat("function(1, [this, that] {\n" 18733 " //\n" 18734 "});\n"); 18735 verifyFormat("function([this, that] {\n" 18736 " //\n" 18737 "});\n"); 18738 // FIXME: this format is not ideal and we should consider forcing the first 18739 // arg onto its own line. 18740 verifyFormat("function(a, b, c, //\n" 18741 " d, [this, that] {\n" 18742 " //\n" 18743 " });\n"); 18744 18745 // Multiple lambdas are treated correctly even when there is a short arg0. 18746 verifyFormat("SomeFunction(\n" 18747 " 1,\n" 18748 " [this] {\n" 18749 " //\n" 18750 " },\n" 18751 " [this] {\n" 18752 " //\n" 18753 " },\n" 18754 " 1);\n"); 18755 18756 // More complex introducers. 18757 verifyFormat("return [i, args...] {};"); 18758 18759 // Not lambdas. 18760 verifyFormat("constexpr char hello[]{\"hello\"};"); 18761 verifyFormat("double &operator[](int i) { return 0; }\n" 18762 "int i;"); 18763 verifyFormat("std::unique_ptr<int[]> foo() {}"); 18764 verifyFormat("int i = a[a][a]->f();"); 18765 verifyFormat("int i = (*b)[a]->f();"); 18766 18767 // Other corner cases. 18768 verifyFormat("void f() {\n" 18769 " bar([]() {} // Did not respect SpacesBeforeTrailingComments\n" 18770 " );\n" 18771 "}"); 18772 18773 // Lambdas created through weird macros. 18774 verifyFormat("void f() {\n" 18775 " MACRO((const AA &a) { return 1; });\n" 18776 " MACRO((AA &a) { return 1; });\n" 18777 "}"); 18778 18779 verifyFormat("if (blah_blah(whatever, whatever, [] {\n" 18780 " doo_dah();\n" 18781 " doo_dah();\n" 18782 " })) {\n" 18783 "}"); 18784 verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n" 18785 " doo_dah();\n" 18786 " doo_dah();\n" 18787 " })) {\n" 18788 "}"); 18789 verifyFormat("if CONSTEXPR (blah_blah(whatever, whatever, [] {\n" 18790 " doo_dah();\n" 18791 " doo_dah();\n" 18792 " })) {\n" 18793 "}"); 18794 verifyFormat("auto lambda = []() {\n" 18795 " int a = 2\n" 18796 "#if A\n" 18797 " + 2\n" 18798 "#endif\n" 18799 " ;\n" 18800 "};"); 18801 18802 // Lambdas with complex multiline introducers. 18803 verifyFormat( 18804 "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 18805 " [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n" 18806 " -> ::std::unordered_set<\n" 18807 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n" 18808 " //\n" 18809 " });"); 18810 18811 FormatStyle DoNotMerge = getLLVMStyle(); 18812 DoNotMerge.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; 18813 verifyFormat("auto c = []() {\n" 18814 " return b;\n" 18815 "};", 18816 "auto c = []() { return b; };", DoNotMerge); 18817 verifyFormat("auto c = []() {\n" 18818 "};", 18819 " auto c = []() {};", DoNotMerge); 18820 18821 FormatStyle MergeEmptyOnly = getLLVMStyle(); 18822 MergeEmptyOnly.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Empty; 18823 verifyFormat("auto c = []() {\n" 18824 " return b;\n" 18825 "};", 18826 "auto c = []() {\n" 18827 " return b;\n" 18828 " };", 18829 MergeEmptyOnly); 18830 verifyFormat("auto c = []() {};", 18831 "auto c = []() {\n" 18832 "};", 18833 MergeEmptyOnly); 18834 18835 FormatStyle MergeInline = getLLVMStyle(); 18836 MergeInline.AllowShortLambdasOnASingleLine = FormatStyle::SLS_Inline; 18837 verifyFormat("auto c = []() {\n" 18838 " return b;\n" 18839 "};", 18840 "auto c = []() { return b; };", MergeInline); 18841 verifyFormat("function([]() { return b; })", "function([]() { return b; })", 18842 MergeInline); 18843 verifyFormat("function([]() { return b; }, a)", 18844 "function([]() { return b; }, a)", MergeInline); 18845 verifyFormat("function(a, []() { return b; })", 18846 "function(a, []() { return b; })", MergeInline); 18847 18848 // Check option "BraceWrapping.BeforeLambdaBody" and different state of 18849 // AllowShortLambdasOnASingleLine 18850 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle(); 18851 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 18852 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 18853 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 18854 FormatStyle::ShortLambdaStyle::SLS_None; 18855 verifyFormat("FctWithOneNestedLambdaInline_SLS_None(\n" 18856 " []()\n" 18857 " {\n" 18858 " return 17;\n" 18859 " });", 18860 LLVMWithBeforeLambdaBody); 18861 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_None(\n" 18862 " []()\n" 18863 " {\n" 18864 " });", 18865 LLVMWithBeforeLambdaBody); 18866 verifyFormat("auto fct_SLS_None = []()\n" 18867 "{\n" 18868 " return 17;\n" 18869 "};", 18870 LLVMWithBeforeLambdaBody); 18871 verifyFormat("TwoNestedLambdas_SLS_None(\n" 18872 " []()\n" 18873 " {\n" 18874 " return Call(\n" 18875 " []()\n" 18876 " {\n" 18877 " return 17;\n" 18878 " });\n" 18879 " });", 18880 LLVMWithBeforeLambdaBody); 18881 verifyFormat("void Fct()\n" 18882 "{\n" 18883 " return {[]()\n" 18884 " {\n" 18885 " return 17;\n" 18886 " }};\n" 18887 "}", 18888 LLVMWithBeforeLambdaBody); 18889 18890 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 18891 FormatStyle::ShortLambdaStyle::SLS_Empty; 18892 verifyFormat("FctWithOneNestedLambdaInline_SLS_Empty(\n" 18893 " []()\n" 18894 " {\n" 18895 " return 17;\n" 18896 " });", 18897 LLVMWithBeforeLambdaBody); 18898 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Empty([]() {});", 18899 LLVMWithBeforeLambdaBody); 18900 verifyFormat("FctWithOneNestedLambdaEmptyInsideAVeryVeryVeryVeryVeryVeryVeryL" 18901 "ongFunctionName_SLS_Empty(\n" 18902 " []() {});", 18903 LLVMWithBeforeLambdaBody); 18904 verifyFormat("FctWithMultipleParams_SLS_Empty(A, B,\n" 18905 " []()\n" 18906 " {\n" 18907 " return 17;\n" 18908 " });", 18909 LLVMWithBeforeLambdaBody); 18910 verifyFormat("auto fct_SLS_Empty = []()\n" 18911 "{\n" 18912 " return 17;\n" 18913 "};", 18914 LLVMWithBeforeLambdaBody); 18915 verifyFormat("TwoNestedLambdas_SLS_Empty(\n" 18916 " []()\n" 18917 " {\n" 18918 " return Call([]() {});\n" 18919 " });", 18920 LLVMWithBeforeLambdaBody); 18921 verifyFormat("TwoNestedLambdas_SLS_Empty(A,\n" 18922 " []()\n" 18923 " {\n" 18924 " return Call([]() {});\n" 18925 " });", 18926 LLVMWithBeforeLambdaBody); 18927 verifyFormat( 18928 "FctWithLongLineInLambda_SLS_Empty(\n" 18929 " []()\n" 18930 " {\n" 18931 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 18932 " AndShouldNotBeConsiderAsInline,\n" 18933 " LambdaBodyMustBeBreak);\n" 18934 " });", 18935 LLVMWithBeforeLambdaBody); 18936 18937 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 18938 FormatStyle::ShortLambdaStyle::SLS_Inline; 18939 verifyFormat("FctWithOneNestedLambdaInline_SLS_Inline([]() { return 17; });", 18940 LLVMWithBeforeLambdaBody); 18941 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_Inline([]() {});", 18942 LLVMWithBeforeLambdaBody); 18943 verifyFormat("auto fct_SLS_Inline = []()\n" 18944 "{\n" 18945 " return 17;\n" 18946 "};", 18947 LLVMWithBeforeLambdaBody); 18948 verifyFormat("TwoNestedLambdas_SLS_Inline([]() { return Call([]() { return " 18949 "17; }); });", 18950 LLVMWithBeforeLambdaBody); 18951 verifyFormat( 18952 "FctWithLongLineInLambda_SLS_Inline(\n" 18953 " []()\n" 18954 " {\n" 18955 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 18956 " AndShouldNotBeConsiderAsInline,\n" 18957 " LambdaBodyMustBeBreak);\n" 18958 " });", 18959 LLVMWithBeforeLambdaBody); 18960 verifyFormat("FctWithMultipleParams_SLS_Inline(" 18961 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n" 18962 " []() { return 17; });", 18963 LLVMWithBeforeLambdaBody); 18964 verifyFormat( 18965 "FctWithMultipleParams_SLS_Inline(FirstParam, []() { return 17; });", 18966 LLVMWithBeforeLambdaBody); 18967 18968 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 18969 FormatStyle::ShortLambdaStyle::SLS_All; 18970 verifyFormat("FctWithOneNestedLambdaInline_SLS_All([]() { return 17; });", 18971 LLVMWithBeforeLambdaBody); 18972 verifyFormat("FctWithOneNestedLambdaEmpty_SLS_All([]() {});", 18973 LLVMWithBeforeLambdaBody); 18974 verifyFormat("auto fct_SLS_All = []() { return 17; };", 18975 LLVMWithBeforeLambdaBody); 18976 verifyFormat("FctWithOneParam_SLS_All(\n" 18977 " []()\n" 18978 " {\n" 18979 " // A cool function...\n" 18980 " return 43;\n" 18981 " });", 18982 LLVMWithBeforeLambdaBody); 18983 verifyFormat("FctWithMultipleParams_SLS_All(" 18984 "VeryLongParameterThatShouldAskToBeOnMultiLine,\n" 18985 " []() { return 17; });", 18986 LLVMWithBeforeLambdaBody); 18987 verifyFormat("FctWithMultipleParams_SLS_All(A, []() { return 17; });", 18988 LLVMWithBeforeLambdaBody); 18989 verifyFormat("FctWithMultipleParams_SLS_All(A, B, []() { return 17; });", 18990 LLVMWithBeforeLambdaBody); 18991 verifyFormat( 18992 "FctWithLongLineInLambda_SLS_All(\n" 18993 " []()\n" 18994 " {\n" 18995 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 18996 " AndShouldNotBeConsiderAsInline,\n" 18997 " LambdaBodyMustBeBreak);\n" 18998 " });", 18999 LLVMWithBeforeLambdaBody); 19000 verifyFormat( 19001 "auto fct_SLS_All = []()\n" 19002 "{\n" 19003 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 19004 " AndShouldNotBeConsiderAsInline,\n" 19005 " LambdaBodyMustBeBreak);\n" 19006 "};", 19007 LLVMWithBeforeLambdaBody); 19008 LLVMWithBeforeLambdaBody.BinPackParameters = false; 19009 verifyFormat("FctAllOnSameLine_SLS_All([]() { return S; }, Fst, Second);", 19010 LLVMWithBeforeLambdaBody); 19011 verifyFormat( 19012 "FctWithLongLineInLambda_SLS_All([]() { return SomeValueNotSoLong; },\n" 19013 " FirstParam,\n" 19014 " SecondParam,\n" 19015 " ThirdParam,\n" 19016 " FourthParam);", 19017 LLVMWithBeforeLambdaBody); 19018 verifyFormat("FctWithLongLineInLambda_SLS_All(\n" 19019 " []() { return " 19020 "SomeValueVeryVeryVeryVeryVeryVeryVeryVeryVeryLong; },\n" 19021 " FirstParam,\n" 19022 " SecondParam,\n" 19023 " ThirdParam,\n" 19024 " FourthParam);", 19025 LLVMWithBeforeLambdaBody); 19026 verifyFormat( 19027 "FctWithLongLineInLambda_SLS_All(FirstParam,\n" 19028 " SecondParam,\n" 19029 " ThirdParam,\n" 19030 " FourthParam,\n" 19031 " []() { return SomeValueNotSoLong; });", 19032 LLVMWithBeforeLambdaBody); 19033 verifyFormat("FctWithLongLineInLambda_SLS_All(\n" 19034 " []()\n" 19035 " {\n" 19036 " return " 19037 "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB" 19038 "eConsiderAsInline;\n" 19039 " });", 19040 LLVMWithBeforeLambdaBody); 19041 verifyFormat( 19042 "FctWithLongLineInLambda_SLS_All(\n" 19043 " []()\n" 19044 " {\n" 19045 " return HereAVeryLongLine(ThatWillBeFormatted, OnMultipleLine,\n" 19046 " AndShouldNotBeConsiderAsInline,\n" 19047 " LambdaBodyMustBeBreak);\n" 19048 " });", 19049 LLVMWithBeforeLambdaBody); 19050 verifyFormat("FctWithTwoParams_SLS_All(\n" 19051 " []()\n" 19052 " {\n" 19053 " // A cool function...\n" 19054 " return 43;\n" 19055 " },\n" 19056 " 87);", 19057 LLVMWithBeforeLambdaBody); 19058 verifyFormat("FctWithTwoParams_SLS_All([]() { return 43; }, 87);", 19059 LLVMWithBeforeLambdaBody); 19060 verifyFormat("FctWithOneNestedLambdas_SLS_All([]() { return 17; });", 19061 LLVMWithBeforeLambdaBody); 19062 verifyFormat( 19063 "TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; }); });", 19064 LLVMWithBeforeLambdaBody); 19065 verifyFormat("TwoNestedLambdas_SLS_All([]() { return Call([]() { return 17; " 19066 "}); }, x);", 19067 LLVMWithBeforeLambdaBody); 19068 verifyFormat("TwoNestedLambdas_SLS_All(\n" 19069 " []()\n" 19070 " {\n" 19071 " // A cool function...\n" 19072 " return Call([]() { return 17; });\n" 19073 " });", 19074 LLVMWithBeforeLambdaBody); 19075 verifyFormat("TwoNestedLambdas_SLS_All(\n" 19076 " []()\n" 19077 " {\n" 19078 " return Call(\n" 19079 " []()\n" 19080 " {\n" 19081 " // A cool function...\n" 19082 " return 17;\n" 19083 " });\n" 19084 " });", 19085 LLVMWithBeforeLambdaBody); 19086 } 19087 19088 TEST_F(FormatTest, LambdaWithLineComments) { 19089 FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle(); 19090 LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom; 19091 LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true; 19092 LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine = 19093 FormatStyle::ShortLambdaStyle::SLS_All; 19094 19095 verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody); 19096 verifyFormat("auto k = []() // comment\n" 19097 "{ return; }", 19098 LLVMWithBeforeLambdaBody); 19099 verifyFormat("auto k = []() /* comment */ { return; }", 19100 LLVMWithBeforeLambdaBody); 19101 verifyFormat("auto k = []() /* comment */ /* comment */ { return; }", 19102 LLVMWithBeforeLambdaBody); 19103 verifyFormat("auto k = []() // X\n" 19104 "{ return; }", 19105 LLVMWithBeforeLambdaBody); 19106 verifyFormat( 19107 "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n" 19108 "{ return; }", 19109 LLVMWithBeforeLambdaBody); 19110 } 19111 19112 TEST_F(FormatTest, EmptyLinesInLambdas) { 19113 verifyFormat("auto lambda = []() {\n" 19114 " x(); //\n" 19115 "};", 19116 "auto lambda = []() {\n" 19117 "\n" 19118 " x(); //\n" 19119 "\n" 19120 "};"); 19121 } 19122 19123 TEST_F(FormatTest, FormatsBlocks) { 19124 FormatStyle ShortBlocks = getLLVMStyle(); 19125 ShortBlocks.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 19126 verifyFormat("int (^Block)(int, int);", ShortBlocks); 19127 verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks); 19128 verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks); 19129 verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks); 19130 verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks); 19131 verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks); 19132 19133 verifyFormat("foo(^{ bar(); });", ShortBlocks); 19134 verifyFormat("foo(a, ^{ bar(); });", ShortBlocks); 19135 verifyFormat("{ void (^block)(Object *x); }", ShortBlocks); 19136 19137 verifyFormat("[operation setCompletionBlock:^{\n" 19138 " [self onOperationDone];\n" 19139 "}];"); 19140 verifyFormat("int i = {[operation setCompletionBlock:^{\n" 19141 " [self onOperationDone];\n" 19142 "}]};"); 19143 verifyFormat("[operation setCompletionBlock:^(int *i) {\n" 19144 " f();\n" 19145 "}];"); 19146 verifyFormat("int a = [operation block:^int(int *i) {\n" 19147 " return 1;\n" 19148 "}];"); 19149 verifyFormat("[myObject doSomethingWith:arg1\n" 19150 " aaa:^int(int *a) {\n" 19151 " return 1;\n" 19152 " }\n" 19153 " bbb:f(a * bbbbbbbb)];"); 19154 19155 verifyFormat("[operation setCompletionBlock:^{\n" 19156 " [self.delegate newDataAvailable];\n" 19157 "}];", 19158 getLLVMStyleWithColumns(60)); 19159 verifyFormat("dispatch_async(_fileIOQueue, ^{\n" 19160 " NSString *path = [self sessionFilePath];\n" 19161 " if (path) {\n" 19162 " // ...\n" 19163 " }\n" 19164 "});"); 19165 verifyFormat("[[SessionService sharedService]\n" 19166 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 19167 " if (window) {\n" 19168 " [self windowDidLoad:window];\n" 19169 " } else {\n" 19170 " [self errorLoadingWindow];\n" 19171 " }\n" 19172 " }];"); 19173 verifyFormat("void (^largeBlock)(void) = ^{\n" 19174 " // ...\n" 19175 "};\n", 19176 getLLVMStyleWithColumns(40)); 19177 verifyFormat("[[SessionService sharedService]\n" 19178 " loadWindowWithCompletionBlock: //\n" 19179 " ^(SessionWindow *window) {\n" 19180 " if (window) {\n" 19181 " [self windowDidLoad:window];\n" 19182 " } else {\n" 19183 " [self errorLoadingWindow];\n" 19184 " }\n" 19185 " }];", 19186 getLLVMStyleWithColumns(60)); 19187 verifyFormat("[myObject doSomethingWith:arg1\n" 19188 " firstBlock:^(Foo *a) {\n" 19189 " // ...\n" 19190 " int i;\n" 19191 " }\n" 19192 " secondBlock:^(Bar *b) {\n" 19193 " // ...\n" 19194 " int i;\n" 19195 " }\n" 19196 " thirdBlock:^Foo(Bar *b) {\n" 19197 " // ...\n" 19198 " int i;\n" 19199 " }];"); 19200 verifyFormat("[myObject doSomethingWith:arg1\n" 19201 " firstBlock:-1\n" 19202 " secondBlock:^(Bar *b) {\n" 19203 " // ...\n" 19204 " int i;\n" 19205 " }];"); 19206 19207 verifyFormat("f(^{\n" 19208 " @autoreleasepool {\n" 19209 " if (a) {\n" 19210 " g();\n" 19211 " }\n" 19212 " }\n" 19213 "});"); 19214 verifyFormat("Block b = ^int *(A *a, B *b) {}"); 19215 verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n" 19216 "};"); 19217 19218 FormatStyle FourIndent = getLLVMStyle(); 19219 FourIndent.ObjCBlockIndentWidth = 4; 19220 verifyFormat("[operation setCompletionBlock:^{\n" 19221 " [self onOperationDone];\n" 19222 "}];", 19223 FourIndent); 19224 } 19225 19226 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { 19227 FormatStyle ZeroColumn = getLLVMStyle(); 19228 ZeroColumn.ColumnLimit = 0; 19229 19230 verifyFormat("[[SessionService sharedService] " 19231 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 19232 " if (window) {\n" 19233 " [self windowDidLoad:window];\n" 19234 " } else {\n" 19235 " [self errorLoadingWindow];\n" 19236 " }\n" 19237 "}];", 19238 ZeroColumn); 19239 EXPECT_EQ("[[SessionService sharedService]\n" 19240 " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 19241 " if (window) {\n" 19242 " [self windowDidLoad:window];\n" 19243 " } else {\n" 19244 " [self errorLoadingWindow];\n" 19245 " }\n" 19246 " }];", 19247 format("[[SessionService sharedService]\n" 19248 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" 19249 " if (window) {\n" 19250 " [self windowDidLoad:window];\n" 19251 " } else {\n" 19252 " [self errorLoadingWindow];\n" 19253 " }\n" 19254 "}];", 19255 ZeroColumn)); 19256 verifyFormat("[myObject doSomethingWith:arg1\n" 19257 " firstBlock:^(Foo *a) {\n" 19258 " // ...\n" 19259 " int i;\n" 19260 " }\n" 19261 " secondBlock:^(Bar *b) {\n" 19262 " // ...\n" 19263 " int i;\n" 19264 " }\n" 19265 " thirdBlock:^Foo(Bar *b) {\n" 19266 " // ...\n" 19267 " int i;\n" 19268 " }];", 19269 ZeroColumn); 19270 verifyFormat("f(^{\n" 19271 " @autoreleasepool {\n" 19272 " if (a) {\n" 19273 " g();\n" 19274 " }\n" 19275 " }\n" 19276 "});", 19277 ZeroColumn); 19278 verifyFormat("void (^largeBlock)(void) = ^{\n" 19279 " // ...\n" 19280 "};", 19281 ZeroColumn); 19282 19283 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; 19284 EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", 19285 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 19286 ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; 19287 EXPECT_EQ("void (^largeBlock)(void) = ^{\n" 19288 " int i;\n" 19289 "};", 19290 format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); 19291 } 19292 19293 TEST_F(FormatTest, SupportsCRLF) { 19294 EXPECT_EQ("int a;\r\n" 19295 "int b;\r\n" 19296 "int c;\r\n", 19297 format("int a;\r\n" 19298 " int b;\r\n" 19299 " int c;\r\n", 19300 getLLVMStyle())); 19301 EXPECT_EQ("int a;\r\n" 19302 "int b;\r\n" 19303 "int c;\r\n", 19304 format("int a;\r\n" 19305 " int b;\n" 19306 " int c;\r\n", 19307 getLLVMStyle())); 19308 EXPECT_EQ("int a;\n" 19309 "int b;\n" 19310 "int c;\n", 19311 format("int a;\r\n" 19312 " int b;\n" 19313 " int c;\n", 19314 getLLVMStyle())); 19315 EXPECT_EQ("\"aaaaaaa \"\r\n" 19316 "\"bbbbbbb\";\r\n", 19317 format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); 19318 EXPECT_EQ("#define A \\\r\n" 19319 " b; \\\r\n" 19320 " c; \\\r\n" 19321 " d;\r\n", 19322 format("#define A \\\r\n" 19323 " b; \\\r\n" 19324 " c; d; \r\n", 19325 getGoogleStyle())); 19326 19327 EXPECT_EQ("/*\r\n" 19328 "multi line block comments\r\n" 19329 "should not introduce\r\n" 19330 "an extra carriage return\r\n" 19331 "*/\r\n", 19332 format("/*\r\n" 19333 "multi line block comments\r\n" 19334 "should not introduce\r\n" 19335 "an extra carriage return\r\n" 19336 "*/\r\n")); 19337 EXPECT_EQ("/*\r\n" 19338 "\r\n" 19339 "*/", 19340 format("/*\r\n" 19341 " \r\r\r\n" 19342 "*/")); 19343 19344 FormatStyle style = getLLVMStyle(); 19345 19346 style.DeriveLineEnding = true; 19347 style.UseCRLF = false; 19348 EXPECT_EQ("union FooBarBazQux {\n" 19349 " int foo;\n" 19350 " int bar;\n" 19351 " int baz;\n" 19352 "};", 19353 format("union FooBarBazQux {\r\n" 19354 " int foo;\n" 19355 " int bar;\r\n" 19356 " int baz;\n" 19357 "};", 19358 style)); 19359 style.UseCRLF = true; 19360 EXPECT_EQ("union FooBarBazQux {\r\n" 19361 " int foo;\r\n" 19362 " int bar;\r\n" 19363 " int baz;\r\n" 19364 "};", 19365 format("union FooBarBazQux {\r\n" 19366 " int foo;\n" 19367 " int bar;\r\n" 19368 " int baz;\n" 19369 "};", 19370 style)); 19371 19372 style.DeriveLineEnding = false; 19373 style.UseCRLF = false; 19374 EXPECT_EQ("union FooBarBazQux {\n" 19375 " int foo;\n" 19376 " int bar;\n" 19377 " int baz;\n" 19378 " int qux;\n" 19379 "};", 19380 format("union FooBarBazQux {\r\n" 19381 " int foo;\n" 19382 " int bar;\r\n" 19383 " int baz;\n" 19384 " int qux;\r\n" 19385 "};", 19386 style)); 19387 style.UseCRLF = true; 19388 EXPECT_EQ("union FooBarBazQux {\r\n" 19389 " int foo;\r\n" 19390 " int bar;\r\n" 19391 " int baz;\r\n" 19392 " int qux;\r\n" 19393 "};", 19394 format("union FooBarBazQux {\r\n" 19395 " int foo;\n" 19396 " int bar;\r\n" 19397 " int baz;\n" 19398 " int qux;\n" 19399 "};", 19400 style)); 19401 19402 style.DeriveLineEnding = true; 19403 style.UseCRLF = false; 19404 EXPECT_EQ("union FooBarBazQux {\r\n" 19405 " int foo;\r\n" 19406 " int bar;\r\n" 19407 " int baz;\r\n" 19408 " int qux;\r\n" 19409 "};", 19410 format("union FooBarBazQux {\r\n" 19411 " int foo;\n" 19412 " int bar;\r\n" 19413 " int baz;\n" 19414 " int qux;\r\n" 19415 "};", 19416 style)); 19417 style.UseCRLF = true; 19418 EXPECT_EQ("union FooBarBazQux {\n" 19419 " int foo;\n" 19420 " int bar;\n" 19421 " int baz;\n" 19422 " int qux;\n" 19423 "};", 19424 format("union FooBarBazQux {\r\n" 19425 " int foo;\n" 19426 " int bar;\r\n" 19427 " int baz;\n" 19428 " int qux;\n" 19429 "};", 19430 style)); 19431 } 19432 19433 TEST_F(FormatTest, MunchSemicolonAfterBlocks) { 19434 verifyFormat("MY_CLASS(C) {\n" 19435 " int i;\n" 19436 " int j;\n" 19437 "};"); 19438 } 19439 19440 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { 19441 FormatStyle TwoIndent = getLLVMStyleWithColumns(15); 19442 TwoIndent.ContinuationIndentWidth = 2; 19443 19444 EXPECT_EQ("int i =\n" 19445 " longFunction(\n" 19446 " arg);", 19447 format("int i = longFunction(arg);", TwoIndent)); 19448 19449 FormatStyle SixIndent = getLLVMStyleWithColumns(20); 19450 SixIndent.ContinuationIndentWidth = 6; 19451 19452 EXPECT_EQ("int i =\n" 19453 " longFunction(\n" 19454 " arg);", 19455 format("int i = longFunction(arg);", SixIndent)); 19456 } 19457 19458 TEST_F(FormatTest, WrappedClosingParenthesisIndent) { 19459 FormatStyle Style = getLLVMStyle(); 19460 verifyFormat("int Foo::getter(\n" 19461 " //\n" 19462 ") const {\n" 19463 " return foo;\n" 19464 "}", 19465 Style); 19466 verifyFormat("void Foo::setter(\n" 19467 " //\n" 19468 ") {\n" 19469 " foo = 1;\n" 19470 "}", 19471 Style); 19472 } 19473 19474 TEST_F(FormatTest, SpacesInAngles) { 19475 FormatStyle Spaces = getLLVMStyle(); 19476 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 19477 19478 verifyFormat("vector< ::std::string > x1;", Spaces); 19479 verifyFormat("Foo< int, Bar > x2;", Spaces); 19480 verifyFormat("Foo< ::int, ::Bar > x3;", Spaces); 19481 19482 verifyFormat("static_cast< int >(arg);", Spaces); 19483 verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces); 19484 verifyFormat("f< int, float >();", Spaces); 19485 verifyFormat("template <> g() {}", Spaces); 19486 verifyFormat("template < std::vector< int > > f() {}", Spaces); 19487 verifyFormat("std::function< void(int, int) > fct;", Spaces); 19488 verifyFormat("void inFunction() { std::function< void(int, int) > fct; }", 19489 Spaces); 19490 19491 Spaces.Standard = FormatStyle::LS_Cpp03; 19492 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 19493 verifyFormat("A< A< int > >();", Spaces); 19494 19495 Spaces.SpacesInAngles = FormatStyle::SIAS_Never; 19496 verifyFormat("A<A<int> >();", Spaces); 19497 19498 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave; 19499 verifyFormat("vector< ::std::string> x4;", "vector<::std::string> x4;", 19500 Spaces); 19501 verifyFormat("vector< ::std::string > x4;", "vector<::std::string > x4;", 19502 Spaces); 19503 19504 verifyFormat("A<A<int> >();", Spaces); 19505 verifyFormat("A<A<int> >();", "A<A<int>>();", Spaces); 19506 verifyFormat("A< A< int > >();", Spaces); 19507 19508 Spaces.Standard = FormatStyle::LS_Cpp11; 19509 Spaces.SpacesInAngles = FormatStyle::SIAS_Always; 19510 verifyFormat("A< A< int > >();", Spaces); 19511 19512 Spaces.SpacesInAngles = FormatStyle::SIAS_Never; 19513 verifyFormat("vector<::std::string> x4;", Spaces); 19514 verifyFormat("vector<int> x5;", Spaces); 19515 verifyFormat("Foo<int, Bar> x6;", Spaces); 19516 verifyFormat("Foo<::int, ::Bar> x7;", Spaces); 19517 19518 verifyFormat("A<A<int>>();", Spaces); 19519 19520 Spaces.SpacesInAngles = FormatStyle::SIAS_Leave; 19521 verifyFormat("vector<::std::string> x4;", Spaces); 19522 verifyFormat("vector< ::std::string > x4;", Spaces); 19523 verifyFormat("vector<int> x5;", Spaces); 19524 verifyFormat("vector< int > x5;", Spaces); 19525 verifyFormat("Foo<int, Bar> x6;", Spaces); 19526 verifyFormat("Foo< int, Bar > x6;", Spaces); 19527 verifyFormat("Foo<::int, ::Bar> x7;", Spaces); 19528 verifyFormat("Foo< ::int, ::Bar > x7;", Spaces); 19529 19530 verifyFormat("A<A<int>>();", Spaces); 19531 verifyFormat("A< A< int > >();", Spaces); 19532 verifyFormat("A<A<int > >();", Spaces); 19533 verifyFormat("A< A< int>>();", Spaces); 19534 } 19535 19536 TEST_F(FormatTest, SpaceAfterTemplateKeyword) { 19537 FormatStyle Style = getLLVMStyle(); 19538 Style.SpaceAfterTemplateKeyword = false; 19539 verifyFormat("template<int> void foo();", Style); 19540 } 19541 19542 TEST_F(FormatTest, TripleAngleBrackets) { 19543 verifyFormat("f<<<1, 1>>>();"); 19544 verifyFormat("f<<<1, 1, 1, s>>>();"); 19545 verifyFormat("f<<<a, b, c, d>>>();"); 19546 EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); 19547 verifyFormat("f<param><<<1, 1>>>();"); 19548 verifyFormat("f<1><<<1, 1>>>();"); 19549 EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); 19550 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 19551 "aaaaaaaaaaa<<<\n 1, 1>>>();"); 19552 verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n" 19553 " <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();"); 19554 } 19555 19556 TEST_F(FormatTest, MergeLessLessAtEnd) { 19557 verifyFormat("<<"); 19558 EXPECT_EQ("< < <", format("\\\n<<<")); 19559 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 19560 "aaallvm::outs() <<"); 19561 verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 19562 "aaaallvm::outs()\n <<"); 19563 } 19564 19565 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { 19566 std::string code = "#if A\n" 19567 "#if B\n" 19568 "a.\n" 19569 "#endif\n" 19570 " a = 1;\n" 19571 "#else\n" 19572 "#endif\n" 19573 "#if C\n" 19574 "#else\n" 19575 "#endif\n"; 19576 EXPECT_EQ(code, format(code)); 19577 } 19578 19579 TEST_F(FormatTest, HandleConflictMarkers) { 19580 // Git/SVN conflict markers. 19581 EXPECT_EQ("int a;\n" 19582 "void f() {\n" 19583 " callme(some(parameter1,\n" 19584 "<<<<<<< text by the vcs\n" 19585 " parameter2),\n" 19586 "||||||| text by the vcs\n" 19587 " parameter2),\n" 19588 " parameter3,\n" 19589 "======= text by the vcs\n" 19590 " parameter2, parameter3),\n" 19591 ">>>>>>> text by the vcs\n" 19592 " otherparameter);\n", 19593 format("int a;\n" 19594 "void f() {\n" 19595 " callme(some(parameter1,\n" 19596 "<<<<<<< text by the vcs\n" 19597 " parameter2),\n" 19598 "||||||| text by the vcs\n" 19599 " parameter2),\n" 19600 " parameter3,\n" 19601 "======= text by the vcs\n" 19602 " parameter2,\n" 19603 " parameter3),\n" 19604 ">>>>>>> text by the vcs\n" 19605 " otherparameter);\n")); 19606 19607 // Perforce markers. 19608 EXPECT_EQ("void f() {\n" 19609 " function(\n" 19610 ">>>> text by the vcs\n" 19611 " parameter,\n" 19612 "==== text by the vcs\n" 19613 " parameter,\n" 19614 "==== text by the vcs\n" 19615 " parameter,\n" 19616 "<<<< text by the vcs\n" 19617 " parameter);\n", 19618 format("void f() {\n" 19619 " function(\n" 19620 ">>>> text by the vcs\n" 19621 " parameter,\n" 19622 "==== text by the vcs\n" 19623 " parameter,\n" 19624 "==== text by the vcs\n" 19625 " parameter,\n" 19626 "<<<< text by the vcs\n" 19627 " parameter);\n")); 19628 19629 EXPECT_EQ("<<<<<<<\n" 19630 "|||||||\n" 19631 "=======\n" 19632 ">>>>>>>", 19633 format("<<<<<<<\n" 19634 "|||||||\n" 19635 "=======\n" 19636 ">>>>>>>")); 19637 19638 EXPECT_EQ("<<<<<<<\n" 19639 "|||||||\n" 19640 "int i;\n" 19641 "=======\n" 19642 ">>>>>>>", 19643 format("<<<<<<<\n" 19644 "|||||||\n" 19645 "int i;\n" 19646 "=======\n" 19647 ">>>>>>>")); 19648 19649 // FIXME: Handle parsing of macros around conflict markers correctly: 19650 EXPECT_EQ("#define Macro \\\n" 19651 "<<<<<<<\n" 19652 "Something \\\n" 19653 "|||||||\n" 19654 "Else \\\n" 19655 "=======\n" 19656 "Other \\\n" 19657 ">>>>>>>\n" 19658 " End int i;\n", 19659 format("#define Macro \\\n" 19660 "<<<<<<<\n" 19661 " Something \\\n" 19662 "|||||||\n" 19663 " Else \\\n" 19664 "=======\n" 19665 " Other \\\n" 19666 ">>>>>>>\n" 19667 " End\n" 19668 "int i;\n")); 19669 } 19670 19671 TEST_F(FormatTest, DisableRegions) { 19672 EXPECT_EQ("int i;\n" 19673 "// clang-format off\n" 19674 " int j;\n" 19675 "// clang-format on\n" 19676 "int k;", 19677 format(" int i;\n" 19678 " // clang-format off\n" 19679 " int j;\n" 19680 " // clang-format on\n" 19681 " int k;")); 19682 EXPECT_EQ("int i;\n" 19683 "/* clang-format off */\n" 19684 " int j;\n" 19685 "/* clang-format on */\n" 19686 "int k;", 19687 format(" int i;\n" 19688 " /* clang-format off */\n" 19689 " int j;\n" 19690 " /* clang-format on */\n" 19691 " int k;")); 19692 19693 // Don't reflow comments within disabled regions. 19694 EXPECT_EQ("// clang-format off\n" 19695 "// long long long long long long line\n" 19696 "/* clang-format on */\n" 19697 "/* long long long\n" 19698 " * long long long\n" 19699 " * line */\n" 19700 "int i;\n" 19701 "/* clang-format off */\n" 19702 "/* long long long long long long line */\n", 19703 format("// clang-format off\n" 19704 "// long long long long long long line\n" 19705 "/* clang-format on */\n" 19706 "/* long long long long long long line */\n" 19707 "int i;\n" 19708 "/* clang-format off */\n" 19709 "/* long long long long long long line */\n", 19710 getLLVMStyleWithColumns(20))); 19711 } 19712 19713 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { 19714 format("? ) ="); 19715 verifyNoCrash("#define a\\\n /**/}"); 19716 } 19717 19718 TEST_F(FormatTest, FormatsTableGenCode) { 19719 FormatStyle Style = getLLVMStyle(); 19720 Style.Language = FormatStyle::LK_TableGen; 19721 verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style); 19722 } 19723 19724 TEST_F(FormatTest, ArrayOfTemplates) { 19725 EXPECT_EQ("auto a = new unique_ptr<int>[10];", 19726 format("auto a = new unique_ptr<int > [ 10];")); 19727 19728 FormatStyle Spaces = getLLVMStyle(); 19729 Spaces.SpacesInSquareBrackets = true; 19730 EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];", 19731 format("auto a = new unique_ptr<int > [10];", Spaces)); 19732 } 19733 19734 TEST_F(FormatTest, ArrayAsTemplateType) { 19735 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;", 19736 format("auto a = unique_ptr < Foo < Bar>[ 10]> ;")); 19737 19738 FormatStyle Spaces = getLLVMStyle(); 19739 Spaces.SpacesInSquareBrackets = true; 19740 EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;", 19741 format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces)); 19742 } 19743 19744 TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); } 19745 19746 TEST(FormatStyle, GetStyleWithEmptyFileName) { 19747 llvm::vfs::InMemoryFileSystem FS; 19748 auto Style1 = getStyle("file", "", "Google", "", &FS); 19749 ASSERT_TRUE((bool)Style1); 19750 ASSERT_EQ(*Style1, getGoogleStyle()); 19751 } 19752 19753 TEST(FormatStyle, GetStyleOfFile) { 19754 llvm::vfs::InMemoryFileSystem FS; 19755 // Test 1: format file in the same directory. 19756 ASSERT_TRUE( 19757 FS.addFile("/a/.clang-format", 0, 19758 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); 19759 ASSERT_TRUE( 19760 FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 19761 auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS); 19762 ASSERT_TRUE((bool)Style1); 19763 ASSERT_EQ(*Style1, getLLVMStyle()); 19764 19765 // Test 2.1: fallback to default. 19766 ASSERT_TRUE( 19767 FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 19768 auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS); 19769 ASSERT_TRUE((bool)Style2); 19770 ASSERT_EQ(*Style2, getMozillaStyle()); 19771 19772 // Test 2.2: no format on 'none' fallback style. 19773 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 19774 ASSERT_TRUE((bool)Style2); 19775 ASSERT_EQ(*Style2, getNoStyle()); 19776 19777 // Test 2.3: format if config is found with no based style while fallback is 19778 // 'none'. 19779 ASSERT_TRUE(FS.addFile("/b/.clang-format", 0, 19780 llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2"))); 19781 Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS); 19782 ASSERT_TRUE((bool)Style2); 19783 ASSERT_EQ(*Style2, getLLVMStyle()); 19784 19785 // Test 2.4: format if yaml with no based style, while fallback is 'none'. 19786 Style2 = getStyle("{}", "a.h", "none", "", &FS); 19787 ASSERT_TRUE((bool)Style2); 19788 ASSERT_EQ(*Style2, getLLVMStyle()); 19789 19790 // Test 3: format file in parent directory. 19791 ASSERT_TRUE( 19792 FS.addFile("/c/.clang-format", 0, 19793 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); 19794 ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, 19795 llvm::MemoryBuffer::getMemBuffer("int i;"))); 19796 auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS); 19797 ASSERT_TRUE((bool)Style3); 19798 ASSERT_EQ(*Style3, getGoogleStyle()); 19799 19800 // Test 4: error on invalid fallback style 19801 auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS); 19802 ASSERT_FALSE((bool)Style4); 19803 llvm::consumeError(Style4.takeError()); 19804 19805 // Test 5: error on invalid yaml on command line 19806 auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS); 19807 ASSERT_FALSE((bool)Style5); 19808 llvm::consumeError(Style5.takeError()); 19809 19810 // Test 6: error on invalid style 19811 auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS); 19812 ASSERT_FALSE((bool)Style6); 19813 llvm::consumeError(Style6.takeError()); 19814 19815 // Test 7: found config file, error on parsing it 19816 ASSERT_TRUE( 19817 FS.addFile("/d/.clang-format", 0, 19818 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n" 19819 "InvalidKey: InvalidValue"))); 19820 ASSERT_TRUE( 19821 FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); 19822 auto Style7a = getStyle("file", "/d/.clang-format", "LLVM", "", &FS); 19823 ASSERT_FALSE((bool)Style7a); 19824 llvm::consumeError(Style7a.takeError()); 19825 19826 auto Style7b = getStyle("file", "/d/.clang-format", "LLVM", "", &FS, true); 19827 ASSERT_TRUE((bool)Style7b); 19828 19829 // Test 8: inferred per-language defaults apply. 19830 auto StyleTd = getStyle("file", "x.td", "llvm", "", &FS); 19831 ASSERT_TRUE((bool)StyleTd); 19832 ASSERT_EQ(*StyleTd, getLLVMStyle(FormatStyle::LK_TableGen)); 19833 19834 // Test 9.1: overwriting a file style, when parent no file exists with no 19835 // fallback style 19836 ASSERT_TRUE(FS.addFile( 19837 "/e/sub/.clang-format", 0, 19838 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: InheritParentConfig\n" 19839 "ColumnLimit: 20"))); 19840 ASSERT_TRUE(FS.addFile("/e/sub/code.cpp", 0, 19841 llvm::MemoryBuffer::getMemBuffer("int i;"))); 19842 auto Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS); 19843 ASSERT_TRUE(static_cast<bool>(Style9)); 19844 ASSERT_EQ(*Style9, [] { 19845 auto Style = getNoStyle(); 19846 Style.ColumnLimit = 20; 19847 return Style; 19848 }()); 19849 19850 // Test 9.2: with LLVM fallback style 19851 Style9 = getStyle("file", "/e/sub/code.cpp", "LLVM", "", &FS); 19852 ASSERT_TRUE(static_cast<bool>(Style9)); 19853 ASSERT_EQ(*Style9, [] { 19854 auto Style = getLLVMStyle(); 19855 Style.ColumnLimit = 20; 19856 return Style; 19857 }()); 19858 19859 // Test 9.3: with a parent file 19860 ASSERT_TRUE( 19861 FS.addFile("/e/.clang-format", 0, 19862 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google\n" 19863 "UseTab: Always"))); 19864 Style9 = getStyle("file", "/e/sub/code.cpp", "none", "", &FS); 19865 ASSERT_TRUE(static_cast<bool>(Style9)); 19866 ASSERT_EQ(*Style9, [] { 19867 auto Style = getGoogleStyle(); 19868 Style.ColumnLimit = 20; 19869 Style.UseTab = FormatStyle::UT_Always; 19870 return Style; 19871 }()); 19872 19873 // Test 9.4: propagate more than one level 19874 ASSERT_TRUE(FS.addFile("/e/sub/sub/code.cpp", 0, 19875 llvm::MemoryBuffer::getMemBuffer("int i;"))); 19876 ASSERT_TRUE(FS.addFile("/e/sub/sub/.clang-format", 0, 19877 llvm::MemoryBuffer::getMemBuffer( 19878 "BasedOnStyle: InheritParentConfig\n" 19879 "WhitespaceSensitiveMacros: ['FOO', 'BAR']"))); 19880 std::vector<std::string> NonDefaultWhiteSpaceMacros{"FOO", "BAR"}; 19881 19882 const auto SubSubStyle = [&NonDefaultWhiteSpaceMacros] { 19883 auto Style = getGoogleStyle(); 19884 Style.ColumnLimit = 20; 19885 Style.UseTab = FormatStyle::UT_Always; 19886 Style.WhitespaceSensitiveMacros = NonDefaultWhiteSpaceMacros; 19887 return Style; 19888 }(); 19889 19890 ASSERT_NE(Style9->WhitespaceSensitiveMacros, NonDefaultWhiteSpaceMacros); 19891 Style9 = getStyle("file", "/e/sub/sub/code.cpp", "none", "", &FS); 19892 ASSERT_TRUE(static_cast<bool>(Style9)); 19893 ASSERT_EQ(*Style9, SubSubStyle); 19894 19895 // Test 9.5: use InheritParentConfig as style name 19896 Style9 = 19897 getStyle("inheritparentconfig", "/e/sub/sub/code.cpp", "none", "", &FS); 19898 ASSERT_TRUE(static_cast<bool>(Style9)); 19899 ASSERT_EQ(*Style9, SubSubStyle); 19900 19901 // Test 9.6: use command line style with inheritance 19902 Style9 = getStyle("{BasedOnStyle: InheritParentConfig}", "/e/sub/code.cpp", 19903 "none", "", &FS); 19904 ASSERT_TRUE(static_cast<bool>(Style9)); 19905 ASSERT_EQ(*Style9, SubSubStyle); 19906 19907 // Test 9.7: use command line style with inheritance and own config 19908 Style9 = getStyle("{BasedOnStyle: InheritParentConfig, " 19909 "WhitespaceSensitiveMacros: ['FOO', 'BAR']}", 19910 "/e/sub/code.cpp", "none", "", &FS); 19911 ASSERT_TRUE(static_cast<bool>(Style9)); 19912 ASSERT_EQ(*Style9, SubSubStyle); 19913 19914 // Test 9.8: use inheritance from a file without BasedOnStyle 19915 ASSERT_TRUE(FS.addFile("/e/withoutbase/.clang-format", 0, 19916 llvm::MemoryBuffer::getMemBuffer("ColumnLimit: 123"))); 19917 ASSERT_TRUE( 19918 FS.addFile("/e/withoutbase/sub/.clang-format", 0, 19919 llvm::MemoryBuffer::getMemBuffer( 19920 "BasedOnStyle: InheritParentConfig\nIndentWidth: 7"))); 19921 // Make sure we do not use the fallback style 19922 Style9 = getStyle("file", "/e/withoutbase/code.cpp", "google", "", &FS); 19923 ASSERT_TRUE(static_cast<bool>(Style9)); 19924 ASSERT_EQ(*Style9, [] { 19925 auto Style = getLLVMStyle(); 19926 Style.ColumnLimit = 123; 19927 return Style; 19928 }()); 19929 19930 Style9 = getStyle("file", "/e/withoutbase/sub/code.cpp", "google", "", &FS); 19931 ASSERT_TRUE(static_cast<bool>(Style9)); 19932 ASSERT_EQ(*Style9, [] { 19933 auto Style = getLLVMStyle(); 19934 Style.ColumnLimit = 123; 19935 Style.IndentWidth = 7; 19936 return Style; 19937 }()); 19938 } 19939 19940 TEST_F(ReplacementTest, FormatCodeAfterReplacements) { 19941 // Column limit is 20. 19942 std::string Code = "Type *a =\n" 19943 " new Type();\n" 19944 "g(iiiii, 0, jjjjj,\n" 19945 " 0, kkkkk, 0, mm);\n" 19946 "int bad = format ;"; 19947 std::string Expected = "auto a = new Type();\n" 19948 "g(iiiii, nullptr,\n" 19949 " jjjjj, nullptr,\n" 19950 " kkkkk, nullptr,\n" 19951 " mm);\n" 19952 "int bad = format ;"; 19953 FileID ID = Context.createInMemoryFile("format.cpp", Code); 19954 tooling::Replacements Replaces = toReplacements( 19955 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6, 19956 "auto "), 19957 tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1, 19958 "nullptr"), 19959 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1, 19960 "nullptr"), 19961 tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1, 19962 "nullptr")}); 19963 19964 format::FormatStyle Style = format::getLLVMStyle(); 19965 Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility. 19966 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 19967 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 19968 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 19969 auto Result = applyAllReplacements(Code, *FormattedReplaces); 19970 EXPECT_TRUE(static_cast<bool>(Result)); 19971 EXPECT_EQ(Expected, *Result); 19972 } 19973 19974 TEST_F(ReplacementTest, SortIncludesAfterReplacement) { 19975 std::string Code = "#include \"a.h\"\n" 19976 "#include \"c.h\"\n" 19977 "\n" 19978 "int main() {\n" 19979 " return 0;\n" 19980 "}"; 19981 std::string Expected = "#include \"a.h\"\n" 19982 "#include \"b.h\"\n" 19983 "#include \"c.h\"\n" 19984 "\n" 19985 "int main() {\n" 19986 " return 0;\n" 19987 "}"; 19988 FileID ID = Context.createInMemoryFile("fix.cpp", Code); 19989 tooling::Replacements Replaces = toReplacements( 19990 {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0, 19991 "#include \"b.h\"\n")}); 19992 19993 format::FormatStyle Style = format::getLLVMStyle(); 19994 Style.SortIncludes = FormatStyle::SI_CaseSensitive; 19995 auto FormattedReplaces = formatReplacements(Code, Replaces, Style); 19996 EXPECT_TRUE(static_cast<bool>(FormattedReplaces)) 19997 << llvm::toString(FormattedReplaces.takeError()) << "\n"; 19998 auto Result = applyAllReplacements(Code, *FormattedReplaces); 19999 EXPECT_TRUE(static_cast<bool>(Result)); 20000 EXPECT_EQ(Expected, *Result); 20001 } 20002 20003 TEST_F(FormatTest, FormatSortsUsingDeclarations) { 20004 EXPECT_EQ("using std::cin;\n" 20005 "using std::cout;", 20006 format("using std::cout;\n" 20007 "using std::cin;", 20008 getGoogleStyle())); 20009 } 20010 20011 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) { 20012 format::FormatStyle Style = format::getLLVMStyle(); 20013 Style.Standard = FormatStyle::LS_Cpp03; 20014 // cpp03 recognize this string as identifier u8 and literal character 'a' 20015 EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style)); 20016 } 20017 20018 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) { 20019 // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers 20020 // all modes, including C++11, C++14 and C++17 20021 EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';")); 20022 } 20023 20024 TEST_F(FormatTest, DoNotFormatLikelyXml) { 20025 EXPECT_EQ("<!-- ;> -->", format("<!-- ;> -->", getGoogleStyle())); 20026 EXPECT_EQ(" <!-- >; -->", format(" <!-- >; -->", getGoogleStyle())); 20027 } 20028 20029 TEST_F(FormatTest, StructuredBindings) { 20030 // Structured bindings is a C++17 feature. 20031 // all modes, including C++11, C++14 and C++17 20032 verifyFormat("auto [a, b] = f();"); 20033 EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();")); 20034 EXPECT_EQ("const auto [a, b] = f();", format("const auto[a, b] = f();")); 20035 EXPECT_EQ("auto const [a, b] = f();", format("auto const[a, b] = f();")); 20036 EXPECT_EQ("auto const volatile [a, b] = f();", 20037 format("auto const volatile[a, b] = f();")); 20038 EXPECT_EQ("auto [a, b, c] = f();", format("auto [ a , b,c ] = f();")); 20039 EXPECT_EQ("auto &[a, b, c] = f();", 20040 format("auto &[ a , b,c ] = f();")); 20041 EXPECT_EQ("auto &&[a, b, c] = f();", 20042 format("auto &&[ a , b,c ] = f();")); 20043 EXPECT_EQ("auto const &[a, b] = f();", format("auto const&[a, b] = f();")); 20044 EXPECT_EQ("auto const volatile &&[a, b] = f();", 20045 format("auto const volatile &&[a, b] = f();")); 20046 EXPECT_EQ("auto const &&[a, b] = f();", 20047 format("auto const && [a, b] = f();")); 20048 EXPECT_EQ("const auto &[a, b] = f();", 20049 format("const auto & [a, b] = f();")); 20050 EXPECT_EQ("const auto volatile &&[a, b] = f();", 20051 format("const auto volatile &&[a, b] = f();")); 20052 EXPECT_EQ("volatile const auto &&[a, b] = f();", 20053 format("volatile const auto &&[a, b] = f();")); 20054 EXPECT_EQ("const auto &&[a, b] = f();", 20055 format("const auto && [a, b] = f();")); 20056 20057 // Make sure we don't mistake structured bindings for lambdas. 20058 FormatStyle PointerMiddle = getLLVMStyle(); 20059 PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle; 20060 verifyFormat("auto [a1, b]{A * i};", getGoogleStyle()); 20061 verifyFormat("auto [a2, b]{A * i};", getLLVMStyle()); 20062 verifyFormat("auto [a3, b]{A * i};", PointerMiddle); 20063 verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle()); 20064 verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle()); 20065 verifyFormat("auto const [a3, b]{A * i};", PointerMiddle); 20066 verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle()); 20067 verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle()); 20068 verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle); 20069 verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle()); 20070 verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle()); 20071 verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle); 20072 20073 EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}", 20074 format("for (const auto && [a, b] : some_range) {\n}")); 20075 EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}", 20076 format("for (const auto & [a, b] : some_range) {\n}")); 20077 EXPECT_EQ("for (const auto [a, b] : some_range) {\n}", 20078 format("for (const auto[a, b] : some_range) {\n}")); 20079 EXPECT_EQ("auto [x, y](expr);", format("auto[x,y] (expr);")); 20080 EXPECT_EQ("auto &[x, y](expr);", format("auto & [x,y] (expr);")); 20081 EXPECT_EQ("auto &&[x, y](expr);", format("auto && [x,y] (expr);")); 20082 EXPECT_EQ("auto const &[x, y](expr);", 20083 format("auto const & [x,y] (expr);")); 20084 EXPECT_EQ("auto const &&[x, y](expr);", 20085 format("auto const && [x,y] (expr);")); 20086 EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y] {expr};")); 20087 EXPECT_EQ("auto const &[x, y]{expr};", 20088 format("auto const & [x,y] {expr};")); 20089 EXPECT_EQ("auto const &&[x, y]{expr};", 20090 format("auto const && [x,y] {expr};")); 20091 20092 format::FormatStyle Spaces = format::getLLVMStyle(); 20093 Spaces.SpacesInSquareBrackets = true; 20094 verifyFormat("auto [ a, b ] = f();", Spaces); 20095 verifyFormat("auto &&[ a, b ] = f();", Spaces); 20096 verifyFormat("auto &[ a, b ] = f();", Spaces); 20097 verifyFormat("auto const &&[ a, b ] = f();", Spaces); 20098 verifyFormat("auto const &[ a, b ] = f();", Spaces); 20099 } 20100 20101 TEST_F(FormatTest, FileAndCode) { 20102 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", "")); 20103 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", "")); 20104 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", "")); 20105 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "")); 20106 EXPECT_EQ(FormatStyle::LK_ObjC, 20107 guessLanguage("foo.h", "@interface Foo\n@end\n")); 20108 EXPECT_EQ( 20109 FormatStyle::LK_ObjC, 20110 guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }")); 20111 EXPECT_EQ(FormatStyle::LK_ObjC, 20112 guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))")); 20113 EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;")); 20114 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", "")); 20115 EXPECT_EQ(FormatStyle::LK_ObjC, 20116 guessLanguage("foo", "@interface Foo\n@end\n")); 20117 EXPECT_EQ(FormatStyle::LK_ObjC, 20118 guessLanguage("foo.h", "int DoStuff(CGRect rect);\n")); 20119 EXPECT_EQ( 20120 FormatStyle::LK_ObjC, 20121 guessLanguage("foo.h", 20122 "#define MY_POINT_MAKE(x, y) CGPointMake((x), (y));\n")); 20123 EXPECT_EQ( 20124 FormatStyle::LK_Cpp, 20125 guessLanguage("foo.h", "#define FOO(...) auto bar = [] __VA_ARGS__;")); 20126 } 20127 20128 TEST_F(FormatTest, GuessLanguageWithCpp11AttributeSpecifiers) { 20129 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[noreturn]];")); 20130 EXPECT_EQ(FormatStyle::LK_ObjC, 20131 guessLanguage("foo.h", "array[[calculator getIndex]];")); 20132 EXPECT_EQ(FormatStyle::LK_Cpp, 20133 guessLanguage("foo.h", "[[noreturn, deprecated(\"so sorry\")]];")); 20134 EXPECT_EQ( 20135 FormatStyle::LK_Cpp, 20136 guessLanguage("foo.h", "[[noreturn, deprecated(\"gone, sorry\")]];")); 20137 EXPECT_EQ(FormatStyle::LK_ObjC, 20138 guessLanguage("foo.h", "[[noreturn foo] bar];")); 20139 EXPECT_EQ(FormatStyle::LK_Cpp, 20140 guessLanguage("foo.h", "[[clang::fallthrough]];")); 20141 EXPECT_EQ(FormatStyle::LK_ObjC, 20142 guessLanguage("foo.h", "[[clang:fallthrough] foo];")); 20143 EXPECT_EQ(FormatStyle::LK_Cpp, 20144 guessLanguage("foo.h", "[[gsl::suppress(\"type\")]];")); 20145 EXPECT_EQ(FormatStyle::LK_Cpp, 20146 guessLanguage("foo.h", "[[using clang: fallthrough]];")); 20147 EXPECT_EQ(FormatStyle::LK_ObjC, 20148 guessLanguage("foo.h", "[[abusing clang:fallthrough] bar];")); 20149 EXPECT_EQ(FormatStyle::LK_Cpp, 20150 guessLanguage("foo.h", "[[using gsl: suppress(\"type\")]];")); 20151 EXPECT_EQ( 20152 FormatStyle::LK_Cpp, 20153 guessLanguage("foo.h", "for (auto &&[endpoint, stream] : streams_)")); 20154 EXPECT_EQ( 20155 FormatStyle::LK_Cpp, 20156 guessLanguage("foo.h", 20157 "[[clang::callable_when(\"unconsumed\", \"unknown\")]]")); 20158 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "[[foo::bar, ...]]")); 20159 } 20160 20161 TEST_F(FormatTest, GuessLanguageWithCaret) { 20162 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^);")); 20163 EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "FOO(^, Bar);")); 20164 EXPECT_EQ(FormatStyle::LK_ObjC, 20165 guessLanguage("foo.h", "int(^)(char, float);")); 20166 EXPECT_EQ(FormatStyle::LK_ObjC, 20167 guessLanguage("foo.h", "int(^foo)(char, float);")); 20168 EXPECT_EQ(FormatStyle::LK_ObjC, 20169 guessLanguage("foo.h", "int(^foo[10])(char, float);")); 20170 EXPECT_EQ(FormatStyle::LK_ObjC, 20171 guessLanguage("foo.h", "int(^foo[kNumEntries])(char, float);")); 20172 EXPECT_EQ( 20173 FormatStyle::LK_ObjC, 20174 guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);")); 20175 } 20176 20177 TEST_F(FormatTest, GuessLanguageWithPragmas) { 20178 EXPECT_EQ(FormatStyle::LK_Cpp, 20179 guessLanguage("foo.h", "__pragma(warning(disable:))")); 20180 EXPECT_EQ(FormatStyle::LK_Cpp, 20181 guessLanguage("foo.h", "#pragma(warning(disable:))")); 20182 EXPECT_EQ(FormatStyle::LK_Cpp, 20183 guessLanguage("foo.h", "_Pragma(warning(disable:))")); 20184 } 20185 20186 TEST_F(FormatTest, FormatsInlineAsmSymbolicNames) { 20187 // ASM symbolic names are identifiers that must be surrounded by [] without 20188 // space in between: 20189 // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#InputOperands 20190 20191 // Example from https://bugs.llvm.org/show_bug.cgi?id=45108. 20192 verifyFormat(R"(// 20193 asm volatile("mrs %x[result], FPCR" : [result] "=r"(result)); 20194 )"); 20195 20196 // A list of several ASM symbolic names. 20197 verifyFormat(R"(asm("mov %[e], %[d]" : [d] "=rm"(d), [e] "rm"(*e));)"); 20198 20199 // ASM symbolic names in inline ASM with inputs and outputs. 20200 verifyFormat(R"(// 20201 asm("cmoveq %1, %2, %[result]" 20202 : [result] "=r"(result) 20203 : "r"(test), "r"(new), "[result]"(old)); 20204 )"); 20205 20206 // ASM symbolic names in inline ASM with no outputs. 20207 verifyFormat(R"(asm("mov %[e], %[d]" : : [d] "=rm"(d), [e] "rm"(*e));)"); 20208 } 20209 20210 TEST_F(FormatTest, GuessedLanguageWithInlineAsmClobbers) { 20211 EXPECT_EQ(FormatStyle::LK_Cpp, 20212 guessLanguage("foo.h", "void f() {\n" 20213 " asm (\"mov %[e], %[d]\"\n" 20214 " : [d] \"=rm\" (d)\n" 20215 " [e] \"rm\" (*e));\n" 20216 "}")); 20217 EXPECT_EQ(FormatStyle::LK_Cpp, 20218 guessLanguage("foo.h", "void f() {\n" 20219 " _asm (\"mov %[e], %[d]\"\n" 20220 " : [d] \"=rm\" (d)\n" 20221 " [e] \"rm\" (*e));\n" 20222 "}")); 20223 EXPECT_EQ(FormatStyle::LK_Cpp, 20224 guessLanguage("foo.h", "void f() {\n" 20225 " __asm (\"mov %[e], %[d]\"\n" 20226 " : [d] \"=rm\" (d)\n" 20227 " [e] \"rm\" (*e));\n" 20228 "}")); 20229 EXPECT_EQ(FormatStyle::LK_Cpp, 20230 guessLanguage("foo.h", "void f() {\n" 20231 " __asm__ (\"mov %[e], %[d]\"\n" 20232 " : [d] \"=rm\" (d)\n" 20233 " [e] \"rm\" (*e));\n" 20234 "}")); 20235 EXPECT_EQ(FormatStyle::LK_Cpp, 20236 guessLanguage("foo.h", "void f() {\n" 20237 " asm (\"mov %[e], %[d]\"\n" 20238 " : [d] \"=rm\" (d),\n" 20239 " [e] \"rm\" (*e));\n" 20240 "}")); 20241 EXPECT_EQ(FormatStyle::LK_Cpp, 20242 guessLanguage("foo.h", "void f() {\n" 20243 " asm volatile (\"mov %[e], %[d]\"\n" 20244 " : [d] \"=rm\" (d)\n" 20245 " [e] \"rm\" (*e));\n" 20246 "}")); 20247 } 20248 20249 TEST_F(FormatTest, GuessLanguageWithChildLines) { 20250 EXPECT_EQ(FormatStyle::LK_Cpp, 20251 guessLanguage("foo.h", "#define FOO ({ std::string s; })")); 20252 EXPECT_EQ(FormatStyle::LK_ObjC, 20253 guessLanguage("foo.h", "#define FOO ({ NSString *s; })")); 20254 EXPECT_EQ( 20255 FormatStyle::LK_Cpp, 20256 guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })")); 20257 EXPECT_EQ( 20258 FormatStyle::LK_ObjC, 20259 guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })")); 20260 } 20261 20262 TEST_F(FormatTest, TypenameMacros) { 20263 std::vector<std::string> TypenameMacros = {"STACK_OF", "LIST", "TAILQ_ENTRY"}; 20264 20265 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=30353 20266 FormatStyle Google = getGoogleStyleWithColumns(0); 20267 Google.TypenameMacros = TypenameMacros; 20268 verifyFormat("struct foo {\n" 20269 " int bar;\n" 20270 " TAILQ_ENTRY(a) bleh;\n" 20271 "};", 20272 Google); 20273 20274 FormatStyle Macros = getLLVMStyle(); 20275 Macros.TypenameMacros = TypenameMacros; 20276 20277 verifyFormat("STACK_OF(int) a;", Macros); 20278 verifyFormat("STACK_OF(int) *a;", Macros); 20279 verifyFormat("STACK_OF(int const *) *a;", Macros); 20280 verifyFormat("STACK_OF(int *const) *a;", Macros); 20281 verifyFormat("STACK_OF(int, string) a;", Macros); 20282 verifyFormat("STACK_OF(LIST(int)) a;", Macros); 20283 verifyFormat("STACK_OF(LIST(int)) a, b;", Macros); 20284 verifyFormat("for (LIST(int) *a = NULL; a;) {\n}", Macros); 20285 verifyFormat("STACK_OF(int) f(LIST(int) *arg);", Macros); 20286 verifyFormat("vector<LIST(uint64_t) *attr> x;", Macros); 20287 verifyFormat("vector<LIST(uint64_t) *const> f(LIST(uint64_t) *arg);", Macros); 20288 20289 Macros.PointerAlignment = FormatStyle::PAS_Left; 20290 verifyFormat("STACK_OF(int)* a;", Macros); 20291 verifyFormat("STACK_OF(int*)* a;", Macros); 20292 verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros); 20293 verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros); 20294 verifyFormat("vector<STACK_OF(uint64_t)* attr> x;", Macros); 20295 } 20296 20297 TEST_F(FormatTest, AtomicQualifier) { 20298 // Check that we treate _Atomic as a type and not a function call 20299 FormatStyle Google = getGoogleStyleWithColumns(0); 20300 verifyFormat("struct foo {\n" 20301 " int a1;\n" 20302 " _Atomic(a) a2;\n" 20303 " _Atomic(_Atomic(int) *const) a3;\n" 20304 "};", 20305 Google); 20306 verifyFormat("_Atomic(uint64_t) a;"); 20307 verifyFormat("_Atomic(uint64_t) *a;"); 20308 verifyFormat("_Atomic(uint64_t const *) *a;"); 20309 verifyFormat("_Atomic(uint64_t *const) *a;"); 20310 verifyFormat("_Atomic(const uint64_t *) *a;"); 20311 verifyFormat("_Atomic(uint64_t) a;"); 20312 verifyFormat("_Atomic(_Atomic(uint64_t)) a;"); 20313 verifyFormat("_Atomic(_Atomic(uint64_t)) a, b;"); 20314 verifyFormat("for (_Atomic(uint64_t) *a = NULL; a;) {\n}"); 20315 verifyFormat("_Atomic(uint64_t) f(_Atomic(uint64_t) *arg);"); 20316 20317 verifyFormat("_Atomic(uint64_t) *s(InitValue);"); 20318 verifyFormat("_Atomic(uint64_t) *s{InitValue};"); 20319 FormatStyle Style = getLLVMStyle(); 20320 Style.PointerAlignment = FormatStyle::PAS_Left; 20321 verifyFormat("_Atomic(uint64_t)* s(InitValue);", Style); 20322 verifyFormat("_Atomic(uint64_t)* s{InitValue};", Style); 20323 verifyFormat("_Atomic(int)* a;", Style); 20324 verifyFormat("_Atomic(int*)* a;", Style); 20325 verifyFormat("vector<_Atomic(uint64_t)* attr> x;", Style); 20326 20327 Style.SpacesInCStyleCastParentheses = true; 20328 Style.SpacesInParentheses = false; 20329 verifyFormat("x = ( _Atomic(uint64_t) )*a;", Style); 20330 Style.SpacesInCStyleCastParentheses = false; 20331 Style.SpacesInParentheses = true; 20332 verifyFormat("x = (_Atomic( uint64_t ))*a;", Style); 20333 verifyFormat("x = (_Atomic( uint64_t ))&a;", Style); 20334 } 20335 20336 TEST_F(FormatTest, AmbersandInLamda) { 20337 // Test case reported in https://bugs.llvm.org/show_bug.cgi?id=41899 20338 FormatStyle AlignStyle = getLLVMStyle(); 20339 AlignStyle.PointerAlignment = FormatStyle::PAS_Left; 20340 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle); 20341 AlignStyle.PointerAlignment = FormatStyle::PAS_Right; 20342 verifyFormat("auto lambda = [&a = a]() { a = 2; };", AlignStyle); 20343 } 20344 20345 TEST_F(FormatTest, SpacesInConditionalStatement) { 20346 FormatStyle Spaces = getLLVMStyle(); 20347 Spaces.SpacesInConditionalStatement = true; 20348 verifyFormat("for ( int i = 0; i; i++ )\n continue;", Spaces); 20349 verifyFormat("if ( !a )\n return;", Spaces); 20350 verifyFormat("if ( a )\n return;", Spaces); 20351 verifyFormat("if constexpr ( a )\n return;", Spaces); 20352 verifyFormat("switch ( a )\ncase 1:\n return;", Spaces); 20353 verifyFormat("while ( a )\n return;", Spaces); 20354 verifyFormat("while ( (a && b) )\n return;", Spaces); 20355 verifyFormat("do {\n} while ( 1 != 0 );", Spaces); 20356 verifyFormat("try {\n} catch ( const std::exception & ) {\n}", Spaces); 20357 // Check that space on the left of "::" is inserted as expected at beginning 20358 // of condition. 20359 verifyFormat("while ( ::func() )\n return;", Spaces); 20360 } 20361 20362 TEST_F(FormatTest, AlternativeOperators) { 20363 // Test case for ensuring alternate operators are not 20364 // combined with their right most neighbour. 20365 verifyFormat("int a and b;"); 20366 verifyFormat("int a and_eq b;"); 20367 verifyFormat("int a bitand b;"); 20368 verifyFormat("int a bitor b;"); 20369 verifyFormat("int a compl b;"); 20370 verifyFormat("int a not b;"); 20371 verifyFormat("int a not_eq b;"); 20372 verifyFormat("int a or b;"); 20373 verifyFormat("int a xor b;"); 20374 verifyFormat("int a xor_eq b;"); 20375 verifyFormat("return this not_eq bitand other;"); 20376 verifyFormat("bool operator not_eq(const X bitand other)"); 20377 20378 verifyFormat("int a and 5;"); 20379 verifyFormat("int a and_eq 5;"); 20380 verifyFormat("int a bitand 5;"); 20381 verifyFormat("int a bitor 5;"); 20382 verifyFormat("int a compl 5;"); 20383 verifyFormat("int a not 5;"); 20384 verifyFormat("int a not_eq 5;"); 20385 verifyFormat("int a or 5;"); 20386 verifyFormat("int a xor 5;"); 20387 verifyFormat("int a xor_eq 5;"); 20388 20389 verifyFormat("int a compl(5);"); 20390 verifyFormat("int a not(5);"); 20391 20392 /* FIXME handle alternate tokens 20393 * https://en.cppreference.com/w/cpp/language/operator_alternative 20394 // alternative tokens 20395 verifyFormat("compl foo();"); // ~foo(); 20396 verifyFormat("foo() <%%>;"); // foo(); 20397 verifyFormat("void foo() <%%>;"); // void foo(){} 20398 verifyFormat("int a <:1:>;"); // int a[1];[ 20399 verifyFormat("%:define ABC abc"); // #define ABC abc 20400 verifyFormat("%:%:"); // ## 20401 */ 20402 } 20403 20404 TEST_F(FormatTest, STLWhileNotDefineChed) { 20405 verifyFormat("#if defined(while)\n" 20406 "#define while EMIT WARNING C4005\n" 20407 "#endif // while"); 20408 } 20409 20410 TEST_F(FormatTest, OperatorSpacing) { 20411 FormatStyle Style = getLLVMStyle(); 20412 Style.PointerAlignment = FormatStyle::PAS_Right; 20413 verifyFormat("Foo::operator*();", Style); 20414 verifyFormat("Foo::operator void *();", Style); 20415 verifyFormat("Foo::operator void **();", Style); 20416 verifyFormat("Foo::operator void *&();", Style); 20417 verifyFormat("Foo::operator void *&&();", Style); 20418 verifyFormat("Foo::operator void const *();", Style); 20419 verifyFormat("Foo::operator void const **();", Style); 20420 verifyFormat("Foo::operator void const *&();", Style); 20421 verifyFormat("Foo::operator void const *&&();", Style); 20422 verifyFormat("Foo::operator()(void *);", Style); 20423 verifyFormat("Foo::operator*(void *);", Style); 20424 verifyFormat("Foo::operator*();", Style); 20425 verifyFormat("Foo::operator**();", Style); 20426 verifyFormat("Foo::operator&();", Style); 20427 verifyFormat("Foo::operator<int> *();", Style); 20428 verifyFormat("Foo::operator<Foo> *();", Style); 20429 verifyFormat("Foo::operator<int> **();", Style); 20430 verifyFormat("Foo::operator<Foo> **();", Style); 20431 verifyFormat("Foo::operator<int> &();", Style); 20432 verifyFormat("Foo::operator<Foo> &();", Style); 20433 verifyFormat("Foo::operator<int> &&();", Style); 20434 verifyFormat("Foo::operator<Foo> &&();", Style); 20435 verifyFormat("Foo::operator<int> *&();", Style); 20436 verifyFormat("Foo::operator<Foo> *&();", Style); 20437 verifyFormat("Foo::operator<int> *&&();", Style); 20438 verifyFormat("Foo::operator<Foo> *&&();", Style); 20439 verifyFormat("operator*(int (*)(), class Foo);", Style); 20440 20441 verifyFormat("Foo::operator&();", Style); 20442 verifyFormat("Foo::operator void &();", Style); 20443 verifyFormat("Foo::operator void const &();", Style); 20444 verifyFormat("Foo::operator()(void &);", Style); 20445 verifyFormat("Foo::operator&(void &);", Style); 20446 verifyFormat("Foo::operator&();", Style); 20447 verifyFormat("operator&(int (&)(), class Foo);", Style); 20448 20449 verifyFormat("Foo::operator&&();", Style); 20450 verifyFormat("Foo::operator**();", Style); 20451 verifyFormat("Foo::operator void &&();", Style); 20452 verifyFormat("Foo::operator void const &&();", Style); 20453 verifyFormat("Foo::operator()(void &&);", Style); 20454 verifyFormat("Foo::operator&&(void &&);", Style); 20455 verifyFormat("Foo::operator&&();", Style); 20456 verifyFormat("operator&&(int(&&)(), class Foo);", Style); 20457 verifyFormat("operator const nsTArrayRight<E> &()", Style); 20458 verifyFormat("[[nodiscard]] operator const nsTArrayRight<E, Allocator> &()", 20459 Style); 20460 verifyFormat("operator void **()", Style); 20461 verifyFormat("operator const FooRight<Object> &()", Style); 20462 verifyFormat("operator const FooRight<Object> *()", Style); 20463 verifyFormat("operator const FooRight<Object> **()", Style); 20464 verifyFormat("operator const FooRight<Object> *&()", Style); 20465 verifyFormat("operator const FooRight<Object> *&&()", Style); 20466 20467 Style.PointerAlignment = FormatStyle::PAS_Left; 20468 verifyFormat("Foo::operator*();", Style); 20469 verifyFormat("Foo::operator**();", Style); 20470 verifyFormat("Foo::operator void*();", Style); 20471 verifyFormat("Foo::operator void**();", Style); 20472 verifyFormat("Foo::operator void*&();", Style); 20473 verifyFormat("Foo::operator void*&&();", Style); 20474 verifyFormat("Foo::operator void const*();", Style); 20475 verifyFormat("Foo::operator void const**();", Style); 20476 verifyFormat("Foo::operator void const*&();", Style); 20477 verifyFormat("Foo::operator void const*&&();", Style); 20478 verifyFormat("Foo::operator/*comment*/ void*();", Style); 20479 verifyFormat("Foo::operator/*a*/ const /*b*/ void*();", Style); 20480 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void*();", Style); 20481 verifyFormat("Foo::operator()(void*);", Style); 20482 verifyFormat("Foo::operator*(void*);", Style); 20483 verifyFormat("Foo::operator*();", Style); 20484 verifyFormat("Foo::operator<int>*();", Style); 20485 verifyFormat("Foo::operator<Foo>*();", Style); 20486 verifyFormat("Foo::operator<int>**();", Style); 20487 verifyFormat("Foo::operator<Foo>**();", Style); 20488 verifyFormat("Foo::operator<Foo>*&();", Style); 20489 verifyFormat("Foo::operator<int>&();", Style); 20490 verifyFormat("Foo::operator<Foo>&();", Style); 20491 verifyFormat("Foo::operator<int>&&();", Style); 20492 verifyFormat("Foo::operator<Foo>&&();", Style); 20493 verifyFormat("Foo::operator<int>*&();", Style); 20494 verifyFormat("Foo::operator<Foo>*&();", Style); 20495 verifyFormat("operator*(int (*)(), class Foo);", Style); 20496 20497 verifyFormat("Foo::operator&();", Style); 20498 verifyFormat("Foo::operator void&();", Style); 20499 verifyFormat("Foo::operator void const&();", Style); 20500 verifyFormat("Foo::operator/*comment*/ void&();", Style); 20501 verifyFormat("Foo::operator/*a*/ const /*b*/ void&();", Style); 20502 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&();", Style); 20503 verifyFormat("Foo::operator()(void&);", Style); 20504 verifyFormat("Foo::operator&(void&);", Style); 20505 verifyFormat("Foo::operator&();", Style); 20506 verifyFormat("operator&(int (&)(), class Foo);", Style); 20507 20508 verifyFormat("Foo::operator&&();", Style); 20509 verifyFormat("Foo::operator void&&();", Style); 20510 verifyFormat("Foo::operator void const&&();", Style); 20511 verifyFormat("Foo::operator/*comment*/ void&&();", Style); 20512 verifyFormat("Foo::operator/*a*/ const /*b*/ void&&();", Style); 20513 verifyFormat("Foo::operator/*a*/ volatile /*b*/ void&&();", Style); 20514 verifyFormat("Foo::operator()(void&&);", Style); 20515 verifyFormat("Foo::operator&&(void&&);", Style); 20516 verifyFormat("Foo::operator&&();", Style); 20517 verifyFormat("operator&&(int(&&)(), class Foo);", Style); 20518 verifyFormat("operator const nsTArrayLeft<E>&()", Style); 20519 verifyFormat("[[nodiscard]] operator const nsTArrayLeft<E, Allocator>&()", 20520 Style); 20521 verifyFormat("operator void**()", Style); 20522 verifyFormat("operator const FooLeft<Object>&()", Style); 20523 verifyFormat("operator const FooLeft<Object>*()", Style); 20524 verifyFormat("operator const FooLeft<Object>**()", Style); 20525 verifyFormat("operator const FooLeft<Object>*&()", Style); 20526 verifyFormat("operator const FooLeft<Object>*&&()", Style); 20527 20528 // PR45107 20529 verifyFormat("operator Vector<String>&();", Style); 20530 verifyFormat("operator const Vector<String>&();", Style); 20531 verifyFormat("operator foo::Bar*();", Style); 20532 verifyFormat("operator const Foo<X>::Bar<Y>*();", Style); 20533 verifyFormat("operator/*a*/ const /*b*/ Foo /*c*/<X> /*d*/ ::Bar<Y>*();", 20534 Style); 20535 20536 Style.PointerAlignment = FormatStyle::PAS_Middle; 20537 verifyFormat("Foo::operator*();", Style); 20538 verifyFormat("Foo::operator void *();", Style); 20539 verifyFormat("Foo::operator()(void *);", Style); 20540 verifyFormat("Foo::operator*(void *);", Style); 20541 verifyFormat("Foo::operator*();", Style); 20542 verifyFormat("operator*(int (*)(), class Foo);", Style); 20543 20544 verifyFormat("Foo::operator&();", Style); 20545 verifyFormat("Foo::operator void &();", Style); 20546 verifyFormat("Foo::operator void const &();", Style); 20547 verifyFormat("Foo::operator()(void &);", Style); 20548 verifyFormat("Foo::operator&(void &);", Style); 20549 verifyFormat("Foo::operator&();", Style); 20550 verifyFormat("operator&(int (&)(), class Foo);", Style); 20551 20552 verifyFormat("Foo::operator&&();", Style); 20553 verifyFormat("Foo::operator void &&();", Style); 20554 verifyFormat("Foo::operator void const &&();", Style); 20555 verifyFormat("Foo::operator()(void &&);", Style); 20556 verifyFormat("Foo::operator&&(void &&);", Style); 20557 verifyFormat("Foo::operator&&();", Style); 20558 verifyFormat("operator&&(int(&&)(), class Foo);", Style); 20559 } 20560 20561 TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) { 20562 FormatStyle Style = getLLVMStyle(); 20563 // PR46157 20564 verifyFormat("foo(operator+, -42);", Style); 20565 verifyFormat("foo(operator++, -42);", Style); 20566 verifyFormat("foo(operator--, -42);", Style); 20567 verifyFormat("foo(-42, operator--);", Style); 20568 verifyFormat("foo(-42, operator, );", Style); 20569 verifyFormat("foo(operator, , -42);", Style); 20570 } 20571 20572 TEST_F(FormatTest, WhitespaceSensitiveMacros) { 20573 FormatStyle Style = getLLVMStyle(); 20574 Style.WhitespaceSensitiveMacros.push_back("FOO"); 20575 20576 // Don't use the helpers here, since 'mess up' will change the whitespace 20577 // and these are all whitespace sensitive by definition 20578 EXPECT_EQ("FOO(String-ized&Messy+But(: :Still)=Intentional);", 20579 format("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style)); 20580 EXPECT_EQ( 20581 "FOO(String-ized&Messy+But\\(: :Still)=Intentional);", 20582 format("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style)); 20583 EXPECT_EQ("FOO(String-ized&Messy+But,: :Still=Intentional);", 20584 format("FOO(String-ized&Messy+But,: :Still=Intentional);", Style)); 20585 EXPECT_EQ("FOO(String-ized&Messy+But,: :\n" 20586 " Still=Intentional);", 20587 format("FOO(String-ized&Messy+But,: :\n" 20588 " Still=Intentional);", 20589 Style)); 20590 Style.AlignConsecutiveAssignments = FormatStyle::ACS_Consecutive; 20591 EXPECT_EQ("FOO(String-ized=&Messy+But,: :\n" 20592 " Still=Intentional);", 20593 format("FOO(String-ized=&Messy+But,: :\n" 20594 " Still=Intentional);", 20595 Style)); 20596 20597 Style.ColumnLimit = 21; 20598 EXPECT_EQ("FOO(String-ized&Messy+But: :Still=Intentional);", 20599 format("FOO(String-ized&Messy+But: :Still=Intentional);", Style)); 20600 } 20601 20602 TEST_F(FormatTest, VeryLongNamespaceCommentSplit) { 20603 // These tests are not in NamespaceFixer because that doesn't 20604 // test its interaction with line wrapping 20605 FormatStyle Style = getLLVMStyle(); 20606 Style.ColumnLimit = 80; 20607 verifyFormat("namespace {\n" 20608 "int i;\n" 20609 "int j;\n" 20610 "} // namespace", 20611 Style); 20612 20613 verifyFormat("namespace AAA {\n" 20614 "int i;\n" 20615 "int j;\n" 20616 "} // namespace AAA", 20617 Style); 20618 20619 EXPECT_EQ("namespace Averyveryveryverylongnamespace {\n" 20620 "int i;\n" 20621 "int j;\n" 20622 "} // namespace Averyveryveryverylongnamespace", 20623 format("namespace Averyveryveryverylongnamespace {\n" 20624 "int i;\n" 20625 "int j;\n" 20626 "}", 20627 Style)); 20628 20629 EXPECT_EQ( 20630 "namespace " 20631 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" 20632 " went::mad::now {\n" 20633 "int i;\n" 20634 "int j;\n" 20635 "} // namespace\n" 20636 " // " 20637 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" 20638 "went::mad::now", 20639 format("namespace " 20640 "would::it::save::you::a::lot::of::time::if_::i::" 20641 "just::gave::up::and_::went::mad::now {\n" 20642 "int i;\n" 20643 "int j;\n" 20644 "}", 20645 Style)); 20646 20647 // This used to duplicate the comment again and again on subsequent runs 20648 EXPECT_EQ( 20649 "namespace " 20650 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" 20651 " went::mad::now {\n" 20652 "int i;\n" 20653 "int j;\n" 20654 "} // namespace\n" 20655 " // " 20656 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" 20657 "went::mad::now", 20658 format("namespace " 20659 "would::it::save::you::a::lot::of::time::if_::i::" 20660 "just::gave::up::and_::went::mad::now {\n" 20661 "int i;\n" 20662 "int j;\n" 20663 "} // namespace\n" 20664 " // " 20665 "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::" 20666 "and_::went::mad::now", 20667 Style)); 20668 } 20669 20670 TEST_F(FormatTest, LikelyUnlikely) { 20671 FormatStyle Style = getLLVMStyle(); 20672 20673 verifyFormat("if (argc > 5) [[unlikely]] {\n" 20674 " return 29;\n" 20675 "}", 20676 Style); 20677 20678 verifyFormat("if (argc > 5) [[likely]] {\n" 20679 " return 29;\n" 20680 "}", 20681 Style); 20682 20683 verifyFormat("if (argc > 5) [[unlikely]] {\n" 20684 " return 29;\n" 20685 "} else [[likely]] {\n" 20686 " return 42;\n" 20687 "}\n", 20688 Style); 20689 20690 verifyFormat("if (argc > 5) [[unlikely]] {\n" 20691 " return 29;\n" 20692 "} else if (argc > 10) [[likely]] {\n" 20693 " return 99;\n" 20694 "} else {\n" 20695 " return 42;\n" 20696 "}\n", 20697 Style); 20698 20699 verifyFormat("if (argc > 5) [[gnu::unused]] {\n" 20700 " return 29;\n" 20701 "}", 20702 Style); 20703 } 20704 20705 TEST_F(FormatTest, PenaltyIndentedWhitespace) { 20706 verifyFormat("Constructor()\n" 20707 " : aaaaaa(aaaaaa), aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 20708 " aaaa(aaaaaaaaaaaaaaaaaa, " 20709 "aaaaaaaaaaaaaaaaaat))"); 20710 verifyFormat("Constructor()\n" 20711 " : aaaaaaaaaaaaa(aaaaaa), " 20712 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)"); 20713 20714 FormatStyle StyleWithWhitespacePenalty = getLLVMStyle(); 20715 StyleWithWhitespacePenalty.PenaltyIndentedWhitespace = 5; 20716 verifyFormat("Constructor()\n" 20717 " : aaaaaa(aaaaaa),\n" 20718 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" 20719 " aaaa(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaat))", 20720 StyleWithWhitespacePenalty); 20721 verifyFormat("Constructor()\n" 20722 " : aaaaaaaaaaaaa(aaaaaa), " 20723 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa)", 20724 StyleWithWhitespacePenalty); 20725 } 20726 20727 TEST_F(FormatTest, LLVMDefaultStyle) { 20728 FormatStyle Style = getLLVMStyle(); 20729 verifyFormat("extern \"C\" {\n" 20730 "int foo();\n" 20731 "}", 20732 Style); 20733 } 20734 TEST_F(FormatTest, GNUDefaultStyle) { 20735 FormatStyle Style = getGNUStyle(); 20736 verifyFormat("extern \"C\"\n" 20737 "{\n" 20738 " int foo ();\n" 20739 "}", 20740 Style); 20741 } 20742 TEST_F(FormatTest, MozillaDefaultStyle) { 20743 FormatStyle Style = getMozillaStyle(); 20744 verifyFormat("extern \"C\"\n" 20745 "{\n" 20746 " int foo();\n" 20747 "}", 20748 Style); 20749 } 20750 TEST_F(FormatTest, GoogleDefaultStyle) { 20751 FormatStyle Style = getGoogleStyle(); 20752 verifyFormat("extern \"C\" {\n" 20753 "int foo();\n" 20754 "}", 20755 Style); 20756 } 20757 TEST_F(FormatTest, ChromiumDefaultStyle) { 20758 FormatStyle Style = getChromiumStyle(FormatStyle::LanguageKind::LK_Cpp); 20759 verifyFormat("extern \"C\" {\n" 20760 "int foo();\n" 20761 "}", 20762 Style); 20763 } 20764 TEST_F(FormatTest, MicrosoftDefaultStyle) { 20765 FormatStyle Style = getMicrosoftStyle(FormatStyle::LanguageKind::LK_Cpp); 20766 verifyFormat("extern \"C\"\n" 20767 "{\n" 20768 " int foo();\n" 20769 "}", 20770 Style); 20771 } 20772 TEST_F(FormatTest, WebKitDefaultStyle) { 20773 FormatStyle Style = getWebKitStyle(); 20774 verifyFormat("extern \"C\" {\n" 20775 "int foo();\n" 20776 "}", 20777 Style); 20778 } 20779 20780 TEST_F(FormatTest, ConceptsAndRequires) { 20781 FormatStyle Style = getLLVMStyle(); 20782 Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; 20783 20784 verifyFormat("template <typename T>\n" 20785 "concept Hashable = requires(T a) {\n" 20786 " { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n" 20787 "};", 20788 Style); 20789 verifyFormat("template <typename T>\n" 20790 "concept EqualityComparable = requires(T a, T b) {\n" 20791 " { a == b } -> bool;\n" 20792 "};", 20793 Style); 20794 verifyFormat("template <typename T>\n" 20795 "concept EqualityComparable = requires(T a, T b) {\n" 20796 " { a == b } -> bool;\n" 20797 " { a != b } -> bool;\n" 20798 "};", 20799 Style); 20800 verifyFormat("template <typename T>\n" 20801 "concept EqualityComparable = requires(T a, T b) {\n" 20802 " { a == b } -> bool;\n" 20803 " { a != b } -> bool;\n" 20804 "};", 20805 Style); 20806 20807 verifyFormat("template <typename It>\n" 20808 "requires Iterator<It>\n" 20809 "void sort(It begin, It end) {\n" 20810 " //....\n" 20811 "}", 20812 Style); 20813 20814 verifyFormat("template <typename T>\n" 20815 "concept Large = sizeof(T) > 10;", 20816 Style); 20817 20818 verifyFormat("template <typename T, typename U>\n" 20819 "concept FooableWith = requires(T t, U u) {\n" 20820 " typename T::foo_type;\n" 20821 " { t.foo(u) } -> typename T::foo_type;\n" 20822 " t++;\n" 20823 "};\n" 20824 "void doFoo(FooableWith<int> auto t) {\n" 20825 " t.foo(3);\n" 20826 "}", 20827 Style); 20828 verifyFormat("template <typename T>\n" 20829 "concept Context = sizeof(T) == 1;", 20830 Style); 20831 verifyFormat("template <typename T>\n" 20832 "concept Context = is_specialization_of_v<context, T>;", 20833 Style); 20834 verifyFormat("template <typename T>\n" 20835 "concept Node = std::is_object_v<T>;", 20836 Style); 20837 verifyFormat("template <typename T>\n" 20838 "concept Tree = true;", 20839 Style); 20840 20841 verifyFormat("template <typename T> int g(T i) requires Concept1<I> {\n" 20842 " //...\n" 20843 "}", 20844 Style); 20845 20846 verifyFormat( 20847 "template <typename T> int g(T i) requires Concept1<I> && Concept2<I> {\n" 20848 " //...\n" 20849 "}", 20850 Style); 20851 20852 verifyFormat( 20853 "template <typename T> int g(T i) requires Concept1<I> || Concept2<I> {\n" 20854 " //...\n" 20855 "}", 20856 Style); 20857 20858 verifyFormat("template <typename T>\n" 20859 "veryveryvery_long_return_type g(T i) requires Concept1<I> || " 20860 "Concept2<I> {\n" 20861 " //...\n" 20862 "}", 20863 Style); 20864 20865 verifyFormat("template <typename T>\n" 20866 "veryveryvery_long_return_type g(T i) requires Concept1<I> && " 20867 "Concept2<I> {\n" 20868 " //...\n" 20869 "}", 20870 Style); 20871 20872 verifyFormat( 20873 "template <typename T>\n" 20874 "veryveryvery_long_return_type g(T i) requires Concept1 && Concept2 {\n" 20875 " //...\n" 20876 "}", 20877 Style); 20878 20879 verifyFormat( 20880 "template <typename T>\n" 20881 "veryveryvery_long_return_type g(T i) requires Concept1 || Concept2 {\n" 20882 " //...\n" 20883 "}", 20884 Style); 20885 20886 verifyFormat("template <typename It>\n" 20887 "requires Foo<It>() && Bar<It> {\n" 20888 " //....\n" 20889 "}", 20890 Style); 20891 20892 verifyFormat("template <typename It>\n" 20893 "requires Foo<Bar<It>>() && Bar<Foo<It, It>> {\n" 20894 " //....\n" 20895 "}", 20896 Style); 20897 20898 verifyFormat("template <typename It>\n" 20899 "requires Foo<Bar<It, It>>() && Bar<Foo<It, It>> {\n" 20900 " //....\n" 20901 "}", 20902 Style); 20903 20904 verifyFormat( 20905 "template <typename It>\n" 20906 "requires Foo<Bar<It>, Baz<It>>() && Bar<Foo<It>, Baz<It, It>> {\n" 20907 " //....\n" 20908 "}", 20909 Style); 20910 20911 Style.IndentRequires = true; 20912 verifyFormat("template <typename It>\n" 20913 " requires Iterator<It>\n" 20914 "void sort(It begin, It end) {\n" 20915 " //....\n" 20916 "}", 20917 Style); 20918 verifyFormat("template <std::size index_>\n" 20919 " requires(index_ < sizeof...(Children_))\n" 20920 "Tree auto &child() {\n" 20921 " // ...\n" 20922 "}", 20923 Style); 20924 20925 Style.SpaceBeforeParens = FormatStyle::SBPO_Always; 20926 verifyFormat("template <typename T>\n" 20927 "concept Hashable = requires (T a) {\n" 20928 " { std::hash<T>{}(a) } -> std::convertible_to<std::size_t>;\n" 20929 "};", 20930 Style); 20931 20932 verifyFormat("template <class T = void>\n" 20933 " requires EqualityComparable<T> || Same<T, void>\n" 20934 "struct equal_to;", 20935 Style); 20936 20937 verifyFormat("template <class T>\n" 20938 " requires requires {\n" 20939 " T{};\n" 20940 " T (int);\n" 20941 " }\n", 20942 Style); 20943 20944 Style.ColumnLimit = 78; 20945 verifyFormat("template <typename T>\n" 20946 "concept Context = Traits<typename T::traits_type> and\n" 20947 " Interface<typename T::interface_type> and\n" 20948 " Request<typename T::request_type> and\n" 20949 " Response<typename T::response_type> and\n" 20950 " ContextExtension<typename T::extension_type> and\n" 20951 " ::std::is_copy_constructable<T> and " 20952 "::std::is_move_constructable<T> and\n" 20953 " requires (T c) {\n" 20954 " { c.response; } -> Response;\n" 20955 "} and requires (T c) {\n" 20956 " { c.request; } -> Request;\n" 20957 "}\n", 20958 Style); 20959 20960 verifyFormat("template <typename T>\n" 20961 "concept Context = Traits<typename T::traits_type> or\n" 20962 " Interface<typename T::interface_type> or\n" 20963 " Request<typename T::request_type> or\n" 20964 " Response<typename T::response_type> or\n" 20965 " ContextExtension<typename T::extension_type> or\n" 20966 " ::std::is_copy_constructable<T> or " 20967 "::std::is_move_constructable<T> or\n" 20968 " requires (T c) {\n" 20969 " { c.response; } -> Response;\n" 20970 "} or requires (T c) {\n" 20971 " { c.request; } -> Request;\n" 20972 "}\n", 20973 Style); 20974 20975 verifyFormat("template <typename T>\n" 20976 "concept Context = Traits<typename T::traits_type> &&\n" 20977 " Interface<typename T::interface_type> &&\n" 20978 " Request<typename T::request_type> &&\n" 20979 " Response<typename T::response_type> &&\n" 20980 " ContextExtension<typename T::extension_type> &&\n" 20981 " ::std::is_copy_constructable<T> && " 20982 "::std::is_move_constructable<T> &&\n" 20983 " requires (T c) {\n" 20984 " { c.response; } -> Response;\n" 20985 "} && requires (T c) {\n" 20986 " { c.request; } -> Request;\n" 20987 "}\n", 20988 Style); 20989 20990 verifyFormat("template <typename T>\nconcept someConcept = Constraint1<T> && " 20991 "Constraint2<T>;"); 20992 20993 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 20994 Style.BraceWrapping.AfterFunction = true; 20995 Style.BraceWrapping.AfterClass = true; 20996 Style.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; 20997 Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon; 20998 verifyFormat("void Foo () requires (std::copyable<T>)\n" 20999 "{\n" 21000 " return\n" 21001 "}\n", 21002 Style); 21003 21004 verifyFormat("void Foo () requires std::copyable<T>\n" 21005 "{\n" 21006 " return\n" 21007 "}\n", 21008 Style); 21009 21010 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 21011 " requires (std::invocable<F, std::invoke_result_t<Args>...>)\n" 21012 "struct constant;", 21013 Style); 21014 21015 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 21016 " requires std::invocable<F, std::invoke_result_t<Args>...>\n" 21017 "struct constant;", 21018 Style); 21019 21020 verifyFormat("template <class T>\n" 21021 "class plane_with_very_very_very_long_name\n" 21022 "{\n" 21023 " constexpr plane_with_very_very_very_long_name () requires " 21024 "std::copyable<T>\n" 21025 " : plane_with_very_very_very_long_name (1)\n" 21026 " {\n" 21027 " }\n" 21028 "}\n", 21029 Style); 21030 21031 verifyFormat("template <class T>\n" 21032 "class plane_with_long_name\n" 21033 "{\n" 21034 " constexpr plane_with_long_name () requires std::copyable<T>\n" 21035 " : plane_with_long_name (1)\n" 21036 " {\n" 21037 " }\n" 21038 "}\n", 21039 Style); 21040 21041 Style.BreakBeforeConceptDeclarations = false; 21042 verifyFormat("template <typename T> concept Tree = true;", Style); 21043 21044 Style.IndentRequires = false; 21045 verifyFormat("template <std::semiregular F, std::semiregular... Args>\n" 21046 "requires (std::invocable<F, std::invoke_result_t<Args>...>) " 21047 "struct constant;", 21048 Style); 21049 } 21050 21051 TEST_F(FormatTest, StatementAttributeLikeMacros) { 21052 FormatStyle Style = getLLVMStyle(); 21053 StringRef Source = "void Foo::slot() {\n" 21054 " unsigned char MyChar = 'x';\n" 21055 " emit signal(MyChar);\n" 21056 " Q_EMIT signal(MyChar);\n" 21057 "}"; 21058 21059 EXPECT_EQ(Source, format(Source, Style)); 21060 21061 Style.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; 21062 EXPECT_EQ("void Foo::slot() {\n" 21063 " unsigned char MyChar = 'x';\n" 21064 " emit signal(MyChar);\n" 21065 " Q_EMIT signal(MyChar);\n" 21066 "}", 21067 format(Source, Style)); 21068 21069 Style.StatementAttributeLikeMacros.push_back("emit"); 21070 EXPECT_EQ(Source, format(Source, Style)); 21071 21072 Style.StatementAttributeLikeMacros = {}; 21073 EXPECT_EQ("void Foo::slot() {\n" 21074 " unsigned char MyChar = 'x';\n" 21075 " emit signal(MyChar);\n" 21076 " Q_EMIT signal(MyChar);\n" 21077 "}", 21078 format(Source, Style)); 21079 } 21080 21081 TEST_F(FormatTest, IndentAccessModifiers) { 21082 FormatStyle Style = getLLVMStyle(); 21083 Style.IndentAccessModifiers = true; 21084 // Members are *two* levels below the record; 21085 // Style.IndentWidth == 2, thus yielding a 4 spaces wide indentation. 21086 verifyFormat("class C {\n" 21087 " int i;\n" 21088 "};\n", 21089 Style); 21090 verifyFormat("union C {\n" 21091 " int i;\n" 21092 " unsigned u;\n" 21093 "};\n", 21094 Style); 21095 // Access modifiers should be indented one level below the record. 21096 verifyFormat("class C {\n" 21097 " public:\n" 21098 " int i;\n" 21099 "};\n", 21100 Style); 21101 verifyFormat("struct S {\n" 21102 " private:\n" 21103 " class C {\n" 21104 " int j;\n" 21105 "\n" 21106 " public:\n" 21107 " C();\n" 21108 " };\n" 21109 "\n" 21110 " public:\n" 21111 " int i;\n" 21112 "};\n", 21113 Style); 21114 // Enumerations are not records and should be unaffected. 21115 Style.AllowShortEnumsOnASingleLine = false; 21116 verifyFormat("enum class E\n" 21117 "{\n" 21118 " A,\n" 21119 " B\n" 21120 "};\n", 21121 Style); 21122 // Test with a different indentation width; 21123 // also proves that the result is Style.AccessModifierOffset agnostic. 21124 Style.IndentWidth = 3; 21125 verifyFormat("class C {\n" 21126 " public:\n" 21127 " int i;\n" 21128 "};\n", 21129 Style); 21130 } 21131 21132 TEST_F(FormatTest, LimitlessStringsAndComments) { 21133 auto Style = getLLVMStyleWithColumns(0); 21134 constexpr StringRef Code = 21135 "/**\n" 21136 " * This is a multiline comment with quite some long lines, at least for " 21137 "the LLVM Style.\n" 21138 " * We will redo this with strings and line comments. Just to check if " 21139 "everything is working.\n" 21140 " */\n" 21141 "bool foo() {\n" 21142 " /* Single line multi line comment. */\n" 21143 " const std::string String = \"This is a multiline string with quite " 21144 "some long lines, at least for the LLVM Style.\"\n" 21145 " \"We already did it with multi line " 21146 "comments, and we will do it with line comments. Just to check if " 21147 "everything is working.\";\n" 21148 " // This is a line comment (block) with quite some long lines, at " 21149 "least for the LLVM Style.\n" 21150 " // We already did this with multi line comments and strings. Just to " 21151 "check if everything is working.\n" 21152 " const std::string SmallString = \"Hello World\";\n" 21153 " // Small line comment\n" 21154 " return String.size() > SmallString.size();\n" 21155 "}"; 21156 EXPECT_EQ(Code, format(Code, Style)); 21157 } 21158 } // namespace 21159 } // namespace format 21160 } // namespace clang 21161